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 #include <linux/if_tun.h>
101 #include <linux/ipv6_route.h>
102 #include <linux/route.h>
103 #include <linux/atmdev.h>
104 #include <linux/atmarp.h>
105 #include <linux/atmsvc.h>
106 #include <linux/atmlec.h>
107 #include <linux/atmclip.h>
108 #include <linux/atmmpc.h>
109 #include <linux/atm_tcp.h>
110 #include <linux/sonet.h>
111 #include <linux/sockios.h>
112 #include <linux/atalk.h>
114 static int sock_no_open(struct inode
*irrelevant
, struct file
*dontcare
);
115 static ssize_t
sock_aio_read(struct kiocb
*iocb
, const struct iovec
*iov
,
116 unsigned long nr_segs
, loff_t pos
);
117 static ssize_t
sock_aio_write(struct kiocb
*iocb
, const struct iovec
*iov
,
118 unsigned long nr_segs
, loff_t pos
);
119 static int sock_mmap(struct file
*file
, struct vm_area_struct
*vma
);
121 static int sock_close(struct inode
*inode
, struct file
*file
);
122 static unsigned int sock_poll(struct file
*file
,
123 struct poll_table_struct
*wait
);
124 static long sock_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
);
126 static long compat_sock_ioctl(struct file
*file
,
127 unsigned int cmd
, unsigned long arg
);
129 static int sock_fasync(int fd
, struct file
*filp
, int on
);
130 static ssize_t
sock_sendpage(struct file
*file
, struct page
*page
,
131 int offset
, size_t size
, loff_t
*ppos
, int more
);
132 static ssize_t
sock_splice_read(struct file
*file
, loff_t
*ppos
,
133 struct pipe_inode_info
*pipe
, size_t len
,
137 * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
138 * in the operation structures but are done directly via the socketcall() multiplexor.
141 static const struct file_operations socket_file_ops
= {
142 .owner
= THIS_MODULE
,
144 .aio_read
= sock_aio_read
,
145 .aio_write
= sock_aio_write
,
147 .unlocked_ioctl
= sock_ioctl
,
149 .compat_ioctl
= compat_sock_ioctl
,
152 .open
= sock_no_open
, /* special open code to disallow open via /proc */
153 .release
= sock_close
,
154 .fasync
= sock_fasync
,
155 .sendpage
= sock_sendpage
,
156 .splice_write
= generic_splice_sendpage
,
157 .splice_read
= sock_splice_read
,
161 * The protocol list. Each protocol is registered in here.
164 static DEFINE_SPINLOCK(net_family_lock
);
165 static const struct net_proto_family
*net_families
[NPROTO
] __read_mostly
;
168 * Statistics counters of the socket lists
171 static DEFINE_PER_CPU(int, sockets_in_use
) = 0;
175 * Move socket addresses back and forth across the kernel/user
176 * divide and look after the messy bits.
179 #define MAX_SOCK_ADDR 128 /* 108 for Unix domain -
180 16 for IP, 16 for IPX,
183 must be at least one bigger than
184 the AF_UNIX size (see net/unix/af_unix.c
189 * move_addr_to_kernel - copy a socket address into kernel space
190 * @uaddr: Address in user space
191 * @kaddr: Address in kernel space
192 * @ulen: Length in user space
194 * The address is copied into kernel space. If the provided address is
195 * too long an error code of -EINVAL is returned. If the copy gives
196 * invalid addresses -EFAULT is returned. On a success 0 is returned.
199 int move_addr_to_kernel(void __user
*uaddr
, int ulen
, struct sockaddr
*kaddr
)
201 if (ulen
< 0 || ulen
> sizeof(struct sockaddr_storage
))
205 if (copy_from_user(kaddr
, uaddr
, ulen
))
207 return audit_sockaddr(ulen
, kaddr
);
211 * move_addr_to_user - copy an address to user space
212 * @kaddr: kernel space address
213 * @klen: length of address in kernel
214 * @uaddr: user space address
215 * @ulen: pointer to user length field
217 * The value pointed to by ulen on entry is the buffer length available.
218 * This is overwritten with the buffer space used. -EINVAL is returned
219 * if an overlong buffer is specified or a negative buffer size. -EFAULT
220 * is returned if either the buffer or the length field are not
222 * After copying the data up to the limit the user specifies, the true
223 * length of the data is written over the length limit the user
224 * specified. Zero is returned for a success.
227 int move_addr_to_user(struct sockaddr
*kaddr
, int klen
, void __user
*uaddr
,
233 err
= get_user(len
, ulen
);
238 if (len
< 0 || len
> sizeof(struct sockaddr_storage
))
241 if (audit_sockaddr(klen
, kaddr
))
243 if (copy_to_user(uaddr
, kaddr
, len
))
247 * "fromlen shall refer to the value before truncation.."
250 return __put_user(klen
, ulen
);
253 static struct kmem_cache
*sock_inode_cachep __read_mostly
;
255 static struct inode
*sock_alloc_inode(struct super_block
*sb
)
257 struct socket_alloc
*ei
;
259 ei
= kmem_cache_alloc(sock_inode_cachep
, GFP_KERNEL
);
262 init_waitqueue_head(&ei
->socket
.wait
);
264 ei
->socket
.fasync_list
= NULL
;
265 ei
->socket
.state
= SS_UNCONNECTED
;
266 ei
->socket
.flags
= 0;
267 ei
->socket
.ops
= NULL
;
268 ei
->socket
.sk
= NULL
;
269 ei
->socket
.file
= NULL
;
271 return &ei
->vfs_inode
;
274 static void sock_destroy_inode(struct inode
*inode
)
276 kmem_cache_free(sock_inode_cachep
,
277 container_of(inode
, struct socket_alloc
, vfs_inode
));
280 static void init_once(void *foo
)
282 struct socket_alloc
*ei
= (struct socket_alloc
*)foo
;
284 inode_init_once(&ei
->vfs_inode
);
287 static int init_inodecache(void)
289 sock_inode_cachep
= kmem_cache_create("sock_inode_cache",
290 sizeof(struct socket_alloc
),
292 (SLAB_HWCACHE_ALIGN
|
293 SLAB_RECLAIM_ACCOUNT
|
296 if (sock_inode_cachep
== NULL
)
301 static const struct super_operations sockfs_ops
= {
302 .alloc_inode
= sock_alloc_inode
,
303 .destroy_inode
=sock_destroy_inode
,
304 .statfs
= simple_statfs
,
307 static int sockfs_get_sb(struct file_system_type
*fs_type
,
308 int flags
, const char *dev_name
, void *data
,
309 struct vfsmount
*mnt
)
311 return get_sb_pseudo(fs_type
, "socket:", &sockfs_ops
, SOCKFS_MAGIC
,
315 static struct vfsmount
*sock_mnt __read_mostly
;
317 static struct file_system_type sock_fs_type
= {
319 .get_sb
= sockfs_get_sb
,
320 .kill_sb
= kill_anon_super
,
323 static int sockfs_delete_dentry(struct dentry
*dentry
)
326 * At creation time, we pretended this dentry was hashed
327 * (by clearing DCACHE_UNHASHED bit in d_flags)
328 * At delete time, we restore the truth : not hashed.
329 * (so that dput() can proceed correctly)
331 dentry
->d_flags
|= DCACHE_UNHASHED
;
336 * sockfs_dname() is called from d_path().
338 static char *sockfs_dname(struct dentry
*dentry
, char *buffer
, int buflen
)
340 return dynamic_dname(dentry
, buffer
, buflen
, "socket:[%lu]",
341 dentry
->d_inode
->i_ino
);
344 static const struct dentry_operations sockfs_dentry_operations
= {
345 .d_delete
= sockfs_delete_dentry
,
346 .d_dname
= sockfs_dname
,
350 * Obtains the first available file descriptor and sets it up for use.
352 * These functions create file structures and maps them to fd space
353 * of the current process. On success it returns file descriptor
354 * and file struct implicitly stored in sock->file.
355 * Note that another thread may close file descriptor before we return
356 * from this function. We use the fact that now we do not refer
357 * to socket after mapping. If one day we will need it, this
358 * function will increment ref. count on file by 1.
360 * In any case returned fd MAY BE not valid!
361 * This race condition is unavoidable
362 * with shared fd spaces, we cannot solve it inside kernel,
363 * but we take care of internal coherence yet.
366 static int sock_alloc_fd(struct file
**filep
, int flags
)
370 fd
= get_unused_fd_flags(flags
);
371 if (likely(fd
>= 0)) {
372 struct file
*file
= get_empty_filp();
375 if (unlikely(!file
)) {
384 static int sock_attach_fd(struct socket
*sock
, struct file
*file
, int flags
)
386 struct dentry
*dentry
;
387 struct qstr name
= { .name
= "" };
389 dentry
= d_alloc(sock_mnt
->mnt_sb
->s_root
, &name
);
390 if (unlikely(!dentry
))
393 dentry
->d_op
= &sockfs_dentry_operations
;
395 * We dont want to push this dentry into global dentry hash table.
396 * We pretend dentry is already hashed, by unsetting DCACHE_UNHASHED
397 * This permits a working /proc/$pid/fd/XXX on sockets
399 dentry
->d_flags
&= ~DCACHE_UNHASHED
;
400 d_instantiate(dentry
, SOCK_INODE(sock
));
403 init_file(file
, sock_mnt
, dentry
, FMODE_READ
| FMODE_WRITE
,
405 SOCK_INODE(sock
)->i_fop
= &socket_file_ops
;
406 file
->f_flags
= O_RDWR
| (flags
& O_NONBLOCK
);
408 file
->private_data
= sock
;
413 int sock_map_fd(struct socket
*sock
, int flags
)
415 struct file
*newfile
;
416 int fd
= sock_alloc_fd(&newfile
, flags
);
418 if (likely(fd
>= 0)) {
419 int err
= sock_attach_fd(sock
, newfile
, flags
);
421 if (unlikely(err
< 0)) {
426 fd_install(fd
, newfile
);
431 static struct socket
*sock_from_file(struct file
*file
, int *err
)
433 if (file
->f_op
== &socket_file_ops
)
434 return file
->private_data
; /* set in sock_map_fd */
441 * sockfd_lookup - Go from a file number to its socket slot
443 * @err: pointer to an error code return
445 * The file handle passed in is locked and the socket it is bound
446 * too is returned. If an error occurs the err pointer is overwritten
447 * with a negative errno code and NULL is returned. The function checks
448 * for both invalid handles and passing a handle which is not a socket.
450 * On a success the socket object pointer is returned.
453 struct socket
*sockfd_lookup(int fd
, int *err
)
464 sock
= sock_from_file(file
, err
);
470 static struct socket
*sockfd_lookup_light(int fd
, int *err
, int *fput_needed
)
476 file
= fget_light(fd
, fput_needed
);
478 sock
= sock_from_file(file
, err
);
481 fput_light(file
, *fput_needed
);
487 * sock_alloc - allocate a socket
489 * Allocate a new inode and socket object. The two are bound together
490 * and initialised. The socket is then returned. If we are out of inodes
494 static struct socket
*sock_alloc(void)
499 inode
= new_inode(sock_mnt
->mnt_sb
);
503 sock
= SOCKET_I(inode
);
505 kmemcheck_annotate_bitfield(sock
, type
);
506 inode
->i_mode
= S_IFSOCK
| S_IRWXUGO
;
507 inode
->i_uid
= current_fsuid();
508 inode
->i_gid
= current_fsgid();
510 percpu_add(sockets_in_use
, 1);
515 * In theory you can't get an open on this inode, but /proc provides
516 * a back door. Remember to keep it shut otherwise you'll let the
517 * creepy crawlies in.
520 static int sock_no_open(struct inode
*irrelevant
, struct file
*dontcare
)
525 const struct file_operations bad_sock_fops
= {
526 .owner
= THIS_MODULE
,
527 .open
= sock_no_open
,
531 * sock_release - close a socket
532 * @sock: socket to close
534 * The socket is released from the protocol stack if it has a release
535 * callback, and the inode is then released if the socket is bound to
536 * an inode not a file.
539 void sock_release(struct socket
*sock
)
542 struct module
*owner
= sock
->ops
->owner
;
544 sock
->ops
->release(sock
);
549 if (sock
->fasync_list
)
550 printk(KERN_ERR
"sock_release: fasync list not empty!\n");
552 percpu_sub(sockets_in_use
, 1);
554 iput(SOCK_INODE(sock
));
560 int sock_tx_timestamp(struct msghdr
*msg
, struct sock
*sk
,
561 union skb_shared_tx
*shtx
)
564 if (sock_flag(sk
, SOCK_TIMESTAMPING_TX_HARDWARE
))
566 if (sock_flag(sk
, SOCK_TIMESTAMPING_TX_SOFTWARE
))
570 EXPORT_SYMBOL(sock_tx_timestamp
);
572 static inline int __sock_sendmsg(struct kiocb
*iocb
, struct socket
*sock
,
573 struct msghdr
*msg
, size_t size
)
575 struct sock_iocb
*si
= kiocb_to_siocb(iocb
);
583 err
= security_socket_sendmsg(sock
, msg
, size
);
587 return sock
->ops
->sendmsg(iocb
, sock
, msg
, size
);
590 int sock_sendmsg(struct socket
*sock
, struct msghdr
*msg
, size_t size
)
593 struct sock_iocb siocb
;
596 init_sync_kiocb(&iocb
, NULL
);
597 iocb
.private = &siocb
;
598 ret
= __sock_sendmsg(&iocb
, sock
, msg
, size
);
599 if (-EIOCBQUEUED
== ret
)
600 ret
= wait_on_sync_kiocb(&iocb
);
604 int kernel_sendmsg(struct socket
*sock
, struct msghdr
*msg
,
605 struct kvec
*vec
, size_t num
, size_t size
)
607 mm_segment_t oldfs
= get_fs();
612 * the following is safe, since for compiler definitions of kvec and
613 * iovec are identical, yielding the same in-core layout and alignment
615 msg
->msg_iov
= (struct iovec
*)vec
;
616 msg
->msg_iovlen
= num
;
617 result
= sock_sendmsg(sock
, msg
, size
);
622 static int ktime2ts(ktime_t kt
, struct timespec
*ts
)
625 *ts
= ktime_to_timespec(kt
);
633 * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
635 void __sock_recv_timestamp(struct msghdr
*msg
, struct sock
*sk
,
638 int need_software_tstamp
= sock_flag(sk
, SOCK_RCVTSTAMP
);
639 struct timespec ts
[3];
641 struct skb_shared_hwtstamps
*shhwtstamps
=
644 /* Race occurred between timestamp enabling and packet
645 receiving. Fill in the current time for now. */
646 if (need_software_tstamp
&& skb
->tstamp
.tv64
== 0)
647 __net_timestamp(skb
);
649 if (need_software_tstamp
) {
650 if (!sock_flag(sk
, SOCK_RCVTSTAMPNS
)) {
652 skb_get_timestamp(skb
, &tv
);
653 put_cmsg(msg
, SOL_SOCKET
, SCM_TIMESTAMP
,
657 skb_get_timestampns(skb
, &ts
);
658 put_cmsg(msg
, SOL_SOCKET
, SCM_TIMESTAMPNS
,
664 memset(ts
, 0, sizeof(ts
));
665 if (skb
->tstamp
.tv64
&&
666 sock_flag(sk
, SOCK_TIMESTAMPING_SOFTWARE
)) {
667 skb_get_timestampns(skb
, ts
+ 0);
671 if (sock_flag(sk
, SOCK_TIMESTAMPING_SYS_HARDWARE
) &&
672 ktime2ts(shhwtstamps
->syststamp
, ts
+ 1))
674 if (sock_flag(sk
, SOCK_TIMESTAMPING_RAW_HARDWARE
) &&
675 ktime2ts(shhwtstamps
->hwtstamp
, ts
+ 2))
679 put_cmsg(msg
, SOL_SOCKET
,
680 SCM_TIMESTAMPING
, sizeof(ts
), &ts
);
683 EXPORT_SYMBOL_GPL(__sock_recv_timestamp
);
685 inline void sock_recv_drops(struct msghdr
*msg
, struct sock
*sk
, struct sk_buff
*skb
)
687 if (sock_flag(sk
, SOCK_RXQ_OVFL
) && skb
&& skb
->dropcount
)
688 put_cmsg(msg
, SOL_SOCKET
, SO_RXQ_OVFL
,
689 sizeof(__u32
), &skb
->dropcount
);
692 void sock_recv_ts_and_drops(struct msghdr
*msg
, struct sock
*sk
,
695 sock_recv_timestamp(msg
, sk
, skb
);
696 sock_recv_drops(msg
, sk
, skb
);
698 EXPORT_SYMBOL_GPL(sock_recv_ts_and_drops
);
700 static inline int __sock_recvmsg_nosec(struct kiocb
*iocb
, struct socket
*sock
,
701 struct msghdr
*msg
, size_t size
, int flags
)
703 struct sock_iocb
*si
= kiocb_to_siocb(iocb
);
711 return sock
->ops
->recvmsg(iocb
, sock
, msg
, size
, flags
);
714 static inline int __sock_recvmsg(struct kiocb
*iocb
, struct socket
*sock
,
715 struct msghdr
*msg
, size_t size
, int flags
)
717 int err
= security_socket_recvmsg(sock
, msg
, size
, flags
);
719 return err
?: __sock_recvmsg_nosec(iocb
, sock
, msg
, size
, flags
);
722 int sock_recvmsg(struct socket
*sock
, struct msghdr
*msg
,
723 size_t size
, int flags
)
726 struct sock_iocb siocb
;
729 init_sync_kiocb(&iocb
, NULL
);
730 iocb
.private = &siocb
;
731 ret
= __sock_recvmsg(&iocb
, sock
, msg
, size
, flags
);
732 if (-EIOCBQUEUED
== ret
)
733 ret
= wait_on_sync_kiocb(&iocb
);
737 static int sock_recvmsg_nosec(struct socket
*sock
, struct msghdr
*msg
,
738 size_t size
, int flags
)
741 struct sock_iocb siocb
;
744 init_sync_kiocb(&iocb
, NULL
);
745 iocb
.private = &siocb
;
746 ret
= __sock_recvmsg_nosec(&iocb
, sock
, msg
, size
, flags
);
747 if (-EIOCBQUEUED
== ret
)
748 ret
= wait_on_sync_kiocb(&iocb
);
752 int kernel_recvmsg(struct socket
*sock
, struct msghdr
*msg
,
753 struct kvec
*vec
, size_t num
, size_t size
, int flags
)
755 mm_segment_t oldfs
= get_fs();
760 * the following is safe, since for compiler definitions of kvec and
761 * iovec are identical, yielding the same in-core layout and alignment
763 msg
->msg_iov
= (struct iovec
*)vec
, msg
->msg_iovlen
= num
;
764 result
= sock_recvmsg(sock
, msg
, size
, flags
);
769 static void sock_aio_dtor(struct kiocb
*iocb
)
771 kfree(iocb
->private);
774 static ssize_t
sock_sendpage(struct file
*file
, struct page
*page
,
775 int offset
, size_t size
, loff_t
*ppos
, int more
)
780 sock
= file
->private_data
;
782 flags
= !(file
->f_flags
& O_NONBLOCK
) ? 0 : MSG_DONTWAIT
;
786 return kernel_sendpage(sock
, page
, offset
, size
, flags
);
789 static ssize_t
sock_splice_read(struct file
*file
, loff_t
*ppos
,
790 struct pipe_inode_info
*pipe
, size_t len
,
793 struct socket
*sock
= file
->private_data
;
795 if (unlikely(!sock
->ops
->splice_read
))
798 return sock
->ops
->splice_read(sock
, ppos
, pipe
, len
, flags
);
801 static struct sock_iocb
*alloc_sock_iocb(struct kiocb
*iocb
,
802 struct sock_iocb
*siocb
)
804 if (!is_sync_kiocb(iocb
)) {
805 siocb
= kmalloc(sizeof(*siocb
), GFP_KERNEL
);
808 iocb
->ki_dtor
= sock_aio_dtor
;
812 iocb
->private = siocb
;
816 static ssize_t
do_sock_read(struct msghdr
*msg
, struct kiocb
*iocb
,
817 struct file
*file
, const struct iovec
*iov
,
818 unsigned long nr_segs
)
820 struct socket
*sock
= file
->private_data
;
824 for (i
= 0; i
< nr_segs
; i
++)
825 size
+= iov
[i
].iov_len
;
827 msg
->msg_name
= NULL
;
828 msg
->msg_namelen
= 0;
829 msg
->msg_control
= NULL
;
830 msg
->msg_controllen
= 0;
831 msg
->msg_iov
= (struct iovec
*)iov
;
832 msg
->msg_iovlen
= nr_segs
;
833 msg
->msg_flags
= (file
->f_flags
& O_NONBLOCK
) ? MSG_DONTWAIT
: 0;
835 return __sock_recvmsg(iocb
, sock
, msg
, size
, msg
->msg_flags
);
838 static ssize_t
sock_aio_read(struct kiocb
*iocb
, const struct iovec
*iov
,
839 unsigned long nr_segs
, loff_t pos
)
841 struct sock_iocb siocb
, *x
;
846 if (iocb
->ki_left
== 0) /* Match SYS5 behaviour */
850 x
= alloc_sock_iocb(iocb
, &siocb
);
853 return do_sock_read(&x
->async_msg
, iocb
, iocb
->ki_filp
, iov
, nr_segs
);
856 static ssize_t
do_sock_write(struct msghdr
*msg
, struct kiocb
*iocb
,
857 struct file
*file
, const struct iovec
*iov
,
858 unsigned long nr_segs
)
860 struct socket
*sock
= file
->private_data
;
864 for (i
= 0; i
< nr_segs
; i
++)
865 size
+= iov
[i
].iov_len
;
867 msg
->msg_name
= NULL
;
868 msg
->msg_namelen
= 0;
869 msg
->msg_control
= NULL
;
870 msg
->msg_controllen
= 0;
871 msg
->msg_iov
= (struct iovec
*)iov
;
872 msg
->msg_iovlen
= nr_segs
;
873 msg
->msg_flags
= (file
->f_flags
& O_NONBLOCK
) ? MSG_DONTWAIT
: 0;
874 if (sock
->type
== SOCK_SEQPACKET
)
875 msg
->msg_flags
|= MSG_EOR
;
877 return __sock_sendmsg(iocb
, sock
, msg
, size
);
880 static ssize_t
sock_aio_write(struct kiocb
*iocb
, const struct iovec
*iov
,
881 unsigned long nr_segs
, loff_t pos
)
883 struct sock_iocb siocb
, *x
;
888 x
= alloc_sock_iocb(iocb
, &siocb
);
892 return do_sock_write(&x
->async_msg
, iocb
, iocb
->ki_filp
, iov
, nr_segs
);
896 * Atomic setting of ioctl hooks to avoid race
897 * with module unload.
900 static DEFINE_MUTEX(br_ioctl_mutex
);
901 static int (*br_ioctl_hook
) (struct net
*, unsigned int cmd
, void __user
*arg
) = NULL
;
903 void brioctl_set(int (*hook
) (struct net
*, unsigned int, void __user
*))
905 mutex_lock(&br_ioctl_mutex
);
906 br_ioctl_hook
= hook
;
907 mutex_unlock(&br_ioctl_mutex
);
910 EXPORT_SYMBOL(brioctl_set
);
912 static DEFINE_MUTEX(vlan_ioctl_mutex
);
913 static int (*vlan_ioctl_hook
) (struct net
*, void __user
*arg
);
915 void vlan_ioctl_set(int (*hook
) (struct net
*, void __user
*))
917 mutex_lock(&vlan_ioctl_mutex
);
918 vlan_ioctl_hook
= hook
;
919 mutex_unlock(&vlan_ioctl_mutex
);
922 EXPORT_SYMBOL(vlan_ioctl_set
);
924 static DEFINE_MUTEX(dlci_ioctl_mutex
);
925 static int (*dlci_ioctl_hook
) (unsigned int, void __user
*);
927 void dlci_ioctl_set(int (*hook
) (unsigned int, void __user
*))
929 mutex_lock(&dlci_ioctl_mutex
);
930 dlci_ioctl_hook
= hook
;
931 mutex_unlock(&dlci_ioctl_mutex
);
934 EXPORT_SYMBOL(dlci_ioctl_set
);
936 static long sock_do_ioctl(struct net
*net
, struct socket
*sock
,
937 unsigned int cmd
, unsigned long arg
)
940 void __user
*argp
= (void __user
*)arg
;
942 err
= sock
->ops
->ioctl(sock
, cmd
, arg
);
945 * If this ioctl is unknown try to hand it down
948 if (err
== -ENOIOCTLCMD
)
949 err
= dev_ioctl(net
, cmd
, argp
);
955 * With an ioctl, arg may well be a user mode pointer, but we don't know
956 * what to do with it - that's up to the protocol still.
959 static long sock_ioctl(struct file
*file
, unsigned cmd
, unsigned long arg
)
963 void __user
*argp
= (void __user
*)arg
;
967 sock
= file
->private_data
;
970 if (cmd
>= SIOCDEVPRIVATE
&& cmd
<= (SIOCDEVPRIVATE
+ 15)) {
971 err
= dev_ioctl(net
, cmd
, argp
);
973 #ifdef CONFIG_WEXT_CORE
974 if (cmd
>= SIOCIWFIRST
&& cmd
<= SIOCIWLAST
) {
975 err
= dev_ioctl(net
, cmd
, argp
);
982 if (get_user(pid
, (int __user
*)argp
))
984 err
= f_setown(sock
->file
, pid
, 1);
988 err
= put_user(f_getown(sock
->file
),
997 request_module("bridge");
999 mutex_lock(&br_ioctl_mutex
);
1001 err
= br_ioctl_hook(net
, cmd
, argp
);
1002 mutex_unlock(&br_ioctl_mutex
);
1007 if (!vlan_ioctl_hook
)
1008 request_module("8021q");
1010 mutex_lock(&vlan_ioctl_mutex
);
1011 if (vlan_ioctl_hook
)
1012 err
= vlan_ioctl_hook(net
, argp
);
1013 mutex_unlock(&vlan_ioctl_mutex
);
1018 if (!dlci_ioctl_hook
)
1019 request_module("dlci");
1021 mutex_lock(&dlci_ioctl_mutex
);
1022 if (dlci_ioctl_hook
)
1023 err
= dlci_ioctl_hook(cmd
, argp
);
1024 mutex_unlock(&dlci_ioctl_mutex
);
1027 err
= sock_do_ioctl(net
, sock
, cmd
, arg
);
1033 int sock_create_lite(int family
, int type
, int protocol
, struct socket
**res
)
1036 struct socket
*sock
= NULL
;
1038 err
= security_socket_create(family
, type
, protocol
, 1);
1042 sock
= sock_alloc();
1049 err
= security_socket_post_create(sock
, family
, type
, protocol
, 1);
1062 /* No kernel lock held - perfect */
1063 static unsigned int sock_poll(struct file
*file
, poll_table
*wait
)
1065 struct socket
*sock
;
1068 * We can't return errors to poll, so it's either yes or no.
1070 sock
= file
->private_data
;
1071 return sock
->ops
->poll(file
, sock
, wait
);
1074 static int sock_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1076 struct socket
*sock
= file
->private_data
;
1078 return sock
->ops
->mmap(file
, sock
, vma
);
1081 static int sock_close(struct inode
*inode
, struct file
*filp
)
1084 * It was possible the inode is NULL we were
1085 * closing an unfinished socket.
1089 printk(KERN_DEBUG
"sock_close: NULL inode\n");
1092 sock_release(SOCKET_I(inode
));
1097 * Update the socket async list
1099 * Fasync_list locking strategy.
1101 * 1. fasync_list is modified only under process context socket lock
1102 * i.e. under semaphore.
1103 * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
1104 * or under socket lock.
1105 * 3. fasync_list can be used from softirq context, so that
1106 * modification under socket lock have to be enhanced with
1107 * write_lock_bh(&sk->sk_callback_lock).
1111 static int sock_fasync(int fd
, struct file
*filp
, int on
)
1113 struct fasync_struct
*fa
, *fna
= NULL
, **prev
;
1114 struct socket
*sock
;
1118 fna
= kmalloc(sizeof(struct fasync_struct
), GFP_KERNEL
);
1123 sock
= filp
->private_data
;
1133 spin_lock(&filp
->f_lock
);
1135 filp
->f_flags
|= FASYNC
;
1137 filp
->f_flags
&= ~FASYNC
;
1138 spin_unlock(&filp
->f_lock
);
1140 prev
= &(sock
->fasync_list
);
1142 for (fa
= *prev
; fa
!= NULL
; prev
= &fa
->fa_next
, fa
= *prev
)
1143 if (fa
->fa_file
== filp
)
1148 write_lock_bh(&sk
->sk_callback_lock
);
1150 write_unlock_bh(&sk
->sk_callback_lock
);
1155 fna
->fa_file
= filp
;
1157 fna
->magic
= FASYNC_MAGIC
;
1158 fna
->fa_next
= sock
->fasync_list
;
1159 write_lock_bh(&sk
->sk_callback_lock
);
1160 sock
->fasync_list
= fna
;
1161 sock_set_flag(sk
, SOCK_FASYNC
);
1162 write_unlock_bh(&sk
->sk_callback_lock
);
1165 write_lock_bh(&sk
->sk_callback_lock
);
1166 *prev
= fa
->fa_next
;
1167 if (!sock
->fasync_list
)
1168 sock_reset_flag(sk
, SOCK_FASYNC
);
1169 write_unlock_bh(&sk
->sk_callback_lock
);
1175 release_sock(sock
->sk
);
1179 /* This function may be called only under socket lock or callback_lock */
1181 int sock_wake_async(struct socket
*sock
, int how
, int band
)
1183 if (!sock
|| !sock
->fasync_list
)
1186 case SOCK_WAKE_WAITD
:
1187 if (test_bit(SOCK_ASYNC_WAITDATA
, &sock
->flags
))
1190 case SOCK_WAKE_SPACE
:
1191 if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE
, &sock
->flags
))
1196 __kill_fasync(sock
->fasync_list
, SIGIO
, band
);
1199 __kill_fasync(sock
->fasync_list
, SIGURG
, band
);
1204 static int __sock_create(struct net
*net
, int family
, int type
, int protocol
,
1205 struct socket
**res
, int kern
)
1208 struct socket
*sock
;
1209 const struct net_proto_family
*pf
;
1212 * Check protocol is in range
1214 if (family
< 0 || family
>= NPROTO
)
1215 return -EAFNOSUPPORT
;
1216 if (type
< 0 || type
>= SOCK_MAX
)
1221 This uglymoron is moved from INET layer to here to avoid
1222 deadlock in module load.
1224 if (family
== PF_INET
&& type
== SOCK_PACKET
) {
1228 printk(KERN_INFO
"%s uses obsolete (PF_INET,SOCK_PACKET)\n",
1234 err
= security_socket_create(family
, type
, protocol
, kern
);
1239 * Allocate the socket and allow the family to set things up. if
1240 * the protocol is 0, the family is instructed to select an appropriate
1243 sock
= sock_alloc();
1245 if (net_ratelimit())
1246 printk(KERN_WARNING
"socket: no more sockets\n");
1247 return -ENFILE
; /* Not exactly a match, but its the
1248 closest posix thing */
1253 #ifdef CONFIG_MODULES
1254 /* Attempt to load a protocol module if the find failed.
1256 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
1257 * requested real, full-featured networking support upon configuration.
1258 * Otherwise module support will break!
1260 if (net_families
[family
] == NULL
)
1261 request_module("net-pf-%d", family
);
1265 pf
= rcu_dereference(net_families
[family
]);
1266 err
= -EAFNOSUPPORT
;
1271 * We will call the ->create function, that possibly is in a loadable
1272 * module, so we have to bump that loadable module refcnt first.
1274 if (!try_module_get(pf
->owner
))
1277 /* Now protected by module ref count */
1280 err
= pf
->create(net
, sock
, protocol
, kern
);
1282 goto out_module_put
;
1285 * Now to bump the refcnt of the [loadable] module that owns this
1286 * socket at sock_release time we decrement its refcnt.
1288 if (!try_module_get(sock
->ops
->owner
))
1289 goto out_module_busy
;
1292 * Now that we're done with the ->create function, the [loadable]
1293 * module can have its refcnt decremented
1295 module_put(pf
->owner
);
1296 err
= security_socket_post_create(sock
, family
, type
, protocol
, kern
);
1298 goto out_sock_release
;
1304 err
= -EAFNOSUPPORT
;
1307 module_put(pf
->owner
);
1314 goto out_sock_release
;
1317 int sock_create(int family
, int type
, int protocol
, struct socket
**res
)
1319 return __sock_create(current
->nsproxy
->net_ns
, family
, type
, protocol
, res
, 0);
1322 int sock_create_kern(int family
, int type
, int protocol
, struct socket
**res
)
1324 return __sock_create(&init_net
, family
, type
, protocol
, res
, 1);
1327 SYSCALL_DEFINE3(socket
, int, family
, int, type
, int, protocol
)
1330 struct socket
*sock
;
1333 /* Check the SOCK_* constants for consistency. */
1334 BUILD_BUG_ON(SOCK_CLOEXEC
!= O_CLOEXEC
);
1335 BUILD_BUG_ON((SOCK_MAX
| SOCK_TYPE_MASK
) != SOCK_TYPE_MASK
);
1336 BUILD_BUG_ON(SOCK_CLOEXEC
& SOCK_TYPE_MASK
);
1337 BUILD_BUG_ON(SOCK_NONBLOCK
& SOCK_TYPE_MASK
);
1339 flags
= type
& ~SOCK_TYPE_MASK
;
1340 if (flags
& ~(SOCK_CLOEXEC
| SOCK_NONBLOCK
))
1342 type
&= SOCK_TYPE_MASK
;
1344 if (SOCK_NONBLOCK
!= O_NONBLOCK
&& (flags
& SOCK_NONBLOCK
))
1345 flags
= (flags
& ~SOCK_NONBLOCK
) | O_NONBLOCK
;
1347 retval
= sock_create(family
, type
, protocol
, &sock
);
1351 retval
= sock_map_fd(sock
, flags
& (O_CLOEXEC
| O_NONBLOCK
));
1356 /* It may be already another descriptor 8) Not kernel problem. */
1365 * Create a pair of connected sockets.
1368 SYSCALL_DEFINE4(socketpair
, int, family
, int, type
, int, protocol
,
1369 int __user
*, usockvec
)
1371 struct socket
*sock1
, *sock2
;
1373 struct file
*newfile1
, *newfile2
;
1376 flags
= type
& ~SOCK_TYPE_MASK
;
1377 if (flags
& ~(SOCK_CLOEXEC
| SOCK_NONBLOCK
))
1379 type
&= SOCK_TYPE_MASK
;
1381 if (SOCK_NONBLOCK
!= O_NONBLOCK
&& (flags
& SOCK_NONBLOCK
))
1382 flags
= (flags
& ~SOCK_NONBLOCK
) | O_NONBLOCK
;
1385 * Obtain the first socket and check if the underlying protocol
1386 * supports the socketpair call.
1389 err
= sock_create(family
, type
, protocol
, &sock1
);
1393 err
= sock_create(family
, type
, protocol
, &sock2
);
1397 err
= sock1
->ops
->socketpair(sock1
, sock2
);
1399 goto out_release_both
;
1401 fd1
= sock_alloc_fd(&newfile1
, flags
& O_CLOEXEC
);
1402 if (unlikely(fd1
< 0)) {
1404 goto out_release_both
;
1407 fd2
= sock_alloc_fd(&newfile2
, flags
& O_CLOEXEC
);
1408 if (unlikely(fd2
< 0)) {
1412 goto out_release_both
;
1415 err
= sock_attach_fd(sock1
, newfile1
, flags
& O_NONBLOCK
);
1416 if (unlikely(err
< 0)) {
1420 err
= sock_attach_fd(sock2
, newfile2
, flags
& O_NONBLOCK
);
1421 if (unlikely(err
< 0)) {
1426 audit_fd_pair(fd1
, fd2
);
1427 fd_install(fd1
, newfile1
);
1428 fd_install(fd2
, newfile2
);
1429 /* fd1 and fd2 may be already another descriptors.
1430 * Not kernel problem.
1433 err
= put_user(fd1
, &usockvec
[0]);
1435 err
= put_user(fd2
, &usockvec
[1]);
1444 sock_release(sock2
);
1446 sock_release(sock1
);
1452 sock_release(sock1
);
1455 sock_release(sock2
);
1462 * Bind a name to a socket. Nothing much to do here since it's
1463 * the protocol's responsibility to handle the local address.
1465 * We move the socket address to kernel space before we call
1466 * the protocol layer (having also checked the address is ok).
1469 SYSCALL_DEFINE3(bind
, int, fd
, struct sockaddr __user
*, umyaddr
, int, addrlen
)
1471 struct socket
*sock
;
1472 struct sockaddr_storage address
;
1473 int err
, fput_needed
;
1475 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1477 err
= move_addr_to_kernel(umyaddr
, addrlen
, (struct sockaddr
*)&address
);
1479 err
= security_socket_bind(sock
,
1480 (struct sockaddr
*)&address
,
1483 err
= sock
->ops
->bind(sock
,
1487 fput_light(sock
->file
, fput_needed
);
1493 * Perform a listen. Basically, we allow the protocol to do anything
1494 * necessary for a listen, and if that works, we mark the socket as
1495 * ready for listening.
1498 SYSCALL_DEFINE2(listen
, int, fd
, int, backlog
)
1500 struct socket
*sock
;
1501 int err
, fput_needed
;
1504 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1506 somaxconn
= sock_net(sock
->sk
)->core
.sysctl_somaxconn
;
1507 if ((unsigned)backlog
> somaxconn
)
1508 backlog
= somaxconn
;
1510 err
= security_socket_listen(sock
, backlog
);
1512 err
= sock
->ops
->listen(sock
, backlog
);
1514 fput_light(sock
->file
, fput_needed
);
1520 * For accept, we attempt to create a new socket, set up the link
1521 * with the client, wake up the client, then return the new
1522 * connected fd. We collect the address of the connector in kernel
1523 * space and move it to user at the very end. This is unclean because
1524 * we open the socket then return an error.
1526 * 1003.1g adds the ability to recvmsg() to query connection pending
1527 * status to recvmsg. We need to add that support in a way thats
1528 * clean when we restucture accept also.
1531 SYSCALL_DEFINE4(accept4
, int, fd
, struct sockaddr __user
*, upeer_sockaddr
,
1532 int __user
*, upeer_addrlen
, int, flags
)
1534 struct socket
*sock
, *newsock
;
1535 struct file
*newfile
;
1536 int err
, len
, newfd
, fput_needed
;
1537 struct sockaddr_storage address
;
1539 if (flags
& ~(SOCK_CLOEXEC
| SOCK_NONBLOCK
))
1542 if (SOCK_NONBLOCK
!= O_NONBLOCK
&& (flags
& SOCK_NONBLOCK
))
1543 flags
= (flags
& ~SOCK_NONBLOCK
) | O_NONBLOCK
;
1545 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1550 if (!(newsock
= sock_alloc()))
1553 newsock
->type
= sock
->type
;
1554 newsock
->ops
= sock
->ops
;
1557 * We don't need try_module_get here, as the listening socket (sock)
1558 * has the protocol module (sock->ops->owner) held.
1560 __module_get(newsock
->ops
->owner
);
1562 newfd
= sock_alloc_fd(&newfile
, flags
& O_CLOEXEC
);
1563 if (unlikely(newfd
< 0)) {
1565 sock_release(newsock
);
1569 err
= sock_attach_fd(newsock
, newfile
, flags
& O_NONBLOCK
);
1573 err
= security_socket_accept(sock
, newsock
);
1577 err
= sock
->ops
->accept(sock
, newsock
, sock
->file
->f_flags
);
1581 if (upeer_sockaddr
) {
1582 if (newsock
->ops
->getname(newsock
, (struct sockaddr
*)&address
,
1584 err
= -ECONNABORTED
;
1587 err
= move_addr_to_user((struct sockaddr
*)&address
,
1588 len
, upeer_sockaddr
, upeer_addrlen
);
1593 /* File flags are not inherited via accept() unlike another OSes. */
1595 fd_install(newfd
, newfile
);
1599 fput_light(sock
->file
, fput_needed
);
1603 sock_release(newsock
);
1605 put_unused_fd(newfd
);
1609 put_unused_fd(newfd
);
1613 SYSCALL_DEFINE3(accept
, int, fd
, struct sockaddr __user
*, upeer_sockaddr
,
1614 int __user
*, upeer_addrlen
)
1616 return sys_accept4(fd
, upeer_sockaddr
, upeer_addrlen
, 0);
1620 * Attempt to connect to a socket with the server address. The address
1621 * is in user space so we verify it is OK and move it to kernel space.
1623 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
1626 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
1627 * other SEQPACKET protocols that take time to connect() as it doesn't
1628 * include the -EINPROGRESS status for such sockets.
1631 SYSCALL_DEFINE3(connect
, int, fd
, struct sockaddr __user
*, uservaddr
,
1634 struct socket
*sock
;
1635 struct sockaddr_storage address
;
1636 int err
, fput_needed
;
1638 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1641 err
= move_addr_to_kernel(uservaddr
, addrlen
, (struct sockaddr
*)&address
);
1646 security_socket_connect(sock
, (struct sockaddr
*)&address
, addrlen
);
1650 err
= sock
->ops
->connect(sock
, (struct sockaddr
*)&address
, addrlen
,
1651 sock
->file
->f_flags
);
1653 fput_light(sock
->file
, fput_needed
);
1659 * Get the local address ('name') of a socket object. Move the obtained
1660 * name to user space.
1663 SYSCALL_DEFINE3(getsockname
, int, fd
, struct sockaddr __user
*, usockaddr
,
1664 int __user
*, usockaddr_len
)
1666 struct socket
*sock
;
1667 struct sockaddr_storage address
;
1668 int len
, err
, fput_needed
;
1670 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1674 err
= security_socket_getsockname(sock
);
1678 err
= sock
->ops
->getname(sock
, (struct sockaddr
*)&address
, &len
, 0);
1681 err
= move_addr_to_user((struct sockaddr
*)&address
, len
, usockaddr
, usockaddr_len
);
1684 fput_light(sock
->file
, fput_needed
);
1690 * Get the remote address ('name') of a socket object. Move the obtained
1691 * name to user space.
1694 SYSCALL_DEFINE3(getpeername
, int, fd
, struct sockaddr __user
*, usockaddr
,
1695 int __user
*, usockaddr_len
)
1697 struct socket
*sock
;
1698 struct sockaddr_storage address
;
1699 int len
, err
, fput_needed
;
1701 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1703 err
= security_socket_getpeername(sock
);
1705 fput_light(sock
->file
, fput_needed
);
1710 sock
->ops
->getname(sock
, (struct sockaddr
*)&address
, &len
,
1713 err
= move_addr_to_user((struct sockaddr
*)&address
, len
, usockaddr
,
1715 fput_light(sock
->file
, fput_needed
);
1721 * Send a datagram to a given address. We move the address into kernel
1722 * space and check the user space data area is readable before invoking
1726 SYSCALL_DEFINE6(sendto
, int, fd
, void __user
*, buff
, size_t, len
,
1727 unsigned, flags
, struct sockaddr __user
*, addr
,
1730 struct socket
*sock
;
1731 struct sockaddr_storage address
;
1737 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1741 iov
.iov_base
= buff
;
1743 msg
.msg_name
= NULL
;
1746 msg
.msg_control
= NULL
;
1747 msg
.msg_controllen
= 0;
1748 msg
.msg_namelen
= 0;
1750 err
= move_addr_to_kernel(addr
, addr_len
, (struct sockaddr
*)&address
);
1753 msg
.msg_name
= (struct sockaddr
*)&address
;
1754 msg
.msg_namelen
= addr_len
;
1756 if (sock
->file
->f_flags
& O_NONBLOCK
)
1757 flags
|= MSG_DONTWAIT
;
1758 msg
.msg_flags
= flags
;
1759 err
= sock_sendmsg(sock
, &msg
, len
);
1762 fput_light(sock
->file
, fput_needed
);
1768 * Send a datagram down a socket.
1771 SYSCALL_DEFINE4(send
, int, fd
, void __user
*, buff
, size_t, len
,
1774 return sys_sendto(fd
, buff
, len
, flags
, NULL
, 0);
1778 * Receive a frame from the socket and optionally record the address of the
1779 * sender. We verify the buffers are writable and if needed move the
1780 * sender address from kernel to user space.
1783 SYSCALL_DEFINE6(recvfrom
, int, fd
, void __user
*, ubuf
, size_t, size
,
1784 unsigned, flags
, struct sockaddr __user
*, addr
,
1785 int __user
*, addr_len
)
1787 struct socket
*sock
;
1790 struct sockaddr_storage address
;
1794 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1798 msg
.msg_control
= NULL
;
1799 msg
.msg_controllen
= 0;
1803 iov
.iov_base
= ubuf
;
1804 msg
.msg_name
= (struct sockaddr
*)&address
;
1805 msg
.msg_namelen
= sizeof(address
);
1806 if (sock
->file
->f_flags
& O_NONBLOCK
)
1807 flags
|= MSG_DONTWAIT
;
1808 err
= sock_recvmsg(sock
, &msg
, size
, flags
);
1810 if (err
>= 0 && addr
!= NULL
) {
1811 err2
= move_addr_to_user((struct sockaddr
*)&address
,
1812 msg
.msg_namelen
, addr
, addr_len
);
1817 fput_light(sock
->file
, fput_needed
);
1823 * Receive a datagram from a socket.
1826 asmlinkage
long sys_recv(int fd
, void __user
*ubuf
, size_t size
,
1829 return sys_recvfrom(fd
, ubuf
, size
, flags
, NULL
, NULL
);
1833 * Set a socket option. Because we don't know the option lengths we have
1834 * to pass the user mode parameter for the protocols to sort out.
1837 SYSCALL_DEFINE5(setsockopt
, int, fd
, int, level
, int, optname
,
1838 char __user
*, optval
, int, optlen
)
1840 int err
, fput_needed
;
1841 struct socket
*sock
;
1846 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1848 err
= security_socket_setsockopt(sock
, level
, optname
);
1852 if (level
== SOL_SOCKET
)
1854 sock_setsockopt(sock
, level
, optname
, optval
,
1858 sock
->ops
->setsockopt(sock
, level
, optname
, optval
,
1861 fput_light(sock
->file
, fput_needed
);
1867 * Get a socket option. Because we don't know the option lengths we have
1868 * to pass a user mode parameter for the protocols to sort out.
1871 SYSCALL_DEFINE5(getsockopt
, int, fd
, int, level
, int, optname
,
1872 char __user
*, optval
, int __user
*, optlen
)
1874 int err
, fput_needed
;
1875 struct socket
*sock
;
1877 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1879 err
= security_socket_getsockopt(sock
, level
, optname
);
1883 if (level
== SOL_SOCKET
)
1885 sock_getsockopt(sock
, level
, optname
, optval
,
1889 sock
->ops
->getsockopt(sock
, level
, optname
, optval
,
1892 fput_light(sock
->file
, fput_needed
);
1898 * Shutdown a socket.
1901 SYSCALL_DEFINE2(shutdown
, int, fd
, int, how
)
1903 int err
, fput_needed
;
1904 struct socket
*sock
;
1906 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1908 err
= security_socket_shutdown(sock
, how
);
1910 err
= sock
->ops
->shutdown(sock
, how
);
1911 fput_light(sock
->file
, fput_needed
);
1916 /* A couple of helpful macros for getting the address of the 32/64 bit
1917 * fields which are the same type (int / unsigned) on our platforms.
1919 #define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
1920 #define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
1921 #define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
1924 * BSD sendmsg interface
1927 SYSCALL_DEFINE3(sendmsg
, int, fd
, struct msghdr __user
*, msg
, unsigned, flags
)
1929 struct compat_msghdr __user
*msg_compat
=
1930 (struct compat_msghdr __user
*)msg
;
1931 struct socket
*sock
;
1932 struct sockaddr_storage address
;
1933 struct iovec iovstack
[UIO_FASTIOV
], *iov
= iovstack
;
1934 unsigned char ctl
[sizeof(struct cmsghdr
) + 20]
1935 __attribute__ ((aligned(sizeof(__kernel_size_t
))));
1936 /* 20 is size of ipv6_pktinfo */
1937 unsigned char *ctl_buf
= ctl
;
1938 struct msghdr msg_sys
;
1939 int err
, ctl_len
, iov_size
, total_len
;
1943 if (MSG_CMSG_COMPAT
& flags
) {
1944 if (get_compat_msghdr(&msg_sys
, msg_compat
))
1947 else if (copy_from_user(&msg_sys
, msg
, sizeof(struct msghdr
)))
1950 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1954 /* do not move before msg_sys is valid */
1956 if (msg_sys
.msg_iovlen
> UIO_MAXIOV
)
1959 /* Check whether to allocate the iovec area */
1961 iov_size
= msg_sys
.msg_iovlen
* sizeof(struct iovec
);
1962 if (msg_sys
.msg_iovlen
> UIO_FASTIOV
) {
1963 iov
= sock_kmalloc(sock
->sk
, iov_size
, GFP_KERNEL
);
1968 /* This will also move the address data into kernel space */
1969 if (MSG_CMSG_COMPAT
& flags
) {
1970 err
= verify_compat_iovec(&msg_sys
, iov
,
1971 (struct sockaddr
*)&address
,
1974 err
= verify_iovec(&msg_sys
, iov
,
1975 (struct sockaddr
*)&address
,
1983 if (msg_sys
.msg_controllen
> INT_MAX
)
1985 ctl_len
= msg_sys
.msg_controllen
;
1986 if ((MSG_CMSG_COMPAT
& flags
) && ctl_len
) {
1988 cmsghdr_from_user_compat_to_kern(&msg_sys
, sock
->sk
, ctl
,
1992 ctl_buf
= msg_sys
.msg_control
;
1993 ctl_len
= msg_sys
.msg_controllen
;
1994 } else if (ctl_len
) {
1995 if (ctl_len
> sizeof(ctl
)) {
1996 ctl_buf
= sock_kmalloc(sock
->sk
, ctl_len
, GFP_KERNEL
);
1997 if (ctl_buf
== NULL
)
2002 * Careful! Before this, msg_sys.msg_control contains a user pointer.
2003 * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
2004 * checking falls down on this.
2006 if (copy_from_user(ctl_buf
, (void __user
*)msg_sys
.msg_control
,
2009 msg_sys
.msg_control
= ctl_buf
;
2011 msg_sys
.msg_flags
= flags
;
2013 if (sock
->file
->f_flags
& O_NONBLOCK
)
2014 msg_sys
.msg_flags
|= MSG_DONTWAIT
;
2015 err
= sock_sendmsg(sock
, &msg_sys
, total_len
);
2019 sock_kfree_s(sock
->sk
, ctl_buf
, ctl_len
);
2021 if (iov
!= iovstack
)
2022 sock_kfree_s(sock
->sk
, iov
, iov_size
);
2024 fput_light(sock
->file
, fput_needed
);
2029 static int __sys_recvmsg(struct socket
*sock
, struct msghdr __user
*msg
,
2030 struct msghdr
*msg_sys
, unsigned flags
, int nosec
)
2032 struct compat_msghdr __user
*msg_compat
=
2033 (struct compat_msghdr __user
*)msg
;
2034 struct iovec iovstack
[UIO_FASTIOV
];
2035 struct iovec
*iov
= iovstack
;
2036 unsigned long cmsg_ptr
;
2037 int err
, iov_size
, total_len
, len
;
2039 /* kernel mode address */
2040 struct sockaddr_storage addr
;
2042 /* user mode address pointers */
2043 struct sockaddr __user
*uaddr
;
2044 int __user
*uaddr_len
;
2046 if (MSG_CMSG_COMPAT
& flags
) {
2047 if (get_compat_msghdr(msg_sys
, msg_compat
))
2050 else if (copy_from_user(msg_sys
, msg
, sizeof(struct msghdr
)))
2054 if (msg_sys
->msg_iovlen
> UIO_MAXIOV
)
2057 /* Check whether to allocate the iovec area */
2059 iov_size
= msg_sys
->msg_iovlen
* sizeof(struct iovec
);
2060 if (msg_sys
->msg_iovlen
> UIO_FASTIOV
) {
2061 iov
= sock_kmalloc(sock
->sk
, iov_size
, GFP_KERNEL
);
2067 * Save the user-mode address (verify_iovec will change the
2068 * kernel msghdr to use the kernel address space)
2071 uaddr
= (__force
void __user
*)msg_sys
->msg_name
;
2072 uaddr_len
= COMPAT_NAMELEN(msg
);
2073 if (MSG_CMSG_COMPAT
& flags
) {
2074 err
= verify_compat_iovec(msg_sys
, iov
,
2075 (struct sockaddr
*)&addr
,
2078 err
= verify_iovec(msg_sys
, iov
,
2079 (struct sockaddr
*)&addr
,
2085 cmsg_ptr
= (unsigned long)msg_sys
->msg_control
;
2086 msg_sys
->msg_flags
= flags
& (MSG_CMSG_CLOEXEC
|MSG_CMSG_COMPAT
);
2088 if (sock
->file
->f_flags
& O_NONBLOCK
)
2089 flags
|= MSG_DONTWAIT
;
2090 err
= (nosec
? sock_recvmsg_nosec
: sock_recvmsg
)(sock
, msg_sys
,
2096 if (uaddr
!= NULL
) {
2097 err
= move_addr_to_user((struct sockaddr
*)&addr
,
2098 msg_sys
->msg_namelen
, uaddr
,
2103 err
= __put_user((msg_sys
->msg_flags
& ~MSG_CMSG_COMPAT
),
2107 if (MSG_CMSG_COMPAT
& flags
)
2108 err
= __put_user((unsigned long)msg_sys
->msg_control
- cmsg_ptr
,
2109 &msg_compat
->msg_controllen
);
2111 err
= __put_user((unsigned long)msg_sys
->msg_control
- cmsg_ptr
,
2112 &msg
->msg_controllen
);
2118 if (iov
!= iovstack
)
2119 sock_kfree_s(sock
->sk
, iov
, iov_size
);
2125 * BSD recvmsg interface
2128 SYSCALL_DEFINE3(recvmsg
, int, fd
, struct msghdr __user
*, msg
,
2129 unsigned int, flags
)
2131 int fput_needed
, err
;
2132 struct msghdr msg_sys
;
2133 struct socket
*sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
2138 err
= __sys_recvmsg(sock
, msg
, &msg_sys
, flags
, 0);
2140 fput_light(sock
->file
, fput_needed
);
2146 * Linux recvmmsg interface
2149 int __sys_recvmmsg(int fd
, struct mmsghdr __user
*mmsg
, unsigned int vlen
,
2150 unsigned int flags
, struct timespec
*timeout
)
2152 int fput_needed
, err
, datagrams
;
2153 struct socket
*sock
;
2154 struct mmsghdr __user
*entry
;
2155 struct msghdr msg_sys
;
2156 struct timespec end_time
;
2159 poll_select_set_timeout(&end_time
, timeout
->tv_sec
,
2165 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
2169 err
= sock_error(sock
->sk
);
2175 while (datagrams
< vlen
) {
2177 * No need to ask LSM for more than the first datagram.
2179 err
= __sys_recvmsg(sock
, (struct msghdr __user
*)entry
,
2180 &msg_sys
, flags
, datagrams
);
2183 err
= put_user(err
, &entry
->msg_len
);
2190 ktime_get_ts(timeout
);
2191 *timeout
= timespec_sub(end_time
, *timeout
);
2192 if (timeout
->tv_sec
< 0) {
2193 timeout
->tv_sec
= timeout
->tv_nsec
= 0;
2197 /* Timeout, return less than vlen datagrams */
2198 if (timeout
->tv_nsec
== 0 && timeout
->tv_sec
== 0)
2202 /* Out of band data, return right away */
2203 if (msg_sys
.msg_flags
& MSG_OOB
)
2208 fput_light(sock
->file
, fput_needed
);
2213 if (datagrams
!= 0) {
2215 * We may return less entries than requested (vlen) if the
2216 * sock is non block and there aren't enough datagrams...
2218 if (err
!= -EAGAIN
) {
2220 * ... or if recvmsg returns an error after we
2221 * received some datagrams, where we record the
2222 * error to return on the next call or if the
2223 * app asks about it using getsockopt(SO_ERROR).
2225 sock
->sk
->sk_err
= -err
;
2234 SYSCALL_DEFINE5(recvmmsg
, int, fd
, struct mmsghdr __user
*, mmsg
,
2235 unsigned int, vlen
, unsigned int, flags
,
2236 struct timespec __user
*, timeout
)
2239 struct timespec timeout_sys
;
2242 return __sys_recvmmsg(fd
, mmsg
, vlen
, flags
, NULL
);
2244 if (copy_from_user(&timeout_sys
, timeout
, sizeof(timeout_sys
)))
2247 datagrams
= __sys_recvmmsg(fd
, mmsg
, vlen
, flags
, &timeout_sys
);
2249 if (datagrams
> 0 &&
2250 copy_to_user(timeout
, &timeout_sys
, sizeof(timeout_sys
)))
2251 datagrams
= -EFAULT
;
2256 #ifdef __ARCH_WANT_SYS_SOCKETCALL
2257 /* Argument list sizes for sys_socketcall */
2258 #define AL(x) ((x) * sizeof(unsigned long))
2259 static const unsigned char nargs
[20] = {
2260 AL(0),AL(3),AL(3),AL(3),AL(2),AL(3),
2261 AL(3),AL(3),AL(4),AL(4),AL(4),AL(6),
2262 AL(6),AL(2),AL(5),AL(5),AL(3),AL(3),
2269 * System call vectors.
2271 * Argument checking cleaned up. Saved 20% in size.
2272 * This function doesn't need to set the kernel lock because
2273 * it is set by the callees.
2276 SYSCALL_DEFINE2(socketcall
, int, call
, unsigned long __user
*, args
)
2279 unsigned long a0
, a1
;
2283 if (call
< 1 || call
> SYS_RECVMMSG
)
2287 if (len
> sizeof(a
))
2290 /* copy_from_user should be SMP safe. */
2291 if (copy_from_user(a
, args
, len
))
2294 audit_socketcall(nargs
[call
] / sizeof(unsigned long), a
);
2301 err
= sys_socket(a0
, a1
, a
[2]);
2304 err
= sys_bind(a0
, (struct sockaddr __user
*)a1
, a
[2]);
2307 err
= sys_connect(a0
, (struct sockaddr __user
*)a1
, a
[2]);
2310 err
= sys_listen(a0
, a1
);
2313 err
= sys_accept4(a0
, (struct sockaddr __user
*)a1
,
2314 (int __user
*)a
[2], 0);
2316 case SYS_GETSOCKNAME
:
2318 sys_getsockname(a0
, (struct sockaddr __user
*)a1
,
2319 (int __user
*)a
[2]);
2321 case SYS_GETPEERNAME
:
2323 sys_getpeername(a0
, (struct sockaddr __user
*)a1
,
2324 (int __user
*)a
[2]);
2326 case SYS_SOCKETPAIR
:
2327 err
= sys_socketpair(a0
, a1
, a
[2], (int __user
*)a
[3]);
2330 err
= sys_send(a0
, (void __user
*)a1
, a
[2], a
[3]);
2333 err
= sys_sendto(a0
, (void __user
*)a1
, a
[2], a
[3],
2334 (struct sockaddr __user
*)a
[4], a
[5]);
2337 err
= sys_recv(a0
, (void __user
*)a1
, a
[2], a
[3]);
2340 err
= sys_recvfrom(a0
, (void __user
*)a1
, a
[2], a
[3],
2341 (struct sockaddr __user
*)a
[4],
2342 (int __user
*)a
[5]);
2345 err
= sys_shutdown(a0
, a1
);
2347 case SYS_SETSOCKOPT
:
2348 err
= sys_setsockopt(a0
, a1
, a
[2], (char __user
*)a
[3], a
[4]);
2350 case SYS_GETSOCKOPT
:
2352 sys_getsockopt(a0
, a1
, a
[2], (char __user
*)a
[3],
2353 (int __user
*)a
[4]);
2356 err
= sys_sendmsg(a0
, (struct msghdr __user
*)a1
, a
[2]);
2359 err
= sys_recvmsg(a0
, (struct msghdr __user
*)a1
, a
[2]);
2362 err
= sys_recvmmsg(a0
, (struct mmsghdr __user
*)a1
, a
[2], a
[3],
2363 (struct timespec __user
*)a
[4]);
2366 err
= sys_accept4(a0
, (struct sockaddr __user
*)a1
,
2367 (int __user
*)a
[2], a
[3]);
2376 #endif /* __ARCH_WANT_SYS_SOCKETCALL */
2379 * sock_register - add a socket protocol handler
2380 * @ops: description of protocol
2382 * This function is called by a protocol handler that wants to
2383 * advertise its address family, and have it linked into the
2384 * socket interface. The value ops->family coresponds to the
2385 * socket system call protocol family.
2387 int sock_register(const struct net_proto_family
*ops
)
2391 if (ops
->family
>= NPROTO
) {
2392 printk(KERN_CRIT
"protocol %d >= NPROTO(%d)\n", ops
->family
,
2397 spin_lock(&net_family_lock
);
2398 if (net_families
[ops
->family
])
2401 net_families
[ops
->family
] = ops
;
2404 spin_unlock(&net_family_lock
);
2406 printk(KERN_INFO
"NET: Registered protocol family %d\n", ops
->family
);
2411 * sock_unregister - remove a protocol handler
2412 * @family: protocol family to remove
2414 * This function is called by a protocol handler that wants to
2415 * remove its address family, and have it unlinked from the
2416 * new socket creation.
2418 * If protocol handler is a module, then it can use module reference
2419 * counts to protect against new references. If protocol handler is not
2420 * a module then it needs to provide its own protection in
2421 * the ops->create routine.
2423 void sock_unregister(int family
)
2425 BUG_ON(family
< 0 || family
>= NPROTO
);
2427 spin_lock(&net_family_lock
);
2428 net_families
[family
] = NULL
;
2429 spin_unlock(&net_family_lock
);
2433 printk(KERN_INFO
"NET: Unregistered protocol family %d\n", family
);
2436 static int __init
sock_init(void)
2439 * Initialize sock SLAB cache.
2445 * Initialize skbuff SLAB cache
2450 * Initialize the protocols module.
2454 register_filesystem(&sock_fs_type
);
2455 sock_mnt
= kern_mount(&sock_fs_type
);
2457 /* The real protocol initialization is performed in later initcalls.
2460 #ifdef CONFIG_NETFILTER
2467 core_initcall(sock_init
); /* early initcall */
2469 #ifdef CONFIG_PROC_FS
2470 void socket_seq_show(struct seq_file
*seq
)
2475 for_each_possible_cpu(cpu
)
2476 counter
+= per_cpu(sockets_in_use
, cpu
);
2478 /* It can be negative, by the way. 8) */
2482 seq_printf(seq
, "sockets: used %d\n", counter
);
2484 #endif /* CONFIG_PROC_FS */
2486 #ifdef CONFIG_COMPAT
2487 static int do_siocgstamp(struct net
*net
, struct socket
*sock
,
2488 unsigned int cmd
, struct compat_timeval __user
*up
)
2490 mm_segment_t old_fs
= get_fs();
2495 err
= sock_do_ioctl(net
, sock
, cmd
, (unsigned long)&ktv
);
2498 err
= put_user(ktv
.tv_sec
, &up
->tv_sec
);
2499 err
|= __put_user(ktv
.tv_usec
, &up
->tv_usec
);
2504 static int do_siocgstampns(struct net
*net
, struct socket
*sock
,
2505 unsigned int cmd
, struct compat_timespec __user
*up
)
2507 mm_segment_t old_fs
= get_fs();
2508 struct timespec kts
;
2512 err
= sock_do_ioctl(net
, sock
, cmd
, (unsigned long)&kts
);
2515 err
= put_user(kts
.tv_sec
, &up
->tv_sec
);
2516 err
|= __put_user(kts
.tv_nsec
, &up
->tv_nsec
);
2521 static int dev_ifname32(struct net
*net
, struct compat_ifreq __user
*uifr32
)
2523 struct ifreq __user
*uifr
;
2526 uifr
= compat_alloc_user_space(sizeof(struct ifreq
));
2527 if (copy_in_user(uifr
, uifr32
, sizeof(struct compat_ifreq
)))
2530 err
= dev_ioctl(net
, SIOCGIFNAME
, uifr
);
2534 if (copy_in_user(uifr32
, uifr
, sizeof(struct compat_ifreq
)))
2540 static int dev_ifconf(struct net
*net
, struct compat_ifconf __user
*uifc32
)
2542 struct compat_ifconf ifc32
;
2544 struct ifconf __user
*uifc
;
2545 struct compat_ifreq __user
*ifr32
;
2546 struct ifreq __user
*ifr
;
2550 if (copy_from_user(&ifc32
, uifc32
, sizeof(struct compat_ifconf
)))
2553 if (ifc32
.ifcbuf
== 0) {
2557 uifc
= compat_alloc_user_space(sizeof(struct ifconf
));
2559 size_t len
=((ifc32
.ifc_len
/ sizeof (struct compat_ifreq
)) + 1) *
2560 sizeof (struct ifreq
);
2561 uifc
= compat_alloc_user_space(sizeof(struct ifconf
) + len
);
2563 ifr
= ifc
.ifc_req
= (void __user
*)(uifc
+ 1);
2564 ifr32
= compat_ptr(ifc32
.ifcbuf
);
2565 for (i
= 0; i
< ifc32
.ifc_len
; i
+= sizeof (struct compat_ifreq
)) {
2566 if (copy_in_user(ifr
, ifr32
, sizeof(struct compat_ifreq
)))
2572 if (copy_to_user(uifc
, &ifc
, sizeof(struct ifconf
)))
2575 err
= dev_ioctl(net
, SIOCGIFCONF
, uifc
);
2579 if (copy_from_user(&ifc
, uifc
, sizeof(struct ifconf
)))
2583 ifr32
= compat_ptr(ifc32
.ifcbuf
);
2585 i
+ sizeof (struct compat_ifreq
) <= ifc32
.ifc_len
&& j
< ifc
.ifc_len
;
2586 i
+= sizeof (struct compat_ifreq
), j
+= sizeof (struct ifreq
)) {
2587 if (copy_in_user(ifr32
, ifr
, sizeof (struct compat_ifreq
)))
2593 if (ifc32
.ifcbuf
== 0) {
2594 /* Translate from 64-bit structure multiple to
2598 i
= ((i
/ sizeof(struct ifreq
)) * sizeof(struct compat_ifreq
));
2603 if (copy_to_user(uifc32
, &ifc32
, sizeof(struct compat_ifconf
)))
2609 static int ethtool_ioctl(struct net
*net
, struct compat_ifreq __user
*ifr32
)
2611 struct ifreq __user
*ifr
;
2615 ifr
= compat_alloc_user_space(sizeof(*ifr
));
2617 if (copy_in_user(&ifr
->ifr_name
, &ifr32
->ifr_name
, IFNAMSIZ
))
2620 if (get_user(data
, &ifr32
->ifr_ifru
.ifru_data
))
2623 datap
= compat_ptr(data
);
2624 if (put_user(datap
, &ifr
->ifr_ifru
.ifru_data
))
2627 return dev_ioctl(net
, SIOCETHTOOL
, ifr
);
2630 static int compat_siocwandev(struct net
*net
, struct compat_ifreq __user
*uifr32
)
2633 compat_uptr_t uptr32
;
2634 struct ifreq __user
*uifr
;
2636 uifr
= compat_alloc_user_space(sizeof (*uifr
));
2637 if (copy_in_user(uifr
, uifr32
, sizeof(struct compat_ifreq
)))
2640 if (get_user(uptr32
, &uifr32
->ifr_settings
.ifs_ifsu
))
2643 uptr
= compat_ptr(uptr32
);
2645 if (put_user(uptr
, &uifr
->ifr_settings
.ifs_ifsu
.raw_hdlc
))
2648 return dev_ioctl(net
, SIOCWANDEV
, uifr
);
2651 static int bond_ioctl(struct net
*net
, unsigned int cmd
,
2652 struct compat_ifreq __user
*ifr32
)
2655 struct ifreq __user
*uifr
;
2656 mm_segment_t old_fs
;
2662 case SIOCBONDENSLAVE
:
2663 case SIOCBONDRELEASE
:
2664 case SIOCBONDSETHWADDR
:
2665 case SIOCBONDCHANGEACTIVE
:
2666 if (copy_from_user(&kifr
, ifr32
, sizeof(struct compat_ifreq
)))
2671 err
= dev_ioctl(net
, cmd
, &kifr
);
2675 case SIOCBONDSLAVEINFOQUERY
:
2676 case SIOCBONDINFOQUERY
:
2677 uifr
= compat_alloc_user_space(sizeof(*uifr
));
2678 if (copy_in_user(&uifr
->ifr_name
, &ifr32
->ifr_name
, IFNAMSIZ
))
2681 if (get_user(data
, &ifr32
->ifr_ifru
.ifru_data
))
2684 datap
= compat_ptr(data
);
2685 if (put_user(datap
, &uifr
->ifr_ifru
.ifru_data
))
2688 return dev_ioctl(net
, cmd
, uifr
);
2694 static int siocdevprivate_ioctl(struct net
*net
, unsigned int cmd
,
2695 struct compat_ifreq __user
*u_ifreq32
)
2697 struct ifreq __user
*u_ifreq64
;
2698 char tmp_buf
[IFNAMSIZ
];
2699 void __user
*data64
;
2702 if (copy_from_user(&tmp_buf
[0], &(u_ifreq32
->ifr_ifrn
.ifrn_name
[0]),
2705 if (__get_user(data32
, &u_ifreq32
->ifr_ifru
.ifru_data
))
2707 data64
= compat_ptr(data32
);
2709 u_ifreq64
= compat_alloc_user_space(sizeof(*u_ifreq64
));
2711 /* Don't check these user accesses, just let that get trapped
2712 * in the ioctl handler instead.
2714 if (copy_to_user(&u_ifreq64
->ifr_ifrn
.ifrn_name
[0], &tmp_buf
[0],
2717 if (__put_user(data64
, &u_ifreq64
->ifr_ifru
.ifru_data
))
2720 return dev_ioctl(net
, cmd
, u_ifreq64
);
2723 static int dev_ifsioc(struct net
*net
, struct socket
*sock
,
2724 unsigned int cmd
, struct compat_ifreq __user
*uifr32
)
2727 struct compat_ifmap __user
*uifmap32
;
2728 mm_segment_t old_fs
;
2731 uifmap32
= &uifr32
->ifr_ifru
.ifru_map
;
2734 err
= copy_from_user(&ifr
, uifr32
, sizeof(ifr
.ifr_name
));
2735 err
|= __get_user(ifr
.ifr_map
.mem_start
, &uifmap32
->mem_start
);
2736 err
|= __get_user(ifr
.ifr_map
.mem_end
, &uifmap32
->mem_end
);
2737 err
|= __get_user(ifr
.ifr_map
.base_addr
, &uifmap32
->base_addr
);
2738 err
|= __get_user(ifr
.ifr_map
.irq
, &uifmap32
->irq
);
2739 err
|= __get_user(ifr
.ifr_map
.dma
, &uifmap32
->dma
);
2740 err
|= __get_user(ifr
.ifr_map
.port
, &uifmap32
->port
);
2745 if (copy_from_user(&ifr
, uifr32
, sizeof(*uifr32
)))
2747 ifr
.ifr_data
= compat_ptr(uifr32
->ifr_ifru
.ifru_data
);
2750 if (copy_from_user(&ifr
, uifr32
, sizeof(*uifr32
)))
2756 err
= sock_do_ioctl(net
, sock
, cmd
, (unsigned long)&ifr
);
2767 case SIOCGIFBRDADDR
:
2768 case SIOCGIFDSTADDR
:
2769 case SIOCGIFNETMASK
:
2774 if (copy_to_user(uifr32
, &ifr
, sizeof(*uifr32
)))
2778 err
= copy_to_user(uifr32
, &ifr
, sizeof(ifr
.ifr_name
));
2779 err
|= __put_user(ifr
.ifr_map
.mem_start
, &uifmap32
->mem_start
);
2780 err
|= __put_user(ifr
.ifr_map
.mem_end
, &uifmap32
->mem_end
);
2781 err
|= __put_user(ifr
.ifr_map
.base_addr
, &uifmap32
->base_addr
);
2782 err
|= __put_user(ifr
.ifr_map
.irq
, &uifmap32
->irq
);
2783 err
|= __put_user(ifr
.ifr_map
.dma
, &uifmap32
->dma
);
2784 err
|= __put_user(ifr
.ifr_map
.port
, &uifmap32
->port
);
2795 struct sockaddr rt_dst
; /* target address */
2796 struct sockaddr rt_gateway
; /* gateway addr (RTF_GATEWAY) */
2797 struct sockaddr rt_genmask
; /* target network mask (IP) */
2798 unsigned short rt_flags
;
2801 unsigned char rt_tos
;
2802 unsigned char rt_class
;
2804 short rt_metric
; /* +1 for binary compatibility! */
2805 /* char * */ u32 rt_dev
; /* forcing the device at add */
2806 u32 rt_mtu
; /* per route MTU/Window */
2807 u32 rt_window
; /* Window clamping */
2808 unsigned short rt_irtt
; /* Initial RTT */
2811 struct in6_rtmsg32
{
2812 struct in6_addr rtmsg_dst
;
2813 struct in6_addr rtmsg_src
;
2814 struct in6_addr rtmsg_gateway
;
2824 static int routing_ioctl(struct net
*net
, struct socket
*sock
,
2825 unsigned int cmd
, void __user
*argp
)
2829 struct in6_rtmsg r6
;
2833 mm_segment_t old_fs
= get_fs();
2835 if (sock
&& sock
->sk
&& sock
->sk
->sk_family
== AF_INET6
) { /* ipv6 */
2836 struct in6_rtmsg32 __user
*ur6
= argp
;
2837 ret
= copy_from_user (&r6
.rtmsg_dst
, &(ur6
->rtmsg_dst
),
2838 3 * sizeof(struct in6_addr
));
2839 ret
|= __get_user (r6
.rtmsg_type
, &(ur6
->rtmsg_type
));
2840 ret
|= __get_user (r6
.rtmsg_dst_len
, &(ur6
->rtmsg_dst_len
));
2841 ret
|= __get_user (r6
.rtmsg_src_len
, &(ur6
->rtmsg_src_len
));
2842 ret
|= __get_user (r6
.rtmsg_metric
, &(ur6
->rtmsg_metric
));
2843 ret
|= __get_user (r6
.rtmsg_info
, &(ur6
->rtmsg_info
));
2844 ret
|= __get_user (r6
.rtmsg_flags
, &(ur6
->rtmsg_flags
));
2845 ret
|= __get_user (r6
.rtmsg_ifindex
, &(ur6
->rtmsg_ifindex
));
2849 struct rtentry32 __user
*ur4
= argp
;
2850 ret
= copy_from_user (&r4
.rt_dst
, &(ur4
->rt_dst
),
2851 3 * sizeof(struct sockaddr
));
2852 ret
|= __get_user (r4
.rt_flags
, &(ur4
->rt_flags
));
2853 ret
|= __get_user (r4
.rt_metric
, &(ur4
->rt_metric
));
2854 ret
|= __get_user (r4
.rt_mtu
, &(ur4
->rt_mtu
));
2855 ret
|= __get_user (r4
.rt_window
, &(ur4
->rt_window
));
2856 ret
|= __get_user (r4
.rt_irtt
, &(ur4
->rt_irtt
));
2857 ret
|= __get_user (rtdev
, &(ur4
->rt_dev
));
2859 ret
|= copy_from_user (devname
, compat_ptr(rtdev
), 15);
2860 r4
.rt_dev
= devname
; devname
[15] = 0;
2873 ret
= sock_do_ioctl(net
, sock
, cmd
, (unsigned long) r
);
2880 /* Since old style bridge ioctl's endup using SIOCDEVPRIVATE
2881 * for some operations; this forces use of the newer bridge-utils that
2882 * use compatiable ioctls
2884 static int old_bridge_ioctl(compat_ulong_t __user
*argp
)
2888 if (get_user(tmp
, argp
))
2890 if (tmp
== BRCTL_GET_VERSION
)
2891 return BRCTL_VERSION
+ 1;
2895 struct atmif_sioc32
{
2896 compat_int_t number
;
2897 compat_int_t length
;
2901 struct atm_iobuf32
{
2902 compat_int_t length
;
2903 compat_caddr_t buffer
;
2906 #define ATM_GETLINKRATE32 _IOW('a', ATMIOC_ITF+1, struct atmif_sioc32)
2907 #define ATM_GETNAMES32 _IOW('a', ATMIOC_ITF+3, struct atm_iobuf32)
2908 #define ATM_GETTYPE32 _IOW('a', ATMIOC_ITF+4, struct atmif_sioc32)
2909 #define ATM_GETESI32 _IOW('a', ATMIOC_ITF+5, struct atmif_sioc32)
2910 #define ATM_GETADDR32 _IOW('a', ATMIOC_ITF+6, struct atmif_sioc32)
2911 #define ATM_RSTADDR32 _IOW('a', ATMIOC_ITF+7, struct atmif_sioc32)
2912 #define ATM_ADDADDR32 _IOW('a', ATMIOC_ITF+8, struct atmif_sioc32)
2913 #define ATM_DELADDR32 _IOW('a', ATMIOC_ITF+9, struct atmif_sioc32)
2914 #define ATM_GETCIRANGE32 _IOW('a', ATMIOC_ITF+10, struct atmif_sioc32)
2915 #define ATM_SETCIRANGE32 _IOW('a', ATMIOC_ITF+11, struct atmif_sioc32)
2916 #define ATM_SETESI32 _IOW('a', ATMIOC_ITF+12, struct atmif_sioc32)
2917 #define ATM_SETESIF32 _IOW('a', ATMIOC_ITF+13, struct atmif_sioc32)
2918 #define ATM_GETSTAT32 _IOW('a', ATMIOC_SARCOM+0, struct atmif_sioc32)
2919 #define ATM_GETSTATZ32 _IOW('a', ATMIOC_SARCOM+1, struct atmif_sioc32)
2920 #define ATM_GETLOOP32 _IOW('a', ATMIOC_SARCOM+2, struct atmif_sioc32)
2921 #define ATM_SETLOOP32 _IOW('a', ATMIOC_SARCOM+3, struct atmif_sioc32)
2922 #define ATM_QUERYLOOP32 _IOW('a', ATMIOC_SARCOM+4, struct atmif_sioc32)
2927 } atm_ioctl_map
[] = {
2928 { ATM_GETLINKRATE32
, ATM_GETLINKRATE
},
2929 { ATM_GETNAMES32
, ATM_GETNAMES
},
2930 { ATM_GETTYPE32
, ATM_GETTYPE
},
2931 { ATM_GETESI32
, ATM_GETESI
},
2932 { ATM_GETADDR32
, ATM_GETADDR
},
2933 { ATM_RSTADDR32
, ATM_RSTADDR
},
2934 { ATM_ADDADDR32
, ATM_ADDADDR
},
2935 { ATM_DELADDR32
, ATM_DELADDR
},
2936 { ATM_GETCIRANGE32
, ATM_GETCIRANGE
},
2937 { ATM_SETCIRANGE32
, ATM_SETCIRANGE
},
2938 { ATM_SETESI32
, ATM_SETESI
},
2939 { ATM_SETESIF32
, ATM_SETESIF
},
2940 { ATM_GETSTAT32
, ATM_GETSTAT
},
2941 { ATM_GETSTATZ32
, ATM_GETSTATZ
},
2942 { ATM_GETLOOP32
, ATM_GETLOOP
},
2943 { ATM_SETLOOP32
, ATM_SETLOOP
},
2944 { ATM_QUERYLOOP32
, ATM_QUERYLOOP
}
2947 #define NR_ATM_IOCTL ARRAY_SIZE(atm_ioctl_map)
2949 static int do_atm_iobuf(struct net
*net
, struct socket
*sock
,
2950 unsigned int cmd
, unsigned long arg
)
2952 struct atm_iobuf __user
*iobuf
;
2953 struct atm_iobuf32 __user
*iobuf32
;
2958 iobuf
= compat_alloc_user_space(sizeof(*iobuf
));
2959 iobuf32
= compat_ptr(arg
);
2961 if (get_user(len
, &iobuf32
->length
) ||
2962 get_user(data
, &iobuf32
->buffer
))
2964 datap
= compat_ptr(data
);
2965 if (put_user(len
, &iobuf
->length
) ||
2966 put_user(datap
, &iobuf
->buffer
))
2969 err
= sock_do_ioctl(net
, sock
, cmd
, (unsigned long)iobuf
);
2972 if (copy_in_user(&iobuf32
->length
, &iobuf
->length
,
2980 static int do_atmif_sioc(struct net
*net
, struct socket
*sock
,
2981 unsigned int cmd
, unsigned long arg
)
2983 struct atmif_sioc __user
*sioc
;
2984 struct atmif_sioc32 __user
*sioc32
;
2989 sioc
= compat_alloc_user_space(sizeof(*sioc
));
2990 sioc32
= compat_ptr(arg
);
2992 if (copy_in_user(&sioc
->number
, &sioc32
->number
, 2 * sizeof(int)) ||
2993 get_user(data
, &sioc32
->arg
))
2995 datap
= compat_ptr(data
);
2996 if (put_user(datap
, &sioc
->arg
))
2999 err
= sock_do_ioctl(net
, sock
, cmd
, (unsigned long) sioc
);
3002 if (copy_in_user(&sioc32
->length
, &sioc
->length
,
3009 static int do_atm_ioctl(struct net
*net
, struct socket
*sock
,
3010 unsigned int cmd32
, unsigned long arg
)
3013 unsigned int cmd
= 0;
3017 case SONET_GETSTATZ
:
3021 case SONET_SETFRAMING
:
3022 case SONET_GETFRAMING
:
3023 case SONET_GETFRSENSE
:
3024 return do_atmif_sioc(net
, sock
, cmd32
, arg
);
3027 for (i
= 0; i
< NR_ATM_IOCTL
; i
++) {
3028 if (cmd32
== atm_ioctl_map
[i
].cmd32
) {
3029 cmd
= atm_ioctl_map
[i
].cmd
;
3033 if (i
== NR_ATM_IOCTL
)
3038 return do_atm_iobuf(net
, sock
, cmd
, arg
);
3040 case ATM_GETLINKRATE
:
3047 case ATM_GETCIRANGE
:
3048 case ATM_SETCIRANGE
:
3056 return do_atmif_sioc(net
, sock
, cmd
, arg
);
3062 static int compat_sock_ioctl_trans(struct file
*file
, struct socket
*sock
,
3063 unsigned int cmd
, unsigned long arg
)
3065 void __user
*argp
= compat_ptr(arg
);
3066 struct sock
*sk
= sock
->sk
;
3067 struct net
*net
= sock_net(sk
);
3069 if (cmd
>= SIOCDEVPRIVATE
&& cmd
<= (SIOCDEVPRIVATE
+ 15))
3070 return siocdevprivate_ioctl(net
, cmd
, argp
);
3075 return old_bridge_ioctl(argp
);
3077 return dev_ifname32(net
, argp
);
3079 return dev_ifconf(net
, argp
);
3081 return ethtool_ioctl(net
, argp
);
3083 return compat_siocwandev(net
, argp
);
3084 case SIOCBONDENSLAVE
:
3085 case SIOCBONDRELEASE
:
3086 case SIOCBONDSETHWADDR
:
3087 case SIOCBONDSLAVEINFOQUERY
:
3088 case SIOCBONDINFOQUERY
:
3089 case SIOCBONDCHANGEACTIVE
:
3090 return bond_ioctl(net
, cmd
, argp
);
3093 return routing_ioctl(net
, sock
, cmd
, argp
);
3095 return do_siocgstamp(net
, sock
, cmd
, argp
);
3097 return do_siocgstampns(net
, sock
, cmd
, argp
);
3109 return sock_ioctl(file
, cmd
, arg
);
3128 case SIOCSIFHWBROADCAST
:
3131 case SIOCGIFBRDADDR
:
3132 case SIOCSIFBRDADDR
:
3133 case SIOCGIFDSTADDR
:
3134 case SIOCSIFDSTADDR
:
3135 case SIOCGIFNETMASK
:
3136 case SIOCSIFNETMASK
:
3147 return dev_ifsioc(net
, sock
, cmd
, argp
);
3149 case ATM_GETLINKRATE32
:
3150 case ATM_GETNAMES32
:
3157 case ATM_GETCIRANGE32
:
3158 case ATM_SETCIRANGE32
:
3162 case ATM_GETSTATZ32
:
3165 case ATM_QUERYLOOP32
:
3167 case SONET_GETSTATZ
:
3171 case SONET_SETFRAMING
:
3172 case SONET_GETFRAMING
:
3173 case SONET_GETFRSENSE
:
3174 return do_atm_ioctl(net
, sock
, cmd
, arg
);
3185 case ATMARP_SETENTRY
:
3196 return sock_do_ioctl(net
, sock
, cmd
, arg
);
3199 /* Prevent warning from compat_sys_ioctl, these always
3200 * result in -EINVAL in the native case anyway. */
3213 return -ENOIOCTLCMD
;
3216 static long compat_sock_ioctl(struct file
*file
, unsigned cmd
,
3219 struct socket
*sock
= file
->private_data
;
3220 int ret
= -ENOIOCTLCMD
;
3227 if (sock
->ops
->compat_ioctl
)
3228 ret
= sock
->ops
->compat_ioctl(sock
, cmd
, arg
);
3230 if (ret
== -ENOIOCTLCMD
&&
3231 (cmd
>= SIOCIWFIRST
&& cmd
<= SIOCIWLAST
))
3232 ret
= compat_wext_handle_ioctl(net
, cmd
, arg
);
3234 if (ret
== -ENOIOCTLCMD
)
3235 ret
= compat_sock_ioctl_trans(file
, sock
, cmd
, arg
);
3241 int kernel_bind(struct socket
*sock
, struct sockaddr
*addr
, int addrlen
)
3243 return sock
->ops
->bind(sock
, addr
, addrlen
);
3246 int kernel_listen(struct socket
*sock
, int backlog
)
3248 return sock
->ops
->listen(sock
, backlog
);
3251 int kernel_accept(struct socket
*sock
, struct socket
**newsock
, int flags
)
3253 struct sock
*sk
= sock
->sk
;
3256 err
= sock_create_lite(sk
->sk_family
, sk
->sk_type
, sk
->sk_protocol
,
3261 err
= sock
->ops
->accept(sock
, *newsock
, flags
);
3263 sock_release(*newsock
);
3268 (*newsock
)->ops
= sock
->ops
;
3269 __module_get((*newsock
)->ops
->owner
);
3275 int kernel_connect(struct socket
*sock
, struct sockaddr
*addr
, int addrlen
,
3278 return sock
->ops
->connect(sock
, addr
, addrlen
, flags
);
3281 int kernel_getsockname(struct socket
*sock
, struct sockaddr
*addr
,
3284 return sock
->ops
->getname(sock
, addr
, addrlen
, 0);
3287 int kernel_getpeername(struct socket
*sock
, struct sockaddr
*addr
,
3290 return sock
->ops
->getname(sock
, addr
, addrlen
, 1);
3293 int kernel_getsockopt(struct socket
*sock
, int level
, int optname
,
3294 char *optval
, int *optlen
)
3296 mm_segment_t oldfs
= get_fs();
3300 if (level
== SOL_SOCKET
)
3301 err
= sock_getsockopt(sock
, level
, optname
, optval
, optlen
);
3303 err
= sock
->ops
->getsockopt(sock
, level
, optname
, optval
,
3309 int kernel_setsockopt(struct socket
*sock
, int level
, int optname
,
3310 char *optval
, unsigned int optlen
)
3312 mm_segment_t oldfs
= get_fs();
3316 if (level
== SOL_SOCKET
)
3317 err
= sock_setsockopt(sock
, level
, optname
, optval
, optlen
);
3319 err
= sock
->ops
->setsockopt(sock
, level
, optname
, optval
,
3325 int kernel_sendpage(struct socket
*sock
, struct page
*page
, int offset
,
3326 size_t size
, int flags
)
3328 if (sock
->ops
->sendpage
)
3329 return sock
->ops
->sendpage(sock
, page
, offset
, size
, flags
);
3331 return sock_no_sendpage(sock
, page
, offset
, size
, flags
);
3334 int kernel_sock_ioctl(struct socket
*sock
, int cmd
, unsigned long arg
)
3336 mm_segment_t oldfs
= get_fs();
3340 err
= sock
->ops
->ioctl(sock
, cmd
, arg
);
3346 int kernel_sock_shutdown(struct socket
*sock
, enum sock_shutdown_cmd how
)
3348 return sock
->ops
->shutdown(sock
, how
);
3351 EXPORT_SYMBOL(sock_create
);
3352 EXPORT_SYMBOL(sock_create_kern
);
3353 EXPORT_SYMBOL(sock_create_lite
);
3354 EXPORT_SYMBOL(sock_map_fd
);
3355 EXPORT_SYMBOL(sock_recvmsg
);
3356 EXPORT_SYMBOL(sock_register
);
3357 EXPORT_SYMBOL(sock_release
);
3358 EXPORT_SYMBOL(sock_sendmsg
);
3359 EXPORT_SYMBOL(sock_unregister
);
3360 EXPORT_SYMBOL(sock_wake_async
);
3361 EXPORT_SYMBOL(sockfd_lookup
);
3362 EXPORT_SYMBOL(kernel_sendmsg
);
3363 EXPORT_SYMBOL(kernel_recvmsg
);
3364 EXPORT_SYMBOL(kernel_bind
);
3365 EXPORT_SYMBOL(kernel_listen
);
3366 EXPORT_SYMBOL(kernel_accept
);
3367 EXPORT_SYMBOL(kernel_connect
);
3368 EXPORT_SYMBOL(kernel_getsockname
);
3369 EXPORT_SYMBOL(kernel_getpeername
);
3370 EXPORT_SYMBOL(kernel_getsockopt
);
3371 EXPORT_SYMBOL(kernel_setsockopt
);
3372 EXPORT_SYMBOL(kernel_sendpage
);
3373 EXPORT_SYMBOL(kernel_sock_ioctl
);
3374 EXPORT_SYMBOL(kernel_sock_shutdown
);