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