Merge branch 'master' of master.kernel.org:/pub/scm/linux/kernel/git/davem/net-2.6
[safe/jmp/linux-2.6] / net / socket.c
1 /*
2  * NET          An implementation of the SOCKET network access protocol.
3  *
4  * Version:     @(#)socket.c    1.1.93  18/02/95
5  *
6  * Authors:     Orest Zborowski, <obz@Kodak.COM>
7  *              Ross Biro
8  *              Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
9  *
10  * Fixes:
11  *              Anonymous       :       NOTSOCK/BADF cleanup. Error fix in
12  *                                      shutdown()
13  *              Alan Cox        :       verify_area() fixes
14  *              Alan Cox        :       Removed DDI
15  *              Jonathan Kamens :       SOCK_DGRAM reconnect bug
16  *              Alan Cox        :       Moved a load of checks to the very
17  *                                      top level.
18  *              Alan Cox        :       Move address structures to/from user
19  *                                      mode above the protocol layers.
20  *              Rob Janssen     :       Allow 0 length sends.
21  *              Alan Cox        :       Asynchronous I/O support (cribbed from the
22  *                                      tty drivers).
23  *              Niibe Yutaka    :       Asynchronous I/O for writes (4.4BSD style)
24  *              Jeff Uphoff     :       Made max number of sockets command-line
25  *                                      configurable.
26  *              Matti Aarnio    :       Made the number of sockets dynamic,
27  *                                      to be allocated when needed, and mr.
28  *                                      Uphoff's max is used as max to be
29  *                                      allowed to allocate.
30  *              Linus           :       Argh. removed all the socket allocation
31  *                                      altogether: it's in the inode now.
32  *              Alan Cox        :       Made sock_alloc()/sock_release() public
33  *                                      for NetROM and future kernel nfsd type
34  *                                      stuff.
35  *              Alan Cox        :       sendmsg/recvmsg basics.
36  *              Tom Dyas        :       Export net symbols.
37  *              Marcin Dalecki  :       Fixed problems with CONFIG_NET="n".
38  *              Alan Cox        :       Added thread locking to sys_* calls
39  *                                      for sockets. May have errors at the
40  *                                      moment.
41  *              Kevin Buhr      :       Fixed the dumb errors in the above.
42  *              Andi Kleen      :       Some small cleanups, optimizations,
43  *                                      and fixed a copy_from_user() bug.
44  *              Tigran Aivazian :       sys_send(args) calls sys_sendto(args, NULL, 0)
45  *              Tigran Aivazian :       Made listen(2) backlog sanity checks
46  *                                      protocol-independent
47  *
48  *
49  *              This program is free software; you can redistribute it and/or
50  *              modify it under the terms of the GNU General Public License
51  *              as published by the Free Software Foundation; either version
52  *              2 of the License, or (at your option) any later version.
53  *
54  *
55  *      This module is effectively the top level interface to the BSD socket
56  *      paradigm.
57  *
58  *      Based upon Swansea University Computer Society NET3.039
59  */
60
61 #include <linux/mm.h>
62 #include <linux/socket.h>
63 #include <linux/file.h>
64 #include <linux/net.h>
65 #include <linux/interrupt.h>
66 #include <linux/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 #include <linux/if_tun.h>
101 #include <linux/ipv6_route.h>
102 #include <linux/route.h>
103 #include <linux/sockios.h>
104 #include <linux/atalk.h>
105
106 static int sock_no_open(struct inode *irrelevant, struct file *dontcare);
107 static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov,
108                          unsigned long nr_segs, loff_t pos);
109 static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov,
110                           unsigned long nr_segs, loff_t pos);
111 static int sock_mmap(struct file *file, struct vm_area_struct *vma);
112
113 static int sock_close(struct inode *inode, struct file *file);
114 static unsigned int sock_poll(struct file *file,
115                               struct poll_table_struct *wait);
116 static long sock_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
117 #ifdef CONFIG_COMPAT
118 static long compat_sock_ioctl(struct file *file,
119                               unsigned int cmd, unsigned long arg);
120 #endif
121 static int sock_fasync(int fd, struct file *filp, int on);
122 static ssize_t sock_sendpage(struct file *file, struct page *page,
123                              int offset, size_t size, loff_t *ppos, int more);
124 static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
125                                 struct pipe_inode_info *pipe, size_t len,
126                                 unsigned int flags);
127
128 /*
129  *      Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
130  *      in the operation structures but are done directly via the socketcall() multiplexor.
131  */
132
133 static const struct file_operations socket_file_ops = {
134         .owner =        THIS_MODULE,
135         .llseek =       no_llseek,
136         .aio_read =     sock_aio_read,
137         .aio_write =    sock_aio_write,
138         .poll =         sock_poll,
139         .unlocked_ioctl = sock_ioctl,
140 #ifdef CONFIG_COMPAT
141         .compat_ioctl = compat_sock_ioctl,
142 #endif
143         .mmap =         sock_mmap,
144         .open =         sock_no_open,   /* special open code to disallow open via /proc */
145         .release =      sock_close,
146         .fasync =       sock_fasync,
147         .sendpage =     sock_sendpage,
148         .splice_write = generic_splice_sendpage,
149         .splice_read =  sock_splice_read,
150 };
151
152 /*
153  *      The protocol list. Each protocol is registered in here.
154  */
155
156 static DEFINE_SPINLOCK(net_family_lock);
157 static const struct net_proto_family *net_families[NPROTO] __read_mostly;
158
159 /*
160  *      Statistics counters of the socket lists
161  */
162
163 static DEFINE_PER_CPU(int, sockets_in_use) = 0;
164
165 /*
166  * Support routines.
167  * Move socket addresses back and forth across the kernel/user
168  * divide and look after the messy bits.
169  */
170
171 #define MAX_SOCK_ADDR   128             /* 108 for Unix domain -
172                                            16 for IP, 16 for IPX,
173                                            24 for IPv6,
174                                            about 80 for AX.25
175                                            must be at least one bigger than
176                                            the AF_UNIX size (see net/unix/af_unix.c
177                                            :unix_mkname()).
178                                          */
179
180 /**
181  *      move_addr_to_kernel     -       copy a socket address into kernel space
182  *      @uaddr: Address in user space
183  *      @kaddr: Address in kernel space
184  *      @ulen: Length in user space
185  *
186  *      The address is copied into kernel space. If the provided address is
187  *      too long an error code of -EINVAL is returned. If the copy gives
188  *      invalid addresses -EFAULT is returned. On a success 0 is returned.
189  */
190
191 int move_addr_to_kernel(void __user *uaddr, int ulen, struct sockaddr *kaddr)
192 {
193         if (ulen < 0 || ulen > sizeof(struct sockaddr_storage))
194                 return -EINVAL;
195         if (ulen == 0)
196                 return 0;
197         if (copy_from_user(kaddr, uaddr, ulen))
198                 return -EFAULT;
199         return audit_sockaddr(ulen, kaddr);
200 }
201
202 /**
203  *      move_addr_to_user       -       copy an address to user space
204  *      @kaddr: kernel space address
205  *      @klen: length of address in kernel
206  *      @uaddr: user space address
207  *      @ulen: pointer to user length field
208  *
209  *      The value pointed to by ulen on entry is the buffer length available.
210  *      This is overwritten with the buffer space used. -EINVAL is returned
211  *      if an overlong buffer is specified or a negative buffer size. -EFAULT
212  *      is returned if either the buffer or the length field are not
213  *      accessible.
214  *      After copying the data up to the limit the user specifies, the true
215  *      length of the data is written over the length limit the user
216  *      specified. Zero is returned for a success.
217  */
218
219 int move_addr_to_user(struct sockaddr *kaddr, int klen, void __user *uaddr,
220                       int __user *ulen)
221 {
222         int err;
223         int len;
224
225         err = get_user(len, ulen);
226         if (err)
227                 return err;
228         if (len > klen)
229                 len = klen;
230         if (len < 0 || len > sizeof(struct sockaddr_storage))
231                 return -EINVAL;
232         if (len) {
233                 if (audit_sockaddr(klen, kaddr))
234                         return -ENOMEM;
235                 if (copy_to_user(uaddr, kaddr, len))
236                         return -EFAULT;
237         }
238         /*
239          *      "fromlen shall refer to the value before truncation.."
240          *                      1003.1g
241          */
242         return __put_user(klen, ulen);
243 }
244
245 static struct kmem_cache *sock_inode_cachep __read_mostly;
246
247 static struct inode *sock_alloc_inode(struct super_block *sb)
248 {
249         struct socket_alloc *ei;
250
251         ei = kmem_cache_alloc(sock_inode_cachep, GFP_KERNEL);
252         if (!ei)
253                 return NULL;
254         init_waitqueue_head(&ei->socket.wait);
255
256         ei->socket.fasync_list = NULL;
257         ei->socket.state = SS_UNCONNECTED;
258         ei->socket.flags = 0;
259         ei->socket.ops = NULL;
260         ei->socket.sk = NULL;
261         ei->socket.file = NULL;
262
263         return &ei->vfs_inode;
264 }
265
266 static void sock_destroy_inode(struct inode *inode)
267 {
268         kmem_cache_free(sock_inode_cachep,
269                         container_of(inode, struct socket_alloc, vfs_inode));
270 }
271
272 static void init_once(void *foo)
273 {
274         struct socket_alloc *ei = (struct socket_alloc *)foo;
275
276         inode_init_once(&ei->vfs_inode);
277 }
278
279 static int init_inodecache(void)
280 {
281         sock_inode_cachep = kmem_cache_create("sock_inode_cache",
282                                               sizeof(struct socket_alloc),
283                                               0,
284                                               (SLAB_HWCACHE_ALIGN |
285                                                SLAB_RECLAIM_ACCOUNT |
286                                                SLAB_MEM_SPREAD),
287                                               init_once);
288         if (sock_inode_cachep == NULL)
289                 return -ENOMEM;
290         return 0;
291 }
292
293 static const struct super_operations sockfs_ops = {
294         .alloc_inode =  sock_alloc_inode,
295         .destroy_inode =sock_destroy_inode,
296         .statfs =       simple_statfs,
297 };
298
299 static int sockfs_get_sb(struct file_system_type *fs_type,
300                          int flags, const char *dev_name, void *data,
301                          struct vfsmount *mnt)
302 {
303         return get_sb_pseudo(fs_type, "socket:", &sockfs_ops, SOCKFS_MAGIC,
304                              mnt);
305 }
306
307 static struct vfsmount *sock_mnt __read_mostly;
308
309 static struct file_system_type sock_fs_type = {
310         .name =         "sockfs",
311         .get_sb =       sockfs_get_sb,
312         .kill_sb =      kill_anon_super,
313 };
314
315 /*
316  * sockfs_dname() is called from d_path().
317  */
318 static char *sockfs_dname(struct dentry *dentry, char *buffer, int buflen)
319 {
320         return dynamic_dname(dentry, buffer, buflen, "socket:[%lu]",
321                                 dentry->d_inode->i_ino);
322 }
323
324 static const struct dentry_operations sockfs_dentry_operations = {
325         .d_dname  = sockfs_dname,
326 };
327
328 /*
329  *      Obtains the first available file descriptor and sets it up for use.
330  *
331  *      These functions create file structures and maps them to fd space
332  *      of the current process. On success it returns file descriptor
333  *      and file struct implicitly stored in sock->file.
334  *      Note that another thread may close file descriptor before we return
335  *      from this function. We use the fact that now we do not refer
336  *      to socket after mapping. If one day we will need it, this
337  *      function will increment ref. count on file by 1.
338  *
339  *      In any case returned fd MAY BE not valid!
340  *      This race condition is unavoidable
341  *      with shared fd spaces, we cannot solve it inside kernel,
342  *      but we take care of internal coherence yet.
343  */
344
345 static int sock_alloc_file(struct socket *sock, struct file **f, int flags)
346 {
347         struct qstr name = { .name = "" };
348         struct path path;
349         struct file *file;
350         int fd;
351
352         fd = get_unused_fd_flags(flags);
353         if (unlikely(fd < 0))
354                 return fd;
355
356         path.dentry = d_alloc(sock_mnt->mnt_sb->s_root, &name);
357         if (unlikely(!path.dentry)) {
358                 put_unused_fd(fd);
359                 return -ENOMEM;
360         }
361         path.mnt = mntget(sock_mnt);
362
363         path.dentry->d_op = &sockfs_dentry_operations;
364         d_instantiate(path.dentry, SOCK_INODE(sock));
365         SOCK_INODE(sock)->i_fop = &socket_file_ops;
366
367         file = alloc_file(&path, FMODE_READ | FMODE_WRITE,
368                   &socket_file_ops);
369         if (unlikely(!file)) {
370                 /* drop dentry, keep inode */
371                 atomic_inc(&path.dentry->d_inode->i_count);
372                 path_put(&path);
373                 put_unused_fd(fd);
374                 return -ENFILE;
375         }
376
377         sock->file = file;
378         file->f_flags = O_RDWR | (flags & O_NONBLOCK);
379         file->f_pos = 0;
380         file->private_data = sock;
381
382         *f = file;
383         return fd;
384 }
385
386 int sock_map_fd(struct socket *sock, int flags)
387 {
388         struct file *newfile;
389         int fd = sock_alloc_file(sock, &newfile, flags);
390
391         if (likely(fd >= 0))
392                 fd_install(fd, newfile);
393
394         return fd;
395 }
396
397 static struct socket *sock_from_file(struct file *file, int *err)
398 {
399         if (file->f_op == &socket_file_ops)
400                 return file->private_data;      /* set in sock_map_fd */
401
402         *err = -ENOTSOCK;
403         return NULL;
404 }
405
406 /**
407  *      sockfd_lookup   -       Go from a file number to its socket slot
408  *      @fd: file handle
409  *      @err: pointer to an error code return
410  *
411  *      The file handle passed in is locked and the socket it is bound
412  *      too is returned. If an error occurs the err pointer is overwritten
413  *      with a negative errno code and NULL is returned. The function checks
414  *      for both invalid handles and passing a handle which is not a socket.
415  *
416  *      On a success the socket object pointer is returned.
417  */
418
419 struct socket *sockfd_lookup(int fd, int *err)
420 {
421         struct file *file;
422         struct socket *sock;
423
424         file = fget(fd);
425         if (!file) {
426                 *err = -EBADF;
427                 return NULL;
428         }
429
430         sock = sock_from_file(file, err);
431         if (!sock)
432                 fput(file);
433         return sock;
434 }
435
436 static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed)
437 {
438         struct file *file;
439         struct socket *sock;
440
441         *err = -EBADF;
442         file = fget_light(fd, fput_needed);
443         if (file) {
444                 sock = sock_from_file(file, err);
445                 if (sock)
446                         return sock;
447                 fput_light(file, *fput_needed);
448         }
449         return NULL;
450 }
451
452 /**
453  *      sock_alloc      -       allocate a socket
454  *
455  *      Allocate a new inode and socket object. The two are bound together
456  *      and initialised. The socket is then returned. If we are out of inodes
457  *      NULL is returned.
458  */
459
460 static struct socket *sock_alloc(void)
461 {
462         struct inode *inode;
463         struct socket *sock;
464
465         inode = new_inode(sock_mnt->mnt_sb);
466         if (!inode)
467                 return NULL;
468
469         sock = SOCKET_I(inode);
470
471         kmemcheck_annotate_bitfield(sock, type);
472         inode->i_mode = S_IFSOCK | S_IRWXUGO;
473         inode->i_uid = current_fsuid();
474         inode->i_gid = current_fsgid();
475
476         percpu_add(sockets_in_use, 1);
477         return sock;
478 }
479
480 /*
481  *      In theory you can't get an open on this inode, but /proc provides
482  *      a back door. Remember to keep it shut otherwise you'll let the
483  *      creepy crawlies in.
484  */
485
486 static int sock_no_open(struct inode *irrelevant, struct file *dontcare)
487 {
488         return -ENXIO;
489 }
490
491 const struct file_operations bad_sock_fops = {
492         .owner = THIS_MODULE,
493         .open = sock_no_open,
494 };
495
496 /**
497  *      sock_release    -       close a socket
498  *      @sock: socket to close
499  *
500  *      The socket is released from the protocol stack if it has a release
501  *      callback, and the inode is then released if the socket is bound to
502  *      an inode not a file.
503  */
504
505 void sock_release(struct socket *sock)
506 {
507         if (sock->ops) {
508                 struct module *owner = sock->ops->owner;
509
510                 sock->ops->release(sock);
511                 sock->ops = NULL;
512                 module_put(owner);
513         }
514
515         if (sock->fasync_list)
516                 printk(KERN_ERR "sock_release: fasync list not empty!\n");
517
518         percpu_sub(sockets_in_use, 1);
519         if (!sock->file) {
520                 iput(SOCK_INODE(sock));
521                 return;
522         }
523         sock->file = NULL;
524 }
525
526 int sock_tx_timestamp(struct msghdr *msg, struct sock *sk,
527                       union skb_shared_tx *shtx)
528 {
529         shtx->flags = 0;
530         if (sock_flag(sk, SOCK_TIMESTAMPING_TX_HARDWARE))
531                 shtx->hardware = 1;
532         if (sock_flag(sk, SOCK_TIMESTAMPING_TX_SOFTWARE))
533                 shtx->software = 1;
534         return 0;
535 }
536 EXPORT_SYMBOL(sock_tx_timestamp);
537
538 static inline int __sock_sendmsg(struct kiocb *iocb, struct socket *sock,
539                                  struct msghdr *msg, size_t size)
540 {
541         struct sock_iocb *si = kiocb_to_siocb(iocb);
542         int err;
543
544         si->sock = sock;
545         si->scm = NULL;
546         si->msg = msg;
547         si->size = size;
548
549         err = security_socket_sendmsg(sock, msg, size);
550         if (err)
551                 return err;
552
553         return sock->ops->sendmsg(iocb, sock, msg, size);
554 }
555
556 int sock_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
557 {
558         struct kiocb iocb;
559         struct sock_iocb siocb;
560         int ret;
561
562         init_sync_kiocb(&iocb, NULL);
563         iocb.private = &siocb;
564         ret = __sock_sendmsg(&iocb, sock, msg, size);
565         if (-EIOCBQUEUED == ret)
566                 ret = wait_on_sync_kiocb(&iocb);
567         return ret;
568 }
569
570 int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
571                    struct kvec *vec, size_t num, size_t size)
572 {
573         mm_segment_t oldfs = get_fs();
574         int result;
575
576         set_fs(KERNEL_DS);
577         /*
578          * the following is safe, since for compiler definitions of kvec and
579          * iovec are identical, yielding the same in-core layout and alignment
580          */
581         msg->msg_iov = (struct iovec *)vec;
582         msg->msg_iovlen = num;
583         result = sock_sendmsg(sock, msg, size);
584         set_fs(oldfs);
585         return result;
586 }
587
588 static int ktime2ts(ktime_t kt, struct timespec *ts)
589 {
590         if (kt.tv64) {
591                 *ts = ktime_to_timespec(kt);
592                 return 1;
593         } else {
594                 return 0;
595         }
596 }
597
598 /*
599  * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
600  */
601 void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
602         struct sk_buff *skb)
603 {
604         int need_software_tstamp = sock_flag(sk, SOCK_RCVTSTAMP);
605         struct timespec ts[3];
606         int empty = 1;
607         struct skb_shared_hwtstamps *shhwtstamps =
608                 skb_hwtstamps(skb);
609
610         /* Race occurred between timestamp enabling and packet
611            receiving.  Fill in the current time for now. */
612         if (need_software_tstamp && skb->tstamp.tv64 == 0)
613                 __net_timestamp(skb);
614
615         if (need_software_tstamp) {
616                 if (!sock_flag(sk, SOCK_RCVTSTAMPNS)) {
617                         struct timeval tv;
618                         skb_get_timestamp(skb, &tv);
619                         put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMP,
620                                  sizeof(tv), &tv);
621                 } else {
622                         skb_get_timestampns(skb, &ts[0]);
623                         put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPNS,
624                                  sizeof(ts[0]), &ts[0]);
625                 }
626         }
627
628
629         memset(ts, 0, sizeof(ts));
630         if (skb->tstamp.tv64 &&
631             sock_flag(sk, SOCK_TIMESTAMPING_SOFTWARE)) {
632                 skb_get_timestampns(skb, ts + 0);
633                 empty = 0;
634         }
635         if (shhwtstamps) {
636                 if (sock_flag(sk, SOCK_TIMESTAMPING_SYS_HARDWARE) &&
637                     ktime2ts(shhwtstamps->syststamp, ts + 1))
638                         empty = 0;
639                 if (sock_flag(sk, SOCK_TIMESTAMPING_RAW_HARDWARE) &&
640                     ktime2ts(shhwtstamps->hwtstamp, ts + 2))
641                         empty = 0;
642         }
643         if (!empty)
644                 put_cmsg(msg, SOL_SOCKET,
645                          SCM_TIMESTAMPING, sizeof(ts), &ts);
646 }
647
648 EXPORT_SYMBOL_GPL(__sock_recv_timestamp);
649
650 inline void sock_recv_drops(struct msghdr *msg, struct sock *sk, struct sk_buff *skb)
651 {
652         if (sock_flag(sk, SOCK_RXQ_OVFL) && skb && skb->dropcount)
653                 put_cmsg(msg, SOL_SOCKET, SO_RXQ_OVFL,
654                         sizeof(__u32), &skb->dropcount);
655 }
656
657 void sock_recv_ts_and_drops(struct msghdr *msg, struct sock *sk,
658         struct sk_buff *skb)
659 {
660         sock_recv_timestamp(msg, sk, skb);
661         sock_recv_drops(msg, sk, skb);
662 }
663 EXPORT_SYMBOL_GPL(sock_recv_ts_and_drops);
664
665 static inline int __sock_recvmsg_nosec(struct kiocb *iocb, struct socket *sock,
666                                        struct msghdr *msg, size_t size, int flags)
667 {
668         struct sock_iocb *si = kiocb_to_siocb(iocb);
669
670         si->sock = sock;
671         si->scm = NULL;
672         si->msg = msg;
673         si->size = size;
674         si->flags = flags;
675
676         return sock->ops->recvmsg(iocb, sock, msg, size, flags);
677 }
678
679 static inline int __sock_recvmsg(struct kiocb *iocb, struct socket *sock,
680                                  struct msghdr *msg, size_t size, int flags)
681 {
682         int err = security_socket_recvmsg(sock, msg, size, flags);
683
684         return err ?: __sock_recvmsg_nosec(iocb, sock, msg, size, flags);
685 }
686
687 int sock_recvmsg(struct socket *sock, struct msghdr *msg,
688                  size_t size, int flags)
689 {
690         struct kiocb iocb;
691         struct sock_iocb siocb;
692         int ret;
693
694         init_sync_kiocb(&iocb, NULL);
695         iocb.private = &siocb;
696         ret = __sock_recvmsg(&iocb, sock, msg, size, flags);
697         if (-EIOCBQUEUED == ret)
698                 ret = wait_on_sync_kiocb(&iocb);
699         return ret;
700 }
701
702 static int sock_recvmsg_nosec(struct socket *sock, struct msghdr *msg,
703                               size_t size, int flags)
704 {
705         struct kiocb iocb;
706         struct sock_iocb siocb;
707         int ret;
708
709         init_sync_kiocb(&iocb, NULL);
710         iocb.private = &siocb;
711         ret = __sock_recvmsg_nosec(&iocb, sock, msg, size, flags);
712         if (-EIOCBQUEUED == ret)
713                 ret = wait_on_sync_kiocb(&iocb);
714         return ret;
715 }
716
717 int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
718                    struct kvec *vec, size_t num, size_t size, int flags)
719 {
720         mm_segment_t oldfs = get_fs();
721         int result;
722
723         set_fs(KERNEL_DS);
724         /*
725          * the following is safe, since for compiler definitions of kvec and
726          * iovec are identical, yielding the same in-core layout and alignment
727          */
728         msg->msg_iov = (struct iovec *)vec, msg->msg_iovlen = num;
729         result = sock_recvmsg(sock, msg, size, flags);
730         set_fs(oldfs);
731         return result;
732 }
733
734 static void sock_aio_dtor(struct kiocb *iocb)
735 {
736         kfree(iocb->private);
737 }
738
739 static ssize_t sock_sendpage(struct file *file, struct page *page,
740                              int offset, size_t size, loff_t *ppos, int more)
741 {
742         struct socket *sock;
743         int flags;
744
745         sock = file->private_data;
746
747         flags = !(file->f_flags & O_NONBLOCK) ? 0 : MSG_DONTWAIT;
748         if (more)
749                 flags |= MSG_MORE;
750
751         return kernel_sendpage(sock, page, offset, size, flags);
752 }
753
754 static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
755                                 struct pipe_inode_info *pipe, size_t len,
756                                 unsigned int flags)
757 {
758         struct socket *sock = file->private_data;
759
760         if (unlikely(!sock->ops->splice_read))
761                 return -EINVAL;
762
763         return sock->ops->splice_read(sock, ppos, pipe, len, flags);
764 }
765
766 static struct sock_iocb *alloc_sock_iocb(struct kiocb *iocb,
767                                          struct sock_iocb *siocb)
768 {
769         if (!is_sync_kiocb(iocb)) {
770                 siocb = kmalloc(sizeof(*siocb), GFP_KERNEL);
771                 if (!siocb)
772                         return NULL;
773                 iocb->ki_dtor = sock_aio_dtor;
774         }
775
776         siocb->kiocb = iocb;
777         iocb->private = siocb;
778         return siocb;
779 }
780
781 static ssize_t do_sock_read(struct msghdr *msg, struct kiocb *iocb,
782                 struct file *file, const struct iovec *iov,
783                 unsigned long nr_segs)
784 {
785         struct socket *sock = file->private_data;
786         size_t size = 0;
787         int i;
788
789         for (i = 0; i < nr_segs; i++)
790                 size += iov[i].iov_len;
791
792         msg->msg_name = NULL;
793         msg->msg_namelen = 0;
794         msg->msg_control = NULL;
795         msg->msg_controllen = 0;
796         msg->msg_iov = (struct iovec *)iov;
797         msg->msg_iovlen = nr_segs;
798         msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
799
800         return __sock_recvmsg(iocb, sock, msg, size, msg->msg_flags);
801 }
802
803 static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov,
804                                 unsigned long nr_segs, loff_t pos)
805 {
806         struct sock_iocb siocb, *x;
807
808         if (pos != 0)
809                 return -ESPIPE;
810
811         if (iocb->ki_left == 0) /* Match SYS5 behaviour */
812                 return 0;
813
814
815         x = alloc_sock_iocb(iocb, &siocb);
816         if (!x)
817                 return -ENOMEM;
818         return do_sock_read(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
819 }
820
821 static ssize_t do_sock_write(struct msghdr *msg, struct kiocb *iocb,
822                         struct file *file, const struct iovec *iov,
823                         unsigned long nr_segs)
824 {
825         struct socket *sock = file->private_data;
826         size_t size = 0;
827         int i;
828
829         for (i = 0; i < nr_segs; i++)
830                 size += iov[i].iov_len;
831
832         msg->msg_name = NULL;
833         msg->msg_namelen = 0;
834         msg->msg_control = NULL;
835         msg->msg_controllen = 0;
836         msg->msg_iov = (struct iovec *)iov;
837         msg->msg_iovlen = nr_segs;
838         msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
839         if (sock->type == SOCK_SEQPACKET)
840                 msg->msg_flags |= MSG_EOR;
841
842         return __sock_sendmsg(iocb, sock, msg, size);
843 }
844
845 static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov,
846                           unsigned long nr_segs, loff_t pos)
847 {
848         struct sock_iocb siocb, *x;
849
850         if (pos != 0)
851                 return -ESPIPE;
852
853         x = alloc_sock_iocb(iocb, &siocb);
854         if (!x)
855                 return -ENOMEM;
856
857         return do_sock_write(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
858 }
859
860 /*
861  * Atomic setting of ioctl hooks to avoid race
862  * with module unload.
863  */
864
865 static DEFINE_MUTEX(br_ioctl_mutex);
866 static int (*br_ioctl_hook) (struct net *, unsigned int cmd, void __user *arg) = NULL;
867
868 void brioctl_set(int (*hook) (struct net *, unsigned int, void __user *))
869 {
870         mutex_lock(&br_ioctl_mutex);
871         br_ioctl_hook = hook;
872         mutex_unlock(&br_ioctl_mutex);
873 }
874
875 EXPORT_SYMBOL(brioctl_set);
876
877 static DEFINE_MUTEX(vlan_ioctl_mutex);
878 static int (*vlan_ioctl_hook) (struct net *, void __user *arg);
879
880 void vlan_ioctl_set(int (*hook) (struct net *, void __user *))
881 {
882         mutex_lock(&vlan_ioctl_mutex);
883         vlan_ioctl_hook = hook;
884         mutex_unlock(&vlan_ioctl_mutex);
885 }
886
887 EXPORT_SYMBOL(vlan_ioctl_set);
888
889 static DEFINE_MUTEX(dlci_ioctl_mutex);
890 static int (*dlci_ioctl_hook) (unsigned int, void __user *);
891
892 void dlci_ioctl_set(int (*hook) (unsigned int, void __user *))
893 {
894         mutex_lock(&dlci_ioctl_mutex);
895         dlci_ioctl_hook = hook;
896         mutex_unlock(&dlci_ioctl_mutex);
897 }
898
899 EXPORT_SYMBOL(dlci_ioctl_set);
900
901 static long sock_do_ioctl(struct net *net, struct socket *sock,
902                                  unsigned int cmd, unsigned long arg)
903 {
904         int err;
905         void __user *argp = (void __user *)arg;
906
907         err = sock->ops->ioctl(sock, cmd, arg);
908
909         /*
910          * If this ioctl is unknown try to hand it down
911          * to the NIC driver.
912          */
913         if (err == -ENOIOCTLCMD)
914                 err = dev_ioctl(net, cmd, argp);
915
916         return err;
917 }
918
919 /*
920  *      With an ioctl, arg may well be a user mode pointer, but we don't know
921  *      what to do with it - that's up to the protocol still.
922  */
923
924 static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
925 {
926         struct socket *sock;
927         struct sock *sk;
928         void __user *argp = (void __user *)arg;
929         int pid, err;
930         struct net *net;
931
932         sock = file->private_data;
933         sk = sock->sk;
934         net = sock_net(sk);
935         if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
936                 err = dev_ioctl(net, cmd, argp);
937         } else
938 #ifdef CONFIG_WEXT_CORE
939         if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
940                 err = dev_ioctl(net, cmd, argp);
941         } else
942 #endif
943                 switch (cmd) {
944                 case FIOSETOWN:
945                 case SIOCSPGRP:
946                         err = -EFAULT;
947                         if (get_user(pid, (int __user *)argp))
948                                 break;
949                         err = f_setown(sock->file, pid, 1);
950                         break;
951                 case FIOGETOWN:
952                 case SIOCGPGRP:
953                         err = put_user(f_getown(sock->file),
954                                        (int __user *)argp);
955                         break;
956                 case SIOCGIFBR:
957                 case SIOCSIFBR:
958                 case SIOCBRADDBR:
959                 case SIOCBRDELBR:
960                         err = -ENOPKG;
961                         if (!br_ioctl_hook)
962                                 request_module("bridge");
963
964                         mutex_lock(&br_ioctl_mutex);
965                         if (br_ioctl_hook)
966                                 err = br_ioctl_hook(net, cmd, argp);
967                         mutex_unlock(&br_ioctl_mutex);
968                         break;
969                 case SIOCGIFVLAN:
970                 case SIOCSIFVLAN:
971                         err = -ENOPKG;
972                         if (!vlan_ioctl_hook)
973                                 request_module("8021q");
974
975                         mutex_lock(&vlan_ioctl_mutex);
976                         if (vlan_ioctl_hook)
977                                 err = vlan_ioctl_hook(net, argp);
978                         mutex_unlock(&vlan_ioctl_mutex);
979                         break;
980                 case SIOCADDDLCI:
981                 case SIOCDELDLCI:
982                         err = -ENOPKG;
983                         if (!dlci_ioctl_hook)
984                                 request_module("dlci");
985
986                         mutex_lock(&dlci_ioctl_mutex);
987                         if (dlci_ioctl_hook)
988                                 err = dlci_ioctl_hook(cmd, argp);
989                         mutex_unlock(&dlci_ioctl_mutex);
990                         break;
991                 default:
992                         err = sock_do_ioctl(net, sock, cmd, arg);
993                         break;
994                 }
995         return err;
996 }
997
998 int sock_create_lite(int family, int type, int protocol, struct socket **res)
999 {
1000         int err;
1001         struct socket *sock = NULL;
1002
1003         err = security_socket_create(family, type, protocol, 1);
1004         if (err)
1005                 goto out;
1006
1007         sock = sock_alloc();
1008         if (!sock) {
1009                 err = -ENOMEM;
1010                 goto out;
1011         }
1012
1013         sock->type = type;
1014         err = security_socket_post_create(sock, family, type, protocol, 1);
1015         if (err)
1016                 goto out_release;
1017
1018 out:
1019         *res = sock;
1020         return err;
1021 out_release:
1022         sock_release(sock);
1023         sock = NULL;
1024         goto out;
1025 }
1026
1027 /* No kernel lock held - perfect */
1028 static unsigned int sock_poll(struct file *file, poll_table *wait)
1029 {
1030         struct socket *sock;
1031
1032         /*
1033          *      We can't return errors to poll, so it's either yes or no.
1034          */
1035         sock = file->private_data;
1036         return sock->ops->poll(file, sock, wait);
1037 }
1038
1039 static int sock_mmap(struct file *file, struct vm_area_struct *vma)
1040 {
1041         struct socket *sock = file->private_data;
1042
1043         return sock->ops->mmap(file, sock, vma);
1044 }
1045
1046 static int sock_close(struct inode *inode, struct file *filp)
1047 {
1048         /*
1049          *      It was possible the inode is NULL we were
1050          *      closing an unfinished socket.
1051          */
1052
1053         if (!inode) {
1054                 printk(KERN_DEBUG "sock_close: NULL inode\n");
1055                 return 0;
1056         }
1057         sock_release(SOCKET_I(inode));
1058         return 0;
1059 }
1060
1061 /*
1062  *      Update the socket async list
1063  *
1064  *      Fasync_list locking strategy.
1065  *
1066  *      1. fasync_list is modified only under process context socket lock
1067  *         i.e. under semaphore.
1068  *      2. fasync_list is used under read_lock(&sk->sk_callback_lock)
1069  *         or under socket lock.
1070  *      3. fasync_list can be used from softirq context, so that
1071  *         modification under socket lock have to be enhanced with
1072  *         write_lock_bh(&sk->sk_callback_lock).
1073  *                                                      --ANK (990710)
1074  */
1075
1076 static int sock_fasync(int fd, struct file *filp, int on)
1077 {
1078         struct fasync_struct *fa, *fna = NULL, **prev;
1079         struct socket *sock;
1080         struct sock *sk;
1081
1082         if (on) {
1083                 fna = kmalloc(sizeof(struct fasync_struct), GFP_KERNEL);
1084                 if (fna == NULL)
1085                         return -ENOMEM;
1086         }
1087
1088         sock = filp->private_data;
1089
1090         sk = sock->sk;
1091         if (sk == NULL) {
1092                 kfree(fna);
1093                 return -EINVAL;
1094         }
1095
1096         lock_sock(sk);
1097
1098         spin_lock(&filp->f_lock);
1099         if (on)
1100                 filp->f_flags |= FASYNC;
1101         else
1102                 filp->f_flags &= ~FASYNC;
1103         spin_unlock(&filp->f_lock);
1104
1105         prev = &(sock->fasync_list);
1106
1107         for (fa = *prev; fa != NULL; prev = &fa->fa_next, fa = *prev)
1108                 if (fa->fa_file == filp)
1109                         break;
1110
1111         if (on) {
1112                 if (fa != NULL) {
1113                         write_lock_bh(&sk->sk_callback_lock);
1114                         fa->fa_fd = fd;
1115                         write_unlock_bh(&sk->sk_callback_lock);
1116
1117                         kfree(fna);
1118                         goto out;
1119                 }
1120                 fna->fa_file = filp;
1121                 fna->fa_fd = fd;
1122                 fna->magic = FASYNC_MAGIC;
1123                 fna->fa_next = sock->fasync_list;
1124                 write_lock_bh(&sk->sk_callback_lock);
1125                 sock->fasync_list = fna;
1126                 sock_set_flag(sk, SOCK_FASYNC);
1127                 write_unlock_bh(&sk->sk_callback_lock);
1128         } else {
1129                 if (fa != NULL) {
1130                         write_lock_bh(&sk->sk_callback_lock);
1131                         *prev = fa->fa_next;
1132                         if (!sock->fasync_list)
1133                                 sock_reset_flag(sk, SOCK_FASYNC);
1134                         write_unlock_bh(&sk->sk_callback_lock);
1135                         kfree(fa);
1136                 }
1137         }
1138
1139 out:
1140         release_sock(sock->sk);
1141         return 0;
1142 }
1143
1144 /* This function may be called only under socket lock or callback_lock */
1145
1146 int sock_wake_async(struct socket *sock, int how, int band)
1147 {
1148         if (!sock || !sock->fasync_list)
1149                 return -1;
1150         switch (how) {
1151         case SOCK_WAKE_WAITD:
1152                 if (test_bit(SOCK_ASYNC_WAITDATA, &sock->flags))
1153                         break;
1154                 goto call_kill;
1155         case SOCK_WAKE_SPACE:
1156                 if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags))
1157                         break;
1158                 /* fall through */
1159         case SOCK_WAKE_IO:
1160 call_kill:
1161                 __kill_fasync(sock->fasync_list, SIGIO, band);
1162                 break;
1163         case SOCK_WAKE_URG:
1164                 __kill_fasync(sock->fasync_list, SIGURG, band);
1165         }
1166         return 0;
1167 }
1168
1169 static int __sock_create(struct net *net, int family, int type, int protocol,
1170                          struct socket **res, int kern)
1171 {
1172         int err;
1173         struct socket *sock;
1174         const struct net_proto_family *pf;
1175
1176         /*
1177          *      Check protocol is in range
1178          */
1179         if (family < 0 || family >= NPROTO)
1180                 return -EAFNOSUPPORT;
1181         if (type < 0 || type >= SOCK_MAX)
1182                 return -EINVAL;
1183
1184         /* Compatibility.
1185
1186            This uglymoron is moved from INET layer to here to avoid
1187            deadlock in module load.
1188          */
1189         if (family == PF_INET && type == SOCK_PACKET) {
1190                 static int warned;
1191                 if (!warned) {
1192                         warned = 1;
1193                         printk(KERN_INFO "%s uses obsolete (PF_INET,SOCK_PACKET)\n",
1194                                current->comm);
1195                 }
1196                 family = PF_PACKET;
1197         }
1198
1199         err = security_socket_create(family, type, protocol, kern);
1200         if (err)
1201                 return err;
1202
1203         /*
1204          *      Allocate the socket and allow the family to set things up. if
1205          *      the protocol is 0, the family is instructed to select an appropriate
1206          *      default.
1207          */
1208         sock = sock_alloc();
1209         if (!sock) {
1210                 if (net_ratelimit())
1211                         printk(KERN_WARNING "socket: no more sockets\n");
1212                 return -ENFILE; /* Not exactly a match, but its the
1213                                    closest posix thing */
1214         }
1215
1216         sock->type = type;
1217
1218 #ifdef CONFIG_MODULES
1219         /* Attempt to load a protocol module if the find failed.
1220          *
1221          * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
1222          * requested real, full-featured networking support upon configuration.
1223          * Otherwise module support will break!
1224          */
1225         if (net_families[family] == NULL)
1226                 request_module("net-pf-%d", family);
1227 #endif
1228
1229         rcu_read_lock();
1230         pf = rcu_dereference(net_families[family]);
1231         err = -EAFNOSUPPORT;
1232         if (!pf)
1233                 goto out_release;
1234
1235         /*
1236          * We will call the ->create function, that possibly is in a loadable
1237          * module, so we have to bump that loadable module refcnt first.
1238          */
1239         if (!try_module_get(pf->owner))
1240                 goto out_release;
1241
1242         /* Now protected by module ref count */
1243         rcu_read_unlock();
1244
1245         err = pf->create(net, sock, protocol, kern);
1246         if (err < 0)
1247                 goto out_module_put;
1248
1249         /*
1250          * Now to bump the refcnt of the [loadable] module that owns this
1251          * socket at sock_release time we decrement its refcnt.
1252          */
1253         if (!try_module_get(sock->ops->owner))
1254                 goto out_module_busy;
1255
1256         /*
1257          * Now that we're done with the ->create function, the [loadable]
1258          * module can have its refcnt decremented
1259          */
1260         module_put(pf->owner);
1261         err = security_socket_post_create(sock, family, type, protocol, kern);
1262         if (err)
1263                 goto out_sock_release;
1264         *res = sock;
1265
1266         return 0;
1267
1268 out_module_busy:
1269         err = -EAFNOSUPPORT;
1270 out_module_put:
1271         sock->ops = NULL;
1272         module_put(pf->owner);
1273 out_sock_release:
1274         sock_release(sock);
1275         return err;
1276
1277 out_release:
1278         rcu_read_unlock();
1279         goto out_sock_release;
1280 }
1281
1282 int sock_create(int family, int type, int protocol, struct socket **res)
1283 {
1284         return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0);
1285 }
1286
1287 int sock_create_kern(int family, int type, int protocol, struct socket **res)
1288 {
1289         return __sock_create(&init_net, family, type, protocol, res, 1);
1290 }
1291
1292 SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol)
1293 {
1294         int retval;
1295         struct socket *sock;
1296         int flags;
1297
1298         /* Check the SOCK_* constants for consistency.  */
1299         BUILD_BUG_ON(SOCK_CLOEXEC != O_CLOEXEC);
1300         BUILD_BUG_ON((SOCK_MAX | SOCK_TYPE_MASK) != SOCK_TYPE_MASK);
1301         BUILD_BUG_ON(SOCK_CLOEXEC & SOCK_TYPE_MASK);
1302         BUILD_BUG_ON(SOCK_NONBLOCK & SOCK_TYPE_MASK);
1303
1304         flags = type & ~SOCK_TYPE_MASK;
1305         if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1306                 return -EINVAL;
1307         type &= SOCK_TYPE_MASK;
1308
1309         if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1310                 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1311
1312         retval = sock_create(family, type, protocol, &sock);
1313         if (retval < 0)
1314                 goto out;
1315
1316         retval = sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK));
1317         if (retval < 0)
1318                 goto out_release;
1319
1320 out:
1321         /* It may be already another descriptor 8) Not kernel problem. */
1322         return retval;
1323
1324 out_release:
1325         sock_release(sock);
1326         return retval;
1327 }
1328
1329 /*
1330  *      Create a pair of connected sockets.
1331  */
1332
1333 SYSCALL_DEFINE4(socketpair, int, family, int, type, int, protocol,
1334                 int __user *, usockvec)
1335 {
1336         struct socket *sock1, *sock2;
1337         int fd1, fd2, err;
1338         struct file *newfile1, *newfile2;
1339         int flags;
1340
1341         flags = type & ~SOCK_TYPE_MASK;
1342         if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1343                 return -EINVAL;
1344         type &= SOCK_TYPE_MASK;
1345
1346         if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1347                 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1348
1349         /*
1350          * Obtain the first socket and check if the underlying protocol
1351          * supports the socketpair call.
1352          */
1353
1354         err = sock_create(family, type, protocol, &sock1);
1355         if (err < 0)
1356                 goto out;
1357
1358         err = sock_create(family, type, protocol, &sock2);
1359         if (err < 0)
1360                 goto out_release_1;
1361
1362         err = sock1->ops->socketpair(sock1, sock2);
1363         if (err < 0)
1364                 goto out_release_both;
1365
1366         fd1 = sock_alloc_file(sock1, &newfile1, flags);
1367         if (unlikely(fd1 < 0)) {
1368                 err = fd1;
1369                 goto out_release_both;
1370         }
1371
1372         fd2 = sock_alloc_file(sock2, &newfile2, flags);
1373         if (unlikely(fd2 < 0)) {
1374                 err = fd2;
1375                 fput(newfile1);
1376                 put_unused_fd(fd1);
1377                 sock_release(sock2);
1378                 goto out;
1379         }
1380
1381         audit_fd_pair(fd1, fd2);
1382         fd_install(fd1, newfile1);
1383         fd_install(fd2, newfile2);
1384         /* fd1 and fd2 may be already another descriptors.
1385          * Not kernel problem.
1386          */
1387
1388         err = put_user(fd1, &usockvec[0]);
1389         if (!err)
1390                 err = put_user(fd2, &usockvec[1]);
1391         if (!err)
1392                 return 0;
1393
1394         sys_close(fd2);
1395         sys_close(fd1);
1396         return err;
1397
1398 out_release_both:
1399         sock_release(sock2);
1400 out_release_1:
1401         sock_release(sock1);
1402 out:
1403         return err;
1404 }
1405
1406 /*
1407  *      Bind a name to a socket. Nothing much to do here since it's
1408  *      the protocol's responsibility to handle the local address.
1409  *
1410  *      We move the socket address to kernel space before we call
1411  *      the protocol layer (having also checked the address is ok).
1412  */
1413
1414 SYSCALL_DEFINE3(bind, int, fd, struct sockaddr __user *, umyaddr, int, addrlen)
1415 {
1416         struct socket *sock;
1417         struct sockaddr_storage address;
1418         int err, fput_needed;
1419
1420         sock = sockfd_lookup_light(fd, &err, &fput_needed);
1421         if (sock) {
1422                 err = move_addr_to_kernel(umyaddr, addrlen, (struct sockaddr *)&address);
1423                 if (err >= 0) {
1424                         err = security_socket_bind(sock,
1425                                                    (struct sockaddr *)&address,
1426                                                    addrlen);
1427                         if (!err)
1428                                 err = sock->ops->bind(sock,
1429                                                       (struct sockaddr *)
1430                                                       &address, addrlen);
1431                 }
1432                 fput_light(sock->file, fput_needed);
1433         }
1434         return err;
1435 }
1436
1437 /*
1438  *      Perform a listen. Basically, we allow the protocol to do anything
1439  *      necessary for a listen, and if that works, we mark the socket as
1440  *      ready for listening.
1441  */
1442
1443 SYSCALL_DEFINE2(listen, int, fd, int, backlog)
1444 {
1445         struct socket *sock;
1446         int err, fput_needed;
1447         int somaxconn;
1448
1449         sock = sockfd_lookup_light(fd, &err, &fput_needed);
1450         if (sock) {
1451                 somaxconn = sock_net(sock->sk)->core.sysctl_somaxconn;
1452                 if ((unsigned)backlog > somaxconn)
1453                         backlog = somaxconn;
1454
1455                 err = security_socket_listen(sock, backlog);
1456                 if (!err)
1457                         err = sock->ops->listen(sock, backlog);
1458
1459                 fput_light(sock->file, fput_needed);
1460         }
1461         return err;
1462 }
1463
1464 /*
1465  *      For accept, we attempt to create a new socket, set up the link
1466  *      with the client, wake up the client, then return the new
1467  *      connected fd. We collect the address of the connector in kernel
1468  *      space and move it to user at the very end. This is unclean because
1469  *      we open the socket then return an error.
1470  *
1471  *      1003.1g adds the ability to recvmsg() to query connection pending
1472  *      status to recvmsg. We need to add that support in a way thats
1473  *      clean when we restucture accept also.
1474  */
1475
1476 SYSCALL_DEFINE4(accept4, int, fd, struct sockaddr __user *, upeer_sockaddr,
1477                 int __user *, upeer_addrlen, int, flags)
1478 {
1479         struct socket *sock, *newsock;
1480         struct file *newfile;
1481         int err, len, newfd, fput_needed;
1482         struct sockaddr_storage address;
1483
1484         if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1485                 return -EINVAL;
1486
1487         if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1488                 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1489
1490         sock = sockfd_lookup_light(fd, &err, &fput_needed);
1491         if (!sock)
1492                 goto out;
1493
1494         err = -ENFILE;
1495         if (!(newsock = sock_alloc()))
1496                 goto out_put;
1497
1498         newsock->type = sock->type;
1499         newsock->ops = sock->ops;
1500
1501         /*
1502          * We don't need try_module_get here, as the listening socket (sock)
1503          * has the protocol module (sock->ops->owner) held.
1504          */
1505         __module_get(newsock->ops->owner);
1506
1507         newfd = sock_alloc_file(newsock, &newfile, flags);
1508         if (unlikely(newfd < 0)) {
1509                 err = newfd;
1510                 sock_release(newsock);
1511                 goto out_put;
1512         }
1513
1514         err = security_socket_accept(sock, newsock);
1515         if (err)
1516                 goto out_fd;
1517
1518         err = sock->ops->accept(sock, newsock, sock->file->f_flags);
1519         if (err < 0)
1520                 goto out_fd;
1521
1522         if (upeer_sockaddr) {
1523                 if (newsock->ops->getname(newsock, (struct sockaddr *)&address,
1524                                           &len, 2) < 0) {
1525                         err = -ECONNABORTED;
1526                         goto out_fd;
1527                 }
1528                 err = move_addr_to_user((struct sockaddr *)&address,
1529                                         len, upeer_sockaddr, upeer_addrlen);
1530                 if (err < 0)
1531                         goto out_fd;
1532         }
1533
1534         /* File flags are not inherited via accept() unlike another OSes. */
1535
1536         fd_install(newfd, newfile);
1537         err = newfd;
1538
1539 out_put:
1540         fput_light(sock->file, fput_needed);
1541 out:
1542         return err;
1543 out_fd:
1544         fput(newfile);
1545         put_unused_fd(newfd);
1546         goto out_put;
1547 }
1548
1549 SYSCALL_DEFINE3(accept, int, fd, struct sockaddr __user *, upeer_sockaddr,
1550                 int __user *, upeer_addrlen)
1551 {
1552         return sys_accept4(fd, upeer_sockaddr, upeer_addrlen, 0);
1553 }
1554
1555 /*
1556  *      Attempt to connect to a socket with the server address.  The address
1557  *      is in user space so we verify it is OK and move it to kernel space.
1558  *
1559  *      For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
1560  *      break bindings
1561  *
1562  *      NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
1563  *      other SEQPACKET protocols that take time to connect() as it doesn't
1564  *      include the -EINPROGRESS status for such sockets.
1565  */
1566
1567 SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
1568                 int, addrlen)
1569 {
1570         struct socket *sock;
1571         struct sockaddr_storage address;
1572         int err, fput_needed;
1573
1574         sock = sockfd_lookup_light(fd, &err, &fput_needed);
1575         if (!sock)
1576                 goto out;
1577         err = move_addr_to_kernel(uservaddr, addrlen, (struct sockaddr *)&address);
1578         if (err < 0)
1579                 goto out_put;
1580
1581         err =
1582             security_socket_connect(sock, (struct sockaddr *)&address, addrlen);
1583         if (err)
1584                 goto out_put;
1585
1586         err = sock->ops->connect(sock, (struct sockaddr *)&address, addrlen,
1587                                  sock->file->f_flags);
1588 out_put:
1589         fput_light(sock->file, fput_needed);
1590 out:
1591         return err;
1592 }
1593
1594 /*
1595  *      Get the local address ('name') of a socket object. Move the obtained
1596  *      name to user space.
1597  */
1598
1599 SYSCALL_DEFINE3(getsockname, int, fd, struct sockaddr __user *, usockaddr,
1600                 int __user *, usockaddr_len)
1601 {
1602         struct socket *sock;
1603         struct sockaddr_storage address;
1604         int len, err, fput_needed;
1605
1606         sock = sockfd_lookup_light(fd, &err, &fput_needed);
1607         if (!sock)
1608                 goto out;
1609
1610         err = security_socket_getsockname(sock);
1611         if (err)
1612                 goto out_put;
1613
1614         err = sock->ops->getname(sock, (struct sockaddr *)&address, &len, 0);
1615         if (err)
1616                 goto out_put;
1617         err = move_addr_to_user((struct sockaddr *)&address, len, usockaddr, usockaddr_len);
1618
1619 out_put:
1620         fput_light(sock->file, fput_needed);
1621 out:
1622         return err;
1623 }
1624
1625 /*
1626  *      Get the remote address ('name') of a socket object. Move the obtained
1627  *      name to user space.
1628  */
1629
1630 SYSCALL_DEFINE3(getpeername, int, fd, struct sockaddr __user *, usockaddr,
1631                 int __user *, usockaddr_len)
1632 {
1633         struct socket *sock;
1634         struct sockaddr_storage address;
1635         int len, err, fput_needed;
1636
1637         sock = sockfd_lookup_light(fd, &err, &fput_needed);
1638         if (sock != NULL) {
1639                 err = security_socket_getpeername(sock);
1640                 if (err) {
1641                         fput_light(sock->file, fput_needed);
1642                         return err;
1643                 }
1644
1645                 err =
1646                     sock->ops->getname(sock, (struct sockaddr *)&address, &len,
1647                                        1);
1648                 if (!err)
1649                         err = move_addr_to_user((struct sockaddr *)&address, len, usockaddr,
1650                                                 usockaddr_len);
1651                 fput_light(sock->file, fput_needed);
1652         }
1653         return err;
1654 }
1655
1656 /*
1657  *      Send a datagram to a given address. We move the address into kernel
1658  *      space and check the user space data area is readable before invoking
1659  *      the protocol.
1660  */
1661
1662 SYSCALL_DEFINE6(sendto, int, fd, void __user *, buff, size_t, len,
1663                 unsigned, flags, struct sockaddr __user *, addr,
1664                 int, addr_len)
1665 {
1666         struct socket *sock;
1667         struct sockaddr_storage address;
1668         int err;
1669         struct msghdr msg;
1670         struct iovec iov;
1671         int fput_needed;
1672
1673         sock = sockfd_lookup_light(fd, &err, &fput_needed);
1674         if (!sock)
1675                 goto out;
1676
1677         iov.iov_base = buff;
1678         iov.iov_len = len;
1679         msg.msg_name = NULL;
1680         msg.msg_iov = &iov;
1681         msg.msg_iovlen = 1;
1682         msg.msg_control = NULL;
1683         msg.msg_controllen = 0;
1684         msg.msg_namelen = 0;
1685         if (addr) {
1686                 err = move_addr_to_kernel(addr, addr_len, (struct sockaddr *)&address);
1687                 if (err < 0)
1688                         goto out_put;
1689                 msg.msg_name = (struct sockaddr *)&address;
1690                 msg.msg_namelen = addr_len;
1691         }
1692         if (sock->file->f_flags & O_NONBLOCK)
1693                 flags |= MSG_DONTWAIT;
1694         msg.msg_flags = flags;
1695         err = sock_sendmsg(sock, &msg, len);
1696
1697 out_put:
1698         fput_light(sock->file, fput_needed);
1699 out:
1700         return err;
1701 }
1702
1703 /*
1704  *      Send a datagram down a socket.
1705  */
1706
1707 SYSCALL_DEFINE4(send, int, fd, void __user *, buff, size_t, len,
1708                 unsigned, flags)
1709 {
1710         return sys_sendto(fd, buff, len, flags, NULL, 0);
1711 }
1712
1713 /*
1714  *      Receive a frame from the socket and optionally record the address of the
1715  *      sender. We verify the buffers are writable and if needed move the
1716  *      sender address from kernel to user space.
1717  */
1718
1719 SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, ubuf, size_t, size,
1720                 unsigned, flags, struct sockaddr __user *, addr,
1721                 int __user *, addr_len)
1722 {
1723         struct socket *sock;
1724         struct iovec iov;
1725         struct msghdr msg;
1726         struct sockaddr_storage address;
1727         int err, err2;
1728         int fput_needed;
1729
1730         sock = sockfd_lookup_light(fd, &err, &fput_needed);
1731         if (!sock)
1732                 goto out;
1733
1734         msg.msg_control = NULL;
1735         msg.msg_controllen = 0;
1736         msg.msg_iovlen = 1;
1737         msg.msg_iov = &iov;
1738         iov.iov_len = size;
1739         iov.iov_base = ubuf;
1740         msg.msg_name = (struct sockaddr *)&address;
1741         msg.msg_namelen = sizeof(address);
1742         if (sock->file->f_flags & O_NONBLOCK)
1743                 flags |= MSG_DONTWAIT;
1744         err = sock_recvmsg(sock, &msg, size, flags);
1745
1746         if (err >= 0 && addr != NULL) {
1747                 err2 = move_addr_to_user((struct sockaddr *)&address,
1748                                          msg.msg_namelen, addr, addr_len);
1749                 if (err2 < 0)
1750                         err = err2;
1751         }
1752
1753         fput_light(sock->file, fput_needed);
1754 out:
1755         return err;
1756 }
1757
1758 /*
1759  *      Receive a datagram from a socket.
1760  */
1761
1762 asmlinkage long sys_recv(int fd, void __user *ubuf, size_t size,
1763                          unsigned flags)
1764 {
1765         return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
1766 }
1767
1768 /*
1769  *      Set a socket option. Because we don't know the option lengths we have
1770  *      to pass the user mode parameter for the protocols to sort out.
1771  */
1772
1773 SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname,
1774                 char __user *, optval, int, optlen)
1775 {
1776         int err, fput_needed;
1777         struct socket *sock;
1778
1779         if (optlen < 0)
1780                 return -EINVAL;
1781
1782         sock = sockfd_lookup_light(fd, &err, &fput_needed);
1783         if (sock != NULL) {
1784                 err = security_socket_setsockopt(sock, level, optname);
1785                 if (err)
1786                         goto out_put;
1787
1788                 if (level == SOL_SOCKET)
1789                         err =
1790                             sock_setsockopt(sock, level, optname, optval,
1791                                             optlen);
1792                 else
1793                         err =
1794                             sock->ops->setsockopt(sock, level, optname, optval,
1795                                                   optlen);
1796 out_put:
1797                 fput_light(sock->file, fput_needed);
1798         }
1799         return err;
1800 }
1801
1802 /*
1803  *      Get a socket option. Because we don't know the option lengths we have
1804  *      to pass a user mode parameter for the protocols to sort out.
1805  */
1806
1807 SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname,
1808                 char __user *, optval, int __user *, optlen)
1809 {
1810         int err, fput_needed;
1811         struct socket *sock;
1812
1813         sock = sockfd_lookup_light(fd, &err, &fput_needed);
1814         if (sock != NULL) {
1815                 err = security_socket_getsockopt(sock, level, optname);
1816                 if (err)
1817                         goto out_put;
1818
1819                 if (level == SOL_SOCKET)
1820                         err =
1821                             sock_getsockopt(sock, level, optname, optval,
1822                                             optlen);
1823                 else
1824                         err =
1825                             sock->ops->getsockopt(sock, level, optname, optval,
1826                                                   optlen);
1827 out_put:
1828                 fput_light(sock->file, fput_needed);
1829         }
1830         return err;
1831 }
1832
1833 /*
1834  *      Shutdown a socket.
1835  */
1836
1837 SYSCALL_DEFINE2(shutdown, int, fd, int, how)
1838 {
1839         int err, fput_needed;
1840         struct socket *sock;
1841
1842         sock = sockfd_lookup_light(fd, &err, &fput_needed);
1843         if (sock != NULL) {
1844                 err = security_socket_shutdown(sock, how);
1845                 if (!err)
1846                         err = sock->ops->shutdown(sock, how);
1847                 fput_light(sock->file, fput_needed);
1848         }
1849         return err;
1850 }
1851
1852 /* A couple of helpful macros for getting the address of the 32/64 bit
1853  * fields which are the same type (int / unsigned) on our platforms.
1854  */
1855 #define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
1856 #define COMPAT_NAMELEN(msg)     COMPAT_MSG(msg, msg_namelen)
1857 #define COMPAT_FLAGS(msg)       COMPAT_MSG(msg, msg_flags)
1858
1859 /*
1860  *      BSD sendmsg interface
1861  */
1862
1863 SYSCALL_DEFINE3(sendmsg, int, fd, struct msghdr __user *, msg, unsigned, flags)
1864 {
1865         struct compat_msghdr __user *msg_compat =
1866             (struct compat_msghdr __user *)msg;
1867         struct socket *sock;
1868         struct sockaddr_storage address;
1869         struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
1870         unsigned char ctl[sizeof(struct cmsghdr) + 20]
1871             __attribute__ ((aligned(sizeof(__kernel_size_t))));
1872         /* 20 is size of ipv6_pktinfo */
1873         unsigned char *ctl_buf = ctl;
1874         struct msghdr msg_sys;
1875         int err, ctl_len, iov_size, total_len;
1876         int fput_needed;
1877
1878         err = -EFAULT;
1879         if (MSG_CMSG_COMPAT & flags) {
1880                 if (get_compat_msghdr(&msg_sys, msg_compat))
1881                         return -EFAULT;
1882         }
1883         else if (copy_from_user(&msg_sys, msg, sizeof(struct msghdr)))
1884                 return -EFAULT;
1885
1886         sock = sockfd_lookup_light(fd, &err, &fput_needed);
1887         if (!sock)
1888                 goto out;
1889
1890         /* do not move before msg_sys is valid */
1891         err = -EMSGSIZE;
1892         if (msg_sys.msg_iovlen > UIO_MAXIOV)
1893                 goto out_put;
1894
1895         /* Check whether to allocate the iovec area */
1896         err = -ENOMEM;
1897         iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
1898         if (msg_sys.msg_iovlen > UIO_FASTIOV) {
1899                 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
1900                 if (!iov)
1901                         goto out_put;
1902         }
1903
1904         /* This will also move the address data into kernel space */
1905         if (MSG_CMSG_COMPAT & flags) {
1906                 err = verify_compat_iovec(&msg_sys, iov,
1907                                           (struct sockaddr *)&address,
1908                                           VERIFY_READ);
1909         } else
1910                 err = verify_iovec(&msg_sys, iov,
1911                                    (struct sockaddr *)&address,
1912                                    VERIFY_READ);
1913         if (err < 0)
1914                 goto out_freeiov;
1915         total_len = err;
1916
1917         err = -ENOBUFS;
1918
1919         if (msg_sys.msg_controllen > INT_MAX)
1920                 goto out_freeiov;
1921         ctl_len = msg_sys.msg_controllen;
1922         if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
1923                 err =
1924                     cmsghdr_from_user_compat_to_kern(&msg_sys, sock->sk, ctl,
1925                                                      sizeof(ctl));
1926                 if (err)
1927                         goto out_freeiov;
1928                 ctl_buf = msg_sys.msg_control;
1929                 ctl_len = msg_sys.msg_controllen;
1930         } else if (ctl_len) {
1931                 if (ctl_len > sizeof(ctl)) {
1932                         ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
1933                         if (ctl_buf == NULL)
1934                                 goto out_freeiov;
1935                 }
1936                 err = -EFAULT;
1937                 /*
1938                  * Careful! Before this, msg_sys.msg_control contains a user pointer.
1939                  * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
1940                  * checking falls down on this.
1941                  */
1942                 if (copy_from_user(ctl_buf, (void __user *)msg_sys.msg_control,
1943                                    ctl_len))
1944                         goto out_freectl;
1945                 msg_sys.msg_control = ctl_buf;
1946         }
1947         msg_sys.msg_flags = flags;
1948
1949         if (sock->file->f_flags & O_NONBLOCK)
1950                 msg_sys.msg_flags |= MSG_DONTWAIT;
1951         err = sock_sendmsg(sock, &msg_sys, total_len);
1952
1953 out_freectl:
1954         if (ctl_buf != ctl)
1955                 sock_kfree_s(sock->sk, ctl_buf, ctl_len);
1956 out_freeiov:
1957         if (iov != iovstack)
1958                 sock_kfree_s(sock->sk, iov, iov_size);
1959 out_put:
1960         fput_light(sock->file, fput_needed);
1961 out:
1962         return err;
1963 }
1964
1965 static int __sys_recvmsg(struct socket *sock, struct msghdr __user *msg,
1966                          struct msghdr *msg_sys, unsigned flags, int nosec)
1967 {
1968         struct compat_msghdr __user *msg_compat =
1969             (struct compat_msghdr __user *)msg;
1970         struct iovec iovstack[UIO_FASTIOV];
1971         struct iovec *iov = iovstack;
1972         unsigned long cmsg_ptr;
1973         int err, iov_size, total_len, len;
1974
1975         /* kernel mode address */
1976         struct sockaddr_storage addr;
1977
1978         /* user mode address pointers */
1979         struct sockaddr __user *uaddr;
1980         int __user *uaddr_len;
1981
1982         if (MSG_CMSG_COMPAT & flags) {
1983                 if (get_compat_msghdr(msg_sys, msg_compat))
1984                         return -EFAULT;
1985         }
1986         else if (copy_from_user(msg_sys, msg, sizeof(struct msghdr)))
1987                 return -EFAULT;
1988
1989         err = -EMSGSIZE;
1990         if (msg_sys->msg_iovlen > UIO_MAXIOV)
1991                 goto out;
1992
1993         /* Check whether to allocate the iovec area */
1994         err = -ENOMEM;
1995         iov_size = msg_sys->msg_iovlen * sizeof(struct iovec);
1996         if (msg_sys->msg_iovlen > UIO_FASTIOV) {
1997                 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
1998                 if (!iov)
1999                         goto out;
2000         }
2001
2002         /*
2003          *      Save the user-mode address (verify_iovec will change the
2004          *      kernel msghdr to use the kernel address space)
2005          */
2006
2007         uaddr = (__force void __user *)msg_sys->msg_name;
2008         uaddr_len = COMPAT_NAMELEN(msg);
2009         if (MSG_CMSG_COMPAT & flags) {
2010                 err = verify_compat_iovec(msg_sys, iov,
2011                                           (struct sockaddr *)&addr,
2012                                           VERIFY_WRITE);
2013         } else
2014                 err = verify_iovec(msg_sys, iov,
2015                                    (struct sockaddr *)&addr,
2016                                    VERIFY_WRITE);
2017         if (err < 0)
2018                 goto out_freeiov;
2019         total_len = err;
2020
2021         cmsg_ptr = (unsigned long)msg_sys->msg_control;
2022         msg_sys->msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT);
2023
2024         if (sock->file->f_flags & O_NONBLOCK)
2025                 flags |= MSG_DONTWAIT;
2026         err = (nosec ? sock_recvmsg_nosec : sock_recvmsg)(sock, msg_sys,
2027                                                           total_len, flags);
2028         if (err < 0)
2029                 goto out_freeiov;
2030         len = err;
2031
2032         if (uaddr != NULL) {
2033                 err = move_addr_to_user((struct sockaddr *)&addr,
2034                                         msg_sys->msg_namelen, uaddr,
2035                                         uaddr_len);
2036                 if (err < 0)
2037                         goto out_freeiov;
2038         }
2039         err = __put_user((msg_sys->msg_flags & ~MSG_CMSG_COMPAT),
2040                          COMPAT_FLAGS(msg));
2041         if (err)
2042                 goto out_freeiov;
2043         if (MSG_CMSG_COMPAT & flags)
2044                 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
2045                                  &msg_compat->msg_controllen);
2046         else
2047                 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
2048                                  &msg->msg_controllen);
2049         if (err)
2050                 goto out_freeiov;
2051         err = len;
2052
2053 out_freeiov:
2054         if (iov != iovstack)
2055                 sock_kfree_s(sock->sk, iov, iov_size);
2056 out:
2057         return err;
2058 }
2059
2060 /*
2061  *      BSD recvmsg interface
2062  */
2063
2064 SYSCALL_DEFINE3(recvmsg, int, fd, struct msghdr __user *, msg,
2065                 unsigned int, flags)
2066 {
2067         int fput_needed, err;
2068         struct msghdr msg_sys;
2069         struct socket *sock = sockfd_lookup_light(fd, &err, &fput_needed);
2070
2071         if (!sock)
2072                 goto out;
2073
2074         err = __sys_recvmsg(sock, msg, &msg_sys, flags, 0);
2075
2076         fput_light(sock->file, fput_needed);
2077 out:
2078         return err;
2079 }
2080
2081 /*
2082  *     Linux recvmmsg interface
2083  */
2084
2085 int __sys_recvmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
2086                    unsigned int flags, struct timespec *timeout)
2087 {
2088         int fput_needed, err, datagrams;
2089         struct socket *sock;
2090         struct mmsghdr __user *entry;
2091         struct compat_mmsghdr __user *compat_entry;
2092         struct msghdr msg_sys;
2093         struct timespec end_time;
2094
2095         if (timeout &&
2096             poll_select_set_timeout(&end_time, timeout->tv_sec,
2097                                     timeout->tv_nsec))
2098                 return -EINVAL;
2099
2100         datagrams = 0;
2101
2102         sock = sockfd_lookup_light(fd, &err, &fput_needed);
2103         if (!sock)
2104                 return err;
2105
2106         err = sock_error(sock->sk);
2107         if (err)
2108                 goto out_put;
2109
2110         entry = mmsg;
2111         compat_entry = (struct compat_mmsghdr __user *)mmsg;
2112
2113         while (datagrams < vlen) {
2114                 /*
2115                  * No need to ask LSM for more than the first datagram.
2116                  */
2117                 if (MSG_CMSG_COMPAT & flags) {
2118                         err = __sys_recvmsg(sock, (struct msghdr __user *)compat_entry,
2119                                             &msg_sys, flags, datagrams);
2120                         if (err < 0)
2121                                 break;
2122                         err = __put_user(err, &compat_entry->msg_len);
2123                         ++compat_entry;
2124                 } else {
2125                         err = __sys_recvmsg(sock, (struct msghdr __user *)entry,
2126                                             &msg_sys, flags, datagrams);
2127                         if (err < 0)
2128                                 break;
2129                         err = put_user(err, &entry->msg_len);
2130                         ++entry;
2131                 }
2132
2133                 if (err)
2134                         break;
2135                 ++datagrams;
2136
2137                 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
2138                 if (flags & MSG_WAITFORONE)
2139                         flags |= MSG_DONTWAIT;
2140
2141                 if (timeout) {
2142                         ktime_get_ts(timeout);
2143                         *timeout = timespec_sub(end_time, *timeout);
2144                         if (timeout->tv_sec < 0) {
2145                                 timeout->tv_sec = timeout->tv_nsec = 0;
2146                                 break;
2147                         }
2148
2149                         /* Timeout, return less than vlen datagrams */
2150                         if (timeout->tv_nsec == 0 && timeout->tv_sec == 0)
2151                                 break;
2152                 }
2153
2154                 /* Out of band data, return right away */
2155                 if (msg_sys.msg_flags & MSG_OOB)
2156                         break;
2157         }
2158
2159 out_put:
2160         fput_light(sock->file, fput_needed);
2161
2162         if (err == 0)
2163                 return datagrams;
2164
2165         if (datagrams != 0) {
2166                 /*
2167                  * We may return less entries than requested (vlen) if the
2168                  * sock is non block and there aren't enough datagrams...
2169                  */
2170                 if (err != -EAGAIN) {
2171                         /*
2172                          * ... or  if recvmsg returns an error after we
2173                          * received some datagrams, where we record the
2174                          * error to return on the next call or if the
2175                          * app asks about it using getsockopt(SO_ERROR).
2176                          */
2177                         sock->sk->sk_err = -err;
2178                 }
2179
2180                 return datagrams;
2181         }
2182
2183         return err;
2184 }
2185
2186 SYSCALL_DEFINE5(recvmmsg, int, fd, struct mmsghdr __user *, mmsg,
2187                 unsigned int, vlen, unsigned int, flags,
2188                 struct timespec __user *, timeout)
2189 {
2190         int datagrams;
2191         struct timespec timeout_sys;
2192
2193         if (!timeout)
2194                 return __sys_recvmmsg(fd, mmsg, vlen, flags, NULL);
2195
2196         if (copy_from_user(&timeout_sys, timeout, sizeof(timeout_sys)))
2197                 return -EFAULT;
2198
2199         datagrams = __sys_recvmmsg(fd, mmsg, vlen, flags, &timeout_sys);
2200
2201         if (datagrams > 0 &&
2202             copy_to_user(timeout, &timeout_sys, sizeof(timeout_sys)))
2203                 datagrams = -EFAULT;
2204
2205         return datagrams;
2206 }
2207
2208 #ifdef __ARCH_WANT_SYS_SOCKETCALL
2209 /* Argument list sizes for sys_socketcall */
2210 #define AL(x) ((x) * sizeof(unsigned long))
2211 static const unsigned char nargs[20] = {
2212         AL(0),AL(3),AL(3),AL(3),AL(2),AL(3),
2213         AL(3),AL(3),AL(4),AL(4),AL(4),AL(6),
2214         AL(6),AL(2),AL(5),AL(5),AL(3),AL(3),
2215         AL(4),AL(5)
2216 };
2217
2218 #undef AL
2219
2220 /*
2221  *      System call vectors.
2222  *
2223  *      Argument checking cleaned up. Saved 20% in size.
2224  *  This function doesn't need to set the kernel lock because
2225  *  it is set by the callees.
2226  */
2227
2228 SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args)
2229 {
2230         unsigned long a[6];
2231         unsigned long a0, a1;
2232         int err;
2233         unsigned int len;
2234
2235         if (call < 1 || call > SYS_RECVMMSG)
2236                 return -EINVAL;
2237
2238         len = nargs[call];
2239         if (len > sizeof(a))
2240                 return -EINVAL;
2241
2242         /* copy_from_user should be SMP safe. */
2243         if (copy_from_user(a, args, len))
2244                 return -EFAULT;
2245
2246         audit_socketcall(nargs[call] / sizeof(unsigned long), a);
2247
2248         a0 = a[0];
2249         a1 = a[1];
2250
2251         switch (call) {
2252         case SYS_SOCKET:
2253                 err = sys_socket(a0, a1, a[2]);
2254                 break;
2255         case SYS_BIND:
2256                 err = sys_bind(a0, (struct sockaddr __user *)a1, a[2]);
2257                 break;
2258         case SYS_CONNECT:
2259                 err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
2260                 break;
2261         case SYS_LISTEN:
2262                 err = sys_listen(a0, a1);
2263                 break;
2264         case SYS_ACCEPT:
2265                 err = sys_accept4(a0, (struct sockaddr __user *)a1,
2266                                   (int __user *)a[2], 0);
2267                 break;
2268         case SYS_GETSOCKNAME:
2269                 err =
2270                     sys_getsockname(a0, (struct sockaddr __user *)a1,
2271                                     (int __user *)a[2]);
2272                 break;
2273         case SYS_GETPEERNAME:
2274                 err =
2275                     sys_getpeername(a0, (struct sockaddr __user *)a1,
2276                                     (int __user *)a[2]);
2277                 break;
2278         case SYS_SOCKETPAIR:
2279                 err = sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
2280                 break;
2281         case SYS_SEND:
2282                 err = sys_send(a0, (void __user *)a1, a[2], a[3]);
2283                 break;
2284         case SYS_SENDTO:
2285                 err = sys_sendto(a0, (void __user *)a1, a[2], a[3],
2286                                  (struct sockaddr __user *)a[4], a[5]);
2287                 break;
2288         case SYS_RECV:
2289                 err = sys_recv(a0, (void __user *)a1, a[2], a[3]);
2290                 break;
2291         case SYS_RECVFROM:
2292                 err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
2293                                    (struct sockaddr __user *)a[4],
2294                                    (int __user *)a[5]);
2295                 break;
2296         case SYS_SHUTDOWN:
2297                 err = sys_shutdown(a0, a1);
2298                 break;
2299         case SYS_SETSOCKOPT:
2300                 err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]);
2301                 break;
2302         case SYS_GETSOCKOPT:
2303                 err =
2304                     sys_getsockopt(a0, a1, a[2], (char __user *)a[3],
2305                                    (int __user *)a[4]);
2306                 break;
2307         case SYS_SENDMSG:
2308                 err = sys_sendmsg(a0, (struct msghdr __user *)a1, a[2]);
2309                 break;
2310         case SYS_RECVMSG:
2311                 err = sys_recvmsg(a0, (struct msghdr __user *)a1, a[2]);
2312                 break;
2313         case SYS_RECVMMSG:
2314                 err = sys_recvmmsg(a0, (struct mmsghdr __user *)a1, a[2], a[3],
2315                                    (struct timespec __user *)a[4]);
2316                 break;
2317         case SYS_ACCEPT4:
2318                 err = sys_accept4(a0, (struct sockaddr __user *)a1,
2319                                   (int __user *)a[2], a[3]);
2320                 break;
2321         default:
2322                 err = -EINVAL;
2323                 break;
2324         }
2325         return err;
2326 }
2327
2328 #endif                          /* __ARCH_WANT_SYS_SOCKETCALL */
2329
2330 /**
2331  *      sock_register - add a socket protocol handler
2332  *      @ops: description of protocol
2333  *
2334  *      This function is called by a protocol handler that wants to
2335  *      advertise its address family, and have it linked into the
2336  *      socket interface. The value ops->family coresponds to the
2337  *      socket system call protocol family.
2338  */
2339 int sock_register(const struct net_proto_family *ops)
2340 {
2341         int err;
2342
2343         if (ops->family >= NPROTO) {
2344                 printk(KERN_CRIT "protocol %d >= NPROTO(%d)\n", ops->family,
2345                        NPROTO);
2346                 return -ENOBUFS;
2347         }
2348
2349         spin_lock(&net_family_lock);
2350         if (net_families[ops->family])
2351                 err = -EEXIST;
2352         else {
2353                 net_families[ops->family] = ops;
2354                 err = 0;
2355         }
2356         spin_unlock(&net_family_lock);
2357
2358         printk(KERN_INFO "NET: Registered protocol family %d\n", ops->family);
2359         return err;
2360 }
2361
2362 /**
2363  *      sock_unregister - remove a protocol handler
2364  *      @family: protocol family to remove
2365  *
2366  *      This function is called by a protocol handler that wants to
2367  *      remove its address family, and have it unlinked from the
2368  *      new socket creation.
2369  *
2370  *      If protocol handler is a module, then it can use module reference
2371  *      counts to protect against new references. If protocol handler is not
2372  *      a module then it needs to provide its own protection in
2373  *      the ops->create routine.
2374  */
2375 void sock_unregister(int family)
2376 {
2377         BUG_ON(family < 0 || family >= NPROTO);
2378
2379         spin_lock(&net_family_lock);
2380         net_families[family] = NULL;
2381         spin_unlock(&net_family_lock);
2382
2383         synchronize_rcu();
2384
2385         printk(KERN_INFO "NET: Unregistered protocol family %d\n", family);
2386 }
2387
2388 static int __init sock_init(void)
2389 {
2390         /*
2391          *      Initialize sock SLAB cache.
2392          */
2393
2394         sk_init();
2395
2396         /*
2397          *      Initialize skbuff SLAB cache
2398          */
2399         skb_init();
2400
2401         /*
2402          *      Initialize the protocols module.
2403          */
2404
2405         init_inodecache();
2406         register_filesystem(&sock_fs_type);
2407         sock_mnt = kern_mount(&sock_fs_type);
2408
2409         /* The real protocol initialization is performed in later initcalls.
2410          */
2411
2412 #ifdef CONFIG_NETFILTER
2413         netfilter_init();
2414 #endif
2415
2416         return 0;
2417 }
2418
2419 core_initcall(sock_init);       /* early initcall */
2420
2421 #ifdef CONFIG_PROC_FS
2422 void socket_seq_show(struct seq_file *seq)
2423 {
2424         int cpu;
2425         int counter = 0;
2426
2427         for_each_possible_cpu(cpu)
2428             counter += per_cpu(sockets_in_use, cpu);
2429
2430         /* It can be negative, by the way. 8) */
2431         if (counter < 0)
2432                 counter = 0;
2433
2434         seq_printf(seq, "sockets: used %d\n", counter);
2435 }
2436 #endif                          /* CONFIG_PROC_FS */
2437
2438 #ifdef CONFIG_COMPAT
2439 static int do_siocgstamp(struct net *net, struct socket *sock,
2440                          unsigned int cmd, struct compat_timeval __user *up)
2441 {
2442         mm_segment_t old_fs = get_fs();
2443         struct timeval ktv;
2444         int err;
2445
2446         set_fs(KERNEL_DS);
2447         err = sock_do_ioctl(net, sock, cmd, (unsigned long)&ktv);
2448         set_fs(old_fs);
2449         if (!err) {
2450                 err = put_user(ktv.tv_sec, &up->tv_sec);
2451                 err |= __put_user(ktv.tv_usec, &up->tv_usec);
2452         }
2453         return err;
2454 }
2455
2456 static int do_siocgstampns(struct net *net, struct socket *sock,
2457                          unsigned int cmd, struct compat_timespec __user *up)
2458 {
2459         mm_segment_t old_fs = get_fs();
2460         struct timespec kts;
2461         int err;
2462
2463         set_fs(KERNEL_DS);
2464         err = sock_do_ioctl(net, sock, cmd, (unsigned long)&kts);
2465         set_fs(old_fs);
2466         if (!err) {
2467                 err = put_user(kts.tv_sec, &up->tv_sec);
2468                 err |= __put_user(kts.tv_nsec, &up->tv_nsec);
2469         }
2470         return err;
2471 }
2472
2473 static int dev_ifname32(struct net *net, struct compat_ifreq __user *uifr32)
2474 {
2475         struct ifreq __user *uifr;
2476         int err;
2477
2478         uifr = compat_alloc_user_space(sizeof(struct ifreq));
2479         if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
2480                 return -EFAULT;
2481
2482         err = dev_ioctl(net, SIOCGIFNAME, uifr);
2483         if (err)
2484                 return err;
2485
2486         if (copy_in_user(uifr32, uifr, sizeof(struct compat_ifreq)))
2487                 return -EFAULT;
2488
2489         return 0;
2490 }
2491
2492 static int dev_ifconf(struct net *net, struct compat_ifconf __user *uifc32)
2493 {
2494         struct compat_ifconf ifc32;
2495         struct ifconf ifc;
2496         struct ifconf __user *uifc;
2497         struct compat_ifreq __user *ifr32;
2498         struct ifreq __user *ifr;
2499         unsigned int i, j;
2500         int err;
2501
2502         if (copy_from_user(&ifc32, uifc32, sizeof(struct compat_ifconf)))
2503                 return -EFAULT;
2504
2505         if (ifc32.ifcbuf == 0) {
2506                 ifc32.ifc_len = 0;
2507                 ifc.ifc_len = 0;
2508                 ifc.ifc_req = NULL;
2509                 uifc = compat_alloc_user_space(sizeof(struct ifconf));
2510         } else {
2511                 size_t len =((ifc32.ifc_len / sizeof (struct compat_ifreq)) + 1) *
2512                         sizeof (struct ifreq);
2513                 uifc = compat_alloc_user_space(sizeof(struct ifconf) + len);
2514                 ifc.ifc_len = len;
2515                 ifr = ifc.ifc_req = (void __user *)(uifc + 1);
2516                 ifr32 = compat_ptr(ifc32.ifcbuf);
2517                 for (i = 0; i < ifc32.ifc_len; i += sizeof (struct compat_ifreq)) {
2518                         if (copy_in_user(ifr, ifr32, sizeof(struct compat_ifreq)))
2519                                 return -EFAULT;
2520                         ifr++;
2521                         ifr32++;
2522                 }
2523         }
2524         if (copy_to_user(uifc, &ifc, sizeof(struct ifconf)))
2525                 return -EFAULT;
2526
2527         err = dev_ioctl(net, SIOCGIFCONF, uifc);
2528         if (err)
2529                 return err;
2530
2531         if (copy_from_user(&ifc, uifc, sizeof(struct ifconf)))
2532                 return -EFAULT;
2533
2534         ifr = ifc.ifc_req;
2535         ifr32 = compat_ptr(ifc32.ifcbuf);
2536         for (i = 0, j = 0;
2537              i + sizeof (struct compat_ifreq) <= ifc32.ifc_len && j < ifc.ifc_len;
2538              i += sizeof (struct compat_ifreq), j += sizeof (struct ifreq)) {
2539                 if (copy_in_user(ifr32, ifr, sizeof (struct compat_ifreq)))
2540                         return -EFAULT;
2541                 ifr32++;
2542                 ifr++;
2543         }
2544
2545         if (ifc32.ifcbuf == 0) {
2546                 /* Translate from 64-bit structure multiple to
2547                  * a 32-bit one.
2548                  */
2549                 i = ifc.ifc_len;
2550                 i = ((i / sizeof(struct ifreq)) * sizeof(struct compat_ifreq));
2551                 ifc32.ifc_len = i;
2552         } else {
2553                 ifc32.ifc_len = i;
2554         }
2555         if (copy_to_user(uifc32, &ifc32, sizeof(struct compat_ifconf)))
2556                 return -EFAULT;
2557
2558         return 0;
2559 }
2560
2561 static int ethtool_ioctl(struct net *net, struct compat_ifreq __user *ifr32)
2562 {
2563         struct ifreq __user *ifr;
2564         u32 data;
2565         void __user *datap;
2566
2567         ifr = compat_alloc_user_space(sizeof(*ifr));
2568
2569         if (copy_in_user(&ifr->ifr_name, &ifr32->ifr_name, IFNAMSIZ))
2570                 return -EFAULT;
2571
2572         if (get_user(data, &ifr32->ifr_ifru.ifru_data))
2573                 return -EFAULT;
2574
2575         datap = compat_ptr(data);
2576         if (put_user(datap, &ifr->ifr_ifru.ifru_data))
2577                 return -EFAULT;
2578
2579         return dev_ioctl(net, SIOCETHTOOL, ifr);
2580 }
2581
2582 static int compat_siocwandev(struct net *net, struct compat_ifreq __user *uifr32)
2583 {
2584         void __user *uptr;
2585         compat_uptr_t uptr32;
2586         struct ifreq __user *uifr;
2587
2588         uifr = compat_alloc_user_space(sizeof (*uifr));
2589         if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
2590                 return -EFAULT;
2591
2592         if (get_user(uptr32, &uifr32->ifr_settings.ifs_ifsu))
2593                 return -EFAULT;
2594
2595         uptr = compat_ptr(uptr32);
2596
2597         if (put_user(uptr, &uifr->ifr_settings.ifs_ifsu.raw_hdlc))
2598                 return -EFAULT;
2599
2600         return dev_ioctl(net, SIOCWANDEV, uifr);
2601 }
2602
2603 static int bond_ioctl(struct net *net, unsigned int cmd,
2604                          struct compat_ifreq __user *ifr32)
2605 {
2606         struct ifreq kifr;
2607         struct ifreq __user *uifr;
2608         mm_segment_t old_fs;
2609         int err;
2610         u32 data;
2611         void __user *datap;
2612
2613         switch (cmd) {
2614         case SIOCBONDENSLAVE:
2615         case SIOCBONDRELEASE:
2616         case SIOCBONDSETHWADDR:
2617         case SIOCBONDCHANGEACTIVE:
2618                 if (copy_from_user(&kifr, ifr32, sizeof(struct compat_ifreq)))
2619                         return -EFAULT;
2620
2621                 old_fs = get_fs();
2622                 set_fs (KERNEL_DS);
2623                 err = dev_ioctl(net, cmd, &kifr);
2624                 set_fs (old_fs);
2625
2626                 return err;
2627         case SIOCBONDSLAVEINFOQUERY:
2628         case SIOCBONDINFOQUERY:
2629                 uifr = compat_alloc_user_space(sizeof(*uifr));
2630                 if (copy_in_user(&uifr->ifr_name, &ifr32->ifr_name, IFNAMSIZ))
2631                         return -EFAULT;
2632
2633                 if (get_user(data, &ifr32->ifr_ifru.ifru_data))
2634                         return -EFAULT;
2635
2636                 datap = compat_ptr(data);
2637                 if (put_user(datap, &uifr->ifr_ifru.ifru_data))
2638                         return -EFAULT;
2639
2640                 return dev_ioctl(net, cmd, uifr);
2641         default:
2642                 return -EINVAL;
2643         };
2644 }
2645
2646 static int siocdevprivate_ioctl(struct net *net, unsigned int cmd,
2647                                  struct compat_ifreq __user *u_ifreq32)
2648 {
2649         struct ifreq __user *u_ifreq64;
2650         char tmp_buf[IFNAMSIZ];
2651         void __user *data64;
2652         u32 data32;
2653
2654         if (copy_from_user(&tmp_buf[0], &(u_ifreq32->ifr_ifrn.ifrn_name[0]),
2655                            IFNAMSIZ))
2656                 return -EFAULT;
2657         if (__get_user(data32, &u_ifreq32->ifr_ifru.ifru_data))
2658                 return -EFAULT;
2659         data64 = compat_ptr(data32);
2660
2661         u_ifreq64 = compat_alloc_user_space(sizeof(*u_ifreq64));
2662
2663         /* Don't check these user accesses, just let that get trapped
2664          * in the ioctl handler instead.
2665          */
2666         if (copy_to_user(&u_ifreq64->ifr_ifrn.ifrn_name[0], &tmp_buf[0],
2667                          IFNAMSIZ))
2668                 return -EFAULT;
2669         if (__put_user(data64, &u_ifreq64->ifr_ifru.ifru_data))
2670                 return -EFAULT;
2671
2672         return dev_ioctl(net, cmd, u_ifreq64);
2673 }
2674
2675 static int dev_ifsioc(struct net *net, struct socket *sock,
2676                          unsigned int cmd, struct compat_ifreq __user *uifr32)
2677 {
2678         struct ifreq __user *uifr;
2679         int err;
2680
2681         uifr = compat_alloc_user_space(sizeof(*uifr));
2682         if (copy_in_user(uifr, uifr32, sizeof(*uifr32)))
2683                 return -EFAULT;
2684
2685         err = sock_do_ioctl(net, sock, cmd, (unsigned long)uifr);
2686
2687         if (!err) {
2688                 switch (cmd) {
2689                 case SIOCGIFFLAGS:
2690                 case SIOCGIFMETRIC:
2691                 case SIOCGIFMTU:
2692                 case SIOCGIFMEM:
2693                 case SIOCGIFHWADDR:
2694                 case SIOCGIFINDEX:
2695                 case SIOCGIFADDR:
2696                 case SIOCGIFBRDADDR:
2697                 case SIOCGIFDSTADDR:
2698                 case SIOCGIFNETMASK:
2699                 case SIOCGIFPFLAGS:
2700                 case SIOCGIFTXQLEN:
2701                 case SIOCGMIIPHY:
2702                 case SIOCGMIIREG:
2703                         if (copy_in_user(uifr32, uifr, sizeof(*uifr32)))
2704                                 err = -EFAULT;
2705                         break;
2706                 }
2707         }
2708         return err;
2709 }
2710
2711 static int compat_sioc_ifmap(struct net *net, unsigned int cmd,
2712                         struct compat_ifreq __user *uifr32)
2713 {
2714         struct ifreq ifr;
2715         struct compat_ifmap __user *uifmap32;
2716         mm_segment_t old_fs;
2717         int err;
2718
2719         uifmap32 = &uifr32->ifr_ifru.ifru_map;
2720         err = copy_from_user(&ifr, uifr32, sizeof(ifr.ifr_name));
2721         err |= __get_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
2722         err |= __get_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
2723         err |= __get_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
2724         err |= __get_user(ifr.ifr_map.irq, &uifmap32->irq);
2725         err |= __get_user(ifr.ifr_map.dma, &uifmap32->dma);
2726         err |= __get_user(ifr.ifr_map.port, &uifmap32->port);
2727         if (err)
2728                 return -EFAULT;
2729
2730         old_fs = get_fs();
2731         set_fs (KERNEL_DS);
2732         err = dev_ioctl(net, cmd, (void __user *)&ifr);
2733         set_fs (old_fs);
2734
2735         if (cmd == SIOCGIFMAP && !err) {
2736                 err = copy_to_user(uifr32, &ifr, sizeof(ifr.ifr_name));
2737                 err |= __put_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
2738                 err |= __put_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
2739                 err |= __put_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
2740                 err |= __put_user(ifr.ifr_map.irq, &uifmap32->irq);
2741                 err |= __put_user(ifr.ifr_map.dma, &uifmap32->dma);
2742                 err |= __put_user(ifr.ifr_map.port, &uifmap32->port);
2743                 if (err)
2744                         err = -EFAULT;
2745         }
2746         return err;
2747 }
2748
2749 static int compat_siocshwtstamp(struct net *net, struct compat_ifreq __user *uifr32)
2750 {
2751         void __user *uptr;
2752         compat_uptr_t uptr32;
2753         struct ifreq __user *uifr;
2754
2755         uifr = compat_alloc_user_space(sizeof (*uifr));
2756         if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
2757                 return -EFAULT;
2758
2759         if (get_user(uptr32, &uifr32->ifr_data))
2760                 return -EFAULT;
2761
2762         uptr = compat_ptr(uptr32);
2763
2764         if (put_user(uptr, &uifr->ifr_data))
2765                 return -EFAULT;
2766
2767         return dev_ioctl(net, SIOCSHWTSTAMP, uifr);
2768 }
2769
2770 struct rtentry32 {
2771         u32             rt_pad1;
2772         struct sockaddr rt_dst;         /* target address               */
2773         struct sockaddr rt_gateway;     /* gateway addr (RTF_GATEWAY)   */
2774         struct sockaddr rt_genmask;     /* target network mask (IP)     */
2775         unsigned short  rt_flags;
2776         short           rt_pad2;
2777         u32             rt_pad3;
2778         unsigned char   rt_tos;
2779         unsigned char   rt_class;
2780         short           rt_pad4;
2781         short           rt_metric;      /* +1 for binary compatibility! */
2782         /* char * */ u32 rt_dev;        /* forcing the device at add    */
2783         u32             rt_mtu;         /* per route MTU/Window         */
2784         u32             rt_window;      /* Window clamping              */
2785         unsigned short  rt_irtt;        /* Initial RTT                  */
2786 };
2787
2788 struct in6_rtmsg32 {
2789         struct in6_addr         rtmsg_dst;
2790         struct in6_addr         rtmsg_src;
2791         struct in6_addr         rtmsg_gateway;
2792         u32                     rtmsg_type;
2793         u16                     rtmsg_dst_len;
2794         u16                     rtmsg_src_len;
2795         u32                     rtmsg_metric;
2796         u32                     rtmsg_info;
2797         u32                     rtmsg_flags;
2798         s32                     rtmsg_ifindex;
2799 };
2800
2801 static int routing_ioctl(struct net *net, struct socket *sock,
2802                          unsigned int cmd, void __user *argp)
2803 {
2804         int ret;
2805         void *r = NULL;
2806         struct in6_rtmsg r6;
2807         struct rtentry r4;
2808         char devname[16];
2809         u32 rtdev;
2810         mm_segment_t old_fs = get_fs();
2811
2812         if (sock && sock->sk && sock->sk->sk_family == AF_INET6) { /* ipv6 */
2813                 struct in6_rtmsg32 __user *ur6 = argp;
2814                 ret = copy_from_user (&r6.rtmsg_dst, &(ur6->rtmsg_dst),
2815                         3 * sizeof(struct in6_addr));
2816                 ret |= __get_user (r6.rtmsg_type, &(ur6->rtmsg_type));
2817                 ret |= __get_user (r6.rtmsg_dst_len, &(ur6->rtmsg_dst_len));
2818                 ret |= __get_user (r6.rtmsg_src_len, &(ur6->rtmsg_src_len));
2819                 ret |= __get_user (r6.rtmsg_metric, &(ur6->rtmsg_metric));
2820                 ret |= __get_user (r6.rtmsg_info, &(ur6->rtmsg_info));
2821                 ret |= __get_user (r6.rtmsg_flags, &(ur6->rtmsg_flags));
2822                 ret |= __get_user (r6.rtmsg_ifindex, &(ur6->rtmsg_ifindex));
2823
2824                 r = (void *) &r6;
2825         } else { /* ipv4 */
2826                 struct rtentry32 __user *ur4 = argp;
2827                 ret = copy_from_user (&r4.rt_dst, &(ur4->rt_dst),
2828                                         3 * sizeof(struct sockaddr));
2829                 ret |= __get_user (r4.rt_flags, &(ur4->rt_flags));
2830                 ret |= __get_user (r4.rt_metric, &(ur4->rt_metric));
2831                 ret |= __get_user (r4.rt_mtu, &(ur4->rt_mtu));
2832                 ret |= __get_user (r4.rt_window, &(ur4->rt_window));
2833                 ret |= __get_user (r4.rt_irtt, &(ur4->rt_irtt));
2834                 ret |= __get_user (rtdev, &(ur4->rt_dev));
2835                 if (rtdev) {
2836                         ret |= copy_from_user (devname, compat_ptr(rtdev), 15);
2837                         r4.rt_dev = devname; devname[15] = 0;
2838                 } else
2839                         r4.rt_dev = NULL;
2840
2841                 r = (void *) &r4;
2842         }
2843
2844         if (ret) {
2845                 ret = -EFAULT;
2846                 goto out;
2847         }
2848
2849         set_fs (KERNEL_DS);
2850         ret = sock_do_ioctl(net, sock, cmd, (unsigned long) r);
2851         set_fs (old_fs);
2852
2853 out:
2854         return ret;
2855 }
2856
2857 /* Since old style bridge ioctl's endup using SIOCDEVPRIVATE
2858  * for some operations; this forces use of the newer bridge-utils that
2859  * use compatiable ioctls
2860  */
2861 static int old_bridge_ioctl(compat_ulong_t __user *argp)
2862 {
2863         compat_ulong_t tmp;
2864
2865         if (get_user(tmp, argp))
2866                 return -EFAULT;
2867         if (tmp == BRCTL_GET_VERSION)
2868                 return BRCTL_VERSION + 1;
2869         return -EINVAL;
2870 }
2871
2872 static int compat_sock_ioctl_trans(struct file *file, struct socket *sock,
2873                          unsigned int cmd, unsigned long arg)
2874 {
2875         void __user *argp = compat_ptr(arg);
2876         struct sock *sk = sock->sk;
2877         struct net *net = sock_net(sk);
2878
2879         if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15))
2880                 return siocdevprivate_ioctl(net, cmd, argp);
2881
2882         switch (cmd) {
2883         case SIOCSIFBR:
2884         case SIOCGIFBR:
2885                 return old_bridge_ioctl(argp);
2886         case SIOCGIFNAME:
2887                 return dev_ifname32(net, argp);
2888         case SIOCGIFCONF:
2889                 return dev_ifconf(net, argp);
2890         case SIOCETHTOOL:
2891                 return ethtool_ioctl(net, argp);
2892         case SIOCWANDEV:
2893                 return compat_siocwandev(net, argp);
2894         case SIOCGIFMAP:
2895         case SIOCSIFMAP:
2896                 return compat_sioc_ifmap(net, cmd, argp);
2897         case SIOCBONDENSLAVE:
2898         case SIOCBONDRELEASE:
2899         case SIOCBONDSETHWADDR:
2900         case SIOCBONDSLAVEINFOQUERY:
2901         case SIOCBONDINFOQUERY:
2902         case SIOCBONDCHANGEACTIVE:
2903                 return bond_ioctl(net, cmd, argp);
2904         case SIOCADDRT:
2905         case SIOCDELRT:
2906                 return routing_ioctl(net, sock, cmd, argp);
2907         case SIOCGSTAMP:
2908                 return do_siocgstamp(net, sock, cmd, argp);
2909         case SIOCGSTAMPNS:
2910                 return do_siocgstampns(net, sock, cmd, argp);
2911         case SIOCSHWTSTAMP:
2912                 return compat_siocshwtstamp(net, argp);
2913
2914         case FIOSETOWN:
2915         case SIOCSPGRP:
2916         case FIOGETOWN:
2917         case SIOCGPGRP:
2918         case SIOCBRADDBR:
2919         case SIOCBRDELBR:
2920         case SIOCGIFVLAN:
2921         case SIOCSIFVLAN:
2922         case SIOCADDDLCI:
2923         case SIOCDELDLCI:
2924                 return sock_ioctl(file, cmd, arg);
2925
2926         case SIOCGIFFLAGS:
2927         case SIOCSIFFLAGS:
2928         case SIOCGIFMETRIC:
2929         case SIOCSIFMETRIC:
2930         case SIOCGIFMTU:
2931         case SIOCSIFMTU:
2932         case SIOCGIFMEM:
2933         case SIOCSIFMEM:
2934         case SIOCGIFHWADDR:
2935         case SIOCSIFHWADDR:
2936         case SIOCADDMULTI:
2937         case SIOCDELMULTI:
2938         case SIOCGIFINDEX:
2939         case SIOCGIFADDR:
2940         case SIOCSIFADDR:
2941         case SIOCSIFHWBROADCAST:
2942         case SIOCDIFADDR:
2943         case SIOCGIFBRDADDR:
2944         case SIOCSIFBRDADDR:
2945         case SIOCGIFDSTADDR:
2946         case SIOCSIFDSTADDR:
2947         case SIOCGIFNETMASK:
2948         case SIOCSIFNETMASK:
2949         case SIOCSIFPFLAGS:
2950         case SIOCGIFPFLAGS:
2951         case SIOCGIFTXQLEN:
2952         case SIOCSIFTXQLEN:
2953         case SIOCBRADDIF:
2954         case SIOCBRDELIF:
2955         case SIOCSIFNAME:
2956         case SIOCGMIIPHY:
2957         case SIOCGMIIREG:
2958         case SIOCSMIIREG:
2959                 return dev_ifsioc(net, sock, cmd, argp);
2960
2961         case SIOCSARP:
2962         case SIOCGARP:
2963         case SIOCDARP:
2964         case SIOCATMARK:
2965                 return sock_do_ioctl(net, sock, cmd, arg);
2966         }
2967
2968         /* Prevent warning from compat_sys_ioctl, these always
2969          * result in -EINVAL in the native case anyway. */
2970         switch (cmd) {
2971         case SIOCRTMSG:
2972         case SIOCGIFCOUNT:
2973         case SIOCSRARP:
2974         case SIOCGRARP:
2975         case SIOCDRARP:
2976         case SIOCSIFLINK:
2977         case SIOCGIFSLAVE:
2978         case SIOCSIFSLAVE:
2979                 return -EINVAL;
2980         }
2981
2982         return -ENOIOCTLCMD;
2983 }
2984
2985 static long compat_sock_ioctl(struct file *file, unsigned cmd,
2986                               unsigned long arg)
2987 {
2988         struct socket *sock = file->private_data;
2989         int ret = -ENOIOCTLCMD;
2990         struct sock *sk;
2991         struct net *net;
2992
2993         sk = sock->sk;
2994         net = sock_net(sk);
2995
2996         if (sock->ops->compat_ioctl)
2997                 ret = sock->ops->compat_ioctl(sock, cmd, arg);
2998
2999         if (ret == -ENOIOCTLCMD &&
3000             (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST))
3001                 ret = compat_wext_handle_ioctl(net, cmd, arg);
3002
3003         if (ret == -ENOIOCTLCMD)
3004                 ret = compat_sock_ioctl_trans(file, sock, cmd, arg);
3005
3006         return ret;
3007 }
3008 #endif
3009
3010 int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen)
3011 {
3012         return sock->ops->bind(sock, addr, addrlen);
3013 }
3014
3015 int kernel_listen(struct socket *sock, int backlog)
3016 {
3017         return sock->ops->listen(sock, backlog);
3018 }
3019
3020 int kernel_accept(struct socket *sock, struct socket **newsock, int flags)
3021 {
3022         struct sock *sk = sock->sk;
3023         int err;
3024
3025         err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
3026                                newsock);
3027         if (err < 0)
3028                 goto done;
3029
3030         err = sock->ops->accept(sock, *newsock, flags);
3031         if (err < 0) {
3032                 sock_release(*newsock);
3033                 *newsock = NULL;
3034                 goto done;
3035         }
3036
3037         (*newsock)->ops = sock->ops;
3038         __module_get((*newsock)->ops->owner);
3039
3040 done:
3041         return err;
3042 }
3043
3044 int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
3045                    int flags)
3046 {
3047         return sock->ops->connect(sock, addr, addrlen, flags);
3048 }
3049
3050 int kernel_getsockname(struct socket *sock, struct sockaddr *addr,
3051                          int *addrlen)
3052 {
3053         return sock->ops->getname(sock, addr, addrlen, 0);
3054 }
3055
3056 int kernel_getpeername(struct socket *sock, struct sockaddr *addr,
3057                          int *addrlen)
3058 {
3059         return sock->ops->getname(sock, addr, addrlen, 1);
3060 }
3061
3062 int kernel_getsockopt(struct socket *sock, int level, int optname,
3063                         char *optval, int *optlen)
3064 {
3065         mm_segment_t oldfs = get_fs();
3066         int err;
3067
3068         set_fs(KERNEL_DS);
3069         if (level == SOL_SOCKET)
3070                 err = sock_getsockopt(sock, level, optname, optval, optlen);
3071         else
3072                 err = sock->ops->getsockopt(sock, level, optname, optval,
3073                                             optlen);
3074         set_fs(oldfs);
3075         return err;
3076 }
3077
3078 int kernel_setsockopt(struct socket *sock, int level, int optname,
3079                         char *optval, unsigned int optlen)
3080 {
3081         mm_segment_t oldfs = get_fs();
3082         int err;
3083
3084         set_fs(KERNEL_DS);
3085         if (level == SOL_SOCKET)
3086                 err = sock_setsockopt(sock, level, optname, optval, optlen);
3087         else
3088                 err = sock->ops->setsockopt(sock, level, optname, optval,
3089                                             optlen);
3090         set_fs(oldfs);
3091         return err;
3092 }
3093
3094 int kernel_sendpage(struct socket *sock, struct page *page, int offset,
3095                     size_t size, int flags)
3096 {
3097         if (sock->ops->sendpage)
3098                 return sock->ops->sendpage(sock, page, offset, size, flags);
3099
3100         return sock_no_sendpage(sock, page, offset, size, flags);
3101 }
3102
3103 int kernel_sock_ioctl(struct socket *sock, int cmd, unsigned long arg)
3104 {
3105         mm_segment_t oldfs = get_fs();
3106         int err;
3107
3108         set_fs(KERNEL_DS);
3109         err = sock->ops->ioctl(sock, cmd, arg);
3110         set_fs(oldfs);
3111
3112         return err;
3113 }
3114
3115 int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how)
3116 {
3117         return sock->ops->shutdown(sock, how);
3118 }
3119
3120 EXPORT_SYMBOL(sock_create);
3121 EXPORT_SYMBOL(sock_create_kern);
3122 EXPORT_SYMBOL(sock_create_lite);
3123 EXPORT_SYMBOL(sock_map_fd);
3124 EXPORT_SYMBOL(sock_recvmsg);
3125 EXPORT_SYMBOL(sock_register);
3126 EXPORT_SYMBOL(sock_release);
3127 EXPORT_SYMBOL(sock_sendmsg);
3128 EXPORT_SYMBOL(sock_unregister);
3129 EXPORT_SYMBOL(sock_wake_async);
3130 EXPORT_SYMBOL(sockfd_lookup);
3131 EXPORT_SYMBOL(kernel_sendmsg);
3132 EXPORT_SYMBOL(kernel_recvmsg);
3133 EXPORT_SYMBOL(kernel_bind);
3134 EXPORT_SYMBOL(kernel_listen);
3135 EXPORT_SYMBOL(kernel_accept);
3136 EXPORT_SYMBOL(kernel_connect);
3137 EXPORT_SYMBOL(kernel_getsockname);
3138 EXPORT_SYMBOL(kernel_getpeername);
3139 EXPORT_SYMBOL(kernel_getsockopt);
3140 EXPORT_SYMBOL(kernel_setsockopt);
3141 EXPORT_SYMBOL(kernel_sendpage);
3142 EXPORT_SYMBOL(kernel_sock_ioctl);
3143 EXPORT_SYMBOL(kernel_sock_shutdown);