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