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

TOMOYO Linux Cross Reference
Linux/net/socket.c

Version: ~ [ linux-5.1-rc6 ] ~ [ linux-5.0.9 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.36 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.113 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.170 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.178 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.138 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.65 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.39.4 ] ~ [ linux-2.6.38.8 ] ~ [ linux-2.6.37.6 ] ~ [ linux-2.6.36.4 ] ~ [ linux-2.6.35.14 ] ~ [ linux-2.6.34.15 ] ~ [ linux-2.6.33.20 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

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