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
;
1690 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1694 iov
.iov_base
= buff
;
1696 msg
.msg_name
= NULL
;
1699 msg
.msg_control
= NULL
;
1700 msg
.msg_controllen
= 0;
1701 msg
.msg_namelen
= 0;
1703 err
= move_addr_to_kernel(addr
, addr_len
, (struct sockaddr
*)&address
);
1706 msg
.msg_name
= (struct sockaddr
*)&address
;
1707 msg
.msg_namelen
= addr_len
;
1709 if (sock
->file
->f_flags
& O_NONBLOCK
)
1710 flags
|= MSG_DONTWAIT
;
1711 msg
.msg_flags
= flags
;
1712 err
= sock_sendmsg(sock
, &msg
, len
);
1715 fput_light(sock
->file
, fput_needed
);
1721 * Send a datagram down a socket.
1724 SYSCALL_DEFINE4(send
, int, fd
, void __user
*, buff
, size_t, len
,
1727 return sys_sendto(fd
, buff
, len
, flags
, NULL
, 0);
1731 * Receive a frame from the socket and optionally record the address of the
1732 * sender. We verify the buffers are writable and if needed move the
1733 * sender address from kernel to user space.
1736 SYSCALL_DEFINE6(recvfrom
, int, fd
, void __user
*, ubuf
, size_t, size
,
1737 unsigned, flags
, struct sockaddr __user
*, addr
,
1738 int __user
*, addr_len
)
1740 struct socket
*sock
;
1743 struct sockaddr_storage address
;
1747 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1751 msg
.msg_control
= NULL
;
1752 msg
.msg_controllen
= 0;
1756 iov
.iov_base
= ubuf
;
1757 msg
.msg_name
= (struct sockaddr
*)&address
;
1758 msg
.msg_namelen
= sizeof(address
);
1759 if (sock
->file
->f_flags
& O_NONBLOCK
)
1760 flags
|= MSG_DONTWAIT
;
1761 err
= sock_recvmsg(sock
, &msg
, size
, flags
);
1763 if (err
>= 0 && addr
!= NULL
) {
1764 err2
= move_addr_to_user((struct sockaddr
*)&address
,
1765 msg
.msg_namelen
, addr
, addr_len
);
1770 fput_light(sock
->file
, fput_needed
);
1776 * Receive a datagram from a socket.
1779 asmlinkage
long sys_recv(int fd
, void __user
*ubuf
, size_t size
,
1782 return sys_recvfrom(fd
, ubuf
, size
, flags
, NULL
, NULL
);
1786 * Set a socket option. Because we don't know the option lengths we have
1787 * to pass the user mode parameter for the protocols to sort out.
1790 SYSCALL_DEFINE5(setsockopt
, int, fd
, int, level
, int, optname
,
1791 char __user
*, optval
, int, optlen
)
1793 int err
, fput_needed
;
1794 struct socket
*sock
;
1799 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1801 err
= security_socket_setsockopt(sock
, level
, optname
);
1805 if (level
== SOL_SOCKET
)
1807 sock_setsockopt(sock
, level
, optname
, optval
,
1811 sock
->ops
->setsockopt(sock
, level
, optname
, optval
,
1814 fput_light(sock
->file
, fput_needed
);
1820 * Get a socket option. Because we don't know the option lengths we have
1821 * to pass a user mode parameter for the protocols to sort out.
1824 SYSCALL_DEFINE5(getsockopt
, int, fd
, int, level
, int, optname
,
1825 char __user
*, optval
, int __user
*, optlen
)
1827 int err
, fput_needed
;
1828 struct socket
*sock
;
1830 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1832 err
= security_socket_getsockopt(sock
, level
, optname
);
1836 if (level
== SOL_SOCKET
)
1838 sock_getsockopt(sock
, level
, optname
, optval
,
1842 sock
->ops
->getsockopt(sock
, level
, optname
, optval
,
1845 fput_light(sock
->file
, fput_needed
);
1851 * Shutdown a socket.
1854 SYSCALL_DEFINE2(shutdown
, int, fd
, int, how
)
1856 int err
, fput_needed
;
1857 struct socket
*sock
;
1859 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1861 err
= security_socket_shutdown(sock
, how
);
1863 err
= sock
->ops
->shutdown(sock
, how
);
1864 fput_light(sock
->file
, fput_needed
);
1869 /* A couple of helpful macros for getting the address of the 32/64 bit
1870 * fields which are the same type (int / unsigned) on our platforms.
1872 #define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
1873 #define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
1874 #define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
1877 * BSD sendmsg interface
1880 SYSCALL_DEFINE3(sendmsg
, int, fd
, struct msghdr __user
*, msg
, unsigned, flags
)
1882 struct compat_msghdr __user
*msg_compat
=
1883 (struct compat_msghdr __user
*)msg
;
1884 struct socket
*sock
;
1885 struct sockaddr_storage address
;
1886 struct iovec iovstack
[UIO_FASTIOV
], *iov
= iovstack
;
1887 unsigned char ctl
[sizeof(struct cmsghdr
) + 20]
1888 __attribute__ ((aligned(sizeof(__kernel_size_t
))));
1889 /* 20 is size of ipv6_pktinfo */
1890 unsigned char *ctl_buf
= ctl
;
1891 struct msghdr msg_sys
;
1892 int err
, ctl_len
, iov_size
, total_len
;
1896 if (MSG_CMSG_COMPAT
& flags
) {
1897 if (get_compat_msghdr(&msg_sys
, msg_compat
))
1900 else if (copy_from_user(&msg_sys
, msg
, sizeof(struct msghdr
)))
1903 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1907 /* do not move before msg_sys is valid */
1909 if (msg_sys
.msg_iovlen
> UIO_MAXIOV
)
1912 /* Check whether to allocate the iovec area */
1914 iov_size
= msg_sys
.msg_iovlen
* sizeof(struct iovec
);
1915 if (msg_sys
.msg_iovlen
> UIO_FASTIOV
) {
1916 iov
= sock_kmalloc(sock
->sk
, iov_size
, GFP_KERNEL
);
1921 /* This will also move the address data into kernel space */
1922 if (MSG_CMSG_COMPAT
& flags
) {
1923 err
= verify_compat_iovec(&msg_sys
, iov
,
1924 (struct sockaddr
*)&address
,
1927 err
= verify_iovec(&msg_sys
, iov
,
1928 (struct sockaddr
*)&address
,
1936 if (msg_sys
.msg_controllen
> INT_MAX
)
1938 ctl_len
= msg_sys
.msg_controllen
;
1939 if ((MSG_CMSG_COMPAT
& flags
) && ctl_len
) {
1941 cmsghdr_from_user_compat_to_kern(&msg_sys
, sock
->sk
, ctl
,
1945 ctl_buf
= msg_sys
.msg_control
;
1946 ctl_len
= msg_sys
.msg_controllen
;
1947 } else if (ctl_len
) {
1948 if (ctl_len
> sizeof(ctl
)) {
1949 ctl_buf
= sock_kmalloc(sock
->sk
, ctl_len
, GFP_KERNEL
);
1950 if (ctl_buf
== NULL
)
1955 * Careful! Before this, msg_sys.msg_control contains a user pointer.
1956 * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
1957 * checking falls down on this.
1959 if (copy_from_user(ctl_buf
, (void __user
*)msg_sys
.msg_control
,
1962 msg_sys
.msg_control
= ctl_buf
;
1964 msg_sys
.msg_flags
= flags
;
1966 if (sock
->file
->f_flags
& O_NONBLOCK
)
1967 msg_sys
.msg_flags
|= MSG_DONTWAIT
;
1968 err
= sock_sendmsg(sock
, &msg_sys
, total_len
);
1972 sock_kfree_s(sock
->sk
, ctl_buf
, ctl_len
);
1974 if (iov
!= iovstack
)
1975 sock_kfree_s(sock
->sk
, iov
, iov_size
);
1977 fput_light(sock
->file
, fput_needed
);
1983 * BSD recvmsg interface
1986 SYSCALL_DEFINE3(recvmsg
, int, fd
, struct msghdr __user
*, msg
,
1987 unsigned int, flags
)
1989 struct compat_msghdr __user
*msg_compat
=
1990 (struct compat_msghdr __user
*)msg
;
1991 struct socket
*sock
;
1992 struct iovec iovstack
[UIO_FASTIOV
];
1993 struct iovec
*iov
= iovstack
;
1994 struct msghdr msg_sys
;
1995 unsigned long cmsg_ptr
;
1996 int err
, iov_size
, total_len
, len
;
1999 /* kernel mode address */
2000 struct sockaddr_storage addr
;
2002 /* user mode address pointers */
2003 struct sockaddr __user
*uaddr
;
2004 int __user
*uaddr_len
;
2006 if (MSG_CMSG_COMPAT
& flags
) {
2007 if (get_compat_msghdr(&msg_sys
, msg_compat
))
2010 else if (copy_from_user(&msg_sys
, msg
, sizeof(struct msghdr
)))
2013 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
2018 if (msg_sys
.msg_iovlen
> UIO_MAXIOV
)
2021 /* Check whether to allocate the iovec area */
2023 iov_size
= msg_sys
.msg_iovlen
* sizeof(struct iovec
);
2024 if (msg_sys
.msg_iovlen
> UIO_FASTIOV
) {
2025 iov
= sock_kmalloc(sock
->sk
, iov_size
, GFP_KERNEL
);
2031 * Save the user-mode address (verify_iovec will change the
2032 * kernel msghdr to use the kernel address space)
2035 uaddr
= (__force
void __user
*)msg_sys
.msg_name
;
2036 uaddr_len
= COMPAT_NAMELEN(msg
);
2037 if (MSG_CMSG_COMPAT
& flags
) {
2038 err
= verify_compat_iovec(&msg_sys
, iov
,
2039 (struct sockaddr
*)&addr
,
2042 err
= verify_iovec(&msg_sys
, iov
,
2043 (struct sockaddr
*)&addr
,
2049 cmsg_ptr
= (unsigned long)msg_sys
.msg_control
;
2050 msg_sys
.msg_flags
= flags
& (MSG_CMSG_CLOEXEC
|MSG_CMSG_COMPAT
);
2052 if (sock
->file
->f_flags
& O_NONBLOCK
)
2053 flags
|= MSG_DONTWAIT
;
2054 err
= sock_recvmsg(sock
, &msg_sys
, total_len
, flags
);
2059 if (uaddr
!= NULL
) {
2060 err
= move_addr_to_user((struct sockaddr
*)&addr
,
2061 msg_sys
.msg_namelen
, uaddr
,
2066 err
= __put_user((msg_sys
.msg_flags
& ~MSG_CMSG_COMPAT
),
2070 if (MSG_CMSG_COMPAT
& flags
)
2071 err
= __put_user((unsigned long)msg_sys
.msg_control
- cmsg_ptr
,
2072 &msg_compat
->msg_controllen
);
2074 err
= __put_user((unsigned long)msg_sys
.msg_control
- cmsg_ptr
,
2075 &msg
->msg_controllen
);
2081 if (iov
!= iovstack
)
2082 sock_kfree_s(sock
->sk
, iov
, iov_size
);
2084 fput_light(sock
->file
, fput_needed
);
2089 #ifdef __ARCH_WANT_SYS_SOCKETCALL
2091 /* Argument list sizes for sys_socketcall */
2092 #define AL(x) ((x) * sizeof(unsigned long))
2093 static const unsigned char nargs
[19]={
2094 AL(0),AL(3),AL(3),AL(3),AL(2),AL(3),
2095 AL(3),AL(3),AL(4),AL(4),AL(4),AL(6),
2096 AL(6),AL(2),AL(5),AL(5),AL(3),AL(3),
2103 * System call vectors.
2105 * Argument checking cleaned up. Saved 20% in size.
2106 * This function doesn't need to set the kernel lock because
2107 * it is set by the callees.
2110 SYSCALL_DEFINE2(socketcall
, int, call
, unsigned long __user
*, args
)
2113 unsigned long a0
, a1
;
2117 if (call
< 1 || call
> SYS_ACCEPT4
)
2121 if (len
> sizeof(a
))
2124 /* copy_from_user should be SMP safe. */
2125 if (copy_from_user(a
, args
, len
))
2128 audit_socketcall(nargs
[call
] / sizeof(unsigned long), a
);
2135 err
= sys_socket(a0
, a1
, a
[2]);
2138 err
= sys_bind(a0
, (struct sockaddr __user
*)a1
, a
[2]);
2141 err
= sys_connect(a0
, (struct sockaddr __user
*)a1
, a
[2]);
2144 err
= sys_listen(a0
, a1
);
2147 err
= sys_accept4(a0
, (struct sockaddr __user
*)a1
,
2148 (int __user
*)a
[2], 0);
2150 case SYS_GETSOCKNAME
:
2152 sys_getsockname(a0
, (struct sockaddr __user
*)a1
,
2153 (int __user
*)a
[2]);
2155 case SYS_GETPEERNAME
:
2157 sys_getpeername(a0
, (struct sockaddr __user
*)a1
,
2158 (int __user
*)a
[2]);
2160 case SYS_SOCKETPAIR
:
2161 err
= sys_socketpair(a0
, a1
, a
[2], (int __user
*)a
[3]);
2164 err
= sys_send(a0
, (void __user
*)a1
, a
[2], a
[3]);
2167 err
= sys_sendto(a0
, (void __user
*)a1
, a
[2], a
[3],
2168 (struct sockaddr __user
*)a
[4], a
[5]);
2171 err
= sys_recv(a0
, (void __user
*)a1
, a
[2], a
[3]);
2174 err
= sys_recvfrom(a0
, (void __user
*)a1
, a
[2], a
[3],
2175 (struct sockaddr __user
*)a
[4],
2176 (int __user
*)a
[5]);
2179 err
= sys_shutdown(a0
, a1
);
2181 case SYS_SETSOCKOPT
:
2182 err
= sys_setsockopt(a0
, a1
, a
[2], (char __user
*)a
[3], a
[4]);
2184 case SYS_GETSOCKOPT
:
2186 sys_getsockopt(a0
, a1
, a
[2], (char __user
*)a
[3],
2187 (int __user
*)a
[4]);
2190 err
= sys_sendmsg(a0
, (struct msghdr __user
*)a1
, a
[2]);
2193 err
= sys_recvmsg(a0
, (struct msghdr __user
*)a1
, a
[2]);
2196 err
= sys_accept4(a0
, (struct sockaddr __user
*)a1
,
2197 (int __user
*)a
[2], a
[3]);
2206 #endif /* __ARCH_WANT_SYS_SOCKETCALL */
2209 * sock_register - add a socket protocol handler
2210 * @ops: description of protocol
2212 * This function is called by a protocol handler that wants to
2213 * advertise its address family, and have it linked into the
2214 * socket interface. The value ops->family coresponds to the
2215 * socket system call protocol family.
2217 int sock_register(const struct net_proto_family
*ops
)
2221 if (ops
->family
>= NPROTO
) {
2222 printk(KERN_CRIT
"protocol %d >= NPROTO(%d)\n", ops
->family
,
2227 spin_lock(&net_family_lock
);
2228 if (net_families
[ops
->family
])
2231 net_families
[ops
->family
] = ops
;
2234 spin_unlock(&net_family_lock
);
2236 printk(KERN_INFO
"NET: Registered protocol family %d\n", ops
->family
);
2241 * sock_unregister - remove a protocol handler
2242 * @family: protocol family to remove
2244 * This function is called by a protocol handler that wants to
2245 * remove its address family, and have it unlinked from the
2246 * new socket creation.
2248 * If protocol handler is a module, then it can use module reference
2249 * counts to protect against new references. If protocol handler is not
2250 * a module then it needs to provide its own protection in
2251 * the ops->create routine.
2253 void sock_unregister(int family
)
2255 BUG_ON(family
< 0 || family
>= NPROTO
);
2257 spin_lock(&net_family_lock
);
2258 net_families
[family
] = NULL
;
2259 spin_unlock(&net_family_lock
);
2263 printk(KERN_INFO
"NET: Unregistered protocol family %d\n", family
);
2266 static int __init
sock_init(void)
2269 * Initialize sock SLAB cache.
2275 * Initialize skbuff SLAB cache
2280 * Initialize the protocols module.
2284 register_filesystem(&sock_fs_type
);
2285 sock_mnt
= kern_mount(&sock_fs_type
);
2287 /* The real protocol initialization is performed in later initcalls.
2290 #ifdef CONFIG_NETFILTER
2297 core_initcall(sock_init
); /* early initcall */
2299 #ifdef CONFIG_PROC_FS
2300 void socket_seq_show(struct seq_file
*seq
)
2305 for_each_possible_cpu(cpu
)
2306 counter
+= per_cpu(sockets_in_use
, cpu
);
2308 /* It can be negative, by the way. 8) */
2312 seq_printf(seq
, "sockets: used %d\n", counter
);
2314 #endif /* CONFIG_PROC_FS */
2316 #ifdef CONFIG_COMPAT
2317 static long compat_sock_ioctl(struct file
*file
, unsigned cmd
,
2320 struct socket
*sock
= file
->private_data
;
2321 int ret
= -ENOIOCTLCMD
;
2328 if (sock
->ops
->compat_ioctl
)
2329 ret
= sock
->ops
->compat_ioctl(sock
, cmd
, arg
);
2331 if (ret
== -ENOIOCTLCMD
&&
2332 (cmd
>= SIOCIWFIRST
&& cmd
<= SIOCIWLAST
))
2333 ret
= compat_wext_handle_ioctl(net
, cmd
, arg
);
2339 int kernel_bind(struct socket
*sock
, struct sockaddr
*addr
, int addrlen
)
2341 return sock
->ops
->bind(sock
, addr
, addrlen
);
2344 int kernel_listen(struct socket
*sock
, int backlog
)
2346 return sock
->ops
->listen(sock
, backlog
);
2349 int kernel_accept(struct socket
*sock
, struct socket
**newsock
, int flags
)
2351 struct sock
*sk
= sock
->sk
;
2354 err
= sock_create_lite(sk
->sk_family
, sk
->sk_type
, sk
->sk_protocol
,
2359 err
= sock
->ops
->accept(sock
, *newsock
, flags
);
2361 sock_release(*newsock
);
2366 (*newsock
)->ops
= sock
->ops
;
2367 __module_get((*newsock
)->ops
->owner
);
2373 int kernel_connect(struct socket
*sock
, struct sockaddr
*addr
, int addrlen
,
2376 return sock
->ops
->connect(sock
, addr
, addrlen
, flags
);
2379 int kernel_getsockname(struct socket
*sock
, struct sockaddr
*addr
,
2382 return sock
->ops
->getname(sock
, addr
, addrlen
, 0);
2385 int kernel_getpeername(struct socket
*sock
, struct sockaddr
*addr
,
2388 return sock
->ops
->getname(sock
, addr
, addrlen
, 1);
2391 int kernel_getsockopt(struct socket
*sock
, int level
, int optname
,
2392 char *optval
, int *optlen
)
2394 mm_segment_t oldfs
= get_fs();
2398 if (level
== SOL_SOCKET
)
2399 err
= sock_getsockopt(sock
, level
, optname
, optval
, optlen
);
2401 err
= sock
->ops
->getsockopt(sock
, level
, optname
, optval
,
2407 int kernel_setsockopt(struct socket
*sock
, int level
, int optname
,
2408 char *optval
, unsigned int optlen
)
2410 mm_segment_t oldfs
= get_fs();
2414 if (level
== SOL_SOCKET
)
2415 err
= sock_setsockopt(sock
, level
, optname
, optval
, optlen
);
2417 err
= sock
->ops
->setsockopt(sock
, level
, optname
, optval
,
2423 int kernel_sendpage(struct socket
*sock
, struct page
*page
, int offset
,
2424 size_t size
, int flags
)
2426 if (sock
->ops
->sendpage
)
2427 return sock
->ops
->sendpage(sock
, page
, offset
, size
, flags
);
2429 return sock_no_sendpage(sock
, page
, offset
, size
, flags
);
2432 int kernel_sock_ioctl(struct socket
*sock
, int cmd
, unsigned long arg
)
2434 mm_segment_t oldfs
= get_fs();
2438 err
= sock
->ops
->ioctl(sock
, cmd
, arg
);
2444 int kernel_sock_shutdown(struct socket
*sock
, enum sock_shutdown_cmd how
)
2446 return sock
->ops
->shutdown(sock
, how
);
2449 EXPORT_SYMBOL(sock_create
);
2450 EXPORT_SYMBOL(sock_create_kern
);
2451 EXPORT_SYMBOL(sock_create_lite
);
2452 EXPORT_SYMBOL(sock_map_fd
);
2453 EXPORT_SYMBOL(sock_recvmsg
);
2454 EXPORT_SYMBOL(sock_register
);
2455 EXPORT_SYMBOL(sock_release
);
2456 EXPORT_SYMBOL(sock_sendmsg
);
2457 EXPORT_SYMBOL(sock_unregister
);
2458 EXPORT_SYMBOL(sock_wake_async
);
2459 EXPORT_SYMBOL(sockfd_lookup
);
2460 EXPORT_SYMBOL(kernel_sendmsg
);
2461 EXPORT_SYMBOL(kernel_recvmsg
);
2462 EXPORT_SYMBOL(kernel_bind
);
2463 EXPORT_SYMBOL(kernel_listen
);
2464 EXPORT_SYMBOL(kernel_accept
);
2465 EXPORT_SYMBOL(kernel_connect
);
2466 EXPORT_SYMBOL(kernel_getsockname
);
2467 EXPORT_SYMBOL(kernel_getpeername
);
2468 EXPORT_SYMBOL(kernel_getsockopt
);
2469 EXPORT_SYMBOL(kernel_setsockopt
);
2470 EXPORT_SYMBOL(kernel_sendpage
);
2471 EXPORT_SYMBOL(kernel_sock_ioctl
);
2472 EXPORT_SYMBOL(kernel_sock_shutdown
);