blob: cc3fc4d4263e64441d6a22d4496e1174b4e15e12 [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/wanrouter.h>
73#include <linux/if_bridge.h>
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -030074#include <linux/if_frad.h>
75#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#include <linux/init.h>
77#include <linux/poll.h>
78#include <linux/cache.h>
79#include <linux/module.h>
80#include <linux/highmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#include <linux/mount.h>
82#include <linux/security.h>
83#include <linux/syscalls.h>
84#include <linux/compat.h>
85#include <linux/kmod.h>
David Woodhouse3ec3b2f2005-05-17 12:08:48 +010086#include <linux/audit.h>
Adrian Bunkd86b5e02006-01-21 00:46:55 +010087#include <linux/wireless.h>
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -070088#include <linux/nsproxy.h>
Nick Black1fd73172009-09-22 16:43:33 -070089#include <linux/magic.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090090#include <linux/slab.h>
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 Sowaea9d7dc2013-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 Sowaea9d7dc2013-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
Al Viro7cbe66b2009-08-05 19:59:08 +0400350static int sock_alloc_file(struct socket *sock, struct file **f, int flags)
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 int fd;
David S. Miller39d8c1b2006-03-20 17:13:49 -0800356
Ulrich Dreppera677a032008-07-23 21:29:17 -0700357 fd = get_unused_fd_flags(flags);
Al Viro7cbe66b2009-08-05 19:59:08 +0400358 if (unlikely(fd < 0))
359 return fd;
David S. Miller39d8c1b2006-03-20 17:13:49 -0800360
Nick Piggin4b936882011-01-07 17:50:07 +1100361 path.dentry = d_alloc_pseudo(sock_mnt->mnt_sb, &name);
Al Viro2c48b9c2009-08-09 00:52:35 +0400362 if (unlikely(!path.dentry)) {
Al Viro7cbe66b2009-08-05 19:59:08 +0400363 put_unused_fd(fd);
David S. Miller39d8c1b2006-03-20 17:13:49 -0800364 return -ENOMEM;
Al Viro7cbe66b2009-08-05 19:59:08 +0400365 }
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);
373 if (unlikely(!file)) {
374 /* drop dentry, keep inode */
Al Viro7de9c6e2010-10-23 11:11:40 -0400375 ihold(path.dentry->d_inode);
Al Viro2c48b9c2009-08-09 00:52:35 +0400376 path_put(&path);
Al Virocc3808f2009-08-06 09:43:59 +0400377 put_unused_fd(fd);
378 return -ENFILE;
379 }
David S. Miller39d8c1b2006-03-20 17:13:49 -0800380
381 sock->file = file;
Ulrich Drepper77d27202008-07-23 21:29:35 -0700382 file->f_flags = O_RDWR | (flags & O_NONBLOCK);
David S. Miller39d8c1b2006-03-20 17:13:49 -0800383 file->f_pos = 0;
384 file->private_data = sock;
385
Al Viro7cbe66b2009-08-05 19:59:08 +0400386 *f = file;
387 return fd;
David S. Miller39d8c1b2006-03-20 17:13:49 -0800388}
389
Ulrich Dreppera677a032008-07-23 21:29:17 -0700390int sock_map_fd(struct socket *sock, int flags)
David S. Miller39d8c1b2006-03-20 17:13:49 -0800391{
392 struct file *newfile;
Al Viro7cbe66b2009-08-05 19:59:08 +0400393 int fd = sock_alloc_file(sock, &newfile, flags);
David S. Miller39d8c1b2006-03-20 17:13:49 -0800394
Al Viro7cbe66b2009-08-05 19:59:08 +0400395 if (likely(fd >= 0))
David S. Miller39d8c1b2006-03-20 17:13:49 -0800396 fd_install(fd, newfile);
Al Viro7cbe66b2009-08-05 19:59:08 +0400397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 return fd;
399}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700400EXPORT_SYMBOL(sock_map_fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800402static struct socket *sock_from_file(struct file *file, int *err)
403{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800404 if (file->f_op == &socket_file_ops)
405 return file->private_data; /* set in sock_map_fd */
406
Eric Dumazet23bb80d2007-02-08 14:59:57 -0800407 *err = -ENOTSOCK;
408 return NULL;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800409}
410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411/**
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700412 * sockfd_lookup - Go from a file number to its socket slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 * @fd: file handle
414 * @err: pointer to an error code return
415 *
416 * The file handle passed in is locked and the socket it is bound
417 * too is returned. If an error occurs the err pointer is overwritten
418 * with a negative errno code and NULL is returned. The function checks
419 * for both invalid handles and passing a handle which is not a socket.
420 *
421 * On a success the socket object pointer is returned.
422 */
423
424struct socket *sockfd_lookup(int fd, int *err)
425{
426 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 struct socket *sock;
428
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700429 file = fget(fd);
430 if (!file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 *err = -EBADF;
432 return NULL;
433 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700434
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800435 sock = sock_from_file(file, err);
436 if (!sock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 fput(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 return sock;
439}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700440EXPORT_SYMBOL(sockfd_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800442static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed)
443{
444 struct file *file;
445 struct socket *sock;
446
Hua Zhong36725582006-04-19 15:25:02 -0700447 *err = -EBADF;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800448 file = fget_light(fd, fput_needed);
449 if (file) {
450 sock = sock_from_file(file, err);
451 if (sock)
452 return sock;
453 fput_light(file, *fput_needed);
454 }
455 return NULL;
456}
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458/**
459 * sock_alloc - allocate a socket
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700460 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 * Allocate a new inode and socket object. The two are bound together
462 * and initialised. The socket is then returned. If we are out of inodes
463 * NULL is returned.
464 */
465
466static struct socket *sock_alloc(void)
467{
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700468 struct inode *inode;
469 struct socket *sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Eric Dumazeta209dfc2011-07-26 11:36:34 +0200471 inode = new_inode_pseudo(sock_mnt->mnt_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 if (!inode)
473 return NULL;
474
475 sock = SOCKET_I(inode);
476
Eric Dumazet29a020d2009-09-15 02:39:20 -0700477 kmemcheck_annotate_bitfield(sock, type);
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400478 inode->i_ino = get_next_ino();
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700479 inode->i_mode = S_IFSOCK | S_IRWXUGO;
David Howells8192b0c2008-11-14 10:39:10 +1100480 inode->i_uid = current_fsuid();
481 inode->i_gid = current_fsgid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Eric Dumazet4e694892009-04-04 16:41:09 -0700483 percpu_add(sockets_in_use, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 return sock;
485}
486
487/*
488 * In theory you can't get an open on this inode, but /proc provides
489 * a back door. Remember to keep it shut otherwise you'll let the
490 * creepy crawlies in.
491 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493static int sock_no_open(struct inode *irrelevant, struct file *dontcare)
494{
495 return -ENXIO;
496}
497
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800498const struct file_operations bad_sock_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 .owner = THIS_MODULE,
500 .open = sock_no_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200501 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502};
503
504/**
505 * sock_release - close a socket
506 * @sock: socket to close
507 *
508 * The socket is released from the protocol stack if it has a release
509 * callback, and the inode is then released if the socket is bound to
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700510 * an inode not a file.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513void sock_release(struct socket *sock)
514{
515 if (sock->ops) {
516 struct module *owner = sock->ops->owner;
517
518 sock->ops->release(sock);
519 sock->ops = NULL;
520 module_put(owner);
521 }
522
Eric Dumazeteaefd112011-02-18 03:26:36 +0000523 if (rcu_dereference_protected(sock->wq, 1)->fasync_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 printk(KERN_ERR "sock_release: fasync list not empty!\n");
525
Mikulas Patocka20855fe2012-07-19 06:13:36 +0000526 if (test_bit(SOCK_EXTERNALLY_ALLOCATED, &sock->flags))
527 return;
528
Eric Dumazet4e694892009-04-04 16:41:09 -0700529 percpu_sub(sockets_in_use, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 if (!sock->file) {
531 iput(SOCK_INODE(sock));
532 return;
533 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700534 sock->file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700536EXPORT_SYMBOL(sock_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Oliver Hartkopp2244d072010-08-17 08:59:14 +0000538int sock_tx_timestamp(struct sock *sk, __u8 *tx_flags)
Patrick Ohly20d49472009-02-12 05:03:38 +0000539{
Oliver Hartkopp2244d072010-08-17 08:59:14 +0000540 *tx_flags = 0;
Patrick Ohly20d49472009-02-12 05:03:38 +0000541 if (sock_flag(sk, SOCK_TIMESTAMPING_TX_HARDWARE))
Oliver Hartkopp2244d072010-08-17 08:59:14 +0000542 *tx_flags |= SKBTX_HW_TSTAMP;
Patrick Ohly20d49472009-02-12 05:03:38 +0000543 if (sock_flag(sk, SOCK_TIMESTAMPING_TX_SOFTWARE))
Oliver Hartkopp2244d072010-08-17 08:59:14 +0000544 *tx_flags |= SKBTX_SW_TSTAMP;
Johannes Berg6e3e9392011-11-09 10:15:42 +0100545 if (sock_flag(sk, SOCK_WIFI_STATUS))
546 *tx_flags |= SKBTX_WIFI_STATUS;
Patrick Ohly20d49472009-02-12 05:03:38 +0000547 return 0;
548}
549EXPORT_SYMBOL(sock_tx_timestamp);
550
Anton Blanchard228e5482011-05-02 20:21:35 +0000551static inline int __sock_sendmsg_nosec(struct kiocb *iocb, struct socket *sock,
552 struct msghdr *msg, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
554 struct sock_iocb *si = kiocb_to_siocb(iocb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Herbert Xuf8451722010-05-24 00:12:34 -0700556 sock_update_classid(sock->sk);
557
Neil Horman5bc14212011-11-22 05:10:51 +0000558 sock_update_netprioidx(sock->sk);
559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 si->sock = sock;
561 si->scm = NULL;
562 si->msg = msg;
563 si->size = size;
564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 return sock->ops->sendmsg(iocb, sock, msg, size);
566}
567
Anton Blanchard228e5482011-05-02 20:21:35 +0000568static inline int __sock_sendmsg(struct kiocb *iocb, struct socket *sock,
569 struct msghdr *msg, size_t size)
570{
571 int err = security_socket_sendmsg(sock, msg, size);
572
573 return err ?: __sock_sendmsg_nosec(iocb, sock, msg, size);
574}
575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576int sock_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
577{
578 struct kiocb iocb;
579 struct sock_iocb siocb;
580 int ret;
581
582 init_sync_kiocb(&iocb, NULL);
583 iocb.private = &siocb;
584 ret = __sock_sendmsg(&iocb, sock, msg, size);
585 if (-EIOCBQUEUED == ret)
586 ret = wait_on_sync_kiocb(&iocb);
587 return ret;
588}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700589EXPORT_SYMBOL(sock_sendmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Eric Dumazet894dc242011-07-26 02:39:41 +0000591static int sock_sendmsg_nosec(struct socket *sock, struct msghdr *msg, size_t size)
Anton Blanchard228e5482011-05-02 20:21:35 +0000592{
593 struct kiocb iocb;
594 struct sock_iocb siocb;
595 int ret;
596
597 init_sync_kiocb(&iocb, NULL);
598 iocb.private = &siocb;
599 ret = __sock_sendmsg_nosec(&iocb, sock, msg, size);
600 if (-EIOCBQUEUED == ret)
601 ret = wait_on_sync_kiocb(&iocb);
602 return ret;
603}
604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
606 struct kvec *vec, size_t num, size_t size)
607{
608 mm_segment_t oldfs = get_fs();
609 int result;
610
611 set_fs(KERNEL_DS);
612 /*
613 * the following is safe, since for compiler definitions of kvec and
614 * iovec are identical, yielding the same in-core layout and alignment
615 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700616 msg->msg_iov = (struct iovec *)vec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 msg->msg_iovlen = num;
618 result = sock_sendmsg(sock, msg, size);
619 set_fs(oldfs);
620 return result;
621}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700622EXPORT_SYMBOL(kernel_sendmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Patrick Ohly20d49472009-02-12 05:03:38 +0000624static int ktime2ts(ktime_t kt, struct timespec *ts)
625{
626 if (kt.tv64) {
627 *ts = ktime_to_timespec(kt);
628 return 1;
629 } else {
630 return 0;
631 }
632}
633
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700634/*
635 * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
636 */
637void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
638 struct sk_buff *skb)
639{
Patrick Ohly20d49472009-02-12 05:03:38 +0000640 int need_software_tstamp = sock_flag(sk, SOCK_RCVTSTAMP);
641 struct timespec ts[3];
642 int empty = 1;
643 struct skb_shared_hwtstamps *shhwtstamps =
644 skb_hwtstamps(skb);
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700645
Patrick Ohly20d49472009-02-12 05:03:38 +0000646 /* Race occurred between timestamp enabling and packet
647 receiving. Fill in the current time for now. */
648 if (need_software_tstamp && skb->tstamp.tv64 == 0)
649 __net_timestamp(skb);
650
651 if (need_software_tstamp) {
652 if (!sock_flag(sk, SOCK_RCVTSTAMPNS)) {
653 struct timeval tv;
654 skb_get_timestamp(skb, &tv);
655 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMP,
656 sizeof(tv), &tv);
657 } else {
Hagen Paul Pfeifer842509b2010-04-06 05:39:52 +0000658 skb_get_timestampns(skb, &ts[0]);
Patrick Ohly20d49472009-02-12 05:03:38 +0000659 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPNS,
Hagen Paul Pfeifer842509b2010-04-06 05:39:52 +0000660 sizeof(ts[0]), &ts[0]);
Patrick Ohly20d49472009-02-12 05:03:38 +0000661 }
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700662 }
Patrick Ohly20d49472009-02-12 05:03:38 +0000663
664
665 memset(ts, 0, sizeof(ts));
666 if (skb->tstamp.tv64 &&
667 sock_flag(sk, SOCK_TIMESTAMPING_SOFTWARE)) {
668 skb_get_timestampns(skb, ts + 0);
669 empty = 0;
670 }
671 if (shhwtstamps) {
672 if (sock_flag(sk, SOCK_TIMESTAMPING_SYS_HARDWARE) &&
673 ktime2ts(shhwtstamps->syststamp, ts + 1))
674 empty = 0;
675 if (sock_flag(sk, SOCK_TIMESTAMPING_RAW_HARDWARE) &&
676 ktime2ts(shhwtstamps->hwtstamp, ts + 2))
677 empty = 0;
678 }
679 if (!empty)
680 put_cmsg(msg, SOL_SOCKET,
681 SCM_TIMESTAMPING, sizeof(ts), &ts);
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700682}
Arnaldo Carvalho de Melo7c81fd82007-03-10 00:39:35 -0300683EXPORT_SYMBOL_GPL(__sock_recv_timestamp);
684
Johannes Berg6e3e9392011-11-09 10:15:42 +0100685void __sock_recv_wifi_status(struct msghdr *msg, struct sock *sk,
686 struct sk_buff *skb)
687{
688 int ack;
689
690 if (!sock_flag(sk, SOCK_WIFI_STATUS))
691 return;
692 if (!skb->wifi_acked_valid)
693 return;
694
695 ack = skb->wifi_acked;
696
697 put_cmsg(msg, SOL_SOCKET, SCM_WIFI_STATUS, sizeof(ack), &ack);
698}
699EXPORT_SYMBOL_GPL(__sock_recv_wifi_status);
700
stephen hemminger11165f12010-10-18 14:27:29 +0000701static inline void sock_recv_drops(struct msghdr *msg, struct sock *sk,
702 struct sk_buff *skb)
Neil Horman3b885782009-10-12 13:26:31 -0700703{
704 if (sock_flag(sk, SOCK_RXQ_OVFL) && skb && skb->dropcount)
705 put_cmsg(msg, SOL_SOCKET, SO_RXQ_OVFL,
706 sizeof(__u32), &skb->dropcount);
707}
708
Eric Dumazet767dd032010-04-28 19:14:43 +0000709void __sock_recv_ts_and_drops(struct msghdr *msg, struct sock *sk,
Neil Horman3b885782009-10-12 13:26:31 -0700710 struct sk_buff *skb)
711{
712 sock_recv_timestamp(msg, sk, skb);
713 sock_recv_drops(msg, sk, skb);
714}
Eric Dumazet767dd032010-04-28 19:14:43 +0000715EXPORT_SYMBOL_GPL(__sock_recv_ts_and_drops);
Neil Horman3b885782009-10-12 13:26:31 -0700716
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -0700717static inline int __sock_recvmsg_nosec(struct kiocb *iocb, struct socket *sock,
718 struct msghdr *msg, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 struct sock_iocb *si = kiocb_to_siocb(iocb);
721
Herbert Xuf8451722010-05-24 00:12:34 -0700722 sock_update_classid(sock->sk);
723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 si->sock = sock;
725 si->scm = NULL;
726 si->msg = msg;
727 si->size = size;
728 si->flags = flags;
729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 return sock->ops->recvmsg(iocb, sock, msg, size, flags);
731}
732
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -0700733static inline int __sock_recvmsg(struct kiocb *iocb, struct socket *sock,
734 struct msghdr *msg, size_t size, int flags)
735{
736 int err = security_socket_recvmsg(sock, msg, size, flags);
737
738 return err ?: __sock_recvmsg_nosec(iocb, sock, msg, size, flags);
739}
740
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700741int sock_recvmsg(struct socket *sock, struct msghdr *msg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 size_t size, int flags)
743{
744 struct kiocb iocb;
745 struct sock_iocb siocb;
746 int ret;
747
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700748 init_sync_kiocb(&iocb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 iocb.private = &siocb;
750 ret = __sock_recvmsg(&iocb, sock, msg, size, flags);
751 if (-EIOCBQUEUED == ret)
752 ret = wait_on_sync_kiocb(&iocb);
753 return ret;
754}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700755EXPORT_SYMBOL(sock_recvmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -0700757static int sock_recvmsg_nosec(struct socket *sock, struct msghdr *msg,
758 size_t size, int flags)
759{
760 struct kiocb iocb;
761 struct sock_iocb siocb;
762 int ret;
763
764 init_sync_kiocb(&iocb, NULL);
765 iocb.private = &siocb;
766 ret = __sock_recvmsg_nosec(&iocb, sock, msg, size, flags);
767 if (-EIOCBQUEUED == ret)
768 ret = wait_on_sync_kiocb(&iocb);
769 return ret;
770}
771
Martin Lucinac1249c02010-12-10 00:04:05 +0000772/**
773 * kernel_recvmsg - Receive a message from a socket (kernel space)
774 * @sock: The socket to receive the message from
775 * @msg: Received message
776 * @vec: Input s/g array for message data
777 * @num: Size of input s/g array
778 * @size: Number of bytes to read
779 * @flags: Message flags (MSG_DONTWAIT, etc...)
780 *
781 * On return the msg structure contains the scatter/gather array passed in the
782 * vec argument. The array is modified so that it consists of the unfilled
783 * portion of the original array.
784 *
785 * The returned value is the total number of bytes received, or an error.
786 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700787int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
788 struct kvec *vec, size_t num, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
790 mm_segment_t oldfs = get_fs();
791 int result;
792
793 set_fs(KERNEL_DS);
794 /*
795 * the following is safe, since for compiler definitions of kvec and
796 * iovec are identical, yielding the same in-core layout and alignment
797 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700798 msg->msg_iov = (struct iovec *)vec, msg->msg_iovlen = num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 result = sock_recvmsg(sock, msg, size, flags);
800 set_fs(oldfs);
801 return result;
802}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700803EXPORT_SYMBOL(kernel_recvmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
805static void sock_aio_dtor(struct kiocb *iocb)
806{
807 kfree(iocb->private);
808}
809
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -0300810static ssize_t sock_sendpage(struct file *file, struct page *page,
811 int offset, size_t size, loff_t *ppos, int more)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
813 struct socket *sock;
814 int flags;
815
Eric Dumazetb69aee02005-09-06 14:42:45 -0700816 sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Eric Dumazet35f9c092012-04-05 03:05:35 +0000818 flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
819 /* more is a combination of MSG_MORE and MSG_SENDPAGE_NOTLAST */
820 flags |= more;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Linus Torvaldse6949582009-08-13 08:28:36 -0700822 return kernel_sendpage(sock, page, offset, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823}
824
Jens Axboe9c55e012007-11-06 23:30:13 -0800825static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700826 struct pipe_inode_info *pipe, size_t len,
Jens Axboe9c55e012007-11-06 23:30:13 -0800827 unsigned int flags)
828{
829 struct socket *sock = file->private_data;
830
Rémi Denis-Courmont997b37d2008-02-15 02:35:45 -0800831 if (unlikely(!sock->ops->splice_read))
832 return -EINVAL;
833
Herbert Xuf8451722010-05-24 00:12:34 -0700834 sock_update_classid(sock->sk);
835
Jens Axboe9c55e012007-11-06 23:30:13 -0800836 return sock->ops->splice_read(sock, ppos, pipe, len, flags);
837}
838
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800839static struct sock_iocb *alloc_sock_iocb(struct kiocb *iocb,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700840 struct sock_iocb *siocb)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800841{
842 if (!is_sync_kiocb(iocb)) {
843 siocb = kmalloc(sizeof(*siocb), GFP_KERNEL);
844 if (!siocb)
845 return NULL;
846 iocb->ki_dtor = sock_aio_dtor;
847 }
848
849 siocb->kiocb = iocb;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800850 iocb->private = siocb;
851 return siocb;
852}
853
854static ssize_t do_sock_read(struct msghdr *msg, struct kiocb *iocb,
Badari Pulavarty027445c2006-09-30 23:28:46 -0700855 struct file *file, const struct iovec *iov,
856 unsigned long nr_segs)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800857{
858 struct socket *sock = file->private_data;
859 size_t size = 0;
860 int i;
861
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700862 for (i = 0; i < nr_segs; i++)
863 size += iov[i].iov_len;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800864
865 msg->msg_name = NULL;
866 msg->msg_namelen = 0;
867 msg->msg_control = NULL;
868 msg->msg_controllen = 0;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700869 msg->msg_iov = (struct iovec *)iov;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800870 msg->msg_iovlen = nr_segs;
871 msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
872
873 return __sock_recvmsg(iocb, sock, msg, size, msg->msg_flags);
874}
875
Badari Pulavarty027445c2006-09-30 23:28:46 -0700876static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov,
877 unsigned long nr_segs, loff_t pos)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800878{
879 struct sock_iocb siocb, *x;
880
881 if (pos != 0)
882 return -ESPIPE;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700883
884 if (iocb->ki_left == 0) /* Match SYS5 behaviour */
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800885 return 0;
886
Badari Pulavarty027445c2006-09-30 23:28:46 -0700887
888 x = alloc_sock_iocb(iocb, &siocb);
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800889 if (!x)
890 return -ENOMEM;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700891 return do_sock_read(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800892}
893
894static ssize_t do_sock_write(struct msghdr *msg, struct kiocb *iocb,
Badari Pulavarty027445c2006-09-30 23:28:46 -0700895 struct file *file, const struct iovec *iov,
896 unsigned long nr_segs)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800897{
898 struct socket *sock = file->private_data;
899 size_t size = 0;
900 int i;
901
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700902 for (i = 0; i < nr_segs; i++)
903 size += iov[i].iov_len;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800904
905 msg->msg_name = NULL;
906 msg->msg_namelen = 0;
907 msg->msg_control = NULL;
908 msg->msg_controllen = 0;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700909 msg->msg_iov = (struct iovec *)iov;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800910 msg->msg_iovlen = nr_segs;
911 msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
912 if (sock->type == SOCK_SEQPACKET)
913 msg->msg_flags |= MSG_EOR;
914
915 return __sock_sendmsg(iocb, sock, msg, size);
916}
917
Badari Pulavarty027445c2006-09-30 23:28:46 -0700918static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov,
919 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800921 struct sock_iocb siocb, *x;
922
923 if (pos != 0)
924 return -ESPIPE;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700925
Badari Pulavarty027445c2006-09-30 23:28:46 -0700926 x = alloc_sock_iocb(iocb, &siocb);
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800927 if (!x)
928 return -ENOMEM;
929
Badari Pulavarty027445c2006-09-30 23:28:46 -0700930 return do_sock_write(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931}
932
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933/*
934 * Atomic setting of ioctl hooks to avoid race
935 * with module unload.
936 */
937
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800938static DEFINE_MUTEX(br_ioctl_mutex);
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700939static int (*br_ioctl_hook) (struct net *, unsigned int cmd, void __user *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Eric W. Biederman881d9662007-09-17 11:56:21 -0700941void brioctl_set(int (*hook) (struct net *, unsigned int, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800943 mutex_lock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 br_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800945 mutex_unlock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946}
947EXPORT_SYMBOL(brioctl_set);
948
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800949static DEFINE_MUTEX(vlan_ioctl_mutex);
Eric W. Biederman881d9662007-09-17 11:56:21 -0700950static int (*vlan_ioctl_hook) (struct net *, void __user *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Eric W. Biederman881d9662007-09-17 11:56:21 -0700952void vlan_ioctl_set(int (*hook) (struct net *, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800954 mutex_lock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 vlan_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800956 mutex_unlock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957}
958EXPORT_SYMBOL(vlan_ioctl_set);
959
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800960static DEFINE_MUTEX(dlci_ioctl_mutex);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700961static int (*dlci_ioctl_hook) (unsigned int, void __user *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700963void dlci_ioctl_set(int (*hook) (unsigned int, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800965 mutex_lock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 dlci_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800967 mutex_unlock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968}
969EXPORT_SYMBOL(dlci_ioctl_set);
970
Arnd Bergmann6b960182009-11-06 23:10:54 -0800971static long sock_do_ioctl(struct net *net, struct socket *sock,
972 unsigned int cmd, unsigned long arg)
973{
974 int err;
975 void __user *argp = (void __user *)arg;
976
977 err = sock->ops->ioctl(sock, cmd, arg);
978
979 /*
980 * If this ioctl is unknown try to hand it down
981 * to the NIC driver.
982 */
983 if (err == -ENOIOCTLCMD)
984 err = dev_ioctl(net, cmd, argp);
985
986 return err;
987}
988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989/*
990 * With an ioctl, arg may well be a user mode pointer, but we don't know
991 * what to do with it - that's up to the protocol still.
992 */
993
994static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
995{
996 struct socket *sock;
Eric W. Biederman881d9662007-09-17 11:56:21 -0700997 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 void __user *argp = (void __user *)arg;
999 int pid, err;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001000 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Eric Dumazetb69aee02005-09-06 14:42:45 -07001002 sock = file->private_data;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001003 sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001004 net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
Eric W. Biederman881d9662007-09-17 11:56:21 -07001006 err = dev_ioctl(net, cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 } else
Johannes Berg3d23e342009-09-29 23:27:28 +02001008#ifdef CONFIG_WEXT_CORE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
Eric W. Biederman881d9662007-09-17 11:56:21 -07001010 err = dev_ioctl(net, cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 } else
Johannes Berg3d23e342009-09-29 23:27:28 +02001012#endif
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001013 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 case FIOSETOWN:
1015 case SIOCSPGRP:
1016 err = -EFAULT;
1017 if (get_user(pid, (int __user *)argp))
1018 break;
1019 err = f_setown(sock->file, pid, 1);
1020 break;
1021 case FIOGETOWN:
1022 case SIOCGPGRP:
Eric W. Biederman609d7fa2006-10-02 02:17:15 -07001023 err = put_user(f_getown(sock->file),
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001024 (int __user *)argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 break;
1026 case SIOCGIFBR:
1027 case SIOCSIFBR:
1028 case SIOCBRADDBR:
1029 case SIOCBRDELBR:
1030 err = -ENOPKG;
1031 if (!br_ioctl_hook)
1032 request_module("bridge");
1033
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001034 mutex_lock(&br_ioctl_mutex);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001035 if (br_ioctl_hook)
Eric W. Biederman881d9662007-09-17 11:56:21 -07001036 err = br_ioctl_hook(net, cmd, argp);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001037 mutex_unlock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 break;
1039 case SIOCGIFVLAN:
1040 case SIOCSIFVLAN:
1041 err = -ENOPKG;
1042 if (!vlan_ioctl_hook)
1043 request_module("8021q");
1044
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001045 mutex_lock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 if (vlan_ioctl_hook)
Eric W. Biederman881d9662007-09-17 11:56:21 -07001047 err = vlan_ioctl_hook(net, argp);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001048 mutex_unlock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 case SIOCADDDLCI:
1051 case SIOCDELDLCI:
1052 err = -ENOPKG;
1053 if (!dlci_ioctl_hook)
1054 request_module("dlci");
1055
Pavel Emelyanov7512cbf2008-03-21 15:58:52 -07001056 mutex_lock(&dlci_ioctl_mutex);
1057 if (dlci_ioctl_hook)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 err = dlci_ioctl_hook(cmd, argp);
Pavel Emelyanov7512cbf2008-03-21 15:58:52 -07001059 mutex_unlock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 break;
1061 default:
Arnd Bergmann6b960182009-11-06 23:10:54 -08001062 err = sock_do_ioctl(net, sock, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 break;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001064 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 return err;
1066}
1067
1068int sock_create_lite(int family, int type, int protocol, struct socket **res)
1069{
1070 int err;
1071 struct socket *sock = NULL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001072
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 err = security_socket_create(family, type, protocol, 1);
1074 if (err)
1075 goto out;
1076
1077 sock = sock_alloc();
1078 if (!sock) {
1079 err = -ENOMEM;
1080 goto out;
1081 }
1082
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 sock->type = type;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001084 err = security_socket_post_create(sock, family, type, protocol, 1);
1085 if (err)
1086 goto out_release;
1087
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088out:
1089 *res = sock;
1090 return err;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001091out_release:
1092 sock_release(sock);
1093 sock = NULL;
1094 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001096EXPORT_SYMBOL(sock_create_lite);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
1098/* No kernel lock held - perfect */
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001099static unsigned int sock_poll(struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100{
1101 struct socket *sock;
1102
1103 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001104 * We can't return errors to poll, so it's either yes or no.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 */
Eric Dumazetb69aee02005-09-06 14:42:45 -07001106 sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 return sock->ops->poll(file, sock, wait);
1108}
1109
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001110static int sock_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111{
Eric Dumazetb69aee02005-09-06 14:42:45 -07001112 struct socket *sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
1114 return sock->ops->mmap(file, sock, vma);
1115}
1116
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001117static int sock_close(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118{
1119 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001120 * It was possible the inode is NULL we were
1121 * closing an unfinished socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 */
1123
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001124 if (!inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 printk(KERN_DEBUG "sock_close: NULL inode\n");
1126 return 0;
1127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 sock_release(SOCKET_I(inode));
1129 return 0;
1130}
1131
1132/*
1133 * Update the socket async list
1134 *
1135 * Fasync_list locking strategy.
1136 *
1137 * 1. fasync_list is modified only under process context socket lock
1138 * i.e. under semaphore.
1139 * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
Eric Dumazet989a2972010-04-14 09:55:35 +00001140 * or under socket lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 */
1142
1143static int sock_fasync(int fd, struct file *filp, int on)
1144{
Eric Dumazet989a2972010-04-14 09:55:35 +00001145 struct socket *sock = filp->private_data;
1146 struct sock *sk = sock->sk;
Eric Dumazeteaefd112011-02-18 03:26:36 +00001147 struct socket_wq *wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
Eric Dumazet989a2972010-04-14 09:55:35 +00001149 if (sk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
1152 lock_sock(sk);
Eric Dumazeteaefd112011-02-18 03:26:36 +00001153 wq = rcu_dereference_protected(sock->wq, sock_owned_by_user(sk));
1154 fasync_helper(fd, filp, on, &wq->fasync_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
Eric Dumazeteaefd112011-02-18 03:26:36 +00001156 if (!wq->fasync_list)
Eric Dumazet989a2972010-04-14 09:55:35 +00001157 sock_reset_flag(sk, SOCK_FASYNC);
Jonathan Corbet76398422009-02-01 14:26:59 -07001158 else
Eric Dumazetbcdce712009-10-06 17:28:29 -07001159 sock_set_flag(sk, SOCK_FASYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Eric Dumazet989a2972010-04-14 09:55:35 +00001161 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 return 0;
1163}
1164
Eric Dumazet43815482010-04-29 11:01:49 +00001165/* This function may be called only under socket lock or callback_lock or rcu_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
1167int sock_wake_async(struct socket *sock, int how, int band)
1168{
Eric Dumazet43815482010-04-29 11:01:49 +00001169 struct socket_wq *wq;
1170
1171 if (!sock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 return -1;
Eric Dumazet43815482010-04-29 11:01:49 +00001173 rcu_read_lock();
1174 wq = rcu_dereference(sock->wq);
1175 if (!wq || !wq->fasync_list) {
1176 rcu_read_unlock();
1177 return -1;
1178 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001179 switch (how) {
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001180 case SOCK_WAKE_WAITD:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 if (test_bit(SOCK_ASYNC_WAITDATA, &sock->flags))
1182 break;
1183 goto call_kill;
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001184 case SOCK_WAKE_SPACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags))
1186 break;
1187 /* fall through */
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001188 case SOCK_WAKE_IO:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001189call_kill:
Eric Dumazet43815482010-04-29 11:01:49 +00001190 kill_fasync(&wq->fasync_list, SIGIO, band);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 break;
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001192 case SOCK_WAKE_URG:
Eric Dumazet43815482010-04-29 11:01:49 +00001193 kill_fasync(&wq->fasync_list, SIGURG, band);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 }
Eric Dumazet43815482010-04-29 11:01:49 +00001195 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 return 0;
1197}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001198EXPORT_SYMBOL(sock_wake_async);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Pavel Emelyanov721db932010-09-29 16:06:32 +04001200int __sock_create(struct net *net, int family, int type, int protocol,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001201 struct socket **res, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202{
1203 int err;
1204 struct socket *sock;
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001205 const struct net_proto_family *pf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
1207 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001208 * Check protocol is in range
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 */
1210 if (family < 0 || family >= NPROTO)
1211 return -EAFNOSUPPORT;
1212 if (type < 0 || type >= SOCK_MAX)
1213 return -EINVAL;
1214
1215 /* Compatibility.
1216
1217 This uglymoron is moved from INET layer to here to avoid
1218 deadlock in module load.
1219 */
1220 if (family == PF_INET && type == SOCK_PACKET) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001221 static int warned;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 if (!warned) {
1223 warned = 1;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001224 printk(KERN_INFO "%s uses obsolete (PF_INET,SOCK_PACKET)\n",
1225 current->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 }
1227 family = PF_PACKET;
1228 }
1229
1230 err = security_socket_create(family, type, protocol, kern);
1231 if (err)
1232 return err;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001233
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001234 /*
1235 * Allocate the socket and allow the family to set things up. if
1236 * the protocol is 0, the family is instructed to select an appropriate
1237 * default.
1238 */
1239 sock = sock_alloc();
1240 if (!sock) {
1241 if (net_ratelimit())
1242 printk(KERN_WARNING "socket: no more sockets\n");
1243 return -ENFILE; /* Not exactly a match, but its the
1244 closest posix thing */
1245 }
1246
1247 sock->type = type;
1248
Johannes Berg95a5afc2008-10-16 15:24:51 -07001249#ifdef CONFIG_MODULES
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001250 /* Attempt to load a protocol module if the find failed.
1251 *
1252 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 * requested real, full-featured networking support upon configuration.
1254 * Otherwise module support will break!
1255 */
Eric Dumazet190683a2010-11-10 10:50:44 +00001256 if (rcu_access_pointer(net_families[family]) == NULL)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001257 request_module("net-pf-%d", family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258#endif
1259
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001260 rcu_read_lock();
1261 pf = rcu_dereference(net_families[family]);
1262 err = -EAFNOSUPPORT;
1263 if (!pf)
1264 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
1266 /*
1267 * We will call the ->create function, that possibly is in a loadable
1268 * module, so we have to bump that loadable module refcnt first.
1269 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001270 if (!try_module_get(pf->owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 goto out_release;
1272
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001273 /* Now protected by module ref count */
1274 rcu_read_unlock();
1275
Eric Paris3f378b62009-11-05 22:18:14 -08001276 err = pf->create(net, sock, protocol, kern);
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001277 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 goto out_module_put;
Frank Filza79af592005-09-27 15:23:38 -07001279
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 /*
1281 * Now to bump the refcnt of the [loadable] module that owns this
1282 * socket at sock_release time we decrement its refcnt.
1283 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001284 if (!try_module_get(sock->ops->owner))
1285 goto out_module_busy;
1286
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 /*
1288 * Now that we're done with the ->create function, the [loadable]
1289 * module can have its refcnt decremented
1290 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001291 module_put(pf->owner);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001292 err = security_socket_post_create(sock, family, type, protocol, kern);
1293 if (err)
Herbert Xu3b185522007-08-15 14:46:02 -07001294 goto out_sock_release;
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001295 *res = sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001297 return 0;
1298
1299out_module_busy:
1300 err = -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301out_module_put:
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001302 sock->ops = NULL;
1303 module_put(pf->owner);
1304out_sock_release:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 sock_release(sock);
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001306 return err;
1307
1308out_release:
1309 rcu_read_unlock();
1310 goto out_sock_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311}
Pavel Emelyanov721db932010-09-29 16:06:32 +04001312EXPORT_SYMBOL(__sock_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
1314int sock_create(int family, int type, int protocol, struct socket **res)
1315{
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001316 return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001318EXPORT_SYMBOL(sock_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
1320int sock_create_kern(int family, int type, int protocol, struct socket **res)
1321{
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001322 return __sock_create(&init_net, family, type, protocol, res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001324EXPORT_SYMBOL(sock_create_kern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001326SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327{
1328 int retval;
1329 struct socket *sock;
Ulrich Dreppera677a032008-07-23 21:29:17 -07001330 int flags;
1331
Ulrich Dreppere38b36f2008-07-23 21:29:42 -07001332 /* Check the SOCK_* constants for consistency. */
1333 BUILD_BUG_ON(SOCK_CLOEXEC != O_CLOEXEC);
1334 BUILD_BUG_ON((SOCK_MAX | SOCK_TYPE_MASK) != SOCK_TYPE_MASK);
1335 BUILD_BUG_ON(SOCK_CLOEXEC & SOCK_TYPE_MASK);
1336 BUILD_BUG_ON(SOCK_NONBLOCK & SOCK_TYPE_MASK);
1337
Ulrich Dreppera677a032008-07-23 21:29:17 -07001338 flags = type & ~SOCK_TYPE_MASK;
Ulrich Drepper77d27202008-07-23 21:29:35 -07001339 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Dreppera677a032008-07-23 21:29:17 -07001340 return -EINVAL;
1341 type &= SOCK_TYPE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001343 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1344 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1345
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 retval = sock_create(family, type, protocol, &sock);
1347 if (retval < 0)
1348 goto out;
1349
Ulrich Drepper77d27202008-07-23 21:29:35 -07001350 retval = sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 if (retval < 0)
1352 goto out_release;
1353
1354out:
1355 /* It may be already another descriptor 8) Not kernel problem. */
1356 return retval;
1357
1358out_release:
1359 sock_release(sock);
1360 return retval;
1361}
1362
1363/*
1364 * Create a pair of connected sockets.
1365 */
1366
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001367SYSCALL_DEFINE4(socketpair, int, family, int, type, int, protocol,
1368 int __user *, usockvec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369{
1370 struct socket *sock1, *sock2;
1371 int fd1, fd2, err;
Al Virodb349502007-02-07 01:48:00 -05001372 struct file *newfile1, *newfile2;
Ulrich Dreppera677a032008-07-23 21:29:17 -07001373 int flags;
1374
1375 flags = type & ~SOCK_TYPE_MASK;
Ulrich Drepper77d27202008-07-23 21:29:35 -07001376 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Dreppera677a032008-07-23 21:29:17 -07001377 return -EINVAL;
1378 type &= SOCK_TYPE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001380 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1381 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1382
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 /*
1384 * Obtain the first socket and check if the underlying protocol
1385 * supports the socketpair call.
1386 */
1387
1388 err = sock_create(family, type, protocol, &sock1);
1389 if (err < 0)
1390 goto out;
1391
1392 err = sock_create(family, type, protocol, &sock2);
1393 if (err < 0)
1394 goto out_release_1;
1395
1396 err = sock1->ops->socketpair(sock1, sock2);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001397 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 goto out_release_both;
1399
Al Viro7cbe66b2009-08-05 19:59:08 +04001400 fd1 = sock_alloc_file(sock1, &newfile1, flags);
David S. Millerbf3c23d2007-10-29 21:54:02 -07001401 if (unlikely(fd1 < 0)) {
1402 err = fd1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 goto out_release_both;
David S. Millerbf3c23d2007-10-29 21:54:02 -07001404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
Al Viro7cbe66b2009-08-05 19:59:08 +04001406 fd2 = sock_alloc_file(sock2, &newfile2, flags);
Al Viro198de4d2009-08-05 19:29:23 +04001407 if (unlikely(fd2 < 0)) {
1408 err = fd2;
1409 fput(newfile1);
1410 put_unused_fd(fd1);
1411 sock_release(sock2);
1412 goto out;
Al Virodb349502007-02-07 01:48:00 -05001413 }
1414
Al Viro157cf642008-12-14 04:57:47 -05001415 audit_fd_pair(fd1, fd2);
Al Virodb349502007-02-07 01:48:00 -05001416 fd_install(fd1, newfile1);
1417 fd_install(fd2, newfile2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 /* fd1 and fd2 may be already another descriptors.
1419 * Not kernel problem.
1420 */
1421
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001422 err = put_user(fd1, &usockvec[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 if (!err)
1424 err = put_user(fd2, &usockvec[1]);
1425 if (!err)
1426 return 0;
1427
1428 sys_close(fd2);
1429 sys_close(fd1);
1430 return err;
1431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432out_release_both:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001433 sock_release(sock2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434out_release_1:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001435 sock_release(sock1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436out:
1437 return err;
1438}
1439
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440/*
1441 * Bind a name to a socket. Nothing much to do here since it's
1442 * the protocol's responsibility to handle the local address.
1443 *
1444 * We move the socket address to kernel space before we call
1445 * the protocol layer (having also checked the address is ok).
1446 */
1447
Heiko Carstens20f37032009-01-14 14:14:23 +01001448SYSCALL_DEFINE3(bind, int, fd, struct sockaddr __user *, umyaddr, int, addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449{
1450 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001451 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001452 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001454 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001455 if (sock) {
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001456 err = move_addr_to_kernel(umyaddr, addrlen, &address);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001457 if (err >= 0) {
1458 err = security_socket_bind(sock,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001459 (struct sockaddr *)&address,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001460 addrlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001461 if (!err)
1462 err = sock->ops->bind(sock,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001463 (struct sockaddr *)
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001464 &address, addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 }
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001466 fput_light(sock->file, fput_needed);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 return err;
1469}
1470
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471/*
1472 * Perform a listen. Basically, we allow the protocol to do anything
1473 * necessary for a listen, and if that works, we mark the socket as
1474 * ready for listening.
1475 */
1476
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001477SYSCALL_DEFINE2(listen, int, fd, int, backlog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478{
1479 struct socket *sock;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001480 int err, fput_needed;
Pavel Emelyanovb8e1f9b2007-12-08 00:12:33 -08001481 int somaxconn;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001482
1483 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1484 if (sock) {
Pavel Emelyanov8efa6e92008-03-31 19:41:14 -07001485 somaxconn = sock_net(sock->sk)->core.sysctl_somaxconn;
Pavel Emelyanovb8e1f9b2007-12-08 00:12:33 -08001486 if ((unsigned)backlog > somaxconn)
1487 backlog = somaxconn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
1489 err = security_socket_listen(sock, backlog);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001490 if (!err)
1491 err = sock->ops->listen(sock, backlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001493 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 }
1495 return err;
1496}
1497
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498/*
1499 * For accept, we attempt to create a new socket, set up the link
1500 * with the client, wake up the client, then return the new
1501 * connected fd. We collect the address of the connector in kernel
1502 * space and move it to user at the very end. This is unclean because
1503 * we open the socket then return an error.
1504 *
1505 * 1003.1g adds the ability to recvmsg() to query connection pending
1506 * status to recvmsg. We need to add that support in a way thats
1507 * clean when we restucture accept also.
1508 */
1509
Heiko Carstens20f37032009-01-14 14:14:23 +01001510SYSCALL_DEFINE4(accept4, int, fd, struct sockaddr __user *, upeer_sockaddr,
1511 int __user *, upeer_addrlen, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512{
1513 struct socket *sock, *newsock;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001514 struct file *newfile;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001515 int err, len, newfd, fput_needed;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001516 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
Ulrich Drepper77d27202008-07-23 21:29:35 -07001518 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001519 return -EINVAL;
1520
1521 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1522 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1523
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001524 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 if (!sock)
1526 goto out;
1527
1528 err = -ENFILE;
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001529 newsock = sock_alloc();
1530 if (!newsock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 goto out_put;
1532
1533 newsock->type = sock->type;
1534 newsock->ops = sock->ops;
1535
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 /*
1537 * We don't need try_module_get here, as the listening socket (sock)
1538 * has the protocol module (sock->ops->owner) held.
1539 */
1540 __module_get(newsock->ops->owner);
1541
Al Viro7cbe66b2009-08-05 19:59:08 +04001542 newfd = sock_alloc_file(newsock, &newfile, flags);
David S. Miller39d8c1b2006-03-20 17:13:49 -08001543 if (unlikely(newfd < 0)) {
1544 err = newfd;
David S. Miller9a1875e2006-04-01 12:48:36 -08001545 sock_release(newsock);
1546 goto out_put;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001547 }
1548
Frank Filza79af592005-09-27 15:23:38 -07001549 err = security_socket_accept(sock, newsock);
1550 if (err)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001551 goto out_fd;
Frank Filza79af592005-09-27 15:23:38 -07001552
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 err = sock->ops->accept(sock, newsock, sock->file->f_flags);
1554 if (err < 0)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001555 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
1557 if (upeer_sockaddr) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001558 if (newsock->ops->getname(newsock, (struct sockaddr *)&address,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001559 &len, 2) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 err = -ECONNABORTED;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001561 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 }
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001563 err = move_addr_to_user(&address,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001564 len, upeer_sockaddr, upeer_addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 if (err < 0)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001566 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 }
1568
1569 /* File flags are not inherited via accept() unlike another OSes. */
1570
David S. Miller39d8c1b2006-03-20 17:13:49 -08001571 fd_install(newfd, newfile);
1572 err = newfd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001575 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576out:
1577 return err;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001578out_fd:
David S. Miller9606a212006-04-01 01:00:14 -08001579 fput(newfile);
David S. Miller39d8c1b2006-03-20 17:13:49 -08001580 put_unused_fd(newfd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 goto out_put;
1582}
1583
Heiko Carstens20f37032009-01-14 14:14:23 +01001584SYSCALL_DEFINE3(accept, int, fd, struct sockaddr __user *, upeer_sockaddr,
1585 int __user *, upeer_addrlen)
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001586{
Ulrich Drepperde11def2008-11-19 15:36:14 -08001587 return sys_accept4(fd, upeer_sockaddr, upeer_addrlen, 0);
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001588}
1589
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590/*
1591 * Attempt to connect to a socket with the server address. The address
1592 * is in user space so we verify it is OK and move it to kernel space.
1593 *
1594 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
1595 * break bindings
1596 *
1597 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
1598 * other SEQPACKET protocols that take time to connect() as it doesn't
1599 * include the -EINPROGRESS status for such sockets.
1600 */
1601
Heiko Carstens20f37032009-01-14 14:14:23 +01001602SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
1603 int, addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604{
1605 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001606 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001607 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001609 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 if (!sock)
1611 goto out;
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001612 err = move_addr_to_kernel(uservaddr, addrlen, &address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 if (err < 0)
1614 goto out_put;
1615
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001616 err =
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001617 security_socket_connect(sock, (struct sockaddr *)&address, addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 if (err)
1619 goto out_put;
1620
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001621 err = sock->ops->connect(sock, (struct sockaddr *)&address, addrlen,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 sock->file->f_flags);
1623out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001624 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625out:
1626 return err;
1627}
1628
1629/*
1630 * Get the local address ('name') of a socket object. Move the obtained
1631 * name to user space.
1632 */
1633
Heiko Carstens20f37032009-01-14 14:14:23 +01001634SYSCALL_DEFINE3(getsockname, int, fd, struct sockaddr __user *, usockaddr,
1635 int __user *, usockaddr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636{
1637 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001638 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001639 int len, err, fput_needed;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001640
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001641 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 if (!sock)
1643 goto out;
1644
1645 err = security_socket_getsockname(sock);
1646 if (err)
1647 goto out_put;
1648
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001649 err = sock->ops->getname(sock, (struct sockaddr *)&address, &len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 if (err)
1651 goto out_put;
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001652 err = move_addr_to_user(&address, len, usockaddr, usockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
1654out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001655 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656out:
1657 return err;
1658}
1659
1660/*
1661 * Get the remote address ('name') of a socket object. Move the obtained
1662 * name to user space.
1663 */
1664
Heiko Carstens20f37032009-01-14 14:14:23 +01001665SYSCALL_DEFINE3(getpeername, int, fd, struct sockaddr __user *, usockaddr,
1666 int __user *, usockaddr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667{
1668 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001669 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001670 int len, err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001672 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1673 if (sock != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 err = security_socket_getpeername(sock);
1675 if (err) {
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001676 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 return err;
1678 }
1679
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001680 err =
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001681 sock->ops->getname(sock, (struct sockaddr *)&address, &len,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001682 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 if (!err)
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001684 err = move_addr_to_user(&address, len, usockaddr,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001685 usockaddr_len);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001686 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 }
1688 return err;
1689}
1690
1691/*
1692 * Send a datagram to a given address. We move the address into kernel
1693 * space and check the user space data area is readable before invoking
1694 * the protocol.
1695 */
1696
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001697SYSCALL_DEFINE6(sendto, int, fd, void __user *, buff, size_t, len,
1698 unsigned, flags, struct sockaddr __user *, addr,
1699 int, addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700{
1701 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001702 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 int err;
1704 struct msghdr msg;
1705 struct iovec iov;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001706 int fput_needed;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001707
Linus Torvalds253eacc2010-10-30 16:43:10 -07001708 if (len > INT_MAX)
1709 len = INT_MAX;
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001710 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1711 if (!sock)
David S. Miller4387ff72007-02-08 15:06:08 -08001712 goto out;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001713
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001714 iov.iov_base = buff;
1715 iov.iov_len = len;
1716 msg.msg_name = NULL;
1717 msg.msg_iov = &iov;
1718 msg.msg_iovlen = 1;
1719 msg.msg_control = NULL;
1720 msg.msg_controllen = 0;
1721 msg.msg_namelen = 0;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001722 if (addr) {
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001723 err = move_addr_to_kernel(addr, addr_len, &address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 if (err < 0)
1725 goto out_put;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001726 msg.msg_name = (struct sockaddr *)&address;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001727 msg.msg_namelen = addr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 }
1729 if (sock->file->f_flags & O_NONBLOCK)
1730 flags |= MSG_DONTWAIT;
1731 msg.msg_flags = flags;
1732 err = sock_sendmsg(sock, &msg, len);
1733
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001734out_put:
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001735 fput_light(sock->file, fput_needed);
David S. Miller4387ff72007-02-08 15:06:08 -08001736out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 return err;
1738}
1739
1740/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001741 * Send a datagram down a socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 */
1743
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001744SYSCALL_DEFINE4(send, int, fd, void __user *, buff, size_t, len,
1745 unsigned, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746{
1747 return sys_sendto(fd, buff, len, flags, NULL, 0);
1748}
1749
1750/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001751 * Receive a frame from the socket and optionally record the address of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 * sender. We verify the buffers are writable and if needed move the
1753 * sender address from kernel to user space.
1754 */
1755
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001756SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, ubuf, size_t, size,
1757 unsigned, flags, struct sockaddr __user *, addr,
1758 int __user *, addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759{
1760 struct socket *sock;
1761 struct iovec iov;
1762 struct msghdr msg;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001763 struct sockaddr_storage address;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001764 int err, err2;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001765 int fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
Linus Torvalds253eacc2010-10-30 16:43:10 -07001767 if (size > INT_MAX)
1768 size = INT_MAX;
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001769 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 if (!sock)
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001771 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001773 msg.msg_control = NULL;
1774 msg.msg_controllen = 0;
1775 msg.msg_iovlen = 1;
1776 msg.msg_iov = &iov;
1777 iov.iov_len = size;
1778 iov.iov_base = ubuf;
Hannes Frederic Sowa18719a42013-11-21 03:14:22 +01001779 /* Save some cycles and don't copy the address if not needed */
1780 msg.msg_name = addr ? (struct sockaddr *)&address : NULL;
1781 /* We assume all kernel code knows the size of sockaddr_storage */
1782 msg.msg_namelen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 if (sock->file->f_flags & O_NONBLOCK)
1784 flags |= MSG_DONTWAIT;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001785 err = sock_recvmsg(sock, &msg, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001787 if (err >= 0 && addr != NULL) {
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001788 err2 = move_addr_to_user(&address,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001789 msg.msg_namelen, addr, addr_len);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001790 if (err2 < 0)
1791 err = err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 }
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001793
1794 fput_light(sock->file, fput_needed);
David S. Miller4387ff72007-02-08 15:06:08 -08001795out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 return err;
1797}
1798
1799/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001800 * Receive a datagram from a socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 */
1802
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001803asmlinkage long sys_recv(int fd, void __user *ubuf, size_t size,
1804 unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805{
1806 return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
1807}
1808
1809/*
1810 * Set a socket option. Because we don't know the option lengths we have
1811 * to pass the user mode parameter for the protocols to sort out.
1812 */
1813
Heiko Carstens20f37032009-01-14 14:14:23 +01001814SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname,
1815 char __user *, optval, int, optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001817 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 struct socket *sock;
1819
1820 if (optlen < 0)
1821 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001822
1823 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1824 if (sock != NULL) {
1825 err = security_socket_setsockopt(sock, level, optname);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001826 if (err)
1827 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
1829 if (level == SOL_SOCKET)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001830 err =
1831 sock_setsockopt(sock, level, optname, optval,
1832 optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 else
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001834 err =
1835 sock->ops->setsockopt(sock, level, optname, optval,
1836 optlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001837out_put:
1838 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 }
1840 return err;
1841}
1842
1843/*
1844 * Get a socket option. Because we don't know the option lengths we have
1845 * to pass a user mode parameter for the protocols to sort out.
1846 */
1847
Heiko Carstens20f37032009-01-14 14:14:23 +01001848SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname,
1849 char __user *, optval, int __user *, optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001851 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 struct socket *sock;
1853
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001854 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1855 if (sock != NULL) {
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001856 err = security_socket_getsockopt(sock, level, optname);
1857 if (err)
1858 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
1860 if (level == SOL_SOCKET)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001861 err =
1862 sock_getsockopt(sock, level, optname, optval,
1863 optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 else
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001865 err =
1866 sock->ops->getsockopt(sock, level, optname, optval,
1867 optlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001868out_put:
1869 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 }
1871 return err;
1872}
1873
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874/*
1875 * Shutdown a socket.
1876 */
1877
Heiko Carstens754fe8d2009-01-14 14:14:09 +01001878SYSCALL_DEFINE2(shutdown, int, fd, int, how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001880 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 struct socket *sock;
1882
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001883 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1884 if (sock != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 err = security_socket_shutdown(sock, how);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001886 if (!err)
1887 err = sock->ops->shutdown(sock, how);
1888 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 }
1890 return err;
1891}
1892
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001893/* A couple of helpful macros for getting the address of the 32/64 bit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 * fields which are the same type (int / unsigned) on our platforms.
1895 */
1896#define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
1897#define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
1898#define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
1899
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00001900struct used_address {
1901 struct sockaddr_storage name;
1902 unsigned int name_len;
1903};
1904
Dan Carpenter5684fac32013-10-03 00:27:20 +03001905static int copy_msghdr_from_user(struct msghdr *kmsg,
1906 struct msghdr __user *umsg)
1907{
1908 if (copy_from_user(kmsg, umsg, sizeof(struct msghdr)))
1909 return -EFAULT;
Matthew Leachd82c1522014-03-11 11:58:27 +00001910
1911 if (kmsg->msg_namelen < 0)
1912 return -EINVAL;
1913
Dan Carpenter5684fac32013-10-03 00:27:20 +03001914 if (kmsg->msg_namelen > sizeof(struct sockaddr_storage))
Dan Carpentereb7eb182013-11-27 15:40:21 +03001915 kmsg->msg_namelen = sizeof(struct sockaddr_storage);
Dan Carpenter5684fac32013-10-03 00:27:20 +03001916 return 0;
1917}
1918
Andy Lutomirskic0cc94f2013-05-22 14:07:44 -07001919static int ___sys_sendmsg(struct socket *sock, struct msghdr __user *msg,
1920 struct msghdr *msg_sys, unsigned flags,
1921 struct used_address *used_address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922{
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001923 struct compat_msghdr __user *msg_compat =
1924 (struct compat_msghdr __user *)msg;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001925 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
Alex Williamsonb9d717a2005-09-26 14:28:02 -07001927 unsigned char ctl[sizeof(struct cmsghdr) + 20]
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001928 __attribute__ ((aligned(sizeof(__kernel_size_t))));
1929 /* 20 is size of ipv6_pktinfo */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 unsigned char *ctl_buf = ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 int err, ctl_len, iov_size, total_len;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001932
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 err = -EFAULT;
1934 if (MSG_CMSG_COMPAT & flags) {
Anton Blanchard228e5482011-05-02 20:21:35 +00001935 if (get_compat_msghdr(msg_sys, msg_compat))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 return -EFAULT;
Dan Carpenter5684fac32013-10-03 00:27:20 +03001937 } else {
1938 err = copy_msghdr_from_user(msg_sys, msg);
1939 if (err)
1940 return err;
1941 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 /* do not move before msg_sys is valid */
1944 err = -EMSGSIZE;
Anton Blanchard228e5482011-05-02 20:21:35 +00001945 if (msg_sys->msg_iovlen > UIO_MAXIOV)
1946 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001948 /* Check whether to allocate the iovec area */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 err = -ENOMEM;
Anton Blanchard228e5482011-05-02 20:21:35 +00001950 iov_size = msg_sys->msg_iovlen * sizeof(struct iovec);
1951 if (msg_sys->msg_iovlen > UIO_FASTIOV) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
1953 if (!iov)
Anton Blanchard228e5482011-05-02 20:21:35 +00001954 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 }
1956
1957 /* This will also move the address data into kernel space */
1958 if (MSG_CMSG_COMPAT & flags) {
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001959 err = verify_compat_iovec(msg_sys, iov, &address, VERIFY_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 } else
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001961 err = verify_iovec(msg_sys, iov, &address, VERIFY_READ);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001962 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 goto out_freeiov;
1964 total_len = err;
1965
1966 err = -ENOBUFS;
1967
Anton Blanchard228e5482011-05-02 20:21:35 +00001968 if (msg_sys->msg_controllen > INT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 goto out_freeiov;
Anton Blanchard228e5482011-05-02 20:21:35 +00001970 ctl_len = msg_sys->msg_controllen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001972 err =
Anton Blanchard228e5482011-05-02 20:21:35 +00001973 cmsghdr_from_user_compat_to_kern(msg_sys, sock->sk, ctl,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001974 sizeof(ctl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 if (err)
1976 goto out_freeiov;
Anton Blanchard228e5482011-05-02 20:21:35 +00001977 ctl_buf = msg_sys->msg_control;
1978 ctl_len = msg_sys->msg_controllen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 } else if (ctl_len) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001980 if (ctl_len > sizeof(ctl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001982 if (ctl_buf == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 goto out_freeiov;
1984 }
1985 err = -EFAULT;
1986 /*
Anton Blanchard228e5482011-05-02 20:21:35 +00001987 * Careful! Before this, msg_sys->msg_control contains a user pointer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
1989 * checking falls down on this.
1990 */
Namhyung Kimfb8621b2010-09-07 03:55:00 +00001991 if (copy_from_user(ctl_buf,
Anton Blanchard228e5482011-05-02 20:21:35 +00001992 (void __user __force *)msg_sys->msg_control,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001993 ctl_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 goto out_freectl;
Anton Blanchard228e5482011-05-02 20:21:35 +00001995 msg_sys->msg_control = ctl_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 }
Anton Blanchard228e5482011-05-02 20:21:35 +00001997 msg_sys->msg_flags = flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
1999 if (sock->file->f_flags & O_NONBLOCK)
Anton Blanchard228e5482011-05-02 20:21:35 +00002000 msg_sys->msg_flags |= MSG_DONTWAIT;
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002001 /*
2002 * If this is sendmmsg() and current destination address is same as
2003 * previously succeeded address, omit asking LSM's decision.
2004 * used_address->name_len is initialized to UINT_MAX so that the first
2005 * destination address never matches.
2006 */
Mathieu Desnoyersbc909d92011-08-24 19:45:03 -07002007 if (used_address && msg_sys->msg_name &&
2008 used_address->name_len == msg_sys->msg_namelen &&
2009 !memcmp(&used_address->name, msg_sys->msg_name,
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002010 used_address->name_len)) {
2011 err = sock_sendmsg_nosec(sock, msg_sys, total_len);
2012 goto out_freectl;
2013 }
2014 err = sock_sendmsg(sock, msg_sys, total_len);
2015 /*
2016 * If this is sendmmsg() and sending to current destination address was
2017 * successful, remember it.
2018 */
2019 if (used_address && err >= 0) {
2020 used_address->name_len = msg_sys->msg_namelen;
Mathieu Desnoyersbc909d92011-08-24 19:45:03 -07002021 if (msg_sys->msg_name)
2022 memcpy(&used_address->name, msg_sys->msg_name,
2023 used_address->name_len);
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002024 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
2026out_freectl:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002027 if (ctl_buf != ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 sock_kfree_s(sock->sk, ctl_buf, ctl_len);
2029out_freeiov:
2030 if (iov != iovstack)
2031 sock_kfree_s(sock->sk, iov, iov_size);
Anton Blanchard228e5482011-05-02 20:21:35 +00002032out:
2033 return err;
2034}
2035
2036/*
2037 * BSD sendmsg interface
2038 */
2039
Andy Lutomirskic0cc94f2013-05-22 14:07:44 -07002040long __sys_sendmsg(int fd, struct msghdr __user *msg, unsigned flags)
Anton Blanchard228e5482011-05-02 20:21:35 +00002041{
2042 int fput_needed, err;
2043 struct msghdr msg_sys;
Andy Lutomirskic0cc94f2013-05-22 14:07:44 -07002044 struct socket *sock;
Anton Blanchard228e5482011-05-02 20:21:35 +00002045
Andy Lutomirskic0cc94f2013-05-22 14:07:44 -07002046 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Anton Blanchard228e5482011-05-02 20:21:35 +00002047 if (!sock)
2048 goto out;
2049
Andy Lutomirskic0cc94f2013-05-22 14:07:44 -07002050 err = ___sys_sendmsg(sock, msg, &msg_sys, flags, NULL);
Anton Blanchard228e5482011-05-02 20:21:35 +00002051
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002052 fput_light(sock->file, fput_needed);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002053out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 return err;
2055}
2056
Andy Lutomirskic0cc94f2013-05-22 14:07:44 -07002057SYSCALL_DEFINE3(sendmsg, int, fd, struct msghdr __user *, msg, unsigned int, flags)
2058{
2059 if (flags & MSG_CMSG_COMPAT)
2060 return -EINVAL;
2061 return __sys_sendmsg(fd, msg, flags);
2062}
2063
Anton Blanchard228e5482011-05-02 20:21:35 +00002064/*
2065 * Linux sendmmsg interface
2066 */
2067
2068int __sys_sendmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
2069 unsigned int flags)
2070{
2071 int fput_needed, err, datagrams;
2072 struct socket *sock;
2073 struct mmsghdr __user *entry;
2074 struct compat_mmsghdr __user *compat_entry;
2075 struct msghdr msg_sys;
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002076 struct used_address used_address;
Anton Blanchard228e5482011-05-02 20:21:35 +00002077
Anton Blanchard98382f42011-08-04 14:07:39 +00002078 if (vlen > UIO_MAXIOV)
2079 vlen = UIO_MAXIOV;
Anton Blanchard228e5482011-05-02 20:21:35 +00002080
2081 datagrams = 0;
2082
2083 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2084 if (!sock)
2085 return err;
2086
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002087 used_address.name_len = UINT_MAX;
Anton Blanchard228e5482011-05-02 20:21:35 +00002088 entry = mmsg;
2089 compat_entry = (struct compat_mmsghdr __user *)mmsg;
Anton Blanchard728ffb82011-08-04 14:07:38 +00002090 err = 0;
Anton Blanchard228e5482011-05-02 20:21:35 +00002091
2092 while (datagrams < vlen) {
Anton Blanchard228e5482011-05-02 20:21:35 +00002093 if (MSG_CMSG_COMPAT & flags) {
Andy Lutomirskic0cc94f2013-05-22 14:07:44 -07002094 err = ___sys_sendmsg(sock, (struct msghdr __user *)compat_entry,
2095 &msg_sys, flags, &used_address);
Anton Blanchard228e5482011-05-02 20:21:35 +00002096 if (err < 0)
2097 break;
2098 err = __put_user(err, &compat_entry->msg_len);
2099 ++compat_entry;
2100 } else {
Andy Lutomirskic0cc94f2013-05-22 14:07:44 -07002101 err = ___sys_sendmsg(sock,
2102 (struct msghdr __user *)entry,
2103 &msg_sys, flags, &used_address);
Anton Blanchard228e5482011-05-02 20:21:35 +00002104 if (err < 0)
2105 break;
2106 err = put_user(err, &entry->msg_len);
2107 ++entry;
2108 }
2109
2110 if (err)
2111 break;
2112 ++datagrams;
2113 }
2114
Anton Blanchard228e5482011-05-02 20:21:35 +00002115 fput_light(sock->file, fput_needed);
2116
Anton Blanchard728ffb82011-08-04 14:07:38 +00002117 /* We only return an error if no datagrams were able to be sent */
2118 if (datagrams != 0)
Anton Blanchard228e5482011-05-02 20:21:35 +00002119 return datagrams;
2120
Anton Blanchard228e5482011-05-02 20:21:35 +00002121 return err;
2122}
2123
2124SYSCALL_DEFINE4(sendmmsg, int, fd, struct mmsghdr __user *, mmsg,
2125 unsigned int, vlen, unsigned int, flags)
2126{
Andy Lutomirskic0cc94f2013-05-22 14:07:44 -07002127 if (flags & MSG_CMSG_COMPAT)
2128 return -EINVAL;
Anton Blanchard228e5482011-05-02 20:21:35 +00002129 return __sys_sendmmsg(fd, mmsg, vlen, flags);
2130}
2131
Andy Lutomirskic0cc94f2013-05-22 14:07:44 -07002132static int ___sys_recvmsg(struct socket *sock, struct msghdr __user *msg,
2133 struct msghdr *msg_sys, unsigned flags, int nosec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134{
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002135 struct compat_msghdr __user *msg_compat =
2136 (struct compat_msghdr __user *)msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 struct iovec iovstack[UIO_FASTIOV];
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002138 struct iovec *iov = iovstack;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 unsigned long cmsg_ptr;
2140 int err, iov_size, total_len, len;
2141
2142 /* kernel mode address */
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07002143 struct sockaddr_storage addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
2145 /* user mode address pointers */
2146 struct sockaddr __user *uaddr;
2147 int __user *uaddr_len;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002148
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 if (MSG_CMSG_COMPAT & flags) {
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002150 if (get_compat_msghdr(msg_sys, msg_compat))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 return -EFAULT;
Dan Carpenter5684fac32013-10-03 00:27:20 +03002152 } else {
2153 err = copy_msghdr_from_user(msg_sys, msg);
2154 if (err)
2155 return err;
2156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 err = -EMSGSIZE;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002159 if (msg_sys->msg_iovlen > UIO_MAXIOV)
2160 goto out;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002161
2162 /* Check whether to allocate the iovec area */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 err = -ENOMEM;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002164 iov_size = msg_sys->msg_iovlen * sizeof(struct iovec);
2165 if (msg_sys->msg_iovlen > UIO_FASTIOV) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
2167 if (!iov)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002168 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 }
2170
Hannes Frederic Sowa18719a42013-11-21 03:14:22 +01002171 /* Save the user-mode address (verify_iovec will change the
2172 * kernel msghdr to use the kernel address space)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 */
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002174 uaddr = (__force void __user *)msg_sys->msg_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 uaddr_len = COMPAT_NAMELEN(msg);
Hannes Frederic Sowa18719a42013-11-21 03:14:22 +01002176 if (MSG_CMSG_COMPAT & flags)
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00002177 err = verify_compat_iovec(msg_sys, iov, &addr, VERIFY_WRITE);
Hannes Frederic Sowa18719a42013-11-21 03:14:22 +01002178 else
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00002179 err = verify_iovec(msg_sys, iov, &addr, VERIFY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 if (err < 0)
2181 goto out_freeiov;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002182 total_len = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002184 cmsg_ptr = (unsigned long)msg_sys->msg_control;
2185 msg_sys->msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002186
Hannes Frederic Sowa18719a42013-11-21 03:14:22 +01002187 /* We assume all kernel code knows the size of sockaddr_storage */
2188 msg_sys->msg_namelen = 0;
2189
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 if (sock->file->f_flags & O_NONBLOCK)
2191 flags |= MSG_DONTWAIT;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002192 err = (nosec ? sock_recvmsg_nosec : sock_recvmsg)(sock, msg_sys,
2193 total_len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 if (err < 0)
2195 goto out_freeiov;
2196 len = err;
2197
2198 if (uaddr != NULL) {
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00002199 err = move_addr_to_user(&addr,
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002200 msg_sys->msg_namelen, uaddr,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002201 uaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 if (err < 0)
2203 goto out_freeiov;
2204 }
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002205 err = __put_user((msg_sys->msg_flags & ~MSG_CMSG_COMPAT),
David S. Miller37f7f422005-09-16 16:51:01 -07002206 COMPAT_FLAGS(msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 if (err)
2208 goto out_freeiov;
2209 if (MSG_CMSG_COMPAT & flags)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002210 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 &msg_compat->msg_controllen);
2212 else
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002213 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 &msg->msg_controllen);
2215 if (err)
2216 goto out_freeiov;
2217 err = len;
2218
2219out_freeiov:
2220 if (iov != iovstack)
2221 sock_kfree_s(sock->sk, iov, iov_size);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002222out:
2223 return err;
2224}
2225
2226/*
2227 * BSD recvmsg interface
2228 */
2229
Andy Lutomirskic0cc94f2013-05-22 14:07:44 -07002230long __sys_recvmsg(int fd, struct msghdr __user *msg, unsigned flags)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002231{
2232 int fput_needed, err;
2233 struct msghdr msg_sys;
Andy Lutomirskic0cc94f2013-05-22 14:07:44 -07002234 struct socket *sock;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002235
Andy Lutomirskic0cc94f2013-05-22 14:07:44 -07002236 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002237 if (!sock)
2238 goto out;
2239
Andy Lutomirskic0cc94f2013-05-22 14:07:44 -07002240 err = ___sys_recvmsg(sock, msg, &msg_sys, flags, 0);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002241
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002242 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243out:
2244 return err;
2245}
2246
Andy Lutomirskic0cc94f2013-05-22 14:07:44 -07002247SYSCALL_DEFINE3(recvmsg, int, fd, struct msghdr __user *, msg,
2248 unsigned int, flags)
2249{
2250 if (flags & MSG_CMSG_COMPAT)
2251 return -EINVAL;
2252 return __sys_recvmsg(fd, msg, flags);
2253}
2254
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002255/*
2256 * Linux recvmmsg interface
2257 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002259int __sys_recvmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
2260 unsigned int flags, struct timespec *timeout)
2261{
2262 int fput_needed, err, datagrams;
2263 struct socket *sock;
2264 struct mmsghdr __user *entry;
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002265 struct compat_mmsghdr __user *compat_entry;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002266 struct msghdr msg_sys;
2267 struct timespec end_time;
2268
2269 if (timeout &&
2270 poll_select_set_timeout(&end_time, timeout->tv_sec,
2271 timeout->tv_nsec))
2272 return -EINVAL;
2273
2274 datagrams = 0;
2275
2276 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2277 if (!sock)
2278 return err;
2279
2280 err = sock_error(sock->sk);
2281 if (err)
2282 goto out_put;
2283
2284 entry = mmsg;
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002285 compat_entry = (struct compat_mmsghdr __user *)mmsg;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002286
2287 while (datagrams < vlen) {
2288 /*
2289 * No need to ask LSM for more than the first datagram.
2290 */
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002291 if (MSG_CMSG_COMPAT & flags) {
Andy Lutomirskic0cc94f2013-05-22 14:07:44 -07002292 err = ___sys_recvmsg(sock, (struct msghdr __user *)compat_entry,
2293 &msg_sys, flags & ~MSG_WAITFORONE,
2294 datagrams);
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002295 if (err < 0)
2296 break;
2297 err = __put_user(err, &compat_entry->msg_len);
2298 ++compat_entry;
2299 } else {
Andy Lutomirskic0cc94f2013-05-22 14:07:44 -07002300 err = ___sys_recvmsg(sock,
2301 (struct msghdr __user *)entry,
2302 &msg_sys, flags & ~MSG_WAITFORONE,
2303 datagrams);
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002304 if (err < 0)
2305 break;
2306 err = put_user(err, &entry->msg_len);
2307 ++entry;
2308 }
2309
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002310 if (err)
2311 break;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002312 ++datagrams;
2313
Brandon L Black71c5c152010-03-26 16:18:03 +00002314 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
2315 if (flags & MSG_WAITFORONE)
2316 flags |= MSG_DONTWAIT;
2317
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002318 if (timeout) {
2319 ktime_get_ts(timeout);
2320 *timeout = timespec_sub(end_time, *timeout);
2321 if (timeout->tv_sec < 0) {
2322 timeout->tv_sec = timeout->tv_nsec = 0;
2323 break;
2324 }
2325
2326 /* Timeout, return less than vlen datagrams */
2327 if (timeout->tv_nsec == 0 && timeout->tv_sec == 0)
2328 break;
2329 }
2330
2331 /* Out of band data, return right away */
2332 if (msg_sys.msg_flags & MSG_OOB)
2333 break;
2334 }
2335
2336out_put:
2337 fput_light(sock->file, fput_needed);
2338
2339 if (err == 0)
2340 return datagrams;
2341
2342 if (datagrams != 0) {
2343 /*
2344 * We may return less entries than requested (vlen) if the
2345 * sock is non block and there aren't enough datagrams...
2346 */
2347 if (err != -EAGAIN) {
2348 /*
2349 * ... or if recvmsg returns an error after we
2350 * received some datagrams, where we record the
2351 * error to return on the next call or if the
2352 * app asks about it using getsockopt(SO_ERROR).
2353 */
2354 sock->sk->sk_err = -err;
2355 }
2356
2357 return datagrams;
2358 }
2359
2360 return err;
2361}
2362
2363SYSCALL_DEFINE5(recvmmsg, int, fd, struct mmsghdr __user *, mmsg,
2364 unsigned int, vlen, unsigned int, flags,
2365 struct timespec __user *, timeout)
2366{
2367 int datagrams;
2368 struct timespec timeout_sys;
2369
Andy Lutomirskic0cc94f2013-05-22 14:07:44 -07002370 if (flags & MSG_CMSG_COMPAT)
2371 return -EINVAL;
2372
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002373 if (!timeout)
2374 return __sys_recvmmsg(fd, mmsg, vlen, flags, NULL);
2375
2376 if (copy_from_user(&timeout_sys, timeout, sizeof(timeout_sys)))
2377 return -EFAULT;
2378
2379 datagrams = __sys_recvmmsg(fd, mmsg, vlen, flags, &timeout_sys);
2380
2381 if (datagrams > 0 &&
2382 copy_to_user(timeout, &timeout_sys, sizeof(timeout_sys)))
2383 datagrams = -EFAULT;
2384
2385 return datagrams;
2386}
2387
2388#ifdef __ARCH_WANT_SYS_SOCKETCALL
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389/* Argument list sizes for sys_socketcall */
2390#define AL(x) ((x) * sizeof(unsigned long))
Anton Blanchard228e5482011-05-02 20:21:35 +00002391static const unsigned char nargs[21] = {
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002392 AL(0), AL(3), AL(3), AL(3), AL(2), AL(3),
2393 AL(3), AL(3), AL(4), AL(4), AL(4), AL(6),
2394 AL(6), AL(2), AL(5), AL(5), AL(3), AL(3),
Anton Blanchard228e5482011-05-02 20:21:35 +00002395 AL(4), AL(5), AL(4)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002396};
2397
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398#undef AL
2399
2400/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002401 * System call vectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 *
2403 * Argument checking cleaned up. Saved 20% in size.
2404 * This function doesn't need to set the kernel lock because
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002405 * it is set by the callees.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 */
2407
Heiko Carstens3e0fa652009-01-14 14:14:24 +01002408SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409{
2410 unsigned long a[6];
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002411 unsigned long a0, a1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 int err;
Arjan van de Ven47379052009-09-28 12:57:44 -07002413 unsigned int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414
Anton Blanchard228e5482011-05-02 20:21:35 +00002415 if (call < 1 || call > SYS_SENDMMSG)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 return -EINVAL;
2417
Arjan van de Ven47379052009-09-28 12:57:44 -07002418 len = nargs[call];
2419 if (len > sizeof(a))
2420 return -EINVAL;
2421
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 /* copy_from_user should be SMP safe. */
Arjan van de Ven47379052009-09-28 12:57:44 -07002423 if (copy_from_user(a, args, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 return -EFAULT;
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002425
Al Virof3298dc2008-12-10 03:16:51 -05002426 audit_socketcall(nargs[call] / sizeof(unsigned long), a);
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002427
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002428 a0 = a[0];
2429 a1 = a[1];
2430
2431 switch (call) {
2432 case SYS_SOCKET:
2433 err = sys_socket(a0, a1, a[2]);
2434 break;
2435 case SYS_BIND:
2436 err = sys_bind(a0, (struct sockaddr __user *)a1, a[2]);
2437 break;
2438 case SYS_CONNECT:
2439 err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
2440 break;
2441 case SYS_LISTEN:
2442 err = sys_listen(a0, a1);
2443 break;
2444 case SYS_ACCEPT:
Ulrich Drepperde11def2008-11-19 15:36:14 -08002445 err = sys_accept4(a0, (struct sockaddr __user *)a1,
2446 (int __user *)a[2], 0);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002447 break;
2448 case SYS_GETSOCKNAME:
2449 err =
2450 sys_getsockname(a0, (struct sockaddr __user *)a1,
2451 (int __user *)a[2]);
2452 break;
2453 case SYS_GETPEERNAME:
2454 err =
2455 sys_getpeername(a0, (struct sockaddr __user *)a1,
2456 (int __user *)a[2]);
2457 break;
2458 case SYS_SOCKETPAIR:
2459 err = sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
2460 break;
2461 case SYS_SEND:
2462 err = sys_send(a0, (void __user *)a1, a[2], a[3]);
2463 break;
2464 case SYS_SENDTO:
2465 err = sys_sendto(a0, (void __user *)a1, a[2], a[3],
2466 (struct sockaddr __user *)a[4], a[5]);
2467 break;
2468 case SYS_RECV:
2469 err = sys_recv(a0, (void __user *)a1, a[2], a[3]);
2470 break;
2471 case SYS_RECVFROM:
2472 err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
2473 (struct sockaddr __user *)a[4],
2474 (int __user *)a[5]);
2475 break;
2476 case SYS_SHUTDOWN:
2477 err = sys_shutdown(a0, a1);
2478 break;
2479 case SYS_SETSOCKOPT:
2480 err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]);
2481 break;
2482 case SYS_GETSOCKOPT:
2483 err =
2484 sys_getsockopt(a0, a1, a[2], (char __user *)a[3],
2485 (int __user *)a[4]);
2486 break;
2487 case SYS_SENDMSG:
2488 err = sys_sendmsg(a0, (struct msghdr __user *)a1, a[2]);
2489 break;
Anton Blanchard228e5482011-05-02 20:21:35 +00002490 case SYS_SENDMMSG:
2491 err = sys_sendmmsg(a0, (struct mmsghdr __user *)a1, a[2], a[3]);
2492 break;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002493 case SYS_RECVMSG:
2494 err = sys_recvmsg(a0, (struct msghdr __user *)a1, a[2]);
2495 break;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002496 case SYS_RECVMMSG:
2497 err = sys_recvmmsg(a0, (struct mmsghdr __user *)a1, a[2], a[3],
2498 (struct timespec __user *)a[4]);
2499 break;
Ulrich Drepperde11def2008-11-19 15:36:14 -08002500 case SYS_ACCEPT4:
2501 err = sys_accept4(a0, (struct sockaddr __user *)a1,
2502 (int __user *)a[2], a[3]);
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07002503 break;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002504 default:
2505 err = -EINVAL;
2506 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 }
2508 return err;
2509}
2510
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002511#endif /* __ARCH_WANT_SYS_SOCKETCALL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002513/**
2514 * sock_register - add a socket protocol handler
2515 * @ops: description of protocol
2516 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 * This function is called by a protocol handler that wants to
2518 * advertise its address family, and have it linked into the
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002519 * socket interface. The value ops->family coresponds to the
2520 * socket system call protocol family.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 */
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002522int sock_register(const struct net_proto_family *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523{
2524 int err;
2525
2526 if (ops->family >= NPROTO) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002527 printk(KERN_CRIT "protocol %d >= NPROTO(%d)\n", ops->family,
2528 NPROTO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 return -ENOBUFS;
2530 }
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002531
2532 spin_lock(&net_family_lock);
Eric Dumazet190683a2010-11-10 10:50:44 +00002533 if (rcu_dereference_protected(net_families[ops->family],
2534 lockdep_is_held(&net_family_lock)))
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002535 err = -EEXIST;
2536 else {
Eric Dumazetcf778b02012-01-12 04:41:32 +00002537 rcu_assign_pointer(net_families[ops->family], ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 err = 0;
2539 }
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002540 spin_unlock(&net_family_lock);
2541
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002542 printk(KERN_INFO "NET: Registered protocol family %d\n", ops->family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 return err;
2544}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002545EXPORT_SYMBOL(sock_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002547/**
2548 * sock_unregister - remove a protocol handler
2549 * @family: protocol family to remove
2550 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 * This function is called by a protocol handler that wants to
2552 * remove its address family, and have it unlinked from the
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002553 * new socket creation.
2554 *
2555 * If protocol handler is a module, then it can use module reference
2556 * counts to protect against new references. If protocol handler is not
2557 * a module then it needs to provide its own protection in
2558 * the ops->create routine.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 */
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002560void sock_unregister(int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561{
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002562 BUG_ON(family < 0 || family >= NPROTO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002564 spin_lock(&net_family_lock);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00002565 RCU_INIT_POINTER(net_families[family], NULL);
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002566 spin_unlock(&net_family_lock);
2567
2568 synchronize_rcu();
2569
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002570 printk(KERN_INFO "NET: Unregistered protocol family %d\n", family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002572EXPORT_SYMBOL(sock_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573
Andi Kleen77d76ea2005-12-22 12:43:42 -08002574static int __init sock_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575{
Nick Pigginb3e19d92011-01-07 17:50:11 +11002576 int err;
2577
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002579 * Initialize sock SLAB cache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002581
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 sk_init();
2583
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002585 * Initialize skbuff SLAB cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 */
2587 skb_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588
2589 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002590 * Initialize the protocols module.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 */
2592
2593 init_inodecache();
Nick Pigginb3e19d92011-01-07 17:50:11 +11002594
2595 err = register_filesystem(&sock_fs_type);
2596 if (err)
2597 goto out_fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 sock_mnt = kern_mount(&sock_fs_type);
Nick Pigginb3e19d92011-01-07 17:50:11 +11002599 if (IS_ERR(sock_mnt)) {
2600 err = PTR_ERR(sock_mnt);
2601 goto out_mount;
2602 }
Andi Kleen77d76ea2005-12-22 12:43:42 -08002603
2604 /* The real protocol initialization is performed in later initcalls.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 */
2606
2607#ifdef CONFIG_NETFILTER
2608 netfilter_init();
2609#endif
David S. Millercbeb3212005-12-22 12:58:55 -08002610
Richard Cochranc1f19b52010-07-17 08:49:36 +00002611#ifdef CONFIG_NETWORK_PHY_TIMESTAMPING
2612 skb_timestamping_init();
2613#endif
2614
Nick Pigginb3e19d92011-01-07 17:50:11 +11002615out:
2616 return err;
2617
2618out_mount:
2619 unregister_filesystem(&sock_fs_type);
2620out_fs:
2621 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622}
2623
Andi Kleen77d76ea2005-12-22 12:43:42 -08002624core_initcall(sock_init); /* early initcall */
2625
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626#ifdef CONFIG_PROC_FS
2627void socket_seq_show(struct seq_file *seq)
2628{
2629 int cpu;
2630 int counter = 0;
2631
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -07002632 for_each_possible_cpu(cpu)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002633 counter += per_cpu(sockets_in_use, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634
2635 /* It can be negative, by the way. 8) */
2636 if (counter < 0)
2637 counter = 0;
2638
2639 seq_printf(seq, "sockets: used %d\n", counter);
2640}
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002641#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642
Shaun Pereira89bbfc92006-03-21 23:58:08 -08002643#ifdef CONFIG_COMPAT
Arnd Bergmann6b960182009-11-06 23:10:54 -08002644static int do_siocgstamp(struct net *net, struct socket *sock,
H. Peter Anvin644595f2012-02-19 17:51:59 -08002645 unsigned int cmd, void __user *up)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002646{
Arnd Bergmann7a229382009-11-06 23:00:29 -08002647 mm_segment_t old_fs = get_fs();
2648 struct timeval ktv;
2649 int err;
2650
2651 set_fs(KERNEL_DS);
Arnd Bergmann6b960182009-11-06 23:10:54 -08002652 err = sock_do_ioctl(net, sock, cmd, (unsigned long)&ktv);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002653 set_fs(old_fs);
H. Peter Anvin644595f2012-02-19 17:51:59 -08002654 if (!err)
Mikulas Patocka43da4762012-09-01 12:34:07 -04002655 err = compat_put_timeval(&ktv, up);
H. Peter Anvin644595f2012-02-19 17:51:59 -08002656
Arnd Bergmann7a229382009-11-06 23:00:29 -08002657 return err;
2658}
2659
Arnd Bergmann6b960182009-11-06 23:10:54 -08002660static int do_siocgstampns(struct net *net, struct socket *sock,
H. Peter Anvin644595f2012-02-19 17:51:59 -08002661 unsigned int cmd, void __user *up)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002662{
Arnd Bergmann7a229382009-11-06 23:00:29 -08002663 mm_segment_t old_fs = get_fs();
2664 struct timespec kts;
2665 int err;
2666
2667 set_fs(KERNEL_DS);
Arnd Bergmann6b960182009-11-06 23:10:54 -08002668 err = sock_do_ioctl(net, sock, cmd, (unsigned long)&kts);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002669 set_fs(old_fs);
H. Peter Anvin644595f2012-02-19 17:51:59 -08002670 if (!err)
Mikulas Patocka43da4762012-09-01 12:34:07 -04002671 err = compat_put_timespec(&kts, up);
H. Peter Anvin644595f2012-02-19 17:51:59 -08002672
Arnd Bergmann7a229382009-11-06 23:00:29 -08002673 return err;
2674}
2675
Arnd Bergmann6b960182009-11-06 23:10:54 -08002676static int dev_ifname32(struct net *net, struct compat_ifreq __user *uifr32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002677{
2678 struct ifreq __user *uifr;
2679 int err;
2680
2681 uifr = compat_alloc_user_space(sizeof(struct ifreq));
Arnd Bergmann6b960182009-11-06 23:10:54 -08002682 if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002683 return -EFAULT;
2684
Arnd Bergmann6b960182009-11-06 23:10:54 -08002685 err = dev_ioctl(net, SIOCGIFNAME, uifr);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002686 if (err)
2687 return err;
2688
Arnd Bergmann6b960182009-11-06 23:10:54 -08002689 if (copy_in_user(uifr32, uifr, sizeof(struct compat_ifreq)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002690 return -EFAULT;
2691
2692 return 0;
2693}
2694
Arnd Bergmann6b960182009-11-06 23:10:54 -08002695static int dev_ifconf(struct net *net, struct compat_ifconf __user *uifc32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002696{
Arnd Bergmann6b960182009-11-06 23:10:54 -08002697 struct compat_ifconf ifc32;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002698 struct ifconf ifc;
2699 struct ifconf __user *uifc;
Arnd Bergmann6b960182009-11-06 23:10:54 -08002700 struct compat_ifreq __user *ifr32;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002701 struct ifreq __user *ifr;
2702 unsigned int i, j;
2703 int err;
2704
Arnd Bergmann6b960182009-11-06 23:10:54 -08002705 if (copy_from_user(&ifc32, uifc32, sizeof(struct compat_ifconf)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002706 return -EFAULT;
2707
Mathias Kraused09b3b22012-08-15 11:31:57 +00002708 memset(&ifc, 0, sizeof(ifc));
Arnd Bergmann7a229382009-11-06 23:00:29 -08002709 if (ifc32.ifcbuf == 0) {
2710 ifc32.ifc_len = 0;
2711 ifc.ifc_len = 0;
2712 ifc.ifc_req = NULL;
2713 uifc = compat_alloc_user_space(sizeof(struct ifconf));
2714 } else {
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002715 size_t len = ((ifc32.ifc_len / sizeof(struct compat_ifreq)) + 1) *
2716 sizeof(struct ifreq);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002717 uifc = compat_alloc_user_space(sizeof(struct ifconf) + len);
2718 ifc.ifc_len = len;
2719 ifr = ifc.ifc_req = (void __user *)(uifc + 1);
2720 ifr32 = compat_ptr(ifc32.ifcbuf);
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002721 for (i = 0; i < ifc32.ifc_len; i += sizeof(struct compat_ifreq)) {
Arnd Bergmann6b960182009-11-06 23:10:54 -08002722 if (copy_in_user(ifr, ifr32, sizeof(struct compat_ifreq)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002723 return -EFAULT;
2724 ifr++;
2725 ifr32++;
2726 }
2727 }
2728 if (copy_to_user(uifc, &ifc, sizeof(struct ifconf)))
2729 return -EFAULT;
2730
Arnd Bergmann6b960182009-11-06 23:10:54 -08002731 err = dev_ioctl(net, SIOCGIFCONF, uifc);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002732 if (err)
2733 return err;
2734
2735 if (copy_from_user(&ifc, uifc, sizeof(struct ifconf)))
2736 return -EFAULT;
2737
2738 ifr = ifc.ifc_req;
2739 ifr32 = compat_ptr(ifc32.ifcbuf);
2740 for (i = 0, j = 0;
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002741 i + sizeof(struct compat_ifreq) <= ifc32.ifc_len && j < ifc.ifc_len;
2742 i += sizeof(struct compat_ifreq), j += sizeof(struct ifreq)) {
2743 if (copy_in_user(ifr32, ifr, sizeof(struct compat_ifreq)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002744 return -EFAULT;
2745 ifr32++;
2746 ifr++;
2747 }
2748
2749 if (ifc32.ifcbuf == 0) {
2750 /* Translate from 64-bit structure multiple to
2751 * a 32-bit one.
2752 */
2753 i = ifc.ifc_len;
Arnd Bergmann6b960182009-11-06 23:10:54 -08002754 i = ((i / sizeof(struct ifreq)) * sizeof(struct compat_ifreq));
Arnd Bergmann7a229382009-11-06 23:00:29 -08002755 ifc32.ifc_len = i;
2756 } else {
2757 ifc32.ifc_len = i;
2758 }
Arnd Bergmann6b960182009-11-06 23:10:54 -08002759 if (copy_to_user(uifc32, &ifc32, sizeof(struct compat_ifconf)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002760 return -EFAULT;
2761
2762 return 0;
2763}
2764
Arnd Bergmann6b960182009-11-06 23:10:54 -08002765static int ethtool_ioctl(struct net *net, struct compat_ifreq __user *ifr32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002766{
Ben Hutchings3a7da392011-03-17 07:34:32 +00002767 struct compat_ethtool_rxnfc __user *compat_rxnfc;
2768 bool convert_in = false, convert_out = false;
2769 size_t buf_size = ALIGN(sizeof(struct ifreq), 8);
2770 struct ethtool_rxnfc __user *rxnfc;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002771 struct ifreq __user *ifr;
Ben Hutchings3a7da392011-03-17 07:34:32 +00002772 u32 rule_cnt = 0, actual_rule_cnt;
2773 u32 ethcmd;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002774 u32 data;
Ben Hutchings3a7da392011-03-17 07:34:32 +00002775 int ret;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002776
2777 if (get_user(data, &ifr32->ifr_ifru.ifru_data))
2778 return -EFAULT;
2779
Ben Hutchings3a7da392011-03-17 07:34:32 +00002780 compat_rxnfc = compat_ptr(data);
2781
2782 if (get_user(ethcmd, &compat_rxnfc->cmd))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002783 return -EFAULT;
2784
Ben Hutchings3a7da392011-03-17 07:34:32 +00002785 /* Most ethtool structures are defined without padding.
2786 * Unfortunately struct ethtool_rxnfc is an exception.
2787 */
2788 switch (ethcmd) {
2789 default:
2790 break;
2791 case ETHTOOL_GRXCLSRLALL:
2792 /* Buffer size is variable */
2793 if (get_user(rule_cnt, &compat_rxnfc->rule_cnt))
2794 return -EFAULT;
2795 if (rule_cnt > KMALLOC_MAX_SIZE / sizeof(u32))
2796 return -ENOMEM;
2797 buf_size += rule_cnt * sizeof(u32);
2798 /* fall through */
2799 case ETHTOOL_GRXRINGS:
2800 case ETHTOOL_GRXCLSRLCNT:
2801 case ETHTOOL_GRXCLSRULE:
Ben Hutchings55664f32012-01-03 12:04:51 +00002802 case ETHTOOL_SRXCLSRLINS:
Ben Hutchings3a7da392011-03-17 07:34:32 +00002803 convert_out = true;
2804 /* fall through */
2805 case ETHTOOL_SRXCLSRLDEL:
Ben Hutchings3a7da392011-03-17 07:34:32 +00002806 buf_size += sizeof(struct ethtool_rxnfc);
2807 convert_in = true;
2808 break;
2809 }
2810
2811 ifr = compat_alloc_user_space(buf_size);
2812 rxnfc = (void *)ifr + ALIGN(sizeof(struct ifreq), 8);
2813
2814 if (copy_in_user(&ifr->ifr_name, &ifr32->ifr_name, IFNAMSIZ))
2815 return -EFAULT;
2816
2817 if (put_user(convert_in ? rxnfc : compat_ptr(data),
2818 &ifr->ifr_ifru.ifru_data))
2819 return -EFAULT;
2820
2821 if (convert_in) {
Alexander Duyck127fe532011-04-08 18:01:59 +00002822 /* We expect there to be holes between fs.m_ext and
Ben Hutchings3a7da392011-03-17 07:34:32 +00002823 * fs.ring_cookie and at the end of fs, but nowhere else.
2824 */
Alexander Duyck127fe532011-04-08 18:01:59 +00002825 BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc, fs.m_ext) +
2826 sizeof(compat_rxnfc->fs.m_ext) !=
2827 offsetof(struct ethtool_rxnfc, fs.m_ext) +
2828 sizeof(rxnfc->fs.m_ext));
Ben Hutchings3a7da392011-03-17 07:34:32 +00002829 BUILD_BUG_ON(
2830 offsetof(struct compat_ethtool_rxnfc, fs.location) -
2831 offsetof(struct compat_ethtool_rxnfc, fs.ring_cookie) !=
2832 offsetof(struct ethtool_rxnfc, fs.location) -
2833 offsetof(struct ethtool_rxnfc, fs.ring_cookie));
2834
2835 if (copy_in_user(rxnfc, compat_rxnfc,
Alexander Duyck127fe532011-04-08 18:01:59 +00002836 (void *)(&rxnfc->fs.m_ext + 1) -
Ben Hutchings3a7da392011-03-17 07:34:32 +00002837 (void *)rxnfc) ||
2838 copy_in_user(&rxnfc->fs.ring_cookie,
2839 &compat_rxnfc->fs.ring_cookie,
2840 (void *)(&rxnfc->fs.location + 1) -
2841 (void *)&rxnfc->fs.ring_cookie) ||
2842 copy_in_user(&rxnfc->rule_cnt, &compat_rxnfc->rule_cnt,
2843 sizeof(rxnfc->rule_cnt)))
2844 return -EFAULT;
2845 }
2846
2847 ret = dev_ioctl(net, SIOCETHTOOL, ifr);
2848 if (ret)
2849 return ret;
2850
2851 if (convert_out) {
2852 if (copy_in_user(compat_rxnfc, rxnfc,
Alexander Duyck127fe532011-04-08 18:01:59 +00002853 (const void *)(&rxnfc->fs.m_ext + 1) -
Ben Hutchings3a7da392011-03-17 07:34:32 +00002854 (const void *)rxnfc) ||
2855 copy_in_user(&compat_rxnfc->fs.ring_cookie,
2856 &rxnfc->fs.ring_cookie,
2857 (const void *)(&rxnfc->fs.location + 1) -
2858 (const void *)&rxnfc->fs.ring_cookie) ||
2859 copy_in_user(&compat_rxnfc->rule_cnt, &rxnfc->rule_cnt,
2860 sizeof(rxnfc->rule_cnt)))
2861 return -EFAULT;
2862
2863 if (ethcmd == ETHTOOL_GRXCLSRLALL) {
2864 /* As an optimisation, we only copy the actual
2865 * number of rules that the underlying
2866 * function returned. Since Mallory might
2867 * change the rule count in user memory, we
2868 * check that it is less than the rule count
2869 * originally given (as the user buffer size),
2870 * which has been range-checked.
2871 */
2872 if (get_user(actual_rule_cnt, &rxnfc->rule_cnt))
2873 return -EFAULT;
2874 if (actual_rule_cnt < rule_cnt)
2875 rule_cnt = actual_rule_cnt;
2876 if (copy_in_user(&compat_rxnfc->rule_locs[0],
2877 &rxnfc->rule_locs[0],
2878 rule_cnt * sizeof(u32)))
2879 return -EFAULT;
2880 }
2881 }
2882
2883 return 0;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002884}
2885
Arnd Bergmann7a50a242009-11-08 20:57:03 -08002886static int compat_siocwandev(struct net *net, struct compat_ifreq __user *uifr32)
2887{
2888 void __user *uptr;
2889 compat_uptr_t uptr32;
2890 struct ifreq __user *uifr;
2891
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002892 uifr = compat_alloc_user_space(sizeof(*uifr));
Arnd Bergmann7a50a242009-11-08 20:57:03 -08002893 if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
2894 return -EFAULT;
2895
2896 if (get_user(uptr32, &uifr32->ifr_settings.ifs_ifsu))
2897 return -EFAULT;
2898
2899 uptr = compat_ptr(uptr32);
2900
2901 if (put_user(uptr, &uifr->ifr_settings.ifs_ifsu.raw_hdlc))
2902 return -EFAULT;
2903
2904 return dev_ioctl(net, SIOCWANDEV, uifr);
2905}
2906
Arnd Bergmann6b960182009-11-06 23:10:54 -08002907static int bond_ioctl(struct net *net, unsigned int cmd,
2908 struct compat_ifreq __user *ifr32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002909{
2910 struct ifreq kifr;
2911 struct ifreq __user *uifr;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002912 mm_segment_t old_fs;
2913 int err;
2914 u32 data;
2915 void __user *datap;
2916
2917 switch (cmd) {
2918 case SIOCBONDENSLAVE:
2919 case SIOCBONDRELEASE:
2920 case SIOCBONDSETHWADDR:
2921 case SIOCBONDCHANGEACTIVE:
Arnd Bergmann6b960182009-11-06 23:10:54 -08002922 if (copy_from_user(&kifr, ifr32, sizeof(struct compat_ifreq)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002923 return -EFAULT;
2924
2925 old_fs = get_fs();
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002926 set_fs(KERNEL_DS);
stephen hemmingerc3f52ae2011-02-23 09:06:48 +00002927 err = dev_ioctl(net, cmd,
2928 (struct ifreq __user __force *) &kifr);
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002929 set_fs(old_fs);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002930
2931 return err;
2932 case SIOCBONDSLAVEINFOQUERY:
2933 case SIOCBONDINFOQUERY:
2934 uifr = compat_alloc_user_space(sizeof(*uifr));
2935 if (copy_in_user(&uifr->ifr_name, &ifr32->ifr_name, IFNAMSIZ))
2936 return -EFAULT;
2937
2938 if (get_user(data, &ifr32->ifr_ifru.ifru_data))
2939 return -EFAULT;
2940
2941 datap = compat_ptr(data);
2942 if (put_user(datap, &uifr->ifr_ifru.ifru_data))
2943 return -EFAULT;
2944
Arnd Bergmann6b960182009-11-06 23:10:54 -08002945 return dev_ioctl(net, cmd, uifr);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002946 default:
Linus Torvalds07d106d2012-01-05 15:40:12 -08002947 return -ENOIOCTLCMD;
Joe Perchesccbd6a52010-05-14 10:58:26 +00002948 }
Arnd Bergmann7a229382009-11-06 23:00:29 -08002949}
2950
Arnd Bergmann6b960182009-11-06 23:10:54 -08002951static int siocdevprivate_ioctl(struct net *net, unsigned int cmd,
2952 struct compat_ifreq __user *u_ifreq32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002953{
2954 struct ifreq __user *u_ifreq64;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002955 char tmp_buf[IFNAMSIZ];
2956 void __user *data64;
2957 u32 data32;
2958
2959 if (copy_from_user(&tmp_buf[0], &(u_ifreq32->ifr_ifrn.ifrn_name[0]),
2960 IFNAMSIZ))
2961 return -EFAULT;
2962 if (__get_user(data32, &u_ifreq32->ifr_ifru.ifru_data))
2963 return -EFAULT;
2964 data64 = compat_ptr(data32);
2965
2966 u_ifreq64 = compat_alloc_user_space(sizeof(*u_ifreq64));
2967
2968 /* Don't check these user accesses, just let that get trapped
2969 * in the ioctl handler instead.
2970 */
2971 if (copy_to_user(&u_ifreq64->ifr_ifrn.ifrn_name[0], &tmp_buf[0],
2972 IFNAMSIZ))
2973 return -EFAULT;
2974 if (__put_user(data64, &u_ifreq64->ifr_ifru.ifru_data))
2975 return -EFAULT;
2976
Arnd Bergmann6b960182009-11-06 23:10:54 -08002977 return dev_ioctl(net, cmd, u_ifreq64);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002978}
2979
Arnd Bergmann6b960182009-11-06 23:10:54 -08002980static int dev_ifsioc(struct net *net, struct socket *sock,
2981 unsigned int cmd, struct compat_ifreq __user *uifr32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002982{
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00002983 struct ifreq __user *uifr;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002984 int err;
2985
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00002986 uifr = compat_alloc_user_space(sizeof(*uifr));
2987 if (copy_in_user(uifr, uifr32, sizeof(*uifr32)))
2988 return -EFAULT;
2989
2990 err = sock_do_ioctl(net, sock, cmd, (unsigned long)uifr);
2991
Arnd Bergmann7a229382009-11-06 23:00:29 -08002992 if (!err) {
2993 switch (cmd) {
2994 case SIOCGIFFLAGS:
2995 case SIOCGIFMETRIC:
2996 case SIOCGIFMTU:
2997 case SIOCGIFMEM:
2998 case SIOCGIFHWADDR:
2999 case SIOCGIFINDEX:
3000 case SIOCGIFADDR:
3001 case SIOCGIFBRDADDR:
3002 case SIOCGIFDSTADDR:
3003 case SIOCGIFNETMASK:
Arnd Bergmannfab25322009-11-08 20:56:21 -08003004 case SIOCGIFPFLAGS:
Arnd Bergmann7a229382009-11-06 23:00:29 -08003005 case SIOCGIFTXQLEN:
Arnd Bergmannfab25322009-11-08 20:56:21 -08003006 case SIOCGMIIPHY:
3007 case SIOCGMIIREG:
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003008 if (copy_in_user(uifr32, uifr, sizeof(*uifr32)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08003009 err = -EFAULT;
3010 break;
3011 }
3012 }
3013 return err;
3014}
3015
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003016static int compat_sioc_ifmap(struct net *net, unsigned int cmd,
3017 struct compat_ifreq __user *uifr32)
3018{
3019 struct ifreq ifr;
3020 struct compat_ifmap __user *uifmap32;
3021 mm_segment_t old_fs;
3022 int err;
3023
3024 uifmap32 = &uifr32->ifr_ifru.ifru_map;
3025 err = copy_from_user(&ifr, uifr32, sizeof(ifr.ifr_name));
3026 err |= __get_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
3027 err |= __get_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
3028 err |= __get_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
3029 err |= __get_user(ifr.ifr_map.irq, &uifmap32->irq);
3030 err |= __get_user(ifr.ifr_map.dma, &uifmap32->dma);
3031 err |= __get_user(ifr.ifr_map.port, &uifmap32->port);
3032 if (err)
3033 return -EFAULT;
3034
3035 old_fs = get_fs();
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003036 set_fs(KERNEL_DS);
stephen hemmingerc3f52ae2011-02-23 09:06:48 +00003037 err = dev_ioctl(net, cmd, (void __user __force *)&ifr);
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003038 set_fs(old_fs);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003039
3040 if (cmd == SIOCGIFMAP && !err) {
3041 err = copy_to_user(uifr32, &ifr, sizeof(ifr.ifr_name));
3042 err |= __put_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
3043 err |= __put_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
3044 err |= __put_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
3045 err |= __put_user(ifr.ifr_map.irq, &uifmap32->irq);
3046 err |= __put_user(ifr.ifr_map.dma, &uifmap32->dma);
3047 err |= __put_user(ifr.ifr_map.port, &uifmap32->port);
3048 if (err)
3049 err = -EFAULT;
3050 }
3051 return err;
3052}
3053
3054static int compat_siocshwtstamp(struct net *net, struct compat_ifreq __user *uifr32)
3055{
3056 void __user *uptr;
3057 compat_uptr_t uptr32;
3058 struct ifreq __user *uifr;
3059
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003060 uifr = compat_alloc_user_space(sizeof(*uifr));
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003061 if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
3062 return -EFAULT;
3063
3064 if (get_user(uptr32, &uifr32->ifr_data))
3065 return -EFAULT;
3066
3067 uptr = compat_ptr(uptr32);
3068
3069 if (put_user(uptr, &uifr->ifr_data))
3070 return -EFAULT;
3071
3072 return dev_ioctl(net, SIOCSHWTSTAMP, uifr);
3073}
3074
Arnd Bergmann7a229382009-11-06 23:00:29 -08003075struct rtentry32 {
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003076 u32 rt_pad1;
Arnd Bergmann7a229382009-11-06 23:00:29 -08003077 struct sockaddr rt_dst; /* target address */
3078 struct sockaddr rt_gateway; /* gateway addr (RTF_GATEWAY) */
3079 struct sockaddr rt_genmask; /* target network mask (IP) */
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003080 unsigned short rt_flags;
3081 short rt_pad2;
3082 u32 rt_pad3;
3083 unsigned char rt_tos;
3084 unsigned char rt_class;
3085 short rt_pad4;
3086 short rt_metric; /* +1 for binary compatibility! */
Arnd Bergmann7a229382009-11-06 23:00:29 -08003087 /* char * */ u32 rt_dev; /* forcing the device at add */
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003088 u32 rt_mtu; /* per route MTU/Window */
3089 u32 rt_window; /* Window clamping */
Arnd Bergmann7a229382009-11-06 23:00:29 -08003090 unsigned short rt_irtt; /* Initial RTT */
3091};
3092
3093struct in6_rtmsg32 {
3094 struct in6_addr rtmsg_dst;
3095 struct in6_addr rtmsg_src;
3096 struct in6_addr rtmsg_gateway;
3097 u32 rtmsg_type;
3098 u16 rtmsg_dst_len;
3099 u16 rtmsg_src_len;
3100 u32 rtmsg_metric;
3101 u32 rtmsg_info;
3102 u32 rtmsg_flags;
3103 s32 rtmsg_ifindex;
3104};
3105
Arnd Bergmann6b960182009-11-06 23:10:54 -08003106static int routing_ioctl(struct net *net, struct socket *sock,
3107 unsigned int cmd, void __user *argp)
Arnd Bergmann7a229382009-11-06 23:00:29 -08003108{
3109 int ret;
3110 void *r = NULL;
3111 struct in6_rtmsg r6;
3112 struct rtentry r4;
3113 char devname[16];
3114 u32 rtdev;
3115 mm_segment_t old_fs = get_fs();
3116
Arnd Bergmann6b960182009-11-06 23:10:54 -08003117 if (sock && sock->sk && sock->sk->sk_family == AF_INET6) { /* ipv6 */
3118 struct in6_rtmsg32 __user *ur6 = argp;
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003119 ret = copy_from_user(&r6.rtmsg_dst, &(ur6->rtmsg_dst),
Arnd Bergmann7a229382009-11-06 23:00:29 -08003120 3 * sizeof(struct in6_addr));
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003121 ret |= __get_user(r6.rtmsg_type, &(ur6->rtmsg_type));
3122 ret |= __get_user(r6.rtmsg_dst_len, &(ur6->rtmsg_dst_len));
3123 ret |= __get_user(r6.rtmsg_src_len, &(ur6->rtmsg_src_len));
3124 ret |= __get_user(r6.rtmsg_metric, &(ur6->rtmsg_metric));
3125 ret |= __get_user(r6.rtmsg_info, &(ur6->rtmsg_info));
3126 ret |= __get_user(r6.rtmsg_flags, &(ur6->rtmsg_flags));
3127 ret |= __get_user(r6.rtmsg_ifindex, &(ur6->rtmsg_ifindex));
Arnd Bergmann7a229382009-11-06 23:00:29 -08003128
3129 r = (void *) &r6;
3130 } else { /* ipv4 */
Arnd Bergmann6b960182009-11-06 23:10:54 -08003131 struct rtentry32 __user *ur4 = argp;
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003132 ret = copy_from_user(&r4.rt_dst, &(ur4->rt_dst),
Arnd Bergmann7a229382009-11-06 23:00:29 -08003133 3 * sizeof(struct sockaddr));
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003134 ret |= __get_user(r4.rt_flags, &(ur4->rt_flags));
3135 ret |= __get_user(r4.rt_metric, &(ur4->rt_metric));
3136 ret |= __get_user(r4.rt_mtu, &(ur4->rt_mtu));
3137 ret |= __get_user(r4.rt_window, &(ur4->rt_window));
3138 ret |= __get_user(r4.rt_irtt, &(ur4->rt_irtt));
3139 ret |= __get_user(rtdev, &(ur4->rt_dev));
Arnd Bergmann7a229382009-11-06 23:00:29 -08003140 if (rtdev) {
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003141 ret |= copy_from_user(devname, compat_ptr(rtdev), 15);
stephen hemmingerc3f52ae2011-02-23 09:06:48 +00003142 r4.rt_dev = (char __user __force *)devname;
3143 devname[15] = 0;
Arnd Bergmann7a229382009-11-06 23:00:29 -08003144 } else
3145 r4.rt_dev = NULL;
3146
3147 r = (void *) &r4;
3148 }
3149
3150 if (ret) {
3151 ret = -EFAULT;
3152 goto out;
3153 }
3154
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003155 set_fs(KERNEL_DS);
Arnd Bergmann6b960182009-11-06 23:10:54 -08003156 ret = sock_do_ioctl(net, sock, cmd, (unsigned long) r);
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003157 set_fs(old_fs);
Arnd Bergmann7a229382009-11-06 23:00:29 -08003158
3159out:
Arnd Bergmann7a229382009-11-06 23:00:29 -08003160 return ret;
3161}
3162
3163/* Since old style bridge ioctl's endup using SIOCDEVPRIVATE
3164 * for some operations; this forces use of the newer bridge-utils that
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003165 * use compatible ioctls
Arnd Bergmann7a229382009-11-06 23:00:29 -08003166 */
Arnd Bergmann6b960182009-11-06 23:10:54 -08003167static int old_bridge_ioctl(compat_ulong_t __user *argp)
Arnd Bergmann7a229382009-11-06 23:00:29 -08003168{
Arnd Bergmann6b960182009-11-06 23:10:54 -08003169 compat_ulong_t tmp;
Arnd Bergmann7a229382009-11-06 23:00:29 -08003170
Arnd Bergmann6b960182009-11-06 23:10:54 -08003171 if (get_user(tmp, argp))
Arnd Bergmann7a229382009-11-06 23:00:29 -08003172 return -EFAULT;
3173 if (tmp == BRCTL_GET_VERSION)
3174 return BRCTL_VERSION + 1;
3175 return -EINVAL;
3176}
3177
Arnd Bergmann6b960182009-11-06 23:10:54 -08003178static int compat_sock_ioctl_trans(struct file *file, struct socket *sock,
3179 unsigned int cmd, unsigned long arg)
3180{
3181 void __user *argp = compat_ptr(arg);
3182 struct sock *sk = sock->sk;
3183 struct net *net = sock_net(sk);
Arnd Bergmann7a229382009-11-06 23:00:29 -08003184
Arnd Bergmann6b960182009-11-06 23:10:54 -08003185 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15))
3186 return siocdevprivate_ioctl(net, cmd, argp);
Arnd Bergmann7a229382009-11-06 23:00:29 -08003187
Arnd Bergmann6b960182009-11-06 23:10:54 -08003188 switch (cmd) {
3189 case SIOCSIFBR:
3190 case SIOCGIFBR:
3191 return old_bridge_ioctl(argp);
3192 case SIOCGIFNAME:
3193 return dev_ifname32(net, argp);
3194 case SIOCGIFCONF:
3195 return dev_ifconf(net, argp);
3196 case SIOCETHTOOL:
3197 return ethtool_ioctl(net, argp);
Arnd Bergmann7a50a242009-11-08 20:57:03 -08003198 case SIOCWANDEV:
3199 return compat_siocwandev(net, argp);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003200 case SIOCGIFMAP:
3201 case SIOCSIFMAP:
3202 return compat_sioc_ifmap(net, cmd, argp);
Arnd Bergmann6b960182009-11-06 23:10:54 -08003203 case SIOCBONDENSLAVE:
3204 case SIOCBONDRELEASE:
3205 case SIOCBONDSETHWADDR:
3206 case SIOCBONDSLAVEINFOQUERY:
3207 case SIOCBONDINFOQUERY:
3208 case SIOCBONDCHANGEACTIVE:
3209 return bond_ioctl(net, cmd, argp);
3210 case SIOCADDRT:
3211 case SIOCDELRT:
3212 return routing_ioctl(net, sock, cmd, argp);
3213 case SIOCGSTAMP:
3214 return do_siocgstamp(net, sock, cmd, argp);
3215 case SIOCGSTAMPNS:
3216 return do_siocgstampns(net, sock, cmd, argp);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003217 case SIOCSHWTSTAMP:
3218 return compat_siocshwtstamp(net, argp);
Arnd Bergmann7a229382009-11-06 23:00:29 -08003219
Arnd Bergmann6b960182009-11-06 23:10:54 -08003220 case FIOSETOWN:
3221 case SIOCSPGRP:
3222 case FIOGETOWN:
3223 case SIOCGPGRP:
3224 case SIOCBRADDBR:
3225 case SIOCBRDELBR:
3226 case SIOCGIFVLAN:
3227 case SIOCSIFVLAN:
3228 case SIOCADDDLCI:
3229 case SIOCDELDLCI:
3230 return sock_ioctl(file, cmd, arg);
3231
3232 case SIOCGIFFLAGS:
3233 case SIOCSIFFLAGS:
3234 case SIOCGIFMETRIC:
3235 case SIOCSIFMETRIC:
3236 case SIOCGIFMTU:
3237 case SIOCSIFMTU:
3238 case SIOCGIFMEM:
3239 case SIOCSIFMEM:
3240 case SIOCGIFHWADDR:
3241 case SIOCSIFHWADDR:
3242 case SIOCADDMULTI:
3243 case SIOCDELMULTI:
3244 case SIOCGIFINDEX:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003245 case SIOCGIFADDR:
3246 case SIOCSIFADDR:
3247 case SIOCSIFHWBROADCAST:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003248 case SIOCDIFADDR:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003249 case SIOCGIFBRDADDR:
3250 case SIOCSIFBRDADDR:
3251 case SIOCGIFDSTADDR:
3252 case SIOCSIFDSTADDR:
3253 case SIOCGIFNETMASK:
3254 case SIOCSIFNETMASK:
3255 case SIOCSIFPFLAGS:
3256 case SIOCGIFPFLAGS:
3257 case SIOCGIFTXQLEN:
3258 case SIOCSIFTXQLEN:
3259 case SIOCBRADDIF:
3260 case SIOCBRDELIF:
Arnd Bergmann9177efd2009-11-06 08:09:09 +00003261 case SIOCSIFNAME:
3262 case SIOCGMIIPHY:
3263 case SIOCGMIIREG:
3264 case SIOCSMIIREG:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003265 return dev_ifsioc(net, sock, cmd, argp);
Arnd Bergmann9177efd2009-11-06 08:09:09 +00003266
Arnd Bergmann6b960182009-11-06 23:10:54 -08003267 case SIOCSARP:
3268 case SIOCGARP:
3269 case SIOCDARP:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003270 case SIOCATMARK:
Arnd Bergmann9177efd2009-11-06 08:09:09 +00003271 return sock_do_ioctl(net, sock, cmd, arg);
3272 }
3273
Arnd Bergmann6b960182009-11-06 23:10:54 -08003274 return -ENOIOCTLCMD;
3275}
Arnd Bergmann7a229382009-11-06 23:00:29 -08003276
Shaun Pereira89bbfc92006-03-21 23:58:08 -08003277static long compat_sock_ioctl(struct file *file, unsigned cmd,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07003278 unsigned long arg)
Shaun Pereira89bbfc92006-03-21 23:58:08 -08003279{
3280 struct socket *sock = file->private_data;
3281 int ret = -ENOIOCTLCMD;
David S. Miller87de87d2008-06-03 09:14:03 -07003282 struct sock *sk;
3283 struct net *net;
3284
3285 sk = sock->sk;
3286 net = sock_net(sk);
Shaun Pereira89bbfc92006-03-21 23:58:08 -08003287
3288 if (sock->ops->compat_ioctl)
3289 ret = sock->ops->compat_ioctl(sock, cmd, arg);
3290
David S. Miller87de87d2008-06-03 09:14:03 -07003291 if (ret == -ENOIOCTLCMD &&
3292 (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST))
3293 ret = compat_wext_handle_ioctl(net, cmd, arg);
3294
Arnd Bergmann6b960182009-11-06 23:10:54 -08003295 if (ret == -ENOIOCTLCMD)
3296 ret = compat_sock_ioctl_trans(file, sock, cmd, arg);
3297
Shaun Pereira89bbfc92006-03-21 23:58:08 -08003298 return ret;
3299}
3300#endif
3301
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003302int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen)
3303{
3304 return sock->ops->bind(sock, addr, addrlen);
3305}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003306EXPORT_SYMBOL(kernel_bind);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003307
3308int kernel_listen(struct socket *sock, int backlog)
3309{
3310 return sock->ops->listen(sock, backlog);
3311}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003312EXPORT_SYMBOL(kernel_listen);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003313
3314int kernel_accept(struct socket *sock, struct socket **newsock, int flags)
3315{
3316 struct sock *sk = sock->sk;
3317 int err;
3318
3319 err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
3320 newsock);
3321 if (err < 0)
3322 goto done;
3323
3324 err = sock->ops->accept(sock, *newsock, flags);
3325 if (err < 0) {
3326 sock_release(*newsock);
Tony Battersbyfa8705b2007-10-10 21:09:04 -07003327 *newsock = NULL;
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003328 goto done;
3329 }
3330
3331 (*newsock)->ops = sock->ops;
Wei Yongjun1b085342008-12-18 19:35:10 -08003332 __module_get((*newsock)->ops->owner);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003333
3334done:
3335 return err;
3336}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003337EXPORT_SYMBOL(kernel_accept);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003338
3339int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
YOSHIFUJI Hideaki4768fbc2007-02-09 23:25:31 +09003340 int flags)
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003341{
3342 return sock->ops->connect(sock, addr, addrlen, flags);
3343}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003344EXPORT_SYMBOL(kernel_connect);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003345
3346int kernel_getsockname(struct socket *sock, struct sockaddr *addr,
3347 int *addrlen)
3348{
3349 return sock->ops->getname(sock, addr, addrlen, 0);
3350}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003351EXPORT_SYMBOL(kernel_getsockname);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003352
3353int kernel_getpeername(struct socket *sock, struct sockaddr *addr,
3354 int *addrlen)
3355{
3356 return sock->ops->getname(sock, addr, addrlen, 1);
3357}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003358EXPORT_SYMBOL(kernel_getpeername);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003359
3360int kernel_getsockopt(struct socket *sock, int level, int optname,
3361 char *optval, int *optlen)
3362{
3363 mm_segment_t oldfs = get_fs();
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003364 char __user *uoptval;
3365 int __user *uoptlen;
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003366 int err;
3367
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003368 uoptval = (char __user __force *) optval;
3369 uoptlen = (int __user __force *) optlen;
3370
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003371 set_fs(KERNEL_DS);
3372 if (level == SOL_SOCKET)
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003373 err = sock_getsockopt(sock, level, optname, uoptval, uoptlen);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003374 else
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003375 err = sock->ops->getsockopt(sock, level, optname, uoptval,
3376 uoptlen);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003377 set_fs(oldfs);
3378 return err;
3379}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003380EXPORT_SYMBOL(kernel_getsockopt);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003381
3382int kernel_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07003383 char *optval, unsigned int optlen)
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003384{
3385 mm_segment_t oldfs = get_fs();
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003386 char __user *uoptval;
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003387 int err;
3388
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003389 uoptval = (char __user __force *) optval;
3390
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003391 set_fs(KERNEL_DS);
3392 if (level == SOL_SOCKET)
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003393 err = sock_setsockopt(sock, level, optname, uoptval, optlen);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003394 else
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003395 err = sock->ops->setsockopt(sock, level, optname, uoptval,
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003396 optlen);
3397 set_fs(oldfs);
3398 return err;
3399}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003400EXPORT_SYMBOL(kernel_setsockopt);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003401
3402int kernel_sendpage(struct socket *sock, struct page *page, int offset,
3403 size_t size, int flags)
3404{
Herbert Xuf8451722010-05-24 00:12:34 -07003405 sock_update_classid(sock->sk);
3406
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003407 if (sock->ops->sendpage)
3408 return sock->ops->sendpage(sock, page, offset, size, flags);
3409
3410 return sock_no_sendpage(sock, page, offset, size, flags);
3411}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003412EXPORT_SYMBOL(kernel_sendpage);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003413
3414int kernel_sock_ioctl(struct socket *sock, int cmd, unsigned long arg)
3415{
3416 mm_segment_t oldfs = get_fs();
3417 int err;
3418
3419 set_fs(KERNEL_DS);
3420 err = sock->ops->ioctl(sock, cmd, arg);
3421 set_fs(oldfs);
3422
3423 return err;
3424}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003425EXPORT_SYMBOL(kernel_sock_ioctl);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003426
Trond Myklebust91cf45f2007-11-12 18:10:39 -08003427int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how)
3428{
3429 return sock->ops->shutdown(sock, how);
3430}
Trond Myklebust91cf45f2007-11-12 18:10:39 -08003431EXPORT_SYMBOL(kernel_sock_shutdown);