blob: 58353835497d9cebb139d276a383af858c867e8e [file] [log] [blame]
Amit Kushwaha846cc122016-12-08 18:21:53 +05301
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 *
49 *
50 * This program is free software; you can redistribute it and/or
51 * modify it under the terms of the GNU General Public License
52 * as published by the Free Software Foundation; either version
53 * 2 of the License, or (at your option) any later version.
54 *
55 *
56 * This module is effectively the top level interface to the BSD socket
Stephen Hemminger89bddce2006-09-01 00:19:31 -070057 * paradigm.
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 *
59 * Based upon Swansea University Computer Society NET3.039
60 */
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <linux/socket.h>
64#include <linux/file.h>
65#include <linux/net.h>
66#include <linux/interrupt.h>
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -070067#include <linux/thread_info.h>
Stephen Hemminger55737fd2006-09-01 00:23:39 -070068#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#include <linux/netdevice.h>
70#include <linux/proc_fs.h>
71#include <linux/seq_file.h>
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080072#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#include <linux/if_bridge.h>
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -030074#include <linux/if_frad.h>
75#include <linux/if_vlan.h>
Daniel Borkmann408eccc2014-04-01 16:20:23 +020076#include <linux/ptp_classify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#include <linux/init.h>
78#include <linux/poll.h>
79#include <linux/cache.h>
80#include <linux/module.h>
81#include <linux/highmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#include <linux/mount.h>
83#include <linux/security.h>
84#include <linux/syscalls.h>
85#include <linux/compat.h>
86#include <linux/kmod.h>
David Woodhouse3ec3b2f2005-05-17 12:08:48 +010087#include <linux/audit.h>
Adrian Bunkd86b5e02006-01-21 00:46:55 +010088#include <linux/wireless.h>
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -070089#include <linux/nsproxy.h>
Nick Black1fd7317d2009-09-22 16:43:33 -070090#include <linux/magic.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090091#include <linux/slab.h>
Masatake YAMATO600e1772012-08-29 10:44:29 +000092#include <linux/xattr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94#include <asm/uaccess.h>
95#include <asm/unistd.h>
96
97#include <net/compat.h>
David S. Miller87de87d2008-06-03 09:14:03 -070098#include <net/wext.h>
Herbert Xuf8451722010-05-24 00:12:34 -070099#include <net/cls_cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101#include <net/sock.h>
102#include <linux/netfilter.h>
103
Arnd Bergmann6b960182009-11-06 23:10:54 -0800104#include <linux/if_tun.h>
105#include <linux/ipv6_route.h>
106#include <linux/route.h>
Arnd Bergmann6b960182009-11-06 23:10:54 -0800107#include <linux/sockios.h>
108#include <linux/atalk.h>
Eliezer Tamir076bb0c2013-07-10 17:13:17 +0300109#include <net/busy_poll.h>
Willem de Bruijnf24b9be2014-08-04 22:11:45 -0400110#include <linux/errqueue.h>
Eliezer Tamir06021292013-06-10 11:39:50 +0300111
Cong Wange0d10952013-08-01 11:10:25 +0800112#ifdef CONFIG_NET_RX_BUSY_POLL
Eliezer Tamir64b0dc52013-07-10 17:13:36 +0300113unsigned int sysctl_net_busy_read __read_mostly;
114unsigned int sysctl_net_busy_poll __read_mostly;
Eliezer Tamir06021292013-06-10 11:39:50 +0300115#endif
Arnd Bergmann6b960182009-11-06 23:10:54 -0800116
Al Viro8ae5e0302014-11-28 19:40:50 -0500117static ssize_t sock_read_iter(struct kiocb *iocb, struct iov_iter *to);
118static ssize_t sock_write_iter(struct kiocb *iocb, struct iov_iter *from);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700119static int sock_mmap(struct file *file, struct vm_area_struct *vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121static int sock_close(struct inode *inode, struct file *file);
122static unsigned int sock_poll(struct file *file,
123 struct poll_table_struct *wait);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700124static long sock_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
Shaun Pereira89bbfc92006-03-21 23:58:08 -0800125#ifdef CONFIG_COMPAT
126static long compat_sock_ioctl(struct file *file,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700127 unsigned int cmd, unsigned long arg);
Shaun Pereira89bbfc92006-03-21 23:58:08 -0800128#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129static int sock_fasync(int fd, struct file *filp, int on);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130static ssize_t sock_sendpage(struct file *file, struct page *page,
131 int offset, size_t size, loff_t *ppos, int more);
Jens Axboe9c55e012007-11-06 23:30:13 -0800132static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700133 struct pipe_inode_info *pipe, size_t len,
Jens Axboe9c55e012007-11-06 23:30:13 -0800134 unsigned int flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136/*
137 * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
138 * in the operation structures but are done directly via the socketcall() multiplexor.
139 */
140
Arjan van de Venda7071d2007-02-12 00:55:36 -0800141static const struct file_operations socket_file_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 .owner = THIS_MODULE,
143 .llseek = no_llseek,
Al Viro8ae5e0302014-11-28 19:40:50 -0500144 .read_iter = sock_read_iter,
145 .write_iter = sock_write_iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 .poll = sock_poll,
147 .unlocked_ioctl = sock_ioctl,
Shaun Pereira89bbfc92006-03-21 23:58:08 -0800148#ifdef CONFIG_COMPAT
149 .compat_ioctl = compat_sock_ioctl,
150#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 .mmap = sock_mmap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 .release = sock_close,
153 .fasync = sock_fasync,
Jens Axboe5274f052006-03-30 15:15:30 +0200154 .sendpage = sock_sendpage,
155 .splice_write = generic_splice_sendpage,
Jens Axboe9c55e012007-11-06 23:30:13 -0800156 .splice_read = sock_splice_read,
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/*
167 * Statistics counters of the socket lists
168 */
169
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700170static DEFINE_PER_CPU(int, sockets_in_use);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700173 * Support routines.
174 * Move socket addresses back and forth across the kernel/user
175 * divide and look after the messy bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 */
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178/**
179 * move_addr_to_kernel - copy a socket address into kernel space
180 * @uaddr: Address in user space
181 * @kaddr: Address in kernel space
182 * @ulen: Length in user space
183 *
184 * The address is copied into kernel space. If the provided address is
185 * too long an error code of -EINVAL is returned. If the copy gives
186 * invalid addresses -EFAULT is returned. On a success 0 is returned.
187 */
188
Maciej Żenczykowski43db3622012-03-11 12:51:50 +0000189int move_addr_to_kernel(void __user *uaddr, int ulen, struct sockaddr_storage *kaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -0700191 if (ulen < 0 || ulen > sizeof(struct sockaddr_storage))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700193 if (ulen == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 return 0;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700195 if (copy_from_user(kaddr, uaddr, ulen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 return -EFAULT;
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100197 return audit_sockaddr(ulen, kaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
199
200/**
201 * move_addr_to_user - copy an address to user space
202 * @kaddr: kernel space address
203 * @klen: length of address in kernel
204 * @uaddr: user space address
205 * @ulen: pointer to user length field
206 *
207 * The value pointed to by ulen on entry is the buffer length available.
208 * This is overwritten with the buffer space used. -EINVAL is returned
209 * if an overlong buffer is specified or a negative buffer size. -EFAULT
210 * is returned if either the buffer or the length field are not
211 * accessible.
212 * After copying the data up to the limit the user specifies, the true
213 * length of the data is written over the length limit the user
214 * specified. Zero is returned for a success.
215 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700216
Maciej Żenczykowski43db3622012-03-11 12:51:50 +0000217static int move_addr_to_user(struct sockaddr_storage *kaddr, int klen,
stephen hemminger11165f12010-10-18 14:27:29 +0000218 void __user *uaddr, int __user *ulen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
220 int err;
221 int len;
222
Hannes Frederic Sowa68c6beb2013-11-21 03:14:34 +0100223 BUG_ON(klen > sizeof(struct sockaddr_storage));
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700224 err = get_user(len, ulen);
225 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 return err;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700227 if (len > klen)
228 len = klen;
Hannes Frederic Sowa68c6beb2013-11-21 03:14:34 +0100229 if (len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700231 if (len) {
Steve Grubbd6fe3942006-03-30 12:20:22 -0500232 if (audit_sockaddr(klen, kaddr))
233 return -ENOMEM;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700234 if (copy_to_user(uaddr, kaddr, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 return -EFAULT;
236 }
237 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700238 * "fromlen shall refer to the value before truncation.."
239 * 1003.1g
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 */
241 return __put_user(klen, ulen);
242}
243
Christoph Lametere18b8902006-12-06 20:33:20 -0800244static struct kmem_cache *sock_inode_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246static struct inode *sock_alloc_inode(struct super_block *sb)
247{
248 struct socket_alloc *ei;
Eric Dumazeteaefd112011-02-18 03:26:36 +0000249 struct socket_wq *wq;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700250
Christoph Lametere94b1762006-12-06 20:33:17 -0800251 ei = kmem_cache_alloc(sock_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 if (!ei)
253 return NULL;
Eric Dumazeteaefd112011-02-18 03:26:36 +0000254 wq = kmalloc(sizeof(*wq), GFP_KERNEL);
255 if (!wq) {
Eric Dumazet43815482010-04-29 11:01:49 +0000256 kmem_cache_free(sock_inode_cachep, ei);
257 return NULL;
258 }
Eric Dumazeteaefd112011-02-18 03:26:36 +0000259 init_waitqueue_head(&wq->wait);
260 wq->fasync_list = NULL;
Nicolai Stange574aab12015-12-29 13:29:55 +0100261 wq->flags = 0;
Eric Dumazeteaefd112011-02-18 03:26:36 +0000262 RCU_INIT_POINTER(ei->socket.wq, wq);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 ei->socket.state = SS_UNCONNECTED;
265 ei->socket.flags = 0;
266 ei->socket.ops = NULL;
267 ei->socket.sk = NULL;
268 ei->socket.file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 return &ei->vfs_inode;
271}
272
273static void sock_destroy_inode(struct inode *inode)
274{
Eric Dumazet43815482010-04-29 11:01:49 +0000275 struct socket_alloc *ei;
Eric Dumazeteaefd112011-02-18 03:26:36 +0000276 struct socket_wq *wq;
Eric Dumazet43815482010-04-29 11:01:49 +0000277
278 ei = container_of(inode, struct socket_alloc, vfs_inode);
Eric Dumazeteaefd112011-02-18 03:26:36 +0000279 wq = rcu_dereference_protected(ei->socket.wq, 1);
Lai Jiangshan61845222011-03-18 12:10:25 +0800280 kfree_rcu(wq, rcu);
Eric Dumazet43815482010-04-29 11:01:49 +0000281 kmem_cache_free(sock_inode_cachep, ei);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282}
283
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700284static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700286 struct socket_alloc *ei = (struct socket_alloc *)foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Christoph Lametera35afb82007-05-16 22:10:57 -0700288 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289}
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291static int init_inodecache(void)
292{
293 sock_inode_cachep = kmem_cache_create("sock_inode_cache",
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700294 sizeof(struct socket_alloc),
295 0,
296 (SLAB_HWCACHE_ALIGN |
297 SLAB_RECLAIM_ACCOUNT |
Vladimir Davydov5d097052016-01-14 15:18:21 -0800298 SLAB_MEM_SPREAD | SLAB_ACCOUNT),
Paul Mundt20c2df82007-07-20 10:11:58 +0900299 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 if (sock_inode_cachep == NULL)
301 return -ENOMEM;
302 return 0;
303}
304
Alexey Dobriyanb87221d2009-09-21 17:01:09 -0700305static const struct super_operations sockfs_ops = {
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700306 .alloc_inode = sock_alloc_inode,
307 .destroy_inode = sock_destroy_inode,
308 .statfs = simple_statfs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309};
310
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700311/*
312 * sockfs_dname() is called from d_path().
313 */
314static char *sockfs_dname(struct dentry *dentry, char *buffer, int buflen)
315{
316 return dynamic_dname(dentry, buffer, buflen, "socket:[%lu]",
David Howellsc5ef6032015-03-17 22:26:16 +0000317 d_inode(dentry)->i_ino);
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700318}
319
Al Viro3ba13d12009-02-20 06:02:22 +0000320static const struct dentry_operations sockfs_dentry_operations = {
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700321 .d_dname = sockfs_dname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322};
323
Andreas Gruenbacherbba0bd32016-09-29 17:48:35 +0200324static int sockfs_xattr_get(const struct xattr_handler *handler,
325 struct dentry *dentry, struct inode *inode,
326 const char *suffix, void *value, size_t size)
327{
328 if (value) {
329 if (dentry->d_name.len + 1 > size)
330 return -ERANGE;
331 memcpy(value, dentry->d_name.name, dentry->d_name.len + 1);
332 }
333 return dentry->d_name.len + 1;
334}
335
336#define XATTR_SOCKPROTONAME_SUFFIX "sockprotoname"
337#define XATTR_NAME_SOCKPROTONAME (XATTR_SYSTEM_PREFIX XATTR_SOCKPROTONAME_SUFFIX)
338#define XATTR_NAME_SOCKPROTONAME_LEN (sizeof(XATTR_NAME_SOCKPROTONAME)-1)
339
340static const struct xattr_handler sockfs_xattr_handler = {
341 .name = XATTR_NAME_SOCKPROTONAME,
342 .get = sockfs_xattr_get,
343};
344
Andreas Gruenbacher4a590152016-11-13 21:23:34 +0100345static int sockfs_security_xattr_set(const struct xattr_handler *handler,
346 struct dentry *dentry, struct inode *inode,
347 const char *suffix, const void *value,
348 size_t size, int flags)
349{
350 /* Handled by LSM. */
351 return -EAGAIN;
352}
353
354static const struct xattr_handler sockfs_security_xattr_handler = {
355 .prefix = XATTR_SECURITY_PREFIX,
356 .set = sockfs_security_xattr_set,
357};
358
Andreas Gruenbacherbba0bd32016-09-29 17:48:35 +0200359static const struct xattr_handler *sockfs_xattr_handlers[] = {
360 &sockfs_xattr_handler,
Andreas Gruenbacher4a590152016-11-13 21:23:34 +0100361 &sockfs_security_xattr_handler,
Andreas Gruenbacherbba0bd32016-09-29 17:48:35 +0200362 NULL
363};
364
Al Viroc74a1cb2011-01-12 16:59:34 -0500365static struct dentry *sockfs_mount(struct file_system_type *fs_type,
366 int flags, const char *dev_name, void *data)
367{
Andreas Gruenbacherbba0bd32016-09-29 17:48:35 +0200368 return mount_pseudo_xattr(fs_type, "socket:", &sockfs_ops,
369 sockfs_xattr_handlers,
370 &sockfs_dentry_operations, SOCKFS_MAGIC);
Al Viroc74a1cb2011-01-12 16:59:34 -0500371}
372
373static struct vfsmount *sock_mnt __read_mostly;
374
375static struct file_system_type sock_fs_type = {
376 .name = "sockfs",
377 .mount = sockfs_mount,
378 .kill_sb = kill_anon_super,
379};
380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381/*
382 * Obtains the first available file descriptor and sets it up for use.
383 *
David S. Miller39d8c1b2006-03-20 17:13:49 -0800384 * These functions create file structures and maps them to fd space
385 * of the current process. On success it returns file descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 * and file struct implicitly stored in sock->file.
387 * Note that another thread may close file descriptor before we return
388 * from this function. We use the fact that now we do not refer
389 * to socket after mapping. If one day we will need it, this
390 * function will increment ref. count on file by 1.
391 *
392 * In any case returned fd MAY BE not valid!
393 * This race condition is unavoidable
394 * with shared fd spaces, we cannot solve it inside kernel,
395 * but we take care of internal coherence yet.
396 */
397
Linus Torvaldsaab174f2012-10-02 20:25:04 -0700398struct file *sock_alloc_file(struct socket *sock, int flags, const char *dname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
Al Viro7cbe66b2009-08-05 19:59:08 +0400400 struct qstr name = { .name = "" };
Al Viro2c48b9c2009-08-09 00:52:35 +0400401 struct path path;
Al Viro7cbe66b2009-08-05 19:59:08 +0400402 struct file *file;
David S. Miller39d8c1b2006-03-20 17:13:49 -0800403
Masatake YAMATO600e1772012-08-29 10:44:29 +0000404 if (dname) {
405 name.name = dname;
406 name.len = strlen(name.name);
407 } else if (sock->sk) {
408 name.name = sock->sk->sk_prot_creator->name;
409 name.len = strlen(name.name);
410 }
Nick Piggin4b936882011-01-07 17:50:07 +1100411 path.dentry = d_alloc_pseudo(sock_mnt->mnt_sb, &name);
Al Viro28407632012-08-17 23:54:15 -0400412 if (unlikely(!path.dentry))
413 return ERR_PTR(-ENOMEM);
Al Viro2c48b9c2009-08-09 00:52:35 +0400414 path.mnt = mntget(sock_mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Al Viro2c48b9c2009-08-09 00:52:35 +0400416 d_instantiate(path.dentry, SOCK_INODE(sock));
Al Virocc3808f2009-08-06 09:43:59 +0400417
Al Viro2c48b9c2009-08-09 00:52:35 +0400418 file = alloc_file(&path, FMODE_READ | FMODE_WRITE,
Al Virocc3808f2009-08-06 09:43:59 +0400419 &socket_file_ops);
Viresh Kumarb5ffe632015-08-12 15:59:47 +0530420 if (IS_ERR(file)) {
Al Virocc3808f2009-08-06 09:43:59 +0400421 /* drop dentry, keep inode */
David Howellsc5ef6032015-03-17 22:26:16 +0000422 ihold(d_inode(path.dentry));
Al Viro2c48b9c2009-08-09 00:52:35 +0400423 path_put(&path);
Anatol Pomozov39b65252012-09-12 20:11:55 -0700424 return file;
Al Virocc3808f2009-08-06 09:43:59 +0400425 }
David S. Miller39d8c1b2006-03-20 17:13:49 -0800426
427 sock->file = file;
Ulrich Drepper77d27202008-07-23 21:29:35 -0700428 file->f_flags = O_RDWR | (flags & O_NONBLOCK);
David S. Miller39d8c1b2006-03-20 17:13:49 -0800429 file->private_data = sock;
Al Viro28407632012-08-17 23:54:15 -0400430 return file;
David S. Miller39d8c1b2006-03-20 17:13:49 -0800431}
Al Viro56b31d12012-08-18 00:25:51 -0400432EXPORT_SYMBOL(sock_alloc_file);
David S. Miller39d8c1b2006-03-20 17:13:49 -0800433
Al Viro56b31d12012-08-18 00:25:51 -0400434static int sock_map_fd(struct socket *sock, int flags)
David S. Miller39d8c1b2006-03-20 17:13:49 -0800435{
436 struct file *newfile;
Al Viro28407632012-08-17 23:54:15 -0400437 int fd = get_unused_fd_flags(flags);
438 if (unlikely(fd < 0))
439 return fd;
David S. Miller39d8c1b2006-03-20 17:13:49 -0800440
Linus Torvaldsaab174f2012-10-02 20:25:04 -0700441 newfile = sock_alloc_file(sock, flags, NULL);
Al Viro28407632012-08-17 23:54:15 -0400442 if (likely(!IS_ERR(newfile))) {
David S. Miller39d8c1b2006-03-20 17:13:49 -0800443 fd_install(fd, newfile);
Al Viro28407632012-08-17 23:54:15 -0400444 return fd;
445 }
Al Viro7cbe66b2009-08-05 19:59:08 +0400446
Al Viro28407632012-08-17 23:54:15 -0400447 put_unused_fd(fd);
448 return PTR_ERR(newfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449}
450
John Fastabend406a3c62012-07-20 10:39:25 +0000451struct socket *sock_from_file(struct file *file, int *err)
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800452{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800453 if (file->f_op == &socket_file_ops)
454 return file->private_data; /* set in sock_map_fd */
455
Eric Dumazet23bb80d2007-02-08 14:59:57 -0800456 *err = -ENOTSOCK;
457 return NULL;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800458}
John Fastabend406a3c62012-07-20 10:39:25 +0000459EXPORT_SYMBOL(sock_from_file);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461/**
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700462 * sockfd_lookup - Go from a file number to its socket slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 * @fd: file handle
464 * @err: pointer to an error code return
465 *
466 * The file handle passed in is locked and the socket it is bound
467 * too is returned. If an error occurs the err pointer is overwritten
468 * with a negative errno code and NULL is returned. The function checks
469 * for both invalid handles and passing a handle which is not a socket.
470 *
471 * On a success the socket object pointer is returned.
472 */
473
474struct socket *sockfd_lookup(int fd, int *err)
475{
476 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 struct socket *sock;
478
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700479 file = fget(fd);
480 if (!file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 *err = -EBADF;
482 return NULL;
483 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700484
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800485 sock = sock_from_file(file, err);
486 if (!sock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 fput(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 return sock;
489}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700490EXPORT_SYMBOL(sockfd_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800492static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed)
493{
Al Viro00e188e2014-03-03 23:48:18 -0500494 struct fd f = fdget(fd);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800495 struct socket *sock;
496
Hua Zhong36725582006-04-19 15:25:02 -0700497 *err = -EBADF;
Al Viro00e188e2014-03-03 23:48:18 -0500498 if (f.file) {
499 sock = sock_from_file(f.file, err);
500 if (likely(sock)) {
501 *fput_needed = f.flags;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800502 return sock;
Al Viro00e188e2014-03-03 23:48:18 -0500503 }
504 fdput(f);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800505 }
506 return NULL;
507}
508
Masatake YAMATO600e1772012-08-29 10:44:29 +0000509static ssize_t sockfs_listxattr(struct dentry *dentry, char *buffer,
510 size_t size)
511{
512 ssize_t len;
513 ssize_t used = 0;
514
David Howellsc5ef6032015-03-17 22:26:16 +0000515 len = security_inode_listsecurity(d_inode(dentry), buffer, size);
Masatake YAMATO600e1772012-08-29 10:44:29 +0000516 if (len < 0)
517 return len;
518 used += len;
519 if (buffer) {
520 if (size < used)
521 return -ERANGE;
522 buffer += len;
523 }
524
525 len = (XATTR_NAME_SOCKPROTONAME_LEN + 1);
526 used += len;
527 if (buffer) {
528 if (size < used)
529 return -ERANGE;
530 memcpy(buffer, XATTR_NAME_SOCKPROTONAME, len);
531 buffer += len;
532 }
533
534 return used;
535}
536
Lorenzo Colitti86741ec2016-11-04 02:23:41 +0900537int sockfs_setattr(struct dentry *dentry, struct iattr *iattr)
538{
539 int err = simple_setattr(dentry, iattr);
540
541 if (!err) {
542 struct socket *sock = SOCKET_I(d_inode(dentry));
543
544 sock->sk->sk_uid = iattr->ia_uid;
545 }
546
547 return err;
548}
549
Masatake YAMATO600e1772012-08-29 10:44:29 +0000550static const struct inode_operations sockfs_inode_ops = {
Masatake YAMATO600e1772012-08-29 10:44:29 +0000551 .listxattr = sockfs_listxattr,
Lorenzo Colitti86741ec2016-11-04 02:23:41 +0900552 .setattr = sockfs_setattr,
Masatake YAMATO600e1772012-08-29 10:44:29 +0000553};
554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555/**
556 * sock_alloc - allocate a socket
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700557 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 * Allocate a new inode and socket object. The two are bound together
559 * and initialised. The socket is then returned. If we are out of inodes
560 * NULL is returned.
561 */
562
Tom Herbertf4a00aa2016-03-07 14:11:01 -0800563struct socket *sock_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700565 struct inode *inode;
566 struct socket *sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Eric Dumazeta209dfc2011-07-26 11:36:34 +0200568 inode = new_inode_pseudo(sock_mnt->mnt_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 if (!inode)
570 return NULL;
571
572 sock = SOCKET_I(inode);
573
Eric Dumazet29a020d2009-09-15 02:39:20 -0700574 kmemcheck_annotate_bitfield(sock, type);
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400575 inode->i_ino = get_next_ino();
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700576 inode->i_mode = S_IFSOCK | S_IRWXUGO;
David Howells8192b0c2008-11-14 10:39:10 +1100577 inode->i_uid = current_fsuid();
578 inode->i_gid = current_fsgid();
Masatake YAMATO600e1772012-08-29 10:44:29 +0000579 inode->i_op = &sockfs_inode_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Alex Shi19e8d692012-05-14 14:15:31 -0700581 this_cpu_add(sockets_in_use, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 return sock;
583}
Tom Herbertf4a00aa2016-03-07 14:11:01 -0800584EXPORT_SYMBOL(sock_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586/**
587 * sock_release - close a socket
588 * @sock: socket to close
589 *
590 * The socket is released from the protocol stack if it has a release
591 * callback, and the inode is then released if the socket is bound to
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700592 * an inode not a file.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595void sock_release(struct socket *sock)
596{
597 if (sock->ops) {
598 struct module *owner = sock->ops->owner;
599
600 sock->ops->release(sock);
601 sock->ops = NULL;
602 module_put(owner);
603 }
604
Eric Dumazeteaefd112011-02-18 03:26:36 +0000605 if (rcu_dereference_protected(sock->wq, 1)->fasync_list)
Yang Yingliang3410f222014-02-12 17:09:55 +0800606 pr_err("%s: fasync list not empty!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Alex Shi19e8d692012-05-14 14:15:31 -0700608 this_cpu_sub(sockets_in_use, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 if (!sock->file) {
610 iput(SOCK_INODE(sock));
611 return;
612 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700613 sock->file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700615EXPORT_SYMBOL(sock_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -0400617void __sock_tx_timestamp(__u16 tsflags, __u8 *tx_flags)
Patrick Ohly20d49472009-02-12 05:03:38 +0000618{
Eric Dumazet140c55d2014-08-06 11:49:29 +0200619 u8 flags = *tx_flags;
620
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -0400621 if (tsflags & SOF_TIMESTAMPING_TX_HARDWARE)
Eric Dumazet140c55d2014-08-06 11:49:29 +0200622 flags |= SKBTX_HW_TSTAMP;
623
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -0400624 if (tsflags & SOF_TIMESTAMPING_TX_SOFTWARE)
Eric Dumazet140c55d2014-08-06 11:49:29 +0200625 flags |= SKBTX_SW_TSTAMP;
626
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -0400627 if (tsflags & SOF_TIMESTAMPING_TX_SCHED)
Eric Dumazet140c55d2014-08-06 11:49:29 +0200628 flags |= SKBTX_SCHED_TSTAMP;
629
Eric Dumazet140c55d2014-08-06 11:49:29 +0200630 *tx_flags = flags;
Patrick Ohly20d49472009-02-12 05:03:38 +0000631}
Willem de Bruijn67cc0d42014-09-08 19:58:58 -0400632EXPORT_SYMBOL(__sock_tx_timestamp);
Patrick Ohly20d49472009-02-12 05:03:38 +0000633
Al Virod8725c82014-12-11 00:02:50 -0500634static inline int sock_sendmsg_nosec(struct socket *sock, struct msghdr *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
Al Viro01e97e62014-12-15 21:39:31 -0500636 int ret = sock->ops->sendmsg(sock, msg, msg_data_left(msg));
Al Virod8725c82014-12-11 00:02:50 -0500637 BUG_ON(ret == -EIOCBQUEUED);
638 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639}
Gu Zheng0cf00c62014-12-05 15:14:23 +0800640
Al Virod8725c82014-12-11 00:02:50 -0500641int sock_sendmsg(struct socket *sock, struct msghdr *msg)
Gu Zheng0cf00c62014-12-05 15:14:23 +0800642{
Al Virod8725c82014-12-11 00:02:50 -0500643 int err = security_socket_sendmsg(sock, msg,
Al Viro01e97e62014-12-15 21:39:31 -0500644 msg_data_left(msg));
Ying Xue1b784142015-03-02 15:37:48 +0800645
Al Virod8725c82014-12-11 00:02:50 -0500646 return err ?: sock_sendmsg_nosec(sock, msg);
Gu Zheng0cf00c62014-12-05 15:14:23 +0800647}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700648EXPORT_SYMBOL(sock_sendmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
651 struct kvec *vec, size_t num, size_t size)
652{
Al Viro6aa24812015-03-21 19:56:16 -0400653 iov_iter_kvec(&msg->msg_iter, WRITE | ITER_KVEC, vec, num, size);
Al Virod8725c82014-12-11 00:02:50 -0500654 return sock_sendmsg(sock, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700656EXPORT_SYMBOL(kernel_sendmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700658/*
659 * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
660 */
661void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
662 struct sk_buff *skb)
663{
Patrick Ohly20d49472009-02-12 05:03:38 +0000664 int need_software_tstamp = sock_flag(sk, SOCK_RCVTSTAMP);
Willem de Bruijnf24b9be2014-08-04 22:11:45 -0400665 struct scm_timestamping tss;
Patrick Ohly20d49472009-02-12 05:03:38 +0000666 int empty = 1;
667 struct skb_shared_hwtstamps *shhwtstamps =
668 skb_hwtstamps(skb);
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700669
Patrick Ohly20d49472009-02-12 05:03:38 +0000670 /* Race occurred between timestamp enabling and packet
671 receiving. Fill in the current time for now. */
672 if (need_software_tstamp && skb->tstamp.tv64 == 0)
673 __net_timestamp(skb);
674
675 if (need_software_tstamp) {
676 if (!sock_flag(sk, SOCK_RCVTSTAMPNS)) {
677 struct timeval tv;
678 skb_get_timestamp(skb, &tv);
679 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMP,
680 sizeof(tv), &tv);
681 } else {
Willem de Bruijnf24b9be2014-08-04 22:11:45 -0400682 struct timespec ts;
683 skb_get_timestampns(skb, &ts);
Patrick Ohly20d49472009-02-12 05:03:38 +0000684 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPNS,
Willem de Bruijnf24b9be2014-08-04 22:11:45 -0400685 sizeof(ts), &ts);
Patrick Ohly20d49472009-02-12 05:03:38 +0000686 }
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700687 }
Patrick Ohly20d49472009-02-12 05:03:38 +0000688
Willem de Bruijnf24b9be2014-08-04 22:11:45 -0400689 memset(&tss, 0, sizeof(tss));
Willem de Bruijnc1991052014-09-03 12:01:18 -0400690 if ((sk->sk_tsflags & SOF_TIMESTAMPING_SOFTWARE) &&
Willem de Bruijnf24b9be2014-08-04 22:11:45 -0400691 ktime_to_timespec_cond(skb->tstamp, tss.ts + 0))
Patrick Ohly20d49472009-02-12 05:03:38 +0000692 empty = 0;
Willem de Bruijn4d276eb2014-07-25 18:01:32 -0400693 if (shhwtstamps &&
Willem de Bruijnb9f40e22014-08-04 22:11:46 -0400694 (sk->sk_tsflags & SOF_TIMESTAMPING_RAW_HARDWARE) &&
Willem de Bruijnf24b9be2014-08-04 22:11:45 -0400695 ktime_to_timespec_cond(shhwtstamps->hwtstamp, tss.ts + 2))
Willem de Bruijn4d276eb2014-07-25 18:01:32 -0400696 empty = 0;
Francis Yan1c885802016-11-27 23:07:18 -0800697 if (!empty) {
Patrick Ohly20d49472009-02-12 05:03:38 +0000698 put_cmsg(msg, SOL_SOCKET,
Willem de Bruijnf24b9be2014-08-04 22:11:45 -0400699 SCM_TIMESTAMPING, sizeof(tss), &tss);
Francis Yan1c885802016-11-27 23:07:18 -0800700
701 if (skb->len && (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_STATS))
702 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPING_OPT_STATS,
703 skb->len, skb->data);
704 }
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700705}
Arnaldo Carvalho de Melo7c81fd82007-03-10 00:39:35 -0300706EXPORT_SYMBOL_GPL(__sock_recv_timestamp);
707
Johannes Berg6e3e9392011-11-09 10:15:42 +0100708void __sock_recv_wifi_status(struct msghdr *msg, struct sock *sk,
709 struct sk_buff *skb)
710{
711 int ack;
712
713 if (!sock_flag(sk, SOCK_WIFI_STATUS))
714 return;
715 if (!skb->wifi_acked_valid)
716 return;
717
718 ack = skb->wifi_acked;
719
720 put_cmsg(msg, SOL_SOCKET, SCM_WIFI_STATUS, sizeof(ack), &ack);
721}
722EXPORT_SYMBOL_GPL(__sock_recv_wifi_status);
723
stephen hemminger11165f12010-10-18 14:27:29 +0000724static inline void sock_recv_drops(struct msghdr *msg, struct sock *sk,
725 struct sk_buff *skb)
Neil Horman3b885782009-10-12 13:26:31 -0700726{
Eyal Birger744d5a32015-03-01 14:58:31 +0200727 if (sock_flag(sk, SOCK_RXQ_OVFL) && skb && SOCK_SKB_CB(skb)->dropcount)
Neil Horman3b885782009-10-12 13:26:31 -0700728 put_cmsg(msg, SOL_SOCKET, SO_RXQ_OVFL,
Eyal Birger744d5a32015-03-01 14:58:31 +0200729 sizeof(__u32), &SOCK_SKB_CB(skb)->dropcount);
Neil Horman3b885782009-10-12 13:26:31 -0700730}
731
Eric Dumazet767dd032010-04-28 19:14:43 +0000732void __sock_recv_ts_and_drops(struct msghdr *msg, struct sock *sk,
Neil Horman3b885782009-10-12 13:26:31 -0700733 struct sk_buff *skb)
734{
735 sock_recv_timestamp(msg, sk, skb);
736 sock_recv_drops(msg, sk, skb);
737}
Eric Dumazet767dd032010-04-28 19:14:43 +0000738EXPORT_SYMBOL_GPL(__sock_recv_ts_and_drops);
Neil Horman3b885782009-10-12 13:26:31 -0700739
Ying Xue1b784142015-03-02 15:37:48 +0800740static inline int sock_recvmsg_nosec(struct socket *sock, struct msghdr *msg,
Al Viro2da62902015-03-14 21:13:46 -0400741 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
Al Viro2da62902015-03-14 21:13:46 -0400743 return sock->ops->recvmsg(sock, msg, msg_data_left(msg), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744}
745
Al Viro2da62902015-03-14 21:13:46 -0400746int sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -0700747{
Al Viro2da62902015-03-14 21:13:46 -0400748 int err = security_socket_recvmsg(sock, msg, msg_data_left(msg), flags);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -0700749
Al Viro2da62902015-03-14 21:13:46 -0400750 return err ?: sock_recvmsg_nosec(sock, msg, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700752EXPORT_SYMBOL(sock_recvmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Martin Lucinac1249c02010-12-10 00:04:05 +0000754/**
755 * kernel_recvmsg - Receive a message from a socket (kernel space)
756 * @sock: The socket to receive the message from
757 * @msg: Received message
758 * @vec: Input s/g array for message data
759 * @num: Size of input s/g array
760 * @size: Number of bytes to read
761 * @flags: Message flags (MSG_DONTWAIT, etc...)
762 *
763 * On return the msg structure contains the scatter/gather array passed in the
764 * vec argument. The array is modified so that it consists of the unfilled
765 * portion of the original array.
766 *
767 * The returned value is the total number of bytes received, or an error.
768 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700769int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
770 struct kvec *vec, size_t num, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
772 mm_segment_t oldfs = get_fs();
773 int result;
774
Al Viro6aa24812015-03-21 19:56:16 -0400775 iov_iter_kvec(&msg->msg_iter, READ | ITER_KVEC, vec, num, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 set_fs(KERNEL_DS);
Al Viro2da62902015-03-14 21:13:46 -0400777 result = sock_recvmsg(sock, msg, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 set_fs(oldfs);
779 return result;
780}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700781EXPORT_SYMBOL(kernel_recvmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -0300783static ssize_t sock_sendpage(struct file *file, struct page *page,
784 int offset, size_t size, loff_t *ppos, int more)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
786 struct socket *sock;
787 int flags;
788
Eric Dumazetb69aee02005-09-06 14:42:45 -0700789 sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Eric Dumazet35f9c092012-04-05 03:05:35 +0000791 flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
792 /* more is a combination of MSG_MORE and MSG_SENDPAGE_NOTLAST */
793 flags |= more;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Linus Torvaldse6949582009-08-13 08:28:36 -0700795 return kernel_sendpage(sock, page, offset, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796}
797
Jens Axboe9c55e012007-11-06 23:30:13 -0800798static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700799 struct pipe_inode_info *pipe, size_t len,
Jens Axboe9c55e012007-11-06 23:30:13 -0800800 unsigned int flags)
801{
802 struct socket *sock = file->private_data;
803
Rémi Denis-Courmont997b37d2008-02-15 02:35:45 -0800804 if (unlikely(!sock->ops->splice_read))
805 return -EINVAL;
806
Jens Axboe9c55e012007-11-06 23:30:13 -0800807 return sock->ops->splice_read(sock, ppos, pipe, len, flags);
808}
809
Al Viro8ae5e0302014-11-28 19:40:50 -0500810static ssize_t sock_read_iter(struct kiocb *iocb, struct iov_iter *to)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800811{
Al Viro6d652332015-01-30 16:12:56 -0500812 struct file *file = iocb->ki_filp;
813 struct socket *sock = file->private_data;
tadeusz.struk@intel.com0345f932015-03-19 12:31:25 -0700814 struct msghdr msg = {.msg_iter = *to,
815 .msg_iocb = iocb};
Al Viro8ae5e0302014-11-28 19:40:50 -0500816 ssize_t res;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800817
Al Viro8ae5e0302014-11-28 19:40:50 -0500818 if (file->f_flags & O_NONBLOCK)
819 msg.msg_flags = MSG_DONTWAIT;
820
821 if (iocb->ki_pos != 0)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800822 return -ESPIPE;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700823
Christoph Hellwig66ee59a2015-02-11 19:56:46 +0100824 if (!iov_iter_count(to)) /* Match SYS5 behaviour */
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800825 return 0;
826
Al Viro2da62902015-03-14 21:13:46 -0400827 res = sock_recvmsg(sock, &msg, msg.msg_flags);
Al Viro8ae5e0302014-11-28 19:40:50 -0500828 *to = msg.msg_iter;
829 return res;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800830}
831
Al Viro8ae5e0302014-11-28 19:40:50 -0500832static ssize_t sock_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
Al Viro6d652332015-01-30 16:12:56 -0500834 struct file *file = iocb->ki_filp;
835 struct socket *sock = file->private_data;
tadeusz.struk@intel.com0345f932015-03-19 12:31:25 -0700836 struct msghdr msg = {.msg_iter = *from,
837 .msg_iocb = iocb};
Al Viro8ae5e0302014-11-28 19:40:50 -0500838 ssize_t res;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800839
Al Viro8ae5e0302014-11-28 19:40:50 -0500840 if (iocb->ki_pos != 0)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800841 return -ESPIPE;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700842
Al Viro8ae5e0302014-11-28 19:40:50 -0500843 if (file->f_flags & O_NONBLOCK)
844 msg.msg_flags = MSG_DONTWAIT;
845
Al Viro6d652332015-01-30 16:12:56 -0500846 if (sock->type == SOCK_SEQPACKET)
847 msg.msg_flags |= MSG_EOR;
848
Al Virod8725c82014-12-11 00:02:50 -0500849 res = sock_sendmsg(sock, &msg);
Al Viro8ae5e0302014-11-28 19:40:50 -0500850 *from = msg.msg_iter;
851 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852}
853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854/*
855 * Atomic setting of ioctl hooks to avoid race
856 * with module unload.
857 */
858
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800859static DEFINE_MUTEX(br_ioctl_mutex);
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700860static int (*br_ioctl_hook) (struct net *, unsigned int cmd, void __user *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Eric W. Biederman881d9662007-09-17 11:56:21 -0700862void brioctl_set(int (*hook) (struct net *, unsigned int, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800864 mutex_lock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 br_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800866 mutex_unlock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867}
868EXPORT_SYMBOL(brioctl_set);
869
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800870static DEFINE_MUTEX(vlan_ioctl_mutex);
Eric W. Biederman881d9662007-09-17 11:56:21 -0700871static int (*vlan_ioctl_hook) (struct net *, void __user *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Eric W. Biederman881d9662007-09-17 11:56:21 -0700873void vlan_ioctl_set(int (*hook) (struct net *, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800875 mutex_lock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 vlan_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800877 mutex_unlock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878}
879EXPORT_SYMBOL(vlan_ioctl_set);
880
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800881static DEFINE_MUTEX(dlci_ioctl_mutex);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700882static int (*dlci_ioctl_hook) (unsigned int, void __user *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700884void dlci_ioctl_set(int (*hook) (unsigned int, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800886 mutex_lock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 dlci_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800888 mutex_unlock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889}
890EXPORT_SYMBOL(dlci_ioctl_set);
891
Arnd Bergmann6b960182009-11-06 23:10:54 -0800892static long sock_do_ioctl(struct net *net, struct socket *sock,
893 unsigned int cmd, unsigned long arg)
894{
895 int err;
896 void __user *argp = (void __user *)arg;
897
898 err = sock->ops->ioctl(sock, cmd, arg);
899
900 /*
901 * If this ioctl is unknown try to hand it down
902 * to the NIC driver.
903 */
904 if (err == -ENOIOCTLCMD)
905 err = dev_ioctl(net, cmd, argp);
906
907 return err;
908}
909
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910/*
911 * With an ioctl, arg may well be a user mode pointer, but we don't know
912 * what to do with it - that's up to the protocol still.
913 */
914
Andrey Vaginc62cce22016-10-24 18:29:13 -0700915static struct ns_common *get_net_ns(struct ns_common *ns)
916{
917 return &get_net(container_of(ns, struct net, ns))->ns;
918}
919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
921{
922 struct socket *sock;
Eric W. Biederman881d9662007-09-17 11:56:21 -0700923 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 void __user *argp = (void __user *)arg;
925 int pid, err;
Eric W. Biederman881d9662007-09-17 11:56:21 -0700926 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Eric Dumazetb69aee02005-09-06 14:42:45 -0700928 sock = file->private_data;
Eric W. Biederman881d9662007-09-17 11:56:21 -0700929 sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900930 net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
Eric W. Biederman881d9662007-09-17 11:56:21 -0700932 err = dev_ioctl(net, cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 } else
Johannes Berg3d23e342009-09-29 23:27:28 +0200934#ifdef CONFIG_WEXT_CORE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
Eric W. Biederman881d9662007-09-17 11:56:21 -0700936 err = dev_ioctl(net, cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 } else
Johannes Berg3d23e342009-09-29 23:27:28 +0200938#endif
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700939 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 case FIOSETOWN:
941 case SIOCSPGRP:
942 err = -EFAULT;
943 if (get_user(pid, (int __user *)argp))
944 break;
Jeff Laytone0b93ed2014-08-22 11:27:32 -0400945 f_setown(sock->file, pid, 1);
946 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 break;
948 case FIOGETOWN:
949 case SIOCGPGRP:
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700950 err = put_user(f_getown(sock->file),
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700951 (int __user *)argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 break;
953 case SIOCGIFBR:
954 case SIOCSIFBR:
955 case SIOCBRADDBR:
956 case SIOCBRDELBR:
957 err = -ENOPKG;
958 if (!br_ioctl_hook)
959 request_module("bridge");
960
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800961 mutex_lock(&br_ioctl_mutex);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700962 if (br_ioctl_hook)
Eric W. Biederman881d9662007-09-17 11:56:21 -0700963 err = br_ioctl_hook(net, cmd, argp);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800964 mutex_unlock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 break;
966 case SIOCGIFVLAN:
967 case SIOCSIFVLAN:
968 err = -ENOPKG;
969 if (!vlan_ioctl_hook)
970 request_module("8021q");
971
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800972 mutex_lock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 if (vlan_ioctl_hook)
Eric W. Biederman881d9662007-09-17 11:56:21 -0700974 err = vlan_ioctl_hook(net, argp);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800975 mutex_unlock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 case SIOCADDDLCI:
978 case SIOCDELDLCI:
979 err = -ENOPKG;
980 if (!dlci_ioctl_hook)
981 request_module("dlci");
982
Pavel Emelyanov7512cbf2008-03-21 15:58:52 -0700983 mutex_lock(&dlci_ioctl_mutex);
984 if (dlci_ioctl_hook)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 err = dlci_ioctl_hook(cmd, argp);
Pavel Emelyanov7512cbf2008-03-21 15:58:52 -0700986 mutex_unlock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 break;
Andrey Vaginc62cce22016-10-24 18:29:13 -0700988 case SIOCGSKNS:
989 err = -EPERM;
990 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
991 break;
992
993 err = open_related_ns(&net->ns, get_net_ns);
994 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 default:
Arnd Bergmann6b960182009-11-06 23:10:54 -0800996 err = sock_do_ioctl(net, sock, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 break;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700998 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 return err;
1000}
1001
1002int sock_create_lite(int family, int type, int protocol, struct socket **res)
1003{
1004 int err;
1005 struct socket *sock = NULL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001006
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 err = security_socket_create(family, type, protocol, 1);
1008 if (err)
1009 goto out;
1010
1011 sock = sock_alloc();
1012 if (!sock) {
1013 err = -ENOMEM;
1014 goto out;
1015 }
1016
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 sock->type = type;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001018 err = security_socket_post_create(sock, family, type, protocol, 1);
1019 if (err)
1020 goto out_release;
1021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022out:
1023 *res = sock;
1024 return err;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001025out_release:
1026 sock_release(sock);
1027 sock = NULL;
1028 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001030EXPORT_SYMBOL(sock_create_lite);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
1032/* No kernel lock held - perfect */
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001033static unsigned int sock_poll(struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034{
Eliezer Tamircbf55002013-07-08 16:20:34 +03001035 unsigned int busy_flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 struct socket *sock;
1037
1038 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001039 * We can't return errors to poll, so it's either yes or no.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 */
Eric Dumazetb69aee02005-09-06 14:42:45 -07001041 sock = file->private_data;
Eliezer Tamir2d48d672013-06-24 10:28:03 +03001042
Eliezer Tamircbf55002013-07-08 16:20:34 +03001043 if (sk_can_busy_loop(sock->sk)) {
Eliezer Tamir2d48d672013-06-24 10:28:03 +03001044 /* this socket can poll_ll so tell the system call */
Eliezer Tamircbf55002013-07-08 16:20:34 +03001045 busy_flag = POLL_BUSY_LOOP;
Eliezer Tamir2d48d672013-06-24 10:28:03 +03001046
1047 /* once, only if requested by syscall */
Eliezer Tamircbf55002013-07-08 16:20:34 +03001048 if (wait && (wait->_key & POLL_BUSY_LOOP))
1049 sk_busy_loop(sock->sk, 1);
Eliezer Tamir2d48d672013-06-24 10:28:03 +03001050 }
1051
Eliezer Tamircbf55002013-07-08 16:20:34 +03001052 return busy_flag | sock->ops->poll(file, sock, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053}
1054
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001055static int sock_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056{
Eric Dumazetb69aee02005-09-06 14:42:45 -07001057 struct socket *sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
1059 return sock->ops->mmap(file, sock, vma);
1060}
1061
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001062static int sock_close(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 sock_release(SOCKET_I(inode));
1065 return 0;
1066}
1067
1068/*
1069 * Update the socket async list
1070 *
1071 * Fasync_list locking strategy.
1072 *
1073 * 1. fasync_list is modified only under process context socket lock
1074 * i.e. under semaphore.
1075 * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
Eric Dumazet989a2972010-04-14 09:55:35 +00001076 * or under socket lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 */
1078
1079static int sock_fasync(int fd, struct file *filp, int on)
1080{
Eric Dumazet989a2972010-04-14 09:55:35 +00001081 struct socket *sock = filp->private_data;
1082 struct sock *sk = sock->sk;
Eric Dumazeteaefd112011-02-18 03:26:36 +00001083 struct socket_wq *wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Eric Dumazet989a2972010-04-14 09:55:35 +00001085 if (sk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
1088 lock_sock(sk);
Hannes Frederic Sowa1e1d04e2016-04-05 17:10:15 +02001089 wq = rcu_dereference_protected(sock->wq, lockdep_sock_is_held(sk));
Eric Dumazeteaefd112011-02-18 03:26:36 +00001090 fasync_helper(fd, filp, on, &wq->fasync_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Eric Dumazeteaefd112011-02-18 03:26:36 +00001092 if (!wq->fasync_list)
Eric Dumazet989a2972010-04-14 09:55:35 +00001093 sock_reset_flag(sk, SOCK_FASYNC);
Jonathan Corbet76398422009-02-01 14:26:59 -07001094 else
Eric Dumazetbcdce712009-10-06 17:28:29 -07001095 sock_set_flag(sk, SOCK_FASYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Eric Dumazet989a2972010-04-14 09:55:35 +00001097 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 return 0;
1099}
1100
Eric Dumazetceb5d582015-11-29 20:03:11 -08001101/* This function may be called only under rcu_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Eric Dumazetceb5d582015-11-29 20:03:11 -08001103int sock_wake_async(struct socket_wq *wq, int how, int band)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104{
Eric Dumazetceb5d582015-11-29 20:03:11 -08001105 if (!wq || !wq->fasync_list)
1106 return -1;
Eric Dumazet43815482010-04-29 11:01:49 +00001107
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001108 switch (how) {
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001109 case SOCK_WAKE_WAITD:
Eric Dumazetceb5d582015-11-29 20:03:11 -08001110 if (test_bit(SOCKWQ_ASYNC_WAITDATA, &wq->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 break;
1112 goto call_kill;
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001113 case SOCK_WAKE_SPACE:
Eric Dumazetceb5d582015-11-29 20:03:11 -08001114 if (!test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 break;
1116 /* fall through */
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001117 case SOCK_WAKE_IO:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001118call_kill:
Eric Dumazet43815482010-04-29 11:01:49 +00001119 kill_fasync(&wq->fasync_list, SIGIO, band);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 break;
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001121 case SOCK_WAKE_URG:
Eric Dumazet43815482010-04-29 11:01:49 +00001122 kill_fasync(&wq->fasync_list, SIGURG, band);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 }
Eric Dumazetceb5d582015-11-29 20:03:11 -08001124
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 return 0;
1126}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001127EXPORT_SYMBOL(sock_wake_async);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Pavel Emelyanov721db932010-09-29 16:06:32 +04001129int __sock_create(struct net *net, int family, int type, int protocol,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001130 struct socket **res, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131{
1132 int err;
1133 struct socket *sock;
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001134 const struct net_proto_family *pf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
1136 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001137 * Check protocol is in range
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 */
1139 if (family < 0 || family >= NPROTO)
1140 return -EAFNOSUPPORT;
1141 if (type < 0 || type >= SOCK_MAX)
1142 return -EINVAL;
1143
1144 /* Compatibility.
1145
1146 This uglymoron is moved from INET layer to here to avoid
1147 deadlock in module load.
1148 */
1149 if (family == PF_INET && type == SOCK_PACKET) {
liping.zhangf3c98692016-03-11 23:08:36 +08001150 pr_info_once("%s uses obsolete (PF_INET,SOCK_PACKET)\n",
1151 current->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 family = PF_PACKET;
1153 }
1154
1155 err = security_socket_create(family, type, protocol, kern);
1156 if (err)
1157 return err;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001158
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001159 /*
1160 * Allocate the socket and allow the family to set things up. if
1161 * the protocol is 0, the family is instructed to select an appropriate
1162 * default.
1163 */
1164 sock = sock_alloc();
1165 if (!sock) {
Joe Perchese87cc472012-05-13 21:56:26 +00001166 net_warn_ratelimited("socket: no more sockets\n");
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001167 return -ENFILE; /* Not exactly a match, but its the
1168 closest posix thing */
1169 }
1170
1171 sock->type = type;
1172
Johannes Berg95a5afc2008-10-16 15:24:51 -07001173#ifdef CONFIG_MODULES
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001174 /* Attempt to load a protocol module if the find failed.
1175 *
1176 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 * requested real, full-featured networking support upon configuration.
1178 * Otherwise module support will break!
1179 */
Eric Dumazet190683a2010-11-10 10:50:44 +00001180 if (rcu_access_pointer(net_families[family]) == NULL)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001181 request_module("net-pf-%d", family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182#endif
1183
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001184 rcu_read_lock();
1185 pf = rcu_dereference(net_families[family]);
1186 err = -EAFNOSUPPORT;
1187 if (!pf)
1188 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
1190 /*
1191 * We will call the ->create function, that possibly is in a loadable
1192 * module, so we have to bump that loadable module refcnt first.
1193 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001194 if (!try_module_get(pf->owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 goto out_release;
1196
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001197 /* Now protected by module ref count */
1198 rcu_read_unlock();
1199
Eric Paris3f378b62009-11-05 22:18:14 -08001200 err = pf->create(net, sock, protocol, kern);
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001201 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 goto out_module_put;
Frank Filza79af592005-09-27 15:23:38 -07001203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 /*
1205 * Now to bump the refcnt of the [loadable] module that owns this
1206 * socket at sock_release time we decrement its refcnt.
1207 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001208 if (!try_module_get(sock->ops->owner))
1209 goto out_module_busy;
1210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 /*
1212 * Now that we're done with the ->create function, the [loadable]
1213 * module can have its refcnt decremented
1214 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001215 module_put(pf->owner);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001216 err = security_socket_post_create(sock, family, type, protocol, kern);
1217 if (err)
Herbert Xu3b185522007-08-15 14:46:02 -07001218 goto out_sock_release;
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001219 *res = sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001221 return 0;
1222
1223out_module_busy:
1224 err = -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225out_module_put:
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001226 sock->ops = NULL;
1227 module_put(pf->owner);
1228out_sock_release:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 sock_release(sock);
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001230 return err;
1231
1232out_release:
1233 rcu_read_unlock();
1234 goto out_sock_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235}
Pavel Emelyanov721db932010-09-29 16:06:32 +04001236EXPORT_SYMBOL(__sock_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
1238int sock_create(int family, int type, int protocol, struct socket **res)
1239{
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001240 return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001242EXPORT_SYMBOL(sock_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
Eric W. Biedermaneeb1bd52015-05-08 21:08:05 -05001244int sock_create_kern(struct net *net, int family, int type, int protocol, struct socket **res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245{
Eric W. Biedermaneeb1bd52015-05-08 21:08:05 -05001246 return __sock_create(net, family, type, protocol, res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001248EXPORT_SYMBOL(sock_create_kern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001250SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251{
1252 int retval;
1253 struct socket *sock;
Ulrich Dreppera677a032008-07-23 21:29:17 -07001254 int flags;
1255
Ulrich Dreppere38b36f2008-07-23 21:29:42 -07001256 /* Check the SOCK_* constants for consistency. */
1257 BUILD_BUG_ON(SOCK_CLOEXEC != O_CLOEXEC);
1258 BUILD_BUG_ON((SOCK_MAX | SOCK_TYPE_MASK) != SOCK_TYPE_MASK);
1259 BUILD_BUG_ON(SOCK_CLOEXEC & SOCK_TYPE_MASK);
1260 BUILD_BUG_ON(SOCK_NONBLOCK & SOCK_TYPE_MASK);
1261
Ulrich Dreppera677a032008-07-23 21:29:17 -07001262 flags = type & ~SOCK_TYPE_MASK;
Ulrich Drepper77d27202008-07-23 21:29:35 -07001263 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Dreppera677a032008-07-23 21:29:17 -07001264 return -EINVAL;
1265 type &= SOCK_TYPE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001267 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1268 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 retval = sock_create(family, type, protocol, &sock);
1271 if (retval < 0)
1272 goto out;
1273
Ulrich Drepper77d27202008-07-23 21:29:35 -07001274 retval = sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 if (retval < 0)
1276 goto out_release;
1277
1278out:
1279 /* It may be already another descriptor 8) Not kernel problem. */
1280 return retval;
1281
1282out_release:
1283 sock_release(sock);
1284 return retval;
1285}
1286
1287/*
1288 * Create a pair of connected sockets.
1289 */
1290
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001291SYSCALL_DEFINE4(socketpair, int, family, int, type, int, protocol,
1292 int __user *, usockvec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293{
1294 struct socket *sock1, *sock2;
1295 int fd1, fd2, err;
Al Virodb349502007-02-07 01:48:00 -05001296 struct file *newfile1, *newfile2;
Ulrich Dreppera677a032008-07-23 21:29:17 -07001297 int flags;
1298
1299 flags = type & ~SOCK_TYPE_MASK;
Ulrich Drepper77d27202008-07-23 21:29:35 -07001300 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Dreppera677a032008-07-23 21:29:17 -07001301 return -EINVAL;
1302 type &= SOCK_TYPE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001304 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1305 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1306
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 /*
1308 * Obtain the first socket and check if the underlying protocol
1309 * supports the socketpair call.
1310 */
1311
1312 err = sock_create(family, type, protocol, &sock1);
1313 if (err < 0)
1314 goto out;
1315
1316 err = sock_create(family, type, protocol, &sock2);
1317 if (err < 0)
1318 goto out_release_1;
1319
1320 err = sock1->ops->socketpair(sock1, sock2);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001321 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 goto out_release_both;
1323
Al Viro28407632012-08-17 23:54:15 -04001324 fd1 = get_unused_fd_flags(flags);
David S. Millerbf3c23d2007-10-29 21:54:02 -07001325 if (unlikely(fd1 < 0)) {
1326 err = fd1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 goto out_release_both;
David S. Millerbf3c23d2007-10-29 21:54:02 -07001328 }
Yann Droneaudd73aa282013-12-09 22:42:20 +01001329
Al Viro28407632012-08-17 23:54:15 -04001330 fd2 = get_unused_fd_flags(flags);
Al Viro198de4d2009-08-05 19:29:23 +04001331 if (unlikely(fd2 < 0)) {
1332 err = fd2;
Yann Droneaudd73aa282013-12-09 22:42:20 +01001333 goto out_put_unused_1;
Al Viro28407632012-08-17 23:54:15 -04001334 }
1335
Linus Torvaldsaab174f2012-10-02 20:25:04 -07001336 newfile1 = sock_alloc_file(sock1, flags, NULL);
Viresh Kumarb5ffe632015-08-12 15:59:47 +05301337 if (IS_ERR(newfile1)) {
Al Viro28407632012-08-17 23:54:15 -04001338 err = PTR_ERR(newfile1);
Yann Droneaudd73aa282013-12-09 22:42:20 +01001339 goto out_put_unused_both;
Al Viro28407632012-08-17 23:54:15 -04001340 }
1341
Linus Torvaldsaab174f2012-10-02 20:25:04 -07001342 newfile2 = sock_alloc_file(sock2, flags, NULL);
Al Viro28407632012-08-17 23:54:15 -04001343 if (IS_ERR(newfile2)) {
1344 err = PTR_ERR(newfile2);
Yann Droneaudd73aa282013-12-09 22:42:20 +01001345 goto out_fput_1;
Al Virodb349502007-02-07 01:48:00 -05001346 }
1347
Yann Droneaudd73aa282013-12-09 22:42:20 +01001348 err = put_user(fd1, &usockvec[0]);
1349 if (err)
1350 goto out_fput_both;
1351
1352 err = put_user(fd2, &usockvec[1]);
1353 if (err)
1354 goto out_fput_both;
1355
Al Viro157cf642008-12-14 04:57:47 -05001356 audit_fd_pair(fd1, fd2);
Yann Droneaudd73aa282013-12-09 22:42:20 +01001357
Al Virodb349502007-02-07 01:48:00 -05001358 fd_install(fd1, newfile1);
1359 fd_install(fd2, newfile2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 /* fd1 and fd2 may be already another descriptors.
1361 * Not kernel problem.
1362 */
1363
Yann Droneaudd73aa282013-12-09 22:42:20 +01001364 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Yann Droneaudd73aa282013-12-09 22:42:20 +01001366out_fput_both:
1367 fput(newfile2);
1368 fput(newfile1);
1369 put_unused_fd(fd2);
1370 put_unused_fd(fd1);
1371 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
Yann Droneaudd73aa282013-12-09 22:42:20 +01001373out_fput_1:
1374 fput(newfile1);
1375 put_unused_fd(fd2);
1376 put_unused_fd(fd1);
1377 sock_release(sock2);
1378 goto out;
1379
1380out_put_unused_both:
1381 put_unused_fd(fd2);
1382out_put_unused_1:
1383 put_unused_fd(fd1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384out_release_both:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001385 sock_release(sock2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386out_release_1:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001387 sock_release(sock1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388out:
1389 return err;
1390}
1391
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392/*
1393 * Bind a name to a socket. Nothing much to do here since it's
1394 * the protocol's responsibility to handle the local address.
1395 *
1396 * We move the socket address to kernel space before we call
1397 * the protocol layer (having also checked the address is ok).
1398 */
1399
Heiko Carstens20f37032009-01-14 14:14:23 +01001400SYSCALL_DEFINE3(bind, int, fd, struct sockaddr __user *, umyaddr, int, addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401{
1402 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001403 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001404 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001406 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001407 if (sock) {
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001408 err = move_addr_to_kernel(umyaddr, addrlen, &address);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001409 if (err >= 0) {
1410 err = security_socket_bind(sock,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001411 (struct sockaddr *)&address,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001412 addrlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001413 if (!err)
1414 err = sock->ops->bind(sock,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001415 (struct sockaddr *)
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001416 &address, addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 }
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001418 fput_light(sock->file, fput_needed);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 return err;
1421}
1422
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423/*
1424 * Perform a listen. Basically, we allow the protocol to do anything
1425 * necessary for a listen, and if that works, we mark the socket as
1426 * ready for listening.
1427 */
1428
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001429SYSCALL_DEFINE2(listen, int, fd, int, backlog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430{
1431 struct socket *sock;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001432 int err, fput_needed;
Pavel Emelyanovb8e1f9b2007-12-08 00:12:33 -08001433 int somaxconn;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001434
1435 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1436 if (sock) {
Pavel Emelyanov8efa6e92008-03-31 19:41:14 -07001437 somaxconn = sock_net(sock->sk)->core.sysctl_somaxconn;
Eric Dumazet95c96172012-04-15 05:58:06 +00001438 if ((unsigned int)backlog > somaxconn)
Pavel Emelyanovb8e1f9b2007-12-08 00:12:33 -08001439 backlog = somaxconn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
1441 err = security_socket_listen(sock, backlog);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001442 if (!err)
1443 err = sock->ops->listen(sock, backlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001445 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 }
1447 return err;
1448}
1449
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450/*
1451 * For accept, we attempt to create a new socket, set up the link
1452 * with the client, wake up the client, then return the new
1453 * connected fd. We collect the address of the connector in kernel
1454 * space and move it to user at the very end. This is unclean because
1455 * we open the socket then return an error.
1456 *
1457 * 1003.1g adds the ability to recvmsg() to query connection pending
1458 * status to recvmsg. We need to add that support in a way thats
1459 * clean when we restucture accept also.
1460 */
1461
Heiko Carstens20f37032009-01-14 14:14:23 +01001462SYSCALL_DEFINE4(accept4, int, fd, struct sockaddr __user *, upeer_sockaddr,
1463 int __user *, upeer_addrlen, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464{
1465 struct socket *sock, *newsock;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001466 struct file *newfile;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001467 int err, len, newfd, fput_needed;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001468 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
Ulrich Drepper77d27202008-07-23 21:29:35 -07001470 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001471 return -EINVAL;
1472
1473 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1474 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1475
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001476 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 if (!sock)
1478 goto out;
1479
1480 err = -ENFILE;
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001481 newsock = sock_alloc();
1482 if (!newsock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 goto out_put;
1484
1485 newsock->type = sock->type;
1486 newsock->ops = sock->ops;
1487
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 /*
1489 * We don't need try_module_get here, as the listening socket (sock)
1490 * has the protocol module (sock->ops->owner) held.
1491 */
1492 __module_get(newsock->ops->owner);
1493
Al Viro28407632012-08-17 23:54:15 -04001494 newfd = get_unused_fd_flags(flags);
David S. Miller39d8c1b2006-03-20 17:13:49 -08001495 if (unlikely(newfd < 0)) {
1496 err = newfd;
David S. Miller9a1875e2006-04-01 12:48:36 -08001497 sock_release(newsock);
1498 goto out_put;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001499 }
Linus Torvaldsaab174f2012-10-02 20:25:04 -07001500 newfile = sock_alloc_file(newsock, flags, sock->sk->sk_prot_creator->name);
Viresh Kumarb5ffe632015-08-12 15:59:47 +05301501 if (IS_ERR(newfile)) {
Al Viro28407632012-08-17 23:54:15 -04001502 err = PTR_ERR(newfile);
1503 put_unused_fd(newfd);
1504 sock_release(newsock);
1505 goto out_put;
1506 }
David S. Miller39d8c1b2006-03-20 17:13:49 -08001507
Frank Filza79af592005-09-27 15:23:38 -07001508 err = security_socket_accept(sock, newsock);
1509 if (err)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001510 goto out_fd;
Frank Filza79af592005-09-27 15:23:38 -07001511
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 err = sock->ops->accept(sock, newsock, sock->file->f_flags);
1513 if (err < 0)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001514 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
1516 if (upeer_sockaddr) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001517 if (newsock->ops->getname(newsock, (struct sockaddr *)&address,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001518 &len, 2) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 err = -ECONNABORTED;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001520 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 }
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001522 err = move_addr_to_user(&address,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001523 len, upeer_sockaddr, upeer_addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 if (err < 0)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001525 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 }
1527
1528 /* File flags are not inherited via accept() unlike another OSes. */
1529
David S. Miller39d8c1b2006-03-20 17:13:49 -08001530 fd_install(newfd, newfile);
1531 err = newfd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001534 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535out:
1536 return err;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001537out_fd:
David S. Miller9606a212006-04-01 01:00:14 -08001538 fput(newfile);
David S. Miller39d8c1b2006-03-20 17:13:49 -08001539 put_unused_fd(newfd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 goto out_put;
1541}
1542
Heiko Carstens20f37032009-01-14 14:14:23 +01001543SYSCALL_DEFINE3(accept, int, fd, struct sockaddr __user *, upeer_sockaddr,
1544 int __user *, upeer_addrlen)
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001545{
Ulrich Drepperde11def2008-11-19 15:36:14 -08001546 return sys_accept4(fd, upeer_sockaddr, upeer_addrlen, 0);
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001547}
1548
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549/*
1550 * Attempt to connect to a socket with the server address. The address
1551 * is in user space so we verify it is OK and move it to kernel space.
1552 *
1553 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
1554 * break bindings
1555 *
1556 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
1557 * other SEQPACKET protocols that take time to connect() as it doesn't
1558 * include the -EINPROGRESS status for such sockets.
1559 */
1560
Heiko Carstens20f37032009-01-14 14:14:23 +01001561SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
1562 int, addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563{
1564 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001565 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001566 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001568 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 if (!sock)
1570 goto out;
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001571 err = move_addr_to_kernel(uservaddr, addrlen, &address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 if (err < 0)
1573 goto out_put;
1574
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001575 err =
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001576 security_socket_connect(sock, (struct sockaddr *)&address, addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 if (err)
1578 goto out_put;
1579
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001580 err = sock->ops->connect(sock, (struct sockaddr *)&address, addrlen,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 sock->file->f_flags);
1582out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001583 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584out:
1585 return err;
1586}
1587
1588/*
1589 * Get the local address ('name') of a socket object. Move the obtained
1590 * name to user space.
1591 */
1592
Heiko Carstens20f37032009-01-14 14:14:23 +01001593SYSCALL_DEFINE3(getsockname, int, fd, struct sockaddr __user *, usockaddr,
1594 int __user *, usockaddr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595{
1596 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001597 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001598 int len, err, fput_needed;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001599
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001600 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 if (!sock)
1602 goto out;
1603
1604 err = security_socket_getsockname(sock);
1605 if (err)
1606 goto out_put;
1607
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001608 err = sock->ops->getname(sock, (struct sockaddr *)&address, &len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 if (err)
1610 goto out_put;
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001611 err = move_addr_to_user(&address, len, usockaddr, usockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
1613out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001614 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615out:
1616 return err;
1617}
1618
1619/*
1620 * Get the remote address ('name') of a socket object. Move the obtained
1621 * name to user space.
1622 */
1623
Heiko Carstens20f37032009-01-14 14:14:23 +01001624SYSCALL_DEFINE3(getpeername, int, fd, struct sockaddr __user *, usockaddr,
1625 int __user *, usockaddr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626{
1627 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001628 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001629 int len, err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001631 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1632 if (sock != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 err = security_socket_getpeername(sock);
1634 if (err) {
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001635 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 return err;
1637 }
1638
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001639 err =
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001640 sock->ops->getname(sock, (struct sockaddr *)&address, &len,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001641 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 if (!err)
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001643 err = move_addr_to_user(&address, len, usockaddr,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001644 usockaddr_len);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001645 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 }
1647 return err;
1648}
1649
1650/*
1651 * Send a datagram to a given address. We move the address into kernel
1652 * space and check the user space data area is readable before invoking
1653 * the protocol.
1654 */
1655
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001656SYSCALL_DEFINE6(sendto, int, fd, void __user *, buff, size_t, len,
Eric Dumazet95c96172012-04-15 05:58:06 +00001657 unsigned int, flags, struct sockaddr __user *, addr,
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001658 int, addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659{
1660 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001661 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 int err;
1663 struct msghdr msg;
1664 struct iovec iov;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001665 int fput_needed;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001666
Al Viro602bd0e2015-03-21 19:12:32 -04001667 err = import_single_range(WRITE, buff, len, &iov, &msg.msg_iter);
1668 if (unlikely(err))
1669 return err;
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001670 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1671 if (!sock)
David S. Miller4387ff72007-02-08 15:06:08 -08001672 goto out;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001673
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001674 msg.msg_name = NULL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001675 msg.msg_control = NULL;
1676 msg.msg_controllen = 0;
1677 msg.msg_namelen = 0;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001678 if (addr) {
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001679 err = move_addr_to_kernel(addr, addr_len, &address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 if (err < 0)
1681 goto out_put;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001682 msg.msg_name = (struct sockaddr *)&address;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001683 msg.msg_namelen = addr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 }
1685 if (sock->file->f_flags & O_NONBLOCK)
1686 flags |= MSG_DONTWAIT;
1687 msg.msg_flags = flags;
Al Virod8725c82014-12-11 00:02:50 -05001688 err = sock_sendmsg(sock, &msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001690out_put:
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001691 fput_light(sock->file, fput_needed);
David S. Miller4387ff72007-02-08 15:06:08 -08001692out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 return err;
1694}
1695
1696/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001697 * Send a datagram down a socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 */
1699
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001700SYSCALL_DEFINE4(send, int, fd, void __user *, buff, size_t, len,
Eric Dumazet95c96172012-04-15 05:58:06 +00001701 unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702{
1703 return sys_sendto(fd, buff, len, flags, NULL, 0);
1704}
1705
1706/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001707 * Receive a frame from the socket and optionally record the address of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 * sender. We verify the buffers are writable and if needed move the
1709 * sender address from kernel to user space.
1710 */
1711
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001712SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, ubuf, size_t, size,
Eric Dumazet95c96172012-04-15 05:58:06 +00001713 unsigned int, flags, struct sockaddr __user *, addr,
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001714 int __user *, addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715{
1716 struct socket *sock;
1717 struct iovec iov;
1718 struct msghdr msg;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001719 struct sockaddr_storage address;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001720 int err, err2;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001721 int fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
Al Viro602bd0e2015-03-21 19:12:32 -04001723 err = import_single_range(READ, ubuf, size, &iov, &msg.msg_iter);
1724 if (unlikely(err))
1725 return err;
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001726 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 if (!sock)
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001728 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001730 msg.msg_control = NULL;
1731 msg.msg_controllen = 0;
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01001732 /* Save some cycles and don't copy the address if not needed */
1733 msg.msg_name = addr ? (struct sockaddr *)&address : NULL;
1734 /* We assume all kernel code knows the size of sockaddr_storage */
1735 msg.msg_namelen = 0;
tadeusz.struk@intel.com130ed5d2015-12-15 10:46:17 -08001736 msg.msg_iocb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 if (sock->file->f_flags & O_NONBLOCK)
1738 flags |= MSG_DONTWAIT;
Al Viro2da62902015-03-14 21:13:46 -04001739 err = sock_recvmsg(sock, &msg, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001741 if (err >= 0 && addr != NULL) {
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001742 err2 = move_addr_to_user(&address,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001743 msg.msg_namelen, addr, addr_len);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001744 if (err2 < 0)
1745 err = err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 }
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001747
1748 fput_light(sock->file, fput_needed);
David S. Miller4387ff72007-02-08 15:06:08 -08001749out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 return err;
1751}
1752
1753/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001754 * Receive a datagram from a socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 */
1756
Jan Glauberb7c0ddf2014-04-16 09:32:48 +02001757SYSCALL_DEFINE4(recv, int, fd, void __user *, ubuf, size_t, size,
1758 unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759{
1760 return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
1761}
1762
1763/*
1764 * Set a socket option. Because we don't know the option lengths we have
1765 * to pass the user mode parameter for the protocols to sort out.
1766 */
1767
Heiko Carstens20f37032009-01-14 14:14:23 +01001768SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname,
1769 char __user *, optval, int, optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001771 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 struct socket *sock;
1773
1774 if (optlen < 0)
1775 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001776
1777 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1778 if (sock != NULL) {
1779 err = security_socket_setsockopt(sock, level, optname);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001780 if (err)
1781 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
1783 if (level == SOL_SOCKET)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001784 err =
1785 sock_setsockopt(sock, level, optname, optval,
1786 optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 else
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001788 err =
1789 sock->ops->setsockopt(sock, level, optname, optval,
1790 optlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001791out_put:
1792 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 }
1794 return err;
1795}
1796
1797/*
1798 * Get a socket option. Because we don't know the option lengths we have
1799 * to pass a user mode parameter for the protocols to sort out.
1800 */
1801
Heiko Carstens20f37032009-01-14 14:14:23 +01001802SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname,
1803 char __user *, optval, int __user *, optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001805 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 struct socket *sock;
1807
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001808 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1809 if (sock != NULL) {
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001810 err = security_socket_getsockopt(sock, level, optname);
1811 if (err)
1812 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
1814 if (level == SOL_SOCKET)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001815 err =
1816 sock_getsockopt(sock, level, optname, optval,
1817 optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 else
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001819 err =
1820 sock->ops->getsockopt(sock, level, optname, optval,
1821 optlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001822out_put:
1823 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 }
1825 return err;
1826}
1827
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828/*
1829 * Shutdown a socket.
1830 */
1831
Heiko Carstens754fe8d2009-01-14 14:14:09 +01001832SYSCALL_DEFINE2(shutdown, int, fd, int, how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001834 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 struct socket *sock;
1836
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001837 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1838 if (sock != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 err = security_socket_shutdown(sock, how);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001840 if (!err)
1841 err = sock->ops->shutdown(sock, how);
1842 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 }
1844 return err;
1845}
1846
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001847/* A couple of helpful macros for getting the address of the 32/64 bit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 * fields which are the same type (int / unsigned) on our platforms.
1849 */
1850#define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
1851#define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
1852#define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
1853
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00001854struct used_address {
1855 struct sockaddr_storage name;
1856 unsigned int name_len;
1857};
1858
Al Viroda184282015-03-21 19:29:06 -04001859static int copy_msghdr_from_user(struct msghdr *kmsg,
1860 struct user_msghdr __user *umsg,
1861 struct sockaddr __user **save_addr,
1862 struct iovec **iov)
Dan Carpenter1661bf32013-10-03 00:27:20 +03001863{
Al Viro08adb7d2014-11-10 20:23:13 -05001864 struct sockaddr __user *uaddr;
1865 struct iovec __user *uiov;
Al Viroc0371da2014-11-24 10:42:55 -05001866 size_t nr_segs;
Al Viro08adb7d2014-11-10 20:23:13 -05001867 ssize_t err;
1868
1869 if (!access_ok(VERIFY_READ, umsg, sizeof(*umsg)) ||
1870 __get_user(uaddr, &umsg->msg_name) ||
1871 __get_user(kmsg->msg_namelen, &umsg->msg_namelen) ||
1872 __get_user(uiov, &umsg->msg_iov) ||
Al Viroc0371da2014-11-24 10:42:55 -05001873 __get_user(nr_segs, &umsg->msg_iovlen) ||
Al Viro08adb7d2014-11-10 20:23:13 -05001874 __get_user(kmsg->msg_control, &umsg->msg_control) ||
1875 __get_user(kmsg->msg_controllen, &umsg->msg_controllen) ||
1876 __get_user(kmsg->msg_flags, &umsg->msg_flags))
Dan Carpenter1661bf32013-10-03 00:27:20 +03001877 return -EFAULT;
Matthew Leachdbb490b2014-03-11 11:58:27 +00001878
Al Viro08adb7d2014-11-10 20:23:13 -05001879 if (!uaddr)
Ani Sinha6a2a2b32014-09-08 14:49:59 -07001880 kmsg->msg_namelen = 0;
1881
Matthew Leachdbb490b2014-03-11 11:58:27 +00001882 if (kmsg->msg_namelen < 0)
1883 return -EINVAL;
1884
Dan Carpenter1661bf32013-10-03 00:27:20 +03001885 if (kmsg->msg_namelen > sizeof(struct sockaddr_storage))
Dan Carpenterdb31c552013-11-27 15:40:21 +03001886 kmsg->msg_namelen = sizeof(struct sockaddr_storage);
Al Viro08adb7d2014-11-10 20:23:13 -05001887
1888 if (save_addr)
1889 *save_addr = uaddr;
1890
1891 if (uaddr && kmsg->msg_namelen) {
1892 if (!save_addr) {
1893 err = move_addr_to_kernel(uaddr, kmsg->msg_namelen,
1894 kmsg->msg_name);
1895 if (err < 0)
1896 return err;
1897 }
1898 } else {
1899 kmsg->msg_name = NULL;
1900 kmsg->msg_namelen = 0;
1901 }
1902
Al Viroc0371da2014-11-24 10:42:55 -05001903 if (nr_segs > UIO_MAXIOV)
Al Viro08adb7d2014-11-10 20:23:13 -05001904 return -EMSGSIZE;
1905
tadeusz.struk@intel.com0345f932015-03-19 12:31:25 -07001906 kmsg->msg_iocb = NULL;
1907
Al Viroda184282015-03-21 19:29:06 -04001908 return import_iovec(save_addr ? READ : WRITE, uiov, nr_segs,
1909 UIO_FASTIOV, iov, &kmsg->msg_iter);
Dan Carpenter1661bf32013-10-03 00:27:20 +03001910}
1911
Al Viro666547f2014-04-06 14:03:05 -04001912static int ___sys_sendmsg(struct socket *sock, struct user_msghdr __user *msg,
Eric Dumazet95c96172012-04-15 05:58:06 +00001913 struct msghdr *msg_sys, unsigned int flags,
Tom Herbert28a94d82016-03-07 14:11:02 -08001914 struct used_address *used_address,
1915 unsigned int allowed_msghdr_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916{
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001917 struct compat_msghdr __user *msg_compat =
1918 (struct compat_msghdr __user *)msg;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001919 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
Alex Williamsonb9d717a2005-09-26 14:28:02 -07001921 unsigned char ctl[sizeof(struct cmsghdr) + 20]
Amit Kushwaha846cc122016-12-08 18:21:53 +05301922 __aligned(sizeof(__kernel_size_t));
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001923 /* 20 is size of ipv6_pktinfo */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 unsigned char *ctl_buf = ctl;
Al Virod8725c82014-12-11 00:02:50 -05001925 int ctl_len;
Al Viro08adb7d2014-11-10 20:23:13 -05001926 ssize_t err;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001927
Al Viro08adb7d2014-11-10 20:23:13 -05001928 msg_sys->msg_name = &address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
Al Viro08449322014-11-09 22:33:45 -05001930 if (MSG_CMSG_COMPAT & flags)
Al Viro08adb7d2014-11-10 20:23:13 -05001931 err = get_compat_msghdr(msg_sys, msg_compat, NULL, &iov);
Al Viro08449322014-11-09 22:33:45 -05001932 else
Al Viro08adb7d2014-11-10 20:23:13 -05001933 err = copy_msghdr_from_user(msg_sys, msg, NULL, &iov);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001934 if (err < 0)
Al Viroda184282015-03-21 19:29:06 -04001935 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
1937 err = -ENOBUFS;
1938
Anton Blanchard228e5482011-05-02 20:21:35 +00001939 if (msg_sys->msg_controllen > INT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 goto out_freeiov;
Tom Herbert28a94d82016-03-07 14:11:02 -08001941 flags |= (msg_sys->msg_flags & allowed_msghdr_flags);
Anton Blanchard228e5482011-05-02 20:21:35 +00001942 ctl_len = msg_sys->msg_controllen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001944 err =
Anton Blanchard228e5482011-05-02 20:21:35 +00001945 cmsghdr_from_user_compat_to_kern(msg_sys, sock->sk, ctl,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001946 sizeof(ctl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 if (err)
1948 goto out_freeiov;
Anton Blanchard228e5482011-05-02 20:21:35 +00001949 ctl_buf = msg_sys->msg_control;
1950 ctl_len = msg_sys->msg_controllen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 } else if (ctl_len) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001952 if (ctl_len > sizeof(ctl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001954 if (ctl_buf == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 goto out_freeiov;
1956 }
1957 err = -EFAULT;
1958 /*
Anton Blanchard228e5482011-05-02 20:21:35 +00001959 * Careful! Before this, msg_sys->msg_control contains a user pointer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
1961 * checking falls down on this.
1962 */
Namhyung Kimfb8621b2010-09-07 03:55:00 +00001963 if (copy_from_user(ctl_buf,
Anton Blanchard228e5482011-05-02 20:21:35 +00001964 (void __user __force *)msg_sys->msg_control,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001965 ctl_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 goto out_freectl;
Anton Blanchard228e5482011-05-02 20:21:35 +00001967 msg_sys->msg_control = ctl_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 }
Anton Blanchard228e5482011-05-02 20:21:35 +00001969 msg_sys->msg_flags = flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
1971 if (sock->file->f_flags & O_NONBLOCK)
Anton Blanchard228e5482011-05-02 20:21:35 +00001972 msg_sys->msg_flags |= MSG_DONTWAIT;
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00001973 /*
1974 * If this is sendmmsg() and current destination address is same as
1975 * previously succeeded address, omit asking LSM's decision.
1976 * used_address->name_len is initialized to UINT_MAX so that the first
1977 * destination address never matches.
1978 */
Mathieu Desnoyersbc909d92011-08-24 19:45:03 -07001979 if (used_address && msg_sys->msg_name &&
1980 used_address->name_len == msg_sys->msg_namelen &&
1981 !memcmp(&used_address->name, msg_sys->msg_name,
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00001982 used_address->name_len)) {
Al Virod8725c82014-12-11 00:02:50 -05001983 err = sock_sendmsg_nosec(sock, msg_sys);
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00001984 goto out_freectl;
1985 }
Al Virod8725c82014-12-11 00:02:50 -05001986 err = sock_sendmsg(sock, msg_sys);
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00001987 /*
1988 * If this is sendmmsg() and sending to current destination address was
1989 * successful, remember it.
1990 */
1991 if (used_address && err >= 0) {
1992 used_address->name_len = msg_sys->msg_namelen;
Mathieu Desnoyersbc909d92011-08-24 19:45:03 -07001993 if (msg_sys->msg_name)
1994 memcpy(&used_address->name, msg_sys->msg_name,
1995 used_address->name_len);
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00001996 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
1998out_freectl:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001999 if (ctl_buf != ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 sock_kfree_s(sock->sk, ctl_buf, ctl_len);
2001out_freeiov:
Al Viroda184282015-03-21 19:29:06 -04002002 kfree(iov);
Anton Blanchard228e5482011-05-02 20:21:35 +00002003 return err;
2004}
2005
2006/*
2007 * BSD sendmsg interface
2008 */
2009
Al Viro666547f2014-04-06 14:03:05 -04002010long __sys_sendmsg(int fd, struct user_msghdr __user *msg, unsigned flags)
Anton Blanchard228e5482011-05-02 20:21:35 +00002011{
2012 int fput_needed, err;
2013 struct msghdr msg_sys;
Andy Lutomirski1be374a2013-05-22 14:07:44 -07002014 struct socket *sock;
Anton Blanchard228e5482011-05-02 20:21:35 +00002015
Andy Lutomirski1be374a2013-05-22 14:07:44 -07002016 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Anton Blanchard228e5482011-05-02 20:21:35 +00002017 if (!sock)
2018 goto out;
2019
Tom Herbert28a94d82016-03-07 14:11:02 -08002020 err = ___sys_sendmsg(sock, msg, &msg_sys, flags, NULL, 0);
Anton Blanchard228e5482011-05-02 20:21:35 +00002021
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002022 fput_light(sock->file, fput_needed);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002023out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 return err;
2025}
2026
Al Viro666547f2014-04-06 14:03:05 -04002027SYSCALL_DEFINE3(sendmsg, int, fd, struct user_msghdr __user *, msg, unsigned int, flags)
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002028{
2029 if (flags & MSG_CMSG_COMPAT)
2030 return -EINVAL;
2031 return __sys_sendmsg(fd, msg, flags);
2032}
2033
Anton Blanchard228e5482011-05-02 20:21:35 +00002034/*
2035 * Linux sendmmsg interface
2036 */
2037
2038int __sys_sendmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
2039 unsigned int flags)
2040{
2041 int fput_needed, err, datagrams;
2042 struct socket *sock;
2043 struct mmsghdr __user *entry;
2044 struct compat_mmsghdr __user *compat_entry;
2045 struct msghdr msg_sys;
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002046 struct used_address used_address;
Tom Herbertf0922762016-03-07 14:11:03 -08002047 unsigned int oflags = flags;
Anton Blanchard228e5482011-05-02 20:21:35 +00002048
Anton Blanchard98382f42011-08-04 14:07:39 +00002049 if (vlen > UIO_MAXIOV)
2050 vlen = UIO_MAXIOV;
Anton Blanchard228e5482011-05-02 20:21:35 +00002051
2052 datagrams = 0;
2053
2054 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2055 if (!sock)
2056 return err;
2057
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002058 used_address.name_len = UINT_MAX;
Anton Blanchard228e5482011-05-02 20:21:35 +00002059 entry = mmsg;
2060 compat_entry = (struct compat_mmsghdr __user *)mmsg;
Anton Blanchard728ffb82011-08-04 14:07:38 +00002061 err = 0;
Tom Herbertf0922762016-03-07 14:11:03 -08002062 flags |= MSG_BATCH;
Anton Blanchard228e5482011-05-02 20:21:35 +00002063
2064 while (datagrams < vlen) {
Tom Herbertf0922762016-03-07 14:11:03 -08002065 if (datagrams == vlen - 1)
2066 flags = oflags;
2067
Anton Blanchard228e5482011-05-02 20:21:35 +00002068 if (MSG_CMSG_COMPAT & flags) {
Al Viro666547f2014-04-06 14:03:05 -04002069 err = ___sys_sendmsg(sock, (struct user_msghdr __user *)compat_entry,
Tom Herbert28a94d82016-03-07 14:11:02 -08002070 &msg_sys, flags, &used_address, MSG_EOR);
Anton Blanchard228e5482011-05-02 20:21:35 +00002071 if (err < 0)
2072 break;
2073 err = __put_user(err, &compat_entry->msg_len);
2074 ++compat_entry;
2075 } else {
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002076 err = ___sys_sendmsg(sock,
Al Viro666547f2014-04-06 14:03:05 -04002077 (struct user_msghdr __user *)entry,
Tom Herbert28a94d82016-03-07 14:11:02 -08002078 &msg_sys, flags, &used_address, MSG_EOR);
Anton Blanchard228e5482011-05-02 20:21:35 +00002079 if (err < 0)
2080 break;
2081 err = put_user(err, &entry->msg_len);
2082 ++entry;
2083 }
2084
2085 if (err)
2086 break;
2087 ++datagrams;
Soheil Hassas Yeganeh30238982016-11-04 15:36:49 -04002088 if (msg_data_left(&msg_sys))
2089 break;
Eric Dumazeta78cb842016-01-08 08:37:20 -08002090 cond_resched();
Anton Blanchard228e5482011-05-02 20:21:35 +00002091 }
2092
Anton Blanchard228e5482011-05-02 20:21:35 +00002093 fput_light(sock->file, fput_needed);
2094
Anton Blanchard728ffb82011-08-04 14:07:38 +00002095 /* We only return an error if no datagrams were able to be sent */
2096 if (datagrams != 0)
Anton Blanchard228e5482011-05-02 20:21:35 +00002097 return datagrams;
2098
Anton Blanchard228e5482011-05-02 20:21:35 +00002099 return err;
2100}
2101
2102SYSCALL_DEFINE4(sendmmsg, int, fd, struct mmsghdr __user *, mmsg,
2103 unsigned int, vlen, unsigned int, flags)
2104{
Andy Lutomirski1be374a2013-05-22 14:07:44 -07002105 if (flags & MSG_CMSG_COMPAT)
2106 return -EINVAL;
Anton Blanchard228e5482011-05-02 20:21:35 +00002107 return __sys_sendmmsg(fd, mmsg, vlen, flags);
2108}
2109
Al Viro666547f2014-04-06 14:03:05 -04002110static int ___sys_recvmsg(struct socket *sock, struct user_msghdr __user *msg,
Eric Dumazet95c96172012-04-15 05:58:06 +00002111 struct msghdr *msg_sys, unsigned int flags, int nosec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112{
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002113 struct compat_msghdr __user *msg_compat =
2114 (struct compat_msghdr __user *)msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 struct iovec iovstack[UIO_FASTIOV];
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002116 struct iovec *iov = iovstack;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 unsigned long cmsg_ptr;
Al Viro2da62902015-03-14 21:13:46 -04002118 int len;
Al Viro08adb7d2014-11-10 20:23:13 -05002119 ssize_t err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
2121 /* kernel mode address */
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07002122 struct sockaddr_storage addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
2124 /* user mode address pointers */
2125 struct sockaddr __user *uaddr;
Al Viro08adb7d2014-11-10 20:23:13 -05002126 int __user *uaddr_len = COMPAT_NAMELEN(msg);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002127
Al Viro08adb7d2014-11-10 20:23:13 -05002128 msg_sys->msg_name = &addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01002130 if (MSG_CMSG_COMPAT & flags)
Al Viro08adb7d2014-11-10 20:23:13 -05002131 err = get_compat_msghdr(msg_sys, msg_compat, &uaddr, &iov);
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01002132 else
Al Viro08adb7d2014-11-10 20:23:13 -05002133 err = copy_msghdr_from_user(msg_sys, msg, &uaddr, &iov);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 if (err < 0)
Al Viroda184282015-03-21 19:29:06 -04002135 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002137 cmsg_ptr = (unsigned long)msg_sys->msg_control;
2138 msg_sys->msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002139
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01002140 /* We assume all kernel code knows the size of sockaddr_storage */
2141 msg_sys->msg_namelen = 0;
2142
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 if (sock->file->f_flags & O_NONBLOCK)
2144 flags |= MSG_DONTWAIT;
Al Viro2da62902015-03-14 21:13:46 -04002145 err = (nosec ? sock_recvmsg_nosec : sock_recvmsg)(sock, msg_sys, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 if (err < 0)
2147 goto out_freeiov;
2148 len = err;
2149
2150 if (uaddr != NULL) {
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00002151 err = move_addr_to_user(&addr,
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002152 msg_sys->msg_namelen, uaddr,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002153 uaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 if (err < 0)
2155 goto out_freeiov;
2156 }
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002157 err = __put_user((msg_sys->msg_flags & ~MSG_CMSG_COMPAT),
David S. Miller37f7f422005-09-16 16:51:01 -07002158 COMPAT_FLAGS(msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 if (err)
2160 goto out_freeiov;
2161 if (MSG_CMSG_COMPAT & flags)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002162 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 &msg_compat->msg_controllen);
2164 else
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002165 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 &msg->msg_controllen);
2167 if (err)
2168 goto out_freeiov;
2169 err = len;
2170
2171out_freeiov:
Al Viroda184282015-03-21 19:29:06 -04002172 kfree(iov);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002173 return err;
2174}
2175
2176/*
2177 * BSD recvmsg interface
2178 */
2179
Al Viro666547f2014-04-06 14:03:05 -04002180long __sys_recvmsg(int fd, struct user_msghdr __user *msg, unsigned flags)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002181{
2182 int fput_needed, err;
2183 struct msghdr msg_sys;
Andy Lutomirski1be374a2013-05-22 14:07:44 -07002184 struct socket *sock;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002185
Andy Lutomirski1be374a2013-05-22 14:07:44 -07002186 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002187 if (!sock)
2188 goto out;
2189
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002190 err = ___sys_recvmsg(sock, msg, &msg_sys, flags, 0);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002191
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002192 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193out:
2194 return err;
2195}
2196
Al Viro666547f2014-04-06 14:03:05 -04002197SYSCALL_DEFINE3(recvmsg, int, fd, struct user_msghdr __user *, msg,
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002198 unsigned int, flags)
2199{
2200 if (flags & MSG_CMSG_COMPAT)
2201 return -EINVAL;
2202 return __sys_recvmsg(fd, msg, flags);
2203}
2204
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002205/*
2206 * Linux recvmmsg interface
2207 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002209int __sys_recvmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
2210 unsigned int flags, struct timespec *timeout)
2211{
2212 int fput_needed, err, datagrams;
2213 struct socket *sock;
2214 struct mmsghdr __user *entry;
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002215 struct compat_mmsghdr __user *compat_entry;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002216 struct msghdr msg_sys;
Deepa Dinamani766b9f92016-05-19 17:09:05 -07002217 struct timespec64 end_time;
2218 struct timespec64 timeout64;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002219
2220 if (timeout &&
2221 poll_select_set_timeout(&end_time, timeout->tv_sec,
2222 timeout->tv_nsec))
2223 return -EINVAL;
2224
2225 datagrams = 0;
2226
2227 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2228 if (!sock)
2229 return err;
2230
2231 err = sock_error(sock->sk);
2232 if (err)
2233 goto out_put;
2234
2235 entry = mmsg;
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002236 compat_entry = (struct compat_mmsghdr __user *)mmsg;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002237
2238 while (datagrams < vlen) {
2239 /*
2240 * No need to ask LSM for more than the first datagram.
2241 */
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002242 if (MSG_CMSG_COMPAT & flags) {
Al Viro666547f2014-04-06 14:03:05 -04002243 err = ___sys_recvmsg(sock, (struct user_msghdr __user *)compat_entry,
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002244 &msg_sys, flags & ~MSG_WAITFORONE,
2245 datagrams);
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002246 if (err < 0)
2247 break;
2248 err = __put_user(err, &compat_entry->msg_len);
2249 ++compat_entry;
2250 } else {
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002251 err = ___sys_recvmsg(sock,
Al Viro666547f2014-04-06 14:03:05 -04002252 (struct user_msghdr __user *)entry,
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002253 &msg_sys, flags & ~MSG_WAITFORONE,
2254 datagrams);
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002255 if (err < 0)
2256 break;
2257 err = put_user(err, &entry->msg_len);
2258 ++entry;
2259 }
2260
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002261 if (err)
2262 break;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002263 ++datagrams;
2264
Brandon L Black71c5c1592010-03-26 16:18:03 +00002265 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
2266 if (flags & MSG_WAITFORONE)
2267 flags |= MSG_DONTWAIT;
2268
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002269 if (timeout) {
Deepa Dinamani766b9f92016-05-19 17:09:05 -07002270 ktime_get_ts64(&timeout64);
2271 *timeout = timespec64_to_timespec(
2272 timespec64_sub(end_time, timeout64));
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002273 if (timeout->tv_sec < 0) {
2274 timeout->tv_sec = timeout->tv_nsec = 0;
2275 break;
2276 }
2277
2278 /* Timeout, return less than vlen datagrams */
2279 if (timeout->tv_nsec == 0 && timeout->tv_sec == 0)
2280 break;
2281 }
2282
2283 /* Out of band data, return right away */
2284 if (msg_sys.msg_flags & MSG_OOB)
2285 break;
Eric Dumazeta78cb842016-01-08 08:37:20 -08002286 cond_resched();
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002287 }
2288
Arnaldo Carvalho de Melo34b88a62016-03-14 09:56:35 -03002289 if (err == 0)
2290 goto out_put;
2291
2292 if (datagrams == 0) {
2293 datagrams = err;
2294 goto out_put;
2295 }
2296
2297 /*
2298 * We may return less entries than requested (vlen) if the
2299 * sock is non block and there aren't enough datagrams...
2300 */
2301 if (err != -EAGAIN) {
2302 /*
2303 * ... or if recvmsg returns an error after we
2304 * received some datagrams, where we record the
2305 * error to return on the next call or if the
2306 * app asks about it using getsockopt(SO_ERROR).
2307 */
2308 sock->sk->sk_err = -err;
2309 }
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002310out_put:
2311 fput_light(sock->file, fput_needed);
2312
Arnaldo Carvalho de Melo34b88a62016-03-14 09:56:35 -03002313 return datagrams;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002314}
2315
2316SYSCALL_DEFINE5(recvmmsg, int, fd, struct mmsghdr __user *, mmsg,
2317 unsigned int, vlen, unsigned int, flags,
2318 struct timespec __user *, timeout)
2319{
2320 int datagrams;
2321 struct timespec timeout_sys;
2322
Andy Lutomirski1be374a2013-05-22 14:07:44 -07002323 if (flags & MSG_CMSG_COMPAT)
2324 return -EINVAL;
2325
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002326 if (!timeout)
2327 return __sys_recvmmsg(fd, mmsg, vlen, flags, NULL);
2328
2329 if (copy_from_user(&timeout_sys, timeout, sizeof(timeout_sys)))
2330 return -EFAULT;
2331
2332 datagrams = __sys_recvmmsg(fd, mmsg, vlen, flags, &timeout_sys);
2333
2334 if (datagrams > 0 &&
2335 copy_to_user(timeout, &timeout_sys, sizeof(timeout_sys)))
2336 datagrams = -EFAULT;
2337
2338 return datagrams;
2339}
2340
2341#ifdef __ARCH_WANT_SYS_SOCKETCALL
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342/* Argument list sizes for sys_socketcall */
2343#define AL(x) ((x) * sizeof(unsigned long))
Anton Blanchard228e5482011-05-02 20:21:35 +00002344static const unsigned char nargs[21] = {
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002345 AL(0), AL(3), AL(3), AL(3), AL(2), AL(3),
2346 AL(3), AL(3), AL(4), AL(4), AL(4), AL(6),
2347 AL(6), AL(2), AL(5), AL(5), AL(3), AL(3),
Anton Blanchard228e5482011-05-02 20:21:35 +00002348 AL(4), AL(5), AL(4)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002349};
2350
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351#undef AL
2352
2353/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002354 * System call vectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 *
2356 * Argument checking cleaned up. Saved 20% in size.
2357 * This function doesn't need to set the kernel lock because
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002358 * it is set by the callees.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 */
2360
Heiko Carstens3e0fa652009-01-14 14:14:24 +01002361SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362{
Chen Gang2950fa92013-04-07 16:55:23 +08002363 unsigned long a[AUDITSC_ARGS];
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002364 unsigned long a0, a1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 int err;
Arjan van de Ven47379052009-09-28 12:57:44 -07002366 unsigned int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367
Anton Blanchard228e5482011-05-02 20:21:35 +00002368 if (call < 1 || call > SYS_SENDMMSG)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 return -EINVAL;
2370
Arjan van de Ven47379052009-09-28 12:57:44 -07002371 len = nargs[call];
2372 if (len > sizeof(a))
2373 return -EINVAL;
2374
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 /* copy_from_user should be SMP safe. */
Arjan van de Ven47379052009-09-28 12:57:44 -07002376 if (copy_from_user(a, args, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 return -EFAULT;
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002378
Chen Gang2950fa92013-04-07 16:55:23 +08002379 err = audit_socketcall(nargs[call] / sizeof(unsigned long), a);
2380 if (err)
2381 return err;
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002382
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002383 a0 = a[0];
2384 a1 = a[1];
2385
2386 switch (call) {
2387 case SYS_SOCKET:
2388 err = sys_socket(a0, a1, a[2]);
2389 break;
2390 case SYS_BIND:
2391 err = sys_bind(a0, (struct sockaddr __user *)a1, a[2]);
2392 break;
2393 case SYS_CONNECT:
2394 err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
2395 break;
2396 case SYS_LISTEN:
2397 err = sys_listen(a0, a1);
2398 break;
2399 case SYS_ACCEPT:
Ulrich Drepperde11def2008-11-19 15:36:14 -08002400 err = sys_accept4(a0, (struct sockaddr __user *)a1,
2401 (int __user *)a[2], 0);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002402 break;
2403 case SYS_GETSOCKNAME:
2404 err =
2405 sys_getsockname(a0, (struct sockaddr __user *)a1,
2406 (int __user *)a[2]);
2407 break;
2408 case SYS_GETPEERNAME:
2409 err =
2410 sys_getpeername(a0, (struct sockaddr __user *)a1,
2411 (int __user *)a[2]);
2412 break;
2413 case SYS_SOCKETPAIR:
2414 err = sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
2415 break;
2416 case SYS_SEND:
2417 err = sys_send(a0, (void __user *)a1, a[2], a[3]);
2418 break;
2419 case SYS_SENDTO:
2420 err = sys_sendto(a0, (void __user *)a1, a[2], a[3],
2421 (struct sockaddr __user *)a[4], a[5]);
2422 break;
2423 case SYS_RECV:
2424 err = sys_recv(a0, (void __user *)a1, a[2], a[3]);
2425 break;
2426 case SYS_RECVFROM:
2427 err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
2428 (struct sockaddr __user *)a[4],
2429 (int __user *)a[5]);
2430 break;
2431 case SYS_SHUTDOWN:
2432 err = sys_shutdown(a0, a1);
2433 break;
2434 case SYS_SETSOCKOPT:
2435 err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]);
2436 break;
2437 case SYS_GETSOCKOPT:
2438 err =
2439 sys_getsockopt(a0, a1, a[2], (char __user *)a[3],
2440 (int __user *)a[4]);
2441 break;
2442 case SYS_SENDMSG:
Al Viro666547f2014-04-06 14:03:05 -04002443 err = sys_sendmsg(a0, (struct user_msghdr __user *)a1, a[2]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002444 break;
Anton Blanchard228e5482011-05-02 20:21:35 +00002445 case SYS_SENDMMSG:
2446 err = sys_sendmmsg(a0, (struct mmsghdr __user *)a1, a[2], a[3]);
2447 break;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002448 case SYS_RECVMSG:
Al Viro666547f2014-04-06 14:03:05 -04002449 err = sys_recvmsg(a0, (struct user_msghdr __user *)a1, a[2]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002450 break;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002451 case SYS_RECVMMSG:
2452 err = sys_recvmmsg(a0, (struct mmsghdr __user *)a1, a[2], a[3],
2453 (struct timespec __user *)a[4]);
2454 break;
Ulrich Drepperde11def2008-11-19 15:36:14 -08002455 case SYS_ACCEPT4:
2456 err = sys_accept4(a0, (struct sockaddr __user *)a1,
2457 (int __user *)a[2], a[3]);
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07002458 break;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002459 default:
2460 err = -EINVAL;
2461 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 }
2463 return err;
2464}
2465
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002466#endif /* __ARCH_WANT_SYS_SOCKETCALL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002468/**
2469 * sock_register - add a socket protocol handler
2470 * @ops: description of protocol
2471 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 * This function is called by a protocol handler that wants to
2473 * advertise its address family, and have it linked into the
Masanari Iidae793c0f2014-09-04 23:44:36 +09002474 * socket interface. The value ops->family corresponds to the
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002475 * socket system call protocol family.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 */
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002477int sock_register(const struct net_proto_family *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478{
2479 int err;
2480
2481 if (ops->family >= NPROTO) {
Yang Yingliang3410f222014-02-12 17:09:55 +08002482 pr_crit("protocol %d >= NPROTO(%d)\n", ops->family, NPROTO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 return -ENOBUFS;
2484 }
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002485
2486 spin_lock(&net_family_lock);
Eric Dumazet190683a2010-11-10 10:50:44 +00002487 if (rcu_dereference_protected(net_families[ops->family],
2488 lockdep_is_held(&net_family_lock)))
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002489 err = -EEXIST;
2490 else {
Eric Dumazetcf778b02012-01-12 04:41:32 +00002491 rcu_assign_pointer(net_families[ops->family], ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 err = 0;
2493 }
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002494 spin_unlock(&net_family_lock);
2495
Yang Yingliang3410f222014-02-12 17:09:55 +08002496 pr_info("NET: Registered protocol family %d\n", ops->family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 return err;
2498}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002499EXPORT_SYMBOL(sock_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002501/**
2502 * sock_unregister - remove a protocol handler
2503 * @family: protocol family to remove
2504 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 * This function is called by a protocol handler that wants to
2506 * remove its address family, and have it unlinked from the
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002507 * new socket creation.
2508 *
2509 * If protocol handler is a module, then it can use module reference
2510 * counts to protect against new references. If protocol handler is not
2511 * a module then it needs to provide its own protection in
2512 * the ops->create routine.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 */
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002514void sock_unregister(int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515{
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002516 BUG_ON(family < 0 || family >= NPROTO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002518 spin_lock(&net_family_lock);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00002519 RCU_INIT_POINTER(net_families[family], NULL);
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002520 spin_unlock(&net_family_lock);
2521
2522 synchronize_rcu();
2523
Yang Yingliang3410f222014-02-12 17:09:55 +08002524 pr_info("NET: Unregistered protocol family %d\n", family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002526EXPORT_SYMBOL(sock_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527
Andi Kleen77d76ea2005-12-22 12:43:42 -08002528static int __init sock_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529{
Nick Pigginb3e19d92011-01-07 17:50:11 +11002530 int err;
Eric W. Biederman2ca794e2012-04-19 13:20:32 +00002531 /*
2532 * Initialize the network sysctl infrastructure.
2533 */
2534 err = net_sysctl_init();
2535 if (err)
2536 goto out;
Nick Pigginb3e19d92011-01-07 17:50:11 +11002537
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002539 * Initialize skbuff SLAB cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 */
2541 skb_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542
2543 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002544 * Initialize the protocols module.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 */
2546
2547 init_inodecache();
Nick Pigginb3e19d92011-01-07 17:50:11 +11002548
2549 err = register_filesystem(&sock_fs_type);
2550 if (err)
2551 goto out_fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 sock_mnt = kern_mount(&sock_fs_type);
Nick Pigginb3e19d92011-01-07 17:50:11 +11002553 if (IS_ERR(sock_mnt)) {
2554 err = PTR_ERR(sock_mnt);
2555 goto out_mount;
2556 }
Andi Kleen77d76ea2005-12-22 12:43:42 -08002557
2558 /* The real protocol initialization is performed in later initcalls.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 */
2560
2561#ifdef CONFIG_NETFILTER
Pablo Neira Ayuso6d11cfd2013-05-22 22:42:36 +00002562 err = netfilter_init();
2563 if (err)
2564 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565#endif
David S. Millercbeb3212005-12-22 12:58:55 -08002566
Daniel Borkmann408eccc2014-04-01 16:20:23 +02002567 ptp_classifier_init();
Richard Cochranc1f19b52010-07-17 08:49:36 +00002568
Nick Pigginb3e19d92011-01-07 17:50:11 +11002569out:
2570 return err;
2571
2572out_mount:
2573 unregister_filesystem(&sock_fs_type);
2574out_fs:
2575 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576}
2577
Andi Kleen77d76ea2005-12-22 12:43:42 -08002578core_initcall(sock_init); /* early initcall */
2579
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580#ifdef CONFIG_PROC_FS
2581void socket_seq_show(struct seq_file *seq)
2582{
2583 int cpu;
2584 int counter = 0;
2585
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -07002586 for_each_possible_cpu(cpu)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002587 counter += per_cpu(sockets_in_use, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588
2589 /* It can be negative, by the way. 8) */
2590 if (counter < 0)
2591 counter = 0;
2592
2593 seq_printf(seq, "sockets: used %d\n", counter);
2594}
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002595#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596
Shaun Pereira89bbfc92006-03-21 23:58:08 -08002597#ifdef CONFIG_COMPAT
Arnd Bergmann6b960182009-11-06 23:10:54 -08002598static int do_siocgstamp(struct net *net, struct socket *sock,
H. Peter Anvin644595f2012-02-19 17:51:59 -08002599 unsigned int cmd, void __user *up)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002600{
Arnd Bergmann7a229382009-11-06 23:00:29 -08002601 mm_segment_t old_fs = get_fs();
2602 struct timeval ktv;
2603 int err;
2604
2605 set_fs(KERNEL_DS);
Arnd Bergmann6b960182009-11-06 23:10:54 -08002606 err = sock_do_ioctl(net, sock, cmd, (unsigned long)&ktv);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002607 set_fs(old_fs);
H. Peter Anvin644595f2012-02-19 17:51:59 -08002608 if (!err)
Mikulas Patockaed6fe9d2012-09-01 12:34:07 -04002609 err = compat_put_timeval(&ktv, up);
H. Peter Anvin644595f2012-02-19 17:51:59 -08002610
Arnd Bergmann7a229382009-11-06 23:00:29 -08002611 return err;
2612}
2613
Arnd Bergmann6b960182009-11-06 23:10:54 -08002614static int do_siocgstampns(struct net *net, struct socket *sock,
H. Peter Anvin644595f2012-02-19 17:51:59 -08002615 unsigned int cmd, void __user *up)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002616{
Arnd Bergmann7a229382009-11-06 23:00:29 -08002617 mm_segment_t old_fs = get_fs();
2618 struct timespec kts;
2619 int err;
2620
2621 set_fs(KERNEL_DS);
Arnd Bergmann6b960182009-11-06 23:10:54 -08002622 err = sock_do_ioctl(net, sock, cmd, (unsigned long)&kts);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002623 set_fs(old_fs);
H. Peter Anvin644595f2012-02-19 17:51:59 -08002624 if (!err)
Mikulas Patockaed6fe9d2012-09-01 12:34:07 -04002625 err = compat_put_timespec(&kts, up);
H. Peter Anvin644595f2012-02-19 17:51:59 -08002626
Arnd Bergmann7a229382009-11-06 23:00:29 -08002627 return err;
2628}
2629
Arnd Bergmann6b960182009-11-06 23:10:54 -08002630static int dev_ifname32(struct net *net, struct compat_ifreq __user *uifr32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002631{
2632 struct ifreq __user *uifr;
2633 int err;
2634
2635 uifr = compat_alloc_user_space(sizeof(struct ifreq));
Arnd Bergmann6b960182009-11-06 23:10:54 -08002636 if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002637 return -EFAULT;
2638
Arnd Bergmann6b960182009-11-06 23:10:54 -08002639 err = dev_ioctl(net, SIOCGIFNAME, uifr);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002640 if (err)
2641 return err;
2642
Arnd Bergmann6b960182009-11-06 23:10:54 -08002643 if (copy_in_user(uifr32, uifr, sizeof(struct compat_ifreq)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002644 return -EFAULT;
2645
2646 return 0;
2647}
2648
Arnd Bergmann6b960182009-11-06 23:10:54 -08002649static int dev_ifconf(struct net *net, struct compat_ifconf __user *uifc32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002650{
Arnd Bergmann6b960182009-11-06 23:10:54 -08002651 struct compat_ifconf ifc32;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002652 struct ifconf ifc;
2653 struct ifconf __user *uifc;
Arnd Bergmann6b960182009-11-06 23:10:54 -08002654 struct compat_ifreq __user *ifr32;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002655 struct ifreq __user *ifr;
2656 unsigned int i, j;
2657 int err;
2658
Arnd Bergmann6b960182009-11-06 23:10:54 -08002659 if (copy_from_user(&ifc32, uifc32, sizeof(struct compat_ifconf)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002660 return -EFAULT;
2661
Mathias Krause43da5f22012-08-15 11:31:57 +00002662 memset(&ifc, 0, sizeof(ifc));
Arnd Bergmann7a229382009-11-06 23:00:29 -08002663 if (ifc32.ifcbuf == 0) {
2664 ifc32.ifc_len = 0;
2665 ifc.ifc_len = 0;
2666 ifc.ifc_req = NULL;
2667 uifc = compat_alloc_user_space(sizeof(struct ifconf));
2668 } else {
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002669 size_t len = ((ifc32.ifc_len / sizeof(struct compat_ifreq)) + 1) *
2670 sizeof(struct ifreq);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002671 uifc = compat_alloc_user_space(sizeof(struct ifconf) + len);
2672 ifc.ifc_len = len;
2673 ifr = ifc.ifc_req = (void __user *)(uifc + 1);
2674 ifr32 = compat_ptr(ifc32.ifcbuf);
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002675 for (i = 0; i < ifc32.ifc_len; i += sizeof(struct compat_ifreq)) {
Arnd Bergmann6b960182009-11-06 23:10:54 -08002676 if (copy_in_user(ifr, ifr32, sizeof(struct compat_ifreq)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002677 return -EFAULT;
2678 ifr++;
2679 ifr32++;
2680 }
2681 }
2682 if (copy_to_user(uifc, &ifc, sizeof(struct ifconf)))
2683 return -EFAULT;
2684
Arnd Bergmann6b960182009-11-06 23:10:54 -08002685 err = dev_ioctl(net, SIOCGIFCONF, uifc);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002686 if (err)
2687 return err;
2688
2689 if (copy_from_user(&ifc, uifc, sizeof(struct ifconf)))
2690 return -EFAULT;
2691
2692 ifr = ifc.ifc_req;
2693 ifr32 = compat_ptr(ifc32.ifcbuf);
2694 for (i = 0, j = 0;
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002695 i + sizeof(struct compat_ifreq) <= ifc32.ifc_len && j < ifc.ifc_len;
2696 i += sizeof(struct compat_ifreq), j += sizeof(struct ifreq)) {
2697 if (copy_in_user(ifr32, ifr, sizeof(struct compat_ifreq)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002698 return -EFAULT;
2699 ifr32++;
2700 ifr++;
2701 }
2702
2703 if (ifc32.ifcbuf == 0) {
2704 /* Translate from 64-bit structure multiple to
2705 * a 32-bit one.
2706 */
2707 i = ifc.ifc_len;
Arnd Bergmann6b960182009-11-06 23:10:54 -08002708 i = ((i / sizeof(struct ifreq)) * sizeof(struct compat_ifreq));
Arnd Bergmann7a229382009-11-06 23:00:29 -08002709 ifc32.ifc_len = i;
2710 } else {
2711 ifc32.ifc_len = i;
2712 }
Arnd Bergmann6b960182009-11-06 23:10:54 -08002713 if (copy_to_user(uifc32, &ifc32, sizeof(struct compat_ifconf)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002714 return -EFAULT;
2715
2716 return 0;
2717}
2718
Arnd Bergmann6b960182009-11-06 23:10:54 -08002719static int ethtool_ioctl(struct net *net, struct compat_ifreq __user *ifr32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002720{
Ben Hutchings3a7da392011-03-17 07:34:32 +00002721 struct compat_ethtool_rxnfc __user *compat_rxnfc;
2722 bool convert_in = false, convert_out = false;
2723 size_t buf_size = ALIGN(sizeof(struct ifreq), 8);
2724 struct ethtool_rxnfc __user *rxnfc;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002725 struct ifreq __user *ifr;
Ben Hutchings3a7da392011-03-17 07:34:32 +00002726 u32 rule_cnt = 0, actual_rule_cnt;
2727 u32 ethcmd;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002728 u32 data;
Ben Hutchings3a7da392011-03-17 07:34:32 +00002729 int ret;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002730
2731 if (get_user(data, &ifr32->ifr_ifru.ifru_data))
2732 return -EFAULT;
2733
Ben Hutchings3a7da392011-03-17 07:34:32 +00002734 compat_rxnfc = compat_ptr(data);
2735
2736 if (get_user(ethcmd, &compat_rxnfc->cmd))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002737 return -EFAULT;
2738
Ben Hutchings3a7da392011-03-17 07:34:32 +00002739 /* Most ethtool structures are defined without padding.
2740 * Unfortunately struct ethtool_rxnfc is an exception.
2741 */
2742 switch (ethcmd) {
2743 default:
2744 break;
2745 case ETHTOOL_GRXCLSRLALL:
2746 /* Buffer size is variable */
2747 if (get_user(rule_cnt, &compat_rxnfc->rule_cnt))
2748 return -EFAULT;
2749 if (rule_cnt > KMALLOC_MAX_SIZE / sizeof(u32))
2750 return -ENOMEM;
2751 buf_size += rule_cnt * sizeof(u32);
2752 /* fall through */
2753 case ETHTOOL_GRXRINGS:
2754 case ETHTOOL_GRXCLSRLCNT:
2755 case ETHTOOL_GRXCLSRULE:
Ben Hutchings55664f32012-01-03 12:04:51 +00002756 case ETHTOOL_SRXCLSRLINS:
Ben Hutchings3a7da392011-03-17 07:34:32 +00002757 convert_out = true;
2758 /* fall through */
2759 case ETHTOOL_SRXCLSRLDEL:
Ben Hutchings3a7da392011-03-17 07:34:32 +00002760 buf_size += sizeof(struct ethtool_rxnfc);
2761 convert_in = true;
2762 break;
2763 }
2764
2765 ifr = compat_alloc_user_space(buf_size);
Stephen Hemminger954b1242013-02-11 06:22:28 +00002766 rxnfc = (void __user *)ifr + ALIGN(sizeof(struct ifreq), 8);
Ben Hutchings3a7da392011-03-17 07:34:32 +00002767
2768 if (copy_in_user(&ifr->ifr_name, &ifr32->ifr_name, IFNAMSIZ))
2769 return -EFAULT;
2770
2771 if (put_user(convert_in ? rxnfc : compat_ptr(data),
2772 &ifr->ifr_ifru.ifru_data))
2773 return -EFAULT;
2774
2775 if (convert_in) {
Alexander Duyck127fe532011-04-08 18:01:59 +00002776 /* We expect there to be holes between fs.m_ext and
Ben Hutchings3a7da392011-03-17 07:34:32 +00002777 * fs.ring_cookie and at the end of fs, but nowhere else.
2778 */
Alexander Duyck127fe532011-04-08 18:01:59 +00002779 BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc, fs.m_ext) +
2780 sizeof(compat_rxnfc->fs.m_ext) !=
2781 offsetof(struct ethtool_rxnfc, fs.m_ext) +
2782 sizeof(rxnfc->fs.m_ext));
Ben Hutchings3a7da392011-03-17 07:34:32 +00002783 BUILD_BUG_ON(
2784 offsetof(struct compat_ethtool_rxnfc, fs.location) -
2785 offsetof(struct compat_ethtool_rxnfc, fs.ring_cookie) !=
2786 offsetof(struct ethtool_rxnfc, fs.location) -
2787 offsetof(struct ethtool_rxnfc, fs.ring_cookie));
2788
2789 if (copy_in_user(rxnfc, compat_rxnfc,
Stephen Hemminger954b1242013-02-11 06:22:28 +00002790 (void __user *)(&rxnfc->fs.m_ext + 1) -
2791 (void __user *)rxnfc) ||
Ben Hutchings3a7da392011-03-17 07:34:32 +00002792 copy_in_user(&rxnfc->fs.ring_cookie,
2793 &compat_rxnfc->fs.ring_cookie,
Stephen Hemminger954b1242013-02-11 06:22:28 +00002794 (void __user *)(&rxnfc->fs.location + 1) -
2795 (void __user *)&rxnfc->fs.ring_cookie) ||
Ben Hutchings3a7da392011-03-17 07:34:32 +00002796 copy_in_user(&rxnfc->rule_cnt, &compat_rxnfc->rule_cnt,
2797 sizeof(rxnfc->rule_cnt)))
2798 return -EFAULT;
2799 }
2800
2801 ret = dev_ioctl(net, SIOCETHTOOL, ifr);
2802 if (ret)
2803 return ret;
2804
2805 if (convert_out) {
2806 if (copy_in_user(compat_rxnfc, rxnfc,
Stephen Hemminger954b1242013-02-11 06:22:28 +00002807 (const void __user *)(&rxnfc->fs.m_ext + 1) -
2808 (const void __user *)rxnfc) ||
Ben Hutchings3a7da392011-03-17 07:34:32 +00002809 copy_in_user(&compat_rxnfc->fs.ring_cookie,
2810 &rxnfc->fs.ring_cookie,
Stephen Hemminger954b1242013-02-11 06:22:28 +00002811 (const void __user *)(&rxnfc->fs.location + 1) -
2812 (const void __user *)&rxnfc->fs.ring_cookie) ||
Ben Hutchings3a7da392011-03-17 07:34:32 +00002813 copy_in_user(&compat_rxnfc->rule_cnt, &rxnfc->rule_cnt,
2814 sizeof(rxnfc->rule_cnt)))
2815 return -EFAULT;
2816
2817 if (ethcmd == ETHTOOL_GRXCLSRLALL) {
2818 /* As an optimisation, we only copy the actual
2819 * number of rules that the underlying
2820 * function returned. Since Mallory might
2821 * change the rule count in user memory, we
2822 * check that it is less than the rule count
2823 * originally given (as the user buffer size),
2824 * which has been range-checked.
2825 */
2826 if (get_user(actual_rule_cnt, &rxnfc->rule_cnt))
2827 return -EFAULT;
2828 if (actual_rule_cnt < rule_cnt)
2829 rule_cnt = actual_rule_cnt;
2830 if (copy_in_user(&compat_rxnfc->rule_locs[0],
2831 &rxnfc->rule_locs[0],
2832 rule_cnt * sizeof(u32)))
2833 return -EFAULT;
2834 }
2835 }
2836
2837 return 0;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002838}
2839
Arnd Bergmann7a50a242009-11-08 20:57:03 -08002840static int compat_siocwandev(struct net *net, struct compat_ifreq __user *uifr32)
2841{
2842 void __user *uptr;
2843 compat_uptr_t uptr32;
2844 struct ifreq __user *uifr;
2845
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002846 uifr = compat_alloc_user_space(sizeof(*uifr));
Arnd Bergmann7a50a242009-11-08 20:57:03 -08002847 if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
2848 return -EFAULT;
2849
2850 if (get_user(uptr32, &uifr32->ifr_settings.ifs_ifsu))
2851 return -EFAULT;
2852
2853 uptr = compat_ptr(uptr32);
2854
2855 if (put_user(uptr, &uifr->ifr_settings.ifs_ifsu.raw_hdlc))
2856 return -EFAULT;
2857
2858 return dev_ioctl(net, SIOCWANDEV, uifr);
2859}
2860
Arnd Bergmann6b960182009-11-06 23:10:54 -08002861static int bond_ioctl(struct net *net, unsigned int cmd,
2862 struct compat_ifreq __user *ifr32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002863{
2864 struct ifreq kifr;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002865 mm_segment_t old_fs;
2866 int err;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002867
2868 switch (cmd) {
2869 case SIOCBONDENSLAVE:
2870 case SIOCBONDRELEASE:
2871 case SIOCBONDSETHWADDR:
2872 case SIOCBONDCHANGEACTIVE:
Arnd Bergmann6b960182009-11-06 23:10:54 -08002873 if (copy_from_user(&kifr, ifr32, sizeof(struct compat_ifreq)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002874 return -EFAULT;
2875
2876 old_fs = get_fs();
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002877 set_fs(KERNEL_DS);
stephen hemmingerc3f52ae2011-02-23 09:06:48 +00002878 err = dev_ioctl(net, cmd,
2879 (struct ifreq __user __force *) &kifr);
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002880 set_fs(old_fs);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002881
2882 return err;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002883 default:
Linus Torvalds07d106d2012-01-05 15:40:12 -08002884 return -ENOIOCTLCMD;
Joe Perchesccbd6a52010-05-14 10:58:26 +00002885 }
Arnd Bergmann7a229382009-11-06 23:00:29 -08002886}
2887
Ben Hutchings590d4692013-11-18 17:04:13 +00002888/* Handle ioctls that use ifreq::ifr_data and just need struct ifreq converted */
2889static int compat_ifr_data_ioctl(struct net *net, unsigned int cmd,
Arnd Bergmann6b960182009-11-06 23:10:54 -08002890 struct compat_ifreq __user *u_ifreq32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002891{
2892 struct ifreq __user *u_ifreq64;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002893 char tmp_buf[IFNAMSIZ];
2894 void __user *data64;
2895 u32 data32;
2896
2897 if (copy_from_user(&tmp_buf[0], &(u_ifreq32->ifr_ifrn.ifrn_name[0]),
2898 IFNAMSIZ))
2899 return -EFAULT;
Ben Hutchings417c3522013-11-18 17:04:58 +00002900 if (get_user(data32, &u_ifreq32->ifr_ifru.ifru_data))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002901 return -EFAULT;
2902 data64 = compat_ptr(data32);
2903
2904 u_ifreq64 = compat_alloc_user_space(sizeof(*u_ifreq64));
2905
Arnd Bergmann7a229382009-11-06 23:00:29 -08002906 if (copy_to_user(&u_ifreq64->ifr_ifrn.ifrn_name[0], &tmp_buf[0],
2907 IFNAMSIZ))
2908 return -EFAULT;
Ben Hutchings417c3522013-11-18 17:04:58 +00002909 if (put_user(data64, &u_ifreq64->ifr_ifru.ifru_data))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002910 return -EFAULT;
2911
Arnd Bergmann6b960182009-11-06 23:10:54 -08002912 return dev_ioctl(net, cmd, u_ifreq64);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002913}
2914
Arnd Bergmann6b960182009-11-06 23:10:54 -08002915static int dev_ifsioc(struct net *net, struct socket *sock,
2916 unsigned int cmd, struct compat_ifreq __user *uifr32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002917{
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00002918 struct ifreq __user *uifr;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002919 int err;
2920
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00002921 uifr = compat_alloc_user_space(sizeof(*uifr));
2922 if (copy_in_user(uifr, uifr32, sizeof(*uifr32)))
2923 return -EFAULT;
2924
2925 err = sock_do_ioctl(net, sock, cmd, (unsigned long)uifr);
2926
Arnd Bergmann7a229382009-11-06 23:00:29 -08002927 if (!err) {
2928 switch (cmd) {
2929 case SIOCGIFFLAGS:
2930 case SIOCGIFMETRIC:
2931 case SIOCGIFMTU:
2932 case SIOCGIFMEM:
2933 case SIOCGIFHWADDR:
2934 case SIOCGIFINDEX:
2935 case SIOCGIFADDR:
2936 case SIOCGIFBRDADDR:
2937 case SIOCGIFDSTADDR:
2938 case SIOCGIFNETMASK:
Arnd Bergmannfab25322009-11-08 20:56:21 -08002939 case SIOCGIFPFLAGS:
Arnd Bergmann7a229382009-11-06 23:00:29 -08002940 case SIOCGIFTXQLEN:
Arnd Bergmannfab25322009-11-08 20:56:21 -08002941 case SIOCGMIIPHY:
2942 case SIOCGMIIREG:
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00002943 if (copy_in_user(uifr32, uifr, sizeof(*uifr32)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002944 err = -EFAULT;
2945 break;
2946 }
2947 }
2948 return err;
2949}
2950
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00002951static int compat_sioc_ifmap(struct net *net, unsigned int cmd,
2952 struct compat_ifreq __user *uifr32)
2953{
2954 struct ifreq ifr;
2955 struct compat_ifmap __user *uifmap32;
2956 mm_segment_t old_fs;
2957 int err;
2958
2959 uifmap32 = &uifr32->ifr_ifru.ifru_map;
2960 err = copy_from_user(&ifr, uifr32, sizeof(ifr.ifr_name));
Mathieu Desnoyers3ddc5b42013-09-11 14:23:18 -07002961 err |= get_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
2962 err |= get_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
2963 err |= get_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
2964 err |= get_user(ifr.ifr_map.irq, &uifmap32->irq);
2965 err |= get_user(ifr.ifr_map.dma, &uifmap32->dma);
2966 err |= get_user(ifr.ifr_map.port, &uifmap32->port);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00002967 if (err)
2968 return -EFAULT;
2969
2970 old_fs = get_fs();
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002971 set_fs(KERNEL_DS);
stephen hemmingerc3f52ae2011-02-23 09:06:48 +00002972 err = dev_ioctl(net, cmd, (void __user __force *)&ifr);
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002973 set_fs(old_fs);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00002974
2975 if (cmd == SIOCGIFMAP && !err) {
2976 err = copy_to_user(uifr32, &ifr, sizeof(ifr.ifr_name));
Mathieu Desnoyers3ddc5b42013-09-11 14:23:18 -07002977 err |= put_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
2978 err |= put_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
2979 err |= put_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
2980 err |= put_user(ifr.ifr_map.irq, &uifmap32->irq);
2981 err |= put_user(ifr.ifr_map.dma, &uifmap32->dma);
2982 err |= put_user(ifr.ifr_map.port, &uifmap32->port);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00002983 if (err)
2984 err = -EFAULT;
2985 }
2986 return err;
2987}
2988
Arnd Bergmann7a229382009-11-06 23:00:29 -08002989struct rtentry32 {
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002990 u32 rt_pad1;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002991 struct sockaddr rt_dst; /* target address */
2992 struct sockaddr rt_gateway; /* gateway addr (RTF_GATEWAY) */
2993 struct sockaddr rt_genmask; /* target network mask (IP) */
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002994 unsigned short rt_flags;
2995 short rt_pad2;
2996 u32 rt_pad3;
2997 unsigned char rt_tos;
2998 unsigned char rt_class;
2999 short rt_pad4;
3000 short rt_metric; /* +1 for binary compatibility! */
Arnd Bergmann7a229382009-11-06 23:00:29 -08003001 /* char * */ u32 rt_dev; /* forcing the device at add */
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003002 u32 rt_mtu; /* per route MTU/Window */
3003 u32 rt_window; /* Window clamping */
Arnd Bergmann7a229382009-11-06 23:00:29 -08003004 unsigned short rt_irtt; /* Initial RTT */
3005};
3006
3007struct in6_rtmsg32 {
3008 struct in6_addr rtmsg_dst;
3009 struct in6_addr rtmsg_src;
3010 struct in6_addr rtmsg_gateway;
3011 u32 rtmsg_type;
3012 u16 rtmsg_dst_len;
3013 u16 rtmsg_src_len;
3014 u32 rtmsg_metric;
3015 u32 rtmsg_info;
3016 u32 rtmsg_flags;
3017 s32 rtmsg_ifindex;
3018};
3019
Arnd Bergmann6b960182009-11-06 23:10:54 -08003020static int routing_ioctl(struct net *net, struct socket *sock,
3021 unsigned int cmd, void __user *argp)
Arnd Bergmann7a229382009-11-06 23:00:29 -08003022{
3023 int ret;
3024 void *r = NULL;
3025 struct in6_rtmsg r6;
3026 struct rtentry r4;
3027 char devname[16];
3028 u32 rtdev;
3029 mm_segment_t old_fs = get_fs();
3030
Arnd Bergmann6b960182009-11-06 23:10:54 -08003031 if (sock && sock->sk && sock->sk->sk_family == AF_INET6) { /* ipv6 */
3032 struct in6_rtmsg32 __user *ur6 = argp;
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003033 ret = copy_from_user(&r6.rtmsg_dst, &(ur6->rtmsg_dst),
Arnd Bergmann7a229382009-11-06 23:00:29 -08003034 3 * sizeof(struct in6_addr));
Mathieu Desnoyers3ddc5b42013-09-11 14:23:18 -07003035 ret |= get_user(r6.rtmsg_type, &(ur6->rtmsg_type));
3036 ret |= get_user(r6.rtmsg_dst_len, &(ur6->rtmsg_dst_len));
3037 ret |= get_user(r6.rtmsg_src_len, &(ur6->rtmsg_src_len));
3038 ret |= get_user(r6.rtmsg_metric, &(ur6->rtmsg_metric));
3039 ret |= get_user(r6.rtmsg_info, &(ur6->rtmsg_info));
3040 ret |= get_user(r6.rtmsg_flags, &(ur6->rtmsg_flags));
3041 ret |= get_user(r6.rtmsg_ifindex, &(ur6->rtmsg_ifindex));
Arnd Bergmann7a229382009-11-06 23:00:29 -08003042
3043 r = (void *) &r6;
3044 } else { /* ipv4 */
Arnd Bergmann6b960182009-11-06 23:10:54 -08003045 struct rtentry32 __user *ur4 = argp;
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003046 ret = copy_from_user(&r4.rt_dst, &(ur4->rt_dst),
Arnd Bergmann7a229382009-11-06 23:00:29 -08003047 3 * sizeof(struct sockaddr));
Mathieu Desnoyers3ddc5b42013-09-11 14:23:18 -07003048 ret |= get_user(r4.rt_flags, &(ur4->rt_flags));
3049 ret |= get_user(r4.rt_metric, &(ur4->rt_metric));
3050 ret |= get_user(r4.rt_mtu, &(ur4->rt_mtu));
3051 ret |= get_user(r4.rt_window, &(ur4->rt_window));
3052 ret |= get_user(r4.rt_irtt, &(ur4->rt_irtt));
3053 ret |= get_user(rtdev, &(ur4->rt_dev));
Arnd Bergmann7a229382009-11-06 23:00:29 -08003054 if (rtdev) {
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003055 ret |= copy_from_user(devname, compat_ptr(rtdev), 15);
stephen hemmingerc3f52ae2011-02-23 09:06:48 +00003056 r4.rt_dev = (char __user __force *)devname;
3057 devname[15] = 0;
Arnd Bergmann7a229382009-11-06 23:00:29 -08003058 } else
3059 r4.rt_dev = NULL;
3060
3061 r = (void *) &r4;
3062 }
3063
3064 if (ret) {
3065 ret = -EFAULT;
3066 goto out;
3067 }
3068
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003069 set_fs(KERNEL_DS);
Arnd Bergmann6b960182009-11-06 23:10:54 -08003070 ret = sock_do_ioctl(net, sock, cmd, (unsigned long) r);
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003071 set_fs(old_fs);
Arnd Bergmann7a229382009-11-06 23:00:29 -08003072
3073out:
Arnd Bergmann7a229382009-11-06 23:00:29 -08003074 return ret;
3075}
3076
3077/* Since old style bridge ioctl's endup using SIOCDEVPRIVATE
3078 * for some operations; this forces use of the newer bridge-utils that
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003079 * use compatible ioctls
Arnd Bergmann7a229382009-11-06 23:00:29 -08003080 */
Arnd Bergmann6b960182009-11-06 23:10:54 -08003081static int old_bridge_ioctl(compat_ulong_t __user *argp)
Arnd Bergmann7a229382009-11-06 23:00:29 -08003082{
Arnd Bergmann6b960182009-11-06 23:10:54 -08003083 compat_ulong_t tmp;
Arnd Bergmann7a229382009-11-06 23:00:29 -08003084
Arnd Bergmann6b960182009-11-06 23:10:54 -08003085 if (get_user(tmp, argp))
Arnd Bergmann7a229382009-11-06 23:00:29 -08003086 return -EFAULT;
3087 if (tmp == BRCTL_GET_VERSION)
3088 return BRCTL_VERSION + 1;
3089 return -EINVAL;
3090}
3091
Arnd Bergmann6b960182009-11-06 23:10:54 -08003092static int compat_sock_ioctl_trans(struct file *file, struct socket *sock,
3093 unsigned int cmd, unsigned long arg)
3094{
3095 void __user *argp = compat_ptr(arg);
3096 struct sock *sk = sock->sk;
3097 struct net *net = sock_net(sk);
Arnd Bergmann7a229382009-11-06 23:00:29 -08003098
Arnd Bergmann6b960182009-11-06 23:10:54 -08003099 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15))
Ben Hutchings590d4692013-11-18 17:04:13 +00003100 return compat_ifr_data_ioctl(net, cmd, argp);
Arnd Bergmann7a229382009-11-06 23:00:29 -08003101
Arnd Bergmann6b960182009-11-06 23:10:54 -08003102 switch (cmd) {
3103 case SIOCSIFBR:
3104 case SIOCGIFBR:
3105 return old_bridge_ioctl(argp);
3106 case SIOCGIFNAME:
3107 return dev_ifname32(net, argp);
3108 case SIOCGIFCONF:
3109 return dev_ifconf(net, argp);
3110 case SIOCETHTOOL:
3111 return ethtool_ioctl(net, argp);
Arnd Bergmann7a50a242009-11-08 20:57:03 -08003112 case SIOCWANDEV:
3113 return compat_siocwandev(net, argp);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003114 case SIOCGIFMAP:
3115 case SIOCSIFMAP:
3116 return compat_sioc_ifmap(net, cmd, argp);
Arnd Bergmann6b960182009-11-06 23:10:54 -08003117 case SIOCBONDENSLAVE:
3118 case SIOCBONDRELEASE:
3119 case SIOCBONDSETHWADDR:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003120 case SIOCBONDCHANGEACTIVE:
3121 return bond_ioctl(net, cmd, argp);
3122 case SIOCADDRT:
3123 case SIOCDELRT:
3124 return routing_ioctl(net, sock, cmd, argp);
3125 case SIOCGSTAMP:
3126 return do_siocgstamp(net, sock, cmd, argp);
3127 case SIOCGSTAMPNS:
3128 return do_siocgstampns(net, sock, cmd, argp);
Ben Hutchings590d4692013-11-18 17:04:13 +00003129 case SIOCBONDSLAVEINFOQUERY:
3130 case SIOCBONDINFOQUERY:
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003131 case SIOCSHWTSTAMP:
Ben Hutchingsfd468c72013-11-14 01:19:29 +00003132 case SIOCGHWTSTAMP:
Ben Hutchings590d4692013-11-18 17:04:13 +00003133 return compat_ifr_data_ioctl(net, cmd, argp);
Arnd Bergmann7a229382009-11-06 23:00:29 -08003134
Arnd Bergmann6b960182009-11-06 23:10:54 -08003135 case FIOSETOWN:
3136 case SIOCSPGRP:
3137 case FIOGETOWN:
3138 case SIOCGPGRP:
3139 case SIOCBRADDBR:
3140 case SIOCBRDELBR:
3141 case SIOCGIFVLAN:
3142 case SIOCSIFVLAN:
3143 case SIOCADDDLCI:
3144 case SIOCDELDLCI:
Andrey Vaginc62cce22016-10-24 18:29:13 -07003145 case SIOCGSKNS:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003146 return sock_ioctl(file, cmd, arg);
3147
3148 case SIOCGIFFLAGS:
3149 case SIOCSIFFLAGS:
3150 case SIOCGIFMETRIC:
3151 case SIOCSIFMETRIC:
3152 case SIOCGIFMTU:
3153 case SIOCSIFMTU:
3154 case SIOCGIFMEM:
3155 case SIOCSIFMEM:
3156 case SIOCGIFHWADDR:
3157 case SIOCSIFHWADDR:
3158 case SIOCADDMULTI:
3159 case SIOCDELMULTI:
3160 case SIOCGIFINDEX:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003161 case SIOCGIFADDR:
3162 case SIOCSIFADDR:
3163 case SIOCSIFHWBROADCAST:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003164 case SIOCDIFADDR:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003165 case SIOCGIFBRDADDR:
3166 case SIOCSIFBRDADDR:
3167 case SIOCGIFDSTADDR:
3168 case SIOCSIFDSTADDR:
3169 case SIOCGIFNETMASK:
3170 case SIOCSIFNETMASK:
3171 case SIOCSIFPFLAGS:
3172 case SIOCGIFPFLAGS:
3173 case SIOCGIFTXQLEN:
3174 case SIOCSIFTXQLEN:
3175 case SIOCBRADDIF:
3176 case SIOCBRDELIF:
Arnd Bergmann9177efd2009-11-06 08:09:09 +00003177 case SIOCSIFNAME:
3178 case SIOCGMIIPHY:
3179 case SIOCGMIIREG:
3180 case SIOCSMIIREG:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003181 return dev_ifsioc(net, sock, cmd, argp);
Arnd Bergmann9177efd2009-11-06 08:09:09 +00003182
Arnd Bergmann6b960182009-11-06 23:10:54 -08003183 case SIOCSARP:
3184 case SIOCGARP:
3185 case SIOCDARP:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003186 case SIOCATMARK:
Arnd Bergmann9177efd2009-11-06 08:09:09 +00003187 return sock_do_ioctl(net, sock, cmd, arg);
3188 }
3189
Arnd Bergmann6b960182009-11-06 23:10:54 -08003190 return -ENOIOCTLCMD;
3191}
Arnd Bergmann7a229382009-11-06 23:00:29 -08003192
Eric Dumazet95c96172012-04-15 05:58:06 +00003193static long compat_sock_ioctl(struct file *file, unsigned int cmd,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07003194 unsigned long arg)
Shaun Pereira89bbfc92006-03-21 23:58:08 -08003195{
3196 struct socket *sock = file->private_data;
3197 int ret = -ENOIOCTLCMD;
David S. Miller87de87d2008-06-03 09:14:03 -07003198 struct sock *sk;
3199 struct net *net;
3200
3201 sk = sock->sk;
3202 net = sock_net(sk);
Shaun Pereira89bbfc92006-03-21 23:58:08 -08003203
3204 if (sock->ops->compat_ioctl)
3205 ret = sock->ops->compat_ioctl(sock, cmd, arg);
3206
David S. Miller87de87d2008-06-03 09:14:03 -07003207 if (ret == -ENOIOCTLCMD &&
3208 (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST))
3209 ret = compat_wext_handle_ioctl(net, cmd, arg);
3210
Arnd Bergmann6b960182009-11-06 23:10:54 -08003211 if (ret == -ENOIOCTLCMD)
3212 ret = compat_sock_ioctl_trans(file, sock, cmd, arg);
3213
Shaun Pereira89bbfc92006-03-21 23:58:08 -08003214 return ret;
3215}
3216#endif
3217
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003218int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen)
3219{
3220 return sock->ops->bind(sock, addr, addrlen);
3221}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003222EXPORT_SYMBOL(kernel_bind);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003223
3224int kernel_listen(struct socket *sock, int backlog)
3225{
3226 return sock->ops->listen(sock, backlog);
3227}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003228EXPORT_SYMBOL(kernel_listen);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003229
3230int kernel_accept(struct socket *sock, struct socket **newsock, int flags)
3231{
3232 struct sock *sk = sock->sk;
3233 int err;
3234
3235 err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
3236 newsock);
3237 if (err < 0)
3238 goto done;
3239
3240 err = sock->ops->accept(sock, *newsock, flags);
3241 if (err < 0) {
3242 sock_release(*newsock);
Tony Battersbyfa8705b2007-10-10 21:09:04 -07003243 *newsock = NULL;
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003244 goto done;
3245 }
3246
3247 (*newsock)->ops = sock->ops;
Wei Yongjun1b085342008-12-18 19:35:10 -08003248 __module_get((*newsock)->ops->owner);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003249
3250done:
3251 return err;
3252}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003253EXPORT_SYMBOL(kernel_accept);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003254
3255int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
YOSHIFUJI Hideaki4768fbc2007-02-09 23:25:31 +09003256 int flags)
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003257{
3258 return sock->ops->connect(sock, addr, addrlen, flags);
3259}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003260EXPORT_SYMBOL(kernel_connect);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003261
3262int kernel_getsockname(struct socket *sock, struct sockaddr *addr,
3263 int *addrlen)
3264{
3265 return sock->ops->getname(sock, addr, addrlen, 0);
3266}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003267EXPORT_SYMBOL(kernel_getsockname);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003268
3269int kernel_getpeername(struct socket *sock, struct sockaddr *addr,
3270 int *addrlen)
3271{
3272 return sock->ops->getname(sock, addr, addrlen, 1);
3273}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003274EXPORT_SYMBOL(kernel_getpeername);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003275
3276int kernel_getsockopt(struct socket *sock, int level, int optname,
3277 char *optval, int *optlen)
3278{
3279 mm_segment_t oldfs = get_fs();
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003280 char __user *uoptval;
3281 int __user *uoptlen;
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003282 int err;
3283
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003284 uoptval = (char __user __force *) optval;
3285 uoptlen = (int __user __force *) optlen;
3286
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003287 set_fs(KERNEL_DS);
3288 if (level == SOL_SOCKET)
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003289 err = sock_getsockopt(sock, level, optname, uoptval, uoptlen);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003290 else
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003291 err = sock->ops->getsockopt(sock, level, optname, uoptval,
3292 uoptlen);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003293 set_fs(oldfs);
3294 return err;
3295}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003296EXPORT_SYMBOL(kernel_getsockopt);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003297
3298int kernel_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07003299 char *optval, unsigned int optlen)
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003300{
3301 mm_segment_t oldfs = get_fs();
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003302 char __user *uoptval;
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003303 int err;
3304
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003305 uoptval = (char __user __force *) optval;
3306
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003307 set_fs(KERNEL_DS);
3308 if (level == SOL_SOCKET)
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003309 err = sock_setsockopt(sock, level, optname, uoptval, optlen);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003310 else
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003311 err = sock->ops->setsockopt(sock, level, optname, uoptval,
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003312 optlen);
3313 set_fs(oldfs);
3314 return err;
3315}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003316EXPORT_SYMBOL(kernel_setsockopt);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003317
3318int kernel_sendpage(struct socket *sock, struct page *page, int offset,
3319 size_t size, int flags)
3320{
3321 if (sock->ops->sendpage)
3322 return sock->ops->sendpage(sock, page, offset, size, flags);
3323
3324 return sock_no_sendpage(sock, page, offset, size, flags);
3325}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003326EXPORT_SYMBOL(kernel_sendpage);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003327
3328int kernel_sock_ioctl(struct socket *sock, int cmd, unsigned long arg)
3329{
3330 mm_segment_t oldfs = get_fs();
3331 int err;
3332
3333 set_fs(KERNEL_DS);
3334 err = sock->ops->ioctl(sock, cmd, arg);
3335 set_fs(oldfs);
3336
3337 return err;
3338}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003339EXPORT_SYMBOL(kernel_sock_ioctl);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003340
Trond Myklebust91cf45f2007-11-12 18:10:39 -08003341int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how)
3342{
3343 return sock->ops->shutdown(sock, how);
3344}
Trond Myklebust91cf45f2007-11-12 18:10:39 -08003345EXPORT_SYMBOL(kernel_sock_shutdown);