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