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/ptp_classify.h>
76 #include <linux/init.h>
77 #include <linux/poll.h>
78 #include <linux/cache.h>
79 #include <linux/module.h>
80 #include <linux/highmem.h>
81 #include <linux/mount.h>
82 #include <linux/security.h>
83 #include <linux/syscalls.h>
84 #include <linux/compat.h>
85 #include <linux/kmod.h>
86 #include <linux/audit.h>
87 #include <linux/wireless.h>
88 #include <linux/nsproxy.h>
89 #include <linux/magic.h>
90 #include <linux/slab.h>
91 #include <linux/xattr.h>
92 #include <linux/nospec.h>
94 #include <linux/uaccess.h>
95 #include <asm/unistd.h>
97 #include <net/compat.h>
99 #include <net/cls_cgroup.h>
101 #include <net/sock.h>
102 #include <linux/netfilter.h>
104 #include <linux/if_tun.h>
105 #include <linux/ipv6_route.h>
106 #include <linux/route.h>
107 #include <linux/sockios.h>
108 #include <linux/atalk.h>
109 #include <net/busy_poll.h>
110 #include <linux/errqueue.h>
112 #ifdef CONFIG_NET_RX_BUSY_POLL
113 unsigned int sysctl_net_busy_read __read_mostly
;
114 unsigned int sysctl_net_busy_poll __read_mostly
;
117 static ssize_t
sock_read_iter(struct kiocb
*iocb
, struct iov_iter
*to
);
118 static ssize_t
sock_write_iter(struct kiocb
*iocb
, struct iov_iter
*from
);
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 .read_iter
= sock_read_iter
,
145 .write_iter
= sock_write_iter
,
147 .unlocked_ioctl
= sock_ioctl
,
149 .compat_ioctl
= compat_sock_ioctl
,
152 .release
= sock_close
,
153 .fasync
= sock_fasync
,
154 .sendpage
= sock_sendpage
,
155 .splice_write
= generic_splice_sendpage
,
156 .splice_read
= sock_splice_read
,
160 * The protocol list. Each protocol is registered in here.
163 static DEFINE_SPINLOCK(net_family_lock
);
164 static const struct net_proto_family __rcu
*net_families
[NPROTO
] __read_mostly
;
167 * Statistics counters of the socket lists
170 static DEFINE_PER_CPU(int, sockets_in_use
);
174 * Move socket addresses back and forth across the kernel/user
175 * divide and look after the messy bits.
179 * move_addr_to_kernel - copy a socket address into kernel space
180 * @uaddr: Address in user space
181 * @kaddr: Address in kernel space
182 * @ulen: Length in user space
184 * The address is copied into kernel space. If the provided address is
185 * too long an error code of -EINVAL is returned. If the copy gives
186 * invalid addresses -EFAULT is returned. On a success 0 is returned.
189 int move_addr_to_kernel(void __user
*uaddr
, int ulen
, struct sockaddr_storage
*kaddr
)
191 if (ulen
< 0 || ulen
> sizeof(struct sockaddr_storage
))
195 if (copy_from_user(kaddr
, uaddr
, ulen
))
197 return audit_sockaddr(ulen
, kaddr
);
201 * move_addr_to_user - copy an address to user space
202 * @kaddr: kernel space address
203 * @klen: length of address in kernel
204 * @uaddr: user space address
205 * @ulen: pointer to user length field
207 * The value pointed to by ulen on entry is the buffer length available.
208 * This is overwritten with the buffer space used. -EINVAL is returned
209 * if an overlong buffer is specified or a negative buffer size. -EFAULT
210 * is returned if either the buffer or the length field are not
212 * After copying the data up to the limit the user specifies, the true
213 * length of the data is written over the length limit the user
214 * specified. Zero is returned for a success.
217 static int move_addr_to_user(struct sockaddr_storage
*kaddr
, int klen
,
218 void __user
*uaddr
, int __user
*ulen
)
223 BUG_ON(klen
> sizeof(struct sockaddr_storage
));
224 err
= get_user(len
, ulen
);
232 if (audit_sockaddr(klen
, kaddr
))
234 if (copy_to_user(uaddr
, kaddr
, len
))
238 * "fromlen shall refer to the value before truncation.."
241 return __put_user(klen
, ulen
);
244 static struct kmem_cache
*sock_inode_cachep __read_mostly
;
246 static struct inode
*sock_alloc_inode(struct super_block
*sb
)
248 struct socket_alloc
*ei
;
249 struct socket_wq
*wq
;
251 ei
= kmem_cache_alloc(sock_inode_cachep
, GFP_KERNEL
);
254 wq
= kmalloc(sizeof(*wq
), GFP_KERNEL
);
256 kmem_cache_free(sock_inode_cachep
, ei
);
259 init_waitqueue_head(&wq
->wait
);
260 wq
->fasync_list
= NULL
;
262 RCU_INIT_POINTER(ei
->socket
.wq
, wq
);
264 ei
->socket
.state
= SS_UNCONNECTED
;
265 ei
->socket
.flags
= 0;
266 ei
->socket
.ops
= NULL
;
267 ei
->socket
.sk
= NULL
;
268 ei
->socket
.file
= NULL
;
270 return &ei
->vfs_inode
;
273 static void sock_destroy_inode(struct inode
*inode
)
275 struct socket_alloc
*ei
;
276 struct socket_wq
*wq
;
278 ei
= container_of(inode
, struct socket_alloc
, vfs_inode
);
279 wq
= rcu_dereference_protected(ei
->socket
.wq
, 1);
281 kmem_cache_free(sock_inode_cachep
, ei
);
284 static void init_once(void *foo
)
286 struct socket_alloc
*ei
= (struct socket_alloc
*)foo
;
288 inode_init_once(&ei
->vfs_inode
);
291 static void init_inodecache(void)
293 sock_inode_cachep
= kmem_cache_create("sock_inode_cache",
294 sizeof(struct socket_alloc
),
296 (SLAB_HWCACHE_ALIGN
|
297 SLAB_RECLAIM_ACCOUNT
|
298 SLAB_MEM_SPREAD
| SLAB_ACCOUNT
),
300 BUG_ON(sock_inode_cachep
== NULL
);
303 static const struct super_operations sockfs_ops
= {
304 .alloc_inode
= sock_alloc_inode
,
305 .destroy_inode
= sock_destroy_inode
,
306 .statfs
= simple_statfs
,
310 * sockfs_dname() is called from d_path().
312 static char *sockfs_dname(struct dentry
*dentry
, char *buffer
, int buflen
)
314 return dynamic_dname(dentry
, buffer
, buflen
, "socket:[%lu]",
315 d_inode(dentry
)->i_ino
);
318 static const struct dentry_operations sockfs_dentry_operations
= {
319 .d_dname
= sockfs_dname
,
322 static int sockfs_xattr_get(const struct xattr_handler
*handler
,
323 struct dentry
*dentry
, struct inode
*inode
,
324 const char *suffix
, void *value
, size_t size
)
327 if (dentry
->d_name
.len
+ 1 > size
)
329 memcpy(value
, dentry
->d_name
.name
, dentry
->d_name
.len
+ 1);
331 return dentry
->d_name
.len
+ 1;
334 #define XATTR_SOCKPROTONAME_SUFFIX "sockprotoname"
335 #define XATTR_NAME_SOCKPROTONAME (XATTR_SYSTEM_PREFIX XATTR_SOCKPROTONAME_SUFFIX)
336 #define XATTR_NAME_SOCKPROTONAME_LEN (sizeof(XATTR_NAME_SOCKPROTONAME)-1)
338 static const struct xattr_handler sockfs_xattr_handler
= {
339 .name
= XATTR_NAME_SOCKPROTONAME
,
340 .get
= sockfs_xattr_get
,
343 static int sockfs_security_xattr_set(const struct xattr_handler
*handler
,
344 struct dentry
*dentry
, struct inode
*inode
,
345 const char *suffix
, const void *value
,
346 size_t size
, int flags
)
348 /* Handled by LSM. */
352 static const struct xattr_handler sockfs_security_xattr_handler
= {
353 .prefix
= XATTR_SECURITY_PREFIX
,
354 .set
= sockfs_security_xattr_set
,
357 static const struct xattr_handler
*sockfs_xattr_handlers
[] = {
358 &sockfs_xattr_handler
,
359 &sockfs_security_xattr_handler
,
363 static struct dentry
*sockfs_mount(struct file_system_type
*fs_type
,
364 int flags
, const char *dev_name
, void *data
)
366 return mount_pseudo_xattr(fs_type
, "socket:", &sockfs_ops
,
367 sockfs_xattr_handlers
,
368 &sockfs_dentry_operations
, SOCKFS_MAGIC
);
371 static struct vfsmount
*sock_mnt __read_mostly
;
373 static struct file_system_type sock_fs_type
= {
375 .mount
= sockfs_mount
,
376 .kill_sb
= kill_anon_super
,
380 * Obtains the first available file descriptor and sets it up for use.
382 * These functions create file structures and maps them to fd space
383 * of the current process. On success it returns file descriptor
384 * and file struct implicitly stored in sock->file.
385 * Note that another thread may close file descriptor before we return
386 * from this function. We use the fact that now we do not refer
387 * to socket after mapping. If one day we will need it, this
388 * function will increment ref. count on file by 1.
390 * In any case returned fd MAY BE not valid!
391 * This race condition is unavoidable
392 * with shared fd spaces, we cannot solve it inside kernel,
393 * but we take care of internal coherence yet.
396 struct file
*sock_alloc_file(struct socket
*sock
, int flags
, const char *dname
)
398 struct qstr name
= { .name
= "" };
404 name
.len
= strlen(name
.name
);
405 } else if (sock
->sk
) {
406 name
.name
= sock
->sk
->sk_prot_creator
->name
;
407 name
.len
= strlen(name
.name
);
409 path
.dentry
= d_alloc_pseudo(sock_mnt
->mnt_sb
, &name
);
410 if (unlikely(!path
.dentry
))
411 return ERR_PTR(-ENOMEM
);
412 path
.mnt
= mntget(sock_mnt
);
414 d_instantiate(path
.dentry
, SOCK_INODE(sock
));
416 file
= alloc_file(&path
, FMODE_READ
| FMODE_WRITE
,
419 /* drop dentry, keep inode */
420 ihold(d_inode(path
.dentry
));
426 file
->f_flags
= O_RDWR
| (flags
& O_NONBLOCK
);
427 file
->private_data
= sock
;
430 EXPORT_SYMBOL(sock_alloc_file
);
432 static int sock_map_fd(struct socket
*sock
, int flags
)
434 struct file
*newfile
;
435 int fd
= get_unused_fd_flags(flags
);
436 if (unlikely(fd
< 0))
439 newfile
= sock_alloc_file(sock
, flags
, NULL
);
440 if (likely(!IS_ERR(newfile
))) {
441 fd_install(fd
, newfile
);
446 return PTR_ERR(newfile
);
449 struct socket
*sock_from_file(struct file
*file
, int *err
)
451 if (file
->f_op
== &socket_file_ops
)
452 return file
->private_data
; /* set in sock_map_fd */
457 EXPORT_SYMBOL(sock_from_file
);
460 * sockfd_lookup - Go from a file number to its socket slot
462 * @err: pointer to an error code return
464 * The file handle passed in is locked and the socket it is bound
465 * to is returned. If an error occurs the err pointer is overwritten
466 * with a negative errno code and NULL is returned. The function checks
467 * for both invalid handles and passing a handle which is not a socket.
469 * On a success the socket object pointer is returned.
472 struct socket
*sockfd_lookup(int fd
, int *err
)
483 sock
= sock_from_file(file
, err
);
488 EXPORT_SYMBOL(sockfd_lookup
);
490 static struct socket
*sockfd_lookup_light(int fd
, int *err
, int *fput_needed
)
492 struct fd f
= fdget(fd
);
497 sock
= sock_from_file(f
.file
, err
);
499 *fput_needed
= f
.flags
;
507 static ssize_t
sockfs_listxattr(struct dentry
*dentry
, char *buffer
,
513 len
= security_inode_listsecurity(d_inode(dentry
), buffer
, size
);
523 len
= (XATTR_NAME_SOCKPROTONAME_LEN
+ 1);
528 memcpy(buffer
, XATTR_NAME_SOCKPROTONAME
, len
);
535 static int sockfs_setattr(struct dentry
*dentry
, struct iattr
*iattr
)
537 int err
= simple_setattr(dentry
, iattr
);
539 if (!err
&& (iattr
->ia_valid
& ATTR_UID
)) {
540 struct socket
*sock
= SOCKET_I(d_inode(dentry
));
543 sock
->sk
->sk_uid
= iattr
->ia_uid
;
551 static const struct inode_operations sockfs_inode_ops
= {
552 .listxattr
= sockfs_listxattr
,
553 .setattr
= sockfs_setattr
,
557 * sock_alloc - allocate a socket
559 * Allocate a new inode and socket object. The two are bound together
560 * and initialised. The socket is then returned. If we are out of inodes
564 struct socket
*sock_alloc(void)
569 inode
= new_inode_pseudo(sock_mnt
->mnt_sb
);
573 sock
= SOCKET_I(inode
);
575 inode
->i_ino
= get_next_ino();
576 inode
->i_mode
= S_IFSOCK
| S_IRWXUGO
;
577 inode
->i_uid
= current_fsuid();
578 inode
->i_gid
= current_fsgid();
579 inode
->i_op
= &sockfs_inode_ops
;
581 this_cpu_add(sockets_in_use
, 1);
584 EXPORT_SYMBOL(sock_alloc
);
587 * sock_release - close a socket
588 * @sock: socket to close
590 * The socket is released from the protocol stack if it has a release
591 * callback, and the inode is then released if the socket is bound to
592 * an inode not a file.
595 static void __sock_release(struct socket
*sock
, struct inode
*inode
)
598 struct module
*owner
= sock
->ops
->owner
;
602 sock
->ops
->release(sock
);
610 if (rcu_dereference_protected(sock
->wq
, 1)->fasync_list
)
611 pr_err("%s: fasync list not empty!\n", __func__
);
613 this_cpu_sub(sockets_in_use
, 1);
615 iput(SOCK_INODE(sock
));
621 void sock_release(struct socket
*sock
)
623 __sock_release(sock
, NULL
);
625 EXPORT_SYMBOL(sock_release
);
627 void __sock_tx_timestamp(__u16 tsflags
, __u8
*tx_flags
)
629 u8 flags
= *tx_flags
;
631 if (tsflags
& SOF_TIMESTAMPING_TX_HARDWARE
)
632 flags
|= SKBTX_HW_TSTAMP
;
634 if (tsflags
& SOF_TIMESTAMPING_TX_SOFTWARE
)
635 flags
|= SKBTX_SW_TSTAMP
;
637 if (tsflags
& SOF_TIMESTAMPING_TX_SCHED
)
638 flags
|= SKBTX_SCHED_TSTAMP
;
642 EXPORT_SYMBOL(__sock_tx_timestamp
);
644 static inline int sock_sendmsg_nosec(struct socket
*sock
, struct msghdr
*msg
)
646 int ret
= sock
->ops
->sendmsg(sock
, msg
, msg_data_left(msg
));
647 BUG_ON(ret
== -EIOCBQUEUED
);
651 int sock_sendmsg(struct socket
*sock
, struct msghdr
*msg
)
653 int err
= security_socket_sendmsg(sock
, msg
,
656 return err
?: sock_sendmsg_nosec(sock
, msg
);
658 EXPORT_SYMBOL(sock_sendmsg
);
660 int kernel_sendmsg(struct socket
*sock
, struct msghdr
*msg
,
661 struct kvec
*vec
, size_t num
, size_t size
)
663 iov_iter_kvec(&msg
->msg_iter
, WRITE
| ITER_KVEC
, vec
, num
, size
);
664 return sock_sendmsg(sock
, msg
);
666 EXPORT_SYMBOL(kernel_sendmsg
);
668 int kernel_sendmsg_locked(struct sock
*sk
, struct msghdr
*msg
,
669 struct kvec
*vec
, size_t num
, size_t size
)
671 struct socket
*sock
= sk
->sk_socket
;
673 if (!sock
->ops
->sendmsg_locked
)
674 return sock_no_sendmsg_locked(sk
, msg
, size
);
676 iov_iter_kvec(&msg
->msg_iter
, WRITE
| ITER_KVEC
, vec
, num
, size
);
678 return sock
->ops
->sendmsg_locked(sk
, msg
, msg_data_left(msg
));
680 EXPORT_SYMBOL(kernel_sendmsg_locked
);
682 static bool skb_is_err_queue(const struct sk_buff
*skb
)
684 /* pkt_type of skbs enqueued on the error queue are set to
685 * PACKET_OUTGOING in skb_set_err_queue(). This is only safe to do
686 * in recvmsg, since skbs received on a local socket will never
687 * have a pkt_type of PACKET_OUTGOING.
689 return skb
->pkt_type
== PACKET_OUTGOING
;
692 /* On transmit, software and hardware timestamps are returned independently.
693 * As the two skb clones share the hardware timestamp, which may be updated
694 * before the software timestamp is received, a hardware TX timestamp may be
695 * returned only if there is no software TX timestamp. Ignore false software
696 * timestamps, which may be made in the __sock_recv_timestamp() call when the
697 * option SO_TIMESTAMP(NS) is enabled on the socket, even when the skb has a
698 * hardware timestamp.
700 static bool skb_is_swtx_tstamp(const struct sk_buff
*skb
, int false_tstamp
)
702 return skb
->tstamp
&& !false_tstamp
&& skb_is_err_queue(skb
);
705 static void put_ts_pktinfo(struct msghdr
*msg
, struct sk_buff
*skb
)
707 struct scm_ts_pktinfo ts_pktinfo
;
708 struct net_device
*orig_dev
;
710 if (!skb_mac_header_was_set(skb
))
713 memset(&ts_pktinfo
, 0, sizeof(ts_pktinfo
));
716 orig_dev
= dev_get_by_napi_id(skb_napi_id(skb
));
718 ts_pktinfo
.if_index
= orig_dev
->ifindex
;
721 ts_pktinfo
.pkt_length
= skb
->len
- skb_mac_offset(skb
);
722 put_cmsg(msg
, SOL_SOCKET
, SCM_TIMESTAMPING_PKTINFO
,
723 sizeof(ts_pktinfo
), &ts_pktinfo
);
727 * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
729 void __sock_recv_timestamp(struct msghdr
*msg
, struct sock
*sk
,
732 int need_software_tstamp
= sock_flag(sk
, SOCK_RCVTSTAMP
);
733 struct scm_timestamping tss
;
734 int empty
= 1, false_tstamp
= 0;
735 struct skb_shared_hwtstamps
*shhwtstamps
=
738 /* Race occurred between timestamp enabling and packet
739 receiving. Fill in the current time for now. */
740 if (need_software_tstamp
&& skb
->tstamp
== 0) {
741 __net_timestamp(skb
);
745 if (need_software_tstamp
) {
746 if (!sock_flag(sk
, SOCK_RCVTSTAMPNS
)) {
748 skb_get_timestamp(skb
, &tv
);
749 put_cmsg(msg
, SOL_SOCKET
, SCM_TIMESTAMP
,
753 skb_get_timestampns(skb
, &ts
);
754 put_cmsg(msg
, SOL_SOCKET
, SCM_TIMESTAMPNS
,
759 memset(&tss
, 0, sizeof(tss
));
760 if ((sk
->sk_tsflags
& SOF_TIMESTAMPING_SOFTWARE
) &&
761 ktime_to_timespec_cond(skb
->tstamp
, tss
.ts
+ 0))
764 (sk
->sk_tsflags
& SOF_TIMESTAMPING_RAW_HARDWARE
) &&
765 !skb_is_swtx_tstamp(skb
, false_tstamp
) &&
766 ktime_to_timespec_cond(shhwtstamps
->hwtstamp
, tss
.ts
+ 2)) {
768 if ((sk
->sk_tsflags
& SOF_TIMESTAMPING_OPT_PKTINFO
) &&
769 !skb_is_err_queue(skb
))
770 put_ts_pktinfo(msg
, skb
);
773 put_cmsg(msg
, SOL_SOCKET
,
774 SCM_TIMESTAMPING
, sizeof(tss
), &tss
);
776 if (skb_is_err_queue(skb
) && skb
->len
&&
777 SKB_EXT_ERR(skb
)->opt_stats
)
778 put_cmsg(msg
, SOL_SOCKET
, SCM_TIMESTAMPING_OPT_STATS
,
779 skb
->len
, skb
->data
);
782 EXPORT_SYMBOL_GPL(__sock_recv_timestamp
);
784 void __sock_recv_wifi_status(struct msghdr
*msg
, struct sock
*sk
,
789 if (!sock_flag(sk
, SOCK_WIFI_STATUS
))
791 if (!skb
->wifi_acked_valid
)
794 ack
= skb
->wifi_acked
;
796 put_cmsg(msg
, SOL_SOCKET
, SCM_WIFI_STATUS
, sizeof(ack
), &ack
);
798 EXPORT_SYMBOL_GPL(__sock_recv_wifi_status
);
800 static inline void sock_recv_drops(struct msghdr
*msg
, struct sock
*sk
,
803 if (sock_flag(sk
, SOCK_RXQ_OVFL
) && skb
&& SOCK_SKB_CB(skb
)->dropcount
)
804 put_cmsg(msg
, SOL_SOCKET
, SO_RXQ_OVFL
,
805 sizeof(__u32
), &SOCK_SKB_CB(skb
)->dropcount
);
808 void __sock_recv_ts_and_drops(struct msghdr
*msg
, struct sock
*sk
,
811 sock_recv_timestamp(msg
, sk
, skb
);
812 sock_recv_drops(msg
, sk
, skb
);
814 EXPORT_SYMBOL_GPL(__sock_recv_ts_and_drops
);
816 static inline int sock_recvmsg_nosec(struct socket
*sock
, struct msghdr
*msg
,
819 return sock
->ops
->recvmsg(sock
, msg
, msg_data_left(msg
), flags
);
822 int sock_recvmsg(struct socket
*sock
, struct msghdr
*msg
, int flags
)
824 int err
= security_socket_recvmsg(sock
, msg
, msg_data_left(msg
), flags
);
826 return err
?: sock_recvmsg_nosec(sock
, msg
, flags
);
828 EXPORT_SYMBOL(sock_recvmsg
);
831 * kernel_recvmsg - Receive a message from a socket (kernel space)
832 * @sock: The socket to receive the message from
833 * @msg: Received message
834 * @vec: Input s/g array for message data
835 * @num: Size of input s/g array
836 * @size: Number of bytes to read
837 * @flags: Message flags (MSG_DONTWAIT, etc...)
839 * On return the msg structure contains the scatter/gather array passed in the
840 * vec argument. The array is modified so that it consists of the unfilled
841 * portion of the original array.
843 * The returned value is the total number of bytes received, or an error.
845 int kernel_recvmsg(struct socket
*sock
, struct msghdr
*msg
,
846 struct kvec
*vec
, size_t num
, size_t size
, int flags
)
848 mm_segment_t oldfs
= get_fs();
851 iov_iter_kvec(&msg
->msg_iter
, READ
| ITER_KVEC
, vec
, num
, size
);
853 result
= sock_recvmsg(sock
, msg
, flags
);
857 EXPORT_SYMBOL(kernel_recvmsg
);
859 static ssize_t
sock_sendpage(struct file
*file
, struct page
*page
,
860 int offset
, size_t size
, loff_t
*ppos
, int more
)
865 sock
= file
->private_data
;
867 flags
= (file
->f_flags
& O_NONBLOCK
) ? MSG_DONTWAIT
: 0;
868 /* more is a combination of MSG_MORE and MSG_SENDPAGE_NOTLAST */
871 return kernel_sendpage(sock
, page
, offset
, size
, flags
);
874 static ssize_t
sock_splice_read(struct file
*file
, loff_t
*ppos
,
875 struct pipe_inode_info
*pipe
, size_t len
,
878 struct socket
*sock
= file
->private_data
;
880 if (unlikely(!sock
->ops
->splice_read
))
883 return sock
->ops
->splice_read(sock
, ppos
, pipe
, len
, flags
);
886 static ssize_t
sock_read_iter(struct kiocb
*iocb
, struct iov_iter
*to
)
888 struct file
*file
= iocb
->ki_filp
;
889 struct socket
*sock
= file
->private_data
;
890 struct msghdr msg
= {.msg_iter
= *to
,
894 if (file
->f_flags
& O_NONBLOCK
|| (iocb
->ki_flags
& IOCB_NOWAIT
))
895 msg
.msg_flags
= MSG_DONTWAIT
;
897 if (iocb
->ki_pos
!= 0)
900 if (!iov_iter_count(to
)) /* Match SYS5 behaviour */
903 res
= sock_recvmsg(sock
, &msg
, msg
.msg_flags
);
908 static ssize_t
sock_write_iter(struct kiocb
*iocb
, struct iov_iter
*from
)
910 struct file
*file
= iocb
->ki_filp
;
911 struct socket
*sock
= file
->private_data
;
912 struct msghdr msg
= {.msg_iter
= *from
,
916 if (iocb
->ki_pos
!= 0)
919 if (file
->f_flags
& O_NONBLOCK
|| (iocb
->ki_flags
& IOCB_NOWAIT
))
920 msg
.msg_flags
= MSG_DONTWAIT
;
922 if (sock
->type
== SOCK_SEQPACKET
)
923 msg
.msg_flags
|= MSG_EOR
;
925 res
= sock_sendmsg(sock
, &msg
);
926 *from
= msg
.msg_iter
;
931 * Atomic setting of ioctl hooks to avoid race
932 * with module unload.
935 static DEFINE_MUTEX(br_ioctl_mutex
);
936 static int (*br_ioctl_hook
) (struct net
*, unsigned int cmd
, void __user
*arg
);
938 void brioctl_set(int (*hook
) (struct net
*, unsigned int, void __user
*))
940 mutex_lock(&br_ioctl_mutex
);
941 br_ioctl_hook
= hook
;
942 mutex_unlock(&br_ioctl_mutex
);
944 EXPORT_SYMBOL(brioctl_set
);
946 static DEFINE_MUTEX(vlan_ioctl_mutex
);
947 static int (*vlan_ioctl_hook
) (struct net
*, void __user
*arg
);
949 void vlan_ioctl_set(int (*hook
) (struct net
*, void __user
*))
951 mutex_lock(&vlan_ioctl_mutex
);
952 vlan_ioctl_hook
= hook
;
953 mutex_unlock(&vlan_ioctl_mutex
);
955 EXPORT_SYMBOL(vlan_ioctl_set
);
957 static DEFINE_MUTEX(dlci_ioctl_mutex
);
958 static int (*dlci_ioctl_hook
) (unsigned int, void __user
*);
960 void dlci_ioctl_set(int (*hook
) (unsigned int, void __user
*))
962 mutex_lock(&dlci_ioctl_mutex
);
963 dlci_ioctl_hook
= hook
;
964 mutex_unlock(&dlci_ioctl_mutex
);
966 EXPORT_SYMBOL(dlci_ioctl_set
);
968 static long sock_do_ioctl(struct net
*net
, struct socket
*sock
,
969 unsigned int cmd
, unsigned long arg
)
972 void __user
*argp
= (void __user
*)arg
;
974 err
= sock
->ops
->ioctl(sock
, cmd
, arg
);
977 * If this ioctl is unknown try to hand it down
980 if (err
== -ENOIOCTLCMD
)
981 err
= dev_ioctl(net
, cmd
, argp
);
987 * With an ioctl, arg may well be a user mode pointer, but we don't know
988 * what to do with it - that's up to the protocol still.
991 static struct ns_common
*get_net_ns(struct ns_common
*ns
)
993 return &get_net(container_of(ns
, struct net
, ns
))->ns
;
996 static long sock_ioctl(struct file
*file
, unsigned cmd
, unsigned long arg
)
1000 void __user
*argp
= (void __user
*)arg
;
1004 sock
= file
->private_data
;
1007 if (cmd
>= SIOCDEVPRIVATE
&& cmd
<= (SIOCDEVPRIVATE
+ 15)) {
1008 err
= dev_ioctl(net
, cmd
, argp
);
1010 #ifdef CONFIG_WEXT_CORE
1011 if (cmd
>= SIOCIWFIRST
&& cmd
<= SIOCIWLAST
) {
1012 err
= dev_ioctl(net
, cmd
, argp
);
1019 if (get_user(pid
, (int __user
*)argp
))
1021 err
= f_setown(sock
->file
, pid
, 1);
1025 err
= put_user(f_getown(sock
->file
),
1026 (int __user
*)argp
);
1034 request_module("bridge");
1036 mutex_lock(&br_ioctl_mutex
);
1038 err
= br_ioctl_hook(net
, cmd
, argp
);
1039 mutex_unlock(&br_ioctl_mutex
);
1044 if (!vlan_ioctl_hook
)
1045 request_module("8021q");
1047 mutex_lock(&vlan_ioctl_mutex
);
1048 if (vlan_ioctl_hook
)
1049 err
= vlan_ioctl_hook(net
, argp
);
1050 mutex_unlock(&vlan_ioctl_mutex
);
1055 if (!dlci_ioctl_hook
)
1056 request_module("dlci");
1058 mutex_lock(&dlci_ioctl_mutex
);
1059 if (dlci_ioctl_hook
)
1060 err
= dlci_ioctl_hook(cmd
, argp
);
1061 mutex_unlock(&dlci_ioctl_mutex
);
1065 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
1068 err
= open_related_ns(&net
->ns
, get_net_ns
);
1071 err
= sock_do_ioctl(net
, sock
, cmd
, arg
);
1077 int sock_create_lite(int family
, int type
, int protocol
, struct socket
**res
)
1080 struct socket
*sock
= NULL
;
1082 err
= security_socket_create(family
, type
, protocol
, 1);
1086 sock
= sock_alloc();
1093 err
= security_socket_post_create(sock
, family
, type
, protocol
, 1);
1105 EXPORT_SYMBOL(sock_create_lite
);
1107 /* No kernel lock held - perfect */
1108 static unsigned int sock_poll(struct file
*file
, poll_table
*wait
)
1110 unsigned int busy_flag
= 0;
1111 struct socket
*sock
;
1114 * We can't return errors to poll, so it's either yes or no.
1116 sock
= file
->private_data
;
1118 if (sk_can_busy_loop(sock
->sk
)) {
1119 /* this socket can poll_ll so tell the system call */
1120 busy_flag
= POLL_BUSY_LOOP
;
1122 /* once, only if requested by syscall */
1123 if (wait
&& (wait
->_key
& POLL_BUSY_LOOP
))
1124 sk_busy_loop(sock
->sk
, 1);
1127 return busy_flag
| sock
->ops
->poll(file
, sock
, wait
);
1130 static int sock_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1132 struct socket
*sock
= file
->private_data
;
1134 return sock
->ops
->mmap(file
, sock
, vma
);
1137 static int sock_close(struct inode
*inode
, struct file
*filp
)
1139 __sock_release(SOCKET_I(inode
), inode
);
1144 * Update the socket async list
1146 * Fasync_list locking strategy.
1148 * 1. fasync_list is modified only under process context socket lock
1149 * i.e. under semaphore.
1150 * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
1151 * or under socket lock
1154 static int sock_fasync(int fd
, struct file
*filp
, int on
)
1156 struct socket
*sock
= filp
->private_data
;
1157 struct sock
*sk
= sock
->sk
;
1158 struct socket_wq
*wq
;
1164 wq
= rcu_dereference_protected(sock
->wq
, lockdep_sock_is_held(sk
));
1165 fasync_helper(fd
, filp
, on
, &wq
->fasync_list
);
1167 if (!wq
->fasync_list
)
1168 sock_reset_flag(sk
, SOCK_FASYNC
);
1170 sock_set_flag(sk
, SOCK_FASYNC
);
1176 /* This function may be called only under rcu_lock */
1178 int sock_wake_async(struct socket_wq
*wq
, int how
, int band
)
1180 if (!wq
|| !wq
->fasync_list
)
1184 case SOCK_WAKE_WAITD
:
1185 if (test_bit(SOCKWQ_ASYNC_WAITDATA
, &wq
->flags
))
1188 case SOCK_WAKE_SPACE
:
1189 if (!test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE
, &wq
->flags
))
1194 kill_fasync(&wq
->fasync_list
, SIGIO
, band
);
1197 kill_fasync(&wq
->fasync_list
, SIGURG
, band
);
1202 EXPORT_SYMBOL(sock_wake_async
);
1204 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
) {
1225 pr_info_once("%s uses obsolete (PF_INET,SOCK_PACKET)\n",
1230 err
= security_socket_create(family
, type
, protocol
, kern
);
1235 * Allocate the socket and allow the family to set things up. if
1236 * the protocol is 0, the family is instructed to select an appropriate
1239 sock
= sock_alloc();
1241 net_warn_ratelimited("socket: no more sockets\n");
1242 return -ENFILE
; /* Not exactly a match, but its the
1243 closest posix thing */
1248 #ifdef CONFIG_MODULES
1249 /* Attempt to load a protocol module if the find failed.
1251 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
1252 * requested real, full-featured networking support upon configuration.
1253 * Otherwise module support will break!
1255 if (rcu_access_pointer(net_families
[family
]) == NULL
)
1256 request_module("net-pf-%d", family
);
1260 pf
= rcu_dereference(net_families
[family
]);
1261 err
= -EAFNOSUPPORT
;
1266 * We will call the ->create function, that possibly is in a loadable
1267 * module, so we have to bump that loadable module refcnt first.
1269 if (!try_module_get(pf
->owner
))
1272 /* Now protected by module ref count */
1275 err
= pf
->create(net
, sock
, protocol
, kern
);
1277 goto out_module_put
;
1280 * Now to bump the refcnt of the [loadable] module that owns this
1281 * socket at sock_release time we decrement its refcnt.
1283 if (!try_module_get(sock
->ops
->owner
))
1284 goto out_module_busy
;
1287 * Now that we're done with the ->create function, the [loadable]
1288 * module can have its refcnt decremented
1290 module_put(pf
->owner
);
1291 err
= security_socket_post_create(sock
, family
, type
, protocol
, kern
);
1293 goto out_sock_release
;
1299 err
= -EAFNOSUPPORT
;
1302 module_put(pf
->owner
);
1309 goto out_sock_release
;
1311 EXPORT_SYMBOL(__sock_create
);
1313 int sock_create(int family
, int type
, int protocol
, struct socket
**res
)
1315 return __sock_create(current
->nsproxy
->net_ns
, family
, type
, protocol
, res
, 0);
1317 EXPORT_SYMBOL(sock_create
);
1319 int sock_create_kern(struct net
*net
, int family
, int type
, int protocol
, struct socket
**res
)
1321 return __sock_create(net
, family
, type
, protocol
, res
, 1);
1323 EXPORT_SYMBOL(sock_create_kern
);
1325 SYSCALL_DEFINE3(socket
, int, family
, int, type
, int, protocol
)
1328 struct socket
*sock
;
1331 /* Check the SOCK_* constants for consistency. */
1332 BUILD_BUG_ON(SOCK_CLOEXEC
!= O_CLOEXEC
);
1333 BUILD_BUG_ON((SOCK_MAX
| SOCK_TYPE_MASK
) != SOCK_TYPE_MASK
);
1334 BUILD_BUG_ON(SOCK_CLOEXEC
& SOCK_TYPE_MASK
);
1335 BUILD_BUG_ON(SOCK_NONBLOCK
& SOCK_TYPE_MASK
);
1337 flags
= type
& ~SOCK_TYPE_MASK
;
1338 if (flags
& ~(SOCK_CLOEXEC
| SOCK_NONBLOCK
))
1340 type
&= SOCK_TYPE_MASK
;
1342 if (SOCK_NONBLOCK
!= O_NONBLOCK
&& (flags
& SOCK_NONBLOCK
))
1343 flags
= (flags
& ~SOCK_NONBLOCK
) | O_NONBLOCK
;
1345 retval
= sock_create(family
, type
, protocol
, &sock
);
1349 retval
= sock_map_fd(sock
, flags
& (O_CLOEXEC
| O_NONBLOCK
));
1354 /* It may be already another descriptor 8) Not kernel problem. */
1363 * Create a pair of connected sockets.
1366 SYSCALL_DEFINE4(socketpair
, int, family
, int, type
, int, protocol
,
1367 int __user
*, usockvec
)
1369 struct socket
*sock1
, *sock2
;
1371 struct file
*newfile1
, *newfile2
;
1374 flags
= type
& ~SOCK_TYPE_MASK
;
1375 if (flags
& ~(SOCK_CLOEXEC
| SOCK_NONBLOCK
))
1377 type
&= SOCK_TYPE_MASK
;
1379 if (SOCK_NONBLOCK
!= O_NONBLOCK
&& (flags
& SOCK_NONBLOCK
))
1380 flags
= (flags
& ~SOCK_NONBLOCK
) | O_NONBLOCK
;
1383 * Obtain the first socket and check if the underlying protocol
1384 * supports the socketpair call.
1387 err
= sock_create(family
, type
, protocol
, &sock1
);
1391 err
= sock_create(family
, type
, protocol
, &sock2
);
1395 err
= sock1
->ops
->socketpair(sock1
, sock2
);
1397 goto out_release_both
;
1399 fd1
= get_unused_fd_flags(flags
);
1400 if (unlikely(fd1
< 0)) {
1402 goto out_release_both
;
1405 fd2
= get_unused_fd_flags(flags
);
1406 if (unlikely(fd2
< 0)) {
1408 goto out_put_unused_1
;
1411 newfile1
= sock_alloc_file(sock1
, flags
, NULL
);
1412 if (IS_ERR(newfile1
)) {
1413 err
= PTR_ERR(newfile1
);
1414 goto out_put_unused_both
;
1417 newfile2
= sock_alloc_file(sock2
, flags
, NULL
);
1418 if (IS_ERR(newfile2
)) {
1419 err
= PTR_ERR(newfile2
);
1423 err
= put_user(fd1
, &usockvec
[0]);
1427 err
= put_user(fd2
, &usockvec
[1]);
1431 audit_fd_pair(fd1
, fd2
);
1433 fd_install(fd1
, newfile1
);
1434 fd_install(fd2
, newfile2
);
1435 /* fd1 and fd2 may be already another descriptors.
1436 * Not kernel problem.
1452 sock_release(sock2
);
1455 out_put_unused_both
:
1460 sock_release(sock2
);
1462 sock_release(sock1
);
1468 * Bind a name to a socket. Nothing much to do here since it's
1469 * the protocol's responsibility to handle the local address.
1471 * We move the socket address to kernel space before we call
1472 * the protocol layer (having also checked the address is ok).
1475 SYSCALL_DEFINE3(bind
, int, fd
, struct sockaddr __user
*, umyaddr
, int, addrlen
)
1477 struct socket
*sock
;
1478 struct sockaddr_storage address
;
1479 int err
, fput_needed
;
1481 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1483 err
= move_addr_to_kernel(umyaddr
, addrlen
, &address
);
1485 err
= security_socket_bind(sock
,
1486 (struct sockaddr
*)&address
,
1489 err
= sock
->ops
->bind(sock
,
1493 fput_light(sock
->file
, fput_needed
);
1499 * Perform a listen. Basically, we allow the protocol to do anything
1500 * necessary for a listen, and if that works, we mark the socket as
1501 * ready for listening.
1504 SYSCALL_DEFINE2(listen
, int, fd
, int, backlog
)
1506 struct socket
*sock
;
1507 int err
, fput_needed
;
1510 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1512 somaxconn
= sock_net(sock
->sk
)->core
.sysctl_somaxconn
;
1513 if ((unsigned int)backlog
> somaxconn
)
1514 backlog
= somaxconn
;
1516 err
= security_socket_listen(sock
, backlog
);
1518 err
= sock
->ops
->listen(sock
, backlog
);
1520 fput_light(sock
->file
, fput_needed
);
1526 * For accept, we attempt to create a new socket, set up the link
1527 * with the client, wake up the client, then return the new
1528 * connected fd. We collect the address of the connector in kernel
1529 * space and move it to user at the very end. This is unclean because
1530 * we open the socket then return an error.
1532 * 1003.1g adds the ability to recvmsg() to query connection pending
1533 * status to recvmsg. We need to add that support in a way thats
1534 * clean when we restucture accept also.
1537 SYSCALL_DEFINE4(accept4
, int, fd
, struct sockaddr __user
*, upeer_sockaddr
,
1538 int __user
*, upeer_addrlen
, int, flags
)
1540 struct socket
*sock
, *newsock
;
1541 struct file
*newfile
;
1542 int err
, len
, newfd
, fput_needed
;
1543 struct sockaddr_storage address
;
1545 if (flags
& ~(SOCK_CLOEXEC
| SOCK_NONBLOCK
))
1548 if (SOCK_NONBLOCK
!= O_NONBLOCK
&& (flags
& SOCK_NONBLOCK
))
1549 flags
= (flags
& ~SOCK_NONBLOCK
) | O_NONBLOCK
;
1551 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1556 newsock
= sock_alloc();
1560 newsock
->type
= sock
->type
;
1561 newsock
->ops
= sock
->ops
;
1564 * We don't need try_module_get here, as the listening socket (sock)
1565 * has the protocol module (sock->ops->owner) held.
1567 __module_get(newsock
->ops
->owner
);
1569 newfd
= get_unused_fd_flags(flags
);
1570 if (unlikely(newfd
< 0)) {
1572 sock_release(newsock
);
1575 newfile
= sock_alloc_file(newsock
, flags
, sock
->sk
->sk_prot_creator
->name
);
1576 if (IS_ERR(newfile
)) {
1577 err
= PTR_ERR(newfile
);
1578 put_unused_fd(newfd
);
1579 sock_release(newsock
);
1583 err
= security_socket_accept(sock
, newsock
);
1587 err
= sock
->ops
->accept(sock
, newsock
, sock
->file
->f_flags
, false);
1591 if (upeer_sockaddr
) {
1592 if (newsock
->ops
->getname(newsock
, (struct sockaddr
*)&address
,
1594 err
= -ECONNABORTED
;
1597 err
= move_addr_to_user(&address
,
1598 len
, upeer_sockaddr
, upeer_addrlen
);
1603 /* File flags are not inherited via accept() unlike another OSes. */
1605 fd_install(newfd
, newfile
);
1609 fput_light(sock
->file
, fput_needed
);
1614 put_unused_fd(newfd
);
1618 SYSCALL_DEFINE3(accept
, int, fd
, struct sockaddr __user
*, upeer_sockaddr
,
1619 int __user
*, upeer_addrlen
)
1621 return sys_accept4(fd
, upeer_sockaddr
, upeer_addrlen
, 0);
1625 * Attempt to connect to a socket with the server address. The address
1626 * is in user space so we verify it is OK and move it to kernel space.
1628 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
1631 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
1632 * other SEQPACKET protocols that take time to connect() as it doesn't
1633 * include the -EINPROGRESS status for such sockets.
1636 SYSCALL_DEFINE3(connect
, int, fd
, struct sockaddr __user
*, uservaddr
,
1639 struct socket
*sock
;
1640 struct sockaddr_storage address
;
1641 int err
, fput_needed
;
1643 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1646 err
= move_addr_to_kernel(uservaddr
, addrlen
, &address
);
1651 security_socket_connect(sock
, (struct sockaddr
*)&address
, addrlen
);
1655 err
= sock
->ops
->connect(sock
, (struct sockaddr
*)&address
, addrlen
,
1656 sock
->file
->f_flags
);
1658 fput_light(sock
->file
, fput_needed
);
1664 * Get the local address ('name') of a socket object. Move the obtained
1665 * name to user space.
1668 SYSCALL_DEFINE3(getsockname
, int, fd
, struct sockaddr __user
*, usockaddr
,
1669 int __user
*, usockaddr_len
)
1671 struct socket
*sock
;
1672 struct sockaddr_storage address
;
1673 int len
, err
, fput_needed
;
1675 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1679 err
= security_socket_getsockname(sock
);
1683 err
= sock
->ops
->getname(sock
, (struct sockaddr
*)&address
, &len
, 0);
1686 err
= move_addr_to_user(&address
, len
, usockaddr
, usockaddr_len
);
1689 fput_light(sock
->file
, fput_needed
);
1695 * Get the remote address ('name') of a socket object. Move the obtained
1696 * name to user space.
1699 SYSCALL_DEFINE3(getpeername
, int, fd
, struct sockaddr __user
*, usockaddr
,
1700 int __user
*, usockaddr_len
)
1702 struct socket
*sock
;
1703 struct sockaddr_storage address
;
1704 int len
, err
, fput_needed
;
1706 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1708 err
= security_socket_getpeername(sock
);
1710 fput_light(sock
->file
, fput_needed
);
1715 sock
->ops
->getname(sock
, (struct sockaddr
*)&address
, &len
,
1718 err
= move_addr_to_user(&address
, len
, usockaddr
,
1720 fput_light(sock
->file
, fput_needed
);
1726 * Send a datagram to a given address. We move the address into kernel
1727 * space and check the user space data area is readable before invoking
1731 SYSCALL_DEFINE6(sendto
, int, fd
, void __user
*, buff
, size_t, len
,
1732 unsigned int, flags
, struct sockaddr __user
*, addr
,
1735 struct socket
*sock
;
1736 struct sockaddr_storage address
;
1742 err
= import_single_range(WRITE
, buff
, len
, &iov
, &msg
.msg_iter
);
1745 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1749 msg
.msg_name
= NULL
;
1750 msg
.msg_control
= NULL
;
1751 msg
.msg_controllen
= 0;
1752 msg
.msg_namelen
= 0;
1754 err
= move_addr_to_kernel(addr
, addr_len
, &address
);
1757 msg
.msg_name
= (struct sockaddr
*)&address
;
1758 msg
.msg_namelen
= addr_len
;
1760 if (sock
->file
->f_flags
& O_NONBLOCK
)
1761 flags
|= MSG_DONTWAIT
;
1762 msg
.msg_flags
= flags
;
1763 err
= sock_sendmsg(sock
, &msg
);
1766 fput_light(sock
->file
, fput_needed
);
1772 * Send a datagram down a socket.
1775 SYSCALL_DEFINE4(send
, int, fd
, void __user
*, buff
, size_t, len
,
1776 unsigned int, flags
)
1778 return sys_sendto(fd
, buff
, len
, flags
, NULL
, 0);
1782 * Receive a frame from the socket and optionally record the address of the
1783 * sender. We verify the buffers are writable and if needed move the
1784 * sender address from kernel to user space.
1787 SYSCALL_DEFINE6(recvfrom
, int, fd
, void __user
*, ubuf
, size_t, size
,
1788 unsigned int, flags
, struct sockaddr __user
*, addr
,
1789 int __user
*, addr_len
)
1791 struct socket
*sock
;
1794 struct sockaddr_storage address
;
1798 err
= import_single_range(READ
, ubuf
, size
, &iov
, &msg
.msg_iter
);
1801 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1805 msg
.msg_control
= NULL
;
1806 msg
.msg_controllen
= 0;
1807 /* Save some cycles and don't copy the address if not needed */
1808 msg
.msg_name
= addr
? (struct sockaddr
*)&address
: NULL
;
1809 /* We assume all kernel code knows the size of sockaddr_storage */
1810 msg
.msg_namelen
= 0;
1811 msg
.msg_iocb
= NULL
;
1813 if (sock
->file
->f_flags
& O_NONBLOCK
)
1814 flags
|= MSG_DONTWAIT
;
1815 err
= sock_recvmsg(sock
, &msg
, flags
);
1817 if (err
>= 0 && addr
!= NULL
) {
1818 err2
= move_addr_to_user(&address
,
1819 msg
.msg_namelen
, addr
, addr_len
);
1824 fput_light(sock
->file
, fput_needed
);
1830 * Receive a datagram from a socket.
1833 SYSCALL_DEFINE4(recv
, int, fd
, void __user
*, ubuf
, size_t, size
,
1834 unsigned int, flags
)
1836 return sys_recvfrom(fd
, ubuf
, size
, flags
, NULL
, NULL
);
1840 * Set a socket option. Because we don't know the option lengths we have
1841 * to pass the user mode parameter for the protocols to sort out.
1844 SYSCALL_DEFINE5(setsockopt
, int, fd
, int, level
, int, optname
,
1845 char __user
*, optval
, int, optlen
)
1847 int err
, fput_needed
;
1848 struct socket
*sock
;
1853 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1855 err
= security_socket_setsockopt(sock
, level
, optname
);
1859 if (level
== SOL_SOCKET
)
1861 sock_setsockopt(sock
, level
, optname
, optval
,
1865 sock
->ops
->setsockopt(sock
, level
, optname
, optval
,
1868 fput_light(sock
->file
, fput_needed
);
1874 * Get a socket option. Because we don't know the option lengths we have
1875 * to pass a user mode parameter for the protocols to sort out.
1878 SYSCALL_DEFINE5(getsockopt
, int, fd
, int, level
, int, optname
,
1879 char __user
*, optval
, int __user
*, optlen
)
1881 int err
, fput_needed
;
1882 struct socket
*sock
;
1884 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1886 err
= security_socket_getsockopt(sock
, level
, optname
);
1890 if (level
== SOL_SOCKET
)
1892 sock_getsockopt(sock
, level
, optname
, optval
,
1896 sock
->ops
->getsockopt(sock
, level
, optname
, optval
,
1899 fput_light(sock
->file
, fput_needed
);
1905 * Shutdown a socket.
1908 SYSCALL_DEFINE2(shutdown
, int, fd
, int, how
)
1910 int err
, fput_needed
;
1911 struct socket
*sock
;
1913 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1915 err
= security_socket_shutdown(sock
, how
);
1917 err
= sock
->ops
->shutdown(sock
, how
);
1918 fput_light(sock
->file
, fput_needed
);
1923 /* A couple of helpful macros for getting the address of the 32/64 bit
1924 * fields which are the same type (int / unsigned) on our platforms.
1926 #define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
1927 #define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
1928 #define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
1930 struct used_address
{
1931 struct sockaddr_storage name
;
1932 unsigned int name_len
;
1935 static int copy_msghdr_from_user(struct msghdr
*kmsg
,
1936 struct user_msghdr __user
*umsg
,
1937 struct sockaddr __user
**save_addr
,
1940 struct user_msghdr msg
;
1943 if (copy_from_user(&msg
, umsg
, sizeof(*umsg
)))
1946 kmsg
->msg_control
= (void __force
*)msg
.msg_control
;
1947 kmsg
->msg_controllen
= msg
.msg_controllen
;
1948 kmsg
->msg_flags
= msg
.msg_flags
;
1950 kmsg
->msg_namelen
= msg
.msg_namelen
;
1952 kmsg
->msg_namelen
= 0;
1954 if (kmsg
->msg_namelen
< 0)
1957 if (kmsg
->msg_namelen
> sizeof(struct sockaddr_storage
))
1958 kmsg
->msg_namelen
= sizeof(struct sockaddr_storage
);
1961 *save_addr
= msg
.msg_name
;
1963 if (msg
.msg_name
&& kmsg
->msg_namelen
) {
1965 err
= move_addr_to_kernel(msg
.msg_name
,
1972 kmsg
->msg_name
= NULL
;
1973 kmsg
->msg_namelen
= 0;
1976 if (msg
.msg_iovlen
> UIO_MAXIOV
)
1979 kmsg
->msg_iocb
= NULL
;
1981 return import_iovec(save_addr
? READ
: WRITE
,
1982 msg
.msg_iov
, msg
.msg_iovlen
,
1983 UIO_FASTIOV
, iov
, &kmsg
->msg_iter
);
1986 static int ___sys_sendmsg(struct socket
*sock
, struct user_msghdr __user
*msg
,
1987 struct msghdr
*msg_sys
, unsigned int flags
,
1988 struct used_address
*used_address
,
1989 unsigned int allowed_msghdr_flags
)
1991 struct compat_msghdr __user
*msg_compat
=
1992 (struct compat_msghdr __user
*)msg
;
1993 struct sockaddr_storage address
;
1994 struct iovec iovstack
[UIO_FASTIOV
], *iov
= iovstack
;
1995 unsigned char ctl
[sizeof(struct cmsghdr
) + 20]
1996 __aligned(sizeof(__kernel_size_t
));
1997 /* 20 is size of ipv6_pktinfo */
1998 unsigned char *ctl_buf
= ctl
;
2002 msg_sys
->msg_name
= &address
;
2004 if (MSG_CMSG_COMPAT
& flags
)
2005 err
= get_compat_msghdr(msg_sys
, msg_compat
, NULL
, &iov
);
2007 err
= copy_msghdr_from_user(msg_sys
, msg
, NULL
, &iov
);
2013 if (msg_sys
->msg_controllen
> INT_MAX
)
2015 flags
|= (msg_sys
->msg_flags
& allowed_msghdr_flags
);
2016 ctl_len
= msg_sys
->msg_controllen
;
2017 if ((MSG_CMSG_COMPAT
& flags
) && ctl_len
) {
2019 cmsghdr_from_user_compat_to_kern(msg_sys
, sock
->sk
, ctl
,
2023 ctl_buf
= msg_sys
->msg_control
;
2024 ctl_len
= msg_sys
->msg_controllen
;
2025 } else if (ctl_len
) {
2026 BUILD_BUG_ON(sizeof(struct cmsghdr
) !=
2027 CMSG_ALIGN(sizeof(struct cmsghdr
)));
2028 if (ctl_len
> sizeof(ctl
)) {
2029 ctl_buf
= sock_kmalloc(sock
->sk
, ctl_len
, GFP_KERNEL
);
2030 if (ctl_buf
== NULL
)
2035 * Careful! Before this, msg_sys->msg_control contains a user pointer.
2036 * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
2037 * checking falls down on this.
2039 if (copy_from_user(ctl_buf
,
2040 (void __user __force
*)msg_sys
->msg_control
,
2043 msg_sys
->msg_control
= ctl_buf
;
2045 msg_sys
->msg_flags
= flags
;
2047 if (sock
->file
->f_flags
& O_NONBLOCK
)
2048 msg_sys
->msg_flags
|= MSG_DONTWAIT
;
2050 * If this is sendmmsg() and current destination address is same as
2051 * previously succeeded address, omit asking LSM's decision.
2052 * used_address->name_len is initialized to UINT_MAX so that the first
2053 * destination address never matches.
2055 if (used_address
&& msg_sys
->msg_name
&&
2056 used_address
->name_len
== msg_sys
->msg_namelen
&&
2057 !memcmp(&used_address
->name
, msg_sys
->msg_name
,
2058 used_address
->name_len
)) {
2059 err
= sock_sendmsg_nosec(sock
, msg_sys
);
2062 err
= sock_sendmsg(sock
, msg_sys
);
2064 * If this is sendmmsg() and sending to current destination address was
2065 * successful, remember it.
2067 if (used_address
&& err
>= 0) {
2068 used_address
->name_len
= msg_sys
->msg_namelen
;
2069 if (msg_sys
->msg_name
)
2070 memcpy(&used_address
->name
, msg_sys
->msg_name
,
2071 used_address
->name_len
);
2076 sock_kfree_s(sock
->sk
, ctl_buf
, ctl_len
);
2083 * BSD sendmsg interface
2086 long __sys_sendmsg(int fd
, struct user_msghdr __user
*msg
, unsigned flags
)
2088 int fput_needed
, err
;
2089 struct msghdr msg_sys
;
2090 struct socket
*sock
;
2092 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
2096 err
= ___sys_sendmsg(sock
, msg
, &msg_sys
, flags
, NULL
, 0);
2098 fput_light(sock
->file
, fput_needed
);
2103 SYSCALL_DEFINE3(sendmsg
, int, fd
, struct user_msghdr __user
*, msg
, unsigned int, flags
)
2105 if (flags
& MSG_CMSG_COMPAT
)
2107 return __sys_sendmsg(fd
, msg
, flags
);
2111 * Linux sendmmsg interface
2114 int __sys_sendmmsg(int fd
, struct mmsghdr __user
*mmsg
, unsigned int vlen
,
2117 int fput_needed
, err
, datagrams
;
2118 struct socket
*sock
;
2119 struct mmsghdr __user
*entry
;
2120 struct compat_mmsghdr __user
*compat_entry
;
2121 struct msghdr msg_sys
;
2122 struct used_address used_address
;
2123 unsigned int oflags
= flags
;
2125 if (vlen
> UIO_MAXIOV
)
2130 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
2134 used_address
.name_len
= UINT_MAX
;
2136 compat_entry
= (struct compat_mmsghdr __user
*)mmsg
;
2140 while (datagrams
< vlen
) {
2141 if (datagrams
== vlen
- 1)
2144 if (MSG_CMSG_COMPAT
& flags
) {
2145 err
= ___sys_sendmsg(sock
, (struct user_msghdr __user
*)compat_entry
,
2146 &msg_sys
, flags
, &used_address
, MSG_EOR
);
2149 err
= __put_user(err
, &compat_entry
->msg_len
);
2152 err
= ___sys_sendmsg(sock
,
2153 (struct user_msghdr __user
*)entry
,
2154 &msg_sys
, flags
, &used_address
, MSG_EOR
);
2157 err
= put_user(err
, &entry
->msg_len
);
2164 if (msg_data_left(&msg_sys
))
2169 fput_light(sock
->file
, fput_needed
);
2171 /* We only return an error if no datagrams were able to be sent */
2178 SYSCALL_DEFINE4(sendmmsg
, int, fd
, struct mmsghdr __user
*, mmsg
,
2179 unsigned int, vlen
, unsigned int, flags
)
2181 if (flags
& MSG_CMSG_COMPAT
)
2183 return __sys_sendmmsg(fd
, mmsg
, vlen
, flags
);
2186 static int ___sys_recvmsg(struct socket
*sock
, struct user_msghdr __user
*msg
,
2187 struct msghdr
*msg_sys
, unsigned int flags
, int nosec
)
2189 struct compat_msghdr __user
*msg_compat
=
2190 (struct compat_msghdr __user
*)msg
;
2191 struct iovec iovstack
[UIO_FASTIOV
];
2192 struct iovec
*iov
= iovstack
;
2193 unsigned long cmsg_ptr
;
2197 /* kernel mode address */
2198 struct sockaddr_storage addr
;
2200 /* user mode address pointers */
2201 struct sockaddr __user
*uaddr
;
2202 int __user
*uaddr_len
= COMPAT_NAMELEN(msg
);
2204 msg_sys
->msg_name
= &addr
;
2206 if (MSG_CMSG_COMPAT
& flags
)
2207 err
= get_compat_msghdr(msg_sys
, msg_compat
, &uaddr
, &iov
);
2209 err
= copy_msghdr_from_user(msg_sys
, msg
, &uaddr
, &iov
);
2213 cmsg_ptr
= (unsigned long)msg_sys
->msg_control
;
2214 msg_sys
->msg_flags
= flags
& (MSG_CMSG_CLOEXEC
|MSG_CMSG_COMPAT
);
2216 /* We assume all kernel code knows the size of sockaddr_storage */
2217 msg_sys
->msg_namelen
= 0;
2219 if (sock
->file
->f_flags
& O_NONBLOCK
)
2220 flags
|= MSG_DONTWAIT
;
2221 err
= (nosec
? sock_recvmsg_nosec
: sock_recvmsg
)(sock
, msg_sys
, flags
);
2226 if (uaddr
!= NULL
) {
2227 err
= move_addr_to_user(&addr
,
2228 msg_sys
->msg_namelen
, uaddr
,
2233 err
= __put_user((msg_sys
->msg_flags
& ~MSG_CMSG_COMPAT
),
2237 if (MSG_CMSG_COMPAT
& flags
)
2238 err
= __put_user((unsigned long)msg_sys
->msg_control
- cmsg_ptr
,
2239 &msg_compat
->msg_controllen
);
2241 err
= __put_user((unsigned long)msg_sys
->msg_control
- cmsg_ptr
,
2242 &msg
->msg_controllen
);
2253 * BSD recvmsg interface
2256 long __sys_recvmsg(int fd
, struct user_msghdr __user
*msg
, unsigned flags
)
2258 int fput_needed
, err
;
2259 struct msghdr msg_sys
;
2260 struct socket
*sock
;
2262 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
2266 err
= ___sys_recvmsg(sock
, msg
, &msg_sys
, flags
, 0);
2268 fput_light(sock
->file
, fput_needed
);
2273 SYSCALL_DEFINE3(recvmsg
, int, fd
, struct user_msghdr __user
*, msg
,
2274 unsigned int, flags
)
2276 if (flags
& MSG_CMSG_COMPAT
)
2278 return __sys_recvmsg(fd
, msg
, flags
);
2282 * Linux recvmmsg interface
2285 int __sys_recvmmsg(int fd
, struct mmsghdr __user
*mmsg
, unsigned int vlen
,
2286 unsigned int flags
, struct timespec
*timeout
)
2288 int fput_needed
, err
, datagrams
;
2289 struct socket
*sock
;
2290 struct mmsghdr __user
*entry
;
2291 struct compat_mmsghdr __user
*compat_entry
;
2292 struct msghdr msg_sys
;
2293 struct timespec64 end_time
;
2294 struct timespec64 timeout64
;
2297 poll_select_set_timeout(&end_time
, timeout
->tv_sec
,
2303 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
2307 err
= sock_error(sock
->sk
);
2314 compat_entry
= (struct compat_mmsghdr __user
*)mmsg
;
2316 while (datagrams
< vlen
) {
2318 * No need to ask LSM for more than the first datagram.
2320 if (MSG_CMSG_COMPAT
& flags
) {
2321 err
= ___sys_recvmsg(sock
, (struct user_msghdr __user
*)compat_entry
,
2322 &msg_sys
, flags
& ~MSG_WAITFORONE
,
2326 err
= __put_user(err
, &compat_entry
->msg_len
);
2329 err
= ___sys_recvmsg(sock
,
2330 (struct user_msghdr __user
*)entry
,
2331 &msg_sys
, flags
& ~MSG_WAITFORONE
,
2335 err
= put_user(err
, &entry
->msg_len
);
2343 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
2344 if (flags
& MSG_WAITFORONE
)
2345 flags
|= MSG_DONTWAIT
;
2348 ktime_get_ts64(&timeout64
);
2349 *timeout
= timespec64_to_timespec(
2350 timespec64_sub(end_time
, timeout64
));
2351 if (timeout
->tv_sec
< 0) {
2352 timeout
->tv_sec
= timeout
->tv_nsec
= 0;
2356 /* Timeout, return less than vlen datagrams */
2357 if (timeout
->tv_nsec
== 0 && timeout
->tv_sec
== 0)
2361 /* Out of band data, return right away */
2362 if (msg_sys
.msg_flags
& MSG_OOB
)
2370 if (datagrams
== 0) {
2376 * We may return less entries than requested (vlen) if the
2377 * sock is non block and there aren't enough datagrams...
2379 if (err
!= -EAGAIN
) {
2381 * ... or if recvmsg returns an error after we
2382 * received some datagrams, where we record the
2383 * error to return on the next call or if the
2384 * app asks about it using getsockopt(SO_ERROR).
2386 sock
->sk
->sk_err
= -err
;
2389 fput_light(sock
->file
, fput_needed
);
2394 SYSCALL_DEFINE5(recvmmsg
, int, fd
, struct mmsghdr __user
*, mmsg
,
2395 unsigned int, vlen
, unsigned int, flags
,
2396 struct timespec __user
*, timeout
)
2399 struct timespec timeout_sys
;
2401 if (flags
& MSG_CMSG_COMPAT
)
2405 return __sys_recvmmsg(fd
, mmsg
, vlen
, flags
, NULL
);
2407 if (copy_from_user(&timeout_sys
, timeout
, sizeof(timeout_sys
)))
2410 datagrams
= __sys_recvmmsg(fd
, mmsg
, vlen
, flags
, &timeout_sys
);
2412 if (datagrams
> 0 &&
2413 copy_to_user(timeout
, &timeout_sys
, sizeof(timeout_sys
)))
2414 datagrams
= -EFAULT
;
2419 #ifdef __ARCH_WANT_SYS_SOCKETCALL
2420 /* Argument list sizes for sys_socketcall */
2421 #define AL(x) ((x) * sizeof(unsigned long))
2422 static const unsigned char nargs
[21] = {
2423 AL(0), AL(3), AL(3), AL(3), AL(2), AL(3),
2424 AL(3), AL(3), AL(4), AL(4), AL(4), AL(6),
2425 AL(6), AL(2), AL(5), AL(5), AL(3), AL(3),
2432 * System call vectors.
2434 * Argument checking cleaned up. Saved 20% in size.
2435 * This function doesn't need to set the kernel lock because
2436 * it is set by the callees.
2439 SYSCALL_DEFINE2(socketcall
, int, call
, unsigned long __user
*, args
)
2441 unsigned long a
[AUDITSC_ARGS
];
2442 unsigned long a0
, a1
;
2446 if (call
< 1 || call
> SYS_SENDMMSG
)
2448 call
= array_index_nospec(call
, SYS_SENDMMSG
+ 1);
2451 if (len
> sizeof(a
))
2454 /* copy_from_user should be SMP safe. */
2455 if (copy_from_user(a
, args
, len
))
2458 err
= audit_socketcall(nargs
[call
] / sizeof(unsigned long), a
);
2467 err
= sys_socket(a0
, a1
, a
[2]);
2470 err
= sys_bind(a0
, (struct sockaddr __user
*)a1
, a
[2]);
2473 err
= sys_connect(a0
, (struct sockaddr __user
*)a1
, a
[2]);
2476 err
= sys_listen(a0
, a1
);
2479 err
= sys_accept4(a0
, (struct sockaddr __user
*)a1
,
2480 (int __user
*)a
[2], 0);
2482 case SYS_GETSOCKNAME
:
2484 sys_getsockname(a0
, (struct sockaddr __user
*)a1
,
2485 (int __user
*)a
[2]);
2487 case SYS_GETPEERNAME
:
2489 sys_getpeername(a0
, (struct sockaddr __user
*)a1
,
2490 (int __user
*)a
[2]);
2492 case SYS_SOCKETPAIR
:
2493 err
= sys_socketpair(a0
, a1
, a
[2], (int __user
*)a
[3]);
2496 err
= sys_send(a0
, (void __user
*)a1
, a
[2], a
[3]);
2499 err
= sys_sendto(a0
, (void __user
*)a1
, a
[2], a
[3],
2500 (struct sockaddr __user
*)a
[4], a
[5]);
2503 err
= sys_recv(a0
, (void __user
*)a1
, a
[2], a
[3]);
2506 err
= sys_recvfrom(a0
, (void __user
*)a1
, a
[2], a
[3],
2507 (struct sockaddr __user
*)a
[4],
2508 (int __user
*)a
[5]);
2511 err
= sys_shutdown(a0
, a1
);
2513 case SYS_SETSOCKOPT
:
2514 err
= sys_setsockopt(a0
, a1
, a
[2], (char __user
*)a
[3], a
[4]);
2516 case SYS_GETSOCKOPT
:
2518 sys_getsockopt(a0
, a1
, a
[2], (char __user
*)a
[3],
2519 (int __user
*)a
[4]);
2522 err
= sys_sendmsg(a0
, (struct user_msghdr __user
*)a1
, a
[2]);
2525 err
= sys_sendmmsg(a0
, (struct mmsghdr __user
*)a1
, a
[2], a
[3]);
2528 err
= sys_recvmsg(a0
, (struct user_msghdr __user
*)a1
, a
[2]);
2531 err
= sys_recvmmsg(a0
, (struct mmsghdr __user
*)a1
, a
[2], a
[3],
2532 (struct timespec __user
*)a
[4]);
2535 err
= sys_accept4(a0
, (struct sockaddr __user
*)a1
,
2536 (int __user
*)a
[2], a
[3]);
2545 #endif /* __ARCH_WANT_SYS_SOCKETCALL */
2548 * sock_register - add a socket protocol handler
2549 * @ops: description of protocol
2551 * This function is called by a protocol handler that wants to
2552 * advertise its address family, and have it linked into the
2553 * socket interface. The value ops->family corresponds to the
2554 * socket system call protocol family.
2556 int sock_register(const struct net_proto_family
*ops
)
2560 if (ops
->family
>= NPROTO
) {
2561 pr_crit("protocol %d >= NPROTO(%d)\n", ops
->family
, NPROTO
);
2565 spin_lock(&net_family_lock
);
2566 if (rcu_dereference_protected(net_families
[ops
->family
],
2567 lockdep_is_held(&net_family_lock
)))
2570 rcu_assign_pointer(net_families
[ops
->family
], ops
);
2573 spin_unlock(&net_family_lock
);
2575 pr_info("NET: Registered protocol family %d\n", ops
->family
);
2578 EXPORT_SYMBOL(sock_register
);
2581 * sock_unregister - remove a protocol handler
2582 * @family: protocol family to remove
2584 * This function is called by a protocol handler that wants to
2585 * remove its address family, and have it unlinked from the
2586 * new socket creation.
2588 * If protocol handler is a module, then it can use module reference
2589 * counts to protect against new references. If protocol handler is not
2590 * a module then it needs to provide its own protection in
2591 * the ops->create routine.
2593 void sock_unregister(int family
)
2595 BUG_ON(family
< 0 || family
>= NPROTO
);
2597 spin_lock(&net_family_lock
);
2598 RCU_INIT_POINTER(net_families
[family
], NULL
);
2599 spin_unlock(&net_family_lock
);
2603 pr_info("NET: Unregistered protocol family %d\n", family
);
2605 EXPORT_SYMBOL(sock_unregister
);
2607 static int __init
sock_init(void)
2611 * Initialize the network sysctl infrastructure.
2613 err
= net_sysctl_init();
2618 * Initialize skbuff SLAB cache
2623 * Initialize the protocols module.
2628 err
= register_filesystem(&sock_fs_type
);
2631 sock_mnt
= kern_mount(&sock_fs_type
);
2632 if (IS_ERR(sock_mnt
)) {
2633 err
= PTR_ERR(sock_mnt
);
2637 /* The real protocol initialization is performed in later initcalls.
2640 #ifdef CONFIG_NETFILTER
2641 err
= netfilter_init();
2646 ptp_classifier_init();
2652 unregister_filesystem(&sock_fs_type
);
2657 core_initcall(sock_init
); /* early initcall */
2659 #ifdef CONFIG_PROC_FS
2660 void socket_seq_show(struct seq_file
*seq
)
2665 for_each_possible_cpu(cpu
)
2666 counter
+= per_cpu(sockets_in_use
, cpu
);
2668 /* It can be negative, by the way. 8) */
2672 seq_printf(seq
, "sockets: used %d\n", counter
);
2674 #endif /* CONFIG_PROC_FS */
2676 #ifdef CONFIG_COMPAT
2677 static int do_siocgstamp(struct net
*net
, struct socket
*sock
,
2678 unsigned int cmd
, void __user
*up
)
2680 mm_segment_t old_fs
= get_fs();
2685 err
= sock_do_ioctl(net
, sock
, cmd
, (unsigned long)&ktv
);
2688 err
= compat_put_timeval(&ktv
, up
);
2693 static int do_siocgstampns(struct net
*net
, struct socket
*sock
,
2694 unsigned int cmd
, void __user
*up
)
2696 mm_segment_t old_fs
= get_fs();
2697 struct timespec kts
;
2701 err
= sock_do_ioctl(net
, sock
, cmd
, (unsigned long)&kts
);
2704 err
= compat_put_timespec(&kts
, up
);
2709 static int dev_ifname32(struct net
*net
, struct compat_ifreq __user
*uifr32
)
2711 struct ifreq __user
*uifr
;
2714 uifr
= compat_alloc_user_space(sizeof(struct ifreq
));
2715 if (copy_in_user(uifr
, uifr32
, sizeof(struct compat_ifreq
)))
2718 err
= dev_ioctl(net
, SIOCGIFNAME
, uifr
);
2722 if (copy_in_user(uifr32
, uifr
, sizeof(struct compat_ifreq
)))
2728 static int dev_ifconf(struct net
*net
, struct compat_ifconf __user
*uifc32
)
2730 struct compat_ifconf ifc32
;
2732 struct ifconf __user
*uifc
;
2733 struct compat_ifreq __user
*ifr32
;
2734 struct ifreq __user
*ifr
;
2738 if (copy_from_user(&ifc32
, uifc32
, sizeof(struct compat_ifconf
)))
2741 memset(&ifc
, 0, sizeof(ifc
));
2742 if (ifc32
.ifcbuf
== 0) {
2746 uifc
= compat_alloc_user_space(sizeof(struct ifconf
));
2748 size_t len
= ((ifc32
.ifc_len
/ sizeof(struct compat_ifreq
)) + 1) *
2749 sizeof(struct ifreq
);
2750 uifc
= compat_alloc_user_space(sizeof(struct ifconf
) + len
);
2752 ifr
= ifc
.ifc_req
= (void __user
*)(uifc
+ 1);
2753 ifr32
= compat_ptr(ifc32
.ifcbuf
);
2754 for (i
= 0; i
< ifc32
.ifc_len
; i
+= sizeof(struct compat_ifreq
)) {
2755 if (copy_in_user(ifr
, ifr32
, sizeof(struct compat_ifreq
)))
2761 if (copy_to_user(uifc
, &ifc
, sizeof(struct ifconf
)))
2764 err
= dev_ioctl(net
, SIOCGIFCONF
, uifc
);
2768 if (copy_from_user(&ifc
, uifc
, sizeof(struct ifconf
)))
2772 ifr32
= compat_ptr(ifc32
.ifcbuf
);
2774 i
+ sizeof(struct compat_ifreq
) <= ifc32
.ifc_len
&& j
< ifc
.ifc_len
;
2775 i
+= sizeof(struct compat_ifreq
), j
+= sizeof(struct ifreq
)) {
2776 if (copy_in_user(ifr32
, ifr
, sizeof(struct compat_ifreq
)))
2782 if (ifc32
.ifcbuf
== 0) {
2783 /* Translate from 64-bit structure multiple to
2787 i
= ((i
/ sizeof(struct ifreq
)) * sizeof(struct compat_ifreq
));
2792 if (copy_to_user(uifc32
, &ifc32
, sizeof(struct compat_ifconf
)))
2798 static int ethtool_ioctl(struct net
*net
, struct compat_ifreq __user
*ifr32
)
2800 struct compat_ethtool_rxnfc __user
*compat_rxnfc
;
2801 bool convert_in
= false, convert_out
= false;
2802 size_t buf_size
= ALIGN(sizeof(struct ifreq
), 8);
2803 struct ethtool_rxnfc __user
*rxnfc
;
2804 struct ifreq __user
*ifr
;
2805 u32 rule_cnt
= 0, actual_rule_cnt
;
2810 if (get_user(data
, &ifr32
->ifr_ifru
.ifru_data
))
2813 compat_rxnfc
= compat_ptr(data
);
2815 if (get_user(ethcmd
, &compat_rxnfc
->cmd
))
2818 /* Most ethtool structures are defined without padding.
2819 * Unfortunately struct ethtool_rxnfc is an exception.
2824 case ETHTOOL_GRXCLSRLALL
:
2825 /* Buffer size is variable */
2826 if (get_user(rule_cnt
, &compat_rxnfc
->rule_cnt
))
2828 if (rule_cnt
> KMALLOC_MAX_SIZE
/ sizeof(u32
))
2830 buf_size
+= rule_cnt
* sizeof(u32
);
2832 case ETHTOOL_GRXRINGS
:
2833 case ETHTOOL_GRXCLSRLCNT
:
2834 case ETHTOOL_GRXCLSRULE
:
2835 case ETHTOOL_SRXCLSRLINS
:
2838 case ETHTOOL_SRXCLSRLDEL
:
2839 buf_size
+= sizeof(struct ethtool_rxnfc
);
2844 ifr
= compat_alloc_user_space(buf_size
);
2845 rxnfc
= (void __user
*)ifr
+ ALIGN(sizeof(struct ifreq
), 8);
2847 if (copy_in_user(&ifr
->ifr_name
, &ifr32
->ifr_name
, IFNAMSIZ
))
2850 if (put_user(convert_in
? rxnfc
: compat_ptr(data
),
2851 &ifr
->ifr_ifru
.ifru_data
))
2855 /* We expect there to be holes between fs.m_ext and
2856 * fs.ring_cookie and at the end of fs, but nowhere else.
2858 BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc
, fs
.m_ext
) +
2859 sizeof(compat_rxnfc
->fs
.m_ext
) !=
2860 offsetof(struct ethtool_rxnfc
, fs
.m_ext
) +
2861 sizeof(rxnfc
->fs
.m_ext
));
2863 offsetof(struct compat_ethtool_rxnfc
, fs
.location
) -
2864 offsetof(struct compat_ethtool_rxnfc
, fs
.ring_cookie
) !=
2865 offsetof(struct ethtool_rxnfc
, fs
.location
) -
2866 offsetof(struct ethtool_rxnfc
, fs
.ring_cookie
));
2868 if (copy_in_user(rxnfc
, compat_rxnfc
,
2869 (void __user
*)(&rxnfc
->fs
.m_ext
+ 1) -
2870 (void __user
*)rxnfc
) ||
2871 copy_in_user(&rxnfc
->fs
.ring_cookie
,
2872 &compat_rxnfc
->fs
.ring_cookie
,
2873 (void __user
*)(&rxnfc
->fs
.location
+ 1) -
2874 (void __user
*)&rxnfc
->fs
.ring_cookie
))
2876 if (ethcmd
== ETHTOOL_GRXCLSRLALL
) {
2877 if (put_user(rule_cnt
, &rxnfc
->rule_cnt
))
2879 } else if (copy_in_user(&rxnfc
->rule_cnt
,
2880 &compat_rxnfc
->rule_cnt
,
2881 sizeof(rxnfc
->rule_cnt
)))
2885 ret
= dev_ioctl(net
, SIOCETHTOOL
, ifr
);
2890 if (copy_in_user(compat_rxnfc
, rxnfc
,
2891 (const void __user
*)(&rxnfc
->fs
.m_ext
+ 1) -
2892 (const void __user
*)rxnfc
) ||
2893 copy_in_user(&compat_rxnfc
->fs
.ring_cookie
,
2894 &rxnfc
->fs
.ring_cookie
,
2895 (const void __user
*)(&rxnfc
->fs
.location
+ 1) -
2896 (const void __user
*)&rxnfc
->fs
.ring_cookie
) ||
2897 copy_in_user(&compat_rxnfc
->rule_cnt
, &rxnfc
->rule_cnt
,
2898 sizeof(rxnfc
->rule_cnt
)))
2901 if (ethcmd
== ETHTOOL_GRXCLSRLALL
) {
2902 /* As an optimisation, we only copy the actual
2903 * number of rules that the underlying
2904 * function returned. Since Mallory might
2905 * change the rule count in user memory, we
2906 * check that it is less than the rule count
2907 * originally given (as the user buffer size),
2908 * which has been range-checked.
2910 if (get_user(actual_rule_cnt
, &rxnfc
->rule_cnt
))
2912 if (actual_rule_cnt
< rule_cnt
)
2913 rule_cnt
= actual_rule_cnt
;
2914 if (copy_in_user(&compat_rxnfc
->rule_locs
[0],
2915 &rxnfc
->rule_locs
[0],
2916 rule_cnt
* sizeof(u32
)))
2924 static int compat_siocwandev(struct net
*net
, struct compat_ifreq __user
*uifr32
)
2927 compat_uptr_t uptr32
;
2928 struct ifreq __user
*uifr
;
2930 uifr
= compat_alloc_user_space(sizeof(*uifr
));
2931 if (copy_in_user(uifr
, uifr32
, sizeof(struct compat_ifreq
)))
2934 if (get_user(uptr32
, &uifr32
->ifr_settings
.ifs_ifsu
))
2937 uptr
= compat_ptr(uptr32
);
2939 if (put_user(uptr
, &uifr
->ifr_settings
.ifs_ifsu
.raw_hdlc
))
2942 return dev_ioctl(net
, SIOCWANDEV
, uifr
);
2945 static int bond_ioctl(struct net
*net
, unsigned int cmd
,
2946 struct compat_ifreq __user
*ifr32
)
2949 mm_segment_t old_fs
;
2953 case SIOCBONDENSLAVE
:
2954 case SIOCBONDRELEASE
:
2955 case SIOCBONDSETHWADDR
:
2956 case SIOCBONDCHANGEACTIVE
:
2957 if (copy_from_user(&kifr
, ifr32
, sizeof(struct compat_ifreq
)))
2962 err
= dev_ioctl(net
, cmd
,
2963 (struct ifreq __user __force
*) &kifr
);
2968 return -ENOIOCTLCMD
;
2972 /* Handle ioctls that use ifreq::ifr_data and just need struct ifreq converted */
2973 static int compat_ifr_data_ioctl(struct net
*net
, unsigned int cmd
,
2974 struct compat_ifreq __user
*u_ifreq32
)
2976 struct ifreq __user
*u_ifreq64
;
2977 char tmp_buf
[IFNAMSIZ
];
2978 void __user
*data64
;
2981 if (copy_from_user(&tmp_buf
[0], &(u_ifreq32
->ifr_ifrn
.ifrn_name
[0]),
2984 if (get_user(data32
, &u_ifreq32
->ifr_ifru
.ifru_data
))
2986 data64
= compat_ptr(data32
);
2988 u_ifreq64
= compat_alloc_user_space(sizeof(*u_ifreq64
));
2990 if (copy_to_user(&u_ifreq64
->ifr_ifrn
.ifrn_name
[0], &tmp_buf
[0],
2993 if (put_user(data64
, &u_ifreq64
->ifr_ifru
.ifru_data
))
2996 return dev_ioctl(net
, cmd
, u_ifreq64
);
2999 static int dev_ifsioc(struct net
*net
, struct socket
*sock
,
3000 unsigned int cmd
, struct compat_ifreq __user
*uifr32
)
3002 struct ifreq __user
*uifr
;
3005 uifr
= compat_alloc_user_space(sizeof(*uifr
));
3006 if (copy_in_user(uifr
, uifr32
, sizeof(*uifr32
)))
3009 err
= sock_do_ioctl(net
, sock
, cmd
, (unsigned long)uifr
);
3020 case SIOCGIFBRDADDR
:
3021 case SIOCGIFDSTADDR
:
3022 case SIOCGIFNETMASK
:
3027 if (copy_in_user(uifr32
, uifr
, sizeof(*uifr32
)))
3035 static int compat_sioc_ifmap(struct net
*net
, unsigned int cmd
,
3036 struct compat_ifreq __user
*uifr32
)
3039 struct compat_ifmap __user
*uifmap32
;
3040 mm_segment_t old_fs
;
3043 uifmap32
= &uifr32
->ifr_ifru
.ifru_map
;
3044 err
= copy_from_user(&ifr
, uifr32
, sizeof(ifr
.ifr_name
));
3045 err
|= get_user(ifr
.ifr_map
.mem_start
, &uifmap32
->mem_start
);
3046 err
|= get_user(ifr
.ifr_map
.mem_end
, &uifmap32
->mem_end
);
3047 err
|= get_user(ifr
.ifr_map
.base_addr
, &uifmap32
->base_addr
);
3048 err
|= get_user(ifr
.ifr_map
.irq
, &uifmap32
->irq
);
3049 err
|= get_user(ifr
.ifr_map
.dma
, &uifmap32
->dma
);
3050 err
|= get_user(ifr
.ifr_map
.port
, &uifmap32
->port
);
3056 err
= dev_ioctl(net
, cmd
, (void __user __force
*)&ifr
);
3059 if (cmd
== SIOCGIFMAP
&& !err
) {
3060 err
= copy_to_user(uifr32
, &ifr
, sizeof(ifr
.ifr_name
));
3061 err
|= put_user(ifr
.ifr_map
.mem_start
, &uifmap32
->mem_start
);
3062 err
|= put_user(ifr
.ifr_map
.mem_end
, &uifmap32
->mem_end
);
3063 err
|= put_user(ifr
.ifr_map
.base_addr
, &uifmap32
->base_addr
);
3064 err
|= put_user(ifr
.ifr_map
.irq
, &uifmap32
->irq
);
3065 err
|= put_user(ifr
.ifr_map
.dma
, &uifmap32
->dma
);
3066 err
|= put_user(ifr
.ifr_map
.port
, &uifmap32
->port
);
3075 struct sockaddr rt_dst
; /* target address */
3076 struct sockaddr rt_gateway
; /* gateway addr (RTF_GATEWAY) */
3077 struct sockaddr rt_genmask
; /* target network mask (IP) */
3078 unsigned short rt_flags
;
3081 unsigned char rt_tos
;
3082 unsigned char rt_class
;
3084 short rt_metric
; /* +1 for binary compatibility! */
3085 /* char * */ u32 rt_dev
; /* forcing the device at add */
3086 u32 rt_mtu
; /* per route MTU/Window */
3087 u32 rt_window
; /* Window clamping */
3088 unsigned short rt_irtt
; /* Initial RTT */
3091 struct in6_rtmsg32
{
3092 struct in6_addr rtmsg_dst
;
3093 struct in6_addr rtmsg_src
;
3094 struct in6_addr rtmsg_gateway
;
3104 static int routing_ioctl(struct net
*net
, struct socket
*sock
,
3105 unsigned int cmd
, void __user
*argp
)
3109 struct in6_rtmsg r6
;
3113 mm_segment_t old_fs
= get_fs();
3115 if (sock
&& sock
->sk
&& sock
->sk
->sk_family
== AF_INET6
) { /* ipv6 */
3116 struct in6_rtmsg32 __user
*ur6
= argp
;
3117 ret
= copy_from_user(&r6
.rtmsg_dst
, &(ur6
->rtmsg_dst
),
3118 3 * sizeof(struct in6_addr
));
3119 ret
|= get_user(r6
.rtmsg_type
, &(ur6
->rtmsg_type
));
3120 ret
|= get_user(r6
.rtmsg_dst_len
, &(ur6
->rtmsg_dst_len
));
3121 ret
|= get_user(r6
.rtmsg_src_len
, &(ur6
->rtmsg_src_len
));
3122 ret
|= get_user(r6
.rtmsg_metric
, &(ur6
->rtmsg_metric
));
3123 ret
|= get_user(r6
.rtmsg_info
, &(ur6
->rtmsg_info
));
3124 ret
|= get_user(r6
.rtmsg_flags
, &(ur6
->rtmsg_flags
));
3125 ret
|= get_user(r6
.rtmsg_ifindex
, &(ur6
->rtmsg_ifindex
));
3129 struct rtentry32 __user
*ur4
= argp
;
3130 ret
= copy_from_user(&r4
.rt_dst
, &(ur4
->rt_dst
),
3131 3 * sizeof(struct sockaddr
));
3132 ret
|= get_user(r4
.rt_flags
, &(ur4
->rt_flags
));
3133 ret
|= get_user(r4
.rt_metric
, &(ur4
->rt_metric
));
3134 ret
|= get_user(r4
.rt_mtu
, &(ur4
->rt_mtu
));
3135 ret
|= get_user(r4
.rt_window
, &(ur4
->rt_window
));
3136 ret
|= get_user(r4
.rt_irtt
, &(ur4
->rt_irtt
));
3137 ret
|= get_user(rtdev
, &(ur4
->rt_dev
));
3139 ret
|= copy_from_user(devname
, compat_ptr(rtdev
), 15);
3140 r4
.rt_dev
= (char __user __force
*)devname
;
3154 ret
= sock_do_ioctl(net
, sock
, cmd
, (unsigned long) r
);
3161 /* Since old style bridge ioctl's endup using SIOCDEVPRIVATE
3162 * for some operations; this forces use of the newer bridge-utils that
3163 * use compatible ioctls
3165 static int old_bridge_ioctl(compat_ulong_t __user
*argp
)
3169 if (get_user(tmp
, argp
))
3171 if (tmp
== BRCTL_GET_VERSION
)
3172 return BRCTL_VERSION
+ 1;
3176 static int compat_sock_ioctl_trans(struct file
*file
, struct socket
*sock
,
3177 unsigned int cmd
, unsigned long arg
)
3179 void __user
*argp
= compat_ptr(arg
);
3180 struct sock
*sk
= sock
->sk
;
3181 struct net
*net
= sock_net(sk
);
3183 if (cmd
>= SIOCDEVPRIVATE
&& cmd
<= (SIOCDEVPRIVATE
+ 15))
3184 return compat_ifr_data_ioctl(net
, cmd
, argp
);
3189 return old_bridge_ioctl(argp
);
3191 return dev_ifname32(net
, argp
);
3193 return dev_ifconf(net
, argp
);
3195 return ethtool_ioctl(net
, argp
);
3197 return compat_siocwandev(net
, argp
);
3200 return compat_sioc_ifmap(net
, cmd
, argp
);
3201 case SIOCBONDENSLAVE
:
3202 case SIOCBONDRELEASE
:
3203 case SIOCBONDSETHWADDR
:
3204 case SIOCBONDCHANGEACTIVE
:
3205 return bond_ioctl(net
, cmd
, argp
);
3208 return routing_ioctl(net
, sock
, cmd
, argp
);
3210 return do_siocgstamp(net
, sock
, cmd
, argp
);
3212 return do_siocgstampns(net
, sock
, cmd
, argp
);
3213 case SIOCBONDSLAVEINFOQUERY
:
3214 case SIOCBONDINFOQUERY
:
3217 return compat_ifr_data_ioctl(net
, cmd
, argp
);
3230 return sock_ioctl(file
, cmd
, arg
);
3247 case SIOCSIFHWBROADCAST
:
3249 case SIOCGIFBRDADDR
:
3250 case SIOCSIFBRDADDR
:
3251 case SIOCGIFDSTADDR
:
3252 case SIOCSIFDSTADDR
:
3253 case SIOCGIFNETMASK
:
3254 case SIOCSIFNETMASK
:
3265 return dev_ifsioc(net
, sock
, cmd
, argp
);
3272 return sock_do_ioctl(net
, sock
, cmd
, arg
);
3275 return -ENOIOCTLCMD
;
3278 static long compat_sock_ioctl(struct file
*file
, unsigned int cmd
,
3281 struct socket
*sock
= file
->private_data
;
3282 int ret
= -ENOIOCTLCMD
;
3289 if (sock
->ops
->compat_ioctl
)
3290 ret
= sock
->ops
->compat_ioctl(sock
, cmd
, arg
);
3292 if (ret
== -ENOIOCTLCMD
&&
3293 (cmd
>= SIOCIWFIRST
&& cmd
<= SIOCIWLAST
))
3294 ret
= compat_wext_handle_ioctl(net
, cmd
, arg
);
3296 if (ret
== -ENOIOCTLCMD
)
3297 ret
= compat_sock_ioctl_trans(file
, sock
, cmd
, arg
);
3303 int kernel_bind(struct socket
*sock
, struct sockaddr
*addr
, int addrlen
)
3305 return sock
->ops
->bind(sock
, addr
, addrlen
);
3307 EXPORT_SYMBOL(kernel_bind
);
3309 int kernel_listen(struct socket
*sock
, int backlog
)
3311 return sock
->ops
->listen(sock
, backlog
);
3313 EXPORT_SYMBOL(kernel_listen
);
3315 int kernel_accept(struct socket
*sock
, struct socket
**newsock
, int flags
)
3317 struct sock
*sk
= sock
->sk
;
3320 err
= sock_create_lite(sk
->sk_family
, sk
->sk_type
, sk
->sk_protocol
,
3325 err
= sock
->ops
->accept(sock
, *newsock
, flags
, true);
3327 sock_release(*newsock
);
3332 (*newsock
)->ops
= sock
->ops
;
3333 __module_get((*newsock
)->ops
->owner
);
3338 EXPORT_SYMBOL(kernel_accept
);
3340 int kernel_connect(struct socket
*sock
, struct sockaddr
*addr
, int addrlen
,
3343 return sock
->ops
->connect(sock
, addr
, addrlen
, flags
);
3345 EXPORT_SYMBOL(kernel_connect
);
3347 int kernel_getsockname(struct socket
*sock
, struct sockaddr
*addr
,
3350 return sock
->ops
->getname(sock
, addr
, addrlen
, 0);
3352 EXPORT_SYMBOL(kernel_getsockname
);
3354 int kernel_getpeername(struct socket
*sock
, struct sockaddr
*addr
,
3357 return sock
->ops
->getname(sock
, addr
, addrlen
, 1);
3359 EXPORT_SYMBOL(kernel_getpeername
);
3361 int kernel_getsockopt(struct socket
*sock
, int level
, int optname
,
3362 char *optval
, int *optlen
)
3364 mm_segment_t oldfs
= get_fs();
3365 char __user
*uoptval
;
3366 int __user
*uoptlen
;
3369 uoptval
= (char __user __force
*) optval
;
3370 uoptlen
= (int __user __force
*) optlen
;
3373 if (level
== SOL_SOCKET
)
3374 err
= sock_getsockopt(sock
, level
, optname
, uoptval
, uoptlen
);
3376 err
= sock
->ops
->getsockopt(sock
, level
, optname
, uoptval
,
3381 EXPORT_SYMBOL(kernel_getsockopt
);
3383 int kernel_setsockopt(struct socket
*sock
, int level
, int optname
,
3384 char *optval
, unsigned int optlen
)
3386 mm_segment_t oldfs
= get_fs();
3387 char __user
*uoptval
;
3390 uoptval
= (char __user __force
*) optval
;
3393 if (level
== SOL_SOCKET
)
3394 err
= sock_setsockopt(sock
, level
, optname
, uoptval
, optlen
);
3396 err
= sock
->ops
->setsockopt(sock
, level
, optname
, uoptval
,
3401 EXPORT_SYMBOL(kernel_setsockopt
);
3403 int kernel_sendpage(struct socket
*sock
, struct page
*page
, int offset
,
3404 size_t size
, int flags
)
3406 if (sock
->ops
->sendpage
)
3407 return sock
->ops
->sendpage(sock
, page
, offset
, size
, flags
);
3409 return sock_no_sendpage(sock
, page
, offset
, size
, flags
);
3411 EXPORT_SYMBOL(kernel_sendpage
);
3413 int kernel_sendpage_locked(struct sock
*sk
, struct page
*page
, int offset
,
3414 size_t size
, int flags
)
3416 struct socket
*sock
= sk
->sk_socket
;
3418 if (sock
->ops
->sendpage_locked
)
3419 return sock
->ops
->sendpage_locked(sk
, page
, offset
, size
,
3422 return sock_no_sendpage_locked(sk
, page
, offset
, size
, flags
);
3424 EXPORT_SYMBOL(kernel_sendpage_locked
);
3426 int kernel_sock_ioctl(struct socket
*sock
, int cmd
, unsigned long arg
)
3428 mm_segment_t oldfs
= get_fs();
3432 err
= sock
->ops
->ioctl(sock
, cmd
, arg
);
3437 EXPORT_SYMBOL(kernel_sock_ioctl
);
3439 int kernel_sock_shutdown(struct socket
*sock
, enum sock_shutdown_cmd how
)
3441 return sock
->ops
->shutdown(sock
, how
);
3443 EXPORT_SYMBOL(kernel_sock_shutdown
);
3445 /* This routine returns the IP overhead imposed by a socket i.e.
3446 * the length of the underlying IP header, depending on whether
3447 * this is an IPv4 or IPv6 socket and the length from IP options turned
3448 * on at the socket. Assumes that the caller has a lock on the socket.
3450 u32
kernel_sock_ip_overhead(struct sock
*sk
)
3452 struct inet_sock
*inet
;
3453 struct ip_options_rcu
*opt
;
3455 #if IS_ENABLED(CONFIG_IPV6)
3456 struct ipv6_pinfo
*np
;
3457 struct ipv6_txoptions
*optv6
= NULL
;
3458 #endif /* IS_ENABLED(CONFIG_IPV6) */
3463 switch (sk
->sk_family
) {
3466 overhead
+= sizeof(struct iphdr
);
3467 opt
= rcu_dereference_protected(inet
->inet_opt
,
3468 sock_owned_by_user(sk
));
3470 overhead
+= opt
->opt
.optlen
;
3472 #if IS_ENABLED(CONFIG_IPV6)
3475 overhead
+= sizeof(struct ipv6hdr
);
3477 optv6
= rcu_dereference_protected(np
->opt
,
3478 sock_owned_by_user(sk
));
3480 overhead
+= (optv6
->opt_flen
+ optv6
->opt_nflen
);
3482 #endif /* IS_ENABLED(CONFIG_IPV6) */
3483 default: /* Returns 0 overhead if the socket is not ipv4 or ipv6 */
3487 EXPORT_SYMBOL(kernel_sock_ip_overhead
);