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