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