RHEL5/net/socket.c
<<
>>
Prefs
   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/smp_lock.h>
  63#include <linux/socket.h>
  64#include <linux/file.h>
  65#include <linux/net.h>
  66#include <linux/interrupt.h>
  67#include <linux/netdevice.h>
  68#include <linux/proc_fs.h>
  69#include <linux/seq_file.h>
  70#include <net/tux.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/divert.h>
  82#include <linux/mount.h>
  83#include <linux/security.h>
  84#include <linux/syscalls.h>
  85#include <linux/compat.h>
  86#include <linux/kmod.h>
  87#include <linux/audit.h>
  88#include <linux/wireless.h>
  89
  90#include <asm/uaccess.h>
  91#include <asm/unistd.h>
  92
  93#include <net/compat.h>
  94
  95#include <net/sock.h>
  96#include <linux/netfilter.h>
  97
  98static int sock_no_open(struct inode *irrelevant, struct file *dontcare);
  99static ssize_t sock_aio_read(struct kiocb *iocb, char __user *buf,
 100                         size_t size, loff_t pos);
 101static ssize_t sock_aio_write(struct kiocb *iocb, const char __user *buf,
 102                          size_t size, loff_t pos);
 103static int sock_mmap(struct file *file, struct vm_area_struct * vma);
 104
 105static int sock_close(struct inode *inode, struct file *file);
 106static unsigned int sock_poll(struct file *file,
 107                              struct poll_table_struct *wait);
 108static long sock_ioctl(struct file *file,
 109                      unsigned int cmd, unsigned long arg);
 110#ifdef CONFIG_COMPAT
 111static long compat_sock_ioctl(struct file *file,
 112                      unsigned int cmd, unsigned long arg);
 113#endif
 114static int sock_fasync(int fd, struct file *filp, int on);
 115static ssize_t sock_readv(struct file *file, const struct iovec *vector,
 116                          unsigned long count, loff_t *ppos);
 117static ssize_t sock_writev(struct file *file, const struct iovec *vector,
 118                          unsigned long count, loff_t *ppos);
 119static ssize_t sock_sendpage(struct file *file, struct page *page,
 120                             int offset, size_t size, loff_t *ppos, int more);
 121
 122/*
 123 *      Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
 124 *      in the operation structures but are done directly via the socketcall() multiplexor.
 125 */
 126
 127struct file_operations socket_file_ops = {
 128        .owner =        THIS_MODULE,
 129        .llseek =       no_llseek,
 130        .aio_read =     sock_aio_read,
 131        .aio_write =    sock_aio_write,
 132        .poll =         sock_poll,
 133        .unlocked_ioctl = sock_ioctl,
 134#ifdef CONFIG_COMPAT
 135        .compat_ioctl = compat_sock_ioctl,
 136#endif
 137        .mmap =         sock_mmap,
 138        .open =         sock_no_open,   /* special open code to disallow open via /proc */
 139        .release =      sock_close,
 140        .fasync =       sock_fasync,
 141        .readv =        sock_readv,
 142        .writev =       sock_writev,
 143        .sendpage =     sock_sendpage,
 144        .splice_write = generic_splice_sendpage,
 145};
 146
 147/*
 148 *      The protocol list. Each protocol is registered in here.
 149 */
 150
 151static struct net_proto_family *net_families[NPROTO];
 152
 153#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
 154static atomic_t net_family_lockct = ATOMIC_INIT(0);
 155static DEFINE_SPINLOCK(net_family_lock);
 156
 157/* The strategy is: modifications net_family vector are short, do not
 158   sleep and veeery rare, but read access should be free of any exclusive
 159   locks.
 160 */
 161
 162static void net_family_write_lock(void)
 163{
 164        spin_lock(&net_family_lock);
 165        while (atomic_read(&net_family_lockct) != 0) {
 166                spin_unlock(&net_family_lock);
 167
 168                yield();
 169
 170                spin_lock(&net_family_lock);
 171        }
 172}
 173
 174static __inline__ void net_family_write_unlock(void)
 175{
 176        spin_unlock(&net_family_lock);
 177}
 178
 179static __inline__ void net_family_read_lock(void)
 180{
 181        atomic_inc(&net_family_lockct);
 182        spin_unlock_wait(&net_family_lock);
 183}
 184
 185static __inline__ void net_family_read_unlock(void)
 186{
 187        atomic_dec(&net_family_lockct);
 188}
 189
 190#else
 191#define net_family_write_lock() do { } while(0)
 192#define net_family_write_unlock() do { } while(0)
 193#define net_family_read_lock() do { } while(0)
 194#define net_family_read_unlock() do { } while(0)
 195#endif
 196
 197
 198/*
 199 *      Statistics counters of the socket lists
 200 */
 201
 202static DEFINE_PER_CPU(int, sockets_in_use) = 0;
 203
 204/*
 205 *      Support routines. Move socket addresses back and forth across the kernel/user
 206 *      divide and look after the messy bits.
 207 */
 208
 209#define MAX_SOCK_ADDR   128             /* 108 for Unix domain - 
 210                                           16 for IP, 16 for IPX,
 211                                           24 for IPv6,
 212                                           about 80 for AX.25 
 213                                           must be at least one bigger than
 214                                           the AF_UNIX size (see net/unix/af_unix.c
 215                                           :unix_mkname()).  
 216                                         */
 217                                         
 218/**
 219 *      move_addr_to_kernel     -       copy a socket address into kernel space
 220 *      @uaddr: Address in user space
 221 *      @kaddr: Address in kernel space
 222 *      @ulen: Length in user space
 223 *
 224 *      The address is copied into kernel space. If the provided address is
 225 *      too long an error code of -EINVAL is returned. If the copy gives
 226 *      invalid addresses -EFAULT is returned. On a success 0 is returned.
 227 */
 228
 229int move_addr_to_kernel(void __user *uaddr, int ulen, void *kaddr)
 230{
 231        if(ulen<0||ulen>MAX_SOCK_ADDR)
 232                return -EINVAL;
 233        if(ulen==0)
 234                return 0;
 235        if(copy_from_user(kaddr,uaddr,ulen))
 236                return -EFAULT;
 237        return audit_sockaddr(ulen, kaddr);
 238}
 239
 240/**
 241 *      move_addr_to_user       -       copy an address to user space
 242 *      @kaddr: kernel space address
 243 *      @klen: length of address in kernel
 244 *      @uaddr: user space address
 245 *      @ulen: pointer to user length field
 246 *
 247 *      The value pointed to by ulen on entry is the buffer length available.
 248 *      This is overwritten with the buffer space used. -EINVAL is returned
 249 *      if an overlong buffer is specified or a negative buffer size. -EFAULT
 250 *      is returned if either the buffer or the length field are not
 251 *      accessible.
 252 *      After copying the data up to the limit the user specifies, the true
 253 *      length of the data is written over the length limit the user
 254 *      specified. Zero is returned for a success.
 255 */
 256 
 257int move_addr_to_user(void *kaddr, int klen, void __user *uaddr, int __user *ulen)
 258{
 259        int err;
 260        int len;
 261
 262        if((err=get_user(len, ulen)))
 263                return err;
 264        if(len>klen)
 265                len=klen;
 266        if(len<0 || len> MAX_SOCK_ADDR)
 267                return -EINVAL;
 268        if(len)
 269        {
 270                if (audit_sockaddr(klen, kaddr))
 271                        return -ENOMEM;
 272                if(copy_to_user(uaddr,kaddr,len))
 273                        return -EFAULT;
 274        }
 275        /*
 276         *      "fromlen shall refer to the value before truncation.."
 277         *                      1003.1g
 278         */
 279        return __put_user(klen, ulen);
 280}
 281
 282#define SOCKFS_MAGIC 0x534F434B
 283
 284static kmem_cache_t * sock_inode_cachep __read_mostly;
 285
 286static struct inode *sock_alloc_inode(struct super_block *sb)
 287{
 288        struct socket_alloc *ei;
 289        ei = (struct socket_alloc *)kmem_cache_alloc(sock_inode_cachep, SLAB_KERNEL);
 290        if (!ei)
 291                return NULL;
 292        init_waitqueue_head(&ei->socket.wait);
 293        
 294        ei->socket.fasync_list = NULL;
 295        ei->socket.state = SS_UNCONNECTED;
 296        ei->socket.flags = 0;
 297        ei->socket.ops = NULL;
 298        ei->socket.sk = NULL;
 299        ei->socket.file = NULL;
 300        ei->socket.flags = 0;
 301
 302        return &ei->vfs_inode;
 303}
 304
 305static void sock_destroy_inode(struct inode *inode)
 306{
 307        kmem_cache_free(sock_inode_cachep,
 308                        container_of(inode, struct socket_alloc, vfs_inode));
 309}
 310
 311static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
 312{
 313        struct socket_alloc *ei = (struct socket_alloc *) foo;
 314
 315        if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
 316            SLAB_CTOR_CONSTRUCTOR)
 317                inode_init_once(&ei->vfs_inode);
 318}
 319 
 320static int init_inodecache(void)
 321{
 322        sock_inode_cachep = kmem_cache_create("sock_inode_cache",
 323                                sizeof(struct socket_alloc),
 324                                0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
 325                                        SLAB_MEM_SPREAD),
 326                                init_once, NULL);
 327        if (sock_inode_cachep == NULL)
 328                return -ENOMEM;
 329        return 0;
 330}
 331
 332static struct super_operations sockfs_ops = {
 333        .alloc_inode =  sock_alloc_inode,
 334        .destroy_inode =sock_destroy_inode,
 335        .statfs =       simple_statfs,
 336};
 337
 338static int sockfs_get_sb(struct file_system_type *fs_type,
 339        int flags, const char *dev_name, void *data, struct vfsmount *mnt)
 340{
 341        return get_sb_pseudo(fs_type, "socket:", &sockfs_ops, SOCKFS_MAGIC,
 342                             mnt);
 343}
 344
 345static struct vfsmount *sock_mnt __read_mostly;
 346
 347static struct file_system_type sock_fs_type = {
 348        .name =         "sockfs",
 349        .get_sb =       sockfs_get_sb,
 350        .kill_sb =      kill_anon_super,
 351};
 352static int sockfs_delete_dentry(struct dentry *dentry)
 353{
 354        return 1;
 355}
 356static struct dentry_operations sockfs_dentry_operations = {
 357        .d_delete =     sockfs_delete_dentry,
 358};
 359
 360/*
 361 *      Obtains the first available file descriptor and sets it up for use.
 362 *
 363 *      These functions create file structures and maps them to fd space
 364 *      of the current process. On success it returns file descriptor
 365 *      and file struct implicitly stored in sock->file.
 366 *      Note that another thread may close file descriptor before we return
 367 *      from this function. We use the fact that now we do not refer
 368 *      to socket after mapping. If one day we will need it, this
 369 *      function will increment ref. count on file by 1.
 370 *
 371 *      In any case returned fd MAY BE not valid!
 372 *      This race condition is unavoidable
 373 *      with shared fd spaces, we cannot solve it inside kernel,
 374 *      but we take care of internal coherence yet.
 375 */
 376
 377static int sock_alloc_fd(struct file **filep)
 378{
 379        int fd;
 380
 381        fd = get_unused_fd();
 382        if (likely(fd >= 0)) {
 383                struct file *file = get_empty_filp();
 384
 385                *filep = file;
 386                if (unlikely(!file)) {
 387                        put_unused_fd(fd);
 388                        return -ENFILE;
 389                }
 390        } else
 391                *filep = NULL;
 392        return fd;
 393}
 394
 395static int sock_attach_fd(struct socket *sock, struct file *file)
 396{
 397        struct qstr this;
 398        char name[32];
 399
 400        this.len = sprintf(name, "[%lu]", SOCK_INODE(sock)->i_ino);
 401        this.name = name;
 402        this.hash = SOCK_INODE(sock)->i_ino;
 403
 404        file->f_dentry = d_alloc(sock_mnt->mnt_sb->s_root, &this);
 405        if (unlikely(!file->f_dentry))
 406                return -ENOMEM;
 407
 408        file->f_dentry->d_op = &sockfs_dentry_operations;
 409        d_add(file->f_dentry, SOCK_INODE(sock));
 410        file->f_vfsmnt = mntget(sock_mnt);
 411        file->f_mapping = file->f_dentry->d_inode->i_mapping;
 412
 413        sock->file = file;
 414        file->f_op = SOCK_INODE(sock)->i_fop = &socket_file_ops;
 415        file->f_mode = FMODE_READ | FMODE_WRITE;
 416        file->f_flags = O_RDWR;
 417        file->f_pos = 0;
 418        file->private_data = sock;
 419
 420        return 0;
 421}
 422
 423int sock_map_fd(struct socket *sock)
 424{
 425        struct file *newfile;
 426        int fd = sock_alloc_fd(&newfile);
 427
 428        if (likely(fd >= 0)) {
 429                int err = sock_attach_fd(sock, newfile);
 430
 431                if (unlikely(err < 0)) {
 432                        put_filp(newfile);
 433                        put_unused_fd(fd);
 434                        return err;
 435                }
 436                fd_install(fd, newfile);
 437        }
 438        return fd;
 439}
 440
 441static struct socket *sock_from_file(struct file *file, int *err)
 442{
 443        struct inode *inode;
 444        struct socket *sock;
 445
 446        if (file->f_op == &socket_file_ops)
 447                return file->private_data;      /* set in sock_map_fd */
 448
 449        inode = file->f_dentry->d_inode;
 450        if (!S_ISSOCK(inode->i_mode)) {
 451                *err = -ENOTSOCK;
 452                return NULL;
 453        }
 454
 455        sock = SOCKET_I(inode);
 456        if (sock->file != file) {
 457                printk(KERN_ERR "socki_lookup: socket file changed!\n");
 458                sock->file = file;
 459        }
 460        return sock;
 461}
 462
 463/**
 464 *      sockfd_lookup   -       Go from a file number to its socket slot
 465 *      @fd: file handle
 466 *      @err: pointer to an error code return
 467 *
 468 *      The file handle passed in is locked and the socket it is bound
 469 *      too is returned. If an error occurs the err pointer is overwritten
 470 *      with a negative errno code and NULL is returned. The function checks
 471 *      for both invalid handles and passing a handle which is not a socket.
 472 *
 473 *      On a success the socket object pointer is returned.
 474 */
 475
 476struct socket *sockfd_lookup(int fd, int *err)
 477{
 478        struct file *file;
 479        struct socket *sock;
 480
 481        if (!(file = fget(fd))) {
 482                *err = -EBADF;
 483                return NULL;
 484        }
 485        sock = sock_from_file(file, err);
 486        if (!sock)
 487                fput(file);
 488        return sock;
 489}
 490
 491static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed)
 492{
 493        struct file *file;
 494        struct socket *sock;
 495
 496        *err = -EBADF;
 497        file = fget_light(fd, fput_needed);
 498        if (file) {
 499                sock = sock_from_file(file, err);
 500                if (sock)
 501                        return sock;
 502                fput_light(file, *fput_needed);
 503        }
 504        return NULL;
 505}
 506
 507/**
 508 *      sock_alloc      -       allocate a socket
 509 *      
 510 *      Allocate a new inode and socket object. The two are bound together
 511 *      and initialised. The socket is then returned. If we are out of inodes
 512 *      NULL is returned.
 513 */
 514
 515struct socket *sock_alloc(void)
 516{
 517        struct inode * inode;
 518        struct socket * sock;
 519
 520        inode = new_inode(sock_mnt->mnt_sb);
 521        if (!inode)
 522                return NULL;
 523
 524        sock = SOCKET_I(inode);
 525
 526        inode->i_mode = S_IFSOCK|S_IRWXUGO;
 527        inode->i_uid = current->fsuid;
 528        inode->i_gid = current->fsgid;
 529
 530        get_cpu_var(sockets_in_use)++;
 531        put_cpu_var(sockets_in_use);
 532        return sock;
 533}
 534
 535EXPORT_SYMBOL_GPL(sock_alloc);
 536
 537/*
 538 *      In theory you can't get an open on this inode, but /proc provides
 539 *      a back door. Remember to keep it shut otherwise you'll let the
 540 *      creepy crawlies in.
 541 */
 542  
 543static int sock_no_open(struct inode *irrelevant, struct file *dontcare)
 544{
 545        return -ENXIO;
 546}
 547
 548const struct file_operations bad_sock_fops = {
 549        .owner = THIS_MODULE,
 550        .open = sock_no_open,
 551};
 552
 553/**
 554 *      sock_release    -       close a socket
 555 *      @sock: socket to close
 556 *
 557 *      The socket is released from the protocol stack if it has a release
 558 *      callback, and the inode is then released if the socket is bound to
 559 *      an inode not a file. 
 560 */
 561 
 562void sock_release(struct socket *sock)
 563{
 564        if (sock->ops) {
 565                struct module *owner = sock->ops->owner;
 566
 567                sock->ops->release(sock);
 568                sock->ops = NULL;
 569                module_put(owner);
 570        }
 571
 572        if (sock->fasync_list)
 573                printk(KERN_ERR "sock_release: fasync list not empty!\n");
 574
 575        get_cpu_var(sockets_in_use)--;
 576        put_cpu_var(sockets_in_use);
 577        if (!sock->file) {
 578                iput(SOCK_INODE(sock));
 579                return;
 580        }
 581        sock->file=NULL;
 582}
 583
 584static inline int __sock_sendmsg(struct kiocb *iocb, struct socket *sock, 
 585                                 struct msghdr *msg, size_t size)
 586{
 587        struct sock_iocb *si = kiocb_to_siocb(iocb);
 588        int err;
 589
 590        si->sock = sock;
 591        si->scm = NULL;
 592        si->msg = msg;
 593        si->size = size;
 594
 595        err = security_socket_sendmsg(sock, msg, size);
 596        if (err)
 597                return err;
 598
 599        return sock->ops->sendmsg(iocb, sock, msg, size);
 600}
 601
 602int sock_sendmsg(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(&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}
 633
 634static inline int __sock_recvmsg(struct kiocb *iocb, struct socket *sock, 
 635                                 struct msghdr *msg, size_t size, int flags)
 636{
 637        int err;
 638        struct sock_iocb *si = kiocb_to_siocb(iocb);
 639
 640        si->sock = sock;
 641        si->scm = NULL;
 642        si->msg = msg;
 643        si->size = size;
 644        si->flags = flags;
 645
 646        err = security_socket_recvmsg(sock, msg, size, flags);
 647        if (err)
 648                return err;
 649
 650        return sock->ops->recvmsg(iocb, sock, msg, size, flags);
 651}
 652
 653int sock_recvmsg(struct socket *sock, struct msghdr *msg, 
 654                 size_t size, int flags)
 655{
 656        struct kiocb iocb;
 657        struct sock_iocb siocb;
 658        int ret;
 659
 660        init_sync_kiocb(&iocb, NULL);
 661        iocb.private = &siocb;
 662        ret = __sock_recvmsg(&iocb, sock, msg, size, flags);
 663        if (-EIOCBQUEUED == ret)
 664                ret = wait_on_sync_kiocb(&iocb);
 665        return ret;
 666}
 667
 668int kernel_recvmsg(struct socket *sock, struct msghdr *msg, 
 669                   struct kvec *vec, size_t num,
 670                   size_t size, int flags)
 671{
 672        mm_segment_t oldfs = get_fs();
 673        int result;
 674
 675        set_fs(KERNEL_DS);
 676        /*
 677         * the following is safe, since for compiler definitions of kvec and
 678         * iovec are identical, yielding the same in-core layout and alignment
 679         */
 680        msg->msg_iov = (struct iovec *)vec,
 681        msg->msg_iovlen = num;
 682        result = sock_recvmsg(sock, msg, size, flags);
 683        set_fs(oldfs);
 684        return result;
 685}
 686
 687static void sock_aio_dtor(struct kiocb *iocb)
 688{
 689        kfree(iocb->private);
 690}
 691
 692static ssize_t sock_sendpage(struct file *file, struct page *page,
 693                             int offset, size_t size, loff_t *ppos, int more)
 694{
 695        struct socket *sock;
 696        int flags;
 697
 698        sock = file->private_data;
 699
 700        flags = !(file->f_flags & O_NONBLOCK) ? 0 : MSG_DONTWAIT;
 701        if (more)
 702                flags |= MSG_MORE;
 703
 704        return sock->ops->sendpage(sock, page, offset, size, flags);
 705}
 706
 707static struct sock_iocb *alloc_sock_iocb(struct kiocb *iocb,
 708                char __user *ubuf, size_t size, struct sock_iocb *siocb)
 709{
 710        if (!is_sync_kiocb(iocb)) {
 711                siocb = kmalloc(sizeof(*siocb), GFP_KERNEL);
 712                if (!siocb)
 713                        return NULL;
 714                iocb->ki_dtor = sock_aio_dtor;
 715        }
 716
 717        siocb->kiocb = iocb;
 718        siocb->async_iov.iov_base = ubuf;
 719        siocb->async_iov.iov_len = size;
 720
 721        iocb->private = siocb;
 722        return siocb;
 723}
 724
 725static ssize_t do_sock_read(struct msghdr *msg, struct kiocb *iocb,
 726                struct file *file, struct iovec *iov, unsigned long nr_segs)
 727{
 728        struct socket *sock = file->private_data;
 729        size_t size = 0;
 730        int i;
 731
 732        for (i = 0 ; i < nr_segs ; i++)
 733                size += iov[i].iov_len;
 734
 735        msg->msg_name = NULL;
 736        msg->msg_namelen = 0;
 737        msg->msg_control = NULL;
 738        msg->msg_controllen = 0;
 739        msg->msg_iov = (struct iovec *) iov;
 740        msg->msg_iovlen = nr_segs;
 741        msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
 742
 743        return __sock_recvmsg(iocb, sock, msg, size, msg->msg_flags);
 744}
 745
 746static ssize_t sock_readv(struct file *file, const struct iovec *iov,
 747                          unsigned long nr_segs, loff_t *ppos)
 748{
 749        struct kiocb iocb;
 750        struct sock_iocb siocb;
 751        struct msghdr msg;
 752        int ret;
 753
 754        init_sync_kiocb(&iocb, NULL);
 755        iocb.private = &siocb;
 756
 757        ret = do_sock_read(&msg, &iocb, file, (struct iovec *)iov, nr_segs);
 758        if (-EIOCBQUEUED == ret)
 759                ret = wait_on_sync_kiocb(&iocb);
 760        return ret;
 761}
 762
 763static ssize_t sock_aio_read(struct kiocb *iocb, char __user *ubuf,
 764                         size_t count, loff_t pos)
 765{
 766        struct sock_iocb siocb, *x;
 767
 768        if (pos != 0)
 769                return -ESPIPE;
 770        if (count == 0)         /* Match SYS5 behaviour */
 771                return 0;
 772
 773        x = alloc_sock_iocb(iocb, ubuf, count, &siocb);
 774        if (!x)
 775                return -ENOMEM;
 776        return do_sock_read(&x->async_msg, iocb, iocb->ki_filp,
 777                        &x->async_iov, 1);
 778}
 779
 780static ssize_t do_sock_write(struct msghdr *msg, struct kiocb *iocb,
 781                struct file *file, struct iovec *iov, unsigned long nr_segs)
 782{
 783        struct socket *sock = file->private_data;
 784        size_t size = 0;
 785        int i;
 786
 787        for (i = 0 ; i < nr_segs ; i++)
 788                size += iov[i].iov_len;
 789
 790        msg->msg_name = NULL;
 791        msg->msg_namelen = 0;
 792        msg->msg_control = NULL;
 793        msg->msg_controllen = 0;
 794        msg->msg_iov = (struct iovec *) iov;
 795        msg->msg_iovlen = nr_segs;
 796        msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
 797        if (sock->type == SOCK_SEQPACKET)
 798                msg->msg_flags |= MSG_EOR;
 799
 800        return __sock_sendmsg(iocb, sock, msg, size);
 801}
 802
 803static ssize_t sock_writev(struct file *file, const struct iovec *iov,
 804                           unsigned long nr_segs, loff_t *ppos)
 805{
 806        struct msghdr msg;
 807        struct kiocb iocb;
 808        struct sock_iocb siocb;
 809        int ret;
 810
 811        init_sync_kiocb(&iocb, NULL);
 812        iocb.private = &siocb;
 813
 814        ret = do_sock_write(&msg, &iocb, file, (struct iovec *)iov, nr_segs);
 815        if (-EIOCBQUEUED == ret)
 816                ret = wait_on_sync_kiocb(&iocb);
 817        return ret;
 818}
 819
 820static ssize_t sock_aio_write(struct kiocb *iocb, const char __user *ubuf,
 821                          size_t count, loff_t pos)
 822{
 823        struct sock_iocb siocb, *x;
 824
 825        if (pos != 0)
 826                return -ESPIPE;
 827        if (count == 0)         /* Match SYS5 behaviour */
 828                return 0;
 829
 830        x = alloc_sock_iocb(iocb, (void __user *)ubuf, count, &siocb);
 831        if (!x)
 832                return -ENOMEM;
 833
 834        return do_sock_write(&x->async_msg, iocb, iocb->ki_filp,
 835                        &x->async_iov, 1);
 836}
 837
 838
 839/*
 840 * Atomic setting of ioctl hooks to avoid race
 841 * with module unload.
 842 */
 843
 844static DEFINE_MUTEX(br_ioctl_mutex);
 845static int (*br_ioctl_hook)(unsigned int cmd, void __user *arg) = NULL;
 846
 847void brioctl_set(int (*hook)(unsigned int, void __user *))
 848{
 849        mutex_lock(&br_ioctl_mutex);
 850        br_ioctl_hook = hook;
 851        mutex_unlock(&br_ioctl_mutex);
 852}
 853EXPORT_SYMBOL(brioctl_set);
 854
 855static DEFINE_MUTEX(vlan_ioctl_mutex);
 856static int (*vlan_ioctl_hook)(void __user *arg);
 857
 858void vlan_ioctl_set(int (*hook)(void __user *))
 859{
 860        mutex_lock(&vlan_ioctl_mutex);
 861        vlan_ioctl_hook = hook;
 862        mutex_unlock(&vlan_ioctl_mutex);
 863}
 864EXPORT_SYMBOL(vlan_ioctl_set);
 865
 866static DEFINE_MUTEX(dlci_ioctl_mutex);
 867static int (*dlci_ioctl_hook)(unsigned int, void __user *);
 868
 869void dlci_ioctl_set(int (*hook)(unsigned int, void __user *))
 870{
 871        mutex_lock(&dlci_ioctl_mutex);
 872        dlci_ioctl_hook = hook;
 873        mutex_unlock(&dlci_ioctl_mutex);
 874}
 875EXPORT_SYMBOL(dlci_ioctl_set);
 876
 877/*
 878 *      With an ioctl, arg may well be a user mode pointer, but we don't know
 879 *      what to do with it - that's up to the protocol still.
 880 */
 881
 882static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
 883{
 884        struct socket *sock;
 885        void __user *argp = (void __user *)arg;
 886        int pid, err;
 887
 888        sock = file->private_data;
 889        if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
 890                err = dev_ioctl(cmd, argp);
 891        } else
 892#ifdef CONFIG_WIRELESS_EXT
 893        if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
 894                err = dev_ioctl(cmd, argp);
 895        } else
 896#endif  /* CONFIG_WIRELESS_EXT */
 897        switch (cmd) {
 898                case FIOSETOWN:
 899                case SIOCSPGRP:
 900                        err = -EFAULT;
 901                        if (get_user(pid, (int __user *)argp))
 902                                break;
 903                        err = f_setown(sock->file, pid, 1);
 904                        break;
 905                case FIOGETOWN:
 906                case SIOCGPGRP:
 907                        err = put_user(sock->file->f_owner.pid, (int __user *)argp);
 908                        break;
 909                case SIOCGIFBR:
 910                case SIOCSIFBR:
 911                case SIOCBRADDBR:
 912                case SIOCBRDELBR:
 913                        err = -ENOPKG;
 914                        if (!br_ioctl_hook)
 915                                request_module("bridge");
 916
 917                        mutex_lock(&br_ioctl_mutex);
 918                        if (br_ioctl_hook) 
 919                                err = br_ioctl_hook(cmd, argp);
 920                        mutex_unlock(&br_ioctl_mutex);
 921                        break;
 922                case SIOCGIFVLAN:
 923                case SIOCSIFVLAN:
 924                        err = -ENOPKG;
 925                        if (!vlan_ioctl_hook)
 926                                request_module("8021q");
 927
 928                        mutex_lock(&vlan_ioctl_mutex);
 929                        if (vlan_ioctl_hook)
 930                                err = vlan_ioctl_hook(argp);
 931                        mutex_unlock(&vlan_ioctl_mutex);
 932                        break;
 933                case SIOCGIFDIVERT:
 934                case SIOCSIFDIVERT:
 935                /* Convert this to call through a hook */
 936                        err = divert_ioctl(cmd, argp);
 937                        break;
 938                case SIOCADDDLCI:
 939                case SIOCDELDLCI:
 940                        err = -ENOPKG;
 941                        if (!dlci_ioctl_hook)
 942                                request_module("dlci");
 943
 944                        if (dlci_ioctl_hook) {
 945                                mutex_lock(&dlci_ioctl_mutex);
 946                                err = dlci_ioctl_hook(cmd, argp);
 947                                mutex_unlock(&dlci_ioctl_mutex);
 948                        }
 949                        break;
 950                default:
 951                        err = sock->ops->ioctl(sock, cmd, arg);
 952
 953                        /*
 954                         * If this ioctl is unknown try to hand it down
 955                         * to the NIC driver.
 956                         */
 957                        if (err == -ENOIOCTLCMD)
 958                                err = dev_ioctl(cmd, argp);
 959                        break;
 960        }
 961        return err;
 962}
 963
 964int sock_create_lite(int family, int type, int protocol, struct socket **res)
 965{
 966        int err;
 967        struct socket *sock = NULL;
 968        
 969        err = security_socket_create(family, type, protocol, 1);
 970        if (err)
 971                goto out;
 972
 973        sock = sock_alloc();
 974        if (!sock) {
 975                err = -ENOMEM;
 976                goto out;
 977        }
 978
 979        sock->type = type;
 980        err = security_socket_post_create(sock, family, type, protocol, 1);
 981        if (err)
 982                goto out_release;
 983
 984out:
 985        *res = sock;
 986        return err;
 987out_release:
 988        sock_release(sock);
 989        sock = NULL;
 990        goto out;
 991}
 992
 993/* No kernel lock held - perfect */
 994static unsigned int sock_poll(struct file *file, poll_table * wait)
 995{
 996        struct socket *sock;
 997
 998        /*
 999         *      We can't return errors to poll, so it's either yes or no. 
1000         */
1001        sock = file->private_data;
1002        return sock->ops->poll(file, sock, wait);
1003}
1004
1005static int sock_mmap(struct file * file, struct vm_area_struct * vma)
1006{
1007        struct socket *sock = file->private_data;
1008
1009        return sock->ops->mmap(file, sock, vma);
1010}
1011
1012static int sock_close(struct inode *inode, struct file *filp)
1013{
1014        /*
1015         *      It was possible the inode is NULL we were 
1016         *      closing an unfinished socket. 
1017         */
1018
1019        if (!inode)
1020        {
1021                printk(KERN_DEBUG "sock_close: NULL inode\n");
1022                return 0;
1023        }
1024        sock_fasync(-1, filp, 0);
1025        sock_release(SOCKET_I(inode));
1026        return 0;
1027}
1028
1029/*
1030 *      Update the socket async list
1031 *
1032 *      Fasync_list locking strategy.
1033 *
1034 *      1. fasync_list is modified only under process context socket lock
1035 *         i.e. under semaphore.
1036 *      2. fasync_list is used under read_lock(&sk->sk_callback_lock)
1037 *         or under socket lock.
1038 *      3. fasync_list can be used from softirq context, so that
1039 *         modification under socket lock have to be enhanced with
1040 *         write_lock_bh(&sk->sk_callback_lock).
1041 *                                                      --ANK (990710)
1042 */
1043
1044static int sock_fasync(int fd, struct file *filp, int on)
1045{
1046        struct fasync_struct *fa, *fna=NULL, **prev;
1047        struct socket *sock;
1048        struct sock *sk;
1049
1050        if (on)
1051        {
1052                fna = kmalloc(sizeof(struct fasync_struct), GFP_KERNEL);
1053                if(fna==NULL)
1054                        return -ENOMEM;
1055        }
1056
1057        sock = filp->private_data;
1058
1059        if ((sk=sock->sk) == NULL) {
1060                kfree(fna);
1061                return -EINVAL;
1062        }
1063
1064        lock_sock(sk);
1065
1066        prev=&(sock->fasync_list);
1067
1068        for (fa=*prev; fa!=NULL; prev=&fa->fa_next,fa=*prev)
1069                if (fa->fa_file==filp)
1070                        break;
1071
1072        if(on)
1073        {
1074                if(fa!=NULL)
1075                {
1076                        write_lock_bh(&sk->sk_callback_lock);
1077                        fa->fa_fd=fd;
1078                        write_unlock_bh(&sk->sk_callback_lock);
1079
1080                        kfree(fna);
1081                        goto out;
1082                }
1083                fna->fa_file=filp;
1084                fna->fa_fd=fd;
1085                fna->magic=FASYNC_MAGIC;
1086                fna->fa_next=sock->fasync_list;
1087                write_lock_bh(&sk->sk_callback_lock);
1088                sock->fasync_list=fna;
1089                write_unlock_bh(&sk->sk_callback_lock);
1090        }
1091        else
1092        {
1093                if (fa!=NULL)
1094                {
1095                        write_lock_bh(&sk->sk_callback_lock);
1096                        *prev=fa->fa_next;
1097                        write_unlock_bh(&sk->sk_callback_lock);
1098                        kfree(fa);
1099                }
1100        }
1101
1102out:
1103        if (sock->sk != sk)
1104                BUG();
1105        release_sock(sock->sk);
1106        return 0;
1107}
1108
1109/* This function may be called only under socket lock or callback_lock */
1110
1111int sock_wake_async(struct socket *sock, int how, int band)
1112{
1113        if (!sock || !sock->fasync_list)
1114                return -1;
1115        switch (how)
1116        {
1117        case 1:
1118                
1119                if (test_bit(SOCK_ASYNC_WAITDATA, &sock->flags))
1120                        break;
1121                goto call_kill;
1122        case 2:
1123                if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags))
1124                        break;
1125                /* fall through */
1126        case 0:
1127        call_kill:
1128                __kill_fasync(sock->fasync_list, SIGIO, band);
1129                break;
1130        case 3:
1131                __kill_fasync(sock->fasync_list, SIGURG, band);
1132        }
1133        return 0;
1134}
1135
1136static int __sock_create(int family, int type, int protocol, struct socket **res, int kern)
1137{
1138        int err;
1139        struct socket *sock;
1140
1141        /*
1142         *      Check protocol is in range
1143         */
1144        if (family < 0 || family >= NPROTO)
1145                return -EAFNOSUPPORT;
1146        if (type < 0 || type >= SOCK_MAX)
1147                return -EINVAL;
1148
1149        /* Compatibility.
1150
1151           This uglymoron is moved from INET layer to here to avoid
1152           deadlock in module load.
1153         */
1154        if (family == PF_INET && type == SOCK_PACKET) {
1155                static int warned; 
1156                if (!warned) {
1157                        warned = 1;
1158                        printk(KERN_INFO "%s uses obsolete (PF_INET,SOCK_PACKET)\n", current->comm);
1159                }
1160                family = PF_PACKET;
1161        }
1162
1163        err = security_socket_create(family, type, protocol, kern);
1164        if (err)
1165                return err;
1166                
1167#if defined(CONFIG_KMOD)
1168        /* Attempt to load a protocol module if the find failed. 
1169         * 
1170         * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user 
1171         * requested real, full-featured networking support upon configuration.
1172         * Otherwise module support will break!
1173         */
1174        if (net_families[family]==NULL)
1175        {
1176                request_module("net-pf-%d",family);
1177        }
1178#endif
1179
1180        net_family_read_lock();
1181        if (net_families[family] == NULL) {
1182                err = -EAFNOSUPPORT;
1183                goto out;
1184        }
1185
1186/*
1187 *      Allocate the socket and allow the family to set things up. if
1188 *      the protocol is 0, the family is instructed to select an appropriate
1189 *      default.
1190 */
1191
1192        if (!(sock = sock_alloc())) {
1193                if (net_ratelimit())
1194                        printk(KERN_WARNING "socket: no more sockets\n");
1195                err = -ENFILE;          /* Not exactly a match, but its the
1196                                           closest posix thing */
1197                goto out;
1198        }
1199
1200        sock->type  = type;
1201
1202        /*
1203         * We will call the ->create function, that possibly is in a loadable
1204         * module, so we have to bump that loadable module refcnt first.
1205         */
1206        err = -EAFNOSUPPORT;
1207        if (!try_module_get(net_families[family]->owner))
1208                goto out_release;
1209
1210        if ((err = net_families[family]->create(sock, protocol)) < 0) {
1211                sock->ops = NULL;
1212                goto out_module_put;
1213        }
1214
1215        /*
1216         * Now to bump the refcnt of the [loadable] module that owns this
1217         * socket at sock_release time we decrement its refcnt.
1218         */
1219        if (!try_module_get(sock->ops->owner)) {
1220                sock->ops = NULL;
1221                goto out_module_put;
1222        }
1223        /*
1224         * Now that we're done with the ->create function, the [loadable]
1225         * module can have its refcnt decremented
1226         */
1227        module_put(net_families[family]->owner);
1228        *res = sock;
1229        err = security_socket_post_create(sock, family, type, protocol, kern);
1230        if (err)
1231                goto out_release;
1232
1233out:
1234        net_family_read_unlock();
1235        return err;
1236out_module_put:
1237        module_put(net_families[family]->owner);
1238out_release:
1239        sock_release(sock);
1240        goto out;
1241}
1242
1243int sock_create(int family, int type, int protocol, struct socket **res)
1244{
1245        return __sock_create(family, type, protocol, res, 0);
1246}
1247
1248int sock_create_kern(int family, int type, int protocol, struct socket **res)
1249{
1250        static struct lock_class_key sk_lock_internal_key;
1251        int ret;
1252        ret = __sock_create(family, type, protocol, res, 1);
1253        if (!ret)
1254                lockdep_set_class(&(*res)->sk->sk_lock.slock,
1255                        &sk_lock_internal_key);
1256        return ret;
1257}
1258
1259asmlinkage long sys_socket(int family, int type, int protocol)
1260{
1261        int retval;
1262        struct socket *sock;
1263
1264        retval = sock_create(family, type, protocol, &sock);
1265        if (retval < 0)
1266                goto out;
1267
1268        retval = sock_map_fd(sock);
1269        if (retval < 0)
1270                goto out_release;
1271
1272out:
1273        /* It may be already another descriptor 8) Not kernel problem. */
1274        return retval;
1275
1276out_release:
1277        sock_release(sock);
1278        return retval;
1279}
1280
1281/*
1282 *      Create a pair of connected sockets.
1283 */
1284
1285asmlinkage long sys_socketpair(int family, int type, int protocol, int __user *usockvec)
1286{
1287        struct socket *sock1, *sock2;
1288        int fd1, fd2, err;
1289
1290        /*
1291         * Obtain the first socket and check if the underlying protocol
1292         * supports the socketpair call.
1293         */
1294
1295        err = sock_create(family, type, protocol, &sock1);
1296        if (err < 0)
1297                goto out;
1298
1299        err = sock_create(family, type, protocol, &sock2);
1300        if (err < 0)
1301                goto out_release_1;
1302
1303        err = sock1->ops->socketpair(sock1, sock2);
1304        if (err < 0) 
1305                goto out_release_both;
1306
1307        fd1 = fd2 = -1;
1308
1309        err = sock_map_fd(sock1);
1310        if (err < 0)
1311                goto out_release_both;
1312        fd1 = err;
1313
1314        err = sock_map_fd(sock2);
1315        if (err < 0)
1316                goto out_close_1;
1317        fd2 = err;
1318
1319        /* fd1 and fd2 may be already another descriptors.
1320         * Not kernel problem.
1321         */
1322
1323        err = put_user(fd1, &usockvec[0]); 
1324        if (!err)
1325                err = put_user(fd2, &usockvec[1]);
1326        if (!err)
1327                return 0;
1328
1329        sys_close(fd2);
1330        sys_close(fd1);
1331        return err;
1332
1333out_close_1:
1334        sock_release(sock2);
1335        sys_close(fd1);
1336        return err;
1337
1338out_release_both:
1339        sock_release(sock2);
1340out_release_1:
1341        sock_release(sock1);
1342out:
1343        return err;
1344}
1345
1346
1347/*
1348 *      Bind a name to a socket. Nothing much to do here since it's
1349 *      the protocol's responsibility to handle the local address.
1350 *
1351 *      We move the socket address to kernel space before we call
1352 *      the protocol layer (having also checked the address is ok).
1353 */
1354
1355asmlinkage long sys_bind(int fd, struct sockaddr __user *umyaddr, int addrlen)
1356{
1357        struct socket *sock;
1358        char address[MAX_SOCK_ADDR];
1359        int err, fput_needed;
1360
1361        if((sock = sockfd_lookup_light(fd, &err, &fput_needed))!=NULL)
1362        {
1363                if((err=move_addr_to_kernel(umyaddr,addrlen,address))>=0) {
1364                        err = security_socket_bind(sock, (struct sockaddr *)address, addrlen);
1365                        if (!err)
1366                                err = sock->ops->bind(sock,
1367                                        (struct sockaddr *)address, addrlen);
1368                }
1369                fput_light(sock->file, fput_needed);
1370        }                       
1371        return err;
1372}
1373
1374
1375/*
1376 *      Perform a listen. Basically, we allow the protocol to do anything
1377 *      necessary for a listen, and if that works, we mark the socket as
1378 *      ready for listening.
1379 */
1380
1381int sysctl_somaxconn = SOMAXCONN;
1382
1383asmlinkage long sys_listen(int fd, int backlog)
1384{
1385        struct socket *sock;
1386        int err, fput_needed;
1387        
1388        if ((sock = sockfd_lookup_light(fd, &err, &fput_needed)) != NULL) {
1389                if ((unsigned) backlog > sysctl_somaxconn)
1390                        backlog = sysctl_somaxconn;
1391
1392                err = security_socket_listen(sock, backlog);
1393                if (!err)
1394                        err = sock->ops->listen(sock, backlog);
1395
1396                fput_light(sock->file, fput_needed);
1397        }
1398        return err;
1399}
1400
1401
1402/*
1403 *      For accept, we attempt to create a new socket, set up the link
1404 *      with the client, wake up the client, then return the new
1405 *      connected fd. We collect the address of the connector in kernel
1406 *      space and move it to user at the very end. This is unclean because
1407 *      we open the socket then return an error.
1408 *
1409 *      1003.1g adds the ability to recvmsg() to query connection pending
1410 *      status to recvmsg. We need to add that support in a way thats
1411 *      clean when we restucture accept also.
1412 */
1413
1414asmlinkage long sys_accept(int fd, struct sockaddr __user *upeer_sockaddr, int __user *upeer_addrlen)
1415{
1416        struct socket *sock, *newsock;
1417        struct file *newfile;
1418        int err, len, newfd, fput_needed;
1419        char address[MAX_SOCK_ADDR];
1420
1421        sock = sockfd_lookup_light(fd, &err, &fput_needed);
1422        if (!sock)
1423                goto out;
1424
1425        err = -ENFILE;
1426        if (!(newsock = sock_alloc())) 
1427                goto out_put;
1428
1429        newsock->type = sock->type;
1430        newsock->ops = sock->ops;
1431
1432        /*
1433         * We don't need try_module_get here, as the listening socket (sock)
1434         * has the protocol module (sock->ops->owner) held.
1435         */
1436        __module_get(newsock->ops->owner);
1437
1438        newfd = sock_alloc_fd(&newfile);
1439        if (unlikely(newfd < 0)) {
1440                err = newfd;
1441                sock_release(newsock);
1442                goto out_put;
1443        }
1444
1445        err = sock_attach_fd(newsock, newfile);
1446        if (err < 0)
1447                goto out_fd;
1448
1449        err = security_socket_accept(sock, newsock);
1450        if (err)
1451                goto out_fd;
1452
1453        err = sock->ops->accept(sock, newsock, sock->file->f_flags);
1454        if (err < 0)
1455                goto out_fd;
1456
1457        if (upeer_sockaddr) {
1458                if(newsock->ops->getname(newsock, (struct sockaddr *)address, &len, 2)<0) {
1459                        err = -ECONNABORTED;
1460                        goto out_fd;
1461                }
1462                err = move_addr_to_user(address, len, upeer_sockaddr, upeer_addrlen);
1463                if (err < 0)
1464                        goto out_fd;
1465        }
1466
1467        /* File flags are not inherited via accept() unlike another OSes. */
1468
1469        fd_install(newfd, newfile);
1470        err = newfd;
1471
1472        security_socket_post_accept(sock, newsock);
1473
1474out_put:
1475        fput_light(sock->file, fput_needed);
1476out:
1477        return err;
1478out_fd:
1479        fput(newfile);
1480        put_unused_fd(newfd);
1481        goto out_put;
1482}
1483
1484
1485/*
1486 *      Attempt to connect to a socket with the server address.  The address
1487 *      is in user space so we verify it is OK and move it to kernel space.
1488 *
1489 *      For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
1490 *      break bindings
1491 *
1492 *      NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
1493 *      other SEQPACKET protocols that take time to connect() as it doesn't
1494 *      include the -EINPROGRESS status for such sockets.
1495 */
1496
1497asmlinkage long sys_connect(int fd, struct sockaddr __user *uservaddr, int addrlen)
1498{
1499        struct socket *sock;
1500        char address[MAX_SOCK_ADDR];
1501        int err, fput_needed;
1502
1503        sock = sockfd_lookup_light(fd, &err, &fput_needed);
1504        if (!sock)
1505                goto out;
1506        err = move_addr_to_kernel(uservaddr, addrlen, address);
1507        if (err < 0)
1508                goto out_put;
1509
1510        err = security_socket_connect(sock, (struct sockaddr *)address, addrlen);
1511        if (err)
1512                goto out_put;
1513
1514        err = sock->ops->connect(sock, (struct sockaddr *) address, addrlen,
1515                                 sock->file->f_flags);
1516out_put:
1517        fput_light(sock->file, fput_needed);
1518out:
1519        return err;
1520}
1521
1522/*
1523 *      Get the local address ('name') of a socket object. Move the obtained
1524 *      name to user space.
1525 */
1526
1527asmlinkage long sys_getsockname(int fd, struct sockaddr __user *usockaddr, int __user *usockaddr_len)
1528{
1529        struct socket *sock;
1530        char address[MAX_SOCK_ADDR];
1531        int len, err, fput_needed;
1532        
1533        sock = sockfd_lookup_light(fd, &err, &fput_needed);
1534        if (!sock)
1535                goto out;
1536
1537        err = security_socket_getsockname(sock);
1538        if (err)
1539                goto out_put;
1540
1541        err = sock->ops->getname(sock, (struct sockaddr *)address, &len, 0);
1542        if (err)
1543                goto out_put;
1544        err = move_addr_to_user(address, len, usockaddr, usockaddr_len);
1545
1546out_put:
1547        fput_light(sock->file, fput_needed);
1548out:
1549        return err;
1550}
1551
1552/*
1553 *      Get the remote address ('name') of a socket object. Move the obtained
1554 *      name to user space.
1555 */
1556
1557asmlinkage long sys_getpeername(int fd, struct sockaddr __user *usockaddr, int __user *usockaddr_len)
1558{
1559        struct socket *sock;
1560        char address[MAX_SOCK_ADDR];
1561        int len, err, fput_needed;
1562
1563        if ((sock = sockfd_lookup_light(fd, &err, &fput_needed)) != NULL) {
1564                err = security_socket_getpeername(sock);
1565                if (err) {
1566                        fput_light(sock->file, fput_needed);
1567                        return err;
1568                }
1569
1570                err = sock->ops->getname(sock, (struct sockaddr *)address, &len, 1);
1571                if (!err)
1572                        err=move_addr_to_user(address,len, usockaddr, usockaddr_len);
1573                fput_light(sock->file, fput_needed);
1574        }
1575        return err;
1576}
1577
1578/*
1579 *      Send a datagram to a given address. We move the address into kernel
1580 *      space and check the user space data area is readable before invoking
1581 *      the protocol.
1582 */
1583
1584asmlinkage long sys_sendto(int fd, void __user * buff, size_t len, unsigned flags,
1585                           struct sockaddr __user *addr, int addr_len)
1586{
1587        struct socket *sock;
1588        char address[MAX_SOCK_ADDR];
1589        int err;
1590        struct msghdr msg;
1591        struct iovec iov;
1592        int fput_needed;
1593        struct file *sock_file;
1594
1595        sock_file = fget_light(fd, &fput_needed);
1596        if (!sock_file)
1597                return -EBADF;
1598
1599        sock = sock_from_file(sock_file, &err);
1600        if (!sock)
1601                goto out_put;
1602        iov.iov_base=buff;
1603        iov.iov_len=len;
1604        msg.msg_name=NULL;
1605        msg.msg_iov=&iov;
1606        msg.msg_iovlen=1;
1607        msg.msg_control=NULL;
1608        msg.msg_controllen=0;
1609        msg.msg_namelen=0;
1610        if (addr) {
1611                err = move_addr_to_kernel(addr, addr_len, address);
1612                if (err < 0)
1613                        goto out_put;
1614                msg.msg_name=address;
1615                msg.msg_namelen=addr_len;
1616        }
1617        if (sock->file->f_flags & O_NONBLOCK)
1618                flags |= MSG_DONTWAIT;
1619        msg.msg_flags = flags;
1620        err = sock_sendmsg(sock, &msg, len);
1621
1622out_put:                
1623        fput_light(sock_file, fput_needed);
1624        return err;
1625}
1626
1627/*
1628 *      Send a datagram down a socket. 
1629 */
1630
1631asmlinkage long sys_send(int fd, void __user * buff, size_t len, unsigned flags)
1632{
1633        return sys_sendto(fd, buff, len, flags, NULL, 0);
1634}
1635
1636/*
1637 *      Receive a frame from the socket and optionally record the address of the 
1638 *      sender. We verify the buffers are writable and if needed move the
1639 *      sender address from kernel to user space.
1640 */
1641
1642asmlinkage long sys_recvfrom(int fd, void __user * ubuf, size_t size, unsigned flags,
1643                             struct sockaddr __user *addr, int __user *addr_len)
1644{
1645        struct socket *sock;
1646        struct iovec iov;
1647        struct msghdr msg;
1648        char address[MAX_SOCK_ADDR];
1649        int err,err2;
1650        struct file *sock_file;
1651        int fput_needed;
1652
1653        sock_file = fget_light(fd, &fput_needed);
1654        if (!sock_file)
1655                return -EBADF;
1656
1657        sock = sock_from_file(sock_file, &err);
1658        if (!sock)
1659                goto out;
1660
1661        msg.msg_control=NULL;
1662        msg.msg_controllen=0;
1663        msg.msg_iovlen=1;
1664        msg.msg_iov=&iov;
1665        iov.iov_len=size;
1666        iov.iov_base=ubuf;
1667        msg.msg_name=address;
1668        msg.msg_namelen=MAX_SOCK_ADDR;
1669        if (sock->file->f_flags & O_NONBLOCK)
1670                flags |= MSG_DONTWAIT;
1671        err=sock_recvmsg(sock, &msg, size, flags);
1672
1673        if(err >= 0 && addr != NULL)
1674        {
1675                err2=move_addr_to_user(address, msg.msg_namelen, addr, addr_len);
1676                if(err2<0)
1677                        err=err2;
1678        }
1679out:
1680        fput_light(sock_file, fput_needed);
1681        return err;
1682}
1683
1684/*
1685 *      Receive a datagram from a socket. 
1686 */
1687
1688asmlinkage long sys_recv(int fd, void __user * ubuf, size_t size, unsigned flags)
1689{
1690        return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
1691}
1692
1693/*
1694 *      Set a socket option. Because we don't know the option lengths we have
1695 *      to pass the user mode parameter for the protocols to sort out.
1696 */
1697
1698asmlinkage long sys_setsockopt(int fd, int level, int optname, char __user *optval, int optlen)
1699{
1700        int err, fput_needed;
1701        struct socket *sock;
1702
1703        if (optlen < 0)
1704                return -EINVAL;
1705                        
1706        if ((sock = sockfd_lookup_light(fd, &err, &fput_needed)) != NULL)
1707        {
1708                err = security_socket_setsockopt(sock,level,optname);
1709                if (err)
1710                        goto out_put;
1711
1712                if (level == SOL_SOCKET)
1713                        err=sock_setsockopt(sock,level,optname,optval,optlen);
1714                else
1715                        err=sock->ops->setsockopt(sock, level, optname, optval, optlen);
1716out_put:
1717                fput_light(sock->file, fput_needed);
1718        }
1719        return err;
1720}
1721
1722/*
1723 *      Get a socket option. Because we don't know the option lengths we have
1724 *      to pass a user mode parameter for the protocols to sort out.
1725 */
1726
1727asmlinkage long sys_getsockopt(int fd, int level, int optname, char __user *optval, int __user *optlen)
1728{
1729        int err, fput_needed;
1730        struct socket *sock;
1731
1732        if ((sock = sockfd_lookup_light(fd, &err, &fput_needed)) != NULL) {
1733                err = security_socket_getsockopt(sock, level, optname);
1734                if (err)
1735                        goto out_put;
1736
1737                if (level == SOL_SOCKET)
1738                        err=sock_getsockopt(sock,level,optname,optval,optlen);
1739                else
1740                        err=sock->ops->getsockopt(sock, level, optname, optval, optlen);
1741out_put:
1742                fput_light(sock->file, fput_needed);
1743        }
1744        return err;
1745}
1746
1747
1748/*
1749 *      Shutdown a socket.
1750 */
1751
1752asmlinkage long sys_shutdown(int fd, int how)
1753{
1754        int err, fput_needed;
1755        struct socket *sock;
1756
1757        if ((sock = sockfd_lookup_light(fd, &err, &fput_needed))!=NULL)
1758        {
1759                err = security_socket_shutdown(sock, how);
1760                if (!err)
1761                        err = sock->ops->shutdown(sock, how);
1762                fput_light(sock->file, fput_needed);
1763        }
1764        return err;
1765}
1766
1767/* A couple of helpful macros for getting the address of the 32/64 bit 
1768 * fields which are the same type (int / unsigned) on our platforms.
1769 */
1770#define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
1771#define COMPAT_NAMELEN(msg)     COMPAT_MSG(msg, msg_namelen)
1772#define COMPAT_FLAGS(msg)       COMPAT_MSG(msg, msg_flags)
1773
1774
1775/*
1776 *      BSD sendmsg interface
1777 */
1778
1779asmlinkage long sys_sendmsg(int fd, struct msghdr __user *msg, unsigned flags)
1780{
1781        struct compat_msghdr __user *msg_compat = (struct compat_msghdr __user *)msg;
1782        struct socket *sock;
1783        char address[MAX_SOCK_ADDR];
1784        struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
1785        unsigned char ctl[sizeof(struct cmsghdr) + 20]
1786                        __attribute__ ((aligned (sizeof(__kernel_size_t))));
1787                        /* 20 is size of ipv6_pktinfo */
1788        unsigned char *ctl_buf = ctl;
1789        struct msghdr msg_sys;
1790        int err, ctl_len, iov_size, total_len;
1791        int fput_needed;
1792        
1793        err = -EFAULT;
1794        if (MSG_CMSG_COMPAT & flags) {
1795                if (get_compat_msghdr(&msg_sys, msg_compat))
1796                        return -EFAULT;
1797        } else if (copy_from_user(&msg_sys, msg, sizeof(struct msghdr)))
1798                return -EFAULT;
1799
1800        sock = sockfd_lookup_light(fd, &err, &fput_needed);
1801        if (!sock) 
1802                goto out;
1803
1804        /* do not move before msg_sys is valid */
1805        err = -EMSGSIZE;
1806        if (msg_sys.msg_iovlen > UIO_MAXIOV)
1807                goto out_put;
1808
1809        /* Check whether to allocate the iovec area*/
1810        err = -ENOMEM;
1811        iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
1812        if (msg_sys.msg_iovlen > UIO_FASTIOV) {
1813                iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
1814                if (!iov)
1815                        goto out_put;
1816        }
1817
1818        /* This will also move the address data into kernel space */
1819        if (MSG_CMSG_COMPAT & flags) {
1820                err = verify_compat_iovec(&msg_sys, iov, address, VERIFY_READ);
1821        } else
1822                err = verify_iovec(&msg_sys, iov, address, VERIFY_READ);
1823        if (err < 0) 
1824                goto out_freeiov;
1825        total_len = err;
1826
1827        err = -ENOBUFS;
1828
1829        if (msg_sys.msg_controllen > INT_MAX)
1830                goto out_freeiov;
1831        ctl_len = msg_sys.msg_controllen; 
1832        if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
1833                err = cmsghdr_from_user_compat_to_kern(&msg_sys, sock->sk, ctl, sizeof(ctl));
1834                if (err)
1835                        goto out_freeiov;
1836                ctl_buf = msg_sys.msg_control;
1837                ctl_len = msg_sys.msg_controllen;
1838        } else if (ctl_len) {
1839                if (ctl_len > sizeof(ctl))
1840                {
1841                        ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
1842                        if (ctl_buf == NULL) 
1843                                goto out_freeiov;
1844                }
1845                err = -EFAULT;
1846                /*
1847                 * Careful! Before this, msg_sys.msg_control contains a user pointer.
1848                 * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
1849                 * checking falls down on this.
1850                 */
1851                if (copy_from_user(ctl_buf, (void __user *) msg_sys.msg_control, ctl_len))
1852                        goto out_freectl;
1853                msg_sys.msg_control = ctl_buf;
1854        }
1855        msg_sys.msg_flags = flags;
1856
1857        if (sock->file->f_flags & O_NONBLOCK)
1858                msg_sys.msg_flags |= MSG_DONTWAIT;
1859        err = sock_sendmsg(sock, &msg_sys, total_len);
1860
1861out_freectl:
1862        if (ctl_buf != ctl)    
1863                sock_kfree_s(sock->sk, ctl_buf, ctl_len);
1864out_freeiov:
1865        if (iov != iovstack)
1866                sock_kfree_s(sock->sk, iov, iov_size);
1867out_put:
1868        fput_light(sock->file, fput_needed);
1869out:       
1870        return err;
1871}
1872
1873/*
1874 *      BSD recvmsg interface
1875 */
1876
1877asmlinkage long sys_recvmsg(int fd, struct msghdr __user *msg, unsigned int flags)
1878{
1879        struct compat_msghdr __user *msg_compat = (struct compat_msghdr __user *)msg;
1880        struct socket *sock;
1881        struct iovec iovstack[UIO_FASTIOV];
1882        struct iovec *iov=iovstack;
1883        struct msghdr msg_sys;
1884        unsigned long cmsg_ptr;
1885        int err, iov_size, total_len, len;
1886        int fput_needed;
1887
1888        /* kernel mode address */
1889        char addr[MAX_SOCK_ADDR];
1890
1891        /* user mode address pointers */
1892        struct sockaddr __user *uaddr;
1893        int __user *uaddr_len;
1894        
1895        if (MSG_CMSG_COMPAT & flags) {
1896                if (get_compat_msghdr(&msg_sys, msg_compat))
1897                        return -EFAULT;
1898        } else
1899                if (copy_from_user(&msg_sys,msg,sizeof(struct msghdr)))
1900                        return -EFAULT;
1901
1902        sock = sockfd_lookup_light(fd, &err, &fput_needed);
1903        if (!sock)
1904                goto out;
1905
1906        err = -EMSGSIZE;
1907        if (msg_sys.msg_iovlen > UIO_MAXIOV)
1908                goto out_put;
1909        
1910        /* Check whether to allocate the iovec area*/
1911        err = -ENOMEM;
1912        iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
1913        if (msg_sys.msg_iovlen > UIO_FASTIOV) {
1914                iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
1915                if (!iov)
1916                        goto out_put;
1917        }
1918
1919        /*
1920         *      Save the user-mode address (verify_iovec will change the
1921         *      kernel msghdr to use the kernel address space)
1922         */
1923         
1924        uaddr = (void __user *) msg_sys.msg_name;
1925        uaddr_len = COMPAT_NAMELEN(msg);
1926        if (MSG_CMSG_COMPAT & flags) {
1927                err = verify_compat_iovec(&msg_sys, iov, addr, VERIFY_WRITE);
1928        } else
1929                err = verify_iovec(&msg_sys, iov, addr, VERIFY_WRITE);
1930        if (err < 0)
1931                goto out_freeiov;
1932        total_len=err;
1933
1934        cmsg_ptr = (unsigned long)msg_sys.msg_control;
1935        msg_sys.msg_flags = 0;
1936        if (MSG_CMSG_COMPAT & flags)
1937                msg_sys.msg_flags = MSG_CMSG_COMPAT;
1938        
1939        if (sock->file->f_flags & O_NONBLOCK)
1940                flags |= MSG_DONTWAIT;
1941        err = sock_recvmsg(sock, &msg_sys, total_len, flags);
1942        if (err < 0)
1943                goto out_freeiov;
1944        len = err;
1945
1946        if (uaddr != NULL) {
1947                err = move_addr_to_user(addr, msg_sys.msg_namelen, uaddr, uaddr_len);
1948                if (err < 0)
1949                        goto out_freeiov;
1950        }
1951        err = __put_user((msg_sys.msg_flags & ~MSG_CMSG_COMPAT),
1952                         COMPAT_FLAGS(msg));
1953        if (err)
1954                goto out_freeiov;
1955        if (MSG_CMSG_COMPAT & flags)
1956                err = __put_user((unsigned long)msg_sys.msg_control-cmsg_ptr, 
1957                                 &msg_compat->msg_controllen);
1958        else
1959                err = __put_user((unsigned long)msg_sys.msg_control-cmsg_ptr, 
1960                                 &msg->msg_controllen);
1961        if (err)
1962                goto out_freeiov;
1963        err = len;
1964
1965out_freeiov:
1966        if (iov != iovstack)
1967                sock_kfree_s(sock->sk, iov, iov_size);
1968out_put:
1969        fput_light(sock->file, fput_needed);
1970out:
1971        return err;
1972}
1973
1974#ifdef __ARCH_WANT_SYS_SOCKETCALL
1975
1976/* Argument list sizes for sys_socketcall */
1977#define AL(x) ((x) * sizeof(unsigned long))
1978static unsigned char nargs[18]={AL(0),AL(3),AL(3),AL(3),AL(2),AL(3),
1979                                AL(3),AL(3),AL(4),AL(4),AL(4),AL(6),
1980                                AL(6),AL(2),AL(5),AL(5),AL(3),AL(3)};
1981#undef AL
1982
1983/*
1984 *      System call vectors. 
1985 *
1986 *      Argument checking cleaned up. Saved 20% in size.
1987 *  This function doesn't need to set the kernel lock because
1988 *  it is set by the callees. 
1989 */
1990
1991asmlinkage long sys_socketcall(int call, unsigned long __user *args)
1992{
1993        unsigned long a[6];
1994        unsigned long a0,a1;
1995        int err;
1996
1997        if(call<1||call>SYS_RECVMSG)
1998                return -EINVAL;
1999
2000        /* copy_from_user should be SMP safe. */
2001        if (copy_from_user(a, args, nargs[call]))
2002                return -EFAULT;
2003
2004        err = audit_socketcall(nargs[call]/sizeof(unsigned long), a);
2005        if (err)
2006                return err;
2007
2008        a0=a[0];
2009        a1=a[1];
2010        
2011        switch(call) 
2012        {
2013                case SYS_SOCKET:
2014                        err = sys_socket(a0,a1,a[2]);
2015                        break;
2016                case SYS_BIND:
2017                        err = sys_bind(a0,(struct sockaddr __user *)a1, a[2]);
2018                        break;
2019                case SYS_CONNECT:
2020                        err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
2021                        break;
2022                case SYS_LISTEN:
2023                        err = sys_listen(a0,a1);
2024                        break;
2025                case SYS_ACCEPT:
2026                        err = sys_accept(a0,(struct sockaddr __user *)a1, (int __user *)a[2]);
2027                        break;
2028                case SYS_GETSOCKNAME:
2029                        err = sys_getsockname(a0,(struct sockaddr __user *)a1, (int __user *)a[2]);
2030                        break;
2031                case SYS_GETPEERNAME:
2032                        err = sys_getpeername(a0, (struct sockaddr __user *)a1, (int __user *)a[2]);
2033                        break;
2034                case SYS_SOCKETPAIR:
2035                        err = sys_socketpair(a0,a1, a[2], (int __user *)a[3]);
2036                        break;
2037                case SYS_SEND:
2038                        err = sys_send(a0, (void __user *)a1, a[2], a[3]);
2039                        break;
2040                case SYS_SENDTO:
2041                        err = sys_sendto(a0,(void __user *)a1, a[2], a[3],
2042                                         (struct sockaddr __user *)a[4], a[5]);
2043                        break;
2044                case SYS_RECV:
2045                        err = sys_recv(a0, (void __user *)a1, a[2], a[3]);
2046                        break;
2047                case SYS_RECVFROM:
2048                        err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
2049                                           (struct sockaddr __user *)a[4], (int __user *)a[5]);
2050                        break;
2051                case SYS_SHUTDOWN:
2052                        err = sys_shutdown(a0,a1);
2053                        break;
2054                case SYS_SETSOCKOPT:
2055                        err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]);
2056                        break;
2057                case SYS_GETSOCKOPT:
2058                        err = sys_getsockopt(a0, a1, a[2], (char __user *)a[3], (int __user *)a[4]);
2059                        break;
2060                case SYS_SENDMSG:
2061                        err = sys_sendmsg(a0, (struct msghdr __user *) a1, a[2]);
2062                        break;
2063                case SYS_RECVMSG:
2064                        err = sys_recvmsg(a0, (struct msghdr __user *) a1, a[2]);
2065                        break;
2066                default:
2067                        err = -EINVAL;
2068                        break;
2069        }
2070        return err;
2071}
2072
2073#endif /* __ARCH_WANT_SYS_SOCKETCALL */
2074
2075/*
2076 *      This function is called by a protocol handler that wants to
2077 *      advertise its address family, and have it linked into the
2078 *      SOCKET module.
2079 */
2080
2081int sock_register(struct net_proto_family *ops)
2082{
2083        int err;
2084
2085        if (ops->family >= NPROTO) {
2086                printk(KERN_CRIT "protocol %d >= NPROTO(%d)\n", ops->family, NPROTO);
2087                return -ENOBUFS;
2088        }
2089        net_family_write_lock();
2090        err = -EEXIST;
2091        if (net_families[ops->family] == NULL) {
2092                net_families[ops->family]=ops;
2093                err = 0;
2094        }
2095        net_family_write_unlock();
2096        printk(KERN_INFO "NET: Registered protocol family %d\n",
2097               ops->family);
2098        return err;
2099}
2100
2101/*
2102 *      This function is called by a protocol handler that wants to
2103 *      remove its address family, and have it unlinked from the
2104 *      SOCKET module.
2105 */
2106
2107int sock_unregister(int family)
2108{
2109        if (family < 0 || family >= NPROTO)
2110                return -1;
2111
2112        net_family_write_lock();
2113        net_families[family]=NULL;
2114        net_family_write_unlock();
2115        printk(KERN_INFO "NET: Unregistered protocol family %d\n",
2116               family);
2117        return 0;
2118}
2119
2120static int __init sock_init(void)
2121{
2122        /*
2123         *      Initialize sock SLAB cache.
2124         */
2125         
2126        sk_init();
2127
2128        /*
2129         *      Initialize skbuff SLAB cache 
2130         */
2131        skb_init();
2132
2133        /*
2134         *      Initialize the protocols module. 
2135         */
2136
2137        init_inodecache();
2138        register_filesystem(&sock_fs_type);
2139        sock_mnt = kern_mount(&sock_fs_type);
2140
2141        /* The real protocol initialization is performed in later initcalls.
2142         */
2143
2144#ifdef CONFIG_NETFILTER
2145        netfilter_init();
2146#endif
2147
2148        return 0;
2149}
2150
2151core_initcall(sock_init);       /* early initcall */
2152
2153int tux_Dprintk;
2154int tux_TDprintk;
2155
2156struct module *tux_module = NULL;
2157
2158#ifdef CONFIG_TUX_MODULE
2159
2160asmlinkage long (*sys_tux_ptr) (unsigned int action, user_req_t *u_info) = NULL;
2161spinlock_t tux_module_lock = SPIN_LOCK_UNLOCKED;
2162
2163asmlinkage long sys_tux (unsigned int action, user_req_t *u_info)
2164{
2165        int ret;
2166
2167        if (current->tux_info)
2168                return sys_tux_ptr(action, u_info);
2169
2170        ret = -ENOSYS;
2171        spin_lock(&tux_module_lock);
2172        if (!tux_module)
2173                goto out_unlock;
2174        if (!try_module_get(tux_module))
2175                goto out_unlock;
2176        spin_unlock(&tux_module_lock);
2177
2178        if (!sys_tux_ptr)
2179                TUX_BUG();
2180        ret = sys_tux_ptr(action, u_info);
2181
2182        spin_lock(&tux_module_lock);
2183        module_put(tux_module);
2184out_unlock:
2185        spin_unlock(&tux_module_lock);
2186
2187        return ret;
2188}
2189
2190EXPORT_SYMBOL_GPL(tux_module);
2191EXPORT_SYMBOL_GPL(tux_module_lock);
2192EXPORT_SYMBOL_GPL(sys_tux_ptr);
2193
2194EXPORT_SYMBOL_GPL(tux_Dprintk);
2195EXPORT_SYMBOL_GPL(tux_TDprintk);
2196
2197#endif
2198#ifdef CONFIG_PROC_FS
2199void socket_seq_show(struct seq_file *seq)
2200{
2201        int cpu;
2202        int counter = 0;
2203
2204        for_each_possible_cpu(cpu)
2205                counter += per_cpu(sockets_in_use, cpu);
2206
2207        /* It can be negative, by the way. 8) */
2208        if (counter < 0)
2209                counter = 0;
2210
2211        seq_printf(seq, "sockets: used %d\n", counter);
2212}
2213#endif /* CONFIG_PROC_FS */
2214
2215#ifdef CONFIG_COMPAT
2216static long compat_sock_ioctl(struct file *file, unsigned cmd,
2217                                unsigned long arg)
2218{
2219        struct socket *sock = file->private_data;
2220        int ret = -ENOIOCTLCMD;
2221
2222        if (sock->ops->compat_ioctl)
2223                ret = sock->ops->compat_ioctl(sock, cmd, arg);
2224
2225        return ret;
2226}
2227#endif
2228
2229/* ABI emulation layers need these two */
2230EXPORT_SYMBOL(move_addr_to_kernel);
2231EXPORT_SYMBOL(move_addr_to_user);
2232EXPORT_SYMBOL(sock_create);
2233EXPORT_SYMBOL(sock_create_kern);
2234EXPORT_SYMBOL(sock_create_lite);
2235EXPORT_SYMBOL(sock_map_fd);
2236EXPORT_SYMBOL(sock_recvmsg);
2237EXPORT_SYMBOL(sock_register);
2238EXPORT_SYMBOL(sock_release);
2239EXPORT_SYMBOL(sock_sendmsg);
2240EXPORT_SYMBOL(sock_unregister);
2241EXPORT_SYMBOL(sock_wake_async);
2242EXPORT_SYMBOL(sockfd_lookup);
2243EXPORT_SYMBOL(kernel_sendmsg);
2244EXPORT_SYMBOL(kernel_recvmsg);
2245