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