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