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