blob: 3379c64217a4c1e166f3fc0ef5053c152b2eb80e [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * NET An implementation of the SOCKET network access protocol.
4 *
5 * Version: @(#)socket.c 1.1.93 18/02/95
6 *
7 * Authors: Orest Zborowski, <obz@Kodak.COM>
Jesper Juhl02c30a82005-05-05 16:16:16 -07008 * Ross Biro
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
10 *
11 * Fixes:
12 * Anonymous : NOTSOCK/BADF cleanup. Error fix in
13 * shutdown()
14 * Alan Cox : verify_area() fixes
15 * Alan Cox : Removed DDI
16 * Jonathan Kamens : SOCK_DGRAM reconnect bug
17 * Alan Cox : Moved a load of checks to the very
18 * top level.
19 * Alan Cox : Move address structures to/from user
20 * mode above the protocol layers.
21 * Rob Janssen : Allow 0 length sends.
22 * Alan Cox : Asynchronous I/O support (cribbed from the
23 * tty drivers).
24 * Niibe Yutaka : Asynchronous I/O for writes (4.4BSD style)
25 * Jeff Uphoff : Made max number of sockets command-line
26 * configurable.
27 * Matti Aarnio : Made the number of sockets dynamic,
28 * to be allocated when needed, and mr.
29 * Uphoff's max is used as max to be
30 * allowed to allocate.
31 * Linus : Argh. removed all the socket allocation
32 * altogether: it's in the inode now.
33 * Alan Cox : Made sock_alloc()/sock_release() public
34 * for NetROM and future kernel nfsd type
35 * stuff.
36 * Alan Cox : sendmsg/recvmsg basics.
37 * Tom Dyas : Export net symbols.
38 * Marcin Dalecki : Fixed problems with CONFIG_NET="n".
39 * Alan Cox : Added thread locking to sys_* calls
40 * for sockets. May have errors at the
41 * moment.
42 * Kevin Buhr : Fixed the dumb errors in the above.
43 * Andi Kleen : Some small cleanups, optimizations,
44 * and fixed a copy_from_user() bug.
45 * Tigran Aivazian : sys_send(args) calls sys_sendto(args, NULL, 0)
Stephen Hemminger89bddce2006-09-01 00:19:31 -070046 * Tigran Aivazian : Made listen(2) backlog sanity checks
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 * protocol-independent
48 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 * This module is effectively the top level interface to the BSD socket
Stephen Hemminger89bddce2006-09-01 00:19:31 -070050 * paradigm.
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 *
52 * Based upon Swansea University Computer Society NET3.039
53 */
54
Jakub Kicinskiaef2fed2021-12-15 18:55:37 -080055#include <linux/bpf-cgroup.h>
Jakub Kicinskicc698372020-11-20 14:50:52 -080056#include <linux/ethtool.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <linux/socket.h>
59#include <linux/file.h>
David Howells2dc334f2023-06-07 19:19:09 +010060#include <linux/splice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/net.h>
62#include <linux/interrupt.h>
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -070063#include <linux/thread_info.h>
Stephen Hemminger55737fd2006-09-01 00:23:39 -070064#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <linux/netdevice.h>
66#include <linux/proc_fs.h>
67#include <linux/seq_file.h>
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080068#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#include <linux/if_bridge.h>
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -030070#include <linux/if_vlan.h>
Daniel Borkmann408eccc2014-04-01 16:20:23 +020071#include <linux/ptp_classify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include <linux/init.h>
73#include <linux/poll.h>
74#include <linux/cache.h>
75#include <linux/module.h>
76#include <linux/highmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#include <linux/mount.h>
David Howellsfba9be42019-03-25 16:38:24 +000078#include <linux/pseudo_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#include <linux/security.h>
80#include <linux/syscalls.h>
81#include <linux/compat.h>
82#include <linux/kmod.h>
David Woodhouse3ec3b2f2005-05-17 12:08:48 +010083#include <linux/audit.h>
Adrian Bunkd86b5e02006-01-21 00:46:55 +010084#include <linux/wireless.h>
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -070085#include <linux/nsproxy.h>
Nick Black1fd7317d2009-09-22 16:43:33 -070086#include <linux/magic.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090087#include <linux/slab.h>
Masatake YAMATO600e1772012-08-29 10:44:29 +000088#include <linux/xattr.h>
Jeremy Clinec8e8cd52018-07-27 22:43:01 +000089#include <linux/nospec.h>
Paolo Abeni8c3c4472019-05-03 17:01:39 +020090#include <linux/indirect_call_wrapper.h>
Breno Leitao8e9fad02023-06-27 06:44:24 -070091#include <linux/io_uring.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080093#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070094#include <asm/unistd.h>
95
96#include <net/compat.h>
David S. Miller87de87d2008-06-03 09:14:03 -070097#include <net/wext.h>
Herbert Xuf8451722010-05-24 00:12:34 -070098#include <net/cls_cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100#include <net/sock.h>
101#include <linux/netfilter.h>
102
Arnd Bergmann6b960182009-11-06 23:10:54 -0800103#include <linux/if_tun.h>
104#include <linux/ipv6_route.h>
105#include <linux/route.h>
Arnd Bergmannc7dc5042019-06-03 23:07:12 +0200106#include <linux/termios.h>
Arnd Bergmann6b960182009-11-06 23:10:54 -0800107#include <linux/sockios.h>
Eliezer Tamir076bb0c2013-07-10 17:13:17 +0300108#include <net/busy_poll.h>
Willem de Bruijnf24b9be2014-08-04 22:11:45 -0400109#include <linux/errqueue.h>
Yangbo Lud7c08822021-06-30 16:12:00 +0800110#include <linux/ptp_clock_kernel.h>
Yunhui Cui6e6eda42023-01-11 14:59:30 +0800111#include <trace/events/sock.h>
Eliezer Tamir06021292013-06-10 11:39:50 +0300112
Cong Wange0d10952013-08-01 11:10:25 +0800113#ifdef CONFIG_NET_RX_BUSY_POLL
Eliezer Tamir64b0dc52013-07-10 17:13:36 +0300114unsigned int sysctl_net_busy_read __read_mostly;
115unsigned int sysctl_net_busy_poll __read_mostly;
Eliezer Tamir06021292013-06-10 11:39:50 +0300116#endif
Arnd Bergmann6b960182009-11-06 23:10:54 -0800117
Al Viro8ae5e0302014-11-28 19:40:50 -0500118static ssize_t sock_read_iter(struct kiocb *iocb, struct iov_iter *to);
119static ssize_t sock_write_iter(struct kiocb *iocb, struct iov_iter *from);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700120static int sock_mmap(struct file *file, struct vm_area_struct *vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122static int sock_close(struct inode *inode, struct file *file);
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700123static __poll_t sock_poll(struct file *file,
124 struct poll_table_struct *wait);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700125static long sock_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
Shaun Pereira89bbfc92006-03-21 23:58:08 -0800126#ifdef CONFIG_COMPAT
127static long compat_sock_ioctl(struct file *file,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700128 unsigned int cmd, unsigned long arg);
Shaun Pereira89bbfc92006-03-21 23:58:08 -0800129#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130static int sock_fasync(int fd, struct file *filp, int on);
Jens Axboe9c55e012007-11-06 23:30:13 -0800131static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700132 struct pipe_inode_info *pipe, size_t len,
Jens Axboe9c55e012007-11-06 23:30:13 -0800133 unsigned int flags);
David Howells2bfc6682023-06-07 19:19:10 +0100134static void sock_splice_eof(struct file *file);
Arnd Bergmann542d3062020-01-08 22:44:43 +0100135
136#ifdef CONFIG_PROC_FS
137static void sock_show_fdinfo(struct seq_file *m, struct file *f)
138{
139 struct socket *sock = f->private_data;
Eric Dumazet1ded5e52023-08-08 13:58:09 +0000140 const struct proto_ops *ops = READ_ONCE(sock->ops);
Arnd Bergmann542d3062020-01-08 22:44:43 +0100141
Eric Dumazet1ded5e52023-08-08 13:58:09 +0000142 if (ops->show_fdinfo)
143 ops->show_fdinfo(m, sock);
Arnd Bergmann542d3062020-01-08 22:44:43 +0100144}
145#else
146#define sock_show_fdinfo NULL
147#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149/*
150 * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
151 * in the operation structures but are done directly via the socketcall() multiplexor.
152 */
153
Arjan van de Venda7071d2007-02-12 00:55:36 -0800154static const struct file_operations socket_file_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 .owner = THIS_MODULE,
156 .llseek = no_llseek,
Al Viro8ae5e0302014-11-28 19:40:50 -0500157 .read_iter = sock_read_iter,
158 .write_iter = sock_write_iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 .poll = sock_poll,
160 .unlocked_ioctl = sock_ioctl,
Shaun Pereira89bbfc92006-03-21 23:58:08 -0800161#ifdef CONFIG_COMPAT
162 .compat_ioctl = compat_sock_ioctl,
163#endif
Breno Leitao8e9fad02023-06-27 06:44:24 -0700164 .uring_cmd = io_uring_cmd_sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 .mmap = sock_mmap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 .release = sock_close,
167 .fasync = sock_fasync,
David Howells2dc334f2023-06-07 19:19:09 +0100168 .splice_write = splice_to_socket,
Jens Axboe9c55e012007-11-06 23:30:13 -0800169 .splice_read = sock_splice_read,
David Howells2bfc6682023-06-07 19:19:10 +0100170 .splice_eof = sock_splice_eof,
Kirill Tkhaib4653342019-12-09 13:03:40 +0300171 .show_fdinfo = sock_show_fdinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172};
173
Yejune Dengfe0bdbd2021-06-21 13:12:25 +0800174static const char * const pf_family_names[] = {
175 [PF_UNSPEC] = "PF_UNSPEC",
176 [PF_UNIX] = "PF_UNIX/PF_LOCAL",
177 [PF_INET] = "PF_INET",
178 [PF_AX25] = "PF_AX25",
179 [PF_IPX] = "PF_IPX",
180 [PF_APPLETALK] = "PF_APPLETALK",
181 [PF_NETROM] = "PF_NETROM",
182 [PF_BRIDGE] = "PF_BRIDGE",
183 [PF_ATMPVC] = "PF_ATMPVC",
184 [PF_X25] = "PF_X25",
185 [PF_INET6] = "PF_INET6",
186 [PF_ROSE] = "PF_ROSE",
187 [PF_DECnet] = "PF_DECnet",
188 [PF_NETBEUI] = "PF_NETBEUI",
189 [PF_SECURITY] = "PF_SECURITY",
190 [PF_KEY] = "PF_KEY",
191 [PF_NETLINK] = "PF_NETLINK/PF_ROUTE",
192 [PF_PACKET] = "PF_PACKET",
193 [PF_ASH] = "PF_ASH",
194 [PF_ECONET] = "PF_ECONET",
195 [PF_ATMSVC] = "PF_ATMSVC",
196 [PF_RDS] = "PF_RDS",
197 [PF_SNA] = "PF_SNA",
198 [PF_IRDA] = "PF_IRDA",
199 [PF_PPPOX] = "PF_PPPOX",
200 [PF_WANPIPE] = "PF_WANPIPE",
201 [PF_LLC] = "PF_LLC",
202 [PF_IB] = "PF_IB",
203 [PF_MPLS] = "PF_MPLS",
204 [PF_CAN] = "PF_CAN",
205 [PF_TIPC] = "PF_TIPC",
206 [PF_BLUETOOTH] = "PF_BLUETOOTH",
207 [PF_IUCV] = "PF_IUCV",
208 [PF_RXRPC] = "PF_RXRPC",
209 [PF_ISDN] = "PF_ISDN",
210 [PF_PHONET] = "PF_PHONET",
211 [PF_IEEE802154] = "PF_IEEE802154",
212 [PF_CAIF] = "PF_CAIF",
213 [PF_ALG] = "PF_ALG",
214 [PF_NFC] = "PF_NFC",
215 [PF_VSOCK] = "PF_VSOCK",
216 [PF_KCM] = "PF_KCM",
217 [PF_QIPCRTR] = "PF_QIPCRTR",
218 [PF_SMC] = "PF_SMC",
219 [PF_XDP] = "PF_XDP",
Jeremy Kerrbc49d812021-07-29 10:20:39 +0800220 [PF_MCTP] = "PF_MCTP",
Yejune Dengfe0bdbd2021-06-21 13:12:25 +0800221};
222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223/*
224 * The protocol list. Each protocol is registered in here.
225 */
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227static DEFINE_SPINLOCK(net_family_lock);
Eric Dumazet190683a2010-11-10 10:50:44 +0000228static const struct net_proto_family __rcu *net_families[NPROTO] __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700231 * Support routines.
232 * Move socket addresses back and forth across the kernel/user
233 * divide and look after the messy bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 */
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236/**
237 * move_addr_to_kernel - copy a socket address into kernel space
238 * @uaddr: Address in user space
239 * @kaddr: Address in kernel space
240 * @ulen: Length in user space
241 *
242 * The address is copied into kernel space. If the provided address is
243 * too long an error code of -EINVAL is returned. If the copy gives
244 * invalid addresses -EFAULT is returned. On a success 0 is returned.
245 */
246
Maciej Żenczykowski43db3622012-03-11 12:51:50 +0000247int move_addr_to_kernel(void __user *uaddr, int ulen, struct sockaddr_storage *kaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -0700249 if (ulen < 0 || ulen > sizeof(struct sockaddr_storage))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700251 if (ulen == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 return 0;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700253 if (copy_from_user(kaddr, uaddr, ulen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 return -EFAULT;
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100255 return audit_sockaddr(ulen, kaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
257
258/**
259 * move_addr_to_user - copy an address to user space
260 * @kaddr: kernel space address
261 * @klen: length of address in kernel
262 * @uaddr: user space address
263 * @ulen: pointer to user length field
264 *
265 * The value pointed to by ulen on entry is the buffer length available.
266 * This is overwritten with the buffer space used. -EINVAL is returned
267 * if an overlong buffer is specified or a negative buffer size. -EFAULT
268 * is returned if either the buffer or the length field are not
269 * accessible.
270 * After copying the data up to the limit the user specifies, the true
271 * length of the data is written over the length limit the user
272 * specified. Zero is returned for a success.
273 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700274
Maciej Żenczykowski43db3622012-03-11 12:51:50 +0000275static int move_addr_to_user(struct sockaddr_storage *kaddr, int klen,
stephen hemminger11165f12010-10-18 14:27:29 +0000276 void __user *uaddr, int __user *ulen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
278 int err;
279 int len;
280
Hannes Frederic Sowa68c6beb2013-11-21 03:14:34 +0100281 BUG_ON(klen > sizeof(struct sockaddr_storage));
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700282 err = get_user(len, ulen);
283 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 return err;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700285 if (len > klen)
286 len = klen;
Hannes Frederic Sowa68c6beb2013-11-21 03:14:34 +0100287 if (len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700289 if (len) {
Steve Grubbd6fe3942006-03-30 12:20:22 -0500290 if (audit_sockaddr(klen, kaddr))
291 return -ENOMEM;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700292 if (copy_to_user(uaddr, kaddr, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 return -EFAULT;
294 }
295 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700296 * "fromlen shall refer to the value before truncation.."
297 * 1003.1g
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 */
299 return __put_user(klen, ulen);
300}
301
Alexey Dobriyan08009a72018-02-24 21:20:33 +0300302static struct kmem_cache *sock_inode_cachep __ro_after_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304static struct inode *sock_alloc_inode(struct super_block *sb)
305{
306 struct socket_alloc *ei;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700307
Muchun Songfd60b282022-03-22 14:41:03 -0700308 ei = alloc_inode_sb(sb, sock_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 if (!ei)
310 return NULL;
Al Viro333f7902019-07-05 20:14:16 +0100311 init_waitqueue_head(&ei->socket.wq.wait);
312 ei->socket.wq.fasync_list = NULL;
313 ei->socket.wq.flags = 0;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 ei->socket.state = SS_UNCONNECTED;
316 ei->socket.flags = 0;
317 ei->socket.ops = NULL;
318 ei->socket.sk = NULL;
319 ei->socket.file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321 return &ei->vfs_inode;
322}
323
Al Viro6d7855c2019-07-05 20:13:22 +0100324static void sock_free_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
Eric Dumazet43815482010-04-29 11:01:49 +0000326 struct socket_alloc *ei;
327
328 ei = container_of(inode, struct socket_alloc, vfs_inode);
Eric Dumazet43815482010-04-29 11:01:49 +0000329 kmem_cache_free(sock_inode_cachep, ei);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700332static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700334 struct socket_alloc *ei = (struct socket_alloc *)foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Christoph Lametera35afb82007-05-16 22:10:57 -0700336 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700338
yuan linyu1e911632017-01-07 17:18:31 +0800339static void init_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
341 sock_inode_cachep = kmem_cache_create("sock_inode_cache",
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700342 sizeof(struct socket_alloc),
343 0,
344 (SLAB_HWCACHE_ALIGN |
345 SLAB_RECLAIM_ACCOUNT |
Vladimir Davydov5d097052016-01-14 15:18:21 -0800346 SLAB_MEM_SPREAD | SLAB_ACCOUNT),
Paul Mundt20c2df82007-07-20 10:11:58 +0900347 init_once);
yuan linyu1e911632017-01-07 17:18:31 +0800348 BUG_ON(sock_inode_cachep == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349}
350
Alexey Dobriyanb87221d2009-09-21 17:01:09 -0700351static const struct super_operations sockfs_ops = {
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700352 .alloc_inode = sock_alloc_inode,
Al Viro6d7855c2019-07-05 20:13:22 +0100353 .free_inode = sock_free_inode,
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700354 .statfs = simple_statfs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355};
356
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700357/*
358 * sockfs_dname() is called from d_path().
359 */
360static char *sockfs_dname(struct dentry *dentry, char *buffer, int buflen)
361{
Al Viro0f60d282022-01-30 15:03:49 -0500362 return dynamic_dname(buffer, buflen, "socket:[%lu]",
David Howellsc5ef6032015-03-17 22:26:16 +0000363 d_inode(dentry)->i_ino);
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700364}
365
Al Viro3ba13d12009-02-20 06:02:22 +0000366static const struct dentry_operations sockfs_dentry_operations = {
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700367 .d_dname = sockfs_dname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368};
369
Andreas Gruenbacherbba0bd32016-09-29 17:48:35 +0200370static int sockfs_xattr_get(const struct xattr_handler *handler,
371 struct dentry *dentry, struct inode *inode,
372 const char *suffix, void *value, size_t size)
373{
374 if (value) {
375 if (dentry->d_name.len + 1 > size)
376 return -ERANGE;
377 memcpy(value, dentry->d_name.name, dentry->d_name.len + 1);
378 }
379 return dentry->d_name.len + 1;
380}
381
382#define XATTR_SOCKPROTONAME_SUFFIX "sockprotoname"
383#define XATTR_NAME_SOCKPROTONAME (XATTR_SYSTEM_PREFIX XATTR_SOCKPROTONAME_SUFFIX)
384#define XATTR_NAME_SOCKPROTONAME_LEN (sizeof(XATTR_NAME_SOCKPROTONAME)-1)
385
386static const struct xattr_handler sockfs_xattr_handler = {
387 .name = XATTR_NAME_SOCKPROTONAME,
388 .get = sockfs_xattr_get,
389};
390
Andreas Gruenbacher4a590152016-11-13 21:23:34 +0100391static int sockfs_security_xattr_set(const struct xattr_handler *handler,
Christian Brauner39f60c12023-01-13 12:49:23 +0100392 struct mnt_idmap *idmap,
Andreas Gruenbacher4a590152016-11-13 21:23:34 +0100393 struct dentry *dentry, struct inode *inode,
394 const char *suffix, const void *value,
395 size_t size, int flags)
396{
397 /* Handled by LSM. */
398 return -EAGAIN;
399}
400
401static const struct xattr_handler sockfs_security_xattr_handler = {
402 .prefix = XATTR_SECURITY_PREFIX,
403 .set = sockfs_security_xattr_set,
404};
405
Wedson Almeida Filho295d3c42023-09-30 02:00:33 -0300406static const struct xattr_handler * const sockfs_xattr_handlers[] = {
Andreas Gruenbacherbba0bd32016-09-29 17:48:35 +0200407 &sockfs_xattr_handler,
Andreas Gruenbacher4a590152016-11-13 21:23:34 +0100408 &sockfs_security_xattr_handler,
Andreas Gruenbacherbba0bd32016-09-29 17:48:35 +0200409 NULL
410};
411
David Howellsfba9be42019-03-25 16:38:24 +0000412static int sockfs_init_fs_context(struct fs_context *fc)
Al Viroc74a1cb2011-01-12 16:59:34 -0500413{
David Howellsfba9be42019-03-25 16:38:24 +0000414 struct pseudo_fs_context *ctx = init_pseudo(fc, SOCKFS_MAGIC);
415 if (!ctx)
416 return -ENOMEM;
417 ctx->ops = &sockfs_ops;
418 ctx->dops = &sockfs_dentry_operations;
419 ctx->xattr = sockfs_xattr_handlers;
420 return 0;
Al Viroc74a1cb2011-01-12 16:59:34 -0500421}
422
423static struct vfsmount *sock_mnt __read_mostly;
424
425static struct file_system_type sock_fs_type = {
426 .name = "sockfs",
David Howellsfba9be42019-03-25 16:38:24 +0000427 .init_fs_context = sockfs_init_fs_context,
Al Viroc74a1cb2011-01-12 16:59:34 -0500428 .kill_sb = kill_anon_super,
429};
430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431/*
432 * Obtains the first available file descriptor and sets it up for use.
433 *
David S. Miller39d8c1b2006-03-20 17:13:49 -0800434 * These functions create file structures and maps them to fd space
435 * of the current process. On success it returns file descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 * and file struct implicitly stored in sock->file.
437 * Note that another thread may close file descriptor before we return
438 * from this function. We use the fact that now we do not refer
439 * to socket after mapping. If one day we will need it, this
440 * function will increment ref. count on file by 1.
441 *
442 * In any case returned fd MAY BE not valid!
443 * This race condition is unavoidable
444 * with shared fd spaces, we cannot solve it inside kernel,
445 * but we take care of internal coherence yet.
446 */
447
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300448/**
449 * sock_alloc_file - Bind a &socket to a &file
450 * @sock: socket
451 * @flags: file status flags
452 * @dname: protocol name
453 *
454 * Returns the &file bound with @sock, implicitly storing it
455 * in sock->file. If dname is %NULL, sets to "".
Thadeu Lima de Souza Cascardo649c15c2023-03-07 14:37:07 -0300456 *
457 * On failure @sock is released, and an ERR pointer is returned.
458 *
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300459 * This function uses GFP_KERNEL internally.
460 */
461
Linus Torvaldsaab174f2012-10-02 20:25:04 -0700462struct file *sock_alloc_file(struct socket *sock, int flags, const char *dname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
Al Viro7cbe66b2009-08-05 19:59:08 +0400464 struct file *file;
David S. Miller39d8c1b2006-03-20 17:13:49 -0800465
Al Virod93aa9d2018-06-09 09:40:05 -0400466 if (!dname)
467 dname = sock->sk ? sock->sk->sk_prot_creator->name : "";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Al Virod93aa9d2018-06-09 09:40:05 -0400469 file = alloc_file_pseudo(SOCK_INODE(sock), sock_mnt, dname,
470 O_RDWR | (flags & O_NONBLOCK),
471 &socket_file_ops);
Viresh Kumarb5ffe632015-08-12 15:59:47 +0530472 if (IS_ERR(file)) {
Al Viro8e1611e2017-12-05 23:29:09 +0000473 sock_release(sock);
Anatol Pomozov39b652522012-09-12 20:11:55 -0700474 return file;
Al Virocc3808f2009-08-06 09:43:59 +0400475 }
David S. Miller39d8c1b2006-03-20 17:13:49 -0800476
Jens Axboefe34db02023-05-09 09:19:08 -0600477 file->f_mode |= FMODE_NOWAIT;
David S. Miller39d8c1b2006-03-20 17:13:49 -0800478 sock->file = file;
David S. Miller39d8c1b2006-03-20 17:13:49 -0800479 file->private_data = sock;
Linus Torvaldsd8e464e2019-11-17 11:20:48 -0800480 stream_open(SOCK_INODE(sock), file);
Al Viro28407632012-08-17 23:54:15 -0400481 return file;
David S. Miller39d8c1b2006-03-20 17:13:49 -0800482}
Al Viro56b31d12012-08-18 00:25:51 -0400483EXPORT_SYMBOL(sock_alloc_file);
David S. Miller39d8c1b2006-03-20 17:13:49 -0800484
Al Viro56b31d12012-08-18 00:25:51 -0400485static int sock_map_fd(struct socket *sock, int flags)
David S. Miller39d8c1b2006-03-20 17:13:49 -0800486{
487 struct file *newfile;
Al Viro28407632012-08-17 23:54:15 -0400488 int fd = get_unused_fd_flags(flags);
Al Viroce4bb042018-01-10 18:47:05 -0500489 if (unlikely(fd < 0)) {
490 sock_release(sock);
Al Viro28407632012-08-17 23:54:15 -0400491 return fd;
Al Viroce4bb042018-01-10 18:47:05 -0500492 }
David S. Miller39d8c1b2006-03-20 17:13:49 -0800493
Linus Torvaldsaab174f2012-10-02 20:25:04 -0700494 newfile = sock_alloc_file(sock, flags, NULL);
Enrico Weigelt4546e442019-06-05 22:58:50 +0200495 if (!IS_ERR(newfile)) {
David S. Miller39d8c1b2006-03-20 17:13:49 -0800496 fd_install(fd, newfile);
Al Viro28407632012-08-17 23:54:15 -0400497 return fd;
498 }
Al Viro7cbe66b2009-08-05 19:59:08 +0400499
Al Viro28407632012-08-17 23:54:15 -0400500 put_unused_fd(fd);
501 return PTR_ERR(newfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502}
503
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300504/**
505 * sock_from_file - Return the &socket bounded to @file.
506 * @file: file
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300507 *
Florent Revestdba4a922020-12-04 12:36:04 +0100508 * On failure returns %NULL.
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300509 */
510
Florent Revestdba4a922020-12-04 12:36:04 +0100511struct socket *sock_from_file(struct file *file)
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800512{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800513 if (file->f_op == &socket_file_ops)
Jens Axboeda214a42022-04-12 14:22:39 -0600514 return file->private_data; /* set in sock_alloc_file */
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800515
Eric Dumazet23bb80d2007-02-08 14:59:57 -0800516 return NULL;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800517}
John Fastabend406a3c62012-07-20 10:39:25 +0000518EXPORT_SYMBOL(sock_from_file);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520/**
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700521 * sockfd_lookup - Go from a file number to its socket slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 * @fd: file handle
523 * @err: pointer to an error code return
524 *
525 * The file handle passed in is locked and the socket it is bound
Rosen, Rami241c4662017-05-21 22:12:38 +0300526 * to is returned. If an error occurs the err pointer is overwritten
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 * with a negative errno code and NULL is returned. The function checks
528 * for both invalid handles and passing a handle which is not a socket.
529 *
530 * On a success the socket object pointer is returned.
531 */
532
533struct socket *sockfd_lookup(int fd, int *err)
534{
535 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 struct socket *sock;
537
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700538 file = fget(fd);
539 if (!file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 *err = -EBADF;
541 return NULL;
542 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700543
Florent Revestdba4a922020-12-04 12:36:04 +0100544 sock = sock_from_file(file);
545 if (!sock) {
546 *err = -ENOTSOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 fput(file);
Florent Revestdba4a922020-12-04 12:36:04 +0100548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 return sock;
550}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700551EXPORT_SYMBOL(sockfd_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800553static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed)
554{
Al Viro00e188e2014-03-03 23:48:18 -0500555 struct fd f = fdget(fd);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800556 struct socket *sock;
557
Hua Zhong36725582006-04-19 15:25:02 -0700558 *err = -EBADF;
Al Viro00e188e2014-03-03 23:48:18 -0500559 if (f.file) {
Florent Revestdba4a922020-12-04 12:36:04 +0100560 sock = sock_from_file(f.file);
Al Viro00e188e2014-03-03 23:48:18 -0500561 if (likely(sock)) {
Miaohe Lince787a52020-08-06 19:53:16 +0800562 *fput_needed = f.flags & FDPUT_FPUT;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800563 return sock;
Al Viro00e188e2014-03-03 23:48:18 -0500564 }
Florent Revestdba4a922020-12-04 12:36:04 +0100565 *err = -ENOTSOCK;
Al Viro00e188e2014-03-03 23:48:18 -0500566 fdput(f);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800567 }
568 return NULL;
569}
570
Masatake YAMATO600e1772012-08-29 10:44:29 +0000571static ssize_t sockfs_listxattr(struct dentry *dentry, char *buffer,
572 size_t size)
573{
574 ssize_t len;
575 ssize_t used = 0;
576
David Howellsc5ef6032015-03-17 22:26:16 +0000577 len = security_inode_listsecurity(d_inode(dentry), buffer, size);
Masatake YAMATO600e1772012-08-29 10:44:29 +0000578 if (len < 0)
579 return len;
580 used += len;
581 if (buffer) {
582 if (size < used)
583 return -ERANGE;
584 buffer += len;
585 }
586
587 len = (XATTR_NAME_SOCKPROTONAME_LEN + 1);
588 used += len;
589 if (buffer) {
590 if (size < used)
591 return -ERANGE;
592 memcpy(buffer, XATTR_NAME_SOCKPROTONAME, len);
593 buffer += len;
594 }
595
596 return used;
597}
598
Christian Braunerc1632a02023-01-13 12:49:11 +0100599static int sockfs_setattr(struct mnt_idmap *idmap,
Christian Brauner549c7292021-01-21 14:19:43 +0100600 struct dentry *dentry, struct iattr *iattr)
Lorenzo Colitti86741ec2016-11-04 02:23:41 +0900601{
Christian Braunerc1632a02023-01-13 12:49:11 +0100602 int err = simple_setattr(&nop_mnt_idmap, dentry, iattr);
Lorenzo Colitti86741ec2016-11-04 02:23:41 +0900603
Eric Biggerse1a3a602016-12-30 17:42:32 -0600604 if (!err && (iattr->ia_valid & ATTR_UID)) {
Lorenzo Colitti86741ec2016-11-04 02:23:41 +0900605 struct socket *sock = SOCKET_I(d_inode(dentry));
606
Cong Wang6d8c50d2018-06-07 13:39:49 -0700607 if (sock->sk)
608 sock->sk->sk_uid = iattr->ia_uid;
609 else
610 err = -ENOENT;
Lorenzo Colitti86741ec2016-11-04 02:23:41 +0900611 }
612
613 return err;
614}
615
Masatake YAMATO600e1772012-08-29 10:44:29 +0000616static const struct inode_operations sockfs_inode_ops = {
Masatake YAMATO600e1772012-08-29 10:44:29 +0000617 .listxattr = sockfs_listxattr,
Lorenzo Colitti86741ec2016-11-04 02:23:41 +0900618 .setattr = sockfs_setattr,
Masatake YAMATO600e1772012-08-29 10:44:29 +0000619};
620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621/**
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300622 * sock_alloc - allocate a socket
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700623 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 * Allocate a new inode and socket object. The two are bound together
625 * and initialised. The socket is then returned. If we are out of inodes
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300626 * NULL is returned. This functions uses GFP_KERNEL internally.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 */
628
Tom Herbertf4a00aa2016-03-07 14:11:01 -0800629struct socket *sock_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700631 struct inode *inode;
632 struct socket *sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Eric Dumazeta209dfc2011-07-26 11:36:34 +0200634 inode = new_inode_pseudo(sock_mnt->mnt_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 if (!inode)
636 return NULL;
637
638 sock = SOCKET_I(inode);
639
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400640 inode->i_ino = get_next_ino();
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700641 inode->i_mode = S_IFSOCK | S_IRWXUGO;
David Howells8192b0c2008-11-14 10:39:10 +1100642 inode->i_uid = current_fsuid();
643 inode->i_gid = current_fsgid();
Masatake YAMATO600e1772012-08-29 10:44:29 +0000644 inode->i_op = &sockfs_inode_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 return sock;
647}
Tom Herbertf4a00aa2016-03-07 14:11:01 -0800648EXPORT_SYMBOL(sock_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Cong Wang6d8c50d2018-06-07 13:39:49 -0700650static void __sock_release(struct socket *sock, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
Eric Dumazet1ded5e52023-08-08 13:58:09 +0000652 const struct proto_ops *ops = READ_ONCE(sock->ops);
653
654 if (ops) {
655 struct module *owner = ops->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Cong Wang6d8c50d2018-06-07 13:39:49 -0700657 if (inode)
658 inode_lock(inode);
Eric Dumazet1ded5e52023-08-08 13:58:09 +0000659 ops->release(sock);
Eric Biggersff7b11a2019-02-21 14:13:56 -0800660 sock->sk = NULL;
Cong Wang6d8c50d2018-06-07 13:39:49 -0700661 if (inode)
662 inode_unlock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 sock->ops = NULL;
664 module_put(owner);
665 }
666
Al Viro333f7902019-07-05 20:14:16 +0100667 if (sock->wq.fasync_list)
Yang Yingliang3410f222014-02-12 17:09:55 +0800668 pr_err("%s: fasync list not empty!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 if (!sock->file) {
671 iput(SOCK_INODE(sock));
672 return;
673 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700674 sock->file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675}
Cong Wang6d8c50d2018-06-07 13:39:49 -0700676
Andrew Lunn9a8ad9a2020-07-13 01:15:12 +0200677/**
678 * sock_release - close a socket
679 * @sock: socket to close
680 *
681 * The socket is released from the protocol stack if it has a release
682 * callback, and the inode is then released if the socket is bound to
683 * an inode not a file.
684 */
Cong Wang6d8c50d2018-06-07 13:39:49 -0700685void sock_release(struct socket *sock)
686{
687 __sock_release(sock, NULL);
688}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700689EXPORT_SYMBOL(sock_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -0400691void __sock_tx_timestamp(__u16 tsflags, __u8 *tx_flags)
Patrick Ohly20d49472009-02-12 05:03:38 +0000692{
Eric Dumazet140c55d2014-08-06 11:49:29 +0200693 u8 flags = *tx_flags;
694
Gerhard Engleder51eb7492022-05-06 22:01:38 +0200695 if (tsflags & SOF_TIMESTAMPING_TX_HARDWARE) {
Eric Dumazet140c55d2014-08-06 11:49:29 +0200696 flags |= SKBTX_HW_TSTAMP;
697
Gerhard Engleder51eb7492022-05-06 22:01:38 +0200698 /* PTP hardware clocks can provide a free running cycle counter
699 * as a time base for virtual clocks. Tell driver to use the
700 * free running cycle counter for timestamp if socket is bound
701 * to virtual clock.
702 */
703 if (tsflags & SOF_TIMESTAMPING_BIND_PHC)
704 flags |= SKBTX_HW_TSTAMP_USE_CYCLES;
705 }
706
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -0400707 if (tsflags & SOF_TIMESTAMPING_TX_SOFTWARE)
Eric Dumazet140c55d2014-08-06 11:49:29 +0200708 flags |= SKBTX_SW_TSTAMP;
709
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -0400710 if (tsflags & SOF_TIMESTAMPING_TX_SCHED)
Eric Dumazet140c55d2014-08-06 11:49:29 +0200711 flags |= SKBTX_SCHED_TSTAMP;
712
Eric Dumazet140c55d2014-08-06 11:49:29 +0200713 *tx_flags = flags;
Patrick Ohly20d49472009-02-12 05:03:38 +0000714}
Willem de Bruijn67cc0d42014-09-08 19:58:58 -0400715EXPORT_SYMBOL(__sock_tx_timestamp);
Patrick Ohly20d49472009-02-12 05:03:38 +0000716
Paolo Abeni8c3c4472019-05-03 17:01:39 +0200717INDIRECT_CALLABLE_DECLARE(int inet_sendmsg(struct socket *, struct msghdr *,
718 size_t));
Paolo Abenia648a592019-07-03 16:06:54 +0200719INDIRECT_CALLABLE_DECLARE(int inet6_sendmsg(struct socket *, struct msghdr *,
720 size_t));
Yunhui Cui6e6eda42023-01-11 14:59:30 +0800721
722static noinline void call_trace_sock_send_length(struct sock *sk, int ret,
723 int flags)
724{
725 trace_sock_send_length(sk, ret, 0);
726}
727
Al Virod8725c82014-12-11 00:02:50 -0500728static inline int sock_sendmsg_nosec(struct socket *sock, struct msghdr *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729{
Eric Dumazet1ded5e52023-08-08 13:58:09 +0000730 int ret = INDIRECT_CALL_INET(READ_ONCE(sock->ops)->sendmsg, inet6_sendmsg,
Paolo Abenia648a592019-07-03 16:06:54 +0200731 inet_sendmsg, sock, msg,
732 msg_data_left(msg));
Al Virod8725c82014-12-11 00:02:50 -0500733 BUG_ON(ret == -EIOCBQUEUED);
Yunhui Cui6e6eda42023-01-11 14:59:30 +0800734
735 if (trace_sock_send_length_enabled())
736 call_trace_sock_send_length(sock->sk, ret, 0);
Al Virod8725c82014-12-11 00:02:50 -0500737 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738}
Gu Zheng0cf00c62014-12-05 15:14:23 +0800739
Jordan Rife86a7e0b2023-09-21 18:46:41 -0500740static int __sock_sendmsg(struct socket *sock, struct msghdr *msg)
741{
742 int err = security_socket_sendmsg(sock, msg,
743 msg_data_left(msg));
744
745 return err ?: sock_sendmsg_nosec(sock, msg);
746}
747
Randy Dunlap85806af2019-05-18 21:23:07 -0700748/**
749 * sock_sendmsg - send a message through @sock
750 * @sock: socket
751 * @msg: message to send
752 *
753 * Sends @msg through @sock, passing through LSM.
754 * Returns the number of bytes sent, or an error code.
755 */
Al Virod8725c82014-12-11 00:02:50 -0500756int sock_sendmsg(struct socket *sock, struct msghdr *msg)
Gu Zheng0cf00c62014-12-05 15:14:23 +0800757{
Jordan Rife86a7e0b2023-09-21 18:46:41 -0500758 struct sockaddr_storage *save_addr = (struct sockaddr_storage *)msg->msg_name;
759 struct sockaddr_storage address;
760 int ret;
Ying Xue1b784142015-03-02 15:37:48 +0800761
Jordan Rife86a7e0b2023-09-21 18:46:41 -0500762 if (msg->msg_name) {
763 memcpy(&address, msg->msg_name, msg->msg_namelen);
764 msg->msg_name = &address;
765 }
766
767 ret = __sock_sendmsg(sock, msg);
768 msg->msg_name = save_addr;
769
770 return ret;
Gu Zheng0cf00c62014-12-05 15:14:23 +0800771}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700772EXPORT_SYMBOL(sock_sendmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300774/**
775 * kernel_sendmsg - send a message through @sock (kernel-space)
776 * @sock: socket
777 * @msg: message header
778 * @vec: kernel vec
779 * @num: vec array length
780 * @size: total message data size
781 *
782 * Builds the message data with @vec and sends it through @sock.
783 * Returns the number of bytes sent, or an error code.
784 */
785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
787 struct kvec *vec, size_t num, size_t size)
788{
Al Virode4eda92022-09-15 20:25:47 -0400789 iov_iter_kvec(&msg->msg_iter, ITER_SOURCE, vec, num, size);
Al Virod8725c82014-12-11 00:02:50 -0500790 return sock_sendmsg(sock, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700792EXPORT_SYMBOL(kernel_sendmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300794/**
795 * kernel_sendmsg_locked - send a message through @sock (kernel-space)
796 * @sk: sock
797 * @msg: message header
798 * @vec: output s/g array
799 * @num: output s/g array length
800 * @size: total message data size
801 *
802 * Builds the message data with @vec and sends it through @sock.
803 * Returns the number of bytes sent, or an error code.
804 * Caller must hold @sk.
805 */
806
Tom Herbert306b13e2017-07-28 16:22:41 -0700807int kernel_sendmsg_locked(struct sock *sk, struct msghdr *msg,
808 struct kvec *vec, size_t num, size_t size)
809{
810 struct socket *sock = sk->sk_socket;
Eric Dumazet1ded5e52023-08-08 13:58:09 +0000811 const struct proto_ops *ops = READ_ONCE(sock->ops);
Tom Herbert306b13e2017-07-28 16:22:41 -0700812
Eric Dumazet1ded5e52023-08-08 13:58:09 +0000813 if (!ops->sendmsg_locked)
John Fastabenddb5980d2017-08-15 22:31:34 -0700814 return sock_no_sendmsg_locked(sk, msg, size);
Tom Herbert306b13e2017-07-28 16:22:41 -0700815
Al Virode4eda92022-09-15 20:25:47 -0400816 iov_iter_kvec(&msg->msg_iter, ITER_SOURCE, vec, num, size);
Tom Herbert306b13e2017-07-28 16:22:41 -0700817
Eric Dumazet1ded5e52023-08-08 13:58:09 +0000818 return ops->sendmsg_locked(sk, msg, msg_data_left(msg));
Tom Herbert306b13e2017-07-28 16:22:41 -0700819}
820EXPORT_SYMBOL(kernel_sendmsg_locked);
821
Soheil Hassas Yeganeh8605330a2017-03-18 17:02:59 -0400822static bool skb_is_err_queue(const struct sk_buff *skb)
823{
824 /* pkt_type of skbs enqueued on the error queue are set to
825 * PACKET_OUTGOING in skb_set_err_queue(). This is only safe to do
826 * in recvmsg, since skbs received on a local socket will never
827 * have a pkt_type of PACKET_OUTGOING.
828 */
829 return skb->pkt_type == PACKET_OUTGOING;
830}
831
Miroslav Lichvarb50a5c72017-05-19 17:52:40 +0200832/* On transmit, software and hardware timestamps are returned independently.
833 * As the two skb clones share the hardware timestamp, which may be updated
834 * before the software timestamp is received, a hardware TX timestamp may be
835 * returned only if there is no software TX timestamp. Ignore false software
836 * timestamps, which may be made in the __sock_recv_timestamp() call when the
Deepa Dinamani7f1bc6e2019-02-02 07:34:46 -0800837 * option SO_TIMESTAMP_OLD(NS) is enabled on the socket, even when the skb has a
Miroslav Lichvarb50a5c72017-05-19 17:52:40 +0200838 * hardware timestamp.
839 */
840static bool skb_is_swtx_tstamp(const struct sk_buff *skb, int false_tstamp)
841{
842 return skb->tstamp && !false_tstamp && skb_is_err_queue(skb);
843}
844
Gerhard Engleder97dc7cd2022-05-06 22:01:40 +0200845static ktime_t get_timestamp(struct sock *sk, struct sk_buff *skb, int *if_index)
846{
Eric Dumazete3390b32023-08-31 13:52:11 +0000847 bool cycles = READ_ONCE(sk->sk_tsflags) & SOF_TIMESTAMPING_BIND_PHC;
Gerhard Engleder97dc7cd2022-05-06 22:01:40 +0200848 struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
849 struct net_device *orig_dev;
850 ktime_t hwtstamp;
851
852 rcu_read_lock();
853 orig_dev = dev_get_by_napi_id(skb_napi_id(skb));
854 if (orig_dev) {
855 *if_index = orig_dev->ifindex;
856 hwtstamp = netdev_get_tstamp(orig_dev, shhwtstamps, cycles);
857 } else {
858 hwtstamp = shhwtstamps->hwtstamp;
859 }
860 rcu_read_unlock();
861
862 return hwtstamp;
863}
864
865static void put_ts_pktinfo(struct msghdr *msg, struct sk_buff *skb,
866 int if_index)
Miroslav Lichvaraad9c8c2017-05-19 17:52:38 +0200867{
868 struct scm_ts_pktinfo ts_pktinfo;
869 struct net_device *orig_dev;
870
871 if (!skb_mac_header_was_set(skb))
872 return;
873
874 memset(&ts_pktinfo, 0, sizeof(ts_pktinfo));
875
Gerhard Engleder97dc7cd2022-05-06 22:01:40 +0200876 if (!if_index) {
877 rcu_read_lock();
878 orig_dev = dev_get_by_napi_id(skb_napi_id(skb));
879 if (orig_dev)
880 if_index = orig_dev->ifindex;
881 rcu_read_unlock();
882 }
883 ts_pktinfo.if_index = if_index;
Miroslav Lichvaraad9c8c2017-05-19 17:52:38 +0200884
885 ts_pktinfo.pkt_length = skb->len - skb_mac_offset(skb);
886 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPING_PKTINFO,
887 sizeof(ts_pktinfo), &ts_pktinfo);
888}
889
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700890/*
891 * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
892 */
893void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
894 struct sk_buff *skb)
895{
Patrick Ohly20d49472009-02-12 05:03:38 +0000896 int need_software_tstamp = sock_flag(sk, SOCK_RCVTSTAMP);
Deepa Dinamani887feae2019-02-02 07:34:50 -0800897 int new_tstamp = sock_flag(sk, SOCK_TSTAMP_NEW);
Deepa Dinamani97184752019-02-02 07:34:51 -0800898 struct scm_timestamping_internal tss;
Miroslav Lichvarb50a5c72017-05-19 17:52:40 +0200899 int empty = 1, false_tstamp = 0;
Patrick Ohly20d49472009-02-12 05:03:38 +0000900 struct skb_shared_hwtstamps *shhwtstamps =
901 skb_hwtstamps(skb);
Gerhard Engleder97dc7cd2022-05-06 22:01:40 +0200902 int if_index;
Miroslav Lichvar007747a92022-01-05 11:33:26 +0100903 ktime_t hwtstamp;
Eric Dumazete3390b32023-08-31 13:52:11 +0000904 u32 tsflags;
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700905
Patrick Ohly20d49472009-02-12 05:03:38 +0000906 /* Race occurred between timestamp enabling and packet
907 receiving. Fill in the current time for now. */
Miroslav Lichvarb50a5c72017-05-19 17:52:40 +0200908 if (need_software_tstamp && skb->tstamp == 0) {
Patrick Ohly20d49472009-02-12 05:03:38 +0000909 __net_timestamp(skb);
Miroslav Lichvarb50a5c72017-05-19 17:52:40 +0200910 false_tstamp = 1;
911 }
Patrick Ohly20d49472009-02-12 05:03:38 +0000912
913 if (need_software_tstamp) {
914 if (!sock_flag(sk, SOCK_RCVTSTAMPNS)) {
Deepa Dinamani887feae2019-02-02 07:34:50 -0800915 if (new_tstamp) {
916 struct __kernel_sock_timeval tv;
917
918 skb_get_new_timestamp(skb, &tv);
919 put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMP_NEW,
920 sizeof(tv), &tv);
921 } else {
922 struct __kernel_old_timeval tv;
923
924 skb_get_timestamp(skb, &tv);
925 put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMP_OLD,
926 sizeof(tv), &tv);
927 }
Patrick Ohly20d49472009-02-12 05:03:38 +0000928 } else {
Deepa Dinamani887feae2019-02-02 07:34:50 -0800929 if (new_tstamp) {
930 struct __kernel_timespec ts;
931
932 skb_get_new_timestampns(skb, &ts);
933 put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMPNS_NEW,
934 sizeof(ts), &ts);
935 } else {
Arnd Bergmanndf1b4ba2019-10-25 22:04:46 +0200936 struct __kernel_old_timespec ts;
Deepa Dinamani887feae2019-02-02 07:34:50 -0800937
938 skb_get_timestampns(skb, &ts);
939 put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMPNS_OLD,
940 sizeof(ts), &ts);
941 }
Patrick Ohly20d49472009-02-12 05:03:38 +0000942 }
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700943 }
Patrick Ohly20d49472009-02-12 05:03:38 +0000944
Willem de Bruijnf24b9be2014-08-04 22:11:45 -0400945 memset(&tss, 0, sizeof(tss));
Eric Dumazete3390b32023-08-31 13:52:11 +0000946 tsflags = READ_ONCE(sk->sk_tsflags);
947 if ((tsflags & SOF_TIMESTAMPING_SOFTWARE) &&
Deepa Dinamani97184752019-02-02 07:34:51 -0800948 ktime_to_timespec64_cond(skb->tstamp, tss.ts + 0))
Patrick Ohly20d49472009-02-12 05:03:38 +0000949 empty = 0;
Willem de Bruijn4d276eb2014-07-25 18:01:32 -0400950 if (shhwtstamps &&
Eric Dumazete3390b32023-08-31 13:52:11 +0000951 (tsflags & SOF_TIMESTAMPING_RAW_HARDWARE) &&
Yangbo Lud7c08822021-06-30 16:12:00 +0800952 !skb_is_swtx_tstamp(skb, false_tstamp)) {
Gerhard Engleder97dc7cd2022-05-06 22:01:40 +0200953 if_index = 0;
954 if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP_NETDEV)
955 hwtstamp = get_timestamp(sk, skb, &if_index);
Miroslav Lichvar007747a92022-01-05 11:33:26 +0100956 else
957 hwtstamp = shhwtstamps->hwtstamp;
Yangbo Lud7c08822021-06-30 16:12:00 +0800958
Eric Dumazete3390b32023-08-31 13:52:11 +0000959 if (tsflags & SOF_TIMESTAMPING_BIND_PHC)
Gerhard Engleder97dc7cd2022-05-06 22:01:40 +0200960 hwtstamp = ptp_convert_timestamp(&hwtstamp,
Eric Dumazet251cd402023-08-31 13:52:12 +0000961 READ_ONCE(sk->sk_bind_phc));
Gerhard Engleder97dc7cd2022-05-06 22:01:40 +0200962
Miroslav Lichvar007747a92022-01-05 11:33:26 +0100963 if (ktime_to_timespec64_cond(hwtstamp, tss.ts + 2)) {
Yangbo Lud7c08822021-06-30 16:12:00 +0800964 empty = 0;
965
Eric Dumazete3390b32023-08-31 13:52:11 +0000966 if ((tsflags & SOF_TIMESTAMPING_OPT_PKTINFO) &&
Yangbo Lud7c08822021-06-30 16:12:00 +0800967 !skb_is_err_queue(skb))
Gerhard Engleder97dc7cd2022-05-06 22:01:40 +0200968 put_ts_pktinfo(msg, skb, if_index);
Yangbo Lud7c08822021-06-30 16:12:00 +0800969 }
Miroslav Lichvaraad9c8c2017-05-19 17:52:38 +0200970 }
Francis Yan1c885802016-11-27 23:07:18 -0800971 if (!empty) {
Deepa Dinamani97184752019-02-02 07:34:51 -0800972 if (sock_flag(sk, SOCK_TSTAMP_NEW))
973 put_cmsg_scm_timestamping64(msg, &tss);
974 else
975 put_cmsg_scm_timestamping(msg, &tss);
Francis Yan1c885802016-11-27 23:07:18 -0800976
Soheil Hassas Yeganeh8605330a2017-03-18 17:02:59 -0400977 if (skb_is_err_queue(skb) && skb->len &&
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -0400978 SKB_EXT_ERR(skb)->opt_stats)
Francis Yan1c885802016-11-27 23:07:18 -0800979 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPING_OPT_STATS,
980 skb->len, skb->data);
981 }
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700982}
Arnaldo Carvalho de Melo7c81fd82007-03-10 00:39:35 -0300983EXPORT_SYMBOL_GPL(__sock_recv_timestamp);
984
Jakub Kicinskieb6fba72023-04-17 08:53:46 -0700985#ifdef CONFIG_WIRELESS
Johannes Berg6e3e9392011-11-09 10:15:42 +0100986void __sock_recv_wifi_status(struct msghdr *msg, struct sock *sk,
987 struct sk_buff *skb)
988{
989 int ack;
990
991 if (!sock_flag(sk, SOCK_WIFI_STATUS))
992 return;
993 if (!skb->wifi_acked_valid)
994 return;
995
996 ack = skb->wifi_acked;
997
998 put_cmsg(msg, SOL_SOCKET, SCM_WIFI_STATUS, sizeof(ack), &ack);
999}
1000EXPORT_SYMBOL_GPL(__sock_recv_wifi_status);
Jakub Kicinskieb6fba72023-04-17 08:53:46 -07001001#endif
Johannes Berg6e3e9392011-11-09 10:15:42 +01001002
stephen hemminger11165f12010-10-18 14:27:29 +00001003static inline void sock_recv_drops(struct msghdr *msg, struct sock *sk,
1004 struct sk_buff *skb)
Neil Horman3b885782009-10-12 13:26:31 -07001005{
Eyal Birger744d5a32015-03-01 14:58:31 +02001006 if (sock_flag(sk, SOCK_RXQ_OVFL) && skb && SOCK_SKB_CB(skb)->dropcount)
Neil Horman3b885782009-10-12 13:26:31 -07001007 put_cmsg(msg, SOL_SOCKET, SO_RXQ_OVFL,
Eyal Birger744d5a32015-03-01 14:58:31 +02001008 sizeof(__u32), &SOCK_SKB_CB(skb)->dropcount);
Neil Horman3b885782009-10-12 13:26:31 -07001009}
1010
Erin MacNeil6fd1d512022-04-27 16:02:37 -04001011static void sock_recv_mark(struct msghdr *msg, struct sock *sk,
1012 struct sk_buff *skb)
1013{
Eric Dumazet2558b802023-02-13 16:00:59 +00001014 if (sock_flag(sk, SOCK_RCVMARK) && skb) {
1015 /* We must use a bounce buffer for CONFIG_HARDENED_USERCOPY=y */
1016 __u32 mark = skb->mark;
1017
1018 put_cmsg(msg, SOL_SOCKET, SO_MARK, sizeof(__u32), &mark);
1019 }
Erin MacNeil6fd1d512022-04-27 16:02:37 -04001020}
1021
1022void __sock_recv_cmsgs(struct msghdr *msg, struct sock *sk,
1023 struct sk_buff *skb)
Neil Horman3b885782009-10-12 13:26:31 -07001024{
1025 sock_recv_timestamp(msg, sk, skb);
1026 sock_recv_drops(msg, sk, skb);
Erin MacNeil6fd1d512022-04-27 16:02:37 -04001027 sock_recv_mark(msg, sk, skb);
Neil Horman3b885782009-10-12 13:26:31 -07001028}
Erin MacNeil6fd1d512022-04-27 16:02:37 -04001029EXPORT_SYMBOL_GPL(__sock_recv_cmsgs);
Neil Horman3b885782009-10-12 13:26:31 -07001030
Paolo Abeni8c3c4472019-05-03 17:01:39 +02001031INDIRECT_CALLABLE_DECLARE(int inet_recvmsg(struct socket *, struct msghdr *,
Paolo Abenia648a592019-07-03 16:06:54 +02001032 size_t, int));
1033INDIRECT_CALLABLE_DECLARE(int inet6_recvmsg(struct socket *, struct msghdr *,
1034 size_t, int));
Yunhui Cui6e6eda42023-01-11 14:59:30 +08001035
1036static noinline void call_trace_sock_recv_length(struct sock *sk, int ret, int flags)
1037{
1038 trace_sock_recv_length(sk, ret, flags);
1039}
1040
Ying Xue1b784142015-03-02 15:37:48 +08001041static inline int sock_recvmsg_nosec(struct socket *sock, struct msghdr *msg,
Al Viro2da62902015-03-14 21:13:46 -04001042 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043{
Eric Dumazet1ded5e52023-08-08 13:58:09 +00001044 int ret = INDIRECT_CALL_INET(READ_ONCE(sock->ops)->recvmsg,
1045 inet6_recvmsg,
Yunhui Cui6e6eda42023-01-11 14:59:30 +08001046 inet_recvmsg, sock, msg,
1047 msg_data_left(msg), flags);
1048 if (trace_sock_recv_length_enabled())
1049 call_trace_sock_recv_length(sock->sk, ret, flags);
1050 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051}
1052
Randy Dunlap85806af2019-05-18 21:23:07 -07001053/**
1054 * sock_recvmsg - receive a message from @sock
1055 * @sock: socket
1056 * @msg: message to receive
1057 * @flags: message flags
1058 *
1059 * Receives @msg from @sock, passing through LSM. Returns the total number
1060 * of bytes received, or an error.
1061 */
Al Viro2da62902015-03-14 21:13:46 -04001062int sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07001063{
Al Viro2da62902015-03-14 21:13:46 -04001064 int err = security_socket_recvmsg(sock, msg, msg_data_left(msg), flags);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07001065
Al Viro2da62902015-03-14 21:13:46 -04001066 return err ?: sock_recvmsg_nosec(sock, msg, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001068EXPORT_SYMBOL(sock_recvmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
Martin Lucinac1249c02010-12-10 00:04:05 +00001070/**
Pedro Tammela8a3c2452019-03-14 10:45:23 -03001071 * kernel_recvmsg - Receive a message from a socket (kernel space)
1072 * @sock: The socket to receive the message from
1073 * @msg: Received message
1074 * @vec: Input s/g array for message data
1075 * @num: Size of input s/g array
1076 * @size: Number of bytes to read
1077 * @flags: Message flags (MSG_DONTWAIT, etc...)
Martin Lucinac1249c02010-12-10 00:04:05 +00001078 *
Pedro Tammela8a3c2452019-03-14 10:45:23 -03001079 * On return the msg structure contains the scatter/gather array passed in the
1080 * vec argument. The array is modified so that it consists of the unfilled
1081 * portion of the original array.
Martin Lucinac1249c02010-12-10 00:04:05 +00001082 *
Pedro Tammela8a3c2452019-03-14 10:45:23 -03001083 * The returned value is the total number of bytes received, or an error.
Martin Lucinac1249c02010-12-10 00:04:05 +00001084 */
Pedro Tammela8a3c2452019-03-14 10:45:23 -03001085
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001086int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
1087 struct kvec *vec, size_t num, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088{
Christoph Hellwig1f466e12020-05-11 13:59:13 +02001089 msg->msg_control_is_user = false;
Al Virode4eda92022-09-15 20:25:47 -04001090 iov_iter_kvec(&msg->msg_iter, ITER_DEST, vec, num, size);
Christoph Hellwig1f466e12020-05-11 13:59:13 +02001091 return sock_recvmsg(sock, msg, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001093EXPORT_SYMBOL(kernel_recvmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Jens Axboe9c55e012007-11-06 23:30:13 -08001095static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001096 struct pipe_inode_info *pipe, size_t len,
Jens Axboe9c55e012007-11-06 23:30:13 -08001097 unsigned int flags)
1098{
1099 struct socket *sock = file->private_data;
Eric Dumazet1ded5e52023-08-08 13:58:09 +00001100 const struct proto_ops *ops;
Jens Axboe9c55e012007-11-06 23:30:13 -08001101
Eric Dumazet1ded5e52023-08-08 13:58:09 +00001102 ops = READ_ONCE(sock->ops);
1103 if (unlikely(!ops->splice_read))
David Howells67178fd2023-05-22 14:50:00 +01001104 return copy_splice_read(file, ppos, pipe, len, flags);
Rémi Denis-Courmont997b37d2008-02-15 02:35:45 -08001105
Eric Dumazet1ded5e52023-08-08 13:58:09 +00001106 return ops->splice_read(sock, ppos, pipe, len, flags);
Jens Axboe9c55e012007-11-06 23:30:13 -08001107}
1108
David Howells2bfc6682023-06-07 19:19:10 +01001109static void sock_splice_eof(struct file *file)
1110{
1111 struct socket *sock = file->private_data;
Eric Dumazet1ded5e52023-08-08 13:58:09 +00001112 const struct proto_ops *ops;
David Howells2bfc6682023-06-07 19:19:10 +01001113
Eric Dumazet1ded5e52023-08-08 13:58:09 +00001114 ops = READ_ONCE(sock->ops);
1115 if (ops->splice_eof)
1116 ops->splice_eof(sock);
David Howells2bfc6682023-06-07 19:19:10 +01001117}
1118
Al Viro8ae5e0302014-11-28 19:40:50 -05001119static ssize_t sock_read_iter(struct kiocb *iocb, struct iov_iter *to)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -08001120{
Al Viro6d652332015-01-30 16:12:56 -05001121 struct file *file = iocb->ki_filp;
1122 struct socket *sock = file->private_data;
tadeusz.struk@intel.com0345f932015-03-19 12:31:25 -07001123 struct msghdr msg = {.msg_iter = *to,
1124 .msg_iocb = iocb};
Al Viro8ae5e0302014-11-28 19:40:50 -05001125 ssize_t res;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -08001126
Jens Axboeebfcd892019-12-09 20:58:56 -07001127 if (file->f_flags & O_NONBLOCK || (iocb->ki_flags & IOCB_NOWAIT))
Al Viro8ae5e0302014-11-28 19:40:50 -05001128 msg.msg_flags = MSG_DONTWAIT;
1129
1130 if (iocb->ki_pos != 0)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -08001131 return -ESPIPE;
Badari Pulavarty027445c2006-09-30 23:28:46 -07001132
Christoph Hellwig66ee59a2015-02-11 19:56:46 +01001133 if (!iov_iter_count(to)) /* Match SYS5 behaviour */
Christoph Hellwigce1d4d32005-12-22 21:08:46 -08001134 return 0;
1135
Al Viro2da62902015-03-14 21:13:46 -04001136 res = sock_recvmsg(sock, &msg, msg.msg_flags);
Al Viro8ae5e0302014-11-28 19:40:50 -05001137 *to = msg.msg_iter;
1138 return res;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -08001139}
1140
Al Viro8ae5e0302014-11-28 19:40:50 -05001141static ssize_t sock_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142{
Al Viro6d652332015-01-30 16:12:56 -05001143 struct file *file = iocb->ki_filp;
1144 struct socket *sock = file->private_data;
tadeusz.struk@intel.com0345f932015-03-19 12:31:25 -07001145 struct msghdr msg = {.msg_iter = *from,
1146 .msg_iocb = iocb};
Al Viro8ae5e0302014-11-28 19:40:50 -05001147 ssize_t res;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -08001148
Al Viro8ae5e0302014-11-28 19:40:50 -05001149 if (iocb->ki_pos != 0)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -08001150 return -ESPIPE;
Badari Pulavarty027445c2006-09-30 23:28:46 -07001151
Jens Axboeebfcd892019-12-09 20:58:56 -07001152 if (file->f_flags & O_NONBLOCK || (iocb->ki_flags & IOCB_NOWAIT))
Al Viro8ae5e0302014-11-28 19:40:50 -05001153 msg.msg_flags = MSG_DONTWAIT;
1154
Al Viro6d652332015-01-30 16:12:56 -05001155 if (sock->type == SOCK_SEQPACKET)
1156 msg.msg_flags |= MSG_EOR;
1157
Jordan Rife86a7e0b2023-09-21 18:46:41 -05001158 res = __sock_sendmsg(sock, &msg);
Al Viro8ae5e0302014-11-28 19:40:50 -05001159 *from = msg.msg_iter;
1160 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161}
1162
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163/*
1164 * Atomic setting of ioctl hooks to avoid race
1165 * with module unload.
1166 */
1167
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001168static DEFINE_MUTEX(br_ioctl_mutex);
Arnd Bergmannad2f99a2021-07-27 15:45:16 +02001169static int (*br_ioctl_hook)(struct net *net, struct net_bridge *br,
1170 unsigned int cmd, struct ifreq *ifr,
1171 void __user *uarg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Arnd Bergmannad2f99a2021-07-27 15:45:16 +02001173void brioctl_set(int (*hook)(struct net *net, struct net_bridge *br,
1174 unsigned int cmd, struct ifreq *ifr,
1175 void __user *uarg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001177 mutex_lock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 br_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001179 mutex_unlock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180}
1181EXPORT_SYMBOL(brioctl_set);
1182
Arnd Bergmannad2f99a2021-07-27 15:45:16 +02001183int br_ioctl_call(struct net *net, struct net_bridge *br, unsigned int cmd,
1184 struct ifreq *ifr, void __user *uarg)
1185{
1186 int err = -ENOPKG;
1187
1188 if (!br_ioctl_hook)
1189 request_module("bridge");
1190
1191 mutex_lock(&br_ioctl_mutex);
1192 if (br_ioctl_hook)
1193 err = br_ioctl_hook(net, br, cmd, ifr, uarg);
1194 mutex_unlock(&br_ioctl_mutex);
1195
1196 return err;
1197}
1198
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001199static DEFINE_MUTEX(vlan_ioctl_mutex);
Eric W. Biederman881d9662007-09-17 11:56:21 -07001200static int (*vlan_ioctl_hook) (struct net *, void __user *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
Eric W. Biederman881d9662007-09-17 11:56:21 -07001202void vlan_ioctl_set(int (*hook) (struct net *, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001204 mutex_lock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 vlan_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001206 mutex_unlock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207}
1208EXPORT_SYMBOL(vlan_ioctl_set);
1209
Arnd Bergmann6b960182009-11-06 23:10:54 -08001210static long sock_do_ioctl(struct net *net, struct socket *sock,
Johannes Berg63ff03a2019-01-25 22:43:17 +01001211 unsigned int cmd, unsigned long arg)
Arnd Bergmann6b960182009-11-06 23:10:54 -08001212{
Eric Dumazet1ded5e52023-08-08 13:58:09 +00001213 const struct proto_ops *ops = READ_ONCE(sock->ops);
Arnd Bergmann876f0bf2021-07-22 16:29:02 +02001214 struct ifreq ifr;
1215 bool need_copyout;
Arnd Bergmann6b960182009-11-06 23:10:54 -08001216 int err;
1217 void __user *argp = (void __user *)arg;
Arnd Bergmanna554bf92021-07-27 15:45:12 +02001218 void __user *data;
Arnd Bergmann6b960182009-11-06 23:10:54 -08001219
Eric Dumazet1ded5e52023-08-08 13:58:09 +00001220 err = ops->ioctl(sock, cmd, arg);
Arnd Bergmann6b960182009-11-06 23:10:54 -08001221
1222 /*
1223 * If this ioctl is unknown try to hand it down
1224 * to the NIC driver.
1225 */
Al Viro36fd6332017-06-26 13:19:16 -04001226 if (err != -ENOIOCTLCMD)
1227 return err;
Arnd Bergmann6b960182009-11-06 23:10:54 -08001228
Jakub Kicinski29ce8f92021-08-31 09:06:04 -07001229 if (!is_socket_ioctl_cmd(cmd))
1230 return -ENOTTY;
1231
Arnd Bergmanna554bf92021-07-27 15:45:12 +02001232 if (get_user_ifreq(&ifr, &data, argp))
Arnd Bergmann876f0bf2021-07-22 16:29:02 +02001233 return -EFAULT;
Arnd Bergmanna554bf92021-07-27 15:45:12 +02001234 err = dev_ioctl(net, cmd, &ifr, data, &need_copyout);
Arnd Bergmann876f0bf2021-07-22 16:29:02 +02001235 if (!err && need_copyout)
Arnd Bergmanna554bf92021-07-27 15:45:12 +02001236 if (put_user_ifreq(&ifr, argp))
Al Viro36fd6332017-06-26 13:19:16 -04001237 return -EFAULT;
Arnd Bergmann876f0bf2021-07-22 16:29:02 +02001238
Arnd Bergmann6b960182009-11-06 23:10:54 -08001239 return err;
1240}
1241
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242/*
1243 * With an ioctl, arg may well be a user mode pointer, but we don't know
1244 * what to do with it - that's up to the protocol still.
1245 */
1246
1247static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
1248{
Eric Dumazet1ded5e52023-08-08 13:58:09 +00001249 const struct proto_ops *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 struct socket *sock;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001251 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 void __user *argp = (void __user *)arg;
1253 int pid, err;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001254 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
Eric Dumazetb69aee02005-09-06 14:42:45 -07001256 sock = file->private_data;
Eric Dumazet1ded5e52023-08-08 13:58:09 +00001257 ops = READ_ONCE(sock->ops);
Eric W. Biederman881d9662007-09-17 11:56:21 -07001258 sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001259 net = sock_net(sk);
Al Viro44c02a22017-10-05 12:59:44 -04001260 if (unlikely(cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15))) {
1261 struct ifreq ifr;
Arnd Bergmanna554bf92021-07-27 15:45:12 +02001262 void __user *data;
Al Viro44c02a22017-10-05 12:59:44 -04001263 bool need_copyout;
Arnd Bergmanna554bf92021-07-27 15:45:12 +02001264 if (get_user_ifreq(&ifr, &data, argp))
Al Viro44c02a22017-10-05 12:59:44 -04001265 return -EFAULT;
Arnd Bergmanna554bf92021-07-27 15:45:12 +02001266 err = dev_ioctl(net, cmd, &ifr, data, &need_copyout);
Al Viro44c02a22017-10-05 12:59:44 -04001267 if (!err && need_copyout)
Arnd Bergmanna554bf92021-07-27 15:45:12 +02001268 if (put_user_ifreq(&ifr, argp))
Al Viro44c02a22017-10-05 12:59:44 -04001269 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 } else
Johannes Berg3d23e342009-09-29 23:27:28 +02001271#ifdef CONFIG_WEXT_CORE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
Al Virob1b0c242017-10-01 20:13:08 -04001273 err = wext_handle_ioctl(net, cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 } else
Johannes Berg3d23e342009-09-29 23:27:28 +02001275#endif
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001276 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 case FIOSETOWN:
1278 case SIOCSPGRP:
1279 err = -EFAULT;
1280 if (get_user(pid, (int __user *)argp))
1281 break;
Jiri Slaby393cc3f2017-06-13 13:35:50 +02001282 err = f_setown(sock->file, pid, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 break;
1284 case FIOGETOWN:
1285 case SIOCGPGRP:
Eric W. Biederman609d7fa2006-10-02 02:17:15 -07001286 err = put_user(f_getown(sock->file),
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001287 (int __user *)argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 break;
1289 case SIOCGIFBR:
1290 case SIOCSIFBR:
1291 case SIOCBRADDBR:
1292 case SIOCBRDELBR:
Arnd Bergmannad2f99a2021-07-27 15:45:16 +02001293 err = br_ioctl_call(net, NULL, cmd, NULL, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 break;
1295 case SIOCGIFVLAN:
1296 case SIOCSIFVLAN:
1297 err = -ENOPKG;
1298 if (!vlan_ioctl_hook)
1299 request_module("8021q");
1300
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001301 mutex_lock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 if (vlan_ioctl_hook)
Eric W. Biederman881d9662007-09-17 11:56:21 -07001303 err = vlan_ioctl_hook(net, argp);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001304 mutex_unlock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 break;
Andrey Vaginc62cce22016-10-24 18:29:13 -07001306 case SIOCGSKNS:
1307 err = -EPERM;
1308 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1309 break;
1310
1311 err = open_related_ns(&net->ns, get_net_ns);
1312 break;
Arnd Bergmann0768e172019-04-17 22:56:11 +02001313 case SIOCGSTAMP_OLD:
1314 case SIOCGSTAMPNS_OLD:
Eric Dumazet1ded5e52023-08-08 13:58:09 +00001315 if (!ops->gettstamp) {
Arnd Bergmannc7cbdbf2019-04-17 22:51:48 +02001316 err = -ENOIOCTLCMD;
1317 break;
1318 }
Eric Dumazet1ded5e52023-08-08 13:58:09 +00001319 err = ops->gettstamp(sock, argp,
1320 cmd == SIOCGSTAMP_OLD,
1321 !IS_ENABLED(CONFIG_64BIT));
Gustavo A. R. Silva60747822019-04-24 10:31:24 -05001322 break;
Arnd Bergmann0768e172019-04-17 22:56:11 +02001323 case SIOCGSTAMP_NEW:
1324 case SIOCGSTAMPNS_NEW:
Eric Dumazet1ded5e52023-08-08 13:58:09 +00001325 if (!ops->gettstamp) {
Arnd Bergmann0768e172019-04-17 22:56:11 +02001326 err = -ENOIOCTLCMD;
1327 break;
1328 }
Eric Dumazet1ded5e52023-08-08 13:58:09 +00001329 err = ops->gettstamp(sock, argp,
1330 cmd == SIOCGSTAMP_NEW,
1331 false);
Arnd Bergmannc7cbdbf2019-04-17 22:51:48 +02001332 break;
Arnd Bergmann876f0bf2021-07-22 16:29:02 +02001333
1334 case SIOCGIFCONF:
1335 err = dev_ifconf(net, argp);
1336 break;
1337
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 default:
Johannes Berg63ff03a2019-01-25 22:43:17 +01001339 err = sock_do_ioctl(net, sock, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 break;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 return err;
1343}
1344
Pedro Tammela8a3c2452019-03-14 10:45:23 -03001345/**
1346 * sock_create_lite - creates a socket
1347 * @family: protocol family (AF_INET, ...)
1348 * @type: communication type (SOCK_STREAM, ...)
1349 * @protocol: protocol (0, ...)
1350 * @res: new socket
1351 *
1352 * Creates a new socket and assigns it to @res, passing through LSM.
1353 * The new socket initialization is not complete, see kernel_accept().
1354 * Returns 0 or an error. On failure @res is set to %NULL.
1355 * This function internally uses GFP_KERNEL.
1356 */
1357
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358int sock_create_lite(int family, int type, int protocol, struct socket **res)
1359{
1360 int err;
1361 struct socket *sock = NULL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001362
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 err = security_socket_create(family, type, protocol, 1);
1364 if (err)
1365 goto out;
1366
1367 sock = sock_alloc();
1368 if (!sock) {
1369 err = -ENOMEM;
1370 goto out;
1371 }
1372
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 sock->type = type;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001374 err = security_socket_post_create(sock, family, type, protocol, 1);
1375 if (err)
1376 goto out_release;
1377
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378out:
1379 *res = sock;
1380 return err;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001381out_release:
1382 sock_release(sock);
1383 sock = NULL;
1384 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001386EXPORT_SYMBOL(sock_create_lite);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
1388/* No kernel lock held - perfect */
Al Viroade994f2017-07-03 00:01:49 -04001389static __poll_t sock_poll(struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390{
Christoph Hellwig3cafb372018-01-09 16:07:34 +01001391 struct socket *sock = file->private_data;
Eric Dumazet1ded5e52023-08-08 13:58:09 +00001392 const struct proto_ops *ops = READ_ONCE(sock->ops);
Christoph Hellwiga331de32018-07-30 09:42:13 +02001393 __poll_t events = poll_requested_events(wait), flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
Eric Dumazet1ded5e52023-08-08 13:58:09 +00001395 if (!ops->poll)
Christoph Hellwige88958e2018-06-29 15:37:24 +02001396 return 0;
Christoph Hellwigf641f13b2018-07-30 09:42:12 +02001397
Christoph Hellwiga331de32018-07-30 09:42:13 +02001398 if (sk_can_busy_loop(sock->sk)) {
1399 /* poll once if requested by the syscall */
1400 if (events & POLL_BUSY_LOOP)
1401 sk_busy_loop(sock->sk, 1);
1402
1403 /* if this socket can poll_ll, tell the system call */
1404 flag = POLL_BUSY_LOOP;
1405 }
1406
Eric Dumazet1ded5e52023-08-08 13:58:09 +00001407 return ops->poll(file, sock, wait) | flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408}
1409
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001410static int sock_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411{
Eric Dumazetb69aee02005-09-06 14:42:45 -07001412 struct socket *sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Eric Dumazet1ded5e52023-08-08 13:58:09 +00001414 return READ_ONCE(sock->ops)->mmap(file, sock, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415}
1416
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001417static int sock_close(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418{
Cong Wang6d8c50d2018-06-07 13:39:49 -07001419 __sock_release(SOCKET_I(inode), inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 return 0;
1421}
1422
1423/*
1424 * Update the socket async list
1425 *
1426 * Fasync_list locking strategy.
1427 *
1428 * 1. fasync_list is modified only under process context socket lock
1429 * i.e. under semaphore.
1430 * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
Eric Dumazet989a2972010-04-14 09:55:35 +00001431 * or under socket lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 */
1433
1434static int sock_fasync(int fd, struct file *filp, int on)
1435{
Eric Dumazet989a2972010-04-14 09:55:35 +00001436 struct socket *sock = filp->private_data;
1437 struct sock *sk = sock->sk;
Al Viro333f7902019-07-05 20:14:16 +01001438 struct socket_wq *wq = &sock->wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
Eric Dumazet989a2972010-04-14 09:55:35 +00001440 if (sk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
1443 lock_sock(sk);
Eric Dumazeteaefd1102011-02-18 03:26:36 +00001444 fasync_helper(fd, filp, on, &wq->fasync_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Eric Dumazeteaefd1102011-02-18 03:26:36 +00001446 if (!wq->fasync_list)
Eric Dumazet989a2972010-04-14 09:55:35 +00001447 sock_reset_flag(sk, SOCK_FASYNC);
Jonathan Corbet76398422009-02-01 14:26:59 -07001448 else
Eric Dumazetbcdce712009-10-06 17:28:29 -07001449 sock_set_flag(sk, SOCK_FASYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Eric Dumazet989a2972010-04-14 09:55:35 +00001451 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 return 0;
1453}
1454
Eric Dumazetceb5d582015-11-29 20:03:11 -08001455/* This function may be called only under rcu_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Eric Dumazetceb5d582015-11-29 20:03:11 -08001457int sock_wake_async(struct socket_wq *wq, int how, int band)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458{
Eric Dumazetceb5d582015-11-29 20:03:11 -08001459 if (!wq || !wq->fasync_list)
1460 return -1;
Eric Dumazet43815482010-04-29 11:01:49 +00001461
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001462 switch (how) {
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001463 case SOCK_WAKE_WAITD:
Eric Dumazetceb5d582015-11-29 20:03:11 -08001464 if (test_bit(SOCKWQ_ASYNC_WAITDATA, &wq->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 break;
1466 goto call_kill;
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001467 case SOCK_WAKE_SPACE:
Eric Dumazetceb5d582015-11-29 20:03:11 -08001468 if (!test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 break;
Miaohe Lin7c7ab582020-08-08 16:23:10 +08001470 fallthrough;
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001471 case SOCK_WAKE_IO:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001472call_kill:
Eric Dumazet43815482010-04-29 11:01:49 +00001473 kill_fasync(&wq->fasync_list, SIGIO, band);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 break;
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001475 case SOCK_WAKE_URG:
Eric Dumazet43815482010-04-29 11:01:49 +00001476 kill_fasync(&wq->fasync_list, SIGURG, band);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 }
Eric Dumazetceb5d582015-11-29 20:03:11 -08001478
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 return 0;
1480}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001481EXPORT_SYMBOL(sock_wake_async);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Pedro Tammela8a3c2452019-03-14 10:45:23 -03001483/**
1484 * __sock_create - creates a socket
1485 * @net: net namespace
1486 * @family: protocol family (AF_INET, ...)
1487 * @type: communication type (SOCK_STREAM, ...)
1488 * @protocol: protocol (0, ...)
1489 * @res: new socket
1490 * @kern: boolean for kernel space sockets
1491 *
1492 * Creates a new socket and assigns it to @res, passing through LSM.
1493 * Returns 0 or an error. On failure @res is set to %NULL. @kern must
1494 * be set to true if the socket resides in kernel space.
1495 * This function internally uses GFP_KERNEL.
1496 */
1497
Pavel Emelyanov721db932010-09-29 16:06:32 +04001498int __sock_create(struct net *net, int family, int type, int protocol,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001499 struct socket **res, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500{
1501 int err;
1502 struct socket *sock;
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001503 const struct net_proto_family *pf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
1505 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001506 * Check protocol is in range
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 */
1508 if (family < 0 || family >= NPROTO)
1509 return -EAFNOSUPPORT;
1510 if (type < 0 || type >= SOCK_MAX)
1511 return -EINVAL;
1512
1513 /* Compatibility.
1514
1515 This uglymoron is moved from INET layer to here to avoid
1516 deadlock in module load.
1517 */
1518 if (family == PF_INET && type == SOCK_PACKET) {
liping.zhangf3c98692016-03-11 23:08:36 +08001519 pr_info_once("%s uses obsolete (PF_INET,SOCK_PACKET)\n",
1520 current->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 family = PF_PACKET;
1522 }
1523
1524 err = security_socket_create(family, type, protocol, kern);
1525 if (err)
1526 return err;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001527
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001528 /*
1529 * Allocate the socket and allow the family to set things up. if
1530 * the protocol is 0, the family is instructed to select an appropriate
1531 * default.
1532 */
1533 sock = sock_alloc();
1534 if (!sock) {
Joe Perchese87cc472012-05-13 21:56:26 +00001535 net_warn_ratelimited("socket: no more sockets\n");
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001536 return -ENFILE; /* Not exactly a match, but its the
1537 closest posix thing */
1538 }
1539
1540 sock->type = type;
1541
Johannes Berg95a5afc2008-10-16 15:24:51 -07001542#ifdef CONFIG_MODULES
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001543 /* Attempt to load a protocol module if the find failed.
1544 *
1545 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 * requested real, full-featured networking support upon configuration.
1547 * Otherwise module support will break!
1548 */
Eric Dumazet190683a2010-11-10 10:50:44 +00001549 if (rcu_access_pointer(net_families[family]) == NULL)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001550 request_module("net-pf-%d", family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551#endif
1552
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001553 rcu_read_lock();
1554 pf = rcu_dereference(net_families[family]);
1555 err = -EAFNOSUPPORT;
1556 if (!pf)
1557 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
1559 /*
1560 * We will call the ->create function, that possibly is in a loadable
1561 * module, so we have to bump that loadable module refcnt first.
1562 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001563 if (!try_module_get(pf->owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 goto out_release;
1565
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001566 /* Now protected by module ref count */
1567 rcu_read_unlock();
1568
Eric Paris3f378b682009-11-05 22:18:14 -08001569 err = pf->create(net, sock, protocol, kern);
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001570 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 goto out_module_put;
Frank Filza79af592005-09-27 15:23:38 -07001572
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 /*
1574 * Now to bump the refcnt of the [loadable] module that owns this
1575 * socket at sock_release time we decrement its refcnt.
1576 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001577 if (!try_module_get(sock->ops->owner))
1578 goto out_module_busy;
1579
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 /*
1581 * Now that we're done with the ->create function, the [loadable]
1582 * module can have its refcnt decremented
1583 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001584 module_put(pf->owner);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001585 err = security_socket_post_create(sock, family, type, protocol, kern);
1586 if (err)
Herbert Xu3b185522007-08-15 14:46:02 -07001587 goto out_sock_release;
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001588 *res = sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001590 return 0;
1591
1592out_module_busy:
1593 err = -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594out_module_put:
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001595 sock->ops = NULL;
1596 module_put(pf->owner);
1597out_sock_release:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 sock_release(sock);
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001599 return err;
1600
1601out_release:
1602 rcu_read_unlock();
1603 goto out_sock_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604}
Pavel Emelyanov721db932010-09-29 16:06:32 +04001605EXPORT_SYMBOL(__sock_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
Pedro Tammela8a3c2452019-03-14 10:45:23 -03001607/**
1608 * sock_create - creates a socket
1609 * @family: protocol family (AF_INET, ...)
1610 * @type: communication type (SOCK_STREAM, ...)
1611 * @protocol: protocol (0, ...)
1612 * @res: new socket
1613 *
1614 * A wrapper around __sock_create().
1615 * Returns 0 or an error. This function internally uses GFP_KERNEL.
1616 */
1617
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618int sock_create(int family, int type, int protocol, struct socket **res)
1619{
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001620 return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001622EXPORT_SYMBOL(sock_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
Pedro Tammela8a3c2452019-03-14 10:45:23 -03001624/**
1625 * sock_create_kern - creates a socket (kernel space)
1626 * @net: net namespace
1627 * @family: protocol family (AF_INET, ...)
1628 * @type: communication type (SOCK_STREAM, ...)
1629 * @protocol: protocol (0, ...)
1630 * @res: new socket
1631 *
1632 * A wrapper around __sock_create().
1633 * Returns 0 or an error. This function internally uses GFP_KERNEL.
1634 */
1635
Eric W. Biedermaneeb1bd52015-05-08 21:08:05 -05001636int sock_create_kern(struct net *net, int family, int type, int protocol, struct socket **res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637{
Eric W. Biedermaneeb1bd52015-05-08 21:08:05 -05001638 return __sock_create(net, family, type, protocol, res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001640EXPORT_SYMBOL(sock_create_kern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
Jens Axboeda214a42022-04-12 14:22:39 -06001642static struct socket *__sys_socket_create(int family, int type, int protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 struct socket *sock;
Jens Axboeda214a42022-04-12 14:22:39 -06001645 int retval;
Ulrich Dreppera677a032008-07-23 21:29:17 -07001646
Ulrich Dreppere38b36f2008-07-23 21:29:42 -07001647 /* Check the SOCK_* constants for consistency. */
1648 BUILD_BUG_ON(SOCK_CLOEXEC != O_CLOEXEC);
1649 BUILD_BUG_ON((SOCK_MAX | SOCK_TYPE_MASK) != SOCK_TYPE_MASK);
1650 BUILD_BUG_ON(SOCK_CLOEXEC & SOCK_TYPE_MASK);
1651 BUILD_BUG_ON(SOCK_NONBLOCK & SOCK_TYPE_MASK);
1652
Jens Axboeda214a42022-04-12 14:22:39 -06001653 if ((type & ~SOCK_TYPE_MASK) & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1654 return ERR_PTR(-EINVAL);
Ulrich Dreppera677a032008-07-23 21:29:17 -07001655 type &= SOCK_TYPE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
1657 retval = sock_create(family, type, protocol, &sock);
1658 if (retval < 0)
Jens Axboeda214a42022-04-12 14:22:39 -06001659 return ERR_PTR(retval);
1660
1661 return sock;
1662}
1663
1664struct file *__sys_socket_file(int family, int type, int protocol)
1665{
1666 struct socket *sock;
Jens Axboeda214a42022-04-12 14:22:39 -06001667 int flags;
1668
1669 sock = __sys_socket_create(family, type, protocol);
1670 if (IS_ERR(sock))
1671 return ERR_CAST(sock);
1672
1673 flags = type & ~SOCK_TYPE_MASK;
1674 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1675 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1676
Thadeu Lima de Souza Cascardo649c15c2023-03-07 14:37:07 -03001677 return sock_alloc_file(sock, flags, NULL);
Jens Axboeda214a42022-04-12 14:22:39 -06001678}
1679
Geliang Tang0dd061a2023-08-16 09:11:56 +08001680/* A hook for bpf progs to attach to and update socket protocol.
1681 *
1682 * A static noinline declaration here could cause the compiler to
1683 * optimize away the function. A global noinline declaration will
1684 * keep the definition, but may optimize away the callsite.
1685 * Therefore, __weak is needed to ensure that the call is still
1686 * emitted, by telling the compiler that we don't know what the
1687 * function might eventually be.
Geliang Tang0dd061a2023-08-16 09:11:56 +08001688 */
1689
Dave Marchevsky15fb6f2b2023-10-31 14:56:25 -07001690__bpf_hook_start();
Geliang Tang0dd061a2023-08-16 09:11:56 +08001691
1692__weak noinline int update_socket_protocol(int family, int type, int protocol)
1693{
1694 return protocol;
1695}
1696
Dave Marchevsky15fb6f2b2023-10-31 14:56:25 -07001697__bpf_hook_end();
Geliang Tang0dd061a2023-08-16 09:11:56 +08001698
Jens Axboeda214a42022-04-12 14:22:39 -06001699int __sys_socket(int family, int type, int protocol)
1700{
1701 struct socket *sock;
1702 int flags;
1703
Geliang Tang0dd061a2023-08-16 09:11:56 +08001704 sock = __sys_socket_create(family, type,
1705 update_socket_protocol(family, type, protocol));
Jens Axboeda214a42022-04-12 14:22:39 -06001706 if (IS_ERR(sock))
1707 return PTR_ERR(sock);
1708
1709 flags = type & ~SOCK_TYPE_MASK;
1710 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1711 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
Al Viro8e1611e2017-12-05 23:29:09 +00001713 return sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714}
1715
Dominik Brodowski9d6a15c2018-03-13 19:29:43 +01001716SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol)
1717{
1718 return __sys_socket(family, type, protocol);
1719}
1720
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721/*
1722 * Create a pair of connected sockets.
1723 */
1724
Dominik Brodowski6debc8d2018-03-13 19:49:23 +01001725int __sys_socketpair(int family, int type, int protocol, int __user *usockvec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726{
1727 struct socket *sock1, *sock2;
1728 int fd1, fd2, err;
Al Virodb349502007-02-07 01:48:00 -05001729 struct file *newfile1, *newfile2;
Ulrich Dreppera677a032008-07-23 21:29:17 -07001730 int flags;
1731
1732 flags = type & ~SOCK_TYPE_MASK;
Ulrich Drepper77d27202008-07-23 21:29:35 -07001733 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Dreppera677a032008-07-23 21:29:17 -07001734 return -EINVAL;
1735 type &= SOCK_TYPE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001737 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1738 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1739
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 /*
Al Viro016a2662017-12-05 23:28:38 +00001741 * reserve descriptors and make sure we won't fail
1742 * to return them to userland.
1743 */
1744 fd1 = get_unused_fd_flags(flags);
1745 if (unlikely(fd1 < 0))
1746 return fd1;
1747
1748 fd2 = get_unused_fd_flags(flags);
1749 if (unlikely(fd2 < 0)) {
1750 put_unused_fd(fd1);
1751 return fd2;
1752 }
1753
1754 err = put_user(fd1, &usockvec[0]);
1755 if (err)
1756 goto out;
1757
1758 err = put_user(fd2, &usockvec[1]);
1759 if (err)
1760 goto out;
1761
1762 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 * Obtain the first socket and check if the underlying protocol
1764 * supports the socketpair call.
1765 */
1766
1767 err = sock_create(family, type, protocol, &sock1);
Al Viro016a2662017-12-05 23:28:38 +00001768 if (unlikely(err < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 goto out;
1770
1771 err = sock_create(family, type, protocol, &sock2);
Al Viro016a2662017-12-05 23:28:38 +00001772 if (unlikely(err < 0)) {
1773 sock_release(sock1);
1774 goto out;
David S. Millerbf3c23d2007-10-29 21:54:02 -07001775 }
Yann Droneaudd73aa282013-12-09 22:42:20 +01001776
David Herrmannd47cd942018-05-04 16:28:20 +02001777 err = security_socket_socketpair(sock1, sock2);
1778 if (unlikely(err)) {
1779 sock_release(sock2);
1780 sock_release(sock1);
1781 goto out;
1782 }
1783
Eric Dumazet1ded5e52023-08-08 13:58:09 +00001784 err = READ_ONCE(sock1->ops)->socketpair(sock1, sock2);
Al Viro016a2662017-12-05 23:28:38 +00001785 if (unlikely(err < 0)) {
1786 sock_release(sock2);
1787 sock_release(sock1);
1788 goto out;
Al Viro28407632012-08-17 23:54:15 -04001789 }
1790
Linus Torvaldsaab174f2012-10-02 20:25:04 -07001791 newfile1 = sock_alloc_file(sock1, flags, NULL);
Viresh Kumarb5ffe632015-08-12 15:59:47 +05301792 if (IS_ERR(newfile1)) {
Al Viro28407632012-08-17 23:54:15 -04001793 err = PTR_ERR(newfile1);
Al Viro016a2662017-12-05 23:28:38 +00001794 sock_release(sock2);
1795 goto out;
Al Viro28407632012-08-17 23:54:15 -04001796 }
1797
Linus Torvaldsaab174f2012-10-02 20:25:04 -07001798 newfile2 = sock_alloc_file(sock2, flags, NULL);
Al Viro28407632012-08-17 23:54:15 -04001799 if (IS_ERR(newfile2)) {
1800 err = PTR_ERR(newfile2);
Al Viro016a2662017-12-05 23:28:38 +00001801 fput(newfile1);
1802 goto out;
Al Virodb349502007-02-07 01:48:00 -05001803 }
1804
Al Viro157cf642008-12-14 04:57:47 -05001805 audit_fd_pair(fd1, fd2);
Yann Droneaudd73aa282013-12-09 22:42:20 +01001806
Al Virodb349502007-02-07 01:48:00 -05001807 fd_install(fd1, newfile1);
1808 fd_install(fd2, newfile2);
Yann Droneaudd73aa282013-12-09 22:42:20 +01001809 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811out:
Al Viro016a2662017-12-05 23:28:38 +00001812 put_unused_fd(fd2);
1813 put_unused_fd(fd1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 return err;
1815}
1816
Dominik Brodowski6debc8d2018-03-13 19:49:23 +01001817SYSCALL_DEFINE4(socketpair, int, family, int, type, int, protocol,
1818 int __user *, usockvec)
1819{
1820 return __sys_socketpair(family, type, protocol, usockvec);
1821}
1822
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823/*
1824 * Bind a name to a socket. Nothing much to do here since it's
1825 * the protocol's responsibility to handle the local address.
1826 *
1827 * We move the socket address to kernel space before we call
1828 * the protocol layer (having also checked the address is ok).
1829 */
1830
Dominik Brodowskia87d35d2018-03-13 19:33:09 +01001831int __sys_bind(int fd, struct sockaddr __user *umyaddr, int addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832{
1833 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001834 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001835 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001837 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001838 if (sock) {
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001839 err = move_addr_to_kernel(umyaddr, addrlen, &address);
Jakub Sitnicki068b88c2018-10-04 11:09:40 +02001840 if (!err) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001841 err = security_socket_bind(sock,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001842 (struct sockaddr *)&address,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001843 addrlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001844 if (!err)
Eric Dumazet1ded5e52023-08-08 13:58:09 +00001845 err = READ_ONCE(sock->ops)->bind(sock,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001846 (struct sockaddr *)
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001847 &address, addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 }
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001849 fput_light(sock->file, fput_needed);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001850 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 return err;
1852}
1853
Dominik Brodowskia87d35d2018-03-13 19:33:09 +01001854SYSCALL_DEFINE3(bind, int, fd, struct sockaddr __user *, umyaddr, int, addrlen)
1855{
1856 return __sys_bind(fd, umyaddr, addrlen);
1857}
1858
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859/*
1860 * Perform a listen. Basically, we allow the protocol to do anything
1861 * necessary for a listen, and if that works, we mark the socket as
1862 * ready for listening.
1863 */
1864
Dominik Brodowski25e290e2018-03-13 19:36:54 +01001865int __sys_listen(int fd, int backlog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866{
1867 struct socket *sock;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001868 int err, fput_needed;
Pavel Emelyanovb8e1f9b2007-12-08 00:12:33 -08001869 int somaxconn;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001870
1871 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1872 if (sock) {
Kuniyuki Iwashima3c9ba812022-08-23 10:47:00 -07001873 somaxconn = READ_ONCE(sock_net(sock->sk)->core.sysctl_somaxconn);
Eric Dumazet95c96172012-04-15 05:58:06 +00001874 if ((unsigned int)backlog > somaxconn)
Pavel Emelyanovb8e1f9b2007-12-08 00:12:33 -08001875 backlog = somaxconn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
1877 err = security_socket_listen(sock, backlog);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001878 if (!err)
Eric Dumazet1ded5e52023-08-08 13:58:09 +00001879 err = READ_ONCE(sock->ops)->listen(sock, backlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001881 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 }
1883 return err;
1884}
1885
Dominik Brodowski25e290e2018-03-13 19:36:54 +01001886SYSCALL_DEFINE2(listen, int, fd, int, backlog)
1887{
1888 return __sys_listen(fd, backlog);
1889}
1890
Pavel Begunkovd32f89d2021-08-25 12:25:44 +01001891struct file *do_accept(struct file *file, unsigned file_flags,
Jens Axboede2ea4b2019-10-17 14:41:29 -06001892 struct sockaddr __user *upeer_sockaddr,
Pavel Begunkovd32f89d2021-08-25 12:25:44 +01001893 int __user *upeer_addrlen, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894{
1895 struct socket *sock, *newsock;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001896 struct file *newfile;
Pavel Begunkovd32f89d2021-08-25 12:25:44 +01001897 int err, len;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001898 struct sockaddr_storage address;
Eric Dumazet1ded5e52023-08-08 13:58:09 +00001899 const struct proto_ops *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
Florent Revestdba4a922020-12-04 12:36:04 +01001901 sock = sock_from_file(file);
Pavel Begunkovd32f89d2021-08-25 12:25:44 +01001902 if (!sock)
1903 return ERR_PTR(-ENOTSOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001905 newsock = sock_alloc();
1906 if (!newsock)
Pavel Begunkovd32f89d2021-08-25 12:25:44 +01001907 return ERR_PTR(-ENFILE);
Eric Dumazet1ded5e52023-08-08 13:58:09 +00001908 ops = READ_ONCE(sock->ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
1910 newsock->type = sock->type;
Eric Dumazet1ded5e52023-08-08 13:58:09 +00001911 newsock->ops = ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 /*
1914 * We don't need try_module_get here, as the listening socket (sock)
1915 * has the protocol module (sock->ops->owner) held.
1916 */
Eric Dumazet1ded5e52023-08-08 13:58:09 +00001917 __module_get(ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
Linus Torvaldsaab174f2012-10-02 20:25:04 -07001919 newfile = sock_alloc_file(newsock, flags, sock->sk->sk_prot_creator->name);
Pavel Begunkovd32f89d2021-08-25 12:25:44 +01001920 if (IS_ERR(newfile))
1921 return newfile;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001922
Frank Filza79af592005-09-27 15:23:38 -07001923 err = security_socket_accept(sock, newsock);
1924 if (err)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001925 goto out_fd;
Frank Filza79af592005-09-27 15:23:38 -07001926
Eric Dumazet1ded5e52023-08-08 13:58:09 +00001927 err = ops->accept(sock, newsock, sock->file->f_flags | file_flags,
Jens Axboede2ea4b2019-10-17 14:41:29 -06001928 false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 if (err < 0)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001930 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
1932 if (upeer_sockaddr) {
Eric Dumazet1ded5e52023-08-08 13:58:09 +00001933 len = ops->getname(newsock, (struct sockaddr *)&address, 2);
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001934 if (len < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 err = -ECONNABORTED;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001936 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 }
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001938 err = move_addr_to_user(&address,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001939 len, upeer_sockaddr, upeer_addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 if (err < 0)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001941 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 }
1943
1944 /* File flags are not inherited via accept() unlike another OSes. */
Pavel Begunkovd32f89d2021-08-25 12:25:44 +01001945 return newfile;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001946out_fd:
David S. Miller9606a212006-04-01 01:00:14 -08001947 fput(newfile);
Pavel Begunkovd32f89d2021-08-25 12:25:44 +01001948 return ERR_PTR(err);
1949}
Jens Axboede2ea4b2019-10-17 14:41:29 -06001950
Yajun Dengc0424532022-06-10 17:10:17 +08001951static int __sys_accept4_file(struct file *file, struct sockaddr __user *upeer_sockaddr,
1952 int __user *upeer_addrlen, int flags)
Pavel Begunkovd32f89d2021-08-25 12:25:44 +01001953{
1954 struct file *newfile;
1955 int newfd;
1956
1957 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1958 return -EINVAL;
1959
1960 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1961 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1962
Yajun Dengc0424532022-06-10 17:10:17 +08001963 newfd = get_unused_fd_flags(flags);
Pavel Begunkovd32f89d2021-08-25 12:25:44 +01001964 if (unlikely(newfd < 0))
1965 return newfd;
1966
Yajun Dengc0424532022-06-10 17:10:17 +08001967 newfile = do_accept(file, 0, upeer_sockaddr, upeer_addrlen,
Pavel Begunkovd32f89d2021-08-25 12:25:44 +01001968 flags);
1969 if (IS_ERR(newfile)) {
1970 put_unused_fd(newfd);
1971 return PTR_ERR(newfile);
1972 }
1973 fd_install(newfd, newfile);
1974 return newfd;
Jens Axboede2ea4b2019-10-17 14:41:29 -06001975}
1976
1977/*
1978 * For accept, we attempt to create a new socket, set up the link
1979 * with the client, wake up the client, then return the new
1980 * connected fd. We collect the address of the connector in kernel
1981 * space and move it to user at the very end. This is unclean because
1982 * we open the socket then return an error.
1983 *
1984 * 1003.1g adds the ability to recvmsg() to query connection pending
1985 * status to recvmsg. We need to add that support in a way thats
1986 * clean when we restructure accept also.
1987 */
1988
1989int __sys_accept4(int fd, struct sockaddr __user *upeer_sockaddr,
1990 int __user *upeer_addrlen, int flags)
1991{
1992 int ret = -EBADF;
1993 struct fd f;
1994
1995 f = fdget(fd);
1996 if (f.file) {
Yajun Dengc0424532022-06-10 17:10:17 +08001997 ret = __sys_accept4_file(f.file, upeer_sockaddr,
1998 upeer_addrlen, flags);
Miaohe Lin6b07ede2020-08-06 19:52:24 +08001999 fdput(f);
Jens Axboede2ea4b2019-10-17 14:41:29 -06002000 }
2001
2002 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003}
2004
Dominik Brodowski4541e802018-03-13 19:24:23 +01002005SYSCALL_DEFINE4(accept4, int, fd, struct sockaddr __user *, upeer_sockaddr,
2006 int __user *, upeer_addrlen, int, flags)
2007{
2008 return __sys_accept4(fd, upeer_sockaddr, upeer_addrlen, flags);
2009}
2010
Heiko Carstens20f37032009-01-14 14:14:23 +01002011SYSCALL_DEFINE3(accept, int, fd, struct sockaddr __user *, upeer_sockaddr,
2012 int __user *, upeer_addrlen)
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07002013{
Dominik Brodowski4541e802018-03-13 19:24:23 +01002014 return __sys_accept4(fd, upeer_sockaddr, upeer_addrlen, 0);
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07002015}
2016
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017/*
2018 * Attempt to connect to a socket with the server address. The address
2019 * is in user space so we verify it is OK and move it to kernel space.
2020 *
2021 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
2022 * break bindings
2023 *
2024 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
2025 * other SEQPACKET protocols that take time to connect() as it doesn't
2026 * include the -EINPROGRESS status for such sockets.
2027 */
2028
Jens Axboef499a022019-12-02 16:28:46 -07002029int __sys_connect_file(struct file *file, struct sockaddr_storage *address,
Jens Axboebd3ded32019-11-23 14:17:16 -07002030 int addrlen, int file_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031{
2032 struct socket *sock;
Jens Axboebd3ded32019-11-23 14:17:16 -07002033 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
Florent Revestdba4a922020-12-04 12:36:04 +01002035 sock = sock_from_file(file);
2036 if (!sock) {
2037 err = -ENOTSOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 goto out;
Florent Revestdba4a922020-12-04 12:36:04 +01002039 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002041 err =
Jens Axboef499a022019-12-02 16:28:46 -07002042 security_socket_connect(sock, (struct sockaddr *)address, addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 if (err)
Jens Axboebd3ded32019-11-23 14:17:16 -07002044 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
Eric Dumazet1ded5e52023-08-08 13:58:09 +00002046 err = READ_ONCE(sock->ops)->connect(sock, (struct sockaddr *)address,
2047 addrlen, sock->file->f_flags | file_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048out:
2049 return err;
2050}
2051
Jens Axboebd3ded32019-11-23 14:17:16 -07002052int __sys_connect(int fd, struct sockaddr __user *uservaddr, int addrlen)
2053{
2054 int ret = -EBADF;
2055 struct fd f;
2056
2057 f = fdget(fd);
2058 if (f.file) {
Jens Axboef499a022019-12-02 16:28:46 -07002059 struct sockaddr_storage address;
2060
2061 ret = move_addr_to_kernel(uservaddr, addrlen, &address);
2062 if (!ret)
2063 ret = __sys_connect_file(f.file, &address, addrlen, 0);
Miaohe Lin6b07ede2020-08-06 19:52:24 +08002064 fdput(f);
Jens Axboebd3ded32019-11-23 14:17:16 -07002065 }
2066
2067 return ret;
2068}
2069
Dominik Brodowski1387c2c2018-03-13 19:35:09 +01002070SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
2071 int, addrlen)
2072{
2073 return __sys_connect(fd, uservaddr, addrlen);
2074}
2075
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076/*
2077 * Get the local address ('name') of a socket object. Move the obtained
2078 * name to user space.
2079 */
2080
Dominik Brodowski8882a102018-03-13 19:43:14 +01002081int __sys_getsockname(int fd, struct sockaddr __user *usockaddr,
2082 int __user *usockaddr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083{
2084 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07002085 struct sockaddr_storage address;
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01002086 int err, fput_needed;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002087
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002088 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 if (!sock)
2090 goto out;
2091
2092 err = security_socket_getsockname(sock);
2093 if (err)
2094 goto out_put;
2095
Eric Dumazet1ded5e52023-08-08 13:58:09 +00002096 err = READ_ONCE(sock->ops)->getname(sock, (struct sockaddr *)&address, 0);
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01002097 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 goto out_put;
Hamish MacDonalde44ef1d2022-01-01 22:38:25 +10002099 /* "err" is actually length in this case */
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01002100 err = move_addr_to_user(&address, err, usockaddr, usockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
2102out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002103 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104out:
2105 return err;
2106}
2107
Dominik Brodowski8882a102018-03-13 19:43:14 +01002108SYSCALL_DEFINE3(getsockname, int, fd, struct sockaddr __user *, usockaddr,
2109 int __user *, usockaddr_len)
2110{
2111 return __sys_getsockname(fd, usockaddr, usockaddr_len);
2112}
2113
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114/*
2115 * Get the remote address ('name') of a socket object. Move the obtained
2116 * name to user space.
2117 */
2118
Dominik Brodowskib21c8f82018-03-13 19:47:00 +01002119int __sys_getpeername(int fd, struct sockaddr __user *usockaddr,
2120 int __user *usockaddr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121{
2122 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07002123 struct sockaddr_storage address;
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01002124 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002126 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2127 if (sock != NULL) {
Eric Dumazet1ded5e52023-08-08 13:58:09 +00002128 const struct proto_ops *ops = READ_ONCE(sock->ops);
2129
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 err = security_socket_getpeername(sock);
2131 if (err) {
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002132 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 return err;
2134 }
2135
Eric Dumazet1ded5e52023-08-08 13:58:09 +00002136 err = ops->getname(sock, (struct sockaddr *)&address, 1);
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01002137 if (err >= 0)
2138 /* "err" is actually length in this case */
2139 err = move_addr_to_user(&address, err, usockaddr,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002140 usockaddr_len);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002141 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 }
2143 return err;
2144}
2145
Dominik Brodowskib21c8f82018-03-13 19:47:00 +01002146SYSCALL_DEFINE3(getpeername, int, fd, struct sockaddr __user *, usockaddr,
2147 int __user *, usockaddr_len)
2148{
2149 return __sys_getpeername(fd, usockaddr, usockaddr_len);
2150}
2151
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152/*
2153 * Send a datagram to a given address. We move the address into kernel
2154 * space and check the user space data area is readable before invoking
2155 * the protocol.
2156 */
Dominik Brodowski211b6342018-03-13 19:18:52 +01002157int __sys_sendto(int fd, void __user *buff, size_t len, unsigned int flags,
2158 struct sockaddr __user *addr, int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159{
2160 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07002161 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 int err;
2163 struct msghdr msg;
2164 struct iovec iov;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002165 int fput_needed;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002166
Al Virode4eda92022-09-15 20:25:47 -04002167 err = import_single_range(ITER_SOURCE, buff, len, &iov, &msg.msg_iter);
Al Viro602bd0e2015-03-21 19:12:32 -04002168 if (unlikely(err))
2169 return err;
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08002170 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2171 if (!sock)
David S. Miller4387ff752007-02-08 15:06:08 -08002172 goto out;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002173
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002174 msg.msg_name = NULL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002175 msg.msg_control = NULL;
2176 msg.msg_controllen = 0;
2177 msg.msg_namelen = 0;
Pavel Begunkov7c701d92022-07-12 21:52:29 +01002178 msg.msg_ubuf = NULL;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002179 if (addr) {
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00002180 err = move_addr_to_kernel(addr, addr_len, &address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 if (err < 0)
2182 goto out_put;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07002183 msg.msg_name = (struct sockaddr *)&address;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002184 msg.msg_namelen = addr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 }
David Howellsb841b902023-05-22 13:11:10 +01002186 flags &= ~MSG_INTERNAL_SENDMSG_FLAGS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 if (sock->file->f_flags & O_NONBLOCK)
2188 flags |= MSG_DONTWAIT;
2189 msg.msg_flags = flags;
Jordan Rife86a7e0b2023-09-21 18:46:41 -05002190 err = __sock_sendmsg(sock, &msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002192out_put:
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08002193 fput_light(sock->file, fput_needed);
David S. Miller4387ff752007-02-08 15:06:08 -08002194out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 return err;
2196}
2197
Dominik Brodowski211b6342018-03-13 19:18:52 +01002198SYSCALL_DEFINE6(sendto, int, fd, void __user *, buff, size_t, len,
2199 unsigned int, flags, struct sockaddr __user *, addr,
2200 int, addr_len)
2201{
2202 return __sys_sendto(fd, buff, len, flags, addr, addr_len);
2203}
2204
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002206 * Send a datagram down a socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 */
2208
Heiko Carstens3e0fa652009-01-14 14:14:24 +01002209SYSCALL_DEFINE4(send, int, fd, void __user *, buff, size_t, len,
Eric Dumazet95c96172012-04-15 05:58:06 +00002210 unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211{
Dominik Brodowski211b6342018-03-13 19:18:52 +01002212 return __sys_sendto(fd, buff, len, flags, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213}
2214
2215/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002216 * Receive a frame from the socket and optionally record the address of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 * sender. We verify the buffers are writable and if needed move the
2218 * sender address from kernel to user space.
2219 */
Dominik Brodowski7a09e1e2018-03-13 19:10:06 +01002220int __sys_recvfrom(int fd, void __user *ubuf, size_t size, unsigned int flags,
2221 struct sockaddr __user *addr, int __user *addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222{
Eric Dumazet1228b342022-06-22 15:02:20 +00002223 struct sockaddr_storage address;
2224 struct msghdr msg = {
2225 /* Save some cycles and don't copy the address if not needed */
2226 .msg_name = addr ? (struct sockaddr *)&address : NULL,
2227 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 struct socket *sock;
2229 struct iovec iov;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002230 int err, err2;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002231 int fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232
Al Virode4eda92022-09-15 20:25:47 -04002233 err = import_single_range(ITER_DEST, ubuf, size, &iov, &msg.msg_iter);
Al Viro602bd0e2015-03-21 19:12:32 -04002234 if (unlikely(err))
2235 return err;
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08002236 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 if (!sock)
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08002238 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 if (sock->file->f_flags & O_NONBLOCK)
2241 flags |= MSG_DONTWAIT;
Al Viro2da62902015-03-14 21:13:46 -04002242 err = sock_recvmsg(sock, &msg, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002244 if (err >= 0 && addr != NULL) {
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00002245 err2 = move_addr_to_user(&address,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07002246 msg.msg_namelen, addr, addr_len);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002247 if (err2 < 0)
2248 err = err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 }
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08002250
2251 fput_light(sock->file, fput_needed);
David S. Miller4387ff752007-02-08 15:06:08 -08002252out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 return err;
2254}
2255
Dominik Brodowski7a09e1e2018-03-13 19:10:06 +01002256SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, ubuf, size_t, size,
2257 unsigned int, flags, struct sockaddr __user *, addr,
2258 int __user *, addr_len)
2259{
2260 return __sys_recvfrom(fd, ubuf, size, flags, addr, addr_len);
2261}
2262
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002264 * Receive a datagram from a socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 */
2266
Jan Glauberb7c0ddf2014-04-16 09:32:48 +02002267SYSCALL_DEFINE4(recv, int, fd, void __user *, ubuf, size_t, size,
2268 unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269{
Dominik Brodowski7a09e1e2018-03-13 19:10:06 +01002270 return __sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271}
2272
Florian Westphal83f0c102020-07-05 01:30:15 +02002273static bool sock_use_custom_sol_socket(const struct socket *sock)
2274{
Paolo Abenia5ef0582022-10-20 19:48:51 +02002275 return test_bit(SOCK_CUSTOM_SOCKOPT, &sock->flags);
Florian Westphal83f0c102020-07-05 01:30:15 +02002276}
2277
Breno Leitao14062452023-10-16 06:47:41 -07002278int do_sock_setsockopt(struct socket *sock, bool compat, int level,
2279 int optname, sockptr_t optval, int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280{
Eric Dumazet1ded5e52023-08-08 13:58:09 +00002281 const struct proto_ops *ops;
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07002282 char *kernel_optval = NULL;
Breno Leitao14062452023-10-16 06:47:41 -07002283 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284
2285 if (optlen < 0)
2286 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002287
Christoph Hellwig4a367292020-07-17 08:23:11 +02002288 err = security_socket_setsockopt(sock, level, optname);
2289 if (err)
2290 goto out_put;
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07002291
Breno Leitao14062452023-10-16 06:47:41 -07002292 if (!compat)
Christoph Hellwig55db9c02020-07-17 08:23:15 +02002293 err = BPF_CGROUP_RUN_PROG_SETSOCKOPT(sock->sk, &level, &optname,
Breno Leitao3f31e0d2023-10-16 06:47:40 -07002294 optval, &optlen,
Christoph Hellwig55db9c02020-07-17 08:23:15 +02002295 &kernel_optval);
Christoph Hellwig4a367292020-07-17 08:23:11 +02002296 if (err < 0)
2297 goto out_put;
2298 if (err > 0) {
2299 err = 0;
2300 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 }
Christoph Hellwig4a367292020-07-17 08:23:11 +02002302
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02002303 if (kernel_optval)
2304 optval = KERNEL_SOCKPTR(kernel_optval);
Eric Dumazet1ded5e52023-08-08 13:58:09 +00002305 ops = READ_ONCE(sock->ops);
Christoph Hellwig4a367292020-07-17 08:23:11 +02002306 if (level == SOL_SOCKET && !sock_use_custom_sol_socket(sock))
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02002307 err = sock_setsockopt(sock, level, optname, optval, optlen);
Eric Dumazet1ded5e52023-08-08 13:58:09 +00002308 else if (unlikely(!ops->setsockopt))
Christoph Hellwiga44d9e72020-07-17 08:23:31 +02002309 err = -EOPNOTSUPP;
Christoph Hellwig4a367292020-07-17 08:23:11 +02002310 else
Eric Dumazet1ded5e52023-08-08 13:58:09 +00002311 err = ops->setsockopt(sock, level, optname, optval,
Christoph Hellwig4a367292020-07-17 08:23:11 +02002312 optlen);
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02002313 kfree(kernel_optval);
Christoph Hellwig4a367292020-07-17 08:23:11 +02002314out_put:
Breno Leitao14062452023-10-16 06:47:41 -07002315 return err;
2316}
2317EXPORT_SYMBOL(do_sock_setsockopt);
2318
2319/* Set a socket option. Because we don't know the option lengths we have
2320 * to pass the user mode parameter for the protocols to sort out.
2321 */
2322int __sys_setsockopt(int fd, int level, int optname, char __user *user_optval,
2323 int optlen)
2324{
2325 sockptr_t optval = USER_SOCKPTR(user_optval);
2326 bool compat = in_compat_syscall();
2327 int err, fput_needed;
2328 struct socket *sock;
2329
2330 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2331 if (!sock)
2332 return err;
2333
2334 err = do_sock_setsockopt(sock, compat, level, optname, optval, optlen);
2335
Christoph Hellwig4a367292020-07-17 08:23:11 +02002336 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 return err;
2338}
2339
Dominik Brodowskicc36dca2018-03-13 20:10:59 +01002340SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname,
2341 char __user *, optval, int, optlen)
2342{
2343 return __sys_setsockopt(fd, level, optname, optval, optlen);
2344}
2345
Stanislav Fomichev9cacf812021-01-15 08:34:59 -08002346INDIRECT_CALLABLE_DECLARE(bool tcp_bpf_bypass_getsockopt(int level,
2347 int optname));
2348
Breno Leitao0b05b0c2023-10-16 06:47:42 -07002349int do_sock_getsockopt(struct socket *sock, bool compat, int level,
2350 int optname, sockptr_t optval, sockptr_t optlen)
2351{
2352 int max_optlen __maybe_unused;
2353 const struct proto_ops *ops;
2354 int err;
2355
2356 err = security_socket_getsockopt(sock, level, optname);
2357 if (err)
2358 return err;
2359
2360 if (!compat)
2361 max_optlen = BPF_CGROUP_GETSOCKOPT_MAX_OPTLEN(optlen);
2362
2363 ops = READ_ONCE(sock->ops);
2364 if (level == SOL_SOCKET) {
2365 err = sk_getsockopt(sock->sk, level, optname, optval, optlen);
2366 } else if (unlikely(!ops->getsockopt)) {
2367 err = -EOPNOTSUPP;
2368 } else {
2369 if (WARN_ONCE(optval.is_kernel || optlen.is_kernel,
2370 "Invalid argument type"))
2371 return -EOPNOTSUPP;
2372
2373 err = ops->getsockopt(sock, level, optname, optval.user,
2374 optlen.user);
2375 }
2376
2377 if (!compat)
2378 err = BPF_CGROUP_RUN_PROG_GETSOCKOPT(sock->sk, level, optname,
2379 optval, optlen, max_optlen,
2380 err);
2381
2382 return err;
2383}
2384EXPORT_SYMBOL(do_sock_getsockopt);
2385
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386/*
2387 * Get a socket option. Because we don't know the option lengths we have
2388 * to pass a user mode parameter for the protocols to sort out.
2389 */
Christoph Hellwig55db9c02020-07-17 08:23:15 +02002390int __sys_getsockopt(int fd, int level, int optname, char __user *optval,
2391 int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002393 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 struct socket *sock;
Breno Leitao0b05b0c2023-10-16 06:47:42 -07002395 bool compat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002397 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Christoph Hellwigd8a9b382020-07-17 08:23:12 +02002398 if (!sock)
2399 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400
Breno Leitao0b05b0c2023-10-16 06:47:42 -07002401 compat = in_compat_syscall();
2402 err = do_sock_getsockopt(sock, compat, level, optname,
2403 USER_SOCKPTR(optval), USER_SOCKPTR(optlen));
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07002404
Christoph Hellwigd8a9b382020-07-17 08:23:12 +02002405 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 return err;
2407}
2408
Dominik Brodowski13a2d702018-03-13 20:15:04 +01002409SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname,
2410 char __user *, optval, int __user *, optlen)
2411{
2412 return __sys_getsockopt(fd, level, optname, optval, optlen);
2413}
2414
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415/*
2416 * Shutdown a socket.
2417 */
2418
Jens Axboeb713c192020-09-05 11:13:35 -06002419int __sys_shutdown_sock(struct socket *sock, int how)
2420{
2421 int err;
2422
2423 err = security_socket_shutdown(sock, how);
2424 if (!err)
Eric Dumazet1ded5e52023-08-08 13:58:09 +00002425 err = READ_ONCE(sock->ops)->shutdown(sock, how);
Jens Axboeb713c192020-09-05 11:13:35 -06002426
2427 return err;
2428}
2429
Dominik Brodowski005a1ae2018-03-13 20:07:05 +01002430int __sys_shutdown(int fd, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002432 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 struct socket *sock;
2434
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002435 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2436 if (sock != NULL) {
Jens Axboeb713c192020-09-05 11:13:35 -06002437 err = __sys_shutdown_sock(sock, how);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002438 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 }
2440 return err;
2441}
2442
Dominik Brodowski005a1ae2018-03-13 20:07:05 +01002443SYSCALL_DEFINE2(shutdown, int, fd, int, how)
2444{
2445 return __sys_shutdown(fd, how);
2446}
2447
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002448/* A couple of helpful macros for getting the address of the 32/64 bit
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 * fields which are the same type (int / unsigned) on our platforms.
2450 */
2451#define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
2452#define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
2453#define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
2454
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002455struct used_address {
2456 struct sockaddr_storage name;
2457 unsigned int name_len;
2458};
2459
Dylan Yudaken7fa875b2022-07-14 04:02:56 -07002460int __copy_msghdr(struct msghdr *kmsg,
2461 struct user_msghdr *msg,
2462 struct sockaddr __user **save_addr)
Dan Carpenter1661bf32013-10-03 00:27:20 +03002463{
Al Viro08adb7d2014-11-10 20:23:13 -05002464 ssize_t err;
2465
Christoph Hellwig1f466e12020-05-11 13:59:13 +02002466 kmsg->msg_control_is_user = true;
Eric Dumazet1228b342022-06-22 15:02:20 +00002467 kmsg->msg_get_inq = 0;
Dylan Yudaken7fa875b2022-07-14 04:02:56 -07002468 kmsg->msg_control_user = msg->msg_control;
2469 kmsg->msg_controllen = msg->msg_controllen;
2470 kmsg->msg_flags = msg->msg_flags;
Al Viroffb07552017-06-27 19:32:04 -04002471
Dylan Yudaken7fa875b2022-07-14 04:02:56 -07002472 kmsg->msg_namelen = msg->msg_namelen;
2473 if (!msg->msg_name)
Ani Sinha6a2a2b32014-09-08 14:49:59 -07002474 kmsg->msg_namelen = 0;
2475
Matthew Leachdbb490b2014-03-11 11:58:27 +00002476 if (kmsg->msg_namelen < 0)
2477 return -EINVAL;
2478
Dan Carpenter1661bf32013-10-03 00:27:20 +03002479 if (kmsg->msg_namelen > sizeof(struct sockaddr_storage))
Dan Carpenterdb31c552013-11-27 15:40:21 +03002480 kmsg->msg_namelen = sizeof(struct sockaddr_storage);
Al Viro08adb7d2014-11-10 20:23:13 -05002481
2482 if (save_addr)
Dylan Yudaken7fa875b2022-07-14 04:02:56 -07002483 *save_addr = msg->msg_name;
Al Viro08adb7d2014-11-10 20:23:13 -05002484
Dylan Yudaken7fa875b2022-07-14 04:02:56 -07002485 if (msg->msg_name && kmsg->msg_namelen) {
Al Viro08adb7d2014-11-10 20:23:13 -05002486 if (!save_addr) {
Dylan Yudaken7fa875b2022-07-14 04:02:56 -07002487 err = move_addr_to_kernel(msg->msg_name,
Paolo Abeni864d9662017-07-21 18:49:45 +02002488 kmsg->msg_namelen,
Al Viro08adb7d2014-11-10 20:23:13 -05002489 kmsg->msg_name);
2490 if (err < 0)
2491 return err;
2492 }
2493 } else {
2494 kmsg->msg_name = NULL;
2495 kmsg->msg_namelen = 0;
2496 }
2497
Dylan Yudaken7fa875b2022-07-14 04:02:56 -07002498 if (msg->msg_iovlen > UIO_MAXIOV)
Al Viro08adb7d2014-11-10 20:23:13 -05002499 return -EMSGSIZE;
2500
tadeusz.struk@intel.com0345f932015-03-19 12:31:25 -07002501 kmsg->msg_iocb = NULL;
Pavel Begunkov7c701d92022-07-12 21:52:29 +01002502 kmsg->msg_ubuf = NULL;
Jens Axboe0a384ab2020-02-27 08:11:20 -07002503 return 0;
2504}
2505
2506static int copy_msghdr_from_user(struct msghdr *kmsg,
2507 struct user_msghdr __user *umsg,
2508 struct sockaddr __user **save_addr,
2509 struct iovec **iov)
2510{
2511 struct user_msghdr msg;
2512 ssize_t err;
2513
Dylan Yudaken7fa875b2022-07-14 04:02:56 -07002514 if (copy_from_user(&msg, umsg, sizeof(*umsg)))
2515 return -EFAULT;
2516
2517 err = __copy_msghdr(kmsg, &msg, save_addr);
Jens Axboe0a384ab2020-02-27 08:11:20 -07002518 if (err)
2519 return err;
tadeusz.struk@intel.com0345f932015-03-19 12:31:25 -07002520
Al Virode4eda92022-09-15 20:25:47 -04002521 err = import_iovec(save_addr ? ITER_DEST : ITER_SOURCE,
Al Viroffb07552017-06-27 19:32:04 -04002522 msg.msg_iov, msg.msg_iovlen,
Al Viroda184282015-03-21 19:29:06 -04002523 UIO_FASTIOV, iov, &kmsg->msg_iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06002524 return err < 0 ? err : 0;
Dan Carpenter1661bf32013-10-03 00:27:20 +03002525}
2526
Jens Axboe4257c8c2019-11-25 14:27:34 -07002527static int ____sys_sendmsg(struct socket *sock, struct msghdr *msg_sys,
2528 unsigned int flags, struct used_address *used_address,
2529 unsigned int allowed_msghdr_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530{
Alex Williamsonb9d717a2005-09-26 14:28:02 -07002531 unsigned char ctl[sizeof(struct cmsghdr) + 20]
Amit Kushwaha846cc122016-12-08 18:21:53 +05302532 __aligned(sizeof(__kernel_size_t));
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002533 /* 20 is size of ipv6_pktinfo */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 unsigned char *ctl_buf = ctl;
Al Virod8725c82014-12-11 00:02:50 -05002535 int ctl_len;
Al Viro08adb7d2014-11-10 20:23:13 -05002536 ssize_t err;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002537
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 err = -ENOBUFS;
2539
Anton Blanchard228e5482011-05-02 20:21:35 +00002540 if (msg_sys->msg_controllen > INT_MAX)
Jens Axboe4257c8c2019-11-25 14:27:34 -07002541 goto out;
Tom Herbert28a94d82016-03-07 14:11:02 -08002542 flags |= (msg_sys->msg_flags & allowed_msghdr_flags);
Anton Blanchard228e5482011-05-02 20:21:35 +00002543 ctl_len = msg_sys->msg_controllen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002545 err =
Anton Blanchard228e5482011-05-02 20:21:35 +00002546 cmsghdr_from_user_compat_to_kern(msg_sys, sock->sk, ctl,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002547 sizeof(ctl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 if (err)
Jens Axboe4257c8c2019-11-25 14:27:34 -07002549 goto out;
Anton Blanchard228e5482011-05-02 20:21:35 +00002550 ctl_buf = msg_sys->msg_control;
2551 ctl_len = msg_sys->msg_controllen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 } else if (ctl_len) {
David S. Millerac4340f2017-01-04 13:24:19 -05002553 BUILD_BUG_ON(sizeof(struct cmsghdr) !=
2554 CMSG_ALIGN(sizeof(struct cmsghdr)));
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002555 if (ctl_len > sizeof(ctl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002557 if (ctl_buf == NULL)
Jens Axboe4257c8c2019-11-25 14:27:34 -07002558 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 }
2560 err = -EFAULT;
Christoph Hellwig1f466e12020-05-11 13:59:13 +02002561 if (copy_from_user(ctl_buf, msg_sys->msg_control_user, ctl_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 goto out_freectl;
Anton Blanchard228e5482011-05-02 20:21:35 +00002563 msg_sys->msg_control = ctl_buf;
Christoph Hellwig1f466e12020-05-11 13:59:13 +02002564 msg_sys->msg_control_is_user = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 }
David Howellsb841b902023-05-22 13:11:10 +01002566 flags &= ~MSG_INTERNAL_SENDMSG_FLAGS;
Anton Blanchard228e5482011-05-02 20:21:35 +00002567 msg_sys->msg_flags = flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568
2569 if (sock->file->f_flags & O_NONBLOCK)
Anton Blanchard228e5482011-05-02 20:21:35 +00002570 msg_sys->msg_flags |= MSG_DONTWAIT;
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002571 /*
2572 * If this is sendmmsg() and current destination address is same as
2573 * previously succeeded address, omit asking LSM's decision.
2574 * used_address->name_len is initialized to UINT_MAX so that the first
2575 * destination address never matches.
2576 */
Mathieu Desnoyersbc909d92011-08-24 19:45:03 -07002577 if (used_address && msg_sys->msg_name &&
2578 used_address->name_len == msg_sys->msg_namelen &&
2579 !memcmp(&used_address->name, msg_sys->msg_name,
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002580 used_address->name_len)) {
Al Virod8725c82014-12-11 00:02:50 -05002581 err = sock_sendmsg_nosec(sock, msg_sys);
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002582 goto out_freectl;
2583 }
Jordan Rife86a7e0b2023-09-21 18:46:41 -05002584 err = __sock_sendmsg(sock, msg_sys);
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002585 /*
2586 * If this is sendmmsg() and sending to current destination address was
2587 * successful, remember it.
2588 */
2589 if (used_address && err >= 0) {
2590 used_address->name_len = msg_sys->msg_namelen;
Mathieu Desnoyersbc909d92011-08-24 19:45:03 -07002591 if (msg_sys->msg_name)
2592 memcpy(&used_address->name, msg_sys->msg_name,
2593 used_address->name_len);
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002594 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595
2596out_freectl:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002597 if (ctl_buf != ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 sock_kfree_s(sock->sk, ctl_buf, ctl_len);
Jens Axboe4257c8c2019-11-25 14:27:34 -07002599out:
2600 return err;
2601}
2602
Jens Axboe03b12302019-12-02 18:50:25 -07002603int sendmsg_copy_msghdr(struct msghdr *msg,
2604 struct user_msghdr __user *umsg, unsigned flags,
2605 struct iovec **iov)
Jens Axboe4257c8c2019-11-25 14:27:34 -07002606{
2607 int err;
2608
2609 if (flags & MSG_CMSG_COMPAT) {
2610 struct compat_msghdr __user *msg_compat;
2611
2612 msg_compat = (struct compat_msghdr __user *) umsg;
2613 err = get_compat_msghdr(msg, msg_compat, NULL, iov);
2614 } else {
2615 err = copy_msghdr_from_user(msg, umsg, NULL, iov);
2616 }
2617 if (err < 0)
2618 return err;
2619
2620 return 0;
2621}
2622
2623static int ___sys_sendmsg(struct socket *sock, struct user_msghdr __user *msg,
2624 struct msghdr *msg_sys, unsigned int flags,
2625 struct used_address *used_address,
2626 unsigned int allowed_msghdr_flags)
2627{
2628 struct sockaddr_storage address;
2629 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
2630 ssize_t err;
2631
2632 msg_sys->msg_name = &address;
2633
2634 err = sendmsg_copy_msghdr(msg_sys, msg, flags, &iov);
2635 if (err < 0)
2636 return err;
2637
2638 err = ____sys_sendmsg(sock, msg_sys, flags, used_address,
2639 allowed_msghdr_flags);
Al Viroda184282015-03-21 19:29:06 -04002640 kfree(iov);
Anton Blanchard228e5482011-05-02 20:21:35 +00002641 return err;
2642}
2643
2644/*
2645 * BSD sendmsg interface
2646 */
Jens Axboe03b12302019-12-02 18:50:25 -07002647long __sys_sendmsg_sock(struct socket *sock, struct msghdr *msg,
Jens Axboe0fa03c62019-04-19 13:34:07 -06002648 unsigned int flags)
2649{
Jens Axboe03b12302019-12-02 18:50:25 -07002650 return ____sys_sendmsg(sock, msg, flags, NULL, 0);
Jens Axboe0fa03c62019-04-19 13:34:07 -06002651}
Anton Blanchard228e5482011-05-02 20:21:35 +00002652
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002653long __sys_sendmsg(int fd, struct user_msghdr __user *msg, unsigned int flags,
2654 bool forbid_cmsg_compat)
Anton Blanchard228e5482011-05-02 20:21:35 +00002655{
2656 int fput_needed, err;
2657 struct msghdr msg_sys;
Andy Lutomirski1be374a2013-05-22 14:07:44 -07002658 struct socket *sock;
Anton Blanchard228e5482011-05-02 20:21:35 +00002659
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002660 if (forbid_cmsg_compat && (flags & MSG_CMSG_COMPAT))
2661 return -EINVAL;
2662
Andy Lutomirski1be374a2013-05-22 14:07:44 -07002663 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Anton Blanchard228e5482011-05-02 20:21:35 +00002664 if (!sock)
2665 goto out;
2666
Tom Herbert28a94d82016-03-07 14:11:02 -08002667 err = ___sys_sendmsg(sock, msg, &msg_sys, flags, NULL, 0);
Anton Blanchard228e5482011-05-02 20:21:35 +00002668
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002669 fput_light(sock->file, fput_needed);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002670out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 return err;
2672}
2673
Al Viro666547f2014-04-06 14:03:05 -04002674SYSCALL_DEFINE3(sendmsg, int, fd, struct user_msghdr __user *, msg, unsigned int, flags)
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002675{
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002676 return __sys_sendmsg(fd, msg, flags, true);
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002677}
2678
Anton Blanchard228e5482011-05-02 20:21:35 +00002679/*
2680 * Linux sendmmsg interface
2681 */
2682
2683int __sys_sendmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002684 unsigned int flags, bool forbid_cmsg_compat)
Anton Blanchard228e5482011-05-02 20:21:35 +00002685{
2686 int fput_needed, err, datagrams;
2687 struct socket *sock;
2688 struct mmsghdr __user *entry;
2689 struct compat_mmsghdr __user *compat_entry;
2690 struct msghdr msg_sys;
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002691 struct used_address used_address;
Tom Herbertf0922762016-03-07 14:11:03 -08002692 unsigned int oflags = flags;
Anton Blanchard228e5482011-05-02 20:21:35 +00002693
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002694 if (forbid_cmsg_compat && (flags & MSG_CMSG_COMPAT))
2695 return -EINVAL;
2696
Anton Blanchard98382f42011-08-04 14:07:39 +00002697 if (vlen > UIO_MAXIOV)
2698 vlen = UIO_MAXIOV;
Anton Blanchard228e5482011-05-02 20:21:35 +00002699
2700 datagrams = 0;
2701
2702 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2703 if (!sock)
2704 return err;
2705
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002706 used_address.name_len = UINT_MAX;
Anton Blanchard228e5482011-05-02 20:21:35 +00002707 entry = mmsg;
2708 compat_entry = (struct compat_mmsghdr __user *)mmsg;
Anton Blanchard728ffb82011-08-04 14:07:38 +00002709 err = 0;
Tom Herbertf0922762016-03-07 14:11:03 -08002710 flags |= MSG_BATCH;
Anton Blanchard228e5482011-05-02 20:21:35 +00002711
2712 while (datagrams < vlen) {
Tom Herbertf0922762016-03-07 14:11:03 -08002713 if (datagrams == vlen - 1)
2714 flags = oflags;
2715
Anton Blanchard228e5482011-05-02 20:21:35 +00002716 if (MSG_CMSG_COMPAT & flags) {
Al Viro666547f2014-04-06 14:03:05 -04002717 err = ___sys_sendmsg(sock, (struct user_msghdr __user *)compat_entry,
Tom Herbert28a94d82016-03-07 14:11:02 -08002718 &msg_sys, flags, &used_address, MSG_EOR);
Anton Blanchard228e5482011-05-02 20:21:35 +00002719 if (err < 0)
2720 break;
2721 err = __put_user(err, &compat_entry->msg_len);
2722 ++compat_entry;
2723 } else {
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002724 err = ___sys_sendmsg(sock,
Al Viro666547f2014-04-06 14:03:05 -04002725 (struct user_msghdr __user *)entry,
Tom Herbert28a94d82016-03-07 14:11:02 -08002726 &msg_sys, flags, &used_address, MSG_EOR);
Anton Blanchard228e5482011-05-02 20:21:35 +00002727 if (err < 0)
2728 break;
2729 err = put_user(err, &entry->msg_len);
2730 ++entry;
2731 }
2732
2733 if (err)
2734 break;
2735 ++datagrams;
Soheil Hassas Yeganeh30238982016-11-04 15:36:49 -04002736 if (msg_data_left(&msg_sys))
2737 break;
Eric Dumazeta78cb842016-01-08 08:37:20 -08002738 cond_resched();
Anton Blanchard228e5482011-05-02 20:21:35 +00002739 }
2740
Anton Blanchard228e5482011-05-02 20:21:35 +00002741 fput_light(sock->file, fput_needed);
2742
Anton Blanchard728ffb82011-08-04 14:07:38 +00002743 /* We only return an error if no datagrams were able to be sent */
2744 if (datagrams != 0)
Anton Blanchard228e5482011-05-02 20:21:35 +00002745 return datagrams;
2746
Anton Blanchard228e5482011-05-02 20:21:35 +00002747 return err;
2748}
2749
2750SYSCALL_DEFINE4(sendmmsg, int, fd, struct mmsghdr __user *, mmsg,
2751 unsigned int, vlen, unsigned int, flags)
2752{
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002753 return __sys_sendmmsg(fd, mmsg, vlen, flags, true);
Anton Blanchard228e5482011-05-02 20:21:35 +00002754}
2755
Jens Axboe03b12302019-12-02 18:50:25 -07002756int recvmsg_copy_msghdr(struct msghdr *msg,
2757 struct user_msghdr __user *umsg, unsigned flags,
2758 struct sockaddr __user **uaddr,
2759 struct iovec **iov)
Jens Axboe4257c8c2019-11-25 14:27:34 -07002760{
2761 ssize_t err;
2762
2763 if (MSG_CMSG_COMPAT & flags) {
2764 struct compat_msghdr __user *msg_compat;
2765
2766 msg_compat = (struct compat_msghdr __user *) umsg;
2767 err = get_compat_msghdr(msg, msg_compat, uaddr, iov);
2768 } else {
2769 err = copy_msghdr_from_user(msg, umsg, uaddr, iov);
2770 }
2771 if (err < 0)
2772 return err;
2773
2774 return 0;
2775}
2776
2777static int ____sys_recvmsg(struct socket *sock, struct msghdr *msg_sys,
2778 struct user_msghdr __user *msg,
2779 struct sockaddr __user *uaddr,
2780 unsigned int flags, int nosec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781{
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002782 struct compat_msghdr __user *msg_compat =
Jens Axboe4257c8c2019-11-25 14:27:34 -07002783 (struct compat_msghdr __user *) msg;
2784 int __user *uaddr_len = COMPAT_NAMELEN(msg);
2785 struct sockaddr_storage addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 unsigned long cmsg_ptr;
Al Viro2da62902015-03-14 21:13:46 -04002787 int len;
Al Viro08adb7d2014-11-10 20:23:13 -05002788 ssize_t err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789
Al Viro08adb7d2014-11-10 20:23:13 -05002790 msg_sys->msg_name = &addr;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002791 cmsg_ptr = (unsigned long)msg_sys->msg_control;
2792 msg_sys->msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002793
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01002794 /* We assume all kernel code knows the size of sockaddr_storage */
2795 msg_sys->msg_namelen = 0;
2796
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 if (sock->file->f_flags & O_NONBLOCK)
2798 flags |= MSG_DONTWAIT;
Eric Dumazet1af66222019-12-06 09:38:36 -08002799
2800 if (unlikely(nosec))
2801 err = sock_recvmsg_nosec(sock, msg_sys, flags);
2802 else
2803 err = sock_recvmsg(sock, msg_sys, flags);
2804
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 if (err < 0)
Jens Axboe4257c8c2019-11-25 14:27:34 -07002806 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 len = err;
2808
2809 if (uaddr != NULL) {
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00002810 err = move_addr_to_user(&addr,
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002811 msg_sys->msg_namelen, uaddr,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002812 uaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 if (err < 0)
Jens Axboe4257c8c2019-11-25 14:27:34 -07002814 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 }
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002816 err = __put_user((msg_sys->msg_flags & ~MSG_CMSG_COMPAT),
David S. Miller37f7f422005-09-16 16:51:01 -07002817 COMPAT_FLAGS(msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 if (err)
Jens Axboe4257c8c2019-11-25 14:27:34 -07002819 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 if (MSG_CMSG_COMPAT & flags)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002821 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 &msg_compat->msg_controllen);
2823 else
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002824 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 &msg->msg_controllen);
2826 if (err)
Jens Axboe4257c8c2019-11-25 14:27:34 -07002827 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 err = len;
Jens Axboe4257c8c2019-11-25 14:27:34 -07002829out:
2830 return err;
2831}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832
Jens Axboe4257c8c2019-11-25 14:27:34 -07002833static int ___sys_recvmsg(struct socket *sock, struct user_msghdr __user *msg,
2834 struct msghdr *msg_sys, unsigned int flags, int nosec)
2835{
2836 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
2837 /* user mode address pointers */
2838 struct sockaddr __user *uaddr;
2839 ssize_t err;
2840
2841 err = recvmsg_copy_msghdr(msg_sys, msg, flags, &uaddr, &iov);
2842 if (err < 0)
2843 return err;
2844
2845 err = ____sys_recvmsg(sock, msg_sys, msg, uaddr, flags, nosec);
Al Viroda184282015-03-21 19:29:06 -04002846 kfree(iov);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002847 return err;
2848}
2849
2850/*
2851 * BSD recvmsg interface
2852 */
2853
Jens Axboe03b12302019-12-02 18:50:25 -07002854long __sys_recvmsg_sock(struct socket *sock, struct msghdr *msg,
2855 struct user_msghdr __user *umsg,
2856 struct sockaddr __user *uaddr, unsigned int flags)
Jens Axboeaa1fa282019-04-19 13:38:09 -06002857{
Jens Axboe03b12302019-12-02 18:50:25 -07002858 return ____sys_recvmsg(sock, msg, umsg, uaddr, flags, 0);
Jens Axboeaa1fa282019-04-19 13:38:09 -06002859}
2860
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002861long __sys_recvmsg(int fd, struct user_msghdr __user *msg, unsigned int flags,
2862 bool forbid_cmsg_compat)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002863{
2864 int fput_needed, err;
2865 struct msghdr msg_sys;
Andy Lutomirski1be374a2013-05-22 14:07:44 -07002866 struct socket *sock;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002867
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002868 if (forbid_cmsg_compat && (flags & MSG_CMSG_COMPAT))
2869 return -EINVAL;
2870
Andy Lutomirski1be374a2013-05-22 14:07:44 -07002871 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002872 if (!sock)
2873 goto out;
2874
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002875 err = ___sys_recvmsg(sock, msg, &msg_sys, flags, 0);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002876
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002877 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878out:
2879 return err;
2880}
2881
Al Viro666547f2014-04-06 14:03:05 -04002882SYSCALL_DEFINE3(recvmsg, int, fd, struct user_msghdr __user *, msg,
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002883 unsigned int, flags)
2884{
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002885 return __sys_recvmsg(fd, msg, flags, true);
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002886}
2887
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002888/*
2889 * Linux recvmmsg interface
2890 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891
Arnd Bergmanne11d4282018-04-18 13:43:52 +02002892static int do_recvmmsg(int fd, struct mmsghdr __user *mmsg,
2893 unsigned int vlen, unsigned int flags,
2894 struct timespec64 *timeout)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002895{
2896 int fput_needed, err, datagrams;
2897 struct socket *sock;
2898 struct mmsghdr __user *entry;
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002899 struct compat_mmsghdr __user *compat_entry;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002900 struct msghdr msg_sys;
Deepa Dinamani766b9f92016-05-19 17:09:05 -07002901 struct timespec64 end_time;
2902 struct timespec64 timeout64;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002903
2904 if (timeout &&
2905 poll_select_set_timeout(&end_time, timeout->tv_sec,
2906 timeout->tv_nsec))
2907 return -EINVAL;
2908
2909 datagrams = 0;
2910
2911 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2912 if (!sock)
2913 return err;
2914
Soheil Hassas Yeganeh7797dc42018-02-27 18:22:40 -05002915 if (likely(!(flags & MSG_ERRQUEUE))) {
2916 err = sock_error(sock->sk);
2917 if (err) {
2918 datagrams = err;
2919 goto out_put;
2920 }
Maxime Jayate623a9e2017-02-21 18:35:51 +01002921 }
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002922
2923 entry = mmsg;
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002924 compat_entry = (struct compat_mmsghdr __user *)mmsg;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002925
2926 while (datagrams < vlen) {
2927 /*
2928 * No need to ask LSM for more than the first datagram.
2929 */
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002930 if (MSG_CMSG_COMPAT & flags) {
Al Viro666547f2014-04-06 14:03:05 -04002931 err = ___sys_recvmsg(sock, (struct user_msghdr __user *)compat_entry,
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002932 &msg_sys, flags & ~MSG_WAITFORONE,
2933 datagrams);
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002934 if (err < 0)
2935 break;
2936 err = __put_user(err, &compat_entry->msg_len);
2937 ++compat_entry;
2938 } else {
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002939 err = ___sys_recvmsg(sock,
Al Viro666547f2014-04-06 14:03:05 -04002940 (struct user_msghdr __user *)entry,
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002941 &msg_sys, flags & ~MSG_WAITFORONE,
2942 datagrams);
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002943 if (err < 0)
2944 break;
2945 err = put_user(err, &entry->msg_len);
2946 ++entry;
2947 }
2948
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002949 if (err)
2950 break;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002951 ++datagrams;
2952
Brandon L Black71c5c1592010-03-26 16:18:03 +00002953 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
2954 if (flags & MSG_WAITFORONE)
2955 flags |= MSG_DONTWAIT;
2956
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002957 if (timeout) {
Deepa Dinamani766b9f92016-05-19 17:09:05 -07002958 ktime_get_ts64(&timeout64);
Arnd Bergmannc2e6c852018-04-18 13:42:25 +02002959 *timeout = timespec64_sub(end_time, timeout64);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002960 if (timeout->tv_sec < 0) {
2961 timeout->tv_sec = timeout->tv_nsec = 0;
2962 break;
2963 }
2964
2965 /* Timeout, return less than vlen datagrams */
2966 if (timeout->tv_nsec == 0 && timeout->tv_sec == 0)
2967 break;
2968 }
2969
2970 /* Out of band data, return right away */
2971 if (msg_sys.msg_flags & MSG_OOB)
2972 break;
Eric Dumazeta78cb842016-01-08 08:37:20 -08002973 cond_resched();
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002974 }
2975
Arnaldo Carvalho de Melo34b88a62016-03-14 09:56:35 -03002976 if (err == 0)
2977 goto out_put;
2978
2979 if (datagrams == 0) {
2980 datagrams = err;
2981 goto out_put;
2982 }
2983
2984 /*
2985 * We may return less entries than requested (vlen) if the
2986 * sock is non block and there aren't enough datagrams...
2987 */
2988 if (err != -EAGAIN) {
2989 /*
2990 * ... or if recvmsg returns an error after we
2991 * received some datagrams, where we record the
2992 * error to return on the next call or if the
2993 * app asks about it using getsockopt(SO_ERROR).
2994 */
Eric Dumazete05a5f52023-05-09 16:35:53 +00002995 WRITE_ONCE(sock->sk->sk_err, -err);
Arnaldo Carvalho de Melo34b88a62016-03-14 09:56:35 -03002996 }
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002997out_put:
2998 fput_light(sock->file, fput_needed);
2999
Arnaldo Carvalho de Melo34b88a62016-03-14 09:56:35 -03003000 return datagrams;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07003001}
3002
Arnd Bergmanne11d4282018-04-18 13:43:52 +02003003int __sys_recvmmsg(int fd, struct mmsghdr __user *mmsg,
3004 unsigned int vlen, unsigned int flags,
3005 struct __kernel_timespec __user *timeout,
3006 struct old_timespec32 __user *timeout32)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07003007{
3008 int datagrams;
Arnd Bergmannc2e6c852018-04-18 13:42:25 +02003009 struct timespec64 timeout_sys;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07003010
Arnd Bergmanne11d4282018-04-18 13:43:52 +02003011 if (timeout && get_timespec64(&timeout_sys, timeout))
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07003012 return -EFAULT;
3013
Arnd Bergmanne11d4282018-04-18 13:43:52 +02003014 if (timeout32 && get_old_timespec32(&timeout_sys, timeout32))
3015 return -EFAULT;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07003016
Arnd Bergmanne11d4282018-04-18 13:43:52 +02003017 if (!timeout && !timeout32)
3018 return do_recvmmsg(fd, mmsg, vlen, flags, NULL);
3019
3020 datagrams = do_recvmmsg(fd, mmsg, vlen, flags, &timeout_sys);
3021
3022 if (datagrams <= 0)
3023 return datagrams;
3024
3025 if (timeout && put_timespec64(&timeout_sys, timeout))
3026 datagrams = -EFAULT;
3027
3028 if (timeout32 && put_old_timespec32(&timeout_sys, timeout32))
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07003029 datagrams = -EFAULT;
3030
3031 return datagrams;
3032}
3033
Dominik Brodowski1255e262018-03-13 20:44:21 +01003034SYSCALL_DEFINE5(recvmmsg, int, fd, struct mmsghdr __user *, mmsg,
3035 unsigned int, vlen, unsigned int, flags,
Arnd Bergmannc2e6c852018-04-18 13:42:25 +02003036 struct __kernel_timespec __user *, timeout)
Dominik Brodowski1255e262018-03-13 20:44:21 +01003037{
Arnd Bergmanne11d4282018-04-18 13:43:52 +02003038 if (flags & MSG_CMSG_COMPAT)
3039 return -EINVAL;
3040
3041 return __sys_recvmmsg(fd, mmsg, vlen, flags, timeout, NULL);
Dominik Brodowski1255e262018-03-13 20:44:21 +01003042}
3043
Arnd Bergmanne11d4282018-04-18 13:43:52 +02003044#ifdef CONFIG_COMPAT_32BIT_TIME
3045SYSCALL_DEFINE5(recvmmsg_time32, int, fd, struct mmsghdr __user *, mmsg,
3046 unsigned int, vlen, unsigned int, flags,
3047 struct old_timespec32 __user *, timeout)
3048{
3049 if (flags & MSG_CMSG_COMPAT)
3050 return -EINVAL;
3051
3052 return __sys_recvmmsg(fd, mmsg, vlen, flags, NULL, timeout);
3053}
3054#endif
3055
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07003056#ifdef __ARCH_WANT_SYS_SOCKETCALL
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057/* Argument list sizes for sys_socketcall */
3058#define AL(x) ((x) * sizeof(unsigned long))
Anton Blanchard228e5482011-05-02 20:21:35 +00003059static const unsigned char nargs[21] = {
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003060 AL(0), AL(3), AL(3), AL(3), AL(2), AL(3),
3061 AL(3), AL(3), AL(4), AL(4), AL(4), AL(6),
3062 AL(6), AL(2), AL(5), AL(5), AL(3), AL(3),
Anton Blanchard228e5482011-05-02 20:21:35 +00003063 AL(4), AL(5), AL(4)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07003064};
3065
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066#undef AL
3067
3068/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07003069 * System call vectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 *
3071 * Argument checking cleaned up. Saved 20% in size.
3072 * This function doesn't need to set the kernel lock because
Stephen Hemminger89bddce2006-09-01 00:19:31 -07003073 * it is set by the callees.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 */
3075
Heiko Carstens3e0fa652009-01-14 14:14:24 +01003076SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077{
Chen Gang2950fa92013-04-07 16:55:23 +08003078 unsigned long a[AUDITSC_ARGS];
Stephen Hemminger89bddce2006-09-01 00:19:31 -07003079 unsigned long a0, a1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 int err;
Arjan van de Ven47379052009-09-28 12:57:44 -07003081 unsigned int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082
Anton Blanchard228e5482011-05-02 20:21:35 +00003083 if (call < 1 || call > SYS_SENDMMSG)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 return -EINVAL;
Jeremy Clinec8e8cd52018-07-27 22:43:01 +00003085 call = array_index_nospec(call, SYS_SENDMMSG + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086
Arjan van de Ven47379052009-09-28 12:57:44 -07003087 len = nargs[call];
3088 if (len > sizeof(a))
3089 return -EINVAL;
3090
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 /* copy_from_user should be SMP safe. */
Arjan van de Ven47379052009-09-28 12:57:44 -07003092 if (copy_from_user(a, args, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 return -EFAULT;
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01003094
Chen Gang2950fa92013-04-07 16:55:23 +08003095 err = audit_socketcall(nargs[call] / sizeof(unsigned long), a);
3096 if (err)
3097 return err;
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01003098
Stephen Hemminger89bddce2006-09-01 00:19:31 -07003099 a0 = a[0];
3100 a1 = a[1];
3101
3102 switch (call) {
3103 case SYS_SOCKET:
Dominik Brodowski9d6a15c2018-03-13 19:29:43 +01003104 err = __sys_socket(a0, a1, a[2]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07003105 break;
3106 case SYS_BIND:
Dominik Brodowskia87d35d2018-03-13 19:33:09 +01003107 err = __sys_bind(a0, (struct sockaddr __user *)a1, a[2]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07003108 break;
3109 case SYS_CONNECT:
Dominik Brodowski1387c2c2018-03-13 19:35:09 +01003110 err = __sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07003111 break;
3112 case SYS_LISTEN:
Dominik Brodowski25e290e2018-03-13 19:36:54 +01003113 err = __sys_listen(a0, a1);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07003114 break;
3115 case SYS_ACCEPT:
Dominik Brodowski4541e802018-03-13 19:24:23 +01003116 err = __sys_accept4(a0, (struct sockaddr __user *)a1,
3117 (int __user *)a[2], 0);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07003118 break;
3119 case SYS_GETSOCKNAME:
3120 err =
Dominik Brodowski8882a102018-03-13 19:43:14 +01003121 __sys_getsockname(a0, (struct sockaddr __user *)a1,
3122 (int __user *)a[2]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07003123 break;
3124 case SYS_GETPEERNAME:
3125 err =
Dominik Brodowskib21c8f82018-03-13 19:47:00 +01003126 __sys_getpeername(a0, (struct sockaddr __user *)a1,
3127 (int __user *)a[2]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07003128 break;
3129 case SYS_SOCKETPAIR:
Dominik Brodowski6debc8d2018-03-13 19:49:23 +01003130 err = __sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07003131 break;
3132 case SYS_SEND:
Dominik Brodowskif3bf8962018-03-13 19:52:00 +01003133 err = __sys_sendto(a0, (void __user *)a1, a[2], a[3],
3134 NULL, 0);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07003135 break;
3136 case SYS_SENDTO:
Dominik Brodowski211b6342018-03-13 19:18:52 +01003137 err = __sys_sendto(a0, (void __user *)a1, a[2], a[3],
3138 (struct sockaddr __user *)a[4], a[5]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07003139 break;
3140 case SYS_RECV:
Dominik Brodowskid27e9af2018-03-13 19:54:17 +01003141 err = __sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
3142 NULL, NULL);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07003143 break;
3144 case SYS_RECVFROM:
Dominik Brodowski7a09e1e2018-03-13 19:10:06 +01003145 err = __sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
3146 (struct sockaddr __user *)a[4],
3147 (int __user *)a[5]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07003148 break;
3149 case SYS_SHUTDOWN:
Dominik Brodowski005a1ae2018-03-13 20:07:05 +01003150 err = __sys_shutdown(a0, a1);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07003151 break;
3152 case SYS_SETSOCKOPT:
Dominik Brodowskicc36dca2018-03-13 20:10:59 +01003153 err = __sys_setsockopt(a0, a1, a[2], (char __user *)a[3],
3154 a[4]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07003155 break;
3156 case SYS_GETSOCKOPT:
3157 err =
Dominik Brodowski13a2d702018-03-13 20:15:04 +01003158 __sys_getsockopt(a0, a1, a[2], (char __user *)a[3],
3159 (int __user *)a[4]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07003160 break;
3161 case SYS_SENDMSG:
Dominik Brodowskie1834a32018-03-13 20:35:57 +01003162 err = __sys_sendmsg(a0, (struct user_msghdr __user *)a1,
3163 a[2], true);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07003164 break;
Anton Blanchard228e5482011-05-02 20:21:35 +00003165 case SYS_SENDMMSG:
Dominik Brodowskie1834a32018-03-13 20:35:57 +01003166 err = __sys_sendmmsg(a0, (struct mmsghdr __user *)a1, a[2],
3167 a[3], true);
Anton Blanchard228e5482011-05-02 20:21:35 +00003168 break;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07003169 case SYS_RECVMSG:
Dominik Brodowskie1834a32018-03-13 20:35:57 +01003170 err = __sys_recvmsg(a0, (struct user_msghdr __user *)a1,
3171 a[2], true);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07003172 break;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07003173 case SYS_RECVMMSG:
Arnd Bergmann3ca47e92019-04-23 17:43:50 +02003174 if (IS_ENABLED(CONFIG_64BIT))
Arnd Bergmanne11d4282018-04-18 13:43:52 +02003175 err = __sys_recvmmsg(a0, (struct mmsghdr __user *)a1,
3176 a[2], a[3],
3177 (struct __kernel_timespec __user *)a[4],
3178 NULL);
3179 else
3180 err = __sys_recvmmsg(a0, (struct mmsghdr __user *)a1,
3181 a[2], a[3], NULL,
3182 (struct old_timespec32 __user *)a[4]);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07003183 break;
Ulrich Drepperde11def2008-11-19 15:36:14 -08003184 case SYS_ACCEPT4:
Dominik Brodowski4541e802018-03-13 19:24:23 +01003185 err = __sys_accept4(a0, (struct sockaddr __user *)a1,
3186 (int __user *)a[2], a[3]);
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07003187 break;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07003188 default:
3189 err = -EINVAL;
3190 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 }
3192 return err;
3193}
3194
Stephen Hemminger89bddce2006-09-01 00:19:31 -07003195#endif /* __ARCH_WANT_SYS_SOCKETCALL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196
Stephen Hemminger55737fd2006-09-01 00:23:39 -07003197/**
3198 * sock_register - add a socket protocol handler
3199 * @ops: description of protocol
3200 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 * This function is called by a protocol handler that wants to
3202 * advertise its address family, and have it linked into the
Masanari Iidae793c0f2014-09-04 23:44:36 +09003203 * socket interface. The value ops->family corresponds to the
Stephen Hemminger55737fd2006-09-01 00:23:39 -07003204 * socket system call protocol family.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 */
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07003206int sock_register(const struct net_proto_family *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207{
3208 int err;
3209
3210 if (ops->family >= NPROTO) {
Yang Yingliang3410f222014-02-12 17:09:55 +08003211 pr_crit("protocol %d >= NPROTO(%d)\n", ops->family, NPROTO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 return -ENOBUFS;
3213 }
Stephen Hemminger55737fd2006-09-01 00:23:39 -07003214
3215 spin_lock(&net_family_lock);
Eric Dumazet190683a2010-11-10 10:50:44 +00003216 if (rcu_dereference_protected(net_families[ops->family],
3217 lockdep_is_held(&net_family_lock)))
Stephen Hemminger55737fd2006-09-01 00:23:39 -07003218 err = -EEXIST;
3219 else {
Eric Dumazetcf778b02012-01-12 04:41:32 +00003220 rcu_assign_pointer(net_families[ops->family], ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 err = 0;
3222 }
Stephen Hemminger55737fd2006-09-01 00:23:39 -07003223 spin_unlock(&net_family_lock);
3224
Yejune Dengfe0bdbd2021-06-21 13:12:25 +08003225 pr_info("NET: Registered %s protocol family\n", pf_family_names[ops->family]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 return err;
3227}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003228EXPORT_SYMBOL(sock_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229
Stephen Hemminger55737fd2006-09-01 00:23:39 -07003230/**
3231 * sock_unregister - remove a protocol handler
3232 * @family: protocol family to remove
3233 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 * This function is called by a protocol handler that wants to
3235 * remove its address family, and have it unlinked from the
Stephen Hemminger55737fd2006-09-01 00:23:39 -07003236 * new socket creation.
3237 *
3238 * If protocol handler is a module, then it can use module reference
3239 * counts to protect against new references. If protocol handler is not
3240 * a module then it needs to provide its own protection in
3241 * the ops->create routine.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 */
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07003243void sock_unregister(int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244{
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07003245 BUG_ON(family < 0 || family >= NPROTO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246
Stephen Hemminger55737fd2006-09-01 00:23:39 -07003247 spin_lock(&net_family_lock);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00003248 RCU_INIT_POINTER(net_families[family], NULL);
Stephen Hemminger55737fd2006-09-01 00:23:39 -07003249 spin_unlock(&net_family_lock);
3250
3251 synchronize_rcu();
3252
Yejune Dengfe0bdbd2021-06-21 13:12:25 +08003253 pr_info("NET: Unregistered %s protocol family\n", pf_family_names[family]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003255EXPORT_SYMBOL(sock_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256
Xin Longbf2ae2e2018-03-10 18:57:50 +08003257bool sock_is_registered(int family)
3258{
Jeremy Cline66b51b02018-08-13 22:23:13 +00003259 return family < NPROTO && rcu_access_pointer(net_families[family]);
Xin Longbf2ae2e2018-03-10 18:57:50 +08003260}
3261
Andi Kleen77d76ea2005-12-22 12:43:42 -08003262static int __init sock_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263{
Nick Pigginb3e19d92011-01-07 17:50:11 +11003264 int err;
Eric W. Biederman2ca794e2012-04-19 13:20:32 +00003265 /*
3266 * Initialize the network sysctl infrastructure.
3267 */
3268 err = net_sysctl_init();
3269 if (err)
3270 goto out;
Nick Pigginb3e19d92011-01-07 17:50:11 +11003271
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07003273 * Initialize skbuff SLAB cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 */
3275 skb_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276
3277 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07003278 * Initialize the protocols module.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 */
3280
3281 init_inodecache();
Nick Pigginb3e19d92011-01-07 17:50:11 +11003282
3283 err = register_filesystem(&sock_fs_type);
3284 if (err)
Miaohe Lin47260ba2020-08-06 19:54:19 +08003285 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 sock_mnt = kern_mount(&sock_fs_type);
Nick Pigginb3e19d92011-01-07 17:50:11 +11003287 if (IS_ERR(sock_mnt)) {
3288 err = PTR_ERR(sock_mnt);
3289 goto out_mount;
3290 }
Andi Kleen77d76ea2005-12-22 12:43:42 -08003291
3292 /* The real protocol initialization is performed in later initcalls.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 */
3294
3295#ifdef CONFIG_NETFILTER
Pablo Neira Ayuso6d11cfd2013-05-22 22:42:36 +00003296 err = netfilter_init();
3297 if (err)
3298 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299#endif
David S. Millercbeb3212005-12-22 12:58:55 -08003300
Daniel Borkmann408eccc2014-04-01 16:20:23 +02003301 ptp_classifier_init();
Richard Cochranc1f19b52010-07-17 08:49:36 +00003302
Nick Pigginb3e19d92011-01-07 17:50:11 +11003303out:
3304 return err;
3305
3306out_mount:
3307 unregister_filesystem(&sock_fs_type);
Nick Pigginb3e19d92011-01-07 17:50:11 +11003308 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309}
3310
Andi Kleen77d76ea2005-12-22 12:43:42 -08003311core_initcall(sock_init); /* early initcall */
3312
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313#ifdef CONFIG_PROC_FS
3314void socket_seq_show(struct seq_file *seq)
3315{
Tonghao Zhang648845a2017-12-14 05:51:58 -08003316 seq_printf(seq, "sockets: used %d\n",
3317 sock_inuse_get(seq->private));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318}
Stephen Hemminger89bddce2006-09-01 00:19:31 -07003319#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320
Arnd Bergmann29c49642021-07-22 16:29:03 +02003321/* Handle the fact that while struct ifreq has the same *layout* on
3322 * 32/64 for everything but ifreq::ifru_ifmap and ifreq::ifru_data,
3323 * which are handled elsewhere, it still has different *size* due to
3324 * ifreq::ifru_ifmap (which is 16 bytes on 32 bit, 24 bytes on 64-bit,
3325 * resulting in struct ifreq being 32 and 40 bytes respectively).
3326 * As a result, if the struct happens to be at the end of a page and
3327 * the next page isn't readable/writable, we get a fault. To prevent
3328 * that, copy back and forth to the full size.
3329 */
3330int get_user_ifreq(struct ifreq *ifr, void __user **ifrdata, void __user *arg)
3331{
3332 if (in_compat_syscall()) {
3333 struct compat_ifreq *ifr32 = (struct compat_ifreq *)ifr;
3334
3335 memset(ifr, 0, sizeof(*ifr));
3336 if (copy_from_user(ifr32, arg, sizeof(*ifr32)))
3337 return -EFAULT;
3338
3339 if (ifrdata)
3340 *ifrdata = compat_ptr(ifr32->ifr_data);
3341
3342 return 0;
3343 }
3344
3345 if (copy_from_user(ifr, arg, sizeof(*ifr)))
3346 return -EFAULT;
3347
3348 if (ifrdata)
3349 *ifrdata = ifr->ifr_data;
3350
3351 return 0;
3352}
3353EXPORT_SYMBOL(get_user_ifreq);
3354
3355int put_user_ifreq(struct ifreq *ifr, void __user *arg)
3356{
3357 size_t size = sizeof(*ifr);
3358
3359 if (in_compat_syscall())
3360 size = sizeof(struct compat_ifreq);
3361
3362 if (copy_to_user(arg, ifr, size))
3363 return -EFAULT;
3364
3365 return 0;
3366}
3367EXPORT_SYMBOL(put_user_ifreq);
3368
Shaun Pereira89bbfc92006-03-21 23:58:08 -08003369#ifdef CONFIG_COMPAT
Arnd Bergmann7a50a242009-11-08 20:57:03 -08003370static int compat_siocwandev(struct net *net, struct compat_ifreq __user *uifr32)
3371{
Arnd Bergmann7a50a242009-11-08 20:57:03 -08003372 compat_uptr_t uptr32;
Al Viro44c02a22017-10-05 12:59:44 -04003373 struct ifreq ifr;
3374 void __user *saved;
3375 int err;
Arnd Bergmann7a50a242009-11-08 20:57:03 -08003376
Arnd Bergmann29c49642021-07-22 16:29:03 +02003377 if (get_user_ifreq(&ifr, NULL, uifr32))
Arnd Bergmann7a50a242009-11-08 20:57:03 -08003378 return -EFAULT;
3379
3380 if (get_user(uptr32, &uifr32->ifr_settings.ifs_ifsu))
3381 return -EFAULT;
3382
Al Viro44c02a22017-10-05 12:59:44 -04003383 saved = ifr.ifr_settings.ifs_ifsu.raw_hdlc;
3384 ifr.ifr_settings.ifs_ifsu.raw_hdlc = compat_ptr(uptr32);
Arnd Bergmann7a50a242009-11-08 20:57:03 -08003385
Arnd Bergmanna554bf92021-07-27 15:45:12 +02003386 err = dev_ioctl(net, SIOCWANDEV, &ifr, NULL, NULL);
Al Viro44c02a22017-10-05 12:59:44 -04003387 if (!err) {
3388 ifr.ifr_settings.ifs_ifsu.raw_hdlc = saved;
Arnd Bergmann29c49642021-07-22 16:29:03 +02003389 if (put_user_ifreq(&ifr, uifr32))
Al Viro44c02a22017-10-05 12:59:44 -04003390 err = -EFAULT;
Joe Perchesccbd6a52010-05-14 10:58:26 +00003391 }
Al Viro44c02a22017-10-05 12:59:44 -04003392 return err;
Arnd Bergmann7a229382009-11-06 23:00:29 -08003393}
3394
Ben Hutchings590d4692013-11-18 17:04:13 +00003395/* Handle ioctls that use ifreq::ifr_data and just need struct ifreq converted */
3396static int compat_ifr_data_ioctl(struct net *net, unsigned int cmd,
Arnd Bergmann6b960182009-11-06 23:10:54 -08003397 struct compat_ifreq __user *u_ifreq32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08003398{
Al Viro44c02a22017-10-05 12:59:44 -04003399 struct ifreq ifreq;
Arnd Bergmanna554bf92021-07-27 15:45:12 +02003400 void __user *data;
Arnd Bergmann7a229382009-11-06 23:00:29 -08003401
Peter Collingbourned0efb162021-08-26 12:46:01 -07003402 if (!is_socket_ioctl_cmd(cmd))
3403 return -ENOTTY;
Arnd Bergmanna554bf92021-07-27 15:45:12 +02003404 if (get_user_ifreq(&ifreq, &data, u_ifreq32))
Arnd Bergmann7a229382009-11-06 23:00:29 -08003405 return -EFAULT;
Arnd Bergmanna554bf92021-07-27 15:45:12 +02003406 ifreq.ifr_data = data;
Arnd Bergmann7a229382009-11-06 23:00:29 -08003407
Arnd Bergmanna554bf92021-07-27 15:45:12 +02003408 return dev_ioctl(net, cmd, &ifreq, data, NULL);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003409}
3410
Arnd Bergmann6b960182009-11-06 23:10:54 -08003411static int compat_sock_ioctl_trans(struct file *file, struct socket *sock,
3412 unsigned int cmd, unsigned long arg)
3413{
3414 void __user *argp = compat_ptr(arg);
3415 struct sock *sk = sock->sk;
3416 struct net *net = sock_net(sk);
Eric Dumazet1ded5e52023-08-08 13:58:09 +00003417 const struct proto_ops *ops;
Arnd Bergmann7a229382009-11-06 23:00:29 -08003418
Arnd Bergmann6b960182009-11-06 23:10:54 -08003419 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15))
Arnd Bergmann88fc0232021-07-27 15:45:15 +02003420 return sock_ioctl(file, cmd, (unsigned long)argp);
Arnd Bergmann7a229382009-11-06 23:00:29 -08003421
Arnd Bergmann6b960182009-11-06 23:10:54 -08003422 switch (cmd) {
Arnd Bergmann7a50a242009-11-08 20:57:03 -08003423 case SIOCWANDEV:
3424 return compat_siocwandev(net, argp);
Arnd Bergmann0768e172019-04-17 22:56:11 +02003425 case SIOCGSTAMP_OLD:
3426 case SIOCGSTAMPNS_OLD:
Eric Dumazet1ded5e52023-08-08 13:58:09 +00003427 ops = READ_ONCE(sock->ops);
3428 if (!ops->gettstamp)
Arnd Bergmannc7cbdbf2019-04-17 22:51:48 +02003429 return -ENOIOCTLCMD;
Eric Dumazet1ded5e52023-08-08 13:58:09 +00003430 return ops->gettstamp(sock, argp, cmd == SIOCGSTAMP_OLD,
3431 !COMPAT_USE_64BIT_TIME);
Arnd Bergmannc7cbdbf2019-04-17 22:51:48 +02003432
Arnd Bergmanndd98d282021-07-22 16:28:59 +02003433 case SIOCETHTOOL:
Ben Hutchings590d4692013-11-18 17:04:13 +00003434 case SIOCBONDSLAVEINFOQUERY:
3435 case SIOCBONDINFOQUERY:
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003436 case SIOCSHWTSTAMP:
Ben Hutchingsfd468c72013-11-14 01:19:29 +00003437 case SIOCGHWTSTAMP:
Ben Hutchings590d4692013-11-18 17:04:13 +00003438 return compat_ifr_data_ioctl(net, cmd, argp);
Arnd Bergmann7a229382009-11-06 23:00:29 -08003439
Arnd Bergmann6b960182009-11-06 23:10:54 -08003440 case FIOSETOWN:
3441 case SIOCSPGRP:
3442 case FIOGETOWN:
3443 case SIOCGPGRP:
3444 case SIOCBRADDBR:
3445 case SIOCBRDELBR:
3446 case SIOCGIFVLAN:
3447 case SIOCSIFVLAN:
Andrey Vaginc62cce22016-10-24 18:29:13 -07003448 case SIOCGSKNS:
Arnd Bergmann0768e172019-04-17 22:56:11 +02003449 case SIOCGSTAMP_NEW:
3450 case SIOCGSTAMPNS_NEW:
Arnd Bergmann876f0bf2021-07-22 16:29:02 +02003451 case SIOCGIFCONF:
Remi Pommarelfd3a4592021-12-24 12:46:40 +01003452 case SIOCSIFBR:
3453 case SIOCGIFBR:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003454 return sock_ioctl(file, cmd, arg);
3455
3456 case SIOCGIFFLAGS:
3457 case SIOCSIFFLAGS:
Arnd Bergmann709566d2021-07-22 16:29:00 +02003458 case SIOCGIFMAP:
3459 case SIOCSIFMAP:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003460 case SIOCGIFMETRIC:
3461 case SIOCSIFMETRIC:
3462 case SIOCGIFMTU:
3463 case SIOCSIFMTU:
3464 case SIOCGIFMEM:
3465 case SIOCSIFMEM:
3466 case SIOCGIFHWADDR:
3467 case SIOCSIFHWADDR:
3468 case SIOCADDMULTI:
3469 case SIOCDELMULTI:
3470 case SIOCGIFINDEX:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003471 case SIOCGIFADDR:
3472 case SIOCSIFADDR:
3473 case SIOCSIFHWBROADCAST:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003474 case SIOCDIFADDR:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003475 case SIOCGIFBRDADDR:
3476 case SIOCSIFBRDADDR:
3477 case SIOCGIFDSTADDR:
3478 case SIOCSIFDSTADDR:
3479 case SIOCGIFNETMASK:
3480 case SIOCSIFNETMASK:
3481 case SIOCSIFPFLAGS:
3482 case SIOCGIFPFLAGS:
3483 case SIOCGIFTXQLEN:
3484 case SIOCSIFTXQLEN:
3485 case SIOCBRADDIF:
3486 case SIOCBRDELIF:
Johannes Bergc6c9fee2019-01-25 22:43:19 +01003487 case SIOCGIFNAME:
Arnd Bergmann9177efd2009-11-06 08:09:09 +00003488 case SIOCSIFNAME:
3489 case SIOCGMIIPHY:
3490 case SIOCGMIIREG:
3491 case SIOCSMIIREG:
Al Virof92d4fc2017-09-30 19:32:17 -04003492 case SIOCBONDENSLAVE:
3493 case SIOCBONDRELEASE:
3494 case SIOCBONDSETHWADDR:
3495 case SIOCBONDCHANGEACTIVE:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003496 case SIOCSARP:
3497 case SIOCGARP:
3498 case SIOCDARP:
Arnd Bergmannc7dc5042019-06-03 23:07:12 +02003499 case SIOCOUTQ:
Arnd Bergmann9d7bf412019-06-03 23:06:00 +02003500 case SIOCOUTQNSD:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003501 case SIOCATMARK:
Johannes Berg63ff03a2019-01-25 22:43:17 +01003502 return sock_do_ioctl(net, sock, cmd, arg);
Arnd Bergmann9177efd2009-11-06 08:09:09 +00003503 }
3504
Arnd Bergmann6b960182009-11-06 23:10:54 -08003505 return -ENOIOCTLCMD;
3506}
Arnd Bergmann7a229382009-11-06 23:00:29 -08003507
Eric Dumazet95c96172012-04-15 05:58:06 +00003508static long compat_sock_ioctl(struct file *file, unsigned int cmd,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07003509 unsigned long arg)
Shaun Pereira89bbfc92006-03-21 23:58:08 -08003510{
3511 struct socket *sock = file->private_data;
Eric Dumazet1ded5e52023-08-08 13:58:09 +00003512 const struct proto_ops *ops = READ_ONCE(sock->ops);
Shaun Pereira89bbfc92006-03-21 23:58:08 -08003513 int ret = -ENOIOCTLCMD;
David S. Miller87de87d2008-06-03 09:14:03 -07003514 struct sock *sk;
3515 struct net *net;
3516
3517 sk = sock->sk;
3518 net = sock_net(sk);
Shaun Pereira89bbfc92006-03-21 23:58:08 -08003519
Eric Dumazet1ded5e52023-08-08 13:58:09 +00003520 if (ops->compat_ioctl)
3521 ret = ops->compat_ioctl(sock, cmd, arg);
Shaun Pereira89bbfc92006-03-21 23:58:08 -08003522
David S. Miller87de87d2008-06-03 09:14:03 -07003523 if (ret == -ENOIOCTLCMD &&
3524 (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST))
3525 ret = compat_wext_handle_ioctl(net, cmd, arg);
3526
Arnd Bergmann6b960182009-11-06 23:10:54 -08003527 if (ret == -ENOIOCTLCMD)
3528 ret = compat_sock_ioctl_trans(file, sock, cmd, arg);
3529
Shaun Pereira89bbfc92006-03-21 23:58:08 -08003530 return ret;
3531}
3532#endif
3533
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003534/**
3535 * kernel_bind - bind an address to a socket (kernel space)
3536 * @sock: socket
3537 * @addr: address
3538 * @addrlen: length of address
3539 *
3540 * Returns 0 or an error.
3541 */
3542
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003543int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen)
3544{
Jordan Rifec889a99a2023-09-21 18:46:42 -05003545 struct sockaddr_storage address;
3546
3547 memcpy(&address, addr, addrlen);
3548
3549 return READ_ONCE(sock->ops)->bind(sock, (struct sockaddr *)&address,
3550 addrlen);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003551}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003552EXPORT_SYMBOL(kernel_bind);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003553
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003554/**
3555 * kernel_listen - move socket to listening state (kernel space)
3556 * @sock: socket
3557 * @backlog: pending connections queue size
3558 *
3559 * Returns 0 or an error.
3560 */
3561
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003562int kernel_listen(struct socket *sock, int backlog)
3563{
Eric Dumazet1ded5e52023-08-08 13:58:09 +00003564 return READ_ONCE(sock->ops)->listen(sock, backlog);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003565}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003566EXPORT_SYMBOL(kernel_listen);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003567
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003568/**
3569 * kernel_accept - accept a connection (kernel space)
3570 * @sock: listening socket
3571 * @newsock: new connected socket
3572 * @flags: flags
3573 *
3574 * @flags must be SOCK_CLOEXEC, SOCK_NONBLOCK or 0.
3575 * If it fails, @newsock is guaranteed to be %NULL.
3576 * Returns 0 or an error.
3577 */
3578
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003579int kernel_accept(struct socket *sock, struct socket **newsock, int flags)
3580{
3581 struct sock *sk = sock->sk;
Eric Dumazet1ded5e52023-08-08 13:58:09 +00003582 const struct proto_ops *ops = READ_ONCE(sock->ops);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003583 int err;
3584
3585 err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
3586 newsock);
3587 if (err < 0)
3588 goto done;
3589
Eric Dumazet1ded5e52023-08-08 13:58:09 +00003590 err = ops->accept(sock, *newsock, flags, true);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003591 if (err < 0) {
3592 sock_release(*newsock);
Tony Battersbyfa8705b2007-10-10 21:09:04 -07003593 *newsock = NULL;
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003594 goto done;
3595 }
3596
Eric Dumazet1ded5e52023-08-08 13:58:09 +00003597 (*newsock)->ops = ops;
3598 __module_get(ops->owner);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003599
3600done:
3601 return err;
3602}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003603EXPORT_SYMBOL(kernel_accept);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003604
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003605/**
3606 * kernel_connect - connect a socket (kernel space)
3607 * @sock: socket
3608 * @addr: address
3609 * @addrlen: address length
3610 * @flags: flags (O_NONBLOCK, ...)
3611 *
Lu Weif1dcffc2021-03-25 11:01:55 +08003612 * For datagram sockets, @addr is the address to which datagrams are sent
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003613 * by default, and the only address from which datagrams are received.
3614 * For stream sockets, attempts to connect to @addr.
3615 * Returns 0 or an error code.
3616 */
3617
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003618int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
YOSHIFUJI Hideaki4768fbc2007-02-09 23:25:31 +09003619 int flags)
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003620{
Jordan Rife0bdf3992023-08-21 16:45:23 -05003621 struct sockaddr_storage address;
3622
3623 memcpy(&address, addr, addrlen);
3624
3625 return READ_ONCE(sock->ops)->connect(sock, (struct sockaddr *)&address,
3626 addrlen, flags);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003627}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003628EXPORT_SYMBOL(kernel_connect);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003629
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003630/**
3631 * kernel_getsockname - get the address which the socket is bound (kernel space)
3632 * @sock: socket
3633 * @addr: address holder
3634 *
3635 * Fills the @addr pointer with the address which the socket is bound.
Alex Maydanik0fc95de2022-02-12 12:29:27 +02003636 * Returns the length of the address in bytes or an error code.
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003637 */
3638
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01003639int kernel_getsockname(struct socket *sock, struct sockaddr *addr)
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003640{
Eric Dumazet1ded5e52023-08-08 13:58:09 +00003641 return READ_ONCE(sock->ops)->getname(sock, addr, 0);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003642}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003643EXPORT_SYMBOL(kernel_getsockname);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003644
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003645/**
Miaohe Lin645f0892020-08-27 07:27:49 -04003646 * kernel_getpeername - get the address which the socket is connected (kernel space)
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003647 * @sock: socket
3648 * @addr: address holder
3649 *
3650 * Fills the @addr pointer with the address which the socket is connected.
Alex Maydanik0fc95de2022-02-12 12:29:27 +02003651 * Returns the length of the address in bytes or an error code.
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003652 */
3653
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01003654int kernel_getpeername(struct socket *sock, struct sockaddr *addr)
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003655{
Eric Dumazet1ded5e52023-08-08 13:58:09 +00003656 return READ_ONCE(sock->ops)->getname(sock, addr, 1);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003657}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003658EXPORT_SYMBOL(kernel_getpeername);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003659
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003660/**
Miaohe Lin645f0892020-08-27 07:27:49 -04003661 * kernel_sock_shutdown - shut down part of a full-duplex connection (kernel space)
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003662 * @sock: socket
3663 * @how: connection part
3664 *
3665 * Returns 0 or an error.
3666 */
3667
Trond Myklebust91cf45f2007-11-12 18:10:39 -08003668int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how)
3669{
Eric Dumazet1ded5e52023-08-08 13:58:09 +00003670 return READ_ONCE(sock->ops)->shutdown(sock, how);
Trond Myklebust91cf45f2007-11-12 18:10:39 -08003671}
Trond Myklebust91cf45f2007-11-12 18:10:39 -08003672EXPORT_SYMBOL(kernel_sock_shutdown);
R. Parameswaran113c3072017-04-05 16:50:35 -07003673
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003674/**
3675 * kernel_sock_ip_overhead - returns the IP overhead imposed by a socket
3676 * @sk: socket
3677 *
3678 * This routine returns the IP overhead imposed by a socket i.e.
3679 * the length of the underlying IP header, depending on whether
3680 * this is an IPv4 or IPv6 socket and the length from IP options turned
3681 * on at the socket. Assumes that the caller has a lock on the socket.
R. Parameswaran113c3072017-04-05 16:50:35 -07003682 */
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003683
R. Parameswaran113c3072017-04-05 16:50:35 -07003684u32 kernel_sock_ip_overhead(struct sock *sk)
3685{
3686 struct inet_sock *inet;
3687 struct ip_options_rcu *opt;
3688 u32 overhead = 0;
R. Parameswaran113c3072017-04-05 16:50:35 -07003689#if IS_ENABLED(CONFIG_IPV6)
3690 struct ipv6_pinfo *np;
3691 struct ipv6_txoptions *optv6 = NULL;
3692#endif /* IS_ENABLED(CONFIG_IPV6) */
3693
3694 if (!sk)
3695 return overhead;
3696
R. Parameswaran113c3072017-04-05 16:50:35 -07003697 switch (sk->sk_family) {
3698 case AF_INET:
3699 inet = inet_sk(sk);
3700 overhead += sizeof(struct iphdr);
3701 opt = rcu_dereference_protected(inet->inet_opt,
stephen hemminger614d79c2017-07-24 10:25:22 -07003702 sock_owned_by_user(sk));
R. Parameswaran113c3072017-04-05 16:50:35 -07003703 if (opt)
3704 overhead += opt->opt.optlen;
3705 return overhead;
3706#if IS_ENABLED(CONFIG_IPV6)
3707 case AF_INET6:
3708 np = inet6_sk(sk);
3709 overhead += sizeof(struct ipv6hdr);
3710 if (np)
3711 optv6 = rcu_dereference_protected(np->opt,
stephen hemminger614d79c2017-07-24 10:25:22 -07003712 sock_owned_by_user(sk));
R. Parameswaran113c3072017-04-05 16:50:35 -07003713 if (optv6)
3714 overhead += (optv6->opt_flen + optv6->opt_nflen);
3715 return overhead;
3716#endif /* IS_ENABLED(CONFIG_IPV6) */
3717 default: /* Returns 0 overhead if the socket is not ipv4 or ipv6 */
3718 return overhead;
3719 }
3720}
3721EXPORT_SYMBOL(kernel_sock_ip_overhead);