Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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