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

TOMOYO Linux Cross Reference
Linux/net/socket.c

Version: ~ [ linux-5.19-rc8 ] ~ [ linux-5.18.14 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.57 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.133 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.207 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.253 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.289 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.324 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.302 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

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