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