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
7 * Authors: Orest Zborowski, <obz@Kodak.COM>
9 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
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
55 #include <linux/ethtool.h>
57 #include <linux/socket.h>
58 #include <linux/file.h>
59 #include <linux/net.h>
60 #include <linux/interrupt.h>
61 #include <linux/thread_info.h>
62 #include <linux/rcupdate.h>
63 #include <linux/netdevice.h>
64 #include <linux/proc_fs.h>
65 #include <linux/seq_file.h>
66 #include <linux/mutex.h>
67 #include <linux/if_bridge.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/pseudo_fs.h>
77 #include <linux/security.h>
78 #include <linux/syscalls.h>
79 #include <linux/compat.h>
80 #include <linux/kmod.h>
81 #include <linux/audit.h>
82 #include <linux/wireless.h>
83 #include <linux/nsproxy.h>
84 #include <linux/magic.h>
85 #include <linux/slab.h>
86 #include <linux/xattr.h>
87 #include <linux/nospec.h>
88 #include <linux/indirect_call_wrapper.h>
90 #include <linux/uaccess.h>
91 #include <asm/unistd.h>
93 #include <net/compat.h>
95 #include <net/cls_cgroup.h>
98 #include <linux/netfilter.h>
100 #include <linux/if_tun.h>
101 #include <linux/ipv6_route.h>
102 #include <linux/route.h>
103 #include <linux/termios.h>
104 #include <linux/sockios.h>
105 #include <net/busy_poll.h>
106 #include <linux/errqueue.h>
108 #ifdef CONFIG_NET_RX_BUSY_POLL
109 unsigned int sysctl_net_busy_read __read_mostly;
110 unsigned int sysctl_net_busy_poll __read_mostly;
113 static ssize_t sock_read_iter(struct kiocb *iocb, struct iov_iter *to);
114 static ssize_t sock_write_iter(struct kiocb *iocb, struct iov_iter *from);
115 static int sock_mmap(struct file *file, struct vm_area_struct *vma);
117 static int sock_close(struct inode *inode, struct file *file);
118 static __poll_t sock_poll(struct file *file,
119 struct poll_table_struct *wait);
120 static long sock_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
122 static long compat_sock_ioctl(struct file *file,
123 unsigned int cmd, unsigned long arg);
125 static int sock_fasync(int fd, struct file *filp, int on);
126 static ssize_t sock_sendpage(struct file *file, struct page *page,
127 int offset, size_t size, loff_t *ppos, int more);
128 static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
129 struct pipe_inode_info *pipe, size_t len,
132 #ifdef CONFIG_PROC_FS
133 static void sock_show_fdinfo(struct seq_file *m, struct file *f)
135 struct socket *sock = f->private_data;
137 if (sock->ops->show_fdinfo)
138 sock->ops->show_fdinfo(m, sock);
141 #define sock_show_fdinfo NULL
145 * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
146 * in the operation structures but are done directly via the socketcall() multiplexor.
149 static const struct file_operations socket_file_ops = {
150 .owner = THIS_MODULE,
152 .read_iter = sock_read_iter,
153 .write_iter = sock_write_iter,
155 .unlocked_ioctl = sock_ioctl,
157 .compat_ioctl = compat_sock_ioctl,
160 .release = sock_close,
161 .fasync = sock_fasync,
162 .sendpage = sock_sendpage,
163 .splice_write = generic_splice_sendpage,
164 .splice_read = sock_splice_read,
165 .show_fdinfo = sock_show_fdinfo,
169 * The protocol list. Each protocol is registered in here.
172 static DEFINE_SPINLOCK(net_family_lock);
173 static const struct net_proto_family __rcu *net_families[NPROTO] __read_mostly;
177 * Move socket addresses back and forth across the kernel/user
178 * divide and look after the messy bits.
182 * move_addr_to_kernel - copy a socket address into kernel space
183 * @uaddr: Address in user space
184 * @kaddr: Address in kernel space
185 * @ulen: Length in user space
187 * The address is copied into kernel space. If the provided address is
188 * too long an error code of -EINVAL is returned. If the copy gives
189 * invalid addresses -EFAULT is returned. On a success 0 is returned.
192 int move_addr_to_kernel(void __user *uaddr, int ulen, struct sockaddr_storage *kaddr)
194 if (ulen < 0 || ulen > sizeof(struct sockaddr_storage))
198 if (copy_from_user(kaddr, uaddr, ulen))
200 return audit_sockaddr(ulen, kaddr);
204 * move_addr_to_user - copy an address to user space
205 * @kaddr: kernel space address
206 * @klen: length of address in kernel
207 * @uaddr: user space address
208 * @ulen: pointer to user length field
210 * The value pointed to by ulen on entry is the buffer length available.
211 * This is overwritten with the buffer space used. -EINVAL is returned
212 * if an overlong buffer is specified or a negative buffer size. -EFAULT
213 * is returned if either the buffer or the length field are not
215 * After copying the data up to the limit the user specifies, the true
216 * length of the data is written over the length limit the user
217 * specified. Zero is returned for a success.
220 static int move_addr_to_user(struct sockaddr_storage *kaddr, int klen,
221 void __user *uaddr, int __user *ulen)
226 BUG_ON(klen > sizeof(struct sockaddr_storage));
227 err = get_user(len, ulen);
235 if (audit_sockaddr(klen, kaddr))
237 if (copy_to_user(uaddr, kaddr, len))
241 * "fromlen shall refer to the value before truncation.."
244 return __put_user(klen, ulen);
247 static struct kmem_cache *sock_inode_cachep __ro_after_init;
249 static struct inode *sock_alloc_inode(struct super_block *sb)
251 struct socket_alloc *ei;
253 ei = kmem_cache_alloc(sock_inode_cachep, GFP_KERNEL);
256 init_waitqueue_head(&ei->socket.wq.wait);
257 ei->socket.wq.fasync_list = NULL;
258 ei->socket.wq.flags = 0;
260 ei->socket.state = SS_UNCONNECTED;
261 ei->socket.flags = 0;
262 ei->socket.ops = NULL;
263 ei->socket.sk = NULL;
264 ei->socket.file = NULL;
266 return &ei->vfs_inode;
269 static void sock_free_inode(struct inode *inode)
271 struct socket_alloc *ei;
273 ei = container_of(inode, struct socket_alloc, vfs_inode);
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 .free_inode = sock_free_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 int sockfs_init_fs_context(struct fs_context *fc)
358 struct pseudo_fs_context *ctx = init_pseudo(fc, SOCKFS_MAGIC);
361 ctx->ops = &sockfs_ops;
362 ctx->dops = &sockfs_dentry_operations;
363 ctx->xattr = sockfs_xattr_handlers;
367 static struct vfsmount *sock_mnt __read_mostly;
369 static struct file_system_type sock_fs_type = {
371 .init_fs_context = sockfs_init_fs_context,
372 .kill_sb = kill_anon_super,
376 * Obtains the first available file descriptor and sets it up for use.
378 * These functions create file structures and maps them to fd space
379 * of the current process. On success it returns file descriptor
380 * and file struct implicitly stored in sock->file.
381 * Note that another thread may close file descriptor before we return
382 * from this function. We use the fact that now we do not refer
383 * to socket after mapping. If one day we will need it, this
384 * function will increment ref. count on file by 1.
386 * In any case returned fd MAY BE not valid!
387 * This race condition is unavoidable
388 * with shared fd spaces, we cannot solve it inside kernel,
389 * but we take care of internal coherence yet.
393 * sock_alloc_file - Bind a &socket to a &file
395 * @flags: file status flags
396 * @dname: protocol name
398 * Returns the &file bound with @sock, implicitly storing it
399 * in sock->file. If dname is %NULL, sets to "".
400 * On failure the return is a ERR pointer (see linux/err.h).
401 * This function uses GFP_KERNEL internally.
404 struct file *sock_alloc_file(struct socket *sock, int flags, const char *dname)
409 dname = sock->sk ? sock->sk->sk_prot_creator->name : "";
411 file = alloc_file_pseudo(SOCK_INODE(sock), sock_mnt, dname,
412 O_RDWR | (flags & O_NONBLOCK),
420 file->private_data = sock;
421 stream_open(SOCK_INODE(sock), file);
424 EXPORT_SYMBOL(sock_alloc_file);
426 static int sock_map_fd(struct socket *sock, int flags)
428 struct file *newfile;
429 int fd = get_unused_fd_flags(flags);
430 if (unlikely(fd < 0)) {
435 newfile = sock_alloc_file(sock, flags, NULL);
436 if (!IS_ERR(newfile)) {
437 fd_install(fd, newfile);
442 return PTR_ERR(newfile);
446 * sock_from_file - Return the &socket bounded to @file.
449 * On failure returns %NULL.
452 struct socket *sock_from_file(struct file *file)
454 if (file->f_op == &socket_file_ops)
455 return file->private_data; /* set in sock_map_fd */
459 EXPORT_SYMBOL(sock_from_file);
462 * sockfd_lookup - Go from a file number to its socket slot
464 * @err: pointer to an error code return
466 * The file handle passed in is locked and the socket it is bound
467 * to is returned. If an error occurs the err pointer is overwritten
468 * with a negative errno code and NULL is returned. The function checks
469 * for both invalid handles and passing a handle which is not a socket.
471 * On a success the socket object pointer is returned.
474 struct socket *sockfd_lookup(int fd, int *err)
485 sock = sock_from_file(file);
492 EXPORT_SYMBOL(sockfd_lookup);
494 static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed)
496 struct fd f = fdget(fd);
501 sock = sock_from_file(f.file);
503 *fput_needed = f.flags & FDPUT_FPUT;
512 static ssize_t sockfs_listxattr(struct dentry *dentry, char *buffer,
518 len = security_inode_listsecurity(d_inode(dentry), buffer, size);
528 len = (XATTR_NAME_SOCKPROTONAME_LEN + 1);
533 memcpy(buffer, XATTR_NAME_SOCKPROTONAME, len);
540 static int sockfs_setattr(struct dentry *dentry, struct iattr *iattr)
542 int err = simple_setattr(dentry, iattr);
544 if (!err && (iattr->ia_valid & ATTR_UID)) {
545 struct socket *sock = SOCKET_I(d_inode(dentry));
548 sock->sk->sk_uid = iattr->ia_uid;
556 static const struct inode_operations sockfs_inode_ops = {
557 .listxattr = sockfs_listxattr,
558 .setattr = sockfs_setattr,
562 * sock_alloc - allocate a socket
564 * Allocate a new inode and socket object. The two are bound together
565 * and initialised. The socket is then returned. If we are out of inodes
566 * NULL is returned. This functions uses GFP_KERNEL internally.
569 struct socket *sock_alloc(void)
574 inode = new_inode_pseudo(sock_mnt->mnt_sb);
578 sock = SOCKET_I(inode);
580 inode->i_ino = get_next_ino();
581 inode->i_mode = S_IFSOCK | S_IRWXUGO;
582 inode->i_uid = current_fsuid();
583 inode->i_gid = current_fsgid();
584 inode->i_op = &sockfs_inode_ops;
588 EXPORT_SYMBOL(sock_alloc);
590 static void __sock_release(struct socket *sock, struct inode *inode)
593 struct module *owner = sock->ops->owner;
597 sock->ops->release(sock);
605 if (sock->wq.fasync_list)
606 pr_err("%s: fasync list not empty!\n", __func__);
609 iput(SOCK_INODE(sock));
616 * sock_release - close a socket
617 * @sock: socket to close
619 * The socket is released from the protocol stack if it has a release
620 * callback, and the inode is then released if the socket is bound to
621 * an inode not a file.
623 void sock_release(struct socket *sock)
625 __sock_release(sock, NULL);
627 EXPORT_SYMBOL(sock_release);
629 void __sock_tx_timestamp(__u16 tsflags, __u8 *tx_flags)
631 u8 flags = *tx_flags;
633 if (tsflags & SOF_TIMESTAMPING_TX_HARDWARE)
634 flags |= SKBTX_HW_TSTAMP;
636 if (tsflags & SOF_TIMESTAMPING_TX_SOFTWARE)
637 flags |= SKBTX_SW_TSTAMP;
639 if (tsflags & SOF_TIMESTAMPING_TX_SCHED)
640 flags |= SKBTX_SCHED_TSTAMP;
644 EXPORT_SYMBOL(__sock_tx_timestamp);
646 INDIRECT_CALLABLE_DECLARE(int inet_sendmsg(struct socket *, struct msghdr *,
648 INDIRECT_CALLABLE_DECLARE(int inet6_sendmsg(struct socket *, struct msghdr *,
650 static inline int sock_sendmsg_nosec(struct socket *sock, struct msghdr *msg)
652 int ret = INDIRECT_CALL_INET(sock->ops->sendmsg, inet6_sendmsg,
653 inet_sendmsg, sock, msg,
655 BUG_ON(ret == -EIOCBQUEUED);
660 * sock_sendmsg - send a message through @sock
662 * @msg: message to send
664 * Sends @msg through @sock, passing through LSM.
665 * Returns the number of bytes sent, or an error code.
667 int sock_sendmsg(struct socket *sock, struct msghdr *msg)
669 int err = security_socket_sendmsg(sock, msg,
672 return err ?: sock_sendmsg_nosec(sock, msg);
674 EXPORT_SYMBOL(sock_sendmsg);
677 * kernel_sendmsg - send a message through @sock (kernel-space)
679 * @msg: message header
681 * @num: vec array length
682 * @size: total message data size
684 * Builds the message data with @vec and sends it through @sock.
685 * Returns the number of bytes sent, or an error code.
688 int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
689 struct kvec *vec, size_t num, size_t size)
691 iov_iter_kvec(&msg->msg_iter, WRITE, vec, num, size);
692 return sock_sendmsg(sock, msg);
694 EXPORT_SYMBOL(kernel_sendmsg);
697 * kernel_sendmsg_locked - send a message through @sock (kernel-space)
699 * @msg: message header
700 * @vec: output s/g array
701 * @num: output s/g array length
702 * @size: total message data size
704 * Builds the message data with @vec and sends it through @sock.
705 * Returns the number of bytes sent, or an error code.
706 * Caller must hold @sk.
709 int kernel_sendmsg_locked(struct sock *sk, struct msghdr *msg,
710 struct kvec *vec, size_t num, size_t size)
712 struct socket *sock = sk->sk_socket;
714 if (!sock->ops->sendmsg_locked)
715 return sock_no_sendmsg_locked(sk, msg, size);
717 iov_iter_kvec(&msg->msg_iter, WRITE, vec, num, size);
719 return sock->ops->sendmsg_locked(sk, msg, msg_data_left(msg));
721 EXPORT_SYMBOL(kernel_sendmsg_locked);
723 static bool skb_is_err_queue(const struct sk_buff *skb)
725 /* pkt_type of skbs enqueued on the error queue are set to
726 * PACKET_OUTGOING in skb_set_err_queue(). This is only safe to do
727 * in recvmsg, since skbs received on a local socket will never
728 * have a pkt_type of PACKET_OUTGOING.
730 return skb->pkt_type == PACKET_OUTGOING;
733 /* On transmit, software and hardware timestamps are returned independently.
734 * As the two skb clones share the hardware timestamp, which may be updated
735 * before the software timestamp is received, a hardware TX timestamp may be
736 * returned only if there is no software TX timestamp. Ignore false software
737 * timestamps, which may be made in the __sock_recv_timestamp() call when the
738 * option SO_TIMESTAMP_OLD(NS) is enabled on the socket, even when the skb has a
739 * hardware timestamp.
741 static bool skb_is_swtx_tstamp(const struct sk_buff *skb, int false_tstamp)
743 return skb->tstamp && !false_tstamp && skb_is_err_queue(skb);
746 static void put_ts_pktinfo(struct msghdr *msg, struct sk_buff *skb)
748 struct scm_ts_pktinfo ts_pktinfo;
749 struct net_device *orig_dev;
751 if (!skb_mac_header_was_set(skb))
754 memset(&ts_pktinfo, 0, sizeof(ts_pktinfo));
757 orig_dev = dev_get_by_napi_id(skb_napi_id(skb));
759 ts_pktinfo.if_index = orig_dev->ifindex;
762 ts_pktinfo.pkt_length = skb->len - skb_mac_offset(skb);
763 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPING_PKTINFO,
764 sizeof(ts_pktinfo), &ts_pktinfo);
768 * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
770 void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
773 int need_software_tstamp = sock_flag(sk, SOCK_RCVTSTAMP);
774 int new_tstamp = sock_flag(sk, SOCK_TSTAMP_NEW);
775 struct scm_timestamping_internal tss;
777 int empty = 1, false_tstamp = 0;
778 struct skb_shared_hwtstamps *shhwtstamps =
781 /* Race occurred between timestamp enabling and packet
782 receiving. Fill in the current time for now. */
783 if (need_software_tstamp && skb->tstamp == 0) {
784 __net_timestamp(skb);
788 if (need_software_tstamp) {
789 if (!sock_flag(sk, SOCK_RCVTSTAMPNS)) {
791 struct __kernel_sock_timeval tv;
793 skb_get_new_timestamp(skb, &tv);
794 put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMP_NEW,
797 struct __kernel_old_timeval tv;
799 skb_get_timestamp(skb, &tv);
800 put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMP_OLD,
805 struct __kernel_timespec ts;
807 skb_get_new_timestampns(skb, &ts);
808 put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMPNS_NEW,
811 struct __kernel_old_timespec ts;
813 skb_get_timestampns(skb, &ts);
814 put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMPNS_OLD,
820 memset(&tss, 0, sizeof(tss));
821 if ((sk->sk_tsflags & SOF_TIMESTAMPING_SOFTWARE) &&
822 ktime_to_timespec64_cond(skb->tstamp, tss.ts + 0))
825 (sk->sk_tsflags & SOF_TIMESTAMPING_RAW_HARDWARE) &&
826 !skb_is_swtx_tstamp(skb, false_tstamp) &&
827 ktime_to_timespec64_cond(shhwtstamps->hwtstamp, tss.ts + 2)) {
829 if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_PKTINFO) &&
830 !skb_is_err_queue(skb))
831 put_ts_pktinfo(msg, skb);
834 if (sock_flag(sk, SOCK_TSTAMP_NEW))
835 put_cmsg_scm_timestamping64(msg, &tss);
837 put_cmsg_scm_timestamping(msg, &tss);
839 if (skb_is_err_queue(skb) && skb->len &&
840 SKB_EXT_ERR(skb)->opt_stats)
841 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPING_OPT_STATS,
842 skb->len, skb->data);
845 EXPORT_SYMBOL_GPL(__sock_recv_timestamp);
847 void __sock_recv_wifi_status(struct msghdr *msg, struct sock *sk,
852 if (!sock_flag(sk, SOCK_WIFI_STATUS))
854 if (!skb->wifi_acked_valid)
857 ack = skb->wifi_acked;
859 put_cmsg(msg, SOL_SOCKET, SCM_WIFI_STATUS, sizeof(ack), &ack);
861 EXPORT_SYMBOL_GPL(__sock_recv_wifi_status);
863 static inline void sock_recv_drops(struct msghdr *msg, struct sock *sk,
866 if (sock_flag(sk, SOCK_RXQ_OVFL) && skb && SOCK_SKB_CB(skb)->dropcount)
867 put_cmsg(msg, SOL_SOCKET, SO_RXQ_OVFL,
868 sizeof(__u32), &SOCK_SKB_CB(skb)->dropcount);
871 void __sock_recv_ts_and_drops(struct msghdr *msg, struct sock *sk,
874 sock_recv_timestamp(msg, sk, skb);
875 sock_recv_drops(msg, sk, skb);
877 EXPORT_SYMBOL_GPL(__sock_recv_ts_and_drops);
879 INDIRECT_CALLABLE_DECLARE(int inet_recvmsg(struct socket *, struct msghdr *,
881 INDIRECT_CALLABLE_DECLARE(int inet6_recvmsg(struct socket *, struct msghdr *,
883 static inline int sock_recvmsg_nosec(struct socket *sock, struct msghdr *msg,
886 return INDIRECT_CALL_INET(sock->ops->recvmsg, inet6_recvmsg,
887 inet_recvmsg, sock, msg, msg_data_left(msg),
892 * sock_recvmsg - receive a message from @sock
894 * @msg: message to receive
895 * @flags: message flags
897 * Receives @msg from @sock, passing through LSM. Returns the total number
898 * of bytes received, or an error.
900 int sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags)
902 int err = security_socket_recvmsg(sock, msg, msg_data_left(msg), flags);
904 return err ?: sock_recvmsg_nosec(sock, msg, flags);
906 EXPORT_SYMBOL(sock_recvmsg);
909 * kernel_recvmsg - Receive a message from a socket (kernel space)
910 * @sock: The socket to receive the message from
911 * @msg: Received message
912 * @vec: Input s/g array for message data
913 * @num: Size of input s/g array
914 * @size: Number of bytes to read
915 * @flags: Message flags (MSG_DONTWAIT, etc...)
917 * On return the msg structure contains the scatter/gather array passed in the
918 * vec argument. The array is modified so that it consists of the unfilled
919 * portion of the original array.
921 * The returned value is the total number of bytes received, or an error.
924 int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
925 struct kvec *vec, size_t num, size_t size, int flags)
927 msg->msg_control_is_user = false;
928 iov_iter_kvec(&msg->msg_iter, READ, vec, num, size);
929 return sock_recvmsg(sock, msg, flags);
931 EXPORT_SYMBOL(kernel_recvmsg);
933 static ssize_t sock_sendpage(struct file *file, struct page *page,
934 int offset, size_t size, loff_t *ppos, int more)
939 sock = file->private_data;
941 flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
942 /* more is a combination of MSG_MORE and MSG_SENDPAGE_NOTLAST */
945 return kernel_sendpage(sock, page, offset, size, flags);
948 static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
949 struct pipe_inode_info *pipe, size_t len,
952 struct socket *sock = file->private_data;
954 if (unlikely(!sock->ops->splice_read))
955 return generic_file_splice_read(file, ppos, pipe, len, flags);
957 return sock->ops->splice_read(sock, ppos, pipe, len, flags);
960 static ssize_t sock_read_iter(struct kiocb *iocb, struct iov_iter *to)
962 struct file *file = iocb->ki_filp;
963 struct socket *sock = file->private_data;
964 struct msghdr msg = {.msg_iter = *to,
968 if (file->f_flags & O_NONBLOCK || (iocb->ki_flags & IOCB_NOWAIT))
969 msg.msg_flags = MSG_DONTWAIT;
971 if (iocb->ki_pos != 0)
974 if (!iov_iter_count(to)) /* Match SYS5 behaviour */
977 res = sock_recvmsg(sock, &msg, msg.msg_flags);
982 static ssize_t sock_write_iter(struct kiocb *iocb, struct iov_iter *from)
984 struct file *file = iocb->ki_filp;
985 struct socket *sock = file->private_data;
986 struct msghdr msg = {.msg_iter = *from,
990 if (iocb->ki_pos != 0)
993 if (file->f_flags & O_NONBLOCK || (iocb->ki_flags & IOCB_NOWAIT))
994 msg.msg_flags = MSG_DONTWAIT;
996 if (sock->type == SOCK_SEQPACKET)
997 msg.msg_flags |= MSG_EOR;
999 res = sock_sendmsg(sock, &msg);
1000 *from = msg.msg_iter;
1005 * Atomic setting of ioctl hooks to avoid race
1006 * with module unload.
1009 static DEFINE_MUTEX(br_ioctl_mutex);
1010 static int (*br_ioctl_hook) (struct net *, unsigned int cmd, void __user *arg);
1012 void brioctl_set(int (*hook) (struct net *, unsigned int, void __user *))
1014 mutex_lock(&br_ioctl_mutex);
1015 br_ioctl_hook = hook;
1016 mutex_unlock(&br_ioctl_mutex);
1018 EXPORT_SYMBOL(brioctl_set);
1020 static DEFINE_MUTEX(vlan_ioctl_mutex);
1021 static int (*vlan_ioctl_hook) (struct net *, void __user *arg);
1023 void vlan_ioctl_set(int (*hook) (struct net *, void __user *))
1025 mutex_lock(&vlan_ioctl_mutex);
1026 vlan_ioctl_hook = hook;
1027 mutex_unlock(&vlan_ioctl_mutex);
1029 EXPORT_SYMBOL(vlan_ioctl_set);
1031 static long sock_do_ioctl(struct net *net, struct socket *sock,
1032 unsigned int cmd, unsigned long arg)
1035 void __user *argp = (void __user *)arg;
1037 err = sock->ops->ioctl(sock, cmd, arg);
1040 * If this ioctl is unknown try to hand it down
1041 * to the NIC driver.
1043 if (err != -ENOIOCTLCMD)
1046 if (cmd == SIOCGIFCONF) {
1048 if (copy_from_user(&ifc, argp, sizeof(struct ifconf)))
1051 err = dev_ifconf(net, &ifc, sizeof(struct ifreq));
1053 if (!err && copy_to_user(argp, &ifc, sizeof(struct ifconf)))
1058 if (copy_from_user(&ifr, argp, sizeof(struct ifreq)))
1060 err = dev_ioctl(net, cmd, &ifr, &need_copyout);
1061 if (!err && need_copyout)
1062 if (copy_to_user(argp, &ifr, sizeof(struct ifreq)))
1069 * With an ioctl, arg may well be a user mode pointer, but we don't know
1070 * what to do with it - that's up to the protocol still.
1074 * get_net_ns - increment the refcount of the network namespace
1075 * @ns: common namespace (net)
1077 * Returns the net's common namespace.
1080 struct ns_common *get_net_ns(struct ns_common *ns)
1082 return &get_net(container_of(ns, struct net, ns))->ns;
1084 EXPORT_SYMBOL_GPL(get_net_ns);
1086 static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
1088 struct socket *sock;
1090 void __user *argp = (void __user *)arg;
1094 sock = file->private_data;
1097 if (unlikely(cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15))) {
1100 if (copy_from_user(&ifr, argp, sizeof(struct ifreq)))
1102 err = dev_ioctl(net, cmd, &ifr, &need_copyout);
1103 if (!err && need_copyout)
1104 if (copy_to_user(argp, &ifr, sizeof(struct ifreq)))
1107 #ifdef CONFIG_WEXT_CORE
1108 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
1109 err = wext_handle_ioctl(net, cmd, argp);
1116 if (get_user(pid, (int __user *)argp))
1118 err = f_setown(sock->file, pid, 1);
1122 err = put_user(f_getown(sock->file),
1123 (int __user *)argp);
1131 request_module("bridge");
1133 mutex_lock(&br_ioctl_mutex);
1135 err = br_ioctl_hook(net, cmd, argp);
1136 mutex_unlock(&br_ioctl_mutex);
1141 if (!vlan_ioctl_hook)
1142 request_module("8021q");
1144 mutex_lock(&vlan_ioctl_mutex);
1145 if (vlan_ioctl_hook)
1146 err = vlan_ioctl_hook(net, argp);
1147 mutex_unlock(&vlan_ioctl_mutex);
1151 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1154 err = open_related_ns(&net->ns, get_net_ns);
1156 case SIOCGSTAMP_OLD:
1157 case SIOCGSTAMPNS_OLD:
1158 if (!sock->ops->gettstamp) {
1162 err = sock->ops->gettstamp(sock, argp,
1163 cmd == SIOCGSTAMP_OLD,
1164 !IS_ENABLED(CONFIG_64BIT));
1166 case SIOCGSTAMP_NEW:
1167 case SIOCGSTAMPNS_NEW:
1168 if (!sock->ops->gettstamp) {
1172 err = sock->ops->gettstamp(sock, argp,
1173 cmd == SIOCGSTAMP_NEW,
1177 err = sock_do_ioctl(net, sock, cmd, arg);
1184 * sock_create_lite - creates a socket
1185 * @family: protocol family (AF_INET, ...)
1186 * @type: communication type (SOCK_STREAM, ...)
1187 * @protocol: protocol (0, ...)
1190 * Creates a new socket and assigns it to @res, passing through LSM.
1191 * The new socket initialization is not complete, see kernel_accept().
1192 * Returns 0 or an error. On failure @res is set to %NULL.
1193 * This function internally uses GFP_KERNEL.
1196 int sock_create_lite(int family, int type, int protocol, struct socket **res)
1199 struct socket *sock = NULL;
1201 err = security_socket_create(family, type, protocol, 1);
1205 sock = sock_alloc();
1212 err = security_socket_post_create(sock, family, type, protocol, 1);
1224 EXPORT_SYMBOL(sock_create_lite);
1226 /* No kernel lock held - perfect */
1227 static __poll_t sock_poll(struct file *file, poll_table *wait)
1229 struct socket *sock = file->private_data;
1230 __poll_t events = poll_requested_events(wait), flag = 0;
1232 if (!sock->ops->poll)
1235 if (sk_can_busy_loop(sock->sk)) {
1236 /* poll once if requested by the syscall */
1237 if (events & POLL_BUSY_LOOP)
1238 sk_busy_loop(sock->sk, 1);
1240 /* if this socket can poll_ll, tell the system call */
1241 flag = POLL_BUSY_LOOP;
1244 return sock->ops->poll(file, sock, wait) | flag;
1247 static int sock_mmap(struct file *file, struct vm_area_struct *vma)
1249 struct socket *sock = file->private_data;
1251 return sock->ops->mmap(file, sock, vma);
1254 static int sock_close(struct inode *inode, struct file *filp)
1256 __sock_release(SOCKET_I(inode), inode);
1261 * Update the socket async list
1263 * Fasync_list locking strategy.
1265 * 1. fasync_list is modified only under process context socket lock
1266 * i.e. under semaphore.
1267 * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
1268 * or under socket lock
1271 static int sock_fasync(int fd, struct file *filp, int on)
1273 struct socket *sock = filp->private_data;
1274 struct sock *sk = sock->sk;
1275 struct socket_wq *wq = &sock->wq;
1281 fasync_helper(fd, filp, on, &wq->fasync_list);
1283 if (!wq->fasync_list)
1284 sock_reset_flag(sk, SOCK_FASYNC);
1286 sock_set_flag(sk, SOCK_FASYNC);
1292 /* This function may be called only under rcu_lock */
1294 int sock_wake_async(struct socket_wq *wq, int how, int band)
1296 if (!wq || !wq->fasync_list)
1300 case SOCK_WAKE_WAITD:
1301 if (test_bit(SOCKWQ_ASYNC_WAITDATA, &wq->flags))
1304 case SOCK_WAKE_SPACE:
1305 if (!test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags))
1310 kill_fasync(&wq->fasync_list, SIGIO, band);
1313 kill_fasync(&wq->fasync_list, SIGURG, band);
1318 EXPORT_SYMBOL(sock_wake_async);
1321 * __sock_create - creates a socket
1322 * @net: net namespace
1323 * @family: protocol family (AF_INET, ...)
1324 * @type: communication type (SOCK_STREAM, ...)
1325 * @protocol: protocol (0, ...)
1327 * @kern: boolean for kernel space sockets
1329 * Creates a new socket and assigns it to @res, passing through LSM.
1330 * Returns 0 or an error. On failure @res is set to %NULL. @kern must
1331 * be set to true if the socket resides in kernel space.
1332 * This function internally uses GFP_KERNEL.
1335 int __sock_create(struct net *net, int family, int type, int protocol,
1336 struct socket **res, int kern)
1339 struct socket *sock;
1340 const struct net_proto_family *pf;
1343 * Check protocol is in range
1345 if (family < 0 || family >= NPROTO)
1346 return -EAFNOSUPPORT;
1347 if (type < 0 || type >= SOCK_MAX)
1352 This uglymoron is moved from INET layer to here to avoid
1353 deadlock in module load.
1355 if (family == PF_INET && type == SOCK_PACKET) {
1356 pr_info_once("%s uses obsolete (PF_INET,SOCK_PACKET)\n",
1361 err = security_socket_create(family, type, protocol, kern);
1366 * Allocate the socket and allow the family to set things up. if
1367 * the protocol is 0, the family is instructed to select an appropriate
1370 sock = sock_alloc();
1372 net_warn_ratelimited("socket: no more sockets\n");
1373 return -ENFILE; /* Not exactly a match, but its the
1374 closest posix thing */
1379 #ifdef CONFIG_MODULES
1380 /* Attempt to load a protocol module if the find failed.
1382 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
1383 * requested real, full-featured networking support upon configuration.
1384 * Otherwise module support will break!
1386 if (rcu_access_pointer(net_families[family]) == NULL)
1387 request_module("net-pf-%d", family);
1391 pf = rcu_dereference(net_families[family]);
1392 err = -EAFNOSUPPORT;
1397 * We will call the ->create function, that possibly is in a loadable
1398 * module, so we have to bump that loadable module refcnt first.
1400 if (!try_module_get(pf->owner))
1403 /* Now protected by module ref count */
1406 err = pf->create(net, sock, protocol, kern);
1408 goto out_module_put;
1411 * Now to bump the refcnt of the [loadable] module that owns this
1412 * socket at sock_release time we decrement its refcnt.
1414 if (!try_module_get(sock->ops->owner))
1415 goto out_module_busy;
1418 * Now that we're done with the ->create function, the [loadable]
1419 * module can have its refcnt decremented
1421 module_put(pf->owner);
1422 err = security_socket_post_create(sock, family, type, protocol, kern);
1424 goto out_sock_release;
1430 err = -EAFNOSUPPORT;
1433 module_put(pf->owner);
1440 goto out_sock_release;
1442 EXPORT_SYMBOL(__sock_create);
1445 * sock_create - creates a socket
1446 * @family: protocol family (AF_INET, ...)
1447 * @type: communication type (SOCK_STREAM, ...)
1448 * @protocol: protocol (0, ...)
1451 * A wrapper around __sock_create().
1452 * Returns 0 or an error. This function internally uses GFP_KERNEL.
1455 int sock_create(int family, int type, int protocol, struct socket **res)
1457 return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0);
1459 EXPORT_SYMBOL(sock_create);
1462 * sock_create_kern - creates a socket (kernel space)
1463 * @net: net namespace
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_kern(struct net *net, int family, int type, int protocol, struct socket **res)
1475 return __sock_create(net, family, type, protocol, res, 1);
1477 EXPORT_SYMBOL(sock_create_kern);
1479 int __sys_socket(int family, int type, int protocol)
1482 struct socket *sock;
1485 /* Check the SOCK_* constants for consistency. */
1486 BUILD_BUG_ON(SOCK_CLOEXEC != O_CLOEXEC);
1487 BUILD_BUG_ON((SOCK_MAX | SOCK_TYPE_MASK) != SOCK_TYPE_MASK);
1488 BUILD_BUG_ON(SOCK_CLOEXEC & SOCK_TYPE_MASK);
1489 BUILD_BUG_ON(SOCK_NONBLOCK & SOCK_TYPE_MASK);
1491 flags = type & ~SOCK_TYPE_MASK;
1492 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1494 type &= SOCK_TYPE_MASK;
1496 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1497 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1499 retval = sock_create(family, type, protocol, &sock);
1503 return sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK));
1506 SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol)
1508 return __sys_socket(family, type, protocol);
1512 * Create a pair of connected sockets.
1515 int __sys_socketpair(int family, int type, int protocol, int __user *usockvec)
1517 struct socket *sock1, *sock2;
1519 struct file *newfile1, *newfile2;
1522 flags = type & ~SOCK_TYPE_MASK;
1523 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1525 type &= SOCK_TYPE_MASK;
1527 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1528 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1531 * reserve descriptors and make sure we won't fail
1532 * to return them to userland.
1534 fd1 = get_unused_fd_flags(flags);
1535 if (unlikely(fd1 < 0))
1538 fd2 = get_unused_fd_flags(flags);
1539 if (unlikely(fd2 < 0)) {
1544 err = put_user(fd1, &usockvec[0]);
1548 err = put_user(fd2, &usockvec[1]);
1553 * Obtain the first socket and check if the underlying protocol
1554 * supports the socketpair call.
1557 err = sock_create(family, type, protocol, &sock1);
1558 if (unlikely(err < 0))
1561 err = sock_create(family, type, protocol, &sock2);
1562 if (unlikely(err < 0)) {
1563 sock_release(sock1);
1567 err = security_socket_socketpair(sock1, sock2);
1568 if (unlikely(err)) {
1569 sock_release(sock2);
1570 sock_release(sock1);
1574 err = sock1->ops->socketpair(sock1, sock2);
1575 if (unlikely(err < 0)) {
1576 sock_release(sock2);
1577 sock_release(sock1);
1581 newfile1 = sock_alloc_file(sock1, flags, NULL);
1582 if (IS_ERR(newfile1)) {
1583 err = PTR_ERR(newfile1);
1584 sock_release(sock2);
1588 newfile2 = sock_alloc_file(sock2, flags, NULL);
1589 if (IS_ERR(newfile2)) {
1590 err = PTR_ERR(newfile2);
1595 audit_fd_pair(fd1, fd2);
1597 fd_install(fd1, newfile1);
1598 fd_install(fd2, newfile2);
1607 SYSCALL_DEFINE4(socketpair, int, family, int, type, int, protocol,
1608 int __user *, usockvec)
1610 return __sys_socketpair(family, type, protocol, usockvec);
1614 * Bind a name to a socket. Nothing much to do here since it's
1615 * the protocol's responsibility to handle the local address.
1617 * We move the socket address to kernel space before we call
1618 * the protocol layer (having also checked the address is ok).
1621 int __sys_bind(int fd, struct sockaddr __user *umyaddr, int addrlen)
1623 struct socket *sock;
1624 struct sockaddr_storage address;
1625 int err, fput_needed;
1627 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1629 err = move_addr_to_kernel(umyaddr, addrlen, &address);
1631 err = security_socket_bind(sock,
1632 (struct sockaddr *)&address,
1635 err = sock->ops->bind(sock,
1639 fput_light(sock->file, fput_needed);
1644 SYSCALL_DEFINE3(bind, int, fd, struct sockaddr __user *, umyaddr, int, addrlen)
1646 return __sys_bind(fd, umyaddr, addrlen);
1650 * Perform a listen. Basically, we allow the protocol to do anything
1651 * necessary for a listen, and if that works, we mark the socket as
1652 * ready for listening.
1655 int __sys_listen(int fd, int backlog)
1657 struct socket *sock;
1658 int err, fput_needed;
1661 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1663 somaxconn = sock_net(sock->sk)->core.sysctl_somaxconn;
1664 if ((unsigned int)backlog > somaxconn)
1665 backlog = somaxconn;
1667 err = security_socket_listen(sock, backlog);
1669 err = sock->ops->listen(sock, backlog);
1671 fput_light(sock->file, fput_needed);
1676 SYSCALL_DEFINE2(listen, int, fd, int, backlog)
1678 return __sys_listen(fd, backlog);
1681 int __sys_accept4_file(struct file *file, unsigned file_flags,
1682 struct sockaddr __user *upeer_sockaddr,
1683 int __user *upeer_addrlen, int flags,
1684 unsigned long nofile)
1686 struct socket *sock, *newsock;
1687 struct file *newfile;
1688 int err, len, newfd;
1689 struct sockaddr_storage address;
1691 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1694 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1695 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1697 sock = sock_from_file(file);
1704 newsock = sock_alloc();
1708 newsock->type = sock->type;
1709 newsock->ops = sock->ops;
1712 * We don't need try_module_get here, as the listening socket (sock)
1713 * has the protocol module (sock->ops->owner) held.
1715 __module_get(newsock->ops->owner);
1717 newfd = __get_unused_fd_flags(flags, nofile);
1718 if (unlikely(newfd < 0)) {
1720 sock_release(newsock);
1723 newfile = sock_alloc_file(newsock, flags, sock->sk->sk_prot_creator->name);
1724 if (IS_ERR(newfile)) {
1725 err = PTR_ERR(newfile);
1726 put_unused_fd(newfd);
1730 err = security_socket_accept(sock, newsock);
1734 err = sock->ops->accept(sock, newsock, sock->file->f_flags | file_flags,
1739 if (upeer_sockaddr) {
1740 len = newsock->ops->getname(newsock,
1741 (struct sockaddr *)&address, 2);
1743 err = -ECONNABORTED;
1746 err = move_addr_to_user(&address,
1747 len, upeer_sockaddr, upeer_addrlen);
1752 /* File flags are not inherited via accept() unlike another OSes. */
1754 fd_install(newfd, newfile);
1760 put_unused_fd(newfd);
1766 * For accept, we attempt to create a new socket, set up the link
1767 * with the client, wake up the client, then return the new
1768 * connected fd. We collect the address of the connector in kernel
1769 * space and move it to user at the very end. This is unclean because
1770 * we open the socket then return an error.
1772 * 1003.1g adds the ability to recvmsg() to query connection pending
1773 * status to recvmsg. We need to add that support in a way thats
1774 * clean when we restructure accept also.
1777 int __sys_accept4(int fd, struct sockaddr __user *upeer_sockaddr,
1778 int __user *upeer_addrlen, int flags)
1785 ret = __sys_accept4_file(f.file, 0, upeer_sockaddr,
1786 upeer_addrlen, flags,
1787 rlimit(RLIMIT_NOFILE));
1794 SYSCALL_DEFINE4(accept4, int, fd, struct sockaddr __user *, upeer_sockaddr,
1795 int __user *, upeer_addrlen, int, flags)
1797 return __sys_accept4(fd, upeer_sockaddr, upeer_addrlen, flags);
1800 SYSCALL_DEFINE3(accept, int, fd, struct sockaddr __user *, upeer_sockaddr,
1801 int __user *, upeer_addrlen)
1803 return __sys_accept4(fd, upeer_sockaddr, upeer_addrlen, 0);
1807 * Attempt to connect to a socket with the server address. The address
1808 * is in user space so we verify it is OK and move it to kernel space.
1810 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
1813 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
1814 * other SEQPACKET protocols that take time to connect() as it doesn't
1815 * include the -EINPROGRESS status for such sockets.
1818 int __sys_connect_file(struct file *file, struct sockaddr_storage *address,
1819 int addrlen, int file_flags)
1821 struct socket *sock;
1824 sock = sock_from_file(file);
1831 security_socket_connect(sock, (struct sockaddr *)address, addrlen);
1835 err = sock->ops->connect(sock, (struct sockaddr *)address, addrlen,
1836 sock->file->f_flags | file_flags);
1841 int __sys_connect(int fd, struct sockaddr __user *uservaddr, int addrlen)
1848 struct sockaddr_storage address;
1850 ret = move_addr_to_kernel(uservaddr, addrlen, &address);
1852 ret = __sys_connect_file(f.file, &address, addrlen, 0);
1859 SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
1862 return __sys_connect(fd, uservaddr, addrlen);
1866 * Get the local address ('name') of a socket object. Move the obtained
1867 * name to user space.
1870 int __sys_getsockname(int fd, struct sockaddr __user *usockaddr,
1871 int __user *usockaddr_len)
1873 struct socket *sock;
1874 struct sockaddr_storage address;
1875 int err, fput_needed;
1877 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1881 err = security_socket_getsockname(sock);
1885 err = sock->ops->getname(sock, (struct sockaddr *)&address, 0);
1888 /* "err" is actually length in this case */
1889 err = move_addr_to_user(&address, err, usockaddr, usockaddr_len);
1892 fput_light(sock->file, fput_needed);
1897 SYSCALL_DEFINE3(getsockname, int, fd, struct sockaddr __user *, usockaddr,
1898 int __user *, usockaddr_len)
1900 return __sys_getsockname(fd, usockaddr, usockaddr_len);
1904 * Get the remote address ('name') of a socket object. Move the obtained
1905 * name to user space.
1908 int __sys_getpeername(int fd, struct sockaddr __user *usockaddr,
1909 int __user *usockaddr_len)
1911 struct socket *sock;
1912 struct sockaddr_storage address;
1913 int err, fput_needed;
1915 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1917 err = security_socket_getpeername(sock);
1919 fput_light(sock->file, fput_needed);
1923 err = sock->ops->getname(sock, (struct sockaddr *)&address, 1);
1925 /* "err" is actually length in this case */
1926 err = move_addr_to_user(&address, err, usockaddr,
1928 fput_light(sock->file, fput_needed);
1933 SYSCALL_DEFINE3(getpeername, int, fd, struct sockaddr __user *, usockaddr,
1934 int __user *, usockaddr_len)
1936 return __sys_getpeername(fd, usockaddr, usockaddr_len);
1940 * Send a datagram to a given address. We move the address into kernel
1941 * space and check the user space data area is readable before invoking
1944 int __sys_sendto(int fd, void __user *buff, size_t len, unsigned int flags,
1945 struct sockaddr __user *addr, int addr_len)
1947 struct socket *sock;
1948 struct sockaddr_storage address;
1954 err = import_single_range(WRITE, buff, len, &iov, &msg.msg_iter);
1957 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1961 msg.msg_name = NULL;
1962 msg.msg_control = NULL;
1963 msg.msg_controllen = 0;
1964 msg.msg_namelen = 0;
1966 err = move_addr_to_kernel(addr, addr_len, &address);
1969 msg.msg_name = (struct sockaddr *)&address;
1970 msg.msg_namelen = addr_len;
1972 if (sock->file->f_flags & O_NONBLOCK)
1973 flags |= MSG_DONTWAIT;
1974 msg.msg_flags = flags;
1975 err = sock_sendmsg(sock, &msg);
1978 fput_light(sock->file, fput_needed);
1983 SYSCALL_DEFINE6(sendto, int, fd, void __user *, buff, size_t, len,
1984 unsigned int, flags, struct sockaddr __user *, addr,
1987 return __sys_sendto(fd, buff, len, flags, addr, addr_len);
1991 * Send a datagram down a socket.
1994 SYSCALL_DEFINE4(send, int, fd, void __user *, buff, size_t, len,
1995 unsigned int, flags)
1997 return __sys_sendto(fd, buff, len, flags, NULL, 0);
2001 * Receive a frame from the socket and optionally record the address of the
2002 * sender. We verify the buffers are writable and if needed move the
2003 * sender address from kernel to user space.
2005 int __sys_recvfrom(int fd, void __user *ubuf, size_t size, unsigned int flags,
2006 struct sockaddr __user *addr, int __user *addr_len)
2008 struct socket *sock;
2011 struct sockaddr_storage address;
2015 err = import_single_range(READ, ubuf, size, &iov, &msg.msg_iter);
2018 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2022 msg.msg_control = NULL;
2023 msg.msg_controllen = 0;
2024 /* Save some cycles and don't copy the address if not needed */
2025 msg.msg_name = addr ? (struct sockaddr *)&address : NULL;
2026 /* We assume all kernel code knows the size of sockaddr_storage */
2027 msg.msg_namelen = 0;
2028 msg.msg_iocb = NULL;
2030 if (sock->file->f_flags & O_NONBLOCK)
2031 flags |= MSG_DONTWAIT;
2032 err = sock_recvmsg(sock, &msg, flags);
2034 if (err >= 0 && addr != NULL) {
2035 err2 = move_addr_to_user(&address,
2036 msg.msg_namelen, addr, addr_len);
2041 fput_light(sock->file, fput_needed);
2046 SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, ubuf, size_t, size,
2047 unsigned int, flags, struct sockaddr __user *, addr,
2048 int __user *, addr_len)
2050 return __sys_recvfrom(fd, ubuf, size, flags, addr, addr_len);
2054 * Receive a datagram from a socket.
2057 SYSCALL_DEFINE4(recv, int, fd, void __user *, ubuf, size_t, size,
2058 unsigned int, flags)
2060 return __sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
2063 static bool sock_use_custom_sol_socket(const struct socket *sock)
2065 const struct sock *sk = sock->sk;
2067 /* Use sock->ops->setsockopt() for MPTCP */
2068 return IS_ENABLED(CONFIG_MPTCP) &&
2069 sk->sk_protocol == IPPROTO_MPTCP &&
2070 sk->sk_type == SOCK_STREAM &&
2071 (sk->sk_family == AF_INET || sk->sk_family == AF_INET6);
2075 * Set a socket option. Because we don't know the option lengths we have
2076 * to pass the user mode parameter for the protocols to sort out.
2078 int __sys_setsockopt(int fd, int level, int optname, char __user *user_optval,
2081 sockptr_t optval = USER_SOCKPTR(user_optval);
2082 char *kernel_optval = NULL;
2083 int err, fput_needed;
2084 struct socket *sock;
2089 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2093 err = security_socket_setsockopt(sock, level, optname);
2097 if (!in_compat_syscall())
2098 err = BPF_CGROUP_RUN_PROG_SETSOCKOPT(sock->sk, &level, &optname,
2099 user_optval, &optlen,
2109 optval = KERNEL_SOCKPTR(kernel_optval);
2110 if (level == SOL_SOCKET && !sock_use_custom_sol_socket(sock))
2111 err = sock_setsockopt(sock, level, optname, optval, optlen);
2112 else if (unlikely(!sock->ops->setsockopt))
2115 err = sock->ops->setsockopt(sock, level, optname, optval,
2117 kfree(kernel_optval);
2119 fput_light(sock->file, fput_needed);
2123 SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname,
2124 char __user *, optval, int, optlen)
2126 return __sys_setsockopt(fd, level, optname, optval, optlen);
2130 * Get a socket option. Because we don't know the option lengths we have
2131 * to pass a user mode parameter for the protocols to sort out.
2133 int __sys_getsockopt(int fd, int level, int optname, char __user *optval,
2136 int err, fput_needed;
2137 struct socket *sock;
2140 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2144 err = security_socket_getsockopt(sock, level, optname);
2148 if (!in_compat_syscall())
2149 max_optlen = BPF_CGROUP_GETSOCKOPT_MAX_OPTLEN(optlen);
2151 if (level == SOL_SOCKET)
2152 err = sock_getsockopt(sock, level, optname, optval, optlen);
2153 else if (unlikely(!sock->ops->getsockopt))
2156 err = sock->ops->getsockopt(sock, level, optname, optval,
2159 if (!in_compat_syscall())
2160 err = BPF_CGROUP_RUN_PROG_GETSOCKOPT(sock->sk, level, optname,
2161 optval, optlen, max_optlen,
2164 fput_light(sock->file, fput_needed);
2168 SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname,
2169 char __user *, optval, int __user *, optlen)
2171 return __sys_getsockopt(fd, level, optname, optval, optlen);
2175 * Shutdown a socket.
2178 int __sys_shutdown_sock(struct socket *sock, int how)
2182 err = security_socket_shutdown(sock, how);
2184 err = sock->ops->shutdown(sock, how);
2189 int __sys_shutdown(int fd, int how)
2191 int err, fput_needed;
2192 struct socket *sock;
2194 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2196 err = __sys_shutdown_sock(sock, how);
2197 fput_light(sock->file, fput_needed);
2202 SYSCALL_DEFINE2(shutdown, int, fd, int, how)
2204 return __sys_shutdown(fd, how);
2207 /* A couple of helpful macros for getting the address of the 32/64 bit
2208 * fields which are the same type (int / unsigned) on our platforms.
2210 #define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
2211 #define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
2212 #define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
2214 struct used_address {
2215 struct sockaddr_storage name;
2216 unsigned int name_len;
2219 int __copy_msghdr_from_user(struct msghdr *kmsg,
2220 struct user_msghdr __user *umsg,
2221 struct sockaddr __user **save_addr,
2222 struct iovec __user **uiov, size_t *nsegs)
2224 struct user_msghdr msg;
2227 if (copy_from_user(&msg, umsg, sizeof(*umsg)))
2230 kmsg->msg_control_is_user = true;
2231 kmsg->msg_control_user = msg.msg_control;
2232 kmsg->msg_controllen = msg.msg_controllen;
2233 kmsg->msg_flags = msg.msg_flags;
2235 kmsg->msg_namelen = msg.msg_namelen;
2237 kmsg->msg_namelen = 0;
2239 if (kmsg->msg_namelen < 0)
2242 if (kmsg->msg_namelen > sizeof(struct sockaddr_storage))
2243 kmsg->msg_namelen = sizeof(struct sockaddr_storage);
2246 *save_addr = msg.msg_name;
2248 if (msg.msg_name && kmsg->msg_namelen) {
2250 err = move_addr_to_kernel(msg.msg_name,
2257 kmsg->msg_name = NULL;
2258 kmsg->msg_namelen = 0;
2261 if (msg.msg_iovlen > UIO_MAXIOV)
2264 kmsg->msg_iocb = NULL;
2265 *uiov = msg.msg_iov;
2266 *nsegs = msg.msg_iovlen;
2270 static int copy_msghdr_from_user(struct msghdr *kmsg,
2271 struct user_msghdr __user *umsg,
2272 struct sockaddr __user **save_addr,
2275 struct user_msghdr msg;
2278 err = __copy_msghdr_from_user(kmsg, umsg, save_addr, &msg.msg_iov,
2283 err = import_iovec(save_addr ? READ : WRITE,
2284 msg.msg_iov, msg.msg_iovlen,
2285 UIO_FASTIOV, iov, &kmsg->msg_iter);
2286 return err < 0 ? err : 0;
2289 static int ____sys_sendmsg(struct socket *sock, struct msghdr *msg_sys,
2290 unsigned int flags, struct used_address *used_address,
2291 unsigned int allowed_msghdr_flags)
2293 unsigned char ctl[sizeof(struct cmsghdr) + 20]
2294 __aligned(sizeof(__kernel_size_t));
2295 /* 20 is size of ipv6_pktinfo */
2296 unsigned char *ctl_buf = ctl;
2302 if (msg_sys->msg_controllen > INT_MAX)
2304 flags |= (msg_sys->msg_flags & allowed_msghdr_flags);
2305 ctl_len = msg_sys->msg_controllen;
2306 if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
2308 cmsghdr_from_user_compat_to_kern(msg_sys, sock->sk, ctl,
2312 ctl_buf = msg_sys->msg_control;
2313 ctl_len = msg_sys->msg_controllen;
2314 } else if (ctl_len) {
2315 BUILD_BUG_ON(sizeof(struct cmsghdr) !=
2316 CMSG_ALIGN(sizeof(struct cmsghdr)));
2317 if (ctl_len > sizeof(ctl)) {
2318 ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
2319 if (ctl_buf == NULL)
2323 if (copy_from_user(ctl_buf, msg_sys->msg_control_user, ctl_len))
2325 msg_sys->msg_control = ctl_buf;
2326 msg_sys->msg_control_is_user = false;
2328 msg_sys->msg_flags = flags;
2330 if (sock->file->f_flags & O_NONBLOCK)
2331 msg_sys->msg_flags |= MSG_DONTWAIT;
2333 * If this is sendmmsg() and current destination address is same as
2334 * previously succeeded address, omit asking LSM's decision.
2335 * used_address->name_len is initialized to UINT_MAX so that the first
2336 * destination address never matches.
2338 if (used_address && msg_sys->msg_name &&
2339 used_address->name_len == msg_sys->msg_namelen &&
2340 !memcmp(&used_address->name, msg_sys->msg_name,
2341 used_address->name_len)) {
2342 err = sock_sendmsg_nosec(sock, msg_sys);
2345 err = sock_sendmsg(sock, msg_sys);
2347 * If this is sendmmsg() and sending to current destination address was
2348 * successful, remember it.
2350 if (used_address && err >= 0) {
2351 used_address->name_len = msg_sys->msg_namelen;
2352 if (msg_sys->msg_name)
2353 memcpy(&used_address->name, msg_sys->msg_name,
2354 used_address->name_len);
2359 sock_kfree_s(sock->sk, ctl_buf, ctl_len);
2364 int sendmsg_copy_msghdr(struct msghdr *msg,
2365 struct user_msghdr __user *umsg, unsigned flags,
2370 if (flags & MSG_CMSG_COMPAT) {
2371 struct compat_msghdr __user *msg_compat;
2373 msg_compat = (struct compat_msghdr __user *) umsg;
2374 err = get_compat_msghdr(msg, msg_compat, NULL, iov);
2376 err = copy_msghdr_from_user(msg, umsg, NULL, iov);
2384 static int ___sys_sendmsg(struct socket *sock, struct user_msghdr __user *msg,
2385 struct msghdr *msg_sys, unsigned int flags,
2386 struct used_address *used_address,
2387 unsigned int allowed_msghdr_flags)
2389 struct sockaddr_storage address;
2390 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
2393 msg_sys->msg_name = &address;
2395 err = sendmsg_copy_msghdr(msg_sys, msg, flags, &iov);
2399 err = ____sys_sendmsg(sock, msg_sys, flags, used_address,
2400 allowed_msghdr_flags);
2406 * BSD sendmsg interface
2408 long __sys_sendmsg_sock(struct socket *sock, struct msghdr *msg,
2411 /* disallow ancillary data requests from this path */
2412 if (msg->msg_control || msg->msg_controllen)
2415 return ____sys_sendmsg(sock, msg, flags, NULL, 0);
2418 long __sys_sendmsg(int fd, struct user_msghdr __user *msg, unsigned int flags,
2419 bool forbid_cmsg_compat)
2421 int fput_needed, err;
2422 struct msghdr msg_sys;
2423 struct socket *sock;
2425 if (forbid_cmsg_compat && (flags & MSG_CMSG_COMPAT))
2428 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2432 err = ___sys_sendmsg(sock, msg, &msg_sys, flags, NULL, 0);
2434 fput_light(sock->file, fput_needed);
2439 SYSCALL_DEFINE3(sendmsg, int, fd, struct user_msghdr __user *, msg, unsigned int, flags)
2441 return __sys_sendmsg(fd, msg, flags, true);
2445 * Linux sendmmsg interface
2448 int __sys_sendmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
2449 unsigned int flags, bool forbid_cmsg_compat)
2451 int fput_needed, err, datagrams;
2452 struct socket *sock;
2453 struct mmsghdr __user *entry;
2454 struct compat_mmsghdr __user *compat_entry;
2455 struct msghdr msg_sys;
2456 struct used_address used_address;
2457 unsigned int oflags = flags;
2459 if (forbid_cmsg_compat && (flags & MSG_CMSG_COMPAT))
2462 if (vlen > UIO_MAXIOV)
2467 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2471 used_address.name_len = UINT_MAX;
2473 compat_entry = (struct compat_mmsghdr __user *)mmsg;
2477 while (datagrams < vlen) {
2478 if (datagrams == vlen - 1)
2481 if (MSG_CMSG_COMPAT & flags) {
2482 err = ___sys_sendmsg(sock, (struct user_msghdr __user *)compat_entry,
2483 &msg_sys, flags, &used_address, MSG_EOR);
2486 err = __put_user(err, &compat_entry->msg_len);
2489 err = ___sys_sendmsg(sock,
2490 (struct user_msghdr __user *)entry,
2491 &msg_sys, flags, &used_address, MSG_EOR);
2494 err = put_user(err, &entry->msg_len);
2501 if (msg_data_left(&msg_sys))
2506 fput_light(sock->file, fput_needed);
2508 /* We only return an error if no datagrams were able to be sent */
2515 SYSCALL_DEFINE4(sendmmsg, int, fd, struct mmsghdr __user *, mmsg,
2516 unsigned int, vlen, unsigned int, flags)
2518 return __sys_sendmmsg(fd, mmsg, vlen, flags, true);
2521 int recvmsg_copy_msghdr(struct msghdr *msg,
2522 struct user_msghdr __user *umsg, unsigned flags,
2523 struct sockaddr __user **uaddr,
2528 if (MSG_CMSG_COMPAT & flags) {
2529 struct compat_msghdr __user *msg_compat;
2531 msg_compat = (struct compat_msghdr __user *) umsg;
2532 err = get_compat_msghdr(msg, msg_compat, uaddr, iov);
2534 err = copy_msghdr_from_user(msg, umsg, uaddr, iov);
2542 static int ____sys_recvmsg(struct socket *sock, struct msghdr *msg_sys,
2543 struct user_msghdr __user *msg,
2544 struct sockaddr __user *uaddr,
2545 unsigned int flags, int nosec)
2547 struct compat_msghdr __user *msg_compat =
2548 (struct compat_msghdr __user *) msg;
2549 int __user *uaddr_len = COMPAT_NAMELEN(msg);
2550 struct sockaddr_storage addr;
2551 unsigned long cmsg_ptr;
2555 msg_sys->msg_name = &addr;
2556 cmsg_ptr = (unsigned long)msg_sys->msg_control;
2557 msg_sys->msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT);
2559 /* We assume all kernel code knows the size of sockaddr_storage */
2560 msg_sys->msg_namelen = 0;
2562 if (sock->file->f_flags & O_NONBLOCK)
2563 flags |= MSG_DONTWAIT;
2565 if (unlikely(nosec))
2566 err = sock_recvmsg_nosec(sock, msg_sys, flags);
2568 err = sock_recvmsg(sock, msg_sys, flags);
2574 if (uaddr != NULL) {
2575 err = move_addr_to_user(&addr,
2576 msg_sys->msg_namelen, uaddr,
2581 err = __put_user((msg_sys->msg_flags & ~MSG_CMSG_COMPAT),
2585 if (MSG_CMSG_COMPAT & flags)
2586 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
2587 &msg_compat->msg_controllen);
2589 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
2590 &msg->msg_controllen);
2598 static int ___sys_recvmsg(struct socket *sock, struct user_msghdr __user *msg,
2599 struct msghdr *msg_sys, unsigned int flags, int nosec)
2601 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
2602 /* user mode address pointers */
2603 struct sockaddr __user *uaddr;
2606 err = recvmsg_copy_msghdr(msg_sys, msg, flags, &uaddr, &iov);
2610 err = ____sys_recvmsg(sock, msg_sys, msg, uaddr, flags, nosec);
2616 * BSD recvmsg interface
2619 long __sys_recvmsg_sock(struct socket *sock, struct msghdr *msg,
2620 struct user_msghdr __user *umsg,
2621 struct sockaddr __user *uaddr, unsigned int flags)
2623 if (msg->msg_control || msg->msg_controllen) {
2624 /* disallow ancillary data reqs unless cmsg is plain data */
2625 if (!(sock->ops->flags & PROTO_CMSG_DATA_ONLY))
2629 return ____sys_recvmsg(sock, msg, umsg, uaddr, flags, 0);
2632 long __sys_recvmsg(int fd, struct user_msghdr __user *msg, unsigned int flags,
2633 bool forbid_cmsg_compat)
2635 int fput_needed, err;
2636 struct msghdr msg_sys;
2637 struct socket *sock;
2639 if (forbid_cmsg_compat && (flags & MSG_CMSG_COMPAT))
2642 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2646 err = ___sys_recvmsg(sock, msg, &msg_sys, flags, 0);
2648 fput_light(sock->file, fput_needed);
2653 SYSCALL_DEFINE3(recvmsg, int, fd, struct user_msghdr __user *, msg,
2654 unsigned int, flags)
2656 return __sys_recvmsg(fd, msg, flags, true);
2660 * Linux recvmmsg interface
2663 static int do_recvmmsg(int fd, struct mmsghdr __user *mmsg,
2664 unsigned int vlen, unsigned int flags,
2665 struct timespec64 *timeout)
2667 int fput_needed, err, datagrams;
2668 struct socket *sock;
2669 struct mmsghdr __user *entry;
2670 struct compat_mmsghdr __user *compat_entry;
2671 struct msghdr msg_sys;
2672 struct timespec64 end_time;
2673 struct timespec64 timeout64;
2676 poll_select_set_timeout(&end_time, timeout->tv_sec,
2682 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2686 if (likely(!(flags & MSG_ERRQUEUE))) {
2687 err = sock_error(sock->sk);
2695 compat_entry = (struct compat_mmsghdr __user *)mmsg;
2697 while (datagrams < vlen) {
2699 * No need to ask LSM for more than the first datagram.
2701 if (MSG_CMSG_COMPAT & flags) {
2702 err = ___sys_recvmsg(sock, (struct user_msghdr __user *)compat_entry,
2703 &msg_sys, flags & ~MSG_WAITFORONE,
2707 err = __put_user(err, &compat_entry->msg_len);
2710 err = ___sys_recvmsg(sock,
2711 (struct user_msghdr __user *)entry,
2712 &msg_sys, flags & ~MSG_WAITFORONE,
2716 err = put_user(err, &entry->msg_len);
2724 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
2725 if (flags & MSG_WAITFORONE)
2726 flags |= MSG_DONTWAIT;
2729 ktime_get_ts64(&timeout64);
2730 *timeout = timespec64_sub(end_time, timeout64);
2731 if (timeout->tv_sec < 0) {
2732 timeout->tv_sec = timeout->tv_nsec = 0;
2736 /* Timeout, return less than vlen datagrams */
2737 if (timeout->tv_nsec == 0 && timeout->tv_sec == 0)
2741 /* Out of band data, return right away */
2742 if (msg_sys.msg_flags & MSG_OOB)
2750 if (datagrams == 0) {
2756 * We may return less entries than requested (vlen) if the
2757 * sock is non block and there aren't enough datagrams...
2759 if (err != -EAGAIN) {
2761 * ... or if recvmsg returns an error after we
2762 * received some datagrams, where we record the
2763 * error to return on the next call or if the
2764 * app asks about it using getsockopt(SO_ERROR).
2766 sock->sk->sk_err = -err;
2769 fput_light(sock->file, fput_needed);
2774 int __sys_recvmmsg(int fd, struct mmsghdr __user *mmsg,
2775 unsigned int vlen, unsigned int flags,
2776 struct __kernel_timespec __user *timeout,
2777 struct old_timespec32 __user *timeout32)
2780 struct timespec64 timeout_sys;
2782 if (timeout && get_timespec64(&timeout_sys, timeout))
2785 if (timeout32 && get_old_timespec32(&timeout_sys, timeout32))
2788 if (!timeout && !timeout32)
2789 return do_recvmmsg(fd, mmsg, vlen, flags, NULL);
2791 datagrams = do_recvmmsg(fd, mmsg, vlen, flags, &timeout_sys);
2796 if (timeout && put_timespec64(&timeout_sys, timeout))
2797 datagrams = -EFAULT;
2799 if (timeout32 && put_old_timespec32(&timeout_sys, timeout32))
2800 datagrams = -EFAULT;
2805 SYSCALL_DEFINE5(recvmmsg, int, fd, struct mmsghdr __user *, mmsg,
2806 unsigned int, vlen, unsigned int, flags,
2807 struct __kernel_timespec __user *, timeout)
2809 if (flags & MSG_CMSG_COMPAT)
2812 return __sys_recvmmsg(fd, mmsg, vlen, flags, timeout, NULL);
2815 #ifdef CONFIG_COMPAT_32BIT_TIME
2816 SYSCALL_DEFINE5(recvmmsg_time32, int, fd, struct mmsghdr __user *, mmsg,
2817 unsigned int, vlen, unsigned int, flags,
2818 struct old_timespec32 __user *, timeout)
2820 if (flags & MSG_CMSG_COMPAT)
2823 return __sys_recvmmsg(fd, mmsg, vlen, flags, NULL, timeout);
2827 #ifdef __ARCH_WANT_SYS_SOCKETCALL
2828 /* Argument list sizes for sys_socketcall */
2829 #define AL(x) ((x) * sizeof(unsigned long))
2830 static const unsigned char nargs[21] = {
2831 AL(0), AL(3), AL(3), AL(3), AL(2), AL(3),
2832 AL(3), AL(3), AL(4), AL(4), AL(4), AL(6),
2833 AL(6), AL(2), AL(5), AL(5), AL(3), AL(3),
2840 * System call vectors.
2842 * Argument checking cleaned up. Saved 20% in size.
2843 * This function doesn't need to set the kernel lock because
2844 * it is set by the callees.
2847 SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args)
2849 unsigned long a[AUDITSC_ARGS];
2850 unsigned long a0, a1;
2854 if (call < 1 || call > SYS_SENDMMSG)
2856 call = array_index_nospec(call, SYS_SENDMMSG + 1);
2859 if (len > sizeof(a))
2862 /* copy_from_user should be SMP safe. */
2863 if (copy_from_user(a, args, len))
2866 err = audit_socketcall(nargs[call] / sizeof(unsigned long), a);
2875 err = __sys_socket(a0, a1, a[2]);
2878 err = __sys_bind(a0, (struct sockaddr __user *)a1, a[2]);
2881 err = __sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
2884 err = __sys_listen(a0, a1);
2887 err = __sys_accept4(a0, (struct sockaddr __user *)a1,
2888 (int __user *)a[2], 0);
2890 case SYS_GETSOCKNAME:
2892 __sys_getsockname(a0, (struct sockaddr __user *)a1,
2893 (int __user *)a[2]);
2895 case SYS_GETPEERNAME:
2897 __sys_getpeername(a0, (struct sockaddr __user *)a1,
2898 (int __user *)a[2]);
2900 case SYS_SOCKETPAIR:
2901 err = __sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
2904 err = __sys_sendto(a0, (void __user *)a1, a[2], a[3],
2908 err = __sys_sendto(a0, (void __user *)a1, a[2], a[3],
2909 (struct sockaddr __user *)a[4], a[5]);
2912 err = __sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
2916 err = __sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
2917 (struct sockaddr __user *)a[4],
2918 (int __user *)a[5]);
2921 err = __sys_shutdown(a0, a1);
2923 case SYS_SETSOCKOPT:
2924 err = __sys_setsockopt(a0, a1, a[2], (char __user *)a[3],
2927 case SYS_GETSOCKOPT:
2929 __sys_getsockopt(a0, a1, a[2], (char __user *)a[3],
2930 (int __user *)a[4]);
2933 err = __sys_sendmsg(a0, (struct user_msghdr __user *)a1,
2937 err = __sys_sendmmsg(a0, (struct mmsghdr __user *)a1, a[2],
2941 err = __sys_recvmsg(a0, (struct user_msghdr __user *)a1,
2945 if (IS_ENABLED(CONFIG_64BIT))
2946 err = __sys_recvmmsg(a0, (struct mmsghdr __user *)a1,
2948 (struct __kernel_timespec __user *)a[4],
2951 err = __sys_recvmmsg(a0, (struct mmsghdr __user *)a1,
2953 (struct old_timespec32 __user *)a[4]);
2956 err = __sys_accept4(a0, (struct sockaddr __user *)a1,
2957 (int __user *)a[2], a[3]);
2966 #endif /* __ARCH_WANT_SYS_SOCKETCALL */
2969 * sock_register - add a socket protocol handler
2970 * @ops: description of protocol
2972 * This function is called by a protocol handler that wants to
2973 * advertise its address family, and have it linked into the
2974 * socket interface. The value ops->family corresponds to the
2975 * socket system call protocol family.
2977 int sock_register(const struct net_proto_family *ops)
2981 if (ops->family >= NPROTO) {
2982 pr_crit("protocol %d >= NPROTO(%d)\n", ops->family, NPROTO);
2986 spin_lock(&net_family_lock);
2987 if (rcu_dereference_protected(net_families[ops->family],
2988 lockdep_is_held(&net_family_lock)))
2991 rcu_assign_pointer(net_families[ops->family], ops);
2994 spin_unlock(&net_family_lock);
2996 pr_info("NET: Registered protocol family %d\n", ops->family);
2999 EXPORT_SYMBOL(sock_register);
3002 * sock_unregister - remove a protocol handler
3003 * @family: protocol family to remove
3005 * This function is called by a protocol handler that wants to
3006 * remove its address family, and have it unlinked from the
3007 * new socket creation.
3009 * If protocol handler is a module, then it can use module reference
3010 * counts to protect against new references. If protocol handler is not
3011 * a module then it needs to provide its own protection in
3012 * the ops->create routine.
3014 void sock_unregister(int family)
3016 BUG_ON(family < 0 || family >= NPROTO);
3018 spin_lock(&net_family_lock);
3019 RCU_INIT_POINTER(net_families[family], NULL);
3020 spin_unlock(&net_family_lock);
3024 pr_info("NET: Unregistered protocol family %d\n", family);
3026 EXPORT_SYMBOL(sock_unregister);
3028 bool sock_is_registered(int family)
3030 return family < NPROTO && rcu_access_pointer(net_families[family]);
3033 static int __init sock_init(void)
3037 * Initialize the network sysctl infrastructure.
3039 err = net_sysctl_init();
3044 * Initialize skbuff SLAB cache
3049 * Initialize the protocols module.
3054 err = register_filesystem(&sock_fs_type);
3057 sock_mnt = kern_mount(&sock_fs_type);
3058 if (IS_ERR(sock_mnt)) {
3059 err = PTR_ERR(sock_mnt);
3063 /* The real protocol initialization is performed in later initcalls.
3066 #ifdef CONFIG_NETFILTER
3067 err = netfilter_init();
3072 ptp_classifier_init();
3078 unregister_filesystem(&sock_fs_type);
3082 core_initcall(sock_init); /* early initcall */
3084 #ifdef CONFIG_PROC_FS
3085 void socket_seq_show(struct seq_file *seq)
3087 seq_printf(seq, "sockets: used %d\n",
3088 sock_inuse_get(seq->private));
3090 #endif /* CONFIG_PROC_FS */
3092 #ifdef CONFIG_COMPAT
3093 static int compat_dev_ifconf(struct net *net, struct compat_ifconf __user *uifc32)
3095 struct compat_ifconf ifc32;
3099 if (copy_from_user(&ifc32, uifc32, sizeof(struct compat_ifconf)))
3102 ifc.ifc_len = ifc32.ifc_len;
3103 ifc.ifc_req = compat_ptr(ifc32.ifcbuf);
3106 err = dev_ifconf(net, &ifc, sizeof(struct compat_ifreq));
3111 ifc32.ifc_len = ifc.ifc_len;
3112 if (copy_to_user(uifc32, &ifc32, sizeof(struct compat_ifconf)))
3118 static int ethtool_ioctl(struct net *net, struct compat_ifreq __user *ifr32)
3120 struct compat_ethtool_rxnfc __user *compat_rxnfc;
3121 bool convert_in = false, convert_out = false;
3122 size_t buf_size = 0;
3123 struct ethtool_rxnfc __user *rxnfc = NULL;
3125 u32 rule_cnt = 0, actual_rule_cnt;
3130 if (get_user(data, &ifr32->ifr_ifru.ifru_data))
3133 compat_rxnfc = compat_ptr(data);
3135 if (get_user(ethcmd, &compat_rxnfc->cmd))
3138 /* Most ethtool structures are defined without padding.
3139 * Unfortunately struct ethtool_rxnfc is an exception.
3144 case ETHTOOL_GRXCLSRLALL:
3145 /* Buffer size is variable */
3146 if (get_user(rule_cnt, &compat_rxnfc->rule_cnt))
3148 if (rule_cnt > KMALLOC_MAX_SIZE / sizeof(u32))
3150 buf_size += rule_cnt * sizeof(u32);
3152 case ETHTOOL_GRXRINGS:
3153 case ETHTOOL_GRXCLSRLCNT:
3154 case ETHTOOL_GRXCLSRULE:
3155 case ETHTOOL_SRXCLSRLINS:
3158 case ETHTOOL_SRXCLSRLDEL:
3159 buf_size += sizeof(struct ethtool_rxnfc);
3161 rxnfc = compat_alloc_user_space(buf_size);
3165 if (copy_from_user(&ifr.ifr_name, &ifr32->ifr_name, IFNAMSIZ))
3168 ifr.ifr_data = convert_in ? rxnfc : (void __user *)compat_rxnfc;
3171 /* We expect there to be holes between fs.m_ext and
3172 * fs.ring_cookie and at the end of fs, but nowhere else.
3174 BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc, fs.m_ext) +
3175 sizeof(compat_rxnfc->fs.m_ext) !=
3176 offsetof(struct ethtool_rxnfc, fs.m_ext) +
3177 sizeof(rxnfc->fs.m_ext));
3179 offsetof(struct compat_ethtool_rxnfc, fs.location) -
3180 offsetof(struct compat_ethtool_rxnfc, fs.ring_cookie) !=
3181 offsetof(struct ethtool_rxnfc, fs.location) -
3182 offsetof(struct ethtool_rxnfc, fs.ring_cookie));
3184 if (copy_in_user(rxnfc, compat_rxnfc,
3185 (void __user *)(&rxnfc->fs.m_ext + 1) -
3186 (void __user *)rxnfc) ||
3187 copy_in_user(&rxnfc->fs.ring_cookie,
3188 &compat_rxnfc->fs.ring_cookie,
3189 (void __user *)(&rxnfc->fs.location + 1) -
3190 (void __user *)&rxnfc->fs.ring_cookie))
3192 if (ethcmd == ETHTOOL_GRXCLSRLALL) {
3193 if (put_user(rule_cnt, &rxnfc->rule_cnt))
3195 } else if (copy_in_user(&rxnfc->rule_cnt,
3196 &compat_rxnfc->rule_cnt,
3197 sizeof(rxnfc->rule_cnt)))
3201 ret = dev_ioctl(net, SIOCETHTOOL, &ifr, NULL);
3206 if (copy_in_user(compat_rxnfc, rxnfc,
3207 (const void __user *)(&rxnfc->fs.m_ext + 1) -
3208 (const void __user *)rxnfc) ||
3209 copy_in_user(&compat_rxnfc->fs.ring_cookie,
3210 &rxnfc->fs.ring_cookie,
3211 (const void __user *)(&rxnfc->fs.location + 1) -
3212 (const void __user *)&rxnfc->fs.ring_cookie) ||
3213 copy_in_user(&compat_rxnfc->rule_cnt, &rxnfc->rule_cnt,
3214 sizeof(rxnfc->rule_cnt)))
3217 if (ethcmd == ETHTOOL_GRXCLSRLALL) {
3218 /* As an optimisation, we only copy the actual
3219 * number of rules that the underlying
3220 * function returned. Since Mallory might
3221 * change the rule count in user memory, we
3222 * check that it is less than the rule count
3223 * originally given (as the user buffer size),
3224 * which has been range-checked.
3226 if (get_user(actual_rule_cnt, &rxnfc->rule_cnt))
3228 if (actual_rule_cnt < rule_cnt)
3229 rule_cnt = actual_rule_cnt;
3230 if (copy_in_user(&compat_rxnfc->rule_locs[0],
3231 &rxnfc->rule_locs[0],
3232 rule_cnt * sizeof(u32)))
3240 static int compat_siocwandev(struct net *net, struct compat_ifreq __user *uifr32)
3242 compat_uptr_t uptr32;
3247 if (copy_from_user(&ifr, uifr32, sizeof(struct compat_ifreq)))
3250 if (get_user(uptr32, &uifr32->ifr_settings.ifs_ifsu))
3253 saved = ifr.ifr_settings.ifs_ifsu.raw_hdlc;
3254 ifr.ifr_settings.ifs_ifsu.raw_hdlc = compat_ptr(uptr32);
3256 err = dev_ioctl(net, SIOCWANDEV, &ifr, NULL);
3258 ifr.ifr_settings.ifs_ifsu.raw_hdlc = saved;
3259 if (copy_to_user(uifr32, &ifr, sizeof(struct compat_ifreq)))
3265 /* Handle ioctls that use ifreq::ifr_data and just need struct ifreq converted */
3266 static int compat_ifr_data_ioctl(struct net *net, unsigned int cmd,
3267 struct compat_ifreq __user *u_ifreq32)
3272 if (copy_from_user(ifreq.ifr_name, u_ifreq32->ifr_name, IFNAMSIZ))
3274 if (get_user(data32, &u_ifreq32->ifr_data))
3276 ifreq.ifr_data = compat_ptr(data32);
3278 return dev_ioctl(net, cmd, &ifreq, NULL);
3281 static int compat_ifreq_ioctl(struct net *net, struct socket *sock,
3283 struct compat_ifreq __user *uifr32)
3285 struct ifreq __user *uifr;
3288 /* Handle the fact that while struct ifreq has the same *layout* on
3289 * 32/64 for everything but ifreq::ifru_ifmap and ifreq::ifru_data,
3290 * which are handled elsewhere, it still has different *size* due to
3291 * ifreq::ifru_ifmap (which is 16 bytes on 32 bit, 24 bytes on 64-bit,
3292 * resulting in struct ifreq being 32 and 40 bytes respectively).
3293 * As a result, if the struct happens to be at the end of a page and
3294 * the next page isn't readable/writable, we get a fault. To prevent
3295 * that, copy back and forth to the full size.
3298 uifr = compat_alloc_user_space(sizeof(*uifr));
3299 if (copy_in_user(uifr, uifr32, sizeof(*uifr32)))
3302 err = sock_do_ioctl(net, sock, cmd, (unsigned long)uifr);
3313 case SIOCGIFBRDADDR:
3314 case SIOCGIFDSTADDR:
3315 case SIOCGIFNETMASK:
3321 if (copy_in_user(uifr32, uifr, sizeof(*uifr32)))
3329 static int compat_sioc_ifmap(struct net *net, unsigned int cmd,
3330 struct compat_ifreq __user *uifr32)
3333 struct compat_ifmap __user *uifmap32;
3336 uifmap32 = &uifr32->ifr_ifru.ifru_map;
3337 err = copy_from_user(&ifr, uifr32, sizeof(ifr.ifr_name));
3338 err |= get_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
3339 err |= get_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
3340 err |= get_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
3341 err |= get_user(ifr.ifr_map.irq, &uifmap32->irq);
3342 err |= get_user(ifr.ifr_map.dma, &uifmap32->dma);
3343 err |= get_user(ifr.ifr_map.port, &uifmap32->port);
3347 err = dev_ioctl(net, cmd, &ifr, NULL);
3349 if (cmd == SIOCGIFMAP && !err) {
3350 err = copy_to_user(uifr32, &ifr, sizeof(ifr.ifr_name));
3351 err |= put_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
3352 err |= put_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
3353 err |= put_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
3354 err |= put_user(ifr.ifr_map.irq, &uifmap32->irq);
3355 err |= put_user(ifr.ifr_map.dma, &uifmap32->dma);
3356 err |= put_user(ifr.ifr_map.port, &uifmap32->port);
3363 /* Since old style bridge ioctl's endup using SIOCDEVPRIVATE
3364 * for some operations; this forces use of the newer bridge-utils that
3365 * use compatible ioctls
3367 static int old_bridge_ioctl(compat_ulong_t __user *argp)
3371 if (get_user(tmp, argp))
3373 if (tmp == BRCTL_GET_VERSION)
3374 return BRCTL_VERSION + 1;
3378 static int compat_sock_ioctl_trans(struct file *file, struct socket *sock,
3379 unsigned int cmd, unsigned long arg)
3381 void __user *argp = compat_ptr(arg);
3382 struct sock *sk = sock->sk;
3383 struct net *net = sock_net(sk);
3385 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15))
3386 return compat_ifr_data_ioctl(net, cmd, argp);
3391 return old_bridge_ioctl(argp);
3393 return compat_dev_ifconf(net, argp);
3395 return ethtool_ioctl(net, argp);
3397 return compat_siocwandev(net, argp);
3400 return compat_sioc_ifmap(net, cmd, argp);
3401 case SIOCGSTAMP_OLD:
3402 case SIOCGSTAMPNS_OLD:
3403 if (!sock->ops->gettstamp)
3404 return -ENOIOCTLCMD;
3405 return sock->ops->gettstamp(sock, argp, cmd == SIOCGSTAMP_OLD,
3406 !COMPAT_USE_64BIT_TIME);
3408 case SIOCBONDSLAVEINFOQUERY:
3409 case SIOCBONDINFOQUERY:
3412 return compat_ifr_data_ioctl(net, cmd, argp);
3423 case SIOCGSTAMP_NEW:
3424 case SIOCGSTAMPNS_NEW:
3425 return sock_ioctl(file, cmd, arg);
3442 case SIOCSIFHWBROADCAST:
3444 case SIOCGIFBRDADDR:
3445 case SIOCSIFBRDADDR:
3446 case SIOCGIFDSTADDR:
3447 case SIOCSIFDSTADDR:
3448 case SIOCGIFNETMASK:
3449 case SIOCSIFNETMASK:
3461 case SIOCBONDENSLAVE:
3462 case SIOCBONDRELEASE:
3463 case SIOCBONDSETHWADDR:
3464 case SIOCBONDCHANGEACTIVE:
3465 return compat_ifreq_ioctl(net, sock, cmd, argp);
3473 return sock_do_ioctl(net, sock, cmd, arg);
3476 return -ENOIOCTLCMD;
3479 static long compat_sock_ioctl(struct file *file, unsigned int cmd,
3482 struct socket *sock = file->private_data;
3483 int ret = -ENOIOCTLCMD;
3490 if (sock->ops->compat_ioctl)
3491 ret = sock->ops->compat_ioctl(sock, cmd, arg);
3493 if (ret == -ENOIOCTLCMD &&
3494 (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST))
3495 ret = compat_wext_handle_ioctl(net, cmd, arg);
3497 if (ret == -ENOIOCTLCMD)
3498 ret = compat_sock_ioctl_trans(file, sock, cmd, arg);
3505 * kernel_bind - bind an address to a socket (kernel space)
3508 * @addrlen: length of address
3510 * Returns 0 or an error.
3513 int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen)
3515 return sock->ops->bind(sock, addr, addrlen);
3517 EXPORT_SYMBOL(kernel_bind);
3520 * kernel_listen - move socket to listening state (kernel space)
3522 * @backlog: pending connections queue size
3524 * Returns 0 or an error.
3527 int kernel_listen(struct socket *sock, int backlog)
3529 return sock->ops->listen(sock, backlog);
3531 EXPORT_SYMBOL(kernel_listen);
3534 * kernel_accept - accept a connection (kernel space)
3535 * @sock: listening socket
3536 * @newsock: new connected socket
3539 * @flags must be SOCK_CLOEXEC, SOCK_NONBLOCK or 0.
3540 * If it fails, @newsock is guaranteed to be %NULL.
3541 * Returns 0 or an error.
3544 int kernel_accept(struct socket *sock, struct socket **newsock, int flags)
3546 struct sock *sk = sock->sk;
3549 err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
3554 err = sock->ops->accept(sock, *newsock, flags, true);
3556 sock_release(*newsock);
3561 (*newsock)->ops = sock->ops;
3562 __module_get((*newsock)->ops->owner);
3567 EXPORT_SYMBOL(kernel_accept);
3570 * kernel_connect - connect a socket (kernel space)
3573 * @addrlen: address length
3574 * @flags: flags (O_NONBLOCK, ...)
3576 * For datagram sockets, @addr is the addres to which datagrams are sent
3577 * by default, and the only address from which datagrams are received.
3578 * For stream sockets, attempts to connect to @addr.
3579 * Returns 0 or an error code.
3582 int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
3585 return sock->ops->connect(sock, addr, addrlen, flags);
3587 EXPORT_SYMBOL(kernel_connect);
3590 * kernel_getsockname - get the address which the socket is bound (kernel space)
3592 * @addr: address holder
3594 * Fills the @addr pointer with the address which the socket is bound.
3595 * Returns 0 or an error code.
3598 int kernel_getsockname(struct socket *sock, struct sockaddr *addr)
3600 return sock->ops->getname(sock, addr, 0);
3602 EXPORT_SYMBOL(kernel_getsockname);
3605 * kernel_getpeername - get the address which the socket is connected (kernel space)
3607 * @addr: address holder
3609 * Fills the @addr pointer with the address which the socket is connected.
3610 * Returns 0 or an error code.
3613 int kernel_getpeername(struct socket *sock, struct sockaddr *addr)
3615 return sock->ops->getname(sock, addr, 1);
3617 EXPORT_SYMBOL(kernel_getpeername);
3620 * kernel_sendpage - send a &page through a socket (kernel space)
3623 * @offset: page offset
3624 * @size: total size in bytes
3625 * @flags: flags (MSG_DONTWAIT, ...)
3627 * Returns the total amount sent in bytes or an error.
3630 int kernel_sendpage(struct socket *sock, struct page *page, int offset,
3631 size_t size, int flags)
3633 if (sock->ops->sendpage) {
3634 /* Warn in case the improper page to zero-copy send */
3635 WARN_ONCE(!sendpage_ok(page), "improper page for zero-copy send");
3636 return sock->ops->sendpage(sock, page, offset, size, flags);
3638 return sock_no_sendpage(sock, page, offset, size, flags);
3640 EXPORT_SYMBOL(kernel_sendpage);
3643 * kernel_sendpage_locked - send a &page through the locked sock (kernel space)
3646 * @offset: page offset
3647 * @size: total size in bytes
3648 * @flags: flags (MSG_DONTWAIT, ...)
3650 * Returns the total amount sent in bytes or an error.
3651 * Caller must hold @sk.
3654 int kernel_sendpage_locked(struct sock *sk, struct page *page, int offset,
3655 size_t size, int flags)
3657 struct socket *sock = sk->sk_socket;
3659 if (sock->ops->sendpage_locked)
3660 return sock->ops->sendpage_locked(sk, page, offset, size,
3663 return sock_no_sendpage_locked(sk, page, offset, size, flags);
3665 EXPORT_SYMBOL(kernel_sendpage_locked);
3668 * kernel_sock_shutdown - shut down part of a full-duplex connection (kernel space)
3670 * @how: connection part
3672 * Returns 0 or an error.
3675 int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how)
3677 return sock->ops->shutdown(sock, how);
3679 EXPORT_SYMBOL(kernel_sock_shutdown);
3682 * kernel_sock_ip_overhead - returns the IP overhead imposed by a socket
3685 * This routine returns the IP overhead imposed by a socket i.e.
3686 * the length of the underlying IP header, depending on whether
3687 * this is an IPv4 or IPv6 socket and the length from IP options turned
3688 * on at the socket. Assumes that the caller has a lock on the socket.
3691 u32 kernel_sock_ip_overhead(struct sock *sk)
3693 struct inet_sock *inet;
3694 struct ip_options_rcu *opt;
3696 #if IS_ENABLED(CONFIG_IPV6)
3697 struct ipv6_pinfo *np;
3698 struct ipv6_txoptions *optv6 = NULL;
3699 #endif /* IS_ENABLED(CONFIG_IPV6) */
3704 switch (sk->sk_family) {
3707 overhead += sizeof(struct iphdr);
3708 opt = rcu_dereference_protected(inet->inet_opt,
3709 sock_owned_by_user(sk));
3711 overhead += opt->opt.optlen;
3713 #if IS_ENABLED(CONFIG_IPV6)
3716 overhead += sizeof(struct ipv6hdr);
3718 optv6 = rcu_dereference_protected(np->opt,
3719 sock_owned_by_user(sk));
3721 overhead += (optv6->opt_flen + optv6->opt_nflen);
3723 #endif /* IS_ENABLED(CONFIG_IPV6) */
3724 default: /* Returns 0 overhead if the socket is not ipv4 or ipv6 */
3728 EXPORT_SYMBOL(kernel_sock_ip_overhead);