ARM: OMAP: Remove IVA IRQ bank
[linux-2.6/openmoko-kernel.git] / net / socket.c
blob1bc4167e0da8da20c2364c14ea48b73a0cca28df
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] __read_mostly;
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;
258 return &ei->vfs_inode;
261 static void sock_destroy_inode(struct inode *inode)
263 kmem_cache_free(sock_inode_cachep,
264 container_of(inode, struct socket_alloc, vfs_inode));
267 static void init_once(void *foo, kmem_cache_t *cachep, unsigned long flags)
269 struct socket_alloc *ei = (struct socket_alloc *)foo;
271 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR))
272 == SLAB_CTOR_CONSTRUCTOR)
273 inode_init_once(&ei->vfs_inode);
276 static int init_inodecache(void)
278 sock_inode_cachep = kmem_cache_create("sock_inode_cache",
279 sizeof(struct socket_alloc),
281 (SLAB_HWCACHE_ALIGN |
282 SLAB_RECLAIM_ACCOUNT |
283 SLAB_MEM_SPREAD),
284 init_once,
285 NULL);
286 if (sock_inode_cachep == NULL)
287 return -ENOMEM;
288 return 0;
291 static struct super_operations sockfs_ops = {
292 .alloc_inode = sock_alloc_inode,
293 .destroy_inode =sock_destroy_inode,
294 .statfs = simple_statfs,
297 static int sockfs_get_sb(struct file_system_type *fs_type,
298 int flags, const char *dev_name, void *data,
299 struct vfsmount *mnt)
301 return get_sb_pseudo(fs_type, "socket:", &sockfs_ops, SOCKFS_MAGIC,
302 mnt);
305 static struct vfsmount *sock_mnt __read_mostly;
307 static struct file_system_type sock_fs_type = {
308 .name = "sockfs",
309 .get_sb = sockfs_get_sb,
310 .kill_sb = kill_anon_super,
313 static int sockfs_delete_dentry(struct dentry *dentry)
315 return 1;
317 static struct dentry_operations sockfs_dentry_operations = {
318 .d_delete = sockfs_delete_dentry,
322 * Obtains the first available file descriptor and sets it up for use.
324 * These functions create file structures and maps them to fd space
325 * of the current process. On success it returns file descriptor
326 * and file struct implicitly stored in sock->file.
327 * Note that another thread may close file descriptor before we return
328 * from this function. We use the fact that now we do not refer
329 * to socket after mapping. If one day we will need it, this
330 * function will increment ref. count on file by 1.
332 * In any case returned fd MAY BE not valid!
333 * This race condition is unavoidable
334 * with shared fd spaces, we cannot solve it inside kernel,
335 * but we take care of internal coherence yet.
338 static int sock_alloc_fd(struct file **filep)
340 int fd;
342 fd = get_unused_fd();
343 if (likely(fd >= 0)) {
344 struct file *file = get_empty_filp();
346 *filep = file;
347 if (unlikely(!file)) {
348 put_unused_fd(fd);
349 return -ENFILE;
351 } else
352 *filep = NULL;
353 return fd;
356 static int sock_attach_fd(struct socket *sock, struct file *file)
358 struct qstr this;
359 char name[32];
361 this.len = sprintf(name, "[%lu]", SOCK_INODE(sock)->i_ino);
362 this.name = name;
363 this.hash = SOCK_INODE(sock)->i_ino;
365 file->f_dentry = d_alloc(sock_mnt->mnt_sb->s_root, &this);
366 if (unlikely(!file->f_dentry))
367 return -ENOMEM;
369 file->f_dentry->d_op = &sockfs_dentry_operations;
370 d_add(file->f_dentry, SOCK_INODE(sock));
371 file->f_vfsmnt = mntget(sock_mnt);
372 file->f_mapping = file->f_dentry->d_inode->i_mapping;
374 sock->file = file;
375 file->f_op = SOCK_INODE(sock)->i_fop = &socket_file_ops;
376 file->f_mode = FMODE_READ | FMODE_WRITE;
377 file->f_flags = O_RDWR;
378 file->f_pos = 0;
379 file->private_data = sock;
381 return 0;
384 int sock_map_fd(struct socket *sock)
386 struct file *newfile;
387 int fd = sock_alloc_fd(&newfile);
389 if (likely(fd >= 0)) {
390 int err = sock_attach_fd(sock, newfile);
392 if (unlikely(err < 0)) {
393 put_filp(newfile);
394 put_unused_fd(fd);
395 return err;
397 fd_install(fd, newfile);
399 return fd;
402 static struct socket *sock_from_file(struct file *file, int *err)
404 struct inode *inode;
405 struct socket *sock;
407 if (file->f_op == &socket_file_ops)
408 return file->private_data; /* set in sock_map_fd */
410 inode = file->f_dentry->d_inode;
411 if (!S_ISSOCK(inode->i_mode)) {
412 *err = -ENOTSOCK;
413 return NULL;
416 sock = SOCKET_I(inode);
417 if (sock->file != file) {
418 printk(KERN_ERR "socki_lookup: socket file changed!\n");
419 sock->file = file;
421 return sock;
425 * sockfd_lookup - Go from a file number to its socket slot
426 * @fd: file handle
427 * @err: pointer to an error code return
429 * The file handle passed in is locked and the socket it is bound
430 * too is returned. If an error occurs the err pointer is overwritten
431 * with a negative errno code and NULL is returned. The function checks
432 * for both invalid handles and passing a handle which is not a socket.
434 * On a success the socket object pointer is returned.
437 struct socket *sockfd_lookup(int fd, int *err)
439 struct file *file;
440 struct socket *sock;
442 file = fget(fd);
443 if (!file) {
444 *err = -EBADF;
445 return NULL;
448 sock = sock_from_file(file, err);
449 if (!sock)
450 fput(file);
451 return sock;
454 static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed)
456 struct file *file;
457 struct socket *sock;
459 *err = -EBADF;
460 file = fget_light(fd, fput_needed);
461 if (file) {
462 sock = sock_from_file(file, err);
463 if (sock)
464 return sock;
465 fput_light(file, *fput_needed);
467 return NULL;
471 * sock_alloc - allocate a socket
473 * Allocate a new inode and socket object. The two are bound together
474 * and initialised. The socket is then returned. If we are out of inodes
475 * NULL is returned.
478 static struct socket *sock_alloc(void)
480 struct inode *inode;
481 struct socket *sock;
483 inode = new_inode(sock_mnt->mnt_sb);
484 if (!inode)
485 return NULL;
487 sock = SOCKET_I(inode);
489 inode->i_mode = S_IFSOCK | S_IRWXUGO;
490 inode->i_uid = current->fsuid;
491 inode->i_gid = current->fsgid;
493 get_cpu_var(sockets_in_use)++;
494 put_cpu_var(sockets_in_use);
495 return sock;
499 * In theory you can't get an open on this inode, but /proc provides
500 * a back door. Remember to keep it shut otherwise you'll let the
501 * creepy crawlies in.
504 static int sock_no_open(struct inode *irrelevant, struct file *dontcare)
506 return -ENXIO;
509 const struct file_operations bad_sock_fops = {
510 .owner = THIS_MODULE,
511 .open = sock_no_open,
515 * sock_release - close a socket
516 * @sock: socket to close
518 * The socket is released from the protocol stack if it has a release
519 * callback, and the inode is then released if the socket is bound to
520 * an inode not a file.
523 void sock_release(struct socket *sock)
525 if (sock->ops) {
526 struct module *owner = sock->ops->owner;
528 sock->ops->release(sock);
529 sock->ops = NULL;
530 module_put(owner);
533 if (sock->fasync_list)
534 printk(KERN_ERR "sock_release: fasync list not empty!\n");
536 get_cpu_var(sockets_in_use)--;
537 put_cpu_var(sockets_in_use);
538 if (!sock->file) {
539 iput(SOCK_INODE(sock));
540 return;
542 sock->file = NULL;
545 static inline int __sock_sendmsg(struct kiocb *iocb, struct socket *sock,
546 struct msghdr *msg, size_t size)
548 struct sock_iocb *si = kiocb_to_siocb(iocb);
549 int err;
551 si->sock = sock;
552 si->scm = NULL;
553 si->msg = msg;
554 si->size = size;
556 err = security_socket_sendmsg(sock, msg, size);
557 if (err)
558 return err;
560 return sock->ops->sendmsg(iocb, sock, msg, size);
563 int sock_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
565 struct kiocb iocb;
566 struct sock_iocb siocb;
567 int ret;
569 init_sync_kiocb(&iocb, NULL);
570 iocb.private = &siocb;
571 ret = __sock_sendmsg(&iocb, sock, msg, size);
572 if (-EIOCBQUEUED == ret)
573 ret = wait_on_sync_kiocb(&iocb);
574 return ret;
577 int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
578 struct kvec *vec, size_t num, size_t size)
580 mm_segment_t oldfs = get_fs();
581 int result;
583 set_fs(KERNEL_DS);
585 * the following is safe, since for compiler definitions of kvec and
586 * iovec are identical, yielding the same in-core layout and alignment
588 msg->msg_iov = (struct iovec *)vec;
589 msg->msg_iovlen = num;
590 result = sock_sendmsg(sock, msg, size);
591 set_fs(oldfs);
592 return result;
595 static inline int __sock_recvmsg(struct kiocb *iocb, struct socket *sock,
596 struct msghdr *msg, size_t size, int flags)
598 int err;
599 struct sock_iocb *si = kiocb_to_siocb(iocb);
601 si->sock = sock;
602 si->scm = NULL;
603 si->msg = msg;
604 si->size = size;
605 si->flags = flags;
607 err = security_socket_recvmsg(sock, msg, size, flags);
608 if (err)
609 return err;
611 return sock->ops->recvmsg(iocb, sock, msg, size, flags);
614 int sock_recvmsg(struct socket *sock, struct msghdr *msg,
615 size_t size, int flags)
617 struct kiocb iocb;
618 struct sock_iocb siocb;
619 int ret;
621 init_sync_kiocb(&iocb, NULL);
622 iocb.private = &siocb;
623 ret = __sock_recvmsg(&iocb, sock, msg, size, flags);
624 if (-EIOCBQUEUED == ret)
625 ret = wait_on_sync_kiocb(&iocb);
626 return ret;
629 int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
630 struct kvec *vec, size_t num, size_t size, int flags)
632 mm_segment_t oldfs = get_fs();
633 int result;
635 set_fs(KERNEL_DS);
637 * the following is safe, since for compiler definitions of kvec and
638 * iovec are identical, yielding the same in-core layout and alignment
640 msg->msg_iov = (struct iovec *)vec, msg->msg_iovlen = num;
641 result = sock_recvmsg(sock, msg, size, flags);
642 set_fs(oldfs);
643 return result;
646 static void sock_aio_dtor(struct kiocb *iocb)
648 kfree(iocb->private);
651 static ssize_t sock_sendpage(struct file *file, struct page *page,
652 int offset, size_t size, loff_t *ppos, int more)
654 struct socket *sock;
655 int flags;
657 sock = file->private_data;
659 flags = !(file->f_flags & O_NONBLOCK) ? 0 : MSG_DONTWAIT;
660 if (more)
661 flags |= MSG_MORE;
663 return sock->ops->sendpage(sock, page, offset, size, flags);
666 static struct sock_iocb *alloc_sock_iocb(struct kiocb *iocb,
667 char __user *ubuf, size_t size,
668 struct sock_iocb *siocb)
670 if (!is_sync_kiocb(iocb)) {
671 siocb = kmalloc(sizeof(*siocb), GFP_KERNEL);
672 if (!siocb)
673 return NULL;
674 iocb->ki_dtor = sock_aio_dtor;
677 siocb->kiocb = iocb;
678 siocb->async_iov.iov_base = ubuf;
679 siocb->async_iov.iov_len = size;
681 iocb->private = siocb;
682 return siocb;
685 static ssize_t do_sock_read(struct msghdr *msg, struct kiocb *iocb,
686 struct file *file, struct iovec *iov,
687 unsigned long nr_segs)
689 struct socket *sock = file->private_data;
690 size_t size = 0;
691 int i;
693 for (i = 0; i < nr_segs; i++)
694 size += iov[i].iov_len;
696 msg->msg_name = NULL;
697 msg->msg_namelen = 0;
698 msg->msg_control = NULL;
699 msg->msg_controllen = 0;
700 msg->msg_iov = (struct iovec *)iov;
701 msg->msg_iovlen = nr_segs;
702 msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
704 return __sock_recvmsg(iocb, sock, msg, size, msg->msg_flags);
707 static ssize_t sock_readv(struct file *file, const struct iovec *iov,
708 unsigned long nr_segs, loff_t *ppos)
710 struct kiocb iocb;
711 struct sock_iocb siocb;
712 struct msghdr msg;
713 int ret;
715 init_sync_kiocb(&iocb, NULL);
716 iocb.private = &siocb;
718 ret = do_sock_read(&msg, &iocb, file, (struct iovec *)iov, nr_segs);
719 if (-EIOCBQUEUED == ret)
720 ret = wait_on_sync_kiocb(&iocb);
721 return ret;
724 static ssize_t sock_aio_read(struct kiocb *iocb, char __user *ubuf,
725 size_t count, loff_t pos)
727 struct sock_iocb siocb, *x;
729 if (pos != 0)
730 return -ESPIPE;
731 if (count == 0) /* Match SYS5 behaviour */
732 return 0;
734 x = alloc_sock_iocb(iocb, ubuf, count, &siocb);
735 if (!x)
736 return -ENOMEM;
737 return do_sock_read(&x->async_msg, iocb, iocb->ki_filp,
738 &x->async_iov, 1);
741 static ssize_t do_sock_write(struct msghdr *msg, struct kiocb *iocb,
742 struct file *file, struct iovec *iov,
743 unsigned long nr_segs)
745 struct socket *sock = file->private_data;
746 size_t size = 0;
747 int i;
749 for (i = 0; i < nr_segs; i++)
750 size += iov[i].iov_len;
752 msg->msg_name = NULL;
753 msg->msg_namelen = 0;
754 msg->msg_control = NULL;
755 msg->msg_controllen = 0;
756 msg->msg_iov = (struct iovec *)iov;
757 msg->msg_iovlen = nr_segs;
758 msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
759 if (sock->type == SOCK_SEQPACKET)
760 msg->msg_flags |= MSG_EOR;
762 return __sock_sendmsg(iocb, sock, msg, size);
765 static ssize_t sock_writev(struct file *file, const struct iovec *iov,
766 unsigned long nr_segs, loff_t *ppos)
768 struct msghdr msg;
769 struct kiocb iocb;
770 struct sock_iocb siocb;
771 int ret;
773 init_sync_kiocb(&iocb, NULL);
774 iocb.private = &siocb;
776 ret = do_sock_write(&msg, &iocb, file, (struct iovec *)iov, nr_segs);
777 if (-EIOCBQUEUED == ret)
778 ret = wait_on_sync_kiocb(&iocb);
779 return ret;
782 static ssize_t sock_aio_write(struct kiocb *iocb, const char __user *ubuf,
783 size_t count, loff_t pos)
785 struct sock_iocb siocb, *x;
787 if (pos != 0)
788 return -ESPIPE;
789 if (count == 0) /* Match SYS5 behaviour */
790 return 0;
792 x = alloc_sock_iocb(iocb, (void __user *)ubuf, count, &siocb);
793 if (!x)
794 return -ENOMEM;
796 return do_sock_write(&x->async_msg, iocb, iocb->ki_filp,
797 &x->async_iov, 1);
801 * Atomic setting of ioctl hooks to avoid race
802 * with module unload.
805 static DEFINE_MUTEX(br_ioctl_mutex);
806 static int (*br_ioctl_hook) (unsigned int cmd, void __user *arg) = NULL;
808 void brioctl_set(int (*hook) (unsigned int, void __user *))
810 mutex_lock(&br_ioctl_mutex);
811 br_ioctl_hook = hook;
812 mutex_unlock(&br_ioctl_mutex);
815 EXPORT_SYMBOL(brioctl_set);
817 static DEFINE_MUTEX(vlan_ioctl_mutex);
818 static int (*vlan_ioctl_hook) (void __user *arg);
820 void vlan_ioctl_set(int (*hook) (void __user *))
822 mutex_lock(&vlan_ioctl_mutex);
823 vlan_ioctl_hook = hook;
824 mutex_unlock(&vlan_ioctl_mutex);
827 EXPORT_SYMBOL(vlan_ioctl_set);
829 static DEFINE_MUTEX(dlci_ioctl_mutex);
830 static int (*dlci_ioctl_hook) (unsigned int, void __user *);
832 void dlci_ioctl_set(int (*hook) (unsigned int, void __user *))
834 mutex_lock(&dlci_ioctl_mutex);
835 dlci_ioctl_hook = hook;
836 mutex_unlock(&dlci_ioctl_mutex);
839 EXPORT_SYMBOL(dlci_ioctl_set);
842 * With an ioctl, arg may well be a user mode pointer, but we don't know
843 * what to do with it - that's up to the protocol still.
846 static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
848 struct socket *sock;
849 void __user *argp = (void __user *)arg;
850 int pid, err;
852 sock = file->private_data;
853 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
854 err = dev_ioctl(cmd, argp);
855 } else
856 #ifdef CONFIG_WIRELESS_EXT
857 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
858 err = dev_ioctl(cmd, argp);
859 } else
860 #endif /* CONFIG_WIRELESS_EXT */
861 switch (cmd) {
862 case FIOSETOWN:
863 case SIOCSPGRP:
864 err = -EFAULT;
865 if (get_user(pid, (int __user *)argp))
866 break;
867 err = f_setown(sock->file, pid, 1);
868 break;
869 case FIOGETOWN:
870 case SIOCGPGRP:
871 err = put_user(sock->file->f_owner.pid,
872 (int __user *)argp);
873 break;
874 case SIOCGIFBR:
875 case SIOCSIFBR:
876 case SIOCBRADDBR:
877 case SIOCBRDELBR:
878 err = -ENOPKG;
879 if (!br_ioctl_hook)
880 request_module("bridge");
882 mutex_lock(&br_ioctl_mutex);
883 if (br_ioctl_hook)
884 err = br_ioctl_hook(cmd, argp);
885 mutex_unlock(&br_ioctl_mutex);
886 break;
887 case SIOCGIFVLAN:
888 case SIOCSIFVLAN:
889 err = -ENOPKG;
890 if (!vlan_ioctl_hook)
891 request_module("8021q");
893 mutex_lock(&vlan_ioctl_mutex);
894 if (vlan_ioctl_hook)
895 err = vlan_ioctl_hook(argp);
896 mutex_unlock(&vlan_ioctl_mutex);
897 break;
898 case SIOCGIFDIVERT:
899 case SIOCSIFDIVERT:
900 /* Convert this to call through a hook */
901 err = divert_ioctl(cmd, argp);
902 break;
903 case SIOCADDDLCI:
904 case SIOCDELDLCI:
905 err = -ENOPKG;
906 if (!dlci_ioctl_hook)
907 request_module("dlci");
909 if (dlci_ioctl_hook) {
910 mutex_lock(&dlci_ioctl_mutex);
911 err = dlci_ioctl_hook(cmd, argp);
912 mutex_unlock(&dlci_ioctl_mutex);
914 break;
915 default:
916 err = sock->ops->ioctl(sock, cmd, arg);
919 * If this ioctl is unknown try to hand it down
920 * to the NIC driver.
922 if (err == -ENOIOCTLCMD)
923 err = dev_ioctl(cmd, argp);
924 break;
926 return err;
929 int sock_create_lite(int family, int type, int protocol, struct socket **res)
931 int err;
932 struct socket *sock = NULL;
934 err = security_socket_create(family, type, protocol, 1);
935 if (err)
936 goto out;
938 sock = sock_alloc();
939 if (!sock) {
940 err = -ENOMEM;
941 goto out;
944 sock->type = type;
945 err = security_socket_post_create(sock, family, type, protocol, 1);
946 if (err)
947 goto out_release;
949 out:
950 *res = sock;
951 return err;
952 out_release:
953 sock_release(sock);
954 sock = NULL;
955 goto out;
958 /* No kernel lock held - perfect */
959 static unsigned int sock_poll(struct file *file, poll_table *wait)
961 struct socket *sock;
964 * We can't return errors to poll, so it's either yes or no.
966 sock = file->private_data;
967 return sock->ops->poll(file, sock, wait);
970 static int sock_mmap(struct file *file, struct vm_area_struct *vma)
972 struct socket *sock = file->private_data;
974 return sock->ops->mmap(file, sock, vma);
977 static int sock_close(struct inode *inode, struct file *filp)
980 * It was possible the inode is NULL we were
981 * closing an unfinished socket.
984 if (!inode) {
985 printk(KERN_DEBUG "sock_close: NULL inode\n");
986 return 0;
988 sock_fasync(-1, filp, 0);
989 sock_release(SOCKET_I(inode));
990 return 0;
994 * Update the socket async list
996 * Fasync_list locking strategy.
998 * 1. fasync_list is modified only under process context socket lock
999 * i.e. under semaphore.
1000 * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
1001 * or under socket lock.
1002 * 3. fasync_list can be used from softirq context, so that
1003 * modification under socket lock have to be enhanced with
1004 * write_lock_bh(&sk->sk_callback_lock).
1005 * --ANK (990710)
1008 static int sock_fasync(int fd, struct file *filp, int on)
1010 struct fasync_struct *fa, *fna = NULL, **prev;
1011 struct socket *sock;
1012 struct sock *sk;
1014 if (on) {
1015 fna = kmalloc(sizeof(struct fasync_struct), GFP_KERNEL);
1016 if (fna == NULL)
1017 return -ENOMEM;
1020 sock = filp->private_data;
1022 sk = sock->sk;
1023 if (sk == NULL) {
1024 kfree(fna);
1025 return -EINVAL;
1028 lock_sock(sk);
1030 prev = &(sock->fasync_list);
1032 for (fa = *prev; fa != NULL; prev = &fa->fa_next, fa = *prev)
1033 if (fa->fa_file == filp)
1034 break;
1036 if (on) {
1037 if (fa != NULL) {
1038 write_lock_bh(&sk->sk_callback_lock);
1039 fa->fa_fd = fd;
1040 write_unlock_bh(&sk->sk_callback_lock);
1042 kfree(fna);
1043 goto out;
1045 fna->fa_file = filp;
1046 fna->fa_fd = fd;
1047 fna->magic = FASYNC_MAGIC;
1048 fna->fa_next = sock->fasync_list;
1049 write_lock_bh(&sk->sk_callback_lock);
1050 sock->fasync_list = fna;
1051 write_unlock_bh(&sk->sk_callback_lock);
1052 } else {
1053 if (fa != NULL) {
1054 write_lock_bh(&sk->sk_callback_lock);
1055 *prev = fa->fa_next;
1056 write_unlock_bh(&sk->sk_callback_lock);
1057 kfree(fa);
1061 out:
1062 release_sock(sock->sk);
1063 return 0;
1066 /* This function may be called only under socket lock or callback_lock */
1068 int sock_wake_async(struct socket *sock, int how, int band)
1070 if (!sock || !sock->fasync_list)
1071 return -1;
1072 switch (how) {
1073 case 1:
1075 if (test_bit(SOCK_ASYNC_WAITDATA, &sock->flags))
1076 break;
1077 goto call_kill;
1078 case 2:
1079 if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags))
1080 break;
1081 /* fall through */
1082 case 0:
1083 call_kill:
1084 __kill_fasync(sock->fasync_list, SIGIO, band);
1085 break;
1086 case 3:
1087 __kill_fasync(sock->fasync_list, SIGURG, band);
1089 return 0;
1092 static int __sock_create(int family, int type, int protocol,
1093 struct socket **res, int kern)
1095 int err;
1096 struct socket *sock;
1097 const struct net_proto_family *pf;
1100 * Check protocol is in range
1102 if (family < 0 || family >= NPROTO)
1103 return -EAFNOSUPPORT;
1104 if (type < 0 || type >= SOCK_MAX)
1105 return -EINVAL;
1107 /* Compatibility.
1109 This uglymoron is moved from INET layer to here to avoid
1110 deadlock in module load.
1112 if (family == PF_INET && type == SOCK_PACKET) {
1113 static int warned;
1114 if (!warned) {
1115 warned = 1;
1116 printk(KERN_INFO "%s uses obsolete (PF_INET,SOCK_PACKET)\n",
1117 current->comm);
1119 family = PF_PACKET;
1122 err = security_socket_create(family, type, protocol, kern);
1123 if (err)
1124 return err;
1127 * Allocate the socket and allow the family to set things up. if
1128 * the protocol is 0, the family is instructed to select an appropriate
1129 * default.
1131 sock = sock_alloc();
1132 if (!sock) {
1133 if (net_ratelimit())
1134 printk(KERN_WARNING "socket: no more sockets\n");
1135 return -ENFILE; /* Not exactly a match, but its the
1136 closest posix thing */
1139 sock->type = type;
1141 #if defined(CONFIG_KMOD)
1142 /* Attempt to load a protocol module if the find failed.
1144 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
1145 * requested real, full-featured networking support upon configuration.
1146 * Otherwise module support will break!
1148 if (net_families[family] == NULL)
1149 request_module("net-pf-%d", family);
1150 #endif
1152 rcu_read_lock();
1153 pf = rcu_dereference(net_families[family]);
1154 err = -EAFNOSUPPORT;
1155 if (!pf)
1156 goto out_release;
1159 * We will call the ->create function, that possibly is in a loadable
1160 * module, so we have to bump that loadable module refcnt first.
1162 if (!try_module_get(pf->owner))
1163 goto out_release;
1165 /* Now protected by module ref count */
1166 rcu_read_unlock();
1168 err = pf->create(sock, protocol);
1169 if (err < 0)
1170 goto out_module_put;
1173 * Now to bump the refcnt of the [loadable] module that owns this
1174 * socket at sock_release time we decrement its refcnt.
1176 if (!try_module_get(sock->ops->owner))
1177 goto out_module_busy;
1180 * Now that we're done with the ->create function, the [loadable]
1181 * module can have its refcnt decremented
1183 module_put(pf->owner);
1184 err = security_socket_post_create(sock, family, type, protocol, kern);
1185 if (err)
1186 goto out_release;
1187 *res = sock;
1189 return 0;
1191 out_module_busy:
1192 err = -EAFNOSUPPORT;
1193 out_module_put:
1194 sock->ops = NULL;
1195 module_put(pf->owner);
1196 out_sock_release:
1197 sock_release(sock);
1198 return err;
1200 out_release:
1201 rcu_read_unlock();
1202 goto out_sock_release;
1205 int sock_create(int family, int type, int protocol, struct socket **res)
1207 return __sock_create(family, type, protocol, res, 0);
1210 int sock_create_kern(int family, int type, int protocol, struct socket **res)
1212 return __sock_create(family, type, protocol, res, 1);
1215 asmlinkage long sys_socket(int family, int type, int protocol)
1217 int retval;
1218 struct socket *sock;
1220 retval = sock_create(family, type, protocol, &sock);
1221 if (retval < 0)
1222 goto out;
1224 retval = sock_map_fd(sock);
1225 if (retval < 0)
1226 goto out_release;
1228 out:
1229 /* It may be already another descriptor 8) Not kernel problem. */
1230 return retval;
1232 out_release:
1233 sock_release(sock);
1234 return retval;
1238 * Create a pair of connected sockets.
1241 asmlinkage long sys_socketpair(int family, int type, int protocol,
1242 int __user *usockvec)
1244 struct socket *sock1, *sock2;
1245 int fd1, fd2, err;
1248 * Obtain the first socket and check if the underlying protocol
1249 * supports the socketpair call.
1252 err = sock_create(family, type, protocol, &sock1);
1253 if (err < 0)
1254 goto out;
1256 err = sock_create(family, type, protocol, &sock2);
1257 if (err < 0)
1258 goto out_release_1;
1260 err = sock1->ops->socketpair(sock1, sock2);
1261 if (err < 0)
1262 goto out_release_both;
1264 fd1 = fd2 = -1;
1266 err = sock_map_fd(sock1);
1267 if (err < 0)
1268 goto out_release_both;
1269 fd1 = err;
1271 err = sock_map_fd(sock2);
1272 if (err < 0)
1273 goto out_close_1;
1274 fd2 = err;
1276 /* fd1 and fd2 may be already another descriptors.
1277 * Not kernel problem.
1280 err = put_user(fd1, &usockvec[0]);
1281 if (!err)
1282 err = put_user(fd2, &usockvec[1]);
1283 if (!err)
1284 return 0;
1286 sys_close(fd2);
1287 sys_close(fd1);
1288 return err;
1290 out_close_1:
1291 sock_release(sock2);
1292 sys_close(fd1);
1293 return err;
1295 out_release_both:
1296 sock_release(sock2);
1297 out_release_1:
1298 sock_release(sock1);
1299 out:
1300 return err;
1304 * Bind a name to a socket. Nothing much to do here since it's
1305 * the protocol's responsibility to handle the local address.
1307 * We move the socket address to kernel space before we call
1308 * the protocol layer (having also checked the address is ok).
1311 asmlinkage long sys_bind(int fd, struct sockaddr __user *umyaddr, int addrlen)
1313 struct socket *sock;
1314 char address[MAX_SOCK_ADDR];
1315 int err, fput_needed;
1317 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1318 if(sock) {
1319 err = move_addr_to_kernel(umyaddr, addrlen, address);
1320 if (err >= 0) {
1321 err = security_socket_bind(sock,
1322 (struct sockaddr *)address,
1323 addrlen);
1324 if (!err)
1325 err = sock->ops->bind(sock,
1326 (struct sockaddr *)
1327 address, addrlen);
1329 fput_light(sock->file, fput_needed);
1331 return err;
1335 * Perform a listen. Basically, we allow the protocol to do anything
1336 * necessary for a listen, and if that works, we mark the socket as
1337 * ready for listening.
1340 int sysctl_somaxconn __read_mostly = SOMAXCONN;
1342 asmlinkage long sys_listen(int fd, int backlog)
1344 struct socket *sock;
1345 int err, fput_needed;
1347 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1348 if (sock) {
1349 if ((unsigned)backlog > sysctl_somaxconn)
1350 backlog = sysctl_somaxconn;
1352 err = security_socket_listen(sock, backlog);
1353 if (!err)
1354 err = sock->ops->listen(sock, backlog);
1356 fput_light(sock->file, fput_needed);
1358 return err;
1362 * For accept, we attempt to create a new socket, set up the link
1363 * with the client, wake up the client, then return the new
1364 * connected fd. We collect the address of the connector in kernel
1365 * space and move it to user at the very end. This is unclean because
1366 * we open the socket then return an error.
1368 * 1003.1g adds the ability to recvmsg() to query connection pending
1369 * status to recvmsg. We need to add that support in a way thats
1370 * clean when we restucture accept also.
1373 asmlinkage long sys_accept(int fd, struct sockaddr __user *upeer_sockaddr,
1374 int __user *upeer_addrlen)
1376 struct socket *sock, *newsock;
1377 struct file *newfile;
1378 int err, len, newfd, fput_needed;
1379 char address[MAX_SOCK_ADDR];
1381 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1382 if (!sock)
1383 goto out;
1385 err = -ENFILE;
1386 if (!(newsock = sock_alloc()))
1387 goto out_put;
1389 newsock->type = sock->type;
1390 newsock->ops = sock->ops;
1393 * We don't need try_module_get here, as the listening socket (sock)
1394 * has the protocol module (sock->ops->owner) held.
1396 __module_get(newsock->ops->owner);
1398 newfd = sock_alloc_fd(&newfile);
1399 if (unlikely(newfd < 0)) {
1400 err = newfd;
1401 sock_release(newsock);
1402 goto out_put;
1405 err = sock_attach_fd(newsock, newfile);
1406 if (err < 0)
1407 goto out_fd;
1409 err = security_socket_accept(sock, newsock);
1410 if (err)
1411 goto out_fd;
1413 err = sock->ops->accept(sock, newsock, sock->file->f_flags);
1414 if (err < 0)
1415 goto out_fd;
1417 if (upeer_sockaddr) {
1418 if (newsock->ops->getname(newsock, (struct sockaddr *)address,
1419 &len, 2) < 0) {
1420 err = -ECONNABORTED;
1421 goto out_fd;
1423 err = move_addr_to_user(address, len, upeer_sockaddr,
1424 upeer_addrlen);
1425 if (err < 0)
1426 goto out_fd;
1429 /* File flags are not inherited via accept() unlike another OSes. */
1431 fd_install(newfd, newfile);
1432 err = newfd;
1434 security_socket_post_accept(sock, newsock);
1436 out_put:
1437 fput_light(sock->file, fput_needed);
1438 out:
1439 return err;
1440 out_fd:
1441 fput(newfile);
1442 put_unused_fd(newfd);
1443 goto out_put;
1447 * Attempt to connect to a socket with the server address. The address
1448 * is in user space so we verify it is OK and move it to kernel space.
1450 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
1451 * break bindings
1453 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
1454 * other SEQPACKET protocols that take time to connect() as it doesn't
1455 * include the -EINPROGRESS status for such sockets.
1458 asmlinkage long sys_connect(int fd, struct sockaddr __user *uservaddr,
1459 int addrlen)
1461 struct socket *sock;
1462 char address[MAX_SOCK_ADDR];
1463 int err, fput_needed;
1465 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1466 if (!sock)
1467 goto out;
1468 err = move_addr_to_kernel(uservaddr, addrlen, address);
1469 if (err < 0)
1470 goto out_put;
1472 err =
1473 security_socket_connect(sock, (struct sockaddr *)address, addrlen);
1474 if (err)
1475 goto out_put;
1477 err = sock->ops->connect(sock, (struct sockaddr *)address, addrlen,
1478 sock->file->f_flags);
1479 out_put:
1480 fput_light(sock->file, fput_needed);
1481 out:
1482 return err;
1486 * Get the local address ('name') of a socket object. Move the obtained
1487 * name to user space.
1490 asmlinkage long sys_getsockname(int fd, struct sockaddr __user *usockaddr,
1491 int __user *usockaddr_len)
1493 struct socket *sock;
1494 char address[MAX_SOCK_ADDR];
1495 int len, err, fput_needed;
1497 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1498 if (!sock)
1499 goto out;
1501 err = security_socket_getsockname(sock);
1502 if (err)
1503 goto out_put;
1505 err = sock->ops->getname(sock, (struct sockaddr *)address, &len, 0);
1506 if (err)
1507 goto out_put;
1508 err = move_addr_to_user(address, len, usockaddr, usockaddr_len);
1510 out_put:
1511 fput_light(sock->file, fput_needed);
1512 out:
1513 return err;
1517 * Get the remote address ('name') of a socket object. Move the obtained
1518 * name to user space.
1521 asmlinkage long sys_getpeername(int fd, struct sockaddr __user *usockaddr,
1522 int __user *usockaddr_len)
1524 struct socket *sock;
1525 char address[MAX_SOCK_ADDR];
1526 int len, err, fput_needed;
1528 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1529 if (sock != NULL) {
1530 err = security_socket_getpeername(sock);
1531 if (err) {
1532 fput_light(sock->file, fput_needed);
1533 return err;
1536 err =
1537 sock->ops->getname(sock, (struct sockaddr *)address, &len,
1539 if (!err)
1540 err = move_addr_to_user(address, len, usockaddr,
1541 usockaddr_len);
1542 fput_light(sock->file, fput_needed);
1544 return err;
1548 * Send a datagram to a given address. We move the address into kernel
1549 * space and check the user space data area is readable before invoking
1550 * the protocol.
1553 asmlinkage long sys_sendto(int fd, void __user *buff, size_t len,
1554 unsigned flags, struct sockaddr __user *addr,
1555 int addr_len)
1557 struct socket *sock;
1558 char address[MAX_SOCK_ADDR];
1559 int err;
1560 struct msghdr msg;
1561 struct iovec iov;
1562 int fput_needed;
1563 struct file *sock_file;
1565 sock_file = fget_light(fd, &fput_needed);
1566 if (!sock_file)
1567 return -EBADF;
1569 sock = sock_from_file(sock_file, &err);
1570 if (!sock)
1571 goto out_put;
1572 iov.iov_base = buff;
1573 iov.iov_len = len;
1574 msg.msg_name = NULL;
1575 msg.msg_iov = &iov;
1576 msg.msg_iovlen = 1;
1577 msg.msg_control = NULL;
1578 msg.msg_controllen = 0;
1579 msg.msg_namelen = 0;
1580 if (addr) {
1581 err = move_addr_to_kernel(addr, addr_len, address);
1582 if (err < 0)
1583 goto out_put;
1584 msg.msg_name = address;
1585 msg.msg_namelen = addr_len;
1587 if (sock->file->f_flags & O_NONBLOCK)
1588 flags |= MSG_DONTWAIT;
1589 msg.msg_flags = flags;
1590 err = sock_sendmsg(sock, &msg, len);
1592 out_put:
1593 fput_light(sock_file, fput_needed);
1594 return err;
1598 * Send a datagram down a socket.
1601 asmlinkage long sys_send(int fd, void __user *buff, size_t len, unsigned flags)
1603 return sys_sendto(fd, buff, len, flags, NULL, 0);
1607 * Receive a frame from the socket and optionally record the address of the
1608 * sender. We verify the buffers are writable and if needed move the
1609 * sender address from kernel to user space.
1612 asmlinkage long sys_recvfrom(int fd, void __user *ubuf, size_t size,
1613 unsigned flags, struct sockaddr __user *addr,
1614 int __user *addr_len)
1616 struct socket *sock;
1617 struct iovec iov;
1618 struct msghdr msg;
1619 char address[MAX_SOCK_ADDR];
1620 int err, err2;
1621 struct file *sock_file;
1622 int fput_needed;
1624 sock_file = fget_light(fd, &fput_needed);
1625 if (!sock_file)
1626 return -EBADF;
1628 sock = sock_from_file(sock_file, &err);
1629 if (!sock)
1630 goto out;
1632 msg.msg_control = NULL;
1633 msg.msg_controllen = 0;
1634 msg.msg_iovlen = 1;
1635 msg.msg_iov = &iov;
1636 iov.iov_len = size;
1637 iov.iov_base = ubuf;
1638 msg.msg_name = address;
1639 msg.msg_namelen = MAX_SOCK_ADDR;
1640 if (sock->file->f_flags & O_NONBLOCK)
1641 flags |= MSG_DONTWAIT;
1642 err = sock_recvmsg(sock, &msg, size, flags);
1644 if (err >= 0 && addr != NULL) {
1645 err2 = move_addr_to_user(address, msg.msg_namelen, addr, addr_len);
1646 if (err2 < 0)
1647 err = err2;
1649 out:
1650 fput_light(sock_file, fput_needed);
1651 return err;
1655 * Receive a datagram from a socket.
1658 asmlinkage long sys_recv(int fd, void __user *ubuf, size_t size,
1659 unsigned flags)
1661 return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
1665 * Set a socket option. Because we don't know the option lengths we have
1666 * to pass the user mode parameter for the protocols to sort out.
1669 asmlinkage long sys_setsockopt(int fd, int level, int optname,
1670 char __user *optval, int optlen)
1672 int err, fput_needed;
1673 struct socket *sock;
1675 if (optlen < 0)
1676 return -EINVAL;
1678 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1679 if (sock != NULL) {
1680 err = security_socket_setsockopt(sock, level, optname);
1681 if (err)
1682 goto out_put;
1684 if (level == SOL_SOCKET)
1685 err =
1686 sock_setsockopt(sock, level, optname, optval,
1687 optlen);
1688 else
1689 err =
1690 sock->ops->setsockopt(sock, level, optname, optval,
1691 optlen);
1692 out_put:
1693 fput_light(sock->file, fput_needed);
1695 return err;
1699 * Get a socket option. Because we don't know the option lengths we have
1700 * to pass a user mode parameter for the protocols to sort out.
1703 asmlinkage long sys_getsockopt(int fd, int level, int optname,
1704 char __user *optval, int __user *optlen)
1706 int err, fput_needed;
1707 struct socket *sock;
1709 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1710 if (sock != NULL) {
1711 err = security_socket_getsockopt(sock, level, optname);
1712 if (err)
1713 goto out_put;
1715 if (level == SOL_SOCKET)
1716 err =
1717 sock_getsockopt(sock, level, optname, optval,
1718 optlen);
1719 else
1720 err =
1721 sock->ops->getsockopt(sock, level, optname, optval,
1722 optlen);
1723 out_put:
1724 fput_light(sock->file, fput_needed);
1726 return err;
1730 * Shutdown a socket.
1733 asmlinkage long sys_shutdown(int fd, int how)
1735 int err, fput_needed;
1736 struct socket *sock;
1738 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1739 if (sock != NULL) {
1740 err = security_socket_shutdown(sock, how);
1741 if (!err)
1742 err = sock->ops->shutdown(sock, how);
1743 fput_light(sock->file, fput_needed);
1745 return err;
1748 /* A couple of helpful macros for getting the address of the 32/64 bit
1749 * fields which are the same type (int / unsigned) on our platforms.
1751 #define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
1752 #define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
1753 #define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
1756 * BSD sendmsg interface
1759 asmlinkage long sys_sendmsg(int fd, struct msghdr __user *msg, unsigned flags)
1761 struct compat_msghdr __user *msg_compat =
1762 (struct compat_msghdr __user *)msg;
1763 struct socket *sock;
1764 char address[MAX_SOCK_ADDR];
1765 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
1766 unsigned char ctl[sizeof(struct cmsghdr) + 20]
1767 __attribute__ ((aligned(sizeof(__kernel_size_t))));
1768 /* 20 is size of ipv6_pktinfo */
1769 unsigned char *ctl_buf = ctl;
1770 struct msghdr msg_sys;
1771 int err, ctl_len, iov_size, total_len;
1772 int fput_needed;
1774 err = -EFAULT;
1775 if (MSG_CMSG_COMPAT & flags) {
1776 if (get_compat_msghdr(&msg_sys, msg_compat))
1777 return -EFAULT;
1779 else if (copy_from_user(&msg_sys, msg, sizeof(struct msghdr)))
1780 return -EFAULT;
1782 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1783 if (!sock)
1784 goto out;
1786 /* do not move before msg_sys is valid */
1787 err = -EMSGSIZE;
1788 if (msg_sys.msg_iovlen > UIO_MAXIOV)
1789 goto out_put;
1791 /* Check whether to allocate the iovec area */
1792 err = -ENOMEM;
1793 iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
1794 if (msg_sys.msg_iovlen > UIO_FASTIOV) {
1795 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
1796 if (!iov)
1797 goto out_put;
1800 /* This will also move the address data into kernel space */
1801 if (MSG_CMSG_COMPAT & flags) {
1802 err = verify_compat_iovec(&msg_sys, iov, address, VERIFY_READ);
1803 } else
1804 err = verify_iovec(&msg_sys, iov, address, VERIFY_READ);
1805 if (err < 0)
1806 goto out_freeiov;
1807 total_len = err;
1809 err = -ENOBUFS;
1811 if (msg_sys.msg_controllen > INT_MAX)
1812 goto out_freeiov;
1813 ctl_len = msg_sys.msg_controllen;
1814 if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
1815 err =
1816 cmsghdr_from_user_compat_to_kern(&msg_sys, sock->sk, ctl,
1817 sizeof(ctl));
1818 if (err)
1819 goto out_freeiov;
1820 ctl_buf = msg_sys.msg_control;
1821 ctl_len = msg_sys.msg_controllen;
1822 } else if (ctl_len) {
1823 if (ctl_len > sizeof(ctl)) {
1824 ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
1825 if (ctl_buf == NULL)
1826 goto out_freeiov;
1828 err = -EFAULT;
1830 * Careful! Before this, msg_sys.msg_control contains a user pointer.
1831 * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
1832 * checking falls down on this.
1834 if (copy_from_user(ctl_buf, (void __user *)msg_sys.msg_control,
1835 ctl_len))
1836 goto out_freectl;
1837 msg_sys.msg_control = ctl_buf;
1839 msg_sys.msg_flags = flags;
1841 if (sock->file->f_flags & O_NONBLOCK)
1842 msg_sys.msg_flags |= MSG_DONTWAIT;
1843 err = sock_sendmsg(sock, &msg_sys, total_len);
1845 out_freectl:
1846 if (ctl_buf != ctl)
1847 sock_kfree_s(sock->sk, ctl_buf, ctl_len);
1848 out_freeiov:
1849 if (iov != iovstack)
1850 sock_kfree_s(sock->sk, iov, iov_size);
1851 out_put:
1852 fput_light(sock->file, fput_needed);
1853 out:
1854 return err;
1858 * BSD recvmsg interface
1861 asmlinkage long sys_recvmsg(int fd, struct msghdr __user *msg,
1862 unsigned int flags)
1864 struct compat_msghdr __user *msg_compat =
1865 (struct compat_msghdr __user *)msg;
1866 struct socket *sock;
1867 struct iovec iovstack[UIO_FASTIOV];
1868 struct iovec *iov = iovstack;
1869 struct msghdr msg_sys;
1870 unsigned long cmsg_ptr;
1871 int err, iov_size, total_len, len;
1872 int fput_needed;
1874 /* kernel mode address */
1875 char addr[MAX_SOCK_ADDR];
1877 /* user mode address pointers */
1878 struct sockaddr __user *uaddr;
1879 int __user *uaddr_len;
1881 if (MSG_CMSG_COMPAT & flags) {
1882 if (get_compat_msghdr(&msg_sys, msg_compat))
1883 return -EFAULT;
1885 else if (copy_from_user(&msg_sys, msg, sizeof(struct msghdr)))
1886 return -EFAULT;
1888 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1889 if (!sock)
1890 goto out;
1892 err = -EMSGSIZE;
1893 if (msg_sys.msg_iovlen > UIO_MAXIOV)
1894 goto out_put;
1896 /* Check whether to allocate the iovec area */
1897 err = -ENOMEM;
1898 iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
1899 if (msg_sys.msg_iovlen > UIO_FASTIOV) {
1900 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
1901 if (!iov)
1902 goto out_put;
1906 * Save the user-mode address (verify_iovec will change the
1907 * kernel msghdr to use the kernel address space)
1910 uaddr = (void __user *)msg_sys.msg_name;
1911 uaddr_len = COMPAT_NAMELEN(msg);
1912 if (MSG_CMSG_COMPAT & flags) {
1913 err = verify_compat_iovec(&msg_sys, iov, addr, VERIFY_WRITE);
1914 } else
1915 err = verify_iovec(&msg_sys, iov, addr, VERIFY_WRITE);
1916 if (err < 0)
1917 goto out_freeiov;
1918 total_len = err;
1920 cmsg_ptr = (unsigned long)msg_sys.msg_control;
1921 msg_sys.msg_flags = 0;
1922 if (MSG_CMSG_COMPAT & flags)
1923 msg_sys.msg_flags = MSG_CMSG_COMPAT;
1925 if (sock->file->f_flags & O_NONBLOCK)
1926 flags |= MSG_DONTWAIT;
1927 err = sock_recvmsg(sock, &msg_sys, total_len, flags);
1928 if (err < 0)
1929 goto out_freeiov;
1930 len = err;
1932 if (uaddr != NULL) {
1933 err = move_addr_to_user(addr, msg_sys.msg_namelen, uaddr,
1934 uaddr_len);
1935 if (err < 0)
1936 goto out_freeiov;
1938 err = __put_user((msg_sys.msg_flags & ~MSG_CMSG_COMPAT),
1939 COMPAT_FLAGS(msg));
1940 if (err)
1941 goto out_freeiov;
1942 if (MSG_CMSG_COMPAT & flags)
1943 err = __put_user((unsigned long)msg_sys.msg_control - cmsg_ptr,
1944 &msg_compat->msg_controllen);
1945 else
1946 err = __put_user((unsigned long)msg_sys.msg_control - cmsg_ptr,
1947 &msg->msg_controllen);
1948 if (err)
1949 goto out_freeiov;
1950 err = len;
1952 out_freeiov:
1953 if (iov != iovstack)
1954 sock_kfree_s(sock->sk, iov, iov_size);
1955 out_put:
1956 fput_light(sock->file, fput_needed);
1957 out:
1958 return err;
1961 #ifdef __ARCH_WANT_SYS_SOCKETCALL
1963 /* Argument list sizes for sys_socketcall */
1964 #define AL(x) ((x) * sizeof(unsigned long))
1965 static const unsigned char nargs[18]={
1966 AL(0),AL(3),AL(3),AL(3),AL(2),AL(3),
1967 AL(3),AL(3),AL(4),AL(4),AL(4),AL(6),
1968 AL(6),AL(2),AL(5),AL(5),AL(3),AL(3)
1971 #undef AL
1974 * System call vectors.
1976 * Argument checking cleaned up. Saved 20% in size.
1977 * This function doesn't need to set the kernel lock because
1978 * it is set by the callees.
1981 asmlinkage long sys_socketcall(int call, unsigned long __user *args)
1983 unsigned long a[6];
1984 unsigned long a0, a1;
1985 int err;
1987 if (call < 1 || call > SYS_RECVMSG)
1988 return -EINVAL;
1990 /* copy_from_user should be SMP safe. */
1991 if (copy_from_user(a, args, nargs[call]))
1992 return -EFAULT;
1994 err = audit_socketcall(nargs[call] / sizeof(unsigned long), a);
1995 if (err)
1996 return err;
1998 a0 = a[0];
1999 a1 = a[1];
2001 switch (call) {
2002 case SYS_SOCKET:
2003 err = sys_socket(a0, a1, a[2]);
2004 break;
2005 case SYS_BIND:
2006 err = sys_bind(a0, (struct sockaddr __user *)a1, a[2]);
2007 break;
2008 case SYS_CONNECT:
2009 err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
2010 break;
2011 case SYS_LISTEN:
2012 err = sys_listen(a0, a1);
2013 break;
2014 case SYS_ACCEPT:
2015 err =
2016 sys_accept(a0, (struct sockaddr __user *)a1,
2017 (int __user *)a[2]);
2018 break;
2019 case SYS_GETSOCKNAME:
2020 err =
2021 sys_getsockname(a0, (struct sockaddr __user *)a1,
2022 (int __user *)a[2]);
2023 break;
2024 case SYS_GETPEERNAME:
2025 err =
2026 sys_getpeername(a0, (struct sockaddr __user *)a1,
2027 (int __user *)a[2]);
2028 break;
2029 case SYS_SOCKETPAIR:
2030 err = sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
2031 break;
2032 case SYS_SEND:
2033 err = sys_send(a0, (void __user *)a1, a[2], a[3]);
2034 break;
2035 case SYS_SENDTO:
2036 err = sys_sendto(a0, (void __user *)a1, a[2], a[3],
2037 (struct sockaddr __user *)a[4], a[5]);
2038 break;
2039 case SYS_RECV:
2040 err = sys_recv(a0, (void __user *)a1, a[2], a[3]);
2041 break;
2042 case SYS_RECVFROM:
2043 err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
2044 (struct sockaddr __user *)a[4],
2045 (int __user *)a[5]);
2046 break;
2047 case SYS_SHUTDOWN:
2048 err = sys_shutdown(a0, a1);
2049 break;
2050 case SYS_SETSOCKOPT:
2051 err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]);
2052 break;
2053 case SYS_GETSOCKOPT:
2054 err =
2055 sys_getsockopt(a0, a1, a[2], (char __user *)a[3],
2056 (int __user *)a[4]);
2057 break;
2058 case SYS_SENDMSG:
2059 err = sys_sendmsg(a0, (struct msghdr __user *)a1, a[2]);
2060 break;
2061 case SYS_RECVMSG:
2062 err = sys_recvmsg(a0, (struct msghdr __user *)a1, a[2]);
2063 break;
2064 default:
2065 err = -EINVAL;
2066 break;
2068 return err;
2071 #endif /* __ARCH_WANT_SYS_SOCKETCALL */
2074 * sock_register - add a socket protocol handler
2075 * @ops: description of protocol
2077 * This function is called by a protocol handler that wants to
2078 * advertise its address family, and have it linked into the
2079 * socket interface. The value ops->family coresponds to the
2080 * socket system call protocol family.
2082 int sock_register(const struct net_proto_family *ops)
2084 int err;
2086 if (ops->family >= NPROTO) {
2087 printk(KERN_CRIT "protocol %d >= NPROTO(%d)\n", ops->family,
2088 NPROTO);
2089 return -ENOBUFS;
2092 spin_lock(&net_family_lock);
2093 if (net_families[ops->family])
2094 err = -EEXIST;
2095 else {
2096 net_families[ops->family] = ops;
2097 err = 0;
2099 spin_unlock(&net_family_lock);
2101 printk(KERN_INFO "NET: Registered protocol family %d\n", ops->family);
2102 return err;
2106 * sock_unregister - remove a protocol handler
2107 * @family: protocol family to remove
2109 * This function is called by a protocol handler that wants to
2110 * remove its address family, and have it unlinked from the
2111 * new socket creation.
2113 * If protocol handler is a module, then it can use module reference
2114 * counts to protect against new references. If protocol handler is not
2115 * a module then it needs to provide its own protection in
2116 * the ops->create routine.
2118 void sock_unregister(int family)
2120 BUG_ON(family < 0 || family >= NPROTO);
2122 spin_lock(&net_family_lock);
2123 net_families[family] = NULL;
2124 spin_unlock(&net_family_lock);
2126 synchronize_rcu();
2128 printk(KERN_INFO "NET: Unregistered protocol family %d\n", family);
2131 static int __init sock_init(void)
2134 * Initialize sock SLAB cache.
2137 sk_init();
2140 * Initialize skbuff SLAB cache
2142 skb_init();
2145 * Initialize the protocols module.
2148 init_inodecache();
2149 register_filesystem(&sock_fs_type);
2150 sock_mnt = kern_mount(&sock_fs_type);
2152 /* The real protocol initialization is performed in later initcalls.
2155 #ifdef CONFIG_NETFILTER
2156 netfilter_init();
2157 #endif
2159 return 0;
2162 core_initcall(sock_init); /* early initcall */
2164 #ifdef CONFIG_PROC_FS
2165 void socket_seq_show(struct seq_file *seq)
2167 int cpu;
2168 int counter = 0;
2170 for_each_possible_cpu(cpu)
2171 counter += per_cpu(sockets_in_use, cpu);
2173 /* It can be negative, by the way. 8) */
2174 if (counter < 0)
2175 counter = 0;
2177 seq_printf(seq, "sockets: used %d\n", counter);
2179 #endif /* CONFIG_PROC_FS */
2181 #ifdef CONFIG_COMPAT
2182 static long compat_sock_ioctl(struct file *file, unsigned cmd,
2183 unsigned long arg)
2185 struct socket *sock = file->private_data;
2186 int ret = -ENOIOCTLCMD;
2188 if (sock->ops->compat_ioctl)
2189 ret = sock->ops->compat_ioctl(sock, cmd, arg);
2191 return ret;
2193 #endif
2195 int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen)
2197 return sock->ops->bind(sock, addr, addrlen);
2200 int kernel_listen(struct socket *sock, int backlog)
2202 return sock->ops->listen(sock, backlog);
2205 int kernel_accept(struct socket *sock, struct socket **newsock, int flags)
2207 struct sock *sk = sock->sk;
2208 int err;
2210 err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
2211 newsock);
2212 if (err < 0)
2213 goto done;
2215 err = sock->ops->accept(sock, *newsock, flags);
2216 if (err < 0) {
2217 sock_release(*newsock);
2218 goto done;
2221 (*newsock)->ops = sock->ops;
2223 done:
2224 return err;
2227 int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
2228 int flags)
2230 return sock->ops->connect(sock, addr, addrlen, flags);
2233 int kernel_getsockname(struct socket *sock, struct sockaddr *addr,
2234 int *addrlen)
2236 return sock->ops->getname(sock, addr, addrlen, 0);
2239 int kernel_getpeername(struct socket *sock, struct sockaddr *addr,
2240 int *addrlen)
2242 return sock->ops->getname(sock, addr, addrlen, 1);
2245 int kernel_getsockopt(struct socket *sock, int level, int optname,
2246 char *optval, int *optlen)
2248 mm_segment_t oldfs = get_fs();
2249 int err;
2251 set_fs(KERNEL_DS);
2252 if (level == SOL_SOCKET)
2253 err = sock_getsockopt(sock, level, optname, optval, optlen);
2254 else
2255 err = sock->ops->getsockopt(sock, level, optname, optval,
2256 optlen);
2257 set_fs(oldfs);
2258 return err;
2261 int kernel_setsockopt(struct socket *sock, int level, int optname,
2262 char *optval, int optlen)
2264 mm_segment_t oldfs = get_fs();
2265 int err;
2267 set_fs(KERNEL_DS);
2268 if (level == SOL_SOCKET)
2269 err = sock_setsockopt(sock, level, optname, optval, optlen);
2270 else
2271 err = sock->ops->setsockopt(sock, level, optname, optval,
2272 optlen);
2273 set_fs(oldfs);
2274 return err;
2277 int kernel_sendpage(struct socket *sock, struct page *page, int offset,
2278 size_t size, int flags)
2280 if (sock->ops->sendpage)
2281 return sock->ops->sendpage(sock, page, offset, size, flags);
2283 return sock_no_sendpage(sock, page, offset, size, flags);
2286 int kernel_sock_ioctl(struct socket *sock, int cmd, unsigned long arg)
2288 mm_segment_t oldfs = get_fs();
2289 int err;
2291 set_fs(KERNEL_DS);
2292 err = sock->ops->ioctl(sock, cmd, arg);
2293 set_fs(oldfs);
2295 return err;
2298 /* ABI emulation layers need these two */
2299 EXPORT_SYMBOL(move_addr_to_kernel);
2300 EXPORT_SYMBOL(move_addr_to_user);
2301 EXPORT_SYMBOL(sock_create);
2302 EXPORT_SYMBOL(sock_create_kern);
2303 EXPORT_SYMBOL(sock_create_lite);
2304 EXPORT_SYMBOL(sock_map_fd);
2305 EXPORT_SYMBOL(sock_recvmsg);
2306 EXPORT_SYMBOL(sock_register);
2307 EXPORT_SYMBOL(sock_release);
2308 EXPORT_SYMBOL(sock_sendmsg);
2309 EXPORT_SYMBOL(sock_unregister);
2310 EXPORT_SYMBOL(sock_wake_async);
2311 EXPORT_SYMBOL(sockfd_lookup);
2312 EXPORT_SYMBOL(kernel_sendmsg);
2313 EXPORT_SYMBOL(kernel_recvmsg);
2314 EXPORT_SYMBOL(kernel_bind);
2315 EXPORT_SYMBOL(kernel_listen);
2316 EXPORT_SYMBOL(kernel_accept);
2317 EXPORT_SYMBOL(kernel_connect);
2318 EXPORT_SYMBOL(kernel_getsockname);
2319 EXPORT_SYMBOL(kernel_getpeername);
2320 EXPORT_SYMBOL(kernel_getsockopt);
2321 EXPORT_SYMBOL(kernel_setsockopt);
2322 EXPORT_SYMBOL(kernel_sendpage);
2323 EXPORT_SYMBOL(kernel_sock_ioctl);