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