blob: 5061c5d2730c7f7722f9f32b3cef0636bd0743e8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * NET An implementation of the SOCKET network access protocol.
3 *
4 * Version: @(#)socket.c 1.1.93 18/02/95
5 *
6 * Authors: Orest Zborowski, <obz@Kodak.COM>
Jesper Juhl02c30a82005-05-05 16:16:16 -07007 * Ross Biro
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
9 *
10 * Fixes:
11 * Anonymous : NOTSOCK/BADF cleanup. Error fix in
12 * shutdown()
13 * Alan Cox : verify_area() fixes
14 * Alan Cox : Removed DDI
15 * Jonathan Kamens : SOCK_DGRAM reconnect bug
16 * Alan Cox : Moved a load of checks to the very
17 * top level.
18 * Alan Cox : Move address structures to/from user
19 * mode above the protocol layers.
20 * Rob Janssen : Allow 0 length sends.
21 * Alan Cox : Asynchronous I/O support (cribbed from the
22 * tty drivers).
23 * Niibe Yutaka : Asynchronous I/O for writes (4.4BSD style)
24 * Jeff Uphoff : Made max number of sockets command-line
25 * configurable.
26 * Matti Aarnio : Made the number of sockets dynamic,
27 * to be allocated when needed, and mr.
28 * Uphoff's max is used as max to be
29 * allowed to allocate.
30 * Linus : Argh. removed all the socket allocation
31 * altogether: it's in the inode now.
32 * Alan Cox : Made sock_alloc()/sock_release() public
33 * for NetROM and future kernel nfsd type
34 * stuff.
35 * Alan Cox : sendmsg/recvmsg basics.
36 * Tom Dyas : Export net symbols.
37 * Marcin Dalecki : Fixed problems with CONFIG_NET="n".
38 * Alan Cox : Added thread locking to sys_* calls
39 * for sockets. May have errors at the
40 * moment.
41 * Kevin Buhr : Fixed the dumb errors in the above.
42 * Andi Kleen : Some small cleanups, optimizations,
43 * and fixed a copy_from_user() bug.
44 * Tigran Aivazian : sys_send(args) calls sys_sendto(args, NULL, 0)
Stephen Hemminger89bddce2006-09-01 00:19:31 -070045 * Tigran Aivazian : Made listen(2) backlog sanity checks
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 * protocol-independent
47 *
48 *
49 * This program is free software; you can redistribute it and/or
50 * modify it under the terms of the GNU General Public License
51 * as published by the Free Software Foundation; either version
52 * 2 of the License, or (at your option) any later version.
53 *
54 *
55 * This module is effectively the top level interface to the BSD socket
Stephen Hemminger89bddce2006-09-01 00:19:31 -070056 * paradigm.
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 *
58 * Based upon Swansea University Computer Society NET3.039
59 */
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <linux/socket.h>
63#include <linux/file.h>
64#include <linux/net.h>
65#include <linux/interrupt.h>
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -070066#include <linux/thread_info.h>
Stephen Hemminger55737fd2006-09-01 00:23:39 -070067#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/netdevice.h>
69#include <linux/proc_fs.h>
70#include <linux/seq_file.h>
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080071#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include <linux/if_bridge.h>
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -030073#include <linux/if_frad.h>
74#include <linux/if_vlan.h>
Daniel Borkmann408eccc2014-04-01 16:20:23 +020075#include <linux/ptp_classify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#include <linux/init.h>
77#include <linux/poll.h>
78#include <linux/cache.h>
79#include <linux/module.h>
80#include <linux/highmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#include <linux/mount.h>
David Howellsfba9be42019-03-25 16:38:24 +000082#include <linux/pseudo_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#include <linux/security.h>
84#include <linux/syscalls.h>
85#include <linux/compat.h>
86#include <linux/kmod.h>
David Woodhouse3ec3b2f2005-05-17 12:08:48 +010087#include <linux/audit.h>
Adrian Bunkd86b5e02006-01-21 00:46:55 +010088#include <linux/wireless.h>
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -070089#include <linux/nsproxy.h>
Nick Black1fd7317d2009-09-22 16:43:33 -070090#include <linux/magic.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090091#include <linux/slab.h>
Masatake YAMATO600e1772012-08-29 10:44:29 +000092#include <linux/xattr.h>
Jeremy Clinec8e8cd52018-07-27 22:43:01 +000093#include <linux/nospec.h>
Paolo Abeni8c3c4472019-05-03 17:01:39 +020094#include <linux/indirect_call_wrapper.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080096#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070097#include <asm/unistd.h>
98
99#include <net/compat.h>
David S. Miller87de87d2008-06-03 09:14:03 -0700100#include <net/wext.h>
Herbert Xuf8451722010-05-24 00:12:34 -0700101#include <net/cls_cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103#include <net/sock.h>
104#include <linux/netfilter.h>
105
Arnd Bergmann6b960182009-11-06 23:10:54 -0800106#include <linux/if_tun.h>
107#include <linux/ipv6_route.h>
108#include <linux/route.h>
Arnd Bergmann6b960182009-11-06 23:10:54 -0800109#include <linux/sockios.h>
Eliezer Tamir076bb0c2013-07-10 17:13:17 +0300110#include <net/busy_poll.h>
Willem de Bruijnf24b9be2014-08-04 22:11:45 -0400111#include <linux/errqueue.h>
Eliezer Tamir06021292013-06-10 11:39:50 +0300112
Paolo Abeni8c3c4472019-05-03 17:01:39 +0200113/* proto_ops for ipv4 and ipv6 use the same {recv,send}msg function */
114#if IS_ENABLED(CONFIG_INET)
115#define INDIRECT_CALL_INET4(f, f1, ...) INDIRECT_CALL_1(f, f1, __VA_ARGS__)
116#else
117#define INDIRECT_CALL_INET4(f, f1, ...) f(__VA_ARGS__)
118#endif
119
Cong Wange0d10952013-08-01 11:10:25 +0800120#ifdef CONFIG_NET_RX_BUSY_POLL
Eliezer Tamir64b0dc52013-07-10 17:13:36 +0300121unsigned int sysctl_net_busy_read __read_mostly;
122unsigned int sysctl_net_busy_poll __read_mostly;
Eliezer Tamir06021292013-06-10 11:39:50 +0300123#endif
Arnd Bergmann6b960182009-11-06 23:10:54 -0800124
Al Viro8ae5e0302014-11-28 19:40:50 -0500125static ssize_t sock_read_iter(struct kiocb *iocb, struct iov_iter *to);
126static ssize_t sock_write_iter(struct kiocb *iocb, struct iov_iter *from);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700127static int sock_mmap(struct file *file, struct vm_area_struct *vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129static int sock_close(struct inode *inode, struct file *file);
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700130static __poll_t sock_poll(struct file *file,
131 struct poll_table_struct *wait);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700132static long sock_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
Shaun Pereira89bbfc92006-03-21 23:58:08 -0800133#ifdef CONFIG_COMPAT
134static long compat_sock_ioctl(struct file *file,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700135 unsigned int cmd, unsigned long arg);
Shaun Pereira89bbfc92006-03-21 23:58:08 -0800136#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137static int sock_fasync(int fd, struct file *filp, int on);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138static ssize_t sock_sendpage(struct file *file, struct page *page,
139 int offset, size_t size, loff_t *ppos, int more);
Jens Axboe9c55e012007-11-06 23:30:13 -0800140static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700141 struct pipe_inode_info *pipe, size_t len,
Jens Axboe9c55e012007-11-06 23:30:13 -0800142 unsigned int flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144/*
145 * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
146 * in the operation structures but are done directly via the socketcall() multiplexor.
147 */
148
Arjan van de Venda7071d2007-02-12 00:55:36 -0800149static const struct file_operations socket_file_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 .owner = THIS_MODULE,
151 .llseek = no_llseek,
Al Viro8ae5e0302014-11-28 19:40:50 -0500152 .read_iter = sock_read_iter,
153 .write_iter = sock_write_iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 .poll = sock_poll,
155 .unlocked_ioctl = sock_ioctl,
Shaun Pereira89bbfc92006-03-21 23:58:08 -0800156#ifdef CONFIG_COMPAT
157 .compat_ioctl = compat_sock_ioctl,
158#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 .mmap = sock_mmap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 .release = sock_close,
161 .fasync = sock_fasync,
Jens Axboe5274f052006-03-30 15:15:30 +0200162 .sendpage = sock_sendpage,
163 .splice_write = generic_splice_sendpage,
Jens Axboe9c55e012007-11-06 23:30:13 -0800164 .splice_read = sock_splice_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165};
166
167/*
168 * The protocol list. Each protocol is registered in here.
169 */
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171static DEFINE_SPINLOCK(net_family_lock);
Eric Dumazet190683a2010-11-10 10:50:44 +0000172static const struct net_proto_family __rcu *net_families[NPROTO] __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700175 * Support routines.
176 * Move socket addresses back and forth across the kernel/user
177 * divide and look after the messy bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 */
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180/**
181 * move_addr_to_kernel - copy a socket address into kernel space
182 * @uaddr: Address in user space
183 * @kaddr: Address in kernel space
184 * @ulen: Length in user space
185 *
186 * The address is copied into kernel space. If the provided address is
187 * too long an error code of -EINVAL is returned. If the copy gives
188 * invalid addresses -EFAULT is returned. On a success 0 is returned.
189 */
190
Maciej Żenczykowski43db3622012-03-11 12:51:50 +0000191int move_addr_to_kernel(void __user *uaddr, int ulen, struct sockaddr_storage *kaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -0700193 if (ulen < 0 || ulen > sizeof(struct sockaddr_storage))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700195 if (ulen == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 return 0;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700197 if (copy_from_user(kaddr, uaddr, ulen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 return -EFAULT;
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100199 return audit_sockaddr(ulen, kaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
202/**
203 * move_addr_to_user - copy an address to user space
204 * @kaddr: kernel space address
205 * @klen: length of address in kernel
206 * @uaddr: user space address
207 * @ulen: pointer to user length field
208 *
209 * The value pointed to by ulen on entry is the buffer length available.
210 * This is overwritten with the buffer space used. -EINVAL is returned
211 * if an overlong buffer is specified or a negative buffer size. -EFAULT
212 * is returned if either the buffer or the length field are not
213 * accessible.
214 * After copying the data up to the limit the user specifies, the true
215 * length of the data is written over the length limit the user
216 * specified. Zero is returned for a success.
217 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700218
Maciej Żenczykowski43db3622012-03-11 12:51:50 +0000219static int move_addr_to_user(struct sockaddr_storage *kaddr, int klen,
stephen hemminger11165f12010-10-18 14:27:29 +0000220 void __user *uaddr, int __user *ulen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
222 int err;
223 int len;
224
Hannes Frederic Sowa68c6beb2013-11-21 03:14:34 +0100225 BUG_ON(klen > sizeof(struct sockaddr_storage));
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700226 err = get_user(len, ulen);
227 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 return err;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700229 if (len > klen)
230 len = klen;
Hannes Frederic Sowa68c6beb2013-11-21 03:14:34 +0100231 if (len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700233 if (len) {
Steve Grubbd6fe3942006-03-30 12:20:22 -0500234 if (audit_sockaddr(klen, kaddr))
235 return -ENOMEM;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700236 if (copy_to_user(uaddr, kaddr, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 return -EFAULT;
238 }
239 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700240 * "fromlen shall refer to the value before truncation.."
241 * 1003.1g
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 */
243 return __put_user(klen, ulen);
244}
245
Alexey Dobriyan08009a72018-02-24 21:20:33 +0300246static struct kmem_cache *sock_inode_cachep __ro_after_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248static struct inode *sock_alloc_inode(struct super_block *sb)
249{
250 struct socket_alloc *ei;
Eric Dumazeteaefd1102011-02-18 03:26:36 +0000251 struct socket_wq *wq;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700252
Christoph Lametere94b1762006-12-06 20:33:17 -0800253 ei = kmem_cache_alloc(sock_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 if (!ei)
255 return NULL;
Eric Dumazeteaefd1102011-02-18 03:26:36 +0000256 wq = kmalloc(sizeof(*wq), GFP_KERNEL);
257 if (!wq) {
Eric Dumazet43815482010-04-29 11:01:49 +0000258 kmem_cache_free(sock_inode_cachep, ei);
259 return NULL;
260 }
Eric Dumazeteaefd1102011-02-18 03:26:36 +0000261 init_waitqueue_head(&wq->wait);
262 wq->fasync_list = NULL;
Nicolai Stange574aab12015-12-29 13:29:55 +0100263 wq->flags = 0;
Christoph Hellwige6476c22018-07-30 09:45:07 +0200264 ei->socket.wq = wq;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 ei->socket.state = SS_UNCONNECTED;
267 ei->socket.flags = 0;
268 ei->socket.ops = NULL;
269 ei->socket.sk = NULL;
270 ei->socket.file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 return &ei->vfs_inode;
273}
274
275static void sock_destroy_inode(struct inode *inode)
276{
Eric Dumazet43815482010-04-29 11:01:49 +0000277 struct socket_alloc *ei;
278
279 ei = container_of(inode, struct socket_alloc, vfs_inode);
Christoph Hellwige6476c22018-07-30 09:45:07 +0200280 kfree_rcu(ei->socket.wq, rcu);
Eric Dumazet43815482010-04-29 11:01:49 +0000281 kmem_cache_free(sock_inode_cachep, ei);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282}
283
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700284static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700286 struct socket_alloc *ei = (struct socket_alloc *)foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Christoph Lametera35afb82007-05-16 22:10:57 -0700288 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289}
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700290
yuan linyu1e911632017-01-07 17:18:31 +0800291static void init_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
293 sock_inode_cachep = kmem_cache_create("sock_inode_cache",
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700294 sizeof(struct socket_alloc),
295 0,
296 (SLAB_HWCACHE_ALIGN |
297 SLAB_RECLAIM_ACCOUNT |
Vladimir Davydov5d097052016-01-14 15:18:21 -0800298 SLAB_MEM_SPREAD | SLAB_ACCOUNT),
Paul Mundt20c2df82007-07-20 10:11:58 +0900299 init_once);
yuan linyu1e911632017-01-07 17:18:31 +0800300 BUG_ON(sock_inode_cachep == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}
302
Alexey Dobriyanb87221d2009-09-21 17:01:09 -0700303static const struct super_operations sockfs_ops = {
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700304 .alloc_inode = sock_alloc_inode,
305 .destroy_inode = sock_destroy_inode,
306 .statfs = simple_statfs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307};
308
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700309/*
310 * sockfs_dname() is called from d_path().
311 */
312static char *sockfs_dname(struct dentry *dentry, char *buffer, int buflen)
313{
314 return dynamic_dname(dentry, buffer, buflen, "socket:[%lu]",
David Howellsc5ef6032015-03-17 22:26:16 +0000315 d_inode(dentry)->i_ino);
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700316}
317
Al Viro3ba13d12009-02-20 06:02:22 +0000318static const struct dentry_operations sockfs_dentry_operations = {
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700319 .d_dname = sockfs_dname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320};
321
Andreas Gruenbacherbba0bd32016-09-29 17:48:35 +0200322static int sockfs_xattr_get(const struct xattr_handler *handler,
323 struct dentry *dentry, struct inode *inode,
324 const char *suffix, void *value, size_t size)
325{
326 if (value) {
327 if (dentry->d_name.len + 1 > size)
328 return -ERANGE;
329 memcpy(value, dentry->d_name.name, dentry->d_name.len + 1);
330 }
331 return dentry->d_name.len + 1;
332}
333
334#define XATTR_SOCKPROTONAME_SUFFIX "sockprotoname"
335#define XATTR_NAME_SOCKPROTONAME (XATTR_SYSTEM_PREFIX XATTR_SOCKPROTONAME_SUFFIX)
336#define XATTR_NAME_SOCKPROTONAME_LEN (sizeof(XATTR_NAME_SOCKPROTONAME)-1)
337
338static const struct xattr_handler sockfs_xattr_handler = {
339 .name = XATTR_NAME_SOCKPROTONAME,
340 .get = sockfs_xattr_get,
341};
342
Andreas Gruenbacher4a590152016-11-13 21:23:34 +0100343static int sockfs_security_xattr_set(const struct xattr_handler *handler,
344 struct dentry *dentry, struct inode *inode,
345 const char *suffix, const void *value,
346 size_t size, int flags)
347{
348 /* Handled by LSM. */
349 return -EAGAIN;
350}
351
352static const struct xattr_handler sockfs_security_xattr_handler = {
353 .prefix = XATTR_SECURITY_PREFIX,
354 .set = sockfs_security_xattr_set,
355};
356
Andreas Gruenbacherbba0bd32016-09-29 17:48:35 +0200357static const struct xattr_handler *sockfs_xattr_handlers[] = {
358 &sockfs_xattr_handler,
Andreas Gruenbacher4a590152016-11-13 21:23:34 +0100359 &sockfs_security_xattr_handler,
Andreas Gruenbacherbba0bd32016-09-29 17:48:35 +0200360 NULL
361};
362
David Howellsfba9be42019-03-25 16:38:24 +0000363static int sockfs_init_fs_context(struct fs_context *fc)
Al Viroc74a1cb2011-01-12 16:59:34 -0500364{
David Howellsfba9be42019-03-25 16:38:24 +0000365 struct pseudo_fs_context *ctx = init_pseudo(fc, SOCKFS_MAGIC);
366 if (!ctx)
367 return -ENOMEM;
368 ctx->ops = &sockfs_ops;
369 ctx->dops = &sockfs_dentry_operations;
370 ctx->xattr = sockfs_xattr_handlers;
371 return 0;
Al Viroc74a1cb2011-01-12 16:59:34 -0500372}
373
374static struct vfsmount *sock_mnt __read_mostly;
375
376static struct file_system_type sock_fs_type = {
377 .name = "sockfs",
David Howellsfba9be42019-03-25 16:38:24 +0000378 .init_fs_context = sockfs_init_fs_context,
Al Viroc74a1cb2011-01-12 16:59:34 -0500379 .kill_sb = kill_anon_super,
380};
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382/*
383 * Obtains the first available file descriptor and sets it up for use.
384 *
David S. Miller39d8c1b2006-03-20 17:13:49 -0800385 * These functions create file structures and maps them to fd space
386 * of the current process. On success it returns file descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 * and file struct implicitly stored in sock->file.
388 * Note that another thread may close file descriptor before we return
389 * from this function. We use the fact that now we do not refer
390 * to socket after mapping. If one day we will need it, this
391 * function will increment ref. count on file by 1.
392 *
393 * In any case returned fd MAY BE not valid!
394 * This race condition is unavoidable
395 * with shared fd spaces, we cannot solve it inside kernel,
396 * but we take care of internal coherence yet.
397 */
398
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300399/**
400 * sock_alloc_file - Bind a &socket to a &file
401 * @sock: socket
402 * @flags: file status flags
403 * @dname: protocol name
404 *
405 * Returns the &file bound with @sock, implicitly storing it
406 * in sock->file. If dname is %NULL, sets to "".
407 * On failure the return is a ERR pointer (see linux/err.h).
408 * This function uses GFP_KERNEL internally.
409 */
410
Linus Torvaldsaab174f2012-10-02 20:25:04 -0700411struct file *sock_alloc_file(struct socket *sock, int flags, const char *dname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
Al Viro7cbe66b2009-08-05 19:59:08 +0400413 struct file *file;
David S. Miller39d8c1b2006-03-20 17:13:49 -0800414
Al Virod93aa9d2018-06-09 09:40:05 -0400415 if (!dname)
416 dname = sock->sk ? sock->sk->sk_prot_creator->name : "";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Al Virod93aa9d2018-06-09 09:40:05 -0400418 file = alloc_file_pseudo(SOCK_INODE(sock), sock_mnt, dname,
419 O_RDWR | (flags & O_NONBLOCK),
420 &socket_file_ops);
Viresh Kumarb5ffe632015-08-12 15:59:47 +0530421 if (IS_ERR(file)) {
Al Viro8e1611e2017-12-05 23:29:09 +0000422 sock_release(sock);
Anatol Pomozov39b65252012-09-12 20:11:55 -0700423 return file;
Al Virocc3808f2009-08-06 09:43:59 +0400424 }
David S. Miller39d8c1b2006-03-20 17:13:49 -0800425
426 sock->file = file;
David S. Miller39d8c1b2006-03-20 17:13:49 -0800427 file->private_data = sock;
Al Viro28407632012-08-17 23:54:15 -0400428 return file;
David S. Miller39d8c1b2006-03-20 17:13:49 -0800429}
Al Viro56b31d12012-08-18 00:25:51 -0400430EXPORT_SYMBOL(sock_alloc_file);
David S. Miller39d8c1b2006-03-20 17:13:49 -0800431
Al Viro56b31d12012-08-18 00:25:51 -0400432static int sock_map_fd(struct socket *sock, int flags)
David S. Miller39d8c1b2006-03-20 17:13:49 -0800433{
434 struct file *newfile;
Al Viro28407632012-08-17 23:54:15 -0400435 int fd = get_unused_fd_flags(flags);
Al Viroce4bb042018-01-10 18:47:05 -0500436 if (unlikely(fd < 0)) {
437 sock_release(sock);
Al Viro28407632012-08-17 23:54:15 -0400438 return fd;
Al Viroce4bb042018-01-10 18:47:05 -0500439 }
David S. Miller39d8c1b2006-03-20 17:13:49 -0800440
Linus Torvaldsaab174f2012-10-02 20:25:04 -0700441 newfile = sock_alloc_file(sock, flags, NULL);
Al Viro28407632012-08-17 23:54:15 -0400442 if (likely(!IS_ERR(newfile))) {
David S. Miller39d8c1b2006-03-20 17:13:49 -0800443 fd_install(fd, newfile);
Al Viro28407632012-08-17 23:54:15 -0400444 return fd;
445 }
Al Viro7cbe66b2009-08-05 19:59:08 +0400446
Al Viro28407632012-08-17 23:54:15 -0400447 put_unused_fd(fd);
448 return PTR_ERR(newfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449}
450
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300451/**
452 * sock_from_file - Return the &socket bounded to @file.
453 * @file: file
454 * @err: pointer to an error code return
455 *
456 * On failure returns %NULL and assigns -ENOTSOCK to @err.
457 */
458
John Fastabend406a3c62012-07-20 10:39:25 +0000459struct socket *sock_from_file(struct file *file, int *err)
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800460{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800461 if (file->f_op == &socket_file_ops)
462 return file->private_data; /* set in sock_map_fd */
463
Eric Dumazet23bb80d2007-02-08 14:59:57 -0800464 *err = -ENOTSOCK;
465 return NULL;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800466}
John Fastabend406a3c62012-07-20 10:39:25 +0000467EXPORT_SYMBOL(sock_from_file);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469/**
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700470 * sockfd_lookup - Go from a file number to its socket slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 * @fd: file handle
472 * @err: pointer to an error code return
473 *
474 * The file handle passed in is locked and the socket it is bound
Rosen, Rami241c4662017-05-21 22:12:38 +0300475 * to is returned. If an error occurs the err pointer is overwritten
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 * with a negative errno code and NULL is returned. The function checks
477 * for both invalid handles and passing a handle which is not a socket.
478 *
479 * On a success the socket object pointer is returned.
480 */
481
482struct socket *sockfd_lookup(int fd, int *err)
483{
484 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 struct socket *sock;
486
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700487 file = fget(fd);
488 if (!file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 *err = -EBADF;
490 return NULL;
491 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700492
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800493 sock = sock_from_file(file, err);
494 if (!sock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 fput(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 return sock;
497}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700498EXPORT_SYMBOL(sockfd_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800500static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed)
501{
Al Viro00e188e2014-03-03 23:48:18 -0500502 struct fd f = fdget(fd);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800503 struct socket *sock;
504
Hua Zhong36725582006-04-19 15:25:02 -0700505 *err = -EBADF;
Al Viro00e188e2014-03-03 23:48:18 -0500506 if (f.file) {
507 sock = sock_from_file(f.file, err);
508 if (likely(sock)) {
509 *fput_needed = f.flags;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800510 return sock;
Al Viro00e188e2014-03-03 23:48:18 -0500511 }
512 fdput(f);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800513 }
514 return NULL;
515}
516
Masatake YAMATO600e1772012-08-29 10:44:29 +0000517static ssize_t sockfs_listxattr(struct dentry *dentry, char *buffer,
518 size_t size)
519{
520 ssize_t len;
521 ssize_t used = 0;
522
David Howellsc5ef6032015-03-17 22:26:16 +0000523 len = security_inode_listsecurity(d_inode(dentry), buffer, size);
Masatake YAMATO600e1772012-08-29 10:44:29 +0000524 if (len < 0)
525 return len;
526 used += len;
527 if (buffer) {
528 if (size < used)
529 return -ERANGE;
530 buffer += len;
531 }
532
533 len = (XATTR_NAME_SOCKPROTONAME_LEN + 1);
534 used += len;
535 if (buffer) {
536 if (size < used)
537 return -ERANGE;
538 memcpy(buffer, XATTR_NAME_SOCKPROTONAME, len);
539 buffer += len;
540 }
541
542 return used;
543}
544
Tobias Klauserdc647ec2017-01-10 09:30:51 +0100545static int sockfs_setattr(struct dentry *dentry, struct iattr *iattr)
Lorenzo Colitti86741ec2016-11-04 02:23:41 +0900546{
547 int err = simple_setattr(dentry, iattr);
548
Eric Biggerse1a3a602016-12-30 17:42:32 -0600549 if (!err && (iattr->ia_valid & ATTR_UID)) {
Lorenzo Colitti86741ec2016-11-04 02:23:41 +0900550 struct socket *sock = SOCKET_I(d_inode(dentry));
551
Cong Wang6d8c50d2018-06-07 13:39:49 -0700552 if (sock->sk)
553 sock->sk->sk_uid = iattr->ia_uid;
554 else
555 err = -ENOENT;
Lorenzo Colitti86741ec2016-11-04 02:23:41 +0900556 }
557
558 return err;
559}
560
Masatake YAMATO600e1772012-08-29 10:44:29 +0000561static const struct inode_operations sockfs_inode_ops = {
Masatake YAMATO600e1772012-08-29 10:44:29 +0000562 .listxattr = sockfs_listxattr,
Lorenzo Colitti86741ec2016-11-04 02:23:41 +0900563 .setattr = sockfs_setattr,
Masatake YAMATO600e1772012-08-29 10:44:29 +0000564};
565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566/**
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300567 * sock_alloc - allocate a socket
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700568 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 * Allocate a new inode and socket object. The two are bound together
570 * and initialised. The socket is then returned. If we are out of inodes
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300571 * NULL is returned. This functions uses GFP_KERNEL internally.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 */
573
Tom Herbertf4a00aa2016-03-07 14:11:01 -0800574struct socket *sock_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700576 struct inode *inode;
577 struct socket *sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Eric Dumazeta209dfc2011-07-26 11:36:34 +0200579 inode = new_inode_pseudo(sock_mnt->mnt_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 if (!inode)
581 return NULL;
582
583 sock = SOCKET_I(inode);
584
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400585 inode->i_ino = get_next_ino();
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700586 inode->i_mode = S_IFSOCK | S_IRWXUGO;
David Howells8192b0c2008-11-14 10:39:10 +1100587 inode->i_uid = current_fsuid();
588 inode->i_gid = current_fsgid();
Masatake YAMATO600e1772012-08-29 10:44:29 +0000589 inode->i_op = &sockfs_inode_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 return sock;
592}
Tom Herbertf4a00aa2016-03-07 14:11:01 -0800593EXPORT_SYMBOL(sock_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595/**
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300596 * sock_release - close a socket
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 * @sock: socket to close
598 *
599 * The socket is released from the protocol stack if it has a release
600 * callback, and the inode is then released if the socket is bound to
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700601 * an inode not a file.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700603
Cong Wang6d8c50d2018-06-07 13:39:49 -0700604static void __sock_release(struct socket *sock, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
606 if (sock->ops) {
607 struct module *owner = sock->ops->owner;
608
Cong Wang6d8c50d2018-06-07 13:39:49 -0700609 if (inode)
610 inode_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 sock->ops->release(sock);
Eric Biggersff7b11a2019-02-21 14:13:56 -0800612 sock->sk = NULL;
Cong Wang6d8c50d2018-06-07 13:39:49 -0700613 if (inode)
614 inode_unlock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 sock->ops = NULL;
616 module_put(owner);
617 }
618
Christoph Hellwige6476c22018-07-30 09:45:07 +0200619 if (sock->wq->fasync_list)
Yang Yingliang3410f222014-02-12 17:09:55 +0800620 pr_err("%s: fasync list not empty!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 if (!sock->file) {
623 iput(SOCK_INODE(sock));
624 return;
625 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700626 sock->file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627}
Cong Wang6d8c50d2018-06-07 13:39:49 -0700628
629void sock_release(struct socket *sock)
630{
631 __sock_release(sock, NULL);
632}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700633EXPORT_SYMBOL(sock_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -0400635void __sock_tx_timestamp(__u16 tsflags, __u8 *tx_flags)
Patrick Ohly20d49472009-02-12 05:03:38 +0000636{
Eric Dumazet140c55d2014-08-06 11:49:29 +0200637 u8 flags = *tx_flags;
638
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -0400639 if (tsflags & SOF_TIMESTAMPING_TX_HARDWARE)
Eric Dumazet140c55d2014-08-06 11:49:29 +0200640 flags |= SKBTX_HW_TSTAMP;
641
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -0400642 if (tsflags & SOF_TIMESTAMPING_TX_SOFTWARE)
Eric Dumazet140c55d2014-08-06 11:49:29 +0200643 flags |= SKBTX_SW_TSTAMP;
644
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -0400645 if (tsflags & SOF_TIMESTAMPING_TX_SCHED)
Eric Dumazet140c55d2014-08-06 11:49:29 +0200646 flags |= SKBTX_SCHED_TSTAMP;
647
Eric Dumazet140c55d2014-08-06 11:49:29 +0200648 *tx_flags = flags;
Patrick Ohly20d49472009-02-12 05:03:38 +0000649}
Willem de Bruijn67cc0d42014-09-08 19:58:58 -0400650EXPORT_SYMBOL(__sock_tx_timestamp);
Patrick Ohly20d49472009-02-12 05:03:38 +0000651
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300652/**
653 * sock_sendmsg - send a message through @sock
654 * @sock: socket
655 * @msg: message to send
656 *
657 * Sends @msg through @sock, passing through LSM.
658 * Returns the number of bytes sent, or an error code.
659 */
Paolo Abeni8c3c4472019-05-03 17:01:39 +0200660INDIRECT_CALLABLE_DECLARE(int inet_sendmsg(struct socket *, struct msghdr *,
661 size_t));
Al Virod8725c82014-12-11 00:02:50 -0500662static inline int sock_sendmsg_nosec(struct socket *sock, struct msghdr *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
Paolo Abeni8c3c4472019-05-03 17:01:39 +0200664 int ret = INDIRECT_CALL_INET4(sock->ops->sendmsg, inet_sendmsg, sock,
665 msg, msg_data_left(msg));
Al Virod8725c82014-12-11 00:02:50 -0500666 BUG_ON(ret == -EIOCBQUEUED);
667 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668}
Gu Zheng0cf00c62014-12-05 15:14:23 +0800669
Al Virod8725c82014-12-11 00:02:50 -0500670int sock_sendmsg(struct socket *sock, struct msghdr *msg)
Gu Zheng0cf00c62014-12-05 15:14:23 +0800671{
Al Virod8725c82014-12-11 00:02:50 -0500672 int err = security_socket_sendmsg(sock, msg,
Al Viro01e97e62014-12-15 21:39:31 -0500673 msg_data_left(msg));
Ying Xue1b784142015-03-02 15:37:48 +0800674
Al Virod8725c82014-12-11 00:02:50 -0500675 return err ?: sock_sendmsg_nosec(sock, msg);
Gu Zheng0cf00c62014-12-05 15:14:23 +0800676}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700677EXPORT_SYMBOL(sock_sendmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300679/**
680 * kernel_sendmsg - send a message through @sock (kernel-space)
681 * @sock: socket
682 * @msg: message header
683 * @vec: kernel vec
684 * @num: vec array length
685 * @size: total message data size
686 *
687 * Builds the message data with @vec and sends it through @sock.
688 * Returns the number of bytes sent, or an error code.
689 */
690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
692 struct kvec *vec, size_t num, size_t size)
693{
David Howellsaa563d72018-10-20 00:57:56 +0100694 iov_iter_kvec(&msg->msg_iter, WRITE, vec, num, size);
Al Virod8725c82014-12-11 00:02:50 -0500695 return sock_sendmsg(sock, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700697EXPORT_SYMBOL(kernel_sendmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300699/**
700 * kernel_sendmsg_locked - send a message through @sock (kernel-space)
701 * @sk: sock
702 * @msg: message header
703 * @vec: output s/g array
704 * @num: output s/g array length
705 * @size: total message data size
706 *
707 * Builds the message data with @vec and sends it through @sock.
708 * Returns the number of bytes sent, or an error code.
709 * Caller must hold @sk.
710 */
711
Tom Herbert306b13e2017-07-28 16:22:41 -0700712int kernel_sendmsg_locked(struct sock *sk, struct msghdr *msg,
713 struct kvec *vec, size_t num, size_t size)
714{
715 struct socket *sock = sk->sk_socket;
716
717 if (!sock->ops->sendmsg_locked)
John Fastabenddb5980d2017-08-15 22:31:34 -0700718 return sock_no_sendmsg_locked(sk, msg, size);
Tom Herbert306b13e2017-07-28 16:22:41 -0700719
David Howellsaa563d72018-10-20 00:57:56 +0100720 iov_iter_kvec(&msg->msg_iter, WRITE, vec, num, size);
Tom Herbert306b13e2017-07-28 16:22:41 -0700721
722 return sock->ops->sendmsg_locked(sk, msg, msg_data_left(msg));
723}
724EXPORT_SYMBOL(kernel_sendmsg_locked);
725
Soheil Hassas Yeganeh8605330a2017-03-18 17:02:59 -0400726static bool skb_is_err_queue(const struct sk_buff *skb)
727{
728 /* pkt_type of skbs enqueued on the error queue are set to
729 * PACKET_OUTGOING in skb_set_err_queue(). This is only safe to do
730 * in recvmsg, since skbs received on a local socket will never
731 * have a pkt_type of PACKET_OUTGOING.
732 */
733 return skb->pkt_type == PACKET_OUTGOING;
734}
735
Miroslav Lichvarb50a5c72017-05-19 17:52:40 +0200736/* On transmit, software and hardware timestamps are returned independently.
737 * As the two skb clones share the hardware timestamp, which may be updated
738 * before the software timestamp is received, a hardware TX timestamp may be
739 * returned only if there is no software TX timestamp. Ignore false software
740 * timestamps, which may be made in the __sock_recv_timestamp() call when the
Deepa Dinamani7f1bc6e2019-02-02 07:34:46 -0800741 * option SO_TIMESTAMP_OLD(NS) is enabled on the socket, even when the skb has a
Miroslav Lichvarb50a5c72017-05-19 17:52:40 +0200742 * hardware timestamp.
743 */
744static bool skb_is_swtx_tstamp(const struct sk_buff *skb, int false_tstamp)
745{
746 return skb->tstamp && !false_tstamp && skb_is_err_queue(skb);
747}
748
Miroslav Lichvaraad9c8c2017-05-19 17:52:38 +0200749static void put_ts_pktinfo(struct msghdr *msg, struct sk_buff *skb)
750{
751 struct scm_ts_pktinfo ts_pktinfo;
752 struct net_device *orig_dev;
753
754 if (!skb_mac_header_was_set(skb))
755 return;
756
757 memset(&ts_pktinfo, 0, sizeof(ts_pktinfo));
758
759 rcu_read_lock();
760 orig_dev = dev_get_by_napi_id(skb_napi_id(skb));
761 if (orig_dev)
762 ts_pktinfo.if_index = orig_dev->ifindex;
763 rcu_read_unlock();
764
765 ts_pktinfo.pkt_length = skb->len - skb_mac_offset(skb);
766 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPING_PKTINFO,
767 sizeof(ts_pktinfo), &ts_pktinfo);
768}
769
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700770/*
771 * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
772 */
773void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
774 struct sk_buff *skb)
775{
Patrick Ohly20d49472009-02-12 05:03:38 +0000776 int need_software_tstamp = sock_flag(sk, SOCK_RCVTSTAMP);
Deepa Dinamani887feae2019-02-02 07:34:50 -0800777 int new_tstamp = sock_flag(sk, SOCK_TSTAMP_NEW);
Deepa Dinamani97184752019-02-02 07:34:51 -0800778 struct scm_timestamping_internal tss;
779
Miroslav Lichvarb50a5c72017-05-19 17:52:40 +0200780 int empty = 1, false_tstamp = 0;
Patrick Ohly20d49472009-02-12 05:03:38 +0000781 struct skb_shared_hwtstamps *shhwtstamps =
782 skb_hwtstamps(skb);
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700783
Patrick Ohly20d49472009-02-12 05:03:38 +0000784 /* Race occurred between timestamp enabling and packet
785 receiving. Fill in the current time for now. */
Miroslav Lichvarb50a5c72017-05-19 17:52:40 +0200786 if (need_software_tstamp && skb->tstamp == 0) {
Patrick Ohly20d49472009-02-12 05:03:38 +0000787 __net_timestamp(skb);
Miroslav Lichvarb50a5c72017-05-19 17:52:40 +0200788 false_tstamp = 1;
789 }
Patrick Ohly20d49472009-02-12 05:03:38 +0000790
791 if (need_software_tstamp) {
792 if (!sock_flag(sk, SOCK_RCVTSTAMPNS)) {
Deepa Dinamani887feae2019-02-02 07:34:50 -0800793 if (new_tstamp) {
794 struct __kernel_sock_timeval tv;
795
796 skb_get_new_timestamp(skb, &tv);
797 put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMP_NEW,
798 sizeof(tv), &tv);
799 } else {
800 struct __kernel_old_timeval tv;
801
802 skb_get_timestamp(skb, &tv);
803 put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMP_OLD,
804 sizeof(tv), &tv);
805 }
Patrick Ohly20d49472009-02-12 05:03:38 +0000806 } else {
Deepa Dinamani887feae2019-02-02 07:34:50 -0800807 if (new_tstamp) {
808 struct __kernel_timespec ts;
809
810 skb_get_new_timestampns(skb, &ts);
811 put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMPNS_NEW,
812 sizeof(ts), &ts);
813 } else {
814 struct timespec ts;
815
816 skb_get_timestampns(skb, &ts);
817 put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMPNS_OLD,
818 sizeof(ts), &ts);
819 }
Patrick Ohly20d49472009-02-12 05:03:38 +0000820 }
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700821 }
Patrick Ohly20d49472009-02-12 05:03:38 +0000822
Willem de Bruijnf24b9be2014-08-04 22:11:45 -0400823 memset(&tss, 0, sizeof(tss));
Willem de Bruijnc1991052014-09-03 12:01:18 -0400824 if ((sk->sk_tsflags & SOF_TIMESTAMPING_SOFTWARE) &&
Deepa Dinamani97184752019-02-02 07:34:51 -0800825 ktime_to_timespec64_cond(skb->tstamp, tss.ts + 0))
Patrick Ohly20d49472009-02-12 05:03:38 +0000826 empty = 0;
Willem de Bruijn4d276eb2014-07-25 18:01:32 -0400827 if (shhwtstamps &&
Willem de Bruijnb9f40e22014-08-04 22:11:46 -0400828 (sk->sk_tsflags & SOF_TIMESTAMPING_RAW_HARDWARE) &&
Miroslav Lichvarb50a5c72017-05-19 17:52:40 +0200829 !skb_is_swtx_tstamp(skb, false_tstamp) &&
Deepa Dinamani97184752019-02-02 07:34:51 -0800830 ktime_to_timespec64_cond(shhwtstamps->hwtstamp, tss.ts + 2)) {
Willem de Bruijn4d276eb2014-07-25 18:01:32 -0400831 empty = 0;
Miroslav Lichvaraad9c8c2017-05-19 17:52:38 +0200832 if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_PKTINFO) &&
833 !skb_is_err_queue(skb))
834 put_ts_pktinfo(msg, skb);
835 }
Francis Yan1c885802016-11-27 23:07:18 -0800836 if (!empty) {
Deepa Dinamani97184752019-02-02 07:34:51 -0800837 if (sock_flag(sk, SOCK_TSTAMP_NEW))
838 put_cmsg_scm_timestamping64(msg, &tss);
839 else
840 put_cmsg_scm_timestamping(msg, &tss);
Francis Yan1c885802016-11-27 23:07:18 -0800841
Soheil Hassas Yeganeh8605330a2017-03-18 17:02:59 -0400842 if (skb_is_err_queue(skb) && skb->len &&
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -0400843 SKB_EXT_ERR(skb)->opt_stats)
Francis Yan1c885802016-11-27 23:07:18 -0800844 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPING_OPT_STATS,
845 skb->len, skb->data);
846 }
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700847}
Arnaldo Carvalho de Melo7c81fd82007-03-10 00:39:35 -0300848EXPORT_SYMBOL_GPL(__sock_recv_timestamp);
849
Johannes Berg6e3e9392011-11-09 10:15:42 +0100850void __sock_recv_wifi_status(struct msghdr *msg, struct sock *sk,
851 struct sk_buff *skb)
852{
853 int ack;
854
855 if (!sock_flag(sk, SOCK_WIFI_STATUS))
856 return;
857 if (!skb->wifi_acked_valid)
858 return;
859
860 ack = skb->wifi_acked;
861
862 put_cmsg(msg, SOL_SOCKET, SCM_WIFI_STATUS, sizeof(ack), &ack);
863}
864EXPORT_SYMBOL_GPL(__sock_recv_wifi_status);
865
stephen hemminger11165f12010-10-18 14:27:29 +0000866static inline void sock_recv_drops(struct msghdr *msg, struct sock *sk,
867 struct sk_buff *skb)
Neil Horman3b885782009-10-12 13:26:31 -0700868{
Eyal Birger744d5a32015-03-01 14:58:31 +0200869 if (sock_flag(sk, SOCK_RXQ_OVFL) && skb && SOCK_SKB_CB(skb)->dropcount)
Neil Horman3b885782009-10-12 13:26:31 -0700870 put_cmsg(msg, SOL_SOCKET, SO_RXQ_OVFL,
Eyal Birger744d5a32015-03-01 14:58:31 +0200871 sizeof(__u32), &SOCK_SKB_CB(skb)->dropcount);
Neil Horman3b885782009-10-12 13:26:31 -0700872}
873
Eric Dumazet767dd032010-04-28 19:14:43 +0000874void __sock_recv_ts_and_drops(struct msghdr *msg, struct sock *sk,
Neil Horman3b885782009-10-12 13:26:31 -0700875 struct sk_buff *skb)
876{
877 sock_recv_timestamp(msg, sk, skb);
878 sock_recv_drops(msg, sk, skb);
879}
Eric Dumazet767dd032010-04-28 19:14:43 +0000880EXPORT_SYMBOL_GPL(__sock_recv_ts_and_drops);
Neil Horman3b885782009-10-12 13:26:31 -0700881
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300882/**
883 * sock_recvmsg - receive a message from @sock
884 * @sock: socket
885 * @msg: message to receive
886 * @flags: message flags
887 *
888 * Receives @msg from @sock, passing through LSM. Returns the total number
889 * of bytes received, or an error.
890 */
Paolo Abeni8c3c4472019-05-03 17:01:39 +0200891INDIRECT_CALLABLE_DECLARE(int inet_recvmsg(struct socket *, struct msghdr *,
892 size_t , int ));
Ying Xue1b784142015-03-02 15:37:48 +0800893static inline int sock_recvmsg_nosec(struct socket *sock, struct msghdr *msg,
Al Viro2da62902015-03-14 21:13:46 -0400894 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
Paolo Abeni8c3c4472019-05-03 17:01:39 +0200896 return INDIRECT_CALL_INET4(sock->ops->recvmsg, inet_recvmsg, sock, msg,
897 msg_data_left(msg), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898}
899
Al Viro2da62902015-03-14 21:13:46 -0400900int sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -0700901{
Al Viro2da62902015-03-14 21:13:46 -0400902 int err = security_socket_recvmsg(sock, msg, msg_data_left(msg), flags);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -0700903
Al Viro2da62902015-03-14 21:13:46 -0400904 return err ?: sock_recvmsg_nosec(sock, msg, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700906EXPORT_SYMBOL(sock_recvmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Martin Lucinac1249c02010-12-10 00:04:05 +0000908/**
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300909 * kernel_recvmsg - Receive a message from a socket (kernel space)
910 * @sock: The socket to receive the message from
911 * @msg: Received message
912 * @vec: Input s/g array for message data
913 * @num: Size of input s/g array
914 * @size: Number of bytes to read
915 * @flags: Message flags (MSG_DONTWAIT, etc...)
Martin Lucinac1249c02010-12-10 00:04:05 +0000916 *
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300917 * On return the msg structure contains the scatter/gather array passed in the
918 * vec argument. The array is modified so that it consists of the unfilled
919 * portion of the original array.
Martin Lucinac1249c02010-12-10 00:04:05 +0000920 *
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300921 * The returned value is the total number of bytes received, or an error.
Martin Lucinac1249c02010-12-10 00:04:05 +0000922 */
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300923
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700924int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
925 struct kvec *vec, size_t num, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926{
927 mm_segment_t oldfs = get_fs();
928 int result;
929
David Howellsaa563d72018-10-20 00:57:56 +0100930 iov_iter_kvec(&msg->msg_iter, READ, vec, num, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 set_fs(KERNEL_DS);
Al Viro2da62902015-03-14 21:13:46 -0400932 result = sock_recvmsg(sock, msg, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 set_fs(oldfs);
934 return result;
935}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700936EXPORT_SYMBOL(kernel_recvmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -0300938static ssize_t sock_sendpage(struct file *file, struct page *page,
939 int offset, size_t size, loff_t *ppos, int more)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940{
941 struct socket *sock;
942 int flags;
943
Eric Dumazetb69aee02005-09-06 14:42:45 -0700944 sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Eric Dumazet35f9c092012-04-05 03:05:35 +0000946 flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
947 /* more is a combination of MSG_MORE and MSG_SENDPAGE_NOTLAST */
948 flags |= more;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Linus Torvaldse6949582009-08-13 08:28:36 -0700950 return kernel_sendpage(sock, page, offset, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951}
952
Jens Axboe9c55e012007-11-06 23:30:13 -0800953static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700954 struct pipe_inode_info *pipe, size_t len,
Jens Axboe9c55e012007-11-06 23:30:13 -0800955 unsigned int flags)
956{
957 struct socket *sock = file->private_data;
958
Rémi Denis-Courmont997b37d2008-02-15 02:35:45 -0800959 if (unlikely(!sock->ops->splice_read))
Slavomir Kaslev95506582018-11-16 11:27:53 +0200960 return generic_file_splice_read(file, ppos, pipe, len, flags);
Rémi Denis-Courmont997b37d2008-02-15 02:35:45 -0800961
Jens Axboe9c55e012007-11-06 23:30:13 -0800962 return sock->ops->splice_read(sock, ppos, pipe, len, flags);
963}
964
Al Viro8ae5e0302014-11-28 19:40:50 -0500965static ssize_t sock_read_iter(struct kiocb *iocb, struct iov_iter *to)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800966{
Al Viro6d652332015-01-30 16:12:56 -0500967 struct file *file = iocb->ki_filp;
968 struct socket *sock = file->private_data;
tadeusz.struk@intel.com0345f932015-03-19 12:31:25 -0700969 struct msghdr msg = {.msg_iter = *to,
970 .msg_iocb = iocb};
Al Viro8ae5e0302014-11-28 19:40:50 -0500971 ssize_t res;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800972
Al Viro8ae5e0302014-11-28 19:40:50 -0500973 if (file->f_flags & O_NONBLOCK)
974 msg.msg_flags = MSG_DONTWAIT;
975
976 if (iocb->ki_pos != 0)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800977 return -ESPIPE;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700978
Christoph Hellwig66ee59a2015-02-11 19:56:46 +0100979 if (!iov_iter_count(to)) /* Match SYS5 behaviour */
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800980 return 0;
981
Al Viro2da62902015-03-14 21:13:46 -0400982 res = sock_recvmsg(sock, &msg, msg.msg_flags);
Al Viro8ae5e0302014-11-28 19:40:50 -0500983 *to = msg.msg_iter;
984 return res;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800985}
986
Al Viro8ae5e0302014-11-28 19:40:50 -0500987static ssize_t sock_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988{
Al Viro6d652332015-01-30 16:12:56 -0500989 struct file *file = iocb->ki_filp;
990 struct socket *sock = file->private_data;
tadeusz.struk@intel.com0345f932015-03-19 12:31:25 -0700991 struct msghdr msg = {.msg_iter = *from,
992 .msg_iocb = iocb};
Al Viro8ae5e0302014-11-28 19:40:50 -0500993 ssize_t res;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800994
Al Viro8ae5e0302014-11-28 19:40:50 -0500995 if (iocb->ki_pos != 0)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800996 return -ESPIPE;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700997
Al Viro8ae5e0302014-11-28 19:40:50 -0500998 if (file->f_flags & O_NONBLOCK)
999 msg.msg_flags = MSG_DONTWAIT;
1000
Al Viro6d652332015-01-30 16:12:56 -05001001 if (sock->type == SOCK_SEQPACKET)
1002 msg.msg_flags |= MSG_EOR;
1003
Al Virod8725c82014-12-11 00:02:50 -05001004 res = sock_sendmsg(sock, &msg);
Al Viro8ae5e0302014-11-28 19:40:50 -05001005 *from = msg.msg_iter;
1006 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007}
1008
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009/*
1010 * Atomic setting of ioctl hooks to avoid race
1011 * with module unload.
1012 */
1013
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001014static DEFINE_MUTEX(br_ioctl_mutex);
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001015static int (*br_ioctl_hook) (struct net *, unsigned int cmd, void __user *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
Eric W. Biederman881d9662007-09-17 11:56:21 -07001017void brioctl_set(int (*hook) (struct net *, unsigned int, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001019 mutex_lock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 br_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001021 mutex_unlock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022}
1023EXPORT_SYMBOL(brioctl_set);
1024
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001025static DEFINE_MUTEX(vlan_ioctl_mutex);
Eric W. Biederman881d9662007-09-17 11:56:21 -07001026static int (*vlan_ioctl_hook) (struct net *, void __user *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
Eric W. Biederman881d9662007-09-17 11:56:21 -07001028void vlan_ioctl_set(int (*hook) (struct net *, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001030 mutex_lock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 vlan_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001032 mutex_unlock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033}
1034EXPORT_SYMBOL(vlan_ioctl_set);
1035
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001036static DEFINE_MUTEX(dlci_ioctl_mutex);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001037static int (*dlci_ioctl_hook) (unsigned int, void __user *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001039void dlci_ioctl_set(int (*hook) (unsigned int, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001041 mutex_lock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 dlci_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001043 mutex_unlock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044}
1045EXPORT_SYMBOL(dlci_ioctl_set);
1046
Arnd Bergmann6b960182009-11-06 23:10:54 -08001047static long sock_do_ioctl(struct net *net, struct socket *sock,
Johannes Berg63ff03a2019-01-25 22:43:17 +01001048 unsigned int cmd, unsigned long arg)
Arnd Bergmann6b960182009-11-06 23:10:54 -08001049{
1050 int err;
1051 void __user *argp = (void __user *)arg;
1052
1053 err = sock->ops->ioctl(sock, cmd, arg);
1054
1055 /*
1056 * If this ioctl is unknown try to hand it down
1057 * to the NIC driver.
1058 */
Al Viro36fd6332017-06-26 13:19:16 -04001059 if (err != -ENOIOCTLCMD)
1060 return err;
Arnd Bergmann6b960182009-11-06 23:10:54 -08001061
Al Viro36fd6332017-06-26 13:19:16 -04001062 if (cmd == SIOCGIFCONF) {
1063 struct ifconf ifc;
1064 if (copy_from_user(&ifc, argp, sizeof(struct ifconf)))
1065 return -EFAULT;
1066 rtnl_lock();
1067 err = dev_ifconf(net, &ifc, sizeof(struct ifreq));
1068 rtnl_unlock();
1069 if (!err && copy_to_user(argp, &ifc, sizeof(struct ifconf)))
1070 err = -EFAULT;
Al Viro44c02a22017-10-05 12:59:44 -04001071 } else {
1072 struct ifreq ifr;
1073 bool need_copyout;
Johannes Berg63ff03a2019-01-25 22:43:17 +01001074 if (copy_from_user(&ifr, argp, sizeof(struct ifreq)))
Al Viro44c02a22017-10-05 12:59:44 -04001075 return -EFAULT;
1076 err = dev_ioctl(net, cmd, &ifr, &need_copyout);
1077 if (!err && need_copyout)
Johannes Berg63ff03a2019-01-25 22:43:17 +01001078 if (copy_to_user(argp, &ifr, sizeof(struct ifreq)))
Al Viro44c02a22017-10-05 12:59:44 -04001079 return -EFAULT;
Al Viro36fd6332017-06-26 13:19:16 -04001080 }
Arnd Bergmann6b960182009-11-06 23:10:54 -08001081 return err;
1082}
1083
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084/*
1085 * With an ioctl, arg may well be a user mode pointer, but we don't know
1086 * what to do with it - that's up to the protocol still.
1087 */
1088
Pedro Tammela8a3c2452019-03-14 10:45:23 -03001089/**
1090 * get_net_ns - increment the refcount of the network namespace
1091 * @ns: common namespace (net)
1092 *
1093 * Returns the net's common namespace.
1094 */
1095
Kirill Tkhaid8d211a2018-02-14 16:39:56 +03001096struct ns_common *get_net_ns(struct ns_common *ns)
Andrey Vaginc62cce22016-10-24 18:29:13 -07001097{
1098 return &get_net(container_of(ns, struct net, ns))->ns;
1099}
Kirill Tkhaid8d211a2018-02-14 16:39:56 +03001100EXPORT_SYMBOL_GPL(get_net_ns);
Andrey Vaginc62cce22016-10-24 18:29:13 -07001101
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
1103{
1104 struct socket *sock;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001105 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 void __user *argp = (void __user *)arg;
1107 int pid, err;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001108 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Eric Dumazetb69aee02005-09-06 14:42:45 -07001110 sock = file->private_data;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001111 sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001112 net = sock_net(sk);
Al Viro44c02a22017-10-05 12:59:44 -04001113 if (unlikely(cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15))) {
1114 struct ifreq ifr;
1115 bool need_copyout;
1116 if (copy_from_user(&ifr, argp, sizeof(struct ifreq)))
1117 return -EFAULT;
1118 err = dev_ioctl(net, cmd, &ifr, &need_copyout);
1119 if (!err && need_copyout)
1120 if (copy_to_user(argp, &ifr, sizeof(struct ifreq)))
1121 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 } else
Johannes Berg3d23e342009-09-29 23:27:28 +02001123#ifdef CONFIG_WEXT_CORE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
Al Virob1b0c242017-10-01 20:13:08 -04001125 err = wext_handle_ioctl(net, cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 } else
Johannes Berg3d23e342009-09-29 23:27:28 +02001127#endif
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001128 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 case FIOSETOWN:
1130 case SIOCSPGRP:
1131 err = -EFAULT;
1132 if (get_user(pid, (int __user *)argp))
1133 break;
Jiri Slaby393cc3f2017-06-13 13:35:50 +02001134 err = f_setown(sock->file, pid, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 break;
1136 case FIOGETOWN:
1137 case SIOCGPGRP:
Eric W. Biederman609d7fa2006-10-02 02:17:15 -07001138 err = put_user(f_getown(sock->file),
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001139 (int __user *)argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 break;
1141 case SIOCGIFBR:
1142 case SIOCSIFBR:
1143 case SIOCBRADDBR:
1144 case SIOCBRDELBR:
1145 err = -ENOPKG;
1146 if (!br_ioctl_hook)
1147 request_module("bridge");
1148
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001149 mutex_lock(&br_ioctl_mutex);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001150 if (br_ioctl_hook)
Eric W. Biederman881d9662007-09-17 11:56:21 -07001151 err = br_ioctl_hook(net, cmd, argp);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001152 mutex_unlock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 break;
1154 case SIOCGIFVLAN:
1155 case SIOCSIFVLAN:
1156 err = -ENOPKG;
1157 if (!vlan_ioctl_hook)
1158 request_module("8021q");
1159
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001160 mutex_lock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 if (vlan_ioctl_hook)
Eric W. Biederman881d9662007-09-17 11:56:21 -07001162 err = vlan_ioctl_hook(net, argp);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001163 mutex_unlock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 case SIOCADDDLCI:
1166 case SIOCDELDLCI:
1167 err = -ENOPKG;
1168 if (!dlci_ioctl_hook)
1169 request_module("dlci");
1170
Pavel Emelyanov7512cbf2008-03-21 15:58:52 -07001171 mutex_lock(&dlci_ioctl_mutex);
1172 if (dlci_ioctl_hook)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 err = dlci_ioctl_hook(cmd, argp);
Pavel Emelyanov7512cbf2008-03-21 15:58:52 -07001174 mutex_unlock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 break;
Andrey Vaginc62cce22016-10-24 18:29:13 -07001176 case SIOCGSKNS:
1177 err = -EPERM;
1178 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1179 break;
1180
1181 err = open_related_ns(&net->ns, get_net_ns);
1182 break;
Arnd Bergmann0768e172019-04-17 22:56:11 +02001183 case SIOCGSTAMP_OLD:
1184 case SIOCGSTAMPNS_OLD:
Arnd Bergmannc7cbdbf2019-04-17 22:51:48 +02001185 if (!sock->ops->gettstamp) {
1186 err = -ENOIOCTLCMD;
1187 break;
1188 }
1189 err = sock->ops->gettstamp(sock, argp,
Arnd Bergmann0768e172019-04-17 22:56:11 +02001190 cmd == SIOCGSTAMP_OLD,
1191 !IS_ENABLED(CONFIG_64BIT));
Gustavo A. R. Silva60747822019-04-24 10:31:24 -05001192 break;
Arnd Bergmann0768e172019-04-17 22:56:11 +02001193 case SIOCGSTAMP_NEW:
1194 case SIOCGSTAMPNS_NEW:
1195 if (!sock->ops->gettstamp) {
1196 err = -ENOIOCTLCMD;
1197 break;
1198 }
1199 err = sock->ops->gettstamp(sock, argp,
1200 cmd == SIOCGSTAMP_NEW,
1201 false);
Arnd Bergmannc7cbdbf2019-04-17 22:51:48 +02001202 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 default:
Johannes Berg63ff03a2019-01-25 22:43:17 +01001204 err = sock_do_ioctl(net, sock, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 break;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 return err;
1208}
1209
Pedro Tammela8a3c2452019-03-14 10:45:23 -03001210/**
1211 * sock_create_lite - creates a socket
1212 * @family: protocol family (AF_INET, ...)
1213 * @type: communication type (SOCK_STREAM, ...)
1214 * @protocol: protocol (0, ...)
1215 * @res: new socket
1216 *
1217 * Creates a new socket and assigns it to @res, passing through LSM.
1218 * The new socket initialization is not complete, see kernel_accept().
1219 * Returns 0 or an error. On failure @res is set to %NULL.
1220 * This function internally uses GFP_KERNEL.
1221 */
1222
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223int sock_create_lite(int family, int type, int protocol, struct socket **res)
1224{
1225 int err;
1226 struct socket *sock = NULL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001227
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 err = security_socket_create(family, type, protocol, 1);
1229 if (err)
1230 goto out;
1231
1232 sock = sock_alloc();
1233 if (!sock) {
1234 err = -ENOMEM;
1235 goto out;
1236 }
1237
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 sock->type = type;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001239 err = security_socket_post_create(sock, family, type, protocol, 1);
1240 if (err)
1241 goto out_release;
1242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243out:
1244 *res = sock;
1245 return err;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001246out_release:
1247 sock_release(sock);
1248 sock = NULL;
1249 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001251EXPORT_SYMBOL(sock_create_lite);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
1253/* No kernel lock held - perfect */
Al Viroade994f2017-07-03 00:01:49 -04001254static __poll_t sock_poll(struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255{
Christoph Hellwig3cafb372018-01-09 16:07:34 +01001256 struct socket *sock = file->private_data;
Christoph Hellwiga331de32018-07-30 09:42:13 +02001257 __poll_t events = poll_requested_events(wait), flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Christoph Hellwige88958e2018-06-29 15:37:24 +02001259 if (!sock->ops->poll)
1260 return 0;
Christoph Hellwigf641f13b2018-07-30 09:42:12 +02001261
Christoph Hellwiga331de32018-07-30 09:42:13 +02001262 if (sk_can_busy_loop(sock->sk)) {
1263 /* poll once if requested by the syscall */
1264 if (events & POLL_BUSY_LOOP)
1265 sk_busy_loop(sock->sk, 1);
1266
1267 /* if this socket can poll_ll, tell the system call */
1268 flag = POLL_BUSY_LOOP;
1269 }
1270
1271 return sock->ops->poll(file, sock, wait) | flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272}
1273
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001274static int sock_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275{
Eric Dumazetb69aee02005-09-06 14:42:45 -07001276 struct socket *sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
1278 return sock->ops->mmap(file, sock, vma);
1279}
1280
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001281static int sock_close(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282{
Cong Wang6d8c50d2018-06-07 13:39:49 -07001283 __sock_release(SOCKET_I(inode), inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 return 0;
1285}
1286
1287/*
1288 * Update the socket async list
1289 *
1290 * Fasync_list locking strategy.
1291 *
1292 * 1. fasync_list is modified only under process context socket lock
1293 * i.e. under semaphore.
1294 * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
Eric Dumazet989a2972010-04-14 09:55:35 +00001295 * or under socket lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 */
1297
1298static int sock_fasync(int fd, struct file *filp, int on)
1299{
Eric Dumazet989a2972010-04-14 09:55:35 +00001300 struct socket *sock = filp->private_data;
1301 struct sock *sk = sock->sk;
Eric Dumazeteaefd1102011-02-18 03:26:36 +00001302 struct socket_wq *wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
Eric Dumazet989a2972010-04-14 09:55:35 +00001304 if (sk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
1307 lock_sock(sk);
Christoph Hellwige6476c22018-07-30 09:45:07 +02001308 wq = sock->wq;
Eric Dumazeteaefd1102011-02-18 03:26:36 +00001309 fasync_helper(fd, filp, on, &wq->fasync_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
Eric Dumazeteaefd1102011-02-18 03:26:36 +00001311 if (!wq->fasync_list)
Eric Dumazet989a2972010-04-14 09:55:35 +00001312 sock_reset_flag(sk, SOCK_FASYNC);
Jonathan Corbet76398422009-02-01 14:26:59 -07001313 else
Eric Dumazetbcdce712009-10-06 17:28:29 -07001314 sock_set_flag(sk, SOCK_FASYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
Eric Dumazet989a2972010-04-14 09:55:35 +00001316 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 return 0;
1318}
1319
Eric Dumazetceb5d582015-11-29 20:03:11 -08001320/* This function may be called only under rcu_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
Eric Dumazetceb5d582015-11-29 20:03:11 -08001322int sock_wake_async(struct socket_wq *wq, int how, int band)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323{
Eric Dumazetceb5d582015-11-29 20:03:11 -08001324 if (!wq || !wq->fasync_list)
1325 return -1;
Eric Dumazet43815482010-04-29 11:01:49 +00001326
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001327 switch (how) {
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001328 case SOCK_WAKE_WAITD:
Eric Dumazetceb5d582015-11-29 20:03:11 -08001329 if (test_bit(SOCKWQ_ASYNC_WAITDATA, &wq->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 break;
1331 goto call_kill;
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001332 case SOCK_WAKE_SPACE:
Eric Dumazetceb5d582015-11-29 20:03:11 -08001333 if (!test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 break;
1335 /* fall through */
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001336 case SOCK_WAKE_IO:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001337call_kill:
Eric Dumazet43815482010-04-29 11:01:49 +00001338 kill_fasync(&wq->fasync_list, SIGIO, band);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 break;
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001340 case SOCK_WAKE_URG:
Eric Dumazet43815482010-04-29 11:01:49 +00001341 kill_fasync(&wq->fasync_list, SIGURG, band);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 }
Eric Dumazetceb5d582015-11-29 20:03:11 -08001343
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 return 0;
1345}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001346EXPORT_SYMBOL(sock_wake_async);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
Pedro Tammela8a3c2452019-03-14 10:45:23 -03001348/**
1349 * __sock_create - creates a socket
1350 * @net: net namespace
1351 * @family: protocol family (AF_INET, ...)
1352 * @type: communication type (SOCK_STREAM, ...)
1353 * @protocol: protocol (0, ...)
1354 * @res: new socket
1355 * @kern: boolean for kernel space sockets
1356 *
1357 * Creates a new socket and assigns it to @res, passing through LSM.
1358 * Returns 0 or an error. On failure @res is set to %NULL. @kern must
1359 * be set to true if the socket resides in kernel space.
1360 * This function internally uses GFP_KERNEL.
1361 */
1362
Pavel Emelyanov721db932010-09-29 16:06:32 +04001363int __sock_create(struct net *net, int family, int type, int protocol,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001364 struct socket **res, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365{
1366 int err;
1367 struct socket *sock;
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001368 const struct net_proto_family *pf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
1370 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001371 * Check protocol is in range
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 */
1373 if (family < 0 || family >= NPROTO)
1374 return -EAFNOSUPPORT;
1375 if (type < 0 || type >= SOCK_MAX)
1376 return -EINVAL;
1377
1378 /* Compatibility.
1379
1380 This uglymoron is moved from INET layer to here to avoid
1381 deadlock in module load.
1382 */
1383 if (family == PF_INET && type == SOCK_PACKET) {
liping.zhangf3c98692016-03-11 23:08:36 +08001384 pr_info_once("%s uses obsolete (PF_INET,SOCK_PACKET)\n",
1385 current->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 family = PF_PACKET;
1387 }
1388
1389 err = security_socket_create(family, type, protocol, kern);
1390 if (err)
1391 return err;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001392
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001393 /*
1394 * Allocate the socket and allow the family to set things up. if
1395 * the protocol is 0, the family is instructed to select an appropriate
1396 * default.
1397 */
1398 sock = sock_alloc();
1399 if (!sock) {
Joe Perchese87cc472012-05-13 21:56:26 +00001400 net_warn_ratelimited("socket: no more sockets\n");
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001401 return -ENFILE; /* Not exactly a match, but its the
1402 closest posix thing */
1403 }
1404
1405 sock->type = type;
1406
Johannes Berg95a5afc2008-10-16 15:24:51 -07001407#ifdef CONFIG_MODULES
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001408 /* Attempt to load a protocol module if the find failed.
1409 *
1410 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 * requested real, full-featured networking support upon configuration.
1412 * Otherwise module support will break!
1413 */
Eric Dumazet190683a2010-11-10 10:50:44 +00001414 if (rcu_access_pointer(net_families[family]) == NULL)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001415 request_module("net-pf-%d", family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416#endif
1417
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001418 rcu_read_lock();
1419 pf = rcu_dereference(net_families[family]);
1420 err = -EAFNOSUPPORT;
1421 if (!pf)
1422 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
1424 /*
1425 * We will call the ->create function, that possibly is in a loadable
1426 * module, so we have to bump that loadable module refcnt first.
1427 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001428 if (!try_module_get(pf->owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 goto out_release;
1430
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001431 /* Now protected by module ref count */
1432 rcu_read_unlock();
1433
Eric Paris3f378b62009-11-05 22:18:14 -08001434 err = pf->create(net, sock, protocol, kern);
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001435 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 goto out_module_put;
Frank Filza79af592005-09-27 15:23:38 -07001437
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 /*
1439 * Now to bump the refcnt of the [loadable] module that owns this
1440 * socket at sock_release time we decrement its refcnt.
1441 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001442 if (!try_module_get(sock->ops->owner))
1443 goto out_module_busy;
1444
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 /*
1446 * Now that we're done with the ->create function, the [loadable]
1447 * module can have its refcnt decremented
1448 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001449 module_put(pf->owner);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001450 err = security_socket_post_create(sock, family, type, protocol, kern);
1451 if (err)
Herbert Xu3b185522007-08-15 14:46:02 -07001452 goto out_sock_release;
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001453 *res = sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001455 return 0;
1456
1457out_module_busy:
1458 err = -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459out_module_put:
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001460 sock->ops = NULL;
1461 module_put(pf->owner);
1462out_sock_release:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 sock_release(sock);
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001464 return err;
1465
1466out_release:
1467 rcu_read_unlock();
1468 goto out_sock_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469}
Pavel Emelyanov721db932010-09-29 16:06:32 +04001470EXPORT_SYMBOL(__sock_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
Pedro Tammela8a3c2452019-03-14 10:45:23 -03001472/**
1473 * sock_create - creates a socket
1474 * @family: protocol family (AF_INET, ...)
1475 * @type: communication type (SOCK_STREAM, ...)
1476 * @protocol: protocol (0, ...)
1477 * @res: new socket
1478 *
1479 * A wrapper around __sock_create().
1480 * Returns 0 or an error. This function internally uses GFP_KERNEL.
1481 */
1482
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483int sock_create(int family, int type, int protocol, struct socket **res)
1484{
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001485 return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001487EXPORT_SYMBOL(sock_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Pedro Tammela8a3c2452019-03-14 10:45:23 -03001489/**
1490 * sock_create_kern - creates a socket (kernel space)
1491 * @net: net namespace
1492 * @family: protocol family (AF_INET, ...)
1493 * @type: communication type (SOCK_STREAM, ...)
1494 * @protocol: protocol (0, ...)
1495 * @res: new socket
1496 *
1497 * A wrapper around __sock_create().
1498 * Returns 0 or an error. This function internally uses GFP_KERNEL.
1499 */
1500
Eric W. Biedermaneeb1bd52015-05-08 21:08:05 -05001501int sock_create_kern(struct net *net, int family, int type, int protocol, struct socket **res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502{
Eric W. Biedermaneeb1bd52015-05-08 21:08:05 -05001503 return __sock_create(net, family, type, protocol, res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001505EXPORT_SYMBOL(sock_create_kern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
Dominik Brodowski9d6a15c2018-03-13 19:29:43 +01001507int __sys_socket(int family, int type, int protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508{
1509 int retval;
1510 struct socket *sock;
Ulrich Dreppera677a032008-07-23 21:29:17 -07001511 int flags;
1512
Ulrich Dreppere38b36f2008-07-23 21:29:42 -07001513 /* Check the SOCK_* constants for consistency. */
1514 BUILD_BUG_ON(SOCK_CLOEXEC != O_CLOEXEC);
1515 BUILD_BUG_ON((SOCK_MAX | SOCK_TYPE_MASK) != SOCK_TYPE_MASK);
1516 BUILD_BUG_ON(SOCK_CLOEXEC & SOCK_TYPE_MASK);
1517 BUILD_BUG_ON(SOCK_NONBLOCK & SOCK_TYPE_MASK);
1518
Ulrich Dreppera677a032008-07-23 21:29:17 -07001519 flags = type & ~SOCK_TYPE_MASK;
Ulrich Drepper77d27202008-07-23 21:29:35 -07001520 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Dreppera677a032008-07-23 21:29:17 -07001521 return -EINVAL;
1522 type &= SOCK_TYPE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001524 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1525 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1526
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 retval = sock_create(family, type, protocol, &sock);
1528 if (retval < 0)
Al Viro8e1611e2017-12-05 23:29:09 +00001529 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
Al Viro8e1611e2017-12-05 23:29:09 +00001531 return sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532}
1533
Dominik Brodowski9d6a15c2018-03-13 19:29:43 +01001534SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol)
1535{
1536 return __sys_socket(family, type, protocol);
1537}
1538
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539/*
1540 * Create a pair of connected sockets.
1541 */
1542
Dominik Brodowski6debc8d2018-03-13 19:49:23 +01001543int __sys_socketpair(int family, int type, int protocol, int __user *usockvec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544{
1545 struct socket *sock1, *sock2;
1546 int fd1, fd2, err;
Al Virodb349502007-02-07 01:48:00 -05001547 struct file *newfile1, *newfile2;
Ulrich Dreppera677a032008-07-23 21:29:17 -07001548 int flags;
1549
1550 flags = type & ~SOCK_TYPE_MASK;
Ulrich Drepper77d27202008-07-23 21:29:35 -07001551 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Dreppera677a032008-07-23 21:29:17 -07001552 return -EINVAL;
1553 type &= SOCK_TYPE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001555 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1556 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1557
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 /*
Al Viro016a2662017-12-05 23:28:38 +00001559 * reserve descriptors and make sure we won't fail
1560 * to return them to userland.
1561 */
1562 fd1 = get_unused_fd_flags(flags);
1563 if (unlikely(fd1 < 0))
1564 return fd1;
1565
1566 fd2 = get_unused_fd_flags(flags);
1567 if (unlikely(fd2 < 0)) {
1568 put_unused_fd(fd1);
1569 return fd2;
1570 }
1571
1572 err = put_user(fd1, &usockvec[0]);
1573 if (err)
1574 goto out;
1575
1576 err = put_user(fd2, &usockvec[1]);
1577 if (err)
1578 goto out;
1579
1580 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 * Obtain the first socket and check if the underlying protocol
1582 * supports the socketpair call.
1583 */
1584
1585 err = sock_create(family, type, protocol, &sock1);
Al Viro016a2662017-12-05 23:28:38 +00001586 if (unlikely(err < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 goto out;
1588
1589 err = sock_create(family, type, protocol, &sock2);
Al Viro016a2662017-12-05 23:28:38 +00001590 if (unlikely(err < 0)) {
1591 sock_release(sock1);
1592 goto out;
David S. Millerbf3c23d2007-10-29 21:54:02 -07001593 }
Yann Droneaudd73aa282013-12-09 22:42:20 +01001594
David Herrmannd47cd942018-05-04 16:28:20 +02001595 err = security_socket_socketpair(sock1, sock2);
1596 if (unlikely(err)) {
1597 sock_release(sock2);
1598 sock_release(sock1);
1599 goto out;
1600 }
1601
Al Viro016a2662017-12-05 23:28:38 +00001602 err = sock1->ops->socketpair(sock1, sock2);
1603 if (unlikely(err < 0)) {
1604 sock_release(sock2);
1605 sock_release(sock1);
1606 goto out;
Al Viro28407632012-08-17 23:54:15 -04001607 }
1608
Linus Torvaldsaab174f2012-10-02 20:25:04 -07001609 newfile1 = sock_alloc_file(sock1, flags, NULL);
Viresh Kumarb5ffe632015-08-12 15:59:47 +05301610 if (IS_ERR(newfile1)) {
Al Viro28407632012-08-17 23:54:15 -04001611 err = PTR_ERR(newfile1);
Al Viro016a2662017-12-05 23:28:38 +00001612 sock_release(sock2);
1613 goto out;
Al Viro28407632012-08-17 23:54:15 -04001614 }
1615
Linus Torvaldsaab174f2012-10-02 20:25:04 -07001616 newfile2 = sock_alloc_file(sock2, flags, NULL);
Al Viro28407632012-08-17 23:54:15 -04001617 if (IS_ERR(newfile2)) {
1618 err = PTR_ERR(newfile2);
Al Viro016a2662017-12-05 23:28:38 +00001619 fput(newfile1);
1620 goto out;
Al Virodb349502007-02-07 01:48:00 -05001621 }
1622
Al Viro157cf642008-12-14 04:57:47 -05001623 audit_fd_pair(fd1, fd2);
Yann Droneaudd73aa282013-12-09 22:42:20 +01001624
Al Virodb349502007-02-07 01:48:00 -05001625 fd_install(fd1, newfile1);
1626 fd_install(fd2, newfile2);
Yann Droneaudd73aa282013-12-09 22:42:20 +01001627 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629out:
Al Viro016a2662017-12-05 23:28:38 +00001630 put_unused_fd(fd2);
1631 put_unused_fd(fd1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 return err;
1633}
1634
Dominik Brodowski6debc8d2018-03-13 19:49:23 +01001635SYSCALL_DEFINE4(socketpair, int, family, int, type, int, protocol,
1636 int __user *, usockvec)
1637{
1638 return __sys_socketpair(family, type, protocol, usockvec);
1639}
1640
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641/*
1642 * Bind a name to a socket. Nothing much to do here since it's
1643 * the protocol's responsibility to handle the local address.
1644 *
1645 * We move the socket address to kernel space before we call
1646 * the protocol layer (having also checked the address is ok).
1647 */
1648
Dominik Brodowskia87d35d2018-03-13 19:33:09 +01001649int __sys_bind(int fd, struct sockaddr __user *umyaddr, int addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650{
1651 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001652 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001653 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001655 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001656 if (sock) {
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001657 err = move_addr_to_kernel(umyaddr, addrlen, &address);
Jakub Sitnicki068b88c2018-10-04 11:09:40 +02001658 if (!err) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001659 err = security_socket_bind(sock,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001660 (struct sockaddr *)&address,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001661 addrlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001662 if (!err)
1663 err = sock->ops->bind(sock,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001664 (struct sockaddr *)
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001665 &address, addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 }
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001667 fput_light(sock->file, fput_needed);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 return err;
1670}
1671
Dominik Brodowskia87d35d2018-03-13 19:33:09 +01001672SYSCALL_DEFINE3(bind, int, fd, struct sockaddr __user *, umyaddr, int, addrlen)
1673{
1674 return __sys_bind(fd, umyaddr, addrlen);
1675}
1676
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677/*
1678 * Perform a listen. Basically, we allow the protocol to do anything
1679 * necessary for a listen, and if that works, we mark the socket as
1680 * ready for listening.
1681 */
1682
Dominik Brodowski25e290e2018-03-13 19:36:54 +01001683int __sys_listen(int fd, int backlog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684{
1685 struct socket *sock;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001686 int err, fput_needed;
Pavel Emelyanovb8e1f9b2007-12-08 00:12:33 -08001687 int somaxconn;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001688
1689 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1690 if (sock) {
Pavel Emelyanov8efa6e92008-03-31 19:41:14 -07001691 somaxconn = sock_net(sock->sk)->core.sysctl_somaxconn;
Eric Dumazet95c96172012-04-15 05:58:06 +00001692 if ((unsigned int)backlog > somaxconn)
Pavel Emelyanovb8e1f9b2007-12-08 00:12:33 -08001693 backlog = somaxconn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
1695 err = security_socket_listen(sock, backlog);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001696 if (!err)
1697 err = sock->ops->listen(sock, backlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001699 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 }
1701 return err;
1702}
1703
Dominik Brodowski25e290e2018-03-13 19:36:54 +01001704SYSCALL_DEFINE2(listen, int, fd, int, backlog)
1705{
1706 return __sys_listen(fd, backlog);
1707}
1708
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709/*
1710 * For accept, we attempt to create a new socket, set up the link
1711 * with the client, wake up the client, then return the new
1712 * connected fd. We collect the address of the connector in kernel
1713 * space and move it to user at the very end. This is unclean because
1714 * we open the socket then return an error.
1715 *
1716 * 1003.1g adds the ability to recvmsg() to query connection pending
1717 * status to recvmsg. We need to add that support in a way thats
Geert Uytterhoevenb9030362018-03-02 14:43:23 +01001718 * clean when we restructure accept also.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 */
1720
Dominik Brodowski4541e802018-03-13 19:24:23 +01001721int __sys_accept4(int fd, struct sockaddr __user *upeer_sockaddr,
1722 int __user *upeer_addrlen, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723{
1724 struct socket *sock, *newsock;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001725 struct file *newfile;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001726 int err, len, newfd, fput_needed;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001727 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
Ulrich Drepper77d27202008-07-23 21:29:35 -07001729 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001730 return -EINVAL;
1731
1732 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1733 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1734
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001735 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 if (!sock)
1737 goto out;
1738
1739 err = -ENFILE;
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001740 newsock = sock_alloc();
1741 if (!newsock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 goto out_put;
1743
1744 newsock->type = sock->type;
1745 newsock->ops = sock->ops;
1746
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 /*
1748 * We don't need try_module_get here, as the listening socket (sock)
1749 * has the protocol module (sock->ops->owner) held.
1750 */
1751 __module_get(newsock->ops->owner);
1752
Al Viro28407632012-08-17 23:54:15 -04001753 newfd = get_unused_fd_flags(flags);
David S. Miller39d8c1b2006-03-20 17:13:49 -08001754 if (unlikely(newfd < 0)) {
1755 err = newfd;
David S. Miller9a1875e2006-04-01 12:48:36 -08001756 sock_release(newsock);
1757 goto out_put;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001758 }
Linus Torvaldsaab174f2012-10-02 20:25:04 -07001759 newfile = sock_alloc_file(newsock, flags, sock->sk->sk_prot_creator->name);
Viresh Kumarb5ffe632015-08-12 15:59:47 +05301760 if (IS_ERR(newfile)) {
Al Viro28407632012-08-17 23:54:15 -04001761 err = PTR_ERR(newfile);
1762 put_unused_fd(newfd);
Al Viro28407632012-08-17 23:54:15 -04001763 goto out_put;
1764 }
David S. Miller39d8c1b2006-03-20 17:13:49 -08001765
Frank Filza79af592005-09-27 15:23:38 -07001766 err = security_socket_accept(sock, newsock);
1767 if (err)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001768 goto out_fd;
Frank Filza79af592005-09-27 15:23:38 -07001769
David Howellscdfbabf2017-03-09 08:09:05 +00001770 err = sock->ops->accept(sock, newsock, sock->file->f_flags, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 if (err < 0)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001772 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
1774 if (upeer_sockaddr) {
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001775 len = newsock->ops->getname(newsock,
1776 (struct sockaddr *)&address, 2);
1777 if (len < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 err = -ECONNABORTED;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001779 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 }
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001781 err = move_addr_to_user(&address,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001782 len, upeer_sockaddr, upeer_addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 if (err < 0)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001784 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 }
1786
1787 /* File flags are not inherited via accept() unlike another OSes. */
1788
David S. Miller39d8c1b2006-03-20 17:13:49 -08001789 fd_install(newfd, newfile);
1790 err = newfd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001793 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794out:
1795 return err;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001796out_fd:
David S. Miller9606a212006-04-01 01:00:14 -08001797 fput(newfile);
David S. Miller39d8c1b2006-03-20 17:13:49 -08001798 put_unused_fd(newfd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 goto out_put;
1800}
1801
Dominik Brodowski4541e802018-03-13 19:24:23 +01001802SYSCALL_DEFINE4(accept4, int, fd, struct sockaddr __user *, upeer_sockaddr,
1803 int __user *, upeer_addrlen, int, flags)
1804{
1805 return __sys_accept4(fd, upeer_sockaddr, upeer_addrlen, flags);
1806}
1807
Heiko Carstens20f37032009-01-14 14:14:23 +01001808SYSCALL_DEFINE3(accept, int, fd, struct sockaddr __user *, upeer_sockaddr,
1809 int __user *, upeer_addrlen)
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001810{
Dominik Brodowski4541e802018-03-13 19:24:23 +01001811 return __sys_accept4(fd, upeer_sockaddr, upeer_addrlen, 0);
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001812}
1813
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814/*
1815 * Attempt to connect to a socket with the server address. The address
1816 * is in user space so we verify it is OK and move it to kernel space.
1817 *
1818 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
1819 * break bindings
1820 *
1821 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
1822 * other SEQPACKET protocols that take time to connect() as it doesn't
1823 * include the -EINPROGRESS status for such sockets.
1824 */
1825
Dominik Brodowski1387c2c2018-03-13 19:35:09 +01001826int __sys_connect(int fd, struct sockaddr __user *uservaddr, int addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827{
1828 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001829 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001830 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001832 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 if (!sock)
1834 goto out;
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001835 err = move_addr_to_kernel(uservaddr, addrlen, &address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 if (err < 0)
1837 goto out_put;
1838
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001839 err =
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001840 security_socket_connect(sock, (struct sockaddr *)&address, addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 if (err)
1842 goto out_put;
1843
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001844 err = sock->ops->connect(sock, (struct sockaddr *)&address, addrlen,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 sock->file->f_flags);
1846out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001847 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848out:
1849 return err;
1850}
1851
Dominik Brodowski1387c2c2018-03-13 19:35:09 +01001852SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
1853 int, addrlen)
1854{
1855 return __sys_connect(fd, uservaddr, addrlen);
1856}
1857
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858/*
1859 * Get the local address ('name') of a socket object. Move the obtained
1860 * name to user space.
1861 */
1862
Dominik Brodowski8882a102018-03-13 19:43:14 +01001863int __sys_getsockname(int fd, struct sockaddr __user *usockaddr,
1864 int __user *usockaddr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865{
1866 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001867 struct sockaddr_storage address;
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001868 int err, fput_needed;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001869
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001870 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 if (!sock)
1872 goto out;
1873
1874 err = security_socket_getsockname(sock);
1875 if (err)
1876 goto out_put;
1877
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001878 err = sock->ops->getname(sock, (struct sockaddr *)&address, 0);
1879 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 goto out_put;
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001881 /* "err" is actually length in this case */
1882 err = move_addr_to_user(&address, err, usockaddr, usockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
1884out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001885 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886out:
1887 return err;
1888}
1889
Dominik Brodowski8882a102018-03-13 19:43:14 +01001890SYSCALL_DEFINE3(getsockname, int, fd, struct sockaddr __user *, usockaddr,
1891 int __user *, usockaddr_len)
1892{
1893 return __sys_getsockname(fd, usockaddr, usockaddr_len);
1894}
1895
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896/*
1897 * Get the remote address ('name') of a socket object. Move the obtained
1898 * name to user space.
1899 */
1900
Dominik Brodowskib21c8f82018-03-13 19:47:00 +01001901int __sys_getpeername(int fd, struct sockaddr __user *usockaddr,
1902 int __user *usockaddr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903{
1904 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001905 struct sockaddr_storage address;
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001906 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001908 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1909 if (sock != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 err = security_socket_getpeername(sock);
1911 if (err) {
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001912 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 return err;
1914 }
1915
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001916 err = sock->ops->getname(sock, (struct sockaddr *)&address, 1);
1917 if (err >= 0)
1918 /* "err" is actually length in this case */
1919 err = move_addr_to_user(&address, err, usockaddr,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001920 usockaddr_len);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001921 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 }
1923 return err;
1924}
1925
Dominik Brodowskib21c8f82018-03-13 19:47:00 +01001926SYSCALL_DEFINE3(getpeername, int, fd, struct sockaddr __user *, usockaddr,
1927 int __user *, usockaddr_len)
1928{
1929 return __sys_getpeername(fd, usockaddr, usockaddr_len);
1930}
1931
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932/*
1933 * Send a datagram to a given address. We move the address into kernel
1934 * space and check the user space data area is readable before invoking
1935 * the protocol.
1936 */
Dominik Brodowski211b6342018-03-13 19:18:52 +01001937int __sys_sendto(int fd, void __user *buff, size_t len, unsigned int flags,
1938 struct sockaddr __user *addr, int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939{
1940 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001941 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 int err;
1943 struct msghdr msg;
1944 struct iovec iov;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001945 int fput_needed;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001946
Al Viro602bd0e2015-03-21 19:12:32 -04001947 err = import_single_range(WRITE, buff, len, &iov, &msg.msg_iter);
1948 if (unlikely(err))
1949 return err;
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001950 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1951 if (!sock)
David S. Miller4387ff72007-02-08 15:06:08 -08001952 goto out;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001953
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001954 msg.msg_name = NULL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001955 msg.msg_control = NULL;
1956 msg.msg_controllen = 0;
1957 msg.msg_namelen = 0;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001958 if (addr) {
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001959 err = move_addr_to_kernel(addr, addr_len, &address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 if (err < 0)
1961 goto out_put;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001962 msg.msg_name = (struct sockaddr *)&address;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001963 msg.msg_namelen = addr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 }
1965 if (sock->file->f_flags & O_NONBLOCK)
1966 flags |= MSG_DONTWAIT;
1967 msg.msg_flags = flags;
Al Virod8725c82014-12-11 00:02:50 -05001968 err = sock_sendmsg(sock, &msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001970out_put:
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001971 fput_light(sock->file, fput_needed);
David S. Miller4387ff72007-02-08 15:06:08 -08001972out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 return err;
1974}
1975
Dominik Brodowski211b6342018-03-13 19:18:52 +01001976SYSCALL_DEFINE6(sendto, int, fd, void __user *, buff, size_t, len,
1977 unsigned int, flags, struct sockaddr __user *, addr,
1978 int, addr_len)
1979{
1980 return __sys_sendto(fd, buff, len, flags, addr, addr_len);
1981}
1982
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001984 * Send a datagram down a socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 */
1986
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001987SYSCALL_DEFINE4(send, int, fd, void __user *, buff, size_t, len,
Eric Dumazet95c96172012-04-15 05:58:06 +00001988 unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989{
Dominik Brodowski211b6342018-03-13 19:18:52 +01001990 return __sys_sendto(fd, buff, len, flags, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991}
1992
1993/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001994 * Receive a frame from the socket and optionally record the address of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 * sender. We verify the buffers are writable and if needed move the
1996 * sender address from kernel to user space.
1997 */
Dominik Brodowski7a09e1e2018-03-13 19:10:06 +01001998int __sys_recvfrom(int fd, void __user *ubuf, size_t size, unsigned int flags,
1999 struct sockaddr __user *addr, int __user *addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000{
2001 struct socket *sock;
2002 struct iovec iov;
2003 struct msghdr msg;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07002004 struct sockaddr_storage address;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002005 int err, err2;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002006 int fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
Al Viro602bd0e2015-03-21 19:12:32 -04002008 err = import_single_range(READ, ubuf, size, &iov, &msg.msg_iter);
2009 if (unlikely(err))
2010 return err;
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08002011 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 if (!sock)
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08002013 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002015 msg.msg_control = NULL;
2016 msg.msg_controllen = 0;
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01002017 /* Save some cycles and don't copy the address if not needed */
2018 msg.msg_name = addr ? (struct sockaddr *)&address : NULL;
2019 /* We assume all kernel code knows the size of sockaddr_storage */
2020 msg.msg_namelen = 0;
tadeusz.struk@intel.com130ed5d2015-12-15 10:46:17 -08002021 msg.msg_iocb = NULL;
Alexander Potapenko9f138fa2017-03-08 18:08:16 +01002022 msg.msg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 if (sock->file->f_flags & O_NONBLOCK)
2024 flags |= MSG_DONTWAIT;
Al Viro2da62902015-03-14 21:13:46 -04002025 err = sock_recvmsg(sock, &msg, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002027 if (err >= 0 && addr != NULL) {
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00002028 err2 = move_addr_to_user(&address,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07002029 msg.msg_namelen, addr, addr_len);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002030 if (err2 < 0)
2031 err = err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 }
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08002033
2034 fput_light(sock->file, fput_needed);
David S. Miller4387ff72007-02-08 15:06:08 -08002035out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 return err;
2037}
2038
Dominik Brodowski7a09e1e2018-03-13 19:10:06 +01002039SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, ubuf, size_t, size,
2040 unsigned int, flags, struct sockaddr __user *, addr,
2041 int __user *, addr_len)
2042{
2043 return __sys_recvfrom(fd, ubuf, size, flags, addr, addr_len);
2044}
2045
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002047 * Receive a datagram from a socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 */
2049
Jan Glauberb7c0ddf2014-04-16 09:32:48 +02002050SYSCALL_DEFINE4(recv, int, fd, void __user *, ubuf, size_t, size,
2051 unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052{
Dominik Brodowski7a09e1e2018-03-13 19:10:06 +01002053 return __sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054}
2055
2056/*
2057 * Set a socket option. Because we don't know the option lengths we have
2058 * to pass the user mode parameter for the protocols to sort out.
2059 */
2060
Dominik Brodowskicc36dca2018-03-13 20:10:59 +01002061static int __sys_setsockopt(int fd, int level, int optname,
2062 char __user *optval, int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002064 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 struct socket *sock;
2066
2067 if (optlen < 0)
2068 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002069
2070 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2071 if (sock != NULL) {
2072 err = security_socket_setsockopt(sock, level, optname);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002073 if (err)
2074 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
2076 if (level == SOL_SOCKET)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002077 err =
2078 sock_setsockopt(sock, level, optname, optval,
2079 optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 else
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002081 err =
2082 sock->ops->setsockopt(sock, level, optname, optval,
2083 optlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002084out_put:
2085 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 }
2087 return err;
2088}
2089
Dominik Brodowskicc36dca2018-03-13 20:10:59 +01002090SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname,
2091 char __user *, optval, int, optlen)
2092{
2093 return __sys_setsockopt(fd, level, optname, optval, optlen);
2094}
2095
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096/*
2097 * Get a socket option. Because we don't know the option lengths we have
2098 * to pass a user mode parameter for the protocols to sort out.
2099 */
2100
Dominik Brodowski13a2d702018-03-13 20:15:04 +01002101static int __sys_getsockopt(int fd, int level, int optname,
2102 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002104 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 struct socket *sock;
2106
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002107 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2108 if (sock != NULL) {
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002109 err = security_socket_getsockopt(sock, level, optname);
2110 if (err)
2111 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
2113 if (level == SOL_SOCKET)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002114 err =
2115 sock_getsockopt(sock, level, optname, optval,
2116 optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 else
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002118 err =
2119 sock->ops->getsockopt(sock, level, optname, optval,
2120 optlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002121out_put:
2122 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 }
2124 return err;
2125}
2126
Dominik Brodowski13a2d702018-03-13 20:15:04 +01002127SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname,
2128 char __user *, optval, int __user *, optlen)
2129{
2130 return __sys_getsockopt(fd, level, optname, optval, optlen);
2131}
2132
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133/*
2134 * Shutdown a socket.
2135 */
2136
Dominik Brodowski005a1ae2018-03-13 20:07:05 +01002137int __sys_shutdown(int fd, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002139 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 struct socket *sock;
2141
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002142 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2143 if (sock != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 err = security_socket_shutdown(sock, how);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002145 if (!err)
2146 err = sock->ops->shutdown(sock, how);
2147 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 }
2149 return err;
2150}
2151
Dominik Brodowski005a1ae2018-03-13 20:07:05 +01002152SYSCALL_DEFINE2(shutdown, int, fd, int, how)
2153{
2154 return __sys_shutdown(fd, how);
2155}
2156
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002157/* A couple of helpful macros for getting the address of the 32/64 bit
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 * fields which are the same type (int / unsigned) on our platforms.
2159 */
2160#define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
2161#define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
2162#define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
2163
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002164struct used_address {
2165 struct sockaddr_storage name;
2166 unsigned int name_len;
2167};
2168
Al Viroda184282015-03-21 19:29:06 -04002169static int copy_msghdr_from_user(struct msghdr *kmsg,
2170 struct user_msghdr __user *umsg,
2171 struct sockaddr __user **save_addr,
2172 struct iovec **iov)
Dan Carpenter1661bf32013-10-03 00:27:20 +03002173{
Al Viroffb07552017-06-27 19:32:04 -04002174 struct user_msghdr msg;
Al Viro08adb7d2014-11-10 20:23:13 -05002175 ssize_t err;
2176
Al Viroffb07552017-06-27 19:32:04 -04002177 if (copy_from_user(&msg, umsg, sizeof(*umsg)))
Dan Carpenter1661bf32013-10-03 00:27:20 +03002178 return -EFAULT;
Matthew Leachdbb490b2014-03-11 11:58:27 +00002179
Paolo Abeni864d9662017-07-21 18:49:45 +02002180 kmsg->msg_control = (void __force *)msg.msg_control;
Al Viroffb07552017-06-27 19:32:04 -04002181 kmsg->msg_controllen = msg.msg_controllen;
2182 kmsg->msg_flags = msg.msg_flags;
2183
2184 kmsg->msg_namelen = msg.msg_namelen;
2185 if (!msg.msg_name)
Ani Sinha6a2a2b32014-09-08 14:49:59 -07002186 kmsg->msg_namelen = 0;
2187
Matthew Leachdbb490b2014-03-11 11:58:27 +00002188 if (kmsg->msg_namelen < 0)
2189 return -EINVAL;
2190
Dan Carpenter1661bf32013-10-03 00:27:20 +03002191 if (kmsg->msg_namelen > sizeof(struct sockaddr_storage))
Dan Carpenterdb31c552013-11-27 15:40:21 +03002192 kmsg->msg_namelen = sizeof(struct sockaddr_storage);
Al Viro08adb7d2014-11-10 20:23:13 -05002193
2194 if (save_addr)
Al Viroffb07552017-06-27 19:32:04 -04002195 *save_addr = msg.msg_name;
Al Viro08adb7d2014-11-10 20:23:13 -05002196
Al Viroffb07552017-06-27 19:32:04 -04002197 if (msg.msg_name && kmsg->msg_namelen) {
Al Viro08adb7d2014-11-10 20:23:13 -05002198 if (!save_addr) {
Paolo Abeni864d9662017-07-21 18:49:45 +02002199 err = move_addr_to_kernel(msg.msg_name,
2200 kmsg->msg_namelen,
Al Viro08adb7d2014-11-10 20:23:13 -05002201 kmsg->msg_name);
2202 if (err < 0)
2203 return err;
2204 }
2205 } else {
2206 kmsg->msg_name = NULL;
2207 kmsg->msg_namelen = 0;
2208 }
2209
Al Viroffb07552017-06-27 19:32:04 -04002210 if (msg.msg_iovlen > UIO_MAXIOV)
Al Viro08adb7d2014-11-10 20:23:13 -05002211 return -EMSGSIZE;
2212
tadeusz.struk@intel.com0345f932015-03-19 12:31:25 -07002213 kmsg->msg_iocb = NULL;
2214
Al Viroffb07552017-06-27 19:32:04 -04002215 return import_iovec(save_addr ? READ : WRITE,
2216 msg.msg_iov, msg.msg_iovlen,
Al Viroda184282015-03-21 19:29:06 -04002217 UIO_FASTIOV, iov, &kmsg->msg_iter);
Dan Carpenter1661bf32013-10-03 00:27:20 +03002218}
2219
Al Viro666547f2014-04-06 14:03:05 -04002220static int ___sys_sendmsg(struct socket *sock, struct user_msghdr __user *msg,
Eric Dumazet95c96172012-04-15 05:58:06 +00002221 struct msghdr *msg_sys, unsigned int flags,
Tom Herbert28a94d82016-03-07 14:11:02 -08002222 struct used_address *used_address,
2223 unsigned int allowed_msghdr_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224{
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002225 struct compat_msghdr __user *msg_compat =
2226 (struct compat_msghdr __user *)msg;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07002227 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
Alex Williamsonb9d717a2005-09-26 14:28:02 -07002229 unsigned char ctl[sizeof(struct cmsghdr) + 20]
Amit Kushwaha846cc122016-12-08 18:21:53 +05302230 __aligned(sizeof(__kernel_size_t));
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002231 /* 20 is size of ipv6_pktinfo */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 unsigned char *ctl_buf = ctl;
Al Virod8725c82014-12-11 00:02:50 -05002233 int ctl_len;
Al Viro08adb7d2014-11-10 20:23:13 -05002234 ssize_t err;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002235
Al Viro08adb7d2014-11-10 20:23:13 -05002236 msg_sys->msg_name = &address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
Al Viro08449322014-11-09 22:33:45 -05002238 if (MSG_CMSG_COMPAT & flags)
Al Viro08adb7d2014-11-10 20:23:13 -05002239 err = get_compat_msghdr(msg_sys, msg_compat, NULL, &iov);
Al Viro08449322014-11-09 22:33:45 -05002240 else
Al Viro08adb7d2014-11-10 20:23:13 -05002241 err = copy_msghdr_from_user(msg_sys, msg, NULL, &iov);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002242 if (err < 0)
Al Viroda184282015-03-21 19:29:06 -04002243 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244
2245 err = -ENOBUFS;
2246
Anton Blanchard228e5482011-05-02 20:21:35 +00002247 if (msg_sys->msg_controllen > INT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 goto out_freeiov;
Tom Herbert28a94d82016-03-07 14:11:02 -08002249 flags |= (msg_sys->msg_flags & allowed_msghdr_flags);
Anton Blanchard228e5482011-05-02 20:21:35 +00002250 ctl_len = msg_sys->msg_controllen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002252 err =
Anton Blanchard228e5482011-05-02 20:21:35 +00002253 cmsghdr_from_user_compat_to_kern(msg_sys, sock->sk, ctl,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002254 sizeof(ctl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 if (err)
2256 goto out_freeiov;
Anton Blanchard228e5482011-05-02 20:21:35 +00002257 ctl_buf = msg_sys->msg_control;
2258 ctl_len = msg_sys->msg_controllen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 } else if (ctl_len) {
David S. Millerac4340f2017-01-04 13:24:19 -05002260 BUILD_BUG_ON(sizeof(struct cmsghdr) !=
2261 CMSG_ALIGN(sizeof(struct cmsghdr)));
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002262 if (ctl_len > sizeof(ctl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002264 if (ctl_buf == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 goto out_freeiov;
2266 }
2267 err = -EFAULT;
2268 /*
Anton Blanchard228e5482011-05-02 20:21:35 +00002269 * Careful! Before this, msg_sys->msg_control contains a user pointer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
2271 * checking falls down on this.
2272 */
Namhyung Kimfb8621b2010-09-07 03:55:00 +00002273 if (copy_from_user(ctl_buf,
Anton Blanchard228e5482011-05-02 20:21:35 +00002274 (void __user __force *)msg_sys->msg_control,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002275 ctl_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 goto out_freectl;
Anton Blanchard228e5482011-05-02 20:21:35 +00002277 msg_sys->msg_control = ctl_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 }
Anton Blanchard228e5482011-05-02 20:21:35 +00002279 msg_sys->msg_flags = flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280
2281 if (sock->file->f_flags & O_NONBLOCK)
Anton Blanchard228e5482011-05-02 20:21:35 +00002282 msg_sys->msg_flags |= MSG_DONTWAIT;
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002283 /*
2284 * If this is sendmmsg() and current destination address is same as
2285 * previously succeeded address, omit asking LSM's decision.
2286 * used_address->name_len is initialized to UINT_MAX so that the first
2287 * destination address never matches.
2288 */
Mathieu Desnoyersbc909d92011-08-24 19:45:03 -07002289 if (used_address && msg_sys->msg_name &&
2290 used_address->name_len == msg_sys->msg_namelen &&
2291 !memcmp(&used_address->name, msg_sys->msg_name,
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002292 used_address->name_len)) {
Al Virod8725c82014-12-11 00:02:50 -05002293 err = sock_sendmsg_nosec(sock, msg_sys);
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002294 goto out_freectl;
2295 }
Al Virod8725c82014-12-11 00:02:50 -05002296 err = sock_sendmsg(sock, msg_sys);
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002297 /*
2298 * If this is sendmmsg() and sending to current destination address was
2299 * successful, remember it.
2300 */
2301 if (used_address && err >= 0) {
2302 used_address->name_len = msg_sys->msg_namelen;
Mathieu Desnoyersbc909d92011-08-24 19:45:03 -07002303 if (msg_sys->msg_name)
2304 memcpy(&used_address->name, msg_sys->msg_name,
2305 used_address->name_len);
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307
2308out_freectl:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002309 if (ctl_buf != ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 sock_kfree_s(sock->sk, ctl_buf, ctl_len);
2311out_freeiov:
Al Viroda184282015-03-21 19:29:06 -04002312 kfree(iov);
Anton Blanchard228e5482011-05-02 20:21:35 +00002313 return err;
2314}
2315
2316/*
2317 * BSD sendmsg interface
2318 */
2319
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002320long __sys_sendmsg(int fd, struct user_msghdr __user *msg, unsigned int flags,
2321 bool forbid_cmsg_compat)
Anton Blanchard228e5482011-05-02 20:21:35 +00002322{
2323 int fput_needed, err;
2324 struct msghdr msg_sys;
Andy Lutomirski1be374a2013-05-22 14:07:44 -07002325 struct socket *sock;
Anton Blanchard228e5482011-05-02 20:21:35 +00002326
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002327 if (forbid_cmsg_compat && (flags & MSG_CMSG_COMPAT))
2328 return -EINVAL;
2329
Andy Lutomirski1be374a2013-05-22 14:07:44 -07002330 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Anton Blanchard228e5482011-05-02 20:21:35 +00002331 if (!sock)
2332 goto out;
2333
Tom Herbert28a94d82016-03-07 14:11:02 -08002334 err = ___sys_sendmsg(sock, msg, &msg_sys, flags, NULL, 0);
Anton Blanchard228e5482011-05-02 20:21:35 +00002335
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002336 fput_light(sock->file, fput_needed);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002337out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 return err;
2339}
2340
Al Viro666547f2014-04-06 14:03:05 -04002341SYSCALL_DEFINE3(sendmsg, int, fd, struct user_msghdr __user *, msg, unsigned int, flags)
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002342{
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002343 return __sys_sendmsg(fd, msg, flags, true);
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002344}
2345
Anton Blanchard228e5482011-05-02 20:21:35 +00002346/*
2347 * Linux sendmmsg interface
2348 */
2349
2350int __sys_sendmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002351 unsigned int flags, bool forbid_cmsg_compat)
Anton Blanchard228e5482011-05-02 20:21:35 +00002352{
2353 int fput_needed, err, datagrams;
2354 struct socket *sock;
2355 struct mmsghdr __user *entry;
2356 struct compat_mmsghdr __user *compat_entry;
2357 struct msghdr msg_sys;
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002358 struct used_address used_address;
Tom Herbertf0922762016-03-07 14:11:03 -08002359 unsigned int oflags = flags;
Anton Blanchard228e5482011-05-02 20:21:35 +00002360
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002361 if (forbid_cmsg_compat && (flags & MSG_CMSG_COMPAT))
2362 return -EINVAL;
2363
Anton Blanchard98382f42011-08-04 14:07:39 +00002364 if (vlen > UIO_MAXIOV)
2365 vlen = UIO_MAXIOV;
Anton Blanchard228e5482011-05-02 20:21:35 +00002366
2367 datagrams = 0;
2368
2369 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2370 if (!sock)
2371 return err;
2372
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002373 used_address.name_len = UINT_MAX;
Anton Blanchard228e5482011-05-02 20:21:35 +00002374 entry = mmsg;
2375 compat_entry = (struct compat_mmsghdr __user *)mmsg;
Anton Blanchard728ffb82011-08-04 14:07:38 +00002376 err = 0;
Tom Herbertf0922762016-03-07 14:11:03 -08002377 flags |= MSG_BATCH;
Anton Blanchard228e5482011-05-02 20:21:35 +00002378
2379 while (datagrams < vlen) {
Tom Herbertf0922762016-03-07 14:11:03 -08002380 if (datagrams == vlen - 1)
2381 flags = oflags;
2382
Anton Blanchard228e5482011-05-02 20:21:35 +00002383 if (MSG_CMSG_COMPAT & flags) {
Al Viro666547f2014-04-06 14:03:05 -04002384 err = ___sys_sendmsg(sock, (struct user_msghdr __user *)compat_entry,
Tom Herbert28a94d82016-03-07 14:11:02 -08002385 &msg_sys, flags, &used_address, MSG_EOR);
Anton Blanchard228e5482011-05-02 20:21:35 +00002386 if (err < 0)
2387 break;
2388 err = __put_user(err, &compat_entry->msg_len);
2389 ++compat_entry;
2390 } else {
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002391 err = ___sys_sendmsg(sock,
Al Viro666547f2014-04-06 14:03:05 -04002392 (struct user_msghdr __user *)entry,
Tom Herbert28a94d82016-03-07 14:11:02 -08002393 &msg_sys, flags, &used_address, MSG_EOR);
Anton Blanchard228e5482011-05-02 20:21:35 +00002394 if (err < 0)
2395 break;
2396 err = put_user(err, &entry->msg_len);
2397 ++entry;
2398 }
2399
2400 if (err)
2401 break;
2402 ++datagrams;
Soheil Hassas Yeganeh30238982016-11-04 15:36:49 -04002403 if (msg_data_left(&msg_sys))
2404 break;
Eric Dumazeta78cb842016-01-08 08:37:20 -08002405 cond_resched();
Anton Blanchard228e5482011-05-02 20:21:35 +00002406 }
2407
Anton Blanchard228e5482011-05-02 20:21:35 +00002408 fput_light(sock->file, fput_needed);
2409
Anton Blanchard728ffb82011-08-04 14:07:38 +00002410 /* We only return an error if no datagrams were able to be sent */
2411 if (datagrams != 0)
Anton Blanchard228e5482011-05-02 20:21:35 +00002412 return datagrams;
2413
Anton Blanchard228e5482011-05-02 20:21:35 +00002414 return err;
2415}
2416
2417SYSCALL_DEFINE4(sendmmsg, int, fd, struct mmsghdr __user *, mmsg,
2418 unsigned int, vlen, unsigned int, flags)
2419{
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002420 return __sys_sendmmsg(fd, mmsg, vlen, flags, true);
Anton Blanchard228e5482011-05-02 20:21:35 +00002421}
2422
Al Viro666547f2014-04-06 14:03:05 -04002423static int ___sys_recvmsg(struct socket *sock, struct user_msghdr __user *msg,
Eric Dumazet95c96172012-04-15 05:58:06 +00002424 struct msghdr *msg_sys, unsigned int flags, int nosec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425{
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002426 struct compat_msghdr __user *msg_compat =
2427 (struct compat_msghdr __user *)msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 struct iovec iovstack[UIO_FASTIOV];
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002429 struct iovec *iov = iovstack;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 unsigned long cmsg_ptr;
Al Viro2da62902015-03-14 21:13:46 -04002431 int len;
Al Viro08adb7d2014-11-10 20:23:13 -05002432 ssize_t err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433
2434 /* kernel mode address */
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07002435 struct sockaddr_storage addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436
2437 /* user mode address pointers */
2438 struct sockaddr __user *uaddr;
Al Viro08adb7d2014-11-10 20:23:13 -05002439 int __user *uaddr_len = COMPAT_NAMELEN(msg);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002440
Al Viro08adb7d2014-11-10 20:23:13 -05002441 msg_sys->msg_name = &addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01002443 if (MSG_CMSG_COMPAT & flags)
Al Viro08adb7d2014-11-10 20:23:13 -05002444 err = get_compat_msghdr(msg_sys, msg_compat, &uaddr, &iov);
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01002445 else
Al Viro08adb7d2014-11-10 20:23:13 -05002446 err = copy_msghdr_from_user(msg_sys, msg, &uaddr, &iov);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 if (err < 0)
Al Viroda184282015-03-21 19:29:06 -04002448 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002450 cmsg_ptr = (unsigned long)msg_sys->msg_control;
2451 msg_sys->msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002452
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01002453 /* We assume all kernel code knows the size of sockaddr_storage */
2454 msg_sys->msg_namelen = 0;
2455
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 if (sock->file->f_flags & O_NONBLOCK)
2457 flags |= MSG_DONTWAIT;
Al Viro2da62902015-03-14 21:13:46 -04002458 err = (nosec ? sock_recvmsg_nosec : sock_recvmsg)(sock, msg_sys, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 if (err < 0)
2460 goto out_freeiov;
2461 len = err;
2462
2463 if (uaddr != NULL) {
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00002464 err = move_addr_to_user(&addr,
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002465 msg_sys->msg_namelen, uaddr,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002466 uaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 if (err < 0)
2468 goto out_freeiov;
2469 }
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002470 err = __put_user((msg_sys->msg_flags & ~MSG_CMSG_COMPAT),
David S. Miller37f7f422005-09-16 16:51:01 -07002471 COMPAT_FLAGS(msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 if (err)
2473 goto out_freeiov;
2474 if (MSG_CMSG_COMPAT & flags)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002475 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 &msg_compat->msg_controllen);
2477 else
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002478 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 &msg->msg_controllen);
2480 if (err)
2481 goto out_freeiov;
2482 err = len;
2483
2484out_freeiov:
Al Viroda184282015-03-21 19:29:06 -04002485 kfree(iov);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002486 return err;
2487}
2488
2489/*
2490 * BSD recvmsg interface
2491 */
2492
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002493long __sys_recvmsg(int fd, struct user_msghdr __user *msg, unsigned int flags,
2494 bool forbid_cmsg_compat)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002495{
2496 int fput_needed, err;
2497 struct msghdr msg_sys;
Andy Lutomirski1be374a2013-05-22 14:07:44 -07002498 struct socket *sock;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002499
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002500 if (forbid_cmsg_compat && (flags & MSG_CMSG_COMPAT))
2501 return -EINVAL;
2502
Andy Lutomirski1be374a2013-05-22 14:07:44 -07002503 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002504 if (!sock)
2505 goto out;
2506
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002507 err = ___sys_recvmsg(sock, msg, &msg_sys, flags, 0);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002508
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002509 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510out:
2511 return err;
2512}
2513
Al Viro666547f2014-04-06 14:03:05 -04002514SYSCALL_DEFINE3(recvmsg, int, fd, struct user_msghdr __user *, msg,
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002515 unsigned int, flags)
2516{
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002517 return __sys_recvmsg(fd, msg, flags, true);
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002518}
2519
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002520/*
2521 * Linux recvmmsg interface
2522 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523
Arnd Bergmanne11d4282018-04-18 13:43:52 +02002524static int do_recvmmsg(int fd, struct mmsghdr __user *mmsg,
2525 unsigned int vlen, unsigned int flags,
2526 struct timespec64 *timeout)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002527{
2528 int fput_needed, err, datagrams;
2529 struct socket *sock;
2530 struct mmsghdr __user *entry;
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002531 struct compat_mmsghdr __user *compat_entry;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002532 struct msghdr msg_sys;
Deepa Dinamani766b9f92016-05-19 17:09:05 -07002533 struct timespec64 end_time;
2534 struct timespec64 timeout64;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002535
2536 if (timeout &&
2537 poll_select_set_timeout(&end_time, timeout->tv_sec,
2538 timeout->tv_nsec))
2539 return -EINVAL;
2540
2541 datagrams = 0;
2542
2543 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2544 if (!sock)
2545 return err;
2546
Soheil Hassas Yeganeh7797dc42018-02-27 18:22:40 -05002547 if (likely(!(flags & MSG_ERRQUEUE))) {
2548 err = sock_error(sock->sk);
2549 if (err) {
2550 datagrams = err;
2551 goto out_put;
2552 }
Maxime Jayate623a9e2017-02-21 18:35:51 +01002553 }
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002554
2555 entry = mmsg;
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002556 compat_entry = (struct compat_mmsghdr __user *)mmsg;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002557
2558 while (datagrams < vlen) {
2559 /*
2560 * No need to ask LSM for more than the first datagram.
2561 */
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002562 if (MSG_CMSG_COMPAT & flags) {
Al Viro666547f2014-04-06 14:03:05 -04002563 err = ___sys_recvmsg(sock, (struct user_msghdr __user *)compat_entry,
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002564 &msg_sys, flags & ~MSG_WAITFORONE,
2565 datagrams);
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002566 if (err < 0)
2567 break;
2568 err = __put_user(err, &compat_entry->msg_len);
2569 ++compat_entry;
2570 } else {
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002571 err = ___sys_recvmsg(sock,
Al Viro666547f2014-04-06 14:03:05 -04002572 (struct user_msghdr __user *)entry,
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002573 &msg_sys, flags & ~MSG_WAITFORONE,
2574 datagrams);
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002575 if (err < 0)
2576 break;
2577 err = put_user(err, &entry->msg_len);
2578 ++entry;
2579 }
2580
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002581 if (err)
2582 break;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002583 ++datagrams;
2584
Brandon L Black71c5c1592010-03-26 16:18:03 +00002585 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
2586 if (flags & MSG_WAITFORONE)
2587 flags |= MSG_DONTWAIT;
2588
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002589 if (timeout) {
Deepa Dinamani766b9f92016-05-19 17:09:05 -07002590 ktime_get_ts64(&timeout64);
Arnd Bergmannc2e6c852018-04-18 13:42:25 +02002591 *timeout = timespec64_sub(end_time, timeout64);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002592 if (timeout->tv_sec < 0) {
2593 timeout->tv_sec = timeout->tv_nsec = 0;
2594 break;
2595 }
2596
2597 /* Timeout, return less than vlen datagrams */
2598 if (timeout->tv_nsec == 0 && timeout->tv_sec == 0)
2599 break;
2600 }
2601
2602 /* Out of band data, return right away */
2603 if (msg_sys.msg_flags & MSG_OOB)
2604 break;
Eric Dumazeta78cb842016-01-08 08:37:20 -08002605 cond_resched();
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002606 }
2607
Arnaldo Carvalho de Melo34b88a62016-03-14 09:56:35 -03002608 if (err == 0)
2609 goto out_put;
2610
2611 if (datagrams == 0) {
2612 datagrams = err;
2613 goto out_put;
2614 }
2615
2616 /*
2617 * We may return less entries than requested (vlen) if the
2618 * sock is non block and there aren't enough datagrams...
2619 */
2620 if (err != -EAGAIN) {
2621 /*
2622 * ... or if recvmsg returns an error after we
2623 * received some datagrams, where we record the
2624 * error to return on the next call or if the
2625 * app asks about it using getsockopt(SO_ERROR).
2626 */
2627 sock->sk->sk_err = -err;
2628 }
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002629out_put:
2630 fput_light(sock->file, fput_needed);
2631
Arnaldo Carvalho de Melo34b88a62016-03-14 09:56:35 -03002632 return datagrams;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002633}
2634
Arnd Bergmanne11d4282018-04-18 13:43:52 +02002635int __sys_recvmmsg(int fd, struct mmsghdr __user *mmsg,
2636 unsigned int vlen, unsigned int flags,
2637 struct __kernel_timespec __user *timeout,
2638 struct old_timespec32 __user *timeout32)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002639{
2640 int datagrams;
Arnd Bergmannc2e6c852018-04-18 13:42:25 +02002641 struct timespec64 timeout_sys;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002642
Arnd Bergmanne11d4282018-04-18 13:43:52 +02002643 if (timeout && get_timespec64(&timeout_sys, timeout))
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002644 return -EFAULT;
2645
Arnd Bergmanne11d4282018-04-18 13:43:52 +02002646 if (timeout32 && get_old_timespec32(&timeout_sys, timeout32))
2647 return -EFAULT;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002648
Arnd Bergmanne11d4282018-04-18 13:43:52 +02002649 if (!timeout && !timeout32)
2650 return do_recvmmsg(fd, mmsg, vlen, flags, NULL);
2651
2652 datagrams = do_recvmmsg(fd, mmsg, vlen, flags, &timeout_sys);
2653
2654 if (datagrams <= 0)
2655 return datagrams;
2656
2657 if (timeout && put_timespec64(&timeout_sys, timeout))
2658 datagrams = -EFAULT;
2659
2660 if (timeout32 && put_old_timespec32(&timeout_sys, timeout32))
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002661 datagrams = -EFAULT;
2662
2663 return datagrams;
2664}
2665
Dominik Brodowski1255e262018-03-13 20:44:21 +01002666SYSCALL_DEFINE5(recvmmsg, int, fd, struct mmsghdr __user *, mmsg,
2667 unsigned int, vlen, unsigned int, flags,
Arnd Bergmannc2e6c852018-04-18 13:42:25 +02002668 struct __kernel_timespec __user *, timeout)
Dominik Brodowski1255e262018-03-13 20:44:21 +01002669{
Arnd Bergmanne11d4282018-04-18 13:43:52 +02002670 if (flags & MSG_CMSG_COMPAT)
2671 return -EINVAL;
2672
2673 return __sys_recvmmsg(fd, mmsg, vlen, flags, timeout, NULL);
Dominik Brodowski1255e262018-03-13 20:44:21 +01002674}
2675
Arnd Bergmanne11d4282018-04-18 13:43:52 +02002676#ifdef CONFIG_COMPAT_32BIT_TIME
2677SYSCALL_DEFINE5(recvmmsg_time32, int, fd, struct mmsghdr __user *, mmsg,
2678 unsigned int, vlen, unsigned int, flags,
2679 struct old_timespec32 __user *, timeout)
2680{
2681 if (flags & MSG_CMSG_COMPAT)
2682 return -EINVAL;
2683
2684 return __sys_recvmmsg(fd, mmsg, vlen, flags, NULL, timeout);
2685}
2686#endif
2687
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002688#ifdef __ARCH_WANT_SYS_SOCKETCALL
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689/* Argument list sizes for sys_socketcall */
2690#define AL(x) ((x) * sizeof(unsigned long))
Anton Blanchard228e5482011-05-02 20:21:35 +00002691static const unsigned char nargs[21] = {
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002692 AL(0), AL(3), AL(3), AL(3), AL(2), AL(3),
2693 AL(3), AL(3), AL(4), AL(4), AL(4), AL(6),
2694 AL(6), AL(2), AL(5), AL(5), AL(3), AL(3),
Anton Blanchard228e5482011-05-02 20:21:35 +00002695 AL(4), AL(5), AL(4)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002696};
2697
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698#undef AL
2699
2700/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002701 * System call vectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 *
2703 * Argument checking cleaned up. Saved 20% in size.
2704 * This function doesn't need to set the kernel lock because
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002705 * it is set by the callees.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 */
2707
Heiko Carstens3e0fa652009-01-14 14:14:24 +01002708SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709{
Chen Gang2950fa92013-04-07 16:55:23 +08002710 unsigned long a[AUDITSC_ARGS];
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002711 unsigned long a0, a1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 int err;
Arjan van de Ven47379052009-09-28 12:57:44 -07002713 unsigned int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714
Anton Blanchard228e5482011-05-02 20:21:35 +00002715 if (call < 1 || call > SYS_SENDMMSG)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 return -EINVAL;
Jeremy Clinec8e8cd52018-07-27 22:43:01 +00002717 call = array_index_nospec(call, SYS_SENDMMSG + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718
Arjan van de Ven47379052009-09-28 12:57:44 -07002719 len = nargs[call];
2720 if (len > sizeof(a))
2721 return -EINVAL;
2722
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 /* copy_from_user should be SMP safe. */
Arjan van de Ven47379052009-09-28 12:57:44 -07002724 if (copy_from_user(a, args, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 return -EFAULT;
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002726
Chen Gang2950fa92013-04-07 16:55:23 +08002727 err = audit_socketcall(nargs[call] / sizeof(unsigned long), a);
2728 if (err)
2729 return err;
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002730
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002731 a0 = a[0];
2732 a1 = a[1];
2733
2734 switch (call) {
2735 case SYS_SOCKET:
Dominik Brodowski9d6a15c2018-03-13 19:29:43 +01002736 err = __sys_socket(a0, a1, a[2]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002737 break;
2738 case SYS_BIND:
Dominik Brodowskia87d35d2018-03-13 19:33:09 +01002739 err = __sys_bind(a0, (struct sockaddr __user *)a1, a[2]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002740 break;
2741 case SYS_CONNECT:
Dominik Brodowski1387c2c2018-03-13 19:35:09 +01002742 err = __sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002743 break;
2744 case SYS_LISTEN:
Dominik Brodowski25e290e2018-03-13 19:36:54 +01002745 err = __sys_listen(a0, a1);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002746 break;
2747 case SYS_ACCEPT:
Dominik Brodowski4541e802018-03-13 19:24:23 +01002748 err = __sys_accept4(a0, (struct sockaddr __user *)a1,
2749 (int __user *)a[2], 0);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002750 break;
2751 case SYS_GETSOCKNAME:
2752 err =
Dominik Brodowski8882a102018-03-13 19:43:14 +01002753 __sys_getsockname(a0, (struct sockaddr __user *)a1,
2754 (int __user *)a[2]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002755 break;
2756 case SYS_GETPEERNAME:
2757 err =
Dominik Brodowskib21c8f82018-03-13 19:47:00 +01002758 __sys_getpeername(a0, (struct sockaddr __user *)a1,
2759 (int __user *)a[2]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002760 break;
2761 case SYS_SOCKETPAIR:
Dominik Brodowski6debc8d2018-03-13 19:49:23 +01002762 err = __sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002763 break;
2764 case SYS_SEND:
Dominik Brodowskif3bf8962018-03-13 19:52:00 +01002765 err = __sys_sendto(a0, (void __user *)a1, a[2], a[3],
2766 NULL, 0);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002767 break;
2768 case SYS_SENDTO:
Dominik Brodowski211b6342018-03-13 19:18:52 +01002769 err = __sys_sendto(a0, (void __user *)a1, a[2], a[3],
2770 (struct sockaddr __user *)a[4], a[5]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002771 break;
2772 case SYS_RECV:
Dominik Brodowskid27e9af2018-03-13 19:54:17 +01002773 err = __sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
2774 NULL, NULL);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002775 break;
2776 case SYS_RECVFROM:
Dominik Brodowski7a09e1e2018-03-13 19:10:06 +01002777 err = __sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
2778 (struct sockaddr __user *)a[4],
2779 (int __user *)a[5]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002780 break;
2781 case SYS_SHUTDOWN:
Dominik Brodowski005a1ae2018-03-13 20:07:05 +01002782 err = __sys_shutdown(a0, a1);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002783 break;
2784 case SYS_SETSOCKOPT:
Dominik Brodowskicc36dca2018-03-13 20:10:59 +01002785 err = __sys_setsockopt(a0, a1, a[2], (char __user *)a[3],
2786 a[4]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002787 break;
2788 case SYS_GETSOCKOPT:
2789 err =
Dominik Brodowski13a2d702018-03-13 20:15:04 +01002790 __sys_getsockopt(a0, a1, a[2], (char __user *)a[3],
2791 (int __user *)a[4]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002792 break;
2793 case SYS_SENDMSG:
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002794 err = __sys_sendmsg(a0, (struct user_msghdr __user *)a1,
2795 a[2], true);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002796 break;
Anton Blanchard228e5482011-05-02 20:21:35 +00002797 case SYS_SENDMMSG:
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002798 err = __sys_sendmmsg(a0, (struct mmsghdr __user *)a1, a[2],
2799 a[3], true);
Anton Blanchard228e5482011-05-02 20:21:35 +00002800 break;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002801 case SYS_RECVMSG:
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002802 err = __sys_recvmsg(a0, (struct user_msghdr __user *)a1,
2803 a[2], true);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002804 break;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002805 case SYS_RECVMMSG:
Arnd Bergmanne11d4282018-04-18 13:43:52 +02002806 if (IS_ENABLED(CONFIG_64BIT) || !IS_ENABLED(CONFIG_64BIT_TIME))
2807 err = __sys_recvmmsg(a0, (struct mmsghdr __user *)a1,
2808 a[2], a[3],
2809 (struct __kernel_timespec __user *)a[4],
2810 NULL);
2811 else
2812 err = __sys_recvmmsg(a0, (struct mmsghdr __user *)a1,
2813 a[2], a[3], NULL,
2814 (struct old_timespec32 __user *)a[4]);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002815 break;
Ulrich Drepperde11def2008-11-19 15:36:14 -08002816 case SYS_ACCEPT4:
Dominik Brodowski4541e802018-03-13 19:24:23 +01002817 err = __sys_accept4(a0, (struct sockaddr __user *)a1,
2818 (int __user *)a[2], a[3]);
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07002819 break;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002820 default:
2821 err = -EINVAL;
2822 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 }
2824 return err;
2825}
2826
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002827#endif /* __ARCH_WANT_SYS_SOCKETCALL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002829/**
2830 * sock_register - add a socket protocol handler
2831 * @ops: description of protocol
2832 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 * This function is called by a protocol handler that wants to
2834 * advertise its address family, and have it linked into the
Masanari Iidae793c0f2014-09-04 23:44:36 +09002835 * socket interface. The value ops->family corresponds to the
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002836 * socket system call protocol family.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 */
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002838int sock_register(const struct net_proto_family *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839{
2840 int err;
2841
2842 if (ops->family >= NPROTO) {
Yang Yingliang3410f222014-02-12 17:09:55 +08002843 pr_crit("protocol %d >= NPROTO(%d)\n", ops->family, NPROTO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 return -ENOBUFS;
2845 }
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002846
2847 spin_lock(&net_family_lock);
Eric Dumazet190683a2010-11-10 10:50:44 +00002848 if (rcu_dereference_protected(net_families[ops->family],
2849 lockdep_is_held(&net_family_lock)))
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002850 err = -EEXIST;
2851 else {
Eric Dumazetcf778b02012-01-12 04:41:32 +00002852 rcu_assign_pointer(net_families[ops->family], ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 err = 0;
2854 }
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002855 spin_unlock(&net_family_lock);
2856
Yang Yingliang3410f222014-02-12 17:09:55 +08002857 pr_info("NET: Registered protocol family %d\n", ops->family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 return err;
2859}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002860EXPORT_SYMBOL(sock_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002862/**
2863 * sock_unregister - remove a protocol handler
2864 * @family: protocol family to remove
2865 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 * This function is called by a protocol handler that wants to
2867 * remove its address family, and have it unlinked from the
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002868 * new socket creation.
2869 *
2870 * If protocol handler is a module, then it can use module reference
2871 * counts to protect against new references. If protocol handler is not
2872 * a module then it needs to provide its own protection in
2873 * the ops->create routine.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 */
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002875void sock_unregister(int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876{
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002877 BUG_ON(family < 0 || family >= NPROTO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002879 spin_lock(&net_family_lock);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00002880 RCU_INIT_POINTER(net_families[family], NULL);
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002881 spin_unlock(&net_family_lock);
2882
2883 synchronize_rcu();
2884
Yang Yingliang3410f222014-02-12 17:09:55 +08002885 pr_info("NET: Unregistered protocol family %d\n", family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002887EXPORT_SYMBOL(sock_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888
Xin Longbf2ae2e2018-03-10 18:57:50 +08002889bool sock_is_registered(int family)
2890{
Jeremy Cline66b51b02018-08-13 22:23:13 +00002891 return family < NPROTO && rcu_access_pointer(net_families[family]);
Xin Longbf2ae2e2018-03-10 18:57:50 +08002892}
2893
Andi Kleen77d76ea2005-12-22 12:43:42 -08002894static int __init sock_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895{
Nick Pigginb3e19d92011-01-07 17:50:11 +11002896 int err;
Eric W. Biederman2ca794e2012-04-19 13:20:32 +00002897 /*
2898 * Initialize the network sysctl infrastructure.
2899 */
2900 err = net_sysctl_init();
2901 if (err)
2902 goto out;
Nick Pigginb3e19d92011-01-07 17:50:11 +11002903
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002905 * Initialize skbuff SLAB cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 */
2907 skb_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908
2909 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002910 * Initialize the protocols module.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 */
2912
2913 init_inodecache();
Nick Pigginb3e19d92011-01-07 17:50:11 +11002914
2915 err = register_filesystem(&sock_fs_type);
2916 if (err)
2917 goto out_fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 sock_mnt = kern_mount(&sock_fs_type);
Nick Pigginb3e19d92011-01-07 17:50:11 +11002919 if (IS_ERR(sock_mnt)) {
2920 err = PTR_ERR(sock_mnt);
2921 goto out_mount;
2922 }
Andi Kleen77d76ea2005-12-22 12:43:42 -08002923
2924 /* The real protocol initialization is performed in later initcalls.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 */
2926
2927#ifdef CONFIG_NETFILTER
Pablo Neira Ayuso6d11cfd2013-05-22 22:42:36 +00002928 err = netfilter_init();
2929 if (err)
2930 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931#endif
David S. Millercbeb3212005-12-22 12:58:55 -08002932
Daniel Borkmann408eccc2014-04-01 16:20:23 +02002933 ptp_classifier_init();
Richard Cochranc1f19b52010-07-17 08:49:36 +00002934
Nick Pigginb3e19d92011-01-07 17:50:11 +11002935out:
2936 return err;
2937
2938out_mount:
2939 unregister_filesystem(&sock_fs_type);
2940out_fs:
2941 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942}
2943
Andi Kleen77d76ea2005-12-22 12:43:42 -08002944core_initcall(sock_init); /* early initcall */
2945
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946#ifdef CONFIG_PROC_FS
2947void socket_seq_show(struct seq_file *seq)
2948{
Tonghao Zhang648845a2017-12-14 05:51:58 -08002949 seq_printf(seq, "sockets: used %d\n",
2950 sock_inuse_get(seq->private));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951}
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002952#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953
Shaun Pereira89bbfc92006-03-21 23:58:08 -08002954#ifdef CONFIG_COMPAT
Al Viro36fd6332017-06-26 13:19:16 -04002955static int compat_dev_ifconf(struct net *net, struct compat_ifconf __user *uifc32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002956{
Arnd Bergmann6b960182009-11-06 23:10:54 -08002957 struct compat_ifconf ifc32;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002958 struct ifconf ifc;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002959 int err;
2960
Arnd Bergmann6b960182009-11-06 23:10:54 -08002961 if (copy_from_user(&ifc32, uifc32, sizeof(struct compat_ifconf)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002962 return -EFAULT;
2963
Al Viro36fd6332017-06-26 13:19:16 -04002964 ifc.ifc_len = ifc32.ifc_len;
2965 ifc.ifc_req = compat_ptr(ifc32.ifcbuf);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002966
Al Viro36fd6332017-06-26 13:19:16 -04002967 rtnl_lock();
2968 err = dev_ifconf(net, &ifc, sizeof(struct compat_ifreq));
2969 rtnl_unlock();
Arnd Bergmann7a229382009-11-06 23:00:29 -08002970 if (err)
2971 return err;
2972
Al Viro36fd6332017-06-26 13:19:16 -04002973 ifc32.ifc_len = ifc.ifc_len;
Arnd Bergmann6b960182009-11-06 23:10:54 -08002974 if (copy_to_user(uifc32, &ifc32, sizeof(struct compat_ifconf)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002975 return -EFAULT;
2976
2977 return 0;
2978}
2979
Arnd Bergmann6b960182009-11-06 23:10:54 -08002980static int ethtool_ioctl(struct net *net, struct compat_ifreq __user *ifr32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002981{
Ben Hutchings3a7da392011-03-17 07:34:32 +00002982 struct compat_ethtool_rxnfc __user *compat_rxnfc;
2983 bool convert_in = false, convert_out = false;
Al Viro44c02a22017-10-05 12:59:44 -04002984 size_t buf_size = 0;
2985 struct ethtool_rxnfc __user *rxnfc = NULL;
2986 struct ifreq ifr;
Ben Hutchings3a7da392011-03-17 07:34:32 +00002987 u32 rule_cnt = 0, actual_rule_cnt;
2988 u32 ethcmd;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002989 u32 data;
Ben Hutchings3a7da392011-03-17 07:34:32 +00002990 int ret;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002991
2992 if (get_user(data, &ifr32->ifr_ifru.ifru_data))
2993 return -EFAULT;
2994
Ben Hutchings3a7da392011-03-17 07:34:32 +00002995 compat_rxnfc = compat_ptr(data);
2996
2997 if (get_user(ethcmd, &compat_rxnfc->cmd))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002998 return -EFAULT;
2999
Ben Hutchings3a7da392011-03-17 07:34:32 +00003000 /* Most ethtool structures are defined without padding.
3001 * Unfortunately struct ethtool_rxnfc is an exception.
3002 */
3003 switch (ethcmd) {
3004 default:
3005 break;
3006 case ETHTOOL_GRXCLSRLALL:
3007 /* Buffer size is variable */
3008 if (get_user(rule_cnt, &compat_rxnfc->rule_cnt))
3009 return -EFAULT;
3010 if (rule_cnt > KMALLOC_MAX_SIZE / sizeof(u32))
3011 return -ENOMEM;
3012 buf_size += rule_cnt * sizeof(u32);
3013 /* fall through */
3014 case ETHTOOL_GRXRINGS:
3015 case ETHTOOL_GRXCLSRLCNT:
3016 case ETHTOOL_GRXCLSRULE:
Ben Hutchings55664f32012-01-03 12:04:51 +00003017 case ETHTOOL_SRXCLSRLINS:
Ben Hutchings3a7da392011-03-17 07:34:32 +00003018 convert_out = true;
3019 /* fall through */
3020 case ETHTOOL_SRXCLSRLDEL:
Ben Hutchings3a7da392011-03-17 07:34:32 +00003021 buf_size += sizeof(struct ethtool_rxnfc);
3022 convert_in = true;
Al Viro44c02a22017-10-05 12:59:44 -04003023 rxnfc = compat_alloc_user_space(buf_size);
Ben Hutchings3a7da392011-03-17 07:34:32 +00003024 break;
3025 }
3026
Al Viro44c02a22017-10-05 12:59:44 -04003027 if (copy_from_user(&ifr.ifr_name, &ifr32->ifr_name, IFNAMSIZ))
Ben Hutchings3a7da392011-03-17 07:34:32 +00003028 return -EFAULT;
3029
Al Viro44c02a22017-10-05 12:59:44 -04003030 ifr.ifr_data = convert_in ? rxnfc : (void __user *)compat_rxnfc;
Ben Hutchings3a7da392011-03-17 07:34:32 +00003031
3032 if (convert_in) {
Alexander Duyck127fe532011-04-08 18:01:59 +00003033 /* We expect there to be holes between fs.m_ext and
Ben Hutchings3a7da392011-03-17 07:34:32 +00003034 * fs.ring_cookie and at the end of fs, but nowhere else.
3035 */
Alexander Duyck127fe532011-04-08 18:01:59 +00003036 BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc, fs.m_ext) +
3037 sizeof(compat_rxnfc->fs.m_ext) !=
3038 offsetof(struct ethtool_rxnfc, fs.m_ext) +
3039 sizeof(rxnfc->fs.m_ext));
Ben Hutchings3a7da392011-03-17 07:34:32 +00003040 BUILD_BUG_ON(
3041 offsetof(struct compat_ethtool_rxnfc, fs.location) -
3042 offsetof(struct compat_ethtool_rxnfc, fs.ring_cookie) !=
3043 offsetof(struct ethtool_rxnfc, fs.location) -
3044 offsetof(struct ethtool_rxnfc, fs.ring_cookie));
3045
3046 if (copy_in_user(rxnfc, compat_rxnfc,
Stephen Hemminger954b1242013-02-11 06:22:28 +00003047 (void __user *)(&rxnfc->fs.m_ext + 1) -
3048 (void __user *)rxnfc) ||
Ben Hutchings3a7da392011-03-17 07:34:32 +00003049 copy_in_user(&rxnfc->fs.ring_cookie,
3050 &compat_rxnfc->fs.ring_cookie,
Stephen Hemminger954b1242013-02-11 06:22:28 +00003051 (void __user *)(&rxnfc->fs.location + 1) -
Wenwen Wangb6168562018-10-18 09:36:46 -05003052 (void __user *)&rxnfc->fs.ring_cookie))
3053 return -EFAULT;
3054 if (ethcmd == ETHTOOL_GRXCLSRLALL) {
3055 if (put_user(rule_cnt, &rxnfc->rule_cnt))
3056 return -EFAULT;
3057 } else if (copy_in_user(&rxnfc->rule_cnt,
3058 &compat_rxnfc->rule_cnt,
3059 sizeof(rxnfc->rule_cnt)))
Ben Hutchings3a7da392011-03-17 07:34:32 +00003060 return -EFAULT;
3061 }
3062
Al Viro44c02a22017-10-05 12:59:44 -04003063 ret = dev_ioctl(net, SIOCETHTOOL, &ifr, NULL);
Ben Hutchings3a7da392011-03-17 07:34:32 +00003064 if (ret)
3065 return ret;
3066
3067 if (convert_out) {
3068 if (copy_in_user(compat_rxnfc, rxnfc,
Stephen Hemminger954b1242013-02-11 06:22:28 +00003069 (const void __user *)(&rxnfc->fs.m_ext + 1) -
3070 (const void __user *)rxnfc) ||
Ben Hutchings3a7da392011-03-17 07:34:32 +00003071 copy_in_user(&compat_rxnfc->fs.ring_cookie,
3072 &rxnfc->fs.ring_cookie,
Stephen Hemminger954b1242013-02-11 06:22:28 +00003073 (const void __user *)(&rxnfc->fs.location + 1) -
3074 (const void __user *)&rxnfc->fs.ring_cookie) ||
Ben Hutchings3a7da392011-03-17 07:34:32 +00003075 copy_in_user(&compat_rxnfc->rule_cnt, &rxnfc->rule_cnt,
3076 sizeof(rxnfc->rule_cnt)))
3077 return -EFAULT;
3078
3079 if (ethcmd == ETHTOOL_GRXCLSRLALL) {
3080 /* As an optimisation, we only copy the actual
3081 * number of rules that the underlying
3082 * function returned. Since Mallory might
3083 * change the rule count in user memory, we
3084 * check that it is less than the rule count
3085 * originally given (as the user buffer size),
3086 * which has been range-checked.
3087 */
3088 if (get_user(actual_rule_cnt, &rxnfc->rule_cnt))
3089 return -EFAULT;
3090 if (actual_rule_cnt < rule_cnt)
3091 rule_cnt = actual_rule_cnt;
3092 if (copy_in_user(&compat_rxnfc->rule_locs[0],
3093 &rxnfc->rule_locs[0],
3094 rule_cnt * sizeof(u32)))
3095 return -EFAULT;
3096 }
3097 }
3098
3099 return 0;
Arnd Bergmann7a229382009-11-06 23:00:29 -08003100}
3101
Arnd Bergmann7a50a242009-11-08 20:57:03 -08003102static int compat_siocwandev(struct net *net, struct compat_ifreq __user *uifr32)
3103{
Arnd Bergmann7a50a242009-11-08 20:57:03 -08003104 compat_uptr_t uptr32;
Al Viro44c02a22017-10-05 12:59:44 -04003105 struct ifreq ifr;
3106 void __user *saved;
3107 int err;
Arnd Bergmann7a50a242009-11-08 20:57:03 -08003108
Al Viro44c02a22017-10-05 12:59:44 -04003109 if (copy_from_user(&ifr, uifr32, sizeof(struct compat_ifreq)))
Arnd Bergmann7a50a242009-11-08 20:57:03 -08003110 return -EFAULT;
3111
3112 if (get_user(uptr32, &uifr32->ifr_settings.ifs_ifsu))
3113 return -EFAULT;
3114
Al Viro44c02a22017-10-05 12:59:44 -04003115 saved = ifr.ifr_settings.ifs_ifsu.raw_hdlc;
3116 ifr.ifr_settings.ifs_ifsu.raw_hdlc = compat_ptr(uptr32);
Arnd Bergmann7a50a242009-11-08 20:57:03 -08003117
Al Viro44c02a22017-10-05 12:59:44 -04003118 err = dev_ioctl(net, SIOCWANDEV, &ifr, NULL);
3119 if (!err) {
3120 ifr.ifr_settings.ifs_ifsu.raw_hdlc = saved;
3121 if (copy_to_user(uifr32, &ifr, sizeof(struct compat_ifreq)))
3122 err = -EFAULT;
Joe Perchesccbd6a52010-05-14 10:58:26 +00003123 }
Al Viro44c02a22017-10-05 12:59:44 -04003124 return err;
Arnd Bergmann7a229382009-11-06 23:00:29 -08003125}
3126
Ben Hutchings590d4692013-11-18 17:04:13 +00003127/* Handle ioctls that use ifreq::ifr_data and just need struct ifreq converted */
3128static int compat_ifr_data_ioctl(struct net *net, unsigned int cmd,
Arnd Bergmann6b960182009-11-06 23:10:54 -08003129 struct compat_ifreq __user *u_ifreq32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08003130{
Al Viro44c02a22017-10-05 12:59:44 -04003131 struct ifreq ifreq;
Arnd Bergmann7a229382009-11-06 23:00:29 -08003132 u32 data32;
3133
Al Viro44c02a22017-10-05 12:59:44 -04003134 if (copy_from_user(ifreq.ifr_name, u_ifreq32->ifr_name, IFNAMSIZ))
Arnd Bergmann7a229382009-11-06 23:00:29 -08003135 return -EFAULT;
Al Viro44c02a22017-10-05 12:59:44 -04003136 if (get_user(data32, &u_ifreq32->ifr_data))
Arnd Bergmann7a229382009-11-06 23:00:29 -08003137 return -EFAULT;
Al Viro44c02a22017-10-05 12:59:44 -04003138 ifreq.ifr_data = compat_ptr(data32);
Arnd Bergmann7a229382009-11-06 23:00:29 -08003139
Al Viro44c02a22017-10-05 12:59:44 -04003140 return dev_ioctl(net, cmd, &ifreq, NULL);
Arnd Bergmann7a229382009-11-06 23:00:29 -08003141}
3142
Johannes Berg37ac39b2019-01-25 22:43:18 +01003143static int compat_ifreq_ioctl(struct net *net, struct socket *sock,
3144 unsigned int cmd,
3145 struct compat_ifreq __user *uifr32)
3146{
3147 struct ifreq __user *uifr;
3148 int err;
3149
3150 /* Handle the fact that while struct ifreq has the same *layout* on
3151 * 32/64 for everything but ifreq::ifru_ifmap and ifreq::ifru_data,
3152 * which are handled elsewhere, it still has different *size* due to
3153 * ifreq::ifru_ifmap (which is 16 bytes on 32 bit, 24 bytes on 64-bit,
3154 * resulting in struct ifreq being 32 and 40 bytes respectively).
3155 * As a result, if the struct happens to be at the end of a page and
3156 * the next page isn't readable/writable, we get a fault. To prevent
3157 * that, copy back and forth to the full size.
3158 */
3159
3160 uifr = compat_alloc_user_space(sizeof(*uifr));
3161 if (copy_in_user(uifr, uifr32, sizeof(*uifr32)))
3162 return -EFAULT;
3163
3164 err = sock_do_ioctl(net, sock, cmd, (unsigned long)uifr);
3165
3166 if (!err) {
3167 switch (cmd) {
3168 case SIOCGIFFLAGS:
3169 case SIOCGIFMETRIC:
3170 case SIOCGIFMTU:
3171 case SIOCGIFMEM:
3172 case SIOCGIFHWADDR:
3173 case SIOCGIFINDEX:
3174 case SIOCGIFADDR:
3175 case SIOCGIFBRDADDR:
3176 case SIOCGIFDSTADDR:
3177 case SIOCGIFNETMASK:
3178 case SIOCGIFPFLAGS:
3179 case SIOCGIFTXQLEN:
3180 case SIOCGMIIPHY:
3181 case SIOCGMIIREG:
Johannes Bergc6c9fee2019-01-25 22:43:19 +01003182 case SIOCGIFNAME:
Johannes Berg37ac39b2019-01-25 22:43:18 +01003183 if (copy_in_user(uifr32, uifr, sizeof(*uifr32)))
3184 err = -EFAULT;
3185 break;
3186 }
3187 }
3188 return err;
3189}
3190
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003191static int compat_sioc_ifmap(struct net *net, unsigned int cmd,
3192 struct compat_ifreq __user *uifr32)
3193{
3194 struct ifreq ifr;
3195 struct compat_ifmap __user *uifmap32;
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003196 int err;
3197
3198 uifmap32 = &uifr32->ifr_ifru.ifru_map;
3199 err = copy_from_user(&ifr, uifr32, sizeof(ifr.ifr_name));
Mathieu Desnoyers3ddc5b42013-09-11 14:23:18 -07003200 err |= get_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
3201 err |= get_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
3202 err |= get_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
3203 err |= get_user(ifr.ifr_map.irq, &uifmap32->irq);
3204 err |= get_user(ifr.ifr_map.dma, &uifmap32->dma);
3205 err |= get_user(ifr.ifr_map.port, &uifmap32->port);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003206 if (err)
3207 return -EFAULT;
3208
Al Viro44c02a22017-10-05 12:59:44 -04003209 err = dev_ioctl(net, cmd, &ifr, NULL);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003210
3211 if (cmd == SIOCGIFMAP && !err) {
3212 err = copy_to_user(uifr32, &ifr, sizeof(ifr.ifr_name));
Mathieu Desnoyers3ddc5b42013-09-11 14:23:18 -07003213 err |= put_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
3214 err |= put_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
3215 err |= put_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
3216 err |= put_user(ifr.ifr_map.irq, &uifmap32->irq);
3217 err |= put_user(ifr.ifr_map.dma, &uifmap32->dma);
3218 err |= put_user(ifr.ifr_map.port, &uifmap32->port);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003219 if (err)
3220 err = -EFAULT;
3221 }
3222 return err;
3223}
3224
Arnd Bergmann7a229382009-11-06 23:00:29 -08003225struct rtentry32 {
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003226 u32 rt_pad1;
Arnd Bergmann7a229382009-11-06 23:00:29 -08003227 struct sockaddr rt_dst; /* target address */
3228 struct sockaddr rt_gateway; /* gateway addr (RTF_GATEWAY) */
3229 struct sockaddr rt_genmask; /* target network mask (IP) */
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003230 unsigned short rt_flags;
3231 short rt_pad2;
3232 u32 rt_pad3;
3233 unsigned char rt_tos;
3234 unsigned char rt_class;
3235 short rt_pad4;
3236 short rt_metric; /* +1 for binary compatibility! */
Arnd Bergmann7a229382009-11-06 23:00:29 -08003237 /* char * */ u32 rt_dev; /* forcing the device at add */
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003238 u32 rt_mtu; /* per route MTU/Window */
3239 u32 rt_window; /* Window clamping */
Arnd Bergmann7a229382009-11-06 23:00:29 -08003240 unsigned short rt_irtt; /* Initial RTT */
3241};
3242
3243struct in6_rtmsg32 {
3244 struct in6_addr rtmsg_dst;
3245 struct in6_addr rtmsg_src;
3246 struct in6_addr rtmsg_gateway;
3247 u32 rtmsg_type;
3248 u16 rtmsg_dst_len;
3249 u16 rtmsg_src_len;
3250 u32 rtmsg_metric;
3251 u32 rtmsg_info;
3252 u32 rtmsg_flags;
3253 s32 rtmsg_ifindex;
3254};
3255
Arnd Bergmann6b960182009-11-06 23:10:54 -08003256static int routing_ioctl(struct net *net, struct socket *sock,
3257 unsigned int cmd, void __user *argp)
Arnd Bergmann7a229382009-11-06 23:00:29 -08003258{
3259 int ret;
3260 void *r = NULL;
3261 struct in6_rtmsg r6;
3262 struct rtentry r4;
3263 char devname[16];
3264 u32 rtdev;
3265 mm_segment_t old_fs = get_fs();
3266
Arnd Bergmann6b960182009-11-06 23:10:54 -08003267 if (sock && sock->sk && sock->sk->sk_family == AF_INET6) { /* ipv6 */
3268 struct in6_rtmsg32 __user *ur6 = argp;
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003269 ret = copy_from_user(&r6.rtmsg_dst, &(ur6->rtmsg_dst),
Arnd Bergmann7a229382009-11-06 23:00:29 -08003270 3 * sizeof(struct in6_addr));
Mathieu Desnoyers3ddc5b42013-09-11 14:23:18 -07003271 ret |= get_user(r6.rtmsg_type, &(ur6->rtmsg_type));
3272 ret |= get_user(r6.rtmsg_dst_len, &(ur6->rtmsg_dst_len));
3273 ret |= get_user(r6.rtmsg_src_len, &(ur6->rtmsg_src_len));
3274 ret |= get_user(r6.rtmsg_metric, &(ur6->rtmsg_metric));
3275 ret |= get_user(r6.rtmsg_info, &(ur6->rtmsg_info));
3276 ret |= get_user(r6.rtmsg_flags, &(ur6->rtmsg_flags));
3277 ret |= get_user(r6.rtmsg_ifindex, &(ur6->rtmsg_ifindex));
Arnd Bergmann7a229382009-11-06 23:00:29 -08003278
3279 r = (void *) &r6;
3280 } else { /* ipv4 */
Arnd Bergmann6b960182009-11-06 23:10:54 -08003281 struct rtentry32 __user *ur4 = argp;
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003282 ret = copy_from_user(&r4.rt_dst, &(ur4->rt_dst),
Arnd Bergmann7a229382009-11-06 23:00:29 -08003283 3 * sizeof(struct sockaddr));
Mathieu Desnoyers3ddc5b42013-09-11 14:23:18 -07003284 ret |= get_user(r4.rt_flags, &(ur4->rt_flags));
3285 ret |= get_user(r4.rt_metric, &(ur4->rt_metric));
3286 ret |= get_user(r4.rt_mtu, &(ur4->rt_mtu));
3287 ret |= get_user(r4.rt_window, &(ur4->rt_window));
3288 ret |= get_user(r4.rt_irtt, &(ur4->rt_irtt));
3289 ret |= get_user(rtdev, &(ur4->rt_dev));
Arnd Bergmann7a229382009-11-06 23:00:29 -08003290 if (rtdev) {
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003291 ret |= copy_from_user(devname, compat_ptr(rtdev), 15);
stephen hemmingerc3f52ae2011-02-23 09:06:48 +00003292 r4.rt_dev = (char __user __force *)devname;
3293 devname[15] = 0;
Arnd Bergmann7a229382009-11-06 23:00:29 -08003294 } else
3295 r4.rt_dev = NULL;
3296
3297 r = (void *) &r4;
3298 }
3299
3300 if (ret) {
3301 ret = -EFAULT;
3302 goto out;
3303 }
3304
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003305 set_fs(KERNEL_DS);
Johannes Berg63ff03a2019-01-25 22:43:17 +01003306 ret = sock_do_ioctl(net, sock, cmd, (unsigned long) r);
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003307 set_fs(old_fs);
Arnd Bergmann7a229382009-11-06 23:00:29 -08003308
3309out:
Arnd Bergmann7a229382009-11-06 23:00:29 -08003310 return ret;
3311}
3312
3313/* Since old style bridge ioctl's endup using SIOCDEVPRIVATE
3314 * for some operations; this forces use of the newer bridge-utils that
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003315 * use compatible ioctls
Arnd Bergmann7a229382009-11-06 23:00:29 -08003316 */
Arnd Bergmann6b960182009-11-06 23:10:54 -08003317static int old_bridge_ioctl(compat_ulong_t __user *argp)
Arnd Bergmann7a229382009-11-06 23:00:29 -08003318{
Arnd Bergmann6b960182009-11-06 23:10:54 -08003319 compat_ulong_t tmp;
Arnd Bergmann7a229382009-11-06 23:00:29 -08003320
Arnd Bergmann6b960182009-11-06 23:10:54 -08003321 if (get_user(tmp, argp))
Arnd Bergmann7a229382009-11-06 23:00:29 -08003322 return -EFAULT;
3323 if (tmp == BRCTL_GET_VERSION)
3324 return BRCTL_VERSION + 1;
3325 return -EINVAL;
3326}
3327
Arnd Bergmann6b960182009-11-06 23:10:54 -08003328static int compat_sock_ioctl_trans(struct file *file, struct socket *sock,
3329 unsigned int cmd, unsigned long arg)
3330{
3331 void __user *argp = compat_ptr(arg);
3332 struct sock *sk = sock->sk;
3333 struct net *net = sock_net(sk);
Arnd Bergmann7a229382009-11-06 23:00:29 -08003334
Arnd Bergmann6b960182009-11-06 23:10:54 -08003335 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15))
Ben Hutchings590d4692013-11-18 17:04:13 +00003336 return compat_ifr_data_ioctl(net, cmd, argp);
Arnd Bergmann7a229382009-11-06 23:00:29 -08003337
Arnd Bergmann6b960182009-11-06 23:10:54 -08003338 switch (cmd) {
3339 case SIOCSIFBR:
3340 case SIOCGIFBR:
3341 return old_bridge_ioctl(argp);
Arnd Bergmann6b960182009-11-06 23:10:54 -08003342 case SIOCGIFCONF:
Al Viro36fd6332017-06-26 13:19:16 -04003343 return compat_dev_ifconf(net, argp);
Arnd Bergmann6b960182009-11-06 23:10:54 -08003344 case SIOCETHTOOL:
3345 return ethtool_ioctl(net, argp);
Arnd Bergmann7a50a242009-11-08 20:57:03 -08003346 case SIOCWANDEV:
3347 return compat_siocwandev(net, argp);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003348 case SIOCGIFMAP:
3349 case SIOCSIFMAP:
3350 return compat_sioc_ifmap(net, cmd, argp);
Arnd Bergmann6b960182009-11-06 23:10:54 -08003351 case SIOCADDRT:
3352 case SIOCDELRT:
3353 return routing_ioctl(net, sock, cmd, argp);
Arnd Bergmann0768e172019-04-17 22:56:11 +02003354 case SIOCGSTAMP_OLD:
3355 case SIOCGSTAMPNS_OLD:
Arnd Bergmannc7cbdbf2019-04-17 22:51:48 +02003356 if (!sock->ops->gettstamp)
3357 return -ENOIOCTLCMD;
Arnd Bergmann0768e172019-04-17 22:56:11 +02003358 return sock->ops->gettstamp(sock, argp, cmd == SIOCGSTAMP_OLD,
Arnd Bergmannc7cbdbf2019-04-17 22:51:48 +02003359 !COMPAT_USE_64BIT_TIME);
3360
Ben Hutchings590d4692013-11-18 17:04:13 +00003361 case SIOCBONDSLAVEINFOQUERY:
3362 case SIOCBONDINFOQUERY:
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003363 case SIOCSHWTSTAMP:
Ben Hutchingsfd468c72013-11-14 01:19:29 +00003364 case SIOCGHWTSTAMP:
Ben Hutchings590d4692013-11-18 17:04:13 +00003365 return compat_ifr_data_ioctl(net, cmd, argp);
Arnd Bergmann7a229382009-11-06 23:00:29 -08003366
Arnd Bergmann6b960182009-11-06 23:10:54 -08003367 case FIOSETOWN:
3368 case SIOCSPGRP:
3369 case FIOGETOWN:
3370 case SIOCGPGRP:
3371 case SIOCBRADDBR:
3372 case SIOCBRDELBR:
3373 case SIOCGIFVLAN:
3374 case SIOCSIFVLAN:
3375 case SIOCADDDLCI:
3376 case SIOCDELDLCI:
Andrey Vaginc62cce22016-10-24 18:29:13 -07003377 case SIOCGSKNS:
Arnd Bergmann0768e172019-04-17 22:56:11 +02003378 case SIOCGSTAMP_NEW:
3379 case SIOCGSTAMPNS_NEW:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003380 return sock_ioctl(file, cmd, arg);
3381
3382 case SIOCGIFFLAGS:
3383 case SIOCSIFFLAGS:
3384 case SIOCGIFMETRIC:
3385 case SIOCSIFMETRIC:
3386 case SIOCGIFMTU:
3387 case SIOCSIFMTU:
3388 case SIOCGIFMEM:
3389 case SIOCSIFMEM:
3390 case SIOCGIFHWADDR:
3391 case SIOCSIFHWADDR:
3392 case SIOCADDMULTI:
3393 case SIOCDELMULTI:
3394 case SIOCGIFINDEX:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003395 case SIOCGIFADDR:
3396 case SIOCSIFADDR:
3397 case SIOCSIFHWBROADCAST:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003398 case SIOCDIFADDR:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003399 case SIOCGIFBRDADDR:
3400 case SIOCSIFBRDADDR:
3401 case SIOCGIFDSTADDR:
3402 case SIOCSIFDSTADDR:
3403 case SIOCGIFNETMASK:
3404 case SIOCSIFNETMASK:
3405 case SIOCSIFPFLAGS:
3406 case SIOCGIFPFLAGS:
3407 case SIOCGIFTXQLEN:
3408 case SIOCSIFTXQLEN:
3409 case SIOCBRADDIF:
3410 case SIOCBRDELIF:
Johannes Bergc6c9fee2019-01-25 22:43:19 +01003411 case SIOCGIFNAME:
Arnd Bergmann9177efd2009-11-06 08:09:09 +00003412 case SIOCSIFNAME:
3413 case SIOCGMIIPHY:
3414 case SIOCGMIIREG:
3415 case SIOCSMIIREG:
Al Virof92d4fc2017-09-30 19:32:17 -04003416 case SIOCBONDENSLAVE:
3417 case SIOCBONDRELEASE:
3418 case SIOCBONDSETHWADDR:
3419 case SIOCBONDCHANGEACTIVE:
Johannes Berg37ac39b2019-01-25 22:43:18 +01003420 return compat_ifreq_ioctl(net, sock, cmd, argp);
3421
Arnd Bergmann6b960182009-11-06 23:10:54 -08003422 case SIOCSARP:
3423 case SIOCGARP:
3424 case SIOCDARP:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003425 case SIOCATMARK:
Johannes Berg63ff03a2019-01-25 22:43:17 +01003426 return sock_do_ioctl(net, sock, cmd, arg);
Arnd Bergmann9177efd2009-11-06 08:09:09 +00003427 }
3428
Arnd Bergmann6b960182009-11-06 23:10:54 -08003429 return -ENOIOCTLCMD;
3430}
Arnd Bergmann7a229382009-11-06 23:00:29 -08003431
Eric Dumazet95c96172012-04-15 05:58:06 +00003432static long compat_sock_ioctl(struct file *file, unsigned int cmd,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07003433 unsigned long arg)
Shaun Pereira89bbfc92006-03-21 23:58:08 -08003434{
3435 struct socket *sock = file->private_data;
3436 int ret = -ENOIOCTLCMD;
David S. Miller87de87d2008-06-03 09:14:03 -07003437 struct sock *sk;
3438 struct net *net;
3439
3440 sk = sock->sk;
3441 net = sock_net(sk);
Shaun Pereira89bbfc92006-03-21 23:58:08 -08003442
3443 if (sock->ops->compat_ioctl)
3444 ret = sock->ops->compat_ioctl(sock, cmd, arg);
3445
David S. Miller87de87d2008-06-03 09:14:03 -07003446 if (ret == -ENOIOCTLCMD &&
3447 (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST))
3448 ret = compat_wext_handle_ioctl(net, cmd, arg);
3449
Arnd Bergmann6b960182009-11-06 23:10:54 -08003450 if (ret == -ENOIOCTLCMD)
3451 ret = compat_sock_ioctl_trans(file, sock, cmd, arg);
3452
Shaun Pereira89bbfc92006-03-21 23:58:08 -08003453 return ret;
3454}
3455#endif
3456
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003457/**
3458 * kernel_bind - bind an address to a socket (kernel space)
3459 * @sock: socket
3460 * @addr: address
3461 * @addrlen: length of address
3462 *
3463 * Returns 0 or an error.
3464 */
3465
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003466int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen)
3467{
3468 return sock->ops->bind(sock, addr, addrlen);
3469}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003470EXPORT_SYMBOL(kernel_bind);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003471
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003472/**
3473 * kernel_listen - move socket to listening state (kernel space)
3474 * @sock: socket
3475 * @backlog: pending connections queue size
3476 *
3477 * Returns 0 or an error.
3478 */
3479
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003480int kernel_listen(struct socket *sock, int backlog)
3481{
3482 return sock->ops->listen(sock, backlog);
3483}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003484EXPORT_SYMBOL(kernel_listen);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003485
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003486/**
3487 * kernel_accept - accept a connection (kernel space)
3488 * @sock: listening socket
3489 * @newsock: new connected socket
3490 * @flags: flags
3491 *
3492 * @flags must be SOCK_CLOEXEC, SOCK_NONBLOCK or 0.
3493 * If it fails, @newsock is guaranteed to be %NULL.
3494 * Returns 0 or an error.
3495 */
3496
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003497int kernel_accept(struct socket *sock, struct socket **newsock, int flags)
3498{
3499 struct sock *sk = sock->sk;
3500 int err;
3501
3502 err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
3503 newsock);
3504 if (err < 0)
3505 goto done;
3506
David Howellscdfbabf2017-03-09 08:09:05 +00003507 err = sock->ops->accept(sock, *newsock, flags, true);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003508 if (err < 0) {
3509 sock_release(*newsock);
Tony Battersbyfa8705b2007-10-10 21:09:04 -07003510 *newsock = NULL;
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003511 goto done;
3512 }
3513
3514 (*newsock)->ops = sock->ops;
Wei Yongjun1b085342008-12-18 19:35:10 -08003515 __module_get((*newsock)->ops->owner);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003516
3517done:
3518 return err;
3519}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003520EXPORT_SYMBOL(kernel_accept);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003521
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003522/**
3523 * kernel_connect - connect a socket (kernel space)
3524 * @sock: socket
3525 * @addr: address
3526 * @addrlen: address length
3527 * @flags: flags (O_NONBLOCK, ...)
3528 *
3529 * For datagram sockets, @addr is the addres to which datagrams are sent
3530 * by default, and the only address from which datagrams are received.
3531 * For stream sockets, attempts to connect to @addr.
3532 * Returns 0 or an error code.
3533 */
3534
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003535int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
YOSHIFUJI Hideaki4768fbc2007-02-09 23:25:31 +09003536 int flags)
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003537{
3538 return sock->ops->connect(sock, addr, addrlen, flags);
3539}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003540EXPORT_SYMBOL(kernel_connect);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003541
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003542/**
3543 * kernel_getsockname - get the address which the socket is bound (kernel space)
3544 * @sock: socket
3545 * @addr: address holder
3546 *
3547 * Fills the @addr pointer with the address which the socket is bound.
3548 * Returns 0 or an error code.
3549 */
3550
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01003551int kernel_getsockname(struct socket *sock, struct sockaddr *addr)
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003552{
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01003553 return sock->ops->getname(sock, addr, 0);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003554}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003555EXPORT_SYMBOL(kernel_getsockname);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003556
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003557/**
3558 * kernel_peername - get the address which the socket is connected (kernel space)
3559 * @sock: socket
3560 * @addr: address holder
3561 *
3562 * Fills the @addr pointer with the address which the socket is connected.
3563 * Returns 0 or an error code.
3564 */
3565
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01003566int kernel_getpeername(struct socket *sock, struct sockaddr *addr)
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003567{
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01003568 return sock->ops->getname(sock, addr, 1);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003569}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003570EXPORT_SYMBOL(kernel_getpeername);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003571
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003572/**
3573 * kernel_getsockopt - get a socket option (kernel space)
3574 * @sock: socket
3575 * @level: API level (SOL_SOCKET, ...)
3576 * @optname: option tag
3577 * @optval: option value
3578 * @optlen: option length
3579 *
3580 * Assigns the option length to @optlen.
3581 * Returns 0 or an error.
3582 */
3583
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003584int kernel_getsockopt(struct socket *sock, int level, int optname,
3585 char *optval, int *optlen)
3586{
3587 mm_segment_t oldfs = get_fs();
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003588 char __user *uoptval;
3589 int __user *uoptlen;
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003590 int err;
3591
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003592 uoptval = (char __user __force *) optval;
3593 uoptlen = (int __user __force *) optlen;
3594
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003595 set_fs(KERNEL_DS);
3596 if (level == SOL_SOCKET)
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003597 err = sock_getsockopt(sock, level, optname, uoptval, uoptlen);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003598 else
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003599 err = sock->ops->getsockopt(sock, level, optname, uoptval,
3600 uoptlen);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003601 set_fs(oldfs);
3602 return err;
3603}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003604EXPORT_SYMBOL(kernel_getsockopt);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003605
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003606/**
3607 * kernel_setsockopt - set a socket option (kernel space)
3608 * @sock: socket
3609 * @level: API level (SOL_SOCKET, ...)
3610 * @optname: option tag
3611 * @optval: option value
3612 * @optlen: option length
3613 *
3614 * Returns 0 or an error.
3615 */
3616
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003617int kernel_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07003618 char *optval, unsigned int optlen)
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003619{
3620 mm_segment_t oldfs = get_fs();
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003621 char __user *uoptval;
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003622 int err;
3623
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003624 uoptval = (char __user __force *) optval;
3625
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003626 set_fs(KERNEL_DS);
3627 if (level == SOL_SOCKET)
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003628 err = sock_setsockopt(sock, level, optname, uoptval, optlen);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003629 else
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003630 err = sock->ops->setsockopt(sock, level, optname, uoptval,
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003631 optlen);
3632 set_fs(oldfs);
3633 return err;
3634}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003635EXPORT_SYMBOL(kernel_setsockopt);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003636
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003637/**
3638 * kernel_sendpage - send a &page through a socket (kernel space)
3639 * @sock: socket
3640 * @page: page
3641 * @offset: page offset
3642 * @size: total size in bytes
3643 * @flags: flags (MSG_DONTWAIT, ...)
3644 *
3645 * Returns the total amount sent in bytes or an error.
3646 */
3647
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003648int kernel_sendpage(struct socket *sock, struct page *page, int offset,
3649 size_t size, int flags)
3650{
3651 if (sock->ops->sendpage)
3652 return sock->ops->sendpage(sock, page, offset, size, flags);
3653
3654 return sock_no_sendpage(sock, page, offset, size, flags);
3655}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003656EXPORT_SYMBOL(kernel_sendpage);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003657
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003658/**
3659 * kernel_sendpage_locked - send a &page through the locked sock (kernel space)
3660 * @sk: sock
3661 * @page: page
3662 * @offset: page offset
3663 * @size: total size in bytes
3664 * @flags: flags (MSG_DONTWAIT, ...)
3665 *
3666 * Returns the total amount sent in bytes or an error.
3667 * Caller must hold @sk.
3668 */
3669
Tom Herbert306b13e2017-07-28 16:22:41 -07003670int kernel_sendpage_locked(struct sock *sk, struct page *page, int offset,
3671 size_t size, int flags)
3672{
3673 struct socket *sock = sk->sk_socket;
3674
3675 if (sock->ops->sendpage_locked)
3676 return sock->ops->sendpage_locked(sk, page, offset, size,
3677 flags);
3678
3679 return sock_no_sendpage_locked(sk, page, offset, size, flags);
3680}
3681EXPORT_SYMBOL(kernel_sendpage_locked);
3682
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003683/**
3684 * kernel_shutdown - shut down part of a full-duplex connection (kernel space)
3685 * @sock: socket
3686 * @how: connection part
3687 *
3688 * Returns 0 or an error.
3689 */
3690
Trond Myklebust91cf45f2007-11-12 18:10:39 -08003691int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how)
3692{
3693 return sock->ops->shutdown(sock, how);
3694}
Trond Myklebust91cf45f2007-11-12 18:10:39 -08003695EXPORT_SYMBOL(kernel_sock_shutdown);
R. Parameswaran113c3072017-04-05 16:50:35 -07003696
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003697/**
3698 * kernel_sock_ip_overhead - returns the IP overhead imposed by a socket
3699 * @sk: socket
3700 *
3701 * This routine returns the IP overhead imposed by a socket i.e.
3702 * the length of the underlying IP header, depending on whether
3703 * this is an IPv4 or IPv6 socket and the length from IP options turned
3704 * on at the socket. Assumes that the caller has a lock on the socket.
R. Parameswaran113c3072017-04-05 16:50:35 -07003705 */
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003706
R. Parameswaran113c3072017-04-05 16:50:35 -07003707u32 kernel_sock_ip_overhead(struct sock *sk)
3708{
3709 struct inet_sock *inet;
3710 struct ip_options_rcu *opt;
3711 u32 overhead = 0;
R. Parameswaran113c3072017-04-05 16:50:35 -07003712#if IS_ENABLED(CONFIG_IPV6)
3713 struct ipv6_pinfo *np;
3714 struct ipv6_txoptions *optv6 = NULL;
3715#endif /* IS_ENABLED(CONFIG_IPV6) */
3716
3717 if (!sk)
3718 return overhead;
3719
R. Parameswaran113c3072017-04-05 16:50:35 -07003720 switch (sk->sk_family) {
3721 case AF_INET:
3722 inet = inet_sk(sk);
3723 overhead += sizeof(struct iphdr);
3724 opt = rcu_dereference_protected(inet->inet_opt,
stephen hemminger614d79c2017-07-24 10:25:22 -07003725 sock_owned_by_user(sk));
R. Parameswaran113c3072017-04-05 16:50:35 -07003726 if (opt)
3727 overhead += opt->opt.optlen;
3728 return overhead;
3729#if IS_ENABLED(CONFIG_IPV6)
3730 case AF_INET6:
3731 np = inet6_sk(sk);
3732 overhead += sizeof(struct ipv6hdr);
3733 if (np)
3734 optv6 = rcu_dereference_protected(np->opt,
stephen hemminger614d79c2017-07-24 10:25:22 -07003735 sock_owned_by_user(sk));
R. Parameswaran113c3072017-04-05 16:50:35 -07003736 if (optv6)
3737 overhead += (optv6->opt_flen + optv6->opt_nflen);
3738 return overhead;
3739#endif /* IS_ENABLED(CONFIG_IPV6) */
3740 default: /* Returns 0 overhead if the socket is not ipv4 or ipv6 */
3741 return overhead;
3742 }
3743}
3744EXPORT_SYMBOL(kernel_sock_ip_overhead);