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/if_bridge.h>
73 #include <linux/if_frad.h>
74 #include <linux/if_vlan.h>
75 #include <linux/init.h>
76 #include <linux/poll.h>
77 #include <linux/cache.h>
78 #include <linux/module.h>
79 #include <linux/highmem.h>
80 #include <linux/mount.h>
81 #include <linux/security.h>
82 #include <linux/syscalls.h>
83 #include <linux/compat.h>
84 #include <linux/kmod.h>
85 #include <linux/audit.h>
86 #include <linux/wireless.h>
87 #include <linux/nsproxy.h>
88 #include <linux/magic.h>
89 #include <linux/slab.h>
90 #include <linux/xattr.h>
92 #include <asm/uaccess.h>
93 #include <asm/unistd.h>
95 #include <net/compat.h>
97 #include <net/cls_cgroup.h>
100 #include <linux/netfilter.h>
102 #include <linux/if_tun.h>
103 #include <linux/ipv6_route.h>
104 #include <linux/route.h>
105 #include <linux/sockios.h>
106 #include <linux/atalk.h>
108 static int sock_no_open(struct inode
*irrelevant
, struct file
*dontcare
);
109 static ssize_t
sock_aio_read(struct kiocb
*iocb
, const struct iovec
*iov
,
110 unsigned long nr_segs
, loff_t pos
);
111 static ssize_t
sock_aio_write(struct kiocb
*iocb
, const struct iovec
*iov
,
112 unsigned long nr_segs
, loff_t pos
);
113 static int sock_mmap(struct file
*file
, struct vm_area_struct
*vma
);
115 static int sock_close(struct inode
*inode
, struct file
*file
);
116 static unsigned int sock_poll(struct file
*file
,
117 struct poll_table_struct
*wait
);
118 static long sock_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
);
120 static long compat_sock_ioctl(struct file
*file
,
121 unsigned int cmd
, unsigned long arg
);
123 static int sock_fasync(int fd
, struct file
*filp
, int on
);
124 static ssize_t
sock_sendpage(struct file
*file
, struct page
*page
,
125 int offset
, size_t size
, loff_t
*ppos
, int more
);
126 static ssize_t
sock_splice_read(struct file
*file
, loff_t
*ppos
,
127 struct pipe_inode_info
*pipe
, size_t len
,
131 * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
132 * in the operation structures but are done directly via the socketcall() multiplexor.
135 static const struct file_operations socket_file_ops
= {
136 .owner
= THIS_MODULE
,
138 .aio_read
= sock_aio_read
,
139 .aio_write
= sock_aio_write
,
141 .unlocked_ioctl
= sock_ioctl
,
143 .compat_ioctl
= compat_sock_ioctl
,
146 .open
= sock_no_open
, /* special open code to disallow open via /proc */
147 .release
= sock_close
,
148 .fasync
= sock_fasync
,
149 .sendpage
= sock_sendpage
,
150 .splice_write
= generic_splice_sendpage
,
151 .splice_read
= sock_splice_read
,
155 * The protocol list. Each protocol is registered in here.
158 static DEFINE_SPINLOCK(net_family_lock
);
159 static const struct net_proto_family __rcu
*net_families
[NPROTO
] __read_mostly
;
162 * Statistics counters of the socket lists
165 static DEFINE_PER_CPU(int, sockets_in_use
);
169 * Move socket addresses back and forth across the kernel/user
170 * divide and look after the messy bits.
174 * move_addr_to_kernel - copy a socket address into kernel space
175 * @uaddr: Address in user space
176 * @kaddr: Address in kernel space
177 * @ulen: Length in user space
179 * The address is copied into kernel space. If the provided address is
180 * too long an error code of -EINVAL is returned. If the copy gives
181 * invalid addresses -EFAULT is returned. On a success 0 is returned.
184 int move_addr_to_kernel(void __user
*uaddr
, int ulen
, struct sockaddr_storage
*kaddr
)
186 if (ulen
< 0 || ulen
> sizeof(struct sockaddr_storage
))
190 if (copy_from_user(kaddr
, uaddr
, ulen
))
192 return audit_sockaddr(ulen
, kaddr
);
196 * move_addr_to_user - copy an address to user space
197 * @kaddr: kernel space address
198 * @klen: length of address in kernel
199 * @uaddr: user space address
200 * @ulen: pointer to user length field
202 * The value pointed to by ulen on entry is the buffer length available.
203 * This is overwritten with the buffer space used. -EINVAL is returned
204 * if an overlong buffer is specified or a negative buffer size. -EFAULT
205 * is returned if either the buffer or the length field are not
207 * After copying the data up to the limit the user specifies, the true
208 * length of the data is written over the length limit the user
209 * specified. Zero is returned for a success.
212 static int move_addr_to_user(struct sockaddr_storage
*kaddr
, int klen
,
213 void __user
*uaddr
, int __user
*ulen
)
218 BUG_ON(klen
> sizeof(struct sockaddr_storage
));
219 err
= get_user(len
, ulen
);
227 if (audit_sockaddr(klen
, kaddr
))
229 if (copy_to_user(uaddr
, kaddr
, len
))
233 * "fromlen shall refer to the value before truncation.."
236 return __put_user(klen
, ulen
);
239 static struct kmem_cache
*sock_inode_cachep __read_mostly
;
241 static struct inode
*sock_alloc_inode(struct super_block
*sb
)
243 struct socket_alloc
*ei
;
244 struct socket_wq
*wq
;
246 ei
= kmem_cache_alloc(sock_inode_cachep
, GFP_KERNEL
);
249 wq
= kmalloc(sizeof(*wq
), GFP_KERNEL
);
251 kmem_cache_free(sock_inode_cachep
, ei
);
254 init_waitqueue_head(&wq
->wait
);
255 wq
->fasync_list
= NULL
;
256 RCU_INIT_POINTER(ei
->socket
.wq
, wq
);
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 struct socket_alloc
*ei
;
270 struct socket_wq
*wq
;
272 ei
= container_of(inode
, struct socket_alloc
, vfs_inode
);
273 wq
= rcu_dereference_protected(ei
->socket
.wq
, 1);
275 kmem_cache_free(sock_inode_cachep
, ei
);
278 static void init_once(void *foo
)
280 struct socket_alloc
*ei
= (struct socket_alloc
*)foo
;
282 inode_init_once(&ei
->vfs_inode
);
285 static int init_inodecache(void)
287 sock_inode_cachep
= kmem_cache_create("sock_inode_cache",
288 sizeof(struct socket_alloc
),
290 (SLAB_HWCACHE_ALIGN
|
291 SLAB_RECLAIM_ACCOUNT
|
294 if (sock_inode_cachep
== NULL
)
299 static const struct super_operations sockfs_ops
= {
300 .alloc_inode
= sock_alloc_inode
,
301 .destroy_inode
= sock_destroy_inode
,
302 .statfs
= simple_statfs
,
306 * sockfs_dname() is called from d_path().
308 static char *sockfs_dname(struct dentry
*dentry
, char *buffer
, int buflen
)
310 return dynamic_dname(dentry
, buffer
, buflen
, "socket:[%lu]",
311 dentry
->d_inode
->i_ino
);
314 static const struct dentry_operations sockfs_dentry_operations
= {
315 .d_dname
= sockfs_dname
,
318 static struct dentry
*sockfs_mount(struct file_system_type
*fs_type
,
319 int flags
, const char *dev_name
, void *data
)
321 return mount_pseudo(fs_type
, "socket:", &sockfs_ops
,
322 &sockfs_dentry_operations
, SOCKFS_MAGIC
);
325 static struct vfsmount
*sock_mnt __read_mostly
;
327 static struct file_system_type sock_fs_type
= {
329 .mount
= sockfs_mount
,
330 .kill_sb
= kill_anon_super
,
334 * Obtains the first available file descriptor and sets it up for use.
336 * These functions create file structures and maps them to fd space
337 * of the current process. On success it returns file descriptor
338 * and file struct implicitly stored in sock->file.
339 * Note that another thread may close file descriptor before we return
340 * from this function. We use the fact that now we do not refer
341 * to socket after mapping. If one day we will need it, this
342 * function will increment ref. count on file by 1.
344 * In any case returned fd MAY BE not valid!
345 * This race condition is unavoidable
346 * with shared fd spaces, we cannot solve it inside kernel,
347 * but we take care of internal coherence yet.
350 struct file
*sock_alloc_file(struct socket
*sock
, int flags
, const char *dname
)
352 struct qstr name
= { .name
= "" };
358 name
.len
= strlen(name
.name
);
359 } else if (sock
->sk
) {
360 name
.name
= sock
->sk
->sk_prot_creator
->name
;
361 name
.len
= strlen(name
.name
);
363 path
.dentry
= d_alloc_pseudo(sock_mnt
->mnt_sb
, &name
);
364 if (unlikely(!path
.dentry
))
365 return ERR_PTR(-ENOMEM
);
366 path
.mnt
= mntget(sock_mnt
);
368 d_instantiate(path
.dentry
, SOCK_INODE(sock
));
369 SOCK_INODE(sock
)->i_fop
= &socket_file_ops
;
371 file
= alloc_file(&path
, FMODE_READ
| FMODE_WRITE
,
373 if (unlikely(IS_ERR(file
))) {
374 /* drop dentry, keep inode */
375 ihold(path
.dentry
->d_inode
);
381 file
->f_flags
= O_RDWR
| (flags
& O_NONBLOCK
);
382 file
->private_data
= sock
;
385 EXPORT_SYMBOL(sock_alloc_file
);
387 static int sock_map_fd(struct socket
*sock
, int flags
)
389 struct file
*newfile
;
390 int fd
= get_unused_fd_flags(flags
);
391 if (unlikely(fd
< 0))
394 newfile
= sock_alloc_file(sock
, flags
, NULL
);
395 if (likely(!IS_ERR(newfile
))) {
396 fd_install(fd
, newfile
);
401 return PTR_ERR(newfile
);
404 struct socket
*sock_from_file(struct file
*file
, int *err
)
406 if (file
->f_op
== &socket_file_ops
)
407 return file
->private_data
; /* set in sock_map_fd */
412 EXPORT_SYMBOL(sock_from_file
);
415 * sockfd_lookup - Go from a file number to its socket slot
417 * @err: pointer to an error code return
419 * The file handle passed in is locked and the socket it is bound
420 * too is returned. If an error occurs the err pointer is overwritten
421 * with a negative errno code and NULL is returned. The function checks
422 * for both invalid handles and passing a handle which is not a socket.
424 * On a success the socket object pointer is returned.
427 struct socket
*sockfd_lookup(int fd
, int *err
)
438 sock
= sock_from_file(file
, err
);
443 EXPORT_SYMBOL(sockfd_lookup
);
445 static struct socket
*sockfd_lookup_light(int fd
, int *err
, int *fput_needed
)
451 file
= fget_light(fd
, fput_needed
);
453 sock
= sock_from_file(file
, err
);
456 fput_light(file
, *fput_needed
);
461 #define XATTR_SOCKPROTONAME_SUFFIX "sockprotoname"
462 #define XATTR_NAME_SOCKPROTONAME (XATTR_SYSTEM_PREFIX XATTR_SOCKPROTONAME_SUFFIX)
463 #define XATTR_NAME_SOCKPROTONAME_LEN (sizeof(XATTR_NAME_SOCKPROTONAME)-1)
464 static ssize_t
sockfs_getxattr(struct dentry
*dentry
,
465 const char *name
, void *value
, size_t size
)
467 const char *proto_name
;
472 if (!strncmp(name
, XATTR_NAME_SOCKPROTONAME
, XATTR_NAME_SOCKPROTONAME_LEN
)) {
473 proto_name
= dentry
->d_name
.name
;
474 proto_size
= strlen(proto_name
);
478 if (proto_size
+ 1 > size
)
481 strncpy(value
, proto_name
, proto_size
+ 1);
483 error
= proto_size
+ 1;
490 static ssize_t
sockfs_listxattr(struct dentry
*dentry
, char *buffer
,
496 len
= security_inode_listsecurity(dentry
->d_inode
, buffer
, size
);
506 len
= (XATTR_NAME_SOCKPROTONAME_LEN
+ 1);
511 memcpy(buffer
, XATTR_NAME_SOCKPROTONAME
, len
);
518 static const struct inode_operations sockfs_inode_ops
= {
519 .getxattr
= sockfs_getxattr
,
520 .listxattr
= sockfs_listxattr
,
524 * sock_alloc - allocate a socket
526 * Allocate a new inode and socket object. The two are bound together
527 * and initialised. The socket is then returned. If we are out of inodes
531 static struct socket
*sock_alloc(void)
536 inode
= new_inode_pseudo(sock_mnt
->mnt_sb
);
540 sock
= SOCKET_I(inode
);
542 kmemcheck_annotate_bitfield(sock
, type
);
543 inode
->i_ino
= get_next_ino();
544 inode
->i_mode
= S_IFSOCK
| S_IRWXUGO
;
545 inode
->i_uid
= current_fsuid();
546 inode
->i_gid
= current_fsgid();
547 inode
->i_op
= &sockfs_inode_ops
;
549 this_cpu_add(sockets_in_use
, 1);
554 * In theory you can't get an open on this inode, but /proc provides
555 * a back door. Remember to keep it shut otherwise you'll let the
556 * creepy crawlies in.
559 static int sock_no_open(struct inode
*irrelevant
, struct file
*dontcare
)
564 const struct file_operations bad_sock_fops
= {
565 .owner
= THIS_MODULE
,
566 .open
= sock_no_open
,
567 .llseek
= noop_llseek
,
571 * sock_release - close a socket
572 * @sock: socket to close
574 * The socket is released from the protocol stack if it has a release
575 * callback, and the inode is then released if the socket is bound to
576 * an inode not a file.
579 void sock_release(struct socket
*sock
)
582 struct module
*owner
= sock
->ops
->owner
;
584 sock
->ops
->release(sock
);
589 if (rcu_dereference_protected(sock
->wq
, 1)->fasync_list
)
590 printk(KERN_ERR
"sock_release: fasync list not empty!\n");
592 if (test_bit(SOCK_EXTERNALLY_ALLOCATED
, &sock
->flags
))
595 this_cpu_sub(sockets_in_use
, 1);
597 iput(SOCK_INODE(sock
));
602 EXPORT_SYMBOL(sock_release
);
604 void sock_tx_timestamp(struct sock
*sk
, __u8
*tx_flags
)
607 if (sock_flag(sk
, SOCK_TIMESTAMPING_TX_HARDWARE
))
608 *tx_flags
|= SKBTX_HW_TSTAMP
;
609 if (sock_flag(sk
, SOCK_TIMESTAMPING_TX_SOFTWARE
))
610 *tx_flags
|= SKBTX_SW_TSTAMP
;
611 if (sock_flag(sk
, SOCK_WIFI_STATUS
))
612 *tx_flags
|= SKBTX_WIFI_STATUS
;
614 EXPORT_SYMBOL(sock_tx_timestamp
);
616 static inline int __sock_sendmsg_nosec(struct kiocb
*iocb
, struct socket
*sock
,
617 struct msghdr
*msg
, size_t size
)
619 struct sock_iocb
*si
= kiocb_to_siocb(iocb
);
626 return sock
->ops
->sendmsg(iocb
, sock
, msg
, size
);
629 static inline int __sock_sendmsg(struct kiocb
*iocb
, struct socket
*sock
,
630 struct msghdr
*msg
, size_t size
)
632 int err
= security_socket_sendmsg(sock
, msg
, size
);
634 return err
?: __sock_sendmsg_nosec(iocb
, sock
, msg
, size
);
637 int sock_sendmsg(struct socket
*sock
, struct msghdr
*msg
, size_t size
)
640 struct sock_iocb siocb
;
643 init_sync_kiocb(&iocb
, NULL
);
644 iocb
.private = &siocb
;
645 ret
= __sock_sendmsg(&iocb
, sock
, msg
, size
);
646 if (-EIOCBQUEUED
== ret
)
647 ret
= wait_on_sync_kiocb(&iocb
);
650 EXPORT_SYMBOL(sock_sendmsg
);
652 static int sock_sendmsg_nosec(struct socket
*sock
, struct msghdr
*msg
, size_t size
)
655 struct sock_iocb siocb
;
658 init_sync_kiocb(&iocb
, NULL
);
659 iocb
.private = &siocb
;
660 ret
= __sock_sendmsg_nosec(&iocb
, sock
, msg
, size
);
661 if (-EIOCBQUEUED
== ret
)
662 ret
= wait_on_sync_kiocb(&iocb
);
666 int kernel_sendmsg(struct socket
*sock
, struct msghdr
*msg
,
667 struct kvec
*vec
, size_t num
, size_t size
)
669 mm_segment_t oldfs
= get_fs();
674 * the following is safe, since for compiler definitions of kvec and
675 * iovec are identical, yielding the same in-core layout and alignment
677 msg
->msg_iov
= (struct iovec
*)vec
;
678 msg
->msg_iovlen
= num
;
679 result
= sock_sendmsg(sock
, msg
, size
);
683 EXPORT_SYMBOL(kernel_sendmsg
);
686 * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
688 void __sock_recv_timestamp(struct msghdr
*msg
, struct sock
*sk
,
691 int need_software_tstamp
= sock_flag(sk
, SOCK_RCVTSTAMP
);
692 struct timespec ts
[3];
694 struct skb_shared_hwtstamps
*shhwtstamps
=
697 /* Race occurred between timestamp enabling and packet
698 receiving. Fill in the current time for now. */
699 if (need_software_tstamp
&& skb
->tstamp
.tv64
== 0)
700 __net_timestamp(skb
);
702 if (need_software_tstamp
) {
703 if (!sock_flag(sk
, SOCK_RCVTSTAMPNS
)) {
705 skb_get_timestamp(skb
, &tv
);
706 put_cmsg(msg
, SOL_SOCKET
, SCM_TIMESTAMP
,
709 skb_get_timestampns(skb
, &ts
[0]);
710 put_cmsg(msg
, SOL_SOCKET
, SCM_TIMESTAMPNS
,
711 sizeof(ts
[0]), &ts
[0]);
716 memset(ts
, 0, sizeof(ts
));
717 if (sock_flag(sk
, SOCK_TIMESTAMPING_SOFTWARE
) &&
718 ktime_to_timespec_cond(skb
->tstamp
, ts
+ 0))
721 if (sock_flag(sk
, SOCK_TIMESTAMPING_SYS_HARDWARE
) &&
722 ktime_to_timespec_cond(shhwtstamps
->syststamp
, ts
+ 1))
724 if (sock_flag(sk
, SOCK_TIMESTAMPING_RAW_HARDWARE
) &&
725 ktime_to_timespec_cond(shhwtstamps
->hwtstamp
, ts
+ 2))
729 put_cmsg(msg
, SOL_SOCKET
,
730 SCM_TIMESTAMPING
, sizeof(ts
), &ts
);
732 EXPORT_SYMBOL_GPL(__sock_recv_timestamp
);
734 void __sock_recv_wifi_status(struct msghdr
*msg
, struct sock
*sk
,
739 if (!sock_flag(sk
, SOCK_WIFI_STATUS
))
741 if (!skb
->wifi_acked_valid
)
744 ack
= skb
->wifi_acked
;
746 put_cmsg(msg
, SOL_SOCKET
, SCM_WIFI_STATUS
, sizeof(ack
), &ack
);
748 EXPORT_SYMBOL_GPL(__sock_recv_wifi_status
);
750 static inline void sock_recv_drops(struct msghdr
*msg
, struct sock
*sk
,
753 if (sock_flag(sk
, SOCK_RXQ_OVFL
) && skb
&& skb
->dropcount
)
754 put_cmsg(msg
, SOL_SOCKET
, SO_RXQ_OVFL
,
755 sizeof(__u32
), &skb
->dropcount
);
758 void __sock_recv_ts_and_drops(struct msghdr
*msg
, struct sock
*sk
,
761 sock_recv_timestamp(msg
, sk
, skb
);
762 sock_recv_drops(msg
, sk
, skb
);
764 EXPORT_SYMBOL_GPL(__sock_recv_ts_and_drops
);
766 static inline int __sock_recvmsg_nosec(struct kiocb
*iocb
, struct socket
*sock
,
767 struct msghdr
*msg
, size_t size
, int flags
)
769 struct sock_iocb
*si
= kiocb_to_siocb(iocb
);
777 return sock
->ops
->recvmsg(iocb
, sock
, msg
, size
, flags
);
780 static inline int __sock_recvmsg(struct kiocb
*iocb
, struct socket
*sock
,
781 struct msghdr
*msg
, size_t size
, int flags
)
783 int err
= security_socket_recvmsg(sock
, msg
, size
, flags
);
785 return err
?: __sock_recvmsg_nosec(iocb
, sock
, msg
, size
, flags
);
788 int sock_recvmsg(struct socket
*sock
, struct msghdr
*msg
,
789 size_t size
, int flags
)
792 struct sock_iocb siocb
;
795 init_sync_kiocb(&iocb
, NULL
);
796 iocb
.private = &siocb
;
797 ret
= __sock_recvmsg(&iocb
, sock
, msg
, size
, flags
);
798 if (-EIOCBQUEUED
== ret
)
799 ret
= wait_on_sync_kiocb(&iocb
);
802 EXPORT_SYMBOL(sock_recvmsg
);
804 static int sock_recvmsg_nosec(struct socket
*sock
, struct msghdr
*msg
,
805 size_t size
, int flags
)
808 struct sock_iocb siocb
;
811 init_sync_kiocb(&iocb
, NULL
);
812 iocb
.private = &siocb
;
813 ret
= __sock_recvmsg_nosec(&iocb
, sock
, msg
, size
, flags
);
814 if (-EIOCBQUEUED
== ret
)
815 ret
= wait_on_sync_kiocb(&iocb
);
820 * kernel_recvmsg - Receive a message from a socket (kernel space)
821 * @sock: The socket to receive the message from
822 * @msg: Received message
823 * @vec: Input s/g array for message data
824 * @num: Size of input s/g array
825 * @size: Number of bytes to read
826 * @flags: Message flags (MSG_DONTWAIT, etc...)
828 * On return the msg structure contains the scatter/gather array passed in the
829 * vec argument. The array is modified so that it consists of the unfilled
830 * portion of the original array.
832 * The returned value is the total number of bytes received, or an error.
834 int kernel_recvmsg(struct socket
*sock
, struct msghdr
*msg
,
835 struct kvec
*vec
, size_t num
, size_t size
, int flags
)
837 mm_segment_t oldfs
= get_fs();
842 * the following is safe, since for compiler definitions of kvec and
843 * iovec are identical, yielding the same in-core layout and alignment
845 msg
->msg_iov
= (struct iovec
*)vec
, msg
->msg_iovlen
= num
;
846 result
= sock_recvmsg(sock
, msg
, size
, flags
);
850 EXPORT_SYMBOL(kernel_recvmsg
);
852 static void sock_aio_dtor(struct kiocb
*iocb
)
854 kfree(iocb
->private);
857 static ssize_t
sock_sendpage(struct file
*file
, struct page
*page
,
858 int offset
, size_t size
, loff_t
*ppos
, int more
)
863 sock
= file
->private_data
;
865 flags
= (file
->f_flags
& O_NONBLOCK
) ? MSG_DONTWAIT
: 0;
866 /* more is a combination of MSG_MORE and MSG_SENDPAGE_NOTLAST */
869 return kernel_sendpage(sock
, page
, offset
, size
, flags
);
872 static ssize_t
sock_splice_read(struct file
*file
, loff_t
*ppos
,
873 struct pipe_inode_info
*pipe
, size_t len
,
876 struct socket
*sock
= file
->private_data
;
878 if (unlikely(!sock
->ops
->splice_read
))
881 return sock
->ops
->splice_read(sock
, ppos
, pipe
, len
, flags
);
884 static struct sock_iocb
*alloc_sock_iocb(struct kiocb
*iocb
,
885 struct sock_iocb
*siocb
)
887 if (!is_sync_kiocb(iocb
)) {
888 siocb
= kmalloc(sizeof(*siocb
), GFP_KERNEL
);
891 iocb
->ki_dtor
= sock_aio_dtor
;
895 iocb
->private = siocb
;
899 static ssize_t
do_sock_read(struct msghdr
*msg
, struct kiocb
*iocb
,
900 struct file
*file
, const struct iovec
*iov
,
901 unsigned long nr_segs
)
903 struct socket
*sock
= file
->private_data
;
907 for (i
= 0; i
< nr_segs
; i
++)
908 size
+= iov
[i
].iov_len
;
910 msg
->msg_name
= NULL
;
911 msg
->msg_namelen
= 0;
912 msg
->msg_control
= NULL
;
913 msg
->msg_controllen
= 0;
914 msg
->msg_iov
= (struct iovec
*)iov
;
915 msg
->msg_iovlen
= nr_segs
;
916 msg
->msg_flags
= (file
->f_flags
& O_NONBLOCK
) ? MSG_DONTWAIT
: 0;
918 return __sock_recvmsg(iocb
, sock
, msg
, size
, msg
->msg_flags
);
921 static ssize_t
sock_aio_read(struct kiocb
*iocb
, const struct iovec
*iov
,
922 unsigned long nr_segs
, loff_t pos
)
924 struct sock_iocb siocb
, *x
;
929 if (iocb
->ki_left
== 0) /* Match SYS5 behaviour */
933 x
= alloc_sock_iocb(iocb
, &siocb
);
936 return do_sock_read(&x
->async_msg
, iocb
, iocb
->ki_filp
, iov
, nr_segs
);
939 static ssize_t
do_sock_write(struct msghdr
*msg
, struct kiocb
*iocb
,
940 struct file
*file
, const struct iovec
*iov
,
941 unsigned long nr_segs
)
943 struct socket
*sock
= file
->private_data
;
947 for (i
= 0; i
< nr_segs
; i
++)
948 size
+= iov
[i
].iov_len
;
950 msg
->msg_name
= NULL
;
951 msg
->msg_namelen
= 0;
952 msg
->msg_control
= NULL
;
953 msg
->msg_controllen
= 0;
954 msg
->msg_iov
= (struct iovec
*)iov
;
955 msg
->msg_iovlen
= nr_segs
;
956 msg
->msg_flags
= (file
->f_flags
& O_NONBLOCK
) ? MSG_DONTWAIT
: 0;
957 if (sock
->type
== SOCK_SEQPACKET
)
958 msg
->msg_flags
|= MSG_EOR
;
960 return __sock_sendmsg(iocb
, sock
, msg
, size
);
963 static ssize_t
sock_aio_write(struct kiocb
*iocb
, const struct iovec
*iov
,
964 unsigned long nr_segs
, loff_t pos
)
966 struct sock_iocb siocb
, *x
;
971 x
= alloc_sock_iocb(iocb
, &siocb
);
975 return do_sock_write(&x
->async_msg
, iocb
, iocb
->ki_filp
, iov
, nr_segs
);
979 * Atomic setting of ioctl hooks to avoid race
980 * with module unload.
983 static DEFINE_MUTEX(br_ioctl_mutex
);
984 static int (*br_ioctl_hook
) (struct net
*, unsigned int cmd
, void __user
*arg
);
986 void brioctl_set(int (*hook
) (struct net
*, unsigned int, void __user
*))
988 mutex_lock(&br_ioctl_mutex
);
989 br_ioctl_hook
= hook
;
990 mutex_unlock(&br_ioctl_mutex
);
992 EXPORT_SYMBOL(brioctl_set
);
994 static DEFINE_MUTEX(vlan_ioctl_mutex
);
995 static int (*vlan_ioctl_hook
) (struct net
*, void __user
*arg
);
997 void vlan_ioctl_set(int (*hook
) (struct net
*, void __user
*))
999 mutex_lock(&vlan_ioctl_mutex
);
1000 vlan_ioctl_hook
= hook
;
1001 mutex_unlock(&vlan_ioctl_mutex
);
1003 EXPORT_SYMBOL(vlan_ioctl_set
);
1005 static DEFINE_MUTEX(dlci_ioctl_mutex
);
1006 static int (*dlci_ioctl_hook
) (unsigned int, void __user
*);
1008 void dlci_ioctl_set(int (*hook
) (unsigned int, void __user
*))
1010 mutex_lock(&dlci_ioctl_mutex
);
1011 dlci_ioctl_hook
= hook
;
1012 mutex_unlock(&dlci_ioctl_mutex
);
1014 EXPORT_SYMBOL(dlci_ioctl_set
);
1016 static long sock_do_ioctl(struct net
*net
, struct socket
*sock
,
1017 unsigned int cmd
, unsigned long arg
)
1020 void __user
*argp
= (void __user
*)arg
;
1022 err
= sock
->ops
->ioctl(sock
, cmd
, arg
);
1025 * If this ioctl is unknown try to hand it down
1026 * to the NIC driver.
1028 if (err
== -ENOIOCTLCMD
)
1029 err
= dev_ioctl(net
, cmd
, argp
);
1035 * With an ioctl, arg may well be a user mode pointer, but we don't know
1036 * what to do with it - that's up to the protocol still.
1039 static long sock_ioctl(struct file
*file
, unsigned cmd
, unsigned long arg
)
1041 struct socket
*sock
;
1043 void __user
*argp
= (void __user
*)arg
;
1047 sock
= file
->private_data
;
1050 if (cmd
>= SIOCDEVPRIVATE
&& cmd
<= (SIOCDEVPRIVATE
+ 15)) {
1051 err
= dev_ioctl(net
, cmd
, argp
);
1053 #ifdef CONFIG_WEXT_CORE
1054 if (cmd
>= SIOCIWFIRST
&& cmd
<= SIOCIWLAST
) {
1055 err
= dev_ioctl(net
, cmd
, argp
);
1062 if (get_user(pid
, (int __user
*)argp
))
1064 err
= f_setown(sock
->file
, pid
, 1);
1068 err
= put_user(f_getown(sock
->file
),
1069 (int __user
*)argp
);
1077 request_module("bridge");
1079 mutex_lock(&br_ioctl_mutex
);
1081 err
= br_ioctl_hook(net
, cmd
, argp
);
1082 mutex_unlock(&br_ioctl_mutex
);
1087 if (!vlan_ioctl_hook
)
1088 request_module("8021q");
1090 mutex_lock(&vlan_ioctl_mutex
);
1091 if (vlan_ioctl_hook
)
1092 err
= vlan_ioctl_hook(net
, argp
);
1093 mutex_unlock(&vlan_ioctl_mutex
);
1098 if (!dlci_ioctl_hook
)
1099 request_module("dlci");
1101 mutex_lock(&dlci_ioctl_mutex
);
1102 if (dlci_ioctl_hook
)
1103 err
= dlci_ioctl_hook(cmd
, argp
);
1104 mutex_unlock(&dlci_ioctl_mutex
);
1107 err
= sock_do_ioctl(net
, sock
, cmd
, arg
);
1113 int sock_create_lite(int family
, int type
, int protocol
, struct socket
**res
)
1116 struct socket
*sock
= NULL
;
1118 err
= security_socket_create(family
, type
, protocol
, 1);
1122 sock
= sock_alloc();
1129 err
= security_socket_post_create(sock
, family
, type
, protocol
, 1);
1141 EXPORT_SYMBOL(sock_create_lite
);
1143 /* No kernel lock held - perfect */
1144 static unsigned int sock_poll(struct file
*file
, poll_table
*wait
)
1146 struct socket
*sock
;
1149 * We can't return errors to poll, so it's either yes or no.
1151 sock
= file
->private_data
;
1152 return sock
->ops
->poll(file
, sock
, wait
);
1155 static int sock_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1157 struct socket
*sock
= file
->private_data
;
1159 return sock
->ops
->mmap(file
, sock
, vma
);
1162 static int sock_close(struct inode
*inode
, struct file
*filp
)
1164 sock_release(SOCKET_I(inode
));
1169 * Update the socket async list
1171 * Fasync_list locking strategy.
1173 * 1. fasync_list is modified only under process context socket lock
1174 * i.e. under semaphore.
1175 * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
1176 * or under socket lock
1179 static int sock_fasync(int fd
, struct file
*filp
, int on
)
1181 struct socket
*sock
= filp
->private_data
;
1182 struct sock
*sk
= sock
->sk
;
1183 struct socket_wq
*wq
;
1189 wq
= rcu_dereference_protected(sock
->wq
, sock_owned_by_user(sk
));
1190 fasync_helper(fd
, filp
, on
, &wq
->fasync_list
);
1192 if (!wq
->fasync_list
)
1193 sock_reset_flag(sk
, SOCK_FASYNC
);
1195 sock_set_flag(sk
, SOCK_FASYNC
);
1201 /* This function may be called only under socket lock or callback_lock or rcu_lock */
1203 int sock_wake_async(struct socket
*sock
, int how
, int band
)
1205 struct socket_wq
*wq
;
1210 wq
= rcu_dereference(sock
->wq
);
1211 if (!wq
|| !wq
->fasync_list
) {
1216 case SOCK_WAKE_WAITD
:
1217 if (test_bit(SOCK_ASYNC_WAITDATA
, &sock
->flags
))
1220 case SOCK_WAKE_SPACE
:
1221 if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE
, &sock
->flags
))
1226 kill_fasync(&wq
->fasync_list
, SIGIO
, band
);
1229 kill_fasync(&wq
->fasync_list
, SIGURG
, band
);
1234 EXPORT_SYMBOL(sock_wake_async
);
1236 int __sock_create(struct net
*net
, int family
, int type
, int protocol
,
1237 struct socket
**res
, int kern
)
1240 struct socket
*sock
;
1241 const struct net_proto_family
*pf
;
1244 * Check protocol is in range
1246 if (family
< 0 || family
>= NPROTO
)
1247 return -EAFNOSUPPORT
;
1248 if (type
< 0 || type
>= SOCK_MAX
)
1253 This uglymoron is moved from INET layer to here to avoid
1254 deadlock in module load.
1256 if (family
== PF_INET
&& type
== SOCK_PACKET
) {
1260 printk(KERN_INFO
"%s uses obsolete (PF_INET,SOCK_PACKET)\n",
1266 err
= security_socket_create(family
, type
, protocol
, kern
);
1271 * Allocate the socket and allow the family to set things up. if
1272 * the protocol is 0, the family is instructed to select an appropriate
1275 sock
= sock_alloc();
1277 net_warn_ratelimited("socket: no more sockets\n");
1278 return -ENFILE
; /* Not exactly a match, but its the
1279 closest posix thing */
1284 #ifdef CONFIG_MODULES
1285 /* Attempt to load a protocol module if the find failed.
1287 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
1288 * requested real, full-featured networking support upon configuration.
1289 * Otherwise module support will break!
1291 if (rcu_access_pointer(net_families
[family
]) == NULL
)
1292 request_module("net-pf-%d", family
);
1296 pf
= rcu_dereference(net_families
[family
]);
1297 err
= -EAFNOSUPPORT
;
1302 * We will call the ->create function, that possibly is in a loadable
1303 * module, so we have to bump that loadable module refcnt first.
1305 if (!try_module_get(pf
->owner
))
1308 /* Now protected by module ref count */
1311 err
= pf
->create(net
, sock
, protocol
, kern
);
1313 goto out_module_put
;
1316 * Now to bump the refcnt of the [loadable] module that owns this
1317 * socket at sock_release time we decrement its refcnt.
1319 if (!try_module_get(sock
->ops
->owner
))
1320 goto out_module_busy
;
1323 * Now that we're done with the ->create function, the [loadable]
1324 * module can have its refcnt decremented
1326 module_put(pf
->owner
);
1327 err
= security_socket_post_create(sock
, family
, type
, protocol
, kern
);
1329 goto out_sock_release
;
1335 err
= -EAFNOSUPPORT
;
1338 module_put(pf
->owner
);
1345 goto out_sock_release
;
1347 EXPORT_SYMBOL(__sock_create
);
1349 int sock_create(int family
, int type
, int protocol
, struct socket
**res
)
1351 return __sock_create(current
->nsproxy
->net_ns
, family
, type
, protocol
, res
, 0);
1353 EXPORT_SYMBOL(sock_create
);
1355 int sock_create_kern(int family
, int type
, int protocol
, struct socket
**res
)
1357 return __sock_create(&init_net
, family
, type
, protocol
, res
, 1);
1359 EXPORT_SYMBOL(sock_create_kern
);
1361 SYSCALL_DEFINE3(socket
, int, family
, int, type
, int, protocol
)
1364 struct socket
*sock
;
1367 /* Check the SOCK_* constants for consistency. */
1368 BUILD_BUG_ON(SOCK_CLOEXEC
!= O_CLOEXEC
);
1369 BUILD_BUG_ON((SOCK_MAX
| SOCK_TYPE_MASK
) != SOCK_TYPE_MASK
);
1370 BUILD_BUG_ON(SOCK_CLOEXEC
& SOCK_TYPE_MASK
);
1371 BUILD_BUG_ON(SOCK_NONBLOCK
& SOCK_TYPE_MASK
);
1373 flags
= type
& ~SOCK_TYPE_MASK
;
1374 if (flags
& ~(SOCK_CLOEXEC
| SOCK_NONBLOCK
))
1376 type
&= SOCK_TYPE_MASK
;
1378 if (SOCK_NONBLOCK
!= O_NONBLOCK
&& (flags
& SOCK_NONBLOCK
))
1379 flags
= (flags
& ~SOCK_NONBLOCK
) | O_NONBLOCK
;
1381 retval
= sock_create(family
, type
, protocol
, &sock
);
1385 retval
= sock_map_fd(sock
, flags
& (O_CLOEXEC
| O_NONBLOCK
));
1390 /* It may be already another descriptor 8) Not kernel problem. */
1399 * Create a pair of connected sockets.
1402 SYSCALL_DEFINE4(socketpair
, int, family
, int, type
, int, protocol
,
1403 int __user
*, usockvec
)
1405 struct socket
*sock1
, *sock2
;
1407 struct file
*newfile1
, *newfile2
;
1410 flags
= type
& ~SOCK_TYPE_MASK
;
1411 if (flags
& ~(SOCK_CLOEXEC
| SOCK_NONBLOCK
))
1413 type
&= SOCK_TYPE_MASK
;
1415 if (SOCK_NONBLOCK
!= O_NONBLOCK
&& (flags
& SOCK_NONBLOCK
))
1416 flags
= (flags
& ~SOCK_NONBLOCK
) | O_NONBLOCK
;
1419 * Obtain the first socket and check if the underlying protocol
1420 * supports the socketpair call.
1423 err
= sock_create(family
, type
, protocol
, &sock1
);
1427 err
= sock_create(family
, type
, protocol
, &sock2
);
1431 err
= sock1
->ops
->socketpair(sock1
, sock2
);
1433 goto out_release_both
;
1435 fd1
= get_unused_fd_flags(flags
);
1436 if (unlikely(fd1
< 0)) {
1438 goto out_release_both
;
1440 fd2
= get_unused_fd_flags(flags
);
1441 if (unlikely(fd2
< 0)) {
1444 goto out_release_both
;
1447 newfile1
= sock_alloc_file(sock1
, flags
, NULL
);
1448 if (unlikely(IS_ERR(newfile1
))) {
1449 err
= PTR_ERR(newfile1
);
1452 goto out_release_both
;
1455 newfile2
= sock_alloc_file(sock2
, flags
, NULL
);
1456 if (IS_ERR(newfile2
)) {
1457 err
= PTR_ERR(newfile2
);
1461 sock_release(sock2
);
1465 audit_fd_pair(fd1
, fd2
);
1466 fd_install(fd1
, newfile1
);
1467 fd_install(fd2
, newfile2
);
1468 /* fd1 and fd2 may be already another descriptors.
1469 * Not kernel problem.
1472 err
= put_user(fd1
, &usockvec
[0]);
1474 err
= put_user(fd2
, &usockvec
[1]);
1483 sock_release(sock2
);
1485 sock_release(sock1
);
1491 * Bind a name to a socket. Nothing much to do here since it's
1492 * the protocol's responsibility to handle the local address.
1494 * We move the socket address to kernel space before we call
1495 * the protocol layer (having also checked the address is ok).
1498 SYSCALL_DEFINE3(bind
, int, fd
, struct sockaddr __user
*, umyaddr
, int, addrlen
)
1500 struct socket
*sock
;
1501 struct sockaddr_storage address
;
1502 int err
, fput_needed
;
1504 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1506 err
= move_addr_to_kernel(umyaddr
, addrlen
, &address
);
1508 err
= security_socket_bind(sock
,
1509 (struct sockaddr
*)&address
,
1512 err
= sock
->ops
->bind(sock
,
1516 fput_light(sock
->file
, fput_needed
);
1522 * Perform a listen. Basically, we allow the protocol to do anything
1523 * necessary for a listen, and if that works, we mark the socket as
1524 * ready for listening.
1527 SYSCALL_DEFINE2(listen
, int, fd
, int, backlog
)
1529 struct socket
*sock
;
1530 int err
, fput_needed
;
1533 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1535 somaxconn
= sock_net(sock
->sk
)->core
.sysctl_somaxconn
;
1536 if ((unsigned int)backlog
> somaxconn
)
1537 backlog
= somaxconn
;
1539 err
= security_socket_listen(sock
, backlog
);
1541 err
= sock
->ops
->listen(sock
, backlog
);
1543 fput_light(sock
->file
, fput_needed
);
1549 * For accept, we attempt to create a new socket, set up the link
1550 * with the client, wake up the client, then return the new
1551 * connected fd. We collect the address of the connector in kernel
1552 * space and move it to user at the very end. This is unclean because
1553 * we open the socket then return an error.
1555 * 1003.1g adds the ability to recvmsg() to query connection pending
1556 * status to recvmsg. We need to add that support in a way thats
1557 * clean when we restucture accept also.
1560 SYSCALL_DEFINE4(accept4
, int, fd
, struct sockaddr __user
*, upeer_sockaddr
,
1561 int __user
*, upeer_addrlen
, int, flags
)
1563 struct socket
*sock
, *newsock
;
1564 struct file
*newfile
;
1565 int err
, len
, newfd
, fput_needed
;
1566 struct sockaddr_storage address
;
1568 if (flags
& ~(SOCK_CLOEXEC
| SOCK_NONBLOCK
))
1571 if (SOCK_NONBLOCK
!= O_NONBLOCK
&& (flags
& SOCK_NONBLOCK
))
1572 flags
= (flags
& ~SOCK_NONBLOCK
) | O_NONBLOCK
;
1574 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1579 newsock
= sock_alloc();
1583 newsock
->type
= sock
->type
;
1584 newsock
->ops
= sock
->ops
;
1587 * We don't need try_module_get here, as the listening socket (sock)
1588 * has the protocol module (sock->ops->owner) held.
1590 __module_get(newsock
->ops
->owner
);
1592 newfd
= get_unused_fd_flags(flags
);
1593 if (unlikely(newfd
< 0)) {
1595 sock_release(newsock
);
1598 newfile
= sock_alloc_file(newsock
, flags
, sock
->sk
->sk_prot_creator
->name
);
1599 if (unlikely(IS_ERR(newfile
))) {
1600 err
= PTR_ERR(newfile
);
1601 put_unused_fd(newfd
);
1602 sock_release(newsock
);
1606 err
= security_socket_accept(sock
, newsock
);
1610 err
= sock
->ops
->accept(sock
, newsock
, sock
->file
->f_flags
);
1614 if (upeer_sockaddr
) {
1615 if (newsock
->ops
->getname(newsock
, (struct sockaddr
*)&address
,
1617 err
= -ECONNABORTED
;
1620 err
= move_addr_to_user(&address
,
1621 len
, upeer_sockaddr
, upeer_addrlen
);
1626 /* File flags are not inherited via accept() unlike another OSes. */
1628 fd_install(newfd
, newfile
);
1632 fput_light(sock
->file
, fput_needed
);
1637 put_unused_fd(newfd
);
1641 SYSCALL_DEFINE3(accept
, int, fd
, struct sockaddr __user
*, upeer_sockaddr
,
1642 int __user
*, upeer_addrlen
)
1644 return sys_accept4(fd
, upeer_sockaddr
, upeer_addrlen
, 0);
1648 * Attempt to connect to a socket with the server address. The address
1649 * is in user space so we verify it is OK and move it to kernel space.
1651 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
1654 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
1655 * other SEQPACKET protocols that take time to connect() as it doesn't
1656 * include the -EINPROGRESS status for such sockets.
1659 SYSCALL_DEFINE3(connect
, int, fd
, struct sockaddr __user
*, uservaddr
,
1662 struct socket
*sock
;
1663 struct sockaddr_storage address
;
1664 int err
, fput_needed
;
1666 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1669 err
= move_addr_to_kernel(uservaddr
, addrlen
, &address
);
1674 security_socket_connect(sock
, (struct sockaddr
*)&address
, addrlen
);
1678 err
= sock
->ops
->connect(sock
, (struct sockaddr
*)&address
, addrlen
,
1679 sock
->file
->f_flags
);
1681 fput_light(sock
->file
, fput_needed
);
1687 * Get the local address ('name') of a socket object. Move the obtained
1688 * name to user space.
1691 SYSCALL_DEFINE3(getsockname
, int, fd
, struct sockaddr __user
*, usockaddr
,
1692 int __user
*, usockaddr_len
)
1694 struct socket
*sock
;
1695 struct sockaddr_storage address
;
1696 int len
, err
, fput_needed
;
1698 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1702 err
= security_socket_getsockname(sock
);
1706 err
= sock
->ops
->getname(sock
, (struct sockaddr
*)&address
, &len
, 0);
1709 err
= move_addr_to_user(&address
, len
, usockaddr
, usockaddr_len
);
1712 fput_light(sock
->file
, fput_needed
);
1718 * Get the remote address ('name') of a socket object. Move the obtained
1719 * name to user space.
1722 SYSCALL_DEFINE3(getpeername
, int, fd
, struct sockaddr __user
*, usockaddr
,
1723 int __user
*, usockaddr_len
)
1725 struct socket
*sock
;
1726 struct sockaddr_storage address
;
1727 int len
, err
, fput_needed
;
1729 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1731 err
= security_socket_getpeername(sock
);
1733 fput_light(sock
->file
, fput_needed
);
1738 sock
->ops
->getname(sock
, (struct sockaddr
*)&address
, &len
,
1741 err
= move_addr_to_user(&address
, len
, usockaddr
,
1743 fput_light(sock
->file
, fput_needed
);
1749 * Send a datagram to a given address. We move the address into kernel
1750 * space and check the user space data area is readable before invoking
1754 SYSCALL_DEFINE6(sendto
, int, fd
, void __user
*, buff
, size_t, len
,
1755 unsigned int, flags
, struct sockaddr __user
*, addr
,
1758 struct socket
*sock
;
1759 struct sockaddr_storage address
;
1767 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1771 iov
.iov_base
= buff
;
1773 msg
.msg_name
= NULL
;
1776 msg
.msg_control
= NULL
;
1777 msg
.msg_controllen
= 0;
1778 msg
.msg_namelen
= 0;
1780 err
= move_addr_to_kernel(addr
, addr_len
, &address
);
1783 msg
.msg_name
= (struct sockaddr
*)&address
;
1784 msg
.msg_namelen
= addr_len
;
1786 if (sock
->file
->f_flags
& O_NONBLOCK
)
1787 flags
|= MSG_DONTWAIT
;
1788 msg
.msg_flags
= flags
;
1789 err
= sock_sendmsg(sock
, &msg
, len
);
1792 fput_light(sock
->file
, fput_needed
);
1798 * Send a datagram down a socket.
1801 SYSCALL_DEFINE4(send
, int, fd
, void __user
*, buff
, size_t, len
,
1802 unsigned int, flags
)
1804 return sys_sendto(fd
, buff
, len
, flags
, NULL
, 0);
1808 * Receive a frame from the socket and optionally record the address of the
1809 * sender. We verify the buffers are writable and if needed move the
1810 * sender address from kernel to user space.
1813 SYSCALL_DEFINE6(recvfrom
, int, fd
, void __user
*, ubuf
, size_t, size
,
1814 unsigned int, flags
, struct sockaddr __user
*, addr
,
1815 int __user
*, addr_len
)
1817 struct socket
*sock
;
1820 struct sockaddr_storage address
;
1826 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1830 msg
.msg_control
= NULL
;
1831 msg
.msg_controllen
= 0;
1835 iov
.iov_base
= ubuf
;
1836 /* Save some cycles and don't copy the address if not needed */
1837 msg
.msg_name
= addr
? (struct sockaddr
*)&address
: NULL
;
1838 /* We assume all kernel code knows the size of sockaddr_storage */
1839 msg
.msg_namelen
= 0;
1840 if (sock
->file
->f_flags
& O_NONBLOCK
)
1841 flags
|= MSG_DONTWAIT
;
1842 err
= sock_recvmsg(sock
, &msg
, size
, flags
);
1844 if (err
>= 0 && addr
!= NULL
) {
1845 err2
= move_addr_to_user(&address
,
1846 msg
.msg_namelen
, addr
, addr_len
);
1851 fput_light(sock
->file
, fput_needed
);
1857 * Receive a datagram from a socket.
1860 asmlinkage
long sys_recv(int fd
, void __user
*ubuf
, size_t size
,
1863 return sys_recvfrom(fd
, ubuf
, size
, flags
, NULL
, NULL
);
1867 * Set a socket option. Because we don't know the option lengths we have
1868 * to pass the user mode parameter for the protocols to sort out.
1871 SYSCALL_DEFINE5(setsockopt
, int, fd
, int, level
, int, optname
,
1872 char __user
*, optval
, int, optlen
)
1874 int err
, fput_needed
;
1875 struct socket
*sock
;
1880 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1882 err
= security_socket_setsockopt(sock
, level
, optname
);
1886 if (level
== SOL_SOCKET
)
1888 sock_setsockopt(sock
, level
, optname
, optval
,
1892 sock
->ops
->setsockopt(sock
, level
, optname
, optval
,
1895 fput_light(sock
->file
, fput_needed
);
1901 * Get a socket option. Because we don't know the option lengths we have
1902 * to pass a user mode parameter for the protocols to sort out.
1905 SYSCALL_DEFINE5(getsockopt
, int, fd
, int, level
, int, optname
,
1906 char __user
*, optval
, int __user
*, optlen
)
1908 int err
, fput_needed
;
1909 struct socket
*sock
;
1911 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1913 err
= security_socket_getsockopt(sock
, level
, optname
);
1917 if (level
== SOL_SOCKET
)
1919 sock_getsockopt(sock
, level
, optname
, optval
,
1923 sock
->ops
->getsockopt(sock
, level
, optname
, optval
,
1926 fput_light(sock
->file
, fput_needed
);
1932 * Shutdown a socket.
1935 SYSCALL_DEFINE2(shutdown
, int, fd
, int, how
)
1937 int err
, fput_needed
;
1938 struct socket
*sock
;
1940 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1942 err
= security_socket_shutdown(sock
, how
);
1944 err
= sock
->ops
->shutdown(sock
, how
);
1945 fput_light(sock
->file
, fput_needed
);
1950 /* A couple of helpful macros for getting the address of the 32/64 bit
1951 * fields which are the same type (int / unsigned) on our platforms.
1953 #define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
1954 #define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
1955 #define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
1957 struct used_address
{
1958 struct sockaddr_storage name
;
1959 unsigned int name_len
;
1962 static int copy_msghdr_from_user(struct msghdr
*kmsg
,
1963 struct msghdr __user
*umsg
)
1965 if (copy_from_user(kmsg
, umsg
, sizeof(struct msghdr
)))
1968 if (kmsg
->msg_namelen
< 0)
1971 if (kmsg
->msg_namelen
> sizeof(struct sockaddr_storage
))
1972 kmsg
->msg_namelen
= sizeof(struct sockaddr_storage
);
1976 static int ___sys_sendmsg(struct socket
*sock
, struct msghdr __user
*msg
,
1977 struct msghdr
*msg_sys
, unsigned int flags
,
1978 struct used_address
*used_address
)
1980 struct compat_msghdr __user
*msg_compat
=
1981 (struct compat_msghdr __user
*)msg
;
1982 struct sockaddr_storage address
;
1983 struct iovec iovstack
[UIO_FASTIOV
], *iov
= iovstack
;
1984 unsigned char ctl
[sizeof(struct cmsghdr
) + 20]
1985 __attribute__ ((aligned(sizeof(__kernel_size_t
))));
1986 /* 20 is size of ipv6_pktinfo */
1987 unsigned char *ctl_buf
= ctl
;
1988 int err
, ctl_len
, total_len
;
1991 if (MSG_CMSG_COMPAT
& flags
)
1992 err
= get_compat_msghdr(msg_sys
, msg_compat
);
1994 err
= copy_msghdr_from_user(msg_sys
, msg
);
1998 if (msg_sys
->msg_iovlen
> UIO_FASTIOV
) {
2000 if (msg_sys
->msg_iovlen
> UIO_MAXIOV
)
2003 iov
= kmalloc(msg_sys
->msg_iovlen
* sizeof(struct iovec
),
2009 /* This will also move the address data into kernel space */
2010 if (MSG_CMSG_COMPAT
& flags
) {
2011 err
= verify_compat_iovec(msg_sys
, iov
, &address
, VERIFY_READ
);
2013 err
= verify_iovec(msg_sys
, iov
, &address
, VERIFY_READ
);
2020 if (msg_sys
->msg_controllen
> INT_MAX
)
2022 ctl_len
= msg_sys
->msg_controllen
;
2023 if ((MSG_CMSG_COMPAT
& flags
) && ctl_len
) {
2025 cmsghdr_from_user_compat_to_kern(msg_sys
, sock
->sk
, ctl
,
2029 ctl_buf
= msg_sys
->msg_control
;
2030 ctl_len
= msg_sys
->msg_controllen
;
2031 } else if (ctl_len
) {
2032 if (ctl_len
> sizeof(ctl
)) {
2033 ctl_buf
= sock_kmalloc(sock
->sk
, ctl_len
, GFP_KERNEL
);
2034 if (ctl_buf
== NULL
)
2039 * Careful! Before this, msg_sys->msg_control contains a user pointer.
2040 * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
2041 * checking falls down on this.
2043 if (copy_from_user(ctl_buf
,
2044 (void __user __force
*)msg_sys
->msg_control
,
2047 msg_sys
->msg_control
= ctl_buf
;
2049 msg_sys
->msg_flags
= flags
;
2051 if (sock
->file
->f_flags
& O_NONBLOCK
)
2052 msg_sys
->msg_flags
|= MSG_DONTWAIT
;
2054 * If this is sendmmsg() and current destination address is same as
2055 * previously succeeded address, omit asking LSM's decision.
2056 * used_address->name_len is initialized to UINT_MAX so that the first
2057 * destination address never matches.
2059 if (used_address
&& msg_sys
->msg_name
&&
2060 used_address
->name_len
== msg_sys
->msg_namelen
&&
2061 !memcmp(&used_address
->name
, msg_sys
->msg_name
,
2062 used_address
->name_len
)) {
2063 err
= sock_sendmsg_nosec(sock
, msg_sys
, total_len
);
2066 err
= sock_sendmsg(sock
, msg_sys
, total_len
);
2068 * If this is sendmmsg() and sending to current destination address was
2069 * successful, remember it.
2071 if (used_address
&& err
>= 0) {
2072 used_address
->name_len
= msg_sys
->msg_namelen
;
2073 if (msg_sys
->msg_name
)
2074 memcpy(&used_address
->name
, msg_sys
->msg_name
,
2075 used_address
->name_len
);
2080 sock_kfree_s(sock
->sk
, ctl_buf
, ctl_len
);
2082 if (iov
!= iovstack
)
2089 * BSD sendmsg interface
2092 long __sys_sendmsg(int fd
, struct msghdr __user
*msg
, unsigned flags
)
2094 int fput_needed
, err
;
2095 struct msghdr msg_sys
;
2096 struct socket
*sock
;
2098 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
2102 err
= ___sys_sendmsg(sock
, msg
, &msg_sys
, flags
, NULL
);
2104 fput_light(sock
->file
, fput_needed
);
2109 SYSCALL_DEFINE3(sendmsg
, int, fd
, struct msghdr __user
*, msg
, unsigned int, flags
)
2111 if (flags
& MSG_CMSG_COMPAT
)
2113 return __sys_sendmsg(fd
, msg
, flags
);
2117 * Linux sendmmsg interface
2120 int __sys_sendmmsg(int fd
, struct mmsghdr __user
*mmsg
, unsigned int vlen
,
2123 int fput_needed
, err
, datagrams
;
2124 struct socket
*sock
;
2125 struct mmsghdr __user
*entry
;
2126 struct compat_mmsghdr __user
*compat_entry
;
2127 struct msghdr msg_sys
;
2128 struct used_address used_address
;
2130 if (vlen
> UIO_MAXIOV
)
2135 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
2139 used_address
.name_len
= UINT_MAX
;
2141 compat_entry
= (struct compat_mmsghdr __user
*)mmsg
;
2144 while (datagrams
< vlen
) {
2145 if (MSG_CMSG_COMPAT
& flags
) {
2146 err
= ___sys_sendmsg(sock
, (struct msghdr __user
*)compat_entry
,
2147 &msg_sys
, flags
, &used_address
);
2150 err
= __put_user(err
, &compat_entry
->msg_len
);
2153 err
= ___sys_sendmsg(sock
,
2154 (struct msghdr __user
*)entry
,
2155 &msg_sys
, flags
, &used_address
);
2158 err
= put_user(err
, &entry
->msg_len
);
2167 fput_light(sock
->file
, fput_needed
);
2169 /* We only return an error if no datagrams were able to be sent */
2176 SYSCALL_DEFINE4(sendmmsg
, int, fd
, struct mmsghdr __user
*, mmsg
,
2177 unsigned int, vlen
, unsigned int, flags
)
2179 if (flags
& MSG_CMSG_COMPAT
)
2181 return __sys_sendmmsg(fd
, mmsg
, vlen
, flags
);
2184 static int ___sys_recvmsg(struct socket
*sock
, struct msghdr __user
*msg
,
2185 struct msghdr
*msg_sys
, unsigned int flags
, int nosec
)
2187 struct compat_msghdr __user
*msg_compat
=
2188 (struct compat_msghdr __user
*)msg
;
2189 struct iovec iovstack
[UIO_FASTIOV
];
2190 struct iovec
*iov
= iovstack
;
2191 unsigned long cmsg_ptr
;
2192 int err
, total_len
, len
;
2194 /* kernel mode address */
2195 struct sockaddr_storage addr
;
2197 /* user mode address pointers */
2198 struct sockaddr __user
*uaddr
;
2199 int __user
*uaddr_len
;
2201 if (MSG_CMSG_COMPAT
& flags
)
2202 err
= get_compat_msghdr(msg_sys
, msg_compat
);
2204 err
= copy_msghdr_from_user(msg_sys
, msg
);
2208 if (msg_sys
->msg_iovlen
> UIO_FASTIOV
) {
2210 if (msg_sys
->msg_iovlen
> UIO_MAXIOV
)
2213 iov
= kmalloc(msg_sys
->msg_iovlen
* sizeof(struct iovec
),
2219 /* Save the user-mode address (verify_iovec will change the
2220 * kernel msghdr to use the kernel address space)
2222 uaddr
= (__force
void __user
*)msg_sys
->msg_name
;
2223 uaddr_len
= COMPAT_NAMELEN(msg
);
2224 if (MSG_CMSG_COMPAT
& flags
)
2225 err
= verify_compat_iovec(msg_sys
, iov
, &addr
, VERIFY_WRITE
);
2227 err
= verify_iovec(msg_sys
, iov
, &addr
, VERIFY_WRITE
);
2232 cmsg_ptr
= (unsigned long)msg_sys
->msg_control
;
2233 msg_sys
->msg_flags
= flags
& (MSG_CMSG_CLOEXEC
|MSG_CMSG_COMPAT
);
2235 /* We assume all kernel code knows the size of sockaddr_storage */
2236 msg_sys
->msg_namelen
= 0;
2238 if (sock
->file
->f_flags
& O_NONBLOCK
)
2239 flags
|= MSG_DONTWAIT
;
2240 err
= (nosec
? sock_recvmsg_nosec
: sock_recvmsg
)(sock
, msg_sys
,
2246 if (uaddr
!= NULL
) {
2247 err
= move_addr_to_user(&addr
,
2248 msg_sys
->msg_namelen
, uaddr
,
2253 err
= __put_user((msg_sys
->msg_flags
& ~MSG_CMSG_COMPAT
),
2257 if (MSG_CMSG_COMPAT
& flags
)
2258 err
= __put_user((unsigned long)msg_sys
->msg_control
- cmsg_ptr
,
2259 &msg_compat
->msg_controllen
);
2261 err
= __put_user((unsigned long)msg_sys
->msg_control
- cmsg_ptr
,
2262 &msg
->msg_controllen
);
2268 if (iov
!= iovstack
)
2275 * BSD recvmsg interface
2278 long __sys_recvmsg(int fd
, struct msghdr __user
*msg
, unsigned flags
)
2280 int fput_needed
, err
;
2281 struct msghdr msg_sys
;
2282 struct socket
*sock
;
2284 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
2288 err
= ___sys_recvmsg(sock
, msg
, &msg_sys
, flags
, 0);
2290 fput_light(sock
->file
, fput_needed
);
2295 SYSCALL_DEFINE3(recvmsg
, int, fd
, struct msghdr __user
*, msg
,
2296 unsigned int, flags
)
2298 if (flags
& MSG_CMSG_COMPAT
)
2300 return __sys_recvmsg(fd
, msg
, flags
);
2304 * Linux recvmmsg interface
2307 int __sys_recvmmsg(int fd
, struct mmsghdr __user
*mmsg
, unsigned int vlen
,
2308 unsigned int flags
, struct timespec
*timeout
)
2310 int fput_needed
, err
, datagrams
;
2311 struct socket
*sock
;
2312 struct mmsghdr __user
*entry
;
2313 struct compat_mmsghdr __user
*compat_entry
;
2314 struct msghdr msg_sys
;
2315 struct timespec end_time
;
2318 poll_select_set_timeout(&end_time
, timeout
->tv_sec
,
2324 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
2328 err
= sock_error(sock
->sk
);
2335 compat_entry
= (struct compat_mmsghdr __user
*)mmsg
;
2337 while (datagrams
< vlen
) {
2339 * No need to ask LSM for more than the first datagram.
2341 if (MSG_CMSG_COMPAT
& flags
) {
2342 err
= ___sys_recvmsg(sock
, (struct msghdr __user
*)compat_entry
,
2343 &msg_sys
, flags
& ~MSG_WAITFORONE
,
2347 err
= __put_user(err
, &compat_entry
->msg_len
);
2350 err
= ___sys_recvmsg(sock
,
2351 (struct msghdr __user
*)entry
,
2352 &msg_sys
, flags
& ~MSG_WAITFORONE
,
2356 err
= put_user(err
, &entry
->msg_len
);
2364 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
2365 if (flags
& MSG_WAITFORONE
)
2366 flags
|= MSG_DONTWAIT
;
2369 ktime_get_ts(timeout
);
2370 *timeout
= timespec_sub(end_time
, *timeout
);
2371 if (timeout
->tv_sec
< 0) {
2372 timeout
->tv_sec
= timeout
->tv_nsec
= 0;
2376 /* Timeout, return less than vlen datagrams */
2377 if (timeout
->tv_nsec
== 0 && timeout
->tv_sec
== 0)
2381 /* Out of band data, return right away */
2382 if (msg_sys
.msg_flags
& MSG_OOB
)
2389 if (datagrams
== 0) {
2395 * We may return less entries than requested (vlen) if the
2396 * sock is non block and there aren't enough datagrams...
2398 if (err
!= -EAGAIN
) {
2400 * ... or if recvmsg returns an error after we
2401 * received some datagrams, where we record the
2402 * error to return on the next call or if the
2403 * app asks about it using getsockopt(SO_ERROR).
2405 sock
->sk
->sk_err
= -err
;
2408 fput_light(sock
->file
, fput_needed
);
2413 SYSCALL_DEFINE5(recvmmsg
, int, fd
, struct mmsghdr __user
*, mmsg
,
2414 unsigned int, vlen
, unsigned int, flags
,
2415 struct timespec __user
*, timeout
)
2418 struct timespec timeout_sys
;
2420 if (flags
& MSG_CMSG_COMPAT
)
2424 return __sys_recvmmsg(fd
, mmsg
, vlen
, flags
, NULL
);
2426 if (copy_from_user(&timeout_sys
, timeout
, sizeof(timeout_sys
)))
2429 datagrams
= __sys_recvmmsg(fd
, mmsg
, vlen
, flags
, &timeout_sys
);
2431 if (datagrams
> 0 &&
2432 copy_to_user(timeout
, &timeout_sys
, sizeof(timeout_sys
)))
2433 datagrams
= -EFAULT
;
2438 #ifdef __ARCH_WANT_SYS_SOCKETCALL
2439 /* Argument list sizes for sys_socketcall */
2440 #define AL(x) ((x) * sizeof(unsigned long))
2441 static const unsigned char nargs
[21] = {
2442 AL(0), AL(3), AL(3), AL(3), AL(2), AL(3),
2443 AL(3), AL(3), AL(4), AL(4), AL(4), AL(6),
2444 AL(6), AL(2), AL(5), AL(5), AL(3), AL(3),
2451 * System call vectors.
2453 * Argument checking cleaned up. Saved 20% in size.
2454 * This function doesn't need to set the kernel lock because
2455 * it is set by the callees.
2458 SYSCALL_DEFINE2(socketcall
, int, call
, unsigned long __user
*, args
)
2460 unsigned long a
[AUDITSC_ARGS
];
2461 unsigned long a0
, a1
;
2465 if (call
< 1 || call
> SYS_SENDMMSG
)
2469 if (len
> sizeof(a
))
2472 /* copy_from_user should be SMP safe. */
2473 if (copy_from_user(a
, args
, len
))
2476 err
= audit_socketcall(nargs
[call
] / sizeof(unsigned long), a
);
2485 err
= sys_socket(a0
, a1
, a
[2]);
2488 err
= sys_bind(a0
, (struct sockaddr __user
*)a1
, a
[2]);
2491 err
= sys_connect(a0
, (struct sockaddr __user
*)a1
, a
[2]);
2494 err
= sys_listen(a0
, a1
);
2497 err
= sys_accept4(a0
, (struct sockaddr __user
*)a1
,
2498 (int __user
*)a
[2], 0);
2500 case SYS_GETSOCKNAME
:
2502 sys_getsockname(a0
, (struct sockaddr __user
*)a1
,
2503 (int __user
*)a
[2]);
2505 case SYS_GETPEERNAME
:
2507 sys_getpeername(a0
, (struct sockaddr __user
*)a1
,
2508 (int __user
*)a
[2]);
2510 case SYS_SOCKETPAIR
:
2511 err
= sys_socketpair(a0
, a1
, a
[2], (int __user
*)a
[3]);
2514 err
= sys_send(a0
, (void __user
*)a1
, a
[2], a
[3]);
2517 err
= sys_sendto(a0
, (void __user
*)a1
, a
[2], a
[3],
2518 (struct sockaddr __user
*)a
[4], a
[5]);
2521 err
= sys_recv(a0
, (void __user
*)a1
, a
[2], a
[3]);
2524 err
= sys_recvfrom(a0
, (void __user
*)a1
, a
[2], a
[3],
2525 (struct sockaddr __user
*)a
[4],
2526 (int __user
*)a
[5]);
2529 err
= sys_shutdown(a0
, a1
);
2531 case SYS_SETSOCKOPT
:
2532 err
= sys_setsockopt(a0
, a1
, a
[2], (char __user
*)a
[3], a
[4]);
2534 case SYS_GETSOCKOPT
:
2536 sys_getsockopt(a0
, a1
, a
[2], (char __user
*)a
[3],
2537 (int __user
*)a
[4]);
2540 err
= sys_sendmsg(a0
, (struct msghdr __user
*)a1
, a
[2]);
2543 err
= sys_sendmmsg(a0
, (struct mmsghdr __user
*)a1
, a
[2], a
[3]);
2546 err
= sys_recvmsg(a0
, (struct msghdr __user
*)a1
, a
[2]);
2549 err
= sys_recvmmsg(a0
, (struct mmsghdr __user
*)a1
, a
[2], a
[3],
2550 (struct timespec __user
*)a
[4]);
2553 err
= sys_accept4(a0
, (struct sockaddr __user
*)a1
,
2554 (int __user
*)a
[2], a
[3]);
2563 #endif /* __ARCH_WANT_SYS_SOCKETCALL */
2566 * sock_register - add a socket protocol handler
2567 * @ops: description of protocol
2569 * This function is called by a protocol handler that wants to
2570 * advertise its address family, and have it linked into the
2571 * socket interface. The value ops->family coresponds to the
2572 * socket system call protocol family.
2574 int sock_register(const struct net_proto_family
*ops
)
2578 if (ops
->family
>= NPROTO
) {
2579 printk(KERN_CRIT
"protocol %d >= NPROTO(%d)\n", ops
->family
,
2584 spin_lock(&net_family_lock
);
2585 if (rcu_dereference_protected(net_families
[ops
->family
],
2586 lockdep_is_held(&net_family_lock
)))
2589 rcu_assign_pointer(net_families
[ops
->family
], ops
);
2592 spin_unlock(&net_family_lock
);
2594 printk(KERN_INFO
"NET: Registered protocol family %d\n", ops
->family
);
2597 EXPORT_SYMBOL(sock_register
);
2600 * sock_unregister - remove a protocol handler
2601 * @family: protocol family to remove
2603 * This function is called by a protocol handler that wants to
2604 * remove its address family, and have it unlinked from the
2605 * new socket creation.
2607 * If protocol handler is a module, then it can use module reference
2608 * counts to protect against new references. If protocol handler is not
2609 * a module then it needs to provide its own protection in
2610 * the ops->create routine.
2612 void sock_unregister(int family
)
2614 BUG_ON(family
< 0 || family
>= NPROTO
);
2616 spin_lock(&net_family_lock
);
2617 RCU_INIT_POINTER(net_families
[family
], NULL
);
2618 spin_unlock(&net_family_lock
);
2622 printk(KERN_INFO
"NET: Unregistered protocol family %d\n", family
);
2624 EXPORT_SYMBOL(sock_unregister
);
2626 static int __init
sock_init(void)
2630 * Initialize the network sysctl infrastructure.
2632 err
= net_sysctl_init();
2637 * Initialize skbuff SLAB cache
2642 * Initialize the protocols module.
2647 err
= register_filesystem(&sock_fs_type
);
2650 sock_mnt
= kern_mount(&sock_fs_type
);
2651 if (IS_ERR(sock_mnt
)) {
2652 err
= PTR_ERR(sock_mnt
);
2656 /* The real protocol initialization is performed in later initcalls.
2659 #ifdef CONFIG_NETFILTER
2663 #ifdef CONFIG_NETWORK_PHY_TIMESTAMPING
2664 skb_timestamping_init();
2671 unregister_filesystem(&sock_fs_type
);
2676 core_initcall(sock_init
); /* early initcall */
2678 #ifdef CONFIG_PROC_FS
2679 void socket_seq_show(struct seq_file
*seq
)
2684 for_each_possible_cpu(cpu
)
2685 counter
+= per_cpu(sockets_in_use
, cpu
);
2687 /* It can be negative, by the way. 8) */
2691 seq_printf(seq
, "sockets: used %d\n", counter
);
2693 #endif /* CONFIG_PROC_FS */
2695 #ifdef CONFIG_COMPAT
2696 static int do_siocgstamp(struct net
*net
, struct socket
*sock
,
2697 unsigned int cmd
, void __user
*up
)
2699 mm_segment_t old_fs
= get_fs();
2704 err
= sock_do_ioctl(net
, sock
, cmd
, (unsigned long)&ktv
);
2707 err
= compat_put_timeval(&ktv
, up
);
2712 static int do_siocgstampns(struct net
*net
, struct socket
*sock
,
2713 unsigned int cmd
, void __user
*up
)
2715 mm_segment_t old_fs
= get_fs();
2716 struct timespec kts
;
2720 err
= sock_do_ioctl(net
, sock
, cmd
, (unsigned long)&kts
);
2723 err
= compat_put_timespec(&kts
, up
);
2728 static int dev_ifname32(struct net
*net
, struct compat_ifreq __user
*uifr32
)
2730 struct ifreq __user
*uifr
;
2733 uifr
= compat_alloc_user_space(sizeof(struct ifreq
));
2734 if (copy_in_user(uifr
, uifr32
, sizeof(struct compat_ifreq
)))
2737 err
= dev_ioctl(net
, SIOCGIFNAME
, uifr
);
2741 if (copy_in_user(uifr32
, uifr
, sizeof(struct compat_ifreq
)))
2747 static int dev_ifconf(struct net
*net
, struct compat_ifconf __user
*uifc32
)
2749 struct compat_ifconf ifc32
;
2751 struct ifconf __user
*uifc
;
2752 struct compat_ifreq __user
*ifr32
;
2753 struct ifreq __user
*ifr
;
2757 if (copy_from_user(&ifc32
, uifc32
, sizeof(struct compat_ifconf
)))
2760 memset(&ifc
, 0, sizeof(ifc
));
2761 if (ifc32
.ifcbuf
== 0) {
2765 uifc
= compat_alloc_user_space(sizeof(struct ifconf
));
2767 size_t len
= ((ifc32
.ifc_len
/ sizeof(struct compat_ifreq
)) + 1) *
2768 sizeof(struct ifreq
);
2769 uifc
= compat_alloc_user_space(sizeof(struct ifconf
) + len
);
2771 ifr
= ifc
.ifc_req
= (void __user
*)(uifc
+ 1);
2772 ifr32
= compat_ptr(ifc32
.ifcbuf
);
2773 for (i
= 0; i
< ifc32
.ifc_len
; i
+= sizeof(struct compat_ifreq
)) {
2774 if (copy_in_user(ifr
, ifr32
, sizeof(struct compat_ifreq
)))
2780 if (copy_to_user(uifc
, &ifc
, sizeof(struct ifconf
)))
2783 err
= dev_ioctl(net
, SIOCGIFCONF
, uifc
);
2787 if (copy_from_user(&ifc
, uifc
, sizeof(struct ifconf
)))
2791 ifr32
= compat_ptr(ifc32
.ifcbuf
);
2793 i
+ sizeof(struct compat_ifreq
) <= ifc32
.ifc_len
&& j
< ifc
.ifc_len
;
2794 i
+= sizeof(struct compat_ifreq
), j
+= sizeof(struct ifreq
)) {
2795 if (copy_in_user(ifr32
, ifr
, sizeof(struct compat_ifreq
)))
2801 if (ifc32
.ifcbuf
== 0) {
2802 /* Translate from 64-bit structure multiple to
2806 i
= ((i
/ sizeof(struct ifreq
)) * sizeof(struct compat_ifreq
));
2811 if (copy_to_user(uifc32
, &ifc32
, sizeof(struct compat_ifconf
)))
2817 static int ethtool_ioctl(struct net
*net
, struct compat_ifreq __user
*ifr32
)
2819 struct compat_ethtool_rxnfc __user
*compat_rxnfc
;
2820 bool convert_in
= false, convert_out
= false;
2821 size_t buf_size
= ALIGN(sizeof(struct ifreq
), 8);
2822 struct ethtool_rxnfc __user
*rxnfc
;
2823 struct ifreq __user
*ifr
;
2824 u32 rule_cnt
= 0, actual_rule_cnt
;
2829 if (get_user(data
, &ifr32
->ifr_ifru
.ifru_data
))
2832 compat_rxnfc
= compat_ptr(data
);
2834 if (get_user(ethcmd
, &compat_rxnfc
->cmd
))
2837 /* Most ethtool structures are defined without padding.
2838 * Unfortunately struct ethtool_rxnfc is an exception.
2843 case ETHTOOL_GRXCLSRLALL
:
2844 /* Buffer size is variable */
2845 if (get_user(rule_cnt
, &compat_rxnfc
->rule_cnt
))
2847 if (rule_cnt
> KMALLOC_MAX_SIZE
/ sizeof(u32
))
2849 buf_size
+= rule_cnt
* sizeof(u32
);
2851 case ETHTOOL_GRXRINGS
:
2852 case ETHTOOL_GRXCLSRLCNT
:
2853 case ETHTOOL_GRXCLSRULE
:
2854 case ETHTOOL_SRXCLSRLINS
:
2857 case ETHTOOL_SRXCLSRLDEL
:
2858 buf_size
+= sizeof(struct ethtool_rxnfc
);
2863 ifr
= compat_alloc_user_space(buf_size
);
2864 rxnfc
= (void __user
*)ifr
+ ALIGN(sizeof(struct ifreq
), 8);
2866 if (copy_in_user(&ifr
->ifr_name
, &ifr32
->ifr_name
, IFNAMSIZ
))
2869 if (put_user(convert_in
? rxnfc
: compat_ptr(data
),
2870 &ifr
->ifr_ifru
.ifru_data
))
2874 /* We expect there to be holes between fs.m_ext and
2875 * fs.ring_cookie and at the end of fs, but nowhere else.
2877 BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc
, fs
.m_ext
) +
2878 sizeof(compat_rxnfc
->fs
.m_ext
) !=
2879 offsetof(struct ethtool_rxnfc
, fs
.m_ext
) +
2880 sizeof(rxnfc
->fs
.m_ext
));
2882 offsetof(struct compat_ethtool_rxnfc
, fs
.location
) -
2883 offsetof(struct compat_ethtool_rxnfc
, fs
.ring_cookie
) !=
2884 offsetof(struct ethtool_rxnfc
, fs
.location
) -
2885 offsetof(struct ethtool_rxnfc
, fs
.ring_cookie
));
2887 if (copy_in_user(rxnfc
, compat_rxnfc
,
2888 (void __user
*)(&rxnfc
->fs
.m_ext
+ 1) -
2889 (void __user
*)rxnfc
) ||
2890 copy_in_user(&rxnfc
->fs
.ring_cookie
,
2891 &compat_rxnfc
->fs
.ring_cookie
,
2892 (void __user
*)(&rxnfc
->fs
.location
+ 1) -
2893 (void __user
*)&rxnfc
->fs
.ring_cookie
) ||
2894 copy_in_user(&rxnfc
->rule_cnt
, &compat_rxnfc
->rule_cnt
,
2895 sizeof(rxnfc
->rule_cnt
)))
2899 ret
= dev_ioctl(net
, SIOCETHTOOL
, ifr
);
2904 if (copy_in_user(compat_rxnfc
, rxnfc
,
2905 (const void __user
*)(&rxnfc
->fs
.m_ext
+ 1) -
2906 (const void __user
*)rxnfc
) ||
2907 copy_in_user(&compat_rxnfc
->fs
.ring_cookie
,
2908 &rxnfc
->fs
.ring_cookie
,
2909 (const void __user
*)(&rxnfc
->fs
.location
+ 1) -
2910 (const void __user
*)&rxnfc
->fs
.ring_cookie
) ||
2911 copy_in_user(&compat_rxnfc
->rule_cnt
, &rxnfc
->rule_cnt
,
2912 sizeof(rxnfc
->rule_cnt
)))
2915 if (ethcmd
== ETHTOOL_GRXCLSRLALL
) {
2916 /* As an optimisation, we only copy the actual
2917 * number of rules that the underlying
2918 * function returned. Since Mallory might
2919 * change the rule count in user memory, we
2920 * check that it is less than the rule count
2921 * originally given (as the user buffer size),
2922 * which has been range-checked.
2924 if (get_user(actual_rule_cnt
, &rxnfc
->rule_cnt
))
2926 if (actual_rule_cnt
< rule_cnt
)
2927 rule_cnt
= actual_rule_cnt
;
2928 if (copy_in_user(&compat_rxnfc
->rule_locs
[0],
2929 &rxnfc
->rule_locs
[0],
2930 rule_cnt
* sizeof(u32
)))
2938 static int compat_siocwandev(struct net
*net
, struct compat_ifreq __user
*uifr32
)
2941 compat_uptr_t uptr32
;
2942 struct ifreq __user
*uifr
;
2944 uifr
= compat_alloc_user_space(sizeof(*uifr
));
2945 if (copy_in_user(uifr
, uifr32
, sizeof(struct compat_ifreq
)))
2948 if (get_user(uptr32
, &uifr32
->ifr_settings
.ifs_ifsu
))
2951 uptr
= compat_ptr(uptr32
);
2953 if (put_user(uptr
, &uifr
->ifr_settings
.ifs_ifsu
.raw_hdlc
))
2956 return dev_ioctl(net
, SIOCWANDEV
, uifr
);
2959 static int bond_ioctl(struct net
*net
, unsigned int cmd
,
2960 struct compat_ifreq __user
*ifr32
)
2963 struct ifreq __user
*uifr
;
2964 mm_segment_t old_fs
;
2970 case SIOCBONDENSLAVE
:
2971 case SIOCBONDRELEASE
:
2972 case SIOCBONDSETHWADDR
:
2973 case SIOCBONDCHANGEACTIVE
:
2974 if (copy_from_user(&kifr
, ifr32
, sizeof(struct compat_ifreq
)))
2979 err
= dev_ioctl(net
, cmd
,
2980 (struct ifreq __user __force
*) &kifr
);
2984 case SIOCBONDSLAVEINFOQUERY
:
2985 case SIOCBONDINFOQUERY
:
2986 uifr
= compat_alloc_user_space(sizeof(*uifr
));
2987 if (copy_in_user(&uifr
->ifr_name
, &ifr32
->ifr_name
, IFNAMSIZ
))
2990 if (get_user(data
, &ifr32
->ifr_ifru
.ifru_data
))
2993 datap
= compat_ptr(data
);
2994 if (put_user(datap
, &uifr
->ifr_ifru
.ifru_data
))
2997 return dev_ioctl(net
, cmd
, uifr
);
2999 return -ENOIOCTLCMD
;
3003 static int siocdevprivate_ioctl(struct net
*net
, unsigned int cmd
,
3004 struct compat_ifreq __user
*u_ifreq32
)
3006 struct ifreq __user
*u_ifreq64
;
3007 char tmp_buf
[IFNAMSIZ
];
3008 void __user
*data64
;
3011 if (copy_from_user(&tmp_buf
[0], &(u_ifreq32
->ifr_ifrn
.ifrn_name
[0]),
3014 if (__get_user(data32
, &u_ifreq32
->ifr_ifru
.ifru_data
))
3016 data64
= compat_ptr(data32
);
3018 u_ifreq64
= compat_alloc_user_space(sizeof(*u_ifreq64
));
3020 /* Don't check these user accesses, just let that get trapped
3021 * in the ioctl handler instead.
3023 if (copy_to_user(&u_ifreq64
->ifr_ifrn
.ifrn_name
[0], &tmp_buf
[0],
3026 if (__put_user(data64
, &u_ifreq64
->ifr_ifru
.ifru_data
))
3029 return dev_ioctl(net
, cmd
, u_ifreq64
);
3032 static int dev_ifsioc(struct net
*net
, struct socket
*sock
,
3033 unsigned int cmd
, struct compat_ifreq __user
*uifr32
)
3035 struct ifreq __user
*uifr
;
3038 uifr
= compat_alloc_user_space(sizeof(*uifr
));
3039 if (copy_in_user(uifr
, uifr32
, sizeof(*uifr32
)))
3042 err
= sock_do_ioctl(net
, sock
, cmd
, (unsigned long)uifr
);
3053 case SIOCGIFBRDADDR
:
3054 case SIOCGIFDSTADDR
:
3055 case SIOCGIFNETMASK
:
3060 if (copy_in_user(uifr32
, uifr
, sizeof(*uifr32
)))
3068 static int compat_sioc_ifmap(struct net
*net
, unsigned int cmd
,
3069 struct compat_ifreq __user
*uifr32
)
3072 struct compat_ifmap __user
*uifmap32
;
3073 mm_segment_t old_fs
;
3076 uifmap32
= &uifr32
->ifr_ifru
.ifru_map
;
3077 err
= copy_from_user(&ifr
, uifr32
, sizeof(ifr
.ifr_name
));
3078 err
|= __get_user(ifr
.ifr_map
.mem_start
, &uifmap32
->mem_start
);
3079 err
|= __get_user(ifr
.ifr_map
.mem_end
, &uifmap32
->mem_end
);
3080 err
|= __get_user(ifr
.ifr_map
.base_addr
, &uifmap32
->base_addr
);
3081 err
|= __get_user(ifr
.ifr_map
.irq
, &uifmap32
->irq
);
3082 err
|= __get_user(ifr
.ifr_map
.dma
, &uifmap32
->dma
);
3083 err
|= __get_user(ifr
.ifr_map
.port
, &uifmap32
->port
);
3089 err
= dev_ioctl(net
, cmd
, (void __user __force
*)&ifr
);
3092 if (cmd
== SIOCGIFMAP
&& !err
) {
3093 err
= copy_to_user(uifr32
, &ifr
, sizeof(ifr
.ifr_name
));
3094 err
|= __put_user(ifr
.ifr_map
.mem_start
, &uifmap32
->mem_start
);
3095 err
|= __put_user(ifr
.ifr_map
.mem_end
, &uifmap32
->mem_end
);
3096 err
|= __put_user(ifr
.ifr_map
.base_addr
, &uifmap32
->base_addr
);
3097 err
|= __put_user(ifr
.ifr_map
.irq
, &uifmap32
->irq
);
3098 err
|= __put_user(ifr
.ifr_map
.dma
, &uifmap32
->dma
);
3099 err
|= __put_user(ifr
.ifr_map
.port
, &uifmap32
->port
);
3106 static int compat_siocshwtstamp(struct net
*net
, struct compat_ifreq __user
*uifr32
)
3109 compat_uptr_t uptr32
;
3110 struct ifreq __user
*uifr
;
3112 uifr
= compat_alloc_user_space(sizeof(*uifr
));
3113 if (copy_in_user(uifr
, uifr32
, sizeof(struct compat_ifreq
)))
3116 if (get_user(uptr32
, &uifr32
->ifr_data
))
3119 uptr
= compat_ptr(uptr32
);
3121 if (put_user(uptr
, &uifr
->ifr_data
))
3124 return dev_ioctl(net
, SIOCSHWTSTAMP
, uifr
);
3129 struct sockaddr rt_dst
; /* target address */
3130 struct sockaddr rt_gateway
; /* gateway addr (RTF_GATEWAY) */
3131 struct sockaddr rt_genmask
; /* target network mask (IP) */
3132 unsigned short rt_flags
;
3135 unsigned char rt_tos
;
3136 unsigned char rt_class
;
3138 short rt_metric
; /* +1 for binary compatibility! */
3139 /* char * */ u32 rt_dev
; /* forcing the device at add */
3140 u32 rt_mtu
; /* per route MTU/Window */
3141 u32 rt_window
; /* Window clamping */
3142 unsigned short rt_irtt
; /* Initial RTT */
3145 struct in6_rtmsg32
{
3146 struct in6_addr rtmsg_dst
;
3147 struct in6_addr rtmsg_src
;
3148 struct in6_addr rtmsg_gateway
;
3158 static int routing_ioctl(struct net
*net
, struct socket
*sock
,
3159 unsigned int cmd
, void __user
*argp
)
3163 struct in6_rtmsg r6
;
3167 mm_segment_t old_fs
= get_fs();
3169 if (sock
&& sock
->sk
&& sock
->sk
->sk_family
== AF_INET6
) { /* ipv6 */
3170 struct in6_rtmsg32 __user
*ur6
= argp
;
3171 ret
= copy_from_user(&r6
.rtmsg_dst
, &(ur6
->rtmsg_dst
),
3172 3 * sizeof(struct in6_addr
));
3173 ret
|= __get_user(r6
.rtmsg_type
, &(ur6
->rtmsg_type
));
3174 ret
|= __get_user(r6
.rtmsg_dst_len
, &(ur6
->rtmsg_dst_len
));
3175 ret
|= __get_user(r6
.rtmsg_src_len
, &(ur6
->rtmsg_src_len
));
3176 ret
|= __get_user(r6
.rtmsg_metric
, &(ur6
->rtmsg_metric
));
3177 ret
|= __get_user(r6
.rtmsg_info
, &(ur6
->rtmsg_info
));
3178 ret
|= __get_user(r6
.rtmsg_flags
, &(ur6
->rtmsg_flags
));
3179 ret
|= __get_user(r6
.rtmsg_ifindex
, &(ur6
->rtmsg_ifindex
));
3183 struct rtentry32 __user
*ur4
= argp
;
3184 ret
= copy_from_user(&r4
.rt_dst
, &(ur4
->rt_dst
),
3185 3 * sizeof(struct sockaddr
));
3186 ret
|= __get_user(r4
.rt_flags
, &(ur4
->rt_flags
));
3187 ret
|= __get_user(r4
.rt_metric
, &(ur4
->rt_metric
));
3188 ret
|= __get_user(r4
.rt_mtu
, &(ur4
->rt_mtu
));
3189 ret
|= __get_user(r4
.rt_window
, &(ur4
->rt_window
));
3190 ret
|= __get_user(r4
.rt_irtt
, &(ur4
->rt_irtt
));
3191 ret
|= __get_user(rtdev
, &(ur4
->rt_dev
));
3193 ret
|= copy_from_user(devname
, compat_ptr(rtdev
), 15);
3194 r4
.rt_dev
= (char __user __force
*)devname
;
3208 ret
= sock_do_ioctl(net
, sock
, cmd
, (unsigned long) r
);
3215 /* Since old style bridge ioctl's endup using SIOCDEVPRIVATE
3216 * for some operations; this forces use of the newer bridge-utils that
3217 * use compatible ioctls
3219 static int old_bridge_ioctl(compat_ulong_t __user
*argp
)
3223 if (get_user(tmp
, argp
))
3225 if (tmp
== BRCTL_GET_VERSION
)
3226 return BRCTL_VERSION
+ 1;
3230 static int compat_sock_ioctl_trans(struct file
*file
, struct socket
*sock
,
3231 unsigned int cmd
, unsigned long arg
)
3233 void __user
*argp
= compat_ptr(arg
);
3234 struct sock
*sk
= sock
->sk
;
3235 struct net
*net
= sock_net(sk
);
3237 if (cmd
>= SIOCDEVPRIVATE
&& cmd
<= (SIOCDEVPRIVATE
+ 15))
3238 return siocdevprivate_ioctl(net
, cmd
, argp
);
3243 return old_bridge_ioctl(argp
);
3245 return dev_ifname32(net
, argp
);
3247 return dev_ifconf(net
, argp
);
3249 return ethtool_ioctl(net
, argp
);
3251 return compat_siocwandev(net
, argp
);
3254 return compat_sioc_ifmap(net
, cmd
, argp
);
3255 case SIOCBONDENSLAVE
:
3256 case SIOCBONDRELEASE
:
3257 case SIOCBONDSETHWADDR
:
3258 case SIOCBONDSLAVEINFOQUERY
:
3259 case SIOCBONDINFOQUERY
:
3260 case SIOCBONDCHANGEACTIVE
:
3261 return bond_ioctl(net
, cmd
, argp
);
3264 return routing_ioctl(net
, sock
, cmd
, argp
);
3266 return do_siocgstamp(net
, sock
, cmd
, argp
);
3268 return do_siocgstampns(net
, sock
, cmd
, argp
);
3270 return compat_siocshwtstamp(net
, argp
);
3282 return sock_ioctl(file
, cmd
, arg
);
3299 case SIOCSIFHWBROADCAST
:
3301 case SIOCGIFBRDADDR
:
3302 case SIOCSIFBRDADDR
:
3303 case SIOCGIFDSTADDR
:
3304 case SIOCSIFDSTADDR
:
3305 case SIOCGIFNETMASK
:
3306 case SIOCSIFNETMASK
:
3317 return dev_ifsioc(net
, sock
, cmd
, argp
);
3323 return sock_do_ioctl(net
, sock
, cmd
, arg
);
3326 return -ENOIOCTLCMD
;
3329 static long compat_sock_ioctl(struct file
*file
, unsigned int cmd
,
3332 struct socket
*sock
= file
->private_data
;
3333 int ret
= -ENOIOCTLCMD
;
3340 if (sock
->ops
->compat_ioctl
)
3341 ret
= sock
->ops
->compat_ioctl(sock
, cmd
, arg
);
3343 if (ret
== -ENOIOCTLCMD
&&
3344 (cmd
>= SIOCIWFIRST
&& cmd
<= SIOCIWLAST
))
3345 ret
= compat_wext_handle_ioctl(net
, cmd
, arg
);
3347 if (ret
== -ENOIOCTLCMD
)
3348 ret
= compat_sock_ioctl_trans(file
, sock
, cmd
, arg
);
3354 int kernel_bind(struct socket
*sock
, struct sockaddr
*addr
, int addrlen
)
3356 return sock
->ops
->bind(sock
, addr
, addrlen
);
3358 EXPORT_SYMBOL(kernel_bind
);
3360 int kernel_listen(struct socket
*sock
, int backlog
)
3362 return sock
->ops
->listen(sock
, backlog
);
3364 EXPORT_SYMBOL(kernel_listen
);
3366 int kernel_accept(struct socket
*sock
, struct socket
**newsock
, int flags
)
3368 struct sock
*sk
= sock
->sk
;
3371 err
= sock_create_lite(sk
->sk_family
, sk
->sk_type
, sk
->sk_protocol
,
3376 err
= sock
->ops
->accept(sock
, *newsock
, flags
);
3378 sock_release(*newsock
);
3383 (*newsock
)->ops
= sock
->ops
;
3384 __module_get((*newsock
)->ops
->owner
);
3389 EXPORT_SYMBOL(kernel_accept
);
3391 int kernel_connect(struct socket
*sock
, struct sockaddr
*addr
, int addrlen
,
3394 return sock
->ops
->connect(sock
, addr
, addrlen
, flags
);
3396 EXPORT_SYMBOL(kernel_connect
);
3398 int kernel_getsockname(struct socket
*sock
, struct sockaddr
*addr
,
3401 return sock
->ops
->getname(sock
, addr
, addrlen
, 0);
3403 EXPORT_SYMBOL(kernel_getsockname
);
3405 int kernel_getpeername(struct socket
*sock
, struct sockaddr
*addr
,
3408 return sock
->ops
->getname(sock
, addr
, addrlen
, 1);
3410 EXPORT_SYMBOL(kernel_getpeername
);
3412 int kernel_getsockopt(struct socket
*sock
, int level
, int optname
,
3413 char *optval
, int *optlen
)
3415 mm_segment_t oldfs
= get_fs();
3416 char __user
*uoptval
;
3417 int __user
*uoptlen
;
3420 uoptval
= (char __user __force
*) optval
;
3421 uoptlen
= (int __user __force
*) optlen
;
3424 if (level
== SOL_SOCKET
)
3425 err
= sock_getsockopt(sock
, level
, optname
, uoptval
, uoptlen
);
3427 err
= sock
->ops
->getsockopt(sock
, level
, optname
, uoptval
,
3432 EXPORT_SYMBOL(kernel_getsockopt
);
3434 int kernel_setsockopt(struct socket
*sock
, int level
, int optname
,
3435 char *optval
, unsigned int optlen
)
3437 mm_segment_t oldfs
= get_fs();
3438 char __user
*uoptval
;
3441 uoptval
= (char __user __force
*) optval
;
3444 if (level
== SOL_SOCKET
)
3445 err
= sock_setsockopt(sock
, level
, optname
, uoptval
, optlen
);
3447 err
= sock
->ops
->setsockopt(sock
, level
, optname
, uoptval
,
3452 EXPORT_SYMBOL(kernel_setsockopt
);
3454 int kernel_sendpage(struct socket
*sock
, struct page
*page
, int offset
,
3455 size_t size
, int flags
)
3457 if (sock
->ops
->sendpage
)
3458 return sock
->ops
->sendpage(sock
, page
, offset
, size
, flags
);
3460 return sock_no_sendpage(sock
, page
, offset
, size
, flags
);
3462 EXPORT_SYMBOL(kernel_sendpage
);
3464 int kernel_sock_ioctl(struct socket
*sock
, int cmd
, unsigned long arg
)
3466 mm_segment_t oldfs
= get_fs();
3470 err
= sock
->ops
->ioctl(sock
, cmd
, arg
);
3475 EXPORT_SYMBOL(kernel_sock_ioctl
);
3477 int kernel_sock_shutdown(struct socket
*sock
, enum sock_shutdown_cmd how
)
3479 return sock
->ops
->shutdown(sock
, how
);
3481 EXPORT_SYMBOL(kernel_sock_shutdown
);