[NET]: socket family using RCU
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / socket.c
blobb5a3fcb9ed6dab6b6509ae9d7ccd4f5b442dabe1
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
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.
58 * Based upon Swansea University Computer Society NET3.039
61 #include <linux/mm.h>
62 #include <linux/socket.h>
63 #include <linux/file.h>
64 #include <linux/net.h>
65 #include <linux/interrupt.h>
66 #include <linux/rcupdate.h>
67 #include <linux/netdevice.h>
68 #include <linux/proc_fs.h>
69 #include <linux/seq_file.h>
70 #include <linux/mutex.h>
71 #include <linux/wanrouter.h>
72 #include <linux/if_bridge.h>
73 #include <linux/if_frad.h>
74 #include <linux/if_vlan.h>
75 #include <linux/init.h>
76 #include <linux/poll.h>
77 #include <linux/cache.h>
78 #include <linux/module.h>
79 #include <linux/highmem.h>
80 #include <linux/divert.h>
81 #include <linux/mount.h>
82 #include <linux/security.h>
83 #include <linux/syscalls.h>
84 #include <linux/compat.h>
85 #include <linux/kmod.h>
86 #include <linux/audit.h>
87 #include <linux/wireless.h>
89 #include <asm/uaccess.h>
90 #include <asm/unistd.h>
92 #include <net/compat.h>
94 #include <net/sock.h>
95 #include <linux/netfilter.h>
97 static int sock_no_open(struct inode *irrelevant, struct file *dontcare);
98 static ssize_t sock_aio_read(struct kiocb *iocb, char __user *buf,
99 size_t size, loff_t pos);
100 static ssize_t sock_aio_write(struct kiocb *iocb, const char __user *buf,
101 size_t size, loff_t pos);
102 static int sock_mmap(struct file *file, struct vm_area_struct *vma);
104 static int sock_close(struct inode *inode, struct file *file);
105 static unsigned int sock_poll(struct file *file,
106 struct poll_table_struct *wait);
107 static long sock_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
108 #ifdef CONFIG_COMPAT
109 static long compat_sock_ioctl(struct file *file,
110 unsigned int cmd, unsigned long arg);
111 #endif
112 static int sock_fasync(int fd, struct file *filp, int on);
113 static ssize_t sock_readv(struct file *file, const struct iovec *vector,
114 unsigned long count, loff_t *ppos);
115 static ssize_t sock_writev(struct file *file, const struct iovec *vector,
116 unsigned long count, loff_t *ppos);
117 static ssize_t sock_sendpage(struct file *file, struct page *page,
118 int offset, size_t size, loff_t *ppos, int more);
121 * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
122 * in the operation structures but are done directly via the socketcall() multiplexor.
125 static struct file_operations socket_file_ops = {
126 .owner = THIS_MODULE,
127 .llseek = no_llseek,
128 .aio_read = sock_aio_read,
129 .aio_write = sock_aio_write,
130 .poll = sock_poll,
131 .unlocked_ioctl = sock_ioctl,
132 #ifdef CONFIG_COMPAT
133 .compat_ioctl = compat_sock_ioctl,
134 #endif
135 .mmap = sock_mmap,
136 .open = sock_no_open, /* special open code to disallow open via /proc */
137 .release = sock_close,
138 .fasync = sock_fasync,
139 .readv = sock_readv,
140 .writev = sock_writev,
141 .sendpage = sock_sendpage,
142 .splice_write = generic_splice_sendpage,
146 * The protocol list. Each protocol is registered in here.
149 static DEFINE_SPINLOCK(net_family_lock);
150 static const struct net_proto_family *net_families[NPROTO];
153 * Statistics counters of the socket lists
156 static DEFINE_PER_CPU(int, sockets_in_use) = 0;
159 * Support routines.
160 * Move socket addresses back and forth across the kernel/user
161 * divide and look after the messy bits.
164 #define MAX_SOCK_ADDR 128 /* 108 for Unix domain -
165 16 for IP, 16 for IPX,
166 24 for IPv6,
167 about 80 for AX.25
168 must be at least one bigger than
169 the AF_UNIX size (see net/unix/af_unix.c
170 :unix_mkname()).
174 * move_addr_to_kernel - copy a socket address into kernel space
175 * @uaddr: Address in user space
176 * @kaddr: Address in kernel space
177 * @ulen: Length in user space
179 * The address is copied into kernel space. If the provided address is
180 * too long an error code of -EINVAL is returned. If the copy gives
181 * invalid addresses -EFAULT is returned. On a success 0 is returned.
184 int move_addr_to_kernel(void __user *uaddr, int ulen, void *kaddr)
186 if (ulen < 0 || ulen > MAX_SOCK_ADDR)
187 return -EINVAL;
188 if (ulen == 0)
189 return 0;
190 if (copy_from_user(kaddr, uaddr, ulen))
191 return -EFAULT;
192 return audit_sockaddr(ulen, kaddr);
196 * move_addr_to_user - copy an address to user space
197 * @kaddr: kernel space address
198 * @klen: length of address in kernel
199 * @uaddr: user space address
200 * @ulen: pointer to user length field
202 * The value pointed to by ulen on entry is the buffer length available.
203 * This is overwritten with the buffer space used. -EINVAL is returned
204 * if an overlong buffer is specified or a negative buffer size. -EFAULT
205 * is returned if either the buffer or the length field are not
206 * accessible.
207 * After copying the data up to the limit the user specifies, the true
208 * length of the data is written over the length limit the user
209 * specified. Zero is returned for a success.
212 int move_addr_to_user(void *kaddr, int klen, void __user *uaddr,
213 int __user *ulen)
215 int err;
216 int len;
218 err = get_user(len, ulen);
219 if (err)
220 return err;
221 if (len > klen)
222 len = klen;
223 if (len < 0 || len > MAX_SOCK_ADDR)
224 return -EINVAL;
225 if (len) {
226 if (audit_sockaddr(klen, kaddr))
227 return -ENOMEM;
228 if (copy_to_user(uaddr, kaddr, len))
229 return -EFAULT;
232 * "fromlen shall refer to the value before truncation.."
233 * 1003.1g
235 return __put_user(klen, ulen);
238 #define SOCKFS_MAGIC 0x534F434B
240 static kmem_cache_t *sock_inode_cachep __read_mostly;
242 static struct inode *sock_alloc_inode(struct super_block *sb)
244 struct socket_alloc *ei;
246 ei = kmem_cache_alloc(sock_inode_cachep, SLAB_KERNEL);
247 if (!ei)
248 return NULL;
249 init_waitqueue_head(&ei->socket.wait);
251 ei->socket.fasync_list = NULL;
252 ei->socket.state = SS_UNCONNECTED;
253 ei->socket.flags = 0;
254 ei->socket.ops = NULL;
255 ei->socket.sk = NULL;
256 ei->socket.file = NULL;
257 ei->socket.flags = 0;
259 return &ei->vfs_inode;
262 static void sock_destroy_inode(struct inode *inode)
264 kmem_cache_free(sock_inode_cachep,
265 container_of(inode, struct socket_alloc, vfs_inode));
268 static void init_once(void *foo, kmem_cache_t *cachep, unsigned long flags)
270 struct socket_alloc *ei = (struct socket_alloc *)foo;
272 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR))
273 == SLAB_CTOR_CONSTRUCTOR)
274 inode_init_once(&ei->vfs_inode);
277 static int init_inodecache(void)
279 sock_inode_cachep = kmem_cache_create("sock_inode_cache",
280 sizeof(struct socket_alloc),
282 (SLAB_HWCACHE_ALIGN |
283 SLAB_RECLAIM_ACCOUNT |
284 SLAB_MEM_SPREAD),
285 init_once,
286 NULL);
287 if (sock_inode_cachep == NULL)
288 return -ENOMEM;
289 return 0;
292 static struct super_operations sockfs_ops = {
293 .alloc_inode = sock_alloc_inode,
294 .destroy_inode =sock_destroy_inode,
295 .statfs = simple_statfs,
298 static int sockfs_get_sb(struct file_system_type *fs_type,
299 int flags, const char *dev_name, void *data,
300 struct vfsmount *mnt)
302 return get_sb_pseudo(fs_type, "socket:", &sockfs_ops, SOCKFS_MAGIC,
303 mnt);
306 static struct vfsmount *sock_mnt __read_mostly;
308 static struct file_system_type sock_fs_type = {
309 .name = "sockfs",
310 .get_sb = sockfs_get_sb,
311 .kill_sb = kill_anon_super,
314 static int sockfs_delete_dentry(struct dentry *dentry)
316 return 1;
318 static struct dentry_operations sockfs_dentry_operations = {
319 .d_delete = sockfs_delete_dentry,
323 * Obtains the first available file descriptor and sets it up for use.
325 * These functions create file structures and maps them to fd space
326 * of the current process. On success it returns file descriptor
327 * and file struct implicitly stored in sock->file.
328 * Note that another thread may close file descriptor before we return
329 * from this function. We use the fact that now we do not refer
330 * to socket after mapping. If one day we will need it, this
331 * function will increment ref. count on file by 1.
333 * In any case returned fd MAY BE not valid!
334 * This race condition is unavoidable
335 * with shared fd spaces, we cannot solve it inside kernel,
336 * but we take care of internal coherence yet.
339 static int sock_alloc_fd(struct file **filep)
341 int fd;
343 fd = get_unused_fd();
344 if (likely(fd >= 0)) {
345 struct file *file = get_empty_filp();
347 *filep = file;
348 if (unlikely(!file)) {
349 put_unused_fd(fd);
350 return -ENFILE;
352 } else
353 *filep = NULL;
354 return fd;
357 static int sock_attach_fd(struct socket *sock, struct file *file)
359 struct qstr this;
360 char name[32];
362 this.len = sprintf(name, "[%lu]", SOCK_INODE(sock)->i_ino);
363 this.name = name;
364 this.hash = SOCK_INODE(sock)->i_ino;
366 file->f_dentry = d_alloc(sock_mnt->mnt_sb->s_root, &this);
367 if (unlikely(!file->f_dentry))
368 return -ENOMEM;
370 file->f_dentry->d_op = &sockfs_dentry_operations;
371 d_add(file->f_dentry, SOCK_INODE(sock));
372 file->f_vfsmnt = mntget(sock_mnt);
373 file->f_mapping = file->f_dentry->d_inode->i_mapping;
375 sock->file = file;
376 file->f_op = SOCK_INODE(sock)->i_fop = &socket_file_ops;
377 file->f_mode = FMODE_READ | FMODE_WRITE;
378 file->f_flags = O_RDWR;
379 file->f_pos = 0;
380 file->private_data = sock;
382 return 0;
385 int sock_map_fd(struct socket *sock)
387 struct file *newfile;
388 int fd = sock_alloc_fd(&newfile);
390 if (likely(fd >= 0)) {
391 int err = sock_attach_fd(sock, newfile);
393 if (unlikely(err < 0)) {
394 put_filp(newfile);
395 put_unused_fd(fd);
396 return err;
398 fd_install(fd, newfile);
400 return fd;
403 static struct socket *sock_from_file(struct file *file, int *err)
405 struct inode *inode;
406 struct socket *sock;
408 if (file->f_op == &socket_file_ops)
409 return file->private_data; /* set in sock_map_fd */
411 inode = file->f_dentry->d_inode;
412 if (!S_ISSOCK(inode->i_mode)) {
413 *err = -ENOTSOCK;
414 return NULL;
417 sock = SOCKET_I(inode);
418 if (sock->file != file) {
419 printk(KERN_ERR "socki_lookup: socket file changed!\n");
420 sock->file = file;
422 return sock;
426 * sockfd_lookup - Go from a file number to its socket slot
427 * @fd: file handle
428 * @err: pointer to an error code return
430 * The file handle passed in is locked and the socket it is bound
431 * too is returned. If an error occurs the err pointer is overwritten
432 * with a negative errno code and NULL is returned. The function checks
433 * for both invalid handles and passing a handle which is not a socket.
435 * On a success the socket object pointer is returned.
438 struct socket *sockfd_lookup(int fd, int *err)
440 struct file *file;
441 struct socket *sock;
443 file = fget(fd);
444 if (!file) {
445 *err = -EBADF;
446 return NULL;
449 sock = sock_from_file(file, err);
450 if (!sock)
451 fput(file);
452 return sock;
455 static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed)
457 struct file *file;
458 struct socket *sock;
460 *err = -EBADF;
461 file = fget_light(fd, fput_needed);
462 if (file) {
463 sock = sock_from_file(file, err);
464 if (sock)
465 return sock;
466 fput_light(file, *fput_needed);
468 return NULL;
472 * sock_alloc - allocate a socket
474 * Allocate a new inode and socket object. The two are bound together
475 * and initialised. The socket is then returned. If we are out of inodes
476 * NULL is returned.
479 static struct socket *sock_alloc(void)
481 struct inode *inode;
482 struct socket *sock;
484 inode = new_inode(sock_mnt->mnt_sb);
485 if (!inode)
486 return NULL;
488 sock = SOCKET_I(inode);
490 inode->i_mode = S_IFSOCK | S_IRWXUGO;
491 inode->i_uid = current->fsuid;
492 inode->i_gid = current->fsgid;
494 get_cpu_var(sockets_in_use)++;
495 put_cpu_var(sockets_in_use);
496 return sock;
500 * In theory you can't get an open on this inode, but /proc provides
501 * a back door. Remember to keep it shut otherwise you'll let the
502 * creepy crawlies in.
505 static int sock_no_open(struct inode *irrelevant, struct file *dontcare)
507 return -ENXIO;
510 const struct file_operations bad_sock_fops = {
511 .owner = THIS_MODULE,
512 .open = sock_no_open,
516 * sock_release - close a socket
517 * @sock: socket to close
519 * The socket is released from the protocol stack if it has a release
520 * callback, and the inode is then released if the socket is bound to
521 * an inode not a file.
524 void sock_release(struct socket *sock)
526 if (sock->ops) {
527 struct module *owner = sock->ops->owner;
529 sock->ops->release(sock);
530 sock->ops = NULL;
531 module_put(owner);
534 if (sock->fasync_list)
535 printk(KERN_ERR "sock_release: fasync list not empty!\n");
537 get_cpu_var(sockets_in_use)--;
538 put_cpu_var(sockets_in_use);
539 if (!sock->file) {
540 iput(SOCK_INODE(sock));
541 return;
543 sock->file = NULL;
546 static inline int __sock_sendmsg(struct kiocb *iocb, struct socket *sock,
547 struct msghdr *msg, size_t size)
549 struct sock_iocb *si = kiocb_to_siocb(iocb);
550 int err;
552 si->sock = sock;
553 si->scm = NULL;
554 si->msg = msg;
555 si->size = size;
557 err = security_socket_sendmsg(sock, msg, size);
558 if (err)
559 return err;
561 return sock->ops->sendmsg(iocb, sock, msg, size);
564 int sock_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
566 struct kiocb iocb;
567 struct sock_iocb siocb;
568 int ret;
570 init_sync_kiocb(&iocb, NULL);
571 iocb.private = &siocb;
572 ret = __sock_sendmsg(&iocb, sock, msg, size);
573 if (-EIOCBQUEUED == ret)
574 ret = wait_on_sync_kiocb(&iocb);
575 return ret;
578 int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
579 struct kvec *vec, size_t num, size_t size)
581 mm_segment_t oldfs = get_fs();
582 int result;
584 set_fs(KERNEL_DS);
586 * the following is safe, since for compiler definitions of kvec and
587 * iovec are identical, yielding the same in-core layout and alignment
589 msg->msg_iov = (struct iovec *)vec;
590 msg->msg_iovlen = num;
591 result = sock_sendmsg(sock, msg, size);
592 set_fs(oldfs);
593 return result;
596 static inline int __sock_recvmsg(struct kiocb *iocb, struct socket *sock,
597 struct msghdr *msg, size_t size, int flags)
599 int err;
600 struct sock_iocb *si = kiocb_to_siocb(iocb);
602 si->sock = sock;
603 si->scm = NULL;
604 si->msg = msg;
605 si->size = size;
606 si->flags = flags;
608 err = security_socket_recvmsg(sock, msg, size, flags);
609 if (err)
610 return err;
612 return sock->ops->recvmsg(iocb, sock, msg, size, flags);
615 int sock_recvmsg(struct socket *sock, struct msghdr *msg,
616 size_t size, int flags)
618 struct kiocb iocb;
619 struct sock_iocb siocb;
620 int ret;
622 init_sync_kiocb(&iocb, NULL);
623 iocb.private = &siocb;
624 ret = __sock_recvmsg(&iocb, sock, msg, size, flags);
625 if (-EIOCBQUEUED == ret)
626 ret = wait_on_sync_kiocb(&iocb);
627 return ret;
630 int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
631 struct kvec *vec, size_t num, size_t size, int flags)
633 mm_segment_t oldfs = get_fs();
634 int result;
636 set_fs(KERNEL_DS);
638 * the following is safe, since for compiler definitions of kvec and
639 * iovec are identical, yielding the same in-core layout and alignment
641 msg->msg_iov = (struct iovec *)vec, msg->msg_iovlen = num;
642 result = sock_recvmsg(sock, msg, size, flags);
643 set_fs(oldfs);
644 return result;
647 static void sock_aio_dtor(struct kiocb *iocb)
649 kfree(iocb->private);
652 static ssize_t sock_sendpage(struct file *file, struct page *page,
653 int offset, size_t size, loff_t *ppos, int more)
655 struct socket *sock;
656 int flags;
658 sock = file->private_data;
660 flags = !(file->f_flags & O_NONBLOCK) ? 0 : MSG_DONTWAIT;
661 if (more)
662 flags |= MSG_MORE;
664 return sock->ops->sendpage(sock, page, offset, size, flags);
667 static struct sock_iocb *alloc_sock_iocb(struct kiocb *iocb,
668 char __user *ubuf, size_t size,
669 struct sock_iocb *siocb)
671 if (!is_sync_kiocb(iocb)) {
672 siocb = kmalloc(sizeof(*siocb), GFP_KERNEL);
673 if (!siocb)
674 return NULL;
675 iocb->ki_dtor = sock_aio_dtor;
678 siocb->kiocb = iocb;
679 siocb->async_iov.iov_base = ubuf;
680 siocb->async_iov.iov_len = size;
682 iocb->private = siocb;
683 return siocb;
686 static ssize_t do_sock_read(struct msghdr *msg, struct kiocb *iocb,
687 struct file *file, struct iovec *iov,
688 unsigned long nr_segs)
690 struct socket *sock = file->private_data;
691 size_t size = 0;
692 int i;
694 for (i = 0; i < nr_segs; i++)
695 size += iov[i].iov_len;
697 msg->msg_name = NULL;
698 msg->msg_namelen = 0;
699 msg->msg_control = NULL;
700 msg->msg_controllen = 0;
701 msg->msg_iov = (struct iovec *)iov;
702 msg->msg_iovlen = nr_segs;
703 msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
705 return __sock_recvmsg(iocb, sock, msg, size, msg->msg_flags);
708 static ssize_t sock_readv(struct file *file, const struct iovec *iov,
709 unsigned long nr_segs, loff_t *ppos)
711 struct kiocb iocb;
712 struct sock_iocb siocb;
713 struct msghdr msg;
714 int ret;
716 init_sync_kiocb(&iocb, NULL);
717 iocb.private = &siocb;
719 ret = do_sock_read(&msg, &iocb, file, (struct iovec *)iov, nr_segs);
720 if (-EIOCBQUEUED == ret)
721 ret = wait_on_sync_kiocb(&iocb);
722 return ret;
725 static ssize_t sock_aio_read(struct kiocb *iocb, char __user *ubuf,
726 size_t count, loff_t pos)
728 struct sock_iocb siocb, *x;
730 if (pos != 0)
731 return -ESPIPE;
732 if (count == 0) /* Match SYS5 behaviour */
733 return 0;
735 x = alloc_sock_iocb(iocb, ubuf, count, &siocb);
736 if (!x)
737 return -ENOMEM;
738 return do_sock_read(&x->async_msg, iocb, iocb->ki_filp,
739 &x->async_iov, 1);
742 static ssize_t do_sock_write(struct msghdr *msg, struct kiocb *iocb,
743 struct file *file, struct iovec *iov,
744 unsigned long nr_segs)
746 struct socket *sock = file->private_data;
747 size_t size = 0;
748 int i;
750 for (i = 0; i < nr_segs; i++)
751 size += iov[i].iov_len;
753 msg->msg_name = NULL;
754 msg->msg_namelen = 0;
755 msg->msg_control = NULL;
756 msg->msg_controllen = 0;
757 msg->msg_iov = (struct iovec *)iov;
758 msg->msg_iovlen = nr_segs;
759 msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
760 if (sock->type == SOCK_SEQPACKET)
761 msg->msg_flags |= MSG_EOR;
763 return __sock_sendmsg(iocb, sock, msg, size);
766 static ssize_t sock_writev(struct file *file, const struct iovec *iov,
767 unsigned long nr_segs, loff_t *ppos)
769 struct msghdr msg;
770 struct kiocb iocb;
771 struct sock_iocb siocb;
772 int ret;
774 init_sync_kiocb(&iocb, NULL);
775 iocb.private = &siocb;
777 ret = do_sock_write(&msg, &iocb, file, (struct iovec *)iov, nr_segs);
778 if (-EIOCBQUEUED == ret)
779 ret = wait_on_sync_kiocb(&iocb);
780 return ret;
783 static ssize_t sock_aio_write(struct kiocb *iocb, const char __user *ubuf,
784 size_t count, loff_t pos)
786 struct sock_iocb siocb, *x;
788 if (pos != 0)
789 return -ESPIPE;
790 if (count == 0) /* Match SYS5 behaviour */
791 return 0;
793 x = alloc_sock_iocb(iocb, (void __user *)ubuf, count, &siocb);
794 if (!x)
795 return -ENOMEM;
797 return do_sock_write(&x->async_msg, iocb, iocb->ki_filp,
798 &x->async_iov, 1);
802 * Atomic setting of ioctl hooks to avoid race
803 * with module unload.
806 static DEFINE_MUTEX(br_ioctl_mutex);
807 static int (*br_ioctl_hook) (unsigned int cmd, void __user *arg) = NULL;
809 void brioctl_set(int (*hook) (unsigned int, void __user *))
811 mutex_lock(&br_ioctl_mutex);
812 br_ioctl_hook = hook;
813 mutex_unlock(&br_ioctl_mutex);
816 EXPORT_SYMBOL(brioctl_set);
818 static DEFINE_MUTEX(vlan_ioctl_mutex);
819 static int (*vlan_ioctl_hook) (void __user *arg);
821 void vlan_ioctl_set(int (*hook) (void __user *))
823 mutex_lock(&vlan_ioctl_mutex);
824 vlan_ioctl_hook = hook;
825 mutex_unlock(&vlan_ioctl_mutex);
828 EXPORT_SYMBOL(vlan_ioctl_set);
830 static DEFINE_MUTEX(dlci_ioctl_mutex);
831 static int (*dlci_ioctl_hook) (unsigned int, void __user *);
833 void dlci_ioctl_set(int (*hook) (unsigned int, void __user *))
835 mutex_lock(&dlci_ioctl_mutex);
836 dlci_ioctl_hook = hook;
837 mutex_unlock(&dlci_ioctl_mutex);
840 EXPORT_SYMBOL(dlci_ioctl_set);
843 * With an ioctl, arg may well be a user mode pointer, but we don't know
844 * what to do with it - that's up to the protocol still.
847 static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
849 struct socket *sock;
850 void __user *argp = (void __user *)arg;
851 int pid, err;
853 sock = file->private_data;
854 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
855 err = dev_ioctl(cmd, argp);
856 } else
857 #ifdef CONFIG_WIRELESS_EXT
858 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
859 err = dev_ioctl(cmd, argp);
860 } else
861 #endif /* CONFIG_WIRELESS_EXT */
862 switch (cmd) {
863 case FIOSETOWN:
864 case SIOCSPGRP:
865 err = -EFAULT;
866 if (get_user(pid, (int __user *)argp))
867 break;
868 err = f_setown(sock->file, pid, 1);
869 break;
870 case FIOGETOWN:
871 case SIOCGPGRP:
872 err = put_user(sock->file->f_owner.pid,
873 (int __user *)argp);
874 break;
875 case SIOCGIFBR:
876 case SIOCSIFBR:
877 case SIOCBRADDBR:
878 case SIOCBRDELBR:
879 err = -ENOPKG;
880 if (!br_ioctl_hook)
881 request_module("bridge");
883 mutex_lock(&br_ioctl_mutex);
884 if (br_ioctl_hook)
885 err = br_ioctl_hook(cmd, argp);
886 mutex_unlock(&br_ioctl_mutex);
887 break;
888 case SIOCGIFVLAN:
889 case SIOCSIFVLAN:
890 err = -ENOPKG;
891 if (!vlan_ioctl_hook)
892 request_module("8021q");
894 mutex_lock(&vlan_ioctl_mutex);
895 if (vlan_ioctl_hook)
896 err = vlan_ioctl_hook(argp);
897 mutex_unlock(&vlan_ioctl_mutex);
898 break;
899 case SIOCGIFDIVERT:
900 case SIOCSIFDIVERT:
901 /* Convert this to call through a hook */
902 err = divert_ioctl(cmd, argp);
903 break;
904 case SIOCADDDLCI:
905 case SIOCDELDLCI:
906 err = -ENOPKG;
907 if (!dlci_ioctl_hook)
908 request_module("dlci");
910 if (dlci_ioctl_hook) {
911 mutex_lock(&dlci_ioctl_mutex);
912 err = dlci_ioctl_hook(cmd, argp);
913 mutex_unlock(&dlci_ioctl_mutex);
915 break;
916 default:
917 err = sock->ops->ioctl(sock, cmd, arg);
920 * If this ioctl is unknown try to hand it down
921 * to the NIC driver.
923 if (err == -ENOIOCTLCMD)
924 err = dev_ioctl(cmd, argp);
925 break;
927 return err;
930 int sock_create_lite(int family, int type, int protocol, struct socket **res)
932 int err;
933 struct socket *sock = NULL;
935 err = security_socket_create(family, type, protocol, 1);
936 if (err)
937 goto out;
939 sock = sock_alloc();
940 if (!sock) {
941 err = -ENOMEM;
942 goto out;
945 sock->type = type;
946 err = security_socket_post_create(sock, family, type, protocol, 1);
947 if (err)
948 goto out_release;
950 out:
951 *res = sock;
952 return err;
953 out_release:
954 sock_release(sock);
955 sock = NULL;
956 goto out;
959 /* No kernel lock held - perfect */
960 static unsigned int sock_poll(struct file *file, poll_table *wait)
962 struct socket *sock;
965 * We can't return errors to poll, so it's either yes or no.
967 sock = file->private_data;
968 return sock->ops->poll(file, sock, wait);
971 static int sock_mmap(struct file *file, struct vm_area_struct *vma)
973 struct socket *sock = file->private_data;
975 return sock->ops->mmap(file, sock, vma);
978 static int sock_close(struct inode *inode, struct file *filp)
981 * It was possible the inode is NULL we were
982 * closing an unfinished socket.
985 if (!inode) {
986 printk(KERN_DEBUG "sock_close: NULL inode\n");
987 return 0;
989 sock_fasync(-1, filp, 0);
990 sock_release(SOCKET_I(inode));
991 return 0;
995 * Update the socket async list
997 * Fasync_list locking strategy.
999 * 1. fasync_list is modified only under process context socket lock
1000 * i.e. under semaphore.
1001 * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
1002 * or under socket lock.
1003 * 3. fasync_list can be used from softirq context, so that
1004 * modification under socket lock have to be enhanced with
1005 * write_lock_bh(&sk->sk_callback_lock).
1006 * --ANK (990710)
1009 static int sock_fasync(int fd, struct file *filp, int on)
1011 struct fasync_struct *fa, *fna = NULL, **prev;
1012 struct socket *sock;
1013 struct sock *sk;
1015 if (on) {
1016 fna = kmalloc(sizeof(struct fasync_struct), GFP_KERNEL);
1017 if (fna == NULL)
1018 return -ENOMEM;
1021 sock = filp->private_data;
1023 sk = sock->sk;
1024 if (sk == NULL) {
1025 kfree(fna);
1026 return -EINVAL;
1029 lock_sock(sk);
1031 prev = &(sock->fasync_list);
1033 for (fa = *prev; fa != NULL; prev = &fa->fa_next, fa = *prev)
1034 if (fa->fa_file == filp)
1035 break;
1037 if (on) {
1038 if (fa != NULL) {
1039 write_lock_bh(&sk->sk_callback_lock);
1040 fa->fa_fd = fd;
1041 write_unlock_bh(&sk->sk_callback_lock);
1043 kfree(fna);
1044 goto out;
1046 fna->fa_file = filp;
1047 fna->fa_fd = fd;
1048 fna->magic = FASYNC_MAGIC;
1049 fna->fa_next = sock->fasync_list;
1050 write_lock_bh(&sk->sk_callback_lock);
1051 sock->fasync_list = fna;
1052 write_unlock_bh(&sk->sk_callback_lock);
1053 } else {
1054 if (fa != NULL) {
1055 write_lock_bh(&sk->sk_callback_lock);
1056 *prev = fa->fa_next;
1057 write_unlock_bh(&sk->sk_callback_lock);
1058 kfree(fa);
1062 out:
1063 release_sock(sock->sk);
1064 return 0;
1067 /* This function may be called only under socket lock or callback_lock */
1069 int sock_wake_async(struct socket *sock, int how, int band)
1071 if (!sock || !sock->fasync_list)
1072 return -1;
1073 switch (how) {
1074 case 1:
1076 if (test_bit(SOCK_ASYNC_WAITDATA, &sock->flags))
1077 break;
1078 goto call_kill;
1079 case 2:
1080 if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags))
1081 break;
1082 /* fall through */
1083 case 0:
1084 call_kill:
1085 __kill_fasync(sock->fasync_list, SIGIO, band);
1086 break;
1087 case 3:
1088 __kill_fasync(sock->fasync_list, SIGURG, band);
1090 return 0;
1093 static int __sock_create(int family, int type, int protocol,
1094 struct socket **res, int kern)
1096 int err;
1097 struct socket *sock;
1098 const struct net_proto_family *pf;
1101 * Check protocol is in range
1103 if (family < 0 || family >= NPROTO)
1104 return -EAFNOSUPPORT;
1105 if (type < 0 || type >= SOCK_MAX)
1106 return -EINVAL;
1108 /* Compatibility.
1110 This uglymoron is moved from INET layer to here to avoid
1111 deadlock in module load.
1113 if (family == PF_INET && type == SOCK_PACKET) {
1114 static int warned;
1115 if (!warned) {
1116 warned = 1;
1117 printk(KERN_INFO "%s uses obsolete (PF_INET,SOCK_PACKET)\n",
1118 current->comm);
1120 family = PF_PACKET;
1123 err = security_socket_create(family, type, protocol, kern);
1124 if (err)
1125 return err;
1128 * Allocate the socket and allow the family to set things up. if
1129 * the protocol is 0, the family is instructed to select an appropriate
1130 * default.
1132 sock = sock_alloc();
1133 if (!sock) {
1134 if (net_ratelimit())
1135 printk(KERN_WARNING "socket: no more sockets\n");
1136 return -ENFILE; /* Not exactly a match, but its the
1137 closest posix thing */
1140 sock->type = type;
1142 #if defined(CONFIG_KMOD)
1143 /* Attempt to load a protocol module if the find failed.
1145 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
1146 * requested real, full-featured networking support upon configuration.
1147 * Otherwise module support will break!
1149 if (net_families[family] == NULL)
1150 request_module("net-pf-%d", family);
1151 #endif
1153 rcu_read_lock();
1154 pf = rcu_dereference(net_families[family]);
1155 err = -EAFNOSUPPORT;
1156 if (!pf)
1157 goto out_release;
1160 * We will call the ->create function, that possibly is in a loadable
1161 * module, so we have to bump that loadable module refcnt first.
1163 if (!try_module_get(pf->owner))
1164 goto out_release;
1166 /* Now protected by module ref count */
1167 rcu_read_unlock();
1169 err = pf->create(sock, protocol);
1170 if (err < 0)
1171 goto out_module_put;
1174 * Now to bump the refcnt of the [loadable] module that owns this
1175 * socket at sock_release time we decrement its refcnt.
1177 if (!try_module_get(sock->ops->owner))
1178 goto out_module_busy;
1181 * Now that we're done with the ->create function, the [loadable]
1182 * module can have its refcnt decremented
1184 module_put(pf->owner);
1185 err = security_socket_post_create(sock, family, type, protocol, kern);
1186 if (err)
1187 goto out_release;
1188 *res = sock;
1190 return 0;
1192 out_module_busy:
1193 err = -EAFNOSUPPORT;
1194 out_module_put:
1195 sock->ops = NULL;
1196 module_put(pf->owner);
1197 out_sock_release:
1198 sock_release(sock);
1199 return err;
1201 out_release:
1202 rcu_read_unlock();
1203 goto out_sock_release;
1206 int sock_create(int family, int type, int protocol, struct socket **res)
1208 return __sock_create(family, type, protocol, res, 0);
1211 int sock_create_kern(int family, int type, int protocol, struct socket **res)
1213 return __sock_create(family, type, protocol, res, 1);
1216 asmlinkage long sys_socket(int family, int type, int protocol)
1218 int retval;
1219 struct socket *sock;
1221 retval = sock_create(family, type, protocol, &sock);
1222 if (retval < 0)
1223 goto out;
1225 retval = sock_map_fd(sock);
1226 if (retval < 0)
1227 goto out_release;
1229 out:
1230 /* It may be already another descriptor 8) Not kernel problem. */
1231 return retval;
1233 out_release:
1234 sock_release(sock);
1235 return retval;
1239 * Create a pair of connected sockets.
1242 asmlinkage long sys_socketpair(int family, int type, int protocol,
1243 int __user *usockvec)
1245 struct socket *sock1, *sock2;
1246 int fd1, fd2, err;
1249 * Obtain the first socket and check if the underlying protocol
1250 * supports the socketpair call.
1253 err = sock_create(family, type, protocol, &sock1);
1254 if (err < 0)
1255 goto out;
1257 err = sock_create(family, type, protocol, &sock2);
1258 if (err < 0)
1259 goto out_release_1;
1261 err = sock1->ops->socketpair(sock1, sock2);
1262 if (err < 0)
1263 goto out_release_both;
1265 fd1 = fd2 = -1;
1267 err = sock_map_fd(sock1);
1268 if (err < 0)
1269 goto out_release_both;
1270 fd1 = err;
1272 err = sock_map_fd(sock2);
1273 if (err < 0)
1274 goto out_close_1;
1275 fd2 = err;
1277 /* fd1 and fd2 may be already another descriptors.
1278 * Not kernel problem.
1281 err = put_user(fd1, &usockvec[0]);
1282 if (!err)
1283 err = put_user(fd2, &usockvec[1]);
1284 if (!err)
1285 return 0;
1287 sys_close(fd2);
1288 sys_close(fd1);
1289 return err;
1291 out_close_1:
1292 sock_release(sock2);
1293 sys_close(fd1);
1294 return err;
1296 out_release_both:
1297 sock_release(sock2);
1298 out_release_1:
1299 sock_release(sock1);
1300 out:
1301 return err;
1305 * Bind a name to a socket. Nothing much to do here since it's
1306 * the protocol's responsibility to handle the local address.
1308 * We move the socket address to kernel space before we call
1309 * the protocol layer (having also checked the address is ok).
1312 asmlinkage long sys_bind(int fd, struct sockaddr __user *umyaddr, int addrlen)
1314 struct socket *sock;
1315 char address[MAX_SOCK_ADDR];
1316 int err, fput_needed;
1318 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1319 if(sock) {
1320 err = move_addr_to_kernel(umyaddr, addrlen, address);
1321 if (err >= 0) {
1322 err = security_socket_bind(sock,
1323 (struct sockaddr *)address,
1324 addrlen);
1325 if (!err)
1326 err = sock->ops->bind(sock,
1327 (struct sockaddr *)
1328 address, addrlen);
1330 fput_light(sock->file, fput_needed);
1332 return err;
1336 * Perform a listen. Basically, we allow the protocol to do anything
1337 * necessary for a listen, and if that works, we mark the socket as
1338 * ready for listening.
1341 int sysctl_somaxconn = SOMAXCONN;
1343 asmlinkage long sys_listen(int fd, int backlog)
1345 struct socket *sock;
1346 int err, fput_needed;
1348 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1349 if (sock) {
1350 if ((unsigned)backlog > sysctl_somaxconn)
1351 backlog = sysctl_somaxconn;
1353 err = security_socket_listen(sock, backlog);
1354 if (!err)
1355 err = sock->ops->listen(sock, backlog);
1357 fput_light(sock->file, fput_needed);
1359 return err;
1363 * For accept, we attempt to create a new socket, set up the link
1364 * with the client, wake up the client, then return the new
1365 * connected fd. We collect the address of the connector in kernel
1366 * space and move it to user at the very end. This is unclean because
1367 * we open the socket then return an error.
1369 * 1003.1g adds the ability to recvmsg() to query connection pending
1370 * status to recvmsg. We need to add that support in a way thats
1371 * clean when we restucture accept also.
1374 asmlinkage long sys_accept(int fd, struct sockaddr __user *upeer_sockaddr,
1375 int __user *upeer_addrlen)
1377 struct socket *sock, *newsock;
1378 struct file *newfile;
1379 int err, len, newfd, fput_needed;
1380 char address[MAX_SOCK_ADDR];
1382 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1383 if (!sock)
1384 goto out;
1386 err = -ENFILE;
1387 if (!(newsock = sock_alloc()))
1388 goto out_put;
1390 newsock->type = sock->type;
1391 newsock->ops = sock->ops;
1394 * We don't need try_module_get here, as the listening socket (sock)
1395 * has the protocol module (sock->ops->owner) held.
1397 __module_get(newsock->ops->owner);
1399 newfd = sock_alloc_fd(&newfile);
1400 if (unlikely(newfd < 0)) {
1401 err = newfd;
1402 sock_release(newsock);
1403 goto out_put;
1406 err = sock_attach_fd(newsock, newfile);
1407 if (err < 0)
1408 goto out_fd;
1410 err = security_socket_accept(sock, newsock);
1411 if (err)
1412 goto out_fd;
1414 err = sock->ops->accept(sock, newsock, sock->file->f_flags);
1415 if (err < 0)
1416 goto out_fd;
1418 if (upeer_sockaddr) {
1419 if (newsock->ops->getname(newsock, (struct sockaddr *)address,
1420 &len, 2) < 0) {
1421 err = -ECONNABORTED;
1422 goto out_fd;
1424 err = move_addr_to_user(address, len, upeer_sockaddr,
1425 upeer_addrlen);
1426 if (err < 0)
1427 goto out_fd;
1430 /* File flags are not inherited via accept() unlike another OSes. */
1432 fd_install(newfd, newfile);
1433 err = newfd;
1435 security_socket_post_accept(sock, newsock);
1437 out_put:
1438 fput_light(sock->file, fput_needed);
1439 out:
1440 return err;
1441 out_fd:
1442 fput(newfile);
1443 put_unused_fd(newfd);
1444 goto out_put;
1448 * Attempt to connect to a socket with the server address. The address
1449 * is in user space so we verify it is OK and move it to kernel space.
1451 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
1452 * break bindings
1454 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
1455 * other SEQPACKET protocols that take time to connect() as it doesn't
1456 * include the -EINPROGRESS status for such sockets.
1459 asmlinkage long sys_connect(int fd, struct sockaddr __user *uservaddr,
1460 int addrlen)
1462 struct socket *sock;
1463 char address[MAX_SOCK_ADDR];
1464 int err, fput_needed;
1466 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1467 if (!sock)
1468 goto out;
1469 err = move_addr_to_kernel(uservaddr, addrlen, address);
1470 if (err < 0)
1471 goto out_put;
1473 err =
1474 security_socket_connect(sock, (struct sockaddr *)address, addrlen);
1475 if (err)
1476 goto out_put;
1478 err = sock->ops->connect(sock, (struct sockaddr *)address, addrlen,
1479 sock->file->f_flags);
1480 out_put:
1481 fput_light(sock->file, fput_needed);
1482 out:
1483 return err;
1487 * Get the local address ('name') of a socket object. Move the obtained
1488 * name to user space.
1491 asmlinkage long sys_getsockname(int fd, struct sockaddr __user *usockaddr,
1492 int __user *usockaddr_len)
1494 struct socket *sock;
1495 char address[MAX_SOCK_ADDR];
1496 int len, err, fput_needed;
1498 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1499 if (!sock)
1500 goto out;
1502 err = security_socket_getsockname(sock);
1503 if (err)
1504 goto out_put;
1506 err = sock->ops->getname(sock, (struct sockaddr *)address, &len, 0);
1507 if (err)
1508 goto out_put;
1509 err = move_addr_to_user(address, len, usockaddr, usockaddr_len);
1511 out_put:
1512 fput_light(sock->file, fput_needed);
1513 out:
1514 return err;
1518 * Get the remote address ('name') of a socket object. Move the obtained
1519 * name to user space.
1522 asmlinkage long sys_getpeername(int fd, struct sockaddr __user *usockaddr,
1523 int __user *usockaddr_len)
1525 struct socket *sock;
1526 char address[MAX_SOCK_ADDR];
1527 int len, err, fput_needed;
1529 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1530 if (sock != NULL) {
1531 err = security_socket_getpeername(sock);
1532 if (err) {
1533 fput_light(sock->file, fput_needed);
1534 return err;
1537 err =
1538 sock->ops->getname(sock, (struct sockaddr *)address, &len,
1540 if (!err)
1541 err = move_addr_to_user(address, len, usockaddr,
1542 usockaddr_len);
1543 fput_light(sock->file, fput_needed);
1545 return err;
1549 * Send a datagram to a given address. We move the address into kernel
1550 * space and check the user space data area is readable before invoking
1551 * the protocol.
1554 asmlinkage long sys_sendto(int fd, void __user *buff, size_t len,
1555 unsigned flags, struct sockaddr __user *addr,
1556 int addr_len)
1558 struct socket *sock;
1559 char address[MAX_SOCK_ADDR];
1560 int err;
1561 struct msghdr msg;
1562 struct iovec iov;
1563 int fput_needed;
1564 struct file *sock_file;
1566 sock_file = fget_light(fd, &fput_needed);
1567 if (!sock_file)
1568 return -EBADF;
1570 sock = sock_from_file(sock_file, &err);
1571 if (!sock)
1572 goto out_put;
1573 iov.iov_base = buff;
1574 iov.iov_len = len;
1575 msg.msg_name = NULL;
1576 msg.msg_iov = &iov;
1577 msg.msg_iovlen = 1;
1578 msg.msg_control = NULL;
1579 msg.msg_controllen = 0;
1580 msg.msg_namelen = 0;
1581 if (addr) {
1582 err = move_addr_to_kernel(addr, addr_len, address);
1583 if (err < 0)
1584 goto out_put;
1585 msg.msg_name = address;
1586 msg.msg_namelen = addr_len;
1588 if (sock->file->f_flags & O_NONBLOCK)
1589 flags |= MSG_DONTWAIT;
1590 msg.msg_flags = flags;
1591 err = sock_sendmsg(sock, &msg, len);
1593 out_put:
1594 fput_light(sock_file, fput_needed);
1595 return err;
1599 * Send a datagram down a socket.
1602 asmlinkage long sys_send(int fd, void __user *buff, size_t len, unsigned flags)
1604 return sys_sendto(fd, buff, len, flags, NULL, 0);
1608 * Receive a frame from the socket and optionally record the address of the
1609 * sender. We verify the buffers are writable and if needed move the
1610 * sender address from kernel to user space.
1613 asmlinkage long sys_recvfrom(int fd, void __user *ubuf, size_t size,
1614 unsigned flags, struct sockaddr __user *addr,
1615 int __user *addr_len)
1617 struct socket *sock;
1618 struct iovec iov;
1619 struct msghdr msg;
1620 char address[MAX_SOCK_ADDR];
1621 int err, err2;
1622 struct file *sock_file;
1623 int fput_needed;
1625 sock_file = fget_light(fd, &fput_needed);
1626 if (!sock_file)
1627 return -EBADF;
1629 sock = sock_from_file(sock_file, &err);
1630 if (!sock)
1631 goto out;
1633 msg.msg_control = NULL;
1634 msg.msg_controllen = 0;
1635 msg.msg_iovlen = 1;
1636 msg.msg_iov = &iov;
1637 iov.iov_len = size;
1638 iov.iov_base = ubuf;
1639 msg.msg_name = address;
1640 msg.msg_namelen = MAX_SOCK_ADDR;
1641 if (sock->file->f_flags & O_NONBLOCK)
1642 flags |= MSG_DONTWAIT;
1643 err = sock_recvmsg(sock, &msg, size, flags);
1645 if (err >= 0 && addr != NULL) {
1646 err2 = move_addr_to_user(address, msg.msg_namelen, addr, addr_len);
1647 if (err2 < 0)
1648 err = err2;
1650 out:
1651 fput_light(sock_file, fput_needed);
1652 return err;
1656 * Receive a datagram from a socket.
1659 asmlinkage long sys_recv(int fd, void __user *ubuf, size_t size,
1660 unsigned flags)
1662 return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
1666 * Set a socket option. Because we don't know the option lengths we have
1667 * to pass the user mode parameter for the protocols to sort out.
1670 asmlinkage long sys_setsockopt(int fd, int level, int optname,
1671 char __user *optval, int optlen)
1673 int err, fput_needed;
1674 struct socket *sock;
1676 if (optlen < 0)
1677 return -EINVAL;
1679 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1680 if (sock != NULL) {
1681 err = security_socket_setsockopt(sock, level, optname);
1682 if (err)
1683 goto out_put;
1685 if (level == SOL_SOCKET)
1686 err =
1687 sock_setsockopt(sock, level, optname, optval,
1688 optlen);
1689 else
1690 err =
1691 sock->ops->setsockopt(sock, level, optname, optval,
1692 optlen);
1693 out_put:
1694 fput_light(sock->file, fput_needed);
1696 return err;
1700 * Get a socket option. Because we don't know the option lengths we have
1701 * to pass a user mode parameter for the protocols to sort out.
1704 asmlinkage long sys_getsockopt(int fd, int level, int optname,
1705 char __user *optval, int __user *optlen)
1707 int err, fput_needed;
1708 struct socket *sock;
1710 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1711 if (sock != NULL) {
1712 err = security_socket_getsockopt(sock, level, optname);
1713 if (err)
1714 goto out_put;
1716 if (level == SOL_SOCKET)
1717 err =
1718 sock_getsockopt(sock, level, optname, optval,
1719 optlen);
1720 else
1721 err =
1722 sock->ops->getsockopt(sock, level, optname, optval,
1723 optlen);
1724 out_put:
1725 fput_light(sock->file, fput_needed);
1727 return err;
1731 * Shutdown a socket.
1734 asmlinkage long sys_shutdown(int fd, int how)
1736 int err, fput_needed;
1737 struct socket *sock;
1739 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1740 if (sock != NULL) {
1741 err = security_socket_shutdown(sock, how);
1742 if (!err)
1743 err = sock->ops->shutdown(sock, how);
1744 fput_light(sock->file, fput_needed);
1746 return err;
1749 /* A couple of helpful macros for getting the address of the 32/64 bit
1750 * fields which are the same type (int / unsigned) on our platforms.
1752 #define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
1753 #define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
1754 #define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
1757 * BSD sendmsg interface
1760 asmlinkage long sys_sendmsg(int fd, struct msghdr __user *msg, unsigned flags)
1762 struct compat_msghdr __user *msg_compat =
1763 (struct compat_msghdr __user *)msg;
1764 struct socket *sock;
1765 char address[MAX_SOCK_ADDR];
1766 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
1767 unsigned char ctl[sizeof(struct cmsghdr) + 20]
1768 __attribute__ ((aligned(sizeof(__kernel_size_t))));
1769 /* 20 is size of ipv6_pktinfo */
1770 unsigned char *ctl_buf = ctl;
1771 struct msghdr msg_sys;
1772 int err, ctl_len, iov_size, total_len;
1773 int fput_needed;
1775 err = -EFAULT;
1776 if (MSG_CMSG_COMPAT & flags) {
1777 if (get_compat_msghdr(&msg_sys, msg_compat))
1778 return -EFAULT;
1780 else if (copy_from_user(&msg_sys, msg, sizeof(struct msghdr)))
1781 return -EFAULT;
1783 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1784 if (!sock)
1785 goto out;
1787 /* do not move before msg_sys is valid */
1788 err = -EMSGSIZE;
1789 if (msg_sys.msg_iovlen > UIO_MAXIOV)
1790 goto out_put;
1792 /* Check whether to allocate the iovec area */
1793 err = -ENOMEM;
1794 iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
1795 if (msg_sys.msg_iovlen > UIO_FASTIOV) {
1796 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
1797 if (!iov)
1798 goto out_put;
1801 /* This will also move the address data into kernel space */
1802 if (MSG_CMSG_COMPAT & flags) {
1803 err = verify_compat_iovec(&msg_sys, iov, address, VERIFY_READ);
1804 } else
1805 err = verify_iovec(&msg_sys, iov, address, VERIFY_READ);
1806 if (err < 0)
1807 goto out_freeiov;
1808 total_len = err;
1810 err = -ENOBUFS;
1812 if (msg_sys.msg_controllen > INT_MAX)
1813 goto out_freeiov;
1814 ctl_len = msg_sys.msg_controllen;
1815 if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
1816 err =
1817 cmsghdr_from_user_compat_to_kern(&msg_sys, sock->sk, ctl,
1818 sizeof(ctl));
1819 if (err)
1820 goto out_freeiov;
1821 ctl_buf = msg_sys.msg_control;
1822 ctl_len = msg_sys.msg_controllen;
1823 } else if (ctl_len) {
1824 if (ctl_len > sizeof(ctl)) {
1825 ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
1826 if (ctl_buf == NULL)
1827 goto out_freeiov;
1829 err = -EFAULT;
1831 * Careful! Before this, msg_sys.msg_control contains a user pointer.
1832 * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
1833 * checking falls down on this.
1835 if (copy_from_user(ctl_buf, (void __user *)msg_sys.msg_control,
1836 ctl_len))
1837 goto out_freectl;
1838 msg_sys.msg_control = ctl_buf;
1840 msg_sys.msg_flags = flags;
1842 if (sock->file->f_flags & O_NONBLOCK)
1843 msg_sys.msg_flags |= MSG_DONTWAIT;
1844 err = sock_sendmsg(sock, &msg_sys, total_len);
1846 out_freectl:
1847 if (ctl_buf != ctl)
1848 sock_kfree_s(sock->sk, ctl_buf, ctl_len);
1849 out_freeiov:
1850 if (iov != iovstack)
1851 sock_kfree_s(sock->sk, iov, iov_size);
1852 out_put:
1853 fput_light(sock->file, fput_needed);
1854 out:
1855 return err;
1859 * BSD recvmsg interface
1862 asmlinkage long sys_recvmsg(int fd, struct msghdr __user *msg,
1863 unsigned int flags)
1865 struct compat_msghdr __user *msg_compat =
1866 (struct compat_msghdr __user *)msg;
1867 struct socket *sock;
1868 struct iovec iovstack[UIO_FASTIOV];
1869 struct iovec *iov = iovstack;
1870 struct msghdr msg_sys;
1871 unsigned long cmsg_ptr;
1872 int err, iov_size, total_len, len;
1873 int fput_needed;
1875 /* kernel mode address */
1876 char addr[MAX_SOCK_ADDR];
1878 /* user mode address pointers */
1879 struct sockaddr __user *uaddr;
1880 int __user *uaddr_len;
1882 if (MSG_CMSG_COMPAT & flags) {
1883 if (get_compat_msghdr(&msg_sys, msg_compat))
1884 return -EFAULT;
1886 else if (copy_from_user(&msg_sys, msg, sizeof(struct msghdr)))
1887 return -EFAULT;
1889 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1890 if (!sock)
1891 goto out;
1893 err = -EMSGSIZE;
1894 if (msg_sys.msg_iovlen > UIO_MAXIOV)
1895 goto out_put;
1897 /* Check whether to allocate the iovec area */
1898 err = -ENOMEM;
1899 iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
1900 if (msg_sys.msg_iovlen > UIO_FASTIOV) {
1901 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
1902 if (!iov)
1903 goto out_put;
1907 * Save the user-mode address (verify_iovec will change the
1908 * kernel msghdr to use the kernel address space)
1911 uaddr = (void __user *)msg_sys.msg_name;
1912 uaddr_len = COMPAT_NAMELEN(msg);
1913 if (MSG_CMSG_COMPAT & flags) {
1914 err = verify_compat_iovec(&msg_sys, iov, addr, VERIFY_WRITE);
1915 } else
1916 err = verify_iovec(&msg_sys, iov, addr, VERIFY_WRITE);
1917 if (err < 0)
1918 goto out_freeiov;
1919 total_len = err;
1921 cmsg_ptr = (unsigned long)msg_sys.msg_control;
1922 msg_sys.msg_flags = 0;
1923 if (MSG_CMSG_COMPAT & flags)
1924 msg_sys.msg_flags = MSG_CMSG_COMPAT;
1926 if (sock->file->f_flags & O_NONBLOCK)
1927 flags |= MSG_DONTWAIT;
1928 err = sock_recvmsg(sock, &msg_sys, total_len, flags);
1929 if (err < 0)
1930 goto out_freeiov;
1931 len = err;
1933 if (uaddr != NULL) {
1934 err = move_addr_to_user(addr, msg_sys.msg_namelen, uaddr,
1935 uaddr_len);
1936 if (err < 0)
1937 goto out_freeiov;
1939 err = __put_user((msg_sys.msg_flags & ~MSG_CMSG_COMPAT),
1940 COMPAT_FLAGS(msg));
1941 if (err)
1942 goto out_freeiov;
1943 if (MSG_CMSG_COMPAT & flags)
1944 err = __put_user((unsigned long)msg_sys.msg_control - cmsg_ptr,
1945 &msg_compat->msg_controllen);
1946 else
1947 err = __put_user((unsigned long)msg_sys.msg_control - cmsg_ptr,
1948 &msg->msg_controllen);
1949 if (err)
1950 goto out_freeiov;
1951 err = len;
1953 out_freeiov:
1954 if (iov != iovstack)
1955 sock_kfree_s(sock->sk, iov, iov_size);
1956 out_put:
1957 fput_light(sock->file, fput_needed);
1958 out:
1959 return err;
1962 #ifdef __ARCH_WANT_SYS_SOCKETCALL
1964 /* Argument list sizes for sys_socketcall */
1965 #define AL(x) ((x) * sizeof(unsigned long))
1966 static const unsigned char nargs[18]={
1967 AL(0),AL(3),AL(3),AL(3),AL(2),AL(3),
1968 AL(3),AL(3),AL(4),AL(4),AL(4),AL(6),
1969 AL(6),AL(2),AL(5),AL(5),AL(3),AL(3)
1972 #undef AL
1975 * System call vectors.
1977 * Argument checking cleaned up. Saved 20% in size.
1978 * This function doesn't need to set the kernel lock because
1979 * it is set by the callees.
1982 asmlinkage long sys_socketcall(int call, unsigned long __user *args)
1984 unsigned long a[6];
1985 unsigned long a0, a1;
1986 int err;
1988 if (call < 1 || call > SYS_RECVMSG)
1989 return -EINVAL;
1991 /* copy_from_user should be SMP safe. */
1992 if (copy_from_user(a, args, nargs[call]))
1993 return -EFAULT;
1995 err = audit_socketcall(nargs[call] / sizeof(unsigned long), a);
1996 if (err)
1997 return err;
1999 a0 = a[0];
2000 a1 = a[1];
2002 switch (call) {
2003 case SYS_SOCKET:
2004 err = sys_socket(a0, a1, a[2]);
2005 break;
2006 case SYS_BIND:
2007 err = sys_bind(a0, (struct sockaddr __user *)a1, a[2]);
2008 break;
2009 case SYS_CONNECT:
2010 err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
2011 break;
2012 case SYS_LISTEN:
2013 err = sys_listen(a0, a1);
2014 break;
2015 case SYS_ACCEPT:
2016 err =
2017 sys_accept(a0, (struct sockaddr __user *)a1,
2018 (int __user *)a[2]);
2019 break;
2020 case SYS_GETSOCKNAME:
2021 err =
2022 sys_getsockname(a0, (struct sockaddr __user *)a1,
2023 (int __user *)a[2]);
2024 break;
2025 case SYS_GETPEERNAME:
2026 err =
2027 sys_getpeername(a0, (struct sockaddr __user *)a1,
2028 (int __user *)a[2]);
2029 break;
2030 case SYS_SOCKETPAIR:
2031 err = sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
2032 break;
2033 case SYS_SEND:
2034 err = sys_send(a0, (void __user *)a1, a[2], a[3]);
2035 break;
2036 case SYS_SENDTO:
2037 err = sys_sendto(a0, (void __user *)a1, a[2], a[3],
2038 (struct sockaddr __user *)a[4], a[5]);
2039 break;
2040 case SYS_RECV:
2041 err = sys_recv(a0, (void __user *)a1, a[2], a[3]);
2042 break;
2043 case SYS_RECVFROM:
2044 err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
2045 (struct sockaddr __user *)a[4],
2046 (int __user *)a[5]);
2047 break;
2048 case SYS_SHUTDOWN:
2049 err = sys_shutdown(a0, a1);
2050 break;
2051 case SYS_SETSOCKOPT:
2052 err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]);
2053 break;
2054 case SYS_GETSOCKOPT:
2055 err =
2056 sys_getsockopt(a0, a1, a[2], (char __user *)a[3],
2057 (int __user *)a[4]);
2058 break;
2059 case SYS_SENDMSG:
2060 err = sys_sendmsg(a0, (struct msghdr __user *)a1, a[2]);
2061 break;
2062 case SYS_RECVMSG:
2063 err = sys_recvmsg(a0, (struct msghdr __user *)a1, a[2]);
2064 break;
2065 default:
2066 err = -EINVAL;
2067 break;
2069 return err;
2072 #endif /* __ARCH_WANT_SYS_SOCKETCALL */
2075 * sock_register - add a socket protocol handler
2076 * @ops: description of protocol
2078 * This function is called by a protocol handler that wants to
2079 * advertise its address family, and have it linked into the
2080 * socket interface. The value ops->family coresponds to the
2081 * socket system call protocol family.
2083 int sock_register(struct net_proto_family *ops)
2085 int err;
2087 if (ops->family >= NPROTO) {
2088 printk(KERN_CRIT "protocol %d >= NPROTO(%d)\n", ops->family,
2089 NPROTO);
2090 return -ENOBUFS;
2093 spin_lock(&net_family_lock);
2094 if (net_families[ops->family])
2095 err = -EEXIST;
2096 else {
2097 net_families[ops->family] = ops;
2098 err = 0;
2100 spin_unlock(&net_family_lock);
2102 printk(KERN_INFO "NET: Registered protocol family %d\n", ops->family);
2103 return err;
2107 * sock_unregister - remove a protocol handler
2108 * @family: protocol family to remove
2110 * This function is called by a protocol handler that wants to
2111 * remove its address family, and have it unlinked from the
2112 * new socket creation.
2114 * If protocol handler is a module, then it can use module reference
2115 * counts to protect against new references. If protocol handler is not
2116 * a module then it needs to provide its own protection in
2117 * the ops->create routine.
2119 int sock_unregister(int family)
2121 if (family < 0 || family >= NPROTO)
2122 return -EINVAL;
2124 spin_lock(&net_family_lock);
2125 net_families[family] = NULL;
2126 spin_unlock(&net_family_lock);
2128 synchronize_rcu();
2130 printk(KERN_INFO "NET: Unregistered protocol family %d\n", family);
2131 return 0;
2134 static int __init sock_init(void)
2137 * Initialize sock SLAB cache.
2140 sk_init();
2143 * Initialize skbuff SLAB cache
2145 skb_init();
2148 * Initialize the protocols module.
2151 init_inodecache();
2152 register_filesystem(&sock_fs_type);
2153 sock_mnt = kern_mount(&sock_fs_type);
2155 /* The real protocol initialization is performed in later initcalls.
2158 #ifdef CONFIG_NETFILTER
2159 netfilter_init();
2160 #endif
2162 return 0;
2165 core_initcall(sock_init); /* early initcall */
2167 #ifdef CONFIG_PROC_FS
2168 void socket_seq_show(struct seq_file *seq)
2170 int cpu;
2171 int counter = 0;
2173 for_each_possible_cpu(cpu)
2174 counter += per_cpu(sockets_in_use, cpu);
2176 /* It can be negative, by the way. 8) */
2177 if (counter < 0)
2178 counter = 0;
2180 seq_printf(seq, "sockets: used %d\n", counter);
2182 #endif /* CONFIG_PROC_FS */
2184 #ifdef CONFIG_COMPAT
2185 static long compat_sock_ioctl(struct file *file, unsigned cmd,
2186 unsigned long arg)
2188 struct socket *sock = file->private_data;
2189 int ret = -ENOIOCTLCMD;
2191 if (sock->ops->compat_ioctl)
2192 ret = sock->ops->compat_ioctl(sock, cmd, arg);
2194 return ret;
2196 #endif
2198 int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen)
2200 return sock->ops->bind(sock, addr, addrlen);
2203 int kernel_listen(struct socket *sock, int backlog)
2205 return sock->ops->listen(sock, backlog);
2208 int kernel_accept(struct socket *sock, struct socket **newsock, int flags)
2210 struct sock *sk = sock->sk;
2211 int err;
2213 err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
2214 newsock);
2215 if (err < 0)
2216 goto done;
2218 err = sock->ops->accept(sock, *newsock, flags);
2219 if (err < 0) {
2220 sock_release(*newsock);
2221 goto done;
2224 (*newsock)->ops = sock->ops;
2226 done:
2227 return err;
2230 int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
2231 int flags)
2233 return sock->ops->connect(sock, addr, addrlen, flags);
2236 int kernel_getsockname(struct socket *sock, struct sockaddr *addr,
2237 int *addrlen)
2239 return sock->ops->getname(sock, addr, addrlen, 0);
2242 int kernel_getpeername(struct socket *sock, struct sockaddr *addr,
2243 int *addrlen)
2245 return sock->ops->getname(sock, addr, addrlen, 1);
2248 int kernel_getsockopt(struct socket *sock, int level, int optname,
2249 char *optval, int *optlen)
2251 mm_segment_t oldfs = get_fs();
2252 int err;
2254 set_fs(KERNEL_DS);
2255 if (level == SOL_SOCKET)
2256 err = sock_getsockopt(sock, level, optname, optval, optlen);
2257 else
2258 err = sock->ops->getsockopt(sock, level, optname, optval,
2259 optlen);
2260 set_fs(oldfs);
2261 return err;
2264 int kernel_setsockopt(struct socket *sock, int level, int optname,
2265 char *optval, int optlen)
2267 mm_segment_t oldfs = get_fs();
2268 int err;
2270 set_fs(KERNEL_DS);
2271 if (level == SOL_SOCKET)
2272 err = sock_setsockopt(sock, level, optname, optval, optlen);
2273 else
2274 err = sock->ops->setsockopt(sock, level, optname, optval,
2275 optlen);
2276 set_fs(oldfs);
2277 return err;
2280 int kernel_sendpage(struct socket *sock, struct page *page, int offset,
2281 size_t size, int flags)
2283 if (sock->ops->sendpage)
2284 return sock->ops->sendpage(sock, page, offset, size, flags);
2286 return sock_no_sendpage(sock, page, offset, size, flags);
2289 int kernel_sock_ioctl(struct socket *sock, int cmd, unsigned long arg)
2291 mm_segment_t oldfs = get_fs();
2292 int err;
2294 set_fs(KERNEL_DS);
2295 err = sock->ops->ioctl(sock, cmd, arg);
2296 set_fs(oldfs);
2298 return err;
2301 /* ABI emulation layers need these two */
2302 EXPORT_SYMBOL(move_addr_to_kernel);
2303 EXPORT_SYMBOL(move_addr_to_user);
2304 EXPORT_SYMBOL(sock_create);
2305 EXPORT_SYMBOL(sock_create_kern);
2306 EXPORT_SYMBOL(sock_create_lite);
2307 EXPORT_SYMBOL(sock_map_fd);
2308 EXPORT_SYMBOL(sock_recvmsg);
2309 EXPORT_SYMBOL(sock_register);
2310 EXPORT_SYMBOL(sock_release);
2311 EXPORT_SYMBOL(sock_sendmsg);
2312 EXPORT_SYMBOL(sock_unregister);
2313 EXPORT_SYMBOL(sock_wake_async);
2314 EXPORT_SYMBOL(sockfd_lookup);
2315 EXPORT_SYMBOL(kernel_sendmsg);
2316 EXPORT_SYMBOL(kernel_recvmsg);
2317 EXPORT_SYMBOL(kernel_bind);
2318 EXPORT_SYMBOL(kernel_listen);
2319 EXPORT_SYMBOL(kernel_accept);
2320 EXPORT_SYMBOL(kernel_connect);
2321 EXPORT_SYMBOL(kernel_getsockname);
2322 EXPORT_SYMBOL(kernel_getpeername);
2323 EXPORT_SYMBOL(kernel_getsockopt);
2324 EXPORT_SYMBOL(kernel_setsockopt);
2325 EXPORT_SYMBOL(kernel_sendpage);
2326 EXPORT_SYMBOL(kernel_sock_ioctl);