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