[DCCP]: Remove leftover dccp_send_response prototype
[linux-2.6/kvm.git] / net / socket.c
blob510ae18d220a31aa7e82bc374369e09fcf583158
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/config.h>
62 #include <linux/mm.h>
63 #include <linux/smp_lock.h>
64 #include <linux/socket.h>
65 #include <linux/file.h>
66 #include <linux/net.h>
67 #include <linux/interrupt.h>
68 #include <linux/netdevice.h>
69 #include <linux/proc_fs.h>
70 #include <linux/seq_file.h>
71 #include <linux/wanrouter.h>
72 #include <linux/if_bridge.h>
73 #include <linux/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,
108 unsigned int cmd, unsigned long arg);
109 static int sock_fasync(int fd, struct file *filp, int on);
110 static ssize_t sock_readv(struct file *file, const struct iovec *vector,
111 unsigned long count, loff_t *ppos);
112 static ssize_t sock_writev(struct file *file, const struct iovec *vector,
113 unsigned long count, loff_t *ppos);
114 static ssize_t sock_sendpage(struct file *file, struct page *page,
115 int offset, size_t size, loff_t *ppos, int more);
119 * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
120 * in the operation structures but are done directly via the socketcall() multiplexor.
123 static struct file_operations socket_file_ops = {
124 .owner = THIS_MODULE,
125 .llseek = no_llseek,
126 .aio_read = sock_aio_read,
127 .aio_write = sock_aio_write,
128 .poll = sock_poll,
129 .unlocked_ioctl = sock_ioctl,
130 .mmap = sock_mmap,
131 .open = sock_no_open, /* special open code to disallow open via /proc */
132 .release = sock_close,
133 .fasync = sock_fasync,
134 .readv = sock_readv,
135 .writev = sock_writev,
136 .sendpage = sock_sendpage
140 * The protocol list. Each protocol is registered in here.
143 static struct net_proto_family *net_families[NPROTO];
145 #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
146 static atomic_t net_family_lockct = ATOMIC_INIT(0);
147 static DEFINE_SPINLOCK(net_family_lock);
149 /* The strategy is: modifications net_family vector are short, do not
150 sleep and veeery rare, but read access should be free of any exclusive
151 locks.
154 static void net_family_write_lock(void)
156 spin_lock(&net_family_lock);
157 while (atomic_read(&net_family_lockct) != 0) {
158 spin_unlock(&net_family_lock);
160 yield();
162 spin_lock(&net_family_lock);
166 static __inline__ void net_family_write_unlock(void)
168 spin_unlock(&net_family_lock);
171 static __inline__ void net_family_read_lock(void)
173 atomic_inc(&net_family_lockct);
174 spin_unlock_wait(&net_family_lock);
177 static __inline__ void net_family_read_unlock(void)
179 atomic_dec(&net_family_lockct);
182 #else
183 #define net_family_write_lock() do { } while(0)
184 #define net_family_write_unlock() do { } while(0)
185 #define net_family_read_lock() do { } while(0)
186 #define net_family_read_unlock() do { } while(0)
187 #endif
191 * Statistics counters of the socket lists
194 static DEFINE_PER_CPU(int, sockets_in_use) = 0;
197 * Support routines. Move socket addresses back and forth across the kernel/user
198 * divide and look after the messy bits.
201 #define MAX_SOCK_ADDR 128 /* 108 for Unix domain -
202 16 for IP, 16 for IPX,
203 24 for IPv6,
204 about 80 for AX.25
205 must be at least one bigger than
206 the AF_UNIX size (see net/unix/af_unix.c
207 :unix_mkname()).
211 * move_addr_to_kernel - copy a socket address into kernel space
212 * @uaddr: Address in user space
213 * @kaddr: Address in kernel space
214 * @ulen: Length in user space
216 * The address is copied into kernel space. If the provided address is
217 * too long an error code of -EINVAL is returned. If the copy gives
218 * invalid addresses -EFAULT is returned. On a success 0 is returned.
221 int move_addr_to_kernel(void __user *uaddr, int ulen, void *kaddr)
223 if(ulen<0||ulen>MAX_SOCK_ADDR)
224 return -EINVAL;
225 if(ulen==0)
226 return 0;
227 if(copy_from_user(kaddr,uaddr,ulen))
228 return -EFAULT;
229 return audit_sockaddr(ulen, kaddr);
233 * move_addr_to_user - copy an address to user space
234 * @kaddr: kernel space address
235 * @klen: length of address in kernel
236 * @uaddr: user space address
237 * @ulen: pointer to user length field
239 * The value pointed to by ulen on entry is the buffer length available.
240 * This is overwritten with the buffer space used. -EINVAL is returned
241 * if an overlong buffer is specified or a negative buffer size. -EFAULT
242 * is returned if either the buffer or the length field are not
243 * accessible.
244 * After copying the data up to the limit the user specifies, the true
245 * length of the data is written over the length limit the user
246 * specified. Zero is returned for a success.
249 int move_addr_to_user(void *kaddr, int klen, void __user *uaddr, int __user *ulen)
251 int err;
252 int len;
254 if((err=get_user(len, ulen)))
255 return err;
256 if(len>klen)
257 len=klen;
258 if(len<0 || len> MAX_SOCK_ADDR)
259 return -EINVAL;
260 if(len)
262 if(copy_to_user(uaddr,kaddr,len))
263 return -EFAULT;
266 * "fromlen shall refer to the value before truncation.."
267 * 1003.1g
269 return __put_user(klen, ulen);
272 #define SOCKFS_MAGIC 0x534F434B
274 static kmem_cache_t * sock_inode_cachep __read_mostly;
276 static struct inode *sock_alloc_inode(struct super_block *sb)
278 struct socket_alloc *ei;
279 ei = (struct socket_alloc *)kmem_cache_alloc(sock_inode_cachep, SLAB_KERNEL);
280 if (!ei)
281 return NULL;
282 init_waitqueue_head(&ei->socket.wait);
284 ei->socket.fasync_list = NULL;
285 ei->socket.state = SS_UNCONNECTED;
286 ei->socket.flags = 0;
287 ei->socket.ops = NULL;
288 ei->socket.sk = NULL;
289 ei->socket.file = NULL;
290 ei->socket.flags = 0;
292 return &ei->vfs_inode;
295 static void sock_destroy_inode(struct inode *inode)
297 kmem_cache_free(sock_inode_cachep,
298 container_of(inode, struct socket_alloc, vfs_inode));
301 static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
303 struct socket_alloc *ei = (struct socket_alloc *) foo;
305 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
306 SLAB_CTOR_CONSTRUCTOR)
307 inode_init_once(&ei->vfs_inode);
310 static int init_inodecache(void)
312 sock_inode_cachep = kmem_cache_create("sock_inode_cache",
313 sizeof(struct socket_alloc),
314 0, SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT,
315 init_once, NULL);
316 if (sock_inode_cachep == NULL)
317 return -ENOMEM;
318 return 0;
321 static struct super_operations sockfs_ops = {
322 .alloc_inode = sock_alloc_inode,
323 .destroy_inode =sock_destroy_inode,
324 .statfs = simple_statfs,
327 static struct super_block *sockfs_get_sb(struct file_system_type *fs_type,
328 int flags, const char *dev_name, void *data)
330 return get_sb_pseudo(fs_type, "socket:", &sockfs_ops, SOCKFS_MAGIC);
333 static struct vfsmount *sock_mnt __read_mostly;
335 static struct file_system_type sock_fs_type = {
336 .name = "sockfs",
337 .get_sb = sockfs_get_sb,
338 .kill_sb = kill_anon_super,
340 static int sockfs_delete_dentry(struct dentry *dentry)
342 return 1;
344 static struct dentry_operations sockfs_dentry_operations = {
345 .d_delete = sockfs_delete_dentry,
349 * Obtains the first available file descriptor and sets it up for use.
351 * These functions create file structures and maps them to fd space
352 * of the current process. On success it returns file descriptor
353 * and file struct implicitly stored in sock->file.
354 * Note that another thread may close file descriptor before we return
355 * from this function. We use the fact that now we do not refer
356 * to socket after mapping. If one day we will need it, this
357 * function will increment ref. count on file by 1.
359 * In any case returned fd MAY BE not valid!
360 * This race condition is unavoidable
361 * with shared fd spaces, we cannot solve it inside kernel,
362 * but we take care of internal coherence yet.
365 static int sock_alloc_fd(struct file **filep)
367 int fd;
369 fd = get_unused_fd();
370 if (likely(fd >= 0)) {
371 struct file *file = get_empty_filp();
373 *filep = file;
374 if (unlikely(!file)) {
375 put_unused_fd(fd);
376 return -ENFILE;
378 } else
379 *filep = NULL;
380 return fd;
383 static int sock_attach_fd(struct socket *sock, struct file *file)
385 struct qstr this;
386 char name[32];
388 this.len = sprintf(name, "[%lu]", SOCK_INODE(sock)->i_ino);
389 this.name = name;
390 this.hash = SOCK_INODE(sock)->i_ino;
392 file->f_dentry = d_alloc(sock_mnt->mnt_sb->s_root, &this);
393 if (unlikely(!file->f_dentry))
394 return -ENOMEM;
396 file->f_dentry->d_op = &sockfs_dentry_operations;
397 d_add(file->f_dentry, SOCK_INODE(sock));
398 file->f_vfsmnt = mntget(sock_mnt);
399 file->f_mapping = file->f_dentry->d_inode->i_mapping;
401 sock->file = file;
402 file->f_op = SOCK_INODE(sock)->i_fop = &socket_file_ops;
403 file->f_mode = FMODE_READ | FMODE_WRITE;
404 file->f_flags = O_RDWR;
405 file->f_pos = 0;
406 file->private_data = sock;
408 return 0;
411 int sock_map_fd(struct socket *sock)
413 struct file *newfile;
414 int fd = sock_alloc_fd(&newfile);
416 if (likely(fd >= 0)) {
417 int err = sock_attach_fd(sock, newfile);
419 if (unlikely(err < 0)) {
420 put_filp(newfile);
421 put_unused_fd(fd);
422 return err;
424 fd_install(fd, newfile);
426 return fd;
429 static struct socket *sock_from_file(struct file *file, int *err)
431 struct inode *inode;
432 struct socket *sock;
434 if (file->f_op == &socket_file_ops)
435 return file->private_data; /* set in sock_map_fd */
437 inode = file->f_dentry->d_inode;
438 if (!S_ISSOCK(inode->i_mode)) {
439 *err = -ENOTSOCK;
440 return NULL;
443 sock = SOCKET_I(inode);
444 if (sock->file != file) {
445 printk(KERN_ERR "socki_lookup: socket file changed!\n");
446 sock->file = file;
448 return sock;
452 * sockfd_lookup - Go from a file number to its socket slot
453 * @fd: file handle
454 * @err: pointer to an error code return
456 * The file handle passed in is locked and the socket it is bound
457 * too is returned. If an error occurs the err pointer is overwritten
458 * with a negative errno code and NULL is returned. The function checks
459 * for both invalid handles and passing a handle which is not a socket.
461 * On a success the socket object pointer is returned.
464 struct socket *sockfd_lookup(int fd, int *err)
466 struct file *file;
467 struct socket *sock;
469 if (!(file = fget(fd))) {
470 *err = -EBADF;
471 return NULL;
473 sock = sock_from_file(file, err);
474 if (!sock)
475 fput(file);
476 return sock;
479 static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed)
481 struct file *file;
482 struct socket *sock;
484 file = fget_light(fd, fput_needed);
485 if (file) {
486 sock = sock_from_file(file, err);
487 if (sock)
488 return sock;
489 fput_light(file, *fput_needed);
491 return NULL;
495 * sock_alloc - allocate a socket
497 * Allocate a new inode and socket object. The two are bound together
498 * and initialised. The socket is then returned. If we are out of inodes
499 * NULL is returned.
502 static struct socket *sock_alloc(void)
504 struct inode * inode;
505 struct socket * sock;
507 inode = new_inode(sock_mnt->mnt_sb);
508 if (!inode)
509 return NULL;
511 sock = SOCKET_I(inode);
513 inode->i_mode = S_IFSOCK|S_IRWXUGO;
514 inode->i_uid = current->fsuid;
515 inode->i_gid = current->fsgid;
517 get_cpu_var(sockets_in_use)++;
518 put_cpu_var(sockets_in_use);
519 return sock;
523 * In theory you can't get an open on this inode, but /proc provides
524 * a back door. Remember to keep it shut otherwise you'll let the
525 * creepy crawlies in.
528 static int sock_no_open(struct inode *irrelevant, struct file *dontcare)
530 return -ENXIO;
533 struct file_operations bad_sock_fops = {
534 .owner = THIS_MODULE,
535 .open = sock_no_open,
539 * sock_release - close a socket
540 * @sock: socket to close
542 * The socket is released from the protocol stack if it has a release
543 * callback, and the inode is then released if the socket is bound to
544 * an inode not a file.
547 void sock_release(struct socket *sock)
549 if (sock->ops) {
550 struct module *owner = sock->ops->owner;
552 sock->ops->release(sock);
553 sock->ops = NULL;
554 module_put(owner);
557 if (sock->fasync_list)
558 printk(KERN_ERR "sock_release: fasync list not empty!\n");
560 get_cpu_var(sockets_in_use)--;
561 put_cpu_var(sockets_in_use);
562 if (!sock->file) {
563 iput(SOCK_INODE(sock));
564 return;
566 sock->file=NULL;
569 static inline int __sock_sendmsg(struct kiocb *iocb, struct socket *sock,
570 struct msghdr *msg, size_t size)
572 struct sock_iocb *si = kiocb_to_siocb(iocb);
573 int err;
575 si->sock = sock;
576 si->scm = NULL;
577 si->msg = msg;
578 si->size = size;
580 err = security_socket_sendmsg(sock, msg, size);
581 if (err)
582 return err;
584 return sock->ops->sendmsg(iocb, sock, msg, size);
587 int sock_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
589 struct kiocb iocb;
590 struct sock_iocb siocb;
591 int ret;
593 init_sync_kiocb(&iocb, NULL);
594 iocb.private = &siocb;
595 ret = __sock_sendmsg(&iocb, sock, msg, size);
596 if (-EIOCBQUEUED == ret)
597 ret = wait_on_sync_kiocb(&iocb);
598 return ret;
601 int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
602 struct kvec *vec, size_t num, size_t size)
604 mm_segment_t oldfs = get_fs();
605 int result;
607 set_fs(KERNEL_DS);
609 * the following is safe, since for compiler definitions of kvec and
610 * iovec are identical, yielding the same in-core layout and alignment
612 msg->msg_iov = (struct iovec *)vec,
613 msg->msg_iovlen = num;
614 result = sock_sendmsg(sock, msg, size);
615 set_fs(oldfs);
616 return result;
619 static inline int __sock_recvmsg(struct kiocb *iocb, struct socket *sock,
620 struct msghdr *msg, size_t size, int flags)
622 int err;
623 struct sock_iocb *si = kiocb_to_siocb(iocb);
625 si->sock = sock;
626 si->scm = NULL;
627 si->msg = msg;
628 si->size = size;
629 si->flags = flags;
631 err = security_socket_recvmsg(sock, msg, size, flags);
632 if (err)
633 return err;
635 return sock->ops->recvmsg(iocb, sock, msg, size, flags);
638 int sock_recvmsg(struct socket *sock, struct msghdr *msg,
639 size_t size, int flags)
641 struct kiocb iocb;
642 struct sock_iocb siocb;
643 int ret;
645 init_sync_kiocb(&iocb, NULL);
646 iocb.private = &siocb;
647 ret = __sock_recvmsg(&iocb, sock, msg, size, flags);
648 if (-EIOCBQUEUED == ret)
649 ret = wait_on_sync_kiocb(&iocb);
650 return ret;
653 int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
654 struct kvec *vec, size_t num,
655 size_t size, int flags)
657 mm_segment_t oldfs = get_fs();
658 int result;
660 set_fs(KERNEL_DS);
662 * the following is safe, since for compiler definitions of kvec and
663 * iovec are identical, yielding the same in-core layout and alignment
665 msg->msg_iov = (struct iovec *)vec,
666 msg->msg_iovlen = num;
667 result = sock_recvmsg(sock, msg, size, flags);
668 set_fs(oldfs);
669 return result;
672 static void sock_aio_dtor(struct kiocb *iocb)
674 kfree(iocb->private);
677 static ssize_t sock_sendpage(struct file *file, struct page *page,
678 int offset, size_t size, loff_t *ppos, int more)
680 struct socket *sock;
681 int flags;
683 sock = file->private_data;
685 flags = !(file->f_flags & O_NONBLOCK) ? 0 : MSG_DONTWAIT;
686 if (more)
687 flags |= MSG_MORE;
689 return sock->ops->sendpage(sock, page, offset, size, flags);
692 static struct sock_iocb *alloc_sock_iocb(struct kiocb *iocb,
693 char __user *ubuf, size_t size, struct sock_iocb *siocb)
695 if (!is_sync_kiocb(iocb)) {
696 siocb = kmalloc(sizeof(*siocb), GFP_KERNEL);
697 if (!siocb)
698 return NULL;
699 iocb->ki_dtor = sock_aio_dtor;
702 siocb->kiocb = iocb;
703 siocb->async_iov.iov_base = ubuf;
704 siocb->async_iov.iov_len = size;
706 iocb->private = siocb;
707 return siocb;
710 static ssize_t do_sock_read(struct msghdr *msg, struct kiocb *iocb,
711 struct file *file, struct iovec *iov, unsigned long nr_segs)
713 struct socket *sock = file->private_data;
714 size_t size = 0;
715 int i;
717 for (i = 0 ; i < nr_segs ; i++)
718 size += iov[i].iov_len;
720 msg->msg_name = NULL;
721 msg->msg_namelen = 0;
722 msg->msg_control = NULL;
723 msg->msg_controllen = 0;
724 msg->msg_iov = (struct iovec *) iov;
725 msg->msg_iovlen = nr_segs;
726 msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
728 return __sock_recvmsg(iocb, sock, msg, size, msg->msg_flags);
731 static ssize_t sock_readv(struct file *file, const struct iovec *iov,
732 unsigned long nr_segs, loff_t *ppos)
734 struct kiocb iocb;
735 struct sock_iocb siocb;
736 struct msghdr msg;
737 int ret;
739 init_sync_kiocb(&iocb, NULL);
740 iocb.private = &siocb;
742 ret = do_sock_read(&msg, &iocb, file, (struct iovec *)iov, nr_segs);
743 if (-EIOCBQUEUED == ret)
744 ret = wait_on_sync_kiocb(&iocb);
745 return ret;
748 static ssize_t sock_aio_read(struct kiocb *iocb, char __user *ubuf,
749 size_t count, loff_t pos)
751 struct sock_iocb siocb, *x;
753 if (pos != 0)
754 return -ESPIPE;
755 if (count == 0) /* Match SYS5 behaviour */
756 return 0;
758 x = alloc_sock_iocb(iocb, ubuf, count, &siocb);
759 if (!x)
760 return -ENOMEM;
761 return do_sock_read(&x->async_msg, iocb, iocb->ki_filp,
762 &x->async_iov, 1);
765 static ssize_t do_sock_write(struct msghdr *msg, struct kiocb *iocb,
766 struct file *file, struct iovec *iov, unsigned long nr_segs)
768 struct socket *sock = file->private_data;
769 size_t size = 0;
770 int i;
772 for (i = 0 ; i < nr_segs ; i++)
773 size += iov[i].iov_len;
775 msg->msg_name = NULL;
776 msg->msg_namelen = 0;
777 msg->msg_control = NULL;
778 msg->msg_controllen = 0;
779 msg->msg_iov = (struct iovec *) iov;
780 msg->msg_iovlen = nr_segs;
781 msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
782 if (sock->type == SOCK_SEQPACKET)
783 msg->msg_flags |= MSG_EOR;
785 return __sock_sendmsg(iocb, sock, msg, size);
788 static ssize_t sock_writev(struct file *file, const struct iovec *iov,
789 unsigned long nr_segs, loff_t *ppos)
791 struct msghdr msg;
792 struct kiocb iocb;
793 struct sock_iocb siocb;
794 int ret;
796 init_sync_kiocb(&iocb, NULL);
797 iocb.private = &siocb;
799 ret = do_sock_write(&msg, &iocb, file, (struct iovec *)iov, nr_segs);
800 if (-EIOCBQUEUED == ret)
801 ret = wait_on_sync_kiocb(&iocb);
802 return ret;
805 static ssize_t sock_aio_write(struct kiocb *iocb, const char __user *ubuf,
806 size_t count, loff_t pos)
808 struct sock_iocb siocb, *x;
810 if (pos != 0)
811 return -ESPIPE;
812 if (count == 0) /* Match SYS5 behaviour */
813 return 0;
815 x = alloc_sock_iocb(iocb, (void __user *)ubuf, count, &siocb);
816 if (!x)
817 return -ENOMEM;
819 return do_sock_write(&x->async_msg, iocb, iocb->ki_filp,
820 &x->async_iov, 1);
825 * Atomic setting of ioctl hooks to avoid race
826 * with module unload.
829 static DECLARE_MUTEX(br_ioctl_mutex);
830 static int (*br_ioctl_hook)(unsigned int cmd, void __user *arg) = NULL;
832 void brioctl_set(int (*hook)(unsigned int, void __user *))
834 down(&br_ioctl_mutex);
835 br_ioctl_hook = hook;
836 up(&br_ioctl_mutex);
838 EXPORT_SYMBOL(brioctl_set);
840 static DECLARE_MUTEX(vlan_ioctl_mutex);
841 static int (*vlan_ioctl_hook)(void __user *arg);
843 void vlan_ioctl_set(int (*hook)(void __user *))
845 down(&vlan_ioctl_mutex);
846 vlan_ioctl_hook = hook;
847 up(&vlan_ioctl_mutex);
849 EXPORT_SYMBOL(vlan_ioctl_set);
851 static DECLARE_MUTEX(dlci_ioctl_mutex);
852 static int (*dlci_ioctl_hook)(unsigned int, void __user *);
854 void dlci_ioctl_set(int (*hook)(unsigned int, void __user *))
856 down(&dlci_ioctl_mutex);
857 dlci_ioctl_hook = hook;
858 up(&dlci_ioctl_mutex);
860 EXPORT_SYMBOL(dlci_ioctl_set);
863 * With an ioctl, arg may well be a user mode pointer, but we don't know
864 * what to do with it - that's up to the protocol still.
867 static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
869 struct socket *sock;
870 void __user *argp = (void __user *)arg;
871 int pid, err;
873 sock = file->private_data;
874 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
875 err = dev_ioctl(cmd, argp);
876 } else
877 #ifdef CONFIG_WIRELESS_EXT
878 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
879 err = dev_ioctl(cmd, argp);
880 } else
881 #endif /* CONFIG_WIRELESS_EXT */
882 switch (cmd) {
883 case FIOSETOWN:
884 case SIOCSPGRP:
885 err = -EFAULT;
886 if (get_user(pid, (int __user *)argp))
887 break;
888 err = f_setown(sock->file, pid, 1);
889 break;
890 case FIOGETOWN:
891 case SIOCGPGRP:
892 err = put_user(sock->file->f_owner.pid, (int __user *)argp);
893 break;
894 case SIOCGIFBR:
895 case SIOCSIFBR:
896 case SIOCBRADDBR:
897 case SIOCBRDELBR:
898 err = -ENOPKG;
899 if (!br_ioctl_hook)
900 request_module("bridge");
902 down(&br_ioctl_mutex);
903 if (br_ioctl_hook)
904 err = br_ioctl_hook(cmd, argp);
905 up(&br_ioctl_mutex);
906 break;
907 case SIOCGIFVLAN:
908 case SIOCSIFVLAN:
909 err = -ENOPKG;
910 if (!vlan_ioctl_hook)
911 request_module("8021q");
913 down(&vlan_ioctl_mutex);
914 if (vlan_ioctl_hook)
915 err = vlan_ioctl_hook(argp);
916 up(&vlan_ioctl_mutex);
917 break;
918 case SIOCGIFDIVERT:
919 case SIOCSIFDIVERT:
920 /* Convert this to call through a hook */
921 err = divert_ioctl(cmd, argp);
922 break;
923 case SIOCADDDLCI:
924 case SIOCDELDLCI:
925 err = -ENOPKG;
926 if (!dlci_ioctl_hook)
927 request_module("dlci");
929 if (dlci_ioctl_hook) {
930 down(&dlci_ioctl_mutex);
931 err = dlci_ioctl_hook(cmd, argp);
932 up(&dlci_ioctl_mutex);
934 break;
935 default:
936 err = sock->ops->ioctl(sock, cmd, arg);
939 * If this ioctl is unknown try to hand it down
940 * to the NIC driver.
942 if (err == -ENOIOCTLCMD)
943 err = dev_ioctl(cmd, argp);
944 break;
946 return err;
949 int sock_create_lite(int family, int type, int protocol, struct socket **res)
951 int err;
952 struct socket *sock = NULL;
954 err = security_socket_create(family, type, protocol, 1);
955 if (err)
956 goto out;
958 sock = sock_alloc();
959 if (!sock) {
960 err = -ENOMEM;
961 goto out;
964 security_socket_post_create(sock, family, type, protocol, 1);
965 sock->type = type;
966 out:
967 *res = sock;
968 return err;
971 /* No kernel lock held - perfect */
972 static unsigned int sock_poll(struct file *file, poll_table * wait)
974 struct socket *sock;
977 * We can't return errors to poll, so it's either yes or no.
979 sock = file->private_data;
980 return sock->ops->poll(file, sock, wait);
983 static int sock_mmap(struct file * file, struct vm_area_struct * vma)
985 struct socket *sock = file->private_data;
987 return sock->ops->mmap(file, sock, vma);
990 static int sock_close(struct inode *inode, struct file *filp)
993 * It was possible the inode is NULL we were
994 * closing an unfinished socket.
997 if (!inode)
999 printk(KERN_DEBUG "sock_close: NULL inode\n");
1000 return 0;
1002 sock_fasync(-1, filp, 0);
1003 sock_release(SOCKET_I(inode));
1004 return 0;
1008 * Update the socket async list
1010 * Fasync_list locking strategy.
1012 * 1. fasync_list is modified only under process context socket lock
1013 * i.e. under semaphore.
1014 * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
1015 * or under socket lock.
1016 * 3. fasync_list can be used from softirq context, so that
1017 * modification under socket lock have to be enhanced with
1018 * write_lock_bh(&sk->sk_callback_lock).
1019 * --ANK (990710)
1022 static int sock_fasync(int fd, struct file *filp, int on)
1024 struct fasync_struct *fa, *fna=NULL, **prev;
1025 struct socket *sock;
1026 struct sock *sk;
1028 if (on)
1030 fna = kmalloc(sizeof(struct fasync_struct), GFP_KERNEL);
1031 if(fna==NULL)
1032 return -ENOMEM;
1035 sock = filp->private_data;
1037 if ((sk=sock->sk) == NULL) {
1038 kfree(fna);
1039 return -EINVAL;
1042 lock_sock(sk);
1044 prev=&(sock->fasync_list);
1046 for (fa=*prev; fa!=NULL; prev=&fa->fa_next,fa=*prev)
1047 if (fa->fa_file==filp)
1048 break;
1050 if(on)
1052 if(fa!=NULL)
1054 write_lock_bh(&sk->sk_callback_lock);
1055 fa->fa_fd=fd;
1056 write_unlock_bh(&sk->sk_callback_lock);
1058 kfree(fna);
1059 goto out;
1061 fna->fa_file=filp;
1062 fna->fa_fd=fd;
1063 fna->magic=FASYNC_MAGIC;
1064 fna->fa_next=sock->fasync_list;
1065 write_lock_bh(&sk->sk_callback_lock);
1066 sock->fasync_list=fna;
1067 write_unlock_bh(&sk->sk_callback_lock);
1069 else
1071 if (fa!=NULL)
1073 write_lock_bh(&sk->sk_callback_lock);
1074 *prev=fa->fa_next;
1075 write_unlock_bh(&sk->sk_callback_lock);
1076 kfree(fa);
1080 out:
1081 release_sock(sock->sk);
1082 return 0;
1085 /* This function may be called only under socket lock or callback_lock */
1087 int sock_wake_async(struct socket *sock, int how, int band)
1089 if (!sock || !sock->fasync_list)
1090 return -1;
1091 switch (how)
1093 case 1:
1095 if (test_bit(SOCK_ASYNC_WAITDATA, &sock->flags))
1096 break;
1097 goto call_kill;
1098 case 2:
1099 if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags))
1100 break;
1101 /* fall through */
1102 case 0:
1103 call_kill:
1104 __kill_fasync(sock->fasync_list, SIGIO, band);
1105 break;
1106 case 3:
1107 __kill_fasync(sock->fasync_list, SIGURG, band);
1109 return 0;
1112 static int __sock_create(int family, int type, int protocol, struct socket **res, int kern)
1114 int err;
1115 struct socket *sock;
1118 * Check protocol is in range
1120 if (family < 0 || family >= NPROTO)
1121 return -EAFNOSUPPORT;
1122 if (type < 0 || type >= SOCK_MAX)
1123 return -EINVAL;
1125 /* Compatibility.
1127 This uglymoron is moved from INET layer to here to avoid
1128 deadlock in module load.
1130 if (family == PF_INET && type == SOCK_PACKET) {
1131 static int warned;
1132 if (!warned) {
1133 warned = 1;
1134 printk(KERN_INFO "%s uses obsolete (PF_INET,SOCK_PACKET)\n", current->comm);
1136 family = PF_PACKET;
1139 err = security_socket_create(family, type, protocol, kern);
1140 if (err)
1141 return err;
1143 #if defined(CONFIG_KMOD)
1144 /* Attempt to load a protocol module if the find failed.
1146 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
1147 * requested real, full-featured networking support upon configuration.
1148 * Otherwise module support will break!
1150 if (net_families[family]==NULL)
1152 request_module("net-pf-%d",family);
1154 #endif
1156 net_family_read_lock();
1157 if (net_families[family] == NULL) {
1158 err = -EAFNOSUPPORT;
1159 goto out;
1163 * Allocate the socket and allow the family to set things up. if
1164 * the protocol is 0, the family is instructed to select an appropriate
1165 * default.
1168 if (!(sock = sock_alloc())) {
1169 printk(KERN_WARNING "socket: no more sockets\n");
1170 err = -ENFILE; /* Not exactly a match, but its the
1171 closest posix thing */
1172 goto out;
1175 sock->type = type;
1178 * We will call the ->create function, that possibly is in a loadable
1179 * module, so we have to bump that loadable module refcnt first.
1181 err = -EAFNOSUPPORT;
1182 if (!try_module_get(net_families[family]->owner))
1183 goto out_release;
1185 if ((err = net_families[family]->create(sock, protocol)) < 0) {
1186 sock->ops = NULL;
1187 goto out_module_put;
1191 * Now to bump the refcnt of the [loadable] module that owns this
1192 * socket at sock_release time we decrement its refcnt.
1194 if (!try_module_get(sock->ops->owner)) {
1195 sock->ops = NULL;
1196 goto out_module_put;
1199 * Now that we're done with the ->create function, the [loadable]
1200 * module can have its refcnt decremented
1202 module_put(net_families[family]->owner);
1203 *res = sock;
1204 security_socket_post_create(sock, family, type, protocol, kern);
1206 out:
1207 net_family_read_unlock();
1208 return err;
1209 out_module_put:
1210 module_put(net_families[family]->owner);
1211 out_release:
1212 sock_release(sock);
1213 goto out;
1216 int sock_create(int family, int type, int protocol, struct socket **res)
1218 return __sock_create(family, type, protocol, res, 0);
1221 int sock_create_kern(int family, int type, int protocol, struct socket **res)
1223 return __sock_create(family, type, protocol, res, 1);
1226 asmlinkage long sys_socket(int family, int type, int protocol)
1228 int retval;
1229 struct socket *sock;
1231 retval = sock_create(family, type, protocol, &sock);
1232 if (retval < 0)
1233 goto out;
1235 retval = sock_map_fd(sock);
1236 if (retval < 0)
1237 goto out_release;
1239 out:
1240 /* It may be already another descriptor 8) Not kernel problem. */
1241 return retval;
1243 out_release:
1244 sock_release(sock);
1245 return retval;
1249 * Create a pair of connected sockets.
1252 asmlinkage long sys_socketpair(int family, int type, int protocol, int __user *usockvec)
1254 struct socket *sock1, *sock2;
1255 int fd1, fd2, err;
1258 * Obtain the first socket and check if the underlying protocol
1259 * supports the socketpair call.
1262 err = sock_create(family, type, protocol, &sock1);
1263 if (err < 0)
1264 goto out;
1266 err = sock_create(family, type, protocol, &sock2);
1267 if (err < 0)
1268 goto out_release_1;
1270 err = sock1->ops->socketpair(sock1, sock2);
1271 if (err < 0)
1272 goto out_release_both;
1274 fd1 = fd2 = -1;
1276 err = sock_map_fd(sock1);
1277 if (err < 0)
1278 goto out_release_both;
1279 fd1 = err;
1281 err = sock_map_fd(sock2);
1282 if (err < 0)
1283 goto out_close_1;
1284 fd2 = err;
1286 /* fd1 and fd2 may be already another descriptors.
1287 * Not kernel problem.
1290 err = put_user(fd1, &usockvec[0]);
1291 if (!err)
1292 err = put_user(fd2, &usockvec[1]);
1293 if (!err)
1294 return 0;
1296 sys_close(fd2);
1297 sys_close(fd1);
1298 return err;
1300 out_close_1:
1301 sock_release(sock2);
1302 sys_close(fd1);
1303 return err;
1305 out_release_both:
1306 sock_release(sock2);
1307 out_release_1:
1308 sock_release(sock1);
1309 out:
1310 return err;
1315 * Bind a name to a socket. Nothing much to do here since it's
1316 * the protocol's responsibility to handle the local address.
1318 * We move the socket address to kernel space before we call
1319 * the protocol layer (having also checked the address is ok).
1322 asmlinkage long sys_bind(int fd, struct sockaddr __user *umyaddr, int addrlen)
1324 struct socket *sock;
1325 char address[MAX_SOCK_ADDR];
1326 int err, fput_needed;
1328 if((sock = sockfd_lookup_light(fd, &err, &fput_needed))!=NULL)
1330 if((err=move_addr_to_kernel(umyaddr,addrlen,address))>=0) {
1331 err = security_socket_bind(sock, (struct sockaddr *)address, addrlen);
1332 if (!err)
1333 err = sock->ops->bind(sock,
1334 (struct sockaddr *)address, addrlen);
1336 fput_light(sock->file, fput_needed);
1338 return err;
1343 * Perform a listen. Basically, we allow the protocol to do anything
1344 * necessary for a listen, and if that works, we mark the socket as
1345 * ready for listening.
1348 int sysctl_somaxconn = SOMAXCONN;
1350 asmlinkage long sys_listen(int fd, int backlog)
1352 struct socket *sock;
1353 int err, fput_needed;
1355 if ((sock = sockfd_lookup_light(fd, &err, &fput_needed)) != NULL) {
1356 if ((unsigned) backlog > sysctl_somaxconn)
1357 backlog = sysctl_somaxconn;
1359 err = security_socket_listen(sock, backlog);
1360 if (!err)
1361 err = sock->ops->listen(sock, backlog);
1363 fput_light(sock->file, fput_needed);
1365 return err;
1370 * For accept, we attempt to create a new socket, set up the link
1371 * with the client, wake up the client, then return the new
1372 * connected fd. We collect the address of the connector in kernel
1373 * space and move it to user at the very end. This is unclean because
1374 * we open the socket then return an error.
1376 * 1003.1g adds the ability to recvmsg() to query connection pending
1377 * status to recvmsg. We need to add that support in a way thats
1378 * clean when we restucture accept also.
1381 asmlinkage long sys_accept(int fd, struct sockaddr __user *upeer_sockaddr, int __user *upeer_addrlen)
1383 struct socket *sock, *newsock;
1384 struct file *newfile;
1385 int err, len, newfd, fput_needed;
1386 char address[MAX_SOCK_ADDR];
1388 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1389 if (!sock)
1390 goto out;
1392 err = -ENFILE;
1393 if (!(newsock = sock_alloc()))
1394 goto out_put;
1396 newsock->type = sock->type;
1397 newsock->ops = sock->ops;
1400 * We don't need try_module_get here, as the listening socket (sock)
1401 * has the protocol module (sock->ops->owner) held.
1403 __module_get(newsock->ops->owner);
1405 newfd = sock_alloc_fd(&newfile);
1406 if (unlikely(newfd < 0)) {
1407 err = newfd;
1408 goto out_release;
1411 err = sock_attach_fd(newsock, newfile);
1412 if (err < 0)
1413 goto out_fd;
1415 err = security_socket_accept(sock, newsock);
1416 if (err)
1417 goto out_fd;
1419 err = sock->ops->accept(sock, newsock, sock->file->f_flags);
1420 if (err < 0)
1421 goto out_fd;
1423 if (upeer_sockaddr) {
1424 if(newsock->ops->getname(newsock, (struct sockaddr *)address, &len, 2)<0) {
1425 err = -ECONNABORTED;
1426 goto out_fd;
1428 err = move_addr_to_user(address, len, upeer_sockaddr, upeer_addrlen);
1429 if (err < 0)
1430 goto out_fd;
1433 /* File flags are not inherited via accept() unlike another OSes. */
1435 fd_install(newfd, newfile);
1436 err = newfd;
1438 security_socket_post_accept(sock, newsock);
1440 out_put:
1441 fput_light(sock->file, fput_needed);
1442 out:
1443 return err;
1444 out_fd:
1445 put_filp(newfile);
1446 put_unused_fd(newfd);
1447 out_release:
1448 sock_release(newsock);
1449 goto out_put;
1454 * Attempt to connect to a socket with the server address. The address
1455 * is in user space so we verify it is OK and move it to kernel space.
1457 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
1458 * break bindings
1460 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
1461 * other SEQPACKET protocols that take time to connect() as it doesn't
1462 * include the -EINPROGRESS status for such sockets.
1465 asmlinkage long sys_connect(int fd, struct sockaddr __user *uservaddr, int addrlen)
1467 struct socket *sock;
1468 char address[MAX_SOCK_ADDR];
1469 int err, fput_needed;
1471 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1472 if (!sock)
1473 goto out;
1474 err = move_addr_to_kernel(uservaddr, addrlen, address);
1475 if (err < 0)
1476 goto out_put;
1478 err = security_socket_connect(sock, (struct sockaddr *)address, addrlen);
1479 if (err)
1480 goto out_put;
1482 err = sock->ops->connect(sock, (struct sockaddr *) address, addrlen,
1483 sock->file->f_flags);
1484 out_put:
1485 fput_light(sock->file, fput_needed);
1486 out:
1487 return err;
1491 * Get the local address ('name') of a socket object. Move the obtained
1492 * name to user space.
1495 asmlinkage long sys_getsockname(int fd, struct sockaddr __user *usockaddr, int __user *usockaddr_len)
1497 struct socket *sock;
1498 char address[MAX_SOCK_ADDR];
1499 int len, err, fput_needed;
1501 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1502 if (!sock)
1503 goto out;
1505 err = security_socket_getsockname(sock);
1506 if (err)
1507 goto out_put;
1509 err = sock->ops->getname(sock, (struct sockaddr *)address, &len, 0);
1510 if (err)
1511 goto out_put;
1512 err = move_addr_to_user(address, len, usockaddr, usockaddr_len);
1514 out_put:
1515 fput_light(sock->file, fput_needed);
1516 out:
1517 return err;
1521 * Get the remote address ('name') of a socket object. Move the obtained
1522 * name to user space.
1525 asmlinkage long sys_getpeername(int fd, struct sockaddr __user *usockaddr, int __user *usockaddr_len)
1527 struct socket *sock;
1528 char address[MAX_SOCK_ADDR];
1529 int len, err, fput_needed;
1531 if ((sock = sockfd_lookup_light(fd, &err, &fput_needed)) != NULL) {
1532 err = security_socket_getpeername(sock);
1533 if (err) {
1534 fput_light(sock->file, fput_needed);
1535 return err;
1538 err = sock->ops->getname(sock, (struct sockaddr *)address, &len, 1);
1539 if (!err)
1540 err=move_addr_to_user(address,len, usockaddr, usockaddr_len);
1541 fput_light(sock->file, fput_needed);
1543 return err;
1547 * Send a datagram to a given address. We move the address into kernel
1548 * space and check the user space data area is readable before invoking
1549 * the protocol.
1552 asmlinkage long sys_sendto(int fd, void __user * buff, size_t len, unsigned flags,
1553 struct sockaddr __user *addr, int addr_len)
1555 struct socket *sock;
1556 char address[MAX_SOCK_ADDR];
1557 int err;
1558 struct msghdr msg;
1559 struct iovec iov;
1560 int fput_needed;
1561 struct file *sock_file;
1563 sock_file = fget_light(fd, &fput_needed);
1564 if (!sock_file)
1565 return -EBADF;
1567 sock = sock_from_file(sock_file, &err);
1568 if (!sock)
1569 goto out_put;
1570 iov.iov_base=buff;
1571 iov.iov_len=len;
1572 msg.msg_name=NULL;
1573 msg.msg_iov=&iov;
1574 msg.msg_iovlen=1;
1575 msg.msg_control=NULL;
1576 msg.msg_controllen=0;
1577 msg.msg_namelen=0;
1578 if (addr) {
1579 err = move_addr_to_kernel(addr, addr_len, address);
1580 if (err < 0)
1581 goto out_put;
1582 msg.msg_name=address;
1583 msg.msg_namelen=addr_len;
1585 if (sock->file->f_flags & O_NONBLOCK)
1586 flags |= MSG_DONTWAIT;
1587 msg.msg_flags = flags;
1588 err = sock_sendmsg(sock, &msg, len);
1590 out_put:
1591 fput_light(sock_file, fput_needed);
1592 return err;
1596 * Send a datagram down a socket.
1599 asmlinkage long sys_send(int fd, void __user * buff, size_t len, unsigned flags)
1601 return sys_sendto(fd, buff, len, flags, NULL, 0);
1605 * Receive a frame from the socket and optionally record the address of the
1606 * sender. We verify the buffers are writable and if needed move the
1607 * sender address from kernel to user space.
1610 asmlinkage long sys_recvfrom(int fd, void __user * ubuf, size_t size, unsigned flags,
1611 struct sockaddr __user *addr, int __user *addr_len)
1613 struct socket *sock;
1614 struct iovec iov;
1615 struct msghdr msg;
1616 char address[MAX_SOCK_ADDR];
1617 int err,err2;
1618 struct file *sock_file;
1619 int fput_needed;
1621 sock_file = fget_light(fd, &fput_needed);
1622 if (!sock_file)
1623 return -EBADF;
1625 sock = sock_from_file(sock_file, &err);
1626 if (!sock)
1627 goto out;
1629 msg.msg_control=NULL;
1630 msg.msg_controllen=0;
1631 msg.msg_iovlen=1;
1632 msg.msg_iov=&iov;
1633 iov.iov_len=size;
1634 iov.iov_base=ubuf;
1635 msg.msg_name=address;
1636 msg.msg_namelen=MAX_SOCK_ADDR;
1637 if (sock->file->f_flags & O_NONBLOCK)
1638 flags |= MSG_DONTWAIT;
1639 err=sock_recvmsg(sock, &msg, size, flags);
1641 if(err >= 0 && addr != NULL)
1643 err2=move_addr_to_user(address, msg.msg_namelen, addr, addr_len);
1644 if(err2<0)
1645 err=err2;
1647 out:
1648 fput_light(sock_file, fput_needed);
1649 return err;
1653 * Receive a datagram from a socket.
1656 asmlinkage long sys_recv(int fd, void __user * ubuf, size_t size, unsigned flags)
1658 return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
1662 * Set a socket option. Because we don't know the option lengths we have
1663 * to pass the user mode parameter for the protocols to sort out.
1666 asmlinkage long sys_setsockopt(int fd, int level, int optname, char __user *optval, int optlen)
1668 int err, fput_needed;
1669 struct socket *sock;
1671 if (optlen < 0)
1672 return -EINVAL;
1674 if ((sock = sockfd_lookup_light(fd, &err, &fput_needed)) != NULL)
1676 err = security_socket_setsockopt(sock,level,optname);
1677 if (err)
1678 goto out_put;
1680 if (level == SOL_SOCKET)
1681 err=sock_setsockopt(sock,level,optname,optval,optlen);
1682 else
1683 err=sock->ops->setsockopt(sock, level, optname, optval, optlen);
1684 out_put:
1685 fput_light(sock->file, fput_needed);
1687 return err;
1691 * Get a socket option. Because we don't know the option lengths we have
1692 * to pass a user mode parameter for the protocols to sort out.
1695 asmlinkage long sys_getsockopt(int fd, int level, int optname, char __user *optval, int __user *optlen)
1697 int err, fput_needed;
1698 struct socket *sock;
1700 if ((sock = sockfd_lookup_light(fd, &err, &fput_needed)) != NULL) {
1701 err = security_socket_getsockopt(sock, level, optname);
1702 if (err)
1703 goto out_put;
1705 if (level == SOL_SOCKET)
1706 err=sock_getsockopt(sock,level,optname,optval,optlen);
1707 else
1708 err=sock->ops->getsockopt(sock, level, optname, optval, optlen);
1709 out_put:
1710 fput_light(sock->file, fput_needed);
1712 return err;
1717 * Shutdown a socket.
1720 asmlinkage long sys_shutdown(int fd, int how)
1722 int err, fput_needed;
1723 struct socket *sock;
1725 if ((sock = sockfd_lookup_light(fd, &err, &fput_needed))!=NULL)
1727 err = security_socket_shutdown(sock, how);
1728 if (!err)
1729 err = sock->ops->shutdown(sock, how);
1730 fput_light(sock->file, fput_needed);
1732 return err;
1735 /* A couple of helpful macros for getting the address of the 32/64 bit
1736 * fields which are the same type (int / unsigned) on our platforms.
1738 #define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
1739 #define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
1740 #define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
1744 * BSD sendmsg interface
1747 asmlinkage long sys_sendmsg(int fd, struct msghdr __user *msg, unsigned flags)
1749 struct compat_msghdr __user *msg_compat = (struct compat_msghdr __user *)msg;
1750 struct socket *sock;
1751 char address[MAX_SOCK_ADDR];
1752 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
1753 unsigned char ctl[sizeof(struct cmsghdr) + 20]
1754 __attribute__ ((aligned (sizeof(__kernel_size_t))));
1755 /* 20 is size of ipv6_pktinfo */
1756 unsigned char *ctl_buf = ctl;
1757 struct msghdr msg_sys;
1758 int err, ctl_len, iov_size, total_len;
1759 int fput_needed;
1761 err = -EFAULT;
1762 if (MSG_CMSG_COMPAT & flags) {
1763 if (get_compat_msghdr(&msg_sys, msg_compat))
1764 return -EFAULT;
1765 } else if (copy_from_user(&msg_sys, msg, sizeof(struct msghdr)))
1766 return -EFAULT;
1768 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1769 if (!sock)
1770 goto out;
1772 /* do not move before msg_sys is valid */
1773 err = -EMSGSIZE;
1774 if (msg_sys.msg_iovlen > UIO_MAXIOV)
1775 goto out_put;
1777 /* Check whether to allocate the iovec area*/
1778 err = -ENOMEM;
1779 iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
1780 if (msg_sys.msg_iovlen > UIO_FASTIOV) {
1781 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
1782 if (!iov)
1783 goto out_put;
1786 /* This will also move the address data into kernel space */
1787 if (MSG_CMSG_COMPAT & flags) {
1788 err = verify_compat_iovec(&msg_sys, iov, address, VERIFY_READ);
1789 } else
1790 err = verify_iovec(&msg_sys, iov, address, VERIFY_READ);
1791 if (err < 0)
1792 goto out_freeiov;
1793 total_len = err;
1795 err = -ENOBUFS;
1797 if (msg_sys.msg_controllen > INT_MAX)
1798 goto out_freeiov;
1799 ctl_len = msg_sys.msg_controllen;
1800 if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
1801 err = cmsghdr_from_user_compat_to_kern(&msg_sys, sock->sk, ctl, sizeof(ctl));
1802 if (err)
1803 goto out_freeiov;
1804 ctl_buf = msg_sys.msg_control;
1805 ctl_len = msg_sys.msg_controllen;
1806 } else if (ctl_len) {
1807 if (ctl_len > sizeof(ctl))
1809 ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
1810 if (ctl_buf == NULL)
1811 goto out_freeiov;
1813 err = -EFAULT;
1815 * Careful! Before this, msg_sys.msg_control contains a user pointer.
1816 * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
1817 * checking falls down on this.
1819 if (copy_from_user(ctl_buf, (void __user *) msg_sys.msg_control, ctl_len))
1820 goto out_freectl;
1821 msg_sys.msg_control = ctl_buf;
1823 msg_sys.msg_flags = flags;
1825 if (sock->file->f_flags & O_NONBLOCK)
1826 msg_sys.msg_flags |= MSG_DONTWAIT;
1827 err = sock_sendmsg(sock, &msg_sys, total_len);
1829 out_freectl:
1830 if (ctl_buf != ctl)
1831 sock_kfree_s(sock->sk, ctl_buf, ctl_len);
1832 out_freeiov:
1833 if (iov != iovstack)
1834 sock_kfree_s(sock->sk, iov, iov_size);
1835 out_put:
1836 fput_light(sock->file, fput_needed);
1837 out:
1838 return err;
1842 * BSD recvmsg interface
1845 asmlinkage long sys_recvmsg(int fd, struct msghdr __user *msg, unsigned int flags)
1847 struct compat_msghdr __user *msg_compat = (struct compat_msghdr __user *)msg;
1848 struct socket *sock;
1849 struct iovec iovstack[UIO_FASTIOV];
1850 struct iovec *iov=iovstack;
1851 struct msghdr msg_sys;
1852 unsigned long cmsg_ptr;
1853 int err, iov_size, total_len, len;
1854 int fput_needed;
1856 /* kernel mode address */
1857 char addr[MAX_SOCK_ADDR];
1859 /* user mode address pointers */
1860 struct sockaddr __user *uaddr;
1861 int __user *uaddr_len;
1863 if (MSG_CMSG_COMPAT & flags) {
1864 if (get_compat_msghdr(&msg_sys, msg_compat))
1865 return -EFAULT;
1866 } else
1867 if (copy_from_user(&msg_sys,msg,sizeof(struct msghdr)))
1868 return -EFAULT;
1870 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1871 if (!sock)
1872 goto out;
1874 err = -EMSGSIZE;
1875 if (msg_sys.msg_iovlen > UIO_MAXIOV)
1876 goto out_put;
1878 /* Check whether to allocate the iovec area*/
1879 err = -ENOMEM;
1880 iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
1881 if (msg_sys.msg_iovlen > UIO_FASTIOV) {
1882 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
1883 if (!iov)
1884 goto out_put;
1888 * Save the user-mode address (verify_iovec will change the
1889 * kernel msghdr to use the kernel address space)
1892 uaddr = (void __user *) msg_sys.msg_name;
1893 uaddr_len = COMPAT_NAMELEN(msg);
1894 if (MSG_CMSG_COMPAT & flags) {
1895 err = verify_compat_iovec(&msg_sys, iov, addr, VERIFY_WRITE);
1896 } else
1897 err = verify_iovec(&msg_sys, iov, addr, VERIFY_WRITE);
1898 if (err < 0)
1899 goto out_freeiov;
1900 total_len=err;
1902 cmsg_ptr = (unsigned long)msg_sys.msg_control;
1903 msg_sys.msg_flags = 0;
1904 if (MSG_CMSG_COMPAT & flags)
1905 msg_sys.msg_flags = MSG_CMSG_COMPAT;
1907 if (sock->file->f_flags & O_NONBLOCK)
1908 flags |= MSG_DONTWAIT;
1909 err = sock_recvmsg(sock, &msg_sys, total_len, flags);
1910 if (err < 0)
1911 goto out_freeiov;
1912 len = err;
1914 if (uaddr != NULL) {
1915 err = move_addr_to_user(addr, msg_sys.msg_namelen, uaddr, uaddr_len);
1916 if (err < 0)
1917 goto out_freeiov;
1919 err = __put_user((msg_sys.msg_flags & ~MSG_CMSG_COMPAT),
1920 COMPAT_FLAGS(msg));
1921 if (err)
1922 goto out_freeiov;
1923 if (MSG_CMSG_COMPAT & flags)
1924 err = __put_user((unsigned long)msg_sys.msg_control-cmsg_ptr,
1925 &msg_compat->msg_controllen);
1926 else
1927 err = __put_user((unsigned long)msg_sys.msg_control-cmsg_ptr,
1928 &msg->msg_controllen);
1929 if (err)
1930 goto out_freeiov;
1931 err = len;
1933 out_freeiov:
1934 if (iov != iovstack)
1935 sock_kfree_s(sock->sk, iov, iov_size);
1936 out_put:
1937 fput_light(sock->file, fput_needed);
1938 out:
1939 return err;
1942 #ifdef __ARCH_WANT_SYS_SOCKETCALL
1944 /* Argument list sizes for sys_socketcall */
1945 #define AL(x) ((x) * sizeof(unsigned long))
1946 static unsigned char nargs[18]={AL(0),AL(3),AL(3),AL(3),AL(2),AL(3),
1947 AL(3),AL(3),AL(4),AL(4),AL(4),AL(6),
1948 AL(6),AL(2),AL(5),AL(5),AL(3),AL(3)};
1949 #undef AL
1952 * System call vectors.
1954 * Argument checking cleaned up. Saved 20% in size.
1955 * This function doesn't need to set the kernel lock because
1956 * it is set by the callees.
1959 asmlinkage long sys_socketcall(int call, unsigned long __user *args)
1961 unsigned long a[6];
1962 unsigned long a0,a1;
1963 int err;
1965 if(call<1||call>SYS_RECVMSG)
1966 return -EINVAL;
1968 /* copy_from_user should be SMP safe. */
1969 if (copy_from_user(a, args, nargs[call]))
1970 return -EFAULT;
1972 err = audit_socketcall(nargs[call]/sizeof(unsigned long), a);
1973 if (err)
1974 return err;
1976 a0=a[0];
1977 a1=a[1];
1979 switch(call)
1981 case SYS_SOCKET:
1982 err = sys_socket(a0,a1,a[2]);
1983 break;
1984 case SYS_BIND:
1985 err = sys_bind(a0,(struct sockaddr __user *)a1, a[2]);
1986 break;
1987 case SYS_CONNECT:
1988 err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
1989 break;
1990 case SYS_LISTEN:
1991 err = sys_listen(a0,a1);
1992 break;
1993 case SYS_ACCEPT:
1994 err = sys_accept(a0,(struct sockaddr __user *)a1, (int __user *)a[2]);
1995 break;
1996 case SYS_GETSOCKNAME:
1997 err = sys_getsockname(a0,(struct sockaddr __user *)a1, (int __user *)a[2]);
1998 break;
1999 case SYS_GETPEERNAME:
2000 err = sys_getpeername(a0, (struct sockaddr __user *)a1, (int __user *)a[2]);
2001 break;
2002 case SYS_SOCKETPAIR:
2003 err = sys_socketpair(a0,a1, a[2], (int __user *)a[3]);
2004 break;
2005 case SYS_SEND:
2006 err = sys_send(a0, (void __user *)a1, a[2], a[3]);
2007 break;
2008 case SYS_SENDTO:
2009 err = sys_sendto(a0,(void __user *)a1, a[2], a[3],
2010 (struct sockaddr __user *)a[4], a[5]);
2011 break;
2012 case SYS_RECV:
2013 err = sys_recv(a0, (void __user *)a1, a[2], a[3]);
2014 break;
2015 case SYS_RECVFROM:
2016 err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
2017 (struct sockaddr __user *)a[4], (int __user *)a[5]);
2018 break;
2019 case SYS_SHUTDOWN:
2020 err = sys_shutdown(a0,a1);
2021 break;
2022 case SYS_SETSOCKOPT:
2023 err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]);
2024 break;
2025 case SYS_GETSOCKOPT:
2026 err = sys_getsockopt(a0, a1, a[2], (char __user *)a[3], (int __user *)a[4]);
2027 break;
2028 case SYS_SENDMSG:
2029 err = sys_sendmsg(a0, (struct msghdr __user *) a1, a[2]);
2030 break;
2031 case SYS_RECVMSG:
2032 err = sys_recvmsg(a0, (struct msghdr __user *) a1, a[2]);
2033 break;
2034 default:
2035 err = -EINVAL;
2036 break;
2038 return err;
2041 #endif /* __ARCH_WANT_SYS_SOCKETCALL */
2044 * This function is called by a protocol handler that wants to
2045 * advertise its address family, and have it linked into the
2046 * SOCKET module.
2049 int sock_register(struct net_proto_family *ops)
2051 int err;
2053 if (ops->family >= NPROTO) {
2054 printk(KERN_CRIT "protocol %d >= NPROTO(%d)\n", ops->family, NPROTO);
2055 return -ENOBUFS;
2057 net_family_write_lock();
2058 err = -EEXIST;
2059 if (net_families[ops->family] == NULL) {
2060 net_families[ops->family]=ops;
2061 err = 0;
2063 net_family_write_unlock();
2064 printk(KERN_INFO "NET: Registered protocol family %d\n",
2065 ops->family);
2066 return err;
2070 * This function is called by a protocol handler that wants to
2071 * remove its address family, and have it unlinked from the
2072 * SOCKET module.
2075 int sock_unregister(int family)
2077 if (family < 0 || family >= NPROTO)
2078 return -1;
2080 net_family_write_lock();
2081 net_families[family]=NULL;
2082 net_family_write_unlock();
2083 printk(KERN_INFO "NET: Unregistered protocol family %d\n",
2084 family);
2085 return 0;
2088 static int __init sock_init(void)
2091 * Initialize sock SLAB cache.
2094 sk_init();
2097 * Initialize skbuff SLAB cache
2099 skb_init();
2102 * Initialize the protocols module.
2105 init_inodecache();
2106 register_filesystem(&sock_fs_type);
2107 sock_mnt = kern_mount(&sock_fs_type);
2109 /* The real protocol initialization is performed in later initcalls.
2112 #ifdef CONFIG_NETFILTER
2113 netfilter_init();
2114 #endif
2116 return 0;
2119 core_initcall(sock_init); /* early initcall */
2121 #ifdef CONFIG_PROC_FS
2122 void socket_seq_show(struct seq_file *seq)
2124 int cpu;
2125 int counter = 0;
2127 for_each_cpu(cpu)
2128 counter += per_cpu(sockets_in_use, cpu);
2130 /* It can be negative, by the way. 8) */
2131 if (counter < 0)
2132 counter = 0;
2134 seq_printf(seq, "sockets: used %d\n", counter);
2136 #endif /* CONFIG_PROC_FS */
2138 /* ABI emulation layers need these two */
2139 EXPORT_SYMBOL(move_addr_to_kernel);
2140 EXPORT_SYMBOL(move_addr_to_user);
2141 EXPORT_SYMBOL(sock_create);
2142 EXPORT_SYMBOL(sock_create_kern);
2143 EXPORT_SYMBOL(sock_create_lite);
2144 EXPORT_SYMBOL(sock_map_fd);
2145 EXPORT_SYMBOL(sock_recvmsg);
2146 EXPORT_SYMBOL(sock_register);
2147 EXPORT_SYMBOL(sock_release);
2148 EXPORT_SYMBOL(sock_sendmsg);
2149 EXPORT_SYMBOL(sock_unregister);
2150 EXPORT_SYMBOL(sock_wake_async);
2151 EXPORT_SYMBOL(sockfd_lookup);
2152 EXPORT_SYMBOL(kernel_sendmsg);
2153 EXPORT_SYMBOL(kernel_recvmsg);