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