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