blob: 36883fea44f3c0cf2e44f399023f274014546339 [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);
Anatol Pomozov39b65252012-09-12 20:11:55 -0700372 if (unlikely(IS_ERR(file))) {
Al Virocc3808f2009-08-06 09:43:59 +0400373 /* 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);
Anatol Pomozov39b65252012-09-12 20:11:55 -0700376 return file;
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->private_data = sock;
Al Viro28407632012-08-17 23:54:15 -0400382 return file;
David S. Miller39d8c1b2006-03-20 17:13:49 -0800383}
Al Viro56b31d12012-08-18 00:25:51 -0400384EXPORT_SYMBOL(sock_alloc_file);
David S. Miller39d8c1b2006-03-20 17:13:49 -0800385
Al Viro56b31d12012-08-18 00:25:51 -0400386static int sock_map_fd(struct socket *sock, int flags)
David S. Miller39d8c1b2006-03-20 17:13:49 -0800387{
388 struct file *newfile;
Al Viro28407632012-08-17 23:54:15 -0400389 int fd = get_unused_fd_flags(flags);
390 if (unlikely(fd < 0))
391 return fd;
David S. Miller39d8c1b2006-03-20 17:13:49 -0800392
Linus Torvaldsaab174f2012-10-02 20:25:04 -0700393 newfile = sock_alloc_file(sock, flags, NULL);
Al Viro28407632012-08-17 23:54:15 -0400394 if (likely(!IS_ERR(newfile))) {
David S. Miller39d8c1b2006-03-20 17:13:49 -0800395 fd_install(fd, newfile);
Al Viro28407632012-08-17 23:54:15 -0400396 return fd;
397 }
Al Viro7cbe66b2009-08-05 19:59:08 +0400398
Al Viro28407632012-08-17 23:54:15 -0400399 put_unused_fd(fd);
400 return PTR_ERR(newfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401}
402
John Fastabend406a3c62012-07-20 10:39:25 +0000403struct socket *sock_from_file(struct file *file, int *err)
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800404{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800405 if (file->f_op == &socket_file_ops)
406 return file->private_data; /* set in sock_map_fd */
407
Eric Dumazet23bb80d2007-02-08 14:59:57 -0800408 *err = -ENOTSOCK;
409 return NULL;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800410}
John Fastabend406a3c62012-07-20 10:39:25 +0000411EXPORT_SYMBOL(sock_from_file);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413/**
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700414 * sockfd_lookup - Go from a file number to its socket slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 * @fd: file handle
416 * @err: pointer to an error code return
417 *
418 * The file handle passed in is locked and the socket it is bound
419 * too is returned. If an error occurs the err pointer is overwritten
420 * with a negative errno code and NULL is returned. The function checks
421 * for both invalid handles and passing a handle which is not a socket.
422 *
423 * On a success the socket object pointer is returned.
424 */
425
426struct socket *sockfd_lookup(int fd, int *err)
427{
428 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 struct socket *sock;
430
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700431 file = fget(fd);
432 if (!file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 *err = -EBADF;
434 return NULL;
435 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700436
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800437 sock = sock_from_file(file, err);
438 if (!sock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 fput(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 return sock;
441}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700442EXPORT_SYMBOL(sockfd_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800444static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed)
445{
446 struct file *file;
447 struct socket *sock;
448
Hua Zhong36725582006-04-19 15:25:02 -0700449 *err = -EBADF;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800450 file = fget_light(fd, fput_needed);
451 if (file) {
452 sock = sock_from_file(file, err);
453 if (sock)
454 return sock;
455 fput_light(file, *fput_needed);
456 }
457 return NULL;
458}
459
Masatake YAMATO600e1772012-08-29 10:44:29 +0000460#define XATTR_SOCKPROTONAME_SUFFIX "sockprotoname"
461#define XATTR_NAME_SOCKPROTONAME (XATTR_SYSTEM_PREFIX XATTR_SOCKPROTONAME_SUFFIX)
462#define XATTR_NAME_SOCKPROTONAME_LEN (sizeof(XATTR_NAME_SOCKPROTONAME)-1)
463static ssize_t sockfs_getxattr(struct dentry *dentry,
464 const char *name, void *value, size_t size)
465{
466 const char *proto_name;
467 size_t proto_size;
468 int error;
469
470 error = -ENODATA;
471 if (!strncmp(name, XATTR_NAME_SOCKPROTONAME, XATTR_NAME_SOCKPROTONAME_LEN)) {
472 proto_name = dentry->d_name.name;
473 proto_size = strlen(proto_name);
474
475 if (value) {
476 error = -ERANGE;
477 if (proto_size + 1 > size)
478 goto out;
479
480 strncpy(value, proto_name, proto_size + 1);
481 }
482 error = proto_size + 1;
483 }
484
485out:
486 return error;
487}
488
489static ssize_t sockfs_listxattr(struct dentry *dentry, char *buffer,
490 size_t size)
491{
492 ssize_t len;
493 ssize_t used = 0;
494
495 len = security_inode_listsecurity(dentry->d_inode, buffer, size);
496 if (len < 0)
497 return len;
498 used += len;
499 if (buffer) {
500 if (size < used)
501 return -ERANGE;
502 buffer += len;
503 }
504
505 len = (XATTR_NAME_SOCKPROTONAME_LEN + 1);
506 used += len;
507 if (buffer) {
508 if (size < used)
509 return -ERANGE;
510 memcpy(buffer, XATTR_NAME_SOCKPROTONAME, len);
511 buffer += len;
512 }
513
514 return used;
515}
516
517static const struct inode_operations sockfs_inode_ops = {
518 .getxattr = sockfs_getxattr,
519 .listxattr = sockfs_listxattr,
520};
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522/**
523 * sock_alloc - allocate a socket
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700524 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 * Allocate a new inode and socket object. The two are bound together
526 * and initialised. The socket is then returned. If we are out of inodes
527 * NULL is returned.
528 */
529
530static struct socket *sock_alloc(void)
531{
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700532 struct inode *inode;
533 struct socket *sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Eric Dumazeta209dfc2011-07-26 11:36:34 +0200535 inode = new_inode_pseudo(sock_mnt->mnt_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 if (!inode)
537 return NULL;
538
539 sock = SOCKET_I(inode);
540
Eric Dumazet29a020d2009-09-15 02:39:20 -0700541 kmemcheck_annotate_bitfield(sock, type);
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400542 inode->i_ino = get_next_ino();
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700543 inode->i_mode = S_IFSOCK | S_IRWXUGO;
David Howells8192b0c2008-11-14 10:39:10 +1100544 inode->i_uid = current_fsuid();
545 inode->i_gid = current_fsgid();
Masatake YAMATO600e1772012-08-29 10:44:29 +0000546 inode->i_op = &sockfs_inode_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Alex Shi19e8d692012-05-14 14:15:31 -0700548 this_cpu_add(sockets_in_use, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 return sock;
550}
551
552/*
553 * In theory you can't get an open on this inode, but /proc provides
554 * a back door. Remember to keep it shut otherwise you'll let the
555 * creepy crawlies in.
556 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558static int sock_no_open(struct inode *irrelevant, struct file *dontcare)
559{
560 return -ENXIO;
561}
562
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800563const struct file_operations bad_sock_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 .owner = THIS_MODULE,
565 .open = sock_no_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200566 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567};
568
569/**
570 * sock_release - close a socket
571 * @sock: socket to close
572 *
573 * The socket is released from the protocol stack if it has a release
574 * callback, and the inode is then released if the socket is bound to
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700575 * an inode not a file.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578void sock_release(struct socket *sock)
579{
580 if (sock->ops) {
581 struct module *owner = sock->ops->owner;
582
583 sock->ops->release(sock);
584 sock->ops = NULL;
585 module_put(owner);
586 }
587
Eric Dumazeteaefd112011-02-18 03:26:36 +0000588 if (rcu_dereference_protected(sock->wq, 1)->fasync_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 printk(KERN_ERR "sock_release: fasync list not empty!\n");
590
Mikulas Patockab09e7862012-07-19 06:13:36 +0000591 if (test_bit(SOCK_EXTERNALLY_ALLOCATED, &sock->flags))
592 return;
593
Alex Shi19e8d692012-05-14 14:15:31 -0700594 this_cpu_sub(sockets_in_use, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 if (!sock->file) {
596 iput(SOCK_INODE(sock));
597 return;
598 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700599 sock->file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700601EXPORT_SYMBOL(sock_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Daniel Borkmannbf84a012013-04-14 08:08:13 +0000603void sock_tx_timestamp(struct sock *sk, __u8 *tx_flags)
Patrick Ohly20d49472009-02-12 05:03:38 +0000604{
Oliver Hartkopp2244d072010-08-17 08:59:14 +0000605 *tx_flags = 0;
Patrick Ohly20d49472009-02-12 05:03:38 +0000606 if (sock_flag(sk, SOCK_TIMESTAMPING_TX_HARDWARE))
Oliver Hartkopp2244d072010-08-17 08:59:14 +0000607 *tx_flags |= SKBTX_HW_TSTAMP;
Patrick Ohly20d49472009-02-12 05:03:38 +0000608 if (sock_flag(sk, SOCK_TIMESTAMPING_TX_SOFTWARE))
Oliver Hartkopp2244d072010-08-17 08:59:14 +0000609 *tx_flags |= SKBTX_SW_TSTAMP;
Johannes Berg6e3e9392011-11-09 10:15:42 +0100610 if (sock_flag(sk, SOCK_WIFI_STATUS))
611 *tx_flags |= SKBTX_WIFI_STATUS;
Patrick Ohly20d49472009-02-12 05:03:38 +0000612}
613EXPORT_SYMBOL(sock_tx_timestamp);
614
Anton Blanchard228e5482011-05-02 20:21:35 +0000615static inline int __sock_sendmsg_nosec(struct kiocb *iocb, struct socket *sock,
616 struct msghdr *msg, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
618 struct sock_iocb *si = kiocb_to_siocb(iocb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
620 si->sock = sock;
621 si->scm = NULL;
622 si->msg = msg;
623 si->size = size;
624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 return sock->ops->sendmsg(iocb, sock, msg, size);
626}
627
Anton Blanchard228e5482011-05-02 20:21:35 +0000628static inline int __sock_sendmsg(struct kiocb *iocb, struct socket *sock,
629 struct msghdr *msg, size_t size)
630{
631 int err = security_socket_sendmsg(sock, msg, size);
632
633 return err ?: __sock_sendmsg_nosec(iocb, sock, msg, size);
634}
635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636int sock_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
637{
638 struct kiocb iocb;
639 struct sock_iocb siocb;
640 int ret;
641
642 init_sync_kiocb(&iocb, NULL);
643 iocb.private = &siocb;
644 ret = __sock_sendmsg(&iocb, sock, msg, size);
645 if (-EIOCBQUEUED == ret)
646 ret = wait_on_sync_kiocb(&iocb);
647 return ret;
648}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700649EXPORT_SYMBOL(sock_sendmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Eric Dumazet894dc242011-07-26 02:39:41 +0000651static int sock_sendmsg_nosec(struct socket *sock, struct msghdr *msg, size_t size)
Anton Blanchard228e5482011-05-02 20:21:35 +0000652{
653 struct kiocb iocb;
654 struct sock_iocb siocb;
655 int ret;
656
657 init_sync_kiocb(&iocb, NULL);
658 iocb.private = &siocb;
659 ret = __sock_sendmsg_nosec(&iocb, sock, msg, size);
660 if (-EIOCBQUEUED == ret)
661 ret = wait_on_sync_kiocb(&iocb);
662 return ret;
663}
664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
666 struct kvec *vec, size_t num, size_t size)
667{
668 mm_segment_t oldfs = get_fs();
669 int result;
670
671 set_fs(KERNEL_DS);
672 /*
673 * the following is safe, since for compiler definitions of kvec and
674 * iovec are identical, yielding the same in-core layout and alignment
675 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700676 msg->msg_iov = (struct iovec *)vec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 msg->msg_iovlen = num;
678 result = sock_sendmsg(sock, msg, size);
679 set_fs(oldfs);
680 return result;
681}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700682EXPORT_SYMBOL(kernel_sendmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Patrick Ohly20d49472009-02-12 05:03:38 +0000684static int ktime2ts(ktime_t kt, struct timespec *ts)
685{
686 if (kt.tv64) {
687 *ts = ktime_to_timespec(kt);
688 return 1;
689 } else {
690 return 0;
691 }
692}
693
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700694/*
695 * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
696 */
697void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
698 struct sk_buff *skb)
699{
Patrick Ohly20d49472009-02-12 05:03:38 +0000700 int need_software_tstamp = sock_flag(sk, SOCK_RCVTSTAMP);
701 struct timespec ts[3];
702 int empty = 1;
703 struct skb_shared_hwtstamps *shhwtstamps =
704 skb_hwtstamps(skb);
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700705
Patrick Ohly20d49472009-02-12 05:03:38 +0000706 /* Race occurred between timestamp enabling and packet
707 receiving. Fill in the current time for now. */
708 if (need_software_tstamp && skb->tstamp.tv64 == 0)
709 __net_timestamp(skb);
710
711 if (need_software_tstamp) {
712 if (!sock_flag(sk, SOCK_RCVTSTAMPNS)) {
713 struct timeval tv;
714 skb_get_timestamp(skb, &tv);
715 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMP,
716 sizeof(tv), &tv);
717 } else {
Hagen Paul Pfeifer842509b2010-04-06 05:39:52 +0000718 skb_get_timestampns(skb, &ts[0]);
Patrick Ohly20d49472009-02-12 05:03:38 +0000719 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPNS,
Hagen Paul Pfeifer842509b2010-04-06 05:39:52 +0000720 sizeof(ts[0]), &ts[0]);
Patrick Ohly20d49472009-02-12 05:03:38 +0000721 }
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700722 }
Patrick Ohly20d49472009-02-12 05:03:38 +0000723
724
725 memset(ts, 0, sizeof(ts));
726 if (skb->tstamp.tv64 &&
727 sock_flag(sk, SOCK_TIMESTAMPING_SOFTWARE)) {
728 skb_get_timestampns(skb, ts + 0);
729 empty = 0;
730 }
731 if (shhwtstamps) {
732 if (sock_flag(sk, SOCK_TIMESTAMPING_SYS_HARDWARE) &&
733 ktime2ts(shhwtstamps->syststamp, ts + 1))
734 empty = 0;
735 if (sock_flag(sk, SOCK_TIMESTAMPING_RAW_HARDWARE) &&
736 ktime2ts(shhwtstamps->hwtstamp, ts + 2))
737 empty = 0;
738 }
739 if (!empty)
740 put_cmsg(msg, SOL_SOCKET,
741 SCM_TIMESTAMPING, sizeof(ts), &ts);
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700742}
Arnaldo Carvalho de Melo7c81fd82007-03-10 00:39:35 -0300743EXPORT_SYMBOL_GPL(__sock_recv_timestamp);
744
Johannes Berg6e3e9392011-11-09 10:15:42 +0100745void __sock_recv_wifi_status(struct msghdr *msg, struct sock *sk,
746 struct sk_buff *skb)
747{
748 int ack;
749
750 if (!sock_flag(sk, SOCK_WIFI_STATUS))
751 return;
752 if (!skb->wifi_acked_valid)
753 return;
754
755 ack = skb->wifi_acked;
756
757 put_cmsg(msg, SOL_SOCKET, SCM_WIFI_STATUS, sizeof(ack), &ack);
758}
759EXPORT_SYMBOL_GPL(__sock_recv_wifi_status);
760
stephen hemminger11165f12010-10-18 14:27:29 +0000761static inline void sock_recv_drops(struct msghdr *msg, struct sock *sk,
762 struct sk_buff *skb)
Neil Horman3b885782009-10-12 13:26:31 -0700763{
764 if (sock_flag(sk, SOCK_RXQ_OVFL) && skb && skb->dropcount)
765 put_cmsg(msg, SOL_SOCKET, SO_RXQ_OVFL,
766 sizeof(__u32), &skb->dropcount);
767}
768
Eric Dumazet767dd032010-04-28 19:14:43 +0000769void __sock_recv_ts_and_drops(struct msghdr *msg, struct sock *sk,
Neil Horman3b885782009-10-12 13:26:31 -0700770 struct sk_buff *skb)
771{
772 sock_recv_timestamp(msg, sk, skb);
773 sock_recv_drops(msg, sk, skb);
774}
Eric Dumazet767dd032010-04-28 19:14:43 +0000775EXPORT_SYMBOL_GPL(__sock_recv_ts_and_drops);
Neil Horman3b885782009-10-12 13:26:31 -0700776
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -0700777static inline int __sock_recvmsg_nosec(struct kiocb *iocb, struct socket *sock,
778 struct msghdr *msg, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 struct sock_iocb *si = kiocb_to_siocb(iocb);
781
782 si->sock = sock;
783 si->scm = NULL;
784 si->msg = msg;
785 si->size = size;
786 si->flags = flags;
787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 return sock->ops->recvmsg(iocb, sock, msg, size, flags);
789}
790
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -0700791static inline int __sock_recvmsg(struct kiocb *iocb, struct socket *sock,
792 struct msghdr *msg, size_t size, int flags)
793{
794 int err = security_socket_recvmsg(sock, msg, size, flags);
795
796 return err ?: __sock_recvmsg_nosec(iocb, sock, msg, size, flags);
797}
798
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700799int sock_recvmsg(struct socket *sock, struct msghdr *msg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 size_t size, int flags)
801{
802 struct kiocb iocb;
803 struct sock_iocb siocb;
804 int ret;
805
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700806 init_sync_kiocb(&iocb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 iocb.private = &siocb;
808 ret = __sock_recvmsg(&iocb, sock, msg, size, flags);
809 if (-EIOCBQUEUED == ret)
810 ret = wait_on_sync_kiocb(&iocb);
811 return ret;
812}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700813EXPORT_SYMBOL(sock_recvmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -0700815static int sock_recvmsg_nosec(struct socket *sock, struct msghdr *msg,
816 size_t size, int flags)
817{
818 struct kiocb iocb;
819 struct sock_iocb siocb;
820 int ret;
821
822 init_sync_kiocb(&iocb, NULL);
823 iocb.private = &siocb;
824 ret = __sock_recvmsg_nosec(&iocb, sock, msg, size, flags);
825 if (-EIOCBQUEUED == ret)
826 ret = wait_on_sync_kiocb(&iocb);
827 return ret;
828}
829
Martin Lucinac1249c02010-12-10 00:04:05 +0000830/**
831 * kernel_recvmsg - Receive a message from a socket (kernel space)
832 * @sock: The socket to receive the message from
833 * @msg: Received message
834 * @vec: Input s/g array for message data
835 * @num: Size of input s/g array
836 * @size: Number of bytes to read
837 * @flags: Message flags (MSG_DONTWAIT, etc...)
838 *
839 * On return the msg structure contains the scatter/gather array passed in the
840 * vec argument. The array is modified so that it consists of the unfilled
841 * portion of the original array.
842 *
843 * The returned value is the total number of bytes received, or an error.
844 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700845int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
846 struct kvec *vec, size_t num, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
848 mm_segment_t oldfs = get_fs();
849 int result;
850
851 set_fs(KERNEL_DS);
852 /*
853 * the following is safe, since for compiler definitions of kvec and
854 * iovec are identical, yielding the same in-core layout and alignment
855 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700856 msg->msg_iov = (struct iovec *)vec, msg->msg_iovlen = num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 result = sock_recvmsg(sock, msg, size, flags);
858 set_fs(oldfs);
859 return result;
860}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700861EXPORT_SYMBOL(kernel_recvmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
863static void sock_aio_dtor(struct kiocb *iocb)
864{
865 kfree(iocb->private);
866}
867
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -0300868static ssize_t sock_sendpage(struct file *file, struct page *page,
869 int offset, size_t size, loff_t *ppos, int more)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
871 struct socket *sock;
872 int flags;
873
Eric Dumazetb69aee02005-09-06 14:42:45 -0700874 sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Eric Dumazet35f9c092012-04-05 03:05:35 +0000876 flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
877 /* more is a combination of MSG_MORE and MSG_SENDPAGE_NOTLAST */
878 flags |= more;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Linus Torvaldse6949582009-08-13 08:28:36 -0700880 return kernel_sendpage(sock, page, offset, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881}
882
Jens Axboe9c55e012007-11-06 23:30:13 -0800883static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700884 struct pipe_inode_info *pipe, size_t len,
Jens Axboe9c55e012007-11-06 23:30:13 -0800885 unsigned int flags)
886{
887 struct socket *sock = file->private_data;
888
Rémi Denis-Courmont997b37d2008-02-15 02:35:45 -0800889 if (unlikely(!sock->ops->splice_read))
890 return -EINVAL;
891
Jens Axboe9c55e012007-11-06 23:30:13 -0800892 return sock->ops->splice_read(sock, ppos, pipe, len, flags);
893}
894
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800895static struct sock_iocb *alloc_sock_iocb(struct kiocb *iocb,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700896 struct sock_iocb *siocb)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800897{
898 if (!is_sync_kiocb(iocb)) {
899 siocb = kmalloc(sizeof(*siocb), GFP_KERNEL);
900 if (!siocb)
901 return NULL;
902 iocb->ki_dtor = sock_aio_dtor;
903 }
904
905 siocb->kiocb = iocb;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800906 iocb->private = siocb;
907 return siocb;
908}
909
910static ssize_t do_sock_read(struct msghdr *msg, struct kiocb *iocb,
Badari Pulavarty027445c2006-09-30 23:28:46 -0700911 struct file *file, const struct iovec *iov,
912 unsigned long nr_segs)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800913{
914 struct socket *sock = file->private_data;
915 size_t size = 0;
916 int i;
917
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700918 for (i = 0; i < nr_segs; i++)
919 size += iov[i].iov_len;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800920
921 msg->msg_name = NULL;
922 msg->msg_namelen = 0;
923 msg->msg_control = NULL;
924 msg->msg_controllen = 0;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700925 msg->msg_iov = (struct iovec *)iov;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800926 msg->msg_iovlen = nr_segs;
927 msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
928
929 return __sock_recvmsg(iocb, sock, msg, size, msg->msg_flags);
930}
931
Badari Pulavarty027445c2006-09-30 23:28:46 -0700932static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov,
933 unsigned long nr_segs, loff_t pos)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800934{
935 struct sock_iocb siocb, *x;
936
937 if (pos != 0)
938 return -ESPIPE;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700939
940 if (iocb->ki_left == 0) /* Match SYS5 behaviour */
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800941 return 0;
942
Badari Pulavarty027445c2006-09-30 23:28:46 -0700943
944 x = alloc_sock_iocb(iocb, &siocb);
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800945 if (!x)
946 return -ENOMEM;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700947 return do_sock_read(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800948}
949
950static ssize_t do_sock_write(struct msghdr *msg, struct kiocb *iocb,
Badari Pulavarty027445c2006-09-30 23:28:46 -0700951 struct file *file, const struct iovec *iov,
952 unsigned long nr_segs)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800953{
954 struct socket *sock = file->private_data;
955 size_t size = 0;
956 int i;
957
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700958 for (i = 0; i < nr_segs; i++)
959 size += iov[i].iov_len;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800960
961 msg->msg_name = NULL;
962 msg->msg_namelen = 0;
963 msg->msg_control = NULL;
964 msg->msg_controllen = 0;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700965 msg->msg_iov = (struct iovec *)iov;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800966 msg->msg_iovlen = nr_segs;
967 msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
968 if (sock->type == SOCK_SEQPACKET)
969 msg->msg_flags |= MSG_EOR;
970
971 return __sock_sendmsg(iocb, sock, msg, size);
972}
973
Badari Pulavarty027445c2006-09-30 23:28:46 -0700974static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov,
975 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800977 struct sock_iocb siocb, *x;
978
979 if (pos != 0)
980 return -ESPIPE;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700981
Badari Pulavarty027445c2006-09-30 23:28:46 -0700982 x = alloc_sock_iocb(iocb, &siocb);
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800983 if (!x)
984 return -ENOMEM;
985
Badari Pulavarty027445c2006-09-30 23:28:46 -0700986 return do_sock_write(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987}
988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989/*
990 * Atomic setting of ioctl hooks to avoid race
991 * with module unload.
992 */
993
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800994static DEFINE_MUTEX(br_ioctl_mutex);
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700995static int (*br_ioctl_hook) (struct net *, unsigned int cmd, void __user *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Eric W. Biederman881d9662007-09-17 11:56:21 -0700997void brioctl_set(int (*hook) (struct net *, unsigned int, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800999 mutex_lock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 br_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001001 mutex_unlock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002}
1003EXPORT_SYMBOL(brioctl_set);
1004
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001005static DEFINE_MUTEX(vlan_ioctl_mutex);
Eric W. Biederman881d9662007-09-17 11:56:21 -07001006static int (*vlan_ioctl_hook) (struct net *, void __user *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Eric W. Biederman881d9662007-09-17 11:56:21 -07001008void vlan_ioctl_set(int (*hook) (struct net *, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001010 mutex_lock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 vlan_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001012 mutex_unlock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013}
1014EXPORT_SYMBOL(vlan_ioctl_set);
1015
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001016static DEFINE_MUTEX(dlci_ioctl_mutex);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001017static int (*dlci_ioctl_hook) (unsigned int, void __user *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001019void dlci_ioctl_set(int (*hook) (unsigned int, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001021 mutex_lock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 dlci_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001023 mutex_unlock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024}
1025EXPORT_SYMBOL(dlci_ioctl_set);
1026
Arnd Bergmann6b960182009-11-06 23:10:54 -08001027static long sock_do_ioctl(struct net *net, struct socket *sock,
1028 unsigned int cmd, unsigned long arg)
1029{
1030 int err;
1031 void __user *argp = (void __user *)arg;
1032
1033 err = sock->ops->ioctl(sock, cmd, arg);
1034
1035 /*
1036 * If this ioctl is unknown try to hand it down
1037 * to the NIC driver.
1038 */
1039 if (err == -ENOIOCTLCMD)
1040 err = dev_ioctl(net, cmd, argp);
1041
1042 return err;
1043}
1044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045/*
1046 * With an ioctl, arg may well be a user mode pointer, but we don't know
1047 * what to do with it - that's up to the protocol still.
1048 */
1049
1050static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
1051{
1052 struct socket *sock;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001053 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 void __user *argp = (void __user *)arg;
1055 int pid, err;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001056 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
Eric Dumazetb69aee02005-09-06 14:42:45 -07001058 sock = file->private_data;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001059 sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001060 net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
Eric W. Biederman881d9662007-09-17 11:56:21 -07001062 err = dev_ioctl(net, cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 } else
Johannes Berg3d23e342009-09-29 23:27:28 +02001064#ifdef CONFIG_WEXT_CORE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
Eric W. Biederman881d9662007-09-17 11:56:21 -07001066 err = dev_ioctl(net, cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 } else
Johannes Berg3d23e342009-09-29 23:27:28 +02001068#endif
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001069 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 case FIOSETOWN:
1071 case SIOCSPGRP:
1072 err = -EFAULT;
1073 if (get_user(pid, (int __user *)argp))
1074 break;
1075 err = f_setown(sock->file, pid, 1);
1076 break;
1077 case FIOGETOWN:
1078 case SIOCGPGRP:
Eric W. Biederman609d7fa2006-10-02 02:17:15 -07001079 err = put_user(f_getown(sock->file),
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001080 (int __user *)argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 break;
1082 case SIOCGIFBR:
1083 case SIOCSIFBR:
1084 case SIOCBRADDBR:
1085 case SIOCBRDELBR:
1086 err = -ENOPKG;
1087 if (!br_ioctl_hook)
1088 request_module("bridge");
1089
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001090 mutex_lock(&br_ioctl_mutex);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001091 if (br_ioctl_hook)
Eric W. Biederman881d9662007-09-17 11:56:21 -07001092 err = br_ioctl_hook(net, cmd, argp);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001093 mutex_unlock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 break;
1095 case SIOCGIFVLAN:
1096 case SIOCSIFVLAN:
1097 err = -ENOPKG;
1098 if (!vlan_ioctl_hook)
1099 request_module("8021q");
1100
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001101 mutex_lock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 if (vlan_ioctl_hook)
Eric W. Biederman881d9662007-09-17 11:56:21 -07001103 err = vlan_ioctl_hook(net, argp);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001104 mutex_unlock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 case SIOCADDDLCI:
1107 case SIOCDELDLCI:
1108 err = -ENOPKG;
1109 if (!dlci_ioctl_hook)
1110 request_module("dlci");
1111
Pavel Emelyanov7512cbf2008-03-21 15:58:52 -07001112 mutex_lock(&dlci_ioctl_mutex);
1113 if (dlci_ioctl_hook)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 err = dlci_ioctl_hook(cmd, argp);
Pavel Emelyanov7512cbf2008-03-21 15:58:52 -07001115 mutex_unlock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 break;
1117 default:
Arnd Bergmann6b960182009-11-06 23:10:54 -08001118 err = sock_do_ioctl(net, sock, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 break;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 return err;
1122}
1123
1124int sock_create_lite(int family, int type, int protocol, struct socket **res)
1125{
1126 int err;
1127 struct socket *sock = NULL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 err = security_socket_create(family, type, protocol, 1);
1130 if (err)
1131 goto out;
1132
1133 sock = sock_alloc();
1134 if (!sock) {
1135 err = -ENOMEM;
1136 goto out;
1137 }
1138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 sock->type = type;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001140 err = security_socket_post_create(sock, family, type, protocol, 1);
1141 if (err)
1142 goto out_release;
1143
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144out:
1145 *res = sock;
1146 return err;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001147out_release:
1148 sock_release(sock);
1149 sock = NULL;
1150 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001152EXPORT_SYMBOL(sock_create_lite);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
1154/* No kernel lock held - perfect */
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001155static unsigned int sock_poll(struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156{
1157 struct socket *sock;
1158
1159 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001160 * We can't return errors to poll, so it's either yes or no.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 */
Eric Dumazetb69aee02005-09-06 14:42:45 -07001162 sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 return sock->ops->poll(file, sock, wait);
1164}
1165
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001166static int sock_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167{
Eric Dumazetb69aee02005-09-06 14:42:45 -07001168 struct socket *sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
1170 return sock->ops->mmap(file, sock, vma);
1171}
1172
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001173static int sock_close(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174{
1175 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001176 * It was possible the inode is NULL we were
1177 * closing an unfinished socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 */
1179
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001180 if (!inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 printk(KERN_DEBUG "sock_close: NULL inode\n");
1182 return 0;
1183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 sock_release(SOCKET_I(inode));
1185 return 0;
1186}
1187
1188/*
1189 * Update the socket async list
1190 *
1191 * Fasync_list locking strategy.
1192 *
1193 * 1. fasync_list is modified only under process context socket lock
1194 * i.e. under semaphore.
1195 * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
Eric Dumazet989a2972010-04-14 09:55:35 +00001196 * or under socket lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 */
1198
1199static int sock_fasync(int fd, struct file *filp, int on)
1200{
Eric Dumazet989a2972010-04-14 09:55:35 +00001201 struct socket *sock = filp->private_data;
1202 struct sock *sk = sock->sk;
Eric Dumazeteaefd112011-02-18 03:26:36 +00001203 struct socket_wq *wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Eric Dumazet989a2972010-04-14 09:55:35 +00001205 if (sk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
1208 lock_sock(sk);
Eric Dumazeteaefd112011-02-18 03:26:36 +00001209 wq = rcu_dereference_protected(sock->wq, sock_owned_by_user(sk));
1210 fasync_helper(fd, filp, on, &wq->fasync_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Eric Dumazeteaefd112011-02-18 03:26:36 +00001212 if (!wq->fasync_list)
Eric Dumazet989a2972010-04-14 09:55:35 +00001213 sock_reset_flag(sk, SOCK_FASYNC);
Jonathan Corbet76398422009-02-01 14:26:59 -07001214 else
Eric Dumazetbcdce712009-10-06 17:28:29 -07001215 sock_set_flag(sk, SOCK_FASYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
Eric Dumazet989a2972010-04-14 09:55:35 +00001217 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 return 0;
1219}
1220
Eric Dumazet43815482010-04-29 11:01:49 +00001221/* This function may be called only under socket lock or callback_lock or rcu_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
1223int sock_wake_async(struct socket *sock, int how, int band)
1224{
Eric Dumazet43815482010-04-29 11:01:49 +00001225 struct socket_wq *wq;
1226
1227 if (!sock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 return -1;
Eric Dumazet43815482010-04-29 11:01:49 +00001229 rcu_read_lock();
1230 wq = rcu_dereference(sock->wq);
1231 if (!wq || !wq->fasync_list) {
1232 rcu_read_unlock();
1233 return -1;
1234 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001235 switch (how) {
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001236 case SOCK_WAKE_WAITD:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 if (test_bit(SOCK_ASYNC_WAITDATA, &sock->flags))
1238 break;
1239 goto call_kill;
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001240 case SOCK_WAKE_SPACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags))
1242 break;
1243 /* fall through */
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001244 case SOCK_WAKE_IO:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001245call_kill:
Eric Dumazet43815482010-04-29 11:01:49 +00001246 kill_fasync(&wq->fasync_list, SIGIO, band);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 break;
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001248 case SOCK_WAKE_URG:
Eric Dumazet43815482010-04-29 11:01:49 +00001249 kill_fasync(&wq->fasync_list, SIGURG, band);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 }
Eric Dumazet43815482010-04-29 11:01:49 +00001251 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 return 0;
1253}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001254EXPORT_SYMBOL(sock_wake_async);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
Pavel Emelyanov721db932010-09-29 16:06:32 +04001256int __sock_create(struct net *net, int family, int type, int protocol,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001257 struct socket **res, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258{
1259 int err;
1260 struct socket *sock;
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001261 const struct net_proto_family *pf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
1263 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001264 * Check protocol is in range
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 */
1266 if (family < 0 || family >= NPROTO)
1267 return -EAFNOSUPPORT;
1268 if (type < 0 || type >= SOCK_MAX)
1269 return -EINVAL;
1270
1271 /* Compatibility.
1272
1273 This uglymoron is moved from INET layer to here to avoid
1274 deadlock in module load.
1275 */
1276 if (family == PF_INET && type == SOCK_PACKET) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001277 static int warned;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 if (!warned) {
1279 warned = 1;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001280 printk(KERN_INFO "%s uses obsolete (PF_INET,SOCK_PACKET)\n",
1281 current->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 }
1283 family = PF_PACKET;
1284 }
1285
1286 err = security_socket_create(family, type, protocol, kern);
1287 if (err)
1288 return err;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001289
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001290 /*
1291 * Allocate the socket and allow the family to set things up. if
1292 * the protocol is 0, the family is instructed to select an appropriate
1293 * default.
1294 */
1295 sock = sock_alloc();
1296 if (!sock) {
Joe Perchese87cc472012-05-13 21:56:26 +00001297 net_warn_ratelimited("socket: no more sockets\n");
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001298 return -ENFILE; /* Not exactly a match, but its the
1299 closest posix thing */
1300 }
1301
1302 sock->type = type;
1303
Johannes Berg95a5afc2008-10-16 15:24:51 -07001304#ifdef CONFIG_MODULES
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001305 /* Attempt to load a protocol module if the find failed.
1306 *
1307 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 * requested real, full-featured networking support upon configuration.
1309 * Otherwise module support will break!
1310 */
Eric Dumazet190683a2010-11-10 10:50:44 +00001311 if (rcu_access_pointer(net_families[family]) == NULL)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001312 request_module("net-pf-%d", family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313#endif
1314
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001315 rcu_read_lock();
1316 pf = rcu_dereference(net_families[family]);
1317 err = -EAFNOSUPPORT;
1318 if (!pf)
1319 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
1321 /*
1322 * We will call the ->create function, that possibly is in a loadable
1323 * module, so we have to bump that loadable module refcnt first.
1324 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001325 if (!try_module_get(pf->owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 goto out_release;
1327
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001328 /* Now protected by module ref count */
1329 rcu_read_unlock();
1330
Eric Paris3f378b62009-11-05 22:18:14 -08001331 err = pf->create(net, sock, protocol, kern);
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001332 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 goto out_module_put;
Frank Filza79af592005-09-27 15:23:38 -07001334
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 /*
1336 * Now to bump the refcnt of the [loadable] module that owns this
1337 * socket at sock_release time we decrement its refcnt.
1338 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001339 if (!try_module_get(sock->ops->owner))
1340 goto out_module_busy;
1341
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 /*
1343 * Now that we're done with the ->create function, the [loadable]
1344 * module can have its refcnt decremented
1345 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001346 module_put(pf->owner);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001347 err = security_socket_post_create(sock, family, type, protocol, kern);
1348 if (err)
Herbert Xu3b185522007-08-15 14:46:02 -07001349 goto out_sock_release;
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001350 *res = sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001352 return 0;
1353
1354out_module_busy:
1355 err = -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356out_module_put:
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001357 sock->ops = NULL;
1358 module_put(pf->owner);
1359out_sock_release:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 sock_release(sock);
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001361 return err;
1362
1363out_release:
1364 rcu_read_unlock();
1365 goto out_sock_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366}
Pavel Emelyanov721db932010-09-29 16:06:32 +04001367EXPORT_SYMBOL(__sock_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
1369int sock_create(int family, int type, int protocol, struct socket **res)
1370{
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001371 return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001373EXPORT_SYMBOL(sock_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
1375int sock_create_kern(int family, int type, int protocol, struct socket **res)
1376{
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001377 return __sock_create(&init_net, family, type, protocol, res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001379EXPORT_SYMBOL(sock_create_kern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001381SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382{
1383 int retval;
1384 struct socket *sock;
Ulrich Dreppera677a032008-07-23 21:29:17 -07001385 int flags;
1386
Ulrich Dreppere38b36f2008-07-23 21:29:42 -07001387 /* Check the SOCK_* constants for consistency. */
1388 BUILD_BUG_ON(SOCK_CLOEXEC != O_CLOEXEC);
1389 BUILD_BUG_ON((SOCK_MAX | SOCK_TYPE_MASK) != SOCK_TYPE_MASK);
1390 BUILD_BUG_ON(SOCK_CLOEXEC & SOCK_TYPE_MASK);
1391 BUILD_BUG_ON(SOCK_NONBLOCK & SOCK_TYPE_MASK);
1392
Ulrich Dreppera677a032008-07-23 21:29:17 -07001393 flags = type & ~SOCK_TYPE_MASK;
Ulrich Drepper77d27202008-07-23 21:29:35 -07001394 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Dreppera677a032008-07-23 21:29:17 -07001395 return -EINVAL;
1396 type &= SOCK_TYPE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001398 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1399 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1400
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 retval = sock_create(family, type, protocol, &sock);
1402 if (retval < 0)
1403 goto out;
1404
Ulrich Drepper77d27202008-07-23 21:29:35 -07001405 retval = sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 if (retval < 0)
1407 goto out_release;
1408
1409out:
1410 /* It may be already another descriptor 8) Not kernel problem. */
1411 return retval;
1412
1413out_release:
1414 sock_release(sock);
1415 return retval;
1416}
1417
1418/*
1419 * Create a pair of connected sockets.
1420 */
1421
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001422SYSCALL_DEFINE4(socketpair, int, family, int, type, int, protocol,
1423 int __user *, usockvec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424{
1425 struct socket *sock1, *sock2;
1426 int fd1, fd2, err;
Al Virodb349502007-02-07 01:48:00 -05001427 struct file *newfile1, *newfile2;
Ulrich Dreppera677a032008-07-23 21:29:17 -07001428 int flags;
1429
1430 flags = type & ~SOCK_TYPE_MASK;
Ulrich Drepper77d27202008-07-23 21:29:35 -07001431 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Dreppera677a032008-07-23 21:29:17 -07001432 return -EINVAL;
1433 type &= SOCK_TYPE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001435 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1436 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1437
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 /*
1439 * Obtain the first socket and check if the underlying protocol
1440 * supports the socketpair call.
1441 */
1442
1443 err = sock_create(family, type, protocol, &sock1);
1444 if (err < 0)
1445 goto out;
1446
1447 err = sock_create(family, type, protocol, &sock2);
1448 if (err < 0)
1449 goto out_release_1;
1450
1451 err = sock1->ops->socketpair(sock1, sock2);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001452 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 goto out_release_both;
1454
Al Viro28407632012-08-17 23:54:15 -04001455 fd1 = get_unused_fd_flags(flags);
David S. Millerbf3c23d2007-10-29 21:54:02 -07001456 if (unlikely(fd1 < 0)) {
1457 err = fd1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 goto out_release_both;
David S. Millerbf3c23d2007-10-29 21:54:02 -07001459 }
Al Viro28407632012-08-17 23:54:15 -04001460 fd2 = get_unused_fd_flags(flags);
Al Viro198de4d2009-08-05 19:29:23 +04001461 if (unlikely(fd2 < 0)) {
1462 err = fd2;
Al Viro28407632012-08-17 23:54:15 -04001463 put_unused_fd(fd1);
1464 goto out_release_both;
1465 }
1466
Linus Torvaldsaab174f2012-10-02 20:25:04 -07001467 newfile1 = sock_alloc_file(sock1, flags, NULL);
Al Viro28407632012-08-17 23:54:15 -04001468 if (unlikely(IS_ERR(newfile1))) {
1469 err = PTR_ERR(newfile1);
1470 put_unused_fd(fd1);
1471 put_unused_fd(fd2);
1472 goto out_release_both;
1473 }
1474
Linus Torvaldsaab174f2012-10-02 20:25:04 -07001475 newfile2 = sock_alloc_file(sock2, flags, NULL);
Al Viro28407632012-08-17 23:54:15 -04001476 if (IS_ERR(newfile2)) {
1477 err = PTR_ERR(newfile2);
Al Viro198de4d2009-08-05 19:29:23 +04001478 fput(newfile1);
1479 put_unused_fd(fd1);
Al Viro28407632012-08-17 23:54:15 -04001480 put_unused_fd(fd2);
Al Viro198de4d2009-08-05 19:29:23 +04001481 sock_release(sock2);
1482 goto out;
Al Virodb349502007-02-07 01:48:00 -05001483 }
1484
Al Viro157cf642008-12-14 04:57:47 -05001485 audit_fd_pair(fd1, fd2);
Al Virodb349502007-02-07 01:48:00 -05001486 fd_install(fd1, newfile1);
1487 fd_install(fd2, newfile2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 /* fd1 and fd2 may be already another descriptors.
1489 * Not kernel problem.
1490 */
1491
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001492 err = put_user(fd1, &usockvec[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 if (!err)
1494 err = put_user(fd2, &usockvec[1]);
1495 if (!err)
1496 return 0;
1497
1498 sys_close(fd2);
1499 sys_close(fd1);
1500 return err;
1501
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502out_release_both:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001503 sock_release(sock2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504out_release_1:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001505 sock_release(sock1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506out:
1507 return err;
1508}
1509
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510/*
1511 * Bind a name to a socket. Nothing much to do here since it's
1512 * the protocol's responsibility to handle the local address.
1513 *
1514 * We move the socket address to kernel space before we call
1515 * the protocol layer (having also checked the address is ok).
1516 */
1517
Heiko Carstens20f37032009-01-14 14:14:23 +01001518SYSCALL_DEFINE3(bind, int, fd, struct sockaddr __user *, umyaddr, int, addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519{
1520 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001521 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001522 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001524 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001525 if (sock) {
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001526 err = move_addr_to_kernel(umyaddr, addrlen, &address);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001527 if (err >= 0) {
1528 err = security_socket_bind(sock,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001529 (struct sockaddr *)&address,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001530 addrlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001531 if (!err)
1532 err = sock->ops->bind(sock,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001533 (struct sockaddr *)
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001534 &address, addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 }
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001536 fput_light(sock->file, fput_needed);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 return err;
1539}
1540
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541/*
1542 * Perform a listen. Basically, we allow the protocol to do anything
1543 * necessary for a listen, and if that works, we mark the socket as
1544 * ready for listening.
1545 */
1546
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001547SYSCALL_DEFINE2(listen, int, fd, int, backlog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548{
1549 struct socket *sock;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001550 int err, fput_needed;
Pavel Emelyanovb8e1f9b2007-12-08 00:12:33 -08001551 int somaxconn;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001552
1553 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1554 if (sock) {
Pavel Emelyanov8efa6e92008-03-31 19:41:14 -07001555 somaxconn = sock_net(sock->sk)->core.sysctl_somaxconn;
Eric Dumazet95c96172012-04-15 05:58:06 +00001556 if ((unsigned int)backlog > somaxconn)
Pavel Emelyanovb8e1f9b2007-12-08 00:12:33 -08001557 backlog = somaxconn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
1559 err = security_socket_listen(sock, backlog);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001560 if (!err)
1561 err = sock->ops->listen(sock, backlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001563 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 }
1565 return err;
1566}
1567
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568/*
1569 * For accept, we attempt to create a new socket, set up the link
1570 * with the client, wake up the client, then return the new
1571 * connected fd. We collect the address of the connector in kernel
1572 * space and move it to user at the very end. This is unclean because
1573 * we open the socket then return an error.
1574 *
1575 * 1003.1g adds the ability to recvmsg() to query connection pending
1576 * status to recvmsg. We need to add that support in a way thats
1577 * clean when we restucture accept also.
1578 */
1579
Heiko Carstens20f37032009-01-14 14:14:23 +01001580SYSCALL_DEFINE4(accept4, int, fd, struct sockaddr __user *, upeer_sockaddr,
1581 int __user *, upeer_addrlen, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582{
1583 struct socket *sock, *newsock;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001584 struct file *newfile;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001585 int err, len, newfd, fput_needed;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001586 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
Ulrich Drepper77d27202008-07-23 21:29:35 -07001588 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001589 return -EINVAL;
1590
1591 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1592 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1593
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001594 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 if (!sock)
1596 goto out;
1597
1598 err = -ENFILE;
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001599 newsock = sock_alloc();
1600 if (!newsock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 goto out_put;
1602
1603 newsock->type = sock->type;
1604 newsock->ops = sock->ops;
1605
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 /*
1607 * We don't need try_module_get here, as the listening socket (sock)
1608 * has the protocol module (sock->ops->owner) held.
1609 */
1610 __module_get(newsock->ops->owner);
1611
Al Viro28407632012-08-17 23:54:15 -04001612 newfd = get_unused_fd_flags(flags);
David S. Miller39d8c1b2006-03-20 17:13:49 -08001613 if (unlikely(newfd < 0)) {
1614 err = newfd;
David S. Miller9a1875e2006-04-01 12:48:36 -08001615 sock_release(newsock);
1616 goto out_put;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001617 }
Linus Torvaldsaab174f2012-10-02 20:25:04 -07001618 newfile = sock_alloc_file(newsock, flags, sock->sk->sk_prot_creator->name);
Al Viro28407632012-08-17 23:54:15 -04001619 if (unlikely(IS_ERR(newfile))) {
1620 err = PTR_ERR(newfile);
1621 put_unused_fd(newfd);
1622 sock_release(newsock);
1623 goto out_put;
1624 }
David S. Miller39d8c1b2006-03-20 17:13:49 -08001625
Frank Filza79af592005-09-27 15:23:38 -07001626 err = security_socket_accept(sock, newsock);
1627 if (err)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001628 goto out_fd;
Frank Filza79af592005-09-27 15:23:38 -07001629
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 err = sock->ops->accept(sock, newsock, sock->file->f_flags);
1631 if (err < 0)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001632 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
1634 if (upeer_sockaddr) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001635 if (newsock->ops->getname(newsock, (struct sockaddr *)&address,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001636 &len, 2) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 err = -ECONNABORTED;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001638 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 }
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001640 err = move_addr_to_user(&address,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001641 len, upeer_sockaddr, upeer_addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 if (err < 0)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001643 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 }
1645
1646 /* File flags are not inherited via accept() unlike another OSes. */
1647
David S. Miller39d8c1b2006-03-20 17:13:49 -08001648 fd_install(newfd, newfile);
1649 err = newfd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001652 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653out:
1654 return err;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001655out_fd:
David S. Miller9606a212006-04-01 01:00:14 -08001656 fput(newfile);
David S. Miller39d8c1b2006-03-20 17:13:49 -08001657 put_unused_fd(newfd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 goto out_put;
1659}
1660
Heiko Carstens20f37032009-01-14 14:14:23 +01001661SYSCALL_DEFINE3(accept, int, fd, struct sockaddr __user *, upeer_sockaddr,
1662 int __user *, upeer_addrlen)
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001663{
Ulrich Drepperde11def2008-11-19 15:36:14 -08001664 return sys_accept4(fd, upeer_sockaddr, upeer_addrlen, 0);
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001665}
1666
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667/*
1668 * Attempt to connect to a socket with the server address. The address
1669 * is in user space so we verify it is OK and move it to kernel space.
1670 *
1671 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
1672 * break bindings
1673 *
1674 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
1675 * other SEQPACKET protocols that take time to connect() as it doesn't
1676 * include the -EINPROGRESS status for such sockets.
1677 */
1678
Heiko Carstens20f37032009-01-14 14:14:23 +01001679SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
1680 int, addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681{
1682 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001683 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001684 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001686 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 if (!sock)
1688 goto out;
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001689 err = move_addr_to_kernel(uservaddr, addrlen, &address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 if (err < 0)
1691 goto out_put;
1692
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001693 err =
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001694 security_socket_connect(sock, (struct sockaddr *)&address, addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 if (err)
1696 goto out_put;
1697
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001698 err = sock->ops->connect(sock, (struct sockaddr *)&address, addrlen,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 sock->file->f_flags);
1700out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001701 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702out:
1703 return err;
1704}
1705
1706/*
1707 * Get the local address ('name') of a socket object. Move the obtained
1708 * name to user space.
1709 */
1710
Heiko Carstens20f37032009-01-14 14:14:23 +01001711SYSCALL_DEFINE3(getsockname, int, fd, struct sockaddr __user *, usockaddr,
1712 int __user *, usockaddr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713{
1714 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001715 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001716 int len, err, fput_needed;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001717
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001718 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 if (!sock)
1720 goto out;
1721
1722 err = security_socket_getsockname(sock);
1723 if (err)
1724 goto out_put;
1725
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001726 err = sock->ops->getname(sock, (struct sockaddr *)&address, &len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 if (err)
1728 goto out_put;
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001729 err = move_addr_to_user(&address, len, usockaddr, usockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
1731out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001732 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733out:
1734 return err;
1735}
1736
1737/*
1738 * Get the remote address ('name') of a socket object. Move the obtained
1739 * name to user space.
1740 */
1741
Heiko Carstens20f37032009-01-14 14:14:23 +01001742SYSCALL_DEFINE3(getpeername, int, fd, struct sockaddr __user *, usockaddr,
1743 int __user *, usockaddr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744{
1745 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001746 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001747 int len, err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001749 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1750 if (sock != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 err = security_socket_getpeername(sock);
1752 if (err) {
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001753 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 return err;
1755 }
1756
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001757 err =
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001758 sock->ops->getname(sock, (struct sockaddr *)&address, &len,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001759 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 if (!err)
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001761 err = move_addr_to_user(&address, len, usockaddr,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001762 usockaddr_len);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001763 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 }
1765 return err;
1766}
1767
1768/*
1769 * Send a datagram to a given address. We move the address into kernel
1770 * space and check the user space data area is readable before invoking
1771 * the protocol.
1772 */
1773
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001774SYSCALL_DEFINE6(sendto, int, fd, void __user *, buff, size_t, len,
Eric Dumazet95c96172012-04-15 05:58:06 +00001775 unsigned int, flags, struct sockaddr __user *, addr,
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001776 int, addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777{
1778 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001779 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 int err;
1781 struct msghdr msg;
1782 struct iovec iov;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001783 int fput_needed;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001784
Linus Torvalds253eacc2010-10-30 16:43:10 -07001785 if (len > INT_MAX)
1786 len = INT_MAX;
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001787 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1788 if (!sock)
David S. Miller4387ff72007-02-08 15:06:08 -08001789 goto out;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001790
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001791 iov.iov_base = buff;
1792 iov.iov_len = len;
1793 msg.msg_name = NULL;
1794 msg.msg_iov = &iov;
1795 msg.msg_iovlen = 1;
1796 msg.msg_control = NULL;
1797 msg.msg_controllen = 0;
1798 msg.msg_namelen = 0;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001799 if (addr) {
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001800 err = move_addr_to_kernel(addr, addr_len, &address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 if (err < 0)
1802 goto out_put;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001803 msg.msg_name = (struct sockaddr *)&address;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001804 msg.msg_namelen = addr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 }
1806 if (sock->file->f_flags & O_NONBLOCK)
1807 flags |= MSG_DONTWAIT;
1808 msg.msg_flags = flags;
1809 err = sock_sendmsg(sock, &msg, len);
1810
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001811out_put:
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001812 fput_light(sock->file, fput_needed);
David S. Miller4387ff72007-02-08 15:06:08 -08001813out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 return err;
1815}
1816
1817/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001818 * Send a datagram down a socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 */
1820
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001821SYSCALL_DEFINE4(send, int, fd, void __user *, buff, size_t, len,
Eric Dumazet95c96172012-04-15 05:58:06 +00001822 unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823{
1824 return sys_sendto(fd, buff, len, flags, NULL, 0);
1825}
1826
1827/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001828 * Receive a frame from the socket and optionally record the address of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 * sender. We verify the buffers are writable and if needed move the
1830 * sender address from kernel to user space.
1831 */
1832
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001833SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, ubuf, size_t, size,
Eric Dumazet95c96172012-04-15 05:58:06 +00001834 unsigned int, flags, struct sockaddr __user *, addr,
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001835 int __user *, addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836{
1837 struct socket *sock;
1838 struct iovec iov;
1839 struct msghdr msg;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001840 struct sockaddr_storage address;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001841 int err, err2;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001842 int fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
Linus Torvalds253eacc2010-10-30 16:43:10 -07001844 if (size > INT_MAX)
1845 size = INT_MAX;
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001846 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 if (!sock)
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001848 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001850 msg.msg_control = NULL;
1851 msg.msg_controllen = 0;
1852 msg.msg_iovlen = 1;
1853 msg.msg_iov = &iov;
1854 iov.iov_len = size;
1855 iov.iov_base = ubuf;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001856 msg.msg_name = (struct sockaddr *)&address;
1857 msg.msg_namelen = sizeof(address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 if (sock->file->f_flags & O_NONBLOCK)
1859 flags |= MSG_DONTWAIT;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001860 err = sock_recvmsg(sock, &msg, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001862 if (err >= 0 && addr != NULL) {
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001863 err2 = move_addr_to_user(&address,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001864 msg.msg_namelen, addr, addr_len);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001865 if (err2 < 0)
1866 err = err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 }
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001868
1869 fput_light(sock->file, fput_needed);
David S. Miller4387ff72007-02-08 15:06:08 -08001870out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 return err;
1872}
1873
1874/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001875 * Receive a datagram from a socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 */
1877
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001878asmlinkage long sys_recv(int fd, void __user *ubuf, size_t size,
Eric Dumazet95c96172012-04-15 05:58:06 +00001879 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880{
1881 return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
1882}
1883
1884/*
1885 * Set a socket option. Because we don't know the option lengths we have
1886 * to pass the user mode parameter for the protocols to sort out.
1887 */
1888
Heiko Carstens20f37032009-01-14 14:14:23 +01001889SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname,
1890 char __user *, optval, int, optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001892 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 struct socket *sock;
1894
1895 if (optlen < 0)
1896 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001897
1898 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1899 if (sock != NULL) {
1900 err = security_socket_setsockopt(sock, level, optname);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001901 if (err)
1902 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
1904 if (level == SOL_SOCKET)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001905 err =
1906 sock_setsockopt(sock, level, optname, optval,
1907 optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 else
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001909 err =
1910 sock->ops->setsockopt(sock, level, optname, optval,
1911 optlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001912out_put:
1913 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 }
1915 return err;
1916}
1917
1918/*
1919 * Get a socket option. Because we don't know the option lengths we have
1920 * to pass a user mode parameter for the protocols to sort out.
1921 */
1922
Heiko Carstens20f37032009-01-14 14:14:23 +01001923SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname,
1924 char __user *, optval, int __user *, optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001926 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 struct socket *sock;
1928
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001929 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1930 if (sock != NULL) {
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001931 err = security_socket_getsockopt(sock, level, optname);
1932 if (err)
1933 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
1935 if (level == SOL_SOCKET)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001936 err =
1937 sock_getsockopt(sock, level, optname, optval,
1938 optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 else
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001940 err =
1941 sock->ops->getsockopt(sock, level, optname, optval,
1942 optlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001943out_put:
1944 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 }
1946 return err;
1947}
1948
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949/*
1950 * Shutdown a socket.
1951 */
1952
Heiko Carstens754fe8d2009-01-14 14:14:09 +01001953SYSCALL_DEFINE2(shutdown, int, fd, int, how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001955 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 struct socket *sock;
1957
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001958 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1959 if (sock != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 err = security_socket_shutdown(sock, how);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001961 if (!err)
1962 err = sock->ops->shutdown(sock, how);
1963 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 }
1965 return err;
1966}
1967
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001968/* A couple of helpful macros for getting the address of the 32/64 bit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 * fields which are the same type (int / unsigned) on our platforms.
1970 */
1971#define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
1972#define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
1973#define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
1974
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00001975struct used_address {
1976 struct sockaddr_storage name;
1977 unsigned int name_len;
1978};
1979
Anton Blanchard228e5482011-05-02 20:21:35 +00001980static int __sys_sendmsg(struct socket *sock, struct msghdr __user *msg,
Eric Dumazet95c96172012-04-15 05:58:06 +00001981 struct msghdr *msg_sys, unsigned int flags,
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00001982 struct used_address *used_address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983{
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001984 struct compat_msghdr __user *msg_compat =
1985 (struct compat_msghdr __user *)msg;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001986 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
Alex Williamsonb9d717a2005-09-26 14:28:02 -07001988 unsigned char ctl[sizeof(struct cmsghdr) + 20]
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001989 __attribute__ ((aligned(sizeof(__kernel_size_t))));
1990 /* 20 is size of ipv6_pktinfo */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 unsigned char *ctl_buf = ctl;
Eric Dumazeta74e9102012-04-20 20:04:01 +02001992 int err, ctl_len, total_len;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001993
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 err = -EFAULT;
1995 if (MSG_CMSG_COMPAT & flags) {
Anton Blanchard228e5482011-05-02 20:21:35 +00001996 if (get_compat_msghdr(msg_sys, msg_compat))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 return -EFAULT;
Anton Blanchard228e5482011-05-02 20:21:35 +00001998 } else if (copy_from_user(msg_sys, msg, sizeof(struct msghdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 return -EFAULT;
2000
Anton Blanchard228e5482011-05-02 20:21:35 +00002001 if (msg_sys->msg_iovlen > UIO_FASTIOV) {
Eric Dumazeta74e9102012-04-20 20:04:01 +02002002 err = -EMSGSIZE;
2003 if (msg_sys->msg_iovlen > UIO_MAXIOV)
2004 goto out;
2005 err = -ENOMEM;
2006 iov = kmalloc(msg_sys->msg_iovlen * sizeof(struct iovec),
2007 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 if (!iov)
Anton Blanchard228e5482011-05-02 20:21:35 +00002009 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 }
2011
2012 /* This will also move the address data into kernel space */
2013 if (MSG_CMSG_COMPAT & flags) {
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00002014 err = verify_compat_iovec(msg_sys, iov, &address, VERIFY_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 } else
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00002016 err = verify_iovec(msg_sys, iov, &address, VERIFY_READ);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002017 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 goto out_freeiov;
2019 total_len = err;
2020
2021 err = -ENOBUFS;
2022
Anton Blanchard228e5482011-05-02 20:21:35 +00002023 if (msg_sys->msg_controllen > INT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 goto out_freeiov;
Anton Blanchard228e5482011-05-02 20:21:35 +00002025 ctl_len = msg_sys->msg_controllen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002027 err =
Anton Blanchard228e5482011-05-02 20:21:35 +00002028 cmsghdr_from_user_compat_to_kern(msg_sys, sock->sk, ctl,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002029 sizeof(ctl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 if (err)
2031 goto out_freeiov;
Anton Blanchard228e5482011-05-02 20:21:35 +00002032 ctl_buf = msg_sys->msg_control;
2033 ctl_len = msg_sys->msg_controllen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 } else if (ctl_len) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002035 if (ctl_len > sizeof(ctl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002037 if (ctl_buf == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 goto out_freeiov;
2039 }
2040 err = -EFAULT;
2041 /*
Anton Blanchard228e5482011-05-02 20:21:35 +00002042 * Careful! Before this, msg_sys->msg_control contains a user pointer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
2044 * checking falls down on this.
2045 */
Namhyung Kimfb8621b2010-09-07 03:55:00 +00002046 if (copy_from_user(ctl_buf,
Anton Blanchard228e5482011-05-02 20:21:35 +00002047 (void __user __force *)msg_sys->msg_control,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002048 ctl_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 goto out_freectl;
Anton Blanchard228e5482011-05-02 20:21:35 +00002050 msg_sys->msg_control = ctl_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 }
Anton Blanchard228e5482011-05-02 20:21:35 +00002052 msg_sys->msg_flags = flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
2054 if (sock->file->f_flags & O_NONBLOCK)
Anton Blanchard228e5482011-05-02 20:21:35 +00002055 msg_sys->msg_flags |= MSG_DONTWAIT;
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002056 /*
2057 * If this is sendmmsg() and current destination address is same as
2058 * previously succeeded address, omit asking LSM's decision.
2059 * used_address->name_len is initialized to UINT_MAX so that the first
2060 * destination address never matches.
2061 */
Mathieu Desnoyersbc909d92011-08-24 19:45:03 -07002062 if (used_address && msg_sys->msg_name &&
2063 used_address->name_len == msg_sys->msg_namelen &&
2064 !memcmp(&used_address->name, msg_sys->msg_name,
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002065 used_address->name_len)) {
2066 err = sock_sendmsg_nosec(sock, msg_sys, total_len);
2067 goto out_freectl;
2068 }
2069 err = sock_sendmsg(sock, msg_sys, total_len);
2070 /*
2071 * If this is sendmmsg() and sending to current destination address was
2072 * successful, remember it.
2073 */
2074 if (used_address && err >= 0) {
2075 used_address->name_len = msg_sys->msg_namelen;
Mathieu Desnoyersbc909d92011-08-24 19:45:03 -07002076 if (msg_sys->msg_name)
2077 memcpy(&used_address->name, msg_sys->msg_name,
2078 used_address->name_len);
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
2081out_freectl:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002082 if (ctl_buf != ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 sock_kfree_s(sock->sk, ctl_buf, ctl_len);
2084out_freeiov:
2085 if (iov != iovstack)
Eric Dumazeta74e9102012-04-20 20:04:01 +02002086 kfree(iov);
Anton Blanchard228e5482011-05-02 20:21:35 +00002087out:
2088 return err;
2089}
2090
2091/*
2092 * BSD sendmsg interface
2093 */
2094
Eric Dumazet95c96172012-04-15 05:58:06 +00002095SYSCALL_DEFINE3(sendmsg, int, fd, struct msghdr __user *, msg, unsigned int, flags)
Anton Blanchard228e5482011-05-02 20:21:35 +00002096{
2097 int fput_needed, err;
2098 struct msghdr msg_sys;
2099 struct socket *sock = sockfd_lookup_light(fd, &err, &fput_needed);
2100
2101 if (!sock)
2102 goto out;
2103
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002104 err = __sys_sendmsg(sock, msg, &msg_sys, flags, NULL);
Anton Blanchard228e5482011-05-02 20:21:35 +00002105
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002106 fput_light(sock->file, fput_needed);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002107out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 return err;
2109}
2110
Anton Blanchard228e5482011-05-02 20:21:35 +00002111/*
2112 * Linux sendmmsg interface
2113 */
2114
2115int __sys_sendmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
2116 unsigned int flags)
2117{
2118 int fput_needed, err, datagrams;
2119 struct socket *sock;
2120 struct mmsghdr __user *entry;
2121 struct compat_mmsghdr __user *compat_entry;
2122 struct msghdr msg_sys;
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002123 struct used_address used_address;
Anton Blanchard228e5482011-05-02 20:21:35 +00002124
Anton Blanchard98382f42011-08-04 14:07:39 +00002125 if (vlen > UIO_MAXIOV)
2126 vlen = UIO_MAXIOV;
Anton Blanchard228e5482011-05-02 20:21:35 +00002127
2128 datagrams = 0;
2129
2130 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2131 if (!sock)
2132 return err;
2133
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002134 used_address.name_len = UINT_MAX;
Anton Blanchard228e5482011-05-02 20:21:35 +00002135 entry = mmsg;
2136 compat_entry = (struct compat_mmsghdr __user *)mmsg;
Anton Blanchard728ffb82011-08-04 14:07:38 +00002137 err = 0;
Anton Blanchard228e5482011-05-02 20:21:35 +00002138
2139 while (datagrams < vlen) {
Anton Blanchard228e5482011-05-02 20:21:35 +00002140 if (MSG_CMSG_COMPAT & flags) {
2141 err = __sys_sendmsg(sock, (struct msghdr __user *)compat_entry,
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002142 &msg_sys, flags, &used_address);
Anton Blanchard228e5482011-05-02 20:21:35 +00002143 if (err < 0)
2144 break;
2145 err = __put_user(err, &compat_entry->msg_len);
2146 ++compat_entry;
2147 } else {
2148 err = __sys_sendmsg(sock, (struct msghdr __user *)entry,
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002149 &msg_sys, flags, &used_address);
Anton Blanchard228e5482011-05-02 20:21:35 +00002150 if (err < 0)
2151 break;
2152 err = put_user(err, &entry->msg_len);
2153 ++entry;
2154 }
2155
2156 if (err)
2157 break;
2158 ++datagrams;
2159 }
2160
Anton Blanchard228e5482011-05-02 20:21:35 +00002161 fput_light(sock->file, fput_needed);
2162
Anton Blanchard728ffb82011-08-04 14:07:38 +00002163 /* We only return an error if no datagrams were able to be sent */
2164 if (datagrams != 0)
Anton Blanchard228e5482011-05-02 20:21:35 +00002165 return datagrams;
2166
Anton Blanchard228e5482011-05-02 20:21:35 +00002167 return err;
2168}
2169
2170SYSCALL_DEFINE4(sendmmsg, int, fd, struct mmsghdr __user *, mmsg,
2171 unsigned int, vlen, unsigned int, flags)
2172{
2173 return __sys_sendmmsg(fd, mmsg, vlen, flags);
2174}
2175
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002176static int __sys_recvmsg(struct socket *sock, struct msghdr __user *msg,
Eric Dumazet95c96172012-04-15 05:58:06 +00002177 struct msghdr *msg_sys, unsigned int flags, int nosec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178{
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002179 struct compat_msghdr __user *msg_compat =
2180 (struct compat_msghdr __user *)msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 struct iovec iovstack[UIO_FASTIOV];
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002182 struct iovec *iov = iovstack;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 unsigned long cmsg_ptr;
Eric Dumazeta74e9102012-04-20 20:04:01 +02002184 int err, total_len, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
2186 /* kernel mode address */
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07002187 struct sockaddr_storage addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
2189 /* user mode address pointers */
2190 struct sockaddr __user *uaddr;
2191 int __user *uaddr_len;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002192
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 if (MSG_CMSG_COMPAT & flags) {
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002194 if (get_compat_msghdr(msg_sys, msg_compat))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 return -EFAULT;
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002196 } else if (copy_from_user(msg_sys, msg, sizeof(struct msghdr)))
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002197 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002199 if (msg_sys->msg_iovlen > UIO_FASTIOV) {
Eric Dumazeta74e9102012-04-20 20:04:01 +02002200 err = -EMSGSIZE;
2201 if (msg_sys->msg_iovlen > UIO_MAXIOV)
2202 goto out;
2203 err = -ENOMEM;
2204 iov = kmalloc(msg_sys->msg_iovlen * sizeof(struct iovec),
2205 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 if (!iov)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002207 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 }
2209
2210 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002211 * Save the user-mode address (verify_iovec will change the
2212 * kernel msghdr to use the kernel address space)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002214
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002215 uaddr = (__force void __user *)msg_sys->msg_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 uaddr_len = COMPAT_NAMELEN(msg);
2217 if (MSG_CMSG_COMPAT & flags) {
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00002218 err = verify_compat_iovec(msg_sys, iov, &addr, VERIFY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 } else
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00002220 err = verify_iovec(msg_sys, iov, &addr, VERIFY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 if (err < 0)
2222 goto out_freeiov;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002223 total_len = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002225 cmsg_ptr = (unsigned long)msg_sys->msg_control;
2226 msg_sys->msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002227
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 if (sock->file->f_flags & O_NONBLOCK)
2229 flags |= MSG_DONTWAIT;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002230 err = (nosec ? sock_recvmsg_nosec : sock_recvmsg)(sock, msg_sys,
2231 total_len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 if (err < 0)
2233 goto out_freeiov;
2234 len = err;
2235
2236 if (uaddr != NULL) {
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00002237 err = move_addr_to_user(&addr,
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002238 msg_sys->msg_namelen, uaddr,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002239 uaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 if (err < 0)
2241 goto out_freeiov;
2242 }
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002243 err = __put_user((msg_sys->msg_flags & ~MSG_CMSG_COMPAT),
David S. Miller37f7f422005-09-16 16:51:01 -07002244 COMPAT_FLAGS(msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 if (err)
2246 goto out_freeiov;
2247 if (MSG_CMSG_COMPAT & flags)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002248 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 &msg_compat->msg_controllen);
2250 else
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002251 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 &msg->msg_controllen);
2253 if (err)
2254 goto out_freeiov;
2255 err = len;
2256
2257out_freeiov:
2258 if (iov != iovstack)
Eric Dumazeta74e9102012-04-20 20:04:01 +02002259 kfree(iov);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002260out:
2261 return err;
2262}
2263
2264/*
2265 * BSD recvmsg interface
2266 */
2267
2268SYSCALL_DEFINE3(recvmsg, int, fd, struct msghdr __user *, msg,
2269 unsigned int, flags)
2270{
2271 int fput_needed, err;
2272 struct msghdr msg_sys;
2273 struct socket *sock = sockfd_lookup_light(fd, &err, &fput_needed);
2274
2275 if (!sock)
2276 goto out;
2277
2278 err = __sys_recvmsg(sock, msg, &msg_sys, flags, 0);
2279
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002280 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281out:
2282 return err;
2283}
2284
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002285/*
2286 * Linux recvmmsg interface
2287 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002289int __sys_recvmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
2290 unsigned int flags, struct timespec *timeout)
2291{
2292 int fput_needed, err, datagrams;
2293 struct socket *sock;
2294 struct mmsghdr __user *entry;
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002295 struct compat_mmsghdr __user *compat_entry;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002296 struct msghdr msg_sys;
2297 struct timespec end_time;
2298
2299 if (timeout &&
2300 poll_select_set_timeout(&end_time, timeout->tv_sec,
2301 timeout->tv_nsec))
2302 return -EINVAL;
2303
2304 datagrams = 0;
2305
2306 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2307 if (!sock)
2308 return err;
2309
2310 err = sock_error(sock->sk);
2311 if (err)
2312 goto out_put;
2313
2314 entry = mmsg;
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002315 compat_entry = (struct compat_mmsghdr __user *)mmsg;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002316
2317 while (datagrams < vlen) {
2318 /*
2319 * No need to ask LSM for more than the first datagram.
2320 */
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002321 if (MSG_CMSG_COMPAT & flags) {
2322 err = __sys_recvmsg(sock, (struct msghdr __user *)compat_entry,
Anton Blanchardb9eb8b82011-05-17 15:38:57 -04002323 &msg_sys, flags & ~MSG_WAITFORONE,
2324 datagrams);
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002325 if (err < 0)
2326 break;
2327 err = __put_user(err, &compat_entry->msg_len);
2328 ++compat_entry;
2329 } else {
2330 err = __sys_recvmsg(sock, (struct msghdr __user *)entry,
Anton Blanchardb9eb8b82011-05-17 15:38:57 -04002331 &msg_sys, flags & ~MSG_WAITFORONE,
2332 datagrams);
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002333 if (err < 0)
2334 break;
2335 err = put_user(err, &entry->msg_len);
2336 ++entry;
2337 }
2338
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002339 if (err)
2340 break;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002341 ++datagrams;
2342
Brandon L Black71c5c152010-03-26 16:18:03 +00002343 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
2344 if (flags & MSG_WAITFORONE)
2345 flags |= MSG_DONTWAIT;
2346
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002347 if (timeout) {
2348 ktime_get_ts(timeout);
2349 *timeout = timespec_sub(end_time, *timeout);
2350 if (timeout->tv_sec < 0) {
2351 timeout->tv_sec = timeout->tv_nsec = 0;
2352 break;
2353 }
2354
2355 /* Timeout, return less than vlen datagrams */
2356 if (timeout->tv_nsec == 0 && timeout->tv_sec == 0)
2357 break;
2358 }
2359
2360 /* Out of band data, return right away */
2361 if (msg_sys.msg_flags & MSG_OOB)
2362 break;
2363 }
2364
2365out_put:
2366 fput_light(sock->file, fput_needed);
2367
2368 if (err == 0)
2369 return datagrams;
2370
2371 if (datagrams != 0) {
2372 /*
2373 * We may return less entries than requested (vlen) if the
2374 * sock is non block and there aren't enough datagrams...
2375 */
2376 if (err != -EAGAIN) {
2377 /*
2378 * ... or if recvmsg returns an error after we
2379 * received some datagrams, where we record the
2380 * error to return on the next call or if the
2381 * app asks about it using getsockopt(SO_ERROR).
2382 */
2383 sock->sk->sk_err = -err;
2384 }
2385
2386 return datagrams;
2387 }
2388
2389 return err;
2390}
2391
2392SYSCALL_DEFINE5(recvmmsg, int, fd, struct mmsghdr __user *, mmsg,
2393 unsigned int, vlen, unsigned int, flags,
2394 struct timespec __user *, timeout)
2395{
2396 int datagrams;
2397 struct timespec timeout_sys;
2398
2399 if (!timeout)
2400 return __sys_recvmmsg(fd, mmsg, vlen, flags, NULL);
2401
2402 if (copy_from_user(&timeout_sys, timeout, sizeof(timeout_sys)))
2403 return -EFAULT;
2404
2405 datagrams = __sys_recvmmsg(fd, mmsg, vlen, flags, &timeout_sys);
2406
2407 if (datagrams > 0 &&
2408 copy_to_user(timeout, &timeout_sys, sizeof(timeout_sys)))
2409 datagrams = -EFAULT;
2410
2411 return datagrams;
2412}
2413
2414#ifdef __ARCH_WANT_SYS_SOCKETCALL
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415/* Argument list sizes for sys_socketcall */
2416#define AL(x) ((x) * sizeof(unsigned long))
Anton Blanchard228e5482011-05-02 20:21:35 +00002417static const unsigned char nargs[21] = {
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002418 AL(0), AL(3), AL(3), AL(3), AL(2), AL(3),
2419 AL(3), AL(3), AL(4), AL(4), AL(4), AL(6),
2420 AL(6), AL(2), AL(5), AL(5), AL(3), AL(3),
Anton Blanchard228e5482011-05-02 20:21:35 +00002421 AL(4), AL(5), AL(4)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002422};
2423
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424#undef AL
2425
2426/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002427 * System call vectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 *
2429 * Argument checking cleaned up. Saved 20% in size.
2430 * This function doesn't need to set the kernel lock because
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002431 * it is set by the callees.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 */
2433
Heiko Carstens3e0fa652009-01-14 14:14:24 +01002434SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435{
2436 unsigned long a[6];
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002437 unsigned long a0, a1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 int err;
Arjan van de Ven47379052009-09-28 12:57:44 -07002439 unsigned int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440
Anton Blanchard228e5482011-05-02 20:21:35 +00002441 if (call < 1 || call > SYS_SENDMMSG)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 return -EINVAL;
2443
Arjan van de Ven47379052009-09-28 12:57:44 -07002444 len = nargs[call];
2445 if (len > sizeof(a))
2446 return -EINVAL;
2447
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 /* copy_from_user should be SMP safe. */
Arjan van de Ven47379052009-09-28 12:57:44 -07002449 if (copy_from_user(a, args, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 return -EFAULT;
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002451
Al Virof3298dc2008-12-10 03:16:51 -05002452 audit_socketcall(nargs[call] / sizeof(unsigned long), a);
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002453
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002454 a0 = a[0];
2455 a1 = a[1];
2456
2457 switch (call) {
2458 case SYS_SOCKET:
2459 err = sys_socket(a0, a1, a[2]);
2460 break;
2461 case SYS_BIND:
2462 err = sys_bind(a0, (struct sockaddr __user *)a1, a[2]);
2463 break;
2464 case SYS_CONNECT:
2465 err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
2466 break;
2467 case SYS_LISTEN:
2468 err = sys_listen(a0, a1);
2469 break;
2470 case SYS_ACCEPT:
Ulrich Drepperde11def2008-11-19 15:36:14 -08002471 err = sys_accept4(a0, (struct sockaddr __user *)a1,
2472 (int __user *)a[2], 0);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002473 break;
2474 case SYS_GETSOCKNAME:
2475 err =
2476 sys_getsockname(a0, (struct sockaddr __user *)a1,
2477 (int __user *)a[2]);
2478 break;
2479 case SYS_GETPEERNAME:
2480 err =
2481 sys_getpeername(a0, (struct sockaddr __user *)a1,
2482 (int __user *)a[2]);
2483 break;
2484 case SYS_SOCKETPAIR:
2485 err = sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
2486 break;
2487 case SYS_SEND:
2488 err = sys_send(a0, (void __user *)a1, a[2], a[3]);
2489 break;
2490 case SYS_SENDTO:
2491 err = sys_sendto(a0, (void __user *)a1, a[2], a[3],
2492 (struct sockaddr __user *)a[4], a[5]);
2493 break;
2494 case SYS_RECV:
2495 err = sys_recv(a0, (void __user *)a1, a[2], a[3]);
2496 break;
2497 case SYS_RECVFROM:
2498 err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
2499 (struct sockaddr __user *)a[4],
2500 (int __user *)a[5]);
2501 break;
2502 case SYS_SHUTDOWN:
2503 err = sys_shutdown(a0, a1);
2504 break;
2505 case SYS_SETSOCKOPT:
2506 err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]);
2507 break;
2508 case SYS_GETSOCKOPT:
2509 err =
2510 sys_getsockopt(a0, a1, a[2], (char __user *)a[3],
2511 (int __user *)a[4]);
2512 break;
2513 case SYS_SENDMSG:
2514 err = sys_sendmsg(a0, (struct msghdr __user *)a1, a[2]);
2515 break;
Anton Blanchard228e5482011-05-02 20:21:35 +00002516 case SYS_SENDMMSG:
2517 err = sys_sendmmsg(a0, (struct mmsghdr __user *)a1, a[2], a[3]);
2518 break;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002519 case SYS_RECVMSG:
2520 err = sys_recvmsg(a0, (struct msghdr __user *)a1, a[2]);
2521 break;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002522 case SYS_RECVMMSG:
2523 err = sys_recvmmsg(a0, (struct mmsghdr __user *)a1, a[2], a[3],
2524 (struct timespec __user *)a[4]);
2525 break;
Ulrich Drepperde11def2008-11-19 15:36:14 -08002526 case SYS_ACCEPT4:
2527 err = sys_accept4(a0, (struct sockaddr __user *)a1,
2528 (int __user *)a[2], a[3]);
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07002529 break;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002530 default:
2531 err = -EINVAL;
2532 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 }
2534 return err;
2535}
2536
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002537#endif /* __ARCH_WANT_SYS_SOCKETCALL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002539/**
2540 * sock_register - add a socket protocol handler
2541 * @ops: description of protocol
2542 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 * This function is called by a protocol handler that wants to
2544 * advertise its address family, and have it linked into the
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002545 * socket interface. The value ops->family coresponds to the
2546 * socket system call protocol family.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 */
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002548int sock_register(const struct net_proto_family *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549{
2550 int err;
2551
2552 if (ops->family >= NPROTO) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002553 printk(KERN_CRIT "protocol %d >= NPROTO(%d)\n", ops->family,
2554 NPROTO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 return -ENOBUFS;
2556 }
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002557
2558 spin_lock(&net_family_lock);
Eric Dumazet190683a2010-11-10 10:50:44 +00002559 if (rcu_dereference_protected(net_families[ops->family],
2560 lockdep_is_held(&net_family_lock)))
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002561 err = -EEXIST;
2562 else {
Eric Dumazetcf778b02012-01-12 04:41:32 +00002563 rcu_assign_pointer(net_families[ops->family], ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 err = 0;
2565 }
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002566 spin_unlock(&net_family_lock);
2567
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002568 printk(KERN_INFO "NET: Registered protocol family %d\n", ops->family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 return err;
2570}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002571EXPORT_SYMBOL(sock_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002573/**
2574 * sock_unregister - remove a protocol handler
2575 * @family: protocol family to remove
2576 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 * This function is called by a protocol handler that wants to
2578 * remove its address family, and have it unlinked from the
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002579 * new socket creation.
2580 *
2581 * If protocol handler is a module, then it can use module reference
2582 * counts to protect against new references. If protocol handler is not
2583 * a module then it needs to provide its own protection in
2584 * the ops->create routine.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 */
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002586void sock_unregister(int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587{
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002588 BUG_ON(family < 0 || family >= NPROTO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002590 spin_lock(&net_family_lock);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00002591 RCU_INIT_POINTER(net_families[family], NULL);
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002592 spin_unlock(&net_family_lock);
2593
2594 synchronize_rcu();
2595
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002596 printk(KERN_INFO "NET: Unregistered protocol family %d\n", family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002598EXPORT_SYMBOL(sock_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599
Andi Kleen77d76ea2005-12-22 12:43:42 -08002600static int __init sock_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601{
Nick Pigginb3e19d92011-01-07 17:50:11 +11002602 int err;
Eric W. Biederman2ca794e2012-04-19 13:20:32 +00002603 /*
2604 * Initialize the network sysctl infrastructure.
2605 */
2606 err = net_sysctl_init();
2607 if (err)
2608 goto out;
Nick Pigginb3e19d92011-01-07 17:50:11 +11002609
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002611 * Initialize skbuff SLAB cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 */
2613 skb_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614
2615 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002616 * Initialize the protocols module.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 */
2618
2619 init_inodecache();
Nick Pigginb3e19d92011-01-07 17:50:11 +11002620
2621 err = register_filesystem(&sock_fs_type);
2622 if (err)
2623 goto out_fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 sock_mnt = kern_mount(&sock_fs_type);
Nick Pigginb3e19d92011-01-07 17:50:11 +11002625 if (IS_ERR(sock_mnt)) {
2626 err = PTR_ERR(sock_mnt);
2627 goto out_mount;
2628 }
Andi Kleen77d76ea2005-12-22 12:43:42 -08002629
2630 /* The real protocol initialization is performed in later initcalls.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 */
2632
2633#ifdef CONFIG_NETFILTER
2634 netfilter_init();
2635#endif
David S. Millercbeb3212005-12-22 12:58:55 -08002636
Richard Cochranc1f19b52010-07-17 08:49:36 +00002637#ifdef CONFIG_NETWORK_PHY_TIMESTAMPING
2638 skb_timestamping_init();
2639#endif
2640
Nick Pigginb3e19d92011-01-07 17:50:11 +11002641out:
2642 return err;
2643
2644out_mount:
2645 unregister_filesystem(&sock_fs_type);
2646out_fs:
2647 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648}
2649
Andi Kleen77d76ea2005-12-22 12:43:42 -08002650core_initcall(sock_init); /* early initcall */
2651
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652#ifdef CONFIG_PROC_FS
2653void socket_seq_show(struct seq_file *seq)
2654{
2655 int cpu;
2656 int counter = 0;
2657
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -07002658 for_each_possible_cpu(cpu)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002659 counter += per_cpu(sockets_in_use, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660
2661 /* It can be negative, by the way. 8) */
2662 if (counter < 0)
2663 counter = 0;
2664
2665 seq_printf(seq, "sockets: used %d\n", counter);
2666}
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002667#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668
Shaun Pereira89bbfc92006-03-21 23:58:08 -08002669#ifdef CONFIG_COMPAT
Arnd Bergmann6b960182009-11-06 23:10:54 -08002670static int do_siocgstamp(struct net *net, struct socket *sock,
H. Peter Anvin644595f2012-02-19 17:51:59 -08002671 unsigned int cmd, void __user *up)
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002672{
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002673 mm_segment_t old_fs = get_fs();
2674 struct timeval ktv;
2675 int err;
2676
2677 set_fs(KERNEL_DS);
Arnd Bergmann6b960182009-11-06 23:10:54 -08002678 err = sock_do_ioctl(net, sock, cmd, (unsigned long)&ktv);
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002679 set_fs(old_fs);
H. Peter Anvin644595f2012-02-19 17:51:59 -08002680 if (!err)
Mikulas Patockaed6fe9d2012-09-01 12:34:07 -04002681 err = compat_put_timeval(&ktv, up);
H. Peter Anvin644595f2012-02-19 17:51:59 -08002682
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002683 return err;
2684}
2685
Arnd Bergmann6b960182009-11-06 23:10:54 -08002686static int do_siocgstampns(struct net *net, struct socket *sock,
H. Peter Anvin644595f2012-02-19 17:51:59 -08002687 unsigned int cmd, void __user *up)
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002688{
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002689 mm_segment_t old_fs = get_fs();
2690 struct timespec kts;
2691 int err;
2692
2693 set_fs(KERNEL_DS);
Arnd Bergmann6b960182009-11-06 23:10:54 -08002694 err = sock_do_ioctl(net, sock, cmd, (unsigned long)&kts);
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002695 set_fs(old_fs);
H. Peter Anvin644595f2012-02-19 17:51:59 -08002696 if (!err)
Mikulas Patockaed6fe9d2012-09-01 12:34:07 -04002697 err = compat_put_timespec(&kts, up);
H. Peter Anvin644595f2012-02-19 17:51:59 -08002698
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002699 return err;
2700}
2701
Arnd Bergmann6b960182009-11-06 23:10:54 -08002702static int dev_ifname32(struct net *net, struct compat_ifreq __user *uifr32)
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002703{
2704 struct ifreq __user *uifr;
2705 int err;
2706
2707 uifr = compat_alloc_user_space(sizeof(struct ifreq));
Arnd Bergmann6b960182009-11-06 23:10:54 -08002708 if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002709 return -EFAULT;
2710
Arnd Bergmann6b960182009-11-06 23:10:54 -08002711 err = dev_ioctl(net, SIOCGIFNAME, uifr);
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002712 if (err)
2713 return err;
2714
Arnd Bergmann6b960182009-11-06 23:10:54 -08002715 if (copy_in_user(uifr32, uifr, sizeof(struct compat_ifreq)))
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002716 return -EFAULT;
2717
2718 return 0;
2719}
2720
Arnd Bergmann6b960182009-11-06 23:10:54 -08002721static int dev_ifconf(struct net *net, struct compat_ifconf __user *uifc32)
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002722{
Arnd Bergmann6b960182009-11-06 23:10:54 -08002723 struct compat_ifconf ifc32;
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002724 struct ifconf ifc;
2725 struct ifconf __user *uifc;
Arnd Bergmann6b960182009-11-06 23:10:54 -08002726 struct compat_ifreq __user *ifr32;
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002727 struct ifreq __user *ifr;
2728 unsigned int i, j;
2729 int err;
2730
Arnd Bergmann6b960182009-11-06 23:10:54 -08002731 if (copy_from_user(&ifc32, uifc32, sizeof(struct compat_ifconf)))
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002732 return -EFAULT;
2733
Mathias Krause43da5f22012-08-15 11:31:57 +00002734 memset(&ifc, 0, sizeof(ifc));
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002735 if (ifc32.ifcbuf == 0) {
2736 ifc32.ifc_len = 0;
2737 ifc.ifc_len = 0;
2738 ifc.ifc_req = NULL;
2739 uifc = compat_alloc_user_space(sizeof(struct ifconf));
2740 } else {
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002741 size_t len = ((ifc32.ifc_len / sizeof(struct compat_ifreq)) + 1) *
2742 sizeof(struct ifreq);
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002743 uifc = compat_alloc_user_space(sizeof(struct ifconf) + len);
2744 ifc.ifc_len = len;
2745 ifr = ifc.ifc_req = (void __user *)(uifc + 1);
2746 ifr32 = compat_ptr(ifc32.ifcbuf);
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002747 for (i = 0; i < ifc32.ifc_len; i += sizeof(struct compat_ifreq)) {
Arnd Bergmann6b960182009-11-06 23:10:54 -08002748 if (copy_in_user(ifr, ifr32, sizeof(struct compat_ifreq)))
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002749 return -EFAULT;
2750 ifr++;
2751 ifr32++;
2752 }
2753 }
2754 if (copy_to_user(uifc, &ifc, sizeof(struct ifconf)))
2755 return -EFAULT;
2756
Arnd Bergmann6b960182009-11-06 23:10:54 -08002757 err = dev_ioctl(net, SIOCGIFCONF, uifc);
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002758 if (err)
2759 return err;
2760
2761 if (copy_from_user(&ifc, uifc, sizeof(struct ifconf)))
2762 return -EFAULT;
2763
2764 ifr = ifc.ifc_req;
2765 ifr32 = compat_ptr(ifc32.ifcbuf);
2766 for (i = 0, j = 0;
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002767 i + sizeof(struct compat_ifreq) <= ifc32.ifc_len && j < ifc.ifc_len;
2768 i += sizeof(struct compat_ifreq), j += sizeof(struct ifreq)) {
2769 if (copy_in_user(ifr32, ifr, sizeof(struct compat_ifreq)))
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002770 return -EFAULT;
2771 ifr32++;
2772 ifr++;
2773 }
2774
2775 if (ifc32.ifcbuf == 0) {
2776 /* Translate from 64-bit structure multiple to
2777 * a 32-bit one.
2778 */
2779 i = ifc.ifc_len;
Arnd Bergmann6b960182009-11-06 23:10:54 -08002780 i = ((i / sizeof(struct ifreq)) * sizeof(struct compat_ifreq));
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002781 ifc32.ifc_len = i;
2782 } else {
2783 ifc32.ifc_len = i;
2784 }
Arnd Bergmann6b960182009-11-06 23:10:54 -08002785 if (copy_to_user(uifc32, &ifc32, sizeof(struct compat_ifconf)))
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002786 return -EFAULT;
2787
2788 return 0;
2789}
2790
Arnd Bergmann6b960182009-11-06 23:10:54 -08002791static int ethtool_ioctl(struct net *net, struct compat_ifreq __user *ifr32)
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002792{
Ben Hutchings3a7da392011-03-17 07:34:32 +00002793 struct compat_ethtool_rxnfc __user *compat_rxnfc;
2794 bool convert_in = false, convert_out = false;
2795 size_t buf_size = ALIGN(sizeof(struct ifreq), 8);
2796 struct ethtool_rxnfc __user *rxnfc;
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002797 struct ifreq __user *ifr;
Ben Hutchings3a7da392011-03-17 07:34:32 +00002798 u32 rule_cnt = 0, actual_rule_cnt;
2799 u32 ethcmd;
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002800 u32 data;
Ben Hutchings3a7da392011-03-17 07:34:32 +00002801 int ret;
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002802
2803 if (get_user(data, &ifr32->ifr_ifru.ifru_data))
2804 return -EFAULT;
2805
Ben Hutchings3a7da392011-03-17 07:34:32 +00002806 compat_rxnfc = compat_ptr(data);
2807
2808 if (get_user(ethcmd, &compat_rxnfc->cmd))
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002809 return -EFAULT;
2810
Ben Hutchings3a7da392011-03-17 07:34:32 +00002811 /* Most ethtool structures are defined without padding.
2812 * Unfortunately struct ethtool_rxnfc is an exception.
2813 */
2814 switch (ethcmd) {
2815 default:
2816 break;
2817 case ETHTOOL_GRXCLSRLALL:
2818 /* Buffer size is variable */
2819 if (get_user(rule_cnt, &compat_rxnfc->rule_cnt))
2820 return -EFAULT;
2821 if (rule_cnt > KMALLOC_MAX_SIZE / sizeof(u32))
2822 return -ENOMEM;
2823 buf_size += rule_cnt * sizeof(u32);
2824 /* fall through */
2825 case ETHTOOL_GRXRINGS:
2826 case ETHTOOL_GRXCLSRLCNT:
2827 case ETHTOOL_GRXCLSRULE:
Ben Hutchings55664f32012-01-03 12:04:51 +00002828 case ETHTOOL_SRXCLSRLINS:
Ben Hutchings3a7da392011-03-17 07:34:32 +00002829 convert_out = true;
2830 /* fall through */
2831 case ETHTOOL_SRXCLSRLDEL:
Ben Hutchings3a7da392011-03-17 07:34:32 +00002832 buf_size += sizeof(struct ethtool_rxnfc);
2833 convert_in = true;
2834 break;
2835 }
2836
2837 ifr = compat_alloc_user_space(buf_size);
Stephen Hemminger954b1242013-02-11 06:22:28 +00002838 rxnfc = (void __user *)ifr + ALIGN(sizeof(struct ifreq), 8);
Ben Hutchings3a7da392011-03-17 07:34:32 +00002839
2840 if (copy_in_user(&ifr->ifr_name, &ifr32->ifr_name, IFNAMSIZ))
2841 return -EFAULT;
2842
2843 if (put_user(convert_in ? rxnfc : compat_ptr(data),
2844 &ifr->ifr_ifru.ifru_data))
2845 return -EFAULT;
2846
2847 if (convert_in) {
Alexander Duyck127fe532011-04-08 18:01:59 +00002848 /* We expect there to be holes between fs.m_ext and
Ben Hutchings3a7da392011-03-17 07:34:32 +00002849 * fs.ring_cookie and at the end of fs, but nowhere else.
2850 */
Alexander Duyck127fe532011-04-08 18:01:59 +00002851 BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc, fs.m_ext) +
2852 sizeof(compat_rxnfc->fs.m_ext) !=
2853 offsetof(struct ethtool_rxnfc, fs.m_ext) +
2854 sizeof(rxnfc->fs.m_ext));
Ben Hutchings3a7da392011-03-17 07:34:32 +00002855 BUILD_BUG_ON(
2856 offsetof(struct compat_ethtool_rxnfc, fs.location) -
2857 offsetof(struct compat_ethtool_rxnfc, fs.ring_cookie) !=
2858 offsetof(struct ethtool_rxnfc, fs.location) -
2859 offsetof(struct ethtool_rxnfc, fs.ring_cookie));
2860
2861 if (copy_in_user(rxnfc, compat_rxnfc,
Stephen Hemminger954b1242013-02-11 06:22:28 +00002862 (void __user *)(&rxnfc->fs.m_ext + 1) -
2863 (void __user *)rxnfc) ||
Ben Hutchings3a7da392011-03-17 07:34:32 +00002864 copy_in_user(&rxnfc->fs.ring_cookie,
2865 &compat_rxnfc->fs.ring_cookie,
Stephen Hemminger954b1242013-02-11 06:22:28 +00002866 (void __user *)(&rxnfc->fs.location + 1) -
2867 (void __user *)&rxnfc->fs.ring_cookie) ||
Ben Hutchings3a7da392011-03-17 07:34:32 +00002868 copy_in_user(&rxnfc->rule_cnt, &compat_rxnfc->rule_cnt,
2869 sizeof(rxnfc->rule_cnt)))
2870 return -EFAULT;
2871 }
2872
2873 ret = dev_ioctl(net, SIOCETHTOOL, ifr);
2874 if (ret)
2875 return ret;
2876
2877 if (convert_out) {
2878 if (copy_in_user(compat_rxnfc, rxnfc,
Stephen Hemminger954b1242013-02-11 06:22:28 +00002879 (const void __user *)(&rxnfc->fs.m_ext + 1) -
2880 (const void __user *)rxnfc) ||
Ben Hutchings3a7da392011-03-17 07:34:32 +00002881 copy_in_user(&compat_rxnfc->fs.ring_cookie,
2882 &rxnfc->fs.ring_cookie,
Stephen Hemminger954b1242013-02-11 06:22:28 +00002883 (const void __user *)(&rxnfc->fs.location + 1) -
2884 (const void __user *)&rxnfc->fs.ring_cookie) ||
Ben Hutchings3a7da392011-03-17 07:34:32 +00002885 copy_in_user(&compat_rxnfc->rule_cnt, &rxnfc->rule_cnt,
2886 sizeof(rxnfc->rule_cnt)))
2887 return -EFAULT;
2888
2889 if (ethcmd == ETHTOOL_GRXCLSRLALL) {
2890 /* As an optimisation, we only copy the actual
2891 * number of rules that the underlying
2892 * function returned. Since Mallory might
2893 * change the rule count in user memory, we
2894 * check that it is less than the rule count
2895 * originally given (as the user buffer size),
2896 * which has been range-checked.
2897 */
2898 if (get_user(actual_rule_cnt, &rxnfc->rule_cnt))
2899 return -EFAULT;
2900 if (actual_rule_cnt < rule_cnt)
2901 rule_cnt = actual_rule_cnt;
2902 if (copy_in_user(&compat_rxnfc->rule_locs[0],
2903 &rxnfc->rule_locs[0],
2904 rule_cnt * sizeof(u32)))
2905 return -EFAULT;
2906 }
2907 }
2908
2909 return 0;
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002910}
2911
Arnd Bergmann7a50a242009-11-08 20:57:03 -08002912static int compat_siocwandev(struct net *net, struct compat_ifreq __user *uifr32)
2913{
2914 void __user *uptr;
2915 compat_uptr_t uptr32;
2916 struct ifreq __user *uifr;
2917
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002918 uifr = compat_alloc_user_space(sizeof(*uifr));
Arnd Bergmann7a50a242009-11-08 20:57:03 -08002919 if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
2920 return -EFAULT;
2921
2922 if (get_user(uptr32, &uifr32->ifr_settings.ifs_ifsu))
2923 return -EFAULT;
2924
2925 uptr = compat_ptr(uptr32);
2926
2927 if (put_user(uptr, &uifr->ifr_settings.ifs_ifsu.raw_hdlc))
2928 return -EFAULT;
2929
2930 return dev_ioctl(net, SIOCWANDEV, uifr);
2931}
2932
Arnd Bergmann6b960182009-11-06 23:10:54 -08002933static int bond_ioctl(struct net *net, unsigned int cmd,
2934 struct compat_ifreq __user *ifr32)
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002935{
2936 struct ifreq kifr;
2937 struct ifreq __user *uifr;
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002938 mm_segment_t old_fs;
2939 int err;
2940 u32 data;
2941 void __user *datap;
2942
2943 switch (cmd) {
2944 case SIOCBONDENSLAVE:
2945 case SIOCBONDRELEASE:
2946 case SIOCBONDSETHWADDR:
2947 case SIOCBONDCHANGEACTIVE:
Arnd Bergmann6b960182009-11-06 23:10:54 -08002948 if (copy_from_user(&kifr, ifr32, sizeof(struct compat_ifreq)))
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002949 return -EFAULT;
2950
2951 old_fs = get_fs();
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002952 set_fs(KERNEL_DS);
stephen hemmingerc3f52ae2011-02-23 09:06:48 +00002953 err = dev_ioctl(net, cmd,
2954 (struct ifreq __user __force *) &kifr);
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002955 set_fs(old_fs);
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002956
2957 return err;
2958 case SIOCBONDSLAVEINFOQUERY:
2959 case SIOCBONDINFOQUERY:
2960 uifr = compat_alloc_user_space(sizeof(*uifr));
2961 if (copy_in_user(&uifr->ifr_name, &ifr32->ifr_name, IFNAMSIZ))
2962 return -EFAULT;
2963
2964 if (get_user(data, &ifr32->ifr_ifru.ifru_data))
2965 return -EFAULT;
2966
2967 datap = compat_ptr(data);
2968 if (put_user(datap, &uifr->ifr_ifru.ifru_data))
2969 return -EFAULT;
2970
Arnd Bergmann6b960182009-11-06 23:10:54 -08002971 return dev_ioctl(net, cmd, uifr);
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002972 default:
Linus Torvalds07d106d2012-01-05 15:40:12 -08002973 return -ENOIOCTLCMD;
Joe Perchesccbd6a52010-05-14 10:58:26 +00002974 }
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002975}
2976
Arnd Bergmann6b960182009-11-06 23:10:54 -08002977static int siocdevprivate_ioctl(struct net *net, unsigned int cmd,
2978 struct compat_ifreq __user *u_ifreq32)
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002979{
2980 struct ifreq __user *u_ifreq64;
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002981 char tmp_buf[IFNAMSIZ];
2982 void __user *data64;
2983 u32 data32;
2984
2985 if (copy_from_user(&tmp_buf[0], &(u_ifreq32->ifr_ifrn.ifrn_name[0]),
2986 IFNAMSIZ))
2987 return -EFAULT;
2988 if (__get_user(data32, &u_ifreq32->ifr_ifru.ifru_data))
2989 return -EFAULT;
2990 data64 = compat_ptr(data32);
2991
2992 u_ifreq64 = compat_alloc_user_space(sizeof(*u_ifreq64));
2993
2994 /* Don't check these user accesses, just let that get trapped
2995 * in the ioctl handler instead.
2996 */
2997 if (copy_to_user(&u_ifreq64->ifr_ifrn.ifrn_name[0], &tmp_buf[0],
2998 IFNAMSIZ))
2999 return -EFAULT;
3000 if (__put_user(data64, &u_ifreq64->ifr_ifru.ifru_data))
3001 return -EFAULT;
3002
Arnd Bergmann6b960182009-11-06 23:10:54 -08003003 return dev_ioctl(net, cmd, u_ifreq64);
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003004}
3005
Arnd Bergmann6b960182009-11-06 23:10:54 -08003006static int dev_ifsioc(struct net *net, struct socket *sock,
3007 unsigned int cmd, struct compat_ifreq __user *uifr32)
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003008{
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003009 struct ifreq __user *uifr;
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003010 int err;
3011
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003012 uifr = compat_alloc_user_space(sizeof(*uifr));
3013 if (copy_in_user(uifr, uifr32, sizeof(*uifr32)))
3014 return -EFAULT;
3015
3016 err = sock_do_ioctl(net, sock, cmd, (unsigned long)uifr);
3017
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003018 if (!err) {
3019 switch (cmd) {
3020 case SIOCGIFFLAGS:
3021 case SIOCGIFMETRIC:
3022 case SIOCGIFMTU:
3023 case SIOCGIFMEM:
3024 case SIOCGIFHWADDR:
3025 case SIOCGIFINDEX:
3026 case SIOCGIFADDR:
3027 case SIOCGIFBRDADDR:
3028 case SIOCGIFDSTADDR:
3029 case SIOCGIFNETMASK:
Arnd Bergmannfab25322009-11-08 20:56:21 -08003030 case SIOCGIFPFLAGS:
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003031 case SIOCGIFTXQLEN:
Arnd Bergmannfab25322009-11-08 20:56:21 -08003032 case SIOCGMIIPHY:
3033 case SIOCGMIIREG:
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003034 if (copy_in_user(uifr32, uifr, sizeof(*uifr32)))
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003035 err = -EFAULT;
3036 break;
3037 }
3038 }
3039 return err;
3040}
3041
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003042static int compat_sioc_ifmap(struct net *net, unsigned int cmd,
3043 struct compat_ifreq __user *uifr32)
3044{
3045 struct ifreq ifr;
3046 struct compat_ifmap __user *uifmap32;
3047 mm_segment_t old_fs;
3048 int err;
3049
3050 uifmap32 = &uifr32->ifr_ifru.ifru_map;
3051 err = copy_from_user(&ifr, uifr32, sizeof(ifr.ifr_name));
3052 err |= __get_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
3053 err |= __get_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
3054 err |= __get_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
3055 err |= __get_user(ifr.ifr_map.irq, &uifmap32->irq);
3056 err |= __get_user(ifr.ifr_map.dma, &uifmap32->dma);
3057 err |= __get_user(ifr.ifr_map.port, &uifmap32->port);
3058 if (err)
3059 return -EFAULT;
3060
3061 old_fs = get_fs();
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003062 set_fs(KERNEL_DS);
stephen hemmingerc3f52ae2011-02-23 09:06:48 +00003063 err = dev_ioctl(net, cmd, (void __user __force *)&ifr);
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003064 set_fs(old_fs);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003065
3066 if (cmd == SIOCGIFMAP && !err) {
3067 err = copy_to_user(uifr32, &ifr, sizeof(ifr.ifr_name));
3068 err |= __put_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
3069 err |= __put_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
3070 err |= __put_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
3071 err |= __put_user(ifr.ifr_map.irq, &uifmap32->irq);
3072 err |= __put_user(ifr.ifr_map.dma, &uifmap32->dma);
3073 err |= __put_user(ifr.ifr_map.port, &uifmap32->port);
3074 if (err)
3075 err = -EFAULT;
3076 }
3077 return err;
3078}
3079
3080static int compat_siocshwtstamp(struct net *net, struct compat_ifreq __user *uifr32)
3081{
3082 void __user *uptr;
3083 compat_uptr_t uptr32;
3084 struct ifreq __user *uifr;
3085
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003086 uifr = compat_alloc_user_space(sizeof(*uifr));
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003087 if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
3088 return -EFAULT;
3089
3090 if (get_user(uptr32, &uifr32->ifr_data))
3091 return -EFAULT;
3092
3093 uptr = compat_ptr(uptr32);
3094
3095 if (put_user(uptr, &uifr->ifr_data))
3096 return -EFAULT;
3097
3098 return dev_ioctl(net, SIOCSHWTSTAMP, uifr);
3099}
3100
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003101struct rtentry32 {
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003102 u32 rt_pad1;
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003103 struct sockaddr rt_dst; /* target address */
3104 struct sockaddr rt_gateway; /* gateway addr (RTF_GATEWAY) */
3105 struct sockaddr rt_genmask; /* target network mask (IP) */
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003106 unsigned short rt_flags;
3107 short rt_pad2;
3108 u32 rt_pad3;
3109 unsigned char rt_tos;
3110 unsigned char rt_class;
3111 short rt_pad4;
3112 short rt_metric; /* +1 for binary compatibility! */
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003113 /* char * */ u32 rt_dev; /* forcing the device at add */
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003114 u32 rt_mtu; /* per route MTU/Window */
3115 u32 rt_window; /* Window clamping */
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003116 unsigned short rt_irtt; /* Initial RTT */
3117};
3118
3119struct in6_rtmsg32 {
3120 struct in6_addr rtmsg_dst;
3121 struct in6_addr rtmsg_src;
3122 struct in6_addr rtmsg_gateway;
3123 u32 rtmsg_type;
3124 u16 rtmsg_dst_len;
3125 u16 rtmsg_src_len;
3126 u32 rtmsg_metric;
3127 u32 rtmsg_info;
3128 u32 rtmsg_flags;
3129 s32 rtmsg_ifindex;
3130};
3131
Arnd Bergmann6b960182009-11-06 23:10:54 -08003132static int routing_ioctl(struct net *net, struct socket *sock,
3133 unsigned int cmd, void __user *argp)
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003134{
3135 int ret;
3136 void *r = NULL;
3137 struct in6_rtmsg r6;
3138 struct rtentry r4;
3139 char devname[16];
3140 u32 rtdev;
3141 mm_segment_t old_fs = get_fs();
3142
Arnd Bergmann6b960182009-11-06 23:10:54 -08003143 if (sock && sock->sk && sock->sk->sk_family == AF_INET6) { /* ipv6 */
3144 struct in6_rtmsg32 __user *ur6 = argp;
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003145 ret = copy_from_user(&r6.rtmsg_dst, &(ur6->rtmsg_dst),
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003146 3 * sizeof(struct in6_addr));
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003147 ret |= __get_user(r6.rtmsg_type, &(ur6->rtmsg_type));
3148 ret |= __get_user(r6.rtmsg_dst_len, &(ur6->rtmsg_dst_len));
3149 ret |= __get_user(r6.rtmsg_src_len, &(ur6->rtmsg_src_len));
3150 ret |= __get_user(r6.rtmsg_metric, &(ur6->rtmsg_metric));
3151 ret |= __get_user(r6.rtmsg_info, &(ur6->rtmsg_info));
3152 ret |= __get_user(r6.rtmsg_flags, &(ur6->rtmsg_flags));
3153 ret |= __get_user(r6.rtmsg_ifindex, &(ur6->rtmsg_ifindex));
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003154
3155 r = (void *) &r6;
3156 } else { /* ipv4 */
Arnd Bergmann6b960182009-11-06 23:10:54 -08003157 struct rtentry32 __user *ur4 = argp;
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003158 ret = copy_from_user(&r4.rt_dst, &(ur4->rt_dst),
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003159 3 * sizeof(struct sockaddr));
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003160 ret |= __get_user(r4.rt_flags, &(ur4->rt_flags));
3161 ret |= __get_user(r4.rt_metric, &(ur4->rt_metric));
3162 ret |= __get_user(r4.rt_mtu, &(ur4->rt_mtu));
3163 ret |= __get_user(r4.rt_window, &(ur4->rt_window));
3164 ret |= __get_user(r4.rt_irtt, &(ur4->rt_irtt));
3165 ret |= __get_user(rtdev, &(ur4->rt_dev));
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003166 if (rtdev) {
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003167 ret |= copy_from_user(devname, compat_ptr(rtdev), 15);
stephen hemmingerc3f52ae2011-02-23 09:06:48 +00003168 r4.rt_dev = (char __user __force *)devname;
3169 devname[15] = 0;
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003170 } else
3171 r4.rt_dev = NULL;
3172
3173 r = (void *) &r4;
3174 }
3175
3176 if (ret) {
3177 ret = -EFAULT;
3178 goto out;
3179 }
3180
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003181 set_fs(KERNEL_DS);
Arnd Bergmann6b960182009-11-06 23:10:54 -08003182 ret = sock_do_ioctl(net, sock, cmd, (unsigned long) r);
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003183 set_fs(old_fs);
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003184
3185out:
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003186 return ret;
3187}
3188
3189/* Since old style bridge ioctl's endup using SIOCDEVPRIVATE
3190 * for some operations; this forces use of the newer bridge-utils that
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003191 * use compatible ioctls
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003192 */
Arnd Bergmann6b960182009-11-06 23:10:54 -08003193static int old_bridge_ioctl(compat_ulong_t __user *argp)
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003194{
Arnd Bergmann6b960182009-11-06 23:10:54 -08003195 compat_ulong_t tmp;
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003196
Arnd Bergmann6b960182009-11-06 23:10:54 -08003197 if (get_user(tmp, argp))
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003198 return -EFAULT;
3199 if (tmp == BRCTL_GET_VERSION)
3200 return BRCTL_VERSION + 1;
3201 return -EINVAL;
3202}
3203
Arnd Bergmann6b960182009-11-06 23:10:54 -08003204static int compat_sock_ioctl_trans(struct file *file, struct socket *sock,
3205 unsigned int cmd, unsigned long arg)
3206{
3207 void __user *argp = compat_ptr(arg);
3208 struct sock *sk = sock->sk;
3209 struct net *net = sock_net(sk);
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003210
Arnd Bergmann6b960182009-11-06 23:10:54 -08003211 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15))
3212 return siocdevprivate_ioctl(net, cmd, argp);
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003213
Arnd Bergmann6b960182009-11-06 23:10:54 -08003214 switch (cmd) {
3215 case SIOCSIFBR:
3216 case SIOCGIFBR:
3217 return old_bridge_ioctl(argp);
3218 case SIOCGIFNAME:
3219 return dev_ifname32(net, argp);
3220 case SIOCGIFCONF:
3221 return dev_ifconf(net, argp);
3222 case SIOCETHTOOL:
3223 return ethtool_ioctl(net, argp);
Arnd Bergmann7a50a242009-11-08 20:57:03 -08003224 case SIOCWANDEV:
3225 return compat_siocwandev(net, argp);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003226 case SIOCGIFMAP:
3227 case SIOCSIFMAP:
3228 return compat_sioc_ifmap(net, cmd, argp);
Arnd Bergmann6b960182009-11-06 23:10:54 -08003229 case SIOCBONDENSLAVE:
3230 case SIOCBONDRELEASE:
3231 case SIOCBONDSETHWADDR:
3232 case SIOCBONDSLAVEINFOQUERY:
3233 case SIOCBONDINFOQUERY:
3234 case SIOCBONDCHANGEACTIVE:
3235 return bond_ioctl(net, cmd, argp);
3236 case SIOCADDRT:
3237 case SIOCDELRT:
3238 return routing_ioctl(net, sock, cmd, argp);
3239 case SIOCGSTAMP:
3240 return do_siocgstamp(net, sock, cmd, argp);
3241 case SIOCGSTAMPNS:
3242 return do_siocgstampns(net, sock, cmd, argp);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003243 case SIOCSHWTSTAMP:
3244 return compat_siocshwtstamp(net, argp);
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003245
Arnd Bergmann6b960182009-11-06 23:10:54 -08003246 case FIOSETOWN:
3247 case SIOCSPGRP:
3248 case FIOGETOWN:
3249 case SIOCGPGRP:
3250 case SIOCBRADDBR:
3251 case SIOCBRDELBR:
3252 case SIOCGIFVLAN:
3253 case SIOCSIFVLAN:
3254 case SIOCADDDLCI:
3255 case SIOCDELDLCI:
3256 return sock_ioctl(file, cmd, arg);
3257
3258 case SIOCGIFFLAGS:
3259 case SIOCSIFFLAGS:
3260 case SIOCGIFMETRIC:
3261 case SIOCSIFMETRIC:
3262 case SIOCGIFMTU:
3263 case SIOCSIFMTU:
3264 case SIOCGIFMEM:
3265 case SIOCSIFMEM:
3266 case SIOCGIFHWADDR:
3267 case SIOCSIFHWADDR:
3268 case SIOCADDMULTI:
3269 case SIOCDELMULTI:
3270 case SIOCGIFINDEX:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003271 case SIOCGIFADDR:
3272 case SIOCSIFADDR:
3273 case SIOCSIFHWBROADCAST:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003274 case SIOCDIFADDR:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003275 case SIOCGIFBRDADDR:
3276 case SIOCSIFBRDADDR:
3277 case SIOCGIFDSTADDR:
3278 case SIOCSIFDSTADDR:
3279 case SIOCGIFNETMASK:
3280 case SIOCSIFNETMASK:
3281 case SIOCSIFPFLAGS:
3282 case SIOCGIFPFLAGS:
3283 case SIOCGIFTXQLEN:
3284 case SIOCSIFTXQLEN:
3285 case SIOCBRADDIF:
3286 case SIOCBRDELIF:
Arnd Bergmann9177efd2009-11-06 08:09:09 +00003287 case SIOCSIFNAME:
3288 case SIOCGMIIPHY:
3289 case SIOCGMIIREG:
3290 case SIOCSMIIREG:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003291 return dev_ifsioc(net, sock, cmd, argp);
Arnd Bergmann9177efd2009-11-06 08:09:09 +00003292
Arnd Bergmann6b960182009-11-06 23:10:54 -08003293 case SIOCSARP:
3294 case SIOCGARP:
3295 case SIOCDARP:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003296 case SIOCATMARK:
Arnd Bergmann9177efd2009-11-06 08:09:09 +00003297 return sock_do_ioctl(net, sock, cmd, arg);
3298 }
3299
Arnd Bergmann6b960182009-11-06 23:10:54 -08003300 return -ENOIOCTLCMD;
3301}
Arnd Bergmann7a2293872009-11-06 23:00:29 -08003302
Eric Dumazet95c96172012-04-15 05:58:06 +00003303static long compat_sock_ioctl(struct file *file, unsigned int cmd,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07003304 unsigned long arg)
Shaun Pereira89bbfc92006-03-21 23:58:08 -08003305{
3306 struct socket *sock = file->private_data;
3307 int ret = -ENOIOCTLCMD;
David S. Miller87de87d2008-06-03 09:14:03 -07003308 struct sock *sk;
3309 struct net *net;
3310
3311 sk = sock->sk;
3312 net = sock_net(sk);
Shaun Pereira89bbfc92006-03-21 23:58:08 -08003313
3314 if (sock->ops->compat_ioctl)
3315 ret = sock->ops->compat_ioctl(sock, cmd, arg);
3316
David S. Miller87de87d2008-06-03 09:14:03 -07003317 if (ret == -ENOIOCTLCMD &&
3318 (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST))
3319 ret = compat_wext_handle_ioctl(net, cmd, arg);
3320
Arnd Bergmann6b960182009-11-06 23:10:54 -08003321 if (ret == -ENOIOCTLCMD)
3322 ret = compat_sock_ioctl_trans(file, sock, cmd, arg);
3323
Shaun Pereira89bbfc92006-03-21 23:58:08 -08003324 return ret;
3325}
3326#endif
3327
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003328int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen)
3329{
3330 return sock->ops->bind(sock, addr, addrlen);
3331}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003332EXPORT_SYMBOL(kernel_bind);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003333
3334int kernel_listen(struct socket *sock, int backlog)
3335{
3336 return sock->ops->listen(sock, backlog);
3337}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003338EXPORT_SYMBOL(kernel_listen);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003339
3340int kernel_accept(struct socket *sock, struct socket **newsock, int flags)
3341{
3342 struct sock *sk = sock->sk;
3343 int err;
3344
3345 err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
3346 newsock);
3347 if (err < 0)
3348 goto done;
3349
3350 err = sock->ops->accept(sock, *newsock, flags);
3351 if (err < 0) {
3352 sock_release(*newsock);
Tony Battersbyfa8705b2007-10-10 21:09:04 -07003353 *newsock = NULL;
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003354 goto done;
3355 }
3356
3357 (*newsock)->ops = sock->ops;
Wei Yongjun1b085342008-12-18 19:35:10 -08003358 __module_get((*newsock)->ops->owner);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003359
3360done:
3361 return err;
3362}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003363EXPORT_SYMBOL(kernel_accept);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003364
3365int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
YOSHIFUJI Hideaki4768fbc2007-02-09 23:25:31 +09003366 int flags)
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003367{
3368 return sock->ops->connect(sock, addr, addrlen, flags);
3369}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003370EXPORT_SYMBOL(kernel_connect);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003371
3372int kernel_getsockname(struct socket *sock, struct sockaddr *addr,
3373 int *addrlen)
3374{
3375 return sock->ops->getname(sock, addr, addrlen, 0);
3376}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003377EXPORT_SYMBOL(kernel_getsockname);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003378
3379int kernel_getpeername(struct socket *sock, struct sockaddr *addr,
3380 int *addrlen)
3381{
3382 return sock->ops->getname(sock, addr, addrlen, 1);
3383}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003384EXPORT_SYMBOL(kernel_getpeername);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003385
3386int kernel_getsockopt(struct socket *sock, int level, int optname,
3387 char *optval, int *optlen)
3388{
3389 mm_segment_t oldfs = get_fs();
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003390 char __user *uoptval;
3391 int __user *uoptlen;
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003392 int err;
3393
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003394 uoptval = (char __user __force *) optval;
3395 uoptlen = (int __user __force *) optlen;
3396
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003397 set_fs(KERNEL_DS);
3398 if (level == SOL_SOCKET)
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003399 err = sock_getsockopt(sock, level, optname, uoptval, uoptlen);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003400 else
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003401 err = sock->ops->getsockopt(sock, level, optname, uoptval,
3402 uoptlen);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003403 set_fs(oldfs);
3404 return err;
3405}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003406EXPORT_SYMBOL(kernel_getsockopt);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003407
3408int kernel_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07003409 char *optval, unsigned int optlen)
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003410{
3411 mm_segment_t oldfs = get_fs();
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003412 char __user *uoptval;
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003413 int err;
3414
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003415 uoptval = (char __user __force *) optval;
3416
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003417 set_fs(KERNEL_DS);
3418 if (level == SOL_SOCKET)
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003419 err = sock_setsockopt(sock, level, optname, uoptval, optlen);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003420 else
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003421 err = sock->ops->setsockopt(sock, level, optname, uoptval,
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003422 optlen);
3423 set_fs(oldfs);
3424 return err;
3425}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003426EXPORT_SYMBOL(kernel_setsockopt);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003427
3428int kernel_sendpage(struct socket *sock, struct page *page, int offset,
3429 size_t size, int flags)
3430{
3431 if (sock->ops->sendpage)
3432 return sock->ops->sendpage(sock, page, offset, size, flags);
3433
3434 return sock_no_sendpage(sock, page, offset, size, flags);
3435}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003436EXPORT_SYMBOL(kernel_sendpage);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003437
3438int kernel_sock_ioctl(struct socket *sock, int cmd, unsigned long arg)
3439{
3440 mm_segment_t oldfs = get_fs();
3441 int err;
3442
3443 set_fs(KERNEL_DS);
3444 err = sock->ops->ioctl(sock, cmd, arg);
3445 set_fs(oldfs);
3446
3447 return err;
3448}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003449EXPORT_SYMBOL(kernel_sock_ioctl);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003450
Trond Myklebust91cf45f2007-11-12 18:10:39 -08003451int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how)
3452{
3453 return sock->ops->shutdown(sock, how);
3454}
Trond Myklebust91cf45f2007-11-12 18:10:39 -08003455EXPORT_SYMBOL(kernel_sock_shutdown);