vserver 1.9.5.x5
[linux-2.6.git] / net / socket.c
index bee0949..1afd112 100644 (file)
 #endif /* CONFIG_NET_RADIO */
 
 #include <asm/uaccess.h>
+#include <asm/unistd.h>
+
 #include <net/compat.h>
 
 #include <net/sock.h>
 #include <linux/netfilter.h>
+#include <linux/vs_socket.h>
 
 static int sock_no_open(struct inode *irrelevant, struct file *dontcare);
 static ssize_t sock_aio_read(struct kiocb *iocb, char __user *buf,
@@ -102,7 +105,7 @@ static int sock_mmap(struct file *file, struct vm_area_struct * vma);
 static int sock_close(struct inode *inode, struct file *file);
 static unsigned int sock_poll(struct file *file,
                              struct poll_table_struct *wait);
-static int sock_ioctl(struct inode *inode, struct file *file,
+static long sock_ioctl(struct file *file,
                      unsigned int cmd, unsigned long arg);
 static int sock_fasync(int fd, struct file *filp, int on);
 static ssize_t sock_readv(struct file *file, const struct iovec *vector,
@@ -124,7 +127,7 @@ static struct file_operations socket_file_ops = {
        .aio_read =     sock_aio_read,
        .aio_write =    sock_aio_write,
        .poll =         sock_poll,
-       .ioctl =        sock_ioctl,
+       .unlocked_ioctl = sock_ioctl,
        .mmap =         sock_mmap,
        .open =         sock_no_open,   /* special open code to disallow open via /proc */
        .release =      sock_close,
@@ -142,7 +145,7 @@ static struct net_proto_family *net_families[NPROTO];
 
 #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
 static atomic_t net_family_lockct = ATOMIC_INIT(0);
-static spinlock_t net_family_lock = SPIN_LOCK_UNLOCKED;
+static DEFINE_SPINLOCK(net_family_lock);
 
 /* The strategy is: modifications net_family vector are short, do not
    sleep and veeery rare, but read access should be free of any exclusive
@@ -285,7 +288,7 @@ static struct inode *sock_alloc_inode(struct super_block *sb)
        ei->socket.ops = NULL;
        ei->socket.sk = NULL;
        ei->socket.file = NULL;
-       ei->socket.passcred = 0;
+       ei->socket.flags = 0;
 
        return &ei->vfs_inode;
 }
@@ -308,9 +311,9 @@ static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
 static int init_inodecache(void)
 {
        sock_inode_cachep = kmem_cache_create("sock_inode_cache",
-                                            sizeof(struct socket_alloc),
-                                            0, SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT,
-                                            init_once, NULL);
+                               sizeof(struct socket_alloc),
+                               0, SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT,
+                               init_once, NULL);
        if (sock_inode_cachep == NULL)
                return -ENOMEM;
        return 0;
@@ -399,7 +402,7 @@ int sock_map_fd(struct socket *sock)
 
                sock->file = file;
                file->f_op = SOCK_INODE(sock)->i_fop = &socket_file_ops;
-               file->f_mode = 3;
+               file->f_mode = FMODE_READ | FMODE_WRITE;
                file->f_flags = O_RDWR;
                file->f_pos = 0;
                fd_install(fd, file);
@@ -457,7 +460,7 @@ struct socket *sockfd_lookup(int fd, int *err)
  *     NULL is returned.
  */
 
-struct socket *sock_alloc(void)
+static struct socket *sock_alloc(void)
 {
        struct inode * inode;
        struct socket * sock;
@@ -529,7 +532,7 @@ static inline int __sock_sendmsg(struct kiocb *iocb, struct socket *sock,
                                 struct msghdr *msg, size_t size)
 {
        struct sock_iocb *si = kiocb_to_siocb(iocb);
-       int err;
+       int err, len;
 
        si->sock = sock;
        si->scm = NULL;
@@ -540,26 +543,59 @@ static inline int __sock_sendmsg(struct kiocb *iocb, struct socket *sock,
        if (err)
                return err;
 
-       return sock->ops->sendmsg(iocb, sock, msg, size);
+       len = sock->ops->sendmsg(iocb, sock, msg, size);
+       if (sock->sk) {
+               if (len == size)
+                       vx_sock_send(sock->sk, size);
+               else
+                       vx_sock_fail(sock->sk, size);
+       }
+       vxdprintk(VXD_CBIT(net, 7),
+               "__sock_sendmsg: %p[%p,%p,%p;%d]:%d/%d",
+               sock, sock->sk,
+               (sock->sk)?sock->sk->sk_nx_info:0,
+               (sock->sk)?sock->sk->sk_vx_info:0,
+               (sock->sk)?sock->sk->sk_xid:0,
+               (unsigned int)size, len);
+       return len;
 }
 
 int sock_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
 {
        struct kiocb iocb;
+       struct sock_iocb siocb;
        int ret;
 
        init_sync_kiocb(&iocb, NULL);
+       iocb.private = &siocb;
        ret = __sock_sendmsg(&iocb, sock, msg, size);
        if (-EIOCBQUEUED == ret)
                ret = wait_on_sync_kiocb(&iocb);
        return ret;
 }
 
+int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
+                  struct kvec *vec, size_t num, size_t size)
+{
+       mm_segment_t oldfs = get_fs();
+       int result;
+
+       set_fs(KERNEL_DS);
+       /*
+        * the following is safe, since for compiler definitions of kvec and
+        * iovec are identical, yielding the same in-core layout and alignment
+        */
+       msg->msg_iov = (struct iovec *)vec,
+       msg->msg_iovlen = num;
+       result = sock_sendmsg(sock, msg, size);
+       set_fs(oldfs);
+       return result;
+}
 
 static inline int __sock_recvmsg(struct kiocb *iocb, struct socket *sock, 
                                 struct msghdr *msg, size_t size, int flags)
 {
-       int err;
+       int err, len;
        struct sock_iocb *si = kiocb_to_siocb(iocb);
 
        si->sock = sock;
@@ -572,22 +608,58 @@ static inline int __sock_recvmsg(struct kiocb *iocb, struct socket *sock,
        if (err)
                return err;
 
-       return sock->ops->recvmsg(iocb, sock, msg, size, flags);
+       len = sock->ops->recvmsg(iocb, sock, msg, size, flags);
+       if ((len >= 0) && sock->sk)
+               vx_sock_recv(sock->sk, len);
+       vxdprintk(VXD_CBIT(net, 7),
+               "__sock_recvmsg: %p[%p,%p,%p;%d]:%d/%d",
+               sock, sock->sk,
+               (sock->sk)?sock->sk->sk_nx_info:0,
+               (sock->sk)?sock->sk->sk_vx_info:0,
+               (sock->sk)?sock->sk->sk_xid:0,
+               (unsigned int)size, len);
+       return len;
 }
 
 int sock_recvmsg(struct socket *sock, struct msghdr *msg, 
                 size_t size, int flags)
 {
        struct kiocb iocb;
+       struct sock_iocb siocb;
        int ret;
 
         init_sync_kiocb(&iocb, NULL);
+       iocb.private = &siocb;
        ret = __sock_recvmsg(&iocb, sock, msg, size, flags);
        if (-EIOCBQUEUED == ret)
                ret = wait_on_sync_kiocb(&iocb);
        return ret;
 }
 
+int kernel_recvmsg(struct socket *sock, struct msghdr *msg, 
+                  struct kvec *vec, size_t num,
+                  size_t size, int flags)
+{
+       mm_segment_t oldfs = get_fs();
+       int result;
+
+       set_fs(KERNEL_DS);
+       /*
+        * the following is safe, since for compiler definitions of kvec and
+        * iovec are identical, yielding the same in-core layout and alignment
+        */
+       msg->msg_iov = (struct iovec *)vec,
+       msg->msg_iovlen = num;
+       result = sock_recvmsg(sock, msg, size, flags);
+       set_fs(oldfs);
+       return result;
+}
+
+static void sock_aio_dtor(struct kiocb *iocb)
+{
+       kfree(iocb->private);
+}
+
 /*
  *     Read data from a socket. ubuf is a user mode pointer. We make sure the user
  *     area ubuf...ubuf+size-1 is writable before asking the protocol.
@@ -596,7 +668,7 @@ int sock_recvmsg(struct socket *sock, struct msghdr *msg,
 static ssize_t sock_aio_read(struct kiocb *iocb, char __user *ubuf,
                         size_t size, loff_t pos)
 {
-       struct sock_iocb *x = kiocb_to_siocb(iocb);
+       struct sock_iocb *x, siocb;
        struct socket *sock;
        int flags;
 
@@ -605,6 +677,16 @@ static ssize_t sock_aio_read(struct kiocb *iocb, char __user *ubuf,
        if (size==0)            /* Match SYS5 behaviour */
                return 0;
 
+       if (is_sync_kiocb(iocb))
+               x = &siocb;
+       else {
+               x = kmalloc(sizeof(struct sock_iocb), GFP_KERNEL);
+               if (!x)
+                       return -ENOMEM;
+               iocb->ki_dtor = sock_aio_dtor;
+       }
+       iocb->private = x;
+       x->kiocb = iocb;
        sock = SOCKET_I(iocb->ki_filp->f_dentry->d_inode); 
 
        x->async_msg.msg_name = NULL;
@@ -629,7 +711,7 @@ static ssize_t sock_aio_read(struct kiocb *iocb, char __user *ubuf,
 static ssize_t sock_aio_write(struct kiocb *iocb, const char __user *ubuf,
                          size_t size, loff_t pos)
 {
-       struct sock_iocb *x = kiocb_to_siocb(iocb);
+       struct sock_iocb *x, siocb;
        struct socket *sock;
        
        if (pos != 0)
@@ -637,6 +719,16 @@ static ssize_t sock_aio_write(struct kiocb *iocb, const char __user *ubuf,
        if(size==0)             /* Match SYS5 behaviour */
                return 0;
 
+       if (is_sync_kiocb(iocb))
+               x = &siocb;
+       else {
+               x = kmalloc(sizeof(struct sock_iocb), GFP_KERNEL);
+               if (!x)
+                       return -ENOMEM;
+               iocb->ki_dtor = sock_aio_dtor;
+       }
+       iocb->private = x;
+       x->kiocb = iocb;
        sock = SOCKET_I(iocb->ki_filp->f_dentry->d_inode); 
 
        x->async_msg.msg_name = NULL;
@@ -660,9 +752,6 @@ ssize_t sock_sendpage(struct file *file, struct page *page,
        struct socket *sock;
        int flags;
 
-       if (ppos != &file->f_pos)
-               return -ESPIPE;
-
        sock = SOCKET_I(file->f_dentry->d_inode);
 
        flags = !(file->f_flags & O_NONBLOCK) ? 0 : MSG_DONTWAIT;
@@ -672,8 +761,9 @@ ssize_t sock_sendpage(struct file *file, struct page *page,
        return sock->ops->sendpage(sock, page, offset, size, flags);
 }
 
-int sock_readv_writev(int type, struct inode * inode, struct file * file,
-                     const struct iovec * iov, long count, size_t size)
+static int sock_readv_writev(int type, struct inode * inode,
+                            struct file * file, const struct iovec * iov,
+                            long count, size_t size)
 {
        struct msghdr msg;
        struct socket *sock;
@@ -727,9 +817,9 @@ static ssize_t sock_writev(struct file *file, const struct iovec *vector,
  */
 
 static DECLARE_MUTEX(br_ioctl_mutex);
-static int (*br_ioctl_hook)(unsigned long arg) = NULL;
+static int (*br_ioctl_hook)(unsigned int cmd, void __user *arg) = NULL;
 
-void brioctl_set(int (*hook)(unsigned long))
+void brioctl_set(int (*hook)(unsigned int, void __user *))
 {
        down(&br_ioctl_mutex);
        br_ioctl_hook = hook;
@@ -738,9 +828,9 @@ void brioctl_set(int (*hook)(unsigned long))
 EXPORT_SYMBOL(brioctl_set);
 
 static DECLARE_MUTEX(vlan_ioctl_mutex);
-static int (*vlan_ioctl_hook)(unsigned long arg);
+static int (*vlan_ioctl_hook)(void __user *arg);
 
-void vlan_ioctl_set(int (*hook)(unsigned long))
+void vlan_ioctl_set(int (*hook)(void __user *))
 {
        down(&vlan_ioctl_mutex);
        vlan_ioctl_hook = hook;
@@ -749,9 +839,9 @@ void vlan_ioctl_set(int (*hook)(unsigned long))
 EXPORT_SYMBOL(vlan_ioctl_set);
 
 static DECLARE_MUTEX(dlci_ioctl_mutex);
-static int (*dlci_ioctl_hook)(unsigned int, void *);
+static int (*dlci_ioctl_hook)(unsigned int, void __user *);
 
-void dlci_ioctl_set(int (*hook)(unsigned int, void *))
+void dlci_ioctl_set(int (*hook)(unsigned int, void __user *))
 {
        down(&dlci_ioctl_mutex);
        dlci_ioctl_hook = hook;
@@ -764,43 +854,44 @@ EXPORT_SYMBOL(dlci_ioctl_set);
  *     what to do with it - that's up to the protocol still.
  */
 
-static int sock_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
-                     unsigned long arg)
+static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
 {
        struct socket *sock;
+       void __user *argp = (void __user *)arg;
        int pid, err;
 
-       unlock_kernel();
-       sock = SOCKET_I(inode);
+       sock = SOCKET_I(file->f_dentry->d_inode);
        if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
-               err = dev_ioctl(cmd, (void *)arg);
+               err = dev_ioctl(cmd, argp);
        } else
 #ifdef WIRELESS_EXT
        if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
-               err = dev_ioctl(cmd, (void *)arg);
+               err = dev_ioctl(cmd, argp);
        } else
 #endif /* WIRELESS_EXT */
        switch (cmd) {
                case FIOSETOWN:
                case SIOCSPGRP:
                        err = -EFAULT;
-                       if (get_user(pid, (int *)arg))
+                       if (get_user(pid, (int __user *)argp))
                                break;
                        err = f_setown(sock->file, pid, 1);
                        break;
                case FIOGETOWN:
                case SIOCGPGRP:
-                       err = put_user(sock->file->f_owner.pid, (int *)arg);
+                       err = put_user(sock->file->f_owner.pid, (int __user *)argp);
                        break;
                case SIOCGIFBR:
                case SIOCSIFBR:
+               case SIOCBRADDBR:
+               case SIOCBRDELBR:
                        err = -ENOPKG;
                        if (!br_ioctl_hook)
                                request_module("bridge");
 
                        down(&br_ioctl_mutex);
                        if (br_ioctl_hook) 
-                               err = br_ioctl_hook(arg);
+                               err = br_ioctl_hook(cmd, argp);
                        up(&br_ioctl_mutex);
                        break;
                case SIOCGIFVLAN:
@@ -811,13 +902,13 @@ static int sock_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
 
                        down(&vlan_ioctl_mutex);
                        if (vlan_ioctl_hook)
-                               err = vlan_ioctl_hook(arg);
+                               err = vlan_ioctl_hook(argp);
                        up(&vlan_ioctl_mutex);
                        break;
                case SIOCGIFDIVERT:
                case SIOCSIFDIVERT:
                /* Convert this to call through a hook */
-                       err = divert_ioctl(cmd, (struct divert_cf *)arg);
+                       err = divert_ioctl(cmd, argp);
                        break;
                case SIOCADDDLCI:
                case SIOCDELDLCI:
@@ -827,7 +918,7 @@ static int sock_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
 
                        if (dlci_ioctl_hook) {
                                down(&dlci_ioctl_mutex);
-                               err = dlci_ioctl_hook(cmd, (void *)arg);
+                               err = dlci_ioctl_hook(cmd, argp);
                                up(&dlci_ioctl_mutex);
                        }
                        break;
@@ -835,8 +926,6 @@ static int sock_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
                        err = sock->ops->ioctl(sock, cmd, arg);
                        break;
        }
-       lock_kernel();
-
        return err;
 }
 
@@ -1006,7 +1095,6 @@ int sock_wake_async(struct socket *sock, int how, int band)
 
 static int __sock_create(int family, int type, int protocol, struct socket **res, int kern)
 {
-       int i;
        int err;
        struct socket *sock;
 
@@ -1018,6 +1106,10 @@ static int __sock_create(int family, int type, int protocol, struct socket **res
        if (type < 0 || type >= SOCK_MAX)
                return -EINVAL;
 
+       /* disable IPv6 inside vservers for now */
+       if (family == PF_INET6 && !vx_check(0, VX_ADMIN))
+               return -EAFNOSUPPORT;
+
        /* Compatibility.
 
           This uglymoron is moved from INET layer to here to avoid
@@ -1051,7 +1143,7 @@ static int __sock_create(int family, int type, int protocol, struct socket **res
 
        net_family_read_lock();
        if (net_families[family] == NULL) {
-               i = -EAFNOSUPPORT;
+               err = -EAFNOSUPPORT;
                goto out;
        }
 
@@ -1061,10 +1153,9 @@ static int __sock_create(int family, int type, int protocol, struct socket **res
  *     default.
  */
 
-       if (!(sock = sock_alloc())) 
-       {
+       if (!(sock = sock_alloc())) {
                printk(KERN_WARNING "socket: no more sockets\n");
-               i = -ENFILE;            /* Not exactly a match, but its the
+               err = -ENFILE;          /* Not exactly a match, but its the
                                           closest posix thing */
                goto out;
        }
@@ -1075,11 +1166,11 @@ static int __sock_create(int family, int type, int protocol, struct socket **res
         * We will call the ->create function, that possibly is in a loadable
         * module, so we have to bump that loadable module refcnt first.
         */
-       i = -EAFNOSUPPORT;
+       err = -EAFNOSUPPORT;
        if (!try_module_get(net_families[family]->owner))
                goto out_release;
 
-       if ((i = net_families[family]->create(sock, protocol)) < 0)
+       if ((err = net_families[family]->create(sock, protocol)) < 0)
                goto out_module_put;
        /*
         * Now to bump the refcnt of the [loadable] module that owns this
@@ -1099,7 +1190,7 @@ static int __sock_create(int family, int type, int protocol, struct socket **res
 
 out:
        net_family_read_unlock();
-       return i;
+       return err;
 out_module_put:
        module_put(net_families[family]->owner);
 out_release:
@@ -1126,6 +1217,7 @@ asmlinkage long sys_socket(int family, int type, int protocol)
        if (retval < 0)
                goto out;
 
+       set_bit(SOCK_USER_SOCKET, &sock->flags);
        retval = sock_map_fd(sock);
        if (retval < 0)
                goto out_release;
@@ -1156,10 +1248,12 @@ asmlinkage long sys_socketpair(int family, int type, int protocol, int __user *u
        err = sock_create(family, type, protocol, &sock1);
        if (err < 0)
                goto out;
+       set_bit(SOCK_USER_SOCKET, &sock1->flags);
 
        err = sock_create(family, type, protocol, &sock2);
        if (err < 0)
                goto out_release_1;
+       set_bit(SOCK_USER_SOCKET, &sock2->flags);
 
        err = sock1->ops->socketpair(sock1, sock2);
        if (err < 0) 
@@ -1287,7 +1381,7 @@ asmlinkage long sys_accept(int fd, struct sockaddr __user *upeer_sockaddr, int _
        if (!sock)
                goto out;
 
-       err = -EMFILE;
+       err = -ENFILE;
        if (!(newsock = sock_alloc())) 
                goto out_put;
 
@@ -1817,6 +1911,8 @@ out:
        return err;
 }
 
+#ifdef __ARCH_WANT_SYS_SOCKETCALL
+
 /* Argument list sizes for sys_socketcall */
 #define AL(x) ((x) * sizeof(unsigned long))
 static unsigned char nargs[18]={AL(0),AL(3),AL(3),AL(3),AL(2),AL(3),
@@ -1910,6 +2006,8 @@ asmlinkage long sys_socketcall(int call, unsigned long __user *args)
        return err;
 }
 
+#endif /* __ARCH_WANT_SYS_SOCKETCALL */
+
 /*
  *     This function is called by a protocol handler that wants to
  *     advertise its address family, and have it linked into the
@@ -1960,15 +2058,6 @@ extern void sk_init(void);
 
 void __init sock_init(void)
 {
-       int i;
-
-       /*
-        *      Initialize all address (protocol) families. 
-        */
-        
-       for (i = 0; i < NPROTO; i++) 
-               net_families[i] = NULL;
-
        /*
         *      Initialize sock SLAB cache.
         */
@@ -2018,8 +2107,6 @@ void socket_seq_show(struct seq_file *seq)
 /* ABI emulation layers need these two */
 EXPORT_SYMBOL(move_addr_to_kernel);
 EXPORT_SYMBOL(move_addr_to_user);
-EXPORT_SYMBOL(sock_alloc);
-EXPORT_SYMBOL(sock_alloc_inode);
 EXPORT_SYMBOL(sock_create);
 EXPORT_SYMBOL(sock_create_kern);
 EXPORT_SYMBOL(sock_create_lite);
@@ -2031,3 +2118,5 @@ EXPORT_SYMBOL(sock_sendmsg);
 EXPORT_SYMBOL(sock_unregister);
 EXPORT_SYMBOL(sock_wake_async);
 EXPORT_SYMBOL(sockfd_lookup);
+EXPORT_SYMBOL(kernel_sendmsg);
+EXPORT_SYMBOL(kernel_recvmsg);