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