blob: 603ab20123bb39469940fc2aa0f14be779e029e2 [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>
82#include <linux/security.h>
83#include <linux/syscalls.h>
84#include <linux/compat.h>
85#include <linux/kmod.h>
David Woodhouse3ec3b2f2005-05-17 12:08:48 +010086#include <linux/audit.h>
Adrian Bunkd86b5e02006-01-21 00:46:55 +010087#include <linux/wireless.h>
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -070088#include <linux/nsproxy.h>
Nick Black1fd73172009-09-22 16:43:33 -070089#include <linux/magic.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090090#include <linux/slab.h>
Masatake YAMATO600e1772012-08-29 10:44:29 +000091#include <linux/xattr.h>
Yida Wang0bf43bd2017-03-22 18:16:31 -040092#include <linux/seemp_api.h>
93#include <linux/seemp_instrumentation.h>
Jeremy Cline9a492f82018-07-27 22:43:01 +000094#include <linux/nospec.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96#include <asm/uaccess.h>
97#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>
110#include <linux/atalk.h>
Eliezer Tamir076bb0c2013-07-10 17:13:17 +0300111#include <net/busy_poll.h>
Willem de Bruijnf24b9be2014-08-04 22:11:45 -0400112#include <linux/errqueue.h>
Eliezer Tamir06021292013-06-10 11:39:50 +0300113
Cong Wange0d10952013-08-01 11:10:25 +0800114#ifdef CONFIG_NET_RX_BUSY_POLL
Eliezer Tamir64b0dc52013-07-10 17:13:36 +0300115unsigned int sysctl_net_busy_read __read_mostly;
116unsigned int sysctl_net_busy_poll __read_mostly;
Eliezer Tamir06021292013-06-10 11:39:50 +0300117#endif
Arnd Bergmann6b960182009-11-06 23:10:54 -0800118
Al Viro8ae5e032014-11-28 19:40:50 -0500119static ssize_t sock_read_iter(struct kiocb *iocb, struct iov_iter *to);
120static ssize_t sock_write_iter(struct kiocb *iocb, struct iov_iter *from);
Devi Sandeep Endluri V V25c44d12014-03-04 07:31:47 -0700121static BLOCKING_NOTIFIER_HEAD(sockev_notifier_list);
122
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700123static int sock_mmap(struct file *file, struct vm_area_struct *vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125static int sock_close(struct inode *inode, struct file *file);
126static unsigned int sock_poll(struct file *file,
127 struct poll_table_struct *wait);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700128static long sock_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
Shaun Pereira89bbfc92006-03-21 23:58:08 -0800129#ifdef CONFIG_COMPAT
130static long compat_sock_ioctl(struct file *file,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700131 unsigned int cmd, unsigned long arg);
Shaun Pereira89bbfc92006-03-21 23:58:08 -0800132#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133static int sock_fasync(int fd, struct file *filp, int on);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134static ssize_t sock_sendpage(struct file *file, struct page *page,
135 int offset, size_t size, loff_t *ppos, int more);
Jens Axboe9c55e012007-11-06 23:30:13 -0800136static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700137 struct pipe_inode_info *pipe, size_t len,
Jens Axboe9c55e012007-11-06 23:30:13 -0800138 unsigned int flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140/*
141 * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
142 * in the operation structures but are done directly via the socketcall() multiplexor.
143 */
144
Arjan van de Venda7071d2007-02-12 00:55:36 -0800145static const struct file_operations socket_file_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 .owner = THIS_MODULE,
147 .llseek = no_llseek,
Al Viro8ae5e032014-11-28 19:40:50 -0500148 .read_iter = sock_read_iter,
149 .write_iter = sock_write_iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 .poll = sock_poll,
151 .unlocked_ioctl = sock_ioctl,
Shaun Pereira89bbfc92006-03-21 23:58:08 -0800152#ifdef CONFIG_COMPAT
153 .compat_ioctl = compat_sock_ioctl,
154#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 .mmap = sock_mmap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 .release = sock_close,
157 .fasync = sock_fasync,
Jens Axboe5274f052006-03-30 15:15:30 +0200158 .sendpage = sock_sendpage,
159 .splice_write = generic_splice_sendpage,
Jens Axboe9c55e012007-11-06 23:30:13 -0800160 .splice_read = sock_splice_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161};
162
163/*
164 * The protocol list. Each protocol is registered in here.
165 */
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167static DEFINE_SPINLOCK(net_family_lock);
Eric Dumazet190683a2010-11-10 10:50:44 +0000168static const struct net_proto_family __rcu *net_families[NPROTO] __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170/*
171 * Statistics counters of the socket lists
172 */
173
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700174static DEFINE_PER_CPU(int, sockets_in_use);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176/*
Devi Sandeep Endluri V V25c44d12014-03-04 07:31:47 -0700177 * Socket Event framework helpers
178 */
179static void sockev_notify(unsigned long event, struct socket *sk)
180{
181 blocking_notifier_call_chain(&sockev_notifier_list, event, sk);
182}
183
184/**
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700185 * Support routines.
186 * Move socket addresses back and forth across the kernel/user
187 * divide and look after the messy bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 */
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190/**
191 * move_addr_to_kernel - copy a socket address into kernel space
192 * @uaddr: Address in user space
193 * @kaddr: Address in kernel space
194 * @ulen: Length in user space
195 *
196 * The address is copied into kernel space. If the provided address is
197 * too long an error code of -EINVAL is returned. If the copy gives
198 * invalid addresses -EFAULT is returned. On a success 0 is returned.
199 */
200
Maciej Żenczykowski43db3622012-03-11 12:51:50 +0000201int move_addr_to_kernel(void __user *uaddr, int ulen, struct sockaddr_storage *kaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -0700203 if (ulen < 0 || ulen > sizeof(struct sockaddr_storage))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700205 if (ulen == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 return 0;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700207 if (copy_from_user(kaddr, uaddr, ulen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 return -EFAULT;
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100209 return audit_sockaddr(ulen, kaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
211
212/**
213 * move_addr_to_user - copy an address to user space
214 * @kaddr: kernel space address
215 * @klen: length of address in kernel
216 * @uaddr: user space address
217 * @ulen: pointer to user length field
218 *
219 * The value pointed to by ulen on entry is the buffer length available.
220 * This is overwritten with the buffer space used. -EINVAL is returned
221 * if an overlong buffer is specified or a negative buffer size. -EFAULT
222 * is returned if either the buffer or the length field are not
223 * accessible.
224 * After copying the data up to the limit the user specifies, the true
225 * length of the data is written over the length limit the user
226 * specified. Zero is returned for a success.
227 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700228
Maciej Żenczykowski43db3622012-03-11 12:51:50 +0000229static int move_addr_to_user(struct sockaddr_storage *kaddr, int klen,
stephen hemminger11165f12010-10-18 14:27:29 +0000230 void __user *uaddr, int __user *ulen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
232 int err;
233 int len;
234
Hannes Frederic Sowa68c6beb2013-11-21 03:14:34 +0100235 BUG_ON(klen > sizeof(struct sockaddr_storage));
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700236 err = get_user(len, ulen);
237 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 return err;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700239 if (len > klen)
240 len = klen;
Hannes Frederic Sowa68c6beb2013-11-21 03:14:34 +0100241 if (len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700243 if (len) {
Steve Grubbd6fe3942006-03-30 12:20:22 -0500244 if (audit_sockaddr(klen, kaddr))
245 return -ENOMEM;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700246 if (copy_to_user(uaddr, kaddr, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return -EFAULT;
248 }
249 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700250 * "fromlen shall refer to the value before truncation.."
251 * 1003.1g
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 */
253 return __put_user(klen, ulen);
254}
255
Christoph Lametere18b8902006-12-06 20:33:20 -0800256static struct kmem_cache *sock_inode_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258static struct inode *sock_alloc_inode(struct super_block *sb)
259{
260 struct socket_alloc *ei;
Eric Dumazeteaefd112011-02-18 03:26:36 +0000261 struct socket_wq *wq;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700262
Christoph Lametere94b1762006-12-06 20:33:17 -0800263 ei = kmem_cache_alloc(sock_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 if (!ei)
265 return NULL;
Eric Dumazeteaefd112011-02-18 03:26:36 +0000266 wq = kmalloc(sizeof(*wq), GFP_KERNEL);
267 if (!wq) {
Eric Dumazet43815482010-04-29 11:01:49 +0000268 kmem_cache_free(sock_inode_cachep, ei);
269 return NULL;
270 }
Eric Dumazeteaefd112011-02-18 03:26:36 +0000271 init_waitqueue_head(&wq->wait);
272 wq->fasync_list = NULL;
Nicolai Stange574aab12015-12-29 13:29:55 +0100273 wq->flags = 0;
Eric Dumazeteaefd112011-02-18 03:26:36 +0000274 RCU_INIT_POINTER(ei->socket.wq, wq);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 ei->socket.state = SS_UNCONNECTED;
277 ei->socket.flags = 0;
278 ei->socket.ops = NULL;
279 ei->socket.sk = NULL;
280 ei->socket.file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 return &ei->vfs_inode;
283}
284
285static void sock_destroy_inode(struct inode *inode)
286{
Eric Dumazet43815482010-04-29 11:01:49 +0000287 struct socket_alloc *ei;
Eric Dumazeteaefd112011-02-18 03:26:36 +0000288 struct socket_wq *wq;
Eric Dumazet43815482010-04-29 11:01:49 +0000289
290 ei = container_of(inode, struct socket_alloc, vfs_inode);
Eric Dumazeteaefd112011-02-18 03:26:36 +0000291 wq = rcu_dereference_protected(ei->socket.wq, 1);
Lai Jiangshan61845222011-03-18 12:10:25 +0800292 kfree_rcu(wq, rcu);
Eric Dumazet43815482010-04-29 11:01:49 +0000293 kmem_cache_free(sock_inode_cachep, ei);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
295
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700296static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700298 struct socket_alloc *ei = (struct socket_alloc *)foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Christoph Lametera35afb82007-05-16 22:10:57 -0700300 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303static int init_inodecache(void)
304{
305 sock_inode_cachep = kmem_cache_create("sock_inode_cache",
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700306 sizeof(struct socket_alloc),
307 0,
308 (SLAB_HWCACHE_ALIGN |
309 SLAB_RECLAIM_ACCOUNT |
Vladimir Davydov5d097052016-01-14 15:18:21 -0800310 SLAB_MEM_SPREAD | SLAB_ACCOUNT),
Paul Mundt20c2df82007-07-20 10:11:58 +0900311 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 if (sock_inode_cachep == NULL)
313 return -ENOMEM;
314 return 0;
315}
316
Alexey Dobriyanb87221d2009-09-21 17:01:09 -0700317static const struct super_operations sockfs_ops = {
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700318 .alloc_inode = sock_alloc_inode,
319 .destroy_inode = sock_destroy_inode,
320 .statfs = simple_statfs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321};
322
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700323/*
324 * sockfs_dname() is called from d_path().
325 */
326static char *sockfs_dname(struct dentry *dentry, char *buffer, int buflen)
327{
328 return dynamic_dname(dentry, buffer, buflen, "socket:[%lu]",
David Howellsc5ef6032015-03-17 22:26:16 +0000329 d_inode(dentry)->i_ino);
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700330}
331
Al Viro3ba13d12009-02-20 06:02:22 +0000332static const struct dentry_operations sockfs_dentry_operations = {
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700333 .d_dname = sockfs_dname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334};
335
Andreas Gruenbacherbba0bd32016-09-29 17:48:35 +0200336static int sockfs_xattr_get(const struct xattr_handler *handler,
337 struct dentry *dentry, struct inode *inode,
338 const char *suffix, void *value, size_t size)
339{
340 if (value) {
341 if (dentry->d_name.len + 1 > size)
342 return -ERANGE;
343 memcpy(value, dentry->d_name.name, dentry->d_name.len + 1);
344 }
345 return dentry->d_name.len + 1;
346}
347
348#define XATTR_SOCKPROTONAME_SUFFIX "sockprotoname"
349#define XATTR_NAME_SOCKPROTONAME (XATTR_SYSTEM_PREFIX XATTR_SOCKPROTONAME_SUFFIX)
350#define XATTR_NAME_SOCKPROTONAME_LEN (sizeof(XATTR_NAME_SOCKPROTONAME)-1)
351
352static const struct xattr_handler sockfs_xattr_handler = {
353 .name = XATTR_NAME_SOCKPROTONAME,
354 .get = sockfs_xattr_get,
355};
356
Andreas Gruenbacher4a590152016-11-13 21:23:34 +0100357static int sockfs_security_xattr_set(const struct xattr_handler *handler,
358 struct dentry *dentry, struct inode *inode,
359 const char *suffix, const void *value,
360 size_t size, int flags)
361{
362 /* Handled by LSM. */
363 return -EAGAIN;
364}
365
366static const struct xattr_handler sockfs_security_xattr_handler = {
367 .prefix = XATTR_SECURITY_PREFIX,
368 .set = sockfs_security_xattr_set,
369};
370
Andreas Gruenbacherbba0bd32016-09-29 17:48:35 +0200371static const struct xattr_handler *sockfs_xattr_handlers[] = {
372 &sockfs_xattr_handler,
Andreas Gruenbacher4a590152016-11-13 21:23:34 +0100373 &sockfs_security_xattr_handler,
Andreas Gruenbacherbba0bd32016-09-29 17:48:35 +0200374 NULL
375};
376
Al Viroc74a1cb2011-01-12 16:59:34 -0500377static struct dentry *sockfs_mount(struct file_system_type *fs_type,
378 int flags, const char *dev_name, void *data)
379{
Andreas Gruenbacherbba0bd32016-09-29 17:48:35 +0200380 return mount_pseudo_xattr(fs_type, "socket:", &sockfs_ops,
381 sockfs_xattr_handlers,
382 &sockfs_dentry_operations, SOCKFS_MAGIC);
Al Viroc74a1cb2011-01-12 16:59:34 -0500383}
384
385static struct vfsmount *sock_mnt __read_mostly;
386
387static struct file_system_type sock_fs_type = {
388 .name = "sockfs",
389 .mount = sockfs_mount,
390 .kill_sb = kill_anon_super,
391};
392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393/*
394 * Obtains the first available file descriptor and sets it up for use.
395 *
David S. Miller39d8c1b2006-03-20 17:13:49 -0800396 * These functions create file structures and maps them to fd space
397 * of the current process. On success it returns file descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 * and file struct implicitly stored in sock->file.
399 * Note that another thread may close file descriptor before we return
400 * from this function. We use the fact that now we do not refer
401 * to socket after mapping. If one day we will need it, this
402 * function will increment ref. count on file by 1.
403 *
404 * In any case returned fd MAY BE not valid!
405 * This race condition is unavoidable
406 * with shared fd spaces, we cannot solve it inside kernel,
407 * but we take care of internal coherence yet.
408 */
409
Linus Torvaldsaab174f2012-10-02 20:25:04 -0700410struct file *sock_alloc_file(struct socket *sock, int flags, const char *dname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
Al Viro7cbe66b2009-08-05 19:59:08 +0400412 struct qstr name = { .name = "" };
Al Viro2c48b9c2009-08-09 00:52:35 +0400413 struct path path;
Al Viro7cbe66b2009-08-05 19:59:08 +0400414 struct file *file;
David S. Miller39d8c1b2006-03-20 17:13:49 -0800415
Masatake YAMATO600e1772012-08-29 10:44:29 +0000416 if (dname) {
417 name.name = dname;
418 name.len = strlen(name.name);
419 } else if (sock->sk) {
420 name.name = sock->sk->sk_prot_creator->name;
421 name.len = strlen(name.name);
422 }
Nick Piggin4b936882011-01-07 17:50:07 +1100423 path.dentry = d_alloc_pseudo(sock_mnt->mnt_sb, &name);
Al Viro28407632012-08-17 23:54:15 -0400424 if (unlikely(!path.dentry))
425 return ERR_PTR(-ENOMEM);
Al Viro2c48b9c2009-08-09 00:52:35 +0400426 path.mnt = mntget(sock_mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Al Viro2c48b9c2009-08-09 00:52:35 +0400428 d_instantiate(path.dentry, SOCK_INODE(sock));
Al Virocc3808f2009-08-06 09:43:59 +0400429
Al Viro2c48b9c2009-08-09 00:52:35 +0400430 file = alloc_file(&path, FMODE_READ | FMODE_WRITE,
Al Virocc3808f2009-08-06 09:43:59 +0400431 &socket_file_ops);
Viresh Kumarb5ffe632015-08-12 15:59:47 +0530432 if (IS_ERR(file)) {
Al Virocc3808f2009-08-06 09:43:59 +0400433 /* drop dentry, keep inode */
David Howellsc5ef6032015-03-17 22:26:16 +0000434 ihold(d_inode(path.dentry));
Al Viro2c48b9c2009-08-09 00:52:35 +0400435 path_put(&path);
Anatol Pomozov39b65252012-09-12 20:11:55 -0700436 return file;
Al Virocc3808f2009-08-06 09:43:59 +0400437 }
David S. Miller39d8c1b2006-03-20 17:13:49 -0800438
439 sock->file = file;
Ulrich Drepper77d27202008-07-23 21:29:35 -0700440 file->f_flags = O_RDWR | (flags & O_NONBLOCK);
David S. Miller39d8c1b2006-03-20 17:13:49 -0800441 file->private_data = sock;
Al Viro28407632012-08-17 23:54:15 -0400442 return file;
David S. Miller39d8c1b2006-03-20 17:13:49 -0800443}
Al Viro56b31d12012-08-18 00:25:51 -0400444EXPORT_SYMBOL(sock_alloc_file);
David S. Miller39d8c1b2006-03-20 17:13:49 -0800445
Al Viro56b31d12012-08-18 00:25:51 -0400446static int sock_map_fd(struct socket *sock, int flags)
David S. Miller39d8c1b2006-03-20 17:13:49 -0800447{
448 struct file *newfile;
Al Viro28407632012-08-17 23:54:15 -0400449 int fd = get_unused_fd_flags(flags);
450 if (unlikely(fd < 0))
451 return fd;
David S. Miller39d8c1b2006-03-20 17:13:49 -0800452
Linus Torvaldsaab174f2012-10-02 20:25:04 -0700453 newfile = sock_alloc_file(sock, flags, NULL);
Al Viro28407632012-08-17 23:54:15 -0400454 if (likely(!IS_ERR(newfile))) {
David S. Miller39d8c1b2006-03-20 17:13:49 -0800455 fd_install(fd, newfile);
Al Viro28407632012-08-17 23:54:15 -0400456 return fd;
457 }
Al Viro7cbe66b2009-08-05 19:59:08 +0400458
Al Viro28407632012-08-17 23:54:15 -0400459 put_unused_fd(fd);
460 return PTR_ERR(newfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461}
462
John Fastabend406a3c62012-07-20 10:39:25 +0000463struct socket *sock_from_file(struct file *file, int *err)
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800464{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800465 if (file->f_op == &socket_file_ops)
466 return file->private_data; /* set in sock_map_fd */
467
Eric Dumazet23bb80d2007-02-08 14:59:57 -0800468 *err = -ENOTSOCK;
469 return NULL;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800470}
John Fastabend406a3c62012-07-20 10:39:25 +0000471EXPORT_SYMBOL(sock_from_file);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473/**
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700474 * sockfd_lookup - Go from a file number to its socket slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 * @fd: file handle
476 * @err: pointer to an error code return
477 *
478 * The file handle passed in is locked and the socket it is bound
479 * too is returned. If an error occurs the err pointer is overwritten
480 * with a negative errno code and NULL is returned. The function checks
481 * for both invalid handles and passing a handle which is not a socket.
482 *
483 * On a success the socket object pointer is returned.
484 */
485
486struct socket *sockfd_lookup(int fd, int *err)
487{
488 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 struct socket *sock;
490
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700491 file = fget(fd);
492 if (!file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 *err = -EBADF;
494 return NULL;
495 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700496
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800497 sock = sock_from_file(file, err);
498 if (!sock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 fput(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 return sock;
501}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700502EXPORT_SYMBOL(sockfd_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800504static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed)
505{
Al Viro00e188e2014-03-03 23:48:18 -0500506 struct fd f = fdget(fd);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800507 struct socket *sock;
508
Hua Zhong36725582006-04-19 15:25:02 -0700509 *err = -EBADF;
Al Viro00e188e2014-03-03 23:48:18 -0500510 if (f.file) {
511 sock = sock_from_file(f.file, err);
512 if (likely(sock)) {
Miaohe Lin645086b2020-08-06 19:53:16 +0800513 *fput_needed = f.flags & FDPUT_FPUT;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800514 return sock;
Al Viro00e188e2014-03-03 23:48:18 -0500515 }
516 fdput(f);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800517 }
518 return NULL;
519}
520
Masatake YAMATO600e1772012-08-29 10:44:29 +0000521static ssize_t sockfs_listxattr(struct dentry *dentry, char *buffer,
522 size_t size)
523{
524 ssize_t len;
525 ssize_t used = 0;
526
David Howellsc5ef6032015-03-17 22:26:16 +0000527 len = security_inode_listsecurity(d_inode(dentry), buffer, size);
Masatake YAMATO600e1772012-08-29 10:44:29 +0000528 if (len < 0)
529 return len;
530 used += len;
531 if (buffer) {
532 if (size < used)
533 return -ERANGE;
534 buffer += len;
535 }
536
537 len = (XATTR_NAME_SOCKPROTONAME_LEN + 1);
538 used += len;
539 if (buffer) {
540 if (size < used)
541 return -ERANGE;
542 memcpy(buffer, XATTR_NAME_SOCKPROTONAME, len);
543 buffer += len;
544 }
545
546 return used;
547}
548
Tobias Klauserb68cf932017-01-10 09:30:51 +0100549static int sockfs_setattr(struct dentry *dentry, struct iattr *iattr)
Lorenzo Colitti81a15912016-11-04 02:23:41 +0900550{
551 int err = simple_setattr(dentry, iattr);
552
Eric Biggers1452d392016-12-30 17:42:32 -0600553 if (!err && (iattr->ia_valid & ATTR_UID)) {
Lorenzo Colitti81a15912016-11-04 02:23:41 +0900554 struct socket *sock = SOCKET_I(d_inode(dentry));
555
Cong Wang7fa8c152018-06-07 13:39:49 -0700556 if (sock->sk)
557 sock->sk->sk_uid = iattr->ia_uid;
558 else
559 err = -ENOENT;
Lorenzo Colitti81a15912016-11-04 02:23:41 +0900560 }
561
562 return err;
563}
564
Masatake YAMATO600e1772012-08-29 10:44:29 +0000565static const struct inode_operations sockfs_inode_ops = {
Masatake YAMATO600e1772012-08-29 10:44:29 +0000566 .listxattr = sockfs_listxattr,
Lorenzo Colitti81a15912016-11-04 02:23:41 +0900567 .setattr = sockfs_setattr,
Masatake YAMATO600e1772012-08-29 10:44:29 +0000568};
569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570/**
571 * sock_alloc - allocate a socket
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700572 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 * Allocate a new inode and socket object. The two are bound together
574 * and initialised. The socket is then returned. If we are out of inodes
575 * NULL is returned.
576 */
577
Tom Herbertf4a00aa2016-03-07 14:11:01 -0800578struct socket *sock_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700580 struct inode *inode;
581 struct socket *sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Eric Dumazeta209dfc2011-07-26 11:36:34 +0200583 inode = new_inode_pseudo(sock_mnt->mnt_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 if (!inode)
585 return NULL;
586
587 sock = SOCKET_I(inode);
588
Eric Dumazet29a020d2009-09-15 02:39:20 -0700589 kmemcheck_annotate_bitfield(sock, type);
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400590 inode->i_ino = get_next_ino();
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700591 inode->i_mode = S_IFSOCK | S_IRWXUGO;
David Howells8192b0c2008-11-14 10:39:10 +1100592 inode->i_uid = current_fsuid();
593 inode->i_gid = current_fsgid();
Masatake YAMATO600e1772012-08-29 10:44:29 +0000594 inode->i_op = &sockfs_inode_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Alex Shi19e8d692012-05-14 14:15:31 -0700596 this_cpu_add(sockets_in_use, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 return sock;
598}
Tom Herbertf4a00aa2016-03-07 14:11:01 -0800599EXPORT_SYMBOL(sock_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601/**
602 * sock_release - close a socket
603 * @sock: socket to close
604 *
605 * The socket is released from the protocol stack if it has a release
606 * callback, and the inode is then released if the socket is bound to
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700607 * an inode not a file.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700609
Cong Wang7fa8c152018-06-07 13:39:49 -0700610static void __sock_release(struct socket *sock, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
612 if (sock->ops) {
613 struct module *owner = sock->ops->owner;
614
Cong Wang7fa8c152018-06-07 13:39:49 -0700615 if (inode)
616 inode_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 sock->ops->release(sock);
Eric Biggers60679b32019-02-21 14:13:56 -0800618 sock->sk = NULL;
Cong Wang7fa8c152018-06-07 13:39:49 -0700619 if (inode)
620 inode_unlock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 sock->ops = NULL;
622 module_put(owner);
623 }
624
Eric Dumazeteaefd112011-02-18 03:26:36 +0000625 if (rcu_dereference_protected(sock->wq, 1)->fasync_list)
Yang Yingliang3410f222014-02-12 17:09:55 +0800626 pr_err("%s: fasync list not empty!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Alex Shi19e8d692012-05-14 14:15:31 -0700628 this_cpu_sub(sockets_in_use, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 if (!sock->file) {
630 iput(SOCK_INODE(sock));
631 return;
632 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700633 sock->file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634}
Cong Wang7fa8c152018-06-07 13:39:49 -0700635
636void sock_release(struct socket *sock)
637{
638 __sock_release(sock, NULL);
639}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700640EXPORT_SYMBOL(sock_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -0400642void __sock_tx_timestamp(__u16 tsflags, __u8 *tx_flags)
Patrick Ohly20d49472009-02-12 05:03:38 +0000643{
Eric Dumazet140c55d2014-08-06 11:49:29 +0200644 u8 flags = *tx_flags;
645
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -0400646 if (tsflags & SOF_TIMESTAMPING_TX_HARDWARE)
Eric Dumazet140c55d2014-08-06 11:49:29 +0200647 flags |= SKBTX_HW_TSTAMP;
648
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -0400649 if (tsflags & SOF_TIMESTAMPING_TX_SOFTWARE)
Eric Dumazet140c55d2014-08-06 11:49:29 +0200650 flags |= SKBTX_SW_TSTAMP;
651
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -0400652 if (tsflags & SOF_TIMESTAMPING_TX_SCHED)
Eric Dumazet140c55d2014-08-06 11:49:29 +0200653 flags |= SKBTX_SCHED_TSTAMP;
654
Eric Dumazet140c55d2014-08-06 11:49:29 +0200655 *tx_flags = flags;
Patrick Ohly20d49472009-02-12 05:03:38 +0000656}
Willem de Bruijn67cc0d42014-09-08 19:58:58 -0400657EXPORT_SYMBOL(__sock_tx_timestamp);
Patrick Ohly20d49472009-02-12 05:03:38 +0000658
Al Virod8725c82014-12-11 00:02:50 -0500659static inline int sock_sendmsg_nosec(struct socket *sock, struct msghdr *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
Al Viro01e97e62014-12-15 21:39:31 -0500661 int ret = sock->ops->sendmsg(sock, msg, msg_data_left(msg));
Al Virod8725c82014-12-11 00:02:50 -0500662 BUG_ON(ret == -EIOCBQUEUED);
663 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664}
Gu Zheng0cf00c62014-12-05 15:14:23 +0800665
Al Virod8725c82014-12-11 00:02:50 -0500666int sock_sendmsg(struct socket *sock, struct msghdr *msg)
Gu Zheng0cf00c62014-12-05 15:14:23 +0800667{
Al Virod8725c82014-12-11 00:02:50 -0500668 int err = security_socket_sendmsg(sock, msg,
Al Viro01e97e62014-12-15 21:39:31 -0500669 msg_data_left(msg));
Ying Xue1b784142015-03-02 15:37:48 +0800670
Al Virod8725c82014-12-11 00:02:50 -0500671 return err ?: sock_sendmsg_nosec(sock, msg);
Gu Zheng0cf00c62014-12-05 15:14:23 +0800672}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700673EXPORT_SYMBOL(sock_sendmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
675int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
676 struct kvec *vec, size_t num, size_t size)
677{
Al Viro6aa24812015-03-21 19:56:16 -0400678 iov_iter_kvec(&msg->msg_iter, WRITE | ITER_KVEC, vec, num, size);
Al Virod8725c82014-12-11 00:02:50 -0500679 return sock_sendmsg(sock, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700681EXPORT_SYMBOL(kernel_sendmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700683/*
684 * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
685 */
686void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
687 struct sk_buff *skb)
688{
Patrick Ohly20d49472009-02-12 05:03:38 +0000689 int need_software_tstamp = sock_flag(sk, SOCK_RCVTSTAMP);
Willem de Bruijnf24b9be2014-08-04 22:11:45 -0400690 struct scm_timestamping tss;
Patrick Ohly20d49472009-02-12 05:03:38 +0000691 int empty = 1;
692 struct skb_shared_hwtstamps *shhwtstamps =
693 skb_hwtstamps(skb);
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700694
Patrick Ohly20d49472009-02-12 05:03:38 +0000695 /* Race occurred between timestamp enabling and packet
696 receiving. Fill in the current time for now. */
697 if (need_software_tstamp && skb->tstamp.tv64 == 0)
698 __net_timestamp(skb);
699
700 if (need_software_tstamp) {
701 if (!sock_flag(sk, SOCK_RCVTSTAMPNS)) {
702 struct timeval tv;
703 skb_get_timestamp(skb, &tv);
704 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMP,
705 sizeof(tv), &tv);
706 } else {
Willem de Bruijnf24b9be2014-08-04 22:11:45 -0400707 struct timespec ts;
708 skb_get_timestampns(skb, &ts);
Patrick Ohly20d49472009-02-12 05:03:38 +0000709 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPNS,
Willem de Bruijnf24b9be2014-08-04 22:11:45 -0400710 sizeof(ts), &ts);
Patrick Ohly20d49472009-02-12 05:03:38 +0000711 }
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700712 }
Patrick Ohly20d49472009-02-12 05:03:38 +0000713
Willem de Bruijnf24b9be2014-08-04 22:11:45 -0400714 memset(&tss, 0, sizeof(tss));
Willem de Bruijnc1991052014-09-03 12:01:18 -0400715 if ((sk->sk_tsflags & SOF_TIMESTAMPING_SOFTWARE) &&
Willem de Bruijnf24b9be2014-08-04 22:11:45 -0400716 ktime_to_timespec_cond(skb->tstamp, tss.ts + 0))
Patrick Ohly20d49472009-02-12 05:03:38 +0000717 empty = 0;
Willem de Bruijn4d276eb2014-07-25 18:01:32 -0400718 if (shhwtstamps &&
Willem de Bruijnb9f40e22014-08-04 22:11:46 -0400719 (sk->sk_tsflags & SOF_TIMESTAMPING_RAW_HARDWARE) &&
Willem de Bruijnf24b9be2014-08-04 22:11:45 -0400720 ktime_to_timespec_cond(shhwtstamps->hwtstamp, tss.ts + 2))
Willem de Bruijn4d276eb2014-07-25 18:01:32 -0400721 empty = 0;
Patrick Ohly20d49472009-02-12 05:03:38 +0000722 if (!empty)
723 put_cmsg(msg, SOL_SOCKET,
Willem de Bruijnf24b9be2014-08-04 22:11:45 -0400724 SCM_TIMESTAMPING, sizeof(tss), &tss);
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700725}
Arnaldo Carvalho de Melo7c81fd82007-03-10 00:39:35 -0300726EXPORT_SYMBOL_GPL(__sock_recv_timestamp);
727
Johannes Berg6e3e9392011-11-09 10:15:42 +0100728void __sock_recv_wifi_status(struct msghdr *msg, struct sock *sk,
729 struct sk_buff *skb)
730{
731 int ack;
732
733 if (!sock_flag(sk, SOCK_WIFI_STATUS))
734 return;
735 if (!skb->wifi_acked_valid)
736 return;
737
738 ack = skb->wifi_acked;
739
740 put_cmsg(msg, SOL_SOCKET, SCM_WIFI_STATUS, sizeof(ack), &ack);
741}
742EXPORT_SYMBOL_GPL(__sock_recv_wifi_status);
743
stephen hemminger11165f12010-10-18 14:27:29 +0000744static inline void sock_recv_drops(struct msghdr *msg, struct sock *sk,
745 struct sk_buff *skb)
Neil Horman3b885782009-10-12 13:26:31 -0700746{
Eyal Birger744d5a32015-03-01 14:58:31 +0200747 if (sock_flag(sk, SOCK_RXQ_OVFL) && skb && SOCK_SKB_CB(skb)->dropcount)
Neil Horman3b885782009-10-12 13:26:31 -0700748 put_cmsg(msg, SOL_SOCKET, SO_RXQ_OVFL,
Eyal Birger744d5a32015-03-01 14:58:31 +0200749 sizeof(__u32), &SOCK_SKB_CB(skb)->dropcount);
Neil Horman3b885782009-10-12 13:26:31 -0700750}
751
Eric Dumazet767dd032010-04-28 19:14:43 +0000752void __sock_recv_ts_and_drops(struct msghdr *msg, struct sock *sk,
Neil Horman3b885782009-10-12 13:26:31 -0700753 struct sk_buff *skb)
754{
755 sock_recv_timestamp(msg, sk, skb);
756 sock_recv_drops(msg, sk, skb);
757}
Eric Dumazet767dd032010-04-28 19:14:43 +0000758EXPORT_SYMBOL_GPL(__sock_recv_ts_and_drops);
Neil Horman3b885782009-10-12 13:26:31 -0700759
Ying Xue1b784142015-03-02 15:37:48 +0800760static inline int sock_recvmsg_nosec(struct socket *sock, struct msghdr *msg,
Al Viro2da62902015-03-14 21:13:46 -0400761 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
Al Viro2da62902015-03-14 21:13:46 -0400763 return sock->ops->recvmsg(sock, msg, msg_data_left(msg), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764}
765
Al Viro2da62902015-03-14 21:13:46 -0400766int sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -0700767{
Al Viro2da62902015-03-14 21:13:46 -0400768 int err = security_socket_recvmsg(sock, msg, msg_data_left(msg), flags);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -0700769
Al Viro2da62902015-03-14 21:13:46 -0400770 return err ?: sock_recvmsg_nosec(sock, msg, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700772EXPORT_SYMBOL(sock_recvmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Martin Lucinac1249c02010-12-10 00:04:05 +0000774/**
775 * kernel_recvmsg - Receive a message from a socket (kernel space)
776 * @sock: The socket to receive the message from
777 * @msg: Received message
778 * @vec: Input s/g array for message data
779 * @num: Size of input s/g array
780 * @size: Number of bytes to read
781 * @flags: Message flags (MSG_DONTWAIT, etc...)
782 *
783 * On return the msg structure contains the scatter/gather array passed in the
784 * vec argument. The array is modified so that it consists of the unfilled
785 * portion of the original array.
786 *
787 * The returned value is the total number of bytes received, or an error.
788 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700789int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
790 struct kvec *vec, size_t num, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
792 mm_segment_t oldfs = get_fs();
793 int result;
794
Al Viro6aa24812015-03-21 19:56:16 -0400795 iov_iter_kvec(&msg->msg_iter, READ | ITER_KVEC, vec, num, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 set_fs(KERNEL_DS);
Al Viro2da62902015-03-14 21:13:46 -0400797 result = sock_recvmsg(sock, msg, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 set_fs(oldfs);
799 return result;
800}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700801EXPORT_SYMBOL(kernel_recvmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -0300803static ssize_t sock_sendpage(struct file *file, struct page *page,
804 int offset, size_t size, loff_t *ppos, int more)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
806 struct socket *sock;
807 int flags;
808
Eric Dumazetb69aee02005-09-06 14:42:45 -0700809 sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Eric Dumazet35f9c092012-04-05 03:05:35 +0000811 flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
812 /* more is a combination of MSG_MORE and MSG_SENDPAGE_NOTLAST */
813 flags |= more;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Linus Torvaldse6949582009-08-13 08:28:36 -0700815 return kernel_sendpage(sock, page, offset, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816}
817
Jens Axboe9c55e012007-11-06 23:30:13 -0800818static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700819 struct pipe_inode_info *pipe, size_t len,
Jens Axboe9c55e012007-11-06 23:30:13 -0800820 unsigned int flags)
821{
822 struct socket *sock = file->private_data;
823
Rémi Denis-Courmont997b37d2008-02-15 02:35:45 -0800824 if (unlikely(!sock->ops->splice_read))
825 return -EINVAL;
826
Jens Axboe9c55e012007-11-06 23:30:13 -0800827 return sock->ops->splice_read(sock, ppos, pipe, len, flags);
828}
829
Al Viro8ae5e032014-11-28 19:40:50 -0500830static ssize_t sock_read_iter(struct kiocb *iocb, struct iov_iter *to)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800831{
Al Viro6d652332015-01-30 16:12:56 -0500832 struct file *file = iocb->ki_filp;
833 struct socket *sock = file->private_data;
tadeusz.struk@intel.com0345f932015-03-19 12:31:25 -0700834 struct msghdr msg = {.msg_iter = *to,
835 .msg_iocb = iocb};
Al Viro8ae5e032014-11-28 19:40:50 -0500836 ssize_t res;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800837
Al Viro8ae5e032014-11-28 19:40:50 -0500838 if (file->f_flags & O_NONBLOCK)
839 msg.msg_flags = MSG_DONTWAIT;
840
841 if (iocb->ki_pos != 0)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800842 return -ESPIPE;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700843
Christoph Hellwig66ee59a2015-02-11 19:56:46 +0100844 if (!iov_iter_count(to)) /* Match SYS5 behaviour */
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800845 return 0;
846
Al Viro2da62902015-03-14 21:13:46 -0400847 res = sock_recvmsg(sock, &msg, msg.msg_flags);
Al Viro8ae5e032014-11-28 19:40:50 -0500848 *to = msg.msg_iter;
849 return res;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800850}
851
Al Viro8ae5e032014-11-28 19:40:50 -0500852static ssize_t sock_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
Al Viro6d652332015-01-30 16:12:56 -0500854 struct file *file = iocb->ki_filp;
855 struct socket *sock = file->private_data;
tadeusz.struk@intel.com0345f932015-03-19 12:31:25 -0700856 struct msghdr msg = {.msg_iter = *from,
857 .msg_iocb = iocb};
Al Viro8ae5e032014-11-28 19:40:50 -0500858 ssize_t res;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800859
Al Viro8ae5e032014-11-28 19:40:50 -0500860 if (iocb->ki_pos != 0)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800861 return -ESPIPE;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700862
Al Viro8ae5e032014-11-28 19:40:50 -0500863 if (file->f_flags & O_NONBLOCK)
864 msg.msg_flags = MSG_DONTWAIT;
865
Al Viro6d652332015-01-30 16:12:56 -0500866 if (sock->type == SOCK_SEQPACKET)
867 msg.msg_flags |= MSG_EOR;
868
Al Virod8725c82014-12-11 00:02:50 -0500869 res = sock_sendmsg(sock, &msg);
Al Viro8ae5e032014-11-28 19:40:50 -0500870 *from = msg.msg_iter;
871 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872}
873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874/*
875 * Atomic setting of ioctl hooks to avoid race
876 * with module unload.
877 */
878
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800879static DEFINE_MUTEX(br_ioctl_mutex);
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700880static int (*br_ioctl_hook) (struct net *, unsigned int cmd, void __user *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
Eric W. Biederman881d9662007-09-17 11:56:21 -0700882void brioctl_set(int (*hook) (struct net *, unsigned int, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800884 mutex_lock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 br_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800886 mutex_unlock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887}
888EXPORT_SYMBOL(brioctl_set);
889
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800890static DEFINE_MUTEX(vlan_ioctl_mutex);
Eric W. Biederman881d9662007-09-17 11:56:21 -0700891static int (*vlan_ioctl_hook) (struct net *, void __user *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Eric W. Biederman881d9662007-09-17 11:56:21 -0700893void vlan_ioctl_set(int (*hook) (struct net *, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800895 mutex_lock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 vlan_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800897 mutex_unlock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898}
899EXPORT_SYMBOL(vlan_ioctl_set);
900
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800901static DEFINE_MUTEX(dlci_ioctl_mutex);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700902static int (*dlci_ioctl_hook) (unsigned int, void __user *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700904void dlci_ioctl_set(int (*hook) (unsigned int, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800906 mutex_lock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 dlci_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800908 mutex_unlock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909}
910EXPORT_SYMBOL(dlci_ioctl_set);
911
Arnd Bergmann6b960182009-11-06 23:10:54 -0800912static long sock_do_ioctl(struct net *net, struct socket *sock,
913 unsigned int cmd, unsigned long arg)
914{
915 int err;
916 void __user *argp = (void __user *)arg;
917
918 err = sock->ops->ioctl(sock, cmd, arg);
919
920 /*
921 * If this ioctl is unknown try to hand it down
922 * to the NIC driver.
923 */
924 if (err == -ENOIOCTLCMD)
925 err = dev_ioctl(net, cmd, argp);
926
927 return err;
928}
929
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930/*
931 * With an ioctl, arg may well be a user mode pointer, but we don't know
932 * what to do with it - that's up to the protocol still.
933 */
934
935static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
936{
937 struct socket *sock;
Eric W. Biederman881d9662007-09-17 11:56:21 -0700938 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 void __user *argp = (void __user *)arg;
940 int pid, err;
Eric W. Biederman881d9662007-09-17 11:56:21 -0700941 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Eric Dumazetb69aee02005-09-06 14:42:45 -0700943 sock = file->private_data;
Eric W. Biederman881d9662007-09-17 11:56:21 -0700944 sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900945 net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
Eric W. Biederman881d9662007-09-17 11:56:21 -0700947 err = dev_ioctl(net, cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 } else
Johannes Berg3d23e342009-09-29 23:27:28 +0200949#ifdef CONFIG_WEXT_CORE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
Eric W. Biederman881d9662007-09-17 11:56:21 -0700951 err = dev_ioctl(net, cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 } else
Johannes Berg3d23e342009-09-29 23:27:28 +0200953#endif
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700954 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 case FIOSETOWN:
956 case SIOCSPGRP:
957 err = -EFAULT;
958 if (get_user(pid, (int __user *)argp))
959 break;
Jeff Laytone0b93ed2014-08-22 11:27:32 -0400960 f_setown(sock->file, pid, 1);
961 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 break;
963 case FIOGETOWN:
964 case SIOCGPGRP:
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700965 err = put_user(f_getown(sock->file),
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700966 (int __user *)argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 break;
968 case SIOCGIFBR:
969 case SIOCSIFBR:
970 case SIOCBRADDBR:
971 case SIOCBRDELBR:
972 err = -ENOPKG;
973 if (!br_ioctl_hook)
974 request_module("bridge");
975
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800976 mutex_lock(&br_ioctl_mutex);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700977 if (br_ioctl_hook)
Eric W. Biederman881d9662007-09-17 11:56:21 -0700978 err = br_ioctl_hook(net, cmd, argp);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800979 mutex_unlock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 break;
981 case SIOCGIFVLAN:
982 case SIOCSIFVLAN:
983 err = -ENOPKG;
984 if (!vlan_ioctl_hook)
985 request_module("8021q");
986
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800987 mutex_lock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 if (vlan_ioctl_hook)
Eric W. Biederman881d9662007-09-17 11:56:21 -0700989 err = vlan_ioctl_hook(net, argp);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800990 mutex_unlock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 case SIOCADDDLCI:
993 case SIOCDELDLCI:
994 err = -ENOPKG;
995 if (!dlci_ioctl_hook)
996 request_module("dlci");
997
Pavel Emelyanov7512cbf2008-03-21 15:58:52 -0700998 mutex_lock(&dlci_ioctl_mutex);
999 if (dlci_ioctl_hook)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 err = dlci_ioctl_hook(cmd, argp);
Pavel Emelyanov7512cbf2008-03-21 15:58:52 -07001001 mutex_unlock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 break;
1003 default:
Arnd Bergmann6b960182009-11-06 23:10:54 -08001004 err = sock_do_ioctl(net, sock, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 break;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 return err;
1008}
1009
1010int sock_create_lite(int family, int type, int protocol, struct socket **res)
1011{
1012 int err;
1013 struct socket *sock = NULL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 err = security_socket_create(family, type, protocol, 1);
1016 if (err)
1017 goto out;
1018
1019 sock = sock_alloc();
1020 if (!sock) {
1021 err = -ENOMEM;
1022 goto out;
1023 }
1024
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 sock->type = type;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001026 err = security_socket_post_create(sock, family, type, protocol, 1);
1027 if (err)
1028 goto out_release;
1029
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030out:
1031 *res = sock;
1032 return err;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001033out_release:
1034 sock_release(sock);
1035 sock = NULL;
1036 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001038EXPORT_SYMBOL(sock_create_lite);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
1040/* No kernel lock held - perfect */
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001041static unsigned int sock_poll(struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
Eliezer Tamircbf55002013-07-08 16:20:34 +03001043 unsigned int busy_flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 struct socket *sock;
1045
1046 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001047 * We can't return errors to poll, so it's either yes or no.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 */
Eric Dumazetb69aee02005-09-06 14:42:45 -07001049 sock = file->private_data;
Eliezer Tamir2d48d672013-06-24 10:28:03 +03001050
Eliezer Tamircbf55002013-07-08 16:20:34 +03001051 if (sk_can_busy_loop(sock->sk)) {
Eliezer Tamir2d48d672013-06-24 10:28:03 +03001052 /* this socket can poll_ll so tell the system call */
Eliezer Tamircbf55002013-07-08 16:20:34 +03001053 busy_flag = POLL_BUSY_LOOP;
Eliezer Tamir2d48d672013-06-24 10:28:03 +03001054
1055 /* once, only if requested by syscall */
Eliezer Tamircbf55002013-07-08 16:20:34 +03001056 if (wait && (wait->_key & POLL_BUSY_LOOP))
1057 sk_busy_loop(sock->sk, 1);
Eliezer Tamir2d48d672013-06-24 10:28:03 +03001058 }
1059
Eliezer Tamircbf55002013-07-08 16:20:34 +03001060 return busy_flag | sock->ops->poll(file, sock, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061}
1062
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001063static int sock_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
Eric Dumazetb69aee02005-09-06 14:42:45 -07001065 struct socket *sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
1067 return sock->ops->mmap(file, sock, vma);
1068}
1069
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001070static int sock_close(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
Cong Wang7fa8c152018-06-07 13:39:49 -07001072 __sock_release(SOCKET_I(inode), inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 return 0;
1074}
1075
1076/*
1077 * Update the socket async list
1078 *
1079 * Fasync_list locking strategy.
1080 *
1081 * 1. fasync_list is modified only under process context socket lock
1082 * i.e. under semaphore.
1083 * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
Eric Dumazet989a2972010-04-14 09:55:35 +00001084 * or under socket lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 */
1086
1087static int sock_fasync(int fd, struct file *filp, int on)
1088{
Eric Dumazet989a2972010-04-14 09:55:35 +00001089 struct socket *sock = filp->private_data;
1090 struct sock *sk = sock->sk;
Eric Dumazeteaefd112011-02-18 03:26:36 +00001091 struct socket_wq *wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Eric Dumazet989a2972010-04-14 09:55:35 +00001093 if (sk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
1096 lock_sock(sk);
Hannes Frederic Sowa1e1d04e2016-04-05 17:10:15 +02001097 wq = rcu_dereference_protected(sock->wq, lockdep_sock_is_held(sk));
Eric Dumazeteaefd112011-02-18 03:26:36 +00001098 fasync_helper(fd, filp, on, &wq->fasync_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Eric Dumazeteaefd112011-02-18 03:26:36 +00001100 if (!wq->fasync_list)
Eric Dumazet989a2972010-04-14 09:55:35 +00001101 sock_reset_flag(sk, SOCK_FASYNC);
Jonathan Corbet76398422009-02-01 14:26:59 -07001102 else
Eric Dumazetbcdce712009-10-06 17:28:29 -07001103 sock_set_flag(sk, SOCK_FASYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Eric Dumazet989a2972010-04-14 09:55:35 +00001105 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 return 0;
1107}
1108
Eric Dumazetceb5d582015-11-29 20:03:11 -08001109/* This function may be called only under rcu_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
Eric Dumazetceb5d582015-11-29 20:03:11 -08001111int sock_wake_async(struct socket_wq *wq, int how, int band)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112{
Eric Dumazetceb5d582015-11-29 20:03:11 -08001113 if (!wq || !wq->fasync_list)
1114 return -1;
Eric Dumazet43815482010-04-29 11:01:49 +00001115
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001116 switch (how) {
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001117 case SOCK_WAKE_WAITD:
Eric Dumazetceb5d582015-11-29 20:03:11 -08001118 if (test_bit(SOCKWQ_ASYNC_WAITDATA, &wq->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 break;
1120 goto call_kill;
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001121 case SOCK_WAKE_SPACE:
Eric Dumazetceb5d582015-11-29 20:03:11 -08001122 if (!test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 break;
1124 /* fall through */
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001125 case SOCK_WAKE_IO:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001126call_kill:
Eric Dumazet43815482010-04-29 11:01:49 +00001127 kill_fasync(&wq->fasync_list, SIGIO, band);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 break;
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001129 case SOCK_WAKE_URG:
Eric Dumazet43815482010-04-29 11:01:49 +00001130 kill_fasync(&wq->fasync_list, SIGURG, band);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 }
Eric Dumazetceb5d582015-11-29 20:03:11 -08001132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 return 0;
1134}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001135EXPORT_SYMBOL(sock_wake_async);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Pavel Emelyanov721db932010-09-29 16:06:32 +04001137int __sock_create(struct net *net, int family, int type, int protocol,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001138 struct socket **res, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139{
1140 int err;
1141 struct socket *sock;
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001142 const struct net_proto_family *pf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
1144 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001145 * Check protocol is in range
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 */
1147 if (family < 0 || family >= NPROTO)
1148 return -EAFNOSUPPORT;
1149 if (type < 0 || type >= SOCK_MAX)
1150 return -EINVAL;
1151
1152 /* Compatibility.
1153
1154 This uglymoron is moved from INET layer to here to avoid
1155 deadlock in module load.
1156 */
1157 if (family == PF_INET && type == SOCK_PACKET) {
liping.zhangf3c98692016-03-11 23:08:36 +08001158 pr_info_once("%s uses obsolete (PF_INET,SOCK_PACKET)\n",
1159 current->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 family = PF_PACKET;
1161 }
1162
1163 err = security_socket_create(family, type, protocol, kern);
1164 if (err)
1165 return err;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001166
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001167 /*
1168 * Allocate the socket and allow the family to set things up. if
1169 * the protocol is 0, the family is instructed to select an appropriate
1170 * default.
1171 */
1172 sock = sock_alloc();
1173 if (!sock) {
Joe Perchese87cc472012-05-13 21:56:26 +00001174 net_warn_ratelimited("socket: no more sockets\n");
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001175 return -ENFILE; /* Not exactly a match, but its the
1176 closest posix thing */
1177 }
1178
1179 sock->type = type;
1180
Johannes Berg95a5afc2008-10-16 15:24:51 -07001181#ifdef CONFIG_MODULES
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001182 /* Attempt to load a protocol module if the find failed.
1183 *
1184 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 * requested real, full-featured networking support upon configuration.
1186 * Otherwise module support will break!
1187 */
Eric Dumazet190683a2010-11-10 10:50:44 +00001188 if (rcu_access_pointer(net_families[family]) == NULL)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001189 request_module("net-pf-%d", family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190#endif
1191
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001192 rcu_read_lock();
1193 pf = rcu_dereference(net_families[family]);
1194 err = -EAFNOSUPPORT;
1195 if (!pf)
1196 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
1198 /*
1199 * We will call the ->create function, that possibly is in a loadable
1200 * module, so we have to bump that loadable module refcnt first.
1201 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001202 if (!try_module_get(pf->owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 goto out_release;
1204
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001205 /* Now protected by module ref count */
1206 rcu_read_unlock();
1207
Eric Paris3f378b62009-11-05 22:18:14 -08001208 err = pf->create(net, sock, protocol, kern);
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001209 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 goto out_module_put;
Frank Filza79af592005-09-27 15:23:38 -07001211
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 /*
1213 * Now to bump the refcnt of the [loadable] module that owns this
1214 * socket at sock_release time we decrement its refcnt.
1215 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001216 if (!try_module_get(sock->ops->owner))
1217 goto out_module_busy;
1218
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 /*
1220 * Now that we're done with the ->create function, the [loadable]
1221 * module can have its refcnt decremented
1222 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001223 module_put(pf->owner);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001224 err = security_socket_post_create(sock, family, type, protocol, kern);
1225 if (err)
Herbert Xu3b185522007-08-15 14:46:02 -07001226 goto out_sock_release;
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001227 *res = sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001229 return 0;
1230
1231out_module_busy:
1232 err = -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233out_module_put:
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001234 sock->ops = NULL;
1235 module_put(pf->owner);
1236out_sock_release:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 sock_release(sock);
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001238 return err;
1239
1240out_release:
1241 rcu_read_unlock();
1242 goto out_sock_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243}
Pavel Emelyanov721db932010-09-29 16:06:32 +04001244EXPORT_SYMBOL(__sock_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
1246int sock_create(int family, int type, int protocol, struct socket **res)
1247{
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001248 return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001250EXPORT_SYMBOL(sock_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Eric W. Biedermaneeb1bd52015-05-08 21:08:05 -05001252int sock_create_kern(struct net *net, int family, int type, int protocol, struct socket **res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253{
Eric W. Biedermaneeb1bd52015-05-08 21:08:05 -05001254 return __sock_create(net, family, type, protocol, res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001256EXPORT_SYMBOL(sock_create_kern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001258SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259{
1260 int retval;
1261 struct socket *sock;
Ulrich Dreppera677a032008-07-23 21:29:17 -07001262 int flags;
1263
Ulrich Dreppere38b36f2008-07-23 21:29:42 -07001264 /* Check the SOCK_* constants for consistency. */
1265 BUILD_BUG_ON(SOCK_CLOEXEC != O_CLOEXEC);
1266 BUILD_BUG_ON((SOCK_MAX | SOCK_TYPE_MASK) != SOCK_TYPE_MASK);
1267 BUILD_BUG_ON(SOCK_CLOEXEC & SOCK_TYPE_MASK);
1268 BUILD_BUG_ON(SOCK_NONBLOCK & SOCK_TYPE_MASK);
1269
Ulrich Dreppera677a032008-07-23 21:29:17 -07001270 flags = type & ~SOCK_TYPE_MASK;
Ulrich Drepper77d27202008-07-23 21:29:35 -07001271 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Dreppera677a032008-07-23 21:29:17 -07001272 return -EINVAL;
1273 type &= SOCK_TYPE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001275 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1276 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1277
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 retval = sock_create(family, type, protocol, &sock);
1279 if (retval < 0)
1280 goto out;
1281
Devi Sandeep Endluri V V25c44d12014-03-04 07:31:47 -07001282 if (retval == 0)
1283 sockev_notify(SOCKEV_SOCKET, sock);
1284
Ulrich Drepper77d27202008-07-23 21:29:35 -07001285 retval = sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 if (retval < 0)
1287 goto out_release;
1288
1289out:
1290 /* It may be already another descriptor 8) Not kernel problem. */
1291 return retval;
1292
1293out_release:
1294 sock_release(sock);
1295 return retval;
1296}
1297
1298/*
1299 * Create a pair of connected sockets.
1300 */
1301
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001302SYSCALL_DEFINE4(socketpair, int, family, int, type, int, protocol,
1303 int __user *, usockvec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304{
1305 struct socket *sock1, *sock2;
1306 int fd1, fd2, err;
Al Virodb349502007-02-07 01:48:00 -05001307 struct file *newfile1, *newfile2;
Ulrich Dreppera677a032008-07-23 21:29:17 -07001308 int flags;
1309
1310 flags = type & ~SOCK_TYPE_MASK;
Ulrich Drepper77d27202008-07-23 21:29:35 -07001311 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Dreppera677a032008-07-23 21:29:17 -07001312 return -EINVAL;
1313 type &= SOCK_TYPE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001315 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1316 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1317
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 /*
1319 * Obtain the first socket and check if the underlying protocol
1320 * supports the socketpair call.
1321 */
1322
1323 err = sock_create(family, type, protocol, &sock1);
1324 if (err < 0)
1325 goto out;
1326
1327 err = sock_create(family, type, protocol, &sock2);
1328 if (err < 0)
1329 goto out_release_1;
1330
1331 err = sock1->ops->socketpair(sock1, sock2);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001332 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 goto out_release_both;
1334
Al Viro28407632012-08-17 23:54:15 -04001335 fd1 = get_unused_fd_flags(flags);
David S. Millerbf3c23d2007-10-29 21:54:02 -07001336 if (unlikely(fd1 < 0)) {
1337 err = fd1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 goto out_release_both;
David S. Millerbf3c23d2007-10-29 21:54:02 -07001339 }
Yann Droneaudd73aa282013-12-09 22:42:20 +01001340
Al Viro28407632012-08-17 23:54:15 -04001341 fd2 = get_unused_fd_flags(flags);
Al Viro198de4d2009-08-05 19:29:23 +04001342 if (unlikely(fd2 < 0)) {
1343 err = fd2;
Yann Droneaudd73aa282013-12-09 22:42:20 +01001344 goto out_put_unused_1;
Al Viro28407632012-08-17 23:54:15 -04001345 }
1346
Linus Torvaldsaab174f2012-10-02 20:25:04 -07001347 newfile1 = sock_alloc_file(sock1, flags, NULL);
Viresh Kumarb5ffe632015-08-12 15:59:47 +05301348 if (IS_ERR(newfile1)) {
Al Viro28407632012-08-17 23:54:15 -04001349 err = PTR_ERR(newfile1);
Yann Droneaudd73aa282013-12-09 22:42:20 +01001350 goto out_put_unused_both;
Al Viro28407632012-08-17 23:54:15 -04001351 }
1352
Linus Torvaldsaab174f2012-10-02 20:25:04 -07001353 newfile2 = sock_alloc_file(sock2, flags, NULL);
Al Viro28407632012-08-17 23:54:15 -04001354 if (IS_ERR(newfile2)) {
1355 err = PTR_ERR(newfile2);
Yann Droneaudd73aa282013-12-09 22:42:20 +01001356 goto out_fput_1;
Al Virodb349502007-02-07 01:48:00 -05001357 }
1358
Yann Droneaudd73aa282013-12-09 22:42:20 +01001359 err = put_user(fd1, &usockvec[0]);
1360 if (err)
1361 goto out_fput_both;
1362
1363 err = put_user(fd2, &usockvec[1]);
1364 if (err)
1365 goto out_fput_both;
1366
Al Viro157cf642008-12-14 04:57:47 -05001367 audit_fd_pair(fd1, fd2);
Yann Droneaudd73aa282013-12-09 22:42:20 +01001368
Al Virodb349502007-02-07 01:48:00 -05001369 fd_install(fd1, newfile1);
1370 fd_install(fd2, newfile2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 /* fd1 and fd2 may be already another descriptors.
1372 * Not kernel problem.
1373 */
1374
Yann Droneaudd73aa282013-12-09 22:42:20 +01001375 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
Yann Droneaudd73aa282013-12-09 22:42:20 +01001377out_fput_both:
1378 fput(newfile2);
1379 fput(newfile1);
1380 put_unused_fd(fd2);
1381 put_unused_fd(fd1);
1382 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
Yann Droneaudd73aa282013-12-09 22:42:20 +01001384out_fput_1:
1385 fput(newfile1);
1386 put_unused_fd(fd2);
1387 put_unused_fd(fd1);
1388 sock_release(sock2);
1389 goto out;
1390
1391out_put_unused_both:
1392 put_unused_fd(fd2);
1393out_put_unused_1:
1394 put_unused_fd(fd1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395out_release_both:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001396 sock_release(sock2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397out_release_1:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001398 sock_release(sock1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399out:
1400 return err;
1401}
1402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403/*
1404 * Bind a name to a socket. Nothing much to do here since it's
1405 * the protocol's responsibility to handle the local address.
1406 *
1407 * We move the socket address to kernel space before we call
1408 * the protocol layer (having also checked the address is ok).
1409 */
1410
Heiko Carstens20f37032009-01-14 14:14:23 +01001411SYSCALL_DEFINE3(bind, int, fd, struct sockaddr __user *, umyaddr, int, addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412{
1413 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001414 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001415 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001417 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001418 if (sock) {
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001419 err = move_addr_to_kernel(umyaddr, addrlen, &address);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001420 if (err >= 0) {
1421 err = security_socket_bind(sock,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001422 (struct sockaddr *)&address,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001423 addrlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001424 if (!err)
1425 err = sock->ops->bind(sock,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001426 (struct sockaddr *)
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001427 &address, addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 }
Devi Sandeep Endluri V V25c44d12014-03-04 07:31:47 -07001429 if (!err)
1430 sockev_notify(SOCKEV_BIND, sock);
Sharath Chandra Vurukala1ec72de2020-09-04 20:20:25 +05301431
1432 fput_light(sock->file, fput_needed);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 return err;
1435}
1436
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437/*
1438 * Perform a listen. Basically, we allow the protocol to do anything
1439 * necessary for a listen, and if that works, we mark the socket as
1440 * ready for listening.
1441 */
1442
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001443SYSCALL_DEFINE2(listen, int, fd, int, backlog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444{
1445 struct socket *sock;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001446 int err, fput_needed;
Pavel Emelyanovb8e1f9b2007-12-08 00:12:33 -08001447 int somaxconn;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001448
1449 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1450 if (sock) {
Kuniyuki Iwashima29e01222022-08-23 10:47:00 -07001451 somaxconn = READ_ONCE(sock_net(sock->sk)->core.sysctl_somaxconn);
Eric Dumazet95c96172012-04-15 05:58:06 +00001452 if ((unsigned int)backlog > somaxconn)
Pavel Emelyanovb8e1f9b2007-12-08 00:12:33 -08001453 backlog = somaxconn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
1455 err = security_socket_listen(sock, backlog);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001456 if (!err)
1457 err = sock->ops->listen(sock, backlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
Devi Sandeep Endluri V V25c44d12014-03-04 07:31:47 -07001459 if (!err)
1460 sockev_notify(SOCKEV_LISTEN, sock);
Sharath Chandra Vurukala1ec72de2020-09-04 20:20:25 +05301461
1462 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 }
1464 return err;
1465}
1466
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467/*
1468 * For accept, we attempt to create a new socket, set up the link
1469 * with the client, wake up the client, then return the new
1470 * connected fd. We collect the address of the connector in kernel
1471 * space and move it to user at the very end. This is unclean because
1472 * we open the socket then return an error.
1473 *
1474 * 1003.1g adds the ability to recvmsg() to query connection pending
1475 * status to recvmsg. We need to add that support in a way thats
1476 * clean when we restucture accept also.
1477 */
1478
Heiko Carstens20f37032009-01-14 14:14:23 +01001479SYSCALL_DEFINE4(accept4, int, fd, struct sockaddr __user *, upeer_sockaddr,
1480 int __user *, upeer_addrlen, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481{
1482 struct socket *sock, *newsock;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001483 struct file *newfile;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001484 int err, len, newfd, fput_needed;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001485 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Ulrich Drepper77d27202008-07-23 21:29:35 -07001487 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001488 return -EINVAL;
1489
1490 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1491 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1492
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001493 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 if (!sock)
1495 goto out;
1496
1497 err = -ENFILE;
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001498 newsock = sock_alloc();
1499 if (!newsock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 goto out_put;
1501
1502 newsock->type = sock->type;
1503 newsock->ops = sock->ops;
1504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 /*
1506 * We don't need try_module_get here, as the listening socket (sock)
1507 * has the protocol module (sock->ops->owner) held.
1508 */
1509 __module_get(newsock->ops->owner);
1510
Al Viro28407632012-08-17 23:54:15 -04001511 newfd = get_unused_fd_flags(flags);
David S. Miller39d8c1b2006-03-20 17:13:49 -08001512 if (unlikely(newfd < 0)) {
1513 err = newfd;
David S. Miller9a1875e2006-04-01 12:48:36 -08001514 sock_release(newsock);
1515 goto out_put;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001516 }
Linus Torvaldsaab174f2012-10-02 20:25:04 -07001517 newfile = sock_alloc_file(newsock, flags, sock->sk->sk_prot_creator->name);
Viresh Kumarb5ffe632015-08-12 15:59:47 +05301518 if (IS_ERR(newfile)) {
Al Viro28407632012-08-17 23:54:15 -04001519 err = PTR_ERR(newfile);
1520 put_unused_fd(newfd);
1521 sock_release(newsock);
1522 goto out_put;
1523 }
David S. Miller39d8c1b2006-03-20 17:13:49 -08001524
Frank Filza79af592005-09-27 15:23:38 -07001525 err = security_socket_accept(sock, newsock);
1526 if (err)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001527 goto out_fd;
Frank Filza79af592005-09-27 15:23:38 -07001528
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 err = sock->ops->accept(sock, newsock, sock->file->f_flags);
1530 if (err < 0)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001531 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
1533 if (upeer_sockaddr) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001534 if (newsock->ops->getname(newsock, (struct sockaddr *)&address,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001535 &len, 2) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 err = -ECONNABORTED;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001537 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 }
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001539 err = move_addr_to_user(&address,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001540 len, upeer_sockaddr, upeer_addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 if (err < 0)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001542 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 }
1544
1545 /* File flags are not inherited via accept() unlike another OSes. */
1546
David S. Miller39d8c1b2006-03-20 17:13:49 -08001547 fd_install(newfd, newfile);
1548 err = newfd;
Devi Sandeep Endluri V V25c44d12014-03-04 07:31:47 -07001549 if (!err)
1550 sockev_notify(SOCKEV_ACCEPT, sock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001552 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553out:
1554 return err;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001555out_fd:
David S. Miller9606a212006-04-01 01:00:14 -08001556 fput(newfile);
David S. Miller39d8c1b2006-03-20 17:13:49 -08001557 put_unused_fd(newfd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 goto out_put;
1559}
1560
Heiko Carstens20f37032009-01-14 14:14:23 +01001561SYSCALL_DEFINE3(accept, int, fd, struct sockaddr __user *, upeer_sockaddr,
1562 int __user *, upeer_addrlen)
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001563{
Ulrich Drepperde11def2008-11-19 15:36:14 -08001564 return sys_accept4(fd, upeer_sockaddr, upeer_addrlen, 0);
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001565}
1566
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567/*
1568 * Attempt to connect to a socket with the server address. The address
1569 * is in user space so we verify it is OK and move it to kernel space.
1570 *
1571 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
1572 * break bindings
1573 *
1574 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
1575 * other SEQPACKET protocols that take time to connect() as it doesn't
1576 * include the -EINPROGRESS status for such sockets.
1577 */
1578
Heiko Carstens20f37032009-01-14 14:14:23 +01001579SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
1580 int, addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581{
1582 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001583 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001584 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001586 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 if (!sock)
1588 goto out;
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001589 err = move_addr_to_kernel(uservaddr, addrlen, &address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 if (err < 0)
1591 goto out_put;
1592
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001593 err =
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001594 security_socket_connect(sock, (struct sockaddr *)&address, addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 if (err)
1596 goto out_put;
1597
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001598 err = sock->ops->connect(sock, (struct sockaddr *)&address, addrlen,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 sock->file->f_flags);
Devi Sandeep Endluri V V25c44d12014-03-04 07:31:47 -07001600 if (!err)
1601 sockev_notify(SOCKEV_CONNECT, sock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001603 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604out:
1605 return err;
1606}
1607
1608/*
1609 * Get the local address ('name') of a socket object. Move the obtained
1610 * name to user space.
1611 */
1612
Heiko Carstens20f37032009-01-14 14:14:23 +01001613SYSCALL_DEFINE3(getsockname, int, fd, struct sockaddr __user *, usockaddr,
1614 int __user *, usockaddr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615{
1616 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001617 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001618 int len, err, fput_needed;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001619
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001620 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 if (!sock)
1622 goto out;
1623
1624 err = security_socket_getsockname(sock);
1625 if (err)
1626 goto out_put;
1627
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001628 err = sock->ops->getname(sock, (struct sockaddr *)&address, &len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 if (err)
1630 goto out_put;
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001631 err = move_addr_to_user(&address, len, usockaddr, usockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
1633out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001634 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635out:
1636 return err;
1637}
1638
1639/*
1640 * Get the remote address ('name') of a socket object. Move the obtained
1641 * name to user space.
1642 */
1643
Heiko Carstens20f37032009-01-14 14:14:23 +01001644SYSCALL_DEFINE3(getpeername, int, fd, struct sockaddr __user *, usockaddr,
1645 int __user *, usockaddr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646{
1647 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001648 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001649 int len, err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001651 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1652 if (sock != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 err = security_socket_getpeername(sock);
1654 if (err) {
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001655 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 return err;
1657 }
1658
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001659 err =
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001660 sock->ops->getname(sock, (struct sockaddr *)&address, &len,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001661 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 if (!err)
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001663 err = move_addr_to_user(&address, len, usockaddr,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001664 usockaddr_len);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001665 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 }
1667 return err;
1668}
1669
1670/*
1671 * Send a datagram to a given address. We move the address into kernel
1672 * space and check the user space data area is readable before invoking
1673 * the protocol.
1674 */
1675
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001676SYSCALL_DEFINE6(sendto, int, fd, void __user *, buff, size_t, len,
Eric Dumazet95c96172012-04-15 05:58:06 +00001677 unsigned int, flags, struct sockaddr __user *, addr,
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001678 int, addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679{
1680 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001681 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 int err;
1683 struct msghdr msg;
1684 struct iovec iov;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001685 int fput_needed;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001686
Yida Wang0bf43bd2017-03-22 18:16:31 -04001687 seemp_logk_sendto(fd, buff, len, flags, addr, addr_len);
1688
Al Viro602bd0e2015-03-21 19:12:32 -04001689 err = import_single_range(WRITE, buff, len, &iov, &msg.msg_iter);
1690 if (unlikely(err))
1691 return err;
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001692 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1693 if (!sock)
David S. Miller4387ff72007-02-08 15:06:08 -08001694 goto out;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001695
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001696 msg.msg_name = NULL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001697 msg.msg_control = NULL;
1698 msg.msg_controllen = 0;
1699 msg.msg_namelen = 0;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001700 if (addr) {
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001701 err = move_addr_to_kernel(addr, addr_len, &address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 if (err < 0)
1703 goto out_put;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001704 msg.msg_name = (struct sockaddr *)&address;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001705 msg.msg_namelen = addr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 }
1707 if (sock->file->f_flags & O_NONBLOCK)
1708 flags |= MSG_DONTWAIT;
1709 msg.msg_flags = flags;
Al Virod8725c82014-12-11 00:02:50 -05001710 err = sock_sendmsg(sock, &msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001712out_put:
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001713 fput_light(sock->file, fput_needed);
David S. Miller4387ff72007-02-08 15:06:08 -08001714out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 return err;
1716}
1717
1718/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001719 * Send a datagram down a socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 */
1721
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001722SYSCALL_DEFINE4(send, int, fd, void __user *, buff, size_t, len,
Eric Dumazet95c96172012-04-15 05:58:06 +00001723 unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724{
1725 return sys_sendto(fd, buff, len, flags, NULL, 0);
1726}
1727
1728/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001729 * Receive a frame from the socket and optionally record the address of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 * sender. We verify the buffers are writable and if needed move the
1731 * sender address from kernel to user space.
1732 */
1733
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001734SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, ubuf, size_t, size,
Eric Dumazet95c96172012-04-15 05:58:06 +00001735 unsigned int, flags, struct sockaddr __user *, addr,
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001736 int __user *, addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737{
1738 struct socket *sock;
1739 struct iovec iov;
1740 struct msghdr msg;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001741 struct sockaddr_storage address;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001742 int err, err2;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001743 int fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
Al Viro602bd0e2015-03-21 19:12:32 -04001745 err = import_single_range(READ, ubuf, size, &iov, &msg.msg_iter);
1746 if (unlikely(err))
1747 return err;
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001748 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 if (!sock)
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001750 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001752 msg.msg_control = NULL;
1753 msg.msg_controllen = 0;
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01001754 /* Save some cycles and don't copy the address if not needed */
1755 msg.msg_name = addr ? (struct sockaddr *)&address : NULL;
1756 /* We assume all kernel code knows the size of sockaddr_storage */
1757 msg.msg_namelen = 0;
tadeusz.struk@intel.com130ed5d2015-12-15 10:46:17 -08001758 msg.msg_iocb = NULL;
Alexander Potapenkoae0ebdb2017-03-08 18:08:16 +01001759 msg.msg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 if (sock->file->f_flags & O_NONBLOCK)
1761 flags |= MSG_DONTWAIT;
Al Viro2da62902015-03-14 21:13:46 -04001762 err = sock_recvmsg(sock, &msg, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001764 if (err >= 0 && addr != NULL) {
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001765 err2 = move_addr_to_user(&address,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001766 msg.msg_namelen, addr, addr_len);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001767 if (err2 < 0)
1768 err = err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 }
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001770
1771 fput_light(sock->file, fput_needed);
David S. Miller4387ff72007-02-08 15:06:08 -08001772out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 return err;
1774}
1775
1776/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001777 * Receive a datagram from a socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 */
1779
Jan Glauberb7c0ddf2014-04-16 09:32:48 +02001780SYSCALL_DEFINE4(recv, int, fd, void __user *, ubuf, size_t, size,
1781 unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782{
1783 return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
1784}
1785
1786/*
1787 * Set a socket option. Because we don't know the option lengths we have
1788 * to pass the user mode parameter for the protocols to sort out.
1789 */
1790
Heiko Carstens20f37032009-01-14 14:14:23 +01001791SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname,
1792 char __user *, optval, int, optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001794 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 struct socket *sock;
1796
1797 if (optlen < 0)
1798 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001799
1800 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1801 if (sock != NULL) {
1802 err = security_socket_setsockopt(sock, level, optname);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001803 if (err)
1804 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
1806 if (level == SOL_SOCKET)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001807 err =
1808 sock_setsockopt(sock, level, optname, optval,
1809 optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 else
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001811 err =
1812 sock->ops->setsockopt(sock, level, optname, optval,
1813 optlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001814out_put:
1815 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 }
1817 return err;
1818}
1819
1820/*
1821 * Get a socket option. Because we don't know the option lengths we have
1822 * to pass a user mode parameter for the protocols to sort out.
1823 */
1824
Heiko Carstens20f37032009-01-14 14:14:23 +01001825SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname,
1826 char __user *, optval, int __user *, optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001828 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 struct socket *sock;
1830
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001831 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1832 if (sock != NULL) {
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001833 err = security_socket_getsockopt(sock, level, optname);
1834 if (err)
1835 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
1837 if (level == SOL_SOCKET)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001838 err =
1839 sock_getsockopt(sock, level, optname, optval,
1840 optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 else
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001842 err =
1843 sock->ops->getsockopt(sock, level, optname, optval,
1844 optlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001845out_put:
1846 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 }
1848 return err;
1849}
1850
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851/*
1852 * Shutdown a socket.
1853 */
1854
Heiko Carstens754fe8d2009-01-14 14:14:09 +01001855SYSCALL_DEFINE2(shutdown, int, fd, int, how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001857 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 struct socket *sock;
1859
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001860 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1861 if (sock != NULL) {
Devi Sandeep Endluri V V25c44d12014-03-04 07:31:47 -07001862 sockev_notify(SOCKEV_SHUTDOWN, sock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 err = security_socket_shutdown(sock, how);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001864 if (!err)
1865 err = sock->ops->shutdown(sock, how);
1866 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 }
1868 return err;
1869}
1870
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001871/* A couple of helpful macros for getting the address of the 32/64 bit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 * fields which are the same type (int / unsigned) on our platforms.
1873 */
1874#define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
1875#define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
1876#define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
1877
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00001878struct used_address {
1879 struct sockaddr_storage name;
1880 unsigned int name_len;
1881};
1882
Al Viroda184282015-03-21 19:29:06 -04001883static int copy_msghdr_from_user(struct msghdr *kmsg,
1884 struct user_msghdr __user *umsg,
1885 struct sockaddr __user **save_addr,
1886 struct iovec **iov)
Dan Carpenter1661bf32013-10-03 00:27:20 +03001887{
Al Viro08adb7d2014-11-10 20:23:13 -05001888 struct sockaddr __user *uaddr;
1889 struct iovec __user *uiov;
Al Viroc0371da2014-11-24 10:42:55 -05001890 size_t nr_segs;
Al Viro08adb7d2014-11-10 20:23:13 -05001891 ssize_t err;
1892
1893 if (!access_ok(VERIFY_READ, umsg, sizeof(*umsg)) ||
1894 __get_user(uaddr, &umsg->msg_name) ||
1895 __get_user(kmsg->msg_namelen, &umsg->msg_namelen) ||
1896 __get_user(uiov, &umsg->msg_iov) ||
Al Viroc0371da2014-11-24 10:42:55 -05001897 __get_user(nr_segs, &umsg->msg_iovlen) ||
Al Viro08adb7d2014-11-10 20:23:13 -05001898 __get_user(kmsg->msg_control, &umsg->msg_control) ||
1899 __get_user(kmsg->msg_controllen, &umsg->msg_controllen) ||
1900 __get_user(kmsg->msg_flags, &umsg->msg_flags))
Dan Carpenter1661bf32013-10-03 00:27:20 +03001901 return -EFAULT;
Matthew Leachdbb490b2014-03-11 11:58:27 +00001902
Al Viro08adb7d2014-11-10 20:23:13 -05001903 if (!uaddr)
Ani Sinha6a2a2b32014-09-08 14:49:59 -07001904 kmsg->msg_namelen = 0;
1905
Matthew Leachdbb490b2014-03-11 11:58:27 +00001906 if (kmsg->msg_namelen < 0)
1907 return -EINVAL;
1908
Dan Carpenter1661bf32013-10-03 00:27:20 +03001909 if (kmsg->msg_namelen > sizeof(struct sockaddr_storage))
Dan Carpenterdb31c552013-11-27 15:40:21 +03001910 kmsg->msg_namelen = sizeof(struct sockaddr_storage);
Al Viro08adb7d2014-11-10 20:23:13 -05001911
1912 if (save_addr)
1913 *save_addr = uaddr;
1914
1915 if (uaddr && kmsg->msg_namelen) {
1916 if (!save_addr) {
1917 err = move_addr_to_kernel(uaddr, kmsg->msg_namelen,
1918 kmsg->msg_name);
1919 if (err < 0)
1920 return err;
1921 }
1922 } else {
1923 kmsg->msg_name = NULL;
1924 kmsg->msg_namelen = 0;
1925 }
1926
Al Viroc0371da2014-11-24 10:42:55 -05001927 if (nr_segs > UIO_MAXIOV)
Al Viro08adb7d2014-11-10 20:23:13 -05001928 return -EMSGSIZE;
1929
tadeusz.struk@intel.com0345f932015-03-19 12:31:25 -07001930 kmsg->msg_iocb = NULL;
1931
Al Viroda184282015-03-21 19:29:06 -04001932 return import_iovec(save_addr ? READ : WRITE, uiov, nr_segs,
1933 UIO_FASTIOV, iov, &kmsg->msg_iter);
Dan Carpenter1661bf32013-10-03 00:27:20 +03001934}
1935
Al Viro666547f2014-04-06 14:03:05 -04001936static int ___sys_sendmsg(struct socket *sock, struct user_msghdr __user *msg,
Eric Dumazet95c96172012-04-15 05:58:06 +00001937 struct msghdr *msg_sys, unsigned int flags,
Tom Herbert28a94d82016-03-07 14:11:02 -08001938 struct used_address *used_address,
1939 unsigned int allowed_msghdr_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940{
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001941 struct compat_msghdr __user *msg_compat =
1942 (struct compat_msghdr __user *)msg;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001943 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
Alex Williamsonb9d717a2005-09-26 14:28:02 -07001945 unsigned char ctl[sizeof(struct cmsghdr) + 20]
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001946 __attribute__ ((aligned(sizeof(__kernel_size_t))));
1947 /* 20 is size of ipv6_pktinfo */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 unsigned char *ctl_buf = ctl;
Al Virod8725c82014-12-11 00:02:50 -05001949 int ctl_len;
Al Viro08adb7d2014-11-10 20:23:13 -05001950 ssize_t err;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001951
Al Viro08adb7d2014-11-10 20:23:13 -05001952 msg_sys->msg_name = &address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
Al Viro08449322014-11-09 22:33:45 -05001954 if (MSG_CMSG_COMPAT & flags)
Al Viro08adb7d2014-11-10 20:23:13 -05001955 err = get_compat_msghdr(msg_sys, msg_compat, NULL, &iov);
Al Viro08449322014-11-09 22:33:45 -05001956 else
Al Viro08adb7d2014-11-10 20:23:13 -05001957 err = copy_msghdr_from_user(msg_sys, msg, NULL, &iov);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001958 if (err < 0)
Al Viroda184282015-03-21 19:29:06 -04001959 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
1961 err = -ENOBUFS;
1962
Anton Blanchard228e5482011-05-02 20:21:35 +00001963 if (msg_sys->msg_controllen > INT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 goto out_freeiov;
Tom Herbert28a94d82016-03-07 14:11:02 -08001965 flags |= (msg_sys->msg_flags & allowed_msghdr_flags);
Anton Blanchard228e5482011-05-02 20:21:35 +00001966 ctl_len = msg_sys->msg_controllen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001968 err =
Anton Blanchard228e5482011-05-02 20:21:35 +00001969 cmsghdr_from_user_compat_to_kern(msg_sys, sock->sk, ctl,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001970 sizeof(ctl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 if (err)
1972 goto out_freeiov;
Anton Blanchard228e5482011-05-02 20:21:35 +00001973 ctl_buf = msg_sys->msg_control;
1974 ctl_len = msg_sys->msg_controllen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 } else if (ctl_len) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001976 if (ctl_len > sizeof(ctl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001978 if (ctl_buf == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 goto out_freeiov;
1980 }
1981 err = -EFAULT;
1982 /*
Anton Blanchard228e5482011-05-02 20:21:35 +00001983 * Careful! Before this, msg_sys->msg_control contains a user pointer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
1985 * checking falls down on this.
1986 */
Namhyung Kimfb8621b2010-09-07 03:55:00 +00001987 if (copy_from_user(ctl_buf,
Anton Blanchard228e5482011-05-02 20:21:35 +00001988 (void __user __force *)msg_sys->msg_control,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001989 ctl_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 goto out_freectl;
Anton Blanchard228e5482011-05-02 20:21:35 +00001991 msg_sys->msg_control = ctl_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 }
Anton Blanchard228e5482011-05-02 20:21:35 +00001993 msg_sys->msg_flags = flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
1995 if (sock->file->f_flags & O_NONBLOCK)
Anton Blanchard228e5482011-05-02 20:21:35 +00001996 msg_sys->msg_flags |= MSG_DONTWAIT;
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00001997 /*
1998 * If this is sendmmsg() and current destination address is same as
1999 * previously succeeded address, omit asking LSM's decision.
2000 * used_address->name_len is initialized to UINT_MAX so that the first
2001 * destination address never matches.
2002 */
Mathieu Desnoyersbc909d92011-08-24 19:45:03 -07002003 if (used_address && msg_sys->msg_name &&
2004 used_address->name_len == msg_sys->msg_namelen &&
2005 !memcmp(&used_address->name, msg_sys->msg_name,
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002006 used_address->name_len)) {
Al Virod8725c82014-12-11 00:02:50 -05002007 err = sock_sendmsg_nosec(sock, msg_sys);
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002008 goto out_freectl;
2009 }
Al Virod8725c82014-12-11 00:02:50 -05002010 err = sock_sendmsg(sock, msg_sys);
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002011 /*
2012 * If this is sendmmsg() and sending to current destination address was
2013 * successful, remember it.
2014 */
2015 if (used_address && err >= 0) {
2016 used_address->name_len = msg_sys->msg_namelen;
Mathieu Desnoyersbc909d92011-08-24 19:45:03 -07002017 if (msg_sys->msg_name)
2018 memcpy(&used_address->name, msg_sys->msg_name,
2019 used_address->name_len);
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
2022out_freectl:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002023 if (ctl_buf != ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 sock_kfree_s(sock->sk, ctl_buf, ctl_len);
2025out_freeiov:
Al Viroda184282015-03-21 19:29:06 -04002026 kfree(iov);
Anton Blanchard228e5482011-05-02 20:21:35 +00002027 return err;
2028}
2029
2030/*
2031 * BSD sendmsg interface
2032 */
2033
Al Viro666547f2014-04-06 14:03:05 -04002034long __sys_sendmsg(int fd, struct user_msghdr __user *msg, unsigned flags)
Anton Blanchard228e5482011-05-02 20:21:35 +00002035{
2036 int fput_needed, err;
2037 struct msghdr msg_sys;
Andy Lutomirski1be374a2013-05-22 14:07:44 -07002038 struct socket *sock;
Anton Blanchard228e5482011-05-02 20:21:35 +00002039
Andy Lutomirski1be374a2013-05-22 14:07:44 -07002040 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Anton Blanchard228e5482011-05-02 20:21:35 +00002041 if (!sock)
2042 goto out;
2043
Tom Herbert28a94d82016-03-07 14:11:02 -08002044 err = ___sys_sendmsg(sock, msg, &msg_sys, flags, NULL, 0);
Anton Blanchard228e5482011-05-02 20:21:35 +00002045
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002046 fput_light(sock->file, fput_needed);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002047out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 return err;
2049}
2050
Al Viro666547f2014-04-06 14:03:05 -04002051SYSCALL_DEFINE3(sendmsg, int, fd, struct user_msghdr __user *, msg, unsigned int, flags)
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002052{
2053 if (flags & MSG_CMSG_COMPAT)
2054 return -EINVAL;
2055 return __sys_sendmsg(fd, msg, flags);
2056}
2057
Anton Blanchard228e5482011-05-02 20:21:35 +00002058/*
2059 * Linux sendmmsg interface
2060 */
2061
2062int __sys_sendmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
2063 unsigned int flags)
2064{
2065 int fput_needed, err, datagrams;
2066 struct socket *sock;
2067 struct mmsghdr __user *entry;
2068 struct compat_mmsghdr __user *compat_entry;
2069 struct msghdr msg_sys;
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002070 struct used_address used_address;
Tom Herbertf0922762016-03-07 14:11:03 -08002071 unsigned int oflags = flags;
Anton Blanchard228e5482011-05-02 20:21:35 +00002072
Anton Blanchard98382f42011-08-04 14:07:39 +00002073 if (vlen > UIO_MAXIOV)
2074 vlen = UIO_MAXIOV;
Anton Blanchard228e5482011-05-02 20:21:35 +00002075
2076 datagrams = 0;
2077
2078 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2079 if (!sock)
2080 return err;
2081
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002082 used_address.name_len = UINT_MAX;
Anton Blanchard228e5482011-05-02 20:21:35 +00002083 entry = mmsg;
2084 compat_entry = (struct compat_mmsghdr __user *)mmsg;
Anton Blanchard728ffb82011-08-04 14:07:38 +00002085 err = 0;
Tom Herbertf0922762016-03-07 14:11:03 -08002086 flags |= MSG_BATCH;
Anton Blanchard228e5482011-05-02 20:21:35 +00002087
2088 while (datagrams < vlen) {
Tom Herbertf0922762016-03-07 14:11:03 -08002089 if (datagrams == vlen - 1)
2090 flags = oflags;
2091
Anton Blanchard228e5482011-05-02 20:21:35 +00002092 if (MSG_CMSG_COMPAT & flags) {
Al Viro666547f2014-04-06 14:03:05 -04002093 err = ___sys_sendmsg(sock, (struct user_msghdr __user *)compat_entry,
Tom Herbert28a94d82016-03-07 14:11:02 -08002094 &msg_sys, flags, &used_address, MSG_EOR);
Anton Blanchard228e5482011-05-02 20:21:35 +00002095 if (err < 0)
2096 break;
2097 err = __put_user(err, &compat_entry->msg_len);
2098 ++compat_entry;
2099 } else {
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002100 err = ___sys_sendmsg(sock,
Al Viro666547f2014-04-06 14:03:05 -04002101 (struct user_msghdr __user *)entry,
Tom Herbert28a94d82016-03-07 14:11:02 -08002102 &msg_sys, flags, &used_address, MSG_EOR);
Anton Blanchard228e5482011-05-02 20:21:35 +00002103 if (err < 0)
2104 break;
2105 err = put_user(err, &entry->msg_len);
2106 ++entry;
2107 }
2108
2109 if (err)
2110 break;
2111 ++datagrams;
Soheil Hassas Yeganeh30238982016-11-04 15:36:49 -04002112 if (msg_data_left(&msg_sys))
2113 break;
Eric Dumazeta78cb842016-01-08 08:37:20 -08002114 cond_resched();
Anton Blanchard228e5482011-05-02 20:21:35 +00002115 }
2116
Anton Blanchard228e5482011-05-02 20:21:35 +00002117 fput_light(sock->file, fput_needed);
2118
Anton Blanchard728ffb82011-08-04 14:07:38 +00002119 /* We only return an error if no datagrams were able to be sent */
2120 if (datagrams != 0)
Anton Blanchard228e5482011-05-02 20:21:35 +00002121 return datagrams;
2122
Anton Blanchard228e5482011-05-02 20:21:35 +00002123 return err;
2124}
2125
2126SYSCALL_DEFINE4(sendmmsg, int, fd, struct mmsghdr __user *, mmsg,
2127 unsigned int, vlen, unsigned int, flags)
2128{
Andy Lutomirski1be374a2013-05-22 14:07:44 -07002129 if (flags & MSG_CMSG_COMPAT)
2130 return -EINVAL;
Anton Blanchard228e5482011-05-02 20:21:35 +00002131 return __sys_sendmmsg(fd, mmsg, vlen, flags);
2132}
2133
Al Viro666547f2014-04-06 14:03:05 -04002134static int ___sys_recvmsg(struct socket *sock, struct user_msghdr __user *msg,
Eric Dumazet95c96172012-04-15 05:58:06 +00002135 struct msghdr *msg_sys, unsigned int flags, int nosec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136{
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002137 struct compat_msghdr __user *msg_compat =
2138 (struct compat_msghdr __user *)msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 struct iovec iovstack[UIO_FASTIOV];
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002140 struct iovec *iov = iovstack;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 unsigned long cmsg_ptr;
Al Viro2da62902015-03-14 21:13:46 -04002142 int len;
Al Viro08adb7d2014-11-10 20:23:13 -05002143 ssize_t err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
2145 /* kernel mode address */
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07002146 struct sockaddr_storage addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
2148 /* user mode address pointers */
2149 struct sockaddr __user *uaddr;
Al Viro08adb7d2014-11-10 20:23:13 -05002150 int __user *uaddr_len = COMPAT_NAMELEN(msg);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002151
Al Viro08adb7d2014-11-10 20:23:13 -05002152 msg_sys->msg_name = &addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01002154 if (MSG_CMSG_COMPAT & flags)
Al Viro08adb7d2014-11-10 20:23:13 -05002155 err = get_compat_msghdr(msg_sys, msg_compat, &uaddr, &iov);
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01002156 else
Al Viro08adb7d2014-11-10 20:23:13 -05002157 err = copy_msghdr_from_user(msg_sys, msg, &uaddr, &iov);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 if (err < 0)
Al Viroda184282015-03-21 19:29:06 -04002159 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002161 cmsg_ptr = (unsigned long)msg_sys->msg_control;
2162 msg_sys->msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002163
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01002164 /* We assume all kernel code knows the size of sockaddr_storage */
2165 msg_sys->msg_namelen = 0;
2166
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 if (sock->file->f_flags & O_NONBLOCK)
2168 flags |= MSG_DONTWAIT;
Al Viro2da62902015-03-14 21:13:46 -04002169 err = (nosec ? sock_recvmsg_nosec : sock_recvmsg)(sock, msg_sys, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 if (err < 0)
2171 goto out_freeiov;
2172 len = err;
2173
2174 if (uaddr != NULL) {
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00002175 err = move_addr_to_user(&addr,
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002176 msg_sys->msg_namelen, uaddr,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002177 uaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 if (err < 0)
2179 goto out_freeiov;
2180 }
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002181 err = __put_user((msg_sys->msg_flags & ~MSG_CMSG_COMPAT),
David S. Miller37f7f422005-09-16 16:51:01 -07002182 COMPAT_FLAGS(msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 if (err)
2184 goto out_freeiov;
2185 if (MSG_CMSG_COMPAT & flags)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002186 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 &msg_compat->msg_controllen);
2188 else
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002189 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 &msg->msg_controllen);
2191 if (err)
2192 goto out_freeiov;
2193 err = len;
2194
2195out_freeiov:
Al Viroda184282015-03-21 19:29:06 -04002196 kfree(iov);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002197 return err;
2198}
2199
2200/*
2201 * BSD recvmsg interface
2202 */
2203
Al Viro666547f2014-04-06 14:03:05 -04002204long __sys_recvmsg(int fd, struct user_msghdr __user *msg, unsigned flags)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002205{
2206 int fput_needed, err;
2207 struct msghdr msg_sys;
Andy Lutomirski1be374a2013-05-22 14:07:44 -07002208 struct socket *sock;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002209
Andy Lutomirski1be374a2013-05-22 14:07:44 -07002210 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002211 if (!sock)
2212 goto out;
2213
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002214 err = ___sys_recvmsg(sock, msg, &msg_sys, flags, 0);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002215
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002216 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217out:
2218 return err;
2219}
2220
Al Viro666547f2014-04-06 14:03:05 -04002221SYSCALL_DEFINE3(recvmsg, int, fd, struct user_msghdr __user *, msg,
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002222 unsigned int, flags)
2223{
2224 if (flags & MSG_CMSG_COMPAT)
2225 return -EINVAL;
2226 return __sys_recvmsg(fd, msg, flags);
2227}
2228
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002229/*
2230 * Linux recvmmsg interface
2231 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002233int __sys_recvmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
2234 unsigned int flags, struct timespec *timeout)
2235{
2236 int fput_needed, err, datagrams;
2237 struct socket *sock;
2238 struct mmsghdr __user *entry;
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002239 struct compat_mmsghdr __user *compat_entry;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002240 struct msghdr msg_sys;
Deepa Dinamani766b9f92016-05-19 17:09:05 -07002241 struct timespec64 end_time;
2242 struct timespec64 timeout64;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002243
2244 if (timeout &&
2245 poll_select_set_timeout(&end_time, timeout->tv_sec,
2246 timeout->tv_nsec))
2247 return -EINVAL;
2248
2249 datagrams = 0;
2250
2251 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2252 if (!sock)
2253 return err;
2254
2255 err = sock_error(sock->sk);
Maxime Jayat1a0e2592017-02-21 18:35:51 +01002256 if (err) {
2257 datagrams = err;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002258 goto out_put;
Maxime Jayat1a0e2592017-02-21 18:35:51 +01002259 }
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002260
2261 entry = mmsg;
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002262 compat_entry = (struct compat_mmsghdr __user *)mmsg;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002263
2264 while (datagrams < vlen) {
2265 /*
2266 * No need to ask LSM for more than the first datagram.
2267 */
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002268 if (MSG_CMSG_COMPAT & flags) {
Al Viro666547f2014-04-06 14:03:05 -04002269 err = ___sys_recvmsg(sock, (struct user_msghdr __user *)compat_entry,
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002270 &msg_sys, flags & ~MSG_WAITFORONE,
2271 datagrams);
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002272 if (err < 0)
2273 break;
2274 err = __put_user(err, &compat_entry->msg_len);
2275 ++compat_entry;
2276 } else {
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002277 err = ___sys_recvmsg(sock,
Al Viro666547f2014-04-06 14:03:05 -04002278 (struct user_msghdr __user *)entry,
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002279 &msg_sys, flags & ~MSG_WAITFORONE,
2280 datagrams);
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002281 if (err < 0)
2282 break;
2283 err = put_user(err, &entry->msg_len);
2284 ++entry;
2285 }
2286
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002287 if (err)
2288 break;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002289 ++datagrams;
2290
Brandon L Black71c5c152010-03-26 16:18:03 +00002291 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
2292 if (flags & MSG_WAITFORONE)
2293 flags |= MSG_DONTWAIT;
2294
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002295 if (timeout) {
Deepa Dinamani766b9f92016-05-19 17:09:05 -07002296 ktime_get_ts64(&timeout64);
2297 *timeout = timespec64_to_timespec(
2298 timespec64_sub(end_time, timeout64));
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002299 if (timeout->tv_sec < 0) {
2300 timeout->tv_sec = timeout->tv_nsec = 0;
2301 break;
2302 }
2303
2304 /* Timeout, return less than vlen datagrams */
2305 if (timeout->tv_nsec == 0 && timeout->tv_sec == 0)
2306 break;
2307 }
2308
2309 /* Out of band data, return right away */
2310 if (msg_sys.msg_flags & MSG_OOB)
2311 break;
Eric Dumazeta78cb842016-01-08 08:37:20 -08002312 cond_resched();
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002313 }
2314
Arnaldo Carvalho de Melo34b88a62016-03-14 09:56:35 -03002315 if (err == 0)
2316 goto out_put;
2317
2318 if (datagrams == 0) {
2319 datagrams = err;
2320 goto out_put;
2321 }
2322
2323 /*
2324 * We may return less entries than requested (vlen) if the
2325 * sock is non block and there aren't enough datagrams...
2326 */
2327 if (err != -EAGAIN) {
2328 /*
2329 * ... or if recvmsg returns an error after we
2330 * received some datagrams, where we record the
2331 * error to return on the next call or if the
2332 * app asks about it using getsockopt(SO_ERROR).
2333 */
2334 sock->sk->sk_err = -err;
2335 }
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002336out_put:
2337 fput_light(sock->file, fput_needed);
2338
Arnaldo Carvalho de Melo34b88a62016-03-14 09:56:35 -03002339 return datagrams;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002340}
2341
2342SYSCALL_DEFINE5(recvmmsg, int, fd, struct mmsghdr __user *, mmsg,
2343 unsigned int, vlen, unsigned int, flags,
2344 struct timespec __user *, timeout)
2345{
2346 int datagrams;
2347 struct timespec timeout_sys;
2348
Andy Lutomirski1be374a2013-05-22 14:07:44 -07002349 if (flags & MSG_CMSG_COMPAT)
2350 return -EINVAL;
2351
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002352 if (!timeout)
2353 return __sys_recvmmsg(fd, mmsg, vlen, flags, NULL);
2354
2355 if (copy_from_user(&timeout_sys, timeout, sizeof(timeout_sys)))
2356 return -EFAULT;
2357
2358 datagrams = __sys_recvmmsg(fd, mmsg, vlen, flags, &timeout_sys);
2359
2360 if (datagrams > 0 &&
2361 copy_to_user(timeout, &timeout_sys, sizeof(timeout_sys)))
2362 datagrams = -EFAULT;
2363
2364 return datagrams;
2365}
2366
2367#ifdef __ARCH_WANT_SYS_SOCKETCALL
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368/* Argument list sizes for sys_socketcall */
2369#define AL(x) ((x) * sizeof(unsigned long))
Anton Blanchard228e5482011-05-02 20:21:35 +00002370static const unsigned char nargs[21] = {
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002371 AL(0), AL(3), AL(3), AL(3), AL(2), AL(3),
2372 AL(3), AL(3), AL(4), AL(4), AL(4), AL(6),
2373 AL(6), AL(2), AL(5), AL(5), AL(3), AL(3),
Anton Blanchard228e5482011-05-02 20:21:35 +00002374 AL(4), AL(5), AL(4)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002375};
2376
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377#undef AL
2378
2379/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002380 * System call vectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 *
2382 * Argument checking cleaned up. Saved 20% in size.
2383 * This function doesn't need to set the kernel lock because
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002384 * it is set by the callees.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 */
2386
Heiko Carstens3e0fa652009-01-14 14:14:24 +01002387SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388{
Chen Gang2950fa92013-04-07 16:55:23 +08002389 unsigned long a[AUDITSC_ARGS];
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002390 unsigned long a0, a1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 int err;
Arjan van de Ven47379052009-09-28 12:57:44 -07002392 unsigned int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393
Anton Blanchard228e5482011-05-02 20:21:35 +00002394 if (call < 1 || call > SYS_SENDMMSG)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 return -EINVAL;
Jeremy Cline9a492f82018-07-27 22:43:01 +00002396 call = array_index_nospec(call, SYS_SENDMMSG + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
Arjan van de Ven47379052009-09-28 12:57:44 -07002398 len = nargs[call];
2399 if (len > sizeof(a))
2400 return -EINVAL;
2401
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 /* copy_from_user should be SMP safe. */
Arjan van de Ven47379052009-09-28 12:57:44 -07002403 if (copy_from_user(a, args, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 return -EFAULT;
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002405
Chen Gang2950fa92013-04-07 16:55:23 +08002406 err = audit_socketcall(nargs[call] / sizeof(unsigned long), a);
2407 if (err)
2408 return err;
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002409
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002410 a0 = a[0];
2411 a1 = a[1];
2412
2413 switch (call) {
2414 case SYS_SOCKET:
2415 err = sys_socket(a0, a1, a[2]);
2416 break;
2417 case SYS_BIND:
2418 err = sys_bind(a0, (struct sockaddr __user *)a1, a[2]);
2419 break;
2420 case SYS_CONNECT:
2421 err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
2422 break;
2423 case SYS_LISTEN:
2424 err = sys_listen(a0, a1);
2425 break;
2426 case SYS_ACCEPT:
Ulrich Drepperde11def2008-11-19 15:36:14 -08002427 err = sys_accept4(a0, (struct sockaddr __user *)a1,
2428 (int __user *)a[2], 0);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002429 break;
2430 case SYS_GETSOCKNAME:
2431 err =
2432 sys_getsockname(a0, (struct sockaddr __user *)a1,
2433 (int __user *)a[2]);
2434 break;
2435 case SYS_GETPEERNAME:
2436 err =
2437 sys_getpeername(a0, (struct sockaddr __user *)a1,
2438 (int __user *)a[2]);
2439 break;
2440 case SYS_SOCKETPAIR:
2441 err = sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
2442 break;
2443 case SYS_SEND:
2444 err = sys_send(a0, (void __user *)a1, a[2], a[3]);
2445 break;
2446 case SYS_SENDTO:
2447 err = sys_sendto(a0, (void __user *)a1, a[2], a[3],
2448 (struct sockaddr __user *)a[4], a[5]);
2449 break;
2450 case SYS_RECV:
2451 err = sys_recv(a0, (void __user *)a1, a[2], a[3]);
2452 break;
2453 case SYS_RECVFROM:
2454 err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
2455 (struct sockaddr __user *)a[4],
2456 (int __user *)a[5]);
2457 break;
2458 case SYS_SHUTDOWN:
2459 err = sys_shutdown(a0, a1);
2460 break;
2461 case SYS_SETSOCKOPT:
2462 err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]);
2463 break;
2464 case SYS_GETSOCKOPT:
2465 err =
2466 sys_getsockopt(a0, a1, a[2], (char __user *)a[3],
2467 (int __user *)a[4]);
2468 break;
2469 case SYS_SENDMSG:
Al Viro666547f2014-04-06 14:03:05 -04002470 err = sys_sendmsg(a0, (struct user_msghdr __user *)a1, a[2]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002471 break;
Anton Blanchard228e5482011-05-02 20:21:35 +00002472 case SYS_SENDMMSG:
2473 err = sys_sendmmsg(a0, (struct mmsghdr __user *)a1, a[2], a[3]);
2474 break;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002475 case SYS_RECVMSG:
Al Viro666547f2014-04-06 14:03:05 -04002476 err = sys_recvmsg(a0, (struct user_msghdr __user *)a1, a[2]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002477 break;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002478 case SYS_RECVMMSG:
2479 err = sys_recvmmsg(a0, (struct mmsghdr __user *)a1, a[2], a[3],
2480 (struct timespec __user *)a[4]);
2481 break;
Ulrich Drepperde11def2008-11-19 15:36:14 -08002482 case SYS_ACCEPT4:
2483 err = sys_accept4(a0, (struct sockaddr __user *)a1,
2484 (int __user *)a[2], a[3]);
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07002485 break;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002486 default:
2487 err = -EINVAL;
2488 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 }
2490 return err;
2491}
2492
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002493#endif /* __ARCH_WANT_SYS_SOCKETCALL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002495/**
2496 * sock_register - add a socket protocol handler
2497 * @ops: description of protocol
2498 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 * This function is called by a protocol handler that wants to
2500 * advertise its address family, and have it linked into the
Masanari Iidae793c0f2014-09-04 23:44:36 +09002501 * socket interface. The value ops->family corresponds to the
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002502 * socket system call protocol family.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 */
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002504int sock_register(const struct net_proto_family *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505{
2506 int err;
2507
2508 if (ops->family >= NPROTO) {
Yang Yingliang3410f222014-02-12 17:09:55 +08002509 pr_crit("protocol %d >= NPROTO(%d)\n", ops->family, NPROTO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 return -ENOBUFS;
2511 }
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002512
2513 spin_lock(&net_family_lock);
Eric Dumazet190683a2010-11-10 10:50:44 +00002514 if (rcu_dereference_protected(net_families[ops->family],
2515 lockdep_is_held(&net_family_lock)))
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002516 err = -EEXIST;
2517 else {
Eric Dumazetcf778b02012-01-12 04:41:32 +00002518 rcu_assign_pointer(net_families[ops->family], ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 err = 0;
2520 }
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002521 spin_unlock(&net_family_lock);
2522
Yang Yingliang3410f222014-02-12 17:09:55 +08002523 pr_info("NET: Registered protocol family %d\n", ops->family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 return err;
2525}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002526EXPORT_SYMBOL(sock_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002528/**
2529 * sock_unregister - remove a protocol handler
2530 * @family: protocol family to remove
2531 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 * This function is called by a protocol handler that wants to
2533 * remove its address family, and have it unlinked from the
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002534 * new socket creation.
2535 *
2536 * If protocol handler is a module, then it can use module reference
2537 * counts to protect against new references. If protocol handler is not
2538 * a module then it needs to provide its own protection in
2539 * the ops->create routine.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 */
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002541void sock_unregister(int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542{
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002543 BUG_ON(family < 0 || family >= NPROTO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002545 spin_lock(&net_family_lock);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00002546 RCU_INIT_POINTER(net_families[family], NULL);
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002547 spin_unlock(&net_family_lock);
2548
2549 synchronize_rcu();
2550
Yang Yingliang3410f222014-02-12 17:09:55 +08002551 pr_info("NET: Unregistered protocol family %d\n", family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002553EXPORT_SYMBOL(sock_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554
Andi Kleen77d76ea2005-12-22 12:43:42 -08002555static int __init sock_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556{
Nick Pigginb3e19d92011-01-07 17:50:11 +11002557 int err;
Eric W. Biederman2ca794e2012-04-19 13:20:32 +00002558 /*
2559 * Initialize the network sysctl infrastructure.
2560 */
2561 err = net_sysctl_init();
2562 if (err)
2563 goto out;
Nick Pigginb3e19d92011-01-07 17:50:11 +11002564
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002566 * Initialize skbuff SLAB cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 */
2568 skb_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569
2570 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002571 * Initialize the protocols module.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 */
2573
2574 init_inodecache();
Nick Pigginb3e19d92011-01-07 17:50:11 +11002575
2576 err = register_filesystem(&sock_fs_type);
2577 if (err)
2578 goto out_fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 sock_mnt = kern_mount(&sock_fs_type);
Nick Pigginb3e19d92011-01-07 17:50:11 +11002580 if (IS_ERR(sock_mnt)) {
2581 err = PTR_ERR(sock_mnt);
2582 goto out_mount;
2583 }
Andi Kleen77d76ea2005-12-22 12:43:42 -08002584
2585 /* The real protocol initialization is performed in later initcalls.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 */
2587
2588#ifdef CONFIG_NETFILTER
Pablo Neira Ayuso6d11cfd2013-05-22 22:42:36 +00002589 err = netfilter_init();
2590 if (err)
2591 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592#endif
David S. Millercbeb3212005-12-22 12:58:55 -08002593
Daniel Borkmann408eccc2014-04-01 16:20:23 +02002594 ptp_classifier_init();
Richard Cochranc1f19b52010-07-17 08:49:36 +00002595
Nick Pigginb3e19d92011-01-07 17:50:11 +11002596out:
2597 return err;
2598
2599out_mount:
2600 unregister_filesystem(&sock_fs_type);
2601out_fs:
2602 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603}
2604
Andi Kleen77d76ea2005-12-22 12:43:42 -08002605core_initcall(sock_init); /* early initcall */
2606
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607#ifdef CONFIG_PROC_FS
2608void socket_seq_show(struct seq_file *seq)
2609{
2610 int cpu;
2611 int counter = 0;
2612
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -07002613 for_each_possible_cpu(cpu)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002614 counter += per_cpu(sockets_in_use, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615
2616 /* It can be negative, by the way. 8) */
2617 if (counter < 0)
2618 counter = 0;
2619
2620 seq_printf(seq, "sockets: used %d\n", counter);
2621}
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002622#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623
Shaun Pereira89bbfc92006-03-21 23:58:08 -08002624#ifdef CONFIG_COMPAT
Arnd Bergmann6b960182009-11-06 23:10:54 -08002625static int do_siocgstamp(struct net *net, struct socket *sock,
H. Peter Anvin644595f2012-02-19 17:51:59 -08002626 unsigned int cmd, void __user *up)
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002627{
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002628 mm_segment_t old_fs = get_fs();
2629 struct timeval ktv;
2630 int err;
2631
2632 set_fs(KERNEL_DS);
Arnd Bergmann6b960182009-11-06 23:10:54 -08002633 err = sock_do_ioctl(net, sock, cmd, (unsigned long)&ktv);
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002634 set_fs(old_fs);
H. Peter Anvin644595f2012-02-19 17:51:59 -08002635 if (!err)
Mikulas Patockaed6fe9d2012-09-01 12:34:07 -04002636 err = compat_put_timeval(&ktv, up);
H. Peter Anvin644595f2012-02-19 17:51:59 -08002637
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002638 return err;
2639}
2640
Arnd Bergmann6b960182009-11-06 23:10:54 -08002641static int do_siocgstampns(struct net *net, struct socket *sock,
H. Peter Anvin644595f2012-02-19 17:51:59 -08002642 unsigned int cmd, void __user *up)
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002643{
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002644 mm_segment_t old_fs = get_fs();
2645 struct timespec kts;
2646 int err;
2647
2648 set_fs(KERNEL_DS);
Arnd Bergmann6b960182009-11-06 23:10:54 -08002649 err = sock_do_ioctl(net, sock, cmd, (unsigned long)&kts);
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002650 set_fs(old_fs);
H. Peter Anvin644595f2012-02-19 17:51:59 -08002651 if (!err)
Mikulas Patockaed6fe9d2012-09-01 12:34:07 -04002652 err = compat_put_timespec(&kts, up);
H. Peter Anvin644595f2012-02-19 17:51:59 -08002653
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002654 return err;
2655}
2656
Arnd Bergmann6b960182009-11-06 23:10:54 -08002657static int dev_ifname32(struct net *net, struct compat_ifreq __user *uifr32)
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002658{
2659 struct ifreq __user *uifr;
2660 int err;
2661
2662 uifr = compat_alloc_user_space(sizeof(struct ifreq));
Arnd Bergmann6b960182009-11-06 23:10:54 -08002663 if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002664 return -EFAULT;
2665
Arnd Bergmann6b960182009-11-06 23:10:54 -08002666 err = dev_ioctl(net, SIOCGIFNAME, uifr);
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002667 if (err)
2668 return err;
2669
Arnd Bergmann6b960182009-11-06 23:10:54 -08002670 if (copy_in_user(uifr32, uifr, sizeof(struct compat_ifreq)))
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002671 return -EFAULT;
2672
2673 return 0;
2674}
2675
Arnd Bergmann6b960182009-11-06 23:10:54 -08002676static int dev_ifconf(struct net *net, struct compat_ifconf __user *uifc32)
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002677{
Arnd Bergmann6b960182009-11-06 23:10:54 -08002678 struct compat_ifconf ifc32;
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002679 struct ifconf ifc;
2680 struct ifconf __user *uifc;
Arnd Bergmann6b960182009-11-06 23:10:54 -08002681 struct compat_ifreq __user *ifr32;
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002682 struct ifreq __user *ifr;
2683 unsigned int i, j;
2684 int err;
2685
Arnd Bergmann6b960182009-11-06 23:10:54 -08002686 if (copy_from_user(&ifc32, uifc32, sizeof(struct compat_ifconf)))
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002687 return -EFAULT;
2688
Mathias Krause43da5f22012-08-15 11:31:57 +00002689 memset(&ifc, 0, sizeof(ifc));
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002690 if (ifc32.ifcbuf == 0) {
2691 ifc32.ifc_len = 0;
2692 ifc.ifc_len = 0;
2693 ifc.ifc_req = NULL;
2694 uifc = compat_alloc_user_space(sizeof(struct ifconf));
2695 } else {
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002696 size_t len = ((ifc32.ifc_len / sizeof(struct compat_ifreq)) + 1) *
2697 sizeof(struct ifreq);
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002698 uifc = compat_alloc_user_space(sizeof(struct ifconf) + len);
2699 ifc.ifc_len = len;
2700 ifr = ifc.ifc_req = (void __user *)(uifc + 1);
2701 ifr32 = compat_ptr(ifc32.ifcbuf);
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002702 for (i = 0; i < ifc32.ifc_len; i += sizeof(struct compat_ifreq)) {
Arnd Bergmann6b960182009-11-06 23:10:54 -08002703 if (copy_in_user(ifr, ifr32, sizeof(struct compat_ifreq)))
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002704 return -EFAULT;
2705 ifr++;
2706 ifr32++;
2707 }
2708 }
2709 if (copy_to_user(uifc, &ifc, sizeof(struct ifconf)))
2710 return -EFAULT;
2711
Arnd Bergmann6b960182009-11-06 23:10:54 -08002712 err = dev_ioctl(net, SIOCGIFCONF, uifc);
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002713 if (err)
2714 return err;
2715
2716 if (copy_from_user(&ifc, uifc, sizeof(struct ifconf)))
2717 return -EFAULT;
2718
2719 ifr = ifc.ifc_req;
2720 ifr32 = compat_ptr(ifc32.ifcbuf);
2721 for (i = 0, j = 0;
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002722 i + sizeof(struct compat_ifreq) <= ifc32.ifc_len && j < ifc.ifc_len;
2723 i += sizeof(struct compat_ifreq), j += sizeof(struct ifreq)) {
2724 if (copy_in_user(ifr32, ifr, sizeof(struct compat_ifreq)))
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002725 return -EFAULT;
2726 ifr32++;
2727 ifr++;
2728 }
2729
2730 if (ifc32.ifcbuf == 0) {
2731 /* Translate from 64-bit structure multiple to
2732 * a 32-bit one.
2733 */
2734 i = ifc.ifc_len;
Arnd Bergmann6b960182009-11-06 23:10:54 -08002735 i = ((i / sizeof(struct ifreq)) * sizeof(struct compat_ifreq));
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002736 ifc32.ifc_len = i;
2737 } else {
2738 ifc32.ifc_len = i;
2739 }
Arnd Bergmann6b960182009-11-06 23:10:54 -08002740 if (copy_to_user(uifc32, &ifc32, sizeof(struct compat_ifconf)))
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002741 return -EFAULT;
2742
2743 return 0;
2744}
2745
Arnd Bergmann6b960182009-11-06 23:10:54 -08002746static int ethtool_ioctl(struct net *net, struct compat_ifreq __user *ifr32)
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002747{
Ben Hutchings3a7da392011-03-17 07:34:32 +00002748 struct compat_ethtool_rxnfc __user *compat_rxnfc;
2749 bool convert_in = false, convert_out = false;
2750 size_t buf_size = ALIGN(sizeof(struct ifreq), 8);
2751 struct ethtool_rxnfc __user *rxnfc;
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002752 struct ifreq __user *ifr;
Ben Hutchings3a7da392011-03-17 07:34:32 +00002753 u32 rule_cnt = 0, actual_rule_cnt;
2754 u32 ethcmd;
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002755 u32 data;
Ben Hutchings3a7da392011-03-17 07:34:32 +00002756 int ret;
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002757
2758 if (get_user(data, &ifr32->ifr_ifru.ifru_data))
2759 return -EFAULT;
2760
Ben Hutchings3a7da392011-03-17 07:34:32 +00002761 compat_rxnfc = compat_ptr(data);
2762
2763 if (get_user(ethcmd, &compat_rxnfc->cmd))
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002764 return -EFAULT;
2765
Ben Hutchings3a7da392011-03-17 07:34:32 +00002766 /* Most ethtool structures are defined without padding.
2767 * Unfortunately struct ethtool_rxnfc is an exception.
2768 */
2769 switch (ethcmd) {
2770 default:
2771 break;
2772 case ETHTOOL_GRXCLSRLALL:
2773 /* Buffer size is variable */
2774 if (get_user(rule_cnt, &compat_rxnfc->rule_cnt))
2775 return -EFAULT;
2776 if (rule_cnt > KMALLOC_MAX_SIZE / sizeof(u32))
2777 return -ENOMEM;
2778 buf_size += rule_cnt * sizeof(u32);
2779 /* fall through */
2780 case ETHTOOL_GRXRINGS:
2781 case ETHTOOL_GRXCLSRLCNT:
2782 case ETHTOOL_GRXCLSRULE:
Ben Hutchings55664f32012-01-03 12:04:51 +00002783 case ETHTOOL_SRXCLSRLINS:
Ben Hutchings3a7da392011-03-17 07:34:32 +00002784 convert_out = true;
2785 /* fall through */
2786 case ETHTOOL_SRXCLSRLDEL:
Ben Hutchings3a7da392011-03-17 07:34:32 +00002787 buf_size += sizeof(struct ethtool_rxnfc);
2788 convert_in = true;
2789 break;
2790 }
2791
2792 ifr = compat_alloc_user_space(buf_size);
Stephen Hemminger954b1242013-02-11 06:22:28 +00002793 rxnfc = (void __user *)ifr + ALIGN(sizeof(struct ifreq), 8);
Ben Hutchings3a7da392011-03-17 07:34:32 +00002794
2795 if (copy_in_user(&ifr->ifr_name, &ifr32->ifr_name, IFNAMSIZ))
2796 return -EFAULT;
2797
2798 if (put_user(convert_in ? rxnfc : compat_ptr(data),
2799 &ifr->ifr_ifru.ifru_data))
2800 return -EFAULT;
2801
2802 if (convert_in) {
Alexander Duyck127fe532011-04-08 18:01:59 +00002803 /* We expect there to be holes between fs.m_ext and
Ben Hutchings3a7da392011-03-17 07:34:32 +00002804 * fs.ring_cookie and at the end of fs, but nowhere else.
2805 */
Alexander Duyck127fe532011-04-08 18:01:59 +00002806 BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc, fs.m_ext) +
2807 sizeof(compat_rxnfc->fs.m_ext) !=
2808 offsetof(struct ethtool_rxnfc, fs.m_ext) +
2809 sizeof(rxnfc->fs.m_ext));
Ben Hutchings3a7da392011-03-17 07:34:32 +00002810 BUILD_BUG_ON(
2811 offsetof(struct compat_ethtool_rxnfc, fs.location) -
2812 offsetof(struct compat_ethtool_rxnfc, fs.ring_cookie) !=
2813 offsetof(struct ethtool_rxnfc, fs.location) -
2814 offsetof(struct ethtool_rxnfc, fs.ring_cookie));
2815
2816 if (copy_in_user(rxnfc, compat_rxnfc,
Stephen Hemminger954b1242013-02-11 06:22:28 +00002817 (void __user *)(&rxnfc->fs.m_ext + 1) -
2818 (void __user *)rxnfc) ||
Ben Hutchings3a7da392011-03-17 07:34:32 +00002819 copy_in_user(&rxnfc->fs.ring_cookie,
2820 &compat_rxnfc->fs.ring_cookie,
Stephen Hemminger954b1242013-02-11 06:22:28 +00002821 (void __user *)(&rxnfc->fs.location + 1) -
Wenwen Wangf57ef242018-10-18 09:36:46 -05002822 (void __user *)&rxnfc->fs.ring_cookie))
2823 return -EFAULT;
2824 if (ethcmd == ETHTOOL_GRXCLSRLALL) {
2825 if (put_user(rule_cnt, &rxnfc->rule_cnt))
2826 return -EFAULT;
2827 } else if (copy_in_user(&rxnfc->rule_cnt,
2828 &compat_rxnfc->rule_cnt,
2829 sizeof(rxnfc->rule_cnt)))
Ben Hutchings3a7da392011-03-17 07:34:32 +00002830 return -EFAULT;
2831 }
2832
2833 ret = dev_ioctl(net, SIOCETHTOOL, ifr);
2834 if (ret)
2835 return ret;
2836
2837 if (convert_out) {
2838 if (copy_in_user(compat_rxnfc, rxnfc,
Stephen Hemminger954b1242013-02-11 06:22:28 +00002839 (const void __user *)(&rxnfc->fs.m_ext + 1) -
2840 (const void __user *)rxnfc) ||
Ben Hutchings3a7da392011-03-17 07:34:32 +00002841 copy_in_user(&compat_rxnfc->fs.ring_cookie,
2842 &rxnfc->fs.ring_cookie,
Stephen Hemminger954b1242013-02-11 06:22:28 +00002843 (const void __user *)(&rxnfc->fs.location + 1) -
2844 (const void __user *)&rxnfc->fs.ring_cookie) ||
Ben Hutchings3a7da392011-03-17 07:34:32 +00002845 copy_in_user(&compat_rxnfc->rule_cnt, &rxnfc->rule_cnt,
2846 sizeof(rxnfc->rule_cnt)))
2847 return -EFAULT;
2848
2849 if (ethcmd == ETHTOOL_GRXCLSRLALL) {
2850 /* As an optimisation, we only copy the actual
2851 * number of rules that the underlying
2852 * function returned. Since Mallory might
2853 * change the rule count in user memory, we
2854 * check that it is less than the rule count
2855 * originally given (as the user buffer size),
2856 * which has been range-checked.
2857 */
2858 if (get_user(actual_rule_cnt, &rxnfc->rule_cnt))
2859 return -EFAULT;
2860 if (actual_rule_cnt < rule_cnt)
2861 rule_cnt = actual_rule_cnt;
2862 if (copy_in_user(&compat_rxnfc->rule_locs[0],
2863 &rxnfc->rule_locs[0],
2864 rule_cnt * sizeof(u32)))
2865 return -EFAULT;
2866 }
2867 }
2868
2869 return 0;
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002870}
2871
Arnd Bergmann7a50a242009-11-08 20:57:03 -08002872static int compat_siocwandev(struct net *net, struct compat_ifreq __user *uifr32)
2873{
2874 void __user *uptr;
2875 compat_uptr_t uptr32;
2876 struct ifreq __user *uifr;
2877
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002878 uifr = compat_alloc_user_space(sizeof(*uifr));
Arnd Bergmann7a50a242009-11-08 20:57:03 -08002879 if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
2880 return -EFAULT;
2881
2882 if (get_user(uptr32, &uifr32->ifr_settings.ifs_ifsu))
2883 return -EFAULT;
2884
2885 uptr = compat_ptr(uptr32);
2886
2887 if (put_user(uptr, &uifr->ifr_settings.ifs_ifsu.raw_hdlc))
2888 return -EFAULT;
2889
2890 return dev_ioctl(net, SIOCWANDEV, uifr);
2891}
2892
Arnd Bergmann6b960182009-11-06 23:10:54 -08002893static int bond_ioctl(struct net *net, unsigned int cmd,
2894 struct compat_ifreq __user *ifr32)
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002895{
2896 struct ifreq kifr;
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002897 mm_segment_t old_fs;
2898 int err;
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002899
2900 switch (cmd) {
2901 case SIOCBONDENSLAVE:
2902 case SIOCBONDRELEASE:
2903 case SIOCBONDSETHWADDR:
2904 case SIOCBONDCHANGEACTIVE:
Arnd Bergmann6b960182009-11-06 23:10:54 -08002905 if (copy_from_user(&kifr, ifr32, sizeof(struct compat_ifreq)))
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002906 return -EFAULT;
2907
2908 old_fs = get_fs();
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002909 set_fs(KERNEL_DS);
stephen hemmingerc3f52ae2011-02-23 09:06:48 +00002910 err = dev_ioctl(net, cmd,
2911 (struct ifreq __user __force *) &kifr);
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002912 set_fs(old_fs);
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002913
2914 return err;
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002915 default:
Linus Torvalds07d106d2012-01-05 15:40:12 -08002916 return -ENOIOCTLCMD;
Joe Perchesccbd6a52010-05-14 10:58:26 +00002917 }
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002918}
2919
Ben Hutchings590d4692013-11-18 17:04:13 +00002920/* Handle ioctls that use ifreq::ifr_data and just need struct ifreq converted */
2921static int compat_ifr_data_ioctl(struct net *net, unsigned int cmd,
Arnd Bergmann6b960182009-11-06 23:10:54 -08002922 struct compat_ifreq __user *u_ifreq32)
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002923{
2924 struct ifreq __user *u_ifreq64;
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002925 char tmp_buf[IFNAMSIZ];
2926 void __user *data64;
2927 u32 data32;
2928
2929 if (copy_from_user(&tmp_buf[0], &(u_ifreq32->ifr_ifrn.ifrn_name[0]),
2930 IFNAMSIZ))
2931 return -EFAULT;
Ben Hutchings417c3522013-11-18 17:04:58 +00002932 if (get_user(data32, &u_ifreq32->ifr_ifru.ifru_data))
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002933 return -EFAULT;
2934 data64 = compat_ptr(data32);
2935
2936 u_ifreq64 = compat_alloc_user_space(sizeof(*u_ifreq64));
2937
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002938 if (copy_to_user(&u_ifreq64->ifr_ifrn.ifrn_name[0], &tmp_buf[0],
2939 IFNAMSIZ))
2940 return -EFAULT;
Ben Hutchings417c3522013-11-18 17:04:58 +00002941 if (put_user(data64, &u_ifreq64->ifr_ifru.ifru_data))
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002942 return -EFAULT;
2943
Arnd Bergmann6b960182009-11-06 23:10:54 -08002944 return dev_ioctl(net, cmd, u_ifreq64);
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002945}
2946
Arnd Bergmann6b960182009-11-06 23:10:54 -08002947static int dev_ifsioc(struct net *net, struct socket *sock,
2948 unsigned int cmd, struct compat_ifreq __user *uifr32)
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002949{
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00002950 struct ifreq __user *uifr;
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002951 int err;
2952
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00002953 uifr = compat_alloc_user_space(sizeof(*uifr));
2954 if (copy_in_user(uifr, uifr32, sizeof(*uifr32)))
2955 return -EFAULT;
2956
2957 err = sock_do_ioctl(net, sock, cmd, (unsigned long)uifr);
2958
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002959 if (!err) {
2960 switch (cmd) {
2961 case SIOCGIFFLAGS:
2962 case SIOCGIFMETRIC:
2963 case SIOCGIFMTU:
2964 case SIOCGIFMEM:
2965 case SIOCGIFHWADDR:
2966 case SIOCGIFINDEX:
2967 case SIOCGIFADDR:
2968 case SIOCGIFBRDADDR:
2969 case SIOCGIFDSTADDR:
2970 case SIOCGIFNETMASK:
Arnd Bergmannfab25322009-11-08 20:56:21 -08002971 case SIOCGIFPFLAGS:
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002972 case SIOCGIFTXQLEN:
Arnd Bergmannfab25322009-11-08 20:56:21 -08002973 case SIOCGMIIPHY:
2974 case SIOCGMIIREG:
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00002975 if (copy_in_user(uifr32, uifr, sizeof(*uifr32)))
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002976 err = -EFAULT;
2977 break;
2978 }
2979 }
2980 return err;
2981}
2982
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00002983static int compat_sioc_ifmap(struct net *net, unsigned int cmd,
2984 struct compat_ifreq __user *uifr32)
2985{
2986 struct ifreq ifr;
2987 struct compat_ifmap __user *uifmap32;
2988 mm_segment_t old_fs;
2989 int err;
2990
2991 uifmap32 = &uifr32->ifr_ifru.ifru_map;
2992 err = copy_from_user(&ifr, uifr32, sizeof(ifr.ifr_name));
Mathieu Desnoyers3ddc5b42013-09-11 14:23:18 -07002993 err |= get_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
2994 err |= get_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
2995 err |= get_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
2996 err |= get_user(ifr.ifr_map.irq, &uifmap32->irq);
2997 err |= get_user(ifr.ifr_map.dma, &uifmap32->dma);
2998 err |= get_user(ifr.ifr_map.port, &uifmap32->port);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00002999 if (err)
3000 return -EFAULT;
3001
3002 old_fs = get_fs();
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003003 set_fs(KERNEL_DS);
stephen hemmingerc3f52ae2011-02-23 09:06:48 +00003004 err = dev_ioctl(net, cmd, (void __user __force *)&ifr);
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003005 set_fs(old_fs);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003006
3007 if (cmd == SIOCGIFMAP && !err) {
3008 err = copy_to_user(uifr32, &ifr, sizeof(ifr.ifr_name));
Mathieu Desnoyers3ddc5b42013-09-11 14:23:18 -07003009 err |= put_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
3010 err |= put_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
3011 err |= put_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
3012 err |= put_user(ifr.ifr_map.irq, &uifmap32->irq);
3013 err |= put_user(ifr.ifr_map.dma, &uifmap32->dma);
3014 err |= put_user(ifr.ifr_map.port, &uifmap32->port);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003015 if (err)
3016 err = -EFAULT;
3017 }
3018 return err;
3019}
3020
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003021struct rtentry32 {
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003022 u32 rt_pad1;
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003023 struct sockaddr rt_dst; /* target address */
3024 struct sockaddr rt_gateway; /* gateway addr (RTF_GATEWAY) */
3025 struct sockaddr rt_genmask; /* target network mask (IP) */
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003026 unsigned short rt_flags;
3027 short rt_pad2;
3028 u32 rt_pad3;
3029 unsigned char rt_tos;
3030 unsigned char rt_class;
3031 short rt_pad4;
3032 short rt_metric; /* +1 for binary compatibility! */
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003033 /* char * */ u32 rt_dev; /* forcing the device at add */
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003034 u32 rt_mtu; /* per route MTU/Window */
3035 u32 rt_window; /* Window clamping */
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003036 unsigned short rt_irtt; /* Initial RTT */
3037};
3038
3039struct in6_rtmsg32 {
3040 struct in6_addr rtmsg_dst;
3041 struct in6_addr rtmsg_src;
3042 struct in6_addr rtmsg_gateway;
3043 u32 rtmsg_type;
3044 u16 rtmsg_dst_len;
3045 u16 rtmsg_src_len;
3046 u32 rtmsg_metric;
3047 u32 rtmsg_info;
3048 u32 rtmsg_flags;
3049 s32 rtmsg_ifindex;
3050};
3051
Arnd Bergmann6b960182009-11-06 23:10:54 -08003052static int routing_ioctl(struct net *net, struct socket *sock,
3053 unsigned int cmd, void __user *argp)
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003054{
3055 int ret;
3056 void *r = NULL;
3057 struct in6_rtmsg r6;
3058 struct rtentry r4;
3059 char devname[16];
3060 u32 rtdev;
3061 mm_segment_t old_fs = get_fs();
3062
Arnd Bergmann6b960182009-11-06 23:10:54 -08003063 if (sock && sock->sk && sock->sk->sk_family == AF_INET6) { /* ipv6 */
3064 struct in6_rtmsg32 __user *ur6 = argp;
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003065 ret = copy_from_user(&r6.rtmsg_dst, &(ur6->rtmsg_dst),
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003066 3 * sizeof(struct in6_addr));
Mathieu Desnoyers3ddc5b42013-09-11 14:23:18 -07003067 ret |= get_user(r6.rtmsg_type, &(ur6->rtmsg_type));
3068 ret |= get_user(r6.rtmsg_dst_len, &(ur6->rtmsg_dst_len));
3069 ret |= get_user(r6.rtmsg_src_len, &(ur6->rtmsg_src_len));
3070 ret |= get_user(r6.rtmsg_metric, &(ur6->rtmsg_metric));
3071 ret |= get_user(r6.rtmsg_info, &(ur6->rtmsg_info));
3072 ret |= get_user(r6.rtmsg_flags, &(ur6->rtmsg_flags));
3073 ret |= get_user(r6.rtmsg_ifindex, &(ur6->rtmsg_ifindex));
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003074
3075 r = (void *) &r6;
3076 } else { /* ipv4 */
Arnd Bergmann6b960182009-11-06 23:10:54 -08003077 struct rtentry32 __user *ur4 = argp;
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003078 ret = copy_from_user(&r4.rt_dst, &(ur4->rt_dst),
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003079 3 * sizeof(struct sockaddr));
Mathieu Desnoyers3ddc5b42013-09-11 14:23:18 -07003080 ret |= get_user(r4.rt_flags, &(ur4->rt_flags));
3081 ret |= get_user(r4.rt_metric, &(ur4->rt_metric));
3082 ret |= get_user(r4.rt_mtu, &(ur4->rt_mtu));
3083 ret |= get_user(r4.rt_window, &(ur4->rt_window));
3084 ret |= get_user(r4.rt_irtt, &(ur4->rt_irtt));
3085 ret |= get_user(rtdev, &(ur4->rt_dev));
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003086 if (rtdev) {
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003087 ret |= copy_from_user(devname, compat_ptr(rtdev), 15);
stephen hemmingerc3f52ae2011-02-23 09:06:48 +00003088 r4.rt_dev = (char __user __force *)devname;
3089 devname[15] = 0;
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003090 } else
3091 r4.rt_dev = NULL;
3092
3093 r = (void *) &r4;
3094 }
3095
3096 if (ret) {
3097 ret = -EFAULT;
3098 goto out;
3099 }
3100
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003101 set_fs(KERNEL_DS);
Arnd Bergmann6b960182009-11-06 23:10:54 -08003102 ret = sock_do_ioctl(net, sock, cmd, (unsigned long) r);
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003103 set_fs(old_fs);
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003104
3105out:
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003106 return ret;
3107}
3108
3109/* Since old style bridge ioctl's endup using SIOCDEVPRIVATE
3110 * for some operations; this forces use of the newer bridge-utils that
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003111 * use compatible ioctls
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003112 */
Arnd Bergmann6b960182009-11-06 23:10:54 -08003113static int old_bridge_ioctl(compat_ulong_t __user *argp)
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003114{
Arnd Bergmann6b960182009-11-06 23:10:54 -08003115 compat_ulong_t tmp;
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003116
Arnd Bergmann6b960182009-11-06 23:10:54 -08003117 if (get_user(tmp, argp))
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003118 return -EFAULT;
3119 if (tmp == BRCTL_GET_VERSION)
3120 return BRCTL_VERSION + 1;
3121 return -EINVAL;
3122}
3123
Arnd Bergmann6b960182009-11-06 23:10:54 -08003124static int compat_sock_ioctl_trans(struct file *file, struct socket *sock,
3125 unsigned int cmd, unsigned long arg)
3126{
3127 void __user *argp = compat_ptr(arg);
3128 struct sock *sk = sock->sk;
3129 struct net *net = sock_net(sk);
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003130
Arnd Bergmann6b960182009-11-06 23:10:54 -08003131 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15))
Ben Hutchings590d4692013-11-18 17:04:13 +00003132 return compat_ifr_data_ioctl(net, cmd, argp);
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003133
Arnd Bergmann6b960182009-11-06 23:10:54 -08003134 switch (cmd) {
3135 case SIOCSIFBR:
3136 case SIOCGIFBR:
3137 return old_bridge_ioctl(argp);
3138 case SIOCGIFNAME:
3139 return dev_ifname32(net, argp);
3140 case SIOCGIFCONF:
3141 return dev_ifconf(net, argp);
3142 case SIOCETHTOOL:
3143 return ethtool_ioctl(net, argp);
Arnd Bergmann7a50a242009-11-08 20:57:03 -08003144 case SIOCWANDEV:
3145 return compat_siocwandev(net, argp);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003146 case SIOCGIFMAP:
3147 case SIOCSIFMAP:
3148 return compat_sioc_ifmap(net, cmd, argp);
Arnd Bergmann6b960182009-11-06 23:10:54 -08003149 case SIOCBONDENSLAVE:
3150 case SIOCBONDRELEASE:
3151 case SIOCBONDSETHWADDR:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003152 case SIOCBONDCHANGEACTIVE:
3153 return bond_ioctl(net, cmd, argp);
3154 case SIOCADDRT:
3155 case SIOCDELRT:
3156 return routing_ioctl(net, sock, cmd, argp);
3157 case SIOCGSTAMP:
3158 return do_siocgstamp(net, sock, cmd, argp);
3159 case SIOCGSTAMPNS:
3160 return do_siocgstampns(net, sock, cmd, argp);
Ben Hutchings590d4692013-11-18 17:04:13 +00003161 case SIOCBONDSLAVEINFOQUERY:
3162 case SIOCBONDINFOQUERY:
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003163 case SIOCSHWTSTAMP:
Ben Hutchingsfd468c72013-11-14 01:19:29 +00003164 case SIOCGHWTSTAMP:
Ben Hutchings590d4692013-11-18 17:04:13 +00003165 return compat_ifr_data_ioctl(net, cmd, argp);
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003166
Arnd Bergmann6b960182009-11-06 23:10:54 -08003167 case FIOSETOWN:
3168 case SIOCSPGRP:
3169 case FIOGETOWN:
3170 case SIOCGPGRP:
3171 case SIOCBRADDBR:
3172 case SIOCBRDELBR:
3173 case SIOCGIFVLAN:
3174 case SIOCSIFVLAN:
3175 case SIOCADDDLCI:
3176 case SIOCDELDLCI:
3177 return sock_ioctl(file, cmd, arg);
3178
3179 case SIOCGIFFLAGS:
3180 case SIOCSIFFLAGS:
3181 case SIOCGIFMETRIC:
3182 case SIOCSIFMETRIC:
3183 case SIOCGIFMTU:
3184 case SIOCSIFMTU:
3185 case SIOCGIFMEM:
3186 case SIOCSIFMEM:
3187 case SIOCGIFHWADDR:
3188 case SIOCSIFHWADDR:
3189 case SIOCADDMULTI:
3190 case SIOCDELMULTI:
3191 case SIOCGIFINDEX:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003192 case SIOCGIFADDR:
3193 case SIOCSIFADDR:
3194 case SIOCSIFHWBROADCAST:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003195 case SIOCDIFADDR:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003196 case SIOCGIFBRDADDR:
3197 case SIOCSIFBRDADDR:
3198 case SIOCGIFDSTADDR:
3199 case SIOCSIFDSTADDR:
3200 case SIOCGIFNETMASK:
3201 case SIOCSIFNETMASK:
3202 case SIOCSIFPFLAGS:
3203 case SIOCGIFPFLAGS:
3204 case SIOCGIFTXQLEN:
3205 case SIOCSIFTXQLEN:
3206 case SIOCBRADDIF:
3207 case SIOCBRDELIF:
Arnd Bergmann9177efd2009-11-06 08:09:09 +00003208 case SIOCSIFNAME:
3209 case SIOCGMIIPHY:
3210 case SIOCGMIIREG:
3211 case SIOCSMIIREG:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003212 return dev_ifsioc(net, sock, cmd, argp);
Arnd Bergmann9177efd2009-11-06 08:09:09 +00003213
Arnd Bergmann6b960182009-11-06 23:10:54 -08003214 case SIOCSARP:
3215 case SIOCGARP:
3216 case SIOCDARP:
Arnd Bergmannfc49aa72019-06-03 23:06:00 +02003217 case SIOCOUTQNSD:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003218 case SIOCATMARK:
Arnd Bergmann9177efd2009-11-06 08:09:09 +00003219 return sock_do_ioctl(net, sock, cmd, arg);
3220 }
3221
Arnd Bergmann6b960182009-11-06 23:10:54 -08003222 return -ENOIOCTLCMD;
3223}
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003224
Eric Dumazet95c96172012-04-15 05:58:06 +00003225static long compat_sock_ioctl(struct file *file, unsigned int cmd,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07003226 unsigned long arg)
Shaun Pereira89bbfc92006-03-21 23:58:08 -08003227{
3228 struct socket *sock = file->private_data;
3229 int ret = -ENOIOCTLCMD;
David S. Miller87de87d2008-06-03 09:14:03 -07003230 struct sock *sk;
3231 struct net *net;
3232
3233 sk = sock->sk;
3234 net = sock_net(sk);
Shaun Pereira89bbfc92006-03-21 23:58:08 -08003235
3236 if (sock->ops->compat_ioctl)
3237 ret = sock->ops->compat_ioctl(sock, cmd, arg);
3238
David S. Miller87de87d2008-06-03 09:14:03 -07003239 if (ret == -ENOIOCTLCMD &&
3240 (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST))
3241 ret = compat_wext_handle_ioctl(net, cmd, arg);
3242
Arnd Bergmann6b960182009-11-06 23:10:54 -08003243 if (ret == -ENOIOCTLCMD)
3244 ret = compat_sock_ioctl_trans(file, sock, cmd, arg);
3245
Shaun Pereira89bbfc92006-03-21 23:58:08 -08003246 return ret;
3247}
3248#endif
3249
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003250int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen)
3251{
3252 return sock->ops->bind(sock, addr, addrlen);
3253}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003254EXPORT_SYMBOL(kernel_bind);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003255
3256int kernel_listen(struct socket *sock, int backlog)
3257{
3258 return sock->ops->listen(sock, backlog);
3259}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003260EXPORT_SYMBOL(kernel_listen);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003261
3262int kernel_accept(struct socket *sock, struct socket **newsock, int flags)
3263{
3264 struct sock *sk = sock->sk;
3265 int err;
3266
3267 err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
3268 newsock);
3269 if (err < 0)
3270 goto done;
3271
3272 err = sock->ops->accept(sock, *newsock, flags);
3273 if (err < 0) {
3274 sock_release(*newsock);
Tony Battersbyfa8705b2007-10-10 21:09:04 -07003275 *newsock = NULL;
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003276 goto done;
3277 }
3278
3279 (*newsock)->ops = sock->ops;
Wei Yongjun1b085342008-12-18 19:35:10 -08003280 __module_get((*newsock)->ops->owner);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003281
3282done:
3283 return err;
3284}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003285EXPORT_SYMBOL(kernel_accept);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003286
3287int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
YOSHIFUJI Hideaki4768fbc2007-02-09 23:25:31 +09003288 int flags)
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003289{
3290 return sock->ops->connect(sock, addr, addrlen, flags);
3291}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003292EXPORT_SYMBOL(kernel_connect);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003293
3294int kernel_getsockname(struct socket *sock, struct sockaddr *addr,
3295 int *addrlen)
3296{
3297 return sock->ops->getname(sock, addr, addrlen, 0);
3298}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003299EXPORT_SYMBOL(kernel_getsockname);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003300
3301int kernel_getpeername(struct socket *sock, struct sockaddr *addr,
3302 int *addrlen)
3303{
3304 return sock->ops->getname(sock, addr, addrlen, 1);
3305}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003306EXPORT_SYMBOL(kernel_getpeername);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003307
3308int kernel_getsockopt(struct socket *sock, int level, int optname,
3309 char *optval, int *optlen)
3310{
3311 mm_segment_t oldfs = get_fs();
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003312 char __user *uoptval;
3313 int __user *uoptlen;
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003314 int err;
3315
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003316 uoptval = (char __user __force *) optval;
3317 uoptlen = (int __user __force *) optlen;
3318
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003319 set_fs(KERNEL_DS);
3320 if (level == SOL_SOCKET)
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003321 err = sock_getsockopt(sock, level, optname, uoptval, uoptlen);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003322 else
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003323 err = sock->ops->getsockopt(sock, level, optname, uoptval,
3324 uoptlen);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003325 set_fs(oldfs);
3326 return err;
3327}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003328EXPORT_SYMBOL(kernel_getsockopt);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003329
3330int kernel_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07003331 char *optval, unsigned int optlen)
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003332{
3333 mm_segment_t oldfs = get_fs();
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003334 char __user *uoptval;
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003335 int err;
3336
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003337 uoptval = (char __user __force *) optval;
3338
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003339 set_fs(KERNEL_DS);
3340 if (level == SOL_SOCKET)
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003341 err = sock_setsockopt(sock, level, optname, uoptval, optlen);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003342 else
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003343 err = sock->ops->setsockopt(sock, level, optname, uoptval,
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003344 optlen);
3345 set_fs(oldfs);
3346 return err;
3347}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003348EXPORT_SYMBOL(kernel_setsockopt);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003349
3350int kernel_sendpage(struct socket *sock, struct page *page, int offset,
3351 size_t size, int flags)
3352{
3353 if (sock->ops->sendpage)
3354 return sock->ops->sendpage(sock, page, offset, size, flags);
3355
3356 return sock_no_sendpage(sock, page, offset, size, flags);
3357}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003358EXPORT_SYMBOL(kernel_sendpage);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003359
3360int kernel_sock_ioctl(struct socket *sock, int cmd, unsigned long arg)
3361{
3362 mm_segment_t oldfs = get_fs();
3363 int err;
3364
3365 set_fs(KERNEL_DS);
3366 err = sock->ops->ioctl(sock, cmd, arg);
3367 set_fs(oldfs);
3368
3369 return err;
3370}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003371EXPORT_SYMBOL(kernel_sock_ioctl);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003372
Trond Myklebust91cf45f2007-11-12 18:10:39 -08003373int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how)
3374{
3375 return sock->ops->shutdown(sock, how);
3376}
Trond Myklebust91cf45f2007-11-12 18:10:39 -08003377EXPORT_SYMBOL(kernel_sock_shutdown);
Devi Sandeep Endluri V V25c44d12014-03-04 07:31:47 -07003378
3379int sockev_register_notify(struct notifier_block *nb)
3380{
3381 return blocking_notifier_chain_register(&sockev_notifier_list, nb);
3382}
3383EXPORT_SYMBOL(sockev_register_notify);
3384
3385int sockev_unregister_notify(struct notifier_block *nb)
3386{
3387 return blocking_notifier_chain_unregister(&sockev_notifier_list, nb);
3388}
3389EXPORT_SYMBOL(sockev_unregister_notify);
Michael Bestas997774b2020-06-30 02:33:58 +03003390
R. Parameswaranb25415f2020-05-22 00:39:19 +01003391/* This routine returns the IP overhead imposed by a socket i.e.
3392 * the length of the underlying IP header, depending on whether
3393 * this is an IPv4 or IPv6 socket and the length from IP options turned
R. Parameswarane6cfc102017-04-12 18:31:04 -07003394 * on at the socket. Assumes that the caller has a lock on the socket.
R. Parameswaranb25415f2020-05-22 00:39:19 +01003395 */
3396u32 kernel_sock_ip_overhead(struct sock *sk)
3397{
3398 struct inet_sock *inet;
3399 struct ip_options_rcu *opt;
3400 u32 overhead = 0;
3401 bool owned_by_user;
3402#if IS_ENABLED(CONFIG_IPV6)
3403 struct ipv6_pinfo *np;
3404 struct ipv6_txoptions *optv6 = NULL;
3405#endif /* IS_ENABLED(CONFIG_IPV6) */
3406
3407 if (!sk)
3408 return overhead;
3409
3410 owned_by_user = sock_owned_by_user(sk);
3411 switch (sk->sk_family) {
3412 case AF_INET:
3413 inet = inet_sk(sk);
3414 overhead += sizeof(struct iphdr);
3415 opt = rcu_dereference_protected(inet->inet_opt,
3416 owned_by_user);
3417 if (opt)
3418 overhead += opt->opt.optlen;
3419 return overhead;
3420#if IS_ENABLED(CONFIG_IPV6)
3421 case AF_INET6:
3422 np = inet6_sk(sk);
3423 overhead += sizeof(struct ipv6hdr);
3424 if (np)
3425 optv6 = rcu_dereference_protected(np->opt,
3426 owned_by_user);
3427 if (optv6)
3428 overhead += (optv6->opt_flen + optv6->opt_nflen);
3429 return overhead;
3430#endif /* IS_ENABLED(CONFIG_IPV6) */
3431 default: /* Returns 0 overhead if the socket is not ipv4 or ipv6 */
3432 return overhead;
3433 }
3434}
3435EXPORT_SYMBOL(kernel_sock_ip_overhead);