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