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