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