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