blob: ee0d029e51306c1f74ba5d4e4bcc61327dd32e4f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * NET An implementation of the SOCKET network access protocol.
3 *
4 * Version: @(#)socket.c 1.1.93 18/02/95
5 *
6 * Authors: Orest Zborowski, <obz@Kodak.COM>
Jesper Juhl02c30a82005-05-05 16:16:16 -07007 * Ross Biro
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
9 *
10 * Fixes:
11 * Anonymous : NOTSOCK/BADF cleanup. Error fix in
12 * shutdown()
13 * Alan Cox : verify_area() fixes
14 * Alan Cox : Removed DDI
15 * Jonathan Kamens : SOCK_DGRAM reconnect bug
16 * Alan Cox : Moved a load of checks to the very
17 * top level.
18 * Alan Cox : Move address structures to/from user
19 * mode above the protocol layers.
20 * Rob Janssen : Allow 0 length sends.
21 * Alan Cox : Asynchronous I/O support (cribbed from the
22 * tty drivers).
23 * Niibe Yutaka : Asynchronous I/O for writes (4.4BSD style)
24 * Jeff Uphoff : Made max number of sockets command-line
25 * configurable.
26 * Matti Aarnio : Made the number of sockets dynamic,
27 * to be allocated when needed, and mr.
28 * Uphoff's max is used as max to be
29 * allowed to allocate.
30 * Linus : Argh. removed all the socket allocation
31 * altogether: it's in the inode now.
32 * Alan Cox : Made sock_alloc()/sock_release() public
33 * for NetROM and future kernel nfsd type
34 * stuff.
35 * Alan Cox : sendmsg/recvmsg basics.
36 * Tom Dyas : Export net symbols.
37 * Marcin Dalecki : Fixed problems with CONFIG_NET="n".
38 * Alan Cox : Added thread locking to sys_* calls
39 * for sockets. May have errors at the
40 * moment.
41 * Kevin Buhr : Fixed the dumb errors in the above.
42 * Andi Kleen : Some small cleanups, optimizations,
43 * and fixed a copy_from_user() bug.
44 * Tigran Aivazian : sys_send(args) calls sys_sendto(args, NULL, 0)
Stephen Hemminger89bddce2006-09-01 00:19:31 -070045 * Tigran Aivazian : Made listen(2) backlog sanity checks
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 * protocol-independent
47 *
48 *
49 * This program is free software; you can redistribute it and/or
50 * modify it under the terms of the GNU General Public License
51 * as published by the Free Software Foundation; either version
52 * 2 of the License, or (at your option) any later version.
53 *
54 *
55 * This module is effectively the top level interface to the BSD socket
Stephen Hemminger89bddce2006-09-01 00:19:31 -070056 * paradigm.
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 *
58 * Based upon Swansea University Computer Society NET3.039
59 */
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <linux/socket.h>
63#include <linux/file.h>
64#include <linux/net.h>
65#include <linux/interrupt.h>
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -070066#include <linux/thread_info.h>
Stephen Hemminger55737fd2006-09-01 00:23:39 -070067#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/netdevice.h>
69#include <linux/proc_fs.h>
70#include <linux/seq_file.h>
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080071#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include <linux/if_bridge.h>
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -030073#include <linux/if_frad.h>
74#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#include <linux/init.h>
76#include <linux/poll.h>
77#include <linux/cache.h>
78#include <linux/module.h>
79#include <linux/highmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#include <linux/mount.h>
81#include <linux/security.h>
82#include <linux/syscalls.h>
83#include <linux/compat.h>
84#include <linux/kmod.h>
David Woodhouse3ec3b2f2005-05-17 12:08:48 +010085#include <linux/audit.h>
Adrian Bunkd86b5e02006-01-21 00:46:55 +010086#include <linux/wireless.h>
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -070087#include <linux/nsproxy.h>
Nick Black1fd7317d2009-09-22 16:43:33 -070088#include <linux/magic.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090089#include <linux/slab.h>
Masatake YAMATO600e1772012-08-29 10:44:29 +000090#include <linux/xattr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92#include <asm/uaccess.h>
93#include <asm/unistd.h>
94
95#include <net/compat.h>
David S. Miller87de87d2008-06-03 09:14:03 -070096#include <net/wext.h>
Herbert Xuf8451722010-05-24 00:12:34 -070097#include <net/cls_cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99#include <net/sock.h>
100#include <linux/netfilter.h>
101
Arnd Bergmann6b960182009-11-06 23:10:54 -0800102#include <linux/if_tun.h>
103#include <linux/ipv6_route.h>
104#include <linux/route.h>
Arnd Bergmann6b960182009-11-06 23:10:54 -0800105#include <linux/sockios.h>
106#include <linux/atalk.h>
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108static int sock_no_open(struct inode *irrelevant, struct file *dontcare);
Badari Pulavarty027445c2006-09-30 23:28:46 -0700109static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov,
110 unsigned long nr_segs, loff_t pos);
111static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov,
112 unsigned long nr_segs, loff_t pos);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700113static int sock_mmap(struct file *file, struct vm_area_struct *vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115static int sock_close(struct inode *inode, struct file *file);
116static unsigned int sock_poll(struct file *file,
117 struct poll_table_struct *wait);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700118static long sock_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
Shaun Pereira89bbfc92006-03-21 23:58:08 -0800119#ifdef CONFIG_COMPAT
120static long compat_sock_ioctl(struct file *file,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700121 unsigned int cmd, unsigned long arg);
Shaun Pereira89bbfc92006-03-21 23:58:08 -0800122#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123static int sock_fasync(int fd, struct file *filp, int on);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124static ssize_t sock_sendpage(struct file *file, struct page *page,
125 int offset, size_t size, loff_t *ppos, int more);
Jens Axboe9c55e012007-11-06 23:30:13 -0800126static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700127 struct pipe_inode_info *pipe, size_t len,
Jens Axboe9c55e012007-11-06 23:30:13 -0800128 unsigned int flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130/*
131 * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
132 * in the operation structures but are done directly via the socketcall() multiplexor.
133 */
134
Arjan van de Venda7071d2007-02-12 00:55:36 -0800135static const struct file_operations socket_file_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 .owner = THIS_MODULE,
137 .llseek = no_llseek,
138 .aio_read = sock_aio_read,
139 .aio_write = sock_aio_write,
140 .poll = sock_poll,
141 .unlocked_ioctl = sock_ioctl,
Shaun Pereira89bbfc92006-03-21 23:58:08 -0800142#ifdef CONFIG_COMPAT
143 .compat_ioctl = compat_sock_ioctl,
144#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 .mmap = sock_mmap,
146 .open = sock_no_open, /* special open code to disallow open via /proc */
147 .release = sock_close,
148 .fasync = sock_fasync,
Jens Axboe5274f052006-03-30 15:15:30 +0200149 .sendpage = sock_sendpage,
150 .splice_write = generic_splice_sendpage,
Jens Axboe9c55e012007-11-06 23:30:13 -0800151 .splice_read = sock_splice_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152};
153
154/*
155 * The protocol list. Each protocol is registered in here.
156 */
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158static DEFINE_SPINLOCK(net_family_lock);
Eric Dumazet190683a2010-11-10 10:50:44 +0000159static const struct net_proto_family __rcu *net_families[NPROTO] __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161/*
162 * Statistics counters of the socket lists
163 */
164
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700165static DEFINE_PER_CPU(int, sockets_in_use);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700168 * Support routines.
169 * Move socket addresses back and forth across the kernel/user
170 * divide and look after the messy bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 */
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173/**
174 * move_addr_to_kernel - copy a socket address into kernel space
175 * @uaddr: Address in user space
176 * @kaddr: Address in kernel space
177 * @ulen: Length in user space
178 *
179 * The address is copied into kernel space. If the provided address is
180 * too long an error code of -EINVAL is returned. If the copy gives
181 * invalid addresses -EFAULT is returned. On a success 0 is returned.
182 */
183
Maciej Żenczykowski43db3622012-03-11 12:51:50 +0000184int move_addr_to_kernel(void __user *uaddr, int ulen, struct sockaddr_storage *kaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -0700186 if (ulen < 0 || ulen > sizeof(struct sockaddr_storage))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700188 if (ulen == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 return 0;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700190 if (copy_from_user(kaddr, uaddr, ulen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 return -EFAULT;
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100192 return audit_sockaddr(ulen, kaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
194
195/**
196 * move_addr_to_user - copy an address to user space
197 * @kaddr: kernel space address
198 * @klen: length of address in kernel
199 * @uaddr: user space address
200 * @ulen: pointer to user length field
201 *
202 * The value pointed to by ulen on entry is the buffer length available.
203 * This is overwritten with the buffer space used. -EINVAL is returned
204 * if an overlong buffer is specified or a negative buffer size. -EFAULT
205 * is returned if either the buffer or the length field are not
206 * accessible.
207 * After copying the data up to the limit the user specifies, the true
208 * length of the data is written over the length limit the user
209 * specified. Zero is returned for a success.
210 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700211
Maciej Żenczykowski43db3622012-03-11 12:51:50 +0000212static int move_addr_to_user(struct sockaddr_storage *kaddr, int klen,
stephen hemminger11165f12010-10-18 14:27:29 +0000213 void __user *uaddr, int __user *ulen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
215 int err;
216 int len;
217
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700218 err = get_user(len, ulen);
219 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 return err;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700221 if (len > klen)
222 len = klen;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -0700223 if (len < 0 || len > sizeof(struct sockaddr_storage))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700225 if (len) {
Steve Grubbd6fe3942006-03-30 12:20:22 -0500226 if (audit_sockaddr(klen, kaddr))
227 return -ENOMEM;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700228 if (copy_to_user(uaddr, kaddr, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 return -EFAULT;
230 }
231 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700232 * "fromlen shall refer to the value before truncation.."
233 * 1003.1g
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 */
235 return __put_user(klen, ulen);
236}
237
Christoph Lametere18b8902006-12-06 20:33:20 -0800238static struct kmem_cache *sock_inode_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240static struct inode *sock_alloc_inode(struct super_block *sb)
241{
242 struct socket_alloc *ei;
Eric Dumazeteaefd112011-02-18 03:26:36 +0000243 struct socket_wq *wq;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700244
Christoph Lametere94b1762006-12-06 20:33:17 -0800245 ei = kmem_cache_alloc(sock_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 if (!ei)
247 return NULL;
Eric Dumazeteaefd112011-02-18 03:26:36 +0000248 wq = kmalloc(sizeof(*wq), GFP_KERNEL);
249 if (!wq) {
Eric Dumazet43815482010-04-29 11:01:49 +0000250 kmem_cache_free(sock_inode_cachep, ei);
251 return NULL;
252 }
Eric Dumazeteaefd112011-02-18 03:26:36 +0000253 init_waitqueue_head(&wq->wait);
254 wq->fasync_list = NULL;
255 RCU_INIT_POINTER(ei->socket.wq, wq);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 ei->socket.state = SS_UNCONNECTED;
258 ei->socket.flags = 0;
259 ei->socket.ops = NULL;
260 ei->socket.sk = NULL;
261 ei->socket.file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 return &ei->vfs_inode;
264}
265
266static void sock_destroy_inode(struct inode *inode)
267{
Eric Dumazet43815482010-04-29 11:01:49 +0000268 struct socket_alloc *ei;
Eric Dumazeteaefd112011-02-18 03:26:36 +0000269 struct socket_wq *wq;
Eric Dumazet43815482010-04-29 11:01:49 +0000270
271 ei = container_of(inode, struct socket_alloc, vfs_inode);
Eric Dumazeteaefd112011-02-18 03:26:36 +0000272 wq = rcu_dereference_protected(ei->socket.wq, 1);
Lai Jiangshan61845222011-03-18 12:10:25 +0800273 kfree_rcu(wq, rcu);
Eric Dumazet43815482010-04-29 11:01:49 +0000274 kmem_cache_free(sock_inode_cachep, ei);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
276
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700277static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700279 struct socket_alloc *ei = (struct socket_alloc *)foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Christoph Lametera35afb82007-05-16 22:10:57 -0700281 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282}
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284static int init_inodecache(void)
285{
286 sock_inode_cachep = kmem_cache_create("sock_inode_cache",
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700287 sizeof(struct socket_alloc),
288 0,
289 (SLAB_HWCACHE_ALIGN |
290 SLAB_RECLAIM_ACCOUNT |
291 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +0900292 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 if (sock_inode_cachep == NULL)
294 return -ENOMEM;
295 return 0;
296}
297
Alexey Dobriyanb87221d2009-09-21 17:01:09 -0700298static const struct super_operations sockfs_ops = {
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700299 .alloc_inode = sock_alloc_inode,
300 .destroy_inode = sock_destroy_inode,
301 .statfs = simple_statfs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302};
303
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700304/*
305 * sockfs_dname() is called from d_path().
306 */
307static char *sockfs_dname(struct dentry *dentry, char *buffer, int buflen)
308{
309 return dynamic_dname(dentry, buffer, buflen, "socket:[%lu]",
310 dentry->d_inode->i_ino);
311}
312
Al Viro3ba13d12009-02-20 06:02:22 +0000313static const struct dentry_operations sockfs_dentry_operations = {
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700314 .d_dname = sockfs_dname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315};
316
Al Viroc74a1cb2011-01-12 16:59:34 -0500317static struct dentry *sockfs_mount(struct file_system_type *fs_type,
318 int flags, const char *dev_name, void *data)
319{
320 return mount_pseudo(fs_type, "socket:", &sockfs_ops,
321 &sockfs_dentry_operations, SOCKFS_MAGIC);
322}
323
324static struct vfsmount *sock_mnt __read_mostly;
325
326static struct file_system_type sock_fs_type = {
327 .name = "sockfs",
328 .mount = sockfs_mount,
329 .kill_sb = kill_anon_super,
330};
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332/*
333 * Obtains the first available file descriptor and sets it up for use.
334 *
David S. Miller39d8c1b2006-03-20 17:13:49 -0800335 * These functions create file structures and maps them to fd space
336 * of the current process. On success it returns file descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 * and file struct implicitly stored in sock->file.
338 * Note that another thread may close file descriptor before we return
339 * from this function. We use the fact that now we do not refer
340 * to socket after mapping. If one day we will need it, this
341 * function will increment ref. count on file by 1.
342 *
343 * In any case returned fd MAY BE not valid!
344 * This race condition is unavoidable
345 * with shared fd spaces, we cannot solve it inside kernel,
346 * but we take care of internal coherence yet.
347 */
348
Linus Torvaldsaab174f2012-10-02 20:25:04 -0700349struct file *sock_alloc_file(struct socket *sock, int flags, const char *dname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
Al Viro7cbe66b2009-08-05 19:59:08 +0400351 struct qstr name = { .name = "" };
Al Viro2c48b9c2009-08-09 00:52:35 +0400352 struct path path;
Al Viro7cbe66b2009-08-05 19:59:08 +0400353 struct file *file;
David S. Miller39d8c1b2006-03-20 17:13:49 -0800354
Masatake YAMATO600e1772012-08-29 10:44:29 +0000355 if (dname) {
356 name.name = dname;
357 name.len = strlen(name.name);
358 } else if (sock->sk) {
359 name.name = sock->sk->sk_prot_creator->name;
360 name.len = strlen(name.name);
361 }
Nick Piggin4b936882011-01-07 17:50:07 +1100362 path.dentry = d_alloc_pseudo(sock_mnt->mnt_sb, &name);
Al Viro28407632012-08-17 23:54:15 -0400363 if (unlikely(!path.dentry))
364 return ERR_PTR(-ENOMEM);
Al Viro2c48b9c2009-08-09 00:52:35 +0400365 path.mnt = mntget(sock_mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Al Viro2c48b9c2009-08-09 00:52:35 +0400367 d_instantiate(path.dentry, SOCK_INODE(sock));
Al Virocc3808f2009-08-06 09:43:59 +0400368 SOCK_INODE(sock)->i_fop = &socket_file_ops;
369
Al Viro2c48b9c2009-08-09 00:52:35 +0400370 file = alloc_file(&path, FMODE_READ | FMODE_WRITE,
Al Virocc3808f2009-08-06 09:43:59 +0400371 &socket_file_ops);
372 if (unlikely(!file)) {
373 /* drop dentry, keep inode */
Al Viro7de9c6ee2010-10-23 11:11:40 -0400374 ihold(path.dentry->d_inode);
Al Viro2c48b9c2009-08-09 00:52:35 +0400375 path_put(&path);
Al Viro28407632012-08-17 23:54:15 -0400376 return ERR_PTR(-ENFILE);
Al Virocc3808f2009-08-06 09:43:59 +0400377 }
David S. Miller39d8c1b2006-03-20 17:13:49 -0800378
379 sock->file = file;
Ulrich Drepper77d27202008-07-23 21:29:35 -0700380 file->f_flags = O_RDWR | (flags & O_NONBLOCK);
David S. Miller39d8c1b2006-03-20 17:13:49 -0800381 file->f_pos = 0;
382 file->private_data = sock;
Al Viro28407632012-08-17 23:54:15 -0400383 return file;
David S. Miller39d8c1b2006-03-20 17:13:49 -0800384}
Al Viro56b31d12012-08-18 00:25:51 -0400385EXPORT_SYMBOL(sock_alloc_file);
David S. Miller39d8c1b2006-03-20 17:13:49 -0800386
Al Viro56b31d12012-08-18 00:25:51 -0400387static int sock_map_fd(struct socket *sock, int flags)
David S. Miller39d8c1b2006-03-20 17:13:49 -0800388{
389 struct file *newfile;
Al Viro28407632012-08-17 23:54:15 -0400390 int fd = get_unused_fd_flags(flags);
391 if (unlikely(fd < 0))
392 return fd;
David S. Miller39d8c1b2006-03-20 17:13:49 -0800393
Linus Torvaldsaab174f2012-10-02 20:25:04 -0700394 newfile = sock_alloc_file(sock, flags, NULL);
Al Viro28407632012-08-17 23:54:15 -0400395 if (likely(!IS_ERR(newfile))) {
David S. Miller39d8c1b2006-03-20 17:13:49 -0800396 fd_install(fd, newfile);
Al Viro28407632012-08-17 23:54:15 -0400397 return fd;
398 }
Al Viro7cbe66b2009-08-05 19:59:08 +0400399
Al Viro28407632012-08-17 23:54:15 -0400400 put_unused_fd(fd);
401 return PTR_ERR(newfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402}
403
John Fastabend406a3c62012-07-20 10:39:25 +0000404struct socket *sock_from_file(struct file *file, int *err)
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800405{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800406 if (file->f_op == &socket_file_ops)
407 return file->private_data; /* set in sock_map_fd */
408
Eric Dumazet23bb80d2007-02-08 14:59:57 -0800409 *err = -ENOTSOCK;
410 return NULL;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800411}
John Fastabend406a3c62012-07-20 10:39:25 +0000412EXPORT_SYMBOL(sock_from_file);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414/**
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700415 * sockfd_lookup - Go from a file number to its socket slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 * @fd: file handle
417 * @err: pointer to an error code return
418 *
419 * The file handle passed in is locked and the socket it is bound
420 * too is returned. If an error occurs the err pointer is overwritten
421 * with a negative errno code and NULL is returned. The function checks
422 * for both invalid handles and passing a handle which is not a socket.
423 *
424 * On a success the socket object pointer is returned.
425 */
426
427struct socket *sockfd_lookup(int fd, int *err)
428{
429 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 struct socket *sock;
431
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700432 file = fget(fd);
433 if (!file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 *err = -EBADF;
435 return NULL;
436 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700437
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800438 sock = sock_from_file(file, err);
439 if (!sock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 fput(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return sock;
442}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700443EXPORT_SYMBOL(sockfd_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800445static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed)
446{
447 struct file *file;
448 struct socket *sock;
449
Hua Zhong36725582006-04-19 15:25:02 -0700450 *err = -EBADF;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800451 file = fget_light(fd, fput_needed);
452 if (file) {
453 sock = sock_from_file(file, err);
454 if (sock)
455 return sock;
456 fput_light(file, *fput_needed);
457 }
458 return NULL;
459}
460
Masatake YAMATO600e1772012-08-29 10:44:29 +0000461#define XATTR_SOCKPROTONAME_SUFFIX "sockprotoname"
462#define XATTR_NAME_SOCKPROTONAME (XATTR_SYSTEM_PREFIX XATTR_SOCKPROTONAME_SUFFIX)
463#define XATTR_NAME_SOCKPROTONAME_LEN (sizeof(XATTR_NAME_SOCKPROTONAME)-1)
464static ssize_t sockfs_getxattr(struct dentry *dentry,
465 const char *name, void *value, size_t size)
466{
467 const char *proto_name;
468 size_t proto_size;
469 int error;
470
471 error = -ENODATA;
472 if (!strncmp(name, XATTR_NAME_SOCKPROTONAME, XATTR_NAME_SOCKPROTONAME_LEN)) {
473 proto_name = dentry->d_name.name;
474 proto_size = strlen(proto_name);
475
476 if (value) {
477 error = -ERANGE;
478 if (proto_size + 1 > size)
479 goto out;
480
481 strncpy(value, proto_name, proto_size + 1);
482 }
483 error = proto_size + 1;
484 }
485
486out:
487 return error;
488}
489
490static ssize_t sockfs_listxattr(struct dentry *dentry, char *buffer,
491 size_t size)
492{
493 ssize_t len;
494 ssize_t used = 0;
495
496 len = security_inode_listsecurity(dentry->d_inode, buffer, size);
497 if (len < 0)
498 return len;
499 used += len;
500 if (buffer) {
501 if (size < used)
502 return -ERANGE;
503 buffer += len;
504 }
505
506 len = (XATTR_NAME_SOCKPROTONAME_LEN + 1);
507 used += len;
508 if (buffer) {
509 if (size < used)
510 return -ERANGE;
511 memcpy(buffer, XATTR_NAME_SOCKPROTONAME, len);
512 buffer += len;
513 }
514
515 return used;
516}
517
518static const struct inode_operations sockfs_inode_ops = {
519 .getxattr = sockfs_getxattr,
520 .listxattr = sockfs_listxattr,
521};
522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523/**
524 * sock_alloc - allocate a socket
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700525 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 * Allocate a new inode and socket object. The two are bound together
527 * and initialised. The socket is then returned. If we are out of inodes
528 * NULL is returned.
529 */
530
531static struct socket *sock_alloc(void)
532{
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700533 struct inode *inode;
534 struct socket *sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Eric Dumazeta209dfc2011-07-26 11:36:34 +0200536 inode = new_inode_pseudo(sock_mnt->mnt_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 if (!inode)
538 return NULL;
539
540 sock = SOCKET_I(inode);
541
Eric Dumazet29a020d2009-09-15 02:39:20 -0700542 kmemcheck_annotate_bitfield(sock, type);
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400543 inode->i_ino = get_next_ino();
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700544 inode->i_mode = S_IFSOCK | S_IRWXUGO;
David Howells8192b0c2008-11-14 10:39:10 +1100545 inode->i_uid = current_fsuid();
546 inode->i_gid = current_fsgid();
Masatake YAMATO600e1772012-08-29 10:44:29 +0000547 inode->i_op = &sockfs_inode_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Alex Shi19e8d692012-05-14 14:15:31 -0700549 this_cpu_add(sockets_in_use, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 return sock;
551}
552
553/*
554 * In theory you can't get an open on this inode, but /proc provides
555 * a back door. Remember to keep it shut otherwise you'll let the
556 * creepy crawlies in.
557 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559static int sock_no_open(struct inode *irrelevant, struct file *dontcare)
560{
561 return -ENXIO;
562}
563
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800564const struct file_operations bad_sock_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 .owner = THIS_MODULE,
566 .open = sock_no_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200567 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568};
569
570/**
571 * sock_release - close a socket
572 * @sock: socket to close
573 *
574 * The socket is released from the protocol stack if it has a release
575 * callback, and the inode is then released if the socket is bound to
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700576 * an inode not a file.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579void sock_release(struct socket *sock)
580{
581 if (sock->ops) {
582 struct module *owner = sock->ops->owner;
583
584 sock->ops->release(sock);
585 sock->ops = NULL;
586 module_put(owner);
587 }
588
Eric Dumazeteaefd112011-02-18 03:26:36 +0000589 if (rcu_dereference_protected(sock->wq, 1)->fasync_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 printk(KERN_ERR "sock_release: fasync list not empty!\n");
591
Mikulas Patockab09e7862012-07-19 06:13:36 +0000592 if (test_bit(SOCK_EXTERNALLY_ALLOCATED, &sock->flags))
593 return;
594
Alex Shi19e8d692012-05-14 14:15:31 -0700595 this_cpu_sub(sockets_in_use, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 if (!sock->file) {
597 iput(SOCK_INODE(sock));
598 return;
599 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700600 sock->file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700602EXPORT_SYMBOL(sock_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Oliver Hartkopp2244d072010-08-17 08:59:14 +0000604int sock_tx_timestamp(struct sock *sk, __u8 *tx_flags)
Patrick Ohly20d49472009-02-12 05:03:38 +0000605{
Oliver Hartkopp2244d072010-08-17 08:59:14 +0000606 *tx_flags = 0;
Patrick Ohly20d49472009-02-12 05:03:38 +0000607 if (sock_flag(sk, SOCK_TIMESTAMPING_TX_HARDWARE))
Oliver Hartkopp2244d072010-08-17 08:59:14 +0000608 *tx_flags |= SKBTX_HW_TSTAMP;
Patrick Ohly20d49472009-02-12 05:03:38 +0000609 if (sock_flag(sk, SOCK_TIMESTAMPING_TX_SOFTWARE))
Oliver Hartkopp2244d072010-08-17 08:59:14 +0000610 *tx_flags |= SKBTX_SW_TSTAMP;
Johannes Berg6e3e9392011-11-09 10:15:42 +0100611 if (sock_flag(sk, SOCK_WIFI_STATUS))
612 *tx_flags |= SKBTX_WIFI_STATUS;
Patrick Ohly20d49472009-02-12 05:03:38 +0000613 return 0;
614}
615EXPORT_SYMBOL(sock_tx_timestamp);
616
Anton Blanchard228e5482011-05-02 20:21:35 +0000617static inline int __sock_sendmsg_nosec(struct kiocb *iocb, struct socket *sock,
618 struct msghdr *msg, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
620 struct sock_iocb *si = kiocb_to_siocb(iocb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 si->sock = sock;
623 si->scm = NULL;
624 si->msg = msg;
625 si->size = size;
626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 return sock->ops->sendmsg(iocb, sock, msg, size);
628}
629
Anton Blanchard228e5482011-05-02 20:21:35 +0000630static inline int __sock_sendmsg(struct kiocb *iocb, struct socket *sock,
631 struct msghdr *msg, size_t size)
632{
633 int err = security_socket_sendmsg(sock, msg, size);
634
635 return err ?: __sock_sendmsg_nosec(iocb, sock, msg, size);
636}
637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638int sock_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
639{
640 struct kiocb iocb;
641 struct sock_iocb siocb;
642 int ret;
643
644 init_sync_kiocb(&iocb, NULL);
645 iocb.private = &siocb;
646 ret = __sock_sendmsg(&iocb, sock, msg, size);
647 if (-EIOCBQUEUED == ret)
648 ret = wait_on_sync_kiocb(&iocb);
649 return ret;
650}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700651EXPORT_SYMBOL(sock_sendmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Eric Dumazet894dc242011-07-26 02:39:41 +0000653static int sock_sendmsg_nosec(struct socket *sock, struct msghdr *msg, size_t size)
Anton Blanchard228e5482011-05-02 20:21:35 +0000654{
655 struct kiocb iocb;
656 struct sock_iocb siocb;
657 int ret;
658
659 init_sync_kiocb(&iocb, NULL);
660 iocb.private = &siocb;
661 ret = __sock_sendmsg_nosec(&iocb, sock, msg, size);
662 if (-EIOCBQUEUED == ret)
663 ret = wait_on_sync_kiocb(&iocb);
664 return ret;
665}
666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
668 struct kvec *vec, size_t num, size_t size)
669{
670 mm_segment_t oldfs = get_fs();
671 int result;
672
673 set_fs(KERNEL_DS);
674 /*
675 * the following is safe, since for compiler definitions of kvec and
676 * iovec are identical, yielding the same in-core layout and alignment
677 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700678 msg->msg_iov = (struct iovec *)vec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 msg->msg_iovlen = num;
680 result = sock_sendmsg(sock, msg, size);
681 set_fs(oldfs);
682 return result;
683}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700684EXPORT_SYMBOL(kernel_sendmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Patrick Ohly20d49472009-02-12 05:03:38 +0000686static int ktime2ts(ktime_t kt, struct timespec *ts)
687{
688 if (kt.tv64) {
689 *ts = ktime_to_timespec(kt);
690 return 1;
691 } else {
692 return 0;
693 }
694}
695
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700696/*
697 * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
698 */
699void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
700 struct sk_buff *skb)
701{
Patrick Ohly20d49472009-02-12 05:03:38 +0000702 int need_software_tstamp = sock_flag(sk, SOCK_RCVTSTAMP);
703 struct timespec ts[3];
704 int empty = 1;
705 struct skb_shared_hwtstamps *shhwtstamps =
706 skb_hwtstamps(skb);
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700707
Patrick Ohly20d49472009-02-12 05:03:38 +0000708 /* Race occurred between timestamp enabling and packet
709 receiving. Fill in the current time for now. */
710 if (need_software_tstamp && skb->tstamp.tv64 == 0)
711 __net_timestamp(skb);
712
713 if (need_software_tstamp) {
714 if (!sock_flag(sk, SOCK_RCVTSTAMPNS)) {
715 struct timeval tv;
716 skb_get_timestamp(skb, &tv);
717 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMP,
718 sizeof(tv), &tv);
719 } else {
Hagen Paul Pfeifer842509b2010-04-06 05:39:52 +0000720 skb_get_timestampns(skb, &ts[0]);
Patrick Ohly20d49472009-02-12 05:03:38 +0000721 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPNS,
Hagen Paul Pfeifer842509b2010-04-06 05:39:52 +0000722 sizeof(ts[0]), &ts[0]);
Patrick Ohly20d49472009-02-12 05:03:38 +0000723 }
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700724 }
Patrick Ohly20d49472009-02-12 05:03:38 +0000725
726
727 memset(ts, 0, sizeof(ts));
728 if (skb->tstamp.tv64 &&
729 sock_flag(sk, SOCK_TIMESTAMPING_SOFTWARE)) {
730 skb_get_timestampns(skb, ts + 0);
731 empty = 0;
732 }
733 if (shhwtstamps) {
734 if (sock_flag(sk, SOCK_TIMESTAMPING_SYS_HARDWARE) &&
735 ktime2ts(shhwtstamps->syststamp, ts + 1))
736 empty = 0;
737 if (sock_flag(sk, SOCK_TIMESTAMPING_RAW_HARDWARE) &&
738 ktime2ts(shhwtstamps->hwtstamp, ts + 2))
739 empty = 0;
740 }
741 if (!empty)
742 put_cmsg(msg, SOL_SOCKET,
743 SCM_TIMESTAMPING, sizeof(ts), &ts);
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700744}
Arnaldo Carvalho de Melo7c81fd82007-03-10 00:39:35 -0300745EXPORT_SYMBOL_GPL(__sock_recv_timestamp);
746
Johannes Berg6e3e9392011-11-09 10:15:42 +0100747void __sock_recv_wifi_status(struct msghdr *msg, struct sock *sk,
748 struct sk_buff *skb)
749{
750 int ack;
751
752 if (!sock_flag(sk, SOCK_WIFI_STATUS))
753 return;
754 if (!skb->wifi_acked_valid)
755 return;
756
757 ack = skb->wifi_acked;
758
759 put_cmsg(msg, SOL_SOCKET, SCM_WIFI_STATUS, sizeof(ack), &ack);
760}
761EXPORT_SYMBOL_GPL(__sock_recv_wifi_status);
762
stephen hemminger11165f12010-10-18 14:27:29 +0000763static inline void sock_recv_drops(struct msghdr *msg, struct sock *sk,
764 struct sk_buff *skb)
Neil Horman3b885782009-10-12 13:26:31 -0700765{
766 if (sock_flag(sk, SOCK_RXQ_OVFL) && skb && skb->dropcount)
767 put_cmsg(msg, SOL_SOCKET, SO_RXQ_OVFL,
768 sizeof(__u32), &skb->dropcount);
769}
770
Eric Dumazet767dd032010-04-28 19:14:43 +0000771void __sock_recv_ts_and_drops(struct msghdr *msg, struct sock *sk,
Neil Horman3b885782009-10-12 13:26:31 -0700772 struct sk_buff *skb)
773{
774 sock_recv_timestamp(msg, sk, skb);
775 sock_recv_drops(msg, sk, skb);
776}
Eric Dumazet767dd032010-04-28 19:14:43 +0000777EXPORT_SYMBOL_GPL(__sock_recv_ts_and_drops);
Neil Horman3b885782009-10-12 13:26:31 -0700778
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -0700779static inline int __sock_recvmsg_nosec(struct kiocb *iocb, struct socket *sock,
780 struct msghdr *msg, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 struct sock_iocb *si = kiocb_to_siocb(iocb);
783
784 si->sock = sock;
785 si->scm = NULL;
786 si->msg = msg;
787 si->size = size;
788 si->flags = flags;
789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 return sock->ops->recvmsg(iocb, sock, msg, size, flags);
791}
792
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -0700793static inline int __sock_recvmsg(struct kiocb *iocb, struct socket *sock,
794 struct msghdr *msg, size_t size, int flags)
795{
796 int err = security_socket_recvmsg(sock, msg, size, flags);
797
798 return err ?: __sock_recvmsg_nosec(iocb, sock, msg, size, flags);
799}
800
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700801int sock_recvmsg(struct socket *sock, struct msghdr *msg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 size_t size, int flags)
803{
804 struct kiocb iocb;
805 struct sock_iocb siocb;
806 int ret;
807
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700808 init_sync_kiocb(&iocb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 iocb.private = &siocb;
810 ret = __sock_recvmsg(&iocb, sock, msg, size, flags);
811 if (-EIOCBQUEUED == ret)
812 ret = wait_on_sync_kiocb(&iocb);
813 return ret;
814}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700815EXPORT_SYMBOL(sock_recvmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -0700817static int sock_recvmsg_nosec(struct socket *sock, struct msghdr *msg,
818 size_t size, int flags)
819{
820 struct kiocb iocb;
821 struct sock_iocb siocb;
822 int ret;
823
824 init_sync_kiocb(&iocb, NULL);
825 iocb.private = &siocb;
826 ret = __sock_recvmsg_nosec(&iocb, sock, msg, size, flags);
827 if (-EIOCBQUEUED == ret)
828 ret = wait_on_sync_kiocb(&iocb);
829 return ret;
830}
831
Martin Lucinac1249c02010-12-10 00:04:05 +0000832/**
833 * kernel_recvmsg - Receive a message from a socket (kernel space)
834 * @sock: The socket to receive the message from
835 * @msg: Received message
836 * @vec: Input s/g array for message data
837 * @num: Size of input s/g array
838 * @size: Number of bytes to read
839 * @flags: Message flags (MSG_DONTWAIT, etc...)
840 *
841 * On return the msg structure contains the scatter/gather array passed in the
842 * vec argument. The array is modified so that it consists of the unfilled
843 * portion of the original array.
844 *
845 * The returned value is the total number of bytes received, or an error.
846 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700847int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
848 struct kvec *vec, size_t num, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
850 mm_segment_t oldfs = get_fs();
851 int result;
852
853 set_fs(KERNEL_DS);
854 /*
855 * the following is safe, since for compiler definitions of kvec and
856 * iovec are identical, yielding the same in-core layout and alignment
857 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700858 msg->msg_iov = (struct iovec *)vec, msg->msg_iovlen = num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 result = sock_recvmsg(sock, msg, size, flags);
860 set_fs(oldfs);
861 return result;
862}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700863EXPORT_SYMBOL(kernel_recvmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
865static void sock_aio_dtor(struct kiocb *iocb)
866{
867 kfree(iocb->private);
868}
869
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -0300870static ssize_t sock_sendpage(struct file *file, struct page *page,
871 int offset, size_t size, loff_t *ppos, int more)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
873 struct socket *sock;
874 int flags;
875
Eric Dumazetb69aee02005-09-06 14:42:45 -0700876 sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Eric Dumazet35f9c092012-04-05 03:05:35 +0000878 flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
879 /* more is a combination of MSG_MORE and MSG_SENDPAGE_NOTLAST */
880 flags |= more;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
Linus Torvaldse6949582009-08-13 08:28:36 -0700882 return kernel_sendpage(sock, page, offset, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883}
884
Jens Axboe9c55e012007-11-06 23:30:13 -0800885static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700886 struct pipe_inode_info *pipe, size_t len,
Jens Axboe9c55e012007-11-06 23:30:13 -0800887 unsigned int flags)
888{
889 struct socket *sock = file->private_data;
890
Rémi Denis-Courmont997b37d2008-02-15 02:35:45 -0800891 if (unlikely(!sock->ops->splice_read))
892 return -EINVAL;
893
Jens Axboe9c55e012007-11-06 23:30:13 -0800894 return sock->ops->splice_read(sock, ppos, pipe, len, flags);
895}
896
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800897static struct sock_iocb *alloc_sock_iocb(struct kiocb *iocb,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700898 struct sock_iocb *siocb)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800899{
900 if (!is_sync_kiocb(iocb)) {
901 siocb = kmalloc(sizeof(*siocb), GFP_KERNEL);
902 if (!siocb)
903 return NULL;
904 iocb->ki_dtor = sock_aio_dtor;
905 }
906
907 siocb->kiocb = iocb;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800908 iocb->private = siocb;
909 return siocb;
910}
911
912static ssize_t do_sock_read(struct msghdr *msg, struct kiocb *iocb,
Badari Pulavarty027445c2006-09-30 23:28:46 -0700913 struct file *file, const struct iovec *iov,
914 unsigned long nr_segs)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800915{
916 struct socket *sock = file->private_data;
917 size_t size = 0;
918 int i;
919
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700920 for (i = 0; i < nr_segs; i++)
921 size += iov[i].iov_len;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800922
923 msg->msg_name = NULL;
924 msg->msg_namelen = 0;
925 msg->msg_control = NULL;
926 msg->msg_controllen = 0;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700927 msg->msg_iov = (struct iovec *)iov;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800928 msg->msg_iovlen = nr_segs;
929 msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
930
931 return __sock_recvmsg(iocb, sock, msg, size, msg->msg_flags);
932}
933
Badari Pulavarty027445c2006-09-30 23:28:46 -0700934static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov,
935 unsigned long nr_segs, loff_t pos)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800936{
937 struct sock_iocb siocb, *x;
938
939 if (pos != 0)
940 return -ESPIPE;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700941
942 if (iocb->ki_left == 0) /* Match SYS5 behaviour */
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800943 return 0;
944
Badari Pulavarty027445c2006-09-30 23:28:46 -0700945
946 x = alloc_sock_iocb(iocb, &siocb);
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800947 if (!x)
948 return -ENOMEM;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700949 return do_sock_read(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800950}
951
952static ssize_t do_sock_write(struct msghdr *msg, struct kiocb *iocb,
Badari Pulavarty027445c2006-09-30 23:28:46 -0700953 struct file *file, const struct iovec *iov,
954 unsigned long nr_segs)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800955{
956 struct socket *sock = file->private_data;
957 size_t size = 0;
958 int i;
959
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700960 for (i = 0; i < nr_segs; i++)
961 size += iov[i].iov_len;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800962
963 msg->msg_name = NULL;
964 msg->msg_namelen = 0;
965 msg->msg_control = NULL;
966 msg->msg_controllen = 0;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700967 msg->msg_iov = (struct iovec *)iov;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800968 msg->msg_iovlen = nr_segs;
969 msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
970 if (sock->type == SOCK_SEQPACKET)
971 msg->msg_flags |= MSG_EOR;
972
973 return __sock_sendmsg(iocb, sock, msg, size);
974}
975
Badari Pulavarty027445c2006-09-30 23:28:46 -0700976static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov,
977 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978{
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800979 struct sock_iocb siocb, *x;
980
981 if (pos != 0)
982 return -ESPIPE;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700983
Badari Pulavarty027445c2006-09-30 23:28:46 -0700984 x = alloc_sock_iocb(iocb, &siocb);
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800985 if (!x)
986 return -ENOMEM;
987
Badari Pulavarty027445c2006-09-30 23:28:46 -0700988 return do_sock_write(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989}
990
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991/*
992 * Atomic setting of ioctl hooks to avoid race
993 * with module unload.
994 */
995
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800996static DEFINE_MUTEX(br_ioctl_mutex);
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700997static int (*br_ioctl_hook) (struct net *, unsigned int cmd, void __user *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
Eric W. Biederman881d9662007-09-17 11:56:21 -0700999void brioctl_set(int (*hook) (struct net *, unsigned int, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001001 mutex_lock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 br_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001003 mutex_unlock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004}
1005EXPORT_SYMBOL(brioctl_set);
1006
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001007static DEFINE_MUTEX(vlan_ioctl_mutex);
Eric W. Biederman881d9662007-09-17 11:56:21 -07001008static int (*vlan_ioctl_hook) (struct net *, void __user *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Eric W. Biederman881d9662007-09-17 11:56:21 -07001010void vlan_ioctl_set(int (*hook) (struct net *, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001012 mutex_lock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 vlan_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001014 mutex_unlock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015}
1016EXPORT_SYMBOL(vlan_ioctl_set);
1017
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001018static DEFINE_MUTEX(dlci_ioctl_mutex);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001019static int (*dlci_ioctl_hook) (unsigned int, void __user *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001021void dlci_ioctl_set(int (*hook) (unsigned int, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001023 mutex_lock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 dlci_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001025 mutex_unlock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026}
1027EXPORT_SYMBOL(dlci_ioctl_set);
1028
Arnd Bergmann6b960182009-11-06 23:10:54 -08001029static long sock_do_ioctl(struct net *net, struct socket *sock,
1030 unsigned int cmd, unsigned long arg)
1031{
1032 int err;
1033 void __user *argp = (void __user *)arg;
1034
1035 err = sock->ops->ioctl(sock, cmd, arg);
1036
1037 /*
1038 * If this ioctl is unknown try to hand it down
1039 * to the NIC driver.
1040 */
1041 if (err == -ENOIOCTLCMD)
1042 err = dev_ioctl(net, cmd, argp);
1043
1044 return err;
1045}
1046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047/*
1048 * With an ioctl, arg may well be a user mode pointer, but we don't know
1049 * what to do with it - that's up to the protocol still.
1050 */
1051
1052static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
1053{
1054 struct socket *sock;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001055 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 void __user *argp = (void __user *)arg;
1057 int pid, err;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001058 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Eric Dumazetb69aee02005-09-06 14:42:45 -07001060 sock = file->private_data;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001061 sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001062 net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
Eric W. Biederman881d9662007-09-17 11:56:21 -07001064 err = dev_ioctl(net, cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 } else
Johannes Berg3d23e342009-09-29 23:27:28 +02001066#ifdef CONFIG_WEXT_CORE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
Eric W. Biederman881d9662007-09-17 11:56:21 -07001068 err = dev_ioctl(net, cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 } else
Johannes Berg3d23e342009-09-29 23:27:28 +02001070#endif
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001071 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 case FIOSETOWN:
1073 case SIOCSPGRP:
1074 err = -EFAULT;
1075 if (get_user(pid, (int __user *)argp))
1076 break;
1077 err = f_setown(sock->file, pid, 1);
1078 break;
1079 case FIOGETOWN:
1080 case SIOCGPGRP:
Eric W. Biederman609d7fa2006-10-02 02:17:15 -07001081 err = put_user(f_getown(sock->file),
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001082 (int __user *)argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 break;
1084 case SIOCGIFBR:
1085 case SIOCSIFBR:
1086 case SIOCBRADDBR:
1087 case SIOCBRDELBR:
1088 err = -ENOPKG;
1089 if (!br_ioctl_hook)
1090 request_module("bridge");
1091
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001092 mutex_lock(&br_ioctl_mutex);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001093 if (br_ioctl_hook)
Eric W. Biederman881d9662007-09-17 11:56:21 -07001094 err = br_ioctl_hook(net, cmd, argp);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001095 mutex_unlock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 break;
1097 case SIOCGIFVLAN:
1098 case SIOCSIFVLAN:
1099 err = -ENOPKG;
1100 if (!vlan_ioctl_hook)
1101 request_module("8021q");
1102
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001103 mutex_lock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 if (vlan_ioctl_hook)
Eric W. Biederman881d9662007-09-17 11:56:21 -07001105 err = vlan_ioctl_hook(net, argp);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001106 mutex_unlock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 case SIOCADDDLCI:
1109 case SIOCDELDLCI:
1110 err = -ENOPKG;
1111 if (!dlci_ioctl_hook)
1112 request_module("dlci");
1113
Pavel Emelyanov7512cbf2008-03-21 15:58:52 -07001114 mutex_lock(&dlci_ioctl_mutex);
1115 if (dlci_ioctl_hook)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 err = dlci_ioctl_hook(cmd, argp);
Pavel Emelyanov7512cbf2008-03-21 15:58:52 -07001117 mutex_unlock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 break;
1119 default:
Arnd Bergmann6b960182009-11-06 23:10:54 -08001120 err = sock_do_ioctl(net, sock, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 break;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 return err;
1124}
1125
1126int sock_create_lite(int family, int type, int protocol, struct socket **res)
1127{
1128 int err;
1129 struct socket *sock = NULL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001130
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 err = security_socket_create(family, type, protocol, 1);
1132 if (err)
1133 goto out;
1134
1135 sock = sock_alloc();
1136 if (!sock) {
1137 err = -ENOMEM;
1138 goto out;
1139 }
1140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 sock->type = type;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001142 err = security_socket_post_create(sock, family, type, protocol, 1);
1143 if (err)
1144 goto out_release;
1145
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146out:
1147 *res = sock;
1148 return err;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001149out_release:
1150 sock_release(sock);
1151 sock = NULL;
1152 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001154EXPORT_SYMBOL(sock_create_lite);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
1156/* No kernel lock held - perfect */
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001157static unsigned int sock_poll(struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158{
1159 struct socket *sock;
1160
1161 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001162 * We can't return errors to poll, so it's either yes or no.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 */
Eric Dumazetb69aee02005-09-06 14:42:45 -07001164 sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 return sock->ops->poll(file, sock, wait);
1166}
1167
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001168static int sock_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169{
Eric Dumazetb69aee02005-09-06 14:42:45 -07001170 struct socket *sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
1172 return sock->ops->mmap(file, sock, vma);
1173}
1174
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001175static int sock_close(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176{
1177 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001178 * It was possible the inode is NULL we were
1179 * closing an unfinished socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 */
1181
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001182 if (!inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 printk(KERN_DEBUG "sock_close: NULL inode\n");
1184 return 0;
1185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 sock_release(SOCKET_I(inode));
1187 return 0;
1188}
1189
1190/*
1191 * Update the socket async list
1192 *
1193 * Fasync_list locking strategy.
1194 *
1195 * 1. fasync_list is modified only under process context socket lock
1196 * i.e. under semaphore.
1197 * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
Eric Dumazet989a2972010-04-14 09:55:35 +00001198 * or under socket lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 */
1200
1201static int sock_fasync(int fd, struct file *filp, int on)
1202{
Eric Dumazet989a2972010-04-14 09:55:35 +00001203 struct socket *sock = filp->private_data;
1204 struct sock *sk = sock->sk;
Eric Dumazeteaefd112011-02-18 03:26:36 +00001205 struct socket_wq *wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Eric Dumazet989a2972010-04-14 09:55:35 +00001207 if (sk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
1210 lock_sock(sk);
Eric Dumazeteaefd112011-02-18 03:26:36 +00001211 wq = rcu_dereference_protected(sock->wq, sock_owned_by_user(sk));
1212 fasync_helper(fd, filp, on, &wq->fasync_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
Eric Dumazeteaefd112011-02-18 03:26:36 +00001214 if (!wq->fasync_list)
Eric Dumazet989a2972010-04-14 09:55:35 +00001215 sock_reset_flag(sk, SOCK_FASYNC);
Jonathan Corbet76398422009-02-01 14:26:59 -07001216 else
Eric Dumazetbcdce712009-10-06 17:28:29 -07001217 sock_set_flag(sk, SOCK_FASYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
Eric Dumazet989a2972010-04-14 09:55:35 +00001219 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 return 0;
1221}
1222
Eric Dumazet43815482010-04-29 11:01:49 +00001223/* This function may be called only under socket lock or callback_lock or rcu_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
1225int sock_wake_async(struct socket *sock, int how, int band)
1226{
Eric Dumazet43815482010-04-29 11:01:49 +00001227 struct socket_wq *wq;
1228
1229 if (!sock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 return -1;
Eric Dumazet43815482010-04-29 11:01:49 +00001231 rcu_read_lock();
1232 wq = rcu_dereference(sock->wq);
1233 if (!wq || !wq->fasync_list) {
1234 rcu_read_unlock();
1235 return -1;
1236 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001237 switch (how) {
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001238 case SOCK_WAKE_WAITD:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 if (test_bit(SOCK_ASYNC_WAITDATA, &sock->flags))
1240 break;
1241 goto call_kill;
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001242 case SOCK_WAKE_SPACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags))
1244 break;
1245 /* fall through */
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001246 case SOCK_WAKE_IO:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001247call_kill:
Eric Dumazet43815482010-04-29 11:01:49 +00001248 kill_fasync(&wq->fasync_list, SIGIO, band);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 break;
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001250 case SOCK_WAKE_URG:
Eric Dumazet43815482010-04-29 11:01:49 +00001251 kill_fasync(&wq->fasync_list, SIGURG, band);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 }
Eric Dumazet43815482010-04-29 11:01:49 +00001253 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 return 0;
1255}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001256EXPORT_SYMBOL(sock_wake_async);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Pavel Emelyanov721db932010-09-29 16:06:32 +04001258int __sock_create(struct net *net, int family, int type, int protocol,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001259 struct socket **res, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260{
1261 int err;
1262 struct socket *sock;
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001263 const struct net_proto_family *pf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
1265 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001266 * Check protocol is in range
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 */
1268 if (family < 0 || family >= NPROTO)
1269 return -EAFNOSUPPORT;
1270 if (type < 0 || type >= SOCK_MAX)
1271 return -EINVAL;
1272
1273 /* Compatibility.
1274
1275 This uglymoron is moved from INET layer to here to avoid
1276 deadlock in module load.
1277 */
1278 if (family == PF_INET && type == SOCK_PACKET) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001279 static int warned;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 if (!warned) {
1281 warned = 1;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001282 printk(KERN_INFO "%s uses obsolete (PF_INET,SOCK_PACKET)\n",
1283 current->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 }
1285 family = PF_PACKET;
1286 }
1287
1288 err = security_socket_create(family, type, protocol, kern);
1289 if (err)
1290 return err;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001291
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001292 /*
1293 * Allocate the socket and allow the family to set things up. if
1294 * the protocol is 0, the family is instructed to select an appropriate
1295 * default.
1296 */
1297 sock = sock_alloc();
1298 if (!sock) {
Joe Perchese87cc472012-05-13 21:56:26 +00001299 net_warn_ratelimited("socket: no more sockets\n");
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001300 return -ENFILE; /* Not exactly a match, but its the
1301 closest posix thing */
1302 }
1303
1304 sock->type = type;
1305
Johannes Berg95a5afc2008-10-16 15:24:51 -07001306#ifdef CONFIG_MODULES
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001307 /* Attempt to load a protocol module if the find failed.
1308 *
1309 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 * requested real, full-featured networking support upon configuration.
1311 * Otherwise module support will break!
1312 */
Eric Dumazet190683a2010-11-10 10:50:44 +00001313 if (rcu_access_pointer(net_families[family]) == NULL)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001314 request_module("net-pf-%d", family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315#endif
1316
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001317 rcu_read_lock();
1318 pf = rcu_dereference(net_families[family]);
1319 err = -EAFNOSUPPORT;
1320 if (!pf)
1321 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
1323 /*
1324 * We will call the ->create function, that possibly is in a loadable
1325 * module, so we have to bump that loadable module refcnt first.
1326 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001327 if (!try_module_get(pf->owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 goto out_release;
1329
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001330 /* Now protected by module ref count */
1331 rcu_read_unlock();
1332
Eric Paris3f378b62009-11-05 22:18:14 -08001333 err = pf->create(net, sock, protocol, kern);
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001334 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 goto out_module_put;
Frank Filza79af592005-09-27 15:23:38 -07001336
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 /*
1338 * Now to bump the refcnt of the [loadable] module that owns this
1339 * socket at sock_release time we decrement its refcnt.
1340 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001341 if (!try_module_get(sock->ops->owner))
1342 goto out_module_busy;
1343
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 /*
1345 * Now that we're done with the ->create function, the [loadable]
1346 * module can have its refcnt decremented
1347 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001348 module_put(pf->owner);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001349 err = security_socket_post_create(sock, family, type, protocol, kern);
1350 if (err)
Herbert Xu3b185522007-08-15 14:46:02 -07001351 goto out_sock_release;
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001352 *res = sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001354 return 0;
1355
1356out_module_busy:
1357 err = -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358out_module_put:
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001359 sock->ops = NULL;
1360 module_put(pf->owner);
1361out_sock_release:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 sock_release(sock);
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001363 return err;
1364
1365out_release:
1366 rcu_read_unlock();
1367 goto out_sock_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368}
Pavel Emelyanov721db932010-09-29 16:06:32 +04001369EXPORT_SYMBOL(__sock_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
1371int sock_create(int family, int type, int protocol, struct socket **res)
1372{
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001373 return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001375EXPORT_SYMBOL(sock_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
1377int sock_create_kern(int family, int type, int protocol, struct socket **res)
1378{
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001379 return __sock_create(&init_net, family, type, protocol, res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001381EXPORT_SYMBOL(sock_create_kern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001383SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384{
1385 int retval;
1386 struct socket *sock;
Ulrich Dreppera677a032008-07-23 21:29:17 -07001387 int flags;
1388
Ulrich Dreppere38b36f2008-07-23 21:29:42 -07001389 /* Check the SOCK_* constants for consistency. */
1390 BUILD_BUG_ON(SOCK_CLOEXEC != O_CLOEXEC);
1391 BUILD_BUG_ON((SOCK_MAX | SOCK_TYPE_MASK) != SOCK_TYPE_MASK);
1392 BUILD_BUG_ON(SOCK_CLOEXEC & SOCK_TYPE_MASK);
1393 BUILD_BUG_ON(SOCK_NONBLOCK & SOCK_TYPE_MASK);
1394
Ulrich Dreppera677a032008-07-23 21:29:17 -07001395 flags = type & ~SOCK_TYPE_MASK;
Ulrich Drepper77d27202008-07-23 21:29:35 -07001396 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Dreppera677a032008-07-23 21:29:17 -07001397 return -EINVAL;
1398 type &= SOCK_TYPE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001400 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1401 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 retval = sock_create(family, type, protocol, &sock);
1404 if (retval < 0)
1405 goto out;
1406
Ulrich Drepper77d27202008-07-23 21:29:35 -07001407 retval = sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 if (retval < 0)
1409 goto out_release;
1410
1411out:
1412 /* It may be already another descriptor 8) Not kernel problem. */
1413 return retval;
1414
1415out_release:
1416 sock_release(sock);
1417 return retval;
1418}
1419
1420/*
1421 * Create a pair of connected sockets.
1422 */
1423
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001424SYSCALL_DEFINE4(socketpair, int, family, int, type, int, protocol,
1425 int __user *, usockvec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426{
1427 struct socket *sock1, *sock2;
1428 int fd1, fd2, err;
Al Virodb349502007-02-07 01:48:00 -05001429 struct file *newfile1, *newfile2;
Ulrich Dreppera677a032008-07-23 21:29:17 -07001430 int flags;
1431
1432 flags = type & ~SOCK_TYPE_MASK;
Ulrich Drepper77d27202008-07-23 21:29:35 -07001433 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Dreppera677a032008-07-23 21:29:17 -07001434 return -EINVAL;
1435 type &= SOCK_TYPE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001437 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1438 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1439
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 /*
1441 * Obtain the first socket and check if the underlying protocol
1442 * supports the socketpair call.
1443 */
1444
1445 err = sock_create(family, type, protocol, &sock1);
1446 if (err < 0)
1447 goto out;
1448
1449 err = sock_create(family, type, protocol, &sock2);
1450 if (err < 0)
1451 goto out_release_1;
1452
1453 err = sock1->ops->socketpair(sock1, sock2);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001454 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 goto out_release_both;
1456
Al Viro28407632012-08-17 23:54:15 -04001457 fd1 = get_unused_fd_flags(flags);
David S. Millerbf3c23d2007-10-29 21:54:02 -07001458 if (unlikely(fd1 < 0)) {
1459 err = fd1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 goto out_release_both;
David S. Millerbf3c23d2007-10-29 21:54:02 -07001461 }
Al Viro28407632012-08-17 23:54:15 -04001462 fd2 = get_unused_fd_flags(flags);
Al Viro198de4d2009-08-05 19:29:23 +04001463 if (unlikely(fd2 < 0)) {
1464 err = fd2;
Al Viro28407632012-08-17 23:54:15 -04001465 put_unused_fd(fd1);
1466 goto out_release_both;
1467 }
1468
Linus Torvaldsaab174f2012-10-02 20:25:04 -07001469 newfile1 = sock_alloc_file(sock1, flags, NULL);
Al Viro28407632012-08-17 23:54:15 -04001470 if (unlikely(IS_ERR(newfile1))) {
1471 err = PTR_ERR(newfile1);
1472 put_unused_fd(fd1);
1473 put_unused_fd(fd2);
1474 goto out_release_both;
1475 }
1476
Linus Torvaldsaab174f2012-10-02 20:25:04 -07001477 newfile2 = sock_alloc_file(sock2, flags, NULL);
Al Viro28407632012-08-17 23:54:15 -04001478 if (IS_ERR(newfile2)) {
1479 err = PTR_ERR(newfile2);
Al Viro198de4d2009-08-05 19:29:23 +04001480 fput(newfile1);
1481 put_unused_fd(fd1);
Al Viro28407632012-08-17 23:54:15 -04001482 put_unused_fd(fd2);
Al Viro198de4d2009-08-05 19:29:23 +04001483 sock_release(sock2);
1484 goto out;
Al Virodb349502007-02-07 01:48:00 -05001485 }
1486
Al Viro157cf642008-12-14 04:57:47 -05001487 audit_fd_pair(fd1, fd2);
Al Virodb349502007-02-07 01:48:00 -05001488 fd_install(fd1, newfile1);
1489 fd_install(fd2, newfile2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 /* fd1 and fd2 may be already another descriptors.
1491 * Not kernel problem.
1492 */
1493
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001494 err = put_user(fd1, &usockvec[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 if (!err)
1496 err = put_user(fd2, &usockvec[1]);
1497 if (!err)
1498 return 0;
1499
1500 sys_close(fd2);
1501 sys_close(fd1);
1502 return err;
1503
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504out_release_both:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001505 sock_release(sock2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506out_release_1:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001507 sock_release(sock1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508out:
1509 return err;
1510}
1511
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512/*
1513 * Bind a name to a socket. Nothing much to do here since it's
1514 * the protocol's responsibility to handle the local address.
1515 *
1516 * We move the socket address to kernel space before we call
1517 * the protocol layer (having also checked the address is ok).
1518 */
1519
Heiko Carstens20f37032009-01-14 14:14:23 +01001520SYSCALL_DEFINE3(bind, int, fd, struct sockaddr __user *, umyaddr, int, addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521{
1522 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001523 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001524 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001526 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001527 if (sock) {
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001528 err = move_addr_to_kernel(umyaddr, addrlen, &address);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001529 if (err >= 0) {
1530 err = security_socket_bind(sock,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001531 (struct sockaddr *)&address,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001532 addrlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001533 if (!err)
1534 err = sock->ops->bind(sock,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001535 (struct sockaddr *)
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001536 &address, addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 }
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001538 fput_light(sock->file, fput_needed);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 return err;
1541}
1542
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543/*
1544 * Perform a listen. Basically, we allow the protocol to do anything
1545 * necessary for a listen, and if that works, we mark the socket as
1546 * ready for listening.
1547 */
1548
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001549SYSCALL_DEFINE2(listen, int, fd, int, backlog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550{
1551 struct socket *sock;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001552 int err, fput_needed;
Pavel Emelyanovb8e1f9b2007-12-08 00:12:33 -08001553 int somaxconn;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001554
1555 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1556 if (sock) {
Pavel Emelyanov8efa6e92008-03-31 19:41:14 -07001557 somaxconn = sock_net(sock->sk)->core.sysctl_somaxconn;
Eric Dumazet95c96172012-04-15 05:58:06 +00001558 if ((unsigned int)backlog > somaxconn)
Pavel Emelyanovb8e1f9b2007-12-08 00:12:33 -08001559 backlog = somaxconn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
1561 err = security_socket_listen(sock, backlog);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001562 if (!err)
1563 err = sock->ops->listen(sock, backlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001565 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 }
1567 return err;
1568}
1569
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570/*
1571 * For accept, we attempt to create a new socket, set up the link
1572 * with the client, wake up the client, then return the new
1573 * connected fd. We collect the address of the connector in kernel
1574 * space and move it to user at the very end. This is unclean because
1575 * we open the socket then return an error.
1576 *
1577 * 1003.1g adds the ability to recvmsg() to query connection pending
1578 * status to recvmsg. We need to add that support in a way thats
1579 * clean when we restucture accept also.
1580 */
1581
Heiko Carstens20f37032009-01-14 14:14:23 +01001582SYSCALL_DEFINE4(accept4, int, fd, struct sockaddr __user *, upeer_sockaddr,
1583 int __user *, upeer_addrlen, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584{
1585 struct socket *sock, *newsock;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001586 struct file *newfile;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001587 int err, len, newfd, fput_needed;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001588 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
Ulrich Drepper77d27202008-07-23 21:29:35 -07001590 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001591 return -EINVAL;
1592
1593 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1594 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1595
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001596 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 if (!sock)
1598 goto out;
1599
1600 err = -ENFILE;
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001601 newsock = sock_alloc();
1602 if (!newsock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 goto out_put;
1604
1605 newsock->type = sock->type;
1606 newsock->ops = sock->ops;
1607
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 /*
1609 * We don't need try_module_get here, as the listening socket (sock)
1610 * has the protocol module (sock->ops->owner) held.
1611 */
1612 __module_get(newsock->ops->owner);
1613
Al Viro28407632012-08-17 23:54:15 -04001614 newfd = get_unused_fd_flags(flags);
David S. Miller39d8c1b2006-03-20 17:13:49 -08001615 if (unlikely(newfd < 0)) {
1616 err = newfd;
David S. Miller9a1875e2006-04-01 12:48:36 -08001617 sock_release(newsock);
1618 goto out_put;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001619 }
Linus Torvaldsaab174f2012-10-02 20:25:04 -07001620 newfile = sock_alloc_file(newsock, flags, sock->sk->sk_prot_creator->name);
Al Viro28407632012-08-17 23:54:15 -04001621 if (unlikely(IS_ERR(newfile))) {
1622 err = PTR_ERR(newfile);
1623 put_unused_fd(newfd);
1624 sock_release(newsock);
1625 goto out_put;
1626 }
David S. Miller39d8c1b2006-03-20 17:13:49 -08001627
Frank Filza79af592005-09-27 15:23:38 -07001628 err = security_socket_accept(sock, newsock);
1629 if (err)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001630 goto out_fd;
Frank Filza79af592005-09-27 15:23:38 -07001631
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 err = sock->ops->accept(sock, newsock, sock->file->f_flags);
1633 if (err < 0)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001634 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
1636 if (upeer_sockaddr) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001637 if (newsock->ops->getname(newsock, (struct sockaddr *)&address,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001638 &len, 2) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 err = -ECONNABORTED;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001640 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 }
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001642 err = move_addr_to_user(&address,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001643 len, upeer_sockaddr, upeer_addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 if (err < 0)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001645 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 }
1647
1648 /* File flags are not inherited via accept() unlike another OSes. */
1649
David S. Miller39d8c1b2006-03-20 17:13:49 -08001650 fd_install(newfd, newfile);
1651 err = newfd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001654 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655out:
1656 return err;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001657out_fd:
David S. Miller9606a212006-04-01 01:00:14 -08001658 fput(newfile);
David S. Miller39d8c1b2006-03-20 17:13:49 -08001659 put_unused_fd(newfd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 goto out_put;
1661}
1662
Heiko Carstens20f37032009-01-14 14:14:23 +01001663SYSCALL_DEFINE3(accept, int, fd, struct sockaddr __user *, upeer_sockaddr,
1664 int __user *, upeer_addrlen)
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001665{
Ulrich Drepperde11def2008-11-19 15:36:14 -08001666 return sys_accept4(fd, upeer_sockaddr, upeer_addrlen, 0);
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001667}
1668
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669/*
1670 * Attempt to connect to a socket with the server address. The address
1671 * is in user space so we verify it is OK and move it to kernel space.
1672 *
1673 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
1674 * break bindings
1675 *
1676 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
1677 * other SEQPACKET protocols that take time to connect() as it doesn't
1678 * include the -EINPROGRESS status for such sockets.
1679 */
1680
Heiko Carstens20f37032009-01-14 14:14:23 +01001681SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
1682 int, addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683{
1684 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001685 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001686 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001688 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 if (!sock)
1690 goto out;
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001691 err = move_addr_to_kernel(uservaddr, addrlen, &address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 if (err < 0)
1693 goto out_put;
1694
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001695 err =
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001696 security_socket_connect(sock, (struct sockaddr *)&address, addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 if (err)
1698 goto out_put;
1699
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001700 err = sock->ops->connect(sock, (struct sockaddr *)&address, addrlen,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 sock->file->f_flags);
1702out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001703 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704out:
1705 return err;
1706}
1707
1708/*
1709 * Get the local address ('name') of a socket object. Move the obtained
1710 * name to user space.
1711 */
1712
Heiko Carstens20f37032009-01-14 14:14:23 +01001713SYSCALL_DEFINE3(getsockname, int, fd, struct sockaddr __user *, usockaddr,
1714 int __user *, usockaddr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715{
1716 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001717 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001718 int len, err, fput_needed;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001719
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001720 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 if (!sock)
1722 goto out;
1723
1724 err = security_socket_getsockname(sock);
1725 if (err)
1726 goto out_put;
1727
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001728 err = sock->ops->getname(sock, (struct sockaddr *)&address, &len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 if (err)
1730 goto out_put;
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001731 err = move_addr_to_user(&address, len, usockaddr, usockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
1733out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001734 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735out:
1736 return err;
1737}
1738
1739/*
1740 * Get the remote address ('name') of a socket object. Move the obtained
1741 * name to user space.
1742 */
1743
Heiko Carstens20f37032009-01-14 14:14:23 +01001744SYSCALL_DEFINE3(getpeername, int, fd, struct sockaddr __user *, usockaddr,
1745 int __user *, usockaddr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746{
1747 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001748 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001749 int len, err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001751 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1752 if (sock != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 err = security_socket_getpeername(sock);
1754 if (err) {
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001755 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 return err;
1757 }
1758
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001759 err =
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001760 sock->ops->getname(sock, (struct sockaddr *)&address, &len,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001761 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 if (!err)
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001763 err = move_addr_to_user(&address, len, usockaddr,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001764 usockaddr_len);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001765 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 }
1767 return err;
1768}
1769
1770/*
1771 * Send a datagram to a given address. We move the address into kernel
1772 * space and check the user space data area is readable before invoking
1773 * the protocol.
1774 */
1775
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001776SYSCALL_DEFINE6(sendto, int, fd, void __user *, buff, size_t, len,
Eric Dumazet95c96172012-04-15 05:58:06 +00001777 unsigned int, flags, struct sockaddr __user *, addr,
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001778 int, addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779{
1780 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001781 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 int err;
1783 struct msghdr msg;
1784 struct iovec iov;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001785 int fput_needed;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001786
Linus Torvalds253eacc2010-10-30 16:43:10 -07001787 if (len > INT_MAX)
1788 len = INT_MAX;
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001789 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1790 if (!sock)
David S. Miller4387ff72007-02-08 15:06:08 -08001791 goto out;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001792
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001793 iov.iov_base = buff;
1794 iov.iov_len = len;
1795 msg.msg_name = NULL;
1796 msg.msg_iov = &iov;
1797 msg.msg_iovlen = 1;
1798 msg.msg_control = NULL;
1799 msg.msg_controllen = 0;
1800 msg.msg_namelen = 0;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001801 if (addr) {
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001802 err = move_addr_to_kernel(addr, addr_len, &address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 if (err < 0)
1804 goto out_put;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001805 msg.msg_name = (struct sockaddr *)&address;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001806 msg.msg_namelen = addr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 }
1808 if (sock->file->f_flags & O_NONBLOCK)
1809 flags |= MSG_DONTWAIT;
1810 msg.msg_flags = flags;
1811 err = sock_sendmsg(sock, &msg, len);
1812
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001813out_put:
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001814 fput_light(sock->file, fput_needed);
David S. Miller4387ff72007-02-08 15:06:08 -08001815out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 return err;
1817}
1818
1819/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001820 * Send a datagram down a socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 */
1822
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001823SYSCALL_DEFINE4(send, int, fd, void __user *, buff, size_t, len,
Eric Dumazet95c96172012-04-15 05:58:06 +00001824 unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825{
1826 return sys_sendto(fd, buff, len, flags, NULL, 0);
1827}
1828
1829/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001830 * Receive a frame from the socket and optionally record the address of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 * sender. We verify the buffers are writable and if needed move the
1832 * sender address from kernel to user space.
1833 */
1834
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001835SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, ubuf, size_t, size,
Eric Dumazet95c96172012-04-15 05:58:06 +00001836 unsigned int, flags, struct sockaddr __user *, addr,
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001837 int __user *, addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838{
1839 struct socket *sock;
1840 struct iovec iov;
1841 struct msghdr msg;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001842 struct sockaddr_storage address;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001843 int err, err2;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001844 int fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
Linus Torvalds253eacc2010-10-30 16:43:10 -07001846 if (size > INT_MAX)
1847 size = INT_MAX;
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001848 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 if (!sock)
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001850 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001852 msg.msg_control = NULL;
1853 msg.msg_controllen = 0;
1854 msg.msg_iovlen = 1;
1855 msg.msg_iov = &iov;
1856 iov.iov_len = size;
1857 iov.iov_base = ubuf;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001858 msg.msg_name = (struct sockaddr *)&address;
1859 msg.msg_namelen = sizeof(address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 if (sock->file->f_flags & O_NONBLOCK)
1861 flags |= MSG_DONTWAIT;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001862 err = sock_recvmsg(sock, &msg, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001864 if (err >= 0 && addr != NULL) {
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001865 err2 = move_addr_to_user(&address,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001866 msg.msg_namelen, addr, addr_len);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001867 if (err2 < 0)
1868 err = err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 }
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001870
1871 fput_light(sock->file, fput_needed);
David S. Miller4387ff72007-02-08 15:06:08 -08001872out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 return err;
1874}
1875
1876/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001877 * Receive a datagram from a socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 */
1879
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001880asmlinkage long sys_recv(int fd, void __user *ubuf, size_t size,
Eric Dumazet95c96172012-04-15 05:58:06 +00001881 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882{
1883 return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
1884}
1885
1886/*
1887 * Set a socket option. Because we don't know the option lengths we have
1888 * to pass the user mode parameter for the protocols to sort out.
1889 */
1890
Heiko Carstens20f37032009-01-14 14:14:23 +01001891SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname,
1892 char __user *, optval, int, optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001894 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 struct socket *sock;
1896
1897 if (optlen < 0)
1898 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001899
1900 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1901 if (sock != NULL) {
1902 err = security_socket_setsockopt(sock, level, optname);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001903 if (err)
1904 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
1906 if (level == SOL_SOCKET)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001907 err =
1908 sock_setsockopt(sock, level, optname, optval,
1909 optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 else
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001911 err =
1912 sock->ops->setsockopt(sock, level, optname, optval,
1913 optlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001914out_put:
1915 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 }
1917 return err;
1918}
1919
1920/*
1921 * Get a socket option. Because we don't know the option lengths we have
1922 * to pass a user mode parameter for the protocols to sort out.
1923 */
1924
Heiko Carstens20f37032009-01-14 14:14:23 +01001925SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname,
1926 char __user *, optval, int __user *, optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001928 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 struct socket *sock;
1930
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001931 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1932 if (sock != NULL) {
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001933 err = security_socket_getsockopt(sock, level, optname);
1934 if (err)
1935 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
1937 if (level == SOL_SOCKET)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001938 err =
1939 sock_getsockopt(sock, level, optname, optval,
1940 optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 else
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001942 err =
1943 sock->ops->getsockopt(sock, level, optname, optval,
1944 optlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001945out_put:
1946 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 }
1948 return err;
1949}
1950
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951/*
1952 * Shutdown a socket.
1953 */
1954
Heiko Carstens754fe8d2009-01-14 14:14:09 +01001955SYSCALL_DEFINE2(shutdown, int, fd, int, how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001957 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 struct socket *sock;
1959
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001960 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1961 if (sock != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 err = security_socket_shutdown(sock, how);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001963 if (!err)
1964 err = sock->ops->shutdown(sock, how);
1965 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 }
1967 return err;
1968}
1969
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001970/* A couple of helpful macros for getting the address of the 32/64 bit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 * fields which are the same type (int / unsigned) on our platforms.
1972 */
1973#define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
1974#define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
1975#define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
1976
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00001977struct used_address {
1978 struct sockaddr_storage name;
1979 unsigned int name_len;
1980};
1981
Anton Blanchard228e5482011-05-02 20:21:35 +00001982static int __sys_sendmsg(struct socket *sock, struct msghdr __user *msg,
Eric Dumazet95c96172012-04-15 05:58:06 +00001983 struct msghdr *msg_sys, unsigned int flags,
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00001984 struct used_address *used_address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985{
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001986 struct compat_msghdr __user *msg_compat =
1987 (struct compat_msghdr __user *)msg;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001988 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
Alex Williamsonb9d717a2005-09-26 14:28:02 -07001990 unsigned char ctl[sizeof(struct cmsghdr) + 20]
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001991 __attribute__ ((aligned(sizeof(__kernel_size_t))));
1992 /* 20 is size of ipv6_pktinfo */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 unsigned char *ctl_buf = ctl;
Eric Dumazeta74e9102012-04-20 20:04:01 +02001994 int err, ctl_len, total_len;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001995
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 err = -EFAULT;
1997 if (MSG_CMSG_COMPAT & flags) {
Anton Blanchard228e5482011-05-02 20:21:35 +00001998 if (get_compat_msghdr(msg_sys, msg_compat))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 return -EFAULT;
Anton Blanchard228e5482011-05-02 20:21:35 +00002000 } else if (copy_from_user(msg_sys, msg, sizeof(struct msghdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 return -EFAULT;
2002
Anton Blanchard228e5482011-05-02 20:21:35 +00002003 if (msg_sys->msg_iovlen > UIO_FASTIOV) {
Eric Dumazeta74e9102012-04-20 20:04:01 +02002004 err = -EMSGSIZE;
2005 if (msg_sys->msg_iovlen > UIO_MAXIOV)
2006 goto out;
2007 err = -ENOMEM;
2008 iov = kmalloc(msg_sys->msg_iovlen * sizeof(struct iovec),
2009 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 if (!iov)
Anton Blanchard228e5482011-05-02 20:21:35 +00002011 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 }
2013
2014 /* This will also move the address data into kernel space */
2015 if (MSG_CMSG_COMPAT & flags) {
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00002016 err = verify_compat_iovec(msg_sys, iov, &address, VERIFY_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 } else
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00002018 err = verify_iovec(msg_sys, iov, &address, VERIFY_READ);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002019 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 goto out_freeiov;
2021 total_len = err;
2022
2023 err = -ENOBUFS;
2024
Anton Blanchard228e5482011-05-02 20:21:35 +00002025 if (msg_sys->msg_controllen > INT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 goto out_freeiov;
Anton Blanchard228e5482011-05-02 20:21:35 +00002027 ctl_len = msg_sys->msg_controllen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002029 err =
Anton Blanchard228e5482011-05-02 20:21:35 +00002030 cmsghdr_from_user_compat_to_kern(msg_sys, sock->sk, ctl,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002031 sizeof(ctl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 if (err)
2033 goto out_freeiov;
Anton Blanchard228e5482011-05-02 20:21:35 +00002034 ctl_buf = msg_sys->msg_control;
2035 ctl_len = msg_sys->msg_controllen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 } else if (ctl_len) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002037 if (ctl_len > sizeof(ctl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002039 if (ctl_buf == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 goto out_freeiov;
2041 }
2042 err = -EFAULT;
2043 /*
Anton Blanchard228e5482011-05-02 20:21:35 +00002044 * Careful! Before this, msg_sys->msg_control contains a user pointer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
2046 * checking falls down on this.
2047 */
Namhyung Kimfb8621b2010-09-07 03:55:00 +00002048 if (copy_from_user(ctl_buf,
Anton Blanchard228e5482011-05-02 20:21:35 +00002049 (void __user __force *)msg_sys->msg_control,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002050 ctl_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 goto out_freectl;
Anton Blanchard228e5482011-05-02 20:21:35 +00002052 msg_sys->msg_control = ctl_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 }
Anton Blanchard228e5482011-05-02 20:21:35 +00002054 msg_sys->msg_flags = flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
2056 if (sock->file->f_flags & O_NONBLOCK)
Anton Blanchard228e5482011-05-02 20:21:35 +00002057 msg_sys->msg_flags |= MSG_DONTWAIT;
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002058 /*
2059 * If this is sendmmsg() and current destination address is same as
2060 * previously succeeded address, omit asking LSM's decision.
2061 * used_address->name_len is initialized to UINT_MAX so that the first
2062 * destination address never matches.
2063 */
Mathieu Desnoyersbc909d92011-08-24 19:45:03 -07002064 if (used_address && msg_sys->msg_name &&
2065 used_address->name_len == msg_sys->msg_namelen &&
2066 !memcmp(&used_address->name, msg_sys->msg_name,
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002067 used_address->name_len)) {
2068 err = sock_sendmsg_nosec(sock, msg_sys, total_len);
2069 goto out_freectl;
2070 }
2071 err = sock_sendmsg(sock, msg_sys, total_len);
2072 /*
2073 * If this is sendmmsg() and sending to current destination address was
2074 * successful, remember it.
2075 */
2076 if (used_address && err >= 0) {
2077 used_address->name_len = msg_sys->msg_namelen;
Mathieu Desnoyersbc909d92011-08-24 19:45:03 -07002078 if (msg_sys->msg_name)
2079 memcpy(&used_address->name, msg_sys->msg_name,
2080 used_address->name_len);
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002081 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
2083out_freectl:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002084 if (ctl_buf != ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 sock_kfree_s(sock->sk, ctl_buf, ctl_len);
2086out_freeiov:
2087 if (iov != iovstack)
Eric Dumazeta74e9102012-04-20 20:04:01 +02002088 kfree(iov);
Anton Blanchard228e5482011-05-02 20:21:35 +00002089out:
2090 return err;
2091}
2092
2093/*
2094 * BSD sendmsg interface
2095 */
2096
Eric Dumazet95c96172012-04-15 05:58:06 +00002097SYSCALL_DEFINE3(sendmsg, int, fd, struct msghdr __user *, msg, unsigned int, flags)
Anton Blanchard228e5482011-05-02 20:21:35 +00002098{
2099 int fput_needed, err;
2100 struct msghdr msg_sys;
2101 struct socket *sock = sockfd_lookup_light(fd, &err, &fput_needed);
2102
2103 if (!sock)
2104 goto out;
2105
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002106 err = __sys_sendmsg(sock, msg, &msg_sys, flags, NULL);
Anton Blanchard228e5482011-05-02 20:21:35 +00002107
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002108 fput_light(sock->file, fput_needed);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002109out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 return err;
2111}
2112
Anton Blanchard228e5482011-05-02 20:21:35 +00002113/*
2114 * Linux sendmmsg interface
2115 */
2116
2117int __sys_sendmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
2118 unsigned int flags)
2119{
2120 int fput_needed, err, datagrams;
2121 struct socket *sock;
2122 struct mmsghdr __user *entry;
2123 struct compat_mmsghdr __user *compat_entry;
2124 struct msghdr msg_sys;
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002125 struct used_address used_address;
Anton Blanchard228e5482011-05-02 20:21:35 +00002126
Anton Blanchard98382f42011-08-04 14:07:39 +00002127 if (vlen > UIO_MAXIOV)
2128 vlen = UIO_MAXIOV;
Anton Blanchard228e5482011-05-02 20:21:35 +00002129
2130 datagrams = 0;
2131
2132 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2133 if (!sock)
2134 return err;
2135
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002136 used_address.name_len = UINT_MAX;
Anton Blanchard228e5482011-05-02 20:21:35 +00002137 entry = mmsg;
2138 compat_entry = (struct compat_mmsghdr __user *)mmsg;
Anton Blanchard728ffb82011-08-04 14:07:38 +00002139 err = 0;
Anton Blanchard228e5482011-05-02 20:21:35 +00002140
2141 while (datagrams < vlen) {
Anton Blanchard228e5482011-05-02 20:21:35 +00002142 if (MSG_CMSG_COMPAT & flags) {
2143 err = __sys_sendmsg(sock, (struct msghdr __user *)compat_entry,
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002144 &msg_sys, flags, &used_address);
Anton Blanchard228e5482011-05-02 20:21:35 +00002145 if (err < 0)
2146 break;
2147 err = __put_user(err, &compat_entry->msg_len);
2148 ++compat_entry;
2149 } else {
2150 err = __sys_sendmsg(sock, (struct msghdr __user *)entry,
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002151 &msg_sys, flags, &used_address);
Anton Blanchard228e5482011-05-02 20:21:35 +00002152 if (err < 0)
2153 break;
2154 err = put_user(err, &entry->msg_len);
2155 ++entry;
2156 }
2157
2158 if (err)
2159 break;
2160 ++datagrams;
2161 }
2162
Anton Blanchard228e5482011-05-02 20:21:35 +00002163 fput_light(sock->file, fput_needed);
2164
Anton Blanchard728ffb82011-08-04 14:07:38 +00002165 /* We only return an error if no datagrams were able to be sent */
2166 if (datagrams != 0)
Anton Blanchard228e5482011-05-02 20:21:35 +00002167 return datagrams;
2168
Anton Blanchard228e5482011-05-02 20:21:35 +00002169 return err;
2170}
2171
2172SYSCALL_DEFINE4(sendmmsg, int, fd, struct mmsghdr __user *, mmsg,
2173 unsigned int, vlen, unsigned int, flags)
2174{
2175 return __sys_sendmmsg(fd, mmsg, vlen, flags);
2176}
2177
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002178static int __sys_recvmsg(struct socket *sock, struct msghdr __user *msg,
Eric Dumazet95c96172012-04-15 05:58:06 +00002179 struct msghdr *msg_sys, unsigned int flags, int nosec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180{
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002181 struct compat_msghdr __user *msg_compat =
2182 (struct compat_msghdr __user *)msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 struct iovec iovstack[UIO_FASTIOV];
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002184 struct iovec *iov = iovstack;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 unsigned long cmsg_ptr;
Eric Dumazeta74e9102012-04-20 20:04:01 +02002186 int err, total_len, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
2188 /* kernel mode address */
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07002189 struct sockaddr_storage addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
2191 /* user mode address pointers */
2192 struct sockaddr __user *uaddr;
2193 int __user *uaddr_len;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002194
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 if (MSG_CMSG_COMPAT & flags) {
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002196 if (get_compat_msghdr(msg_sys, msg_compat))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 return -EFAULT;
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002198 } else if (copy_from_user(msg_sys, msg, sizeof(struct msghdr)))
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002199 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002201 if (msg_sys->msg_iovlen > UIO_FASTIOV) {
Eric Dumazeta74e9102012-04-20 20:04:01 +02002202 err = -EMSGSIZE;
2203 if (msg_sys->msg_iovlen > UIO_MAXIOV)
2204 goto out;
2205 err = -ENOMEM;
2206 iov = kmalloc(msg_sys->msg_iovlen * sizeof(struct iovec),
2207 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 if (!iov)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002209 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 }
2211
2212 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002213 * Save the user-mode address (verify_iovec will change the
2214 * kernel msghdr to use the kernel address space)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002216
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002217 uaddr = (__force void __user *)msg_sys->msg_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 uaddr_len = COMPAT_NAMELEN(msg);
2219 if (MSG_CMSG_COMPAT & flags) {
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00002220 err = verify_compat_iovec(msg_sys, iov, &addr, VERIFY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 } else
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00002222 err = verify_iovec(msg_sys, iov, &addr, VERIFY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 if (err < 0)
2224 goto out_freeiov;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002225 total_len = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002227 cmsg_ptr = (unsigned long)msg_sys->msg_control;
2228 msg_sys->msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002229
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 if (sock->file->f_flags & O_NONBLOCK)
2231 flags |= MSG_DONTWAIT;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002232 err = (nosec ? sock_recvmsg_nosec : sock_recvmsg)(sock, msg_sys,
2233 total_len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 if (err < 0)
2235 goto out_freeiov;
2236 len = err;
2237
2238 if (uaddr != NULL) {
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00002239 err = move_addr_to_user(&addr,
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002240 msg_sys->msg_namelen, uaddr,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002241 uaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 if (err < 0)
2243 goto out_freeiov;
2244 }
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002245 err = __put_user((msg_sys->msg_flags & ~MSG_CMSG_COMPAT),
David S. Miller37f7f422005-09-16 16:51:01 -07002246 COMPAT_FLAGS(msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 if (err)
2248 goto out_freeiov;
2249 if (MSG_CMSG_COMPAT & flags)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002250 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 &msg_compat->msg_controllen);
2252 else
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002253 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 &msg->msg_controllen);
2255 if (err)
2256 goto out_freeiov;
2257 err = len;
2258
2259out_freeiov:
2260 if (iov != iovstack)
Eric Dumazeta74e9102012-04-20 20:04:01 +02002261 kfree(iov);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002262out:
2263 return err;
2264}
2265
2266/*
2267 * BSD recvmsg interface
2268 */
2269
2270SYSCALL_DEFINE3(recvmsg, int, fd, struct msghdr __user *, msg,
2271 unsigned int, flags)
2272{
2273 int fput_needed, err;
2274 struct msghdr msg_sys;
2275 struct socket *sock = sockfd_lookup_light(fd, &err, &fput_needed);
2276
2277 if (!sock)
2278 goto out;
2279
2280 err = __sys_recvmsg(sock, msg, &msg_sys, flags, 0);
2281
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002282 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283out:
2284 return err;
2285}
2286
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002287/*
2288 * Linux recvmmsg interface
2289 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002291int __sys_recvmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
2292 unsigned int flags, struct timespec *timeout)
2293{
2294 int fput_needed, err, datagrams;
2295 struct socket *sock;
2296 struct mmsghdr __user *entry;
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002297 struct compat_mmsghdr __user *compat_entry;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002298 struct msghdr msg_sys;
2299 struct timespec end_time;
2300
2301 if (timeout &&
2302 poll_select_set_timeout(&end_time, timeout->tv_sec,
2303 timeout->tv_nsec))
2304 return -EINVAL;
2305
2306 datagrams = 0;
2307
2308 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2309 if (!sock)
2310 return err;
2311
2312 err = sock_error(sock->sk);
2313 if (err)
2314 goto out_put;
2315
2316 entry = mmsg;
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002317 compat_entry = (struct compat_mmsghdr __user *)mmsg;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002318
2319 while (datagrams < vlen) {
2320 /*
2321 * No need to ask LSM for more than the first datagram.
2322 */
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002323 if (MSG_CMSG_COMPAT & flags) {
2324 err = __sys_recvmsg(sock, (struct msghdr __user *)compat_entry,
Anton Blanchardb9eb8b82011-05-17 15:38:57 -04002325 &msg_sys, flags & ~MSG_WAITFORONE,
2326 datagrams);
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002327 if (err < 0)
2328 break;
2329 err = __put_user(err, &compat_entry->msg_len);
2330 ++compat_entry;
2331 } else {
2332 err = __sys_recvmsg(sock, (struct msghdr __user *)entry,
Anton Blanchardb9eb8b82011-05-17 15:38:57 -04002333 &msg_sys, flags & ~MSG_WAITFORONE,
2334 datagrams);
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002335 if (err < 0)
2336 break;
2337 err = put_user(err, &entry->msg_len);
2338 ++entry;
2339 }
2340
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002341 if (err)
2342 break;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002343 ++datagrams;
2344
Brandon L Black71c5c152010-03-26 16:18:03 +00002345 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
2346 if (flags & MSG_WAITFORONE)
2347 flags |= MSG_DONTWAIT;
2348
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002349 if (timeout) {
2350 ktime_get_ts(timeout);
2351 *timeout = timespec_sub(end_time, *timeout);
2352 if (timeout->tv_sec < 0) {
2353 timeout->tv_sec = timeout->tv_nsec = 0;
2354 break;
2355 }
2356
2357 /* Timeout, return less than vlen datagrams */
2358 if (timeout->tv_nsec == 0 && timeout->tv_sec == 0)
2359 break;
2360 }
2361
2362 /* Out of band data, return right away */
2363 if (msg_sys.msg_flags & MSG_OOB)
2364 break;
2365 }
2366
2367out_put:
2368 fput_light(sock->file, fput_needed);
2369
2370 if (err == 0)
2371 return datagrams;
2372
2373 if (datagrams != 0) {
2374 /*
2375 * We may return less entries than requested (vlen) if the
2376 * sock is non block and there aren't enough datagrams...
2377 */
2378 if (err != -EAGAIN) {
2379 /*
2380 * ... or if recvmsg returns an error after we
2381 * received some datagrams, where we record the
2382 * error to return on the next call or if the
2383 * app asks about it using getsockopt(SO_ERROR).
2384 */
2385 sock->sk->sk_err = -err;
2386 }
2387
2388 return datagrams;
2389 }
2390
2391 return err;
2392}
2393
2394SYSCALL_DEFINE5(recvmmsg, int, fd, struct mmsghdr __user *, mmsg,
2395 unsigned int, vlen, unsigned int, flags,
2396 struct timespec __user *, timeout)
2397{
2398 int datagrams;
2399 struct timespec timeout_sys;
2400
2401 if (!timeout)
2402 return __sys_recvmmsg(fd, mmsg, vlen, flags, NULL);
2403
2404 if (copy_from_user(&timeout_sys, timeout, sizeof(timeout_sys)))
2405 return -EFAULT;
2406
2407 datagrams = __sys_recvmmsg(fd, mmsg, vlen, flags, &timeout_sys);
2408
2409 if (datagrams > 0 &&
2410 copy_to_user(timeout, &timeout_sys, sizeof(timeout_sys)))
2411 datagrams = -EFAULT;
2412
2413 return datagrams;
2414}
2415
2416#ifdef __ARCH_WANT_SYS_SOCKETCALL
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417/* Argument list sizes for sys_socketcall */
2418#define AL(x) ((x) * sizeof(unsigned long))
Anton Blanchard228e5482011-05-02 20:21:35 +00002419static const unsigned char nargs[21] = {
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002420 AL(0), AL(3), AL(3), AL(3), AL(2), AL(3),
2421 AL(3), AL(3), AL(4), AL(4), AL(4), AL(6),
2422 AL(6), AL(2), AL(5), AL(5), AL(3), AL(3),
Anton Blanchard228e5482011-05-02 20:21:35 +00002423 AL(4), AL(5), AL(4)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002424};
2425
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426#undef AL
2427
2428/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002429 * System call vectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 *
2431 * Argument checking cleaned up. Saved 20% in size.
2432 * This function doesn't need to set the kernel lock because
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002433 * it is set by the callees.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 */
2435
Heiko Carstens3e0fa652009-01-14 14:14:24 +01002436SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437{
2438 unsigned long a[6];
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002439 unsigned long a0, a1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 int err;
Arjan van de Ven47379052009-09-28 12:57:44 -07002441 unsigned int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442
Anton Blanchard228e5482011-05-02 20:21:35 +00002443 if (call < 1 || call > SYS_SENDMMSG)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 return -EINVAL;
2445
Arjan van de Ven47379052009-09-28 12:57:44 -07002446 len = nargs[call];
2447 if (len > sizeof(a))
2448 return -EINVAL;
2449
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 /* copy_from_user should be SMP safe. */
Arjan van de Ven47379052009-09-28 12:57:44 -07002451 if (copy_from_user(a, args, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 return -EFAULT;
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002453
Al Virof3298dc2008-12-10 03:16:51 -05002454 audit_socketcall(nargs[call] / sizeof(unsigned long), a);
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002455
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002456 a0 = a[0];
2457 a1 = a[1];
2458
2459 switch (call) {
2460 case SYS_SOCKET:
2461 err = sys_socket(a0, a1, a[2]);
2462 break;
2463 case SYS_BIND:
2464 err = sys_bind(a0, (struct sockaddr __user *)a1, a[2]);
2465 break;
2466 case SYS_CONNECT:
2467 err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
2468 break;
2469 case SYS_LISTEN:
2470 err = sys_listen(a0, a1);
2471 break;
2472 case SYS_ACCEPT:
Ulrich Drepperde11def2008-11-19 15:36:14 -08002473 err = sys_accept4(a0, (struct sockaddr __user *)a1,
2474 (int __user *)a[2], 0);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002475 break;
2476 case SYS_GETSOCKNAME:
2477 err =
2478 sys_getsockname(a0, (struct sockaddr __user *)a1,
2479 (int __user *)a[2]);
2480 break;
2481 case SYS_GETPEERNAME:
2482 err =
2483 sys_getpeername(a0, (struct sockaddr __user *)a1,
2484 (int __user *)a[2]);
2485 break;
2486 case SYS_SOCKETPAIR:
2487 err = sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
2488 break;
2489 case SYS_SEND:
2490 err = sys_send(a0, (void __user *)a1, a[2], a[3]);
2491 break;
2492 case SYS_SENDTO:
2493 err = sys_sendto(a0, (void __user *)a1, a[2], a[3],
2494 (struct sockaddr __user *)a[4], a[5]);
2495 break;
2496 case SYS_RECV:
2497 err = sys_recv(a0, (void __user *)a1, a[2], a[3]);
2498 break;
2499 case SYS_RECVFROM:
2500 err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
2501 (struct sockaddr __user *)a[4],
2502 (int __user *)a[5]);
2503 break;
2504 case SYS_SHUTDOWN:
2505 err = sys_shutdown(a0, a1);
2506 break;
2507 case SYS_SETSOCKOPT:
2508 err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]);
2509 break;
2510 case SYS_GETSOCKOPT:
2511 err =
2512 sys_getsockopt(a0, a1, a[2], (char __user *)a[3],
2513 (int __user *)a[4]);
2514 break;
2515 case SYS_SENDMSG:
2516 err = sys_sendmsg(a0, (struct msghdr __user *)a1, a[2]);
2517 break;
Anton Blanchard228e5482011-05-02 20:21:35 +00002518 case SYS_SENDMMSG:
2519 err = sys_sendmmsg(a0, (struct mmsghdr __user *)a1, a[2], a[3]);
2520 break;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002521 case SYS_RECVMSG:
2522 err = sys_recvmsg(a0, (struct msghdr __user *)a1, a[2]);
2523 break;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002524 case SYS_RECVMMSG:
2525 err = sys_recvmmsg(a0, (struct mmsghdr __user *)a1, a[2], a[3],
2526 (struct timespec __user *)a[4]);
2527 break;
Ulrich Drepperde11def2008-11-19 15:36:14 -08002528 case SYS_ACCEPT4:
2529 err = sys_accept4(a0, (struct sockaddr __user *)a1,
2530 (int __user *)a[2], a[3]);
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07002531 break;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002532 default:
2533 err = -EINVAL;
2534 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 }
2536 return err;
2537}
2538
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002539#endif /* __ARCH_WANT_SYS_SOCKETCALL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002541/**
2542 * sock_register - add a socket protocol handler
2543 * @ops: description of protocol
2544 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 * This function is called by a protocol handler that wants to
2546 * advertise its address family, and have it linked into the
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002547 * socket interface. The value ops->family coresponds to the
2548 * socket system call protocol family.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 */
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002550int sock_register(const struct net_proto_family *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551{
2552 int err;
2553
2554 if (ops->family >= NPROTO) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002555 printk(KERN_CRIT "protocol %d >= NPROTO(%d)\n", ops->family,
2556 NPROTO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 return -ENOBUFS;
2558 }
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002559
2560 spin_lock(&net_family_lock);
Eric Dumazet190683a2010-11-10 10:50:44 +00002561 if (rcu_dereference_protected(net_families[ops->family],
2562 lockdep_is_held(&net_family_lock)))
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002563 err = -EEXIST;
2564 else {
Eric Dumazetcf778b02012-01-12 04:41:32 +00002565 rcu_assign_pointer(net_families[ops->family], ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 err = 0;
2567 }
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002568 spin_unlock(&net_family_lock);
2569
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002570 printk(KERN_INFO "NET: Registered protocol family %d\n", ops->family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 return err;
2572}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002573EXPORT_SYMBOL(sock_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002575/**
2576 * sock_unregister - remove a protocol handler
2577 * @family: protocol family to remove
2578 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 * This function is called by a protocol handler that wants to
2580 * remove its address family, and have it unlinked from the
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002581 * new socket creation.
2582 *
2583 * If protocol handler is a module, then it can use module reference
2584 * counts to protect against new references. If protocol handler is not
2585 * a module then it needs to provide its own protection in
2586 * the ops->create routine.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 */
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002588void sock_unregister(int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589{
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002590 BUG_ON(family < 0 || family >= NPROTO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002592 spin_lock(&net_family_lock);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00002593 RCU_INIT_POINTER(net_families[family], NULL);
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002594 spin_unlock(&net_family_lock);
2595
2596 synchronize_rcu();
2597
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002598 printk(KERN_INFO "NET: Unregistered protocol family %d\n", family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002600EXPORT_SYMBOL(sock_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601
Andi Kleen77d76ea2005-12-22 12:43:42 -08002602static int __init sock_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603{
Nick Pigginb3e19d92011-01-07 17:50:11 +11002604 int err;
Eric W. Biederman2ca794e2012-04-19 13:20:32 +00002605 /*
2606 * Initialize the network sysctl infrastructure.
2607 */
2608 err = net_sysctl_init();
2609 if (err)
2610 goto out;
Nick Pigginb3e19d92011-01-07 17:50:11 +11002611
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002613 * Initialize skbuff SLAB cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 */
2615 skb_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616
2617 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002618 * Initialize the protocols module.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 */
2620
2621 init_inodecache();
Nick Pigginb3e19d92011-01-07 17:50:11 +11002622
2623 err = register_filesystem(&sock_fs_type);
2624 if (err)
2625 goto out_fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 sock_mnt = kern_mount(&sock_fs_type);
Nick Pigginb3e19d92011-01-07 17:50:11 +11002627 if (IS_ERR(sock_mnt)) {
2628 err = PTR_ERR(sock_mnt);
2629 goto out_mount;
2630 }
Andi Kleen77d76ea2005-12-22 12:43:42 -08002631
2632 /* The real protocol initialization is performed in later initcalls.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 */
2634
2635#ifdef CONFIG_NETFILTER
2636 netfilter_init();
2637#endif
David S. Millercbeb3212005-12-22 12:58:55 -08002638
Richard Cochranc1f19b52010-07-17 08:49:36 +00002639#ifdef CONFIG_NETWORK_PHY_TIMESTAMPING
2640 skb_timestamping_init();
2641#endif
2642
Nick Pigginb3e19d92011-01-07 17:50:11 +11002643out:
2644 return err;
2645
2646out_mount:
2647 unregister_filesystem(&sock_fs_type);
2648out_fs:
2649 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650}
2651
Andi Kleen77d76ea2005-12-22 12:43:42 -08002652core_initcall(sock_init); /* early initcall */
2653
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654#ifdef CONFIG_PROC_FS
2655void socket_seq_show(struct seq_file *seq)
2656{
2657 int cpu;
2658 int counter = 0;
2659
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -07002660 for_each_possible_cpu(cpu)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002661 counter += per_cpu(sockets_in_use, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662
2663 /* It can be negative, by the way. 8) */
2664 if (counter < 0)
2665 counter = 0;
2666
2667 seq_printf(seq, "sockets: used %d\n", counter);
2668}
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002669#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670
Shaun Pereira89bbfc92006-03-21 23:58:08 -08002671#ifdef CONFIG_COMPAT
Arnd Bergmann6b960182009-11-06 23:10:54 -08002672static int do_siocgstamp(struct net *net, struct socket *sock,
H. Peter Anvin644595f2012-02-19 17:51:59 -08002673 unsigned int cmd, void __user *up)
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002674{
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002675 mm_segment_t old_fs = get_fs();
2676 struct timeval ktv;
2677 int err;
2678
2679 set_fs(KERNEL_DS);
Arnd Bergmann6b960182009-11-06 23:10:54 -08002680 err = sock_do_ioctl(net, sock, cmd, (unsigned long)&ktv);
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002681 set_fs(old_fs);
H. Peter Anvin644595f2012-02-19 17:51:59 -08002682 if (!err)
Mikulas Patockaed6fe9d2012-09-01 12:34:07 -04002683 err = compat_put_timeval(&ktv, up);
H. Peter Anvin644595f2012-02-19 17:51:59 -08002684
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002685 return err;
2686}
2687
Arnd Bergmann6b960182009-11-06 23:10:54 -08002688static int do_siocgstampns(struct net *net, struct socket *sock,
H. Peter Anvin644595f2012-02-19 17:51:59 -08002689 unsigned int cmd, void __user *up)
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002690{
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002691 mm_segment_t old_fs = get_fs();
2692 struct timespec kts;
2693 int err;
2694
2695 set_fs(KERNEL_DS);
Arnd Bergmann6b960182009-11-06 23:10:54 -08002696 err = sock_do_ioctl(net, sock, cmd, (unsigned long)&kts);
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002697 set_fs(old_fs);
H. Peter Anvin644595f2012-02-19 17:51:59 -08002698 if (!err)
Mikulas Patockaed6fe9d2012-09-01 12:34:07 -04002699 err = compat_put_timespec(&kts, up);
H. Peter Anvin644595f2012-02-19 17:51:59 -08002700
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002701 return err;
2702}
2703
Arnd Bergmann6b960182009-11-06 23:10:54 -08002704static int dev_ifname32(struct net *net, struct compat_ifreq __user *uifr32)
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002705{
2706 struct ifreq __user *uifr;
2707 int err;
2708
2709 uifr = compat_alloc_user_space(sizeof(struct ifreq));
Arnd Bergmann6b960182009-11-06 23:10:54 -08002710 if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002711 return -EFAULT;
2712
Arnd Bergmann6b960182009-11-06 23:10:54 -08002713 err = dev_ioctl(net, SIOCGIFNAME, uifr);
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002714 if (err)
2715 return err;
2716
Arnd Bergmann6b960182009-11-06 23:10:54 -08002717 if (copy_in_user(uifr32, uifr, sizeof(struct compat_ifreq)))
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002718 return -EFAULT;
2719
2720 return 0;
2721}
2722
Arnd Bergmann6b960182009-11-06 23:10:54 -08002723static int dev_ifconf(struct net *net, struct compat_ifconf __user *uifc32)
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002724{
Arnd Bergmann6b960182009-11-06 23:10:54 -08002725 struct compat_ifconf ifc32;
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002726 struct ifconf ifc;
2727 struct ifconf __user *uifc;
Arnd Bergmann6b960182009-11-06 23:10:54 -08002728 struct compat_ifreq __user *ifr32;
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002729 struct ifreq __user *ifr;
2730 unsigned int i, j;
2731 int err;
2732
Arnd Bergmann6b960182009-11-06 23:10:54 -08002733 if (copy_from_user(&ifc32, uifc32, sizeof(struct compat_ifconf)))
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002734 return -EFAULT;
2735
Mathias Krause43da5f22012-08-15 11:31:57 +00002736 memset(&ifc, 0, sizeof(ifc));
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002737 if (ifc32.ifcbuf == 0) {
2738 ifc32.ifc_len = 0;
2739 ifc.ifc_len = 0;
2740 ifc.ifc_req = NULL;
2741 uifc = compat_alloc_user_space(sizeof(struct ifconf));
2742 } else {
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002743 size_t len = ((ifc32.ifc_len / sizeof(struct compat_ifreq)) + 1) *
2744 sizeof(struct ifreq);
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002745 uifc = compat_alloc_user_space(sizeof(struct ifconf) + len);
2746 ifc.ifc_len = len;
2747 ifr = ifc.ifc_req = (void __user *)(uifc + 1);
2748 ifr32 = compat_ptr(ifc32.ifcbuf);
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002749 for (i = 0; i < ifc32.ifc_len; i += sizeof(struct compat_ifreq)) {
Arnd Bergmann6b960182009-11-06 23:10:54 -08002750 if (copy_in_user(ifr, ifr32, sizeof(struct compat_ifreq)))
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002751 return -EFAULT;
2752 ifr++;
2753 ifr32++;
2754 }
2755 }
2756 if (copy_to_user(uifc, &ifc, sizeof(struct ifconf)))
2757 return -EFAULT;
2758
Arnd Bergmann6b960182009-11-06 23:10:54 -08002759 err = dev_ioctl(net, SIOCGIFCONF, uifc);
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002760 if (err)
2761 return err;
2762
2763 if (copy_from_user(&ifc, uifc, sizeof(struct ifconf)))
2764 return -EFAULT;
2765
2766 ifr = ifc.ifc_req;
2767 ifr32 = compat_ptr(ifc32.ifcbuf);
2768 for (i = 0, j = 0;
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002769 i + sizeof(struct compat_ifreq) <= ifc32.ifc_len && j < ifc.ifc_len;
2770 i += sizeof(struct compat_ifreq), j += sizeof(struct ifreq)) {
2771 if (copy_in_user(ifr32, ifr, sizeof(struct compat_ifreq)))
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002772 return -EFAULT;
2773 ifr32++;
2774 ifr++;
2775 }
2776
2777 if (ifc32.ifcbuf == 0) {
2778 /* Translate from 64-bit structure multiple to
2779 * a 32-bit one.
2780 */
2781 i = ifc.ifc_len;
Arnd Bergmann6b960182009-11-06 23:10:54 -08002782 i = ((i / sizeof(struct ifreq)) * sizeof(struct compat_ifreq));
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002783 ifc32.ifc_len = i;
2784 } else {
2785 ifc32.ifc_len = i;
2786 }
Arnd Bergmann6b960182009-11-06 23:10:54 -08002787 if (copy_to_user(uifc32, &ifc32, sizeof(struct compat_ifconf)))
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002788 return -EFAULT;
2789
2790 return 0;
2791}
2792
Arnd Bergmann6b960182009-11-06 23:10:54 -08002793static int ethtool_ioctl(struct net *net, struct compat_ifreq __user *ifr32)
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002794{
Ben Hutchings3a7da392011-03-17 07:34:32 +00002795 struct compat_ethtool_rxnfc __user *compat_rxnfc;
2796 bool convert_in = false, convert_out = false;
2797 size_t buf_size = ALIGN(sizeof(struct ifreq), 8);
2798 struct ethtool_rxnfc __user *rxnfc;
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002799 struct ifreq __user *ifr;
Ben Hutchings3a7da392011-03-17 07:34:32 +00002800 u32 rule_cnt = 0, actual_rule_cnt;
2801 u32 ethcmd;
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002802 u32 data;
Ben Hutchings3a7da392011-03-17 07:34:32 +00002803 int ret;
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002804
2805 if (get_user(data, &ifr32->ifr_ifru.ifru_data))
2806 return -EFAULT;
2807
Ben Hutchings3a7da392011-03-17 07:34:32 +00002808 compat_rxnfc = compat_ptr(data);
2809
2810 if (get_user(ethcmd, &compat_rxnfc->cmd))
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002811 return -EFAULT;
2812
Ben Hutchings3a7da392011-03-17 07:34:32 +00002813 /* Most ethtool structures are defined without padding.
2814 * Unfortunately struct ethtool_rxnfc is an exception.
2815 */
2816 switch (ethcmd) {
2817 default:
2818 break;
2819 case ETHTOOL_GRXCLSRLALL:
2820 /* Buffer size is variable */
2821 if (get_user(rule_cnt, &compat_rxnfc->rule_cnt))
2822 return -EFAULT;
2823 if (rule_cnt > KMALLOC_MAX_SIZE / sizeof(u32))
2824 return -ENOMEM;
2825 buf_size += rule_cnt * sizeof(u32);
2826 /* fall through */
2827 case ETHTOOL_GRXRINGS:
2828 case ETHTOOL_GRXCLSRLCNT:
2829 case ETHTOOL_GRXCLSRULE:
Ben Hutchings55664f32012-01-03 12:04:51 +00002830 case ETHTOOL_SRXCLSRLINS:
Ben Hutchings3a7da392011-03-17 07:34:32 +00002831 convert_out = true;
2832 /* fall through */
2833 case ETHTOOL_SRXCLSRLDEL:
Ben Hutchings3a7da392011-03-17 07:34:32 +00002834 buf_size += sizeof(struct ethtool_rxnfc);
2835 convert_in = true;
2836 break;
2837 }
2838
2839 ifr = compat_alloc_user_space(buf_size);
Stephen Hemminger954b1242013-02-11 06:22:28 +00002840 rxnfc = (void __user *)ifr + ALIGN(sizeof(struct ifreq), 8);
Ben Hutchings3a7da392011-03-17 07:34:32 +00002841
2842 if (copy_in_user(&ifr->ifr_name, &ifr32->ifr_name, IFNAMSIZ))
2843 return -EFAULT;
2844
2845 if (put_user(convert_in ? rxnfc : compat_ptr(data),
2846 &ifr->ifr_ifru.ifru_data))
2847 return -EFAULT;
2848
2849 if (convert_in) {
Alexander Duyck127fe532011-04-08 18:01:59 +00002850 /* We expect there to be holes between fs.m_ext and
Ben Hutchings3a7da392011-03-17 07:34:32 +00002851 * fs.ring_cookie and at the end of fs, but nowhere else.
2852 */
Alexander Duyck127fe532011-04-08 18:01:59 +00002853 BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc, fs.m_ext) +
2854 sizeof(compat_rxnfc->fs.m_ext) !=
2855 offsetof(struct ethtool_rxnfc, fs.m_ext) +
2856 sizeof(rxnfc->fs.m_ext));
Ben Hutchings3a7da392011-03-17 07:34:32 +00002857 BUILD_BUG_ON(
2858 offsetof(struct compat_ethtool_rxnfc, fs.location) -
2859 offsetof(struct compat_ethtool_rxnfc, fs.ring_cookie) !=
2860 offsetof(struct ethtool_rxnfc, fs.location) -
2861 offsetof(struct ethtool_rxnfc, fs.ring_cookie));
2862
2863 if (copy_in_user(rxnfc, compat_rxnfc,
Stephen Hemminger954b1242013-02-11 06:22:28 +00002864 (void __user *)(&rxnfc->fs.m_ext + 1) -
2865 (void __user *)rxnfc) ||
Ben Hutchings3a7da392011-03-17 07:34:32 +00002866 copy_in_user(&rxnfc->fs.ring_cookie,
2867 &compat_rxnfc->fs.ring_cookie,
Stephen Hemminger954b1242013-02-11 06:22:28 +00002868 (void __user *)(&rxnfc->fs.location + 1) -
2869 (void __user *)&rxnfc->fs.ring_cookie) ||
Ben Hutchings3a7da392011-03-17 07:34:32 +00002870 copy_in_user(&rxnfc->rule_cnt, &compat_rxnfc->rule_cnt,
2871 sizeof(rxnfc->rule_cnt)))
2872 return -EFAULT;
2873 }
2874
2875 ret = dev_ioctl(net, SIOCETHTOOL, ifr);
2876 if (ret)
2877 return ret;
2878
2879 if (convert_out) {
2880 if (copy_in_user(compat_rxnfc, rxnfc,
Stephen Hemminger954b1242013-02-11 06:22:28 +00002881 (const void __user *)(&rxnfc->fs.m_ext + 1) -
2882 (const void __user *)rxnfc) ||
Ben Hutchings3a7da392011-03-17 07:34:32 +00002883 copy_in_user(&compat_rxnfc->fs.ring_cookie,
2884 &rxnfc->fs.ring_cookie,
Stephen Hemminger954b1242013-02-11 06:22:28 +00002885 (const void __user *)(&rxnfc->fs.location + 1) -
2886 (const void __user *)&rxnfc->fs.ring_cookie) ||
Ben Hutchings3a7da392011-03-17 07:34:32 +00002887 copy_in_user(&compat_rxnfc->rule_cnt, &rxnfc->rule_cnt,
2888 sizeof(rxnfc->rule_cnt)))
2889 return -EFAULT;
2890
2891 if (ethcmd == ETHTOOL_GRXCLSRLALL) {
2892 /* As an optimisation, we only copy the actual
2893 * number of rules that the underlying
2894 * function returned. Since Mallory might
2895 * change the rule count in user memory, we
2896 * check that it is less than the rule count
2897 * originally given (as the user buffer size),
2898 * which has been range-checked.
2899 */
2900 if (get_user(actual_rule_cnt, &rxnfc->rule_cnt))
2901 return -EFAULT;
2902 if (actual_rule_cnt < rule_cnt)
2903 rule_cnt = actual_rule_cnt;
2904 if (copy_in_user(&compat_rxnfc->rule_locs[0],
2905 &rxnfc->rule_locs[0],
2906 rule_cnt * sizeof(u32)))
2907 return -EFAULT;
2908 }
2909 }
2910
2911 return 0;
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002912}
2913
Arnd Bergmann7a50a242009-11-08 20:57:03 -08002914static int compat_siocwandev(struct net *net, struct compat_ifreq __user *uifr32)
2915{
2916 void __user *uptr;
2917 compat_uptr_t uptr32;
2918 struct ifreq __user *uifr;
2919
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002920 uifr = compat_alloc_user_space(sizeof(*uifr));
Arnd Bergmann7a50a242009-11-08 20:57:03 -08002921 if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
2922 return -EFAULT;
2923
2924 if (get_user(uptr32, &uifr32->ifr_settings.ifs_ifsu))
2925 return -EFAULT;
2926
2927 uptr = compat_ptr(uptr32);
2928
2929 if (put_user(uptr, &uifr->ifr_settings.ifs_ifsu.raw_hdlc))
2930 return -EFAULT;
2931
2932 return dev_ioctl(net, SIOCWANDEV, uifr);
2933}
2934
Arnd Bergmann6b960182009-11-06 23:10:54 -08002935static int bond_ioctl(struct net *net, unsigned int cmd,
2936 struct compat_ifreq __user *ifr32)
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002937{
2938 struct ifreq kifr;
2939 struct ifreq __user *uifr;
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002940 mm_segment_t old_fs;
2941 int err;
2942 u32 data;
2943 void __user *datap;
2944
2945 switch (cmd) {
2946 case SIOCBONDENSLAVE:
2947 case SIOCBONDRELEASE:
2948 case SIOCBONDSETHWADDR:
2949 case SIOCBONDCHANGEACTIVE:
Arnd Bergmann6b960182009-11-06 23:10:54 -08002950 if (copy_from_user(&kifr, ifr32, sizeof(struct compat_ifreq)))
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002951 return -EFAULT;
2952
2953 old_fs = get_fs();
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002954 set_fs(KERNEL_DS);
stephen hemmingerc3f52ae2011-02-23 09:06:48 +00002955 err = dev_ioctl(net, cmd,
2956 (struct ifreq __user __force *) &kifr);
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002957 set_fs(old_fs);
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002958
2959 return err;
2960 case SIOCBONDSLAVEINFOQUERY:
2961 case SIOCBONDINFOQUERY:
2962 uifr = compat_alloc_user_space(sizeof(*uifr));
2963 if (copy_in_user(&uifr->ifr_name, &ifr32->ifr_name, IFNAMSIZ))
2964 return -EFAULT;
2965
2966 if (get_user(data, &ifr32->ifr_ifru.ifru_data))
2967 return -EFAULT;
2968
2969 datap = compat_ptr(data);
2970 if (put_user(datap, &uifr->ifr_ifru.ifru_data))
2971 return -EFAULT;
2972
Arnd Bergmann6b960182009-11-06 23:10:54 -08002973 return dev_ioctl(net, cmd, uifr);
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002974 default:
Linus Torvalds07d106d2012-01-05 15:40:12 -08002975 return -ENOIOCTLCMD;
Joe Perchesccbd6a52010-05-14 10:58:26 +00002976 }
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002977}
2978
Arnd Bergmann6b960182009-11-06 23:10:54 -08002979static int siocdevprivate_ioctl(struct net *net, unsigned int cmd,
2980 struct compat_ifreq __user *u_ifreq32)
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002981{
2982 struct ifreq __user *u_ifreq64;
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002983 char tmp_buf[IFNAMSIZ];
2984 void __user *data64;
2985 u32 data32;
2986
2987 if (copy_from_user(&tmp_buf[0], &(u_ifreq32->ifr_ifrn.ifrn_name[0]),
2988 IFNAMSIZ))
2989 return -EFAULT;
2990 if (__get_user(data32, &u_ifreq32->ifr_ifru.ifru_data))
2991 return -EFAULT;
2992 data64 = compat_ptr(data32);
2993
2994 u_ifreq64 = compat_alloc_user_space(sizeof(*u_ifreq64));
2995
2996 /* Don't check these user accesses, just let that get trapped
2997 * in the ioctl handler instead.
2998 */
2999 if (copy_to_user(&u_ifreq64->ifr_ifrn.ifrn_name[0], &tmp_buf[0],
3000 IFNAMSIZ))
3001 return -EFAULT;
3002 if (__put_user(data64, &u_ifreq64->ifr_ifru.ifru_data))
3003 return -EFAULT;
3004
Arnd Bergmann6b960182009-11-06 23:10:54 -08003005 return dev_ioctl(net, cmd, u_ifreq64);
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003006}
3007
Arnd Bergmann6b960182009-11-06 23:10:54 -08003008static int dev_ifsioc(struct net *net, struct socket *sock,
3009 unsigned int cmd, struct compat_ifreq __user *uifr32)
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003010{
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003011 struct ifreq __user *uifr;
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003012 int err;
3013
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003014 uifr = compat_alloc_user_space(sizeof(*uifr));
3015 if (copy_in_user(uifr, uifr32, sizeof(*uifr32)))
3016 return -EFAULT;
3017
3018 err = sock_do_ioctl(net, sock, cmd, (unsigned long)uifr);
3019
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003020 if (!err) {
3021 switch (cmd) {
3022 case SIOCGIFFLAGS:
3023 case SIOCGIFMETRIC:
3024 case SIOCGIFMTU:
3025 case SIOCGIFMEM:
3026 case SIOCGIFHWADDR:
3027 case SIOCGIFINDEX:
3028 case SIOCGIFADDR:
3029 case SIOCGIFBRDADDR:
3030 case SIOCGIFDSTADDR:
3031 case SIOCGIFNETMASK:
Arnd Bergmannfab25322009-11-08 20:56:21 -08003032 case SIOCGIFPFLAGS:
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003033 case SIOCGIFTXQLEN:
Arnd Bergmannfab25322009-11-08 20:56:21 -08003034 case SIOCGMIIPHY:
3035 case SIOCGMIIREG:
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003036 if (copy_in_user(uifr32, uifr, sizeof(*uifr32)))
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003037 err = -EFAULT;
3038 break;
3039 }
3040 }
3041 return err;
3042}
3043
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003044static int compat_sioc_ifmap(struct net *net, unsigned int cmd,
3045 struct compat_ifreq __user *uifr32)
3046{
3047 struct ifreq ifr;
3048 struct compat_ifmap __user *uifmap32;
3049 mm_segment_t old_fs;
3050 int err;
3051
3052 uifmap32 = &uifr32->ifr_ifru.ifru_map;
3053 err = copy_from_user(&ifr, uifr32, sizeof(ifr.ifr_name));
3054 err |= __get_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
3055 err |= __get_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
3056 err |= __get_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
3057 err |= __get_user(ifr.ifr_map.irq, &uifmap32->irq);
3058 err |= __get_user(ifr.ifr_map.dma, &uifmap32->dma);
3059 err |= __get_user(ifr.ifr_map.port, &uifmap32->port);
3060 if (err)
3061 return -EFAULT;
3062
3063 old_fs = get_fs();
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003064 set_fs(KERNEL_DS);
stephen hemmingerc3f52ae2011-02-23 09:06:48 +00003065 err = dev_ioctl(net, cmd, (void __user __force *)&ifr);
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003066 set_fs(old_fs);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003067
3068 if (cmd == SIOCGIFMAP && !err) {
3069 err = copy_to_user(uifr32, &ifr, sizeof(ifr.ifr_name));
3070 err |= __put_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
3071 err |= __put_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
3072 err |= __put_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
3073 err |= __put_user(ifr.ifr_map.irq, &uifmap32->irq);
3074 err |= __put_user(ifr.ifr_map.dma, &uifmap32->dma);
3075 err |= __put_user(ifr.ifr_map.port, &uifmap32->port);
3076 if (err)
3077 err = -EFAULT;
3078 }
3079 return err;
3080}
3081
3082static int compat_siocshwtstamp(struct net *net, struct compat_ifreq __user *uifr32)
3083{
3084 void __user *uptr;
3085 compat_uptr_t uptr32;
3086 struct ifreq __user *uifr;
3087
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003088 uifr = compat_alloc_user_space(sizeof(*uifr));
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003089 if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
3090 return -EFAULT;
3091
3092 if (get_user(uptr32, &uifr32->ifr_data))
3093 return -EFAULT;
3094
3095 uptr = compat_ptr(uptr32);
3096
3097 if (put_user(uptr, &uifr->ifr_data))
3098 return -EFAULT;
3099
3100 return dev_ioctl(net, SIOCSHWTSTAMP, uifr);
3101}
3102
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003103struct rtentry32 {
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003104 u32 rt_pad1;
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003105 struct sockaddr rt_dst; /* target address */
3106 struct sockaddr rt_gateway; /* gateway addr (RTF_GATEWAY) */
3107 struct sockaddr rt_genmask; /* target network mask (IP) */
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003108 unsigned short rt_flags;
3109 short rt_pad2;
3110 u32 rt_pad3;
3111 unsigned char rt_tos;
3112 unsigned char rt_class;
3113 short rt_pad4;
3114 short rt_metric; /* +1 for binary compatibility! */
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003115 /* char * */ u32 rt_dev; /* forcing the device at add */
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003116 u32 rt_mtu; /* per route MTU/Window */
3117 u32 rt_window; /* Window clamping */
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003118 unsigned short rt_irtt; /* Initial RTT */
3119};
3120
3121struct in6_rtmsg32 {
3122 struct in6_addr rtmsg_dst;
3123 struct in6_addr rtmsg_src;
3124 struct in6_addr rtmsg_gateway;
3125 u32 rtmsg_type;
3126 u16 rtmsg_dst_len;
3127 u16 rtmsg_src_len;
3128 u32 rtmsg_metric;
3129 u32 rtmsg_info;
3130 u32 rtmsg_flags;
3131 s32 rtmsg_ifindex;
3132};
3133
Arnd Bergmann6b960182009-11-06 23:10:54 -08003134static int routing_ioctl(struct net *net, struct socket *sock,
3135 unsigned int cmd, void __user *argp)
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003136{
3137 int ret;
3138 void *r = NULL;
3139 struct in6_rtmsg r6;
3140 struct rtentry r4;
3141 char devname[16];
3142 u32 rtdev;
3143 mm_segment_t old_fs = get_fs();
3144
Arnd Bergmann6b960182009-11-06 23:10:54 -08003145 if (sock && sock->sk && sock->sk->sk_family == AF_INET6) { /* ipv6 */
3146 struct in6_rtmsg32 __user *ur6 = argp;
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003147 ret = copy_from_user(&r6.rtmsg_dst, &(ur6->rtmsg_dst),
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003148 3 * sizeof(struct in6_addr));
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003149 ret |= __get_user(r6.rtmsg_type, &(ur6->rtmsg_type));
3150 ret |= __get_user(r6.rtmsg_dst_len, &(ur6->rtmsg_dst_len));
3151 ret |= __get_user(r6.rtmsg_src_len, &(ur6->rtmsg_src_len));
3152 ret |= __get_user(r6.rtmsg_metric, &(ur6->rtmsg_metric));
3153 ret |= __get_user(r6.rtmsg_info, &(ur6->rtmsg_info));
3154 ret |= __get_user(r6.rtmsg_flags, &(ur6->rtmsg_flags));
3155 ret |= __get_user(r6.rtmsg_ifindex, &(ur6->rtmsg_ifindex));
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003156
3157 r = (void *) &r6;
3158 } else { /* ipv4 */
Arnd Bergmann6b960182009-11-06 23:10:54 -08003159 struct rtentry32 __user *ur4 = argp;
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003160 ret = copy_from_user(&r4.rt_dst, &(ur4->rt_dst),
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003161 3 * sizeof(struct sockaddr));
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003162 ret |= __get_user(r4.rt_flags, &(ur4->rt_flags));
3163 ret |= __get_user(r4.rt_metric, &(ur4->rt_metric));
3164 ret |= __get_user(r4.rt_mtu, &(ur4->rt_mtu));
3165 ret |= __get_user(r4.rt_window, &(ur4->rt_window));
3166 ret |= __get_user(r4.rt_irtt, &(ur4->rt_irtt));
3167 ret |= __get_user(rtdev, &(ur4->rt_dev));
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003168 if (rtdev) {
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003169 ret |= copy_from_user(devname, compat_ptr(rtdev), 15);
stephen hemmingerc3f52ae2011-02-23 09:06:48 +00003170 r4.rt_dev = (char __user __force *)devname;
3171 devname[15] = 0;
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003172 } else
3173 r4.rt_dev = NULL;
3174
3175 r = (void *) &r4;
3176 }
3177
3178 if (ret) {
3179 ret = -EFAULT;
3180 goto out;
3181 }
3182
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003183 set_fs(KERNEL_DS);
Arnd Bergmann6b960182009-11-06 23:10:54 -08003184 ret = sock_do_ioctl(net, sock, cmd, (unsigned long) r);
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003185 set_fs(old_fs);
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003186
3187out:
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003188 return ret;
3189}
3190
3191/* Since old style bridge ioctl's endup using SIOCDEVPRIVATE
3192 * for some operations; this forces use of the newer bridge-utils that
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003193 * use compatible ioctls
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003194 */
Arnd Bergmann6b960182009-11-06 23:10:54 -08003195static int old_bridge_ioctl(compat_ulong_t __user *argp)
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003196{
Arnd Bergmann6b960182009-11-06 23:10:54 -08003197 compat_ulong_t tmp;
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003198
Arnd Bergmann6b960182009-11-06 23:10:54 -08003199 if (get_user(tmp, argp))
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003200 return -EFAULT;
3201 if (tmp == BRCTL_GET_VERSION)
3202 return BRCTL_VERSION + 1;
3203 return -EINVAL;
3204}
3205
Arnd Bergmann6b960182009-11-06 23:10:54 -08003206static int compat_sock_ioctl_trans(struct file *file, struct socket *sock,
3207 unsigned int cmd, unsigned long arg)
3208{
3209 void __user *argp = compat_ptr(arg);
3210 struct sock *sk = sock->sk;
3211 struct net *net = sock_net(sk);
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003212
Arnd Bergmann6b960182009-11-06 23:10:54 -08003213 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15))
3214 return siocdevprivate_ioctl(net, cmd, argp);
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003215
Arnd Bergmann6b960182009-11-06 23:10:54 -08003216 switch (cmd) {
3217 case SIOCSIFBR:
3218 case SIOCGIFBR:
3219 return old_bridge_ioctl(argp);
3220 case SIOCGIFNAME:
3221 return dev_ifname32(net, argp);
3222 case SIOCGIFCONF:
3223 return dev_ifconf(net, argp);
3224 case SIOCETHTOOL:
3225 return ethtool_ioctl(net, argp);
Arnd Bergmann7a50a242009-11-08 20:57:03 -08003226 case SIOCWANDEV:
3227 return compat_siocwandev(net, argp);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003228 case SIOCGIFMAP:
3229 case SIOCSIFMAP:
3230 return compat_sioc_ifmap(net, cmd, argp);
Arnd Bergmann6b960182009-11-06 23:10:54 -08003231 case SIOCBONDENSLAVE:
3232 case SIOCBONDRELEASE:
3233 case SIOCBONDSETHWADDR:
3234 case SIOCBONDSLAVEINFOQUERY:
3235 case SIOCBONDINFOQUERY:
3236 case SIOCBONDCHANGEACTIVE:
3237 return bond_ioctl(net, cmd, argp);
3238 case SIOCADDRT:
3239 case SIOCDELRT:
3240 return routing_ioctl(net, sock, cmd, argp);
3241 case SIOCGSTAMP:
3242 return do_siocgstamp(net, sock, cmd, argp);
3243 case SIOCGSTAMPNS:
3244 return do_siocgstampns(net, sock, cmd, argp);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003245 case SIOCSHWTSTAMP:
3246 return compat_siocshwtstamp(net, argp);
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003247
Arnd Bergmann6b960182009-11-06 23:10:54 -08003248 case FIOSETOWN:
3249 case SIOCSPGRP:
3250 case FIOGETOWN:
3251 case SIOCGPGRP:
3252 case SIOCBRADDBR:
3253 case SIOCBRDELBR:
3254 case SIOCGIFVLAN:
3255 case SIOCSIFVLAN:
3256 case SIOCADDDLCI:
3257 case SIOCDELDLCI:
3258 return sock_ioctl(file, cmd, arg);
3259
3260 case SIOCGIFFLAGS:
3261 case SIOCSIFFLAGS:
3262 case SIOCGIFMETRIC:
3263 case SIOCSIFMETRIC:
3264 case SIOCGIFMTU:
3265 case SIOCSIFMTU:
3266 case SIOCGIFMEM:
3267 case SIOCSIFMEM:
3268 case SIOCGIFHWADDR:
3269 case SIOCSIFHWADDR:
3270 case SIOCADDMULTI:
3271 case SIOCDELMULTI:
3272 case SIOCGIFINDEX:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003273 case SIOCGIFADDR:
3274 case SIOCSIFADDR:
3275 case SIOCSIFHWBROADCAST:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003276 case SIOCDIFADDR:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003277 case SIOCGIFBRDADDR:
3278 case SIOCSIFBRDADDR:
3279 case SIOCGIFDSTADDR:
3280 case SIOCSIFDSTADDR:
3281 case SIOCGIFNETMASK:
3282 case SIOCSIFNETMASK:
3283 case SIOCSIFPFLAGS:
3284 case SIOCGIFPFLAGS:
3285 case SIOCGIFTXQLEN:
3286 case SIOCSIFTXQLEN:
3287 case SIOCBRADDIF:
3288 case SIOCBRDELIF:
Arnd Bergmann9177efd2009-11-06 08:09:09 +00003289 case SIOCSIFNAME:
3290 case SIOCGMIIPHY:
3291 case SIOCGMIIREG:
3292 case SIOCSMIIREG:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003293 return dev_ifsioc(net, sock, cmd, argp);
Arnd Bergmann9177efd2009-11-06 08:09:09 +00003294
Arnd Bergmann6b960182009-11-06 23:10:54 -08003295 case SIOCSARP:
3296 case SIOCGARP:
3297 case SIOCDARP:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003298 case SIOCATMARK:
Arnd Bergmann9177efd2009-11-06 08:09:09 +00003299 return sock_do_ioctl(net, sock, cmd, arg);
3300 }
3301
Arnd Bergmann6b960182009-11-06 23:10:54 -08003302 return -ENOIOCTLCMD;
3303}
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003304
Eric Dumazet95c96172012-04-15 05:58:06 +00003305static long compat_sock_ioctl(struct file *file, unsigned int cmd,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07003306 unsigned long arg)
Shaun Pereira89bbfc92006-03-21 23:58:08 -08003307{
3308 struct socket *sock = file->private_data;
3309 int ret = -ENOIOCTLCMD;
David S. Miller87de87d2008-06-03 09:14:03 -07003310 struct sock *sk;
3311 struct net *net;
3312
3313 sk = sock->sk;
3314 net = sock_net(sk);
Shaun Pereira89bbfc92006-03-21 23:58:08 -08003315
3316 if (sock->ops->compat_ioctl)
3317 ret = sock->ops->compat_ioctl(sock, cmd, arg);
3318
David S. Miller87de87d2008-06-03 09:14:03 -07003319 if (ret == -ENOIOCTLCMD &&
3320 (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST))
3321 ret = compat_wext_handle_ioctl(net, cmd, arg);
3322
Arnd Bergmann6b960182009-11-06 23:10:54 -08003323 if (ret == -ENOIOCTLCMD)
3324 ret = compat_sock_ioctl_trans(file, sock, cmd, arg);
3325
Shaun Pereira89bbfc92006-03-21 23:58:08 -08003326 return ret;
3327}
3328#endif
3329
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003330int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen)
3331{
3332 return sock->ops->bind(sock, addr, addrlen);
3333}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003334EXPORT_SYMBOL(kernel_bind);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003335
3336int kernel_listen(struct socket *sock, int backlog)
3337{
3338 return sock->ops->listen(sock, backlog);
3339}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003340EXPORT_SYMBOL(kernel_listen);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003341
3342int kernel_accept(struct socket *sock, struct socket **newsock, int flags)
3343{
3344 struct sock *sk = sock->sk;
3345 int err;
3346
3347 err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
3348 newsock);
3349 if (err < 0)
3350 goto done;
3351
3352 err = sock->ops->accept(sock, *newsock, flags);
3353 if (err < 0) {
3354 sock_release(*newsock);
Tony Battersbyfa8705b2007-10-10 21:09:04 -07003355 *newsock = NULL;
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003356 goto done;
3357 }
3358
3359 (*newsock)->ops = sock->ops;
Wei Yongjun1b085342008-12-18 19:35:10 -08003360 __module_get((*newsock)->ops->owner);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003361
3362done:
3363 return err;
3364}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003365EXPORT_SYMBOL(kernel_accept);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003366
3367int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
YOSHIFUJI Hideaki4768fbc2007-02-09 23:25:31 +09003368 int flags)
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003369{
3370 return sock->ops->connect(sock, addr, addrlen, flags);
3371}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003372EXPORT_SYMBOL(kernel_connect);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003373
3374int kernel_getsockname(struct socket *sock, struct sockaddr *addr,
3375 int *addrlen)
3376{
3377 return sock->ops->getname(sock, addr, addrlen, 0);
3378}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003379EXPORT_SYMBOL(kernel_getsockname);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003380
3381int kernel_getpeername(struct socket *sock, struct sockaddr *addr,
3382 int *addrlen)
3383{
3384 return sock->ops->getname(sock, addr, addrlen, 1);
3385}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003386EXPORT_SYMBOL(kernel_getpeername);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003387
3388int kernel_getsockopt(struct socket *sock, int level, int optname,
3389 char *optval, int *optlen)
3390{
3391 mm_segment_t oldfs = get_fs();
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003392 char __user *uoptval;
3393 int __user *uoptlen;
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003394 int err;
3395
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003396 uoptval = (char __user __force *) optval;
3397 uoptlen = (int __user __force *) optlen;
3398
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003399 set_fs(KERNEL_DS);
3400 if (level == SOL_SOCKET)
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003401 err = sock_getsockopt(sock, level, optname, uoptval, uoptlen);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003402 else
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003403 err = sock->ops->getsockopt(sock, level, optname, uoptval,
3404 uoptlen);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003405 set_fs(oldfs);
3406 return err;
3407}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003408EXPORT_SYMBOL(kernel_getsockopt);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003409
3410int kernel_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07003411 char *optval, unsigned int optlen)
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003412{
3413 mm_segment_t oldfs = get_fs();
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003414 char __user *uoptval;
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003415 int err;
3416
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003417 uoptval = (char __user __force *) optval;
3418
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003419 set_fs(KERNEL_DS);
3420 if (level == SOL_SOCKET)
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003421 err = sock_setsockopt(sock, level, optname, uoptval, optlen);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003422 else
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003423 err = sock->ops->setsockopt(sock, level, optname, uoptval,
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003424 optlen);
3425 set_fs(oldfs);
3426 return err;
3427}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003428EXPORT_SYMBOL(kernel_setsockopt);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003429
3430int kernel_sendpage(struct socket *sock, struct page *page, int offset,
3431 size_t size, int flags)
3432{
3433 if (sock->ops->sendpage)
3434 return sock->ops->sendpage(sock, page, offset, size, flags);
3435
3436 return sock_no_sendpage(sock, page, offset, size, flags);
3437}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003438EXPORT_SYMBOL(kernel_sendpage);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003439
3440int kernel_sock_ioctl(struct socket *sock, int cmd, unsigned long arg)
3441{
3442 mm_segment_t oldfs = get_fs();
3443 int err;
3444
3445 set_fs(KERNEL_DS);
3446 err = sock->ops->ioctl(sock, cmd, arg);
3447 set_fs(oldfs);
3448
3449 return err;
3450}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003451EXPORT_SYMBOL(kernel_sock_ioctl);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003452
Trond Myklebust91cf45f2007-11-12 18:10:39 -08003453int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how)
3454{
3455 return sock->ops->shutdown(sock, how);
3456}
Trond Myklebust91cf45f2007-11-12 18:10:39 -08003457EXPORT_SYMBOL(kernel_sock_shutdown);