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>
90 #include <linux/slab.h>
92 #include <asm/uaccess.h>
93 #include <asm/unistd.h>
95 #include <net/compat.h>
99 #include <linux/netfilter.h>
101 #include <linux/if_tun.h>
102 #include <linux/ipv6_route.h>
103 #include <linux/route.h>
104 #include <linux/sockios.h>
105 #include <linux/atalk.h>
107 static int sock_no_open(struct inode
*irrelevant
, struct file
*dontcare
);
108 static ssize_t
sock_aio_read(struct kiocb
*iocb
, const struct iovec
*iov
,
109 unsigned long nr_segs
, loff_t pos
);
110 static ssize_t
sock_aio_write(struct kiocb
*iocb
, const struct iovec
*iov
,
111 unsigned long nr_segs
, loff_t pos
);
112 static int sock_mmap(struct file
*file
, struct vm_area_struct
*vma
);
114 static int sock_close(struct inode
*inode
, struct file
*file
);
115 static unsigned int sock_poll(struct file
*file
,
116 struct poll_table_struct
*wait
);
117 static long sock_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
);
119 static long compat_sock_ioctl(struct file
*file
,
120 unsigned int cmd
, unsigned long arg
);
122 static int sock_fasync(int fd
, struct file
*filp
, int on
);
123 static ssize_t
sock_sendpage(struct file
*file
, struct page
*page
,
124 int offset
, size_t size
, loff_t
*ppos
, int more
);
125 static ssize_t
sock_splice_read(struct file
*file
, loff_t
*ppos
,
126 struct pipe_inode_info
*pipe
, size_t len
,
130 * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
131 * in the operation structures but are done directly via the socketcall() multiplexor.
134 static const struct file_operations socket_file_ops
= {
135 .owner
= THIS_MODULE
,
137 .aio_read
= sock_aio_read
,
138 .aio_write
= sock_aio_write
,
140 .unlocked_ioctl
= sock_ioctl
,
142 .compat_ioctl
= compat_sock_ioctl
,
145 .open
= sock_no_open
, /* special open code to disallow open via /proc */
146 .release
= sock_close
,
147 .fasync
= sock_fasync
,
148 .sendpage
= sock_sendpage
,
149 .splice_write
= generic_splice_sendpage
,
150 .splice_read
= sock_splice_read
,
154 * The protocol list. Each protocol is registered in here.
157 static DEFINE_SPINLOCK(net_family_lock
);
158 static const struct net_proto_family
*net_families
[NPROTO
] __read_mostly
;
161 * Statistics counters of the socket lists
164 static DEFINE_PER_CPU(int, sockets_in_use
) = 0;
168 * Move socket addresses back and forth across the kernel/user
169 * divide and look after the messy bits.
172 #define MAX_SOCK_ADDR 128 /* 108 for Unix domain -
173 16 for IP, 16 for IPX,
176 must be at least one bigger than
177 the AF_UNIX size (see net/unix/af_unix.c
182 * move_addr_to_kernel - copy a socket address into kernel space
183 * @uaddr: Address in user space
184 * @kaddr: Address in kernel space
185 * @ulen: Length in user space
187 * The address is copied into kernel space. If the provided address is
188 * too long an error code of -EINVAL is returned. If the copy gives
189 * invalid addresses -EFAULT is returned. On a success 0 is returned.
192 int move_addr_to_kernel(void __user
*uaddr
, int ulen
, struct sockaddr
*kaddr
)
194 if (ulen
< 0 || ulen
> sizeof(struct sockaddr_storage
))
198 if (copy_from_user(kaddr
, uaddr
, ulen
))
200 return audit_sockaddr(ulen
, kaddr
);
204 * move_addr_to_user - copy an address to user space
205 * @kaddr: kernel space address
206 * @klen: length of address in kernel
207 * @uaddr: user space address
208 * @ulen: pointer to user length field
210 * The value pointed to by ulen on entry is the buffer length available.
211 * This is overwritten with the buffer space used. -EINVAL is returned
212 * if an overlong buffer is specified or a negative buffer size. -EFAULT
213 * is returned if either the buffer or the length field are not
215 * After copying the data up to the limit the user specifies, the true
216 * length of the data is written over the length limit the user
217 * specified. Zero is returned for a success.
220 int move_addr_to_user(struct sockaddr
*kaddr
, int klen
, void __user
*uaddr
,
226 err
= get_user(len
, ulen
);
231 if (len
< 0 || len
> sizeof(struct sockaddr_storage
))
234 if (audit_sockaddr(klen
, kaddr
))
236 if (copy_to_user(uaddr
, kaddr
, len
))
240 * "fromlen shall refer to the value before truncation.."
243 return __put_user(klen
, ulen
);
246 static struct kmem_cache
*sock_inode_cachep __read_mostly
;
248 static struct inode
*sock_alloc_inode(struct super_block
*sb
)
250 struct socket_alloc
*ei
;
252 ei
= kmem_cache_alloc(sock_inode_cachep
, GFP_KERNEL
);
255 init_waitqueue_head(&ei
->socket
.wait
);
257 ei
->socket
.fasync_list
= NULL
;
258 ei
->socket
.state
= SS_UNCONNECTED
;
259 ei
->socket
.flags
= 0;
260 ei
->socket
.ops
= NULL
;
261 ei
->socket
.sk
= NULL
;
262 ei
->socket
.file
= NULL
;
264 return &ei
->vfs_inode
;
267 static void sock_destroy_inode(struct inode
*inode
)
269 kmem_cache_free(sock_inode_cachep
,
270 container_of(inode
, struct socket_alloc
, vfs_inode
));
273 static void init_once(void *foo
)
275 struct socket_alloc
*ei
= (struct socket_alloc
*)foo
;
277 inode_init_once(&ei
->vfs_inode
);
280 static int init_inodecache(void)
282 sock_inode_cachep
= kmem_cache_create("sock_inode_cache",
283 sizeof(struct socket_alloc
),
285 (SLAB_HWCACHE_ALIGN
|
286 SLAB_RECLAIM_ACCOUNT
|
289 if (sock_inode_cachep
== NULL
)
294 static const struct super_operations sockfs_ops
= {
295 .alloc_inode
= sock_alloc_inode
,
296 .destroy_inode
=sock_destroy_inode
,
297 .statfs
= simple_statfs
,
300 static int sockfs_get_sb(struct file_system_type
*fs_type
,
301 int flags
, const char *dev_name
, void *data
,
302 struct vfsmount
*mnt
)
304 return get_sb_pseudo(fs_type
, "socket:", &sockfs_ops
, SOCKFS_MAGIC
,
308 static struct vfsmount
*sock_mnt __read_mostly
;
310 static struct file_system_type sock_fs_type
= {
312 .get_sb
= sockfs_get_sb
,
313 .kill_sb
= kill_anon_super
,
317 * sockfs_dname() is called from d_path().
319 static char *sockfs_dname(struct dentry
*dentry
, char *buffer
, int buflen
)
321 return dynamic_dname(dentry
, buffer
, buflen
, "socket:[%lu]",
322 dentry
->d_inode
->i_ino
);
325 static const struct dentry_operations sockfs_dentry_operations
= {
326 .d_dname
= sockfs_dname
,
330 * Obtains the first available file descriptor and sets it up for use.
332 * These functions create file structures and maps them to fd space
333 * of the current process. On success it returns file descriptor
334 * and file struct implicitly stored in sock->file.
335 * Note that another thread may close file descriptor before we return
336 * from this function. We use the fact that now we do not refer
337 * to socket after mapping. If one day we will need it, this
338 * function will increment ref. count on file by 1.
340 * In any case returned fd MAY BE not valid!
341 * This race condition is unavoidable
342 * with shared fd spaces, we cannot solve it inside kernel,
343 * but we take care of internal coherence yet.
346 static int sock_alloc_file(struct socket
*sock
, struct file
**f
, int flags
)
348 struct qstr name
= { .name
= "" };
353 fd
= get_unused_fd_flags(flags
);
354 if (unlikely(fd
< 0))
357 path
.dentry
= d_alloc(sock_mnt
->mnt_sb
->s_root
, &name
);
358 if (unlikely(!path
.dentry
)) {
362 path
.mnt
= mntget(sock_mnt
);
364 path
.dentry
->d_op
= &sockfs_dentry_operations
;
365 d_instantiate(path
.dentry
, SOCK_INODE(sock
));
366 SOCK_INODE(sock
)->i_fop
= &socket_file_ops
;
368 file
= alloc_file(&path
, FMODE_READ
| FMODE_WRITE
,
370 if (unlikely(!file
)) {
371 /* drop dentry, keep inode */
372 atomic_inc(&path
.dentry
->d_inode
->i_count
);
379 file
->f_flags
= O_RDWR
| (flags
& O_NONBLOCK
);
381 file
->private_data
= sock
;
387 int sock_map_fd(struct socket
*sock
, int flags
)
389 struct file
*newfile
;
390 int fd
= sock_alloc_file(sock
, &newfile
, flags
);
393 fd_install(fd
, newfile
);
398 static struct socket
*sock_from_file(struct file
*file
, int *err
)
400 if (file
->f_op
== &socket_file_ops
)
401 return file
->private_data
; /* set in sock_map_fd */
408 * sockfd_lookup - Go from a file number to its socket slot
410 * @err: pointer to an error code return
412 * The file handle passed in is locked and the socket it is bound
413 * too is returned. If an error occurs the err pointer is overwritten
414 * with a negative errno code and NULL is returned. The function checks
415 * for both invalid handles and passing a handle which is not a socket.
417 * On a success the socket object pointer is returned.
420 struct socket
*sockfd_lookup(int fd
, int *err
)
431 sock
= sock_from_file(file
, err
);
437 static struct socket
*sockfd_lookup_light(int fd
, int *err
, int *fput_needed
)
443 file
= fget_light(fd
, fput_needed
);
445 sock
= sock_from_file(file
, err
);
448 fput_light(file
, *fput_needed
);
454 * sock_alloc - allocate a socket
456 * Allocate a new inode and socket object. The two are bound together
457 * and initialised. The socket is then returned. If we are out of inodes
461 static struct socket
*sock_alloc(void)
466 inode
= new_inode(sock_mnt
->mnt_sb
);
470 sock
= SOCKET_I(inode
);
472 kmemcheck_annotate_bitfield(sock
, type
);
473 inode
->i_mode
= S_IFSOCK
| S_IRWXUGO
;
474 inode
->i_uid
= current_fsuid();
475 inode
->i_gid
= current_fsgid();
477 percpu_add(sockets_in_use
, 1);
482 * In theory you can't get an open on this inode, but /proc provides
483 * a back door. Remember to keep it shut otherwise you'll let the
484 * creepy crawlies in.
487 static int sock_no_open(struct inode
*irrelevant
, struct file
*dontcare
)
492 const struct file_operations bad_sock_fops
= {
493 .owner
= THIS_MODULE
,
494 .open
= sock_no_open
,
498 * sock_release - close a socket
499 * @sock: socket to close
501 * The socket is released from the protocol stack if it has a release
502 * callback, and the inode is then released if the socket is bound to
503 * an inode not a file.
506 void sock_release(struct socket
*sock
)
509 struct module
*owner
= sock
->ops
->owner
;
511 sock
->ops
->release(sock
);
516 if (sock
->fasync_list
)
517 printk(KERN_ERR
"sock_release: fasync list not empty!\n");
519 percpu_sub(sockets_in_use
, 1);
521 iput(SOCK_INODE(sock
));
527 int sock_tx_timestamp(struct msghdr
*msg
, struct sock
*sk
,
528 union skb_shared_tx
*shtx
)
531 if (sock_flag(sk
, SOCK_TIMESTAMPING_TX_HARDWARE
))
533 if (sock_flag(sk
, SOCK_TIMESTAMPING_TX_SOFTWARE
))
537 EXPORT_SYMBOL(sock_tx_timestamp
);
539 static inline int __sock_sendmsg(struct kiocb
*iocb
, struct socket
*sock
,
540 struct msghdr
*msg
, size_t size
)
542 struct sock_iocb
*si
= kiocb_to_siocb(iocb
);
550 err
= security_socket_sendmsg(sock
, msg
, size
);
554 return sock
->ops
->sendmsg(iocb
, sock
, msg
, size
);
557 int sock_sendmsg(struct socket
*sock
, struct msghdr
*msg
, size_t size
)
560 struct sock_iocb siocb
;
563 init_sync_kiocb(&iocb
, NULL
);
564 iocb
.private = &siocb
;
565 ret
= __sock_sendmsg(&iocb
, sock
, msg
, size
);
566 if (-EIOCBQUEUED
== ret
)
567 ret
= wait_on_sync_kiocb(&iocb
);
571 int kernel_sendmsg(struct socket
*sock
, struct msghdr
*msg
,
572 struct kvec
*vec
, size_t num
, size_t size
)
574 mm_segment_t oldfs
= get_fs();
579 * the following is safe, since for compiler definitions of kvec and
580 * iovec are identical, yielding the same in-core layout and alignment
582 msg
->msg_iov
= (struct iovec
*)vec
;
583 msg
->msg_iovlen
= num
;
584 result
= sock_sendmsg(sock
, msg
, size
);
589 static int ktime2ts(ktime_t kt
, struct timespec
*ts
)
592 *ts
= ktime_to_timespec(kt
);
600 * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
602 void __sock_recv_timestamp(struct msghdr
*msg
, struct sock
*sk
,
605 int need_software_tstamp
= sock_flag(sk
, SOCK_RCVTSTAMP
);
606 struct timespec ts
[3];
608 struct skb_shared_hwtstamps
*shhwtstamps
=
611 /* Race occurred between timestamp enabling and packet
612 receiving. Fill in the current time for now. */
613 if (need_software_tstamp
&& skb
->tstamp
.tv64
== 0)
614 __net_timestamp(skb
);
616 if (need_software_tstamp
) {
617 if (!sock_flag(sk
, SOCK_RCVTSTAMPNS
)) {
619 skb_get_timestamp(skb
, &tv
);
620 put_cmsg(msg
, SOL_SOCKET
, SCM_TIMESTAMP
,
624 skb_get_timestampns(skb
, &ts
);
625 put_cmsg(msg
, SOL_SOCKET
, SCM_TIMESTAMPNS
,
631 memset(ts
, 0, sizeof(ts
));
632 if (skb
->tstamp
.tv64
&&
633 sock_flag(sk
, SOCK_TIMESTAMPING_SOFTWARE
)) {
634 skb_get_timestampns(skb
, ts
+ 0);
638 if (sock_flag(sk
, SOCK_TIMESTAMPING_SYS_HARDWARE
) &&
639 ktime2ts(shhwtstamps
->syststamp
, ts
+ 1))
641 if (sock_flag(sk
, SOCK_TIMESTAMPING_RAW_HARDWARE
) &&
642 ktime2ts(shhwtstamps
->hwtstamp
, ts
+ 2))
646 put_cmsg(msg
, SOL_SOCKET
,
647 SCM_TIMESTAMPING
, sizeof(ts
), &ts
);
650 EXPORT_SYMBOL_GPL(__sock_recv_timestamp
);
652 inline void sock_recv_drops(struct msghdr
*msg
, struct sock
*sk
, struct sk_buff
*skb
)
654 if (sock_flag(sk
, SOCK_RXQ_OVFL
) && skb
&& skb
->dropcount
)
655 put_cmsg(msg
, SOL_SOCKET
, SO_RXQ_OVFL
,
656 sizeof(__u32
), &skb
->dropcount
);
659 void sock_recv_ts_and_drops(struct msghdr
*msg
, struct sock
*sk
,
662 sock_recv_timestamp(msg
, sk
, skb
);
663 sock_recv_drops(msg
, sk
, skb
);
665 EXPORT_SYMBOL_GPL(sock_recv_ts_and_drops
);
667 static inline int __sock_recvmsg_nosec(struct kiocb
*iocb
, struct socket
*sock
,
668 struct msghdr
*msg
, size_t size
, int flags
)
670 struct sock_iocb
*si
= kiocb_to_siocb(iocb
);
678 return sock
->ops
->recvmsg(iocb
, sock
, msg
, size
, flags
);
681 static inline int __sock_recvmsg(struct kiocb
*iocb
, struct socket
*sock
,
682 struct msghdr
*msg
, size_t size
, int flags
)
684 int err
= security_socket_recvmsg(sock
, msg
, size
, flags
);
686 return err
?: __sock_recvmsg_nosec(iocb
, sock
, msg
, size
, flags
);
689 int sock_recvmsg(struct socket
*sock
, struct msghdr
*msg
,
690 size_t size
, int flags
)
693 struct sock_iocb siocb
;
696 init_sync_kiocb(&iocb
, NULL
);
697 iocb
.private = &siocb
;
698 ret
= __sock_recvmsg(&iocb
, sock
, msg
, size
, flags
);
699 if (-EIOCBQUEUED
== ret
)
700 ret
= wait_on_sync_kiocb(&iocb
);
704 static int sock_recvmsg_nosec(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_nosec(&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
);
903 static long sock_do_ioctl(struct net
*net
, struct socket
*sock
,
904 unsigned int cmd
, unsigned long arg
)
907 void __user
*argp
= (void __user
*)arg
;
909 err
= sock
->ops
->ioctl(sock
, cmd
, arg
);
912 * If this ioctl is unknown try to hand it down
915 if (err
== -ENOIOCTLCMD
)
916 err
= dev_ioctl(net
, cmd
, argp
);
922 * With an ioctl, arg may well be a user mode pointer, but we don't know
923 * what to do with it - that's up to the protocol still.
926 static long sock_ioctl(struct file
*file
, unsigned cmd
, unsigned long arg
)
930 void __user
*argp
= (void __user
*)arg
;
934 sock
= file
->private_data
;
937 if (cmd
>= SIOCDEVPRIVATE
&& cmd
<= (SIOCDEVPRIVATE
+ 15)) {
938 err
= dev_ioctl(net
, cmd
, argp
);
940 #ifdef CONFIG_WEXT_CORE
941 if (cmd
>= SIOCIWFIRST
&& cmd
<= SIOCIWLAST
) {
942 err
= dev_ioctl(net
, cmd
, argp
);
949 if (get_user(pid
, (int __user
*)argp
))
951 err
= f_setown(sock
->file
, pid
, 1);
955 err
= put_user(f_getown(sock
->file
),
964 request_module("bridge");
966 mutex_lock(&br_ioctl_mutex
);
968 err
= br_ioctl_hook(net
, cmd
, argp
);
969 mutex_unlock(&br_ioctl_mutex
);
974 if (!vlan_ioctl_hook
)
975 request_module("8021q");
977 mutex_lock(&vlan_ioctl_mutex
);
979 err
= vlan_ioctl_hook(net
, argp
);
980 mutex_unlock(&vlan_ioctl_mutex
);
985 if (!dlci_ioctl_hook
)
986 request_module("dlci");
988 mutex_lock(&dlci_ioctl_mutex
);
990 err
= dlci_ioctl_hook(cmd
, argp
);
991 mutex_unlock(&dlci_ioctl_mutex
);
994 err
= sock_do_ioctl(net
, sock
, cmd
, arg
);
1000 int sock_create_lite(int family
, int type
, int protocol
, struct socket
**res
)
1003 struct socket
*sock
= NULL
;
1005 err
= security_socket_create(family
, type
, protocol
, 1);
1009 sock
= sock_alloc();
1016 err
= security_socket_post_create(sock
, family
, type
, protocol
, 1);
1029 /* No kernel lock held - perfect */
1030 static unsigned int sock_poll(struct file
*file
, poll_table
*wait
)
1032 struct socket
*sock
;
1035 * We can't return errors to poll, so it's either yes or no.
1037 sock
= file
->private_data
;
1038 return sock
->ops
->poll(file
, sock
, wait
);
1041 static int sock_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1043 struct socket
*sock
= file
->private_data
;
1045 return sock
->ops
->mmap(file
, sock
, vma
);
1048 static int sock_close(struct inode
*inode
, struct file
*filp
)
1051 * It was possible the inode is NULL we were
1052 * closing an unfinished socket.
1056 printk(KERN_DEBUG
"sock_close: NULL inode\n");
1059 sock_release(SOCKET_I(inode
));
1064 * Update the socket async list
1066 * Fasync_list locking strategy.
1068 * 1. fasync_list is modified only under process context socket lock
1069 * i.e. under semaphore.
1070 * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
1071 * or under socket lock.
1072 * 3. fasync_list can be used from softirq context, so that
1073 * modification under socket lock have to be enhanced with
1074 * write_lock_bh(&sk->sk_callback_lock).
1078 static int sock_fasync(int fd
, struct file
*filp
, int on
)
1080 struct fasync_struct
*fa
, *fna
= NULL
, **prev
;
1081 struct socket
*sock
;
1085 fna
= kmalloc(sizeof(struct fasync_struct
), GFP_KERNEL
);
1090 sock
= filp
->private_data
;
1100 spin_lock(&filp
->f_lock
);
1102 filp
->f_flags
|= FASYNC
;
1104 filp
->f_flags
&= ~FASYNC
;
1105 spin_unlock(&filp
->f_lock
);
1107 prev
= &(sock
->fasync_list
);
1109 for (fa
= *prev
; fa
!= NULL
; prev
= &fa
->fa_next
, fa
= *prev
)
1110 if (fa
->fa_file
== filp
)
1115 write_lock_bh(&sk
->sk_callback_lock
);
1117 write_unlock_bh(&sk
->sk_callback_lock
);
1122 fna
->fa_file
= filp
;
1124 fna
->magic
= FASYNC_MAGIC
;
1125 fna
->fa_next
= sock
->fasync_list
;
1126 write_lock_bh(&sk
->sk_callback_lock
);
1127 sock
->fasync_list
= fna
;
1128 sock_set_flag(sk
, SOCK_FASYNC
);
1129 write_unlock_bh(&sk
->sk_callback_lock
);
1132 write_lock_bh(&sk
->sk_callback_lock
);
1133 *prev
= fa
->fa_next
;
1134 if (!sock
->fasync_list
)
1135 sock_reset_flag(sk
, SOCK_FASYNC
);
1136 write_unlock_bh(&sk
->sk_callback_lock
);
1142 release_sock(sock
->sk
);
1146 /* This function may be called only under socket lock or callback_lock */
1148 int sock_wake_async(struct socket
*sock
, int how
, int band
)
1150 if (!sock
|| !sock
->fasync_list
)
1153 case SOCK_WAKE_WAITD
:
1154 if (test_bit(SOCK_ASYNC_WAITDATA
, &sock
->flags
))
1157 case SOCK_WAKE_SPACE
:
1158 if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE
, &sock
->flags
))
1163 __kill_fasync(sock
->fasync_list
, SIGIO
, band
);
1166 __kill_fasync(sock
->fasync_list
, SIGURG
, band
);
1171 static int __sock_create(struct net
*net
, int family
, int type
, int protocol
,
1172 struct socket
**res
, int kern
)
1175 struct socket
*sock
;
1176 const struct net_proto_family
*pf
;
1179 * Check protocol is in range
1181 if (family
< 0 || family
>= NPROTO
)
1182 return -EAFNOSUPPORT
;
1183 if (type
< 0 || type
>= SOCK_MAX
)
1188 This uglymoron is moved from INET layer to here to avoid
1189 deadlock in module load.
1191 if (family
== PF_INET
&& type
== SOCK_PACKET
) {
1195 printk(KERN_INFO
"%s uses obsolete (PF_INET,SOCK_PACKET)\n",
1201 err
= security_socket_create(family
, type
, protocol
, kern
);
1206 * Allocate the socket and allow the family to set things up. if
1207 * the protocol is 0, the family is instructed to select an appropriate
1210 sock
= sock_alloc();
1212 if (net_ratelimit())
1213 printk(KERN_WARNING
"socket: no more sockets\n");
1214 return -ENFILE
; /* Not exactly a match, but its the
1215 closest posix thing */
1220 #ifdef CONFIG_MODULES
1221 /* Attempt to load a protocol module if the find failed.
1223 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
1224 * requested real, full-featured networking support upon configuration.
1225 * Otherwise module support will break!
1227 if (net_families
[family
] == NULL
)
1228 request_module("net-pf-%d", family
);
1232 pf
= rcu_dereference(net_families
[family
]);
1233 err
= -EAFNOSUPPORT
;
1238 * We will call the ->create function, that possibly is in a loadable
1239 * module, so we have to bump that loadable module refcnt first.
1241 if (!try_module_get(pf
->owner
))
1244 /* Now protected by module ref count */
1247 err
= pf
->create(net
, sock
, protocol
, kern
);
1249 goto out_module_put
;
1252 * Now to bump the refcnt of the [loadable] module that owns this
1253 * socket at sock_release time we decrement its refcnt.
1255 if (!try_module_get(sock
->ops
->owner
))
1256 goto out_module_busy
;
1259 * Now that we're done with the ->create function, the [loadable]
1260 * module can have its refcnt decremented
1262 module_put(pf
->owner
);
1263 err
= security_socket_post_create(sock
, family
, type
, protocol
, kern
);
1265 goto out_sock_release
;
1271 err
= -EAFNOSUPPORT
;
1274 module_put(pf
->owner
);
1281 goto out_sock_release
;
1284 int sock_create(int family
, int type
, int protocol
, struct socket
**res
)
1286 return __sock_create(current
->nsproxy
->net_ns
, family
, type
, protocol
, res
, 0);
1289 int sock_create_kern(int family
, int type
, int protocol
, struct socket
**res
)
1291 return __sock_create(&init_net
, family
, type
, protocol
, res
, 1);
1294 SYSCALL_DEFINE3(socket
, int, family
, int, type
, int, protocol
)
1297 struct socket
*sock
;
1300 /* Check the SOCK_* constants for consistency. */
1301 BUILD_BUG_ON(SOCK_CLOEXEC
!= O_CLOEXEC
);
1302 BUILD_BUG_ON((SOCK_MAX
| SOCK_TYPE_MASK
) != SOCK_TYPE_MASK
);
1303 BUILD_BUG_ON(SOCK_CLOEXEC
& SOCK_TYPE_MASK
);
1304 BUILD_BUG_ON(SOCK_NONBLOCK
& SOCK_TYPE_MASK
);
1306 flags
= type
& ~SOCK_TYPE_MASK
;
1307 if (flags
& ~(SOCK_CLOEXEC
| SOCK_NONBLOCK
))
1309 type
&= SOCK_TYPE_MASK
;
1311 if (SOCK_NONBLOCK
!= O_NONBLOCK
&& (flags
& SOCK_NONBLOCK
))
1312 flags
= (flags
& ~SOCK_NONBLOCK
) | O_NONBLOCK
;
1314 retval
= sock_create(family
, type
, protocol
, &sock
);
1318 retval
= sock_map_fd(sock
, flags
& (O_CLOEXEC
| O_NONBLOCK
));
1323 /* It may be already another descriptor 8) Not kernel problem. */
1332 * Create a pair of connected sockets.
1335 SYSCALL_DEFINE4(socketpair
, int, family
, int, type
, int, protocol
,
1336 int __user
*, usockvec
)
1338 struct socket
*sock1
, *sock2
;
1340 struct file
*newfile1
, *newfile2
;
1343 flags
= type
& ~SOCK_TYPE_MASK
;
1344 if (flags
& ~(SOCK_CLOEXEC
| SOCK_NONBLOCK
))
1346 type
&= SOCK_TYPE_MASK
;
1348 if (SOCK_NONBLOCK
!= O_NONBLOCK
&& (flags
& SOCK_NONBLOCK
))
1349 flags
= (flags
& ~SOCK_NONBLOCK
) | O_NONBLOCK
;
1352 * Obtain the first socket and check if the underlying protocol
1353 * supports the socketpair call.
1356 err
= sock_create(family
, type
, protocol
, &sock1
);
1360 err
= sock_create(family
, type
, protocol
, &sock2
);
1364 err
= sock1
->ops
->socketpair(sock1
, sock2
);
1366 goto out_release_both
;
1368 fd1
= sock_alloc_file(sock1
, &newfile1
, flags
);
1369 if (unlikely(fd1
< 0)) {
1371 goto out_release_both
;
1374 fd2
= sock_alloc_file(sock2
, &newfile2
, flags
);
1375 if (unlikely(fd2
< 0)) {
1379 sock_release(sock2
);
1383 audit_fd_pair(fd1
, fd2
);
1384 fd_install(fd1
, newfile1
);
1385 fd_install(fd2
, newfile2
);
1386 /* fd1 and fd2 may be already another descriptors.
1387 * Not kernel problem.
1390 err
= put_user(fd1
, &usockvec
[0]);
1392 err
= put_user(fd2
, &usockvec
[1]);
1401 sock_release(sock2
);
1403 sock_release(sock1
);
1409 * Bind a name to a socket. Nothing much to do here since it's
1410 * the protocol's responsibility to handle the local address.
1412 * We move the socket address to kernel space before we call
1413 * the protocol layer (having also checked the address is ok).
1416 SYSCALL_DEFINE3(bind
, int, fd
, struct sockaddr __user
*, umyaddr
, int, addrlen
)
1418 struct socket
*sock
;
1419 struct sockaddr_storage address
;
1420 int err
, fput_needed
;
1422 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1424 err
= move_addr_to_kernel(umyaddr
, addrlen
, (struct sockaddr
*)&address
);
1426 err
= security_socket_bind(sock
,
1427 (struct sockaddr
*)&address
,
1430 err
= sock
->ops
->bind(sock
,
1434 fput_light(sock
->file
, fput_needed
);
1440 * Perform a listen. Basically, we allow the protocol to do anything
1441 * necessary for a listen, and if that works, we mark the socket as
1442 * ready for listening.
1445 SYSCALL_DEFINE2(listen
, int, fd
, int, backlog
)
1447 struct socket
*sock
;
1448 int err
, fput_needed
;
1451 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1453 somaxconn
= sock_net(sock
->sk
)->core
.sysctl_somaxconn
;
1454 if ((unsigned)backlog
> somaxconn
)
1455 backlog
= somaxconn
;
1457 err
= security_socket_listen(sock
, backlog
);
1459 err
= sock
->ops
->listen(sock
, backlog
);
1461 fput_light(sock
->file
, fput_needed
);
1467 * For accept, we attempt to create a new socket, set up the link
1468 * with the client, wake up the client, then return the new
1469 * connected fd. We collect the address of the connector in kernel
1470 * space and move it to user at the very end. This is unclean because
1471 * we open the socket then return an error.
1473 * 1003.1g adds the ability to recvmsg() to query connection pending
1474 * status to recvmsg. We need to add that support in a way thats
1475 * clean when we restucture accept also.
1478 SYSCALL_DEFINE4(accept4
, int, fd
, struct sockaddr __user
*, upeer_sockaddr
,
1479 int __user
*, upeer_addrlen
, int, flags
)
1481 struct socket
*sock
, *newsock
;
1482 struct file
*newfile
;
1483 int err
, len
, newfd
, fput_needed
;
1484 struct sockaddr_storage address
;
1486 if (flags
& ~(SOCK_CLOEXEC
| SOCK_NONBLOCK
))
1489 if (SOCK_NONBLOCK
!= O_NONBLOCK
&& (flags
& SOCK_NONBLOCK
))
1490 flags
= (flags
& ~SOCK_NONBLOCK
) | O_NONBLOCK
;
1492 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1497 if (!(newsock
= sock_alloc()))
1500 newsock
->type
= sock
->type
;
1501 newsock
->ops
= sock
->ops
;
1504 * We don't need try_module_get here, as the listening socket (sock)
1505 * has the protocol module (sock->ops->owner) held.
1507 __module_get(newsock
->ops
->owner
);
1509 newfd
= sock_alloc_file(newsock
, &newfile
, flags
);
1510 if (unlikely(newfd
< 0)) {
1512 sock_release(newsock
);
1516 err
= security_socket_accept(sock
, newsock
);
1520 err
= sock
->ops
->accept(sock
, newsock
, sock
->file
->f_flags
);
1524 if (upeer_sockaddr
) {
1525 if (newsock
->ops
->getname(newsock
, (struct sockaddr
*)&address
,
1527 err
= -ECONNABORTED
;
1530 err
= move_addr_to_user((struct sockaddr
*)&address
,
1531 len
, upeer_sockaddr
, upeer_addrlen
);
1536 /* File flags are not inherited via accept() unlike another OSes. */
1538 fd_install(newfd
, newfile
);
1542 fput_light(sock
->file
, fput_needed
);
1547 put_unused_fd(newfd
);
1551 SYSCALL_DEFINE3(accept
, int, fd
, struct sockaddr __user
*, upeer_sockaddr
,
1552 int __user
*, upeer_addrlen
)
1554 return sys_accept4(fd
, upeer_sockaddr
, upeer_addrlen
, 0);
1558 * Attempt to connect to a socket with the server address. The address
1559 * is in user space so we verify it is OK and move it to kernel space.
1561 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
1564 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
1565 * other SEQPACKET protocols that take time to connect() as it doesn't
1566 * include the -EINPROGRESS status for such sockets.
1569 SYSCALL_DEFINE3(connect
, int, fd
, struct sockaddr __user
*, uservaddr
,
1572 struct socket
*sock
;
1573 struct sockaddr_storage address
;
1574 int err
, fput_needed
;
1576 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1579 err
= move_addr_to_kernel(uservaddr
, addrlen
, (struct sockaddr
*)&address
);
1584 security_socket_connect(sock
, (struct sockaddr
*)&address
, addrlen
);
1588 err
= sock
->ops
->connect(sock
, (struct sockaddr
*)&address
, addrlen
,
1589 sock
->file
->f_flags
);
1591 fput_light(sock
->file
, fput_needed
);
1597 * Get the local address ('name') of a socket object. Move the obtained
1598 * name to user space.
1601 SYSCALL_DEFINE3(getsockname
, int, fd
, struct sockaddr __user
*, usockaddr
,
1602 int __user
*, usockaddr_len
)
1604 struct socket
*sock
;
1605 struct sockaddr_storage address
;
1606 int len
, err
, fput_needed
;
1608 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1612 err
= security_socket_getsockname(sock
);
1616 err
= sock
->ops
->getname(sock
, (struct sockaddr
*)&address
, &len
, 0);
1619 err
= move_addr_to_user((struct sockaddr
*)&address
, len
, usockaddr
, usockaddr_len
);
1622 fput_light(sock
->file
, fput_needed
);
1628 * Get the remote address ('name') of a socket object. Move the obtained
1629 * name to user space.
1632 SYSCALL_DEFINE3(getpeername
, int, fd
, struct sockaddr __user
*, usockaddr
,
1633 int __user
*, usockaddr_len
)
1635 struct socket
*sock
;
1636 struct sockaddr_storage address
;
1637 int len
, err
, fput_needed
;
1639 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1641 err
= security_socket_getpeername(sock
);
1643 fput_light(sock
->file
, fput_needed
);
1648 sock
->ops
->getname(sock
, (struct sockaddr
*)&address
, &len
,
1651 err
= move_addr_to_user((struct sockaddr
*)&address
, len
, usockaddr
,
1653 fput_light(sock
->file
, fput_needed
);
1659 * Send a datagram to a given address. We move the address into kernel
1660 * space and check the user space data area is readable before invoking
1664 SYSCALL_DEFINE6(sendto
, int, fd
, void __user
*, buff
, size_t, len
,
1665 unsigned, flags
, struct sockaddr __user
*, addr
,
1668 struct socket
*sock
;
1669 struct sockaddr_storage address
;
1675 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1679 iov
.iov_base
= buff
;
1681 msg
.msg_name
= NULL
;
1684 msg
.msg_control
= NULL
;
1685 msg
.msg_controllen
= 0;
1686 msg
.msg_namelen
= 0;
1688 err
= move_addr_to_kernel(addr
, addr_len
, (struct sockaddr
*)&address
);
1691 msg
.msg_name
= (struct sockaddr
*)&address
;
1692 msg
.msg_namelen
= addr_len
;
1694 if (sock
->file
->f_flags
& O_NONBLOCK
)
1695 flags
|= MSG_DONTWAIT
;
1696 msg
.msg_flags
= flags
;
1697 err
= sock_sendmsg(sock
, &msg
, len
);
1700 fput_light(sock
->file
, fput_needed
);
1706 * Send a datagram down a socket.
1709 SYSCALL_DEFINE4(send
, int, fd
, void __user
*, buff
, size_t, len
,
1712 return sys_sendto(fd
, buff
, len
, flags
, NULL
, 0);
1716 * Receive a frame from the socket and optionally record the address of the
1717 * sender. We verify the buffers are writable and if needed move the
1718 * sender address from kernel to user space.
1721 SYSCALL_DEFINE6(recvfrom
, int, fd
, void __user
*, ubuf
, size_t, size
,
1722 unsigned, flags
, struct sockaddr __user
*, addr
,
1723 int __user
*, addr_len
)
1725 struct socket
*sock
;
1728 struct sockaddr_storage address
;
1732 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1736 msg
.msg_control
= NULL
;
1737 msg
.msg_controllen
= 0;
1741 iov
.iov_base
= ubuf
;
1742 msg
.msg_name
= (struct sockaddr
*)&address
;
1743 msg
.msg_namelen
= sizeof(address
);
1744 if (sock
->file
->f_flags
& O_NONBLOCK
)
1745 flags
|= MSG_DONTWAIT
;
1746 err
= sock_recvmsg(sock
, &msg
, size
, flags
);
1748 if (err
>= 0 && addr
!= NULL
) {
1749 err2
= move_addr_to_user((struct sockaddr
*)&address
,
1750 msg
.msg_namelen
, addr
, addr_len
);
1755 fput_light(sock
->file
, fput_needed
);
1761 * Receive a datagram from a socket.
1764 asmlinkage
long sys_recv(int fd
, void __user
*ubuf
, size_t size
,
1767 return sys_recvfrom(fd
, ubuf
, size
, flags
, NULL
, NULL
);
1771 * Set a socket option. Because we don't know the option lengths we have
1772 * to pass the user mode parameter for the protocols to sort out.
1775 SYSCALL_DEFINE5(setsockopt
, int, fd
, int, level
, int, optname
,
1776 char __user
*, optval
, int, optlen
)
1778 int err
, fput_needed
;
1779 struct socket
*sock
;
1784 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1786 err
= security_socket_setsockopt(sock
, level
, optname
);
1790 if (level
== SOL_SOCKET
)
1792 sock_setsockopt(sock
, level
, optname
, optval
,
1796 sock
->ops
->setsockopt(sock
, level
, optname
, optval
,
1799 fput_light(sock
->file
, fput_needed
);
1805 * Get a socket option. Because we don't know the option lengths we have
1806 * to pass a user mode parameter for the protocols to sort out.
1809 SYSCALL_DEFINE5(getsockopt
, int, fd
, int, level
, int, optname
,
1810 char __user
*, optval
, int __user
*, optlen
)
1812 int err
, fput_needed
;
1813 struct socket
*sock
;
1815 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1817 err
= security_socket_getsockopt(sock
, level
, optname
);
1821 if (level
== SOL_SOCKET
)
1823 sock_getsockopt(sock
, level
, optname
, optval
,
1827 sock
->ops
->getsockopt(sock
, level
, optname
, optval
,
1830 fput_light(sock
->file
, fput_needed
);
1836 * Shutdown a socket.
1839 SYSCALL_DEFINE2(shutdown
, int, fd
, int, how
)
1841 int err
, fput_needed
;
1842 struct socket
*sock
;
1844 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1846 err
= security_socket_shutdown(sock
, how
);
1848 err
= sock
->ops
->shutdown(sock
, how
);
1849 fput_light(sock
->file
, fput_needed
);
1854 /* A couple of helpful macros for getting the address of the 32/64 bit
1855 * fields which are the same type (int / unsigned) on our platforms.
1857 #define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
1858 #define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
1859 #define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
1862 * BSD sendmsg interface
1865 SYSCALL_DEFINE3(sendmsg
, int, fd
, struct msghdr __user
*, msg
, unsigned, flags
)
1867 struct compat_msghdr __user
*msg_compat
=
1868 (struct compat_msghdr __user
*)msg
;
1869 struct socket
*sock
;
1870 struct sockaddr_storage address
;
1871 struct iovec iovstack
[UIO_FASTIOV
], *iov
= iovstack
;
1872 unsigned char ctl
[sizeof(struct cmsghdr
) + 20]
1873 __attribute__ ((aligned(sizeof(__kernel_size_t
))));
1874 /* 20 is size of ipv6_pktinfo */
1875 unsigned char *ctl_buf
= ctl
;
1876 struct msghdr msg_sys
;
1877 int err
, ctl_len
, iov_size
, total_len
;
1881 if (MSG_CMSG_COMPAT
& flags
) {
1882 if (get_compat_msghdr(&msg_sys
, msg_compat
))
1885 else if (copy_from_user(&msg_sys
, msg
, sizeof(struct msghdr
)))
1888 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1892 /* do not move before msg_sys is valid */
1894 if (msg_sys
.msg_iovlen
> UIO_MAXIOV
)
1897 /* Check whether to allocate the iovec area */
1899 iov_size
= msg_sys
.msg_iovlen
* sizeof(struct iovec
);
1900 if (msg_sys
.msg_iovlen
> UIO_FASTIOV
) {
1901 iov
= sock_kmalloc(sock
->sk
, iov_size
, GFP_KERNEL
);
1906 /* This will also move the address data into kernel space */
1907 if (MSG_CMSG_COMPAT
& flags
) {
1908 err
= verify_compat_iovec(&msg_sys
, iov
,
1909 (struct sockaddr
*)&address
,
1912 err
= verify_iovec(&msg_sys
, iov
,
1913 (struct sockaddr
*)&address
,
1921 if (msg_sys
.msg_controllen
> INT_MAX
)
1923 ctl_len
= msg_sys
.msg_controllen
;
1924 if ((MSG_CMSG_COMPAT
& flags
) && ctl_len
) {
1926 cmsghdr_from_user_compat_to_kern(&msg_sys
, sock
->sk
, ctl
,
1930 ctl_buf
= msg_sys
.msg_control
;
1931 ctl_len
= msg_sys
.msg_controllen
;
1932 } else if (ctl_len
) {
1933 if (ctl_len
> sizeof(ctl
)) {
1934 ctl_buf
= sock_kmalloc(sock
->sk
, ctl_len
, GFP_KERNEL
);
1935 if (ctl_buf
== NULL
)
1940 * Careful! Before this, msg_sys.msg_control contains a user pointer.
1941 * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
1942 * checking falls down on this.
1944 if (copy_from_user(ctl_buf
, (void __user
*)msg_sys
.msg_control
,
1947 msg_sys
.msg_control
= ctl_buf
;
1949 msg_sys
.msg_flags
= flags
;
1951 if (sock
->file
->f_flags
& O_NONBLOCK
)
1952 msg_sys
.msg_flags
|= MSG_DONTWAIT
;
1953 err
= sock_sendmsg(sock
, &msg_sys
, total_len
);
1957 sock_kfree_s(sock
->sk
, ctl_buf
, ctl_len
);
1959 if (iov
!= iovstack
)
1960 sock_kfree_s(sock
->sk
, iov
, iov_size
);
1962 fput_light(sock
->file
, fput_needed
);
1967 static int __sys_recvmsg(struct socket
*sock
, struct msghdr __user
*msg
,
1968 struct msghdr
*msg_sys
, unsigned flags
, int nosec
)
1970 struct compat_msghdr __user
*msg_compat
=
1971 (struct compat_msghdr __user
*)msg
;
1972 struct iovec iovstack
[UIO_FASTIOV
];
1973 struct iovec
*iov
= iovstack
;
1974 unsigned long cmsg_ptr
;
1975 int err
, iov_size
, total_len
, len
;
1977 /* kernel mode address */
1978 struct sockaddr_storage addr
;
1980 /* user mode address pointers */
1981 struct sockaddr __user
*uaddr
;
1982 int __user
*uaddr_len
;
1984 if (MSG_CMSG_COMPAT
& flags
) {
1985 if (get_compat_msghdr(msg_sys
, msg_compat
))
1988 else if (copy_from_user(msg_sys
, msg
, sizeof(struct msghdr
)))
1992 if (msg_sys
->msg_iovlen
> UIO_MAXIOV
)
1995 /* Check whether to allocate the iovec area */
1997 iov_size
= msg_sys
->msg_iovlen
* sizeof(struct iovec
);
1998 if (msg_sys
->msg_iovlen
> UIO_FASTIOV
) {
1999 iov
= sock_kmalloc(sock
->sk
, iov_size
, GFP_KERNEL
);
2005 * Save the user-mode address (verify_iovec will change the
2006 * kernel msghdr to use the kernel address space)
2009 uaddr
= (__force
void __user
*)msg_sys
->msg_name
;
2010 uaddr_len
= COMPAT_NAMELEN(msg
);
2011 if (MSG_CMSG_COMPAT
& flags
) {
2012 err
= verify_compat_iovec(msg_sys
, iov
,
2013 (struct sockaddr
*)&addr
,
2016 err
= verify_iovec(msg_sys
, iov
,
2017 (struct sockaddr
*)&addr
,
2023 cmsg_ptr
= (unsigned long)msg_sys
->msg_control
;
2024 msg_sys
->msg_flags
= flags
& (MSG_CMSG_CLOEXEC
|MSG_CMSG_COMPAT
);
2026 if (sock
->file
->f_flags
& O_NONBLOCK
)
2027 flags
|= MSG_DONTWAIT
;
2028 err
= (nosec
? sock_recvmsg_nosec
: sock_recvmsg
)(sock
, msg_sys
,
2034 if (uaddr
!= NULL
) {
2035 err
= move_addr_to_user((struct sockaddr
*)&addr
,
2036 msg_sys
->msg_namelen
, uaddr
,
2041 err
= __put_user((msg_sys
->msg_flags
& ~MSG_CMSG_COMPAT
),
2045 if (MSG_CMSG_COMPAT
& flags
)
2046 err
= __put_user((unsigned long)msg_sys
->msg_control
- cmsg_ptr
,
2047 &msg_compat
->msg_controllen
);
2049 err
= __put_user((unsigned long)msg_sys
->msg_control
- cmsg_ptr
,
2050 &msg
->msg_controllen
);
2056 if (iov
!= iovstack
)
2057 sock_kfree_s(sock
->sk
, iov
, iov_size
);
2063 * BSD recvmsg interface
2066 SYSCALL_DEFINE3(recvmsg
, int, fd
, struct msghdr __user
*, msg
,
2067 unsigned int, flags
)
2069 int fput_needed
, err
;
2070 struct msghdr msg_sys
;
2071 struct socket
*sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
2076 err
= __sys_recvmsg(sock
, msg
, &msg_sys
, flags
, 0);
2078 fput_light(sock
->file
, fput_needed
);
2084 * Linux recvmmsg interface
2087 int __sys_recvmmsg(int fd
, struct mmsghdr __user
*mmsg
, unsigned int vlen
,
2088 unsigned int flags
, struct timespec
*timeout
)
2090 int fput_needed
, err
, datagrams
;
2091 struct socket
*sock
;
2092 struct mmsghdr __user
*entry
;
2093 struct compat_mmsghdr __user
*compat_entry
;
2094 struct msghdr msg_sys
;
2095 struct timespec end_time
;
2098 poll_select_set_timeout(&end_time
, timeout
->tv_sec
,
2104 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
2108 err
= sock_error(sock
->sk
);
2113 compat_entry
= (struct compat_mmsghdr __user
*)mmsg
;
2115 while (datagrams
< vlen
) {
2117 * No need to ask LSM for more than the first datagram.
2119 if (MSG_CMSG_COMPAT
& flags
) {
2120 err
= __sys_recvmsg(sock
, (struct msghdr __user
*)compat_entry
,
2121 &msg_sys
, flags
, datagrams
);
2124 err
= __put_user(err
, &compat_entry
->msg_len
);
2127 err
= __sys_recvmsg(sock
, (struct msghdr __user
*)entry
,
2128 &msg_sys
, flags
, datagrams
);
2131 err
= put_user(err
, &entry
->msg_len
);
2139 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
2140 if (flags
& MSG_WAITFORONE
)
2141 flags
|= MSG_DONTWAIT
;
2144 ktime_get_ts(timeout
);
2145 *timeout
= timespec_sub(end_time
, *timeout
);
2146 if (timeout
->tv_sec
< 0) {
2147 timeout
->tv_sec
= timeout
->tv_nsec
= 0;
2151 /* Timeout, return less than vlen datagrams */
2152 if (timeout
->tv_nsec
== 0 && timeout
->tv_sec
== 0)
2156 /* Out of band data, return right away */
2157 if (msg_sys
.msg_flags
& MSG_OOB
)
2162 fput_light(sock
->file
, fput_needed
);
2167 if (datagrams
!= 0) {
2169 * We may return less entries than requested (vlen) if the
2170 * sock is non block and there aren't enough datagrams...
2172 if (err
!= -EAGAIN
) {
2174 * ... or if recvmsg returns an error after we
2175 * received some datagrams, where we record the
2176 * error to return on the next call or if the
2177 * app asks about it using getsockopt(SO_ERROR).
2179 sock
->sk
->sk_err
= -err
;
2188 SYSCALL_DEFINE5(recvmmsg
, int, fd
, struct mmsghdr __user
*, mmsg
,
2189 unsigned int, vlen
, unsigned int, flags
,
2190 struct timespec __user
*, timeout
)
2193 struct timespec timeout_sys
;
2196 return __sys_recvmmsg(fd
, mmsg
, vlen
, flags
, NULL
);
2198 if (copy_from_user(&timeout_sys
, timeout
, sizeof(timeout_sys
)))
2201 datagrams
= __sys_recvmmsg(fd
, mmsg
, vlen
, flags
, &timeout_sys
);
2203 if (datagrams
> 0 &&
2204 copy_to_user(timeout
, &timeout_sys
, sizeof(timeout_sys
)))
2205 datagrams
= -EFAULT
;
2210 #ifdef __ARCH_WANT_SYS_SOCKETCALL
2211 /* Argument list sizes for sys_socketcall */
2212 #define AL(x) ((x) * sizeof(unsigned long))
2213 static const unsigned char nargs
[20] = {
2214 AL(0),AL(3),AL(3),AL(3),AL(2),AL(3),
2215 AL(3),AL(3),AL(4),AL(4),AL(4),AL(6),
2216 AL(6),AL(2),AL(5),AL(5),AL(3),AL(3),
2223 * System call vectors.
2225 * Argument checking cleaned up. Saved 20% in size.
2226 * This function doesn't need to set the kernel lock because
2227 * it is set by the callees.
2230 SYSCALL_DEFINE2(socketcall
, int, call
, unsigned long __user
*, args
)
2233 unsigned long a0
, a1
;
2237 if (call
< 1 || call
> SYS_RECVMMSG
)
2241 if (len
> sizeof(a
))
2244 /* copy_from_user should be SMP safe. */
2245 if (copy_from_user(a
, args
, len
))
2248 audit_socketcall(nargs
[call
] / sizeof(unsigned long), a
);
2255 err
= sys_socket(a0
, a1
, a
[2]);
2258 err
= sys_bind(a0
, (struct sockaddr __user
*)a1
, a
[2]);
2261 err
= sys_connect(a0
, (struct sockaddr __user
*)a1
, a
[2]);
2264 err
= sys_listen(a0
, a1
);
2267 err
= sys_accept4(a0
, (struct sockaddr __user
*)a1
,
2268 (int __user
*)a
[2], 0);
2270 case SYS_GETSOCKNAME
:
2272 sys_getsockname(a0
, (struct sockaddr __user
*)a1
,
2273 (int __user
*)a
[2]);
2275 case SYS_GETPEERNAME
:
2277 sys_getpeername(a0
, (struct sockaddr __user
*)a1
,
2278 (int __user
*)a
[2]);
2280 case SYS_SOCKETPAIR
:
2281 err
= sys_socketpair(a0
, a1
, a
[2], (int __user
*)a
[3]);
2284 err
= sys_send(a0
, (void __user
*)a1
, a
[2], a
[3]);
2287 err
= sys_sendto(a0
, (void __user
*)a1
, a
[2], a
[3],
2288 (struct sockaddr __user
*)a
[4], a
[5]);
2291 err
= sys_recv(a0
, (void __user
*)a1
, a
[2], a
[3]);
2294 err
= sys_recvfrom(a0
, (void __user
*)a1
, a
[2], a
[3],
2295 (struct sockaddr __user
*)a
[4],
2296 (int __user
*)a
[5]);
2299 err
= sys_shutdown(a0
, a1
);
2301 case SYS_SETSOCKOPT
:
2302 err
= sys_setsockopt(a0
, a1
, a
[2], (char __user
*)a
[3], a
[4]);
2304 case SYS_GETSOCKOPT
:
2306 sys_getsockopt(a0
, a1
, a
[2], (char __user
*)a
[3],
2307 (int __user
*)a
[4]);
2310 err
= sys_sendmsg(a0
, (struct msghdr __user
*)a1
, a
[2]);
2313 err
= sys_recvmsg(a0
, (struct msghdr __user
*)a1
, a
[2]);
2316 err
= sys_recvmmsg(a0
, (struct mmsghdr __user
*)a1
, a
[2], a
[3],
2317 (struct timespec __user
*)a
[4]);
2320 err
= sys_accept4(a0
, (struct sockaddr __user
*)a1
,
2321 (int __user
*)a
[2], a
[3]);
2330 #endif /* __ARCH_WANT_SYS_SOCKETCALL */
2333 * sock_register - add a socket protocol handler
2334 * @ops: description of protocol
2336 * This function is called by a protocol handler that wants to
2337 * advertise its address family, and have it linked into the
2338 * socket interface. The value ops->family coresponds to the
2339 * socket system call protocol family.
2341 int sock_register(const struct net_proto_family
*ops
)
2345 if (ops
->family
>= NPROTO
) {
2346 printk(KERN_CRIT
"protocol %d >= NPROTO(%d)\n", ops
->family
,
2351 spin_lock(&net_family_lock
);
2352 if (net_families
[ops
->family
])
2355 net_families
[ops
->family
] = ops
;
2358 spin_unlock(&net_family_lock
);
2360 printk(KERN_INFO
"NET: Registered protocol family %d\n", ops
->family
);
2365 * sock_unregister - remove a protocol handler
2366 * @family: protocol family to remove
2368 * This function is called by a protocol handler that wants to
2369 * remove its address family, and have it unlinked from the
2370 * new socket creation.
2372 * If protocol handler is a module, then it can use module reference
2373 * counts to protect against new references. If protocol handler is not
2374 * a module then it needs to provide its own protection in
2375 * the ops->create routine.
2377 void sock_unregister(int family
)
2379 BUG_ON(family
< 0 || family
>= NPROTO
);
2381 spin_lock(&net_family_lock
);
2382 net_families
[family
] = NULL
;
2383 spin_unlock(&net_family_lock
);
2387 printk(KERN_INFO
"NET: Unregistered protocol family %d\n", family
);
2390 static int __init
sock_init(void)
2393 * Initialize sock SLAB cache.
2399 * Initialize skbuff SLAB cache
2404 * Initialize the protocols module.
2408 register_filesystem(&sock_fs_type
);
2409 sock_mnt
= kern_mount(&sock_fs_type
);
2411 /* The real protocol initialization is performed in later initcalls.
2414 #ifdef CONFIG_NETFILTER
2421 core_initcall(sock_init
); /* early initcall */
2423 #ifdef CONFIG_PROC_FS
2424 void socket_seq_show(struct seq_file
*seq
)
2429 for_each_possible_cpu(cpu
)
2430 counter
+= per_cpu(sockets_in_use
, cpu
);
2432 /* It can be negative, by the way. 8) */
2436 seq_printf(seq
, "sockets: used %d\n", counter
);
2438 #endif /* CONFIG_PROC_FS */
2440 #ifdef CONFIG_COMPAT
2441 static int do_siocgstamp(struct net
*net
, struct socket
*sock
,
2442 unsigned int cmd
, struct compat_timeval __user
*up
)
2444 mm_segment_t old_fs
= get_fs();
2449 err
= sock_do_ioctl(net
, sock
, cmd
, (unsigned long)&ktv
);
2452 err
= put_user(ktv
.tv_sec
, &up
->tv_sec
);
2453 err
|= __put_user(ktv
.tv_usec
, &up
->tv_usec
);
2458 static int do_siocgstampns(struct net
*net
, struct socket
*sock
,
2459 unsigned int cmd
, struct compat_timespec __user
*up
)
2461 mm_segment_t old_fs
= get_fs();
2462 struct timespec kts
;
2466 err
= sock_do_ioctl(net
, sock
, cmd
, (unsigned long)&kts
);
2469 err
= put_user(kts
.tv_sec
, &up
->tv_sec
);
2470 err
|= __put_user(kts
.tv_nsec
, &up
->tv_nsec
);
2475 static int dev_ifname32(struct net
*net
, struct compat_ifreq __user
*uifr32
)
2477 struct ifreq __user
*uifr
;
2480 uifr
= compat_alloc_user_space(sizeof(struct ifreq
));
2481 if (copy_in_user(uifr
, uifr32
, sizeof(struct compat_ifreq
)))
2484 err
= dev_ioctl(net
, SIOCGIFNAME
, uifr
);
2488 if (copy_in_user(uifr32
, uifr
, sizeof(struct compat_ifreq
)))
2494 static int dev_ifconf(struct net
*net
, struct compat_ifconf __user
*uifc32
)
2496 struct compat_ifconf ifc32
;
2498 struct ifconf __user
*uifc
;
2499 struct compat_ifreq __user
*ifr32
;
2500 struct ifreq __user
*ifr
;
2504 if (copy_from_user(&ifc32
, uifc32
, sizeof(struct compat_ifconf
)))
2507 if (ifc32
.ifcbuf
== 0) {
2511 uifc
= compat_alloc_user_space(sizeof(struct ifconf
));
2513 size_t len
=((ifc32
.ifc_len
/ sizeof (struct compat_ifreq
)) + 1) *
2514 sizeof (struct ifreq
);
2515 uifc
= compat_alloc_user_space(sizeof(struct ifconf
) + len
);
2517 ifr
= ifc
.ifc_req
= (void __user
*)(uifc
+ 1);
2518 ifr32
= compat_ptr(ifc32
.ifcbuf
);
2519 for (i
= 0; i
< ifc32
.ifc_len
; i
+= sizeof (struct compat_ifreq
)) {
2520 if (copy_in_user(ifr
, ifr32
, sizeof(struct compat_ifreq
)))
2526 if (copy_to_user(uifc
, &ifc
, sizeof(struct ifconf
)))
2529 err
= dev_ioctl(net
, SIOCGIFCONF
, uifc
);
2533 if (copy_from_user(&ifc
, uifc
, sizeof(struct ifconf
)))
2537 ifr32
= compat_ptr(ifc32
.ifcbuf
);
2539 i
+ sizeof (struct compat_ifreq
) <= ifc32
.ifc_len
&& j
< ifc
.ifc_len
;
2540 i
+= sizeof (struct compat_ifreq
), j
+= sizeof (struct ifreq
)) {
2541 if (copy_in_user(ifr32
, ifr
, sizeof (struct compat_ifreq
)))
2547 if (ifc32
.ifcbuf
== 0) {
2548 /* Translate from 64-bit structure multiple to
2552 i
= ((i
/ sizeof(struct ifreq
)) * sizeof(struct compat_ifreq
));
2557 if (copy_to_user(uifc32
, &ifc32
, sizeof(struct compat_ifconf
)))
2563 static int ethtool_ioctl(struct net
*net
, struct compat_ifreq __user
*ifr32
)
2565 struct ifreq __user
*ifr
;
2569 ifr
= compat_alloc_user_space(sizeof(*ifr
));
2571 if (copy_in_user(&ifr
->ifr_name
, &ifr32
->ifr_name
, IFNAMSIZ
))
2574 if (get_user(data
, &ifr32
->ifr_ifru
.ifru_data
))
2577 datap
= compat_ptr(data
);
2578 if (put_user(datap
, &ifr
->ifr_ifru
.ifru_data
))
2581 return dev_ioctl(net
, SIOCETHTOOL
, ifr
);
2584 static int compat_siocwandev(struct net
*net
, struct compat_ifreq __user
*uifr32
)
2587 compat_uptr_t uptr32
;
2588 struct ifreq __user
*uifr
;
2590 uifr
= compat_alloc_user_space(sizeof (*uifr
));
2591 if (copy_in_user(uifr
, uifr32
, sizeof(struct compat_ifreq
)))
2594 if (get_user(uptr32
, &uifr32
->ifr_settings
.ifs_ifsu
))
2597 uptr
= compat_ptr(uptr32
);
2599 if (put_user(uptr
, &uifr
->ifr_settings
.ifs_ifsu
.raw_hdlc
))
2602 return dev_ioctl(net
, SIOCWANDEV
, uifr
);
2605 static int bond_ioctl(struct net
*net
, unsigned int cmd
,
2606 struct compat_ifreq __user
*ifr32
)
2609 struct ifreq __user
*uifr
;
2610 mm_segment_t old_fs
;
2616 case SIOCBONDENSLAVE
:
2617 case SIOCBONDRELEASE
:
2618 case SIOCBONDSETHWADDR
:
2619 case SIOCBONDCHANGEACTIVE
:
2620 if (copy_from_user(&kifr
, ifr32
, sizeof(struct compat_ifreq
)))
2625 err
= dev_ioctl(net
, cmd
, &kifr
);
2629 case SIOCBONDSLAVEINFOQUERY
:
2630 case SIOCBONDINFOQUERY
:
2631 uifr
= compat_alloc_user_space(sizeof(*uifr
));
2632 if (copy_in_user(&uifr
->ifr_name
, &ifr32
->ifr_name
, IFNAMSIZ
))
2635 if (get_user(data
, &ifr32
->ifr_ifru
.ifru_data
))
2638 datap
= compat_ptr(data
);
2639 if (put_user(datap
, &uifr
->ifr_ifru
.ifru_data
))
2642 return dev_ioctl(net
, cmd
, uifr
);
2648 static int siocdevprivate_ioctl(struct net
*net
, unsigned int cmd
,
2649 struct compat_ifreq __user
*u_ifreq32
)
2651 struct ifreq __user
*u_ifreq64
;
2652 char tmp_buf
[IFNAMSIZ
];
2653 void __user
*data64
;
2656 if (copy_from_user(&tmp_buf
[0], &(u_ifreq32
->ifr_ifrn
.ifrn_name
[0]),
2659 if (__get_user(data32
, &u_ifreq32
->ifr_ifru
.ifru_data
))
2661 data64
= compat_ptr(data32
);
2663 u_ifreq64
= compat_alloc_user_space(sizeof(*u_ifreq64
));
2665 /* Don't check these user accesses, just let that get trapped
2666 * in the ioctl handler instead.
2668 if (copy_to_user(&u_ifreq64
->ifr_ifrn
.ifrn_name
[0], &tmp_buf
[0],
2671 if (__put_user(data64
, &u_ifreq64
->ifr_ifru
.ifru_data
))
2674 return dev_ioctl(net
, cmd
, u_ifreq64
);
2677 static int dev_ifsioc(struct net
*net
, struct socket
*sock
,
2678 unsigned int cmd
, struct compat_ifreq __user
*uifr32
)
2680 struct ifreq __user
*uifr
;
2683 uifr
= compat_alloc_user_space(sizeof(*uifr
));
2684 if (copy_in_user(uifr
, uifr32
, sizeof(*uifr32
)))
2687 err
= sock_do_ioctl(net
, sock
, cmd
, (unsigned long)uifr
);
2698 case SIOCGIFBRDADDR
:
2699 case SIOCGIFDSTADDR
:
2700 case SIOCGIFNETMASK
:
2705 if (copy_in_user(uifr32
, uifr
, sizeof(*uifr32
)))
2713 static int compat_sioc_ifmap(struct net
*net
, unsigned int cmd
,
2714 struct compat_ifreq __user
*uifr32
)
2717 struct compat_ifmap __user
*uifmap32
;
2718 mm_segment_t old_fs
;
2721 uifmap32
= &uifr32
->ifr_ifru
.ifru_map
;
2722 err
= copy_from_user(&ifr
, uifr32
, sizeof(ifr
.ifr_name
));
2723 err
|= __get_user(ifr
.ifr_map
.mem_start
, &uifmap32
->mem_start
);
2724 err
|= __get_user(ifr
.ifr_map
.mem_end
, &uifmap32
->mem_end
);
2725 err
|= __get_user(ifr
.ifr_map
.base_addr
, &uifmap32
->base_addr
);
2726 err
|= __get_user(ifr
.ifr_map
.irq
, &uifmap32
->irq
);
2727 err
|= __get_user(ifr
.ifr_map
.dma
, &uifmap32
->dma
);
2728 err
|= __get_user(ifr
.ifr_map
.port
, &uifmap32
->port
);
2734 err
= dev_ioctl(net
, cmd
, (void __user
*)&ifr
);
2737 if (cmd
== SIOCGIFMAP
&& !err
) {
2738 err
= copy_to_user(uifr32
, &ifr
, sizeof(ifr
.ifr_name
));
2739 err
|= __put_user(ifr
.ifr_map
.mem_start
, &uifmap32
->mem_start
);
2740 err
|= __put_user(ifr
.ifr_map
.mem_end
, &uifmap32
->mem_end
);
2741 err
|= __put_user(ifr
.ifr_map
.base_addr
, &uifmap32
->base_addr
);
2742 err
|= __put_user(ifr
.ifr_map
.irq
, &uifmap32
->irq
);
2743 err
|= __put_user(ifr
.ifr_map
.dma
, &uifmap32
->dma
);
2744 err
|= __put_user(ifr
.ifr_map
.port
, &uifmap32
->port
);
2751 static int compat_siocshwtstamp(struct net
*net
, struct compat_ifreq __user
*uifr32
)
2754 compat_uptr_t uptr32
;
2755 struct ifreq __user
*uifr
;
2757 uifr
= compat_alloc_user_space(sizeof (*uifr
));
2758 if (copy_in_user(uifr
, uifr32
, sizeof(struct compat_ifreq
)))
2761 if (get_user(uptr32
, &uifr32
->ifr_data
))
2764 uptr
= compat_ptr(uptr32
);
2766 if (put_user(uptr
, &uifr
->ifr_data
))
2769 return dev_ioctl(net
, SIOCSHWTSTAMP
, uifr
);
2774 struct sockaddr rt_dst
; /* target address */
2775 struct sockaddr rt_gateway
; /* gateway addr (RTF_GATEWAY) */
2776 struct sockaddr rt_genmask
; /* target network mask (IP) */
2777 unsigned short rt_flags
;
2780 unsigned char rt_tos
;
2781 unsigned char rt_class
;
2783 short rt_metric
; /* +1 for binary compatibility! */
2784 /* char * */ u32 rt_dev
; /* forcing the device at add */
2785 u32 rt_mtu
; /* per route MTU/Window */
2786 u32 rt_window
; /* Window clamping */
2787 unsigned short rt_irtt
; /* Initial RTT */
2790 struct in6_rtmsg32
{
2791 struct in6_addr rtmsg_dst
;
2792 struct in6_addr rtmsg_src
;
2793 struct in6_addr rtmsg_gateway
;
2803 static int routing_ioctl(struct net
*net
, struct socket
*sock
,
2804 unsigned int cmd
, void __user
*argp
)
2808 struct in6_rtmsg r6
;
2812 mm_segment_t old_fs
= get_fs();
2814 if (sock
&& sock
->sk
&& sock
->sk
->sk_family
== AF_INET6
) { /* ipv6 */
2815 struct in6_rtmsg32 __user
*ur6
= argp
;
2816 ret
= copy_from_user (&r6
.rtmsg_dst
, &(ur6
->rtmsg_dst
),
2817 3 * sizeof(struct in6_addr
));
2818 ret
|= __get_user (r6
.rtmsg_type
, &(ur6
->rtmsg_type
));
2819 ret
|= __get_user (r6
.rtmsg_dst_len
, &(ur6
->rtmsg_dst_len
));
2820 ret
|= __get_user (r6
.rtmsg_src_len
, &(ur6
->rtmsg_src_len
));
2821 ret
|= __get_user (r6
.rtmsg_metric
, &(ur6
->rtmsg_metric
));
2822 ret
|= __get_user (r6
.rtmsg_info
, &(ur6
->rtmsg_info
));
2823 ret
|= __get_user (r6
.rtmsg_flags
, &(ur6
->rtmsg_flags
));
2824 ret
|= __get_user (r6
.rtmsg_ifindex
, &(ur6
->rtmsg_ifindex
));
2828 struct rtentry32 __user
*ur4
= argp
;
2829 ret
= copy_from_user (&r4
.rt_dst
, &(ur4
->rt_dst
),
2830 3 * sizeof(struct sockaddr
));
2831 ret
|= __get_user (r4
.rt_flags
, &(ur4
->rt_flags
));
2832 ret
|= __get_user (r4
.rt_metric
, &(ur4
->rt_metric
));
2833 ret
|= __get_user (r4
.rt_mtu
, &(ur4
->rt_mtu
));
2834 ret
|= __get_user (r4
.rt_window
, &(ur4
->rt_window
));
2835 ret
|= __get_user (r4
.rt_irtt
, &(ur4
->rt_irtt
));
2836 ret
|= __get_user (rtdev
, &(ur4
->rt_dev
));
2838 ret
|= copy_from_user (devname
, compat_ptr(rtdev
), 15);
2839 r4
.rt_dev
= devname
; devname
[15] = 0;
2852 ret
= sock_do_ioctl(net
, sock
, cmd
, (unsigned long) r
);
2859 /* Since old style bridge ioctl's endup using SIOCDEVPRIVATE
2860 * for some operations; this forces use of the newer bridge-utils that
2861 * use compatiable ioctls
2863 static int old_bridge_ioctl(compat_ulong_t __user
*argp
)
2867 if (get_user(tmp
, argp
))
2869 if (tmp
== BRCTL_GET_VERSION
)
2870 return BRCTL_VERSION
+ 1;
2874 static int compat_sock_ioctl_trans(struct file
*file
, struct socket
*sock
,
2875 unsigned int cmd
, unsigned long arg
)
2877 void __user
*argp
= compat_ptr(arg
);
2878 struct sock
*sk
= sock
->sk
;
2879 struct net
*net
= sock_net(sk
);
2881 if (cmd
>= SIOCDEVPRIVATE
&& cmd
<= (SIOCDEVPRIVATE
+ 15))
2882 return siocdevprivate_ioctl(net
, cmd
, argp
);
2887 return old_bridge_ioctl(argp
);
2889 return dev_ifname32(net
, argp
);
2891 return dev_ifconf(net
, argp
);
2893 return ethtool_ioctl(net
, argp
);
2895 return compat_siocwandev(net
, argp
);
2898 return compat_sioc_ifmap(net
, cmd
, argp
);
2899 case SIOCBONDENSLAVE
:
2900 case SIOCBONDRELEASE
:
2901 case SIOCBONDSETHWADDR
:
2902 case SIOCBONDSLAVEINFOQUERY
:
2903 case SIOCBONDINFOQUERY
:
2904 case SIOCBONDCHANGEACTIVE
:
2905 return bond_ioctl(net
, cmd
, argp
);
2908 return routing_ioctl(net
, sock
, cmd
, argp
);
2910 return do_siocgstamp(net
, sock
, cmd
, argp
);
2912 return do_siocgstampns(net
, sock
, cmd
, argp
);
2914 return compat_siocshwtstamp(net
, argp
);
2926 return sock_ioctl(file
, cmd
, arg
);
2943 case SIOCSIFHWBROADCAST
:
2945 case SIOCGIFBRDADDR
:
2946 case SIOCSIFBRDADDR
:
2947 case SIOCGIFDSTADDR
:
2948 case SIOCSIFDSTADDR
:
2949 case SIOCGIFNETMASK
:
2950 case SIOCSIFNETMASK
:
2961 return dev_ifsioc(net
, sock
, cmd
, argp
);
2967 return sock_do_ioctl(net
, sock
, cmd
, arg
);
2970 /* Prevent warning from compat_sys_ioctl, these always
2971 * result in -EINVAL in the native case anyway. */
2984 return -ENOIOCTLCMD
;
2987 static long compat_sock_ioctl(struct file
*file
, unsigned cmd
,
2990 struct socket
*sock
= file
->private_data
;
2991 int ret
= -ENOIOCTLCMD
;
2998 if (sock
->ops
->compat_ioctl
)
2999 ret
= sock
->ops
->compat_ioctl(sock
, cmd
, arg
);
3001 if (ret
== -ENOIOCTLCMD
&&
3002 (cmd
>= SIOCIWFIRST
&& cmd
<= SIOCIWLAST
))
3003 ret
= compat_wext_handle_ioctl(net
, cmd
, arg
);
3005 if (ret
== -ENOIOCTLCMD
)
3006 ret
= compat_sock_ioctl_trans(file
, sock
, cmd
, arg
);
3012 int kernel_bind(struct socket
*sock
, struct sockaddr
*addr
, int addrlen
)
3014 return sock
->ops
->bind(sock
, addr
, addrlen
);
3017 int kernel_listen(struct socket
*sock
, int backlog
)
3019 return sock
->ops
->listen(sock
, backlog
);
3022 int kernel_accept(struct socket
*sock
, struct socket
**newsock
, int flags
)
3024 struct sock
*sk
= sock
->sk
;
3027 err
= sock_create_lite(sk
->sk_family
, sk
->sk_type
, sk
->sk_protocol
,
3032 err
= sock
->ops
->accept(sock
, *newsock
, flags
);
3034 sock_release(*newsock
);
3039 (*newsock
)->ops
= sock
->ops
;
3040 __module_get((*newsock
)->ops
->owner
);
3046 int kernel_connect(struct socket
*sock
, struct sockaddr
*addr
, int addrlen
,
3049 return sock
->ops
->connect(sock
, addr
, addrlen
, flags
);
3052 int kernel_getsockname(struct socket
*sock
, struct sockaddr
*addr
,
3055 return sock
->ops
->getname(sock
, addr
, addrlen
, 0);
3058 int kernel_getpeername(struct socket
*sock
, struct sockaddr
*addr
,
3061 return sock
->ops
->getname(sock
, addr
, addrlen
, 1);
3064 int kernel_getsockopt(struct socket
*sock
, int level
, int optname
,
3065 char *optval
, int *optlen
)
3067 mm_segment_t oldfs
= get_fs();
3071 if (level
== SOL_SOCKET
)
3072 err
= sock_getsockopt(sock
, level
, optname
, optval
, optlen
);
3074 err
= sock
->ops
->getsockopt(sock
, level
, optname
, optval
,
3080 int kernel_setsockopt(struct socket
*sock
, int level
, int optname
,
3081 char *optval
, unsigned int optlen
)
3083 mm_segment_t oldfs
= get_fs();
3087 if (level
== SOL_SOCKET
)
3088 err
= sock_setsockopt(sock
, level
, optname
, optval
, optlen
);
3090 err
= sock
->ops
->setsockopt(sock
, level
, optname
, optval
,
3096 int kernel_sendpage(struct socket
*sock
, struct page
*page
, int offset
,
3097 size_t size
, int flags
)
3099 if (sock
->ops
->sendpage
)
3100 return sock
->ops
->sendpage(sock
, page
, offset
, size
, flags
);
3102 return sock_no_sendpage(sock
, page
, offset
, size
, flags
);
3105 int kernel_sock_ioctl(struct socket
*sock
, int cmd
, unsigned long arg
)
3107 mm_segment_t oldfs
= get_fs();
3111 err
= sock
->ops
->ioctl(sock
, cmd
, arg
);
3117 int kernel_sock_shutdown(struct socket
*sock
, enum sock_shutdown_cmd how
)
3119 return sock
->ops
->shutdown(sock
, how
);
3122 EXPORT_SYMBOL(sock_create
);
3123 EXPORT_SYMBOL(sock_create_kern
);
3124 EXPORT_SYMBOL(sock_create_lite
);
3125 EXPORT_SYMBOL(sock_map_fd
);
3126 EXPORT_SYMBOL(sock_recvmsg
);
3127 EXPORT_SYMBOL(sock_register
);
3128 EXPORT_SYMBOL(sock_release
);
3129 EXPORT_SYMBOL(sock_sendmsg
);
3130 EXPORT_SYMBOL(sock_unregister
);
3131 EXPORT_SYMBOL(sock_wake_async
);
3132 EXPORT_SYMBOL(sockfd_lookup
);
3133 EXPORT_SYMBOL(kernel_sendmsg
);
3134 EXPORT_SYMBOL(kernel_recvmsg
);
3135 EXPORT_SYMBOL(kernel_bind
);
3136 EXPORT_SYMBOL(kernel_listen
);
3137 EXPORT_SYMBOL(kernel_accept
);
3138 EXPORT_SYMBOL(kernel_connect
);
3139 EXPORT_SYMBOL(kernel_getsockname
);
3140 EXPORT_SYMBOL(kernel_getpeername
);
3141 EXPORT_SYMBOL(kernel_getsockopt
);
3142 EXPORT_SYMBOL(kernel_setsockopt
);
3143 EXPORT_SYMBOL(kernel_sendpage
);
3144 EXPORT_SYMBOL(kernel_sock_ioctl
);
3145 EXPORT_SYMBOL(kernel_sock_shutdown
);