blob: a865708940f9664618b46fa4d5f7b11d2f754e12 [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 Dumazeteaefd112011-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 Dumazeteaefd112011-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 Dumazeteaefd112011-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
261static void sock_destroy_inode(struct inode *inode)
262{
Eric Dumazet43815482010-04-29 11:01:49 +0000263 struct socket_alloc *ei;
264
265 ei = container_of(inode, struct socket_alloc, vfs_inode);
Christoph Hellwige6476c22018-07-30 09:45:07 +0200266 kfree_rcu(ei->socket.wq, rcu);
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,
291 .destroy_inode = sock_destroy_inode,
292 .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 Lucinac1249c02010-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 Lucinac1249c02010-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 Lucinac1249c02010-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 Lucinac1249c02010-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 Dumazeteaefd112011-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 Dumazeteaefd112011-02-18 03:26:36 +00001298 fasync_helper(fd, filp, on, &wq->fasync_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Eric Dumazeteaefd112011-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{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002053 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 struct socket *sock;
2055
2056 if (optlen < 0)
2057 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002058
2059 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2060 if (sock != NULL) {
2061 err = security_socket_setsockopt(sock, level, optname);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002062 if (err)
2063 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
2065 if (level == SOL_SOCKET)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002066 err =
2067 sock_setsockopt(sock, level, optname, optval,
2068 optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 else
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002070 err =
2071 sock->ops->setsockopt(sock, level, optname, optval,
2072 optlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002073out_put:
2074 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 }
2076 return err;
2077}
2078
Dominik Brodowskicc36dca2018-03-13 20:10:59 +01002079SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname,
2080 char __user *, optval, int, optlen)
2081{
2082 return __sys_setsockopt(fd, level, optname, optval, optlen);
2083}
2084
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085/*
2086 * Get a socket option. Because we don't know the option lengths we have
2087 * to pass a user mode parameter for the protocols to sort out.
2088 */
2089
Dominik Brodowski13a2d702018-03-13 20:15:04 +01002090static int __sys_getsockopt(int fd, int level, int optname,
2091 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002093 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 struct socket *sock;
2095
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002096 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2097 if (sock != NULL) {
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002098 err = security_socket_getsockopt(sock, level, optname);
2099 if (err)
2100 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
2102 if (level == SOL_SOCKET)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002103 err =
2104 sock_getsockopt(sock, level, optname, optval,
2105 optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 else
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002107 err =
2108 sock->ops->getsockopt(sock, level, optname, optval,
2109 optlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002110out_put:
2111 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 }
2113 return err;
2114}
2115
Dominik Brodowski13a2d702018-03-13 20:15:04 +01002116SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname,
2117 char __user *, optval, int __user *, optlen)
2118{
2119 return __sys_getsockopt(fd, level, optname, optval, optlen);
2120}
2121
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122/*
2123 * Shutdown a socket.
2124 */
2125
Dominik Brodowski005a1ae2018-03-13 20:07:05 +01002126int __sys_shutdown(int fd, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002128 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 struct socket *sock;
2130
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002131 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2132 if (sock != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 err = security_socket_shutdown(sock, how);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002134 if (!err)
2135 err = sock->ops->shutdown(sock, how);
2136 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 }
2138 return err;
2139}
2140
Dominik Brodowski005a1ae2018-03-13 20:07:05 +01002141SYSCALL_DEFINE2(shutdown, int, fd, int, how)
2142{
2143 return __sys_shutdown(fd, how);
2144}
2145
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002146/* A couple of helpful macros for getting the address of the 32/64 bit
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 * fields which are the same type (int / unsigned) on our platforms.
2148 */
2149#define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
2150#define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
2151#define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
2152
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002153struct used_address {
2154 struct sockaddr_storage name;
2155 unsigned int name_len;
2156};
2157
Al Viroda184282015-03-21 19:29:06 -04002158static int copy_msghdr_from_user(struct msghdr *kmsg,
2159 struct user_msghdr __user *umsg,
2160 struct sockaddr __user **save_addr,
2161 struct iovec **iov)
Dan Carpenter1661bf32013-10-03 00:27:20 +03002162{
Al Viroffb07552017-06-27 19:32:04 -04002163 struct user_msghdr msg;
Al Viro08adb7d2014-11-10 20:23:13 -05002164 ssize_t err;
2165
Al Viroffb07552017-06-27 19:32:04 -04002166 if (copy_from_user(&msg, umsg, sizeof(*umsg)))
Dan Carpenter1661bf32013-10-03 00:27:20 +03002167 return -EFAULT;
Matthew Leachdbb490b2014-03-11 11:58:27 +00002168
Paolo Abeni864d9662017-07-21 18:49:45 +02002169 kmsg->msg_control = (void __force *)msg.msg_control;
Al Viroffb07552017-06-27 19:32:04 -04002170 kmsg->msg_controllen = msg.msg_controllen;
2171 kmsg->msg_flags = msg.msg_flags;
2172
2173 kmsg->msg_namelen = msg.msg_namelen;
2174 if (!msg.msg_name)
Ani Sinha6a2a2b32014-09-08 14:49:59 -07002175 kmsg->msg_namelen = 0;
2176
Matthew Leachdbb490b2014-03-11 11:58:27 +00002177 if (kmsg->msg_namelen < 0)
2178 return -EINVAL;
2179
Dan Carpenter1661bf32013-10-03 00:27:20 +03002180 if (kmsg->msg_namelen > sizeof(struct sockaddr_storage))
Dan Carpenterdb31c552013-11-27 15:40:21 +03002181 kmsg->msg_namelen = sizeof(struct sockaddr_storage);
Al Viro08adb7d2014-11-10 20:23:13 -05002182
2183 if (save_addr)
Al Viroffb07552017-06-27 19:32:04 -04002184 *save_addr = msg.msg_name;
Al Viro08adb7d2014-11-10 20:23:13 -05002185
Al Viroffb07552017-06-27 19:32:04 -04002186 if (msg.msg_name && kmsg->msg_namelen) {
Al Viro08adb7d2014-11-10 20:23:13 -05002187 if (!save_addr) {
Paolo Abeni864d9662017-07-21 18:49:45 +02002188 err = move_addr_to_kernel(msg.msg_name,
2189 kmsg->msg_namelen,
Al Viro08adb7d2014-11-10 20:23:13 -05002190 kmsg->msg_name);
2191 if (err < 0)
2192 return err;
2193 }
2194 } else {
2195 kmsg->msg_name = NULL;
2196 kmsg->msg_namelen = 0;
2197 }
2198
Al Viroffb07552017-06-27 19:32:04 -04002199 if (msg.msg_iovlen > UIO_MAXIOV)
Al Viro08adb7d2014-11-10 20:23:13 -05002200 return -EMSGSIZE;
2201
tadeusz.struk@intel.com0345f932015-03-19 12:31:25 -07002202 kmsg->msg_iocb = NULL;
2203
Al Viroffb07552017-06-27 19:32:04 -04002204 return import_iovec(save_addr ? READ : WRITE,
2205 msg.msg_iov, msg.msg_iovlen,
Al Viroda184282015-03-21 19:29:06 -04002206 UIO_FASTIOV, iov, &kmsg->msg_iter);
Dan Carpenter1661bf32013-10-03 00:27:20 +03002207}
2208
Al Viro666547f2014-04-06 14:03:05 -04002209static int ___sys_sendmsg(struct socket *sock, struct user_msghdr __user *msg,
Eric Dumazet95c96172012-04-15 05:58:06 +00002210 struct msghdr *msg_sys, unsigned int flags,
Tom Herbert28a94d82016-03-07 14:11:02 -08002211 struct used_address *used_address,
2212 unsigned int allowed_msghdr_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213{
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002214 struct compat_msghdr __user *msg_compat =
2215 (struct compat_msghdr __user *)msg;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07002216 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
Alex Williamsonb9d717a2005-09-26 14:28:02 -07002218 unsigned char ctl[sizeof(struct cmsghdr) + 20]
Amit Kushwaha846cc122016-12-08 18:21:53 +05302219 __aligned(sizeof(__kernel_size_t));
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002220 /* 20 is size of ipv6_pktinfo */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 unsigned char *ctl_buf = ctl;
Al Virod8725c82014-12-11 00:02:50 -05002222 int ctl_len;
Al Viro08adb7d2014-11-10 20:23:13 -05002223 ssize_t err;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002224
Al Viro08adb7d2014-11-10 20:23:13 -05002225 msg_sys->msg_name = &address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
Al Viro08449322014-11-09 22:33:45 -05002227 if (MSG_CMSG_COMPAT & flags)
Al Viro08adb7d2014-11-10 20:23:13 -05002228 err = get_compat_msghdr(msg_sys, msg_compat, NULL, &iov);
Al Viro08449322014-11-09 22:33:45 -05002229 else
Al Viro08adb7d2014-11-10 20:23:13 -05002230 err = copy_msghdr_from_user(msg_sys, msg, NULL, &iov);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002231 if (err < 0)
Al Viroda184282015-03-21 19:29:06 -04002232 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233
2234 err = -ENOBUFS;
2235
Anton Blanchard228e5482011-05-02 20:21:35 +00002236 if (msg_sys->msg_controllen > INT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 goto out_freeiov;
Tom Herbert28a94d82016-03-07 14:11:02 -08002238 flags |= (msg_sys->msg_flags & allowed_msghdr_flags);
Anton Blanchard228e5482011-05-02 20:21:35 +00002239 ctl_len = msg_sys->msg_controllen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002241 err =
Anton Blanchard228e5482011-05-02 20:21:35 +00002242 cmsghdr_from_user_compat_to_kern(msg_sys, sock->sk, ctl,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002243 sizeof(ctl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 if (err)
2245 goto out_freeiov;
Anton Blanchard228e5482011-05-02 20:21:35 +00002246 ctl_buf = msg_sys->msg_control;
2247 ctl_len = msg_sys->msg_controllen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 } else if (ctl_len) {
David S. Millerac4340f2017-01-04 13:24:19 -05002249 BUILD_BUG_ON(sizeof(struct cmsghdr) !=
2250 CMSG_ALIGN(sizeof(struct cmsghdr)));
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002251 if (ctl_len > sizeof(ctl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002253 if (ctl_buf == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 goto out_freeiov;
2255 }
2256 err = -EFAULT;
2257 /*
Anton Blanchard228e5482011-05-02 20:21:35 +00002258 * Careful! Before this, msg_sys->msg_control contains a user pointer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
2260 * checking falls down on this.
2261 */
Namhyung Kimfb8621b2010-09-07 03:55:00 +00002262 if (copy_from_user(ctl_buf,
Anton Blanchard228e5482011-05-02 20:21:35 +00002263 (void __user __force *)msg_sys->msg_control,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002264 ctl_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 goto out_freectl;
Anton Blanchard228e5482011-05-02 20:21:35 +00002266 msg_sys->msg_control = ctl_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 }
Anton Blanchard228e5482011-05-02 20:21:35 +00002268 msg_sys->msg_flags = flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269
2270 if (sock->file->f_flags & O_NONBLOCK)
Anton Blanchard228e5482011-05-02 20:21:35 +00002271 msg_sys->msg_flags |= MSG_DONTWAIT;
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002272 /*
2273 * If this is sendmmsg() and current destination address is same as
2274 * previously succeeded address, omit asking LSM's decision.
2275 * used_address->name_len is initialized to UINT_MAX so that the first
2276 * destination address never matches.
2277 */
Mathieu Desnoyersbc909d92011-08-24 19:45:03 -07002278 if (used_address && msg_sys->msg_name &&
2279 used_address->name_len == msg_sys->msg_namelen &&
2280 !memcmp(&used_address->name, msg_sys->msg_name,
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002281 used_address->name_len)) {
Al Virod8725c82014-12-11 00:02:50 -05002282 err = sock_sendmsg_nosec(sock, msg_sys);
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002283 goto out_freectl;
2284 }
Al Virod8725c82014-12-11 00:02:50 -05002285 err = sock_sendmsg(sock, msg_sys);
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002286 /*
2287 * If this is sendmmsg() and sending to current destination address was
2288 * successful, remember it.
2289 */
2290 if (used_address && err >= 0) {
2291 used_address->name_len = msg_sys->msg_namelen;
Mathieu Desnoyersbc909d92011-08-24 19:45:03 -07002292 if (msg_sys->msg_name)
2293 memcpy(&used_address->name, msg_sys->msg_name,
2294 used_address->name_len);
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296
2297out_freectl:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002298 if (ctl_buf != ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 sock_kfree_s(sock->sk, ctl_buf, ctl_len);
2300out_freeiov:
Al Viroda184282015-03-21 19:29:06 -04002301 kfree(iov);
Anton Blanchard228e5482011-05-02 20:21:35 +00002302 return err;
2303}
2304
2305/*
2306 * BSD sendmsg interface
2307 */
2308
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002309long __sys_sendmsg(int fd, struct user_msghdr __user *msg, unsigned int flags,
2310 bool forbid_cmsg_compat)
Anton Blanchard228e5482011-05-02 20:21:35 +00002311{
2312 int fput_needed, err;
2313 struct msghdr msg_sys;
Andy Lutomirski1be374a2013-05-22 14:07:44 -07002314 struct socket *sock;
Anton Blanchard228e5482011-05-02 20:21:35 +00002315
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002316 if (forbid_cmsg_compat && (flags & MSG_CMSG_COMPAT))
2317 return -EINVAL;
2318
Andy Lutomirski1be374a2013-05-22 14:07:44 -07002319 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Anton Blanchard228e5482011-05-02 20:21:35 +00002320 if (!sock)
2321 goto out;
2322
Tom Herbert28a94d82016-03-07 14:11:02 -08002323 err = ___sys_sendmsg(sock, msg, &msg_sys, flags, NULL, 0);
Anton Blanchard228e5482011-05-02 20:21:35 +00002324
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002325 fput_light(sock->file, fput_needed);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002326out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 return err;
2328}
2329
Al Viro666547f2014-04-06 14:03:05 -04002330SYSCALL_DEFINE3(sendmsg, int, fd, struct user_msghdr __user *, msg, unsigned int, flags)
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002331{
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002332 return __sys_sendmsg(fd, msg, flags, true);
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002333}
2334
Anton Blanchard228e5482011-05-02 20:21:35 +00002335/*
2336 * Linux sendmmsg interface
2337 */
2338
2339int __sys_sendmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002340 unsigned int flags, bool forbid_cmsg_compat)
Anton Blanchard228e5482011-05-02 20:21:35 +00002341{
2342 int fput_needed, err, datagrams;
2343 struct socket *sock;
2344 struct mmsghdr __user *entry;
2345 struct compat_mmsghdr __user *compat_entry;
2346 struct msghdr msg_sys;
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002347 struct used_address used_address;
Tom Herbertf0922762016-03-07 14:11:03 -08002348 unsigned int oflags = flags;
Anton Blanchard228e5482011-05-02 20:21:35 +00002349
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002350 if (forbid_cmsg_compat && (flags & MSG_CMSG_COMPAT))
2351 return -EINVAL;
2352
Anton Blanchard98382f42011-08-04 14:07:39 +00002353 if (vlen > UIO_MAXIOV)
2354 vlen = UIO_MAXIOV;
Anton Blanchard228e5482011-05-02 20:21:35 +00002355
2356 datagrams = 0;
2357
2358 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2359 if (!sock)
2360 return err;
2361
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002362 used_address.name_len = UINT_MAX;
Anton Blanchard228e5482011-05-02 20:21:35 +00002363 entry = mmsg;
2364 compat_entry = (struct compat_mmsghdr __user *)mmsg;
Anton Blanchard728ffb82011-08-04 14:07:38 +00002365 err = 0;
Tom Herbertf0922762016-03-07 14:11:03 -08002366 flags |= MSG_BATCH;
Anton Blanchard228e5482011-05-02 20:21:35 +00002367
2368 while (datagrams < vlen) {
Tom Herbertf0922762016-03-07 14:11:03 -08002369 if (datagrams == vlen - 1)
2370 flags = oflags;
2371
Anton Blanchard228e5482011-05-02 20:21:35 +00002372 if (MSG_CMSG_COMPAT & flags) {
Al Viro666547f2014-04-06 14:03:05 -04002373 err = ___sys_sendmsg(sock, (struct user_msghdr __user *)compat_entry,
Tom Herbert28a94d82016-03-07 14:11:02 -08002374 &msg_sys, flags, &used_address, MSG_EOR);
Anton Blanchard228e5482011-05-02 20:21:35 +00002375 if (err < 0)
2376 break;
2377 err = __put_user(err, &compat_entry->msg_len);
2378 ++compat_entry;
2379 } else {
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002380 err = ___sys_sendmsg(sock,
Al Viro666547f2014-04-06 14:03:05 -04002381 (struct user_msghdr __user *)entry,
Tom Herbert28a94d82016-03-07 14:11:02 -08002382 &msg_sys, flags, &used_address, MSG_EOR);
Anton Blanchard228e5482011-05-02 20:21:35 +00002383 if (err < 0)
2384 break;
2385 err = put_user(err, &entry->msg_len);
2386 ++entry;
2387 }
2388
2389 if (err)
2390 break;
2391 ++datagrams;
Soheil Hassas Yeganeh30238982016-11-04 15:36:49 -04002392 if (msg_data_left(&msg_sys))
2393 break;
Eric Dumazeta78cb842016-01-08 08:37:20 -08002394 cond_resched();
Anton Blanchard228e5482011-05-02 20:21:35 +00002395 }
2396
Anton Blanchard228e5482011-05-02 20:21:35 +00002397 fput_light(sock->file, fput_needed);
2398
Anton Blanchard728ffb82011-08-04 14:07:38 +00002399 /* We only return an error if no datagrams were able to be sent */
2400 if (datagrams != 0)
Anton Blanchard228e5482011-05-02 20:21:35 +00002401 return datagrams;
2402
Anton Blanchard228e5482011-05-02 20:21:35 +00002403 return err;
2404}
2405
2406SYSCALL_DEFINE4(sendmmsg, int, fd, struct mmsghdr __user *, mmsg,
2407 unsigned int, vlen, unsigned int, flags)
2408{
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002409 return __sys_sendmmsg(fd, mmsg, vlen, flags, true);
Anton Blanchard228e5482011-05-02 20:21:35 +00002410}
2411
Al Viro666547f2014-04-06 14:03:05 -04002412static int ___sys_recvmsg(struct socket *sock, struct user_msghdr __user *msg,
Eric Dumazet95c96172012-04-15 05:58:06 +00002413 struct msghdr *msg_sys, unsigned int flags, int nosec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414{
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002415 struct compat_msghdr __user *msg_compat =
2416 (struct compat_msghdr __user *)msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 struct iovec iovstack[UIO_FASTIOV];
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002418 struct iovec *iov = iovstack;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 unsigned long cmsg_ptr;
Al Viro2da62902015-03-14 21:13:46 -04002420 int len;
Al Viro08adb7d2014-11-10 20:23:13 -05002421 ssize_t err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422
2423 /* kernel mode address */
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07002424 struct sockaddr_storage addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425
2426 /* user mode address pointers */
2427 struct sockaddr __user *uaddr;
Al Viro08adb7d2014-11-10 20:23:13 -05002428 int __user *uaddr_len = COMPAT_NAMELEN(msg);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002429
Al Viro08adb7d2014-11-10 20:23:13 -05002430 msg_sys->msg_name = &addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01002432 if (MSG_CMSG_COMPAT & flags)
Al Viro08adb7d2014-11-10 20:23:13 -05002433 err = get_compat_msghdr(msg_sys, msg_compat, &uaddr, &iov);
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01002434 else
Al Viro08adb7d2014-11-10 20:23:13 -05002435 err = copy_msghdr_from_user(msg_sys, msg, &uaddr, &iov);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 if (err < 0)
Al Viroda184282015-03-21 19:29:06 -04002437 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002439 cmsg_ptr = (unsigned long)msg_sys->msg_control;
2440 msg_sys->msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002441
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01002442 /* We assume all kernel code knows the size of sockaddr_storage */
2443 msg_sys->msg_namelen = 0;
2444
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 if (sock->file->f_flags & O_NONBLOCK)
2446 flags |= MSG_DONTWAIT;
Al Viro2da62902015-03-14 21:13:46 -04002447 err = (nosec ? sock_recvmsg_nosec : sock_recvmsg)(sock, msg_sys, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 if (err < 0)
2449 goto out_freeiov;
2450 len = err;
2451
2452 if (uaddr != NULL) {
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00002453 err = move_addr_to_user(&addr,
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002454 msg_sys->msg_namelen, uaddr,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002455 uaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 if (err < 0)
2457 goto out_freeiov;
2458 }
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002459 err = __put_user((msg_sys->msg_flags & ~MSG_CMSG_COMPAT),
David S. Miller37f7f4212005-09-16 16:51:01 -07002460 COMPAT_FLAGS(msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 if (err)
2462 goto out_freeiov;
2463 if (MSG_CMSG_COMPAT & flags)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002464 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 &msg_compat->msg_controllen);
2466 else
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002467 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 &msg->msg_controllen);
2469 if (err)
2470 goto out_freeiov;
2471 err = len;
2472
2473out_freeiov:
Al Viroda184282015-03-21 19:29:06 -04002474 kfree(iov);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002475 return err;
2476}
2477
2478/*
2479 * BSD recvmsg interface
2480 */
2481
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002482long __sys_recvmsg(int fd, struct user_msghdr __user *msg, unsigned int flags,
2483 bool forbid_cmsg_compat)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002484{
2485 int fput_needed, err;
2486 struct msghdr msg_sys;
Andy Lutomirski1be374a2013-05-22 14:07:44 -07002487 struct socket *sock;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002488
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002489 if (forbid_cmsg_compat && (flags & MSG_CMSG_COMPAT))
2490 return -EINVAL;
2491
Andy Lutomirski1be374a2013-05-22 14:07:44 -07002492 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002493 if (!sock)
2494 goto out;
2495
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002496 err = ___sys_recvmsg(sock, msg, &msg_sys, flags, 0);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002497
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002498 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499out:
2500 return err;
2501}
2502
Al Viro666547f2014-04-06 14:03:05 -04002503SYSCALL_DEFINE3(recvmsg, int, fd, struct user_msghdr __user *, msg,
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002504 unsigned int, flags)
2505{
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002506 return __sys_recvmsg(fd, msg, flags, true);
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002507}
2508
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002509/*
2510 * Linux recvmmsg interface
2511 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512
Arnd Bergmanne11d4282018-04-18 13:43:52 +02002513static int do_recvmmsg(int fd, struct mmsghdr __user *mmsg,
2514 unsigned int vlen, unsigned int flags,
2515 struct timespec64 *timeout)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002516{
2517 int fput_needed, err, datagrams;
2518 struct socket *sock;
2519 struct mmsghdr __user *entry;
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002520 struct compat_mmsghdr __user *compat_entry;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002521 struct msghdr msg_sys;
Deepa Dinamani766b9f92016-05-19 17:09:05 -07002522 struct timespec64 end_time;
2523 struct timespec64 timeout64;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002524
2525 if (timeout &&
2526 poll_select_set_timeout(&end_time, timeout->tv_sec,
2527 timeout->tv_nsec))
2528 return -EINVAL;
2529
2530 datagrams = 0;
2531
2532 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2533 if (!sock)
2534 return err;
2535
Soheil Hassas Yeganeh7797dc42018-02-27 18:22:40 -05002536 if (likely(!(flags & MSG_ERRQUEUE))) {
2537 err = sock_error(sock->sk);
2538 if (err) {
2539 datagrams = err;
2540 goto out_put;
2541 }
Maxime Jayate623a9e2017-02-21 18:35:51 +01002542 }
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002543
2544 entry = mmsg;
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002545 compat_entry = (struct compat_mmsghdr __user *)mmsg;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002546
2547 while (datagrams < vlen) {
2548 /*
2549 * No need to ask LSM for more than the first datagram.
2550 */
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002551 if (MSG_CMSG_COMPAT & flags) {
Al Viro666547f2014-04-06 14:03:05 -04002552 err = ___sys_recvmsg(sock, (struct user_msghdr __user *)compat_entry,
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002553 &msg_sys, flags & ~MSG_WAITFORONE,
2554 datagrams);
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002555 if (err < 0)
2556 break;
2557 err = __put_user(err, &compat_entry->msg_len);
2558 ++compat_entry;
2559 } else {
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002560 err = ___sys_recvmsg(sock,
Al Viro666547f2014-04-06 14:03:05 -04002561 (struct user_msghdr __user *)entry,
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002562 &msg_sys, flags & ~MSG_WAITFORONE,
2563 datagrams);
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002564 if (err < 0)
2565 break;
2566 err = put_user(err, &entry->msg_len);
2567 ++entry;
2568 }
2569
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002570 if (err)
2571 break;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002572 ++datagrams;
2573
Brandon L Black71c5c1592010-03-26 16:18:03 +00002574 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
2575 if (flags & MSG_WAITFORONE)
2576 flags |= MSG_DONTWAIT;
2577
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002578 if (timeout) {
Deepa Dinamani766b9f92016-05-19 17:09:05 -07002579 ktime_get_ts64(&timeout64);
Arnd Bergmannc2e6c852018-04-18 13:42:25 +02002580 *timeout = timespec64_sub(end_time, timeout64);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002581 if (timeout->tv_sec < 0) {
2582 timeout->tv_sec = timeout->tv_nsec = 0;
2583 break;
2584 }
2585
2586 /* Timeout, return less than vlen datagrams */
2587 if (timeout->tv_nsec == 0 && timeout->tv_sec == 0)
2588 break;
2589 }
2590
2591 /* Out of band data, return right away */
2592 if (msg_sys.msg_flags & MSG_OOB)
2593 break;
Eric Dumazeta78cb842016-01-08 08:37:20 -08002594 cond_resched();
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002595 }
2596
Arnaldo Carvalho de Melo34b88a62016-03-14 09:56:35 -03002597 if (err == 0)
2598 goto out_put;
2599
2600 if (datagrams == 0) {
2601 datagrams = err;
2602 goto out_put;
2603 }
2604
2605 /*
2606 * We may return less entries than requested (vlen) if the
2607 * sock is non block and there aren't enough datagrams...
2608 */
2609 if (err != -EAGAIN) {
2610 /*
2611 * ... or if recvmsg returns an error after we
2612 * received some datagrams, where we record the
2613 * error to return on the next call or if the
2614 * app asks about it using getsockopt(SO_ERROR).
2615 */
2616 sock->sk->sk_err = -err;
2617 }
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002618out_put:
2619 fput_light(sock->file, fput_needed);
2620
Arnaldo Carvalho de Melo34b88a62016-03-14 09:56:35 -03002621 return datagrams;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002622}
2623
Arnd Bergmanne11d4282018-04-18 13:43:52 +02002624int __sys_recvmmsg(int fd, struct mmsghdr __user *mmsg,
2625 unsigned int vlen, unsigned int flags,
2626 struct __kernel_timespec __user *timeout,
2627 struct old_timespec32 __user *timeout32)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002628{
2629 int datagrams;
Arnd Bergmannc2e6c852018-04-18 13:42:25 +02002630 struct timespec64 timeout_sys;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002631
Arnd Bergmanne11d4282018-04-18 13:43:52 +02002632 if (timeout && get_timespec64(&timeout_sys, timeout))
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002633 return -EFAULT;
2634
Arnd Bergmanne11d4282018-04-18 13:43:52 +02002635 if (timeout32 && get_old_timespec32(&timeout_sys, timeout32))
2636 return -EFAULT;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002637
Arnd Bergmanne11d4282018-04-18 13:43:52 +02002638 if (!timeout && !timeout32)
2639 return do_recvmmsg(fd, mmsg, vlen, flags, NULL);
2640
2641 datagrams = do_recvmmsg(fd, mmsg, vlen, flags, &timeout_sys);
2642
2643 if (datagrams <= 0)
2644 return datagrams;
2645
2646 if (timeout && put_timespec64(&timeout_sys, timeout))
2647 datagrams = -EFAULT;
2648
2649 if (timeout32 && put_old_timespec32(&timeout_sys, timeout32))
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002650 datagrams = -EFAULT;
2651
2652 return datagrams;
2653}
2654
Dominik Brodowski1255e262018-03-13 20:44:21 +01002655SYSCALL_DEFINE5(recvmmsg, int, fd, struct mmsghdr __user *, mmsg,
2656 unsigned int, vlen, unsigned int, flags,
Arnd Bergmannc2e6c852018-04-18 13:42:25 +02002657 struct __kernel_timespec __user *, timeout)
Dominik Brodowski1255e262018-03-13 20:44:21 +01002658{
Arnd Bergmanne11d4282018-04-18 13:43:52 +02002659 if (flags & MSG_CMSG_COMPAT)
2660 return -EINVAL;
2661
2662 return __sys_recvmmsg(fd, mmsg, vlen, flags, timeout, NULL);
Dominik Brodowski1255e262018-03-13 20:44:21 +01002663}
2664
Arnd Bergmanne11d4282018-04-18 13:43:52 +02002665#ifdef CONFIG_COMPAT_32BIT_TIME
2666SYSCALL_DEFINE5(recvmmsg_time32, int, fd, struct mmsghdr __user *, mmsg,
2667 unsigned int, vlen, unsigned int, flags,
2668 struct old_timespec32 __user *, timeout)
2669{
2670 if (flags & MSG_CMSG_COMPAT)
2671 return -EINVAL;
2672
2673 return __sys_recvmmsg(fd, mmsg, vlen, flags, NULL, timeout);
2674}
2675#endif
2676
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002677#ifdef __ARCH_WANT_SYS_SOCKETCALL
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678/* Argument list sizes for sys_socketcall */
2679#define AL(x) ((x) * sizeof(unsigned long))
Anton Blanchard228e5482011-05-02 20:21:35 +00002680static const unsigned char nargs[21] = {
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002681 AL(0), AL(3), AL(3), AL(3), AL(2), AL(3),
2682 AL(3), AL(3), AL(4), AL(4), AL(4), AL(6),
2683 AL(6), AL(2), AL(5), AL(5), AL(3), AL(3),
Anton Blanchard228e5482011-05-02 20:21:35 +00002684 AL(4), AL(5), AL(4)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002685};
2686
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687#undef AL
2688
2689/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002690 * System call vectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 *
2692 * Argument checking cleaned up. Saved 20% in size.
2693 * This function doesn't need to set the kernel lock because
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002694 * it is set by the callees.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 */
2696
Heiko Carstens3e0fa652009-01-14 14:14:24 +01002697SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698{
Chen Gang2950fa92013-04-07 16:55:23 +08002699 unsigned long a[AUDITSC_ARGS];
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002700 unsigned long a0, a1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 int err;
Arjan van de Ven47379052009-09-28 12:57:44 -07002702 unsigned int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703
Anton Blanchard228e5482011-05-02 20:21:35 +00002704 if (call < 1 || call > SYS_SENDMMSG)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 return -EINVAL;
Jeremy Clinec8e8cd52018-07-27 22:43:01 +00002706 call = array_index_nospec(call, SYS_SENDMMSG + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707
Arjan van de Ven47379052009-09-28 12:57:44 -07002708 len = nargs[call];
2709 if (len > sizeof(a))
2710 return -EINVAL;
2711
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 /* copy_from_user should be SMP safe. */
Arjan van de Ven47379052009-09-28 12:57:44 -07002713 if (copy_from_user(a, args, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 return -EFAULT;
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002715
Chen Gang2950fa92013-04-07 16:55:23 +08002716 err = audit_socketcall(nargs[call] / sizeof(unsigned long), a);
2717 if (err)
2718 return err;
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002719
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002720 a0 = a[0];
2721 a1 = a[1];
2722
2723 switch (call) {
2724 case SYS_SOCKET:
Dominik Brodowski9d6a15c2018-03-13 19:29:43 +01002725 err = __sys_socket(a0, a1, a[2]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002726 break;
2727 case SYS_BIND:
Dominik Brodowskia87d35d2018-03-13 19:33:09 +01002728 err = __sys_bind(a0, (struct sockaddr __user *)a1, a[2]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002729 break;
2730 case SYS_CONNECT:
Dominik Brodowski1387c2c2018-03-13 19:35:09 +01002731 err = __sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002732 break;
2733 case SYS_LISTEN:
Dominik Brodowski25e290e2018-03-13 19:36:54 +01002734 err = __sys_listen(a0, a1);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002735 break;
2736 case SYS_ACCEPT:
Dominik Brodowski4541e802018-03-13 19:24:23 +01002737 err = __sys_accept4(a0, (struct sockaddr __user *)a1,
2738 (int __user *)a[2], 0);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002739 break;
2740 case SYS_GETSOCKNAME:
2741 err =
Dominik Brodowski8882a102018-03-13 19:43:14 +01002742 __sys_getsockname(a0, (struct sockaddr __user *)a1,
2743 (int __user *)a[2]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002744 break;
2745 case SYS_GETPEERNAME:
2746 err =
Dominik Brodowskib21c8f82018-03-13 19:47:00 +01002747 __sys_getpeername(a0, (struct sockaddr __user *)a1,
2748 (int __user *)a[2]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002749 break;
2750 case SYS_SOCKETPAIR:
Dominik Brodowski6debc8d2018-03-13 19:49:23 +01002751 err = __sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002752 break;
2753 case SYS_SEND:
Dominik Brodowskif3bf8962018-03-13 19:52:00 +01002754 err = __sys_sendto(a0, (void __user *)a1, a[2], a[3],
2755 NULL, 0);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002756 break;
2757 case SYS_SENDTO:
Dominik Brodowski211b6342018-03-13 19:18:52 +01002758 err = __sys_sendto(a0, (void __user *)a1, a[2], a[3],
2759 (struct sockaddr __user *)a[4], a[5]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002760 break;
2761 case SYS_RECV:
Dominik Brodowskid27e9af2018-03-13 19:54:17 +01002762 err = __sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
2763 NULL, NULL);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002764 break;
2765 case SYS_RECVFROM:
Dominik Brodowski7a09e1e2018-03-13 19:10:06 +01002766 err = __sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
2767 (struct sockaddr __user *)a[4],
2768 (int __user *)a[5]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002769 break;
2770 case SYS_SHUTDOWN:
Dominik Brodowski005a1ae2018-03-13 20:07:05 +01002771 err = __sys_shutdown(a0, a1);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002772 break;
2773 case SYS_SETSOCKOPT:
Dominik Brodowskicc36dca2018-03-13 20:10:59 +01002774 err = __sys_setsockopt(a0, a1, a[2], (char __user *)a[3],
2775 a[4]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002776 break;
2777 case SYS_GETSOCKOPT:
2778 err =
Dominik Brodowski13a2d702018-03-13 20:15:04 +01002779 __sys_getsockopt(a0, a1, a[2], (char __user *)a[3],
2780 (int __user *)a[4]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002781 break;
2782 case SYS_SENDMSG:
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002783 err = __sys_sendmsg(a0, (struct user_msghdr __user *)a1,
2784 a[2], true);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002785 break;
Anton Blanchard228e5482011-05-02 20:21:35 +00002786 case SYS_SENDMMSG:
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002787 err = __sys_sendmmsg(a0, (struct mmsghdr __user *)a1, a[2],
2788 a[3], true);
Anton Blanchard228e5482011-05-02 20:21:35 +00002789 break;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002790 case SYS_RECVMSG:
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002791 err = __sys_recvmsg(a0, (struct user_msghdr __user *)a1,
2792 a[2], true);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002793 break;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002794 case SYS_RECVMMSG:
Arnd Bergmanne11d4282018-04-18 13:43:52 +02002795 if (IS_ENABLED(CONFIG_64BIT) || !IS_ENABLED(CONFIG_64BIT_TIME))
2796 err = __sys_recvmmsg(a0, (struct mmsghdr __user *)a1,
2797 a[2], a[3],
2798 (struct __kernel_timespec __user *)a[4],
2799 NULL);
2800 else
2801 err = __sys_recvmmsg(a0, (struct mmsghdr __user *)a1,
2802 a[2], a[3], NULL,
2803 (struct old_timespec32 __user *)a[4]);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002804 break;
Ulrich Drepperde11def2008-11-19 15:36:14 -08002805 case SYS_ACCEPT4:
Dominik Brodowski4541e802018-03-13 19:24:23 +01002806 err = __sys_accept4(a0, (struct sockaddr __user *)a1,
2807 (int __user *)a[2], a[3]);
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07002808 break;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002809 default:
2810 err = -EINVAL;
2811 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 }
2813 return err;
2814}
2815
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002816#endif /* __ARCH_WANT_SYS_SOCKETCALL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002818/**
2819 * sock_register - add a socket protocol handler
2820 * @ops: description of protocol
2821 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 * This function is called by a protocol handler that wants to
2823 * advertise its address family, and have it linked into the
Masanari Iidae793c0f2014-09-04 23:44:36 +09002824 * socket interface. The value ops->family corresponds to the
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002825 * socket system call protocol family.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 */
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002827int sock_register(const struct net_proto_family *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828{
2829 int err;
2830
2831 if (ops->family >= NPROTO) {
Yang Yingliang3410f222014-02-12 17:09:55 +08002832 pr_crit("protocol %d >= NPROTO(%d)\n", ops->family, NPROTO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 return -ENOBUFS;
2834 }
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002835
2836 spin_lock(&net_family_lock);
Eric Dumazet190683a2010-11-10 10:50:44 +00002837 if (rcu_dereference_protected(net_families[ops->family],
2838 lockdep_is_held(&net_family_lock)))
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002839 err = -EEXIST;
2840 else {
Eric Dumazetcf778b02012-01-12 04:41:32 +00002841 rcu_assign_pointer(net_families[ops->family], ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 err = 0;
2843 }
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002844 spin_unlock(&net_family_lock);
2845
Yang Yingliang3410f222014-02-12 17:09:55 +08002846 pr_info("NET: Registered protocol family %d\n", ops->family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 return err;
2848}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002849EXPORT_SYMBOL(sock_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002851/**
2852 * sock_unregister - remove a protocol handler
2853 * @family: protocol family to remove
2854 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 * This function is called by a protocol handler that wants to
2856 * remove its address family, and have it unlinked from the
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002857 * new socket creation.
2858 *
2859 * If protocol handler is a module, then it can use module reference
2860 * counts to protect against new references. If protocol handler is not
2861 * a module then it needs to provide its own protection in
2862 * the ops->create routine.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 */
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002864void sock_unregister(int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865{
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002866 BUG_ON(family < 0 || family >= NPROTO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002868 spin_lock(&net_family_lock);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00002869 RCU_INIT_POINTER(net_families[family], NULL);
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002870 spin_unlock(&net_family_lock);
2871
2872 synchronize_rcu();
2873
Yang Yingliang3410f222014-02-12 17:09:55 +08002874 pr_info("NET: Unregistered protocol family %d\n", family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002876EXPORT_SYMBOL(sock_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877
Xin Longbf2ae2e2018-03-10 18:57:50 +08002878bool sock_is_registered(int family)
2879{
Jeremy Cline66b51b02018-08-13 22:23:13 +00002880 return family < NPROTO && rcu_access_pointer(net_families[family]);
Xin Longbf2ae2e2018-03-10 18:57:50 +08002881}
2882
Andi Kleen77d76ea2005-12-22 12:43:42 -08002883static int __init sock_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884{
Nick Pigginb3e19d92011-01-07 17:50:11 +11002885 int err;
Eric W. Biederman2ca794e2012-04-19 13:20:32 +00002886 /*
2887 * Initialize the network sysctl infrastructure.
2888 */
2889 err = net_sysctl_init();
2890 if (err)
2891 goto out;
Nick Pigginb3e19d92011-01-07 17:50:11 +11002892
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002894 * Initialize skbuff SLAB cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 */
2896 skb_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897
2898 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002899 * Initialize the protocols module.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 */
2901
2902 init_inodecache();
Nick Pigginb3e19d92011-01-07 17:50:11 +11002903
2904 err = register_filesystem(&sock_fs_type);
2905 if (err)
2906 goto out_fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 sock_mnt = kern_mount(&sock_fs_type);
Nick Pigginb3e19d92011-01-07 17:50:11 +11002908 if (IS_ERR(sock_mnt)) {
2909 err = PTR_ERR(sock_mnt);
2910 goto out_mount;
2911 }
Andi Kleen77d76ea2005-12-22 12:43:42 -08002912
2913 /* The real protocol initialization is performed in later initcalls.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 */
2915
2916#ifdef CONFIG_NETFILTER
Pablo Neira Ayuso6d11cfd2013-05-22 22:42:36 +00002917 err = netfilter_init();
2918 if (err)
2919 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920#endif
David S. Millercbeb3212005-12-22 12:58:55 -08002921
Daniel Borkmann408eccc2014-04-01 16:20:23 +02002922 ptp_classifier_init();
Richard Cochranc1f19b52010-07-17 08:49:36 +00002923
Nick Pigginb3e19d92011-01-07 17:50:11 +11002924out:
2925 return err;
2926
2927out_mount:
2928 unregister_filesystem(&sock_fs_type);
2929out_fs:
2930 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931}
2932
Andi Kleen77d76ea2005-12-22 12:43:42 -08002933core_initcall(sock_init); /* early initcall */
2934
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935#ifdef CONFIG_PROC_FS
2936void socket_seq_show(struct seq_file *seq)
2937{
Tonghao Zhang648845a2017-12-14 05:51:58 -08002938 seq_printf(seq, "sockets: used %d\n",
2939 sock_inuse_get(seq->private));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940}
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002941#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942
Shaun Pereira89bbfc92006-03-21 23:58:08 -08002943#ifdef CONFIG_COMPAT
Al Viro36fd6332017-06-26 13:19:16 -04002944static int compat_dev_ifconf(struct net *net, struct compat_ifconf __user *uifc32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002945{
Arnd Bergmann6b960182009-11-06 23:10:54 -08002946 struct compat_ifconf ifc32;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002947 struct ifconf ifc;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002948 int err;
2949
Arnd Bergmann6b960182009-11-06 23:10:54 -08002950 if (copy_from_user(&ifc32, uifc32, sizeof(struct compat_ifconf)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002951 return -EFAULT;
2952
Al Viro36fd6332017-06-26 13:19:16 -04002953 ifc.ifc_len = ifc32.ifc_len;
2954 ifc.ifc_req = compat_ptr(ifc32.ifcbuf);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002955
Al Viro36fd6332017-06-26 13:19:16 -04002956 rtnl_lock();
2957 err = dev_ifconf(net, &ifc, sizeof(struct compat_ifreq));
2958 rtnl_unlock();
Arnd Bergmann7a229382009-11-06 23:00:29 -08002959 if (err)
2960 return err;
2961
Al Viro36fd6332017-06-26 13:19:16 -04002962 ifc32.ifc_len = ifc.ifc_len;
Arnd Bergmann6b960182009-11-06 23:10:54 -08002963 if (copy_to_user(uifc32, &ifc32, sizeof(struct compat_ifconf)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002964 return -EFAULT;
2965
2966 return 0;
2967}
2968
Arnd Bergmann6b960182009-11-06 23:10:54 -08002969static int ethtool_ioctl(struct net *net, struct compat_ifreq __user *ifr32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002970{
Ben Hutchings3a7da392011-03-17 07:34:32 +00002971 struct compat_ethtool_rxnfc __user *compat_rxnfc;
2972 bool convert_in = false, convert_out = false;
Al Viro44c02a22017-10-05 12:59:44 -04002973 size_t buf_size = 0;
2974 struct ethtool_rxnfc __user *rxnfc = NULL;
2975 struct ifreq ifr;
Ben Hutchings3a7da392011-03-17 07:34:32 +00002976 u32 rule_cnt = 0, actual_rule_cnt;
2977 u32 ethcmd;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002978 u32 data;
Ben Hutchings3a7da392011-03-17 07:34:32 +00002979 int ret;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002980
2981 if (get_user(data, &ifr32->ifr_ifru.ifru_data))
2982 return -EFAULT;
2983
Ben Hutchings3a7da392011-03-17 07:34:32 +00002984 compat_rxnfc = compat_ptr(data);
2985
2986 if (get_user(ethcmd, &compat_rxnfc->cmd))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002987 return -EFAULT;
2988
Ben Hutchings3a7da392011-03-17 07:34:32 +00002989 /* Most ethtool structures are defined without padding.
2990 * Unfortunately struct ethtool_rxnfc is an exception.
2991 */
2992 switch (ethcmd) {
2993 default:
2994 break;
2995 case ETHTOOL_GRXCLSRLALL:
2996 /* Buffer size is variable */
2997 if (get_user(rule_cnt, &compat_rxnfc->rule_cnt))
2998 return -EFAULT;
2999 if (rule_cnt > KMALLOC_MAX_SIZE / sizeof(u32))
3000 return -ENOMEM;
3001 buf_size += rule_cnt * sizeof(u32);
3002 /* fall through */
3003 case ETHTOOL_GRXRINGS:
3004 case ETHTOOL_GRXCLSRLCNT:
3005 case ETHTOOL_GRXCLSRULE:
Ben Hutchings55664f32012-01-03 12:04:51 +00003006 case ETHTOOL_SRXCLSRLINS:
Ben Hutchings3a7da392011-03-17 07:34:32 +00003007 convert_out = true;
3008 /* fall through */
3009 case ETHTOOL_SRXCLSRLDEL:
Ben Hutchings3a7da392011-03-17 07:34:32 +00003010 buf_size += sizeof(struct ethtool_rxnfc);
3011 convert_in = true;
Al Viro44c02a22017-10-05 12:59:44 -04003012 rxnfc = compat_alloc_user_space(buf_size);
Ben Hutchings3a7da392011-03-17 07:34:32 +00003013 break;
3014 }
3015
Al Viro44c02a22017-10-05 12:59:44 -04003016 if (copy_from_user(&ifr.ifr_name, &ifr32->ifr_name, IFNAMSIZ))
Ben Hutchings3a7da392011-03-17 07:34:32 +00003017 return -EFAULT;
3018
Al Viro44c02a22017-10-05 12:59:44 -04003019 ifr.ifr_data = convert_in ? rxnfc : (void __user *)compat_rxnfc;
Ben Hutchings3a7da392011-03-17 07:34:32 +00003020
3021 if (convert_in) {
Alexander Duyck127fe532011-04-08 18:01:59 +00003022 /* We expect there to be holes between fs.m_ext and
Ben Hutchings3a7da392011-03-17 07:34:32 +00003023 * fs.ring_cookie and at the end of fs, but nowhere else.
3024 */
Alexander Duyck127fe532011-04-08 18:01:59 +00003025 BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc, fs.m_ext) +
3026 sizeof(compat_rxnfc->fs.m_ext) !=
3027 offsetof(struct ethtool_rxnfc, fs.m_ext) +
3028 sizeof(rxnfc->fs.m_ext));
Ben Hutchings3a7da392011-03-17 07:34:32 +00003029 BUILD_BUG_ON(
3030 offsetof(struct compat_ethtool_rxnfc, fs.location) -
3031 offsetof(struct compat_ethtool_rxnfc, fs.ring_cookie) !=
3032 offsetof(struct ethtool_rxnfc, fs.location) -
3033 offsetof(struct ethtool_rxnfc, fs.ring_cookie));
3034
3035 if (copy_in_user(rxnfc, compat_rxnfc,
Stephen Hemminger954b1242013-02-11 06:22:28 +00003036 (void __user *)(&rxnfc->fs.m_ext + 1) -
3037 (void __user *)rxnfc) ||
Ben Hutchings3a7da392011-03-17 07:34:32 +00003038 copy_in_user(&rxnfc->fs.ring_cookie,
3039 &compat_rxnfc->fs.ring_cookie,
Stephen Hemminger954b1242013-02-11 06:22:28 +00003040 (void __user *)(&rxnfc->fs.location + 1) -
Wenwen Wangb6168562018-10-18 09:36:46 -05003041 (void __user *)&rxnfc->fs.ring_cookie))
3042 return -EFAULT;
3043 if (ethcmd == ETHTOOL_GRXCLSRLALL) {
3044 if (put_user(rule_cnt, &rxnfc->rule_cnt))
3045 return -EFAULT;
3046 } else if (copy_in_user(&rxnfc->rule_cnt,
3047 &compat_rxnfc->rule_cnt,
3048 sizeof(rxnfc->rule_cnt)))
Ben Hutchings3a7da392011-03-17 07:34:32 +00003049 return -EFAULT;
3050 }
3051
Al Viro44c02a22017-10-05 12:59:44 -04003052 ret = dev_ioctl(net, SIOCETHTOOL, &ifr, NULL);
Ben Hutchings3a7da392011-03-17 07:34:32 +00003053 if (ret)
3054 return ret;
3055
3056 if (convert_out) {
3057 if (copy_in_user(compat_rxnfc, rxnfc,
Stephen Hemminger954b1242013-02-11 06:22:28 +00003058 (const void __user *)(&rxnfc->fs.m_ext + 1) -
3059 (const void __user *)rxnfc) ||
Ben Hutchings3a7da392011-03-17 07:34:32 +00003060 copy_in_user(&compat_rxnfc->fs.ring_cookie,
3061 &rxnfc->fs.ring_cookie,
Stephen Hemminger954b1242013-02-11 06:22:28 +00003062 (const void __user *)(&rxnfc->fs.location + 1) -
3063 (const void __user *)&rxnfc->fs.ring_cookie) ||
Ben Hutchings3a7da392011-03-17 07:34:32 +00003064 copy_in_user(&compat_rxnfc->rule_cnt, &rxnfc->rule_cnt,
3065 sizeof(rxnfc->rule_cnt)))
3066 return -EFAULT;
3067
3068 if (ethcmd == ETHTOOL_GRXCLSRLALL) {
3069 /* As an optimisation, we only copy the actual
3070 * number of rules that the underlying
3071 * function returned. Since Mallory might
3072 * change the rule count in user memory, we
3073 * check that it is less than the rule count
3074 * originally given (as the user buffer size),
3075 * which has been range-checked.
3076 */
3077 if (get_user(actual_rule_cnt, &rxnfc->rule_cnt))
3078 return -EFAULT;
3079 if (actual_rule_cnt < rule_cnt)
3080 rule_cnt = actual_rule_cnt;
3081 if (copy_in_user(&compat_rxnfc->rule_locs[0],
3082 &rxnfc->rule_locs[0],
3083 rule_cnt * sizeof(u32)))
3084 return -EFAULT;
3085 }
3086 }
3087
3088 return 0;
Arnd Bergmann7a229382009-11-06 23:00:29 -08003089}
3090
Arnd Bergmann7a50a242009-11-08 20:57:03 -08003091static int compat_siocwandev(struct net *net, struct compat_ifreq __user *uifr32)
3092{
Arnd Bergmann7a50a242009-11-08 20:57:03 -08003093 compat_uptr_t uptr32;
Al Viro44c02a22017-10-05 12:59:44 -04003094 struct ifreq ifr;
3095 void __user *saved;
3096 int err;
Arnd Bergmann7a50a242009-11-08 20:57:03 -08003097
Al Viro44c02a22017-10-05 12:59:44 -04003098 if (copy_from_user(&ifr, uifr32, sizeof(struct compat_ifreq)))
Arnd Bergmann7a50a242009-11-08 20:57:03 -08003099 return -EFAULT;
3100
3101 if (get_user(uptr32, &uifr32->ifr_settings.ifs_ifsu))
3102 return -EFAULT;
3103
Al Viro44c02a22017-10-05 12:59:44 -04003104 saved = ifr.ifr_settings.ifs_ifsu.raw_hdlc;
3105 ifr.ifr_settings.ifs_ifsu.raw_hdlc = compat_ptr(uptr32);
Arnd Bergmann7a50a242009-11-08 20:57:03 -08003106
Al Viro44c02a22017-10-05 12:59:44 -04003107 err = dev_ioctl(net, SIOCWANDEV, &ifr, NULL);
3108 if (!err) {
3109 ifr.ifr_settings.ifs_ifsu.raw_hdlc = saved;
3110 if (copy_to_user(uifr32, &ifr, sizeof(struct compat_ifreq)))
3111 err = -EFAULT;
Joe Perchesccbd6a52010-05-14 10:58:26 +00003112 }
Al Viro44c02a22017-10-05 12:59:44 -04003113 return err;
Arnd Bergmann7a229382009-11-06 23:00:29 -08003114}
3115
Ben Hutchings590d4692013-11-18 17:04:13 +00003116/* Handle ioctls that use ifreq::ifr_data and just need struct ifreq converted */
3117static int compat_ifr_data_ioctl(struct net *net, unsigned int cmd,
Arnd Bergmann6b960182009-11-06 23:10:54 -08003118 struct compat_ifreq __user *u_ifreq32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08003119{
Al Viro44c02a22017-10-05 12:59:44 -04003120 struct ifreq ifreq;
Arnd Bergmann7a229382009-11-06 23:00:29 -08003121 u32 data32;
3122
Al Viro44c02a22017-10-05 12:59:44 -04003123 if (copy_from_user(ifreq.ifr_name, u_ifreq32->ifr_name, IFNAMSIZ))
Arnd Bergmann7a229382009-11-06 23:00:29 -08003124 return -EFAULT;
Al Viro44c02a22017-10-05 12:59:44 -04003125 if (get_user(data32, &u_ifreq32->ifr_data))
Arnd Bergmann7a229382009-11-06 23:00:29 -08003126 return -EFAULT;
Al Viro44c02a22017-10-05 12:59:44 -04003127 ifreq.ifr_data = compat_ptr(data32);
Arnd Bergmann7a229382009-11-06 23:00:29 -08003128
Al Viro44c02a22017-10-05 12:59:44 -04003129 return dev_ioctl(net, cmd, &ifreq, NULL);
Arnd Bergmann7a229382009-11-06 23:00:29 -08003130}
3131
Johannes Berg37ac39b2019-01-25 22:43:18 +01003132static int compat_ifreq_ioctl(struct net *net, struct socket *sock,
3133 unsigned int cmd,
3134 struct compat_ifreq __user *uifr32)
3135{
3136 struct ifreq __user *uifr;
3137 int err;
3138
3139 /* Handle the fact that while struct ifreq has the same *layout* on
3140 * 32/64 for everything but ifreq::ifru_ifmap and ifreq::ifru_data,
3141 * which are handled elsewhere, it still has different *size* due to
3142 * ifreq::ifru_ifmap (which is 16 bytes on 32 bit, 24 bytes on 64-bit,
3143 * resulting in struct ifreq being 32 and 40 bytes respectively).
3144 * As a result, if the struct happens to be at the end of a page and
3145 * the next page isn't readable/writable, we get a fault. To prevent
3146 * that, copy back and forth to the full size.
3147 */
3148
3149 uifr = compat_alloc_user_space(sizeof(*uifr));
3150 if (copy_in_user(uifr, uifr32, sizeof(*uifr32)))
3151 return -EFAULT;
3152
3153 err = sock_do_ioctl(net, sock, cmd, (unsigned long)uifr);
3154
3155 if (!err) {
3156 switch (cmd) {
3157 case SIOCGIFFLAGS:
3158 case SIOCGIFMETRIC:
3159 case SIOCGIFMTU:
3160 case SIOCGIFMEM:
3161 case SIOCGIFHWADDR:
3162 case SIOCGIFINDEX:
3163 case SIOCGIFADDR:
3164 case SIOCGIFBRDADDR:
3165 case SIOCGIFDSTADDR:
3166 case SIOCGIFNETMASK:
3167 case SIOCGIFPFLAGS:
3168 case SIOCGIFTXQLEN:
3169 case SIOCGMIIPHY:
3170 case SIOCGMIIREG:
Johannes Bergc6c9fee2019-01-25 22:43:19 +01003171 case SIOCGIFNAME:
Johannes Berg37ac39b2019-01-25 22:43:18 +01003172 if (copy_in_user(uifr32, uifr, sizeof(*uifr32)))
3173 err = -EFAULT;
3174 break;
3175 }
3176 }
3177 return err;
3178}
3179
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003180static int compat_sioc_ifmap(struct net *net, unsigned int cmd,
3181 struct compat_ifreq __user *uifr32)
3182{
3183 struct ifreq ifr;
3184 struct compat_ifmap __user *uifmap32;
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003185 int err;
3186
3187 uifmap32 = &uifr32->ifr_ifru.ifru_map;
3188 err = copy_from_user(&ifr, uifr32, sizeof(ifr.ifr_name));
Mathieu Desnoyers3ddc5b42013-09-11 14:23:18 -07003189 err |= get_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
3190 err |= get_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
3191 err |= get_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
3192 err |= get_user(ifr.ifr_map.irq, &uifmap32->irq);
3193 err |= get_user(ifr.ifr_map.dma, &uifmap32->dma);
3194 err |= get_user(ifr.ifr_map.port, &uifmap32->port);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003195 if (err)
3196 return -EFAULT;
3197
Al Viro44c02a22017-10-05 12:59:44 -04003198 err = dev_ioctl(net, cmd, &ifr, NULL);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003199
3200 if (cmd == SIOCGIFMAP && !err) {
3201 err = copy_to_user(uifr32, &ifr, sizeof(ifr.ifr_name));
Mathieu Desnoyers3ddc5b42013-09-11 14:23:18 -07003202 err |= put_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
3203 err |= put_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
3204 err |= put_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
3205 err |= put_user(ifr.ifr_map.irq, &uifmap32->irq);
3206 err |= put_user(ifr.ifr_map.dma, &uifmap32->dma);
3207 err |= put_user(ifr.ifr_map.port, &uifmap32->port);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003208 if (err)
3209 err = -EFAULT;
3210 }
3211 return err;
3212}
3213
Arnd Bergmann7a229382009-11-06 23:00:29 -08003214struct rtentry32 {
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003215 u32 rt_pad1;
Arnd Bergmann7a229382009-11-06 23:00:29 -08003216 struct sockaddr rt_dst; /* target address */
3217 struct sockaddr rt_gateway; /* gateway addr (RTF_GATEWAY) */
3218 struct sockaddr rt_genmask; /* target network mask (IP) */
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003219 unsigned short rt_flags;
3220 short rt_pad2;
3221 u32 rt_pad3;
3222 unsigned char rt_tos;
3223 unsigned char rt_class;
3224 short rt_pad4;
3225 short rt_metric; /* +1 for binary compatibility! */
Arnd Bergmann7a229382009-11-06 23:00:29 -08003226 /* char * */ u32 rt_dev; /* forcing the device at add */
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003227 u32 rt_mtu; /* per route MTU/Window */
3228 u32 rt_window; /* Window clamping */
Arnd Bergmann7a229382009-11-06 23:00:29 -08003229 unsigned short rt_irtt; /* Initial RTT */
3230};
3231
3232struct in6_rtmsg32 {
3233 struct in6_addr rtmsg_dst;
3234 struct in6_addr rtmsg_src;
3235 struct in6_addr rtmsg_gateway;
3236 u32 rtmsg_type;
3237 u16 rtmsg_dst_len;
3238 u16 rtmsg_src_len;
3239 u32 rtmsg_metric;
3240 u32 rtmsg_info;
3241 u32 rtmsg_flags;
3242 s32 rtmsg_ifindex;
3243};
3244
Arnd Bergmann6b960182009-11-06 23:10:54 -08003245static int routing_ioctl(struct net *net, struct socket *sock,
3246 unsigned int cmd, void __user *argp)
Arnd Bergmann7a229382009-11-06 23:00:29 -08003247{
3248 int ret;
3249 void *r = NULL;
3250 struct in6_rtmsg r6;
3251 struct rtentry r4;
3252 char devname[16];
3253 u32 rtdev;
3254 mm_segment_t old_fs = get_fs();
3255
Arnd Bergmann6b960182009-11-06 23:10:54 -08003256 if (sock && sock->sk && sock->sk->sk_family == AF_INET6) { /* ipv6 */
3257 struct in6_rtmsg32 __user *ur6 = argp;
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003258 ret = copy_from_user(&r6.rtmsg_dst, &(ur6->rtmsg_dst),
Arnd Bergmann7a229382009-11-06 23:00:29 -08003259 3 * sizeof(struct in6_addr));
Mathieu Desnoyers3ddc5b42013-09-11 14:23:18 -07003260 ret |= get_user(r6.rtmsg_type, &(ur6->rtmsg_type));
3261 ret |= get_user(r6.rtmsg_dst_len, &(ur6->rtmsg_dst_len));
3262 ret |= get_user(r6.rtmsg_src_len, &(ur6->rtmsg_src_len));
3263 ret |= get_user(r6.rtmsg_metric, &(ur6->rtmsg_metric));
3264 ret |= get_user(r6.rtmsg_info, &(ur6->rtmsg_info));
3265 ret |= get_user(r6.rtmsg_flags, &(ur6->rtmsg_flags));
3266 ret |= get_user(r6.rtmsg_ifindex, &(ur6->rtmsg_ifindex));
Arnd Bergmann7a229382009-11-06 23:00:29 -08003267
3268 r = (void *) &r6;
3269 } else { /* ipv4 */
Arnd Bergmann6b960182009-11-06 23:10:54 -08003270 struct rtentry32 __user *ur4 = argp;
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003271 ret = copy_from_user(&r4.rt_dst, &(ur4->rt_dst),
Arnd Bergmann7a229382009-11-06 23:00:29 -08003272 3 * sizeof(struct sockaddr));
Mathieu Desnoyers3ddc5b42013-09-11 14:23:18 -07003273 ret |= get_user(r4.rt_flags, &(ur4->rt_flags));
3274 ret |= get_user(r4.rt_metric, &(ur4->rt_metric));
3275 ret |= get_user(r4.rt_mtu, &(ur4->rt_mtu));
3276 ret |= get_user(r4.rt_window, &(ur4->rt_window));
3277 ret |= get_user(r4.rt_irtt, &(ur4->rt_irtt));
3278 ret |= get_user(rtdev, &(ur4->rt_dev));
Arnd Bergmann7a229382009-11-06 23:00:29 -08003279 if (rtdev) {
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003280 ret |= copy_from_user(devname, compat_ptr(rtdev), 15);
stephen hemmingerc3f52ae2011-02-23 09:06:48 +00003281 r4.rt_dev = (char __user __force *)devname;
3282 devname[15] = 0;
Arnd Bergmann7a229382009-11-06 23:00:29 -08003283 } else
3284 r4.rt_dev = NULL;
3285
3286 r = (void *) &r4;
3287 }
3288
3289 if (ret) {
3290 ret = -EFAULT;
3291 goto out;
3292 }
3293
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003294 set_fs(KERNEL_DS);
Johannes Berg63ff03a2019-01-25 22:43:17 +01003295 ret = sock_do_ioctl(net, sock, cmd, (unsigned long) r);
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003296 set_fs(old_fs);
Arnd Bergmann7a229382009-11-06 23:00:29 -08003297
3298out:
Arnd Bergmann7a229382009-11-06 23:00:29 -08003299 return ret;
3300}
3301
3302/* Since old style bridge ioctl's endup using SIOCDEVPRIVATE
3303 * for some operations; this forces use of the newer bridge-utils that
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003304 * use compatible ioctls
Arnd Bergmann7a229382009-11-06 23:00:29 -08003305 */
Arnd Bergmann6b960182009-11-06 23:10:54 -08003306static int old_bridge_ioctl(compat_ulong_t __user *argp)
Arnd Bergmann7a229382009-11-06 23:00:29 -08003307{
Arnd Bergmann6b960182009-11-06 23:10:54 -08003308 compat_ulong_t tmp;
Arnd Bergmann7a229382009-11-06 23:00:29 -08003309
Arnd Bergmann6b960182009-11-06 23:10:54 -08003310 if (get_user(tmp, argp))
Arnd Bergmann7a229382009-11-06 23:00:29 -08003311 return -EFAULT;
3312 if (tmp == BRCTL_GET_VERSION)
3313 return BRCTL_VERSION + 1;
3314 return -EINVAL;
3315}
3316
Arnd Bergmann6b960182009-11-06 23:10:54 -08003317static int compat_sock_ioctl_trans(struct file *file, struct socket *sock,
3318 unsigned int cmd, unsigned long arg)
3319{
3320 void __user *argp = compat_ptr(arg);
3321 struct sock *sk = sock->sk;
3322 struct net *net = sock_net(sk);
Arnd Bergmann7a229382009-11-06 23:00:29 -08003323
Arnd Bergmann6b960182009-11-06 23:10:54 -08003324 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15))
Ben Hutchings590d4692013-11-18 17:04:13 +00003325 return compat_ifr_data_ioctl(net, cmd, argp);
Arnd Bergmann7a229382009-11-06 23:00:29 -08003326
Arnd Bergmann6b960182009-11-06 23:10:54 -08003327 switch (cmd) {
3328 case SIOCSIFBR:
3329 case SIOCGIFBR:
3330 return old_bridge_ioctl(argp);
Arnd Bergmann6b960182009-11-06 23:10:54 -08003331 case SIOCGIFCONF:
Al Viro36fd6332017-06-26 13:19:16 -04003332 return compat_dev_ifconf(net, argp);
Arnd Bergmann6b960182009-11-06 23:10:54 -08003333 case SIOCETHTOOL:
3334 return ethtool_ioctl(net, argp);
Arnd Bergmann7a50a242009-11-08 20:57:03 -08003335 case SIOCWANDEV:
3336 return compat_siocwandev(net, argp);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003337 case SIOCGIFMAP:
3338 case SIOCSIFMAP:
3339 return compat_sioc_ifmap(net, cmd, argp);
Arnd Bergmann6b960182009-11-06 23:10:54 -08003340 case SIOCADDRT:
3341 case SIOCDELRT:
3342 return routing_ioctl(net, sock, cmd, argp);
Arnd Bergmann0768e172019-04-17 22:56:11 +02003343 case SIOCGSTAMP_OLD:
3344 case SIOCGSTAMPNS_OLD:
Arnd Bergmannc7cbdbf2019-04-17 22:51:48 +02003345 if (!sock->ops->gettstamp)
3346 return -ENOIOCTLCMD;
Arnd Bergmann0768e172019-04-17 22:56:11 +02003347 return sock->ops->gettstamp(sock, argp, cmd == SIOCGSTAMP_OLD,
Arnd Bergmannc7cbdbf2019-04-17 22:51:48 +02003348 !COMPAT_USE_64BIT_TIME);
3349
Ben Hutchings590d4692013-11-18 17:04:13 +00003350 case SIOCBONDSLAVEINFOQUERY:
3351 case SIOCBONDINFOQUERY:
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003352 case SIOCSHWTSTAMP:
Ben Hutchingsfd468c72013-11-14 01:19:29 +00003353 case SIOCGHWTSTAMP:
Ben Hutchings590d4692013-11-18 17:04:13 +00003354 return compat_ifr_data_ioctl(net, cmd, argp);
Arnd Bergmann7a229382009-11-06 23:00:29 -08003355
Arnd Bergmann6b960182009-11-06 23:10:54 -08003356 case FIOSETOWN:
3357 case SIOCSPGRP:
3358 case FIOGETOWN:
3359 case SIOCGPGRP:
3360 case SIOCBRADDBR:
3361 case SIOCBRDELBR:
3362 case SIOCGIFVLAN:
3363 case SIOCSIFVLAN:
3364 case SIOCADDDLCI:
3365 case SIOCDELDLCI:
Andrey Vaginc62cce22016-10-24 18:29:13 -07003366 case SIOCGSKNS:
Arnd Bergmann0768e172019-04-17 22:56:11 +02003367 case SIOCGSTAMP_NEW:
3368 case SIOCGSTAMPNS_NEW:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003369 return sock_ioctl(file, cmd, arg);
3370
3371 case SIOCGIFFLAGS:
3372 case SIOCSIFFLAGS:
3373 case SIOCGIFMETRIC:
3374 case SIOCSIFMETRIC:
3375 case SIOCGIFMTU:
3376 case SIOCSIFMTU:
3377 case SIOCGIFMEM:
3378 case SIOCSIFMEM:
3379 case SIOCGIFHWADDR:
3380 case SIOCSIFHWADDR:
3381 case SIOCADDMULTI:
3382 case SIOCDELMULTI:
3383 case SIOCGIFINDEX:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003384 case SIOCGIFADDR:
3385 case SIOCSIFADDR:
3386 case SIOCSIFHWBROADCAST:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003387 case SIOCDIFADDR:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003388 case SIOCGIFBRDADDR:
3389 case SIOCSIFBRDADDR:
3390 case SIOCGIFDSTADDR:
3391 case SIOCSIFDSTADDR:
3392 case SIOCGIFNETMASK:
3393 case SIOCSIFNETMASK:
3394 case SIOCSIFPFLAGS:
3395 case SIOCGIFPFLAGS:
3396 case SIOCGIFTXQLEN:
3397 case SIOCSIFTXQLEN:
3398 case SIOCBRADDIF:
3399 case SIOCBRDELIF:
Johannes Bergc6c9fee2019-01-25 22:43:19 +01003400 case SIOCGIFNAME:
Arnd Bergmann9177efd2009-11-06 08:09:09 +00003401 case SIOCSIFNAME:
3402 case SIOCGMIIPHY:
3403 case SIOCGMIIREG:
3404 case SIOCSMIIREG:
Al Virof92d4fc2017-09-30 19:32:17 -04003405 case SIOCBONDENSLAVE:
3406 case SIOCBONDRELEASE:
3407 case SIOCBONDSETHWADDR:
3408 case SIOCBONDCHANGEACTIVE:
Johannes Berg37ac39b2019-01-25 22:43:18 +01003409 return compat_ifreq_ioctl(net, sock, cmd, argp);
3410
Arnd Bergmann6b960182009-11-06 23:10:54 -08003411 case SIOCSARP:
3412 case SIOCGARP:
3413 case SIOCDARP:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003414 case SIOCATMARK:
Johannes Berg63ff03a2019-01-25 22:43:17 +01003415 return sock_do_ioctl(net, sock, cmd, arg);
Arnd Bergmann9177efd2009-11-06 08:09:09 +00003416 }
3417
Arnd Bergmann6b960182009-11-06 23:10:54 -08003418 return -ENOIOCTLCMD;
3419}
Arnd Bergmann7a229382009-11-06 23:00:29 -08003420
Eric Dumazet95c96172012-04-15 05:58:06 +00003421static long compat_sock_ioctl(struct file *file, unsigned int cmd,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07003422 unsigned long arg)
Shaun Pereira89bbfc92006-03-21 23:58:08 -08003423{
3424 struct socket *sock = file->private_data;
3425 int ret = -ENOIOCTLCMD;
David S. Miller87de87d2008-06-03 09:14:03 -07003426 struct sock *sk;
3427 struct net *net;
3428
3429 sk = sock->sk;
3430 net = sock_net(sk);
Shaun Pereira89bbfc92006-03-21 23:58:08 -08003431
3432 if (sock->ops->compat_ioctl)
3433 ret = sock->ops->compat_ioctl(sock, cmd, arg);
3434
David S. Miller87de87d2008-06-03 09:14:03 -07003435 if (ret == -ENOIOCTLCMD &&
3436 (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST))
3437 ret = compat_wext_handle_ioctl(net, cmd, arg);
3438
Arnd Bergmann6b960182009-11-06 23:10:54 -08003439 if (ret == -ENOIOCTLCMD)
3440 ret = compat_sock_ioctl_trans(file, sock, cmd, arg);
3441
Shaun Pereira89bbfc92006-03-21 23:58:08 -08003442 return ret;
3443}
3444#endif
3445
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003446/**
3447 * kernel_bind - bind an address to a socket (kernel space)
3448 * @sock: socket
3449 * @addr: address
3450 * @addrlen: length of address
3451 *
3452 * Returns 0 or an error.
3453 */
3454
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003455int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen)
3456{
3457 return sock->ops->bind(sock, addr, addrlen);
3458}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003459EXPORT_SYMBOL(kernel_bind);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003460
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003461/**
3462 * kernel_listen - move socket to listening state (kernel space)
3463 * @sock: socket
3464 * @backlog: pending connections queue size
3465 *
3466 * Returns 0 or an error.
3467 */
3468
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003469int kernel_listen(struct socket *sock, int backlog)
3470{
3471 return sock->ops->listen(sock, backlog);
3472}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003473EXPORT_SYMBOL(kernel_listen);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003474
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003475/**
3476 * kernel_accept - accept a connection (kernel space)
3477 * @sock: listening socket
3478 * @newsock: new connected socket
3479 * @flags: flags
3480 *
3481 * @flags must be SOCK_CLOEXEC, SOCK_NONBLOCK or 0.
3482 * If it fails, @newsock is guaranteed to be %NULL.
3483 * Returns 0 or an error.
3484 */
3485
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003486int kernel_accept(struct socket *sock, struct socket **newsock, int flags)
3487{
3488 struct sock *sk = sock->sk;
3489 int err;
3490
3491 err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
3492 newsock);
3493 if (err < 0)
3494 goto done;
3495
David Howellscdfbabf2017-03-09 08:09:05 +00003496 err = sock->ops->accept(sock, *newsock, flags, true);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003497 if (err < 0) {
3498 sock_release(*newsock);
Tony Battersbyfa8705b2007-10-10 21:09:04 -07003499 *newsock = NULL;
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003500 goto done;
3501 }
3502
3503 (*newsock)->ops = sock->ops;
Wei Yongjun1b085342008-12-18 19:35:10 -08003504 __module_get((*newsock)->ops->owner);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003505
3506done:
3507 return err;
3508}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003509EXPORT_SYMBOL(kernel_accept);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003510
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003511/**
3512 * kernel_connect - connect a socket (kernel space)
3513 * @sock: socket
3514 * @addr: address
3515 * @addrlen: address length
3516 * @flags: flags (O_NONBLOCK, ...)
3517 *
3518 * For datagram sockets, @addr is the addres to which datagrams are sent
3519 * by default, and the only address from which datagrams are received.
3520 * For stream sockets, attempts to connect to @addr.
3521 * Returns 0 or an error code.
3522 */
3523
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003524int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
YOSHIFUJI Hideaki4768fbc2007-02-09 23:25:31 +09003525 int flags)
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003526{
3527 return sock->ops->connect(sock, addr, addrlen, flags);
3528}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003529EXPORT_SYMBOL(kernel_connect);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003530
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003531/**
3532 * kernel_getsockname - get the address which the socket is bound (kernel space)
3533 * @sock: socket
3534 * @addr: address holder
3535 *
3536 * Fills the @addr pointer with the address which the socket is bound.
3537 * Returns 0 or an error code.
3538 */
3539
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01003540int kernel_getsockname(struct socket *sock, struct sockaddr *addr)
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003541{
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01003542 return sock->ops->getname(sock, addr, 0);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003543}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003544EXPORT_SYMBOL(kernel_getsockname);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003545
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003546/**
3547 * kernel_peername - get the address which the socket is connected (kernel space)
3548 * @sock: socket
3549 * @addr: address holder
3550 *
3551 * Fills the @addr pointer with the address which the socket is connected.
3552 * Returns 0 or an error code.
3553 */
3554
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01003555int kernel_getpeername(struct socket *sock, struct sockaddr *addr)
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003556{
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01003557 return sock->ops->getname(sock, addr, 1);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003558}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003559EXPORT_SYMBOL(kernel_getpeername);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003560
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003561/**
3562 * kernel_getsockopt - get a socket option (kernel space)
3563 * @sock: socket
3564 * @level: API level (SOL_SOCKET, ...)
3565 * @optname: option tag
3566 * @optval: option value
3567 * @optlen: option length
3568 *
3569 * Assigns the option length to @optlen.
3570 * Returns 0 or an error.
3571 */
3572
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003573int kernel_getsockopt(struct socket *sock, int level, int optname,
3574 char *optval, int *optlen)
3575{
3576 mm_segment_t oldfs = get_fs();
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003577 char __user *uoptval;
3578 int __user *uoptlen;
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003579 int err;
3580
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003581 uoptval = (char __user __force *) optval;
3582 uoptlen = (int __user __force *) optlen;
3583
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003584 set_fs(KERNEL_DS);
3585 if (level == SOL_SOCKET)
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003586 err = sock_getsockopt(sock, level, optname, uoptval, uoptlen);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003587 else
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003588 err = sock->ops->getsockopt(sock, level, optname, uoptval,
3589 uoptlen);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003590 set_fs(oldfs);
3591 return err;
3592}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003593EXPORT_SYMBOL(kernel_getsockopt);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003594
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003595/**
3596 * kernel_setsockopt - set a socket option (kernel space)
3597 * @sock: socket
3598 * @level: API level (SOL_SOCKET, ...)
3599 * @optname: option tag
3600 * @optval: option value
3601 * @optlen: option length
3602 *
3603 * Returns 0 or an error.
3604 */
3605
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003606int kernel_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07003607 char *optval, unsigned int optlen)
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003608{
3609 mm_segment_t oldfs = get_fs();
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003610 char __user *uoptval;
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003611 int err;
3612
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003613 uoptval = (char __user __force *) optval;
3614
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003615 set_fs(KERNEL_DS);
3616 if (level == SOL_SOCKET)
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003617 err = sock_setsockopt(sock, level, optname, uoptval, optlen);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003618 else
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003619 err = sock->ops->setsockopt(sock, level, optname, uoptval,
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003620 optlen);
3621 set_fs(oldfs);
3622 return err;
3623}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003624EXPORT_SYMBOL(kernel_setsockopt);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003625
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003626/**
3627 * kernel_sendpage - send a &page through a socket (kernel space)
3628 * @sock: socket
3629 * @page: page
3630 * @offset: page offset
3631 * @size: total size in bytes
3632 * @flags: flags (MSG_DONTWAIT, ...)
3633 *
3634 * Returns the total amount sent in bytes or an error.
3635 */
3636
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003637int kernel_sendpage(struct socket *sock, struct page *page, int offset,
3638 size_t size, int flags)
3639{
3640 if (sock->ops->sendpage)
3641 return sock->ops->sendpage(sock, page, offset, size, flags);
3642
3643 return sock_no_sendpage(sock, page, offset, size, flags);
3644}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003645EXPORT_SYMBOL(kernel_sendpage);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003646
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003647/**
3648 * kernel_sendpage_locked - send a &page through the locked sock (kernel space)
3649 * @sk: sock
3650 * @page: page
3651 * @offset: page offset
3652 * @size: total size in bytes
3653 * @flags: flags (MSG_DONTWAIT, ...)
3654 *
3655 * Returns the total amount sent in bytes or an error.
3656 * Caller must hold @sk.
3657 */
3658
Tom Herbert306b13e2017-07-28 16:22:41 -07003659int kernel_sendpage_locked(struct sock *sk, struct page *page, int offset,
3660 size_t size, int flags)
3661{
3662 struct socket *sock = sk->sk_socket;
3663
3664 if (sock->ops->sendpage_locked)
3665 return sock->ops->sendpage_locked(sk, page, offset, size,
3666 flags);
3667
3668 return sock_no_sendpage_locked(sk, page, offset, size, flags);
3669}
3670EXPORT_SYMBOL(kernel_sendpage_locked);
3671
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003672/**
3673 * kernel_shutdown - shut down part of a full-duplex connection (kernel space)
3674 * @sock: socket
3675 * @how: connection part
3676 *
3677 * Returns 0 or an error.
3678 */
3679
Trond Myklebust91cf45f2007-11-12 18:10:39 -08003680int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how)
3681{
3682 return sock->ops->shutdown(sock, how);
3683}
Trond Myklebust91cf45f2007-11-12 18:10:39 -08003684EXPORT_SYMBOL(kernel_sock_shutdown);
R. Parameswaran113c3072017-04-05 16:50:35 -07003685
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003686/**
3687 * kernel_sock_ip_overhead - returns the IP overhead imposed by a socket
3688 * @sk: socket
3689 *
3690 * This routine returns the IP overhead imposed by a socket i.e.
3691 * the length of the underlying IP header, depending on whether
3692 * this is an IPv4 or IPv6 socket and the length from IP options turned
3693 * on at the socket. Assumes that the caller has a lock on the socket.
R. Parameswaran113c3072017-04-05 16:50:35 -07003694 */
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003695
R. Parameswaran113c3072017-04-05 16:50:35 -07003696u32 kernel_sock_ip_overhead(struct sock *sk)
3697{
3698 struct inet_sock *inet;
3699 struct ip_options_rcu *opt;
3700 u32 overhead = 0;
R. Parameswaran113c3072017-04-05 16:50:35 -07003701#if IS_ENABLED(CONFIG_IPV6)
3702 struct ipv6_pinfo *np;
3703 struct ipv6_txoptions *optv6 = NULL;
3704#endif /* IS_ENABLED(CONFIG_IPV6) */
3705
3706 if (!sk)
3707 return overhead;
3708
R. Parameswaran113c3072017-04-05 16:50:35 -07003709 switch (sk->sk_family) {
3710 case AF_INET:
3711 inet = inet_sk(sk);
3712 overhead += sizeof(struct iphdr);
3713 opt = rcu_dereference_protected(inet->inet_opt,
stephen hemminger614d79c2017-07-24 10:25:22 -07003714 sock_owned_by_user(sk));
R. Parameswaran113c3072017-04-05 16:50:35 -07003715 if (opt)
3716 overhead += opt->opt.optlen;
3717 return overhead;
3718#if IS_ENABLED(CONFIG_IPV6)
3719 case AF_INET6:
3720 np = inet6_sk(sk);
3721 overhead += sizeof(struct ipv6hdr);
3722 if (np)
3723 optv6 = rcu_dereference_protected(np->opt,
stephen hemminger614d79c2017-07-24 10:25:22 -07003724 sock_owned_by_user(sk));
R. Parameswaran113c3072017-04-05 16:50:35 -07003725 if (optv6)
3726 overhead += (optv6->opt_flen + optv6->opt_nflen);
3727 return overhead;
3728#endif /* IS_ENABLED(CONFIG_IPV6) */
3729 default: /* Returns 0 overhead if the socket is not ipv4 or ipv6 */
3730 return overhead;
3731 }
3732}
3733EXPORT_SYMBOL(kernel_sock_ip_overhead);