2 * NET An implementation of the SOCKET network access protocol.
4 * Version: @(#)socket.c 1.1.93 18/02/95
6 * Authors: Orest Zborowski, <obz@Kodak.COM>
8 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
11 * Anonymous : NOTSOCK/BADF cleanup. Error fix in
13 * Alan Cox : verify_area() fixes
14 * Alan Cox : Removed DDI
15 * Jonathan Kamens : SOCK_DGRAM reconnect bug
16 * Alan Cox : Moved a load of checks to the very
18 * Alan Cox : Move address structures to/from user
19 * mode above the protocol layers.
20 * Rob Janssen : Allow 0 length sends.
21 * Alan Cox : Asynchronous I/O support (cribbed from the
23 * Niibe Yutaka : Asynchronous I/O for writes (4.4BSD style)
24 * Jeff Uphoff : Made max number of sockets command-line
26 * Matti Aarnio : Made the number of sockets dynamic,
27 * to be allocated when needed, and mr.
28 * Uphoff's max is used as max to be
29 * allowed to allocate.
30 * Linus : Argh. removed all the socket allocation
31 * altogether: it's in the inode now.
32 * Alan Cox : Made sock_alloc()/sock_release() public
33 * for NetROM and future kernel nfsd type
35 * Alan Cox : sendmsg/recvmsg basics.
36 * Tom Dyas : Export net symbols.
37 * Marcin Dalecki : Fixed problems with CONFIG_NET="n".
38 * Alan Cox : Added thread locking to sys_* calls
39 * for sockets. May have errors at the
41 * Kevin Buhr : Fixed the dumb errors in the above.
42 * Andi Kleen : Some small cleanups, optimizations,
43 * and fixed a copy_from_user() bug.
44 * Tigran Aivazian : sys_send(args) calls sys_sendto(args, NULL, 0)
45 * Tigran Aivazian : Made listen(2) backlog sanity checks
46 * protocol-independent
49 * This program is free software; you can redistribute it and/or
50 * modify it under the terms of the GNU General Public License
51 * as published by the Free Software Foundation; either version
52 * 2 of the License, or (at your option) any later version.
55 * This module is effectively the top level interface to the BSD socket
58 * Based upon Swansea University Computer Society NET3.039
62 #include <linux/socket.h>
63 #include <linux/file.h>
64 #include <linux/net.h>
65 #include <linux/interrupt.h>
66 #include <linux/thread_info.h>
67 #include <linux/rcupdate.h>
68 #include <linux/netdevice.h>
69 #include <linux/proc_fs.h>
70 #include <linux/seq_file.h>
71 #include <linux/mutex.h>
72 #include <linux/wanrouter.h>
73 #include <linux/if_bridge.h>
74 #include <linux/if_frad.h>
75 #include <linux/if_vlan.h>
76 #include <linux/init.h>
77 #include <linux/poll.h>
78 #include <linux/cache.h>
79 #include <linux/module.h>
80 #include <linux/highmem.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>
88 #include <linux/nsproxy.h>
89 #include <linux/magic.h>
91 #include <asm/uaccess.h>
92 #include <asm/unistd.h>
94 #include <net/compat.h>
98 #include <linux/netfilter.h>
100 #ifdef CONFIG_UID_STAT
101 #include <linux/uid_stat.h>
104 static int sock_no_open(struct inode *irrelevant, struct file *dontcare);
105 static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov,
106 unsigned long nr_segs, loff_t pos);
107 static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov,
108 unsigned long nr_segs, loff_t pos);
109 static int sock_mmap(struct file *file, struct vm_area_struct *vma);
111 static int sock_close(struct inode *inode, struct file *file);
112 static unsigned int sock_poll(struct file *file,
113 struct poll_table_struct *wait);
114 static long sock_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
116 static long compat_sock_ioctl(struct file *file,
117 unsigned int cmd, unsigned long arg);
119 static int sock_fasync(int fd, struct file *filp, int on);
120 static ssize_t sock_sendpage(struct file *file, struct page *page,
121 int offset, size_t size, loff_t *ppos, int more);
122 static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
123 struct pipe_inode_info *pipe, size_t len,
127 * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
128 * in the operation structures but are done directly via the socketcall() multiplexor.
131 static const struct file_operations socket_file_ops = {
132 .owner = THIS_MODULE,
134 .aio_read = sock_aio_read,
135 .aio_write = sock_aio_write,
137 .unlocked_ioctl = sock_ioctl,
139 .compat_ioctl = compat_sock_ioctl,
142 .open = sock_no_open, /* special open code to disallow open via /proc */
143 .release = sock_close,
144 .fasync = sock_fasync,
145 .sendpage = sock_sendpage,
146 .splice_write = generic_splice_sendpage,
147 .splice_read = sock_splice_read,
151 * The protocol list. Each protocol is registered in here.
154 static DEFINE_SPINLOCK(net_family_lock);
155 static const struct net_proto_family *net_families[NPROTO] __read_mostly;
158 * Statistics counters of the socket lists
161 static DEFINE_PER_CPU(int, sockets_in_use) = 0;
165 * Move socket addresses back and forth across the kernel/user
166 * divide and look after the messy bits.
169 #define MAX_SOCK_ADDR 128 /* 108 for Unix domain -
170 16 for IP, 16 for IPX,
173 must be at least one bigger than
174 the AF_UNIX size (see net/unix/af_unix.c
179 * move_addr_to_kernel - copy a socket address into kernel space
180 * @uaddr: Address in user space
181 * @kaddr: Address in kernel space
182 * @ulen: Length in user space
184 * The address is copied into kernel space. If the provided address is
185 * too long an error code of -EINVAL is returned. If the copy gives
186 * invalid addresses -EFAULT is returned. On a success 0 is returned.
189 int move_addr_to_kernel(void __user *uaddr, int ulen, struct sockaddr *kaddr)
191 if (ulen < 0 || ulen > sizeof(struct sockaddr_storage))
195 if (copy_from_user(kaddr, uaddr, ulen))
197 return audit_sockaddr(ulen, kaddr);
201 * move_addr_to_user - copy an address to user space
202 * @kaddr: kernel space address
203 * @klen: length of address in kernel
204 * @uaddr: user space address
205 * @ulen: pointer to user length field
207 * The value pointed to by ulen on entry is the buffer length available.
208 * This is overwritten with the buffer space used. -EINVAL is returned
209 * if an overlong buffer is specified or a negative buffer size. -EFAULT
210 * is returned if either the buffer or the length field are not
212 * After copying the data up to the limit the user specifies, the true
213 * length of the data is written over the length limit the user
214 * specified. Zero is returned for a success.
217 int move_addr_to_user(struct sockaddr *kaddr, int klen, void __user *uaddr,
223 err = get_user(len, ulen);
228 if (len < 0 || len > sizeof(struct sockaddr_storage))
231 if (audit_sockaddr(klen, kaddr))
233 if (copy_to_user(uaddr, kaddr, len))
237 * "fromlen shall refer to the value before truncation.."
240 return __put_user(klen, ulen);
243 static struct kmem_cache *sock_inode_cachep __read_mostly;
245 static struct inode *sock_alloc_inode(struct super_block *sb)
247 struct socket_alloc *ei;
249 ei = kmem_cache_alloc(sock_inode_cachep, GFP_KERNEL);
252 init_waitqueue_head(&ei->socket.wait);
254 ei->socket.fasync_list = NULL;
255 ei->socket.state = SS_UNCONNECTED;
256 ei->socket.flags = 0;
257 ei->socket.ops = NULL;
258 ei->socket.sk = NULL;
259 ei->socket.file = NULL;
261 return &ei->vfs_inode;
264 static void sock_destroy_inode(struct inode *inode)
266 kmem_cache_free(sock_inode_cachep,
267 container_of(inode, struct socket_alloc, vfs_inode));
270 static void init_once(void *foo)
272 struct socket_alloc *ei = (struct socket_alloc *)foo;
274 inode_init_once(&ei->vfs_inode);
277 static int init_inodecache(void)
279 sock_inode_cachep = kmem_cache_create("sock_inode_cache",
280 sizeof(struct socket_alloc),
282 (SLAB_HWCACHE_ALIGN |
283 SLAB_RECLAIM_ACCOUNT |
286 if (sock_inode_cachep == NULL)
291 static const 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,
305 static struct vfsmount *sock_mnt __read_mostly;
307 static struct file_system_type sock_fs_type = {
309 .get_sb = sockfs_get_sb,
310 .kill_sb = kill_anon_super,
313 static int sockfs_delete_dentry(struct dentry *dentry)
316 * At creation time, we pretended this dentry was hashed
317 * (by clearing DCACHE_UNHASHED bit in d_flags)
318 * At delete time, we restore the truth : not hashed.
319 * (so that dput() can proceed correctly)
321 dentry->d_flags |= DCACHE_UNHASHED;
326 * sockfs_dname() is called from d_path().
328 static char *sockfs_dname(struct dentry *dentry, char *buffer, int buflen)
330 return dynamic_dname(dentry, buffer, buflen, "socket:[%lu]",
331 dentry->d_inode->i_ino);
334 static const struct dentry_operations sockfs_dentry_operations = {
335 .d_delete = sockfs_delete_dentry,
336 .d_dname = sockfs_dname,
340 * Obtains the first available file descriptor and sets it up for use.
342 * These functions create file structures and maps them to fd space
343 * of the current process. On success it returns file descriptor
344 * and file struct implicitly stored in sock->file.
345 * Note that another thread may close file descriptor before we return
346 * from this function. We use the fact that now we do not refer
347 * to socket after mapping. If one day we will need it, this
348 * function will increment ref. count on file by 1.
350 * In any case returned fd MAY BE not valid!
351 * This race condition is unavoidable
352 * with shared fd spaces, we cannot solve it inside kernel,
353 * but we take care of internal coherence yet.
356 static int sock_alloc_fd(struct file **filep, int flags)
360 fd = get_unused_fd_flags(flags);
361 if (likely(fd >= 0)) {
362 struct file *file = get_empty_filp();
365 if (unlikely(!file)) {
374 static int sock_attach_fd(struct socket *sock, struct file *file, int flags)
376 struct dentry *dentry;
377 struct qstr name = { .name = "" };
379 dentry = d_alloc(sock_mnt->mnt_sb->s_root, &name);
380 if (unlikely(!dentry))
383 dentry->d_op = &sockfs_dentry_operations;
385 * We dont want to push this dentry into global dentry hash table.
386 * We pretend dentry is already hashed, by unsetting DCACHE_UNHASHED
387 * This permits a working /proc/$pid/fd/XXX on sockets
389 dentry->d_flags &= ~DCACHE_UNHASHED;
390 d_instantiate(dentry, SOCK_INODE(sock));
393 init_file(file, sock_mnt, dentry, FMODE_READ | FMODE_WRITE,
395 SOCK_INODE(sock)->i_fop = &socket_file_ops;
396 file->f_flags = O_RDWR | (flags & O_NONBLOCK);
398 file->private_data = sock;
403 int sock_map_fd(struct socket *sock, int flags)
405 struct file *newfile;
406 int fd = sock_alloc_fd(&newfile, flags);
408 if (likely(fd >= 0)) {
409 int err = sock_attach_fd(sock, newfile, flags);
411 if (unlikely(err < 0)) {
416 fd_install(fd, newfile);
421 static struct socket *sock_from_file(struct file *file, int *err)
423 if (file->f_op == &socket_file_ops)
424 return file->private_data; /* set in sock_map_fd */
431 * sockfd_lookup - Go from a file number to its socket slot
433 * @err: pointer to an error code return
435 * The file handle passed in is locked and the socket it is bound
436 * too is returned. If an error occurs the err pointer is overwritten
437 * with a negative errno code and NULL is returned. The function checks
438 * for both invalid handles and passing a handle which is not a socket.
440 * On a success the socket object pointer is returned.
443 struct socket *sockfd_lookup(int fd, int *err)
454 sock = sock_from_file(file, err);
460 static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed)
466 file = fget_light(fd, fput_needed);
468 sock = sock_from_file(file, err);
471 fput_light(file, *fput_needed);
477 * sock_alloc - allocate a socket
479 * Allocate a new inode and socket object. The two are bound together
480 * and initialised. The socket is then returned. If we are out of inodes
484 static struct socket *sock_alloc(void)
489 inode = new_inode(sock_mnt->mnt_sb);
493 sock = SOCKET_I(inode);
495 kmemcheck_annotate_bitfield(sock, type);
496 inode->i_mode = S_IFSOCK | S_IRWXUGO;
497 inode->i_uid = current_fsuid();
498 inode->i_gid = current_fsgid();
500 percpu_add(sockets_in_use, 1);
505 * In theory you can't get an open on this inode, but /proc provides
506 * a back door. Remember to keep it shut otherwise you'll let the
507 * creepy crawlies in.
510 static int sock_no_open(struct inode *irrelevant, struct file *dontcare)
515 const struct file_operations bad_sock_fops = {
516 .owner = THIS_MODULE,
517 .open = sock_no_open,
521 * sock_release - close a socket
522 * @sock: socket to close
524 * The socket is released from the protocol stack if it has a release
525 * callback, and the inode is then released if the socket is bound to
526 * an inode not a file.
529 void sock_release(struct socket *sock)
532 struct module *owner = sock->ops->owner;
534 sock->ops->release(sock);
539 if (sock->fasync_list)
540 printk(KERN_ERR "sock_release: fasync list not empty!\n");
542 percpu_sub(sockets_in_use, 1);
544 iput(SOCK_INODE(sock));
550 int sock_tx_timestamp(struct msghdr *msg, struct sock *sk,
551 union skb_shared_tx *shtx)
554 if (sock_flag(sk, SOCK_TIMESTAMPING_TX_HARDWARE))
556 if (sock_flag(sk, SOCK_TIMESTAMPING_TX_SOFTWARE))
560 EXPORT_SYMBOL(sock_tx_timestamp);
562 static inline int __sock_sendmsg(struct kiocb *iocb, struct socket *sock,
563 struct msghdr *msg, size_t size)
565 struct sock_iocb *si = kiocb_to_siocb(iocb);
573 err = security_socket_sendmsg(sock, msg, size);
577 err = sock->ops->sendmsg(iocb, sock, msg, size);
578 #ifdef CONFIG_UID_STAT
580 update_tcp_snd(current_uid(), err);
585 int sock_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
588 struct sock_iocb siocb;
591 init_sync_kiocb(&iocb, NULL);
592 iocb.private = &siocb;
593 ret = __sock_sendmsg(&iocb, sock, msg, size);
594 if (-EIOCBQUEUED == ret)
595 ret = wait_on_sync_kiocb(&iocb);
599 int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
600 struct kvec *vec, size_t num, size_t size)
602 mm_segment_t oldfs = get_fs();
607 * the following is safe, since for compiler definitions of kvec and
608 * iovec are identical, yielding the same in-core layout and alignment
610 msg->msg_iov = (struct iovec *)vec;
611 msg->msg_iovlen = num;
612 result = sock_sendmsg(sock, msg, size);
617 static int ktime2ts(ktime_t kt, struct timespec *ts)
620 *ts = ktime_to_timespec(kt);
628 * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
630 void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
633 int need_software_tstamp = sock_flag(sk, SOCK_RCVTSTAMP);
634 struct timespec ts[3];
636 struct skb_shared_hwtstamps *shhwtstamps =
639 /* Race occurred between timestamp enabling and packet
640 receiving. Fill in the current time for now. */
641 if (need_software_tstamp && skb->tstamp.tv64 == 0)
642 __net_timestamp(skb);
644 if (need_software_tstamp) {
645 if (!sock_flag(sk, SOCK_RCVTSTAMPNS)) {
647 skb_get_timestamp(skb, &tv);
648 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMP,
652 skb_get_timestampns(skb, &ts);
653 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPNS,
659 memset(ts, 0, sizeof(ts));
660 if (skb->tstamp.tv64 &&
661 sock_flag(sk, SOCK_TIMESTAMPING_SOFTWARE)) {
662 skb_get_timestampns(skb, ts + 0);
666 if (sock_flag(sk, SOCK_TIMESTAMPING_SYS_HARDWARE) &&
667 ktime2ts(shhwtstamps->syststamp, ts + 1))
669 if (sock_flag(sk, SOCK_TIMESTAMPING_RAW_HARDWARE) &&
670 ktime2ts(shhwtstamps->hwtstamp, ts + 2))
674 put_cmsg(msg, SOL_SOCKET,
675 SCM_TIMESTAMPING, sizeof(ts), &ts);
678 EXPORT_SYMBOL_GPL(__sock_recv_timestamp);
680 static inline int __sock_recvmsg(struct kiocb *iocb, struct socket *sock,
681 struct msghdr *msg, size_t size, int flags)
684 struct sock_iocb *si = kiocb_to_siocb(iocb);
692 err = security_socket_recvmsg(sock, msg, size, flags);
696 err = sock->ops->recvmsg(iocb, sock, msg, size, flags);
697 #ifdef CONFIG_UID_STAT
699 update_tcp_rcv(current_uid(), err);
704 int sock_recvmsg(struct socket *sock, struct msghdr *msg,
705 size_t size, int flags)
708 struct sock_iocb siocb;
711 init_sync_kiocb(&iocb, NULL);
712 iocb.private = &siocb;
713 ret = __sock_recvmsg(&iocb, sock, msg, size, flags);
714 if (-EIOCBQUEUED == ret)
715 ret = wait_on_sync_kiocb(&iocb);
719 int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
720 struct kvec *vec, size_t num, size_t size, int flags)
722 mm_segment_t oldfs = get_fs();
727 * the following is safe, since for compiler definitions of kvec and
728 * iovec are identical, yielding the same in-core layout and alignment
730 msg->msg_iov = (struct iovec *)vec, msg->msg_iovlen = num;
731 result = sock_recvmsg(sock, msg, size, flags);
736 static void sock_aio_dtor(struct kiocb *iocb)
738 kfree(iocb->private);
741 static ssize_t sock_sendpage(struct file *file, struct page *page,
742 int offset, size_t size, loff_t *ppos, int more)
747 sock = file->private_data;
749 flags = !(file->f_flags & O_NONBLOCK) ? 0 : MSG_DONTWAIT;
753 return kernel_sendpage(sock, page, offset, size, flags);
756 static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
757 struct pipe_inode_info *pipe, size_t len,
760 struct socket *sock = file->private_data;
762 if (unlikely(!sock->ops->splice_read))
765 return sock->ops->splice_read(sock, ppos, pipe, len, flags);
768 static struct sock_iocb *alloc_sock_iocb(struct kiocb *iocb,
769 struct sock_iocb *siocb)
771 if (!is_sync_kiocb(iocb)) {
772 siocb = kmalloc(sizeof(*siocb), GFP_KERNEL);
775 iocb->ki_dtor = sock_aio_dtor;
779 iocb->private = siocb;
783 static ssize_t do_sock_read(struct msghdr *msg, struct kiocb *iocb,
784 struct file *file, const struct iovec *iov,
785 unsigned long nr_segs)
787 struct socket *sock = file->private_data;
791 for (i = 0; i < nr_segs; i++)
792 size += iov[i].iov_len;
794 msg->msg_name = NULL;
795 msg->msg_namelen = 0;
796 msg->msg_control = NULL;
797 msg->msg_controllen = 0;
798 msg->msg_iov = (struct iovec *)iov;
799 msg->msg_iovlen = nr_segs;
800 msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
802 return __sock_recvmsg(iocb, sock, msg, size, msg->msg_flags);
805 static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov,
806 unsigned long nr_segs, loff_t pos)
808 struct sock_iocb siocb, *x;
813 if (iocb->ki_left == 0) /* Match SYS5 behaviour */
817 x = alloc_sock_iocb(iocb, &siocb);
820 return do_sock_read(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
823 static ssize_t do_sock_write(struct msghdr *msg, struct kiocb *iocb,
824 struct file *file, const struct iovec *iov,
825 unsigned long nr_segs)
827 struct socket *sock = file->private_data;
831 for (i = 0; i < nr_segs; i++)
832 size += iov[i].iov_len;
834 msg->msg_name = NULL;
835 msg->msg_namelen = 0;
836 msg->msg_control = NULL;
837 msg->msg_controllen = 0;
838 msg->msg_iov = (struct iovec *)iov;
839 msg->msg_iovlen = nr_segs;
840 msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
841 if (sock->type == SOCK_SEQPACKET)
842 msg->msg_flags |= MSG_EOR;
844 return __sock_sendmsg(iocb, sock, msg, size);
847 static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov,
848 unsigned long nr_segs, loff_t pos)
850 struct sock_iocb siocb, *x;
855 x = alloc_sock_iocb(iocb, &siocb);
859 return do_sock_write(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
863 * Atomic setting of ioctl hooks to avoid race
864 * with module unload.
867 static DEFINE_MUTEX(br_ioctl_mutex);
868 static int (*br_ioctl_hook) (struct net *, unsigned int cmd, void __user *arg) = NULL;
870 void brioctl_set(int (*hook) (struct net *, unsigned int, void __user *))
872 mutex_lock(&br_ioctl_mutex);
873 br_ioctl_hook = hook;
874 mutex_unlock(&br_ioctl_mutex);
877 EXPORT_SYMBOL(brioctl_set);
879 static DEFINE_MUTEX(vlan_ioctl_mutex);
880 static int (*vlan_ioctl_hook) (struct net *, void __user *arg);
882 void vlan_ioctl_set(int (*hook) (struct net *, void __user *))
884 mutex_lock(&vlan_ioctl_mutex);
885 vlan_ioctl_hook = hook;
886 mutex_unlock(&vlan_ioctl_mutex);
889 EXPORT_SYMBOL(vlan_ioctl_set);
891 static DEFINE_MUTEX(dlci_ioctl_mutex);
892 static int (*dlci_ioctl_hook) (unsigned int, void __user *);
894 void dlci_ioctl_set(int (*hook) (unsigned int, void __user *))
896 mutex_lock(&dlci_ioctl_mutex);
897 dlci_ioctl_hook = hook;
898 mutex_unlock(&dlci_ioctl_mutex);
901 EXPORT_SYMBOL(dlci_ioctl_set);
904 * With an ioctl, arg may well be a user mode pointer, but we don't know
905 * what to do with it - that's up to the protocol still.
908 static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
912 void __user *argp = (void __user *)arg;
916 sock = file->private_data;
919 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
920 err = dev_ioctl(net, cmd, argp);
922 #ifdef CONFIG_WIRELESS_EXT
923 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
924 err = dev_ioctl(net, cmd, argp);
926 #endif /* CONFIG_WIRELESS_EXT */
931 if (get_user(pid, (int __user *)argp))
933 err = f_setown(sock->file, pid, 1);
937 err = put_user(f_getown(sock->file),
946 request_module("bridge");
948 mutex_lock(&br_ioctl_mutex);
950 err = br_ioctl_hook(net, cmd, argp);
951 mutex_unlock(&br_ioctl_mutex);
956 if (!vlan_ioctl_hook)
957 request_module("8021q");
959 mutex_lock(&vlan_ioctl_mutex);
961 err = vlan_ioctl_hook(net, argp);
962 mutex_unlock(&vlan_ioctl_mutex);
967 if (!dlci_ioctl_hook)
968 request_module("dlci");
970 mutex_lock(&dlci_ioctl_mutex);
972 err = dlci_ioctl_hook(cmd, argp);
973 mutex_unlock(&dlci_ioctl_mutex);
976 err = sock->ops->ioctl(sock, cmd, arg);
979 * If this ioctl is unknown try to hand it down
982 if (err == -ENOIOCTLCMD)
983 err = dev_ioctl(net, cmd, argp);
989 int sock_create_lite(int family, int type, int protocol, struct socket **res)
992 struct socket *sock = NULL;
994 err = security_socket_create(family, type, protocol, 1);
1005 err = security_socket_post_create(sock, family, type, protocol, 1);
1018 /* No kernel lock held - perfect */
1019 static unsigned int sock_poll(struct file *file, poll_table *wait)
1021 struct socket *sock;
1024 * We can't return errors to poll, so it's either yes or no.
1026 sock = file->private_data;
1027 return sock->ops->poll(file, sock, wait);
1030 static int sock_mmap(struct file *file, struct vm_area_struct *vma)
1032 struct socket *sock = file->private_data;
1034 return sock->ops->mmap(file, sock, vma);
1037 static int sock_close(struct inode *inode, struct file *filp)
1040 * It was possible the inode is NULL we were
1041 * closing an unfinished socket.
1045 printk(KERN_DEBUG "sock_close: NULL inode\n");
1048 sock_release(SOCKET_I(inode));
1053 * Update the socket async list
1055 * Fasync_list locking strategy.
1057 * 1. fasync_list is modified only under process context socket lock
1058 * i.e. under semaphore.
1059 * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
1060 * or under socket lock.
1061 * 3. fasync_list can be used from softirq context, so that
1062 * modification under socket lock have to be enhanced with
1063 * write_lock_bh(&sk->sk_callback_lock).
1067 static int sock_fasync(int fd, struct file *filp, int on)
1069 struct fasync_struct *fa, *fna = NULL, **prev;
1070 struct socket *sock;
1074 fna = kmalloc(sizeof(struct fasync_struct), GFP_KERNEL);
1079 sock = filp->private_data;
1089 spin_lock(&filp->f_lock);
1091 filp->f_flags |= FASYNC;
1093 filp->f_flags &= ~FASYNC;
1094 spin_unlock(&filp->f_lock);
1096 prev = &(sock->fasync_list);
1098 for (fa = *prev; fa != NULL; prev = &fa->fa_next, fa = *prev)
1099 if (fa->fa_file == filp)
1104 write_lock_bh(&sk->sk_callback_lock);
1106 write_unlock_bh(&sk->sk_callback_lock);
1111 fna->fa_file = filp;
1113 fna->magic = FASYNC_MAGIC;
1114 fna->fa_next = sock->fasync_list;
1115 write_lock_bh(&sk->sk_callback_lock);
1116 sock->fasync_list = fna;
1117 write_unlock_bh(&sk->sk_callback_lock);
1120 write_lock_bh(&sk->sk_callback_lock);
1121 *prev = fa->fa_next;
1122 write_unlock_bh(&sk->sk_callback_lock);
1128 release_sock(sock->sk);
1132 /* This function may be called only under socket lock or callback_lock */
1134 int sock_wake_async(struct socket *sock, int how, int band)
1136 if (!sock || !sock->fasync_list)
1139 case SOCK_WAKE_WAITD:
1140 if (test_bit(SOCK_ASYNC_WAITDATA, &sock->flags))
1143 case SOCK_WAKE_SPACE:
1144 if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags))
1149 __kill_fasync(sock->fasync_list, SIGIO, band);
1152 __kill_fasync(sock->fasync_list, SIGURG, band);
1157 static int __sock_create(struct net *net, int family, int type, int protocol,
1158 struct socket **res, int kern)
1161 struct socket *sock;
1162 const struct net_proto_family *pf;
1165 * Check protocol is in range
1167 if (family < 0 || family >= NPROTO)
1168 return -EAFNOSUPPORT;
1169 if (type < 0 || type >= SOCK_MAX)
1174 This uglymoron is moved from INET layer to here to avoid
1175 deadlock in module load.
1177 if (family == PF_INET && type == SOCK_PACKET) {
1181 printk(KERN_INFO "%s uses obsolete (PF_INET,SOCK_PACKET)\n",
1187 err = security_socket_create(family, type, protocol, kern);
1192 * Allocate the socket and allow the family to set things up. if
1193 * the protocol is 0, the family is instructed to select an appropriate
1196 sock = sock_alloc();
1198 if (net_ratelimit())
1199 printk(KERN_WARNING "socket: no more sockets\n");
1200 return -ENFILE; /* Not exactly a match, but its the
1201 closest posix thing */
1206 #ifdef CONFIG_MODULES
1207 /* Attempt to load a protocol module if the find failed.
1209 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
1210 * requested real, full-featured networking support upon configuration.
1211 * Otherwise module support will break!
1213 if (net_families[family] == NULL)
1214 request_module("net-pf-%d", family);
1218 pf = rcu_dereference(net_families[family]);
1219 err = -EAFNOSUPPORT;
1224 * We will call the ->create function, that possibly is in a loadable
1225 * module, so we have to bump that loadable module refcnt first.
1227 if (!try_module_get(pf->owner))
1230 /* Now protected by module ref count */
1233 err = pf->create(net, sock, protocol);
1235 goto out_module_put;
1238 * Now to bump the refcnt of the [loadable] module that owns this
1239 * socket at sock_release time we decrement its refcnt.
1241 if (!try_module_get(sock->ops->owner))
1242 goto out_module_busy;
1245 * Now that we're done with the ->create function, the [loadable]
1246 * module can have its refcnt decremented
1248 module_put(pf->owner);
1249 err = security_socket_post_create(sock, family, type, protocol, kern);
1251 goto out_sock_release;
1257 err = -EAFNOSUPPORT;
1260 module_put(pf->owner);
1267 goto out_sock_release;
1270 int sock_create(int family, int type, int protocol, struct socket **res)
1272 return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0);
1275 int sock_create_kern(int family, int type, int protocol, struct socket **res)
1277 return __sock_create(&init_net, family, type, protocol, res, 1);
1280 SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol)
1283 struct socket *sock;
1286 /* Check the SOCK_* constants for consistency. */
1287 BUILD_BUG_ON(SOCK_CLOEXEC != O_CLOEXEC);
1288 BUILD_BUG_ON((SOCK_MAX | SOCK_TYPE_MASK) != SOCK_TYPE_MASK);
1289 BUILD_BUG_ON(SOCK_CLOEXEC & SOCK_TYPE_MASK);
1290 BUILD_BUG_ON(SOCK_NONBLOCK & SOCK_TYPE_MASK);
1292 flags = type & ~SOCK_TYPE_MASK;
1293 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1295 type &= SOCK_TYPE_MASK;
1297 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1298 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1300 retval = sock_create(family, type, protocol, &sock);
1304 retval = sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK));
1309 /* It may be already another descriptor 8) Not kernel problem. */
1318 * Create a pair of connected sockets.
1321 SYSCALL_DEFINE4(socketpair, int, family, int, type, int, protocol,
1322 int __user *, usockvec)
1324 struct socket *sock1, *sock2;
1326 struct file *newfile1, *newfile2;
1329 flags = type & ~SOCK_TYPE_MASK;
1330 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1332 type &= SOCK_TYPE_MASK;
1334 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1335 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1338 * Obtain the first socket and check if the underlying protocol
1339 * supports the socketpair call.
1342 err = sock_create(family, type, protocol, &sock1);
1346 err = sock_create(family, type, protocol, &sock2);
1350 err = sock1->ops->socketpair(sock1, sock2);
1352 goto out_release_both;
1354 fd1 = sock_alloc_fd(&newfile1, flags & O_CLOEXEC);
1355 if (unlikely(fd1 < 0)) {
1357 goto out_release_both;
1360 fd2 = sock_alloc_fd(&newfile2, flags & O_CLOEXEC);
1361 if (unlikely(fd2 < 0)) {
1365 goto out_release_both;
1368 err = sock_attach_fd(sock1, newfile1, flags & O_NONBLOCK);
1369 if (unlikely(err < 0)) {
1373 err = sock_attach_fd(sock2, newfile2, flags & O_NONBLOCK);
1374 if (unlikely(err < 0)) {
1379 audit_fd_pair(fd1, fd2);
1380 fd_install(fd1, newfile1);
1381 fd_install(fd2, newfile2);
1382 /* fd1 and fd2 may be already another descriptors.
1383 * Not kernel problem.
1386 err = put_user(fd1, &usockvec[0]);
1388 err = put_user(fd2, &usockvec[1]);
1397 sock_release(sock2);
1399 sock_release(sock1);
1405 sock_release(sock1);
1408 sock_release(sock2);
1415 * Bind a name to a socket. Nothing much to do here since it's
1416 * the protocol's responsibility to handle the local address.
1418 * We move the socket address to kernel space before we call
1419 * the protocol layer (having also checked the address is ok).
1422 SYSCALL_DEFINE3(bind, int, fd, struct sockaddr __user *, umyaddr, int, addrlen)
1424 struct socket *sock;
1425 struct sockaddr_storage address;
1426 int err, fput_needed;
1428 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1430 err = move_addr_to_kernel(umyaddr, addrlen, (struct sockaddr *)&address);
1432 err = security_socket_bind(sock,
1433 (struct sockaddr *)&address,
1436 err = sock->ops->bind(sock,
1440 fput_light(sock->file, fput_needed);
1446 * Perform a listen. Basically, we allow the protocol to do anything
1447 * necessary for a listen, and if that works, we mark the socket as
1448 * ready for listening.
1451 SYSCALL_DEFINE2(listen, int, fd, int, backlog)
1453 struct socket *sock;
1454 int err, fput_needed;
1457 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1459 somaxconn = sock_net(sock->sk)->core.sysctl_somaxconn;
1460 if ((unsigned)backlog > somaxconn)
1461 backlog = somaxconn;
1463 err = security_socket_listen(sock, backlog);
1465 err = sock->ops->listen(sock, backlog);
1467 fput_light(sock->file, fput_needed);
1473 * For accept, we attempt to create a new socket, set up the link
1474 * with the client, wake up the client, then return the new
1475 * connected fd. We collect the address of the connector in kernel
1476 * space and move it to user at the very end. This is unclean because
1477 * we open the socket then return an error.
1479 * 1003.1g adds the ability to recvmsg() to query connection pending
1480 * status to recvmsg. We need to add that support in a way thats
1481 * clean when we restucture accept also.
1484 SYSCALL_DEFINE4(accept4, int, fd, struct sockaddr __user *, upeer_sockaddr,
1485 int __user *, upeer_addrlen, int, flags)
1487 struct socket *sock, *newsock;
1488 struct file *newfile;
1489 int err, len, newfd, fput_needed;
1490 struct sockaddr_storage address;
1492 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1495 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1496 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1498 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1503 if (!(newsock = sock_alloc()))
1506 newsock->type = sock->type;
1507 newsock->ops = sock->ops;
1510 * We don't need try_module_get here, as the listening socket (sock)
1511 * has the protocol module (sock->ops->owner) held.
1513 __module_get(newsock->ops->owner);
1515 newfd = sock_alloc_fd(&newfile, flags & O_CLOEXEC);
1516 if (unlikely(newfd < 0)) {
1518 sock_release(newsock);
1522 err = sock_attach_fd(newsock, newfile, flags & O_NONBLOCK);
1526 err = security_socket_accept(sock, newsock);
1530 err = sock->ops->accept(sock, newsock, sock->file->f_flags);
1534 if (upeer_sockaddr) {
1535 if (newsock->ops->getname(newsock, (struct sockaddr *)&address,
1537 err = -ECONNABORTED;
1540 err = move_addr_to_user((struct sockaddr *)&address,
1541 len, upeer_sockaddr, upeer_addrlen);
1546 /* File flags are not inherited via accept() unlike another OSes. */
1548 fd_install(newfd, newfile);
1552 fput_light(sock->file, fput_needed);
1556 sock_release(newsock);
1558 put_unused_fd(newfd);
1562 put_unused_fd(newfd);
1566 SYSCALL_DEFINE3(accept, int, fd, struct sockaddr __user *, upeer_sockaddr,
1567 int __user *, upeer_addrlen)
1569 return sys_accept4(fd, upeer_sockaddr, upeer_addrlen, 0);
1573 * Attempt to connect to a socket with the server address. The address
1574 * is in user space so we verify it is OK and move it to kernel space.
1576 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
1579 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
1580 * other SEQPACKET protocols that take time to connect() as it doesn't
1581 * include the -EINPROGRESS status for such sockets.
1584 SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
1587 struct socket *sock;
1588 struct sockaddr_storage address;
1589 int err, fput_needed;
1591 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1594 err = move_addr_to_kernel(uservaddr, addrlen, (struct sockaddr *)&address);
1599 security_socket_connect(sock, (struct sockaddr *)&address, addrlen);
1603 err = sock->ops->connect(sock, (struct sockaddr *)&address, addrlen,
1604 sock->file->f_flags);
1606 fput_light(sock->file, fput_needed);
1612 * Get the local address ('name') of a socket object. Move the obtained
1613 * name to user space.
1616 SYSCALL_DEFINE3(getsockname, int, fd, struct sockaddr __user *, usockaddr,
1617 int __user *, usockaddr_len)
1619 struct socket *sock;
1620 struct sockaddr_storage address;
1621 int len, err, fput_needed;
1623 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1627 err = security_socket_getsockname(sock);
1631 err = sock->ops->getname(sock, (struct sockaddr *)&address, &len, 0);
1634 err = move_addr_to_user((struct sockaddr *)&address, len, usockaddr, usockaddr_len);
1637 fput_light(sock->file, fput_needed);
1643 * Get the remote address ('name') of a socket object. Move the obtained
1644 * name to user space.
1647 SYSCALL_DEFINE3(getpeername, int, fd, struct sockaddr __user *, usockaddr,
1648 int __user *, usockaddr_len)
1650 struct socket *sock;
1651 struct sockaddr_storage address;
1652 int len, err, fput_needed;
1654 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1656 err = security_socket_getpeername(sock);
1658 fput_light(sock->file, fput_needed);
1663 sock->ops->getname(sock, (struct sockaddr *)&address, &len,
1666 err = move_addr_to_user((struct sockaddr *)&address, len, usockaddr,
1668 fput_light(sock->file, fput_needed);
1674 * Send a datagram to a given address. We move the address into kernel
1675 * space and check the user space data area is readable before invoking
1679 SYSCALL_DEFINE6(sendto, int, fd, void __user *, buff, size_t, len,
1680 unsigned, flags, struct sockaddr __user *, addr,
1683 struct socket *sock;
1684 struct sockaddr_storage address;
1692 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1696 iov.iov_base = buff;
1698 msg.msg_name = NULL;
1701 msg.msg_control = NULL;
1702 msg.msg_controllen = 0;
1703 msg.msg_namelen = 0;
1705 err = move_addr_to_kernel(addr, addr_len, (struct sockaddr *)&address);
1708 msg.msg_name = (struct sockaddr *)&address;
1709 msg.msg_namelen = addr_len;
1711 if (sock->file->f_flags & O_NONBLOCK)
1712 flags |= MSG_DONTWAIT;
1713 msg.msg_flags = flags;
1714 err = sock_sendmsg(sock, &msg, len);
1717 fput_light(sock->file, fput_needed);
1723 * Send a datagram down a socket.
1726 SYSCALL_DEFINE4(send, int, fd, void __user *, buff, size_t, len,
1729 return sys_sendto(fd, buff, len, flags, NULL, 0);
1733 * Receive a frame from the socket and optionally record the address of the
1734 * sender. We verify the buffers are writable and if needed move the
1735 * sender address from kernel to user space.
1738 SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, ubuf, size_t, size,
1739 unsigned, flags, struct sockaddr __user *, addr,
1740 int __user *, addr_len)
1742 struct socket *sock;
1745 struct sockaddr_storage address;
1751 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1755 msg.msg_control = NULL;
1756 msg.msg_controllen = 0;
1760 iov.iov_base = ubuf;
1761 msg.msg_name = (struct sockaddr *)&address;
1762 msg.msg_namelen = sizeof(address);
1763 if (sock->file->f_flags & O_NONBLOCK)
1764 flags |= MSG_DONTWAIT;
1765 err = sock_recvmsg(sock, &msg, size, flags);
1767 if (err >= 0 && addr != NULL) {
1768 err2 = move_addr_to_user((struct sockaddr *)&address,
1769 msg.msg_namelen, addr, addr_len);
1774 fput_light(sock->file, fput_needed);
1780 * Receive a datagram from a socket.
1783 asmlinkage long sys_recv(int fd, void __user *ubuf, size_t size,
1786 return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
1790 * Set a socket option. Because we don't know the option lengths we have
1791 * to pass the user mode parameter for the protocols to sort out.
1794 SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname,
1795 char __user *, optval, int, optlen)
1797 int err, fput_needed;
1798 struct socket *sock;
1803 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1805 err = security_socket_setsockopt(sock, level, optname);
1809 if (level == SOL_SOCKET)
1811 sock_setsockopt(sock, level, optname, optval,
1815 sock->ops->setsockopt(sock, level, optname, optval,
1818 fput_light(sock->file, fput_needed);
1824 * Get a socket option. Because we don't know the option lengths we have
1825 * to pass a user mode parameter for the protocols to sort out.
1828 SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname,
1829 char __user *, optval, int __user *, optlen)
1831 int err, fput_needed;
1832 struct socket *sock;
1834 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1836 err = security_socket_getsockopt(sock, level, optname);
1840 if (level == SOL_SOCKET)
1842 sock_getsockopt(sock, level, optname, optval,
1846 sock->ops->getsockopt(sock, level, optname, optval,
1849 fput_light(sock->file, fput_needed);
1855 * Shutdown a socket.
1858 SYSCALL_DEFINE2(shutdown, int, fd, int, how)
1860 int err, fput_needed;
1861 struct socket *sock;
1863 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1865 err = security_socket_shutdown(sock, how);
1867 err = sock->ops->shutdown(sock, how);
1868 fput_light(sock->file, fput_needed);
1873 /* A couple of helpful macros for getting the address of the 32/64 bit
1874 * fields which are the same type (int / unsigned) on our platforms.
1876 #define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
1877 #define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
1878 #define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
1881 * BSD sendmsg interface
1884 SYSCALL_DEFINE3(sendmsg, int, fd, struct msghdr __user *, msg, unsigned, flags)
1886 struct compat_msghdr __user *msg_compat =
1887 (struct compat_msghdr __user *)msg;
1888 struct socket *sock;
1889 struct sockaddr_storage address;
1890 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
1891 unsigned char ctl[sizeof(struct cmsghdr) + 20]
1892 __attribute__ ((aligned(sizeof(__kernel_size_t))));
1893 /* 20 is size of ipv6_pktinfo */
1894 unsigned char *ctl_buf = ctl;
1895 struct msghdr msg_sys;
1896 int err, ctl_len, iov_size, total_len;
1900 if (MSG_CMSG_COMPAT & flags) {
1901 if (get_compat_msghdr(&msg_sys, msg_compat))
1904 else if (copy_from_user(&msg_sys, msg, sizeof(struct msghdr)))
1907 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1911 /* do not move before msg_sys is valid */
1913 if (msg_sys.msg_iovlen > UIO_MAXIOV)
1916 /* Check whether to allocate the iovec area */
1918 iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
1919 if (msg_sys.msg_iovlen > UIO_FASTIOV) {
1920 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
1925 /* This will also move the address data into kernel space */
1926 if (MSG_CMSG_COMPAT & flags) {
1927 err = verify_compat_iovec(&msg_sys, iov,
1928 (struct sockaddr *)&address,
1931 err = verify_iovec(&msg_sys, iov,
1932 (struct sockaddr *)&address,
1940 if (msg_sys.msg_controllen > INT_MAX)
1942 ctl_len = msg_sys.msg_controllen;
1943 if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
1945 cmsghdr_from_user_compat_to_kern(&msg_sys, sock->sk, ctl,
1949 ctl_buf = msg_sys.msg_control;
1950 ctl_len = msg_sys.msg_controllen;
1951 } else if (ctl_len) {
1952 if (ctl_len > sizeof(ctl)) {
1953 ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
1954 if (ctl_buf == NULL)
1959 * Careful! Before this, msg_sys.msg_control contains a user pointer.
1960 * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
1961 * checking falls down on this.
1963 if (copy_from_user(ctl_buf, (void __user *)msg_sys.msg_control,
1966 msg_sys.msg_control = ctl_buf;
1968 msg_sys.msg_flags = flags;
1970 if (sock->file->f_flags & O_NONBLOCK)
1971 msg_sys.msg_flags |= MSG_DONTWAIT;
1972 err = sock_sendmsg(sock, &msg_sys, total_len);
1976 sock_kfree_s(sock->sk, ctl_buf, ctl_len);
1978 if (iov != iovstack)
1979 sock_kfree_s(sock->sk, iov, iov_size);
1981 fput_light(sock->file, fput_needed);
1987 * BSD recvmsg interface
1990 SYSCALL_DEFINE3(recvmsg, int, fd, struct msghdr __user *, msg,
1991 unsigned int, flags)
1993 struct compat_msghdr __user *msg_compat =
1994 (struct compat_msghdr __user *)msg;
1995 struct socket *sock;
1996 struct iovec iovstack[UIO_FASTIOV];
1997 struct iovec *iov = iovstack;
1998 struct msghdr msg_sys;
1999 unsigned long cmsg_ptr;
2000 int err, iov_size, total_len, len;
2003 /* kernel mode address */
2004 struct sockaddr_storage addr;
2006 /* user mode address pointers */
2007 struct sockaddr __user *uaddr;
2008 int __user *uaddr_len;
2010 if (MSG_CMSG_COMPAT & flags) {
2011 if (get_compat_msghdr(&msg_sys, msg_compat))
2014 else if (copy_from_user(&msg_sys, msg, sizeof(struct msghdr)))
2017 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2022 if (msg_sys.msg_iovlen > UIO_MAXIOV)
2025 /* Check whether to allocate the iovec area */
2027 iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
2028 if (msg_sys.msg_iovlen > UIO_FASTIOV) {
2029 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
2035 * Save the user-mode address (verify_iovec will change the
2036 * kernel msghdr to use the kernel address space)
2039 uaddr = (__force void __user *)msg_sys.msg_name;
2040 uaddr_len = COMPAT_NAMELEN(msg);
2041 if (MSG_CMSG_COMPAT & flags) {
2042 err = verify_compat_iovec(&msg_sys, iov,
2043 (struct sockaddr *)&addr,
2046 err = verify_iovec(&msg_sys, iov,
2047 (struct sockaddr *)&addr,
2053 cmsg_ptr = (unsigned long)msg_sys.msg_control;
2054 msg_sys.msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT);
2056 if (sock->file->f_flags & O_NONBLOCK)
2057 flags |= MSG_DONTWAIT;
2058 err = sock_recvmsg(sock, &msg_sys, total_len, flags);
2063 if (uaddr != NULL) {
2064 err = move_addr_to_user((struct sockaddr *)&addr,
2065 msg_sys.msg_namelen, uaddr,
2070 err = __put_user((msg_sys.msg_flags & ~MSG_CMSG_COMPAT),
2074 if (MSG_CMSG_COMPAT & flags)
2075 err = __put_user((unsigned long)msg_sys.msg_control - cmsg_ptr,
2076 &msg_compat->msg_controllen);
2078 err = __put_user((unsigned long)msg_sys.msg_control - cmsg_ptr,
2079 &msg->msg_controllen);
2085 if (iov != iovstack)
2086 sock_kfree_s(sock->sk, iov, iov_size);
2088 fput_light(sock->file, fput_needed);
2093 #ifdef __ARCH_WANT_SYS_SOCKETCALL
2095 /* Argument list sizes for sys_socketcall */
2096 #define AL(x) ((x) * sizeof(unsigned long))
2097 static const unsigned char nargs[19]={
2098 AL(0),AL(3),AL(3),AL(3),AL(2),AL(3),
2099 AL(3),AL(3),AL(4),AL(4),AL(4),AL(6),
2100 AL(6),AL(2),AL(5),AL(5),AL(3),AL(3),
2107 * System call vectors.
2109 * Argument checking cleaned up. Saved 20% in size.
2110 * This function doesn't need to set the kernel lock because
2111 * it is set by the callees.
2114 SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args)
2117 unsigned long a0, a1;
2121 if (call < 1 || call > SYS_ACCEPT4)
2125 if (len > sizeof(a))
2128 /* copy_from_user should be SMP safe. */
2129 if (copy_from_user(a, args, len))
2132 audit_socketcall(nargs[call] / sizeof(unsigned long), a);
2139 err = sys_socket(a0, a1, a[2]);
2142 err = sys_bind(a0, (struct sockaddr __user *)a1, a[2]);
2145 err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
2148 err = sys_listen(a0, a1);
2151 err = sys_accept4(a0, (struct sockaddr __user *)a1,
2152 (int __user *)a[2], 0);
2154 case SYS_GETSOCKNAME:
2156 sys_getsockname(a0, (struct sockaddr __user *)a1,
2157 (int __user *)a[2]);
2159 case SYS_GETPEERNAME:
2161 sys_getpeername(a0, (struct sockaddr __user *)a1,
2162 (int __user *)a[2]);
2164 case SYS_SOCKETPAIR:
2165 err = sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
2168 err = sys_send(a0, (void __user *)a1, a[2], a[3]);
2171 err = sys_sendto(a0, (void __user *)a1, a[2], a[3],
2172 (struct sockaddr __user *)a[4], a[5]);
2175 err = sys_recv(a0, (void __user *)a1, a[2], a[3]);
2178 err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
2179 (struct sockaddr __user *)a[4],
2180 (int __user *)a[5]);
2183 err = sys_shutdown(a0, a1);
2185 case SYS_SETSOCKOPT:
2186 err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]);
2188 case SYS_GETSOCKOPT:
2190 sys_getsockopt(a0, a1, a[2], (char __user *)a[3],
2191 (int __user *)a[4]);
2194 err = sys_sendmsg(a0, (struct msghdr __user *)a1, a[2]);
2197 err = sys_recvmsg(a0, (struct msghdr __user *)a1, a[2]);
2200 err = sys_accept4(a0, (struct sockaddr __user *)a1,
2201 (int __user *)a[2], a[3]);
2210 #endif /* __ARCH_WANT_SYS_SOCKETCALL */
2213 * sock_register - add a socket protocol handler
2214 * @ops: description of protocol
2216 * This function is called by a protocol handler that wants to
2217 * advertise its address family, and have it linked into the
2218 * socket interface. The value ops->family coresponds to the
2219 * socket system call protocol family.
2221 int sock_register(const struct net_proto_family *ops)
2225 if (ops->family >= NPROTO) {
2226 printk(KERN_CRIT "protocol %d >= NPROTO(%d)\n", ops->family,
2231 spin_lock(&net_family_lock);
2232 if (net_families[ops->family])
2235 net_families[ops->family] = ops;
2238 spin_unlock(&net_family_lock);
2240 printk(KERN_INFO "NET: Registered protocol family %d\n", ops->family);
2245 * sock_unregister - remove a protocol handler
2246 * @family: protocol family to remove
2248 * This function is called by a protocol handler that wants to
2249 * remove its address family, and have it unlinked from the
2250 * new socket creation.
2252 * If protocol handler is a module, then it can use module reference
2253 * counts to protect against new references. If protocol handler is not
2254 * a module then it needs to provide its own protection in
2255 * the ops->create routine.
2257 void sock_unregister(int family)
2259 BUG_ON(family < 0 || family >= NPROTO);
2261 spin_lock(&net_family_lock);
2262 net_families[family] = NULL;
2263 spin_unlock(&net_family_lock);
2267 printk(KERN_INFO "NET: Unregistered protocol family %d\n", family);
2270 static int __init sock_init(void)
2273 * Initialize sock SLAB cache.
2279 * Initialize skbuff SLAB cache
2284 * Initialize the protocols module.
2288 register_filesystem(&sock_fs_type);
2289 sock_mnt = kern_mount(&sock_fs_type);
2291 /* The real protocol initialization is performed in later initcalls.
2294 #ifdef CONFIG_NETFILTER
2301 core_initcall(sock_init); /* early initcall */
2303 #ifdef CONFIG_PROC_FS
2304 void socket_seq_show(struct seq_file *seq)
2309 for_each_possible_cpu(cpu)
2310 counter += per_cpu(sockets_in_use, cpu);
2312 /* It can be negative, by the way. 8) */
2316 seq_printf(seq, "sockets: used %d\n", counter);
2318 #endif /* CONFIG_PROC_FS */
2320 #ifdef CONFIG_COMPAT
2321 static long compat_sock_ioctl(struct file *file, unsigned cmd,
2324 struct socket *sock = file->private_data;
2325 int ret = -ENOIOCTLCMD;
2332 if (sock->ops->compat_ioctl)
2333 ret = sock->ops->compat_ioctl(sock, cmd, arg);
2335 if (ret == -ENOIOCTLCMD &&
2336 (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST))
2337 ret = compat_wext_handle_ioctl(net, cmd, arg);
2343 int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen)
2345 return sock->ops->bind(sock, addr, addrlen);
2348 int kernel_listen(struct socket *sock, int backlog)
2350 return sock->ops->listen(sock, backlog);
2353 int kernel_accept(struct socket *sock, struct socket **newsock, int flags)
2355 struct sock *sk = sock->sk;
2358 err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
2363 err = sock->ops->accept(sock, *newsock, flags);
2365 sock_release(*newsock);
2370 (*newsock)->ops = sock->ops;
2371 __module_get((*newsock)->ops->owner);
2377 int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
2380 return sock->ops->connect(sock, addr, addrlen, flags);
2383 int kernel_getsockname(struct socket *sock, struct sockaddr *addr,
2386 return sock->ops->getname(sock, addr, addrlen, 0);
2389 int kernel_getpeername(struct socket *sock, struct sockaddr *addr,
2392 return sock->ops->getname(sock, addr, addrlen, 1);
2395 int kernel_getsockopt(struct socket *sock, int level, int optname,
2396 char *optval, int *optlen)
2398 mm_segment_t oldfs = get_fs();
2402 if (level == SOL_SOCKET)
2403 err = sock_getsockopt(sock, level, optname, optval, optlen);
2405 err = sock->ops->getsockopt(sock, level, optname, optval,
2411 int kernel_setsockopt(struct socket *sock, int level, int optname,
2412 char *optval, unsigned int optlen)
2414 mm_segment_t oldfs = get_fs();
2418 if (level == SOL_SOCKET)
2419 err = sock_setsockopt(sock, level, optname, optval, optlen);
2421 err = sock->ops->setsockopt(sock, level, optname, optval,
2427 int kernel_sendpage(struct socket *sock, struct page *page, int offset,
2428 size_t size, int flags)
2430 if (sock->ops->sendpage)
2431 return sock->ops->sendpage(sock, page, offset, size, flags);
2433 return sock_no_sendpage(sock, page, offset, size, flags);
2436 int kernel_sock_ioctl(struct socket *sock, int cmd, unsigned long arg)
2438 mm_segment_t oldfs = get_fs();
2442 err = sock->ops->ioctl(sock, cmd, arg);
2448 int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how)
2450 return sock->ops->shutdown(sock, how);
2453 EXPORT_SYMBOL(sock_create);
2454 EXPORT_SYMBOL(sock_create_kern);
2455 EXPORT_SYMBOL(sock_create_lite);
2456 EXPORT_SYMBOL(sock_map_fd);
2457 EXPORT_SYMBOL(sock_recvmsg);
2458 EXPORT_SYMBOL(sock_register);
2459 EXPORT_SYMBOL(sock_release);
2460 EXPORT_SYMBOL(sock_sendmsg);
2461 EXPORT_SYMBOL(sock_unregister);
2462 EXPORT_SYMBOL(sock_wake_async);
2463 EXPORT_SYMBOL(sockfd_lookup);
2464 EXPORT_SYMBOL(kernel_sendmsg);
2465 EXPORT_SYMBOL(kernel_recvmsg);
2466 EXPORT_SYMBOL(kernel_bind);
2467 EXPORT_SYMBOL(kernel_listen);
2468 EXPORT_SYMBOL(kernel_accept);
2469 EXPORT_SYMBOL(kernel_connect);
2470 EXPORT_SYMBOL(kernel_getsockname);
2471 EXPORT_SYMBOL(kernel_getpeername);
2472 EXPORT_SYMBOL(kernel_getsockopt);
2473 EXPORT_SYMBOL(kernel_setsockopt);
2474 EXPORT_SYMBOL(kernel_sendpage);
2475 EXPORT_SYMBOL(kernel_sock_ioctl);
2476 EXPORT_SYMBOL(kernel_sock_shutdown);