4e948dc6d35339f57a753d6ed9561f16c09d9887
[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, void __user *arg) = NULL;
745
746 void brioctl_set(int (*hook)(unsigned int, void __user *))
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)(void __user *arg);
756
757 void vlan_ioctl_set(int (*hook)(void __user *))
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 __user *);
767
768 void dlci_ioctl_set(int (*hook)(unsigned int, void __user *))
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         void __user *argp = (void __user *)arg;
786         int pid, err;
787
788         unlock_kernel();
789         sock = SOCKET_I(inode);
790         if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
791                 err = dev_ioctl(cmd, argp);
792         } else
793 #ifdef WIRELESS_EXT
794         if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
795                 err = dev_ioctl(cmd, argp);
796         } else
797 #endif  /* WIRELESS_EXT */
798         switch (cmd) {
799                 case FIOSETOWN:
800                 case SIOCSPGRP:
801                         err = -EFAULT;
802                         if (get_user(pid, (int __user *)argp))
803                                 break;
804                         err = f_setown(sock->file, pid, 1);
805                         break;
806                 case FIOGETOWN:
807                 case SIOCGPGRP:
808                         err = put_user(sock->file->f_owner.pid, (int __user *)argp);
809                         break;
810                 case SIOCGIFBR:
811                 case SIOCSIFBR:
812                 case SIOCBRADDBR:
813                 case SIOCBRDELBR:
814                         err = -ENOPKG;
815                         if (!br_ioctl_hook)
816                                 request_module("bridge");
817
818                         down(&br_ioctl_mutex);
819                         if (br_ioctl_hook) 
820                                 err = br_ioctl_hook(cmd, argp);
821                         up(&br_ioctl_mutex);
822                         break;
823                 case SIOCGIFVLAN:
824                 case SIOCSIFVLAN:
825                         err = -ENOPKG;
826                         if (!vlan_ioctl_hook)
827                                 request_module("8021q");
828
829                         down(&vlan_ioctl_mutex);
830                         if (vlan_ioctl_hook)
831                                 err = vlan_ioctl_hook(argp);
832                         up(&vlan_ioctl_mutex);
833                         break;
834                 case SIOCGIFDIVERT:
835                 case SIOCSIFDIVERT:
836                 /* Convert this to call through a hook */
837                         err = divert_ioctl(cmd, argp);
838                         break;
839                 case SIOCADDDLCI:
840                 case SIOCDELDLCI:
841                         err = -ENOPKG;
842                         if (!dlci_ioctl_hook)
843                                 request_module("dlci");
844
845                         if (dlci_ioctl_hook) {
846                                 down(&dlci_ioctl_mutex);
847                                 err = dlci_ioctl_hook(cmd, argp);
848                                 up(&dlci_ioctl_mutex);
849                         }
850                         break;
851                 default:
852                         err = sock->ops->ioctl(sock, cmd, arg);
853                         break;
854         }
855         lock_kernel();
856
857         return err;
858 }
859
860 int sock_create_lite(int family, int type, int protocol, struct socket **res)
861 {
862         int err;
863         struct socket *sock = NULL;
864         
865         err = security_socket_create(family, type, protocol, 1);
866         if (err)
867                 goto out;
868
869         sock = sock_alloc();
870         if (!sock) {
871                 err = -ENOMEM;
872                 goto out;
873         }
874
875         security_socket_post_create(sock, family, type, protocol, 1);
876         sock->type = type;
877 out:
878         *res = sock;
879         return err;
880 }
881
882 /* No kernel lock held - perfect */
883 static unsigned int sock_poll(struct file *file, poll_table * wait)
884 {
885         struct socket *sock;
886
887         /*
888          *      We can't return errors to poll, so it's either yes or no. 
889          */
890         sock = SOCKET_I(file->f_dentry->d_inode);
891         return sock->ops->poll(file, sock, wait);
892 }
893
894 static int sock_mmap(struct file * file, struct vm_area_struct * vma)
895 {
896         struct socket *sock = SOCKET_I(file->f_dentry->d_inode);
897
898         return sock->ops->mmap(file, sock, vma);
899 }
900
901 int sock_close(struct inode *inode, struct file *filp)
902 {
903         /*
904          *      It was possible the inode is NULL we were 
905          *      closing an unfinished socket. 
906          */
907
908         if (!inode)
909         {
910                 printk(KERN_DEBUG "sock_close: NULL inode\n");
911                 return 0;
912         }
913         sock_fasync(-1, filp, 0);
914         sock_release(SOCKET_I(inode));
915         return 0;
916 }
917
918 /*
919  *      Update the socket async list
920  *
921  *      Fasync_list locking strategy.
922  *
923  *      1. fasync_list is modified only under process context socket lock
924  *         i.e. under semaphore.
925  *      2. fasync_list is used under read_lock(&sk->sk_callback_lock)
926  *         or under socket lock.
927  *      3. fasync_list can be used from softirq context, so that
928  *         modification under socket lock have to be enhanced with
929  *         write_lock_bh(&sk->sk_callback_lock).
930  *                                                      --ANK (990710)
931  */
932
933 static int sock_fasync(int fd, struct file *filp, int on)
934 {
935         struct fasync_struct *fa, *fna=NULL, **prev;
936         struct socket *sock;
937         struct sock *sk;
938
939         if (on)
940         {
941                 fna=(struct fasync_struct *)kmalloc(sizeof(struct fasync_struct), GFP_KERNEL);
942                 if(fna==NULL)
943                         return -ENOMEM;
944         }
945
946         sock = SOCKET_I(filp->f_dentry->d_inode);
947
948         if ((sk=sock->sk) == NULL) {
949                 if (fna)
950                         kfree(fna);
951                 return -EINVAL;
952         }
953
954         lock_sock(sk);
955
956         prev=&(sock->fasync_list);
957
958         for (fa=*prev; fa!=NULL; prev=&fa->fa_next,fa=*prev)
959                 if (fa->fa_file==filp)
960                         break;
961
962         if(on)
963         {
964                 if(fa!=NULL)
965                 {
966                         write_lock_bh(&sk->sk_callback_lock);
967                         fa->fa_fd=fd;
968                         write_unlock_bh(&sk->sk_callback_lock);
969
970                         kfree(fna);
971                         goto out;
972                 }
973                 fna->fa_file=filp;
974                 fna->fa_fd=fd;
975                 fna->magic=FASYNC_MAGIC;
976                 fna->fa_next=sock->fasync_list;
977                 write_lock_bh(&sk->sk_callback_lock);
978                 sock->fasync_list=fna;
979                 write_unlock_bh(&sk->sk_callback_lock);
980         }
981         else
982         {
983                 if (fa!=NULL)
984                 {
985                         write_lock_bh(&sk->sk_callback_lock);
986                         *prev=fa->fa_next;
987                         write_unlock_bh(&sk->sk_callback_lock);
988                         kfree(fa);
989                 }
990         }
991
992 out:
993         if (sock->sk != sk)
994                 BUG();
995         release_sock(sock->sk);
996         return 0;
997 }
998
999 /* This function may be called only under socket lock or callback_lock */
1000
1001 int sock_wake_async(struct socket *sock, int how, int band)
1002 {
1003         if (!sock || !sock->fasync_list)
1004                 return -1;
1005         switch (how)
1006         {
1007         case 1:
1008                 
1009                 if (test_bit(SOCK_ASYNC_WAITDATA, &sock->flags))
1010                         break;
1011                 goto call_kill;
1012         case 2:
1013                 if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags))
1014                         break;
1015                 /* fall through */
1016         case 0:
1017         call_kill:
1018                 __kill_fasync(sock->fasync_list, SIGIO, band);
1019                 break;
1020         case 3:
1021                 __kill_fasync(sock->fasync_list, SIGURG, band);
1022         }
1023         return 0;
1024 }
1025
1026 static int __sock_create(int family, int type, int protocol, struct socket **res, int kern)
1027 {
1028         int i;
1029         int err;
1030         struct socket *sock;
1031
1032         /*
1033          *      Check protocol is in range
1034          */
1035         if (family < 0 || family >= NPROTO)
1036                 return -EAFNOSUPPORT;
1037         if (type < 0 || type >= SOCK_MAX)
1038                 return -EINVAL;
1039
1040         /* Compatibility.
1041
1042            This uglymoron is moved from INET layer to here to avoid
1043            deadlock in module load.
1044          */
1045         if (family == PF_INET && type == SOCK_PACKET) {
1046                 static int warned; 
1047                 if (!warned) {
1048                         warned = 1;
1049                         printk(KERN_INFO "%s uses obsolete (PF_INET,SOCK_PACKET)\n", current->comm);
1050                 }
1051                 family = PF_PACKET;
1052         }
1053
1054         err = security_socket_create(family, type, protocol, kern);
1055         if (err)
1056                 return err;
1057                 
1058 #if defined(CONFIG_KMOD)
1059         /* Attempt to load a protocol module if the find failed. 
1060          * 
1061          * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user 
1062          * requested real, full-featured networking support upon configuration.
1063          * Otherwise module support will break!
1064          */
1065         if (net_families[family]==NULL)
1066         {
1067                 request_module("net-pf-%d",family);
1068         }
1069 #endif
1070
1071         net_family_read_lock();
1072         if (net_families[family] == NULL) {
1073                 i = -EAFNOSUPPORT;
1074                 goto out;
1075         }
1076
1077 /*
1078  *      Allocate the socket and allow the family to set things up. if
1079  *      the protocol is 0, the family is instructed to select an appropriate
1080  *      default.
1081  */
1082
1083         if (!(sock = sock_alloc())) 
1084         {
1085                 printk(KERN_WARNING "socket: no more sockets\n");
1086                 i = -ENFILE;            /* Not exactly a match, but its the
1087                                            closest posix thing */
1088                 goto out;
1089         }
1090
1091         sock->type  = type;
1092
1093         /*
1094          * We will call the ->create function, that possibly is in a loadable
1095          * module, so we have to bump that loadable module refcnt first.
1096          */
1097         i = -EAFNOSUPPORT;
1098         if (!try_module_get(net_families[family]->owner))
1099                 goto out_release;
1100
1101         if ((i = net_families[family]->create(sock, protocol)) < 0)
1102                 goto out_module_put;
1103         /*
1104          * Now to bump the refcnt of the [loadable] module that owns this
1105          * socket at sock_release time we decrement its refcnt.
1106          */
1107         if (!try_module_get(sock->ops->owner)) {
1108                 sock->ops = NULL;
1109                 goto out_module_put;
1110         }
1111         /*
1112          * Now that we're done with the ->create function, the [loadable]
1113          * module can have its refcnt decremented
1114          */
1115         module_put(net_families[family]->owner);
1116         *res = sock;
1117         security_socket_post_create(sock, family, type, protocol, kern);
1118
1119 out:
1120         net_family_read_unlock();
1121         return i;
1122 out_module_put:
1123         module_put(net_families[family]->owner);
1124 out_release:
1125         sock_release(sock);
1126         goto out;
1127 }
1128
1129 int sock_create(int family, int type, int protocol, struct socket **res)
1130 {
1131         return __sock_create(family, type, protocol, res, 0);
1132 }
1133
1134 int sock_create_kern(int family, int type, int protocol, struct socket **res)
1135 {
1136         return __sock_create(family, type, protocol, res, 1);
1137 }
1138
1139 asmlinkage long sys_socket(int family, int type, int protocol)
1140 {
1141         int retval;
1142         struct socket *sock;
1143
1144         retval = sock_create(family, type, protocol, &sock);
1145         if (retval < 0)
1146                 goto out;
1147
1148         retval = sock_map_fd(sock);
1149         if (retval < 0)
1150                 goto out_release;
1151
1152 out:
1153         /* It may be already another descriptor 8) Not kernel problem. */
1154         return retval;
1155
1156 out_release:
1157         sock_release(sock);
1158         return retval;
1159 }
1160
1161 /*
1162  *      Create a pair of connected sockets.
1163  */
1164
1165 asmlinkage long sys_socketpair(int family, int type, int protocol, int __user *usockvec)
1166 {
1167         struct socket *sock1, *sock2;
1168         int fd1, fd2, err;
1169
1170         /*
1171          * Obtain the first socket and check if the underlying protocol
1172          * supports the socketpair call.
1173          */
1174
1175         err = sock_create(family, type, protocol, &sock1);
1176         if (err < 0)
1177                 goto out;
1178
1179         err = sock_create(family, type, protocol, &sock2);
1180         if (err < 0)
1181                 goto out_release_1;
1182
1183         err = sock1->ops->socketpair(sock1, sock2);
1184         if (err < 0) 
1185                 goto out_release_both;
1186
1187         fd1 = fd2 = -1;
1188
1189         err = sock_map_fd(sock1);
1190         if (err < 0)
1191                 goto out_release_both;
1192         fd1 = err;
1193
1194         err = sock_map_fd(sock2);
1195         if (err < 0)
1196                 goto out_close_1;
1197         fd2 = err;
1198
1199         /* fd1 and fd2 may be already another descriptors.
1200          * Not kernel problem.
1201          */
1202
1203         err = put_user(fd1, &usockvec[0]); 
1204         if (!err)
1205                 err = put_user(fd2, &usockvec[1]);
1206         if (!err)
1207                 return 0;
1208
1209         sys_close(fd2);
1210         sys_close(fd1);
1211         return err;
1212
1213 out_close_1:
1214         sock_release(sock2);
1215         sys_close(fd1);
1216         return err;
1217
1218 out_release_both:
1219         sock_release(sock2);
1220 out_release_1:
1221         sock_release(sock1);
1222 out:
1223         return err;
1224 }
1225
1226
1227 /*
1228  *      Bind a name to a socket. Nothing much to do here since it's
1229  *      the protocol's responsibility to handle the local address.
1230  *
1231  *      We move the socket address to kernel space before we call
1232  *      the protocol layer (having also checked the address is ok).
1233  */
1234
1235 asmlinkage long sys_bind(int fd, struct sockaddr __user *umyaddr, int addrlen)
1236 {
1237         struct socket *sock;
1238         char address[MAX_SOCK_ADDR];
1239         int err;
1240
1241         if((sock = sockfd_lookup(fd,&err))!=NULL)
1242         {
1243                 if((err=move_addr_to_kernel(umyaddr,addrlen,address))>=0) {
1244                         err = security_socket_bind(sock, (struct sockaddr *)address, addrlen);
1245                         if (err) {
1246                                 sockfd_put(sock);
1247                                 return err;
1248                         }
1249                         err = sock->ops->bind(sock, (struct sockaddr *)address, addrlen);
1250                 }
1251                 sockfd_put(sock);
1252         }                       
1253         return err;
1254 }
1255
1256
1257 /*
1258  *      Perform a listen. Basically, we allow the protocol to do anything
1259  *      necessary for a listen, and if that works, we mark the socket as
1260  *      ready for listening.
1261  */
1262
1263 int sysctl_somaxconn = SOMAXCONN;
1264
1265 asmlinkage long sys_listen(int fd, int backlog)
1266 {
1267         struct socket *sock;
1268         int err;
1269         
1270         if ((sock = sockfd_lookup(fd, &err)) != NULL) {
1271                 if ((unsigned) backlog > sysctl_somaxconn)
1272                         backlog = sysctl_somaxconn;
1273
1274                 err = security_socket_listen(sock, backlog);
1275                 if (err) {
1276                         sockfd_put(sock);
1277                         return err;
1278                 }
1279
1280                 err=sock->ops->listen(sock, backlog);
1281                 sockfd_put(sock);
1282         }
1283         return err;
1284 }
1285
1286
1287 /*
1288  *      For accept, we attempt to create a new socket, set up the link
1289  *      with the client, wake up the client, then return the new
1290  *      connected fd. We collect the address of the connector in kernel
1291  *      space and move it to user at the very end. This is unclean because
1292  *      we open the socket then return an error.
1293  *
1294  *      1003.1g adds the ability to recvmsg() to query connection pending
1295  *      status to recvmsg. We need to add that support in a way thats
1296  *      clean when we restucture accept also.
1297  */
1298
1299 asmlinkage long sys_accept(int fd, struct sockaddr __user *upeer_sockaddr, int __user *upeer_addrlen)
1300 {
1301         struct socket *sock, *newsock;
1302         int err, len;
1303         char address[MAX_SOCK_ADDR];
1304
1305         sock = sockfd_lookup(fd, &err);
1306         if (!sock)
1307                 goto out;
1308
1309         err = -EMFILE;
1310         if (!(newsock = sock_alloc())) 
1311                 goto out_put;
1312
1313         newsock->type = sock->type;
1314         newsock->ops = sock->ops;
1315
1316         err = security_socket_accept(sock, newsock);
1317         if (err)
1318                 goto out_release;
1319
1320         /*
1321          * We don't need try_module_get here, as the listening socket (sock)
1322          * has the protocol module (sock->ops->owner) held.
1323          */
1324         __module_get(newsock->ops->owner);
1325
1326         err = sock->ops->accept(sock, newsock, sock->file->f_flags);
1327         if (err < 0)
1328                 goto out_release;
1329
1330         if (upeer_sockaddr) {
1331                 if(newsock->ops->getname(newsock, (struct sockaddr *)address, &len, 2)<0) {
1332                         err = -ECONNABORTED;
1333                         goto out_release;
1334                 }
1335                 err = move_addr_to_user(address, len, upeer_sockaddr, upeer_addrlen);
1336                 if (err < 0)
1337                         goto out_release;
1338         }
1339
1340         /* File flags are not inherited via accept() unlike another OSes. */
1341
1342         if ((err = sock_map_fd(newsock)) < 0)
1343                 goto out_release;
1344
1345         security_socket_post_accept(sock, newsock);
1346
1347 out_put:
1348         sockfd_put(sock);
1349 out:
1350         return err;
1351 out_release:
1352         sock_release(newsock);
1353         goto out_put;
1354 }
1355
1356
1357 /*
1358  *      Attempt to connect to a socket with the server address.  The address
1359  *      is in user space so we verify it is OK and move it to kernel space.
1360  *
1361  *      For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
1362  *      break bindings
1363  *
1364  *      NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
1365  *      other SEQPACKET protocols that take time to connect() as it doesn't
1366  *      include the -EINPROGRESS status for such sockets.
1367  */
1368
1369 asmlinkage long sys_connect(int fd, struct sockaddr __user *uservaddr, int addrlen)
1370 {
1371         struct socket *sock;
1372         char address[MAX_SOCK_ADDR];
1373         int err;
1374
1375         sock = sockfd_lookup(fd, &err);
1376         if (!sock)
1377                 goto out;
1378         err = move_addr_to_kernel(uservaddr, addrlen, address);
1379         if (err < 0)
1380                 goto out_put;
1381
1382         err = security_socket_connect(sock, (struct sockaddr *)address, addrlen);
1383         if (err)
1384                 goto out_put;
1385
1386         err = sock->ops->connect(sock, (struct sockaddr *) address, addrlen,
1387                                  sock->file->f_flags);
1388 out_put:
1389         sockfd_put(sock);
1390 out:
1391         return err;
1392 }
1393
1394 /*
1395  *      Get the local address ('name') of a socket object. Move the obtained
1396  *      name to user space.
1397  */
1398
1399 asmlinkage long sys_getsockname(int fd, struct sockaddr __user *usockaddr, int __user *usockaddr_len)
1400 {
1401         struct socket *sock;
1402         char address[MAX_SOCK_ADDR];
1403         int len, err;
1404         
1405         sock = sockfd_lookup(fd, &err);
1406         if (!sock)
1407                 goto out;
1408
1409         err = security_socket_getsockname(sock);
1410         if (err)
1411                 goto out_put;
1412
1413         err = sock->ops->getname(sock, (struct sockaddr *)address, &len, 0);
1414         if (err)
1415                 goto out_put;
1416         err = move_addr_to_user(address, len, usockaddr, usockaddr_len);
1417
1418 out_put:
1419         sockfd_put(sock);
1420 out:
1421         return err;
1422 }
1423
1424 /*
1425  *      Get the remote address ('name') of a socket object. Move the obtained
1426  *      name to user space.
1427  */
1428
1429 asmlinkage long sys_getpeername(int fd, struct sockaddr __user *usockaddr, int __user *usockaddr_len)
1430 {
1431         struct socket *sock;
1432         char address[MAX_SOCK_ADDR];
1433         int len, err;
1434
1435         if ((sock = sockfd_lookup(fd, &err))!=NULL)
1436         {
1437                 err = security_socket_getpeername(sock);
1438                 if (err) {
1439                         sockfd_put(sock);
1440                         return err;
1441                 }
1442
1443                 err = sock->ops->getname(sock, (struct sockaddr *)address, &len, 1);
1444                 if (!err)
1445                         err=move_addr_to_user(address,len, usockaddr, usockaddr_len);
1446                 sockfd_put(sock);
1447         }
1448         return err;
1449 }
1450
1451 /*
1452  *      Send a datagram to a given address. We move the address into kernel
1453  *      space and check the user space data area is readable before invoking
1454  *      the protocol.
1455  */
1456
1457 asmlinkage long sys_sendto(int fd, void __user * buff, size_t len, unsigned flags,
1458                            struct sockaddr __user *addr, int addr_len)
1459 {
1460         struct socket *sock;
1461         char address[MAX_SOCK_ADDR];
1462         int err;
1463         struct msghdr msg;
1464         struct iovec iov;
1465         
1466         sock = sockfd_lookup(fd, &err);
1467         if (!sock)
1468                 goto out;
1469         iov.iov_base=buff;
1470         iov.iov_len=len;
1471         msg.msg_name=NULL;
1472         msg.msg_iov=&iov;
1473         msg.msg_iovlen=1;
1474         msg.msg_control=NULL;
1475         msg.msg_controllen=0;
1476         msg.msg_namelen=0;
1477         if(addr)
1478         {
1479                 err = move_addr_to_kernel(addr, addr_len, address);
1480                 if (err < 0)
1481                         goto out_put;
1482                 msg.msg_name=address;
1483                 msg.msg_namelen=addr_len;
1484         }
1485         if (sock->file->f_flags & O_NONBLOCK)
1486                 flags |= MSG_DONTWAIT;
1487         msg.msg_flags = flags;
1488         err = sock_sendmsg(sock, &msg, len);
1489
1490 out_put:                
1491         sockfd_put(sock);
1492 out:
1493         return err;
1494 }
1495
1496 /*
1497  *      Send a datagram down a socket. 
1498  */
1499
1500 asmlinkage long sys_send(int fd, void __user * buff, size_t len, unsigned flags)
1501 {
1502         return sys_sendto(fd, buff, len, flags, NULL, 0);
1503 }
1504
1505 /*
1506  *      Receive a frame from the socket and optionally record the address of the 
1507  *      sender. We verify the buffers are writable and if needed move the
1508  *      sender address from kernel to user space.
1509  */
1510
1511 asmlinkage long sys_recvfrom(int fd, void __user * ubuf, size_t size, unsigned flags,
1512                              struct sockaddr __user *addr, int __user *addr_len)
1513 {
1514         struct socket *sock;
1515         struct iovec iov;
1516         struct msghdr msg;
1517         char address[MAX_SOCK_ADDR];
1518         int err,err2;
1519
1520         sock = sockfd_lookup(fd, &err);
1521         if (!sock)
1522                 goto out;
1523
1524         msg.msg_control=NULL;
1525         msg.msg_controllen=0;
1526         msg.msg_iovlen=1;
1527         msg.msg_iov=&iov;
1528         iov.iov_len=size;
1529         iov.iov_base=ubuf;
1530         msg.msg_name=address;
1531         msg.msg_namelen=MAX_SOCK_ADDR;
1532         if (sock->file->f_flags & O_NONBLOCK)
1533                 flags |= MSG_DONTWAIT;
1534         err=sock_recvmsg(sock, &msg, size, flags);
1535
1536         if(err >= 0 && addr != NULL)
1537         {
1538                 err2=move_addr_to_user(address, msg.msg_namelen, addr, addr_len);
1539                 if(err2<0)
1540                         err=err2;
1541         }
1542         sockfd_put(sock);                       
1543 out:
1544         return err;
1545 }
1546
1547 /*
1548  *      Receive a datagram from a socket. 
1549  */
1550
1551 asmlinkage long sys_recv(int fd, void __user * ubuf, size_t size, unsigned flags)
1552 {
1553         return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
1554 }
1555
1556 /*
1557  *      Set a socket option. Because we don't know the option lengths we have
1558  *      to pass the user mode parameter for the protocols to sort out.
1559  */
1560
1561 asmlinkage long sys_setsockopt(int fd, int level, int optname, char __user *optval, int optlen)
1562 {
1563         int err;
1564         struct socket *sock;
1565
1566         if (optlen < 0)
1567                 return -EINVAL;
1568                         
1569         if ((sock = sockfd_lookup(fd, &err))!=NULL)
1570         {
1571                 err = security_socket_setsockopt(sock,level,optname);
1572                 if (err) {
1573                         sockfd_put(sock);
1574                         return err;
1575                 }
1576
1577                 if (level == SOL_SOCKET)
1578                         err=sock_setsockopt(sock,level,optname,optval,optlen);
1579                 else
1580                         err=sock->ops->setsockopt(sock, level, optname, optval, optlen);
1581                 sockfd_put(sock);
1582         }
1583         return err;
1584 }
1585
1586 /*
1587  *      Get a socket option. Because we don't know the option lengths we have
1588  *      to pass a user mode parameter for the protocols to sort out.
1589  */
1590
1591 asmlinkage long sys_getsockopt(int fd, int level, int optname, char __user *optval, int __user *optlen)
1592 {
1593         int err;
1594         struct socket *sock;
1595
1596         if ((sock = sockfd_lookup(fd, &err))!=NULL)
1597         {
1598                 err = security_socket_getsockopt(sock, level, 
1599                                                            optname);
1600                 if (err) {
1601                         sockfd_put(sock);
1602                         return err;
1603                 }
1604
1605                 if (level == SOL_SOCKET)
1606                         err=sock_getsockopt(sock,level,optname,optval,optlen);
1607                 else
1608                         err=sock->ops->getsockopt(sock, level, optname, optval, optlen);
1609                 sockfd_put(sock);
1610         }
1611         return err;
1612 }
1613
1614
1615 /*
1616  *      Shutdown a socket.
1617  */
1618
1619 asmlinkage long sys_shutdown(int fd, int how)
1620 {
1621         int err;
1622         struct socket *sock;
1623
1624         if ((sock = sockfd_lookup(fd, &err))!=NULL)
1625         {
1626                 err = security_socket_shutdown(sock, how);
1627                 if (err) {
1628                         sockfd_put(sock);
1629                         return err;
1630                 }
1631                                 
1632                 err=sock->ops->shutdown(sock, how);
1633                 sockfd_put(sock);
1634         }
1635         return err;
1636 }
1637
1638 /* A couple of helpful macros for getting the address of the 32/64 bit 
1639  * fields which are the same type (int / unsigned) on our platforms.
1640  */
1641 #define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
1642 #define COMPAT_NAMELEN(msg)     COMPAT_MSG(msg, msg_namelen)
1643 #define COMPAT_FLAGS(msg)       COMPAT_MSG(msg, msg_flags)
1644
1645
1646 /*
1647  *      BSD sendmsg interface
1648  */
1649
1650 asmlinkage long sys_sendmsg(int fd, struct msghdr __user *msg, unsigned flags)
1651 {
1652         struct compat_msghdr __user *msg_compat = (struct compat_msghdr __user *)msg;
1653         struct socket *sock;
1654         char address[MAX_SOCK_ADDR];
1655         struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
1656         unsigned char ctl[sizeof(struct cmsghdr) + 20]; /* 20 is size of ipv6_pktinfo */
1657         unsigned char *ctl_buf = ctl;
1658         struct msghdr msg_sys;
1659         int err, ctl_len, iov_size, total_len;
1660         
1661         err = -EFAULT;
1662         if (MSG_CMSG_COMPAT & flags) {
1663                 if (get_compat_msghdr(&msg_sys, msg_compat))
1664                         return -EFAULT;
1665         } else if (copy_from_user(&msg_sys, msg, sizeof(struct msghdr)))
1666                 return -EFAULT;
1667
1668         sock = sockfd_lookup(fd, &err);
1669         if (!sock) 
1670                 goto out;
1671
1672         /* do not move before msg_sys is valid */
1673         err = -EMSGSIZE;
1674         if (msg_sys.msg_iovlen > UIO_MAXIOV)
1675                 goto out_put;
1676
1677         /* Check whether to allocate the iovec area*/
1678         err = -ENOMEM;
1679         iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
1680         if (msg_sys.msg_iovlen > UIO_FASTIOV) {
1681                 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
1682                 if (!iov)
1683                         goto out_put;
1684         }
1685
1686         /* This will also move the address data into kernel space */
1687         if (MSG_CMSG_COMPAT & flags) {
1688                 err = verify_compat_iovec(&msg_sys, iov, address, VERIFY_READ);
1689         } else
1690                 err = verify_iovec(&msg_sys, iov, address, VERIFY_READ);
1691         if (err < 0) 
1692                 goto out_freeiov;
1693         total_len = err;
1694
1695         err = -ENOBUFS;
1696
1697         if (msg_sys.msg_controllen > INT_MAX)
1698                 goto out_freeiov;
1699         ctl_len = msg_sys.msg_controllen; 
1700         if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
1701                 err = cmsghdr_from_user_compat_to_kern(&msg_sys, ctl, sizeof(ctl));
1702                 if (err)
1703                         goto out_freeiov;
1704                 ctl_buf = msg_sys.msg_control;
1705         } else if (ctl_len) {
1706                 if (ctl_len > sizeof(ctl))
1707                 {
1708                         ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
1709                         if (ctl_buf == NULL) 
1710                                 goto out_freeiov;
1711                 }
1712                 err = -EFAULT;
1713                 /*
1714                  * Careful! Before this, msg_sys.msg_control contains a user pointer.
1715                  * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
1716                  * checking falls down on this.
1717                  */
1718                 if (copy_from_user(ctl_buf, (void __user *) msg_sys.msg_control, ctl_len))
1719                         goto out_freectl;
1720                 msg_sys.msg_control = ctl_buf;
1721         }
1722         msg_sys.msg_flags = flags;
1723
1724         if (sock->file->f_flags & O_NONBLOCK)
1725                 msg_sys.msg_flags |= MSG_DONTWAIT;
1726         err = sock_sendmsg(sock, &msg_sys, total_len);
1727
1728 out_freectl:
1729         if (ctl_buf != ctl)    
1730                 sock_kfree_s(sock->sk, ctl_buf, ctl_len);
1731 out_freeiov:
1732         if (iov != iovstack)
1733                 sock_kfree_s(sock->sk, iov, iov_size);
1734 out_put:
1735         sockfd_put(sock);
1736 out:       
1737         return err;
1738 }
1739
1740 /*
1741  *      BSD recvmsg interface
1742  */
1743
1744 asmlinkage long sys_recvmsg(int fd, struct msghdr __user *msg, unsigned int flags)
1745 {
1746         struct compat_msghdr __user *msg_compat = (struct compat_msghdr __user *)msg;
1747         struct socket *sock;
1748         struct iovec iovstack[UIO_FASTIOV];
1749         struct iovec *iov=iovstack;
1750         struct msghdr msg_sys;
1751         unsigned long cmsg_ptr;
1752         int err, iov_size, total_len, len;
1753
1754         /* kernel mode address */
1755         char addr[MAX_SOCK_ADDR];
1756
1757         /* user mode address pointers */
1758         struct sockaddr __user *uaddr;
1759         int __user *uaddr_len;
1760         
1761         if (MSG_CMSG_COMPAT & flags) {
1762                 if (get_compat_msghdr(&msg_sys, msg_compat))
1763                         return -EFAULT;
1764         } else
1765                 if (copy_from_user(&msg_sys,msg,sizeof(struct msghdr)))
1766                         return -EFAULT;
1767
1768         sock = sockfd_lookup(fd, &err);
1769         if (!sock)
1770                 goto out;
1771
1772         err = -EMSGSIZE;
1773         if (msg_sys.msg_iovlen > UIO_MAXIOV)
1774                 goto out_put;
1775         
1776         /* Check whether to allocate the iovec area*/
1777         err = -ENOMEM;
1778         iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
1779         if (msg_sys.msg_iovlen > UIO_FASTIOV) {
1780                 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
1781                 if (!iov)
1782                         goto out_put;
1783         }
1784
1785         /*
1786          *      Save the user-mode address (verify_iovec will change the
1787          *      kernel msghdr to use the kernel address space)
1788          */
1789          
1790         uaddr = (void __user *) msg_sys.msg_name;
1791         uaddr_len = COMPAT_NAMELEN(msg);
1792         if (MSG_CMSG_COMPAT & flags) {
1793                 err = verify_compat_iovec(&msg_sys, iov, addr, VERIFY_WRITE);
1794         } else
1795                 err = verify_iovec(&msg_sys, iov, addr, VERIFY_WRITE);
1796         if (err < 0)
1797                 goto out_freeiov;
1798         total_len=err;
1799
1800         cmsg_ptr = (unsigned long)msg_sys.msg_control;
1801         msg_sys.msg_flags = 0;
1802         if (MSG_CMSG_COMPAT & flags)
1803                 msg_sys.msg_flags = MSG_CMSG_COMPAT;
1804         
1805         if (sock->file->f_flags & O_NONBLOCK)
1806                 flags |= MSG_DONTWAIT;
1807         err = sock_recvmsg(sock, &msg_sys, total_len, flags);
1808         if (err < 0)
1809                 goto out_freeiov;
1810         len = err;
1811
1812         if (uaddr != NULL) {
1813                 err = move_addr_to_user(addr, msg_sys.msg_namelen, uaddr, uaddr_len);
1814                 if (err < 0)
1815                         goto out_freeiov;
1816         }
1817         err = __put_user(msg_sys.msg_flags, COMPAT_FLAGS(msg));
1818         if (err)
1819                 goto out_freeiov;
1820         if (MSG_CMSG_COMPAT & flags)
1821                 err = __put_user((unsigned long)msg_sys.msg_control-cmsg_ptr, 
1822                                  &msg_compat->msg_controllen);
1823         else
1824                 err = __put_user((unsigned long)msg_sys.msg_control-cmsg_ptr, 
1825                                  &msg->msg_controllen);
1826         if (err)
1827                 goto out_freeiov;
1828         err = len;
1829
1830 out_freeiov:
1831         if (iov != iovstack)
1832                 sock_kfree_s(sock->sk, iov, iov_size);
1833 out_put:
1834         sockfd_put(sock);
1835 out:
1836         return err;
1837 }
1838
1839 #ifdef __ARCH_WANT_SYS_SOCKETCALL
1840
1841 /* Argument list sizes for sys_socketcall */
1842 #define AL(x) ((x) * sizeof(unsigned long))
1843 static unsigned char nargs[18]={AL(0),AL(3),AL(3),AL(3),AL(2),AL(3),
1844                                 AL(3),AL(3),AL(4),AL(4),AL(4),AL(6),
1845                                 AL(6),AL(2),AL(5),AL(5),AL(3),AL(3)};
1846 #undef AL
1847
1848 /*
1849  *      System call vectors. 
1850  *
1851  *      Argument checking cleaned up. Saved 20% in size.
1852  *  This function doesn't need to set the kernel lock because
1853  *  it is set by the callees. 
1854  */
1855
1856 asmlinkage long sys_socketcall(int call, unsigned long __user *args)
1857 {
1858         unsigned long a[6];
1859         unsigned long a0,a1;
1860         int err;
1861
1862         if(call<1||call>SYS_RECVMSG)
1863                 return -EINVAL;
1864
1865         /* copy_from_user should be SMP safe. */
1866         if (copy_from_user(a, args, nargs[call]))
1867                 return -EFAULT;
1868                 
1869         a0=a[0];
1870         a1=a[1];
1871         
1872         switch(call) 
1873         {
1874                 case SYS_SOCKET:
1875                         err = sys_socket(a0,a1,a[2]);
1876                         break;
1877                 case SYS_BIND:
1878                         err = sys_bind(a0,(struct sockaddr __user *)a1, a[2]);
1879                         break;
1880                 case SYS_CONNECT:
1881                         err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
1882                         break;
1883                 case SYS_LISTEN:
1884                         err = sys_listen(a0,a1);
1885                         break;
1886                 case SYS_ACCEPT:
1887                         err = sys_accept(a0,(struct sockaddr __user *)a1, (int __user *)a[2]);
1888                         break;
1889                 case SYS_GETSOCKNAME:
1890                         err = sys_getsockname(a0,(struct sockaddr __user *)a1, (int __user *)a[2]);
1891                         break;
1892                 case SYS_GETPEERNAME:
1893                         err = sys_getpeername(a0, (struct sockaddr __user *)a1, (int __user *)a[2]);
1894                         break;
1895                 case SYS_SOCKETPAIR:
1896                         err = sys_socketpair(a0,a1, a[2], (int __user *)a[3]);
1897                         break;
1898                 case SYS_SEND:
1899                         err = sys_send(a0, (void __user *)a1, a[2], a[3]);
1900                         break;
1901                 case SYS_SENDTO:
1902                         err = sys_sendto(a0,(void __user *)a1, a[2], a[3],
1903                                          (struct sockaddr __user *)a[4], a[5]);
1904                         break;
1905                 case SYS_RECV:
1906                         err = sys_recv(a0, (void __user *)a1, a[2], a[3]);
1907                         break;
1908                 case SYS_RECVFROM:
1909                         err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
1910                                            (struct sockaddr __user *)a[4], (int __user *)a[5]);
1911                         break;
1912                 case SYS_SHUTDOWN:
1913                         err = sys_shutdown(a0,a1);
1914                         break;
1915                 case SYS_SETSOCKOPT:
1916                         err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]);
1917                         break;
1918                 case SYS_GETSOCKOPT:
1919                         err = sys_getsockopt(a0, a1, a[2], (char __user *)a[3], (int __user *)a[4]);
1920                         break;
1921                 case SYS_SENDMSG:
1922                         err = sys_sendmsg(a0, (struct msghdr __user *) a1, a[2]);
1923                         break;
1924                 case SYS_RECVMSG:
1925                         err = sys_recvmsg(a0, (struct msghdr __user *) a1, a[2]);
1926                         break;
1927                 default:
1928                         err = -EINVAL;
1929                         break;
1930         }
1931         return err;
1932 }
1933
1934 #endif /* __ARCH_WANT_SYS_SOCKETCALL */
1935
1936 /*
1937  *      This function is called by a protocol handler that wants to
1938  *      advertise its address family, and have it linked into the
1939  *      SOCKET module.
1940  */
1941
1942 int sock_register(struct net_proto_family *ops)
1943 {
1944         int err;
1945
1946         if (ops->family >= NPROTO) {
1947                 printk(KERN_CRIT "protocol %d >= NPROTO(%d)\n", ops->family, NPROTO);
1948                 return -ENOBUFS;
1949         }
1950         net_family_write_lock();
1951         err = -EEXIST;
1952         if (net_families[ops->family] == NULL) {
1953                 net_families[ops->family]=ops;
1954                 err = 0;
1955         }
1956         net_family_write_unlock();
1957         printk(KERN_INFO "NET: Registered protocol family %d\n",
1958                ops->family);
1959         return err;
1960 }
1961
1962 /*
1963  *      This function is called by a protocol handler that wants to
1964  *      remove its address family, and have it unlinked from the
1965  *      SOCKET module.
1966  */
1967
1968 int sock_unregister(int family)
1969 {
1970         if (family < 0 || family >= NPROTO)
1971                 return -1;
1972
1973         net_family_write_lock();
1974         net_families[family]=NULL;
1975         net_family_write_unlock();
1976         printk(KERN_INFO "NET: Unregistered protocol family %d\n",
1977                family);
1978         return 0;
1979 }
1980
1981
1982 extern void sk_init(void);
1983
1984 void __init sock_init(void)
1985 {
1986         int i;
1987
1988         /*
1989          *      Initialize all address (protocol) families. 
1990          */
1991          
1992         for (i = 0; i < NPROTO; i++) 
1993                 net_families[i] = NULL;
1994
1995         /*
1996          *      Initialize sock SLAB cache.
1997          */
1998          
1999         sk_init();
2000
2001 #ifdef SLAB_SKB
2002         /*
2003          *      Initialize skbuff SLAB cache 
2004          */
2005         skb_init();
2006 #endif
2007
2008         /*
2009          *      Initialize the protocols module. 
2010          */
2011
2012         init_inodecache();
2013         register_filesystem(&sock_fs_type);
2014         sock_mnt = kern_mount(&sock_fs_type);
2015         /* The real protocol initialization is performed when
2016          *  do_initcalls is run.  
2017          */
2018
2019 #ifdef CONFIG_NETFILTER
2020         netfilter_init();
2021 #endif
2022 }
2023
2024 int tux_Dprintk;
2025 int tux_TDprintk;
2026
2027 #ifdef CONFIG_TUX_MODULE
2028
2029 asmlinkage long (*sys_tux_ptr) (unsigned int action, user_req_t *u_info) = NULL;
2030
2031 struct module *tux_module = NULL;
2032 spinlock_t tux_module_lock = SPIN_LOCK_UNLOCKED;
2033
2034 asmlinkage long sys_tux (unsigned int action, user_req_t *u_info)
2035 {
2036         int ret;
2037
2038         if (current->tux_info)
2039                 return sys_tux_ptr(action, u_info);
2040
2041         ret = -ENOSYS;
2042         spin_lock(&tux_module_lock);
2043         if (!tux_module)
2044                 goto out_unlock;
2045         if (!try_module_get(tux_module))
2046                 goto out_unlock;
2047         spin_unlock(&tux_module_lock);
2048
2049         if (!sys_tux_ptr)
2050                 TUX_BUG();
2051         ret = sys_tux_ptr(action, u_info);
2052
2053         spin_lock(&tux_module_lock);
2054         module_put(tux_module);
2055 out_unlock:
2056         spin_unlock(&tux_module_lock);
2057
2058         return ret;
2059 }
2060
2061 EXPORT_SYMBOL_GPL(tux_module);
2062 EXPORT_SYMBOL_GPL(tux_module_lock);
2063 EXPORT_SYMBOL_GPL(sys_tux_ptr);
2064
2065 EXPORT_SYMBOL_GPL(tux_Dprintk);
2066 EXPORT_SYMBOL_GPL(tux_TDprintk);
2067
2068 #endif
2069 #ifdef CONFIG_PROC_FS
2070 void socket_seq_show(struct seq_file *seq)
2071 {
2072         int cpu;
2073         int counter = 0;
2074
2075         for (cpu = 0; cpu < NR_CPUS; cpu++)
2076                 counter += per_cpu(sockets_in_use, cpu);
2077
2078         /* It can be negative, by the way. 8) */
2079         if (counter < 0)
2080                 counter = 0;
2081
2082         seq_printf(seq, "sockets: used %d\n", counter);
2083 }
2084 #endif /* CONFIG_PROC_FS */
2085
2086 /* ABI emulation layers need these two */
2087 EXPORT_SYMBOL(move_addr_to_kernel);
2088 EXPORT_SYMBOL(move_addr_to_user);
2089 EXPORT_SYMBOL(sock_alloc);
2090 EXPORT_SYMBOL(sock_alloc_inode);
2091 EXPORT_SYMBOL(sock_create);
2092 EXPORT_SYMBOL(sock_create_kern);
2093 EXPORT_SYMBOL(sock_create_lite);
2094 EXPORT_SYMBOL(sock_map_fd);
2095 EXPORT_SYMBOL(sock_recvmsg);
2096 EXPORT_SYMBOL(sock_register);
2097 EXPORT_SYMBOL(sock_release);
2098 EXPORT_SYMBOL(sock_sendmsg);
2099 EXPORT_SYMBOL(sock_unregister);
2100 EXPORT_SYMBOL(sock_wake_async);
2101 EXPORT_SYMBOL(sockfd_lookup);