blob: 33e8b6c4e1d38201893fc087052a395415baab49 [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 Kicinskicc698372020-11-20 14:50:52 -080055#include <linux/ethtool.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <linux/socket.h>
58#include <linux/file.h>
59#include <linux/net.h>
60#include <linux/interrupt.h>
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -070061#include <linux/thread_info.h>
Stephen Hemminger55737fd2006-09-01 00:23:39 -070062#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <linux/netdevice.h>
64#include <linux/proc_fs.h>
65#include <linux/seq_file.h>
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080066#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include <linux/if_bridge.h>
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -030068#include <linux/if_vlan.h>
Daniel Borkmann408eccc2014-04-01 16:20:23 +020069#include <linux/ptp_classify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#include <linux/init.h>
71#include <linux/poll.h>
72#include <linux/cache.h>
73#include <linux/module.h>
74#include <linux/highmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#include <linux/mount.h>
David Howellsfba9be42019-03-25 16:38:24 +000076#include <linux/pseudo_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#include <linux/security.h>
78#include <linux/syscalls.h>
79#include <linux/compat.h>
80#include <linux/kmod.h>
David Woodhouse3ec3b2f2005-05-17 12:08:48 +010081#include <linux/audit.h>
Adrian Bunkd86b5e02006-01-21 00:46:55 +010082#include <linux/wireless.h>
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -070083#include <linux/nsproxy.h>
Nick Black1fd7317d2009-09-22 16:43:33 -070084#include <linux/magic.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090085#include <linux/slab.h>
Masatake YAMATO600e1772012-08-29 10:44:29 +000086#include <linux/xattr.h>
Jeremy Clinec8e8cd52018-07-27 22:43:01 +000087#include <linux/nospec.h>
Paolo Abeni8c3c4472019-05-03 17:01:39 +020088#include <linux/indirect_call_wrapper.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080090#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070091#include <asm/unistd.h>
92
93#include <net/compat.h>
David S. Miller87de87d2008-06-03 09:14:03 -070094#include <net/wext.h>
Herbert Xuf8451722010-05-24 00:12:34 -070095#include <net/cls_cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97#include <net/sock.h>
98#include <linux/netfilter.h>
99
Arnd Bergmann6b960182009-11-06 23:10:54 -0800100#include <linux/if_tun.h>
101#include <linux/ipv6_route.h>
102#include <linux/route.h>
Arnd Bergmannc7dc5042019-06-03 23:07:12 +0200103#include <linux/termios.h>
Arnd Bergmann6b960182009-11-06 23:10:54 -0800104#include <linux/sockios.h>
Eliezer Tamir076bb0c2013-07-10 17:13:17 +0300105#include <net/busy_poll.h>
Willem de Bruijnf24b9be2014-08-04 22:11:45 -0400106#include <linux/errqueue.h>
Eliezer Tamir06021292013-06-10 11:39:50 +0300107
Cong Wange0d10952013-08-01 11:10:25 +0800108#ifdef CONFIG_NET_RX_BUSY_POLL
Eliezer Tamir64b0dc52013-07-10 17:13:36 +0300109unsigned int sysctl_net_busy_read __read_mostly;
110unsigned int sysctl_net_busy_poll __read_mostly;
Eliezer Tamir06021292013-06-10 11:39:50 +0300111#endif
Arnd Bergmann6b960182009-11-06 23:10:54 -0800112
Al Viro8ae5e0302014-11-28 19:40:50 -0500113static ssize_t sock_read_iter(struct kiocb *iocb, struct iov_iter *to);
114static ssize_t sock_write_iter(struct kiocb *iocb, struct iov_iter *from);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700115static int sock_mmap(struct file *file, struct vm_area_struct *vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117static int sock_close(struct inode *inode, struct file *file);
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700118static __poll_t sock_poll(struct file *file,
119 struct poll_table_struct *wait);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700120static long sock_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
Shaun Pereira89bbfc92006-03-21 23:58:08 -0800121#ifdef CONFIG_COMPAT
122static long compat_sock_ioctl(struct file *file,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700123 unsigned int cmd, unsigned long arg);
Shaun Pereira89bbfc92006-03-21 23:58:08 -0800124#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125static int sock_fasync(int fd, struct file *filp, int on);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126static ssize_t sock_sendpage(struct file *file, struct page *page,
127 int offset, size_t size, loff_t *ppos, int more);
Jens Axboe9c55e012007-11-06 23:30:13 -0800128static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700129 struct pipe_inode_info *pipe, size_t len,
Jens Axboe9c55e012007-11-06 23:30:13 -0800130 unsigned int flags);
Arnd Bergmann542d3062020-01-08 22:44:43 +0100131
132#ifdef CONFIG_PROC_FS
133static void sock_show_fdinfo(struct seq_file *m, struct file *f)
134{
135 struct socket *sock = f->private_data;
136
137 if (sock->ops->show_fdinfo)
138 sock->ops->show_fdinfo(m, sock);
139}
140#else
141#define sock_show_fdinfo NULL
142#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144/*
145 * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
146 * in the operation structures but are done directly via the socketcall() multiplexor.
147 */
148
Arjan van de Venda7071d2007-02-12 00:55:36 -0800149static const struct file_operations socket_file_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 .owner = THIS_MODULE,
151 .llseek = no_llseek,
Al Viro8ae5e0302014-11-28 19:40:50 -0500152 .read_iter = sock_read_iter,
153 .write_iter = sock_write_iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 .poll = sock_poll,
155 .unlocked_ioctl = sock_ioctl,
Shaun Pereira89bbfc92006-03-21 23:58:08 -0800156#ifdef CONFIG_COMPAT
157 .compat_ioctl = compat_sock_ioctl,
158#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 .mmap = sock_mmap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 .release = sock_close,
161 .fasync = sock_fasync,
Jens Axboe5274f052006-03-30 15:15:30 +0200162 .sendpage = sock_sendpage,
163 .splice_write = generic_splice_sendpage,
Jens Axboe9c55e012007-11-06 23:30:13 -0800164 .splice_read = sock_splice_read,
Kirill Tkhaib4653342019-12-09 13:03:40 +0300165 .show_fdinfo = sock_show_fdinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166};
167
168/*
169 * The protocol list. Each protocol is registered in here.
170 */
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172static DEFINE_SPINLOCK(net_family_lock);
Eric Dumazet190683a2010-11-10 10:50:44 +0000173static const struct net_proto_family __rcu *net_families[NPROTO] __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700176 * Support routines.
177 * Move socket addresses back and forth across the kernel/user
178 * divide and look after the messy bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 */
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181/**
182 * move_addr_to_kernel - copy a socket address into kernel space
183 * @uaddr: Address in user space
184 * @kaddr: Address in kernel space
185 * @ulen: Length in user space
186 *
187 * The address is copied into kernel space. If the provided address is
188 * too long an error code of -EINVAL is returned. If the copy gives
189 * invalid addresses -EFAULT is returned. On a success 0 is returned.
190 */
191
Maciej Żenczykowski43db3622012-03-11 12:51:50 +0000192int move_addr_to_kernel(void __user *uaddr, int ulen, struct sockaddr_storage *kaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -0700194 if (ulen < 0 || ulen > sizeof(struct sockaddr_storage))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700196 if (ulen == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 return 0;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700198 if (copy_from_user(kaddr, uaddr, ulen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 return -EFAULT;
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100200 return audit_sockaddr(ulen, kaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201}
202
203/**
204 * move_addr_to_user - copy an address to user space
205 * @kaddr: kernel space address
206 * @klen: length of address in kernel
207 * @uaddr: user space address
208 * @ulen: pointer to user length field
209 *
210 * The value pointed to by ulen on entry is the buffer length available.
211 * This is overwritten with the buffer space used. -EINVAL is returned
212 * if an overlong buffer is specified or a negative buffer size. -EFAULT
213 * is returned if either the buffer or the length field are not
214 * accessible.
215 * After copying the data up to the limit the user specifies, the true
216 * length of the data is written over the length limit the user
217 * specified. Zero is returned for a success.
218 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700219
Maciej Żenczykowski43db3622012-03-11 12:51:50 +0000220static int move_addr_to_user(struct sockaddr_storage *kaddr, int klen,
stephen hemminger11165f12010-10-18 14:27:29 +0000221 void __user *uaddr, int __user *ulen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
223 int err;
224 int len;
225
Hannes Frederic Sowa68c6beb2013-11-21 03:14:34 +0100226 BUG_ON(klen > sizeof(struct sockaddr_storage));
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700227 err = get_user(len, ulen);
228 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 return err;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700230 if (len > klen)
231 len = klen;
Hannes Frederic Sowa68c6beb2013-11-21 03:14:34 +0100232 if (len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700234 if (len) {
Steve Grubbd6fe3942006-03-30 12:20:22 -0500235 if (audit_sockaddr(klen, kaddr))
236 return -ENOMEM;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700237 if (copy_to_user(uaddr, kaddr, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 return -EFAULT;
239 }
240 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700241 * "fromlen shall refer to the value before truncation.."
242 * 1003.1g
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 */
244 return __put_user(klen, ulen);
245}
246
Alexey Dobriyan08009a72018-02-24 21:20:33 +0300247static struct kmem_cache *sock_inode_cachep __ro_after_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249static struct inode *sock_alloc_inode(struct super_block *sb)
250{
251 struct socket_alloc *ei;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700252
Christoph Lametere94b1762006-12-06 20:33:17 -0800253 ei = kmem_cache_alloc(sock_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 if (!ei)
255 return NULL;
Al Viro333f7902019-07-05 20:14:16 +0100256 init_waitqueue_head(&ei->socket.wq.wait);
257 ei->socket.wq.fasync_list = NULL;
258 ei->socket.wq.flags = 0;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 ei->socket.state = SS_UNCONNECTED;
261 ei->socket.flags = 0;
262 ei->socket.ops = NULL;
263 ei->socket.sk = NULL;
264 ei->socket.file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266 return &ei->vfs_inode;
267}
268
Al Viro6d7855c2019-07-05 20:13:22 +0100269static void sock_free_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
Eric Dumazet43815482010-04-29 11:01:49 +0000271 struct socket_alloc *ei;
272
273 ei = container_of(inode, struct socket_alloc, vfs_inode);
Eric Dumazet43815482010-04-29 11:01:49 +0000274 kmem_cache_free(sock_inode_cachep, ei);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
276
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700277static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700279 struct socket_alloc *ei = (struct socket_alloc *)foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Christoph Lametera35afb82007-05-16 22:10:57 -0700281 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282}
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700283
yuan linyu1e911632017-01-07 17:18:31 +0800284static void init_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
286 sock_inode_cachep = kmem_cache_create("sock_inode_cache",
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700287 sizeof(struct socket_alloc),
288 0,
289 (SLAB_HWCACHE_ALIGN |
290 SLAB_RECLAIM_ACCOUNT |
Vladimir Davydov5d097052016-01-14 15:18:21 -0800291 SLAB_MEM_SPREAD | SLAB_ACCOUNT),
Paul Mundt20c2df82007-07-20 10:11:58 +0900292 init_once);
yuan linyu1e911632017-01-07 17:18:31 +0800293 BUG_ON(sock_inode_cachep == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
295
Alexey Dobriyanb87221d2009-09-21 17:01:09 -0700296static const struct super_operations sockfs_ops = {
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700297 .alloc_inode = sock_alloc_inode,
Al Viro6d7855c2019-07-05 20:13:22 +0100298 .free_inode = sock_free_inode,
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700299 .statfs = simple_statfs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300};
301
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700302/*
303 * sockfs_dname() is called from d_path().
304 */
305static char *sockfs_dname(struct dentry *dentry, char *buffer, int buflen)
306{
307 return dynamic_dname(dentry, buffer, buflen, "socket:[%lu]",
David Howellsc5ef6032015-03-17 22:26:16 +0000308 d_inode(dentry)->i_ino);
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700309}
310
Al Viro3ba13d12009-02-20 06:02:22 +0000311static const struct dentry_operations sockfs_dentry_operations = {
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700312 .d_dname = sockfs_dname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313};
314
Andreas Gruenbacherbba0bd32016-09-29 17:48:35 +0200315static int sockfs_xattr_get(const struct xattr_handler *handler,
316 struct dentry *dentry, struct inode *inode,
317 const char *suffix, void *value, size_t size)
318{
319 if (value) {
320 if (dentry->d_name.len + 1 > size)
321 return -ERANGE;
322 memcpy(value, dentry->d_name.name, dentry->d_name.len + 1);
323 }
324 return dentry->d_name.len + 1;
325}
326
327#define XATTR_SOCKPROTONAME_SUFFIX "sockprotoname"
328#define XATTR_NAME_SOCKPROTONAME (XATTR_SYSTEM_PREFIX XATTR_SOCKPROTONAME_SUFFIX)
329#define XATTR_NAME_SOCKPROTONAME_LEN (sizeof(XATTR_NAME_SOCKPROTONAME)-1)
330
331static const struct xattr_handler sockfs_xattr_handler = {
332 .name = XATTR_NAME_SOCKPROTONAME,
333 .get = sockfs_xattr_get,
334};
335
Andreas Gruenbacher4a590152016-11-13 21:23:34 +0100336static int sockfs_security_xattr_set(const struct xattr_handler *handler,
337 struct dentry *dentry, struct inode *inode,
338 const char *suffix, const void *value,
339 size_t size, int flags)
340{
341 /* Handled by LSM. */
342 return -EAGAIN;
343}
344
345static const struct xattr_handler sockfs_security_xattr_handler = {
346 .prefix = XATTR_SECURITY_PREFIX,
347 .set = sockfs_security_xattr_set,
348};
349
Andreas Gruenbacherbba0bd32016-09-29 17:48:35 +0200350static const struct xattr_handler *sockfs_xattr_handlers[] = {
351 &sockfs_xattr_handler,
Andreas Gruenbacher4a590152016-11-13 21:23:34 +0100352 &sockfs_security_xattr_handler,
Andreas Gruenbacherbba0bd32016-09-29 17:48:35 +0200353 NULL
354};
355
David Howellsfba9be42019-03-25 16:38:24 +0000356static int sockfs_init_fs_context(struct fs_context *fc)
Al Viroc74a1cb2011-01-12 16:59:34 -0500357{
David Howellsfba9be42019-03-25 16:38:24 +0000358 struct pseudo_fs_context *ctx = init_pseudo(fc, SOCKFS_MAGIC);
359 if (!ctx)
360 return -ENOMEM;
361 ctx->ops = &sockfs_ops;
362 ctx->dops = &sockfs_dentry_operations;
363 ctx->xattr = sockfs_xattr_handlers;
364 return 0;
Al Viroc74a1cb2011-01-12 16:59:34 -0500365}
366
367static struct vfsmount *sock_mnt __read_mostly;
368
369static struct file_system_type sock_fs_type = {
370 .name = "sockfs",
David Howellsfba9be42019-03-25 16:38:24 +0000371 .init_fs_context = sockfs_init_fs_context,
Al Viroc74a1cb2011-01-12 16:59:34 -0500372 .kill_sb = kill_anon_super,
373};
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375/*
376 * Obtains the first available file descriptor and sets it up for use.
377 *
David S. Miller39d8c1b2006-03-20 17:13:49 -0800378 * These functions create file structures and maps them to fd space
379 * of the current process. On success it returns file descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 * and file struct implicitly stored in sock->file.
381 * Note that another thread may close file descriptor before we return
382 * from this function. We use the fact that now we do not refer
383 * to socket after mapping. If one day we will need it, this
384 * function will increment ref. count on file by 1.
385 *
386 * In any case returned fd MAY BE not valid!
387 * This race condition is unavoidable
388 * with shared fd spaces, we cannot solve it inside kernel,
389 * but we take care of internal coherence yet.
390 */
391
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300392/**
393 * sock_alloc_file - Bind a &socket to a &file
394 * @sock: socket
395 * @flags: file status flags
396 * @dname: protocol name
397 *
398 * Returns the &file bound with @sock, implicitly storing it
399 * in sock->file. If dname is %NULL, sets to "".
400 * On failure the return is a ERR pointer (see linux/err.h).
401 * This function uses GFP_KERNEL internally.
402 */
403
Linus Torvaldsaab174f2012-10-02 20:25:04 -0700404struct file *sock_alloc_file(struct socket *sock, int flags, const char *dname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
Al Viro7cbe66b2009-08-05 19:59:08 +0400406 struct file *file;
David S. Miller39d8c1b2006-03-20 17:13:49 -0800407
Al Virod93aa9d2018-06-09 09:40:05 -0400408 if (!dname)
409 dname = sock->sk ? sock->sk->sk_prot_creator->name : "";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Al Virod93aa9d2018-06-09 09:40:05 -0400411 file = alloc_file_pseudo(SOCK_INODE(sock), sock_mnt, dname,
412 O_RDWR | (flags & O_NONBLOCK),
413 &socket_file_ops);
Viresh Kumarb5ffe632015-08-12 15:59:47 +0530414 if (IS_ERR(file)) {
Al Viro8e1611e2017-12-05 23:29:09 +0000415 sock_release(sock);
Anatol Pomozov39b65252012-09-12 20:11:55 -0700416 return file;
Al Virocc3808f2009-08-06 09:43:59 +0400417 }
David S. Miller39d8c1b2006-03-20 17:13:49 -0800418
419 sock->file = file;
David S. Miller39d8c1b2006-03-20 17:13:49 -0800420 file->private_data = sock;
Linus Torvaldsd8e464e2019-11-17 11:20:48 -0800421 stream_open(SOCK_INODE(sock), file);
Al Viro28407632012-08-17 23:54:15 -0400422 return file;
David S. Miller39d8c1b2006-03-20 17:13:49 -0800423}
Al Viro56b31d12012-08-18 00:25:51 -0400424EXPORT_SYMBOL(sock_alloc_file);
David S. Miller39d8c1b2006-03-20 17:13:49 -0800425
Al Viro56b31d12012-08-18 00:25:51 -0400426static int sock_map_fd(struct socket *sock, int flags)
David S. Miller39d8c1b2006-03-20 17:13:49 -0800427{
428 struct file *newfile;
Al Viro28407632012-08-17 23:54:15 -0400429 int fd = get_unused_fd_flags(flags);
Al Viroce4bb042018-01-10 18:47:05 -0500430 if (unlikely(fd < 0)) {
431 sock_release(sock);
Al Viro28407632012-08-17 23:54:15 -0400432 return fd;
Al Viroce4bb042018-01-10 18:47:05 -0500433 }
David S. Miller39d8c1b2006-03-20 17:13:49 -0800434
Linus Torvaldsaab174f2012-10-02 20:25:04 -0700435 newfile = sock_alloc_file(sock, flags, NULL);
Enrico Weigelt4546e442019-06-05 22:58:50 +0200436 if (!IS_ERR(newfile)) {
David S. Miller39d8c1b2006-03-20 17:13:49 -0800437 fd_install(fd, newfile);
Al Viro28407632012-08-17 23:54:15 -0400438 return fd;
439 }
Al Viro7cbe66b2009-08-05 19:59:08 +0400440
Al Viro28407632012-08-17 23:54:15 -0400441 put_unused_fd(fd);
442 return PTR_ERR(newfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443}
444
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300445/**
446 * sock_from_file - Return the &socket bounded to @file.
447 * @file: file
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300448 *
Florent Revestdba4a922020-12-04 12:36:04 +0100449 * On failure returns %NULL.
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300450 */
451
Florent Revestdba4a922020-12-04 12:36:04 +0100452struct socket *sock_from_file(struct file *file)
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800453{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800454 if (file->f_op == &socket_file_ops)
455 return file->private_data; /* set in sock_map_fd */
456
Eric Dumazet23bb80d2007-02-08 14:59:57 -0800457 return NULL;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800458}
John Fastabend406a3c62012-07-20 10:39:25 +0000459EXPORT_SYMBOL(sock_from_file);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461/**
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700462 * sockfd_lookup - Go from a file number to its socket slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 * @fd: file handle
464 * @err: pointer to an error code return
465 *
466 * The file handle passed in is locked and the socket it is bound
Rosen, Rami241c4662017-05-21 22:12:38 +0300467 * to is returned. If an error occurs the err pointer is overwritten
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 * with a negative errno code and NULL is returned. The function checks
469 * for both invalid handles and passing a handle which is not a socket.
470 *
471 * On a success the socket object pointer is returned.
472 */
473
474struct socket *sockfd_lookup(int fd, int *err)
475{
476 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 struct socket *sock;
478
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700479 file = fget(fd);
480 if (!file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 *err = -EBADF;
482 return NULL;
483 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700484
Florent Revestdba4a922020-12-04 12:36:04 +0100485 sock = sock_from_file(file);
486 if (!sock) {
487 *err = -ENOTSOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 fput(file);
Florent Revestdba4a922020-12-04 12:36:04 +0100489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 return sock;
491}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700492EXPORT_SYMBOL(sockfd_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800494static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed)
495{
Al Viro00e188e2014-03-03 23:48:18 -0500496 struct fd f = fdget(fd);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800497 struct socket *sock;
498
Hua Zhong36725582006-04-19 15:25:02 -0700499 *err = -EBADF;
Al Viro00e188e2014-03-03 23:48:18 -0500500 if (f.file) {
Florent Revestdba4a922020-12-04 12:36:04 +0100501 sock = sock_from_file(f.file);
Al Viro00e188e2014-03-03 23:48:18 -0500502 if (likely(sock)) {
Miaohe Lince787a52020-08-06 19:53:16 +0800503 *fput_needed = f.flags & FDPUT_FPUT;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800504 return sock;
Al Viro00e188e2014-03-03 23:48:18 -0500505 }
Florent Revestdba4a922020-12-04 12:36:04 +0100506 *err = -ENOTSOCK;
Al Viro00e188e2014-03-03 23:48:18 -0500507 fdput(f);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800508 }
509 return NULL;
510}
511
Masatake YAMATO600e1772012-08-29 10:44:29 +0000512static ssize_t sockfs_listxattr(struct dentry *dentry, char *buffer,
513 size_t size)
514{
515 ssize_t len;
516 ssize_t used = 0;
517
David Howellsc5ef6032015-03-17 22:26:16 +0000518 len = security_inode_listsecurity(d_inode(dentry), buffer, size);
Masatake YAMATO600e1772012-08-29 10:44:29 +0000519 if (len < 0)
520 return len;
521 used += len;
522 if (buffer) {
523 if (size < used)
524 return -ERANGE;
525 buffer += len;
526 }
527
528 len = (XATTR_NAME_SOCKPROTONAME_LEN + 1);
529 used += len;
530 if (buffer) {
531 if (size < used)
532 return -ERANGE;
533 memcpy(buffer, XATTR_NAME_SOCKPROTONAME, len);
534 buffer += len;
535 }
536
537 return used;
538}
539
Tobias Klauserdc647ec2017-01-10 09:30:51 +0100540static int sockfs_setattr(struct dentry *dentry, struct iattr *iattr)
Lorenzo Colitti86741ec2016-11-04 02:23:41 +0900541{
542 int err = simple_setattr(dentry, iattr);
543
Eric Biggerse1a3a602016-12-30 17:42:32 -0600544 if (!err && (iattr->ia_valid & ATTR_UID)) {
Lorenzo Colitti86741ec2016-11-04 02:23:41 +0900545 struct socket *sock = SOCKET_I(d_inode(dentry));
546
Cong Wang6d8c50d2018-06-07 13:39:49 -0700547 if (sock->sk)
548 sock->sk->sk_uid = iattr->ia_uid;
549 else
550 err = -ENOENT;
Lorenzo Colitti86741ec2016-11-04 02:23:41 +0900551 }
552
553 return err;
554}
555
Masatake YAMATO600e1772012-08-29 10:44:29 +0000556static const struct inode_operations sockfs_inode_ops = {
Masatake YAMATO600e1772012-08-29 10:44:29 +0000557 .listxattr = sockfs_listxattr,
Lorenzo Colitti86741ec2016-11-04 02:23:41 +0900558 .setattr = sockfs_setattr,
Masatake YAMATO600e1772012-08-29 10:44:29 +0000559};
560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561/**
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300562 * sock_alloc - allocate a socket
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700563 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 * Allocate a new inode and socket object. The two are bound together
565 * and initialised. The socket is then returned. If we are out of inodes
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300566 * NULL is returned. This functions uses GFP_KERNEL internally.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 */
568
Tom Herbertf4a00aa2016-03-07 14:11:01 -0800569struct socket *sock_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700571 struct inode *inode;
572 struct socket *sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Eric Dumazeta209dfc2011-07-26 11:36:34 +0200574 inode = new_inode_pseudo(sock_mnt->mnt_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 if (!inode)
576 return NULL;
577
578 sock = SOCKET_I(inode);
579
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400580 inode->i_ino = get_next_ino();
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700581 inode->i_mode = S_IFSOCK | S_IRWXUGO;
David Howells8192b0c2008-11-14 10:39:10 +1100582 inode->i_uid = current_fsuid();
583 inode->i_gid = current_fsgid();
Masatake YAMATO600e1772012-08-29 10:44:29 +0000584 inode->i_op = &sockfs_inode_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 return sock;
587}
Tom Herbertf4a00aa2016-03-07 14:11:01 -0800588EXPORT_SYMBOL(sock_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Cong Wang6d8c50d2018-06-07 13:39:49 -0700590static void __sock_release(struct socket *sock, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
592 if (sock->ops) {
593 struct module *owner = sock->ops->owner;
594
Cong Wang6d8c50d2018-06-07 13:39:49 -0700595 if (inode)
596 inode_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 sock->ops->release(sock);
Eric Biggersff7b11a2019-02-21 14:13:56 -0800598 sock->sk = NULL;
Cong Wang6d8c50d2018-06-07 13:39:49 -0700599 if (inode)
600 inode_unlock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 sock->ops = NULL;
602 module_put(owner);
603 }
604
Al Viro333f7902019-07-05 20:14:16 +0100605 if (sock->wq.fasync_list)
Yang Yingliang3410f222014-02-12 17:09:55 +0800606 pr_err("%s: fasync list not empty!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 if (!sock->file) {
609 iput(SOCK_INODE(sock));
610 return;
611 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700612 sock->file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613}
Cong Wang6d8c50d2018-06-07 13:39:49 -0700614
Andrew Lunn9a8ad9a2020-07-13 01:15:12 +0200615/**
616 * sock_release - close a socket
617 * @sock: socket to close
618 *
619 * The socket is released from the protocol stack if it has a release
620 * callback, and the inode is then released if the socket is bound to
621 * an inode not a file.
622 */
Cong Wang6d8c50d2018-06-07 13:39:49 -0700623void sock_release(struct socket *sock)
624{
625 __sock_release(sock, NULL);
626}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700627EXPORT_SYMBOL(sock_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -0400629void __sock_tx_timestamp(__u16 tsflags, __u8 *tx_flags)
Patrick Ohly20d49472009-02-12 05:03:38 +0000630{
Eric Dumazet140c55d2014-08-06 11:49:29 +0200631 u8 flags = *tx_flags;
632
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -0400633 if (tsflags & SOF_TIMESTAMPING_TX_HARDWARE)
Eric Dumazet140c55d2014-08-06 11:49:29 +0200634 flags |= SKBTX_HW_TSTAMP;
635
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -0400636 if (tsflags & SOF_TIMESTAMPING_TX_SOFTWARE)
Eric Dumazet140c55d2014-08-06 11:49:29 +0200637 flags |= SKBTX_SW_TSTAMP;
638
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -0400639 if (tsflags & SOF_TIMESTAMPING_TX_SCHED)
Eric Dumazet140c55d2014-08-06 11:49:29 +0200640 flags |= SKBTX_SCHED_TSTAMP;
641
Eric Dumazet140c55d2014-08-06 11:49:29 +0200642 *tx_flags = flags;
Patrick Ohly20d49472009-02-12 05:03:38 +0000643}
Willem de Bruijn67cc0d42014-09-08 19:58:58 -0400644EXPORT_SYMBOL(__sock_tx_timestamp);
Patrick Ohly20d49472009-02-12 05:03:38 +0000645
Paolo Abeni8c3c4472019-05-03 17:01:39 +0200646INDIRECT_CALLABLE_DECLARE(int inet_sendmsg(struct socket *, struct msghdr *,
647 size_t));
Paolo Abenia648a592019-07-03 16:06:54 +0200648INDIRECT_CALLABLE_DECLARE(int inet6_sendmsg(struct socket *, struct msghdr *,
649 size_t));
Al Virod8725c82014-12-11 00:02:50 -0500650static inline int sock_sendmsg_nosec(struct socket *sock, struct msghdr *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
Paolo Abenia648a592019-07-03 16:06:54 +0200652 int ret = INDIRECT_CALL_INET(sock->ops->sendmsg, inet6_sendmsg,
653 inet_sendmsg, sock, msg,
654 msg_data_left(msg));
Al Virod8725c82014-12-11 00:02:50 -0500655 BUG_ON(ret == -EIOCBQUEUED);
656 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657}
Gu Zheng0cf00c62014-12-05 15:14:23 +0800658
Randy Dunlap85806af2019-05-18 21:23:07 -0700659/**
660 * sock_sendmsg - send a message through @sock
661 * @sock: socket
662 * @msg: message to send
663 *
664 * Sends @msg through @sock, passing through LSM.
665 * Returns the number of bytes sent, or an error code.
666 */
Al Virod8725c82014-12-11 00:02:50 -0500667int sock_sendmsg(struct socket *sock, struct msghdr *msg)
Gu Zheng0cf00c62014-12-05 15:14:23 +0800668{
Al Virod8725c82014-12-11 00:02:50 -0500669 int err = security_socket_sendmsg(sock, msg,
Al Viro01e97e62014-12-15 21:39:31 -0500670 msg_data_left(msg));
Ying Xue1b784142015-03-02 15:37:48 +0800671
Al Virod8725c82014-12-11 00:02:50 -0500672 return err ?: sock_sendmsg_nosec(sock, msg);
Gu Zheng0cf00c62014-12-05 15:14:23 +0800673}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700674EXPORT_SYMBOL(sock_sendmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300676/**
677 * kernel_sendmsg - send a message through @sock (kernel-space)
678 * @sock: socket
679 * @msg: message header
680 * @vec: kernel vec
681 * @num: vec array length
682 * @size: total message data size
683 *
684 * Builds the message data with @vec and sends it through @sock.
685 * Returns the number of bytes sent, or an error code.
686 */
687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
689 struct kvec *vec, size_t num, size_t size)
690{
David Howellsaa563d72018-10-20 00:57:56 +0100691 iov_iter_kvec(&msg->msg_iter, WRITE, vec, num, size);
Al Virod8725c82014-12-11 00:02:50 -0500692 return sock_sendmsg(sock, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700694EXPORT_SYMBOL(kernel_sendmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300696/**
697 * kernel_sendmsg_locked - send a message through @sock (kernel-space)
698 * @sk: sock
699 * @msg: message header
700 * @vec: output s/g array
701 * @num: output s/g array length
702 * @size: total message data size
703 *
704 * Builds the message data with @vec and sends it through @sock.
705 * Returns the number of bytes sent, or an error code.
706 * Caller must hold @sk.
707 */
708
Tom Herbert306b13e2017-07-28 16:22:41 -0700709int kernel_sendmsg_locked(struct sock *sk, struct msghdr *msg,
710 struct kvec *vec, size_t num, size_t size)
711{
712 struct socket *sock = sk->sk_socket;
713
714 if (!sock->ops->sendmsg_locked)
John Fastabenddb5980d2017-08-15 22:31:34 -0700715 return sock_no_sendmsg_locked(sk, msg, size);
Tom Herbert306b13e2017-07-28 16:22:41 -0700716
David Howellsaa563d72018-10-20 00:57:56 +0100717 iov_iter_kvec(&msg->msg_iter, WRITE, vec, num, size);
Tom Herbert306b13e2017-07-28 16:22:41 -0700718
719 return sock->ops->sendmsg_locked(sk, msg, msg_data_left(msg));
720}
721EXPORT_SYMBOL(kernel_sendmsg_locked);
722
Soheil Hassas Yeganeh8605330a2017-03-18 17:02:59 -0400723static bool skb_is_err_queue(const struct sk_buff *skb)
724{
725 /* pkt_type of skbs enqueued on the error queue are set to
726 * PACKET_OUTGOING in skb_set_err_queue(). This is only safe to do
727 * in recvmsg, since skbs received on a local socket will never
728 * have a pkt_type of PACKET_OUTGOING.
729 */
730 return skb->pkt_type == PACKET_OUTGOING;
731}
732
Miroslav Lichvarb50a5c72017-05-19 17:52:40 +0200733/* On transmit, software and hardware timestamps are returned independently.
734 * As the two skb clones share the hardware timestamp, which may be updated
735 * before the software timestamp is received, a hardware TX timestamp may be
736 * returned only if there is no software TX timestamp. Ignore false software
737 * timestamps, which may be made in the __sock_recv_timestamp() call when the
Deepa Dinamani7f1bc6e2019-02-02 07:34:46 -0800738 * option SO_TIMESTAMP_OLD(NS) is enabled on the socket, even when the skb has a
Miroslav Lichvarb50a5c72017-05-19 17:52:40 +0200739 * hardware timestamp.
740 */
741static bool skb_is_swtx_tstamp(const struct sk_buff *skb, int false_tstamp)
742{
743 return skb->tstamp && !false_tstamp && skb_is_err_queue(skb);
744}
745
Miroslav Lichvaraad9c8c2017-05-19 17:52:38 +0200746static void put_ts_pktinfo(struct msghdr *msg, struct sk_buff *skb)
747{
748 struct scm_ts_pktinfo ts_pktinfo;
749 struct net_device *orig_dev;
750
751 if (!skb_mac_header_was_set(skb))
752 return;
753
754 memset(&ts_pktinfo, 0, sizeof(ts_pktinfo));
755
756 rcu_read_lock();
757 orig_dev = dev_get_by_napi_id(skb_napi_id(skb));
758 if (orig_dev)
759 ts_pktinfo.if_index = orig_dev->ifindex;
760 rcu_read_unlock();
761
762 ts_pktinfo.pkt_length = skb->len - skb_mac_offset(skb);
763 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPING_PKTINFO,
764 sizeof(ts_pktinfo), &ts_pktinfo);
765}
766
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700767/*
768 * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
769 */
770void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
771 struct sk_buff *skb)
772{
Patrick Ohly20d49472009-02-12 05:03:38 +0000773 int need_software_tstamp = sock_flag(sk, SOCK_RCVTSTAMP);
Deepa Dinamani887feae2019-02-02 07:34:50 -0800774 int new_tstamp = sock_flag(sk, SOCK_TSTAMP_NEW);
Deepa Dinamani97184752019-02-02 07:34:51 -0800775 struct scm_timestamping_internal tss;
776
Miroslav Lichvarb50a5c72017-05-19 17:52:40 +0200777 int empty = 1, false_tstamp = 0;
Patrick Ohly20d49472009-02-12 05:03:38 +0000778 struct skb_shared_hwtstamps *shhwtstamps =
779 skb_hwtstamps(skb);
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700780
Patrick Ohly20d49472009-02-12 05:03:38 +0000781 /* Race occurred between timestamp enabling and packet
782 receiving. Fill in the current time for now. */
Miroslav Lichvarb50a5c72017-05-19 17:52:40 +0200783 if (need_software_tstamp && skb->tstamp == 0) {
Patrick Ohly20d49472009-02-12 05:03:38 +0000784 __net_timestamp(skb);
Miroslav Lichvarb50a5c72017-05-19 17:52:40 +0200785 false_tstamp = 1;
786 }
Patrick Ohly20d49472009-02-12 05:03:38 +0000787
788 if (need_software_tstamp) {
789 if (!sock_flag(sk, SOCK_RCVTSTAMPNS)) {
Deepa Dinamani887feae2019-02-02 07:34:50 -0800790 if (new_tstamp) {
791 struct __kernel_sock_timeval tv;
792
793 skb_get_new_timestamp(skb, &tv);
794 put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMP_NEW,
795 sizeof(tv), &tv);
796 } else {
797 struct __kernel_old_timeval tv;
798
799 skb_get_timestamp(skb, &tv);
800 put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMP_OLD,
801 sizeof(tv), &tv);
802 }
Patrick Ohly20d49472009-02-12 05:03:38 +0000803 } else {
Deepa Dinamani887feae2019-02-02 07:34:50 -0800804 if (new_tstamp) {
805 struct __kernel_timespec ts;
806
807 skb_get_new_timestampns(skb, &ts);
808 put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMPNS_NEW,
809 sizeof(ts), &ts);
810 } else {
Arnd Bergmanndf1b4ba2019-10-25 22:04:46 +0200811 struct __kernel_old_timespec ts;
Deepa Dinamani887feae2019-02-02 07:34:50 -0800812
813 skb_get_timestampns(skb, &ts);
814 put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMPNS_OLD,
815 sizeof(ts), &ts);
816 }
Patrick Ohly20d49472009-02-12 05:03:38 +0000817 }
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700818 }
Patrick Ohly20d49472009-02-12 05:03:38 +0000819
Willem de Bruijnf24b9be2014-08-04 22:11:45 -0400820 memset(&tss, 0, sizeof(tss));
Willem de Bruijnc1991052014-09-03 12:01:18 -0400821 if ((sk->sk_tsflags & SOF_TIMESTAMPING_SOFTWARE) &&
Deepa Dinamani97184752019-02-02 07:34:51 -0800822 ktime_to_timespec64_cond(skb->tstamp, tss.ts + 0))
Patrick Ohly20d49472009-02-12 05:03:38 +0000823 empty = 0;
Willem de Bruijn4d276eb2014-07-25 18:01:32 -0400824 if (shhwtstamps &&
Willem de Bruijnb9f40e22014-08-04 22:11:46 -0400825 (sk->sk_tsflags & SOF_TIMESTAMPING_RAW_HARDWARE) &&
Miroslav Lichvarb50a5c72017-05-19 17:52:40 +0200826 !skb_is_swtx_tstamp(skb, false_tstamp) &&
Deepa Dinamani97184752019-02-02 07:34:51 -0800827 ktime_to_timespec64_cond(shhwtstamps->hwtstamp, tss.ts + 2)) {
Willem de Bruijn4d276eb2014-07-25 18:01:32 -0400828 empty = 0;
Miroslav Lichvaraad9c8c2017-05-19 17:52:38 +0200829 if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_PKTINFO) &&
830 !skb_is_err_queue(skb))
831 put_ts_pktinfo(msg, skb);
832 }
Francis Yan1c885802016-11-27 23:07:18 -0800833 if (!empty) {
Deepa Dinamani97184752019-02-02 07:34:51 -0800834 if (sock_flag(sk, SOCK_TSTAMP_NEW))
835 put_cmsg_scm_timestamping64(msg, &tss);
836 else
837 put_cmsg_scm_timestamping(msg, &tss);
Francis Yan1c885802016-11-27 23:07:18 -0800838
Soheil Hassas Yeganeh8605330a2017-03-18 17:02:59 -0400839 if (skb_is_err_queue(skb) && skb->len &&
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -0400840 SKB_EXT_ERR(skb)->opt_stats)
Francis Yan1c885802016-11-27 23:07:18 -0800841 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPING_OPT_STATS,
842 skb->len, skb->data);
843 }
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700844}
Arnaldo Carvalho de Melo7c81fd82007-03-10 00:39:35 -0300845EXPORT_SYMBOL_GPL(__sock_recv_timestamp);
846
Johannes Berg6e3e9392011-11-09 10:15:42 +0100847void __sock_recv_wifi_status(struct msghdr *msg, struct sock *sk,
848 struct sk_buff *skb)
849{
850 int ack;
851
852 if (!sock_flag(sk, SOCK_WIFI_STATUS))
853 return;
854 if (!skb->wifi_acked_valid)
855 return;
856
857 ack = skb->wifi_acked;
858
859 put_cmsg(msg, SOL_SOCKET, SCM_WIFI_STATUS, sizeof(ack), &ack);
860}
861EXPORT_SYMBOL_GPL(__sock_recv_wifi_status);
862
stephen hemminger11165f12010-10-18 14:27:29 +0000863static inline void sock_recv_drops(struct msghdr *msg, struct sock *sk,
864 struct sk_buff *skb)
Neil Horman3b885782009-10-12 13:26:31 -0700865{
Eyal Birger744d5a32015-03-01 14:58:31 +0200866 if (sock_flag(sk, SOCK_RXQ_OVFL) && skb && SOCK_SKB_CB(skb)->dropcount)
Neil Horman3b885782009-10-12 13:26:31 -0700867 put_cmsg(msg, SOL_SOCKET, SO_RXQ_OVFL,
Eyal Birger744d5a32015-03-01 14:58:31 +0200868 sizeof(__u32), &SOCK_SKB_CB(skb)->dropcount);
Neil Horman3b885782009-10-12 13:26:31 -0700869}
870
Eric Dumazet767dd032010-04-28 19:14:43 +0000871void __sock_recv_ts_and_drops(struct msghdr *msg, struct sock *sk,
Neil Horman3b885782009-10-12 13:26:31 -0700872 struct sk_buff *skb)
873{
874 sock_recv_timestamp(msg, sk, skb);
875 sock_recv_drops(msg, sk, skb);
876}
Eric Dumazet767dd032010-04-28 19:14:43 +0000877EXPORT_SYMBOL_GPL(__sock_recv_ts_and_drops);
Neil Horman3b885782009-10-12 13:26:31 -0700878
Paolo Abeni8c3c4472019-05-03 17:01:39 +0200879INDIRECT_CALLABLE_DECLARE(int inet_recvmsg(struct socket *, struct msghdr *,
Paolo Abenia648a592019-07-03 16:06:54 +0200880 size_t, int));
881INDIRECT_CALLABLE_DECLARE(int inet6_recvmsg(struct socket *, struct msghdr *,
882 size_t, int));
Ying Xue1b784142015-03-02 15:37:48 +0800883static inline int sock_recvmsg_nosec(struct socket *sock, struct msghdr *msg,
Al Viro2da62902015-03-14 21:13:46 -0400884 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885{
Paolo Abenia648a592019-07-03 16:06:54 +0200886 return INDIRECT_CALL_INET(sock->ops->recvmsg, inet6_recvmsg,
887 inet_recvmsg, sock, msg, msg_data_left(msg),
888 flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889}
890
Randy Dunlap85806af2019-05-18 21:23:07 -0700891/**
892 * sock_recvmsg - receive a message from @sock
893 * @sock: socket
894 * @msg: message to receive
895 * @flags: message flags
896 *
897 * Receives @msg from @sock, passing through LSM. Returns the total number
898 * of bytes received, or an error.
899 */
Al Viro2da62902015-03-14 21:13:46 -0400900int sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -0700901{
Al Viro2da62902015-03-14 21:13:46 -0400902 int err = security_socket_recvmsg(sock, msg, msg_data_left(msg), flags);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -0700903
Al Viro2da62902015-03-14 21:13:46 -0400904 return err ?: sock_recvmsg_nosec(sock, msg, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700906EXPORT_SYMBOL(sock_recvmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Martin Lucinac1249c02010-12-10 00:04:05 +0000908/**
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300909 * kernel_recvmsg - Receive a message from a socket (kernel space)
910 * @sock: The socket to receive the message from
911 * @msg: Received message
912 * @vec: Input s/g array for message data
913 * @num: Size of input s/g array
914 * @size: Number of bytes to read
915 * @flags: Message flags (MSG_DONTWAIT, etc...)
Martin Lucinac1249c02010-12-10 00:04:05 +0000916 *
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300917 * On return the msg structure contains the scatter/gather array passed in the
918 * vec argument. The array is modified so that it consists of the unfilled
919 * portion of the original array.
Martin Lucinac1249c02010-12-10 00:04:05 +0000920 *
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300921 * The returned value is the total number of bytes received, or an error.
Martin Lucinac1249c02010-12-10 00:04:05 +0000922 */
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300923
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700924int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
925 struct kvec *vec, size_t num, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926{
Christoph Hellwig1f466e12020-05-11 13:59:13 +0200927 msg->msg_control_is_user = false;
David Howellsaa563d72018-10-20 00:57:56 +0100928 iov_iter_kvec(&msg->msg_iter, READ, vec, num, size);
Christoph Hellwig1f466e12020-05-11 13:59:13 +0200929 return sock_recvmsg(sock, msg, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700931EXPORT_SYMBOL(kernel_recvmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -0300933static ssize_t sock_sendpage(struct file *file, struct page *page,
934 int offset, size_t size, loff_t *ppos, int more)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
936 struct socket *sock;
937 int flags;
938
Eric Dumazetb69aee02005-09-06 14:42:45 -0700939 sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Eric Dumazet35f9c092012-04-05 03:05:35 +0000941 flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
942 /* more is a combination of MSG_MORE and MSG_SENDPAGE_NOTLAST */
943 flags |= more;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Linus Torvaldse6949582009-08-13 08:28:36 -0700945 return kernel_sendpage(sock, page, offset, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946}
947
Jens Axboe9c55e012007-11-06 23:30:13 -0800948static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700949 struct pipe_inode_info *pipe, size_t len,
Jens Axboe9c55e012007-11-06 23:30:13 -0800950 unsigned int flags)
951{
952 struct socket *sock = file->private_data;
953
Rémi Denis-Courmont997b37d2008-02-15 02:35:45 -0800954 if (unlikely(!sock->ops->splice_read))
Slavomir Kaslev95506582018-11-16 11:27:53 +0200955 return generic_file_splice_read(file, ppos, pipe, len, flags);
Rémi Denis-Courmont997b37d2008-02-15 02:35:45 -0800956
Jens Axboe9c55e012007-11-06 23:30:13 -0800957 return sock->ops->splice_read(sock, ppos, pipe, len, flags);
958}
959
Al Viro8ae5e0302014-11-28 19:40:50 -0500960static ssize_t sock_read_iter(struct kiocb *iocb, struct iov_iter *to)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800961{
Al Viro6d652332015-01-30 16:12:56 -0500962 struct file *file = iocb->ki_filp;
963 struct socket *sock = file->private_data;
tadeusz.struk@intel.com0345f932015-03-19 12:31:25 -0700964 struct msghdr msg = {.msg_iter = *to,
965 .msg_iocb = iocb};
Al Viro8ae5e0302014-11-28 19:40:50 -0500966 ssize_t res;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800967
Jens Axboeebfcd892019-12-09 20:58:56 -0700968 if (file->f_flags & O_NONBLOCK || (iocb->ki_flags & IOCB_NOWAIT))
Al Viro8ae5e0302014-11-28 19:40:50 -0500969 msg.msg_flags = MSG_DONTWAIT;
970
971 if (iocb->ki_pos != 0)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800972 return -ESPIPE;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700973
Christoph Hellwig66ee59a2015-02-11 19:56:46 +0100974 if (!iov_iter_count(to)) /* Match SYS5 behaviour */
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800975 return 0;
976
Al Viro2da62902015-03-14 21:13:46 -0400977 res = sock_recvmsg(sock, &msg, msg.msg_flags);
Al Viro8ae5e0302014-11-28 19:40:50 -0500978 *to = msg.msg_iter;
979 return res;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800980}
981
Al Viro8ae5e0302014-11-28 19:40:50 -0500982static ssize_t sock_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983{
Al Viro6d652332015-01-30 16:12:56 -0500984 struct file *file = iocb->ki_filp;
985 struct socket *sock = file->private_data;
tadeusz.struk@intel.com0345f932015-03-19 12:31:25 -0700986 struct msghdr msg = {.msg_iter = *from,
987 .msg_iocb = iocb};
Al Viro8ae5e0302014-11-28 19:40:50 -0500988 ssize_t res;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800989
Al Viro8ae5e0302014-11-28 19:40:50 -0500990 if (iocb->ki_pos != 0)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800991 return -ESPIPE;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700992
Jens Axboeebfcd892019-12-09 20:58:56 -0700993 if (file->f_flags & O_NONBLOCK || (iocb->ki_flags & IOCB_NOWAIT))
Al Viro8ae5e0302014-11-28 19:40:50 -0500994 msg.msg_flags = MSG_DONTWAIT;
995
Al Viro6d652332015-01-30 16:12:56 -0500996 if (sock->type == SOCK_SEQPACKET)
997 msg.msg_flags |= MSG_EOR;
998
Al Virod8725c82014-12-11 00:02:50 -0500999 res = sock_sendmsg(sock, &msg);
Al Viro8ae5e0302014-11-28 19:40:50 -05001000 *from = msg.msg_iter;
1001 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002}
1003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004/*
1005 * Atomic setting of ioctl hooks to avoid race
1006 * with module unload.
1007 */
1008
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001009static DEFINE_MUTEX(br_ioctl_mutex);
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001010static int (*br_ioctl_hook) (struct net *, unsigned int cmd, void __user *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Eric W. Biederman881d9662007-09-17 11:56:21 -07001012void brioctl_set(int (*hook) (struct net *, unsigned int, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001014 mutex_lock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 br_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001016 mutex_unlock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017}
1018EXPORT_SYMBOL(brioctl_set);
1019
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001020static DEFINE_MUTEX(vlan_ioctl_mutex);
Eric W. Biederman881d9662007-09-17 11:56:21 -07001021static int (*vlan_ioctl_hook) (struct net *, void __user *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Eric W. Biederman881d9662007-09-17 11:56:21 -07001023void vlan_ioctl_set(int (*hook) (struct net *, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001025 mutex_lock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 vlan_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001027 mutex_unlock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028}
1029EXPORT_SYMBOL(vlan_ioctl_set);
1030
Arnd Bergmann6b960182009-11-06 23:10:54 -08001031static long sock_do_ioctl(struct net *net, struct socket *sock,
Johannes Berg63ff03a2019-01-25 22:43:17 +01001032 unsigned int cmd, unsigned long arg)
Arnd Bergmann6b960182009-11-06 23:10:54 -08001033{
1034 int err;
1035 void __user *argp = (void __user *)arg;
1036
1037 err = sock->ops->ioctl(sock, cmd, arg);
1038
1039 /*
1040 * If this ioctl is unknown try to hand it down
1041 * to the NIC driver.
1042 */
Al Viro36fd6332017-06-26 13:19:16 -04001043 if (err != -ENOIOCTLCMD)
1044 return err;
Arnd Bergmann6b960182009-11-06 23:10:54 -08001045
Al Viro36fd6332017-06-26 13:19:16 -04001046 if (cmd == SIOCGIFCONF) {
1047 struct ifconf ifc;
1048 if (copy_from_user(&ifc, argp, sizeof(struct ifconf)))
1049 return -EFAULT;
1050 rtnl_lock();
1051 err = dev_ifconf(net, &ifc, sizeof(struct ifreq));
1052 rtnl_unlock();
1053 if (!err && copy_to_user(argp, &ifc, sizeof(struct ifconf)))
1054 err = -EFAULT;
Al Viro44c02a22017-10-05 12:59:44 -04001055 } else {
1056 struct ifreq ifr;
1057 bool need_copyout;
Johannes Berg63ff03a2019-01-25 22:43:17 +01001058 if (copy_from_user(&ifr, argp, sizeof(struct ifreq)))
Al Viro44c02a22017-10-05 12:59:44 -04001059 return -EFAULT;
1060 err = dev_ioctl(net, cmd, &ifr, &need_copyout);
1061 if (!err && need_copyout)
Johannes Berg63ff03a2019-01-25 22:43:17 +01001062 if (copy_to_user(argp, &ifr, sizeof(struct ifreq)))
Al Viro44c02a22017-10-05 12:59:44 -04001063 return -EFAULT;
Al Viro36fd6332017-06-26 13:19:16 -04001064 }
Arnd Bergmann6b960182009-11-06 23:10:54 -08001065 return err;
1066}
1067
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068/*
1069 * With an ioctl, arg may well be a user mode pointer, but we don't know
1070 * what to do with it - that's up to the protocol still.
1071 */
1072
Pedro Tammela8a3c2452019-03-14 10:45:23 -03001073/**
1074 * get_net_ns - increment the refcount of the network namespace
1075 * @ns: common namespace (net)
1076 *
1077 * Returns the net's common namespace.
1078 */
1079
Kirill Tkhaid8d211a2018-02-14 16:39:56 +03001080struct ns_common *get_net_ns(struct ns_common *ns)
Andrey Vaginc62cce22016-10-24 18:29:13 -07001081{
1082 return &get_net(container_of(ns, struct net, ns))->ns;
1083}
Kirill Tkhaid8d211a2018-02-14 16:39:56 +03001084EXPORT_SYMBOL_GPL(get_net_ns);
Andrey Vaginc62cce22016-10-24 18:29:13 -07001085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
1087{
1088 struct socket *sock;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001089 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 void __user *argp = (void __user *)arg;
1091 int pid, err;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001092 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
Eric Dumazetb69aee02005-09-06 14:42:45 -07001094 sock = file->private_data;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001095 sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001096 net = sock_net(sk);
Al Viro44c02a22017-10-05 12:59:44 -04001097 if (unlikely(cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15))) {
1098 struct ifreq ifr;
1099 bool need_copyout;
1100 if (copy_from_user(&ifr, argp, sizeof(struct ifreq)))
1101 return -EFAULT;
1102 err = dev_ioctl(net, cmd, &ifr, &need_copyout);
1103 if (!err && need_copyout)
1104 if (copy_to_user(argp, &ifr, sizeof(struct ifreq)))
1105 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 } else
Johannes Berg3d23e342009-09-29 23:27:28 +02001107#ifdef CONFIG_WEXT_CORE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
Al Virob1b0c242017-10-01 20:13:08 -04001109 err = wext_handle_ioctl(net, cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 } else
Johannes Berg3d23e342009-09-29 23:27:28 +02001111#endif
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001112 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 case FIOSETOWN:
1114 case SIOCSPGRP:
1115 err = -EFAULT;
1116 if (get_user(pid, (int __user *)argp))
1117 break;
Jiri Slaby393cc3f2017-06-13 13:35:50 +02001118 err = f_setown(sock->file, pid, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 break;
1120 case FIOGETOWN:
1121 case SIOCGPGRP:
Eric W. Biederman609d7fa2006-10-02 02:17:15 -07001122 err = put_user(f_getown(sock->file),
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001123 (int __user *)argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 break;
1125 case SIOCGIFBR:
1126 case SIOCSIFBR:
1127 case SIOCBRADDBR:
1128 case SIOCBRDELBR:
1129 err = -ENOPKG;
1130 if (!br_ioctl_hook)
1131 request_module("bridge");
1132
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001133 mutex_lock(&br_ioctl_mutex);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001134 if (br_ioctl_hook)
Eric W. Biederman881d9662007-09-17 11:56:21 -07001135 err = br_ioctl_hook(net, cmd, argp);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001136 mutex_unlock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 break;
1138 case SIOCGIFVLAN:
1139 case SIOCSIFVLAN:
1140 err = -ENOPKG;
1141 if (!vlan_ioctl_hook)
1142 request_module("8021q");
1143
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001144 mutex_lock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 if (vlan_ioctl_hook)
Eric W. Biederman881d9662007-09-17 11:56:21 -07001146 err = vlan_ioctl_hook(net, argp);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001147 mutex_unlock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 break;
Andrey Vaginc62cce22016-10-24 18:29:13 -07001149 case SIOCGSKNS:
1150 err = -EPERM;
1151 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1152 break;
1153
1154 err = open_related_ns(&net->ns, get_net_ns);
1155 break;
Arnd Bergmann0768e172019-04-17 22:56:11 +02001156 case SIOCGSTAMP_OLD:
1157 case SIOCGSTAMPNS_OLD:
Arnd Bergmannc7cbdbf2019-04-17 22:51:48 +02001158 if (!sock->ops->gettstamp) {
1159 err = -ENOIOCTLCMD;
1160 break;
1161 }
1162 err = sock->ops->gettstamp(sock, argp,
Arnd Bergmann0768e172019-04-17 22:56:11 +02001163 cmd == SIOCGSTAMP_OLD,
1164 !IS_ENABLED(CONFIG_64BIT));
Gustavo A. R. Silva60747822019-04-24 10:31:24 -05001165 break;
Arnd Bergmann0768e172019-04-17 22:56:11 +02001166 case SIOCGSTAMP_NEW:
1167 case SIOCGSTAMPNS_NEW:
1168 if (!sock->ops->gettstamp) {
1169 err = -ENOIOCTLCMD;
1170 break;
1171 }
1172 err = sock->ops->gettstamp(sock, argp,
1173 cmd == SIOCGSTAMP_NEW,
1174 false);
Arnd Bergmannc7cbdbf2019-04-17 22:51:48 +02001175 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 default:
Johannes Berg63ff03a2019-01-25 22:43:17 +01001177 err = sock_do_ioctl(net, sock, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 break;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 return err;
1181}
1182
Pedro Tammela8a3c2452019-03-14 10:45:23 -03001183/**
1184 * sock_create_lite - creates a socket
1185 * @family: protocol family (AF_INET, ...)
1186 * @type: communication type (SOCK_STREAM, ...)
1187 * @protocol: protocol (0, ...)
1188 * @res: new socket
1189 *
1190 * Creates a new socket and assigns it to @res, passing through LSM.
1191 * The new socket initialization is not complete, see kernel_accept().
1192 * Returns 0 or an error. On failure @res is set to %NULL.
1193 * This function internally uses GFP_KERNEL.
1194 */
1195
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196int sock_create_lite(int family, int type, int protocol, struct socket **res)
1197{
1198 int err;
1199 struct socket *sock = NULL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001200
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 err = security_socket_create(family, type, protocol, 1);
1202 if (err)
1203 goto out;
1204
1205 sock = sock_alloc();
1206 if (!sock) {
1207 err = -ENOMEM;
1208 goto out;
1209 }
1210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 sock->type = type;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001212 err = security_socket_post_create(sock, family, type, protocol, 1);
1213 if (err)
1214 goto out_release;
1215
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216out:
1217 *res = sock;
1218 return err;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001219out_release:
1220 sock_release(sock);
1221 sock = NULL;
1222 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001224EXPORT_SYMBOL(sock_create_lite);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226/* No kernel lock held - perfect */
Al Viroade994f2017-07-03 00:01:49 -04001227static __poll_t sock_poll(struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228{
Christoph Hellwig3cafb372018-01-09 16:07:34 +01001229 struct socket *sock = file->private_data;
Christoph Hellwiga331de32018-07-30 09:42:13 +02001230 __poll_t events = poll_requested_events(wait), flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Christoph Hellwige88958e2018-06-29 15:37:24 +02001232 if (!sock->ops->poll)
1233 return 0;
Christoph Hellwigf641f13b2018-07-30 09:42:12 +02001234
Christoph Hellwiga331de32018-07-30 09:42:13 +02001235 if (sk_can_busy_loop(sock->sk)) {
1236 /* poll once if requested by the syscall */
1237 if (events & POLL_BUSY_LOOP)
1238 sk_busy_loop(sock->sk, 1);
1239
1240 /* if this socket can poll_ll, tell the system call */
1241 flag = POLL_BUSY_LOOP;
1242 }
1243
1244 return sock->ops->poll(file, sock, wait) | flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245}
1246
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001247static int sock_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248{
Eric Dumazetb69aee02005-09-06 14:42:45 -07001249 struct socket *sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
1251 return sock->ops->mmap(file, sock, vma);
1252}
1253
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001254static int sock_close(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255{
Cong Wang6d8c50d2018-06-07 13:39:49 -07001256 __sock_release(SOCKET_I(inode), inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 return 0;
1258}
1259
1260/*
1261 * Update the socket async list
1262 *
1263 * Fasync_list locking strategy.
1264 *
1265 * 1. fasync_list is modified only under process context socket lock
1266 * i.e. under semaphore.
1267 * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
Eric Dumazet989a2972010-04-14 09:55:35 +00001268 * or under socket lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 */
1270
1271static int sock_fasync(int fd, struct file *filp, int on)
1272{
Eric Dumazet989a2972010-04-14 09:55:35 +00001273 struct socket *sock = filp->private_data;
1274 struct sock *sk = sock->sk;
Al Viro333f7902019-07-05 20:14:16 +01001275 struct socket_wq *wq = &sock->wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
Eric Dumazet989a2972010-04-14 09:55:35 +00001277 if (sk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
1280 lock_sock(sk);
Eric Dumazeteaefd1102011-02-18 03:26:36 +00001281 fasync_helper(fd, filp, on, &wq->fasync_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
Eric Dumazeteaefd1102011-02-18 03:26:36 +00001283 if (!wq->fasync_list)
Eric Dumazet989a2972010-04-14 09:55:35 +00001284 sock_reset_flag(sk, SOCK_FASYNC);
Jonathan Corbet76398422009-02-01 14:26:59 -07001285 else
Eric Dumazetbcdce712009-10-06 17:28:29 -07001286 sock_set_flag(sk, SOCK_FASYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
Eric Dumazet989a2972010-04-14 09:55:35 +00001288 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 return 0;
1290}
1291
Eric Dumazetceb5d582015-11-29 20:03:11 -08001292/* This function may be called only under rcu_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
Eric Dumazetceb5d582015-11-29 20:03:11 -08001294int sock_wake_async(struct socket_wq *wq, int how, int band)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295{
Eric Dumazetceb5d582015-11-29 20:03:11 -08001296 if (!wq || !wq->fasync_list)
1297 return -1;
Eric Dumazet43815482010-04-29 11:01:49 +00001298
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001299 switch (how) {
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001300 case SOCK_WAKE_WAITD:
Eric Dumazetceb5d582015-11-29 20:03:11 -08001301 if (test_bit(SOCKWQ_ASYNC_WAITDATA, &wq->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 break;
1303 goto call_kill;
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001304 case SOCK_WAKE_SPACE:
Eric Dumazetceb5d582015-11-29 20:03:11 -08001305 if (!test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 break;
Miaohe Lin7c7ab582020-08-08 16:23:10 +08001307 fallthrough;
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001308 case SOCK_WAKE_IO:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001309call_kill:
Eric Dumazet43815482010-04-29 11:01:49 +00001310 kill_fasync(&wq->fasync_list, SIGIO, band);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 break;
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001312 case SOCK_WAKE_URG:
Eric Dumazet43815482010-04-29 11:01:49 +00001313 kill_fasync(&wq->fasync_list, SIGURG, band);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 }
Eric Dumazetceb5d582015-11-29 20:03:11 -08001315
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 return 0;
1317}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001318EXPORT_SYMBOL(sock_wake_async);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
Pedro Tammela8a3c2452019-03-14 10:45:23 -03001320/**
1321 * __sock_create - creates a socket
1322 * @net: net namespace
1323 * @family: protocol family (AF_INET, ...)
1324 * @type: communication type (SOCK_STREAM, ...)
1325 * @protocol: protocol (0, ...)
1326 * @res: new socket
1327 * @kern: boolean for kernel space sockets
1328 *
1329 * Creates a new socket and assigns it to @res, passing through LSM.
1330 * Returns 0 or an error. On failure @res is set to %NULL. @kern must
1331 * be set to true if the socket resides in kernel space.
1332 * This function internally uses GFP_KERNEL.
1333 */
1334
Pavel Emelyanov721db932010-09-29 16:06:32 +04001335int __sock_create(struct net *net, int family, int type, int protocol,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001336 struct socket **res, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337{
1338 int err;
1339 struct socket *sock;
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001340 const struct net_proto_family *pf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
1342 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001343 * Check protocol is in range
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 */
1345 if (family < 0 || family >= NPROTO)
1346 return -EAFNOSUPPORT;
1347 if (type < 0 || type >= SOCK_MAX)
1348 return -EINVAL;
1349
1350 /* Compatibility.
1351
1352 This uglymoron is moved from INET layer to here to avoid
1353 deadlock in module load.
1354 */
1355 if (family == PF_INET && type == SOCK_PACKET) {
liping.zhangf3c98692016-03-11 23:08:36 +08001356 pr_info_once("%s uses obsolete (PF_INET,SOCK_PACKET)\n",
1357 current->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 family = PF_PACKET;
1359 }
1360
1361 err = security_socket_create(family, type, protocol, kern);
1362 if (err)
1363 return err;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001364
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001365 /*
1366 * Allocate the socket and allow the family to set things up. if
1367 * the protocol is 0, the family is instructed to select an appropriate
1368 * default.
1369 */
1370 sock = sock_alloc();
1371 if (!sock) {
Joe Perchese87cc472012-05-13 21:56:26 +00001372 net_warn_ratelimited("socket: no more sockets\n");
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001373 return -ENFILE; /* Not exactly a match, but its the
1374 closest posix thing */
1375 }
1376
1377 sock->type = type;
1378
Johannes Berg95a5afc2008-10-16 15:24:51 -07001379#ifdef CONFIG_MODULES
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001380 /* Attempt to load a protocol module if the find failed.
1381 *
1382 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 * requested real, full-featured networking support upon configuration.
1384 * Otherwise module support will break!
1385 */
Eric Dumazet190683a2010-11-10 10:50:44 +00001386 if (rcu_access_pointer(net_families[family]) == NULL)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001387 request_module("net-pf-%d", family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388#endif
1389
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001390 rcu_read_lock();
1391 pf = rcu_dereference(net_families[family]);
1392 err = -EAFNOSUPPORT;
1393 if (!pf)
1394 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
1396 /*
1397 * We will call the ->create function, that possibly is in a loadable
1398 * module, so we have to bump that loadable module refcnt first.
1399 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001400 if (!try_module_get(pf->owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 goto out_release;
1402
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001403 /* Now protected by module ref count */
1404 rcu_read_unlock();
1405
Eric Paris3f378b62009-11-05 22:18:14 -08001406 err = pf->create(net, sock, protocol, kern);
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001407 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 goto out_module_put;
Frank Filza79af592005-09-27 15:23:38 -07001409
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 /*
1411 * Now to bump the refcnt of the [loadable] module that owns this
1412 * socket at sock_release time we decrement its refcnt.
1413 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001414 if (!try_module_get(sock->ops->owner))
1415 goto out_module_busy;
1416
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 /*
1418 * Now that we're done with the ->create function, the [loadable]
1419 * module can have its refcnt decremented
1420 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001421 module_put(pf->owner);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001422 err = security_socket_post_create(sock, family, type, protocol, kern);
1423 if (err)
Herbert Xu3b185522007-08-15 14:46:02 -07001424 goto out_sock_release;
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001425 *res = sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001427 return 0;
1428
1429out_module_busy:
1430 err = -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431out_module_put:
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001432 sock->ops = NULL;
1433 module_put(pf->owner);
1434out_sock_release:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 sock_release(sock);
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001436 return err;
1437
1438out_release:
1439 rcu_read_unlock();
1440 goto out_sock_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441}
Pavel Emelyanov721db932010-09-29 16:06:32 +04001442EXPORT_SYMBOL(__sock_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Pedro Tammela8a3c2452019-03-14 10:45:23 -03001444/**
1445 * sock_create - creates a socket
1446 * @family: protocol family (AF_INET, ...)
1447 * @type: communication type (SOCK_STREAM, ...)
1448 * @protocol: protocol (0, ...)
1449 * @res: new socket
1450 *
1451 * A wrapper around __sock_create().
1452 * Returns 0 or an error. This function internally uses GFP_KERNEL.
1453 */
1454
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455int sock_create(int family, int type, int protocol, struct socket **res)
1456{
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001457 return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001459EXPORT_SYMBOL(sock_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Pedro Tammela8a3c2452019-03-14 10:45:23 -03001461/**
1462 * sock_create_kern - creates a socket (kernel space)
1463 * @net: net namespace
1464 * @family: protocol family (AF_INET, ...)
1465 * @type: communication type (SOCK_STREAM, ...)
1466 * @protocol: protocol (0, ...)
1467 * @res: new socket
1468 *
1469 * A wrapper around __sock_create().
1470 * Returns 0 or an error. This function internally uses GFP_KERNEL.
1471 */
1472
Eric W. Biedermaneeb1bd52015-05-08 21:08:05 -05001473int sock_create_kern(struct net *net, int family, int type, int protocol, struct socket **res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474{
Eric W. Biedermaneeb1bd52015-05-08 21:08:05 -05001475 return __sock_create(net, family, type, protocol, res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001477EXPORT_SYMBOL(sock_create_kern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
Dominik Brodowski9d6a15c2018-03-13 19:29:43 +01001479int __sys_socket(int family, int type, int protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480{
1481 int retval;
1482 struct socket *sock;
Ulrich Dreppera677a032008-07-23 21:29:17 -07001483 int flags;
1484
Ulrich Dreppere38b36f2008-07-23 21:29:42 -07001485 /* Check the SOCK_* constants for consistency. */
1486 BUILD_BUG_ON(SOCK_CLOEXEC != O_CLOEXEC);
1487 BUILD_BUG_ON((SOCK_MAX | SOCK_TYPE_MASK) != SOCK_TYPE_MASK);
1488 BUILD_BUG_ON(SOCK_CLOEXEC & SOCK_TYPE_MASK);
1489 BUILD_BUG_ON(SOCK_NONBLOCK & SOCK_TYPE_MASK);
1490
Ulrich Dreppera677a032008-07-23 21:29:17 -07001491 flags = type & ~SOCK_TYPE_MASK;
Ulrich Drepper77d27202008-07-23 21:29:35 -07001492 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Dreppera677a032008-07-23 21:29:17 -07001493 return -EINVAL;
1494 type &= SOCK_TYPE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001496 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1497 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1498
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 retval = sock_create(family, type, protocol, &sock);
1500 if (retval < 0)
Al Viro8e1611e2017-12-05 23:29:09 +00001501 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
Al Viro8e1611e2017-12-05 23:29:09 +00001503 return sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504}
1505
Dominik Brodowski9d6a15c2018-03-13 19:29:43 +01001506SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol)
1507{
1508 return __sys_socket(family, type, protocol);
1509}
1510
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511/*
1512 * Create a pair of connected sockets.
1513 */
1514
Dominik Brodowski6debc8d2018-03-13 19:49:23 +01001515int __sys_socketpair(int family, int type, int protocol, int __user *usockvec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516{
1517 struct socket *sock1, *sock2;
1518 int fd1, fd2, err;
Al Virodb349502007-02-07 01:48:00 -05001519 struct file *newfile1, *newfile2;
Ulrich Dreppera677a032008-07-23 21:29:17 -07001520 int flags;
1521
1522 flags = type & ~SOCK_TYPE_MASK;
Ulrich Drepper77d27202008-07-23 21:29:35 -07001523 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Dreppera677a032008-07-23 21:29:17 -07001524 return -EINVAL;
1525 type &= SOCK_TYPE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001527 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1528 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1529
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 /*
Al Viro016a2662017-12-05 23:28:38 +00001531 * reserve descriptors and make sure we won't fail
1532 * to return them to userland.
1533 */
1534 fd1 = get_unused_fd_flags(flags);
1535 if (unlikely(fd1 < 0))
1536 return fd1;
1537
1538 fd2 = get_unused_fd_flags(flags);
1539 if (unlikely(fd2 < 0)) {
1540 put_unused_fd(fd1);
1541 return fd2;
1542 }
1543
1544 err = put_user(fd1, &usockvec[0]);
1545 if (err)
1546 goto out;
1547
1548 err = put_user(fd2, &usockvec[1]);
1549 if (err)
1550 goto out;
1551
1552 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 * Obtain the first socket and check if the underlying protocol
1554 * supports the socketpair call.
1555 */
1556
1557 err = sock_create(family, type, protocol, &sock1);
Al Viro016a2662017-12-05 23:28:38 +00001558 if (unlikely(err < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 goto out;
1560
1561 err = sock_create(family, type, protocol, &sock2);
Al Viro016a2662017-12-05 23:28:38 +00001562 if (unlikely(err < 0)) {
1563 sock_release(sock1);
1564 goto out;
David S. Millerbf3c23d2007-10-29 21:54:02 -07001565 }
Yann Droneaudd73aa282013-12-09 22:42:20 +01001566
David Herrmannd47cd942018-05-04 16:28:20 +02001567 err = security_socket_socketpair(sock1, sock2);
1568 if (unlikely(err)) {
1569 sock_release(sock2);
1570 sock_release(sock1);
1571 goto out;
1572 }
1573
Al Viro016a2662017-12-05 23:28:38 +00001574 err = sock1->ops->socketpair(sock1, sock2);
1575 if (unlikely(err < 0)) {
1576 sock_release(sock2);
1577 sock_release(sock1);
1578 goto out;
Al Viro28407632012-08-17 23:54:15 -04001579 }
1580
Linus Torvaldsaab174f2012-10-02 20:25:04 -07001581 newfile1 = sock_alloc_file(sock1, flags, NULL);
Viresh Kumarb5ffe632015-08-12 15:59:47 +05301582 if (IS_ERR(newfile1)) {
Al Viro28407632012-08-17 23:54:15 -04001583 err = PTR_ERR(newfile1);
Al Viro016a2662017-12-05 23:28:38 +00001584 sock_release(sock2);
1585 goto out;
Al Viro28407632012-08-17 23:54:15 -04001586 }
1587
Linus Torvaldsaab174f2012-10-02 20:25:04 -07001588 newfile2 = sock_alloc_file(sock2, flags, NULL);
Al Viro28407632012-08-17 23:54:15 -04001589 if (IS_ERR(newfile2)) {
1590 err = PTR_ERR(newfile2);
Al Viro016a2662017-12-05 23:28:38 +00001591 fput(newfile1);
1592 goto out;
Al Virodb349502007-02-07 01:48:00 -05001593 }
1594
Al Viro157cf642008-12-14 04:57:47 -05001595 audit_fd_pair(fd1, fd2);
Yann Droneaudd73aa282013-12-09 22:42:20 +01001596
Al Virodb349502007-02-07 01:48:00 -05001597 fd_install(fd1, newfile1);
1598 fd_install(fd2, newfile2);
Yann Droneaudd73aa282013-12-09 22:42:20 +01001599 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601out:
Al Viro016a2662017-12-05 23:28:38 +00001602 put_unused_fd(fd2);
1603 put_unused_fd(fd1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 return err;
1605}
1606
Dominik Brodowski6debc8d2018-03-13 19:49:23 +01001607SYSCALL_DEFINE4(socketpair, int, family, int, type, int, protocol,
1608 int __user *, usockvec)
1609{
1610 return __sys_socketpair(family, type, protocol, usockvec);
1611}
1612
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613/*
1614 * Bind a name to a socket. Nothing much to do here since it's
1615 * the protocol's responsibility to handle the local address.
1616 *
1617 * We move the socket address to kernel space before we call
1618 * the protocol layer (having also checked the address is ok).
1619 */
1620
Dominik Brodowskia87d35d2018-03-13 19:33:09 +01001621int __sys_bind(int fd, struct sockaddr __user *umyaddr, int addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622{
1623 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001624 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001625 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001627 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001628 if (sock) {
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001629 err = move_addr_to_kernel(umyaddr, addrlen, &address);
Jakub Sitnicki068b88c2018-10-04 11:09:40 +02001630 if (!err) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001631 err = security_socket_bind(sock,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001632 (struct sockaddr *)&address,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001633 addrlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001634 if (!err)
1635 err = sock->ops->bind(sock,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001636 (struct sockaddr *)
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001637 &address, addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 }
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001639 fput_light(sock->file, fput_needed);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 return err;
1642}
1643
Dominik Brodowskia87d35d2018-03-13 19:33:09 +01001644SYSCALL_DEFINE3(bind, int, fd, struct sockaddr __user *, umyaddr, int, addrlen)
1645{
1646 return __sys_bind(fd, umyaddr, addrlen);
1647}
1648
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649/*
1650 * Perform a listen. Basically, we allow the protocol to do anything
1651 * necessary for a listen, and if that works, we mark the socket as
1652 * ready for listening.
1653 */
1654
Dominik Brodowski25e290e2018-03-13 19:36:54 +01001655int __sys_listen(int fd, int backlog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656{
1657 struct socket *sock;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001658 int err, fput_needed;
Pavel Emelyanovb8e1f9b2007-12-08 00:12:33 -08001659 int somaxconn;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001660
1661 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1662 if (sock) {
Pavel Emelyanov8efa6e92008-03-31 19:41:14 -07001663 somaxconn = sock_net(sock->sk)->core.sysctl_somaxconn;
Eric Dumazet95c96172012-04-15 05:58:06 +00001664 if ((unsigned int)backlog > somaxconn)
Pavel Emelyanovb8e1f9b2007-12-08 00:12:33 -08001665 backlog = somaxconn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
1667 err = security_socket_listen(sock, backlog);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001668 if (!err)
1669 err = sock->ops->listen(sock, backlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001671 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 }
1673 return err;
1674}
1675
Dominik Brodowski25e290e2018-03-13 19:36:54 +01001676SYSCALL_DEFINE2(listen, int, fd, int, backlog)
1677{
1678 return __sys_listen(fd, backlog);
1679}
1680
Jens Axboede2ea4b2019-10-17 14:41:29 -06001681int __sys_accept4_file(struct file *file, unsigned file_flags,
1682 struct sockaddr __user *upeer_sockaddr,
Jens Axboe09952e32020-03-19 20:16:56 -06001683 int __user *upeer_addrlen, int flags,
1684 unsigned long nofile)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685{
1686 struct socket *sock, *newsock;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001687 struct file *newfile;
Jens Axboede2ea4b2019-10-17 14:41:29 -06001688 int err, len, newfd;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001689 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
Ulrich Drepper77d27202008-07-23 21:29:35 -07001691 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001692 return -EINVAL;
1693
1694 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1695 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1696
Florent Revestdba4a922020-12-04 12:36:04 +01001697 sock = sock_from_file(file);
1698 if (!sock) {
1699 err = -ENOTSOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 goto out;
Florent Revestdba4a922020-12-04 12:36:04 +01001701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
1703 err = -ENFILE;
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001704 newsock = sock_alloc();
1705 if (!newsock)
Jens Axboede2ea4b2019-10-17 14:41:29 -06001706 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
1708 newsock->type = sock->type;
1709 newsock->ops = sock->ops;
1710
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 /*
1712 * We don't need try_module_get here, as the listening socket (sock)
1713 * has the protocol module (sock->ops->owner) held.
1714 */
1715 __module_get(newsock->ops->owner);
1716
Jens Axboe09952e32020-03-19 20:16:56 -06001717 newfd = __get_unused_fd_flags(flags, nofile);
David S. Miller39d8c1b2006-03-20 17:13:49 -08001718 if (unlikely(newfd < 0)) {
1719 err = newfd;
David S. Miller9a1875e2006-04-01 12:48:36 -08001720 sock_release(newsock);
Jens Axboede2ea4b2019-10-17 14:41:29 -06001721 goto out;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001722 }
Linus Torvaldsaab174f2012-10-02 20:25:04 -07001723 newfile = sock_alloc_file(newsock, flags, sock->sk->sk_prot_creator->name);
Viresh Kumarb5ffe632015-08-12 15:59:47 +05301724 if (IS_ERR(newfile)) {
Al Viro28407632012-08-17 23:54:15 -04001725 err = PTR_ERR(newfile);
1726 put_unused_fd(newfd);
Jens Axboede2ea4b2019-10-17 14:41:29 -06001727 goto out;
Al Viro28407632012-08-17 23:54:15 -04001728 }
David S. Miller39d8c1b2006-03-20 17:13:49 -08001729
Frank Filza79af592005-09-27 15:23:38 -07001730 err = security_socket_accept(sock, newsock);
1731 if (err)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001732 goto out_fd;
Frank Filza79af592005-09-27 15:23:38 -07001733
Jens Axboede2ea4b2019-10-17 14:41:29 -06001734 err = sock->ops->accept(sock, newsock, sock->file->f_flags | file_flags,
1735 false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 if (err < 0)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001737 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
1739 if (upeer_sockaddr) {
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001740 len = newsock->ops->getname(newsock,
1741 (struct sockaddr *)&address, 2);
1742 if (len < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 err = -ECONNABORTED;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001744 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 }
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001746 err = move_addr_to_user(&address,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001747 len, upeer_sockaddr, upeer_addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 if (err < 0)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001749 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 }
1751
1752 /* File flags are not inherited via accept() unlike another OSes. */
1753
David S. Miller39d8c1b2006-03-20 17:13:49 -08001754 fd_install(newfd, newfile);
1755 err = newfd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756out:
1757 return err;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001758out_fd:
David S. Miller9606a212006-04-01 01:00:14 -08001759 fput(newfile);
David S. Miller39d8c1b2006-03-20 17:13:49 -08001760 put_unused_fd(newfd);
Jens Axboede2ea4b2019-10-17 14:41:29 -06001761 goto out;
1762
1763}
1764
1765/*
1766 * For accept, we attempt to create a new socket, set up the link
1767 * with the client, wake up the client, then return the new
1768 * connected fd. We collect the address of the connector in kernel
1769 * space and move it to user at the very end. This is unclean because
1770 * we open the socket then return an error.
1771 *
1772 * 1003.1g adds the ability to recvmsg() to query connection pending
1773 * status to recvmsg. We need to add that support in a way thats
1774 * clean when we restructure accept also.
1775 */
1776
1777int __sys_accept4(int fd, struct sockaddr __user *upeer_sockaddr,
1778 int __user *upeer_addrlen, int flags)
1779{
1780 int ret = -EBADF;
1781 struct fd f;
1782
1783 f = fdget(fd);
1784 if (f.file) {
1785 ret = __sys_accept4_file(f.file, 0, upeer_sockaddr,
Jens Axboe09952e32020-03-19 20:16:56 -06001786 upeer_addrlen, flags,
1787 rlimit(RLIMIT_NOFILE));
Miaohe Lin6b07ede2020-08-06 19:52:24 +08001788 fdput(f);
Jens Axboede2ea4b2019-10-17 14:41:29 -06001789 }
1790
1791 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792}
1793
Dominik Brodowski4541e802018-03-13 19:24:23 +01001794SYSCALL_DEFINE4(accept4, int, fd, struct sockaddr __user *, upeer_sockaddr,
1795 int __user *, upeer_addrlen, int, flags)
1796{
1797 return __sys_accept4(fd, upeer_sockaddr, upeer_addrlen, flags);
1798}
1799
Heiko Carstens20f37032009-01-14 14:14:23 +01001800SYSCALL_DEFINE3(accept, int, fd, struct sockaddr __user *, upeer_sockaddr,
1801 int __user *, upeer_addrlen)
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001802{
Dominik Brodowski4541e802018-03-13 19:24:23 +01001803 return __sys_accept4(fd, upeer_sockaddr, upeer_addrlen, 0);
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001804}
1805
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806/*
1807 * Attempt to connect to a socket with the server address. The address
1808 * is in user space so we verify it is OK and move it to kernel space.
1809 *
1810 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
1811 * break bindings
1812 *
1813 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
1814 * other SEQPACKET protocols that take time to connect() as it doesn't
1815 * include the -EINPROGRESS status for such sockets.
1816 */
1817
Jens Axboef499a022019-12-02 16:28:46 -07001818int __sys_connect_file(struct file *file, struct sockaddr_storage *address,
Jens Axboebd3ded32019-11-23 14:17:16 -07001819 int addrlen, int file_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820{
1821 struct socket *sock;
Jens Axboebd3ded32019-11-23 14:17:16 -07001822 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
Florent Revestdba4a922020-12-04 12:36:04 +01001824 sock = sock_from_file(file);
1825 if (!sock) {
1826 err = -ENOTSOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 goto out;
Florent Revestdba4a922020-12-04 12:36:04 +01001828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001830 err =
Jens Axboef499a022019-12-02 16:28:46 -07001831 security_socket_connect(sock, (struct sockaddr *)address, addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 if (err)
Jens Axboebd3ded32019-11-23 14:17:16 -07001833 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
Jens Axboef499a022019-12-02 16:28:46 -07001835 err = sock->ops->connect(sock, (struct sockaddr *)address, addrlen,
Jens Axboebd3ded32019-11-23 14:17:16 -07001836 sock->file->f_flags | file_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837out:
1838 return err;
1839}
1840
Jens Axboebd3ded32019-11-23 14:17:16 -07001841int __sys_connect(int fd, struct sockaddr __user *uservaddr, int addrlen)
1842{
1843 int ret = -EBADF;
1844 struct fd f;
1845
1846 f = fdget(fd);
1847 if (f.file) {
Jens Axboef499a022019-12-02 16:28:46 -07001848 struct sockaddr_storage address;
1849
1850 ret = move_addr_to_kernel(uservaddr, addrlen, &address);
1851 if (!ret)
1852 ret = __sys_connect_file(f.file, &address, addrlen, 0);
Miaohe Lin6b07ede2020-08-06 19:52:24 +08001853 fdput(f);
Jens Axboebd3ded32019-11-23 14:17:16 -07001854 }
1855
1856 return ret;
1857}
1858
Dominik Brodowski1387c2c2018-03-13 19:35:09 +01001859SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
1860 int, addrlen)
1861{
1862 return __sys_connect(fd, uservaddr, addrlen);
1863}
1864
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865/*
1866 * Get the local address ('name') of a socket object. Move the obtained
1867 * name to user space.
1868 */
1869
Dominik Brodowski8882a102018-03-13 19:43:14 +01001870int __sys_getsockname(int fd, struct sockaddr __user *usockaddr,
1871 int __user *usockaddr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872{
1873 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001874 struct sockaddr_storage address;
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001875 int err, fput_needed;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001876
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001877 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 if (!sock)
1879 goto out;
1880
1881 err = security_socket_getsockname(sock);
1882 if (err)
1883 goto out_put;
1884
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001885 err = sock->ops->getname(sock, (struct sockaddr *)&address, 0);
1886 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 goto out_put;
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001888 /* "err" is actually length in this case */
1889 err = move_addr_to_user(&address, err, usockaddr, usockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
1891out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001892 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893out:
1894 return err;
1895}
1896
Dominik Brodowski8882a102018-03-13 19:43:14 +01001897SYSCALL_DEFINE3(getsockname, int, fd, struct sockaddr __user *, usockaddr,
1898 int __user *, usockaddr_len)
1899{
1900 return __sys_getsockname(fd, usockaddr, usockaddr_len);
1901}
1902
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903/*
1904 * Get the remote address ('name') of a socket object. Move the obtained
1905 * name to user space.
1906 */
1907
Dominik Brodowskib21c8f82018-03-13 19:47:00 +01001908int __sys_getpeername(int fd, struct sockaddr __user *usockaddr,
1909 int __user *usockaddr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910{
1911 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001912 struct sockaddr_storage address;
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001913 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001915 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1916 if (sock != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 err = security_socket_getpeername(sock);
1918 if (err) {
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001919 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 return err;
1921 }
1922
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001923 err = sock->ops->getname(sock, (struct sockaddr *)&address, 1);
1924 if (err >= 0)
1925 /* "err" is actually length in this case */
1926 err = move_addr_to_user(&address, err, usockaddr,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001927 usockaddr_len);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001928 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 }
1930 return err;
1931}
1932
Dominik Brodowskib21c8f82018-03-13 19:47:00 +01001933SYSCALL_DEFINE3(getpeername, int, fd, struct sockaddr __user *, usockaddr,
1934 int __user *, usockaddr_len)
1935{
1936 return __sys_getpeername(fd, usockaddr, usockaddr_len);
1937}
1938
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939/*
1940 * Send a datagram to a given address. We move the address into kernel
1941 * space and check the user space data area is readable before invoking
1942 * the protocol.
1943 */
Dominik Brodowski211b6342018-03-13 19:18:52 +01001944int __sys_sendto(int fd, void __user *buff, size_t len, unsigned int flags,
1945 struct sockaddr __user *addr, int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946{
1947 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001948 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 int err;
1950 struct msghdr msg;
1951 struct iovec iov;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001952 int fput_needed;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001953
Al Viro602bd0e2015-03-21 19:12:32 -04001954 err = import_single_range(WRITE, buff, len, &iov, &msg.msg_iter);
1955 if (unlikely(err))
1956 return err;
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001957 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1958 if (!sock)
David S. Miller4387ff72007-02-08 15:06:08 -08001959 goto out;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001960
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001961 msg.msg_name = NULL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001962 msg.msg_control = NULL;
1963 msg.msg_controllen = 0;
1964 msg.msg_namelen = 0;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001965 if (addr) {
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001966 err = move_addr_to_kernel(addr, addr_len, &address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 if (err < 0)
1968 goto out_put;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001969 msg.msg_name = (struct sockaddr *)&address;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001970 msg.msg_namelen = addr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 }
1972 if (sock->file->f_flags & O_NONBLOCK)
1973 flags |= MSG_DONTWAIT;
1974 msg.msg_flags = flags;
Al Virod8725c82014-12-11 00:02:50 -05001975 err = sock_sendmsg(sock, &msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001977out_put:
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001978 fput_light(sock->file, fput_needed);
David S. Miller4387ff72007-02-08 15:06:08 -08001979out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 return err;
1981}
1982
Dominik Brodowski211b6342018-03-13 19:18:52 +01001983SYSCALL_DEFINE6(sendto, int, fd, void __user *, buff, size_t, len,
1984 unsigned int, flags, struct sockaddr __user *, addr,
1985 int, addr_len)
1986{
1987 return __sys_sendto(fd, buff, len, flags, addr, addr_len);
1988}
1989
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001991 * Send a datagram down a socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 */
1993
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001994SYSCALL_DEFINE4(send, int, fd, void __user *, buff, size_t, len,
Eric Dumazet95c96172012-04-15 05:58:06 +00001995 unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996{
Dominik Brodowski211b6342018-03-13 19:18:52 +01001997 return __sys_sendto(fd, buff, len, flags, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998}
1999
2000/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002001 * Receive a frame from the socket and optionally record the address of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 * sender. We verify the buffers are writable and if needed move the
2003 * sender address from kernel to user space.
2004 */
Dominik Brodowski7a09e1e2018-03-13 19:10:06 +01002005int __sys_recvfrom(int fd, void __user *ubuf, size_t size, unsigned int flags,
2006 struct sockaddr __user *addr, int __user *addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007{
2008 struct socket *sock;
2009 struct iovec iov;
2010 struct msghdr msg;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07002011 struct sockaddr_storage address;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002012 int err, err2;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002013 int fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
Al Viro602bd0e2015-03-21 19:12:32 -04002015 err = import_single_range(READ, ubuf, size, &iov, &msg.msg_iter);
2016 if (unlikely(err))
2017 return err;
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08002018 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 if (!sock)
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08002020 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002022 msg.msg_control = NULL;
2023 msg.msg_controllen = 0;
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01002024 /* Save some cycles and don't copy the address if not needed */
2025 msg.msg_name = addr ? (struct sockaddr *)&address : NULL;
2026 /* We assume all kernel code knows the size of sockaddr_storage */
2027 msg.msg_namelen = 0;
tadeusz.struk@intel.com130ed5d2015-12-15 10:46:17 -08002028 msg.msg_iocb = NULL;
Alexander Potapenko9f138fa2017-03-08 18:08:16 +01002029 msg.msg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 if (sock->file->f_flags & O_NONBLOCK)
2031 flags |= MSG_DONTWAIT;
Al Viro2da62902015-03-14 21:13:46 -04002032 err = sock_recvmsg(sock, &msg, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002034 if (err >= 0 && addr != NULL) {
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00002035 err2 = move_addr_to_user(&address,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07002036 msg.msg_namelen, addr, addr_len);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002037 if (err2 < 0)
2038 err = err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 }
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08002040
2041 fput_light(sock->file, fput_needed);
David S. Miller4387ff72007-02-08 15:06:08 -08002042out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 return err;
2044}
2045
Dominik Brodowski7a09e1e2018-03-13 19:10:06 +01002046SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, ubuf, size_t, size,
2047 unsigned int, flags, struct sockaddr __user *, addr,
2048 int __user *, addr_len)
2049{
2050 return __sys_recvfrom(fd, ubuf, size, flags, addr, addr_len);
2051}
2052
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002054 * Receive a datagram from a socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 */
2056
Jan Glauberb7c0ddf2014-04-16 09:32:48 +02002057SYSCALL_DEFINE4(recv, int, fd, void __user *, ubuf, size_t, size,
2058 unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059{
Dominik Brodowski7a09e1e2018-03-13 19:10:06 +01002060 return __sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061}
2062
Florian Westphal83f0c102020-07-05 01:30:15 +02002063static bool sock_use_custom_sol_socket(const struct socket *sock)
2064{
2065 const struct sock *sk = sock->sk;
2066
2067 /* Use sock->ops->setsockopt() for MPTCP */
2068 return IS_ENABLED(CONFIG_MPTCP) &&
2069 sk->sk_protocol == IPPROTO_MPTCP &&
2070 sk->sk_type == SOCK_STREAM &&
2071 (sk->sk_family == AF_INET || sk->sk_family == AF_INET6);
2072}
2073
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074/*
2075 * Set a socket option. Because we don't know the option lengths we have
2076 * to pass the user mode parameter for the protocols to sort out.
2077 */
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02002078int __sys_setsockopt(int fd, int level, int optname, char __user *user_optval,
Christoph Hellwig55db9c02020-07-17 08:23:15 +02002079 int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080{
Christoph Hellwig519a8a62020-08-10 18:42:14 +02002081 sockptr_t optval = USER_SOCKPTR(user_optval);
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07002082 char *kernel_optval = NULL;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002083 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 struct socket *sock;
2085
2086 if (optlen < 0)
2087 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002088
2089 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Christoph Hellwig4a367292020-07-17 08:23:11 +02002090 if (!sock)
2091 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
Christoph Hellwig4a367292020-07-17 08:23:11 +02002093 err = security_socket_setsockopt(sock, level, optname);
2094 if (err)
2095 goto out_put;
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07002096
Christoph Hellwig55db9c02020-07-17 08:23:15 +02002097 if (!in_compat_syscall())
2098 err = BPF_CGROUP_RUN_PROG_SETSOCKOPT(sock->sk, &level, &optname,
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02002099 user_optval, &optlen,
Christoph Hellwig55db9c02020-07-17 08:23:15 +02002100 &kernel_optval);
Christoph Hellwig4a367292020-07-17 08:23:11 +02002101 if (err < 0)
2102 goto out_put;
2103 if (err > 0) {
2104 err = 0;
2105 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 }
Christoph Hellwig4a367292020-07-17 08:23:11 +02002107
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02002108 if (kernel_optval)
2109 optval = KERNEL_SOCKPTR(kernel_optval);
Christoph Hellwig4a367292020-07-17 08:23:11 +02002110 if (level == SOL_SOCKET && !sock_use_custom_sol_socket(sock))
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02002111 err = sock_setsockopt(sock, level, optname, optval, optlen);
Christoph Hellwiga44d9e72020-07-17 08:23:31 +02002112 else if (unlikely(!sock->ops->setsockopt))
2113 err = -EOPNOTSUPP;
Christoph Hellwig4a367292020-07-17 08:23:11 +02002114 else
2115 err = sock->ops->setsockopt(sock, level, optname, optval,
2116 optlen);
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02002117 kfree(kernel_optval);
Christoph Hellwig4a367292020-07-17 08:23:11 +02002118out_put:
2119 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 return err;
2121}
2122
Dominik Brodowskicc36dca2018-03-13 20:10:59 +01002123SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname,
2124 char __user *, optval, int, optlen)
2125{
2126 return __sys_setsockopt(fd, level, optname, optval, optlen);
2127}
2128
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129/*
2130 * Get a socket option. Because we don't know the option lengths we have
2131 * to pass a user mode parameter for the protocols to sort out.
2132 */
Christoph Hellwig55db9c02020-07-17 08:23:15 +02002133int __sys_getsockopt(int fd, int level, int optname, char __user *optval,
2134 int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002136 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 struct socket *sock;
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07002138 int max_optlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002140 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Christoph Hellwigd8a9b382020-07-17 08:23:12 +02002141 if (!sock)
2142 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143
Christoph Hellwigd8a9b382020-07-17 08:23:12 +02002144 err = security_socket_getsockopt(sock, level, optname);
2145 if (err)
2146 goto out_put;
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07002147
Christoph Hellwig55db9c02020-07-17 08:23:15 +02002148 if (!in_compat_syscall())
2149 max_optlen = BPF_CGROUP_GETSOCKOPT_MAX_OPTLEN(optlen);
Christoph Hellwigd8a9b382020-07-17 08:23:12 +02002150
2151 if (level == SOL_SOCKET)
2152 err = sock_getsockopt(sock, level, optname, optval, optlen);
Christoph Hellwiga44d9e72020-07-17 08:23:31 +02002153 else if (unlikely(!sock->ops->getsockopt))
2154 err = -EOPNOTSUPP;
Christoph Hellwigd8a9b382020-07-17 08:23:12 +02002155 else
2156 err = sock->ops->getsockopt(sock, level, optname, optval,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002157 optlen);
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07002158
Christoph Hellwig55db9c02020-07-17 08:23:15 +02002159 if (!in_compat_syscall())
2160 err = BPF_CGROUP_RUN_PROG_GETSOCKOPT(sock->sk, level, optname,
2161 optval, optlen, max_optlen,
2162 err);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002163out_put:
Christoph Hellwigd8a9b382020-07-17 08:23:12 +02002164 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 return err;
2166}
2167
Dominik Brodowski13a2d702018-03-13 20:15:04 +01002168SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname,
2169 char __user *, optval, int __user *, optlen)
2170{
2171 return __sys_getsockopt(fd, level, optname, optval, optlen);
2172}
2173
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174/*
2175 * Shutdown a socket.
2176 */
2177
Jens Axboeb713c192020-09-05 11:13:35 -06002178int __sys_shutdown_sock(struct socket *sock, int how)
2179{
2180 int err;
2181
2182 err = security_socket_shutdown(sock, how);
2183 if (!err)
2184 err = sock->ops->shutdown(sock, how);
2185
2186 return err;
2187}
2188
Dominik Brodowski005a1ae2018-03-13 20:07:05 +01002189int __sys_shutdown(int fd, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002191 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 struct socket *sock;
2193
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002194 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2195 if (sock != NULL) {
Jens Axboeb713c192020-09-05 11:13:35 -06002196 err = __sys_shutdown_sock(sock, how);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002197 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 }
2199 return err;
2200}
2201
Dominik Brodowski005a1ae2018-03-13 20:07:05 +01002202SYSCALL_DEFINE2(shutdown, int, fd, int, how)
2203{
2204 return __sys_shutdown(fd, how);
2205}
2206
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002207/* A couple of helpful macros for getting the address of the 32/64 bit
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 * fields which are the same type (int / unsigned) on our platforms.
2209 */
2210#define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
2211#define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
2212#define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
2213
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002214struct used_address {
2215 struct sockaddr_storage name;
2216 unsigned int name_len;
2217};
2218
Jens Axboe0a384ab2020-02-27 08:11:20 -07002219int __copy_msghdr_from_user(struct msghdr *kmsg,
2220 struct user_msghdr __user *umsg,
2221 struct sockaddr __user **save_addr,
2222 struct iovec __user **uiov, size_t *nsegs)
Dan Carpenter1661bf32013-10-03 00:27:20 +03002223{
Al Viroffb07552017-06-27 19:32:04 -04002224 struct user_msghdr msg;
Al Viro08adb7d2014-11-10 20:23:13 -05002225 ssize_t err;
2226
Al Viroffb07552017-06-27 19:32:04 -04002227 if (copy_from_user(&msg, umsg, sizeof(*umsg)))
Dan Carpenter1661bf32013-10-03 00:27:20 +03002228 return -EFAULT;
Matthew Leachdbb490b2014-03-11 11:58:27 +00002229
Christoph Hellwig1f466e12020-05-11 13:59:13 +02002230 kmsg->msg_control_is_user = true;
2231 kmsg->msg_control_user = msg.msg_control;
Al Viroffb07552017-06-27 19:32:04 -04002232 kmsg->msg_controllen = msg.msg_controllen;
2233 kmsg->msg_flags = msg.msg_flags;
2234
2235 kmsg->msg_namelen = msg.msg_namelen;
2236 if (!msg.msg_name)
Ani Sinha6a2a2b32014-09-08 14:49:59 -07002237 kmsg->msg_namelen = 0;
2238
Matthew Leachdbb490b2014-03-11 11:58:27 +00002239 if (kmsg->msg_namelen < 0)
2240 return -EINVAL;
2241
Dan Carpenter1661bf32013-10-03 00:27:20 +03002242 if (kmsg->msg_namelen > sizeof(struct sockaddr_storage))
Dan Carpenterdb31c552013-11-27 15:40:21 +03002243 kmsg->msg_namelen = sizeof(struct sockaddr_storage);
Al Viro08adb7d2014-11-10 20:23:13 -05002244
2245 if (save_addr)
Al Viroffb07552017-06-27 19:32:04 -04002246 *save_addr = msg.msg_name;
Al Viro08adb7d2014-11-10 20:23:13 -05002247
Al Viroffb07552017-06-27 19:32:04 -04002248 if (msg.msg_name && kmsg->msg_namelen) {
Al Viro08adb7d2014-11-10 20:23:13 -05002249 if (!save_addr) {
Paolo Abeni864d9662017-07-21 18:49:45 +02002250 err = move_addr_to_kernel(msg.msg_name,
2251 kmsg->msg_namelen,
Al Viro08adb7d2014-11-10 20:23:13 -05002252 kmsg->msg_name);
2253 if (err < 0)
2254 return err;
2255 }
2256 } else {
2257 kmsg->msg_name = NULL;
2258 kmsg->msg_namelen = 0;
2259 }
2260
Al Viroffb07552017-06-27 19:32:04 -04002261 if (msg.msg_iovlen > UIO_MAXIOV)
Al Viro08adb7d2014-11-10 20:23:13 -05002262 return -EMSGSIZE;
2263
tadeusz.struk@intel.com0345f932015-03-19 12:31:25 -07002264 kmsg->msg_iocb = NULL;
Jens Axboe0a384ab2020-02-27 08:11:20 -07002265 *uiov = msg.msg_iov;
2266 *nsegs = msg.msg_iovlen;
2267 return 0;
2268}
2269
2270static int copy_msghdr_from_user(struct msghdr *kmsg,
2271 struct user_msghdr __user *umsg,
2272 struct sockaddr __user **save_addr,
2273 struct iovec **iov)
2274{
2275 struct user_msghdr msg;
2276 ssize_t err;
2277
2278 err = __copy_msghdr_from_user(kmsg, umsg, save_addr, &msg.msg_iov,
2279 &msg.msg_iovlen);
2280 if (err)
2281 return err;
tadeusz.struk@intel.com0345f932015-03-19 12:31:25 -07002282
Jens Axboe87e5e6d2019-05-14 16:02:22 -06002283 err = import_iovec(save_addr ? READ : WRITE,
Al Viroffb07552017-06-27 19:32:04 -04002284 msg.msg_iov, msg.msg_iovlen,
Al Viroda184282015-03-21 19:29:06 -04002285 UIO_FASTIOV, iov, &kmsg->msg_iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06002286 return err < 0 ? err : 0;
Dan Carpenter1661bf32013-10-03 00:27:20 +03002287}
2288
Jens Axboe4257c8c2019-11-25 14:27:34 -07002289static int ____sys_sendmsg(struct socket *sock, struct msghdr *msg_sys,
2290 unsigned int flags, struct used_address *used_address,
2291 unsigned int allowed_msghdr_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292{
Alex Williamsonb9d717a2005-09-26 14:28:02 -07002293 unsigned char ctl[sizeof(struct cmsghdr) + 20]
Amit Kushwaha846cc122016-12-08 18:21:53 +05302294 __aligned(sizeof(__kernel_size_t));
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002295 /* 20 is size of ipv6_pktinfo */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 unsigned char *ctl_buf = ctl;
Al Virod8725c82014-12-11 00:02:50 -05002297 int ctl_len;
Al Viro08adb7d2014-11-10 20:23:13 -05002298 ssize_t err;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002299
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 err = -ENOBUFS;
2301
Anton Blanchard228e5482011-05-02 20:21:35 +00002302 if (msg_sys->msg_controllen > INT_MAX)
Jens Axboe4257c8c2019-11-25 14:27:34 -07002303 goto out;
Tom Herbert28a94d82016-03-07 14:11:02 -08002304 flags |= (msg_sys->msg_flags & allowed_msghdr_flags);
Anton Blanchard228e5482011-05-02 20:21:35 +00002305 ctl_len = msg_sys->msg_controllen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002307 err =
Anton Blanchard228e5482011-05-02 20:21:35 +00002308 cmsghdr_from_user_compat_to_kern(msg_sys, sock->sk, ctl,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002309 sizeof(ctl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 if (err)
Jens Axboe4257c8c2019-11-25 14:27:34 -07002311 goto out;
Anton Blanchard228e5482011-05-02 20:21:35 +00002312 ctl_buf = msg_sys->msg_control;
2313 ctl_len = msg_sys->msg_controllen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 } else if (ctl_len) {
David S. Millerac4340f2017-01-04 13:24:19 -05002315 BUILD_BUG_ON(sizeof(struct cmsghdr) !=
2316 CMSG_ALIGN(sizeof(struct cmsghdr)));
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002317 if (ctl_len > sizeof(ctl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002319 if (ctl_buf == NULL)
Jens Axboe4257c8c2019-11-25 14:27:34 -07002320 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 }
2322 err = -EFAULT;
Christoph Hellwig1f466e12020-05-11 13:59:13 +02002323 if (copy_from_user(ctl_buf, msg_sys->msg_control_user, ctl_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 goto out_freectl;
Anton Blanchard228e5482011-05-02 20:21:35 +00002325 msg_sys->msg_control = ctl_buf;
Christoph Hellwig1f466e12020-05-11 13:59:13 +02002326 msg_sys->msg_control_is_user = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 }
Anton Blanchard228e5482011-05-02 20:21:35 +00002328 msg_sys->msg_flags = flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
2330 if (sock->file->f_flags & O_NONBLOCK)
Anton Blanchard228e5482011-05-02 20:21:35 +00002331 msg_sys->msg_flags |= MSG_DONTWAIT;
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002332 /*
2333 * If this is sendmmsg() and current destination address is same as
2334 * previously succeeded address, omit asking LSM's decision.
2335 * used_address->name_len is initialized to UINT_MAX so that the first
2336 * destination address never matches.
2337 */
Mathieu Desnoyersbc909d92011-08-24 19:45:03 -07002338 if (used_address && msg_sys->msg_name &&
2339 used_address->name_len == msg_sys->msg_namelen &&
2340 !memcmp(&used_address->name, msg_sys->msg_name,
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002341 used_address->name_len)) {
Al Virod8725c82014-12-11 00:02:50 -05002342 err = sock_sendmsg_nosec(sock, msg_sys);
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002343 goto out_freectl;
2344 }
Al Virod8725c82014-12-11 00:02:50 -05002345 err = sock_sendmsg(sock, msg_sys);
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002346 /*
2347 * If this is sendmmsg() and sending to current destination address was
2348 * successful, remember it.
2349 */
2350 if (used_address && err >= 0) {
2351 used_address->name_len = msg_sys->msg_namelen;
Mathieu Desnoyersbc909d92011-08-24 19:45:03 -07002352 if (msg_sys->msg_name)
2353 memcpy(&used_address->name, msg_sys->msg_name,
2354 used_address->name_len);
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356
2357out_freectl:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002358 if (ctl_buf != ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 sock_kfree_s(sock->sk, ctl_buf, ctl_len);
Jens Axboe4257c8c2019-11-25 14:27:34 -07002360out:
2361 return err;
2362}
2363
Jens Axboe03b12302019-12-02 18:50:25 -07002364int sendmsg_copy_msghdr(struct msghdr *msg,
2365 struct user_msghdr __user *umsg, unsigned flags,
2366 struct iovec **iov)
Jens Axboe4257c8c2019-11-25 14:27:34 -07002367{
2368 int err;
2369
2370 if (flags & MSG_CMSG_COMPAT) {
2371 struct compat_msghdr __user *msg_compat;
2372
2373 msg_compat = (struct compat_msghdr __user *) umsg;
2374 err = get_compat_msghdr(msg, msg_compat, NULL, iov);
2375 } else {
2376 err = copy_msghdr_from_user(msg, umsg, NULL, iov);
2377 }
2378 if (err < 0)
2379 return err;
2380
2381 return 0;
2382}
2383
2384static int ___sys_sendmsg(struct socket *sock, struct user_msghdr __user *msg,
2385 struct msghdr *msg_sys, unsigned int flags,
2386 struct used_address *used_address,
2387 unsigned int allowed_msghdr_flags)
2388{
2389 struct sockaddr_storage address;
2390 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
2391 ssize_t err;
2392
2393 msg_sys->msg_name = &address;
2394
2395 err = sendmsg_copy_msghdr(msg_sys, msg, flags, &iov);
2396 if (err < 0)
2397 return err;
2398
2399 err = ____sys_sendmsg(sock, msg_sys, flags, used_address,
2400 allowed_msghdr_flags);
Al Viroda184282015-03-21 19:29:06 -04002401 kfree(iov);
Anton Blanchard228e5482011-05-02 20:21:35 +00002402 return err;
2403}
2404
2405/*
2406 * BSD sendmsg interface
2407 */
Jens Axboe03b12302019-12-02 18:50:25 -07002408long __sys_sendmsg_sock(struct socket *sock, struct msghdr *msg,
Jens Axboe0fa03c62019-04-19 13:34:07 -06002409 unsigned int flags)
2410{
Jens Axboed69e0772019-11-25 17:04:13 -07002411 /* disallow ancillary data requests from this path */
Jens Axboe03b12302019-12-02 18:50:25 -07002412 if (msg->msg_control || msg->msg_controllen)
2413 return -EINVAL;
Jens Axboed69e0772019-11-25 17:04:13 -07002414
Jens Axboe03b12302019-12-02 18:50:25 -07002415 return ____sys_sendmsg(sock, msg, flags, NULL, 0);
Jens Axboe0fa03c62019-04-19 13:34:07 -06002416}
Anton Blanchard228e5482011-05-02 20:21:35 +00002417
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002418long __sys_sendmsg(int fd, struct user_msghdr __user *msg, unsigned int flags,
2419 bool forbid_cmsg_compat)
Anton Blanchard228e5482011-05-02 20:21:35 +00002420{
2421 int fput_needed, err;
2422 struct msghdr msg_sys;
Andy Lutomirski1be374a2013-05-22 14:07:44 -07002423 struct socket *sock;
Anton Blanchard228e5482011-05-02 20:21:35 +00002424
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002425 if (forbid_cmsg_compat && (flags & MSG_CMSG_COMPAT))
2426 return -EINVAL;
2427
Andy Lutomirski1be374a2013-05-22 14:07:44 -07002428 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Anton Blanchard228e5482011-05-02 20:21:35 +00002429 if (!sock)
2430 goto out;
2431
Tom Herbert28a94d82016-03-07 14:11:02 -08002432 err = ___sys_sendmsg(sock, msg, &msg_sys, flags, NULL, 0);
Anton Blanchard228e5482011-05-02 20:21:35 +00002433
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002434 fput_light(sock->file, fput_needed);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002435out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 return err;
2437}
2438
Al Viro666547f2014-04-06 14:03:05 -04002439SYSCALL_DEFINE3(sendmsg, int, fd, struct user_msghdr __user *, msg, unsigned int, flags)
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002440{
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002441 return __sys_sendmsg(fd, msg, flags, true);
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002442}
2443
Anton Blanchard228e5482011-05-02 20:21:35 +00002444/*
2445 * Linux sendmmsg interface
2446 */
2447
2448int __sys_sendmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002449 unsigned int flags, bool forbid_cmsg_compat)
Anton Blanchard228e5482011-05-02 20:21:35 +00002450{
2451 int fput_needed, err, datagrams;
2452 struct socket *sock;
2453 struct mmsghdr __user *entry;
2454 struct compat_mmsghdr __user *compat_entry;
2455 struct msghdr msg_sys;
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002456 struct used_address used_address;
Tom Herbertf0922762016-03-07 14:11:03 -08002457 unsigned int oflags = flags;
Anton Blanchard228e5482011-05-02 20:21:35 +00002458
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002459 if (forbid_cmsg_compat && (flags & MSG_CMSG_COMPAT))
2460 return -EINVAL;
2461
Anton Blanchard98382f42011-08-04 14:07:39 +00002462 if (vlen > UIO_MAXIOV)
2463 vlen = UIO_MAXIOV;
Anton Blanchard228e5482011-05-02 20:21:35 +00002464
2465 datagrams = 0;
2466
2467 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2468 if (!sock)
2469 return err;
2470
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002471 used_address.name_len = UINT_MAX;
Anton Blanchard228e5482011-05-02 20:21:35 +00002472 entry = mmsg;
2473 compat_entry = (struct compat_mmsghdr __user *)mmsg;
Anton Blanchard728ffb82011-08-04 14:07:38 +00002474 err = 0;
Tom Herbertf0922762016-03-07 14:11:03 -08002475 flags |= MSG_BATCH;
Anton Blanchard228e5482011-05-02 20:21:35 +00002476
2477 while (datagrams < vlen) {
Tom Herbertf0922762016-03-07 14:11:03 -08002478 if (datagrams == vlen - 1)
2479 flags = oflags;
2480
Anton Blanchard228e5482011-05-02 20:21:35 +00002481 if (MSG_CMSG_COMPAT & flags) {
Al Viro666547f2014-04-06 14:03:05 -04002482 err = ___sys_sendmsg(sock, (struct user_msghdr __user *)compat_entry,
Tom Herbert28a94d82016-03-07 14:11:02 -08002483 &msg_sys, flags, &used_address, MSG_EOR);
Anton Blanchard228e5482011-05-02 20:21:35 +00002484 if (err < 0)
2485 break;
2486 err = __put_user(err, &compat_entry->msg_len);
2487 ++compat_entry;
2488 } else {
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002489 err = ___sys_sendmsg(sock,
Al Viro666547f2014-04-06 14:03:05 -04002490 (struct user_msghdr __user *)entry,
Tom Herbert28a94d82016-03-07 14:11:02 -08002491 &msg_sys, flags, &used_address, MSG_EOR);
Anton Blanchard228e5482011-05-02 20:21:35 +00002492 if (err < 0)
2493 break;
2494 err = put_user(err, &entry->msg_len);
2495 ++entry;
2496 }
2497
2498 if (err)
2499 break;
2500 ++datagrams;
Soheil Hassas Yeganeh30238982016-11-04 15:36:49 -04002501 if (msg_data_left(&msg_sys))
2502 break;
Eric Dumazeta78cb842016-01-08 08:37:20 -08002503 cond_resched();
Anton Blanchard228e5482011-05-02 20:21:35 +00002504 }
2505
Anton Blanchard228e5482011-05-02 20:21:35 +00002506 fput_light(sock->file, fput_needed);
2507
Anton Blanchard728ffb82011-08-04 14:07:38 +00002508 /* We only return an error if no datagrams were able to be sent */
2509 if (datagrams != 0)
Anton Blanchard228e5482011-05-02 20:21:35 +00002510 return datagrams;
2511
Anton Blanchard228e5482011-05-02 20:21:35 +00002512 return err;
2513}
2514
2515SYSCALL_DEFINE4(sendmmsg, int, fd, struct mmsghdr __user *, mmsg,
2516 unsigned int, vlen, unsigned int, flags)
2517{
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002518 return __sys_sendmmsg(fd, mmsg, vlen, flags, true);
Anton Blanchard228e5482011-05-02 20:21:35 +00002519}
2520
Jens Axboe03b12302019-12-02 18:50:25 -07002521int recvmsg_copy_msghdr(struct msghdr *msg,
2522 struct user_msghdr __user *umsg, unsigned flags,
2523 struct sockaddr __user **uaddr,
2524 struct iovec **iov)
Jens Axboe4257c8c2019-11-25 14:27:34 -07002525{
2526 ssize_t err;
2527
2528 if (MSG_CMSG_COMPAT & flags) {
2529 struct compat_msghdr __user *msg_compat;
2530
2531 msg_compat = (struct compat_msghdr __user *) umsg;
2532 err = get_compat_msghdr(msg, msg_compat, uaddr, iov);
2533 } else {
2534 err = copy_msghdr_from_user(msg, umsg, uaddr, iov);
2535 }
2536 if (err < 0)
2537 return err;
2538
2539 return 0;
2540}
2541
2542static int ____sys_recvmsg(struct socket *sock, struct msghdr *msg_sys,
2543 struct user_msghdr __user *msg,
2544 struct sockaddr __user *uaddr,
2545 unsigned int flags, int nosec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546{
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002547 struct compat_msghdr __user *msg_compat =
Jens Axboe4257c8c2019-11-25 14:27:34 -07002548 (struct compat_msghdr __user *) msg;
2549 int __user *uaddr_len = COMPAT_NAMELEN(msg);
2550 struct sockaddr_storage addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 unsigned long cmsg_ptr;
Al Viro2da62902015-03-14 21:13:46 -04002552 int len;
Al Viro08adb7d2014-11-10 20:23:13 -05002553 ssize_t err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554
Al Viro08adb7d2014-11-10 20:23:13 -05002555 msg_sys->msg_name = &addr;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002556 cmsg_ptr = (unsigned long)msg_sys->msg_control;
2557 msg_sys->msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002558
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01002559 /* We assume all kernel code knows the size of sockaddr_storage */
2560 msg_sys->msg_namelen = 0;
2561
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 if (sock->file->f_flags & O_NONBLOCK)
2563 flags |= MSG_DONTWAIT;
Eric Dumazet1af66222019-12-06 09:38:36 -08002564
2565 if (unlikely(nosec))
2566 err = sock_recvmsg_nosec(sock, msg_sys, flags);
2567 else
2568 err = sock_recvmsg(sock, msg_sys, flags);
2569
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 if (err < 0)
Jens Axboe4257c8c2019-11-25 14:27:34 -07002571 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 len = err;
2573
2574 if (uaddr != NULL) {
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00002575 err = move_addr_to_user(&addr,
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002576 msg_sys->msg_namelen, uaddr,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002577 uaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 if (err < 0)
Jens Axboe4257c8c2019-11-25 14:27:34 -07002579 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 }
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002581 err = __put_user((msg_sys->msg_flags & ~MSG_CMSG_COMPAT),
David S. Miller37f7f422005-09-16 16:51:01 -07002582 COMPAT_FLAGS(msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 if (err)
Jens Axboe4257c8c2019-11-25 14:27:34 -07002584 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 if (MSG_CMSG_COMPAT & flags)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002586 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 &msg_compat->msg_controllen);
2588 else
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002589 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 &msg->msg_controllen);
2591 if (err)
Jens Axboe4257c8c2019-11-25 14:27:34 -07002592 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 err = len;
Jens Axboe4257c8c2019-11-25 14:27:34 -07002594out:
2595 return err;
2596}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597
Jens Axboe4257c8c2019-11-25 14:27:34 -07002598static int ___sys_recvmsg(struct socket *sock, struct user_msghdr __user *msg,
2599 struct msghdr *msg_sys, unsigned int flags, int nosec)
2600{
2601 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
2602 /* user mode address pointers */
2603 struct sockaddr __user *uaddr;
2604 ssize_t err;
2605
2606 err = recvmsg_copy_msghdr(msg_sys, msg, flags, &uaddr, &iov);
2607 if (err < 0)
2608 return err;
2609
2610 err = ____sys_recvmsg(sock, msg_sys, msg, uaddr, flags, nosec);
Al Viroda184282015-03-21 19:29:06 -04002611 kfree(iov);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002612 return err;
2613}
2614
2615/*
2616 * BSD recvmsg interface
2617 */
2618
Jens Axboe03b12302019-12-02 18:50:25 -07002619long __sys_recvmsg_sock(struct socket *sock, struct msghdr *msg,
2620 struct user_msghdr __user *umsg,
2621 struct sockaddr __user *uaddr, unsigned int flags)
Jens Axboeaa1fa282019-04-19 13:38:09 -06002622{
Luke Hsiao583bbf02020-08-21 21:41:04 -07002623 if (msg->msg_control || msg->msg_controllen) {
2624 /* disallow ancillary data reqs unless cmsg is plain data */
2625 if (!(sock->ops->flags & PROTO_CMSG_DATA_ONLY))
2626 return -EINVAL;
2627 }
Jens Axboed69e0772019-11-25 17:04:13 -07002628
Jens Axboe03b12302019-12-02 18:50:25 -07002629 return ____sys_recvmsg(sock, msg, umsg, uaddr, flags, 0);
Jens Axboeaa1fa282019-04-19 13:38:09 -06002630}
2631
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002632long __sys_recvmsg(int fd, struct user_msghdr __user *msg, unsigned int flags,
2633 bool forbid_cmsg_compat)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002634{
2635 int fput_needed, err;
2636 struct msghdr msg_sys;
Andy Lutomirski1be374a2013-05-22 14:07:44 -07002637 struct socket *sock;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002638
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002639 if (forbid_cmsg_compat && (flags & MSG_CMSG_COMPAT))
2640 return -EINVAL;
2641
Andy Lutomirski1be374a2013-05-22 14:07:44 -07002642 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002643 if (!sock)
2644 goto out;
2645
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002646 err = ___sys_recvmsg(sock, msg, &msg_sys, flags, 0);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002647
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002648 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649out:
2650 return err;
2651}
2652
Al Viro666547f2014-04-06 14:03:05 -04002653SYSCALL_DEFINE3(recvmsg, int, fd, struct user_msghdr __user *, msg,
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002654 unsigned int, flags)
2655{
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002656 return __sys_recvmsg(fd, msg, flags, true);
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002657}
2658
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002659/*
2660 * Linux recvmmsg interface
2661 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662
Arnd Bergmanne11d4282018-04-18 13:43:52 +02002663static int do_recvmmsg(int fd, struct mmsghdr __user *mmsg,
2664 unsigned int vlen, unsigned int flags,
2665 struct timespec64 *timeout)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002666{
2667 int fput_needed, err, datagrams;
2668 struct socket *sock;
2669 struct mmsghdr __user *entry;
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002670 struct compat_mmsghdr __user *compat_entry;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002671 struct msghdr msg_sys;
Deepa Dinamani766b9f92016-05-19 17:09:05 -07002672 struct timespec64 end_time;
2673 struct timespec64 timeout64;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002674
2675 if (timeout &&
2676 poll_select_set_timeout(&end_time, timeout->tv_sec,
2677 timeout->tv_nsec))
2678 return -EINVAL;
2679
2680 datagrams = 0;
2681
2682 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2683 if (!sock)
2684 return err;
2685
Soheil Hassas Yeganeh7797dc42018-02-27 18:22:40 -05002686 if (likely(!(flags & MSG_ERRQUEUE))) {
2687 err = sock_error(sock->sk);
2688 if (err) {
2689 datagrams = err;
2690 goto out_put;
2691 }
Maxime Jayate623a9e2017-02-21 18:35:51 +01002692 }
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002693
2694 entry = mmsg;
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002695 compat_entry = (struct compat_mmsghdr __user *)mmsg;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002696
2697 while (datagrams < vlen) {
2698 /*
2699 * No need to ask LSM for more than the first datagram.
2700 */
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002701 if (MSG_CMSG_COMPAT & flags) {
Al Viro666547f2014-04-06 14:03:05 -04002702 err = ___sys_recvmsg(sock, (struct user_msghdr __user *)compat_entry,
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002703 &msg_sys, flags & ~MSG_WAITFORONE,
2704 datagrams);
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002705 if (err < 0)
2706 break;
2707 err = __put_user(err, &compat_entry->msg_len);
2708 ++compat_entry;
2709 } else {
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002710 err = ___sys_recvmsg(sock,
Al Viro666547f2014-04-06 14:03:05 -04002711 (struct user_msghdr __user *)entry,
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002712 &msg_sys, flags & ~MSG_WAITFORONE,
2713 datagrams);
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002714 if (err < 0)
2715 break;
2716 err = put_user(err, &entry->msg_len);
2717 ++entry;
2718 }
2719
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002720 if (err)
2721 break;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002722 ++datagrams;
2723
Brandon L Black71c5c1592010-03-26 16:18:03 +00002724 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
2725 if (flags & MSG_WAITFORONE)
2726 flags |= MSG_DONTWAIT;
2727
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002728 if (timeout) {
Deepa Dinamani766b9f92016-05-19 17:09:05 -07002729 ktime_get_ts64(&timeout64);
Arnd Bergmannc2e6c852018-04-18 13:42:25 +02002730 *timeout = timespec64_sub(end_time, timeout64);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002731 if (timeout->tv_sec < 0) {
2732 timeout->tv_sec = timeout->tv_nsec = 0;
2733 break;
2734 }
2735
2736 /* Timeout, return less than vlen datagrams */
2737 if (timeout->tv_nsec == 0 && timeout->tv_sec == 0)
2738 break;
2739 }
2740
2741 /* Out of band data, return right away */
2742 if (msg_sys.msg_flags & MSG_OOB)
2743 break;
Eric Dumazeta78cb842016-01-08 08:37:20 -08002744 cond_resched();
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002745 }
2746
Arnaldo Carvalho de Melo34b88a62016-03-14 09:56:35 -03002747 if (err == 0)
2748 goto out_put;
2749
2750 if (datagrams == 0) {
2751 datagrams = err;
2752 goto out_put;
2753 }
2754
2755 /*
2756 * We may return less entries than requested (vlen) if the
2757 * sock is non block and there aren't enough datagrams...
2758 */
2759 if (err != -EAGAIN) {
2760 /*
2761 * ... or if recvmsg returns an error after we
2762 * received some datagrams, where we record the
2763 * error to return on the next call or if the
2764 * app asks about it using getsockopt(SO_ERROR).
2765 */
2766 sock->sk->sk_err = -err;
2767 }
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002768out_put:
2769 fput_light(sock->file, fput_needed);
2770
Arnaldo Carvalho de Melo34b88a62016-03-14 09:56:35 -03002771 return datagrams;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002772}
2773
Arnd Bergmanne11d4282018-04-18 13:43:52 +02002774int __sys_recvmmsg(int fd, struct mmsghdr __user *mmsg,
2775 unsigned int vlen, unsigned int flags,
2776 struct __kernel_timespec __user *timeout,
2777 struct old_timespec32 __user *timeout32)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002778{
2779 int datagrams;
Arnd Bergmannc2e6c852018-04-18 13:42:25 +02002780 struct timespec64 timeout_sys;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002781
Arnd Bergmanne11d4282018-04-18 13:43:52 +02002782 if (timeout && get_timespec64(&timeout_sys, timeout))
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002783 return -EFAULT;
2784
Arnd Bergmanne11d4282018-04-18 13:43:52 +02002785 if (timeout32 && get_old_timespec32(&timeout_sys, timeout32))
2786 return -EFAULT;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002787
Arnd Bergmanne11d4282018-04-18 13:43:52 +02002788 if (!timeout && !timeout32)
2789 return do_recvmmsg(fd, mmsg, vlen, flags, NULL);
2790
2791 datagrams = do_recvmmsg(fd, mmsg, vlen, flags, &timeout_sys);
2792
2793 if (datagrams <= 0)
2794 return datagrams;
2795
2796 if (timeout && put_timespec64(&timeout_sys, timeout))
2797 datagrams = -EFAULT;
2798
2799 if (timeout32 && put_old_timespec32(&timeout_sys, timeout32))
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002800 datagrams = -EFAULT;
2801
2802 return datagrams;
2803}
2804
Dominik Brodowski1255e262018-03-13 20:44:21 +01002805SYSCALL_DEFINE5(recvmmsg, int, fd, struct mmsghdr __user *, mmsg,
2806 unsigned int, vlen, unsigned int, flags,
Arnd Bergmannc2e6c852018-04-18 13:42:25 +02002807 struct __kernel_timespec __user *, timeout)
Dominik Brodowski1255e262018-03-13 20:44:21 +01002808{
Arnd Bergmanne11d4282018-04-18 13:43:52 +02002809 if (flags & MSG_CMSG_COMPAT)
2810 return -EINVAL;
2811
2812 return __sys_recvmmsg(fd, mmsg, vlen, flags, timeout, NULL);
Dominik Brodowski1255e262018-03-13 20:44:21 +01002813}
2814
Arnd Bergmanne11d4282018-04-18 13:43:52 +02002815#ifdef CONFIG_COMPAT_32BIT_TIME
2816SYSCALL_DEFINE5(recvmmsg_time32, int, fd, struct mmsghdr __user *, mmsg,
2817 unsigned int, vlen, unsigned int, flags,
2818 struct old_timespec32 __user *, timeout)
2819{
2820 if (flags & MSG_CMSG_COMPAT)
2821 return -EINVAL;
2822
2823 return __sys_recvmmsg(fd, mmsg, vlen, flags, NULL, timeout);
2824}
2825#endif
2826
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002827#ifdef __ARCH_WANT_SYS_SOCKETCALL
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828/* Argument list sizes for sys_socketcall */
2829#define AL(x) ((x) * sizeof(unsigned long))
Anton Blanchard228e5482011-05-02 20:21:35 +00002830static const unsigned char nargs[21] = {
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002831 AL(0), AL(3), AL(3), AL(3), AL(2), AL(3),
2832 AL(3), AL(3), AL(4), AL(4), AL(4), AL(6),
2833 AL(6), AL(2), AL(5), AL(5), AL(3), AL(3),
Anton Blanchard228e5482011-05-02 20:21:35 +00002834 AL(4), AL(5), AL(4)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002835};
2836
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837#undef AL
2838
2839/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002840 * System call vectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 *
2842 * Argument checking cleaned up. Saved 20% in size.
2843 * This function doesn't need to set the kernel lock because
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002844 * it is set by the callees.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 */
2846
Heiko Carstens3e0fa652009-01-14 14:14:24 +01002847SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848{
Chen Gang2950fa92013-04-07 16:55:23 +08002849 unsigned long a[AUDITSC_ARGS];
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002850 unsigned long a0, a1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 int err;
Arjan van de Ven47379052009-09-28 12:57:44 -07002852 unsigned int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853
Anton Blanchard228e5482011-05-02 20:21:35 +00002854 if (call < 1 || call > SYS_SENDMMSG)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 return -EINVAL;
Jeremy Clinec8e8cd52018-07-27 22:43:01 +00002856 call = array_index_nospec(call, SYS_SENDMMSG + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857
Arjan van de Ven47379052009-09-28 12:57:44 -07002858 len = nargs[call];
2859 if (len > sizeof(a))
2860 return -EINVAL;
2861
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 /* copy_from_user should be SMP safe. */
Arjan van de Ven47379052009-09-28 12:57:44 -07002863 if (copy_from_user(a, args, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 return -EFAULT;
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002865
Chen Gang2950fa92013-04-07 16:55:23 +08002866 err = audit_socketcall(nargs[call] / sizeof(unsigned long), a);
2867 if (err)
2868 return err;
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002869
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002870 a0 = a[0];
2871 a1 = a[1];
2872
2873 switch (call) {
2874 case SYS_SOCKET:
Dominik Brodowski9d6a15c2018-03-13 19:29:43 +01002875 err = __sys_socket(a0, a1, a[2]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002876 break;
2877 case SYS_BIND:
Dominik Brodowskia87d35d2018-03-13 19:33:09 +01002878 err = __sys_bind(a0, (struct sockaddr __user *)a1, a[2]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002879 break;
2880 case SYS_CONNECT:
Dominik Brodowski1387c2c2018-03-13 19:35:09 +01002881 err = __sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002882 break;
2883 case SYS_LISTEN:
Dominik Brodowski25e290e2018-03-13 19:36:54 +01002884 err = __sys_listen(a0, a1);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002885 break;
2886 case SYS_ACCEPT:
Dominik Brodowski4541e802018-03-13 19:24:23 +01002887 err = __sys_accept4(a0, (struct sockaddr __user *)a1,
2888 (int __user *)a[2], 0);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002889 break;
2890 case SYS_GETSOCKNAME:
2891 err =
Dominik Brodowski8882a102018-03-13 19:43:14 +01002892 __sys_getsockname(a0, (struct sockaddr __user *)a1,
2893 (int __user *)a[2]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002894 break;
2895 case SYS_GETPEERNAME:
2896 err =
Dominik Brodowskib21c8f82018-03-13 19:47:00 +01002897 __sys_getpeername(a0, (struct sockaddr __user *)a1,
2898 (int __user *)a[2]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002899 break;
2900 case SYS_SOCKETPAIR:
Dominik Brodowski6debc8d2018-03-13 19:49:23 +01002901 err = __sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002902 break;
2903 case SYS_SEND:
Dominik Brodowskif3bf8962018-03-13 19:52:00 +01002904 err = __sys_sendto(a0, (void __user *)a1, a[2], a[3],
2905 NULL, 0);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002906 break;
2907 case SYS_SENDTO:
Dominik Brodowski211b6342018-03-13 19:18:52 +01002908 err = __sys_sendto(a0, (void __user *)a1, a[2], a[3],
2909 (struct sockaddr __user *)a[4], a[5]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002910 break;
2911 case SYS_RECV:
Dominik Brodowskid27e9af2018-03-13 19:54:17 +01002912 err = __sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
2913 NULL, NULL);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002914 break;
2915 case SYS_RECVFROM:
Dominik Brodowski7a09e1e2018-03-13 19:10:06 +01002916 err = __sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
2917 (struct sockaddr __user *)a[4],
2918 (int __user *)a[5]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002919 break;
2920 case SYS_SHUTDOWN:
Dominik Brodowski005a1ae2018-03-13 20:07:05 +01002921 err = __sys_shutdown(a0, a1);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002922 break;
2923 case SYS_SETSOCKOPT:
Dominik Brodowskicc36dca2018-03-13 20:10:59 +01002924 err = __sys_setsockopt(a0, a1, a[2], (char __user *)a[3],
2925 a[4]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002926 break;
2927 case SYS_GETSOCKOPT:
2928 err =
Dominik Brodowski13a2d702018-03-13 20:15:04 +01002929 __sys_getsockopt(a0, a1, a[2], (char __user *)a[3],
2930 (int __user *)a[4]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002931 break;
2932 case SYS_SENDMSG:
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002933 err = __sys_sendmsg(a0, (struct user_msghdr __user *)a1,
2934 a[2], true);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002935 break;
Anton Blanchard228e5482011-05-02 20:21:35 +00002936 case SYS_SENDMMSG:
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002937 err = __sys_sendmmsg(a0, (struct mmsghdr __user *)a1, a[2],
2938 a[3], true);
Anton Blanchard228e5482011-05-02 20:21:35 +00002939 break;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002940 case SYS_RECVMSG:
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002941 err = __sys_recvmsg(a0, (struct user_msghdr __user *)a1,
2942 a[2], true);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002943 break;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002944 case SYS_RECVMMSG:
Arnd Bergmann3ca47e92019-04-23 17:43:50 +02002945 if (IS_ENABLED(CONFIG_64BIT))
Arnd Bergmanne11d4282018-04-18 13:43:52 +02002946 err = __sys_recvmmsg(a0, (struct mmsghdr __user *)a1,
2947 a[2], a[3],
2948 (struct __kernel_timespec __user *)a[4],
2949 NULL);
2950 else
2951 err = __sys_recvmmsg(a0, (struct mmsghdr __user *)a1,
2952 a[2], a[3], NULL,
2953 (struct old_timespec32 __user *)a[4]);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002954 break;
Ulrich Drepperde11def2008-11-19 15:36:14 -08002955 case SYS_ACCEPT4:
Dominik Brodowski4541e802018-03-13 19:24:23 +01002956 err = __sys_accept4(a0, (struct sockaddr __user *)a1,
2957 (int __user *)a[2], a[3]);
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07002958 break;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002959 default:
2960 err = -EINVAL;
2961 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 }
2963 return err;
2964}
2965
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002966#endif /* __ARCH_WANT_SYS_SOCKETCALL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002968/**
2969 * sock_register - add a socket protocol handler
2970 * @ops: description of protocol
2971 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 * This function is called by a protocol handler that wants to
2973 * advertise its address family, and have it linked into the
Masanari Iidae793c0f2014-09-04 23:44:36 +09002974 * socket interface. The value ops->family corresponds to the
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002975 * socket system call protocol family.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 */
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002977int sock_register(const struct net_proto_family *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978{
2979 int err;
2980
2981 if (ops->family >= NPROTO) {
Yang Yingliang3410f222014-02-12 17:09:55 +08002982 pr_crit("protocol %d >= NPROTO(%d)\n", ops->family, NPROTO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 return -ENOBUFS;
2984 }
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002985
2986 spin_lock(&net_family_lock);
Eric Dumazet190683a2010-11-10 10:50:44 +00002987 if (rcu_dereference_protected(net_families[ops->family],
2988 lockdep_is_held(&net_family_lock)))
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002989 err = -EEXIST;
2990 else {
Eric Dumazetcf778b02012-01-12 04:41:32 +00002991 rcu_assign_pointer(net_families[ops->family], ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 err = 0;
2993 }
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002994 spin_unlock(&net_family_lock);
2995
Yang Yingliang3410f222014-02-12 17:09:55 +08002996 pr_info("NET: Registered protocol family %d\n", ops->family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 return err;
2998}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002999EXPORT_SYMBOL(sock_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000
Stephen Hemminger55737fd2006-09-01 00:23:39 -07003001/**
3002 * sock_unregister - remove a protocol handler
3003 * @family: protocol family to remove
3004 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 * This function is called by a protocol handler that wants to
3006 * remove its address family, and have it unlinked from the
Stephen Hemminger55737fd2006-09-01 00:23:39 -07003007 * new socket creation.
3008 *
3009 * If protocol handler is a module, then it can use module reference
3010 * counts to protect against new references. If protocol handler is not
3011 * a module then it needs to provide its own protection in
3012 * the ops->create routine.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 */
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07003014void sock_unregister(int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015{
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07003016 BUG_ON(family < 0 || family >= NPROTO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017
Stephen Hemminger55737fd2006-09-01 00:23:39 -07003018 spin_lock(&net_family_lock);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00003019 RCU_INIT_POINTER(net_families[family], NULL);
Stephen Hemminger55737fd2006-09-01 00:23:39 -07003020 spin_unlock(&net_family_lock);
3021
3022 synchronize_rcu();
3023
Yang Yingliang3410f222014-02-12 17:09:55 +08003024 pr_info("NET: Unregistered protocol family %d\n", family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003026EXPORT_SYMBOL(sock_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027
Xin Longbf2ae2e2018-03-10 18:57:50 +08003028bool sock_is_registered(int family)
3029{
Jeremy Cline66b51b02018-08-13 22:23:13 +00003030 return family < NPROTO && rcu_access_pointer(net_families[family]);
Xin Longbf2ae2e2018-03-10 18:57:50 +08003031}
3032
Andi Kleen77d76ea2005-12-22 12:43:42 -08003033static int __init sock_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034{
Nick Pigginb3e19d92011-01-07 17:50:11 +11003035 int err;
Eric W. Biederman2ca794e2012-04-19 13:20:32 +00003036 /*
3037 * Initialize the network sysctl infrastructure.
3038 */
3039 err = net_sysctl_init();
3040 if (err)
3041 goto out;
Nick Pigginb3e19d92011-01-07 17:50:11 +11003042
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07003044 * Initialize skbuff SLAB cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 */
3046 skb_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047
3048 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07003049 * Initialize the protocols module.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 */
3051
3052 init_inodecache();
Nick Pigginb3e19d92011-01-07 17:50:11 +11003053
3054 err = register_filesystem(&sock_fs_type);
3055 if (err)
Miaohe Lin47260ba2020-08-06 19:54:19 +08003056 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 sock_mnt = kern_mount(&sock_fs_type);
Nick Pigginb3e19d92011-01-07 17:50:11 +11003058 if (IS_ERR(sock_mnt)) {
3059 err = PTR_ERR(sock_mnt);
3060 goto out_mount;
3061 }
Andi Kleen77d76ea2005-12-22 12:43:42 -08003062
3063 /* The real protocol initialization is performed in later initcalls.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 */
3065
3066#ifdef CONFIG_NETFILTER
Pablo Neira Ayuso6d11cfd2013-05-22 22:42:36 +00003067 err = netfilter_init();
3068 if (err)
3069 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070#endif
David S. Millercbeb3212005-12-22 12:58:55 -08003071
Daniel Borkmann408eccc2014-04-01 16:20:23 +02003072 ptp_classifier_init();
Richard Cochranc1f19b52010-07-17 08:49:36 +00003073
Nick Pigginb3e19d92011-01-07 17:50:11 +11003074out:
3075 return err;
3076
3077out_mount:
3078 unregister_filesystem(&sock_fs_type);
Nick Pigginb3e19d92011-01-07 17:50:11 +11003079 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080}
3081
Andi Kleen77d76ea2005-12-22 12:43:42 -08003082core_initcall(sock_init); /* early initcall */
3083
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084#ifdef CONFIG_PROC_FS
3085void socket_seq_show(struct seq_file *seq)
3086{
Tonghao Zhang648845a2017-12-14 05:51:58 -08003087 seq_printf(seq, "sockets: used %d\n",
3088 sock_inuse_get(seq->private));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089}
Stephen Hemminger89bddce2006-09-01 00:19:31 -07003090#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091
Shaun Pereira89bbfc92006-03-21 23:58:08 -08003092#ifdef CONFIG_COMPAT
Al Viro36fd6332017-06-26 13:19:16 -04003093static int compat_dev_ifconf(struct net *net, struct compat_ifconf __user *uifc32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08003094{
Arnd Bergmann6b960182009-11-06 23:10:54 -08003095 struct compat_ifconf ifc32;
Arnd Bergmann7a229382009-11-06 23:00:29 -08003096 struct ifconf ifc;
Arnd Bergmann7a229382009-11-06 23:00:29 -08003097 int err;
3098
Arnd Bergmann6b960182009-11-06 23:10:54 -08003099 if (copy_from_user(&ifc32, uifc32, sizeof(struct compat_ifconf)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08003100 return -EFAULT;
3101
Al Viro36fd6332017-06-26 13:19:16 -04003102 ifc.ifc_len = ifc32.ifc_len;
3103 ifc.ifc_req = compat_ptr(ifc32.ifcbuf);
Arnd Bergmann7a229382009-11-06 23:00:29 -08003104
Al Viro36fd6332017-06-26 13:19:16 -04003105 rtnl_lock();
3106 err = dev_ifconf(net, &ifc, sizeof(struct compat_ifreq));
3107 rtnl_unlock();
Arnd Bergmann7a229382009-11-06 23:00:29 -08003108 if (err)
3109 return err;
3110
Al Viro36fd6332017-06-26 13:19:16 -04003111 ifc32.ifc_len = ifc.ifc_len;
Arnd Bergmann6b960182009-11-06 23:10:54 -08003112 if (copy_to_user(uifc32, &ifc32, sizeof(struct compat_ifconf)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08003113 return -EFAULT;
3114
3115 return 0;
3116}
3117
Arnd Bergmann6b960182009-11-06 23:10:54 -08003118static int ethtool_ioctl(struct net *net, struct compat_ifreq __user *ifr32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08003119{
Ben Hutchings3a7da392011-03-17 07:34:32 +00003120 struct compat_ethtool_rxnfc __user *compat_rxnfc;
3121 bool convert_in = false, convert_out = false;
Al Viro44c02a22017-10-05 12:59:44 -04003122 size_t buf_size = 0;
3123 struct ethtool_rxnfc __user *rxnfc = NULL;
3124 struct ifreq ifr;
Ben Hutchings3a7da392011-03-17 07:34:32 +00003125 u32 rule_cnt = 0, actual_rule_cnt;
3126 u32 ethcmd;
Arnd Bergmann7a229382009-11-06 23:00:29 -08003127 u32 data;
Ben Hutchings3a7da392011-03-17 07:34:32 +00003128 int ret;
Arnd Bergmann7a229382009-11-06 23:00:29 -08003129
3130 if (get_user(data, &ifr32->ifr_ifru.ifru_data))
3131 return -EFAULT;
3132
Ben Hutchings3a7da392011-03-17 07:34:32 +00003133 compat_rxnfc = compat_ptr(data);
3134
3135 if (get_user(ethcmd, &compat_rxnfc->cmd))
Arnd Bergmann7a229382009-11-06 23:00:29 -08003136 return -EFAULT;
3137
Ben Hutchings3a7da392011-03-17 07:34:32 +00003138 /* Most ethtool structures are defined without padding.
3139 * Unfortunately struct ethtool_rxnfc is an exception.
3140 */
3141 switch (ethcmd) {
3142 default:
3143 break;
3144 case ETHTOOL_GRXCLSRLALL:
3145 /* Buffer size is variable */
3146 if (get_user(rule_cnt, &compat_rxnfc->rule_cnt))
3147 return -EFAULT;
3148 if (rule_cnt > KMALLOC_MAX_SIZE / sizeof(u32))
3149 return -ENOMEM;
3150 buf_size += rule_cnt * sizeof(u32);
Miaohe Lin7c7ab582020-08-08 16:23:10 +08003151 fallthrough;
Ben Hutchings3a7da392011-03-17 07:34:32 +00003152 case ETHTOOL_GRXRINGS:
3153 case ETHTOOL_GRXCLSRLCNT:
3154 case ETHTOOL_GRXCLSRULE:
Ben Hutchings55664f32012-01-03 12:04:51 +00003155 case ETHTOOL_SRXCLSRLINS:
Ben Hutchings3a7da392011-03-17 07:34:32 +00003156 convert_out = true;
Miaohe Lin7c7ab582020-08-08 16:23:10 +08003157 fallthrough;
Ben Hutchings3a7da392011-03-17 07:34:32 +00003158 case ETHTOOL_SRXCLSRLDEL:
Ben Hutchings3a7da392011-03-17 07:34:32 +00003159 buf_size += sizeof(struct ethtool_rxnfc);
3160 convert_in = true;
Al Viro44c02a22017-10-05 12:59:44 -04003161 rxnfc = compat_alloc_user_space(buf_size);
Ben Hutchings3a7da392011-03-17 07:34:32 +00003162 break;
3163 }
3164
Al Viro44c02a22017-10-05 12:59:44 -04003165 if (copy_from_user(&ifr.ifr_name, &ifr32->ifr_name, IFNAMSIZ))
Ben Hutchings3a7da392011-03-17 07:34:32 +00003166 return -EFAULT;
3167
Al Viro44c02a22017-10-05 12:59:44 -04003168 ifr.ifr_data = convert_in ? rxnfc : (void __user *)compat_rxnfc;
Ben Hutchings3a7da392011-03-17 07:34:32 +00003169
3170 if (convert_in) {
Alexander Duyck127fe532011-04-08 18:01:59 +00003171 /* We expect there to be holes between fs.m_ext and
Ben Hutchings3a7da392011-03-17 07:34:32 +00003172 * fs.ring_cookie and at the end of fs, but nowhere else.
3173 */
Alexander Duyck127fe532011-04-08 18:01:59 +00003174 BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc, fs.m_ext) +
3175 sizeof(compat_rxnfc->fs.m_ext) !=
3176 offsetof(struct ethtool_rxnfc, fs.m_ext) +
3177 sizeof(rxnfc->fs.m_ext));
Ben Hutchings3a7da392011-03-17 07:34:32 +00003178 BUILD_BUG_ON(
3179 offsetof(struct compat_ethtool_rxnfc, fs.location) -
3180 offsetof(struct compat_ethtool_rxnfc, fs.ring_cookie) !=
3181 offsetof(struct ethtool_rxnfc, fs.location) -
3182 offsetof(struct ethtool_rxnfc, fs.ring_cookie));
3183
3184 if (copy_in_user(rxnfc, compat_rxnfc,
Stephen Hemminger954b1242013-02-11 06:22:28 +00003185 (void __user *)(&rxnfc->fs.m_ext + 1) -
3186 (void __user *)rxnfc) ||
Ben Hutchings3a7da392011-03-17 07:34:32 +00003187 copy_in_user(&rxnfc->fs.ring_cookie,
3188 &compat_rxnfc->fs.ring_cookie,
Stephen Hemminger954b1242013-02-11 06:22:28 +00003189 (void __user *)(&rxnfc->fs.location + 1) -
Wenwen Wangb6168562018-10-18 09:36:46 -05003190 (void __user *)&rxnfc->fs.ring_cookie))
3191 return -EFAULT;
3192 if (ethcmd == ETHTOOL_GRXCLSRLALL) {
3193 if (put_user(rule_cnt, &rxnfc->rule_cnt))
3194 return -EFAULT;
3195 } else if (copy_in_user(&rxnfc->rule_cnt,
3196 &compat_rxnfc->rule_cnt,
3197 sizeof(rxnfc->rule_cnt)))
Ben Hutchings3a7da392011-03-17 07:34:32 +00003198 return -EFAULT;
3199 }
3200
Al Viro44c02a22017-10-05 12:59:44 -04003201 ret = dev_ioctl(net, SIOCETHTOOL, &ifr, NULL);
Ben Hutchings3a7da392011-03-17 07:34:32 +00003202 if (ret)
3203 return ret;
3204
3205 if (convert_out) {
3206 if (copy_in_user(compat_rxnfc, rxnfc,
Stephen Hemminger954b1242013-02-11 06:22:28 +00003207 (const void __user *)(&rxnfc->fs.m_ext + 1) -
3208 (const void __user *)rxnfc) ||
Ben Hutchings3a7da392011-03-17 07:34:32 +00003209 copy_in_user(&compat_rxnfc->fs.ring_cookie,
3210 &rxnfc->fs.ring_cookie,
Stephen Hemminger954b1242013-02-11 06:22:28 +00003211 (const void __user *)(&rxnfc->fs.location + 1) -
3212 (const void __user *)&rxnfc->fs.ring_cookie) ||
Ben Hutchings3a7da392011-03-17 07:34:32 +00003213 copy_in_user(&compat_rxnfc->rule_cnt, &rxnfc->rule_cnt,
3214 sizeof(rxnfc->rule_cnt)))
3215 return -EFAULT;
3216
3217 if (ethcmd == ETHTOOL_GRXCLSRLALL) {
3218 /* As an optimisation, we only copy the actual
3219 * number of rules that the underlying
3220 * function returned. Since Mallory might
3221 * change the rule count in user memory, we
3222 * check that it is less than the rule count
3223 * originally given (as the user buffer size),
3224 * which has been range-checked.
3225 */
3226 if (get_user(actual_rule_cnt, &rxnfc->rule_cnt))
3227 return -EFAULT;
3228 if (actual_rule_cnt < rule_cnt)
3229 rule_cnt = actual_rule_cnt;
3230 if (copy_in_user(&compat_rxnfc->rule_locs[0],
3231 &rxnfc->rule_locs[0],
3232 rule_cnt * sizeof(u32)))
3233 return -EFAULT;
3234 }
3235 }
3236
3237 return 0;
Arnd Bergmann7a229382009-11-06 23:00:29 -08003238}
3239
Arnd Bergmann7a50a242009-11-08 20:57:03 -08003240static int compat_siocwandev(struct net *net, struct compat_ifreq __user *uifr32)
3241{
Arnd Bergmann7a50a242009-11-08 20:57:03 -08003242 compat_uptr_t uptr32;
Al Viro44c02a22017-10-05 12:59:44 -04003243 struct ifreq ifr;
3244 void __user *saved;
3245 int err;
Arnd Bergmann7a50a242009-11-08 20:57:03 -08003246
Al Viro44c02a22017-10-05 12:59:44 -04003247 if (copy_from_user(&ifr, uifr32, sizeof(struct compat_ifreq)))
Arnd Bergmann7a50a242009-11-08 20:57:03 -08003248 return -EFAULT;
3249
3250 if (get_user(uptr32, &uifr32->ifr_settings.ifs_ifsu))
3251 return -EFAULT;
3252
Al Viro44c02a22017-10-05 12:59:44 -04003253 saved = ifr.ifr_settings.ifs_ifsu.raw_hdlc;
3254 ifr.ifr_settings.ifs_ifsu.raw_hdlc = compat_ptr(uptr32);
Arnd Bergmann7a50a242009-11-08 20:57:03 -08003255
Al Viro44c02a22017-10-05 12:59:44 -04003256 err = dev_ioctl(net, SIOCWANDEV, &ifr, NULL);
3257 if (!err) {
3258 ifr.ifr_settings.ifs_ifsu.raw_hdlc = saved;
3259 if (copy_to_user(uifr32, &ifr, sizeof(struct compat_ifreq)))
3260 err = -EFAULT;
Joe Perchesccbd6a52010-05-14 10:58:26 +00003261 }
Al Viro44c02a22017-10-05 12:59:44 -04003262 return err;
Arnd Bergmann7a229382009-11-06 23:00:29 -08003263}
3264
Ben Hutchings590d4692013-11-18 17:04:13 +00003265/* Handle ioctls that use ifreq::ifr_data and just need struct ifreq converted */
3266static int compat_ifr_data_ioctl(struct net *net, unsigned int cmd,
Arnd Bergmann6b960182009-11-06 23:10:54 -08003267 struct compat_ifreq __user *u_ifreq32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08003268{
Al Viro44c02a22017-10-05 12:59:44 -04003269 struct ifreq ifreq;
Arnd Bergmann7a229382009-11-06 23:00:29 -08003270 u32 data32;
3271
Al Viro44c02a22017-10-05 12:59:44 -04003272 if (copy_from_user(ifreq.ifr_name, u_ifreq32->ifr_name, IFNAMSIZ))
Arnd Bergmann7a229382009-11-06 23:00:29 -08003273 return -EFAULT;
Al Viro44c02a22017-10-05 12:59:44 -04003274 if (get_user(data32, &u_ifreq32->ifr_data))
Arnd Bergmann7a229382009-11-06 23:00:29 -08003275 return -EFAULT;
Al Viro44c02a22017-10-05 12:59:44 -04003276 ifreq.ifr_data = compat_ptr(data32);
Arnd Bergmann7a229382009-11-06 23:00:29 -08003277
Al Viro44c02a22017-10-05 12:59:44 -04003278 return dev_ioctl(net, cmd, &ifreq, NULL);
Arnd Bergmann7a229382009-11-06 23:00:29 -08003279}
3280
Johannes Berg37ac39b2019-01-25 22:43:18 +01003281static int compat_ifreq_ioctl(struct net *net, struct socket *sock,
3282 unsigned int cmd,
3283 struct compat_ifreq __user *uifr32)
3284{
3285 struct ifreq __user *uifr;
3286 int err;
3287
3288 /* Handle the fact that while struct ifreq has the same *layout* on
3289 * 32/64 for everything but ifreq::ifru_ifmap and ifreq::ifru_data,
3290 * which are handled elsewhere, it still has different *size* due to
3291 * ifreq::ifru_ifmap (which is 16 bytes on 32 bit, 24 bytes on 64-bit,
3292 * resulting in struct ifreq being 32 and 40 bytes respectively).
3293 * As a result, if the struct happens to be at the end of a page and
3294 * the next page isn't readable/writable, we get a fault. To prevent
3295 * that, copy back and forth to the full size.
3296 */
3297
3298 uifr = compat_alloc_user_space(sizeof(*uifr));
3299 if (copy_in_user(uifr, uifr32, sizeof(*uifr32)))
3300 return -EFAULT;
3301
3302 err = sock_do_ioctl(net, sock, cmd, (unsigned long)uifr);
3303
3304 if (!err) {
3305 switch (cmd) {
3306 case SIOCGIFFLAGS:
3307 case SIOCGIFMETRIC:
3308 case SIOCGIFMTU:
3309 case SIOCGIFMEM:
3310 case SIOCGIFHWADDR:
3311 case SIOCGIFINDEX:
3312 case SIOCGIFADDR:
3313 case SIOCGIFBRDADDR:
3314 case SIOCGIFDSTADDR:
3315 case SIOCGIFNETMASK:
3316 case SIOCGIFPFLAGS:
3317 case SIOCGIFTXQLEN:
3318 case SIOCGMIIPHY:
3319 case SIOCGMIIREG:
Johannes Bergc6c9fee2019-01-25 22:43:19 +01003320 case SIOCGIFNAME:
Johannes Berg37ac39b2019-01-25 22:43:18 +01003321 if (copy_in_user(uifr32, uifr, sizeof(*uifr32)))
3322 err = -EFAULT;
3323 break;
3324 }
3325 }
3326 return err;
3327}
3328
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003329static int compat_sioc_ifmap(struct net *net, unsigned int cmd,
3330 struct compat_ifreq __user *uifr32)
3331{
3332 struct ifreq ifr;
3333 struct compat_ifmap __user *uifmap32;
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003334 int err;
3335
3336 uifmap32 = &uifr32->ifr_ifru.ifru_map;
3337 err = copy_from_user(&ifr, uifr32, sizeof(ifr.ifr_name));
Mathieu Desnoyers3ddc5b42013-09-11 14:23:18 -07003338 err |= get_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
3339 err |= get_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
3340 err |= get_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
3341 err |= get_user(ifr.ifr_map.irq, &uifmap32->irq);
3342 err |= get_user(ifr.ifr_map.dma, &uifmap32->dma);
3343 err |= get_user(ifr.ifr_map.port, &uifmap32->port);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003344 if (err)
3345 return -EFAULT;
3346
Al Viro44c02a22017-10-05 12:59:44 -04003347 err = dev_ioctl(net, cmd, &ifr, NULL);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003348
3349 if (cmd == SIOCGIFMAP && !err) {
3350 err = copy_to_user(uifr32, &ifr, sizeof(ifr.ifr_name));
Mathieu Desnoyers3ddc5b42013-09-11 14:23:18 -07003351 err |= put_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
3352 err |= put_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
3353 err |= put_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
3354 err |= put_user(ifr.ifr_map.irq, &uifmap32->irq);
3355 err |= put_user(ifr.ifr_map.dma, &uifmap32->dma);
3356 err |= put_user(ifr.ifr_map.port, &uifmap32->port);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003357 if (err)
3358 err = -EFAULT;
3359 }
3360 return err;
3361}
3362
Arnd Bergmann7a229382009-11-06 23:00:29 -08003363/* Since old style bridge ioctl's endup using SIOCDEVPRIVATE
3364 * for some operations; this forces use of the newer bridge-utils that
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003365 * use compatible ioctls
Arnd Bergmann7a229382009-11-06 23:00:29 -08003366 */
Arnd Bergmann6b960182009-11-06 23:10:54 -08003367static int old_bridge_ioctl(compat_ulong_t __user *argp)
Arnd Bergmann7a229382009-11-06 23:00:29 -08003368{
Arnd Bergmann6b960182009-11-06 23:10:54 -08003369 compat_ulong_t tmp;
Arnd Bergmann7a229382009-11-06 23:00:29 -08003370
Arnd Bergmann6b960182009-11-06 23:10:54 -08003371 if (get_user(tmp, argp))
Arnd Bergmann7a229382009-11-06 23:00:29 -08003372 return -EFAULT;
3373 if (tmp == BRCTL_GET_VERSION)
3374 return BRCTL_VERSION + 1;
3375 return -EINVAL;
3376}
3377
Arnd Bergmann6b960182009-11-06 23:10:54 -08003378static int compat_sock_ioctl_trans(struct file *file, struct socket *sock,
3379 unsigned int cmd, unsigned long arg)
3380{
3381 void __user *argp = compat_ptr(arg);
3382 struct sock *sk = sock->sk;
3383 struct net *net = sock_net(sk);
Arnd Bergmann7a229382009-11-06 23:00:29 -08003384
Arnd Bergmann6b960182009-11-06 23:10:54 -08003385 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15))
Ben Hutchings590d4692013-11-18 17:04:13 +00003386 return compat_ifr_data_ioctl(net, cmd, argp);
Arnd Bergmann7a229382009-11-06 23:00:29 -08003387
Arnd Bergmann6b960182009-11-06 23:10:54 -08003388 switch (cmd) {
3389 case SIOCSIFBR:
3390 case SIOCGIFBR:
3391 return old_bridge_ioctl(argp);
Arnd Bergmann6b960182009-11-06 23:10:54 -08003392 case SIOCGIFCONF:
Al Viro36fd6332017-06-26 13:19:16 -04003393 return compat_dev_ifconf(net, argp);
Arnd Bergmann6b960182009-11-06 23:10:54 -08003394 case SIOCETHTOOL:
3395 return ethtool_ioctl(net, argp);
Arnd Bergmann7a50a242009-11-08 20:57:03 -08003396 case SIOCWANDEV:
3397 return compat_siocwandev(net, argp);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003398 case SIOCGIFMAP:
3399 case SIOCSIFMAP:
3400 return compat_sioc_ifmap(net, cmd, argp);
Arnd Bergmann0768e172019-04-17 22:56:11 +02003401 case SIOCGSTAMP_OLD:
3402 case SIOCGSTAMPNS_OLD:
Arnd Bergmannc7cbdbf2019-04-17 22:51:48 +02003403 if (!sock->ops->gettstamp)
3404 return -ENOIOCTLCMD;
Arnd Bergmann0768e172019-04-17 22:56:11 +02003405 return sock->ops->gettstamp(sock, argp, cmd == SIOCGSTAMP_OLD,
Arnd Bergmannc7cbdbf2019-04-17 22:51:48 +02003406 !COMPAT_USE_64BIT_TIME);
3407
Ben Hutchings590d4692013-11-18 17:04:13 +00003408 case SIOCBONDSLAVEINFOQUERY:
3409 case SIOCBONDINFOQUERY:
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003410 case SIOCSHWTSTAMP:
Ben Hutchingsfd468c72013-11-14 01:19:29 +00003411 case SIOCGHWTSTAMP:
Ben Hutchings590d4692013-11-18 17:04:13 +00003412 return compat_ifr_data_ioctl(net, cmd, argp);
Arnd Bergmann7a229382009-11-06 23:00:29 -08003413
Arnd Bergmann6b960182009-11-06 23:10:54 -08003414 case FIOSETOWN:
3415 case SIOCSPGRP:
3416 case FIOGETOWN:
3417 case SIOCGPGRP:
3418 case SIOCBRADDBR:
3419 case SIOCBRDELBR:
3420 case SIOCGIFVLAN:
3421 case SIOCSIFVLAN:
Andrey Vaginc62cce22016-10-24 18:29:13 -07003422 case SIOCGSKNS:
Arnd Bergmann0768e172019-04-17 22:56:11 +02003423 case SIOCGSTAMP_NEW:
3424 case SIOCGSTAMPNS_NEW:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003425 return sock_ioctl(file, cmd, arg);
3426
3427 case SIOCGIFFLAGS:
3428 case SIOCSIFFLAGS:
3429 case SIOCGIFMETRIC:
3430 case SIOCSIFMETRIC:
3431 case SIOCGIFMTU:
3432 case SIOCSIFMTU:
3433 case SIOCGIFMEM:
3434 case SIOCSIFMEM:
3435 case SIOCGIFHWADDR:
3436 case SIOCSIFHWADDR:
3437 case SIOCADDMULTI:
3438 case SIOCDELMULTI:
3439 case SIOCGIFINDEX:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003440 case SIOCGIFADDR:
3441 case SIOCSIFADDR:
3442 case SIOCSIFHWBROADCAST:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003443 case SIOCDIFADDR:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003444 case SIOCGIFBRDADDR:
3445 case SIOCSIFBRDADDR:
3446 case SIOCGIFDSTADDR:
3447 case SIOCSIFDSTADDR:
3448 case SIOCGIFNETMASK:
3449 case SIOCSIFNETMASK:
3450 case SIOCSIFPFLAGS:
3451 case SIOCGIFPFLAGS:
3452 case SIOCGIFTXQLEN:
3453 case SIOCSIFTXQLEN:
3454 case SIOCBRADDIF:
3455 case SIOCBRDELIF:
Johannes Bergc6c9fee2019-01-25 22:43:19 +01003456 case SIOCGIFNAME:
Arnd Bergmann9177efd2009-11-06 08:09:09 +00003457 case SIOCSIFNAME:
3458 case SIOCGMIIPHY:
3459 case SIOCGMIIREG:
3460 case SIOCSMIIREG:
Al Virof92d4fc2017-09-30 19:32:17 -04003461 case SIOCBONDENSLAVE:
3462 case SIOCBONDRELEASE:
3463 case SIOCBONDSETHWADDR:
3464 case SIOCBONDCHANGEACTIVE:
Johannes Berg37ac39b2019-01-25 22:43:18 +01003465 return compat_ifreq_ioctl(net, sock, cmd, argp);
3466
Arnd Bergmann6b960182009-11-06 23:10:54 -08003467 case SIOCSARP:
3468 case SIOCGARP:
3469 case SIOCDARP:
Arnd Bergmannc7dc5042019-06-03 23:07:12 +02003470 case SIOCOUTQ:
Arnd Bergmann9d7bf412019-06-03 23:06:00 +02003471 case SIOCOUTQNSD:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003472 case SIOCATMARK:
Johannes Berg63ff03a2019-01-25 22:43:17 +01003473 return sock_do_ioctl(net, sock, cmd, arg);
Arnd Bergmann9177efd2009-11-06 08:09:09 +00003474 }
3475
Arnd Bergmann6b960182009-11-06 23:10:54 -08003476 return -ENOIOCTLCMD;
3477}
Arnd Bergmann7a229382009-11-06 23:00:29 -08003478
Eric Dumazet95c96172012-04-15 05:58:06 +00003479static long compat_sock_ioctl(struct file *file, unsigned int cmd,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07003480 unsigned long arg)
Shaun Pereira89bbfc92006-03-21 23:58:08 -08003481{
3482 struct socket *sock = file->private_data;
3483 int ret = -ENOIOCTLCMD;
David S. Miller87de87d2008-06-03 09:14:03 -07003484 struct sock *sk;
3485 struct net *net;
3486
3487 sk = sock->sk;
3488 net = sock_net(sk);
Shaun Pereira89bbfc92006-03-21 23:58:08 -08003489
3490 if (sock->ops->compat_ioctl)
3491 ret = sock->ops->compat_ioctl(sock, cmd, arg);
3492
David S. Miller87de87d2008-06-03 09:14:03 -07003493 if (ret == -ENOIOCTLCMD &&
3494 (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST))
3495 ret = compat_wext_handle_ioctl(net, cmd, arg);
3496
Arnd Bergmann6b960182009-11-06 23:10:54 -08003497 if (ret == -ENOIOCTLCMD)
3498 ret = compat_sock_ioctl_trans(file, sock, cmd, arg);
3499
Shaun Pereira89bbfc92006-03-21 23:58:08 -08003500 return ret;
3501}
3502#endif
3503
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003504/**
3505 * kernel_bind - bind an address to a socket (kernel space)
3506 * @sock: socket
3507 * @addr: address
3508 * @addrlen: length of address
3509 *
3510 * Returns 0 or an error.
3511 */
3512
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003513int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen)
3514{
3515 return sock->ops->bind(sock, addr, addrlen);
3516}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003517EXPORT_SYMBOL(kernel_bind);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003518
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003519/**
3520 * kernel_listen - move socket to listening state (kernel space)
3521 * @sock: socket
3522 * @backlog: pending connections queue size
3523 *
3524 * Returns 0 or an error.
3525 */
3526
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003527int kernel_listen(struct socket *sock, int backlog)
3528{
3529 return sock->ops->listen(sock, backlog);
3530}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003531EXPORT_SYMBOL(kernel_listen);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003532
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003533/**
3534 * kernel_accept - accept a connection (kernel space)
3535 * @sock: listening socket
3536 * @newsock: new connected socket
3537 * @flags: flags
3538 *
3539 * @flags must be SOCK_CLOEXEC, SOCK_NONBLOCK or 0.
3540 * If it fails, @newsock is guaranteed to be %NULL.
3541 * Returns 0 or an error.
3542 */
3543
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003544int kernel_accept(struct socket *sock, struct socket **newsock, int flags)
3545{
3546 struct sock *sk = sock->sk;
3547 int err;
3548
3549 err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
3550 newsock);
3551 if (err < 0)
3552 goto done;
3553
David Howellscdfbabf2017-03-09 08:09:05 +00003554 err = sock->ops->accept(sock, *newsock, flags, true);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003555 if (err < 0) {
3556 sock_release(*newsock);
Tony Battersbyfa8705b2007-10-10 21:09:04 -07003557 *newsock = NULL;
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003558 goto done;
3559 }
3560
3561 (*newsock)->ops = sock->ops;
Wei Yongjun1b085342008-12-18 19:35:10 -08003562 __module_get((*newsock)->ops->owner);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003563
3564done:
3565 return err;
3566}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003567EXPORT_SYMBOL(kernel_accept);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003568
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003569/**
3570 * kernel_connect - connect a socket (kernel space)
3571 * @sock: socket
3572 * @addr: address
3573 * @addrlen: address length
3574 * @flags: flags (O_NONBLOCK, ...)
3575 *
3576 * For datagram sockets, @addr is the addres to which datagrams are sent
3577 * by default, and the only address from which datagrams are received.
3578 * For stream sockets, attempts to connect to @addr.
3579 * Returns 0 or an error code.
3580 */
3581
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003582int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
YOSHIFUJI Hideaki4768fbc2007-02-09 23:25:31 +09003583 int flags)
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003584{
3585 return sock->ops->connect(sock, addr, addrlen, flags);
3586}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003587EXPORT_SYMBOL(kernel_connect);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003588
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003589/**
3590 * kernel_getsockname - get the address which the socket is bound (kernel space)
3591 * @sock: socket
3592 * @addr: address holder
3593 *
3594 * Fills the @addr pointer with the address which the socket is bound.
3595 * Returns 0 or an error code.
3596 */
3597
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01003598int kernel_getsockname(struct socket *sock, struct sockaddr *addr)
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003599{
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01003600 return sock->ops->getname(sock, addr, 0);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003601}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003602EXPORT_SYMBOL(kernel_getsockname);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003603
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003604/**
Miaohe Lin645f0892020-08-27 07:27:49 -04003605 * kernel_getpeername - get the address which the socket is connected (kernel space)
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003606 * @sock: socket
3607 * @addr: address holder
3608 *
3609 * Fills the @addr pointer with the address which the socket is connected.
3610 * Returns 0 or an error code.
3611 */
3612
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01003613int kernel_getpeername(struct socket *sock, struct sockaddr *addr)
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003614{
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01003615 return sock->ops->getname(sock, addr, 1);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003616}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003617EXPORT_SYMBOL(kernel_getpeername);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003618
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003619/**
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003620 * kernel_sendpage - send a &page through a socket (kernel space)
3621 * @sock: socket
3622 * @page: page
3623 * @offset: page offset
3624 * @size: total size in bytes
3625 * @flags: flags (MSG_DONTWAIT, ...)
3626 *
3627 * Returns the total amount sent in bytes or an error.
3628 */
3629
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003630int kernel_sendpage(struct socket *sock, struct page *page, int offset,
3631 size_t size, int flags)
3632{
Coly Li7b62d312020-10-02 16:27:29 +08003633 if (sock->ops->sendpage) {
3634 /* Warn in case the improper page to zero-copy send */
3635 WARN_ONCE(!sendpage_ok(page), "improper page for zero-copy send");
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003636 return sock->ops->sendpage(sock, page, offset, size, flags);
Coly Li7b62d312020-10-02 16:27:29 +08003637 }
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003638 return sock_no_sendpage(sock, page, offset, size, flags);
3639}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003640EXPORT_SYMBOL(kernel_sendpage);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003641
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003642/**
3643 * kernel_sendpage_locked - send a &page through the locked sock (kernel space)
3644 * @sk: sock
3645 * @page: page
3646 * @offset: page offset
3647 * @size: total size in bytes
3648 * @flags: flags (MSG_DONTWAIT, ...)
3649 *
3650 * Returns the total amount sent in bytes or an error.
3651 * Caller must hold @sk.
3652 */
3653
Tom Herbert306b13e2017-07-28 16:22:41 -07003654int kernel_sendpage_locked(struct sock *sk, struct page *page, int offset,
3655 size_t size, int flags)
3656{
3657 struct socket *sock = sk->sk_socket;
3658
3659 if (sock->ops->sendpage_locked)
3660 return sock->ops->sendpage_locked(sk, page, offset, size,
3661 flags);
3662
3663 return sock_no_sendpage_locked(sk, page, offset, size, flags);
3664}
3665EXPORT_SYMBOL(kernel_sendpage_locked);
3666
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003667/**
Miaohe Lin645f0892020-08-27 07:27:49 -04003668 * kernel_sock_shutdown - shut down part of a full-duplex connection (kernel space)
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003669 * @sock: socket
3670 * @how: connection part
3671 *
3672 * Returns 0 or an error.
3673 */
3674
Trond Myklebust91cf45f2007-11-12 18:10:39 -08003675int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how)
3676{
3677 return sock->ops->shutdown(sock, how);
3678}
Trond Myklebust91cf45f2007-11-12 18:10:39 -08003679EXPORT_SYMBOL(kernel_sock_shutdown);
R. Parameswaran113c3072017-04-05 16:50:35 -07003680
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003681/**
3682 * kernel_sock_ip_overhead - returns the IP overhead imposed by a socket
3683 * @sk: socket
3684 *
3685 * This routine returns the IP overhead imposed by a socket i.e.
3686 * the length of the underlying IP header, depending on whether
3687 * this is an IPv4 or IPv6 socket and the length from IP options turned
3688 * on at the socket. Assumes that the caller has a lock on the socket.
R. Parameswaran113c3072017-04-05 16:50:35 -07003689 */
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003690
R. Parameswaran113c3072017-04-05 16:50:35 -07003691u32 kernel_sock_ip_overhead(struct sock *sk)
3692{
3693 struct inet_sock *inet;
3694 struct ip_options_rcu *opt;
3695 u32 overhead = 0;
R. Parameswaran113c3072017-04-05 16:50:35 -07003696#if IS_ENABLED(CONFIG_IPV6)
3697 struct ipv6_pinfo *np;
3698 struct ipv6_txoptions *optv6 = NULL;
3699#endif /* IS_ENABLED(CONFIG_IPV6) */
3700
3701 if (!sk)
3702 return overhead;
3703
R. Parameswaran113c3072017-04-05 16:50:35 -07003704 switch (sk->sk_family) {
3705 case AF_INET:
3706 inet = inet_sk(sk);
3707 overhead += sizeof(struct iphdr);
3708 opt = rcu_dereference_protected(inet->inet_opt,
stephen hemminger614d79c2017-07-24 10:25:22 -07003709 sock_owned_by_user(sk));
R. Parameswaran113c3072017-04-05 16:50:35 -07003710 if (opt)
3711 overhead += opt->opt.optlen;
3712 return overhead;
3713#if IS_ENABLED(CONFIG_IPV6)
3714 case AF_INET6:
3715 np = inet6_sk(sk);
3716 overhead += sizeof(struct ipv6hdr);
3717 if (np)
3718 optv6 = rcu_dereference_protected(np->opt,
stephen hemminger614d79c2017-07-24 10:25:22 -07003719 sock_owned_by_user(sk));
R. Parameswaran113c3072017-04-05 16:50:35 -07003720 if (optv6)
3721 overhead += (optv6->opt_flen + optv6->opt_nflen);
3722 return overhead;
3723#endif /* IS_ENABLED(CONFIG_IPV6) */
3724 default: /* Returns 0 overhead if the socket is not ipv4 or ipv6 */
3725 return overhead;
3726 }
3727}
3728EXPORT_SYMBOL(kernel_sock_ip_overhead);