Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[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/config.h>
62 #include <linux/mm.h>
63 #include <linux/smp_lock.h>
64 #include <linux/socket.h>
65 #include <linux/file.h>
66 #include <linux/net.h>
67 #include <linux/interrupt.h>
68 #include <linux/netdevice.h>
69 #include <linux/proc_fs.h>
70 #include <linux/seq_file.h>
71 #include <net/tux.h>
72 #include <linux/mutex.h>
73 #include <linux/wanrouter.h>
74 #include <linux/if_bridge.h>
75 #include <linux/if_frad.h>
76 #include <linux/if_vlan.h>
77 #include <linux/init.h>
78 #include <linux/poll.h>
79 #include <linux/cache.h>
80 #include <linux/module.h>
81 #include <linux/highmem.h>
82 #include <linux/divert.h>
83 #include <linux/mount.h>
84 #include <linux/security.h>
85 #include <linux/syscalls.h>
86 #include <linux/compat.h>
87 #include <linux/kmod.h>
88 #include <linux/audit.h>
89 #include <linux/wireless.h>
90
91 #include <asm/uaccess.h>
92 #include <asm/unistd.h>
93
94 #include <net/compat.h>
95
96 #include <net/sock.h>
97 #include <linux/netfilter.h>
98 #include <linux/vs_socket.h>
99
100 static int sock_no_open(struct inode *irrelevant, struct file *dontcare);
101 static ssize_t sock_aio_read(struct kiocb *iocb, char __user *buf,
102                          size_t size, loff_t pos);
103 static ssize_t sock_aio_write(struct kiocb *iocb, const char __user *buf,
104                           size_t size, loff_t pos);
105 static int sock_mmap(struct file *file, struct vm_area_struct * vma);
106
107 static int sock_close(struct inode *inode, struct file *file);
108 static unsigned int sock_poll(struct file *file,
109                               struct poll_table_struct *wait);
110 static long sock_ioctl(struct file *file,
111                       unsigned int cmd, unsigned long arg);
112 #ifdef CONFIG_COMPAT
113 static long compat_sock_ioctl(struct file *file,
114                       unsigned int cmd, unsigned long arg);
115 #endif
116 static int sock_fasync(int fd, struct file *filp, int on);
117 static ssize_t sock_readv(struct file *file, const struct iovec *vector,
118                           unsigned long count, loff_t *ppos);
119 static ssize_t sock_writev(struct file *file, const struct iovec *vector,
120                           unsigned long count, loff_t *ppos);
121 static ssize_t sock_sendpage(struct file *file, struct page *page,
122                              int offset, size_t size, loff_t *ppos, int more);
123
124 /*
125  *      Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
126  *      in the operation structures but are done directly via the socketcall() multiplexor.
127  */
128
129 struct file_operations socket_file_ops = {
130         .owner =        THIS_MODULE,
131         .llseek =       no_llseek,
132         .aio_read =     sock_aio_read,
133         .aio_write =    sock_aio_write,
134         .poll =         sock_poll,
135         .unlocked_ioctl = sock_ioctl,
136 #ifdef CONFIG_COMPAT
137         .compat_ioctl = compat_sock_ioctl,
138 #endif
139         .mmap =         sock_mmap,
140         .open =         sock_no_open,   /* special open code to disallow open via /proc */
141         .release =      sock_close,
142         .fasync =       sock_fasync,
143         .readv =        sock_readv,
144         .writev =       sock_writev,
145         .sendpage =     sock_sendpage,
146         .splice_write = generic_splice_sendpage,
147 };
148
149 /*
150  *      The protocol list. Each protocol is registered in here.
151  */
152
153 static struct net_proto_family *net_families[NPROTO];
154
155 #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
156 static atomic_t net_family_lockct = ATOMIC_INIT(0);
157 static DEFINE_SPINLOCK(net_family_lock);
158
159 /* The strategy is: modifications net_family vector are short, do not
160    sleep and veeery rare, but read access should be free of any exclusive
161    locks.
162  */
163
164 static void net_family_write_lock(void)
165 {
166         spin_lock(&net_family_lock);
167         while (atomic_read(&net_family_lockct) != 0) {
168                 spin_unlock(&net_family_lock);
169
170                 yield();
171
172                 spin_lock(&net_family_lock);
173         }
174 }
175
176 static __inline__ void net_family_write_unlock(void)
177 {
178         spin_unlock(&net_family_lock);
179 }
180
181 static __inline__ void net_family_read_lock(void)
182 {
183         atomic_inc(&net_family_lockct);
184         spin_unlock_wait(&net_family_lock);
185 }
186
187 static __inline__ void net_family_read_unlock(void)
188 {
189         atomic_dec(&net_family_lockct);
190 }
191
192 #else
193 #define net_family_write_lock() do { } while(0)
194 #define net_family_write_unlock() do { } while(0)
195 #define net_family_read_lock() do { } while(0)
196 #define net_family_read_unlock() do { } while(0)
197 #endif
198
199
200 /*
201  *      Statistics counters of the socket lists
202  */
203
204 static DEFINE_PER_CPU(int, sockets_in_use) = 0;
205
206 /*
207  *      Support routines. Move socket addresses back and forth across the kernel/user
208  *      divide and look after the messy bits.
209  */
210
211 #define MAX_SOCK_ADDR   128             /* 108 for Unix domain - 
212                                            16 for IP, 16 for IPX,
213                                            24 for IPv6,
214                                            about 80 for AX.25 
215                                            must be at least one bigger than
216                                            the AF_UNIX size (see net/unix/af_unix.c
217                                            :unix_mkname()).  
218                                          */
219                                          
220 /**
221  *      move_addr_to_kernel     -       copy a socket address into kernel space
222  *      @uaddr: Address in user space
223  *      @kaddr: Address in kernel space
224  *      @ulen: Length in user space
225  *
226  *      The address is copied into kernel space. If the provided address is
227  *      too long an error code of -EINVAL is returned. If the copy gives
228  *      invalid addresses -EFAULT is returned. On a success 0 is returned.
229  */
230
231 int move_addr_to_kernel(void __user *uaddr, int ulen, void *kaddr)
232 {
233         if(ulen<0||ulen>MAX_SOCK_ADDR)
234                 return -EINVAL;
235         if(ulen==0)
236                 return 0;
237         if(copy_from_user(kaddr,uaddr,ulen))
238                 return -EFAULT;
239         return audit_sockaddr(ulen, kaddr);
240 }
241
242 /**
243  *      move_addr_to_user       -       copy an address to user space
244  *      @kaddr: kernel space address
245  *      @klen: length of address in kernel
246  *      @uaddr: user space address
247  *      @ulen: pointer to user length field
248  *
249  *      The value pointed to by ulen on entry is the buffer length available.
250  *      This is overwritten with the buffer space used. -EINVAL is returned
251  *      if an overlong buffer is specified or a negative buffer size. -EFAULT
252  *      is returned if either the buffer or the length field are not
253  *      accessible.
254  *      After copying the data up to the limit the user specifies, the true
255  *      length of the data is written over the length limit the user
256  *      specified. Zero is returned for a success.
257  */
258  
259 int move_addr_to_user(void *kaddr, int klen, void __user *uaddr, int __user *ulen)
260 {
261         int err;
262         int len;
263
264         if((err=get_user(len, ulen)))
265                 return err;
266         if(len>klen)
267                 len=klen;
268         if(len<0 || len> MAX_SOCK_ADDR)
269                 return -EINVAL;
270         if(len)
271         {
272                 if (audit_sockaddr(klen, kaddr))
273                         return -ENOMEM;
274                 if(copy_to_user(uaddr,kaddr,len))
275                         return -EFAULT;
276         }
277         /*
278          *      "fromlen shall refer to the value before truncation.."
279          *                      1003.1g
280          */
281         return __put_user(klen, ulen);
282 }
283
284 #define SOCKFS_MAGIC 0x534F434B
285
286 static kmem_cache_t * sock_inode_cachep __read_mostly;
287
288 static struct inode *sock_alloc_inode(struct super_block *sb)
289 {
290         struct socket_alloc *ei;
291         ei = (struct socket_alloc *)kmem_cache_alloc(sock_inode_cachep, SLAB_KERNEL);
292         if (!ei)
293                 return NULL;
294         init_waitqueue_head(&ei->socket.wait);
295         
296         ei->socket.fasync_list = NULL;
297         ei->socket.state = SS_UNCONNECTED;
298         ei->socket.flags = 0;
299         ei->socket.ops = NULL;
300         ei->socket.sk = NULL;
301         ei->socket.file = NULL;
302         ei->socket.flags = 0;
303
304         return &ei->vfs_inode;
305 }
306
307 static void sock_destroy_inode(struct inode *inode)
308 {
309         kmem_cache_free(sock_inode_cachep,
310                         container_of(inode, struct socket_alloc, vfs_inode));
311 }
312
313 static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
314 {
315         struct socket_alloc *ei = (struct socket_alloc *) foo;
316
317         if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
318             SLAB_CTOR_CONSTRUCTOR)
319                 inode_init_once(&ei->vfs_inode);
320 }
321  
322 static int init_inodecache(void)
323 {
324         sock_inode_cachep = kmem_cache_create("sock_inode_cache",
325                                 sizeof(struct socket_alloc),
326                                 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
327                                         SLAB_MEM_SPREAD),
328                                 init_once, NULL);
329         if (sock_inode_cachep == NULL)
330                 return -ENOMEM;
331         return 0;
332 }
333
334 static struct super_operations sockfs_ops = {
335         .alloc_inode =  sock_alloc_inode,
336         .destroy_inode =sock_destroy_inode,
337         .statfs =       simple_statfs,
338 };
339
340 static struct super_block *sockfs_get_sb(struct file_system_type *fs_type,
341         int flags, const char *dev_name, void *data)
342 {
343         return get_sb_pseudo(fs_type, "socket:", &sockfs_ops, SOCKFS_MAGIC);
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                 printk(KERN_WARNING "socket: no more sockets\n");
1216                 err = -ENFILE;          /* Not exactly a match, but its the
1217                                            closest posix thing */
1218                 goto out;
1219         }
1220
1221         sock->type  = type;
1222
1223         /*
1224          * We will call the ->create function, that possibly is in a loadable
1225          * module, so we have to bump that loadable module refcnt first.
1226          */
1227         err = -EAFNOSUPPORT;
1228         if (!try_module_get(net_families[family]->owner))
1229                 goto out_release;
1230
1231         if ((err = net_families[family]->create(sock, protocol)) < 0) {
1232                 sock->ops = NULL;
1233                 goto out_module_put;
1234         }
1235
1236         /*
1237          * Now to bump the refcnt of the [loadable] module that owns this
1238          * socket at sock_release time we decrement its refcnt.
1239          */
1240         if (!try_module_get(sock->ops->owner)) {
1241                 sock->ops = NULL;
1242                 goto out_module_put;
1243         }
1244         /*
1245          * Now that we're done with the ->create function, the [loadable]
1246          * module can have its refcnt decremented
1247          */
1248         module_put(net_families[family]->owner);
1249         *res = sock;
1250         security_socket_post_create(sock, family, type, protocol, kern);
1251
1252 out:
1253         net_family_read_unlock();
1254         return err;
1255 out_module_put:
1256         module_put(net_families[family]->owner);
1257 out_release:
1258         sock_release(sock);
1259         goto out;
1260 }
1261
1262 int sock_create(int family, int type, int protocol, struct socket **res)
1263 {
1264         return __sock_create(family, type, protocol, res, 0);
1265 }
1266
1267 int sock_create_kern(int family, int type, int protocol, struct socket **res)
1268 {
1269         return __sock_create(family, type, protocol, res, 1);
1270 }
1271
1272 asmlinkage long sys_socket(int family, int type, int protocol)
1273 {
1274         int retval;
1275         struct socket *sock;
1276
1277         retval = sock_create(family, type, protocol, &sock);
1278         if (retval < 0)
1279                 goto out;
1280
1281         set_bit(SOCK_USER_SOCKET, &sock->flags);
1282         retval = sock_map_fd(sock);
1283         if (retval < 0)
1284                 goto out_release;
1285
1286 out:
1287         /* It may be already another descriptor 8) Not kernel problem. */
1288         return retval;
1289
1290 out_release:
1291         sock_release(sock);
1292         return retval;
1293 }
1294
1295 /*
1296  *      Create a pair of connected sockets.
1297  */
1298
1299 asmlinkage long sys_socketpair(int family, int type, int protocol, int __user *usockvec)
1300 {
1301         struct socket *sock1, *sock2;
1302         int fd1, fd2, err;
1303
1304         /*
1305          * Obtain the first socket and check if the underlying protocol
1306          * supports the socketpair call.
1307          */
1308
1309         err = sock_create(family, type, protocol, &sock1);
1310         if (err < 0)
1311                 goto out;
1312         set_bit(SOCK_USER_SOCKET, &sock1->flags);
1313
1314         err = sock_create(family, type, protocol, &sock2);
1315         if (err < 0)
1316                 goto out_release_1;
1317         set_bit(SOCK_USER_SOCKET, &sock2->flags);
1318
1319         err = sock1->ops->socketpair(sock1, sock2);
1320         if (err < 0) 
1321                 goto out_release_both;
1322
1323         fd1 = fd2 = -1;
1324
1325         err = sock_map_fd(sock1);
1326         if (err < 0)
1327                 goto out_release_both;
1328         fd1 = err;
1329
1330         err = sock_map_fd(sock2);
1331         if (err < 0)
1332                 goto out_close_1;
1333         fd2 = err;
1334
1335         /* fd1 and fd2 may be already another descriptors.
1336          * Not kernel problem.
1337          */
1338
1339         err = put_user(fd1, &usockvec[0]); 
1340         if (!err)
1341                 err = put_user(fd2, &usockvec[1]);
1342         if (!err)
1343                 return 0;
1344
1345         sys_close(fd2);
1346         sys_close(fd1);
1347         return err;
1348
1349 out_close_1:
1350         sock_release(sock2);
1351         sys_close(fd1);
1352         return err;
1353
1354 out_release_both:
1355         sock_release(sock2);
1356 out_release_1:
1357         sock_release(sock1);
1358 out:
1359         return err;
1360 }
1361
1362
1363 /*
1364  *      Bind a name to a socket. Nothing much to do here since it's
1365  *      the protocol's responsibility to handle the local address.
1366  *
1367  *      We move the socket address to kernel space before we call
1368  *      the protocol layer (having also checked the address is ok).
1369  */
1370
1371 asmlinkage long sys_bind(int fd, struct sockaddr __user *umyaddr, int addrlen)
1372 {
1373         struct socket *sock;
1374         char address[MAX_SOCK_ADDR];
1375         int err, fput_needed;
1376
1377         if((sock = sockfd_lookup_light(fd, &err, &fput_needed))!=NULL)
1378         {
1379                 if((err=move_addr_to_kernel(umyaddr,addrlen,address))>=0) {
1380                         err = security_socket_bind(sock, (struct sockaddr *)address, addrlen);
1381                         if (!err)
1382                                 err = sock->ops->bind(sock,
1383                                         (struct sockaddr *)address, addrlen);
1384                 }
1385                 fput_light(sock->file, fput_needed);
1386         }                       
1387         return err;
1388 }
1389
1390
1391 /*
1392  *      Perform a listen. Basically, we allow the protocol to do anything
1393  *      necessary for a listen, and if that works, we mark the socket as
1394  *      ready for listening.
1395  */
1396
1397 int sysctl_somaxconn = SOMAXCONN;
1398
1399 asmlinkage long sys_listen(int fd, int backlog)
1400 {
1401         struct socket *sock;
1402         int err, fput_needed;
1403         
1404         if ((sock = sockfd_lookup_light(fd, &err, &fput_needed)) != NULL) {
1405                 if ((unsigned) backlog > sysctl_somaxconn)
1406                         backlog = sysctl_somaxconn;
1407
1408                 err = security_socket_listen(sock, backlog);
1409                 if (!err)
1410                         err = sock->ops->listen(sock, backlog);
1411
1412                 fput_light(sock->file, fput_needed);
1413         }
1414         return err;
1415 }
1416
1417
1418 /*
1419  *      For accept, we attempt to create a new socket, set up the link
1420  *      with the client, wake up the client, then return the new
1421  *      connected fd. We collect the address of the connector in kernel
1422  *      space and move it to user at the very end. This is unclean because
1423  *      we open the socket then return an error.
1424  *
1425  *      1003.1g adds the ability to recvmsg() to query connection pending
1426  *      status to recvmsg. We need to add that support in a way thats
1427  *      clean when we restucture accept also.
1428  */
1429
1430 asmlinkage long sys_accept(int fd, struct sockaddr __user *upeer_sockaddr, int __user *upeer_addrlen)
1431 {
1432         struct socket *sock, *newsock;
1433         struct file *newfile;
1434         int err, len, newfd, fput_needed;
1435         char address[MAX_SOCK_ADDR];
1436
1437         sock = sockfd_lookup_light(fd, &err, &fput_needed);
1438         if (!sock)
1439                 goto out;
1440
1441         err = -ENFILE;
1442         if (!(newsock = sock_alloc())) 
1443                 goto out_put;
1444
1445         newsock->type = sock->type;
1446         newsock->ops = sock->ops;
1447
1448         /*
1449          * We don't need try_module_get here, as the listening socket (sock)
1450          * has the protocol module (sock->ops->owner) held.
1451          */
1452         __module_get(newsock->ops->owner);
1453
1454         newfd = sock_alloc_fd(&newfile);
1455         if (unlikely(newfd < 0)) {
1456                 err = newfd;
1457                 sock_release(newsock);
1458                 goto out_put;
1459         }
1460
1461         err = sock_attach_fd(newsock, newfile);
1462         if (err < 0)
1463                 goto out_fd;
1464
1465         err = security_socket_accept(sock, newsock);
1466         if (err)
1467                 goto out_fd;
1468
1469         err = sock->ops->accept(sock, newsock, sock->file->f_flags);
1470         if (err < 0)
1471                 goto out_fd;
1472
1473         if (upeer_sockaddr) {
1474                 if(newsock->ops->getname(newsock, (struct sockaddr *)address, &len, 2)<0) {
1475                         err = -ECONNABORTED;
1476                         goto out_fd;
1477                 }
1478                 err = move_addr_to_user(address, len, upeer_sockaddr, upeer_addrlen);
1479                 if (err < 0)
1480                         goto out_fd;
1481         }
1482
1483         /* File flags are not inherited via accept() unlike another OSes. */
1484
1485         fd_install(newfd, newfile);
1486         err = newfd;
1487
1488         security_socket_post_accept(sock, newsock);
1489
1490 out_put:
1491         fput_light(sock->file, fput_needed);
1492 out:
1493         return err;
1494 out_fd:
1495         fput(newfile);
1496         put_unused_fd(newfd);
1497         goto out_put;
1498 }
1499
1500
1501 /*
1502  *      Attempt to connect to a socket with the server address.  The address
1503  *      is in user space so we verify it is OK and move it to kernel space.
1504  *
1505  *      For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
1506  *      break bindings
1507  *
1508  *      NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
1509  *      other SEQPACKET protocols that take time to connect() as it doesn't
1510  *      include the -EINPROGRESS status for such sockets.
1511  */
1512
1513 asmlinkage long sys_connect(int fd, struct sockaddr __user *uservaddr, int addrlen)
1514 {
1515         struct socket *sock;
1516         char address[MAX_SOCK_ADDR];
1517         int err, fput_needed;
1518
1519         sock = sockfd_lookup_light(fd, &err, &fput_needed);
1520         if (!sock)
1521                 goto out;
1522         err = move_addr_to_kernel(uservaddr, addrlen, address);
1523         if (err < 0)
1524                 goto out_put;
1525
1526         err = security_socket_connect(sock, (struct sockaddr *)address, addrlen);
1527         if (err)
1528                 goto out_put;
1529
1530         err = sock->ops->connect(sock, (struct sockaddr *) address, addrlen,
1531                                  sock->file->f_flags);
1532 out_put:
1533         fput_light(sock->file, fput_needed);
1534 out:
1535         return err;
1536 }
1537
1538 /*
1539  *      Get the local address ('name') of a socket object. Move the obtained
1540  *      name to user space.
1541  */
1542
1543 asmlinkage long sys_getsockname(int fd, struct sockaddr __user *usockaddr, int __user *usockaddr_len)
1544 {
1545         struct socket *sock;
1546         char address[MAX_SOCK_ADDR];
1547         int len, err, fput_needed;
1548         
1549         sock = sockfd_lookup_light(fd, &err, &fput_needed);
1550         if (!sock)
1551                 goto out;
1552
1553         err = security_socket_getsockname(sock);
1554         if (err)
1555                 goto out_put;
1556
1557         err = sock->ops->getname(sock, (struct sockaddr *)address, &len, 0);
1558         if (err)
1559                 goto out_put;
1560         err = move_addr_to_user(address, len, usockaddr, usockaddr_len);
1561
1562 out_put:
1563         fput_light(sock->file, fput_needed);
1564 out:
1565         return err;
1566 }
1567
1568 /*
1569  *      Get the remote address ('name') of a socket object. Move the obtained
1570  *      name to user space.
1571  */
1572
1573 asmlinkage long sys_getpeername(int fd, struct sockaddr __user *usockaddr, int __user *usockaddr_len)
1574 {
1575         struct socket *sock;
1576         char address[MAX_SOCK_ADDR];
1577         int len, err, fput_needed;
1578
1579         if ((sock = sockfd_lookup_light(fd, &err, &fput_needed)) != NULL) {
1580                 err = security_socket_getpeername(sock);
1581                 if (err) {
1582                         fput_light(sock->file, fput_needed);
1583                         return err;
1584                 }
1585
1586                 err = sock->ops->getname(sock, (struct sockaddr *)address, &len, 1);
1587                 if (!err)
1588                         err=move_addr_to_user(address,len, usockaddr, usockaddr_len);
1589                 fput_light(sock->file, fput_needed);
1590         }
1591         return err;
1592 }
1593
1594 /*
1595  *      Send a datagram to a given address. We move the address into kernel
1596  *      space and check the user space data area is readable before invoking
1597  *      the protocol.
1598  */
1599
1600 asmlinkage long sys_sendto(int fd, void __user * buff, size_t len, unsigned flags,
1601                            struct sockaddr __user *addr, int addr_len)
1602 {
1603         struct socket *sock;
1604         char address[MAX_SOCK_ADDR];
1605         int err;
1606         struct msghdr msg;
1607         struct iovec iov;
1608         int fput_needed;
1609         struct file *sock_file;
1610
1611         sock_file = fget_light(fd, &fput_needed);
1612         if (!sock_file)
1613                 return -EBADF;
1614
1615         sock = sock_from_file(sock_file, &err);
1616         if (!sock)
1617                 goto out_put;
1618         iov.iov_base=buff;
1619         iov.iov_len=len;
1620         msg.msg_name=NULL;
1621         msg.msg_iov=&iov;
1622         msg.msg_iovlen=1;
1623         msg.msg_control=NULL;
1624         msg.msg_controllen=0;
1625         msg.msg_namelen=0;
1626         if (addr) {
1627                 err = move_addr_to_kernel(addr, addr_len, address);
1628                 if (err < 0)
1629                         goto out_put;
1630                 msg.msg_name=address;
1631                 msg.msg_namelen=addr_len;
1632         }
1633         if (sock->file->f_flags & O_NONBLOCK)
1634                 flags |= MSG_DONTWAIT;
1635         msg.msg_flags = flags;
1636         err = sock_sendmsg(sock, &msg, len);
1637
1638 out_put:                
1639         fput_light(sock_file, fput_needed);
1640         return err;
1641 }
1642
1643 /*
1644  *      Send a datagram down a socket. 
1645  */
1646
1647 asmlinkage long sys_send(int fd, void __user * buff, size_t len, unsigned flags)
1648 {
1649         return sys_sendto(fd, buff, len, flags, NULL, 0);
1650 }
1651
1652 /*
1653  *      Receive a frame from the socket and optionally record the address of the 
1654  *      sender. We verify the buffers are writable and if needed move the
1655  *      sender address from kernel to user space.
1656  */
1657
1658 asmlinkage long sys_recvfrom(int fd, void __user * ubuf, size_t size, unsigned flags,
1659                              struct sockaddr __user *addr, int __user *addr_len)
1660 {
1661         struct socket *sock;
1662         struct iovec iov;
1663         struct msghdr msg;
1664         char address[MAX_SOCK_ADDR];
1665         int err,err2;
1666         struct file *sock_file;
1667         int fput_needed;
1668
1669         sock_file = fget_light(fd, &fput_needed);
1670         if (!sock_file)
1671                 return -EBADF;
1672
1673         sock = sock_from_file(sock_file, &err);
1674         if (!sock)
1675                 goto out;
1676
1677         msg.msg_control=NULL;
1678         msg.msg_controllen=0;
1679         msg.msg_iovlen=1;
1680         msg.msg_iov=&iov;
1681         iov.iov_len=size;
1682         iov.iov_base=ubuf;
1683         msg.msg_name=address;
1684         msg.msg_namelen=MAX_SOCK_ADDR;
1685         if (sock->file->f_flags & O_NONBLOCK)
1686                 flags |= MSG_DONTWAIT;
1687         err=sock_recvmsg(sock, &msg, size, flags);
1688
1689         if(err >= 0 && addr != NULL)
1690         {
1691                 err2=move_addr_to_user(address, msg.msg_namelen, addr, addr_len);
1692                 if(err2<0)
1693                         err=err2;
1694         }
1695 out:
1696         fput_light(sock_file, fput_needed);
1697         return err;
1698 }
1699
1700 /*
1701  *      Receive a datagram from a socket. 
1702  */
1703
1704 asmlinkage long sys_recv(int fd, void __user * ubuf, size_t size, unsigned flags)
1705 {
1706         return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
1707 }
1708
1709 /*
1710  *      Set a socket option. Because we don't know the option lengths we have
1711  *      to pass the user mode parameter for the protocols to sort out.
1712  */
1713
1714 asmlinkage long sys_setsockopt(int fd, int level, int optname, char __user *optval, int optlen)
1715 {
1716         int err, fput_needed;
1717         struct socket *sock;
1718
1719         if (optlen < 0)
1720                 return -EINVAL;
1721                         
1722         if ((sock = sockfd_lookup_light(fd, &err, &fput_needed)) != NULL)
1723         {
1724                 err = security_socket_setsockopt(sock,level,optname);
1725                 if (err)
1726                         goto out_put;
1727
1728                 if (level == SOL_SOCKET)
1729                         err=sock_setsockopt(sock,level,optname,optval,optlen);
1730                 else
1731                         err=sock->ops->setsockopt(sock, level, optname, optval, optlen);
1732 out_put:
1733                 fput_light(sock->file, fput_needed);
1734         }
1735         return err;
1736 }
1737
1738 /*
1739  *      Get a socket option. Because we don't know the option lengths we have
1740  *      to pass a user mode parameter for the protocols to sort out.
1741  */
1742
1743 asmlinkage long sys_getsockopt(int fd, int level, int optname, char __user *optval, int __user *optlen)
1744 {
1745         int err, fput_needed;
1746         struct socket *sock;
1747
1748         if ((sock = sockfd_lookup_light(fd, &err, &fput_needed)) != NULL) {
1749                 err = security_socket_getsockopt(sock, level, optname);
1750                 if (err)
1751                         goto out_put;
1752
1753                 if (level == SOL_SOCKET)
1754                         err=sock_getsockopt(sock,level,optname,optval,optlen);
1755                 else
1756                         err=sock->ops->getsockopt(sock, level, optname, optval, optlen);
1757 out_put:
1758                 fput_light(sock->file, fput_needed);
1759         }
1760         return err;
1761 }
1762
1763
1764 /*
1765  *      Shutdown a socket.
1766  */
1767
1768 asmlinkage long sys_shutdown(int fd, int how)
1769 {
1770         int err, fput_needed;
1771         struct socket *sock;
1772
1773         if ((sock = sockfd_lookup_light(fd, &err, &fput_needed))!=NULL)
1774         {
1775                 err = security_socket_shutdown(sock, how);
1776                 if (!err)
1777                         err = sock->ops->shutdown(sock, how);
1778                 fput_light(sock->file, fput_needed);
1779         }
1780         return err;
1781 }
1782
1783 /* A couple of helpful macros for getting the address of the 32/64 bit 
1784  * fields which are the same type (int / unsigned) on our platforms.
1785  */
1786 #define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
1787 #define COMPAT_NAMELEN(msg)     COMPAT_MSG(msg, msg_namelen)
1788 #define COMPAT_FLAGS(msg)       COMPAT_MSG(msg, msg_flags)
1789
1790
1791 /*
1792  *      BSD sendmsg interface
1793  */
1794
1795 asmlinkage long sys_sendmsg(int fd, struct msghdr __user *msg, unsigned flags)
1796 {
1797         struct compat_msghdr __user *msg_compat = (struct compat_msghdr __user *)msg;
1798         struct socket *sock;
1799         char address[MAX_SOCK_ADDR];
1800         struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
1801         unsigned char ctl[sizeof(struct cmsghdr) + 20]
1802                         __attribute__ ((aligned (sizeof(__kernel_size_t))));
1803                         /* 20 is size of ipv6_pktinfo */
1804         unsigned char *ctl_buf = ctl;
1805         struct msghdr msg_sys;
1806         int err, ctl_len, iov_size, total_len;
1807         int fput_needed;
1808         
1809         err = -EFAULT;
1810         if (MSG_CMSG_COMPAT & flags) {
1811                 if (get_compat_msghdr(&msg_sys, msg_compat))
1812                         return -EFAULT;
1813         } else if (copy_from_user(&msg_sys, msg, sizeof(struct msghdr)))
1814                 return -EFAULT;
1815
1816         sock = sockfd_lookup_light(fd, &err, &fput_needed);
1817         if (!sock) 
1818                 goto out;
1819
1820         /* do not move before msg_sys is valid */
1821         err = -EMSGSIZE;
1822         if (msg_sys.msg_iovlen > UIO_MAXIOV)
1823                 goto out_put;
1824
1825         /* Check whether to allocate the iovec area*/
1826         err = -ENOMEM;
1827         iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
1828         if (msg_sys.msg_iovlen > UIO_FASTIOV) {
1829                 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
1830                 if (!iov)
1831                         goto out_put;
1832         }
1833
1834         /* This will also move the address data into kernel space */
1835         if (MSG_CMSG_COMPAT & flags) {
1836                 err = verify_compat_iovec(&msg_sys, iov, address, VERIFY_READ);
1837         } else
1838                 err = verify_iovec(&msg_sys, iov, address, VERIFY_READ);
1839         if (err < 0) 
1840                 goto out_freeiov;
1841         total_len = err;
1842
1843         err = -ENOBUFS;
1844
1845         if (msg_sys.msg_controllen > INT_MAX)
1846                 goto out_freeiov;
1847         ctl_len = msg_sys.msg_controllen; 
1848         if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
1849                 err = cmsghdr_from_user_compat_to_kern(&msg_sys, sock->sk, ctl, sizeof(ctl));
1850                 if (err)
1851                         goto out_freeiov;
1852                 ctl_buf = msg_sys.msg_control;
1853                 ctl_len = msg_sys.msg_controllen;
1854         } else if (ctl_len) {
1855                 if (ctl_len > sizeof(ctl))
1856                 {
1857                         ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
1858                         if (ctl_buf == NULL) 
1859                                 goto out_freeiov;
1860                 }
1861                 err = -EFAULT;
1862                 /*
1863                  * Careful! Before this, msg_sys.msg_control contains a user pointer.
1864                  * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
1865                  * checking falls down on this.
1866                  */
1867                 if (copy_from_user(ctl_buf, (void __user *) msg_sys.msg_control, ctl_len))
1868                         goto out_freectl;
1869                 msg_sys.msg_control = ctl_buf;
1870         }
1871         msg_sys.msg_flags = flags;
1872
1873         if (sock->file->f_flags & O_NONBLOCK)
1874                 msg_sys.msg_flags |= MSG_DONTWAIT;
1875         err = sock_sendmsg(sock, &msg_sys, total_len);
1876
1877 out_freectl:
1878         if (ctl_buf != ctl)    
1879                 sock_kfree_s(sock->sk, ctl_buf, ctl_len);
1880 out_freeiov:
1881         if (iov != iovstack)
1882                 sock_kfree_s(sock->sk, iov, iov_size);
1883 out_put:
1884         fput_light(sock->file, fput_needed);
1885 out:       
1886         return err;
1887 }
1888
1889 /*
1890  *      BSD recvmsg interface
1891  */
1892
1893 asmlinkage long sys_recvmsg(int fd, struct msghdr __user *msg, unsigned int flags)
1894 {
1895         struct compat_msghdr __user *msg_compat = (struct compat_msghdr __user *)msg;
1896         struct socket *sock;
1897         struct iovec iovstack[UIO_FASTIOV];
1898         struct iovec *iov=iovstack;
1899         struct msghdr msg_sys;
1900         unsigned long cmsg_ptr;
1901         int err, iov_size, total_len, len;
1902         int fput_needed;
1903
1904         /* kernel mode address */
1905         char addr[MAX_SOCK_ADDR];
1906
1907         /* user mode address pointers */
1908         struct sockaddr __user *uaddr;
1909         int __user *uaddr_len;
1910         
1911         if (MSG_CMSG_COMPAT & flags) {
1912                 if (get_compat_msghdr(&msg_sys, msg_compat))
1913                         return -EFAULT;
1914         } else
1915                 if (copy_from_user(&msg_sys,msg,sizeof(struct msghdr)))
1916                         return -EFAULT;
1917
1918         sock = sockfd_lookup_light(fd, &err, &fput_needed);
1919         if (!sock)
1920                 goto out;
1921
1922         err = -EMSGSIZE;
1923         if (msg_sys.msg_iovlen > UIO_MAXIOV)
1924                 goto out_put;
1925         
1926         /* Check whether to allocate the iovec area*/
1927         err = -ENOMEM;
1928         iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
1929         if (msg_sys.msg_iovlen > UIO_FASTIOV) {
1930                 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
1931                 if (!iov)
1932                         goto out_put;
1933         }
1934
1935         /*
1936          *      Save the user-mode address (verify_iovec will change the
1937          *      kernel msghdr to use the kernel address space)
1938          */
1939          
1940         uaddr = (void __user *) msg_sys.msg_name;
1941         uaddr_len = COMPAT_NAMELEN(msg);
1942         if (MSG_CMSG_COMPAT & flags) {
1943                 err = verify_compat_iovec(&msg_sys, iov, addr, VERIFY_WRITE);
1944         } else
1945                 err = verify_iovec(&msg_sys, iov, addr, VERIFY_WRITE);
1946         if (err < 0)
1947                 goto out_freeiov;
1948         total_len=err;
1949
1950         cmsg_ptr = (unsigned long)msg_sys.msg_control;
1951         msg_sys.msg_flags = 0;
1952         if (MSG_CMSG_COMPAT & flags)
1953                 msg_sys.msg_flags = MSG_CMSG_COMPAT;
1954         
1955         if (sock->file->f_flags & O_NONBLOCK)
1956                 flags |= MSG_DONTWAIT;
1957         err = sock_recvmsg(sock, &msg_sys, total_len, flags);
1958         if (err < 0)
1959                 goto out_freeiov;
1960         len = err;
1961
1962         if (uaddr != NULL) {
1963                 err = move_addr_to_user(addr, msg_sys.msg_namelen, uaddr, uaddr_len);
1964                 if (err < 0)
1965                         goto out_freeiov;
1966         }
1967         err = __put_user((msg_sys.msg_flags & ~MSG_CMSG_COMPAT),
1968                          COMPAT_FLAGS(msg));
1969         if (err)
1970                 goto out_freeiov;
1971         if (MSG_CMSG_COMPAT & flags)
1972                 err = __put_user((unsigned long)msg_sys.msg_control-cmsg_ptr, 
1973                                  &msg_compat->msg_controllen);
1974         else
1975                 err = __put_user((unsigned long)msg_sys.msg_control-cmsg_ptr, 
1976                                  &msg->msg_controllen);
1977         if (err)
1978                 goto out_freeiov;
1979         err = len;
1980
1981 out_freeiov:
1982         if (iov != iovstack)
1983                 sock_kfree_s(sock->sk, iov, iov_size);
1984 out_put:
1985         fput_light(sock->file, fput_needed);
1986 out:
1987         return err;
1988 }
1989
1990 #ifdef __ARCH_WANT_SYS_SOCKETCALL
1991
1992 /* Argument list sizes for sys_socketcall */
1993 #define AL(x) ((x) * sizeof(unsigned long))
1994 static unsigned char nargs[18]={AL(0),AL(3),AL(3),AL(3),AL(2),AL(3),
1995                                 AL(3),AL(3),AL(4),AL(4),AL(4),AL(6),
1996                                 AL(6),AL(2),AL(5),AL(5),AL(3),AL(3)};
1997 #undef AL
1998
1999 /*
2000  *      System call vectors. 
2001  *
2002  *      Argument checking cleaned up. Saved 20% in size.
2003  *  This function doesn't need to set the kernel lock because
2004  *  it is set by the callees. 
2005  */
2006
2007 asmlinkage long sys_socketcall(int call, unsigned long __user *args)
2008 {
2009         unsigned long a[6];
2010         unsigned long a0,a1;
2011         int err;
2012
2013         if(call<1||call>SYS_RECVMSG)
2014                 return -EINVAL;
2015
2016         /* copy_from_user should be SMP safe. */
2017         if (copy_from_user(a, args, nargs[call]))
2018                 return -EFAULT;
2019
2020         err = audit_socketcall(nargs[call]/sizeof(unsigned long), a);
2021         if (err)
2022                 return err;
2023
2024         a0=a[0];
2025         a1=a[1];
2026         
2027         switch(call) 
2028         {
2029                 case SYS_SOCKET:
2030                         err = sys_socket(a0,a1,a[2]);
2031                         break;
2032                 case SYS_BIND:
2033                         err = sys_bind(a0,(struct sockaddr __user *)a1, a[2]);
2034                         break;
2035                 case SYS_CONNECT:
2036                         err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
2037                         break;
2038                 case SYS_LISTEN:
2039                         err = sys_listen(a0,a1);
2040                         break;
2041                 case SYS_ACCEPT:
2042                         err = sys_accept(a0,(struct sockaddr __user *)a1, (int __user *)a[2]);
2043                         break;
2044                 case SYS_GETSOCKNAME:
2045                         err = sys_getsockname(a0,(struct sockaddr __user *)a1, (int __user *)a[2]);
2046                         break;
2047                 case SYS_GETPEERNAME:
2048                         err = sys_getpeername(a0, (struct sockaddr __user *)a1, (int __user *)a[2]);
2049                         break;
2050                 case SYS_SOCKETPAIR:
2051                         err = sys_socketpair(a0,a1, a[2], (int __user *)a[3]);
2052                         break;
2053                 case SYS_SEND:
2054                         err = sys_send(a0, (void __user *)a1, a[2], a[3]);
2055                         break;
2056                 case SYS_SENDTO:
2057                         err = sys_sendto(a0,(void __user *)a1, a[2], a[3],
2058                                          (struct sockaddr __user *)a[4], a[5]);
2059                         break;
2060                 case SYS_RECV:
2061                         err = sys_recv(a0, (void __user *)a1, a[2], a[3]);
2062                         break;
2063                 case SYS_RECVFROM:
2064                         err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
2065                                            (struct sockaddr __user *)a[4], (int __user *)a[5]);
2066                         break;
2067                 case SYS_SHUTDOWN:
2068                         err = sys_shutdown(a0,a1);
2069                         break;
2070                 case SYS_SETSOCKOPT:
2071                         err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]);
2072                         break;
2073                 case SYS_GETSOCKOPT:
2074                         err = sys_getsockopt(a0, a1, a[2], (char __user *)a[3], (int __user *)a[4]);
2075                         break;
2076                 case SYS_SENDMSG:
2077                         err = sys_sendmsg(a0, (struct msghdr __user *) a1, a[2]);
2078                         break;
2079                 case SYS_RECVMSG:
2080                         err = sys_recvmsg(a0, (struct msghdr __user *) a1, a[2]);
2081                         break;
2082                 default:
2083                         err = -EINVAL;
2084                         break;
2085         }
2086         return err;
2087 }
2088
2089 #endif /* __ARCH_WANT_SYS_SOCKETCALL */
2090
2091 /*
2092  *      This function is called by a protocol handler that wants to
2093  *      advertise its address family, and have it linked into the
2094  *      SOCKET module.
2095  */
2096
2097 int sock_register(struct net_proto_family *ops)
2098 {
2099         int err;
2100
2101         if (ops->family >= NPROTO) {
2102                 printk(KERN_CRIT "protocol %d >= NPROTO(%d)\n", ops->family, NPROTO);
2103                 return -ENOBUFS;
2104         }
2105         net_family_write_lock();
2106         err = -EEXIST;
2107         if (net_families[ops->family] == NULL) {
2108                 net_families[ops->family]=ops;
2109                 err = 0;
2110         }
2111         net_family_write_unlock();
2112         printk(KERN_INFO "NET: Registered protocol family %d\n",
2113                ops->family);
2114         return err;
2115 }
2116
2117 /*
2118  *      This function is called by a protocol handler that wants to
2119  *      remove its address family, and have it unlinked from the
2120  *      SOCKET module.
2121  */
2122
2123 int sock_unregister(int family)
2124 {
2125         if (family < 0 || family >= NPROTO)
2126                 return -1;
2127
2128         net_family_write_lock();
2129         net_families[family]=NULL;
2130         net_family_write_unlock();
2131         printk(KERN_INFO "NET: Unregistered protocol family %d\n",
2132                family);
2133         return 0;
2134 }
2135
2136 static int __init sock_init(void)
2137 {
2138         /*
2139          *      Initialize sock SLAB cache.
2140          */
2141          
2142         sk_init();
2143
2144         /*
2145          *      Initialize skbuff SLAB cache 
2146          */
2147         skb_init();
2148
2149         /*
2150          *      Initialize the protocols module. 
2151          */
2152
2153         init_inodecache();
2154         register_filesystem(&sock_fs_type);
2155         sock_mnt = kern_mount(&sock_fs_type);
2156
2157         /* The real protocol initialization is performed in later initcalls.
2158          */
2159
2160 #ifdef CONFIG_NETFILTER
2161         netfilter_init();
2162 #endif
2163
2164         return 0;
2165 }
2166
2167 core_initcall(sock_init);       /* early initcall */
2168
2169 int tux_Dprintk;
2170 int tux_TDprintk;
2171
2172 struct module *tux_module = NULL;
2173
2174 #ifdef CONFIG_TUX_MODULE
2175
2176 asmlinkage long (*sys_tux_ptr) (unsigned int action, user_req_t *u_info) = NULL;
2177 spinlock_t tux_module_lock = SPIN_LOCK_UNLOCKED;
2178
2179 asmlinkage long sys_tux (unsigned int action, user_req_t *u_info)
2180 {
2181         int ret;
2182
2183         if (current->tux_info)
2184                 return sys_tux_ptr(action, u_info);
2185
2186         ret = -ENOSYS;
2187         spin_lock(&tux_module_lock);
2188         if (!tux_module)
2189                 goto out_unlock;
2190         if (!try_module_get(tux_module))
2191                 goto out_unlock;
2192         spin_unlock(&tux_module_lock);
2193
2194         if (!sys_tux_ptr)
2195                 TUX_BUG();
2196         ret = sys_tux_ptr(action, u_info);
2197
2198         spin_lock(&tux_module_lock);
2199         module_put(tux_module);
2200 out_unlock:
2201         spin_unlock(&tux_module_lock);
2202
2203         return ret;
2204 }
2205
2206 EXPORT_SYMBOL_GPL(tux_module);
2207 EXPORT_SYMBOL_GPL(tux_module_lock);
2208 EXPORT_SYMBOL_GPL(sys_tux_ptr);
2209
2210 EXPORT_SYMBOL_GPL(tux_Dprintk);
2211 EXPORT_SYMBOL_GPL(tux_TDprintk);
2212
2213 #endif
2214 #ifdef CONFIG_PROC_FS
2215 void socket_seq_show(struct seq_file *seq)
2216 {
2217         int cpu;
2218         int counter = 0;
2219
2220         for_each_possible_cpu(cpu)
2221                 counter += per_cpu(sockets_in_use, cpu);
2222
2223         /* It can be negative, by the way. 8) */
2224         if (counter < 0)
2225                 counter = 0;
2226
2227         seq_printf(seq, "sockets: used %d\n", counter);
2228 }
2229 #endif /* CONFIG_PROC_FS */
2230
2231 #ifdef CONFIG_COMPAT
2232 static long compat_sock_ioctl(struct file *file, unsigned cmd,
2233                                 unsigned long arg)
2234 {
2235         struct socket *sock = file->private_data;
2236         int ret = -ENOIOCTLCMD;
2237
2238         if (sock->ops->compat_ioctl)
2239                 ret = sock->ops->compat_ioctl(sock, cmd, arg);
2240
2241         return ret;
2242 }
2243 #endif
2244
2245 /* ABI emulation layers need these two */
2246 EXPORT_SYMBOL(move_addr_to_kernel);
2247 EXPORT_SYMBOL(move_addr_to_user);
2248 EXPORT_SYMBOL(sock_create);
2249 EXPORT_SYMBOL(sock_create_kern);
2250 EXPORT_SYMBOL(sock_create_lite);
2251 EXPORT_SYMBOL(sock_map_fd);
2252 EXPORT_SYMBOL(sock_recvmsg);
2253 EXPORT_SYMBOL(sock_register);
2254 EXPORT_SYMBOL(sock_release);
2255 EXPORT_SYMBOL(sock_sendmsg);
2256 EXPORT_SYMBOL(sock_unregister);
2257 EXPORT_SYMBOL(sock_wake_async);
2258 EXPORT_SYMBOL(sockfd_lookup);
2259 EXPORT_SYMBOL(kernel_sendmsg);
2260 EXPORT_SYMBOL(kernel_recvmsg);