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