Merge tag 'rproc-v6.14' of git://git.kernel.org/pub/scm/linux/kernel/git/remoteproc...
[linux.git] / net / socket.c
blob262a28b59c7f0f760fd29e207f270e65150abec8
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
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>
8 * Ross Biro
9 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
11 * Fixes:
12 * Anonymous : NOTSOCK/BADF cleanup. Error fix in
13 * shutdown()
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
18 * top level.
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
23 * tty drivers).
24 * Niibe Yutaka : Asynchronous I/O for writes (4.4BSD style)
25 * Jeff Uphoff : Made max number of sockets command-line
26 * configurable.
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
35 * stuff.
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
41 * moment.
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
50 * paradigm.
52 * Based upon Swansea University Computer Society NET3.039
55 #include <linux/bpf-cgroup.h>
56 #include <linux/ethtool.h>
57 #include <linux/mm.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>
97 #include <net/wext.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;
118 #endif
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);
128 #ifdef CONFIG_COMPAT
129 static long compat_sock_ioctl(struct file *file,
130 unsigned int cmd, unsigned long arg);
131 #endif
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,
135 unsigned int flags);
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);
147 #else
148 #define sock_show_fdinfo NULL
149 #endif
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,
160 .poll = sock_poll,
161 .unlocked_ioctl = sock_ioctl,
162 #ifdef CONFIG_COMPAT
163 .compat_ioctl = compat_sock_ioctl,
164 #endif
165 .uring_cmd = io_uring_cmd_sock,
166 .mmap = sock_mmap,
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",
180 [PF_IPX] = "PF_IPX",
181 [PF_APPLETALK] = "PF_APPLETALK",
182 [PF_NETROM] = "PF_NETROM",
183 [PF_BRIDGE] = "PF_BRIDGE",
184 [PF_ATMPVC] = "PF_ATMPVC",
185 [PF_X25] = "PF_X25",
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",
191 [PF_KEY] = "PF_KEY",
192 [PF_NETLINK] = "PF_NETLINK/PF_ROUTE",
193 [PF_PACKET] = "PF_PACKET",
194 [PF_ASH] = "PF_ASH",
195 [PF_ECONET] = "PF_ECONET",
196 [PF_ATMSVC] = "PF_ATMSVC",
197 [PF_RDS] = "PF_RDS",
198 [PF_SNA] = "PF_SNA",
199 [PF_IRDA] = "PF_IRDA",
200 [PF_PPPOX] = "PF_PPPOX",
201 [PF_WANPIPE] = "PF_WANPIPE",
202 [PF_LLC] = "PF_LLC",
203 [PF_IB] = "PF_IB",
204 [PF_MPLS] = "PF_MPLS",
205 [PF_CAN] = "PF_CAN",
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",
214 [PF_ALG] = "PF_ALG",
215 [PF_NFC] = "PF_NFC",
216 [PF_VSOCK] = "PF_VSOCK",
217 [PF_KCM] = "PF_KCM",
218 [PF_QIPCRTR] = "PF_QIPCRTR",
219 [PF_SMC] = "PF_SMC",
220 [PF_XDP] = "PF_XDP",
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;
232 * Support routines.
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))
251 return -EINVAL;
252 if (ulen == 0)
253 return 0;
254 if (copy_from_user(kaddr, uaddr, ulen))
255 return -EFAULT;
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
270 * accessible.
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)
279 int err;
280 int len;
282 BUG_ON(klen > sizeof(struct sockaddr_storage));
283 err = get_user(len, ulen);
284 if (err)
285 return err;
286 if (len > klen)
287 len = klen;
288 if (len < 0)
289 return -EINVAL;
290 if (len) {
291 if (audit_sockaddr(klen, kaddr))
292 return -ENOMEM;
293 if (copy_to_user(uaddr, kaddr, len))
294 return -EFAULT;
297 * "fromlen shall refer to the value before truncation.."
298 * 1003.1g
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);
310 if (!ei)
311 return NULL;
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 |
347 SLAB_ACCOUNT),
348 init_once);
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)
375 if (value) {
376 if (dentry->d_name.len + 1 > size)
377 return -ERANGE;
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. */
399 return -EAGAIN;
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,
410 NULL
413 static int sockfs_init_fs_context(struct fs_context *fc)
415 struct pseudo_fs_context *ctx = init_pseudo(fc, SOCKFS_MAGIC);
416 if (!ctx)
417 return -ENOMEM;
418 ctx->ops = &sockfs_ops;
419 ctx->dops = &sockfs_dentry_operations;
420 ctx->xattr = sockfs_xattr_handlers;
421 return 0;
424 static struct vfsmount *sock_mnt __read_mostly;
426 static struct file_system_type sock_fs_type = {
427 .name = "sockfs",
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
451 * @sock: socket
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)
465 struct file *file;
467 if (!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),
472 &socket_file_ops);
473 if (IS_ERR(file)) {
474 sock_release(sock);
475 return file;
478 file->f_mode |= FMODE_NOWAIT;
479 sock->file = file;
480 file->private_data = sock;
481 stream_open(SOCK_INODE(sock), file);
482 return file;
484 EXPORT_SYMBOL(sock_alloc_file);
486 static int sock_map_fd(struct socket *sock, int flags)
488 struct file *newfile;
489 int fd = get_unused_fd_flags(flags);
490 if (unlikely(fd < 0)) {
491 sock_release(sock);
492 return fd;
495 newfile = sock_alloc_file(sock, flags, NULL);
496 if (!IS_ERR(newfile)) {
497 fd_install(fd, newfile);
498 return fd;
501 put_unused_fd(fd);
502 return PTR_ERR(newfile);
506 * sock_from_file - Return the &socket bounded to @file.
507 * @file: file
509 * On failure returns %NULL.
512 struct socket *sock_from_file(struct file *file)
514 if (likely(file->f_op == &socket_file_ops))
515 return file->private_data; /* set in sock_alloc_file */
517 return NULL;
519 EXPORT_SYMBOL(sock_from_file);
522 * sockfd_lookup - Go from a file number to its socket slot
523 * @fd: file handle
524 * @err: pointer to an error code return
526 * The file handle passed in is locked and the socket it is bound
527 * to is returned. If an error occurs the err pointer is overwritten
528 * with a negative errno code and NULL is returned. The function checks
529 * for both invalid handles and passing a handle which is not a socket.
531 * On a success the socket object pointer is returned.
534 struct socket *sockfd_lookup(int fd, int *err)
536 struct file *file;
537 struct socket *sock;
539 file = fget(fd);
540 if (!file) {
541 *err = -EBADF;
542 return NULL;
545 sock = sock_from_file(file);
546 if (!sock) {
547 *err = -ENOTSOCK;
548 fput(file);
550 return sock;
552 EXPORT_SYMBOL(sockfd_lookup);
554 static ssize_t sockfs_listxattr(struct dentry *dentry, char *buffer,
555 size_t size)
557 ssize_t len;
558 ssize_t used = 0;
560 len = security_inode_listsecurity(d_inode(dentry), buffer, size);
561 if (len < 0)
562 return len;
563 used += len;
564 if (buffer) {
565 if (size < used)
566 return -ERANGE;
567 buffer += len;
570 len = (XATTR_NAME_SOCKPROTONAME_LEN + 1);
571 used += len;
572 if (buffer) {
573 if (size < used)
574 return -ERANGE;
575 memcpy(buffer, XATTR_NAME_SOCKPROTONAME, len);
576 buffer += len;
579 return used;
582 static int sockfs_setattr(struct mnt_idmap *idmap,
583 struct dentry *dentry, struct iattr *iattr)
585 int err = simple_setattr(&nop_mnt_idmap, dentry, iattr);
587 if (!err && (iattr->ia_valid & ATTR_UID)) {
588 struct socket *sock = SOCKET_I(d_inode(dentry));
590 if (sock->sk)
591 sock->sk->sk_uid = iattr->ia_uid;
592 else
593 err = -ENOENT;
596 return err;
599 static const struct inode_operations sockfs_inode_ops = {
600 .listxattr = sockfs_listxattr,
601 .setattr = sockfs_setattr,
605 * sock_alloc - allocate a socket
607 * Allocate a new inode and socket object. The two are bound together
608 * and initialised. The socket is then returned. If we are out of inodes
609 * NULL is returned. This functions uses GFP_KERNEL internally.
612 struct socket *sock_alloc(void)
614 struct inode *inode;
615 struct socket *sock;
617 inode = new_inode_pseudo(sock_mnt->mnt_sb);
618 if (!inode)
619 return NULL;
621 sock = SOCKET_I(inode);
623 inode->i_ino = get_next_ino();
624 inode->i_mode = S_IFSOCK | S_IRWXUGO;
625 inode->i_uid = current_fsuid();
626 inode->i_gid = current_fsgid();
627 inode->i_op = &sockfs_inode_ops;
629 return sock;
631 EXPORT_SYMBOL(sock_alloc);
633 static void __sock_release(struct socket *sock, struct inode *inode)
635 const struct proto_ops *ops = READ_ONCE(sock->ops);
637 if (ops) {
638 struct module *owner = ops->owner;
640 if (inode)
641 inode_lock(inode);
642 ops->release(sock);
643 sock->sk = NULL;
644 if (inode)
645 inode_unlock(inode);
646 sock->ops = NULL;
647 module_put(owner);
650 if (sock->wq.fasync_list)
651 pr_err("%s: fasync list not empty!\n", __func__);
653 if (!sock->file) {
654 iput(SOCK_INODE(sock));
655 return;
657 sock->file = NULL;
661 * sock_release - close a socket
662 * @sock: socket to close
664 * The socket is released from the protocol stack if it has a release
665 * callback, and the inode is then released if the socket is bound to
666 * an inode not a file.
668 void sock_release(struct socket *sock)
670 __sock_release(sock, NULL);
672 EXPORT_SYMBOL(sock_release);
674 void __sock_tx_timestamp(__u32 tsflags, __u8 *tx_flags)
676 u8 flags = *tx_flags;
678 if (tsflags & SOF_TIMESTAMPING_TX_HARDWARE) {
679 flags |= SKBTX_HW_TSTAMP;
681 /* PTP hardware clocks can provide a free running cycle counter
682 * as a time base for virtual clocks. Tell driver to use the
683 * free running cycle counter for timestamp if socket is bound
684 * to virtual clock.
686 if (tsflags & SOF_TIMESTAMPING_BIND_PHC)
687 flags |= SKBTX_HW_TSTAMP_USE_CYCLES;
690 if (tsflags & SOF_TIMESTAMPING_TX_SOFTWARE)
691 flags |= SKBTX_SW_TSTAMP;
693 if (tsflags & SOF_TIMESTAMPING_TX_SCHED)
694 flags |= SKBTX_SCHED_TSTAMP;
696 *tx_flags = flags;
698 EXPORT_SYMBOL(__sock_tx_timestamp);
700 INDIRECT_CALLABLE_DECLARE(int inet_sendmsg(struct socket *, struct msghdr *,
701 size_t));
702 INDIRECT_CALLABLE_DECLARE(int inet6_sendmsg(struct socket *, struct msghdr *,
703 size_t));
705 static noinline void call_trace_sock_send_length(struct sock *sk, int ret,
706 int flags)
708 trace_sock_send_length(sk, ret, 0);
711 static inline int sock_sendmsg_nosec(struct socket *sock, struct msghdr *msg)
713 int ret = INDIRECT_CALL_INET(READ_ONCE(sock->ops)->sendmsg, inet6_sendmsg,
714 inet_sendmsg, sock, msg,
715 msg_data_left(msg));
716 BUG_ON(ret == -EIOCBQUEUED);
718 if (trace_sock_send_length_enabled())
719 call_trace_sock_send_length(sock->sk, ret, 0);
720 return ret;
723 static int __sock_sendmsg(struct socket *sock, struct msghdr *msg)
725 int err = security_socket_sendmsg(sock, msg,
726 msg_data_left(msg));
728 return err ?: sock_sendmsg_nosec(sock, msg);
732 * sock_sendmsg - send a message through @sock
733 * @sock: socket
734 * @msg: message to send
736 * Sends @msg through @sock, passing through LSM.
737 * Returns the number of bytes sent, or an error code.
739 int sock_sendmsg(struct socket *sock, struct msghdr *msg)
741 struct sockaddr_storage *save_addr = (struct sockaddr_storage *)msg->msg_name;
742 struct sockaddr_storage address;
743 int save_len = msg->msg_namelen;
744 int ret;
746 if (msg->msg_name) {
747 memcpy(&address, msg->msg_name, msg->msg_namelen);
748 msg->msg_name = &address;
751 ret = __sock_sendmsg(sock, msg);
752 msg->msg_name = save_addr;
753 msg->msg_namelen = save_len;
755 return ret;
757 EXPORT_SYMBOL(sock_sendmsg);
760 * kernel_sendmsg - send a message through @sock (kernel-space)
761 * @sock: socket
762 * @msg: message header
763 * @vec: kernel vec
764 * @num: vec array length
765 * @size: total message data size
767 * Builds the message data with @vec and sends it through @sock.
768 * Returns the number of bytes sent, or an error code.
771 int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
772 struct kvec *vec, size_t num, size_t size)
774 iov_iter_kvec(&msg->msg_iter, ITER_SOURCE, vec, num, size);
775 return sock_sendmsg(sock, msg);
777 EXPORT_SYMBOL(kernel_sendmsg);
779 static bool skb_is_err_queue(const struct sk_buff *skb)
781 /* pkt_type of skbs enqueued on the error queue are set to
782 * PACKET_OUTGOING in skb_set_err_queue(). This is only safe to do
783 * in recvmsg, since skbs received on a local socket will never
784 * have a pkt_type of PACKET_OUTGOING.
786 return skb->pkt_type == PACKET_OUTGOING;
789 /* On transmit, software and hardware timestamps are returned independently.
790 * As the two skb clones share the hardware timestamp, which may be updated
791 * before the software timestamp is received, a hardware TX timestamp may be
792 * returned only if there is no software TX timestamp. Ignore false software
793 * timestamps, which may be made in the __sock_recv_timestamp() call when the
794 * option SO_TIMESTAMP_OLD(NS) is enabled on the socket, even when the skb has a
795 * hardware timestamp.
797 static bool skb_is_swtx_tstamp(const struct sk_buff *skb, int false_tstamp)
799 return skb->tstamp && !false_tstamp && skb_is_err_queue(skb);
802 static ktime_t get_timestamp(struct sock *sk, struct sk_buff *skb, int *if_index)
804 bool cycles = READ_ONCE(sk->sk_tsflags) & SOF_TIMESTAMPING_BIND_PHC;
805 struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
806 struct net_device *orig_dev;
807 ktime_t hwtstamp;
809 rcu_read_lock();
810 orig_dev = dev_get_by_napi_id(skb_napi_id(skb));
811 if (orig_dev) {
812 *if_index = orig_dev->ifindex;
813 hwtstamp = netdev_get_tstamp(orig_dev, shhwtstamps, cycles);
814 } else {
815 hwtstamp = shhwtstamps->hwtstamp;
817 rcu_read_unlock();
819 return hwtstamp;
822 static void put_ts_pktinfo(struct msghdr *msg, struct sk_buff *skb,
823 int if_index)
825 struct scm_ts_pktinfo ts_pktinfo;
826 struct net_device *orig_dev;
828 if (!skb_mac_header_was_set(skb))
829 return;
831 memset(&ts_pktinfo, 0, sizeof(ts_pktinfo));
833 if (!if_index) {
834 rcu_read_lock();
835 orig_dev = dev_get_by_napi_id(skb_napi_id(skb));
836 if (orig_dev)
837 if_index = orig_dev->ifindex;
838 rcu_read_unlock();
840 ts_pktinfo.if_index = if_index;
842 ts_pktinfo.pkt_length = skb->len - skb_mac_offset(skb);
843 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPING_PKTINFO,
844 sizeof(ts_pktinfo), &ts_pktinfo);
848 * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
850 void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
851 struct sk_buff *skb)
853 int need_software_tstamp = sock_flag(sk, SOCK_RCVTSTAMP);
854 int new_tstamp = sock_flag(sk, SOCK_TSTAMP_NEW);
855 struct scm_timestamping_internal tss;
856 int empty = 1, false_tstamp = 0;
857 struct skb_shared_hwtstamps *shhwtstamps =
858 skb_hwtstamps(skb);
859 int if_index;
860 ktime_t hwtstamp;
861 u32 tsflags;
863 /* Race occurred between timestamp enabling and packet
864 receiving. Fill in the current time for now. */
865 if (need_software_tstamp && skb->tstamp == 0) {
866 __net_timestamp(skb);
867 false_tstamp = 1;
870 if (need_software_tstamp) {
871 if (!sock_flag(sk, SOCK_RCVTSTAMPNS)) {
872 if (new_tstamp) {
873 struct __kernel_sock_timeval tv;
875 skb_get_new_timestamp(skb, &tv);
876 put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMP_NEW,
877 sizeof(tv), &tv);
878 } else {
879 struct __kernel_old_timeval tv;
881 skb_get_timestamp(skb, &tv);
882 put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMP_OLD,
883 sizeof(tv), &tv);
885 } else {
886 if (new_tstamp) {
887 struct __kernel_timespec ts;
889 skb_get_new_timestampns(skb, &ts);
890 put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMPNS_NEW,
891 sizeof(ts), &ts);
892 } else {
893 struct __kernel_old_timespec ts;
895 skb_get_timestampns(skb, &ts);
896 put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMPNS_OLD,
897 sizeof(ts), &ts);
902 memset(&tss, 0, sizeof(tss));
903 tsflags = READ_ONCE(sk->sk_tsflags);
904 if ((tsflags & SOF_TIMESTAMPING_SOFTWARE &&
905 (tsflags & SOF_TIMESTAMPING_RX_SOFTWARE ||
906 skb_is_err_queue(skb) ||
907 !(tsflags & SOF_TIMESTAMPING_OPT_RX_FILTER))) &&
908 ktime_to_timespec64_cond(skb->tstamp, tss.ts + 0))
909 empty = 0;
910 if (shhwtstamps &&
911 (tsflags & SOF_TIMESTAMPING_RAW_HARDWARE &&
912 (tsflags & SOF_TIMESTAMPING_RX_HARDWARE ||
913 skb_is_err_queue(skb) ||
914 !(tsflags & SOF_TIMESTAMPING_OPT_RX_FILTER))) &&
915 !skb_is_swtx_tstamp(skb, false_tstamp)) {
916 if_index = 0;
917 if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP_NETDEV)
918 hwtstamp = get_timestamp(sk, skb, &if_index);
919 else
920 hwtstamp = shhwtstamps->hwtstamp;
922 if (tsflags & SOF_TIMESTAMPING_BIND_PHC)
923 hwtstamp = ptp_convert_timestamp(&hwtstamp,
924 READ_ONCE(sk->sk_bind_phc));
926 if (ktime_to_timespec64_cond(hwtstamp, tss.ts + 2)) {
927 empty = 0;
929 if ((tsflags & SOF_TIMESTAMPING_OPT_PKTINFO) &&
930 !skb_is_err_queue(skb))
931 put_ts_pktinfo(msg, skb, if_index);
934 if (!empty) {
935 if (sock_flag(sk, SOCK_TSTAMP_NEW))
936 put_cmsg_scm_timestamping64(msg, &tss);
937 else
938 put_cmsg_scm_timestamping(msg, &tss);
940 if (skb_is_err_queue(skb) && skb->len &&
941 SKB_EXT_ERR(skb)->opt_stats)
942 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPING_OPT_STATS,
943 skb->len, skb->data);
946 EXPORT_SYMBOL_GPL(__sock_recv_timestamp);
948 #ifdef CONFIG_WIRELESS
949 void __sock_recv_wifi_status(struct msghdr *msg, struct sock *sk,
950 struct sk_buff *skb)
952 int ack;
954 if (!sock_flag(sk, SOCK_WIFI_STATUS))
955 return;
956 if (!skb->wifi_acked_valid)
957 return;
959 ack = skb->wifi_acked;
961 put_cmsg(msg, SOL_SOCKET, SCM_WIFI_STATUS, sizeof(ack), &ack);
963 EXPORT_SYMBOL_GPL(__sock_recv_wifi_status);
964 #endif
966 static inline void sock_recv_drops(struct msghdr *msg, struct sock *sk,
967 struct sk_buff *skb)
969 if (sock_flag(sk, SOCK_RXQ_OVFL) && skb && SOCK_SKB_CB(skb)->dropcount)
970 put_cmsg(msg, SOL_SOCKET, SO_RXQ_OVFL,
971 sizeof(__u32), &SOCK_SKB_CB(skb)->dropcount);
974 static void sock_recv_mark(struct msghdr *msg, struct sock *sk,
975 struct sk_buff *skb)
977 if (sock_flag(sk, SOCK_RCVMARK) && skb) {
978 /* We must use a bounce buffer for CONFIG_HARDENED_USERCOPY=y */
979 __u32 mark = skb->mark;
981 put_cmsg(msg, SOL_SOCKET, SO_MARK, sizeof(__u32), &mark);
985 static void sock_recv_priority(struct msghdr *msg, struct sock *sk,
986 struct sk_buff *skb)
988 if (sock_flag(sk, SOCK_RCVPRIORITY) && skb) {
989 __u32 priority = skb->priority;
991 put_cmsg(msg, SOL_SOCKET, SO_PRIORITY, sizeof(__u32), &priority);
995 void __sock_recv_cmsgs(struct msghdr *msg, struct sock *sk,
996 struct sk_buff *skb)
998 sock_recv_timestamp(msg, sk, skb);
999 sock_recv_drops(msg, sk, skb);
1000 sock_recv_mark(msg, sk, skb);
1001 sock_recv_priority(msg, sk, skb);
1003 EXPORT_SYMBOL_GPL(__sock_recv_cmsgs);
1005 INDIRECT_CALLABLE_DECLARE(int inet_recvmsg(struct socket *, struct msghdr *,
1006 size_t, int));
1007 INDIRECT_CALLABLE_DECLARE(int inet6_recvmsg(struct socket *, struct msghdr *,
1008 size_t, int));
1010 static noinline void call_trace_sock_recv_length(struct sock *sk, int ret, int flags)
1012 trace_sock_recv_length(sk, ret, flags);
1015 static inline int sock_recvmsg_nosec(struct socket *sock, struct msghdr *msg,
1016 int flags)
1018 int ret = INDIRECT_CALL_INET(READ_ONCE(sock->ops)->recvmsg,
1019 inet6_recvmsg,
1020 inet_recvmsg, sock, msg,
1021 msg_data_left(msg), flags);
1022 if (trace_sock_recv_length_enabled())
1023 call_trace_sock_recv_length(sock->sk, ret, flags);
1024 return ret;
1028 * sock_recvmsg - receive a message from @sock
1029 * @sock: socket
1030 * @msg: message to receive
1031 * @flags: message flags
1033 * Receives @msg from @sock, passing through LSM. Returns the total number
1034 * of bytes received, or an error.
1036 int sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags)
1038 int err = security_socket_recvmsg(sock, msg, msg_data_left(msg), flags);
1040 return err ?: sock_recvmsg_nosec(sock, msg, flags);
1042 EXPORT_SYMBOL(sock_recvmsg);
1045 * kernel_recvmsg - Receive a message from a socket (kernel space)
1046 * @sock: The socket to receive the message from
1047 * @msg: Received message
1048 * @vec: Input s/g array for message data
1049 * @num: Size of input s/g array
1050 * @size: Number of bytes to read
1051 * @flags: Message flags (MSG_DONTWAIT, etc...)
1053 * On return the msg structure contains the scatter/gather array passed in the
1054 * vec argument. The array is modified so that it consists of the unfilled
1055 * portion of the original array.
1057 * The returned value is the total number of bytes received, or an error.
1060 int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
1061 struct kvec *vec, size_t num, size_t size, int flags)
1063 msg->msg_control_is_user = false;
1064 iov_iter_kvec(&msg->msg_iter, ITER_DEST, vec, num, size);
1065 return sock_recvmsg(sock, msg, flags);
1067 EXPORT_SYMBOL(kernel_recvmsg);
1069 static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
1070 struct pipe_inode_info *pipe, size_t len,
1071 unsigned int flags)
1073 struct socket *sock = file->private_data;
1074 const struct proto_ops *ops;
1076 ops = READ_ONCE(sock->ops);
1077 if (unlikely(!ops->splice_read))
1078 return copy_splice_read(file, ppos, pipe, len, flags);
1080 return ops->splice_read(sock, ppos, pipe, len, flags);
1083 static void sock_splice_eof(struct file *file)
1085 struct socket *sock = file->private_data;
1086 const struct proto_ops *ops;
1088 ops = READ_ONCE(sock->ops);
1089 if (ops->splice_eof)
1090 ops->splice_eof(sock);
1093 static ssize_t sock_read_iter(struct kiocb *iocb, struct iov_iter *to)
1095 struct file *file = iocb->ki_filp;
1096 struct socket *sock = file->private_data;
1097 struct msghdr msg = {.msg_iter = *to,
1098 .msg_iocb = iocb};
1099 ssize_t res;
1101 if (file->f_flags & O_NONBLOCK || (iocb->ki_flags & IOCB_NOWAIT))
1102 msg.msg_flags = MSG_DONTWAIT;
1104 if (iocb->ki_pos != 0)
1105 return -ESPIPE;
1107 if (!iov_iter_count(to)) /* Match SYS5 behaviour */
1108 return 0;
1110 res = sock_recvmsg(sock, &msg, msg.msg_flags);
1111 *to = msg.msg_iter;
1112 return res;
1115 static ssize_t sock_write_iter(struct kiocb *iocb, struct iov_iter *from)
1117 struct file *file = iocb->ki_filp;
1118 struct socket *sock = file->private_data;
1119 struct msghdr msg = {.msg_iter = *from,
1120 .msg_iocb = iocb};
1121 ssize_t res;
1123 if (iocb->ki_pos != 0)
1124 return -ESPIPE;
1126 if (file->f_flags & O_NONBLOCK || (iocb->ki_flags & IOCB_NOWAIT))
1127 msg.msg_flags = MSG_DONTWAIT;
1129 if (sock->type == SOCK_SEQPACKET)
1130 msg.msg_flags |= MSG_EOR;
1132 res = __sock_sendmsg(sock, &msg);
1133 *from = msg.msg_iter;
1134 return res;
1138 * Atomic setting of ioctl hooks to avoid race
1139 * with module unload.
1142 static DEFINE_MUTEX(br_ioctl_mutex);
1143 static int (*br_ioctl_hook)(struct net *net, struct net_bridge *br,
1144 unsigned int cmd, struct ifreq *ifr,
1145 void __user *uarg);
1147 void brioctl_set(int (*hook)(struct net *net, struct net_bridge *br,
1148 unsigned int cmd, struct ifreq *ifr,
1149 void __user *uarg))
1151 mutex_lock(&br_ioctl_mutex);
1152 br_ioctl_hook = hook;
1153 mutex_unlock(&br_ioctl_mutex);
1155 EXPORT_SYMBOL(brioctl_set);
1157 int br_ioctl_call(struct net *net, struct net_bridge *br, unsigned int cmd,
1158 struct ifreq *ifr, void __user *uarg)
1160 int err = -ENOPKG;
1162 if (!br_ioctl_hook)
1163 request_module("bridge");
1165 mutex_lock(&br_ioctl_mutex);
1166 if (br_ioctl_hook)
1167 err = br_ioctl_hook(net, br, cmd, ifr, uarg);
1168 mutex_unlock(&br_ioctl_mutex);
1170 return err;
1173 static DEFINE_MUTEX(vlan_ioctl_mutex);
1174 static int (*vlan_ioctl_hook) (struct net *, void __user *arg);
1176 void vlan_ioctl_set(int (*hook) (struct net *, void __user *))
1178 mutex_lock(&vlan_ioctl_mutex);
1179 vlan_ioctl_hook = hook;
1180 mutex_unlock(&vlan_ioctl_mutex);
1182 EXPORT_SYMBOL(vlan_ioctl_set);
1184 static long sock_do_ioctl(struct net *net, struct socket *sock,
1185 unsigned int cmd, unsigned long arg)
1187 const struct proto_ops *ops = READ_ONCE(sock->ops);
1188 struct ifreq ifr;
1189 bool need_copyout;
1190 int err;
1191 void __user *argp = (void __user *)arg;
1192 void __user *data;
1194 err = ops->ioctl(sock, cmd, arg);
1197 * If this ioctl is unknown try to hand it down
1198 * to the NIC driver.
1200 if (err != -ENOIOCTLCMD)
1201 return err;
1203 if (!is_socket_ioctl_cmd(cmd))
1204 return -ENOTTY;
1206 if (get_user_ifreq(&ifr, &data, argp))
1207 return -EFAULT;
1208 err = dev_ioctl(net, cmd, &ifr, data, &need_copyout);
1209 if (!err && need_copyout)
1210 if (put_user_ifreq(&ifr, argp))
1211 return -EFAULT;
1213 return err;
1217 * With an ioctl, arg may well be a user mode pointer, but we don't know
1218 * what to do with it - that's up to the protocol still.
1221 static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
1223 const struct proto_ops *ops;
1224 struct socket *sock;
1225 struct sock *sk;
1226 void __user *argp = (void __user *)arg;
1227 int pid, err;
1228 struct net *net;
1230 sock = file->private_data;
1231 ops = READ_ONCE(sock->ops);
1232 sk = sock->sk;
1233 net = sock_net(sk);
1234 if (unlikely(cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15))) {
1235 struct ifreq ifr;
1236 void __user *data;
1237 bool need_copyout;
1238 if (get_user_ifreq(&ifr, &data, argp))
1239 return -EFAULT;
1240 err = dev_ioctl(net, cmd, &ifr, data, &need_copyout);
1241 if (!err && need_copyout)
1242 if (put_user_ifreq(&ifr, argp))
1243 return -EFAULT;
1244 } else
1245 #ifdef CONFIG_WEXT_CORE
1246 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
1247 err = wext_handle_ioctl(net, cmd, argp);
1248 } else
1249 #endif
1250 switch (cmd) {
1251 case FIOSETOWN:
1252 case SIOCSPGRP:
1253 err = -EFAULT;
1254 if (get_user(pid, (int __user *)argp))
1255 break;
1256 err = f_setown(sock->file, pid, 1);
1257 break;
1258 case FIOGETOWN:
1259 case SIOCGPGRP:
1260 err = put_user(f_getown(sock->file),
1261 (int __user *)argp);
1262 break;
1263 case SIOCGIFBR:
1264 case SIOCSIFBR:
1265 case SIOCBRADDBR:
1266 case SIOCBRDELBR:
1267 err = br_ioctl_call(net, NULL, cmd, NULL, argp);
1268 break;
1269 case SIOCGIFVLAN:
1270 case SIOCSIFVLAN:
1271 err = -ENOPKG;
1272 if (!vlan_ioctl_hook)
1273 request_module("8021q");
1275 mutex_lock(&vlan_ioctl_mutex);
1276 if (vlan_ioctl_hook)
1277 err = vlan_ioctl_hook(net, argp);
1278 mutex_unlock(&vlan_ioctl_mutex);
1279 break;
1280 case SIOCGSKNS:
1281 err = -EPERM;
1282 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1283 break;
1285 err = open_related_ns(&net->ns, get_net_ns);
1286 break;
1287 case SIOCGSTAMP_OLD:
1288 case SIOCGSTAMPNS_OLD:
1289 if (!ops->gettstamp) {
1290 err = -ENOIOCTLCMD;
1291 break;
1293 err = ops->gettstamp(sock, argp,
1294 cmd == SIOCGSTAMP_OLD,
1295 !IS_ENABLED(CONFIG_64BIT));
1296 break;
1297 case SIOCGSTAMP_NEW:
1298 case SIOCGSTAMPNS_NEW:
1299 if (!ops->gettstamp) {
1300 err = -ENOIOCTLCMD;
1301 break;
1303 err = ops->gettstamp(sock, argp,
1304 cmd == SIOCGSTAMP_NEW,
1305 false);
1306 break;
1308 case SIOCGIFCONF:
1309 err = dev_ifconf(net, argp);
1310 break;
1312 default:
1313 err = sock_do_ioctl(net, sock, cmd, arg);
1314 break;
1316 return err;
1320 * sock_create_lite - creates a socket
1321 * @family: protocol family (AF_INET, ...)
1322 * @type: communication type (SOCK_STREAM, ...)
1323 * @protocol: protocol (0, ...)
1324 * @res: new socket
1326 * Creates a new socket and assigns it to @res, passing through LSM.
1327 * The new socket initialization is not complete, see kernel_accept().
1328 * Returns 0 or an error. On failure @res is set to %NULL.
1329 * This function internally uses GFP_KERNEL.
1332 int sock_create_lite(int family, int type, int protocol, struct socket **res)
1334 int err;
1335 struct socket *sock = NULL;
1337 err = security_socket_create(family, type, protocol, 1);
1338 if (err)
1339 goto out;
1341 sock = sock_alloc();
1342 if (!sock) {
1343 err = -ENOMEM;
1344 goto out;
1347 sock->type = type;
1348 err = security_socket_post_create(sock, family, type, protocol, 1);
1349 if (err)
1350 goto out_release;
1352 out:
1353 *res = sock;
1354 return err;
1355 out_release:
1356 sock_release(sock);
1357 sock = NULL;
1358 goto out;
1360 EXPORT_SYMBOL(sock_create_lite);
1362 /* No kernel lock held - perfect */
1363 static __poll_t sock_poll(struct file *file, poll_table *wait)
1365 struct socket *sock = file->private_data;
1366 const struct proto_ops *ops = READ_ONCE(sock->ops);
1367 __poll_t events = poll_requested_events(wait), flag = 0;
1369 if (!ops->poll)
1370 return 0;
1372 if (sk_can_busy_loop(sock->sk)) {
1373 /* poll once if requested by the syscall */
1374 if (events & POLL_BUSY_LOOP)
1375 sk_busy_loop(sock->sk, 1);
1377 /* if this socket can poll_ll, tell the system call */
1378 flag = POLL_BUSY_LOOP;
1381 return ops->poll(file, sock, wait) | flag;
1384 static int sock_mmap(struct file *file, struct vm_area_struct *vma)
1386 struct socket *sock = file->private_data;
1388 return READ_ONCE(sock->ops)->mmap(file, sock, vma);
1391 static int sock_close(struct inode *inode, struct file *filp)
1393 __sock_release(SOCKET_I(inode), inode);
1394 return 0;
1398 * Update the socket async list
1400 * Fasync_list locking strategy.
1402 * 1. fasync_list is modified only under process context socket lock
1403 * i.e. under semaphore.
1404 * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
1405 * or under socket lock
1408 static int sock_fasync(int fd, struct file *filp, int on)
1410 struct socket *sock = filp->private_data;
1411 struct sock *sk = sock->sk;
1412 struct socket_wq *wq = &sock->wq;
1414 if (sk == NULL)
1415 return -EINVAL;
1417 lock_sock(sk);
1418 fasync_helper(fd, filp, on, &wq->fasync_list);
1420 if (!wq->fasync_list)
1421 sock_reset_flag(sk, SOCK_FASYNC);
1422 else
1423 sock_set_flag(sk, SOCK_FASYNC);
1425 release_sock(sk);
1426 return 0;
1429 /* This function may be called only under rcu_lock */
1431 int sock_wake_async(struct socket_wq *wq, int how, int band)
1433 if (!wq || !wq->fasync_list)
1434 return -1;
1436 switch (how) {
1437 case SOCK_WAKE_WAITD:
1438 if (test_bit(SOCKWQ_ASYNC_WAITDATA, &wq->flags))
1439 break;
1440 goto call_kill;
1441 case SOCK_WAKE_SPACE:
1442 if (!test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags))
1443 break;
1444 fallthrough;
1445 case SOCK_WAKE_IO:
1446 call_kill:
1447 kill_fasync(&wq->fasync_list, SIGIO, band);
1448 break;
1449 case SOCK_WAKE_URG:
1450 kill_fasync(&wq->fasync_list, SIGURG, band);
1453 return 0;
1455 EXPORT_SYMBOL(sock_wake_async);
1458 * __sock_create - creates a socket
1459 * @net: net namespace
1460 * @family: protocol family (AF_INET, ...)
1461 * @type: communication type (SOCK_STREAM, ...)
1462 * @protocol: protocol (0, ...)
1463 * @res: new socket
1464 * @kern: boolean for kernel space sockets
1466 * Creates a new socket and assigns it to @res, passing through LSM.
1467 * Returns 0 or an error. On failure @res is set to %NULL. @kern must
1468 * be set to true if the socket resides in kernel space.
1469 * This function internally uses GFP_KERNEL.
1472 int __sock_create(struct net *net, int family, int type, int protocol,
1473 struct socket **res, int kern)
1475 int err;
1476 struct socket *sock;
1477 const struct net_proto_family *pf;
1480 * Check protocol is in range
1482 if (family < 0 || family >= NPROTO)
1483 return -EAFNOSUPPORT;
1484 if (type < 0 || type >= SOCK_MAX)
1485 return -EINVAL;
1487 /* Compatibility.
1489 This uglymoron is moved from INET layer to here to avoid
1490 deadlock in module load.
1492 if (family == PF_INET && type == SOCK_PACKET) {
1493 pr_info_once("%s uses obsolete (PF_INET,SOCK_PACKET)\n",
1494 current->comm);
1495 family = PF_PACKET;
1498 err = security_socket_create(family, type, protocol, kern);
1499 if (err)
1500 return err;
1503 * Allocate the socket and allow the family to set things up. if
1504 * the protocol is 0, the family is instructed to select an appropriate
1505 * default.
1507 sock = sock_alloc();
1508 if (!sock) {
1509 net_warn_ratelimited("socket: no more sockets\n");
1510 return -ENFILE; /* Not exactly a match, but its the
1511 closest posix thing */
1514 sock->type = type;
1516 #ifdef CONFIG_MODULES
1517 /* Attempt to load a protocol module if the find failed.
1519 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
1520 * requested real, full-featured networking support upon configuration.
1521 * Otherwise module support will break!
1523 if (rcu_access_pointer(net_families[family]) == NULL)
1524 request_module("net-pf-%d", family);
1525 #endif
1527 rcu_read_lock();
1528 pf = rcu_dereference(net_families[family]);
1529 err = -EAFNOSUPPORT;
1530 if (!pf)
1531 goto out_release;
1534 * We will call the ->create function, that possibly is in a loadable
1535 * module, so we have to bump that loadable module refcnt first.
1537 if (!try_module_get(pf->owner))
1538 goto out_release;
1540 /* Now protected by module ref count */
1541 rcu_read_unlock();
1543 err = pf->create(net, sock, protocol, kern);
1544 if (err < 0) {
1545 /* ->create should release the allocated sock->sk object on error
1546 * and make sure sock->sk is set to NULL to avoid use-after-free
1548 DEBUG_NET_WARN_ONCE(sock->sk,
1549 "%ps must clear sock->sk on failure, family: %d, type: %d, protocol: %d\n",
1550 pf->create, family, type, protocol);
1551 goto out_module_put;
1555 * Now to bump the refcnt of the [loadable] module that owns this
1556 * socket at sock_release time we decrement its refcnt.
1558 if (!try_module_get(sock->ops->owner))
1559 goto out_module_busy;
1562 * Now that we're done with the ->create function, the [loadable]
1563 * module can have its refcnt decremented
1565 module_put(pf->owner);
1566 err = security_socket_post_create(sock, family, type, protocol, kern);
1567 if (err)
1568 goto out_sock_release;
1569 *res = sock;
1571 return 0;
1573 out_module_busy:
1574 err = -EAFNOSUPPORT;
1575 out_module_put:
1576 sock->ops = NULL;
1577 module_put(pf->owner);
1578 out_sock_release:
1579 sock_release(sock);
1580 return err;
1582 out_release:
1583 rcu_read_unlock();
1584 goto out_sock_release;
1586 EXPORT_SYMBOL(__sock_create);
1589 * sock_create - creates a socket
1590 * @family: protocol family (AF_INET, ...)
1591 * @type: communication type (SOCK_STREAM, ...)
1592 * @protocol: protocol (0, ...)
1593 * @res: new socket
1595 * A wrapper around __sock_create().
1596 * Returns 0 or an error. This function internally uses GFP_KERNEL.
1599 int sock_create(int family, int type, int protocol, struct socket **res)
1601 return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0);
1603 EXPORT_SYMBOL(sock_create);
1606 * sock_create_kern - creates a socket (kernel space)
1607 * @net: net namespace
1608 * @family: protocol family (AF_INET, ...)
1609 * @type: communication type (SOCK_STREAM, ...)
1610 * @protocol: protocol (0, ...)
1611 * @res: new socket
1613 * A wrapper around __sock_create().
1614 * Returns 0 or an error. This function internally uses GFP_KERNEL.
1617 int sock_create_kern(struct net *net, int family, int type, int protocol, struct socket **res)
1619 return __sock_create(net, family, type, protocol, res, 1);
1621 EXPORT_SYMBOL(sock_create_kern);
1623 static struct socket *__sys_socket_create(int family, int type, int protocol)
1625 struct socket *sock;
1626 int retval;
1628 /* Check the SOCK_* constants for consistency. */
1629 BUILD_BUG_ON(SOCK_CLOEXEC != O_CLOEXEC);
1630 BUILD_BUG_ON((SOCK_MAX | SOCK_TYPE_MASK) != SOCK_TYPE_MASK);
1631 BUILD_BUG_ON(SOCK_CLOEXEC & SOCK_TYPE_MASK);
1632 BUILD_BUG_ON(SOCK_NONBLOCK & SOCK_TYPE_MASK);
1634 if ((type & ~SOCK_TYPE_MASK) & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1635 return ERR_PTR(-EINVAL);
1636 type &= SOCK_TYPE_MASK;
1638 retval = sock_create(family, type, protocol, &sock);
1639 if (retval < 0)
1640 return ERR_PTR(retval);
1642 return sock;
1645 struct file *__sys_socket_file(int family, int type, int protocol)
1647 struct socket *sock;
1648 int flags;
1650 sock = __sys_socket_create(family, type, protocol);
1651 if (IS_ERR(sock))
1652 return ERR_CAST(sock);
1654 flags = type & ~SOCK_TYPE_MASK;
1655 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1656 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1658 return sock_alloc_file(sock, flags, NULL);
1661 /* A hook for bpf progs to attach to and update socket protocol.
1663 * A static noinline declaration here could cause the compiler to
1664 * optimize away the function. A global noinline declaration will
1665 * keep the definition, but may optimize away the callsite.
1666 * Therefore, __weak is needed to ensure that the call is still
1667 * emitted, by telling the compiler that we don't know what the
1668 * function might eventually be.
1671 __bpf_hook_start();
1673 __weak noinline int update_socket_protocol(int family, int type, int protocol)
1675 return protocol;
1678 __bpf_hook_end();
1680 int __sys_socket(int family, int type, int protocol)
1682 struct socket *sock;
1683 int flags;
1685 sock = __sys_socket_create(family, type,
1686 update_socket_protocol(family, type, protocol));
1687 if (IS_ERR(sock))
1688 return PTR_ERR(sock);
1690 flags = type & ~SOCK_TYPE_MASK;
1691 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1692 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1694 return sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK));
1697 SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol)
1699 return __sys_socket(family, type, protocol);
1703 * Create a pair of connected sockets.
1706 int __sys_socketpair(int family, int type, int protocol, int __user *usockvec)
1708 struct socket *sock1, *sock2;
1709 int fd1, fd2, err;
1710 struct file *newfile1, *newfile2;
1711 int flags;
1713 flags = type & ~SOCK_TYPE_MASK;
1714 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1715 return -EINVAL;
1716 type &= SOCK_TYPE_MASK;
1718 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1719 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1722 * reserve descriptors and make sure we won't fail
1723 * to return them to userland.
1725 fd1 = get_unused_fd_flags(flags);
1726 if (unlikely(fd1 < 0))
1727 return fd1;
1729 fd2 = get_unused_fd_flags(flags);
1730 if (unlikely(fd2 < 0)) {
1731 put_unused_fd(fd1);
1732 return fd2;
1735 err = put_user(fd1, &usockvec[0]);
1736 if (err)
1737 goto out;
1739 err = put_user(fd2, &usockvec[1]);
1740 if (err)
1741 goto out;
1744 * Obtain the first socket and check if the underlying protocol
1745 * supports the socketpair call.
1748 err = sock_create(family, type, protocol, &sock1);
1749 if (unlikely(err < 0))
1750 goto out;
1752 err = sock_create(family, type, protocol, &sock2);
1753 if (unlikely(err < 0)) {
1754 sock_release(sock1);
1755 goto out;
1758 err = security_socket_socketpair(sock1, sock2);
1759 if (unlikely(err)) {
1760 sock_release(sock2);
1761 sock_release(sock1);
1762 goto out;
1765 err = READ_ONCE(sock1->ops)->socketpair(sock1, sock2);
1766 if (unlikely(err < 0)) {
1767 sock_release(sock2);
1768 sock_release(sock1);
1769 goto out;
1772 newfile1 = sock_alloc_file(sock1, flags, NULL);
1773 if (IS_ERR(newfile1)) {
1774 err = PTR_ERR(newfile1);
1775 sock_release(sock2);
1776 goto out;
1779 newfile2 = sock_alloc_file(sock2, flags, NULL);
1780 if (IS_ERR(newfile2)) {
1781 err = PTR_ERR(newfile2);
1782 fput(newfile1);
1783 goto out;
1786 audit_fd_pair(fd1, fd2);
1788 fd_install(fd1, newfile1);
1789 fd_install(fd2, newfile2);
1790 return 0;
1792 out:
1793 put_unused_fd(fd2);
1794 put_unused_fd(fd1);
1795 return err;
1798 SYSCALL_DEFINE4(socketpair, int, family, int, type, int, protocol,
1799 int __user *, usockvec)
1801 return __sys_socketpair(family, type, protocol, usockvec);
1804 int __sys_bind_socket(struct socket *sock, struct sockaddr_storage *address,
1805 int addrlen)
1807 int err;
1809 err = security_socket_bind(sock, (struct sockaddr *)address,
1810 addrlen);
1811 if (!err)
1812 err = READ_ONCE(sock->ops)->bind(sock,
1813 (struct sockaddr *)address,
1814 addrlen);
1815 return err;
1819 * Bind a name to a socket. Nothing much to do here since it's
1820 * the protocol's responsibility to handle the local address.
1822 * We move the socket address to kernel space before we call
1823 * the protocol layer (having also checked the address is ok).
1826 int __sys_bind(int fd, struct sockaddr __user *umyaddr, int addrlen)
1828 struct socket *sock;
1829 struct sockaddr_storage address;
1830 CLASS(fd, f)(fd);
1831 int err;
1833 if (fd_empty(f))
1834 return -EBADF;
1835 sock = sock_from_file(fd_file(f));
1836 if (unlikely(!sock))
1837 return -ENOTSOCK;
1839 err = move_addr_to_kernel(umyaddr, addrlen, &address);
1840 if (unlikely(err))
1841 return err;
1843 return __sys_bind_socket(sock, &address, addrlen);
1846 SYSCALL_DEFINE3(bind, int, fd, struct sockaddr __user *, umyaddr, int, addrlen)
1848 return __sys_bind(fd, umyaddr, addrlen);
1852 * Perform a listen. Basically, we allow the protocol to do anything
1853 * necessary for a listen, and if that works, we mark the socket as
1854 * ready for listening.
1856 int __sys_listen_socket(struct socket *sock, int backlog)
1858 int somaxconn, err;
1860 somaxconn = READ_ONCE(sock_net(sock->sk)->core.sysctl_somaxconn);
1861 if ((unsigned int)backlog > somaxconn)
1862 backlog = somaxconn;
1864 err = security_socket_listen(sock, backlog);
1865 if (!err)
1866 err = READ_ONCE(sock->ops)->listen(sock, backlog);
1867 return err;
1870 int __sys_listen(int fd, int backlog)
1872 CLASS(fd, f)(fd);
1873 struct socket *sock;
1875 if (fd_empty(f))
1876 return -EBADF;
1877 sock = sock_from_file(fd_file(f));
1878 if (unlikely(!sock))
1879 return -ENOTSOCK;
1881 return __sys_listen_socket(sock, backlog);
1884 SYSCALL_DEFINE2(listen, int, fd, int, backlog)
1886 return __sys_listen(fd, backlog);
1889 struct file *do_accept(struct file *file, struct proto_accept_arg *arg,
1890 struct sockaddr __user *upeer_sockaddr,
1891 int __user *upeer_addrlen, int flags)
1893 struct socket *sock, *newsock;
1894 struct file *newfile;
1895 int err, len;
1896 struct sockaddr_storage address;
1897 const struct proto_ops *ops;
1899 sock = sock_from_file(file);
1900 if (!sock)
1901 return ERR_PTR(-ENOTSOCK);
1903 newsock = sock_alloc();
1904 if (!newsock)
1905 return ERR_PTR(-ENFILE);
1906 ops = READ_ONCE(sock->ops);
1908 newsock->type = sock->type;
1909 newsock->ops = ops;
1912 * We don't need try_module_get here, as the listening socket (sock)
1913 * has the protocol module (sock->ops->owner) held.
1915 __module_get(ops->owner);
1917 newfile = sock_alloc_file(newsock, flags, sock->sk->sk_prot_creator->name);
1918 if (IS_ERR(newfile))
1919 return newfile;
1921 err = security_socket_accept(sock, newsock);
1922 if (err)
1923 goto out_fd;
1925 arg->flags |= sock->file->f_flags;
1926 err = ops->accept(sock, newsock, arg);
1927 if (err < 0)
1928 goto out_fd;
1930 if (upeer_sockaddr) {
1931 len = ops->getname(newsock, (struct sockaddr *)&address, 2);
1932 if (len < 0) {
1933 err = -ECONNABORTED;
1934 goto out_fd;
1936 err = move_addr_to_user(&address,
1937 len, upeer_sockaddr, upeer_addrlen);
1938 if (err < 0)
1939 goto out_fd;
1942 /* File flags are not inherited via accept() unlike another OSes. */
1943 return newfile;
1944 out_fd:
1945 fput(newfile);
1946 return ERR_PTR(err);
1949 static int __sys_accept4_file(struct file *file, struct sockaddr __user *upeer_sockaddr,
1950 int __user *upeer_addrlen, int flags)
1952 struct proto_accept_arg arg = { };
1953 struct file *newfile;
1954 int newfd;
1956 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1957 return -EINVAL;
1959 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1960 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1962 newfd = get_unused_fd_flags(flags);
1963 if (unlikely(newfd < 0))
1964 return newfd;
1966 newfile = do_accept(file, &arg, upeer_sockaddr, upeer_addrlen,
1967 flags);
1968 if (IS_ERR(newfile)) {
1969 put_unused_fd(newfd);
1970 return PTR_ERR(newfile);
1972 fd_install(newfd, newfile);
1973 return newfd;
1977 * For accept, we attempt to create a new socket, set up the link
1978 * with the client, wake up the client, then return the new
1979 * connected fd. We collect the address of the connector in kernel
1980 * space and move it to user at the very end. This is unclean because
1981 * we open the socket then return an error.
1983 * 1003.1g adds the ability to recvmsg() to query connection pending
1984 * status to recvmsg. We need to add that support in a way thats
1985 * clean when we restructure accept also.
1988 int __sys_accept4(int fd, struct sockaddr __user *upeer_sockaddr,
1989 int __user *upeer_addrlen, int flags)
1991 CLASS(fd, f)(fd);
1993 if (fd_empty(f))
1994 return -EBADF;
1995 return __sys_accept4_file(fd_file(f), upeer_sockaddr,
1996 upeer_addrlen, flags);
1999 SYSCALL_DEFINE4(accept4, int, fd, struct sockaddr __user *, upeer_sockaddr,
2000 int __user *, upeer_addrlen, int, flags)
2002 return __sys_accept4(fd, upeer_sockaddr, upeer_addrlen, flags);
2005 SYSCALL_DEFINE3(accept, int, fd, struct sockaddr __user *, upeer_sockaddr,
2006 int __user *, upeer_addrlen)
2008 return __sys_accept4(fd, upeer_sockaddr, upeer_addrlen, 0);
2012 * Attempt to connect to a socket with the server address. The address
2013 * is in user space so we verify it is OK and move it to kernel space.
2015 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
2016 * break bindings
2018 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
2019 * other SEQPACKET protocols that take time to connect() as it doesn't
2020 * include the -EINPROGRESS status for such sockets.
2023 int __sys_connect_file(struct file *file, struct sockaddr_storage *address,
2024 int addrlen, int file_flags)
2026 struct socket *sock;
2027 int err;
2029 sock = sock_from_file(file);
2030 if (!sock) {
2031 err = -ENOTSOCK;
2032 goto out;
2035 err =
2036 security_socket_connect(sock, (struct sockaddr *)address, addrlen);
2037 if (err)
2038 goto out;
2040 err = READ_ONCE(sock->ops)->connect(sock, (struct sockaddr *)address,
2041 addrlen, sock->file->f_flags | file_flags);
2042 out:
2043 return err;
2046 int __sys_connect(int fd, struct sockaddr __user *uservaddr, int addrlen)
2048 struct sockaddr_storage address;
2049 CLASS(fd, f)(fd);
2050 int ret;
2052 if (fd_empty(f))
2053 return -EBADF;
2055 ret = move_addr_to_kernel(uservaddr, addrlen, &address);
2056 if (ret)
2057 return ret;
2059 return __sys_connect_file(fd_file(f), &address, addrlen, 0);
2062 SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
2063 int, addrlen)
2065 return __sys_connect(fd, uservaddr, addrlen);
2069 * Get the local address ('name') of a socket object. Move the obtained
2070 * name to user space.
2073 int __sys_getsockname(int fd, struct sockaddr __user *usockaddr,
2074 int __user *usockaddr_len)
2076 struct socket *sock;
2077 struct sockaddr_storage address;
2078 CLASS(fd, f)(fd);
2079 int err;
2081 if (fd_empty(f))
2082 return -EBADF;
2083 sock = sock_from_file(fd_file(f));
2084 if (unlikely(!sock))
2085 return -ENOTSOCK;
2087 err = security_socket_getsockname(sock);
2088 if (err)
2089 return err;
2091 err = READ_ONCE(sock->ops)->getname(sock, (struct sockaddr *)&address, 0);
2092 if (err < 0)
2093 return err;
2095 /* "err" is actually length in this case */
2096 return move_addr_to_user(&address, err, usockaddr, usockaddr_len);
2099 SYSCALL_DEFINE3(getsockname, int, fd, struct sockaddr __user *, usockaddr,
2100 int __user *, usockaddr_len)
2102 return __sys_getsockname(fd, usockaddr, usockaddr_len);
2106 * Get the remote address ('name') of a socket object. Move the obtained
2107 * name to user space.
2110 int __sys_getpeername(int fd, struct sockaddr __user *usockaddr,
2111 int __user *usockaddr_len)
2113 struct socket *sock;
2114 struct sockaddr_storage address;
2115 CLASS(fd, f)(fd);
2116 int err;
2118 if (fd_empty(f))
2119 return -EBADF;
2120 sock = sock_from_file(fd_file(f));
2121 if (unlikely(!sock))
2122 return -ENOTSOCK;
2124 err = security_socket_getpeername(sock);
2125 if (err)
2126 return err;
2128 err = READ_ONCE(sock->ops)->getname(sock, (struct sockaddr *)&address, 1);
2129 if (err < 0)
2130 return err;
2132 /* "err" is actually length in this case */
2133 return move_addr_to_user(&address, err, usockaddr, usockaddr_len);
2136 SYSCALL_DEFINE3(getpeername, int, fd, struct sockaddr __user *, usockaddr,
2137 int __user *, usockaddr_len)
2139 return __sys_getpeername(fd, usockaddr, usockaddr_len);
2143 * Send a datagram to a given address. We move the address into kernel
2144 * space and check the user space data area is readable before invoking
2145 * the protocol.
2147 int __sys_sendto(int fd, void __user *buff, size_t len, unsigned int flags,
2148 struct sockaddr __user *addr, int addr_len)
2150 struct socket *sock;
2151 struct sockaddr_storage address;
2152 int err;
2153 struct msghdr msg;
2155 err = import_ubuf(ITER_SOURCE, buff, len, &msg.msg_iter);
2156 if (unlikely(err))
2157 return err;
2159 CLASS(fd, f)(fd);
2160 if (fd_empty(f))
2161 return -EBADF;
2162 sock = sock_from_file(fd_file(f));
2163 if (unlikely(!sock))
2164 return -ENOTSOCK;
2166 msg.msg_name = NULL;
2167 msg.msg_control = NULL;
2168 msg.msg_controllen = 0;
2169 msg.msg_namelen = 0;
2170 msg.msg_ubuf = NULL;
2171 if (addr) {
2172 err = move_addr_to_kernel(addr, addr_len, &address);
2173 if (err < 0)
2174 return err;
2175 msg.msg_name = (struct sockaddr *)&address;
2176 msg.msg_namelen = addr_len;
2178 flags &= ~MSG_INTERNAL_SENDMSG_FLAGS;
2179 if (sock->file->f_flags & O_NONBLOCK)
2180 flags |= MSG_DONTWAIT;
2181 msg.msg_flags = flags;
2182 return __sock_sendmsg(sock, &msg);
2185 SYSCALL_DEFINE6(sendto, int, fd, void __user *, buff, size_t, len,
2186 unsigned int, flags, struct sockaddr __user *, addr,
2187 int, addr_len)
2189 return __sys_sendto(fd, buff, len, flags, addr, addr_len);
2193 * Send a datagram down a socket.
2196 SYSCALL_DEFINE4(send, int, fd, void __user *, buff, size_t, len,
2197 unsigned int, flags)
2199 return __sys_sendto(fd, buff, len, flags, NULL, 0);
2203 * Receive a frame from the socket and optionally record the address of the
2204 * sender. We verify the buffers are writable and if needed move the
2205 * sender address from kernel to user space.
2207 int __sys_recvfrom(int fd, void __user *ubuf, size_t size, unsigned int flags,
2208 struct sockaddr __user *addr, int __user *addr_len)
2210 struct sockaddr_storage address;
2211 struct msghdr msg = {
2212 /* Save some cycles and don't copy the address if not needed */
2213 .msg_name = addr ? (struct sockaddr *)&address : NULL,
2215 struct socket *sock;
2216 int err, err2;
2218 err = import_ubuf(ITER_DEST, ubuf, size, &msg.msg_iter);
2219 if (unlikely(err))
2220 return err;
2222 CLASS(fd, f)(fd);
2224 if (fd_empty(f))
2225 return -EBADF;
2226 sock = sock_from_file(fd_file(f));
2227 if (unlikely(!sock))
2228 return -ENOTSOCK;
2230 if (sock->file->f_flags & O_NONBLOCK)
2231 flags |= MSG_DONTWAIT;
2232 err = sock_recvmsg(sock, &msg, flags);
2234 if (err >= 0 && addr != NULL) {
2235 err2 = move_addr_to_user(&address,
2236 msg.msg_namelen, addr, addr_len);
2237 if (err2 < 0)
2238 err = err2;
2240 return err;
2243 SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, ubuf, size_t, size,
2244 unsigned int, flags, struct sockaddr __user *, addr,
2245 int __user *, addr_len)
2247 return __sys_recvfrom(fd, ubuf, size, flags, addr, addr_len);
2251 * Receive a datagram from a socket.
2254 SYSCALL_DEFINE4(recv, int, fd, void __user *, ubuf, size_t, size,
2255 unsigned int, flags)
2257 return __sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
2260 static bool sock_use_custom_sol_socket(const struct socket *sock)
2262 return test_bit(SOCK_CUSTOM_SOCKOPT, &sock->flags);
2265 int do_sock_setsockopt(struct socket *sock, bool compat, int level,
2266 int optname, sockptr_t optval, int optlen)
2268 const struct proto_ops *ops;
2269 char *kernel_optval = NULL;
2270 int err;
2272 if (optlen < 0)
2273 return -EINVAL;
2275 err = security_socket_setsockopt(sock, level, optname);
2276 if (err)
2277 goto out_put;
2279 if (!compat)
2280 err = BPF_CGROUP_RUN_PROG_SETSOCKOPT(sock->sk, &level, &optname,
2281 optval, &optlen,
2282 &kernel_optval);
2283 if (err < 0)
2284 goto out_put;
2285 if (err > 0) {
2286 err = 0;
2287 goto out_put;
2290 if (kernel_optval)
2291 optval = KERNEL_SOCKPTR(kernel_optval);
2292 ops = READ_ONCE(sock->ops);
2293 if (level == SOL_SOCKET && !sock_use_custom_sol_socket(sock))
2294 err = sock_setsockopt(sock, level, optname, optval, optlen);
2295 else if (unlikely(!ops->setsockopt))
2296 err = -EOPNOTSUPP;
2297 else
2298 err = ops->setsockopt(sock, level, optname, optval,
2299 optlen);
2300 kfree(kernel_optval);
2301 out_put:
2302 return err;
2304 EXPORT_SYMBOL(do_sock_setsockopt);
2306 /* Set a socket option. Because we don't know the option lengths we have
2307 * to pass the user mode parameter for the protocols to sort out.
2309 int __sys_setsockopt(int fd, int level, int optname, char __user *user_optval,
2310 int optlen)
2312 sockptr_t optval = USER_SOCKPTR(user_optval);
2313 bool compat = in_compat_syscall();
2314 struct socket *sock;
2315 CLASS(fd, f)(fd);
2317 if (fd_empty(f))
2318 return -EBADF;
2319 sock = sock_from_file(fd_file(f));
2320 if (unlikely(!sock))
2321 return -ENOTSOCK;
2323 return do_sock_setsockopt(sock, compat, level, optname, optval, optlen);
2326 SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname,
2327 char __user *, optval, int, optlen)
2329 return __sys_setsockopt(fd, level, optname, optval, optlen);
2332 INDIRECT_CALLABLE_DECLARE(bool tcp_bpf_bypass_getsockopt(int level,
2333 int optname));
2335 int do_sock_getsockopt(struct socket *sock, bool compat, int level,
2336 int optname, sockptr_t optval, sockptr_t optlen)
2338 int max_optlen __maybe_unused = 0;
2339 const struct proto_ops *ops;
2340 int err;
2342 err = security_socket_getsockopt(sock, level, optname);
2343 if (err)
2344 return err;
2346 if (!compat)
2347 copy_from_sockptr(&max_optlen, optlen, sizeof(int));
2349 ops = READ_ONCE(sock->ops);
2350 if (level == SOL_SOCKET) {
2351 err = sk_getsockopt(sock->sk, level, optname, optval, optlen);
2352 } else if (unlikely(!ops->getsockopt)) {
2353 err = -EOPNOTSUPP;
2354 } else {
2355 if (WARN_ONCE(optval.is_kernel || optlen.is_kernel,
2356 "Invalid argument type"))
2357 return -EOPNOTSUPP;
2359 err = ops->getsockopt(sock, level, optname, optval.user,
2360 optlen.user);
2363 if (!compat)
2364 err = BPF_CGROUP_RUN_PROG_GETSOCKOPT(sock->sk, level, optname,
2365 optval, optlen, max_optlen,
2366 err);
2368 return err;
2370 EXPORT_SYMBOL(do_sock_getsockopt);
2373 * Get a socket option. Because we don't know the option lengths we have
2374 * to pass a user mode parameter for the protocols to sort out.
2376 int __sys_getsockopt(int fd, int level, int optname, char __user *optval,
2377 int __user *optlen)
2379 struct socket *sock;
2380 CLASS(fd, f)(fd);
2382 if (fd_empty(f))
2383 return -EBADF;
2384 sock = sock_from_file(fd_file(f));
2385 if (unlikely(!sock))
2386 return -ENOTSOCK;
2388 return do_sock_getsockopt(sock, in_compat_syscall(), level, optname,
2389 USER_SOCKPTR(optval), USER_SOCKPTR(optlen));
2392 SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname,
2393 char __user *, optval, int __user *, optlen)
2395 return __sys_getsockopt(fd, level, optname, optval, optlen);
2399 * Shutdown a socket.
2402 int __sys_shutdown_sock(struct socket *sock, int how)
2404 int err;
2406 err = security_socket_shutdown(sock, how);
2407 if (!err)
2408 err = READ_ONCE(sock->ops)->shutdown(sock, how);
2410 return err;
2413 int __sys_shutdown(int fd, int how)
2415 struct socket *sock;
2416 CLASS(fd, f)(fd);
2418 if (fd_empty(f))
2419 return -EBADF;
2420 sock = sock_from_file(fd_file(f));
2421 if (unlikely(!sock))
2422 return -ENOTSOCK;
2424 return __sys_shutdown_sock(sock, how);
2427 SYSCALL_DEFINE2(shutdown, int, fd, int, how)
2429 return __sys_shutdown(fd, how);
2432 /* A couple of helpful macros for getting the address of the 32/64 bit
2433 * fields which are the same type (int / unsigned) on our platforms.
2435 #define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
2436 #define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
2437 #define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
2439 struct used_address {
2440 struct sockaddr_storage name;
2441 unsigned int name_len;
2444 int __copy_msghdr(struct msghdr *kmsg,
2445 struct user_msghdr *msg,
2446 struct sockaddr __user **save_addr)
2448 ssize_t err;
2450 kmsg->msg_control_is_user = true;
2451 kmsg->msg_get_inq = 0;
2452 kmsg->msg_control_user = msg->msg_control;
2453 kmsg->msg_controllen = msg->msg_controllen;
2454 kmsg->msg_flags = msg->msg_flags;
2456 kmsg->msg_namelen = msg->msg_namelen;
2457 if (!msg->msg_name)
2458 kmsg->msg_namelen = 0;
2460 if (kmsg->msg_namelen < 0)
2461 return -EINVAL;
2463 if (kmsg->msg_namelen > sizeof(struct sockaddr_storage))
2464 kmsg->msg_namelen = sizeof(struct sockaddr_storage);
2466 if (save_addr)
2467 *save_addr = msg->msg_name;
2469 if (msg->msg_name && kmsg->msg_namelen) {
2470 if (!save_addr) {
2471 err = move_addr_to_kernel(msg->msg_name,
2472 kmsg->msg_namelen,
2473 kmsg->msg_name);
2474 if (err < 0)
2475 return err;
2477 } else {
2478 kmsg->msg_name = NULL;
2479 kmsg->msg_namelen = 0;
2482 if (msg->msg_iovlen > UIO_MAXIOV)
2483 return -EMSGSIZE;
2485 kmsg->msg_iocb = NULL;
2486 kmsg->msg_ubuf = NULL;
2487 return 0;
2490 static int copy_msghdr_from_user(struct msghdr *kmsg,
2491 struct user_msghdr __user *umsg,
2492 struct sockaddr __user **save_addr,
2493 struct iovec **iov)
2495 struct user_msghdr msg;
2496 ssize_t err;
2498 if (copy_from_user(&msg, umsg, sizeof(*umsg)))
2499 return -EFAULT;
2501 err = __copy_msghdr(kmsg, &msg, save_addr);
2502 if (err)
2503 return err;
2505 err = import_iovec(save_addr ? ITER_DEST : ITER_SOURCE,
2506 msg.msg_iov, msg.msg_iovlen,
2507 UIO_FASTIOV, iov, &kmsg->msg_iter);
2508 return err < 0 ? err : 0;
2511 static int ____sys_sendmsg(struct socket *sock, struct msghdr *msg_sys,
2512 unsigned int flags, struct used_address *used_address,
2513 unsigned int allowed_msghdr_flags)
2515 unsigned char ctl[sizeof(struct cmsghdr) + 20]
2516 __aligned(sizeof(__kernel_size_t));
2517 /* 20 is size of ipv6_pktinfo */
2518 unsigned char *ctl_buf = ctl;
2519 int ctl_len;
2520 ssize_t err;
2522 err = -ENOBUFS;
2524 if (msg_sys->msg_controllen > INT_MAX)
2525 goto out;
2526 flags |= (msg_sys->msg_flags & allowed_msghdr_flags);
2527 ctl_len = msg_sys->msg_controllen;
2528 if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
2529 err =
2530 cmsghdr_from_user_compat_to_kern(msg_sys, sock->sk, ctl,
2531 sizeof(ctl));
2532 if (err)
2533 goto out;
2534 ctl_buf = msg_sys->msg_control;
2535 ctl_len = msg_sys->msg_controllen;
2536 } else if (ctl_len) {
2537 BUILD_BUG_ON(sizeof(struct cmsghdr) !=
2538 CMSG_ALIGN(sizeof(struct cmsghdr)));
2539 if (ctl_len > sizeof(ctl)) {
2540 ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
2541 if (ctl_buf == NULL)
2542 goto out;
2544 err = -EFAULT;
2545 if (copy_from_user(ctl_buf, msg_sys->msg_control_user, ctl_len))
2546 goto out_freectl;
2547 msg_sys->msg_control = ctl_buf;
2548 msg_sys->msg_control_is_user = false;
2550 flags &= ~MSG_INTERNAL_SENDMSG_FLAGS;
2551 msg_sys->msg_flags = flags;
2553 if (sock->file->f_flags & O_NONBLOCK)
2554 msg_sys->msg_flags |= MSG_DONTWAIT;
2556 * If this is sendmmsg() and current destination address is same as
2557 * previously succeeded address, omit asking LSM's decision.
2558 * used_address->name_len is initialized to UINT_MAX so that the first
2559 * destination address never matches.
2561 if (used_address && msg_sys->msg_name &&
2562 used_address->name_len == msg_sys->msg_namelen &&
2563 !memcmp(&used_address->name, msg_sys->msg_name,
2564 used_address->name_len)) {
2565 err = sock_sendmsg_nosec(sock, msg_sys);
2566 goto out_freectl;
2568 err = __sock_sendmsg(sock, msg_sys);
2570 * If this is sendmmsg() and sending to current destination address was
2571 * successful, remember it.
2573 if (used_address && err >= 0) {
2574 used_address->name_len = msg_sys->msg_namelen;
2575 if (msg_sys->msg_name)
2576 memcpy(&used_address->name, msg_sys->msg_name,
2577 used_address->name_len);
2580 out_freectl:
2581 if (ctl_buf != ctl)
2582 sock_kfree_s(sock->sk, ctl_buf, ctl_len);
2583 out:
2584 return err;
2587 static int sendmsg_copy_msghdr(struct msghdr *msg,
2588 struct user_msghdr __user *umsg, unsigned flags,
2589 struct iovec **iov)
2591 int err;
2593 if (flags & MSG_CMSG_COMPAT) {
2594 struct compat_msghdr __user *msg_compat;
2596 msg_compat = (struct compat_msghdr __user *) umsg;
2597 err = get_compat_msghdr(msg, msg_compat, NULL, iov);
2598 } else {
2599 err = copy_msghdr_from_user(msg, umsg, NULL, iov);
2601 if (err < 0)
2602 return err;
2604 return 0;
2607 static int ___sys_sendmsg(struct socket *sock, struct user_msghdr __user *msg,
2608 struct msghdr *msg_sys, unsigned int flags,
2609 struct used_address *used_address,
2610 unsigned int allowed_msghdr_flags)
2612 struct sockaddr_storage address;
2613 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
2614 ssize_t err;
2616 msg_sys->msg_name = &address;
2618 err = sendmsg_copy_msghdr(msg_sys, msg, flags, &iov);
2619 if (err < 0)
2620 return err;
2622 err = ____sys_sendmsg(sock, msg_sys, flags, used_address,
2623 allowed_msghdr_flags);
2624 kfree(iov);
2625 return err;
2629 * BSD sendmsg interface
2631 long __sys_sendmsg_sock(struct socket *sock, struct msghdr *msg,
2632 unsigned int flags)
2634 return ____sys_sendmsg(sock, msg, flags, NULL, 0);
2637 long __sys_sendmsg(int fd, struct user_msghdr __user *msg, unsigned int flags,
2638 bool forbid_cmsg_compat)
2640 struct msghdr msg_sys;
2641 struct socket *sock;
2643 if (forbid_cmsg_compat && (flags & MSG_CMSG_COMPAT))
2644 return -EINVAL;
2646 CLASS(fd, f)(fd);
2648 if (fd_empty(f))
2649 return -EBADF;
2650 sock = sock_from_file(fd_file(f));
2651 if (unlikely(!sock))
2652 return -ENOTSOCK;
2654 return ___sys_sendmsg(sock, msg, &msg_sys, flags, NULL, 0);
2657 SYSCALL_DEFINE3(sendmsg, int, fd, struct user_msghdr __user *, msg, unsigned int, flags)
2659 return __sys_sendmsg(fd, msg, flags, true);
2663 * Linux sendmmsg interface
2666 int __sys_sendmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
2667 unsigned int flags, bool forbid_cmsg_compat)
2669 int err, datagrams;
2670 struct socket *sock;
2671 struct mmsghdr __user *entry;
2672 struct compat_mmsghdr __user *compat_entry;
2673 struct msghdr msg_sys;
2674 struct used_address used_address;
2675 unsigned int oflags = flags;
2677 if (forbid_cmsg_compat && (flags & MSG_CMSG_COMPAT))
2678 return -EINVAL;
2680 if (vlen > UIO_MAXIOV)
2681 vlen = UIO_MAXIOV;
2683 datagrams = 0;
2685 CLASS(fd, f)(fd);
2687 if (fd_empty(f))
2688 return -EBADF;
2689 sock = sock_from_file(fd_file(f));
2690 if (unlikely(!sock))
2691 return -ENOTSOCK;
2693 used_address.name_len = UINT_MAX;
2694 entry = mmsg;
2695 compat_entry = (struct compat_mmsghdr __user *)mmsg;
2696 err = 0;
2697 flags |= MSG_BATCH;
2699 while (datagrams < vlen) {
2700 if (datagrams == vlen - 1)
2701 flags = oflags;
2703 if (MSG_CMSG_COMPAT & flags) {
2704 err = ___sys_sendmsg(sock, (struct user_msghdr __user *)compat_entry,
2705 &msg_sys, flags, &used_address, MSG_EOR);
2706 if (err < 0)
2707 break;
2708 err = __put_user(err, &compat_entry->msg_len);
2709 ++compat_entry;
2710 } else {
2711 err = ___sys_sendmsg(sock,
2712 (struct user_msghdr __user *)entry,
2713 &msg_sys, flags, &used_address, MSG_EOR);
2714 if (err < 0)
2715 break;
2716 err = put_user(err, &entry->msg_len);
2717 ++entry;
2720 if (err)
2721 break;
2722 ++datagrams;
2723 if (msg_data_left(&msg_sys))
2724 break;
2725 cond_resched();
2728 /* We only return an error if no datagrams were able to be sent */
2729 if (datagrams != 0)
2730 return datagrams;
2732 return err;
2735 SYSCALL_DEFINE4(sendmmsg, int, fd, struct mmsghdr __user *, mmsg,
2736 unsigned int, vlen, unsigned int, flags)
2738 return __sys_sendmmsg(fd, mmsg, vlen, flags, true);
2741 static int recvmsg_copy_msghdr(struct msghdr *msg,
2742 struct user_msghdr __user *umsg, unsigned flags,
2743 struct sockaddr __user **uaddr,
2744 struct iovec **iov)
2746 ssize_t err;
2748 if (MSG_CMSG_COMPAT & flags) {
2749 struct compat_msghdr __user *msg_compat;
2751 msg_compat = (struct compat_msghdr __user *) umsg;
2752 err = get_compat_msghdr(msg, msg_compat, uaddr, iov);
2753 } else {
2754 err = copy_msghdr_from_user(msg, umsg, uaddr, iov);
2756 if (err < 0)
2757 return err;
2759 return 0;
2762 static int ____sys_recvmsg(struct socket *sock, struct msghdr *msg_sys,
2763 struct user_msghdr __user *msg,
2764 struct sockaddr __user *uaddr,
2765 unsigned int flags, int nosec)
2767 struct compat_msghdr __user *msg_compat =
2768 (struct compat_msghdr __user *) msg;
2769 int __user *uaddr_len = COMPAT_NAMELEN(msg);
2770 struct sockaddr_storage addr;
2771 unsigned long cmsg_ptr;
2772 int len;
2773 ssize_t err;
2775 msg_sys->msg_name = &addr;
2776 cmsg_ptr = (unsigned long)msg_sys->msg_control;
2777 msg_sys->msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT);
2779 /* We assume all kernel code knows the size of sockaddr_storage */
2780 msg_sys->msg_namelen = 0;
2782 if (sock->file->f_flags & O_NONBLOCK)
2783 flags |= MSG_DONTWAIT;
2785 if (unlikely(nosec))
2786 err = sock_recvmsg_nosec(sock, msg_sys, flags);
2787 else
2788 err = sock_recvmsg(sock, msg_sys, flags);
2790 if (err < 0)
2791 goto out;
2792 len = err;
2794 if (uaddr != NULL) {
2795 err = move_addr_to_user(&addr,
2796 msg_sys->msg_namelen, uaddr,
2797 uaddr_len);
2798 if (err < 0)
2799 goto out;
2801 err = __put_user((msg_sys->msg_flags & ~MSG_CMSG_COMPAT),
2802 COMPAT_FLAGS(msg));
2803 if (err)
2804 goto out;
2805 if (MSG_CMSG_COMPAT & flags)
2806 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
2807 &msg_compat->msg_controllen);
2808 else
2809 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
2810 &msg->msg_controllen);
2811 if (err)
2812 goto out;
2813 err = len;
2814 out:
2815 return err;
2818 static int ___sys_recvmsg(struct socket *sock, struct user_msghdr __user *msg,
2819 struct msghdr *msg_sys, unsigned int flags, int nosec)
2821 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
2822 /* user mode address pointers */
2823 struct sockaddr __user *uaddr;
2824 ssize_t err;
2826 err = recvmsg_copy_msghdr(msg_sys, msg, flags, &uaddr, &iov);
2827 if (err < 0)
2828 return err;
2830 err = ____sys_recvmsg(sock, msg_sys, msg, uaddr, flags, nosec);
2831 kfree(iov);
2832 return err;
2836 * BSD recvmsg interface
2839 long __sys_recvmsg_sock(struct socket *sock, struct msghdr *msg,
2840 struct user_msghdr __user *umsg,
2841 struct sockaddr __user *uaddr, unsigned int flags)
2843 return ____sys_recvmsg(sock, msg, umsg, uaddr, flags, 0);
2846 long __sys_recvmsg(int fd, struct user_msghdr __user *msg, unsigned int flags,
2847 bool forbid_cmsg_compat)
2849 struct msghdr msg_sys;
2850 struct socket *sock;
2852 if (forbid_cmsg_compat && (flags & MSG_CMSG_COMPAT))
2853 return -EINVAL;
2855 CLASS(fd, f)(fd);
2857 if (fd_empty(f))
2858 return -EBADF;
2859 sock = sock_from_file(fd_file(f));
2860 if (unlikely(!sock))
2861 return -ENOTSOCK;
2863 return ___sys_recvmsg(sock, msg, &msg_sys, flags, 0);
2866 SYSCALL_DEFINE3(recvmsg, int, fd, struct user_msghdr __user *, msg,
2867 unsigned int, flags)
2869 return __sys_recvmsg(fd, msg, flags, true);
2873 * Linux recvmmsg interface
2876 static int do_recvmmsg(int fd, struct mmsghdr __user *mmsg,
2877 unsigned int vlen, unsigned int flags,
2878 struct timespec64 *timeout)
2880 int err = 0, datagrams;
2881 struct socket *sock;
2882 struct mmsghdr __user *entry;
2883 struct compat_mmsghdr __user *compat_entry;
2884 struct msghdr msg_sys;
2885 struct timespec64 end_time;
2886 struct timespec64 timeout64;
2888 if (timeout &&
2889 poll_select_set_timeout(&end_time, timeout->tv_sec,
2890 timeout->tv_nsec))
2891 return -EINVAL;
2893 datagrams = 0;
2895 CLASS(fd, f)(fd);
2897 if (fd_empty(f))
2898 return -EBADF;
2899 sock = sock_from_file(fd_file(f));
2900 if (unlikely(!sock))
2901 return -ENOTSOCK;
2903 if (likely(!(flags & MSG_ERRQUEUE))) {
2904 err = sock_error(sock->sk);
2905 if (err)
2906 return err;
2909 entry = mmsg;
2910 compat_entry = (struct compat_mmsghdr __user *)mmsg;
2912 while (datagrams < vlen) {
2914 * No need to ask LSM for more than the first datagram.
2916 if (MSG_CMSG_COMPAT & flags) {
2917 err = ___sys_recvmsg(sock, (struct user_msghdr __user *)compat_entry,
2918 &msg_sys, flags & ~MSG_WAITFORONE,
2919 datagrams);
2920 if (err < 0)
2921 break;
2922 err = __put_user(err, &compat_entry->msg_len);
2923 ++compat_entry;
2924 } else {
2925 err = ___sys_recvmsg(sock,
2926 (struct user_msghdr __user *)entry,
2927 &msg_sys, flags & ~MSG_WAITFORONE,
2928 datagrams);
2929 if (err < 0)
2930 break;
2931 err = put_user(err, &entry->msg_len);
2932 ++entry;
2935 if (err)
2936 break;
2937 ++datagrams;
2939 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
2940 if (flags & MSG_WAITFORONE)
2941 flags |= MSG_DONTWAIT;
2943 if (timeout) {
2944 ktime_get_ts64(&timeout64);
2945 *timeout = timespec64_sub(end_time, timeout64);
2946 if (timeout->tv_sec < 0) {
2947 timeout->tv_sec = timeout->tv_nsec = 0;
2948 break;
2951 /* Timeout, return less than vlen datagrams */
2952 if (timeout->tv_nsec == 0 && timeout->tv_sec == 0)
2953 break;
2956 /* Out of band data, return right away */
2957 if (msg_sys.msg_flags & MSG_OOB)
2958 break;
2959 cond_resched();
2962 if (err == 0)
2963 return datagrams;
2965 if (datagrams == 0)
2966 return err;
2969 * We may return less entries than requested (vlen) if the
2970 * sock is non block and there aren't enough datagrams...
2972 if (err != -EAGAIN) {
2974 * ... or if recvmsg returns an error after we
2975 * received some datagrams, where we record the
2976 * error to return on the next call or if the
2977 * app asks about it using getsockopt(SO_ERROR).
2979 WRITE_ONCE(sock->sk->sk_err, -err);
2981 return datagrams;
2984 int __sys_recvmmsg(int fd, struct mmsghdr __user *mmsg,
2985 unsigned int vlen, unsigned int flags,
2986 struct __kernel_timespec __user *timeout,
2987 struct old_timespec32 __user *timeout32)
2989 int datagrams;
2990 struct timespec64 timeout_sys;
2992 if (timeout && get_timespec64(&timeout_sys, timeout))
2993 return -EFAULT;
2995 if (timeout32 && get_old_timespec32(&timeout_sys, timeout32))
2996 return -EFAULT;
2998 if (!timeout && !timeout32)
2999 return do_recvmmsg(fd, mmsg, vlen, flags, NULL);
3001 datagrams = do_recvmmsg(fd, mmsg, vlen, flags, &timeout_sys);
3003 if (datagrams <= 0)
3004 return datagrams;
3006 if (timeout && put_timespec64(&timeout_sys, timeout))
3007 datagrams = -EFAULT;
3009 if (timeout32 && put_old_timespec32(&timeout_sys, timeout32))
3010 datagrams = -EFAULT;
3012 return datagrams;
3015 SYSCALL_DEFINE5(recvmmsg, int, fd, struct mmsghdr __user *, mmsg,
3016 unsigned int, vlen, unsigned int, flags,
3017 struct __kernel_timespec __user *, timeout)
3019 if (flags & MSG_CMSG_COMPAT)
3020 return -EINVAL;
3022 return __sys_recvmmsg(fd, mmsg, vlen, flags, timeout, NULL);
3025 #ifdef CONFIG_COMPAT_32BIT_TIME
3026 SYSCALL_DEFINE5(recvmmsg_time32, int, fd, struct mmsghdr __user *, mmsg,
3027 unsigned int, vlen, unsigned int, flags,
3028 struct old_timespec32 __user *, timeout)
3030 if (flags & MSG_CMSG_COMPAT)
3031 return -EINVAL;
3033 return __sys_recvmmsg(fd, mmsg, vlen, flags, NULL, timeout);
3035 #endif
3037 #ifdef __ARCH_WANT_SYS_SOCKETCALL
3038 /* Argument list sizes for sys_socketcall */
3039 #define AL(x) ((x) * sizeof(unsigned long))
3040 static const unsigned char nargs[21] = {
3041 AL(0), AL(3), AL(3), AL(3), AL(2), AL(3),
3042 AL(3), AL(3), AL(4), AL(4), AL(4), AL(6),
3043 AL(6), AL(2), AL(5), AL(5), AL(3), AL(3),
3044 AL(4), AL(5), AL(4)
3047 #undef AL
3050 * System call vectors.
3052 * Argument checking cleaned up. Saved 20% in size.
3053 * This function doesn't need to set the kernel lock because
3054 * it is set by the callees.
3057 SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args)
3059 unsigned long a[AUDITSC_ARGS];
3060 unsigned long a0, a1;
3061 int err;
3062 unsigned int len;
3064 if (call < 1 || call > SYS_SENDMMSG)
3065 return -EINVAL;
3066 call = array_index_nospec(call, SYS_SENDMMSG + 1);
3068 len = nargs[call];
3069 if (len > sizeof(a))
3070 return -EINVAL;
3072 /* copy_from_user should be SMP safe. */
3073 if (copy_from_user(a, args, len))
3074 return -EFAULT;
3076 err = audit_socketcall(nargs[call] / sizeof(unsigned long), a);
3077 if (err)
3078 return err;
3080 a0 = a[0];
3081 a1 = a[1];
3083 switch (call) {
3084 case SYS_SOCKET:
3085 err = __sys_socket(a0, a1, a[2]);
3086 break;
3087 case SYS_BIND:
3088 err = __sys_bind(a0, (struct sockaddr __user *)a1, a[2]);
3089 break;
3090 case SYS_CONNECT:
3091 err = __sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
3092 break;
3093 case SYS_LISTEN:
3094 err = __sys_listen(a0, a1);
3095 break;
3096 case SYS_ACCEPT:
3097 err = __sys_accept4(a0, (struct sockaddr __user *)a1,
3098 (int __user *)a[2], 0);
3099 break;
3100 case SYS_GETSOCKNAME:
3101 err =
3102 __sys_getsockname(a0, (struct sockaddr __user *)a1,
3103 (int __user *)a[2]);
3104 break;
3105 case SYS_GETPEERNAME:
3106 err =
3107 __sys_getpeername(a0, (struct sockaddr __user *)a1,
3108 (int __user *)a[2]);
3109 break;
3110 case SYS_SOCKETPAIR:
3111 err = __sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
3112 break;
3113 case SYS_SEND:
3114 err = __sys_sendto(a0, (void __user *)a1, a[2], a[3],
3115 NULL, 0);
3116 break;
3117 case SYS_SENDTO:
3118 err = __sys_sendto(a0, (void __user *)a1, a[2], a[3],
3119 (struct sockaddr __user *)a[4], a[5]);
3120 break;
3121 case SYS_RECV:
3122 err = __sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
3123 NULL, NULL);
3124 break;
3125 case SYS_RECVFROM:
3126 err = __sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
3127 (struct sockaddr __user *)a[4],
3128 (int __user *)a[5]);
3129 break;
3130 case SYS_SHUTDOWN:
3131 err = __sys_shutdown(a0, a1);
3132 break;
3133 case SYS_SETSOCKOPT:
3134 err = __sys_setsockopt(a0, a1, a[2], (char __user *)a[3],
3135 a[4]);
3136 break;
3137 case SYS_GETSOCKOPT:
3138 err =
3139 __sys_getsockopt(a0, a1, a[2], (char __user *)a[3],
3140 (int __user *)a[4]);
3141 break;
3142 case SYS_SENDMSG:
3143 err = __sys_sendmsg(a0, (struct user_msghdr __user *)a1,
3144 a[2], true);
3145 break;
3146 case SYS_SENDMMSG:
3147 err = __sys_sendmmsg(a0, (struct mmsghdr __user *)a1, a[2],
3148 a[3], true);
3149 break;
3150 case SYS_RECVMSG:
3151 err = __sys_recvmsg(a0, (struct user_msghdr __user *)a1,
3152 a[2], true);
3153 break;
3154 case SYS_RECVMMSG:
3155 if (IS_ENABLED(CONFIG_64BIT))
3156 err = __sys_recvmmsg(a0, (struct mmsghdr __user *)a1,
3157 a[2], a[3],
3158 (struct __kernel_timespec __user *)a[4],
3159 NULL);
3160 else
3161 err = __sys_recvmmsg(a0, (struct mmsghdr __user *)a1,
3162 a[2], a[3], NULL,
3163 (struct old_timespec32 __user *)a[4]);
3164 break;
3165 case SYS_ACCEPT4:
3166 err = __sys_accept4(a0, (struct sockaddr __user *)a1,
3167 (int __user *)a[2], a[3]);
3168 break;
3169 default:
3170 err = -EINVAL;
3171 break;
3173 return err;
3176 #endif /* __ARCH_WANT_SYS_SOCKETCALL */
3179 * sock_register - add a socket protocol handler
3180 * @ops: description of protocol
3182 * This function is called by a protocol handler that wants to
3183 * advertise its address family, and have it linked into the
3184 * socket interface. The value ops->family corresponds to the
3185 * socket system call protocol family.
3187 int sock_register(const struct net_proto_family *ops)
3189 int err;
3191 if (ops->family >= NPROTO) {
3192 pr_crit("protocol %d >= NPROTO(%d)\n", ops->family, NPROTO);
3193 return -ENOBUFS;
3196 spin_lock(&net_family_lock);
3197 if (rcu_dereference_protected(net_families[ops->family],
3198 lockdep_is_held(&net_family_lock)))
3199 err = -EEXIST;
3200 else {
3201 rcu_assign_pointer(net_families[ops->family], ops);
3202 err = 0;
3204 spin_unlock(&net_family_lock);
3206 pr_info("NET: Registered %s protocol family\n", pf_family_names[ops->family]);
3207 return err;
3209 EXPORT_SYMBOL(sock_register);
3212 * sock_unregister - remove a protocol handler
3213 * @family: protocol family to remove
3215 * This function is called by a protocol handler that wants to
3216 * remove its address family, and have it unlinked from the
3217 * new socket creation.
3219 * If protocol handler is a module, then it can use module reference
3220 * counts to protect against new references. If protocol handler is not
3221 * a module then it needs to provide its own protection in
3222 * the ops->create routine.
3224 void sock_unregister(int family)
3226 BUG_ON(family < 0 || family >= NPROTO);
3228 spin_lock(&net_family_lock);
3229 RCU_INIT_POINTER(net_families[family], NULL);
3230 spin_unlock(&net_family_lock);
3232 synchronize_rcu();
3234 pr_info("NET: Unregistered %s protocol family\n", pf_family_names[family]);
3236 EXPORT_SYMBOL(sock_unregister);
3238 bool sock_is_registered(int family)
3240 return family < NPROTO && rcu_access_pointer(net_families[family]);
3243 static int __init sock_init(void)
3245 int err;
3247 * Initialize the network sysctl infrastructure.
3249 err = net_sysctl_init();
3250 if (err)
3251 goto out;
3254 * Initialize skbuff SLAB cache
3256 skb_init();
3259 * Initialize the protocols module.
3262 init_inodecache();
3264 err = register_filesystem(&sock_fs_type);
3265 if (err)
3266 goto out;
3267 sock_mnt = kern_mount(&sock_fs_type);
3268 if (IS_ERR(sock_mnt)) {
3269 err = PTR_ERR(sock_mnt);
3270 goto out_mount;
3273 /* The real protocol initialization is performed in later initcalls.
3276 #ifdef CONFIG_NETFILTER
3277 err = netfilter_init();
3278 if (err)
3279 goto out;
3280 #endif
3282 ptp_classifier_init();
3284 out:
3285 return err;
3287 out_mount:
3288 unregister_filesystem(&sock_fs_type);
3289 goto out;
3292 core_initcall(sock_init); /* early initcall */
3294 #ifdef CONFIG_PROC_FS
3295 void socket_seq_show(struct seq_file *seq)
3297 seq_printf(seq, "sockets: used %d\n",
3298 sock_inuse_get(seq->private));
3300 #endif /* CONFIG_PROC_FS */
3302 /* Handle the fact that while struct ifreq has the same *layout* on
3303 * 32/64 for everything but ifreq::ifru_ifmap and ifreq::ifru_data,
3304 * which are handled elsewhere, it still has different *size* due to
3305 * ifreq::ifru_ifmap (which is 16 bytes on 32 bit, 24 bytes on 64-bit,
3306 * resulting in struct ifreq being 32 and 40 bytes respectively).
3307 * As a result, if the struct happens to be at the end of a page and
3308 * the next page isn't readable/writable, we get a fault. To prevent
3309 * that, copy back and forth to the full size.
3311 int get_user_ifreq(struct ifreq *ifr, void __user **ifrdata, void __user *arg)
3313 if (in_compat_syscall()) {
3314 struct compat_ifreq *ifr32 = (struct compat_ifreq *)ifr;
3316 memset(ifr, 0, sizeof(*ifr));
3317 if (copy_from_user(ifr32, arg, sizeof(*ifr32)))
3318 return -EFAULT;
3320 if (ifrdata)
3321 *ifrdata = compat_ptr(ifr32->ifr_data);
3323 return 0;
3326 if (copy_from_user(ifr, arg, sizeof(*ifr)))
3327 return -EFAULT;
3329 if (ifrdata)
3330 *ifrdata = ifr->ifr_data;
3332 return 0;
3334 EXPORT_SYMBOL(get_user_ifreq);
3336 int put_user_ifreq(struct ifreq *ifr, void __user *arg)
3338 size_t size = sizeof(*ifr);
3340 if (in_compat_syscall())
3341 size = sizeof(struct compat_ifreq);
3343 if (copy_to_user(arg, ifr, size))
3344 return -EFAULT;
3346 return 0;
3348 EXPORT_SYMBOL(put_user_ifreq);
3350 #ifdef CONFIG_COMPAT
3351 static int compat_siocwandev(struct net *net, struct compat_ifreq __user *uifr32)
3353 compat_uptr_t uptr32;
3354 struct ifreq ifr;
3355 void __user *saved;
3356 int err;
3358 if (get_user_ifreq(&ifr, NULL, uifr32))
3359 return -EFAULT;
3361 if (get_user(uptr32, &uifr32->ifr_settings.ifs_ifsu))
3362 return -EFAULT;
3364 saved = ifr.ifr_settings.ifs_ifsu.raw_hdlc;
3365 ifr.ifr_settings.ifs_ifsu.raw_hdlc = compat_ptr(uptr32);
3367 err = dev_ioctl(net, SIOCWANDEV, &ifr, NULL, NULL);
3368 if (!err) {
3369 ifr.ifr_settings.ifs_ifsu.raw_hdlc = saved;
3370 if (put_user_ifreq(&ifr, uifr32))
3371 err = -EFAULT;
3373 return err;
3376 /* Handle ioctls that use ifreq::ifr_data and just need struct ifreq converted */
3377 static int compat_ifr_data_ioctl(struct net *net, unsigned int cmd,
3378 struct compat_ifreq __user *u_ifreq32)
3380 struct ifreq ifreq;
3381 void __user *data;
3383 if (!is_socket_ioctl_cmd(cmd))
3384 return -ENOTTY;
3385 if (get_user_ifreq(&ifreq, &data, u_ifreq32))
3386 return -EFAULT;
3387 ifreq.ifr_data = data;
3389 return dev_ioctl(net, cmd, &ifreq, data, NULL);
3392 static int compat_sock_ioctl_trans(struct file *file, struct socket *sock,
3393 unsigned int cmd, unsigned long arg)
3395 void __user *argp = compat_ptr(arg);
3396 struct sock *sk = sock->sk;
3397 struct net *net = sock_net(sk);
3398 const struct proto_ops *ops;
3400 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15))
3401 return sock_ioctl(file, cmd, (unsigned long)argp);
3403 switch (cmd) {
3404 case SIOCWANDEV:
3405 return compat_siocwandev(net, argp);
3406 case SIOCGSTAMP_OLD:
3407 case SIOCGSTAMPNS_OLD:
3408 ops = READ_ONCE(sock->ops);
3409 if (!ops->gettstamp)
3410 return -ENOIOCTLCMD;
3411 return ops->gettstamp(sock, argp, cmd == SIOCGSTAMP_OLD,
3412 !COMPAT_USE_64BIT_TIME);
3414 case SIOCETHTOOL:
3415 case SIOCBONDSLAVEINFOQUERY:
3416 case SIOCBONDINFOQUERY:
3417 case SIOCSHWTSTAMP:
3418 case SIOCGHWTSTAMP:
3419 return compat_ifr_data_ioctl(net, cmd, argp);
3421 case FIOSETOWN:
3422 case SIOCSPGRP:
3423 case FIOGETOWN:
3424 case SIOCGPGRP:
3425 case SIOCBRADDBR:
3426 case SIOCBRDELBR:
3427 case SIOCGIFVLAN:
3428 case SIOCSIFVLAN:
3429 case SIOCGSKNS:
3430 case SIOCGSTAMP_NEW:
3431 case SIOCGSTAMPNS_NEW:
3432 case SIOCGIFCONF:
3433 case SIOCSIFBR:
3434 case SIOCGIFBR:
3435 return sock_ioctl(file, cmd, arg);
3437 case SIOCGIFFLAGS:
3438 case SIOCSIFFLAGS:
3439 case SIOCGIFMAP:
3440 case SIOCSIFMAP:
3441 case SIOCGIFMETRIC:
3442 case SIOCSIFMETRIC:
3443 case SIOCGIFMTU:
3444 case SIOCSIFMTU:
3445 case SIOCGIFMEM:
3446 case SIOCSIFMEM:
3447 case SIOCGIFHWADDR:
3448 case SIOCSIFHWADDR:
3449 case SIOCADDMULTI:
3450 case SIOCDELMULTI:
3451 case SIOCGIFINDEX:
3452 case SIOCGIFADDR:
3453 case SIOCSIFADDR:
3454 case SIOCSIFHWBROADCAST:
3455 case SIOCDIFADDR:
3456 case SIOCGIFBRDADDR:
3457 case SIOCSIFBRDADDR:
3458 case SIOCGIFDSTADDR:
3459 case SIOCSIFDSTADDR:
3460 case SIOCGIFNETMASK:
3461 case SIOCSIFNETMASK:
3462 case SIOCSIFPFLAGS:
3463 case SIOCGIFPFLAGS:
3464 case SIOCGIFTXQLEN:
3465 case SIOCSIFTXQLEN:
3466 case SIOCBRADDIF:
3467 case SIOCBRDELIF:
3468 case SIOCGIFNAME:
3469 case SIOCSIFNAME:
3470 case SIOCGMIIPHY:
3471 case SIOCGMIIREG:
3472 case SIOCSMIIREG:
3473 case SIOCBONDENSLAVE:
3474 case SIOCBONDRELEASE:
3475 case SIOCBONDSETHWADDR:
3476 case SIOCBONDCHANGEACTIVE:
3477 case SIOCSARP:
3478 case SIOCGARP:
3479 case SIOCDARP:
3480 case SIOCOUTQ:
3481 case SIOCOUTQNSD:
3482 case SIOCATMARK:
3483 return sock_do_ioctl(net, sock, cmd, arg);
3486 return -ENOIOCTLCMD;
3489 static long compat_sock_ioctl(struct file *file, unsigned int cmd,
3490 unsigned long arg)
3492 struct socket *sock = file->private_data;
3493 const struct proto_ops *ops = READ_ONCE(sock->ops);
3494 int ret = -ENOIOCTLCMD;
3495 struct sock *sk;
3496 struct net *net;
3498 sk = sock->sk;
3499 net = sock_net(sk);
3501 if (ops->compat_ioctl)
3502 ret = ops->compat_ioctl(sock, cmd, arg);
3504 if (ret == -ENOIOCTLCMD &&
3505 (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST))
3506 ret = compat_wext_handle_ioctl(net, cmd, arg);
3508 if (ret == -ENOIOCTLCMD)
3509 ret = compat_sock_ioctl_trans(file, sock, cmd, arg);
3511 return ret;
3513 #endif
3516 * kernel_bind - bind an address to a socket (kernel space)
3517 * @sock: socket
3518 * @addr: address
3519 * @addrlen: length of address
3521 * Returns 0 or an error.
3524 int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen)
3526 struct sockaddr_storage address;
3528 memcpy(&address, addr, addrlen);
3530 return READ_ONCE(sock->ops)->bind(sock, (struct sockaddr *)&address,
3531 addrlen);
3533 EXPORT_SYMBOL(kernel_bind);
3536 * kernel_listen - move socket to listening state (kernel space)
3537 * @sock: socket
3538 * @backlog: pending connections queue size
3540 * Returns 0 or an error.
3543 int kernel_listen(struct socket *sock, int backlog)
3545 return READ_ONCE(sock->ops)->listen(sock, backlog);
3547 EXPORT_SYMBOL(kernel_listen);
3550 * kernel_accept - accept a connection (kernel space)
3551 * @sock: listening socket
3552 * @newsock: new connected socket
3553 * @flags: flags
3555 * @flags must be SOCK_CLOEXEC, SOCK_NONBLOCK or 0.
3556 * If it fails, @newsock is guaranteed to be %NULL.
3557 * Returns 0 or an error.
3560 int kernel_accept(struct socket *sock, struct socket **newsock, int flags)
3562 struct sock *sk = sock->sk;
3563 const struct proto_ops *ops = READ_ONCE(sock->ops);
3564 struct proto_accept_arg arg = {
3565 .flags = flags,
3566 .kern = true,
3568 int err;
3570 err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
3571 newsock);
3572 if (err < 0)
3573 goto done;
3575 err = ops->accept(sock, *newsock, &arg);
3576 if (err < 0) {
3577 sock_release(*newsock);
3578 *newsock = NULL;
3579 goto done;
3582 (*newsock)->ops = ops;
3583 __module_get(ops->owner);
3585 done:
3586 return err;
3588 EXPORT_SYMBOL(kernel_accept);
3591 * kernel_connect - connect a socket (kernel space)
3592 * @sock: socket
3593 * @addr: address
3594 * @addrlen: address length
3595 * @flags: flags (O_NONBLOCK, ...)
3597 * For datagram sockets, @addr is the address to which datagrams are sent
3598 * by default, and the only address from which datagrams are received.
3599 * For stream sockets, attempts to connect to @addr.
3600 * Returns 0 or an error code.
3603 int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
3604 int flags)
3606 struct sockaddr_storage address;
3608 memcpy(&address, addr, addrlen);
3610 return READ_ONCE(sock->ops)->connect(sock, (struct sockaddr *)&address,
3611 addrlen, flags);
3613 EXPORT_SYMBOL(kernel_connect);
3616 * kernel_getsockname - get the address which the socket is bound (kernel space)
3617 * @sock: socket
3618 * @addr: address holder
3620 * Fills the @addr pointer with the address which the socket is bound.
3621 * Returns the length of the address in bytes or an error code.
3624 int kernel_getsockname(struct socket *sock, struct sockaddr *addr)
3626 return READ_ONCE(sock->ops)->getname(sock, addr, 0);
3628 EXPORT_SYMBOL(kernel_getsockname);
3631 * kernel_getpeername - get the address which the socket is connected (kernel space)
3632 * @sock: socket
3633 * @addr: address holder
3635 * Fills the @addr pointer with the address which the socket is connected.
3636 * Returns the length of the address in bytes or an error code.
3639 int kernel_getpeername(struct socket *sock, struct sockaddr *addr)
3641 return READ_ONCE(sock->ops)->getname(sock, addr, 1);
3643 EXPORT_SYMBOL(kernel_getpeername);
3646 * kernel_sock_shutdown - shut down part of a full-duplex connection (kernel space)
3647 * @sock: socket
3648 * @how: connection part
3650 * Returns 0 or an error.
3653 int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how)
3655 return READ_ONCE(sock->ops)->shutdown(sock, how);
3657 EXPORT_SYMBOL(kernel_sock_shutdown);
3660 * kernel_sock_ip_overhead - returns the IP overhead imposed by a socket
3661 * @sk: socket
3663 * This routine returns the IP overhead imposed by a socket i.e.
3664 * the length of the underlying IP header, depending on whether
3665 * this is an IPv4 or IPv6 socket and the length from IP options turned
3666 * on at the socket. Assumes that the caller has a lock on the socket.
3669 u32 kernel_sock_ip_overhead(struct sock *sk)
3671 struct inet_sock *inet;
3672 struct ip_options_rcu *opt;
3673 u32 overhead = 0;
3674 #if IS_ENABLED(CONFIG_IPV6)
3675 struct ipv6_pinfo *np;
3676 struct ipv6_txoptions *optv6 = NULL;
3677 #endif /* IS_ENABLED(CONFIG_IPV6) */
3679 if (!sk)
3680 return overhead;
3682 switch (sk->sk_family) {
3683 case AF_INET:
3684 inet = inet_sk(sk);
3685 overhead += sizeof(struct iphdr);
3686 opt = rcu_dereference_protected(inet->inet_opt,
3687 sock_owned_by_user(sk));
3688 if (opt)
3689 overhead += opt->opt.optlen;
3690 return overhead;
3691 #if IS_ENABLED(CONFIG_IPV6)
3692 case AF_INET6:
3693 np = inet6_sk(sk);
3694 overhead += sizeof(struct ipv6hdr);
3695 if (np)
3696 optv6 = rcu_dereference_protected(np->opt,
3697 sock_owned_by_user(sk));
3698 if (optv6)
3699 overhead += (optv6->opt_flen + optv6->opt_nflen);
3700 return overhead;
3701 #endif /* IS_ENABLED(CONFIG_IPV6) */
3702 default: /* Returns 0 overhead if the socket is not ipv4 or ipv6 */
3703 return overhead;
3706 EXPORT_SYMBOL(kernel_sock_ip_overhead);