~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/socket.c

Version: ~ [ linux-5.9.1 ] ~ [ linux-5.8.16 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.72 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.152 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.202 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.240 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.240 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp