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