projects/linux·blob v7.1 · refs

3837 lines · 93.9 KiB

1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * NET An implementation of the SOCKET network access protocol.
4 *
5 * Version: @(#)socket.c 1.1.93 18/02/95
6 *
7 * Authors: Orest Zborowski, <[email protected]>
8 * Ross Biro
9 * Fred N. van Kempen, <[email protected]>
10 *
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
48 *
49 * This module is effectively the top level interface to the BSD socket
50 * paradigm.
51 *
52 * Based upon Swansea University Computer Society NET3.039
53 */
54
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/uio.h>
81#include <linux/syscalls.h>
82#include <linux/compat.h>
83#include <linux/kmod.h>
84#include <linux/audit.h>
85#include <linux/wireless.h>
86#include <linux/nsproxy.h>
87#include <linux/magic.h>
88#include <linux/slab.h>
89#include <linux/xattr.h>
90#include <linux/nospec.h>
91#include <linux/indirect_call_wrapper.h>
92#include <linux/io_uring/net.h>
93
94#include <linux/uaccess.h>
95#include <asm/unistd.h>
96
97#include <net/compat.h>
98#include <net/wext.h>
99#include <net/cls_cgroup.h>
100
101#include <net/sock.h>
102#include <linux/netfilter.h>
103
104#include <linux/if_tun.h>
105#include <linux/ipv6_route.h>
106#include <linux/route.h>
107#include <linux/termios.h>
108#include <linux/sockios.h>
109#include <net/busy_poll.h>
110#include <linux/errqueue.h>
111#include <linux/ptp_clock_kernel.h>
112#include <trace/events/sock.h>
113
114#include "core/dev.h"
115
116#ifdef CONFIG_NET_RX_BUSY_POLL
117unsigned int sysctl_net_busy_read __read_mostly;
118unsigned int sysctl_net_busy_poll __read_mostly;
119#endif
120
121static ssize_t sock_read_iter(struct kiocb *iocb, struct iov_iter *to);
122static ssize_t sock_write_iter(struct kiocb *iocb, struct iov_iter *from);
123static int sock_mmap(struct file *file, struct vm_area_struct *vma);
124
125static int sock_close(struct inode *inode, struct file *file);
126static __poll_t sock_poll(struct file *file,
127 struct poll_table_struct *wait);
128static long sock_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
129#ifdef CONFIG_COMPAT
130static long compat_sock_ioctl(struct file *file,
131 unsigned int cmd, unsigned long arg);
132#endif
133static int sock_fasync(int fd, struct file *filp, int on);
134static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
135 struct pipe_inode_info *pipe, size_t len,
136 unsigned int flags);
137static void sock_splice_eof(struct file *file);
138
139#ifdef CONFIG_PROC_FS
140static void sock_show_fdinfo(struct seq_file *m, struct file *f)
141{
142 struct socket *sock = f->private_data;
143 const struct proto_ops *ops = READ_ONCE(sock->ops);
144
145 if (ops->show_fdinfo)
146 ops->show_fdinfo(m, sock);
147}
148#else
149#define sock_show_fdinfo NULL
150#endif
151
152/*
153 * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
154 * in the operation structures but are done directly via the socketcall() multiplexor.
155 */
156
157static const struct file_operations socket_file_ops = {
158 .owner = THIS_MODULE,
159 .read_iter = sock_read_iter,
160 .write_iter = sock_write_iter,
161 .poll = sock_poll,
162 .unlocked_ioctl = sock_ioctl,
163#ifdef CONFIG_COMPAT
164 .compat_ioctl = compat_sock_ioctl,
165#endif
166 .uring_cmd = io_uring_cmd_sock,
167 .mmap = sock_mmap,
168 .release = sock_close,
169 .fasync = sock_fasync,
170 .splice_write = splice_to_socket,
171 .splice_read = sock_splice_read,
172 .splice_eof = sock_splice_eof,
173 .show_fdinfo = sock_show_fdinfo,
174};
175
176static const char * const pf_family_names[] = {
177 [PF_UNSPEC] = "PF_UNSPEC",
178 [PF_UNIX] = "PF_UNIX/PF_LOCAL",
179 [PF_INET] = "PF_INET",
180 [PF_AX25] = "PF_AX25",
181 [PF_IPX] = "PF_IPX",
182 [PF_APPLETALK] = "PF_APPLETALK",
183 [PF_NETROM] = "PF_NETROM",
184 [PF_BRIDGE] = "PF_BRIDGE",
185 [PF_ATMPVC] = "PF_ATMPVC",
186 [PF_X25] = "PF_X25",
187 [PF_INET6] = "PF_INET6",
188 [PF_ROSE] = "PF_ROSE",
189 [PF_DECnet] = "PF_DECnet",
190 [PF_NETBEUI] = "PF_NETBEUI",
191 [PF_SECURITY] = "PF_SECURITY",
192 [PF_KEY] = "PF_KEY",
193 [PF_NETLINK] = "PF_NETLINK/PF_ROUTE",
194 [PF_PACKET] = "PF_PACKET",
195 [PF_ASH] = "PF_ASH",
196 [PF_ECONET] = "PF_ECONET",
197 [PF_ATMSVC] = "PF_ATMSVC",
198 [PF_RDS] = "PF_RDS",
199 [PF_SNA] = "PF_SNA",
200 [PF_IRDA] = "PF_IRDA",
201 [PF_PPPOX] = "PF_PPPOX",
202 [PF_WANPIPE] = "PF_WANPIPE",
203 [PF_LLC] = "PF_LLC",
204 [PF_IB] = "PF_IB",
205 [PF_MPLS] = "PF_MPLS",
206 [PF_CAN] = "PF_CAN",
207 [PF_TIPC] = "PF_TIPC",
208 [PF_BLUETOOTH] = "PF_BLUETOOTH",
209 [PF_IUCV] = "PF_IUCV",
210 [PF_RXRPC] = "PF_RXRPC",
211 [PF_ISDN] = "PF_ISDN",
212 [PF_PHONET] = "PF_PHONET",
213 [PF_IEEE802154] = "PF_IEEE802154",
214 [PF_CAIF] = "PF_CAIF",
215 [PF_ALG] = "PF_ALG",
216 [PF_NFC] = "PF_NFC",
217 [PF_VSOCK] = "PF_VSOCK",
218 [PF_KCM] = "PF_KCM",
219 [PF_QIPCRTR] = "PF_QIPCRTR",
220 [PF_SMC] = "PF_SMC",
221 [PF_XDP] = "PF_XDP",
222 [PF_MCTP] = "PF_MCTP",
223};
224
225/*
226 * The protocol list. Each protocol is registered in here.
227 */
228
229static DEFINE_SPINLOCK(net_family_lock);
230static const struct net_proto_family __rcu *net_families[NPROTO] __read_mostly;
231
232/*
233 * Support routines.
234 * Move socket addresses back and forth across the kernel/user
235 * divide and look after the messy bits.
236 */
237
238/**
239 * move_addr_to_kernel - copy a socket address into kernel space
240 * @uaddr: Address in user space
241 * @kaddr: Address in kernel space
242 * @ulen: Length in user space
243 *
244 * The address is copied into kernel space. If the provided address is
245 * too long an error code of -EINVAL is returned. If the copy gives
246 * invalid addresses -EFAULT is returned. On a success 0 is returned.
247 */
248
249int move_addr_to_kernel(void __user *uaddr, int ulen, struct sockaddr_storage *kaddr)
250{
251 if (ulen < 0 || ulen > sizeof(struct sockaddr_storage))
252 return -EINVAL;
253 if (ulen == 0)
254 return 0;
255 if (copy_from_user(kaddr, uaddr, ulen))
256 return -EFAULT;
257 return audit_sockaddr(ulen, kaddr);
258}
259
260/**
261 * move_addr_to_user - copy an address to user space
262 * @kaddr: kernel space address
263 * @klen: length of address in kernel
264 * @uaddr: user space address
265 * @ulen: pointer to user length field
266 *
267 * The value pointed to by ulen on entry is the buffer length available.
268 * This is overwritten with the buffer space used. -EINVAL is returned
269 * if an overlong buffer is specified or a negative buffer size. -EFAULT
270 * is returned if either the buffer or the length field are not
271 * accessible.
272 * After copying the data up to the limit the user specifies, the true
273 * length of the data is written over the length limit the user
274 * specified. Zero is returned for a success.
275 */
276
277static int move_addr_to_user(struct sockaddr_storage *kaddr, int klen,
278 void __user *uaddr, int __user *ulen)
279{
280 int len;
281
282 BUG_ON(klen > sizeof(struct sockaddr_storage));
283
284 scoped_user_rw_access_size(ulen, 4, efault_end) {
285 unsafe_get_user(len, ulen, efault_end);
286
287 if (len > klen)
288 len = klen;
289 /*
290 * "fromlen shall refer to the value before truncation.."
291 * 1003.1g
292 */
293 if (len >= 0)
294 unsafe_put_user(klen, ulen, efault_end);
295 }
296
297 if (len) {
298 if (len < 0)
299 return -EINVAL;
300 if (audit_sockaddr(klen, kaddr))
301 return -ENOMEM;
302 if (copy_to_user(uaddr, kaddr, len))
303 return -EFAULT;
304 }
305 return 0;
306
307efault_end:
308 return -EFAULT;
309}
310
311static struct kmem_cache *sock_inode_cachep __ro_after_init;
312
313struct sockfs_inode {
314 struct simple_xattrs *xattrs;
315 struct simple_xattr_limits xattr_limits;
316 struct socket_alloc;
317};
318
319static struct sockfs_inode *SOCKFS_I(struct inode *inode)
320{
321 return container_of(inode, struct sockfs_inode, vfs_inode);
322}
323
324static struct inode *sock_alloc_inode(struct super_block *sb)
325{
326 struct sockfs_inode *si;
327
328 si = alloc_inode_sb(sb, sock_inode_cachep, GFP_KERNEL);
329 if (!si)
330 return NULL;
331 si->xattrs = NULL;
332 simple_xattr_limits_init(&si->xattr_limits);
333
334 init_waitqueue_head(&si->socket.wq.wait);
335 si->socket.wq.fasync_list = NULL;
336 si->socket.wq.flags = 0;
337
338 si->socket.state = SS_UNCONNECTED;
339 si->socket.flags = 0;
340 si->socket.ops = NULL;
341 si->socket.sk = NULL;
342 si->socket.file = NULL;
343
344 return &si->vfs_inode;
345}
346
347static void sock_evict_inode(struct inode *inode)
348{
349 struct sockfs_inode *si = SOCKFS_I(inode);
350 struct simple_xattrs *xattrs = si->xattrs;
351
352 if (xattrs) {
353 simple_xattrs_free(xattrs, NULL);
354 kfree(xattrs);
355 }
356 clear_inode(inode);
357}
358
359static void sock_free_inode(struct inode *inode)
360{
361 struct sockfs_inode *si = SOCKFS_I(inode);
362
363 kmem_cache_free(sock_inode_cachep, si);
364}
365
366static void init_once(void *foo)
367{
368 struct sockfs_inode *si = (struct sockfs_inode *)foo;
369
370 inode_init_once(&si->vfs_inode);
371}
372
373static void init_inodecache(void)
374{
375 sock_inode_cachep = kmem_cache_create("sock_inode_cache",
376 sizeof(struct sockfs_inode),
377 0,
378 (SLAB_HWCACHE_ALIGN |
379 SLAB_RECLAIM_ACCOUNT |
380 SLAB_ACCOUNT),
381 init_once);
382 BUG_ON(sock_inode_cachep == NULL);
383}
384
385static const struct super_operations sockfs_ops = {
386 .alloc_inode = sock_alloc_inode,
387 .free_inode = sock_free_inode,
388 .evict_inode = sock_evict_inode,
389 .statfs = simple_statfs,
390};
391
392/*
393 * sockfs_dname() is called from d_path().
394 */
395static char *sockfs_dname(struct dentry *dentry, char *buffer, int buflen)
396{
397 return dynamic_dname(buffer, buflen, "socket:[%llu]",
398 d_inode(dentry)->i_ino);
399}
400
401static const struct dentry_operations sockfs_dentry_operations = {
402 .d_dname = sockfs_dname,
403};
404
405static int sockfs_xattr_get(const struct xattr_handler *handler,
406 struct dentry *dentry, struct inode *inode,
407 const char *suffix, void *value, size_t size)
408{
409 if (value) {
410 if (dentry->d_name.len + 1 > size)
411 return -ERANGE;
412 memcpy(value, dentry->d_name.name, dentry->d_name.len + 1);
413 }
414 return dentry->d_name.len + 1;
415}
416
417#define XATTR_SOCKPROTONAME_SUFFIX "sockprotoname"
418#define XATTR_NAME_SOCKPROTONAME (XATTR_SYSTEM_PREFIX XATTR_SOCKPROTONAME_SUFFIX)
419#define XATTR_NAME_SOCKPROTONAME_LEN (sizeof(XATTR_NAME_SOCKPROTONAME)-1)
420
421static const struct xattr_handler sockfs_xattr_handler = {
422 .name = XATTR_NAME_SOCKPROTONAME,
423 .get = sockfs_xattr_get,
424};
425
426static int sockfs_security_xattr_set(const struct xattr_handler *handler,
427 struct mnt_idmap *idmap,
428 struct dentry *dentry, struct inode *inode,
429 const char *suffix, const void *value,
430 size_t size, int flags)
431{
432 /* Handled by LSM. */
433 return -EAGAIN;
434}
435
436static const struct xattr_handler sockfs_security_xattr_handler = {
437 .prefix = XATTR_SECURITY_PREFIX,
438 .set = sockfs_security_xattr_set,
439};
440
441static int sockfs_user_xattr_get(const struct xattr_handler *handler,
442 struct dentry *dentry, struct inode *inode,
443 const char *suffix, void *value, size_t size)
444{
445 const char *name = xattr_full_name(handler, suffix);
446 struct simple_xattrs *xattrs;
447
448 xattrs = READ_ONCE(SOCKFS_I(inode)->xattrs);
449 if (!xattrs)
450 return -ENODATA;
451
452 return simple_xattr_get(xattrs, name, value, size);
453}
454
455static int sockfs_user_xattr_set(const struct xattr_handler *handler,
456 struct mnt_idmap *idmap,
457 struct dentry *dentry, struct inode *inode,
458 const char *suffix, const void *value,
459 size_t size, int flags)
460{
461 const char *name = xattr_full_name(handler, suffix);
462 struct sockfs_inode *si = SOCKFS_I(inode);
463 struct simple_xattrs *xattrs;
464
465 xattrs = simple_xattrs_lazy_alloc(&si->xattrs, value, flags);
466 if (IS_ERR_OR_NULL(xattrs))
467 return PTR_ERR(xattrs);
468
469 return simple_xattr_set_limited(xattrs, &si->xattr_limits,
470 name, value, size, flags);
471}
472
473static const struct xattr_handler sockfs_user_xattr_handler = {
474 .prefix = XATTR_USER_PREFIX,
475 .get = sockfs_user_xattr_get,
476 .set = sockfs_user_xattr_set,
477};
478
479static const struct xattr_handler * const sockfs_xattr_handlers[] = {
480 &sockfs_xattr_handler,
481 &sockfs_security_xattr_handler,
482 &sockfs_user_xattr_handler,
483 NULL
484};
485
486static int sockfs_init_fs_context(struct fs_context *fc)
487{
488 struct pseudo_fs_context *ctx = init_pseudo(fc, SOCKFS_MAGIC);
489 if (!ctx)
490 return -ENOMEM;
491 ctx->ops = &sockfs_ops;
492 ctx->dops = &sockfs_dentry_operations;
493 ctx->xattr = sockfs_xattr_handlers;
494 return 0;
495}
496
497static struct vfsmount *sock_mnt __read_mostly;
498
499static struct file_system_type sock_fs_type = {
500 .name = "sockfs",
501 .init_fs_context = sockfs_init_fs_context,
502 .kill_sb = kill_anon_super,
503};
504
505/*
506 * Obtains the first available file descriptor and sets it up for use.
507 *
508 * These functions create file structures and maps them to fd space
509 * of the current process. On success it returns file descriptor
510 * and file struct implicitly stored in sock->file.
511 * Note that another thread may close file descriptor before we return
512 * from this function. We use the fact that now we do not refer
513 * to socket after mapping. If one day we will need it, this
514 * function will increment ref. count on file by 1.
515 *
516 * In any case returned fd MAY BE not valid!
517 * This race condition is unavoidable
518 * with shared fd spaces, we cannot solve it inside kernel,
519 * but we take care of internal coherence yet.
520 */
521
522/**
523 * sock_alloc_file - Bind a &socket to a &file
524 * @sock: socket
525 * @flags: file status flags
526 * @dname: protocol name
527 *
528 * Returns the &file bound with @sock, implicitly storing it
529 * in sock->file. If dname is %NULL, sets to "".
530 *
531 * On failure @sock is released, and an ERR pointer is returned.
532 *
533 * This function uses GFP_KERNEL internally.
534 */
535
536struct file *sock_alloc_file(struct socket *sock, int flags, const char *dname)
537{
538 struct file *file;
539
540 if (!dname)
541 dname = sock->sk ? sock->sk->sk_prot_creator->name : "";
542
543 file = alloc_file_pseudo(SOCK_INODE(sock), sock_mnt, dname,
544 O_RDWR | (flags & O_NONBLOCK),
545 &socket_file_ops);
546 if (IS_ERR(file)) {
547 sock_release(sock);
548 return file;
549 }
550
551 file->f_mode |= FMODE_NOWAIT;
552 sock->file = file;
553 file->private_data = sock;
554 stream_open(SOCK_INODE(sock), file);
555 /*
556 * Disable permission and pre-content events, but enable legacy
557 * inotify events for legacy users.
558 */
559 file_set_fsnotify_mode(file, FMODE_NONOTIFY_PERM);
560 return file;
561}
562EXPORT_SYMBOL(sock_alloc_file);
563
564static int sock_map_fd(struct socket *sock, int flags)
565{
566 struct file *newfile;
567 int fd = get_unused_fd_flags(flags);
568 if (unlikely(fd < 0)) {
569 sock_release(sock);
570 return fd;
571 }
572
573 newfile = sock_alloc_file(sock, flags, NULL);
574 if (!IS_ERR(newfile)) {
575 fd_install(fd, newfile);
576 return fd;
577 }
578
579 put_unused_fd(fd);
580 return PTR_ERR(newfile);
581}
582
583/**
584 * sock_from_file - Return the &socket bounded to @file.
585 * @file: file
586 *
587 * On failure returns %NULL.
588 */
589
590struct socket *sock_from_file(struct file *file)
591{
592 if (likely(file->f_op == &socket_file_ops))
593 return file->private_data; /* set in sock_alloc_file */
594
595 return NULL;
596}
597EXPORT_SYMBOL(sock_from_file);
598
599/**
600 * sockfd_lookup - Go from a file number to its socket slot
601 * @fd: file handle
602 * @err: pointer to an error code return
603 *
604 * The file handle passed in is locked and the socket it is bound
605 * to is returned. If an error occurs the err pointer is overwritten
606 * with a negative errno code and NULL is returned. The function checks
607 * for both invalid handles and passing a handle which is not a socket.
608 *
609 * On a success the socket object pointer is returned.
610 */
611
612struct socket *sockfd_lookup(int fd, int *err)
613{
614 struct file *file;
615 struct socket *sock;
616
617 file = fget(fd);
618 if (!file) {
619 *err = -EBADF;
620 return NULL;
621 }
622
623 sock = sock_from_file(file);
624 if (!sock) {
625 *err = -ENOTSOCK;
626 fput(file);
627 }
628 return sock;
629}
630EXPORT_SYMBOL(sockfd_lookup);
631
632static ssize_t sockfs_listxattr(struct dentry *dentry, char *buffer,
633 size_t size)
634{
635 struct sockfs_inode *si = SOCKFS_I(d_inode(dentry));
636 ssize_t len, used;
637
638 len = simple_xattr_list(d_inode(dentry), READ_ONCE(si->xattrs),
639 buffer, size);
640 if (len < 0)
641 return len;
642
643 used = len;
644 if (buffer) {
645 buffer += len;
646 size -= len;
647 }
648
649 len = XATTR_NAME_SOCKPROTONAME_LEN + 1;
650 used += len;
651 if (buffer) {
652 if (size < len)
653 return -ERANGE;
654 memcpy(buffer, XATTR_NAME_SOCKPROTONAME, len);
655 }
656
657 return used;
658}
659
660static int sockfs_setattr(struct mnt_idmap *idmap,
661 struct dentry *dentry, struct iattr *iattr)
662{
663 int err = simple_setattr(&nop_mnt_idmap, dentry, iattr);
664
665 if (!err && (iattr->ia_valid & ATTR_UID)) {
666 struct socket *sock = SOCKET_I(d_inode(dentry));
667
668 if (sock->sk) {
669 /* Paired with READ_ONCE() in sk_uid() */
670 WRITE_ONCE(sock->sk->sk_uid, iattr->ia_uid);
671 } else {
672 err = -ENOENT;
673 }
674 }
675
676 return err;
677}
678
679static const struct inode_operations sockfs_inode_ops = {
680 .listxattr = sockfs_listxattr,
681 .setattr = sockfs_setattr,
682};
683
684/**
685 * sock_alloc - allocate a socket
686 *
687 * Allocate a new inode and socket object. The two are bound together
688 * and initialised. The socket is then returned. If we are out of inodes
689 * NULL is returned. This functions uses GFP_KERNEL internally.
690 */
691
692struct socket *sock_alloc(void)
693{
694 struct inode *inode;
695 struct socket *sock;
696
697 inode = new_inode_pseudo(sock_mnt->mnt_sb);
698 if (!inode)
699 return NULL;
700
701 sock = SOCKET_I(inode);
702
703 inode->i_ino = get_next_ino();
704 inode->i_mode = S_IFSOCK | S_IRWXUGO;
705 inode->i_uid = current_fsuid();
706 inode->i_gid = current_fsgid();
707 inode->i_op = &sockfs_inode_ops;
708
709 return sock;
710}
711EXPORT_SYMBOL(sock_alloc);
712
713static void __sock_release(struct socket *sock, struct inode *inode)
714{
715 const struct proto_ops *ops = READ_ONCE(sock->ops);
716
717 if (ops) {
718 struct module *owner = ops->owner;
719
720 if (inode)
721 inode_lock(inode);
722 ops->release(sock);
723 sock->sk = NULL;
724 if (inode)
725 inode_unlock(inode);
726 sock->ops = NULL;
727 module_put(owner);
728 }
729
730 if (sock->wq.fasync_list)
731 pr_err("%s: fasync list not empty!\n", __func__);
732
733 if (!sock->file) {
734 iput(SOCK_INODE(sock));
735 return;
736 }
737 WRITE_ONCE(sock->file, NULL);
738}
739
740/**
741 * sock_release - close a socket
742 * @sock: socket to close
743 *
744 * The socket is released from the protocol stack if it has a release
745 * callback, and the inode is then released if the socket is bound to
746 * an inode not a file.
747 */
748void sock_release(struct socket *sock)
749{
750 __sock_release(sock, NULL);
751}
752EXPORT_SYMBOL(sock_release);
753
754void __sock_tx_timestamp(__u32 tsflags, __u8 *tx_flags)
755{
756 u8 flags = *tx_flags;
757
758 if (tsflags & SOF_TIMESTAMPING_TX_HARDWARE)
759 flags |= SKBTX_HW_TSTAMP_NOBPF;
760
761 if (tsflags & SOF_TIMESTAMPING_TX_SOFTWARE)
762 flags |= SKBTX_SW_TSTAMP;
763
764 if (tsflags & SOF_TIMESTAMPING_TX_SCHED)
765 flags |= SKBTX_SCHED_TSTAMP;
766
767 if (tsflags & SOF_TIMESTAMPING_TX_COMPLETION)
768 flags |= SKBTX_COMPLETION_TSTAMP;
769
770 *tx_flags = flags;
771}
772EXPORT_SYMBOL(__sock_tx_timestamp);
773
774INDIRECT_CALLABLE_DECLARE(int inet_sendmsg(struct socket *, struct msghdr *,
775 size_t));
776INDIRECT_CALLABLE_DECLARE(int inet6_sendmsg(struct socket *, struct msghdr *,
777 size_t));
778
779static noinline void call_trace_sock_send_length(struct sock *sk, int ret,
780 int flags)
781{
782 trace_sock_send_length(sk, ret, 0);
783}
784
785static inline int sock_sendmsg_nosec(struct socket *sock, struct msghdr *msg)
786{
787 int ret = INDIRECT_CALL_INET(READ_ONCE(sock->ops)->sendmsg, inet6_sendmsg,
788 inet_sendmsg, sock, msg,
789 msg_data_left(msg));
790 BUG_ON(ret == -EIOCBQUEUED);
791
792 if (trace_sock_send_length_enabled())
793 call_trace_sock_send_length(sock->sk, ret, 0);
794 return ret;
795}
796
797static int __sock_sendmsg(struct socket *sock, struct msghdr *msg)
798{
799 int err = security_socket_sendmsg(sock, msg,
800 msg_data_left(msg));
801
802 return err ?: sock_sendmsg_nosec(sock, msg);
803}
804
805/**
806 * sock_sendmsg - send a message through @sock
807 * @sock: socket
808 * @msg: message to send
809 *
810 * Sends @msg through @sock, passing through LSM.
811 * Returns the number of bytes sent, or an error code.
812 */
813int sock_sendmsg(struct socket *sock, struct msghdr *msg)
814{
815 struct sockaddr_storage *save_addr = (struct sockaddr_storage *)msg->msg_name;
816 struct sockaddr_storage address;
817 int save_len = msg->msg_namelen;
818 int ret;
819
820 if (msg->msg_name) {
821 memcpy(&address, msg->msg_name, msg->msg_namelen);
822 msg->msg_name = &address;
823 }
824
825 ret = __sock_sendmsg(sock, msg);
826 msg->msg_name = save_addr;
827 msg->msg_namelen = save_len;
828
829 return ret;
830}
831EXPORT_SYMBOL(sock_sendmsg);
832
833/**
834 * kernel_sendmsg - send a message through @sock (kernel-space)
835 * @sock: socket
836 * @msg: message header
837 * @vec: kernel vec
838 * @num: vec array length
839 * @size: total message data size
840 *
841 * Builds the message data with @vec and sends it through @sock.
842 * Returns the number of bytes sent, or an error code.
843 */
844
845int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
846 struct kvec *vec, size_t num, size_t size)
847{
848 iov_iter_kvec(&msg->msg_iter, ITER_SOURCE, vec, num, size);
849 return sock_sendmsg(sock, msg);
850}
851EXPORT_SYMBOL(kernel_sendmsg);
852
853static bool skb_is_err_queue(const struct sk_buff *skb)
854{
855 /* Error-queue skbs are marked as PACKET_OUTGOING in
856 * skb_set_err_queue() and use the destructor installed by
857 * sock_queue_err_skb(). PACKET_OUTGOING alone is not unique:
858 * AF_PACKET outgoing taps use the same pkt_type.
859 */
860 return skb->pkt_type == PACKET_OUTGOING &&
861 skb->destructor == sock_rmem_free;
862}
863
864/* On transmit, software and hardware timestamps are returned independently.
865 * As the two skb clones share the hardware timestamp, which may be updated
866 * before the software timestamp is received, a hardware TX timestamp may be
867 * returned only if there is no software TX timestamp. Ignore false software
868 * timestamps, which may be made in the __sock_recv_timestamp() call when the
869 * option SO_TIMESTAMP_OLD(NS) is enabled on the socket, even when the skb has a
870 * hardware timestamp.
871 */
872static bool skb_is_swtx_tstamp(const struct sk_buff *skb, int false_tstamp)
873{
874 return skb->tstamp && !false_tstamp && skb_is_err_queue(skb);
875}
876
877static ktime_t get_timestamp(struct sock *sk, struct sk_buff *skb, int *if_index)
878{
879 bool cycles = READ_ONCE(sk->sk_tsflags) & SOF_TIMESTAMPING_BIND_PHC;
880 struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
881 struct net_device *orig_dev;
882 ktime_t hwtstamp;
883
884 rcu_read_lock();
885 orig_dev = dev_get_by_napi_id(skb_napi_id(skb));
886 if (orig_dev) {
887 *if_index = orig_dev->ifindex;
888 hwtstamp = netdev_get_tstamp(orig_dev, shhwtstamps, cycles);
889 } else {
890 hwtstamp = shhwtstamps->hwtstamp;
891 }
892 rcu_read_unlock();
893
894 return hwtstamp;
895}
896
897static void put_ts_pktinfo(struct msghdr *msg, struct sk_buff *skb,
898 int if_index)
899{
900 struct scm_ts_pktinfo ts_pktinfo;
901 struct net_device *orig_dev;
902
903 if (!skb_mac_header_was_set(skb))
904 return;
905
906 memset(&ts_pktinfo, 0, sizeof(ts_pktinfo));
907
908 if (!if_index) {
909 rcu_read_lock();
910 orig_dev = dev_get_by_napi_id(skb_napi_id(skb));
911 if (orig_dev)
912 if_index = orig_dev->ifindex;
913 rcu_read_unlock();
914 }
915 ts_pktinfo.if_index = if_index;
916
917 ts_pktinfo.pkt_length = skb->len - skb_mac_offset(skb);
918 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPING_PKTINFO,
919 sizeof(ts_pktinfo), &ts_pktinfo);
920}
921
922bool skb_has_tx_timestamp(struct sk_buff *skb, const struct sock *sk)
923{
924 const struct sock_exterr_skb *serr = SKB_EXT_ERR(skb);
925 u32 tsflags = READ_ONCE(sk->sk_tsflags);
926
927 if (serr->ee.ee_errno != ENOMSG ||
928 serr->ee.ee_origin != SO_EE_ORIGIN_TIMESTAMPING)
929 return false;
930
931 /* software time stamp available and wanted */
932 if ((tsflags & SOF_TIMESTAMPING_SOFTWARE) && skb->tstamp)
933 return true;
934 /* hardware time stamps available and wanted */
935 return (tsflags & SOF_TIMESTAMPING_RAW_HARDWARE) &&
936 skb_hwtstamps(skb)->hwtstamp;
937}
938
939int skb_get_tx_timestamp(struct sk_buff *skb, struct sock *sk,
940 struct timespec64 *ts)
941{
942 u32 tsflags = READ_ONCE(sk->sk_tsflags);
943 ktime_t hwtstamp;
944 int if_index = 0;
945
946 if ((tsflags & SOF_TIMESTAMPING_SOFTWARE) &&
947 ktime_to_timespec64_cond(skb->tstamp, ts))
948 return SOF_TIMESTAMPING_TX_SOFTWARE;
949
950 if (!(tsflags & SOF_TIMESTAMPING_RAW_HARDWARE) ||
951 skb_is_swtx_tstamp(skb, false))
952 return -ENOENT;
953
954 if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP_NETDEV)
955 hwtstamp = get_timestamp(sk, skb, &if_index);
956 else
957 hwtstamp = skb_hwtstamps(skb)->hwtstamp;
958
959 if (tsflags & SOF_TIMESTAMPING_BIND_PHC)
960 hwtstamp = ptp_convert_timestamp(&hwtstamp,
961 READ_ONCE(sk->sk_bind_phc));
962 if (!ktime_to_timespec64_cond(hwtstamp, ts))
963 return -ENOENT;
964
965 return SOF_TIMESTAMPING_TX_HARDWARE;
966}
967
968/*
969 * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
970 */
971void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
972 struct sk_buff *skb)
973{
974 int need_software_tstamp = sock_flag(sk, SOCK_RCVTSTAMP);
975 int new_tstamp = sock_flag(sk, SOCK_TSTAMP_NEW);
976 struct skb_shared_hwtstamps *shhwtstamps =
977 skb_hwtstamps(skb);
978 struct scm_timestamping_internal tss;
979 int if_index, false_tstamp = 0;
980 ktime_t hwtstamp;
981 u32 tsflags;
982
983 /* Race occurred between timestamp enabling and packet
984 receiving. Fill in the current time for now. */
985 if (need_software_tstamp && skb->tstamp == 0) {
986 __net_timestamp(skb);
987 false_tstamp = 1;
988 }
989
990 if (need_software_tstamp) {
991 if (!sock_flag(sk, SOCK_RCVTSTAMPNS)) {
992 if (new_tstamp) {
993 struct __kernel_sock_timeval tv;
994
995 skb_get_new_timestamp(skb, &tv);
996 put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMP_NEW,
997 sizeof(tv), &tv);
998 } else {
999 struct __kernel_old_timeval tv;
1000
1001 skb_get_timestamp(skb, &tv);
1002 put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMP_OLD,
1003 sizeof(tv), &tv);
1004 }
1005 } else {
1006 if (new_tstamp) {
1007 struct __kernel_timespec ts;
1008
1009 skb_get_new_timestampns(skb, &ts);
1010 put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMPNS_NEW,
1011 sizeof(ts), &ts);
1012 } else {
1013 struct __kernel_old_timespec ts;
1014
1015 skb_get_timestampns(skb, &ts);
1016 put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMPNS_OLD,
1017 sizeof(ts), &ts);
1018 }
1019 }
1020 }
1021
1022 memset(&tss, 0, sizeof(tss));
1023 tsflags = READ_ONCE(sk->sk_tsflags);
1024 if (tsflags & SOF_TIMESTAMPING_SOFTWARE &&
1025 (tsflags & SOF_TIMESTAMPING_RX_SOFTWARE ||
1026 skb_is_err_queue(skb) ||
1027 !(tsflags & SOF_TIMESTAMPING_OPT_RX_FILTER)))
1028 tss.ts[0] = skb->tstamp;
1029
1030 if (shhwtstamps &&
1031 (tsflags & SOF_TIMESTAMPING_RAW_HARDWARE &&
1032 (tsflags & SOF_TIMESTAMPING_RX_HARDWARE ||
1033 skb_is_err_queue(skb) ||
1034 !(tsflags & SOF_TIMESTAMPING_OPT_RX_FILTER))) &&
1035 !skb_is_swtx_tstamp(skb, false_tstamp)) {
1036 if_index = 0;
1037 if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP_NETDEV)
1038 hwtstamp = get_timestamp(sk, skb, &if_index);
1039 else
1040 hwtstamp = shhwtstamps->hwtstamp;
1041
1042 if (tsflags & SOF_TIMESTAMPING_BIND_PHC)
1043 hwtstamp = ptp_convert_timestamp(&hwtstamp,
1044 READ_ONCE(sk->sk_bind_phc));
1045
1046 if (hwtstamp) {
1047 tss.ts[2] = hwtstamp;
1048
1049 if ((tsflags & SOF_TIMESTAMPING_OPT_PKTINFO) &&
1050 !skb_is_err_queue(skb))
1051 put_ts_pktinfo(msg, skb, if_index);
1052 }
1053 }
1054 if (tss.ts[0] | tss.ts[2]) {
1055 if (sock_flag(sk, SOCK_TSTAMP_NEW))
1056 put_cmsg_scm_timestamping64(msg, &tss);
1057 else
1058 put_cmsg_scm_timestamping(msg, &tss);
1059
1060 if (skb_is_err_queue(skb) && skb->len &&
1061 SKB_EXT_ERR(skb)->opt_stats)
1062 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPING_OPT_STATS,
1063 skb->len, skb->data);
1064 }
1065}
1066EXPORT_SYMBOL_GPL(__sock_recv_timestamp);
1067
1068#ifdef CONFIG_WIRELESS
1069void __sock_recv_wifi_status(struct msghdr *msg, struct sock *sk,
1070 struct sk_buff *skb)
1071{
1072 int ack;
1073
1074 if (!sock_flag(sk, SOCK_WIFI_STATUS))
1075 return;
1076 if (!skb->wifi_acked_valid)
1077 return;
1078
1079 ack = skb->wifi_acked;
1080
1081 put_cmsg(msg, SOL_SOCKET, SCM_WIFI_STATUS, sizeof(ack), &ack);
1082}
1083EXPORT_SYMBOL_GPL(__sock_recv_wifi_status);
1084#endif
1085
1086static inline void sock_recv_drops(struct msghdr *msg, struct sock *sk,
1087 struct sk_buff *skb)
1088{
1089 if (sock_flag(sk, SOCK_RXQ_OVFL) && skb && SOCK_SKB_CB(skb)->dropcount)
1090 put_cmsg(msg, SOL_SOCKET, SO_RXQ_OVFL,
1091 sizeof(__u32), &SOCK_SKB_CB(skb)->dropcount);
1092}
1093
1094static void sock_recv_mark(struct msghdr *msg, struct sock *sk,
1095 struct sk_buff *skb)
1096{
1097 if (sock_flag(sk, SOCK_RCVMARK) && skb) {
1098 /* We must use a bounce buffer for CONFIG_HARDENED_USERCOPY=y */
1099 __u32 mark = skb->mark;
1100
1101 put_cmsg(msg, SOL_SOCKET, SO_MARK, sizeof(__u32), &mark);
1102 }
1103}
1104
1105static void sock_recv_priority(struct msghdr *msg, struct sock *sk,
1106 struct sk_buff *skb)
1107{
1108 if (sock_flag(sk, SOCK_RCVPRIORITY) && skb) {
1109 __u32 priority = skb->priority;
1110
1111 put_cmsg(msg, SOL_SOCKET, SO_PRIORITY, sizeof(__u32), &priority);
1112 }
1113}
1114
1115void __sock_recv_cmsgs(struct msghdr *msg, struct sock *sk,
1116 struct sk_buff *skb)
1117{
1118 sock_recv_timestamp(msg, sk, skb);
1119 sock_recv_drops(msg, sk, skb);
1120 sock_recv_mark(msg, sk, skb);
1121 sock_recv_priority(msg, sk, skb);
1122}
1123EXPORT_SYMBOL_GPL(__sock_recv_cmsgs);
1124
1125INDIRECT_CALLABLE_DECLARE(int inet_recvmsg(struct socket *, struct msghdr *,
1126 size_t, int));
1127INDIRECT_CALLABLE_DECLARE(int inet6_recvmsg(struct socket *, struct msghdr *,
1128 size_t, int));
1129
1130static noinline void call_trace_sock_recv_length(struct sock *sk, int ret, int flags)
1131{
1132 trace_sock_recv_length(sk, ret, flags);
1133}
1134
1135static inline int sock_recvmsg_nosec(struct socket *sock, struct msghdr *msg,
1136 int flags)
1137{
1138 int ret = INDIRECT_CALL_INET(READ_ONCE(sock->ops)->recvmsg,
1139 inet6_recvmsg,
1140 inet_recvmsg, sock, msg,
1141 msg_data_left(msg), flags);
1142 if (trace_sock_recv_length_enabled())
1143 call_trace_sock_recv_length(sock->sk, ret, flags);
1144 return ret;
1145}
1146
1147/**
1148 * sock_recvmsg - receive a message from @sock
1149 * @sock: socket
1150 * @msg: message to receive
1151 * @flags: message flags
1152 *
1153 * Receives @msg from @sock, passing through LSM. Returns the total number
1154 * of bytes received, or an error.
1155 */
1156int sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags)
1157{
1158 int err = security_socket_recvmsg(sock, msg, msg_data_left(msg), flags);
1159
1160 return err ?: sock_recvmsg_nosec(sock, msg, flags);
1161}
1162EXPORT_SYMBOL(sock_recvmsg);
1163
1164/**
1165 * kernel_recvmsg - Receive a message from a socket (kernel space)
1166 * @sock: The socket to receive the message from
1167 * @msg: Received message
1168 * @vec: Input s/g array for message data
1169 * @num: Size of input s/g array
1170 * @size: Number of bytes to read
1171 * @flags: Message flags (MSG_DONTWAIT, etc...)
1172 *
1173 * On return the msg structure contains the scatter/gather array passed in the
1174 * vec argument. The array is modified so that it consists of the unfilled
1175 * portion of the original array.
1176 *
1177 * The returned value is the total number of bytes received, or an error.
1178 */
1179
1180int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
1181 struct kvec *vec, size_t num, size_t size, int flags)
1182{
1183 msg->msg_control_is_user = false;
1184 iov_iter_kvec(&msg->msg_iter, ITER_DEST, vec, num, size);
1185 return sock_recvmsg(sock, msg, flags);
1186}
1187EXPORT_SYMBOL(kernel_recvmsg);
1188
1189static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
1190 struct pipe_inode_info *pipe, size_t len,
1191 unsigned int flags)
1192{
1193 struct socket *sock = file->private_data;
1194 const struct proto_ops *ops;
1195
1196 ops = READ_ONCE(sock->ops);
1197 if (unlikely(!ops->splice_read))
1198 return copy_splice_read(file, ppos, pipe, len, flags);
1199
1200 return ops->splice_read(sock, ppos, pipe, len, flags);
1201}
1202
1203static void sock_splice_eof(struct file *file)
1204{
1205 struct socket *sock = file->private_data;
1206 const struct proto_ops *ops;
1207
1208 ops = READ_ONCE(sock->ops);
1209 if (ops->splice_eof)
1210 ops->splice_eof(sock);
1211}
1212
1213static ssize_t sock_read_iter(struct kiocb *iocb, struct iov_iter *to)
1214{
1215 struct file *file = iocb->ki_filp;
1216 struct socket *sock = file->private_data;
1217 struct msghdr msg = {.msg_iter = *to,
1218 .msg_iocb = iocb};
1219 ssize_t res;
1220
1221 if (file->f_flags & O_NONBLOCK || (iocb->ki_flags & IOCB_NOWAIT))
1222 msg.msg_flags = MSG_DONTWAIT;
1223
1224 if (iocb->ki_pos != 0)
1225 return -ESPIPE;
1226
1227 if (!iov_iter_count(to)) /* Match SYS5 behaviour */
1228 return 0;
1229
1230 res = sock_recvmsg(sock, &msg, msg.msg_flags);
1231 *to = msg.msg_iter;
1232 return res;
1233}
1234
1235static ssize_t sock_write_iter(struct kiocb *iocb, struct iov_iter *from)
1236{
1237 struct file *file = iocb->ki_filp;
1238 struct socket *sock = file->private_data;
1239 struct msghdr msg = {.msg_iter = *from,
1240 .msg_iocb = iocb};
1241 ssize_t res;
1242
1243 if (iocb->ki_pos != 0)
1244 return -ESPIPE;
1245
1246 if (file->f_flags & O_NONBLOCK || (iocb->ki_flags & IOCB_NOWAIT))
1247 msg.msg_flags = MSG_DONTWAIT;
1248
1249 if (sock->type == SOCK_SEQPACKET)
1250 msg.msg_flags |= MSG_EOR;
1251
1252 if (iocb->ki_flags & IOCB_NOSIGNAL)
1253 msg.msg_flags |= MSG_NOSIGNAL;
1254
1255 res = __sock_sendmsg(sock, &msg);
1256 *from = msg.msg_iter;
1257 return res;
1258}
1259
1260/*
1261 * Atomic setting of ioctl hooks to avoid race
1262 * with module unload.
1263 */
1264
1265static DEFINE_MUTEX(br_ioctl_mutex);
1266static int (*br_ioctl_hook)(struct net *net, unsigned int cmd,
1267 void __user *uarg);
1268
1269void brioctl_set(int (*hook)(struct net *net, unsigned int cmd,
1270 void __user *uarg))
1271{
1272 mutex_lock(&br_ioctl_mutex);
1273 br_ioctl_hook = hook;
1274 mutex_unlock(&br_ioctl_mutex);
1275}
1276EXPORT_SYMBOL(brioctl_set);
1277
1278int br_ioctl_call(struct net *net, unsigned int cmd, void __user *uarg)
1279{
1280 int err = -ENOPKG;
1281
1282 if (!br_ioctl_hook)
1283 request_module("bridge");
1284
1285 mutex_lock(&br_ioctl_mutex);
1286 if (br_ioctl_hook)
1287 err = br_ioctl_hook(net, cmd, uarg);
1288 mutex_unlock(&br_ioctl_mutex);
1289
1290 return err;
1291}
1292
1293static DEFINE_MUTEX(vlan_ioctl_mutex);
1294static int (*vlan_ioctl_hook) (struct net *, void __user *arg);
1295
1296void vlan_ioctl_set(int (*hook) (struct net *, void __user *))
1297{
1298 mutex_lock(&vlan_ioctl_mutex);
1299 vlan_ioctl_hook = hook;
1300 mutex_unlock(&vlan_ioctl_mutex);
1301}
1302EXPORT_SYMBOL(vlan_ioctl_set);
1303
1304static long sock_do_ioctl(struct net *net, struct socket *sock,
1305 unsigned int cmd, unsigned long arg)
1306{
1307 const struct proto_ops *ops = READ_ONCE(sock->ops);
1308 struct ifreq ifr;
1309 bool need_copyout;
1310 int err;
1311 void __user *argp = (void __user *)arg;
1312 void __user *data;
1313
1314 err = ops->ioctl(sock, cmd, arg);
1315
1316 /*
1317 * If this ioctl is unknown try to hand it down
1318 * to the NIC driver.
1319 */
1320 if (err != -ENOIOCTLCMD)
1321 return err;
1322
1323 if (!is_socket_ioctl_cmd(cmd))
1324 return -ENOTTY;
1325
1326 if (get_user_ifreq(&ifr, &data, argp))
1327 return -EFAULT;
1328 err = dev_ioctl(net, cmd, &ifr, data, &need_copyout);
1329 if (!err && need_copyout)
1330 if (put_user_ifreq(&ifr, argp))
1331 return -EFAULT;
1332
1333 return err;
1334}
1335
1336/*
1337 * With an ioctl, arg may well be a user mode pointer, but we don't know
1338 * what to do with it - that's up to the protocol still.
1339 */
1340
1341static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
1342{
1343 const struct proto_ops *ops;
1344 struct socket *sock;
1345 struct sock *sk;
1346 void __user *argp = (void __user *)arg;
1347 int pid, err;
1348 struct net *net;
1349
1350 sock = file->private_data;
1351 ops = READ_ONCE(sock->ops);
1352 sk = sock->sk;
1353 net = sock_net(sk);
1354 if (unlikely(cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15))) {
1355 struct ifreq ifr;
1356 void __user *data;
1357 bool need_copyout;
1358 if (get_user_ifreq(&ifr, &data, argp))
1359 return -EFAULT;
1360 err = dev_ioctl(net, cmd, &ifr, data, &need_copyout);
1361 if (!err && need_copyout)
1362 if (put_user_ifreq(&ifr, argp))
1363 return -EFAULT;
1364 } else
1365#ifdef CONFIG_WEXT_CORE
1366 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
1367 err = wext_handle_ioctl(net, cmd, argp);
1368 } else
1369#endif
1370 switch (cmd) {
1371 case FIOSETOWN:
1372 case SIOCSPGRP:
1373 err = -EFAULT;
1374 if (get_user(pid, (int __user *)argp))
1375 break;
1376 err = f_setown(sock->file, pid, 1);
1377 break;
1378 case FIOGETOWN:
1379 case SIOCGPGRP:
1380 err = put_user(f_getown(sock->file),
1381 (int __user *)argp);
1382 break;
1383 case SIOCGIFBR:
1384 case SIOCSIFBR:
1385 case SIOCBRADDBR:
1386 case SIOCBRDELBR:
1387 case SIOCBRADDIF:
1388 case SIOCBRDELIF:
1389 err = br_ioctl_call(net, cmd, argp);
1390 break;
1391 case SIOCGIFVLAN:
1392 case SIOCSIFVLAN:
1393 err = -ENOPKG;
1394 if (!vlan_ioctl_hook)
1395 request_module("8021q");
1396
1397 mutex_lock(&vlan_ioctl_mutex);
1398 if (vlan_ioctl_hook)
1399 err = vlan_ioctl_hook(net, argp);
1400 mutex_unlock(&vlan_ioctl_mutex);
1401 break;
1402 case SIOCGSKNS:
1403 err = -EPERM;
1404 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1405 break;
1406
1407 err = open_related_ns(&net->ns, get_net_ns);
1408 break;
1409 case SIOCGSTAMP_OLD:
1410 case SIOCGSTAMPNS_OLD:
1411 if (!ops->gettstamp) {
1412 err = -ENOIOCTLCMD;
1413 break;
1414 }
1415 err = ops->gettstamp(sock, argp,
1416 cmd == SIOCGSTAMP_OLD,
1417 !IS_ENABLED(CONFIG_64BIT));
1418 break;
1419 case SIOCGSTAMP_NEW:
1420 case SIOCGSTAMPNS_NEW:
1421 if (!ops->gettstamp) {
1422 err = -ENOIOCTLCMD;
1423 break;
1424 }
1425 err = ops->gettstamp(sock, argp,
1426 cmd == SIOCGSTAMP_NEW,
1427 false);
1428 break;
1429
1430 case SIOCGIFCONF:
1431 err = dev_ifconf(net, argp);
1432 break;
1433
1434 default:
1435 err = sock_do_ioctl(net, sock, cmd, arg);
1436 break;
1437 }
1438 return err;
1439}
1440
1441/**
1442 * sock_create_lite - creates a socket
1443 * @family: protocol family (AF_INET, ...)
1444 * @type: communication type (SOCK_STREAM, ...)
1445 * @protocol: protocol (0, ...)
1446 * @res: new socket
1447 *
1448 * Creates a new socket and assigns it to @res, passing through LSM.
1449 * The new socket initialization is not complete, see kernel_accept().
1450 * Returns 0 or an error. On failure @res is set to %NULL.
1451 * This function internally uses GFP_KERNEL.
1452 */
1453
1454int sock_create_lite(int family, int type, int protocol, struct socket **res)
1455{
1456 int err;
1457 struct socket *sock = NULL;
1458
1459 err = security_socket_create(family, type, protocol, 1);
1460 if (err)
1461 goto out;
1462
1463 sock = sock_alloc();
1464 if (!sock) {
1465 err = -ENOMEM;
1466 goto out;
1467 }
1468
1469 sock->type = type;
1470 err = security_socket_post_create(sock, family, type, protocol, 1);
1471 if (err)
1472 goto out_release;
1473
1474out:
1475 *res = sock;
1476 return err;
1477out_release:
1478 sock_release(sock);
1479 sock = NULL;
1480 goto out;
1481}
1482EXPORT_SYMBOL(sock_create_lite);
1483
1484/* No kernel lock held - perfect */
1485static __poll_t sock_poll(struct file *file, poll_table *wait)
1486{
1487 struct socket *sock = file->private_data;
1488 const struct proto_ops *ops = READ_ONCE(sock->ops);
1489 __poll_t events = poll_requested_events(wait), flag = 0;
1490
1491 if (!ops->poll)
1492 return 0;
1493
1494 if (sk_can_busy_loop(sock->sk)) {
1495 /* poll once if requested by the syscall */
1496 if (events & POLL_BUSY_LOOP)
1497 sk_busy_loop(sock->sk, 1);
1498
1499 /* if this socket can poll_ll, tell the system call */
1500 flag = POLL_BUSY_LOOP;
1501 }
1502
1503 return ops->poll(file, sock, wait) | flag;
1504}
1505
1506static int sock_mmap(struct file *file, struct vm_area_struct *vma)
1507{
1508 struct socket *sock = file->private_data;
1509
1510 return READ_ONCE(sock->ops)->mmap(file, sock, vma);
1511}
1512
1513static int sock_close(struct inode *inode, struct file *filp)
1514{
1515 __sock_release(SOCKET_I(inode), inode);
1516 return 0;
1517}
1518
1519/*
1520 * Update the socket async list
1521 *
1522 * Fasync_list locking strategy.
1523 *
1524 * 1. fasync_list is modified only under process context socket lock
1525 * i.e. under semaphore.
1526 * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
1527 * or under socket lock
1528 */
1529
1530static int sock_fasync(int fd, struct file *filp, int on)
1531{
1532 struct socket *sock = filp->private_data;
1533 struct sock *sk = sock->sk;
1534 struct socket_wq *wq = &sock->wq;
1535
1536 if (sk == NULL)
1537 return -EINVAL;
1538
1539 lock_sock(sk);
1540 fasync_helper(fd, filp, on, &wq->fasync_list);
1541
1542 if (!wq->fasync_list)
1543 sock_reset_flag(sk, SOCK_FASYNC);
1544 else
1545 sock_set_flag(sk, SOCK_FASYNC);
1546
1547 release_sock(sk);
1548 return 0;
1549}
1550
1551/* This function may be called only under rcu_lock */
1552
1553int sock_wake_async(struct socket_wq *wq, int how, int band)
1554{
1555 if (!wq || !wq->fasync_list)
1556 return -1;
1557
1558 switch (how) {
1559 case SOCK_WAKE_WAITD:
1560 if (test_bit(SOCKWQ_ASYNC_WAITDATA, &wq->flags))
1561 break;
1562 goto call_kill;
1563 case SOCK_WAKE_SPACE:
1564 if (!test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags))
1565 break;
1566 fallthrough;
1567 case SOCK_WAKE_IO:
1568call_kill:
1569 kill_fasync(&wq->fasync_list, SIGIO, band);
1570 break;
1571 case SOCK_WAKE_URG:
1572 kill_fasync(&wq->fasync_list, SIGURG, band);
1573 }
1574
1575 return 0;
1576}
1577EXPORT_SYMBOL(sock_wake_async);
1578
1579/**
1580 * __sock_create - creates a socket
1581 * @net: net namespace
1582 * @family: protocol family (AF_INET, ...)
1583 * @type: communication type (SOCK_STREAM, ...)
1584 * @protocol: protocol (0, ...)
1585 * @res: new socket
1586 * @kern: boolean for kernel space sockets
1587 *
1588 * Creates a new socket and assigns it to @res, passing through LSM.
1589 * Returns 0 or an error. On failure @res is set to %NULL. @kern must
1590 * be set to true if the socket resides in kernel space.
1591 * This function internally uses GFP_KERNEL.
1592 */
1593
1594int __sock_create(struct net *net, int family, int type, int protocol,
1595 struct socket **res, int kern)
1596{
1597 int err;
1598 struct socket *sock;
1599 const struct net_proto_family *pf;
1600
1601 /*
1602 * Check protocol is in range
1603 */
1604 if (family < 0 || family >= NPROTO)
1605 return -EAFNOSUPPORT;
1606 if (type < 0 || type >= SOCK_MAX)
1607 return -EINVAL;
1608
1609 /* Compatibility.
1610
1611 This uglymoron is moved from INET layer to here to avoid
1612 deadlock in module load.
1613 */
1614 if (family == PF_INET && type == SOCK_PACKET) {
1615 pr_info_once("%s uses obsolete (PF_INET,SOCK_PACKET)\n",
1616 current->comm);
1617 family = PF_PACKET;
1618 }
1619
1620 err = security_socket_create(family, type, protocol, kern);
1621 if (err)
1622 return err;
1623
1624 /*
1625 * Allocate the socket and allow the family to set things up. if
1626 * the protocol is 0, the family is instructed to select an appropriate
1627 * default.
1628 */
1629 sock = sock_alloc();
1630 if (!sock) {
1631 net_warn_ratelimited("socket: no more sockets\n");
1632 return -ENFILE; /* Not exactly a match, but its the
1633 closest posix thing */
1634 }
1635
1636 sock->type = type;
1637
1638#ifdef CONFIG_MODULES
1639 /* Attempt to load a protocol module if the find failed.
1640 *
1641 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
1642 * requested real, full-featured networking support upon configuration.
1643 * Otherwise module support will break!
1644 */
1645 if (rcu_access_pointer(net_families[family]) == NULL)
1646 request_module("net-pf-%d", family);
1647#endif
1648
1649 rcu_read_lock();
1650 pf = rcu_dereference(net_families[family]);
1651 err = -EAFNOSUPPORT;
1652 if (!pf)
1653 goto out_release;
1654
1655 /*
1656 * We will call the ->create function, that possibly is in a loadable
1657 * module, so we have to bump that loadable module refcnt first.
1658 */
1659 if (!try_module_get(pf->owner))
1660 goto out_release;
1661
1662 /* Now protected by module ref count */
1663 rcu_read_unlock();
1664
1665 err = pf->create(net, sock, protocol, kern);
1666 if (err < 0) {
1667 /* ->create should release the allocated sock->sk object on error
1668 * and make sure sock->sk is set to NULL to avoid use-after-free
1669 */
1670 DEBUG_NET_WARN_ONCE(sock->sk,
1671 "%ps must clear sock->sk on failure, family: %d, type: %d, protocol: %d\n",
1672 pf->create, family, type, protocol);
1673 goto out_module_put;
1674 }
1675
1676 /*
1677 * Now to bump the refcnt of the [loadable] module that owns this
1678 * socket at sock_release time we decrement its refcnt.
1679 */
1680 if (!try_module_get(sock->ops->owner))
1681 goto out_module_busy;
1682
1683 /*
1684 * Now that we're done with the ->create function, the [loadable]
1685 * module can have its refcnt decremented
1686 */
1687 module_put(pf->owner);
1688 err = security_socket_post_create(sock, family, type, protocol, kern);
1689 if (err)
1690 goto out_sock_release;
1691 *res = sock;
1692
1693 return 0;
1694
1695out_module_busy:
1696 err = -EAFNOSUPPORT;
1697out_module_put:
1698 sock->ops = NULL;
1699 module_put(pf->owner);
1700out_sock_release:
1701 sock_release(sock);
1702 return err;
1703
1704out_release:
1705 rcu_read_unlock();
1706 goto out_sock_release;
1707}
1708EXPORT_SYMBOL(__sock_create);
1709
1710/**
1711 * sock_create - creates a socket
1712 * @family: protocol family (AF_INET, ...)
1713 * @type: communication type (SOCK_STREAM, ...)
1714 * @protocol: protocol (0, ...)
1715 * @res: new socket
1716 *
1717 * A wrapper around __sock_create().
1718 * Returns 0 or an error. This function internally uses GFP_KERNEL.
1719 */
1720
1721int sock_create(int family, int type, int protocol, struct socket **res)
1722{
1723 return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0);
1724}
1725EXPORT_SYMBOL(sock_create);
1726
1727/**
1728 * sock_create_kern - creates a socket (kernel space)
1729 * @net: net namespace
1730 * @family: protocol family (AF_INET, ...)
1731 * @type: communication type (SOCK_STREAM, ...)
1732 * @protocol: protocol (0, ...)
1733 * @res: new socket
1734 *
1735 * A wrapper around __sock_create().
1736 * Returns 0 or an error. This function internally uses GFP_KERNEL.
1737 */
1738
1739int sock_create_kern(struct net *net, int family, int type, int protocol, struct socket **res)
1740{
1741 return __sock_create(net, family, type, protocol, res, 1);
1742}
1743EXPORT_SYMBOL(sock_create_kern);
1744
1745static struct socket *__sys_socket_create(int family, int type, int protocol)
1746{
1747 struct socket *sock;
1748 int retval;
1749
1750 /* Check the SOCK_* constants for consistency. */
1751 BUILD_BUG_ON(SOCK_CLOEXEC != O_CLOEXEC);
1752 BUILD_BUG_ON((SOCK_MAX | SOCK_TYPE_MASK) != SOCK_TYPE_MASK);
1753 BUILD_BUG_ON(SOCK_CLOEXEC & SOCK_TYPE_MASK);
1754 BUILD_BUG_ON(SOCK_NONBLOCK & SOCK_TYPE_MASK);
1755
1756 if ((type & ~SOCK_TYPE_MASK) & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1757 return ERR_PTR(-EINVAL);
1758 type &= SOCK_TYPE_MASK;
1759
1760 retval = sock_create(family, type, protocol, &sock);
1761 if (retval < 0)
1762 return ERR_PTR(retval);
1763
1764 return sock;
1765}
1766
1767struct file *__sys_socket_file(int family, int type, int protocol)
1768{
1769 struct socket *sock;
1770 int flags;
1771
1772 sock = __sys_socket_create(family, type, protocol);
1773 if (IS_ERR(sock))
1774 return ERR_CAST(sock);
1775
1776 flags = type & ~SOCK_TYPE_MASK;
1777 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1778 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1779
1780 return sock_alloc_file(sock, flags, NULL);
1781}
1782
1783/* A hook for bpf progs to attach to and update socket protocol.
1784 *
1785 * A static noinline declaration here could cause the compiler to
1786 * optimize away the function. A global noinline declaration will
1787 * keep the definition, but may optimize away the callsite.
1788 * Therefore, __weak is needed to ensure that the call is still
1789 * emitted, by telling the compiler that we don't know what the
1790 * function might eventually be.
1791 */
1792
1793__bpf_hook_start();
1794
1795__weak noinline int update_socket_protocol(int family, int type, int protocol)
1796{
1797 return protocol;
1798}
1799
1800__bpf_hook_end();
1801
1802int __sys_socket(int family, int type, int protocol)
1803{
1804 struct socket *sock;
1805 int flags;
1806
1807 sock = __sys_socket_create(family, type,
1808 update_socket_protocol(family, type, protocol));
1809 if (IS_ERR(sock))
1810 return PTR_ERR(sock);
1811
1812 flags = type & ~SOCK_TYPE_MASK;
1813 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1814 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1815
1816 return sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK));
1817}
1818
1819SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol)
1820{
1821 return __sys_socket(family, type, protocol);
1822}
1823
1824/*
1825 * Create a pair of connected sockets.
1826 */
1827
1828int __sys_socketpair(int family, int type, int protocol, int __user *usockvec)
1829{
1830 struct socket *sock1, *sock2;
1831 int fd1, fd2, err;
1832 struct file *newfile1, *newfile2;
1833 int flags;
1834
1835 flags = type & ~SOCK_TYPE_MASK;
1836 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1837 return -EINVAL;
1838 type &= SOCK_TYPE_MASK;
1839
1840 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1841 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1842
1843 /*
1844 * reserve descriptors and make sure we won't fail
1845 * to return them to userland.
1846 */
1847 fd1 = get_unused_fd_flags(flags);
1848 if (unlikely(fd1 < 0))
1849 return fd1;
1850
1851 fd2 = get_unused_fd_flags(flags);
1852 if (unlikely(fd2 < 0)) {
1853 put_unused_fd(fd1);
1854 return fd2;
1855 }
1856
1857 err = put_user(fd1, &usockvec[0]);
1858 if (err)
1859 goto out;
1860
1861 err = put_user(fd2, &usockvec[1]);
1862 if (err)
1863 goto out;
1864
1865 /*
1866 * Obtain the first socket and check if the underlying protocol
1867 * supports the socketpair call.
1868 */
1869
1870 err = sock_create(family, type, protocol, &sock1);
1871 if (unlikely(err < 0))
1872 goto out;
1873
1874 err = sock_create(family, type, protocol, &sock2);
1875 if (unlikely(err < 0)) {
1876 sock_release(sock1);
1877 goto out;
1878 }
1879
1880 err = security_socket_socketpair(sock1, sock2);
1881 if (unlikely(err)) {
1882 sock_release(sock2);
1883 sock_release(sock1);
1884 goto out;
1885 }
1886
1887 err = READ_ONCE(sock1->ops)->socketpair(sock1, sock2);
1888 if (unlikely(err < 0)) {
1889 sock_release(sock2);
1890 sock_release(sock1);
1891 goto out;
1892 }
1893
1894 newfile1 = sock_alloc_file(sock1, flags, NULL);
1895 if (IS_ERR(newfile1)) {
1896 err = PTR_ERR(newfile1);
1897 sock_release(sock2);
1898 goto out;
1899 }
1900
1901 newfile2 = sock_alloc_file(sock2, flags, NULL);
1902 if (IS_ERR(newfile2)) {
1903 err = PTR_ERR(newfile2);
1904 fput(newfile1);
1905 goto out;
1906 }
1907
1908 audit_fd_pair(fd1, fd2);
1909
1910 fd_install(fd1, newfile1);
1911 fd_install(fd2, newfile2);
1912 return 0;
1913
1914out:
1915 put_unused_fd(fd2);
1916 put_unused_fd(fd1);
1917 return err;
1918}
1919
1920SYSCALL_DEFINE4(socketpair, int, family, int, type, int, protocol,
1921 int __user *, usockvec)
1922{
1923 return __sys_socketpair(family, type, protocol, usockvec);
1924}
1925
1926int __sys_bind_socket(struct socket *sock, struct sockaddr_storage *address,
1927 int addrlen)
1928{
1929 int err;
1930
1931 err = security_socket_bind(sock, (struct sockaddr *)address,
1932 addrlen);
1933 if (!err)
1934 err = READ_ONCE(sock->ops)->bind(sock,
1935 (struct sockaddr_unsized *)address,
1936 addrlen);
1937 return err;
1938}
1939
1940/*
1941 * Bind a name to a socket. Nothing much to do here since it's
1942 * the protocol's responsibility to handle the local address.
1943 *
1944 * We move the socket address to kernel space before we call
1945 * the protocol layer (having also checked the address is ok).
1946 */
1947
1948int __sys_bind(int fd, struct sockaddr __user *umyaddr, int addrlen)
1949{
1950 struct socket *sock;
1951 struct sockaddr_storage address;
1952 CLASS(fd, f)(fd);
1953 int err;
1954
1955 if (fd_empty(f))
1956 return -EBADF;
1957 sock = sock_from_file(fd_file(f));
1958 if (unlikely(!sock))
1959 return -ENOTSOCK;
1960
1961 err = move_addr_to_kernel(umyaddr, addrlen, &address);
1962 if (unlikely(err))
1963 return err;
1964
1965 return __sys_bind_socket(sock, &address, addrlen);
1966}
1967
1968SYSCALL_DEFINE3(bind, int, fd, struct sockaddr __user *, umyaddr, int, addrlen)
1969{
1970 return __sys_bind(fd, umyaddr, addrlen);
1971}
1972
1973/*
1974 * Perform a listen. Basically, we allow the protocol to do anything
1975 * necessary for a listen, and if that works, we mark the socket as
1976 * ready for listening.
1977 */
1978int __sys_listen_socket(struct socket *sock, int backlog)
1979{
1980 int somaxconn, err;
1981
1982 somaxconn = READ_ONCE(sock_net(sock->sk)->core.sysctl_somaxconn);
1983 if ((unsigned int)backlog > somaxconn)
1984 backlog = somaxconn;
1985
1986 err = security_socket_listen(sock, backlog);
1987 if (!err)
1988 err = READ_ONCE(sock->ops)->listen(sock, backlog);
1989 return err;
1990}
1991
1992int __sys_listen(int fd, int backlog)
1993{
1994 CLASS(fd, f)(fd);
1995 struct socket *sock;
1996
1997 if (fd_empty(f))
1998 return -EBADF;
1999 sock = sock_from_file(fd_file(f));
2000 if (unlikely(!sock))
2001 return -ENOTSOCK;
2002
2003 return __sys_listen_socket(sock, backlog);
2004}
2005
2006SYSCALL_DEFINE2(listen, int, fd, int, backlog)
2007{
2008 return __sys_listen(fd, backlog);
2009}
2010
2011struct file *do_accept(struct file *file, struct proto_accept_arg *arg,
2012 struct sockaddr __user *upeer_sockaddr,
2013 int __user *upeer_addrlen, int flags)
2014{
2015 struct socket *sock, *newsock;
2016 struct file *newfile;
2017 int err, len;
2018 struct sockaddr_storage address;
2019 const struct proto_ops *ops;
2020
2021 sock = sock_from_file(file);
2022 if (!sock)
2023 return ERR_PTR(-ENOTSOCK);
2024
2025 newsock = sock_alloc();
2026 if (!newsock)
2027 return ERR_PTR(-ENFILE);
2028 ops = READ_ONCE(sock->ops);
2029
2030 newsock->type = sock->type;
2031 newsock->ops = ops;
2032
2033 /*
2034 * We don't need try_module_get here, as the listening socket (sock)
2035 * has the protocol module (sock->ops->owner) held.
2036 */
2037 __module_get(ops->owner);
2038
2039 newfile = sock_alloc_file(newsock, flags, sock->sk->sk_prot_creator->name);
2040 if (IS_ERR(newfile))
2041 return newfile;
2042
2043 err = security_socket_accept(sock, newsock);
2044 if (err)
2045 goto out_fd;
2046
2047 arg->flags |= sock->file->f_flags;
2048 err = ops->accept(sock, newsock, arg);
2049 if (err < 0)
2050 goto out_fd;
2051
2052 if (upeer_sockaddr) {
2053 len = ops->getname(newsock, (struct sockaddr *)&address, 2);
2054 if (len < 0) {
2055 err = -ECONNABORTED;
2056 goto out_fd;
2057 }
2058 err = move_addr_to_user(&address,
2059 len, upeer_sockaddr, upeer_addrlen);
2060 if (err < 0)
2061 goto out_fd;
2062 }
2063
2064 /* File flags are not inherited via accept() unlike another OSes. */
2065 return newfile;
2066out_fd:
2067 fput(newfile);
2068 return ERR_PTR(err);
2069}
2070
2071static int __sys_accept4_file(struct file *file, struct sockaddr __user *upeer_sockaddr,
2072 int __user *upeer_addrlen, int flags)
2073{
2074 struct proto_accept_arg arg = { };
2075
2076 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
2077 return -EINVAL;
2078
2079 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
2080 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
2081
2082 return FD_ADD(flags, do_accept(file, &arg, upeer_sockaddr, upeer_addrlen, flags));
2083}
2084
2085/*
2086 * For accept, we attempt to create a new socket, set up the link
2087 * with the client, wake up the client, then return the new
2088 * connected fd. We collect the address of the connector in kernel
2089 * space and move it to user at the very end. This is unclean because
2090 * we open the socket then return an error.
2091 *
2092 * 1003.1g adds the ability to recvmsg() to query connection pending
2093 * status to recvmsg. We need to add that support in a way thats
2094 * clean when we restructure accept also.
2095 */
2096
2097int __sys_accept4(int fd, struct sockaddr __user *upeer_sockaddr,
2098 int __user *upeer_addrlen, int flags)
2099{
2100 CLASS(fd, f)(fd);
2101
2102 if (fd_empty(f))
2103 return -EBADF;
2104 return __sys_accept4_file(fd_file(f), upeer_sockaddr,
2105 upeer_addrlen, flags);
2106}
2107
2108SYSCALL_DEFINE4(accept4, int, fd, struct sockaddr __user *, upeer_sockaddr,
2109 int __user *, upeer_addrlen, int, flags)
2110{
2111 return __sys_accept4(fd, upeer_sockaddr, upeer_addrlen, flags);
2112}
2113
2114SYSCALL_DEFINE3(accept, int, fd, struct sockaddr __user *, upeer_sockaddr,
2115 int __user *, upeer_addrlen)
2116{
2117 return __sys_accept4(fd, upeer_sockaddr, upeer_addrlen, 0);
2118}
2119
2120/*
2121 * Attempt to connect to a socket with the server address. The address
2122 * is in user space so we verify it is OK and move it to kernel space.
2123 *
2124 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
2125 * break bindings
2126 *
2127 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
2128 * other SEQPACKET protocols that take time to connect() as it doesn't
2129 * include the -EINPROGRESS status for such sockets.
2130 */
2131
2132int __sys_connect_file(struct file *file, struct sockaddr_storage *address,
2133 int addrlen, int file_flags)
2134{
2135 struct socket *sock;
2136 int err;
2137
2138 sock = sock_from_file(file);
2139 if (!sock) {
2140 err = -ENOTSOCK;
2141 goto out;
2142 }
2143
2144 err =
2145 security_socket_connect(sock, (struct sockaddr *)address, addrlen);
2146 if (err)
2147 goto out;
2148
2149 err = READ_ONCE(sock->ops)->connect(sock, (struct sockaddr_unsized *)address,
2150 addrlen, sock->file->f_flags | file_flags);
2151out:
2152 return err;
2153}
2154
2155int __sys_connect(int fd, struct sockaddr __user *uservaddr, int addrlen)
2156{
2157 struct sockaddr_storage address;
2158 CLASS(fd, f)(fd);
2159 int ret;
2160
2161 if (fd_empty(f))
2162 return -EBADF;
2163
2164 ret = move_addr_to_kernel(uservaddr, addrlen, &address);
2165 if (ret)
2166 return ret;
2167
2168 return __sys_connect_file(fd_file(f), &address, addrlen, 0);
2169}
2170
2171SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
2172 int, addrlen)
2173{
2174 return __sys_connect(fd, uservaddr, addrlen);
2175}
2176
2177int do_getsockname(struct socket *sock, int peer,
2178 struct sockaddr __user *usockaddr, int __user *usockaddr_len)
2179{
2180 struct sockaddr_storage address;
2181 int err;
2182
2183 if (peer)
2184 err = security_socket_getpeername(sock);
2185 else
2186 err = security_socket_getsockname(sock);
2187 if (err)
2188 return err;
2189 err = READ_ONCE(sock->ops)->getname(sock, (struct sockaddr *)&address, peer);
2190 if (err < 0)
2191 return err;
2192 /* "err" is actually length in this case */
2193 return move_addr_to_user(&address, err, usockaddr, usockaddr_len);
2194}
2195
2196/*
2197 * Get the remote or local address ('name') of a socket object. Move the
2198 * obtained name to user space.
2199 */
2200int __sys_getsockname(int fd, struct sockaddr __user *usockaddr,
2201 int __user *usockaddr_len, int peer)
2202{
2203 struct socket *sock;
2204 CLASS(fd, f)(fd);
2205
2206 if (fd_empty(f))
2207 return -EBADF;
2208 sock = sock_from_file(fd_file(f));
2209 if (unlikely(!sock))
2210 return -ENOTSOCK;
2211 return do_getsockname(sock, peer, usockaddr, usockaddr_len);
2212}
2213
2214SYSCALL_DEFINE3(getsockname, int, fd, struct sockaddr __user *, usockaddr,
2215 int __user *, usockaddr_len)
2216{
2217 return __sys_getsockname(fd, usockaddr, usockaddr_len, 0);
2218}
2219
2220SYSCALL_DEFINE3(getpeername, int, fd, struct sockaddr __user *, usockaddr,
2221 int __user *, usockaddr_len)
2222{
2223 return __sys_getsockname(fd, usockaddr, usockaddr_len, 1);
2224}
2225
2226/*
2227 * Send a datagram to a given address. We move the address into kernel
2228 * space and check the user space data area is readable before invoking
2229 * the protocol.
2230 */
2231int __sys_sendto(int fd, void __user *buff, size_t len, unsigned int flags,
2232 struct sockaddr __user *addr, int addr_len)
2233{
2234 struct socket *sock;
2235 struct sockaddr_storage address;
2236 int err;
2237 struct msghdr msg;
2238
2239 err = import_ubuf(ITER_SOURCE, buff, len, &msg.msg_iter);
2240 if (unlikely(err))
2241 return err;
2242
2243 CLASS(fd, f)(fd);
2244 if (fd_empty(f))
2245 return -EBADF;
2246 sock = sock_from_file(fd_file(f));
2247 if (unlikely(!sock))
2248 return -ENOTSOCK;
2249
2250 msg.msg_name = NULL;
2251 msg.msg_control = NULL;
2252 msg.msg_controllen = 0;
2253 msg.msg_namelen = 0;
2254 msg.msg_ubuf = NULL;
2255 if (addr) {
2256 err = move_addr_to_kernel(addr, addr_len, &address);
2257 if (err < 0)
2258 return err;
2259 msg.msg_name = (struct sockaddr *)&address;
2260 msg.msg_namelen = addr_len;
2261 }
2262 flags &= ~MSG_INTERNAL_SENDMSG_FLAGS;
2263 if (sock->file->f_flags & O_NONBLOCK)
2264 flags |= MSG_DONTWAIT;
2265 msg.msg_flags = flags;
2266 return __sock_sendmsg(sock, &msg);
2267}
2268
2269SYSCALL_DEFINE6(sendto, int, fd, void __user *, buff, size_t, len,
2270 unsigned int, flags, struct sockaddr __user *, addr,
2271 int, addr_len)
2272{
2273 return __sys_sendto(fd, buff, len, flags, addr, addr_len);
2274}
2275
2276/*
2277 * Send a datagram down a socket.
2278 */
2279
2280SYSCALL_DEFINE4(send, int, fd, void __user *, buff, size_t, len,
2281 unsigned int, flags)
2282{
2283 return __sys_sendto(fd, buff, len, flags, NULL, 0);
2284}
2285
2286/*
2287 * Receive a frame from the socket and optionally record the address of the
2288 * sender. We verify the buffers are writable and if needed move the
2289 * sender address from kernel to user space.
2290 */
2291int __sys_recvfrom(int fd, void __user *ubuf, size_t size, unsigned int flags,
2292 struct sockaddr __user *addr, int __user *addr_len)
2293{
2294 struct sockaddr_storage address;
2295 struct msghdr msg = {
2296 /* Save some cycles and don't copy the address if not needed */
2297 .msg_name = addr ? (struct sockaddr *)&address : NULL,
2298 };
2299 struct socket *sock;
2300 int err, err2;
2301
2302 err = import_ubuf(ITER_DEST, ubuf, size, &msg.msg_iter);
2303 if (unlikely(err))
2304 return err;
2305
2306 CLASS(fd, f)(fd);
2307
2308 if (fd_empty(f))
2309 return -EBADF;
2310 sock = sock_from_file(fd_file(f));
2311 if (unlikely(!sock))
2312 return -ENOTSOCK;
2313
2314 if (sock->file->f_flags & O_NONBLOCK)
2315 flags |= MSG_DONTWAIT;
2316 err = sock_recvmsg(sock, &msg, flags);
2317
2318 if (err >= 0 && addr != NULL) {
2319 err2 = move_addr_to_user(&address,
2320 msg.msg_namelen, addr, addr_len);
2321 if (err2 < 0)
2322 err = err2;
2323 }
2324 return err;
2325}
2326
2327SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, ubuf, size_t, size,
2328 unsigned int, flags, struct sockaddr __user *, addr,
2329 int __user *, addr_len)
2330{
2331 return __sys_recvfrom(fd, ubuf, size, flags, addr, addr_len);
2332}
2333
2334/*
2335 * Receive a datagram from a socket.
2336 */
2337
2338SYSCALL_DEFINE4(recv, int, fd, void __user *, ubuf, size_t, size,
2339 unsigned int, flags)
2340{
2341 return __sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
2342}
2343
2344static bool sock_use_custom_sol_socket(const struct socket *sock)
2345{
2346 return test_bit(SOCK_CUSTOM_SOCKOPT, &sock->flags);
2347}
2348
2349int do_sock_setsockopt(struct socket *sock, bool compat, int level,
2350 int optname, sockptr_t optval, int optlen)
2351{
2352 const struct proto_ops *ops;
2353 char *kernel_optval = NULL;
2354 int err;
2355
2356 if (optlen < 0)
2357 return -EINVAL;
2358
2359 err = security_socket_setsockopt(sock, level, optname);
2360 if (err)
2361 goto out_put;
2362
2363 if (!compat)
2364 err = BPF_CGROUP_RUN_PROG_SETSOCKOPT(sock->sk, &level, &optname,
2365 optval, &optlen,
2366 &kernel_optval);
2367 if (err < 0)
2368 goto out_put;
2369 if (err > 0) {
2370 err = 0;
2371 goto out_put;
2372 }
2373
2374 if (kernel_optval)
2375 optval = KERNEL_SOCKPTR(kernel_optval);
2376 ops = READ_ONCE(sock->ops);
2377 if (level == SOL_SOCKET && !sock_use_custom_sol_socket(sock))
2378 err = sock_setsockopt(sock, level, optname, optval, optlen);
2379 else if (unlikely(!ops->setsockopt))
2380 err = -EOPNOTSUPP;
2381 else
2382 err = ops->setsockopt(sock, level, optname, optval,
2383 optlen);
2384 kfree(kernel_optval);
2385out_put:
2386 return err;
2387}
2388EXPORT_SYMBOL(do_sock_setsockopt);
2389
2390/* Set a socket option. Because we don't know the option lengths we have
2391 * to pass the user mode parameter for the protocols to sort out.
2392 */
2393int __sys_setsockopt(int fd, int level, int optname, char __user *user_optval,
2394 int optlen)
2395{
2396 sockptr_t optval = USER_SOCKPTR(user_optval);
2397 bool compat = in_compat_syscall();
2398 struct socket *sock;
2399 CLASS(fd, f)(fd);
2400
2401 if (fd_empty(f))
2402 return -EBADF;
2403 sock = sock_from_file(fd_file(f));
2404 if (unlikely(!sock))
2405 return -ENOTSOCK;
2406
2407 return do_sock_setsockopt(sock, compat, level, optname, optval, optlen);
2408}
2409
2410SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname,
2411 char __user *, optval, int, optlen)
2412{
2413 return __sys_setsockopt(fd, level, optname, optval, optlen);
2414}
2415
2416INDIRECT_CALLABLE_DECLARE(bool tcp_bpf_bypass_getsockopt(int level,
2417 int optname));
2418
2419/*
2420 * Initialize a sockopt_t from sockptr optval/optlen, setting up iov_iter
2421 * for both input and output directions.
2422 * It is important to remember that both iov points to the same data, but,
2423 * .iter_in is read-only and .iter_out is write-only by the protocol callbacks
2424 */
2425static int sockptr_to_sockopt(sockopt_t *opt, sockptr_t optval,
2426 sockptr_t optlen, struct kvec *kvec)
2427{
2428 int koptlen;
2429
2430 if (copy_from_sockptr(&koptlen, optlen, sizeof(int)))
2431 return -EFAULT;
2432
2433 if (koptlen < 0)
2434 return -EINVAL;
2435
2436 if (optval.is_kernel) {
2437 kvec->iov_base = optval.kernel;
2438 kvec->iov_len = koptlen;
2439 iov_iter_kvec(&opt->iter_out, ITER_DEST, kvec, 1, koptlen);
2440 iov_iter_kvec(&opt->iter_in, ITER_SOURCE, kvec, 1, koptlen);
2441 } else {
2442 iov_iter_ubuf(&opt->iter_out, ITER_DEST, optval.user, koptlen);
2443 iov_iter_ubuf(&opt->iter_in, ITER_SOURCE, optval.user,
2444 koptlen);
2445 }
2446 opt->optlen = koptlen;
2447
2448 return 0;
2449}
2450
2451int do_sock_getsockopt(struct socket *sock, bool compat, int level,
2452 int optname, sockptr_t optval, sockptr_t optlen)
2453{
2454 int max_optlen __maybe_unused = 0;
2455 const struct proto_ops *ops;
2456 struct kvec kvec;
2457 sockopt_t opt;
2458 int err;
2459
2460 err = security_socket_getsockopt(sock, level, optname);
2461 if (err)
2462 return err;
2463
2464 if (!compat)
2465 copy_from_sockptr(&max_optlen, optlen, sizeof(int));
2466
2467 ops = READ_ONCE(sock->ops);
2468 if (level == SOL_SOCKET) {
2469 err = sk_getsockopt(sock->sk, level, optname, optval, optlen);
2470 } else if (ops->getsockopt_iter) {
2471 err = sockptr_to_sockopt(&opt, optval, optlen, &kvec);
2472 if (err)
2473 return err;
2474
2475 err = ops->getsockopt_iter(sock, level, optname, &opt);
2476
2477 /* Always write back optlen, even on failure. Some protocols
2478 * (e.g. CAN raw) return -ERANGE and set optlen to the
2479 * required buffer size so userspace can discover it.
2480 */
2481 if (copy_to_sockptr(optlen, &opt.optlen, sizeof(int)))
2482 return -EFAULT;
2483 } else if (ops->getsockopt) {
2484 if (WARN_ONCE(optval.is_kernel || optlen.is_kernel,
2485 "Invalid argument type"))
2486 return -EOPNOTSUPP;
2487
2488 err = ops->getsockopt(sock, level, optname, optval.user,
2489 optlen.user);
2490 } else {
2491 err = -EOPNOTSUPP;
2492 }
2493
2494 if (!compat)
2495 err = BPF_CGROUP_RUN_PROG_GETSOCKOPT(sock->sk, level, optname,
2496 optval, optlen, max_optlen,
2497 err);
2498
2499 return err;
2500}
2501EXPORT_SYMBOL(do_sock_getsockopt);
2502
2503/*
2504 * Get a socket option. Because we don't know the option lengths we have
2505 * to pass a user mode parameter for the protocols to sort out.
2506 */
2507int __sys_getsockopt(int fd, int level, int optname, char __user *optval,
2508 int __user *optlen)
2509{
2510 struct socket *sock;
2511 CLASS(fd, f)(fd);
2512
2513 if (fd_empty(f))
2514 return -EBADF;
2515 sock = sock_from_file(fd_file(f));
2516 if (unlikely(!sock))
2517 return -ENOTSOCK;
2518
2519 return do_sock_getsockopt(sock, in_compat_syscall(), level, optname,
2520 USER_SOCKPTR(optval), USER_SOCKPTR(optlen));
2521}
2522
2523SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname,
2524 char __user *, optval, int __user *, optlen)
2525{
2526 return __sys_getsockopt(fd, level, optname, optval, optlen);
2527}
2528
2529/*
2530 * Shutdown a socket.
2531 */
2532
2533int __sys_shutdown_sock(struct socket *sock, int how)
2534{
2535 int err;
2536
2537 err = security_socket_shutdown(sock, how);
2538 if (!err)
2539 err = READ_ONCE(sock->ops)->shutdown(sock, how);
2540
2541 return err;
2542}
2543
2544int __sys_shutdown(int fd, int how)
2545{
2546 struct socket *sock;
2547 CLASS(fd, f)(fd);
2548
2549 if (fd_empty(f))
2550 return -EBADF;
2551 sock = sock_from_file(fd_file(f));
2552 if (unlikely(!sock))
2553 return -ENOTSOCK;
2554
2555 return __sys_shutdown_sock(sock, how);
2556}
2557
2558SYSCALL_DEFINE2(shutdown, int, fd, int, how)
2559{
2560 return __sys_shutdown(fd, how);
2561}
2562
2563/* A couple of helpful macros for getting the address of the 32/64 bit
2564 * fields which are the same type (int / unsigned) on our platforms.
2565 */
2566#define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
2567#define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
2568#define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
2569
2570struct used_address {
2571 struct sockaddr_storage name;
2572 unsigned int name_len;
2573};
2574
2575int __copy_msghdr(struct msghdr *kmsg,
2576 struct user_msghdr *msg,
2577 struct sockaddr __user **save_addr)
2578{
2579 ssize_t err;
2580
2581 kmsg->msg_control_is_user = true;
2582 kmsg->msg_get_inq = 0;
2583 kmsg->msg_control_user = msg->msg_control;
2584 kmsg->msg_controllen = msg->msg_controllen;
2585 kmsg->msg_flags = msg->msg_flags;
2586
2587 kmsg->msg_namelen = msg->msg_namelen;
2588 if (!msg->msg_name)
2589 kmsg->msg_namelen = 0;
2590
2591 if (kmsg->msg_namelen < 0)
2592 return -EINVAL;
2593
2594 if (kmsg->msg_namelen > sizeof(struct sockaddr_storage))
2595 kmsg->msg_namelen = sizeof(struct sockaddr_storage);
2596
2597 if (save_addr)
2598 *save_addr = msg->msg_name;
2599
2600 if (msg->msg_name && kmsg->msg_namelen) {
2601 if (!save_addr) {
2602 err = move_addr_to_kernel(msg->msg_name,
2603 kmsg->msg_namelen,
2604 kmsg->msg_name);
2605 if (err < 0)
2606 return err;
2607 }
2608 } else {
2609 kmsg->msg_name = NULL;
2610 kmsg->msg_namelen = 0;
2611 }
2612
2613 if (msg->msg_iovlen > UIO_MAXIOV)
2614 return -EMSGSIZE;
2615
2616 kmsg->msg_iocb = NULL;
2617 kmsg->msg_ubuf = NULL;
2618 return 0;
2619}
2620
2621static int copy_msghdr_from_user(struct msghdr *kmsg,
2622 struct user_msghdr __user *umsg,
2623 struct sockaddr __user **save_addr,
2624 struct iovec **iov)
2625{
2626 struct user_msghdr msg;
2627 ssize_t err;
2628
2629 if (copy_from_user(&msg, umsg, sizeof(*umsg)))
2630 return -EFAULT;
2631
2632 err = __copy_msghdr(kmsg, &msg, save_addr);
2633 if (err)
2634 return err;
2635
2636 err = import_iovec(save_addr ? ITER_DEST : ITER_SOURCE,
2637 msg.msg_iov, msg.msg_iovlen,
2638 UIO_FASTIOV, iov, &kmsg->msg_iter);
2639 return err < 0 ? err : 0;
2640}
2641
2642static int ____sys_sendmsg(struct socket *sock, struct msghdr *msg_sys,
2643 unsigned int flags, struct used_address *used_address,
2644 unsigned int allowed_msghdr_flags)
2645{
2646 unsigned char ctl[sizeof(struct cmsghdr) + 20]
2647 __aligned(sizeof(__kernel_size_t));
2648 /* 20 is size of ipv6_pktinfo */
2649 unsigned char *ctl_buf = ctl;
2650 int ctl_len;
2651 ssize_t err;
2652
2653 err = -ENOBUFS;
2654
2655 if (msg_sys->msg_controllen > INT_MAX)
2656 goto out;
2657 flags |= (msg_sys->msg_flags & allowed_msghdr_flags);
2658 ctl_len = msg_sys->msg_controllen;
2659 if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
2660 err =
2661 cmsghdr_from_user_compat_to_kern(msg_sys, sock->sk, ctl,
2662 sizeof(ctl));
2663 if (err)
2664 goto out;
2665 ctl_buf = msg_sys->msg_control;
2666 ctl_len = msg_sys->msg_controllen;
2667 } else if (ctl_len) {
2668 BUILD_BUG_ON(sizeof(struct cmsghdr) !=
2669 CMSG_ALIGN(sizeof(struct cmsghdr)));
2670 if (ctl_len > sizeof(ctl)) {
2671 ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
2672 if (ctl_buf == NULL)
2673 goto out;
2674 }
2675 err = -EFAULT;
2676 if (copy_from_user(ctl_buf, msg_sys->msg_control_user, ctl_len))
2677 goto out_freectl;
2678 msg_sys->msg_control = ctl_buf;
2679 msg_sys->msg_control_is_user = false;
2680 }
2681 flags &= ~MSG_INTERNAL_SENDMSG_FLAGS;
2682 msg_sys->msg_flags = flags;
2683
2684 if (sock->file->f_flags & O_NONBLOCK)
2685 msg_sys->msg_flags |= MSG_DONTWAIT;
2686 /*
2687 * If this is sendmmsg() and current destination address is same as
2688 * previously succeeded address, omit asking LSM's decision.
2689 * used_address->name_len is initialized to UINT_MAX so that the first
2690 * destination address never matches.
2691 */
2692 if (used_address && msg_sys->msg_name &&
2693 used_address->name_len == msg_sys->msg_namelen &&
2694 !memcmp(&used_address->name, msg_sys->msg_name,
2695 used_address->name_len)) {
2696 err = sock_sendmsg_nosec(sock, msg_sys);
2697 goto out_freectl;
2698 }
2699 err = __sock_sendmsg(sock, msg_sys);
2700 /*
2701 * If this is sendmmsg() and sending to current destination address was
2702 * successful, remember it.
2703 */
2704 if (used_address && err >= 0) {
2705 used_address->name_len = msg_sys->msg_namelen;
2706 if (msg_sys->msg_name)
2707 memcpy(&used_address->name, msg_sys->msg_name,
2708 used_address->name_len);
2709 }
2710
2711out_freectl:
2712 if (ctl_buf != ctl)
2713 sock_kfree_s(sock->sk, ctl_buf, ctl_len);
2714out:
2715 return err;
2716}
2717
2718static int sendmsg_copy_msghdr(struct msghdr *msg,
2719 struct user_msghdr __user *umsg, unsigned flags,
2720 struct iovec **iov)
2721{
2722 int err;
2723
2724 if (flags & MSG_CMSG_COMPAT) {
2725 struct compat_msghdr __user *msg_compat;
2726
2727 msg_compat = (struct compat_msghdr __user *) umsg;
2728 err = get_compat_msghdr(msg, msg_compat, NULL, iov);
2729 } else {
2730 err = copy_msghdr_from_user(msg, umsg, NULL, iov);
2731 }
2732 if (err < 0)
2733 return err;
2734
2735 return 0;
2736}
2737
2738static int ___sys_sendmsg(struct socket *sock, struct user_msghdr __user *msg,
2739 struct msghdr *msg_sys, unsigned int flags,
2740 struct used_address *used_address,
2741 unsigned int allowed_msghdr_flags)
2742{
2743 struct sockaddr_storage address;
2744 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
2745 ssize_t err;
2746
2747 msg_sys->msg_name = &address;
2748
2749 err = sendmsg_copy_msghdr(msg_sys, msg, flags, &iov);
2750 if (err < 0)
2751 return err;
2752
2753 err = ____sys_sendmsg(sock, msg_sys, flags, used_address,
2754 allowed_msghdr_flags);
2755 kfree(iov);
2756 return err;
2757}
2758
2759/*
2760 * BSD sendmsg interface
2761 */
2762long __sys_sendmsg_sock(struct socket *sock, struct msghdr *msg,
2763 unsigned int flags)
2764{
2765 return ____sys_sendmsg(sock, msg, flags, NULL, 0);
2766}
2767
2768long __sys_sendmsg(int fd, struct user_msghdr __user *msg, unsigned int flags,
2769 bool forbid_cmsg_compat)
2770{
2771 struct msghdr msg_sys;
2772 struct socket *sock;
2773
2774 if (forbid_cmsg_compat && (flags & MSG_CMSG_COMPAT))
2775 return -EINVAL;
2776
2777 CLASS(fd, f)(fd);
2778
2779 if (fd_empty(f))
2780 return -EBADF;
2781 sock = sock_from_file(fd_file(f));
2782 if (unlikely(!sock))
2783 return -ENOTSOCK;
2784
2785 return ___sys_sendmsg(sock, msg, &msg_sys, flags, NULL, 0);
2786}
2787
2788SYSCALL_DEFINE3(sendmsg, int, fd, struct user_msghdr __user *, msg, unsigned int, flags)
2789{
2790 return __sys_sendmsg(fd, msg, flags, true);
2791}
2792
2793/*
2794 * Linux sendmmsg interface
2795 */
2796
2797int __sys_sendmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
2798 unsigned int flags, bool forbid_cmsg_compat)
2799{
2800 int err, datagrams;
2801 struct socket *sock;
2802 struct mmsghdr __user *entry;
2803 struct compat_mmsghdr __user *compat_entry;
2804 struct msghdr msg_sys;
2805 struct used_address used_address;
2806 unsigned int oflags = flags;
2807
2808 if (forbid_cmsg_compat && (flags & MSG_CMSG_COMPAT))
2809 return -EINVAL;
2810
2811 if (vlen > UIO_MAXIOV)
2812 vlen = UIO_MAXIOV;
2813
2814 datagrams = 0;
2815
2816 CLASS(fd, f)(fd);
2817
2818 if (fd_empty(f))
2819 return -EBADF;
2820 sock = sock_from_file(fd_file(f));
2821 if (unlikely(!sock))
2822 return -ENOTSOCK;
2823
2824 used_address.name_len = UINT_MAX;
2825 entry = mmsg;
2826 compat_entry = (struct compat_mmsghdr __user *)mmsg;
2827 err = 0;
2828 flags |= MSG_BATCH;
2829
2830 while (datagrams < vlen) {
2831 if (datagrams == vlen - 1)
2832 flags = oflags;
2833
2834 if (MSG_CMSG_COMPAT & flags) {
2835 err = ___sys_sendmsg(sock, (struct user_msghdr __user *)compat_entry,
2836 &msg_sys, flags, &used_address, MSG_EOR);
2837 if (err < 0)
2838 break;
2839 err = __put_user(err, &compat_entry->msg_len);
2840 ++compat_entry;
2841 } else {
2842 err = ___sys_sendmsg(sock,
2843 (struct user_msghdr __user *)entry,
2844 &msg_sys, flags, &used_address, MSG_EOR);
2845 if (err < 0)
2846 break;
2847 err = put_user(err, &entry->msg_len);
2848 ++entry;
2849 }
2850
2851 if (err)
2852 break;
2853 ++datagrams;
2854 if (msg_data_left(&msg_sys))
2855 break;
2856 cond_resched();
2857 }
2858
2859 /* We only return an error if no datagrams were able to be sent */
2860 if (datagrams != 0)
2861 return datagrams;
2862
2863 return err;
2864}
2865
2866SYSCALL_DEFINE4(sendmmsg, int, fd, struct mmsghdr __user *, mmsg,
2867 unsigned int, vlen, unsigned int, flags)
2868{
2869 return __sys_sendmmsg(fd, mmsg, vlen, flags, true);
2870}
2871
2872static int recvmsg_copy_msghdr(struct msghdr *msg,
2873 struct user_msghdr __user *umsg, unsigned flags,
2874 struct sockaddr __user **uaddr,
2875 struct iovec **iov)
2876{
2877 ssize_t err;
2878
2879 if (MSG_CMSG_COMPAT & flags) {
2880 struct compat_msghdr __user *msg_compat;
2881
2882 msg_compat = (struct compat_msghdr __user *) umsg;
2883 err = get_compat_msghdr(msg, msg_compat, uaddr, iov);
2884 } else {
2885 err = copy_msghdr_from_user(msg, umsg, uaddr, iov);
2886 }
2887 if (err < 0)
2888 return err;
2889
2890 return 0;
2891}
2892
2893static int ____sys_recvmsg(struct socket *sock, struct msghdr *msg_sys,
2894 struct user_msghdr __user *msg,
2895 struct sockaddr __user *uaddr,
2896 unsigned int flags, int nosec)
2897{
2898 struct compat_msghdr __user *msg_compat =
2899 (struct compat_msghdr __user *) msg;
2900 int __user *uaddr_len = COMPAT_NAMELEN(msg);
2901 struct sockaddr_storage addr;
2902 unsigned long cmsg_ptr;
2903 int len;
2904 ssize_t err;
2905
2906 msg_sys->msg_name = &addr;
2907 cmsg_ptr = (unsigned long)msg_sys->msg_control;
2908 msg_sys->msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT);
2909
2910 /* We assume all kernel code knows the size of sockaddr_storage */
2911 msg_sys->msg_namelen = 0;
2912
2913 if (sock->file->f_flags & O_NONBLOCK)
2914 flags |= MSG_DONTWAIT;
2915
2916 if (unlikely(nosec))
2917 err = sock_recvmsg_nosec(sock, msg_sys, flags);
2918 else
2919 err = sock_recvmsg(sock, msg_sys, flags);
2920
2921 if (err < 0)
2922 goto out;
2923 len = err;
2924
2925 if (uaddr != NULL) {
2926 err = move_addr_to_user(&addr,
2927 msg_sys->msg_namelen, uaddr,
2928 uaddr_len);
2929 if (err < 0)
2930 goto out;
2931 }
2932 err = __put_user((msg_sys->msg_flags & ~MSG_CMSG_COMPAT),
2933 COMPAT_FLAGS(msg));
2934 if (err)
2935 goto out;
2936 if (MSG_CMSG_COMPAT & flags)
2937 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
2938 &msg_compat->msg_controllen);
2939 else
2940 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
2941 &msg->msg_controllen);
2942 if (err)
2943 goto out;
2944 err = len;
2945out:
2946 return err;
2947}
2948
2949static int ___sys_recvmsg(struct socket *sock, struct user_msghdr __user *msg,
2950 struct msghdr *msg_sys, unsigned int flags, int nosec)
2951{
2952 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
2953 /* user mode address pointers */
2954 struct sockaddr __user *uaddr;
2955 ssize_t err;
2956
2957 err = recvmsg_copy_msghdr(msg_sys, msg, flags, &uaddr, &iov);
2958 if (err < 0)
2959 return err;
2960
2961 err = ____sys_recvmsg(sock, msg_sys, msg, uaddr, flags, nosec);
2962 kfree(iov);
2963 return err;
2964}
2965
2966/*
2967 * BSD recvmsg interface
2968 */
2969
2970long __sys_recvmsg_sock(struct socket *sock, struct msghdr *msg,
2971 struct user_msghdr __user *umsg,
2972 struct sockaddr __user *uaddr, unsigned int flags)
2973{
2974 return ____sys_recvmsg(sock, msg, umsg, uaddr, flags, 0);
2975}
2976
2977long __sys_recvmsg(int fd, struct user_msghdr __user *msg, unsigned int flags,
2978 bool forbid_cmsg_compat)
2979{
2980 struct msghdr msg_sys;
2981 struct socket *sock;
2982
2983 if (forbid_cmsg_compat && (flags & MSG_CMSG_COMPAT))
2984 return -EINVAL;
2985
2986 CLASS(fd, f)(fd);
2987
2988 if (fd_empty(f))
2989 return -EBADF;
2990 sock = sock_from_file(fd_file(f));
2991 if (unlikely(!sock))
2992 return -ENOTSOCK;
2993
2994 return ___sys_recvmsg(sock, msg, &msg_sys, flags, 0);
2995}
2996
2997SYSCALL_DEFINE3(recvmsg, int, fd, struct user_msghdr __user *, msg,
2998 unsigned int, flags)
2999{
3000 return __sys_recvmsg(fd, msg, flags, true);
3001}
3002
3003/*
3004 * Linux recvmmsg interface
3005 */
3006
3007static int do_recvmmsg(int fd, struct mmsghdr __user *mmsg,
3008 unsigned int vlen, unsigned int flags,
3009 struct timespec64 *timeout)
3010{
3011 int err = 0, datagrams;
3012 struct socket *sock;
3013 struct mmsghdr __user *entry;
3014 struct compat_mmsghdr __user *compat_entry;
3015 struct msghdr msg_sys;
3016 struct timespec64 end_time;
3017 struct timespec64 timeout64;
3018
3019 if (timeout &&
3020 poll_select_set_timeout(&end_time, timeout->tv_sec,
3021 timeout->tv_nsec))
3022 return -EINVAL;
3023
3024 datagrams = 0;
3025
3026 CLASS(fd, f)(fd);
3027
3028 if (fd_empty(f))
3029 return -EBADF;
3030 sock = sock_from_file(fd_file(f));
3031 if (unlikely(!sock))
3032 return -ENOTSOCK;
3033
3034 if (likely(!(flags & MSG_ERRQUEUE))) {
3035 err = sock_error(sock->sk);
3036 if (err)
3037 return err;
3038 }
3039
3040 entry = mmsg;
3041 compat_entry = (struct compat_mmsghdr __user *)mmsg;
3042
3043 while (datagrams < vlen) {
3044 /*
3045 * No need to ask LSM for more than the first datagram.
3046 */
3047 if (MSG_CMSG_COMPAT & flags) {
3048 err = ___sys_recvmsg(sock, (struct user_msghdr __user *)compat_entry,
3049 &msg_sys, flags & ~MSG_WAITFORONE,
3050 datagrams);
3051 if (err < 0)
3052 break;
3053 err = __put_user(err, &compat_entry->msg_len);
3054 ++compat_entry;
3055 } else {
3056 err = ___sys_recvmsg(sock,
3057 (struct user_msghdr __user *)entry,
3058 &msg_sys, flags & ~MSG_WAITFORONE,
3059 datagrams);
3060 if (err < 0)
3061 break;
3062 err = put_user(err, &entry->msg_len);
3063 ++entry;
3064 }
3065
3066 if (err)
3067 break;
3068 ++datagrams;
3069
3070 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
3071 if (flags & MSG_WAITFORONE)
3072 flags |= MSG_DONTWAIT;
3073
3074 if (timeout) {
3075 ktime_get_ts64(&timeout64);
3076 *timeout = timespec64_sub(end_time, timeout64);
3077 if (timeout->tv_sec < 0) {
3078 timeout->tv_sec = timeout->tv_nsec = 0;
3079 break;
3080 }
3081
3082 /* Timeout, return less than vlen datagrams */
3083 if (timeout->tv_nsec == 0 && timeout->tv_sec == 0)
3084 break;
3085 }
3086
3087 /* Out of band data, return right away */
3088 if (msg_sys.msg_flags & MSG_OOB)
3089 break;
3090 cond_resched();
3091 }
3092
3093 if (err == 0)
3094 return datagrams;
3095
3096 if (datagrams == 0)
3097 return err;
3098
3099 /*
3100 * We may return less entries than requested (vlen) if the
3101 * sock is non block and there aren't enough datagrams...
3102 */
3103 if (err != -EAGAIN) {
3104 /*
3105 * ... or if recvmsg returns an error after we
3106 * received some datagrams, where we record the
3107 * error to return on the next call or if the
3108 * app asks about it using getsockopt(SO_ERROR).
3109 */
3110 WRITE_ONCE(sock->sk->sk_err, -err);
3111 }
3112 return datagrams;
3113}
3114
3115int __sys_recvmmsg(int fd, struct mmsghdr __user *mmsg,
3116 unsigned int vlen, unsigned int flags,
3117 struct __kernel_timespec __user *timeout,
3118 struct old_timespec32 __user *timeout32)
3119{
3120 int datagrams;
3121 struct timespec64 timeout_sys;
3122
3123 if (timeout && get_timespec64(&timeout_sys, timeout))
3124 return -EFAULT;
3125
3126 if (timeout32 && get_old_timespec32(&timeout_sys, timeout32))
3127 return -EFAULT;
3128
3129 if (!timeout && !timeout32)
3130 return do_recvmmsg(fd, mmsg, vlen, flags, NULL);
3131
3132 datagrams = do_recvmmsg(fd, mmsg, vlen, flags, &timeout_sys);
3133
3134 if (datagrams <= 0)
3135 return datagrams;
3136
3137 if (timeout && put_timespec64(&timeout_sys, timeout))
3138 datagrams = -EFAULT;
3139
3140 if (timeout32 && put_old_timespec32(&timeout_sys, timeout32))
3141 datagrams = -EFAULT;
3142
3143 return datagrams;
3144}
3145
3146SYSCALL_DEFINE5(recvmmsg, int, fd, struct mmsghdr __user *, mmsg,
3147 unsigned int, vlen, unsigned int, flags,
3148 struct __kernel_timespec __user *, timeout)
3149{
3150 if (flags & MSG_CMSG_COMPAT)
3151 return -EINVAL;
3152
3153 return __sys_recvmmsg(fd, mmsg, vlen, flags, timeout, NULL);
3154}
3155
3156#ifdef CONFIG_COMPAT_32BIT_TIME
3157SYSCALL_DEFINE5(recvmmsg_time32, int, fd, struct mmsghdr __user *, mmsg,
3158 unsigned int, vlen, unsigned int, flags,
3159 struct old_timespec32 __user *, timeout)
3160{
3161 if (flags & MSG_CMSG_COMPAT)
3162 return -EINVAL;
3163
3164 return __sys_recvmmsg(fd, mmsg, vlen, flags, NULL, timeout);
3165}
3166#endif
3167
3168#ifdef __ARCH_WANT_SYS_SOCKETCALL
3169/* Argument list sizes for sys_socketcall */
3170#define AL(x) ((x) * sizeof(unsigned long))
3171static const unsigned char nargs[21] = {
3172 AL(0), AL(3), AL(3), AL(3), AL(2), AL(3),
3173 AL(3), AL(3), AL(4), AL(4), AL(4), AL(6),
3174 AL(6), AL(2), AL(5), AL(5), AL(3), AL(3),
3175 AL(4), AL(5), AL(4)
3176};
3177
3178#undef AL
3179
3180/*
3181 * System call vectors.
3182 *
3183 * Argument checking cleaned up. Saved 20% in size.
3184 * This function doesn't need to set the kernel lock because
3185 * it is set by the callees.
3186 */
3187
3188SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args)
3189{
3190 unsigned long a[AUDITSC_ARGS];
3191 unsigned long a0, a1;
3192 int err;
3193 unsigned int len;
3194
3195 if (call < 1 || call > SYS_SENDMMSG)
3196 return -EINVAL;
3197 call = array_index_nospec(call, SYS_SENDMMSG + 1);
3198
3199 len = nargs[call];
3200 if (len > sizeof(a))
3201 return -EINVAL;
3202
3203 /* copy_from_user should be SMP safe. */
3204 if (copy_from_user(a, args, len))
3205 return -EFAULT;
3206
3207 err = audit_socketcall(nargs[call] / sizeof(unsigned long), a);
3208 if (err)
3209 return err;
3210
3211 a0 = a[0];
3212 a1 = a[1];
3213
3214 switch (call) {
3215 case SYS_SOCKET:
3216 err = __sys_socket(a0, a1, a[2]);
3217 break;
3218 case SYS_BIND:
3219 err = __sys_bind(a0, (struct sockaddr __user *)a1, a[2]);
3220 break;
3221 case SYS_CONNECT:
3222 err = __sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
3223 break;
3224 case SYS_LISTEN:
3225 err = __sys_listen(a0, a1);
3226 break;
3227 case SYS_ACCEPT:
3228 err = __sys_accept4(a0, (struct sockaddr __user *)a1,
3229 (int __user *)a[2], 0);
3230 break;
3231 case SYS_GETSOCKNAME:
3232 err =
3233 __sys_getsockname(a0, (struct sockaddr __user *)a1,
3234 (int __user *)a[2], 0);
3235 break;
3236 case SYS_GETPEERNAME:
3237 err =
3238 __sys_getsockname(a0, (struct sockaddr __user *)a1,
3239 (int __user *)a[2], 1);
3240 break;
3241 case SYS_SOCKETPAIR:
3242 err = __sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
3243 break;
3244 case SYS_SEND:
3245 err = __sys_sendto(a0, (void __user *)a1, a[2], a[3],
3246 NULL, 0);
3247 break;
3248 case SYS_SENDTO:
3249 err = __sys_sendto(a0, (void __user *)a1, a[2], a[3],
3250 (struct sockaddr __user *)a[4], a[5]);
3251 break;
3252 case SYS_RECV:
3253 err = __sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
3254 NULL, NULL);
3255 break;
3256 case SYS_RECVFROM:
3257 err = __sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
3258 (struct sockaddr __user *)a[4],
3259 (int __user *)a[5]);
3260 break;
3261 case SYS_SHUTDOWN:
3262 err = __sys_shutdown(a0, a1);
3263 break;
3264 case SYS_SETSOCKOPT:
3265 err = __sys_setsockopt(a0, a1, a[2], (char __user *)a[3],
3266 a[4]);
3267 break;
3268 case SYS_GETSOCKOPT:
3269 err =
3270 __sys_getsockopt(a0, a1, a[2], (char __user *)a[3],
3271 (int __user *)a[4]);
3272 break;
3273 case SYS_SENDMSG:
3274 err = __sys_sendmsg(a0, (struct user_msghdr __user *)a1,
3275 a[2], true);
3276 break;
3277 case SYS_SENDMMSG:
3278 err = __sys_sendmmsg(a0, (struct mmsghdr __user *)a1, a[2],
3279 a[3], true);
3280 break;
3281 case SYS_RECVMSG:
3282 err = __sys_recvmsg(a0, (struct user_msghdr __user *)a1,
3283 a[2], true);
3284 break;
3285 case SYS_RECVMMSG:
3286 if (IS_ENABLED(CONFIG_64BIT))
3287 err = __sys_recvmmsg(a0, (struct mmsghdr __user *)a1,
3288 a[2], a[3],
3289 (struct __kernel_timespec __user *)a[4],
3290 NULL);
3291 else
3292 err = __sys_recvmmsg(a0, (struct mmsghdr __user *)a1,
3293 a[2], a[3], NULL,
3294 (struct old_timespec32 __user *)a[4]);
3295 break;
3296 case SYS_ACCEPT4:
3297 err = __sys_accept4(a0, (struct sockaddr __user *)a1,
3298 (int __user *)a[2], a[3]);
3299 break;
3300 default:
3301 err = -EINVAL;
3302 break;
3303 }
3304 return err;
3305}
3306
3307#endif /* __ARCH_WANT_SYS_SOCKETCALL */
3308
3309/**
3310 * sock_register - add a socket protocol handler
3311 * @ops: description of protocol
3312 *
3313 * This function is called by a protocol handler that wants to
3314 * advertise its address family, and have it linked into the
3315 * socket interface. The value ops->family corresponds to the
3316 * socket system call protocol family.
3317 */
3318int sock_register(const struct net_proto_family *ops)
3319{
3320 int err;
3321
3322 if (ops->family >= NPROTO) {
3323 pr_crit("protocol %d >= NPROTO(%d)\n", ops->family, NPROTO);
3324 return -ENOBUFS;
3325 }
3326
3327 spin_lock(&net_family_lock);
3328 if (rcu_dereference_protected(net_families[ops->family],
3329 lockdep_is_held(&net_family_lock)))
3330 err = -EEXIST;
3331 else {
3332 rcu_assign_pointer(net_families[ops->family], ops);
3333 err = 0;
3334 }
3335 spin_unlock(&net_family_lock);
3336
3337 pr_info("NET: Registered %s protocol family\n", pf_family_names[ops->family]);
3338 return err;
3339}
3340EXPORT_SYMBOL(sock_register);
3341
3342/**
3343 * sock_unregister - remove a protocol handler
3344 * @family: protocol family to remove
3345 *
3346 * This function is called by a protocol handler that wants to
3347 * remove its address family, and have it unlinked from the
3348 * new socket creation.
3349 *
3350 * If protocol handler is a module, then it can use module reference
3351 * counts to protect against new references. If protocol handler is not
3352 * a module then it needs to provide its own protection in
3353 * the ops->create routine.
3354 */
3355void sock_unregister(int family)
3356{
3357 BUG_ON(family < 0 || family >= NPROTO);
3358
3359 spin_lock(&net_family_lock);
3360 RCU_INIT_POINTER(net_families[family], NULL);
3361 spin_unlock(&net_family_lock);
3362
3363 synchronize_rcu();
3364
3365 pr_info("NET: Unregistered %s protocol family\n", pf_family_names[family]);
3366}
3367EXPORT_SYMBOL(sock_unregister);
3368
3369bool sock_is_registered(int family)
3370{
3371 return family < NPROTO && rcu_access_pointer(net_families[family]);
3372}
3373
3374static int __init sock_init(void)
3375{
3376 int err;
3377 /*
3378 * Initialize the network sysctl infrastructure.
3379 */
3380 err = net_sysctl_init();
3381 if (err)
3382 goto out;
3383
3384 /*
3385 * Initialize skbuff SLAB cache
3386 */
3387 skb_init();
3388
3389 /*
3390 * Initialize the protocols module.
3391 */
3392
3393 init_inodecache();
3394
3395 err = register_filesystem(&sock_fs_type);
3396 if (err)
3397 goto out;
3398 sock_mnt = kern_mount(&sock_fs_type);
3399 if (IS_ERR(sock_mnt)) {
3400 err = PTR_ERR(sock_mnt);
3401 goto out_mount;
3402 }
3403
3404 /* The real protocol initialization is performed in later initcalls.
3405 */
3406
3407#ifdef CONFIG_NETFILTER
3408 err = netfilter_init();
3409 if (err)
3410 goto out;
3411#endif
3412
3413 ptp_classifier_init();
3414
3415out:
3416 return err;
3417
3418out_mount:
3419 unregister_filesystem(&sock_fs_type);
3420 goto out;
3421}
3422
3423core_initcall(sock_init); /* early initcall */
3424
3425#ifdef CONFIG_PROC_FS
3426void socket_seq_show(struct seq_file *seq)
3427{
3428 seq_printf(seq, "sockets: used %d\n",
3429 sock_inuse_get(seq->private));
3430}
3431#endif /* CONFIG_PROC_FS */
3432
3433/* Handle the fact that while struct ifreq has the same *layout* on
3434 * 32/64 for everything but ifreq::ifru_ifmap and ifreq::ifru_data,
3435 * which are handled elsewhere, it still has different *size* due to
3436 * ifreq::ifru_ifmap (which is 16 bytes on 32 bit, 24 bytes on 64-bit,
3437 * resulting in struct ifreq being 32 and 40 bytes respectively).
3438 * As a result, if the struct happens to be at the end of a page and
3439 * the next page isn't readable/writable, we get a fault. To prevent
3440 * that, copy back and forth to the full size.
3441 */
3442int get_user_ifreq(struct ifreq *ifr, void __user **ifrdata, void __user *arg)
3443{
3444 if (in_compat_syscall()) {
3445 struct compat_ifreq *ifr32 = (struct compat_ifreq *)ifr;
3446
3447 memset(ifr, 0, sizeof(*ifr));
3448 if (copy_from_user(ifr32, arg, sizeof(*ifr32)))
3449 return -EFAULT;
3450
3451 if (ifrdata)
3452 *ifrdata = compat_ptr(ifr32->ifr_data);
3453
3454 return 0;
3455 }
3456
3457 if (copy_from_user(ifr, arg, sizeof(*ifr)))
3458 return -EFAULT;
3459
3460 if (ifrdata)
3461 *ifrdata = ifr->ifr_data;
3462
3463 return 0;
3464}
3465EXPORT_SYMBOL(get_user_ifreq);
3466
3467int put_user_ifreq(struct ifreq *ifr, void __user *arg)
3468{
3469 size_t size = sizeof(*ifr);
3470
3471 if (in_compat_syscall())
3472 size = sizeof(struct compat_ifreq);
3473
3474 if (copy_to_user(arg, ifr, size))
3475 return -EFAULT;
3476
3477 return 0;
3478}
3479EXPORT_SYMBOL(put_user_ifreq);
3480
3481#ifdef CONFIG_COMPAT
3482static int compat_siocwandev(struct net *net, struct compat_ifreq __user *uifr32)
3483{
3484 compat_uptr_t uptr32;
3485 struct ifreq ifr;
3486 void __user *saved;
3487 int err;
3488
3489 if (get_user_ifreq(&ifr, NULL, uifr32))
3490 return -EFAULT;
3491
3492 if (get_user(uptr32, &uifr32->ifr_settings.ifs_ifsu))
3493 return -EFAULT;
3494
3495 saved = ifr.ifr_settings.ifs_ifsu.raw_hdlc;
3496 ifr.ifr_settings.ifs_ifsu.raw_hdlc = compat_ptr(uptr32);
3497
3498 err = dev_ioctl(net, SIOCWANDEV, &ifr, NULL, NULL);
3499 if (!err) {
3500 ifr.ifr_settings.ifs_ifsu.raw_hdlc = saved;
3501 if (put_user_ifreq(&ifr, uifr32))
3502 err = -EFAULT;
3503 }
3504 return err;
3505}
3506
3507/* Handle ioctls that use ifreq::ifr_data and just need struct ifreq converted */
3508static int compat_ifr_data_ioctl(struct net *net, unsigned int cmd,
3509 struct compat_ifreq __user *u_ifreq32)
3510{
3511 struct ifreq ifreq;
3512 void __user *data;
3513
3514 if (!is_socket_ioctl_cmd(cmd))
3515 return -ENOTTY;
3516 if (get_user_ifreq(&ifreq, &data, u_ifreq32))
3517 return -EFAULT;
3518 ifreq.ifr_data = data;
3519
3520 return dev_ioctl(net, cmd, &ifreq, data, NULL);
3521}
3522
3523static int compat_sock_ioctl_trans(struct file *file, struct socket *sock,
3524 unsigned int cmd, unsigned long arg)
3525{
3526 void __user *argp = compat_ptr(arg);
3527 struct sock *sk = sock->sk;
3528 struct net *net = sock_net(sk);
3529 const struct proto_ops *ops;
3530
3531 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15))
3532 return sock_ioctl(file, cmd, (unsigned long)argp);
3533
3534 switch (cmd) {
3535 case SIOCWANDEV:
3536 return compat_siocwandev(net, argp);
3537 case SIOCGSTAMP_OLD:
3538 case SIOCGSTAMPNS_OLD:
3539 ops = READ_ONCE(sock->ops);
3540 if (!ops->gettstamp)
3541 return -ENOIOCTLCMD;
3542 return ops->gettstamp(sock, argp, cmd == SIOCGSTAMP_OLD,
3543 !COMPAT_USE_64BIT_TIME);
3544
3545 case SIOCETHTOOL:
3546 case SIOCBONDSLAVEINFOQUERY:
3547 case SIOCBONDINFOQUERY:
3548 case SIOCSHWTSTAMP:
3549 case SIOCGHWTSTAMP:
3550 return compat_ifr_data_ioctl(net, cmd, argp);
3551
3552 case FIOSETOWN:
3553 case SIOCSPGRP:
3554 case FIOGETOWN:
3555 case SIOCGPGRP:
3556 case SIOCBRADDBR:
3557 case SIOCBRDELBR:
3558 case SIOCBRADDIF:
3559 case SIOCBRDELIF:
3560 case SIOCGIFVLAN:
3561 case SIOCSIFVLAN:
3562 case SIOCGSKNS:
3563 case SIOCGSTAMP_NEW:
3564 case SIOCGSTAMPNS_NEW:
3565 case SIOCGIFCONF:
3566 case SIOCSIFBR:
3567 case SIOCGIFBR:
3568 return sock_ioctl(file, cmd, arg);
3569
3570 case SIOCGIFFLAGS:
3571 case SIOCSIFFLAGS:
3572 case SIOCGIFMAP:
3573 case SIOCSIFMAP:
3574 case SIOCGIFMETRIC:
3575 case SIOCSIFMETRIC:
3576 case SIOCGIFMTU:
3577 case SIOCSIFMTU:
3578 case SIOCGIFMEM:
3579 case SIOCSIFMEM:
3580 case SIOCGIFHWADDR:
3581 case SIOCSIFHWADDR:
3582 case SIOCADDMULTI:
3583 case SIOCDELMULTI:
3584 case SIOCGIFINDEX:
3585 case SIOCGIFADDR:
3586 case SIOCSIFADDR:
3587 case SIOCSIFHWBROADCAST:
3588 case SIOCDIFADDR:
3589 case SIOCGIFBRDADDR:
3590 case SIOCSIFBRDADDR:
3591 case SIOCGIFDSTADDR:
3592 case SIOCSIFDSTADDR:
3593 case SIOCGIFNETMASK:
3594 case SIOCSIFNETMASK:
3595 case SIOCSIFPFLAGS:
3596 case SIOCGIFPFLAGS:
3597 case SIOCGIFTXQLEN:
3598 case SIOCSIFTXQLEN:
3599 case SIOCGIFNAME:
3600 case SIOCSIFNAME:
3601 case SIOCGMIIPHY:
3602 case SIOCGMIIREG:
3603 case SIOCSMIIREG:
3604 case SIOCBONDENSLAVE:
3605 case SIOCBONDRELEASE:
3606 case SIOCBONDSETHWADDR:
3607 case SIOCBONDCHANGEACTIVE:
3608 case SIOCSARP:
3609 case SIOCGARP:
3610 case SIOCDARP:
3611 case SIOCOUTQ:
3612 case SIOCOUTQNSD:
3613 case SIOCATMARK:
3614 return sock_do_ioctl(net, sock, cmd, arg);
3615 }
3616
3617 return -ENOIOCTLCMD;
3618}
3619
3620static long compat_sock_ioctl(struct file *file, unsigned int cmd,
3621 unsigned long arg)
3622{
3623 struct socket *sock = file->private_data;
3624 const struct proto_ops *ops = READ_ONCE(sock->ops);
3625 int ret = -ENOIOCTLCMD;
3626 struct sock *sk;
3627 struct net *net;
3628
3629 sk = sock->sk;
3630 net = sock_net(sk);
3631
3632 if (ops->compat_ioctl)
3633 ret = ops->compat_ioctl(sock, cmd, arg);
3634
3635 if (ret == -ENOIOCTLCMD &&
3636 (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST))
3637 ret = compat_wext_handle_ioctl(net, cmd, arg);
3638
3639 if (ret == -ENOIOCTLCMD)
3640 ret = compat_sock_ioctl_trans(file, sock, cmd, arg);
3641
3642 return ret;
3643}
3644#endif
3645
3646/**
3647 * kernel_bind - bind an address to a socket (kernel space)
3648 * @sock: socket
3649 * @addr: address
3650 * @addrlen: length of address
3651 *
3652 * Returns 0 or an error.
3653 */
3654
3655int kernel_bind(struct socket *sock, struct sockaddr_unsized *addr, int addrlen)
3656{
3657 struct sockaddr_storage address;
3658
3659 memcpy(&address, addr, addrlen);
3660
3661 return READ_ONCE(sock->ops)->bind(sock, (struct sockaddr_unsized *)&address,
3662 addrlen);
3663}
3664EXPORT_SYMBOL(kernel_bind);
3665
3666/**
3667 * kernel_listen - move socket to listening state (kernel space)
3668 * @sock: socket
3669 * @backlog: pending connections queue size
3670 *
3671 * Returns 0 or an error.
3672 */
3673
3674int kernel_listen(struct socket *sock, int backlog)
3675{
3676 return READ_ONCE(sock->ops)->listen(sock, backlog);
3677}
3678EXPORT_SYMBOL(kernel_listen);
3679
3680/**
3681 * kernel_accept - accept a connection (kernel space)
3682 * @sock: listening socket
3683 * @newsock: new connected socket
3684 * @flags: flags
3685 *
3686 * @flags must be SOCK_CLOEXEC, SOCK_NONBLOCK or 0.
3687 * If it fails, @newsock is guaranteed to be %NULL.
3688 * Returns 0 or an error.
3689 */
3690
3691int kernel_accept(struct socket *sock, struct socket **newsock, int flags)
3692{
3693 struct sock *sk = sock->sk;
3694 const struct proto_ops *ops = READ_ONCE(sock->ops);
3695 struct proto_accept_arg arg = {
3696 .flags = flags,
3697 .kern = true,
3698 };
3699 int err;
3700
3701 err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
3702 newsock);
3703 if (err < 0)
3704 goto done;
3705
3706 err = ops->accept(sock, *newsock, &arg);
3707 if (err < 0) {
3708 sock_release(*newsock);
3709 *newsock = NULL;
3710 goto done;
3711 }
3712
3713 (*newsock)->ops = ops;
3714 __module_get(ops->owner);
3715
3716done:
3717 return err;
3718}
3719EXPORT_SYMBOL(kernel_accept);
3720
3721/**
3722 * kernel_connect - connect a socket (kernel space)
3723 * @sock: socket
3724 * @addr: address
3725 * @addrlen: address length
3726 * @flags: flags (O_NONBLOCK, ...)
3727 *
3728 * For datagram sockets, @addr is the address to which datagrams are sent
3729 * by default, and the only address from which datagrams are received.
3730 * For stream sockets, attempts to connect to @addr.
3731 * Returns 0 or an error code.
3732 */
3733
3734int kernel_connect(struct socket *sock, struct sockaddr_unsized *addr, int addrlen,
3735 int flags)
3736{
3737 struct sockaddr_storage address;
3738
3739 memcpy(&address, addr, addrlen);
3740
3741 return READ_ONCE(sock->ops)->connect(sock, (struct sockaddr_unsized *)&address,
3742 addrlen, flags);
3743}
3744EXPORT_SYMBOL(kernel_connect);
3745
3746/**
3747 * kernel_getsockname - get the address which the socket is bound (kernel space)
3748 * @sock: socket
3749 * @addr: address holder
3750 *
3751 * Fills the @addr pointer with the address which the socket is bound.
3752 * Returns the length of the address in bytes or an error code.
3753 */
3754
3755int kernel_getsockname(struct socket *sock, struct sockaddr *addr)
3756{
3757 return READ_ONCE(sock->ops)->getname(sock, addr, 0);
3758}
3759EXPORT_SYMBOL(kernel_getsockname);
3760
3761/**
3762 * kernel_getpeername - get the address which the socket is connected (kernel space)
3763 * @sock: socket
3764 * @addr: address holder
3765 *
3766 * Fills the @addr pointer with the address which the socket is connected.
3767 * Returns the length of the address in bytes or an error code.
3768 */
3769
3770int kernel_getpeername(struct socket *sock, struct sockaddr *addr)
3771{
3772 return READ_ONCE(sock->ops)->getname(sock, addr, 1);
3773}
3774EXPORT_SYMBOL(kernel_getpeername);
3775
3776/**
3777 * kernel_sock_shutdown - shut down part of a full-duplex connection (kernel space)
3778 * @sock: socket
3779 * @how: connection part
3780 *
3781 * Returns 0 or an error.
3782 */
3783
3784int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how)
3785{
3786 return READ_ONCE(sock->ops)->shutdown(sock, how);
3787}
3788EXPORT_SYMBOL(kernel_sock_shutdown);
3789
3790/**
3791 * kernel_sock_ip_overhead - returns the IP overhead imposed by a socket
3792 * @sk: socket
3793 *
3794 * This routine returns the IP overhead imposed by a socket i.e.
3795 * the length of the underlying IP header, depending on whether
3796 * this is an IPv4 or IPv6 socket and the length from IP options turned
3797 * on at the socket. Assumes that the caller has a lock on the socket.
3798 */
3799
3800u32 kernel_sock_ip_overhead(struct sock *sk)
3801{
3802 struct inet_sock *inet;
3803 struct ip_options_rcu *opt;
3804 u32 overhead = 0;
3805#if IS_ENABLED(CONFIG_IPV6)
3806 struct ipv6_pinfo *np;
3807 struct ipv6_txoptions *optv6 = NULL;
3808#endif /* IS_ENABLED(CONFIG_IPV6) */
3809
3810 if (!sk)
3811 return overhead;
3812
3813 switch (sk->sk_family) {
3814 case AF_INET:
3815 inet = inet_sk(sk);
3816 overhead += sizeof(struct iphdr);
3817 opt = rcu_dereference_protected(inet->inet_opt,
3818 sock_owned_by_user(sk));
3819 if (opt)
3820 overhead += opt->opt.optlen;
3821 return overhead;
3822#if IS_ENABLED(CONFIG_IPV6)
3823 case AF_INET6:
3824 np = inet6_sk(sk);
3825 overhead += sizeof(struct ipv6hdr);
3826 if (np)
3827 optv6 = rcu_dereference_protected(np->opt,
3828 sock_owned_by_user(sk));
3829 if (optv6)
3830 overhead += (optv6->opt_flen + optv6->opt_nflen);
3831 return overhead;
3832#endif /* IS_ENABLED(CONFIG_IPV6) */
3833 default: /* Returns 0 overhead if the socket is not ipv4 or ipv6 */
3834 return overhead;
3835 }
3836}
3837EXPORT_SYMBOL(kernel_sock_ip_overhead);