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