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