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