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