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