blob: 5b0126234606dac94152b69fb42a571b1f3cc7bf [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * NET An implementation of the SOCKET network access protocol.
3 *
4 * Version: @(#)socket.c 1.1.93 18/02/95
5 *
6 * Authors: Orest Zborowski, <obz@Kodak.COM>
Jesper Juhl02c30a82005-05-05 16:16:16 -07007 * Ross Biro
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
9 *
10 * Fixes:
11 * Anonymous : NOTSOCK/BADF cleanup. Error fix in
12 * shutdown()
13 * Alan Cox : verify_area() fixes
14 * Alan Cox : Removed DDI
15 * Jonathan Kamens : SOCK_DGRAM reconnect bug
16 * Alan Cox : Moved a load of checks to the very
17 * top level.
18 * Alan Cox : Move address structures to/from user
19 * mode above the protocol layers.
20 * Rob Janssen : Allow 0 length sends.
21 * Alan Cox : Asynchronous I/O support (cribbed from the
22 * tty drivers).
23 * Niibe Yutaka : Asynchronous I/O for writes (4.4BSD style)
24 * Jeff Uphoff : Made max number of sockets command-line
25 * configurable.
26 * Matti Aarnio : Made the number of sockets dynamic,
27 * to be allocated when needed, and mr.
28 * Uphoff's max is used as max to be
29 * allowed to allocate.
30 * Linus : Argh. removed all the socket allocation
31 * altogether: it's in the inode now.
32 * Alan Cox : Made sock_alloc()/sock_release() public
33 * for NetROM and future kernel nfsd type
34 * stuff.
35 * Alan Cox : sendmsg/recvmsg basics.
36 * Tom Dyas : Export net symbols.
37 * Marcin Dalecki : Fixed problems with CONFIG_NET="n".
38 * Alan Cox : Added thread locking to sys_* calls
39 * for sockets. May have errors at the
40 * moment.
41 * Kevin Buhr : Fixed the dumb errors in the above.
42 * Andi Kleen : Some small cleanups, optimizations,
43 * and fixed a copy_from_user() bug.
44 * Tigran Aivazian : sys_send(args) calls sys_sendto(args, NULL, 0)
Stephen Hemminger89bddce2006-09-01 00:19:31 -070045 * Tigran Aivazian : Made listen(2) backlog sanity checks
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 * protocol-independent
47 *
48 *
49 * This program is free software; you can redistribute it and/or
50 * modify it under the terms of the GNU General Public License
51 * as published by the Free Software Foundation; either version
52 * 2 of the License, or (at your option) any later version.
53 *
54 *
55 * This module is effectively the top level interface to the BSD socket
Stephen Hemminger89bddce2006-09-01 00:19:31 -070056 * paradigm.
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 *
58 * Based upon Swansea University Computer Society NET3.039
59 */
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <linux/socket.h>
63#include <linux/file.h>
64#include <linux/net.h>
65#include <linux/interrupt.h>
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -070066#include <linux/thread_info.h>
Stephen Hemminger55737fd2006-09-01 00:23:39 -070067#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/netdevice.h>
69#include <linux/proc_fs.h>
70#include <linux/seq_file.h>
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080071#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include <linux/if_bridge.h>
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -030073#include <linux/if_frad.h>
74#include <linux/if_vlan.h>
Daniel Borkmann408eccc2014-04-01 16:20:23 +020075#include <linux/ptp_classify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#include <linux/init.h>
77#include <linux/poll.h>
78#include <linux/cache.h>
79#include <linux/module.h>
80#include <linux/highmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#include <linux/mount.h>
82#include <linux/security.h>
83#include <linux/syscalls.h>
84#include <linux/compat.h>
85#include <linux/kmod.h>
David Woodhouse3ec3b2f2005-05-17 12:08:48 +010086#include <linux/audit.h>
Adrian Bunkd86b5e02006-01-21 00:46:55 +010087#include <linux/wireless.h>
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -070088#include <linux/nsproxy.h>
Nick Black1fd7317d2009-09-22 16:43:33 -070089#include <linux/magic.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090090#include <linux/slab.h>
Masatake YAMATO600e1772012-08-29 10:44:29 +000091#include <linux/xattr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93#include <asm/uaccess.h>
94#include <asm/unistd.h>
95
96#include <net/compat.h>
David S. Miller87de87d2008-06-03 09:14:03 -070097#include <net/wext.h>
Herbert Xuf8451722010-05-24 00:12:34 -070098#include <net/cls_cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100#include <net/sock.h>
101#include <linux/netfilter.h>
102
Arnd Bergmann6b960182009-11-06 23:10:54 -0800103#include <linux/if_tun.h>
104#include <linux/ipv6_route.h>
105#include <linux/route.h>
Arnd Bergmann6b960182009-11-06 23:10:54 -0800106#include <linux/sockios.h>
107#include <linux/atalk.h>
Eliezer Tamir076bb0c2013-07-10 17:13:17 +0300108#include <net/busy_poll.h>
Willem de Bruijnf24b9be2014-08-04 22:11:45 -0400109#include <linux/errqueue.h>
Eliezer Tamir06021292013-06-10 11:39:50 +0300110
Cong Wange0d10952013-08-01 11:10:25 +0800111#ifdef CONFIG_NET_RX_BUSY_POLL
Eliezer Tamir64b0dc52013-07-10 17:13:36 +0300112unsigned int sysctl_net_busy_read __read_mostly;
113unsigned int sysctl_net_busy_poll __read_mostly;
Eliezer Tamir06021292013-06-10 11:39:50 +0300114#endif
Arnd Bergmann6b960182009-11-06 23:10:54 -0800115
Al Viro8ae5e032014-11-28 19:40:50 -0500116static ssize_t sock_read_iter(struct kiocb *iocb, struct iov_iter *to);
117static ssize_t sock_write_iter(struct kiocb *iocb, struct iov_iter *from);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700118static int sock_mmap(struct file *file, struct vm_area_struct *vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120static int sock_close(struct inode *inode, struct file *file);
121static unsigned int sock_poll(struct file *file,
122 struct poll_table_struct *wait);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700123static long sock_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
Shaun Pereira89bbfc92006-03-21 23:58:08 -0800124#ifdef CONFIG_COMPAT
125static long compat_sock_ioctl(struct file *file,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700126 unsigned int cmd, unsigned long arg);
Shaun Pereira89bbfc92006-03-21 23:58:08 -0800127#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128static int sock_fasync(int fd, struct file *filp, int on);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129static ssize_t sock_sendpage(struct file *file, struct page *page,
130 int offset, size_t size, loff_t *ppos, int more);
Jens Axboe9c55e012007-11-06 23:30:13 -0800131static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700132 struct pipe_inode_info *pipe, size_t len,
Jens Axboe9c55e012007-11-06 23:30:13 -0800133 unsigned int flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135/*
136 * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
137 * in the operation structures but are done directly via the socketcall() multiplexor.
138 */
139
Arjan van de Venda7071d2007-02-12 00:55:36 -0800140static const struct file_operations socket_file_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 .owner = THIS_MODULE,
142 .llseek = no_llseek,
Al Viro8ae5e032014-11-28 19:40:50 -0500143 .read = new_sync_read,
144 .write = new_sync_write,
145 .read_iter = sock_read_iter,
146 .write_iter = sock_write_iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 .poll = sock_poll,
148 .unlocked_ioctl = sock_ioctl,
Shaun Pereira89bbfc92006-03-21 23:58:08 -0800149#ifdef CONFIG_COMPAT
150 .compat_ioctl = compat_sock_ioctl,
151#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 .mmap = sock_mmap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 .release = sock_close,
154 .fasync = sock_fasync,
Jens Axboe5274f052006-03-30 15:15:30 +0200155 .sendpage = sock_sendpage,
156 .splice_write = generic_splice_sendpage,
Jens Axboe9c55e012007-11-06 23:30:13 -0800157 .splice_read = sock_splice_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158};
159
160/*
161 * The protocol list. Each protocol is registered in here.
162 */
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164static DEFINE_SPINLOCK(net_family_lock);
Eric Dumazet190683a2010-11-10 10:50:44 +0000165static const struct net_proto_family __rcu *net_families[NPROTO] __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167/*
168 * Statistics counters of the socket lists
169 */
170
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700171static DEFINE_PER_CPU(int, sockets_in_use);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700174 * Support routines.
175 * Move socket addresses back and forth across the kernel/user
176 * divide and look after the messy bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 */
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179/**
180 * move_addr_to_kernel - copy a socket address into kernel space
181 * @uaddr: Address in user space
182 * @kaddr: Address in kernel space
183 * @ulen: Length in user space
184 *
185 * The address is copied into kernel space. If the provided address is
186 * too long an error code of -EINVAL is returned. If the copy gives
187 * invalid addresses -EFAULT is returned. On a success 0 is returned.
188 */
189
Maciej Żenczykowski43db3622012-03-11 12:51:50 +0000190int move_addr_to_kernel(void __user *uaddr, int ulen, struct sockaddr_storage *kaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -0700192 if (ulen < 0 || ulen > sizeof(struct sockaddr_storage))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700194 if (ulen == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 return 0;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700196 if (copy_from_user(kaddr, uaddr, ulen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 return -EFAULT;
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100198 return audit_sockaddr(ulen, kaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
200
201/**
202 * move_addr_to_user - copy an address to user space
203 * @kaddr: kernel space address
204 * @klen: length of address in kernel
205 * @uaddr: user space address
206 * @ulen: pointer to user length field
207 *
208 * The value pointed to by ulen on entry is the buffer length available.
209 * This is overwritten with the buffer space used. -EINVAL is returned
210 * if an overlong buffer is specified or a negative buffer size. -EFAULT
211 * is returned if either the buffer or the length field are not
212 * accessible.
213 * After copying the data up to the limit the user specifies, the true
214 * length of the data is written over the length limit the user
215 * specified. Zero is returned for a success.
216 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700217
Maciej Żenczykowski43db3622012-03-11 12:51:50 +0000218static int move_addr_to_user(struct sockaddr_storage *kaddr, int klen,
stephen hemminger11165f12010-10-18 14:27:29 +0000219 void __user *uaddr, int __user *ulen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
221 int err;
222 int len;
223
Hannes Frederic Sowa68c6beb2013-11-21 03:14:34 +0100224 BUG_ON(klen > sizeof(struct sockaddr_storage));
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700225 err = get_user(len, ulen);
226 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 return err;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700228 if (len > klen)
229 len = klen;
Hannes Frederic Sowa68c6beb2013-11-21 03:14:34 +0100230 if (len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700232 if (len) {
Steve Grubbd6fe3942006-03-30 12:20:22 -0500233 if (audit_sockaddr(klen, kaddr))
234 return -ENOMEM;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700235 if (copy_to_user(uaddr, kaddr, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 return -EFAULT;
237 }
238 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700239 * "fromlen shall refer to the value before truncation.."
240 * 1003.1g
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 */
242 return __put_user(klen, ulen);
243}
244
Christoph Lametere18b8902006-12-06 20:33:20 -0800245static struct kmem_cache *sock_inode_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247static struct inode *sock_alloc_inode(struct super_block *sb)
248{
249 struct socket_alloc *ei;
Eric Dumazeteaefd112011-02-18 03:26:36 +0000250 struct socket_wq *wq;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700251
Christoph Lametere94b1762006-12-06 20:33:17 -0800252 ei = kmem_cache_alloc(sock_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 if (!ei)
254 return NULL;
Eric Dumazeteaefd112011-02-18 03:26:36 +0000255 wq = kmalloc(sizeof(*wq), GFP_KERNEL);
256 if (!wq) {
Eric Dumazet43815482010-04-29 11:01:49 +0000257 kmem_cache_free(sock_inode_cachep, ei);
258 return NULL;
259 }
Eric Dumazeteaefd112011-02-18 03:26:36 +0000260 init_waitqueue_head(&wq->wait);
261 wq->fasync_list = NULL;
262 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 |
298 SLAB_MEM_SPREAD),
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]",
317 dentry->d_inode->i_ino);
318}
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
Al Viroc74a1cb2011-01-12 16:59:34 -0500324static struct dentry *sockfs_mount(struct file_system_type *fs_type,
325 int flags, const char *dev_name, void *data)
326{
327 return mount_pseudo(fs_type, "socket:", &sockfs_ops,
328 &sockfs_dentry_operations, SOCKFS_MAGIC);
329}
330
331static struct vfsmount *sock_mnt __read_mostly;
332
333static struct file_system_type sock_fs_type = {
334 .name = "sockfs",
335 .mount = sockfs_mount,
336 .kill_sb = kill_anon_super,
337};
338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339/*
340 * Obtains the first available file descriptor and sets it up for use.
341 *
David S. Miller39d8c1b2006-03-20 17:13:49 -0800342 * These functions create file structures and maps them to fd space
343 * of the current process. On success it returns file descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 * and file struct implicitly stored in sock->file.
345 * Note that another thread may close file descriptor before we return
346 * from this function. We use the fact that now we do not refer
347 * to socket after mapping. If one day we will need it, this
348 * function will increment ref. count on file by 1.
349 *
350 * In any case returned fd MAY BE not valid!
351 * This race condition is unavoidable
352 * with shared fd spaces, we cannot solve it inside kernel,
353 * but we take care of internal coherence yet.
354 */
355
Linus Torvaldsaab174f2012-10-02 20:25:04 -0700356struct file *sock_alloc_file(struct socket *sock, int flags, const char *dname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
Al Viro7cbe66b2009-08-05 19:59:08 +0400358 struct qstr name = { .name = "" };
Al Viro2c48b9c2009-08-09 00:52:35 +0400359 struct path path;
Al Viro7cbe66b2009-08-05 19:59:08 +0400360 struct file *file;
David S. Miller39d8c1b2006-03-20 17:13:49 -0800361
Masatake YAMATO600e1772012-08-29 10:44:29 +0000362 if (dname) {
363 name.name = dname;
364 name.len = strlen(name.name);
365 } else if (sock->sk) {
366 name.name = sock->sk->sk_prot_creator->name;
367 name.len = strlen(name.name);
368 }
Nick Piggin4b936882011-01-07 17:50:07 +1100369 path.dentry = d_alloc_pseudo(sock_mnt->mnt_sb, &name);
Al Viro28407632012-08-17 23:54:15 -0400370 if (unlikely(!path.dentry))
371 return ERR_PTR(-ENOMEM);
Al Viro2c48b9c2009-08-09 00:52:35 +0400372 path.mnt = mntget(sock_mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Al Viro2c48b9c2009-08-09 00:52:35 +0400374 d_instantiate(path.dentry, SOCK_INODE(sock));
Al Virocc3808f2009-08-06 09:43:59 +0400375
Al Viro2c48b9c2009-08-09 00:52:35 +0400376 file = alloc_file(&path, FMODE_READ | FMODE_WRITE,
Al Virocc3808f2009-08-06 09:43:59 +0400377 &socket_file_ops);
Anatol Pomozov39b65252012-09-12 20:11:55 -0700378 if (unlikely(IS_ERR(file))) {
Al Virocc3808f2009-08-06 09:43:59 +0400379 /* drop dentry, keep inode */
Al Viro7de9c6ee2010-10-23 11:11:40 -0400380 ihold(path.dentry->d_inode);
Al Viro2c48b9c2009-08-09 00:52:35 +0400381 path_put(&path);
Anatol Pomozov39b65252012-09-12 20:11:55 -0700382 return file;
Al Virocc3808f2009-08-06 09:43:59 +0400383 }
David S. Miller39d8c1b2006-03-20 17:13:49 -0800384
385 sock->file = file;
Ulrich Drepper77d27202008-07-23 21:29:35 -0700386 file->f_flags = O_RDWR | (flags & O_NONBLOCK);
David S. Miller39d8c1b2006-03-20 17:13:49 -0800387 file->private_data = sock;
Al Viro28407632012-08-17 23:54:15 -0400388 return file;
David S. Miller39d8c1b2006-03-20 17:13:49 -0800389}
Al Viro56b31d12012-08-18 00:25:51 -0400390EXPORT_SYMBOL(sock_alloc_file);
David S. Miller39d8c1b2006-03-20 17:13:49 -0800391
Al Viro56b31d12012-08-18 00:25:51 -0400392static int sock_map_fd(struct socket *sock, int flags)
David S. Miller39d8c1b2006-03-20 17:13:49 -0800393{
394 struct file *newfile;
Al Viro28407632012-08-17 23:54:15 -0400395 int fd = get_unused_fd_flags(flags);
396 if (unlikely(fd < 0))
397 return fd;
David S. Miller39d8c1b2006-03-20 17:13:49 -0800398
Linus Torvaldsaab174f2012-10-02 20:25:04 -0700399 newfile = sock_alloc_file(sock, flags, NULL);
Al Viro28407632012-08-17 23:54:15 -0400400 if (likely(!IS_ERR(newfile))) {
David S. Miller39d8c1b2006-03-20 17:13:49 -0800401 fd_install(fd, newfile);
Al Viro28407632012-08-17 23:54:15 -0400402 return fd;
403 }
Al Viro7cbe66b2009-08-05 19:59:08 +0400404
Al Viro28407632012-08-17 23:54:15 -0400405 put_unused_fd(fd);
406 return PTR_ERR(newfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
John Fastabend406a3c62012-07-20 10:39:25 +0000409struct socket *sock_from_file(struct file *file, int *err)
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800410{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800411 if (file->f_op == &socket_file_ops)
412 return file->private_data; /* set in sock_map_fd */
413
Eric Dumazet23bb80d2007-02-08 14:59:57 -0800414 *err = -ENOTSOCK;
415 return NULL;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800416}
John Fastabend406a3c62012-07-20 10:39:25 +0000417EXPORT_SYMBOL(sock_from_file);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419/**
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700420 * sockfd_lookup - Go from a file number to its socket slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 * @fd: file handle
422 * @err: pointer to an error code return
423 *
424 * The file handle passed in is locked and the socket it is bound
425 * too is returned. If an error occurs the err pointer is overwritten
426 * with a negative errno code and NULL is returned. The function checks
427 * for both invalid handles and passing a handle which is not a socket.
428 *
429 * On a success the socket object pointer is returned.
430 */
431
432struct socket *sockfd_lookup(int fd, int *err)
433{
434 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 struct socket *sock;
436
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700437 file = fget(fd);
438 if (!file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 *err = -EBADF;
440 return NULL;
441 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700442
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800443 sock = sock_from_file(file, err);
444 if (!sock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 fput(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 return sock;
447}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700448EXPORT_SYMBOL(sockfd_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800450static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed)
451{
Al Viro00e188e2014-03-03 23:48:18 -0500452 struct fd f = fdget(fd);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800453 struct socket *sock;
454
Hua Zhong36725582006-04-19 15:25:02 -0700455 *err = -EBADF;
Al Viro00e188e2014-03-03 23:48:18 -0500456 if (f.file) {
457 sock = sock_from_file(f.file, err);
458 if (likely(sock)) {
459 *fput_needed = f.flags;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800460 return sock;
Al Viro00e188e2014-03-03 23:48:18 -0500461 }
462 fdput(f);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800463 }
464 return NULL;
465}
466
Masatake YAMATO600e1772012-08-29 10:44:29 +0000467#define XATTR_SOCKPROTONAME_SUFFIX "sockprotoname"
468#define XATTR_NAME_SOCKPROTONAME (XATTR_SYSTEM_PREFIX XATTR_SOCKPROTONAME_SUFFIX)
469#define XATTR_NAME_SOCKPROTONAME_LEN (sizeof(XATTR_NAME_SOCKPROTONAME)-1)
470static ssize_t sockfs_getxattr(struct dentry *dentry,
471 const char *name, void *value, size_t size)
472{
473 const char *proto_name;
474 size_t proto_size;
475 int error;
476
477 error = -ENODATA;
478 if (!strncmp(name, XATTR_NAME_SOCKPROTONAME, XATTR_NAME_SOCKPROTONAME_LEN)) {
479 proto_name = dentry->d_name.name;
480 proto_size = strlen(proto_name);
481
482 if (value) {
483 error = -ERANGE;
484 if (proto_size + 1 > size)
485 goto out;
486
487 strncpy(value, proto_name, proto_size + 1);
488 }
489 error = proto_size + 1;
490 }
491
492out:
493 return error;
494}
495
496static ssize_t sockfs_listxattr(struct dentry *dentry, char *buffer,
497 size_t size)
498{
499 ssize_t len;
500 ssize_t used = 0;
501
502 len = security_inode_listsecurity(dentry->d_inode, buffer, size);
503 if (len < 0)
504 return len;
505 used += len;
506 if (buffer) {
507 if (size < used)
508 return -ERANGE;
509 buffer += len;
510 }
511
512 len = (XATTR_NAME_SOCKPROTONAME_LEN + 1);
513 used += len;
514 if (buffer) {
515 if (size < used)
516 return -ERANGE;
517 memcpy(buffer, XATTR_NAME_SOCKPROTONAME, len);
518 buffer += len;
519 }
520
521 return used;
522}
523
524static const struct inode_operations sockfs_inode_ops = {
525 .getxattr = sockfs_getxattr,
526 .listxattr = sockfs_listxattr,
527};
528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529/**
530 * sock_alloc - allocate a socket
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700531 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 * Allocate a new inode and socket object. The two are bound together
533 * and initialised. The socket is then returned. If we are out of inodes
534 * NULL is returned.
535 */
536
537static struct socket *sock_alloc(void)
538{
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700539 struct inode *inode;
540 struct socket *sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Eric Dumazeta209dfc2011-07-26 11:36:34 +0200542 inode = new_inode_pseudo(sock_mnt->mnt_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 if (!inode)
544 return NULL;
545
546 sock = SOCKET_I(inode);
547
Eric Dumazet29a020d2009-09-15 02:39:20 -0700548 kmemcheck_annotate_bitfield(sock, type);
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400549 inode->i_ino = get_next_ino();
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700550 inode->i_mode = S_IFSOCK | S_IRWXUGO;
David Howells8192b0c2008-11-14 10:39:10 +1100551 inode->i_uid = current_fsuid();
552 inode->i_gid = current_fsgid();
Masatake YAMATO600e1772012-08-29 10:44:29 +0000553 inode->i_op = &sockfs_inode_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Alex Shi19e8d692012-05-14 14:15:31 -0700555 this_cpu_add(sockets_in_use, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 return sock;
557}
558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559/**
560 * sock_release - close a socket
561 * @sock: socket to close
562 *
563 * The socket is released from the protocol stack if it has a release
564 * callback, and the inode is then released if the socket is bound to
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700565 * an inode not a file.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568void sock_release(struct socket *sock)
569{
570 if (sock->ops) {
571 struct module *owner = sock->ops->owner;
572
573 sock->ops->release(sock);
574 sock->ops = NULL;
575 module_put(owner);
576 }
577
Eric Dumazeteaefd112011-02-18 03:26:36 +0000578 if (rcu_dereference_protected(sock->wq, 1)->fasync_list)
Yang Yingliang3410f222014-02-12 17:09:55 +0800579 pr_err("%s: fasync list not empty!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Mikulas Patockab09e7862012-07-19 06:13:36 +0000581 if (test_bit(SOCK_EXTERNALLY_ALLOCATED, &sock->flags))
582 return;
583
Alex Shi19e8d692012-05-14 14:15:31 -0700584 this_cpu_sub(sockets_in_use, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 if (!sock->file) {
586 iput(SOCK_INODE(sock));
587 return;
588 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700589 sock->file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700591EXPORT_SYMBOL(sock_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Willem de Bruijn67cc0d42014-09-08 19:58:58 -0400593void __sock_tx_timestamp(const struct sock *sk, __u8 *tx_flags)
Patrick Ohly20d49472009-02-12 05:03:38 +0000594{
Eric Dumazet140c55d2014-08-06 11:49:29 +0200595 u8 flags = *tx_flags;
596
Willem de Bruijnb9f40e22014-08-04 22:11:46 -0400597 if (sk->sk_tsflags & SOF_TIMESTAMPING_TX_HARDWARE)
Eric Dumazet140c55d2014-08-06 11:49:29 +0200598 flags |= SKBTX_HW_TSTAMP;
599
Willem de Bruijnb9f40e22014-08-04 22:11:46 -0400600 if (sk->sk_tsflags & SOF_TIMESTAMPING_TX_SOFTWARE)
Eric Dumazet140c55d2014-08-06 11:49:29 +0200601 flags |= SKBTX_SW_TSTAMP;
602
Willem de Bruijne7fd2882014-08-04 22:11:48 -0400603 if (sk->sk_tsflags & SOF_TIMESTAMPING_TX_SCHED)
Eric Dumazet140c55d2014-08-06 11:49:29 +0200604 flags |= SKBTX_SCHED_TSTAMP;
605
Willem de Bruijne1c8a602014-08-04 22:11:50 -0400606 if (sk->sk_tsflags & SOF_TIMESTAMPING_TX_ACK)
Eric Dumazet140c55d2014-08-06 11:49:29 +0200607 flags |= SKBTX_ACK_TSTAMP;
Willem de Bruijne7fd2882014-08-04 22:11:48 -0400608
Eric Dumazet140c55d2014-08-06 11:49:29 +0200609 *tx_flags = flags;
Patrick Ohly20d49472009-02-12 05:03:38 +0000610}
Willem de Bruijn67cc0d42014-09-08 19:58:58 -0400611EXPORT_SYMBOL(__sock_tx_timestamp);
Patrick Ohly20d49472009-02-12 05:03:38 +0000612
Al Virod8725c82014-12-11 00:02:50 -0500613static inline int sock_sendmsg_nosec(struct socket *sock, struct msghdr *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
Al Viro01e97e62014-12-15 21:39:31 -0500615 int ret = sock->ops->sendmsg(sock, msg, msg_data_left(msg));
Al Virod8725c82014-12-11 00:02:50 -0500616 BUG_ON(ret == -EIOCBQUEUED);
617 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618}
Gu Zheng0cf00c62014-12-05 15:14:23 +0800619
Al Virod8725c82014-12-11 00:02:50 -0500620int sock_sendmsg(struct socket *sock, struct msghdr *msg)
Gu Zheng0cf00c62014-12-05 15:14:23 +0800621{
Al Virod8725c82014-12-11 00:02:50 -0500622 int err = security_socket_sendmsg(sock, msg,
Al Viro01e97e62014-12-15 21:39:31 -0500623 msg_data_left(msg));
Ying Xue1b784142015-03-02 15:37:48 +0800624
Al Virod8725c82014-12-11 00:02:50 -0500625 return err ?: sock_sendmsg_nosec(sock, msg);
Gu Zheng0cf00c62014-12-05 15:14:23 +0800626}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700627EXPORT_SYMBOL(sock_sendmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
630 struct kvec *vec, size_t num, size_t size)
631{
Al Viro6aa24812015-03-21 19:56:16 -0400632 iov_iter_kvec(&msg->msg_iter, WRITE | ITER_KVEC, vec, num, size);
Al Virod8725c82014-12-11 00:02:50 -0500633 return sock_sendmsg(sock, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700635EXPORT_SYMBOL(kernel_sendmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700637/*
638 * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
639 */
640void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
641 struct sk_buff *skb)
642{
Patrick Ohly20d49472009-02-12 05:03:38 +0000643 int need_software_tstamp = sock_flag(sk, SOCK_RCVTSTAMP);
Willem de Bruijnf24b9be2014-08-04 22:11:45 -0400644 struct scm_timestamping tss;
Patrick Ohly20d49472009-02-12 05:03:38 +0000645 int empty = 1;
646 struct skb_shared_hwtstamps *shhwtstamps =
647 skb_hwtstamps(skb);
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700648
Patrick Ohly20d49472009-02-12 05:03:38 +0000649 /* Race occurred between timestamp enabling and packet
650 receiving. Fill in the current time for now. */
651 if (need_software_tstamp && skb->tstamp.tv64 == 0)
652 __net_timestamp(skb);
653
654 if (need_software_tstamp) {
655 if (!sock_flag(sk, SOCK_RCVTSTAMPNS)) {
656 struct timeval tv;
657 skb_get_timestamp(skb, &tv);
658 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMP,
659 sizeof(tv), &tv);
660 } else {
Willem de Bruijnf24b9be2014-08-04 22:11:45 -0400661 struct timespec ts;
662 skb_get_timestampns(skb, &ts);
Patrick Ohly20d49472009-02-12 05:03:38 +0000663 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPNS,
Willem de Bruijnf24b9be2014-08-04 22:11:45 -0400664 sizeof(ts), &ts);
Patrick Ohly20d49472009-02-12 05:03:38 +0000665 }
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700666 }
Patrick Ohly20d49472009-02-12 05:03:38 +0000667
Willem de Bruijnf24b9be2014-08-04 22:11:45 -0400668 memset(&tss, 0, sizeof(tss));
Willem de Bruijnc1991052014-09-03 12:01:18 -0400669 if ((sk->sk_tsflags & SOF_TIMESTAMPING_SOFTWARE) &&
Willem de Bruijnf24b9be2014-08-04 22:11:45 -0400670 ktime_to_timespec_cond(skb->tstamp, tss.ts + 0))
Patrick Ohly20d49472009-02-12 05:03:38 +0000671 empty = 0;
Willem de Bruijn4d276eb2014-07-25 18:01:32 -0400672 if (shhwtstamps &&
Willem de Bruijnb9f40e22014-08-04 22:11:46 -0400673 (sk->sk_tsflags & SOF_TIMESTAMPING_RAW_HARDWARE) &&
Willem de Bruijnf24b9be2014-08-04 22:11:45 -0400674 ktime_to_timespec_cond(shhwtstamps->hwtstamp, tss.ts + 2))
Willem de Bruijn4d276eb2014-07-25 18:01:32 -0400675 empty = 0;
Patrick Ohly20d49472009-02-12 05:03:38 +0000676 if (!empty)
677 put_cmsg(msg, SOL_SOCKET,
Willem de Bruijnf24b9be2014-08-04 22:11:45 -0400678 SCM_TIMESTAMPING, sizeof(tss), &tss);
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700679}
Arnaldo Carvalho de Melo7c81fd82007-03-10 00:39:35 -0300680EXPORT_SYMBOL_GPL(__sock_recv_timestamp);
681
Johannes Berg6e3e9392011-11-09 10:15:42 +0100682void __sock_recv_wifi_status(struct msghdr *msg, struct sock *sk,
683 struct sk_buff *skb)
684{
685 int ack;
686
687 if (!sock_flag(sk, SOCK_WIFI_STATUS))
688 return;
689 if (!skb->wifi_acked_valid)
690 return;
691
692 ack = skb->wifi_acked;
693
694 put_cmsg(msg, SOL_SOCKET, SCM_WIFI_STATUS, sizeof(ack), &ack);
695}
696EXPORT_SYMBOL_GPL(__sock_recv_wifi_status);
697
stephen hemminger11165f12010-10-18 14:27:29 +0000698static inline void sock_recv_drops(struct msghdr *msg, struct sock *sk,
699 struct sk_buff *skb)
Neil Horman3b885782009-10-12 13:26:31 -0700700{
Eyal Birger744d5a32015-03-01 14:58:31 +0200701 if (sock_flag(sk, SOCK_RXQ_OVFL) && skb && SOCK_SKB_CB(skb)->dropcount)
Neil Horman3b885782009-10-12 13:26:31 -0700702 put_cmsg(msg, SOL_SOCKET, SO_RXQ_OVFL,
Eyal Birger744d5a32015-03-01 14:58:31 +0200703 sizeof(__u32), &SOCK_SKB_CB(skb)->dropcount);
Neil Horman3b885782009-10-12 13:26:31 -0700704}
705
Eric Dumazet767dd032010-04-28 19:14:43 +0000706void __sock_recv_ts_and_drops(struct msghdr *msg, struct sock *sk,
Neil Horman3b885782009-10-12 13:26:31 -0700707 struct sk_buff *skb)
708{
709 sock_recv_timestamp(msg, sk, skb);
710 sock_recv_drops(msg, sk, skb);
711}
Eric Dumazet767dd032010-04-28 19:14:43 +0000712EXPORT_SYMBOL_GPL(__sock_recv_ts_and_drops);
Neil Horman3b885782009-10-12 13:26:31 -0700713
Ying Xue1b784142015-03-02 15:37:48 +0800714static inline int sock_recvmsg_nosec(struct socket *sock, struct msghdr *msg,
715 size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
Ying Xue1b784142015-03-02 15:37:48 +0800717 return sock->ops->recvmsg(sock, msg, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718}
719
Ying Xue1b784142015-03-02 15:37:48 +0800720int sock_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
721 int flags)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -0700722{
723 int err = security_socket_recvmsg(sock, msg, size, flags);
724
Ying Xue1b784142015-03-02 15:37:48 +0800725 return err ?: sock_recvmsg_nosec(sock, msg, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700727EXPORT_SYMBOL(sock_recvmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Martin Lucinac1249c02010-12-10 00:04:05 +0000729/**
730 * kernel_recvmsg - Receive a message from a socket (kernel space)
731 * @sock: The socket to receive the message from
732 * @msg: Received message
733 * @vec: Input s/g array for message data
734 * @num: Size of input s/g array
735 * @size: Number of bytes to read
736 * @flags: Message flags (MSG_DONTWAIT, etc...)
737 *
738 * On return the msg structure contains the scatter/gather array passed in the
739 * vec argument. The array is modified so that it consists of the unfilled
740 * portion of the original array.
741 *
742 * The returned value is the total number of bytes received, or an error.
743 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700744int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
745 struct kvec *vec, size_t num, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
747 mm_segment_t oldfs = get_fs();
748 int result;
749
Al Viro6aa24812015-03-21 19:56:16 -0400750 iov_iter_kvec(&msg->msg_iter, READ | ITER_KVEC, vec, num, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 set_fs(KERNEL_DS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 result = sock_recvmsg(sock, msg, size, flags);
753 set_fs(oldfs);
754 return result;
755}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700756EXPORT_SYMBOL(kernel_recvmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -0300758static ssize_t sock_sendpage(struct file *file, struct page *page,
759 int offset, size_t size, loff_t *ppos, int more)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
761 struct socket *sock;
762 int flags;
763
Eric Dumazetb69aee02005-09-06 14:42:45 -0700764 sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Eric Dumazet35f9c092012-04-05 03:05:35 +0000766 flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
767 /* more is a combination of MSG_MORE and MSG_SENDPAGE_NOTLAST */
768 flags |= more;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Linus Torvaldse6949582009-08-13 08:28:36 -0700770 return kernel_sendpage(sock, page, offset, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771}
772
Jens Axboe9c55e012007-11-06 23:30:13 -0800773static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700774 struct pipe_inode_info *pipe, size_t len,
Jens Axboe9c55e012007-11-06 23:30:13 -0800775 unsigned int flags)
776{
777 struct socket *sock = file->private_data;
778
Rémi Denis-Courmont997b37d2008-02-15 02:35:45 -0800779 if (unlikely(!sock->ops->splice_read))
780 return -EINVAL;
781
Jens Axboe9c55e012007-11-06 23:30:13 -0800782 return sock->ops->splice_read(sock, ppos, pipe, len, flags);
783}
784
Al Viro8ae5e032014-11-28 19:40:50 -0500785static ssize_t sock_read_iter(struct kiocb *iocb, struct iov_iter *to)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800786{
Al Viro6d652332015-01-30 16:12:56 -0500787 struct file *file = iocb->ki_filp;
788 struct socket *sock = file->private_data;
tadeusz.struk@intel.com0345f932015-03-19 12:31:25 -0700789 struct msghdr msg = {.msg_iter = *to,
790 .msg_iocb = iocb};
Al Viro8ae5e032014-11-28 19:40:50 -0500791 ssize_t res;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800792
Al Viro8ae5e032014-11-28 19:40:50 -0500793 if (file->f_flags & O_NONBLOCK)
794 msg.msg_flags = MSG_DONTWAIT;
795
796 if (iocb->ki_pos != 0)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800797 return -ESPIPE;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700798
Christoph Hellwig66ee59a2015-02-11 19:56:46 +0100799 if (!iov_iter_count(to)) /* Match SYS5 behaviour */
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800800 return 0;
801
Al Viro237dae82015-04-09 00:00:30 -0400802 res = sock_recvmsg(sock, &msg, iov_iter_count(to), msg.msg_flags);
Al Viro8ae5e032014-11-28 19:40:50 -0500803 *to = msg.msg_iter;
804 return res;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800805}
806
Al Viro8ae5e032014-11-28 19:40:50 -0500807static ssize_t sock_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
Al Viro6d652332015-01-30 16:12:56 -0500809 struct file *file = iocb->ki_filp;
810 struct socket *sock = file->private_data;
tadeusz.struk@intel.com0345f932015-03-19 12:31:25 -0700811 struct msghdr msg = {.msg_iter = *from,
812 .msg_iocb = iocb};
Al Viro8ae5e032014-11-28 19:40:50 -0500813 ssize_t res;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800814
Al Viro8ae5e032014-11-28 19:40:50 -0500815 if (iocb->ki_pos != 0)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800816 return -ESPIPE;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700817
Al Viro8ae5e032014-11-28 19:40:50 -0500818 if (file->f_flags & O_NONBLOCK)
819 msg.msg_flags = MSG_DONTWAIT;
820
Al Viro6d652332015-01-30 16:12:56 -0500821 if (sock->type == SOCK_SEQPACKET)
822 msg.msg_flags |= MSG_EOR;
823
Al Virod8725c82014-12-11 00:02:50 -0500824 res = sock_sendmsg(sock, &msg);
Al Viro8ae5e032014-11-28 19:40:50 -0500825 *from = msg.msg_iter;
826 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827}
828
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829/*
830 * Atomic setting of ioctl hooks to avoid race
831 * with module unload.
832 */
833
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800834static DEFINE_MUTEX(br_ioctl_mutex);
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700835static int (*br_ioctl_hook) (struct net *, unsigned int cmd, void __user *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Eric W. Biederman881d9662007-09-17 11:56:21 -0700837void brioctl_set(int (*hook) (struct net *, unsigned int, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800839 mutex_lock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 br_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800841 mutex_unlock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842}
843EXPORT_SYMBOL(brioctl_set);
844
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800845static DEFINE_MUTEX(vlan_ioctl_mutex);
Eric W. Biederman881d9662007-09-17 11:56:21 -0700846static int (*vlan_ioctl_hook) (struct net *, void __user *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Eric W. Biederman881d9662007-09-17 11:56:21 -0700848void vlan_ioctl_set(int (*hook) (struct net *, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800850 mutex_lock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 vlan_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800852 mutex_unlock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853}
854EXPORT_SYMBOL(vlan_ioctl_set);
855
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800856static DEFINE_MUTEX(dlci_ioctl_mutex);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700857static int (*dlci_ioctl_hook) (unsigned int, void __user *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700859void dlci_ioctl_set(int (*hook) (unsigned int, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800861 mutex_lock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 dlci_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800863 mutex_unlock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864}
865EXPORT_SYMBOL(dlci_ioctl_set);
866
Arnd Bergmann6b960182009-11-06 23:10:54 -0800867static long sock_do_ioctl(struct net *net, struct socket *sock,
868 unsigned int cmd, unsigned long arg)
869{
870 int err;
871 void __user *argp = (void __user *)arg;
872
873 err = sock->ops->ioctl(sock, cmd, arg);
874
875 /*
876 * If this ioctl is unknown try to hand it down
877 * to the NIC driver.
878 */
879 if (err == -ENOIOCTLCMD)
880 err = dev_ioctl(net, cmd, argp);
881
882 return err;
883}
884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885/*
886 * With an ioctl, arg may well be a user mode pointer, but we don't know
887 * what to do with it - that's up to the protocol still.
888 */
889
890static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
891{
892 struct socket *sock;
Eric W. Biederman881d9662007-09-17 11:56:21 -0700893 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 void __user *argp = (void __user *)arg;
895 int pid, err;
Eric W. Biederman881d9662007-09-17 11:56:21 -0700896 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Eric Dumazetb69aee02005-09-06 14:42:45 -0700898 sock = file->private_data;
Eric W. Biederman881d9662007-09-17 11:56:21 -0700899 sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900900 net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
Eric W. Biederman881d9662007-09-17 11:56:21 -0700902 err = dev_ioctl(net, cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 } else
Johannes Berg3d23e342009-09-29 23:27:28 +0200904#ifdef CONFIG_WEXT_CORE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
Eric W. Biederman881d9662007-09-17 11:56:21 -0700906 err = dev_ioctl(net, cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 } else
Johannes Berg3d23e342009-09-29 23:27:28 +0200908#endif
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700909 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 case FIOSETOWN:
911 case SIOCSPGRP:
912 err = -EFAULT;
913 if (get_user(pid, (int __user *)argp))
914 break;
Jeff Laytone0b93ed2014-08-22 11:27:32 -0400915 f_setown(sock->file, pid, 1);
916 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 break;
918 case FIOGETOWN:
919 case SIOCGPGRP:
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700920 err = put_user(f_getown(sock->file),
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700921 (int __user *)argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 break;
923 case SIOCGIFBR:
924 case SIOCSIFBR:
925 case SIOCBRADDBR:
926 case SIOCBRDELBR:
927 err = -ENOPKG;
928 if (!br_ioctl_hook)
929 request_module("bridge");
930
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800931 mutex_lock(&br_ioctl_mutex);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700932 if (br_ioctl_hook)
Eric W. Biederman881d9662007-09-17 11:56:21 -0700933 err = br_ioctl_hook(net, cmd, argp);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800934 mutex_unlock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 break;
936 case SIOCGIFVLAN:
937 case SIOCSIFVLAN:
938 err = -ENOPKG;
939 if (!vlan_ioctl_hook)
940 request_module("8021q");
941
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800942 mutex_lock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 if (vlan_ioctl_hook)
Eric W. Biederman881d9662007-09-17 11:56:21 -0700944 err = vlan_ioctl_hook(net, argp);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800945 mutex_unlock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 case SIOCADDDLCI:
948 case SIOCDELDLCI:
949 err = -ENOPKG;
950 if (!dlci_ioctl_hook)
951 request_module("dlci");
952
Pavel Emelyanov7512cbf2008-03-21 15:58:52 -0700953 mutex_lock(&dlci_ioctl_mutex);
954 if (dlci_ioctl_hook)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 err = dlci_ioctl_hook(cmd, argp);
Pavel Emelyanov7512cbf2008-03-21 15:58:52 -0700956 mutex_unlock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 break;
958 default:
Arnd Bergmann6b960182009-11-06 23:10:54 -0800959 err = sock_do_ioctl(net, sock, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 break;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700961 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 return err;
963}
964
965int sock_create_lite(int family, int type, int protocol, struct socket **res)
966{
967 int err;
968 struct socket *sock = NULL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700969
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 err = security_socket_create(family, type, protocol, 1);
971 if (err)
972 goto out;
973
974 sock = sock_alloc();
975 if (!sock) {
976 err = -ENOMEM;
977 goto out;
978 }
979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 sock->type = type;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -0700981 err = security_socket_post_create(sock, family, type, protocol, 1);
982 if (err)
983 goto out_release;
984
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985out:
986 *res = sock;
987 return err;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -0700988out_release:
989 sock_release(sock);
990 sock = NULL;
991 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700993EXPORT_SYMBOL(sock_create_lite);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
995/* No kernel lock held - perfect */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700996static unsigned int sock_poll(struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997{
Eliezer Tamircbf55002013-07-08 16:20:34 +0300998 unsigned int busy_flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 struct socket *sock;
1000
1001 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001002 * We can't return errors to poll, so it's either yes or no.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 */
Eric Dumazetb69aee02005-09-06 14:42:45 -07001004 sock = file->private_data;
Eliezer Tamir2d48d672013-06-24 10:28:03 +03001005
Eliezer Tamircbf55002013-07-08 16:20:34 +03001006 if (sk_can_busy_loop(sock->sk)) {
Eliezer Tamir2d48d672013-06-24 10:28:03 +03001007 /* this socket can poll_ll so tell the system call */
Eliezer Tamircbf55002013-07-08 16:20:34 +03001008 busy_flag = POLL_BUSY_LOOP;
Eliezer Tamir2d48d672013-06-24 10:28:03 +03001009
1010 /* once, only if requested by syscall */
Eliezer Tamircbf55002013-07-08 16:20:34 +03001011 if (wait && (wait->_key & POLL_BUSY_LOOP))
1012 sk_busy_loop(sock->sk, 1);
Eliezer Tamir2d48d672013-06-24 10:28:03 +03001013 }
1014
Eliezer Tamircbf55002013-07-08 16:20:34 +03001015 return busy_flag | sock->ops->poll(file, sock, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016}
1017
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001018static int sock_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019{
Eric Dumazetb69aee02005-09-06 14:42:45 -07001020 struct socket *sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
1022 return sock->ops->mmap(file, sock, vma);
1023}
1024
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001025static int sock_close(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 sock_release(SOCKET_I(inode));
1028 return 0;
1029}
1030
1031/*
1032 * Update the socket async list
1033 *
1034 * Fasync_list locking strategy.
1035 *
1036 * 1. fasync_list is modified only under process context socket lock
1037 * i.e. under semaphore.
1038 * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
Eric Dumazet989a2972010-04-14 09:55:35 +00001039 * or under socket lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 */
1041
1042static int sock_fasync(int fd, struct file *filp, int on)
1043{
Eric Dumazet989a2972010-04-14 09:55:35 +00001044 struct socket *sock = filp->private_data;
1045 struct sock *sk = sock->sk;
Eric Dumazeteaefd112011-02-18 03:26:36 +00001046 struct socket_wq *wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Eric Dumazet989a2972010-04-14 09:55:35 +00001048 if (sk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
1051 lock_sock(sk);
Eric Dumazeteaefd112011-02-18 03:26:36 +00001052 wq = rcu_dereference_protected(sock->wq, sock_owned_by_user(sk));
1053 fasync_helper(fd, filp, on, &wq->fasync_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Eric Dumazeteaefd112011-02-18 03:26:36 +00001055 if (!wq->fasync_list)
Eric Dumazet989a2972010-04-14 09:55:35 +00001056 sock_reset_flag(sk, SOCK_FASYNC);
Jonathan Corbet76398422009-02-01 14:26:59 -07001057 else
Eric Dumazetbcdce712009-10-06 17:28:29 -07001058 sock_set_flag(sk, SOCK_FASYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Eric Dumazet989a2972010-04-14 09:55:35 +00001060 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 return 0;
1062}
1063
Eric Dumazet43815482010-04-29 11:01:49 +00001064/* This function may be called only under socket lock or callback_lock or rcu_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
1066int sock_wake_async(struct socket *sock, int how, int band)
1067{
Eric Dumazet43815482010-04-29 11:01:49 +00001068 struct socket_wq *wq;
1069
1070 if (!sock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 return -1;
Eric Dumazet43815482010-04-29 11:01:49 +00001072 rcu_read_lock();
1073 wq = rcu_dereference(sock->wq);
1074 if (!wq || !wq->fasync_list) {
1075 rcu_read_unlock();
1076 return -1;
1077 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001078 switch (how) {
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001079 case SOCK_WAKE_WAITD:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 if (test_bit(SOCK_ASYNC_WAITDATA, &sock->flags))
1081 break;
1082 goto call_kill;
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001083 case SOCK_WAKE_SPACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags))
1085 break;
1086 /* fall through */
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001087 case SOCK_WAKE_IO:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001088call_kill:
Eric Dumazet43815482010-04-29 11:01:49 +00001089 kill_fasync(&wq->fasync_list, SIGIO, band);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 break;
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001091 case SOCK_WAKE_URG:
Eric Dumazet43815482010-04-29 11:01:49 +00001092 kill_fasync(&wq->fasync_list, SIGURG, band);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 }
Eric Dumazet43815482010-04-29 11:01:49 +00001094 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 return 0;
1096}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001097EXPORT_SYMBOL(sock_wake_async);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Pavel Emelyanov721db932010-09-29 16:06:32 +04001099int __sock_create(struct net *net, int family, int type, int protocol,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001100 struct socket **res, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101{
1102 int err;
1103 struct socket *sock;
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001104 const struct net_proto_family *pf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
1106 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001107 * Check protocol is in range
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 */
1109 if (family < 0 || family >= NPROTO)
1110 return -EAFNOSUPPORT;
1111 if (type < 0 || type >= SOCK_MAX)
1112 return -EINVAL;
1113
1114 /* Compatibility.
1115
1116 This uglymoron is moved from INET layer to here to avoid
1117 deadlock in module load.
1118 */
1119 if (family == PF_INET && type == SOCK_PACKET) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001120 static int warned;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 if (!warned) {
1122 warned = 1;
Yang Yingliang3410f222014-02-12 17:09:55 +08001123 pr_info("%s uses obsolete (PF_INET,SOCK_PACKET)\n",
1124 current->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 }
1126 family = PF_PACKET;
1127 }
1128
1129 err = security_socket_create(family, type, protocol, kern);
1130 if (err)
1131 return err;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001132
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001133 /*
1134 * Allocate the socket and allow the family to set things up. if
1135 * the protocol is 0, the family is instructed to select an appropriate
1136 * default.
1137 */
1138 sock = sock_alloc();
1139 if (!sock) {
Joe Perchese87cc472012-05-13 21:56:26 +00001140 net_warn_ratelimited("socket: no more sockets\n");
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001141 return -ENFILE; /* Not exactly a match, but its the
1142 closest posix thing */
1143 }
1144
1145 sock->type = type;
1146
Johannes Berg95a5afc2008-10-16 15:24:51 -07001147#ifdef CONFIG_MODULES
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001148 /* Attempt to load a protocol module if the find failed.
1149 *
1150 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 * requested real, full-featured networking support upon configuration.
1152 * Otherwise module support will break!
1153 */
Eric Dumazet190683a2010-11-10 10:50:44 +00001154 if (rcu_access_pointer(net_families[family]) == NULL)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001155 request_module("net-pf-%d", family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156#endif
1157
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001158 rcu_read_lock();
1159 pf = rcu_dereference(net_families[family]);
1160 err = -EAFNOSUPPORT;
1161 if (!pf)
1162 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
1164 /*
1165 * We will call the ->create function, that possibly is in a loadable
1166 * module, so we have to bump that loadable module refcnt first.
1167 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001168 if (!try_module_get(pf->owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 goto out_release;
1170
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001171 /* Now protected by module ref count */
1172 rcu_read_unlock();
1173
Eric Paris3f378b62009-11-05 22:18:14 -08001174 err = pf->create(net, sock, protocol, kern);
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001175 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 goto out_module_put;
Frank Filza79af592005-09-27 15:23:38 -07001177
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 /*
1179 * Now to bump the refcnt of the [loadable] module that owns this
1180 * socket at sock_release time we decrement its refcnt.
1181 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001182 if (!try_module_get(sock->ops->owner))
1183 goto out_module_busy;
1184
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 /*
1186 * Now that we're done with the ->create function, the [loadable]
1187 * module can have its refcnt decremented
1188 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001189 module_put(pf->owner);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001190 err = security_socket_post_create(sock, family, type, protocol, kern);
1191 if (err)
Herbert Xu3b185522007-08-15 14:46:02 -07001192 goto out_sock_release;
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001193 *res = sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001195 return 0;
1196
1197out_module_busy:
1198 err = -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199out_module_put:
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001200 sock->ops = NULL;
1201 module_put(pf->owner);
1202out_sock_release:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 sock_release(sock);
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001204 return err;
1205
1206out_release:
1207 rcu_read_unlock();
1208 goto out_sock_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209}
Pavel Emelyanov721db932010-09-29 16:06:32 +04001210EXPORT_SYMBOL(__sock_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
1212int sock_create(int family, int type, int protocol, struct socket **res)
1213{
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001214 return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001216EXPORT_SYMBOL(sock_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
1218int sock_create_kern(int family, int type, int protocol, struct socket **res)
1219{
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001220 return __sock_create(&init_net, family, type, protocol, res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001222EXPORT_SYMBOL(sock_create_kern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001224SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225{
1226 int retval;
1227 struct socket *sock;
Ulrich Dreppera677a032008-07-23 21:29:17 -07001228 int flags;
1229
Ulrich Dreppere38b36f2008-07-23 21:29:42 -07001230 /* Check the SOCK_* constants for consistency. */
1231 BUILD_BUG_ON(SOCK_CLOEXEC != O_CLOEXEC);
1232 BUILD_BUG_ON((SOCK_MAX | SOCK_TYPE_MASK) != SOCK_TYPE_MASK);
1233 BUILD_BUG_ON(SOCK_CLOEXEC & SOCK_TYPE_MASK);
1234 BUILD_BUG_ON(SOCK_NONBLOCK & SOCK_TYPE_MASK);
1235
Ulrich Dreppera677a032008-07-23 21:29:17 -07001236 flags = type & ~SOCK_TYPE_MASK;
Ulrich Drepper77d27202008-07-23 21:29:35 -07001237 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Dreppera677a032008-07-23 21:29:17 -07001238 return -EINVAL;
1239 type &= SOCK_TYPE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001241 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1242 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1243
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 retval = sock_create(family, type, protocol, &sock);
1245 if (retval < 0)
1246 goto out;
1247
Ulrich Drepper77d27202008-07-23 21:29:35 -07001248 retval = sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 if (retval < 0)
1250 goto out_release;
1251
1252out:
1253 /* It may be already another descriptor 8) Not kernel problem. */
1254 return retval;
1255
1256out_release:
1257 sock_release(sock);
1258 return retval;
1259}
1260
1261/*
1262 * Create a pair of connected sockets.
1263 */
1264
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001265SYSCALL_DEFINE4(socketpair, int, family, int, type, int, protocol,
1266 int __user *, usockvec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267{
1268 struct socket *sock1, *sock2;
1269 int fd1, fd2, err;
Al Virodb349502007-02-07 01:48:00 -05001270 struct file *newfile1, *newfile2;
Ulrich Dreppera677a032008-07-23 21:29:17 -07001271 int flags;
1272
1273 flags = type & ~SOCK_TYPE_MASK;
Ulrich Drepper77d27202008-07-23 21:29:35 -07001274 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Dreppera677a032008-07-23 21:29:17 -07001275 return -EINVAL;
1276 type &= SOCK_TYPE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001278 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1279 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1280
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 /*
1282 * Obtain the first socket and check if the underlying protocol
1283 * supports the socketpair call.
1284 */
1285
1286 err = sock_create(family, type, protocol, &sock1);
1287 if (err < 0)
1288 goto out;
1289
1290 err = sock_create(family, type, protocol, &sock2);
1291 if (err < 0)
1292 goto out_release_1;
1293
1294 err = sock1->ops->socketpair(sock1, sock2);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001295 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 goto out_release_both;
1297
Al Viro28407632012-08-17 23:54:15 -04001298 fd1 = get_unused_fd_flags(flags);
David S. Millerbf3c23d2007-10-29 21:54:02 -07001299 if (unlikely(fd1 < 0)) {
1300 err = fd1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 goto out_release_both;
David S. Millerbf3c23d2007-10-29 21:54:02 -07001302 }
Yann Droneaudd73aa282013-12-09 22:42:20 +01001303
Al Viro28407632012-08-17 23:54:15 -04001304 fd2 = get_unused_fd_flags(flags);
Al Viro198de4d2009-08-05 19:29:23 +04001305 if (unlikely(fd2 < 0)) {
1306 err = fd2;
Yann Droneaudd73aa282013-12-09 22:42:20 +01001307 goto out_put_unused_1;
Al Viro28407632012-08-17 23:54:15 -04001308 }
1309
Linus Torvaldsaab174f2012-10-02 20:25:04 -07001310 newfile1 = sock_alloc_file(sock1, flags, NULL);
Al Viro28407632012-08-17 23:54:15 -04001311 if (unlikely(IS_ERR(newfile1))) {
1312 err = PTR_ERR(newfile1);
Yann Droneaudd73aa282013-12-09 22:42:20 +01001313 goto out_put_unused_both;
Al Viro28407632012-08-17 23:54:15 -04001314 }
1315
Linus Torvaldsaab174f2012-10-02 20:25:04 -07001316 newfile2 = sock_alloc_file(sock2, flags, NULL);
Al Viro28407632012-08-17 23:54:15 -04001317 if (IS_ERR(newfile2)) {
1318 err = PTR_ERR(newfile2);
Yann Droneaudd73aa282013-12-09 22:42:20 +01001319 goto out_fput_1;
Al Virodb349502007-02-07 01:48:00 -05001320 }
1321
Yann Droneaudd73aa282013-12-09 22:42:20 +01001322 err = put_user(fd1, &usockvec[0]);
1323 if (err)
1324 goto out_fput_both;
1325
1326 err = put_user(fd2, &usockvec[1]);
1327 if (err)
1328 goto out_fput_both;
1329
Al Viro157cf642008-12-14 04:57:47 -05001330 audit_fd_pair(fd1, fd2);
Yann Droneaudd73aa282013-12-09 22:42:20 +01001331
Al Virodb349502007-02-07 01:48:00 -05001332 fd_install(fd1, newfile1);
1333 fd_install(fd2, newfile2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 /* fd1 and fd2 may be already another descriptors.
1335 * Not kernel problem.
1336 */
1337
Yann Droneaudd73aa282013-12-09 22:42:20 +01001338 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Yann Droneaudd73aa282013-12-09 22:42:20 +01001340out_fput_both:
1341 fput(newfile2);
1342 fput(newfile1);
1343 put_unused_fd(fd2);
1344 put_unused_fd(fd1);
1345 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
Yann Droneaudd73aa282013-12-09 22:42:20 +01001347out_fput_1:
1348 fput(newfile1);
1349 put_unused_fd(fd2);
1350 put_unused_fd(fd1);
1351 sock_release(sock2);
1352 goto out;
1353
1354out_put_unused_both:
1355 put_unused_fd(fd2);
1356out_put_unused_1:
1357 put_unused_fd(fd1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358out_release_both:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001359 sock_release(sock2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360out_release_1:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001361 sock_release(sock1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362out:
1363 return err;
1364}
1365
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366/*
1367 * Bind a name to a socket. Nothing much to do here since it's
1368 * the protocol's responsibility to handle the local address.
1369 *
1370 * We move the socket address to kernel space before we call
1371 * the protocol layer (having also checked the address is ok).
1372 */
1373
Heiko Carstens20f37032009-01-14 14:14:23 +01001374SYSCALL_DEFINE3(bind, int, fd, struct sockaddr __user *, umyaddr, int, addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375{
1376 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001377 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001378 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001380 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001381 if (sock) {
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001382 err = move_addr_to_kernel(umyaddr, addrlen, &address);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001383 if (err >= 0) {
1384 err = security_socket_bind(sock,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001385 (struct sockaddr *)&address,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001386 addrlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001387 if (!err)
1388 err = sock->ops->bind(sock,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001389 (struct sockaddr *)
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001390 &address, addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 }
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001392 fput_light(sock->file, fput_needed);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 return err;
1395}
1396
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397/*
1398 * Perform a listen. Basically, we allow the protocol to do anything
1399 * necessary for a listen, and if that works, we mark the socket as
1400 * ready for listening.
1401 */
1402
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001403SYSCALL_DEFINE2(listen, int, fd, int, backlog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
1405 struct socket *sock;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001406 int err, fput_needed;
Pavel Emelyanovb8e1f9b2007-12-08 00:12:33 -08001407 int somaxconn;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001408
1409 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1410 if (sock) {
Pavel Emelyanov8efa6e92008-03-31 19:41:14 -07001411 somaxconn = sock_net(sock->sk)->core.sysctl_somaxconn;
Eric Dumazet95c96172012-04-15 05:58:06 +00001412 if ((unsigned int)backlog > somaxconn)
Pavel Emelyanovb8e1f9b2007-12-08 00:12:33 -08001413 backlog = somaxconn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
1415 err = security_socket_listen(sock, backlog);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001416 if (!err)
1417 err = sock->ops->listen(sock, backlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001419 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 }
1421 return err;
1422}
1423
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424/*
1425 * For accept, we attempt to create a new socket, set up the link
1426 * with the client, wake up the client, then return the new
1427 * connected fd. We collect the address of the connector in kernel
1428 * space and move it to user at the very end. This is unclean because
1429 * we open the socket then return an error.
1430 *
1431 * 1003.1g adds the ability to recvmsg() to query connection pending
1432 * status to recvmsg. We need to add that support in a way thats
1433 * clean when we restucture accept also.
1434 */
1435
Heiko Carstens20f37032009-01-14 14:14:23 +01001436SYSCALL_DEFINE4(accept4, int, fd, struct sockaddr __user *, upeer_sockaddr,
1437 int __user *, upeer_addrlen, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438{
1439 struct socket *sock, *newsock;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001440 struct file *newfile;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001441 int err, len, newfd, fput_needed;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001442 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Ulrich Drepper77d27202008-07-23 21:29:35 -07001444 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001445 return -EINVAL;
1446
1447 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1448 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1449
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001450 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 if (!sock)
1452 goto out;
1453
1454 err = -ENFILE;
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001455 newsock = sock_alloc();
1456 if (!newsock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 goto out_put;
1458
1459 newsock->type = sock->type;
1460 newsock->ops = sock->ops;
1461
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 /*
1463 * We don't need try_module_get here, as the listening socket (sock)
1464 * has the protocol module (sock->ops->owner) held.
1465 */
1466 __module_get(newsock->ops->owner);
1467
Al Viro28407632012-08-17 23:54:15 -04001468 newfd = get_unused_fd_flags(flags);
David S. Miller39d8c1b2006-03-20 17:13:49 -08001469 if (unlikely(newfd < 0)) {
1470 err = newfd;
David S. Miller9a1875e2006-04-01 12:48:36 -08001471 sock_release(newsock);
1472 goto out_put;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001473 }
Linus Torvaldsaab174f2012-10-02 20:25:04 -07001474 newfile = sock_alloc_file(newsock, flags, sock->sk->sk_prot_creator->name);
Al Viro28407632012-08-17 23:54:15 -04001475 if (unlikely(IS_ERR(newfile))) {
1476 err = PTR_ERR(newfile);
1477 put_unused_fd(newfd);
1478 sock_release(newsock);
1479 goto out_put;
1480 }
David S. Miller39d8c1b2006-03-20 17:13:49 -08001481
Frank Filza79af592005-09-27 15:23:38 -07001482 err = security_socket_accept(sock, newsock);
1483 if (err)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001484 goto out_fd;
Frank Filza79af592005-09-27 15:23:38 -07001485
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 err = sock->ops->accept(sock, newsock, sock->file->f_flags);
1487 if (err < 0)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001488 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
1490 if (upeer_sockaddr) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001491 if (newsock->ops->getname(newsock, (struct sockaddr *)&address,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001492 &len, 2) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 err = -ECONNABORTED;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001494 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 }
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001496 err = move_addr_to_user(&address,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001497 len, upeer_sockaddr, upeer_addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 if (err < 0)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001499 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 }
1501
1502 /* File flags are not inherited via accept() unlike another OSes. */
1503
David S. Miller39d8c1b2006-03-20 17:13:49 -08001504 fd_install(newfd, newfile);
1505 err = newfd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001508 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509out:
1510 return err;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001511out_fd:
David S. Miller9606a212006-04-01 01:00:14 -08001512 fput(newfile);
David S. Miller39d8c1b2006-03-20 17:13:49 -08001513 put_unused_fd(newfd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 goto out_put;
1515}
1516
Heiko Carstens20f37032009-01-14 14:14:23 +01001517SYSCALL_DEFINE3(accept, int, fd, struct sockaddr __user *, upeer_sockaddr,
1518 int __user *, upeer_addrlen)
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001519{
Ulrich Drepperde11def2008-11-19 15:36:14 -08001520 return sys_accept4(fd, upeer_sockaddr, upeer_addrlen, 0);
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001521}
1522
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523/*
1524 * Attempt to connect to a socket with the server address. The address
1525 * is in user space so we verify it is OK and move it to kernel space.
1526 *
1527 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
1528 * break bindings
1529 *
1530 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
1531 * other SEQPACKET protocols that take time to connect() as it doesn't
1532 * include the -EINPROGRESS status for such sockets.
1533 */
1534
Heiko Carstens20f37032009-01-14 14:14:23 +01001535SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
1536 int, addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537{
1538 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001539 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001540 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001542 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 if (!sock)
1544 goto out;
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001545 err = move_addr_to_kernel(uservaddr, addrlen, &address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 if (err < 0)
1547 goto out_put;
1548
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001549 err =
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001550 security_socket_connect(sock, (struct sockaddr *)&address, addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 if (err)
1552 goto out_put;
1553
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001554 err = sock->ops->connect(sock, (struct sockaddr *)&address, addrlen,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 sock->file->f_flags);
1556out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001557 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558out:
1559 return err;
1560}
1561
1562/*
1563 * Get the local address ('name') of a socket object. Move the obtained
1564 * name to user space.
1565 */
1566
Heiko Carstens20f37032009-01-14 14:14:23 +01001567SYSCALL_DEFINE3(getsockname, int, fd, struct sockaddr __user *, usockaddr,
1568 int __user *, usockaddr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569{
1570 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001571 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001572 int len, err, fput_needed;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001573
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001574 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 if (!sock)
1576 goto out;
1577
1578 err = security_socket_getsockname(sock);
1579 if (err)
1580 goto out_put;
1581
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001582 err = sock->ops->getname(sock, (struct sockaddr *)&address, &len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 if (err)
1584 goto out_put;
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001585 err = move_addr_to_user(&address, len, usockaddr, usockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
1587out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001588 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589out:
1590 return err;
1591}
1592
1593/*
1594 * Get the remote address ('name') of a socket object. Move the obtained
1595 * name to user space.
1596 */
1597
Heiko Carstens20f37032009-01-14 14:14:23 +01001598SYSCALL_DEFINE3(getpeername, int, fd, struct sockaddr __user *, usockaddr,
1599 int __user *, usockaddr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600{
1601 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001602 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001603 int len, err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001605 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1606 if (sock != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 err = security_socket_getpeername(sock);
1608 if (err) {
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001609 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 return err;
1611 }
1612
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001613 err =
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001614 sock->ops->getname(sock, (struct sockaddr *)&address, &len,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001615 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 if (!err)
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001617 err = move_addr_to_user(&address, len, usockaddr,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001618 usockaddr_len);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001619 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 }
1621 return err;
1622}
1623
1624/*
1625 * Send a datagram to a given address. We move the address into kernel
1626 * space and check the user space data area is readable before invoking
1627 * the protocol.
1628 */
1629
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001630SYSCALL_DEFINE6(sendto, int, fd, void __user *, buff, size_t, len,
Eric Dumazet95c96172012-04-15 05:58:06 +00001631 unsigned int, flags, struct sockaddr __user *, addr,
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001632 int, addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633{
1634 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001635 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 int err;
1637 struct msghdr msg;
1638 struct iovec iov;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001639 int fput_needed;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001640
Al Viro602bd0e2015-03-21 19:12:32 -04001641 err = import_single_range(WRITE, buff, len, &iov, &msg.msg_iter);
1642 if (unlikely(err))
1643 return err;
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001644 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1645 if (!sock)
David S. Miller4387ff72007-02-08 15:06:08 -08001646 goto out;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001647
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001648 msg.msg_name = NULL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001649 msg.msg_control = NULL;
1650 msg.msg_controllen = 0;
1651 msg.msg_namelen = 0;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001652 if (addr) {
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001653 err = move_addr_to_kernel(addr, addr_len, &address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 if (err < 0)
1655 goto out_put;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001656 msg.msg_name = (struct sockaddr *)&address;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001657 msg.msg_namelen = addr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 }
1659 if (sock->file->f_flags & O_NONBLOCK)
1660 flags |= MSG_DONTWAIT;
1661 msg.msg_flags = flags;
Al Virod8725c82014-12-11 00:02:50 -05001662 err = sock_sendmsg(sock, &msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001664out_put:
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001665 fput_light(sock->file, fput_needed);
David S. Miller4387ff72007-02-08 15:06:08 -08001666out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 return err;
1668}
1669
1670/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001671 * Send a datagram down a socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 */
1673
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001674SYSCALL_DEFINE4(send, int, fd, void __user *, buff, size_t, len,
Eric Dumazet95c96172012-04-15 05:58:06 +00001675 unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676{
1677 return sys_sendto(fd, buff, len, flags, NULL, 0);
1678}
1679
1680/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001681 * Receive a frame from the socket and optionally record the address of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 * sender. We verify the buffers are writable and if needed move the
1683 * sender address from kernel to user space.
1684 */
1685
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001686SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, ubuf, size_t, size,
Eric Dumazet95c96172012-04-15 05:58:06 +00001687 unsigned int, flags, struct sockaddr __user *, addr,
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001688 int __user *, addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689{
1690 struct socket *sock;
1691 struct iovec iov;
1692 struct msghdr msg;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001693 struct sockaddr_storage address;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001694 int err, err2;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001695 int fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
Al Viro602bd0e2015-03-21 19:12:32 -04001697 err = import_single_range(READ, ubuf, size, &iov, &msg.msg_iter);
1698 if (unlikely(err))
1699 return err;
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001700 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 if (!sock)
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001702 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001704 msg.msg_control = NULL;
1705 msg.msg_controllen = 0;
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01001706 /* Save some cycles and don't copy the address if not needed */
1707 msg.msg_name = addr ? (struct sockaddr *)&address : NULL;
1708 /* We assume all kernel code knows the size of sockaddr_storage */
1709 msg.msg_namelen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 if (sock->file->f_flags & O_NONBLOCK)
1711 flags |= MSG_DONTWAIT;
Al Viro602bd0e2015-03-21 19:12:32 -04001712 err = sock_recvmsg(sock, &msg, iov_iter_count(&msg.msg_iter), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001714 if (err >= 0 && addr != NULL) {
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001715 err2 = move_addr_to_user(&address,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001716 msg.msg_namelen, addr, addr_len);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001717 if (err2 < 0)
1718 err = err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 }
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001720
1721 fput_light(sock->file, fput_needed);
David S. Miller4387ff72007-02-08 15:06:08 -08001722out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 return err;
1724}
1725
1726/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001727 * Receive a datagram from a socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 */
1729
Jan Glauberb7c0ddf2014-04-16 09:32:48 +02001730SYSCALL_DEFINE4(recv, int, fd, void __user *, ubuf, size_t, size,
1731 unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732{
1733 return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
1734}
1735
1736/*
1737 * Set a socket option. Because we don't know the option lengths we have
1738 * to pass the user mode parameter for the protocols to sort out.
1739 */
1740
Heiko Carstens20f37032009-01-14 14:14:23 +01001741SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname,
1742 char __user *, optval, int, optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001744 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 struct socket *sock;
1746
1747 if (optlen < 0)
1748 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001749
1750 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1751 if (sock != NULL) {
1752 err = security_socket_setsockopt(sock, level, optname);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001753 if (err)
1754 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
1756 if (level == SOL_SOCKET)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001757 err =
1758 sock_setsockopt(sock, level, optname, optval,
1759 optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 else
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001761 err =
1762 sock->ops->setsockopt(sock, level, optname, optval,
1763 optlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001764out_put:
1765 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 }
1767 return err;
1768}
1769
1770/*
1771 * Get a socket option. Because we don't know the option lengths we have
1772 * to pass a user mode parameter for the protocols to sort out.
1773 */
1774
Heiko Carstens20f37032009-01-14 14:14:23 +01001775SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname,
1776 char __user *, optval, int __user *, optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001778 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 struct socket *sock;
1780
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001781 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1782 if (sock != NULL) {
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001783 err = security_socket_getsockopt(sock, level, optname);
1784 if (err)
1785 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
1787 if (level == SOL_SOCKET)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001788 err =
1789 sock_getsockopt(sock, level, optname, optval,
1790 optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 else
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001792 err =
1793 sock->ops->getsockopt(sock, level, optname, optval,
1794 optlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001795out_put:
1796 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 }
1798 return err;
1799}
1800
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801/*
1802 * Shutdown a socket.
1803 */
1804
Heiko Carstens754fe8d2009-01-14 14:14:09 +01001805SYSCALL_DEFINE2(shutdown, int, fd, int, how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001807 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 struct socket *sock;
1809
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001810 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1811 if (sock != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 err = security_socket_shutdown(sock, how);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001813 if (!err)
1814 err = sock->ops->shutdown(sock, how);
1815 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 }
1817 return err;
1818}
1819
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001820/* A couple of helpful macros for getting the address of the 32/64 bit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 * fields which are the same type (int / unsigned) on our platforms.
1822 */
1823#define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
1824#define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
1825#define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
1826
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00001827struct used_address {
1828 struct sockaddr_storage name;
1829 unsigned int name_len;
1830};
1831
Al Viroda184282015-03-21 19:29:06 -04001832static int copy_msghdr_from_user(struct msghdr *kmsg,
1833 struct user_msghdr __user *umsg,
1834 struct sockaddr __user **save_addr,
1835 struct iovec **iov)
Dan Carpenter1661bf32013-10-03 00:27:20 +03001836{
Al Viro08adb7d2014-11-10 20:23:13 -05001837 struct sockaddr __user *uaddr;
1838 struct iovec __user *uiov;
Al Viroc0371da2014-11-24 10:42:55 -05001839 size_t nr_segs;
Al Viro08adb7d2014-11-10 20:23:13 -05001840 ssize_t err;
1841
1842 if (!access_ok(VERIFY_READ, umsg, sizeof(*umsg)) ||
1843 __get_user(uaddr, &umsg->msg_name) ||
1844 __get_user(kmsg->msg_namelen, &umsg->msg_namelen) ||
1845 __get_user(uiov, &umsg->msg_iov) ||
Al Viroc0371da2014-11-24 10:42:55 -05001846 __get_user(nr_segs, &umsg->msg_iovlen) ||
Al Viro08adb7d2014-11-10 20:23:13 -05001847 __get_user(kmsg->msg_control, &umsg->msg_control) ||
1848 __get_user(kmsg->msg_controllen, &umsg->msg_controllen) ||
1849 __get_user(kmsg->msg_flags, &umsg->msg_flags))
Dan Carpenter1661bf32013-10-03 00:27:20 +03001850 return -EFAULT;
Matthew Leachdbb490b2014-03-11 11:58:27 +00001851
Al Viro08adb7d2014-11-10 20:23:13 -05001852 if (!uaddr)
Ani Sinha6a2a2b32014-09-08 14:49:59 -07001853 kmsg->msg_namelen = 0;
1854
Matthew Leachdbb490b2014-03-11 11:58:27 +00001855 if (kmsg->msg_namelen < 0)
1856 return -EINVAL;
1857
Dan Carpenter1661bf32013-10-03 00:27:20 +03001858 if (kmsg->msg_namelen > sizeof(struct sockaddr_storage))
Dan Carpenterdb31c552013-11-27 15:40:21 +03001859 kmsg->msg_namelen = sizeof(struct sockaddr_storage);
Al Viro08adb7d2014-11-10 20:23:13 -05001860
1861 if (save_addr)
1862 *save_addr = uaddr;
1863
1864 if (uaddr && kmsg->msg_namelen) {
1865 if (!save_addr) {
1866 err = move_addr_to_kernel(uaddr, kmsg->msg_namelen,
1867 kmsg->msg_name);
1868 if (err < 0)
1869 return err;
1870 }
1871 } else {
1872 kmsg->msg_name = NULL;
1873 kmsg->msg_namelen = 0;
1874 }
1875
Al Viroc0371da2014-11-24 10:42:55 -05001876 if (nr_segs > UIO_MAXIOV)
Al Viro08adb7d2014-11-10 20:23:13 -05001877 return -EMSGSIZE;
1878
tadeusz.struk@intel.com0345f932015-03-19 12:31:25 -07001879 kmsg->msg_iocb = NULL;
1880
Al Viroda184282015-03-21 19:29:06 -04001881 return import_iovec(save_addr ? READ : WRITE, uiov, nr_segs,
1882 UIO_FASTIOV, iov, &kmsg->msg_iter);
Dan Carpenter1661bf32013-10-03 00:27:20 +03001883}
1884
Al Viro666547f2014-04-06 14:03:05 -04001885static int ___sys_sendmsg(struct socket *sock, struct user_msghdr __user *msg,
Eric Dumazet95c96172012-04-15 05:58:06 +00001886 struct msghdr *msg_sys, unsigned int flags,
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00001887 struct used_address *used_address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888{
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001889 struct compat_msghdr __user *msg_compat =
1890 (struct compat_msghdr __user *)msg;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001891 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
Alex Williamsonb9d717a2005-09-26 14:28:02 -07001893 unsigned char ctl[sizeof(struct cmsghdr) + 20]
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001894 __attribute__ ((aligned(sizeof(__kernel_size_t))));
1895 /* 20 is size of ipv6_pktinfo */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 unsigned char *ctl_buf = ctl;
Al Virod8725c82014-12-11 00:02:50 -05001897 int ctl_len;
Al Viro08adb7d2014-11-10 20:23:13 -05001898 ssize_t err;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001899
Al Viro08adb7d2014-11-10 20:23:13 -05001900 msg_sys->msg_name = &address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
Al Viro08449322014-11-09 22:33:45 -05001902 if (MSG_CMSG_COMPAT & flags)
Al Viro08adb7d2014-11-10 20:23:13 -05001903 err = get_compat_msghdr(msg_sys, msg_compat, NULL, &iov);
Al Viro08449322014-11-09 22:33:45 -05001904 else
Al Viro08adb7d2014-11-10 20:23:13 -05001905 err = copy_msghdr_from_user(msg_sys, msg, NULL, &iov);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001906 if (err < 0)
Al Viroda184282015-03-21 19:29:06 -04001907 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
1909 err = -ENOBUFS;
1910
Anton Blanchard228e5482011-05-02 20:21:35 +00001911 if (msg_sys->msg_controllen > INT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 goto out_freeiov;
Anton Blanchard228e5482011-05-02 20:21:35 +00001913 ctl_len = msg_sys->msg_controllen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001915 err =
Anton Blanchard228e5482011-05-02 20:21:35 +00001916 cmsghdr_from_user_compat_to_kern(msg_sys, sock->sk, ctl,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001917 sizeof(ctl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 if (err)
1919 goto out_freeiov;
Anton Blanchard228e5482011-05-02 20:21:35 +00001920 ctl_buf = msg_sys->msg_control;
1921 ctl_len = msg_sys->msg_controllen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 } else if (ctl_len) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001923 if (ctl_len > sizeof(ctl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001925 if (ctl_buf == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 goto out_freeiov;
1927 }
1928 err = -EFAULT;
1929 /*
Anton Blanchard228e5482011-05-02 20:21:35 +00001930 * Careful! Before this, msg_sys->msg_control contains a user pointer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
1932 * checking falls down on this.
1933 */
Namhyung Kimfb8621b2010-09-07 03:55:00 +00001934 if (copy_from_user(ctl_buf,
Anton Blanchard228e5482011-05-02 20:21:35 +00001935 (void __user __force *)msg_sys->msg_control,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001936 ctl_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 goto out_freectl;
Anton Blanchard228e5482011-05-02 20:21:35 +00001938 msg_sys->msg_control = ctl_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 }
Anton Blanchard228e5482011-05-02 20:21:35 +00001940 msg_sys->msg_flags = flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
1942 if (sock->file->f_flags & O_NONBLOCK)
Anton Blanchard228e5482011-05-02 20:21:35 +00001943 msg_sys->msg_flags |= MSG_DONTWAIT;
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00001944 /*
1945 * If this is sendmmsg() and current destination address is same as
1946 * previously succeeded address, omit asking LSM's decision.
1947 * used_address->name_len is initialized to UINT_MAX so that the first
1948 * destination address never matches.
1949 */
Mathieu Desnoyersbc909d92011-08-24 19:45:03 -07001950 if (used_address && msg_sys->msg_name &&
1951 used_address->name_len == msg_sys->msg_namelen &&
1952 !memcmp(&used_address->name, msg_sys->msg_name,
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00001953 used_address->name_len)) {
Al Virod8725c82014-12-11 00:02:50 -05001954 err = sock_sendmsg_nosec(sock, msg_sys);
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00001955 goto out_freectl;
1956 }
Al Virod8725c82014-12-11 00:02:50 -05001957 err = sock_sendmsg(sock, msg_sys);
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00001958 /*
1959 * If this is sendmmsg() and sending to current destination address was
1960 * successful, remember it.
1961 */
1962 if (used_address && err >= 0) {
1963 used_address->name_len = msg_sys->msg_namelen;
Mathieu Desnoyersbc909d92011-08-24 19:45:03 -07001964 if (msg_sys->msg_name)
1965 memcpy(&used_address->name, msg_sys->msg_name,
1966 used_address->name_len);
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00001967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
1969out_freectl:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001970 if (ctl_buf != ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 sock_kfree_s(sock->sk, ctl_buf, ctl_len);
1972out_freeiov:
Al Viroda184282015-03-21 19:29:06 -04001973 kfree(iov);
Anton Blanchard228e5482011-05-02 20:21:35 +00001974 return err;
1975}
1976
1977/*
1978 * BSD sendmsg interface
1979 */
1980
Al Viro666547f2014-04-06 14:03:05 -04001981long __sys_sendmsg(int fd, struct user_msghdr __user *msg, unsigned flags)
Anton Blanchard228e5482011-05-02 20:21:35 +00001982{
1983 int fput_needed, err;
1984 struct msghdr msg_sys;
Andy Lutomirski1be374a2013-05-22 14:07:44 -07001985 struct socket *sock;
Anton Blanchard228e5482011-05-02 20:21:35 +00001986
Andy Lutomirski1be374a2013-05-22 14:07:44 -07001987 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Anton Blanchard228e5482011-05-02 20:21:35 +00001988 if (!sock)
1989 goto out;
1990
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00001991 err = ___sys_sendmsg(sock, msg, &msg_sys, flags, NULL);
Anton Blanchard228e5482011-05-02 20:21:35 +00001992
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001993 fput_light(sock->file, fput_needed);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001994out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 return err;
1996}
1997
Al Viro666547f2014-04-06 14:03:05 -04001998SYSCALL_DEFINE3(sendmsg, int, fd, struct user_msghdr __user *, msg, unsigned int, flags)
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00001999{
2000 if (flags & MSG_CMSG_COMPAT)
2001 return -EINVAL;
2002 return __sys_sendmsg(fd, msg, flags);
2003}
2004
Anton Blanchard228e5482011-05-02 20:21:35 +00002005/*
2006 * Linux sendmmsg interface
2007 */
2008
2009int __sys_sendmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
2010 unsigned int flags)
2011{
2012 int fput_needed, err, datagrams;
2013 struct socket *sock;
2014 struct mmsghdr __user *entry;
2015 struct compat_mmsghdr __user *compat_entry;
2016 struct msghdr msg_sys;
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002017 struct used_address used_address;
Anton Blanchard228e5482011-05-02 20:21:35 +00002018
Anton Blanchard98382f42011-08-04 14:07:39 +00002019 if (vlen > UIO_MAXIOV)
2020 vlen = UIO_MAXIOV;
Anton Blanchard228e5482011-05-02 20:21:35 +00002021
2022 datagrams = 0;
2023
2024 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2025 if (!sock)
2026 return err;
2027
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002028 used_address.name_len = UINT_MAX;
Anton Blanchard228e5482011-05-02 20:21:35 +00002029 entry = mmsg;
2030 compat_entry = (struct compat_mmsghdr __user *)mmsg;
Anton Blanchard728ffb82011-08-04 14:07:38 +00002031 err = 0;
Anton Blanchard228e5482011-05-02 20:21:35 +00002032
2033 while (datagrams < vlen) {
Anton Blanchard228e5482011-05-02 20:21:35 +00002034 if (MSG_CMSG_COMPAT & flags) {
Al Viro666547f2014-04-06 14:03:05 -04002035 err = ___sys_sendmsg(sock, (struct user_msghdr __user *)compat_entry,
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002036 &msg_sys, flags, &used_address);
Anton Blanchard228e5482011-05-02 20:21:35 +00002037 if (err < 0)
2038 break;
2039 err = __put_user(err, &compat_entry->msg_len);
2040 ++compat_entry;
2041 } else {
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002042 err = ___sys_sendmsg(sock,
Al Viro666547f2014-04-06 14:03:05 -04002043 (struct user_msghdr __user *)entry,
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002044 &msg_sys, flags, &used_address);
Anton Blanchard228e5482011-05-02 20:21:35 +00002045 if (err < 0)
2046 break;
2047 err = put_user(err, &entry->msg_len);
2048 ++entry;
2049 }
2050
2051 if (err)
2052 break;
2053 ++datagrams;
2054 }
2055
Anton Blanchard228e5482011-05-02 20:21:35 +00002056 fput_light(sock->file, fput_needed);
2057
Anton Blanchard728ffb82011-08-04 14:07:38 +00002058 /* We only return an error if no datagrams were able to be sent */
2059 if (datagrams != 0)
Anton Blanchard228e5482011-05-02 20:21:35 +00002060 return datagrams;
2061
Anton Blanchard228e5482011-05-02 20:21:35 +00002062 return err;
2063}
2064
2065SYSCALL_DEFINE4(sendmmsg, int, fd, struct mmsghdr __user *, mmsg,
2066 unsigned int, vlen, unsigned int, flags)
2067{
Andy Lutomirski1be374a2013-05-22 14:07:44 -07002068 if (flags & MSG_CMSG_COMPAT)
2069 return -EINVAL;
Anton Blanchard228e5482011-05-02 20:21:35 +00002070 return __sys_sendmmsg(fd, mmsg, vlen, flags);
2071}
2072
Al Viro666547f2014-04-06 14:03:05 -04002073static int ___sys_recvmsg(struct socket *sock, struct user_msghdr __user *msg,
Eric Dumazet95c96172012-04-15 05:58:06 +00002074 struct msghdr *msg_sys, unsigned int flags, int nosec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075{
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002076 struct compat_msghdr __user *msg_compat =
2077 (struct compat_msghdr __user *)msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 struct iovec iovstack[UIO_FASTIOV];
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002079 struct iovec *iov = iovstack;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 unsigned long cmsg_ptr;
Al Viro08adb7d2014-11-10 20:23:13 -05002081 int total_len, len;
2082 ssize_t err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
2084 /* kernel mode address */
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07002085 struct sockaddr_storage addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
2087 /* user mode address pointers */
2088 struct sockaddr __user *uaddr;
Al Viro08adb7d2014-11-10 20:23:13 -05002089 int __user *uaddr_len = COMPAT_NAMELEN(msg);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002090
Al Viro08adb7d2014-11-10 20:23:13 -05002091 msg_sys->msg_name = &addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01002093 if (MSG_CMSG_COMPAT & flags)
Al Viro08adb7d2014-11-10 20:23:13 -05002094 err = get_compat_msghdr(msg_sys, msg_compat, &uaddr, &iov);
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01002095 else
Al Viro08adb7d2014-11-10 20:23:13 -05002096 err = copy_msghdr_from_user(msg_sys, msg, &uaddr, &iov);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 if (err < 0)
Al Viroda184282015-03-21 19:29:06 -04002098 return err;
2099 total_len = iov_iter_count(&msg_sys->msg_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002101 cmsg_ptr = (unsigned long)msg_sys->msg_control;
2102 msg_sys->msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002103
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01002104 /* We assume all kernel code knows the size of sockaddr_storage */
2105 msg_sys->msg_namelen = 0;
2106
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 if (sock->file->f_flags & O_NONBLOCK)
2108 flags |= MSG_DONTWAIT;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002109 err = (nosec ? sock_recvmsg_nosec : sock_recvmsg)(sock, msg_sys,
2110 total_len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 if (err < 0)
2112 goto out_freeiov;
2113 len = err;
2114
2115 if (uaddr != NULL) {
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00002116 err = move_addr_to_user(&addr,
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002117 msg_sys->msg_namelen, uaddr,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002118 uaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 if (err < 0)
2120 goto out_freeiov;
2121 }
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002122 err = __put_user((msg_sys->msg_flags & ~MSG_CMSG_COMPAT),
David S. Miller37f7f422005-09-16 16:51:01 -07002123 COMPAT_FLAGS(msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 if (err)
2125 goto out_freeiov;
2126 if (MSG_CMSG_COMPAT & flags)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002127 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 &msg_compat->msg_controllen);
2129 else
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002130 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 &msg->msg_controllen);
2132 if (err)
2133 goto out_freeiov;
2134 err = len;
2135
2136out_freeiov:
Al Viroda184282015-03-21 19:29:06 -04002137 kfree(iov);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002138 return err;
2139}
2140
2141/*
2142 * BSD recvmsg interface
2143 */
2144
Al Viro666547f2014-04-06 14:03:05 -04002145long __sys_recvmsg(int fd, struct user_msghdr __user *msg, unsigned flags)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002146{
2147 int fput_needed, err;
2148 struct msghdr msg_sys;
Andy Lutomirski1be374a2013-05-22 14:07:44 -07002149 struct socket *sock;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002150
Andy Lutomirski1be374a2013-05-22 14:07:44 -07002151 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002152 if (!sock)
2153 goto out;
2154
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002155 err = ___sys_recvmsg(sock, msg, &msg_sys, flags, 0);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002156
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002157 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158out:
2159 return err;
2160}
2161
Al Viro666547f2014-04-06 14:03:05 -04002162SYSCALL_DEFINE3(recvmsg, int, fd, struct user_msghdr __user *, msg,
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002163 unsigned int, flags)
2164{
2165 if (flags & MSG_CMSG_COMPAT)
2166 return -EINVAL;
2167 return __sys_recvmsg(fd, msg, flags);
2168}
2169
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002170/*
2171 * Linux recvmmsg interface
2172 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002174int __sys_recvmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
2175 unsigned int flags, struct timespec *timeout)
2176{
2177 int fput_needed, err, datagrams;
2178 struct socket *sock;
2179 struct mmsghdr __user *entry;
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002180 struct compat_mmsghdr __user *compat_entry;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002181 struct msghdr msg_sys;
2182 struct timespec end_time;
2183
2184 if (timeout &&
2185 poll_select_set_timeout(&end_time, timeout->tv_sec,
2186 timeout->tv_nsec))
2187 return -EINVAL;
2188
2189 datagrams = 0;
2190
2191 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2192 if (!sock)
2193 return err;
2194
2195 err = sock_error(sock->sk);
2196 if (err)
2197 goto out_put;
2198
2199 entry = mmsg;
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002200 compat_entry = (struct compat_mmsghdr __user *)mmsg;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002201
2202 while (datagrams < vlen) {
2203 /*
2204 * No need to ask LSM for more than the first datagram.
2205 */
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002206 if (MSG_CMSG_COMPAT & flags) {
Al Viro666547f2014-04-06 14:03:05 -04002207 err = ___sys_recvmsg(sock, (struct user_msghdr __user *)compat_entry,
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002208 &msg_sys, flags & ~MSG_WAITFORONE,
2209 datagrams);
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002210 if (err < 0)
2211 break;
2212 err = __put_user(err, &compat_entry->msg_len);
2213 ++compat_entry;
2214 } else {
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002215 err = ___sys_recvmsg(sock,
Al Viro666547f2014-04-06 14:03:05 -04002216 (struct user_msghdr __user *)entry,
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002217 &msg_sys, flags & ~MSG_WAITFORONE,
2218 datagrams);
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002219 if (err < 0)
2220 break;
2221 err = put_user(err, &entry->msg_len);
2222 ++entry;
2223 }
2224
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002225 if (err)
2226 break;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002227 ++datagrams;
2228
Brandon L Black71c5c152010-03-26 16:18:03 +00002229 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
2230 if (flags & MSG_WAITFORONE)
2231 flags |= MSG_DONTWAIT;
2232
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002233 if (timeout) {
2234 ktime_get_ts(timeout);
2235 *timeout = timespec_sub(end_time, *timeout);
2236 if (timeout->tv_sec < 0) {
2237 timeout->tv_sec = timeout->tv_nsec = 0;
2238 break;
2239 }
2240
2241 /* Timeout, return less than vlen datagrams */
2242 if (timeout->tv_nsec == 0 && timeout->tv_sec == 0)
2243 break;
2244 }
2245
2246 /* Out of band data, return right away */
2247 if (msg_sys.msg_flags & MSG_OOB)
2248 break;
2249 }
2250
2251out_put:
2252 fput_light(sock->file, fput_needed);
2253
2254 if (err == 0)
2255 return datagrams;
2256
2257 if (datagrams != 0) {
2258 /*
2259 * We may return less entries than requested (vlen) if the
2260 * sock is non block and there aren't enough datagrams...
2261 */
2262 if (err != -EAGAIN) {
2263 /*
2264 * ... or if recvmsg returns an error after we
2265 * received some datagrams, where we record the
2266 * error to return on the next call or if the
2267 * app asks about it using getsockopt(SO_ERROR).
2268 */
2269 sock->sk->sk_err = -err;
2270 }
2271
2272 return datagrams;
2273 }
2274
2275 return err;
2276}
2277
2278SYSCALL_DEFINE5(recvmmsg, int, fd, struct mmsghdr __user *, mmsg,
2279 unsigned int, vlen, unsigned int, flags,
2280 struct timespec __user *, timeout)
2281{
2282 int datagrams;
2283 struct timespec timeout_sys;
2284
Andy Lutomirski1be374a2013-05-22 14:07:44 -07002285 if (flags & MSG_CMSG_COMPAT)
2286 return -EINVAL;
2287
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002288 if (!timeout)
2289 return __sys_recvmmsg(fd, mmsg, vlen, flags, NULL);
2290
2291 if (copy_from_user(&timeout_sys, timeout, sizeof(timeout_sys)))
2292 return -EFAULT;
2293
2294 datagrams = __sys_recvmmsg(fd, mmsg, vlen, flags, &timeout_sys);
2295
2296 if (datagrams > 0 &&
2297 copy_to_user(timeout, &timeout_sys, sizeof(timeout_sys)))
2298 datagrams = -EFAULT;
2299
2300 return datagrams;
2301}
2302
2303#ifdef __ARCH_WANT_SYS_SOCKETCALL
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304/* Argument list sizes for sys_socketcall */
2305#define AL(x) ((x) * sizeof(unsigned long))
Anton Blanchard228e5482011-05-02 20:21:35 +00002306static const unsigned char nargs[21] = {
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002307 AL(0), AL(3), AL(3), AL(3), AL(2), AL(3),
2308 AL(3), AL(3), AL(4), AL(4), AL(4), AL(6),
2309 AL(6), AL(2), AL(5), AL(5), AL(3), AL(3),
Anton Blanchard228e5482011-05-02 20:21:35 +00002310 AL(4), AL(5), AL(4)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002311};
2312
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313#undef AL
2314
2315/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002316 * System call vectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 *
2318 * Argument checking cleaned up. Saved 20% in size.
2319 * This function doesn't need to set the kernel lock because
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002320 * it is set by the callees.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 */
2322
Heiko Carstens3e0fa652009-01-14 14:14:24 +01002323SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324{
Chen Gang2950fa92013-04-07 16:55:23 +08002325 unsigned long a[AUDITSC_ARGS];
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002326 unsigned long a0, a1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 int err;
Arjan van de Ven47379052009-09-28 12:57:44 -07002328 unsigned int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
Anton Blanchard228e5482011-05-02 20:21:35 +00002330 if (call < 1 || call > SYS_SENDMMSG)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 return -EINVAL;
2332
Arjan van de Ven47379052009-09-28 12:57:44 -07002333 len = nargs[call];
2334 if (len > sizeof(a))
2335 return -EINVAL;
2336
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 /* copy_from_user should be SMP safe. */
Arjan van de Ven47379052009-09-28 12:57:44 -07002338 if (copy_from_user(a, args, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 return -EFAULT;
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002340
Chen Gang2950fa92013-04-07 16:55:23 +08002341 err = audit_socketcall(nargs[call] / sizeof(unsigned long), a);
2342 if (err)
2343 return err;
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002344
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002345 a0 = a[0];
2346 a1 = a[1];
2347
2348 switch (call) {
2349 case SYS_SOCKET:
2350 err = sys_socket(a0, a1, a[2]);
2351 break;
2352 case SYS_BIND:
2353 err = sys_bind(a0, (struct sockaddr __user *)a1, a[2]);
2354 break;
2355 case SYS_CONNECT:
2356 err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
2357 break;
2358 case SYS_LISTEN:
2359 err = sys_listen(a0, a1);
2360 break;
2361 case SYS_ACCEPT:
Ulrich Drepperde11def2008-11-19 15:36:14 -08002362 err = sys_accept4(a0, (struct sockaddr __user *)a1,
2363 (int __user *)a[2], 0);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002364 break;
2365 case SYS_GETSOCKNAME:
2366 err =
2367 sys_getsockname(a0, (struct sockaddr __user *)a1,
2368 (int __user *)a[2]);
2369 break;
2370 case SYS_GETPEERNAME:
2371 err =
2372 sys_getpeername(a0, (struct sockaddr __user *)a1,
2373 (int __user *)a[2]);
2374 break;
2375 case SYS_SOCKETPAIR:
2376 err = sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
2377 break;
2378 case SYS_SEND:
2379 err = sys_send(a0, (void __user *)a1, a[2], a[3]);
2380 break;
2381 case SYS_SENDTO:
2382 err = sys_sendto(a0, (void __user *)a1, a[2], a[3],
2383 (struct sockaddr __user *)a[4], a[5]);
2384 break;
2385 case SYS_RECV:
2386 err = sys_recv(a0, (void __user *)a1, a[2], a[3]);
2387 break;
2388 case SYS_RECVFROM:
2389 err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
2390 (struct sockaddr __user *)a[4],
2391 (int __user *)a[5]);
2392 break;
2393 case SYS_SHUTDOWN:
2394 err = sys_shutdown(a0, a1);
2395 break;
2396 case SYS_SETSOCKOPT:
2397 err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]);
2398 break;
2399 case SYS_GETSOCKOPT:
2400 err =
2401 sys_getsockopt(a0, a1, a[2], (char __user *)a[3],
2402 (int __user *)a[4]);
2403 break;
2404 case SYS_SENDMSG:
Al Viro666547f2014-04-06 14:03:05 -04002405 err = sys_sendmsg(a0, (struct user_msghdr __user *)a1, a[2]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002406 break;
Anton Blanchard228e5482011-05-02 20:21:35 +00002407 case SYS_SENDMMSG:
2408 err = sys_sendmmsg(a0, (struct mmsghdr __user *)a1, a[2], a[3]);
2409 break;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002410 case SYS_RECVMSG:
Al Viro666547f2014-04-06 14:03:05 -04002411 err = sys_recvmsg(a0, (struct user_msghdr __user *)a1, a[2]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002412 break;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002413 case SYS_RECVMMSG:
2414 err = sys_recvmmsg(a0, (struct mmsghdr __user *)a1, a[2], a[3],
2415 (struct timespec __user *)a[4]);
2416 break;
Ulrich Drepperde11def2008-11-19 15:36:14 -08002417 case SYS_ACCEPT4:
2418 err = sys_accept4(a0, (struct sockaddr __user *)a1,
2419 (int __user *)a[2], a[3]);
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07002420 break;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002421 default:
2422 err = -EINVAL;
2423 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 }
2425 return err;
2426}
2427
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002428#endif /* __ARCH_WANT_SYS_SOCKETCALL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002430/**
2431 * sock_register - add a socket protocol handler
2432 * @ops: description of protocol
2433 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 * This function is called by a protocol handler that wants to
2435 * advertise its address family, and have it linked into the
Masanari Iidae793c0f2014-09-04 23:44:36 +09002436 * socket interface. The value ops->family corresponds to the
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002437 * socket system call protocol family.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 */
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002439int sock_register(const struct net_proto_family *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440{
2441 int err;
2442
2443 if (ops->family >= NPROTO) {
Yang Yingliang3410f222014-02-12 17:09:55 +08002444 pr_crit("protocol %d >= NPROTO(%d)\n", ops->family, NPROTO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 return -ENOBUFS;
2446 }
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002447
2448 spin_lock(&net_family_lock);
Eric Dumazet190683a2010-11-10 10:50:44 +00002449 if (rcu_dereference_protected(net_families[ops->family],
2450 lockdep_is_held(&net_family_lock)))
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002451 err = -EEXIST;
2452 else {
Eric Dumazetcf778b02012-01-12 04:41:32 +00002453 rcu_assign_pointer(net_families[ops->family], ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 err = 0;
2455 }
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002456 spin_unlock(&net_family_lock);
2457
Yang Yingliang3410f222014-02-12 17:09:55 +08002458 pr_info("NET: Registered protocol family %d\n", ops->family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 return err;
2460}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002461EXPORT_SYMBOL(sock_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002463/**
2464 * sock_unregister - remove a protocol handler
2465 * @family: protocol family to remove
2466 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 * This function is called by a protocol handler that wants to
2468 * remove its address family, and have it unlinked from the
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002469 * new socket creation.
2470 *
2471 * If protocol handler is a module, then it can use module reference
2472 * counts to protect against new references. If protocol handler is not
2473 * a module then it needs to provide its own protection in
2474 * the ops->create routine.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 */
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002476void sock_unregister(int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477{
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002478 BUG_ON(family < 0 || family >= NPROTO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002480 spin_lock(&net_family_lock);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00002481 RCU_INIT_POINTER(net_families[family], NULL);
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002482 spin_unlock(&net_family_lock);
2483
2484 synchronize_rcu();
2485
Yang Yingliang3410f222014-02-12 17:09:55 +08002486 pr_info("NET: Unregistered protocol family %d\n", family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002488EXPORT_SYMBOL(sock_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489
Andi Kleen77d76ea2005-12-22 12:43:42 -08002490static int __init sock_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491{
Nick Pigginb3e19d92011-01-07 17:50:11 +11002492 int err;
Eric W. Biederman2ca794e2012-04-19 13:20:32 +00002493 /*
2494 * Initialize the network sysctl infrastructure.
2495 */
2496 err = net_sysctl_init();
2497 if (err)
2498 goto out;
Nick Pigginb3e19d92011-01-07 17:50:11 +11002499
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002501 * Initialize skbuff SLAB cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 */
2503 skb_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504
2505 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002506 * Initialize the protocols module.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 */
2508
2509 init_inodecache();
Nick Pigginb3e19d92011-01-07 17:50:11 +11002510
2511 err = register_filesystem(&sock_fs_type);
2512 if (err)
2513 goto out_fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 sock_mnt = kern_mount(&sock_fs_type);
Nick Pigginb3e19d92011-01-07 17:50:11 +11002515 if (IS_ERR(sock_mnt)) {
2516 err = PTR_ERR(sock_mnt);
2517 goto out_mount;
2518 }
Andi Kleen77d76ea2005-12-22 12:43:42 -08002519
2520 /* The real protocol initialization is performed in later initcalls.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 */
2522
2523#ifdef CONFIG_NETFILTER
Pablo Neira Ayuso6d11cfd2013-05-22 22:42:36 +00002524 err = netfilter_init();
2525 if (err)
2526 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527#endif
David S. Millercbeb3212005-12-22 12:58:55 -08002528
Daniel Borkmann408eccc2014-04-01 16:20:23 +02002529 ptp_classifier_init();
Richard Cochranc1f19b52010-07-17 08:49:36 +00002530
Nick Pigginb3e19d92011-01-07 17:50:11 +11002531out:
2532 return err;
2533
2534out_mount:
2535 unregister_filesystem(&sock_fs_type);
2536out_fs:
2537 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538}
2539
Andi Kleen77d76ea2005-12-22 12:43:42 -08002540core_initcall(sock_init); /* early initcall */
2541
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542#ifdef CONFIG_PROC_FS
2543void socket_seq_show(struct seq_file *seq)
2544{
2545 int cpu;
2546 int counter = 0;
2547
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -07002548 for_each_possible_cpu(cpu)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002549 counter += per_cpu(sockets_in_use, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550
2551 /* It can be negative, by the way. 8) */
2552 if (counter < 0)
2553 counter = 0;
2554
2555 seq_printf(seq, "sockets: used %d\n", counter);
2556}
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002557#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558
Shaun Pereira89bbfc92006-03-21 23:58:08 -08002559#ifdef CONFIG_COMPAT
Arnd Bergmann6b960182009-11-06 23:10:54 -08002560static int do_siocgstamp(struct net *net, struct socket *sock,
H. Peter Anvin644595f2012-02-19 17:51:59 -08002561 unsigned int cmd, void __user *up)
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002562{
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002563 mm_segment_t old_fs = get_fs();
2564 struct timeval ktv;
2565 int err;
2566
2567 set_fs(KERNEL_DS);
Arnd Bergmann6b960182009-11-06 23:10:54 -08002568 err = sock_do_ioctl(net, sock, cmd, (unsigned long)&ktv);
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002569 set_fs(old_fs);
H. Peter Anvin644595f2012-02-19 17:51:59 -08002570 if (!err)
Mikulas Patockaed6fe9d2012-09-01 12:34:07 -04002571 err = compat_put_timeval(&ktv, up);
H. Peter Anvin644595f2012-02-19 17:51:59 -08002572
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002573 return err;
2574}
2575
Arnd Bergmann6b960182009-11-06 23:10:54 -08002576static int do_siocgstampns(struct net *net, struct socket *sock,
H. Peter Anvin644595f2012-02-19 17:51:59 -08002577 unsigned int cmd, void __user *up)
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002578{
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002579 mm_segment_t old_fs = get_fs();
2580 struct timespec kts;
2581 int err;
2582
2583 set_fs(KERNEL_DS);
Arnd Bergmann6b960182009-11-06 23:10:54 -08002584 err = sock_do_ioctl(net, sock, cmd, (unsigned long)&kts);
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002585 set_fs(old_fs);
H. Peter Anvin644595f2012-02-19 17:51:59 -08002586 if (!err)
Mikulas Patockaed6fe9d2012-09-01 12:34:07 -04002587 err = compat_put_timespec(&kts, up);
H. Peter Anvin644595f2012-02-19 17:51:59 -08002588
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002589 return err;
2590}
2591
Arnd Bergmann6b960182009-11-06 23:10:54 -08002592static int dev_ifname32(struct net *net, struct compat_ifreq __user *uifr32)
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002593{
2594 struct ifreq __user *uifr;
2595 int err;
2596
2597 uifr = compat_alloc_user_space(sizeof(struct ifreq));
Arnd Bergmann6b960182009-11-06 23:10:54 -08002598 if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002599 return -EFAULT;
2600
Arnd Bergmann6b960182009-11-06 23:10:54 -08002601 err = dev_ioctl(net, SIOCGIFNAME, uifr);
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002602 if (err)
2603 return err;
2604
Arnd Bergmann6b960182009-11-06 23:10:54 -08002605 if (copy_in_user(uifr32, uifr, sizeof(struct compat_ifreq)))
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002606 return -EFAULT;
2607
2608 return 0;
2609}
2610
Arnd Bergmann6b960182009-11-06 23:10:54 -08002611static int dev_ifconf(struct net *net, struct compat_ifconf __user *uifc32)
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002612{
Arnd Bergmann6b960182009-11-06 23:10:54 -08002613 struct compat_ifconf ifc32;
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002614 struct ifconf ifc;
2615 struct ifconf __user *uifc;
Arnd Bergmann6b960182009-11-06 23:10:54 -08002616 struct compat_ifreq __user *ifr32;
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002617 struct ifreq __user *ifr;
2618 unsigned int i, j;
2619 int err;
2620
Arnd Bergmann6b960182009-11-06 23:10:54 -08002621 if (copy_from_user(&ifc32, uifc32, sizeof(struct compat_ifconf)))
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002622 return -EFAULT;
2623
Mathias Krause43da5f22012-08-15 11:31:57 +00002624 memset(&ifc, 0, sizeof(ifc));
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002625 if (ifc32.ifcbuf == 0) {
2626 ifc32.ifc_len = 0;
2627 ifc.ifc_len = 0;
2628 ifc.ifc_req = NULL;
2629 uifc = compat_alloc_user_space(sizeof(struct ifconf));
2630 } else {
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002631 size_t len = ((ifc32.ifc_len / sizeof(struct compat_ifreq)) + 1) *
2632 sizeof(struct ifreq);
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002633 uifc = compat_alloc_user_space(sizeof(struct ifconf) + len);
2634 ifc.ifc_len = len;
2635 ifr = ifc.ifc_req = (void __user *)(uifc + 1);
2636 ifr32 = compat_ptr(ifc32.ifcbuf);
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002637 for (i = 0; i < ifc32.ifc_len; i += sizeof(struct compat_ifreq)) {
Arnd Bergmann6b960182009-11-06 23:10:54 -08002638 if (copy_in_user(ifr, ifr32, sizeof(struct compat_ifreq)))
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002639 return -EFAULT;
2640 ifr++;
2641 ifr32++;
2642 }
2643 }
2644 if (copy_to_user(uifc, &ifc, sizeof(struct ifconf)))
2645 return -EFAULT;
2646
Arnd Bergmann6b960182009-11-06 23:10:54 -08002647 err = dev_ioctl(net, SIOCGIFCONF, uifc);
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002648 if (err)
2649 return err;
2650
2651 if (copy_from_user(&ifc, uifc, sizeof(struct ifconf)))
2652 return -EFAULT;
2653
2654 ifr = ifc.ifc_req;
2655 ifr32 = compat_ptr(ifc32.ifcbuf);
2656 for (i = 0, j = 0;
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002657 i + sizeof(struct compat_ifreq) <= ifc32.ifc_len && j < ifc.ifc_len;
2658 i += sizeof(struct compat_ifreq), j += sizeof(struct ifreq)) {
2659 if (copy_in_user(ifr32, ifr, sizeof(struct compat_ifreq)))
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002660 return -EFAULT;
2661 ifr32++;
2662 ifr++;
2663 }
2664
2665 if (ifc32.ifcbuf == 0) {
2666 /* Translate from 64-bit structure multiple to
2667 * a 32-bit one.
2668 */
2669 i = ifc.ifc_len;
Arnd Bergmann6b960182009-11-06 23:10:54 -08002670 i = ((i / sizeof(struct ifreq)) * sizeof(struct compat_ifreq));
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002671 ifc32.ifc_len = i;
2672 } else {
2673 ifc32.ifc_len = i;
2674 }
Arnd Bergmann6b960182009-11-06 23:10:54 -08002675 if (copy_to_user(uifc32, &ifc32, sizeof(struct compat_ifconf)))
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002676 return -EFAULT;
2677
2678 return 0;
2679}
2680
Arnd Bergmann6b960182009-11-06 23:10:54 -08002681static int ethtool_ioctl(struct net *net, struct compat_ifreq __user *ifr32)
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002682{
Ben Hutchings3a7da392011-03-17 07:34:32 +00002683 struct compat_ethtool_rxnfc __user *compat_rxnfc;
2684 bool convert_in = false, convert_out = false;
2685 size_t buf_size = ALIGN(sizeof(struct ifreq), 8);
2686 struct ethtool_rxnfc __user *rxnfc;
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002687 struct ifreq __user *ifr;
Ben Hutchings3a7da392011-03-17 07:34:32 +00002688 u32 rule_cnt = 0, actual_rule_cnt;
2689 u32 ethcmd;
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002690 u32 data;
Ben Hutchings3a7da392011-03-17 07:34:32 +00002691 int ret;
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002692
2693 if (get_user(data, &ifr32->ifr_ifru.ifru_data))
2694 return -EFAULT;
2695
Ben Hutchings3a7da392011-03-17 07:34:32 +00002696 compat_rxnfc = compat_ptr(data);
2697
2698 if (get_user(ethcmd, &compat_rxnfc->cmd))
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002699 return -EFAULT;
2700
Ben Hutchings3a7da392011-03-17 07:34:32 +00002701 /* Most ethtool structures are defined without padding.
2702 * Unfortunately struct ethtool_rxnfc is an exception.
2703 */
2704 switch (ethcmd) {
2705 default:
2706 break;
2707 case ETHTOOL_GRXCLSRLALL:
2708 /* Buffer size is variable */
2709 if (get_user(rule_cnt, &compat_rxnfc->rule_cnt))
2710 return -EFAULT;
2711 if (rule_cnt > KMALLOC_MAX_SIZE / sizeof(u32))
2712 return -ENOMEM;
2713 buf_size += rule_cnt * sizeof(u32);
2714 /* fall through */
2715 case ETHTOOL_GRXRINGS:
2716 case ETHTOOL_GRXCLSRLCNT:
2717 case ETHTOOL_GRXCLSRULE:
Ben Hutchings55664f32012-01-03 12:04:51 +00002718 case ETHTOOL_SRXCLSRLINS:
Ben Hutchings3a7da392011-03-17 07:34:32 +00002719 convert_out = true;
2720 /* fall through */
2721 case ETHTOOL_SRXCLSRLDEL:
Ben Hutchings3a7da392011-03-17 07:34:32 +00002722 buf_size += sizeof(struct ethtool_rxnfc);
2723 convert_in = true;
2724 break;
2725 }
2726
2727 ifr = compat_alloc_user_space(buf_size);
Stephen Hemminger954b1242013-02-11 06:22:28 +00002728 rxnfc = (void __user *)ifr + ALIGN(sizeof(struct ifreq), 8);
Ben Hutchings3a7da392011-03-17 07:34:32 +00002729
2730 if (copy_in_user(&ifr->ifr_name, &ifr32->ifr_name, IFNAMSIZ))
2731 return -EFAULT;
2732
2733 if (put_user(convert_in ? rxnfc : compat_ptr(data),
2734 &ifr->ifr_ifru.ifru_data))
2735 return -EFAULT;
2736
2737 if (convert_in) {
Alexander Duyck127fe532011-04-08 18:01:59 +00002738 /* We expect there to be holes between fs.m_ext and
Ben Hutchings3a7da392011-03-17 07:34:32 +00002739 * fs.ring_cookie and at the end of fs, but nowhere else.
2740 */
Alexander Duyck127fe532011-04-08 18:01:59 +00002741 BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc, fs.m_ext) +
2742 sizeof(compat_rxnfc->fs.m_ext) !=
2743 offsetof(struct ethtool_rxnfc, fs.m_ext) +
2744 sizeof(rxnfc->fs.m_ext));
Ben Hutchings3a7da392011-03-17 07:34:32 +00002745 BUILD_BUG_ON(
2746 offsetof(struct compat_ethtool_rxnfc, fs.location) -
2747 offsetof(struct compat_ethtool_rxnfc, fs.ring_cookie) !=
2748 offsetof(struct ethtool_rxnfc, fs.location) -
2749 offsetof(struct ethtool_rxnfc, fs.ring_cookie));
2750
2751 if (copy_in_user(rxnfc, compat_rxnfc,
Stephen Hemminger954b1242013-02-11 06:22:28 +00002752 (void __user *)(&rxnfc->fs.m_ext + 1) -
2753 (void __user *)rxnfc) ||
Ben Hutchings3a7da392011-03-17 07:34:32 +00002754 copy_in_user(&rxnfc->fs.ring_cookie,
2755 &compat_rxnfc->fs.ring_cookie,
Stephen Hemminger954b1242013-02-11 06:22:28 +00002756 (void __user *)(&rxnfc->fs.location + 1) -
2757 (void __user *)&rxnfc->fs.ring_cookie) ||
Ben Hutchings3a7da392011-03-17 07:34:32 +00002758 copy_in_user(&rxnfc->rule_cnt, &compat_rxnfc->rule_cnt,
2759 sizeof(rxnfc->rule_cnt)))
2760 return -EFAULT;
2761 }
2762
2763 ret = dev_ioctl(net, SIOCETHTOOL, ifr);
2764 if (ret)
2765 return ret;
2766
2767 if (convert_out) {
2768 if (copy_in_user(compat_rxnfc, rxnfc,
Stephen Hemminger954b1242013-02-11 06:22:28 +00002769 (const void __user *)(&rxnfc->fs.m_ext + 1) -
2770 (const void __user *)rxnfc) ||
Ben Hutchings3a7da392011-03-17 07:34:32 +00002771 copy_in_user(&compat_rxnfc->fs.ring_cookie,
2772 &rxnfc->fs.ring_cookie,
Stephen Hemminger954b1242013-02-11 06:22:28 +00002773 (const void __user *)(&rxnfc->fs.location + 1) -
2774 (const void __user *)&rxnfc->fs.ring_cookie) ||
Ben Hutchings3a7da392011-03-17 07:34:32 +00002775 copy_in_user(&compat_rxnfc->rule_cnt, &rxnfc->rule_cnt,
2776 sizeof(rxnfc->rule_cnt)))
2777 return -EFAULT;
2778
2779 if (ethcmd == ETHTOOL_GRXCLSRLALL) {
2780 /* As an optimisation, we only copy the actual
2781 * number of rules that the underlying
2782 * function returned. Since Mallory might
2783 * change the rule count in user memory, we
2784 * check that it is less than the rule count
2785 * originally given (as the user buffer size),
2786 * which has been range-checked.
2787 */
2788 if (get_user(actual_rule_cnt, &rxnfc->rule_cnt))
2789 return -EFAULT;
2790 if (actual_rule_cnt < rule_cnt)
2791 rule_cnt = actual_rule_cnt;
2792 if (copy_in_user(&compat_rxnfc->rule_locs[0],
2793 &rxnfc->rule_locs[0],
2794 rule_cnt * sizeof(u32)))
2795 return -EFAULT;
2796 }
2797 }
2798
2799 return 0;
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002800}
2801
Arnd Bergmann7a50a242009-11-08 20:57:03 -08002802static int compat_siocwandev(struct net *net, struct compat_ifreq __user *uifr32)
2803{
2804 void __user *uptr;
2805 compat_uptr_t uptr32;
2806 struct ifreq __user *uifr;
2807
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002808 uifr = compat_alloc_user_space(sizeof(*uifr));
Arnd Bergmann7a50a242009-11-08 20:57:03 -08002809 if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
2810 return -EFAULT;
2811
2812 if (get_user(uptr32, &uifr32->ifr_settings.ifs_ifsu))
2813 return -EFAULT;
2814
2815 uptr = compat_ptr(uptr32);
2816
2817 if (put_user(uptr, &uifr->ifr_settings.ifs_ifsu.raw_hdlc))
2818 return -EFAULT;
2819
2820 return dev_ioctl(net, SIOCWANDEV, uifr);
2821}
2822
Arnd Bergmann6b960182009-11-06 23:10:54 -08002823static int bond_ioctl(struct net *net, unsigned int cmd,
2824 struct compat_ifreq __user *ifr32)
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002825{
2826 struct ifreq kifr;
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002827 mm_segment_t old_fs;
2828 int err;
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002829
2830 switch (cmd) {
2831 case SIOCBONDENSLAVE:
2832 case SIOCBONDRELEASE:
2833 case SIOCBONDSETHWADDR:
2834 case SIOCBONDCHANGEACTIVE:
Arnd Bergmann6b960182009-11-06 23:10:54 -08002835 if (copy_from_user(&kifr, ifr32, sizeof(struct compat_ifreq)))
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002836 return -EFAULT;
2837
2838 old_fs = get_fs();
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002839 set_fs(KERNEL_DS);
stephen hemmingerc3f52ae2011-02-23 09:06:48 +00002840 err = dev_ioctl(net, cmd,
2841 (struct ifreq __user __force *) &kifr);
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002842 set_fs(old_fs);
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002843
2844 return err;
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002845 default:
Linus Torvalds07d106d2012-01-05 15:40:12 -08002846 return -ENOIOCTLCMD;
Joe Perchesccbd6a52010-05-14 10:58:26 +00002847 }
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002848}
2849
Ben Hutchings590d4692013-11-18 17:04:13 +00002850/* Handle ioctls that use ifreq::ifr_data and just need struct ifreq converted */
2851static int compat_ifr_data_ioctl(struct net *net, unsigned int cmd,
Arnd Bergmann6b960182009-11-06 23:10:54 -08002852 struct compat_ifreq __user *u_ifreq32)
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002853{
2854 struct ifreq __user *u_ifreq64;
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002855 char tmp_buf[IFNAMSIZ];
2856 void __user *data64;
2857 u32 data32;
2858
2859 if (copy_from_user(&tmp_buf[0], &(u_ifreq32->ifr_ifrn.ifrn_name[0]),
2860 IFNAMSIZ))
2861 return -EFAULT;
Ben Hutchings417c3522013-11-18 17:04:58 +00002862 if (get_user(data32, &u_ifreq32->ifr_ifru.ifru_data))
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002863 return -EFAULT;
2864 data64 = compat_ptr(data32);
2865
2866 u_ifreq64 = compat_alloc_user_space(sizeof(*u_ifreq64));
2867
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002868 if (copy_to_user(&u_ifreq64->ifr_ifrn.ifrn_name[0], &tmp_buf[0],
2869 IFNAMSIZ))
2870 return -EFAULT;
Ben Hutchings417c3522013-11-18 17:04:58 +00002871 if (put_user(data64, &u_ifreq64->ifr_ifru.ifru_data))
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002872 return -EFAULT;
2873
Arnd Bergmann6b960182009-11-06 23:10:54 -08002874 return dev_ioctl(net, cmd, u_ifreq64);
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002875}
2876
Arnd Bergmann6b960182009-11-06 23:10:54 -08002877static int dev_ifsioc(struct net *net, struct socket *sock,
2878 unsigned int cmd, struct compat_ifreq __user *uifr32)
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002879{
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00002880 struct ifreq __user *uifr;
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002881 int err;
2882
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00002883 uifr = compat_alloc_user_space(sizeof(*uifr));
2884 if (copy_in_user(uifr, uifr32, sizeof(*uifr32)))
2885 return -EFAULT;
2886
2887 err = sock_do_ioctl(net, sock, cmd, (unsigned long)uifr);
2888
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002889 if (!err) {
2890 switch (cmd) {
2891 case SIOCGIFFLAGS:
2892 case SIOCGIFMETRIC:
2893 case SIOCGIFMTU:
2894 case SIOCGIFMEM:
2895 case SIOCGIFHWADDR:
2896 case SIOCGIFINDEX:
2897 case SIOCGIFADDR:
2898 case SIOCGIFBRDADDR:
2899 case SIOCGIFDSTADDR:
2900 case SIOCGIFNETMASK:
Arnd Bergmannfab25322009-11-08 20:56:21 -08002901 case SIOCGIFPFLAGS:
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002902 case SIOCGIFTXQLEN:
Arnd Bergmannfab25322009-11-08 20:56:21 -08002903 case SIOCGMIIPHY:
2904 case SIOCGMIIREG:
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00002905 if (copy_in_user(uifr32, uifr, sizeof(*uifr32)))
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002906 err = -EFAULT;
2907 break;
2908 }
2909 }
2910 return err;
2911}
2912
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00002913static int compat_sioc_ifmap(struct net *net, unsigned int cmd,
2914 struct compat_ifreq __user *uifr32)
2915{
2916 struct ifreq ifr;
2917 struct compat_ifmap __user *uifmap32;
2918 mm_segment_t old_fs;
2919 int err;
2920
2921 uifmap32 = &uifr32->ifr_ifru.ifru_map;
2922 err = copy_from_user(&ifr, uifr32, sizeof(ifr.ifr_name));
Mathieu Desnoyers3ddc5b42013-09-11 14:23:18 -07002923 err |= get_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
2924 err |= get_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
2925 err |= get_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
2926 err |= get_user(ifr.ifr_map.irq, &uifmap32->irq);
2927 err |= get_user(ifr.ifr_map.dma, &uifmap32->dma);
2928 err |= get_user(ifr.ifr_map.port, &uifmap32->port);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00002929 if (err)
2930 return -EFAULT;
2931
2932 old_fs = get_fs();
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002933 set_fs(KERNEL_DS);
stephen hemmingerc3f52ae2011-02-23 09:06:48 +00002934 err = dev_ioctl(net, cmd, (void __user __force *)&ifr);
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002935 set_fs(old_fs);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00002936
2937 if (cmd == SIOCGIFMAP && !err) {
2938 err = copy_to_user(uifr32, &ifr, sizeof(ifr.ifr_name));
Mathieu Desnoyers3ddc5b42013-09-11 14:23:18 -07002939 err |= put_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
2940 err |= put_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
2941 err |= put_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
2942 err |= put_user(ifr.ifr_map.irq, &uifmap32->irq);
2943 err |= put_user(ifr.ifr_map.dma, &uifmap32->dma);
2944 err |= put_user(ifr.ifr_map.port, &uifmap32->port);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00002945 if (err)
2946 err = -EFAULT;
2947 }
2948 return err;
2949}
2950
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002951struct rtentry32 {
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002952 u32 rt_pad1;
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002953 struct sockaddr rt_dst; /* target address */
2954 struct sockaddr rt_gateway; /* gateway addr (RTF_GATEWAY) */
2955 struct sockaddr rt_genmask; /* target network mask (IP) */
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002956 unsigned short rt_flags;
2957 short rt_pad2;
2958 u32 rt_pad3;
2959 unsigned char rt_tos;
2960 unsigned char rt_class;
2961 short rt_pad4;
2962 short rt_metric; /* +1 for binary compatibility! */
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002963 /* char * */ u32 rt_dev; /* forcing the device at add */
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002964 u32 rt_mtu; /* per route MTU/Window */
2965 u32 rt_window; /* Window clamping */
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002966 unsigned short rt_irtt; /* Initial RTT */
2967};
2968
2969struct in6_rtmsg32 {
2970 struct in6_addr rtmsg_dst;
2971 struct in6_addr rtmsg_src;
2972 struct in6_addr rtmsg_gateway;
2973 u32 rtmsg_type;
2974 u16 rtmsg_dst_len;
2975 u16 rtmsg_src_len;
2976 u32 rtmsg_metric;
2977 u32 rtmsg_info;
2978 u32 rtmsg_flags;
2979 s32 rtmsg_ifindex;
2980};
2981
Arnd Bergmann6b960182009-11-06 23:10:54 -08002982static int routing_ioctl(struct net *net, struct socket *sock,
2983 unsigned int cmd, void __user *argp)
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002984{
2985 int ret;
2986 void *r = NULL;
2987 struct in6_rtmsg r6;
2988 struct rtentry r4;
2989 char devname[16];
2990 u32 rtdev;
2991 mm_segment_t old_fs = get_fs();
2992
Arnd Bergmann6b960182009-11-06 23:10:54 -08002993 if (sock && sock->sk && sock->sk->sk_family == AF_INET6) { /* ipv6 */
2994 struct in6_rtmsg32 __user *ur6 = argp;
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002995 ret = copy_from_user(&r6.rtmsg_dst, &(ur6->rtmsg_dst),
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002996 3 * sizeof(struct in6_addr));
Mathieu Desnoyers3ddc5b42013-09-11 14:23:18 -07002997 ret |= get_user(r6.rtmsg_type, &(ur6->rtmsg_type));
2998 ret |= get_user(r6.rtmsg_dst_len, &(ur6->rtmsg_dst_len));
2999 ret |= get_user(r6.rtmsg_src_len, &(ur6->rtmsg_src_len));
3000 ret |= get_user(r6.rtmsg_metric, &(ur6->rtmsg_metric));
3001 ret |= get_user(r6.rtmsg_info, &(ur6->rtmsg_info));
3002 ret |= get_user(r6.rtmsg_flags, &(ur6->rtmsg_flags));
3003 ret |= get_user(r6.rtmsg_ifindex, &(ur6->rtmsg_ifindex));
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003004
3005 r = (void *) &r6;
3006 } else { /* ipv4 */
Arnd Bergmann6b960182009-11-06 23:10:54 -08003007 struct rtentry32 __user *ur4 = argp;
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003008 ret = copy_from_user(&r4.rt_dst, &(ur4->rt_dst),
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003009 3 * sizeof(struct sockaddr));
Mathieu Desnoyers3ddc5b42013-09-11 14:23:18 -07003010 ret |= get_user(r4.rt_flags, &(ur4->rt_flags));
3011 ret |= get_user(r4.rt_metric, &(ur4->rt_metric));
3012 ret |= get_user(r4.rt_mtu, &(ur4->rt_mtu));
3013 ret |= get_user(r4.rt_window, &(ur4->rt_window));
3014 ret |= get_user(r4.rt_irtt, &(ur4->rt_irtt));
3015 ret |= get_user(rtdev, &(ur4->rt_dev));
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003016 if (rtdev) {
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003017 ret |= copy_from_user(devname, compat_ptr(rtdev), 15);
stephen hemmingerc3f52ae2011-02-23 09:06:48 +00003018 r4.rt_dev = (char __user __force *)devname;
3019 devname[15] = 0;
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003020 } else
3021 r4.rt_dev = NULL;
3022
3023 r = (void *) &r4;
3024 }
3025
3026 if (ret) {
3027 ret = -EFAULT;
3028 goto out;
3029 }
3030
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003031 set_fs(KERNEL_DS);
Arnd Bergmann6b960182009-11-06 23:10:54 -08003032 ret = sock_do_ioctl(net, sock, cmd, (unsigned long) r);
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003033 set_fs(old_fs);
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003034
3035out:
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003036 return ret;
3037}
3038
3039/* Since old style bridge ioctl's endup using SIOCDEVPRIVATE
3040 * for some operations; this forces use of the newer bridge-utils that
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003041 * use compatible ioctls
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003042 */
Arnd Bergmann6b960182009-11-06 23:10:54 -08003043static int old_bridge_ioctl(compat_ulong_t __user *argp)
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003044{
Arnd Bergmann6b960182009-11-06 23:10:54 -08003045 compat_ulong_t tmp;
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003046
Arnd Bergmann6b960182009-11-06 23:10:54 -08003047 if (get_user(tmp, argp))
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003048 return -EFAULT;
3049 if (tmp == BRCTL_GET_VERSION)
3050 return BRCTL_VERSION + 1;
3051 return -EINVAL;
3052}
3053
Arnd Bergmann6b960182009-11-06 23:10:54 -08003054static int compat_sock_ioctl_trans(struct file *file, struct socket *sock,
3055 unsigned int cmd, unsigned long arg)
3056{
3057 void __user *argp = compat_ptr(arg);
3058 struct sock *sk = sock->sk;
3059 struct net *net = sock_net(sk);
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003060
Arnd Bergmann6b960182009-11-06 23:10:54 -08003061 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15))
Ben Hutchings590d4692013-11-18 17:04:13 +00003062 return compat_ifr_data_ioctl(net, cmd, argp);
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003063
Arnd Bergmann6b960182009-11-06 23:10:54 -08003064 switch (cmd) {
3065 case SIOCSIFBR:
3066 case SIOCGIFBR:
3067 return old_bridge_ioctl(argp);
3068 case SIOCGIFNAME:
3069 return dev_ifname32(net, argp);
3070 case SIOCGIFCONF:
3071 return dev_ifconf(net, argp);
3072 case SIOCETHTOOL:
3073 return ethtool_ioctl(net, argp);
Arnd Bergmann7a50a242009-11-08 20:57:03 -08003074 case SIOCWANDEV:
3075 return compat_siocwandev(net, argp);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003076 case SIOCGIFMAP:
3077 case SIOCSIFMAP:
3078 return compat_sioc_ifmap(net, cmd, argp);
Arnd Bergmann6b960182009-11-06 23:10:54 -08003079 case SIOCBONDENSLAVE:
3080 case SIOCBONDRELEASE:
3081 case SIOCBONDSETHWADDR:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003082 case SIOCBONDCHANGEACTIVE:
3083 return bond_ioctl(net, cmd, argp);
3084 case SIOCADDRT:
3085 case SIOCDELRT:
3086 return routing_ioctl(net, sock, cmd, argp);
3087 case SIOCGSTAMP:
3088 return do_siocgstamp(net, sock, cmd, argp);
3089 case SIOCGSTAMPNS:
3090 return do_siocgstampns(net, sock, cmd, argp);
Ben Hutchings590d4692013-11-18 17:04:13 +00003091 case SIOCBONDSLAVEINFOQUERY:
3092 case SIOCBONDINFOQUERY:
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003093 case SIOCSHWTSTAMP:
Ben Hutchingsfd468c72013-11-14 01:19:29 +00003094 case SIOCGHWTSTAMP:
Ben Hutchings590d4692013-11-18 17:04:13 +00003095 return compat_ifr_data_ioctl(net, cmd, argp);
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003096
Arnd Bergmann6b960182009-11-06 23:10:54 -08003097 case FIOSETOWN:
3098 case SIOCSPGRP:
3099 case FIOGETOWN:
3100 case SIOCGPGRP:
3101 case SIOCBRADDBR:
3102 case SIOCBRDELBR:
3103 case SIOCGIFVLAN:
3104 case SIOCSIFVLAN:
3105 case SIOCADDDLCI:
3106 case SIOCDELDLCI:
3107 return sock_ioctl(file, cmd, arg);
3108
3109 case SIOCGIFFLAGS:
3110 case SIOCSIFFLAGS:
3111 case SIOCGIFMETRIC:
3112 case SIOCSIFMETRIC:
3113 case SIOCGIFMTU:
3114 case SIOCSIFMTU:
3115 case SIOCGIFMEM:
3116 case SIOCSIFMEM:
3117 case SIOCGIFHWADDR:
3118 case SIOCSIFHWADDR:
3119 case SIOCADDMULTI:
3120 case SIOCDELMULTI:
3121 case SIOCGIFINDEX:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003122 case SIOCGIFADDR:
3123 case SIOCSIFADDR:
3124 case SIOCSIFHWBROADCAST:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003125 case SIOCDIFADDR:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003126 case SIOCGIFBRDADDR:
3127 case SIOCSIFBRDADDR:
3128 case SIOCGIFDSTADDR:
3129 case SIOCSIFDSTADDR:
3130 case SIOCGIFNETMASK:
3131 case SIOCSIFNETMASK:
3132 case SIOCSIFPFLAGS:
3133 case SIOCGIFPFLAGS:
3134 case SIOCGIFTXQLEN:
3135 case SIOCSIFTXQLEN:
3136 case SIOCBRADDIF:
3137 case SIOCBRDELIF:
Arnd Bergmann9177efd2009-11-06 08:09:09 +00003138 case SIOCSIFNAME:
3139 case SIOCGMIIPHY:
3140 case SIOCGMIIREG:
3141 case SIOCSMIIREG:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003142 return dev_ifsioc(net, sock, cmd, argp);
Arnd Bergmann9177efd2009-11-06 08:09:09 +00003143
Arnd Bergmann6b960182009-11-06 23:10:54 -08003144 case SIOCSARP:
3145 case SIOCGARP:
3146 case SIOCDARP:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003147 case SIOCATMARK:
Arnd Bergmann9177efd2009-11-06 08:09:09 +00003148 return sock_do_ioctl(net, sock, cmd, arg);
3149 }
3150
Arnd Bergmann6b960182009-11-06 23:10:54 -08003151 return -ENOIOCTLCMD;
3152}
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003153
Eric Dumazet95c96172012-04-15 05:58:06 +00003154static long compat_sock_ioctl(struct file *file, unsigned int cmd,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07003155 unsigned long arg)
Shaun Pereira89bbfc92006-03-21 23:58:08 -08003156{
3157 struct socket *sock = file->private_data;
3158 int ret = -ENOIOCTLCMD;
David S. Miller87de87d2008-06-03 09:14:03 -07003159 struct sock *sk;
3160 struct net *net;
3161
3162 sk = sock->sk;
3163 net = sock_net(sk);
Shaun Pereira89bbfc92006-03-21 23:58:08 -08003164
3165 if (sock->ops->compat_ioctl)
3166 ret = sock->ops->compat_ioctl(sock, cmd, arg);
3167
David S. Miller87de87d2008-06-03 09:14:03 -07003168 if (ret == -ENOIOCTLCMD &&
3169 (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST))
3170 ret = compat_wext_handle_ioctl(net, cmd, arg);
3171
Arnd Bergmann6b960182009-11-06 23:10:54 -08003172 if (ret == -ENOIOCTLCMD)
3173 ret = compat_sock_ioctl_trans(file, sock, cmd, arg);
3174
Shaun Pereira89bbfc92006-03-21 23:58:08 -08003175 return ret;
3176}
3177#endif
3178
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003179int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen)
3180{
3181 return sock->ops->bind(sock, addr, addrlen);
3182}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003183EXPORT_SYMBOL(kernel_bind);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003184
3185int kernel_listen(struct socket *sock, int backlog)
3186{
3187 return sock->ops->listen(sock, backlog);
3188}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003189EXPORT_SYMBOL(kernel_listen);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003190
3191int kernel_accept(struct socket *sock, struct socket **newsock, int flags)
3192{
3193 struct sock *sk = sock->sk;
3194 int err;
3195
3196 err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
3197 newsock);
3198 if (err < 0)
3199 goto done;
3200
3201 err = sock->ops->accept(sock, *newsock, flags);
3202 if (err < 0) {
3203 sock_release(*newsock);
Tony Battersbyfa8705b2007-10-10 21:09:04 -07003204 *newsock = NULL;
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003205 goto done;
3206 }
3207
3208 (*newsock)->ops = sock->ops;
Wei Yongjun1b085342008-12-18 19:35:10 -08003209 __module_get((*newsock)->ops->owner);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003210
3211done:
3212 return err;
3213}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003214EXPORT_SYMBOL(kernel_accept);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003215
3216int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
YOSHIFUJI Hideaki4768fbc2007-02-09 23:25:31 +09003217 int flags)
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003218{
3219 return sock->ops->connect(sock, addr, addrlen, flags);
3220}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003221EXPORT_SYMBOL(kernel_connect);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003222
3223int kernel_getsockname(struct socket *sock, struct sockaddr *addr,
3224 int *addrlen)
3225{
3226 return sock->ops->getname(sock, addr, addrlen, 0);
3227}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003228EXPORT_SYMBOL(kernel_getsockname);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003229
3230int kernel_getpeername(struct socket *sock, struct sockaddr *addr,
3231 int *addrlen)
3232{
3233 return sock->ops->getname(sock, addr, addrlen, 1);
3234}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003235EXPORT_SYMBOL(kernel_getpeername);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003236
3237int kernel_getsockopt(struct socket *sock, int level, int optname,
3238 char *optval, int *optlen)
3239{
3240 mm_segment_t oldfs = get_fs();
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003241 char __user *uoptval;
3242 int __user *uoptlen;
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003243 int err;
3244
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003245 uoptval = (char __user __force *) optval;
3246 uoptlen = (int __user __force *) optlen;
3247
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003248 set_fs(KERNEL_DS);
3249 if (level == SOL_SOCKET)
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003250 err = sock_getsockopt(sock, level, optname, uoptval, uoptlen);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003251 else
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003252 err = sock->ops->getsockopt(sock, level, optname, uoptval,
3253 uoptlen);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003254 set_fs(oldfs);
3255 return err;
3256}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003257EXPORT_SYMBOL(kernel_getsockopt);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003258
3259int kernel_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07003260 char *optval, unsigned int optlen)
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003261{
3262 mm_segment_t oldfs = get_fs();
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003263 char __user *uoptval;
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003264 int err;
3265
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003266 uoptval = (char __user __force *) optval;
3267
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003268 set_fs(KERNEL_DS);
3269 if (level == SOL_SOCKET)
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003270 err = sock_setsockopt(sock, level, optname, uoptval, optlen);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003271 else
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003272 err = sock->ops->setsockopt(sock, level, optname, uoptval,
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003273 optlen);
3274 set_fs(oldfs);
3275 return err;
3276}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003277EXPORT_SYMBOL(kernel_setsockopt);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003278
3279int kernel_sendpage(struct socket *sock, struct page *page, int offset,
3280 size_t size, int flags)
3281{
3282 if (sock->ops->sendpage)
3283 return sock->ops->sendpage(sock, page, offset, size, flags);
3284
3285 return sock_no_sendpage(sock, page, offset, size, flags);
3286}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003287EXPORT_SYMBOL(kernel_sendpage);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003288
3289int kernel_sock_ioctl(struct socket *sock, int cmd, unsigned long arg)
3290{
3291 mm_segment_t oldfs = get_fs();
3292 int err;
3293
3294 set_fs(KERNEL_DS);
3295 err = sock->ops->ioctl(sock, cmd, arg);
3296 set_fs(oldfs);
3297
3298 return err;
3299}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003300EXPORT_SYMBOL(kernel_sock_ioctl);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003301
Trond Myklebust91cf45f2007-11-12 18:10:39 -08003302int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how)
3303{
3304 return sock->ops->shutdown(sock, how);
3305}
Trond Myklebust91cf45f2007-11-12 18:10:39 -08003306EXPORT_SYMBOL(kernel_sock_shutdown);