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>
11 * Anonymous : NOTSOCK/BADF cleanup. Error fix in
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
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
23 * Niibe Yutaka : Asynchronous I/O for writes (4.4BSD style)
24 * Jeff Uphoff : Made max number of sockets command-line
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
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
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
58 * Based upon Swansea University Computer Society NET3.039
61 #include <linux/config.h>
63 #include <linux/smp_lock.h>
64 #include <linux/socket.h>
65 #include <linux/file.h>
66 #include <linux/net.h>
67 #include <linux/interrupt.h>
68 #include <linux/netdevice.h>
69 #include <linux/proc_fs.h>
70 #include <linux/seq_file.h>
71 #include <linux/wanrouter.h>
72 #include <linux/if_bridge.h>
73 #include <linux/init.h>
74 #include <linux/poll.h>
75 #include <linux/cache.h>
76 #include <linux/module.h>
77 #include <linux/highmem.h>
78 #include <linux/divert.h>
79 #include <linux/mount.h>
80 #include <linux/security.h>
81 #include <linux/syscalls.h>
82 #include <linux/compat.h>
83 #include <linux/kmod.h>
85 #ifdef CONFIG_NET_RADIO
86 #include <linux/wireless.h> /* Note : will define WIRELESS_EXT */
87 #endif /* CONFIG_NET_RADIO */
89 #include <asm/uaccess.h>
90 #include <net/compat.h>
93 #include <linux/netfilter.h>
95 static int sock_no_open(struct inode *irrelevant, struct file *dontcare);
96 static ssize_t sock_aio_read(struct kiocb *iocb, char __user *buf,
97 size_t size, loff_t pos);
98 static ssize_t sock_aio_write(struct kiocb *iocb, const char __user *buf,
99 size_t size, loff_t pos);
100 static int sock_mmap(struct file *file, struct vm_area_struct * vma);
102 static int sock_close(struct inode *inode, struct file *file);
103 static unsigned int sock_poll(struct file *file,
104 struct poll_table_struct *wait);
105 static int sock_ioctl(struct inode *inode, struct file *file,
106 unsigned int cmd, unsigned long arg);
107 static int sock_fasync(int fd, struct file *filp, int on);
108 static ssize_t sock_readv(struct file *file, const struct iovec *vector,
109 unsigned long count, loff_t *ppos);
110 static ssize_t sock_writev(struct file *file, const struct iovec *vector,
111 unsigned long count, loff_t *ppos);
112 static ssize_t sock_sendpage(struct file *file, struct page *page,
113 int offset, size_t size, loff_t *ppos, int more);
117 * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
118 * in the operation structures but are done directly via the socketcall() multiplexor.
121 static struct file_operations socket_file_ops = {
122 .owner = THIS_MODULE,
124 .aio_read = sock_aio_read,
125 .aio_write = sock_aio_write,
129 .open = sock_no_open, /* special open code to disallow open via /proc */
130 .release = sock_close,
131 .fasync = sock_fasync,
133 .writev = sock_writev,
134 .sendpage = sock_sendpage
138 * The protocol list. Each protocol is registered in here.
141 static struct net_proto_family *net_families[NPROTO];
143 #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
144 static atomic_t net_family_lockct = ATOMIC_INIT(0);
145 static spinlock_t net_family_lock = SPIN_LOCK_UNLOCKED;
147 /* The strategy is: modifications net_family vector are short, do not
148 sleep and veeery rare, but read access should be free of any exclusive
152 static void net_family_write_lock(void)
154 spin_lock(&net_family_lock);
155 while (atomic_read(&net_family_lockct) != 0) {
156 spin_unlock(&net_family_lock);
160 spin_lock(&net_family_lock);
164 static __inline__ void net_family_write_unlock(void)
166 spin_unlock(&net_family_lock);
169 static __inline__ void net_family_read_lock(void)
171 atomic_inc(&net_family_lockct);
172 spin_unlock_wait(&net_family_lock);
175 static __inline__ void net_family_read_unlock(void)
177 atomic_dec(&net_family_lockct);
181 #define net_family_write_lock() do { } while(0)
182 #define net_family_write_unlock() do { } while(0)
183 #define net_family_read_lock() do { } while(0)
184 #define net_family_read_unlock() do { } while(0)
189 * Statistics counters of the socket lists
192 static DEFINE_PER_CPU(int, sockets_in_use) = 0;
195 * Support routines. Move socket addresses back and forth across the kernel/user
196 * divide and look after the messy bits.
199 #define MAX_SOCK_ADDR 128 /* 108 for Unix domain -
200 16 for IP, 16 for IPX,
203 must be at least one bigger than
204 the AF_UNIX size (see net/unix/af_unix.c
209 * move_addr_to_kernel - copy a socket address into kernel space
210 * @uaddr: Address in user space
211 * @kaddr: Address in kernel space
212 * @ulen: Length in user space
214 * The address is copied into kernel space. If the provided address is
215 * too long an error code of -EINVAL is returned. If the copy gives
216 * invalid addresses -EFAULT is returned. On a success 0 is returned.
219 int move_addr_to_kernel(void __user *uaddr, int ulen, void *kaddr)
221 if(ulen<0||ulen>MAX_SOCK_ADDR)
225 if(copy_from_user(kaddr,uaddr,ulen))
231 * move_addr_to_user - copy an address to user space
232 * @kaddr: kernel space address
233 * @klen: length of address in kernel
234 * @uaddr: user space address
235 * @ulen: pointer to user length field
237 * The value pointed to by ulen on entry is the buffer length available.
238 * This is overwritten with the buffer space used. -EINVAL is returned
239 * if an overlong buffer is specified or a negative buffer size. -EFAULT
240 * is returned if either the buffer or the length field are not
242 * After copying the data up to the limit the user specifies, the true
243 * length of the data is written over the length limit the user
244 * specified. Zero is returned for a success.
247 int move_addr_to_user(void *kaddr, int klen, void __user *uaddr, int __user *ulen)
252 if((err=get_user(len, ulen)))
256 if(len<0 || len> MAX_SOCK_ADDR)
260 if(copy_to_user(uaddr,kaddr,len))
264 * "fromlen shall refer to the value before truncation.."
267 return __put_user(klen, ulen);
270 #define SOCKFS_MAGIC 0x534F434B
272 static kmem_cache_t * sock_inode_cachep;
274 static struct inode *sock_alloc_inode(struct super_block *sb)
276 struct socket_alloc *ei;
277 ei = (struct socket_alloc *)kmem_cache_alloc(sock_inode_cachep, SLAB_KERNEL);
280 init_waitqueue_head(&ei->socket.wait);
282 ei->socket.fasync_list = NULL;
283 ei->socket.state = SS_UNCONNECTED;
284 ei->socket.flags = 0;
285 ei->socket.ops = NULL;
286 ei->socket.sk = NULL;
287 ei->socket.file = NULL;
288 ei->socket.passcred = 0;
290 return &ei->vfs_inode;
293 static void sock_destroy_inode(struct inode *inode)
295 kmem_cache_free(sock_inode_cachep,
296 container_of(inode, struct socket_alloc, vfs_inode));
299 static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
301 struct socket_alloc *ei = (struct socket_alloc *) foo;
303 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
304 SLAB_CTOR_CONSTRUCTOR)
305 inode_init_once(&ei->vfs_inode);
308 static int init_inodecache(void)
310 sock_inode_cachep = kmem_cache_create("sock_inode_cache",
311 sizeof(struct socket_alloc),
312 0, SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT,
314 if (sock_inode_cachep == NULL)
319 static struct super_operations sockfs_ops = {
320 .alloc_inode = sock_alloc_inode,
321 .destroy_inode =sock_destroy_inode,
322 .statfs = simple_statfs,
325 static struct super_block *sockfs_get_sb(struct file_system_type *fs_type,
326 int flags, const char *dev_name, void *data)
328 return get_sb_pseudo(fs_type, "socket:", &sockfs_ops, SOCKFS_MAGIC);
331 static struct vfsmount *sock_mnt;
333 static struct file_system_type sock_fs_type = {
335 .get_sb = sockfs_get_sb,
336 .kill_sb = kill_anon_super,
338 static int sockfs_delete_dentry(struct dentry *dentry)
342 static struct dentry_operations sockfs_dentry_operations = {
343 .d_delete = sockfs_delete_dentry,
347 * Obtains the first available file descriptor and sets it up for use.
349 * This function creates file structure and maps it to fd space
350 * of current process. On success it returns file descriptor
351 * and file struct implicitly stored in sock->file.
352 * Note that another thread may close file descriptor before we return
353 * from this function. We use the fact that now we do not refer
354 * to socket after mapping. If one day we will need it, this
355 * function will increment ref. count on file by 1.
357 * In any case returned fd MAY BE not valid!
358 * This race condition is unavoidable
359 * with shared fd spaces, we cannot solve it inside kernel,
360 * but we take care of internal coherence yet.
363 int sock_map_fd(struct socket *sock)
370 * Find a file descriptor suitable for return to the user.
373 fd = get_unused_fd();
375 struct file *file = get_empty_filp();
383 sprintf(name, "[%lu]", SOCK_INODE(sock)->i_ino);
385 this.len = strlen(name);
386 this.hash = SOCK_INODE(sock)->i_ino;
388 file->f_dentry = d_alloc(sock_mnt->mnt_sb->s_root, &this);
389 if (!file->f_dentry) {
395 file->f_dentry->d_op = &sockfs_dentry_operations;
396 d_add(file->f_dentry, SOCK_INODE(sock));
397 file->f_vfsmnt = mntget(sock_mnt);
398 file->f_mapping = file->f_dentry->d_inode->i_mapping;
401 file->f_op = SOCK_INODE(sock)->i_fop = &socket_file_ops;
403 file->f_flags = O_RDWR;
405 fd_install(fd, file);
413 * sockfd_lookup - Go from a file number to its socket slot
415 * @err: pointer to an error code return
417 * The file handle passed in is locked and the socket it is bound
418 * too is returned. If an error occurs the err pointer is overwritten
419 * with a negative errno code and NULL is returned. The function checks
420 * for both invalid handles and passing a handle which is not a socket.
422 * On a success the socket object pointer is returned.
425 struct socket *sockfd_lookup(int fd, int *err)
431 if (!(file = fget(fd)))
437 inode = file->f_dentry->d_inode;
438 if (!inode->i_sock || !(sock = SOCKET_I(inode)))
445 if (sock->file != file) {
446 printk(KERN_ERR "socki_lookup: socket file changed!\n");
453 * sock_alloc - allocate a socket
455 * Allocate a new inode and socket object. The two are bound together
456 * and initialised. The socket is then returned. If we are out of inodes
460 struct socket *sock_alloc(void)
462 struct inode * inode;
463 struct socket * sock;
465 inode = new_inode(sock_mnt->mnt_sb);
469 sock = SOCKET_I(inode);
471 inode->i_mode = S_IFSOCK|S_IRWXUGO;
473 inode->i_uid = current->fsuid;
474 inode->i_gid = current->fsgid;
476 get_cpu_var(sockets_in_use)++;
477 put_cpu_var(sockets_in_use);
482 * In theory you can't get an open on this inode, but /proc provides
483 * a back door. Remember to keep it shut otherwise you'll let the
484 * creepy crawlies in.
487 static int sock_no_open(struct inode *irrelevant, struct file *dontcare)
492 struct file_operations bad_sock_fops = {
493 .owner = THIS_MODULE,
494 .open = sock_no_open,
498 * sock_release - close a socket
499 * @sock: socket to close
501 * The socket is released from the protocol stack if it has a release
502 * callback, and the inode is then released if the socket is bound to
503 * an inode not a file.
506 void sock_release(struct socket *sock)
509 struct module *owner = sock->ops->owner;
511 sock->ops->release(sock);
516 if (sock->fasync_list)
517 printk(KERN_ERR "sock_release: fasync list not empty!\n");
519 get_cpu_var(sockets_in_use)--;
520 put_cpu_var(sockets_in_use);
522 iput(SOCK_INODE(sock));
528 static inline int __sock_sendmsg(struct kiocb *iocb, struct socket *sock,
529 struct msghdr *msg, size_t size)
531 struct sock_iocb *si = kiocb_to_siocb(iocb);
539 err = security_socket_sendmsg(sock, msg, size);
543 return sock->ops->sendmsg(iocb, sock, msg, size);
546 int sock_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
551 init_sync_kiocb(&iocb, NULL);
552 ret = __sock_sendmsg(&iocb, sock, msg, size);
553 if (-EIOCBQUEUED == ret)
554 ret = wait_on_sync_kiocb(&iocb);
559 static inline int __sock_recvmsg(struct kiocb *iocb, struct socket *sock,
560 struct msghdr *msg, size_t size, int flags)
563 struct sock_iocb *si = kiocb_to_siocb(iocb);
571 err = security_socket_recvmsg(sock, msg, size, flags);
575 return sock->ops->recvmsg(iocb, sock, msg, size, flags);
578 int sock_recvmsg(struct socket *sock, struct msghdr *msg,
579 size_t size, int flags)
584 init_sync_kiocb(&iocb, NULL);
585 ret = __sock_recvmsg(&iocb, sock, msg, size, flags);
586 if (-EIOCBQUEUED == ret)
587 ret = wait_on_sync_kiocb(&iocb);
592 * Read data from a socket. ubuf is a user mode pointer. We make sure the user
593 * area ubuf...ubuf+size-1 is writable before asking the protocol.
596 static ssize_t sock_aio_read(struct kiocb *iocb, char __user *ubuf,
597 size_t size, loff_t pos)
599 struct sock_iocb *x = kiocb_to_siocb(iocb);
605 if (size==0) /* Match SYS5 behaviour */
608 sock = SOCKET_I(iocb->ki_filp->f_dentry->d_inode);
610 x->async_msg.msg_name = NULL;
611 x->async_msg.msg_namelen = 0;
612 x->async_msg.msg_iov = &x->async_iov;
613 x->async_msg.msg_iovlen = 1;
614 x->async_msg.msg_control = NULL;
615 x->async_msg.msg_controllen = 0;
616 x->async_iov.iov_base = ubuf;
617 x->async_iov.iov_len = size;
618 flags = !(iocb->ki_filp->f_flags & O_NONBLOCK) ? 0 : MSG_DONTWAIT;
620 return __sock_recvmsg(iocb, sock, &x->async_msg, size, flags);
625 * Write data to a socket. We verify that the user area ubuf..ubuf+size-1
626 * is readable by the user process.
629 static ssize_t sock_aio_write(struct kiocb *iocb, const char __user *ubuf,
630 size_t size, loff_t pos)
632 struct sock_iocb *x = kiocb_to_siocb(iocb);
637 if(size==0) /* Match SYS5 behaviour */
640 sock = SOCKET_I(iocb->ki_filp->f_dentry->d_inode);
642 x->async_msg.msg_name = NULL;
643 x->async_msg.msg_namelen = 0;
644 x->async_msg.msg_iov = &x->async_iov;
645 x->async_msg.msg_iovlen = 1;
646 x->async_msg.msg_control = NULL;
647 x->async_msg.msg_controllen = 0;
648 x->async_msg.msg_flags = !(iocb->ki_filp->f_flags & O_NONBLOCK) ? 0 : MSG_DONTWAIT;
649 if (sock->type == SOCK_SEQPACKET)
650 x->async_msg.msg_flags |= MSG_EOR;
651 x->async_iov.iov_base = (void __user *)ubuf;
652 x->async_iov.iov_len = size;
654 return __sock_sendmsg(iocb, sock, &x->async_msg, size);
657 ssize_t sock_sendpage(struct file *file, struct page *page,
658 int offset, size_t size, loff_t *ppos, int more)
663 if (ppos != &file->f_pos)
666 sock = SOCKET_I(file->f_dentry->d_inode);
668 flags = !(file->f_flags & O_NONBLOCK) ? 0 : MSG_DONTWAIT;
672 return sock->ops->sendpage(sock, page, offset, size, flags);
675 int sock_readv_writev(int type, struct inode * inode, struct file * file,
676 const struct iovec * iov, long count, size_t size)
681 sock = SOCKET_I(inode);
685 msg.msg_control = NULL;
686 msg.msg_controllen = 0;
687 msg.msg_iov = (struct iovec *) iov;
688 msg.msg_iovlen = count;
689 msg.msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
691 /* read() does a VERIFY_WRITE */
692 if (type == VERIFY_WRITE)
693 return sock_recvmsg(sock, &msg, size, msg.msg_flags);
695 if (sock->type == SOCK_SEQPACKET)
696 msg.msg_flags |= MSG_EOR;
698 return sock_sendmsg(sock, &msg, size);
701 static ssize_t sock_readv(struct file *file, const struct iovec *vector,
702 unsigned long count, loff_t *ppos)
706 for (i = 0 ; i < count ; i++)
707 tot_len += vector[i].iov_len;
708 return sock_readv_writev(VERIFY_WRITE, file->f_dentry->d_inode,
709 file, vector, count, tot_len);
712 static ssize_t sock_writev(struct file *file, const struct iovec *vector,
713 unsigned long count, loff_t *ppos)
717 for (i = 0 ; i < count ; i++)
718 tot_len += vector[i].iov_len;
719 return sock_readv_writev(VERIFY_READ, file->f_dentry->d_inode,
720 file, vector, count, tot_len);
725 * Atomic setting of ioctl hooks to avoid race
726 * with module unload.
729 static DECLARE_MUTEX(br_ioctl_mutex);
730 static int (*br_ioctl_hook)(unsigned long arg) = NULL;
732 void brioctl_set(int (*hook)(unsigned long))
734 down(&br_ioctl_mutex);
735 br_ioctl_hook = hook;
738 EXPORT_SYMBOL(brioctl_set);
740 static DECLARE_MUTEX(vlan_ioctl_mutex);
741 static int (*vlan_ioctl_hook)(unsigned long arg);
743 void vlan_ioctl_set(int (*hook)(unsigned long))
745 down(&vlan_ioctl_mutex);
746 vlan_ioctl_hook = hook;
747 up(&vlan_ioctl_mutex);
749 EXPORT_SYMBOL(vlan_ioctl_set);
751 static DECLARE_MUTEX(dlci_ioctl_mutex);
752 static int (*dlci_ioctl_hook)(unsigned int, void *);
754 void dlci_ioctl_set(int (*hook)(unsigned int, void *))
756 down(&dlci_ioctl_mutex);
757 dlci_ioctl_hook = hook;
758 up(&dlci_ioctl_mutex);
760 EXPORT_SYMBOL(dlci_ioctl_set);
763 * With an ioctl, arg may well be a user mode pointer, but we don't know
764 * what to do with it - that's up to the protocol still.
767 static int sock_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
774 sock = SOCKET_I(inode);
775 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
776 err = dev_ioctl(cmd, (void *)arg);
779 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
780 err = dev_ioctl(cmd, (void *)arg);
782 #endif /* WIRELESS_EXT */
787 if (get_user(pid, (int *)arg))
789 err = f_setown(sock->file, pid, 1);
793 err = put_user(sock->file->f_owner.pid, (int *)arg);
799 request_module("bridge");
801 down(&br_ioctl_mutex);
803 err = br_ioctl_hook(arg);
809 if (!vlan_ioctl_hook)
810 request_module("8021q");
812 down(&vlan_ioctl_mutex);
814 err = vlan_ioctl_hook(arg);
815 up(&vlan_ioctl_mutex);
819 /* Convert this to call through a hook */
820 err = divert_ioctl(cmd, (struct divert_cf *)arg);
825 if (!dlci_ioctl_hook)
826 request_module("dlci");
828 if (dlci_ioctl_hook) {
829 down(&dlci_ioctl_mutex);
830 err = dlci_ioctl_hook(cmd, (void *)arg);
831 up(&dlci_ioctl_mutex);
835 err = sock->ops->ioctl(sock, cmd, arg);
843 int sock_create_lite(int family, int type, int protocol, struct socket **res)
846 struct socket *sock = NULL;
848 err = security_socket_create(family, type, protocol, 1);
858 security_socket_post_create(sock, family, type, protocol, 1);
865 /* No kernel lock held - perfect */
866 static unsigned int sock_poll(struct file *file, poll_table * wait)
871 * We can't return errors to poll, so it's either yes or no.
873 sock = SOCKET_I(file->f_dentry->d_inode);
874 return sock->ops->poll(file, sock, wait);
877 static int sock_mmap(struct file * file, struct vm_area_struct * vma)
879 struct socket *sock = SOCKET_I(file->f_dentry->d_inode);
881 return sock->ops->mmap(file, sock, vma);
884 int sock_close(struct inode *inode, struct file *filp)
887 * It was possible the inode is NULL we were
888 * closing an unfinished socket.
893 printk(KERN_DEBUG "sock_close: NULL inode\n");
896 sock_fasync(-1, filp, 0);
897 sock_release(SOCKET_I(inode));
902 * Update the socket async list
904 * Fasync_list locking strategy.
906 * 1. fasync_list is modified only under process context socket lock
907 * i.e. under semaphore.
908 * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
909 * or under socket lock.
910 * 3. fasync_list can be used from softirq context, so that
911 * modification under socket lock have to be enhanced with
912 * write_lock_bh(&sk->sk_callback_lock).
916 static int sock_fasync(int fd, struct file *filp, int on)
918 struct fasync_struct *fa, *fna=NULL, **prev;
924 fna=(struct fasync_struct *)kmalloc(sizeof(struct fasync_struct), GFP_KERNEL);
929 sock = SOCKET_I(filp->f_dentry->d_inode);
931 if ((sk=sock->sk) == NULL) {
939 prev=&(sock->fasync_list);
941 for (fa=*prev; fa!=NULL; prev=&fa->fa_next,fa=*prev)
942 if (fa->fa_file==filp)
949 write_lock_bh(&sk->sk_callback_lock);
951 write_unlock_bh(&sk->sk_callback_lock);
958 fna->magic=FASYNC_MAGIC;
959 fna->fa_next=sock->fasync_list;
960 write_lock_bh(&sk->sk_callback_lock);
961 sock->fasync_list=fna;
962 write_unlock_bh(&sk->sk_callback_lock);
968 write_lock_bh(&sk->sk_callback_lock);
970 write_unlock_bh(&sk->sk_callback_lock);
976 release_sock(sock->sk);
980 /* This function may be called only under socket lock or callback_lock */
982 int sock_wake_async(struct socket *sock, int how, int band)
984 if (!sock || !sock->fasync_list)
990 if (test_bit(SOCK_ASYNC_WAITDATA, &sock->flags))
994 if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags))
999 __kill_fasync(sock->fasync_list, SIGIO, band);
1002 __kill_fasync(sock->fasync_list, SIGURG, band);
1007 static int __sock_create(int family, int type, int protocol, struct socket **res, int kern)
1011 struct socket *sock;
1014 * Check protocol is in range
1016 if (family < 0 || family >= NPROTO)
1017 return -EAFNOSUPPORT;
1018 if (type < 0 || type >= SOCK_MAX)
1023 This uglymoron is moved from INET layer to here to avoid
1024 deadlock in module load.
1026 if (family == PF_INET && type == SOCK_PACKET) {
1030 printk(KERN_INFO "%s uses obsolete (PF_INET,SOCK_PACKET)\n", current->comm);
1035 err = security_socket_create(family, type, protocol, kern);
1039 #if defined(CONFIG_KMOD)
1040 /* Attempt to load a protocol module if the find failed.
1042 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
1043 * requested real, full-featured networking support upon configuration.
1044 * Otherwise module support will break!
1046 if (net_families[family]==NULL)
1048 request_module("net-pf-%d",family);
1052 net_family_read_lock();
1053 if (net_families[family] == NULL) {
1059 * Allocate the socket and allow the family to set things up. if
1060 * the protocol is 0, the family is instructed to select an appropriate
1064 if (!(sock = sock_alloc()))
1066 printk(KERN_WARNING "socket: no more sockets\n");
1067 i = -ENFILE; /* Not exactly a match, but its the
1068 closest posix thing */
1075 * We will call the ->create function, that possibly is in a loadable
1076 * module, so we have to bump that loadable module refcnt first.
1079 if (!try_module_get(net_families[family]->owner))
1082 if ((i = net_families[family]->create(sock, protocol)) < 0)
1083 goto out_module_put;
1085 * Now to bump the refcnt of the [loadable] module that owns this
1086 * socket at sock_release time we decrement its refcnt.
1088 if (!try_module_get(sock->ops->owner)) {
1090 goto out_module_put;
1093 * Now that we're done with the ->create function, the [loadable]
1094 * module can have its refcnt decremented
1096 module_put(net_families[family]->owner);
1098 security_socket_post_create(sock, family, type, protocol, kern);
1101 net_family_read_unlock();
1104 module_put(net_families[family]->owner);
1110 int sock_create(int family, int type, int protocol, struct socket **res)
1112 return __sock_create(family, type, protocol, res, 0);
1115 int sock_create_kern(int family, int type, int protocol, struct socket **res)
1117 return __sock_create(family, type, protocol, res, 1);
1120 asmlinkage long sys_socket(int family, int type, int protocol)
1123 struct socket *sock;
1125 retval = sock_create(family, type, protocol, &sock);
1129 retval = sock_map_fd(sock);
1134 /* It may be already another descriptor 8) Not kernel problem. */
1143 * Create a pair of connected sockets.
1146 asmlinkage long sys_socketpair(int family, int type, int protocol, int __user *usockvec)
1148 struct socket *sock1, *sock2;
1152 * Obtain the first socket and check if the underlying protocol
1153 * supports the socketpair call.
1156 err = sock_create(family, type, protocol, &sock1);
1160 err = sock_create(family, type, protocol, &sock2);
1164 err = sock1->ops->socketpair(sock1, sock2);
1166 goto out_release_both;
1170 err = sock_map_fd(sock1);
1172 goto out_release_both;
1175 err = sock_map_fd(sock2);
1180 /* fd1 and fd2 may be already another descriptors.
1181 * Not kernel problem.
1184 err = put_user(fd1, &usockvec[0]);
1186 err = put_user(fd2, &usockvec[1]);
1195 sock_release(sock2);
1200 sock_release(sock2);
1202 sock_release(sock1);
1209 * Bind a name to a socket. Nothing much to do here since it's
1210 * the protocol's responsibility to handle the local address.
1212 * We move the socket address to kernel space before we call
1213 * the protocol layer (having also checked the address is ok).
1216 asmlinkage long sys_bind(int fd, struct sockaddr __user *umyaddr, int addrlen)
1218 struct socket *sock;
1219 char address[MAX_SOCK_ADDR];
1222 if((sock = sockfd_lookup(fd,&err))!=NULL)
1224 if((err=move_addr_to_kernel(umyaddr,addrlen,address))>=0) {
1225 err = security_socket_bind(sock, (struct sockaddr *)address, addrlen);
1230 err = sock->ops->bind(sock, (struct sockaddr *)address, addrlen);
1239 * Perform a listen. Basically, we allow the protocol to do anything
1240 * necessary for a listen, and if that works, we mark the socket as
1241 * ready for listening.
1244 int sysctl_somaxconn = SOMAXCONN;
1246 asmlinkage long sys_listen(int fd, int backlog)
1248 struct socket *sock;
1251 if ((sock = sockfd_lookup(fd, &err)) != NULL) {
1252 if ((unsigned) backlog > sysctl_somaxconn)
1253 backlog = sysctl_somaxconn;
1255 err = security_socket_listen(sock, backlog);
1261 err=sock->ops->listen(sock, backlog);
1269 * For accept, we attempt to create a new socket, set up the link
1270 * with the client, wake up the client, then return the new
1271 * connected fd. We collect the address of the connector in kernel
1272 * space and move it to user at the very end. This is unclean because
1273 * we open the socket then return an error.
1275 * 1003.1g adds the ability to recvmsg() to query connection pending
1276 * status to recvmsg. We need to add that support in a way thats
1277 * clean when we restucture accept also.
1280 asmlinkage long sys_accept(int fd, struct sockaddr __user *upeer_sockaddr, int __user *upeer_addrlen)
1282 struct socket *sock, *newsock;
1284 char address[MAX_SOCK_ADDR];
1286 sock = sockfd_lookup(fd, &err);
1291 if (!(newsock = sock_alloc()))
1294 newsock->type = sock->type;
1295 newsock->ops = sock->ops;
1297 err = security_socket_accept(sock, newsock);
1302 * We don't need try_module_get here, as the listening socket (sock)
1303 * has the protocol module (sock->ops->owner) held.
1305 __module_get(newsock->ops->owner);
1307 err = sock->ops->accept(sock, newsock, sock->file->f_flags);
1311 if (upeer_sockaddr) {
1312 if(newsock->ops->getname(newsock, (struct sockaddr *)address, &len, 2)<0) {
1313 err = -ECONNABORTED;
1316 err = move_addr_to_user(address, len, upeer_sockaddr, upeer_addrlen);
1321 /* File flags are not inherited via accept() unlike another OSes. */
1323 if ((err = sock_map_fd(newsock)) < 0)
1326 security_socket_post_accept(sock, newsock);
1333 sock_release(newsock);
1339 * Attempt to connect to a socket with the server address. The address
1340 * is in user space so we verify it is OK and move it to kernel space.
1342 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
1345 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
1346 * other SEQPACKET protocols that take time to connect() as it doesn't
1347 * include the -EINPROGRESS status for such sockets.
1350 asmlinkage long sys_connect(int fd, struct sockaddr __user *uservaddr, int addrlen)
1352 struct socket *sock;
1353 char address[MAX_SOCK_ADDR];
1356 sock = sockfd_lookup(fd, &err);
1359 err = move_addr_to_kernel(uservaddr, addrlen, address);
1363 err = security_socket_connect(sock, (struct sockaddr *)address, addrlen);
1367 err = sock->ops->connect(sock, (struct sockaddr *) address, addrlen,
1368 sock->file->f_flags);
1376 * Get the local address ('name') of a socket object. Move the obtained
1377 * name to user space.
1380 asmlinkage long sys_getsockname(int fd, struct sockaddr __user *usockaddr, int __user *usockaddr_len)
1382 struct socket *sock;
1383 char address[MAX_SOCK_ADDR];
1386 sock = sockfd_lookup(fd, &err);
1390 err = security_socket_getsockname(sock);
1394 err = sock->ops->getname(sock, (struct sockaddr *)address, &len, 0);
1397 err = move_addr_to_user(address, len, usockaddr, usockaddr_len);
1406 * Get the remote address ('name') of a socket object. Move the obtained
1407 * name to user space.
1410 asmlinkage long sys_getpeername(int fd, struct sockaddr __user *usockaddr, int __user *usockaddr_len)
1412 struct socket *sock;
1413 char address[MAX_SOCK_ADDR];
1416 if ((sock = sockfd_lookup(fd, &err))!=NULL)
1418 err = security_socket_getpeername(sock);
1424 err = sock->ops->getname(sock, (struct sockaddr *)address, &len, 1);
1426 err=move_addr_to_user(address,len, usockaddr, usockaddr_len);
1433 * Send a datagram to a given address. We move the address into kernel
1434 * space and check the user space data area is readable before invoking
1438 asmlinkage long sys_sendto(int fd, void __user * buff, size_t len, unsigned flags,
1439 struct sockaddr __user *addr, int addr_len)
1441 struct socket *sock;
1442 char address[MAX_SOCK_ADDR];
1447 sock = sockfd_lookup(fd, &err);
1455 msg.msg_control=NULL;
1456 msg.msg_controllen=0;
1460 err = move_addr_to_kernel(addr, addr_len, address);
1463 msg.msg_name=address;
1464 msg.msg_namelen=addr_len;
1466 if (sock->file->f_flags & O_NONBLOCK)
1467 flags |= MSG_DONTWAIT;
1468 msg.msg_flags = flags;
1469 err = sock_sendmsg(sock, &msg, len);
1478 * Send a datagram down a socket.
1481 asmlinkage long sys_send(int fd, void __user * buff, size_t len, unsigned flags)
1483 return sys_sendto(fd, buff, len, flags, NULL, 0);
1487 * Receive a frame from the socket and optionally record the address of the
1488 * sender. We verify the buffers are writable and if needed move the
1489 * sender address from kernel to user space.
1492 asmlinkage long sys_recvfrom(int fd, void __user * ubuf, size_t size, unsigned flags,
1493 struct sockaddr __user *addr, int __user *addr_len)
1495 struct socket *sock;
1498 char address[MAX_SOCK_ADDR];
1501 sock = sockfd_lookup(fd, &err);
1505 msg.msg_control=NULL;
1506 msg.msg_controllen=0;
1511 msg.msg_name=address;
1512 msg.msg_namelen=MAX_SOCK_ADDR;
1513 if (sock->file->f_flags & O_NONBLOCK)
1514 flags |= MSG_DONTWAIT;
1515 err=sock_recvmsg(sock, &msg, size, flags);
1517 if(err >= 0 && addr != NULL)
1519 err2=move_addr_to_user(address, msg.msg_namelen, addr, addr_len);
1529 * Receive a datagram from a socket.
1532 asmlinkage long sys_recv(int fd, void __user * ubuf, size_t size, unsigned flags)
1534 return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
1538 * Set a socket option. Because we don't know the option lengths we have
1539 * to pass the user mode parameter for the protocols to sort out.
1542 asmlinkage long sys_setsockopt(int fd, int level, int optname, char __user *optval, int optlen)
1545 struct socket *sock;
1550 if ((sock = sockfd_lookup(fd, &err))!=NULL)
1552 err = security_socket_setsockopt(sock,level,optname);
1558 if (level == SOL_SOCKET)
1559 err=sock_setsockopt(sock,level,optname,optval,optlen);
1561 err=sock->ops->setsockopt(sock, level, optname, optval, optlen);
1568 * Get a socket option. Because we don't know the option lengths we have
1569 * to pass a user mode parameter for the protocols to sort out.
1572 asmlinkage long sys_getsockopt(int fd, int level, int optname, char __user *optval, int __user *optlen)
1575 struct socket *sock;
1577 if ((sock = sockfd_lookup(fd, &err))!=NULL)
1579 err = security_socket_getsockopt(sock, level,
1586 if (level == SOL_SOCKET)
1587 err=sock_getsockopt(sock,level,optname,optval,optlen);
1589 err=sock->ops->getsockopt(sock, level, optname, optval, optlen);
1597 * Shutdown a socket.
1600 asmlinkage long sys_shutdown(int fd, int how)
1603 struct socket *sock;
1605 if ((sock = sockfd_lookup(fd, &err))!=NULL)
1607 err = security_socket_shutdown(sock, how);
1613 err=sock->ops->shutdown(sock, how);
1619 /* A couple of helpful macros for getting the address of the 32/64 bit
1620 * fields which are the same type (int / unsigned) on our platforms.
1622 #define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
1623 #define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
1624 #define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
1628 * BSD sendmsg interface
1631 asmlinkage long sys_sendmsg(int fd, struct msghdr __user *msg, unsigned flags)
1633 struct compat_msghdr __user *msg_compat = (struct compat_msghdr __user *)msg;
1634 struct socket *sock;
1635 char address[MAX_SOCK_ADDR];
1636 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
1637 unsigned char ctl[sizeof(struct cmsghdr) + 20]; /* 20 is size of ipv6_pktinfo */
1638 unsigned char *ctl_buf = ctl;
1639 struct msghdr msg_sys;
1640 int err, ctl_len, iov_size, total_len;
1643 if (MSG_CMSG_COMPAT & flags) {
1644 if (get_compat_msghdr(&msg_sys, msg_compat))
1646 } else if (copy_from_user(&msg_sys, msg, sizeof(struct msghdr)))
1649 sock = sockfd_lookup(fd, &err);
1653 /* do not move before msg_sys is valid */
1655 if (msg_sys.msg_iovlen > UIO_MAXIOV)
1658 /* Check whether to allocate the iovec area*/
1660 iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
1661 if (msg_sys.msg_iovlen > UIO_FASTIOV) {
1662 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
1667 /* This will also move the address data into kernel space */
1668 if (MSG_CMSG_COMPAT & flags) {
1669 err = verify_compat_iovec(&msg_sys, iov, address, VERIFY_READ);
1671 err = verify_iovec(&msg_sys, iov, address, VERIFY_READ);
1678 if (msg_sys.msg_controllen > INT_MAX)
1680 ctl_len = msg_sys.msg_controllen;
1681 if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
1682 err = cmsghdr_from_user_compat_to_kern(&msg_sys, ctl, sizeof(ctl));
1685 ctl_buf = msg_sys.msg_control;
1686 } else if (ctl_len) {
1687 if (ctl_len > sizeof(ctl))
1689 ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
1690 if (ctl_buf == NULL)
1695 * Careful! Before this, msg_sys.msg_control contains a user pointer.
1696 * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
1697 * checking falls down on this.
1699 if (copy_from_user(ctl_buf, (void __user *) msg_sys.msg_control, ctl_len))
1701 msg_sys.msg_control = ctl_buf;
1703 msg_sys.msg_flags = flags;
1705 if (sock->file->f_flags & O_NONBLOCK)
1706 msg_sys.msg_flags |= MSG_DONTWAIT;
1707 err = sock_sendmsg(sock, &msg_sys, total_len);
1711 sock_kfree_s(sock->sk, ctl_buf, ctl_len);
1713 if (iov != iovstack)
1714 sock_kfree_s(sock->sk, iov, iov_size);
1722 * BSD recvmsg interface
1725 asmlinkage long sys_recvmsg(int fd, struct msghdr __user *msg, unsigned int flags)
1727 struct compat_msghdr __user *msg_compat = (struct compat_msghdr __user *)msg;
1728 struct socket *sock;
1729 struct iovec iovstack[UIO_FASTIOV];
1730 struct iovec *iov=iovstack;
1731 struct msghdr msg_sys;
1732 unsigned long cmsg_ptr;
1733 int err, iov_size, total_len, len;
1735 /* kernel mode address */
1736 char addr[MAX_SOCK_ADDR];
1738 /* user mode address pointers */
1739 struct sockaddr __user *uaddr;
1740 int __user *uaddr_len;
1742 if (MSG_CMSG_COMPAT & flags) {
1743 if (get_compat_msghdr(&msg_sys, msg_compat))
1746 if (copy_from_user(&msg_sys,msg,sizeof(struct msghdr)))
1749 sock = sockfd_lookup(fd, &err);
1754 if (msg_sys.msg_iovlen > UIO_MAXIOV)
1757 /* Check whether to allocate the iovec area*/
1759 iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
1760 if (msg_sys.msg_iovlen > UIO_FASTIOV) {
1761 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
1767 * Save the user-mode address (verify_iovec will change the
1768 * kernel msghdr to use the kernel address space)
1771 uaddr = (void __user *) msg_sys.msg_name;
1772 uaddr_len = COMPAT_NAMELEN(msg);
1773 if (MSG_CMSG_COMPAT & flags) {
1774 err = verify_compat_iovec(&msg_sys, iov, addr, VERIFY_WRITE);
1776 err = verify_iovec(&msg_sys, iov, addr, VERIFY_WRITE);
1781 cmsg_ptr = (unsigned long)msg_sys.msg_control;
1782 msg_sys.msg_flags = 0;
1783 if (MSG_CMSG_COMPAT & flags)
1784 msg_sys.msg_flags = MSG_CMSG_COMPAT;
1786 if (sock->file->f_flags & O_NONBLOCK)
1787 flags |= MSG_DONTWAIT;
1788 err = sock_recvmsg(sock, &msg_sys, total_len, flags);
1793 if (uaddr != NULL) {
1794 err = move_addr_to_user(addr, msg_sys.msg_namelen, uaddr, uaddr_len);
1798 err = __put_user(msg_sys.msg_flags, COMPAT_FLAGS(msg));
1801 if (MSG_CMSG_COMPAT & flags)
1802 err = __put_user((unsigned long)msg_sys.msg_control-cmsg_ptr,
1803 &msg_compat->msg_controllen);
1805 err = __put_user((unsigned long)msg_sys.msg_control-cmsg_ptr,
1806 &msg->msg_controllen);
1812 if (iov != iovstack)
1813 sock_kfree_s(sock->sk, iov, iov_size);
1820 /* Argument list sizes for sys_socketcall */
1821 #define AL(x) ((x) * sizeof(unsigned long))
1822 static unsigned char nargs[18]={AL(0),AL(3),AL(3),AL(3),AL(2),AL(3),
1823 AL(3),AL(3),AL(4),AL(4),AL(4),AL(6),
1824 AL(6),AL(2),AL(5),AL(5),AL(3),AL(3)};
1828 * System call vectors.
1830 * Argument checking cleaned up. Saved 20% in size.
1831 * This function doesn't need to set the kernel lock because
1832 * it is set by the callees.
1835 asmlinkage long sys_socketcall(int call, unsigned long __user *args)
1838 unsigned long a0,a1;
1841 if(call<1||call>SYS_RECVMSG)
1844 /* copy_from_user should be SMP safe. */
1845 if (copy_from_user(a, args, nargs[call]))
1854 err = sys_socket(a0,a1,a[2]);
1857 err = sys_bind(a0,(struct sockaddr __user *)a1, a[2]);
1860 err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
1863 err = sys_listen(a0,a1);
1866 err = sys_accept(a0,(struct sockaddr __user *)a1, (int __user *)a[2]);
1868 case SYS_GETSOCKNAME:
1869 err = sys_getsockname(a0,(struct sockaddr __user *)a1, (int __user *)a[2]);
1871 case SYS_GETPEERNAME:
1872 err = sys_getpeername(a0, (struct sockaddr __user *)a1, (int __user *)a[2]);
1874 case SYS_SOCKETPAIR:
1875 err = sys_socketpair(a0,a1, a[2], (int __user *)a[3]);
1878 err = sys_send(a0, (void __user *)a1, a[2], a[3]);
1881 err = sys_sendto(a0,(void __user *)a1, a[2], a[3],
1882 (struct sockaddr __user *)a[4], a[5]);
1885 err = sys_recv(a0, (void __user *)a1, a[2], a[3]);
1888 err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
1889 (struct sockaddr __user *)a[4], (int __user *)a[5]);
1892 err = sys_shutdown(a0,a1);
1894 case SYS_SETSOCKOPT:
1895 err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]);
1897 case SYS_GETSOCKOPT:
1898 err = sys_getsockopt(a0, a1, a[2], (char __user *)a[3], (int __user *)a[4]);
1901 err = sys_sendmsg(a0, (struct msghdr __user *) a1, a[2]);
1904 err = sys_recvmsg(a0, (struct msghdr __user *) a1, a[2]);
1914 * This function is called by a protocol handler that wants to
1915 * advertise its address family, and have it linked into the
1919 int sock_register(struct net_proto_family *ops)
1923 if (ops->family >= NPROTO) {
1924 printk(KERN_CRIT "protocol %d >= NPROTO(%d)\n", ops->family, NPROTO);
1927 net_family_write_lock();
1929 if (net_families[ops->family] == NULL) {
1930 net_families[ops->family]=ops;
1933 net_family_write_unlock();
1934 printk(KERN_INFO "NET: Registered protocol family %d\n",
1940 * This function is called by a protocol handler that wants to
1941 * remove its address family, and have it unlinked from the
1945 int sock_unregister(int family)
1947 if (family < 0 || family >= NPROTO)
1950 net_family_write_lock();
1951 net_families[family]=NULL;
1952 net_family_write_unlock();
1953 printk(KERN_INFO "NET: Unregistered protocol family %d\n",
1959 extern void sk_init(void);
1961 void __init sock_init(void)
1966 * Initialize all address (protocol) families.
1969 for (i = 0; i < NPROTO; i++)
1970 net_families[i] = NULL;
1973 * Initialize sock SLAB cache.
1980 * Initialize skbuff SLAB cache
1986 * Initialize the protocols module.
1990 register_filesystem(&sock_fs_type);
1991 sock_mnt = kern_mount(&sock_fs_type);
1992 /* The real protocol initialization is performed when
1993 * do_initcalls is run.
1996 #ifdef CONFIG_NETFILTER
2001 #ifdef CONFIG_PROC_FS
2002 void socket_seq_show(struct seq_file *seq)
2007 for (cpu = 0; cpu < NR_CPUS; cpu++)
2008 counter += per_cpu(sockets_in_use, cpu);
2010 /* It can be negative, by the way. 8) */
2014 seq_printf(seq, "sockets: used %d\n", counter);
2016 #endif /* CONFIG_PROC_FS */
2018 /* ABI emulation layers need these two */
2019 EXPORT_SYMBOL(move_addr_to_kernel);
2020 EXPORT_SYMBOL(move_addr_to_user);
2021 EXPORT_SYMBOL(sock_alloc);
2022 EXPORT_SYMBOL(sock_alloc_inode);
2023 EXPORT_SYMBOL(sock_create);
2024 EXPORT_SYMBOL(sock_create_kern);
2025 EXPORT_SYMBOL(sock_create_lite);
2026 EXPORT_SYMBOL(sock_map_fd);
2027 EXPORT_SYMBOL(sock_recvmsg);
2028 EXPORT_SYMBOL(sock_register);
2029 EXPORT_SYMBOL(sock_release);
2030 EXPORT_SYMBOL(sock_sendmsg);
2031 EXPORT_SYMBOL(sock_unregister);
2032 EXPORT_SYMBOL(sock_wake_async);
2033 EXPORT_SYMBOL(sockfd_lookup);