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