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