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