Import 2.4.0-test2pre7
[davej-history.git] / net / socket.c
blobb0978fb321dbfafa166635fc6b160686491eb04d
1 /*
2 * NET An implementation of the SOCKET network access protocol.
4 * Version: @(#)socket.c 1.1.93 18/02/95
6 * Authors: Orest Zborowski, <obz@Kodak.COM>
7 * Ross Biro, <bir7@leland.Stanford.Edu>
8 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
10 * Fixes:
11 * Anonymous : NOTSOCK/BADF cleanup. Error fix in
12 * shutdown()
13 * Alan Cox : verify_area() fixes
14 * Alan Cox : Removed DDI
15 * Jonathan Kamens : SOCK_DGRAM reconnect bug
16 * Alan Cox : Moved a load of checks to the very
17 * top level.
18 * Alan Cox : Move address structures to/from user
19 * mode above the protocol layers.
20 * Rob Janssen : Allow 0 length sends.
21 * Alan Cox : Asynchronous I/O support (cribbed from the
22 * tty drivers).
23 * Niibe Yutaka : Asynchronous I/O for writes (4.4BSD style)
24 * Jeff Uphoff : Made max number of sockets command-line
25 * configurable.
26 * Matti Aarnio : Made the number of sockets dynamic,
27 * to be allocated when needed, and mr.
28 * Uphoff's max is used as max to be
29 * allowed to allocate.
30 * Linus : Argh. removed all the socket allocation
31 * altogether: it's in the inode now.
32 * Alan Cox : Made sock_alloc()/sock_release() public
33 * for NetROM and future kernel nfsd type
34 * stuff.
35 * Alan Cox : sendmsg/recvmsg basics.
36 * Tom Dyas : Export net symbols.
37 * Marcin Dalecki : Fixed problems with CONFIG_NET="n".
38 * Alan Cox : Added thread locking to sys_* calls
39 * for sockets. May have errors at the
40 * moment.
41 * Kevin Buhr : Fixed the dumb errors in the above.
42 * Andi Kleen : Some small cleanups, optimizations,
43 * and fixed a copy_from_user() bug.
44 * Tigran Aivazian : sys_send(args) calls sys_sendto(args, NULL, 0)
45 * Tigran Aivazian : Made listen(2) backlog sanity checks
46 * protocol-independent
49 * This program is free software; you can redistribute it and/or
50 * modify it under the terms of the GNU General Public License
51 * as published by the Free Software Foundation; either version
52 * 2 of the License, or (at your option) any later version.
55 * This module is effectively the top level interface to the BSD socket
56 * paradigm.
60 #include <linux/config.h>
61 #include <linux/mm.h>
62 #include <linux/smp_lock.h>
63 #include <linux/socket.h>
64 #include <linux/file.h>
65 #include <linux/net.h>
66 #include <linux/interrupt.h>
67 #include <linux/netdevice.h>
68 #include <linux/proc_fs.h>
69 #include <linux/wanrouter.h>
70 #include <linux/init.h>
71 #include <linux/poll.h>
72 #include <linux/cache.h>
74 #if defined(CONFIG_KMOD) && defined(CONFIG_NET)
75 #include <linux/kmod.h>
76 #endif
78 #include <asm/uaccess.h>
80 #include <linux/inet.h>
81 #include <net/ip.h>
82 #include <net/sock.h>
83 #include <net/tcp.h>
84 #include <net/udp.h>
85 #include <net/scm.h>
86 #include <linux/netfilter.h>
88 static int sock_no_open(struct inode *irrelevant, struct file *dontcare);
89 static loff_t sock_lseek(struct file *file, loff_t offset, int whence);
90 static ssize_t sock_read(struct file *file, char *buf,
91 size_t size, loff_t *ppos);
92 static ssize_t sock_write(struct file *file, const char *buf,
93 size_t size, loff_t *ppos);
94 static int sock_mmap(struct file *file, struct vm_area_struct * vma);
96 static int sock_close(struct inode *inode, struct file *file);
97 static unsigned int sock_poll(struct file *file,
98 struct poll_table_struct *wait);
99 static int sock_ioctl(struct inode *inode, struct file *file,
100 unsigned int cmd, unsigned long arg);
101 static int sock_fasync(int fd, struct file *filp, int on);
102 static ssize_t sock_readv(struct file *file, const struct iovec *vector,
103 unsigned long count, loff_t *ppos);
104 static ssize_t sock_writev(struct file *file, const struct iovec *vector,
105 unsigned long count, loff_t *ppos);
109 * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
110 * in the operation structures but are done directly via the socketcall() multiplexor.
113 static struct file_operations socket_file_ops = {
114 llseek: sock_lseek,
115 read: sock_read,
116 write: sock_write,
117 poll: sock_poll,
118 ioctl: sock_ioctl,
119 mmap: sock_mmap,
120 open: sock_no_open, /* special open code to disallow open via /proc */
121 release: sock_close,
122 fasync: sock_fasync,
123 readv: sock_readv,
124 writev: sock_writev
128 * The protocol list. Each protocol is registered in here.
131 static struct net_proto_family *net_families[NPROTO];
133 #ifdef CONFIG_SMP
134 static atomic_t net_family_lockct = ATOMIC_INIT(0);
135 static spinlock_t net_family_lock = SPIN_LOCK_UNLOCKED;
137 /* The strategy is: modifications net_family vector are short, do not
138 sleep and veeery rare, but read access should be free of any exclusive
139 locks.
142 static void net_family_write_lock(void)
144 spin_lock(&net_family_lock);
145 while (atomic_read(&net_family_lockct) != 0) {
146 spin_unlock(&net_family_lock);
148 current->policy |= SCHED_YIELD;
149 schedule();
151 spin_lock(&net_family_lock);
155 static __inline__ void net_family_write_unlock(void)
157 spin_unlock(&net_family_lock);
160 static __inline__ void net_family_read_lock(void)
162 atomic_inc(&net_family_lockct);
163 spin_unlock_wait(&net_family_lock);
166 static __inline__ void net_family_read_unlock(void)
168 atomic_dec(&net_family_lockct);
171 #else
172 #define net_family_write_lock() do { } while(0)
173 #define net_family_write_unlock() do { } while(0)
174 #define net_family_read_lock() do { } while(0)
175 #define net_family_read_unlock() do { } while(0)
176 #endif
180 * Statistics counters of the socket lists
183 static union {
184 int counter;
185 char __pad[SMP_CACHE_BYTES];
186 } sockets_in_use[NR_CPUS] __cacheline_aligned = {{0}};
189 * Support routines. Move socket addresses back and forth across the kernel/user
190 * divide and look after the messy bits.
193 #define MAX_SOCK_ADDR 128 /* 108 for Unix domain -
194 16 for IP, 16 for IPX,
195 24 for IPv6,
196 about 80 for AX.25
197 must be at least one bigger than
198 the AF_UNIX size (see net/unix/af_unix.c
199 :unix_mkname()).
203 * move_addr_to_kernel - copy a socket address into kernel space
204 * @uaddr: Address in user space
205 * @kaddr: Address in kernel space
206 * @ulen: Length in user space
208 * The address is copied into kernel space. If the provided address is
209 * too long an error code of -EINVAL is returned. If the copy gives
210 * invalid addresses -EFAULT is returned. On a success 0 is returned.
213 int move_addr_to_kernel(void *uaddr, int ulen, void *kaddr)
215 if(ulen<0||ulen>MAX_SOCK_ADDR)
216 return -EINVAL;
217 if(ulen==0)
218 return 0;
219 if(copy_from_user(kaddr,uaddr,ulen))
220 return -EFAULT;
221 return 0;
225 * move_addr_to_user - copy an address to user space
226 * @kaddr: kernel space address
227 * @klen: length of address in kernel
228 * @uaddr: user space address
229 * @ulen: pointer to user length field
231 * The value pointed to by ulen on entry is the buffer length available.
232 * This is overwritten with the buffer space used. -EINVAL is returned
233 * if an overlong buffer is specified or a negative buffer size. -EFAULT
234 * is returned if either the buffer or the length field are not
235 * accessible.
236 * After copying the data up to the limit the user specifies, the true
237 * length of the data is written over the length limit the user
238 * specified. Zero is returned for a success.
241 int move_addr_to_user(void *kaddr, int klen, void *uaddr, int *ulen)
243 int err;
244 int len;
246 if((err=get_user(len, ulen)))
247 return err;
248 if(len>klen)
249 len=klen;
250 if(len<0 || len> MAX_SOCK_ADDR)
251 return -EINVAL;
252 if(len)
254 if(copy_to_user(uaddr,kaddr,len))
255 return -EFAULT;
258 * "fromlen shall refer to the value before truncation.."
259 * 1003.1g
261 return __put_user(klen, ulen);
265 * Obtains the first available file descriptor and sets it up for use.
267 * This functions creates file structure and maps it to fd space
268 * of current process. On success it returns file descriptor
269 * and file struct implicitly stored in sock->file.
270 * Note that another thread may close file descriptor before we return
271 * from this function. We use the fact that now we do not refer
272 * to socket after mapping. If one day we will need it, this
273 * function will inincrement ref. count on file by 1.
275 * In any case returned fd MAY BE not valid!
276 * This race condition is inavoidable
277 * with shared fd spaces, we cannot solve is inside kernel,
278 * but we take care of internal coherence yet.
281 static int sock_map_fd(struct socket *sock)
283 int fd;
286 * Find a file descriptor suitable for return to the user.
289 fd = get_unused_fd();
290 if (fd >= 0) {
291 struct file *file = get_empty_filp();
293 if (!file) {
294 put_unused_fd(fd);
295 fd = -ENFILE;
296 goto out;
299 file->f_dentry = d_alloc_root(sock->inode);
300 /* MOUNT_REWRITE: set to sockfs internal vfsmnt */
301 file->f_vfsmnt = NULL;
302 if (!file->f_dentry) {
303 put_filp(file);
304 put_unused_fd(fd);
305 fd = -ENOMEM;
306 goto out;
309 sock->file = file;
310 file->f_op = &socket_file_ops;
311 file->f_mode = 3;
312 file->f_flags = O_RDWR;
313 file->f_pos = 0;
314 fd_install(fd, file);
317 out:
318 return fd;
321 extern __inline__ struct socket *socki_lookup(struct inode *inode)
323 return &inode->u.socket_i;
327 * sockfd_lookup - Go from a file number to its socket slot
328 * @fd: file handle
329 * @err: pointer to an error code return
331 * The file handle passed in is locked and the socket it is bound
332 * too is returned. If an error occurs the err pointer is overwritten
333 * with a negative errno code and NULL is returned. The function checks
334 * for both invalid handles and passing a handle which is not a socket.
336 * On a success the socket object pointer is returned.
339 struct socket *sockfd_lookup(int fd, int *err)
341 struct file *file;
342 struct inode *inode;
343 struct socket *sock;
345 if (!(file = fget(fd)))
347 *err = -EBADF;
348 return NULL;
351 inode = file->f_dentry->d_inode;
352 if (!inode || !inode->i_sock || !(sock = socki_lookup(inode)))
354 *err = -ENOTSOCK;
355 fput(file);
356 return NULL;
359 if (sock->file != file) {
360 printk(KERN_ERR "socki_lookup: socket file changed!\n");
361 sock->file = file;
363 return sock;
366 extern __inline__ void sockfd_put(struct socket *sock)
368 fput(sock->file);
372 * sock_alloc - allocate a socket
374 * Allocate a new inode and socket object. The two are bound together
375 * and initialised. The socket is then returned. If we are out of inodes
376 * NULL is returned.
379 struct socket *sock_alloc(void)
381 struct inode * inode;
382 struct socket * sock;
384 inode = get_empty_inode();
385 if (!inode)
386 return NULL;
388 sock = socki_lookup(inode);
390 inode->i_mode = S_IFSOCK|S_IRWXUGO;
391 inode->i_sock = 1;
392 inode->i_uid = current->fsuid;
393 inode->i_gid = current->fsgid;
395 sock->inode = inode;
396 init_waitqueue_head(&sock->wait);
397 sock->fasync_list = NULL;
398 sock->state = SS_UNCONNECTED;
399 sock->flags = 0;
400 sock->ops = NULL;
401 sock->sk = NULL;
402 sock->file = NULL;
404 sockets_in_use[smp_processor_id()].counter++;
405 return sock;
409 * In theory you can't get an open on this inode, but /proc provides
410 * a back door. Remember to keep it shut otherwise you'll let the
411 * creepy crawlies in.
414 static int sock_no_open(struct inode *irrelevant, struct file *dontcare)
416 return -ENXIO;
420 * sock_release - close a socket
421 * @sock: socket to close
423 * The socket is released from the protocol stack if it has a release
424 * callback, and the inode is then released if the socket is bound to
425 * an inode not a file.
428 void sock_release(struct socket *sock)
430 if (sock->ops)
431 sock->ops->release(sock);
433 if (sock->fasync_list)
434 printk(KERN_ERR "sock_release: fasync list not empty!\n");
436 sockets_in_use[smp_processor_id()].counter--;
437 if (!sock->file) {
438 iput(sock->inode);
439 return;
441 sock->file=NULL;
444 int sock_sendmsg(struct socket *sock, struct msghdr *msg, int size)
446 int err;
447 struct scm_cookie scm;
449 err = scm_send(sock, msg, &scm);
450 if (err >= 0) {
451 err = sock->ops->sendmsg(sock, msg, size, &scm);
452 scm_destroy(&scm);
454 return err;
457 int sock_recvmsg(struct socket *sock, struct msghdr *msg, int size, int flags)
459 struct scm_cookie scm;
461 memset(&scm, 0, sizeof(scm));
463 size = sock->ops->recvmsg(sock, msg, size, flags, &scm);
464 if (size >= 0)
465 scm_recv(sock, msg, &scm, flags);
467 return size;
472 * Sockets are not seekable.
475 static loff_t sock_lseek(struct file *file, loff_t offset, int whence)
477 return -ESPIPE;
481 * Read data from a socket. ubuf is a user mode pointer. We make sure the user
482 * area ubuf...ubuf+size-1 is writable before asking the protocol.
485 static ssize_t sock_read(struct file *file, char *ubuf,
486 size_t size, loff_t *ppos)
488 struct socket *sock;
489 struct iovec iov;
490 struct msghdr msg;
491 int flags;
493 if (ppos != &file->f_pos)
494 return -ESPIPE;
495 if (size==0) /* Match SYS5 behaviour */
496 return 0;
498 sock = socki_lookup(file->f_dentry->d_inode);
500 msg.msg_name=NULL;
501 msg.msg_namelen=0;
502 msg.msg_iov=&iov;
503 msg.msg_iovlen=1;
504 msg.msg_control=NULL;
505 msg.msg_controllen=0;
506 iov.iov_base=ubuf;
507 iov.iov_len=size;
508 flags = !(file->f_flags & O_NONBLOCK) ? 0 : MSG_DONTWAIT;
510 return sock_recvmsg(sock, &msg, size, flags);
515 * Write data to a socket. We verify that the user area ubuf..ubuf+size-1
516 * is readable by the user process.
519 static ssize_t sock_write(struct file *file, const char *ubuf,
520 size_t size, loff_t *ppos)
522 struct socket *sock;
523 struct msghdr msg;
524 struct iovec iov;
526 if (ppos != &file->f_pos)
527 return -ESPIPE;
528 if(size==0) /* Match SYS5 behaviour */
529 return 0;
531 sock = socki_lookup(file->f_dentry->d_inode);
533 msg.msg_name=NULL;
534 msg.msg_namelen=0;
535 msg.msg_iov=&iov;
536 msg.msg_iovlen=1;
537 msg.msg_control=NULL;
538 msg.msg_controllen=0;
539 msg.msg_flags=!(file->f_flags & O_NONBLOCK) ? 0 : MSG_DONTWAIT;
540 if (sock->type == SOCK_SEQPACKET)
541 msg.msg_flags |= MSG_EOR;
542 iov.iov_base=(void *)ubuf;
543 iov.iov_len=size;
545 return sock_sendmsg(sock, &msg, size);
548 int sock_readv_writev(int type, struct inode * inode, struct file * file,
549 const struct iovec * iov, long count, long size)
551 struct msghdr msg;
552 struct socket *sock;
554 sock = socki_lookup(inode);
556 msg.msg_name = NULL;
557 msg.msg_namelen = 0;
558 msg.msg_control = NULL;
559 msg.msg_controllen = 0;
560 msg.msg_iov = (struct iovec *) iov;
561 msg.msg_iovlen = count;
562 msg.msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
564 /* read() does a VERIFY_WRITE */
565 if (type == VERIFY_WRITE)
566 return sock_recvmsg(sock, &msg, size, msg.msg_flags);
568 if (sock->type == SOCK_SEQPACKET)
569 msg.msg_flags |= MSG_EOR;
571 return sock_sendmsg(sock, &msg, size);
574 static ssize_t sock_readv(struct file *file, const struct iovec *vector,
575 unsigned long count, loff_t *ppos)
577 size_t tot_len = 0;
578 int i;
579 for (i = 0 ; i < count ; i++)
580 tot_len += vector[i].iov_len;
581 return sock_readv_writev(VERIFY_WRITE, file->f_dentry->d_inode,
582 file, vector, count, tot_len);
585 static ssize_t sock_writev(struct file *file, const struct iovec *vector,
586 unsigned long count, loff_t *ppos)
588 size_t tot_len = 0;
589 int i;
590 for (i = 0 ; i < count ; i++)
591 tot_len += vector[i].iov_len;
592 return sock_readv_writev(VERIFY_READ, file->f_dentry->d_inode,
593 file, vector, count, tot_len);
597 * With an ioctl arg may well be a user mode pointer, but we don't know what to do
598 * with it - that's up to the protocol still.
601 int sock_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
602 unsigned long arg)
604 struct socket *sock;
605 int err;
607 unlock_kernel();
608 sock = socki_lookup(inode);
609 err = sock->ops->ioctl(sock, cmd, arg);
610 lock_kernel();
612 return err;
616 /* No kernel lock held - perfect */
617 static unsigned int sock_poll(struct file *file, poll_table * wait)
619 struct socket *sock;
622 * We can't return errors to poll, so it's either yes or no.
624 sock = socki_lookup(file->f_dentry->d_inode);
625 return sock->ops->poll(file, sock, wait);
628 static int sock_mmap(struct file * file, struct vm_area_struct * vma)
630 struct socket *sock = socki_lookup(file->f_dentry->d_inode);
632 return sock->ops->mmap(file, sock, vma);
635 int sock_close(struct inode *inode, struct file *filp)
638 * It was possible the inode is NULL we were
639 * closing an unfinished socket.
642 if (!inode)
644 printk(KERN_DEBUG "sock_close: NULL inode\n");
645 return 0;
647 unlock_kernel();
648 sock_fasync(-1, filp, 0);
649 sock_release(socki_lookup(inode));
650 lock_kernel();
651 return 0;
655 * Update the socket async list
657 * Fasync_list locking strategy.
659 * 1. fasync_list is modified only under process context socket lock
660 * i.e. under semaphore.
661 * 2. fasync_list is used under read_lock(&sk->callback_lock)
662 * or under socket lock.
663 * 3. fasync_list can be used from softirq context, so that
664 * modification under socket lock have to be enhanced with
665 * write_lock_bh(&sk->callback_lock).
666 * --ANK (990710)
669 static int sock_fasync(int fd, struct file *filp, int on)
671 struct fasync_struct *fa, *fna=NULL, **prev;
672 struct socket *sock;
673 struct sock *sk;
675 if (on)
677 fna=(struct fasync_struct *)kmalloc(sizeof(struct fasync_struct), GFP_KERNEL);
678 if(fna==NULL)
679 return -ENOMEM;
683 sock = socki_lookup(filp->f_dentry->d_inode);
685 if ((sk=sock->sk) == NULL)
686 return -EINVAL;
688 lock_sock(sk);
690 prev=&(sock->fasync_list);
692 for (fa=*prev; fa!=NULL; prev=&fa->fa_next,fa=*prev)
693 if (fa->fa_file==filp)
694 break;
696 if(on)
698 if(fa!=NULL)
700 write_lock_bh(&sk->callback_lock);
701 fa->fa_fd=fd;
702 write_unlock_bh(&sk->callback_lock);
704 kfree_s(fna,sizeof(struct fasync_struct));
705 goto out;
707 fna->fa_file=filp;
708 fna->fa_fd=fd;
709 fna->magic=FASYNC_MAGIC;
710 fna->fa_next=sock->fasync_list;
711 write_lock_bh(&sk->callback_lock);
712 sock->fasync_list=fna;
713 write_unlock_bh(&sk->callback_lock);
715 else
717 if (fa!=NULL)
719 write_lock_bh(&sk->callback_lock);
720 *prev=fa->fa_next;
721 write_unlock_bh(&sk->callback_lock);
722 kfree_s(fa,sizeof(struct fasync_struct));
726 out:
727 release_sock(sock->sk);
728 return 0;
731 /* This function may be called only under socket lock or callback_lock */
733 int sock_wake_async(struct socket *sock, int how, int band)
735 if (!sock || !sock->fasync_list)
736 return -1;
737 switch (how)
739 case 1:
741 if (test_bit(SOCK_ASYNC_WAITDATA, &sock->flags))
742 break;
743 goto call_kill;
744 case 2:
745 if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags))
746 break;
747 /* fall through */
748 case 0:
749 call_kill:
750 __kill_fasync(sock->fasync_list, SIGIO, band);
751 break;
752 case 3:
753 __kill_fasync(sock->fasync_list, SIGURG, band);
755 return 0;
759 int sock_create(int family, int type, int protocol, struct socket **res)
761 int i;
762 struct socket *sock;
765 * Check protocol is in range
767 if(family<0 || family>=NPROTO)
768 return -EINVAL;
770 /* Compatibility.
772 This uglymoron is moved from INET layer to here to avoid
773 deadlock in module load.
775 if (family == PF_INET && type == SOCK_PACKET) {
776 static int warned;
777 if (!warned) {
778 warned = 1;
779 printk(KERN_INFO "%s uses obsolete (PF_INET,SOCK_PACKET)\n", current->comm);
781 family = PF_PACKET;
784 #if defined(CONFIG_KMOD) && defined(CONFIG_NET)
785 /* Attempt to load a protocol module if the find failed.
787 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
788 * requested real, full-featured networking support upon configuration.
789 * Otherwise module support will break!
791 if (net_families[family]==NULL)
793 char module_name[30];
794 sprintf(module_name,"net-pf-%d",family);
795 request_module(module_name);
797 #endif
799 net_family_read_lock();
800 if (net_families[family] == NULL) {
801 i = -EINVAL;
802 goto out;
806 * Allocate the socket and allow the family to set things up. if
807 * the protocol is 0, the family is instructed to select an appropriate
808 * default.
811 if (!(sock = sock_alloc()))
813 printk(KERN_WARNING "socket: no more sockets\n");
814 i = -ENFILE; /* Not exactly a match, but its the
815 closest posix thing */
816 goto out;
819 sock->type = type;
821 if ((i = net_families[family]->create(sock, protocol)) < 0)
823 sock_release(sock);
824 goto out;
827 *res = sock;
829 out:
830 net_family_read_unlock();
831 return i;
834 asmlinkage long sys_socket(int family, int type, int protocol)
836 int retval;
837 struct socket *sock;
839 retval = sock_create(family, type, protocol, &sock);
840 if (retval < 0)
841 goto out;
843 retval = sock_map_fd(sock);
844 if (retval < 0)
845 goto out_release;
847 out:
848 /* It may be already another descriptor 8) Not kernel problem. */
849 return retval;
851 out_release:
852 sock_release(sock);
853 return retval;
857 * Create a pair of connected sockets.
860 asmlinkage long sys_socketpair(int family, int type, int protocol, int usockvec[2])
862 struct socket *sock1, *sock2;
863 int fd1, fd2, err;
866 * Obtain the first socket and check if the underlying protocol
867 * supports the socketpair call.
870 err = sock_create(family, type, protocol, &sock1);
871 if (err < 0)
872 goto out;
874 err = sock_create(family, type, protocol, &sock2);
875 if (err < 0)
876 goto out_release_1;
878 err = sock1->ops->socketpair(sock1, sock2);
879 if (err < 0)
880 goto out_release_both;
882 fd1 = fd2 = -1;
884 err = sock_map_fd(sock1);
885 if (err < 0)
886 goto out_release_both;
887 fd1 = err;
889 err = sock_map_fd(sock2);
890 if (err < 0)
891 goto out_close_1;
892 fd2 = err;
894 /* fd1 and fd2 may be already another descriptors.
895 * Not kernel problem.
898 err = put_user(fd1, &usockvec[0]);
899 if (!err)
900 err = put_user(fd2, &usockvec[1]);
901 if (!err)
902 return 0;
904 sys_close(fd2);
905 sys_close(fd1);
906 return err;
908 out_close_1:
909 sock_release(sock2);
910 sys_close(fd1);
911 return err;
913 out_release_both:
914 sock_release(sock2);
915 out_release_1:
916 sock_release(sock1);
917 out:
918 return err;
923 * Bind a name to a socket. Nothing much to do here since it's
924 * the protocol's responsibility to handle the local address.
926 * We move the socket address to kernel space before we call
927 * the protocol layer (having also checked the address is ok).
930 asmlinkage long sys_bind(int fd, struct sockaddr *umyaddr, int addrlen)
932 struct socket *sock;
933 char address[MAX_SOCK_ADDR];
934 int err;
936 if((sock = sockfd_lookup(fd,&err))!=NULL)
938 if((err=move_addr_to_kernel(umyaddr,addrlen,address))>=0)
939 err = sock->ops->bind(sock, (struct sockaddr *)address, addrlen);
940 sockfd_put(sock);
942 return err;
947 * Perform a listen. Basically, we allow the protocol to do anything
948 * necessary for a listen, and if that works, we mark the socket as
949 * ready for listening.
952 asmlinkage long sys_listen(int fd, int backlog)
954 struct socket *sock;
955 int err;
957 if ((sock = sockfd_lookup(fd, &err)) != NULL) {
958 if ((unsigned) backlog > SOMAXCONN)
959 backlog = SOMAXCONN;
960 err=sock->ops->listen(sock, backlog);
961 sockfd_put(sock);
963 return err;
968 * For accept, we attempt to create a new socket, set up the link
969 * with the client, wake up the client, then return the new
970 * connected fd. We collect the address of the connector in kernel
971 * space and move it to user at the very end. This is unclean because
972 * we open the socket then return an error.
974 * 1003.1g adds the ability to recvmsg() to query connection pending
975 * status to recvmsg. We need to add that support in a way thats
976 * clean when we restucture accept also.
979 asmlinkage long sys_accept(int fd, struct sockaddr *upeer_sockaddr, int *upeer_addrlen)
981 struct socket *sock, *newsock;
982 int err, len;
983 char address[MAX_SOCK_ADDR];
985 sock = sockfd_lookup(fd, &err);
986 if (!sock)
987 goto out;
989 err = -EMFILE;
990 if (!(newsock = sock_alloc()))
991 goto out_put;
993 newsock->type = sock->type;
994 newsock->ops = sock->ops;
996 err = sock->ops->accept(sock, newsock, sock->file->f_flags);
997 if (err < 0)
998 goto out_release;
1000 if (upeer_sockaddr) {
1001 if(newsock->ops->getname(newsock, (struct sockaddr *)address, &len, 2)<0) {
1002 err = -ECONNABORTED;
1003 goto out_release;
1005 err = move_addr_to_user(address, len, upeer_sockaddr, upeer_addrlen);
1006 if (err < 0)
1007 goto out_release;
1010 /* File flags are not inherited via accept() unlike another OSes. */
1012 if ((err = sock_map_fd(newsock)) < 0)
1013 goto out_release;
1015 out_put:
1016 sockfd_put(sock);
1017 out:
1018 return err;
1020 out_release:
1021 sock_release(newsock);
1022 goto out_put;
1027 * Attempt to connect to a socket with the server address. The address
1028 * is in user space so we verify it is OK and move it to kernel space.
1030 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
1031 * break bindings
1033 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
1034 * other SEQPACKET protocols that take time to connect() as it doesn't
1035 * include the -EINPROGRESS status for such sockets.
1038 asmlinkage long sys_connect(int fd, struct sockaddr *uservaddr, int addrlen)
1040 struct socket *sock;
1041 char address[MAX_SOCK_ADDR];
1042 int err;
1044 sock = sockfd_lookup(fd, &err);
1045 if (!sock)
1046 goto out;
1047 err = move_addr_to_kernel(uservaddr, addrlen, address);
1048 if (err < 0)
1049 goto out_put;
1050 err = sock->ops->connect(sock, (struct sockaddr *) address, addrlen,
1051 sock->file->f_flags);
1052 out_put:
1053 sockfd_put(sock);
1054 out:
1055 return err;
1059 * Get the local address ('name') of a socket object. Move the obtained
1060 * name to user space.
1063 asmlinkage long sys_getsockname(int fd, struct sockaddr *usockaddr, int *usockaddr_len)
1065 struct socket *sock;
1066 char address[MAX_SOCK_ADDR];
1067 int len, err;
1069 sock = sockfd_lookup(fd, &err);
1070 if (!sock)
1071 goto out;
1072 err = sock->ops->getname(sock, (struct sockaddr *)address, &len, 0);
1073 if (err)
1074 goto out_put;
1075 err = move_addr_to_user(address, len, usockaddr, usockaddr_len);
1077 out_put:
1078 sockfd_put(sock);
1079 out:
1080 return err;
1084 * Get the remote address ('name') of a socket object. Move the obtained
1085 * name to user space.
1088 asmlinkage long sys_getpeername(int fd, struct sockaddr *usockaddr, int *usockaddr_len)
1090 struct socket *sock;
1091 char address[MAX_SOCK_ADDR];
1092 int len, err;
1094 if ((sock = sockfd_lookup(fd, &err))!=NULL)
1096 err = sock->ops->getname(sock, (struct sockaddr *)address, &len, 1);
1097 if (!err)
1098 err=move_addr_to_user(address,len, usockaddr, usockaddr_len);
1099 sockfd_put(sock);
1101 return err;
1105 * Send a datagram to a given address. We move the address into kernel
1106 * space and check the user space data area is readable before invoking
1107 * the protocol.
1110 asmlinkage long sys_sendto(int fd, void * buff, size_t len, unsigned flags,
1111 struct sockaddr *addr, int addr_len)
1113 struct socket *sock;
1114 char address[MAX_SOCK_ADDR];
1115 int err;
1116 struct msghdr msg;
1117 struct iovec iov;
1119 sock = sockfd_lookup(fd, &err);
1120 if (!sock)
1121 goto out;
1122 iov.iov_base=buff;
1123 iov.iov_len=len;
1124 msg.msg_name=NULL;
1125 msg.msg_iov=&iov;
1126 msg.msg_iovlen=1;
1127 msg.msg_control=NULL;
1128 msg.msg_controllen=0;
1129 msg.msg_namelen=addr_len;
1130 if(addr)
1132 err = move_addr_to_kernel(addr, addr_len, address);
1133 if (err < 0)
1134 goto out_put;
1135 msg.msg_name=address;
1137 if (sock->file->f_flags & O_NONBLOCK)
1138 flags |= MSG_DONTWAIT;
1139 msg.msg_flags = flags;
1140 err = sock_sendmsg(sock, &msg, len);
1142 out_put:
1143 sockfd_put(sock);
1144 out:
1145 return err;
1149 * Send a datagram down a socket.
1152 asmlinkage long sys_send(int fd, void * buff, size_t len, unsigned flags)
1154 return sys_sendto(fd, buff, len, flags, NULL, 0);
1158 * Receive a frame from the socket and optionally record the address of the
1159 * sender. We verify the buffers are writable and if needed move the
1160 * sender address from kernel to user space.
1163 asmlinkage long sys_recvfrom(int fd, void * ubuf, size_t size, unsigned flags,
1164 struct sockaddr *addr, int *addr_len)
1166 struct socket *sock;
1167 struct iovec iov;
1168 struct msghdr msg;
1169 char address[MAX_SOCK_ADDR];
1170 int err,err2;
1172 sock = sockfd_lookup(fd, &err);
1173 if (!sock)
1174 goto out;
1176 msg.msg_control=NULL;
1177 msg.msg_controllen=0;
1178 msg.msg_iovlen=1;
1179 msg.msg_iov=&iov;
1180 iov.iov_len=size;
1181 iov.iov_base=ubuf;
1182 msg.msg_name=address;
1183 msg.msg_namelen=MAX_SOCK_ADDR;
1184 if (sock->file->f_flags & O_NONBLOCK)
1185 flags |= MSG_DONTWAIT;
1186 err=sock_recvmsg(sock, &msg, size, flags);
1188 if(err >= 0 && addr != NULL && msg.msg_namelen)
1190 err2=move_addr_to_user(address, msg.msg_namelen, addr, addr_len);
1191 if(err2<0)
1192 err=err2;
1194 sockfd_put(sock);
1195 out:
1196 return err;
1200 * Receive a datagram from a socket.
1203 asmlinkage long sys_recv(int fd, void * ubuf, size_t size, unsigned flags)
1205 return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
1209 * Set a socket option. Because we don't know the option lengths we have
1210 * to pass the user mode parameter for the protocols to sort out.
1213 asmlinkage long sys_setsockopt(int fd, int level, int optname, char *optval, int optlen)
1215 int err;
1216 struct socket *sock;
1218 if ((sock = sockfd_lookup(fd, &err))!=NULL)
1220 if (level == SOL_SOCKET)
1221 err=sock_setsockopt(sock,level,optname,optval,optlen);
1222 else
1223 err=sock->ops->setsockopt(sock, level, optname, optval, optlen);
1224 sockfd_put(sock);
1226 return err;
1230 * Get a socket option. Because we don't know the option lengths we have
1231 * to pass a user mode parameter for the protocols to sort out.
1234 asmlinkage long sys_getsockopt(int fd, int level, int optname, char *optval, int *optlen)
1236 int err;
1237 struct socket *sock;
1239 if ((sock = sockfd_lookup(fd, &err))!=NULL)
1241 if (level == SOL_SOCKET)
1242 err=sock_getsockopt(sock,level,optname,optval,optlen);
1243 else
1244 err=sock->ops->getsockopt(sock, level, optname, optval, optlen);
1245 sockfd_put(sock);
1247 return err;
1252 * Shutdown a socket.
1255 asmlinkage long sys_shutdown(int fd, int how)
1257 int err;
1258 struct socket *sock;
1260 if ((sock = sockfd_lookup(fd, &err))!=NULL)
1262 err=sock->ops->shutdown(sock, how);
1263 sockfd_put(sock);
1265 return err;
1269 * BSD sendmsg interface
1272 asmlinkage long sys_sendmsg(int fd, struct msghdr *msg, unsigned flags)
1274 struct socket *sock;
1275 char address[MAX_SOCK_ADDR];
1276 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
1277 unsigned char ctl[sizeof(struct cmsghdr) + 20]; /* 20 is size of ipv6_pktinfo */
1278 unsigned char *ctl_buf = ctl;
1279 struct msghdr msg_sys;
1280 int err, ctl_len, iov_size, total_len;
1282 err = -EFAULT;
1283 if (copy_from_user(&msg_sys,msg,sizeof(struct msghdr)))
1284 goto out;
1286 sock = sockfd_lookup(fd, &err);
1287 if (!sock)
1288 goto out;
1290 /* do not move before msg_sys is valid */
1291 err = -EINVAL;
1292 if (msg_sys.msg_iovlen > UIO_MAXIOV)
1293 goto out_put;
1295 /* Check whether to allocate the iovec area*/
1296 err = -ENOMEM;
1297 iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
1298 if (msg_sys.msg_iovlen > UIO_FASTIOV) {
1299 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
1300 if (!iov)
1301 goto out_put;
1304 /* This will also move the address data into kernel space */
1305 err = verify_iovec(&msg_sys, iov, address, VERIFY_READ);
1306 if (err < 0)
1307 goto out_freeiov;
1308 total_len = err;
1310 err = -ENOBUFS;
1312 if (msg_sys.msg_controllen > INT_MAX)
1313 goto out_freeiov;
1314 ctl_len = msg_sys.msg_controllen;
1315 if (ctl_len)
1317 if (ctl_len > sizeof(ctl))
1319 err = -ENOBUFS;
1320 ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
1321 if (ctl_buf == NULL)
1322 goto out_freeiov;
1324 err = -EFAULT;
1325 if (copy_from_user(ctl_buf, msg_sys.msg_control, ctl_len))
1326 goto out_freectl;
1327 msg_sys.msg_control = ctl_buf;
1329 msg_sys.msg_flags = flags;
1331 if (sock->file->f_flags & O_NONBLOCK)
1332 msg_sys.msg_flags |= MSG_DONTWAIT;
1333 err = sock_sendmsg(sock, &msg_sys, total_len);
1335 out_freectl:
1336 if (ctl_buf != ctl)
1337 sock_kfree_s(sock->sk, ctl_buf, ctl_len);
1338 out_freeiov:
1339 if (iov != iovstack)
1340 sock_kfree_s(sock->sk, iov, iov_size);
1341 out_put:
1342 sockfd_put(sock);
1343 out:
1344 return err;
1348 * BSD recvmsg interface
1351 asmlinkage long sys_recvmsg(int fd, struct msghdr *msg, unsigned int flags)
1353 struct socket *sock;
1354 struct iovec iovstack[UIO_FASTIOV];
1355 struct iovec *iov=iovstack;
1356 struct msghdr msg_sys;
1357 unsigned long cmsg_ptr;
1358 int err, iov_size, total_len, len;
1360 /* kernel mode address */
1361 char addr[MAX_SOCK_ADDR];
1363 /* user mode address pointers */
1364 struct sockaddr *uaddr;
1365 int *uaddr_len;
1367 err=-EFAULT;
1368 if (copy_from_user(&msg_sys,msg,sizeof(struct msghdr)))
1369 goto out;
1371 sock = sockfd_lookup(fd, &err);
1372 if (!sock)
1373 goto out;
1375 err = -EINVAL;
1376 if (msg_sys.msg_iovlen > UIO_MAXIOV)
1377 goto out_put;
1379 /* Check whether to allocate the iovec area*/
1380 err = -ENOMEM;
1381 iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
1382 if (msg_sys.msg_iovlen > UIO_FASTIOV) {
1383 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
1384 if (!iov)
1385 goto out_put;
1389 * Save the user-mode address (verify_iovec will change the
1390 * kernel msghdr to use the kernel address space)
1393 uaddr = msg_sys.msg_name;
1394 uaddr_len = &msg->msg_namelen;
1395 err = verify_iovec(&msg_sys, iov, addr, VERIFY_WRITE);
1396 if (err < 0)
1397 goto out_freeiov;
1398 total_len=err;
1400 cmsg_ptr = (unsigned long)msg_sys.msg_control;
1401 msg_sys.msg_flags = 0;
1403 if (sock->file->f_flags & O_NONBLOCK)
1404 flags |= MSG_DONTWAIT;
1405 err = sock_recvmsg(sock, &msg_sys, total_len, flags);
1406 if (err < 0)
1407 goto out_freeiov;
1408 len = err;
1410 if (uaddr != NULL && msg_sys.msg_namelen) {
1411 err = move_addr_to_user(addr, msg_sys.msg_namelen, uaddr, uaddr_len);
1412 if (err < 0)
1413 goto out_freeiov;
1415 err = __put_user(msg_sys.msg_flags, &msg->msg_flags);
1416 if (err)
1417 goto out_freeiov;
1418 err = __put_user((unsigned long)msg_sys.msg_control-cmsg_ptr,
1419 &msg->msg_controllen);
1420 if (err)
1421 goto out_freeiov;
1422 err = len;
1424 out_freeiov:
1425 if (iov != iovstack)
1426 sock_kfree_s(sock->sk, iov, iov_size);
1427 out_put:
1428 sockfd_put(sock);
1429 out:
1430 return err;
1435 * Perform a file control on a socket file descriptor.
1437 * Doesn't aquire a fd lock, because no network fcntl
1438 * function sleeps currently.
1441 int sock_fcntl(struct file *filp, unsigned int cmd, unsigned long arg)
1443 struct socket *sock;
1445 sock = socki_lookup (filp->f_dentry->d_inode);
1446 if (sock && sock->ops)
1447 return sock_no_fcntl(sock, cmd, arg);
1448 return(-EINVAL);
1451 /* Argument list sizes for sys_socketcall */
1452 #define AL(x) ((x) * sizeof(unsigned long))
1453 static unsigned char nargs[18]={AL(0),AL(3),AL(3),AL(3),AL(2),AL(3),
1454 AL(3),AL(3),AL(4),AL(4),AL(4),AL(6),
1455 AL(6),AL(2),AL(5),AL(5),AL(3),AL(3)};
1456 #undef AL
1459 * System call vectors.
1461 * Argument checking cleaned up. Saved 20% in size.
1462 * This function doesn't need to set the kernel lock because
1463 * it is set by the callees.
1466 asmlinkage long sys_socketcall(int call, unsigned long *args)
1468 unsigned long a[6];
1469 unsigned long a0,a1;
1470 int err;
1472 if(call<1||call>SYS_RECVMSG)
1473 return -EINVAL;
1475 /* copy_from_user should be SMP safe. */
1476 if (copy_from_user(a, args, nargs[call]))
1477 return -EFAULT;
1479 a0=a[0];
1480 a1=a[1];
1482 switch(call)
1484 case SYS_SOCKET:
1485 err = sys_socket(a0,a1,a[2]);
1486 break;
1487 case SYS_BIND:
1488 err = sys_bind(a0,(struct sockaddr *)a1, a[2]);
1489 break;
1490 case SYS_CONNECT:
1491 err = sys_connect(a0, (struct sockaddr *)a1, a[2]);
1492 break;
1493 case SYS_LISTEN:
1494 err = sys_listen(a0,a1);
1495 break;
1496 case SYS_ACCEPT:
1497 err = sys_accept(a0,(struct sockaddr *)a1, (int *)a[2]);
1498 break;
1499 case SYS_GETSOCKNAME:
1500 err = sys_getsockname(a0,(struct sockaddr *)a1, (int *)a[2]);
1501 break;
1502 case SYS_GETPEERNAME:
1503 err = sys_getpeername(a0, (struct sockaddr *)a1, (int *)a[2]);
1504 break;
1505 case SYS_SOCKETPAIR:
1506 err = sys_socketpair(a0,a1, a[2], (int *)a[3]);
1507 break;
1508 case SYS_SEND:
1509 err = sys_send(a0, (void *)a1, a[2], a[3]);
1510 break;
1511 case SYS_SENDTO:
1512 err = sys_sendto(a0,(void *)a1, a[2], a[3],
1513 (struct sockaddr *)a[4], a[5]);
1514 break;
1515 case SYS_RECV:
1516 err = sys_recv(a0, (void *)a1, a[2], a[3]);
1517 break;
1518 case SYS_RECVFROM:
1519 err = sys_recvfrom(a0, (void *)a1, a[2], a[3],
1520 (struct sockaddr *)a[4], (int *)a[5]);
1521 break;
1522 case SYS_SHUTDOWN:
1523 err = sys_shutdown(a0,a1);
1524 break;
1525 case SYS_SETSOCKOPT:
1526 err = sys_setsockopt(a0, a1, a[2], (char *)a[3], a[4]);
1527 break;
1528 case SYS_GETSOCKOPT:
1529 err = sys_getsockopt(a0, a1, a[2], (char *)a[3], (int *)a[4]);
1530 break;
1531 case SYS_SENDMSG:
1532 err = sys_sendmsg(a0, (struct msghdr *) a1, a[2]);
1533 break;
1534 case SYS_RECVMSG:
1535 err = sys_recvmsg(a0, (struct msghdr *) a1, a[2]);
1536 break;
1537 default:
1538 err = -EINVAL;
1539 break;
1541 return err;
1545 * This function is called by a protocol handler that wants to
1546 * advertise its address family, and have it linked into the
1547 * SOCKET module.
1550 int sock_register(struct net_proto_family *ops)
1552 int err;
1554 if (ops->family >= NPROTO) {
1555 printk(KERN_CRIT "protocol %d >= NPROTO(%d)\n", ops->family, NPROTO);
1556 return -ENOBUFS;
1558 net_family_write_lock();
1559 err = -EEXIST;
1560 if (net_families[ops->family] == NULL) {
1561 net_families[ops->family]=ops;
1562 err = 0;
1564 net_family_write_unlock();
1565 return err;
1569 * This function is called by a protocol handler that wants to
1570 * remove its address family, and have it unlinked from the
1571 * SOCKET module.
1574 int sock_unregister(int family)
1576 if (family < 0 || family >= NPROTO)
1577 return -1;
1579 net_family_write_lock();
1580 net_families[family]=NULL;
1581 net_family_write_unlock();
1582 return 0;
1585 void __init proto_init(void)
1587 extern struct net_proto protocols[]; /* Network protocols */
1588 struct net_proto *pro;
1590 /* Kick all configured protocols. */
1591 pro = protocols;
1592 while (pro->name != NULL)
1594 (*pro->init_func)(pro);
1595 pro++;
1597 /* We're all done... */
1600 extern void sk_init(void);
1602 #ifdef CONFIG_BRIDGE
1603 extern int br_init(void);
1604 #endif
1606 #ifdef CONFIG_WAN_ROUTER
1607 extern void wanrouter_init(void);
1608 #endif
1610 void __init sock_init(void)
1612 int i;
1614 printk(KERN_INFO "Linux NET4.0 for Linux 2.3\n");
1615 printk(KERN_INFO "Based upon Swansea University Computer Society NET3.039\n");
1618 * Initialize all address (protocol) families.
1621 for (i = 0; i < NPROTO; i++)
1622 net_families[i] = NULL;
1625 * Initialize sock SLAB cache.
1628 sk_init();
1630 #ifdef SLAB_SKB
1632 * Initialize skbuff SLAB cache
1634 skb_init();
1635 #endif
1638 * Ethernet bridge layer.
1641 #ifdef CONFIG_BRIDGE
1642 br_init();
1643 #endif
1646 * Wan router layer.
1649 #ifdef CONFIG_WAN_ROUTER
1650 wanrouter_init();
1651 #endif
1654 * Initialize the protocols module.
1657 proto_init();
1660 * The netlink device handler may be needed early.
1663 #ifdef CONFIG_RTNETLINK
1664 rtnetlink_init();
1665 #endif
1666 #ifdef CONFIG_NETLINK_DEV
1667 init_netlink();
1668 #endif
1669 #ifdef CONFIG_NETFILTER
1670 netfilter_init();
1671 #endif
1674 int socket_get_info(char *buffer, char **start, off_t offset, int length)
1676 int len, cpu;
1677 int counter = 0;
1679 for (cpu=0; cpu<smp_num_cpus; cpu++)
1680 counter += sockets_in_use[cpu_logical_map(cpu)].counter;
1682 /* It can be negative, by the way. 8) */
1683 if (counter < 0)
1684 counter = 0;
1686 len = sprintf(buffer, "sockets: used %d\n", counter);
1687 if (offset >= len)
1689 *start = buffer;
1690 return 0;
1692 *start = buffer + offset;
1693 len -= offset;
1694 if (len > length)
1695 len = length;
1696 if (len < 0)
1697 len = 0;
1698 return len;