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