blob: 38eec1583f6dae49d4c59ed1ded8c7d1805f9435 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <linux/socket.h>
57#include <linux/file.h>
58#include <linux/net.h>
59#include <linux/interrupt.h>
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -070060#include <linux/thread_info.h>
Stephen Hemminger55737fd2006-09-01 00:23:39 -070061#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <linux/netdevice.h>
63#include <linux/proc_fs.h>
64#include <linux/seq_file.h>
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080065#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#include <linux/if_bridge.h>
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -030067#include <linux/if_frad.h>
68#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>
76#include <linux/security.h>
77#include <linux/syscalls.h>
78#include <linux/compat.h>
79#include <linux/kmod.h>
David Woodhouse3ec3b2f2005-05-17 12:08:48 +010080#include <linux/audit.h>
Adrian Bunkd86b5e02006-01-21 00:46:55 +010081#include <linux/wireless.h>
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -070082#include <linux/nsproxy.h>
Nick Black1fd7317d2009-09-22 16:43:33 -070083#include <linux/magic.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090084#include <linux/slab.h>
Masatake YAMATO600e1772012-08-29 10:44:29 +000085#include <linux/xattr.h>
Jeremy Clinec8e8cd52018-07-27 22:43:01 +000086#include <linux/nospec.h>
Paolo Abeni8c3c4472019-05-03 17:01:39 +020087#include <linux/indirect_call_wrapper.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080089#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070090#include <asm/unistd.h>
91
92#include <net/compat.h>
David S. Miller87de87d2008-06-03 09:14:03 -070093#include <net/wext.h>
Herbert Xuf8451722010-05-24 00:12:34 -070094#include <net/cls_cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96#include <net/sock.h>
97#include <linux/netfilter.h>
98
Arnd Bergmann6b960182009-11-06 23:10:54 -080099#include <linux/if_tun.h>
100#include <linux/ipv6_route.h>
101#include <linux/route.h>
Arnd Bergmann6b960182009-11-06 23:10:54 -0800102#include <linux/sockios.h>
Eliezer Tamir076bb0c2013-07-10 17:13:17 +0300103#include <net/busy_poll.h>
Willem de Bruijnf24b9be2014-08-04 22:11:45 -0400104#include <linux/errqueue.h>
Eliezer Tamir06021292013-06-10 11:39:50 +0300105
Paolo Abeni8c3c4472019-05-03 17:01:39 +0200106/* proto_ops for ipv4 and ipv6 use the same {recv,send}msg function */
107#if IS_ENABLED(CONFIG_INET)
108#define INDIRECT_CALL_INET4(f, f1, ...) INDIRECT_CALL_1(f, f1, __VA_ARGS__)
109#else
110#define INDIRECT_CALL_INET4(f, f1, ...) f(__VA_ARGS__)
111#endif
112
Cong Wange0d10952013-08-01 11:10:25 +0800113#ifdef CONFIG_NET_RX_BUSY_POLL
Eliezer Tamir64b0dc52013-07-10 17:13:36 +0300114unsigned int sysctl_net_busy_read __read_mostly;
115unsigned int sysctl_net_busy_poll __read_mostly;
Eliezer Tamir06021292013-06-10 11:39:50 +0300116#endif
Arnd Bergmann6b960182009-11-06 23:10:54 -0800117
Al Viro8ae5e0302014-11-28 19:40:50 -0500118static ssize_t sock_read_iter(struct kiocb *iocb, struct iov_iter *to);
119static ssize_t sock_write_iter(struct kiocb *iocb, struct iov_iter *from);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700120static int sock_mmap(struct file *file, struct vm_area_struct *vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122static int sock_close(struct inode *inode, struct file *file);
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700123static __poll_t sock_poll(struct file *file,
124 struct poll_table_struct *wait);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700125static long sock_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
Shaun Pereira89bbfc92006-03-21 23:58:08 -0800126#ifdef CONFIG_COMPAT
127static long compat_sock_ioctl(struct file *file,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700128 unsigned int cmd, unsigned long arg);
Shaun Pereira89bbfc92006-03-21 23:58:08 -0800129#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130static int sock_fasync(int fd, struct file *filp, int on);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131static ssize_t sock_sendpage(struct file *file, struct page *page,
132 int offset, size_t size, loff_t *ppos, int more);
Jens Axboe9c55e012007-11-06 23:30:13 -0800133static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700134 struct pipe_inode_info *pipe, size_t len,
Jens Axboe9c55e012007-11-06 23:30:13 -0800135 unsigned int flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137/*
138 * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
139 * in the operation structures but are done directly via the socketcall() multiplexor.
140 */
141
Arjan van de Venda7071d2007-02-12 00:55:36 -0800142static const struct file_operations socket_file_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 .owner = THIS_MODULE,
144 .llseek = no_llseek,
Al Viro8ae5e0302014-11-28 19:40:50 -0500145 .read_iter = sock_read_iter,
146 .write_iter = sock_write_iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 .poll = sock_poll,
148 .unlocked_ioctl = sock_ioctl,
Shaun Pereira89bbfc92006-03-21 23:58:08 -0800149#ifdef CONFIG_COMPAT
150 .compat_ioctl = compat_sock_ioctl,
151#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 .mmap = sock_mmap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 .release = sock_close,
154 .fasync = sock_fasync,
Jens Axboe5274f052006-03-30 15:15:30 +0200155 .sendpage = sock_sendpage,
156 .splice_write = generic_splice_sendpage,
Jens Axboe9c55e012007-11-06 23:30:13 -0800157 .splice_read = sock_splice_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158};
159
160/*
161 * The protocol list. Each protocol is registered in here.
162 */
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164static DEFINE_SPINLOCK(net_family_lock);
Eric Dumazet190683a2010-11-10 10:50:44 +0000165static const struct net_proto_family __rcu *net_families[NPROTO] __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700168 * Support routines.
169 * Move socket addresses back and forth across the kernel/user
170 * divide and look after the messy bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 */
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173/**
174 * move_addr_to_kernel - copy a socket address into kernel space
175 * @uaddr: Address in user space
176 * @kaddr: Address in kernel space
177 * @ulen: Length in user space
178 *
179 * The address is copied into kernel space. If the provided address is
180 * too long an error code of -EINVAL is returned. If the copy gives
181 * invalid addresses -EFAULT is returned. On a success 0 is returned.
182 */
183
Maciej Żenczykowski43db3622012-03-11 12:51:50 +0000184int move_addr_to_kernel(void __user *uaddr, int ulen, struct sockaddr_storage *kaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -0700186 if (ulen < 0 || ulen > sizeof(struct sockaddr_storage))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700188 if (ulen == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 return 0;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700190 if (copy_from_user(kaddr, uaddr, ulen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 return -EFAULT;
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100192 return audit_sockaddr(ulen, kaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
194
195/**
196 * move_addr_to_user - copy an address to user space
197 * @kaddr: kernel space address
198 * @klen: length of address in kernel
199 * @uaddr: user space address
200 * @ulen: pointer to user length field
201 *
202 * The value pointed to by ulen on entry is the buffer length available.
203 * This is overwritten with the buffer space used. -EINVAL is returned
204 * if an overlong buffer is specified or a negative buffer size. -EFAULT
205 * is returned if either the buffer or the length field are not
206 * accessible.
207 * After copying the data up to the limit the user specifies, the true
208 * length of the data is written over the length limit the user
209 * specified. Zero is returned for a success.
210 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700211
Maciej Żenczykowski43db3622012-03-11 12:51:50 +0000212static int move_addr_to_user(struct sockaddr_storage *kaddr, int klen,
stephen hemminger11165f12010-10-18 14:27:29 +0000213 void __user *uaddr, int __user *ulen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
215 int err;
216 int len;
217
Hannes Frederic Sowa68c6beb2013-11-21 03:14:34 +0100218 BUG_ON(klen > sizeof(struct sockaddr_storage));
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700219 err = get_user(len, ulen);
220 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 return err;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700222 if (len > klen)
223 len = klen;
Hannes Frederic Sowa68c6beb2013-11-21 03:14:34 +0100224 if (len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700226 if (len) {
Steve Grubbd6fe3942006-03-30 12:20:22 -0500227 if (audit_sockaddr(klen, kaddr))
228 return -ENOMEM;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700229 if (copy_to_user(uaddr, kaddr, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 return -EFAULT;
231 }
232 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700233 * "fromlen shall refer to the value before truncation.."
234 * 1003.1g
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 */
236 return __put_user(klen, ulen);
237}
238
Alexey Dobriyan08009a72018-02-24 21:20:33 +0300239static struct kmem_cache *sock_inode_cachep __ro_after_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241static struct inode *sock_alloc_inode(struct super_block *sb)
242{
243 struct socket_alloc *ei;
Eric Dumazeteaefd112011-02-18 03:26:36 +0000244 struct socket_wq *wq;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700245
Christoph Lametere94b1762006-12-06 20:33:17 -0800246 ei = kmem_cache_alloc(sock_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 if (!ei)
248 return NULL;
Eric Dumazeteaefd112011-02-18 03:26:36 +0000249 wq = kmalloc(sizeof(*wq), GFP_KERNEL);
250 if (!wq) {
Eric Dumazet43815482010-04-29 11:01:49 +0000251 kmem_cache_free(sock_inode_cachep, ei);
252 return NULL;
253 }
Eric Dumazeteaefd112011-02-18 03:26:36 +0000254 init_waitqueue_head(&wq->wait);
255 wq->fasync_list = NULL;
Nicolai Stange574aab12015-12-29 13:29:55 +0100256 wq->flags = 0;
Christoph Hellwige6476c22018-07-30 09:45:07 +0200257 ei->socket.wq = wq;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 ei->socket.state = SS_UNCONNECTED;
260 ei->socket.flags = 0;
261 ei->socket.ops = NULL;
262 ei->socket.sk = NULL;
263 ei->socket.file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265 return &ei->vfs_inode;
266}
267
268static void sock_destroy_inode(struct inode *inode)
269{
Eric Dumazet43815482010-04-29 11:01:49 +0000270 struct socket_alloc *ei;
271
272 ei = container_of(inode, struct socket_alloc, vfs_inode);
Christoph Hellwige6476c22018-07-30 09:45:07 +0200273 kfree_rcu(ei->socket.wq, rcu);
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,
298 .destroy_inode = sock_destroy_inode,
299 .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
Al Viroc74a1cb2011-01-12 16:59:34 -0500356static struct dentry *sockfs_mount(struct file_system_type *fs_type,
357 int flags, const char *dev_name, void *data)
358{
Andreas Gruenbacherbba0bd32016-09-29 17:48:35 +0200359 return mount_pseudo_xattr(fs_type, "socket:", &sockfs_ops,
360 sockfs_xattr_handlers,
361 &sockfs_dentry_operations, SOCKFS_MAGIC);
Al Viroc74a1cb2011-01-12 16:59:34 -0500362}
363
364static struct vfsmount *sock_mnt __read_mostly;
365
366static struct file_system_type sock_fs_type = {
367 .name = "sockfs",
368 .mount = sockfs_mount,
369 .kill_sb = kill_anon_super,
370};
371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372/*
373 * Obtains the first available file descriptor and sets it up for use.
374 *
David S. Miller39d8c1b2006-03-20 17:13:49 -0800375 * These functions create file structures and maps them to fd space
376 * of the current process. On success it returns file descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 * and file struct implicitly stored in sock->file.
378 * Note that another thread may close file descriptor before we return
379 * from this function. We use the fact that now we do not refer
380 * to socket after mapping. If one day we will need it, this
381 * function will increment ref. count on file by 1.
382 *
383 * In any case returned fd MAY BE not valid!
384 * This race condition is unavoidable
385 * with shared fd spaces, we cannot solve it inside kernel,
386 * but we take care of internal coherence yet.
387 */
388
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300389/**
390 * sock_alloc_file - Bind a &socket to a &file
391 * @sock: socket
392 * @flags: file status flags
393 * @dname: protocol name
394 *
395 * Returns the &file bound with @sock, implicitly storing it
396 * in sock->file. If dname is %NULL, sets to "".
397 * On failure the return is a ERR pointer (see linux/err.h).
398 * This function uses GFP_KERNEL internally.
399 */
400
Linus Torvaldsaab174f2012-10-02 20:25:04 -0700401struct file *sock_alloc_file(struct socket *sock, int flags, const char *dname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
Al Viro7cbe66b2009-08-05 19:59:08 +0400403 struct file *file;
David S. Miller39d8c1b2006-03-20 17:13:49 -0800404
Al Virod93aa9d2018-06-09 09:40:05 -0400405 if (!dname)
406 dname = sock->sk ? sock->sk->sk_prot_creator->name : "";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Al Virod93aa9d2018-06-09 09:40:05 -0400408 file = alloc_file_pseudo(SOCK_INODE(sock), sock_mnt, dname,
409 O_RDWR | (flags & O_NONBLOCK),
410 &socket_file_ops);
Viresh Kumarb5ffe632015-08-12 15:59:47 +0530411 if (IS_ERR(file)) {
Al Viro8e1611e2017-12-05 23:29:09 +0000412 sock_release(sock);
Anatol Pomozov39b65252012-09-12 20:11:55 -0700413 return file;
Al Virocc3808f2009-08-06 09:43:59 +0400414 }
David S. Miller39d8c1b2006-03-20 17:13:49 -0800415
416 sock->file = file;
David S. Miller39d8c1b2006-03-20 17:13:49 -0800417 file->private_data = sock;
Al Viro28407632012-08-17 23:54:15 -0400418 return file;
David S. Miller39d8c1b2006-03-20 17:13:49 -0800419}
Al Viro56b31d12012-08-18 00:25:51 -0400420EXPORT_SYMBOL(sock_alloc_file);
David S. Miller39d8c1b2006-03-20 17:13:49 -0800421
Al Viro56b31d12012-08-18 00:25:51 -0400422static int sock_map_fd(struct socket *sock, int flags)
David S. Miller39d8c1b2006-03-20 17:13:49 -0800423{
424 struct file *newfile;
Al Viro28407632012-08-17 23:54:15 -0400425 int fd = get_unused_fd_flags(flags);
Al Viroce4bb042018-01-10 18:47:05 -0500426 if (unlikely(fd < 0)) {
427 sock_release(sock);
Al Viro28407632012-08-17 23:54:15 -0400428 return fd;
Al Viroce4bb042018-01-10 18:47:05 -0500429 }
David S. Miller39d8c1b2006-03-20 17:13:49 -0800430
Linus Torvaldsaab174f2012-10-02 20:25:04 -0700431 newfile = sock_alloc_file(sock, flags, NULL);
Al Viro28407632012-08-17 23:54:15 -0400432 if (likely(!IS_ERR(newfile))) {
David S. Miller39d8c1b2006-03-20 17:13:49 -0800433 fd_install(fd, newfile);
Al Viro28407632012-08-17 23:54:15 -0400434 return fd;
435 }
Al Viro7cbe66b2009-08-05 19:59:08 +0400436
Al Viro28407632012-08-17 23:54:15 -0400437 put_unused_fd(fd);
438 return PTR_ERR(newfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439}
440
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300441/**
442 * sock_from_file - Return the &socket bounded to @file.
443 * @file: file
444 * @err: pointer to an error code return
445 *
446 * On failure returns %NULL and assigns -ENOTSOCK to @err.
447 */
448
John Fastabend406a3c62012-07-20 10:39:25 +0000449struct socket *sock_from_file(struct file *file, int *err)
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800450{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800451 if (file->f_op == &socket_file_ops)
452 return file->private_data; /* set in sock_map_fd */
453
Eric Dumazet23bb80d2007-02-08 14:59:57 -0800454 *err = -ENOTSOCK;
455 return NULL;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800456}
John Fastabend406a3c62012-07-20 10:39:25 +0000457EXPORT_SYMBOL(sock_from_file);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459/**
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700460 * sockfd_lookup - Go from a file number to its socket slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 * @fd: file handle
462 * @err: pointer to an error code return
463 *
464 * The file handle passed in is locked and the socket it is bound
Rosen, Rami241c4662017-05-21 22:12:38 +0300465 * to is returned. If an error occurs the err pointer is overwritten
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 * with a negative errno code and NULL is returned. The function checks
467 * for both invalid handles and passing a handle which is not a socket.
468 *
469 * On a success the socket object pointer is returned.
470 */
471
472struct socket *sockfd_lookup(int fd, int *err)
473{
474 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 struct socket *sock;
476
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700477 file = fget(fd);
478 if (!file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 *err = -EBADF;
480 return NULL;
481 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700482
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800483 sock = sock_from_file(file, err);
484 if (!sock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 fput(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 return sock;
487}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700488EXPORT_SYMBOL(sockfd_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800490static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed)
491{
Al Viro00e188e2014-03-03 23:48:18 -0500492 struct fd f = fdget(fd);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800493 struct socket *sock;
494
Hua Zhong36725582006-04-19 15:25:02 -0700495 *err = -EBADF;
Al Viro00e188e2014-03-03 23:48:18 -0500496 if (f.file) {
497 sock = sock_from_file(f.file, err);
498 if (likely(sock)) {
499 *fput_needed = f.flags;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800500 return sock;
Al Viro00e188e2014-03-03 23:48:18 -0500501 }
502 fdput(f);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800503 }
504 return NULL;
505}
506
Masatake YAMATO600e1772012-08-29 10:44:29 +0000507static ssize_t sockfs_listxattr(struct dentry *dentry, char *buffer,
508 size_t size)
509{
510 ssize_t len;
511 ssize_t used = 0;
512
David Howellsc5ef6032015-03-17 22:26:16 +0000513 len = security_inode_listsecurity(d_inode(dentry), buffer, size);
Masatake YAMATO600e1772012-08-29 10:44:29 +0000514 if (len < 0)
515 return len;
516 used += len;
517 if (buffer) {
518 if (size < used)
519 return -ERANGE;
520 buffer += len;
521 }
522
523 len = (XATTR_NAME_SOCKPROTONAME_LEN + 1);
524 used += len;
525 if (buffer) {
526 if (size < used)
527 return -ERANGE;
528 memcpy(buffer, XATTR_NAME_SOCKPROTONAME, len);
529 buffer += len;
530 }
531
532 return used;
533}
534
Tobias Klauserdc647ec2017-01-10 09:30:51 +0100535static int sockfs_setattr(struct dentry *dentry, struct iattr *iattr)
Lorenzo Colitti86741ec2016-11-04 02:23:41 +0900536{
537 int err = simple_setattr(dentry, iattr);
538
Eric Biggerse1a3a602016-12-30 17:42:32 -0600539 if (!err && (iattr->ia_valid & ATTR_UID)) {
Lorenzo Colitti86741ec2016-11-04 02:23:41 +0900540 struct socket *sock = SOCKET_I(d_inode(dentry));
541
Cong Wang6d8c50d2018-06-07 13:39:49 -0700542 if (sock->sk)
543 sock->sk->sk_uid = iattr->ia_uid;
544 else
545 err = -ENOENT;
Lorenzo Colitti86741ec2016-11-04 02:23:41 +0900546 }
547
548 return err;
549}
550
Masatake YAMATO600e1772012-08-29 10:44:29 +0000551static const struct inode_operations sockfs_inode_ops = {
Masatake YAMATO600e1772012-08-29 10:44:29 +0000552 .listxattr = sockfs_listxattr,
Lorenzo Colitti86741ec2016-11-04 02:23:41 +0900553 .setattr = sockfs_setattr,
Masatake YAMATO600e1772012-08-29 10:44:29 +0000554};
555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556/**
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300557 * sock_alloc - allocate a socket
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700558 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 * Allocate a new inode and socket object. The two are bound together
560 * and initialised. The socket is then returned. If we are out of inodes
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300561 * NULL is returned. This functions uses GFP_KERNEL internally.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 */
563
Tom Herbertf4a00aa2016-03-07 14:11:01 -0800564struct socket *sock_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700566 struct inode *inode;
567 struct socket *sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Eric Dumazeta209dfc2011-07-26 11:36:34 +0200569 inode = new_inode_pseudo(sock_mnt->mnt_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 if (!inode)
571 return NULL;
572
573 sock = SOCKET_I(inode);
574
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400575 inode->i_ino = get_next_ino();
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700576 inode->i_mode = S_IFSOCK | S_IRWXUGO;
David Howells8192b0c2008-11-14 10:39:10 +1100577 inode->i_uid = current_fsuid();
578 inode->i_gid = current_fsgid();
Masatake YAMATO600e1772012-08-29 10:44:29 +0000579 inode->i_op = &sockfs_inode_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 return sock;
582}
Tom Herbertf4a00aa2016-03-07 14:11:01 -0800583EXPORT_SYMBOL(sock_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585/**
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300586 * sock_release - close a socket
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 * @sock: socket to close
588 *
589 * The socket is released from the protocol stack if it has a release
590 * callback, and the inode is then released if the socket is bound to
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700591 * an inode not a file.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700593
Cong Wang6d8c50d2018-06-07 13:39:49 -0700594static void __sock_release(struct socket *sock, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
596 if (sock->ops) {
597 struct module *owner = sock->ops->owner;
598
Cong Wang6d8c50d2018-06-07 13:39:49 -0700599 if (inode)
600 inode_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 sock->ops->release(sock);
Eric Biggersff7b11a2019-02-21 14:13:56 -0800602 sock->sk = NULL;
Cong Wang6d8c50d2018-06-07 13:39:49 -0700603 if (inode)
604 inode_unlock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 sock->ops = NULL;
606 module_put(owner);
607 }
608
Christoph Hellwige6476c22018-07-30 09:45:07 +0200609 if (sock->wq->fasync_list)
Yang Yingliang3410f222014-02-12 17:09:55 +0800610 pr_err("%s: fasync list not empty!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 if (!sock->file) {
613 iput(SOCK_INODE(sock));
614 return;
615 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700616 sock->file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617}
Cong Wang6d8c50d2018-06-07 13:39:49 -0700618
619void sock_release(struct socket *sock)
620{
621 __sock_release(sock, NULL);
622}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700623EXPORT_SYMBOL(sock_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -0400625void __sock_tx_timestamp(__u16 tsflags, __u8 *tx_flags)
Patrick Ohly20d49472009-02-12 05:03:38 +0000626{
Eric Dumazet140c55d2014-08-06 11:49:29 +0200627 u8 flags = *tx_flags;
628
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -0400629 if (tsflags & SOF_TIMESTAMPING_TX_HARDWARE)
Eric Dumazet140c55d2014-08-06 11:49:29 +0200630 flags |= SKBTX_HW_TSTAMP;
631
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -0400632 if (tsflags & SOF_TIMESTAMPING_TX_SOFTWARE)
Eric Dumazet140c55d2014-08-06 11:49:29 +0200633 flags |= SKBTX_SW_TSTAMP;
634
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -0400635 if (tsflags & SOF_TIMESTAMPING_TX_SCHED)
Eric Dumazet140c55d2014-08-06 11:49:29 +0200636 flags |= SKBTX_SCHED_TSTAMP;
637
Eric Dumazet140c55d2014-08-06 11:49:29 +0200638 *tx_flags = flags;
Patrick Ohly20d49472009-02-12 05:03:38 +0000639}
Willem de Bruijn67cc0d42014-09-08 19:58:58 -0400640EXPORT_SYMBOL(__sock_tx_timestamp);
Patrick Ohly20d49472009-02-12 05:03:38 +0000641
Paolo Abeni8c3c4472019-05-03 17:01:39 +0200642INDIRECT_CALLABLE_DECLARE(int inet_sendmsg(struct socket *, struct msghdr *,
643 size_t));
Al Virod8725c82014-12-11 00:02:50 -0500644static inline int sock_sendmsg_nosec(struct socket *sock, struct msghdr *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
Paolo Abeni8c3c4472019-05-03 17:01:39 +0200646 int ret = INDIRECT_CALL_INET4(sock->ops->sendmsg, inet_sendmsg, sock,
647 msg, msg_data_left(msg));
Al Virod8725c82014-12-11 00:02:50 -0500648 BUG_ON(ret == -EIOCBQUEUED);
649 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650}
Gu Zheng0cf00c62014-12-05 15:14:23 +0800651
Randy Dunlap85806af2019-05-18 21:23:07 -0700652/**
653 * sock_sendmsg - send a message through @sock
654 * @sock: socket
655 * @msg: message to send
656 *
657 * Sends @msg through @sock, passing through LSM.
658 * Returns the number of bytes sent, or an error code.
659 */
Al Virod8725c82014-12-11 00:02:50 -0500660int sock_sendmsg(struct socket *sock, struct msghdr *msg)
Gu Zheng0cf00c62014-12-05 15:14:23 +0800661{
Al Virod8725c82014-12-11 00:02:50 -0500662 int err = security_socket_sendmsg(sock, msg,
Al Viro01e97e62014-12-15 21:39:31 -0500663 msg_data_left(msg));
Ying Xue1b784142015-03-02 15:37:48 +0800664
Al Virod8725c82014-12-11 00:02:50 -0500665 return err ?: sock_sendmsg_nosec(sock, msg);
Gu Zheng0cf00c62014-12-05 15:14:23 +0800666}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700667EXPORT_SYMBOL(sock_sendmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300669/**
670 * kernel_sendmsg - send a message through @sock (kernel-space)
671 * @sock: socket
672 * @msg: message header
673 * @vec: kernel vec
674 * @num: vec array length
675 * @size: total message data size
676 *
677 * Builds the message data with @vec and sends it through @sock.
678 * Returns the number of bytes sent, or an error code.
679 */
680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
682 struct kvec *vec, size_t num, size_t size)
683{
David Howellsaa563d72018-10-20 00:57:56 +0100684 iov_iter_kvec(&msg->msg_iter, WRITE, vec, num, size);
Al Virod8725c82014-12-11 00:02:50 -0500685 return sock_sendmsg(sock, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700687EXPORT_SYMBOL(kernel_sendmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300689/**
690 * kernel_sendmsg_locked - send a message through @sock (kernel-space)
691 * @sk: sock
692 * @msg: message header
693 * @vec: output s/g array
694 * @num: output s/g array length
695 * @size: total message data size
696 *
697 * Builds the message data with @vec and sends it through @sock.
698 * Returns the number of bytes sent, or an error code.
699 * Caller must hold @sk.
700 */
701
Tom Herbert306b13e2017-07-28 16:22:41 -0700702int kernel_sendmsg_locked(struct sock *sk, struct msghdr *msg,
703 struct kvec *vec, size_t num, size_t size)
704{
705 struct socket *sock = sk->sk_socket;
706
707 if (!sock->ops->sendmsg_locked)
John Fastabenddb5980d2017-08-15 22:31:34 -0700708 return sock_no_sendmsg_locked(sk, msg, size);
Tom Herbert306b13e2017-07-28 16:22:41 -0700709
David Howellsaa563d72018-10-20 00:57:56 +0100710 iov_iter_kvec(&msg->msg_iter, WRITE, vec, num, size);
Tom Herbert306b13e2017-07-28 16:22:41 -0700711
712 return sock->ops->sendmsg_locked(sk, msg, msg_data_left(msg));
713}
714EXPORT_SYMBOL(kernel_sendmsg_locked);
715
Soheil Hassas Yeganeh8605330a2017-03-18 17:02:59 -0400716static bool skb_is_err_queue(const struct sk_buff *skb)
717{
718 /* pkt_type of skbs enqueued on the error queue are set to
719 * PACKET_OUTGOING in skb_set_err_queue(). This is only safe to do
720 * in recvmsg, since skbs received on a local socket will never
721 * have a pkt_type of PACKET_OUTGOING.
722 */
723 return skb->pkt_type == PACKET_OUTGOING;
724}
725
Miroslav Lichvarb50a5c72017-05-19 17:52:40 +0200726/* On transmit, software and hardware timestamps are returned independently.
727 * As the two skb clones share the hardware timestamp, which may be updated
728 * before the software timestamp is received, a hardware TX timestamp may be
729 * returned only if there is no software TX timestamp. Ignore false software
730 * timestamps, which may be made in the __sock_recv_timestamp() call when the
Deepa Dinamani7f1bc6e2019-02-02 07:34:46 -0800731 * option SO_TIMESTAMP_OLD(NS) is enabled on the socket, even when the skb has a
Miroslav Lichvarb50a5c72017-05-19 17:52:40 +0200732 * hardware timestamp.
733 */
734static bool skb_is_swtx_tstamp(const struct sk_buff *skb, int false_tstamp)
735{
736 return skb->tstamp && !false_tstamp && skb_is_err_queue(skb);
737}
738
Miroslav Lichvaraad9c8c2017-05-19 17:52:38 +0200739static void put_ts_pktinfo(struct msghdr *msg, struct sk_buff *skb)
740{
741 struct scm_ts_pktinfo ts_pktinfo;
742 struct net_device *orig_dev;
743
744 if (!skb_mac_header_was_set(skb))
745 return;
746
747 memset(&ts_pktinfo, 0, sizeof(ts_pktinfo));
748
749 rcu_read_lock();
750 orig_dev = dev_get_by_napi_id(skb_napi_id(skb));
751 if (orig_dev)
752 ts_pktinfo.if_index = orig_dev->ifindex;
753 rcu_read_unlock();
754
755 ts_pktinfo.pkt_length = skb->len - skb_mac_offset(skb);
756 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPING_PKTINFO,
757 sizeof(ts_pktinfo), &ts_pktinfo);
758}
759
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700760/*
761 * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
762 */
763void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
764 struct sk_buff *skb)
765{
Patrick Ohly20d49472009-02-12 05:03:38 +0000766 int need_software_tstamp = sock_flag(sk, SOCK_RCVTSTAMP);
Deepa Dinamani887feae2019-02-02 07:34:50 -0800767 int new_tstamp = sock_flag(sk, SOCK_TSTAMP_NEW);
Deepa Dinamani97184752019-02-02 07:34:51 -0800768 struct scm_timestamping_internal tss;
769
Miroslav Lichvarb50a5c72017-05-19 17:52:40 +0200770 int empty = 1, false_tstamp = 0;
Patrick Ohly20d49472009-02-12 05:03:38 +0000771 struct skb_shared_hwtstamps *shhwtstamps =
772 skb_hwtstamps(skb);
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700773
Patrick Ohly20d49472009-02-12 05:03:38 +0000774 /* Race occurred between timestamp enabling and packet
775 receiving. Fill in the current time for now. */
Miroslav Lichvarb50a5c72017-05-19 17:52:40 +0200776 if (need_software_tstamp && skb->tstamp == 0) {
Patrick Ohly20d49472009-02-12 05:03:38 +0000777 __net_timestamp(skb);
Miroslav Lichvarb50a5c72017-05-19 17:52:40 +0200778 false_tstamp = 1;
779 }
Patrick Ohly20d49472009-02-12 05:03:38 +0000780
781 if (need_software_tstamp) {
782 if (!sock_flag(sk, SOCK_RCVTSTAMPNS)) {
Deepa Dinamani887feae2019-02-02 07:34:50 -0800783 if (new_tstamp) {
784 struct __kernel_sock_timeval tv;
785
786 skb_get_new_timestamp(skb, &tv);
787 put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMP_NEW,
788 sizeof(tv), &tv);
789 } else {
790 struct __kernel_old_timeval tv;
791
792 skb_get_timestamp(skb, &tv);
793 put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMP_OLD,
794 sizeof(tv), &tv);
795 }
Patrick Ohly20d49472009-02-12 05:03:38 +0000796 } else {
Deepa Dinamani887feae2019-02-02 07:34:50 -0800797 if (new_tstamp) {
798 struct __kernel_timespec ts;
799
800 skb_get_new_timestampns(skb, &ts);
801 put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMPNS_NEW,
802 sizeof(ts), &ts);
803 } else {
804 struct timespec ts;
805
806 skb_get_timestampns(skb, &ts);
807 put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMPNS_OLD,
808 sizeof(ts), &ts);
809 }
Patrick Ohly20d49472009-02-12 05:03:38 +0000810 }
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700811 }
Patrick Ohly20d49472009-02-12 05:03:38 +0000812
Willem de Bruijnf24b9be2014-08-04 22:11:45 -0400813 memset(&tss, 0, sizeof(tss));
Willem de Bruijnc1991052014-09-03 12:01:18 -0400814 if ((sk->sk_tsflags & SOF_TIMESTAMPING_SOFTWARE) &&
Deepa Dinamani97184752019-02-02 07:34:51 -0800815 ktime_to_timespec64_cond(skb->tstamp, tss.ts + 0))
Patrick Ohly20d49472009-02-12 05:03:38 +0000816 empty = 0;
Willem de Bruijn4d276eb2014-07-25 18:01:32 -0400817 if (shhwtstamps &&
Willem de Bruijnb9f40e22014-08-04 22:11:46 -0400818 (sk->sk_tsflags & SOF_TIMESTAMPING_RAW_HARDWARE) &&
Miroslav Lichvarb50a5c72017-05-19 17:52:40 +0200819 !skb_is_swtx_tstamp(skb, false_tstamp) &&
Deepa Dinamani97184752019-02-02 07:34:51 -0800820 ktime_to_timespec64_cond(shhwtstamps->hwtstamp, tss.ts + 2)) {
Willem de Bruijn4d276eb2014-07-25 18:01:32 -0400821 empty = 0;
Miroslav Lichvaraad9c8c2017-05-19 17:52:38 +0200822 if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_PKTINFO) &&
823 !skb_is_err_queue(skb))
824 put_ts_pktinfo(msg, skb);
825 }
Francis Yan1c885802016-11-27 23:07:18 -0800826 if (!empty) {
Deepa Dinamani97184752019-02-02 07:34:51 -0800827 if (sock_flag(sk, SOCK_TSTAMP_NEW))
828 put_cmsg_scm_timestamping64(msg, &tss);
829 else
830 put_cmsg_scm_timestamping(msg, &tss);
Francis Yan1c885802016-11-27 23:07:18 -0800831
Soheil Hassas Yeganeh8605330a2017-03-18 17:02:59 -0400832 if (skb_is_err_queue(skb) && skb->len &&
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -0400833 SKB_EXT_ERR(skb)->opt_stats)
Francis Yan1c885802016-11-27 23:07:18 -0800834 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPING_OPT_STATS,
835 skb->len, skb->data);
836 }
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700837}
Arnaldo Carvalho de Melo7c81fd82007-03-10 00:39:35 -0300838EXPORT_SYMBOL_GPL(__sock_recv_timestamp);
839
Johannes Berg6e3e9392011-11-09 10:15:42 +0100840void __sock_recv_wifi_status(struct msghdr *msg, struct sock *sk,
841 struct sk_buff *skb)
842{
843 int ack;
844
845 if (!sock_flag(sk, SOCK_WIFI_STATUS))
846 return;
847 if (!skb->wifi_acked_valid)
848 return;
849
850 ack = skb->wifi_acked;
851
852 put_cmsg(msg, SOL_SOCKET, SCM_WIFI_STATUS, sizeof(ack), &ack);
853}
854EXPORT_SYMBOL_GPL(__sock_recv_wifi_status);
855
stephen hemminger11165f12010-10-18 14:27:29 +0000856static inline void sock_recv_drops(struct msghdr *msg, struct sock *sk,
857 struct sk_buff *skb)
Neil Horman3b885782009-10-12 13:26:31 -0700858{
Eyal Birger744d5a32015-03-01 14:58:31 +0200859 if (sock_flag(sk, SOCK_RXQ_OVFL) && skb && SOCK_SKB_CB(skb)->dropcount)
Neil Horman3b885782009-10-12 13:26:31 -0700860 put_cmsg(msg, SOL_SOCKET, SO_RXQ_OVFL,
Eyal Birger744d5a32015-03-01 14:58:31 +0200861 sizeof(__u32), &SOCK_SKB_CB(skb)->dropcount);
Neil Horman3b885782009-10-12 13:26:31 -0700862}
863
Eric Dumazet767dd032010-04-28 19:14:43 +0000864void __sock_recv_ts_and_drops(struct msghdr *msg, struct sock *sk,
Neil Horman3b885782009-10-12 13:26:31 -0700865 struct sk_buff *skb)
866{
867 sock_recv_timestamp(msg, sk, skb);
868 sock_recv_drops(msg, sk, skb);
869}
Eric Dumazet767dd032010-04-28 19:14:43 +0000870EXPORT_SYMBOL_GPL(__sock_recv_ts_and_drops);
Neil Horman3b885782009-10-12 13:26:31 -0700871
Paolo Abeni8c3c4472019-05-03 17:01:39 +0200872INDIRECT_CALLABLE_DECLARE(int inet_recvmsg(struct socket *, struct msghdr *,
873 size_t , int ));
Ying Xue1b784142015-03-02 15:37:48 +0800874static inline int sock_recvmsg_nosec(struct socket *sock, struct msghdr *msg,
Al Viro2da62902015-03-14 21:13:46 -0400875 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876{
Paolo Abeni8c3c4472019-05-03 17:01:39 +0200877 return INDIRECT_CALL_INET4(sock->ops->recvmsg, inet_recvmsg, sock, msg,
878 msg_data_left(msg), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879}
880
Randy Dunlap85806af2019-05-18 21:23:07 -0700881/**
882 * sock_recvmsg - receive a message from @sock
883 * @sock: socket
884 * @msg: message to receive
885 * @flags: message flags
886 *
887 * Receives @msg from @sock, passing through LSM. Returns the total number
888 * of bytes received, or an error.
889 */
Al Viro2da62902015-03-14 21:13:46 -0400890int sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -0700891{
Al Viro2da62902015-03-14 21:13:46 -0400892 int err = security_socket_recvmsg(sock, msg, msg_data_left(msg), flags);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -0700893
Al Viro2da62902015-03-14 21:13:46 -0400894 return err ?: sock_recvmsg_nosec(sock, msg, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700896EXPORT_SYMBOL(sock_recvmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Martin Lucinac1249c02010-12-10 00:04:05 +0000898/**
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300899 * kernel_recvmsg - Receive a message from a socket (kernel space)
900 * @sock: The socket to receive the message from
901 * @msg: Received message
902 * @vec: Input s/g array for message data
903 * @num: Size of input s/g array
904 * @size: Number of bytes to read
905 * @flags: Message flags (MSG_DONTWAIT, etc...)
Martin Lucinac1249c02010-12-10 00:04:05 +0000906 *
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300907 * On return the msg structure contains the scatter/gather array passed in the
908 * vec argument. The array is modified so that it consists of the unfilled
909 * portion of the original array.
Martin Lucinac1249c02010-12-10 00:04:05 +0000910 *
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300911 * The returned value is the total number of bytes received, or an error.
Martin Lucinac1249c02010-12-10 00:04:05 +0000912 */
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300913
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700914int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
915 struct kvec *vec, size_t num, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
917 mm_segment_t oldfs = get_fs();
918 int result;
919
David Howellsaa563d72018-10-20 00:57:56 +0100920 iov_iter_kvec(&msg->msg_iter, READ, vec, num, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 set_fs(KERNEL_DS);
Al Viro2da62902015-03-14 21:13:46 -0400922 result = sock_recvmsg(sock, msg, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 set_fs(oldfs);
924 return result;
925}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700926EXPORT_SYMBOL(kernel_recvmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -0300928static ssize_t sock_sendpage(struct file *file, struct page *page,
929 int offset, size_t size, loff_t *ppos, int more)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
931 struct socket *sock;
932 int flags;
933
Eric Dumazetb69aee02005-09-06 14:42:45 -0700934 sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Eric Dumazet35f9c092012-04-05 03:05:35 +0000936 flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
937 /* more is a combination of MSG_MORE and MSG_SENDPAGE_NOTLAST */
938 flags |= more;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Linus Torvaldse6949582009-08-13 08:28:36 -0700940 return kernel_sendpage(sock, page, offset, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941}
942
Jens Axboe9c55e012007-11-06 23:30:13 -0800943static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700944 struct pipe_inode_info *pipe, size_t len,
Jens Axboe9c55e012007-11-06 23:30:13 -0800945 unsigned int flags)
946{
947 struct socket *sock = file->private_data;
948
Rémi Denis-Courmont997b37d2008-02-15 02:35:45 -0800949 if (unlikely(!sock->ops->splice_read))
Slavomir Kaslev95506582018-11-16 11:27:53 +0200950 return generic_file_splice_read(file, ppos, pipe, len, flags);
Rémi Denis-Courmont997b37d2008-02-15 02:35:45 -0800951
Jens Axboe9c55e012007-11-06 23:30:13 -0800952 return sock->ops->splice_read(sock, ppos, pipe, len, flags);
953}
954
Al Viro8ae5e0302014-11-28 19:40:50 -0500955static ssize_t sock_read_iter(struct kiocb *iocb, struct iov_iter *to)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800956{
Al Viro6d652332015-01-30 16:12:56 -0500957 struct file *file = iocb->ki_filp;
958 struct socket *sock = file->private_data;
tadeusz.struk@intel.com0345f932015-03-19 12:31:25 -0700959 struct msghdr msg = {.msg_iter = *to,
960 .msg_iocb = iocb};
Al Viro8ae5e0302014-11-28 19:40:50 -0500961 ssize_t res;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800962
Al Viro8ae5e0302014-11-28 19:40:50 -0500963 if (file->f_flags & O_NONBLOCK)
964 msg.msg_flags = MSG_DONTWAIT;
965
966 if (iocb->ki_pos != 0)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800967 return -ESPIPE;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700968
Christoph Hellwig66ee59a2015-02-11 19:56:46 +0100969 if (!iov_iter_count(to)) /* Match SYS5 behaviour */
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800970 return 0;
971
Al Viro2da62902015-03-14 21:13:46 -0400972 res = sock_recvmsg(sock, &msg, msg.msg_flags);
Al Viro8ae5e0302014-11-28 19:40:50 -0500973 *to = msg.msg_iter;
974 return res;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800975}
976
Al Viro8ae5e0302014-11-28 19:40:50 -0500977static ssize_t sock_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978{
Al Viro6d652332015-01-30 16:12:56 -0500979 struct file *file = iocb->ki_filp;
980 struct socket *sock = file->private_data;
tadeusz.struk@intel.com0345f932015-03-19 12:31:25 -0700981 struct msghdr msg = {.msg_iter = *from,
982 .msg_iocb = iocb};
Al Viro8ae5e0302014-11-28 19:40:50 -0500983 ssize_t res;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800984
Al Viro8ae5e0302014-11-28 19:40:50 -0500985 if (iocb->ki_pos != 0)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800986 return -ESPIPE;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700987
Al Viro8ae5e0302014-11-28 19:40:50 -0500988 if (file->f_flags & O_NONBLOCK)
989 msg.msg_flags = MSG_DONTWAIT;
990
Al Viro6d652332015-01-30 16:12:56 -0500991 if (sock->type == SOCK_SEQPACKET)
992 msg.msg_flags |= MSG_EOR;
993
Al Virod8725c82014-12-11 00:02:50 -0500994 res = sock_sendmsg(sock, &msg);
Al Viro8ae5e0302014-11-28 19:40:50 -0500995 *from = msg.msg_iter;
996 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997}
998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999/*
1000 * Atomic setting of ioctl hooks to avoid race
1001 * with module unload.
1002 */
1003
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001004static DEFINE_MUTEX(br_ioctl_mutex);
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001005static int (*br_ioctl_hook) (struct net *, unsigned int cmd, void __user *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Eric W. Biederman881d9662007-09-17 11:56:21 -07001007void brioctl_set(int (*hook) (struct net *, unsigned int, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001009 mutex_lock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 br_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001011 mutex_unlock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012}
1013EXPORT_SYMBOL(brioctl_set);
1014
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001015static DEFINE_MUTEX(vlan_ioctl_mutex);
Eric W. Biederman881d9662007-09-17 11:56:21 -07001016static int (*vlan_ioctl_hook) (struct net *, void __user *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Eric W. Biederman881d9662007-09-17 11:56:21 -07001018void vlan_ioctl_set(int (*hook) (struct net *, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001020 mutex_lock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 vlan_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001022 mutex_unlock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023}
1024EXPORT_SYMBOL(vlan_ioctl_set);
1025
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001026static DEFINE_MUTEX(dlci_ioctl_mutex);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001027static int (*dlci_ioctl_hook) (unsigned int, void __user *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001029void dlci_ioctl_set(int (*hook) (unsigned int, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001031 mutex_lock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 dlci_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001033 mutex_unlock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034}
1035EXPORT_SYMBOL(dlci_ioctl_set);
1036
Arnd Bergmann6b960182009-11-06 23:10:54 -08001037static long sock_do_ioctl(struct net *net, struct socket *sock,
Johannes Berg63ff03a2019-01-25 22:43:17 +01001038 unsigned int cmd, unsigned long arg)
Arnd Bergmann6b960182009-11-06 23:10:54 -08001039{
1040 int err;
1041 void __user *argp = (void __user *)arg;
1042
1043 err = sock->ops->ioctl(sock, cmd, arg);
1044
1045 /*
1046 * If this ioctl is unknown try to hand it down
1047 * to the NIC driver.
1048 */
Al Viro36fd6332017-06-26 13:19:16 -04001049 if (err != -ENOIOCTLCMD)
1050 return err;
Arnd Bergmann6b960182009-11-06 23:10:54 -08001051
Al Viro36fd6332017-06-26 13:19:16 -04001052 if (cmd == SIOCGIFCONF) {
1053 struct ifconf ifc;
1054 if (copy_from_user(&ifc, argp, sizeof(struct ifconf)))
1055 return -EFAULT;
1056 rtnl_lock();
1057 err = dev_ifconf(net, &ifc, sizeof(struct ifreq));
1058 rtnl_unlock();
1059 if (!err && copy_to_user(argp, &ifc, sizeof(struct ifconf)))
1060 err = -EFAULT;
Al Viro44c02a22017-10-05 12:59:44 -04001061 } else {
1062 struct ifreq ifr;
1063 bool need_copyout;
Johannes Berg63ff03a2019-01-25 22:43:17 +01001064 if (copy_from_user(&ifr, argp, sizeof(struct ifreq)))
Al Viro44c02a22017-10-05 12:59:44 -04001065 return -EFAULT;
1066 err = dev_ioctl(net, cmd, &ifr, &need_copyout);
1067 if (!err && need_copyout)
Johannes Berg63ff03a2019-01-25 22:43:17 +01001068 if (copy_to_user(argp, &ifr, sizeof(struct ifreq)))
Al Viro44c02a22017-10-05 12:59:44 -04001069 return -EFAULT;
Al Viro36fd6332017-06-26 13:19:16 -04001070 }
Arnd Bergmann6b960182009-11-06 23:10:54 -08001071 return err;
1072}
1073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074/*
1075 * With an ioctl, arg may well be a user mode pointer, but we don't know
1076 * what to do with it - that's up to the protocol still.
1077 */
1078
Pedro Tammela8a3c2452019-03-14 10:45:23 -03001079/**
1080 * get_net_ns - increment the refcount of the network namespace
1081 * @ns: common namespace (net)
1082 *
1083 * Returns the net's common namespace.
1084 */
1085
Kirill Tkhaid8d211a2018-02-14 16:39:56 +03001086struct ns_common *get_net_ns(struct ns_common *ns)
Andrey Vaginc62cce22016-10-24 18:29:13 -07001087{
1088 return &get_net(container_of(ns, struct net, ns))->ns;
1089}
Kirill Tkhaid8d211a2018-02-14 16:39:56 +03001090EXPORT_SYMBOL_GPL(get_net_ns);
Andrey Vaginc62cce22016-10-24 18:29:13 -07001091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
1093{
1094 struct socket *sock;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001095 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 void __user *argp = (void __user *)arg;
1097 int pid, err;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001098 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Eric Dumazetb69aee02005-09-06 14:42:45 -07001100 sock = file->private_data;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001101 sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001102 net = sock_net(sk);
Al Viro44c02a22017-10-05 12:59:44 -04001103 if (unlikely(cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15))) {
1104 struct ifreq ifr;
1105 bool need_copyout;
1106 if (copy_from_user(&ifr, argp, sizeof(struct ifreq)))
1107 return -EFAULT;
1108 err = dev_ioctl(net, cmd, &ifr, &need_copyout);
1109 if (!err && need_copyout)
1110 if (copy_to_user(argp, &ifr, sizeof(struct ifreq)))
1111 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 } else
Johannes Berg3d23e342009-09-29 23:27:28 +02001113#ifdef CONFIG_WEXT_CORE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
Al Virob1b0c242017-10-01 20:13:08 -04001115 err = wext_handle_ioctl(net, cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 } else
Johannes Berg3d23e342009-09-29 23:27:28 +02001117#endif
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001118 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 case FIOSETOWN:
1120 case SIOCSPGRP:
1121 err = -EFAULT;
1122 if (get_user(pid, (int __user *)argp))
1123 break;
Jiri Slaby393cc3f2017-06-13 13:35:50 +02001124 err = f_setown(sock->file, pid, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 break;
1126 case FIOGETOWN:
1127 case SIOCGPGRP:
Eric W. Biederman609d7fa2006-10-02 02:17:15 -07001128 err = put_user(f_getown(sock->file),
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001129 (int __user *)argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 break;
1131 case SIOCGIFBR:
1132 case SIOCSIFBR:
1133 case SIOCBRADDBR:
1134 case SIOCBRDELBR:
1135 err = -ENOPKG;
1136 if (!br_ioctl_hook)
1137 request_module("bridge");
1138
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001139 mutex_lock(&br_ioctl_mutex);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001140 if (br_ioctl_hook)
Eric W. Biederman881d9662007-09-17 11:56:21 -07001141 err = br_ioctl_hook(net, cmd, argp);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001142 mutex_unlock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 break;
1144 case SIOCGIFVLAN:
1145 case SIOCSIFVLAN:
1146 err = -ENOPKG;
1147 if (!vlan_ioctl_hook)
1148 request_module("8021q");
1149
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001150 mutex_lock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 if (vlan_ioctl_hook)
Eric W. Biederman881d9662007-09-17 11:56:21 -07001152 err = vlan_ioctl_hook(net, argp);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001153 mutex_unlock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 case SIOCADDDLCI:
1156 case SIOCDELDLCI:
1157 err = -ENOPKG;
1158 if (!dlci_ioctl_hook)
1159 request_module("dlci");
1160
Pavel Emelyanov7512cbf2008-03-21 15:58:52 -07001161 mutex_lock(&dlci_ioctl_mutex);
1162 if (dlci_ioctl_hook)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 err = dlci_ioctl_hook(cmd, argp);
Pavel Emelyanov7512cbf2008-03-21 15:58:52 -07001164 mutex_unlock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 break;
Andrey Vaginc62cce22016-10-24 18:29:13 -07001166 case SIOCGSKNS:
1167 err = -EPERM;
1168 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1169 break;
1170
1171 err = open_related_ns(&net->ns, get_net_ns);
1172 break;
Arnd Bergmann0768e172019-04-17 22:56:11 +02001173 case SIOCGSTAMP_OLD:
1174 case SIOCGSTAMPNS_OLD:
Arnd Bergmannc7cbdbf2019-04-17 22:51:48 +02001175 if (!sock->ops->gettstamp) {
1176 err = -ENOIOCTLCMD;
1177 break;
1178 }
1179 err = sock->ops->gettstamp(sock, argp,
Arnd Bergmann0768e172019-04-17 22:56:11 +02001180 cmd == SIOCGSTAMP_OLD,
1181 !IS_ENABLED(CONFIG_64BIT));
Gustavo A. R. Silva60747822019-04-24 10:31:24 -05001182 break;
Arnd Bergmann0768e172019-04-17 22:56:11 +02001183 case SIOCGSTAMP_NEW:
1184 case SIOCGSTAMPNS_NEW:
1185 if (!sock->ops->gettstamp) {
1186 err = -ENOIOCTLCMD;
1187 break;
1188 }
1189 err = sock->ops->gettstamp(sock, argp,
1190 cmd == SIOCGSTAMP_NEW,
1191 false);
Arnd Bergmannc7cbdbf2019-04-17 22:51:48 +02001192 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 default:
Johannes Berg63ff03a2019-01-25 22:43:17 +01001194 err = sock_do_ioctl(net, sock, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 break;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 return err;
1198}
1199
Pedro Tammela8a3c2452019-03-14 10:45:23 -03001200/**
1201 * sock_create_lite - creates a socket
1202 * @family: protocol family (AF_INET, ...)
1203 * @type: communication type (SOCK_STREAM, ...)
1204 * @protocol: protocol (0, ...)
1205 * @res: new socket
1206 *
1207 * Creates a new socket and assigns it to @res, passing through LSM.
1208 * The new socket initialization is not complete, see kernel_accept().
1209 * Returns 0 or an error. On failure @res is set to %NULL.
1210 * This function internally uses GFP_KERNEL.
1211 */
1212
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213int sock_create_lite(int family, int type, int protocol, struct socket **res)
1214{
1215 int err;
1216 struct socket *sock = NULL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001217
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 err = security_socket_create(family, type, protocol, 1);
1219 if (err)
1220 goto out;
1221
1222 sock = sock_alloc();
1223 if (!sock) {
1224 err = -ENOMEM;
1225 goto out;
1226 }
1227
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 sock->type = type;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001229 err = security_socket_post_create(sock, family, type, protocol, 1);
1230 if (err)
1231 goto out_release;
1232
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233out:
1234 *res = sock;
1235 return err;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001236out_release:
1237 sock_release(sock);
1238 sock = NULL;
1239 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001241EXPORT_SYMBOL(sock_create_lite);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
1243/* No kernel lock held - perfect */
Al Viroade994f2017-07-03 00:01:49 -04001244static __poll_t sock_poll(struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245{
Christoph Hellwig3cafb372018-01-09 16:07:34 +01001246 struct socket *sock = file->private_data;
Christoph Hellwiga331de32018-07-30 09:42:13 +02001247 __poll_t events = poll_requested_events(wait), flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Christoph Hellwige88958e2018-06-29 15:37:24 +02001249 if (!sock->ops->poll)
1250 return 0;
Christoph Hellwigf641f13b2018-07-30 09:42:12 +02001251
Christoph Hellwiga331de32018-07-30 09:42:13 +02001252 if (sk_can_busy_loop(sock->sk)) {
1253 /* poll once if requested by the syscall */
1254 if (events & POLL_BUSY_LOOP)
1255 sk_busy_loop(sock->sk, 1);
1256
1257 /* if this socket can poll_ll, tell the system call */
1258 flag = POLL_BUSY_LOOP;
1259 }
1260
1261 return sock->ops->poll(file, sock, wait) | flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262}
1263
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001264static int sock_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265{
Eric Dumazetb69aee02005-09-06 14:42:45 -07001266 struct socket *sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
1268 return sock->ops->mmap(file, sock, vma);
1269}
1270
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001271static int sock_close(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272{
Cong Wang6d8c50d2018-06-07 13:39:49 -07001273 __sock_release(SOCKET_I(inode), inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 return 0;
1275}
1276
1277/*
1278 * Update the socket async list
1279 *
1280 * Fasync_list locking strategy.
1281 *
1282 * 1. fasync_list is modified only under process context socket lock
1283 * i.e. under semaphore.
1284 * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
Eric Dumazet989a2972010-04-14 09:55:35 +00001285 * or under socket lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 */
1287
1288static int sock_fasync(int fd, struct file *filp, int on)
1289{
Eric Dumazet989a2972010-04-14 09:55:35 +00001290 struct socket *sock = filp->private_data;
1291 struct sock *sk = sock->sk;
Eric Dumazeteaefd112011-02-18 03:26:36 +00001292 struct socket_wq *wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
Eric Dumazet989a2972010-04-14 09:55:35 +00001294 if (sk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
1297 lock_sock(sk);
Christoph Hellwige6476c22018-07-30 09:45:07 +02001298 wq = sock->wq;
Eric Dumazeteaefd112011-02-18 03:26:36 +00001299 fasync_helper(fd, filp, on, &wq->fasync_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Eric Dumazeteaefd112011-02-18 03:26:36 +00001301 if (!wq->fasync_list)
Eric Dumazet989a2972010-04-14 09:55:35 +00001302 sock_reset_flag(sk, SOCK_FASYNC);
Jonathan Corbet76398422009-02-01 14:26:59 -07001303 else
Eric Dumazetbcdce712009-10-06 17:28:29 -07001304 sock_set_flag(sk, SOCK_FASYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Eric Dumazet989a2972010-04-14 09:55:35 +00001306 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 return 0;
1308}
1309
Eric Dumazetceb5d582015-11-29 20:03:11 -08001310/* This function may be called only under rcu_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
Eric Dumazetceb5d582015-11-29 20:03:11 -08001312int sock_wake_async(struct socket_wq *wq, int how, int band)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313{
Eric Dumazetceb5d582015-11-29 20:03:11 -08001314 if (!wq || !wq->fasync_list)
1315 return -1;
Eric Dumazet43815482010-04-29 11:01:49 +00001316
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001317 switch (how) {
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001318 case SOCK_WAKE_WAITD:
Eric Dumazetceb5d582015-11-29 20:03:11 -08001319 if (test_bit(SOCKWQ_ASYNC_WAITDATA, &wq->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 break;
1321 goto call_kill;
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001322 case SOCK_WAKE_SPACE:
Eric Dumazetceb5d582015-11-29 20:03:11 -08001323 if (!test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 break;
1325 /* fall through */
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001326 case SOCK_WAKE_IO:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001327call_kill:
Eric Dumazet43815482010-04-29 11:01:49 +00001328 kill_fasync(&wq->fasync_list, SIGIO, band);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 break;
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001330 case SOCK_WAKE_URG:
Eric Dumazet43815482010-04-29 11:01:49 +00001331 kill_fasync(&wq->fasync_list, SIGURG, band);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 }
Eric Dumazetceb5d582015-11-29 20:03:11 -08001333
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 return 0;
1335}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001336EXPORT_SYMBOL(sock_wake_async);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Pedro Tammela8a3c2452019-03-14 10:45:23 -03001338/**
1339 * __sock_create - creates a socket
1340 * @net: net namespace
1341 * @family: protocol family (AF_INET, ...)
1342 * @type: communication type (SOCK_STREAM, ...)
1343 * @protocol: protocol (0, ...)
1344 * @res: new socket
1345 * @kern: boolean for kernel space sockets
1346 *
1347 * Creates a new socket and assigns it to @res, passing through LSM.
1348 * Returns 0 or an error. On failure @res is set to %NULL. @kern must
1349 * be set to true if the socket resides in kernel space.
1350 * This function internally uses GFP_KERNEL.
1351 */
1352
Pavel Emelyanov721db932010-09-29 16:06:32 +04001353int __sock_create(struct net *net, int family, int type, int protocol,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001354 struct socket **res, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355{
1356 int err;
1357 struct socket *sock;
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001358 const struct net_proto_family *pf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
1360 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001361 * Check protocol is in range
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 */
1363 if (family < 0 || family >= NPROTO)
1364 return -EAFNOSUPPORT;
1365 if (type < 0 || type >= SOCK_MAX)
1366 return -EINVAL;
1367
1368 /* Compatibility.
1369
1370 This uglymoron is moved from INET layer to here to avoid
1371 deadlock in module load.
1372 */
1373 if (family == PF_INET && type == SOCK_PACKET) {
liping.zhangf3c98692016-03-11 23:08:36 +08001374 pr_info_once("%s uses obsolete (PF_INET,SOCK_PACKET)\n",
1375 current->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 family = PF_PACKET;
1377 }
1378
1379 err = security_socket_create(family, type, protocol, kern);
1380 if (err)
1381 return err;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001382
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001383 /*
1384 * Allocate the socket and allow the family to set things up. if
1385 * the protocol is 0, the family is instructed to select an appropriate
1386 * default.
1387 */
1388 sock = sock_alloc();
1389 if (!sock) {
Joe Perchese87cc472012-05-13 21:56:26 +00001390 net_warn_ratelimited("socket: no more sockets\n");
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001391 return -ENFILE; /* Not exactly a match, but its the
1392 closest posix thing */
1393 }
1394
1395 sock->type = type;
1396
Johannes Berg95a5afc2008-10-16 15:24:51 -07001397#ifdef CONFIG_MODULES
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001398 /* Attempt to load a protocol module if the find failed.
1399 *
1400 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 * requested real, full-featured networking support upon configuration.
1402 * Otherwise module support will break!
1403 */
Eric Dumazet190683a2010-11-10 10:50:44 +00001404 if (rcu_access_pointer(net_families[family]) == NULL)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001405 request_module("net-pf-%d", family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406#endif
1407
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001408 rcu_read_lock();
1409 pf = rcu_dereference(net_families[family]);
1410 err = -EAFNOSUPPORT;
1411 if (!pf)
1412 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
1414 /*
1415 * We will call the ->create function, that possibly is in a loadable
1416 * module, so we have to bump that loadable module refcnt first.
1417 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001418 if (!try_module_get(pf->owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 goto out_release;
1420
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001421 /* Now protected by module ref count */
1422 rcu_read_unlock();
1423
Eric Paris3f378b62009-11-05 22:18:14 -08001424 err = pf->create(net, sock, protocol, kern);
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001425 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 goto out_module_put;
Frank Filza79af592005-09-27 15:23:38 -07001427
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 /*
1429 * Now to bump the refcnt of the [loadable] module that owns this
1430 * socket at sock_release time we decrement its refcnt.
1431 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001432 if (!try_module_get(sock->ops->owner))
1433 goto out_module_busy;
1434
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 /*
1436 * Now that we're done with the ->create function, the [loadable]
1437 * module can have its refcnt decremented
1438 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001439 module_put(pf->owner);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001440 err = security_socket_post_create(sock, family, type, protocol, kern);
1441 if (err)
Herbert Xu3b185522007-08-15 14:46:02 -07001442 goto out_sock_release;
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001443 *res = sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001445 return 0;
1446
1447out_module_busy:
1448 err = -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449out_module_put:
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001450 sock->ops = NULL;
1451 module_put(pf->owner);
1452out_sock_release:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 sock_release(sock);
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001454 return err;
1455
1456out_release:
1457 rcu_read_unlock();
1458 goto out_sock_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459}
Pavel Emelyanov721db932010-09-29 16:06:32 +04001460EXPORT_SYMBOL(__sock_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
Pedro Tammela8a3c2452019-03-14 10:45:23 -03001462/**
1463 * sock_create - creates a socket
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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473int sock_create(int family, int type, int protocol, struct socket **res)
1474{
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001475 return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001477EXPORT_SYMBOL(sock_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
Pedro Tammela8a3c2452019-03-14 10:45:23 -03001479/**
1480 * sock_create_kern - creates a socket (kernel space)
1481 * @net: net namespace
1482 * @family: protocol family (AF_INET, ...)
1483 * @type: communication type (SOCK_STREAM, ...)
1484 * @protocol: protocol (0, ...)
1485 * @res: new socket
1486 *
1487 * A wrapper around __sock_create().
1488 * Returns 0 or an error. This function internally uses GFP_KERNEL.
1489 */
1490
Eric W. Biedermaneeb1bd52015-05-08 21:08:05 -05001491int sock_create_kern(struct net *net, int family, int type, int protocol, struct socket **res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492{
Eric W. Biedermaneeb1bd52015-05-08 21:08:05 -05001493 return __sock_create(net, family, type, protocol, res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001495EXPORT_SYMBOL(sock_create_kern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
Dominik Brodowski9d6a15c2018-03-13 19:29:43 +01001497int __sys_socket(int family, int type, int protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498{
1499 int retval;
1500 struct socket *sock;
Ulrich Dreppera677a032008-07-23 21:29:17 -07001501 int flags;
1502
Ulrich Dreppere38b36f2008-07-23 21:29:42 -07001503 /* Check the SOCK_* constants for consistency. */
1504 BUILD_BUG_ON(SOCK_CLOEXEC != O_CLOEXEC);
1505 BUILD_BUG_ON((SOCK_MAX | SOCK_TYPE_MASK) != SOCK_TYPE_MASK);
1506 BUILD_BUG_ON(SOCK_CLOEXEC & SOCK_TYPE_MASK);
1507 BUILD_BUG_ON(SOCK_NONBLOCK & SOCK_TYPE_MASK);
1508
Ulrich Dreppera677a032008-07-23 21:29:17 -07001509 flags = type & ~SOCK_TYPE_MASK;
Ulrich Drepper77d27202008-07-23 21:29:35 -07001510 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Dreppera677a032008-07-23 21:29:17 -07001511 return -EINVAL;
1512 type &= SOCK_TYPE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001514 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1515 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1516
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 retval = sock_create(family, type, protocol, &sock);
1518 if (retval < 0)
Al Viro8e1611e2017-12-05 23:29:09 +00001519 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
Al Viro8e1611e2017-12-05 23:29:09 +00001521 return sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522}
1523
Dominik Brodowski9d6a15c2018-03-13 19:29:43 +01001524SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol)
1525{
1526 return __sys_socket(family, type, protocol);
1527}
1528
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529/*
1530 * Create a pair of connected sockets.
1531 */
1532
Dominik Brodowski6debc8d2018-03-13 19:49:23 +01001533int __sys_socketpair(int family, int type, int protocol, int __user *usockvec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534{
1535 struct socket *sock1, *sock2;
1536 int fd1, fd2, err;
Al Virodb349502007-02-07 01:48:00 -05001537 struct file *newfile1, *newfile2;
Ulrich Dreppera677a032008-07-23 21:29:17 -07001538 int flags;
1539
1540 flags = type & ~SOCK_TYPE_MASK;
Ulrich Drepper77d27202008-07-23 21:29:35 -07001541 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Dreppera677a032008-07-23 21:29:17 -07001542 return -EINVAL;
1543 type &= SOCK_TYPE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001545 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1546 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1547
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 /*
Al Viro016a2662017-12-05 23:28:38 +00001549 * reserve descriptors and make sure we won't fail
1550 * to return them to userland.
1551 */
1552 fd1 = get_unused_fd_flags(flags);
1553 if (unlikely(fd1 < 0))
1554 return fd1;
1555
1556 fd2 = get_unused_fd_flags(flags);
1557 if (unlikely(fd2 < 0)) {
1558 put_unused_fd(fd1);
1559 return fd2;
1560 }
1561
1562 err = put_user(fd1, &usockvec[0]);
1563 if (err)
1564 goto out;
1565
1566 err = put_user(fd2, &usockvec[1]);
1567 if (err)
1568 goto out;
1569
1570 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 * Obtain the first socket and check if the underlying protocol
1572 * supports the socketpair call.
1573 */
1574
1575 err = sock_create(family, type, protocol, &sock1);
Al Viro016a2662017-12-05 23:28:38 +00001576 if (unlikely(err < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 goto out;
1578
1579 err = sock_create(family, type, protocol, &sock2);
Al Viro016a2662017-12-05 23:28:38 +00001580 if (unlikely(err < 0)) {
1581 sock_release(sock1);
1582 goto out;
David S. Millerbf3c23d2007-10-29 21:54:02 -07001583 }
Yann Droneaudd73aa282013-12-09 22:42:20 +01001584
David Herrmannd47cd942018-05-04 16:28:20 +02001585 err = security_socket_socketpair(sock1, sock2);
1586 if (unlikely(err)) {
1587 sock_release(sock2);
1588 sock_release(sock1);
1589 goto out;
1590 }
1591
Al Viro016a2662017-12-05 23:28:38 +00001592 err = sock1->ops->socketpair(sock1, sock2);
1593 if (unlikely(err < 0)) {
1594 sock_release(sock2);
1595 sock_release(sock1);
1596 goto out;
Al Viro28407632012-08-17 23:54:15 -04001597 }
1598
Linus Torvaldsaab174f2012-10-02 20:25:04 -07001599 newfile1 = sock_alloc_file(sock1, flags, NULL);
Viresh Kumarb5ffe632015-08-12 15:59:47 +05301600 if (IS_ERR(newfile1)) {
Al Viro28407632012-08-17 23:54:15 -04001601 err = PTR_ERR(newfile1);
Al Viro016a2662017-12-05 23:28:38 +00001602 sock_release(sock2);
1603 goto out;
Al Viro28407632012-08-17 23:54:15 -04001604 }
1605
Linus Torvaldsaab174f2012-10-02 20:25:04 -07001606 newfile2 = sock_alloc_file(sock2, flags, NULL);
Al Viro28407632012-08-17 23:54:15 -04001607 if (IS_ERR(newfile2)) {
1608 err = PTR_ERR(newfile2);
Al Viro016a2662017-12-05 23:28:38 +00001609 fput(newfile1);
1610 goto out;
Al Virodb349502007-02-07 01:48:00 -05001611 }
1612
Al Viro157cf642008-12-14 04:57:47 -05001613 audit_fd_pair(fd1, fd2);
Yann Droneaudd73aa282013-12-09 22:42:20 +01001614
Al Virodb349502007-02-07 01:48:00 -05001615 fd_install(fd1, newfile1);
1616 fd_install(fd2, newfile2);
Yann Droneaudd73aa282013-12-09 22:42:20 +01001617 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619out:
Al Viro016a2662017-12-05 23:28:38 +00001620 put_unused_fd(fd2);
1621 put_unused_fd(fd1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 return err;
1623}
1624
Dominik Brodowski6debc8d2018-03-13 19:49:23 +01001625SYSCALL_DEFINE4(socketpair, int, family, int, type, int, protocol,
1626 int __user *, usockvec)
1627{
1628 return __sys_socketpair(family, type, protocol, usockvec);
1629}
1630
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631/*
1632 * Bind a name to a socket. Nothing much to do here since it's
1633 * the protocol's responsibility to handle the local address.
1634 *
1635 * We move the socket address to kernel space before we call
1636 * the protocol layer (having also checked the address is ok).
1637 */
1638
Dominik Brodowskia87d35d2018-03-13 19:33:09 +01001639int __sys_bind(int fd, struct sockaddr __user *umyaddr, int addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640{
1641 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001642 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001643 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001645 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001646 if (sock) {
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001647 err = move_addr_to_kernel(umyaddr, addrlen, &address);
Jakub Sitnicki068b88c2018-10-04 11:09:40 +02001648 if (!err) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001649 err = security_socket_bind(sock,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001650 (struct sockaddr *)&address,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001651 addrlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001652 if (!err)
1653 err = sock->ops->bind(sock,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001654 (struct sockaddr *)
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001655 &address, addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 }
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001657 fput_light(sock->file, fput_needed);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001658 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 return err;
1660}
1661
Dominik Brodowskia87d35d2018-03-13 19:33:09 +01001662SYSCALL_DEFINE3(bind, int, fd, struct sockaddr __user *, umyaddr, int, addrlen)
1663{
1664 return __sys_bind(fd, umyaddr, addrlen);
1665}
1666
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667/*
1668 * Perform a listen. Basically, we allow the protocol to do anything
1669 * necessary for a listen, and if that works, we mark the socket as
1670 * ready for listening.
1671 */
1672
Dominik Brodowski25e290e2018-03-13 19:36:54 +01001673int __sys_listen(int fd, int backlog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674{
1675 struct socket *sock;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001676 int err, fput_needed;
Pavel Emelyanovb8e1f9b2007-12-08 00:12:33 -08001677 int somaxconn;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001678
1679 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1680 if (sock) {
Pavel Emelyanov8efa6e92008-03-31 19:41:14 -07001681 somaxconn = sock_net(sock->sk)->core.sysctl_somaxconn;
Eric Dumazet95c96172012-04-15 05:58:06 +00001682 if ((unsigned int)backlog > somaxconn)
Pavel Emelyanovb8e1f9b2007-12-08 00:12:33 -08001683 backlog = somaxconn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
1685 err = security_socket_listen(sock, backlog);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001686 if (!err)
1687 err = sock->ops->listen(sock, backlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001689 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 }
1691 return err;
1692}
1693
Dominik Brodowski25e290e2018-03-13 19:36:54 +01001694SYSCALL_DEFINE2(listen, int, fd, int, backlog)
1695{
1696 return __sys_listen(fd, backlog);
1697}
1698
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699/*
1700 * For accept, we attempt to create a new socket, set up the link
1701 * with the client, wake up the client, then return the new
1702 * connected fd. We collect the address of the connector in kernel
1703 * space and move it to user at the very end. This is unclean because
1704 * we open the socket then return an error.
1705 *
1706 * 1003.1g adds the ability to recvmsg() to query connection pending
1707 * status to recvmsg. We need to add that support in a way thats
Geert Uytterhoevenb9030362018-03-02 14:43:23 +01001708 * clean when we restructure accept also.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 */
1710
Dominik Brodowski4541e802018-03-13 19:24:23 +01001711int __sys_accept4(int fd, struct sockaddr __user *upeer_sockaddr,
1712 int __user *upeer_addrlen, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713{
1714 struct socket *sock, *newsock;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001715 struct file *newfile;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001716 int err, len, newfd, fput_needed;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001717 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
Ulrich Drepper77d27202008-07-23 21:29:35 -07001719 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001720 return -EINVAL;
1721
1722 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1723 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1724
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001725 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 if (!sock)
1727 goto out;
1728
1729 err = -ENFILE;
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001730 newsock = sock_alloc();
1731 if (!newsock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 goto out_put;
1733
1734 newsock->type = sock->type;
1735 newsock->ops = sock->ops;
1736
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 /*
1738 * We don't need try_module_get here, as the listening socket (sock)
1739 * has the protocol module (sock->ops->owner) held.
1740 */
1741 __module_get(newsock->ops->owner);
1742
Al Viro28407632012-08-17 23:54:15 -04001743 newfd = get_unused_fd_flags(flags);
David S. Miller39d8c1b2006-03-20 17:13:49 -08001744 if (unlikely(newfd < 0)) {
1745 err = newfd;
David S. Miller9a1875e2006-04-01 12:48:36 -08001746 sock_release(newsock);
1747 goto out_put;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001748 }
Linus Torvaldsaab174f2012-10-02 20:25:04 -07001749 newfile = sock_alloc_file(newsock, flags, sock->sk->sk_prot_creator->name);
Viresh Kumarb5ffe632015-08-12 15:59:47 +05301750 if (IS_ERR(newfile)) {
Al Viro28407632012-08-17 23:54:15 -04001751 err = PTR_ERR(newfile);
1752 put_unused_fd(newfd);
Al Viro28407632012-08-17 23:54:15 -04001753 goto out_put;
1754 }
David S. Miller39d8c1b2006-03-20 17:13:49 -08001755
Frank Filza79af592005-09-27 15:23:38 -07001756 err = security_socket_accept(sock, newsock);
1757 if (err)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001758 goto out_fd;
Frank Filza79af592005-09-27 15:23:38 -07001759
David Howellscdfbabf2017-03-09 08:09:05 +00001760 err = sock->ops->accept(sock, newsock, sock->file->f_flags, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 if (err < 0)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001762 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
1764 if (upeer_sockaddr) {
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001765 len = newsock->ops->getname(newsock,
1766 (struct sockaddr *)&address, 2);
1767 if (len < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 err = -ECONNABORTED;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001769 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 }
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001771 err = move_addr_to_user(&address,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001772 len, upeer_sockaddr, upeer_addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 if (err < 0)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001774 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 }
1776
1777 /* File flags are not inherited via accept() unlike another OSes. */
1778
David S. Miller39d8c1b2006-03-20 17:13:49 -08001779 fd_install(newfd, newfile);
1780 err = newfd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001783 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784out:
1785 return err;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001786out_fd:
David S. Miller9606a212006-04-01 01:00:14 -08001787 fput(newfile);
David S. Miller39d8c1b2006-03-20 17:13:49 -08001788 put_unused_fd(newfd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 goto out_put;
1790}
1791
Dominik Brodowski4541e802018-03-13 19:24:23 +01001792SYSCALL_DEFINE4(accept4, int, fd, struct sockaddr __user *, upeer_sockaddr,
1793 int __user *, upeer_addrlen, int, flags)
1794{
1795 return __sys_accept4(fd, upeer_sockaddr, upeer_addrlen, flags);
1796}
1797
Heiko Carstens20f37032009-01-14 14:14:23 +01001798SYSCALL_DEFINE3(accept, int, fd, struct sockaddr __user *, upeer_sockaddr,
1799 int __user *, upeer_addrlen)
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001800{
Dominik Brodowski4541e802018-03-13 19:24:23 +01001801 return __sys_accept4(fd, upeer_sockaddr, upeer_addrlen, 0);
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001802}
1803
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804/*
1805 * Attempt to connect to a socket with the server address. The address
1806 * is in user space so we verify it is OK and move it to kernel space.
1807 *
1808 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
1809 * break bindings
1810 *
1811 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
1812 * other SEQPACKET protocols that take time to connect() as it doesn't
1813 * include the -EINPROGRESS status for such sockets.
1814 */
1815
Dominik Brodowski1387c2c2018-03-13 19:35:09 +01001816int __sys_connect(int fd, struct sockaddr __user *uservaddr, int addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817{
1818 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001819 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001820 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001822 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 if (!sock)
1824 goto out;
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001825 err = move_addr_to_kernel(uservaddr, addrlen, &address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 if (err < 0)
1827 goto out_put;
1828
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001829 err =
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001830 security_socket_connect(sock, (struct sockaddr *)&address, addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 if (err)
1832 goto out_put;
1833
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001834 err = sock->ops->connect(sock, (struct sockaddr *)&address, addrlen,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 sock->file->f_flags);
1836out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001837 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838out:
1839 return err;
1840}
1841
Dominik Brodowski1387c2c2018-03-13 19:35:09 +01001842SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
1843 int, addrlen)
1844{
1845 return __sys_connect(fd, uservaddr, addrlen);
1846}
1847
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848/*
1849 * Get the local address ('name') of a socket object. Move the obtained
1850 * name to user space.
1851 */
1852
Dominik Brodowski8882a102018-03-13 19:43:14 +01001853int __sys_getsockname(int fd, struct sockaddr __user *usockaddr,
1854 int __user *usockaddr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855{
1856 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001857 struct sockaddr_storage address;
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001858 int err, fput_needed;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001859
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001860 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 if (!sock)
1862 goto out;
1863
1864 err = security_socket_getsockname(sock);
1865 if (err)
1866 goto out_put;
1867
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001868 err = sock->ops->getname(sock, (struct sockaddr *)&address, 0);
1869 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 goto out_put;
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001871 /* "err" is actually length in this case */
1872 err = move_addr_to_user(&address, err, usockaddr, usockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873
1874out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001875 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876out:
1877 return err;
1878}
1879
Dominik Brodowski8882a102018-03-13 19:43:14 +01001880SYSCALL_DEFINE3(getsockname, int, fd, struct sockaddr __user *, usockaddr,
1881 int __user *, usockaddr_len)
1882{
1883 return __sys_getsockname(fd, usockaddr, usockaddr_len);
1884}
1885
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886/*
1887 * Get the remote address ('name') of a socket object. Move the obtained
1888 * name to user space.
1889 */
1890
Dominik Brodowskib21c8f82018-03-13 19:47:00 +01001891int __sys_getpeername(int fd, struct sockaddr __user *usockaddr,
1892 int __user *usockaddr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893{
1894 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001895 struct sockaddr_storage address;
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001896 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001898 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1899 if (sock != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 err = security_socket_getpeername(sock);
1901 if (err) {
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001902 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 return err;
1904 }
1905
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001906 err = sock->ops->getname(sock, (struct sockaddr *)&address, 1);
1907 if (err >= 0)
1908 /* "err" is actually length in this case */
1909 err = move_addr_to_user(&address, err, usockaddr,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001910 usockaddr_len);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001911 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 }
1913 return err;
1914}
1915
Dominik Brodowskib21c8f82018-03-13 19:47:00 +01001916SYSCALL_DEFINE3(getpeername, int, fd, struct sockaddr __user *, usockaddr,
1917 int __user *, usockaddr_len)
1918{
1919 return __sys_getpeername(fd, usockaddr, usockaddr_len);
1920}
1921
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922/*
1923 * Send a datagram to a given address. We move the address into kernel
1924 * space and check the user space data area is readable before invoking
1925 * the protocol.
1926 */
Dominik Brodowski211b6342018-03-13 19:18:52 +01001927int __sys_sendto(int fd, void __user *buff, size_t len, unsigned int flags,
1928 struct sockaddr __user *addr, int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929{
1930 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001931 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 int err;
1933 struct msghdr msg;
1934 struct iovec iov;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001935 int fput_needed;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001936
Al Viro602bd0e2015-03-21 19:12:32 -04001937 err = import_single_range(WRITE, buff, len, &iov, &msg.msg_iter);
1938 if (unlikely(err))
1939 return err;
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001940 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1941 if (!sock)
David S. Miller4387ff72007-02-08 15:06:08 -08001942 goto out;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001943
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001944 msg.msg_name = NULL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001945 msg.msg_control = NULL;
1946 msg.msg_controllen = 0;
1947 msg.msg_namelen = 0;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001948 if (addr) {
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001949 err = move_addr_to_kernel(addr, addr_len, &address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 if (err < 0)
1951 goto out_put;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001952 msg.msg_name = (struct sockaddr *)&address;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001953 msg.msg_namelen = addr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 }
1955 if (sock->file->f_flags & O_NONBLOCK)
1956 flags |= MSG_DONTWAIT;
1957 msg.msg_flags = flags;
Al Virod8725c82014-12-11 00:02:50 -05001958 err = sock_sendmsg(sock, &msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001960out_put:
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001961 fput_light(sock->file, fput_needed);
David S. Miller4387ff72007-02-08 15:06:08 -08001962out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 return err;
1964}
1965
Dominik Brodowski211b6342018-03-13 19:18:52 +01001966SYSCALL_DEFINE6(sendto, int, fd, void __user *, buff, size_t, len,
1967 unsigned int, flags, struct sockaddr __user *, addr,
1968 int, addr_len)
1969{
1970 return __sys_sendto(fd, buff, len, flags, addr, addr_len);
1971}
1972
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001974 * Send a datagram down a socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 */
1976
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001977SYSCALL_DEFINE4(send, int, fd, void __user *, buff, size_t, len,
Eric Dumazet95c96172012-04-15 05:58:06 +00001978 unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979{
Dominik Brodowski211b6342018-03-13 19:18:52 +01001980 return __sys_sendto(fd, buff, len, flags, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981}
1982
1983/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001984 * Receive a frame from the socket and optionally record the address of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 * sender. We verify the buffers are writable and if needed move the
1986 * sender address from kernel to user space.
1987 */
Dominik Brodowski7a09e1e2018-03-13 19:10:06 +01001988int __sys_recvfrom(int fd, void __user *ubuf, size_t size, unsigned int flags,
1989 struct sockaddr __user *addr, int __user *addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990{
1991 struct socket *sock;
1992 struct iovec iov;
1993 struct msghdr msg;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001994 struct sockaddr_storage address;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001995 int err, err2;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001996 int fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
Al Viro602bd0e2015-03-21 19:12:32 -04001998 err = import_single_range(READ, ubuf, size, &iov, &msg.msg_iter);
1999 if (unlikely(err))
2000 return err;
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08002001 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 if (!sock)
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08002003 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002005 msg.msg_control = NULL;
2006 msg.msg_controllen = 0;
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01002007 /* Save some cycles and don't copy the address if not needed */
2008 msg.msg_name = addr ? (struct sockaddr *)&address : NULL;
2009 /* We assume all kernel code knows the size of sockaddr_storage */
2010 msg.msg_namelen = 0;
tadeusz.struk@intel.com130ed5d2015-12-15 10:46:17 -08002011 msg.msg_iocb = NULL;
Alexander Potapenko9f138fa2017-03-08 18:08:16 +01002012 msg.msg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 if (sock->file->f_flags & O_NONBLOCK)
2014 flags |= MSG_DONTWAIT;
Al Viro2da62902015-03-14 21:13:46 -04002015 err = sock_recvmsg(sock, &msg, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002017 if (err >= 0 && addr != NULL) {
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00002018 err2 = move_addr_to_user(&address,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07002019 msg.msg_namelen, addr, addr_len);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002020 if (err2 < 0)
2021 err = err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 }
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08002023
2024 fput_light(sock->file, fput_needed);
David S. Miller4387ff72007-02-08 15:06:08 -08002025out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 return err;
2027}
2028
Dominik Brodowski7a09e1e2018-03-13 19:10:06 +01002029SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, ubuf, size_t, size,
2030 unsigned int, flags, struct sockaddr __user *, addr,
2031 int __user *, addr_len)
2032{
2033 return __sys_recvfrom(fd, ubuf, size, flags, addr, addr_len);
2034}
2035
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002037 * Receive a datagram from a socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 */
2039
Jan Glauberb7c0ddf2014-04-16 09:32:48 +02002040SYSCALL_DEFINE4(recv, int, fd, void __user *, ubuf, size_t, size,
2041 unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042{
Dominik Brodowski7a09e1e2018-03-13 19:10:06 +01002043 return __sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044}
2045
2046/*
2047 * Set a socket option. Because we don't know the option lengths we have
2048 * to pass the user mode parameter for the protocols to sort out.
2049 */
2050
Dominik Brodowskicc36dca2018-03-13 20:10:59 +01002051static int __sys_setsockopt(int fd, int level, int optname,
2052 char __user *optval, int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002054 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 struct socket *sock;
2056
2057 if (optlen < 0)
2058 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002059
2060 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2061 if (sock != NULL) {
2062 err = security_socket_setsockopt(sock, level, optname);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002063 if (err)
2064 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
2066 if (level == SOL_SOCKET)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002067 err =
2068 sock_setsockopt(sock, level, optname, optval,
2069 optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 else
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002071 err =
2072 sock->ops->setsockopt(sock, level, optname, optval,
2073 optlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002074out_put:
2075 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 }
2077 return err;
2078}
2079
Dominik Brodowskicc36dca2018-03-13 20:10:59 +01002080SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname,
2081 char __user *, optval, int, optlen)
2082{
2083 return __sys_setsockopt(fd, level, optname, optval, optlen);
2084}
2085
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086/*
2087 * Get a socket option. Because we don't know the option lengths we have
2088 * to pass a user mode parameter for the protocols to sort out.
2089 */
2090
Dominik Brodowski13a2d702018-03-13 20:15:04 +01002091static int __sys_getsockopt(int fd, int level, int optname,
2092 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002094 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 struct socket *sock;
2096
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002097 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2098 if (sock != NULL) {
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002099 err = security_socket_getsockopt(sock, level, optname);
2100 if (err)
2101 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
2103 if (level == SOL_SOCKET)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002104 err =
2105 sock_getsockopt(sock, level, optname, optval,
2106 optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 else
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002108 err =
2109 sock->ops->getsockopt(sock, level, optname, optval,
2110 optlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002111out_put:
2112 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 }
2114 return err;
2115}
2116
Dominik Brodowski13a2d702018-03-13 20:15:04 +01002117SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname,
2118 char __user *, optval, int __user *, optlen)
2119{
2120 return __sys_getsockopt(fd, level, optname, optval, optlen);
2121}
2122
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123/*
2124 * Shutdown a socket.
2125 */
2126
Dominik Brodowski005a1ae2018-03-13 20:07:05 +01002127int __sys_shutdown(int fd, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002129 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 struct socket *sock;
2131
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002132 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2133 if (sock != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 err = security_socket_shutdown(sock, how);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002135 if (!err)
2136 err = sock->ops->shutdown(sock, how);
2137 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 }
2139 return err;
2140}
2141
Dominik Brodowski005a1ae2018-03-13 20:07:05 +01002142SYSCALL_DEFINE2(shutdown, int, fd, int, how)
2143{
2144 return __sys_shutdown(fd, how);
2145}
2146
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002147/* A couple of helpful macros for getting the address of the 32/64 bit
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 * fields which are the same type (int / unsigned) on our platforms.
2149 */
2150#define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
2151#define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
2152#define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
2153
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002154struct used_address {
2155 struct sockaddr_storage name;
2156 unsigned int name_len;
2157};
2158
Al Viroda184282015-03-21 19:29:06 -04002159static int copy_msghdr_from_user(struct msghdr *kmsg,
2160 struct user_msghdr __user *umsg,
2161 struct sockaddr __user **save_addr,
2162 struct iovec **iov)
Dan Carpenter1661bf32013-10-03 00:27:20 +03002163{
Al Viroffb07552017-06-27 19:32:04 -04002164 struct user_msghdr msg;
Al Viro08adb7d2014-11-10 20:23:13 -05002165 ssize_t err;
2166
Al Viroffb07552017-06-27 19:32:04 -04002167 if (copy_from_user(&msg, umsg, sizeof(*umsg)))
Dan Carpenter1661bf32013-10-03 00:27:20 +03002168 return -EFAULT;
Matthew Leachdbb490b2014-03-11 11:58:27 +00002169
Paolo Abeni864d9662017-07-21 18:49:45 +02002170 kmsg->msg_control = (void __force *)msg.msg_control;
Al Viroffb07552017-06-27 19:32:04 -04002171 kmsg->msg_controllen = msg.msg_controllen;
2172 kmsg->msg_flags = msg.msg_flags;
2173
2174 kmsg->msg_namelen = msg.msg_namelen;
2175 if (!msg.msg_name)
Ani Sinha6a2a2b32014-09-08 14:49:59 -07002176 kmsg->msg_namelen = 0;
2177
Matthew Leachdbb490b2014-03-11 11:58:27 +00002178 if (kmsg->msg_namelen < 0)
2179 return -EINVAL;
2180
Dan Carpenter1661bf32013-10-03 00:27:20 +03002181 if (kmsg->msg_namelen > sizeof(struct sockaddr_storage))
Dan Carpenterdb31c552013-11-27 15:40:21 +03002182 kmsg->msg_namelen = sizeof(struct sockaddr_storage);
Al Viro08adb7d2014-11-10 20:23:13 -05002183
2184 if (save_addr)
Al Viroffb07552017-06-27 19:32:04 -04002185 *save_addr = msg.msg_name;
Al Viro08adb7d2014-11-10 20:23:13 -05002186
Al Viroffb07552017-06-27 19:32:04 -04002187 if (msg.msg_name && kmsg->msg_namelen) {
Al Viro08adb7d2014-11-10 20:23:13 -05002188 if (!save_addr) {
Paolo Abeni864d9662017-07-21 18:49:45 +02002189 err = move_addr_to_kernel(msg.msg_name,
2190 kmsg->msg_namelen,
Al Viro08adb7d2014-11-10 20:23:13 -05002191 kmsg->msg_name);
2192 if (err < 0)
2193 return err;
2194 }
2195 } else {
2196 kmsg->msg_name = NULL;
2197 kmsg->msg_namelen = 0;
2198 }
2199
Al Viroffb07552017-06-27 19:32:04 -04002200 if (msg.msg_iovlen > UIO_MAXIOV)
Al Viro08adb7d2014-11-10 20:23:13 -05002201 return -EMSGSIZE;
2202
tadeusz.struk@intel.com0345f932015-03-19 12:31:25 -07002203 kmsg->msg_iocb = NULL;
2204
Al Viroffb07552017-06-27 19:32:04 -04002205 return import_iovec(save_addr ? READ : WRITE,
2206 msg.msg_iov, msg.msg_iovlen,
Al Viroda184282015-03-21 19:29:06 -04002207 UIO_FASTIOV, iov, &kmsg->msg_iter);
Dan Carpenter1661bf32013-10-03 00:27:20 +03002208}
2209
Al Viro666547f2014-04-06 14:03:05 -04002210static int ___sys_sendmsg(struct socket *sock, struct user_msghdr __user *msg,
Eric Dumazet95c96172012-04-15 05:58:06 +00002211 struct msghdr *msg_sys, unsigned int flags,
Tom Herbert28a94d82016-03-07 14:11:02 -08002212 struct used_address *used_address,
2213 unsigned int allowed_msghdr_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214{
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002215 struct compat_msghdr __user *msg_compat =
2216 (struct compat_msghdr __user *)msg;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07002217 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
Alex Williamsonb9d717a2005-09-26 14:28:02 -07002219 unsigned char ctl[sizeof(struct cmsghdr) + 20]
Amit Kushwaha846cc122016-12-08 18:21:53 +05302220 __aligned(sizeof(__kernel_size_t));
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002221 /* 20 is size of ipv6_pktinfo */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 unsigned char *ctl_buf = ctl;
Al Virod8725c82014-12-11 00:02:50 -05002223 int ctl_len;
Al Viro08adb7d2014-11-10 20:23:13 -05002224 ssize_t err;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002225
Al Viro08adb7d2014-11-10 20:23:13 -05002226 msg_sys->msg_name = &address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227
Al Viro08449322014-11-09 22:33:45 -05002228 if (MSG_CMSG_COMPAT & flags)
Al Viro08adb7d2014-11-10 20:23:13 -05002229 err = get_compat_msghdr(msg_sys, msg_compat, NULL, &iov);
Al Viro08449322014-11-09 22:33:45 -05002230 else
Al Viro08adb7d2014-11-10 20:23:13 -05002231 err = copy_msghdr_from_user(msg_sys, msg, NULL, &iov);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002232 if (err < 0)
Al Viroda184282015-03-21 19:29:06 -04002233 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234
2235 err = -ENOBUFS;
2236
Anton Blanchard228e5482011-05-02 20:21:35 +00002237 if (msg_sys->msg_controllen > INT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 goto out_freeiov;
Tom Herbert28a94d82016-03-07 14:11:02 -08002239 flags |= (msg_sys->msg_flags & allowed_msghdr_flags);
Anton Blanchard228e5482011-05-02 20:21:35 +00002240 ctl_len = msg_sys->msg_controllen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002242 err =
Anton Blanchard228e5482011-05-02 20:21:35 +00002243 cmsghdr_from_user_compat_to_kern(msg_sys, sock->sk, ctl,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002244 sizeof(ctl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 if (err)
2246 goto out_freeiov;
Anton Blanchard228e5482011-05-02 20:21:35 +00002247 ctl_buf = msg_sys->msg_control;
2248 ctl_len = msg_sys->msg_controllen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 } else if (ctl_len) {
David S. Millerac4340f2017-01-04 13:24:19 -05002250 BUILD_BUG_ON(sizeof(struct cmsghdr) !=
2251 CMSG_ALIGN(sizeof(struct cmsghdr)));
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002252 if (ctl_len > sizeof(ctl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002254 if (ctl_buf == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 goto out_freeiov;
2256 }
2257 err = -EFAULT;
2258 /*
Anton Blanchard228e5482011-05-02 20:21:35 +00002259 * Careful! Before this, msg_sys->msg_control contains a user pointer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
2261 * checking falls down on this.
2262 */
Namhyung Kimfb8621b2010-09-07 03:55:00 +00002263 if (copy_from_user(ctl_buf,
Anton Blanchard228e5482011-05-02 20:21:35 +00002264 (void __user __force *)msg_sys->msg_control,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002265 ctl_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 goto out_freectl;
Anton Blanchard228e5482011-05-02 20:21:35 +00002267 msg_sys->msg_control = ctl_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 }
Anton Blanchard228e5482011-05-02 20:21:35 +00002269 msg_sys->msg_flags = flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270
2271 if (sock->file->f_flags & O_NONBLOCK)
Anton Blanchard228e5482011-05-02 20:21:35 +00002272 msg_sys->msg_flags |= MSG_DONTWAIT;
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002273 /*
2274 * If this is sendmmsg() and current destination address is same as
2275 * previously succeeded address, omit asking LSM's decision.
2276 * used_address->name_len is initialized to UINT_MAX so that the first
2277 * destination address never matches.
2278 */
Mathieu Desnoyersbc909d92011-08-24 19:45:03 -07002279 if (used_address && msg_sys->msg_name &&
2280 used_address->name_len == msg_sys->msg_namelen &&
2281 !memcmp(&used_address->name, msg_sys->msg_name,
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002282 used_address->name_len)) {
Al Virod8725c82014-12-11 00:02:50 -05002283 err = sock_sendmsg_nosec(sock, msg_sys);
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002284 goto out_freectl;
2285 }
Al Virod8725c82014-12-11 00:02:50 -05002286 err = sock_sendmsg(sock, msg_sys);
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002287 /*
2288 * If this is sendmmsg() and sending to current destination address was
2289 * successful, remember it.
2290 */
2291 if (used_address && err >= 0) {
2292 used_address->name_len = msg_sys->msg_namelen;
Mathieu Desnoyersbc909d92011-08-24 19:45:03 -07002293 if (msg_sys->msg_name)
2294 memcpy(&used_address->name, msg_sys->msg_name,
2295 used_address->name_len);
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
2298out_freectl:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002299 if (ctl_buf != ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 sock_kfree_s(sock->sk, ctl_buf, ctl_len);
2301out_freeiov:
Al Viroda184282015-03-21 19:29:06 -04002302 kfree(iov);
Anton Blanchard228e5482011-05-02 20:21:35 +00002303 return err;
2304}
2305
2306/*
2307 * BSD sendmsg interface
2308 */
2309
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002310long __sys_sendmsg(int fd, struct user_msghdr __user *msg, unsigned int flags,
2311 bool forbid_cmsg_compat)
Anton Blanchard228e5482011-05-02 20:21:35 +00002312{
2313 int fput_needed, err;
2314 struct msghdr msg_sys;
Andy Lutomirski1be374a2013-05-22 14:07:44 -07002315 struct socket *sock;
Anton Blanchard228e5482011-05-02 20:21:35 +00002316
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002317 if (forbid_cmsg_compat && (flags & MSG_CMSG_COMPAT))
2318 return -EINVAL;
2319
Andy Lutomirski1be374a2013-05-22 14:07:44 -07002320 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Anton Blanchard228e5482011-05-02 20:21:35 +00002321 if (!sock)
2322 goto out;
2323
Tom Herbert28a94d82016-03-07 14:11:02 -08002324 err = ___sys_sendmsg(sock, msg, &msg_sys, flags, NULL, 0);
Anton Blanchard228e5482011-05-02 20:21:35 +00002325
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002326 fput_light(sock->file, fput_needed);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002327out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 return err;
2329}
2330
Al Viro666547f2014-04-06 14:03:05 -04002331SYSCALL_DEFINE3(sendmsg, int, fd, struct user_msghdr __user *, msg, unsigned int, flags)
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002332{
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002333 return __sys_sendmsg(fd, msg, flags, true);
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002334}
2335
Anton Blanchard228e5482011-05-02 20:21:35 +00002336/*
2337 * Linux sendmmsg interface
2338 */
2339
2340int __sys_sendmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002341 unsigned int flags, bool forbid_cmsg_compat)
Anton Blanchard228e5482011-05-02 20:21:35 +00002342{
2343 int fput_needed, err, datagrams;
2344 struct socket *sock;
2345 struct mmsghdr __user *entry;
2346 struct compat_mmsghdr __user *compat_entry;
2347 struct msghdr msg_sys;
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002348 struct used_address used_address;
Tom Herbertf0922762016-03-07 14:11:03 -08002349 unsigned int oflags = flags;
Anton Blanchard228e5482011-05-02 20:21:35 +00002350
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002351 if (forbid_cmsg_compat && (flags & MSG_CMSG_COMPAT))
2352 return -EINVAL;
2353
Anton Blanchard98382f42011-08-04 14:07:39 +00002354 if (vlen > UIO_MAXIOV)
2355 vlen = UIO_MAXIOV;
Anton Blanchard228e5482011-05-02 20:21:35 +00002356
2357 datagrams = 0;
2358
2359 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2360 if (!sock)
2361 return err;
2362
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002363 used_address.name_len = UINT_MAX;
Anton Blanchard228e5482011-05-02 20:21:35 +00002364 entry = mmsg;
2365 compat_entry = (struct compat_mmsghdr __user *)mmsg;
Anton Blanchard728ffb82011-08-04 14:07:38 +00002366 err = 0;
Tom Herbertf0922762016-03-07 14:11:03 -08002367 flags |= MSG_BATCH;
Anton Blanchard228e5482011-05-02 20:21:35 +00002368
2369 while (datagrams < vlen) {
Tom Herbertf0922762016-03-07 14:11:03 -08002370 if (datagrams == vlen - 1)
2371 flags = oflags;
2372
Anton Blanchard228e5482011-05-02 20:21:35 +00002373 if (MSG_CMSG_COMPAT & flags) {
Al Viro666547f2014-04-06 14:03:05 -04002374 err = ___sys_sendmsg(sock, (struct user_msghdr __user *)compat_entry,
Tom Herbert28a94d82016-03-07 14:11:02 -08002375 &msg_sys, flags, &used_address, MSG_EOR);
Anton Blanchard228e5482011-05-02 20:21:35 +00002376 if (err < 0)
2377 break;
2378 err = __put_user(err, &compat_entry->msg_len);
2379 ++compat_entry;
2380 } else {
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002381 err = ___sys_sendmsg(sock,
Al Viro666547f2014-04-06 14:03:05 -04002382 (struct user_msghdr __user *)entry,
Tom Herbert28a94d82016-03-07 14:11:02 -08002383 &msg_sys, flags, &used_address, MSG_EOR);
Anton Blanchard228e5482011-05-02 20:21:35 +00002384 if (err < 0)
2385 break;
2386 err = put_user(err, &entry->msg_len);
2387 ++entry;
2388 }
2389
2390 if (err)
2391 break;
2392 ++datagrams;
Soheil Hassas Yeganeh30238982016-11-04 15:36:49 -04002393 if (msg_data_left(&msg_sys))
2394 break;
Eric Dumazeta78cb842016-01-08 08:37:20 -08002395 cond_resched();
Anton Blanchard228e5482011-05-02 20:21:35 +00002396 }
2397
Anton Blanchard228e5482011-05-02 20:21:35 +00002398 fput_light(sock->file, fput_needed);
2399
Anton Blanchard728ffb82011-08-04 14:07:38 +00002400 /* We only return an error if no datagrams were able to be sent */
2401 if (datagrams != 0)
Anton Blanchard228e5482011-05-02 20:21:35 +00002402 return datagrams;
2403
Anton Blanchard228e5482011-05-02 20:21:35 +00002404 return err;
2405}
2406
2407SYSCALL_DEFINE4(sendmmsg, int, fd, struct mmsghdr __user *, mmsg,
2408 unsigned int, vlen, unsigned int, flags)
2409{
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002410 return __sys_sendmmsg(fd, mmsg, vlen, flags, true);
Anton Blanchard228e5482011-05-02 20:21:35 +00002411}
2412
Al Viro666547f2014-04-06 14:03:05 -04002413static int ___sys_recvmsg(struct socket *sock, struct user_msghdr __user *msg,
Eric Dumazet95c96172012-04-15 05:58:06 +00002414 struct msghdr *msg_sys, unsigned int flags, int nosec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415{
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002416 struct compat_msghdr __user *msg_compat =
2417 (struct compat_msghdr __user *)msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 struct iovec iovstack[UIO_FASTIOV];
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002419 struct iovec *iov = iovstack;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 unsigned long cmsg_ptr;
Al Viro2da62902015-03-14 21:13:46 -04002421 int len;
Al Viro08adb7d2014-11-10 20:23:13 -05002422 ssize_t err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423
2424 /* kernel mode address */
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07002425 struct sockaddr_storage addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426
2427 /* user mode address pointers */
2428 struct sockaddr __user *uaddr;
Al Viro08adb7d2014-11-10 20:23:13 -05002429 int __user *uaddr_len = COMPAT_NAMELEN(msg);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002430
Al Viro08adb7d2014-11-10 20:23:13 -05002431 msg_sys->msg_name = &addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01002433 if (MSG_CMSG_COMPAT & flags)
Al Viro08adb7d2014-11-10 20:23:13 -05002434 err = get_compat_msghdr(msg_sys, msg_compat, &uaddr, &iov);
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01002435 else
Al Viro08adb7d2014-11-10 20:23:13 -05002436 err = copy_msghdr_from_user(msg_sys, msg, &uaddr, &iov);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 if (err < 0)
Al Viroda184282015-03-21 19:29:06 -04002438 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002440 cmsg_ptr = (unsigned long)msg_sys->msg_control;
2441 msg_sys->msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002442
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01002443 /* We assume all kernel code knows the size of sockaddr_storage */
2444 msg_sys->msg_namelen = 0;
2445
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 if (sock->file->f_flags & O_NONBLOCK)
2447 flags |= MSG_DONTWAIT;
Al Viro2da62902015-03-14 21:13:46 -04002448 err = (nosec ? sock_recvmsg_nosec : sock_recvmsg)(sock, msg_sys, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 if (err < 0)
2450 goto out_freeiov;
2451 len = err;
2452
2453 if (uaddr != NULL) {
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00002454 err = move_addr_to_user(&addr,
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002455 msg_sys->msg_namelen, uaddr,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002456 uaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 if (err < 0)
2458 goto out_freeiov;
2459 }
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002460 err = __put_user((msg_sys->msg_flags & ~MSG_CMSG_COMPAT),
David S. Miller37f7f422005-09-16 16:51:01 -07002461 COMPAT_FLAGS(msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 if (err)
2463 goto out_freeiov;
2464 if (MSG_CMSG_COMPAT & flags)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002465 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 &msg_compat->msg_controllen);
2467 else
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002468 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 &msg->msg_controllen);
2470 if (err)
2471 goto out_freeiov;
2472 err = len;
2473
2474out_freeiov:
Al Viroda184282015-03-21 19:29:06 -04002475 kfree(iov);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002476 return err;
2477}
2478
2479/*
2480 * BSD recvmsg interface
2481 */
2482
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002483long __sys_recvmsg(int fd, struct user_msghdr __user *msg, unsigned int flags,
2484 bool forbid_cmsg_compat)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002485{
2486 int fput_needed, err;
2487 struct msghdr msg_sys;
Andy Lutomirski1be374a2013-05-22 14:07:44 -07002488 struct socket *sock;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002489
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002490 if (forbid_cmsg_compat && (flags & MSG_CMSG_COMPAT))
2491 return -EINVAL;
2492
Andy Lutomirski1be374a2013-05-22 14:07:44 -07002493 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002494 if (!sock)
2495 goto out;
2496
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002497 err = ___sys_recvmsg(sock, msg, &msg_sys, flags, 0);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002498
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002499 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500out:
2501 return err;
2502}
2503
Al Viro666547f2014-04-06 14:03:05 -04002504SYSCALL_DEFINE3(recvmsg, int, fd, struct user_msghdr __user *, msg,
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002505 unsigned int, flags)
2506{
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002507 return __sys_recvmsg(fd, msg, flags, true);
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002508}
2509
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002510/*
2511 * Linux recvmmsg interface
2512 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513
Arnd Bergmanne11d4282018-04-18 13:43:52 +02002514static int do_recvmmsg(int fd, struct mmsghdr __user *mmsg,
2515 unsigned int vlen, unsigned int flags,
2516 struct timespec64 *timeout)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002517{
2518 int fput_needed, err, datagrams;
2519 struct socket *sock;
2520 struct mmsghdr __user *entry;
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002521 struct compat_mmsghdr __user *compat_entry;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002522 struct msghdr msg_sys;
Deepa Dinamani766b9f92016-05-19 17:09:05 -07002523 struct timespec64 end_time;
2524 struct timespec64 timeout64;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002525
2526 if (timeout &&
2527 poll_select_set_timeout(&end_time, timeout->tv_sec,
2528 timeout->tv_nsec))
2529 return -EINVAL;
2530
2531 datagrams = 0;
2532
2533 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2534 if (!sock)
2535 return err;
2536
Soheil Hassas Yeganeh7797dc42018-02-27 18:22:40 -05002537 if (likely(!(flags & MSG_ERRQUEUE))) {
2538 err = sock_error(sock->sk);
2539 if (err) {
2540 datagrams = err;
2541 goto out_put;
2542 }
Maxime Jayate623a9e2017-02-21 18:35:51 +01002543 }
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002544
2545 entry = mmsg;
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002546 compat_entry = (struct compat_mmsghdr __user *)mmsg;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002547
2548 while (datagrams < vlen) {
2549 /*
2550 * No need to ask LSM for more than the first datagram.
2551 */
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002552 if (MSG_CMSG_COMPAT & flags) {
Al Viro666547f2014-04-06 14:03:05 -04002553 err = ___sys_recvmsg(sock, (struct user_msghdr __user *)compat_entry,
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002554 &msg_sys, flags & ~MSG_WAITFORONE,
2555 datagrams);
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002556 if (err < 0)
2557 break;
2558 err = __put_user(err, &compat_entry->msg_len);
2559 ++compat_entry;
2560 } else {
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002561 err = ___sys_recvmsg(sock,
Al Viro666547f2014-04-06 14:03:05 -04002562 (struct user_msghdr __user *)entry,
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002563 &msg_sys, flags & ~MSG_WAITFORONE,
2564 datagrams);
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002565 if (err < 0)
2566 break;
2567 err = put_user(err, &entry->msg_len);
2568 ++entry;
2569 }
2570
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002571 if (err)
2572 break;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002573 ++datagrams;
2574
Brandon L Black71c5c1592010-03-26 16:18:03 +00002575 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
2576 if (flags & MSG_WAITFORONE)
2577 flags |= MSG_DONTWAIT;
2578
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002579 if (timeout) {
Deepa Dinamani766b9f92016-05-19 17:09:05 -07002580 ktime_get_ts64(&timeout64);
Arnd Bergmannc2e6c852018-04-18 13:42:25 +02002581 *timeout = timespec64_sub(end_time, timeout64);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002582 if (timeout->tv_sec < 0) {
2583 timeout->tv_sec = timeout->tv_nsec = 0;
2584 break;
2585 }
2586
2587 /* Timeout, return less than vlen datagrams */
2588 if (timeout->tv_nsec == 0 && timeout->tv_sec == 0)
2589 break;
2590 }
2591
2592 /* Out of band data, return right away */
2593 if (msg_sys.msg_flags & MSG_OOB)
2594 break;
Eric Dumazeta78cb842016-01-08 08:37:20 -08002595 cond_resched();
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002596 }
2597
Arnaldo Carvalho de Melo34b88a62016-03-14 09:56:35 -03002598 if (err == 0)
2599 goto out_put;
2600
2601 if (datagrams == 0) {
2602 datagrams = err;
2603 goto out_put;
2604 }
2605
2606 /*
2607 * We may return less entries than requested (vlen) if the
2608 * sock is non block and there aren't enough datagrams...
2609 */
2610 if (err != -EAGAIN) {
2611 /*
2612 * ... or if recvmsg returns an error after we
2613 * received some datagrams, where we record the
2614 * error to return on the next call or if the
2615 * app asks about it using getsockopt(SO_ERROR).
2616 */
2617 sock->sk->sk_err = -err;
2618 }
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002619out_put:
2620 fput_light(sock->file, fput_needed);
2621
Arnaldo Carvalho de Melo34b88a62016-03-14 09:56:35 -03002622 return datagrams;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002623}
2624
Arnd Bergmanne11d4282018-04-18 13:43:52 +02002625int __sys_recvmmsg(int fd, struct mmsghdr __user *mmsg,
2626 unsigned int vlen, unsigned int flags,
2627 struct __kernel_timespec __user *timeout,
2628 struct old_timespec32 __user *timeout32)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002629{
2630 int datagrams;
Arnd Bergmannc2e6c852018-04-18 13:42:25 +02002631 struct timespec64 timeout_sys;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002632
Arnd Bergmanne11d4282018-04-18 13:43:52 +02002633 if (timeout && get_timespec64(&timeout_sys, timeout))
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002634 return -EFAULT;
2635
Arnd Bergmanne11d4282018-04-18 13:43:52 +02002636 if (timeout32 && get_old_timespec32(&timeout_sys, timeout32))
2637 return -EFAULT;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002638
Arnd Bergmanne11d4282018-04-18 13:43:52 +02002639 if (!timeout && !timeout32)
2640 return do_recvmmsg(fd, mmsg, vlen, flags, NULL);
2641
2642 datagrams = do_recvmmsg(fd, mmsg, vlen, flags, &timeout_sys);
2643
2644 if (datagrams <= 0)
2645 return datagrams;
2646
2647 if (timeout && put_timespec64(&timeout_sys, timeout))
2648 datagrams = -EFAULT;
2649
2650 if (timeout32 && put_old_timespec32(&timeout_sys, timeout32))
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002651 datagrams = -EFAULT;
2652
2653 return datagrams;
2654}
2655
Dominik Brodowski1255e262018-03-13 20:44:21 +01002656SYSCALL_DEFINE5(recvmmsg, int, fd, struct mmsghdr __user *, mmsg,
2657 unsigned int, vlen, unsigned int, flags,
Arnd Bergmannc2e6c852018-04-18 13:42:25 +02002658 struct __kernel_timespec __user *, timeout)
Dominik Brodowski1255e262018-03-13 20:44:21 +01002659{
Arnd Bergmanne11d4282018-04-18 13:43:52 +02002660 if (flags & MSG_CMSG_COMPAT)
2661 return -EINVAL;
2662
2663 return __sys_recvmmsg(fd, mmsg, vlen, flags, timeout, NULL);
Dominik Brodowski1255e262018-03-13 20:44:21 +01002664}
2665
Arnd Bergmanne11d4282018-04-18 13:43:52 +02002666#ifdef CONFIG_COMPAT_32BIT_TIME
2667SYSCALL_DEFINE5(recvmmsg_time32, int, fd, struct mmsghdr __user *, mmsg,
2668 unsigned int, vlen, unsigned int, flags,
2669 struct old_timespec32 __user *, timeout)
2670{
2671 if (flags & MSG_CMSG_COMPAT)
2672 return -EINVAL;
2673
2674 return __sys_recvmmsg(fd, mmsg, vlen, flags, NULL, timeout);
2675}
2676#endif
2677
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002678#ifdef __ARCH_WANT_SYS_SOCKETCALL
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679/* Argument list sizes for sys_socketcall */
2680#define AL(x) ((x) * sizeof(unsigned long))
Anton Blanchard228e5482011-05-02 20:21:35 +00002681static const unsigned char nargs[21] = {
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002682 AL(0), AL(3), AL(3), AL(3), AL(2), AL(3),
2683 AL(3), AL(3), AL(4), AL(4), AL(4), AL(6),
2684 AL(6), AL(2), AL(5), AL(5), AL(3), AL(3),
Anton Blanchard228e5482011-05-02 20:21:35 +00002685 AL(4), AL(5), AL(4)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002686};
2687
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688#undef AL
2689
2690/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002691 * System call vectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 *
2693 * Argument checking cleaned up. Saved 20% in size.
2694 * This function doesn't need to set the kernel lock because
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002695 * it is set by the callees.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 */
2697
Heiko Carstens3e0fa652009-01-14 14:14:24 +01002698SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699{
Chen Gang2950fa92013-04-07 16:55:23 +08002700 unsigned long a[AUDITSC_ARGS];
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002701 unsigned long a0, a1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 int err;
Arjan van de Ven47379052009-09-28 12:57:44 -07002703 unsigned int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704
Anton Blanchard228e5482011-05-02 20:21:35 +00002705 if (call < 1 || call > SYS_SENDMMSG)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 return -EINVAL;
Jeremy Clinec8e8cd52018-07-27 22:43:01 +00002707 call = array_index_nospec(call, SYS_SENDMMSG + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708
Arjan van de Ven47379052009-09-28 12:57:44 -07002709 len = nargs[call];
2710 if (len > sizeof(a))
2711 return -EINVAL;
2712
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 /* copy_from_user should be SMP safe. */
Arjan van de Ven47379052009-09-28 12:57:44 -07002714 if (copy_from_user(a, args, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 return -EFAULT;
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002716
Chen Gang2950fa92013-04-07 16:55:23 +08002717 err = audit_socketcall(nargs[call] / sizeof(unsigned long), a);
2718 if (err)
2719 return err;
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002720
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002721 a0 = a[0];
2722 a1 = a[1];
2723
2724 switch (call) {
2725 case SYS_SOCKET:
Dominik Brodowski9d6a15c2018-03-13 19:29:43 +01002726 err = __sys_socket(a0, a1, a[2]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002727 break;
2728 case SYS_BIND:
Dominik Brodowskia87d35d2018-03-13 19:33:09 +01002729 err = __sys_bind(a0, (struct sockaddr __user *)a1, a[2]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002730 break;
2731 case SYS_CONNECT:
Dominik Brodowski1387c2c2018-03-13 19:35:09 +01002732 err = __sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002733 break;
2734 case SYS_LISTEN:
Dominik Brodowski25e290e2018-03-13 19:36:54 +01002735 err = __sys_listen(a0, a1);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002736 break;
2737 case SYS_ACCEPT:
Dominik Brodowski4541e802018-03-13 19:24:23 +01002738 err = __sys_accept4(a0, (struct sockaddr __user *)a1,
2739 (int __user *)a[2], 0);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002740 break;
2741 case SYS_GETSOCKNAME:
2742 err =
Dominik Brodowski8882a102018-03-13 19:43:14 +01002743 __sys_getsockname(a0, (struct sockaddr __user *)a1,
2744 (int __user *)a[2]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002745 break;
2746 case SYS_GETPEERNAME:
2747 err =
Dominik Brodowskib21c8f82018-03-13 19:47:00 +01002748 __sys_getpeername(a0, (struct sockaddr __user *)a1,
2749 (int __user *)a[2]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002750 break;
2751 case SYS_SOCKETPAIR:
Dominik Brodowski6debc8d2018-03-13 19:49:23 +01002752 err = __sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002753 break;
2754 case SYS_SEND:
Dominik Brodowskif3bf8962018-03-13 19:52:00 +01002755 err = __sys_sendto(a0, (void __user *)a1, a[2], a[3],
2756 NULL, 0);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002757 break;
2758 case SYS_SENDTO:
Dominik Brodowski211b6342018-03-13 19:18:52 +01002759 err = __sys_sendto(a0, (void __user *)a1, a[2], a[3],
2760 (struct sockaddr __user *)a[4], a[5]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002761 break;
2762 case SYS_RECV:
Dominik Brodowskid27e9af2018-03-13 19:54:17 +01002763 err = __sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
2764 NULL, NULL);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002765 break;
2766 case SYS_RECVFROM:
Dominik Brodowski7a09e1e2018-03-13 19:10:06 +01002767 err = __sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
2768 (struct sockaddr __user *)a[4],
2769 (int __user *)a[5]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002770 break;
2771 case SYS_SHUTDOWN:
Dominik Brodowski005a1ae2018-03-13 20:07:05 +01002772 err = __sys_shutdown(a0, a1);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002773 break;
2774 case SYS_SETSOCKOPT:
Dominik Brodowskicc36dca2018-03-13 20:10:59 +01002775 err = __sys_setsockopt(a0, a1, a[2], (char __user *)a[3],
2776 a[4]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002777 break;
2778 case SYS_GETSOCKOPT:
2779 err =
Dominik Brodowski13a2d702018-03-13 20:15:04 +01002780 __sys_getsockopt(a0, a1, a[2], (char __user *)a[3],
2781 (int __user *)a[4]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002782 break;
2783 case SYS_SENDMSG:
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002784 err = __sys_sendmsg(a0, (struct user_msghdr __user *)a1,
2785 a[2], true);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002786 break;
Anton Blanchard228e5482011-05-02 20:21:35 +00002787 case SYS_SENDMMSG:
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002788 err = __sys_sendmmsg(a0, (struct mmsghdr __user *)a1, a[2],
2789 a[3], true);
Anton Blanchard228e5482011-05-02 20:21:35 +00002790 break;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002791 case SYS_RECVMSG:
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002792 err = __sys_recvmsg(a0, (struct user_msghdr __user *)a1,
2793 a[2], true);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002794 break;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002795 case SYS_RECVMMSG:
Arnd Bergmanne11d4282018-04-18 13:43:52 +02002796 if (IS_ENABLED(CONFIG_64BIT) || !IS_ENABLED(CONFIG_64BIT_TIME))
2797 err = __sys_recvmmsg(a0, (struct mmsghdr __user *)a1,
2798 a[2], a[3],
2799 (struct __kernel_timespec __user *)a[4],
2800 NULL);
2801 else
2802 err = __sys_recvmmsg(a0, (struct mmsghdr __user *)a1,
2803 a[2], a[3], NULL,
2804 (struct old_timespec32 __user *)a[4]);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002805 break;
Ulrich Drepperde11def2008-11-19 15:36:14 -08002806 case SYS_ACCEPT4:
Dominik Brodowski4541e802018-03-13 19:24:23 +01002807 err = __sys_accept4(a0, (struct sockaddr __user *)a1,
2808 (int __user *)a[2], a[3]);
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07002809 break;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002810 default:
2811 err = -EINVAL;
2812 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 }
2814 return err;
2815}
2816
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002817#endif /* __ARCH_WANT_SYS_SOCKETCALL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002819/**
2820 * sock_register - add a socket protocol handler
2821 * @ops: description of protocol
2822 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 * This function is called by a protocol handler that wants to
2824 * advertise its address family, and have it linked into the
Masanari Iidae793c0f2014-09-04 23:44:36 +09002825 * socket interface. The value ops->family corresponds to the
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002826 * socket system call protocol family.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 */
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002828int sock_register(const struct net_proto_family *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829{
2830 int err;
2831
2832 if (ops->family >= NPROTO) {
Yang Yingliang3410f222014-02-12 17:09:55 +08002833 pr_crit("protocol %d >= NPROTO(%d)\n", ops->family, NPROTO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 return -ENOBUFS;
2835 }
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002836
2837 spin_lock(&net_family_lock);
Eric Dumazet190683a2010-11-10 10:50:44 +00002838 if (rcu_dereference_protected(net_families[ops->family],
2839 lockdep_is_held(&net_family_lock)))
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002840 err = -EEXIST;
2841 else {
Eric Dumazetcf778b02012-01-12 04:41:32 +00002842 rcu_assign_pointer(net_families[ops->family], ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 err = 0;
2844 }
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002845 spin_unlock(&net_family_lock);
2846
Yang Yingliang3410f222014-02-12 17:09:55 +08002847 pr_info("NET: Registered protocol family %d\n", ops->family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 return err;
2849}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002850EXPORT_SYMBOL(sock_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002852/**
2853 * sock_unregister - remove a protocol handler
2854 * @family: protocol family to remove
2855 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 * This function is called by a protocol handler that wants to
2857 * remove its address family, and have it unlinked from the
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002858 * new socket creation.
2859 *
2860 * If protocol handler is a module, then it can use module reference
2861 * counts to protect against new references. If protocol handler is not
2862 * a module then it needs to provide its own protection in
2863 * the ops->create routine.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 */
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002865void sock_unregister(int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866{
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002867 BUG_ON(family < 0 || family >= NPROTO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002869 spin_lock(&net_family_lock);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00002870 RCU_INIT_POINTER(net_families[family], NULL);
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002871 spin_unlock(&net_family_lock);
2872
2873 synchronize_rcu();
2874
Yang Yingliang3410f222014-02-12 17:09:55 +08002875 pr_info("NET: Unregistered protocol family %d\n", family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002877EXPORT_SYMBOL(sock_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878
Xin Longbf2ae2e2018-03-10 18:57:50 +08002879bool sock_is_registered(int family)
2880{
Jeremy Cline66b51b02018-08-13 22:23:13 +00002881 return family < NPROTO && rcu_access_pointer(net_families[family]);
Xin Longbf2ae2e2018-03-10 18:57:50 +08002882}
2883
Andi Kleen77d76ea2005-12-22 12:43:42 -08002884static int __init sock_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885{
Nick Pigginb3e19d92011-01-07 17:50:11 +11002886 int err;
Eric W. Biederman2ca794e2012-04-19 13:20:32 +00002887 /*
2888 * Initialize the network sysctl infrastructure.
2889 */
2890 err = net_sysctl_init();
2891 if (err)
2892 goto out;
Nick Pigginb3e19d92011-01-07 17:50:11 +11002893
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002895 * Initialize skbuff SLAB cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 */
2897 skb_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898
2899 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002900 * Initialize the protocols module.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 */
2902
2903 init_inodecache();
Nick Pigginb3e19d92011-01-07 17:50:11 +11002904
2905 err = register_filesystem(&sock_fs_type);
2906 if (err)
2907 goto out_fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 sock_mnt = kern_mount(&sock_fs_type);
Nick Pigginb3e19d92011-01-07 17:50:11 +11002909 if (IS_ERR(sock_mnt)) {
2910 err = PTR_ERR(sock_mnt);
2911 goto out_mount;
2912 }
Andi Kleen77d76ea2005-12-22 12:43:42 -08002913
2914 /* The real protocol initialization is performed in later initcalls.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 */
2916
2917#ifdef CONFIG_NETFILTER
Pablo Neira Ayuso6d11cfd2013-05-22 22:42:36 +00002918 err = netfilter_init();
2919 if (err)
2920 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921#endif
David S. Millercbeb3212005-12-22 12:58:55 -08002922
Daniel Borkmann408eccc2014-04-01 16:20:23 +02002923 ptp_classifier_init();
Richard Cochranc1f19b52010-07-17 08:49:36 +00002924
Nick Pigginb3e19d92011-01-07 17:50:11 +11002925out:
2926 return err;
2927
2928out_mount:
2929 unregister_filesystem(&sock_fs_type);
2930out_fs:
2931 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932}
2933
Andi Kleen77d76ea2005-12-22 12:43:42 -08002934core_initcall(sock_init); /* early initcall */
2935
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936#ifdef CONFIG_PROC_FS
2937void socket_seq_show(struct seq_file *seq)
2938{
Tonghao Zhang648845a2017-12-14 05:51:58 -08002939 seq_printf(seq, "sockets: used %d\n",
2940 sock_inuse_get(seq->private));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941}
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002942#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943
Shaun Pereira89bbfc92006-03-21 23:58:08 -08002944#ifdef CONFIG_COMPAT
Al Viro36fd6332017-06-26 13:19:16 -04002945static int compat_dev_ifconf(struct net *net, struct compat_ifconf __user *uifc32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002946{
Arnd Bergmann6b960182009-11-06 23:10:54 -08002947 struct compat_ifconf ifc32;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002948 struct ifconf ifc;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002949 int err;
2950
Arnd Bergmann6b960182009-11-06 23:10:54 -08002951 if (copy_from_user(&ifc32, uifc32, sizeof(struct compat_ifconf)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002952 return -EFAULT;
2953
Al Viro36fd6332017-06-26 13:19:16 -04002954 ifc.ifc_len = ifc32.ifc_len;
2955 ifc.ifc_req = compat_ptr(ifc32.ifcbuf);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002956
Al Viro36fd6332017-06-26 13:19:16 -04002957 rtnl_lock();
2958 err = dev_ifconf(net, &ifc, sizeof(struct compat_ifreq));
2959 rtnl_unlock();
Arnd Bergmann7a229382009-11-06 23:00:29 -08002960 if (err)
2961 return err;
2962
Al Viro36fd6332017-06-26 13:19:16 -04002963 ifc32.ifc_len = ifc.ifc_len;
Arnd Bergmann6b960182009-11-06 23:10:54 -08002964 if (copy_to_user(uifc32, &ifc32, sizeof(struct compat_ifconf)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002965 return -EFAULT;
2966
2967 return 0;
2968}
2969
Arnd Bergmann6b960182009-11-06 23:10:54 -08002970static int ethtool_ioctl(struct net *net, struct compat_ifreq __user *ifr32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002971{
Ben Hutchings3a7da392011-03-17 07:34:32 +00002972 struct compat_ethtool_rxnfc __user *compat_rxnfc;
2973 bool convert_in = false, convert_out = false;
Al Viro44c02a22017-10-05 12:59:44 -04002974 size_t buf_size = 0;
2975 struct ethtool_rxnfc __user *rxnfc = NULL;
2976 struct ifreq ifr;
Ben Hutchings3a7da392011-03-17 07:34:32 +00002977 u32 rule_cnt = 0, actual_rule_cnt;
2978 u32 ethcmd;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002979 u32 data;
Ben Hutchings3a7da392011-03-17 07:34:32 +00002980 int ret;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002981
2982 if (get_user(data, &ifr32->ifr_ifru.ifru_data))
2983 return -EFAULT;
2984
Ben Hutchings3a7da392011-03-17 07:34:32 +00002985 compat_rxnfc = compat_ptr(data);
2986
2987 if (get_user(ethcmd, &compat_rxnfc->cmd))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002988 return -EFAULT;
2989
Ben Hutchings3a7da392011-03-17 07:34:32 +00002990 /* Most ethtool structures are defined without padding.
2991 * Unfortunately struct ethtool_rxnfc is an exception.
2992 */
2993 switch (ethcmd) {
2994 default:
2995 break;
2996 case ETHTOOL_GRXCLSRLALL:
2997 /* Buffer size is variable */
2998 if (get_user(rule_cnt, &compat_rxnfc->rule_cnt))
2999 return -EFAULT;
3000 if (rule_cnt > KMALLOC_MAX_SIZE / sizeof(u32))
3001 return -ENOMEM;
3002 buf_size += rule_cnt * sizeof(u32);
3003 /* fall through */
3004 case ETHTOOL_GRXRINGS:
3005 case ETHTOOL_GRXCLSRLCNT:
3006 case ETHTOOL_GRXCLSRULE:
Ben Hutchings55664f32012-01-03 12:04:51 +00003007 case ETHTOOL_SRXCLSRLINS:
Ben Hutchings3a7da392011-03-17 07:34:32 +00003008 convert_out = true;
3009 /* fall through */
3010 case ETHTOOL_SRXCLSRLDEL:
Ben Hutchings3a7da392011-03-17 07:34:32 +00003011 buf_size += sizeof(struct ethtool_rxnfc);
3012 convert_in = true;
Al Viro44c02a22017-10-05 12:59:44 -04003013 rxnfc = compat_alloc_user_space(buf_size);
Ben Hutchings3a7da392011-03-17 07:34:32 +00003014 break;
3015 }
3016
Al Viro44c02a22017-10-05 12:59:44 -04003017 if (copy_from_user(&ifr.ifr_name, &ifr32->ifr_name, IFNAMSIZ))
Ben Hutchings3a7da392011-03-17 07:34:32 +00003018 return -EFAULT;
3019
Al Viro44c02a22017-10-05 12:59:44 -04003020 ifr.ifr_data = convert_in ? rxnfc : (void __user *)compat_rxnfc;
Ben Hutchings3a7da392011-03-17 07:34:32 +00003021
3022 if (convert_in) {
Alexander Duyck127fe532011-04-08 18:01:59 +00003023 /* We expect there to be holes between fs.m_ext and
Ben Hutchings3a7da392011-03-17 07:34:32 +00003024 * fs.ring_cookie and at the end of fs, but nowhere else.
3025 */
Alexander Duyck127fe532011-04-08 18:01:59 +00003026 BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc, fs.m_ext) +
3027 sizeof(compat_rxnfc->fs.m_ext) !=
3028 offsetof(struct ethtool_rxnfc, fs.m_ext) +
3029 sizeof(rxnfc->fs.m_ext));
Ben Hutchings3a7da392011-03-17 07:34:32 +00003030 BUILD_BUG_ON(
3031 offsetof(struct compat_ethtool_rxnfc, fs.location) -
3032 offsetof(struct compat_ethtool_rxnfc, fs.ring_cookie) !=
3033 offsetof(struct ethtool_rxnfc, fs.location) -
3034 offsetof(struct ethtool_rxnfc, fs.ring_cookie));
3035
3036 if (copy_in_user(rxnfc, compat_rxnfc,
Stephen Hemminger954b1242013-02-11 06:22:28 +00003037 (void __user *)(&rxnfc->fs.m_ext + 1) -
3038 (void __user *)rxnfc) ||
Ben Hutchings3a7da392011-03-17 07:34:32 +00003039 copy_in_user(&rxnfc->fs.ring_cookie,
3040 &compat_rxnfc->fs.ring_cookie,
Stephen Hemminger954b1242013-02-11 06:22:28 +00003041 (void __user *)(&rxnfc->fs.location + 1) -
Wenwen Wangb6168562018-10-18 09:36:46 -05003042 (void __user *)&rxnfc->fs.ring_cookie))
3043 return -EFAULT;
3044 if (ethcmd == ETHTOOL_GRXCLSRLALL) {
3045 if (put_user(rule_cnt, &rxnfc->rule_cnt))
3046 return -EFAULT;
3047 } else if (copy_in_user(&rxnfc->rule_cnt,
3048 &compat_rxnfc->rule_cnt,
3049 sizeof(rxnfc->rule_cnt)))
Ben Hutchings3a7da392011-03-17 07:34:32 +00003050 return -EFAULT;
3051 }
3052
Al Viro44c02a22017-10-05 12:59:44 -04003053 ret = dev_ioctl(net, SIOCETHTOOL, &ifr, NULL);
Ben Hutchings3a7da392011-03-17 07:34:32 +00003054 if (ret)
3055 return ret;
3056
3057 if (convert_out) {
3058 if (copy_in_user(compat_rxnfc, rxnfc,
Stephen Hemminger954b1242013-02-11 06:22:28 +00003059 (const void __user *)(&rxnfc->fs.m_ext + 1) -
3060 (const void __user *)rxnfc) ||
Ben Hutchings3a7da392011-03-17 07:34:32 +00003061 copy_in_user(&compat_rxnfc->fs.ring_cookie,
3062 &rxnfc->fs.ring_cookie,
Stephen Hemminger954b1242013-02-11 06:22:28 +00003063 (const void __user *)(&rxnfc->fs.location + 1) -
3064 (const void __user *)&rxnfc->fs.ring_cookie) ||
Ben Hutchings3a7da392011-03-17 07:34:32 +00003065 copy_in_user(&compat_rxnfc->rule_cnt, &rxnfc->rule_cnt,
3066 sizeof(rxnfc->rule_cnt)))
3067 return -EFAULT;
3068
3069 if (ethcmd == ETHTOOL_GRXCLSRLALL) {
3070 /* As an optimisation, we only copy the actual
3071 * number of rules that the underlying
3072 * function returned. Since Mallory might
3073 * change the rule count in user memory, we
3074 * check that it is less than the rule count
3075 * originally given (as the user buffer size),
3076 * which has been range-checked.
3077 */
3078 if (get_user(actual_rule_cnt, &rxnfc->rule_cnt))
3079 return -EFAULT;
3080 if (actual_rule_cnt < rule_cnt)
3081 rule_cnt = actual_rule_cnt;
3082 if (copy_in_user(&compat_rxnfc->rule_locs[0],
3083 &rxnfc->rule_locs[0],
3084 rule_cnt * sizeof(u32)))
3085 return -EFAULT;
3086 }
3087 }
3088
3089 return 0;
Arnd Bergmann7a229382009-11-06 23:00:29 -08003090}
3091
Arnd Bergmann7a50a242009-11-08 20:57:03 -08003092static int compat_siocwandev(struct net *net, struct compat_ifreq __user *uifr32)
3093{
Arnd Bergmann7a50a242009-11-08 20:57:03 -08003094 compat_uptr_t uptr32;
Al Viro44c02a22017-10-05 12:59:44 -04003095 struct ifreq ifr;
3096 void __user *saved;
3097 int err;
Arnd Bergmann7a50a242009-11-08 20:57:03 -08003098
Al Viro44c02a22017-10-05 12:59:44 -04003099 if (copy_from_user(&ifr, uifr32, sizeof(struct compat_ifreq)))
Arnd Bergmann7a50a242009-11-08 20:57:03 -08003100 return -EFAULT;
3101
3102 if (get_user(uptr32, &uifr32->ifr_settings.ifs_ifsu))
3103 return -EFAULT;
3104
Al Viro44c02a22017-10-05 12:59:44 -04003105 saved = ifr.ifr_settings.ifs_ifsu.raw_hdlc;
3106 ifr.ifr_settings.ifs_ifsu.raw_hdlc = compat_ptr(uptr32);
Arnd Bergmann7a50a242009-11-08 20:57:03 -08003107
Al Viro44c02a22017-10-05 12:59:44 -04003108 err = dev_ioctl(net, SIOCWANDEV, &ifr, NULL);
3109 if (!err) {
3110 ifr.ifr_settings.ifs_ifsu.raw_hdlc = saved;
3111 if (copy_to_user(uifr32, &ifr, sizeof(struct compat_ifreq)))
3112 err = -EFAULT;
Joe Perchesccbd6a52010-05-14 10:58:26 +00003113 }
Al Viro44c02a22017-10-05 12:59:44 -04003114 return err;
Arnd Bergmann7a229382009-11-06 23:00:29 -08003115}
3116
Ben Hutchings590d4692013-11-18 17:04:13 +00003117/* Handle ioctls that use ifreq::ifr_data and just need struct ifreq converted */
3118static int compat_ifr_data_ioctl(struct net *net, unsigned int cmd,
Arnd Bergmann6b960182009-11-06 23:10:54 -08003119 struct compat_ifreq __user *u_ifreq32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08003120{
Al Viro44c02a22017-10-05 12:59:44 -04003121 struct ifreq ifreq;
Arnd Bergmann7a229382009-11-06 23:00:29 -08003122 u32 data32;
3123
Al Viro44c02a22017-10-05 12:59:44 -04003124 if (copy_from_user(ifreq.ifr_name, u_ifreq32->ifr_name, IFNAMSIZ))
Arnd Bergmann7a229382009-11-06 23:00:29 -08003125 return -EFAULT;
Al Viro44c02a22017-10-05 12:59:44 -04003126 if (get_user(data32, &u_ifreq32->ifr_data))
Arnd Bergmann7a229382009-11-06 23:00:29 -08003127 return -EFAULT;
Al Viro44c02a22017-10-05 12:59:44 -04003128 ifreq.ifr_data = compat_ptr(data32);
Arnd Bergmann7a229382009-11-06 23:00:29 -08003129
Al Viro44c02a22017-10-05 12:59:44 -04003130 return dev_ioctl(net, cmd, &ifreq, NULL);
Arnd Bergmann7a229382009-11-06 23:00:29 -08003131}
3132
Johannes Berg37ac39b2019-01-25 22:43:18 +01003133static int compat_ifreq_ioctl(struct net *net, struct socket *sock,
3134 unsigned int cmd,
3135 struct compat_ifreq __user *uifr32)
3136{
3137 struct ifreq __user *uifr;
3138 int err;
3139
3140 /* Handle the fact that while struct ifreq has the same *layout* on
3141 * 32/64 for everything but ifreq::ifru_ifmap and ifreq::ifru_data,
3142 * which are handled elsewhere, it still has different *size* due to
3143 * ifreq::ifru_ifmap (which is 16 bytes on 32 bit, 24 bytes on 64-bit,
3144 * resulting in struct ifreq being 32 and 40 bytes respectively).
3145 * As a result, if the struct happens to be at the end of a page and
3146 * the next page isn't readable/writable, we get a fault. To prevent
3147 * that, copy back and forth to the full size.
3148 */
3149
3150 uifr = compat_alloc_user_space(sizeof(*uifr));
3151 if (copy_in_user(uifr, uifr32, sizeof(*uifr32)))
3152 return -EFAULT;
3153
3154 err = sock_do_ioctl(net, sock, cmd, (unsigned long)uifr);
3155
3156 if (!err) {
3157 switch (cmd) {
3158 case SIOCGIFFLAGS:
3159 case SIOCGIFMETRIC:
3160 case SIOCGIFMTU:
3161 case SIOCGIFMEM:
3162 case SIOCGIFHWADDR:
3163 case SIOCGIFINDEX:
3164 case SIOCGIFADDR:
3165 case SIOCGIFBRDADDR:
3166 case SIOCGIFDSTADDR:
3167 case SIOCGIFNETMASK:
3168 case SIOCGIFPFLAGS:
3169 case SIOCGIFTXQLEN:
3170 case SIOCGMIIPHY:
3171 case SIOCGMIIREG:
Johannes Bergc6c9fee2019-01-25 22:43:19 +01003172 case SIOCGIFNAME:
Johannes Berg37ac39b2019-01-25 22:43:18 +01003173 if (copy_in_user(uifr32, uifr, sizeof(*uifr32)))
3174 err = -EFAULT;
3175 break;
3176 }
3177 }
3178 return err;
3179}
3180
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003181static int compat_sioc_ifmap(struct net *net, unsigned int cmd,
3182 struct compat_ifreq __user *uifr32)
3183{
3184 struct ifreq ifr;
3185 struct compat_ifmap __user *uifmap32;
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003186 int err;
3187
3188 uifmap32 = &uifr32->ifr_ifru.ifru_map;
3189 err = copy_from_user(&ifr, uifr32, sizeof(ifr.ifr_name));
Mathieu Desnoyers3ddc5b42013-09-11 14:23:18 -07003190 err |= get_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
3191 err |= get_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
3192 err |= get_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
3193 err |= get_user(ifr.ifr_map.irq, &uifmap32->irq);
3194 err |= get_user(ifr.ifr_map.dma, &uifmap32->dma);
3195 err |= get_user(ifr.ifr_map.port, &uifmap32->port);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003196 if (err)
3197 return -EFAULT;
3198
Al Viro44c02a22017-10-05 12:59:44 -04003199 err = dev_ioctl(net, cmd, &ifr, NULL);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003200
3201 if (cmd == SIOCGIFMAP && !err) {
3202 err = copy_to_user(uifr32, &ifr, sizeof(ifr.ifr_name));
Mathieu Desnoyers3ddc5b42013-09-11 14:23:18 -07003203 err |= put_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
3204 err |= put_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
3205 err |= put_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
3206 err |= put_user(ifr.ifr_map.irq, &uifmap32->irq);
3207 err |= put_user(ifr.ifr_map.dma, &uifmap32->dma);
3208 err |= put_user(ifr.ifr_map.port, &uifmap32->port);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003209 if (err)
3210 err = -EFAULT;
3211 }
3212 return err;
3213}
3214
Arnd Bergmann7a229382009-11-06 23:00:29 -08003215struct rtentry32 {
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003216 u32 rt_pad1;
Arnd Bergmann7a229382009-11-06 23:00:29 -08003217 struct sockaddr rt_dst; /* target address */
3218 struct sockaddr rt_gateway; /* gateway addr (RTF_GATEWAY) */
3219 struct sockaddr rt_genmask; /* target network mask (IP) */
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003220 unsigned short rt_flags;
3221 short rt_pad2;
3222 u32 rt_pad3;
3223 unsigned char rt_tos;
3224 unsigned char rt_class;
3225 short rt_pad4;
3226 short rt_metric; /* +1 for binary compatibility! */
Arnd Bergmann7a229382009-11-06 23:00:29 -08003227 /* char * */ u32 rt_dev; /* forcing the device at add */
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003228 u32 rt_mtu; /* per route MTU/Window */
3229 u32 rt_window; /* Window clamping */
Arnd Bergmann7a229382009-11-06 23:00:29 -08003230 unsigned short rt_irtt; /* Initial RTT */
3231};
3232
3233struct in6_rtmsg32 {
3234 struct in6_addr rtmsg_dst;
3235 struct in6_addr rtmsg_src;
3236 struct in6_addr rtmsg_gateway;
3237 u32 rtmsg_type;
3238 u16 rtmsg_dst_len;
3239 u16 rtmsg_src_len;
3240 u32 rtmsg_metric;
3241 u32 rtmsg_info;
3242 u32 rtmsg_flags;
3243 s32 rtmsg_ifindex;
3244};
3245
Arnd Bergmann6b960182009-11-06 23:10:54 -08003246static int routing_ioctl(struct net *net, struct socket *sock,
3247 unsigned int cmd, void __user *argp)
Arnd Bergmann7a229382009-11-06 23:00:29 -08003248{
3249 int ret;
3250 void *r = NULL;
3251 struct in6_rtmsg r6;
3252 struct rtentry r4;
3253 char devname[16];
3254 u32 rtdev;
3255 mm_segment_t old_fs = get_fs();
3256
Arnd Bergmann6b960182009-11-06 23:10:54 -08003257 if (sock && sock->sk && sock->sk->sk_family == AF_INET6) { /* ipv6 */
3258 struct in6_rtmsg32 __user *ur6 = argp;
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003259 ret = copy_from_user(&r6.rtmsg_dst, &(ur6->rtmsg_dst),
Arnd Bergmann7a229382009-11-06 23:00:29 -08003260 3 * sizeof(struct in6_addr));
Mathieu Desnoyers3ddc5b42013-09-11 14:23:18 -07003261 ret |= get_user(r6.rtmsg_type, &(ur6->rtmsg_type));
3262 ret |= get_user(r6.rtmsg_dst_len, &(ur6->rtmsg_dst_len));
3263 ret |= get_user(r6.rtmsg_src_len, &(ur6->rtmsg_src_len));
3264 ret |= get_user(r6.rtmsg_metric, &(ur6->rtmsg_metric));
3265 ret |= get_user(r6.rtmsg_info, &(ur6->rtmsg_info));
3266 ret |= get_user(r6.rtmsg_flags, &(ur6->rtmsg_flags));
3267 ret |= get_user(r6.rtmsg_ifindex, &(ur6->rtmsg_ifindex));
Arnd Bergmann7a229382009-11-06 23:00:29 -08003268
3269 r = (void *) &r6;
3270 } else { /* ipv4 */
Arnd Bergmann6b960182009-11-06 23:10:54 -08003271 struct rtentry32 __user *ur4 = argp;
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003272 ret = copy_from_user(&r4.rt_dst, &(ur4->rt_dst),
Arnd Bergmann7a229382009-11-06 23:00:29 -08003273 3 * sizeof(struct sockaddr));
Mathieu Desnoyers3ddc5b42013-09-11 14:23:18 -07003274 ret |= get_user(r4.rt_flags, &(ur4->rt_flags));
3275 ret |= get_user(r4.rt_metric, &(ur4->rt_metric));
3276 ret |= get_user(r4.rt_mtu, &(ur4->rt_mtu));
3277 ret |= get_user(r4.rt_window, &(ur4->rt_window));
3278 ret |= get_user(r4.rt_irtt, &(ur4->rt_irtt));
3279 ret |= get_user(rtdev, &(ur4->rt_dev));
Arnd Bergmann7a229382009-11-06 23:00:29 -08003280 if (rtdev) {
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003281 ret |= copy_from_user(devname, compat_ptr(rtdev), 15);
stephen hemmingerc3f52ae2011-02-23 09:06:48 +00003282 r4.rt_dev = (char __user __force *)devname;
3283 devname[15] = 0;
Arnd Bergmann7a229382009-11-06 23:00:29 -08003284 } else
3285 r4.rt_dev = NULL;
3286
3287 r = (void *) &r4;
3288 }
3289
3290 if (ret) {
3291 ret = -EFAULT;
3292 goto out;
3293 }
3294
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003295 set_fs(KERNEL_DS);
Johannes Berg63ff03a2019-01-25 22:43:17 +01003296 ret = sock_do_ioctl(net, sock, cmd, (unsigned long) r);
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003297 set_fs(old_fs);
Arnd Bergmann7a229382009-11-06 23:00:29 -08003298
3299out:
Arnd Bergmann7a229382009-11-06 23:00:29 -08003300 return ret;
3301}
3302
3303/* Since old style bridge ioctl's endup using SIOCDEVPRIVATE
3304 * for some operations; this forces use of the newer bridge-utils that
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003305 * use compatible ioctls
Arnd Bergmann7a229382009-11-06 23:00:29 -08003306 */
Arnd Bergmann6b960182009-11-06 23:10:54 -08003307static int old_bridge_ioctl(compat_ulong_t __user *argp)
Arnd Bergmann7a229382009-11-06 23:00:29 -08003308{
Arnd Bergmann6b960182009-11-06 23:10:54 -08003309 compat_ulong_t tmp;
Arnd Bergmann7a229382009-11-06 23:00:29 -08003310
Arnd Bergmann6b960182009-11-06 23:10:54 -08003311 if (get_user(tmp, argp))
Arnd Bergmann7a229382009-11-06 23:00:29 -08003312 return -EFAULT;
3313 if (tmp == BRCTL_GET_VERSION)
3314 return BRCTL_VERSION + 1;
3315 return -EINVAL;
3316}
3317
Arnd Bergmann6b960182009-11-06 23:10:54 -08003318static int compat_sock_ioctl_trans(struct file *file, struct socket *sock,
3319 unsigned int cmd, unsigned long arg)
3320{
3321 void __user *argp = compat_ptr(arg);
3322 struct sock *sk = sock->sk;
3323 struct net *net = sock_net(sk);
Arnd Bergmann7a229382009-11-06 23:00:29 -08003324
Arnd Bergmann6b960182009-11-06 23:10:54 -08003325 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15))
Ben Hutchings590d4692013-11-18 17:04:13 +00003326 return compat_ifr_data_ioctl(net, cmd, argp);
Arnd Bergmann7a229382009-11-06 23:00:29 -08003327
Arnd Bergmann6b960182009-11-06 23:10:54 -08003328 switch (cmd) {
3329 case SIOCSIFBR:
3330 case SIOCGIFBR:
3331 return old_bridge_ioctl(argp);
Arnd Bergmann6b960182009-11-06 23:10:54 -08003332 case SIOCGIFCONF:
Al Viro36fd6332017-06-26 13:19:16 -04003333 return compat_dev_ifconf(net, argp);
Arnd Bergmann6b960182009-11-06 23:10:54 -08003334 case SIOCETHTOOL:
3335 return ethtool_ioctl(net, argp);
Arnd Bergmann7a50a242009-11-08 20:57:03 -08003336 case SIOCWANDEV:
3337 return compat_siocwandev(net, argp);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003338 case SIOCGIFMAP:
3339 case SIOCSIFMAP:
3340 return compat_sioc_ifmap(net, cmd, argp);
Arnd Bergmann6b960182009-11-06 23:10:54 -08003341 case SIOCADDRT:
3342 case SIOCDELRT:
3343 return routing_ioctl(net, sock, cmd, argp);
Arnd Bergmann0768e172019-04-17 22:56:11 +02003344 case SIOCGSTAMP_OLD:
3345 case SIOCGSTAMPNS_OLD:
Arnd Bergmannc7cbdbf2019-04-17 22:51:48 +02003346 if (!sock->ops->gettstamp)
3347 return -ENOIOCTLCMD;
Arnd Bergmann0768e172019-04-17 22:56:11 +02003348 return sock->ops->gettstamp(sock, argp, cmd == SIOCGSTAMP_OLD,
Arnd Bergmannc7cbdbf2019-04-17 22:51:48 +02003349 !COMPAT_USE_64BIT_TIME);
3350
Ben Hutchings590d4692013-11-18 17:04:13 +00003351 case SIOCBONDSLAVEINFOQUERY:
3352 case SIOCBONDINFOQUERY:
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003353 case SIOCSHWTSTAMP:
Ben Hutchingsfd468c72013-11-14 01:19:29 +00003354 case SIOCGHWTSTAMP:
Ben Hutchings590d4692013-11-18 17:04:13 +00003355 return compat_ifr_data_ioctl(net, cmd, argp);
Arnd Bergmann7a229382009-11-06 23:00:29 -08003356
Arnd Bergmann6b960182009-11-06 23:10:54 -08003357 case FIOSETOWN:
3358 case SIOCSPGRP:
3359 case FIOGETOWN:
3360 case SIOCGPGRP:
3361 case SIOCBRADDBR:
3362 case SIOCBRDELBR:
3363 case SIOCGIFVLAN:
3364 case SIOCSIFVLAN:
3365 case SIOCADDDLCI:
3366 case SIOCDELDLCI:
Andrey Vaginc62cce22016-10-24 18:29:13 -07003367 case SIOCGSKNS:
Arnd Bergmann0768e172019-04-17 22:56:11 +02003368 case SIOCGSTAMP_NEW:
3369 case SIOCGSTAMPNS_NEW:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003370 return sock_ioctl(file, cmd, arg);
3371
3372 case SIOCGIFFLAGS:
3373 case SIOCSIFFLAGS:
3374 case SIOCGIFMETRIC:
3375 case SIOCSIFMETRIC:
3376 case SIOCGIFMTU:
3377 case SIOCSIFMTU:
3378 case SIOCGIFMEM:
3379 case SIOCSIFMEM:
3380 case SIOCGIFHWADDR:
3381 case SIOCSIFHWADDR:
3382 case SIOCADDMULTI:
3383 case SIOCDELMULTI:
3384 case SIOCGIFINDEX:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003385 case SIOCGIFADDR:
3386 case SIOCSIFADDR:
3387 case SIOCSIFHWBROADCAST:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003388 case SIOCDIFADDR:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003389 case SIOCGIFBRDADDR:
3390 case SIOCSIFBRDADDR:
3391 case SIOCGIFDSTADDR:
3392 case SIOCSIFDSTADDR:
3393 case SIOCGIFNETMASK:
3394 case SIOCSIFNETMASK:
3395 case SIOCSIFPFLAGS:
3396 case SIOCGIFPFLAGS:
3397 case SIOCGIFTXQLEN:
3398 case SIOCSIFTXQLEN:
3399 case SIOCBRADDIF:
3400 case SIOCBRDELIF:
Johannes Bergc6c9fee2019-01-25 22:43:19 +01003401 case SIOCGIFNAME:
Arnd Bergmann9177efd2009-11-06 08:09:09 +00003402 case SIOCSIFNAME:
3403 case SIOCGMIIPHY:
3404 case SIOCGMIIREG:
3405 case SIOCSMIIREG:
Al Virof92d4fc2017-09-30 19:32:17 -04003406 case SIOCBONDENSLAVE:
3407 case SIOCBONDRELEASE:
3408 case SIOCBONDSETHWADDR:
3409 case SIOCBONDCHANGEACTIVE:
Johannes Berg37ac39b2019-01-25 22:43:18 +01003410 return compat_ifreq_ioctl(net, sock, cmd, argp);
3411
Arnd Bergmann6b960182009-11-06 23:10:54 -08003412 case SIOCSARP:
3413 case SIOCGARP:
3414 case SIOCDARP:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003415 case SIOCATMARK:
Johannes Berg63ff03a2019-01-25 22:43:17 +01003416 return sock_do_ioctl(net, sock, cmd, arg);
Arnd Bergmann9177efd2009-11-06 08:09:09 +00003417 }
3418
Arnd Bergmann6b960182009-11-06 23:10:54 -08003419 return -ENOIOCTLCMD;
3420}
Arnd Bergmann7a229382009-11-06 23:00:29 -08003421
Eric Dumazet95c96172012-04-15 05:58:06 +00003422static long compat_sock_ioctl(struct file *file, unsigned int cmd,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07003423 unsigned long arg)
Shaun Pereira89bbfc92006-03-21 23:58:08 -08003424{
3425 struct socket *sock = file->private_data;
3426 int ret = -ENOIOCTLCMD;
David S. Miller87de87d2008-06-03 09:14:03 -07003427 struct sock *sk;
3428 struct net *net;
3429
3430 sk = sock->sk;
3431 net = sock_net(sk);
Shaun Pereira89bbfc92006-03-21 23:58:08 -08003432
3433 if (sock->ops->compat_ioctl)
3434 ret = sock->ops->compat_ioctl(sock, cmd, arg);
3435
David S. Miller87de87d2008-06-03 09:14:03 -07003436 if (ret == -ENOIOCTLCMD &&
3437 (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST))
3438 ret = compat_wext_handle_ioctl(net, cmd, arg);
3439
Arnd Bergmann6b960182009-11-06 23:10:54 -08003440 if (ret == -ENOIOCTLCMD)
3441 ret = compat_sock_ioctl_trans(file, sock, cmd, arg);
3442
Shaun Pereira89bbfc92006-03-21 23:58:08 -08003443 return ret;
3444}
3445#endif
3446
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003447/**
3448 * kernel_bind - bind an address to a socket (kernel space)
3449 * @sock: socket
3450 * @addr: address
3451 * @addrlen: length of address
3452 *
3453 * Returns 0 or an error.
3454 */
3455
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003456int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen)
3457{
3458 return sock->ops->bind(sock, addr, addrlen);
3459}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003460EXPORT_SYMBOL(kernel_bind);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003461
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003462/**
3463 * kernel_listen - move socket to listening state (kernel space)
3464 * @sock: socket
3465 * @backlog: pending connections queue size
3466 *
3467 * Returns 0 or an error.
3468 */
3469
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003470int kernel_listen(struct socket *sock, int backlog)
3471{
3472 return sock->ops->listen(sock, backlog);
3473}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003474EXPORT_SYMBOL(kernel_listen);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003475
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003476/**
3477 * kernel_accept - accept a connection (kernel space)
3478 * @sock: listening socket
3479 * @newsock: new connected socket
3480 * @flags: flags
3481 *
3482 * @flags must be SOCK_CLOEXEC, SOCK_NONBLOCK or 0.
3483 * If it fails, @newsock is guaranteed to be %NULL.
3484 * Returns 0 or an error.
3485 */
3486
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003487int kernel_accept(struct socket *sock, struct socket **newsock, int flags)
3488{
3489 struct sock *sk = sock->sk;
3490 int err;
3491
3492 err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
3493 newsock);
3494 if (err < 0)
3495 goto done;
3496
David Howellscdfbabf2017-03-09 08:09:05 +00003497 err = sock->ops->accept(sock, *newsock, flags, true);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003498 if (err < 0) {
3499 sock_release(*newsock);
Tony Battersbyfa8705b2007-10-10 21:09:04 -07003500 *newsock = NULL;
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003501 goto done;
3502 }
3503
3504 (*newsock)->ops = sock->ops;
Wei Yongjun1b085342008-12-18 19:35:10 -08003505 __module_get((*newsock)->ops->owner);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003506
3507done:
3508 return err;
3509}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003510EXPORT_SYMBOL(kernel_accept);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003511
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003512/**
3513 * kernel_connect - connect a socket (kernel space)
3514 * @sock: socket
3515 * @addr: address
3516 * @addrlen: address length
3517 * @flags: flags (O_NONBLOCK, ...)
3518 *
3519 * For datagram sockets, @addr is the addres to which datagrams are sent
3520 * by default, and the only address from which datagrams are received.
3521 * For stream sockets, attempts to connect to @addr.
3522 * Returns 0 or an error code.
3523 */
3524
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003525int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
YOSHIFUJI Hideaki4768fbc2007-02-09 23:25:31 +09003526 int flags)
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003527{
3528 return sock->ops->connect(sock, addr, addrlen, flags);
3529}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003530EXPORT_SYMBOL(kernel_connect);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003531
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003532/**
3533 * kernel_getsockname - get the address which the socket is bound (kernel space)
3534 * @sock: socket
3535 * @addr: address holder
3536 *
3537 * Fills the @addr pointer with the address which the socket is bound.
3538 * Returns 0 or an error code.
3539 */
3540
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01003541int kernel_getsockname(struct socket *sock, struct sockaddr *addr)
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003542{
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01003543 return sock->ops->getname(sock, addr, 0);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003544}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003545EXPORT_SYMBOL(kernel_getsockname);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003546
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003547/**
3548 * kernel_peername - get the address which the socket is connected (kernel space)
3549 * @sock: socket
3550 * @addr: address holder
3551 *
3552 * Fills the @addr pointer with the address which the socket is connected.
3553 * Returns 0 or an error code.
3554 */
3555
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01003556int kernel_getpeername(struct socket *sock, struct sockaddr *addr)
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003557{
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01003558 return sock->ops->getname(sock, addr, 1);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003559}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003560EXPORT_SYMBOL(kernel_getpeername);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003561
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003562/**
3563 * kernel_getsockopt - get a socket option (kernel space)
3564 * @sock: socket
3565 * @level: API level (SOL_SOCKET, ...)
3566 * @optname: option tag
3567 * @optval: option value
3568 * @optlen: option length
3569 *
3570 * Assigns the option length to @optlen.
3571 * Returns 0 or an error.
3572 */
3573
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003574int kernel_getsockopt(struct socket *sock, int level, int optname,
3575 char *optval, int *optlen)
3576{
3577 mm_segment_t oldfs = get_fs();
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003578 char __user *uoptval;
3579 int __user *uoptlen;
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003580 int err;
3581
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003582 uoptval = (char __user __force *) optval;
3583 uoptlen = (int __user __force *) optlen;
3584
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003585 set_fs(KERNEL_DS);
3586 if (level == SOL_SOCKET)
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003587 err = sock_getsockopt(sock, level, optname, uoptval, uoptlen);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003588 else
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003589 err = sock->ops->getsockopt(sock, level, optname, uoptval,
3590 uoptlen);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003591 set_fs(oldfs);
3592 return err;
3593}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003594EXPORT_SYMBOL(kernel_getsockopt);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003595
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003596/**
3597 * kernel_setsockopt - set a socket option (kernel space)
3598 * @sock: socket
3599 * @level: API level (SOL_SOCKET, ...)
3600 * @optname: option tag
3601 * @optval: option value
3602 * @optlen: option length
3603 *
3604 * Returns 0 or an error.
3605 */
3606
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003607int kernel_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07003608 char *optval, unsigned int optlen)
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003609{
3610 mm_segment_t oldfs = get_fs();
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003611 char __user *uoptval;
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003612 int err;
3613
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003614 uoptval = (char __user __force *) optval;
3615
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003616 set_fs(KERNEL_DS);
3617 if (level == SOL_SOCKET)
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003618 err = sock_setsockopt(sock, level, optname, uoptval, optlen);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003619 else
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003620 err = sock->ops->setsockopt(sock, level, optname, uoptval,
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003621 optlen);
3622 set_fs(oldfs);
3623 return err;
3624}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003625EXPORT_SYMBOL(kernel_setsockopt);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003626
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003627/**
3628 * kernel_sendpage - send a &page through a socket (kernel space)
3629 * @sock: socket
3630 * @page: page
3631 * @offset: page offset
3632 * @size: total size in bytes
3633 * @flags: flags (MSG_DONTWAIT, ...)
3634 *
3635 * Returns the total amount sent in bytes or an error.
3636 */
3637
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003638int kernel_sendpage(struct socket *sock, struct page *page, int offset,
3639 size_t size, int flags)
3640{
3641 if (sock->ops->sendpage)
3642 return sock->ops->sendpage(sock, page, offset, size, flags);
3643
3644 return sock_no_sendpage(sock, page, offset, size, flags);
3645}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003646EXPORT_SYMBOL(kernel_sendpage);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003647
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003648/**
3649 * kernel_sendpage_locked - send a &page through the locked sock (kernel space)
3650 * @sk: sock
3651 * @page: page
3652 * @offset: page offset
3653 * @size: total size in bytes
3654 * @flags: flags (MSG_DONTWAIT, ...)
3655 *
3656 * Returns the total amount sent in bytes or an error.
3657 * Caller must hold @sk.
3658 */
3659
Tom Herbert306b13e2017-07-28 16:22:41 -07003660int kernel_sendpage_locked(struct sock *sk, struct page *page, int offset,
3661 size_t size, int flags)
3662{
3663 struct socket *sock = sk->sk_socket;
3664
3665 if (sock->ops->sendpage_locked)
3666 return sock->ops->sendpage_locked(sk, page, offset, size,
3667 flags);
3668
3669 return sock_no_sendpage_locked(sk, page, offset, size, flags);
3670}
3671EXPORT_SYMBOL(kernel_sendpage_locked);
3672
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003673/**
3674 * kernel_shutdown - shut down part of a full-duplex connection (kernel space)
3675 * @sock: socket
3676 * @how: connection part
3677 *
3678 * Returns 0 or an error.
3679 */
3680
Trond Myklebust91cf45f2007-11-12 18:10:39 -08003681int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how)
3682{
3683 return sock->ops->shutdown(sock, how);
3684}
Trond Myklebust91cf45f2007-11-12 18:10:39 -08003685EXPORT_SYMBOL(kernel_sock_shutdown);
R. Parameswaran113c3072017-04-05 16:50:35 -07003686
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003687/**
3688 * kernel_sock_ip_overhead - returns the IP overhead imposed by a socket
3689 * @sk: socket
3690 *
3691 * This routine returns the IP overhead imposed by a socket i.e.
3692 * the length of the underlying IP header, depending on whether
3693 * this is an IPv4 or IPv6 socket and the length from IP options turned
3694 * on at the socket. Assumes that the caller has a lock on the socket.
R. Parameswaran113c3072017-04-05 16:50:35 -07003695 */
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003696
R. Parameswaran113c3072017-04-05 16:50:35 -07003697u32 kernel_sock_ip_overhead(struct sock *sk)
3698{
3699 struct inet_sock *inet;
3700 struct ip_options_rcu *opt;
3701 u32 overhead = 0;
R. Parameswaran113c3072017-04-05 16:50:35 -07003702#if IS_ENABLED(CONFIG_IPV6)
3703 struct ipv6_pinfo *np;
3704 struct ipv6_txoptions *optv6 = NULL;
3705#endif /* IS_ENABLED(CONFIG_IPV6) */
3706
3707 if (!sk)
3708 return overhead;
3709
R. Parameswaran113c3072017-04-05 16:50:35 -07003710 switch (sk->sk_family) {
3711 case AF_INET:
3712 inet = inet_sk(sk);
3713 overhead += sizeof(struct iphdr);
3714 opt = rcu_dereference_protected(inet->inet_opt,
stephen hemminger614d79c2017-07-24 10:25:22 -07003715 sock_owned_by_user(sk));
R. Parameswaran113c3072017-04-05 16:50:35 -07003716 if (opt)
3717 overhead += opt->opt.optlen;
3718 return overhead;
3719#if IS_ENABLED(CONFIG_IPV6)
3720 case AF_INET6:
3721 np = inet6_sk(sk);
3722 overhead += sizeof(struct ipv6hdr);
3723 if (np)
3724 optv6 = rcu_dereference_protected(np->opt,
stephen hemminger614d79c2017-07-24 10:25:22 -07003725 sock_owned_by_user(sk));
R. Parameswaran113c3072017-04-05 16:50:35 -07003726 if (optv6)
3727 overhead += (optv6->opt_flen + optv6->opt_nflen);
3728 return overhead;
3729#endif /* IS_ENABLED(CONFIG_IPV6) */
3730 default: /* Returns 0 overhead if the socket is not ipv4 or ipv6 */
3731 return overhead;
3732 }
3733}
3734EXPORT_SYMBOL(kernel_sock_ip_overhead);