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/bpf-cgroup.h>
56 #include <linux/ethtool.h>
58 #include <linux/socket.h>
59 #include <linux/file.h>
60 #include <linux/splice.h>
61 #include <linux/net.h>
62 #include <linux/interrupt.h>
63 #include <linux/thread_info.h>
64 #include <linux/rcupdate.h>
65 #include <linux/netdevice.h>
66 #include <linux/proc_fs.h>
67 #include <linux/seq_file.h>
68 #include <linux/mutex.h>
69 #include <linux/if_bridge.h>
70 #include <linux/if_vlan.h>
71 #include <linux/ptp_classify.h>
72 #include <linux/init.h>
73 #include <linux/poll.h>
74 #include <linux/cache.h>
75 #include <linux/module.h>
76 #include <linux/highmem.h>
77 #include <linux/mount.h>
78 #include <linux/pseudo_fs.h>
79 #include <linux/security.h>
80 #include <linux/syscalls.h>
81 #include <linux/compat.h>
82 #include <linux/kmod.h>
83 #include <linux/audit.h>
84 #include <linux/wireless.h>
85 #include <linux/nsproxy.h>
86 #include <linux/magic.h>
87 #include <linux/slab.h>
88 #include <linux/xattr.h>
89 #include <linux/nospec.h>
90 #include <linux/indirect_call_wrapper.h>
91 #include <linux/io_uring/net.h>
93 #include <linux/uaccess.h>
94 #include <asm/unistd.h>
96 #include <net/compat.h>
98 #include <net/cls_cgroup.h>
100 #include <net/sock.h>
101 #include <linux/netfilter.h>
103 #include <linux/if_tun.h>
104 #include <linux/ipv6_route.h>
105 #include <linux/route.h>
106 #include <linux/termios.h>
107 #include <linux/sockios.h>
108 #include <net/busy_poll.h>
109 #include <linux/errqueue.h>
110 #include <linux/ptp_clock_kernel.h>
111 #include <trace/events/sock.h>
113 #include "core/dev.h"
115 #ifdef CONFIG_NET_RX_BUSY_POLL
116 unsigned int sysctl_net_busy_read __read_mostly;
117 unsigned int sysctl_net_busy_poll __read_mostly;
120 static ssize_t sock_read_iter(struct kiocb *iocb, struct iov_iter *to);
121 static ssize_t sock_write_iter(struct kiocb *iocb, struct iov_iter *from);
122 static int sock_mmap(struct file *file, struct vm_area_struct *vma);
124 static int sock_close(struct inode *inode, struct file *file);
125 static __poll_t sock_poll(struct file *file,
126 struct poll_table_struct *wait);
127 static long sock_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
129 static long compat_sock_ioctl(struct file *file,
130 unsigned int cmd, unsigned long arg);
132 static int sock_fasync(int fd, struct file *filp, int on);
133 static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
134 struct pipe_inode_info *pipe, size_t len,
136 static void sock_splice_eof(struct file *file);
138 #ifdef CONFIG_PROC_FS
139 static void sock_show_fdinfo(struct seq_file *m, struct file *f)
141 struct socket *sock = f->private_data;
142 const struct proto_ops *ops = READ_ONCE(sock->ops);
144 if (ops->show_fdinfo)
145 ops->show_fdinfo(m, sock);
148 #define sock_show_fdinfo NULL
152 * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
153 * in the operation structures but are done directly via the socketcall() multiplexor.
156 static const struct file_operations socket_file_ops = {
157 .owner = THIS_MODULE,
158 .read_iter = sock_read_iter,
159 .write_iter = sock_write_iter,
161 .unlocked_ioctl = sock_ioctl,
163 .compat_ioctl = compat_sock_ioctl,
165 .uring_cmd = io_uring_cmd_sock,
167 .release = sock_close,
168 .fasync = sock_fasync,
169 .splice_write = splice_to_socket,
170 .splice_read = sock_splice_read,
171 .splice_eof = sock_splice_eof,
172 .show_fdinfo = sock_show_fdinfo,
175 static const char * const pf_family_names[] = {
176 [PF_UNSPEC] = "PF_UNSPEC",
177 [PF_UNIX] = "PF_UNIX/PF_LOCAL",
178 [PF_INET] = "PF_INET",
179 [PF_AX25] = "PF_AX25",
181 [PF_APPLETALK] = "PF_APPLETALK",
182 [PF_NETROM] = "PF_NETROM",
183 [PF_BRIDGE] = "PF_BRIDGE",
184 [PF_ATMPVC] = "PF_ATMPVC",
186 [PF_INET6] = "PF_INET6",
187 [PF_ROSE] = "PF_ROSE",
188 [PF_DECnet] = "PF_DECnet",
189 [PF_NETBEUI] = "PF_NETBEUI",
190 [PF_SECURITY] = "PF_SECURITY",
192 [PF_NETLINK] = "PF_NETLINK/PF_ROUTE",
193 [PF_PACKET] = "PF_PACKET",
195 [PF_ECONET] = "PF_ECONET",
196 [PF_ATMSVC] = "PF_ATMSVC",
199 [PF_IRDA] = "PF_IRDA",
200 [PF_PPPOX] = "PF_PPPOX",
201 [PF_WANPIPE] = "PF_WANPIPE",
204 [PF_MPLS] = "PF_MPLS",
206 [PF_TIPC] = "PF_TIPC",
207 [PF_BLUETOOTH] = "PF_BLUETOOTH",
208 [PF_IUCV] = "PF_IUCV",
209 [PF_RXRPC] = "PF_RXRPC",
210 [PF_ISDN] = "PF_ISDN",
211 [PF_PHONET] = "PF_PHONET",
212 [PF_IEEE802154] = "PF_IEEE802154",
213 [PF_CAIF] = "PF_CAIF",
216 [PF_VSOCK] = "PF_VSOCK",
218 [PF_QIPCRTR] = "PF_QIPCRTR",
221 [PF_MCTP] = "PF_MCTP",
225 * The protocol list. Each protocol is registered in here.
228 static DEFINE_SPINLOCK(net_family_lock);
229 static const struct net_proto_family __rcu *net_families[NPROTO] __read_mostly;
233 * Move socket addresses back and forth across the kernel/user
234 * divide and look after the messy bits.
238 * move_addr_to_kernel - copy a socket address into kernel space
239 * @uaddr: Address in user space
240 * @kaddr: Address in kernel space
241 * @ulen: Length in user space
243 * The address is copied into kernel space. If the provided address is
244 * too long an error code of -EINVAL is returned. If the copy gives
245 * invalid addresses -EFAULT is returned. On a success 0 is returned.
248 int move_addr_to_kernel(void __user *uaddr, int ulen, struct sockaddr_storage *kaddr)
250 if (ulen < 0 || ulen > sizeof(struct sockaddr_storage))
254 if (copy_from_user(kaddr, uaddr, ulen))
256 return audit_sockaddr(ulen, kaddr);
260 * move_addr_to_user - copy an address to user space
261 * @kaddr: kernel space address
262 * @klen: length of address in kernel
263 * @uaddr: user space address
264 * @ulen: pointer to user length field
266 * The value pointed to by ulen on entry is the buffer length available.
267 * This is overwritten with the buffer space used. -EINVAL is returned
268 * if an overlong buffer is specified or a negative buffer size. -EFAULT
269 * is returned if either the buffer or the length field are not
271 * After copying the data up to the limit the user specifies, the true
272 * length of the data is written over the length limit the user
273 * specified. Zero is returned for a success.
276 static int move_addr_to_user(struct sockaddr_storage *kaddr, int klen,
277 void __user *uaddr, int __user *ulen)
282 BUG_ON(klen > sizeof(struct sockaddr_storage));
283 err = get_user(len, ulen);
291 if (audit_sockaddr(klen, kaddr))
293 if (copy_to_user(uaddr, kaddr, len))
297 * "fromlen shall refer to the value before truncation.."
300 return __put_user(klen, ulen);
303 static struct kmem_cache *sock_inode_cachep __ro_after_init;
305 static struct inode *sock_alloc_inode(struct super_block *sb)
307 struct socket_alloc *ei;
309 ei = alloc_inode_sb(sb, sock_inode_cachep, GFP_KERNEL);
312 init_waitqueue_head(&ei->socket.wq.wait);
313 ei->socket.wq.fasync_list = NULL;
314 ei->socket.wq.flags = 0;
316 ei->socket.state = SS_UNCONNECTED;
317 ei->socket.flags = 0;
318 ei->socket.ops = NULL;
319 ei->socket.sk = NULL;
320 ei->socket.file = NULL;
322 return &ei->vfs_inode;
325 static void sock_free_inode(struct inode *inode)
327 struct socket_alloc *ei;
329 ei = container_of(inode, struct socket_alloc, vfs_inode);
330 kmem_cache_free(sock_inode_cachep, ei);
333 static void init_once(void *foo)
335 struct socket_alloc *ei = (struct socket_alloc *)foo;
337 inode_init_once(&ei->vfs_inode);
340 static void init_inodecache(void)
342 sock_inode_cachep = kmem_cache_create("sock_inode_cache",
343 sizeof(struct socket_alloc),
345 (SLAB_HWCACHE_ALIGN |
346 SLAB_RECLAIM_ACCOUNT |
349 BUG_ON(sock_inode_cachep == NULL);
352 static const struct super_operations sockfs_ops = {
353 .alloc_inode = sock_alloc_inode,
354 .free_inode = sock_free_inode,
355 .statfs = simple_statfs,
359 * sockfs_dname() is called from d_path().
361 static char *sockfs_dname(struct dentry *dentry, char *buffer, int buflen)
363 return dynamic_dname(buffer, buflen, "socket:[%lu]",
364 d_inode(dentry)->i_ino);
367 static const struct dentry_operations sockfs_dentry_operations = {
368 .d_dname = sockfs_dname,
371 static int sockfs_xattr_get(const struct xattr_handler *handler,
372 struct dentry *dentry, struct inode *inode,
373 const char *suffix, void *value, size_t size)
376 if (dentry->d_name.len + 1 > size)
378 memcpy(value, dentry->d_name.name, dentry->d_name.len + 1);
380 return dentry->d_name.len + 1;
383 #define XATTR_SOCKPROTONAME_SUFFIX "sockprotoname"
384 #define XATTR_NAME_SOCKPROTONAME (XATTR_SYSTEM_PREFIX XATTR_SOCKPROTONAME_SUFFIX)
385 #define XATTR_NAME_SOCKPROTONAME_LEN (sizeof(XATTR_NAME_SOCKPROTONAME)-1)
387 static const struct xattr_handler sockfs_xattr_handler = {
388 .name = XATTR_NAME_SOCKPROTONAME,
389 .get = sockfs_xattr_get,
392 static int sockfs_security_xattr_set(const struct xattr_handler *handler,
393 struct mnt_idmap *idmap,
394 struct dentry *dentry, struct inode *inode,
395 const char *suffix, const void *value,
396 size_t size, int flags)
398 /* Handled by LSM. */
402 static const struct xattr_handler sockfs_security_xattr_handler = {
403 .prefix = XATTR_SECURITY_PREFIX,
404 .set = sockfs_security_xattr_set,
407 static const struct xattr_handler * const sockfs_xattr_handlers[] = {
408 &sockfs_xattr_handler,
409 &sockfs_security_xattr_handler,
413 static int sockfs_init_fs_context(struct fs_context *fc)
415 struct pseudo_fs_context *ctx = init_pseudo(fc, SOCKFS_MAGIC);
418 ctx->ops = &sockfs_ops;
419 ctx->dops = &sockfs_dentry_operations;
420 ctx->xattr = sockfs_xattr_handlers;
424 static struct vfsmount *sock_mnt __read_mostly;
426 static struct file_system_type sock_fs_type = {
428 .init_fs_context = sockfs_init_fs_context,
429 .kill_sb = kill_anon_super,
433 * Obtains the first available file descriptor and sets it up for use.
435 * These functions create file structures and maps them to fd space
436 * of the current process. On success it returns file descriptor
437 * and file struct implicitly stored in sock->file.
438 * Note that another thread may close file descriptor before we return
439 * from this function. We use the fact that now we do not refer
440 * to socket after mapping. If one day we will need it, this
441 * function will increment ref. count on file by 1.
443 * In any case returned fd MAY BE not valid!
444 * This race condition is unavoidable
445 * with shared fd spaces, we cannot solve it inside kernel,
446 * but we take care of internal coherence yet.
450 * sock_alloc_file - Bind a &socket to a &file
452 * @flags: file status flags
453 * @dname: protocol name
455 * Returns the &file bound with @sock, implicitly storing it
456 * in sock->file. If dname is %NULL, sets to "".
458 * On failure @sock is released, and an ERR pointer is returned.
460 * This function uses GFP_KERNEL internally.
463 struct file *sock_alloc_file(struct socket *sock, int flags, const char *dname)
468 dname = sock->sk ? sock->sk->sk_prot_creator->name : "";
470 file = alloc_file_pseudo(SOCK_INODE(sock), sock_mnt, dname,
471 O_RDWR | (flags & O_NONBLOCK),
478 file->f_mode |= FMODE_NOWAIT;
480 file->private_data = sock;
481 stream_open(SOCK_INODE(sock), file);
483 * Disable permission and pre-content events, but enable legacy
484 * inotify events for legacy users.
486 file_set_fsnotify_mode(file, FMODE_NONOTIFY_PERM);
489 EXPORT_SYMBOL(sock_alloc_file);
491 static int sock_map_fd(struct socket *sock, int flags)
493 struct file *newfile;
494 int fd = get_unused_fd_flags(flags);
495 if (unlikely(fd < 0)) {
500 newfile = sock_alloc_file(sock, flags, NULL);
501 if (!IS_ERR(newfile)) {
502 fd_install(fd, newfile);
507 return PTR_ERR(newfile);
511 * sock_from_file - Return the &socket bounded to @file.
514 * On failure returns %NULL.
517 struct socket *sock_from_file(struct file *file)
519 if (likely(file->f_op == &socket_file_ops))
520 return file->private_data; /* set in sock_alloc_file */
524 EXPORT_SYMBOL(sock_from_file);
527 * sockfd_lookup - Go from a file number to its socket slot
529 * @err: pointer to an error code return
531 * The file handle passed in is locked and the socket it is bound
532 * to is returned. If an error occurs the err pointer is overwritten
533 * with a negative errno code and NULL is returned. The function checks
534 * for both invalid handles and passing a handle which is not a socket.
536 * On a success the socket object pointer is returned.
539 struct socket *sockfd_lookup(int fd, int *err)
550 sock = sock_from_file(file);
557 EXPORT_SYMBOL(sockfd_lookup);
559 static ssize_t sockfs_listxattr(struct dentry *dentry, char *buffer,
565 len = security_inode_listsecurity(d_inode(dentry), buffer, size);
575 len = (XATTR_NAME_SOCKPROTONAME_LEN + 1);
580 memcpy(buffer, XATTR_NAME_SOCKPROTONAME, len);
587 static int sockfs_setattr(struct mnt_idmap *idmap,
588 struct dentry *dentry, struct iattr *iattr)
590 int err = simple_setattr(&nop_mnt_idmap, dentry, iattr);
592 if (!err && (iattr->ia_valid & ATTR_UID)) {
593 struct socket *sock = SOCKET_I(d_inode(dentry));
596 /* Paired with READ_ONCE() in sk_uid() */
597 WRITE_ONCE(sock->sk->sk_uid, iattr->ia_uid);
606 static const struct inode_operations sockfs_inode_ops = {
607 .listxattr = sockfs_listxattr,
608 .setattr = sockfs_setattr,
612 * sock_alloc - allocate a socket
614 * Allocate a new inode and socket object. The two are bound together
615 * and initialised. The socket is then returned. If we are out of inodes
616 * NULL is returned. This functions uses GFP_KERNEL internally.
619 struct socket *sock_alloc(void)
624 inode = new_inode_pseudo(sock_mnt->mnt_sb);
628 sock = SOCKET_I(inode);
630 inode->i_ino = get_next_ino();
631 inode->i_mode = S_IFSOCK | S_IRWXUGO;
632 inode->i_uid = current_fsuid();
633 inode->i_gid = current_fsgid();
634 inode->i_op = &sockfs_inode_ops;
638 EXPORT_SYMBOL(sock_alloc);
640 static void __sock_release(struct socket *sock, struct inode *inode)
642 const struct proto_ops *ops = READ_ONCE(sock->ops);
645 struct module *owner = ops->owner;
657 if (sock->wq.fasync_list)
658 pr_err("%s: fasync list not empty!\n", __func__);
661 iput(SOCK_INODE(sock));
668 * sock_release - close a socket
669 * @sock: socket to close
671 * The socket is released from the protocol stack if it has a release
672 * callback, and the inode is then released if the socket is bound to
673 * an inode not a file.
675 void sock_release(struct socket *sock)
677 __sock_release(sock, NULL);
679 EXPORT_SYMBOL(sock_release);
681 void __sock_tx_timestamp(__u32 tsflags, __u8 *tx_flags)
683 u8 flags = *tx_flags;
685 if (tsflags & SOF_TIMESTAMPING_TX_HARDWARE)
686 flags |= SKBTX_HW_TSTAMP_NOBPF;
688 if (tsflags & SOF_TIMESTAMPING_TX_SOFTWARE)
689 flags |= SKBTX_SW_TSTAMP;
691 if (tsflags & SOF_TIMESTAMPING_TX_SCHED)
692 flags |= SKBTX_SCHED_TSTAMP;
694 if (tsflags & SOF_TIMESTAMPING_TX_COMPLETION)
695 flags |= SKBTX_COMPLETION_TSTAMP;
699 EXPORT_SYMBOL(__sock_tx_timestamp);
701 INDIRECT_CALLABLE_DECLARE(int inet_sendmsg(struct socket *, struct msghdr *,
703 INDIRECT_CALLABLE_DECLARE(int inet6_sendmsg(struct socket *, struct msghdr *,
706 static noinline void call_trace_sock_send_length(struct sock *sk, int ret,
709 trace_sock_send_length(sk, ret, 0);
712 static inline int sock_sendmsg_nosec(struct socket *sock, struct msghdr *msg)
714 int ret = INDIRECT_CALL_INET(READ_ONCE(sock->ops)->sendmsg, inet6_sendmsg,
715 inet_sendmsg, sock, msg,
717 BUG_ON(ret == -EIOCBQUEUED);
719 if (trace_sock_send_length_enabled())
720 call_trace_sock_send_length(sock->sk, ret, 0);
724 static int __sock_sendmsg(struct socket *sock, struct msghdr *msg)
726 int err = security_socket_sendmsg(sock, msg,
729 return err ?: sock_sendmsg_nosec(sock, msg);
733 * sock_sendmsg - send a message through @sock
735 * @msg: message to send
737 * Sends @msg through @sock, passing through LSM.
738 * Returns the number of bytes sent, or an error code.
740 int sock_sendmsg(struct socket *sock, struct msghdr *msg)
742 struct sockaddr_storage *save_addr = (struct sockaddr_storage *)msg->msg_name;
743 struct sockaddr_storage address;
744 int save_len = msg->msg_namelen;
748 memcpy(&address, msg->msg_name, msg->msg_namelen);
749 msg->msg_name = &address;
752 ret = __sock_sendmsg(sock, msg);
753 msg->msg_name = save_addr;
754 msg->msg_namelen = save_len;
758 EXPORT_SYMBOL(sock_sendmsg);
761 * kernel_sendmsg - send a message through @sock (kernel-space)
763 * @msg: message header
765 * @num: vec array length
766 * @size: total message data size
768 * Builds the message data with @vec and sends it through @sock.
769 * Returns the number of bytes sent, or an error code.
772 int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
773 struct kvec *vec, size_t num, size_t size)
775 iov_iter_kvec(&msg->msg_iter, ITER_SOURCE, vec, num, size);
776 return sock_sendmsg(sock, msg);
778 EXPORT_SYMBOL(kernel_sendmsg);
780 static bool skb_is_err_queue(const struct sk_buff *skb)
782 /* pkt_type of skbs enqueued on the error queue are set to
783 * PACKET_OUTGOING in skb_set_err_queue(). This is only safe to do
784 * in recvmsg, since skbs received on a local socket will never
785 * have a pkt_type of PACKET_OUTGOING.
787 return skb->pkt_type == PACKET_OUTGOING;
790 /* On transmit, software and hardware timestamps are returned independently.
791 * As the two skb clones share the hardware timestamp, which may be updated
792 * before the software timestamp is received, a hardware TX timestamp may be
793 * returned only if there is no software TX timestamp. Ignore false software
794 * timestamps, which may be made in the __sock_recv_timestamp() call when the
795 * option SO_TIMESTAMP_OLD(NS) is enabled on the socket, even when the skb has a
796 * hardware timestamp.
798 static bool skb_is_swtx_tstamp(const struct sk_buff *skb, int false_tstamp)
800 return skb->tstamp && !false_tstamp && skb_is_err_queue(skb);
803 static ktime_t get_timestamp(struct sock *sk, struct sk_buff *skb, int *if_index)
805 bool cycles = READ_ONCE(sk->sk_tsflags) & SOF_TIMESTAMPING_BIND_PHC;
806 struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
807 struct net_device *orig_dev;
811 orig_dev = dev_get_by_napi_id(skb_napi_id(skb));
813 *if_index = orig_dev->ifindex;
814 hwtstamp = netdev_get_tstamp(orig_dev, shhwtstamps, cycles);
816 hwtstamp = shhwtstamps->hwtstamp;
823 static void put_ts_pktinfo(struct msghdr *msg, struct sk_buff *skb,
826 struct scm_ts_pktinfo ts_pktinfo;
827 struct net_device *orig_dev;
829 if (!skb_mac_header_was_set(skb))
832 memset(&ts_pktinfo, 0, sizeof(ts_pktinfo));
836 orig_dev = dev_get_by_napi_id(skb_napi_id(skb));
838 if_index = orig_dev->ifindex;
841 ts_pktinfo.if_index = if_index;
843 ts_pktinfo.pkt_length = skb->len - skb_mac_offset(skb);
844 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPING_PKTINFO,
845 sizeof(ts_pktinfo), &ts_pktinfo);
848 bool skb_has_tx_timestamp(struct sk_buff *skb, const struct sock *sk)
850 const struct sock_exterr_skb *serr = SKB_EXT_ERR(skb);
851 u32 tsflags = READ_ONCE(sk->sk_tsflags);
853 if (serr->ee.ee_errno != ENOMSG ||
854 serr->ee.ee_origin != SO_EE_ORIGIN_TIMESTAMPING)
857 /* software time stamp available and wanted */
858 if ((tsflags & SOF_TIMESTAMPING_SOFTWARE) && skb->tstamp)
860 /* hardware time stamps available and wanted */
861 return (tsflags & SOF_TIMESTAMPING_RAW_HARDWARE) &&
862 skb_hwtstamps(skb)->hwtstamp;
865 int skb_get_tx_timestamp(struct sk_buff *skb, struct sock *sk,
866 struct timespec64 *ts)
868 u32 tsflags = READ_ONCE(sk->sk_tsflags);
872 if ((tsflags & SOF_TIMESTAMPING_SOFTWARE) &&
873 ktime_to_timespec64_cond(skb->tstamp, ts))
874 return SOF_TIMESTAMPING_TX_SOFTWARE;
876 if (!(tsflags & SOF_TIMESTAMPING_RAW_HARDWARE) ||
877 skb_is_swtx_tstamp(skb, false))
880 if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP_NETDEV)
881 hwtstamp = get_timestamp(sk, skb, &if_index);
883 hwtstamp = skb_hwtstamps(skb)->hwtstamp;
885 if (tsflags & SOF_TIMESTAMPING_BIND_PHC)
886 hwtstamp = ptp_convert_timestamp(&hwtstamp,
887 READ_ONCE(sk->sk_bind_phc));
888 if (!ktime_to_timespec64_cond(hwtstamp, ts))
891 return SOF_TIMESTAMPING_TX_HARDWARE;
895 * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
897 void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
900 int need_software_tstamp = sock_flag(sk, SOCK_RCVTSTAMP);
901 int new_tstamp = sock_flag(sk, SOCK_TSTAMP_NEW);
902 struct scm_timestamping_internal tss;
903 int empty = 1, false_tstamp = 0;
904 struct skb_shared_hwtstamps *shhwtstamps =
910 /* Race occurred between timestamp enabling and packet
911 receiving. Fill in the current time for now. */
912 if (need_software_tstamp && skb->tstamp == 0) {
913 __net_timestamp(skb);
917 if (need_software_tstamp) {
918 if (!sock_flag(sk, SOCK_RCVTSTAMPNS)) {
920 struct __kernel_sock_timeval tv;
922 skb_get_new_timestamp(skb, &tv);
923 put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMP_NEW,
926 struct __kernel_old_timeval tv;
928 skb_get_timestamp(skb, &tv);
929 put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMP_OLD,
934 struct __kernel_timespec ts;
936 skb_get_new_timestampns(skb, &ts);
937 put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMPNS_NEW,
940 struct __kernel_old_timespec ts;
942 skb_get_timestampns(skb, &ts);
943 put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMPNS_OLD,
949 memset(&tss, 0, sizeof(tss));
950 tsflags = READ_ONCE(sk->sk_tsflags);
951 if ((tsflags & SOF_TIMESTAMPING_SOFTWARE &&
952 (tsflags & SOF_TIMESTAMPING_RX_SOFTWARE ||
953 skb_is_err_queue(skb) ||
954 !(tsflags & SOF_TIMESTAMPING_OPT_RX_FILTER))) &&
955 ktime_to_timespec64_cond(skb->tstamp, tss.ts + 0))
958 (tsflags & SOF_TIMESTAMPING_RAW_HARDWARE &&
959 (tsflags & SOF_TIMESTAMPING_RX_HARDWARE ||
960 skb_is_err_queue(skb) ||
961 !(tsflags & SOF_TIMESTAMPING_OPT_RX_FILTER))) &&
962 !skb_is_swtx_tstamp(skb, false_tstamp)) {
964 if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP_NETDEV)
965 hwtstamp = get_timestamp(sk, skb, &if_index);
967 hwtstamp = shhwtstamps->hwtstamp;
969 if (tsflags & SOF_TIMESTAMPING_BIND_PHC)
970 hwtstamp = ptp_convert_timestamp(&hwtstamp,
971 READ_ONCE(sk->sk_bind_phc));
973 if (ktime_to_timespec64_cond(hwtstamp, tss.ts + 2)) {
976 if ((tsflags & SOF_TIMESTAMPING_OPT_PKTINFO) &&
977 !skb_is_err_queue(skb))
978 put_ts_pktinfo(msg, skb, if_index);
982 if (sock_flag(sk, SOCK_TSTAMP_NEW))
983 put_cmsg_scm_timestamping64(msg, &tss);
985 put_cmsg_scm_timestamping(msg, &tss);
987 if (skb_is_err_queue(skb) && skb->len &&
988 SKB_EXT_ERR(skb)->opt_stats)
989 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPING_OPT_STATS,
990 skb->len, skb->data);
993 EXPORT_SYMBOL_GPL(__sock_recv_timestamp);
995 #ifdef CONFIG_WIRELESS
996 void __sock_recv_wifi_status(struct msghdr *msg, struct sock *sk,
1001 if (!sock_flag(sk, SOCK_WIFI_STATUS))
1003 if (!skb->wifi_acked_valid)
1006 ack = skb->wifi_acked;
1008 put_cmsg(msg, SOL_SOCKET, SCM_WIFI_STATUS, sizeof(ack), &ack);
1010 EXPORT_SYMBOL_GPL(__sock_recv_wifi_status);
1013 static inline void sock_recv_drops(struct msghdr *msg, struct sock *sk,
1014 struct sk_buff *skb)
1016 if (sock_flag(sk, SOCK_RXQ_OVFL) && skb && SOCK_SKB_CB(skb)->dropcount)
1017 put_cmsg(msg, SOL_SOCKET, SO_RXQ_OVFL,
1018 sizeof(__u32), &SOCK_SKB_CB(skb)->dropcount);
1021 static void sock_recv_mark(struct msghdr *msg, struct sock *sk,
1022 struct sk_buff *skb)
1024 if (sock_flag(sk, SOCK_RCVMARK) && skb) {
1025 /* We must use a bounce buffer for CONFIG_HARDENED_USERCOPY=y */
1026 __u32 mark = skb->mark;
1028 put_cmsg(msg, SOL_SOCKET, SO_MARK, sizeof(__u32), &mark);
1032 static void sock_recv_priority(struct msghdr *msg, struct sock *sk,
1033 struct sk_buff *skb)
1035 if (sock_flag(sk, SOCK_RCVPRIORITY) && skb) {
1036 __u32 priority = skb->priority;
1038 put_cmsg(msg, SOL_SOCKET, SO_PRIORITY, sizeof(__u32), &priority);
1042 void __sock_recv_cmsgs(struct msghdr *msg, struct sock *sk,
1043 struct sk_buff *skb)
1045 sock_recv_timestamp(msg, sk, skb);
1046 sock_recv_drops(msg, sk, skb);
1047 sock_recv_mark(msg, sk, skb);
1048 sock_recv_priority(msg, sk, skb);
1050 EXPORT_SYMBOL_GPL(__sock_recv_cmsgs);
1052 INDIRECT_CALLABLE_DECLARE(int inet_recvmsg(struct socket *, struct msghdr *,
1054 INDIRECT_CALLABLE_DECLARE(int inet6_recvmsg(struct socket *, struct msghdr *,
1057 static noinline void call_trace_sock_recv_length(struct sock *sk, int ret, int flags)
1059 trace_sock_recv_length(sk, ret, flags);
1062 static inline int sock_recvmsg_nosec(struct socket *sock, struct msghdr *msg,
1065 int ret = INDIRECT_CALL_INET(READ_ONCE(sock->ops)->recvmsg,
1067 inet_recvmsg, sock, msg,
1068 msg_data_left(msg), flags);
1069 if (trace_sock_recv_length_enabled())
1070 call_trace_sock_recv_length(sock->sk, ret, flags);
1075 * sock_recvmsg - receive a message from @sock
1077 * @msg: message to receive
1078 * @flags: message flags
1080 * Receives @msg from @sock, passing through LSM. Returns the total number
1081 * of bytes received, or an error.
1083 int sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags)
1085 int err = security_socket_recvmsg(sock, msg, msg_data_left(msg), flags);
1087 return err ?: sock_recvmsg_nosec(sock, msg, flags);
1089 EXPORT_SYMBOL(sock_recvmsg);
1092 * kernel_recvmsg - Receive a message from a socket (kernel space)
1093 * @sock: The socket to receive the message from
1094 * @msg: Received message
1095 * @vec: Input s/g array for message data
1096 * @num: Size of input s/g array
1097 * @size: Number of bytes to read
1098 * @flags: Message flags (MSG_DONTWAIT, etc...)
1100 * On return the msg structure contains the scatter/gather array passed in the
1101 * vec argument. The array is modified so that it consists of the unfilled
1102 * portion of the original array.
1104 * The returned value is the total number of bytes received, or an error.
1107 int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
1108 struct kvec *vec, size_t num, size_t size, int flags)
1110 msg->msg_control_is_user = false;
1111 iov_iter_kvec(&msg->msg_iter, ITER_DEST, vec, num, size);
1112 return sock_recvmsg(sock, msg, flags);
1114 EXPORT_SYMBOL(kernel_recvmsg);
1116 static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
1117 struct pipe_inode_info *pipe, size_t len,
1120 struct socket *sock = file->private_data;
1121 const struct proto_ops *ops;
1123 ops = READ_ONCE(sock->ops);
1124 if (unlikely(!ops->splice_read))
1125 return copy_splice_read(file, ppos, pipe, len, flags);
1127 return ops->splice_read(sock, ppos, pipe, len, flags);
1130 static void sock_splice_eof(struct file *file)
1132 struct socket *sock = file->private_data;
1133 const struct proto_ops *ops;
1135 ops = READ_ONCE(sock->ops);
1136 if (ops->splice_eof)
1137 ops->splice_eof(sock);
1140 static ssize_t sock_read_iter(struct kiocb *iocb, struct iov_iter *to)
1142 struct file *file = iocb->ki_filp;
1143 struct socket *sock = file->private_data;
1144 struct msghdr msg = {.msg_iter = *to,
1148 if (file->f_flags & O_NONBLOCK || (iocb->ki_flags & IOCB_NOWAIT))
1149 msg.msg_flags = MSG_DONTWAIT;
1151 if (iocb->ki_pos != 0)
1154 if (!iov_iter_count(to)) /* Match SYS5 behaviour */
1157 res = sock_recvmsg(sock, &msg, msg.msg_flags);
1162 static ssize_t sock_write_iter(struct kiocb *iocb, struct iov_iter *from)
1164 struct file *file = iocb->ki_filp;
1165 struct socket *sock = file->private_data;
1166 struct msghdr msg = {.msg_iter = *from,
1170 if (iocb->ki_pos != 0)
1173 if (file->f_flags & O_NONBLOCK || (iocb->ki_flags & IOCB_NOWAIT))
1174 msg.msg_flags = MSG_DONTWAIT;
1176 if (sock->type == SOCK_SEQPACKET)
1177 msg.msg_flags |= MSG_EOR;
1179 res = __sock_sendmsg(sock, &msg);
1180 *from = msg.msg_iter;
1185 * Atomic setting of ioctl hooks to avoid race
1186 * with module unload.
1189 static DEFINE_MUTEX(br_ioctl_mutex);
1190 static int (*br_ioctl_hook)(struct net *net, unsigned int cmd,
1193 void brioctl_set(int (*hook)(struct net *net, unsigned int cmd,
1196 mutex_lock(&br_ioctl_mutex);
1197 br_ioctl_hook = hook;
1198 mutex_unlock(&br_ioctl_mutex);
1200 EXPORT_SYMBOL(brioctl_set);
1202 int br_ioctl_call(struct net *net, unsigned int cmd, void __user *uarg)
1207 request_module("bridge");
1209 mutex_lock(&br_ioctl_mutex);
1211 err = br_ioctl_hook(net, cmd, uarg);
1212 mutex_unlock(&br_ioctl_mutex);
1217 static DEFINE_MUTEX(vlan_ioctl_mutex);
1218 static int (*vlan_ioctl_hook) (struct net *, void __user *arg);
1220 void vlan_ioctl_set(int (*hook) (struct net *, void __user *))
1222 mutex_lock(&vlan_ioctl_mutex);
1223 vlan_ioctl_hook = hook;
1224 mutex_unlock(&vlan_ioctl_mutex);
1226 EXPORT_SYMBOL(vlan_ioctl_set);
1228 static long sock_do_ioctl(struct net *net, struct socket *sock,
1229 unsigned int cmd, unsigned long arg)
1231 const struct proto_ops *ops = READ_ONCE(sock->ops);
1235 void __user *argp = (void __user *)arg;
1238 err = ops->ioctl(sock, cmd, arg);
1241 * If this ioctl is unknown try to hand it down
1242 * to the NIC driver.
1244 if (err != -ENOIOCTLCMD)
1247 if (!is_socket_ioctl_cmd(cmd))
1250 if (get_user_ifreq(&ifr, &data, argp))
1252 err = dev_ioctl(net, cmd, &ifr, data, &need_copyout);
1253 if (!err && need_copyout)
1254 if (put_user_ifreq(&ifr, argp))
1261 * With an ioctl, arg may well be a user mode pointer, but we don't know
1262 * what to do with it - that's up to the protocol still.
1265 static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
1267 const struct proto_ops *ops;
1268 struct socket *sock;
1270 void __user *argp = (void __user *)arg;
1274 sock = file->private_data;
1275 ops = READ_ONCE(sock->ops);
1278 if (unlikely(cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15))) {
1282 if (get_user_ifreq(&ifr, &data, argp))
1284 err = dev_ioctl(net, cmd, &ifr, data, &need_copyout);
1285 if (!err && need_copyout)
1286 if (put_user_ifreq(&ifr, argp))
1289 #ifdef CONFIG_WEXT_CORE
1290 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
1291 err = wext_handle_ioctl(net, cmd, argp);
1298 if (get_user(pid, (int __user *)argp))
1300 err = f_setown(sock->file, pid, 1);
1304 err = put_user(f_getown(sock->file),
1305 (int __user *)argp);
1313 err = br_ioctl_call(net, cmd, argp);
1318 if (!vlan_ioctl_hook)
1319 request_module("8021q");
1321 mutex_lock(&vlan_ioctl_mutex);
1322 if (vlan_ioctl_hook)
1323 err = vlan_ioctl_hook(net, argp);
1324 mutex_unlock(&vlan_ioctl_mutex);
1328 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1331 err = open_related_ns(&net->ns, get_net_ns);
1333 case SIOCGSTAMP_OLD:
1334 case SIOCGSTAMPNS_OLD:
1335 if (!ops->gettstamp) {
1339 err = ops->gettstamp(sock, argp,
1340 cmd == SIOCGSTAMP_OLD,
1341 !IS_ENABLED(CONFIG_64BIT));
1343 case SIOCGSTAMP_NEW:
1344 case SIOCGSTAMPNS_NEW:
1345 if (!ops->gettstamp) {
1349 err = ops->gettstamp(sock, argp,
1350 cmd == SIOCGSTAMP_NEW,
1355 err = dev_ifconf(net, argp);
1359 err = sock_do_ioctl(net, sock, cmd, arg);
1366 * sock_create_lite - creates a socket
1367 * @family: protocol family (AF_INET, ...)
1368 * @type: communication type (SOCK_STREAM, ...)
1369 * @protocol: protocol (0, ...)
1372 * Creates a new socket and assigns it to @res, passing through LSM.
1373 * The new socket initialization is not complete, see kernel_accept().
1374 * Returns 0 or an error. On failure @res is set to %NULL.
1375 * This function internally uses GFP_KERNEL.
1378 int sock_create_lite(int family, int type, int protocol, struct socket **res)
1381 struct socket *sock = NULL;
1383 err = security_socket_create(family, type, protocol, 1);
1387 sock = sock_alloc();
1394 err = security_socket_post_create(sock, family, type, protocol, 1);
1406 EXPORT_SYMBOL(sock_create_lite);
1408 /* No kernel lock held - perfect */
1409 static __poll_t sock_poll(struct file *file, poll_table *wait)
1411 struct socket *sock = file->private_data;
1412 const struct proto_ops *ops = READ_ONCE(sock->ops);
1413 __poll_t events = poll_requested_events(wait), flag = 0;
1418 if (sk_can_busy_loop(sock->sk)) {
1419 /* poll once if requested by the syscall */
1420 if (events & POLL_BUSY_LOOP)
1421 sk_busy_loop(sock->sk, 1);
1423 /* if this socket can poll_ll, tell the system call */
1424 flag = POLL_BUSY_LOOP;
1427 return ops->poll(file, sock, wait) | flag;
1430 static int sock_mmap(struct file *file, struct vm_area_struct *vma)
1432 struct socket *sock = file->private_data;
1434 return READ_ONCE(sock->ops)->mmap(file, sock, vma);
1437 static int sock_close(struct inode *inode, struct file *filp)
1439 __sock_release(SOCKET_I(inode), inode);
1444 * Update the socket async list
1446 * Fasync_list locking strategy.
1448 * 1. fasync_list is modified only under process context socket lock
1449 * i.e. under semaphore.
1450 * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
1451 * or under socket lock
1454 static int sock_fasync(int fd, struct file *filp, int on)
1456 struct socket *sock = filp->private_data;
1457 struct sock *sk = sock->sk;
1458 struct socket_wq *wq = &sock->wq;
1464 fasync_helper(fd, filp, on, &wq->fasync_list);
1466 if (!wq->fasync_list)
1467 sock_reset_flag(sk, SOCK_FASYNC);
1469 sock_set_flag(sk, SOCK_FASYNC);
1475 /* This function may be called only under rcu_lock */
1477 int sock_wake_async(struct socket_wq *wq, int how, int band)
1479 if (!wq || !wq->fasync_list)
1483 case SOCK_WAKE_WAITD:
1484 if (test_bit(SOCKWQ_ASYNC_WAITDATA, &wq->flags))
1487 case SOCK_WAKE_SPACE:
1488 if (!test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags))
1493 kill_fasync(&wq->fasync_list, SIGIO, band);
1496 kill_fasync(&wq->fasync_list, SIGURG, band);
1501 EXPORT_SYMBOL(sock_wake_async);
1504 * __sock_create - creates a socket
1505 * @net: net namespace
1506 * @family: protocol family (AF_INET, ...)
1507 * @type: communication type (SOCK_STREAM, ...)
1508 * @protocol: protocol (0, ...)
1510 * @kern: boolean for kernel space sockets
1512 * Creates a new socket and assigns it to @res, passing through LSM.
1513 * Returns 0 or an error. On failure @res is set to %NULL. @kern must
1514 * be set to true if the socket resides in kernel space.
1515 * This function internally uses GFP_KERNEL.
1518 int __sock_create(struct net *net, int family, int type, int protocol,
1519 struct socket **res, int kern)
1522 struct socket *sock;
1523 const struct net_proto_family *pf;
1526 * Check protocol is in range
1528 if (family < 0 || family >= NPROTO)
1529 return -EAFNOSUPPORT;
1530 if (type < 0 || type >= SOCK_MAX)
1535 This uglymoron is moved from INET layer to here to avoid
1536 deadlock in module load.
1538 if (family == PF_INET && type == SOCK_PACKET) {
1539 pr_info_once("%s uses obsolete (PF_INET,SOCK_PACKET)\n",
1544 err = security_socket_create(family, type, protocol, kern);
1549 * Allocate the socket and allow the family to set things up. if
1550 * the protocol is 0, the family is instructed to select an appropriate
1553 sock = sock_alloc();
1555 net_warn_ratelimited("socket: no more sockets\n");
1556 return -ENFILE; /* Not exactly a match, but its the
1557 closest posix thing */
1562 #ifdef CONFIG_MODULES
1563 /* Attempt to load a protocol module if the find failed.
1565 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
1566 * requested real, full-featured networking support upon configuration.
1567 * Otherwise module support will break!
1569 if (rcu_access_pointer(net_families[family]) == NULL)
1570 request_module("net-pf-%d", family);
1574 pf = rcu_dereference(net_families[family]);
1575 err = -EAFNOSUPPORT;
1580 * We will call the ->create function, that possibly is in a loadable
1581 * module, so we have to bump that loadable module refcnt first.
1583 if (!try_module_get(pf->owner))
1586 /* Now protected by module ref count */
1589 err = pf->create(net, sock, protocol, kern);
1591 /* ->create should release the allocated sock->sk object on error
1592 * and make sure sock->sk is set to NULL to avoid use-after-free
1594 DEBUG_NET_WARN_ONCE(sock->sk,
1595 "%ps must clear sock->sk on failure, family: %d, type: %d, protocol: %d\n",
1596 pf->create, family, type, protocol);
1597 goto out_module_put;
1601 * Now to bump the refcnt of the [loadable] module that owns this
1602 * socket at sock_release time we decrement its refcnt.
1604 if (!try_module_get(sock->ops->owner))
1605 goto out_module_busy;
1608 * Now that we're done with the ->create function, the [loadable]
1609 * module can have its refcnt decremented
1611 module_put(pf->owner);
1612 err = security_socket_post_create(sock, family, type, protocol, kern);
1614 goto out_sock_release;
1620 err = -EAFNOSUPPORT;
1623 module_put(pf->owner);
1630 goto out_sock_release;
1632 EXPORT_SYMBOL(__sock_create);
1635 * sock_create - creates a socket
1636 * @family: protocol family (AF_INET, ...)
1637 * @type: communication type (SOCK_STREAM, ...)
1638 * @protocol: protocol (0, ...)
1641 * A wrapper around __sock_create().
1642 * Returns 0 or an error. This function internally uses GFP_KERNEL.
1645 int sock_create(int family, int type, int protocol, struct socket **res)
1647 return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0);
1649 EXPORT_SYMBOL(sock_create);
1652 * sock_create_kern - creates a socket (kernel space)
1653 * @net: net namespace
1654 * @family: protocol family (AF_INET, ...)
1655 * @type: communication type (SOCK_STREAM, ...)
1656 * @protocol: protocol (0, ...)
1659 * A wrapper around __sock_create().
1660 * Returns 0 or an error. This function internally uses GFP_KERNEL.
1663 int sock_create_kern(struct net *net, int family, int type, int protocol, struct socket **res)
1665 return __sock_create(net, family, type, protocol, res, 1);
1667 EXPORT_SYMBOL(sock_create_kern);
1669 static struct socket *__sys_socket_create(int family, int type, int protocol)
1671 struct socket *sock;
1674 /* Check the SOCK_* constants for consistency. */
1675 BUILD_BUG_ON(SOCK_CLOEXEC != O_CLOEXEC);
1676 BUILD_BUG_ON((SOCK_MAX | SOCK_TYPE_MASK) != SOCK_TYPE_MASK);
1677 BUILD_BUG_ON(SOCK_CLOEXEC & SOCK_TYPE_MASK);
1678 BUILD_BUG_ON(SOCK_NONBLOCK & SOCK_TYPE_MASK);
1680 if ((type & ~SOCK_TYPE_MASK) & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1681 return ERR_PTR(-EINVAL);
1682 type &= SOCK_TYPE_MASK;
1684 retval = sock_create(family, type, protocol, &sock);
1686 return ERR_PTR(retval);
1691 struct file *__sys_socket_file(int family, int type, int protocol)
1693 struct socket *sock;
1696 sock = __sys_socket_create(family, type, protocol);
1698 return ERR_CAST(sock);
1700 flags = type & ~SOCK_TYPE_MASK;
1701 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1702 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1704 return sock_alloc_file(sock, flags, NULL);
1707 /* A hook for bpf progs to attach to and update socket protocol.
1709 * A static noinline declaration here could cause the compiler to
1710 * optimize away the function. A global noinline declaration will
1711 * keep the definition, but may optimize away the callsite.
1712 * Therefore, __weak is needed to ensure that the call is still
1713 * emitted, by telling the compiler that we don't know what the
1714 * function might eventually be.
1719 __weak noinline int update_socket_protocol(int family, int type, int protocol)
1726 int __sys_socket(int family, int type, int protocol)
1728 struct socket *sock;
1731 sock = __sys_socket_create(family, type,
1732 update_socket_protocol(family, type, protocol));
1734 return PTR_ERR(sock);
1736 flags = type & ~SOCK_TYPE_MASK;
1737 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1738 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1740 return sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK));
1743 SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol)
1745 return __sys_socket(family, type, protocol);
1749 * Create a pair of connected sockets.
1752 int __sys_socketpair(int family, int type, int protocol, int __user *usockvec)
1754 struct socket *sock1, *sock2;
1756 struct file *newfile1, *newfile2;
1759 flags = type & ~SOCK_TYPE_MASK;
1760 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1762 type &= SOCK_TYPE_MASK;
1764 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1765 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1768 * reserve descriptors and make sure we won't fail
1769 * to return them to userland.
1771 fd1 = get_unused_fd_flags(flags);
1772 if (unlikely(fd1 < 0))
1775 fd2 = get_unused_fd_flags(flags);
1776 if (unlikely(fd2 < 0)) {
1781 err = put_user(fd1, &usockvec[0]);
1785 err = put_user(fd2, &usockvec[1]);
1790 * Obtain the first socket and check if the underlying protocol
1791 * supports the socketpair call.
1794 err = sock_create(family, type, protocol, &sock1);
1795 if (unlikely(err < 0))
1798 err = sock_create(family, type, protocol, &sock2);
1799 if (unlikely(err < 0)) {
1800 sock_release(sock1);
1804 err = security_socket_socketpair(sock1, sock2);
1805 if (unlikely(err)) {
1806 sock_release(sock2);
1807 sock_release(sock1);
1811 err = READ_ONCE(sock1->ops)->socketpair(sock1, sock2);
1812 if (unlikely(err < 0)) {
1813 sock_release(sock2);
1814 sock_release(sock1);
1818 newfile1 = sock_alloc_file(sock1, flags, NULL);
1819 if (IS_ERR(newfile1)) {
1820 err = PTR_ERR(newfile1);
1821 sock_release(sock2);
1825 newfile2 = sock_alloc_file(sock2, flags, NULL);
1826 if (IS_ERR(newfile2)) {
1827 err = PTR_ERR(newfile2);
1832 audit_fd_pair(fd1, fd2);
1834 fd_install(fd1, newfile1);
1835 fd_install(fd2, newfile2);
1844 SYSCALL_DEFINE4(socketpair, int, family, int, type, int, protocol,
1845 int __user *, usockvec)
1847 return __sys_socketpair(family, type, protocol, usockvec);
1850 int __sys_bind_socket(struct socket *sock, struct sockaddr_storage *address,
1855 err = security_socket_bind(sock, (struct sockaddr *)address,
1858 err = READ_ONCE(sock->ops)->bind(sock,
1859 (struct sockaddr *)address,
1865 * Bind a name to a socket. Nothing much to do here since it's
1866 * the protocol's responsibility to handle the local address.
1868 * We move the socket address to kernel space before we call
1869 * the protocol layer (having also checked the address is ok).
1872 int __sys_bind(int fd, struct sockaddr __user *umyaddr, int addrlen)
1874 struct socket *sock;
1875 struct sockaddr_storage address;
1881 sock = sock_from_file(fd_file(f));
1882 if (unlikely(!sock))
1885 err = move_addr_to_kernel(umyaddr, addrlen, &address);
1889 return __sys_bind_socket(sock, &address, addrlen);
1892 SYSCALL_DEFINE3(bind, int, fd, struct sockaddr __user *, umyaddr, int, addrlen)
1894 return __sys_bind(fd, umyaddr, addrlen);
1898 * Perform a listen. Basically, we allow the protocol to do anything
1899 * necessary for a listen, and if that works, we mark the socket as
1900 * ready for listening.
1902 int __sys_listen_socket(struct socket *sock, int backlog)
1906 somaxconn = READ_ONCE(sock_net(sock->sk)->core.sysctl_somaxconn);
1907 if ((unsigned int)backlog > somaxconn)
1908 backlog = somaxconn;
1910 err = security_socket_listen(sock, backlog);
1912 err = READ_ONCE(sock->ops)->listen(sock, backlog);
1916 int __sys_listen(int fd, int backlog)
1919 struct socket *sock;
1923 sock = sock_from_file(fd_file(f));
1924 if (unlikely(!sock))
1927 return __sys_listen_socket(sock, backlog);
1930 SYSCALL_DEFINE2(listen, int, fd, int, backlog)
1932 return __sys_listen(fd, backlog);
1935 struct file *do_accept(struct file *file, struct proto_accept_arg *arg,
1936 struct sockaddr __user *upeer_sockaddr,
1937 int __user *upeer_addrlen, int flags)
1939 struct socket *sock, *newsock;
1940 struct file *newfile;
1942 struct sockaddr_storage address;
1943 const struct proto_ops *ops;
1945 sock = sock_from_file(file);
1947 return ERR_PTR(-ENOTSOCK);
1949 newsock = sock_alloc();
1951 return ERR_PTR(-ENFILE);
1952 ops = READ_ONCE(sock->ops);
1954 newsock->type = sock->type;
1958 * We don't need try_module_get here, as the listening socket (sock)
1959 * has the protocol module (sock->ops->owner) held.
1961 __module_get(ops->owner);
1963 newfile = sock_alloc_file(newsock, flags, sock->sk->sk_prot_creator->name);
1964 if (IS_ERR(newfile))
1967 err = security_socket_accept(sock, newsock);
1971 arg->flags |= sock->file->f_flags;
1972 err = ops->accept(sock, newsock, arg);
1976 if (upeer_sockaddr) {
1977 len = ops->getname(newsock, (struct sockaddr *)&address, 2);
1979 err = -ECONNABORTED;
1982 err = move_addr_to_user(&address,
1983 len, upeer_sockaddr, upeer_addrlen);
1988 /* File flags are not inherited via accept() unlike another OSes. */
1992 return ERR_PTR(err);
1995 static int __sys_accept4_file(struct file *file, struct sockaddr __user *upeer_sockaddr,
1996 int __user *upeer_addrlen, int flags)
1998 struct proto_accept_arg arg = { };
1999 struct file *newfile;
2002 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
2005 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
2006 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
2008 newfd = get_unused_fd_flags(flags);
2009 if (unlikely(newfd < 0))
2012 newfile = do_accept(file, &arg, upeer_sockaddr, upeer_addrlen,
2014 if (IS_ERR(newfile)) {
2015 put_unused_fd(newfd);
2016 return PTR_ERR(newfile);
2018 fd_install(newfd, newfile);
2023 * For accept, we attempt to create a new socket, set up the link
2024 * with the client, wake up the client, then return the new
2025 * connected fd. We collect the address of the connector in kernel
2026 * space and move it to user at the very end. This is unclean because
2027 * we open the socket then return an error.
2029 * 1003.1g adds the ability to recvmsg() to query connection pending
2030 * status to recvmsg. We need to add that support in a way thats
2031 * clean when we restructure accept also.
2034 int __sys_accept4(int fd, struct sockaddr __user *upeer_sockaddr,
2035 int __user *upeer_addrlen, int flags)
2041 return __sys_accept4_file(fd_file(f), upeer_sockaddr,
2042 upeer_addrlen, flags);
2045 SYSCALL_DEFINE4(accept4, int, fd, struct sockaddr __user *, upeer_sockaddr,
2046 int __user *, upeer_addrlen, int, flags)
2048 return __sys_accept4(fd, upeer_sockaddr, upeer_addrlen, flags);
2051 SYSCALL_DEFINE3(accept, int, fd, struct sockaddr __user *, upeer_sockaddr,
2052 int __user *, upeer_addrlen)
2054 return __sys_accept4(fd, upeer_sockaddr, upeer_addrlen, 0);
2058 * Attempt to connect to a socket with the server address. The address
2059 * is in user space so we verify it is OK and move it to kernel space.
2061 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
2064 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
2065 * other SEQPACKET protocols that take time to connect() as it doesn't
2066 * include the -EINPROGRESS status for such sockets.
2069 int __sys_connect_file(struct file *file, struct sockaddr_storage *address,
2070 int addrlen, int file_flags)
2072 struct socket *sock;
2075 sock = sock_from_file(file);
2082 security_socket_connect(sock, (struct sockaddr *)address, addrlen);
2086 err = READ_ONCE(sock->ops)->connect(sock, (struct sockaddr *)address,
2087 addrlen, sock->file->f_flags | file_flags);
2092 int __sys_connect(int fd, struct sockaddr __user *uservaddr, int addrlen)
2094 struct sockaddr_storage address;
2101 ret = move_addr_to_kernel(uservaddr, addrlen, &address);
2105 return __sys_connect_file(fd_file(f), &address, addrlen, 0);
2108 SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
2111 return __sys_connect(fd, uservaddr, addrlen);
2115 * Get the local address ('name') of a socket object. Move the obtained
2116 * name to user space.
2119 int __sys_getsockname(int fd, struct sockaddr __user *usockaddr,
2120 int __user *usockaddr_len)
2122 struct socket *sock;
2123 struct sockaddr_storage address;
2129 sock = sock_from_file(fd_file(f));
2130 if (unlikely(!sock))
2133 err = security_socket_getsockname(sock);
2137 err = READ_ONCE(sock->ops)->getname(sock, (struct sockaddr *)&address, 0);
2141 /* "err" is actually length in this case */
2142 return move_addr_to_user(&address, err, usockaddr, usockaddr_len);
2145 SYSCALL_DEFINE3(getsockname, int, fd, struct sockaddr __user *, usockaddr,
2146 int __user *, usockaddr_len)
2148 return __sys_getsockname(fd, usockaddr, usockaddr_len);
2152 * Get the remote address ('name') of a socket object. Move the obtained
2153 * name to user space.
2156 int __sys_getpeername(int fd, struct sockaddr __user *usockaddr,
2157 int __user *usockaddr_len)
2159 struct socket *sock;
2160 struct sockaddr_storage address;
2166 sock = sock_from_file(fd_file(f));
2167 if (unlikely(!sock))
2170 err = security_socket_getpeername(sock);
2174 err = READ_ONCE(sock->ops)->getname(sock, (struct sockaddr *)&address, 1);
2178 /* "err" is actually length in this case */
2179 return move_addr_to_user(&address, err, usockaddr, usockaddr_len);
2182 SYSCALL_DEFINE3(getpeername, int, fd, struct sockaddr __user *, usockaddr,
2183 int __user *, usockaddr_len)
2185 return __sys_getpeername(fd, usockaddr, usockaddr_len);
2189 * Send a datagram to a given address. We move the address into kernel
2190 * space and check the user space data area is readable before invoking
2193 int __sys_sendto(int fd, void __user *buff, size_t len, unsigned int flags,
2194 struct sockaddr __user *addr, int addr_len)
2196 struct socket *sock;
2197 struct sockaddr_storage address;
2201 err = import_ubuf(ITER_SOURCE, buff, len, &msg.msg_iter);
2208 sock = sock_from_file(fd_file(f));
2209 if (unlikely(!sock))
2212 msg.msg_name = NULL;
2213 msg.msg_control = NULL;
2214 msg.msg_controllen = 0;
2215 msg.msg_namelen = 0;
2216 msg.msg_ubuf = NULL;
2218 err = move_addr_to_kernel(addr, addr_len, &address);
2221 msg.msg_name = (struct sockaddr *)&address;
2222 msg.msg_namelen = addr_len;
2224 flags &= ~MSG_INTERNAL_SENDMSG_FLAGS;
2225 if (sock->file->f_flags & O_NONBLOCK)
2226 flags |= MSG_DONTWAIT;
2227 msg.msg_flags = flags;
2228 return __sock_sendmsg(sock, &msg);
2231 SYSCALL_DEFINE6(sendto, int, fd, void __user *, buff, size_t, len,
2232 unsigned int, flags, struct sockaddr __user *, addr,
2235 return __sys_sendto(fd, buff, len, flags, addr, addr_len);
2239 * Send a datagram down a socket.
2242 SYSCALL_DEFINE4(send, int, fd, void __user *, buff, size_t, len,
2243 unsigned int, flags)
2245 return __sys_sendto(fd, buff, len, flags, NULL, 0);
2249 * Receive a frame from the socket and optionally record the address of the
2250 * sender. We verify the buffers are writable and if needed move the
2251 * sender address from kernel to user space.
2253 int __sys_recvfrom(int fd, void __user *ubuf, size_t size, unsigned int flags,
2254 struct sockaddr __user *addr, int __user *addr_len)
2256 struct sockaddr_storage address;
2257 struct msghdr msg = {
2258 /* Save some cycles and don't copy the address if not needed */
2259 .msg_name = addr ? (struct sockaddr *)&address : NULL,
2261 struct socket *sock;
2264 err = import_ubuf(ITER_DEST, ubuf, size, &msg.msg_iter);
2272 sock = sock_from_file(fd_file(f));
2273 if (unlikely(!sock))
2276 if (sock->file->f_flags & O_NONBLOCK)
2277 flags |= MSG_DONTWAIT;
2278 err = sock_recvmsg(sock, &msg, flags);
2280 if (err >= 0 && addr != NULL) {
2281 err2 = move_addr_to_user(&address,
2282 msg.msg_namelen, addr, addr_len);
2289 SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, ubuf, size_t, size,
2290 unsigned int, flags, struct sockaddr __user *, addr,
2291 int __user *, addr_len)
2293 return __sys_recvfrom(fd, ubuf, size, flags, addr, addr_len);
2297 * Receive a datagram from a socket.
2300 SYSCALL_DEFINE4(recv, int, fd, void __user *, ubuf, size_t, size,
2301 unsigned int, flags)
2303 return __sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
2306 static bool sock_use_custom_sol_socket(const struct socket *sock)
2308 return test_bit(SOCK_CUSTOM_SOCKOPT, &sock->flags);
2311 int do_sock_setsockopt(struct socket *sock, bool compat, int level,
2312 int optname, sockptr_t optval, int optlen)
2314 const struct proto_ops *ops;
2315 char *kernel_optval = NULL;
2321 err = security_socket_setsockopt(sock, level, optname);
2326 err = BPF_CGROUP_RUN_PROG_SETSOCKOPT(sock->sk, &level, &optname,
2337 optval = KERNEL_SOCKPTR(kernel_optval);
2338 ops = READ_ONCE(sock->ops);
2339 if (level == SOL_SOCKET && !sock_use_custom_sol_socket(sock))
2340 err = sock_setsockopt(sock, level, optname, optval, optlen);
2341 else if (unlikely(!ops->setsockopt))
2344 err = ops->setsockopt(sock, level, optname, optval,
2346 kfree(kernel_optval);
2350 EXPORT_SYMBOL(do_sock_setsockopt);
2352 /* Set a socket option. Because we don't know the option lengths we have
2353 * to pass the user mode parameter for the protocols to sort out.
2355 int __sys_setsockopt(int fd, int level, int optname, char __user *user_optval,
2358 sockptr_t optval = USER_SOCKPTR(user_optval);
2359 bool compat = in_compat_syscall();
2360 struct socket *sock;
2365 sock = sock_from_file(fd_file(f));
2366 if (unlikely(!sock))
2369 return do_sock_setsockopt(sock, compat, level, optname, optval, optlen);
2372 SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname,
2373 char __user *, optval, int, optlen)
2375 return __sys_setsockopt(fd, level, optname, optval, optlen);
2378 INDIRECT_CALLABLE_DECLARE(bool tcp_bpf_bypass_getsockopt(int level,
2381 int do_sock_getsockopt(struct socket *sock, bool compat, int level,
2382 int optname, sockptr_t optval, sockptr_t optlen)
2384 int max_optlen __maybe_unused = 0;
2385 const struct proto_ops *ops;
2388 err = security_socket_getsockopt(sock, level, optname);
2393 copy_from_sockptr(&max_optlen, optlen, sizeof(int));
2395 ops = READ_ONCE(sock->ops);
2396 if (level == SOL_SOCKET) {
2397 err = sk_getsockopt(sock->sk, level, optname, optval, optlen);
2398 } else if (unlikely(!ops->getsockopt)) {
2401 if (WARN_ONCE(optval.is_kernel || optlen.is_kernel,
2402 "Invalid argument type"))
2405 err = ops->getsockopt(sock, level, optname, optval.user,
2410 err = BPF_CGROUP_RUN_PROG_GETSOCKOPT(sock->sk, level, optname,
2411 optval, optlen, max_optlen,
2416 EXPORT_SYMBOL(do_sock_getsockopt);
2419 * Get a socket option. Because we don't know the option lengths we have
2420 * to pass a user mode parameter for the protocols to sort out.
2422 int __sys_getsockopt(int fd, int level, int optname, char __user *optval,
2425 struct socket *sock;
2430 sock = sock_from_file(fd_file(f));
2431 if (unlikely(!sock))
2434 return do_sock_getsockopt(sock, in_compat_syscall(), level, optname,
2435 USER_SOCKPTR(optval), USER_SOCKPTR(optlen));
2438 SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname,
2439 char __user *, optval, int __user *, optlen)
2441 return __sys_getsockopt(fd, level, optname, optval, optlen);
2445 * Shutdown a socket.
2448 int __sys_shutdown_sock(struct socket *sock, int how)
2452 err = security_socket_shutdown(sock, how);
2454 err = READ_ONCE(sock->ops)->shutdown(sock, how);
2459 int __sys_shutdown(int fd, int how)
2461 struct socket *sock;
2466 sock = sock_from_file(fd_file(f));
2467 if (unlikely(!sock))
2470 return __sys_shutdown_sock(sock, how);
2473 SYSCALL_DEFINE2(shutdown, int, fd, int, how)
2475 return __sys_shutdown(fd, how);
2478 /* A couple of helpful macros for getting the address of the 32/64 bit
2479 * fields which are the same type (int / unsigned) on our platforms.
2481 #define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
2482 #define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
2483 #define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
2485 struct used_address {
2486 struct sockaddr_storage name;
2487 unsigned int name_len;
2490 int __copy_msghdr(struct msghdr *kmsg,
2491 struct user_msghdr *msg,
2492 struct sockaddr __user **save_addr)
2496 kmsg->msg_control_is_user = true;
2497 kmsg->msg_get_inq = 0;
2498 kmsg->msg_control_user = msg->msg_control;
2499 kmsg->msg_controllen = msg->msg_controllen;
2500 kmsg->msg_flags = msg->msg_flags;
2502 kmsg->msg_namelen = msg->msg_namelen;
2504 kmsg->msg_namelen = 0;
2506 if (kmsg->msg_namelen < 0)
2509 if (kmsg->msg_namelen > sizeof(struct sockaddr_storage))
2510 kmsg->msg_namelen = sizeof(struct sockaddr_storage);
2513 *save_addr = msg->msg_name;
2515 if (msg->msg_name && kmsg->msg_namelen) {
2517 err = move_addr_to_kernel(msg->msg_name,
2524 kmsg->msg_name = NULL;
2525 kmsg->msg_namelen = 0;
2528 if (msg->msg_iovlen > UIO_MAXIOV)
2531 kmsg->msg_iocb = NULL;
2532 kmsg->msg_ubuf = NULL;
2536 static int copy_msghdr_from_user(struct msghdr *kmsg,
2537 struct user_msghdr __user *umsg,
2538 struct sockaddr __user **save_addr,
2541 struct user_msghdr msg;
2544 if (copy_from_user(&msg, umsg, sizeof(*umsg)))
2547 err = __copy_msghdr(kmsg, &msg, save_addr);
2551 err = import_iovec(save_addr ? ITER_DEST : ITER_SOURCE,
2552 msg.msg_iov, msg.msg_iovlen,
2553 UIO_FASTIOV, iov, &kmsg->msg_iter);
2554 return err < 0 ? err : 0;
2557 static int ____sys_sendmsg(struct socket *sock, struct msghdr *msg_sys,
2558 unsigned int flags, struct used_address *used_address,
2559 unsigned int allowed_msghdr_flags)
2561 unsigned char ctl[sizeof(struct cmsghdr) + 20]
2562 __aligned(sizeof(__kernel_size_t));
2563 /* 20 is size of ipv6_pktinfo */
2564 unsigned char *ctl_buf = ctl;
2570 if (msg_sys->msg_controllen > INT_MAX)
2572 flags |= (msg_sys->msg_flags & allowed_msghdr_flags);
2573 ctl_len = msg_sys->msg_controllen;
2574 if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
2576 cmsghdr_from_user_compat_to_kern(msg_sys, sock->sk, ctl,
2580 ctl_buf = msg_sys->msg_control;
2581 ctl_len = msg_sys->msg_controllen;
2582 } else if (ctl_len) {
2583 BUILD_BUG_ON(sizeof(struct cmsghdr) !=
2584 CMSG_ALIGN(sizeof(struct cmsghdr)));
2585 if (ctl_len > sizeof(ctl)) {
2586 ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
2587 if (ctl_buf == NULL)
2591 if (copy_from_user(ctl_buf, msg_sys->msg_control_user, ctl_len))
2593 msg_sys->msg_control = ctl_buf;
2594 msg_sys->msg_control_is_user = false;
2596 flags &= ~MSG_INTERNAL_SENDMSG_FLAGS;
2597 msg_sys->msg_flags = flags;
2599 if (sock->file->f_flags & O_NONBLOCK)
2600 msg_sys->msg_flags |= MSG_DONTWAIT;
2602 * If this is sendmmsg() and current destination address is same as
2603 * previously succeeded address, omit asking LSM's decision.
2604 * used_address->name_len is initialized to UINT_MAX so that the first
2605 * destination address never matches.
2607 if (used_address && msg_sys->msg_name &&
2608 used_address->name_len == msg_sys->msg_namelen &&
2609 !memcmp(&used_address->name, msg_sys->msg_name,
2610 used_address->name_len)) {
2611 err = sock_sendmsg_nosec(sock, msg_sys);
2614 err = __sock_sendmsg(sock, msg_sys);
2616 * If this is sendmmsg() and sending to current destination address was
2617 * successful, remember it.
2619 if (used_address && err >= 0) {
2620 used_address->name_len = msg_sys->msg_namelen;
2621 if (msg_sys->msg_name)
2622 memcpy(&used_address->name, msg_sys->msg_name,
2623 used_address->name_len);
2628 sock_kfree_s(sock->sk, ctl_buf, ctl_len);
2633 static int sendmsg_copy_msghdr(struct msghdr *msg,
2634 struct user_msghdr __user *umsg, unsigned flags,
2639 if (flags & MSG_CMSG_COMPAT) {
2640 struct compat_msghdr __user *msg_compat;
2642 msg_compat = (struct compat_msghdr __user *) umsg;
2643 err = get_compat_msghdr(msg, msg_compat, NULL, iov);
2645 err = copy_msghdr_from_user(msg, umsg, NULL, iov);
2653 static int ___sys_sendmsg(struct socket *sock, struct user_msghdr __user *msg,
2654 struct msghdr *msg_sys, unsigned int flags,
2655 struct used_address *used_address,
2656 unsigned int allowed_msghdr_flags)
2658 struct sockaddr_storage address;
2659 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
2662 msg_sys->msg_name = &address;
2664 err = sendmsg_copy_msghdr(msg_sys, msg, flags, &iov);
2668 err = ____sys_sendmsg(sock, msg_sys, flags, used_address,
2669 allowed_msghdr_flags);
2675 * BSD sendmsg interface
2677 long __sys_sendmsg_sock(struct socket *sock, struct msghdr *msg,
2680 return ____sys_sendmsg(sock, msg, flags, NULL, 0);
2683 long __sys_sendmsg(int fd, struct user_msghdr __user *msg, unsigned int flags,
2684 bool forbid_cmsg_compat)
2686 struct msghdr msg_sys;
2687 struct socket *sock;
2689 if (forbid_cmsg_compat && (flags & MSG_CMSG_COMPAT))
2696 sock = sock_from_file(fd_file(f));
2697 if (unlikely(!sock))
2700 return ___sys_sendmsg(sock, msg, &msg_sys, flags, NULL, 0);
2703 SYSCALL_DEFINE3(sendmsg, int, fd, struct user_msghdr __user *, msg, unsigned int, flags)
2705 return __sys_sendmsg(fd, msg, flags, true);
2709 * Linux sendmmsg interface
2712 int __sys_sendmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
2713 unsigned int flags, bool forbid_cmsg_compat)
2716 struct socket *sock;
2717 struct mmsghdr __user *entry;
2718 struct compat_mmsghdr __user *compat_entry;
2719 struct msghdr msg_sys;
2720 struct used_address used_address;
2721 unsigned int oflags = flags;
2723 if (forbid_cmsg_compat && (flags & MSG_CMSG_COMPAT))
2726 if (vlen > UIO_MAXIOV)
2735 sock = sock_from_file(fd_file(f));
2736 if (unlikely(!sock))
2739 used_address.name_len = UINT_MAX;
2741 compat_entry = (struct compat_mmsghdr __user *)mmsg;
2745 while (datagrams < vlen) {
2746 if (datagrams == vlen - 1)
2749 if (MSG_CMSG_COMPAT & flags) {
2750 err = ___sys_sendmsg(sock, (struct user_msghdr __user *)compat_entry,
2751 &msg_sys, flags, &used_address, MSG_EOR);
2754 err = __put_user(err, &compat_entry->msg_len);
2757 err = ___sys_sendmsg(sock,
2758 (struct user_msghdr __user *)entry,
2759 &msg_sys, flags, &used_address, MSG_EOR);
2762 err = put_user(err, &entry->msg_len);
2769 if (msg_data_left(&msg_sys))
2774 /* We only return an error if no datagrams were able to be sent */
2781 SYSCALL_DEFINE4(sendmmsg, int, fd, struct mmsghdr __user *, mmsg,
2782 unsigned int, vlen, unsigned int, flags)
2784 return __sys_sendmmsg(fd, mmsg, vlen, flags, true);
2787 static int recvmsg_copy_msghdr(struct msghdr *msg,
2788 struct user_msghdr __user *umsg, unsigned flags,
2789 struct sockaddr __user **uaddr,
2794 if (MSG_CMSG_COMPAT & flags) {
2795 struct compat_msghdr __user *msg_compat;
2797 msg_compat = (struct compat_msghdr __user *) umsg;
2798 err = get_compat_msghdr(msg, msg_compat, uaddr, iov);
2800 err = copy_msghdr_from_user(msg, umsg, uaddr, iov);
2808 static int ____sys_recvmsg(struct socket *sock, struct msghdr *msg_sys,
2809 struct user_msghdr __user *msg,
2810 struct sockaddr __user *uaddr,
2811 unsigned int flags, int nosec)
2813 struct compat_msghdr __user *msg_compat =
2814 (struct compat_msghdr __user *) msg;
2815 int __user *uaddr_len = COMPAT_NAMELEN(msg);
2816 struct sockaddr_storage addr;
2817 unsigned long cmsg_ptr;
2821 msg_sys->msg_name = &addr;
2822 cmsg_ptr = (unsigned long)msg_sys->msg_control;
2823 msg_sys->msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT);
2825 /* We assume all kernel code knows the size of sockaddr_storage */
2826 msg_sys->msg_namelen = 0;
2828 if (sock->file->f_flags & O_NONBLOCK)
2829 flags |= MSG_DONTWAIT;
2831 if (unlikely(nosec))
2832 err = sock_recvmsg_nosec(sock, msg_sys, flags);
2834 err = sock_recvmsg(sock, msg_sys, flags);
2840 if (uaddr != NULL) {
2841 err = move_addr_to_user(&addr,
2842 msg_sys->msg_namelen, uaddr,
2847 err = __put_user((msg_sys->msg_flags & ~MSG_CMSG_COMPAT),
2851 if (MSG_CMSG_COMPAT & flags)
2852 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
2853 &msg_compat->msg_controllen);
2855 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
2856 &msg->msg_controllen);
2864 static int ___sys_recvmsg(struct socket *sock, struct user_msghdr __user *msg,
2865 struct msghdr *msg_sys, unsigned int flags, int nosec)
2867 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
2868 /* user mode address pointers */
2869 struct sockaddr __user *uaddr;
2872 err = recvmsg_copy_msghdr(msg_sys, msg, flags, &uaddr, &iov);
2876 err = ____sys_recvmsg(sock, msg_sys, msg, uaddr, flags, nosec);
2882 * BSD recvmsg interface
2885 long __sys_recvmsg_sock(struct socket *sock, struct msghdr *msg,
2886 struct user_msghdr __user *umsg,
2887 struct sockaddr __user *uaddr, unsigned int flags)
2889 return ____sys_recvmsg(sock, msg, umsg, uaddr, flags, 0);
2892 long __sys_recvmsg(int fd, struct user_msghdr __user *msg, unsigned int flags,
2893 bool forbid_cmsg_compat)
2895 struct msghdr msg_sys;
2896 struct socket *sock;
2898 if (forbid_cmsg_compat && (flags & MSG_CMSG_COMPAT))
2905 sock = sock_from_file(fd_file(f));
2906 if (unlikely(!sock))
2909 return ___sys_recvmsg(sock, msg, &msg_sys, flags, 0);
2912 SYSCALL_DEFINE3(recvmsg, int, fd, struct user_msghdr __user *, msg,
2913 unsigned int, flags)
2915 return __sys_recvmsg(fd, msg, flags, true);
2919 * Linux recvmmsg interface
2922 static int do_recvmmsg(int fd, struct mmsghdr __user *mmsg,
2923 unsigned int vlen, unsigned int flags,
2924 struct timespec64 *timeout)
2926 int err = 0, datagrams;
2927 struct socket *sock;
2928 struct mmsghdr __user *entry;
2929 struct compat_mmsghdr __user *compat_entry;
2930 struct msghdr msg_sys;
2931 struct timespec64 end_time;
2932 struct timespec64 timeout64;
2935 poll_select_set_timeout(&end_time, timeout->tv_sec,
2945 sock = sock_from_file(fd_file(f));
2946 if (unlikely(!sock))
2949 if (likely(!(flags & MSG_ERRQUEUE))) {
2950 err = sock_error(sock->sk);
2956 compat_entry = (struct compat_mmsghdr __user *)mmsg;
2958 while (datagrams < vlen) {
2960 * No need to ask LSM for more than the first datagram.
2962 if (MSG_CMSG_COMPAT & flags) {
2963 err = ___sys_recvmsg(sock, (struct user_msghdr __user *)compat_entry,
2964 &msg_sys, flags & ~MSG_WAITFORONE,
2968 err = __put_user(err, &compat_entry->msg_len);
2971 err = ___sys_recvmsg(sock,
2972 (struct user_msghdr __user *)entry,
2973 &msg_sys, flags & ~MSG_WAITFORONE,
2977 err = put_user(err, &entry->msg_len);
2985 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
2986 if (flags & MSG_WAITFORONE)
2987 flags |= MSG_DONTWAIT;
2990 ktime_get_ts64(&timeout64);
2991 *timeout = timespec64_sub(end_time, timeout64);
2992 if (timeout->tv_sec < 0) {
2993 timeout->tv_sec = timeout->tv_nsec = 0;
2997 /* Timeout, return less than vlen datagrams */
2998 if (timeout->tv_nsec == 0 && timeout->tv_sec == 0)
3002 /* Out of band data, return right away */
3003 if (msg_sys.msg_flags & MSG_OOB)
3015 * We may return less entries than requested (vlen) if the
3016 * sock is non block and there aren't enough datagrams...
3018 if (err != -EAGAIN) {
3020 * ... or if recvmsg returns an error after we
3021 * received some datagrams, where we record the
3022 * error to return on the next call or if the
3023 * app asks about it using getsockopt(SO_ERROR).
3025 WRITE_ONCE(sock->sk->sk_err, -err);
3030 int __sys_recvmmsg(int fd, struct mmsghdr __user *mmsg,
3031 unsigned int vlen, unsigned int flags,
3032 struct __kernel_timespec __user *timeout,
3033 struct old_timespec32 __user *timeout32)
3036 struct timespec64 timeout_sys;
3038 if (timeout && get_timespec64(&timeout_sys, timeout))
3041 if (timeout32 && get_old_timespec32(&timeout_sys, timeout32))
3044 if (!timeout && !timeout32)
3045 return do_recvmmsg(fd, mmsg, vlen, flags, NULL);
3047 datagrams = do_recvmmsg(fd, mmsg, vlen, flags, &timeout_sys);
3052 if (timeout && put_timespec64(&timeout_sys, timeout))
3053 datagrams = -EFAULT;
3055 if (timeout32 && put_old_timespec32(&timeout_sys, timeout32))
3056 datagrams = -EFAULT;
3061 SYSCALL_DEFINE5(recvmmsg, int, fd, struct mmsghdr __user *, mmsg,
3062 unsigned int, vlen, unsigned int, flags,
3063 struct __kernel_timespec __user *, timeout)
3065 if (flags & MSG_CMSG_COMPAT)
3068 return __sys_recvmmsg(fd, mmsg, vlen, flags, timeout, NULL);
3071 #ifdef CONFIG_COMPAT_32BIT_TIME
3072 SYSCALL_DEFINE5(recvmmsg_time32, int, fd, struct mmsghdr __user *, mmsg,
3073 unsigned int, vlen, unsigned int, flags,
3074 struct old_timespec32 __user *, timeout)
3076 if (flags & MSG_CMSG_COMPAT)
3079 return __sys_recvmmsg(fd, mmsg, vlen, flags, NULL, timeout);
3083 #ifdef __ARCH_WANT_SYS_SOCKETCALL
3084 /* Argument list sizes for sys_socketcall */
3085 #define AL(x) ((x) * sizeof(unsigned long))
3086 static const unsigned char nargs[21] = {
3087 AL(0), AL(3), AL(3), AL(3), AL(2), AL(3),
3088 AL(3), AL(3), AL(4), AL(4), AL(4), AL(6),
3089 AL(6), AL(2), AL(5), AL(5), AL(3), AL(3),
3096 * System call vectors.
3098 * Argument checking cleaned up. Saved 20% in size.
3099 * This function doesn't need to set the kernel lock because
3100 * it is set by the callees.
3103 SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args)
3105 unsigned long a[AUDITSC_ARGS];
3106 unsigned long a0, a1;
3110 if (call < 1 || call > SYS_SENDMMSG)
3112 call = array_index_nospec(call, SYS_SENDMMSG + 1);
3115 if (len > sizeof(a))
3118 /* copy_from_user should be SMP safe. */
3119 if (copy_from_user(a, args, len))
3122 err = audit_socketcall(nargs[call] / sizeof(unsigned long), a);
3131 err = __sys_socket(a0, a1, a[2]);
3134 err = __sys_bind(a0, (struct sockaddr __user *)a1, a[2]);
3137 err = __sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
3140 err = __sys_listen(a0, a1);
3143 err = __sys_accept4(a0, (struct sockaddr __user *)a1,
3144 (int __user *)a[2], 0);
3146 case SYS_GETSOCKNAME:
3148 __sys_getsockname(a0, (struct sockaddr __user *)a1,
3149 (int __user *)a[2]);
3151 case SYS_GETPEERNAME:
3153 __sys_getpeername(a0, (struct sockaddr __user *)a1,
3154 (int __user *)a[2]);
3156 case SYS_SOCKETPAIR:
3157 err = __sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
3160 err = __sys_sendto(a0, (void __user *)a1, a[2], a[3],
3164 err = __sys_sendto(a0, (void __user *)a1, a[2], a[3],
3165 (struct sockaddr __user *)a[4], a[5]);
3168 err = __sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
3172 err = __sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
3173 (struct sockaddr __user *)a[4],
3174 (int __user *)a[5]);
3177 err = __sys_shutdown(a0, a1);
3179 case SYS_SETSOCKOPT:
3180 err = __sys_setsockopt(a0, a1, a[2], (char __user *)a[3],
3183 case SYS_GETSOCKOPT:
3185 __sys_getsockopt(a0, a1, a[2], (char __user *)a[3],
3186 (int __user *)a[4]);
3189 err = __sys_sendmsg(a0, (struct user_msghdr __user *)a1,
3193 err = __sys_sendmmsg(a0, (struct mmsghdr __user *)a1, a[2],
3197 err = __sys_recvmsg(a0, (struct user_msghdr __user *)a1,
3201 if (IS_ENABLED(CONFIG_64BIT))
3202 err = __sys_recvmmsg(a0, (struct mmsghdr __user *)a1,
3204 (struct __kernel_timespec __user *)a[4],
3207 err = __sys_recvmmsg(a0, (struct mmsghdr __user *)a1,
3209 (struct old_timespec32 __user *)a[4]);
3212 err = __sys_accept4(a0, (struct sockaddr __user *)a1,
3213 (int __user *)a[2], a[3]);
3222 #endif /* __ARCH_WANT_SYS_SOCKETCALL */
3225 * sock_register - add a socket protocol handler
3226 * @ops: description of protocol
3228 * This function is called by a protocol handler that wants to
3229 * advertise its address family, and have it linked into the
3230 * socket interface. The value ops->family corresponds to the
3231 * socket system call protocol family.
3233 int sock_register(const struct net_proto_family *ops)
3237 if (ops->family >= NPROTO) {
3238 pr_crit("protocol %d >= NPROTO(%d)\n", ops->family, NPROTO);
3242 spin_lock(&net_family_lock);
3243 if (rcu_dereference_protected(net_families[ops->family],
3244 lockdep_is_held(&net_family_lock)))
3247 rcu_assign_pointer(net_families[ops->family], ops);
3250 spin_unlock(&net_family_lock);
3252 pr_info("NET: Registered %s protocol family\n", pf_family_names[ops->family]);
3255 EXPORT_SYMBOL(sock_register);
3258 * sock_unregister - remove a protocol handler
3259 * @family: protocol family to remove
3261 * This function is called by a protocol handler that wants to
3262 * remove its address family, and have it unlinked from the
3263 * new socket creation.
3265 * If protocol handler is a module, then it can use module reference
3266 * counts to protect against new references. If protocol handler is not
3267 * a module then it needs to provide its own protection in
3268 * the ops->create routine.
3270 void sock_unregister(int family)
3272 BUG_ON(family < 0 || family >= NPROTO);
3274 spin_lock(&net_family_lock);
3275 RCU_INIT_POINTER(net_families[family], NULL);
3276 spin_unlock(&net_family_lock);
3280 pr_info("NET: Unregistered %s protocol family\n", pf_family_names[family]);
3282 EXPORT_SYMBOL(sock_unregister);
3284 bool sock_is_registered(int family)
3286 return family < NPROTO && rcu_access_pointer(net_families[family]);
3289 static int __init sock_init(void)
3293 * Initialize the network sysctl infrastructure.
3295 err = net_sysctl_init();
3300 * Initialize skbuff SLAB cache
3305 * Initialize the protocols module.
3310 err = register_filesystem(&sock_fs_type);
3313 sock_mnt = kern_mount(&sock_fs_type);
3314 if (IS_ERR(sock_mnt)) {
3315 err = PTR_ERR(sock_mnt);
3319 /* The real protocol initialization is performed in later initcalls.
3322 #ifdef CONFIG_NETFILTER
3323 err = netfilter_init();
3328 ptp_classifier_init();
3334 unregister_filesystem(&sock_fs_type);
3338 core_initcall(sock_init); /* early initcall */
3340 #ifdef CONFIG_PROC_FS
3341 void socket_seq_show(struct seq_file *seq)
3343 seq_printf(seq, "sockets: used %d\n",
3344 sock_inuse_get(seq->private));
3346 #endif /* CONFIG_PROC_FS */
3348 /* Handle the fact that while struct ifreq has the same *layout* on
3349 * 32/64 for everything but ifreq::ifru_ifmap and ifreq::ifru_data,
3350 * which are handled elsewhere, it still has different *size* due to
3351 * ifreq::ifru_ifmap (which is 16 bytes on 32 bit, 24 bytes on 64-bit,
3352 * resulting in struct ifreq being 32 and 40 bytes respectively).
3353 * As a result, if the struct happens to be at the end of a page and
3354 * the next page isn't readable/writable, we get a fault. To prevent
3355 * that, copy back and forth to the full size.
3357 int get_user_ifreq(struct ifreq *ifr, void __user **ifrdata, void __user *arg)
3359 if (in_compat_syscall()) {
3360 struct compat_ifreq *ifr32 = (struct compat_ifreq *)ifr;
3362 memset(ifr, 0, sizeof(*ifr));
3363 if (copy_from_user(ifr32, arg, sizeof(*ifr32)))
3367 *ifrdata = compat_ptr(ifr32->ifr_data);
3372 if (copy_from_user(ifr, arg, sizeof(*ifr)))
3376 *ifrdata = ifr->ifr_data;
3380 EXPORT_SYMBOL(get_user_ifreq);
3382 int put_user_ifreq(struct ifreq *ifr, void __user *arg)
3384 size_t size = sizeof(*ifr);
3386 if (in_compat_syscall())
3387 size = sizeof(struct compat_ifreq);
3389 if (copy_to_user(arg, ifr, size))
3394 EXPORT_SYMBOL(put_user_ifreq);
3396 #ifdef CONFIG_COMPAT
3397 static int compat_siocwandev(struct net *net, struct compat_ifreq __user *uifr32)
3399 compat_uptr_t uptr32;
3404 if (get_user_ifreq(&ifr, NULL, uifr32))
3407 if (get_user(uptr32, &uifr32->ifr_settings.ifs_ifsu))
3410 saved = ifr.ifr_settings.ifs_ifsu.raw_hdlc;
3411 ifr.ifr_settings.ifs_ifsu.raw_hdlc = compat_ptr(uptr32);
3413 err = dev_ioctl(net, SIOCWANDEV, &ifr, NULL, NULL);
3415 ifr.ifr_settings.ifs_ifsu.raw_hdlc = saved;
3416 if (put_user_ifreq(&ifr, uifr32))
3422 /* Handle ioctls that use ifreq::ifr_data and just need struct ifreq converted */
3423 static int compat_ifr_data_ioctl(struct net *net, unsigned int cmd,
3424 struct compat_ifreq __user *u_ifreq32)
3429 if (!is_socket_ioctl_cmd(cmd))
3431 if (get_user_ifreq(&ifreq, &data, u_ifreq32))
3433 ifreq.ifr_data = data;
3435 return dev_ioctl(net, cmd, &ifreq, data, NULL);
3438 static int compat_sock_ioctl_trans(struct file *file, struct socket *sock,
3439 unsigned int cmd, unsigned long arg)
3441 void __user *argp = compat_ptr(arg);
3442 struct sock *sk = sock->sk;
3443 struct net *net = sock_net(sk);
3444 const struct proto_ops *ops;
3446 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15))
3447 return sock_ioctl(file, cmd, (unsigned long)argp);
3451 return compat_siocwandev(net, argp);
3452 case SIOCGSTAMP_OLD:
3453 case SIOCGSTAMPNS_OLD:
3454 ops = READ_ONCE(sock->ops);
3455 if (!ops->gettstamp)
3456 return -ENOIOCTLCMD;
3457 return ops->gettstamp(sock, argp, cmd == SIOCGSTAMP_OLD,
3458 !COMPAT_USE_64BIT_TIME);
3461 case SIOCBONDSLAVEINFOQUERY:
3462 case SIOCBONDINFOQUERY:
3465 return compat_ifr_data_ioctl(net, cmd, argp);
3478 case SIOCGSTAMP_NEW:
3479 case SIOCGSTAMPNS_NEW:
3483 return sock_ioctl(file, cmd, arg);
3502 case SIOCSIFHWBROADCAST:
3504 case SIOCGIFBRDADDR:
3505 case SIOCSIFBRDADDR:
3506 case SIOCGIFDSTADDR:
3507 case SIOCSIFDSTADDR:
3508 case SIOCGIFNETMASK:
3509 case SIOCSIFNETMASK:
3519 case SIOCBONDENSLAVE:
3520 case SIOCBONDRELEASE:
3521 case SIOCBONDSETHWADDR:
3522 case SIOCBONDCHANGEACTIVE:
3529 return sock_do_ioctl(net, sock, cmd, arg);
3532 return -ENOIOCTLCMD;
3535 static long compat_sock_ioctl(struct file *file, unsigned int cmd,
3538 struct socket *sock = file->private_data;
3539 const struct proto_ops *ops = READ_ONCE(sock->ops);
3540 int ret = -ENOIOCTLCMD;
3547 if (ops->compat_ioctl)
3548 ret = ops->compat_ioctl(sock, cmd, arg);
3550 if (ret == -ENOIOCTLCMD &&
3551 (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST))
3552 ret = compat_wext_handle_ioctl(net, cmd, arg);
3554 if (ret == -ENOIOCTLCMD)
3555 ret = compat_sock_ioctl_trans(file, sock, cmd, arg);
3562 * kernel_bind - bind an address to a socket (kernel space)
3565 * @addrlen: length of address
3567 * Returns 0 or an error.
3570 int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen)
3572 struct sockaddr_storage address;
3574 memcpy(&address, addr, addrlen);
3576 return READ_ONCE(sock->ops)->bind(sock, (struct sockaddr *)&address,
3579 EXPORT_SYMBOL(kernel_bind);
3582 * kernel_listen - move socket to listening state (kernel space)
3584 * @backlog: pending connections queue size
3586 * Returns 0 or an error.
3589 int kernel_listen(struct socket *sock, int backlog)
3591 return READ_ONCE(sock->ops)->listen(sock, backlog);
3593 EXPORT_SYMBOL(kernel_listen);
3596 * kernel_accept - accept a connection (kernel space)
3597 * @sock: listening socket
3598 * @newsock: new connected socket
3601 * @flags must be SOCK_CLOEXEC, SOCK_NONBLOCK or 0.
3602 * If it fails, @newsock is guaranteed to be %NULL.
3603 * Returns 0 or an error.
3606 int kernel_accept(struct socket *sock, struct socket **newsock, int flags)
3608 struct sock *sk = sock->sk;
3609 const struct proto_ops *ops = READ_ONCE(sock->ops);
3610 struct proto_accept_arg arg = {
3616 err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
3621 err = ops->accept(sock, *newsock, &arg);
3623 sock_release(*newsock);
3628 (*newsock)->ops = ops;
3629 __module_get(ops->owner);
3634 EXPORT_SYMBOL(kernel_accept);
3637 * kernel_connect - connect a socket (kernel space)
3640 * @addrlen: address length
3641 * @flags: flags (O_NONBLOCK, ...)
3643 * For datagram sockets, @addr is the address to which datagrams are sent
3644 * by default, and the only address from which datagrams are received.
3645 * For stream sockets, attempts to connect to @addr.
3646 * Returns 0 or an error code.
3649 int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
3652 struct sockaddr_storage address;
3654 memcpy(&address, addr, addrlen);
3656 return READ_ONCE(sock->ops)->connect(sock, (struct sockaddr *)&address,
3659 EXPORT_SYMBOL(kernel_connect);
3662 * kernel_getsockname - get the address which the socket is bound (kernel space)
3664 * @addr: address holder
3666 * Fills the @addr pointer with the address which the socket is bound.
3667 * Returns the length of the address in bytes or an error code.
3670 int kernel_getsockname(struct socket *sock, struct sockaddr *addr)
3672 return READ_ONCE(sock->ops)->getname(sock, addr, 0);
3674 EXPORT_SYMBOL(kernel_getsockname);
3677 * kernel_getpeername - get the address which the socket is connected (kernel space)
3679 * @addr: address holder
3681 * Fills the @addr pointer with the address which the socket is connected.
3682 * Returns the length of the address in bytes or an error code.
3685 int kernel_getpeername(struct socket *sock, struct sockaddr *addr)
3687 return READ_ONCE(sock->ops)->getname(sock, addr, 1);
3689 EXPORT_SYMBOL(kernel_getpeername);
3692 * kernel_sock_shutdown - shut down part of a full-duplex connection (kernel space)
3694 * @how: connection part
3696 * Returns 0 or an error.
3699 int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how)
3701 return READ_ONCE(sock->ops)->shutdown(sock, how);
3703 EXPORT_SYMBOL(kernel_sock_shutdown);
3706 * kernel_sock_ip_overhead - returns the IP overhead imposed by a socket
3709 * This routine returns the IP overhead imposed by a socket i.e.
3710 * the length of the underlying IP header, depending on whether
3711 * this is an IPv4 or IPv6 socket and the length from IP options turned
3712 * on at the socket. Assumes that the caller has a lock on the socket.
3715 u32 kernel_sock_ip_overhead(struct sock *sk)
3717 struct inet_sock *inet;
3718 struct ip_options_rcu *opt;
3720 #if IS_ENABLED(CONFIG_IPV6)
3721 struct ipv6_pinfo *np;
3722 struct ipv6_txoptions *optv6 = NULL;
3723 #endif /* IS_ENABLED(CONFIG_IPV6) */
3728 switch (sk->sk_family) {
3731 overhead += sizeof(struct iphdr);
3732 opt = rcu_dereference_protected(inet->inet_opt,
3733 sock_owned_by_user(sk));
3735 overhead += opt->opt.optlen;
3737 #if IS_ENABLED(CONFIG_IPV6)
3740 overhead += sizeof(struct ipv6hdr);
3742 optv6 = rcu_dereference_protected(np->opt,
3743 sock_owned_by_user(sk));
3745 overhead += (optv6->opt_flen + optv6->opt_nflen);
3747 #endif /* IS_ENABLED(CONFIG_IPV6) */
3748 default: /* Returns 0 overhead if the socket is not ipv4 or ipv6 */
3752 EXPORT_SYMBOL(kernel_sock_ip_overhead);