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