1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * NET An implementation of the SOCKET network access protocol.
5 * Version: @(#)socket.c 1.1.93 18/02/95
12 * Anonymous : NOTSOCK/BADF cleanup. Error fix in
14 * Alan Cox : verify_area() fixes
15 * Alan Cox : Removed DDI
16 * Jonathan Kamens : SOCK_DGRAM reconnect bug
17 * Alan Cox : Moved a load of checks to the very
19 * Alan Cox : Move address structures to/from user
20 * mode above the protocol layers.
21 * Rob Janssen : Allow 0 length sends.
22 * Alan Cox : Asynchronous I/O support (cribbed from the
24 * Niibe Yutaka : Asynchronous I/O for writes (4.4BSD style)
25 * Jeff Uphoff : Made max number of sockets command-line
27 * Matti Aarnio : Made the number of sockets dynamic,
28 * to be allocated when needed, and mr.
29 * Uphoff's max is used as max to be
30 * allowed to allocate.
31 * Linus : Argh. removed all the socket allocation
32 * altogether: it's in the inode now.
33 * Alan Cox : Made sock_alloc()/sock_release() public
34 * for NetROM and future kernel nfsd type
36 * Alan Cox : sendmsg/recvmsg basics.
37 * Tom Dyas : Export net symbols.
38 * Marcin Dalecki : Fixed problems with CONFIG_NET="n".
39 * Alan Cox : Added thread locking to sys_* calls
40 * for sockets. May have errors at the
42 * Kevin Buhr : Fixed the dumb errors in the above.
43 * Andi Kleen : Some small cleanups, optimizations,
44 * and fixed a copy_from_user() bug.
45 * Tigran Aivazian : sys_send(args) calls sys_sendto(args, NULL, 0)
46 * Tigran Aivazian : Made listen(2) backlog sanity checks
47 * protocol-independent
49 * This module is effectively the top level interface to the BSD socket
52 * Based upon Swansea University Computer Society NET3.039
56 #include <linux/socket.h>
57 #include <linux/file.h>
58 #include <linux/net.h>
59 #include <linux/interrupt.h>
60 #include <linux/thread_info.h>
61 #include <linux/rcupdate.h>
62 #include <linux/netdevice.h>
63 #include <linux/proc_fs.h>
64 #include <linux/seq_file.h>
65 #include <linux/mutex.h>
66 #include <linux/if_bridge.h>
67 #include <linux/if_frad.h>
68 #include <linux/if_vlan.h>
69 #include <linux/ptp_classify.h>
70 #include <linux/init.h>
71 #include <linux/poll.h>
72 #include <linux/cache.h>
73 #include <linux/module.h>
74 #include <linux/highmem.h>
75 #include <linux/mount.h>
76 #include <linux/security.h>
77 #include <linux/syscalls.h>
78 #include <linux/compat.h>
79 #include <linux/kmod.h>
80 #include <linux/audit.h>
81 #include <linux/wireless.h>
82 #include <linux/nsproxy.h>
83 #include <linux/magic.h>
84 #include <linux/slab.h>
85 #include <linux/xattr.h>
86 #include <linux/nospec.h>
87 #include <linux/indirect_call_wrapper.h>
89 #include <linux/uaccess.h>
90 #include <asm/unistd.h>
92 #include <net/compat.h>
94 #include <net/cls_cgroup.h>
97 #include <linux/netfilter.h>
99 #include <linux/if_tun.h>
100 #include <linux/ipv6_route.h>
101 #include <linux/route.h>
102 #include <linux/sockios.h>
103 #include <net/busy_poll.h>
104 #include <linux/errqueue.h>
106 /* proto_ops for ipv4 and ipv6 use the same {recv,send}msg function */
107 #if IS_ENABLED(CONFIG_INET)
108 #define INDIRECT_CALL_INET4(f, f1, ...) INDIRECT_CALL_1(f, f1, __VA_ARGS__)
110 #define INDIRECT_CALL_INET4(f, f1, ...) f(__VA_ARGS__)
113 #ifdef CONFIG_NET_RX_BUSY_POLL
114 unsigned int sysctl_net_busy_read __read_mostly;
115 unsigned int sysctl_net_busy_poll __read_mostly;
118 static ssize_t sock_read_iter(struct kiocb *iocb, struct iov_iter *to);
119 static ssize_t sock_write_iter(struct kiocb *iocb, struct iov_iter *from);
120 static int sock_mmap(struct file *file, struct vm_area_struct *vma);
122 static int sock_close(struct inode *inode, struct file *file);
123 static __poll_t sock_poll(struct file *file,
124 struct poll_table_struct *wait);
125 static long sock_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
127 static long compat_sock_ioctl(struct file *file,
128 unsigned int cmd, unsigned long arg);
130 static int sock_fasync(int fd, struct file *filp, int on);
131 static ssize_t sock_sendpage(struct file *file, struct page *page,
132 int offset, size_t size, loff_t *ppos, int more);
133 static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
134 struct pipe_inode_info *pipe, size_t len,
138 * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
139 * in the operation structures but are done directly via the socketcall() multiplexor.
142 static const struct file_operations socket_file_ops = {
143 .owner = THIS_MODULE,
145 .read_iter = sock_read_iter,
146 .write_iter = sock_write_iter,
148 .unlocked_ioctl = sock_ioctl,
150 .compat_ioctl = compat_sock_ioctl,
153 .release = sock_close,
154 .fasync = sock_fasync,
155 .sendpage = sock_sendpage,
156 .splice_write = generic_splice_sendpage,
157 .splice_read = sock_splice_read,
161 * The protocol list. Each protocol is registered in here.
164 static DEFINE_SPINLOCK(net_family_lock);
165 static const struct net_proto_family __rcu *net_families[NPROTO] __read_mostly;
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 __ro_after_init;
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;
259 ei->socket.state = SS_UNCONNECTED;
260 ei->socket.flags = 0;
261 ei->socket.ops = NULL;
262 ei->socket.sk = NULL;
263 ei->socket.file = NULL;
265 return &ei->vfs_inode;
268 static void sock_destroy_inode(struct inode *inode)
270 struct socket_alloc *ei;
272 ei = container_of(inode, struct socket_alloc, vfs_inode);
273 kfree_rcu(ei->socket.wq, rcu);
274 kmem_cache_free(sock_inode_cachep, ei);
277 static void init_once(void *foo)
279 struct socket_alloc *ei = (struct socket_alloc *)foo;
281 inode_init_once(&ei->vfs_inode);
284 static void init_inodecache(void)
286 sock_inode_cachep = kmem_cache_create("sock_inode_cache",
287 sizeof(struct socket_alloc),
289 (SLAB_HWCACHE_ALIGN |
290 SLAB_RECLAIM_ACCOUNT |
291 SLAB_MEM_SPREAD | SLAB_ACCOUNT),
293 BUG_ON(sock_inode_cachep == NULL);
296 static const struct super_operations sockfs_ops = {
297 .alloc_inode = sock_alloc_inode,
298 .destroy_inode = sock_destroy_inode,
299 .statfs = simple_statfs,
303 * sockfs_dname() is called from d_path().
305 static char *sockfs_dname(struct dentry *dentry, char *buffer, int buflen)
307 return dynamic_dname(dentry, buffer, buflen, "socket:[%lu]",
308 d_inode(dentry)->i_ino);
311 static const struct dentry_operations sockfs_dentry_operations = {
312 .d_dname = sockfs_dname,
315 static int sockfs_xattr_get(const struct xattr_handler *handler,
316 struct dentry *dentry, struct inode *inode,
317 const char *suffix, void *value, size_t size)
320 if (dentry->d_name.len + 1 > size)
322 memcpy(value, dentry->d_name.name, dentry->d_name.len + 1);
324 return dentry->d_name.len + 1;
327 #define XATTR_SOCKPROTONAME_SUFFIX "sockprotoname"
328 #define XATTR_NAME_SOCKPROTONAME (XATTR_SYSTEM_PREFIX XATTR_SOCKPROTONAME_SUFFIX)
329 #define XATTR_NAME_SOCKPROTONAME_LEN (sizeof(XATTR_NAME_SOCKPROTONAME)-1)
331 static const struct xattr_handler sockfs_xattr_handler = {
332 .name = XATTR_NAME_SOCKPROTONAME,
333 .get = sockfs_xattr_get,
336 static int sockfs_security_xattr_set(const struct xattr_handler *handler,
337 struct dentry *dentry, struct inode *inode,
338 const char *suffix, const void *value,
339 size_t size, int flags)
341 /* Handled by LSM. */
345 static const struct xattr_handler sockfs_security_xattr_handler = {
346 .prefix = XATTR_SECURITY_PREFIX,
347 .set = sockfs_security_xattr_set,
350 static const struct xattr_handler *sockfs_xattr_handlers[] = {
351 &sockfs_xattr_handler,
352 &sockfs_security_xattr_handler,
356 static struct dentry *sockfs_mount(struct file_system_type *fs_type,
357 int flags, const char *dev_name, void *data)
359 return mount_pseudo_xattr(fs_type, "socket:", &sockfs_ops,
360 sockfs_xattr_handlers,
361 &sockfs_dentry_operations, SOCKFS_MAGIC);
364 static struct vfsmount *sock_mnt __read_mostly;
366 static struct file_system_type sock_fs_type = {
368 .mount = sockfs_mount,
369 .kill_sb = kill_anon_super,
373 * Obtains the first available file descriptor and sets it up for use.
375 * These functions create file structures and maps them to fd space
376 * of the current process. On success it returns file descriptor
377 * and file struct implicitly stored in sock->file.
378 * Note that another thread may close file descriptor before we return
379 * from this function. We use the fact that now we do not refer
380 * to socket after mapping. If one day we will need it, this
381 * function will increment ref. count on file by 1.
383 * In any case returned fd MAY BE not valid!
384 * This race condition is unavoidable
385 * with shared fd spaces, we cannot solve it inside kernel,
386 * but we take care of internal coherence yet.
390 * sock_alloc_file - Bind a &socket to a &file
392 * @flags: file status flags
393 * @dname: protocol name
395 * Returns the &file bound with @sock, implicitly storing it
396 * in sock->file. If dname is %NULL, sets to "".
397 * On failure the return is a ERR pointer (see linux/err.h).
398 * This function uses GFP_KERNEL internally.
401 struct file *sock_alloc_file(struct socket *sock, int flags, const char *dname)
406 dname = sock->sk ? sock->sk->sk_prot_creator->name : "";
408 file = alloc_file_pseudo(SOCK_INODE(sock), sock_mnt, dname,
409 O_RDWR | (flags & O_NONBLOCK),
417 file->private_data = sock;
420 EXPORT_SYMBOL(sock_alloc_file);
422 static int sock_map_fd(struct socket *sock, int flags)
424 struct file *newfile;
425 int fd = get_unused_fd_flags(flags);
426 if (unlikely(fd < 0)) {
431 newfile = sock_alloc_file(sock, flags, NULL);
432 if (!IS_ERR(newfile)) {
433 fd_install(fd, newfile);
438 return PTR_ERR(newfile);
442 * sock_from_file - Return the &socket bounded to @file.
444 * @err: pointer to an error code return
446 * On failure returns %NULL and assigns -ENOTSOCK to @err.
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
561 * NULL is returned. This functions uses GFP_KERNEL internally.
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;
583 EXPORT_SYMBOL(sock_alloc);
586 * sock_release - close a socket
587 * @sock: socket to close
589 * The socket is released from the protocol stack if it has a release
590 * callback, and the inode is then released if the socket is bound to
591 * an inode not a file.
594 static void __sock_release(struct socket *sock, struct inode *inode)
597 struct module *owner = sock->ops->owner;
601 sock->ops->release(sock);
609 if (sock->wq->fasync_list)
610 pr_err("%s: fasync list not empty!\n", __func__);
613 iput(SOCK_INODE(sock));
619 void sock_release(struct socket *sock)
621 __sock_release(sock, NULL);
623 EXPORT_SYMBOL(sock_release);
625 void __sock_tx_timestamp(__u16 tsflags, __u8 *tx_flags)
627 u8 flags = *tx_flags;
629 if (tsflags & SOF_TIMESTAMPING_TX_HARDWARE)
630 flags |= SKBTX_HW_TSTAMP;
632 if (tsflags & SOF_TIMESTAMPING_TX_SOFTWARE)
633 flags |= SKBTX_SW_TSTAMP;
635 if (tsflags & SOF_TIMESTAMPING_TX_SCHED)
636 flags |= SKBTX_SCHED_TSTAMP;
640 EXPORT_SYMBOL(__sock_tx_timestamp);
642 INDIRECT_CALLABLE_DECLARE(int inet_sendmsg(struct socket *, struct msghdr *,
644 static inline int sock_sendmsg_nosec(struct socket *sock, struct msghdr *msg)
646 int ret = INDIRECT_CALL_INET4(sock->ops->sendmsg, inet_sendmsg, sock,
647 msg, msg_data_left(msg));
648 BUG_ON(ret == -EIOCBQUEUED);
653 * sock_sendmsg - send a message through @sock
655 * @msg: message to send
657 * Sends @msg through @sock, passing through LSM.
658 * Returns the number of bytes sent, or an error code.
660 int sock_sendmsg(struct socket *sock, struct msghdr *msg)
662 int err = security_socket_sendmsg(sock, msg,
665 return err ?: sock_sendmsg_nosec(sock, msg);
667 EXPORT_SYMBOL(sock_sendmsg);
670 * kernel_sendmsg - send a message through @sock (kernel-space)
672 * @msg: message header
674 * @num: vec array length
675 * @size: total message data size
677 * Builds the message data with @vec and sends it through @sock.
678 * Returns the number of bytes sent, or an error code.
681 int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
682 struct kvec *vec, size_t num, size_t size)
684 iov_iter_kvec(&msg->msg_iter, WRITE, vec, num, size);
685 return sock_sendmsg(sock, msg);
687 EXPORT_SYMBOL(kernel_sendmsg);
690 * kernel_sendmsg_locked - send a message through @sock (kernel-space)
692 * @msg: message header
693 * @vec: output s/g array
694 * @num: output s/g array length
695 * @size: total message data size
697 * Builds the message data with @vec and sends it through @sock.
698 * Returns the number of bytes sent, or an error code.
699 * Caller must hold @sk.
702 int kernel_sendmsg_locked(struct sock *sk, struct msghdr *msg,
703 struct kvec *vec, size_t num, size_t size)
705 struct socket *sock = sk->sk_socket;
707 if (!sock->ops->sendmsg_locked)
708 return sock_no_sendmsg_locked(sk, msg, size);
710 iov_iter_kvec(&msg->msg_iter, WRITE, vec, num, size);
712 return sock->ops->sendmsg_locked(sk, msg, msg_data_left(msg));
714 EXPORT_SYMBOL(kernel_sendmsg_locked);
716 static bool skb_is_err_queue(const struct sk_buff *skb)
718 /* pkt_type of skbs enqueued on the error queue are set to
719 * PACKET_OUTGOING in skb_set_err_queue(). This is only safe to do
720 * in recvmsg, since skbs received on a local socket will never
721 * have a pkt_type of PACKET_OUTGOING.
723 return skb->pkt_type == PACKET_OUTGOING;
726 /* On transmit, software and hardware timestamps are returned independently.
727 * As the two skb clones share the hardware timestamp, which may be updated
728 * before the software timestamp is received, a hardware TX timestamp may be
729 * returned only if there is no software TX timestamp. Ignore false software
730 * timestamps, which may be made in the __sock_recv_timestamp() call when the
731 * option SO_TIMESTAMP_OLD(NS) is enabled on the socket, even when the skb has a
732 * hardware timestamp.
734 static bool skb_is_swtx_tstamp(const struct sk_buff *skb, int false_tstamp)
736 return skb->tstamp && !false_tstamp && skb_is_err_queue(skb);
739 static void put_ts_pktinfo(struct msghdr *msg, struct sk_buff *skb)
741 struct scm_ts_pktinfo ts_pktinfo;
742 struct net_device *orig_dev;
744 if (!skb_mac_header_was_set(skb))
747 memset(&ts_pktinfo, 0, sizeof(ts_pktinfo));
750 orig_dev = dev_get_by_napi_id(skb_napi_id(skb));
752 ts_pktinfo.if_index = orig_dev->ifindex;
755 ts_pktinfo.pkt_length = skb->len - skb_mac_offset(skb);
756 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPING_PKTINFO,
757 sizeof(ts_pktinfo), &ts_pktinfo);
761 * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
763 void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
766 int need_software_tstamp = sock_flag(sk, SOCK_RCVTSTAMP);
767 int new_tstamp = sock_flag(sk, SOCK_TSTAMP_NEW);
768 struct scm_timestamping_internal tss;
770 int empty = 1, false_tstamp = 0;
771 struct skb_shared_hwtstamps *shhwtstamps =
774 /* Race occurred between timestamp enabling and packet
775 receiving. Fill in the current time for now. */
776 if (need_software_tstamp && skb->tstamp == 0) {
777 __net_timestamp(skb);
781 if (need_software_tstamp) {
782 if (!sock_flag(sk, SOCK_RCVTSTAMPNS)) {
784 struct __kernel_sock_timeval tv;
786 skb_get_new_timestamp(skb, &tv);
787 put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMP_NEW,
790 struct __kernel_old_timeval tv;
792 skb_get_timestamp(skb, &tv);
793 put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMP_OLD,
798 struct __kernel_timespec ts;
800 skb_get_new_timestampns(skb, &ts);
801 put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMPNS_NEW,
806 skb_get_timestampns(skb, &ts);
807 put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMPNS_OLD,
813 memset(&tss, 0, sizeof(tss));
814 if ((sk->sk_tsflags & SOF_TIMESTAMPING_SOFTWARE) &&
815 ktime_to_timespec64_cond(skb->tstamp, tss.ts + 0))
818 (sk->sk_tsflags & SOF_TIMESTAMPING_RAW_HARDWARE) &&
819 !skb_is_swtx_tstamp(skb, false_tstamp) &&
820 ktime_to_timespec64_cond(shhwtstamps->hwtstamp, tss.ts + 2)) {
822 if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_PKTINFO) &&
823 !skb_is_err_queue(skb))
824 put_ts_pktinfo(msg, skb);
827 if (sock_flag(sk, SOCK_TSTAMP_NEW))
828 put_cmsg_scm_timestamping64(msg, &tss);
830 put_cmsg_scm_timestamping(msg, &tss);
832 if (skb_is_err_queue(skb) && skb->len &&
833 SKB_EXT_ERR(skb)->opt_stats)
834 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPING_OPT_STATS,
835 skb->len, skb->data);
838 EXPORT_SYMBOL_GPL(__sock_recv_timestamp);
840 void __sock_recv_wifi_status(struct msghdr *msg, struct sock *sk,
845 if (!sock_flag(sk, SOCK_WIFI_STATUS))
847 if (!skb->wifi_acked_valid)
850 ack = skb->wifi_acked;
852 put_cmsg(msg, SOL_SOCKET, SCM_WIFI_STATUS, sizeof(ack), &ack);
854 EXPORT_SYMBOL_GPL(__sock_recv_wifi_status);
856 static inline void sock_recv_drops(struct msghdr *msg, struct sock *sk,
859 if (sock_flag(sk, SOCK_RXQ_OVFL) && skb && SOCK_SKB_CB(skb)->dropcount)
860 put_cmsg(msg, SOL_SOCKET, SO_RXQ_OVFL,
861 sizeof(__u32), &SOCK_SKB_CB(skb)->dropcount);
864 void __sock_recv_ts_and_drops(struct msghdr *msg, struct sock *sk,
867 sock_recv_timestamp(msg, sk, skb);
868 sock_recv_drops(msg, sk, skb);
870 EXPORT_SYMBOL_GPL(__sock_recv_ts_and_drops);
872 INDIRECT_CALLABLE_DECLARE(int inet_recvmsg(struct socket *, struct msghdr *,
874 static inline int sock_recvmsg_nosec(struct socket *sock, struct msghdr *msg,
877 return INDIRECT_CALL_INET4(sock->ops->recvmsg, inet_recvmsg, sock, msg,
878 msg_data_left(msg), flags);
882 * sock_recvmsg - receive a message from @sock
884 * @msg: message to receive
885 * @flags: message flags
887 * Receives @msg from @sock, passing through LSM. Returns the total number
888 * of bytes received, or an error.
890 int sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags)
892 int err = security_socket_recvmsg(sock, msg, msg_data_left(msg), flags);
894 return err ?: sock_recvmsg_nosec(sock, msg, flags);
896 EXPORT_SYMBOL(sock_recvmsg);
899 * kernel_recvmsg - Receive a message from a socket (kernel space)
900 * @sock: The socket to receive the message from
901 * @msg: Received message
902 * @vec: Input s/g array for message data
903 * @num: Size of input s/g array
904 * @size: Number of bytes to read
905 * @flags: Message flags (MSG_DONTWAIT, etc...)
907 * On return the msg structure contains the scatter/gather array passed in the
908 * vec argument. The array is modified so that it consists of the unfilled
909 * portion of the original array.
911 * The returned value is the total number of bytes received, or an error.
914 int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
915 struct kvec *vec, size_t num, size_t size, int flags)
917 mm_segment_t oldfs = get_fs();
920 iov_iter_kvec(&msg->msg_iter, READ, vec, num, size);
922 result = sock_recvmsg(sock, msg, flags);
926 EXPORT_SYMBOL(kernel_recvmsg);
928 static ssize_t sock_sendpage(struct file *file, struct page *page,
929 int offset, size_t size, loff_t *ppos, int more)
934 sock = file->private_data;
936 flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
937 /* more is a combination of MSG_MORE and MSG_SENDPAGE_NOTLAST */
940 return kernel_sendpage(sock, page, offset, size, flags);
943 static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
944 struct pipe_inode_info *pipe, size_t len,
947 struct socket *sock = file->private_data;
949 if (unlikely(!sock->ops->splice_read))
950 return generic_file_splice_read(file, ppos, pipe, len, flags);
952 return sock->ops->splice_read(sock, ppos, pipe, len, flags);
955 static ssize_t sock_read_iter(struct kiocb *iocb, struct iov_iter *to)
957 struct file *file = iocb->ki_filp;
958 struct socket *sock = file->private_data;
959 struct msghdr msg = {.msg_iter = *to,
963 if (file->f_flags & O_NONBLOCK)
964 msg.msg_flags = MSG_DONTWAIT;
966 if (iocb->ki_pos != 0)
969 if (!iov_iter_count(to)) /* Match SYS5 behaviour */
972 res = sock_recvmsg(sock, &msg, msg.msg_flags);
977 static ssize_t sock_write_iter(struct kiocb *iocb, struct iov_iter *from)
979 struct file *file = iocb->ki_filp;
980 struct socket *sock = file->private_data;
981 struct msghdr msg = {.msg_iter = *from,
985 if (iocb->ki_pos != 0)
988 if (file->f_flags & O_NONBLOCK)
989 msg.msg_flags = MSG_DONTWAIT;
991 if (sock->type == SOCK_SEQPACKET)
992 msg.msg_flags |= MSG_EOR;
994 res = sock_sendmsg(sock, &msg);
995 *from = msg.msg_iter;
1000 * Atomic setting of ioctl hooks to avoid race
1001 * with module unload.
1004 static DEFINE_MUTEX(br_ioctl_mutex);
1005 static int (*br_ioctl_hook) (struct net *, unsigned int cmd, void __user *arg);
1007 void brioctl_set(int (*hook) (struct net *, unsigned int, void __user *))
1009 mutex_lock(&br_ioctl_mutex);
1010 br_ioctl_hook = hook;
1011 mutex_unlock(&br_ioctl_mutex);
1013 EXPORT_SYMBOL(brioctl_set);
1015 static DEFINE_MUTEX(vlan_ioctl_mutex);
1016 static int (*vlan_ioctl_hook) (struct net *, void __user *arg);
1018 void vlan_ioctl_set(int (*hook) (struct net *, void __user *))
1020 mutex_lock(&vlan_ioctl_mutex);
1021 vlan_ioctl_hook = hook;
1022 mutex_unlock(&vlan_ioctl_mutex);
1024 EXPORT_SYMBOL(vlan_ioctl_set);
1026 static DEFINE_MUTEX(dlci_ioctl_mutex);
1027 static int (*dlci_ioctl_hook) (unsigned int, void __user *);
1029 void dlci_ioctl_set(int (*hook) (unsigned int, void __user *))
1031 mutex_lock(&dlci_ioctl_mutex);
1032 dlci_ioctl_hook = hook;
1033 mutex_unlock(&dlci_ioctl_mutex);
1035 EXPORT_SYMBOL(dlci_ioctl_set);
1037 static long sock_do_ioctl(struct net *net, struct socket *sock,
1038 unsigned int cmd, unsigned long arg)
1041 void __user *argp = (void __user *)arg;
1043 err = sock->ops->ioctl(sock, cmd, arg);
1046 * If this ioctl is unknown try to hand it down
1047 * to the NIC driver.
1049 if (err != -ENOIOCTLCMD)
1052 if (cmd == SIOCGIFCONF) {
1054 if (copy_from_user(&ifc, argp, sizeof(struct ifconf)))
1057 err = dev_ifconf(net, &ifc, sizeof(struct ifreq));
1059 if (!err && copy_to_user(argp, &ifc, sizeof(struct ifconf)))
1064 if (copy_from_user(&ifr, argp, sizeof(struct ifreq)))
1066 err = dev_ioctl(net, cmd, &ifr, &need_copyout);
1067 if (!err && need_copyout)
1068 if (copy_to_user(argp, &ifr, sizeof(struct ifreq)))
1075 * With an ioctl, arg may well be a user mode pointer, but we don't know
1076 * what to do with it - that's up to the protocol still.
1080 * get_net_ns - increment the refcount of the network namespace
1081 * @ns: common namespace (net)
1083 * Returns the net's common namespace.
1086 struct ns_common *get_net_ns(struct ns_common *ns)
1088 return &get_net(container_of(ns, struct net, ns))->ns;
1090 EXPORT_SYMBOL_GPL(get_net_ns);
1092 static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
1094 struct socket *sock;
1096 void __user *argp = (void __user *)arg;
1100 sock = file->private_data;
1103 if (unlikely(cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15))) {
1106 if (copy_from_user(&ifr, argp, sizeof(struct ifreq)))
1108 err = dev_ioctl(net, cmd, &ifr, &need_copyout);
1109 if (!err && need_copyout)
1110 if (copy_to_user(argp, &ifr, sizeof(struct ifreq)))
1113 #ifdef CONFIG_WEXT_CORE
1114 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
1115 err = wext_handle_ioctl(net, cmd, argp);
1122 if (get_user(pid, (int __user *)argp))
1124 err = f_setown(sock->file, pid, 1);
1128 err = put_user(f_getown(sock->file),
1129 (int __user *)argp);
1137 request_module("bridge");
1139 mutex_lock(&br_ioctl_mutex);
1141 err = br_ioctl_hook(net, cmd, argp);
1142 mutex_unlock(&br_ioctl_mutex);
1147 if (!vlan_ioctl_hook)
1148 request_module("8021q");
1150 mutex_lock(&vlan_ioctl_mutex);
1151 if (vlan_ioctl_hook)
1152 err = vlan_ioctl_hook(net, argp);
1153 mutex_unlock(&vlan_ioctl_mutex);
1158 if (!dlci_ioctl_hook)
1159 request_module("dlci");
1161 mutex_lock(&dlci_ioctl_mutex);
1162 if (dlci_ioctl_hook)
1163 err = dlci_ioctl_hook(cmd, argp);
1164 mutex_unlock(&dlci_ioctl_mutex);
1168 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1171 err = open_related_ns(&net->ns, get_net_ns);
1173 case SIOCGSTAMP_OLD:
1174 case SIOCGSTAMPNS_OLD:
1175 if (!sock->ops->gettstamp) {
1179 err = sock->ops->gettstamp(sock, argp,
1180 cmd == SIOCGSTAMP_OLD,
1181 !IS_ENABLED(CONFIG_64BIT));
1183 case SIOCGSTAMP_NEW:
1184 case SIOCGSTAMPNS_NEW:
1185 if (!sock->ops->gettstamp) {
1189 err = sock->ops->gettstamp(sock, argp,
1190 cmd == SIOCGSTAMP_NEW,
1194 err = sock_do_ioctl(net, sock, cmd, arg);
1201 * sock_create_lite - creates a socket
1202 * @family: protocol family (AF_INET, ...)
1203 * @type: communication type (SOCK_STREAM, ...)
1204 * @protocol: protocol (0, ...)
1207 * Creates a new socket and assigns it to @res, passing through LSM.
1208 * The new socket initialization is not complete, see kernel_accept().
1209 * Returns 0 or an error. On failure @res is set to %NULL.
1210 * This function internally uses GFP_KERNEL.
1213 int sock_create_lite(int family, int type, int protocol, struct socket **res)
1216 struct socket *sock = NULL;
1218 err = security_socket_create(family, type, protocol, 1);
1222 sock = sock_alloc();
1229 err = security_socket_post_create(sock, family, type, protocol, 1);
1241 EXPORT_SYMBOL(sock_create_lite);
1243 /* No kernel lock held - perfect */
1244 static __poll_t sock_poll(struct file *file, poll_table *wait)
1246 struct socket *sock = file->private_data;
1247 __poll_t events = poll_requested_events(wait), flag = 0;
1249 if (!sock->ops->poll)
1252 if (sk_can_busy_loop(sock->sk)) {
1253 /* poll once if requested by the syscall */
1254 if (events & POLL_BUSY_LOOP)
1255 sk_busy_loop(sock->sk, 1);
1257 /* if this socket can poll_ll, tell the system call */
1258 flag = POLL_BUSY_LOOP;
1261 return sock->ops->poll(file, sock, wait) | flag;
1264 static int sock_mmap(struct file *file, struct vm_area_struct *vma)
1266 struct socket *sock = file->private_data;
1268 return sock->ops->mmap(file, sock, vma);
1271 static int sock_close(struct inode *inode, struct file *filp)
1273 __sock_release(SOCKET_I(inode), inode);
1278 * Update the socket async list
1280 * Fasync_list locking strategy.
1282 * 1. fasync_list is modified only under process context socket lock
1283 * i.e. under semaphore.
1284 * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
1285 * or under socket lock
1288 static int sock_fasync(int fd, struct file *filp, int on)
1290 struct socket *sock = filp->private_data;
1291 struct sock *sk = sock->sk;
1292 struct socket_wq *wq;
1299 fasync_helper(fd, filp, on, &wq->fasync_list);
1301 if (!wq->fasync_list)
1302 sock_reset_flag(sk, SOCK_FASYNC);
1304 sock_set_flag(sk, SOCK_FASYNC);
1310 /* This function may be called only under rcu_lock */
1312 int sock_wake_async(struct socket_wq *wq, int how, int band)
1314 if (!wq || !wq->fasync_list)
1318 case SOCK_WAKE_WAITD:
1319 if (test_bit(SOCKWQ_ASYNC_WAITDATA, &wq->flags))
1322 case SOCK_WAKE_SPACE:
1323 if (!test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags))
1328 kill_fasync(&wq->fasync_list, SIGIO, band);
1331 kill_fasync(&wq->fasync_list, SIGURG, band);
1336 EXPORT_SYMBOL(sock_wake_async);
1339 * __sock_create - creates a socket
1340 * @net: net namespace
1341 * @family: protocol family (AF_INET, ...)
1342 * @type: communication type (SOCK_STREAM, ...)
1343 * @protocol: protocol (0, ...)
1345 * @kern: boolean for kernel space sockets
1347 * Creates a new socket and assigns it to @res, passing through LSM.
1348 * Returns 0 or an error. On failure @res is set to %NULL. @kern must
1349 * be set to true if the socket resides in kernel space.
1350 * This function internally uses GFP_KERNEL.
1353 int __sock_create(struct net *net, int family, int type, int protocol,
1354 struct socket **res, int kern)
1357 struct socket *sock;
1358 const struct net_proto_family *pf;
1361 * Check protocol is in range
1363 if (family < 0 || family >= NPROTO)
1364 return -EAFNOSUPPORT;
1365 if (type < 0 || type >= SOCK_MAX)
1370 This uglymoron is moved from INET layer to here to avoid
1371 deadlock in module load.
1373 if (family == PF_INET && type == SOCK_PACKET) {
1374 pr_info_once("%s uses obsolete (PF_INET,SOCK_PACKET)\n",
1379 err = security_socket_create(family, type, protocol, kern);
1384 * Allocate the socket and allow the family to set things up. if
1385 * the protocol is 0, the family is instructed to select an appropriate
1388 sock = sock_alloc();
1390 net_warn_ratelimited("socket: no more sockets\n");
1391 return -ENFILE; /* Not exactly a match, but its the
1392 closest posix thing */
1397 #ifdef CONFIG_MODULES
1398 /* Attempt to load a protocol module if the find failed.
1400 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
1401 * requested real, full-featured networking support upon configuration.
1402 * Otherwise module support will break!
1404 if (rcu_access_pointer(net_families[family]) == NULL)
1405 request_module("net-pf-%d", family);
1409 pf = rcu_dereference(net_families[family]);
1410 err = -EAFNOSUPPORT;
1415 * We will call the ->create function, that possibly is in a loadable
1416 * module, so we have to bump that loadable module refcnt first.
1418 if (!try_module_get(pf->owner))
1421 /* Now protected by module ref count */
1424 err = pf->create(net, sock, protocol, kern);
1426 goto out_module_put;
1429 * Now to bump the refcnt of the [loadable] module that owns this
1430 * socket at sock_release time we decrement its refcnt.
1432 if (!try_module_get(sock->ops->owner))
1433 goto out_module_busy;
1436 * Now that we're done with the ->create function, the [loadable]
1437 * module can have its refcnt decremented
1439 module_put(pf->owner);
1440 err = security_socket_post_create(sock, family, type, protocol, kern);
1442 goto out_sock_release;
1448 err = -EAFNOSUPPORT;
1451 module_put(pf->owner);
1458 goto out_sock_release;
1460 EXPORT_SYMBOL(__sock_create);
1463 * sock_create - creates a socket
1464 * @family: protocol family (AF_INET, ...)
1465 * @type: communication type (SOCK_STREAM, ...)
1466 * @protocol: protocol (0, ...)
1469 * A wrapper around __sock_create().
1470 * Returns 0 or an error. This function internally uses GFP_KERNEL.
1473 int sock_create(int family, int type, int protocol, struct socket **res)
1475 return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0);
1477 EXPORT_SYMBOL(sock_create);
1480 * sock_create_kern - creates a socket (kernel space)
1481 * @net: net namespace
1482 * @family: protocol family (AF_INET, ...)
1483 * @type: communication type (SOCK_STREAM, ...)
1484 * @protocol: protocol (0, ...)
1487 * A wrapper around __sock_create().
1488 * Returns 0 or an error. This function internally uses GFP_KERNEL.
1491 int sock_create_kern(struct net *net, int family, int type, int protocol, struct socket **res)
1493 return __sock_create(net, family, type, protocol, res, 1);
1495 EXPORT_SYMBOL(sock_create_kern);
1497 int __sys_socket(int family, int type, int protocol)
1500 struct socket *sock;
1503 /* Check the SOCK_* constants for consistency. */
1504 BUILD_BUG_ON(SOCK_CLOEXEC != O_CLOEXEC);
1505 BUILD_BUG_ON((SOCK_MAX | SOCK_TYPE_MASK) != SOCK_TYPE_MASK);
1506 BUILD_BUG_ON(SOCK_CLOEXEC & SOCK_TYPE_MASK);
1507 BUILD_BUG_ON(SOCK_NONBLOCK & SOCK_TYPE_MASK);
1509 flags = type & ~SOCK_TYPE_MASK;
1510 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1512 type &= SOCK_TYPE_MASK;
1514 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1515 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1517 retval = sock_create(family, type, protocol, &sock);
1521 return sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK));
1524 SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol)
1526 return __sys_socket(family, type, protocol);
1530 * Create a pair of connected sockets.
1533 int __sys_socketpair(int family, int type, int protocol, int __user *usockvec)
1535 struct socket *sock1, *sock2;
1537 struct file *newfile1, *newfile2;
1540 flags = type & ~SOCK_TYPE_MASK;
1541 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1543 type &= SOCK_TYPE_MASK;
1545 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1546 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1549 * reserve descriptors and make sure we won't fail
1550 * to return them to userland.
1552 fd1 = get_unused_fd_flags(flags);
1553 if (unlikely(fd1 < 0))
1556 fd2 = get_unused_fd_flags(flags);
1557 if (unlikely(fd2 < 0)) {
1562 err = put_user(fd1, &usockvec[0]);
1566 err = put_user(fd2, &usockvec[1]);
1571 * Obtain the first socket and check if the underlying protocol
1572 * supports the socketpair call.
1575 err = sock_create(family, type, protocol, &sock1);
1576 if (unlikely(err < 0))
1579 err = sock_create(family, type, protocol, &sock2);
1580 if (unlikely(err < 0)) {
1581 sock_release(sock1);
1585 err = security_socket_socketpair(sock1, sock2);
1586 if (unlikely(err)) {
1587 sock_release(sock2);
1588 sock_release(sock1);
1592 err = sock1->ops->socketpair(sock1, sock2);
1593 if (unlikely(err < 0)) {
1594 sock_release(sock2);
1595 sock_release(sock1);
1599 newfile1 = sock_alloc_file(sock1, flags, NULL);
1600 if (IS_ERR(newfile1)) {
1601 err = PTR_ERR(newfile1);
1602 sock_release(sock2);
1606 newfile2 = sock_alloc_file(sock2, flags, NULL);
1607 if (IS_ERR(newfile2)) {
1608 err = PTR_ERR(newfile2);
1613 audit_fd_pair(fd1, fd2);
1615 fd_install(fd1, newfile1);
1616 fd_install(fd2, newfile2);
1625 SYSCALL_DEFINE4(socketpair, int, family, int, type, int, protocol,
1626 int __user *, usockvec)
1628 return __sys_socketpair(family, type, protocol, usockvec);
1632 * Bind a name to a socket. Nothing much to do here since it's
1633 * the protocol's responsibility to handle the local address.
1635 * We move the socket address to kernel space before we call
1636 * the protocol layer (having also checked the address is ok).
1639 int __sys_bind(int fd, struct sockaddr __user *umyaddr, int addrlen)
1641 struct socket *sock;
1642 struct sockaddr_storage address;
1643 int err, fput_needed;
1645 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1647 err = move_addr_to_kernel(umyaddr, addrlen, &address);
1649 err = security_socket_bind(sock,
1650 (struct sockaddr *)&address,
1653 err = sock->ops->bind(sock,
1657 fput_light(sock->file, fput_needed);
1662 SYSCALL_DEFINE3(bind, int, fd, struct sockaddr __user *, umyaddr, int, addrlen)
1664 return __sys_bind(fd, umyaddr, addrlen);
1668 * Perform a listen. Basically, we allow the protocol to do anything
1669 * necessary for a listen, and if that works, we mark the socket as
1670 * ready for listening.
1673 int __sys_listen(int fd, int backlog)
1675 struct socket *sock;
1676 int err, fput_needed;
1679 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1681 somaxconn = sock_net(sock->sk)->core.sysctl_somaxconn;
1682 if ((unsigned int)backlog > somaxconn)
1683 backlog = somaxconn;
1685 err = security_socket_listen(sock, backlog);
1687 err = sock->ops->listen(sock, backlog);
1689 fput_light(sock->file, fput_needed);
1694 SYSCALL_DEFINE2(listen, int, fd, int, backlog)
1696 return __sys_listen(fd, backlog);
1700 * For accept, we attempt to create a new socket, set up the link
1701 * with the client, wake up the client, then return the new
1702 * connected fd. We collect the address of the connector in kernel
1703 * space and move it to user at the very end. This is unclean because
1704 * we open the socket then return an error.
1706 * 1003.1g adds the ability to recvmsg() to query connection pending
1707 * status to recvmsg. We need to add that support in a way thats
1708 * clean when we restructure accept also.
1711 int __sys_accept4(int fd, struct sockaddr __user *upeer_sockaddr,
1712 int __user *upeer_addrlen, int flags)
1714 struct socket *sock, *newsock;
1715 struct file *newfile;
1716 int err, len, newfd, fput_needed;
1717 struct sockaddr_storage address;
1719 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1722 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1723 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1725 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1730 newsock = sock_alloc();
1734 newsock->type = sock->type;
1735 newsock->ops = sock->ops;
1738 * We don't need try_module_get here, as the listening socket (sock)
1739 * has the protocol module (sock->ops->owner) held.
1741 __module_get(newsock->ops->owner);
1743 newfd = get_unused_fd_flags(flags);
1744 if (unlikely(newfd < 0)) {
1746 sock_release(newsock);
1749 newfile = sock_alloc_file(newsock, flags, sock->sk->sk_prot_creator->name);
1750 if (IS_ERR(newfile)) {
1751 err = PTR_ERR(newfile);
1752 put_unused_fd(newfd);
1756 err = security_socket_accept(sock, newsock);
1760 err = sock->ops->accept(sock, newsock, sock->file->f_flags, false);
1764 if (upeer_sockaddr) {
1765 len = newsock->ops->getname(newsock,
1766 (struct sockaddr *)&address, 2);
1768 err = -ECONNABORTED;
1771 err = move_addr_to_user(&address,
1772 len, upeer_sockaddr, upeer_addrlen);
1777 /* File flags are not inherited via accept() unlike another OSes. */
1779 fd_install(newfd, newfile);
1783 fput_light(sock->file, fput_needed);
1788 put_unused_fd(newfd);
1792 SYSCALL_DEFINE4(accept4, int, fd, struct sockaddr __user *, upeer_sockaddr,
1793 int __user *, upeer_addrlen, int, flags)
1795 return __sys_accept4(fd, upeer_sockaddr, upeer_addrlen, flags);
1798 SYSCALL_DEFINE3(accept, int, fd, struct sockaddr __user *, upeer_sockaddr,
1799 int __user *, upeer_addrlen)
1801 return __sys_accept4(fd, upeer_sockaddr, upeer_addrlen, 0);
1805 * Attempt to connect to a socket with the server address. The address
1806 * is in user space so we verify it is OK and move it to kernel space.
1808 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
1811 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
1812 * other SEQPACKET protocols that take time to connect() as it doesn't
1813 * include the -EINPROGRESS status for such sockets.
1816 int __sys_connect(int fd, struct sockaddr __user *uservaddr, int addrlen)
1818 struct socket *sock;
1819 struct sockaddr_storage address;
1820 int err, fput_needed;
1822 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1825 err = move_addr_to_kernel(uservaddr, addrlen, &address);
1830 security_socket_connect(sock, (struct sockaddr *)&address, addrlen);
1834 err = sock->ops->connect(sock, (struct sockaddr *)&address, addrlen,
1835 sock->file->f_flags);
1837 fput_light(sock->file, fput_needed);
1842 SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
1845 return __sys_connect(fd, uservaddr, addrlen);
1849 * Get the local address ('name') of a socket object. Move the obtained
1850 * name to user space.
1853 int __sys_getsockname(int fd, struct sockaddr __user *usockaddr,
1854 int __user *usockaddr_len)
1856 struct socket *sock;
1857 struct sockaddr_storage address;
1858 int err, fput_needed;
1860 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1864 err = security_socket_getsockname(sock);
1868 err = sock->ops->getname(sock, (struct sockaddr *)&address, 0);
1871 /* "err" is actually length in this case */
1872 err = move_addr_to_user(&address, err, usockaddr, usockaddr_len);
1875 fput_light(sock->file, fput_needed);
1880 SYSCALL_DEFINE3(getsockname, int, fd, struct sockaddr __user *, usockaddr,
1881 int __user *, usockaddr_len)
1883 return __sys_getsockname(fd, usockaddr, usockaddr_len);
1887 * Get the remote address ('name') of a socket object. Move the obtained
1888 * name to user space.
1891 int __sys_getpeername(int fd, struct sockaddr __user *usockaddr,
1892 int __user *usockaddr_len)
1894 struct socket *sock;
1895 struct sockaddr_storage address;
1896 int err, fput_needed;
1898 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1900 err = security_socket_getpeername(sock);
1902 fput_light(sock->file, fput_needed);
1906 err = sock->ops->getname(sock, (struct sockaddr *)&address, 1);
1908 /* "err" is actually length in this case */
1909 err = move_addr_to_user(&address, err, usockaddr,
1911 fput_light(sock->file, fput_needed);
1916 SYSCALL_DEFINE3(getpeername, int, fd, struct sockaddr __user *, usockaddr,
1917 int __user *, usockaddr_len)
1919 return __sys_getpeername(fd, usockaddr, usockaddr_len);
1923 * Send a datagram to a given address. We move the address into kernel
1924 * space and check the user space data area is readable before invoking
1927 int __sys_sendto(int fd, void __user *buff, size_t len, unsigned int flags,
1928 struct sockaddr __user *addr, int addr_len)
1930 struct socket *sock;
1931 struct sockaddr_storage address;
1937 err = import_single_range(WRITE, buff, len, &iov, &msg.msg_iter);
1940 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1944 msg.msg_name = NULL;
1945 msg.msg_control = NULL;
1946 msg.msg_controllen = 0;
1947 msg.msg_namelen = 0;
1949 err = move_addr_to_kernel(addr, addr_len, &address);
1952 msg.msg_name = (struct sockaddr *)&address;
1953 msg.msg_namelen = addr_len;
1955 if (sock->file->f_flags & O_NONBLOCK)
1956 flags |= MSG_DONTWAIT;
1957 msg.msg_flags = flags;
1958 err = sock_sendmsg(sock, &msg);
1961 fput_light(sock->file, fput_needed);
1966 SYSCALL_DEFINE6(sendto, int, fd, void __user *, buff, size_t, len,
1967 unsigned int, flags, struct sockaddr __user *, addr,
1970 return __sys_sendto(fd, buff, len, flags, addr, addr_len);
1974 * Send a datagram down a socket.
1977 SYSCALL_DEFINE4(send, int, fd, void __user *, buff, size_t, len,
1978 unsigned int, flags)
1980 return __sys_sendto(fd, buff, len, flags, NULL, 0);
1984 * Receive a frame from the socket and optionally record the address of the
1985 * sender. We verify the buffers are writable and if needed move the
1986 * sender address from kernel to user space.
1988 int __sys_recvfrom(int fd, void __user *ubuf, size_t size, unsigned int flags,
1989 struct sockaddr __user *addr, int __user *addr_len)
1991 struct socket *sock;
1994 struct sockaddr_storage address;
1998 err = import_single_range(READ, ubuf, size, &iov, &msg.msg_iter);
2001 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2005 msg.msg_control = NULL;
2006 msg.msg_controllen = 0;
2007 /* Save some cycles and don't copy the address if not needed */
2008 msg.msg_name = addr ? (struct sockaddr *)&address : NULL;
2009 /* We assume all kernel code knows the size of sockaddr_storage */
2010 msg.msg_namelen = 0;
2011 msg.msg_iocb = NULL;
2013 if (sock->file->f_flags & O_NONBLOCK)
2014 flags |= MSG_DONTWAIT;
2015 err = sock_recvmsg(sock, &msg, flags);
2017 if (err >= 0 && addr != NULL) {
2018 err2 = move_addr_to_user(&address,
2019 msg.msg_namelen, addr, addr_len);
2024 fput_light(sock->file, fput_needed);
2029 SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, ubuf, size_t, size,
2030 unsigned int, flags, struct sockaddr __user *, addr,
2031 int __user *, addr_len)
2033 return __sys_recvfrom(fd, ubuf, size, flags, addr, addr_len);
2037 * Receive a datagram from a socket.
2040 SYSCALL_DEFINE4(recv, int, fd, void __user *, ubuf, size_t, size,
2041 unsigned int, flags)
2043 return __sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
2047 * Set a socket option. Because we don't know the option lengths we have
2048 * to pass the user mode parameter for the protocols to sort out.
2051 static int __sys_setsockopt(int fd, int level, int optname,
2052 char __user *optval, int optlen)
2054 int err, fput_needed;
2055 struct socket *sock;
2060 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2062 err = security_socket_setsockopt(sock, level, optname);
2066 if (level == SOL_SOCKET)
2068 sock_setsockopt(sock, level, optname, optval,
2072 sock->ops->setsockopt(sock, level, optname, optval,
2075 fput_light(sock->file, fput_needed);
2080 SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname,
2081 char __user *, optval, int, optlen)
2083 return __sys_setsockopt(fd, level, optname, optval, optlen);
2087 * Get a socket option. Because we don't know the option lengths we have
2088 * to pass a user mode parameter for the protocols to sort out.
2091 static int __sys_getsockopt(int fd, int level, int optname,
2092 char __user *optval, int __user *optlen)
2094 int err, fput_needed;
2095 struct socket *sock;
2097 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2099 err = security_socket_getsockopt(sock, level, optname);
2103 if (level == SOL_SOCKET)
2105 sock_getsockopt(sock, level, optname, optval,
2109 sock->ops->getsockopt(sock, level, optname, optval,
2112 fput_light(sock->file, fput_needed);
2117 SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname,
2118 char __user *, optval, int __user *, optlen)
2120 return __sys_getsockopt(fd, level, optname, optval, optlen);
2124 * Shutdown a socket.
2127 int __sys_shutdown(int fd, int how)
2129 int err, fput_needed;
2130 struct socket *sock;
2132 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2134 err = security_socket_shutdown(sock, how);
2136 err = sock->ops->shutdown(sock, how);
2137 fput_light(sock->file, fput_needed);
2142 SYSCALL_DEFINE2(shutdown, int, fd, int, how)
2144 return __sys_shutdown(fd, how);
2147 /* A couple of helpful macros for getting the address of the 32/64 bit
2148 * fields which are the same type (int / unsigned) on our platforms.
2150 #define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
2151 #define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
2152 #define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
2154 struct used_address {
2155 struct sockaddr_storage name;
2156 unsigned int name_len;
2159 static int copy_msghdr_from_user(struct msghdr *kmsg,
2160 struct user_msghdr __user *umsg,
2161 struct sockaddr __user **save_addr,
2164 struct user_msghdr msg;
2167 if (copy_from_user(&msg, umsg, sizeof(*umsg)))
2170 kmsg->msg_control = (void __force *)msg.msg_control;
2171 kmsg->msg_controllen = msg.msg_controllen;
2172 kmsg->msg_flags = msg.msg_flags;
2174 kmsg->msg_namelen = msg.msg_namelen;
2176 kmsg->msg_namelen = 0;
2178 if (kmsg->msg_namelen < 0)
2181 if (kmsg->msg_namelen > sizeof(struct sockaddr_storage))
2182 kmsg->msg_namelen = sizeof(struct sockaddr_storage);
2185 *save_addr = msg.msg_name;
2187 if (msg.msg_name && kmsg->msg_namelen) {
2189 err = move_addr_to_kernel(msg.msg_name,
2196 kmsg->msg_name = NULL;
2197 kmsg->msg_namelen = 0;
2200 if (msg.msg_iovlen > UIO_MAXIOV)
2203 kmsg->msg_iocb = NULL;
2205 return import_iovec(save_addr ? READ : WRITE,
2206 msg.msg_iov, msg.msg_iovlen,
2207 UIO_FASTIOV, iov, &kmsg->msg_iter);
2210 static int ___sys_sendmsg(struct socket *sock, struct user_msghdr __user *msg,
2211 struct msghdr *msg_sys, unsigned int flags,
2212 struct used_address *used_address,
2213 unsigned int allowed_msghdr_flags)
2215 struct compat_msghdr __user *msg_compat =
2216 (struct compat_msghdr __user *)msg;
2217 struct sockaddr_storage address;
2218 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
2219 unsigned char ctl[sizeof(struct cmsghdr) + 20]
2220 __aligned(sizeof(__kernel_size_t));
2221 /* 20 is size of ipv6_pktinfo */
2222 unsigned char *ctl_buf = ctl;
2226 msg_sys->msg_name = &address;
2228 if (MSG_CMSG_COMPAT & flags)
2229 err = get_compat_msghdr(msg_sys, msg_compat, NULL, &iov);
2231 err = copy_msghdr_from_user(msg_sys, msg, NULL, &iov);
2237 if (msg_sys->msg_controllen > INT_MAX)
2239 flags |= (msg_sys->msg_flags & allowed_msghdr_flags);
2240 ctl_len = msg_sys->msg_controllen;
2241 if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
2243 cmsghdr_from_user_compat_to_kern(msg_sys, sock->sk, ctl,
2247 ctl_buf = msg_sys->msg_control;
2248 ctl_len = msg_sys->msg_controllen;
2249 } else if (ctl_len) {
2250 BUILD_BUG_ON(sizeof(struct cmsghdr) !=
2251 CMSG_ALIGN(sizeof(struct cmsghdr)));
2252 if (ctl_len > sizeof(ctl)) {
2253 ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
2254 if (ctl_buf == NULL)
2259 * Careful! Before this, msg_sys->msg_control contains a user pointer.
2260 * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
2261 * checking falls down on this.
2263 if (copy_from_user(ctl_buf,
2264 (void __user __force *)msg_sys->msg_control,
2267 msg_sys->msg_control = ctl_buf;
2269 msg_sys->msg_flags = flags;
2271 if (sock->file->f_flags & O_NONBLOCK)
2272 msg_sys->msg_flags |= MSG_DONTWAIT;
2274 * If this is sendmmsg() and current destination address is same as
2275 * previously succeeded address, omit asking LSM's decision.
2276 * used_address->name_len is initialized to UINT_MAX so that the first
2277 * destination address never matches.
2279 if (used_address && msg_sys->msg_name &&
2280 used_address->name_len == msg_sys->msg_namelen &&
2281 !memcmp(&used_address->name, msg_sys->msg_name,
2282 used_address->name_len)) {
2283 err = sock_sendmsg_nosec(sock, msg_sys);
2286 err = sock_sendmsg(sock, msg_sys);
2288 * If this is sendmmsg() and sending to current destination address was
2289 * successful, remember it.
2291 if (used_address && err >= 0) {
2292 used_address->name_len = msg_sys->msg_namelen;
2293 if (msg_sys->msg_name)
2294 memcpy(&used_address->name, msg_sys->msg_name,
2295 used_address->name_len);
2300 sock_kfree_s(sock->sk, ctl_buf, ctl_len);
2307 * BSD sendmsg interface
2310 long __sys_sendmsg(int fd, struct user_msghdr __user *msg, unsigned int flags,
2311 bool forbid_cmsg_compat)
2313 int fput_needed, err;
2314 struct msghdr msg_sys;
2315 struct socket *sock;
2317 if (forbid_cmsg_compat && (flags & MSG_CMSG_COMPAT))
2320 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2324 err = ___sys_sendmsg(sock, msg, &msg_sys, flags, NULL, 0);
2326 fput_light(sock->file, fput_needed);
2331 SYSCALL_DEFINE3(sendmsg, int, fd, struct user_msghdr __user *, msg, unsigned int, flags)
2333 return __sys_sendmsg(fd, msg, flags, true);
2337 * Linux sendmmsg interface
2340 int __sys_sendmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
2341 unsigned int flags, bool forbid_cmsg_compat)
2343 int fput_needed, err, datagrams;
2344 struct socket *sock;
2345 struct mmsghdr __user *entry;
2346 struct compat_mmsghdr __user *compat_entry;
2347 struct msghdr msg_sys;
2348 struct used_address used_address;
2349 unsigned int oflags = flags;
2351 if (forbid_cmsg_compat && (flags & MSG_CMSG_COMPAT))
2354 if (vlen > UIO_MAXIOV)
2359 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2363 used_address.name_len = UINT_MAX;
2365 compat_entry = (struct compat_mmsghdr __user *)mmsg;
2369 while (datagrams < vlen) {
2370 if (datagrams == vlen - 1)
2373 if (MSG_CMSG_COMPAT & flags) {
2374 err = ___sys_sendmsg(sock, (struct user_msghdr __user *)compat_entry,
2375 &msg_sys, flags, &used_address, MSG_EOR);
2378 err = __put_user(err, &compat_entry->msg_len);
2381 err = ___sys_sendmsg(sock,
2382 (struct user_msghdr __user *)entry,
2383 &msg_sys, flags, &used_address, MSG_EOR);
2386 err = put_user(err, &entry->msg_len);
2393 if (msg_data_left(&msg_sys))
2398 fput_light(sock->file, fput_needed);
2400 /* We only return an error if no datagrams were able to be sent */
2407 SYSCALL_DEFINE4(sendmmsg, int, fd, struct mmsghdr __user *, mmsg,
2408 unsigned int, vlen, unsigned int, flags)
2410 return __sys_sendmmsg(fd, mmsg, vlen, flags, true);
2413 static int ___sys_recvmsg(struct socket *sock, struct user_msghdr __user *msg,
2414 struct msghdr *msg_sys, unsigned int flags, int nosec)
2416 struct compat_msghdr __user *msg_compat =
2417 (struct compat_msghdr __user *)msg;
2418 struct iovec iovstack[UIO_FASTIOV];
2419 struct iovec *iov = iovstack;
2420 unsigned long cmsg_ptr;
2424 /* kernel mode address */
2425 struct sockaddr_storage addr;
2427 /* user mode address pointers */
2428 struct sockaddr __user *uaddr;
2429 int __user *uaddr_len = COMPAT_NAMELEN(msg);
2431 msg_sys->msg_name = &addr;
2433 if (MSG_CMSG_COMPAT & flags)
2434 err = get_compat_msghdr(msg_sys, msg_compat, &uaddr, &iov);
2436 err = copy_msghdr_from_user(msg_sys, msg, &uaddr, &iov);
2440 cmsg_ptr = (unsigned long)msg_sys->msg_control;
2441 msg_sys->msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT);
2443 /* We assume all kernel code knows the size of sockaddr_storage */
2444 msg_sys->msg_namelen = 0;
2446 if (sock->file->f_flags & O_NONBLOCK)
2447 flags |= MSG_DONTWAIT;
2448 err = (nosec ? sock_recvmsg_nosec : sock_recvmsg)(sock, msg_sys, flags);
2453 if (uaddr != NULL) {
2454 err = move_addr_to_user(&addr,
2455 msg_sys->msg_namelen, uaddr,
2460 err = __put_user((msg_sys->msg_flags & ~MSG_CMSG_COMPAT),
2464 if (MSG_CMSG_COMPAT & flags)
2465 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
2466 &msg_compat->msg_controllen);
2468 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
2469 &msg->msg_controllen);
2480 * BSD recvmsg interface
2483 long __sys_recvmsg(int fd, struct user_msghdr __user *msg, unsigned int flags,
2484 bool forbid_cmsg_compat)
2486 int fput_needed, err;
2487 struct msghdr msg_sys;
2488 struct socket *sock;
2490 if (forbid_cmsg_compat && (flags & MSG_CMSG_COMPAT))
2493 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2497 err = ___sys_recvmsg(sock, msg, &msg_sys, flags, 0);
2499 fput_light(sock->file, fput_needed);
2504 SYSCALL_DEFINE3(recvmsg, int, fd, struct user_msghdr __user *, msg,
2505 unsigned int, flags)
2507 return __sys_recvmsg(fd, msg, flags, true);
2511 * Linux recvmmsg interface
2514 static int do_recvmmsg(int fd, struct mmsghdr __user *mmsg,
2515 unsigned int vlen, unsigned int flags,
2516 struct timespec64 *timeout)
2518 int fput_needed, err, datagrams;
2519 struct socket *sock;
2520 struct mmsghdr __user *entry;
2521 struct compat_mmsghdr __user *compat_entry;
2522 struct msghdr msg_sys;
2523 struct timespec64 end_time;
2524 struct timespec64 timeout64;
2527 poll_select_set_timeout(&end_time, timeout->tv_sec,
2533 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2537 if (likely(!(flags & MSG_ERRQUEUE))) {
2538 err = sock_error(sock->sk);
2546 compat_entry = (struct compat_mmsghdr __user *)mmsg;
2548 while (datagrams < vlen) {
2550 * No need to ask LSM for more than the first datagram.
2552 if (MSG_CMSG_COMPAT & flags) {
2553 err = ___sys_recvmsg(sock, (struct user_msghdr __user *)compat_entry,
2554 &msg_sys, flags & ~MSG_WAITFORONE,
2558 err = __put_user(err, &compat_entry->msg_len);
2561 err = ___sys_recvmsg(sock,
2562 (struct user_msghdr __user *)entry,
2563 &msg_sys, flags & ~MSG_WAITFORONE,
2567 err = put_user(err, &entry->msg_len);
2575 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
2576 if (flags & MSG_WAITFORONE)
2577 flags |= MSG_DONTWAIT;
2580 ktime_get_ts64(&timeout64);
2581 *timeout = timespec64_sub(end_time, timeout64);
2582 if (timeout->tv_sec < 0) {
2583 timeout->tv_sec = timeout->tv_nsec = 0;
2587 /* Timeout, return less than vlen datagrams */
2588 if (timeout->tv_nsec == 0 && timeout->tv_sec == 0)
2592 /* Out of band data, return right away */
2593 if (msg_sys.msg_flags & MSG_OOB)
2601 if (datagrams == 0) {
2607 * We may return less entries than requested (vlen) if the
2608 * sock is non block and there aren't enough datagrams...
2610 if (err != -EAGAIN) {
2612 * ... or if recvmsg returns an error after we
2613 * received some datagrams, where we record the
2614 * error to return on the next call or if the
2615 * app asks about it using getsockopt(SO_ERROR).
2617 sock->sk->sk_err = -err;
2620 fput_light(sock->file, fput_needed);
2625 int __sys_recvmmsg(int fd, struct mmsghdr __user *mmsg,
2626 unsigned int vlen, unsigned int flags,
2627 struct __kernel_timespec __user *timeout,
2628 struct old_timespec32 __user *timeout32)
2631 struct timespec64 timeout_sys;
2633 if (timeout && get_timespec64(&timeout_sys, timeout))
2636 if (timeout32 && get_old_timespec32(&timeout_sys, timeout32))
2639 if (!timeout && !timeout32)
2640 return do_recvmmsg(fd, mmsg, vlen, flags, NULL);
2642 datagrams = do_recvmmsg(fd, mmsg, vlen, flags, &timeout_sys);
2647 if (timeout && put_timespec64(&timeout_sys, timeout))
2648 datagrams = -EFAULT;
2650 if (timeout32 && put_old_timespec32(&timeout_sys, timeout32))
2651 datagrams = -EFAULT;
2656 SYSCALL_DEFINE5(recvmmsg, int, fd, struct mmsghdr __user *, mmsg,
2657 unsigned int, vlen, unsigned int, flags,
2658 struct __kernel_timespec __user *, timeout)
2660 if (flags & MSG_CMSG_COMPAT)
2663 return __sys_recvmmsg(fd, mmsg, vlen, flags, timeout, NULL);
2666 #ifdef CONFIG_COMPAT_32BIT_TIME
2667 SYSCALL_DEFINE5(recvmmsg_time32, int, fd, struct mmsghdr __user *, mmsg,
2668 unsigned int, vlen, unsigned int, flags,
2669 struct old_timespec32 __user *, timeout)
2671 if (flags & MSG_CMSG_COMPAT)
2674 return __sys_recvmmsg(fd, mmsg, vlen, flags, NULL, timeout);
2678 #ifdef __ARCH_WANT_SYS_SOCKETCALL
2679 /* Argument list sizes for sys_socketcall */
2680 #define AL(x) ((x) * sizeof(unsigned long))
2681 static const unsigned char nargs[21] = {
2682 AL(0), AL(3), AL(3), AL(3), AL(2), AL(3),
2683 AL(3), AL(3), AL(4), AL(4), AL(4), AL(6),
2684 AL(6), AL(2), AL(5), AL(5), AL(3), AL(3),
2691 * System call vectors.
2693 * Argument checking cleaned up. Saved 20% in size.
2694 * This function doesn't need to set the kernel lock because
2695 * it is set by the callees.
2698 SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args)
2700 unsigned long a[AUDITSC_ARGS];
2701 unsigned long a0, a1;
2705 if (call < 1 || call > SYS_SENDMMSG)
2707 call = array_index_nospec(call, SYS_SENDMMSG + 1);
2710 if (len > sizeof(a))
2713 /* copy_from_user should be SMP safe. */
2714 if (copy_from_user(a, args, len))
2717 err = audit_socketcall(nargs[call] / sizeof(unsigned long), a);
2726 err = __sys_socket(a0, a1, a[2]);
2729 err = __sys_bind(a0, (struct sockaddr __user *)a1, a[2]);
2732 err = __sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
2735 err = __sys_listen(a0, a1);
2738 err = __sys_accept4(a0, (struct sockaddr __user *)a1,
2739 (int __user *)a[2], 0);
2741 case SYS_GETSOCKNAME:
2743 __sys_getsockname(a0, (struct sockaddr __user *)a1,
2744 (int __user *)a[2]);
2746 case SYS_GETPEERNAME:
2748 __sys_getpeername(a0, (struct sockaddr __user *)a1,
2749 (int __user *)a[2]);
2751 case SYS_SOCKETPAIR:
2752 err = __sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
2755 err = __sys_sendto(a0, (void __user *)a1, a[2], a[3],
2759 err = __sys_sendto(a0, (void __user *)a1, a[2], a[3],
2760 (struct sockaddr __user *)a[4], a[5]);
2763 err = __sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
2767 err = __sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
2768 (struct sockaddr __user *)a[4],
2769 (int __user *)a[5]);
2772 err = __sys_shutdown(a0, a1);
2774 case SYS_SETSOCKOPT:
2775 err = __sys_setsockopt(a0, a1, a[2], (char __user *)a[3],
2778 case SYS_GETSOCKOPT:
2780 __sys_getsockopt(a0, a1, a[2], (char __user *)a[3],
2781 (int __user *)a[4]);
2784 err = __sys_sendmsg(a0, (struct user_msghdr __user *)a1,
2788 err = __sys_sendmmsg(a0, (struct mmsghdr __user *)a1, a[2],
2792 err = __sys_recvmsg(a0, (struct user_msghdr __user *)a1,
2796 if (IS_ENABLED(CONFIG_64BIT) || !IS_ENABLED(CONFIG_64BIT_TIME))
2797 err = __sys_recvmmsg(a0, (struct mmsghdr __user *)a1,
2799 (struct __kernel_timespec __user *)a[4],
2802 err = __sys_recvmmsg(a0, (struct mmsghdr __user *)a1,
2804 (struct old_timespec32 __user *)a[4]);
2807 err = __sys_accept4(a0, (struct sockaddr __user *)a1,
2808 (int __user *)a[2], a[3]);
2817 #endif /* __ARCH_WANT_SYS_SOCKETCALL */
2820 * sock_register - add a socket protocol handler
2821 * @ops: description of protocol
2823 * This function is called by a protocol handler that wants to
2824 * advertise its address family, and have it linked into the
2825 * socket interface. The value ops->family corresponds to the
2826 * socket system call protocol family.
2828 int sock_register(const struct net_proto_family *ops)
2832 if (ops->family >= NPROTO) {
2833 pr_crit("protocol %d >= NPROTO(%d)\n", ops->family, NPROTO);
2837 spin_lock(&net_family_lock);
2838 if (rcu_dereference_protected(net_families[ops->family],
2839 lockdep_is_held(&net_family_lock)))
2842 rcu_assign_pointer(net_families[ops->family], ops);
2845 spin_unlock(&net_family_lock);
2847 pr_info("NET: Registered protocol family %d\n", ops->family);
2850 EXPORT_SYMBOL(sock_register);
2853 * sock_unregister - remove a protocol handler
2854 * @family: protocol family to remove
2856 * This function is called by a protocol handler that wants to
2857 * remove its address family, and have it unlinked from the
2858 * new socket creation.
2860 * If protocol handler is a module, then it can use module reference
2861 * counts to protect against new references. If protocol handler is not
2862 * a module then it needs to provide its own protection in
2863 * the ops->create routine.
2865 void sock_unregister(int family)
2867 BUG_ON(family < 0 || family >= NPROTO);
2869 spin_lock(&net_family_lock);
2870 RCU_INIT_POINTER(net_families[family], NULL);
2871 spin_unlock(&net_family_lock);
2875 pr_info("NET: Unregistered protocol family %d\n", family);
2877 EXPORT_SYMBOL(sock_unregister);
2879 bool sock_is_registered(int family)
2881 return family < NPROTO && rcu_access_pointer(net_families[family]);
2884 static int __init sock_init(void)
2888 * Initialize the network sysctl infrastructure.
2890 err = net_sysctl_init();
2895 * Initialize skbuff SLAB cache
2900 * Initialize the protocols module.
2905 err = register_filesystem(&sock_fs_type);
2908 sock_mnt = kern_mount(&sock_fs_type);
2909 if (IS_ERR(sock_mnt)) {
2910 err = PTR_ERR(sock_mnt);
2914 /* The real protocol initialization is performed in later initcalls.
2917 #ifdef CONFIG_NETFILTER
2918 err = netfilter_init();
2923 ptp_classifier_init();
2929 unregister_filesystem(&sock_fs_type);
2934 core_initcall(sock_init); /* early initcall */
2936 #ifdef CONFIG_PROC_FS
2937 void socket_seq_show(struct seq_file *seq)
2939 seq_printf(seq, "sockets: used %d\n",
2940 sock_inuse_get(seq->private));
2942 #endif /* CONFIG_PROC_FS */
2944 #ifdef CONFIG_COMPAT
2945 static int compat_dev_ifconf(struct net *net, struct compat_ifconf __user *uifc32)
2947 struct compat_ifconf ifc32;
2951 if (copy_from_user(&ifc32, uifc32, sizeof(struct compat_ifconf)))
2954 ifc.ifc_len = ifc32.ifc_len;
2955 ifc.ifc_req = compat_ptr(ifc32.ifcbuf);
2958 err = dev_ifconf(net, &ifc, sizeof(struct compat_ifreq));
2963 ifc32.ifc_len = ifc.ifc_len;
2964 if (copy_to_user(uifc32, &ifc32, sizeof(struct compat_ifconf)))
2970 static int ethtool_ioctl(struct net *net, struct compat_ifreq __user *ifr32)
2972 struct compat_ethtool_rxnfc __user *compat_rxnfc;
2973 bool convert_in = false, convert_out = false;
2974 size_t buf_size = 0;
2975 struct ethtool_rxnfc __user *rxnfc = NULL;
2977 u32 rule_cnt = 0, actual_rule_cnt;
2982 if (get_user(data, &ifr32->ifr_ifru.ifru_data))
2985 compat_rxnfc = compat_ptr(data);
2987 if (get_user(ethcmd, &compat_rxnfc->cmd))
2990 /* Most ethtool structures are defined without padding.
2991 * Unfortunately struct ethtool_rxnfc is an exception.
2996 case ETHTOOL_GRXCLSRLALL:
2997 /* Buffer size is variable */
2998 if (get_user(rule_cnt, &compat_rxnfc->rule_cnt))
3000 if (rule_cnt > KMALLOC_MAX_SIZE / sizeof(u32))
3002 buf_size += rule_cnt * sizeof(u32);
3004 case ETHTOOL_GRXRINGS:
3005 case ETHTOOL_GRXCLSRLCNT:
3006 case ETHTOOL_GRXCLSRULE:
3007 case ETHTOOL_SRXCLSRLINS:
3010 case ETHTOOL_SRXCLSRLDEL:
3011 buf_size += sizeof(struct ethtool_rxnfc);
3013 rxnfc = compat_alloc_user_space(buf_size);
3017 if (copy_from_user(&ifr.ifr_name, &ifr32->ifr_name, IFNAMSIZ))
3020 ifr.ifr_data = convert_in ? rxnfc : (void __user *)compat_rxnfc;
3023 /* We expect there to be holes between fs.m_ext and
3024 * fs.ring_cookie and at the end of fs, but nowhere else.
3026 BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc, fs.m_ext) +
3027 sizeof(compat_rxnfc->fs.m_ext) !=
3028 offsetof(struct ethtool_rxnfc, fs.m_ext) +
3029 sizeof(rxnfc->fs.m_ext));
3031 offsetof(struct compat_ethtool_rxnfc, fs.location) -
3032 offsetof(struct compat_ethtool_rxnfc, fs.ring_cookie) !=
3033 offsetof(struct ethtool_rxnfc, fs.location) -
3034 offsetof(struct ethtool_rxnfc, fs.ring_cookie));
3036 if (copy_in_user(rxnfc, compat_rxnfc,
3037 (void __user *)(&rxnfc->fs.m_ext + 1) -
3038 (void __user *)rxnfc) ||
3039 copy_in_user(&rxnfc->fs.ring_cookie,
3040 &compat_rxnfc->fs.ring_cookie,
3041 (void __user *)(&rxnfc->fs.location + 1) -
3042 (void __user *)&rxnfc->fs.ring_cookie))
3044 if (ethcmd == ETHTOOL_GRXCLSRLALL) {
3045 if (put_user(rule_cnt, &rxnfc->rule_cnt))
3047 } else if (copy_in_user(&rxnfc->rule_cnt,
3048 &compat_rxnfc->rule_cnt,
3049 sizeof(rxnfc->rule_cnt)))
3053 ret = dev_ioctl(net, SIOCETHTOOL, &ifr, NULL);
3058 if (copy_in_user(compat_rxnfc, rxnfc,
3059 (const void __user *)(&rxnfc->fs.m_ext + 1) -
3060 (const void __user *)rxnfc) ||
3061 copy_in_user(&compat_rxnfc->fs.ring_cookie,
3062 &rxnfc->fs.ring_cookie,
3063 (const void __user *)(&rxnfc->fs.location + 1) -
3064 (const void __user *)&rxnfc->fs.ring_cookie) ||
3065 copy_in_user(&compat_rxnfc->rule_cnt, &rxnfc->rule_cnt,
3066 sizeof(rxnfc->rule_cnt)))
3069 if (ethcmd == ETHTOOL_GRXCLSRLALL) {
3070 /* As an optimisation, we only copy the actual
3071 * number of rules that the underlying
3072 * function returned. Since Mallory might
3073 * change the rule count in user memory, we
3074 * check that it is less than the rule count
3075 * originally given (as the user buffer size),
3076 * which has been range-checked.
3078 if (get_user(actual_rule_cnt, &rxnfc->rule_cnt))
3080 if (actual_rule_cnt < rule_cnt)
3081 rule_cnt = actual_rule_cnt;
3082 if (copy_in_user(&compat_rxnfc->rule_locs[0],
3083 &rxnfc->rule_locs[0],
3084 rule_cnt * sizeof(u32)))
3092 static int compat_siocwandev(struct net *net, struct compat_ifreq __user *uifr32)
3094 compat_uptr_t uptr32;
3099 if (copy_from_user(&ifr, uifr32, sizeof(struct compat_ifreq)))
3102 if (get_user(uptr32, &uifr32->ifr_settings.ifs_ifsu))
3105 saved = ifr.ifr_settings.ifs_ifsu.raw_hdlc;
3106 ifr.ifr_settings.ifs_ifsu.raw_hdlc = compat_ptr(uptr32);
3108 err = dev_ioctl(net, SIOCWANDEV, &ifr, NULL);
3110 ifr.ifr_settings.ifs_ifsu.raw_hdlc = saved;
3111 if (copy_to_user(uifr32, &ifr, sizeof(struct compat_ifreq)))
3117 /* Handle ioctls that use ifreq::ifr_data and just need struct ifreq converted */
3118 static int compat_ifr_data_ioctl(struct net *net, unsigned int cmd,
3119 struct compat_ifreq __user *u_ifreq32)
3124 if (copy_from_user(ifreq.ifr_name, u_ifreq32->ifr_name, IFNAMSIZ))
3126 if (get_user(data32, &u_ifreq32->ifr_data))
3128 ifreq.ifr_data = compat_ptr(data32);
3130 return dev_ioctl(net, cmd, &ifreq, NULL);
3133 static int compat_ifreq_ioctl(struct net *net, struct socket *sock,
3135 struct compat_ifreq __user *uifr32)
3137 struct ifreq __user *uifr;
3140 /* Handle the fact that while struct ifreq has the same *layout* on
3141 * 32/64 for everything but ifreq::ifru_ifmap and ifreq::ifru_data,
3142 * which are handled elsewhere, it still has different *size* due to
3143 * ifreq::ifru_ifmap (which is 16 bytes on 32 bit, 24 bytes on 64-bit,
3144 * resulting in struct ifreq being 32 and 40 bytes respectively).
3145 * As a result, if the struct happens to be at the end of a page and
3146 * the next page isn't readable/writable, we get a fault. To prevent
3147 * that, copy back and forth to the full size.
3150 uifr = compat_alloc_user_space(sizeof(*uifr));
3151 if (copy_in_user(uifr, uifr32, sizeof(*uifr32)))
3154 err = sock_do_ioctl(net, sock, cmd, (unsigned long)uifr);
3165 case SIOCGIFBRDADDR:
3166 case SIOCGIFDSTADDR:
3167 case SIOCGIFNETMASK:
3173 if (copy_in_user(uifr32, uifr, sizeof(*uifr32)))
3181 static int compat_sioc_ifmap(struct net *net, unsigned int cmd,
3182 struct compat_ifreq __user *uifr32)
3185 struct compat_ifmap __user *uifmap32;
3188 uifmap32 = &uifr32->ifr_ifru.ifru_map;
3189 err = copy_from_user(&ifr, uifr32, sizeof(ifr.ifr_name));
3190 err |= get_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
3191 err |= get_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
3192 err |= get_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
3193 err |= get_user(ifr.ifr_map.irq, &uifmap32->irq);
3194 err |= get_user(ifr.ifr_map.dma, &uifmap32->dma);
3195 err |= get_user(ifr.ifr_map.port, &uifmap32->port);
3199 err = dev_ioctl(net, cmd, &ifr, NULL);
3201 if (cmd == SIOCGIFMAP && !err) {
3202 err = copy_to_user(uifr32, &ifr, sizeof(ifr.ifr_name));
3203 err |= put_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
3204 err |= put_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
3205 err |= put_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
3206 err |= put_user(ifr.ifr_map.irq, &uifmap32->irq);
3207 err |= put_user(ifr.ifr_map.dma, &uifmap32->dma);
3208 err |= put_user(ifr.ifr_map.port, &uifmap32->port);
3217 struct sockaddr rt_dst; /* target address */
3218 struct sockaddr rt_gateway; /* gateway addr (RTF_GATEWAY) */
3219 struct sockaddr rt_genmask; /* target network mask (IP) */
3220 unsigned short rt_flags;
3223 unsigned char rt_tos;
3224 unsigned char rt_class;
3226 short rt_metric; /* +1 for binary compatibility! */
3227 /* char * */ u32 rt_dev; /* forcing the device at add */
3228 u32 rt_mtu; /* per route MTU/Window */
3229 u32 rt_window; /* Window clamping */
3230 unsigned short rt_irtt; /* Initial RTT */
3233 struct in6_rtmsg32 {
3234 struct in6_addr rtmsg_dst;
3235 struct in6_addr rtmsg_src;
3236 struct in6_addr rtmsg_gateway;
3246 static int routing_ioctl(struct net *net, struct socket *sock,
3247 unsigned int cmd, void __user *argp)
3251 struct in6_rtmsg r6;
3255 mm_segment_t old_fs = get_fs();
3257 if (sock && sock->sk && sock->sk->sk_family == AF_INET6) { /* ipv6 */
3258 struct in6_rtmsg32 __user *ur6 = argp;
3259 ret = copy_from_user(&r6.rtmsg_dst, &(ur6->rtmsg_dst),
3260 3 * sizeof(struct in6_addr));
3261 ret |= get_user(r6.rtmsg_type, &(ur6->rtmsg_type));
3262 ret |= get_user(r6.rtmsg_dst_len, &(ur6->rtmsg_dst_len));
3263 ret |= get_user(r6.rtmsg_src_len, &(ur6->rtmsg_src_len));
3264 ret |= get_user(r6.rtmsg_metric, &(ur6->rtmsg_metric));
3265 ret |= get_user(r6.rtmsg_info, &(ur6->rtmsg_info));
3266 ret |= get_user(r6.rtmsg_flags, &(ur6->rtmsg_flags));
3267 ret |= get_user(r6.rtmsg_ifindex, &(ur6->rtmsg_ifindex));
3271 struct rtentry32 __user *ur4 = argp;
3272 ret = copy_from_user(&r4.rt_dst, &(ur4->rt_dst),
3273 3 * sizeof(struct sockaddr));
3274 ret |= get_user(r4.rt_flags, &(ur4->rt_flags));
3275 ret |= get_user(r4.rt_metric, &(ur4->rt_metric));
3276 ret |= get_user(r4.rt_mtu, &(ur4->rt_mtu));
3277 ret |= get_user(r4.rt_window, &(ur4->rt_window));
3278 ret |= get_user(r4.rt_irtt, &(ur4->rt_irtt));
3279 ret |= get_user(rtdev, &(ur4->rt_dev));
3281 ret |= copy_from_user(devname, compat_ptr(rtdev), 15);
3282 r4.rt_dev = (char __user __force *)devname;
3296 ret = sock_do_ioctl(net, sock, cmd, (unsigned long) r);
3303 /* Since old style bridge ioctl's endup using SIOCDEVPRIVATE
3304 * for some operations; this forces use of the newer bridge-utils that
3305 * use compatible ioctls
3307 static int old_bridge_ioctl(compat_ulong_t __user *argp)
3311 if (get_user(tmp, argp))
3313 if (tmp == BRCTL_GET_VERSION)
3314 return BRCTL_VERSION + 1;
3318 static int compat_sock_ioctl_trans(struct file *file, struct socket *sock,
3319 unsigned int cmd, unsigned long arg)
3321 void __user *argp = compat_ptr(arg);
3322 struct sock *sk = sock->sk;
3323 struct net *net = sock_net(sk);
3325 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15))
3326 return compat_ifr_data_ioctl(net, cmd, argp);
3331 return old_bridge_ioctl(argp);
3333 return compat_dev_ifconf(net, argp);
3335 return ethtool_ioctl(net, argp);
3337 return compat_siocwandev(net, argp);
3340 return compat_sioc_ifmap(net, cmd, argp);
3343 return routing_ioctl(net, sock, cmd, argp);
3344 case SIOCGSTAMP_OLD:
3345 case SIOCGSTAMPNS_OLD:
3346 if (!sock->ops->gettstamp)
3347 return -ENOIOCTLCMD;
3348 return sock->ops->gettstamp(sock, argp, cmd == SIOCGSTAMP_OLD,
3349 !COMPAT_USE_64BIT_TIME);
3351 case SIOCBONDSLAVEINFOQUERY:
3352 case SIOCBONDINFOQUERY:
3355 return compat_ifr_data_ioctl(net, cmd, argp);
3368 case SIOCGSTAMP_NEW:
3369 case SIOCGSTAMPNS_NEW:
3370 return sock_ioctl(file, cmd, arg);
3387 case SIOCSIFHWBROADCAST:
3389 case SIOCGIFBRDADDR:
3390 case SIOCSIFBRDADDR:
3391 case SIOCGIFDSTADDR:
3392 case SIOCSIFDSTADDR:
3393 case SIOCGIFNETMASK:
3394 case SIOCSIFNETMASK:
3406 case SIOCBONDENSLAVE:
3407 case SIOCBONDRELEASE:
3408 case SIOCBONDSETHWADDR:
3409 case SIOCBONDCHANGEACTIVE:
3410 return compat_ifreq_ioctl(net, sock, cmd, argp);
3416 return sock_do_ioctl(net, sock, cmd, arg);
3419 return -ENOIOCTLCMD;
3422 static long compat_sock_ioctl(struct file *file, unsigned int cmd,
3425 struct socket *sock = file->private_data;
3426 int ret = -ENOIOCTLCMD;
3433 if (sock->ops->compat_ioctl)
3434 ret = sock->ops->compat_ioctl(sock, cmd, arg);
3436 if (ret == -ENOIOCTLCMD &&
3437 (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST))
3438 ret = compat_wext_handle_ioctl(net, cmd, arg);
3440 if (ret == -ENOIOCTLCMD)
3441 ret = compat_sock_ioctl_trans(file, sock, cmd, arg);
3448 * kernel_bind - bind an address to a socket (kernel space)
3451 * @addrlen: length of address
3453 * Returns 0 or an error.
3456 int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen)
3458 return sock->ops->bind(sock, addr, addrlen);
3460 EXPORT_SYMBOL(kernel_bind);
3463 * kernel_listen - move socket to listening state (kernel space)
3465 * @backlog: pending connections queue size
3467 * Returns 0 or an error.
3470 int kernel_listen(struct socket *sock, int backlog)
3472 return sock->ops->listen(sock, backlog);
3474 EXPORT_SYMBOL(kernel_listen);
3477 * kernel_accept - accept a connection (kernel space)
3478 * @sock: listening socket
3479 * @newsock: new connected socket
3482 * @flags must be SOCK_CLOEXEC, SOCK_NONBLOCK or 0.
3483 * If it fails, @newsock is guaranteed to be %NULL.
3484 * Returns 0 or an error.
3487 int kernel_accept(struct socket *sock, struct socket **newsock, int flags)
3489 struct sock *sk = sock->sk;
3492 err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
3497 err = sock->ops->accept(sock, *newsock, flags, true);
3499 sock_release(*newsock);
3504 (*newsock)->ops = sock->ops;
3505 __module_get((*newsock)->ops->owner);
3510 EXPORT_SYMBOL(kernel_accept);
3513 * kernel_connect - connect a socket (kernel space)
3516 * @addrlen: address length
3517 * @flags: flags (O_NONBLOCK, ...)
3519 * For datagram sockets, @addr is the addres to which datagrams are sent
3520 * by default, and the only address from which datagrams are received.
3521 * For stream sockets, attempts to connect to @addr.
3522 * Returns 0 or an error code.
3525 int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
3528 return sock->ops->connect(sock, addr, addrlen, flags);
3530 EXPORT_SYMBOL(kernel_connect);
3533 * kernel_getsockname - get the address which the socket is bound (kernel space)
3535 * @addr: address holder
3537 * Fills the @addr pointer with the address which the socket is bound.
3538 * Returns 0 or an error code.
3541 int kernel_getsockname(struct socket *sock, struct sockaddr *addr)
3543 return sock->ops->getname(sock, addr, 0);
3545 EXPORT_SYMBOL(kernel_getsockname);
3548 * kernel_peername - get the address which the socket is connected (kernel space)
3550 * @addr: address holder
3552 * Fills the @addr pointer with the address which the socket is connected.
3553 * Returns 0 or an error code.
3556 int kernel_getpeername(struct socket *sock, struct sockaddr *addr)
3558 return sock->ops->getname(sock, addr, 1);
3560 EXPORT_SYMBOL(kernel_getpeername);
3563 * kernel_getsockopt - get a socket option (kernel space)
3565 * @level: API level (SOL_SOCKET, ...)
3566 * @optname: option tag
3567 * @optval: option value
3568 * @optlen: option length
3570 * Assigns the option length to @optlen.
3571 * Returns 0 or an error.
3574 int kernel_getsockopt(struct socket *sock, int level, int optname,
3575 char *optval, int *optlen)
3577 mm_segment_t oldfs = get_fs();
3578 char __user *uoptval;
3579 int __user *uoptlen;
3582 uoptval = (char __user __force *) optval;
3583 uoptlen = (int __user __force *) optlen;
3586 if (level == SOL_SOCKET)
3587 err = sock_getsockopt(sock, level, optname, uoptval, uoptlen);
3589 err = sock->ops->getsockopt(sock, level, optname, uoptval,
3594 EXPORT_SYMBOL(kernel_getsockopt);
3597 * kernel_setsockopt - set a socket option (kernel space)
3599 * @level: API level (SOL_SOCKET, ...)
3600 * @optname: option tag
3601 * @optval: option value
3602 * @optlen: option length
3604 * Returns 0 or an error.
3607 int kernel_setsockopt(struct socket *sock, int level, int optname,
3608 char *optval, unsigned int optlen)
3610 mm_segment_t oldfs = get_fs();
3611 char __user *uoptval;
3614 uoptval = (char __user __force *) optval;
3617 if (level == SOL_SOCKET)
3618 err = sock_setsockopt(sock, level, optname, uoptval, optlen);
3620 err = sock->ops->setsockopt(sock, level, optname, uoptval,
3625 EXPORT_SYMBOL(kernel_setsockopt);
3628 * kernel_sendpage - send a &page through a socket (kernel space)
3631 * @offset: page offset
3632 * @size: total size in bytes
3633 * @flags: flags (MSG_DONTWAIT, ...)
3635 * Returns the total amount sent in bytes or an error.
3638 int kernel_sendpage(struct socket *sock, struct page *page, int offset,
3639 size_t size, int flags)
3641 if (sock->ops->sendpage)
3642 return sock->ops->sendpage(sock, page, offset, size, flags);
3644 return sock_no_sendpage(sock, page, offset, size, flags);
3646 EXPORT_SYMBOL(kernel_sendpage);
3649 * kernel_sendpage_locked - send a &page through the locked sock (kernel space)
3652 * @offset: page offset
3653 * @size: total size in bytes
3654 * @flags: flags (MSG_DONTWAIT, ...)
3656 * Returns the total amount sent in bytes or an error.
3657 * Caller must hold @sk.
3660 int kernel_sendpage_locked(struct sock *sk, struct page *page, int offset,
3661 size_t size, int flags)
3663 struct socket *sock = sk->sk_socket;
3665 if (sock->ops->sendpage_locked)
3666 return sock->ops->sendpage_locked(sk, page, offset, size,
3669 return sock_no_sendpage_locked(sk, page, offset, size, flags);
3671 EXPORT_SYMBOL(kernel_sendpage_locked);
3674 * kernel_shutdown - shut down part of a full-duplex connection (kernel space)
3676 * @how: connection part
3678 * Returns 0 or an error.
3681 int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how)
3683 return sock->ops->shutdown(sock, how);
3685 EXPORT_SYMBOL(kernel_sock_shutdown);
3688 * kernel_sock_ip_overhead - returns the IP overhead imposed by a socket
3691 * This routine returns the IP overhead imposed by a socket i.e.
3692 * the length of the underlying IP header, depending on whether
3693 * this is an IPv4 or IPv6 socket and the length from IP options turned
3694 * on at the socket. Assumes that the caller has a lock on the socket.
3697 u32 kernel_sock_ip_overhead(struct sock *sk)
3699 struct inet_sock *inet;
3700 struct ip_options_rcu *opt;
3702 #if IS_ENABLED(CONFIG_IPV6)
3703 struct ipv6_pinfo *np;
3704 struct ipv6_txoptions *optv6 = NULL;
3705 #endif /* IS_ENABLED(CONFIG_IPV6) */
3710 switch (sk->sk_family) {
3713 overhead += sizeof(struct iphdr);
3714 opt = rcu_dereference_protected(inet->inet_opt,
3715 sock_owned_by_user(sk));
3717 overhead += opt->opt.optlen;
3719 #if IS_ENABLED(CONFIG_IPV6)
3722 overhead += sizeof(struct ipv6hdr);
3724 optv6 = rcu_dereference_protected(np->opt,
3725 sock_owned_by_user(sk));
3727 overhead += (optv6->opt_flen + optv6->opt_nflen);
3729 #endif /* IS_ENABLED(CONFIG_IPV6) */
3730 default: /* Returns 0 overhead if the socket is not ipv4 or ipv6 */
3734 EXPORT_SYMBOL(kernel_sock_ip_overhead);