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>
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>
72 #include <linux/mutex.h>
73 #include <linux/wanrouter.h>
74 #include <linux/if_bridge.h>
75 #include <linux/if_frad.h>
76 #include <linux/if_vlan.h>
77 #include <linux/init.h>
78 #include <linux/poll.h>
79 #include <linux/cache.h>
80 #include <linux/module.h>
81 #include <linux/highmem.h>
82 #include <linux/divert.h>
83 #include <linux/mount.h>
84 #include <linux/security.h>
85 #include <linux/syscalls.h>
86 #include <linux/compat.h>
87 #include <linux/kmod.h>
88 #include <linux/audit.h>
89 #include <linux/wireless.h>
91 #include <asm/uaccess.h>
92 #include <asm/unistd.h>
94 #include <net/compat.h>
97 #include <linux/netfilter.h>
98 #include <linux/vs_socket.h>
100 static int sock_no_open(struct inode *irrelevant, struct file *dontcare);
101 static ssize_t sock_aio_read(struct kiocb *iocb, char __user *buf,
102 size_t size, loff_t pos);
103 static ssize_t sock_aio_write(struct kiocb *iocb, const char __user *buf,
104 size_t size, loff_t pos);
105 static int sock_mmap(struct file *file, struct vm_area_struct * vma);
107 static int sock_close(struct inode *inode, struct file *file);
108 static unsigned int sock_poll(struct file *file,
109 struct poll_table_struct *wait);
110 static long sock_ioctl(struct file *file,
111 unsigned int cmd, unsigned long arg);
113 static long compat_sock_ioctl(struct file *file,
114 unsigned int cmd, unsigned long arg);
116 static int sock_fasync(int fd, struct file *filp, int on);
117 static ssize_t sock_readv(struct file *file, const struct iovec *vector,
118 unsigned long count, loff_t *ppos);
119 static ssize_t sock_writev(struct file *file, const struct iovec *vector,
120 unsigned long count, loff_t *ppos);
121 static ssize_t sock_sendpage(struct file *file, struct page *page,
122 int offset, size_t size, loff_t *ppos, int more);
125 * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
126 * in the operation structures but are done directly via the socketcall() multiplexor.
129 struct file_operations socket_file_ops = {
130 .owner = THIS_MODULE,
132 .aio_read = sock_aio_read,
133 .aio_write = sock_aio_write,
135 .unlocked_ioctl = sock_ioctl,
137 .compat_ioctl = compat_sock_ioctl,
140 .open = sock_no_open, /* special open code to disallow open via /proc */
141 .release = sock_close,
142 .fasync = sock_fasync,
144 .writev = sock_writev,
145 .sendpage = sock_sendpage,
146 .splice_write = generic_splice_sendpage,
150 * The protocol list. Each protocol is registered in here.
153 static struct net_proto_family *net_families[NPROTO];
155 #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
156 static atomic_t net_family_lockct = ATOMIC_INIT(0);
157 static DEFINE_SPINLOCK(net_family_lock);
159 /* The strategy is: modifications net_family vector are short, do not
160 sleep and veeery rare, but read access should be free of any exclusive
164 static void net_family_write_lock(void)
166 spin_lock(&net_family_lock);
167 while (atomic_read(&net_family_lockct) != 0) {
168 spin_unlock(&net_family_lock);
172 spin_lock(&net_family_lock);
176 static __inline__ void net_family_write_unlock(void)
178 spin_unlock(&net_family_lock);
181 static __inline__ void net_family_read_lock(void)
183 atomic_inc(&net_family_lockct);
184 spin_unlock_wait(&net_family_lock);
187 static __inline__ void net_family_read_unlock(void)
189 atomic_dec(&net_family_lockct);
193 #define net_family_write_lock() do { } while(0)
194 #define net_family_write_unlock() do { } while(0)
195 #define net_family_read_lock() do { } while(0)
196 #define net_family_read_unlock() do { } while(0)
201 * Statistics counters of the socket lists
204 static DEFINE_PER_CPU(int, sockets_in_use) = 0;
207 * Support routines. Move socket addresses back and forth across the kernel/user
208 * divide and look after the messy bits.
211 #define MAX_SOCK_ADDR 128 /* 108 for Unix domain -
212 16 for IP, 16 for IPX,
215 must be at least one bigger than
216 the AF_UNIX size (see net/unix/af_unix.c
221 * move_addr_to_kernel - copy a socket address into kernel space
222 * @uaddr: Address in user space
223 * @kaddr: Address in kernel space
224 * @ulen: Length in user space
226 * The address is copied into kernel space. If the provided address is
227 * too long an error code of -EINVAL is returned. If the copy gives
228 * invalid addresses -EFAULT is returned. On a success 0 is returned.
231 int move_addr_to_kernel(void __user *uaddr, int ulen, void *kaddr)
233 if(ulen<0||ulen>MAX_SOCK_ADDR)
237 if(copy_from_user(kaddr,uaddr,ulen))
239 return audit_sockaddr(ulen, kaddr);
243 * move_addr_to_user - copy an address to user space
244 * @kaddr: kernel space address
245 * @klen: length of address in kernel
246 * @uaddr: user space address
247 * @ulen: pointer to user length field
249 * The value pointed to by ulen on entry is the buffer length available.
250 * This is overwritten with the buffer space used. -EINVAL is returned
251 * if an overlong buffer is specified or a negative buffer size. -EFAULT
252 * is returned if either the buffer or the length field are not
254 * After copying the data up to the limit the user specifies, the true
255 * length of the data is written over the length limit the user
256 * specified. Zero is returned for a success.
259 int move_addr_to_user(void *kaddr, int klen, void __user *uaddr, int __user *ulen)
264 if((err=get_user(len, ulen)))
268 if(len<0 || len> MAX_SOCK_ADDR)
272 if (audit_sockaddr(klen, kaddr))
274 if(copy_to_user(uaddr,kaddr,len))
278 * "fromlen shall refer to the value before truncation.."
281 return __put_user(klen, ulen);
284 #define SOCKFS_MAGIC 0x534F434B
286 static kmem_cache_t * sock_inode_cachep __read_mostly;
288 static struct inode *sock_alloc_inode(struct super_block *sb)
290 struct socket_alloc *ei;
291 ei = (struct socket_alloc *)kmem_cache_alloc(sock_inode_cachep, SLAB_KERNEL);
294 init_waitqueue_head(&ei->socket.wait);
296 ei->socket.fasync_list = NULL;
297 ei->socket.state = SS_UNCONNECTED;
298 ei->socket.flags = 0;
299 ei->socket.ops = NULL;
300 ei->socket.sk = NULL;
301 ei->socket.file = NULL;
302 ei->socket.flags = 0;
304 return &ei->vfs_inode;
307 static void sock_destroy_inode(struct inode *inode)
309 kmem_cache_free(sock_inode_cachep,
310 container_of(inode, struct socket_alloc, vfs_inode));
313 static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
315 struct socket_alloc *ei = (struct socket_alloc *) foo;
317 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
318 SLAB_CTOR_CONSTRUCTOR)
319 inode_init_once(&ei->vfs_inode);
322 static int init_inodecache(void)
324 sock_inode_cachep = kmem_cache_create("sock_inode_cache",
325 sizeof(struct socket_alloc),
326 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
329 if (sock_inode_cachep == NULL)
334 static struct super_operations sockfs_ops = {
335 .alloc_inode = sock_alloc_inode,
336 .destroy_inode =sock_destroy_inode,
337 .statfs = simple_statfs,
340 static struct super_block *sockfs_get_sb(struct file_system_type *fs_type,
341 int flags, const char *dev_name, void *data)
343 return get_sb_pseudo(fs_type, "socket:", &sockfs_ops, SOCKFS_MAGIC);
346 static struct vfsmount *sock_mnt __read_mostly;
348 static struct file_system_type sock_fs_type = {
350 .get_sb = sockfs_get_sb,
351 .kill_sb = kill_anon_super,
353 static int sockfs_delete_dentry(struct dentry *dentry)
357 static struct dentry_operations sockfs_dentry_operations = {
358 .d_delete = sockfs_delete_dentry,
362 * Obtains the first available file descriptor and sets it up for use.
364 * These functions create file structures and maps them to fd space
365 * of the current process. On success it returns file descriptor
366 * and file struct implicitly stored in sock->file.
367 * Note that another thread may close file descriptor before we return
368 * from this function. We use the fact that now we do not refer
369 * to socket after mapping. If one day we will need it, this
370 * function will increment ref. count on file by 1.
372 * In any case returned fd MAY BE not valid!
373 * This race condition is unavoidable
374 * with shared fd spaces, we cannot solve it inside kernel,
375 * but we take care of internal coherence yet.
378 static int sock_alloc_fd(struct file **filep)
382 fd = get_unused_fd();
383 if (likely(fd >= 0)) {
384 struct file *file = get_empty_filp();
387 if (unlikely(!file)) {
396 static int sock_attach_fd(struct socket *sock, struct file *file)
401 this.len = sprintf(name, "[%lu]", SOCK_INODE(sock)->i_ino);
403 this.hash = SOCK_INODE(sock)->i_ino;
405 file->f_dentry = d_alloc(sock_mnt->mnt_sb->s_root, &this);
406 if (unlikely(!file->f_dentry))
409 file->f_dentry->d_op = &sockfs_dentry_operations;
410 d_add(file->f_dentry, SOCK_INODE(sock));
411 file->f_vfsmnt = mntget(sock_mnt);
412 file->f_mapping = file->f_dentry->d_inode->i_mapping;
415 file->f_op = SOCK_INODE(sock)->i_fop = &socket_file_ops;
416 file->f_mode = FMODE_READ | FMODE_WRITE;
417 file->f_flags = O_RDWR;
419 file->private_data = sock;
424 int sock_map_fd(struct socket *sock)
426 struct file *newfile;
427 int fd = sock_alloc_fd(&newfile);
429 if (likely(fd >= 0)) {
430 int err = sock_attach_fd(sock, newfile);
432 if (unlikely(err < 0)) {
437 fd_install(fd, newfile);
442 static struct socket *sock_from_file(struct file *file, int *err)
447 if (file->f_op == &socket_file_ops)
448 return file->private_data; /* set in sock_map_fd */
450 inode = file->f_dentry->d_inode;
451 if (!S_ISSOCK(inode->i_mode)) {
456 sock = SOCKET_I(inode);
457 if (sock->file != file) {
458 printk(KERN_ERR "socki_lookup: socket file changed!\n");
465 * sockfd_lookup - Go from a file number to its socket slot
467 * @err: pointer to an error code return
469 * The file handle passed in is locked and the socket it is bound
470 * too is returned. If an error occurs the err pointer is overwritten
471 * with a negative errno code and NULL is returned. The function checks
472 * for both invalid handles and passing a handle which is not a socket.
474 * On a success the socket object pointer is returned.
477 struct socket *sockfd_lookup(int fd, int *err)
482 if (!(file = fget(fd))) {
486 sock = sock_from_file(file, err);
492 static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed)
498 file = fget_light(fd, fput_needed);
500 sock = sock_from_file(file, err);
503 fput_light(file, *fput_needed);
509 * sock_alloc - allocate a socket
511 * Allocate a new inode and socket object. The two are bound together
512 * and initialised. The socket is then returned. If we are out of inodes
516 struct socket *sock_alloc(void)
518 struct inode * inode;
519 struct socket * sock;
521 inode = new_inode(sock_mnt->mnt_sb);
525 sock = SOCKET_I(inode);
527 inode->i_mode = S_IFSOCK|S_IRWXUGO;
528 inode->i_uid = current->fsuid;
529 inode->i_gid = current->fsgid;
531 get_cpu_var(sockets_in_use)++;
532 put_cpu_var(sockets_in_use);
536 EXPORT_SYMBOL_GPL(sock_alloc);
539 * In theory you can't get an open on this inode, but /proc provides
540 * a back door. Remember to keep it shut otherwise you'll let the
541 * creepy crawlies in.
544 static int sock_no_open(struct inode *irrelevant, struct file *dontcare)
549 const struct file_operations bad_sock_fops = {
550 .owner = THIS_MODULE,
551 .open = sock_no_open,
555 * sock_release - close a socket
556 * @sock: socket to close
558 * The socket is released from the protocol stack if it has a release
559 * callback, and the inode is then released if the socket is bound to
560 * an inode not a file.
563 void sock_release(struct socket *sock)
566 struct module *owner = sock->ops->owner;
568 sock->ops->release(sock);
573 if (sock->fasync_list)
574 printk(KERN_ERR "sock_release: fasync list not empty!\n");
576 get_cpu_var(sockets_in_use)--;
577 put_cpu_var(sockets_in_use);
579 iput(SOCK_INODE(sock));
585 static inline int __sock_sendmsg(struct kiocb *iocb, struct socket *sock,
586 struct msghdr *msg, size_t size)
588 struct sock_iocb *si = kiocb_to_siocb(iocb);
596 err = security_socket_sendmsg(sock, msg, size);
600 len = sock->ops->sendmsg(iocb, sock, msg, size);
603 vx_sock_send(sock->sk, size);
605 vx_sock_fail(sock->sk, size);
607 vxdprintk(VXD_CBIT(net, 7),
608 "__sock_sendmsg: %p[%p,%p,%p;%d]:%d/%d",
610 (sock->sk)?sock->sk->sk_nx_info:0,
611 (sock->sk)?sock->sk->sk_vx_info:0,
612 (sock->sk)?sock->sk->sk_xid:0,
613 (unsigned int)size, len);
617 int sock_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
620 struct sock_iocb siocb;
623 init_sync_kiocb(&iocb, NULL);
624 iocb.private = &siocb;
625 ret = __sock_sendmsg(&iocb, sock, msg, size);
626 if (-EIOCBQUEUED == ret)
627 ret = wait_on_sync_kiocb(&iocb);
631 int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
632 struct kvec *vec, size_t num, size_t size)
634 mm_segment_t oldfs = get_fs();
639 * the following is safe, since for compiler definitions of kvec and
640 * iovec are identical, yielding the same in-core layout and alignment
642 msg->msg_iov = (struct iovec *)vec,
643 msg->msg_iovlen = num;
644 result = sock_sendmsg(sock, msg, size);
649 static inline int __sock_recvmsg(struct kiocb *iocb, struct socket *sock,
650 struct msghdr *msg, size_t size, int flags)
653 struct sock_iocb *si = kiocb_to_siocb(iocb);
661 err = security_socket_recvmsg(sock, msg, size, flags);
665 len = sock->ops->recvmsg(iocb, sock, msg, size, flags);
666 if ((len >= 0) && sock->sk)
667 vx_sock_recv(sock->sk, len);
668 vxdprintk(VXD_CBIT(net, 7),
669 "__sock_recvmsg: %p[%p,%p,%p;%d]:%d/%d",
671 (sock->sk)?sock->sk->sk_nx_info:0,
672 (sock->sk)?sock->sk->sk_vx_info:0,
673 (sock->sk)?sock->sk->sk_xid:0,
674 (unsigned int)size, len);
678 int sock_recvmsg(struct socket *sock, struct msghdr *msg,
679 size_t size, int flags)
682 struct sock_iocb siocb;
685 init_sync_kiocb(&iocb, NULL);
686 iocb.private = &siocb;
687 ret = __sock_recvmsg(&iocb, sock, msg, size, flags);
688 if (-EIOCBQUEUED == ret)
689 ret = wait_on_sync_kiocb(&iocb);
693 int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
694 struct kvec *vec, size_t num,
695 size_t size, int flags)
697 mm_segment_t oldfs = get_fs();
702 * the following is safe, since for compiler definitions of kvec and
703 * iovec are identical, yielding the same in-core layout and alignment
705 msg->msg_iov = (struct iovec *)vec,
706 msg->msg_iovlen = num;
707 result = sock_recvmsg(sock, msg, size, flags);
712 static void sock_aio_dtor(struct kiocb *iocb)
714 kfree(iocb->private);
717 static ssize_t sock_sendpage(struct file *file, struct page *page,
718 int offset, size_t size, loff_t *ppos, int more)
723 sock = file->private_data;
725 flags = !(file->f_flags & O_NONBLOCK) ? 0 : MSG_DONTWAIT;
729 return sock->ops->sendpage(sock, page, offset, size, flags);
732 static struct sock_iocb *alloc_sock_iocb(struct kiocb *iocb,
733 char __user *ubuf, size_t size, struct sock_iocb *siocb)
735 if (!is_sync_kiocb(iocb)) {
736 siocb = kmalloc(sizeof(*siocb), GFP_KERNEL);
739 iocb->ki_dtor = sock_aio_dtor;
743 siocb->async_iov.iov_base = ubuf;
744 siocb->async_iov.iov_len = size;
746 iocb->private = siocb;
750 static ssize_t do_sock_read(struct msghdr *msg, struct kiocb *iocb,
751 struct file *file, struct iovec *iov, unsigned long nr_segs)
753 struct socket *sock = file->private_data;
757 for (i = 0 ; i < nr_segs ; i++)
758 size += iov[i].iov_len;
760 msg->msg_name = NULL;
761 msg->msg_namelen = 0;
762 msg->msg_control = NULL;
763 msg->msg_controllen = 0;
764 msg->msg_iov = (struct iovec *) iov;
765 msg->msg_iovlen = nr_segs;
766 msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
768 return __sock_recvmsg(iocb, sock, msg, size, msg->msg_flags);
771 static ssize_t sock_readv(struct file *file, const struct iovec *iov,
772 unsigned long nr_segs, loff_t *ppos)
775 struct sock_iocb siocb;
779 init_sync_kiocb(&iocb, NULL);
780 iocb.private = &siocb;
782 ret = do_sock_read(&msg, &iocb, file, (struct iovec *)iov, nr_segs);
783 if (-EIOCBQUEUED == ret)
784 ret = wait_on_sync_kiocb(&iocb);
788 static ssize_t sock_aio_read(struct kiocb *iocb, char __user *ubuf,
789 size_t count, loff_t pos)
791 struct sock_iocb siocb, *x;
795 if (count == 0) /* Match SYS5 behaviour */
798 x = alloc_sock_iocb(iocb, ubuf, count, &siocb);
801 return do_sock_read(&x->async_msg, iocb, iocb->ki_filp,
805 static ssize_t do_sock_write(struct msghdr *msg, struct kiocb *iocb,
806 struct file *file, struct iovec *iov, unsigned long nr_segs)
808 struct socket *sock = file->private_data;
812 for (i = 0 ; i < nr_segs ; i++)
813 size += iov[i].iov_len;
815 msg->msg_name = NULL;
816 msg->msg_namelen = 0;
817 msg->msg_control = NULL;
818 msg->msg_controllen = 0;
819 msg->msg_iov = (struct iovec *) iov;
820 msg->msg_iovlen = nr_segs;
821 msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
822 if (sock->type == SOCK_SEQPACKET)
823 msg->msg_flags |= MSG_EOR;
825 return __sock_sendmsg(iocb, sock, msg, size);
828 static ssize_t sock_writev(struct file *file, const struct iovec *iov,
829 unsigned long nr_segs, loff_t *ppos)
833 struct sock_iocb siocb;
836 init_sync_kiocb(&iocb, NULL);
837 iocb.private = &siocb;
839 ret = do_sock_write(&msg, &iocb, file, (struct iovec *)iov, nr_segs);
840 if (-EIOCBQUEUED == ret)
841 ret = wait_on_sync_kiocb(&iocb);
845 static ssize_t sock_aio_write(struct kiocb *iocb, const char __user *ubuf,
846 size_t count, loff_t pos)
848 struct sock_iocb siocb, *x;
852 if (count == 0) /* Match SYS5 behaviour */
855 x = alloc_sock_iocb(iocb, (void __user *)ubuf, count, &siocb);
859 return do_sock_write(&x->async_msg, iocb, iocb->ki_filp,
865 * Atomic setting of ioctl hooks to avoid race
866 * with module unload.
869 static DEFINE_MUTEX(br_ioctl_mutex);
870 static int (*br_ioctl_hook)(unsigned int cmd, void __user *arg) = NULL;
872 void brioctl_set(int (*hook)(unsigned int, void __user *))
874 mutex_lock(&br_ioctl_mutex);
875 br_ioctl_hook = hook;
876 mutex_unlock(&br_ioctl_mutex);
878 EXPORT_SYMBOL(brioctl_set);
880 static DEFINE_MUTEX(vlan_ioctl_mutex);
881 static int (*vlan_ioctl_hook)(void __user *arg);
883 void vlan_ioctl_set(int (*hook)(void __user *))
885 mutex_lock(&vlan_ioctl_mutex);
886 vlan_ioctl_hook = hook;
887 mutex_unlock(&vlan_ioctl_mutex);
889 EXPORT_SYMBOL(vlan_ioctl_set);
891 static DEFINE_MUTEX(dlci_ioctl_mutex);
892 static int (*dlci_ioctl_hook)(unsigned int, void __user *);
894 void dlci_ioctl_set(int (*hook)(unsigned int, void __user *))
896 mutex_lock(&dlci_ioctl_mutex);
897 dlci_ioctl_hook = hook;
898 mutex_unlock(&dlci_ioctl_mutex);
900 EXPORT_SYMBOL(dlci_ioctl_set);
903 * With an ioctl, arg may well be a user mode pointer, but we don't know
904 * what to do with it - that's up to the protocol still.
907 static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
910 void __user *argp = (void __user *)arg;
913 sock = file->private_data;
914 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
915 err = dev_ioctl(cmd, argp);
917 #ifdef CONFIG_WIRELESS_EXT
918 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
919 err = dev_ioctl(cmd, argp);
921 #endif /* CONFIG_WIRELESS_EXT */
926 if (get_user(pid, (int __user *)argp))
928 err = f_setown(sock->file, pid, 1);
932 err = put_user(sock->file->f_owner.pid, (int __user *)argp);
940 request_module("bridge");
942 mutex_lock(&br_ioctl_mutex);
944 err = br_ioctl_hook(cmd, argp);
945 mutex_unlock(&br_ioctl_mutex);
950 if (!vlan_ioctl_hook)
951 request_module("8021q");
953 mutex_lock(&vlan_ioctl_mutex);
955 err = vlan_ioctl_hook(argp);
956 mutex_unlock(&vlan_ioctl_mutex);
960 /* Convert this to call through a hook */
961 err = divert_ioctl(cmd, argp);
966 if (!dlci_ioctl_hook)
967 request_module("dlci");
969 if (dlci_ioctl_hook) {
970 mutex_lock(&dlci_ioctl_mutex);
971 err = dlci_ioctl_hook(cmd, argp);
972 mutex_unlock(&dlci_ioctl_mutex);
976 err = sock->ops->ioctl(sock, cmd, arg);
979 * If this ioctl is unknown try to hand it down
982 if (err == -ENOIOCTLCMD)
983 err = dev_ioctl(cmd, argp);
989 int sock_create_lite(int family, int type, int protocol, struct socket **res)
992 struct socket *sock = NULL;
994 err = security_socket_create(family, type, protocol, 1);
1004 security_socket_post_create(sock, family, type, protocol, 1);
1011 /* No kernel lock held - perfect */
1012 static unsigned int sock_poll(struct file *file, poll_table * wait)
1014 struct socket *sock;
1017 * We can't return errors to poll, so it's either yes or no.
1019 sock = file->private_data;
1020 return sock->ops->poll(file, sock, wait);
1023 static int sock_mmap(struct file * file, struct vm_area_struct * vma)
1025 struct socket *sock = file->private_data;
1027 return sock->ops->mmap(file, sock, vma);
1030 static int sock_close(struct inode *inode, struct file *filp)
1033 * It was possible the inode is NULL we were
1034 * closing an unfinished socket.
1039 printk(KERN_DEBUG "sock_close: NULL inode\n");
1042 sock_fasync(-1, filp, 0);
1043 sock_release(SOCKET_I(inode));
1048 * Update the socket async list
1050 * Fasync_list locking strategy.
1052 * 1. fasync_list is modified only under process context socket lock
1053 * i.e. under semaphore.
1054 * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
1055 * or under socket lock.
1056 * 3. fasync_list can be used from softirq context, so that
1057 * modification under socket lock have to be enhanced with
1058 * write_lock_bh(&sk->sk_callback_lock).
1062 static int sock_fasync(int fd, struct file *filp, int on)
1064 struct fasync_struct *fa, *fna=NULL, **prev;
1065 struct socket *sock;
1070 fna = kmalloc(sizeof(struct fasync_struct), GFP_KERNEL);
1075 sock = filp->private_data;
1077 if ((sk=sock->sk) == NULL) {
1084 prev=&(sock->fasync_list);
1086 for (fa=*prev; fa!=NULL; prev=&fa->fa_next,fa=*prev)
1087 if (fa->fa_file==filp)
1094 write_lock_bh(&sk->sk_callback_lock);
1096 write_unlock_bh(&sk->sk_callback_lock);
1103 fna->magic=FASYNC_MAGIC;
1104 fna->fa_next=sock->fasync_list;
1105 write_lock_bh(&sk->sk_callback_lock);
1106 sock->fasync_list=fna;
1107 write_unlock_bh(&sk->sk_callback_lock);
1113 write_lock_bh(&sk->sk_callback_lock);
1115 write_unlock_bh(&sk->sk_callback_lock);
1123 release_sock(sock->sk);
1127 /* This function may be called only under socket lock or callback_lock */
1129 int sock_wake_async(struct socket *sock, int how, int band)
1131 if (!sock || !sock->fasync_list)
1137 if (test_bit(SOCK_ASYNC_WAITDATA, &sock->flags))
1141 if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags))
1146 __kill_fasync(sock->fasync_list, SIGIO, band);
1149 __kill_fasync(sock->fasync_list, SIGURG, band);
1154 static int __sock_create(int family, int type, int protocol, struct socket **res, int kern)
1157 struct socket *sock;
1160 * Check protocol is in range
1162 if (family < 0 || family >= NPROTO)
1163 return -EAFNOSUPPORT;
1164 if (type < 0 || type >= SOCK_MAX)
1167 /* disable IPv6 inside vservers for now */
1168 if (family == PF_INET6 && !vx_check(0, VX_ADMIN))
1169 return -EAFNOSUPPORT;
1173 This uglymoron is moved from INET layer to here to avoid
1174 deadlock in module load.
1176 if (family == PF_INET && type == SOCK_PACKET) {
1180 printk(KERN_INFO "%s uses obsolete (PF_INET,SOCK_PACKET)\n", current->comm);
1185 err = security_socket_create(family, type, protocol, kern);
1189 #if defined(CONFIG_KMOD)
1190 /* Attempt to load a protocol module if the find failed.
1192 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
1193 * requested real, full-featured networking support upon configuration.
1194 * Otherwise module support will break!
1196 if (net_families[family]==NULL)
1198 request_module("net-pf-%d",family);
1202 net_family_read_lock();
1203 if (net_families[family] == NULL) {
1204 err = -EAFNOSUPPORT;
1209 * Allocate the socket and allow the family to set things up. if
1210 * the protocol is 0, the family is instructed to select an appropriate
1214 if (!(sock = sock_alloc())) {
1215 printk(KERN_WARNING "socket: no more sockets\n");
1216 err = -ENFILE; /* Not exactly a match, but its the
1217 closest posix thing */
1224 * We will call the ->create function, that possibly is in a loadable
1225 * module, so we have to bump that loadable module refcnt first.
1227 err = -EAFNOSUPPORT;
1228 if (!try_module_get(net_families[family]->owner))
1231 if ((err = net_families[family]->create(sock, protocol)) < 0) {
1233 goto out_module_put;
1237 * Now to bump the refcnt of the [loadable] module that owns this
1238 * socket at sock_release time we decrement its refcnt.
1240 if (!try_module_get(sock->ops->owner)) {
1242 goto out_module_put;
1245 * Now that we're done with the ->create function, the [loadable]
1246 * module can have its refcnt decremented
1248 module_put(net_families[family]->owner);
1250 security_socket_post_create(sock, family, type, protocol, kern);
1253 net_family_read_unlock();
1256 module_put(net_families[family]->owner);
1262 int sock_create(int family, int type, int protocol, struct socket **res)
1264 return __sock_create(family, type, protocol, res, 0);
1267 int sock_create_kern(int family, int type, int protocol, struct socket **res)
1269 return __sock_create(family, type, protocol, res, 1);
1272 asmlinkage long sys_socket(int family, int type, int protocol)
1275 struct socket *sock;
1277 retval = sock_create(family, type, protocol, &sock);
1281 set_bit(SOCK_USER_SOCKET, &sock->flags);
1282 retval = sock_map_fd(sock);
1287 /* It may be already another descriptor 8) Not kernel problem. */
1296 * Create a pair of connected sockets.
1299 asmlinkage long sys_socketpair(int family, int type, int protocol, int __user *usockvec)
1301 struct socket *sock1, *sock2;
1305 * Obtain the first socket and check if the underlying protocol
1306 * supports the socketpair call.
1309 err = sock_create(family, type, protocol, &sock1);
1312 set_bit(SOCK_USER_SOCKET, &sock1->flags);
1314 err = sock_create(family, type, protocol, &sock2);
1317 set_bit(SOCK_USER_SOCKET, &sock2->flags);
1319 err = sock1->ops->socketpair(sock1, sock2);
1321 goto out_release_both;
1325 err = sock_map_fd(sock1);
1327 goto out_release_both;
1330 err = sock_map_fd(sock2);
1335 /* fd1 and fd2 may be already another descriptors.
1336 * Not kernel problem.
1339 err = put_user(fd1, &usockvec[0]);
1341 err = put_user(fd2, &usockvec[1]);
1350 sock_release(sock2);
1355 sock_release(sock2);
1357 sock_release(sock1);
1364 * Bind a name to a socket. Nothing much to do here since it's
1365 * the protocol's responsibility to handle the local address.
1367 * We move the socket address to kernel space before we call
1368 * the protocol layer (having also checked the address is ok).
1371 asmlinkage long sys_bind(int fd, struct sockaddr __user *umyaddr, int addrlen)
1373 struct socket *sock;
1374 char address[MAX_SOCK_ADDR];
1375 int err, fput_needed;
1377 if((sock = sockfd_lookup_light(fd, &err, &fput_needed))!=NULL)
1379 if((err=move_addr_to_kernel(umyaddr,addrlen,address))>=0) {
1380 err = security_socket_bind(sock, (struct sockaddr *)address, addrlen);
1382 err = sock->ops->bind(sock,
1383 (struct sockaddr *)address, addrlen);
1385 fput_light(sock->file, fput_needed);
1392 * Perform a listen. Basically, we allow the protocol to do anything
1393 * necessary for a listen, and if that works, we mark the socket as
1394 * ready for listening.
1397 int sysctl_somaxconn = SOMAXCONN;
1399 asmlinkage long sys_listen(int fd, int backlog)
1401 struct socket *sock;
1402 int err, fput_needed;
1404 if ((sock = sockfd_lookup_light(fd, &err, &fput_needed)) != NULL) {
1405 if ((unsigned) backlog > sysctl_somaxconn)
1406 backlog = sysctl_somaxconn;
1408 err = security_socket_listen(sock, backlog);
1410 err = sock->ops->listen(sock, backlog);
1412 fput_light(sock->file, fput_needed);
1419 * For accept, we attempt to create a new socket, set up the link
1420 * with the client, wake up the client, then return the new
1421 * connected fd. We collect the address of the connector in kernel
1422 * space and move it to user at the very end. This is unclean because
1423 * we open the socket then return an error.
1425 * 1003.1g adds the ability to recvmsg() to query connection pending
1426 * status to recvmsg. We need to add that support in a way thats
1427 * clean when we restucture accept also.
1430 asmlinkage long sys_accept(int fd, struct sockaddr __user *upeer_sockaddr, int __user *upeer_addrlen)
1432 struct socket *sock, *newsock;
1433 struct file *newfile;
1434 int err, len, newfd, fput_needed;
1435 char address[MAX_SOCK_ADDR];
1437 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1442 if (!(newsock = sock_alloc()))
1445 newsock->type = sock->type;
1446 newsock->ops = sock->ops;
1449 * We don't need try_module_get here, as the listening socket (sock)
1450 * has the protocol module (sock->ops->owner) held.
1452 __module_get(newsock->ops->owner);
1454 newfd = sock_alloc_fd(&newfile);
1455 if (unlikely(newfd < 0)) {
1457 sock_release(newsock);
1461 err = sock_attach_fd(newsock, newfile);
1465 err = security_socket_accept(sock, newsock);
1469 err = sock->ops->accept(sock, newsock, sock->file->f_flags);
1473 if (upeer_sockaddr) {
1474 if(newsock->ops->getname(newsock, (struct sockaddr *)address, &len, 2)<0) {
1475 err = -ECONNABORTED;
1478 err = move_addr_to_user(address, len, upeer_sockaddr, upeer_addrlen);
1483 /* File flags are not inherited via accept() unlike another OSes. */
1485 fd_install(newfd, newfile);
1488 security_socket_post_accept(sock, newsock);
1491 fput_light(sock->file, fput_needed);
1496 put_unused_fd(newfd);
1502 * Attempt to connect to a socket with the server address. The address
1503 * is in user space so we verify it is OK and move it to kernel space.
1505 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
1508 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
1509 * other SEQPACKET protocols that take time to connect() as it doesn't
1510 * include the -EINPROGRESS status for such sockets.
1513 asmlinkage long sys_connect(int fd, struct sockaddr __user *uservaddr, int addrlen)
1515 struct socket *sock;
1516 char address[MAX_SOCK_ADDR];
1517 int err, fput_needed;
1519 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1522 err = move_addr_to_kernel(uservaddr, addrlen, address);
1526 err = security_socket_connect(sock, (struct sockaddr *)address, addrlen);
1530 err = sock->ops->connect(sock, (struct sockaddr *) address, addrlen,
1531 sock->file->f_flags);
1533 fput_light(sock->file, fput_needed);
1539 * Get the local address ('name') of a socket object. Move the obtained
1540 * name to user space.
1543 asmlinkage long sys_getsockname(int fd, struct sockaddr __user *usockaddr, int __user *usockaddr_len)
1545 struct socket *sock;
1546 char address[MAX_SOCK_ADDR];
1547 int len, err, fput_needed;
1549 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1553 err = security_socket_getsockname(sock);
1557 err = sock->ops->getname(sock, (struct sockaddr *)address, &len, 0);
1560 err = move_addr_to_user(address, len, usockaddr, usockaddr_len);
1563 fput_light(sock->file, fput_needed);
1569 * Get the remote address ('name') of a socket object. Move the obtained
1570 * name to user space.
1573 asmlinkage long sys_getpeername(int fd, struct sockaddr __user *usockaddr, int __user *usockaddr_len)
1575 struct socket *sock;
1576 char address[MAX_SOCK_ADDR];
1577 int len, err, fput_needed;
1579 if ((sock = sockfd_lookup_light(fd, &err, &fput_needed)) != NULL) {
1580 err = security_socket_getpeername(sock);
1582 fput_light(sock->file, fput_needed);
1586 err = sock->ops->getname(sock, (struct sockaddr *)address, &len, 1);
1588 err=move_addr_to_user(address,len, usockaddr, usockaddr_len);
1589 fput_light(sock->file, fput_needed);
1595 * Send a datagram to a given address. We move the address into kernel
1596 * space and check the user space data area is readable before invoking
1600 asmlinkage long sys_sendto(int fd, void __user * buff, size_t len, unsigned flags,
1601 struct sockaddr __user *addr, int addr_len)
1603 struct socket *sock;
1604 char address[MAX_SOCK_ADDR];
1609 struct file *sock_file;
1611 sock_file = fget_light(fd, &fput_needed);
1615 sock = sock_from_file(sock_file, &err);
1623 msg.msg_control=NULL;
1624 msg.msg_controllen=0;
1627 err = move_addr_to_kernel(addr, addr_len, address);
1630 msg.msg_name=address;
1631 msg.msg_namelen=addr_len;
1633 if (sock->file->f_flags & O_NONBLOCK)
1634 flags |= MSG_DONTWAIT;
1635 msg.msg_flags = flags;
1636 err = sock_sendmsg(sock, &msg, len);
1639 fput_light(sock_file, fput_needed);
1644 * Send a datagram down a socket.
1647 asmlinkage long sys_send(int fd, void __user * buff, size_t len, unsigned flags)
1649 return sys_sendto(fd, buff, len, flags, NULL, 0);
1653 * Receive a frame from the socket and optionally record the address of the
1654 * sender. We verify the buffers are writable and if needed move the
1655 * sender address from kernel to user space.
1658 asmlinkage long sys_recvfrom(int fd, void __user * ubuf, size_t size, unsigned flags,
1659 struct sockaddr __user *addr, int __user *addr_len)
1661 struct socket *sock;
1664 char address[MAX_SOCK_ADDR];
1666 struct file *sock_file;
1669 sock_file = fget_light(fd, &fput_needed);
1673 sock = sock_from_file(sock_file, &err);
1677 msg.msg_control=NULL;
1678 msg.msg_controllen=0;
1683 msg.msg_name=address;
1684 msg.msg_namelen=MAX_SOCK_ADDR;
1685 if (sock->file->f_flags & O_NONBLOCK)
1686 flags |= MSG_DONTWAIT;
1687 err=sock_recvmsg(sock, &msg, size, flags);
1689 if(err >= 0 && addr != NULL)
1691 err2=move_addr_to_user(address, msg.msg_namelen, addr, addr_len);
1696 fput_light(sock_file, fput_needed);
1701 * Receive a datagram from a socket.
1704 asmlinkage long sys_recv(int fd, void __user * ubuf, size_t size, unsigned flags)
1706 return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
1710 * Set a socket option. Because we don't know the option lengths we have
1711 * to pass the user mode parameter for the protocols to sort out.
1714 asmlinkage long sys_setsockopt(int fd, int level, int optname, char __user *optval, int optlen)
1716 int err, fput_needed;
1717 struct socket *sock;
1722 if ((sock = sockfd_lookup_light(fd, &err, &fput_needed)) != NULL)
1724 err = security_socket_setsockopt(sock,level,optname);
1728 if (level == SOL_SOCKET)
1729 err=sock_setsockopt(sock,level,optname,optval,optlen);
1731 err=sock->ops->setsockopt(sock, level, optname, optval, optlen);
1733 fput_light(sock->file, fput_needed);
1739 * Get a socket option. Because we don't know the option lengths we have
1740 * to pass a user mode parameter for the protocols to sort out.
1743 asmlinkage long sys_getsockopt(int fd, int level, int optname, char __user *optval, int __user *optlen)
1745 int err, fput_needed;
1746 struct socket *sock;
1748 if ((sock = sockfd_lookup_light(fd, &err, &fput_needed)) != NULL) {
1749 err = security_socket_getsockopt(sock, level, optname);
1753 if (level == SOL_SOCKET)
1754 err=sock_getsockopt(sock,level,optname,optval,optlen);
1756 err=sock->ops->getsockopt(sock, level, optname, optval, optlen);
1758 fput_light(sock->file, fput_needed);
1765 * Shutdown a socket.
1768 asmlinkage long sys_shutdown(int fd, int how)
1770 int err, fput_needed;
1771 struct socket *sock;
1773 if ((sock = sockfd_lookup_light(fd, &err, &fput_needed))!=NULL)
1775 err = security_socket_shutdown(sock, how);
1777 err = sock->ops->shutdown(sock, how);
1778 fput_light(sock->file, fput_needed);
1783 /* A couple of helpful macros for getting the address of the 32/64 bit
1784 * fields which are the same type (int / unsigned) on our platforms.
1786 #define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
1787 #define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
1788 #define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
1792 * BSD sendmsg interface
1795 asmlinkage long sys_sendmsg(int fd, struct msghdr __user *msg, unsigned flags)
1797 struct compat_msghdr __user *msg_compat = (struct compat_msghdr __user *)msg;
1798 struct socket *sock;
1799 char address[MAX_SOCK_ADDR];
1800 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
1801 unsigned char ctl[sizeof(struct cmsghdr) + 20]
1802 __attribute__ ((aligned (sizeof(__kernel_size_t))));
1803 /* 20 is size of ipv6_pktinfo */
1804 unsigned char *ctl_buf = ctl;
1805 struct msghdr msg_sys;
1806 int err, ctl_len, iov_size, total_len;
1810 if (MSG_CMSG_COMPAT & flags) {
1811 if (get_compat_msghdr(&msg_sys, msg_compat))
1813 } else if (copy_from_user(&msg_sys, msg, sizeof(struct msghdr)))
1816 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1820 /* do not move before msg_sys is valid */
1822 if (msg_sys.msg_iovlen > UIO_MAXIOV)
1825 /* Check whether to allocate the iovec area*/
1827 iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
1828 if (msg_sys.msg_iovlen > UIO_FASTIOV) {
1829 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
1834 /* This will also move the address data into kernel space */
1835 if (MSG_CMSG_COMPAT & flags) {
1836 err = verify_compat_iovec(&msg_sys, iov, address, VERIFY_READ);
1838 err = verify_iovec(&msg_sys, iov, address, VERIFY_READ);
1845 if (msg_sys.msg_controllen > INT_MAX)
1847 ctl_len = msg_sys.msg_controllen;
1848 if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
1849 err = cmsghdr_from_user_compat_to_kern(&msg_sys, sock->sk, ctl, sizeof(ctl));
1852 ctl_buf = msg_sys.msg_control;
1853 ctl_len = msg_sys.msg_controllen;
1854 } else if (ctl_len) {
1855 if (ctl_len > sizeof(ctl))
1857 ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
1858 if (ctl_buf == NULL)
1863 * Careful! Before this, msg_sys.msg_control contains a user pointer.
1864 * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
1865 * checking falls down on this.
1867 if (copy_from_user(ctl_buf, (void __user *) msg_sys.msg_control, ctl_len))
1869 msg_sys.msg_control = ctl_buf;
1871 msg_sys.msg_flags = flags;
1873 if (sock->file->f_flags & O_NONBLOCK)
1874 msg_sys.msg_flags |= MSG_DONTWAIT;
1875 err = sock_sendmsg(sock, &msg_sys, total_len);
1879 sock_kfree_s(sock->sk, ctl_buf, ctl_len);
1881 if (iov != iovstack)
1882 sock_kfree_s(sock->sk, iov, iov_size);
1884 fput_light(sock->file, fput_needed);
1890 * BSD recvmsg interface
1893 asmlinkage long sys_recvmsg(int fd, struct msghdr __user *msg, unsigned int flags)
1895 struct compat_msghdr __user *msg_compat = (struct compat_msghdr __user *)msg;
1896 struct socket *sock;
1897 struct iovec iovstack[UIO_FASTIOV];
1898 struct iovec *iov=iovstack;
1899 struct msghdr msg_sys;
1900 unsigned long cmsg_ptr;
1901 int err, iov_size, total_len, len;
1904 /* kernel mode address */
1905 char addr[MAX_SOCK_ADDR];
1907 /* user mode address pointers */
1908 struct sockaddr __user *uaddr;
1909 int __user *uaddr_len;
1911 if (MSG_CMSG_COMPAT & flags) {
1912 if (get_compat_msghdr(&msg_sys, msg_compat))
1915 if (copy_from_user(&msg_sys,msg,sizeof(struct msghdr)))
1918 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1923 if (msg_sys.msg_iovlen > UIO_MAXIOV)
1926 /* Check whether to allocate the iovec area*/
1928 iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
1929 if (msg_sys.msg_iovlen > UIO_FASTIOV) {
1930 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
1936 * Save the user-mode address (verify_iovec will change the
1937 * kernel msghdr to use the kernel address space)
1940 uaddr = (void __user *) msg_sys.msg_name;
1941 uaddr_len = COMPAT_NAMELEN(msg);
1942 if (MSG_CMSG_COMPAT & flags) {
1943 err = verify_compat_iovec(&msg_sys, iov, addr, VERIFY_WRITE);
1945 err = verify_iovec(&msg_sys, iov, addr, VERIFY_WRITE);
1950 cmsg_ptr = (unsigned long)msg_sys.msg_control;
1951 msg_sys.msg_flags = 0;
1952 if (MSG_CMSG_COMPAT & flags)
1953 msg_sys.msg_flags = MSG_CMSG_COMPAT;
1955 if (sock->file->f_flags & O_NONBLOCK)
1956 flags |= MSG_DONTWAIT;
1957 err = sock_recvmsg(sock, &msg_sys, total_len, flags);
1962 if (uaddr != NULL) {
1963 err = move_addr_to_user(addr, msg_sys.msg_namelen, uaddr, uaddr_len);
1967 err = __put_user((msg_sys.msg_flags & ~MSG_CMSG_COMPAT),
1971 if (MSG_CMSG_COMPAT & flags)
1972 err = __put_user((unsigned long)msg_sys.msg_control-cmsg_ptr,
1973 &msg_compat->msg_controllen);
1975 err = __put_user((unsigned long)msg_sys.msg_control-cmsg_ptr,
1976 &msg->msg_controllen);
1982 if (iov != iovstack)
1983 sock_kfree_s(sock->sk, iov, iov_size);
1985 fput_light(sock->file, fput_needed);
1990 #ifdef __ARCH_WANT_SYS_SOCKETCALL
1992 /* Argument list sizes for sys_socketcall */
1993 #define AL(x) ((x) * sizeof(unsigned long))
1994 static unsigned char nargs[18]={AL(0),AL(3),AL(3),AL(3),AL(2),AL(3),
1995 AL(3),AL(3),AL(4),AL(4),AL(4),AL(6),
1996 AL(6),AL(2),AL(5),AL(5),AL(3),AL(3)};
2000 * System call vectors.
2002 * Argument checking cleaned up. Saved 20% in size.
2003 * This function doesn't need to set the kernel lock because
2004 * it is set by the callees.
2007 asmlinkage long sys_socketcall(int call, unsigned long __user *args)
2010 unsigned long a0,a1;
2013 if(call<1||call>SYS_RECVMSG)
2016 /* copy_from_user should be SMP safe. */
2017 if (copy_from_user(a, args, nargs[call]))
2020 err = audit_socketcall(nargs[call]/sizeof(unsigned long), a);
2030 err = sys_socket(a0,a1,a[2]);
2033 err = sys_bind(a0,(struct sockaddr __user *)a1, a[2]);
2036 err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
2039 err = sys_listen(a0,a1);
2042 err = sys_accept(a0,(struct sockaddr __user *)a1, (int __user *)a[2]);
2044 case SYS_GETSOCKNAME:
2045 err = sys_getsockname(a0,(struct sockaddr __user *)a1, (int __user *)a[2]);
2047 case SYS_GETPEERNAME:
2048 err = sys_getpeername(a0, (struct sockaddr __user *)a1, (int __user *)a[2]);
2050 case SYS_SOCKETPAIR:
2051 err = sys_socketpair(a0,a1, a[2], (int __user *)a[3]);
2054 err = sys_send(a0, (void __user *)a1, a[2], a[3]);
2057 err = sys_sendto(a0,(void __user *)a1, a[2], a[3],
2058 (struct sockaddr __user *)a[4], a[5]);
2061 err = sys_recv(a0, (void __user *)a1, a[2], a[3]);
2064 err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
2065 (struct sockaddr __user *)a[4], (int __user *)a[5]);
2068 err = sys_shutdown(a0,a1);
2070 case SYS_SETSOCKOPT:
2071 err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]);
2073 case SYS_GETSOCKOPT:
2074 err = sys_getsockopt(a0, a1, a[2], (char __user *)a[3], (int __user *)a[4]);
2077 err = sys_sendmsg(a0, (struct msghdr __user *) a1, a[2]);
2080 err = sys_recvmsg(a0, (struct msghdr __user *) a1, a[2]);
2089 #endif /* __ARCH_WANT_SYS_SOCKETCALL */
2092 * This function is called by a protocol handler that wants to
2093 * advertise its address family, and have it linked into the
2097 int sock_register(struct net_proto_family *ops)
2101 if (ops->family >= NPROTO) {
2102 printk(KERN_CRIT "protocol %d >= NPROTO(%d)\n", ops->family, NPROTO);
2105 net_family_write_lock();
2107 if (net_families[ops->family] == NULL) {
2108 net_families[ops->family]=ops;
2111 net_family_write_unlock();
2112 printk(KERN_INFO "NET: Registered protocol family %d\n",
2118 * This function is called by a protocol handler that wants to
2119 * remove its address family, and have it unlinked from the
2123 int sock_unregister(int family)
2125 if (family < 0 || family >= NPROTO)
2128 net_family_write_lock();
2129 net_families[family]=NULL;
2130 net_family_write_unlock();
2131 printk(KERN_INFO "NET: Unregistered protocol family %d\n",
2136 static int __init sock_init(void)
2139 * Initialize sock SLAB cache.
2145 * Initialize skbuff SLAB cache
2150 * Initialize the protocols module.
2154 register_filesystem(&sock_fs_type);
2155 sock_mnt = kern_mount(&sock_fs_type);
2157 /* The real protocol initialization is performed in later initcalls.
2160 #ifdef CONFIG_NETFILTER
2167 core_initcall(sock_init); /* early initcall */
2172 struct module *tux_module = NULL;
2174 #ifdef CONFIG_TUX_MODULE
2176 asmlinkage long (*sys_tux_ptr) (unsigned int action, user_req_t *u_info) = NULL;
2177 spinlock_t tux_module_lock = SPIN_LOCK_UNLOCKED;
2179 asmlinkage long sys_tux (unsigned int action, user_req_t *u_info)
2183 if (current->tux_info)
2184 return sys_tux_ptr(action, u_info);
2187 spin_lock(&tux_module_lock);
2190 if (!try_module_get(tux_module))
2192 spin_unlock(&tux_module_lock);
2196 ret = sys_tux_ptr(action, u_info);
2198 spin_lock(&tux_module_lock);
2199 module_put(tux_module);
2201 spin_unlock(&tux_module_lock);
2206 EXPORT_SYMBOL_GPL(tux_module);
2207 EXPORT_SYMBOL_GPL(tux_module_lock);
2208 EXPORT_SYMBOL_GPL(sys_tux_ptr);
2210 EXPORT_SYMBOL_GPL(tux_Dprintk);
2211 EXPORT_SYMBOL_GPL(tux_TDprintk);
2214 #ifdef CONFIG_PROC_FS
2215 void socket_seq_show(struct seq_file *seq)
2220 for_each_possible_cpu(cpu)
2221 counter += per_cpu(sockets_in_use, cpu);
2223 /* It can be negative, by the way. 8) */
2227 seq_printf(seq, "sockets: used %d\n", counter);
2229 #endif /* CONFIG_PROC_FS */
2231 #ifdef CONFIG_COMPAT
2232 static long compat_sock_ioctl(struct file *file, unsigned cmd,
2235 struct socket *sock = file->private_data;
2236 int ret = -ENOIOCTLCMD;
2238 if (sock->ops->compat_ioctl)
2239 ret = sock->ops->compat_ioctl(sock, cmd, arg);
2245 /* ABI emulation layers need these two */
2246 EXPORT_SYMBOL(move_addr_to_kernel);
2247 EXPORT_SYMBOL(move_addr_to_user);
2248 EXPORT_SYMBOL(sock_create);
2249 EXPORT_SYMBOL(sock_create_kern);
2250 EXPORT_SYMBOL(sock_create_lite);
2251 EXPORT_SYMBOL(sock_map_fd);
2252 EXPORT_SYMBOL(sock_recvmsg);
2253 EXPORT_SYMBOL(sock_register);
2254 EXPORT_SYMBOL(sock_release);
2255 EXPORT_SYMBOL(sock_sendmsg);
2256 EXPORT_SYMBOL(sock_unregister);
2257 EXPORT_SYMBOL(sock_wake_async);
2258 EXPORT_SYMBOL(sockfd_lookup);
2259 EXPORT_SYMBOL(kernel_sendmsg);
2260 EXPORT_SYMBOL(kernel_recvmsg);