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