blob: 532f123e04596984de3e9938be322891ed990e80 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * NET An implementation of the SOCKET network access protocol.
4 *
5 * Version: @(#)socket.c 1.1.93 18/02/95
6 *
7 * Authors: Orest Zborowski, <obz@Kodak.COM>
Jesper Juhl02c30a82005-05-05 16:16:16 -07008 * Ross Biro
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
10 *
11 * Fixes:
12 * Anonymous : NOTSOCK/BADF cleanup. Error fix in
13 * shutdown()
14 * Alan Cox : verify_area() fixes
15 * Alan Cox : Removed DDI
16 * Jonathan Kamens : SOCK_DGRAM reconnect bug
17 * Alan Cox : Moved a load of checks to the very
18 * top level.
19 * Alan Cox : Move address structures to/from user
20 * mode above the protocol layers.
21 * Rob Janssen : Allow 0 length sends.
22 * Alan Cox : Asynchronous I/O support (cribbed from the
23 * tty drivers).
24 * Niibe Yutaka : Asynchronous I/O for writes (4.4BSD style)
25 * Jeff Uphoff : Made max number of sockets command-line
26 * configurable.
27 * Matti Aarnio : Made the number of sockets dynamic,
28 * to be allocated when needed, and mr.
29 * Uphoff's max is used as max to be
30 * allowed to allocate.
31 * Linus : Argh. removed all the socket allocation
32 * altogether: it's in the inode now.
33 * Alan Cox : Made sock_alloc()/sock_release() public
34 * for NetROM and future kernel nfsd type
35 * stuff.
36 * Alan Cox : sendmsg/recvmsg basics.
37 * Tom Dyas : Export net symbols.
38 * Marcin Dalecki : Fixed problems with CONFIG_NET="n".
39 * Alan Cox : Added thread locking to sys_* calls
40 * for sockets. May have errors at the
41 * moment.
42 * Kevin Buhr : Fixed the dumb errors in the above.
43 * Andi Kleen : Some small cleanups, optimizations,
44 * and fixed a copy_from_user() bug.
45 * Tigran Aivazian : sys_send(args) calls sys_sendto(args, NULL, 0)
Stephen Hemminger89bddce2006-09-01 00:19:31 -070046 * Tigran Aivazian : Made listen(2) backlog sanity checks
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 * protocol-independent
48 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 * This module is effectively the top level interface to the BSD socket
Stephen Hemminger89bddce2006-09-01 00:19:31 -070050 * paradigm.
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 *
52 * Based upon Swansea University Computer Society NET3.039
53 */
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <linux/socket.h>
57#include <linux/file.h>
58#include <linux/net.h>
59#include <linux/interrupt.h>
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -070060#include <linux/thread_info.h>
Stephen Hemminger55737fd2006-09-01 00:23:39 -070061#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <linux/netdevice.h>
63#include <linux/proc_fs.h>
64#include <linux/seq_file.h>
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080065#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#include <linux/if_bridge.h>
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -030067#include <linux/if_frad.h>
68#include <linux/if_vlan.h>
Daniel Borkmann408eccc2014-04-01 16:20:23 +020069#include <linux/ptp_classify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#include <linux/init.h>
71#include <linux/poll.h>
72#include <linux/cache.h>
73#include <linux/module.h>
74#include <linux/highmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#include <linux/mount.h>
David Howellsfba9be42019-03-25 16:38:24 +000076#include <linux/pseudo_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#include <linux/security.h>
78#include <linux/syscalls.h>
79#include <linux/compat.h>
80#include <linux/kmod.h>
David Woodhouse3ec3b2f2005-05-17 12:08:48 +010081#include <linux/audit.h>
Adrian Bunkd86b5e02006-01-21 00:46:55 +010082#include <linux/wireless.h>
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -070083#include <linux/nsproxy.h>
Nick Black1fd7317d2009-09-22 16:43:33 -070084#include <linux/magic.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090085#include <linux/slab.h>
Masatake YAMATO600e1772012-08-29 10:44:29 +000086#include <linux/xattr.h>
Jeremy Clinec8e8cd52018-07-27 22:43:01 +000087#include <linux/nospec.h>
Paolo Abeni8c3c4472019-05-03 17:01:39 +020088#include <linux/indirect_call_wrapper.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080090#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070091#include <asm/unistd.h>
92
93#include <net/compat.h>
David S. Miller87de87d2008-06-03 09:14:03 -070094#include <net/wext.h>
Herbert Xuf8451722010-05-24 00:12:34 -070095#include <net/cls_cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97#include <net/sock.h>
98#include <linux/netfilter.h>
99
Arnd Bergmann6b960182009-11-06 23:10:54 -0800100#include <linux/if_tun.h>
101#include <linux/ipv6_route.h>
102#include <linux/route.h>
Arnd Bergmannc7dc5042019-06-03 23:07:12 +0200103#include <linux/termios.h>
Arnd Bergmann6b960182009-11-06 23:10:54 -0800104#include <linux/sockios.h>
Eliezer Tamir076bb0c2013-07-10 17:13:17 +0300105#include <net/busy_poll.h>
Willem de Bruijnf24b9be2014-08-04 22:11:45 -0400106#include <linux/errqueue.h>
Eliezer Tamir06021292013-06-10 11:39:50 +0300107
Cong Wange0d10952013-08-01 11:10:25 +0800108#ifdef CONFIG_NET_RX_BUSY_POLL
Eliezer Tamir64b0dc52013-07-10 17:13:36 +0300109unsigned int sysctl_net_busy_read __read_mostly;
110unsigned int sysctl_net_busy_poll __read_mostly;
Eliezer Tamir06021292013-06-10 11:39:50 +0300111#endif
Arnd Bergmann6b960182009-11-06 23:10:54 -0800112
Al Viro8ae5e0302014-11-28 19:40:50 -0500113static ssize_t sock_read_iter(struct kiocb *iocb, struct iov_iter *to);
114static ssize_t sock_write_iter(struct kiocb *iocb, struct iov_iter *from);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700115static int sock_mmap(struct file *file, struct vm_area_struct *vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117static int sock_close(struct inode *inode, struct file *file);
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700118static __poll_t sock_poll(struct file *file,
119 struct poll_table_struct *wait);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700120static long sock_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
Shaun Pereira89bbfc92006-03-21 23:58:08 -0800121#ifdef CONFIG_COMPAT
122static long compat_sock_ioctl(struct file *file,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700123 unsigned int cmd, unsigned long arg);
Shaun Pereira89bbfc92006-03-21 23:58:08 -0800124#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125static int sock_fasync(int fd, struct file *filp, int on);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126static ssize_t sock_sendpage(struct file *file, struct page *page,
127 int offset, size_t size, loff_t *ppos, int more);
Jens Axboe9c55e012007-11-06 23:30:13 -0800128static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700129 struct pipe_inode_info *pipe, size_t len,
Jens Axboe9c55e012007-11-06 23:30:13 -0800130 unsigned int flags);
Kirill Tkhaib4653342019-12-09 13:03:40 +0300131static void sock_show_fdinfo(struct seq_file *m, struct file *f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133/*
134 * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
135 * in the operation structures but are done directly via the socketcall() multiplexor.
136 */
137
Arjan van de Venda7071d2007-02-12 00:55:36 -0800138static const struct file_operations socket_file_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 .owner = THIS_MODULE,
140 .llseek = no_llseek,
Al Viro8ae5e0302014-11-28 19:40:50 -0500141 .read_iter = sock_read_iter,
142 .write_iter = sock_write_iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 .poll = sock_poll,
144 .unlocked_ioctl = sock_ioctl,
Shaun Pereira89bbfc92006-03-21 23:58:08 -0800145#ifdef CONFIG_COMPAT
146 .compat_ioctl = compat_sock_ioctl,
147#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 .mmap = sock_mmap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 .release = sock_close,
150 .fasync = sock_fasync,
Jens Axboe5274f052006-03-30 15:15:30 +0200151 .sendpage = sock_sendpage,
152 .splice_write = generic_splice_sendpage,
Jens Axboe9c55e012007-11-06 23:30:13 -0800153 .splice_read = sock_splice_read,
Kirill Tkhaib4653342019-12-09 13:03:40 +0300154#ifdef CONFIG_PROC_FS
155 .show_fdinfo = sock_show_fdinfo,
156#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157};
158
159/*
160 * The protocol list. Each protocol is registered in here.
161 */
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163static DEFINE_SPINLOCK(net_family_lock);
Eric Dumazet190683a2010-11-10 10:50:44 +0000164static const struct net_proto_family __rcu *net_families[NPROTO] __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700167 * Support routines.
168 * Move socket addresses back and forth across the kernel/user
169 * divide and look after the messy bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 */
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172/**
173 * move_addr_to_kernel - copy a socket address into kernel space
174 * @uaddr: Address in user space
175 * @kaddr: Address in kernel space
176 * @ulen: Length in user space
177 *
178 * The address is copied into kernel space. If the provided address is
179 * too long an error code of -EINVAL is returned. If the copy gives
180 * invalid addresses -EFAULT is returned. On a success 0 is returned.
181 */
182
Maciej Żenczykowski43db3622012-03-11 12:51:50 +0000183int move_addr_to_kernel(void __user *uaddr, int ulen, struct sockaddr_storage *kaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -0700185 if (ulen < 0 || ulen > sizeof(struct sockaddr_storage))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700187 if (ulen == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 return 0;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700189 if (copy_from_user(kaddr, uaddr, ulen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 return -EFAULT;
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100191 return audit_sockaddr(ulen, kaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
194/**
195 * move_addr_to_user - copy an address to user space
196 * @kaddr: kernel space address
197 * @klen: length of address in kernel
198 * @uaddr: user space address
199 * @ulen: pointer to user length field
200 *
201 * The value pointed to by ulen on entry is the buffer length available.
202 * This is overwritten with the buffer space used. -EINVAL is returned
203 * if an overlong buffer is specified or a negative buffer size. -EFAULT
204 * is returned if either the buffer or the length field are not
205 * accessible.
206 * After copying the data up to the limit the user specifies, the true
207 * length of the data is written over the length limit the user
208 * specified. Zero is returned for a success.
209 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700210
Maciej Żenczykowski43db3622012-03-11 12:51:50 +0000211static int move_addr_to_user(struct sockaddr_storage *kaddr, int klen,
stephen hemminger11165f12010-10-18 14:27:29 +0000212 void __user *uaddr, int __user *ulen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
214 int err;
215 int len;
216
Hannes Frederic Sowa68c6beb2013-11-21 03:14:34 +0100217 BUG_ON(klen > sizeof(struct sockaddr_storage));
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700218 err = get_user(len, ulen);
219 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 return err;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700221 if (len > klen)
222 len = klen;
Hannes Frederic Sowa68c6beb2013-11-21 03:14:34 +0100223 if (len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700225 if (len) {
Steve Grubbd6fe3942006-03-30 12:20:22 -0500226 if (audit_sockaddr(klen, kaddr))
227 return -ENOMEM;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700228 if (copy_to_user(uaddr, kaddr, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 return -EFAULT;
230 }
231 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700232 * "fromlen shall refer to the value before truncation.."
233 * 1003.1g
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 */
235 return __put_user(klen, ulen);
236}
237
Alexey Dobriyan08009a72018-02-24 21:20:33 +0300238static struct kmem_cache *sock_inode_cachep __ro_after_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240static struct inode *sock_alloc_inode(struct super_block *sb)
241{
242 struct socket_alloc *ei;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700243
Christoph Lametere94b1762006-12-06 20:33:17 -0800244 ei = kmem_cache_alloc(sock_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 if (!ei)
246 return NULL;
Al Viro333f7902019-07-05 20:14:16 +0100247 init_waitqueue_head(&ei->socket.wq.wait);
248 ei->socket.wq.fasync_list = NULL;
249 ei->socket.wq.flags = 0;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 ei->socket.state = SS_UNCONNECTED;
252 ei->socket.flags = 0;
253 ei->socket.ops = NULL;
254 ei->socket.sk = NULL;
255 ei->socket.file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 return &ei->vfs_inode;
258}
259
Al Viro6d7855c2019-07-05 20:13:22 +0100260static void sock_free_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
Eric Dumazet43815482010-04-29 11:01:49 +0000262 struct socket_alloc *ei;
263
264 ei = container_of(inode, struct socket_alloc, vfs_inode);
Eric Dumazet43815482010-04-29 11:01:49 +0000265 kmem_cache_free(sock_inode_cachep, ei);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}
267
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700268static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700270 struct socket_alloc *ei = (struct socket_alloc *)foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Christoph Lametera35afb82007-05-16 22:10:57 -0700272 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700274
yuan linyu1e911632017-01-07 17:18:31 +0800275static void init_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
277 sock_inode_cachep = kmem_cache_create("sock_inode_cache",
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700278 sizeof(struct socket_alloc),
279 0,
280 (SLAB_HWCACHE_ALIGN |
281 SLAB_RECLAIM_ACCOUNT |
Vladimir Davydov5d097052016-01-14 15:18:21 -0800282 SLAB_MEM_SPREAD | SLAB_ACCOUNT),
Paul Mundt20c2df82007-07-20 10:11:58 +0900283 init_once);
yuan linyu1e911632017-01-07 17:18:31 +0800284 BUG_ON(sock_inode_cachep == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
Alexey Dobriyanb87221d2009-09-21 17:01:09 -0700287static const struct super_operations sockfs_ops = {
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700288 .alloc_inode = sock_alloc_inode,
Al Viro6d7855c2019-07-05 20:13:22 +0100289 .free_inode = sock_free_inode,
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700290 .statfs = simple_statfs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291};
292
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700293/*
294 * sockfs_dname() is called from d_path().
295 */
296static char *sockfs_dname(struct dentry *dentry, char *buffer, int buflen)
297{
298 return dynamic_dname(dentry, buffer, buflen, "socket:[%lu]",
David Howellsc5ef6032015-03-17 22:26:16 +0000299 d_inode(dentry)->i_ino);
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700300}
301
Al Viro3ba13d12009-02-20 06:02:22 +0000302static const struct dentry_operations sockfs_dentry_operations = {
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700303 .d_dname = sockfs_dname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304};
305
Andreas Gruenbacherbba0bd32016-09-29 17:48:35 +0200306static int sockfs_xattr_get(const struct xattr_handler *handler,
307 struct dentry *dentry, struct inode *inode,
308 const char *suffix, void *value, size_t size)
309{
310 if (value) {
311 if (dentry->d_name.len + 1 > size)
312 return -ERANGE;
313 memcpy(value, dentry->d_name.name, dentry->d_name.len + 1);
314 }
315 return dentry->d_name.len + 1;
316}
317
318#define XATTR_SOCKPROTONAME_SUFFIX "sockprotoname"
319#define XATTR_NAME_SOCKPROTONAME (XATTR_SYSTEM_PREFIX XATTR_SOCKPROTONAME_SUFFIX)
320#define XATTR_NAME_SOCKPROTONAME_LEN (sizeof(XATTR_NAME_SOCKPROTONAME)-1)
321
322static const struct xattr_handler sockfs_xattr_handler = {
323 .name = XATTR_NAME_SOCKPROTONAME,
324 .get = sockfs_xattr_get,
325};
326
Andreas Gruenbacher4a590152016-11-13 21:23:34 +0100327static int sockfs_security_xattr_set(const struct xattr_handler *handler,
328 struct dentry *dentry, struct inode *inode,
329 const char *suffix, const void *value,
330 size_t size, int flags)
331{
332 /* Handled by LSM. */
333 return -EAGAIN;
334}
335
336static const struct xattr_handler sockfs_security_xattr_handler = {
337 .prefix = XATTR_SECURITY_PREFIX,
338 .set = sockfs_security_xattr_set,
339};
340
Andreas Gruenbacherbba0bd32016-09-29 17:48:35 +0200341static const struct xattr_handler *sockfs_xattr_handlers[] = {
342 &sockfs_xattr_handler,
Andreas Gruenbacher4a590152016-11-13 21:23:34 +0100343 &sockfs_security_xattr_handler,
Andreas Gruenbacherbba0bd32016-09-29 17:48:35 +0200344 NULL
345};
346
David Howellsfba9be42019-03-25 16:38:24 +0000347static int sockfs_init_fs_context(struct fs_context *fc)
Al Viroc74a1cb2011-01-12 16:59:34 -0500348{
David Howellsfba9be42019-03-25 16:38:24 +0000349 struct pseudo_fs_context *ctx = init_pseudo(fc, SOCKFS_MAGIC);
350 if (!ctx)
351 return -ENOMEM;
352 ctx->ops = &sockfs_ops;
353 ctx->dops = &sockfs_dentry_operations;
354 ctx->xattr = sockfs_xattr_handlers;
355 return 0;
Al Viroc74a1cb2011-01-12 16:59:34 -0500356}
357
358static struct vfsmount *sock_mnt __read_mostly;
359
360static struct file_system_type sock_fs_type = {
361 .name = "sockfs",
David Howellsfba9be42019-03-25 16:38:24 +0000362 .init_fs_context = sockfs_init_fs_context,
Al Viroc74a1cb2011-01-12 16:59:34 -0500363 .kill_sb = kill_anon_super,
364};
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366/*
367 * Obtains the first available file descriptor and sets it up for use.
368 *
David S. Miller39d8c1b2006-03-20 17:13:49 -0800369 * These functions create file structures and maps them to fd space
370 * of the current process. On success it returns file descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 * and file struct implicitly stored in sock->file.
372 * Note that another thread may close file descriptor before we return
373 * from this function. We use the fact that now we do not refer
374 * to socket after mapping. If one day we will need it, this
375 * function will increment ref. count on file by 1.
376 *
377 * In any case returned fd MAY BE not valid!
378 * This race condition is unavoidable
379 * with shared fd spaces, we cannot solve it inside kernel,
380 * but we take care of internal coherence yet.
381 */
382
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300383/**
384 * sock_alloc_file - Bind a &socket to a &file
385 * @sock: socket
386 * @flags: file status flags
387 * @dname: protocol name
388 *
389 * Returns the &file bound with @sock, implicitly storing it
390 * in sock->file. If dname is %NULL, sets to "".
391 * On failure the return is a ERR pointer (see linux/err.h).
392 * This function uses GFP_KERNEL internally.
393 */
394
Linus Torvaldsaab174f2012-10-02 20:25:04 -0700395struct file *sock_alloc_file(struct socket *sock, int flags, const char *dname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
Al Viro7cbe66b2009-08-05 19:59:08 +0400397 struct file *file;
David S. Miller39d8c1b2006-03-20 17:13:49 -0800398
Al Virod93aa9d2018-06-09 09:40:05 -0400399 if (!dname)
400 dname = sock->sk ? sock->sk->sk_prot_creator->name : "";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Al Virod93aa9d2018-06-09 09:40:05 -0400402 file = alloc_file_pseudo(SOCK_INODE(sock), sock_mnt, dname,
403 O_RDWR | (flags & O_NONBLOCK),
404 &socket_file_ops);
Viresh Kumarb5ffe632015-08-12 15:59:47 +0530405 if (IS_ERR(file)) {
Al Viro8e1611e2017-12-05 23:29:09 +0000406 sock_release(sock);
Anatol Pomozov39b65252012-09-12 20:11:55 -0700407 return file;
Al Virocc3808f2009-08-06 09:43:59 +0400408 }
David S. Miller39d8c1b2006-03-20 17:13:49 -0800409
410 sock->file = file;
David S. Miller39d8c1b2006-03-20 17:13:49 -0800411 file->private_data = sock;
Linus Torvaldsd8e464e2019-11-17 11:20:48 -0800412 stream_open(SOCK_INODE(sock), file);
Al Viro28407632012-08-17 23:54:15 -0400413 return file;
David S. Miller39d8c1b2006-03-20 17:13:49 -0800414}
Al Viro56b31d12012-08-18 00:25:51 -0400415EXPORT_SYMBOL(sock_alloc_file);
David S. Miller39d8c1b2006-03-20 17:13:49 -0800416
Al Viro56b31d12012-08-18 00:25:51 -0400417static int sock_map_fd(struct socket *sock, int flags)
David S. Miller39d8c1b2006-03-20 17:13:49 -0800418{
419 struct file *newfile;
Al Viro28407632012-08-17 23:54:15 -0400420 int fd = get_unused_fd_flags(flags);
Al Viroce4bb042018-01-10 18:47:05 -0500421 if (unlikely(fd < 0)) {
422 sock_release(sock);
Al Viro28407632012-08-17 23:54:15 -0400423 return fd;
Al Viroce4bb042018-01-10 18:47:05 -0500424 }
David S. Miller39d8c1b2006-03-20 17:13:49 -0800425
Linus Torvaldsaab174f2012-10-02 20:25:04 -0700426 newfile = sock_alloc_file(sock, flags, NULL);
Enrico Weigelt4546e442019-06-05 22:58:50 +0200427 if (!IS_ERR(newfile)) {
David S. Miller39d8c1b2006-03-20 17:13:49 -0800428 fd_install(fd, newfile);
Al Viro28407632012-08-17 23:54:15 -0400429 return fd;
430 }
Al Viro7cbe66b2009-08-05 19:59:08 +0400431
Al Viro28407632012-08-17 23:54:15 -0400432 put_unused_fd(fd);
433 return PTR_ERR(newfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300436/**
437 * sock_from_file - Return the &socket bounded to @file.
438 * @file: file
439 * @err: pointer to an error code return
440 *
441 * On failure returns %NULL and assigns -ENOTSOCK to @err.
442 */
443
John Fastabend406a3c62012-07-20 10:39:25 +0000444struct socket *sock_from_file(struct file *file, int *err)
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800445{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800446 if (file->f_op == &socket_file_ops)
447 return file->private_data; /* set in sock_map_fd */
448
Eric Dumazet23bb80d2007-02-08 14:59:57 -0800449 *err = -ENOTSOCK;
450 return NULL;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800451}
John Fastabend406a3c62012-07-20 10:39:25 +0000452EXPORT_SYMBOL(sock_from_file);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454/**
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700455 * sockfd_lookup - Go from a file number to its socket slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 * @fd: file handle
457 * @err: pointer to an error code return
458 *
459 * The file handle passed in is locked and the socket it is bound
Rosen, Rami241c4662017-05-21 22:12:38 +0300460 * to is returned. If an error occurs the err pointer is overwritten
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 * with a negative errno code and NULL is returned. The function checks
462 * for both invalid handles and passing a handle which is not a socket.
463 *
464 * On a success the socket object pointer is returned.
465 */
466
467struct socket *sockfd_lookup(int fd, int *err)
468{
469 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 struct socket *sock;
471
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700472 file = fget(fd);
473 if (!file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 *err = -EBADF;
475 return NULL;
476 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700477
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800478 sock = sock_from_file(file, err);
479 if (!sock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 fput(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 return sock;
482}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700483EXPORT_SYMBOL(sockfd_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800485static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed)
486{
Al Viro00e188e2014-03-03 23:48:18 -0500487 struct fd f = fdget(fd);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800488 struct socket *sock;
489
Hua Zhong36725582006-04-19 15:25:02 -0700490 *err = -EBADF;
Al Viro00e188e2014-03-03 23:48:18 -0500491 if (f.file) {
492 sock = sock_from_file(f.file, err);
493 if (likely(sock)) {
494 *fput_needed = f.flags;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800495 return sock;
Al Viro00e188e2014-03-03 23:48:18 -0500496 }
497 fdput(f);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800498 }
499 return NULL;
500}
501
Masatake YAMATO600e1772012-08-29 10:44:29 +0000502static ssize_t sockfs_listxattr(struct dentry *dentry, char *buffer,
503 size_t size)
504{
505 ssize_t len;
506 ssize_t used = 0;
507
David Howellsc5ef6032015-03-17 22:26:16 +0000508 len = security_inode_listsecurity(d_inode(dentry), buffer, size);
Masatake YAMATO600e1772012-08-29 10:44:29 +0000509 if (len < 0)
510 return len;
511 used += len;
512 if (buffer) {
513 if (size < used)
514 return -ERANGE;
515 buffer += len;
516 }
517
518 len = (XATTR_NAME_SOCKPROTONAME_LEN + 1);
519 used += len;
520 if (buffer) {
521 if (size < used)
522 return -ERANGE;
523 memcpy(buffer, XATTR_NAME_SOCKPROTONAME, len);
524 buffer += len;
525 }
526
527 return used;
528}
529
Tobias Klauserdc647ec2017-01-10 09:30:51 +0100530static int sockfs_setattr(struct dentry *dentry, struct iattr *iattr)
Lorenzo Colitti86741ec2016-11-04 02:23:41 +0900531{
532 int err = simple_setattr(dentry, iattr);
533
Eric Biggerse1a3a602016-12-30 17:42:32 -0600534 if (!err && (iattr->ia_valid & ATTR_UID)) {
Lorenzo Colitti86741ec2016-11-04 02:23:41 +0900535 struct socket *sock = SOCKET_I(d_inode(dentry));
536
Cong Wang6d8c50d2018-06-07 13:39:49 -0700537 if (sock->sk)
538 sock->sk->sk_uid = iattr->ia_uid;
539 else
540 err = -ENOENT;
Lorenzo Colitti86741ec2016-11-04 02:23:41 +0900541 }
542
543 return err;
544}
545
Masatake YAMATO600e1772012-08-29 10:44:29 +0000546static const struct inode_operations sockfs_inode_ops = {
Masatake YAMATO600e1772012-08-29 10:44:29 +0000547 .listxattr = sockfs_listxattr,
Lorenzo Colitti86741ec2016-11-04 02:23:41 +0900548 .setattr = sockfs_setattr,
Masatake YAMATO600e1772012-08-29 10:44:29 +0000549};
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551/**
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300552 * sock_alloc - allocate a socket
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700553 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 * Allocate a new inode and socket object. The two are bound together
555 * and initialised. The socket is then returned. If we are out of inodes
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300556 * NULL is returned. This functions uses GFP_KERNEL internally.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 */
558
Tom Herbertf4a00aa2016-03-07 14:11:01 -0800559struct socket *sock_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700561 struct inode *inode;
562 struct socket *sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Eric Dumazeta209dfc2011-07-26 11:36:34 +0200564 inode = new_inode_pseudo(sock_mnt->mnt_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 if (!inode)
566 return NULL;
567
568 sock = SOCKET_I(inode);
569
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400570 inode->i_ino = get_next_ino();
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700571 inode->i_mode = S_IFSOCK | S_IRWXUGO;
David Howells8192b0c2008-11-14 10:39:10 +1100572 inode->i_uid = current_fsuid();
573 inode->i_gid = current_fsgid();
Masatake YAMATO600e1772012-08-29 10:44:29 +0000574 inode->i_op = &sockfs_inode_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 return sock;
577}
Tom Herbertf4a00aa2016-03-07 14:11:01 -0800578EXPORT_SYMBOL(sock_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580/**
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300581 * sock_release - close a socket
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 * @sock: socket to close
583 *
584 * The socket is released from the protocol stack if it has a release
585 * callback, and the inode is then released if the socket is bound to
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700586 * an inode not a file.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700588
Cong Wang6d8c50d2018-06-07 13:39:49 -0700589static void __sock_release(struct socket *sock, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
591 if (sock->ops) {
592 struct module *owner = sock->ops->owner;
593
Cong Wang6d8c50d2018-06-07 13:39:49 -0700594 if (inode)
595 inode_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 sock->ops->release(sock);
Eric Biggersff7b11a2019-02-21 14:13:56 -0800597 sock->sk = NULL;
Cong Wang6d8c50d2018-06-07 13:39:49 -0700598 if (inode)
599 inode_unlock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 sock->ops = NULL;
601 module_put(owner);
602 }
603
Al Viro333f7902019-07-05 20:14:16 +0100604 if (sock->wq.fasync_list)
Yang Yingliang3410f222014-02-12 17:09:55 +0800605 pr_err("%s: fasync list not empty!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 if (!sock->file) {
608 iput(SOCK_INODE(sock));
609 return;
610 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700611 sock->file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612}
Cong Wang6d8c50d2018-06-07 13:39:49 -0700613
614void sock_release(struct socket *sock)
615{
616 __sock_release(sock, NULL);
617}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700618EXPORT_SYMBOL(sock_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -0400620void __sock_tx_timestamp(__u16 tsflags, __u8 *tx_flags)
Patrick Ohly20d49472009-02-12 05:03:38 +0000621{
Eric Dumazet140c55d2014-08-06 11:49:29 +0200622 u8 flags = *tx_flags;
623
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -0400624 if (tsflags & SOF_TIMESTAMPING_TX_HARDWARE)
Eric Dumazet140c55d2014-08-06 11:49:29 +0200625 flags |= SKBTX_HW_TSTAMP;
626
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -0400627 if (tsflags & SOF_TIMESTAMPING_TX_SOFTWARE)
Eric Dumazet140c55d2014-08-06 11:49:29 +0200628 flags |= SKBTX_SW_TSTAMP;
629
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -0400630 if (tsflags & SOF_TIMESTAMPING_TX_SCHED)
Eric Dumazet140c55d2014-08-06 11:49:29 +0200631 flags |= SKBTX_SCHED_TSTAMP;
632
Eric Dumazet140c55d2014-08-06 11:49:29 +0200633 *tx_flags = flags;
Patrick Ohly20d49472009-02-12 05:03:38 +0000634}
Willem de Bruijn67cc0d42014-09-08 19:58:58 -0400635EXPORT_SYMBOL(__sock_tx_timestamp);
Patrick Ohly20d49472009-02-12 05:03:38 +0000636
Paolo Abeni8c3c4472019-05-03 17:01:39 +0200637INDIRECT_CALLABLE_DECLARE(int inet_sendmsg(struct socket *, struct msghdr *,
638 size_t));
Paolo Abenia648a592019-07-03 16:06:54 +0200639INDIRECT_CALLABLE_DECLARE(int inet6_sendmsg(struct socket *, struct msghdr *,
640 size_t));
Al Virod8725c82014-12-11 00:02:50 -0500641static inline int sock_sendmsg_nosec(struct socket *sock, struct msghdr *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
Paolo Abenia648a592019-07-03 16:06:54 +0200643 int ret = INDIRECT_CALL_INET(sock->ops->sendmsg, inet6_sendmsg,
644 inet_sendmsg, sock, msg,
645 msg_data_left(msg));
Al Virod8725c82014-12-11 00:02:50 -0500646 BUG_ON(ret == -EIOCBQUEUED);
647 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648}
Gu Zheng0cf00c62014-12-05 15:14:23 +0800649
Randy Dunlap85806af2019-05-18 21:23:07 -0700650/**
651 * sock_sendmsg - send a message through @sock
652 * @sock: socket
653 * @msg: message to send
654 *
655 * Sends @msg through @sock, passing through LSM.
656 * Returns the number of bytes sent, or an error code.
657 */
Al Virod8725c82014-12-11 00:02:50 -0500658int sock_sendmsg(struct socket *sock, struct msghdr *msg)
Gu Zheng0cf00c62014-12-05 15:14:23 +0800659{
Al Virod8725c82014-12-11 00:02:50 -0500660 int err = security_socket_sendmsg(sock, msg,
Al Viro01e97e62014-12-15 21:39:31 -0500661 msg_data_left(msg));
Ying Xue1b784142015-03-02 15:37:48 +0800662
Al Virod8725c82014-12-11 00:02:50 -0500663 return err ?: sock_sendmsg_nosec(sock, msg);
Gu Zheng0cf00c62014-12-05 15:14:23 +0800664}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700665EXPORT_SYMBOL(sock_sendmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300667/**
668 * kernel_sendmsg - send a message through @sock (kernel-space)
669 * @sock: socket
670 * @msg: message header
671 * @vec: kernel vec
672 * @num: vec array length
673 * @size: total message data size
674 *
675 * Builds the message data with @vec and sends it through @sock.
676 * Returns the number of bytes sent, or an error code.
677 */
678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
680 struct kvec *vec, size_t num, size_t size)
681{
David Howellsaa563d72018-10-20 00:57:56 +0100682 iov_iter_kvec(&msg->msg_iter, WRITE, vec, num, size);
Al Virod8725c82014-12-11 00:02:50 -0500683 return sock_sendmsg(sock, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700685EXPORT_SYMBOL(kernel_sendmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300687/**
688 * kernel_sendmsg_locked - send a message through @sock (kernel-space)
689 * @sk: sock
690 * @msg: message header
691 * @vec: output s/g array
692 * @num: output s/g array length
693 * @size: total message data size
694 *
695 * Builds the message data with @vec and sends it through @sock.
696 * Returns the number of bytes sent, or an error code.
697 * Caller must hold @sk.
698 */
699
Tom Herbert306b13e2017-07-28 16:22:41 -0700700int kernel_sendmsg_locked(struct sock *sk, struct msghdr *msg,
701 struct kvec *vec, size_t num, size_t size)
702{
703 struct socket *sock = sk->sk_socket;
704
705 if (!sock->ops->sendmsg_locked)
John Fastabenddb5980d2017-08-15 22:31:34 -0700706 return sock_no_sendmsg_locked(sk, msg, size);
Tom Herbert306b13e2017-07-28 16:22:41 -0700707
David Howellsaa563d72018-10-20 00:57:56 +0100708 iov_iter_kvec(&msg->msg_iter, WRITE, vec, num, size);
Tom Herbert306b13e2017-07-28 16:22:41 -0700709
710 return sock->ops->sendmsg_locked(sk, msg, msg_data_left(msg));
711}
712EXPORT_SYMBOL(kernel_sendmsg_locked);
713
Soheil Hassas Yeganeh8605330a2017-03-18 17:02:59 -0400714static bool skb_is_err_queue(const struct sk_buff *skb)
715{
716 /* pkt_type of skbs enqueued on the error queue are set to
717 * PACKET_OUTGOING in skb_set_err_queue(). This is only safe to do
718 * in recvmsg, since skbs received on a local socket will never
719 * have a pkt_type of PACKET_OUTGOING.
720 */
721 return skb->pkt_type == PACKET_OUTGOING;
722}
723
Miroslav Lichvarb50a5c72017-05-19 17:52:40 +0200724/* On transmit, software and hardware timestamps are returned independently.
725 * As the two skb clones share the hardware timestamp, which may be updated
726 * before the software timestamp is received, a hardware TX timestamp may be
727 * returned only if there is no software TX timestamp. Ignore false software
728 * timestamps, which may be made in the __sock_recv_timestamp() call when the
Deepa Dinamani7f1bc6e2019-02-02 07:34:46 -0800729 * option SO_TIMESTAMP_OLD(NS) is enabled on the socket, even when the skb has a
Miroslav Lichvarb50a5c72017-05-19 17:52:40 +0200730 * hardware timestamp.
731 */
732static bool skb_is_swtx_tstamp(const struct sk_buff *skb, int false_tstamp)
733{
734 return skb->tstamp && !false_tstamp && skb_is_err_queue(skb);
735}
736
Miroslav Lichvaraad9c8c2017-05-19 17:52:38 +0200737static void put_ts_pktinfo(struct msghdr *msg, struct sk_buff *skb)
738{
739 struct scm_ts_pktinfo ts_pktinfo;
740 struct net_device *orig_dev;
741
742 if (!skb_mac_header_was_set(skb))
743 return;
744
745 memset(&ts_pktinfo, 0, sizeof(ts_pktinfo));
746
747 rcu_read_lock();
748 orig_dev = dev_get_by_napi_id(skb_napi_id(skb));
749 if (orig_dev)
750 ts_pktinfo.if_index = orig_dev->ifindex;
751 rcu_read_unlock();
752
753 ts_pktinfo.pkt_length = skb->len - skb_mac_offset(skb);
754 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPING_PKTINFO,
755 sizeof(ts_pktinfo), &ts_pktinfo);
756}
757
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700758/*
759 * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
760 */
761void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
762 struct sk_buff *skb)
763{
Patrick Ohly20d49472009-02-12 05:03:38 +0000764 int need_software_tstamp = sock_flag(sk, SOCK_RCVTSTAMP);
Deepa Dinamani887feae2019-02-02 07:34:50 -0800765 int new_tstamp = sock_flag(sk, SOCK_TSTAMP_NEW);
Deepa Dinamani97184752019-02-02 07:34:51 -0800766 struct scm_timestamping_internal tss;
767
Miroslav Lichvarb50a5c72017-05-19 17:52:40 +0200768 int empty = 1, false_tstamp = 0;
Patrick Ohly20d49472009-02-12 05:03:38 +0000769 struct skb_shared_hwtstamps *shhwtstamps =
770 skb_hwtstamps(skb);
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700771
Patrick Ohly20d49472009-02-12 05:03:38 +0000772 /* Race occurred between timestamp enabling and packet
773 receiving. Fill in the current time for now. */
Miroslav Lichvarb50a5c72017-05-19 17:52:40 +0200774 if (need_software_tstamp && skb->tstamp == 0) {
Patrick Ohly20d49472009-02-12 05:03:38 +0000775 __net_timestamp(skb);
Miroslav Lichvarb50a5c72017-05-19 17:52:40 +0200776 false_tstamp = 1;
777 }
Patrick Ohly20d49472009-02-12 05:03:38 +0000778
779 if (need_software_tstamp) {
780 if (!sock_flag(sk, SOCK_RCVTSTAMPNS)) {
Deepa Dinamani887feae2019-02-02 07:34:50 -0800781 if (new_tstamp) {
782 struct __kernel_sock_timeval tv;
783
784 skb_get_new_timestamp(skb, &tv);
785 put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMP_NEW,
786 sizeof(tv), &tv);
787 } else {
788 struct __kernel_old_timeval tv;
789
790 skb_get_timestamp(skb, &tv);
791 put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMP_OLD,
792 sizeof(tv), &tv);
793 }
Patrick Ohly20d49472009-02-12 05:03:38 +0000794 } else {
Deepa Dinamani887feae2019-02-02 07:34:50 -0800795 if (new_tstamp) {
796 struct __kernel_timespec ts;
797
798 skb_get_new_timestampns(skb, &ts);
799 put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMPNS_NEW,
800 sizeof(ts), &ts);
801 } else {
Arnd Bergmanndf1b4ba2019-10-25 22:04:46 +0200802 struct __kernel_old_timespec ts;
Deepa Dinamani887feae2019-02-02 07:34:50 -0800803
804 skb_get_timestampns(skb, &ts);
805 put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMPNS_OLD,
806 sizeof(ts), &ts);
807 }
Patrick Ohly20d49472009-02-12 05:03:38 +0000808 }
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700809 }
Patrick Ohly20d49472009-02-12 05:03:38 +0000810
Willem de Bruijnf24b9be2014-08-04 22:11:45 -0400811 memset(&tss, 0, sizeof(tss));
Willem de Bruijnc1991052014-09-03 12:01:18 -0400812 if ((sk->sk_tsflags & SOF_TIMESTAMPING_SOFTWARE) &&
Deepa Dinamani97184752019-02-02 07:34:51 -0800813 ktime_to_timespec64_cond(skb->tstamp, tss.ts + 0))
Patrick Ohly20d49472009-02-12 05:03:38 +0000814 empty = 0;
Willem de Bruijn4d276eb2014-07-25 18:01:32 -0400815 if (shhwtstamps &&
Willem de Bruijnb9f40e22014-08-04 22:11:46 -0400816 (sk->sk_tsflags & SOF_TIMESTAMPING_RAW_HARDWARE) &&
Miroslav Lichvarb50a5c72017-05-19 17:52:40 +0200817 !skb_is_swtx_tstamp(skb, false_tstamp) &&
Deepa Dinamani97184752019-02-02 07:34:51 -0800818 ktime_to_timespec64_cond(shhwtstamps->hwtstamp, tss.ts + 2)) {
Willem de Bruijn4d276eb2014-07-25 18:01:32 -0400819 empty = 0;
Miroslav Lichvaraad9c8c2017-05-19 17:52:38 +0200820 if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_PKTINFO) &&
821 !skb_is_err_queue(skb))
822 put_ts_pktinfo(msg, skb);
823 }
Francis Yan1c885802016-11-27 23:07:18 -0800824 if (!empty) {
Deepa Dinamani97184752019-02-02 07:34:51 -0800825 if (sock_flag(sk, SOCK_TSTAMP_NEW))
826 put_cmsg_scm_timestamping64(msg, &tss);
827 else
828 put_cmsg_scm_timestamping(msg, &tss);
Francis Yan1c885802016-11-27 23:07:18 -0800829
Soheil Hassas Yeganeh8605330a2017-03-18 17:02:59 -0400830 if (skb_is_err_queue(skb) && skb->len &&
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -0400831 SKB_EXT_ERR(skb)->opt_stats)
Francis Yan1c885802016-11-27 23:07:18 -0800832 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPING_OPT_STATS,
833 skb->len, skb->data);
834 }
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700835}
Arnaldo Carvalho de Melo7c81fd82007-03-10 00:39:35 -0300836EXPORT_SYMBOL_GPL(__sock_recv_timestamp);
837
Johannes Berg6e3e9392011-11-09 10:15:42 +0100838void __sock_recv_wifi_status(struct msghdr *msg, struct sock *sk,
839 struct sk_buff *skb)
840{
841 int ack;
842
843 if (!sock_flag(sk, SOCK_WIFI_STATUS))
844 return;
845 if (!skb->wifi_acked_valid)
846 return;
847
848 ack = skb->wifi_acked;
849
850 put_cmsg(msg, SOL_SOCKET, SCM_WIFI_STATUS, sizeof(ack), &ack);
851}
852EXPORT_SYMBOL_GPL(__sock_recv_wifi_status);
853
stephen hemminger11165f12010-10-18 14:27:29 +0000854static inline void sock_recv_drops(struct msghdr *msg, struct sock *sk,
855 struct sk_buff *skb)
Neil Horman3b885782009-10-12 13:26:31 -0700856{
Eyal Birger744d5a32015-03-01 14:58:31 +0200857 if (sock_flag(sk, SOCK_RXQ_OVFL) && skb && SOCK_SKB_CB(skb)->dropcount)
Neil Horman3b885782009-10-12 13:26:31 -0700858 put_cmsg(msg, SOL_SOCKET, SO_RXQ_OVFL,
Eyal Birger744d5a32015-03-01 14:58:31 +0200859 sizeof(__u32), &SOCK_SKB_CB(skb)->dropcount);
Neil Horman3b885782009-10-12 13:26:31 -0700860}
861
Eric Dumazet767dd032010-04-28 19:14:43 +0000862void __sock_recv_ts_and_drops(struct msghdr *msg, struct sock *sk,
Neil Horman3b885782009-10-12 13:26:31 -0700863 struct sk_buff *skb)
864{
865 sock_recv_timestamp(msg, sk, skb);
866 sock_recv_drops(msg, sk, skb);
867}
Eric Dumazet767dd032010-04-28 19:14:43 +0000868EXPORT_SYMBOL_GPL(__sock_recv_ts_and_drops);
Neil Horman3b885782009-10-12 13:26:31 -0700869
Paolo Abeni8c3c4472019-05-03 17:01:39 +0200870INDIRECT_CALLABLE_DECLARE(int inet_recvmsg(struct socket *, struct msghdr *,
Paolo Abenia648a592019-07-03 16:06:54 +0200871 size_t, int));
872INDIRECT_CALLABLE_DECLARE(int inet6_recvmsg(struct socket *, struct msghdr *,
873 size_t, int));
Ying Xue1b784142015-03-02 15:37:48 +0800874static inline int sock_recvmsg_nosec(struct socket *sock, struct msghdr *msg,
Al Viro2da62902015-03-14 21:13:46 -0400875 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876{
Paolo Abenia648a592019-07-03 16:06:54 +0200877 return INDIRECT_CALL_INET(sock->ops->recvmsg, inet6_recvmsg,
878 inet_recvmsg, sock, msg, msg_data_left(msg),
879 flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880}
881
Randy Dunlap85806af2019-05-18 21:23:07 -0700882/**
883 * sock_recvmsg - receive a message from @sock
884 * @sock: socket
885 * @msg: message to receive
886 * @flags: message flags
887 *
888 * Receives @msg from @sock, passing through LSM. Returns the total number
889 * of bytes received, or an error.
890 */
Al Viro2da62902015-03-14 21:13:46 -0400891int sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -0700892{
Al Viro2da62902015-03-14 21:13:46 -0400893 int err = security_socket_recvmsg(sock, msg, msg_data_left(msg), flags);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -0700894
Al Viro2da62902015-03-14 21:13:46 -0400895 return err ?: sock_recvmsg_nosec(sock, msg, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700897EXPORT_SYMBOL(sock_recvmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Martin Lucinac1249c02010-12-10 00:04:05 +0000899/**
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300900 * kernel_recvmsg - Receive a message from a socket (kernel space)
901 * @sock: The socket to receive the message from
902 * @msg: Received message
903 * @vec: Input s/g array for message data
904 * @num: Size of input s/g array
905 * @size: Number of bytes to read
906 * @flags: Message flags (MSG_DONTWAIT, etc...)
Martin Lucinac1249c02010-12-10 00:04:05 +0000907 *
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300908 * On return the msg structure contains the scatter/gather array passed in the
909 * vec argument. The array is modified so that it consists of the unfilled
910 * portion of the original array.
Martin Lucinac1249c02010-12-10 00:04:05 +0000911 *
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300912 * The returned value is the total number of bytes received, or an error.
Martin Lucinac1249c02010-12-10 00:04:05 +0000913 */
Pedro Tammela8a3c2452019-03-14 10:45:23 -0300914
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700915int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
916 struct kvec *vec, size_t num, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
918 mm_segment_t oldfs = get_fs();
919 int result;
920
David Howellsaa563d72018-10-20 00:57:56 +0100921 iov_iter_kvec(&msg->msg_iter, READ, vec, num, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 set_fs(KERNEL_DS);
Al Viro2da62902015-03-14 21:13:46 -0400923 result = sock_recvmsg(sock, msg, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 set_fs(oldfs);
925 return result;
926}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700927EXPORT_SYMBOL(kernel_recvmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -0300929static ssize_t sock_sendpage(struct file *file, struct page *page,
930 int offset, size_t size, loff_t *ppos, int more)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931{
932 struct socket *sock;
933 int flags;
934
Eric Dumazetb69aee02005-09-06 14:42:45 -0700935 sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Eric Dumazet35f9c092012-04-05 03:05:35 +0000937 flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
938 /* more is a combination of MSG_MORE and MSG_SENDPAGE_NOTLAST */
939 flags |= more;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Linus Torvaldse6949582009-08-13 08:28:36 -0700941 return kernel_sendpage(sock, page, offset, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942}
943
Jens Axboe9c55e012007-11-06 23:30:13 -0800944static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700945 struct pipe_inode_info *pipe, size_t len,
Jens Axboe9c55e012007-11-06 23:30:13 -0800946 unsigned int flags)
947{
948 struct socket *sock = file->private_data;
949
Rémi Denis-Courmont997b37d2008-02-15 02:35:45 -0800950 if (unlikely(!sock->ops->splice_read))
Slavomir Kaslev95506582018-11-16 11:27:53 +0200951 return generic_file_splice_read(file, ppos, pipe, len, flags);
Rémi Denis-Courmont997b37d2008-02-15 02:35:45 -0800952
Jens Axboe9c55e012007-11-06 23:30:13 -0800953 return sock->ops->splice_read(sock, ppos, pipe, len, flags);
954}
955
Al Viro8ae5e0302014-11-28 19:40:50 -0500956static ssize_t sock_read_iter(struct kiocb *iocb, struct iov_iter *to)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800957{
Al Viro6d652332015-01-30 16:12:56 -0500958 struct file *file = iocb->ki_filp;
959 struct socket *sock = file->private_data;
tadeusz.struk@intel.com0345f932015-03-19 12:31:25 -0700960 struct msghdr msg = {.msg_iter = *to,
961 .msg_iocb = iocb};
Al Viro8ae5e0302014-11-28 19:40:50 -0500962 ssize_t res;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800963
Al Viro8ae5e0302014-11-28 19:40:50 -0500964 if (file->f_flags & O_NONBLOCK)
965 msg.msg_flags = MSG_DONTWAIT;
966
967 if (iocb->ki_pos != 0)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800968 return -ESPIPE;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700969
Christoph Hellwig66ee59a2015-02-11 19:56:46 +0100970 if (!iov_iter_count(to)) /* Match SYS5 behaviour */
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800971 return 0;
972
Al Viro2da62902015-03-14 21:13:46 -0400973 res = sock_recvmsg(sock, &msg, msg.msg_flags);
Al Viro8ae5e0302014-11-28 19:40:50 -0500974 *to = msg.msg_iter;
975 return res;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800976}
977
Al Viro8ae5e0302014-11-28 19:40:50 -0500978static ssize_t sock_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
Al Viro6d652332015-01-30 16:12:56 -0500980 struct file *file = iocb->ki_filp;
981 struct socket *sock = file->private_data;
tadeusz.struk@intel.com0345f932015-03-19 12:31:25 -0700982 struct msghdr msg = {.msg_iter = *from,
983 .msg_iocb = iocb};
Al Viro8ae5e0302014-11-28 19:40:50 -0500984 ssize_t res;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800985
Al Viro8ae5e0302014-11-28 19:40:50 -0500986 if (iocb->ki_pos != 0)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800987 return -ESPIPE;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700988
Al Viro8ae5e0302014-11-28 19:40:50 -0500989 if (file->f_flags & O_NONBLOCK)
990 msg.msg_flags = MSG_DONTWAIT;
991
Al Viro6d652332015-01-30 16:12:56 -0500992 if (sock->type == SOCK_SEQPACKET)
993 msg.msg_flags |= MSG_EOR;
994
Al Virod8725c82014-12-11 00:02:50 -0500995 res = sock_sendmsg(sock, &msg);
Al Viro8ae5e0302014-11-28 19:40:50 -0500996 *from = msg.msg_iter;
997 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998}
999
Kirill Tkhaib4653342019-12-09 13:03:40 +03001000static void sock_show_fdinfo(struct seq_file *m, struct file *f)
1001{
1002 struct socket *sock = f->private_data;
1003
1004 if (sock->ops->show_fdinfo)
1005 sock->ops->show_fdinfo(m, sock);
1006}
1007
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008/*
1009 * Atomic setting of ioctl hooks to avoid race
1010 * with module unload.
1011 */
1012
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001013static DEFINE_MUTEX(br_ioctl_mutex);
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001014static int (*br_ioctl_hook) (struct net *, unsigned int cmd, void __user *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Eric W. Biederman881d9662007-09-17 11:56:21 -07001016void brioctl_set(int (*hook) (struct net *, unsigned int, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001018 mutex_lock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 br_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001020 mutex_unlock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021}
1022EXPORT_SYMBOL(brioctl_set);
1023
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001024static DEFINE_MUTEX(vlan_ioctl_mutex);
Eric W. Biederman881d9662007-09-17 11:56:21 -07001025static int (*vlan_ioctl_hook) (struct net *, void __user *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
Eric W. Biederman881d9662007-09-17 11:56:21 -07001027void vlan_ioctl_set(int (*hook) (struct net *, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001029 mutex_lock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 vlan_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001031 mutex_unlock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032}
1033EXPORT_SYMBOL(vlan_ioctl_set);
1034
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001035static DEFINE_MUTEX(dlci_ioctl_mutex);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001036static int (*dlci_ioctl_hook) (unsigned int, void __user *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001038void dlci_ioctl_set(int (*hook) (unsigned int, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001040 mutex_lock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 dlci_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001042 mutex_unlock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043}
1044EXPORT_SYMBOL(dlci_ioctl_set);
1045
Arnd Bergmann6b960182009-11-06 23:10:54 -08001046static long sock_do_ioctl(struct net *net, struct socket *sock,
Johannes Berg63ff03a2019-01-25 22:43:17 +01001047 unsigned int cmd, unsigned long arg)
Arnd Bergmann6b960182009-11-06 23:10:54 -08001048{
1049 int err;
1050 void __user *argp = (void __user *)arg;
1051
1052 err = sock->ops->ioctl(sock, cmd, arg);
1053
1054 /*
1055 * If this ioctl is unknown try to hand it down
1056 * to the NIC driver.
1057 */
Al Viro36fd6332017-06-26 13:19:16 -04001058 if (err != -ENOIOCTLCMD)
1059 return err;
Arnd Bergmann6b960182009-11-06 23:10:54 -08001060
Al Viro36fd6332017-06-26 13:19:16 -04001061 if (cmd == SIOCGIFCONF) {
1062 struct ifconf ifc;
1063 if (copy_from_user(&ifc, argp, sizeof(struct ifconf)))
1064 return -EFAULT;
1065 rtnl_lock();
1066 err = dev_ifconf(net, &ifc, sizeof(struct ifreq));
1067 rtnl_unlock();
1068 if (!err && copy_to_user(argp, &ifc, sizeof(struct ifconf)))
1069 err = -EFAULT;
Al Viro44c02a22017-10-05 12:59:44 -04001070 } else {
1071 struct ifreq ifr;
1072 bool need_copyout;
Johannes Berg63ff03a2019-01-25 22:43:17 +01001073 if (copy_from_user(&ifr, argp, sizeof(struct ifreq)))
Al Viro44c02a22017-10-05 12:59:44 -04001074 return -EFAULT;
1075 err = dev_ioctl(net, cmd, &ifr, &need_copyout);
1076 if (!err && need_copyout)
Johannes Berg63ff03a2019-01-25 22:43:17 +01001077 if (copy_to_user(argp, &ifr, sizeof(struct ifreq)))
Al Viro44c02a22017-10-05 12:59:44 -04001078 return -EFAULT;
Al Viro36fd6332017-06-26 13:19:16 -04001079 }
Arnd Bergmann6b960182009-11-06 23:10:54 -08001080 return err;
1081}
1082
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083/*
1084 * With an ioctl, arg may well be a user mode pointer, but we don't know
1085 * what to do with it - that's up to the protocol still.
1086 */
1087
Pedro Tammela8a3c2452019-03-14 10:45:23 -03001088/**
1089 * get_net_ns - increment the refcount of the network namespace
1090 * @ns: common namespace (net)
1091 *
1092 * Returns the net's common namespace.
1093 */
1094
Kirill Tkhaid8d211a2018-02-14 16:39:56 +03001095struct ns_common *get_net_ns(struct ns_common *ns)
Andrey Vaginc62cce22016-10-24 18:29:13 -07001096{
1097 return &get_net(container_of(ns, struct net, ns))->ns;
1098}
Kirill Tkhaid8d211a2018-02-14 16:39:56 +03001099EXPORT_SYMBOL_GPL(get_net_ns);
Andrey Vaginc62cce22016-10-24 18:29:13 -07001100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
1102{
1103 struct socket *sock;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001104 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 void __user *argp = (void __user *)arg;
1106 int pid, err;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001107 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Eric Dumazetb69aee02005-09-06 14:42:45 -07001109 sock = file->private_data;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001110 sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001111 net = sock_net(sk);
Al Viro44c02a22017-10-05 12:59:44 -04001112 if (unlikely(cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15))) {
1113 struct ifreq ifr;
1114 bool need_copyout;
1115 if (copy_from_user(&ifr, argp, sizeof(struct ifreq)))
1116 return -EFAULT;
1117 err = dev_ioctl(net, cmd, &ifr, &need_copyout);
1118 if (!err && need_copyout)
1119 if (copy_to_user(argp, &ifr, sizeof(struct ifreq)))
1120 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 } else
Johannes Berg3d23e342009-09-29 23:27:28 +02001122#ifdef CONFIG_WEXT_CORE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
Al Virob1b0c242017-10-01 20:13:08 -04001124 err = wext_handle_ioctl(net, cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 } else
Johannes Berg3d23e342009-09-29 23:27:28 +02001126#endif
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001127 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 case FIOSETOWN:
1129 case SIOCSPGRP:
1130 err = -EFAULT;
1131 if (get_user(pid, (int __user *)argp))
1132 break;
Jiri Slaby393cc3f2017-06-13 13:35:50 +02001133 err = f_setown(sock->file, pid, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 break;
1135 case FIOGETOWN:
1136 case SIOCGPGRP:
Eric W. Biederman609d7fa2006-10-02 02:17:15 -07001137 err = put_user(f_getown(sock->file),
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001138 (int __user *)argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 break;
1140 case SIOCGIFBR:
1141 case SIOCSIFBR:
1142 case SIOCBRADDBR:
1143 case SIOCBRDELBR:
1144 err = -ENOPKG;
1145 if (!br_ioctl_hook)
1146 request_module("bridge");
1147
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001148 mutex_lock(&br_ioctl_mutex);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001149 if (br_ioctl_hook)
Eric W. Biederman881d9662007-09-17 11:56:21 -07001150 err = br_ioctl_hook(net, cmd, argp);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001151 mutex_unlock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 break;
1153 case SIOCGIFVLAN:
1154 case SIOCSIFVLAN:
1155 err = -ENOPKG;
1156 if (!vlan_ioctl_hook)
1157 request_module("8021q");
1158
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001159 mutex_lock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 if (vlan_ioctl_hook)
Eric W. Biederman881d9662007-09-17 11:56:21 -07001161 err = vlan_ioctl_hook(net, argp);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001162 mutex_unlock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 case SIOCADDDLCI:
1165 case SIOCDELDLCI:
1166 err = -ENOPKG;
1167 if (!dlci_ioctl_hook)
1168 request_module("dlci");
1169
Pavel Emelyanov7512cbf2008-03-21 15:58:52 -07001170 mutex_lock(&dlci_ioctl_mutex);
1171 if (dlci_ioctl_hook)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 err = dlci_ioctl_hook(cmd, argp);
Pavel Emelyanov7512cbf2008-03-21 15:58:52 -07001173 mutex_unlock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 break;
Andrey Vaginc62cce22016-10-24 18:29:13 -07001175 case SIOCGSKNS:
1176 err = -EPERM;
1177 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1178 break;
1179
1180 err = open_related_ns(&net->ns, get_net_ns);
1181 break;
Arnd Bergmann0768e172019-04-17 22:56:11 +02001182 case SIOCGSTAMP_OLD:
1183 case SIOCGSTAMPNS_OLD:
Arnd Bergmannc7cbdbf2019-04-17 22:51:48 +02001184 if (!sock->ops->gettstamp) {
1185 err = -ENOIOCTLCMD;
1186 break;
1187 }
1188 err = sock->ops->gettstamp(sock, argp,
Arnd Bergmann0768e172019-04-17 22:56:11 +02001189 cmd == SIOCGSTAMP_OLD,
1190 !IS_ENABLED(CONFIG_64BIT));
Gustavo A. R. Silva60747822019-04-24 10:31:24 -05001191 break;
Arnd Bergmann0768e172019-04-17 22:56:11 +02001192 case SIOCGSTAMP_NEW:
1193 case SIOCGSTAMPNS_NEW:
1194 if (!sock->ops->gettstamp) {
1195 err = -ENOIOCTLCMD;
1196 break;
1197 }
1198 err = sock->ops->gettstamp(sock, argp,
1199 cmd == SIOCGSTAMP_NEW,
1200 false);
Arnd Bergmannc7cbdbf2019-04-17 22:51:48 +02001201 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 default:
Johannes Berg63ff03a2019-01-25 22:43:17 +01001203 err = sock_do_ioctl(net, sock, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 break;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001205 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 return err;
1207}
1208
Pedro Tammela8a3c2452019-03-14 10:45:23 -03001209/**
1210 * sock_create_lite - creates a socket
1211 * @family: protocol family (AF_INET, ...)
1212 * @type: communication type (SOCK_STREAM, ...)
1213 * @protocol: protocol (0, ...)
1214 * @res: new socket
1215 *
1216 * Creates a new socket and assigns it to @res, passing through LSM.
1217 * The new socket initialization is not complete, see kernel_accept().
1218 * Returns 0 or an error. On failure @res is set to %NULL.
1219 * This function internally uses GFP_KERNEL.
1220 */
1221
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222int sock_create_lite(int family, int type, int protocol, struct socket **res)
1223{
1224 int err;
1225 struct socket *sock = NULL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 err = security_socket_create(family, type, protocol, 1);
1228 if (err)
1229 goto out;
1230
1231 sock = sock_alloc();
1232 if (!sock) {
1233 err = -ENOMEM;
1234 goto out;
1235 }
1236
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 sock->type = type;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001238 err = security_socket_post_create(sock, family, type, protocol, 1);
1239 if (err)
1240 goto out_release;
1241
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242out:
1243 *res = sock;
1244 return err;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001245out_release:
1246 sock_release(sock);
1247 sock = NULL;
1248 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001250EXPORT_SYMBOL(sock_create_lite);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
1252/* No kernel lock held - perfect */
Al Viroade994f2017-07-03 00:01:49 -04001253static __poll_t sock_poll(struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254{
Christoph Hellwig3cafb372018-01-09 16:07:34 +01001255 struct socket *sock = file->private_data;
Christoph Hellwiga331de32018-07-30 09:42:13 +02001256 __poll_t events = poll_requested_events(wait), flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Christoph Hellwige88958e2018-06-29 15:37:24 +02001258 if (!sock->ops->poll)
1259 return 0;
Christoph Hellwigf641f13b2018-07-30 09:42:12 +02001260
Christoph Hellwiga331de32018-07-30 09:42:13 +02001261 if (sk_can_busy_loop(sock->sk)) {
1262 /* poll once if requested by the syscall */
1263 if (events & POLL_BUSY_LOOP)
1264 sk_busy_loop(sock->sk, 1);
1265
1266 /* if this socket can poll_ll, tell the system call */
1267 flag = POLL_BUSY_LOOP;
1268 }
1269
1270 return sock->ops->poll(file, sock, wait) | flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271}
1272
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001273static int sock_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274{
Eric Dumazetb69aee02005-09-06 14:42:45 -07001275 struct socket *sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
1277 return sock->ops->mmap(file, sock, vma);
1278}
1279
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001280static int sock_close(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281{
Cong Wang6d8c50d2018-06-07 13:39:49 -07001282 __sock_release(SOCKET_I(inode), inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 return 0;
1284}
1285
1286/*
1287 * Update the socket async list
1288 *
1289 * Fasync_list locking strategy.
1290 *
1291 * 1. fasync_list is modified only under process context socket lock
1292 * i.e. under semaphore.
1293 * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
Eric Dumazet989a2972010-04-14 09:55:35 +00001294 * or under socket lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 */
1296
1297static int sock_fasync(int fd, struct file *filp, int on)
1298{
Eric Dumazet989a2972010-04-14 09:55:35 +00001299 struct socket *sock = filp->private_data;
1300 struct sock *sk = sock->sk;
Al Viro333f7902019-07-05 20:14:16 +01001301 struct socket_wq *wq = &sock->wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
Eric Dumazet989a2972010-04-14 09:55:35 +00001303 if (sk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
1306 lock_sock(sk);
Eric Dumazeteaefd1102011-02-18 03:26:36 +00001307 fasync_helper(fd, filp, on, &wq->fasync_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
Eric Dumazeteaefd1102011-02-18 03:26:36 +00001309 if (!wq->fasync_list)
Eric Dumazet989a2972010-04-14 09:55:35 +00001310 sock_reset_flag(sk, SOCK_FASYNC);
Jonathan Corbet76398422009-02-01 14:26:59 -07001311 else
Eric Dumazetbcdce712009-10-06 17:28:29 -07001312 sock_set_flag(sk, SOCK_FASYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
Eric Dumazet989a2972010-04-14 09:55:35 +00001314 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 return 0;
1316}
1317
Eric Dumazetceb5d582015-11-29 20:03:11 -08001318/* This function may be called only under rcu_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
Eric Dumazetceb5d582015-11-29 20:03:11 -08001320int sock_wake_async(struct socket_wq *wq, int how, int band)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321{
Eric Dumazetceb5d582015-11-29 20:03:11 -08001322 if (!wq || !wq->fasync_list)
1323 return -1;
Eric Dumazet43815482010-04-29 11:01:49 +00001324
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001325 switch (how) {
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001326 case SOCK_WAKE_WAITD:
Eric Dumazetceb5d582015-11-29 20:03:11 -08001327 if (test_bit(SOCKWQ_ASYNC_WAITDATA, &wq->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 break;
1329 goto call_kill;
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001330 case SOCK_WAKE_SPACE:
Eric Dumazetceb5d582015-11-29 20:03:11 -08001331 if (!test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 break;
1333 /* fall through */
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001334 case SOCK_WAKE_IO:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001335call_kill:
Eric Dumazet43815482010-04-29 11:01:49 +00001336 kill_fasync(&wq->fasync_list, SIGIO, band);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 break;
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001338 case SOCK_WAKE_URG:
Eric Dumazet43815482010-04-29 11:01:49 +00001339 kill_fasync(&wq->fasync_list, SIGURG, band);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 }
Eric Dumazetceb5d582015-11-29 20:03:11 -08001341
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 return 0;
1343}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001344EXPORT_SYMBOL(sock_wake_async);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
Pedro Tammela8a3c2452019-03-14 10:45:23 -03001346/**
1347 * __sock_create - creates a socket
1348 * @net: net namespace
1349 * @family: protocol family (AF_INET, ...)
1350 * @type: communication type (SOCK_STREAM, ...)
1351 * @protocol: protocol (0, ...)
1352 * @res: new socket
1353 * @kern: boolean for kernel space sockets
1354 *
1355 * Creates a new socket and assigns it to @res, passing through LSM.
1356 * Returns 0 or an error. On failure @res is set to %NULL. @kern must
1357 * be set to true if the socket resides in kernel space.
1358 * This function internally uses GFP_KERNEL.
1359 */
1360
Pavel Emelyanov721db932010-09-29 16:06:32 +04001361int __sock_create(struct net *net, int family, int type, int protocol,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001362 struct socket **res, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363{
1364 int err;
1365 struct socket *sock;
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001366 const struct net_proto_family *pf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
1368 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001369 * Check protocol is in range
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 */
1371 if (family < 0 || family >= NPROTO)
1372 return -EAFNOSUPPORT;
1373 if (type < 0 || type >= SOCK_MAX)
1374 return -EINVAL;
1375
1376 /* Compatibility.
1377
1378 This uglymoron is moved from INET layer to here to avoid
1379 deadlock in module load.
1380 */
1381 if (family == PF_INET && type == SOCK_PACKET) {
liping.zhangf3c98692016-03-11 23:08:36 +08001382 pr_info_once("%s uses obsolete (PF_INET,SOCK_PACKET)\n",
1383 current->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 family = PF_PACKET;
1385 }
1386
1387 err = security_socket_create(family, type, protocol, kern);
1388 if (err)
1389 return err;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001390
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001391 /*
1392 * Allocate the socket and allow the family to set things up. if
1393 * the protocol is 0, the family is instructed to select an appropriate
1394 * default.
1395 */
1396 sock = sock_alloc();
1397 if (!sock) {
Joe Perchese87cc472012-05-13 21:56:26 +00001398 net_warn_ratelimited("socket: no more sockets\n");
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001399 return -ENFILE; /* Not exactly a match, but its the
1400 closest posix thing */
1401 }
1402
1403 sock->type = type;
1404
Johannes Berg95a5afc2008-10-16 15:24:51 -07001405#ifdef CONFIG_MODULES
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001406 /* Attempt to load a protocol module if the find failed.
1407 *
1408 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 * requested real, full-featured networking support upon configuration.
1410 * Otherwise module support will break!
1411 */
Eric Dumazet190683a2010-11-10 10:50:44 +00001412 if (rcu_access_pointer(net_families[family]) == NULL)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001413 request_module("net-pf-%d", family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414#endif
1415
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001416 rcu_read_lock();
1417 pf = rcu_dereference(net_families[family]);
1418 err = -EAFNOSUPPORT;
1419 if (!pf)
1420 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
1422 /*
1423 * We will call the ->create function, that possibly is in a loadable
1424 * module, so we have to bump that loadable module refcnt first.
1425 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001426 if (!try_module_get(pf->owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 goto out_release;
1428
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001429 /* Now protected by module ref count */
1430 rcu_read_unlock();
1431
Eric Paris3f378b62009-11-05 22:18:14 -08001432 err = pf->create(net, sock, protocol, kern);
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001433 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 goto out_module_put;
Frank Filza79af592005-09-27 15:23:38 -07001435
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 /*
1437 * Now to bump the refcnt of the [loadable] module that owns this
1438 * socket at sock_release time we decrement its refcnt.
1439 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001440 if (!try_module_get(sock->ops->owner))
1441 goto out_module_busy;
1442
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 /*
1444 * Now that we're done with the ->create function, the [loadable]
1445 * module can have its refcnt decremented
1446 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001447 module_put(pf->owner);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001448 err = security_socket_post_create(sock, family, type, protocol, kern);
1449 if (err)
Herbert Xu3b185522007-08-15 14:46:02 -07001450 goto out_sock_release;
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001451 *res = sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001453 return 0;
1454
1455out_module_busy:
1456 err = -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457out_module_put:
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001458 sock->ops = NULL;
1459 module_put(pf->owner);
1460out_sock_release:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 sock_release(sock);
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001462 return err;
1463
1464out_release:
1465 rcu_read_unlock();
1466 goto out_sock_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467}
Pavel Emelyanov721db932010-09-29 16:06:32 +04001468EXPORT_SYMBOL(__sock_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
Pedro Tammela8a3c2452019-03-14 10:45:23 -03001470/**
1471 * sock_create - creates a socket
1472 * @family: protocol family (AF_INET, ...)
1473 * @type: communication type (SOCK_STREAM, ...)
1474 * @protocol: protocol (0, ...)
1475 * @res: new socket
1476 *
1477 * A wrapper around __sock_create().
1478 * Returns 0 or an error. This function internally uses GFP_KERNEL.
1479 */
1480
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481int sock_create(int family, int type, int protocol, struct socket **res)
1482{
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001483 return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001485EXPORT_SYMBOL(sock_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Pedro Tammela8a3c2452019-03-14 10:45:23 -03001487/**
1488 * sock_create_kern - creates a socket (kernel space)
1489 * @net: net namespace
1490 * @family: protocol family (AF_INET, ...)
1491 * @type: communication type (SOCK_STREAM, ...)
1492 * @protocol: protocol (0, ...)
1493 * @res: new socket
1494 *
1495 * A wrapper around __sock_create().
1496 * Returns 0 or an error. This function internally uses GFP_KERNEL.
1497 */
1498
Eric W. Biedermaneeb1bd52015-05-08 21:08:05 -05001499int sock_create_kern(struct net *net, int family, int type, int protocol, struct socket **res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500{
Eric W. Biedermaneeb1bd52015-05-08 21:08:05 -05001501 return __sock_create(net, family, type, protocol, res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001503EXPORT_SYMBOL(sock_create_kern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
Dominik Brodowski9d6a15c2018-03-13 19:29:43 +01001505int __sys_socket(int family, int type, int protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506{
1507 int retval;
1508 struct socket *sock;
Ulrich Dreppera677a032008-07-23 21:29:17 -07001509 int flags;
1510
Ulrich Dreppere38b36f2008-07-23 21:29:42 -07001511 /* Check the SOCK_* constants for consistency. */
1512 BUILD_BUG_ON(SOCK_CLOEXEC != O_CLOEXEC);
1513 BUILD_BUG_ON((SOCK_MAX | SOCK_TYPE_MASK) != SOCK_TYPE_MASK);
1514 BUILD_BUG_ON(SOCK_CLOEXEC & SOCK_TYPE_MASK);
1515 BUILD_BUG_ON(SOCK_NONBLOCK & SOCK_TYPE_MASK);
1516
Ulrich Dreppera677a032008-07-23 21:29:17 -07001517 flags = type & ~SOCK_TYPE_MASK;
Ulrich Drepper77d27202008-07-23 21:29:35 -07001518 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Dreppera677a032008-07-23 21:29:17 -07001519 return -EINVAL;
1520 type &= SOCK_TYPE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001522 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1523 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1524
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 retval = sock_create(family, type, protocol, &sock);
1526 if (retval < 0)
Al Viro8e1611e2017-12-05 23:29:09 +00001527 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Al Viro8e1611e2017-12-05 23:29:09 +00001529 return sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530}
1531
Dominik Brodowski9d6a15c2018-03-13 19:29:43 +01001532SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol)
1533{
1534 return __sys_socket(family, type, protocol);
1535}
1536
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537/*
1538 * Create a pair of connected sockets.
1539 */
1540
Dominik Brodowski6debc8d2018-03-13 19:49:23 +01001541int __sys_socketpair(int family, int type, int protocol, int __user *usockvec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542{
1543 struct socket *sock1, *sock2;
1544 int fd1, fd2, err;
Al Virodb349502007-02-07 01:48:00 -05001545 struct file *newfile1, *newfile2;
Ulrich Dreppera677a032008-07-23 21:29:17 -07001546 int flags;
1547
1548 flags = type & ~SOCK_TYPE_MASK;
Ulrich Drepper77d27202008-07-23 21:29:35 -07001549 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Dreppera677a032008-07-23 21:29:17 -07001550 return -EINVAL;
1551 type &= SOCK_TYPE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001553 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1554 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1555
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 /*
Al Viro016a2662017-12-05 23:28:38 +00001557 * reserve descriptors and make sure we won't fail
1558 * to return them to userland.
1559 */
1560 fd1 = get_unused_fd_flags(flags);
1561 if (unlikely(fd1 < 0))
1562 return fd1;
1563
1564 fd2 = get_unused_fd_flags(flags);
1565 if (unlikely(fd2 < 0)) {
1566 put_unused_fd(fd1);
1567 return fd2;
1568 }
1569
1570 err = put_user(fd1, &usockvec[0]);
1571 if (err)
1572 goto out;
1573
1574 err = put_user(fd2, &usockvec[1]);
1575 if (err)
1576 goto out;
1577
1578 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 * Obtain the first socket and check if the underlying protocol
1580 * supports the socketpair call.
1581 */
1582
1583 err = sock_create(family, type, protocol, &sock1);
Al Viro016a2662017-12-05 23:28:38 +00001584 if (unlikely(err < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 goto out;
1586
1587 err = sock_create(family, type, protocol, &sock2);
Al Viro016a2662017-12-05 23:28:38 +00001588 if (unlikely(err < 0)) {
1589 sock_release(sock1);
1590 goto out;
David S. Millerbf3c23d2007-10-29 21:54:02 -07001591 }
Yann Droneaudd73aa282013-12-09 22:42:20 +01001592
David Herrmannd47cd942018-05-04 16:28:20 +02001593 err = security_socket_socketpair(sock1, sock2);
1594 if (unlikely(err)) {
1595 sock_release(sock2);
1596 sock_release(sock1);
1597 goto out;
1598 }
1599
Al Viro016a2662017-12-05 23:28:38 +00001600 err = sock1->ops->socketpair(sock1, sock2);
1601 if (unlikely(err < 0)) {
1602 sock_release(sock2);
1603 sock_release(sock1);
1604 goto out;
Al Viro28407632012-08-17 23:54:15 -04001605 }
1606
Linus Torvaldsaab174f2012-10-02 20:25:04 -07001607 newfile1 = sock_alloc_file(sock1, flags, NULL);
Viresh Kumarb5ffe632015-08-12 15:59:47 +05301608 if (IS_ERR(newfile1)) {
Al Viro28407632012-08-17 23:54:15 -04001609 err = PTR_ERR(newfile1);
Al Viro016a2662017-12-05 23:28:38 +00001610 sock_release(sock2);
1611 goto out;
Al Viro28407632012-08-17 23:54:15 -04001612 }
1613
Linus Torvaldsaab174f2012-10-02 20:25:04 -07001614 newfile2 = sock_alloc_file(sock2, flags, NULL);
Al Viro28407632012-08-17 23:54:15 -04001615 if (IS_ERR(newfile2)) {
1616 err = PTR_ERR(newfile2);
Al Viro016a2662017-12-05 23:28:38 +00001617 fput(newfile1);
1618 goto out;
Al Virodb349502007-02-07 01:48:00 -05001619 }
1620
Al Viro157cf642008-12-14 04:57:47 -05001621 audit_fd_pair(fd1, fd2);
Yann Droneaudd73aa282013-12-09 22:42:20 +01001622
Al Virodb349502007-02-07 01:48:00 -05001623 fd_install(fd1, newfile1);
1624 fd_install(fd2, newfile2);
Yann Droneaudd73aa282013-12-09 22:42:20 +01001625 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627out:
Al Viro016a2662017-12-05 23:28:38 +00001628 put_unused_fd(fd2);
1629 put_unused_fd(fd1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 return err;
1631}
1632
Dominik Brodowski6debc8d2018-03-13 19:49:23 +01001633SYSCALL_DEFINE4(socketpair, int, family, int, type, int, protocol,
1634 int __user *, usockvec)
1635{
1636 return __sys_socketpair(family, type, protocol, usockvec);
1637}
1638
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639/*
1640 * Bind a name to a socket. Nothing much to do here since it's
1641 * the protocol's responsibility to handle the local address.
1642 *
1643 * We move the socket address to kernel space before we call
1644 * the protocol layer (having also checked the address is ok).
1645 */
1646
Dominik Brodowskia87d35d2018-03-13 19:33:09 +01001647int __sys_bind(int fd, struct sockaddr __user *umyaddr, int addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648{
1649 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001650 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001651 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001653 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001654 if (sock) {
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001655 err = move_addr_to_kernel(umyaddr, addrlen, &address);
Jakub Sitnicki068b88c2018-10-04 11:09:40 +02001656 if (!err) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001657 err = security_socket_bind(sock,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001658 (struct sockaddr *)&address,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001659 addrlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001660 if (!err)
1661 err = sock->ops->bind(sock,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001662 (struct sockaddr *)
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001663 &address, addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 }
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001665 fput_light(sock->file, fput_needed);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 return err;
1668}
1669
Dominik Brodowskia87d35d2018-03-13 19:33:09 +01001670SYSCALL_DEFINE3(bind, int, fd, struct sockaddr __user *, umyaddr, int, addrlen)
1671{
1672 return __sys_bind(fd, umyaddr, addrlen);
1673}
1674
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675/*
1676 * Perform a listen. Basically, we allow the protocol to do anything
1677 * necessary for a listen, and if that works, we mark the socket as
1678 * ready for listening.
1679 */
1680
Dominik Brodowski25e290e2018-03-13 19:36:54 +01001681int __sys_listen(int fd, int backlog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682{
1683 struct socket *sock;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001684 int err, fput_needed;
Pavel Emelyanovb8e1f9b2007-12-08 00:12:33 -08001685 int somaxconn;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001686
1687 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1688 if (sock) {
Pavel Emelyanov8efa6e92008-03-31 19:41:14 -07001689 somaxconn = sock_net(sock->sk)->core.sysctl_somaxconn;
Eric Dumazet95c96172012-04-15 05:58:06 +00001690 if ((unsigned int)backlog > somaxconn)
Pavel Emelyanovb8e1f9b2007-12-08 00:12:33 -08001691 backlog = somaxconn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
1693 err = security_socket_listen(sock, backlog);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001694 if (!err)
1695 err = sock->ops->listen(sock, backlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001697 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 }
1699 return err;
1700}
1701
Dominik Brodowski25e290e2018-03-13 19:36:54 +01001702SYSCALL_DEFINE2(listen, int, fd, int, backlog)
1703{
1704 return __sys_listen(fd, backlog);
1705}
1706
Jens Axboede2ea4b2019-10-17 14:41:29 -06001707int __sys_accept4_file(struct file *file, unsigned file_flags,
1708 struct sockaddr __user *upeer_sockaddr,
1709 int __user *upeer_addrlen, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710{
1711 struct socket *sock, *newsock;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001712 struct file *newfile;
Jens Axboede2ea4b2019-10-17 14:41:29 -06001713 int err, len, newfd;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001714 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
Ulrich Drepper77d27202008-07-23 21:29:35 -07001716 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001717 return -EINVAL;
1718
1719 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1720 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1721
Jens Axboede2ea4b2019-10-17 14:41:29 -06001722 sock = sock_from_file(file, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 if (!sock)
1724 goto out;
1725
1726 err = -ENFILE;
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001727 newsock = sock_alloc();
1728 if (!newsock)
Jens Axboede2ea4b2019-10-17 14:41:29 -06001729 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
1731 newsock->type = sock->type;
1732 newsock->ops = sock->ops;
1733
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 /*
1735 * We don't need try_module_get here, as the listening socket (sock)
1736 * has the protocol module (sock->ops->owner) held.
1737 */
1738 __module_get(newsock->ops->owner);
1739
Al Viro28407632012-08-17 23:54:15 -04001740 newfd = get_unused_fd_flags(flags);
David S. Miller39d8c1b2006-03-20 17:13:49 -08001741 if (unlikely(newfd < 0)) {
1742 err = newfd;
David S. Miller9a1875e2006-04-01 12:48:36 -08001743 sock_release(newsock);
Jens Axboede2ea4b2019-10-17 14:41:29 -06001744 goto out;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001745 }
Linus Torvaldsaab174f2012-10-02 20:25:04 -07001746 newfile = sock_alloc_file(newsock, flags, sock->sk->sk_prot_creator->name);
Viresh Kumarb5ffe632015-08-12 15:59:47 +05301747 if (IS_ERR(newfile)) {
Al Viro28407632012-08-17 23:54:15 -04001748 err = PTR_ERR(newfile);
1749 put_unused_fd(newfd);
Jens Axboede2ea4b2019-10-17 14:41:29 -06001750 goto out;
Al Viro28407632012-08-17 23:54:15 -04001751 }
David S. Miller39d8c1b2006-03-20 17:13:49 -08001752
Frank Filza79af592005-09-27 15:23:38 -07001753 err = security_socket_accept(sock, newsock);
1754 if (err)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001755 goto out_fd;
Frank Filza79af592005-09-27 15:23:38 -07001756
Jens Axboede2ea4b2019-10-17 14:41:29 -06001757 err = sock->ops->accept(sock, newsock, sock->file->f_flags | file_flags,
1758 false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 if (err < 0)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001760 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
1762 if (upeer_sockaddr) {
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001763 len = newsock->ops->getname(newsock,
1764 (struct sockaddr *)&address, 2);
1765 if (len < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 err = -ECONNABORTED;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001767 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 }
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001769 err = move_addr_to_user(&address,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001770 len, upeer_sockaddr, upeer_addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 if (err < 0)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001772 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 }
1774
1775 /* File flags are not inherited via accept() unlike another OSes. */
1776
David S. Miller39d8c1b2006-03-20 17:13:49 -08001777 fd_install(newfd, newfile);
1778 err = newfd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779out:
1780 return err;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001781out_fd:
David S. Miller9606a212006-04-01 01:00:14 -08001782 fput(newfile);
David S. Miller39d8c1b2006-03-20 17:13:49 -08001783 put_unused_fd(newfd);
Jens Axboede2ea4b2019-10-17 14:41:29 -06001784 goto out;
1785
1786}
1787
1788/*
1789 * For accept, we attempt to create a new socket, set up the link
1790 * with the client, wake up the client, then return the new
1791 * connected fd. We collect the address of the connector in kernel
1792 * space and move it to user at the very end. This is unclean because
1793 * we open the socket then return an error.
1794 *
1795 * 1003.1g adds the ability to recvmsg() to query connection pending
1796 * status to recvmsg. We need to add that support in a way thats
1797 * clean when we restructure accept also.
1798 */
1799
1800int __sys_accept4(int fd, struct sockaddr __user *upeer_sockaddr,
1801 int __user *upeer_addrlen, int flags)
1802{
1803 int ret = -EBADF;
1804 struct fd f;
1805
1806 f = fdget(fd);
1807 if (f.file) {
1808 ret = __sys_accept4_file(f.file, 0, upeer_sockaddr,
1809 upeer_addrlen, flags);
1810 if (f.flags)
1811 fput(f.file);
1812 }
1813
1814 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815}
1816
Dominik Brodowski4541e802018-03-13 19:24:23 +01001817SYSCALL_DEFINE4(accept4, int, fd, struct sockaddr __user *, upeer_sockaddr,
1818 int __user *, upeer_addrlen, int, flags)
1819{
1820 return __sys_accept4(fd, upeer_sockaddr, upeer_addrlen, flags);
1821}
1822
Heiko Carstens20f37032009-01-14 14:14:23 +01001823SYSCALL_DEFINE3(accept, int, fd, struct sockaddr __user *, upeer_sockaddr,
1824 int __user *, upeer_addrlen)
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001825{
Dominik Brodowski4541e802018-03-13 19:24:23 +01001826 return __sys_accept4(fd, upeer_sockaddr, upeer_addrlen, 0);
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001827}
1828
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829/*
1830 * Attempt to connect to a socket with the server address. The address
1831 * is in user space so we verify it is OK and move it to kernel space.
1832 *
1833 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
1834 * break bindings
1835 *
1836 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
1837 * other SEQPACKET protocols that take time to connect() as it doesn't
1838 * include the -EINPROGRESS status for such sockets.
1839 */
1840
Jens Axboef499a022019-12-02 16:28:46 -07001841int __sys_connect_file(struct file *file, struct sockaddr_storage *address,
Jens Axboebd3ded32019-11-23 14:17:16 -07001842 int addrlen, int file_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843{
1844 struct socket *sock;
Jens Axboebd3ded32019-11-23 14:17:16 -07001845 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
Jens Axboebd3ded32019-11-23 14:17:16 -07001847 sock = sock_from_file(file, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 if (!sock)
1849 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001851 err =
Jens Axboef499a022019-12-02 16:28:46 -07001852 security_socket_connect(sock, (struct sockaddr *)address, addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 if (err)
Jens Axboebd3ded32019-11-23 14:17:16 -07001854 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
Jens Axboef499a022019-12-02 16:28:46 -07001856 err = sock->ops->connect(sock, (struct sockaddr *)address, addrlen,
Jens Axboebd3ded32019-11-23 14:17:16 -07001857 sock->file->f_flags | file_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858out:
1859 return err;
1860}
1861
Jens Axboebd3ded32019-11-23 14:17:16 -07001862int __sys_connect(int fd, struct sockaddr __user *uservaddr, int addrlen)
1863{
1864 int ret = -EBADF;
1865 struct fd f;
1866
1867 f = fdget(fd);
1868 if (f.file) {
Jens Axboef499a022019-12-02 16:28:46 -07001869 struct sockaddr_storage address;
1870
1871 ret = move_addr_to_kernel(uservaddr, addrlen, &address);
1872 if (!ret)
1873 ret = __sys_connect_file(f.file, &address, addrlen, 0);
Jens Axboebd3ded32019-11-23 14:17:16 -07001874 if (f.flags)
1875 fput(f.file);
1876 }
1877
1878 return ret;
1879}
1880
Dominik Brodowski1387c2c2018-03-13 19:35:09 +01001881SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
1882 int, addrlen)
1883{
1884 return __sys_connect(fd, uservaddr, addrlen);
1885}
1886
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887/*
1888 * Get the local address ('name') of a socket object. Move the obtained
1889 * name to user space.
1890 */
1891
Dominik Brodowski8882a102018-03-13 19:43:14 +01001892int __sys_getsockname(int fd, struct sockaddr __user *usockaddr,
1893 int __user *usockaddr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894{
1895 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001896 struct sockaddr_storage address;
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001897 int err, fput_needed;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001898
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001899 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 if (!sock)
1901 goto out;
1902
1903 err = security_socket_getsockname(sock);
1904 if (err)
1905 goto out_put;
1906
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001907 err = sock->ops->getname(sock, (struct sockaddr *)&address, 0);
1908 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 goto out_put;
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001910 /* "err" is actually length in this case */
1911 err = move_addr_to_user(&address, err, usockaddr, usockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
1913out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001914 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915out:
1916 return err;
1917}
1918
Dominik Brodowski8882a102018-03-13 19:43:14 +01001919SYSCALL_DEFINE3(getsockname, int, fd, struct sockaddr __user *, usockaddr,
1920 int __user *, usockaddr_len)
1921{
1922 return __sys_getsockname(fd, usockaddr, usockaddr_len);
1923}
1924
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925/*
1926 * Get the remote address ('name') of a socket object. Move the obtained
1927 * name to user space.
1928 */
1929
Dominik Brodowskib21c8f82018-03-13 19:47:00 +01001930int __sys_getpeername(int fd, struct sockaddr __user *usockaddr,
1931 int __user *usockaddr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932{
1933 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001934 struct sockaddr_storage address;
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001935 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001937 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1938 if (sock != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 err = security_socket_getpeername(sock);
1940 if (err) {
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001941 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 return err;
1943 }
1944
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001945 err = sock->ops->getname(sock, (struct sockaddr *)&address, 1);
1946 if (err >= 0)
1947 /* "err" is actually length in this case */
1948 err = move_addr_to_user(&address, err, usockaddr,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001949 usockaddr_len);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001950 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 }
1952 return err;
1953}
1954
Dominik Brodowskib21c8f82018-03-13 19:47:00 +01001955SYSCALL_DEFINE3(getpeername, int, fd, struct sockaddr __user *, usockaddr,
1956 int __user *, usockaddr_len)
1957{
1958 return __sys_getpeername(fd, usockaddr, usockaddr_len);
1959}
1960
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961/*
1962 * Send a datagram to a given address. We move the address into kernel
1963 * space and check the user space data area is readable before invoking
1964 * the protocol.
1965 */
Dominik Brodowski211b6342018-03-13 19:18:52 +01001966int __sys_sendto(int fd, void __user *buff, size_t len, unsigned int flags,
1967 struct sockaddr __user *addr, int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968{
1969 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001970 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 int err;
1972 struct msghdr msg;
1973 struct iovec iov;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001974 int fput_needed;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001975
Al Viro602bd0e2015-03-21 19:12:32 -04001976 err = import_single_range(WRITE, buff, len, &iov, &msg.msg_iter);
1977 if (unlikely(err))
1978 return err;
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001979 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1980 if (!sock)
David S. Miller4387ff72007-02-08 15:06:08 -08001981 goto out;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001982
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001983 msg.msg_name = NULL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001984 msg.msg_control = NULL;
1985 msg.msg_controllen = 0;
1986 msg.msg_namelen = 0;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001987 if (addr) {
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001988 err = move_addr_to_kernel(addr, addr_len, &address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 if (err < 0)
1990 goto out_put;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001991 msg.msg_name = (struct sockaddr *)&address;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001992 msg.msg_namelen = addr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 }
1994 if (sock->file->f_flags & O_NONBLOCK)
1995 flags |= MSG_DONTWAIT;
1996 msg.msg_flags = flags;
Al Virod8725c82014-12-11 00:02:50 -05001997 err = sock_sendmsg(sock, &msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001999out_put:
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08002000 fput_light(sock->file, fput_needed);
David S. Miller4387ff72007-02-08 15:06:08 -08002001out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 return err;
2003}
2004
Dominik Brodowski211b6342018-03-13 19:18:52 +01002005SYSCALL_DEFINE6(sendto, int, fd, void __user *, buff, size_t, len,
2006 unsigned int, flags, struct sockaddr __user *, addr,
2007 int, addr_len)
2008{
2009 return __sys_sendto(fd, buff, len, flags, addr, addr_len);
2010}
2011
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002013 * Send a datagram down a socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 */
2015
Heiko Carstens3e0fa652009-01-14 14:14:24 +01002016SYSCALL_DEFINE4(send, int, fd, void __user *, buff, size_t, len,
Eric Dumazet95c96172012-04-15 05:58:06 +00002017 unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018{
Dominik Brodowski211b6342018-03-13 19:18:52 +01002019 return __sys_sendto(fd, buff, len, flags, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020}
2021
2022/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002023 * Receive a frame from the socket and optionally record the address of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 * sender. We verify the buffers are writable and if needed move the
2025 * sender address from kernel to user space.
2026 */
Dominik Brodowski7a09e1e2018-03-13 19:10:06 +01002027int __sys_recvfrom(int fd, void __user *ubuf, size_t size, unsigned int flags,
2028 struct sockaddr __user *addr, int __user *addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029{
2030 struct socket *sock;
2031 struct iovec iov;
2032 struct msghdr msg;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07002033 struct sockaddr_storage address;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002034 int err, err2;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002035 int fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
Al Viro602bd0e2015-03-21 19:12:32 -04002037 err = import_single_range(READ, ubuf, size, &iov, &msg.msg_iter);
2038 if (unlikely(err))
2039 return err;
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08002040 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 if (!sock)
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08002042 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002044 msg.msg_control = NULL;
2045 msg.msg_controllen = 0;
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01002046 /* Save some cycles and don't copy the address if not needed */
2047 msg.msg_name = addr ? (struct sockaddr *)&address : NULL;
2048 /* We assume all kernel code knows the size of sockaddr_storage */
2049 msg.msg_namelen = 0;
tadeusz.struk@intel.com130ed5d2015-12-15 10:46:17 -08002050 msg.msg_iocb = NULL;
Alexander Potapenko9f138fa2017-03-08 18:08:16 +01002051 msg.msg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 if (sock->file->f_flags & O_NONBLOCK)
2053 flags |= MSG_DONTWAIT;
Al Viro2da62902015-03-14 21:13:46 -04002054 err = sock_recvmsg(sock, &msg, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002056 if (err >= 0 && addr != NULL) {
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00002057 err2 = move_addr_to_user(&address,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07002058 msg.msg_namelen, addr, addr_len);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002059 if (err2 < 0)
2060 err = err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 }
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08002062
2063 fput_light(sock->file, fput_needed);
David S. Miller4387ff72007-02-08 15:06:08 -08002064out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 return err;
2066}
2067
Dominik Brodowski7a09e1e2018-03-13 19:10:06 +01002068SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, ubuf, size_t, size,
2069 unsigned int, flags, struct sockaddr __user *, addr,
2070 int __user *, addr_len)
2071{
2072 return __sys_recvfrom(fd, ubuf, size, flags, addr, addr_len);
2073}
2074
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002076 * Receive a datagram from a socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 */
2078
Jan Glauberb7c0ddf2014-04-16 09:32:48 +02002079SYSCALL_DEFINE4(recv, int, fd, void __user *, ubuf, size_t, size,
2080 unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081{
Dominik Brodowski7a09e1e2018-03-13 19:10:06 +01002082 return __sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083}
2084
2085/*
2086 * Set a socket option. Because we don't know the option lengths we have
2087 * to pass the user mode parameter for the protocols to sort out.
2088 */
2089
Dominik Brodowskicc36dca2018-03-13 20:10:59 +01002090static int __sys_setsockopt(int fd, int level, int optname,
2091 char __user *optval, int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092{
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07002093 mm_segment_t oldfs = get_fs();
2094 char *kernel_optval = NULL;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002095 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 struct socket *sock;
2097
2098 if (optlen < 0)
2099 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002100
2101 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2102 if (sock != NULL) {
2103 err = security_socket_setsockopt(sock, level, optname);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002104 if (err)
2105 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07002107 err = BPF_CGROUP_RUN_PROG_SETSOCKOPT(sock->sk, &level,
2108 &optname, optval, &optlen,
2109 &kernel_optval);
2110
2111 if (err < 0) {
2112 goto out_put;
2113 } else if (err > 0) {
2114 err = 0;
2115 goto out_put;
2116 }
2117
2118 if (kernel_optval) {
2119 set_fs(KERNEL_DS);
2120 optval = (char __user __force *)kernel_optval;
2121 }
2122
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 if (level == SOL_SOCKET)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002124 err =
2125 sock_setsockopt(sock, level, optname, optval,
2126 optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 else
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002128 err =
2129 sock->ops->setsockopt(sock, level, optname, optval,
2130 optlen);
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07002131
2132 if (kernel_optval) {
2133 set_fs(oldfs);
2134 kfree(kernel_optval);
2135 }
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002136out_put:
2137 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 }
2139 return err;
2140}
2141
Dominik Brodowskicc36dca2018-03-13 20:10:59 +01002142SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname,
2143 char __user *, optval, int, optlen)
2144{
2145 return __sys_setsockopt(fd, level, optname, optval, optlen);
2146}
2147
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148/*
2149 * Get a socket option. Because we don't know the option lengths we have
2150 * to pass a user mode parameter for the protocols to sort out.
2151 */
2152
Dominik Brodowski13a2d702018-03-13 20:15:04 +01002153static int __sys_getsockopt(int fd, int level, int optname,
2154 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002156 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 struct socket *sock;
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07002158 int max_optlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002160 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2161 if (sock != NULL) {
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002162 err = security_socket_getsockopt(sock, level, optname);
2163 if (err)
2164 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07002166 max_optlen = BPF_CGROUP_GETSOCKOPT_MAX_OPTLEN(optlen);
2167
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 if (level == SOL_SOCKET)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002169 err =
2170 sock_getsockopt(sock, level, optname, optval,
2171 optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 else
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002173 err =
2174 sock->ops->getsockopt(sock, level, optname, optval,
2175 optlen);
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07002176
2177 err = BPF_CGROUP_RUN_PROG_GETSOCKOPT(sock->sk, level, optname,
2178 optval, optlen,
2179 max_optlen, err);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002180out_put:
2181 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 }
2183 return err;
2184}
2185
Dominik Brodowski13a2d702018-03-13 20:15:04 +01002186SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname,
2187 char __user *, optval, int __user *, optlen)
2188{
2189 return __sys_getsockopt(fd, level, optname, optval, optlen);
2190}
2191
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192/*
2193 * Shutdown a socket.
2194 */
2195
Dominik Brodowski005a1ae2018-03-13 20:07:05 +01002196int __sys_shutdown(int fd, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002198 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 struct socket *sock;
2200
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002201 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2202 if (sock != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 err = security_socket_shutdown(sock, how);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002204 if (!err)
2205 err = sock->ops->shutdown(sock, how);
2206 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 }
2208 return err;
2209}
2210
Dominik Brodowski005a1ae2018-03-13 20:07:05 +01002211SYSCALL_DEFINE2(shutdown, int, fd, int, how)
2212{
2213 return __sys_shutdown(fd, how);
2214}
2215
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002216/* A couple of helpful macros for getting the address of the 32/64 bit
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 * fields which are the same type (int / unsigned) on our platforms.
2218 */
2219#define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
2220#define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
2221#define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
2222
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002223struct used_address {
2224 struct sockaddr_storage name;
2225 unsigned int name_len;
2226};
2227
Al Viroda184282015-03-21 19:29:06 -04002228static int copy_msghdr_from_user(struct msghdr *kmsg,
2229 struct user_msghdr __user *umsg,
2230 struct sockaddr __user **save_addr,
2231 struct iovec **iov)
Dan Carpenter1661bf32013-10-03 00:27:20 +03002232{
Al Viroffb07552017-06-27 19:32:04 -04002233 struct user_msghdr msg;
Al Viro08adb7d2014-11-10 20:23:13 -05002234 ssize_t err;
2235
Al Viroffb07552017-06-27 19:32:04 -04002236 if (copy_from_user(&msg, umsg, sizeof(*umsg)))
Dan Carpenter1661bf32013-10-03 00:27:20 +03002237 return -EFAULT;
Matthew Leachdbb490b2014-03-11 11:58:27 +00002238
Paolo Abeni864d9662017-07-21 18:49:45 +02002239 kmsg->msg_control = (void __force *)msg.msg_control;
Al Viroffb07552017-06-27 19:32:04 -04002240 kmsg->msg_controllen = msg.msg_controllen;
2241 kmsg->msg_flags = msg.msg_flags;
2242
2243 kmsg->msg_namelen = msg.msg_namelen;
2244 if (!msg.msg_name)
Ani Sinha6a2a2b32014-09-08 14:49:59 -07002245 kmsg->msg_namelen = 0;
2246
Matthew Leachdbb490b2014-03-11 11:58:27 +00002247 if (kmsg->msg_namelen < 0)
2248 return -EINVAL;
2249
Dan Carpenter1661bf32013-10-03 00:27:20 +03002250 if (kmsg->msg_namelen > sizeof(struct sockaddr_storage))
Dan Carpenterdb31c552013-11-27 15:40:21 +03002251 kmsg->msg_namelen = sizeof(struct sockaddr_storage);
Al Viro08adb7d2014-11-10 20:23:13 -05002252
2253 if (save_addr)
Al Viroffb07552017-06-27 19:32:04 -04002254 *save_addr = msg.msg_name;
Al Viro08adb7d2014-11-10 20:23:13 -05002255
Al Viroffb07552017-06-27 19:32:04 -04002256 if (msg.msg_name && kmsg->msg_namelen) {
Al Viro08adb7d2014-11-10 20:23:13 -05002257 if (!save_addr) {
Paolo Abeni864d9662017-07-21 18:49:45 +02002258 err = move_addr_to_kernel(msg.msg_name,
2259 kmsg->msg_namelen,
Al Viro08adb7d2014-11-10 20:23:13 -05002260 kmsg->msg_name);
2261 if (err < 0)
2262 return err;
2263 }
2264 } else {
2265 kmsg->msg_name = NULL;
2266 kmsg->msg_namelen = 0;
2267 }
2268
Al Viroffb07552017-06-27 19:32:04 -04002269 if (msg.msg_iovlen > UIO_MAXIOV)
Al Viro08adb7d2014-11-10 20:23:13 -05002270 return -EMSGSIZE;
2271
tadeusz.struk@intel.com0345f932015-03-19 12:31:25 -07002272 kmsg->msg_iocb = NULL;
2273
Jens Axboe87e5e6d2019-05-14 16:02:22 -06002274 err = import_iovec(save_addr ? READ : WRITE,
Al Viroffb07552017-06-27 19:32:04 -04002275 msg.msg_iov, msg.msg_iovlen,
Al Viroda184282015-03-21 19:29:06 -04002276 UIO_FASTIOV, iov, &kmsg->msg_iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06002277 return err < 0 ? err : 0;
Dan Carpenter1661bf32013-10-03 00:27:20 +03002278}
2279
Jens Axboe4257c8c2019-11-25 14:27:34 -07002280static int ____sys_sendmsg(struct socket *sock, struct msghdr *msg_sys,
2281 unsigned int flags, struct used_address *used_address,
2282 unsigned int allowed_msghdr_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283{
Alex Williamsonb9d717a2005-09-26 14:28:02 -07002284 unsigned char ctl[sizeof(struct cmsghdr) + 20]
Amit Kushwaha846cc122016-12-08 18:21:53 +05302285 __aligned(sizeof(__kernel_size_t));
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002286 /* 20 is size of ipv6_pktinfo */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 unsigned char *ctl_buf = ctl;
Al Virod8725c82014-12-11 00:02:50 -05002288 int ctl_len;
Al Viro08adb7d2014-11-10 20:23:13 -05002289 ssize_t err;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002290
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 err = -ENOBUFS;
2292
Anton Blanchard228e5482011-05-02 20:21:35 +00002293 if (msg_sys->msg_controllen > INT_MAX)
Jens Axboe4257c8c2019-11-25 14:27:34 -07002294 goto out;
Tom Herbert28a94d82016-03-07 14:11:02 -08002295 flags |= (msg_sys->msg_flags & allowed_msghdr_flags);
Anton Blanchard228e5482011-05-02 20:21:35 +00002296 ctl_len = msg_sys->msg_controllen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002298 err =
Anton Blanchard228e5482011-05-02 20:21:35 +00002299 cmsghdr_from_user_compat_to_kern(msg_sys, sock->sk, ctl,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002300 sizeof(ctl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 if (err)
Jens Axboe4257c8c2019-11-25 14:27:34 -07002302 goto out;
Anton Blanchard228e5482011-05-02 20:21:35 +00002303 ctl_buf = msg_sys->msg_control;
2304 ctl_len = msg_sys->msg_controllen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 } else if (ctl_len) {
David S. Millerac4340f2017-01-04 13:24:19 -05002306 BUILD_BUG_ON(sizeof(struct cmsghdr) !=
2307 CMSG_ALIGN(sizeof(struct cmsghdr)));
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002308 if (ctl_len > sizeof(ctl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002310 if (ctl_buf == NULL)
Jens Axboe4257c8c2019-11-25 14:27:34 -07002311 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 }
2313 err = -EFAULT;
2314 /*
Anton Blanchard228e5482011-05-02 20:21:35 +00002315 * Careful! Before this, msg_sys->msg_control contains a user pointer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
2317 * checking falls down on this.
2318 */
Namhyung Kimfb8621b2010-09-07 03:55:00 +00002319 if (copy_from_user(ctl_buf,
Anton Blanchard228e5482011-05-02 20:21:35 +00002320 (void __user __force *)msg_sys->msg_control,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002321 ctl_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 goto out_freectl;
Anton Blanchard228e5482011-05-02 20:21:35 +00002323 msg_sys->msg_control = ctl_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 }
Anton Blanchard228e5482011-05-02 20:21:35 +00002325 msg_sys->msg_flags = flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
2327 if (sock->file->f_flags & O_NONBLOCK)
Anton Blanchard228e5482011-05-02 20:21:35 +00002328 msg_sys->msg_flags |= MSG_DONTWAIT;
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002329 /*
2330 * If this is sendmmsg() and current destination address is same as
2331 * previously succeeded address, omit asking LSM's decision.
2332 * used_address->name_len is initialized to UINT_MAX so that the first
2333 * destination address never matches.
2334 */
Mathieu Desnoyersbc909d92011-08-24 19:45:03 -07002335 if (used_address && msg_sys->msg_name &&
2336 used_address->name_len == msg_sys->msg_namelen &&
2337 !memcmp(&used_address->name, msg_sys->msg_name,
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002338 used_address->name_len)) {
Al Virod8725c82014-12-11 00:02:50 -05002339 err = sock_sendmsg_nosec(sock, msg_sys);
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002340 goto out_freectl;
2341 }
Al Virod8725c82014-12-11 00:02:50 -05002342 err = sock_sendmsg(sock, msg_sys);
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002343 /*
2344 * If this is sendmmsg() and sending to current destination address was
2345 * successful, remember it.
2346 */
2347 if (used_address && err >= 0) {
2348 used_address->name_len = msg_sys->msg_namelen;
Mathieu Desnoyersbc909d92011-08-24 19:45:03 -07002349 if (msg_sys->msg_name)
2350 memcpy(&used_address->name, msg_sys->msg_name,
2351 used_address->name_len);
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353
2354out_freectl:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002355 if (ctl_buf != ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 sock_kfree_s(sock->sk, ctl_buf, ctl_len);
Jens Axboe4257c8c2019-11-25 14:27:34 -07002357out:
2358 return err;
2359}
2360
Jens Axboe03b12302019-12-02 18:50:25 -07002361int sendmsg_copy_msghdr(struct msghdr *msg,
2362 struct user_msghdr __user *umsg, unsigned flags,
2363 struct iovec **iov)
Jens Axboe4257c8c2019-11-25 14:27:34 -07002364{
2365 int err;
2366
2367 if (flags & MSG_CMSG_COMPAT) {
2368 struct compat_msghdr __user *msg_compat;
2369
2370 msg_compat = (struct compat_msghdr __user *) umsg;
2371 err = get_compat_msghdr(msg, msg_compat, NULL, iov);
2372 } else {
2373 err = copy_msghdr_from_user(msg, umsg, NULL, iov);
2374 }
2375 if (err < 0)
2376 return err;
2377
2378 return 0;
2379}
2380
2381static int ___sys_sendmsg(struct socket *sock, struct user_msghdr __user *msg,
2382 struct msghdr *msg_sys, unsigned int flags,
2383 struct used_address *used_address,
2384 unsigned int allowed_msghdr_flags)
2385{
2386 struct sockaddr_storage address;
2387 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
2388 ssize_t err;
2389
2390 msg_sys->msg_name = &address;
2391
2392 err = sendmsg_copy_msghdr(msg_sys, msg, flags, &iov);
2393 if (err < 0)
2394 return err;
2395
2396 err = ____sys_sendmsg(sock, msg_sys, flags, used_address,
2397 allowed_msghdr_flags);
Al Viroda184282015-03-21 19:29:06 -04002398 kfree(iov);
Anton Blanchard228e5482011-05-02 20:21:35 +00002399 return err;
2400}
2401
2402/*
2403 * BSD sendmsg interface
2404 */
Jens Axboe03b12302019-12-02 18:50:25 -07002405long __sys_sendmsg_sock(struct socket *sock, struct msghdr *msg,
Jens Axboe0fa03c62019-04-19 13:34:07 -06002406 unsigned int flags)
2407{
Jens Axboed69e0772019-11-25 17:04:13 -07002408 /* disallow ancillary data requests from this path */
Jens Axboe03b12302019-12-02 18:50:25 -07002409 if (msg->msg_control || msg->msg_controllen)
2410 return -EINVAL;
Jens Axboed69e0772019-11-25 17:04:13 -07002411
Jens Axboe03b12302019-12-02 18:50:25 -07002412 return ____sys_sendmsg(sock, msg, flags, NULL, 0);
Jens Axboe0fa03c62019-04-19 13:34:07 -06002413}
Anton Blanchard228e5482011-05-02 20:21:35 +00002414
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002415long __sys_sendmsg(int fd, struct user_msghdr __user *msg, unsigned int flags,
2416 bool forbid_cmsg_compat)
Anton Blanchard228e5482011-05-02 20:21:35 +00002417{
2418 int fput_needed, err;
2419 struct msghdr msg_sys;
Andy Lutomirski1be374a2013-05-22 14:07:44 -07002420 struct socket *sock;
Anton Blanchard228e5482011-05-02 20:21:35 +00002421
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002422 if (forbid_cmsg_compat && (flags & MSG_CMSG_COMPAT))
2423 return -EINVAL;
2424
Andy Lutomirski1be374a2013-05-22 14:07:44 -07002425 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Anton Blanchard228e5482011-05-02 20:21:35 +00002426 if (!sock)
2427 goto out;
2428
Tom Herbert28a94d82016-03-07 14:11:02 -08002429 err = ___sys_sendmsg(sock, msg, &msg_sys, flags, NULL, 0);
Anton Blanchard228e5482011-05-02 20:21:35 +00002430
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002431 fput_light(sock->file, fput_needed);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002432out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 return err;
2434}
2435
Al Viro666547f2014-04-06 14:03:05 -04002436SYSCALL_DEFINE3(sendmsg, int, fd, struct user_msghdr __user *, msg, unsigned int, flags)
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002437{
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002438 return __sys_sendmsg(fd, msg, flags, true);
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002439}
2440
Anton Blanchard228e5482011-05-02 20:21:35 +00002441/*
2442 * Linux sendmmsg interface
2443 */
2444
2445int __sys_sendmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002446 unsigned int flags, bool forbid_cmsg_compat)
Anton Blanchard228e5482011-05-02 20:21:35 +00002447{
2448 int fput_needed, err, datagrams;
2449 struct socket *sock;
2450 struct mmsghdr __user *entry;
2451 struct compat_mmsghdr __user *compat_entry;
2452 struct msghdr msg_sys;
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002453 struct used_address used_address;
Tom Herbertf0922762016-03-07 14:11:03 -08002454 unsigned int oflags = flags;
Anton Blanchard228e5482011-05-02 20:21:35 +00002455
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002456 if (forbid_cmsg_compat && (flags & MSG_CMSG_COMPAT))
2457 return -EINVAL;
2458
Anton Blanchard98382f42011-08-04 14:07:39 +00002459 if (vlen > UIO_MAXIOV)
2460 vlen = UIO_MAXIOV;
Anton Blanchard228e5482011-05-02 20:21:35 +00002461
2462 datagrams = 0;
2463
2464 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2465 if (!sock)
2466 return err;
2467
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002468 used_address.name_len = UINT_MAX;
Anton Blanchard228e5482011-05-02 20:21:35 +00002469 entry = mmsg;
2470 compat_entry = (struct compat_mmsghdr __user *)mmsg;
Anton Blanchard728ffb82011-08-04 14:07:38 +00002471 err = 0;
Tom Herbertf0922762016-03-07 14:11:03 -08002472 flags |= MSG_BATCH;
Anton Blanchard228e5482011-05-02 20:21:35 +00002473
2474 while (datagrams < vlen) {
Tom Herbertf0922762016-03-07 14:11:03 -08002475 if (datagrams == vlen - 1)
2476 flags = oflags;
2477
Anton Blanchard228e5482011-05-02 20:21:35 +00002478 if (MSG_CMSG_COMPAT & flags) {
Al Viro666547f2014-04-06 14:03:05 -04002479 err = ___sys_sendmsg(sock, (struct user_msghdr __user *)compat_entry,
Tom Herbert28a94d82016-03-07 14:11:02 -08002480 &msg_sys, flags, &used_address, MSG_EOR);
Anton Blanchard228e5482011-05-02 20:21:35 +00002481 if (err < 0)
2482 break;
2483 err = __put_user(err, &compat_entry->msg_len);
2484 ++compat_entry;
2485 } else {
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002486 err = ___sys_sendmsg(sock,
Al Viro666547f2014-04-06 14:03:05 -04002487 (struct user_msghdr __user *)entry,
Tom Herbert28a94d82016-03-07 14:11:02 -08002488 &msg_sys, flags, &used_address, MSG_EOR);
Anton Blanchard228e5482011-05-02 20:21:35 +00002489 if (err < 0)
2490 break;
2491 err = put_user(err, &entry->msg_len);
2492 ++entry;
2493 }
2494
2495 if (err)
2496 break;
2497 ++datagrams;
Soheil Hassas Yeganeh30238982016-11-04 15:36:49 -04002498 if (msg_data_left(&msg_sys))
2499 break;
Eric Dumazeta78cb842016-01-08 08:37:20 -08002500 cond_resched();
Anton Blanchard228e5482011-05-02 20:21:35 +00002501 }
2502
Anton Blanchard228e5482011-05-02 20:21:35 +00002503 fput_light(sock->file, fput_needed);
2504
Anton Blanchard728ffb82011-08-04 14:07:38 +00002505 /* We only return an error if no datagrams were able to be sent */
2506 if (datagrams != 0)
Anton Blanchard228e5482011-05-02 20:21:35 +00002507 return datagrams;
2508
Anton Blanchard228e5482011-05-02 20:21:35 +00002509 return err;
2510}
2511
2512SYSCALL_DEFINE4(sendmmsg, int, fd, struct mmsghdr __user *, mmsg,
2513 unsigned int, vlen, unsigned int, flags)
2514{
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002515 return __sys_sendmmsg(fd, mmsg, vlen, flags, true);
Anton Blanchard228e5482011-05-02 20:21:35 +00002516}
2517
Jens Axboe03b12302019-12-02 18:50:25 -07002518int recvmsg_copy_msghdr(struct msghdr *msg,
2519 struct user_msghdr __user *umsg, unsigned flags,
2520 struct sockaddr __user **uaddr,
2521 struct iovec **iov)
Jens Axboe4257c8c2019-11-25 14:27:34 -07002522{
2523 ssize_t err;
2524
2525 if (MSG_CMSG_COMPAT & flags) {
2526 struct compat_msghdr __user *msg_compat;
2527
2528 msg_compat = (struct compat_msghdr __user *) umsg;
2529 err = get_compat_msghdr(msg, msg_compat, uaddr, iov);
2530 } else {
2531 err = copy_msghdr_from_user(msg, umsg, uaddr, iov);
2532 }
2533 if (err < 0)
2534 return err;
2535
2536 return 0;
2537}
2538
2539static int ____sys_recvmsg(struct socket *sock, struct msghdr *msg_sys,
2540 struct user_msghdr __user *msg,
2541 struct sockaddr __user *uaddr,
2542 unsigned int flags, int nosec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543{
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002544 struct compat_msghdr __user *msg_compat =
Jens Axboe4257c8c2019-11-25 14:27:34 -07002545 (struct compat_msghdr __user *) msg;
2546 int __user *uaddr_len = COMPAT_NAMELEN(msg);
2547 struct sockaddr_storage addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 unsigned long cmsg_ptr;
Al Viro2da62902015-03-14 21:13:46 -04002549 int len;
Al Viro08adb7d2014-11-10 20:23:13 -05002550 ssize_t err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551
Al Viro08adb7d2014-11-10 20:23:13 -05002552 msg_sys->msg_name = &addr;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002553 cmsg_ptr = (unsigned long)msg_sys->msg_control;
2554 msg_sys->msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002555
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01002556 /* We assume all kernel code knows the size of sockaddr_storage */
2557 msg_sys->msg_namelen = 0;
2558
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 if (sock->file->f_flags & O_NONBLOCK)
2560 flags |= MSG_DONTWAIT;
Eric Dumazet1af66222019-12-06 09:38:36 -08002561
2562 if (unlikely(nosec))
2563 err = sock_recvmsg_nosec(sock, msg_sys, flags);
2564 else
2565 err = sock_recvmsg(sock, msg_sys, flags);
2566
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 if (err < 0)
Jens Axboe4257c8c2019-11-25 14:27:34 -07002568 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 len = err;
2570
2571 if (uaddr != NULL) {
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00002572 err = move_addr_to_user(&addr,
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002573 msg_sys->msg_namelen, uaddr,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002574 uaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 if (err < 0)
Jens Axboe4257c8c2019-11-25 14:27:34 -07002576 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 }
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002578 err = __put_user((msg_sys->msg_flags & ~MSG_CMSG_COMPAT),
David S. Miller37f7f422005-09-16 16:51:01 -07002579 COMPAT_FLAGS(msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 if (err)
Jens Axboe4257c8c2019-11-25 14:27:34 -07002581 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 if (MSG_CMSG_COMPAT & flags)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002583 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 &msg_compat->msg_controllen);
2585 else
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002586 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 &msg->msg_controllen);
2588 if (err)
Jens Axboe4257c8c2019-11-25 14:27:34 -07002589 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 err = len;
Jens Axboe4257c8c2019-11-25 14:27:34 -07002591out:
2592 return err;
2593}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594
Jens Axboe4257c8c2019-11-25 14:27:34 -07002595static int ___sys_recvmsg(struct socket *sock, struct user_msghdr __user *msg,
2596 struct msghdr *msg_sys, unsigned int flags, int nosec)
2597{
2598 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
2599 /* user mode address pointers */
2600 struct sockaddr __user *uaddr;
2601 ssize_t err;
2602
2603 err = recvmsg_copy_msghdr(msg_sys, msg, flags, &uaddr, &iov);
2604 if (err < 0)
2605 return err;
2606
2607 err = ____sys_recvmsg(sock, msg_sys, msg, uaddr, flags, nosec);
Al Viroda184282015-03-21 19:29:06 -04002608 kfree(iov);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002609 return err;
2610}
2611
2612/*
2613 * BSD recvmsg interface
2614 */
2615
Jens Axboe03b12302019-12-02 18:50:25 -07002616long __sys_recvmsg_sock(struct socket *sock, struct msghdr *msg,
2617 struct user_msghdr __user *umsg,
2618 struct sockaddr __user *uaddr, unsigned int flags)
Jens Axboeaa1fa282019-04-19 13:38:09 -06002619{
Jens Axboed69e0772019-11-25 17:04:13 -07002620 /* disallow ancillary data requests from this path */
Jens Axboe03b12302019-12-02 18:50:25 -07002621 if (msg->msg_control || msg->msg_controllen)
2622 return -EINVAL;
Jens Axboed69e0772019-11-25 17:04:13 -07002623
Jens Axboe03b12302019-12-02 18:50:25 -07002624 return ____sys_recvmsg(sock, msg, umsg, uaddr, flags, 0);
Jens Axboeaa1fa282019-04-19 13:38:09 -06002625}
2626
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002627long __sys_recvmsg(int fd, struct user_msghdr __user *msg, unsigned int flags,
2628 bool forbid_cmsg_compat)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002629{
2630 int fput_needed, err;
2631 struct msghdr msg_sys;
Andy Lutomirski1be374a2013-05-22 14:07:44 -07002632 struct socket *sock;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002633
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002634 if (forbid_cmsg_compat && (flags & MSG_CMSG_COMPAT))
2635 return -EINVAL;
2636
Andy Lutomirski1be374a2013-05-22 14:07:44 -07002637 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002638 if (!sock)
2639 goto out;
2640
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002641 err = ___sys_recvmsg(sock, msg, &msg_sys, flags, 0);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002642
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002643 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644out:
2645 return err;
2646}
2647
Al Viro666547f2014-04-06 14:03:05 -04002648SYSCALL_DEFINE3(recvmsg, int, fd, struct user_msghdr __user *, msg,
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002649 unsigned int, flags)
2650{
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002651 return __sys_recvmsg(fd, msg, flags, true);
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002652}
2653
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002654/*
2655 * Linux recvmmsg interface
2656 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657
Arnd Bergmanne11d4282018-04-18 13:43:52 +02002658static int do_recvmmsg(int fd, struct mmsghdr __user *mmsg,
2659 unsigned int vlen, unsigned int flags,
2660 struct timespec64 *timeout)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002661{
2662 int fput_needed, err, datagrams;
2663 struct socket *sock;
2664 struct mmsghdr __user *entry;
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002665 struct compat_mmsghdr __user *compat_entry;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002666 struct msghdr msg_sys;
Deepa Dinamani766b9f92016-05-19 17:09:05 -07002667 struct timespec64 end_time;
2668 struct timespec64 timeout64;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002669
2670 if (timeout &&
2671 poll_select_set_timeout(&end_time, timeout->tv_sec,
2672 timeout->tv_nsec))
2673 return -EINVAL;
2674
2675 datagrams = 0;
2676
2677 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2678 if (!sock)
2679 return err;
2680
Soheil Hassas Yeganeh7797dc42018-02-27 18:22:40 -05002681 if (likely(!(flags & MSG_ERRQUEUE))) {
2682 err = sock_error(sock->sk);
2683 if (err) {
2684 datagrams = err;
2685 goto out_put;
2686 }
Maxime Jayate623a9e2017-02-21 18:35:51 +01002687 }
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002688
2689 entry = mmsg;
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002690 compat_entry = (struct compat_mmsghdr __user *)mmsg;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002691
2692 while (datagrams < vlen) {
2693 /*
2694 * No need to ask LSM for more than the first datagram.
2695 */
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002696 if (MSG_CMSG_COMPAT & flags) {
Al Viro666547f2014-04-06 14:03:05 -04002697 err = ___sys_recvmsg(sock, (struct user_msghdr __user *)compat_entry,
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002698 &msg_sys, flags & ~MSG_WAITFORONE,
2699 datagrams);
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002700 if (err < 0)
2701 break;
2702 err = __put_user(err, &compat_entry->msg_len);
2703 ++compat_entry;
2704 } else {
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002705 err = ___sys_recvmsg(sock,
Al Viro666547f2014-04-06 14:03:05 -04002706 (struct user_msghdr __user *)entry,
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002707 &msg_sys, flags & ~MSG_WAITFORONE,
2708 datagrams);
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002709 if (err < 0)
2710 break;
2711 err = put_user(err, &entry->msg_len);
2712 ++entry;
2713 }
2714
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002715 if (err)
2716 break;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002717 ++datagrams;
2718
Brandon L Black71c5c1592010-03-26 16:18:03 +00002719 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
2720 if (flags & MSG_WAITFORONE)
2721 flags |= MSG_DONTWAIT;
2722
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002723 if (timeout) {
Deepa Dinamani766b9f92016-05-19 17:09:05 -07002724 ktime_get_ts64(&timeout64);
Arnd Bergmannc2e6c852018-04-18 13:42:25 +02002725 *timeout = timespec64_sub(end_time, timeout64);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002726 if (timeout->tv_sec < 0) {
2727 timeout->tv_sec = timeout->tv_nsec = 0;
2728 break;
2729 }
2730
2731 /* Timeout, return less than vlen datagrams */
2732 if (timeout->tv_nsec == 0 && timeout->tv_sec == 0)
2733 break;
2734 }
2735
2736 /* Out of band data, return right away */
2737 if (msg_sys.msg_flags & MSG_OOB)
2738 break;
Eric Dumazeta78cb842016-01-08 08:37:20 -08002739 cond_resched();
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002740 }
2741
Arnaldo Carvalho de Melo34b88a62016-03-14 09:56:35 -03002742 if (err == 0)
2743 goto out_put;
2744
2745 if (datagrams == 0) {
2746 datagrams = err;
2747 goto out_put;
2748 }
2749
2750 /*
2751 * We may return less entries than requested (vlen) if the
2752 * sock is non block and there aren't enough datagrams...
2753 */
2754 if (err != -EAGAIN) {
2755 /*
2756 * ... or if recvmsg returns an error after we
2757 * received some datagrams, where we record the
2758 * error to return on the next call or if the
2759 * app asks about it using getsockopt(SO_ERROR).
2760 */
2761 sock->sk->sk_err = -err;
2762 }
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002763out_put:
2764 fput_light(sock->file, fput_needed);
2765
Arnaldo Carvalho de Melo34b88a62016-03-14 09:56:35 -03002766 return datagrams;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002767}
2768
Arnd Bergmanne11d4282018-04-18 13:43:52 +02002769int __sys_recvmmsg(int fd, struct mmsghdr __user *mmsg,
2770 unsigned int vlen, unsigned int flags,
2771 struct __kernel_timespec __user *timeout,
2772 struct old_timespec32 __user *timeout32)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002773{
2774 int datagrams;
Arnd Bergmannc2e6c852018-04-18 13:42:25 +02002775 struct timespec64 timeout_sys;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002776
Arnd Bergmanne11d4282018-04-18 13:43:52 +02002777 if (timeout && get_timespec64(&timeout_sys, timeout))
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002778 return -EFAULT;
2779
Arnd Bergmanne11d4282018-04-18 13:43:52 +02002780 if (timeout32 && get_old_timespec32(&timeout_sys, timeout32))
2781 return -EFAULT;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002782
Arnd Bergmanne11d4282018-04-18 13:43:52 +02002783 if (!timeout && !timeout32)
2784 return do_recvmmsg(fd, mmsg, vlen, flags, NULL);
2785
2786 datagrams = do_recvmmsg(fd, mmsg, vlen, flags, &timeout_sys);
2787
2788 if (datagrams <= 0)
2789 return datagrams;
2790
2791 if (timeout && put_timespec64(&timeout_sys, timeout))
2792 datagrams = -EFAULT;
2793
2794 if (timeout32 && put_old_timespec32(&timeout_sys, timeout32))
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002795 datagrams = -EFAULT;
2796
2797 return datagrams;
2798}
2799
Dominik Brodowski1255e262018-03-13 20:44:21 +01002800SYSCALL_DEFINE5(recvmmsg, int, fd, struct mmsghdr __user *, mmsg,
2801 unsigned int, vlen, unsigned int, flags,
Arnd Bergmannc2e6c852018-04-18 13:42:25 +02002802 struct __kernel_timespec __user *, timeout)
Dominik Brodowski1255e262018-03-13 20:44:21 +01002803{
Arnd Bergmanne11d4282018-04-18 13:43:52 +02002804 if (flags & MSG_CMSG_COMPAT)
2805 return -EINVAL;
2806
2807 return __sys_recvmmsg(fd, mmsg, vlen, flags, timeout, NULL);
Dominik Brodowski1255e262018-03-13 20:44:21 +01002808}
2809
Arnd Bergmanne11d4282018-04-18 13:43:52 +02002810#ifdef CONFIG_COMPAT_32BIT_TIME
2811SYSCALL_DEFINE5(recvmmsg_time32, int, fd, struct mmsghdr __user *, mmsg,
2812 unsigned int, vlen, unsigned int, flags,
2813 struct old_timespec32 __user *, timeout)
2814{
2815 if (flags & MSG_CMSG_COMPAT)
2816 return -EINVAL;
2817
2818 return __sys_recvmmsg(fd, mmsg, vlen, flags, NULL, timeout);
2819}
2820#endif
2821
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002822#ifdef __ARCH_WANT_SYS_SOCKETCALL
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823/* Argument list sizes for sys_socketcall */
2824#define AL(x) ((x) * sizeof(unsigned long))
Anton Blanchard228e5482011-05-02 20:21:35 +00002825static const unsigned char nargs[21] = {
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002826 AL(0), AL(3), AL(3), AL(3), AL(2), AL(3),
2827 AL(3), AL(3), AL(4), AL(4), AL(4), AL(6),
2828 AL(6), AL(2), AL(5), AL(5), AL(3), AL(3),
Anton Blanchard228e5482011-05-02 20:21:35 +00002829 AL(4), AL(5), AL(4)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002830};
2831
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832#undef AL
2833
2834/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002835 * System call vectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 *
2837 * Argument checking cleaned up. Saved 20% in size.
2838 * This function doesn't need to set the kernel lock because
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002839 * it is set by the callees.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 */
2841
Heiko Carstens3e0fa652009-01-14 14:14:24 +01002842SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843{
Chen Gang2950fa92013-04-07 16:55:23 +08002844 unsigned long a[AUDITSC_ARGS];
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002845 unsigned long a0, a1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 int err;
Arjan van de Ven47379052009-09-28 12:57:44 -07002847 unsigned int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848
Anton Blanchard228e5482011-05-02 20:21:35 +00002849 if (call < 1 || call > SYS_SENDMMSG)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 return -EINVAL;
Jeremy Clinec8e8cd52018-07-27 22:43:01 +00002851 call = array_index_nospec(call, SYS_SENDMMSG + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852
Arjan van de Ven47379052009-09-28 12:57:44 -07002853 len = nargs[call];
2854 if (len > sizeof(a))
2855 return -EINVAL;
2856
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 /* copy_from_user should be SMP safe. */
Arjan van de Ven47379052009-09-28 12:57:44 -07002858 if (copy_from_user(a, args, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 return -EFAULT;
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002860
Chen Gang2950fa92013-04-07 16:55:23 +08002861 err = audit_socketcall(nargs[call] / sizeof(unsigned long), a);
2862 if (err)
2863 return err;
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002864
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002865 a0 = a[0];
2866 a1 = a[1];
2867
2868 switch (call) {
2869 case SYS_SOCKET:
Dominik Brodowski9d6a15c2018-03-13 19:29:43 +01002870 err = __sys_socket(a0, a1, a[2]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002871 break;
2872 case SYS_BIND:
Dominik Brodowskia87d35d2018-03-13 19:33:09 +01002873 err = __sys_bind(a0, (struct sockaddr __user *)a1, a[2]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002874 break;
2875 case SYS_CONNECT:
Dominik Brodowski1387c2c2018-03-13 19:35:09 +01002876 err = __sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002877 break;
2878 case SYS_LISTEN:
Dominik Brodowski25e290e2018-03-13 19:36:54 +01002879 err = __sys_listen(a0, a1);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002880 break;
2881 case SYS_ACCEPT:
Dominik Brodowski4541e802018-03-13 19:24:23 +01002882 err = __sys_accept4(a0, (struct sockaddr __user *)a1,
2883 (int __user *)a[2], 0);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002884 break;
2885 case SYS_GETSOCKNAME:
2886 err =
Dominik Brodowski8882a102018-03-13 19:43:14 +01002887 __sys_getsockname(a0, (struct sockaddr __user *)a1,
2888 (int __user *)a[2]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002889 break;
2890 case SYS_GETPEERNAME:
2891 err =
Dominik Brodowskib21c8f82018-03-13 19:47:00 +01002892 __sys_getpeername(a0, (struct sockaddr __user *)a1,
2893 (int __user *)a[2]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002894 break;
2895 case SYS_SOCKETPAIR:
Dominik Brodowski6debc8d2018-03-13 19:49:23 +01002896 err = __sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002897 break;
2898 case SYS_SEND:
Dominik Brodowskif3bf8962018-03-13 19:52:00 +01002899 err = __sys_sendto(a0, (void __user *)a1, a[2], a[3],
2900 NULL, 0);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002901 break;
2902 case SYS_SENDTO:
Dominik Brodowski211b6342018-03-13 19:18:52 +01002903 err = __sys_sendto(a0, (void __user *)a1, a[2], a[3],
2904 (struct sockaddr __user *)a[4], a[5]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002905 break;
2906 case SYS_RECV:
Dominik Brodowskid27e9af2018-03-13 19:54:17 +01002907 err = __sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
2908 NULL, NULL);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002909 break;
2910 case SYS_RECVFROM:
Dominik Brodowski7a09e1e2018-03-13 19:10:06 +01002911 err = __sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
2912 (struct sockaddr __user *)a[4],
2913 (int __user *)a[5]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002914 break;
2915 case SYS_SHUTDOWN:
Dominik Brodowski005a1ae2018-03-13 20:07:05 +01002916 err = __sys_shutdown(a0, a1);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002917 break;
2918 case SYS_SETSOCKOPT:
Dominik Brodowskicc36dca2018-03-13 20:10:59 +01002919 err = __sys_setsockopt(a0, a1, a[2], (char __user *)a[3],
2920 a[4]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002921 break;
2922 case SYS_GETSOCKOPT:
2923 err =
Dominik Brodowski13a2d702018-03-13 20:15:04 +01002924 __sys_getsockopt(a0, a1, a[2], (char __user *)a[3],
2925 (int __user *)a[4]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002926 break;
2927 case SYS_SENDMSG:
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002928 err = __sys_sendmsg(a0, (struct user_msghdr __user *)a1,
2929 a[2], true);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002930 break;
Anton Blanchard228e5482011-05-02 20:21:35 +00002931 case SYS_SENDMMSG:
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002932 err = __sys_sendmmsg(a0, (struct mmsghdr __user *)a1, a[2],
2933 a[3], true);
Anton Blanchard228e5482011-05-02 20:21:35 +00002934 break;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002935 case SYS_RECVMSG:
Dominik Brodowskie1834a32018-03-13 20:35:57 +01002936 err = __sys_recvmsg(a0, (struct user_msghdr __user *)a1,
2937 a[2], true);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002938 break;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002939 case SYS_RECVMMSG:
Arnd Bergmann3ca47e92019-04-23 17:43:50 +02002940 if (IS_ENABLED(CONFIG_64BIT))
Arnd Bergmanne11d4282018-04-18 13:43:52 +02002941 err = __sys_recvmmsg(a0, (struct mmsghdr __user *)a1,
2942 a[2], a[3],
2943 (struct __kernel_timespec __user *)a[4],
2944 NULL);
2945 else
2946 err = __sys_recvmmsg(a0, (struct mmsghdr __user *)a1,
2947 a[2], a[3], NULL,
2948 (struct old_timespec32 __user *)a[4]);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002949 break;
Ulrich Drepperde11def2008-11-19 15:36:14 -08002950 case SYS_ACCEPT4:
Dominik Brodowski4541e802018-03-13 19:24:23 +01002951 err = __sys_accept4(a0, (struct sockaddr __user *)a1,
2952 (int __user *)a[2], a[3]);
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07002953 break;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002954 default:
2955 err = -EINVAL;
2956 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 }
2958 return err;
2959}
2960
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002961#endif /* __ARCH_WANT_SYS_SOCKETCALL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002963/**
2964 * sock_register - add a socket protocol handler
2965 * @ops: description of protocol
2966 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 * This function is called by a protocol handler that wants to
2968 * advertise its address family, and have it linked into the
Masanari Iidae793c0f2014-09-04 23:44:36 +09002969 * socket interface. The value ops->family corresponds to the
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002970 * socket system call protocol family.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 */
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002972int sock_register(const struct net_proto_family *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973{
2974 int err;
2975
2976 if (ops->family >= NPROTO) {
Yang Yingliang3410f222014-02-12 17:09:55 +08002977 pr_crit("protocol %d >= NPROTO(%d)\n", ops->family, NPROTO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 return -ENOBUFS;
2979 }
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002980
2981 spin_lock(&net_family_lock);
Eric Dumazet190683a2010-11-10 10:50:44 +00002982 if (rcu_dereference_protected(net_families[ops->family],
2983 lockdep_is_held(&net_family_lock)))
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002984 err = -EEXIST;
2985 else {
Eric Dumazetcf778b02012-01-12 04:41:32 +00002986 rcu_assign_pointer(net_families[ops->family], ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 err = 0;
2988 }
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002989 spin_unlock(&net_family_lock);
2990
Yang Yingliang3410f222014-02-12 17:09:55 +08002991 pr_info("NET: Registered protocol family %d\n", ops->family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 return err;
2993}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002994EXPORT_SYMBOL(sock_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002996/**
2997 * sock_unregister - remove a protocol handler
2998 * @family: protocol family to remove
2999 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 * This function is called by a protocol handler that wants to
3001 * remove its address family, and have it unlinked from the
Stephen Hemminger55737fd2006-09-01 00:23:39 -07003002 * new socket creation.
3003 *
3004 * If protocol handler is a module, then it can use module reference
3005 * counts to protect against new references. If protocol handler is not
3006 * a module then it needs to provide its own protection in
3007 * the ops->create routine.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 */
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07003009void sock_unregister(int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010{
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07003011 BUG_ON(family < 0 || family >= NPROTO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012
Stephen Hemminger55737fd2006-09-01 00:23:39 -07003013 spin_lock(&net_family_lock);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00003014 RCU_INIT_POINTER(net_families[family], NULL);
Stephen Hemminger55737fd2006-09-01 00:23:39 -07003015 spin_unlock(&net_family_lock);
3016
3017 synchronize_rcu();
3018
Yang Yingliang3410f222014-02-12 17:09:55 +08003019 pr_info("NET: Unregistered protocol family %d\n", family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003021EXPORT_SYMBOL(sock_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022
Xin Longbf2ae2e2018-03-10 18:57:50 +08003023bool sock_is_registered(int family)
3024{
Jeremy Cline66b51b02018-08-13 22:23:13 +00003025 return family < NPROTO && rcu_access_pointer(net_families[family]);
Xin Longbf2ae2e2018-03-10 18:57:50 +08003026}
3027
Andi Kleen77d76ea2005-12-22 12:43:42 -08003028static int __init sock_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029{
Nick Pigginb3e19d92011-01-07 17:50:11 +11003030 int err;
Eric W. Biederman2ca794e2012-04-19 13:20:32 +00003031 /*
3032 * Initialize the network sysctl infrastructure.
3033 */
3034 err = net_sysctl_init();
3035 if (err)
3036 goto out;
Nick Pigginb3e19d92011-01-07 17:50:11 +11003037
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07003039 * Initialize skbuff SLAB cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 */
3041 skb_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042
3043 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07003044 * Initialize the protocols module.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 */
3046
3047 init_inodecache();
Nick Pigginb3e19d92011-01-07 17:50:11 +11003048
3049 err = register_filesystem(&sock_fs_type);
3050 if (err)
3051 goto out_fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 sock_mnt = kern_mount(&sock_fs_type);
Nick Pigginb3e19d92011-01-07 17:50:11 +11003053 if (IS_ERR(sock_mnt)) {
3054 err = PTR_ERR(sock_mnt);
3055 goto out_mount;
3056 }
Andi Kleen77d76ea2005-12-22 12:43:42 -08003057
3058 /* The real protocol initialization is performed in later initcalls.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 */
3060
3061#ifdef CONFIG_NETFILTER
Pablo Neira Ayuso6d11cfd2013-05-22 22:42:36 +00003062 err = netfilter_init();
3063 if (err)
3064 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065#endif
David S. Millercbeb3212005-12-22 12:58:55 -08003066
Daniel Borkmann408eccc2014-04-01 16:20:23 +02003067 ptp_classifier_init();
Richard Cochranc1f19b52010-07-17 08:49:36 +00003068
Nick Pigginb3e19d92011-01-07 17:50:11 +11003069out:
3070 return err;
3071
3072out_mount:
3073 unregister_filesystem(&sock_fs_type);
3074out_fs:
3075 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076}
3077
Andi Kleen77d76ea2005-12-22 12:43:42 -08003078core_initcall(sock_init); /* early initcall */
3079
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080#ifdef CONFIG_PROC_FS
3081void socket_seq_show(struct seq_file *seq)
3082{
Tonghao Zhang648845a2017-12-14 05:51:58 -08003083 seq_printf(seq, "sockets: used %d\n",
3084 sock_inuse_get(seq->private));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085}
Stephen Hemminger89bddce2006-09-01 00:19:31 -07003086#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087
Shaun Pereira89bbfc92006-03-21 23:58:08 -08003088#ifdef CONFIG_COMPAT
Al Viro36fd6332017-06-26 13:19:16 -04003089static int compat_dev_ifconf(struct net *net, struct compat_ifconf __user *uifc32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08003090{
Arnd Bergmann6b960182009-11-06 23:10:54 -08003091 struct compat_ifconf ifc32;
Arnd Bergmann7a229382009-11-06 23:00:29 -08003092 struct ifconf ifc;
Arnd Bergmann7a229382009-11-06 23:00:29 -08003093 int err;
3094
Arnd Bergmann6b960182009-11-06 23:10:54 -08003095 if (copy_from_user(&ifc32, uifc32, sizeof(struct compat_ifconf)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08003096 return -EFAULT;
3097
Al Viro36fd6332017-06-26 13:19:16 -04003098 ifc.ifc_len = ifc32.ifc_len;
3099 ifc.ifc_req = compat_ptr(ifc32.ifcbuf);
Arnd Bergmann7a229382009-11-06 23:00:29 -08003100
Al Viro36fd6332017-06-26 13:19:16 -04003101 rtnl_lock();
3102 err = dev_ifconf(net, &ifc, sizeof(struct compat_ifreq));
3103 rtnl_unlock();
Arnd Bergmann7a229382009-11-06 23:00:29 -08003104 if (err)
3105 return err;
3106
Al Viro36fd6332017-06-26 13:19:16 -04003107 ifc32.ifc_len = ifc.ifc_len;
Arnd Bergmann6b960182009-11-06 23:10:54 -08003108 if (copy_to_user(uifc32, &ifc32, sizeof(struct compat_ifconf)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08003109 return -EFAULT;
3110
3111 return 0;
3112}
3113
Arnd Bergmann6b960182009-11-06 23:10:54 -08003114static int ethtool_ioctl(struct net *net, struct compat_ifreq __user *ifr32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08003115{
Ben Hutchings3a7da392011-03-17 07:34:32 +00003116 struct compat_ethtool_rxnfc __user *compat_rxnfc;
3117 bool convert_in = false, convert_out = false;
Al Viro44c02a22017-10-05 12:59:44 -04003118 size_t buf_size = 0;
3119 struct ethtool_rxnfc __user *rxnfc = NULL;
3120 struct ifreq ifr;
Ben Hutchings3a7da392011-03-17 07:34:32 +00003121 u32 rule_cnt = 0, actual_rule_cnt;
3122 u32 ethcmd;
Arnd Bergmann7a229382009-11-06 23:00:29 -08003123 u32 data;
Ben Hutchings3a7da392011-03-17 07:34:32 +00003124 int ret;
Arnd Bergmann7a229382009-11-06 23:00:29 -08003125
3126 if (get_user(data, &ifr32->ifr_ifru.ifru_data))
3127 return -EFAULT;
3128
Ben Hutchings3a7da392011-03-17 07:34:32 +00003129 compat_rxnfc = compat_ptr(data);
3130
3131 if (get_user(ethcmd, &compat_rxnfc->cmd))
Arnd Bergmann7a229382009-11-06 23:00:29 -08003132 return -EFAULT;
3133
Ben Hutchings3a7da392011-03-17 07:34:32 +00003134 /* Most ethtool structures are defined without padding.
3135 * Unfortunately struct ethtool_rxnfc is an exception.
3136 */
3137 switch (ethcmd) {
3138 default:
3139 break;
3140 case ETHTOOL_GRXCLSRLALL:
3141 /* Buffer size is variable */
3142 if (get_user(rule_cnt, &compat_rxnfc->rule_cnt))
3143 return -EFAULT;
3144 if (rule_cnt > KMALLOC_MAX_SIZE / sizeof(u32))
3145 return -ENOMEM;
3146 buf_size += rule_cnt * sizeof(u32);
3147 /* fall through */
3148 case ETHTOOL_GRXRINGS:
3149 case ETHTOOL_GRXCLSRLCNT:
3150 case ETHTOOL_GRXCLSRULE:
Ben Hutchings55664f32012-01-03 12:04:51 +00003151 case ETHTOOL_SRXCLSRLINS:
Ben Hutchings3a7da392011-03-17 07:34:32 +00003152 convert_out = true;
3153 /* fall through */
3154 case ETHTOOL_SRXCLSRLDEL:
Ben Hutchings3a7da392011-03-17 07:34:32 +00003155 buf_size += sizeof(struct ethtool_rxnfc);
3156 convert_in = true;
Al Viro44c02a22017-10-05 12:59:44 -04003157 rxnfc = compat_alloc_user_space(buf_size);
Ben Hutchings3a7da392011-03-17 07:34:32 +00003158 break;
3159 }
3160
Al Viro44c02a22017-10-05 12:59:44 -04003161 if (copy_from_user(&ifr.ifr_name, &ifr32->ifr_name, IFNAMSIZ))
Ben Hutchings3a7da392011-03-17 07:34:32 +00003162 return -EFAULT;
3163
Al Viro44c02a22017-10-05 12:59:44 -04003164 ifr.ifr_data = convert_in ? rxnfc : (void __user *)compat_rxnfc;
Ben Hutchings3a7da392011-03-17 07:34:32 +00003165
3166 if (convert_in) {
Alexander Duyck127fe532011-04-08 18:01:59 +00003167 /* We expect there to be holes between fs.m_ext and
Ben Hutchings3a7da392011-03-17 07:34:32 +00003168 * fs.ring_cookie and at the end of fs, but nowhere else.
3169 */
Alexander Duyck127fe532011-04-08 18:01:59 +00003170 BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc, fs.m_ext) +
3171 sizeof(compat_rxnfc->fs.m_ext) !=
3172 offsetof(struct ethtool_rxnfc, fs.m_ext) +
3173 sizeof(rxnfc->fs.m_ext));
Ben Hutchings3a7da392011-03-17 07:34:32 +00003174 BUILD_BUG_ON(
3175 offsetof(struct compat_ethtool_rxnfc, fs.location) -
3176 offsetof(struct compat_ethtool_rxnfc, fs.ring_cookie) !=
3177 offsetof(struct ethtool_rxnfc, fs.location) -
3178 offsetof(struct ethtool_rxnfc, fs.ring_cookie));
3179
3180 if (copy_in_user(rxnfc, compat_rxnfc,
Stephen Hemminger954b1242013-02-11 06:22:28 +00003181 (void __user *)(&rxnfc->fs.m_ext + 1) -
3182 (void __user *)rxnfc) ||
Ben Hutchings3a7da392011-03-17 07:34:32 +00003183 copy_in_user(&rxnfc->fs.ring_cookie,
3184 &compat_rxnfc->fs.ring_cookie,
Stephen Hemminger954b1242013-02-11 06:22:28 +00003185 (void __user *)(&rxnfc->fs.location + 1) -
Wenwen Wangb6168562018-10-18 09:36:46 -05003186 (void __user *)&rxnfc->fs.ring_cookie))
3187 return -EFAULT;
3188 if (ethcmd == ETHTOOL_GRXCLSRLALL) {
3189 if (put_user(rule_cnt, &rxnfc->rule_cnt))
3190 return -EFAULT;
3191 } else if (copy_in_user(&rxnfc->rule_cnt,
3192 &compat_rxnfc->rule_cnt,
3193 sizeof(rxnfc->rule_cnt)))
Ben Hutchings3a7da392011-03-17 07:34:32 +00003194 return -EFAULT;
3195 }
3196
Al Viro44c02a22017-10-05 12:59:44 -04003197 ret = dev_ioctl(net, SIOCETHTOOL, &ifr, NULL);
Ben Hutchings3a7da392011-03-17 07:34:32 +00003198 if (ret)
3199 return ret;
3200
3201 if (convert_out) {
3202 if (copy_in_user(compat_rxnfc, rxnfc,
Stephen Hemminger954b1242013-02-11 06:22:28 +00003203 (const void __user *)(&rxnfc->fs.m_ext + 1) -
3204 (const void __user *)rxnfc) ||
Ben Hutchings3a7da392011-03-17 07:34:32 +00003205 copy_in_user(&compat_rxnfc->fs.ring_cookie,
3206 &rxnfc->fs.ring_cookie,
Stephen Hemminger954b1242013-02-11 06:22:28 +00003207 (const void __user *)(&rxnfc->fs.location + 1) -
3208 (const void __user *)&rxnfc->fs.ring_cookie) ||
Ben Hutchings3a7da392011-03-17 07:34:32 +00003209 copy_in_user(&compat_rxnfc->rule_cnt, &rxnfc->rule_cnt,
3210 sizeof(rxnfc->rule_cnt)))
3211 return -EFAULT;
3212
3213 if (ethcmd == ETHTOOL_GRXCLSRLALL) {
3214 /* As an optimisation, we only copy the actual
3215 * number of rules that the underlying
3216 * function returned. Since Mallory might
3217 * change the rule count in user memory, we
3218 * check that it is less than the rule count
3219 * originally given (as the user buffer size),
3220 * which has been range-checked.
3221 */
3222 if (get_user(actual_rule_cnt, &rxnfc->rule_cnt))
3223 return -EFAULT;
3224 if (actual_rule_cnt < rule_cnt)
3225 rule_cnt = actual_rule_cnt;
3226 if (copy_in_user(&compat_rxnfc->rule_locs[0],
3227 &rxnfc->rule_locs[0],
3228 rule_cnt * sizeof(u32)))
3229 return -EFAULT;
3230 }
3231 }
3232
3233 return 0;
Arnd Bergmann7a229382009-11-06 23:00:29 -08003234}
3235
Arnd Bergmann7a50a242009-11-08 20:57:03 -08003236static int compat_siocwandev(struct net *net, struct compat_ifreq __user *uifr32)
3237{
Arnd Bergmann7a50a242009-11-08 20:57:03 -08003238 compat_uptr_t uptr32;
Al Viro44c02a22017-10-05 12:59:44 -04003239 struct ifreq ifr;
3240 void __user *saved;
3241 int err;
Arnd Bergmann7a50a242009-11-08 20:57:03 -08003242
Al Viro44c02a22017-10-05 12:59:44 -04003243 if (copy_from_user(&ifr, uifr32, sizeof(struct compat_ifreq)))
Arnd Bergmann7a50a242009-11-08 20:57:03 -08003244 return -EFAULT;
3245
3246 if (get_user(uptr32, &uifr32->ifr_settings.ifs_ifsu))
3247 return -EFAULT;
3248
Al Viro44c02a22017-10-05 12:59:44 -04003249 saved = ifr.ifr_settings.ifs_ifsu.raw_hdlc;
3250 ifr.ifr_settings.ifs_ifsu.raw_hdlc = compat_ptr(uptr32);
Arnd Bergmann7a50a242009-11-08 20:57:03 -08003251
Al Viro44c02a22017-10-05 12:59:44 -04003252 err = dev_ioctl(net, SIOCWANDEV, &ifr, NULL);
3253 if (!err) {
3254 ifr.ifr_settings.ifs_ifsu.raw_hdlc = saved;
3255 if (copy_to_user(uifr32, &ifr, sizeof(struct compat_ifreq)))
3256 err = -EFAULT;
Joe Perchesccbd6a52010-05-14 10:58:26 +00003257 }
Al Viro44c02a22017-10-05 12:59:44 -04003258 return err;
Arnd Bergmann7a229382009-11-06 23:00:29 -08003259}
3260
Ben Hutchings590d4692013-11-18 17:04:13 +00003261/* Handle ioctls that use ifreq::ifr_data and just need struct ifreq converted */
3262static int compat_ifr_data_ioctl(struct net *net, unsigned int cmd,
Arnd Bergmann6b960182009-11-06 23:10:54 -08003263 struct compat_ifreq __user *u_ifreq32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08003264{
Al Viro44c02a22017-10-05 12:59:44 -04003265 struct ifreq ifreq;
Arnd Bergmann7a229382009-11-06 23:00:29 -08003266 u32 data32;
3267
Al Viro44c02a22017-10-05 12:59:44 -04003268 if (copy_from_user(ifreq.ifr_name, u_ifreq32->ifr_name, IFNAMSIZ))
Arnd Bergmann7a229382009-11-06 23:00:29 -08003269 return -EFAULT;
Al Viro44c02a22017-10-05 12:59:44 -04003270 if (get_user(data32, &u_ifreq32->ifr_data))
Arnd Bergmann7a229382009-11-06 23:00:29 -08003271 return -EFAULT;
Al Viro44c02a22017-10-05 12:59:44 -04003272 ifreq.ifr_data = compat_ptr(data32);
Arnd Bergmann7a229382009-11-06 23:00:29 -08003273
Al Viro44c02a22017-10-05 12:59:44 -04003274 return dev_ioctl(net, cmd, &ifreq, NULL);
Arnd Bergmann7a229382009-11-06 23:00:29 -08003275}
3276
Johannes Berg37ac39b2019-01-25 22:43:18 +01003277static int compat_ifreq_ioctl(struct net *net, struct socket *sock,
3278 unsigned int cmd,
3279 struct compat_ifreq __user *uifr32)
3280{
3281 struct ifreq __user *uifr;
3282 int err;
3283
3284 /* Handle the fact that while struct ifreq has the same *layout* on
3285 * 32/64 for everything but ifreq::ifru_ifmap and ifreq::ifru_data,
3286 * which are handled elsewhere, it still has different *size* due to
3287 * ifreq::ifru_ifmap (which is 16 bytes on 32 bit, 24 bytes on 64-bit,
3288 * resulting in struct ifreq being 32 and 40 bytes respectively).
3289 * As a result, if the struct happens to be at the end of a page and
3290 * the next page isn't readable/writable, we get a fault. To prevent
3291 * that, copy back and forth to the full size.
3292 */
3293
3294 uifr = compat_alloc_user_space(sizeof(*uifr));
3295 if (copy_in_user(uifr, uifr32, sizeof(*uifr32)))
3296 return -EFAULT;
3297
3298 err = sock_do_ioctl(net, sock, cmd, (unsigned long)uifr);
3299
3300 if (!err) {
3301 switch (cmd) {
3302 case SIOCGIFFLAGS:
3303 case SIOCGIFMETRIC:
3304 case SIOCGIFMTU:
3305 case SIOCGIFMEM:
3306 case SIOCGIFHWADDR:
3307 case SIOCGIFINDEX:
3308 case SIOCGIFADDR:
3309 case SIOCGIFBRDADDR:
3310 case SIOCGIFDSTADDR:
3311 case SIOCGIFNETMASK:
3312 case SIOCGIFPFLAGS:
3313 case SIOCGIFTXQLEN:
3314 case SIOCGMIIPHY:
3315 case SIOCGMIIREG:
Johannes Bergc6c9fee2019-01-25 22:43:19 +01003316 case SIOCGIFNAME:
Johannes Berg37ac39b2019-01-25 22:43:18 +01003317 if (copy_in_user(uifr32, uifr, sizeof(*uifr32)))
3318 err = -EFAULT;
3319 break;
3320 }
3321 }
3322 return err;
3323}
3324
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003325static int compat_sioc_ifmap(struct net *net, unsigned int cmd,
3326 struct compat_ifreq __user *uifr32)
3327{
3328 struct ifreq ifr;
3329 struct compat_ifmap __user *uifmap32;
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003330 int err;
3331
3332 uifmap32 = &uifr32->ifr_ifru.ifru_map;
3333 err = copy_from_user(&ifr, uifr32, sizeof(ifr.ifr_name));
Mathieu Desnoyers3ddc5b42013-09-11 14:23:18 -07003334 err |= get_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
3335 err |= get_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
3336 err |= get_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
3337 err |= get_user(ifr.ifr_map.irq, &uifmap32->irq);
3338 err |= get_user(ifr.ifr_map.dma, &uifmap32->dma);
3339 err |= get_user(ifr.ifr_map.port, &uifmap32->port);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003340 if (err)
3341 return -EFAULT;
3342
Al Viro44c02a22017-10-05 12:59:44 -04003343 err = dev_ioctl(net, cmd, &ifr, NULL);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003344
3345 if (cmd == SIOCGIFMAP && !err) {
3346 err = copy_to_user(uifr32, &ifr, sizeof(ifr.ifr_name));
Mathieu Desnoyers3ddc5b42013-09-11 14:23:18 -07003347 err |= put_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
3348 err |= put_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
3349 err |= put_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
3350 err |= put_user(ifr.ifr_map.irq, &uifmap32->irq);
3351 err |= put_user(ifr.ifr_map.dma, &uifmap32->dma);
3352 err |= put_user(ifr.ifr_map.port, &uifmap32->port);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003353 if (err)
3354 err = -EFAULT;
3355 }
3356 return err;
3357}
3358
Arnd Bergmann7a229382009-11-06 23:00:29 -08003359struct rtentry32 {
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003360 u32 rt_pad1;
Arnd Bergmann7a229382009-11-06 23:00:29 -08003361 struct sockaddr rt_dst; /* target address */
3362 struct sockaddr rt_gateway; /* gateway addr (RTF_GATEWAY) */
3363 struct sockaddr rt_genmask; /* target network mask (IP) */
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003364 unsigned short rt_flags;
3365 short rt_pad2;
3366 u32 rt_pad3;
3367 unsigned char rt_tos;
3368 unsigned char rt_class;
3369 short rt_pad4;
3370 short rt_metric; /* +1 for binary compatibility! */
Arnd Bergmann7a229382009-11-06 23:00:29 -08003371 /* char * */ u32 rt_dev; /* forcing the device at add */
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003372 u32 rt_mtu; /* per route MTU/Window */
3373 u32 rt_window; /* Window clamping */
Arnd Bergmann7a229382009-11-06 23:00:29 -08003374 unsigned short rt_irtt; /* Initial RTT */
3375};
3376
3377struct in6_rtmsg32 {
3378 struct in6_addr rtmsg_dst;
3379 struct in6_addr rtmsg_src;
3380 struct in6_addr rtmsg_gateway;
3381 u32 rtmsg_type;
3382 u16 rtmsg_dst_len;
3383 u16 rtmsg_src_len;
3384 u32 rtmsg_metric;
3385 u32 rtmsg_info;
3386 u32 rtmsg_flags;
3387 s32 rtmsg_ifindex;
3388};
3389
Arnd Bergmann6b960182009-11-06 23:10:54 -08003390static int routing_ioctl(struct net *net, struct socket *sock,
3391 unsigned int cmd, void __user *argp)
Arnd Bergmann7a229382009-11-06 23:00:29 -08003392{
3393 int ret;
3394 void *r = NULL;
3395 struct in6_rtmsg r6;
3396 struct rtentry r4;
3397 char devname[16];
3398 u32 rtdev;
3399 mm_segment_t old_fs = get_fs();
3400
Arnd Bergmann6b960182009-11-06 23:10:54 -08003401 if (sock && sock->sk && sock->sk->sk_family == AF_INET6) { /* ipv6 */
3402 struct in6_rtmsg32 __user *ur6 = argp;
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003403 ret = copy_from_user(&r6.rtmsg_dst, &(ur6->rtmsg_dst),
Arnd Bergmann7a229382009-11-06 23:00:29 -08003404 3 * sizeof(struct in6_addr));
Mathieu Desnoyers3ddc5b42013-09-11 14:23:18 -07003405 ret |= get_user(r6.rtmsg_type, &(ur6->rtmsg_type));
3406 ret |= get_user(r6.rtmsg_dst_len, &(ur6->rtmsg_dst_len));
3407 ret |= get_user(r6.rtmsg_src_len, &(ur6->rtmsg_src_len));
3408 ret |= get_user(r6.rtmsg_metric, &(ur6->rtmsg_metric));
3409 ret |= get_user(r6.rtmsg_info, &(ur6->rtmsg_info));
3410 ret |= get_user(r6.rtmsg_flags, &(ur6->rtmsg_flags));
3411 ret |= get_user(r6.rtmsg_ifindex, &(ur6->rtmsg_ifindex));
Arnd Bergmann7a229382009-11-06 23:00:29 -08003412
3413 r = (void *) &r6;
3414 } else { /* ipv4 */
Arnd Bergmann6b960182009-11-06 23:10:54 -08003415 struct rtentry32 __user *ur4 = argp;
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003416 ret = copy_from_user(&r4.rt_dst, &(ur4->rt_dst),
Arnd Bergmann7a229382009-11-06 23:00:29 -08003417 3 * sizeof(struct sockaddr));
Mathieu Desnoyers3ddc5b42013-09-11 14:23:18 -07003418 ret |= get_user(r4.rt_flags, &(ur4->rt_flags));
3419 ret |= get_user(r4.rt_metric, &(ur4->rt_metric));
3420 ret |= get_user(r4.rt_mtu, &(ur4->rt_mtu));
3421 ret |= get_user(r4.rt_window, &(ur4->rt_window));
3422 ret |= get_user(r4.rt_irtt, &(ur4->rt_irtt));
3423 ret |= get_user(rtdev, &(ur4->rt_dev));
Arnd Bergmann7a229382009-11-06 23:00:29 -08003424 if (rtdev) {
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003425 ret |= copy_from_user(devname, compat_ptr(rtdev), 15);
stephen hemmingerc3f52ae2011-02-23 09:06:48 +00003426 r4.rt_dev = (char __user __force *)devname;
3427 devname[15] = 0;
Arnd Bergmann7a229382009-11-06 23:00:29 -08003428 } else
3429 r4.rt_dev = NULL;
3430
3431 r = (void *) &r4;
3432 }
3433
3434 if (ret) {
3435 ret = -EFAULT;
3436 goto out;
3437 }
3438
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003439 set_fs(KERNEL_DS);
Johannes Berg63ff03a2019-01-25 22:43:17 +01003440 ret = sock_do_ioctl(net, sock, cmd, (unsigned long) r);
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003441 set_fs(old_fs);
Arnd Bergmann7a229382009-11-06 23:00:29 -08003442
3443out:
Arnd Bergmann7a229382009-11-06 23:00:29 -08003444 return ret;
3445}
3446
3447/* Since old style bridge ioctl's endup using SIOCDEVPRIVATE
3448 * for some operations; this forces use of the newer bridge-utils that
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003449 * use compatible ioctls
Arnd Bergmann7a229382009-11-06 23:00:29 -08003450 */
Arnd Bergmann6b960182009-11-06 23:10:54 -08003451static int old_bridge_ioctl(compat_ulong_t __user *argp)
Arnd Bergmann7a229382009-11-06 23:00:29 -08003452{
Arnd Bergmann6b960182009-11-06 23:10:54 -08003453 compat_ulong_t tmp;
Arnd Bergmann7a229382009-11-06 23:00:29 -08003454
Arnd Bergmann6b960182009-11-06 23:10:54 -08003455 if (get_user(tmp, argp))
Arnd Bergmann7a229382009-11-06 23:00:29 -08003456 return -EFAULT;
3457 if (tmp == BRCTL_GET_VERSION)
3458 return BRCTL_VERSION + 1;
3459 return -EINVAL;
3460}
3461
Arnd Bergmann6b960182009-11-06 23:10:54 -08003462static int compat_sock_ioctl_trans(struct file *file, struct socket *sock,
3463 unsigned int cmd, unsigned long arg)
3464{
3465 void __user *argp = compat_ptr(arg);
3466 struct sock *sk = sock->sk;
3467 struct net *net = sock_net(sk);
Arnd Bergmann7a229382009-11-06 23:00:29 -08003468
Arnd Bergmann6b960182009-11-06 23:10:54 -08003469 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15))
Ben Hutchings590d4692013-11-18 17:04:13 +00003470 return compat_ifr_data_ioctl(net, cmd, argp);
Arnd Bergmann7a229382009-11-06 23:00:29 -08003471
Arnd Bergmann6b960182009-11-06 23:10:54 -08003472 switch (cmd) {
3473 case SIOCSIFBR:
3474 case SIOCGIFBR:
3475 return old_bridge_ioctl(argp);
Arnd Bergmann6b960182009-11-06 23:10:54 -08003476 case SIOCGIFCONF:
Al Viro36fd6332017-06-26 13:19:16 -04003477 return compat_dev_ifconf(net, argp);
Arnd Bergmann6b960182009-11-06 23:10:54 -08003478 case SIOCETHTOOL:
3479 return ethtool_ioctl(net, argp);
Arnd Bergmann7a50a242009-11-08 20:57:03 -08003480 case SIOCWANDEV:
3481 return compat_siocwandev(net, argp);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003482 case SIOCGIFMAP:
3483 case SIOCSIFMAP:
3484 return compat_sioc_ifmap(net, cmd, argp);
Arnd Bergmann6b960182009-11-06 23:10:54 -08003485 case SIOCADDRT:
3486 case SIOCDELRT:
3487 return routing_ioctl(net, sock, cmd, argp);
Arnd Bergmann0768e172019-04-17 22:56:11 +02003488 case SIOCGSTAMP_OLD:
3489 case SIOCGSTAMPNS_OLD:
Arnd Bergmannc7cbdbf2019-04-17 22:51:48 +02003490 if (!sock->ops->gettstamp)
3491 return -ENOIOCTLCMD;
Arnd Bergmann0768e172019-04-17 22:56:11 +02003492 return sock->ops->gettstamp(sock, argp, cmd == SIOCGSTAMP_OLD,
Arnd Bergmannc7cbdbf2019-04-17 22:51:48 +02003493 !COMPAT_USE_64BIT_TIME);
3494
Ben Hutchings590d4692013-11-18 17:04:13 +00003495 case SIOCBONDSLAVEINFOQUERY:
3496 case SIOCBONDINFOQUERY:
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003497 case SIOCSHWTSTAMP:
Ben Hutchingsfd468c72013-11-14 01:19:29 +00003498 case SIOCGHWTSTAMP:
Ben Hutchings590d4692013-11-18 17:04:13 +00003499 return compat_ifr_data_ioctl(net, cmd, argp);
Arnd Bergmann7a229382009-11-06 23:00:29 -08003500
Arnd Bergmann6b960182009-11-06 23:10:54 -08003501 case FIOSETOWN:
3502 case SIOCSPGRP:
3503 case FIOGETOWN:
3504 case SIOCGPGRP:
3505 case SIOCBRADDBR:
3506 case SIOCBRDELBR:
3507 case SIOCGIFVLAN:
3508 case SIOCSIFVLAN:
3509 case SIOCADDDLCI:
3510 case SIOCDELDLCI:
Andrey Vaginc62cce22016-10-24 18:29:13 -07003511 case SIOCGSKNS:
Arnd Bergmann0768e172019-04-17 22:56:11 +02003512 case SIOCGSTAMP_NEW:
3513 case SIOCGSTAMPNS_NEW:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003514 return sock_ioctl(file, cmd, arg);
3515
3516 case SIOCGIFFLAGS:
3517 case SIOCSIFFLAGS:
3518 case SIOCGIFMETRIC:
3519 case SIOCSIFMETRIC:
3520 case SIOCGIFMTU:
3521 case SIOCSIFMTU:
3522 case SIOCGIFMEM:
3523 case SIOCSIFMEM:
3524 case SIOCGIFHWADDR:
3525 case SIOCSIFHWADDR:
3526 case SIOCADDMULTI:
3527 case SIOCDELMULTI:
3528 case SIOCGIFINDEX:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003529 case SIOCGIFADDR:
3530 case SIOCSIFADDR:
3531 case SIOCSIFHWBROADCAST:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003532 case SIOCDIFADDR:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003533 case SIOCGIFBRDADDR:
3534 case SIOCSIFBRDADDR:
3535 case SIOCGIFDSTADDR:
3536 case SIOCSIFDSTADDR:
3537 case SIOCGIFNETMASK:
3538 case SIOCSIFNETMASK:
3539 case SIOCSIFPFLAGS:
3540 case SIOCGIFPFLAGS:
3541 case SIOCGIFTXQLEN:
3542 case SIOCSIFTXQLEN:
3543 case SIOCBRADDIF:
3544 case SIOCBRDELIF:
Johannes Bergc6c9fee2019-01-25 22:43:19 +01003545 case SIOCGIFNAME:
Arnd Bergmann9177efd2009-11-06 08:09:09 +00003546 case SIOCSIFNAME:
3547 case SIOCGMIIPHY:
3548 case SIOCGMIIREG:
3549 case SIOCSMIIREG:
Al Virof92d4fc2017-09-30 19:32:17 -04003550 case SIOCBONDENSLAVE:
3551 case SIOCBONDRELEASE:
3552 case SIOCBONDSETHWADDR:
3553 case SIOCBONDCHANGEACTIVE:
Johannes Berg37ac39b2019-01-25 22:43:18 +01003554 return compat_ifreq_ioctl(net, sock, cmd, argp);
3555
Arnd Bergmann6b960182009-11-06 23:10:54 -08003556 case SIOCSARP:
3557 case SIOCGARP:
3558 case SIOCDARP:
Arnd Bergmannc7dc5042019-06-03 23:07:12 +02003559 case SIOCOUTQ:
Arnd Bergmann9d7bf412019-06-03 23:06:00 +02003560 case SIOCOUTQNSD:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003561 case SIOCATMARK:
Johannes Berg63ff03a2019-01-25 22:43:17 +01003562 return sock_do_ioctl(net, sock, cmd, arg);
Arnd Bergmann9177efd2009-11-06 08:09:09 +00003563 }
3564
Arnd Bergmann6b960182009-11-06 23:10:54 -08003565 return -ENOIOCTLCMD;
3566}
Arnd Bergmann7a229382009-11-06 23:00:29 -08003567
Eric Dumazet95c96172012-04-15 05:58:06 +00003568static long compat_sock_ioctl(struct file *file, unsigned int cmd,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07003569 unsigned long arg)
Shaun Pereira89bbfc92006-03-21 23:58:08 -08003570{
3571 struct socket *sock = file->private_data;
3572 int ret = -ENOIOCTLCMD;
David S. Miller87de87d2008-06-03 09:14:03 -07003573 struct sock *sk;
3574 struct net *net;
3575
3576 sk = sock->sk;
3577 net = sock_net(sk);
Shaun Pereira89bbfc92006-03-21 23:58:08 -08003578
3579 if (sock->ops->compat_ioctl)
3580 ret = sock->ops->compat_ioctl(sock, cmd, arg);
3581
David S. Miller87de87d2008-06-03 09:14:03 -07003582 if (ret == -ENOIOCTLCMD &&
3583 (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST))
3584 ret = compat_wext_handle_ioctl(net, cmd, arg);
3585
Arnd Bergmann6b960182009-11-06 23:10:54 -08003586 if (ret == -ENOIOCTLCMD)
3587 ret = compat_sock_ioctl_trans(file, sock, cmd, arg);
3588
Shaun Pereira89bbfc92006-03-21 23:58:08 -08003589 return ret;
3590}
3591#endif
3592
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003593/**
3594 * kernel_bind - bind an address to a socket (kernel space)
3595 * @sock: socket
3596 * @addr: address
3597 * @addrlen: length of address
3598 *
3599 * Returns 0 or an error.
3600 */
3601
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003602int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen)
3603{
3604 return sock->ops->bind(sock, addr, addrlen);
3605}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003606EXPORT_SYMBOL(kernel_bind);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003607
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003608/**
3609 * kernel_listen - move socket to listening state (kernel space)
3610 * @sock: socket
3611 * @backlog: pending connections queue size
3612 *
3613 * Returns 0 or an error.
3614 */
3615
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003616int kernel_listen(struct socket *sock, int backlog)
3617{
3618 return sock->ops->listen(sock, backlog);
3619}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003620EXPORT_SYMBOL(kernel_listen);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003621
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003622/**
3623 * kernel_accept - accept a connection (kernel space)
3624 * @sock: listening socket
3625 * @newsock: new connected socket
3626 * @flags: flags
3627 *
3628 * @flags must be SOCK_CLOEXEC, SOCK_NONBLOCK or 0.
3629 * If it fails, @newsock is guaranteed to be %NULL.
3630 * Returns 0 or an error.
3631 */
3632
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003633int kernel_accept(struct socket *sock, struct socket **newsock, int flags)
3634{
3635 struct sock *sk = sock->sk;
3636 int err;
3637
3638 err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
3639 newsock);
3640 if (err < 0)
3641 goto done;
3642
David Howellscdfbabf2017-03-09 08:09:05 +00003643 err = sock->ops->accept(sock, *newsock, flags, true);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003644 if (err < 0) {
3645 sock_release(*newsock);
Tony Battersbyfa8705b2007-10-10 21:09:04 -07003646 *newsock = NULL;
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003647 goto done;
3648 }
3649
3650 (*newsock)->ops = sock->ops;
Wei Yongjun1b085342008-12-18 19:35:10 -08003651 __module_get((*newsock)->ops->owner);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003652
3653done:
3654 return err;
3655}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003656EXPORT_SYMBOL(kernel_accept);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003657
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003658/**
3659 * kernel_connect - connect a socket (kernel space)
3660 * @sock: socket
3661 * @addr: address
3662 * @addrlen: address length
3663 * @flags: flags (O_NONBLOCK, ...)
3664 *
3665 * For datagram sockets, @addr is the addres to which datagrams are sent
3666 * by default, and the only address from which datagrams are received.
3667 * For stream sockets, attempts to connect to @addr.
3668 * Returns 0 or an error code.
3669 */
3670
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003671int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
YOSHIFUJI Hideaki4768fbc2007-02-09 23:25:31 +09003672 int flags)
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003673{
3674 return sock->ops->connect(sock, addr, addrlen, flags);
3675}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003676EXPORT_SYMBOL(kernel_connect);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003677
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003678/**
3679 * kernel_getsockname - get the address which the socket is bound (kernel space)
3680 * @sock: socket
3681 * @addr: address holder
3682 *
3683 * Fills the @addr pointer with the address which the socket is bound.
3684 * Returns 0 or an error code.
3685 */
3686
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01003687int kernel_getsockname(struct socket *sock, struct sockaddr *addr)
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003688{
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01003689 return sock->ops->getname(sock, addr, 0);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003690}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003691EXPORT_SYMBOL(kernel_getsockname);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003692
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003693/**
3694 * kernel_peername - get the address which the socket is connected (kernel space)
3695 * @sock: socket
3696 * @addr: address holder
3697 *
3698 * Fills the @addr pointer with the address which the socket is connected.
3699 * Returns 0 or an error code.
3700 */
3701
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01003702int kernel_getpeername(struct socket *sock, struct sockaddr *addr)
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003703{
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01003704 return sock->ops->getname(sock, addr, 1);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003705}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003706EXPORT_SYMBOL(kernel_getpeername);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003707
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003708/**
3709 * kernel_getsockopt - get a socket option (kernel space)
3710 * @sock: socket
3711 * @level: API level (SOL_SOCKET, ...)
3712 * @optname: option tag
3713 * @optval: option value
3714 * @optlen: option length
3715 *
3716 * Assigns the option length to @optlen.
3717 * Returns 0 or an error.
3718 */
3719
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003720int kernel_getsockopt(struct socket *sock, int level, int optname,
3721 char *optval, int *optlen)
3722{
3723 mm_segment_t oldfs = get_fs();
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003724 char __user *uoptval;
3725 int __user *uoptlen;
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003726 int err;
3727
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003728 uoptval = (char __user __force *) optval;
3729 uoptlen = (int __user __force *) optlen;
3730
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003731 set_fs(KERNEL_DS);
3732 if (level == SOL_SOCKET)
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003733 err = sock_getsockopt(sock, level, optname, uoptval, uoptlen);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003734 else
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003735 err = sock->ops->getsockopt(sock, level, optname, uoptval,
3736 uoptlen);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003737 set_fs(oldfs);
3738 return err;
3739}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003740EXPORT_SYMBOL(kernel_getsockopt);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003741
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003742/**
3743 * kernel_setsockopt - set a socket option (kernel space)
3744 * @sock: socket
3745 * @level: API level (SOL_SOCKET, ...)
3746 * @optname: option tag
3747 * @optval: option value
3748 * @optlen: option length
3749 *
3750 * Returns 0 or an error.
3751 */
3752
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003753int kernel_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07003754 char *optval, unsigned int optlen)
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003755{
3756 mm_segment_t oldfs = get_fs();
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003757 char __user *uoptval;
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003758 int err;
3759
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003760 uoptval = (char __user __force *) optval;
3761
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003762 set_fs(KERNEL_DS);
3763 if (level == SOL_SOCKET)
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003764 err = sock_setsockopt(sock, level, optname, uoptval, optlen);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003765 else
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003766 err = sock->ops->setsockopt(sock, level, optname, uoptval,
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003767 optlen);
3768 set_fs(oldfs);
3769 return err;
3770}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003771EXPORT_SYMBOL(kernel_setsockopt);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003772
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003773/**
3774 * kernel_sendpage - send a &page through a socket (kernel space)
3775 * @sock: socket
3776 * @page: page
3777 * @offset: page offset
3778 * @size: total size in bytes
3779 * @flags: flags (MSG_DONTWAIT, ...)
3780 *
3781 * Returns the total amount sent in bytes or an error.
3782 */
3783
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003784int kernel_sendpage(struct socket *sock, struct page *page, int offset,
3785 size_t size, int flags)
3786{
3787 if (sock->ops->sendpage)
3788 return sock->ops->sendpage(sock, page, offset, size, flags);
3789
3790 return sock_no_sendpage(sock, page, offset, size, flags);
3791}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003792EXPORT_SYMBOL(kernel_sendpage);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003793
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003794/**
3795 * kernel_sendpage_locked - send a &page through the locked sock (kernel space)
3796 * @sk: sock
3797 * @page: page
3798 * @offset: page offset
3799 * @size: total size in bytes
3800 * @flags: flags (MSG_DONTWAIT, ...)
3801 *
3802 * Returns the total amount sent in bytes or an error.
3803 * Caller must hold @sk.
3804 */
3805
Tom Herbert306b13e2017-07-28 16:22:41 -07003806int kernel_sendpage_locked(struct sock *sk, struct page *page, int offset,
3807 size_t size, int flags)
3808{
3809 struct socket *sock = sk->sk_socket;
3810
3811 if (sock->ops->sendpage_locked)
3812 return sock->ops->sendpage_locked(sk, page, offset, size,
3813 flags);
3814
3815 return sock_no_sendpage_locked(sk, page, offset, size, flags);
3816}
3817EXPORT_SYMBOL(kernel_sendpage_locked);
3818
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003819/**
3820 * kernel_shutdown - shut down part of a full-duplex connection (kernel space)
3821 * @sock: socket
3822 * @how: connection part
3823 *
3824 * Returns 0 or an error.
3825 */
3826
Trond Myklebust91cf45f2007-11-12 18:10:39 -08003827int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how)
3828{
3829 return sock->ops->shutdown(sock, how);
3830}
Trond Myklebust91cf45f2007-11-12 18:10:39 -08003831EXPORT_SYMBOL(kernel_sock_shutdown);
R. Parameswaran113c3072017-04-05 16:50:35 -07003832
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003833/**
3834 * kernel_sock_ip_overhead - returns the IP overhead imposed by a socket
3835 * @sk: socket
3836 *
3837 * This routine returns the IP overhead imposed by a socket i.e.
3838 * the length of the underlying IP header, depending on whether
3839 * this is an IPv4 or IPv6 socket and the length from IP options turned
3840 * on at the socket. Assumes that the caller has a lock on the socket.
R. Parameswaran113c3072017-04-05 16:50:35 -07003841 */
Pedro Tammela8a3c2452019-03-14 10:45:23 -03003842
R. Parameswaran113c3072017-04-05 16:50:35 -07003843u32 kernel_sock_ip_overhead(struct sock *sk)
3844{
3845 struct inet_sock *inet;
3846 struct ip_options_rcu *opt;
3847 u32 overhead = 0;
R. Parameswaran113c3072017-04-05 16:50:35 -07003848#if IS_ENABLED(CONFIG_IPV6)
3849 struct ipv6_pinfo *np;
3850 struct ipv6_txoptions *optv6 = NULL;
3851#endif /* IS_ENABLED(CONFIG_IPV6) */
3852
3853 if (!sk)
3854 return overhead;
3855
R. Parameswaran113c3072017-04-05 16:50:35 -07003856 switch (sk->sk_family) {
3857 case AF_INET:
3858 inet = inet_sk(sk);
3859 overhead += sizeof(struct iphdr);
3860 opt = rcu_dereference_protected(inet->inet_opt,
stephen hemminger614d79c2017-07-24 10:25:22 -07003861 sock_owned_by_user(sk));
R. Parameswaran113c3072017-04-05 16:50:35 -07003862 if (opt)
3863 overhead += opt->opt.optlen;
3864 return overhead;
3865#if IS_ENABLED(CONFIG_IPV6)
3866 case AF_INET6:
3867 np = inet6_sk(sk);
3868 overhead += sizeof(struct ipv6hdr);
3869 if (np)
3870 optv6 = rcu_dereference_protected(np->opt,
stephen hemminger614d79c2017-07-24 10:25:22 -07003871 sock_owned_by_user(sk));
R. Parameswaran113c3072017-04-05 16:50:35 -07003872 if (optv6)
3873 overhead += (optv6->opt_flen + optv6->opt_nflen);
3874 return overhead;
3875#endif /* IS_ENABLED(CONFIG_IPV6) */
3876 default: /* Returns 0 overhead if the socket is not ipv4 or ipv6 */
3877 return overhead;
3878 }
3879}
3880EXPORT_SYMBOL(kernel_sock_ip_overhead);