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

TOMOYO Linux Cross Reference
Linux/net/socket.c

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

~ [ 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