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