blob: 541719a2443d875046a10d1d305d113eb53803f9 [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
Cong Wange0d10952013-08-01 11:10:25 +0800106#ifdef CONFIG_NET_RX_BUSY_POLL
Eliezer Tamir64b0dc52013-07-10 17:13:36 +0300107unsigned int sysctl_net_busy_read __read_mostly;
108unsigned int sysctl_net_busy_poll __read_mostly;
Eliezer Tamir06021292013-06-10 11:39:50 +0300109#endif
Arnd Bergmann6b960182009-11-06 23:10:54 -0800110
Al Viro8ae5e0302014-11-28 19:40:50 -0500111static ssize_t sock_read_iter(struct kiocb *iocb, struct iov_iter *to);
112static ssize_t sock_write_iter(struct kiocb *iocb, struct iov_iter *from);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700113static int sock_mmap(struct file *file, struct vm_area_struct *vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115static int sock_close(struct inode *inode, struct file *file);
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700116static __poll_t sock_poll(struct file *file,
117 struct poll_table_struct *wait);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700118static long sock_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
Shaun Pereira89bbfc92006-03-21 23:58:08 -0800119#ifdef CONFIG_COMPAT
120static long compat_sock_ioctl(struct file *file,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700121 unsigned int cmd, unsigned long arg);
Shaun Pereira89bbfc92006-03-21 23:58:08 -0800122#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123static int sock_fasync(int fd, struct file *filp, int on);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124static ssize_t sock_sendpage(struct file *file, struct page *page,
125 int offset, size_t size, loff_t *ppos, int more);
Jens Axboe9c55e012007-11-06 23:30:13 -0800126static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700127 struct pipe_inode_info *pipe, size_t len,
Jens Axboe9c55e012007-11-06 23:30:13 -0800128 unsigned int flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130/*
131 * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
132 * in the operation structures but are done directly via the socketcall() multiplexor.
133 */
134
Arjan van de Venda7071d2007-02-12 00:55:36 -0800135static const struct file_operations socket_file_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 .owner = THIS_MODULE,
137 .llseek = no_llseek,
Al Viro8ae5e0302014-11-28 19:40:50 -0500138 .read_iter = sock_read_iter,
139 .write_iter = sock_write_iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 .poll = sock_poll,
141 .unlocked_ioctl = sock_ioctl,
Shaun Pereira89bbfc92006-03-21 23:58:08 -0800142#ifdef CONFIG_COMPAT
143 .compat_ioctl = compat_sock_ioctl,
144#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 .mmap = sock_mmap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 .release = sock_close,
147 .fasync = sock_fasync,
Jens Axboe5274f052006-03-30 15:15:30 +0200148 .sendpage = sock_sendpage,
149 .splice_write = generic_splice_sendpage,
Jens Axboe9c55e012007-11-06 23:30:13 -0800150 .splice_read = sock_splice_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151};
152
153/*
154 * The protocol list. Each protocol is registered in here.
155 */
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157static DEFINE_SPINLOCK(net_family_lock);
Eric Dumazet190683a2010-11-10 10:50:44 +0000158static const struct net_proto_family __rcu *net_families[NPROTO] __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700161 * Support routines.
162 * Move socket addresses back and forth across the kernel/user
163 * divide and look after the messy bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 */
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166/**
167 * move_addr_to_kernel - copy a socket address into kernel space
168 * @uaddr: Address in user space
169 * @kaddr: Address in kernel space
170 * @ulen: Length in user space
171 *
172 * The address is copied into kernel space. If the provided address is
173 * too long an error code of -EINVAL is returned. If the copy gives
174 * invalid addresses -EFAULT is returned. On a success 0 is returned.
175 */
176
Maciej Żenczykowski43db3622012-03-11 12:51:50 +0000177int move_addr_to_kernel(void __user *uaddr, int ulen, struct sockaddr_storage *kaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -0700179 if (ulen < 0 || ulen > sizeof(struct sockaddr_storage))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700181 if (ulen == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 return 0;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700183 if (copy_from_user(kaddr, uaddr, ulen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 return -EFAULT;
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100185 return audit_sockaddr(ulen, kaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
187
188/**
189 * move_addr_to_user - copy an address to user space
190 * @kaddr: kernel space address
191 * @klen: length of address in kernel
192 * @uaddr: user space address
193 * @ulen: pointer to user length field
194 *
195 * The value pointed to by ulen on entry is the buffer length available.
196 * This is overwritten with the buffer space used. -EINVAL is returned
197 * if an overlong buffer is specified or a negative buffer size. -EFAULT
198 * is returned if either the buffer or the length field are not
199 * accessible.
200 * After copying the data up to the limit the user specifies, the true
201 * length of the data is written over the length limit the user
202 * specified. Zero is returned for a success.
203 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700204
Maciej Żenczykowski43db3622012-03-11 12:51:50 +0000205static int move_addr_to_user(struct sockaddr_storage *kaddr, int klen,
stephen hemminger11165f12010-10-18 14:27:29 +0000206 void __user *uaddr, int __user *ulen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
208 int err;
209 int len;
210
Hannes Frederic Sowa68c6beb2013-11-21 03:14:34 +0100211 BUG_ON(klen > sizeof(struct sockaddr_storage));
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700212 err = get_user(len, ulen);
213 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 return err;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700215 if (len > klen)
216 len = klen;
Hannes Frederic Sowa68c6beb2013-11-21 03:14:34 +0100217 if (len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700219 if (len) {
Steve Grubbd6fe3942006-03-30 12:20:22 -0500220 if (audit_sockaddr(klen, kaddr))
221 return -ENOMEM;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700222 if (copy_to_user(uaddr, kaddr, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 return -EFAULT;
224 }
225 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700226 * "fromlen shall refer to the value before truncation.."
227 * 1003.1g
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 */
229 return __put_user(klen, ulen);
230}
231
Alexey Dobriyan08009a72018-02-24 21:20:33 +0300232static struct kmem_cache *sock_inode_cachep __ro_after_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234static struct inode *sock_alloc_inode(struct super_block *sb)
235{
236 struct socket_alloc *ei;
Eric Dumazeteaefd1102011-02-18 03:26:36 +0000237 struct socket_wq *wq;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700238
Christoph Lametere94b1762006-12-06 20:33:17 -0800239 ei = kmem_cache_alloc(sock_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 if (!ei)
241 return NULL;
Eric Dumazeteaefd1102011-02-18 03:26:36 +0000242 wq = kmalloc(sizeof(*wq), GFP_KERNEL);
243 if (!wq) {
Eric Dumazet43815482010-04-29 11:01:49 +0000244 kmem_cache_free(sock_inode_cachep, ei);
245 return NULL;
246 }
Eric Dumazeteaefd1102011-02-18 03:26:36 +0000247 init_waitqueue_head(&wq->wait);
248 wq->fasync_list = NULL;
Nicolai Stange574aab12015-12-29 13:29:55 +0100249 wq->flags = 0;
Christoph Hellwige6476c22018-07-30 09:45:07 +0200250 ei->socket.wq = wq;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 ei->socket.state = SS_UNCONNECTED;
253 ei->socket.flags = 0;
254 ei->socket.ops = NULL;
255 ei->socket.sk = NULL;
256 ei->socket.file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 return &ei->vfs_inode;
259}
260
Al Viro6d7855c2019-07-05 20:13:22 +0100261static void sock_free_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
Eric Dumazet43815482010-04-29 11:01:49 +0000263 struct socket_alloc *ei;
264
265 ei = container_of(inode, struct socket_alloc, vfs_inode);
Al Viro6d7855c2019-07-05 20:13:22 +0100266 kfree(ei->socket.wq);
Eric Dumazet43815482010-04-29 11:01:49 +0000267 kmem_cache_free(sock_inode_cachep, ei);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268}
269
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700270static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700272 struct socket_alloc *ei = (struct socket_alloc *)foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Christoph Lametera35afb82007-05-16 22:10:57 -0700274 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700276
yuan linyu1e911632017-01-07 17:18:31 +0800277static void init_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
279 sock_inode_cachep = kmem_cache_create("sock_inode_cache",
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700280 sizeof(struct socket_alloc),
281 0,
282 (SLAB_HWCACHE_ALIGN |
283 SLAB_RECLAIM_ACCOUNT |
Vladimir Davydov5d097052016-01-14 15:18:21 -0800284 SLAB_MEM_SPREAD | SLAB_ACCOUNT),
Paul Mundt20c2df82007-07-20 10:11:58 +0900285 init_once);
yuan linyu1e911632017-01-07 17:18:31 +0800286 BUG_ON(sock_inode_cachep == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}
288
Alexey Dobriyanb87221d2009-09-21 17:01:09 -0700289static const struct super_operations sockfs_ops = {
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700290 .alloc_inode = sock_alloc_inode,
Al Viro6d7855c2019-07-05 20:13:22 +0100291 .free_inode = sock_free_inode,
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700292 .statfs = simple_statfs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293};
294
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700295/*
296 * sockfs_dname() is called from d_path().
297 */
298static char *sockfs_dname(struct dentry *dentry, char *buffer, int buflen)
299{
300 return dynamic_dname(dentry, buffer, buflen, "socket:[%lu]",
David Howellsc5ef6032015-03-17 22:26:16 +0000301 d_inode(dentry)->i_ino);
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700302}
303
Al Viro3ba13d12009-02-20 06:02:22 +0000304static const struct dentry_operations sockfs_dentry_operations = {
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700305 .d_dname = sockfs_dname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306};
307
Andreas Gruenbacherbba0bd32016-09-29 17:48:35 +0200308static int sockfs_xattr_get(const struct xattr_handler *handler,
309 struct dentry *dentry, struct inode *inode,
310 const char *suffix, void *value, size_t size)
311{
312 if (value) {
313 if (dentry->d_name.len + 1 > size)
314 return -ERANGE;
315 memcpy(value, dentry->d_name.name, dentry->d_name.len + 1);
316 }
317 return dentry->d_name.len + 1;
318}
319
320#define XATTR_SOCKPROTONAME_SUFFIX "sockprotoname"
321#define XATTR_NAME_SOCKPROTONAME (XATTR_SYSTEM_PREFIX XATTR_SOCKPROTONAME_SUFFIX)
322#define XATTR_NAME_SOCKPROTONAME_LEN (sizeof(XATTR_NAME_SOCKPROTONAME)-1)
323
324static const struct xattr_handler sockfs_xattr_handler = {
325 .name = XATTR_NAME_SOCKPROTONAME,
326 .get = sockfs_xattr_get,
327};
328
Andreas Gruenbacher4a590152016-11-13 21:23:34 +0100329static int sockfs_security_xattr_set(const struct xattr_handler *handler,
330 struct dentry *dentry, struct inode *inode,
331 const char *suffix, const void *value,
332 size_t size, int flags)
333{
334 /* Handled by LSM. */
335 return -EAGAIN;
336}
337
338static const struct xattr_handler sockfs_security_xattr_handler = {
339 .prefix = XATTR_SECURITY_PREFIX,
340 .set = sockfs_security_xattr_set,
341};
342
Andreas Gruenbacherbba0bd32016-09-29 17:48:35 +0200343static const struct xattr_handler *sockfs_xattr_handlers[] = {
344 &sockfs_xattr_handler,
Andreas Gruenbacher4a590152016-11-13 21:23:34 +0100345 &sockfs_security_xattr_handler,
Andreas Gruenbacherbba0bd32016-09-29 17:48:35 +0200346 NULL
347};
348
Al Viroc74a1cb2011-01-12 16:59:34 -0500349static struct dentry *sockfs_mount(struct file_system_type *fs_type,
350 int flags, const char *dev_name, void *data)
351{
Andreas Gruenbacherbba0bd32016-09-29 17:48:35 +0200352 return mount_pseudo_xattr(fs_type, "socket:", &sockfs_ops,
353 sockfs_xattr_handlers,
354 &sockfs_dentry_operations, SOCKFS_MAGIC);
Al Viroc74a1cb2011-01-12 16:59:34 -0500355}
356
357static struct vfsmount *sock_mnt __read_mostly;
358
359static struct file_system_type sock_fs_type = {
360 .name = "sockfs",
361 .mount = sockfs_mount,
362 .kill_sb = kill_anon_super,
363};
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365/*
366 * Obtains the first available file descriptor and sets it up for use.
367 *
David S. Miller39d8c1b2006-03-20 17:13:49 -0800368 * These functions create file structures and maps them to fd space
369 * of the current process. On success it returns file descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 * and file struct implicitly stored in sock->file.
371 * Note that another thread may close file descriptor before we return
372 * from this function. We use the fact that now we do not refer
373 * to socket after mapping. If one day we will need it, this
374 * function will increment ref. count on file by 1.
375 *
376 * In any case returned fd MAY BE not valid!
377 * This race condition is unavoidable
378 * with shared fd spaces, we cannot solve it inside kernel,
379 * but we take care of internal coherence yet.
380 */
381
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300382/**
383 * sock_alloc_file - Bind a &socket to a &file
384 * @sock: socket
385 * @flags: file status flags
386 * @dname: protocol name
387 *
388 * Returns the &file bound with @sock, implicitly storing it
389 * in sock->file. If dname is %NULL, sets to "".
390 * On failure the return is a ERR pointer (see linux/err.h).
391 * This function uses GFP_KERNEL internally.
392 */
393
Linus Torvaldsaab174f2012-10-02 20:25:04 -0700394struct file *sock_alloc_file(struct socket *sock, int flags, const char *dname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
Al Viro7cbe66b2009-08-05 19:59:08 +0400396 struct file *file;
David S. Miller39d8c1b2006-03-20 17:13:49 -0800397
Al Virod93aa9d2018-06-09 09:40:05 -0400398 if (!dname)
399 dname = sock->sk ? sock->sk->sk_prot_creator->name : "";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Al Virod93aa9d2018-06-09 09:40:05 -0400401 file = alloc_file_pseudo(SOCK_INODE(sock), sock_mnt, dname,
402 O_RDWR | (flags & O_NONBLOCK),
403 &socket_file_ops);
Viresh Kumarb5ffe632015-08-12 15:59:47 +0530404 if (IS_ERR(file)) {
Al Viro8e1611e2017-12-05 23:29:09 +0000405 sock_release(sock);
Anatol Pomozov39b65252012-09-12 20:11:55 -0700406 return file;
Al Virocc3808f2009-08-06 09:43:59 +0400407 }
David S. Miller39d8c1b2006-03-20 17:13:49 -0800408
409 sock->file = file;
David S. Miller39d8c1b2006-03-20 17:13:49 -0800410 file->private_data = sock;
Al Viro28407632012-08-17 23:54:15 -0400411 return file;
David S. Miller39d8c1b2006-03-20 17:13:49 -0800412}
Al Viro56b31d12012-08-18 00:25:51 -0400413EXPORT_SYMBOL(sock_alloc_file);
David S. Miller39d8c1b2006-03-20 17:13:49 -0800414
Al Viro56b31d12012-08-18 00:25:51 -0400415static int sock_map_fd(struct socket *sock, int flags)
David S. Miller39d8c1b2006-03-20 17:13:49 -0800416{
417 struct file *newfile;
Al Viro28407632012-08-17 23:54:15 -0400418 int fd = get_unused_fd_flags(flags);
Al Viroce4bb042018-01-10 18:47:05 -0500419 if (unlikely(fd < 0)) {
420 sock_release(sock);
Al Viro28407632012-08-17 23:54:15 -0400421 return fd;
Al Viroce4bb042018-01-10 18:47:05 -0500422 }
David S. Miller39d8c1b2006-03-20 17:13:49 -0800423
Linus Torvaldsaab174f2012-10-02 20:25:04 -0700424 newfile = sock_alloc_file(sock, flags, NULL);
Enrico Weigelt4546e442019-06-05 22:58:50 +0200425 if (!IS_ERR(newfile)) {
David S. Miller39d8c1b2006-03-20 17:13:49 -0800426 fd_install(fd, newfile);
Al Viro28407632012-08-17 23:54:15 -0400427 return fd;
428 }
Al Viro7cbe66b2009-08-05 19:59:08 +0400429
Al Viro28407632012-08-17 23:54:15 -0400430 put_unused_fd(fd);
431 return PTR_ERR(newfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
433
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300434/**
435 * sock_from_file - Return the &socket bounded to @file.
436 * @file: file
437 * @err: pointer to an error code return
438 *
439 * On failure returns %NULL and assigns -ENOTSOCK to @err.
440 */
441
John Fastabend406a3c62012-07-20 10:39:25 +0000442struct socket *sock_from_file(struct file *file, int *err)
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800443{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800444 if (file->f_op == &socket_file_ops)
445 return file->private_data; /* set in sock_map_fd */
446
Eric Dumazet23bb80d2007-02-08 14:59:57 -0800447 *err = -ENOTSOCK;
448 return NULL;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800449}
John Fastabend406a3c62012-07-20 10:39:25 +0000450EXPORT_SYMBOL(sock_from_file);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452/**
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700453 * sockfd_lookup - Go from a file number to its socket slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 * @fd: file handle
455 * @err: pointer to an error code return
456 *
457 * The file handle passed in is locked and the socket it is bound
Rosen, Rami241c4662017-05-21 22:12:38 +0300458 * to is returned. If an error occurs the err pointer is overwritten
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 * with a negative errno code and NULL is returned. The function checks
460 * for both invalid handles and passing a handle which is not a socket.
461 *
462 * On a success the socket object pointer is returned.
463 */
464
465struct socket *sockfd_lookup(int fd, int *err)
466{
467 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 struct socket *sock;
469
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700470 file = fget(fd);
471 if (!file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 *err = -EBADF;
473 return NULL;
474 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700475
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800476 sock = sock_from_file(file, err);
477 if (!sock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 fput(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 return sock;
480}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700481EXPORT_SYMBOL(sockfd_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800483static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed)
484{
Al Viro00e188e2014-03-03 23:48:18 -0500485 struct fd f = fdget(fd);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800486 struct socket *sock;
487
Hua Zhong36725582006-04-19 15:25:02 -0700488 *err = -EBADF;
Al Viro00e188e2014-03-03 23:48:18 -0500489 if (f.file) {
490 sock = sock_from_file(f.file, err);
491 if (likely(sock)) {
492 *fput_needed = f.flags;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800493 return sock;
Al Viro00e188e2014-03-03 23:48:18 -0500494 }
495 fdput(f);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800496 }
497 return NULL;
498}
499
Masatake YAMATO600e1772012-08-29 10:44:29 +0000500static ssize_t sockfs_listxattr(struct dentry *dentry, char *buffer,
501 size_t size)
502{
503 ssize_t len;
504 ssize_t used = 0;
505
David Howellsc5ef6032015-03-17 22:26:16 +0000506 len = security_inode_listsecurity(d_inode(dentry), buffer, size);
Masatake YAMATO600e1772012-08-29 10:44:29 +0000507 if (len < 0)
508 return len;
509 used += len;
510 if (buffer) {
511 if (size < used)
512 return -ERANGE;
513 buffer += len;
514 }
515
516 len = (XATTR_NAME_SOCKPROTONAME_LEN + 1);
517 used += len;
518 if (buffer) {
519 if (size < used)
520 return -ERANGE;
521 memcpy(buffer, XATTR_NAME_SOCKPROTONAME, len);
522 buffer += len;
523 }
524
525 return used;
526}
527
Tobias Klauserdc647ec2017-01-10 09:30:51 +0100528static int sockfs_setattr(struct dentry *dentry, struct iattr *iattr)
Lorenzo Colitti86741ec2016-11-04 02:23:41 +0900529{
530 int err = simple_setattr(dentry, iattr);
531
Eric Biggerse1a3a602016-12-30 17:42:32 -0600532 if (!err && (iattr->ia_valid & ATTR_UID)) {
Lorenzo Colitti86741ec2016-11-04 02:23:41 +0900533 struct socket *sock = SOCKET_I(d_inode(dentry));
534
Cong Wang6d8c50d2018-06-07 13:39:49 -0700535 if (sock->sk)
536 sock->sk->sk_uid = iattr->ia_uid;
537 else
538 err = -ENOENT;
Lorenzo Colitti86741ec2016-11-04 02:23:41 +0900539 }
540
541 return err;
542}
543
Masatake YAMATO600e1772012-08-29 10:44:29 +0000544static const struct inode_operations sockfs_inode_ops = {
Masatake YAMATO600e1772012-08-29 10:44:29 +0000545 .listxattr = sockfs_listxattr,
Lorenzo Colitti86741ec2016-11-04 02:23:41 +0900546 .setattr = sockfs_setattr,
Masatake YAMATO600e1772012-08-29 10:44:29 +0000547};
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549/**
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300550 * sock_alloc - allocate a socket
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700551 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 * Allocate a new inode and socket object. The two are bound together
553 * and initialised. The socket is then returned. If we are out of inodes
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300554 * NULL is returned. This functions uses GFP_KERNEL internally.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 */
556
Tom Herbertf4a00aa2016-03-07 14:11:01 -0800557struct socket *sock_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700559 struct inode *inode;
560 struct socket *sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Eric Dumazeta209dfc2011-07-26 11:36:34 +0200562 inode = new_inode_pseudo(sock_mnt->mnt_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 if (!inode)
564 return NULL;
565
566 sock = SOCKET_I(inode);
567
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400568 inode->i_ino = get_next_ino();
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700569 inode->i_mode = S_IFSOCK | S_IRWXUGO;
David Howells8192b0c2008-11-14 10:39:10 +1100570 inode->i_uid = current_fsuid();
571 inode->i_gid = current_fsgid();
Masatake YAMATO600e1772012-08-29 10:44:29 +0000572 inode->i_op = &sockfs_inode_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 return sock;
575}
Tom Herbertf4a00aa2016-03-07 14:11:01 -0800576EXPORT_SYMBOL(sock_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578/**
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300579 * sock_release - close a socket
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 * @sock: socket to close
581 *
582 * The socket is released from the protocol stack if it has a release
583 * callback, and the inode is then released if the socket is bound to
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700584 * an inode not a file.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700586
Cong Wang6d8c50d2018-06-07 13:39:49 -0700587static void __sock_release(struct socket *sock, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
589 if (sock->ops) {
590 struct module *owner = sock->ops->owner;
591
Cong Wang6d8c50d2018-06-07 13:39:49 -0700592 if (inode)
593 inode_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 sock->ops->release(sock);
Eric Biggersff7b11a2019-02-21 14:13:56 -0800595 sock->sk = NULL;
Cong Wang6d8c50d2018-06-07 13:39:49 -0700596 if (inode)
597 inode_unlock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 sock->ops = NULL;
599 module_put(owner);
600 }
601
Christoph Hellwige6476c22018-07-30 09:45:07 +0200602 if (sock->wq->fasync_list)
Yang Yingliang3410f222014-02-12 17:09:55 +0800603 pr_err("%s: fasync list not empty!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 if (!sock->file) {
606 iput(SOCK_INODE(sock));
607 return;
608 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700609 sock->file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610}
Cong Wang6d8c50d2018-06-07 13:39:49 -0700611
612void sock_release(struct socket *sock)
613{
614 __sock_release(sock, NULL);
615}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700616EXPORT_SYMBOL(sock_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -0400618void __sock_tx_timestamp(__u16 tsflags, __u8 *tx_flags)
Patrick Ohly20d49472009-02-12 05:03:38 +0000619{
Eric Dumazet140c55d2014-08-06 11:49:29 +0200620 u8 flags = *tx_flags;
621
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -0400622 if (tsflags & SOF_TIMESTAMPING_TX_HARDWARE)
Eric Dumazet140c55d2014-08-06 11:49:29 +0200623 flags |= SKBTX_HW_TSTAMP;
624
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -0400625 if (tsflags & SOF_TIMESTAMPING_TX_SOFTWARE)
Eric Dumazet140c55d2014-08-06 11:49:29 +0200626 flags |= SKBTX_SW_TSTAMP;
627
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -0400628 if (tsflags & SOF_TIMESTAMPING_TX_SCHED)
Eric Dumazet140c55d2014-08-06 11:49:29 +0200629 flags |= SKBTX_SCHED_TSTAMP;
630
Eric Dumazet140c55d2014-08-06 11:49:29 +0200631 *tx_flags = flags;
Patrick Ohly20d49472009-02-12 05:03:38 +0000632}
Willem de Bruijn67cc0d42014-09-08 19:58:58 -0400633EXPORT_SYMBOL(__sock_tx_timestamp);
Patrick Ohly20d49472009-02-12 05:03:38 +0000634
Paolo Abeni8c3c4472019-05-03 17:01:39 +0200635INDIRECT_CALLABLE_DECLARE(int inet_sendmsg(struct socket *, struct msghdr *,
636 size_t));
Paolo Abenia648a592019-07-03 16:06:54 +0200637INDIRECT_CALLABLE_DECLARE(int inet6_sendmsg(struct socket *, struct msghdr *,
638 size_t));
Al Virod8725c82014-12-11 00:02:50 -0500639static inline int sock_sendmsg_nosec(struct socket *sock, struct msghdr *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
Paolo Abenia648a592019-07-03 16:06:54 +0200641 int ret = INDIRECT_CALL_INET(sock->ops->sendmsg, inet6_sendmsg,
642 inet_sendmsg, sock, msg,
643 msg_data_left(msg));
Al Virod8725c82014-12-11 00:02:50 -0500644 BUG_ON(ret == -EIOCBQUEUED);
645 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646}
Gu Zheng0cf00c62014-12-05 15:14:23 +0800647
Randy Dunlap85806af2019-05-18 21:23:07 -0700648/**
649 * sock_sendmsg - send a message through @sock
650 * @sock: socket
651 * @msg: message to send
652 *
653 * Sends @msg through @sock, passing through LSM.
654 * Returns the number of bytes sent, or an error code.
655 */
Al Virod8725c82014-12-11 00:02:50 -0500656int sock_sendmsg(struct socket *sock, struct msghdr *msg)
Gu Zheng0cf00c62014-12-05 15:14:23 +0800657{
Al Virod8725c82014-12-11 00:02:50 -0500658 int err = security_socket_sendmsg(sock, msg,
Al Viro01e97e62014-12-15 21:39:31 -0500659 msg_data_left(msg));
Ying Xue1b784142015-03-02 15:37:48 +0800660
Al Virod8725c82014-12-11 00:02:50 -0500661 return err ?: sock_sendmsg_nosec(sock, msg);
Gu Zheng0cf00c62014-12-05 15:14:23 +0800662}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700663EXPORT_SYMBOL(sock_sendmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300665/**
666 * kernel_sendmsg - send a message through @sock (kernel-space)
667 * @sock: socket
668 * @msg: message header
669 * @vec: kernel vec
670 * @num: vec array length
671 * @size: total message data size
672 *
673 * Builds the message data with @vec and sends it through @sock.
674 * Returns the number of bytes sent, or an error code.
675 */
676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
678 struct kvec *vec, size_t num, size_t size)
679{
David Howellsaa563d72018-10-20 00:57:56 +0100680 iov_iter_kvec(&msg->msg_iter, WRITE, vec, num, size);
Al Virod8725c82014-12-11 00:02:50 -0500681 return sock_sendmsg(sock, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700683EXPORT_SYMBOL(kernel_sendmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300685/**
686 * kernel_sendmsg_locked - send a message through @sock (kernel-space)
687 * @sk: sock
688 * @msg: message header
689 * @vec: output s/g array
690 * @num: output s/g array length
691 * @size: total message data size
692 *
693 * Builds the message data with @vec and sends it through @sock.
694 * Returns the number of bytes sent, or an error code.
695 * Caller must hold @sk.
696 */
697
Tom Herbert306b13e2017-07-28 16:22:41 -0700698int kernel_sendmsg_locked(struct sock *sk, struct msghdr *msg,
699 struct kvec *vec, size_t num, size_t size)
700{
701 struct socket *sock = sk->sk_socket;
702
703 if (!sock->ops->sendmsg_locked)
John Fastabenddb5980d2017-08-15 22:31:34 -0700704 return sock_no_sendmsg_locked(sk, msg, size);
Tom Herbert306b13e2017-07-28 16:22:41 -0700705
David Howellsaa563d72018-10-20 00:57:56 +0100706 iov_iter_kvec(&msg->msg_iter, WRITE, vec, num, size);
Tom Herbert306b13e2017-07-28 16:22:41 -0700707
708 return sock->ops->sendmsg_locked(sk, msg, msg_data_left(msg));
709}
710EXPORT_SYMBOL(kernel_sendmsg_locked);
711
Soheil Hassas Yeganeh8605330a2017-03-18 17:02:59 -0400712static bool skb_is_err_queue(const struct sk_buff *skb)
713{
714 /* pkt_type of skbs enqueued on the error queue are set to
715 * PACKET_OUTGOING in skb_set_err_queue(). This is only safe to do
716 * in recvmsg, since skbs received on a local socket will never
717 * have a pkt_type of PACKET_OUTGOING.
718 */
719 return skb->pkt_type == PACKET_OUTGOING;
720}
721
Miroslav Lichvarb50a5c72017-05-19 17:52:40 +0200722/* On transmit, software and hardware timestamps are returned independently.
723 * As the two skb clones share the hardware timestamp, which may be updated
724 * before the software timestamp is received, a hardware TX timestamp may be
725 * returned only if there is no software TX timestamp. Ignore false software
726 * timestamps, which may be made in the __sock_recv_timestamp() call when the
Deepa Dinamani7f1bc6e2019-02-02 07:34:46 -0800727 * option SO_TIMESTAMP_OLD(NS) is enabled on the socket, even when the skb has a
Miroslav Lichvarb50a5c72017-05-19 17:52:40 +0200728 * hardware timestamp.
729 */
730static bool skb_is_swtx_tstamp(const struct sk_buff *skb, int false_tstamp)
731{
732 return skb->tstamp && !false_tstamp && skb_is_err_queue(skb);
733}
734
Miroslav Lichvaraad9c8c2017-05-19 17:52:38 +0200735static void put_ts_pktinfo(struct msghdr *msg, struct sk_buff *skb)
736{
737 struct scm_ts_pktinfo ts_pktinfo;
738 struct net_device *orig_dev;
739
740 if (!skb_mac_header_was_set(skb))
741 return;
742
743 memset(&ts_pktinfo, 0, sizeof(ts_pktinfo));
744
745 rcu_read_lock();
746 orig_dev = dev_get_by_napi_id(skb_napi_id(skb));
747 if (orig_dev)
748 ts_pktinfo.if_index = orig_dev->ifindex;
749 rcu_read_unlock();
750
751 ts_pktinfo.pkt_length = skb->len - skb_mac_offset(skb);
752 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPING_PKTINFO,
753 sizeof(ts_pktinfo), &ts_pktinfo);
754}
755
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700756/*
757 * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
758 */
759void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
760 struct sk_buff *skb)
761{
Patrick Ohly20d49472009-02-12 05:03:38 +0000762 int need_software_tstamp = sock_flag(sk, SOCK_RCVTSTAMP);
Deepa Dinamani887feae2019-02-02 07:34:50 -0800763 int new_tstamp = sock_flag(sk, SOCK_TSTAMP_NEW);
Deepa Dinamani97184752019-02-02 07:34:51 -0800764 struct scm_timestamping_internal tss;
765
Miroslav Lichvarb50a5c72017-05-19 17:52:40 +0200766 int empty = 1, false_tstamp = 0;
Patrick Ohly20d49472009-02-12 05:03:38 +0000767 struct skb_shared_hwtstamps *shhwtstamps =
768 skb_hwtstamps(skb);
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700769
Patrick Ohly20d49472009-02-12 05:03:38 +0000770 /* Race occurred between timestamp enabling and packet
771 receiving. Fill in the current time for now. */
Miroslav Lichvarb50a5c72017-05-19 17:52:40 +0200772 if (need_software_tstamp && skb->tstamp == 0) {
Patrick Ohly20d49472009-02-12 05:03:38 +0000773 __net_timestamp(skb);
Miroslav Lichvarb50a5c72017-05-19 17:52:40 +0200774 false_tstamp = 1;
775 }
Patrick Ohly20d49472009-02-12 05:03:38 +0000776
777 if (need_software_tstamp) {
778 if (!sock_flag(sk, SOCK_RCVTSTAMPNS)) {
Deepa Dinamani887feae2019-02-02 07:34:50 -0800779 if (new_tstamp) {
780 struct __kernel_sock_timeval tv;
781
782 skb_get_new_timestamp(skb, &tv);
783 put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMP_NEW,
784 sizeof(tv), &tv);
785 } else {
786 struct __kernel_old_timeval tv;
787
788 skb_get_timestamp(skb, &tv);
789 put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMP_OLD,
790 sizeof(tv), &tv);
791 }
Patrick Ohly20d49472009-02-12 05:03:38 +0000792 } else {
Deepa Dinamani887feae2019-02-02 07:34:50 -0800793 if (new_tstamp) {
794 struct __kernel_timespec ts;
795
796 skb_get_new_timestampns(skb, &ts);
797 put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMPNS_NEW,
798 sizeof(ts), &ts);
799 } else {
800 struct timespec ts;
801
802 skb_get_timestampns(skb, &ts);
803 put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMPNS_OLD,
804 sizeof(ts), &ts);
805 }
Patrick Ohly20d49472009-02-12 05:03:38 +0000806 }
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700807 }
Patrick Ohly20d49472009-02-12 05:03:38 +0000808
Willem de Bruijnf24b9be2014-08-04 22:11:45 -0400809 memset(&tss, 0, sizeof(tss));
Willem de Bruijnc1991052014-09-03 12:01:18 -0400810 if ((sk->sk_tsflags & SOF_TIMESTAMPING_SOFTWARE) &&
Deepa Dinamani97184752019-02-02 07:34:51 -0800811 ktime_to_timespec64_cond(skb->tstamp, tss.ts + 0))
Patrick Ohly20d49472009-02-12 05:03:38 +0000812 empty = 0;
Willem de Bruijn4d276eb2014-07-25 18:01:32 -0400813 if (shhwtstamps &&
Willem de Bruijnb9f40e22014-08-04 22:11:46 -0400814 (sk->sk_tsflags & SOF_TIMESTAMPING_RAW_HARDWARE) &&
Miroslav Lichvarb50a5c72017-05-19 17:52:40 +0200815 !skb_is_swtx_tstamp(skb, false_tstamp) &&
Deepa Dinamani97184752019-02-02 07:34:51 -0800816 ktime_to_timespec64_cond(shhwtstamps->hwtstamp, tss.ts + 2)) {
Willem de Bruijn4d276eb2014-07-25 18:01:32 -0400817 empty = 0;
Miroslav Lichvaraad9c8c2017-05-19 17:52:38 +0200818 if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_PKTINFO) &&
819 !skb_is_err_queue(skb))
820 put_ts_pktinfo(msg, skb);
821 }
Francis Yan1c885802016-11-27 23:07:18 -0800822 if (!empty) {
Deepa Dinamani97184752019-02-02 07:34:51 -0800823 if (sock_flag(sk, SOCK_TSTAMP_NEW))
824 put_cmsg_scm_timestamping64(msg, &tss);
825 else
826 put_cmsg_scm_timestamping(msg, &tss);
Francis Yan1c885802016-11-27 23:07:18 -0800827
Soheil Hassas Yeganeh8605330a2017-03-18 17:02:59 -0400828 if (skb_is_err_queue(skb) && skb->len &&
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -0400829 SKB_EXT_ERR(skb)->opt_stats)
Francis Yan1c885802016-11-27 23:07:18 -0800830 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPING_OPT_STATS,
831 skb->len, skb->data);
832 }
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700833}
Arnaldo Carvalho de Melo7c81fd82007-03-10 00:39:35 -0300834EXPORT_SYMBOL_GPL(__sock_recv_timestamp);
835
Johannes Berg6e3e9392011-11-09 10:15:42 +0100836void __sock_recv_wifi_status(struct msghdr *msg, struct sock *sk,
837 struct sk_buff *skb)
838{
839 int ack;
840
841 if (!sock_flag(sk, SOCK_WIFI_STATUS))
842 return;
843 if (!skb->wifi_acked_valid)
844 return;
845
846 ack = skb->wifi_acked;
847
848 put_cmsg(msg, SOL_SOCKET, SCM_WIFI_STATUS, sizeof(ack), &ack);
849}
850EXPORT_SYMBOL_GPL(__sock_recv_wifi_status);
851
stephen hemminger11165f12010-10-18 14:27:29 +0000852static inline void sock_recv_drops(struct msghdr *msg, struct sock *sk,
853 struct sk_buff *skb)
Neil Horman3b885782009-10-12 13:26:31 -0700854{
Eyal Birger744d5a32015-03-01 14:58:31 +0200855 if (sock_flag(sk, SOCK_RXQ_OVFL) && skb && SOCK_SKB_CB(skb)->dropcount)
Neil Horman3b885782009-10-12 13:26:31 -0700856 put_cmsg(msg, SOL_SOCKET, SO_RXQ_OVFL,
Eyal Birger744d5a32015-03-01 14:58:31 +0200857 sizeof(__u32), &SOCK_SKB_CB(skb)->dropcount);
Neil Horman3b885782009-10-12 13:26:31 -0700858}
859
Eric Dumazet767dd032010-04-28 19:14:43 +0000860void __sock_recv_ts_and_drops(struct msghdr *msg, struct sock *sk,
Neil Horman3b885782009-10-12 13:26:31 -0700861 struct sk_buff *skb)
862{
863 sock_recv_timestamp(msg, sk, skb);
864 sock_recv_drops(msg, sk, skb);
865}
Eric Dumazet767dd032010-04-28 19:14:43 +0000866EXPORT_SYMBOL_GPL(__sock_recv_ts_and_drops);
Neil Horman3b885782009-10-12 13:26:31 -0700867
Paolo Abeni8c3c4472019-05-03 17:01:39 +0200868INDIRECT_CALLABLE_DECLARE(int inet_recvmsg(struct socket *, struct msghdr *,
Paolo Abenia648a592019-07-03 16:06:54 +0200869 size_t, int));
870INDIRECT_CALLABLE_DECLARE(int inet6_recvmsg(struct socket *, struct msghdr *,
871 size_t, int));
Ying Xue1b784142015-03-02 15:37:48 +0800872static inline int sock_recvmsg_nosec(struct socket *sock, struct msghdr *msg,
Al Viro2da62902015-03-14 21:13:46 -0400873 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
Paolo Abenia648a592019-07-03 16:06:54 +0200875 return INDIRECT_CALL_INET(sock->ops->recvmsg, inet6_recvmsg,
876 inet_recvmsg, sock, msg, msg_data_left(msg),
877 flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878}
879
Randy Dunlap85806af2019-05-18 21:23:07 -0700880/**
881 * sock_recvmsg - receive a message from @sock
882 * @sock: socket
883 * @msg: message to receive
884 * @flags: message flags
885 *
886 * Receives @msg from @sock, passing through LSM. Returns the total number
887 * of bytes received, or an error.
888 */
Al Viro2da62902015-03-14 21:13:46 -0400889int sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -0700890{
Al Viro2da62902015-03-14 21:13:46 -0400891 int err = security_socket_recvmsg(sock, msg, msg_data_left(msg), flags);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -0700892
Al Viro2da62902015-03-14 21:13:46 -0400893 return err ?: sock_recvmsg_nosec(sock, msg, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700895EXPORT_SYMBOL(sock_recvmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Martin Lucinac1249c0a2010-12-10 00:04:05 +0000897/**
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300898 * kernel_recvmsg - Receive a message from a socket (kernel space)
899 * @sock: The socket to receive the message from
900 * @msg: Received message
901 * @vec: Input s/g array for message data
902 * @num: Size of input s/g array
903 * @size: Number of bytes to read
904 * @flags: Message flags (MSG_DONTWAIT, etc...)
Martin Lucinac1249c0a2010-12-10 00:04:05 +0000905 *
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300906 * On return the msg structure contains the scatter/gather array passed in the
907 * vec argument. The array is modified so that it consists of the unfilled
908 * portion of the original array.
Martin Lucinac1249c0a2010-12-10 00:04:05 +0000909 *
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300910 * The returned value is the total number of bytes received, or an error.
Martin Lucinac1249c0a2010-12-10 00:04:05 +0000911 */
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300912
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700913int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
914 struct kvec *vec, size_t num, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
916 mm_segment_t oldfs = get_fs();
917 int result;
918
David Howellsaa563d72018-10-20 00:57:56 +0100919 iov_iter_kvec(&msg->msg_iter, READ, vec, num, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 set_fs(KERNEL_DS);
Al Viro2da62902015-03-14 21:13:46 -0400921 result = sock_recvmsg(sock, msg, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 set_fs(oldfs);
923 return result;
924}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700925EXPORT_SYMBOL(kernel_recvmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -0300927static ssize_t sock_sendpage(struct file *file, struct page *page,
928 int offset, size_t size, loff_t *ppos, int more)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
930 struct socket *sock;
931 int flags;
932
Eric Dumazetb69aee02005-09-06 14:42:45 -0700933 sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Eric Dumazet35f9c092012-04-05 03:05:35 +0000935 flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
936 /* more is a combination of MSG_MORE and MSG_SENDPAGE_NOTLAST */
937 flags |= more;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Linus Torvaldse6949582009-08-13 08:28:36 -0700939 return kernel_sendpage(sock, page, offset, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940}
941
Jens Axboe9c55e012007-11-06 23:30:13 -0800942static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700943 struct pipe_inode_info *pipe, size_t len,
Jens Axboe9c55e012007-11-06 23:30:13 -0800944 unsigned int flags)
945{
946 struct socket *sock = file->private_data;
947
Rémi Denis-Courmont997b37d2008-02-15 02:35:45 -0800948 if (unlikely(!sock->ops->splice_read))
Slavomir Kaslev95506582018-11-16 11:27:53 +0200949 return generic_file_splice_read(file, ppos, pipe, len, flags);
Rémi Denis-Courmont997b37d2008-02-15 02:35:45 -0800950
Jens Axboe9c55e012007-11-06 23:30:13 -0800951 return sock->ops->splice_read(sock, ppos, pipe, len, flags);
952}
953
Al Viro8ae5e0302014-11-28 19:40:50 -0500954static ssize_t sock_read_iter(struct kiocb *iocb, struct iov_iter *to)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800955{
Al Viro6d652332015-01-30 16:12:56 -0500956 struct file *file = iocb->ki_filp;
957 struct socket *sock = file->private_data;
tadeusz.struk@intel.com0345f932015-03-19 12:31:25 -0700958 struct msghdr msg = {.msg_iter = *to,
959 .msg_iocb = iocb};
Al Viro8ae5e0302014-11-28 19:40:50 -0500960 ssize_t res;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800961
Al Viro8ae5e0302014-11-28 19:40:50 -0500962 if (file->f_flags & O_NONBLOCK)
963 msg.msg_flags = MSG_DONTWAIT;
964
965 if (iocb->ki_pos != 0)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800966 return -ESPIPE;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700967
Christoph Hellwig66ee59a2015-02-11 19:56:46 +0100968 if (!iov_iter_count(to)) /* Match SYS5 behaviour */
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800969 return 0;
970
Al Viro2da62902015-03-14 21:13:46 -0400971 res = sock_recvmsg(sock, &msg, msg.msg_flags);
Al Viro8ae5e0302014-11-28 19:40:50 -0500972 *to = msg.msg_iter;
973 return res;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800974}
975
Al Viro8ae5e0302014-11-28 19:40:50 -0500976static ssize_t sock_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977{
Al Viro6d652332015-01-30 16:12:56 -0500978 struct file *file = iocb->ki_filp;
979 struct socket *sock = file->private_data;
tadeusz.struk@intel.com0345f932015-03-19 12:31:25 -0700980 struct msghdr msg = {.msg_iter = *from,
981 .msg_iocb = iocb};
Al Viro8ae5e0302014-11-28 19:40:50 -0500982 ssize_t res;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800983
Al Viro8ae5e0302014-11-28 19:40:50 -0500984 if (iocb->ki_pos != 0)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800985 return -ESPIPE;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700986
Al Viro8ae5e0302014-11-28 19:40:50 -0500987 if (file->f_flags & O_NONBLOCK)
988 msg.msg_flags = MSG_DONTWAIT;
989
Al Viro6d652332015-01-30 16:12:56 -0500990 if (sock->type == SOCK_SEQPACKET)
991 msg.msg_flags |= MSG_EOR;
992
Al Virod8725c82014-12-11 00:02:50 -0500993 res = sock_sendmsg(sock, &msg);
Al Viro8ae5e0302014-11-28 19:40:50 -0500994 *from = msg.msg_iter;
995 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996}
997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998/*
999 * Atomic setting of ioctl hooks to avoid race
1000 * with module unload.
1001 */
1002
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001003static DEFINE_MUTEX(br_ioctl_mutex);
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001004static int (*br_ioctl_hook) (struct net *, unsigned int cmd, void __user *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Eric W. Biederman881d9662007-09-17 11:56:21 -07001006void brioctl_set(int (*hook) (struct net *, unsigned int, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001008 mutex_lock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 br_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001010 mutex_unlock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011}
1012EXPORT_SYMBOL(brioctl_set);
1013
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001014static DEFINE_MUTEX(vlan_ioctl_mutex);
Eric W. Biederman881d9662007-09-17 11:56:21 -07001015static int (*vlan_ioctl_hook) (struct net *, void __user *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
Eric W. Biederman881d9662007-09-17 11:56:21 -07001017void vlan_ioctl_set(int (*hook) (struct net *, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001019 mutex_lock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 vlan_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001021 mutex_unlock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022}
1023EXPORT_SYMBOL(vlan_ioctl_set);
1024
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001025static DEFINE_MUTEX(dlci_ioctl_mutex);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001026static int (*dlci_ioctl_hook) (unsigned int, void __user *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001028void dlci_ioctl_set(int (*hook) (unsigned int, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001030 mutex_lock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 dlci_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001032 mutex_unlock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033}
1034EXPORT_SYMBOL(dlci_ioctl_set);
1035
Arnd Bergmann6b960182009-11-06 23:10:54 -08001036static long sock_do_ioctl(struct net *net, struct socket *sock,
Johannes Berg63ff03a2019-01-25 22:43:17 +01001037 unsigned int cmd, unsigned long arg)
Arnd Bergmann6b960182009-11-06 23:10:54 -08001038{
1039 int err;
1040 void __user *argp = (void __user *)arg;
1041
1042 err = sock->ops->ioctl(sock, cmd, arg);
1043
1044 /*
1045 * If this ioctl is unknown try to hand it down
1046 * to the NIC driver.
1047 */
Al Viro36fd6332017-06-26 13:19:16 -04001048 if (err != -ENOIOCTLCMD)
1049 return err;
Arnd Bergmann6b960182009-11-06 23:10:54 -08001050
Al Viro36fd6332017-06-26 13:19:16 -04001051 if (cmd == SIOCGIFCONF) {
1052 struct ifconf ifc;
1053 if (copy_from_user(&ifc, argp, sizeof(struct ifconf)))
1054 return -EFAULT;
1055 rtnl_lock();
1056 err = dev_ifconf(net, &ifc, sizeof(struct ifreq));
1057 rtnl_unlock();
1058 if (!err && copy_to_user(argp, &ifc, sizeof(struct ifconf)))
1059 err = -EFAULT;
Al Viro44c02a22017-10-05 12:59:44 -04001060 } else {
1061 struct ifreq ifr;
1062 bool need_copyout;
Johannes Berg63ff03a2019-01-25 22:43:17 +01001063 if (copy_from_user(&ifr, argp, sizeof(struct ifreq)))
Al Viro44c02a22017-10-05 12:59:44 -04001064 return -EFAULT;
1065 err = dev_ioctl(net, cmd, &ifr, &need_copyout);
1066 if (!err && need_copyout)
Johannes Berg63ff03a2019-01-25 22:43:17 +01001067 if (copy_to_user(argp, &ifr, sizeof(struct ifreq)))
Al Viro44c02a22017-10-05 12:59:44 -04001068 return -EFAULT;
Al Viro36fd6332017-06-26 13:19:16 -04001069 }
Arnd Bergmann6b960182009-11-06 23:10:54 -08001070 return err;
1071}
1072
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073/*
1074 * With an ioctl, arg may well be a user mode pointer, but we don't know
1075 * what to do with it - that's up to the protocol still.
1076 */
1077
Pedro Tammela8a3c2452019-03-14 10:45:23 -03001078/**
1079 * get_net_ns - increment the refcount of the network namespace
1080 * @ns: common namespace (net)
1081 *
1082 * Returns the net's common namespace.
1083 */
1084
Kirill Tkhaid8d211a2018-02-14 16:39:56 +03001085struct ns_common *get_net_ns(struct ns_common *ns)
Andrey Vaginc62cce22016-10-24 18:29:13 -07001086{
1087 return &get_net(container_of(ns, struct net, ns))->ns;
1088}
Kirill Tkhaid8d211a2018-02-14 16:39:56 +03001089EXPORT_SYMBOL_GPL(get_net_ns);
Andrey Vaginc62cce22016-10-24 18:29:13 -07001090
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
1092{
1093 struct socket *sock;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001094 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 void __user *argp = (void __user *)arg;
1096 int pid, err;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001097 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Eric Dumazetb69aee02005-09-06 14:42:45 -07001099 sock = file->private_data;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001100 sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001101 net = sock_net(sk);
Al Viro44c02a22017-10-05 12:59:44 -04001102 if (unlikely(cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15))) {
1103 struct ifreq ifr;
1104 bool need_copyout;
1105 if (copy_from_user(&ifr, argp, sizeof(struct ifreq)))
1106 return -EFAULT;
1107 err = dev_ioctl(net, cmd, &ifr, &need_copyout);
1108 if (!err && need_copyout)
1109 if (copy_to_user(argp, &ifr, sizeof(struct ifreq)))
1110 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 } else
Johannes Berg3d23e342009-09-29 23:27:28 +02001112#ifdef CONFIG_WEXT_CORE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
Al Virob1b0c242017-10-01 20:13:08 -04001114 err = wext_handle_ioctl(net, cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 } else
Johannes Berg3d23e342009-09-29 23:27:28 +02001116#endif
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001117 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 case FIOSETOWN:
1119 case SIOCSPGRP:
1120 err = -EFAULT;
1121 if (get_user(pid, (int __user *)argp))
1122 break;
Jiri Slaby393cc3f2017-06-13 13:35:50 +02001123 err = f_setown(sock->file, pid, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 break;
1125 case FIOGETOWN:
1126 case SIOCGPGRP:
Eric W. Biederman609d7fa2006-10-02 02:17:15 -07001127 err = put_user(f_getown(sock->file),
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001128 (int __user *)argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 break;
1130 case SIOCGIFBR:
1131 case SIOCSIFBR:
1132 case SIOCBRADDBR:
1133 case SIOCBRDELBR:
1134 err = -ENOPKG;
1135 if (!br_ioctl_hook)
1136 request_module("bridge");
1137
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001138 mutex_lock(&br_ioctl_mutex);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001139 if (br_ioctl_hook)
Eric W. Biederman881d9662007-09-17 11:56:21 -07001140 err = br_ioctl_hook(net, cmd, argp);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001141 mutex_unlock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 break;
1143 case SIOCGIFVLAN:
1144 case SIOCSIFVLAN:
1145 err = -ENOPKG;
1146 if (!vlan_ioctl_hook)
1147 request_module("8021q");
1148
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001149 mutex_lock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 if (vlan_ioctl_hook)
Eric W. Biederman881d9662007-09-17 11:56:21 -07001151 err = vlan_ioctl_hook(net, argp);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001152 mutex_unlock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 case SIOCADDDLCI:
1155 case SIOCDELDLCI:
1156 err = -ENOPKG;
1157 if (!dlci_ioctl_hook)
1158 request_module("dlci");
1159
Pavel Emelyanov7512cbf2008-03-21 15:58:52 -07001160 mutex_lock(&dlci_ioctl_mutex);
1161 if (dlci_ioctl_hook)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 err = dlci_ioctl_hook(cmd, argp);
Pavel Emelyanov7512cbf2008-03-21 15:58:52 -07001163 mutex_unlock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 break;
Andrey Vaginc62cce22016-10-24 18:29:13 -07001165 case SIOCGSKNS:
1166 err = -EPERM;
1167 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1168 break;
1169
1170 err = open_related_ns(&net->ns, get_net_ns);
1171 break;
Arnd Bergmann0768e172019-04-17 22:56:11 +02001172 case SIOCGSTAMP_OLD:
1173 case SIOCGSTAMPNS_OLD:
Arnd Bergmannc7cbdbf2019-04-17 22:51:48 +02001174 if (!sock->ops->gettstamp) {
1175 err = -ENOIOCTLCMD;
1176 break;
1177 }
1178 err = sock->ops->gettstamp(sock, argp,
Arnd Bergmann0768e172019-04-17 22:56:11 +02001179 cmd == SIOCGSTAMP_OLD,
1180 !IS_ENABLED(CONFIG_64BIT));
Gustavo A. R. Silva60747822019-04-24 10:31:24 -05001181 break;
Arnd Bergmann0768e172019-04-17 22:56:11 +02001182 case SIOCGSTAMP_NEW:
1183 case SIOCGSTAMPNS_NEW:
1184 if (!sock->ops->gettstamp) {
1185 err = -ENOIOCTLCMD;
1186 break;
1187 }
1188 err = sock->ops->gettstamp(sock, argp,
1189 cmd == SIOCGSTAMP_NEW,
1190 false);
Arnd Bergmannc7cbdbf2019-04-17 22:51:48 +02001191 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 default:
Johannes Berg63ff03a2019-01-25 22:43:17 +01001193 err = sock_do_ioctl(net, sock, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 break;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 return err;
1197}
1198
Pedro Tammela8a3c2452019-03-14 10:45:23 -03001199/**
1200 * sock_create_lite - creates a socket
1201 * @family: protocol family (AF_INET, ...)
1202 * @type: communication type (SOCK_STREAM, ...)
1203 * @protocol: protocol (0, ...)
1204 * @res: new socket
1205 *
1206 * Creates a new socket and assigns it to @res, passing through LSM.
1207 * The new socket initialization is not complete, see kernel_accept().
1208 * Returns 0 or an error. On failure @res is set to %NULL.
1209 * This function internally uses GFP_KERNEL.
1210 */
1211
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212int sock_create_lite(int family, int type, int protocol, struct socket **res)
1213{
1214 int err;
1215 struct socket *sock = NULL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001216
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 err = security_socket_create(family, type, protocol, 1);
1218 if (err)
1219 goto out;
1220
1221 sock = sock_alloc();
1222 if (!sock) {
1223 err = -ENOMEM;
1224 goto out;
1225 }
1226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 sock->type = type;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001228 err = security_socket_post_create(sock, family, type, protocol, 1);
1229 if (err)
1230 goto out_release;
1231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232out:
1233 *res = sock;
1234 return err;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001235out_release:
1236 sock_release(sock);
1237 sock = NULL;
1238 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001240EXPORT_SYMBOL(sock_create_lite);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
1242/* No kernel lock held - perfect */
Al Viroade994f2017-07-03 00:01:49 -04001243static __poll_t sock_poll(struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244{
Christoph Hellwig3cafb372018-01-09 16:07:34 +01001245 struct socket *sock = file->private_data;
Christoph Hellwiga331de32018-07-30 09:42:13 +02001246 __poll_t events = poll_requested_events(wait), flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
Christoph Hellwige88958e2018-06-29 15:37:24 +02001248 if (!sock->ops->poll)
1249 return 0;
Christoph Hellwigf641f13b2018-07-30 09:42:12 +02001250
Christoph Hellwiga331de32018-07-30 09:42:13 +02001251 if (sk_can_busy_loop(sock->sk)) {
1252 /* poll once if requested by the syscall */
1253 if (events & POLL_BUSY_LOOP)
1254 sk_busy_loop(sock->sk, 1);
1255
1256 /* if this socket can poll_ll, tell the system call */
1257 flag = POLL_BUSY_LOOP;
1258 }
1259
1260 return sock->ops->poll(file, sock, wait) | flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261}
1262
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001263static int sock_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264{
Eric Dumazetb69aee02005-09-06 14:42:45 -07001265 struct socket *sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
1267 return sock->ops->mmap(file, sock, vma);
1268}
1269
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001270static int sock_close(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271{
Cong Wang6d8c50d2018-06-07 13:39:49 -07001272 __sock_release(SOCKET_I(inode), inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 return 0;
1274}
1275
1276/*
1277 * Update the socket async list
1278 *
1279 * Fasync_list locking strategy.
1280 *
1281 * 1. fasync_list is modified only under process context socket lock
1282 * i.e. under semaphore.
1283 * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
Eric Dumazet989a2972010-04-14 09:55:35 +00001284 * or under socket lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 */
1286
1287static int sock_fasync(int fd, struct file *filp, int on)
1288{
Eric Dumazet989a2972010-04-14 09:55:35 +00001289 struct socket *sock = filp->private_data;
1290 struct sock *sk = sock->sk;
Eric Dumazeteaefd1102011-02-18 03:26:36 +00001291 struct socket_wq *wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
Eric Dumazet989a2972010-04-14 09:55:35 +00001293 if (sk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
1296 lock_sock(sk);
Christoph Hellwige6476c22018-07-30 09:45:07 +02001297 wq = sock->wq;
Eric Dumazeteaefd1102011-02-18 03:26:36 +00001298 fasync_helper(fd, filp, on, &wq->fasync_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Eric Dumazeteaefd1102011-02-18 03:26:36 +00001300 if (!wq->fasync_list)
Eric Dumazet989a2972010-04-14 09:55:35 +00001301 sock_reset_flag(sk, SOCK_FASYNC);
Jonathan Corbet76398422009-02-01 14:26:59 -07001302 else
Eric Dumazetbcdce712009-10-06 17:28:29 -07001303 sock_set_flag(sk, SOCK_FASYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Eric Dumazet989a2972010-04-14 09:55:35 +00001305 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 return 0;
1307}
1308
Eric Dumazetceb5d582015-11-29 20:03:11 -08001309/* This function may be called only under rcu_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
Eric Dumazetceb5d582015-11-29 20:03:11 -08001311int sock_wake_async(struct socket_wq *wq, int how, int band)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312{
Eric Dumazetceb5d582015-11-29 20:03:11 -08001313 if (!wq || !wq->fasync_list)
1314 return -1;
Eric Dumazet43815482010-04-29 11:01:49 +00001315
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001316 switch (how) {
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001317 case SOCK_WAKE_WAITD:
Eric Dumazetceb5d582015-11-29 20:03:11 -08001318 if (test_bit(SOCKWQ_ASYNC_WAITDATA, &wq->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 break;
1320 goto call_kill;
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001321 case SOCK_WAKE_SPACE:
Eric Dumazetceb5d582015-11-29 20:03:11 -08001322 if (!test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 break;
1324 /* fall through */
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001325 case SOCK_WAKE_IO:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001326call_kill:
Eric Dumazet43815482010-04-29 11:01:49 +00001327 kill_fasync(&wq->fasync_list, SIGIO, band);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 break;
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001329 case SOCK_WAKE_URG:
Eric Dumazet43815482010-04-29 11:01:49 +00001330 kill_fasync(&wq->fasync_list, SIGURG, band);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 }
Eric Dumazetceb5d582015-11-29 20:03:11 -08001332
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 return 0;
1334}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001335EXPORT_SYMBOL(sock_wake_async);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
Pedro Tammela8a3c2452019-03-14 10:45:23 -03001337/**
1338 * __sock_create - creates a socket
1339 * @net: net namespace
1340 * @family: protocol family (AF_INET, ...)
1341 * @type: communication type (SOCK_STREAM, ...)
1342 * @protocol: protocol (0, ...)
1343 * @res: new socket
1344 * @kern: boolean for kernel space sockets
1345 *
1346 * Creates a new socket and assigns it to @res, passing through LSM.
1347 * Returns 0 or an error. On failure @res is set to %NULL. @kern must
1348 * be set to true if the socket resides in kernel space.
1349 * This function internally uses GFP_KERNEL.
1350 */
1351
Pavel Emelyanov721db932010-09-29 16:06:32 +04001352int __sock_create(struct net *net, int family, int type, int protocol,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001353 struct socket **res, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354{
1355 int err;
1356 struct socket *sock;
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001357 const struct net_proto_family *pf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
1359 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001360 * Check protocol is in range
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 */
1362 if (family < 0 || family >= NPROTO)
1363 return -EAFNOSUPPORT;
1364 if (type < 0 || type >= SOCK_MAX)
1365 return -EINVAL;
1366
1367 /* Compatibility.
1368
1369 This uglymoron is moved from INET layer to here to avoid
1370 deadlock in module load.
1371 */
1372 if (family == PF_INET && type == SOCK_PACKET) {
liping.zhangf3c98692016-03-11 23:08:36 +08001373 pr_info_once("%s uses obsolete (PF_INET,SOCK_PACKET)\n",
1374 current->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 family = PF_PACKET;
1376 }
1377
1378 err = security_socket_create(family, type, protocol, kern);
1379 if (err)
1380 return err;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001381
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001382 /*
1383 * Allocate the socket and allow the family to set things up. if
1384 * the protocol is 0, the family is instructed to select an appropriate
1385 * default.
1386 */
1387 sock = sock_alloc();
1388 if (!sock) {
Joe Perchese87cc472012-05-13 21:56:26 +00001389 net_warn_ratelimited("socket: no more sockets\n");
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001390 return -ENFILE; /* Not exactly a match, but its the
1391 closest posix thing */
1392 }
1393
1394 sock->type = type;
1395
Johannes Berg95a5afc2008-10-16 15:24:51 -07001396#ifdef CONFIG_MODULES
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001397 /* Attempt to load a protocol module if the find failed.
1398 *
1399 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 * requested real, full-featured networking support upon configuration.
1401 * Otherwise module support will break!
1402 */
Eric Dumazet190683a2010-11-10 10:50:44 +00001403 if (rcu_access_pointer(net_families[family]) == NULL)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001404 request_module("net-pf-%d", family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405#endif
1406
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001407 rcu_read_lock();
1408 pf = rcu_dereference(net_families[family]);
1409 err = -EAFNOSUPPORT;
1410 if (!pf)
1411 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
1413 /*
1414 * We will call the ->create function, that possibly is in a loadable
1415 * module, so we have to bump that loadable module refcnt first.
1416 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001417 if (!try_module_get(pf->owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 goto out_release;
1419
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001420 /* Now protected by module ref count */
1421 rcu_read_unlock();
1422
Eric Paris3f378b62009-11-05 22:18:14 -08001423 err = pf->create(net, sock, protocol, kern);
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001424 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 goto out_module_put;
Frank Filza79af592005-09-27 15:23:38 -07001426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 /*
1428 * Now to bump the refcnt of the [loadable] module that owns this
1429 * socket at sock_release time we decrement its refcnt.
1430 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001431 if (!try_module_get(sock->ops->owner))
1432 goto out_module_busy;
1433
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 /*
1435 * Now that we're done with the ->create function, the [loadable]
1436 * module can have its refcnt decremented
1437 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001438 module_put(pf->owner);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001439 err = security_socket_post_create(sock, family, type, protocol, kern);
1440 if (err)
Herbert Xu3b185522007-08-15 14:46:02 -07001441 goto out_sock_release;
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001442 *res = sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001444 return 0;
1445
1446out_module_busy:
1447 err = -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448out_module_put:
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001449 sock->ops = NULL;
1450 module_put(pf->owner);
1451out_sock_release:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 sock_release(sock);
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001453 return err;
1454
1455out_release:
1456 rcu_read_unlock();
1457 goto out_sock_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458}
Pavel Emelyanov721db932010-09-29 16:06:32 +04001459EXPORT_SYMBOL(__sock_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Pedro Tammela8a3c2452019-03-14 10:45:23 -03001461/**
1462 * sock_create - creates a socket
1463 * @family: protocol family (AF_INET, ...)
1464 * @type: communication type (SOCK_STREAM, ...)
1465 * @protocol: protocol (0, ...)
1466 * @res: new socket
1467 *
1468 * A wrapper around __sock_create().
1469 * Returns 0 or an error. This function internally uses GFP_KERNEL.
1470 */
1471
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472int sock_create(int family, int type, int protocol, struct socket **res)
1473{
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001474 return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001476EXPORT_SYMBOL(sock_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Pedro Tammela8a3c2452019-03-14 10:45:23 -03001478/**
1479 * sock_create_kern - creates a socket (kernel space)
1480 * @net: net namespace
1481 * @family: protocol family (AF_INET, ...)
1482 * @type: communication type (SOCK_STREAM, ...)
1483 * @protocol: protocol (0, ...)
1484 * @res: new socket
1485 *
1486 * A wrapper around __sock_create().
1487 * Returns 0 or an error. This function internally uses GFP_KERNEL.
1488 */
1489
Eric W. Biedermaneeb1bd52015-05-08 21:08:05 -05001490int sock_create_kern(struct net *net, int family, int type, int protocol, struct socket **res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491{
Eric W. Biedermaneeb1bd52015-05-08 21:08:05 -05001492 return __sock_create(net, family, type, protocol, res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001494EXPORT_SYMBOL(sock_create_kern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
Dominik Brodowski9d6a15c2018-03-13 19:29:43 +01001496int __sys_socket(int family, int type, int protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497{
1498 int retval;
1499 struct socket *sock;
Ulrich Dreppera677a032008-07-23 21:29:17 -07001500 int flags;
1501
Ulrich Dreppere38b36f2008-07-23 21:29:42 -07001502 /* Check the SOCK_* constants for consistency. */
1503 BUILD_BUG_ON(SOCK_CLOEXEC != O_CLOEXEC);
1504 BUILD_BUG_ON((SOCK_MAX | SOCK_TYPE_MASK) != SOCK_TYPE_MASK);
1505 BUILD_BUG_ON(SOCK_CLOEXEC & SOCK_TYPE_MASK);
1506 BUILD_BUG_ON(SOCK_NONBLOCK & SOCK_TYPE_MASK);
1507
Ulrich Dreppera677a032008-07-23 21:29:17 -07001508 flags = type & ~SOCK_TYPE_MASK;
Ulrich Drepper77d27202008-07-23 21:29:35 -07001509 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Dreppera677a032008-07-23 21:29:17 -07001510 return -EINVAL;
1511 type &= SOCK_TYPE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001513 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1514 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1515
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 retval = sock_create(family, type, protocol, &sock);
1517 if (retval < 0)
Al Viro8e1611e2017-12-05 23:29:09 +00001518 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
Al Viro8e1611e2017-12-05 23:29:09 +00001520 return sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521}
1522
Dominik Brodowski9d6a15c2018-03-13 19:29:43 +01001523SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol)
1524{
1525 return __sys_socket(family, type, protocol);
1526}
1527
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528/*
1529 * Create a pair of connected sockets.
1530 */
1531
Dominik Brodowski6debc8d2018-03-13 19:49:23 +01001532int __sys_socketpair(int family, int type, int protocol, int __user *usockvec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533{
1534 struct socket *sock1, *sock2;
1535 int fd1, fd2, err;
Al Virodb349502007-02-07 01:48:00 -05001536 struct file *newfile1, *newfile2;
Ulrich Dreppera677a032008-07-23 21:29:17 -07001537 int flags;
1538
1539 flags = type & ~SOCK_TYPE_MASK;
Ulrich Drepper77d27202008-07-23 21:29:35 -07001540 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Dreppera677a032008-07-23 21:29:17 -07001541 return -EINVAL;
1542 type &= SOCK_TYPE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001544 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1545 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1546
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 /*
Al Viro016a2662017-12-05 23:28:38 +00001548 * reserve descriptors and make sure we won't fail
1549 * to return them to userland.
1550 */
1551 fd1 = get_unused_fd_flags(flags);
1552 if (unlikely(fd1 < 0))
1553 return fd1;
1554
1555 fd2 = get_unused_fd_flags(flags);
1556 if (unlikely(fd2 < 0)) {
1557 put_unused_fd(fd1);
1558 return fd2;
1559 }
1560
1561 err = put_user(fd1, &usockvec[0]);
1562 if (err)
1563 goto out;
1564
1565 err = put_user(fd2, &usockvec[1]);
1566 if (err)
1567 goto out;
1568
1569 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 * Obtain the first socket and check if the underlying protocol
1571 * supports the socketpair call.
1572 */
1573
1574 err = sock_create(family, type, protocol, &sock1);
Al Viro016a2662017-12-05 23:28:38 +00001575 if (unlikely(err < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 goto out;
1577
1578 err = sock_create(family, type, protocol, &sock2);
Al Viro016a2662017-12-05 23:28:38 +00001579 if (unlikely(err < 0)) {
1580 sock_release(sock1);
1581 goto out;
David S. Millerbf3c23d2007-10-29 21:54:02 -07001582 }
Yann Droneaudd73aa282013-12-09 22:42:20 +01001583
David Herrmannd47cd942018-05-04 16:28:20 +02001584 err = security_socket_socketpair(sock1, sock2);
1585 if (unlikely(err)) {
1586 sock_release(sock2);
1587 sock_release(sock1);
1588 goto out;
1589 }
1590
Al Viro016a2662017-12-05 23:28:38 +00001591 err = sock1->ops->socketpair(sock1, sock2);
1592 if (unlikely(err < 0)) {
1593 sock_release(sock2);
1594 sock_release(sock1);
1595 goto out;
Al Viro28407632012-08-17 23:54:15 -04001596 }
1597
Linus Torvaldsaab174f2012-10-02 20:25:04 -07001598 newfile1 = sock_alloc_file(sock1, flags, NULL);
Viresh Kumarb5ffe632015-08-12 15:59:47 +05301599 if (IS_ERR(newfile1)) {
Al Viro28407632012-08-17 23:54:15 -04001600 err = PTR_ERR(newfile1);
Al Viro016a2662017-12-05 23:28:38 +00001601 sock_release(sock2);
1602 goto out;
Al Viro28407632012-08-17 23:54:15 -04001603 }
1604
Linus Torvaldsaab174f2012-10-02 20:25:04 -07001605 newfile2 = sock_alloc_file(sock2, flags, NULL);
Al Viro28407632012-08-17 23:54:15 -04001606 if (IS_ERR(newfile2)) {
1607 err = PTR_ERR(newfile2);
Al Viro016a2662017-12-05 23:28:38 +00001608 fput(newfile1);
1609 goto out;
Al Virodb349502007-02-07 01:48:00 -05001610 }
1611
Al Viro157cf642008-12-14 04:57:47 -05001612 audit_fd_pair(fd1, fd2);
Yann Droneaudd73aa282013-12-09 22:42:20 +01001613
Al Virodb349502007-02-07 01:48:00 -05001614 fd_install(fd1, newfile1);
1615 fd_install(fd2, newfile2);
Yann Droneaudd73aa282013-12-09 22:42:20 +01001616 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618out:
Al Viro016a2662017-12-05 23:28:38 +00001619 put_unused_fd(fd2);
1620 put_unused_fd(fd1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 return err;
1622}
1623
Dominik Brodowski6debc8d2018-03-13 19:49:23 +01001624SYSCALL_DEFINE4(socketpair, int, family, int, type, int, protocol,
1625 int __user *, usockvec)
1626{
1627 return __sys_socketpair(family, type, protocol, usockvec);
1628}
1629
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630/*
1631 * Bind a name to a socket. Nothing much to do here since it's
1632 * the protocol's responsibility to handle the local address.
1633 *
1634 * We move the socket address to kernel space before we call
1635 * the protocol layer (having also checked the address is ok).
1636 */
1637
Dominik Brodowskia87d35d2018-03-13 19:33:09 +01001638int __sys_bind(int fd, struct sockaddr __user *umyaddr, int addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639{
1640 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001641 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001642 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001644 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001645 if (sock) {
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001646 err = move_addr_to_kernel(umyaddr, addrlen, &address);
Jakub Sitnicki068b88c2018-10-04 11:09:40 +02001647 if (!err) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001648 err = security_socket_bind(sock,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001649 (struct sockaddr *)&address,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001650 addrlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001651 if (!err)
1652 err = sock->ops->bind(sock,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001653 (struct sockaddr *)
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001654 &address, addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 }
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001656 fput_light(sock->file, fput_needed);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 return err;
1659}
1660
Dominik Brodowskia87d35d2018-03-13 19:33:09 +01001661SYSCALL_DEFINE3(bind, int, fd, struct sockaddr __user *, umyaddr, int, addrlen)
1662{
1663 return __sys_bind(fd, umyaddr, addrlen);
1664}
1665
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666/*
1667 * Perform a listen. Basically, we allow the protocol to do anything
1668 * necessary for a listen, and if that works, we mark the socket as
1669 * ready for listening.
1670 */
1671
Dominik Brodowski25e290e2018-03-13 19:36:54 +01001672int __sys_listen(int fd, int backlog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673{
1674 struct socket *sock;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001675 int err, fput_needed;
Pavel Emelyanovb8e1f9b2007-12-08 00:12:33 -08001676 int somaxconn;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001677
1678 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1679 if (sock) {
Pavel Emelyanov8efa6e92008-03-31 19:41:14 -07001680 somaxconn = sock_net(sock->sk)->core.sysctl_somaxconn;
Eric Dumazet95c96172012-04-15 05:58:06 +00001681 if ((unsigned int)backlog > somaxconn)
Pavel Emelyanovb8e1f9b2007-12-08 00:12:33 -08001682 backlog = somaxconn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
1684 err = security_socket_listen(sock, backlog);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001685 if (!err)
1686 err = sock->ops->listen(sock, backlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001688 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 }
1690 return err;
1691}
1692
Dominik Brodowski25e290e2018-03-13 19:36:54 +01001693SYSCALL_DEFINE2(listen, int, fd, int, backlog)
1694{
1695 return __sys_listen(fd, backlog);
1696}
1697
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698/*
1699 * For accept, we attempt to create a new socket, set up the link
1700 * with the client, wake up the client, then return the new
1701 * connected fd. We collect the address of the connector in kernel
1702 * space and move it to user at the very end. This is unclean because
1703 * we open the socket then return an error.
1704 *
1705 * 1003.1g adds the ability to recvmsg() to query connection pending
1706 * status to recvmsg. We need to add that support in a way thats
Geert Uytterhoevenb9030362018-03-02 14:43:23 +01001707 * clean when we restructure accept also.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 */
1709
Dominik Brodowski4541e802018-03-13 19:24:23 +01001710int __sys_accept4(int fd, struct sockaddr __user *upeer_sockaddr,
1711 int __user *upeer_addrlen, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712{
1713 struct socket *sock, *newsock;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001714 struct file *newfile;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001715 int err, len, newfd, fput_needed;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001716 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
Ulrich Drepper77d27202008-07-23 21:29:35 -07001718 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001719 return -EINVAL;
1720
1721 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1722 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1723
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001724 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 if (!sock)
1726 goto out;
1727
1728 err = -ENFILE;
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001729 newsock = sock_alloc();
1730 if (!newsock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 goto out_put;
1732
1733 newsock->type = sock->type;
1734 newsock->ops = sock->ops;
1735
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 /*
1737 * We don't need try_module_get here, as the listening socket (sock)
1738 * has the protocol module (sock->ops->owner) held.
1739 */
1740 __module_get(newsock->ops->owner);
1741
Al Viro28407632012-08-17 23:54:15 -04001742 newfd = get_unused_fd_flags(flags);
David S. Miller39d8c1b2006-03-20 17:13:49 -08001743 if (unlikely(newfd < 0)) {
1744 err = newfd;
David S. Miller9a1875e2006-04-01 12:48:36 -08001745 sock_release(newsock);
1746 goto out_put;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001747 }
Linus Torvaldsaab174f2012-10-02 20:25:04 -07001748 newfile = sock_alloc_file(newsock, flags, sock->sk->sk_prot_creator->name);
Viresh Kumarb5ffe632015-08-12 15:59:47 +05301749 if (IS_ERR(newfile)) {
Al Viro28407632012-08-17 23:54:15 -04001750 err = PTR_ERR(newfile);
1751 put_unused_fd(newfd);
Al Viro28407632012-08-17 23:54:15 -04001752 goto out_put;
1753 }
David S. Miller39d8c1b2006-03-20 17:13:49 -08001754
Frank Filza79af592005-09-27 15:23:38 -07001755 err = security_socket_accept(sock, newsock);
1756 if (err)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001757 goto out_fd;
Frank Filza79af592005-09-27 15:23:38 -07001758
David Howellscdfbabf2017-03-09 08:09:05 +00001759 err = sock->ops->accept(sock, newsock, sock->file->f_flags, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 if (err < 0)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001761 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
1763 if (upeer_sockaddr) {
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001764 len = newsock->ops->getname(newsock,
1765 (struct sockaddr *)&address, 2);
1766 if (len < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 err = -ECONNABORTED;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001768 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 }
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001770 err = move_addr_to_user(&address,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001771 len, upeer_sockaddr, upeer_addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 if (err < 0)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001773 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 }
1775
1776 /* File flags are not inherited via accept() unlike another OSes. */
1777
David S. Miller39d8c1b2006-03-20 17:13:49 -08001778 fd_install(newfd, newfile);
1779 err = newfd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001782 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783out:
1784 return err;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001785out_fd:
David S. Miller9606a212006-04-01 01:00:14 -08001786 fput(newfile);
David S. Miller39d8c1b2006-03-20 17:13:49 -08001787 put_unused_fd(newfd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 goto out_put;
1789}
1790
Dominik Brodowski4541e802018-03-13 19:24:23 +01001791SYSCALL_DEFINE4(accept4, int, fd, struct sockaddr __user *, upeer_sockaddr,
1792 int __user *, upeer_addrlen, int, flags)
1793{
1794 return __sys_accept4(fd, upeer_sockaddr, upeer_addrlen, flags);
1795}
1796
Heiko Carstens20f37032009-01-14 14:14:23 +01001797SYSCALL_DEFINE3(accept, int, fd, struct sockaddr __user *, upeer_sockaddr,
1798 int __user *, upeer_addrlen)
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001799{
Dominik Brodowski4541e802018-03-13 19:24:23 +01001800 return __sys_accept4(fd, upeer_sockaddr, upeer_addrlen, 0);
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001801}
1802
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803/*
1804 * Attempt to connect to a socket with the server address. The address
1805 * is in user space so we verify it is OK and move it to kernel space.
1806 *
1807 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
1808 * break bindings
1809 *
1810 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
1811 * other SEQPACKET protocols that take time to connect() as it doesn't
1812 * include the -EINPROGRESS status for such sockets.
1813 */
1814
Dominik Brodowski1387c2c2018-03-13 19:35:09 +01001815int __sys_connect(int fd, struct sockaddr __user *uservaddr, int addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816{
1817 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001818 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001819 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001821 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 if (!sock)
1823 goto out;
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001824 err = move_addr_to_kernel(uservaddr, addrlen, &address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 if (err < 0)
1826 goto out_put;
1827
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001828 err =
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001829 security_socket_connect(sock, (struct sockaddr *)&address, addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 if (err)
1831 goto out_put;
1832
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001833 err = sock->ops->connect(sock, (struct sockaddr *)&address, addrlen,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 sock->file->f_flags);
1835out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001836 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837out:
1838 return err;
1839}
1840
Dominik Brodowski1387c2c2018-03-13 19:35:09 +01001841SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
1842 int, addrlen)
1843{
1844 return __sys_connect(fd, uservaddr, addrlen);
1845}
1846
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847/*
1848 * Get the local address ('name') of a socket object. Move the obtained
1849 * name to user space.
1850 */
1851
Dominik Brodowski8882a102018-03-13 19:43:14 +01001852int __sys_getsockname(int fd, struct sockaddr __user *usockaddr,
1853 int __user *usockaddr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854{
1855 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001856 struct sockaddr_storage address;
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001857 int err, fput_needed;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001858
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001859 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 if (!sock)
1861 goto out;
1862
1863 err = security_socket_getsockname(sock);
1864 if (err)
1865 goto out_put;
1866
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001867 err = sock->ops->getname(sock, (struct sockaddr *)&address, 0);
1868 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 goto out_put;
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001870 /* "err" is actually length in this case */
1871 err = move_addr_to_user(&address, err, usockaddr, usockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
1873out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001874 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875out:
1876 return err;
1877}
1878
Dominik Brodowski8882a102018-03-13 19:43:14 +01001879SYSCALL_DEFINE3(getsockname, int, fd, struct sockaddr __user *, usockaddr,
1880 int __user *, usockaddr_len)
1881{
1882 return __sys_getsockname(fd, usockaddr, usockaddr_len);
1883}
1884
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885/*
1886 * Get the remote address ('name') of a socket object. Move the obtained
1887 * name to user space.
1888 */
1889
Dominik Brodowskib21c8f82018-03-13 19:47:00 +01001890int __sys_getpeername(int fd, struct sockaddr __user *usockaddr,
1891 int __user *usockaddr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892{
1893 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001894 struct sockaddr_storage address;
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001895 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001897 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1898 if (sock != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 err = security_socket_getpeername(sock);
1900 if (err) {
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001901 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 return err;
1903 }
1904
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001905 err = sock->ops->getname(sock, (struct sockaddr *)&address, 1);
1906 if (err >= 0)
1907 /* "err" is actually length in this case */
1908 err = move_addr_to_user(&address, err, usockaddr,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001909 usockaddr_len);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001910 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 }
1912 return err;
1913}
1914
Dominik Brodowskib21c8f82018-03-13 19:47:00 +01001915SYSCALL_DEFINE3(getpeername, int, fd, struct sockaddr __user *, usockaddr,
1916 int __user *, usockaddr_len)
1917{
1918 return __sys_getpeername(fd, usockaddr, usockaddr_len);
1919}
1920
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921/*
1922 * Send a datagram to a given address. We move the address into kernel
1923 * space and check the user space data area is readable before invoking
1924 * the protocol.
1925 */
Dominik Brodowski211b6342018-03-13 19:18:52 +01001926int __sys_sendto(int fd, void __user *buff, size_t len, unsigned int flags,
1927 struct sockaddr __user *addr, int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928{
1929 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001930 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 int err;
1932 struct msghdr msg;
1933 struct iovec iov;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001934 int fput_needed;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001935
Al Viro602bd0e2015-03-21 19:12:32 -04001936 err = import_single_range(WRITE, buff, len, &iov, &msg.msg_iter);
1937 if (unlikely(err))
1938 return err;
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001939 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1940 if (!sock)
David S. Miller4387ff72007-02-08 15:06:08 -08001941 goto out;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001942
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001943 msg.msg_name = NULL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001944 msg.msg_control = NULL;
1945 msg.msg_controllen = 0;
1946 msg.msg_namelen = 0;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001947 if (addr) {
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001948 err = move_addr_to_kernel(addr, addr_len, &address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 if (err < 0)
1950 goto out_put;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001951 msg.msg_name = (struct sockaddr *)&address;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001952 msg.msg_namelen = addr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 }
1954 if (sock->file->f_flags & O_NONBLOCK)
1955 flags |= MSG_DONTWAIT;
1956 msg.msg_flags = flags;
Al Virod8725c82014-12-11 00:02:50 -05001957 err = sock_sendmsg(sock, &msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001959out_put:
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001960 fput_light(sock->file, fput_needed);
David S. Miller4387ff72007-02-08 15:06:08 -08001961out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 return err;
1963}
1964
Dominik Brodowski211b6342018-03-13 19:18:52 +01001965SYSCALL_DEFINE6(sendto, int, fd, void __user *, buff, size_t, len,
1966 unsigned int, flags, struct sockaddr __user *, addr,
1967 int, addr_len)
1968{
1969 return __sys_sendto(fd, buff, len, flags, addr, addr_len);
1970}
1971
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001973 * Send a datagram down a socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 */
1975
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001976SYSCALL_DEFINE4(send, int, fd, void __user *, buff, size_t, len,
Eric Dumazet95c96172012-04-15 05:58:06 +00001977 unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978{
Dominik Brodowski211b6342018-03-13 19:18:52 +01001979 return __sys_sendto(fd, buff, len, flags, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980}
1981
1982/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001983 * Receive a frame from the socket and optionally record the address of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 * sender. We verify the buffers are writable and if needed move the
1985 * sender address from kernel to user space.
1986 */
Dominik Brodowski7a09e1e2018-03-13 19:10:06 +01001987int __sys_recvfrom(int fd, void __user *ubuf, size_t size, unsigned int flags,
1988 struct sockaddr __user *addr, int __user *addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989{
1990 struct socket *sock;
1991 struct iovec iov;
1992 struct msghdr msg;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001993 struct sockaddr_storage address;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001994 int err, err2;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001995 int fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
Al Viro602bd0e2015-03-21 19:12:32 -04001997 err = import_single_range(READ, ubuf, size, &iov, &msg.msg_iter);
1998 if (unlikely(err))
1999 return err;
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08002000 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 if (!sock)
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08002002 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002004 msg.msg_control = NULL;
2005 msg.msg_controllen = 0;
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01002006 /* Save some cycles and don't copy the address if not needed */
2007 msg.msg_name = addr ? (struct sockaddr *)&address : NULL;
2008 /* We assume all kernel code knows the size of sockaddr_storage */
2009 msg.msg_namelen = 0;
tadeusz.struk@intel.com130ed5d2015-12-15 10:46:17 -08002010 msg.msg_iocb = NULL;
Alexander Potapenko9f138fa2017-03-08 18:08:16 +01002011 msg.msg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 if (sock->file->f_flags & O_NONBLOCK)
2013 flags |= MSG_DONTWAIT;
Al Viro2da62902015-03-14 21:13:46 -04002014 err = sock_recvmsg(sock, &msg, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002016 if (err >= 0 && addr != NULL) {
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00002017 err2 = move_addr_to_user(&address,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07002018 msg.msg_namelen, addr, addr_len);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002019 if (err2 < 0)
2020 err = err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 }
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08002022
2023 fput_light(sock->file, fput_needed);
David S. Miller4387ff72007-02-08 15:06:08 -08002024out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 return err;
2026}
2027
Dominik Brodowski7a09e1e2018-03-13 19:10:06 +01002028SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, ubuf, size_t, size,
2029 unsigned int, flags, struct sockaddr __user *, addr,
2030 int __user *, addr_len)
2031{
2032 return __sys_recvfrom(fd, ubuf, size, flags, addr, addr_len);
2033}
2034
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002036 * Receive a datagram from a socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 */
2038
Jan Glauberb7c0ddf2014-04-16 09:32:48 +02002039SYSCALL_DEFINE4(recv, int, fd, void __user *, ubuf, size_t, size,
2040 unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041{
Dominik Brodowski7a09e1e2018-03-13 19:10:06 +01002042 return __sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043}
2044
2045/*
2046 * Set a socket option. Because we don't know the option lengths we have
2047 * to pass the user mode parameter for the protocols to sort out.
2048 */
2049
Dominik Brodowskicc36dca2018-03-13 20:10:59 +01002050static int __sys_setsockopt(int fd, int level, int optname,
2051 char __user *optval, int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052{
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07002053 mm_segment_t oldfs = get_fs();
2054 char *kernel_optval = NULL;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002055 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 struct socket *sock;
2057
2058 if (optlen < 0)
2059 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002060
2061 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2062 if (sock != NULL) {
2063 err = security_socket_setsockopt(sock, level, optname);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002064 if (err)
2065 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07002067 err = BPF_CGROUP_RUN_PROG_SETSOCKOPT(sock->sk, &level,
2068 &optname, optval, &optlen,
2069 &kernel_optval);
2070
2071 if (err < 0) {
2072 goto out_put;
2073 } else if (err > 0) {
2074 err = 0;
2075 goto out_put;
2076 }
2077
2078 if (kernel_optval) {
2079 set_fs(KERNEL_DS);
2080 optval = (char __user __force *)kernel_optval;
2081 }
2082
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 if (level == SOL_SOCKET)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002084 err =
2085 sock_setsockopt(sock, level, optname, optval,
2086 optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 else
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002088 err =
2089 sock->ops->setsockopt(sock, level, optname, optval,
2090 optlen);
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07002091
2092 if (kernel_optval) {
2093 set_fs(oldfs);
2094 kfree(kernel_optval);
2095 }
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002096out_put:
2097 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 }
2099 return err;
2100}
2101
Dominik Brodowskicc36dca2018-03-13 20:10:59 +01002102SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname,
2103 char __user *, optval, int, optlen)
2104{
2105 return __sys_setsockopt(fd, level, optname, optval, optlen);
2106}
2107
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108/*
2109 * Get a socket option. Because we don't know the option lengths we have
2110 * to pass a user mode parameter for the protocols to sort out.
2111 */
2112
Dominik Brodowski13a2d702018-03-13 20:15:04 +01002113static int __sys_getsockopt(int fd, int level, int optname,
2114 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002116 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 struct socket *sock;
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07002118 int max_optlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002120 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2121 if (sock != NULL) {
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002122 err = security_socket_getsockopt(sock, level, optname);
2123 if (err)
2124 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07002126 max_optlen = BPF_CGROUP_GETSOCKOPT_MAX_OPTLEN(optlen);
2127
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 if (level == SOL_SOCKET)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002129 err =
2130 sock_getsockopt(sock, level, optname, optval,
2131 optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 else
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002133 err =
2134 sock->ops->getsockopt(sock, level, optname, optval,
2135 optlen);
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07002136
2137 err = BPF_CGROUP_RUN_PROG_GETSOCKOPT(sock->sk, level, optname,
2138 optval, optlen,
2139 max_optlen, err);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002140out_put:
2141 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 }
2143 return err;
2144}
2145
Dominik Brodowski13a2d702018-03-13 20:15:04 +01002146SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname,
2147 char __user *, optval, int __user *, optlen)
2148{
2149 return __sys_getsockopt(fd, level, optname, optval, optlen);
2150}
2151
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152/*
2153 * Shutdown a socket.
2154 */
2155
Dominik Brodowski005a1ae2018-03-13 20:07:05 +01002156int __sys_shutdown(int fd, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002158 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 struct socket *sock;
2160
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002161 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2162 if (sock != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 err = security_socket_shutdown(sock, how);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002164 if (!err)
2165 err = sock->ops->shutdown(sock, how);
2166 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 }
2168 return err;
2169}
2170
Dominik Brodowski005a1ae2018-03-13 20:07:05 +01002171SYSCALL_DEFINE2(shutdown, int, fd, int, how)
2172{
2173 return __sys_shutdown(fd, how);
2174}
2175
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002176/* A couple of helpful macros for getting the address of the 32/64 bit
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 * fields which are the same type (int / unsigned) on our platforms.
2178 */
2179#define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
2180#define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
2181#define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
2182
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002183struct used_address {
2184 struct sockaddr_storage name;
2185 unsigned int name_len;
2186};
2187
Al Viroda184282015-03-21 19:29:06 -04002188static int copy_msghdr_from_user(struct msghdr *kmsg,
2189 struct user_msghdr __user *umsg,
2190 struct sockaddr __user **save_addr,
2191 struct iovec **iov)
Dan Carpenter1661bf32013-10-03 00:27:20 +03002192{
Al Viroffb07552017-06-27 19:32:04 -04002193 struct user_msghdr msg;
Al Viro08adb7d2014-11-10 20:23:13 -05002194 ssize_t err;
2195
Al Viroffb07552017-06-27 19:32:04 -04002196 if (copy_from_user(&msg, umsg, sizeof(*umsg)))
Dan Carpenter1661bf32013-10-03 00:27:20 +03002197 return -EFAULT;
Matthew Leachdbb490b2014-03-11 11:58:27 +00002198
Paolo Abeni864d9662017-07-21 18:49:45 +02002199 kmsg->msg_control = (void __force *)msg.msg_control;
Al Viroffb07552017-06-27 19:32:04 -04002200 kmsg->msg_controllen = msg.msg_controllen;
2201 kmsg->msg_flags = msg.msg_flags;
2202
2203 kmsg->msg_namelen = msg.msg_namelen;
2204 if (!msg.msg_name)
Ani Sinha6a2a2b32014-09-08 14:49:59 -07002205 kmsg->msg_namelen = 0;
2206
Matthew Leachdbb490b2014-03-11 11:58:27 +00002207 if (kmsg->msg_namelen < 0)
2208 return -EINVAL;
2209
Dan Carpenter1661bf32013-10-03 00:27:20 +03002210 if (kmsg->msg_namelen > sizeof(struct sockaddr_storage))
Dan Carpenterdb31c552013-11-27 15:40:21 +03002211 kmsg->msg_namelen = sizeof(struct sockaddr_storage);
Al Viro08adb7d2014-11-10 20:23:13 -05002212
2213 if (save_addr)
Al Viroffb07552017-06-27 19:32:04 -04002214 *save_addr = msg.msg_name;
Al Viro08adb7d2014-11-10 20:23:13 -05002215
Al Viroffb07552017-06-27 19:32:04 -04002216 if (msg.msg_name && kmsg->msg_namelen) {
Al Viro08adb7d2014-11-10 20:23:13 -05002217 if (!save_addr) {
Paolo Abeni864d9662017-07-21 18:49:45 +02002218 err = move_addr_to_kernel(msg.msg_name,
2219 kmsg->msg_namelen,
Al Viro08adb7d2014-11-10 20:23:13 -05002220 kmsg->msg_name);
2221 if (err < 0)
2222 return err;
2223 }
2224 } else {
2225 kmsg->msg_name = NULL;
2226 kmsg->msg_namelen = 0;
2227 }
2228
Al Viroffb07552017-06-27 19:32:04 -04002229 if (msg.msg_iovlen > UIO_MAXIOV)
Al Viro08adb7d2014-11-10 20:23:13 -05002230 return -EMSGSIZE;
2231
tadeusz.struk@intel.com0345f932015-03-19 12:31:25 -07002232 kmsg->msg_iocb = NULL;
2233
Al Viroffb07552017-06-27 19:32:04 -04002234 return import_iovec(save_addr ? READ : WRITE,
2235 msg.msg_iov, msg.msg_iovlen,
Al Viroda184282015-03-21 19:29:06 -04002236 UIO_FASTIOV, iov, &kmsg->msg_iter);
Dan Carpenter1661bf32013-10-03 00:27:20 +03002237}
2238
Al Viro666547f2014-04-06 14:03:05 -04002239static int ___sys_sendmsg(struct socket *sock, struct user_msghdr __user *msg,
Eric Dumazet95c96172012-04-15 05:58:06 +00002240 struct msghdr *msg_sys, unsigned int flags,
Tom Herbert28a94d82016-03-07 14:11:02 -08002241 struct used_address *used_address,
2242 unsigned int allowed_msghdr_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243{
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002244 struct compat_msghdr __user *msg_compat =
2245 (struct compat_msghdr __user *)msg;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07002246 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
Alex Williamsonb9d717a2005-09-26 14:28:02 -07002248 unsigned char ctl[sizeof(struct cmsghdr) + 20]
Amit Kushwaha846cc122016-12-08 18:21:53 +05302249 __aligned(sizeof(__kernel_size_t));
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002250 /* 20 is size of ipv6_pktinfo */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 unsigned char *ctl_buf = ctl;
Al Virod8725c82014-12-11 00:02:50 -05002252 int ctl_len;
Al Viro08adb7d2014-11-10 20:23:13 -05002253 ssize_t err;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002254
Al Viro08adb7d2014-11-10 20:23:13 -05002255 msg_sys->msg_name = &address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
Al Viro08449322014-11-09 22:33:45 -05002257 if (MSG_CMSG_COMPAT & flags)
Al Viro08adb7d2014-11-10 20:23:13 -05002258 err = get_compat_msghdr(msg_sys, msg_compat, NULL, &iov);
Al Viro08449322014-11-09 22:33:45 -05002259 else
Al Viro08adb7d2014-11-10 20:23:13 -05002260 err = copy_msghdr_from_user(msg_sys, msg, NULL, &iov);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002261 if (err < 0)
Al Viroda184282015-03-21 19:29:06 -04002262 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263
2264 err = -ENOBUFS;
2265
Anton Blanchard228e5482011-05-02 20:21:35 +00002266 if (msg_sys->msg_controllen > INT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 goto out_freeiov;
Tom Herbert28a94d82016-03-07 14:11:02 -08002268 flags |= (msg_sys->msg_flags & allowed_msghdr_flags);
Anton Blanchard228e5482011-05-02 20:21:35 +00002269 ctl_len = msg_sys->msg_controllen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002271 err =
Anton Blanchard228e5482011-05-02 20:21:35 +00002272 cmsghdr_from_user_compat_to_kern(msg_sys, sock->sk, ctl,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002273 sizeof(ctl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 if (err)
2275 goto out_freeiov;
Anton Blanchard228e5482011-05-02 20:21:35 +00002276 ctl_buf = msg_sys->msg_control;
2277 ctl_len = msg_sys->msg_controllen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 } else if (ctl_len) {
David S. Millerac4340f2017-01-04 13:24:19 -05002279 BUILD_BUG_ON(sizeof(struct cmsghdr) !=
2280 CMSG_ALIGN(sizeof(struct cmsghdr)));
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002281 if (ctl_len > sizeof(ctl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002283 if (ctl_buf == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 goto out_freeiov;
2285 }
2286 err = -EFAULT;
2287 /*
Anton Blanchard228e5482011-05-02 20:21:35 +00002288 * Careful! Before this, msg_sys->msg_control contains a user pointer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
2290 * checking falls down on this.
2291 */
Namhyung Kimfb8621b2010-09-07 03:55:00 +00002292 if (copy_from_user(ctl_buf,
Anton Blanchard228e5482011-05-02 20:21:35 +00002293 (void __user __force *)msg_sys->msg_control,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002294 ctl_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 goto out_freectl;
Anton Blanchard228e5482011-05-02 20:21:35 +00002296 msg_sys->msg_control = ctl_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 }
Anton Blanchard228e5482011-05-02 20:21:35 +00002298 msg_sys->msg_flags = flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299
2300 if (sock->file->f_flags & O_NONBLOCK)
Anton Blanchard228e5482011-05-02 20:21:35 +00002301 msg_sys->msg_flags |= MSG_DONTWAIT;
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002302 /*
2303 * If this is sendmmsg() and current destination address is same as
2304 * previously succeeded address, omit asking LSM's decision.
2305 * used_address->name_len is initialized to UINT_MAX so that the first
2306 * destination address never matches.
2307 */
Mathieu Desnoyersbc909d92011-08-24 19:45:03 -07002308 if (used_address && msg_sys->msg_name &&
2309 used_address->name_len == msg_sys->msg_namelen &&
2310 !memcmp(&used_address->name, msg_sys->msg_name,
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002311 used_address->name_len)) {
Al Virod8725c82014-12-11 00:02:50 -05002312 err = sock_sendmsg_nosec(sock, msg_sys);
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002313 goto out_freectl;
2314 }
Al Virod8725c82014-12-11 00:02:50 -05002315 err = sock_sendmsg(sock, msg_sys);
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002316 /*
2317 * If this is sendmmsg() and sending to current destination address was
2318 * successful, remember it.
2319 */
2320 if (used_address && err >= 0) {
2321 used_address->name_len = msg_sys->msg_namelen;
Mathieu Desnoyersbc909d92011-08-24 19:45:03 -07002322 if (msg_sys->msg_name)
2323 memcpy(&used_address->name, msg_sys->msg_name,
2324 used_address->name_len);
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
2327out_freectl:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002328 if (ctl_buf != ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 sock_kfree_s(sock->sk, ctl_buf, ctl_len);
2330out_freeiov:
Al Viroda184282015-03-21 19:29:06 -04002331 kfree(iov);
Anton Blanchard228e5482011-05-02 20:21:35 +00002332 return err;
2333}
2334
2335/*
2336 * BSD sendmsg interface
2337 */
2338
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002339long __sys_sendmsg(int fd, struct user_msghdr __user *msg, unsigned int flags,
2340 bool forbid_cmsg_compat)
Anton Blanchard228e5482011-05-02 20:21:35 +00002341{
2342 int fput_needed, err;
2343 struct msghdr msg_sys;
Andy Lutomirski1be374a2013-05-22 14:07:44 -07002344 struct socket *sock;
Anton Blanchard228e5482011-05-02 20:21:35 +00002345
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002346 if (forbid_cmsg_compat && (flags & MSG_CMSG_COMPAT))
2347 return -EINVAL;
2348
Andy Lutomirski1be374a2013-05-22 14:07:44 -07002349 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Anton Blanchard228e5482011-05-02 20:21:35 +00002350 if (!sock)
2351 goto out;
2352
Tom Herbert28a94d82016-03-07 14:11:02 -08002353 err = ___sys_sendmsg(sock, msg, &msg_sys, flags, NULL, 0);
Anton Blanchard228e5482011-05-02 20:21:35 +00002354
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002355 fput_light(sock->file, fput_needed);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002356out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 return err;
2358}
2359
Al Viro666547f2014-04-06 14:03:05 -04002360SYSCALL_DEFINE3(sendmsg, int, fd, struct user_msghdr __user *, msg, unsigned int, flags)
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002361{
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002362 return __sys_sendmsg(fd, msg, flags, true);
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002363}
2364
Anton Blanchard228e5482011-05-02 20:21:35 +00002365/*
2366 * Linux sendmmsg interface
2367 */
2368
2369int __sys_sendmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002370 unsigned int flags, bool forbid_cmsg_compat)
Anton Blanchard228e5482011-05-02 20:21:35 +00002371{
2372 int fput_needed, err, datagrams;
2373 struct socket *sock;
2374 struct mmsghdr __user *entry;
2375 struct compat_mmsghdr __user *compat_entry;
2376 struct msghdr msg_sys;
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002377 struct used_address used_address;
Tom Herbertf0922762016-03-07 14:11:03 -08002378 unsigned int oflags = flags;
Anton Blanchard228e5482011-05-02 20:21:35 +00002379
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002380 if (forbid_cmsg_compat && (flags & MSG_CMSG_COMPAT))
2381 return -EINVAL;
2382
Anton Blanchard98382f42011-08-04 14:07:39 +00002383 if (vlen > UIO_MAXIOV)
2384 vlen = UIO_MAXIOV;
Anton Blanchard228e5482011-05-02 20:21:35 +00002385
2386 datagrams = 0;
2387
2388 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2389 if (!sock)
2390 return err;
2391
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002392 used_address.name_len = UINT_MAX;
Anton Blanchard228e5482011-05-02 20:21:35 +00002393 entry = mmsg;
2394 compat_entry = (struct compat_mmsghdr __user *)mmsg;
Anton Blanchard728ffb82011-08-04 14:07:38 +00002395 err = 0;
Tom Herbertf0922762016-03-07 14:11:03 -08002396 flags |= MSG_BATCH;
Anton Blanchard228e5482011-05-02 20:21:35 +00002397
2398 while (datagrams < vlen) {
Tom Herbertf0922762016-03-07 14:11:03 -08002399 if (datagrams == vlen - 1)
2400 flags = oflags;
2401
Anton Blanchard228e5482011-05-02 20:21:35 +00002402 if (MSG_CMSG_COMPAT & flags) {
Al Viro666547f2014-04-06 14:03:05 -04002403 err = ___sys_sendmsg(sock, (struct user_msghdr __user *)compat_entry,
Tom Herbert28a94d82016-03-07 14:11:02 -08002404 &msg_sys, flags, &used_address, MSG_EOR);
Anton Blanchard228e5482011-05-02 20:21:35 +00002405 if (err < 0)
2406 break;
2407 err = __put_user(err, &compat_entry->msg_len);
2408 ++compat_entry;
2409 } else {
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002410 err = ___sys_sendmsg(sock,
Al Viro666547f2014-04-06 14:03:05 -04002411 (struct user_msghdr __user *)entry,
Tom Herbert28a94d82016-03-07 14:11:02 -08002412 &msg_sys, flags, &used_address, MSG_EOR);
Anton Blanchard228e5482011-05-02 20:21:35 +00002413 if (err < 0)
2414 break;
2415 err = put_user(err, &entry->msg_len);
2416 ++entry;
2417 }
2418
2419 if (err)
2420 break;
2421 ++datagrams;
Soheil Hassas Yeganeh30238982016-11-04 15:36:49 -04002422 if (msg_data_left(&msg_sys))
2423 break;
Eric Dumazeta78cb842016-01-08 08:37:20 -08002424 cond_resched();
Anton Blanchard228e5482011-05-02 20:21:35 +00002425 }
2426
Anton Blanchard228e5482011-05-02 20:21:35 +00002427 fput_light(sock->file, fput_needed);
2428
Anton Blanchard728ffb82011-08-04 14:07:38 +00002429 /* We only return an error if no datagrams were able to be sent */
2430 if (datagrams != 0)
Anton Blanchard228e5482011-05-02 20:21:35 +00002431 return datagrams;
2432
Anton Blanchard228e5482011-05-02 20:21:35 +00002433 return err;
2434}
2435
2436SYSCALL_DEFINE4(sendmmsg, int, fd, struct mmsghdr __user *, mmsg,
2437 unsigned int, vlen, unsigned int, flags)
2438{
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002439 return __sys_sendmmsg(fd, mmsg, vlen, flags, true);
Anton Blanchard228e5482011-05-02 20:21:35 +00002440}
2441
Al Viro666547f2014-04-06 14:03:05 -04002442static int ___sys_recvmsg(struct socket *sock, struct user_msghdr __user *msg,
Eric Dumazet95c96172012-04-15 05:58:06 +00002443 struct msghdr *msg_sys, unsigned int flags, int nosec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444{
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002445 struct compat_msghdr __user *msg_compat =
2446 (struct compat_msghdr __user *)msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 struct iovec iovstack[UIO_FASTIOV];
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002448 struct iovec *iov = iovstack;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 unsigned long cmsg_ptr;
Al Viro2da62902015-03-14 21:13:46 -04002450 int len;
Al Viro08adb7d2014-11-10 20:23:13 -05002451 ssize_t err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452
2453 /* kernel mode address */
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07002454 struct sockaddr_storage addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455
2456 /* user mode address pointers */
2457 struct sockaddr __user *uaddr;
Al Viro08adb7d2014-11-10 20:23:13 -05002458 int __user *uaddr_len = COMPAT_NAMELEN(msg);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002459
Al Viro08adb7d2014-11-10 20:23:13 -05002460 msg_sys->msg_name = &addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01002462 if (MSG_CMSG_COMPAT & flags)
Al Viro08adb7d2014-11-10 20:23:13 -05002463 err = get_compat_msghdr(msg_sys, msg_compat, &uaddr, &iov);
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01002464 else
Al Viro08adb7d2014-11-10 20:23:13 -05002465 err = copy_msghdr_from_user(msg_sys, msg, &uaddr, &iov);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 if (err < 0)
Al Viroda184282015-03-21 19:29:06 -04002467 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002469 cmsg_ptr = (unsigned long)msg_sys->msg_control;
2470 msg_sys->msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002471
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01002472 /* We assume all kernel code knows the size of sockaddr_storage */
2473 msg_sys->msg_namelen = 0;
2474
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 if (sock->file->f_flags & O_NONBLOCK)
2476 flags |= MSG_DONTWAIT;
Al Viro2da62902015-03-14 21:13:46 -04002477 err = (nosec ? sock_recvmsg_nosec : sock_recvmsg)(sock, msg_sys, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 if (err < 0)
2479 goto out_freeiov;
2480 len = err;
2481
2482 if (uaddr != NULL) {
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00002483 err = move_addr_to_user(&addr,
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002484 msg_sys->msg_namelen, uaddr,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002485 uaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 if (err < 0)
2487 goto out_freeiov;
2488 }
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002489 err = __put_user((msg_sys->msg_flags & ~MSG_CMSG_COMPAT),
David S. Miller37f7f422005-09-16 16:51:01 -07002490 COMPAT_FLAGS(msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 if (err)
2492 goto out_freeiov;
2493 if (MSG_CMSG_COMPAT & flags)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002494 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 &msg_compat->msg_controllen);
2496 else
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002497 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 &msg->msg_controllen);
2499 if (err)
2500 goto out_freeiov;
2501 err = len;
2502
2503out_freeiov:
Al Viroda184282015-03-21 19:29:06 -04002504 kfree(iov);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002505 return err;
2506}
2507
2508/*
2509 * BSD recvmsg interface
2510 */
2511
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002512long __sys_recvmsg(int fd, struct user_msghdr __user *msg, unsigned int flags,
2513 bool forbid_cmsg_compat)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002514{
2515 int fput_needed, err;
2516 struct msghdr msg_sys;
Andy Lutomirski1be374a2013-05-22 14:07:44 -07002517 struct socket *sock;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002518
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002519 if (forbid_cmsg_compat && (flags & MSG_CMSG_COMPAT))
2520 return -EINVAL;
2521
Andy Lutomirski1be374a2013-05-22 14:07:44 -07002522 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002523 if (!sock)
2524 goto out;
2525
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002526 err = ___sys_recvmsg(sock, msg, &msg_sys, flags, 0);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002527
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002528 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529out:
2530 return err;
2531}
2532
Al Viro666547f2014-04-06 14:03:05 -04002533SYSCALL_DEFINE3(recvmsg, int, fd, struct user_msghdr __user *, msg,
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002534 unsigned int, flags)
2535{
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002536 return __sys_recvmsg(fd, msg, flags, true);
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002537}
2538
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002539/*
2540 * Linux recvmmsg interface
2541 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542
Arnd Bergmanne11d4282018-04-18 13:43:52 +02002543static int do_recvmmsg(int fd, struct mmsghdr __user *mmsg,
2544 unsigned int vlen, unsigned int flags,
2545 struct timespec64 *timeout)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002546{
2547 int fput_needed, err, datagrams;
2548 struct socket *sock;
2549 struct mmsghdr __user *entry;
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002550 struct compat_mmsghdr __user *compat_entry;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002551 struct msghdr msg_sys;
Deepa Dinamani766b9f92016-05-19 17:09:05 -07002552 struct timespec64 end_time;
2553 struct timespec64 timeout64;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002554
2555 if (timeout &&
2556 poll_select_set_timeout(&end_time, timeout->tv_sec,
2557 timeout->tv_nsec))
2558 return -EINVAL;
2559
2560 datagrams = 0;
2561
2562 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2563 if (!sock)
2564 return err;
2565
Soheil Hassas Yeganeh7797dc42018-02-27 18:22:40 -05002566 if (likely(!(flags & MSG_ERRQUEUE))) {
2567 err = sock_error(sock->sk);
2568 if (err) {
2569 datagrams = err;
2570 goto out_put;
2571 }
Maxime Jayate623a9e2017-02-21 18:35:51 +01002572 }
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002573
2574 entry = mmsg;
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002575 compat_entry = (struct compat_mmsghdr __user *)mmsg;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002576
2577 while (datagrams < vlen) {
2578 /*
2579 * No need to ask LSM for more than the first datagram.
2580 */
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002581 if (MSG_CMSG_COMPAT & flags) {
Al Viro666547f2014-04-06 14:03:05 -04002582 err = ___sys_recvmsg(sock, (struct user_msghdr __user *)compat_entry,
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002583 &msg_sys, flags & ~MSG_WAITFORONE,
2584 datagrams);
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002585 if (err < 0)
2586 break;
2587 err = __put_user(err, &compat_entry->msg_len);
2588 ++compat_entry;
2589 } else {
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002590 err = ___sys_recvmsg(sock,
Al Viro666547f2014-04-06 14:03:05 -04002591 (struct user_msghdr __user *)entry,
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002592 &msg_sys, flags & ~MSG_WAITFORONE,
2593 datagrams);
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002594 if (err < 0)
2595 break;
2596 err = put_user(err, &entry->msg_len);
2597 ++entry;
2598 }
2599
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002600 if (err)
2601 break;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002602 ++datagrams;
2603
Brandon L Black71c5c1592010-03-26 16:18:03 +00002604 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
2605 if (flags & MSG_WAITFORONE)
2606 flags |= MSG_DONTWAIT;
2607
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002608 if (timeout) {
Deepa Dinamani766b9f92016-05-19 17:09:05 -07002609 ktime_get_ts64(&timeout64);
Arnd Bergmannc2e6c852018-04-18 13:42:25 +02002610 *timeout = timespec64_sub(end_time, timeout64);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002611 if (timeout->tv_sec < 0) {
2612 timeout->tv_sec = timeout->tv_nsec = 0;
2613 break;
2614 }
2615
2616 /* Timeout, return less than vlen datagrams */
2617 if (timeout->tv_nsec == 0 && timeout->tv_sec == 0)
2618 break;
2619 }
2620
2621 /* Out of band data, return right away */
2622 if (msg_sys.msg_flags & MSG_OOB)
2623 break;
Eric Dumazeta78cb842016-01-08 08:37:20 -08002624 cond_resched();
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002625 }
2626
Arnaldo Carvalho de Melo34b88a62016-03-14 09:56:35 -03002627 if (err == 0)
2628 goto out_put;
2629
2630 if (datagrams == 0) {
2631 datagrams = err;
2632 goto out_put;
2633 }
2634
2635 /*
2636 * We may return less entries than requested (vlen) if the
2637 * sock is non block and there aren't enough datagrams...
2638 */
2639 if (err != -EAGAIN) {
2640 /*
2641 * ... or if recvmsg returns an error after we
2642 * received some datagrams, where we record the
2643 * error to return on the next call or if the
2644 * app asks about it using getsockopt(SO_ERROR).
2645 */
2646 sock->sk->sk_err = -err;
2647 }
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002648out_put:
2649 fput_light(sock->file, fput_needed);
2650
Arnaldo Carvalho de Melo34b88a62016-03-14 09:56:35 -03002651 return datagrams;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002652}
2653
Arnd Bergmanne11d4282018-04-18 13:43:52 +02002654int __sys_recvmmsg(int fd, struct mmsghdr __user *mmsg,
2655 unsigned int vlen, unsigned int flags,
2656 struct __kernel_timespec __user *timeout,
2657 struct old_timespec32 __user *timeout32)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002658{
2659 int datagrams;
Arnd Bergmannc2e6c852018-04-18 13:42:25 +02002660 struct timespec64 timeout_sys;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002661
Arnd Bergmanne11d4282018-04-18 13:43:52 +02002662 if (timeout && get_timespec64(&timeout_sys, timeout))
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002663 return -EFAULT;
2664
Arnd Bergmanne11d4282018-04-18 13:43:52 +02002665 if (timeout32 && get_old_timespec32(&timeout_sys, timeout32))
2666 return -EFAULT;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002667
Arnd Bergmanne11d4282018-04-18 13:43:52 +02002668 if (!timeout && !timeout32)
2669 return do_recvmmsg(fd, mmsg, vlen, flags, NULL);
2670
2671 datagrams = do_recvmmsg(fd, mmsg, vlen, flags, &timeout_sys);
2672
2673 if (datagrams <= 0)
2674 return datagrams;
2675
2676 if (timeout && put_timespec64(&timeout_sys, timeout))
2677 datagrams = -EFAULT;
2678
2679 if (timeout32 && put_old_timespec32(&timeout_sys, timeout32))
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002680 datagrams = -EFAULT;
2681
2682 return datagrams;
2683}
2684
Dominik Brodowski1255e262018-03-13 20:44:21 +01002685SYSCALL_DEFINE5(recvmmsg, int, fd, struct mmsghdr __user *, mmsg,
2686 unsigned int, vlen, unsigned int, flags,
Arnd Bergmannc2e6c852018-04-18 13:42:25 +02002687 struct __kernel_timespec __user *, timeout)
Dominik Brodowski1255e262018-03-13 20:44:21 +01002688{
Arnd Bergmanne11d4282018-04-18 13:43:52 +02002689 if (flags & MSG_CMSG_COMPAT)
2690 return -EINVAL;
2691
2692 return __sys_recvmmsg(fd, mmsg, vlen, flags, timeout, NULL);
Dominik Brodowski1255e262018-03-13 20:44:21 +01002693}
2694
Arnd Bergmanne11d4282018-04-18 13:43:52 +02002695#ifdef CONFIG_COMPAT_32BIT_TIME
2696SYSCALL_DEFINE5(recvmmsg_time32, int, fd, struct mmsghdr __user *, mmsg,
2697 unsigned int, vlen, unsigned int, flags,
2698 struct old_timespec32 __user *, timeout)
2699{
2700 if (flags & MSG_CMSG_COMPAT)
2701 return -EINVAL;
2702
2703 return __sys_recvmmsg(fd, mmsg, vlen, flags, NULL, timeout);
2704}
2705#endif
2706
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002707#ifdef __ARCH_WANT_SYS_SOCKETCALL
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708/* Argument list sizes for sys_socketcall */
2709#define AL(x) ((x) * sizeof(unsigned long))
Anton Blanchard228e5482011-05-02 20:21:35 +00002710static const unsigned char nargs[21] = {
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002711 AL(0), AL(3), AL(3), AL(3), AL(2), AL(3),
2712 AL(3), AL(3), AL(4), AL(4), AL(4), AL(6),
2713 AL(6), AL(2), AL(5), AL(5), AL(3), AL(3),
Anton Blanchard228e5482011-05-02 20:21:35 +00002714 AL(4), AL(5), AL(4)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002715};
2716
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717#undef AL
2718
2719/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002720 * System call vectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 *
2722 * Argument checking cleaned up. Saved 20% in size.
2723 * This function doesn't need to set the kernel lock because
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002724 * it is set by the callees.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 */
2726
Heiko Carstens3e0fa652009-01-14 14:14:24 +01002727SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728{
Chen Gang2950fa92013-04-07 16:55:23 +08002729 unsigned long a[AUDITSC_ARGS];
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002730 unsigned long a0, a1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 int err;
Arjan van de Ven47379052009-09-28 12:57:44 -07002732 unsigned int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733
Anton Blanchard228e5482011-05-02 20:21:35 +00002734 if (call < 1 || call > SYS_SENDMMSG)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 return -EINVAL;
Jeremy Clinec8e8cd52018-07-27 22:43:01 +00002736 call = array_index_nospec(call, SYS_SENDMMSG + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737
Arjan van de Ven47379052009-09-28 12:57:44 -07002738 len = nargs[call];
2739 if (len > sizeof(a))
2740 return -EINVAL;
2741
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 /* copy_from_user should be SMP safe. */
Arjan van de Ven47379052009-09-28 12:57:44 -07002743 if (copy_from_user(a, args, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 return -EFAULT;
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002745
Chen Gang2950fa92013-04-07 16:55:23 +08002746 err = audit_socketcall(nargs[call] / sizeof(unsigned long), a);
2747 if (err)
2748 return err;
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002749
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002750 a0 = a[0];
2751 a1 = a[1];
2752
2753 switch (call) {
2754 case SYS_SOCKET:
Dominik Brodowski9d6a15c2018-03-13 19:29:43 +01002755 err = __sys_socket(a0, a1, a[2]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002756 break;
2757 case SYS_BIND:
Dominik Brodowskia87d35d2018-03-13 19:33:09 +01002758 err = __sys_bind(a0, (struct sockaddr __user *)a1, a[2]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002759 break;
2760 case SYS_CONNECT:
Dominik Brodowski1387c2c2018-03-13 19:35:09 +01002761 err = __sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002762 break;
2763 case SYS_LISTEN:
Dominik Brodowski25e290e2018-03-13 19:36:54 +01002764 err = __sys_listen(a0, a1);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002765 break;
2766 case SYS_ACCEPT:
Dominik Brodowski4541e802018-03-13 19:24:23 +01002767 err = __sys_accept4(a0, (struct sockaddr __user *)a1,
2768 (int __user *)a[2], 0);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002769 break;
2770 case SYS_GETSOCKNAME:
2771 err =
Dominik Brodowski8882a102018-03-13 19:43:14 +01002772 __sys_getsockname(a0, (struct sockaddr __user *)a1,
2773 (int __user *)a[2]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002774 break;
2775 case SYS_GETPEERNAME:
2776 err =
Dominik Brodowskib21c8f82018-03-13 19:47:00 +01002777 __sys_getpeername(a0, (struct sockaddr __user *)a1,
2778 (int __user *)a[2]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002779 break;
2780 case SYS_SOCKETPAIR:
Dominik Brodowski6debc8d2018-03-13 19:49:23 +01002781 err = __sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002782 break;
2783 case SYS_SEND:
Dominik Brodowskif3bf8962018-03-13 19:52:00 +01002784 err = __sys_sendto(a0, (void __user *)a1, a[2], a[3],
2785 NULL, 0);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002786 break;
2787 case SYS_SENDTO:
Dominik Brodowski211b6342018-03-13 19:18:52 +01002788 err = __sys_sendto(a0, (void __user *)a1, a[2], a[3],
2789 (struct sockaddr __user *)a[4], a[5]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002790 break;
2791 case SYS_RECV:
Dominik Brodowskid27e9af2018-03-13 19:54:17 +01002792 err = __sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
2793 NULL, NULL);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002794 break;
2795 case SYS_RECVFROM:
Dominik Brodowski7a09e1e2018-03-13 19:10:06 +01002796 err = __sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
2797 (struct sockaddr __user *)a[4],
2798 (int __user *)a[5]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002799 break;
2800 case SYS_SHUTDOWN:
Dominik Brodowski005a1ae2018-03-13 20:07:05 +01002801 err = __sys_shutdown(a0, a1);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002802 break;
2803 case SYS_SETSOCKOPT:
Dominik Brodowskicc36dca2018-03-13 20:10:59 +01002804 err = __sys_setsockopt(a0, a1, a[2], (char __user *)a[3],
2805 a[4]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002806 break;
2807 case SYS_GETSOCKOPT:
2808 err =
Dominik Brodowski13a2d702018-03-13 20:15:04 +01002809 __sys_getsockopt(a0, a1, a[2], (char __user *)a[3],
2810 (int __user *)a[4]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002811 break;
2812 case SYS_SENDMSG:
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002813 err = __sys_sendmsg(a0, (struct user_msghdr __user *)a1,
2814 a[2], true);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002815 break;
Anton Blanchard228e5482011-05-02 20:21:35 +00002816 case SYS_SENDMMSG:
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002817 err = __sys_sendmmsg(a0, (struct mmsghdr __user *)a1, a[2],
2818 a[3], true);
Anton Blanchard228e5482011-05-02 20:21:35 +00002819 break;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002820 case SYS_RECVMSG:
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002821 err = __sys_recvmsg(a0, (struct user_msghdr __user *)a1,
2822 a[2], true);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002823 break;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002824 case SYS_RECVMMSG:
Arnd Bergmanne11d4282018-04-18 13:43:52 +02002825 if (IS_ENABLED(CONFIG_64BIT) || !IS_ENABLED(CONFIG_64BIT_TIME))
2826 err = __sys_recvmmsg(a0, (struct mmsghdr __user *)a1,
2827 a[2], a[3],
2828 (struct __kernel_timespec __user *)a[4],
2829 NULL);
2830 else
2831 err = __sys_recvmmsg(a0, (struct mmsghdr __user *)a1,
2832 a[2], a[3], NULL,
2833 (struct old_timespec32 __user *)a[4]);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002834 break;
Ulrich Drepperde11def2008-11-19 15:36:14 -08002835 case SYS_ACCEPT4:
Dominik Brodowski4541e802018-03-13 19:24:23 +01002836 err = __sys_accept4(a0, (struct sockaddr __user *)a1,
2837 (int __user *)a[2], a[3]);
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07002838 break;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002839 default:
2840 err = -EINVAL;
2841 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 }
2843 return err;
2844}
2845
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002846#endif /* __ARCH_WANT_SYS_SOCKETCALL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002848/**
2849 * sock_register - add a socket protocol handler
2850 * @ops: description of protocol
2851 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 * This function is called by a protocol handler that wants to
2853 * advertise its address family, and have it linked into the
Masanari Iidae793c0f2014-09-04 23:44:36 +09002854 * socket interface. The value ops->family corresponds to the
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002855 * socket system call protocol family.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 */
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002857int sock_register(const struct net_proto_family *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858{
2859 int err;
2860
2861 if (ops->family >= NPROTO) {
Yang Yingliang3410f222014-02-12 17:09:55 +08002862 pr_crit("protocol %d >= NPROTO(%d)\n", ops->family, NPROTO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 return -ENOBUFS;
2864 }
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002865
2866 spin_lock(&net_family_lock);
Eric Dumazet190683a2010-11-10 10:50:44 +00002867 if (rcu_dereference_protected(net_families[ops->family],
2868 lockdep_is_held(&net_family_lock)))
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002869 err = -EEXIST;
2870 else {
Eric Dumazetcf778b02012-01-12 04:41:32 +00002871 rcu_assign_pointer(net_families[ops->family], ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 err = 0;
2873 }
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002874 spin_unlock(&net_family_lock);
2875
Yang Yingliang3410f222014-02-12 17:09:55 +08002876 pr_info("NET: Registered protocol family %d\n", ops->family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 return err;
2878}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002879EXPORT_SYMBOL(sock_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002881/**
2882 * sock_unregister - remove a protocol handler
2883 * @family: protocol family to remove
2884 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 * This function is called by a protocol handler that wants to
2886 * remove its address family, and have it unlinked from the
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002887 * new socket creation.
2888 *
2889 * If protocol handler is a module, then it can use module reference
2890 * counts to protect against new references. If protocol handler is not
2891 * a module then it needs to provide its own protection in
2892 * the ops->create routine.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 */
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002894void sock_unregister(int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895{
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002896 BUG_ON(family < 0 || family >= NPROTO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002898 spin_lock(&net_family_lock);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00002899 RCU_INIT_POINTER(net_families[family], NULL);
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002900 spin_unlock(&net_family_lock);
2901
2902 synchronize_rcu();
2903
Yang Yingliang3410f222014-02-12 17:09:55 +08002904 pr_info("NET: Unregistered protocol family %d\n", family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002906EXPORT_SYMBOL(sock_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907
Xin Longbf2ae2e2018-03-10 18:57:50 +08002908bool sock_is_registered(int family)
2909{
Jeremy Cline66b51b02018-08-13 22:23:13 +00002910 return family < NPROTO && rcu_access_pointer(net_families[family]);
Xin Longbf2ae2e2018-03-10 18:57:50 +08002911}
2912
Andi Kleen77d76ea2005-12-22 12:43:42 -08002913static int __init sock_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914{
Nick Pigginb3e19d92011-01-07 17:50:11 +11002915 int err;
Eric W. Biederman2ca794e2012-04-19 13:20:32 +00002916 /*
2917 * Initialize the network sysctl infrastructure.
2918 */
2919 err = net_sysctl_init();
2920 if (err)
2921 goto out;
Nick Pigginb3e19d92011-01-07 17:50:11 +11002922
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002924 * Initialize skbuff SLAB cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 */
2926 skb_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927
2928 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002929 * Initialize the protocols module.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 */
2931
2932 init_inodecache();
Nick Pigginb3e19d92011-01-07 17:50:11 +11002933
2934 err = register_filesystem(&sock_fs_type);
2935 if (err)
2936 goto out_fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 sock_mnt = kern_mount(&sock_fs_type);
Nick Pigginb3e19d92011-01-07 17:50:11 +11002938 if (IS_ERR(sock_mnt)) {
2939 err = PTR_ERR(sock_mnt);
2940 goto out_mount;
2941 }
Andi Kleen77d76ea2005-12-22 12:43:42 -08002942
2943 /* The real protocol initialization is performed in later initcalls.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 */
2945
2946#ifdef CONFIG_NETFILTER
Pablo Neira Ayuso6d11cfd2013-05-22 22:42:36 +00002947 err = netfilter_init();
2948 if (err)
2949 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950#endif
David S. Millercbeb3212005-12-22 12:58:55 -08002951
Daniel Borkmann408eccc2014-04-01 16:20:23 +02002952 ptp_classifier_init();
Richard Cochranc1f19b52010-07-17 08:49:36 +00002953
Nick Pigginb3e19d92011-01-07 17:50:11 +11002954out:
2955 return err;
2956
2957out_mount:
2958 unregister_filesystem(&sock_fs_type);
2959out_fs:
2960 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961}
2962
Andi Kleen77d76ea2005-12-22 12:43:42 -08002963core_initcall(sock_init); /* early initcall */
2964
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965#ifdef CONFIG_PROC_FS
2966void socket_seq_show(struct seq_file *seq)
2967{
Tonghao Zhang648845a2017-12-14 05:51:58 -08002968 seq_printf(seq, "sockets: used %d\n",
2969 sock_inuse_get(seq->private));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970}
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002971#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972
Shaun Pereira89bbfc92006-03-21 23:58:08 -08002973#ifdef CONFIG_COMPAT
Al Viro36fd6332017-06-26 13:19:16 -04002974static int compat_dev_ifconf(struct net *net, struct compat_ifconf __user *uifc32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002975{
Arnd Bergmann6b960182009-11-06 23:10:54 -08002976 struct compat_ifconf ifc32;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002977 struct ifconf ifc;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002978 int err;
2979
Arnd Bergmann6b960182009-11-06 23:10:54 -08002980 if (copy_from_user(&ifc32, uifc32, sizeof(struct compat_ifconf)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002981 return -EFAULT;
2982
Al Viro36fd6332017-06-26 13:19:16 -04002983 ifc.ifc_len = ifc32.ifc_len;
2984 ifc.ifc_req = compat_ptr(ifc32.ifcbuf);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002985
Al Viro36fd6332017-06-26 13:19:16 -04002986 rtnl_lock();
2987 err = dev_ifconf(net, &ifc, sizeof(struct compat_ifreq));
2988 rtnl_unlock();
Arnd Bergmann7a229382009-11-06 23:00:29 -08002989 if (err)
2990 return err;
2991
Al Viro36fd6332017-06-26 13:19:16 -04002992 ifc32.ifc_len = ifc.ifc_len;
Arnd Bergmann6b960182009-11-06 23:10:54 -08002993 if (copy_to_user(uifc32, &ifc32, sizeof(struct compat_ifconf)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002994 return -EFAULT;
2995
2996 return 0;
2997}
2998
Arnd Bergmann6b960182009-11-06 23:10:54 -08002999static int ethtool_ioctl(struct net *net, struct compat_ifreq __user *ifr32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08003000{
Ben Hutchings3a7da392011-03-17 07:34:32 +00003001 struct compat_ethtool_rxnfc __user *compat_rxnfc;
3002 bool convert_in = false, convert_out = false;
Al Viro44c02a22017-10-05 12:59:44 -04003003 size_t buf_size = 0;
3004 struct ethtool_rxnfc __user *rxnfc = NULL;
3005 struct ifreq ifr;
Ben Hutchings3a7da392011-03-17 07:34:32 +00003006 u32 rule_cnt = 0, actual_rule_cnt;
3007 u32 ethcmd;
Arnd Bergmann7a229382009-11-06 23:00:29 -08003008 u32 data;
Ben Hutchings3a7da392011-03-17 07:34:32 +00003009 int ret;
Arnd Bergmann7a229382009-11-06 23:00:29 -08003010
3011 if (get_user(data, &ifr32->ifr_ifru.ifru_data))
3012 return -EFAULT;
3013
Ben Hutchings3a7da392011-03-17 07:34:32 +00003014 compat_rxnfc = compat_ptr(data);
3015
3016 if (get_user(ethcmd, &compat_rxnfc->cmd))
Arnd Bergmann7a229382009-11-06 23:00:29 -08003017 return -EFAULT;
3018
Ben Hutchings3a7da392011-03-17 07:34:32 +00003019 /* Most ethtool structures are defined without padding.
3020 * Unfortunately struct ethtool_rxnfc is an exception.
3021 */
3022 switch (ethcmd) {
3023 default:
3024 break;
3025 case ETHTOOL_GRXCLSRLALL:
3026 /* Buffer size is variable */
3027 if (get_user(rule_cnt, &compat_rxnfc->rule_cnt))
3028 return -EFAULT;
3029 if (rule_cnt > KMALLOC_MAX_SIZE / sizeof(u32))
3030 return -ENOMEM;
3031 buf_size += rule_cnt * sizeof(u32);
3032 /* fall through */
3033 case ETHTOOL_GRXRINGS:
3034 case ETHTOOL_GRXCLSRLCNT:
3035 case ETHTOOL_GRXCLSRULE:
Ben Hutchings55664f32012-01-03 12:04:51 +00003036 case ETHTOOL_SRXCLSRLINS:
Ben Hutchings3a7da392011-03-17 07:34:32 +00003037 convert_out = true;
3038 /* fall through */
3039 case ETHTOOL_SRXCLSRLDEL:
Ben Hutchings3a7da392011-03-17 07:34:32 +00003040 buf_size += sizeof(struct ethtool_rxnfc);
3041 convert_in = true;
Al Viro44c02a22017-10-05 12:59:44 -04003042 rxnfc = compat_alloc_user_space(buf_size);
Ben Hutchings3a7da392011-03-17 07:34:32 +00003043 break;
3044 }
3045
Al Viro44c02a22017-10-05 12:59:44 -04003046 if (copy_from_user(&ifr.ifr_name, &ifr32->ifr_name, IFNAMSIZ))
Ben Hutchings3a7da392011-03-17 07:34:32 +00003047 return -EFAULT;
3048
Al Viro44c02a22017-10-05 12:59:44 -04003049 ifr.ifr_data = convert_in ? rxnfc : (void __user *)compat_rxnfc;
Ben Hutchings3a7da392011-03-17 07:34:32 +00003050
3051 if (convert_in) {
Alexander Duyck127fe532011-04-08 18:01:59 +00003052 /* We expect there to be holes between fs.m_ext and
Ben Hutchings3a7da392011-03-17 07:34:32 +00003053 * fs.ring_cookie and at the end of fs, but nowhere else.
3054 */
Alexander Duyck127fe532011-04-08 18:01:59 +00003055 BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc, fs.m_ext) +
3056 sizeof(compat_rxnfc->fs.m_ext) !=
3057 offsetof(struct ethtool_rxnfc, fs.m_ext) +
3058 sizeof(rxnfc->fs.m_ext));
Ben Hutchings3a7da392011-03-17 07:34:32 +00003059 BUILD_BUG_ON(
3060 offsetof(struct compat_ethtool_rxnfc, fs.location) -
3061 offsetof(struct compat_ethtool_rxnfc, fs.ring_cookie) !=
3062 offsetof(struct ethtool_rxnfc, fs.location) -
3063 offsetof(struct ethtool_rxnfc, fs.ring_cookie));
3064
3065 if (copy_in_user(rxnfc, compat_rxnfc,
Stephen Hemminger954b1242013-02-11 06:22:28 +00003066 (void __user *)(&rxnfc->fs.m_ext + 1) -
3067 (void __user *)rxnfc) ||
Ben Hutchings3a7da392011-03-17 07:34:32 +00003068 copy_in_user(&rxnfc->fs.ring_cookie,
3069 &compat_rxnfc->fs.ring_cookie,
Stephen Hemminger954b1242013-02-11 06:22:28 +00003070 (void __user *)(&rxnfc->fs.location + 1) -
Wenwen Wangb6168562018-10-18 09:36:46 -05003071 (void __user *)&rxnfc->fs.ring_cookie))
3072 return -EFAULT;
3073 if (ethcmd == ETHTOOL_GRXCLSRLALL) {
3074 if (put_user(rule_cnt, &rxnfc->rule_cnt))
3075 return -EFAULT;
3076 } else if (copy_in_user(&rxnfc->rule_cnt,
3077 &compat_rxnfc->rule_cnt,
3078 sizeof(rxnfc->rule_cnt)))
Ben Hutchings3a7da392011-03-17 07:34:32 +00003079 return -EFAULT;
3080 }
3081
Al Viro44c02a22017-10-05 12:59:44 -04003082 ret = dev_ioctl(net, SIOCETHTOOL, &ifr, NULL);
Ben Hutchings3a7da392011-03-17 07:34:32 +00003083 if (ret)
3084 return ret;
3085
3086 if (convert_out) {
3087 if (copy_in_user(compat_rxnfc, rxnfc,
Stephen Hemminger954b1242013-02-11 06:22:28 +00003088 (const void __user *)(&rxnfc->fs.m_ext + 1) -
3089 (const void __user *)rxnfc) ||
Ben Hutchings3a7da392011-03-17 07:34:32 +00003090 copy_in_user(&compat_rxnfc->fs.ring_cookie,
3091 &rxnfc->fs.ring_cookie,
Stephen Hemminger954b1242013-02-11 06:22:28 +00003092 (const void __user *)(&rxnfc->fs.location + 1) -
3093 (const void __user *)&rxnfc->fs.ring_cookie) ||
Ben Hutchings3a7da392011-03-17 07:34:32 +00003094 copy_in_user(&compat_rxnfc->rule_cnt, &rxnfc->rule_cnt,
3095 sizeof(rxnfc->rule_cnt)))
3096 return -EFAULT;
3097
3098 if (ethcmd == ETHTOOL_GRXCLSRLALL) {
3099 /* As an optimisation, we only copy the actual
3100 * number of rules that the underlying
3101 * function returned. Since Mallory might
3102 * change the rule count in user memory, we
3103 * check that it is less than the rule count
3104 * originally given (as the user buffer size),
3105 * which has been range-checked.
3106 */
3107 if (get_user(actual_rule_cnt, &rxnfc->rule_cnt))
3108 return -EFAULT;
3109 if (actual_rule_cnt < rule_cnt)
3110 rule_cnt = actual_rule_cnt;
3111 if (copy_in_user(&compat_rxnfc->rule_locs[0],
3112 &rxnfc->rule_locs[0],
3113 rule_cnt * sizeof(u32)))
3114 return -EFAULT;
3115 }
3116 }
3117
3118 return 0;
Arnd Bergmann7a229382009-11-06 23:00:29 -08003119}
3120
Arnd Bergmann7a50a242009-11-08 20:57:03 -08003121static int compat_siocwandev(struct net *net, struct compat_ifreq __user *uifr32)
3122{
Arnd Bergmann7a50a242009-11-08 20:57:03 -08003123 compat_uptr_t uptr32;
Al Viro44c02a22017-10-05 12:59:44 -04003124 struct ifreq ifr;
3125 void __user *saved;
3126 int err;
Arnd Bergmann7a50a242009-11-08 20:57:03 -08003127
Al Viro44c02a22017-10-05 12:59:44 -04003128 if (copy_from_user(&ifr, uifr32, sizeof(struct compat_ifreq)))
Arnd Bergmann7a50a242009-11-08 20:57:03 -08003129 return -EFAULT;
3130
3131 if (get_user(uptr32, &uifr32->ifr_settings.ifs_ifsu))
3132 return -EFAULT;
3133
Al Viro44c02a22017-10-05 12:59:44 -04003134 saved = ifr.ifr_settings.ifs_ifsu.raw_hdlc;
3135 ifr.ifr_settings.ifs_ifsu.raw_hdlc = compat_ptr(uptr32);
Arnd Bergmann7a50a242009-11-08 20:57:03 -08003136
Al Viro44c02a22017-10-05 12:59:44 -04003137 err = dev_ioctl(net, SIOCWANDEV, &ifr, NULL);
3138 if (!err) {
3139 ifr.ifr_settings.ifs_ifsu.raw_hdlc = saved;
3140 if (copy_to_user(uifr32, &ifr, sizeof(struct compat_ifreq)))
3141 err = -EFAULT;
Joe Perchesccbd6a52010-05-14 10:58:26 +00003142 }
Al Viro44c02a22017-10-05 12:59:44 -04003143 return err;
Arnd Bergmann7a229382009-11-06 23:00:29 -08003144}
3145
Ben Hutchings590d4692013-11-18 17:04:13 +00003146/* Handle ioctls that use ifreq::ifr_data and just need struct ifreq converted */
3147static int compat_ifr_data_ioctl(struct net *net, unsigned int cmd,
Arnd Bergmann6b960182009-11-06 23:10:54 -08003148 struct compat_ifreq __user *u_ifreq32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08003149{
Al Viro44c02a22017-10-05 12:59:44 -04003150 struct ifreq ifreq;
Arnd Bergmann7a229382009-11-06 23:00:29 -08003151 u32 data32;
3152
Al Viro44c02a22017-10-05 12:59:44 -04003153 if (copy_from_user(ifreq.ifr_name, u_ifreq32->ifr_name, IFNAMSIZ))
Arnd Bergmann7a229382009-11-06 23:00:29 -08003154 return -EFAULT;
Al Viro44c02a22017-10-05 12:59:44 -04003155 if (get_user(data32, &u_ifreq32->ifr_data))
Arnd Bergmann7a229382009-11-06 23:00:29 -08003156 return -EFAULT;
Al Viro44c02a22017-10-05 12:59:44 -04003157 ifreq.ifr_data = compat_ptr(data32);
Arnd Bergmann7a229382009-11-06 23:00:29 -08003158
Al Viro44c02a22017-10-05 12:59:44 -04003159 return dev_ioctl(net, cmd, &ifreq, NULL);
Arnd Bergmann7a229382009-11-06 23:00:29 -08003160}
3161
Johannes Berg37ac39b2019-01-25 22:43:18 +01003162static int compat_ifreq_ioctl(struct net *net, struct socket *sock,
3163 unsigned int cmd,
3164 struct compat_ifreq __user *uifr32)
3165{
3166 struct ifreq __user *uifr;
3167 int err;
3168
3169 /* Handle the fact that while struct ifreq has the same *layout* on
3170 * 32/64 for everything but ifreq::ifru_ifmap and ifreq::ifru_data,
3171 * which are handled elsewhere, it still has different *size* due to
3172 * ifreq::ifru_ifmap (which is 16 bytes on 32 bit, 24 bytes on 64-bit,
3173 * resulting in struct ifreq being 32 and 40 bytes respectively).
3174 * As a result, if the struct happens to be at the end of a page and
3175 * the next page isn't readable/writable, we get a fault. To prevent
3176 * that, copy back and forth to the full size.
3177 */
3178
3179 uifr = compat_alloc_user_space(sizeof(*uifr));
3180 if (copy_in_user(uifr, uifr32, sizeof(*uifr32)))
3181 return -EFAULT;
3182
3183 err = sock_do_ioctl(net, sock, cmd, (unsigned long)uifr);
3184
3185 if (!err) {
3186 switch (cmd) {
3187 case SIOCGIFFLAGS:
3188 case SIOCGIFMETRIC:
3189 case SIOCGIFMTU:
3190 case SIOCGIFMEM:
3191 case SIOCGIFHWADDR:
3192 case SIOCGIFINDEX:
3193 case SIOCGIFADDR:
3194 case SIOCGIFBRDADDR:
3195 case SIOCGIFDSTADDR:
3196 case SIOCGIFNETMASK:
3197 case SIOCGIFPFLAGS:
3198 case SIOCGIFTXQLEN:
3199 case SIOCGMIIPHY:
3200 case SIOCGMIIREG:
Johannes Bergc6c9fee2019-01-25 22:43:19 +01003201 case SIOCGIFNAME:
Johannes Berg37ac39b2019-01-25 22:43:18 +01003202 if (copy_in_user(uifr32, uifr, sizeof(*uifr32)))
3203 err = -EFAULT;
3204 break;
3205 }
3206 }
3207 return err;
3208}
3209
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003210static int compat_sioc_ifmap(struct net *net, unsigned int cmd,
3211 struct compat_ifreq __user *uifr32)
3212{
3213 struct ifreq ifr;
3214 struct compat_ifmap __user *uifmap32;
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003215 int err;
3216
3217 uifmap32 = &uifr32->ifr_ifru.ifru_map;
3218 err = copy_from_user(&ifr, uifr32, sizeof(ifr.ifr_name));
Mathieu Desnoyers3ddc5b42013-09-11 14:23:18 -07003219 err |= get_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
3220 err |= get_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
3221 err |= get_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
3222 err |= get_user(ifr.ifr_map.irq, &uifmap32->irq);
3223 err |= get_user(ifr.ifr_map.dma, &uifmap32->dma);
3224 err |= get_user(ifr.ifr_map.port, &uifmap32->port);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003225 if (err)
3226 return -EFAULT;
3227
Al Viro44c02a22017-10-05 12:59:44 -04003228 err = dev_ioctl(net, cmd, &ifr, NULL);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003229
3230 if (cmd == SIOCGIFMAP && !err) {
3231 err = copy_to_user(uifr32, &ifr, sizeof(ifr.ifr_name));
Mathieu Desnoyers3ddc5b42013-09-11 14:23:18 -07003232 err |= put_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
3233 err |= put_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
3234 err |= put_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
3235 err |= put_user(ifr.ifr_map.irq, &uifmap32->irq);
3236 err |= put_user(ifr.ifr_map.dma, &uifmap32->dma);
3237 err |= put_user(ifr.ifr_map.port, &uifmap32->port);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003238 if (err)
3239 err = -EFAULT;
3240 }
3241 return err;
3242}
3243
Arnd Bergmann7a229382009-11-06 23:00:29 -08003244struct rtentry32 {
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003245 u32 rt_pad1;
Arnd Bergmann7a229382009-11-06 23:00:29 -08003246 struct sockaddr rt_dst; /* target address */
3247 struct sockaddr rt_gateway; /* gateway addr (RTF_GATEWAY) */
3248 struct sockaddr rt_genmask; /* target network mask (IP) */
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003249 unsigned short rt_flags;
3250 short rt_pad2;
3251 u32 rt_pad3;
3252 unsigned char rt_tos;
3253 unsigned char rt_class;
3254 short rt_pad4;
3255 short rt_metric; /* +1 for binary compatibility! */
Arnd Bergmann7a229382009-11-06 23:00:29 -08003256 /* char * */ u32 rt_dev; /* forcing the device at add */
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003257 u32 rt_mtu; /* per route MTU/Window */
3258 u32 rt_window; /* Window clamping */
Arnd Bergmann7a229382009-11-06 23:00:29 -08003259 unsigned short rt_irtt; /* Initial RTT */
3260};
3261
3262struct in6_rtmsg32 {
3263 struct in6_addr rtmsg_dst;
3264 struct in6_addr rtmsg_src;
3265 struct in6_addr rtmsg_gateway;
3266 u32 rtmsg_type;
3267 u16 rtmsg_dst_len;
3268 u16 rtmsg_src_len;
3269 u32 rtmsg_metric;
3270 u32 rtmsg_info;
3271 u32 rtmsg_flags;
3272 s32 rtmsg_ifindex;
3273};
3274
Arnd Bergmann6b960182009-11-06 23:10:54 -08003275static int routing_ioctl(struct net *net, struct socket *sock,
3276 unsigned int cmd, void __user *argp)
Arnd Bergmann7a229382009-11-06 23:00:29 -08003277{
3278 int ret;
3279 void *r = NULL;
3280 struct in6_rtmsg r6;
3281 struct rtentry r4;
3282 char devname[16];
3283 u32 rtdev;
3284 mm_segment_t old_fs = get_fs();
3285
Arnd Bergmann6b960182009-11-06 23:10:54 -08003286 if (sock && sock->sk && sock->sk->sk_family == AF_INET6) { /* ipv6 */
3287 struct in6_rtmsg32 __user *ur6 = argp;
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003288 ret = copy_from_user(&r6.rtmsg_dst, &(ur6->rtmsg_dst),
Arnd Bergmann7a229382009-11-06 23:00:29 -08003289 3 * sizeof(struct in6_addr));
Mathieu Desnoyers3ddc5b42013-09-11 14:23:18 -07003290 ret |= get_user(r6.rtmsg_type, &(ur6->rtmsg_type));
3291 ret |= get_user(r6.rtmsg_dst_len, &(ur6->rtmsg_dst_len));
3292 ret |= get_user(r6.rtmsg_src_len, &(ur6->rtmsg_src_len));
3293 ret |= get_user(r6.rtmsg_metric, &(ur6->rtmsg_metric));
3294 ret |= get_user(r6.rtmsg_info, &(ur6->rtmsg_info));
3295 ret |= get_user(r6.rtmsg_flags, &(ur6->rtmsg_flags));
3296 ret |= get_user(r6.rtmsg_ifindex, &(ur6->rtmsg_ifindex));
Arnd Bergmann7a229382009-11-06 23:00:29 -08003297
3298 r = (void *) &r6;
3299 } else { /* ipv4 */
Arnd Bergmann6b960182009-11-06 23:10:54 -08003300 struct rtentry32 __user *ur4 = argp;
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003301 ret = copy_from_user(&r4.rt_dst, &(ur4->rt_dst),
Arnd Bergmann7a229382009-11-06 23:00:29 -08003302 3 * sizeof(struct sockaddr));
Mathieu Desnoyers3ddc5b42013-09-11 14:23:18 -07003303 ret |= get_user(r4.rt_flags, &(ur4->rt_flags));
3304 ret |= get_user(r4.rt_metric, &(ur4->rt_metric));
3305 ret |= get_user(r4.rt_mtu, &(ur4->rt_mtu));
3306 ret |= get_user(r4.rt_window, &(ur4->rt_window));
3307 ret |= get_user(r4.rt_irtt, &(ur4->rt_irtt));
3308 ret |= get_user(rtdev, &(ur4->rt_dev));
Arnd Bergmann7a229382009-11-06 23:00:29 -08003309 if (rtdev) {
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003310 ret |= copy_from_user(devname, compat_ptr(rtdev), 15);
stephen hemmingerc3f52ae2011-02-23 09:06:48 +00003311 r4.rt_dev = (char __user __force *)devname;
3312 devname[15] = 0;
Arnd Bergmann7a229382009-11-06 23:00:29 -08003313 } else
3314 r4.rt_dev = NULL;
3315
3316 r = (void *) &r4;
3317 }
3318
3319 if (ret) {
3320 ret = -EFAULT;
3321 goto out;
3322 }
3323
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003324 set_fs(KERNEL_DS);
Johannes Berg63ff03a2019-01-25 22:43:17 +01003325 ret = sock_do_ioctl(net, sock, cmd, (unsigned long) r);
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003326 set_fs(old_fs);
Arnd Bergmann7a229382009-11-06 23:00:29 -08003327
3328out:
Arnd Bergmann7a229382009-11-06 23:00:29 -08003329 return ret;
3330}
3331
3332/* Since old style bridge ioctl's endup using SIOCDEVPRIVATE
3333 * for some operations; this forces use of the newer bridge-utils that
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003334 * use compatible ioctls
Arnd Bergmann7a229382009-11-06 23:00:29 -08003335 */
Arnd Bergmann6b960182009-11-06 23:10:54 -08003336static int old_bridge_ioctl(compat_ulong_t __user *argp)
Arnd Bergmann7a229382009-11-06 23:00:29 -08003337{
Arnd Bergmann6b960182009-11-06 23:10:54 -08003338 compat_ulong_t tmp;
Arnd Bergmann7a229382009-11-06 23:00:29 -08003339
Arnd Bergmann6b960182009-11-06 23:10:54 -08003340 if (get_user(tmp, argp))
Arnd Bergmann7a229382009-11-06 23:00:29 -08003341 return -EFAULT;
3342 if (tmp == BRCTL_GET_VERSION)
3343 return BRCTL_VERSION + 1;
3344 return -EINVAL;
3345}
3346
Arnd Bergmann6b960182009-11-06 23:10:54 -08003347static int compat_sock_ioctl_trans(struct file *file, struct socket *sock,
3348 unsigned int cmd, unsigned long arg)
3349{
3350 void __user *argp = compat_ptr(arg);
3351 struct sock *sk = sock->sk;
3352 struct net *net = sock_net(sk);
Arnd Bergmann7a229382009-11-06 23:00:29 -08003353
Arnd Bergmann6b960182009-11-06 23:10:54 -08003354 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15))
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 switch (cmd) {
3358 case SIOCSIFBR:
3359 case SIOCGIFBR:
3360 return old_bridge_ioctl(argp);
Arnd Bergmann6b960182009-11-06 23:10:54 -08003361 case SIOCGIFCONF:
Al Viro36fd6332017-06-26 13:19:16 -04003362 return compat_dev_ifconf(net, argp);
Arnd Bergmann6b960182009-11-06 23:10:54 -08003363 case SIOCETHTOOL:
3364 return ethtool_ioctl(net, argp);
Arnd Bergmann7a50a242009-11-08 20:57:03 -08003365 case SIOCWANDEV:
3366 return compat_siocwandev(net, argp);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003367 case SIOCGIFMAP:
3368 case SIOCSIFMAP:
3369 return compat_sioc_ifmap(net, cmd, argp);
Arnd Bergmann6b960182009-11-06 23:10:54 -08003370 case SIOCADDRT:
3371 case SIOCDELRT:
3372 return routing_ioctl(net, sock, cmd, argp);
Arnd Bergmann0768e172019-04-17 22:56:11 +02003373 case SIOCGSTAMP_OLD:
3374 case SIOCGSTAMPNS_OLD:
Arnd Bergmannc7cbdbf2019-04-17 22:51:48 +02003375 if (!sock->ops->gettstamp)
3376 return -ENOIOCTLCMD;
Arnd Bergmann0768e172019-04-17 22:56:11 +02003377 return sock->ops->gettstamp(sock, argp, cmd == SIOCGSTAMP_OLD,
Arnd Bergmannc7cbdbf2019-04-17 22:51:48 +02003378 !COMPAT_USE_64BIT_TIME);
3379
Ben Hutchings590d4692013-11-18 17:04:13 +00003380 case SIOCBONDSLAVEINFOQUERY:
3381 case SIOCBONDINFOQUERY:
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003382 case SIOCSHWTSTAMP:
Ben Hutchingsfd468c72013-11-14 01:19:29 +00003383 case SIOCGHWTSTAMP:
Ben Hutchings590d4692013-11-18 17:04:13 +00003384 return compat_ifr_data_ioctl(net, cmd, argp);
Arnd Bergmann7a229382009-11-06 23:00:29 -08003385
Arnd Bergmann6b960182009-11-06 23:10:54 -08003386 case FIOSETOWN:
3387 case SIOCSPGRP:
3388 case FIOGETOWN:
3389 case SIOCGPGRP:
3390 case SIOCBRADDBR:
3391 case SIOCBRDELBR:
3392 case SIOCGIFVLAN:
3393 case SIOCSIFVLAN:
3394 case SIOCADDDLCI:
3395 case SIOCDELDLCI:
Andrey Vaginc62cce22016-10-24 18:29:13 -07003396 case SIOCGSKNS:
Arnd Bergmann0768e172019-04-17 22:56:11 +02003397 case SIOCGSTAMP_NEW:
3398 case SIOCGSTAMPNS_NEW:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003399 return sock_ioctl(file, cmd, arg);
3400
3401 case SIOCGIFFLAGS:
3402 case SIOCSIFFLAGS:
3403 case SIOCGIFMETRIC:
3404 case SIOCSIFMETRIC:
3405 case SIOCGIFMTU:
3406 case SIOCSIFMTU:
3407 case SIOCGIFMEM:
3408 case SIOCSIFMEM:
3409 case SIOCGIFHWADDR:
3410 case SIOCSIFHWADDR:
3411 case SIOCADDMULTI:
3412 case SIOCDELMULTI:
3413 case SIOCGIFINDEX:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003414 case SIOCGIFADDR:
3415 case SIOCSIFADDR:
3416 case SIOCSIFHWBROADCAST:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003417 case SIOCDIFADDR:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003418 case SIOCGIFBRDADDR:
3419 case SIOCSIFBRDADDR:
3420 case SIOCGIFDSTADDR:
3421 case SIOCSIFDSTADDR:
3422 case SIOCGIFNETMASK:
3423 case SIOCSIFNETMASK:
3424 case SIOCSIFPFLAGS:
3425 case SIOCGIFPFLAGS:
3426 case SIOCGIFTXQLEN:
3427 case SIOCSIFTXQLEN:
3428 case SIOCBRADDIF:
3429 case SIOCBRDELIF:
Johannes Bergc6c9fee2019-01-25 22:43:19 +01003430 case SIOCGIFNAME:
Arnd Bergmann9177efd2009-11-06 08:09:09 +00003431 case SIOCSIFNAME:
3432 case SIOCGMIIPHY:
3433 case SIOCGMIIREG:
3434 case SIOCSMIIREG:
Al Virof92d4fc2017-09-30 19:32:17 -04003435 case SIOCBONDENSLAVE:
3436 case SIOCBONDRELEASE:
3437 case SIOCBONDSETHWADDR:
3438 case SIOCBONDCHANGEACTIVE:
Johannes Berg37ac39b2019-01-25 22:43:18 +01003439 return compat_ifreq_ioctl(net, sock, cmd, argp);
3440
Arnd Bergmann6b960182009-11-06 23:10:54 -08003441 case SIOCSARP:
3442 case SIOCGARP:
3443 case SIOCDARP:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003444 case SIOCATMARK:
Johannes Berg63ff03a2019-01-25 22:43:17 +01003445 return sock_do_ioctl(net, sock, cmd, arg);
Arnd Bergmann9177efd2009-11-06 08:09:09 +00003446 }
3447
Arnd Bergmann6b960182009-11-06 23:10:54 -08003448 return -ENOIOCTLCMD;
3449}
Arnd Bergmann7a229382009-11-06 23:00:29 -08003450
Eric Dumazet95c96172012-04-15 05:58:06 +00003451static long compat_sock_ioctl(struct file *file, unsigned int cmd,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07003452 unsigned long arg)
Shaun Pereira89bbfc92006-03-21 23:58:08 -08003453{
3454 struct socket *sock = file->private_data;
3455 int ret = -ENOIOCTLCMD;
David S. Miller87de87d2008-06-03 09:14:03 -07003456 struct sock *sk;
3457 struct net *net;
3458
3459 sk = sock->sk;
3460 net = sock_net(sk);
Shaun Pereira89bbfc92006-03-21 23:58:08 -08003461
3462 if (sock->ops->compat_ioctl)
3463 ret = sock->ops->compat_ioctl(sock, cmd, arg);
3464
David S. Miller87de87d2008-06-03 09:14:03 -07003465 if (ret == -ENOIOCTLCMD &&
3466 (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST))
3467 ret = compat_wext_handle_ioctl(net, cmd, arg);
3468
Arnd Bergmann6b960182009-11-06 23:10:54 -08003469 if (ret == -ENOIOCTLCMD)
3470 ret = compat_sock_ioctl_trans(file, sock, cmd, arg);
3471
Shaun Pereira89bbfc92006-03-21 23:58:08 -08003472 return ret;
3473}
3474#endif
3475
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003476/**
3477 * kernel_bind - bind an address to a socket (kernel space)
3478 * @sock: socket
3479 * @addr: address
3480 * @addrlen: length of address
3481 *
3482 * Returns 0 or an error.
3483 */
3484
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003485int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen)
3486{
3487 return sock->ops->bind(sock, addr, addrlen);
3488}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003489EXPORT_SYMBOL(kernel_bind);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003490
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003491/**
3492 * kernel_listen - move socket to listening state (kernel space)
3493 * @sock: socket
3494 * @backlog: pending connections queue size
3495 *
3496 * Returns 0 or an error.
3497 */
3498
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003499int kernel_listen(struct socket *sock, int backlog)
3500{
3501 return sock->ops->listen(sock, backlog);
3502}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003503EXPORT_SYMBOL(kernel_listen);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003504
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003505/**
3506 * kernel_accept - accept a connection (kernel space)
3507 * @sock: listening socket
3508 * @newsock: new connected socket
3509 * @flags: flags
3510 *
3511 * @flags must be SOCK_CLOEXEC, SOCK_NONBLOCK or 0.
3512 * If it fails, @newsock is guaranteed to be %NULL.
3513 * Returns 0 or an error.
3514 */
3515
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003516int kernel_accept(struct socket *sock, struct socket **newsock, int flags)
3517{
3518 struct sock *sk = sock->sk;
3519 int err;
3520
3521 err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
3522 newsock);
3523 if (err < 0)
3524 goto done;
3525
David Howellscdfbabf2017-03-09 08:09:05 +00003526 err = sock->ops->accept(sock, *newsock, flags, true);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003527 if (err < 0) {
3528 sock_release(*newsock);
Tony Battersbyfa8705b2007-10-10 21:09:04 -07003529 *newsock = NULL;
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003530 goto done;
3531 }
3532
3533 (*newsock)->ops = sock->ops;
Wei Yongjun1b085342008-12-18 19:35:10 -08003534 __module_get((*newsock)->ops->owner);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003535
3536done:
3537 return err;
3538}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003539EXPORT_SYMBOL(kernel_accept);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003540
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003541/**
3542 * kernel_connect - connect a socket (kernel space)
3543 * @sock: socket
3544 * @addr: address
3545 * @addrlen: address length
3546 * @flags: flags (O_NONBLOCK, ...)
3547 *
3548 * For datagram sockets, @addr is the addres to which datagrams are sent
3549 * by default, and the only address from which datagrams are received.
3550 * For stream sockets, attempts to connect to @addr.
3551 * Returns 0 or an error code.
3552 */
3553
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003554int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
YOSHIFUJI Hideaki4768fbc2007-02-09 23:25:31 +09003555 int flags)
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003556{
3557 return sock->ops->connect(sock, addr, addrlen, flags);
3558}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003559EXPORT_SYMBOL(kernel_connect);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003560
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003561/**
3562 * kernel_getsockname - get the address which the socket is bound (kernel space)
3563 * @sock: socket
3564 * @addr: address holder
3565 *
3566 * Fills the @addr pointer with the address which the socket is bound.
3567 * Returns 0 or an error code.
3568 */
3569
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01003570int kernel_getsockname(struct socket *sock, struct sockaddr *addr)
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003571{
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01003572 return sock->ops->getname(sock, addr, 0);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003573}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003574EXPORT_SYMBOL(kernel_getsockname);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003575
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003576/**
3577 * kernel_peername - get the address which the socket is connected (kernel space)
3578 * @sock: socket
3579 * @addr: address holder
3580 *
3581 * Fills the @addr pointer with the address which the socket is connected.
3582 * Returns 0 or an error code.
3583 */
3584
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01003585int kernel_getpeername(struct socket *sock, struct sockaddr *addr)
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003586{
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01003587 return sock->ops->getname(sock, addr, 1);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003588}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003589EXPORT_SYMBOL(kernel_getpeername);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003590
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003591/**
3592 * kernel_getsockopt - get a socket option (kernel space)
3593 * @sock: socket
3594 * @level: API level (SOL_SOCKET, ...)
3595 * @optname: option tag
3596 * @optval: option value
3597 * @optlen: option length
3598 *
3599 * Assigns the option length to @optlen.
3600 * Returns 0 or an error.
3601 */
3602
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003603int kernel_getsockopt(struct socket *sock, int level, int optname,
3604 char *optval, int *optlen)
3605{
3606 mm_segment_t oldfs = get_fs();
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003607 char __user *uoptval;
3608 int __user *uoptlen;
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003609 int err;
3610
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003611 uoptval = (char __user __force *) optval;
3612 uoptlen = (int __user __force *) optlen;
3613
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003614 set_fs(KERNEL_DS);
3615 if (level == SOL_SOCKET)
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003616 err = sock_getsockopt(sock, level, optname, uoptval, uoptlen);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003617 else
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003618 err = sock->ops->getsockopt(sock, level, optname, uoptval,
3619 uoptlen);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003620 set_fs(oldfs);
3621 return err;
3622}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003623EXPORT_SYMBOL(kernel_getsockopt);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003624
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003625/**
3626 * kernel_setsockopt - set a socket option (kernel space)
3627 * @sock: socket
3628 * @level: API level (SOL_SOCKET, ...)
3629 * @optname: option tag
3630 * @optval: option value
3631 * @optlen: option length
3632 *
3633 * Returns 0 or an error.
3634 */
3635
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003636int kernel_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07003637 char *optval, unsigned int optlen)
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003638{
3639 mm_segment_t oldfs = get_fs();
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003640 char __user *uoptval;
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003641 int err;
3642
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003643 uoptval = (char __user __force *) optval;
3644
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003645 set_fs(KERNEL_DS);
3646 if (level == SOL_SOCKET)
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003647 err = sock_setsockopt(sock, level, optname, uoptval, optlen);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003648 else
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003649 err = sock->ops->setsockopt(sock, level, optname, uoptval,
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003650 optlen);
3651 set_fs(oldfs);
3652 return err;
3653}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003654EXPORT_SYMBOL(kernel_setsockopt);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003655
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003656/**
3657 * kernel_sendpage - send a &page through a socket (kernel space)
3658 * @sock: socket
3659 * @page: page
3660 * @offset: page offset
3661 * @size: total size in bytes
3662 * @flags: flags (MSG_DONTWAIT, ...)
3663 *
3664 * Returns the total amount sent in bytes or an error.
3665 */
3666
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003667int kernel_sendpage(struct socket *sock, struct page *page, int offset,
3668 size_t size, int flags)
3669{
3670 if (sock->ops->sendpage)
3671 return sock->ops->sendpage(sock, page, offset, size, flags);
3672
3673 return sock_no_sendpage(sock, page, offset, size, flags);
3674}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003675EXPORT_SYMBOL(kernel_sendpage);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003676
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003677/**
3678 * kernel_sendpage_locked - send a &page through the locked sock (kernel space)
3679 * @sk: sock
3680 * @page: page
3681 * @offset: page offset
3682 * @size: total size in bytes
3683 * @flags: flags (MSG_DONTWAIT, ...)
3684 *
3685 * Returns the total amount sent in bytes or an error.
3686 * Caller must hold @sk.
3687 */
3688
Tom Herbert306b13e2017-07-28 16:22:41 -07003689int kernel_sendpage_locked(struct sock *sk, struct page *page, int offset,
3690 size_t size, int flags)
3691{
3692 struct socket *sock = sk->sk_socket;
3693
3694 if (sock->ops->sendpage_locked)
3695 return sock->ops->sendpage_locked(sk, page, offset, size,
3696 flags);
3697
3698 return sock_no_sendpage_locked(sk, page, offset, size, flags);
3699}
3700EXPORT_SYMBOL(kernel_sendpage_locked);
3701
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003702/**
3703 * kernel_shutdown - shut down part of a full-duplex connection (kernel space)
3704 * @sock: socket
3705 * @how: connection part
3706 *
3707 * Returns 0 or an error.
3708 */
3709
Trond Myklebust91cf45f2007-11-12 18:10:39 -08003710int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how)
3711{
3712 return sock->ops->shutdown(sock, how);
3713}
Trond Myklebust91cf45f2007-11-12 18:10:39 -08003714EXPORT_SYMBOL(kernel_sock_shutdown);
R. Parameswaran113c3072017-04-05 16:50:35 -07003715
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003716/**
3717 * kernel_sock_ip_overhead - returns the IP overhead imposed by a socket
3718 * @sk: socket
3719 *
3720 * This routine returns the IP overhead imposed by a socket i.e.
3721 * the length of the underlying IP header, depending on whether
3722 * this is an IPv4 or IPv6 socket and the length from IP options turned
3723 * on at the socket. Assumes that the caller has a lock on the socket.
R. Parameswaran113c3072017-04-05 16:50:35 -07003724 */
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003725
R. Parameswaran113c3072017-04-05 16:50:35 -07003726u32 kernel_sock_ip_overhead(struct sock *sk)
3727{
3728 struct inet_sock *inet;
3729 struct ip_options_rcu *opt;
3730 u32 overhead = 0;
R. Parameswaran113c3072017-04-05 16:50:35 -07003731#if IS_ENABLED(CONFIG_IPV6)
3732 struct ipv6_pinfo *np;
3733 struct ipv6_txoptions *optv6 = NULL;
3734#endif /* IS_ENABLED(CONFIG_IPV6) */
3735
3736 if (!sk)
3737 return overhead;
3738
R. Parameswaran113c3072017-04-05 16:50:35 -07003739 switch (sk->sk_family) {
3740 case AF_INET:
3741 inet = inet_sk(sk);
3742 overhead += sizeof(struct iphdr);
3743 opt = rcu_dereference_protected(inet->inet_opt,
stephen hemminger614d79c2017-07-24 10:25:22 -07003744 sock_owned_by_user(sk));
R. Parameswaran113c3072017-04-05 16:50:35 -07003745 if (opt)
3746 overhead += opt->opt.optlen;
3747 return overhead;
3748#if IS_ENABLED(CONFIG_IPV6)
3749 case AF_INET6:
3750 np = inet6_sk(sk);
3751 overhead += sizeof(struct ipv6hdr);
3752 if (np)
3753 optv6 = rcu_dereference_protected(np->opt,
stephen hemminger614d79c2017-07-24 10:25:22 -07003754 sock_owned_by_user(sk));
R. Parameswaran113c3072017-04-05 16:50:35 -07003755 if (optv6)
3756 overhead += (optv6->opt_flen + optv6->opt_nflen);
3757 return overhead;
3758#endif /* IS_ENABLED(CONFIG_IPV6) */
3759 default: /* Returns 0 overhead if the socket is not ipv4 or ipv6 */
3760 return overhead;
3761 }
3762}
3763EXPORT_SYMBOL(kernel_sock_ip_overhead);