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