blob: fc90b4f0da3c0b30f7d08b68aafe7dcc272c5f58 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#include <linux/init.h>
76#include <linux/poll.h>
77#include <linux/cache.h>
78#include <linux/module.h>
79#include <linux/highmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#include <linux/mount.h>
81#include <linux/security.h>
82#include <linux/syscalls.h>
83#include <linux/compat.h>
84#include <linux/kmod.h>
David Woodhouse3ec3b2f2005-05-17 12:08:48 +010085#include <linux/audit.h>
Adrian Bunkd86b5e02006-01-21 00:46:55 +010086#include <linux/wireless.h>
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -070087#include <linux/nsproxy.h>
Nick Black1fd7317d2009-09-22 16:43:33 -070088#include <linux/magic.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090089#include <linux/slab.h>
Masatake YAMATO600e1772012-08-29 10:44:29 +000090#include <linux/xattr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92#include <asm/uaccess.h>
93#include <asm/unistd.h>
94
95#include <net/compat.h>
David S. Miller87de87d2008-06-03 09:14:03 -070096#include <net/wext.h>
Herbert Xuf8451722010-05-24 00:12:34 -070097#include <net/cls_cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99#include <net/sock.h>
100#include <linux/netfilter.h>
101
Arnd Bergmann6b960182009-11-06 23:10:54 -0800102#include <linux/if_tun.h>
103#include <linux/ipv6_route.h>
104#include <linux/route.h>
Arnd Bergmann6b960182009-11-06 23:10:54 -0800105#include <linux/sockios.h>
106#include <linux/atalk.h>
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108static int sock_no_open(struct inode *irrelevant, struct file *dontcare);
Badari Pulavarty027445c2006-09-30 23:28:46 -0700109static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov,
110 unsigned long nr_segs, loff_t pos);
111static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov,
112 unsigned long nr_segs, loff_t pos);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700113static int sock_mmap(struct file *file, struct vm_area_struct *vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115static int sock_close(struct inode *inode, struct file *file);
116static unsigned int sock_poll(struct file *file,
117 struct poll_table_struct *wait);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700118static long sock_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
Shaun Pereira89bbfc92006-03-21 23:58:08 -0800119#ifdef CONFIG_COMPAT
120static long compat_sock_ioctl(struct file *file,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700121 unsigned int cmd, unsigned long arg);
Shaun Pereira89bbfc92006-03-21 23:58:08 -0800122#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123static int sock_fasync(int fd, struct file *filp, int on);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124static ssize_t sock_sendpage(struct file *file, struct page *page,
125 int offset, size_t size, loff_t *ppos, int more);
Jens Axboe9c55e012007-11-06 23:30:13 -0800126static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700127 struct pipe_inode_info *pipe, size_t len,
Jens Axboe9c55e012007-11-06 23:30:13 -0800128 unsigned int flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130/*
131 * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
132 * in the operation structures but are done directly via the socketcall() multiplexor.
133 */
134
Arjan van de Venda7071d2007-02-12 00:55:36 -0800135static const struct file_operations socket_file_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 .owner = THIS_MODULE,
137 .llseek = no_llseek,
138 .aio_read = sock_aio_read,
139 .aio_write = sock_aio_write,
140 .poll = sock_poll,
141 .unlocked_ioctl = sock_ioctl,
Shaun Pereira89bbfc92006-03-21 23:58:08 -0800142#ifdef CONFIG_COMPAT
143 .compat_ioctl = compat_sock_ioctl,
144#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 .mmap = sock_mmap,
146 .open = sock_no_open, /* special open code to disallow open via /proc */
147 .release = sock_close,
148 .fasync = sock_fasync,
Jens Axboe5274f052006-03-30 15:15:30 +0200149 .sendpage = sock_sendpage,
150 .splice_write = generic_splice_sendpage,
Jens Axboe9c55e012007-11-06 23:30:13 -0800151 .splice_read = sock_splice_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152};
153
154/*
155 * The protocol list. Each protocol is registered in here.
156 */
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158static DEFINE_SPINLOCK(net_family_lock);
Eric Dumazet190683a2010-11-10 10:50:44 +0000159static const struct net_proto_family __rcu *net_families[NPROTO] __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161/*
162 * Statistics counters of the socket lists
163 */
164
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700165static DEFINE_PER_CPU(int, sockets_in_use);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700168 * Support routines.
169 * Move socket addresses back and forth across the kernel/user
170 * divide and look after the messy bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 */
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173/**
174 * move_addr_to_kernel - copy a socket address into kernel space
175 * @uaddr: Address in user space
176 * @kaddr: Address in kernel space
177 * @ulen: Length in user space
178 *
179 * The address is copied into kernel space. If the provided address is
180 * too long an error code of -EINVAL is returned. If the copy gives
181 * invalid addresses -EFAULT is returned. On a success 0 is returned.
182 */
183
Maciej Żenczykowski43db3622012-03-11 12:51:50 +0000184int move_addr_to_kernel(void __user *uaddr, int ulen, struct sockaddr_storage *kaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -0700186 if (ulen < 0 || ulen > sizeof(struct sockaddr_storage))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700188 if (ulen == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 return 0;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700190 if (copy_from_user(kaddr, uaddr, ulen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 return -EFAULT;
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100192 return audit_sockaddr(ulen, kaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
194
195/**
196 * move_addr_to_user - copy an address to user space
197 * @kaddr: kernel space address
198 * @klen: length of address in kernel
199 * @uaddr: user space address
200 * @ulen: pointer to user length field
201 *
202 * The value pointed to by ulen on entry is the buffer length available.
203 * This is overwritten with the buffer space used. -EINVAL is returned
204 * if an overlong buffer is specified or a negative buffer size. -EFAULT
205 * is returned if either the buffer or the length field are not
206 * accessible.
207 * After copying the data up to the limit the user specifies, the true
208 * length of the data is written over the length limit the user
209 * specified. Zero is returned for a success.
210 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700211
Maciej Żenczykowski43db3622012-03-11 12:51:50 +0000212static int move_addr_to_user(struct sockaddr_storage *kaddr, int klen,
stephen hemminger11165f12010-10-18 14:27:29 +0000213 void __user *uaddr, int __user *ulen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
215 int err;
216 int len;
217
Hannes Frederic Sowa5c586f12013-11-21 03:14:34 +0100218 BUG_ON(klen > sizeof(struct sockaddr_storage));
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700219 err = get_user(len, ulen);
220 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 return err;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700222 if (len > klen)
223 len = klen;
Hannes Frederic Sowa5c586f12013-11-21 03:14:34 +0100224 if (len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700226 if (len) {
Steve Grubbd6fe3942006-03-30 12:20:22 -0500227 if (audit_sockaddr(klen, kaddr))
228 return -ENOMEM;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700229 if (copy_to_user(uaddr, kaddr, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 return -EFAULT;
231 }
232 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700233 * "fromlen shall refer to the value before truncation.."
234 * 1003.1g
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 */
236 return __put_user(klen, ulen);
237}
238
Christoph Lametere18b8902006-12-06 20:33:20 -0800239static struct kmem_cache *sock_inode_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241static struct inode *sock_alloc_inode(struct super_block *sb)
242{
243 struct socket_alloc *ei;
Eric Dumazeteaefd112011-02-18 03:26:36 +0000244 struct socket_wq *wq;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700245
Christoph Lametere94b1762006-12-06 20:33:17 -0800246 ei = kmem_cache_alloc(sock_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 if (!ei)
248 return NULL;
Eric Dumazeteaefd112011-02-18 03:26:36 +0000249 wq = kmalloc(sizeof(*wq), GFP_KERNEL);
250 if (!wq) {
Eric Dumazet43815482010-04-29 11:01:49 +0000251 kmem_cache_free(sock_inode_cachep, ei);
252 return NULL;
253 }
Eric Dumazeteaefd112011-02-18 03:26:36 +0000254 init_waitqueue_head(&wq->wait);
255 wq->fasync_list = NULL;
256 RCU_INIT_POINTER(ei->socket.wq, wq);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 ei->socket.state = SS_UNCONNECTED;
259 ei->socket.flags = 0;
260 ei->socket.ops = NULL;
261 ei->socket.sk = NULL;
262 ei->socket.file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 return &ei->vfs_inode;
265}
266
267static void sock_destroy_inode(struct inode *inode)
268{
Eric Dumazet43815482010-04-29 11:01:49 +0000269 struct socket_alloc *ei;
Eric Dumazeteaefd112011-02-18 03:26:36 +0000270 struct socket_wq *wq;
Eric Dumazet43815482010-04-29 11:01:49 +0000271
272 ei = container_of(inode, struct socket_alloc, vfs_inode);
Eric Dumazeteaefd112011-02-18 03:26:36 +0000273 wq = rcu_dereference_protected(ei->socket.wq, 1);
Lai Jiangshan61845222011-03-18 12:10:25 +0800274 kfree_rcu(wq, rcu);
Eric Dumazet43815482010-04-29 11:01:49 +0000275 kmem_cache_free(sock_inode_cachep, ei);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700278static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700280 struct socket_alloc *ei = (struct socket_alloc *)foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Christoph Lametera35afb82007-05-16 22:10:57 -0700282 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285static int init_inodecache(void)
286{
287 sock_inode_cachep = kmem_cache_create("sock_inode_cache",
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700288 sizeof(struct socket_alloc),
289 0,
290 (SLAB_HWCACHE_ALIGN |
291 SLAB_RECLAIM_ACCOUNT |
292 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +0900293 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 if (sock_inode_cachep == NULL)
295 return -ENOMEM;
296 return 0;
297}
298
Alexey Dobriyanb87221d2009-09-21 17:01:09 -0700299static const struct super_operations sockfs_ops = {
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700300 .alloc_inode = sock_alloc_inode,
301 .destroy_inode = sock_destroy_inode,
302 .statfs = simple_statfs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303};
304
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700305/*
306 * sockfs_dname() is called from d_path().
307 */
308static char *sockfs_dname(struct dentry *dentry, char *buffer, int buflen)
309{
310 return dynamic_dname(dentry, buffer, buflen, "socket:[%lu]",
311 dentry->d_inode->i_ino);
312}
313
Al Viro3ba13d12009-02-20 06:02:22 +0000314static const struct dentry_operations sockfs_dentry_operations = {
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700315 .d_dname = sockfs_dname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316};
317
Al Viroc74a1cb2011-01-12 16:59:34 -0500318static struct dentry *sockfs_mount(struct file_system_type *fs_type,
319 int flags, const char *dev_name, void *data)
320{
321 return mount_pseudo(fs_type, "socket:", &sockfs_ops,
322 &sockfs_dentry_operations, SOCKFS_MAGIC);
323}
324
325static struct vfsmount *sock_mnt __read_mostly;
326
327static struct file_system_type sock_fs_type = {
328 .name = "sockfs",
329 .mount = sockfs_mount,
330 .kill_sb = kill_anon_super,
331};
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333/*
334 * Obtains the first available file descriptor and sets it up for use.
335 *
David S. Miller39d8c1b2006-03-20 17:13:49 -0800336 * These functions create file structures and maps them to fd space
337 * of the current process. On success it returns file descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 * and file struct implicitly stored in sock->file.
339 * Note that another thread may close file descriptor before we return
340 * from this function. We use the fact that now we do not refer
341 * to socket after mapping. If one day we will need it, this
342 * function will increment ref. count on file by 1.
343 *
344 * In any case returned fd MAY BE not valid!
345 * This race condition is unavoidable
346 * with shared fd spaces, we cannot solve it inside kernel,
347 * but we take care of internal coherence yet.
348 */
349
Linus Torvaldsaab174f2012-10-02 20:25:04 -0700350struct file *sock_alloc_file(struct socket *sock, int flags, const char *dname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
Al Viro7cbe66b2009-08-05 19:59:08 +0400352 struct qstr name = { .name = "" };
Al Viro2c48b9c2009-08-09 00:52:35 +0400353 struct path path;
Al Viro7cbe66b2009-08-05 19:59:08 +0400354 struct file *file;
David S. Miller39d8c1b2006-03-20 17:13:49 -0800355
Masatake YAMATO600e1772012-08-29 10:44:29 +0000356 if (dname) {
357 name.name = dname;
358 name.len = strlen(name.name);
359 } else if (sock->sk) {
360 name.name = sock->sk->sk_prot_creator->name;
361 name.len = strlen(name.name);
362 }
Nick Piggin4b936882011-01-07 17:50:07 +1100363 path.dentry = d_alloc_pseudo(sock_mnt->mnt_sb, &name);
Al Viro28407632012-08-17 23:54:15 -0400364 if (unlikely(!path.dentry))
365 return ERR_PTR(-ENOMEM);
Al Viro2c48b9c2009-08-09 00:52:35 +0400366 path.mnt = mntget(sock_mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Al Viro2c48b9c2009-08-09 00:52:35 +0400368 d_instantiate(path.dentry, SOCK_INODE(sock));
Al Virocc3808f2009-08-06 09:43:59 +0400369 SOCK_INODE(sock)->i_fop = &socket_file_ops;
370
Al Viro2c48b9c2009-08-09 00:52:35 +0400371 file = alloc_file(&path, FMODE_READ | FMODE_WRITE,
Al Virocc3808f2009-08-06 09:43:59 +0400372 &socket_file_ops);
Anatol Pomozov39b65252012-09-12 20:11:55 -0700373 if (unlikely(IS_ERR(file))) {
Al Virocc3808f2009-08-06 09:43:59 +0400374 /* drop dentry, keep inode */
Al Viro7de9c6ee2010-10-23 11:11:40 -0400375 ihold(path.dentry->d_inode);
Al Viro2c48b9c2009-08-09 00:52:35 +0400376 path_put(&path);
Anatol Pomozov39b65252012-09-12 20:11:55 -0700377 return file;
Al Virocc3808f2009-08-06 09:43:59 +0400378 }
David S. Miller39d8c1b2006-03-20 17:13:49 -0800379
380 sock->file = file;
Ulrich Drepper77d27202008-07-23 21:29:35 -0700381 file->f_flags = O_RDWR | (flags & O_NONBLOCK);
David S. Miller39d8c1b2006-03-20 17:13:49 -0800382 file->private_data = sock;
Al Viro28407632012-08-17 23:54:15 -0400383 return file;
David S. Miller39d8c1b2006-03-20 17:13:49 -0800384}
Al Viro56b31d12012-08-18 00:25:51 -0400385EXPORT_SYMBOL(sock_alloc_file);
David S. Miller39d8c1b2006-03-20 17:13:49 -0800386
Al Viro56b31d12012-08-18 00:25:51 -0400387static int sock_map_fd(struct socket *sock, int flags)
David S. Miller39d8c1b2006-03-20 17:13:49 -0800388{
389 struct file *newfile;
Al Viro28407632012-08-17 23:54:15 -0400390 int fd = get_unused_fd_flags(flags);
391 if (unlikely(fd < 0))
392 return fd;
David S. Miller39d8c1b2006-03-20 17:13:49 -0800393
Linus Torvaldsaab174f2012-10-02 20:25:04 -0700394 newfile = sock_alloc_file(sock, flags, NULL);
Al Viro28407632012-08-17 23:54:15 -0400395 if (likely(!IS_ERR(newfile))) {
David S. Miller39d8c1b2006-03-20 17:13:49 -0800396 fd_install(fd, newfile);
Al Viro28407632012-08-17 23:54:15 -0400397 return fd;
398 }
Al Viro7cbe66b2009-08-05 19:59:08 +0400399
Al Viro28407632012-08-17 23:54:15 -0400400 put_unused_fd(fd);
401 return PTR_ERR(newfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402}
403
John Fastabend406a3c62012-07-20 10:39:25 +0000404struct socket *sock_from_file(struct file *file, int *err)
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800405{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800406 if (file->f_op == &socket_file_ops)
407 return file->private_data; /* set in sock_map_fd */
408
Eric Dumazet23bb80d2007-02-08 14:59:57 -0800409 *err = -ENOTSOCK;
410 return NULL;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800411}
John Fastabend406a3c62012-07-20 10:39:25 +0000412EXPORT_SYMBOL(sock_from_file);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414/**
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700415 * sockfd_lookup - Go from a file number to its socket slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 * @fd: file handle
417 * @err: pointer to an error code return
418 *
419 * The file handle passed in is locked and the socket it is bound
420 * too is returned. If an error occurs the err pointer is overwritten
421 * with a negative errno code and NULL is returned. The function checks
422 * for both invalid handles and passing a handle which is not a socket.
423 *
424 * On a success the socket object pointer is returned.
425 */
426
427struct socket *sockfd_lookup(int fd, int *err)
428{
429 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 struct socket *sock;
431
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700432 file = fget(fd);
433 if (!file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 *err = -EBADF;
435 return NULL;
436 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700437
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800438 sock = sock_from_file(file, err);
439 if (!sock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 fput(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return sock;
442}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700443EXPORT_SYMBOL(sockfd_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800445static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed)
446{
447 struct file *file;
448 struct socket *sock;
449
Hua Zhong36725582006-04-19 15:25:02 -0700450 *err = -EBADF;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800451 file = fget_light(fd, fput_needed);
452 if (file) {
453 sock = sock_from_file(file, err);
454 if (sock)
455 return sock;
456 fput_light(file, *fput_needed);
457 }
458 return NULL;
459}
460
Masatake YAMATO600e1772012-08-29 10:44:29 +0000461#define XATTR_SOCKPROTONAME_SUFFIX "sockprotoname"
462#define XATTR_NAME_SOCKPROTONAME (XATTR_SYSTEM_PREFIX XATTR_SOCKPROTONAME_SUFFIX)
463#define XATTR_NAME_SOCKPROTONAME_LEN (sizeof(XATTR_NAME_SOCKPROTONAME)-1)
464static ssize_t sockfs_getxattr(struct dentry *dentry,
465 const char *name, void *value, size_t size)
466{
467 const char *proto_name;
468 size_t proto_size;
469 int error;
470
471 error = -ENODATA;
472 if (!strncmp(name, XATTR_NAME_SOCKPROTONAME, XATTR_NAME_SOCKPROTONAME_LEN)) {
473 proto_name = dentry->d_name.name;
474 proto_size = strlen(proto_name);
475
476 if (value) {
477 error = -ERANGE;
478 if (proto_size + 1 > size)
479 goto out;
480
481 strncpy(value, proto_name, proto_size + 1);
482 }
483 error = proto_size + 1;
484 }
485
486out:
487 return error;
488}
489
490static ssize_t sockfs_listxattr(struct dentry *dentry, char *buffer,
491 size_t size)
492{
493 ssize_t len;
494 ssize_t used = 0;
495
496 len = security_inode_listsecurity(dentry->d_inode, buffer, size);
497 if (len < 0)
498 return len;
499 used += len;
500 if (buffer) {
501 if (size < used)
502 return -ERANGE;
503 buffer += len;
504 }
505
506 len = (XATTR_NAME_SOCKPROTONAME_LEN + 1);
507 used += len;
508 if (buffer) {
509 if (size < used)
510 return -ERANGE;
511 memcpy(buffer, XATTR_NAME_SOCKPROTONAME, len);
512 buffer += len;
513 }
514
515 return used;
516}
517
518static const struct inode_operations sockfs_inode_ops = {
519 .getxattr = sockfs_getxattr,
520 .listxattr = sockfs_listxattr,
521};
522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523/**
524 * sock_alloc - allocate a socket
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700525 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 * Allocate a new inode and socket object. The two are bound together
527 * and initialised. The socket is then returned. If we are out of inodes
528 * NULL is returned.
529 */
530
531static struct socket *sock_alloc(void)
532{
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700533 struct inode *inode;
534 struct socket *sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Eric Dumazeta209dfc2011-07-26 11:36:34 +0200536 inode = new_inode_pseudo(sock_mnt->mnt_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 if (!inode)
538 return NULL;
539
540 sock = SOCKET_I(inode);
541
Eric Dumazet29a020d2009-09-15 02:39:20 -0700542 kmemcheck_annotate_bitfield(sock, type);
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400543 inode->i_ino = get_next_ino();
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700544 inode->i_mode = S_IFSOCK | S_IRWXUGO;
David Howells8192b0c2008-11-14 10:39:10 +1100545 inode->i_uid = current_fsuid();
546 inode->i_gid = current_fsgid();
Masatake YAMATO600e1772012-08-29 10:44:29 +0000547 inode->i_op = &sockfs_inode_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Alex Shi19e8d692012-05-14 14:15:31 -0700549 this_cpu_add(sockets_in_use, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 return sock;
551}
552
553/*
554 * In theory you can't get an open on this inode, but /proc provides
555 * a back door. Remember to keep it shut otherwise you'll let the
556 * creepy crawlies in.
557 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559static int sock_no_open(struct inode *irrelevant, struct file *dontcare)
560{
561 return -ENXIO;
562}
563
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800564const struct file_operations bad_sock_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 .owner = THIS_MODULE,
566 .open = sock_no_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200567 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568};
569
570/**
571 * sock_release - close a socket
572 * @sock: socket to close
573 *
574 * The socket is released from the protocol stack if it has a release
575 * callback, and the inode is then released if the socket is bound to
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700576 * an inode not a file.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579void sock_release(struct socket *sock)
580{
581 if (sock->ops) {
582 struct module *owner = sock->ops->owner;
583
584 sock->ops->release(sock);
585 sock->ops = NULL;
586 module_put(owner);
587 }
588
Eric Dumazeteaefd112011-02-18 03:26:36 +0000589 if (rcu_dereference_protected(sock->wq, 1)->fasync_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 printk(KERN_ERR "sock_release: fasync list not empty!\n");
591
Mikulas Patockab09e7862012-07-19 06:13:36 +0000592 if (test_bit(SOCK_EXTERNALLY_ALLOCATED, &sock->flags))
593 return;
594
Alex Shi19e8d692012-05-14 14:15:31 -0700595 this_cpu_sub(sockets_in_use, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 if (!sock->file) {
597 iput(SOCK_INODE(sock));
598 return;
599 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700600 sock->file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700602EXPORT_SYMBOL(sock_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Daniel Borkmannbf84a012013-04-14 08:08:13 +0000604void sock_tx_timestamp(struct sock *sk, __u8 *tx_flags)
Patrick Ohly20d49472009-02-12 05:03:38 +0000605{
Oliver Hartkopp2244d072010-08-17 08:59:14 +0000606 *tx_flags = 0;
Patrick Ohly20d49472009-02-12 05:03:38 +0000607 if (sock_flag(sk, SOCK_TIMESTAMPING_TX_HARDWARE))
Oliver Hartkopp2244d072010-08-17 08:59:14 +0000608 *tx_flags |= SKBTX_HW_TSTAMP;
Patrick Ohly20d49472009-02-12 05:03:38 +0000609 if (sock_flag(sk, SOCK_TIMESTAMPING_TX_SOFTWARE))
Oliver Hartkopp2244d072010-08-17 08:59:14 +0000610 *tx_flags |= SKBTX_SW_TSTAMP;
Johannes Berg6e3e9392011-11-09 10:15:42 +0100611 if (sock_flag(sk, SOCK_WIFI_STATUS))
612 *tx_flags |= SKBTX_WIFI_STATUS;
Patrick Ohly20d49472009-02-12 05:03:38 +0000613}
614EXPORT_SYMBOL(sock_tx_timestamp);
615
Anton Blanchard228e5482011-05-02 20:21:35 +0000616static inline int __sock_sendmsg_nosec(struct kiocb *iocb, struct socket *sock,
617 struct msghdr *msg, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
619 struct sock_iocb *si = kiocb_to_siocb(iocb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 si->sock = sock;
622 si->scm = NULL;
623 si->msg = msg;
624 si->size = size;
625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 return sock->ops->sendmsg(iocb, sock, msg, size);
627}
628
Anton Blanchard228e5482011-05-02 20:21:35 +0000629static inline int __sock_sendmsg(struct kiocb *iocb, struct socket *sock,
630 struct msghdr *msg, size_t size)
631{
632 int err = security_socket_sendmsg(sock, msg, size);
633
634 return err ?: __sock_sendmsg_nosec(iocb, sock, msg, size);
635}
636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637int sock_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
638{
639 struct kiocb iocb;
640 struct sock_iocb siocb;
641 int ret;
642
643 init_sync_kiocb(&iocb, NULL);
644 iocb.private = &siocb;
645 ret = __sock_sendmsg(&iocb, sock, msg, size);
646 if (-EIOCBQUEUED == ret)
647 ret = wait_on_sync_kiocb(&iocb);
648 return ret;
649}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700650EXPORT_SYMBOL(sock_sendmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Eric Dumazet894dc242011-07-26 02:39:41 +0000652static int sock_sendmsg_nosec(struct socket *sock, struct msghdr *msg, size_t size)
Anton Blanchard228e5482011-05-02 20:21:35 +0000653{
654 struct kiocb iocb;
655 struct sock_iocb siocb;
656 int ret;
657
658 init_sync_kiocb(&iocb, NULL);
659 iocb.private = &siocb;
660 ret = __sock_sendmsg_nosec(&iocb, sock, msg, size);
661 if (-EIOCBQUEUED == ret)
662 ret = wait_on_sync_kiocb(&iocb);
663 return ret;
664}
665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
667 struct kvec *vec, size_t num, size_t size)
668{
669 mm_segment_t oldfs = get_fs();
670 int result;
671
672 set_fs(KERNEL_DS);
673 /*
674 * the following is safe, since for compiler definitions of kvec and
675 * iovec are identical, yielding the same in-core layout and alignment
676 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700677 msg->msg_iov = (struct iovec *)vec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 msg->msg_iovlen = num;
679 result = sock_sendmsg(sock, msg, size);
680 set_fs(oldfs);
681 return result;
682}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700683EXPORT_SYMBOL(kernel_sendmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700685/*
686 * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
687 */
688void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
689 struct sk_buff *skb)
690{
Patrick Ohly20d49472009-02-12 05:03:38 +0000691 int need_software_tstamp = sock_flag(sk, SOCK_RCVTSTAMP);
692 struct timespec ts[3];
693 int empty = 1;
694 struct skb_shared_hwtstamps *shhwtstamps =
695 skb_hwtstamps(skb);
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700696
Patrick Ohly20d49472009-02-12 05:03:38 +0000697 /* Race occurred between timestamp enabling and packet
698 receiving. Fill in the current time for now. */
699 if (need_software_tstamp && skb->tstamp.tv64 == 0)
700 __net_timestamp(skb);
701
702 if (need_software_tstamp) {
703 if (!sock_flag(sk, SOCK_RCVTSTAMPNS)) {
704 struct timeval tv;
705 skb_get_timestamp(skb, &tv);
706 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMP,
707 sizeof(tv), &tv);
708 } else {
Hagen Paul Pfeifer842509b2010-04-06 05:39:52 +0000709 skb_get_timestampns(skb, &ts[0]);
Patrick Ohly20d49472009-02-12 05:03:38 +0000710 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPNS,
Hagen Paul Pfeifer842509b2010-04-06 05:39:52 +0000711 sizeof(ts[0]), &ts[0]);
Patrick Ohly20d49472009-02-12 05:03:38 +0000712 }
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700713 }
Patrick Ohly20d49472009-02-12 05:03:38 +0000714
715
716 memset(ts, 0, sizeof(ts));
Daniel Borkmann6e94d1e2013-04-16 01:29:10 +0000717 if (sock_flag(sk, SOCK_TIMESTAMPING_SOFTWARE) &&
718 ktime_to_timespec_cond(skb->tstamp, ts + 0))
Patrick Ohly20d49472009-02-12 05:03:38 +0000719 empty = 0;
Patrick Ohly20d49472009-02-12 05:03:38 +0000720 if (shhwtstamps) {
721 if (sock_flag(sk, SOCK_TIMESTAMPING_SYS_HARDWARE) &&
Daniel Borkmann6e94d1e2013-04-16 01:29:10 +0000722 ktime_to_timespec_cond(shhwtstamps->syststamp, ts + 1))
Patrick Ohly20d49472009-02-12 05:03:38 +0000723 empty = 0;
724 if (sock_flag(sk, SOCK_TIMESTAMPING_RAW_HARDWARE) &&
Daniel Borkmann6e94d1e2013-04-16 01:29:10 +0000725 ktime_to_timespec_cond(shhwtstamps->hwtstamp, ts + 2))
Patrick Ohly20d49472009-02-12 05:03:38 +0000726 empty = 0;
727 }
728 if (!empty)
729 put_cmsg(msg, SOL_SOCKET,
730 SCM_TIMESTAMPING, sizeof(ts), &ts);
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700731}
Arnaldo Carvalho de Melo7c81fd82007-03-10 00:39:35 -0300732EXPORT_SYMBOL_GPL(__sock_recv_timestamp);
733
Johannes Berg6e3e9392011-11-09 10:15:42 +0100734void __sock_recv_wifi_status(struct msghdr *msg, struct sock *sk,
735 struct sk_buff *skb)
736{
737 int ack;
738
739 if (!sock_flag(sk, SOCK_WIFI_STATUS))
740 return;
741 if (!skb->wifi_acked_valid)
742 return;
743
744 ack = skb->wifi_acked;
745
746 put_cmsg(msg, SOL_SOCKET, SCM_WIFI_STATUS, sizeof(ack), &ack);
747}
748EXPORT_SYMBOL_GPL(__sock_recv_wifi_status);
749
stephen hemminger11165f12010-10-18 14:27:29 +0000750static inline void sock_recv_drops(struct msghdr *msg, struct sock *sk,
751 struct sk_buff *skb)
Neil Horman3b885782009-10-12 13:26:31 -0700752{
753 if (sock_flag(sk, SOCK_RXQ_OVFL) && skb && skb->dropcount)
754 put_cmsg(msg, SOL_SOCKET, SO_RXQ_OVFL,
755 sizeof(__u32), &skb->dropcount);
756}
757
Eric Dumazet767dd032010-04-28 19:14:43 +0000758void __sock_recv_ts_and_drops(struct msghdr *msg, struct sock *sk,
Neil Horman3b885782009-10-12 13:26:31 -0700759 struct sk_buff *skb)
760{
761 sock_recv_timestamp(msg, sk, skb);
762 sock_recv_drops(msg, sk, skb);
763}
Eric Dumazet767dd032010-04-28 19:14:43 +0000764EXPORT_SYMBOL_GPL(__sock_recv_ts_and_drops);
Neil Horman3b885782009-10-12 13:26:31 -0700765
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -0700766static inline int __sock_recvmsg_nosec(struct kiocb *iocb, struct socket *sock,
767 struct msghdr *msg, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 struct sock_iocb *si = kiocb_to_siocb(iocb);
770
771 si->sock = sock;
772 si->scm = NULL;
773 si->msg = msg;
774 si->size = size;
775 si->flags = flags;
776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 return sock->ops->recvmsg(iocb, sock, msg, size, flags);
778}
779
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -0700780static inline int __sock_recvmsg(struct kiocb *iocb, struct socket *sock,
781 struct msghdr *msg, size_t size, int flags)
782{
783 int err = security_socket_recvmsg(sock, msg, size, flags);
784
785 return err ?: __sock_recvmsg_nosec(iocb, sock, msg, size, flags);
786}
787
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700788int sock_recvmsg(struct socket *sock, struct msghdr *msg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 size_t size, int flags)
790{
791 struct kiocb iocb;
792 struct sock_iocb siocb;
793 int ret;
794
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700795 init_sync_kiocb(&iocb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 iocb.private = &siocb;
797 ret = __sock_recvmsg(&iocb, sock, msg, size, flags);
798 if (-EIOCBQUEUED == ret)
799 ret = wait_on_sync_kiocb(&iocb);
800 return ret;
801}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700802EXPORT_SYMBOL(sock_recvmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -0700804static int sock_recvmsg_nosec(struct socket *sock, struct msghdr *msg,
805 size_t size, int flags)
806{
807 struct kiocb iocb;
808 struct sock_iocb siocb;
809 int ret;
810
811 init_sync_kiocb(&iocb, NULL);
812 iocb.private = &siocb;
813 ret = __sock_recvmsg_nosec(&iocb, sock, msg, size, flags);
814 if (-EIOCBQUEUED == ret)
815 ret = wait_on_sync_kiocb(&iocb);
816 return ret;
817}
818
Martin Lucinac1249c02010-12-10 00:04:05 +0000819/**
820 * kernel_recvmsg - Receive a message from a socket (kernel space)
821 * @sock: The socket to receive the message from
822 * @msg: Received message
823 * @vec: Input s/g array for message data
824 * @num: Size of input s/g array
825 * @size: Number of bytes to read
826 * @flags: Message flags (MSG_DONTWAIT, etc...)
827 *
828 * On return the msg structure contains the scatter/gather array passed in the
829 * vec argument. The array is modified so that it consists of the unfilled
830 * portion of the original array.
831 *
832 * The returned value is the total number of bytes received, or an error.
833 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700834int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
835 struct kvec *vec, size_t num, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
837 mm_segment_t oldfs = get_fs();
838 int result;
839
840 set_fs(KERNEL_DS);
841 /*
842 * the following is safe, since for compiler definitions of kvec and
843 * iovec are identical, yielding the same in-core layout and alignment
844 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700845 msg->msg_iov = (struct iovec *)vec, msg->msg_iovlen = num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 result = sock_recvmsg(sock, msg, size, flags);
847 set_fs(oldfs);
848 return result;
849}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700850EXPORT_SYMBOL(kernel_recvmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852static void sock_aio_dtor(struct kiocb *iocb)
853{
854 kfree(iocb->private);
855}
856
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -0300857static ssize_t sock_sendpage(struct file *file, struct page *page,
858 int offset, size_t size, loff_t *ppos, int more)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859{
860 struct socket *sock;
861 int flags;
862
Eric Dumazetb69aee02005-09-06 14:42:45 -0700863 sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Eric Dumazet35f9c092012-04-05 03:05:35 +0000865 flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
866 /* more is a combination of MSG_MORE and MSG_SENDPAGE_NOTLAST */
867 flags |= more;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Linus Torvaldse6949582009-08-13 08:28:36 -0700869 return kernel_sendpage(sock, page, offset, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870}
871
Jens Axboe9c55e012007-11-06 23:30:13 -0800872static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700873 struct pipe_inode_info *pipe, size_t len,
Jens Axboe9c55e012007-11-06 23:30:13 -0800874 unsigned int flags)
875{
876 struct socket *sock = file->private_data;
877
Rémi Denis-Courmont997b37d2008-02-15 02:35:45 -0800878 if (unlikely(!sock->ops->splice_read))
879 return -EINVAL;
880
Jens Axboe9c55e012007-11-06 23:30:13 -0800881 return sock->ops->splice_read(sock, ppos, pipe, len, flags);
882}
883
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800884static struct sock_iocb *alloc_sock_iocb(struct kiocb *iocb,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700885 struct sock_iocb *siocb)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800886{
887 if (!is_sync_kiocb(iocb)) {
888 siocb = kmalloc(sizeof(*siocb), GFP_KERNEL);
889 if (!siocb)
890 return NULL;
891 iocb->ki_dtor = sock_aio_dtor;
892 }
893
894 siocb->kiocb = iocb;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800895 iocb->private = siocb;
896 return siocb;
897}
898
899static ssize_t do_sock_read(struct msghdr *msg, struct kiocb *iocb,
Badari Pulavarty027445c2006-09-30 23:28:46 -0700900 struct file *file, const struct iovec *iov,
901 unsigned long nr_segs)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800902{
903 struct socket *sock = file->private_data;
904 size_t size = 0;
905 int i;
906
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700907 for (i = 0; i < nr_segs; i++)
908 size += iov[i].iov_len;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800909
910 msg->msg_name = NULL;
911 msg->msg_namelen = 0;
912 msg->msg_control = NULL;
913 msg->msg_controllen = 0;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700914 msg->msg_iov = (struct iovec *)iov;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800915 msg->msg_iovlen = nr_segs;
916 msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
917
918 return __sock_recvmsg(iocb, sock, msg, size, msg->msg_flags);
919}
920
Badari Pulavarty027445c2006-09-30 23:28:46 -0700921static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov,
922 unsigned long nr_segs, loff_t pos)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800923{
924 struct sock_iocb siocb, *x;
925
926 if (pos != 0)
927 return -ESPIPE;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700928
929 if (iocb->ki_left == 0) /* Match SYS5 behaviour */
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800930 return 0;
931
Badari Pulavarty027445c2006-09-30 23:28:46 -0700932
933 x = alloc_sock_iocb(iocb, &siocb);
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800934 if (!x)
935 return -ENOMEM;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700936 return do_sock_read(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800937}
938
939static ssize_t do_sock_write(struct msghdr *msg, struct kiocb *iocb,
Badari Pulavarty027445c2006-09-30 23:28:46 -0700940 struct file *file, const struct iovec *iov,
941 unsigned long nr_segs)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800942{
943 struct socket *sock = file->private_data;
944 size_t size = 0;
945 int i;
946
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700947 for (i = 0; i < nr_segs; i++)
948 size += iov[i].iov_len;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800949
950 msg->msg_name = NULL;
951 msg->msg_namelen = 0;
952 msg->msg_control = NULL;
953 msg->msg_controllen = 0;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700954 msg->msg_iov = (struct iovec *)iov;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800955 msg->msg_iovlen = nr_segs;
956 msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
957 if (sock->type == SOCK_SEQPACKET)
958 msg->msg_flags |= MSG_EOR;
959
960 return __sock_sendmsg(iocb, sock, msg, size);
961}
962
Badari Pulavarty027445c2006-09-30 23:28:46 -0700963static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov,
964 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965{
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800966 struct sock_iocb siocb, *x;
967
968 if (pos != 0)
969 return -ESPIPE;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700970
Badari Pulavarty027445c2006-09-30 23:28:46 -0700971 x = alloc_sock_iocb(iocb, &siocb);
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800972 if (!x)
973 return -ENOMEM;
974
Badari Pulavarty027445c2006-09-30 23:28:46 -0700975 return do_sock_write(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976}
977
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978/*
979 * Atomic setting of ioctl hooks to avoid race
980 * with module unload.
981 */
982
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800983static DEFINE_MUTEX(br_ioctl_mutex);
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700984static int (*br_ioctl_hook) (struct net *, unsigned int cmd, void __user *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Eric W. Biederman881d9662007-09-17 11:56:21 -0700986void brioctl_set(int (*hook) (struct net *, unsigned int, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800988 mutex_lock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 br_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800990 mutex_unlock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991}
992EXPORT_SYMBOL(brioctl_set);
993
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800994static DEFINE_MUTEX(vlan_ioctl_mutex);
Eric W. Biederman881d9662007-09-17 11:56:21 -0700995static int (*vlan_ioctl_hook) (struct net *, void __user *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Eric W. Biederman881d9662007-09-17 11:56:21 -0700997void vlan_ioctl_set(int (*hook) (struct net *, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800999 mutex_lock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 vlan_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001001 mutex_unlock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002}
1003EXPORT_SYMBOL(vlan_ioctl_set);
1004
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001005static DEFINE_MUTEX(dlci_ioctl_mutex);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001006static int (*dlci_ioctl_hook) (unsigned int, void __user *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001008void dlci_ioctl_set(int (*hook) (unsigned int, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001010 mutex_lock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 dlci_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001012 mutex_unlock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013}
1014EXPORT_SYMBOL(dlci_ioctl_set);
1015
Arnd Bergmann6b960182009-11-06 23:10:54 -08001016static long sock_do_ioctl(struct net *net, struct socket *sock,
1017 unsigned int cmd, unsigned long arg)
1018{
1019 int err;
1020 void __user *argp = (void __user *)arg;
1021
1022 err = sock->ops->ioctl(sock, cmd, arg);
1023
1024 /*
1025 * If this ioctl is unknown try to hand it down
1026 * to the NIC driver.
1027 */
1028 if (err == -ENOIOCTLCMD)
1029 err = dev_ioctl(net, cmd, argp);
1030
1031 return err;
1032}
1033
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034/*
1035 * With an ioctl, arg may well be a user mode pointer, but we don't know
1036 * what to do with it - that's up to the protocol still.
1037 */
1038
1039static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
1040{
1041 struct socket *sock;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001042 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 void __user *argp = (void __user *)arg;
1044 int pid, err;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001045 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Eric Dumazetb69aee02005-09-06 14:42:45 -07001047 sock = file->private_data;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001048 sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001049 net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
Eric W. Biederman881d9662007-09-17 11:56:21 -07001051 err = dev_ioctl(net, cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 } else
Johannes Berg3d23e342009-09-29 23:27:28 +02001053#ifdef CONFIG_WEXT_CORE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
Eric W. Biederman881d9662007-09-17 11:56:21 -07001055 err = dev_ioctl(net, cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 } else
Johannes Berg3d23e342009-09-29 23:27:28 +02001057#endif
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001058 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 case FIOSETOWN:
1060 case SIOCSPGRP:
1061 err = -EFAULT;
1062 if (get_user(pid, (int __user *)argp))
1063 break;
1064 err = f_setown(sock->file, pid, 1);
1065 break;
1066 case FIOGETOWN:
1067 case SIOCGPGRP:
Eric W. Biederman609d7fa2006-10-02 02:17:15 -07001068 err = put_user(f_getown(sock->file),
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001069 (int __user *)argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 break;
1071 case SIOCGIFBR:
1072 case SIOCSIFBR:
1073 case SIOCBRADDBR:
1074 case SIOCBRDELBR:
1075 err = -ENOPKG;
1076 if (!br_ioctl_hook)
1077 request_module("bridge");
1078
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001079 mutex_lock(&br_ioctl_mutex);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001080 if (br_ioctl_hook)
Eric W. Biederman881d9662007-09-17 11:56:21 -07001081 err = br_ioctl_hook(net, cmd, argp);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001082 mutex_unlock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 break;
1084 case SIOCGIFVLAN:
1085 case SIOCSIFVLAN:
1086 err = -ENOPKG;
1087 if (!vlan_ioctl_hook)
1088 request_module("8021q");
1089
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001090 mutex_lock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 if (vlan_ioctl_hook)
Eric W. Biederman881d9662007-09-17 11:56:21 -07001092 err = vlan_ioctl_hook(net, argp);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001093 mutex_unlock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 case SIOCADDDLCI:
1096 case SIOCDELDLCI:
1097 err = -ENOPKG;
1098 if (!dlci_ioctl_hook)
1099 request_module("dlci");
1100
Pavel Emelyanov7512cbf2008-03-21 15:58:52 -07001101 mutex_lock(&dlci_ioctl_mutex);
1102 if (dlci_ioctl_hook)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 err = dlci_ioctl_hook(cmd, argp);
Pavel Emelyanov7512cbf2008-03-21 15:58:52 -07001104 mutex_unlock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 break;
1106 default:
Arnd Bergmann6b960182009-11-06 23:10:54 -08001107 err = sock_do_ioctl(net, sock, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 break;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 return err;
1111}
1112
1113int sock_create_lite(int family, int type, int protocol, struct socket **res)
1114{
1115 int err;
1116 struct socket *sock = NULL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 err = security_socket_create(family, type, protocol, 1);
1119 if (err)
1120 goto out;
1121
1122 sock = sock_alloc();
1123 if (!sock) {
1124 err = -ENOMEM;
1125 goto out;
1126 }
1127
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 sock->type = type;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001129 err = security_socket_post_create(sock, family, type, protocol, 1);
1130 if (err)
1131 goto out_release;
1132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133out:
1134 *res = sock;
1135 return err;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001136out_release:
1137 sock_release(sock);
1138 sock = NULL;
1139 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001141EXPORT_SYMBOL(sock_create_lite);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
1143/* No kernel lock held - perfect */
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001144static unsigned int sock_poll(struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
1146 struct socket *sock;
1147
1148 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001149 * We can't return errors to poll, so it's either yes or no.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 */
Eric Dumazetb69aee02005-09-06 14:42:45 -07001151 sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 return sock->ops->poll(file, sock, wait);
1153}
1154
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001155static int sock_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156{
Eric Dumazetb69aee02005-09-06 14:42:45 -07001157 struct socket *sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
1159 return sock->ops->mmap(file, sock, vma);
1160}
1161
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001162static int sock_close(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 sock_release(SOCKET_I(inode));
1165 return 0;
1166}
1167
1168/*
1169 * Update the socket async list
1170 *
1171 * Fasync_list locking strategy.
1172 *
1173 * 1. fasync_list is modified only under process context socket lock
1174 * i.e. under semaphore.
1175 * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
Eric Dumazet989a2972010-04-14 09:55:35 +00001176 * or under socket lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 */
1178
1179static int sock_fasync(int fd, struct file *filp, int on)
1180{
Eric Dumazet989a2972010-04-14 09:55:35 +00001181 struct socket *sock = filp->private_data;
1182 struct sock *sk = sock->sk;
Eric Dumazeteaefd112011-02-18 03:26:36 +00001183 struct socket_wq *wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
Eric Dumazet989a2972010-04-14 09:55:35 +00001185 if (sk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
1188 lock_sock(sk);
Eric Dumazeteaefd112011-02-18 03:26:36 +00001189 wq = rcu_dereference_protected(sock->wq, sock_owned_by_user(sk));
1190 fasync_helper(fd, filp, on, &wq->fasync_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Eric Dumazeteaefd112011-02-18 03:26:36 +00001192 if (!wq->fasync_list)
Eric Dumazet989a2972010-04-14 09:55:35 +00001193 sock_reset_flag(sk, SOCK_FASYNC);
Jonathan Corbet76398422009-02-01 14:26:59 -07001194 else
Eric Dumazetbcdce712009-10-06 17:28:29 -07001195 sock_set_flag(sk, SOCK_FASYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Eric Dumazet989a2972010-04-14 09:55:35 +00001197 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 return 0;
1199}
1200
Eric Dumazet43815482010-04-29 11:01:49 +00001201/* This function may be called only under socket lock or callback_lock or rcu_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
1203int sock_wake_async(struct socket *sock, int how, int band)
1204{
Eric Dumazet43815482010-04-29 11:01:49 +00001205 struct socket_wq *wq;
1206
1207 if (!sock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 return -1;
Eric Dumazet43815482010-04-29 11:01:49 +00001209 rcu_read_lock();
1210 wq = rcu_dereference(sock->wq);
1211 if (!wq || !wq->fasync_list) {
1212 rcu_read_unlock();
1213 return -1;
1214 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001215 switch (how) {
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001216 case SOCK_WAKE_WAITD:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 if (test_bit(SOCK_ASYNC_WAITDATA, &sock->flags))
1218 break;
1219 goto call_kill;
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001220 case SOCK_WAKE_SPACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags))
1222 break;
1223 /* fall through */
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001224 case SOCK_WAKE_IO:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001225call_kill:
Eric Dumazet43815482010-04-29 11:01:49 +00001226 kill_fasync(&wq->fasync_list, SIGIO, band);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 break;
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001228 case SOCK_WAKE_URG:
Eric Dumazet43815482010-04-29 11:01:49 +00001229 kill_fasync(&wq->fasync_list, SIGURG, band);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 }
Eric Dumazet43815482010-04-29 11:01:49 +00001231 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 return 0;
1233}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001234EXPORT_SYMBOL(sock_wake_async);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
Pavel Emelyanov721db932010-09-29 16:06:32 +04001236int __sock_create(struct net *net, int family, int type, int protocol,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001237 struct socket **res, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238{
1239 int err;
1240 struct socket *sock;
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001241 const struct net_proto_family *pf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
1243 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001244 * Check protocol is in range
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 */
1246 if (family < 0 || family >= NPROTO)
1247 return -EAFNOSUPPORT;
1248 if (type < 0 || type >= SOCK_MAX)
1249 return -EINVAL;
1250
1251 /* Compatibility.
1252
1253 This uglymoron is moved from INET layer to here to avoid
1254 deadlock in module load.
1255 */
1256 if (family == PF_INET && type == SOCK_PACKET) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001257 static int warned;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 if (!warned) {
1259 warned = 1;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001260 printk(KERN_INFO "%s uses obsolete (PF_INET,SOCK_PACKET)\n",
1261 current->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 }
1263 family = PF_PACKET;
1264 }
1265
1266 err = security_socket_create(family, type, protocol, kern);
1267 if (err)
1268 return err;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001269
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001270 /*
1271 * Allocate the socket and allow the family to set things up. if
1272 * the protocol is 0, the family is instructed to select an appropriate
1273 * default.
1274 */
1275 sock = sock_alloc();
1276 if (!sock) {
Joe Perchese87cc472012-05-13 21:56:26 +00001277 net_warn_ratelimited("socket: no more sockets\n");
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001278 return -ENFILE; /* Not exactly a match, but its the
1279 closest posix thing */
1280 }
1281
1282 sock->type = type;
1283
Johannes Berg95a5afc2008-10-16 15:24:51 -07001284#ifdef CONFIG_MODULES
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001285 /* Attempt to load a protocol module if the find failed.
1286 *
1287 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 * requested real, full-featured networking support upon configuration.
1289 * Otherwise module support will break!
1290 */
Eric Dumazet190683a2010-11-10 10:50:44 +00001291 if (rcu_access_pointer(net_families[family]) == NULL)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001292 request_module("net-pf-%d", family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293#endif
1294
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001295 rcu_read_lock();
1296 pf = rcu_dereference(net_families[family]);
1297 err = -EAFNOSUPPORT;
1298 if (!pf)
1299 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
1301 /*
1302 * We will call the ->create function, that possibly is in a loadable
1303 * module, so we have to bump that loadable module refcnt first.
1304 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001305 if (!try_module_get(pf->owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 goto out_release;
1307
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001308 /* Now protected by module ref count */
1309 rcu_read_unlock();
1310
Eric Paris3f378b62009-11-05 22:18:14 -08001311 err = pf->create(net, sock, protocol, kern);
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001312 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 goto out_module_put;
Frank Filza79af592005-09-27 15:23:38 -07001314
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 /*
1316 * Now to bump the refcnt of the [loadable] module that owns this
1317 * socket at sock_release time we decrement its refcnt.
1318 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001319 if (!try_module_get(sock->ops->owner))
1320 goto out_module_busy;
1321
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 /*
1323 * Now that we're done with the ->create function, the [loadable]
1324 * module can have its refcnt decremented
1325 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001326 module_put(pf->owner);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001327 err = security_socket_post_create(sock, family, type, protocol, kern);
1328 if (err)
Herbert Xu3b185522007-08-15 14:46:02 -07001329 goto out_sock_release;
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001330 *res = sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001332 return 0;
1333
1334out_module_busy:
1335 err = -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336out_module_put:
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001337 sock->ops = NULL;
1338 module_put(pf->owner);
1339out_sock_release:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 sock_release(sock);
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001341 return err;
1342
1343out_release:
1344 rcu_read_unlock();
1345 goto out_sock_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346}
Pavel Emelyanov721db932010-09-29 16:06:32 +04001347EXPORT_SYMBOL(__sock_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
1349int sock_create(int family, int type, int protocol, struct socket **res)
1350{
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001351 return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001353EXPORT_SYMBOL(sock_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
1355int sock_create_kern(int family, int type, int protocol, struct socket **res)
1356{
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001357 return __sock_create(&init_net, family, type, protocol, res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001359EXPORT_SYMBOL(sock_create_kern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001361SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362{
1363 int retval;
1364 struct socket *sock;
Ulrich Dreppera677a032008-07-23 21:29:17 -07001365 int flags;
1366
Ulrich Dreppere38b36f2008-07-23 21:29:42 -07001367 /* Check the SOCK_* constants for consistency. */
1368 BUILD_BUG_ON(SOCK_CLOEXEC != O_CLOEXEC);
1369 BUILD_BUG_ON((SOCK_MAX | SOCK_TYPE_MASK) != SOCK_TYPE_MASK);
1370 BUILD_BUG_ON(SOCK_CLOEXEC & SOCK_TYPE_MASK);
1371 BUILD_BUG_ON(SOCK_NONBLOCK & SOCK_TYPE_MASK);
1372
Ulrich Dreppera677a032008-07-23 21:29:17 -07001373 flags = type & ~SOCK_TYPE_MASK;
Ulrich Drepper77d27202008-07-23 21:29:35 -07001374 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Dreppera677a032008-07-23 21:29:17 -07001375 return -EINVAL;
1376 type &= SOCK_TYPE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001378 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1379 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1380
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 retval = sock_create(family, type, protocol, &sock);
1382 if (retval < 0)
1383 goto out;
1384
Ulrich Drepper77d27202008-07-23 21:29:35 -07001385 retval = sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 if (retval < 0)
1387 goto out_release;
1388
1389out:
1390 /* It may be already another descriptor 8) Not kernel problem. */
1391 return retval;
1392
1393out_release:
1394 sock_release(sock);
1395 return retval;
1396}
1397
1398/*
1399 * Create a pair of connected sockets.
1400 */
1401
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001402SYSCALL_DEFINE4(socketpair, int, family, int, type, int, protocol,
1403 int __user *, usockvec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
1405 struct socket *sock1, *sock2;
1406 int fd1, fd2, err;
Al Virodb349502007-02-07 01:48:00 -05001407 struct file *newfile1, *newfile2;
Ulrich Dreppera677a032008-07-23 21:29:17 -07001408 int flags;
1409
1410 flags = type & ~SOCK_TYPE_MASK;
Ulrich Drepper77d27202008-07-23 21:29:35 -07001411 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Dreppera677a032008-07-23 21:29:17 -07001412 return -EINVAL;
1413 type &= SOCK_TYPE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001415 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1416 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1417
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 /*
1419 * Obtain the first socket and check if the underlying protocol
1420 * supports the socketpair call.
1421 */
1422
1423 err = sock_create(family, type, protocol, &sock1);
1424 if (err < 0)
1425 goto out;
1426
1427 err = sock_create(family, type, protocol, &sock2);
1428 if (err < 0)
1429 goto out_release_1;
1430
1431 err = sock1->ops->socketpair(sock1, sock2);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001432 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 goto out_release_both;
1434
Al Viro28407632012-08-17 23:54:15 -04001435 fd1 = get_unused_fd_flags(flags);
David S. Millerbf3c23d2007-10-29 21:54:02 -07001436 if (unlikely(fd1 < 0)) {
1437 err = fd1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 goto out_release_both;
David S. Millerbf3c23d2007-10-29 21:54:02 -07001439 }
Al Viro28407632012-08-17 23:54:15 -04001440 fd2 = get_unused_fd_flags(flags);
Al Viro198de4d2009-08-05 19:29:23 +04001441 if (unlikely(fd2 < 0)) {
1442 err = fd2;
Al Viro28407632012-08-17 23:54:15 -04001443 put_unused_fd(fd1);
1444 goto out_release_both;
1445 }
1446
Linus Torvaldsaab174f2012-10-02 20:25:04 -07001447 newfile1 = sock_alloc_file(sock1, flags, NULL);
Al Viro28407632012-08-17 23:54:15 -04001448 if (unlikely(IS_ERR(newfile1))) {
1449 err = PTR_ERR(newfile1);
1450 put_unused_fd(fd1);
1451 put_unused_fd(fd2);
1452 goto out_release_both;
1453 }
1454
Linus Torvaldsaab174f2012-10-02 20:25:04 -07001455 newfile2 = sock_alloc_file(sock2, flags, NULL);
Al Viro28407632012-08-17 23:54:15 -04001456 if (IS_ERR(newfile2)) {
1457 err = PTR_ERR(newfile2);
Al Viro198de4d2009-08-05 19:29:23 +04001458 fput(newfile1);
1459 put_unused_fd(fd1);
Al Viro28407632012-08-17 23:54:15 -04001460 put_unused_fd(fd2);
Al Viro198de4d2009-08-05 19:29:23 +04001461 sock_release(sock2);
1462 goto out;
Al Virodb349502007-02-07 01:48:00 -05001463 }
1464
Al Viro157cf642008-12-14 04:57:47 -05001465 audit_fd_pair(fd1, fd2);
Al Virodb349502007-02-07 01:48:00 -05001466 fd_install(fd1, newfile1);
1467 fd_install(fd2, newfile2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 /* fd1 and fd2 may be already another descriptors.
1469 * Not kernel problem.
1470 */
1471
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001472 err = put_user(fd1, &usockvec[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 if (!err)
1474 err = put_user(fd2, &usockvec[1]);
1475 if (!err)
1476 return 0;
1477
1478 sys_close(fd2);
1479 sys_close(fd1);
1480 return err;
1481
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482out_release_both:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001483 sock_release(sock2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484out_release_1:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001485 sock_release(sock1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486out:
1487 return err;
1488}
1489
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490/*
1491 * Bind a name to a socket. Nothing much to do here since it's
1492 * the protocol's responsibility to handle the local address.
1493 *
1494 * We move the socket address to kernel space before we call
1495 * the protocol layer (having also checked the address is ok).
1496 */
1497
Heiko Carstens20f37032009-01-14 14:14:23 +01001498SYSCALL_DEFINE3(bind, int, fd, struct sockaddr __user *, umyaddr, int, addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499{
1500 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001501 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001502 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001504 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001505 if (sock) {
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001506 err = move_addr_to_kernel(umyaddr, addrlen, &address);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001507 if (err >= 0) {
1508 err = security_socket_bind(sock,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001509 (struct sockaddr *)&address,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001510 addrlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001511 if (!err)
1512 err = sock->ops->bind(sock,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001513 (struct sockaddr *)
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001514 &address, addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 }
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001516 fput_light(sock->file, fput_needed);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 return err;
1519}
1520
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521/*
1522 * Perform a listen. Basically, we allow the protocol to do anything
1523 * necessary for a listen, and if that works, we mark the socket as
1524 * ready for listening.
1525 */
1526
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001527SYSCALL_DEFINE2(listen, int, fd, int, backlog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528{
1529 struct socket *sock;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001530 int err, fput_needed;
Pavel Emelyanovb8e1f9b2007-12-08 00:12:33 -08001531 int somaxconn;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001532
1533 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1534 if (sock) {
Pavel Emelyanov8efa6e92008-03-31 19:41:14 -07001535 somaxconn = sock_net(sock->sk)->core.sysctl_somaxconn;
Eric Dumazet95c96172012-04-15 05:58:06 +00001536 if ((unsigned int)backlog > somaxconn)
Pavel Emelyanovb8e1f9b2007-12-08 00:12:33 -08001537 backlog = somaxconn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
1539 err = security_socket_listen(sock, backlog);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001540 if (!err)
1541 err = sock->ops->listen(sock, backlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001543 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 }
1545 return err;
1546}
1547
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548/*
1549 * For accept, we attempt to create a new socket, set up the link
1550 * with the client, wake up the client, then return the new
1551 * connected fd. We collect the address of the connector in kernel
1552 * space and move it to user at the very end. This is unclean because
1553 * we open the socket then return an error.
1554 *
1555 * 1003.1g adds the ability to recvmsg() to query connection pending
1556 * status to recvmsg. We need to add that support in a way thats
1557 * clean when we restucture accept also.
1558 */
1559
Heiko Carstens20f37032009-01-14 14:14:23 +01001560SYSCALL_DEFINE4(accept4, int, fd, struct sockaddr __user *, upeer_sockaddr,
1561 int __user *, upeer_addrlen, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562{
1563 struct socket *sock, *newsock;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001564 struct file *newfile;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001565 int err, len, newfd, fput_needed;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001566 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
Ulrich Drepper77d27202008-07-23 21:29:35 -07001568 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001569 return -EINVAL;
1570
1571 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1572 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1573
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 = -ENFILE;
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001579 newsock = sock_alloc();
1580 if (!newsock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 goto out_put;
1582
1583 newsock->type = sock->type;
1584 newsock->ops = sock->ops;
1585
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 /*
1587 * We don't need try_module_get here, as the listening socket (sock)
1588 * has the protocol module (sock->ops->owner) held.
1589 */
1590 __module_get(newsock->ops->owner);
1591
Al Viro28407632012-08-17 23:54:15 -04001592 newfd = get_unused_fd_flags(flags);
David S. Miller39d8c1b2006-03-20 17:13:49 -08001593 if (unlikely(newfd < 0)) {
1594 err = newfd;
David S. Miller9a1875e2006-04-01 12:48:36 -08001595 sock_release(newsock);
1596 goto out_put;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001597 }
Linus Torvaldsaab174f2012-10-02 20:25:04 -07001598 newfile = sock_alloc_file(newsock, flags, sock->sk->sk_prot_creator->name);
Al Viro28407632012-08-17 23:54:15 -04001599 if (unlikely(IS_ERR(newfile))) {
1600 err = PTR_ERR(newfile);
1601 put_unused_fd(newfd);
1602 sock_release(newsock);
1603 goto out_put;
1604 }
David S. Miller39d8c1b2006-03-20 17:13:49 -08001605
Frank Filza79af592005-09-27 15:23:38 -07001606 err = security_socket_accept(sock, newsock);
1607 if (err)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001608 goto out_fd;
Frank Filza79af592005-09-27 15:23:38 -07001609
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 err = sock->ops->accept(sock, newsock, sock->file->f_flags);
1611 if (err < 0)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001612 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
1614 if (upeer_sockaddr) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001615 if (newsock->ops->getname(newsock, (struct sockaddr *)&address,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001616 &len, 2) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 err = -ECONNABORTED;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001618 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 }
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001620 err = move_addr_to_user(&address,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001621 len, upeer_sockaddr, upeer_addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 if (err < 0)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001623 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 }
1625
1626 /* File flags are not inherited via accept() unlike another OSes. */
1627
David S. Miller39d8c1b2006-03-20 17:13:49 -08001628 fd_install(newfd, newfile);
1629 err = newfd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001632 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633out:
1634 return err;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001635out_fd:
David S. Miller9606a212006-04-01 01:00:14 -08001636 fput(newfile);
David S. Miller39d8c1b2006-03-20 17:13:49 -08001637 put_unused_fd(newfd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 goto out_put;
1639}
1640
Heiko Carstens20f37032009-01-14 14:14:23 +01001641SYSCALL_DEFINE3(accept, int, fd, struct sockaddr __user *, upeer_sockaddr,
1642 int __user *, upeer_addrlen)
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001643{
Ulrich Drepperde11def2008-11-19 15:36:14 -08001644 return sys_accept4(fd, upeer_sockaddr, upeer_addrlen, 0);
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001645}
1646
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647/*
1648 * Attempt to connect to a socket with the server address. The address
1649 * is in user space so we verify it is OK and move it to kernel space.
1650 *
1651 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
1652 * break bindings
1653 *
1654 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
1655 * other SEQPACKET protocols that take time to connect() as it doesn't
1656 * include the -EINPROGRESS status for such sockets.
1657 */
1658
Heiko Carstens20f37032009-01-14 14:14:23 +01001659SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
1660 int, addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661{
1662 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001663 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001664 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001666 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 if (!sock)
1668 goto out;
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001669 err = move_addr_to_kernel(uservaddr, addrlen, &address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 if (err < 0)
1671 goto out_put;
1672
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001673 err =
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001674 security_socket_connect(sock, (struct sockaddr *)&address, addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 if (err)
1676 goto out_put;
1677
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001678 err = sock->ops->connect(sock, (struct sockaddr *)&address, addrlen,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 sock->file->f_flags);
1680out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001681 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682out:
1683 return err;
1684}
1685
1686/*
1687 * Get the local address ('name') of a socket object. Move the obtained
1688 * name to user space.
1689 */
1690
Heiko Carstens20f37032009-01-14 14:14:23 +01001691SYSCALL_DEFINE3(getsockname, int, fd, struct sockaddr __user *, usockaddr,
1692 int __user *, usockaddr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693{
1694 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001695 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001696 int len, err, fput_needed;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001697
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001698 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 if (!sock)
1700 goto out;
1701
1702 err = security_socket_getsockname(sock);
1703 if (err)
1704 goto out_put;
1705
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001706 err = sock->ops->getname(sock, (struct sockaddr *)&address, &len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 if (err)
1708 goto out_put;
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001709 err = move_addr_to_user(&address, len, usockaddr, usockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710
1711out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001712 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713out:
1714 return err;
1715}
1716
1717/*
1718 * Get the remote address ('name') of a socket object. Move the obtained
1719 * name to user space.
1720 */
1721
Heiko Carstens20f37032009-01-14 14:14:23 +01001722SYSCALL_DEFINE3(getpeername, int, fd, struct sockaddr __user *, usockaddr,
1723 int __user *, usockaddr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724{
1725 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001726 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001727 int len, err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001729 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1730 if (sock != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 err = security_socket_getpeername(sock);
1732 if (err) {
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001733 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 return err;
1735 }
1736
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001737 err =
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001738 sock->ops->getname(sock, (struct sockaddr *)&address, &len,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001739 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 if (!err)
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001741 err = move_addr_to_user(&address, len, usockaddr,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001742 usockaddr_len);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001743 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 }
1745 return err;
1746}
1747
1748/*
1749 * Send a datagram to a given address. We move the address into kernel
1750 * space and check the user space data area is readable before invoking
1751 * the protocol.
1752 */
1753
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001754SYSCALL_DEFINE6(sendto, int, fd, void __user *, buff, size_t, len,
Eric Dumazet95c96172012-04-15 05:58:06 +00001755 unsigned int, flags, struct sockaddr __user *, addr,
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001756 int, addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757{
1758 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001759 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 int err;
1761 struct msghdr msg;
1762 struct iovec iov;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001763 int fput_needed;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001764
Linus Torvalds253eacc2010-10-30 16:43:10 -07001765 if (len > INT_MAX)
1766 len = INT_MAX;
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001767 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1768 if (!sock)
David S. Miller4387ff72007-02-08 15:06:08 -08001769 goto out;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001770
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001771 iov.iov_base = buff;
1772 iov.iov_len = len;
1773 msg.msg_name = NULL;
1774 msg.msg_iov = &iov;
1775 msg.msg_iovlen = 1;
1776 msg.msg_control = NULL;
1777 msg.msg_controllen = 0;
1778 msg.msg_namelen = 0;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001779 if (addr) {
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001780 err = move_addr_to_kernel(addr, addr_len, &address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 if (err < 0)
1782 goto out_put;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001783 msg.msg_name = (struct sockaddr *)&address;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001784 msg.msg_namelen = addr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 }
1786 if (sock->file->f_flags & O_NONBLOCK)
1787 flags |= MSG_DONTWAIT;
1788 msg.msg_flags = flags;
1789 err = sock_sendmsg(sock, &msg, len);
1790
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001791out_put:
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001792 fput_light(sock->file, fput_needed);
David S. Miller4387ff72007-02-08 15:06:08 -08001793out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 return err;
1795}
1796
1797/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001798 * Send a datagram down a socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 */
1800
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001801SYSCALL_DEFINE4(send, int, fd, void __user *, buff, size_t, len,
Eric Dumazet95c96172012-04-15 05:58:06 +00001802 unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803{
1804 return sys_sendto(fd, buff, len, flags, NULL, 0);
1805}
1806
1807/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001808 * Receive a frame from the socket and optionally record the address of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 * sender. We verify the buffers are writable and if needed move the
1810 * sender address from kernel to user space.
1811 */
1812
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001813SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, ubuf, size_t, size,
Eric Dumazet95c96172012-04-15 05:58:06 +00001814 unsigned int, flags, struct sockaddr __user *, addr,
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001815 int __user *, addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816{
1817 struct socket *sock;
1818 struct iovec iov;
1819 struct msghdr msg;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001820 struct sockaddr_storage address;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001821 int err, err2;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001822 int fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
Linus Torvalds253eacc2010-10-30 16:43:10 -07001824 if (size > INT_MAX)
1825 size = INT_MAX;
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001826 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 if (!sock)
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001828 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001830 msg.msg_control = NULL;
1831 msg.msg_controllen = 0;
1832 msg.msg_iovlen = 1;
1833 msg.msg_iov = &iov;
1834 iov.iov_len = size;
1835 iov.iov_base = ubuf;
Hannes Frederic Sowa2f73d7f2013-11-21 03:14:22 +01001836 /* Save some cycles and don't copy the address if not needed */
1837 msg.msg_name = addr ? (struct sockaddr *)&address : NULL;
1838 /* We assume all kernel code knows the size of sockaddr_storage */
1839 msg.msg_namelen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 if (sock->file->f_flags & O_NONBLOCK)
1841 flags |= MSG_DONTWAIT;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001842 err = sock_recvmsg(sock, &msg, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001844 if (err >= 0 && addr != NULL) {
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001845 err2 = move_addr_to_user(&address,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001846 msg.msg_namelen, addr, addr_len);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001847 if (err2 < 0)
1848 err = err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 }
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001850
1851 fput_light(sock->file, fput_needed);
David S. Miller4387ff72007-02-08 15:06:08 -08001852out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 return err;
1854}
1855
1856/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001857 * Receive a datagram from a socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 */
1859
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001860asmlinkage long sys_recv(int fd, void __user *ubuf, size_t size,
Eric Dumazet95c96172012-04-15 05:58:06 +00001861 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862{
1863 return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
1864}
1865
1866/*
1867 * Set a socket option. Because we don't know the option lengths we have
1868 * to pass the user mode parameter for the protocols to sort out.
1869 */
1870
Heiko Carstens20f37032009-01-14 14:14:23 +01001871SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname,
1872 char __user *, optval, int, optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001874 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 struct socket *sock;
1876
1877 if (optlen < 0)
1878 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001879
1880 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1881 if (sock != NULL) {
1882 err = security_socket_setsockopt(sock, level, optname);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001883 if (err)
1884 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
1886 if (level == SOL_SOCKET)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001887 err =
1888 sock_setsockopt(sock, level, optname, optval,
1889 optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 else
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001891 err =
1892 sock->ops->setsockopt(sock, level, optname, optval,
1893 optlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001894out_put:
1895 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 }
1897 return err;
1898}
1899
1900/*
1901 * Get a socket option. Because we don't know the option lengths we have
1902 * to pass a user mode parameter for the protocols to sort out.
1903 */
1904
Heiko Carstens20f37032009-01-14 14:14:23 +01001905SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname,
1906 char __user *, optval, int __user *, optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001908 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 struct socket *sock;
1910
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001911 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1912 if (sock != NULL) {
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001913 err = security_socket_getsockopt(sock, level, optname);
1914 if (err)
1915 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
1917 if (level == SOL_SOCKET)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001918 err =
1919 sock_getsockopt(sock, level, optname, optval,
1920 optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 else
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001922 err =
1923 sock->ops->getsockopt(sock, level, optname, optval,
1924 optlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001925out_put:
1926 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 }
1928 return err;
1929}
1930
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931/*
1932 * Shutdown a socket.
1933 */
1934
Heiko Carstens754fe8d2009-01-14 14:14:09 +01001935SYSCALL_DEFINE2(shutdown, int, fd, int, how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001937 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 struct socket *sock;
1939
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001940 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1941 if (sock != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 err = security_socket_shutdown(sock, how);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001943 if (!err)
1944 err = sock->ops->shutdown(sock, how);
1945 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 }
1947 return err;
1948}
1949
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001950/* A couple of helpful macros for getting the address of the 32/64 bit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 * fields which are the same type (int / unsigned) on our platforms.
1952 */
1953#define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
1954#define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
1955#define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
1956
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00001957struct used_address {
1958 struct sockaddr_storage name;
1959 unsigned int name_len;
1960};
1961
Dan Carpenter2e8d97a2013-10-03 00:27:20 +03001962static int copy_msghdr_from_user(struct msghdr *kmsg,
1963 struct msghdr __user *umsg)
1964{
1965 if (copy_from_user(kmsg, umsg, sizeof(struct msghdr)))
1966 return -EFAULT;
Matthew Leach29a322f2014-03-11 11:58:27 +00001967
1968 if (kmsg->msg_namelen < 0)
1969 return -EINVAL;
1970
Dan Carpenter2e8d97a2013-10-03 00:27:20 +03001971 if (kmsg->msg_namelen > sizeof(struct sockaddr_storage))
Dan Carpentered47bba2013-11-27 15:40:21 +03001972 kmsg->msg_namelen = sizeof(struct sockaddr_storage);
Dan Carpenter2e8d97a2013-10-03 00:27:20 +03001973 return 0;
1974}
1975
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00001976static int ___sys_sendmsg(struct socket *sock, struct msghdr __user *msg,
Eric Dumazet95c96172012-04-15 05:58:06 +00001977 struct msghdr *msg_sys, unsigned int flags,
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00001978 struct used_address *used_address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979{
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001980 struct compat_msghdr __user *msg_compat =
1981 (struct compat_msghdr __user *)msg;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001982 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
Alex Williamsonb9d717a2005-09-26 14:28:02 -07001984 unsigned char ctl[sizeof(struct cmsghdr) + 20]
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001985 __attribute__ ((aligned(sizeof(__kernel_size_t))));
1986 /* 20 is size of ipv6_pktinfo */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 unsigned char *ctl_buf = ctl;
Eric Dumazeta74e9102012-04-20 20:04:01 +02001988 int err, ctl_len, total_len;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001989
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 err = -EFAULT;
1991 if (MSG_CMSG_COMPAT & flags) {
Anton Blanchard228e5482011-05-02 20:21:35 +00001992 if (get_compat_msghdr(msg_sys, msg_compat))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 return -EFAULT;
Dan Carpenter2e8d97a2013-10-03 00:27:20 +03001994 } else {
1995 err = copy_msghdr_from_user(msg_sys, msg);
1996 if (err)
1997 return err;
1998 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
Anton Blanchard228e5482011-05-02 20:21:35 +00002000 if (msg_sys->msg_iovlen > UIO_FASTIOV) {
Eric Dumazeta74e9102012-04-20 20:04:01 +02002001 err = -EMSGSIZE;
2002 if (msg_sys->msg_iovlen > UIO_MAXIOV)
2003 goto out;
2004 err = -ENOMEM;
2005 iov = kmalloc(msg_sys->msg_iovlen * sizeof(struct iovec),
2006 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 if (!iov)
Anton Blanchard228e5482011-05-02 20:21:35 +00002008 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 }
2010
2011 /* This will also move the address data into kernel space */
2012 if (MSG_CMSG_COMPAT & flags) {
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00002013 err = verify_compat_iovec(msg_sys, iov, &address, VERIFY_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 } else
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00002015 err = verify_iovec(msg_sys, iov, &address, VERIFY_READ);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002016 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 goto out_freeiov;
2018 total_len = err;
2019
2020 err = -ENOBUFS;
2021
Anton Blanchard228e5482011-05-02 20:21:35 +00002022 if (msg_sys->msg_controllen > INT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 goto out_freeiov;
Anton Blanchard228e5482011-05-02 20:21:35 +00002024 ctl_len = msg_sys->msg_controllen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002026 err =
Anton Blanchard228e5482011-05-02 20:21:35 +00002027 cmsghdr_from_user_compat_to_kern(msg_sys, sock->sk, ctl,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002028 sizeof(ctl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 if (err)
2030 goto out_freeiov;
Anton Blanchard228e5482011-05-02 20:21:35 +00002031 ctl_buf = msg_sys->msg_control;
2032 ctl_len = msg_sys->msg_controllen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 } else if (ctl_len) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002034 if (ctl_len > sizeof(ctl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002036 if (ctl_buf == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 goto out_freeiov;
2038 }
2039 err = -EFAULT;
2040 /*
Anton Blanchard228e5482011-05-02 20:21:35 +00002041 * Careful! Before this, msg_sys->msg_control contains a user pointer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
2043 * checking falls down on this.
2044 */
Namhyung Kimfb8621b2010-09-07 03:55:00 +00002045 if (copy_from_user(ctl_buf,
Anton Blanchard228e5482011-05-02 20:21:35 +00002046 (void __user __force *)msg_sys->msg_control,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002047 ctl_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 goto out_freectl;
Anton Blanchard228e5482011-05-02 20:21:35 +00002049 msg_sys->msg_control = ctl_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 }
Anton Blanchard228e5482011-05-02 20:21:35 +00002051 msg_sys->msg_flags = flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
2053 if (sock->file->f_flags & O_NONBLOCK)
Anton Blanchard228e5482011-05-02 20:21:35 +00002054 msg_sys->msg_flags |= MSG_DONTWAIT;
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002055 /*
2056 * If this is sendmmsg() and current destination address is same as
2057 * previously succeeded address, omit asking LSM's decision.
2058 * used_address->name_len is initialized to UINT_MAX so that the first
2059 * destination address never matches.
2060 */
Mathieu Desnoyersbc909d92011-08-24 19:45:03 -07002061 if (used_address && msg_sys->msg_name &&
2062 used_address->name_len == msg_sys->msg_namelen &&
2063 !memcmp(&used_address->name, msg_sys->msg_name,
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002064 used_address->name_len)) {
2065 err = sock_sendmsg_nosec(sock, msg_sys, total_len);
2066 goto out_freectl;
2067 }
2068 err = sock_sendmsg(sock, msg_sys, total_len);
2069 /*
2070 * If this is sendmmsg() and sending to current destination address was
2071 * successful, remember it.
2072 */
2073 if (used_address && err >= 0) {
2074 used_address->name_len = msg_sys->msg_namelen;
Mathieu Desnoyersbc909d92011-08-24 19:45:03 -07002075 if (msg_sys->msg_name)
2076 memcpy(&used_address->name, msg_sys->msg_name,
2077 used_address->name_len);
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
2080out_freectl:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002081 if (ctl_buf != ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 sock_kfree_s(sock->sk, ctl_buf, ctl_len);
2083out_freeiov:
2084 if (iov != iovstack)
Eric Dumazeta74e9102012-04-20 20:04:01 +02002085 kfree(iov);
Anton Blanchard228e5482011-05-02 20:21:35 +00002086out:
2087 return err;
2088}
2089
2090/*
2091 * BSD sendmsg interface
2092 */
2093
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002094long __sys_sendmsg(int fd, struct msghdr __user *msg, unsigned flags)
Anton Blanchard228e5482011-05-02 20:21:35 +00002095{
2096 int fput_needed, err;
2097 struct msghdr msg_sys;
Andy Lutomirski1be374a2013-05-22 14:07:44 -07002098 struct socket *sock;
Anton Blanchard228e5482011-05-02 20:21:35 +00002099
Andy Lutomirski1be374a2013-05-22 14:07:44 -07002100 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Anton Blanchard228e5482011-05-02 20:21:35 +00002101 if (!sock)
2102 goto out;
2103
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002104 err = ___sys_sendmsg(sock, msg, &msg_sys, flags, NULL);
Anton Blanchard228e5482011-05-02 20:21:35 +00002105
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002106 fput_light(sock->file, fput_needed);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002107out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 return err;
2109}
2110
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002111SYSCALL_DEFINE3(sendmsg, int, fd, struct msghdr __user *, msg, unsigned int, flags)
2112{
2113 if (flags & MSG_CMSG_COMPAT)
2114 return -EINVAL;
2115 return __sys_sendmsg(fd, msg, flags);
2116}
2117
Anton Blanchard228e5482011-05-02 20:21:35 +00002118/*
2119 * Linux sendmmsg interface
2120 */
2121
2122int __sys_sendmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
2123 unsigned int flags)
2124{
2125 int fput_needed, err, datagrams;
2126 struct socket *sock;
2127 struct mmsghdr __user *entry;
2128 struct compat_mmsghdr __user *compat_entry;
2129 struct msghdr msg_sys;
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002130 struct used_address used_address;
Anton Blanchard228e5482011-05-02 20:21:35 +00002131
Anton Blanchard98382f42011-08-04 14:07:39 +00002132 if (vlen > UIO_MAXIOV)
2133 vlen = UIO_MAXIOV;
Anton Blanchard228e5482011-05-02 20:21:35 +00002134
2135 datagrams = 0;
2136
2137 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2138 if (!sock)
2139 return err;
2140
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002141 used_address.name_len = UINT_MAX;
Anton Blanchard228e5482011-05-02 20:21:35 +00002142 entry = mmsg;
2143 compat_entry = (struct compat_mmsghdr __user *)mmsg;
Anton Blanchard728ffb82011-08-04 14:07:38 +00002144 err = 0;
Anton Blanchard228e5482011-05-02 20:21:35 +00002145
2146 while (datagrams < vlen) {
Anton Blanchard228e5482011-05-02 20:21:35 +00002147 if (MSG_CMSG_COMPAT & flags) {
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002148 err = ___sys_sendmsg(sock, (struct msghdr __user *)compat_entry,
2149 &msg_sys, flags, &used_address);
Anton Blanchard228e5482011-05-02 20:21:35 +00002150 if (err < 0)
2151 break;
2152 err = __put_user(err, &compat_entry->msg_len);
2153 ++compat_entry;
2154 } else {
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002155 err = ___sys_sendmsg(sock,
2156 (struct msghdr __user *)entry,
2157 &msg_sys, flags, &used_address);
Anton Blanchard228e5482011-05-02 20:21:35 +00002158 if (err < 0)
2159 break;
2160 err = put_user(err, &entry->msg_len);
2161 ++entry;
2162 }
2163
2164 if (err)
2165 break;
2166 ++datagrams;
2167 }
2168
Anton Blanchard228e5482011-05-02 20:21:35 +00002169 fput_light(sock->file, fput_needed);
2170
Anton Blanchard728ffb82011-08-04 14:07:38 +00002171 /* We only return an error if no datagrams were able to be sent */
2172 if (datagrams != 0)
Anton Blanchard228e5482011-05-02 20:21:35 +00002173 return datagrams;
2174
Anton Blanchard228e5482011-05-02 20:21:35 +00002175 return err;
2176}
2177
2178SYSCALL_DEFINE4(sendmmsg, int, fd, struct mmsghdr __user *, mmsg,
2179 unsigned int, vlen, unsigned int, flags)
2180{
Andy Lutomirski1be374a2013-05-22 14:07:44 -07002181 if (flags & MSG_CMSG_COMPAT)
2182 return -EINVAL;
Anton Blanchard228e5482011-05-02 20:21:35 +00002183 return __sys_sendmmsg(fd, mmsg, vlen, flags);
2184}
2185
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002186static int ___sys_recvmsg(struct socket *sock, struct msghdr __user *msg,
Eric Dumazet95c96172012-04-15 05:58:06 +00002187 struct msghdr *msg_sys, unsigned int flags, int nosec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188{
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002189 struct compat_msghdr __user *msg_compat =
2190 (struct compat_msghdr __user *)msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 struct iovec iovstack[UIO_FASTIOV];
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002192 struct iovec *iov = iovstack;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 unsigned long cmsg_ptr;
Eric Dumazeta74e9102012-04-20 20:04:01 +02002194 int err, total_len, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
2196 /* kernel mode address */
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07002197 struct sockaddr_storage addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
2199 /* user mode address pointers */
2200 struct sockaddr __user *uaddr;
2201 int __user *uaddr_len;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002202
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 if (MSG_CMSG_COMPAT & flags) {
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002204 if (get_compat_msghdr(msg_sys, msg_compat))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 return -EFAULT;
Dan Carpenter2e8d97a2013-10-03 00:27:20 +03002206 } else {
2207 err = copy_msghdr_from_user(msg_sys, msg);
2208 if (err)
2209 return err;
2210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002212 if (msg_sys->msg_iovlen > UIO_FASTIOV) {
Eric Dumazeta74e9102012-04-20 20:04:01 +02002213 err = -EMSGSIZE;
2214 if (msg_sys->msg_iovlen > UIO_MAXIOV)
2215 goto out;
2216 err = -ENOMEM;
2217 iov = kmalloc(msg_sys->msg_iovlen * sizeof(struct iovec),
2218 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 if (!iov)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002220 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 }
2222
Hannes Frederic Sowa2f73d7f2013-11-21 03:14:22 +01002223 /* Save the user-mode address (verify_iovec will change the
2224 * kernel msghdr to use the kernel address space)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 */
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002226 uaddr = (__force void __user *)msg_sys->msg_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 uaddr_len = COMPAT_NAMELEN(msg);
Hannes Frederic Sowa2f73d7f2013-11-21 03:14:22 +01002228 if (MSG_CMSG_COMPAT & flags)
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00002229 err = verify_compat_iovec(msg_sys, iov, &addr, VERIFY_WRITE);
Hannes Frederic Sowa2f73d7f2013-11-21 03:14:22 +01002230 else
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00002231 err = verify_iovec(msg_sys, iov, &addr, VERIFY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 if (err < 0)
2233 goto out_freeiov;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002234 total_len = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002236 cmsg_ptr = (unsigned long)msg_sys->msg_control;
2237 msg_sys->msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002238
Hannes Frederic Sowa2f73d7f2013-11-21 03:14:22 +01002239 /* We assume all kernel code knows the size of sockaddr_storage */
2240 msg_sys->msg_namelen = 0;
2241
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 if (sock->file->f_flags & O_NONBLOCK)
2243 flags |= MSG_DONTWAIT;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002244 err = (nosec ? sock_recvmsg_nosec : sock_recvmsg)(sock, msg_sys,
2245 total_len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 if (err < 0)
2247 goto out_freeiov;
2248 len = err;
2249
2250 if (uaddr != NULL) {
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00002251 err = move_addr_to_user(&addr,
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002252 msg_sys->msg_namelen, uaddr,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002253 uaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 if (err < 0)
2255 goto out_freeiov;
2256 }
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002257 err = __put_user((msg_sys->msg_flags & ~MSG_CMSG_COMPAT),
David S. Miller37f7f422005-09-16 16:51:01 -07002258 COMPAT_FLAGS(msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 if (err)
2260 goto out_freeiov;
2261 if (MSG_CMSG_COMPAT & flags)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002262 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 &msg_compat->msg_controllen);
2264 else
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002265 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 &msg->msg_controllen);
2267 if (err)
2268 goto out_freeiov;
2269 err = len;
2270
2271out_freeiov:
2272 if (iov != iovstack)
Eric Dumazeta74e9102012-04-20 20:04:01 +02002273 kfree(iov);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002274out:
2275 return err;
2276}
2277
2278/*
2279 * BSD recvmsg interface
2280 */
2281
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002282long __sys_recvmsg(int fd, struct msghdr __user *msg, unsigned flags)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002283{
2284 int fput_needed, err;
2285 struct msghdr msg_sys;
Andy Lutomirski1be374a2013-05-22 14:07:44 -07002286 struct socket *sock;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002287
Andy Lutomirski1be374a2013-05-22 14:07:44 -07002288 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002289 if (!sock)
2290 goto out;
2291
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002292 err = ___sys_recvmsg(sock, msg, &msg_sys, flags, 0);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002293
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002294 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295out:
2296 return err;
2297}
2298
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002299SYSCALL_DEFINE3(recvmsg, int, fd, struct msghdr __user *, msg,
2300 unsigned int, flags)
2301{
2302 if (flags & MSG_CMSG_COMPAT)
2303 return -EINVAL;
2304 return __sys_recvmsg(fd, msg, flags);
2305}
2306
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002307/*
2308 * Linux recvmmsg interface
2309 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002311int __sys_recvmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
2312 unsigned int flags, struct timespec *timeout)
2313{
2314 int fput_needed, err, datagrams;
2315 struct socket *sock;
2316 struct mmsghdr __user *entry;
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002317 struct compat_mmsghdr __user *compat_entry;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002318 struct msghdr msg_sys;
2319 struct timespec end_time;
2320
2321 if (timeout &&
2322 poll_select_set_timeout(&end_time, timeout->tv_sec,
2323 timeout->tv_nsec))
2324 return -EINVAL;
2325
2326 datagrams = 0;
2327
2328 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2329 if (!sock)
2330 return err;
2331
2332 err = sock_error(sock->sk);
2333 if (err)
2334 goto out_put;
2335
2336 entry = mmsg;
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002337 compat_entry = (struct compat_mmsghdr __user *)mmsg;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002338
2339 while (datagrams < vlen) {
2340 /*
2341 * No need to ask LSM for more than the first datagram.
2342 */
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002343 if (MSG_CMSG_COMPAT & flags) {
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002344 err = ___sys_recvmsg(sock, (struct msghdr __user *)compat_entry,
2345 &msg_sys, flags & ~MSG_WAITFORONE,
2346 datagrams);
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002347 if (err < 0)
2348 break;
2349 err = __put_user(err, &compat_entry->msg_len);
2350 ++compat_entry;
2351 } else {
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002352 err = ___sys_recvmsg(sock,
2353 (struct msghdr __user *)entry,
2354 &msg_sys, flags & ~MSG_WAITFORONE,
2355 datagrams);
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002356 if (err < 0)
2357 break;
2358 err = put_user(err, &entry->msg_len);
2359 ++entry;
2360 }
2361
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002362 if (err)
2363 break;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002364 ++datagrams;
2365
Brandon L Black71c5c152010-03-26 16:18:03 +00002366 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
2367 if (flags & MSG_WAITFORONE)
2368 flags |= MSG_DONTWAIT;
2369
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002370 if (timeout) {
2371 ktime_get_ts(timeout);
2372 *timeout = timespec_sub(end_time, *timeout);
2373 if (timeout->tv_sec < 0) {
2374 timeout->tv_sec = timeout->tv_nsec = 0;
2375 break;
2376 }
2377
2378 /* Timeout, return less than vlen datagrams */
2379 if (timeout->tv_nsec == 0 && timeout->tv_sec == 0)
2380 break;
2381 }
2382
2383 /* Out of band data, return right away */
2384 if (msg_sys.msg_flags & MSG_OOB)
2385 break;
2386 }
2387
2388out_put:
2389 fput_light(sock->file, fput_needed);
2390
2391 if (err == 0)
2392 return datagrams;
2393
2394 if (datagrams != 0) {
2395 /*
2396 * We may return less entries than requested (vlen) if the
2397 * sock is non block and there aren't enough datagrams...
2398 */
2399 if (err != -EAGAIN) {
2400 /*
2401 * ... or if recvmsg returns an error after we
2402 * received some datagrams, where we record the
2403 * error to return on the next call or if the
2404 * app asks about it using getsockopt(SO_ERROR).
2405 */
2406 sock->sk->sk_err = -err;
2407 }
2408
2409 return datagrams;
2410 }
2411
2412 return err;
2413}
2414
2415SYSCALL_DEFINE5(recvmmsg, int, fd, struct mmsghdr __user *, mmsg,
2416 unsigned int, vlen, unsigned int, flags,
2417 struct timespec __user *, timeout)
2418{
2419 int datagrams;
2420 struct timespec timeout_sys;
2421
Andy Lutomirski1be374a2013-05-22 14:07:44 -07002422 if (flags & MSG_CMSG_COMPAT)
2423 return -EINVAL;
2424
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002425 if (!timeout)
2426 return __sys_recvmmsg(fd, mmsg, vlen, flags, NULL);
2427
2428 if (copy_from_user(&timeout_sys, timeout, sizeof(timeout_sys)))
2429 return -EFAULT;
2430
2431 datagrams = __sys_recvmmsg(fd, mmsg, vlen, flags, &timeout_sys);
2432
2433 if (datagrams > 0 &&
2434 copy_to_user(timeout, &timeout_sys, sizeof(timeout_sys)))
2435 datagrams = -EFAULT;
2436
2437 return datagrams;
2438}
2439
2440#ifdef __ARCH_WANT_SYS_SOCKETCALL
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441/* Argument list sizes for sys_socketcall */
2442#define AL(x) ((x) * sizeof(unsigned long))
Anton Blanchard228e5482011-05-02 20:21:35 +00002443static const unsigned char nargs[21] = {
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002444 AL(0), AL(3), AL(3), AL(3), AL(2), AL(3),
2445 AL(3), AL(3), AL(4), AL(4), AL(4), AL(6),
2446 AL(6), AL(2), AL(5), AL(5), AL(3), AL(3),
Anton Blanchard228e5482011-05-02 20:21:35 +00002447 AL(4), AL(5), AL(4)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002448};
2449
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450#undef AL
2451
2452/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002453 * System call vectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 *
2455 * Argument checking cleaned up. Saved 20% in size.
2456 * This function doesn't need to set the kernel lock because
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002457 * it is set by the callees.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 */
2459
Heiko Carstens3e0fa652009-01-14 14:14:24 +01002460SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461{
Chen Gang2950fa92013-04-07 16:55:23 +08002462 unsigned long a[AUDITSC_ARGS];
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002463 unsigned long a0, a1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 int err;
Arjan van de Ven47379052009-09-28 12:57:44 -07002465 unsigned int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466
Anton Blanchard228e5482011-05-02 20:21:35 +00002467 if (call < 1 || call > SYS_SENDMMSG)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 return -EINVAL;
2469
Arjan van de Ven47379052009-09-28 12:57:44 -07002470 len = nargs[call];
2471 if (len > sizeof(a))
2472 return -EINVAL;
2473
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 /* copy_from_user should be SMP safe. */
Arjan van de Ven47379052009-09-28 12:57:44 -07002475 if (copy_from_user(a, args, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 return -EFAULT;
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002477
Chen Gang2950fa92013-04-07 16:55:23 +08002478 err = audit_socketcall(nargs[call] / sizeof(unsigned long), a);
2479 if (err)
2480 return err;
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002481
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002482 a0 = a[0];
2483 a1 = a[1];
2484
2485 switch (call) {
2486 case SYS_SOCKET:
2487 err = sys_socket(a0, a1, a[2]);
2488 break;
2489 case SYS_BIND:
2490 err = sys_bind(a0, (struct sockaddr __user *)a1, a[2]);
2491 break;
2492 case SYS_CONNECT:
2493 err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
2494 break;
2495 case SYS_LISTEN:
2496 err = sys_listen(a0, a1);
2497 break;
2498 case SYS_ACCEPT:
Ulrich Drepperde11def2008-11-19 15:36:14 -08002499 err = sys_accept4(a0, (struct sockaddr __user *)a1,
2500 (int __user *)a[2], 0);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002501 break;
2502 case SYS_GETSOCKNAME:
2503 err =
2504 sys_getsockname(a0, (struct sockaddr __user *)a1,
2505 (int __user *)a[2]);
2506 break;
2507 case SYS_GETPEERNAME:
2508 err =
2509 sys_getpeername(a0, (struct sockaddr __user *)a1,
2510 (int __user *)a[2]);
2511 break;
2512 case SYS_SOCKETPAIR:
2513 err = sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
2514 break;
2515 case SYS_SEND:
2516 err = sys_send(a0, (void __user *)a1, a[2], a[3]);
2517 break;
2518 case SYS_SENDTO:
2519 err = sys_sendto(a0, (void __user *)a1, a[2], a[3],
2520 (struct sockaddr __user *)a[4], a[5]);
2521 break;
2522 case SYS_RECV:
2523 err = sys_recv(a0, (void __user *)a1, a[2], a[3]);
2524 break;
2525 case SYS_RECVFROM:
2526 err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
2527 (struct sockaddr __user *)a[4],
2528 (int __user *)a[5]);
2529 break;
2530 case SYS_SHUTDOWN:
2531 err = sys_shutdown(a0, a1);
2532 break;
2533 case SYS_SETSOCKOPT:
2534 err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]);
2535 break;
2536 case SYS_GETSOCKOPT:
2537 err =
2538 sys_getsockopt(a0, a1, a[2], (char __user *)a[3],
2539 (int __user *)a[4]);
2540 break;
2541 case SYS_SENDMSG:
2542 err = sys_sendmsg(a0, (struct msghdr __user *)a1, a[2]);
2543 break;
Anton Blanchard228e5482011-05-02 20:21:35 +00002544 case SYS_SENDMMSG:
2545 err = sys_sendmmsg(a0, (struct mmsghdr __user *)a1, a[2], a[3]);
2546 break;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002547 case SYS_RECVMSG:
2548 err = sys_recvmsg(a0, (struct msghdr __user *)a1, a[2]);
2549 break;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002550 case SYS_RECVMMSG:
2551 err = sys_recvmmsg(a0, (struct mmsghdr __user *)a1, a[2], a[3],
2552 (struct timespec __user *)a[4]);
2553 break;
Ulrich Drepperde11def2008-11-19 15:36:14 -08002554 case SYS_ACCEPT4:
2555 err = sys_accept4(a0, (struct sockaddr __user *)a1,
2556 (int __user *)a[2], a[3]);
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07002557 break;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002558 default:
2559 err = -EINVAL;
2560 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 }
2562 return err;
2563}
2564
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002565#endif /* __ARCH_WANT_SYS_SOCKETCALL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002567/**
2568 * sock_register - add a socket protocol handler
2569 * @ops: description of protocol
2570 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 * This function is called by a protocol handler that wants to
2572 * advertise its address family, and have it linked into the
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002573 * socket interface. The value ops->family coresponds to the
2574 * socket system call protocol family.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 */
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002576int sock_register(const struct net_proto_family *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577{
2578 int err;
2579
2580 if (ops->family >= NPROTO) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002581 printk(KERN_CRIT "protocol %d >= NPROTO(%d)\n", ops->family,
2582 NPROTO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 return -ENOBUFS;
2584 }
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002585
2586 spin_lock(&net_family_lock);
Eric Dumazet190683a2010-11-10 10:50:44 +00002587 if (rcu_dereference_protected(net_families[ops->family],
2588 lockdep_is_held(&net_family_lock)))
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002589 err = -EEXIST;
2590 else {
Eric Dumazetcf778b02012-01-12 04:41:32 +00002591 rcu_assign_pointer(net_families[ops->family], ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 err = 0;
2593 }
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002594 spin_unlock(&net_family_lock);
2595
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002596 printk(KERN_INFO "NET: Registered protocol family %d\n", ops->family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 return err;
2598}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002599EXPORT_SYMBOL(sock_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002601/**
2602 * sock_unregister - remove a protocol handler
2603 * @family: protocol family to remove
2604 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 * This function is called by a protocol handler that wants to
2606 * remove its address family, and have it unlinked from the
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002607 * new socket creation.
2608 *
2609 * If protocol handler is a module, then it can use module reference
2610 * counts to protect against new references. If protocol handler is not
2611 * a module then it needs to provide its own protection in
2612 * the ops->create routine.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 */
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002614void sock_unregister(int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615{
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002616 BUG_ON(family < 0 || family >= NPROTO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002618 spin_lock(&net_family_lock);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00002619 RCU_INIT_POINTER(net_families[family], NULL);
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002620 spin_unlock(&net_family_lock);
2621
2622 synchronize_rcu();
2623
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002624 printk(KERN_INFO "NET: Unregistered protocol family %d\n", family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002626EXPORT_SYMBOL(sock_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627
Andi Kleen77d76ea2005-12-22 12:43:42 -08002628static int __init sock_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629{
Nick Pigginb3e19d92011-01-07 17:50:11 +11002630 int err;
Eric W. Biederman2ca794e2012-04-19 13:20:32 +00002631 /*
2632 * Initialize the network sysctl infrastructure.
2633 */
2634 err = net_sysctl_init();
2635 if (err)
2636 goto out;
Nick Pigginb3e19d92011-01-07 17:50:11 +11002637
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002639 * Initialize skbuff SLAB cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 */
2641 skb_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642
2643 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002644 * Initialize the protocols module.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 */
2646
2647 init_inodecache();
Nick Pigginb3e19d92011-01-07 17:50:11 +11002648
2649 err = register_filesystem(&sock_fs_type);
2650 if (err)
2651 goto out_fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 sock_mnt = kern_mount(&sock_fs_type);
Nick Pigginb3e19d92011-01-07 17:50:11 +11002653 if (IS_ERR(sock_mnt)) {
2654 err = PTR_ERR(sock_mnt);
2655 goto out_mount;
2656 }
Andi Kleen77d76ea2005-12-22 12:43:42 -08002657
2658 /* The real protocol initialization is performed in later initcalls.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 */
2660
2661#ifdef CONFIG_NETFILTER
2662 netfilter_init();
2663#endif
David S. Millercbeb3212005-12-22 12:58:55 -08002664
Richard Cochranc1f19b52010-07-17 08:49:36 +00002665#ifdef CONFIG_NETWORK_PHY_TIMESTAMPING
2666 skb_timestamping_init();
2667#endif
2668
Nick Pigginb3e19d92011-01-07 17:50:11 +11002669out:
2670 return err;
2671
2672out_mount:
2673 unregister_filesystem(&sock_fs_type);
2674out_fs:
2675 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676}
2677
Andi Kleen77d76ea2005-12-22 12:43:42 -08002678core_initcall(sock_init); /* early initcall */
2679
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680#ifdef CONFIG_PROC_FS
2681void socket_seq_show(struct seq_file *seq)
2682{
2683 int cpu;
2684 int counter = 0;
2685
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -07002686 for_each_possible_cpu(cpu)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002687 counter += per_cpu(sockets_in_use, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688
2689 /* It can be negative, by the way. 8) */
2690 if (counter < 0)
2691 counter = 0;
2692
2693 seq_printf(seq, "sockets: used %d\n", counter);
2694}
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002695#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696
Shaun Pereira89bbfc92006-03-21 23:58:08 -08002697#ifdef CONFIG_COMPAT
Arnd Bergmann6b960182009-11-06 23:10:54 -08002698static int do_siocgstamp(struct net *net, struct socket *sock,
H. Peter Anvin644595f2012-02-19 17:51:59 -08002699 unsigned int cmd, void __user *up)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002700{
Arnd Bergmann7a229382009-11-06 23:00:29 -08002701 mm_segment_t old_fs = get_fs();
2702 struct timeval ktv;
2703 int err;
2704
2705 set_fs(KERNEL_DS);
Arnd Bergmann6b960182009-11-06 23:10:54 -08002706 err = sock_do_ioctl(net, sock, cmd, (unsigned long)&ktv);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002707 set_fs(old_fs);
H. Peter Anvin644595f2012-02-19 17:51:59 -08002708 if (!err)
Mikulas Patockaed6fe9d2012-09-01 12:34:07 -04002709 err = compat_put_timeval(&ktv, up);
H. Peter Anvin644595f2012-02-19 17:51:59 -08002710
Arnd Bergmann7a229382009-11-06 23:00:29 -08002711 return err;
2712}
2713
Arnd Bergmann6b960182009-11-06 23:10:54 -08002714static int do_siocgstampns(struct net *net, struct socket *sock,
H. Peter Anvin644595f2012-02-19 17:51:59 -08002715 unsigned int cmd, void __user *up)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002716{
Arnd Bergmann7a229382009-11-06 23:00:29 -08002717 mm_segment_t old_fs = get_fs();
2718 struct timespec kts;
2719 int err;
2720
2721 set_fs(KERNEL_DS);
Arnd Bergmann6b960182009-11-06 23:10:54 -08002722 err = sock_do_ioctl(net, sock, cmd, (unsigned long)&kts);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002723 set_fs(old_fs);
H. Peter Anvin644595f2012-02-19 17:51:59 -08002724 if (!err)
Mikulas Patockaed6fe9d2012-09-01 12:34:07 -04002725 err = compat_put_timespec(&kts, up);
H. Peter Anvin644595f2012-02-19 17:51:59 -08002726
Arnd Bergmann7a229382009-11-06 23:00:29 -08002727 return err;
2728}
2729
Arnd Bergmann6b960182009-11-06 23:10:54 -08002730static int dev_ifname32(struct net *net, struct compat_ifreq __user *uifr32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002731{
2732 struct ifreq __user *uifr;
2733 int err;
2734
2735 uifr = compat_alloc_user_space(sizeof(struct ifreq));
Arnd Bergmann6b960182009-11-06 23:10:54 -08002736 if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002737 return -EFAULT;
2738
Arnd Bergmann6b960182009-11-06 23:10:54 -08002739 err = dev_ioctl(net, SIOCGIFNAME, uifr);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002740 if (err)
2741 return err;
2742
Arnd Bergmann6b960182009-11-06 23:10:54 -08002743 if (copy_in_user(uifr32, uifr, sizeof(struct compat_ifreq)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002744 return -EFAULT;
2745
2746 return 0;
2747}
2748
Arnd Bergmann6b960182009-11-06 23:10:54 -08002749static int dev_ifconf(struct net *net, struct compat_ifconf __user *uifc32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002750{
Arnd Bergmann6b960182009-11-06 23:10:54 -08002751 struct compat_ifconf ifc32;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002752 struct ifconf ifc;
2753 struct ifconf __user *uifc;
Arnd Bergmann6b960182009-11-06 23:10:54 -08002754 struct compat_ifreq __user *ifr32;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002755 struct ifreq __user *ifr;
2756 unsigned int i, j;
2757 int err;
2758
Arnd Bergmann6b960182009-11-06 23:10:54 -08002759 if (copy_from_user(&ifc32, uifc32, sizeof(struct compat_ifconf)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002760 return -EFAULT;
2761
Mathias Krause43da5f22012-08-15 11:31:57 +00002762 memset(&ifc, 0, sizeof(ifc));
Arnd Bergmann7a229382009-11-06 23:00:29 -08002763 if (ifc32.ifcbuf == 0) {
2764 ifc32.ifc_len = 0;
2765 ifc.ifc_len = 0;
2766 ifc.ifc_req = NULL;
2767 uifc = compat_alloc_user_space(sizeof(struct ifconf));
2768 } else {
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002769 size_t len = ((ifc32.ifc_len / sizeof(struct compat_ifreq)) + 1) *
2770 sizeof(struct ifreq);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002771 uifc = compat_alloc_user_space(sizeof(struct ifconf) + len);
2772 ifc.ifc_len = len;
2773 ifr = ifc.ifc_req = (void __user *)(uifc + 1);
2774 ifr32 = compat_ptr(ifc32.ifcbuf);
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002775 for (i = 0; i < ifc32.ifc_len; i += sizeof(struct compat_ifreq)) {
Arnd Bergmann6b960182009-11-06 23:10:54 -08002776 if (copy_in_user(ifr, ifr32, sizeof(struct compat_ifreq)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002777 return -EFAULT;
2778 ifr++;
2779 ifr32++;
2780 }
2781 }
2782 if (copy_to_user(uifc, &ifc, sizeof(struct ifconf)))
2783 return -EFAULT;
2784
Arnd Bergmann6b960182009-11-06 23:10:54 -08002785 err = dev_ioctl(net, SIOCGIFCONF, uifc);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002786 if (err)
2787 return err;
2788
2789 if (copy_from_user(&ifc, uifc, sizeof(struct ifconf)))
2790 return -EFAULT;
2791
2792 ifr = ifc.ifc_req;
2793 ifr32 = compat_ptr(ifc32.ifcbuf);
2794 for (i = 0, j = 0;
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002795 i + sizeof(struct compat_ifreq) <= ifc32.ifc_len && j < ifc.ifc_len;
2796 i += sizeof(struct compat_ifreq), j += sizeof(struct ifreq)) {
2797 if (copy_in_user(ifr32, ifr, sizeof(struct compat_ifreq)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002798 return -EFAULT;
2799 ifr32++;
2800 ifr++;
2801 }
2802
2803 if (ifc32.ifcbuf == 0) {
2804 /* Translate from 64-bit structure multiple to
2805 * a 32-bit one.
2806 */
2807 i = ifc.ifc_len;
Arnd Bergmann6b960182009-11-06 23:10:54 -08002808 i = ((i / sizeof(struct ifreq)) * sizeof(struct compat_ifreq));
Arnd Bergmann7a229382009-11-06 23:00:29 -08002809 ifc32.ifc_len = i;
2810 } else {
2811 ifc32.ifc_len = i;
2812 }
Arnd Bergmann6b960182009-11-06 23:10:54 -08002813 if (copy_to_user(uifc32, &ifc32, sizeof(struct compat_ifconf)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002814 return -EFAULT;
2815
2816 return 0;
2817}
2818
Arnd Bergmann6b960182009-11-06 23:10:54 -08002819static int ethtool_ioctl(struct net *net, struct compat_ifreq __user *ifr32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002820{
Ben Hutchings3a7da392011-03-17 07:34:32 +00002821 struct compat_ethtool_rxnfc __user *compat_rxnfc;
2822 bool convert_in = false, convert_out = false;
2823 size_t buf_size = ALIGN(sizeof(struct ifreq), 8);
2824 struct ethtool_rxnfc __user *rxnfc;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002825 struct ifreq __user *ifr;
Ben Hutchings3a7da392011-03-17 07:34:32 +00002826 u32 rule_cnt = 0, actual_rule_cnt;
2827 u32 ethcmd;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002828 u32 data;
Ben Hutchings3a7da392011-03-17 07:34:32 +00002829 int ret;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002830
2831 if (get_user(data, &ifr32->ifr_ifru.ifru_data))
2832 return -EFAULT;
2833
Ben Hutchings3a7da392011-03-17 07:34:32 +00002834 compat_rxnfc = compat_ptr(data);
2835
2836 if (get_user(ethcmd, &compat_rxnfc->cmd))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002837 return -EFAULT;
2838
Ben Hutchings3a7da392011-03-17 07:34:32 +00002839 /* Most ethtool structures are defined without padding.
2840 * Unfortunately struct ethtool_rxnfc is an exception.
2841 */
2842 switch (ethcmd) {
2843 default:
2844 break;
2845 case ETHTOOL_GRXCLSRLALL:
2846 /* Buffer size is variable */
2847 if (get_user(rule_cnt, &compat_rxnfc->rule_cnt))
2848 return -EFAULT;
2849 if (rule_cnt > KMALLOC_MAX_SIZE / sizeof(u32))
2850 return -ENOMEM;
2851 buf_size += rule_cnt * sizeof(u32);
2852 /* fall through */
2853 case ETHTOOL_GRXRINGS:
2854 case ETHTOOL_GRXCLSRLCNT:
2855 case ETHTOOL_GRXCLSRULE:
Ben Hutchings55664f32012-01-03 12:04:51 +00002856 case ETHTOOL_SRXCLSRLINS:
Ben Hutchings3a7da392011-03-17 07:34:32 +00002857 convert_out = true;
2858 /* fall through */
2859 case ETHTOOL_SRXCLSRLDEL:
Ben Hutchings3a7da392011-03-17 07:34:32 +00002860 buf_size += sizeof(struct ethtool_rxnfc);
2861 convert_in = true;
2862 break;
2863 }
2864
2865 ifr = compat_alloc_user_space(buf_size);
Stephen Hemminger954b1242013-02-11 06:22:28 +00002866 rxnfc = (void __user *)ifr + ALIGN(sizeof(struct ifreq), 8);
Ben Hutchings3a7da392011-03-17 07:34:32 +00002867
2868 if (copy_in_user(&ifr->ifr_name, &ifr32->ifr_name, IFNAMSIZ))
2869 return -EFAULT;
2870
2871 if (put_user(convert_in ? rxnfc : compat_ptr(data),
2872 &ifr->ifr_ifru.ifru_data))
2873 return -EFAULT;
2874
2875 if (convert_in) {
Alexander Duyck127fe532011-04-08 18:01:59 +00002876 /* We expect there to be holes between fs.m_ext and
Ben Hutchings3a7da392011-03-17 07:34:32 +00002877 * fs.ring_cookie and at the end of fs, but nowhere else.
2878 */
Alexander Duyck127fe532011-04-08 18:01:59 +00002879 BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc, fs.m_ext) +
2880 sizeof(compat_rxnfc->fs.m_ext) !=
2881 offsetof(struct ethtool_rxnfc, fs.m_ext) +
2882 sizeof(rxnfc->fs.m_ext));
Ben Hutchings3a7da392011-03-17 07:34:32 +00002883 BUILD_BUG_ON(
2884 offsetof(struct compat_ethtool_rxnfc, fs.location) -
2885 offsetof(struct compat_ethtool_rxnfc, fs.ring_cookie) !=
2886 offsetof(struct ethtool_rxnfc, fs.location) -
2887 offsetof(struct ethtool_rxnfc, fs.ring_cookie));
2888
2889 if (copy_in_user(rxnfc, compat_rxnfc,
Stephen Hemminger954b1242013-02-11 06:22:28 +00002890 (void __user *)(&rxnfc->fs.m_ext + 1) -
2891 (void __user *)rxnfc) ||
Ben Hutchings3a7da392011-03-17 07:34:32 +00002892 copy_in_user(&rxnfc->fs.ring_cookie,
2893 &compat_rxnfc->fs.ring_cookie,
Stephen Hemminger954b1242013-02-11 06:22:28 +00002894 (void __user *)(&rxnfc->fs.location + 1) -
2895 (void __user *)&rxnfc->fs.ring_cookie) ||
Ben Hutchings3a7da392011-03-17 07:34:32 +00002896 copy_in_user(&rxnfc->rule_cnt, &compat_rxnfc->rule_cnt,
2897 sizeof(rxnfc->rule_cnt)))
2898 return -EFAULT;
2899 }
2900
2901 ret = dev_ioctl(net, SIOCETHTOOL, ifr);
2902 if (ret)
2903 return ret;
2904
2905 if (convert_out) {
2906 if (copy_in_user(compat_rxnfc, rxnfc,
Stephen Hemminger954b1242013-02-11 06:22:28 +00002907 (const void __user *)(&rxnfc->fs.m_ext + 1) -
2908 (const void __user *)rxnfc) ||
Ben Hutchings3a7da392011-03-17 07:34:32 +00002909 copy_in_user(&compat_rxnfc->fs.ring_cookie,
2910 &rxnfc->fs.ring_cookie,
Stephen Hemminger954b1242013-02-11 06:22:28 +00002911 (const void __user *)(&rxnfc->fs.location + 1) -
2912 (const void __user *)&rxnfc->fs.ring_cookie) ||
Ben Hutchings3a7da392011-03-17 07:34:32 +00002913 copy_in_user(&compat_rxnfc->rule_cnt, &rxnfc->rule_cnt,
2914 sizeof(rxnfc->rule_cnt)))
2915 return -EFAULT;
2916
2917 if (ethcmd == ETHTOOL_GRXCLSRLALL) {
2918 /* As an optimisation, we only copy the actual
2919 * number of rules that the underlying
2920 * function returned. Since Mallory might
2921 * change the rule count in user memory, we
2922 * check that it is less than the rule count
2923 * originally given (as the user buffer size),
2924 * which has been range-checked.
2925 */
2926 if (get_user(actual_rule_cnt, &rxnfc->rule_cnt))
2927 return -EFAULT;
2928 if (actual_rule_cnt < rule_cnt)
2929 rule_cnt = actual_rule_cnt;
2930 if (copy_in_user(&compat_rxnfc->rule_locs[0],
2931 &rxnfc->rule_locs[0],
2932 rule_cnt * sizeof(u32)))
2933 return -EFAULT;
2934 }
2935 }
2936
2937 return 0;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002938}
2939
Arnd Bergmann7a50a242009-11-08 20:57:03 -08002940static int compat_siocwandev(struct net *net, struct compat_ifreq __user *uifr32)
2941{
2942 void __user *uptr;
2943 compat_uptr_t uptr32;
2944 struct ifreq __user *uifr;
2945
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002946 uifr = compat_alloc_user_space(sizeof(*uifr));
Arnd Bergmann7a50a242009-11-08 20:57:03 -08002947 if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
2948 return -EFAULT;
2949
2950 if (get_user(uptr32, &uifr32->ifr_settings.ifs_ifsu))
2951 return -EFAULT;
2952
2953 uptr = compat_ptr(uptr32);
2954
2955 if (put_user(uptr, &uifr->ifr_settings.ifs_ifsu.raw_hdlc))
2956 return -EFAULT;
2957
2958 return dev_ioctl(net, SIOCWANDEV, uifr);
2959}
2960
Arnd Bergmann6b960182009-11-06 23:10:54 -08002961static int bond_ioctl(struct net *net, unsigned int cmd,
2962 struct compat_ifreq __user *ifr32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002963{
2964 struct ifreq kifr;
2965 struct ifreq __user *uifr;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002966 mm_segment_t old_fs;
2967 int err;
2968 u32 data;
2969 void __user *datap;
2970
2971 switch (cmd) {
2972 case SIOCBONDENSLAVE:
2973 case SIOCBONDRELEASE:
2974 case SIOCBONDSETHWADDR:
2975 case SIOCBONDCHANGEACTIVE:
Arnd Bergmann6b960182009-11-06 23:10:54 -08002976 if (copy_from_user(&kifr, ifr32, sizeof(struct compat_ifreq)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002977 return -EFAULT;
2978
2979 old_fs = get_fs();
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002980 set_fs(KERNEL_DS);
stephen hemmingerc3f52ae2011-02-23 09:06:48 +00002981 err = dev_ioctl(net, cmd,
2982 (struct ifreq __user __force *) &kifr);
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002983 set_fs(old_fs);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002984
2985 return err;
2986 case SIOCBONDSLAVEINFOQUERY:
2987 case SIOCBONDINFOQUERY:
2988 uifr = compat_alloc_user_space(sizeof(*uifr));
2989 if (copy_in_user(&uifr->ifr_name, &ifr32->ifr_name, IFNAMSIZ))
2990 return -EFAULT;
2991
2992 if (get_user(data, &ifr32->ifr_ifru.ifru_data))
2993 return -EFAULT;
2994
2995 datap = compat_ptr(data);
2996 if (put_user(datap, &uifr->ifr_ifru.ifru_data))
2997 return -EFAULT;
2998
Arnd Bergmann6b960182009-11-06 23:10:54 -08002999 return dev_ioctl(net, cmd, uifr);
Arnd Bergmann7a229382009-11-06 23:00:29 -08003000 default:
Linus Torvalds07d106d2012-01-05 15:40:12 -08003001 return -ENOIOCTLCMD;
Joe Perchesccbd6a52010-05-14 10:58:26 +00003002 }
Arnd Bergmann7a229382009-11-06 23:00:29 -08003003}
3004
Arnd Bergmann6b960182009-11-06 23:10:54 -08003005static int siocdevprivate_ioctl(struct net *net, unsigned int cmd,
3006 struct compat_ifreq __user *u_ifreq32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08003007{
3008 struct ifreq __user *u_ifreq64;
Arnd Bergmann7a229382009-11-06 23:00:29 -08003009 char tmp_buf[IFNAMSIZ];
3010 void __user *data64;
3011 u32 data32;
3012
3013 if (copy_from_user(&tmp_buf[0], &(u_ifreq32->ifr_ifrn.ifrn_name[0]),
3014 IFNAMSIZ))
3015 return -EFAULT;
3016 if (__get_user(data32, &u_ifreq32->ifr_ifru.ifru_data))
3017 return -EFAULT;
3018 data64 = compat_ptr(data32);
3019
3020 u_ifreq64 = compat_alloc_user_space(sizeof(*u_ifreq64));
3021
3022 /* Don't check these user accesses, just let that get trapped
3023 * in the ioctl handler instead.
3024 */
3025 if (copy_to_user(&u_ifreq64->ifr_ifrn.ifrn_name[0], &tmp_buf[0],
3026 IFNAMSIZ))
3027 return -EFAULT;
3028 if (__put_user(data64, &u_ifreq64->ifr_ifru.ifru_data))
3029 return -EFAULT;
3030
Arnd Bergmann6b960182009-11-06 23:10:54 -08003031 return dev_ioctl(net, cmd, u_ifreq64);
Arnd Bergmann7a229382009-11-06 23:00:29 -08003032}
3033
Arnd Bergmann6b960182009-11-06 23:10:54 -08003034static int dev_ifsioc(struct net *net, struct socket *sock,
3035 unsigned int cmd, struct compat_ifreq __user *uifr32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08003036{
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003037 struct ifreq __user *uifr;
Arnd Bergmann7a229382009-11-06 23:00:29 -08003038 int err;
3039
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003040 uifr = compat_alloc_user_space(sizeof(*uifr));
3041 if (copy_in_user(uifr, uifr32, sizeof(*uifr32)))
3042 return -EFAULT;
3043
3044 err = sock_do_ioctl(net, sock, cmd, (unsigned long)uifr);
3045
Arnd Bergmann7a229382009-11-06 23:00:29 -08003046 if (!err) {
3047 switch (cmd) {
3048 case SIOCGIFFLAGS:
3049 case SIOCGIFMETRIC:
3050 case SIOCGIFMTU:
3051 case SIOCGIFMEM:
3052 case SIOCGIFHWADDR:
3053 case SIOCGIFINDEX:
3054 case SIOCGIFADDR:
3055 case SIOCGIFBRDADDR:
3056 case SIOCGIFDSTADDR:
3057 case SIOCGIFNETMASK:
Arnd Bergmannfab25322009-11-08 20:56:21 -08003058 case SIOCGIFPFLAGS:
Arnd Bergmann7a229382009-11-06 23:00:29 -08003059 case SIOCGIFTXQLEN:
Arnd Bergmannfab25322009-11-08 20:56:21 -08003060 case SIOCGMIIPHY:
3061 case SIOCGMIIREG:
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003062 if (copy_in_user(uifr32, uifr, sizeof(*uifr32)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08003063 err = -EFAULT;
3064 break;
3065 }
3066 }
3067 return err;
3068}
3069
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003070static int compat_sioc_ifmap(struct net *net, unsigned int cmd,
3071 struct compat_ifreq __user *uifr32)
3072{
3073 struct ifreq ifr;
3074 struct compat_ifmap __user *uifmap32;
3075 mm_segment_t old_fs;
3076 int err;
3077
3078 uifmap32 = &uifr32->ifr_ifru.ifru_map;
3079 err = copy_from_user(&ifr, uifr32, sizeof(ifr.ifr_name));
3080 err |= __get_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
3081 err |= __get_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
3082 err |= __get_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
3083 err |= __get_user(ifr.ifr_map.irq, &uifmap32->irq);
3084 err |= __get_user(ifr.ifr_map.dma, &uifmap32->dma);
3085 err |= __get_user(ifr.ifr_map.port, &uifmap32->port);
3086 if (err)
3087 return -EFAULT;
3088
3089 old_fs = get_fs();
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003090 set_fs(KERNEL_DS);
stephen hemmingerc3f52ae2011-02-23 09:06:48 +00003091 err = dev_ioctl(net, cmd, (void __user __force *)&ifr);
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003092 set_fs(old_fs);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003093
3094 if (cmd == SIOCGIFMAP && !err) {
3095 err = copy_to_user(uifr32, &ifr, sizeof(ifr.ifr_name));
3096 err |= __put_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
3097 err |= __put_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
3098 err |= __put_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
3099 err |= __put_user(ifr.ifr_map.irq, &uifmap32->irq);
3100 err |= __put_user(ifr.ifr_map.dma, &uifmap32->dma);
3101 err |= __put_user(ifr.ifr_map.port, &uifmap32->port);
3102 if (err)
3103 err = -EFAULT;
3104 }
3105 return err;
3106}
3107
3108static int compat_siocshwtstamp(struct net *net, struct compat_ifreq __user *uifr32)
3109{
3110 void __user *uptr;
3111 compat_uptr_t uptr32;
3112 struct ifreq __user *uifr;
3113
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003114 uifr = compat_alloc_user_space(sizeof(*uifr));
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003115 if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
3116 return -EFAULT;
3117
3118 if (get_user(uptr32, &uifr32->ifr_data))
3119 return -EFAULT;
3120
3121 uptr = compat_ptr(uptr32);
3122
3123 if (put_user(uptr, &uifr->ifr_data))
3124 return -EFAULT;
3125
3126 return dev_ioctl(net, SIOCSHWTSTAMP, uifr);
3127}
3128
Arnd Bergmann7a229382009-11-06 23:00:29 -08003129struct rtentry32 {
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003130 u32 rt_pad1;
Arnd Bergmann7a229382009-11-06 23:00:29 -08003131 struct sockaddr rt_dst; /* target address */
3132 struct sockaddr rt_gateway; /* gateway addr (RTF_GATEWAY) */
3133 struct sockaddr rt_genmask; /* target network mask (IP) */
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003134 unsigned short rt_flags;
3135 short rt_pad2;
3136 u32 rt_pad3;
3137 unsigned char rt_tos;
3138 unsigned char rt_class;
3139 short rt_pad4;
3140 short rt_metric; /* +1 for binary compatibility! */
Arnd Bergmann7a229382009-11-06 23:00:29 -08003141 /* char * */ u32 rt_dev; /* forcing the device at add */
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003142 u32 rt_mtu; /* per route MTU/Window */
3143 u32 rt_window; /* Window clamping */
Arnd Bergmann7a229382009-11-06 23:00:29 -08003144 unsigned short rt_irtt; /* Initial RTT */
3145};
3146
3147struct in6_rtmsg32 {
3148 struct in6_addr rtmsg_dst;
3149 struct in6_addr rtmsg_src;
3150 struct in6_addr rtmsg_gateway;
3151 u32 rtmsg_type;
3152 u16 rtmsg_dst_len;
3153 u16 rtmsg_src_len;
3154 u32 rtmsg_metric;
3155 u32 rtmsg_info;
3156 u32 rtmsg_flags;
3157 s32 rtmsg_ifindex;
3158};
3159
Arnd Bergmann6b960182009-11-06 23:10:54 -08003160static int routing_ioctl(struct net *net, struct socket *sock,
3161 unsigned int cmd, void __user *argp)
Arnd Bergmann7a229382009-11-06 23:00:29 -08003162{
3163 int ret;
3164 void *r = NULL;
3165 struct in6_rtmsg r6;
3166 struct rtentry r4;
3167 char devname[16];
3168 u32 rtdev;
3169 mm_segment_t old_fs = get_fs();
3170
Arnd Bergmann6b960182009-11-06 23:10:54 -08003171 if (sock && sock->sk && sock->sk->sk_family == AF_INET6) { /* ipv6 */
3172 struct in6_rtmsg32 __user *ur6 = argp;
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003173 ret = copy_from_user(&r6.rtmsg_dst, &(ur6->rtmsg_dst),
Arnd Bergmann7a229382009-11-06 23:00:29 -08003174 3 * sizeof(struct in6_addr));
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003175 ret |= __get_user(r6.rtmsg_type, &(ur6->rtmsg_type));
3176 ret |= __get_user(r6.rtmsg_dst_len, &(ur6->rtmsg_dst_len));
3177 ret |= __get_user(r6.rtmsg_src_len, &(ur6->rtmsg_src_len));
3178 ret |= __get_user(r6.rtmsg_metric, &(ur6->rtmsg_metric));
3179 ret |= __get_user(r6.rtmsg_info, &(ur6->rtmsg_info));
3180 ret |= __get_user(r6.rtmsg_flags, &(ur6->rtmsg_flags));
3181 ret |= __get_user(r6.rtmsg_ifindex, &(ur6->rtmsg_ifindex));
Arnd Bergmann7a229382009-11-06 23:00:29 -08003182
3183 r = (void *) &r6;
3184 } else { /* ipv4 */
Arnd Bergmann6b960182009-11-06 23:10:54 -08003185 struct rtentry32 __user *ur4 = argp;
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003186 ret = copy_from_user(&r4.rt_dst, &(ur4->rt_dst),
Arnd Bergmann7a229382009-11-06 23:00:29 -08003187 3 * sizeof(struct sockaddr));
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003188 ret |= __get_user(r4.rt_flags, &(ur4->rt_flags));
3189 ret |= __get_user(r4.rt_metric, &(ur4->rt_metric));
3190 ret |= __get_user(r4.rt_mtu, &(ur4->rt_mtu));
3191 ret |= __get_user(r4.rt_window, &(ur4->rt_window));
3192 ret |= __get_user(r4.rt_irtt, &(ur4->rt_irtt));
3193 ret |= __get_user(rtdev, &(ur4->rt_dev));
Arnd Bergmann7a229382009-11-06 23:00:29 -08003194 if (rtdev) {
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003195 ret |= copy_from_user(devname, compat_ptr(rtdev), 15);
stephen hemmingerc3f52ae2011-02-23 09:06:48 +00003196 r4.rt_dev = (char __user __force *)devname;
3197 devname[15] = 0;
Arnd Bergmann7a229382009-11-06 23:00:29 -08003198 } else
3199 r4.rt_dev = NULL;
3200
3201 r = (void *) &r4;
3202 }
3203
3204 if (ret) {
3205 ret = -EFAULT;
3206 goto out;
3207 }
3208
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003209 set_fs(KERNEL_DS);
Arnd Bergmann6b960182009-11-06 23:10:54 -08003210 ret = sock_do_ioctl(net, sock, cmd, (unsigned long) r);
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003211 set_fs(old_fs);
Arnd Bergmann7a229382009-11-06 23:00:29 -08003212
3213out:
Arnd Bergmann7a229382009-11-06 23:00:29 -08003214 return ret;
3215}
3216
3217/* Since old style bridge ioctl's endup using SIOCDEVPRIVATE
3218 * for some operations; this forces use of the newer bridge-utils that
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003219 * use compatible ioctls
Arnd Bergmann7a229382009-11-06 23:00:29 -08003220 */
Arnd Bergmann6b960182009-11-06 23:10:54 -08003221static int old_bridge_ioctl(compat_ulong_t __user *argp)
Arnd Bergmann7a229382009-11-06 23:00:29 -08003222{
Arnd Bergmann6b960182009-11-06 23:10:54 -08003223 compat_ulong_t tmp;
Arnd Bergmann7a229382009-11-06 23:00:29 -08003224
Arnd Bergmann6b960182009-11-06 23:10:54 -08003225 if (get_user(tmp, argp))
Arnd Bergmann7a229382009-11-06 23:00:29 -08003226 return -EFAULT;
3227 if (tmp == BRCTL_GET_VERSION)
3228 return BRCTL_VERSION + 1;
3229 return -EINVAL;
3230}
3231
Arnd Bergmann6b960182009-11-06 23:10:54 -08003232static int compat_sock_ioctl_trans(struct file *file, struct socket *sock,
3233 unsigned int cmd, unsigned long arg)
3234{
3235 void __user *argp = compat_ptr(arg);
3236 struct sock *sk = sock->sk;
3237 struct net *net = sock_net(sk);
Arnd Bergmann7a229382009-11-06 23:00:29 -08003238
Arnd Bergmann6b960182009-11-06 23:10:54 -08003239 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15))
3240 return siocdevprivate_ioctl(net, cmd, argp);
Arnd Bergmann7a229382009-11-06 23:00:29 -08003241
Arnd Bergmann6b960182009-11-06 23:10:54 -08003242 switch (cmd) {
3243 case SIOCSIFBR:
3244 case SIOCGIFBR:
3245 return old_bridge_ioctl(argp);
3246 case SIOCGIFNAME:
3247 return dev_ifname32(net, argp);
3248 case SIOCGIFCONF:
3249 return dev_ifconf(net, argp);
3250 case SIOCETHTOOL:
3251 return ethtool_ioctl(net, argp);
Arnd Bergmann7a50a242009-11-08 20:57:03 -08003252 case SIOCWANDEV:
3253 return compat_siocwandev(net, argp);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003254 case SIOCGIFMAP:
3255 case SIOCSIFMAP:
3256 return compat_sioc_ifmap(net, cmd, argp);
Arnd Bergmann6b960182009-11-06 23:10:54 -08003257 case SIOCBONDENSLAVE:
3258 case SIOCBONDRELEASE:
3259 case SIOCBONDSETHWADDR:
3260 case SIOCBONDSLAVEINFOQUERY:
3261 case SIOCBONDINFOQUERY:
3262 case SIOCBONDCHANGEACTIVE:
3263 return bond_ioctl(net, cmd, argp);
3264 case SIOCADDRT:
3265 case SIOCDELRT:
3266 return routing_ioctl(net, sock, cmd, argp);
3267 case SIOCGSTAMP:
3268 return do_siocgstamp(net, sock, cmd, argp);
3269 case SIOCGSTAMPNS:
3270 return do_siocgstampns(net, sock, cmd, argp);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003271 case SIOCSHWTSTAMP:
3272 return compat_siocshwtstamp(net, argp);
Arnd Bergmann7a229382009-11-06 23:00:29 -08003273
Arnd Bergmann6b960182009-11-06 23:10:54 -08003274 case FIOSETOWN:
3275 case SIOCSPGRP:
3276 case FIOGETOWN:
3277 case SIOCGPGRP:
3278 case SIOCBRADDBR:
3279 case SIOCBRDELBR:
3280 case SIOCGIFVLAN:
3281 case SIOCSIFVLAN:
3282 case SIOCADDDLCI:
3283 case SIOCDELDLCI:
3284 return sock_ioctl(file, cmd, arg);
3285
3286 case SIOCGIFFLAGS:
3287 case SIOCSIFFLAGS:
3288 case SIOCGIFMETRIC:
3289 case SIOCSIFMETRIC:
3290 case SIOCGIFMTU:
3291 case SIOCSIFMTU:
3292 case SIOCGIFMEM:
3293 case SIOCSIFMEM:
3294 case SIOCGIFHWADDR:
3295 case SIOCSIFHWADDR:
3296 case SIOCADDMULTI:
3297 case SIOCDELMULTI:
3298 case SIOCGIFINDEX:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003299 case SIOCGIFADDR:
3300 case SIOCSIFADDR:
3301 case SIOCSIFHWBROADCAST:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003302 case SIOCDIFADDR:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003303 case SIOCGIFBRDADDR:
3304 case SIOCSIFBRDADDR:
3305 case SIOCGIFDSTADDR:
3306 case SIOCSIFDSTADDR:
3307 case SIOCGIFNETMASK:
3308 case SIOCSIFNETMASK:
3309 case SIOCSIFPFLAGS:
3310 case SIOCGIFPFLAGS:
3311 case SIOCGIFTXQLEN:
3312 case SIOCSIFTXQLEN:
3313 case SIOCBRADDIF:
3314 case SIOCBRDELIF:
Arnd Bergmann9177efd2009-11-06 08:09:09 +00003315 case SIOCSIFNAME:
3316 case SIOCGMIIPHY:
3317 case SIOCGMIIREG:
3318 case SIOCSMIIREG:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003319 return dev_ifsioc(net, sock, cmd, argp);
Arnd Bergmann9177efd2009-11-06 08:09:09 +00003320
Arnd Bergmann6b960182009-11-06 23:10:54 -08003321 case SIOCSARP:
3322 case SIOCGARP:
3323 case SIOCDARP:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003324 case SIOCATMARK:
Arnd Bergmann9177efd2009-11-06 08:09:09 +00003325 return sock_do_ioctl(net, sock, cmd, arg);
3326 }
3327
Arnd Bergmann6b960182009-11-06 23:10:54 -08003328 return -ENOIOCTLCMD;
3329}
Arnd Bergmann7a229382009-11-06 23:00:29 -08003330
Eric Dumazet95c96172012-04-15 05:58:06 +00003331static long compat_sock_ioctl(struct file *file, unsigned int cmd,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07003332 unsigned long arg)
Shaun Pereira89bbfc92006-03-21 23:58:08 -08003333{
3334 struct socket *sock = file->private_data;
3335 int ret = -ENOIOCTLCMD;
David S. Miller87de87d2008-06-03 09:14:03 -07003336 struct sock *sk;
3337 struct net *net;
3338
3339 sk = sock->sk;
3340 net = sock_net(sk);
Shaun Pereira89bbfc92006-03-21 23:58:08 -08003341
3342 if (sock->ops->compat_ioctl)
3343 ret = sock->ops->compat_ioctl(sock, cmd, arg);
3344
David S. Miller87de87d2008-06-03 09:14:03 -07003345 if (ret == -ENOIOCTLCMD &&
3346 (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST))
3347 ret = compat_wext_handle_ioctl(net, cmd, arg);
3348
Arnd Bergmann6b960182009-11-06 23:10:54 -08003349 if (ret == -ENOIOCTLCMD)
3350 ret = compat_sock_ioctl_trans(file, sock, cmd, arg);
3351
Shaun Pereira89bbfc92006-03-21 23:58:08 -08003352 return ret;
3353}
3354#endif
3355
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003356int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen)
3357{
3358 return sock->ops->bind(sock, addr, addrlen);
3359}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003360EXPORT_SYMBOL(kernel_bind);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003361
3362int kernel_listen(struct socket *sock, int backlog)
3363{
3364 return sock->ops->listen(sock, backlog);
3365}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003366EXPORT_SYMBOL(kernel_listen);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003367
3368int kernel_accept(struct socket *sock, struct socket **newsock, int flags)
3369{
3370 struct sock *sk = sock->sk;
3371 int err;
3372
3373 err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
3374 newsock);
3375 if (err < 0)
3376 goto done;
3377
3378 err = sock->ops->accept(sock, *newsock, flags);
3379 if (err < 0) {
3380 sock_release(*newsock);
Tony Battersbyfa8705b2007-10-10 21:09:04 -07003381 *newsock = NULL;
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003382 goto done;
3383 }
3384
3385 (*newsock)->ops = sock->ops;
Wei Yongjun1b085342008-12-18 19:35:10 -08003386 __module_get((*newsock)->ops->owner);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003387
3388done:
3389 return err;
3390}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003391EXPORT_SYMBOL(kernel_accept);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003392
3393int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
YOSHIFUJI Hideaki4768fbc2007-02-09 23:25:31 +09003394 int flags)
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003395{
3396 return sock->ops->connect(sock, addr, addrlen, flags);
3397}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003398EXPORT_SYMBOL(kernel_connect);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003399
3400int kernel_getsockname(struct socket *sock, struct sockaddr *addr,
3401 int *addrlen)
3402{
3403 return sock->ops->getname(sock, addr, addrlen, 0);
3404}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003405EXPORT_SYMBOL(kernel_getsockname);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003406
3407int kernel_getpeername(struct socket *sock, struct sockaddr *addr,
3408 int *addrlen)
3409{
3410 return sock->ops->getname(sock, addr, addrlen, 1);
3411}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003412EXPORT_SYMBOL(kernel_getpeername);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003413
3414int kernel_getsockopt(struct socket *sock, int level, int optname,
3415 char *optval, int *optlen)
3416{
3417 mm_segment_t oldfs = get_fs();
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003418 char __user *uoptval;
3419 int __user *uoptlen;
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003420 int err;
3421
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003422 uoptval = (char __user __force *) optval;
3423 uoptlen = (int __user __force *) optlen;
3424
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003425 set_fs(KERNEL_DS);
3426 if (level == SOL_SOCKET)
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003427 err = sock_getsockopt(sock, level, optname, uoptval, uoptlen);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003428 else
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003429 err = sock->ops->getsockopt(sock, level, optname, uoptval,
3430 uoptlen);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003431 set_fs(oldfs);
3432 return err;
3433}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003434EXPORT_SYMBOL(kernel_getsockopt);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003435
3436int kernel_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07003437 char *optval, unsigned int optlen)
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003438{
3439 mm_segment_t oldfs = get_fs();
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003440 char __user *uoptval;
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003441 int err;
3442
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003443 uoptval = (char __user __force *) optval;
3444
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003445 set_fs(KERNEL_DS);
3446 if (level == SOL_SOCKET)
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003447 err = sock_setsockopt(sock, level, optname, uoptval, optlen);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003448 else
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003449 err = sock->ops->setsockopt(sock, level, optname, uoptval,
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003450 optlen);
3451 set_fs(oldfs);
3452 return err;
3453}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003454EXPORT_SYMBOL(kernel_setsockopt);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003455
3456int kernel_sendpage(struct socket *sock, struct page *page, int offset,
3457 size_t size, int flags)
3458{
3459 if (sock->ops->sendpage)
3460 return sock->ops->sendpage(sock, page, offset, size, flags);
3461
3462 return sock_no_sendpage(sock, page, offset, size, flags);
3463}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003464EXPORT_SYMBOL(kernel_sendpage);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003465
3466int kernel_sock_ioctl(struct socket *sock, int cmd, unsigned long arg)
3467{
3468 mm_segment_t oldfs = get_fs();
3469 int err;
3470
3471 set_fs(KERNEL_DS);
3472 err = sock->ops->ioctl(sock, cmd, arg);
3473 set_fs(oldfs);
3474
3475 return err;
3476}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003477EXPORT_SYMBOL(kernel_sock_ioctl);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003478
Trond Myklebust91cf45f2007-11-12 18:10:39 -08003479int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how)
3480{
3481 return sock->ops->shutdown(sock, how);
3482}
Trond Myklebust91cf45f2007-11-12 18:10:39 -08003483EXPORT_SYMBOL(kernel_sock_shutdown);