blob: acfa1738663d66faf7f273164ad130a089d1fc9a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * NET An implementation of the SOCKET network access protocol.
3 *
4 * Version: @(#)socket.c 1.1.93 18/02/95
5 *
6 * Authors: Orest Zborowski, <obz@Kodak.COM>
Jesper Juhl02c30a82005-05-05 16:16:16 -07007 * Ross Biro
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
9 *
10 * Fixes:
11 * Anonymous : NOTSOCK/BADF cleanup. Error fix in
12 * shutdown()
13 * Alan Cox : verify_area() fixes
14 * Alan Cox : Removed DDI
15 * Jonathan Kamens : SOCK_DGRAM reconnect bug
16 * Alan Cox : Moved a load of checks to the very
17 * top level.
18 * Alan Cox : Move address structures to/from user
19 * mode above the protocol layers.
20 * Rob Janssen : Allow 0 length sends.
21 * Alan Cox : Asynchronous I/O support (cribbed from the
22 * tty drivers).
23 * Niibe Yutaka : Asynchronous I/O for writes (4.4BSD style)
24 * Jeff Uphoff : Made max number of sockets command-line
25 * configurable.
26 * Matti Aarnio : Made the number of sockets dynamic,
27 * to be allocated when needed, and mr.
28 * Uphoff's max is used as max to be
29 * allowed to allocate.
30 * Linus : Argh. removed all the socket allocation
31 * altogether: it's in the inode now.
32 * Alan Cox : Made sock_alloc()/sock_release() public
33 * for NetROM and future kernel nfsd type
34 * stuff.
35 * Alan Cox : sendmsg/recvmsg basics.
36 * Tom Dyas : Export net symbols.
37 * Marcin Dalecki : Fixed problems with CONFIG_NET="n".
38 * Alan Cox : Added thread locking to sys_* calls
39 * for sockets. May have errors at the
40 * moment.
41 * Kevin Buhr : Fixed the dumb errors in the above.
42 * Andi Kleen : Some small cleanups, optimizations,
43 * and fixed a copy_from_user() bug.
44 * Tigran Aivazian : sys_send(args) calls sys_sendto(args, NULL, 0)
Stephen Hemminger89bddce2006-09-01 00:19:31 -070045 * Tigran Aivazian : Made listen(2) backlog sanity checks
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 * protocol-independent
47 *
48 *
49 * This program is free software; you can redistribute it and/or
50 * modify it under the terms of the GNU General Public License
51 * as published by the Free Software Foundation; either version
52 * 2 of the License, or (at your option) any later version.
53 *
54 *
55 * This module is effectively the top level interface to the BSD socket
Stephen Hemminger89bddce2006-09-01 00:19:31 -070056 * paradigm.
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 *
58 * Based upon Swansea University Computer Society NET3.039
59 */
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <linux/socket.h>
63#include <linux/file.h>
64#include <linux/net.h>
65#include <linux/interrupt.h>
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -070066#include <linux/thread_info.h>
Stephen Hemminger55737fd2006-09-01 00:23:39 -070067#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/netdevice.h>
69#include <linux/proc_fs.h>
70#include <linux/seq_file.h>
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080071#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include <linux/wanrouter.h>
73#include <linux/if_bridge.h>
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -030074#include <linux/if_frad.h>
75#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#include <linux/init.h>
77#include <linux/poll.h>
78#include <linux/cache.h>
79#include <linux/module.h>
80#include <linux/highmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#include <linux/mount.h>
82#include <linux/security.h>
83#include <linux/syscalls.h>
84#include <linux/compat.h>
85#include <linux/kmod.h>
David Woodhouse3ec3b2f2005-05-17 12:08:48 +010086#include <linux/audit.h>
Adrian Bunkd86b5e02006-01-21 00:46:55 +010087#include <linux/wireless.h>
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -070088#include <linux/nsproxy.h>
Nick Black1fd7317d2009-09-22 16:43:33 -070089#include <linux/magic.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090090#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92#include <asm/uaccess.h>
93#include <asm/unistd.h>
94
95#include <net/compat.h>
David S. Miller87de87d2008-06-03 09:14:03 -070096#include <net/wext.h>
Herbert Xuf8451722010-05-24 00:12:34 -070097#include <net/cls_cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99#include <net/sock.h>
100#include <linux/netfilter.h>
101
Arnd Bergmann6b960182009-11-06 23:10:54 -0800102#include <linux/if_tun.h>
103#include <linux/ipv6_route.h>
104#include <linux/route.h>
Arnd Bergmann6b960182009-11-06 23:10:54 -0800105#include <linux/sockios.h>
106#include <linux/atalk.h>
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108static int sock_no_open(struct inode *irrelevant, struct file *dontcare);
Badari Pulavarty027445c2006-09-30 23:28:46 -0700109static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov,
110 unsigned long nr_segs, loff_t pos);
111static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov,
112 unsigned long nr_segs, loff_t pos);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700113static int sock_mmap(struct file *file, struct vm_area_struct *vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115static int sock_close(struct inode *inode, struct file *file);
116static unsigned int sock_poll(struct file *file,
117 struct poll_table_struct *wait);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700118static long sock_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
Shaun Pereira89bbfc92006-03-21 23:58:08 -0800119#ifdef CONFIG_COMPAT
120static long compat_sock_ioctl(struct file *file,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700121 unsigned int cmd, unsigned long arg);
Shaun Pereira89bbfc92006-03-21 23:58:08 -0800122#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123static int sock_fasync(int fd, struct file *filp, int on);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124static ssize_t sock_sendpage(struct file *file, struct page *page,
125 int offset, size_t size, loff_t *ppos, int more);
Jens Axboe9c55e012007-11-06 23:30:13 -0800126static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700127 struct pipe_inode_info *pipe, size_t len,
Jens Axboe9c55e012007-11-06 23:30:13 -0800128 unsigned int flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130/*
131 * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
132 * in the operation structures but are done directly via the socketcall() multiplexor.
133 */
134
Arjan van de Venda7071d2007-02-12 00:55:36 -0800135static const struct file_operations socket_file_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 .owner = THIS_MODULE,
137 .llseek = no_llseek,
138 .aio_read = sock_aio_read,
139 .aio_write = sock_aio_write,
140 .poll = sock_poll,
141 .unlocked_ioctl = sock_ioctl,
Shaun Pereira89bbfc92006-03-21 23:58:08 -0800142#ifdef CONFIG_COMPAT
143 .compat_ioctl = compat_sock_ioctl,
144#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 .mmap = sock_mmap,
146 .open = sock_no_open, /* special open code to disallow open via /proc */
147 .release = sock_close,
148 .fasync = sock_fasync,
Jens Axboe5274f052006-03-30 15:15:30 +0200149 .sendpage = sock_sendpage,
150 .splice_write = generic_splice_sendpage,
Jens Axboe9c55e012007-11-06 23:30:13 -0800151 .splice_read = sock_splice_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152};
153
154/*
155 * The protocol list. Each protocol is registered in here.
156 */
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158static DEFINE_SPINLOCK(net_family_lock);
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -0700159static const struct net_proto_family *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
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700173#define MAX_SOCK_ADDR 128 /* 108 for Unix domain -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 16 for IP, 16 for IPX,
175 24 for IPv6,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700176 about 80 for AX.25
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 must be at least one bigger than
178 the AF_UNIX size (see net/unix/af_unix.c
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700179 :unix_mkname()).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182/**
183 * move_addr_to_kernel - copy a socket address into kernel space
184 * @uaddr: Address in user space
185 * @kaddr: Address in kernel space
186 * @ulen: Length in user space
187 *
188 * The address is copied into kernel space. If the provided address is
189 * too long an error code of -EINVAL is returned. If the copy gives
190 * invalid addresses -EFAULT is returned. On a success 0 is returned.
191 */
192
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -0700193int move_addr_to_kernel(void __user *uaddr, int ulen, struct sockaddr *kaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -0700195 if (ulen < 0 || ulen > sizeof(struct sockaddr_storage))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700197 if (ulen == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 return 0;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700199 if (copy_from_user(kaddr, uaddr, ulen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 return -EFAULT;
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100201 return audit_sockaddr(ulen, kaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202}
203
204/**
205 * move_addr_to_user - copy an address to user space
206 * @kaddr: kernel space address
207 * @klen: length of address in kernel
208 * @uaddr: user space address
209 * @ulen: pointer to user length field
210 *
211 * The value pointed to by ulen on entry is the buffer length available.
212 * This is overwritten with the buffer space used. -EINVAL is returned
213 * if an overlong buffer is specified or a negative buffer size. -EFAULT
214 * is returned if either the buffer or the length field are not
215 * accessible.
216 * After copying the data up to the limit the user specifies, the true
217 * length of the data is written over the length limit the user
218 * specified. Zero is returned for a success.
219 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700220
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -0700221int move_addr_to_user(struct sockaddr *kaddr, int klen, void __user *uaddr,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700222 int __user *ulen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
224 int err;
225 int len;
226
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700227 err = get_user(len, ulen);
228 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 return err;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700230 if (len > klen)
231 len = klen;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -0700232 if (len < 0 || len > sizeof(struct sockaddr_storage))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700234 if (len) {
Steve Grubbd6fe3942006-03-30 12:20:22 -0500235 if (audit_sockaddr(klen, kaddr))
236 return -ENOMEM;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700237 if (copy_to_user(uaddr, kaddr, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 return -EFAULT;
239 }
240 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700241 * "fromlen shall refer to the value before truncation.."
242 * 1003.1g
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 */
244 return __put_user(klen, ulen);
245}
246
Christoph Lametere18b8902006-12-06 20:33:20 -0800247static struct kmem_cache *sock_inode_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249static struct inode *sock_alloc_inode(struct super_block *sb)
250{
251 struct socket_alloc *ei;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700252
Christoph Lametere94b1762006-12-06 20:33:17 -0800253 ei = kmem_cache_alloc(sock_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 if (!ei)
255 return NULL;
Eric Dumazet43815482010-04-29 11:01:49 +0000256 ei->socket.wq = kmalloc(sizeof(struct socket_wq), GFP_KERNEL);
257 if (!ei->socket.wq) {
258 kmem_cache_free(sock_inode_cachep, ei);
259 return NULL;
260 }
261 init_waitqueue_head(&ei->socket.wq->wait);
262 ei->socket.wq->fasync_list = NULL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 ei->socket.state = SS_UNCONNECTED;
265 ei->socket.flags = 0;
266 ei->socket.ops = NULL;
267 ei->socket.sk = NULL;
268 ei->socket.file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 return &ei->vfs_inode;
271}
272
Eric Dumazet43815482010-04-29 11:01:49 +0000273
274static void wq_free_rcu(struct rcu_head *head)
275{
276 struct socket_wq *wq = container_of(head, struct socket_wq, rcu);
277
278 kfree(wq);
279}
280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281static void sock_destroy_inode(struct inode *inode)
282{
Eric Dumazet43815482010-04-29 11:01:49 +0000283 struct socket_alloc *ei;
284
285 ei = container_of(inode, struct socket_alloc, vfs_inode);
286 call_rcu(&ei->socket.wq->rcu, wq_free_rcu);
287 kmem_cache_free(sock_inode_cachep, ei);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288}
289
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700290static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700292 struct socket_alloc *ei = (struct socket_alloc *)foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Christoph Lametera35afb82007-05-16 22:10:57 -0700294 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297static int init_inodecache(void)
298{
299 sock_inode_cachep = kmem_cache_create("sock_inode_cache",
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700300 sizeof(struct socket_alloc),
301 0,
302 (SLAB_HWCACHE_ALIGN |
303 SLAB_RECLAIM_ACCOUNT |
304 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +0900305 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 if (sock_inode_cachep == NULL)
307 return -ENOMEM;
308 return 0;
309}
310
Alexey Dobriyanb87221d2009-09-21 17:01:09 -0700311static const struct super_operations sockfs_ops = {
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700312 .alloc_inode = sock_alloc_inode,
313 .destroy_inode = sock_destroy_inode,
314 .statfs = simple_statfs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315};
316
David Howells454e2392006-06-23 02:02:57 -0700317static int sockfs_get_sb(struct file_system_type *fs_type,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700318 int flags, const char *dev_name, void *data,
319 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
David Howells454e2392006-06-23 02:02:57 -0700321 return get_sb_pseudo(fs_type, "socket:", &sockfs_ops, SOCKFS_MAGIC,
322 mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323}
324
Eric Dumazetba899662005-08-26 12:05:31 -0700325static struct vfsmount *sock_mnt __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327static struct file_system_type sock_fs_type = {
328 .name = "sockfs",
329 .get_sb = sockfs_get_sb,
330 .kill_sb = kill_anon_super,
331};
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700332
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700333/*
334 * sockfs_dname() is called from d_path().
335 */
336static char *sockfs_dname(struct dentry *dentry, char *buffer, int buflen)
337{
338 return dynamic_dname(dentry, buffer, buflen, "socket:[%lu]",
339 dentry->d_inode->i_ino);
340}
341
Al Viro3ba13d12009-02-20 06:02:22 +0000342static const struct dentry_operations sockfs_dentry_operations = {
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700343 .d_dname = sockfs_dname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344};
345
346/*
347 * Obtains the first available file descriptor and sets it up for use.
348 *
David S. Miller39d8c1b2006-03-20 17:13:49 -0800349 * These functions create file structures and maps them to fd space
350 * of the current process. On success it returns file descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 * and file struct implicitly stored in sock->file.
352 * Note that another thread may close file descriptor before we return
353 * from this function. We use the fact that now we do not refer
354 * to socket after mapping. If one day we will need it, this
355 * function will increment ref. count on file by 1.
356 *
357 * In any case returned fd MAY BE not valid!
358 * This race condition is unavoidable
359 * with shared fd spaces, we cannot solve it inside kernel,
360 * but we take care of internal coherence yet.
361 */
362
Al Viro7cbe66b2009-08-05 19:59:08 +0400363static int sock_alloc_file(struct socket *sock, struct file **f, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
Al Viro7cbe66b2009-08-05 19:59:08 +0400365 struct qstr name = { .name = "" };
Al Viro2c48b9c2009-08-09 00:52:35 +0400366 struct path path;
Al Viro7cbe66b2009-08-05 19:59:08 +0400367 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 int fd;
David S. Miller39d8c1b2006-03-20 17:13:49 -0800369
Ulrich Dreppera677a032008-07-23 21:29:17 -0700370 fd = get_unused_fd_flags(flags);
Al Viro7cbe66b2009-08-05 19:59:08 +0400371 if (unlikely(fd < 0))
372 return fd;
David S. Miller39d8c1b2006-03-20 17:13:49 -0800373
Al Viro2c48b9c2009-08-09 00:52:35 +0400374 path.dentry = d_alloc(sock_mnt->mnt_sb->s_root, &name);
375 if (unlikely(!path.dentry)) {
Al Viro7cbe66b2009-08-05 19:59:08 +0400376 put_unused_fd(fd);
David S. Miller39d8c1b2006-03-20 17:13:49 -0800377 return -ENOMEM;
Al Viro7cbe66b2009-08-05 19:59:08 +0400378 }
Al Viro2c48b9c2009-08-09 00:52:35 +0400379 path.mnt = mntget(sock_mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Al Viro2c48b9c2009-08-09 00:52:35 +0400381 path.dentry->d_op = &sockfs_dentry_operations;
Al Viro2c48b9c2009-08-09 00:52:35 +0400382 d_instantiate(path.dentry, SOCK_INODE(sock));
Al Virocc3808f2009-08-06 09:43:59 +0400383 SOCK_INODE(sock)->i_fop = &socket_file_ops;
384
Al Viro2c48b9c2009-08-09 00:52:35 +0400385 file = alloc_file(&path, FMODE_READ | FMODE_WRITE,
Al Virocc3808f2009-08-06 09:43:59 +0400386 &socket_file_ops);
387 if (unlikely(!file)) {
388 /* drop dentry, keep inode */
389 atomic_inc(&path.dentry->d_inode->i_count);
Al Viro2c48b9c2009-08-09 00:52:35 +0400390 path_put(&path);
Al Virocc3808f2009-08-06 09:43:59 +0400391 put_unused_fd(fd);
392 return -ENFILE;
393 }
David S. Miller39d8c1b2006-03-20 17:13:49 -0800394
395 sock->file = file;
Ulrich Drepper77d27202008-07-23 21:29:35 -0700396 file->f_flags = O_RDWR | (flags & O_NONBLOCK);
David S. Miller39d8c1b2006-03-20 17:13:49 -0800397 file->f_pos = 0;
398 file->private_data = sock;
399
Al Viro7cbe66b2009-08-05 19:59:08 +0400400 *f = file;
401 return fd;
David S. Miller39d8c1b2006-03-20 17:13:49 -0800402}
403
Ulrich Dreppera677a032008-07-23 21:29:17 -0700404int sock_map_fd(struct socket *sock, int flags)
David S. Miller39d8c1b2006-03-20 17:13:49 -0800405{
406 struct file *newfile;
Al Viro7cbe66b2009-08-05 19:59:08 +0400407 int fd = sock_alloc_file(sock, &newfile, flags);
David S. Miller39d8c1b2006-03-20 17:13:49 -0800408
Al Viro7cbe66b2009-08-05 19:59:08 +0400409 if (likely(fd >= 0))
David S. Miller39d8c1b2006-03-20 17:13:49 -0800410 fd_install(fd, newfile);
Al Viro7cbe66b2009-08-05 19:59:08 +0400411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 return fd;
413}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700414EXPORT_SYMBOL(sock_map_fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800416static struct socket *sock_from_file(struct file *file, int *err)
417{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800418 if (file->f_op == &socket_file_ops)
419 return file->private_data; /* set in sock_map_fd */
420
Eric Dumazet23bb80d2007-02-08 14:59:57 -0800421 *err = -ENOTSOCK;
422 return NULL;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800423}
424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425/**
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700426 * sockfd_lookup - Go from a file number to its socket slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 * @fd: file handle
428 * @err: pointer to an error code return
429 *
430 * The file handle passed in is locked and the socket it is bound
431 * too is returned. If an error occurs the err pointer is overwritten
432 * with a negative errno code and NULL is returned. The function checks
433 * for both invalid handles and passing a handle which is not a socket.
434 *
435 * On a success the socket object pointer is returned.
436 */
437
438struct socket *sockfd_lookup(int fd, int *err)
439{
440 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 struct socket *sock;
442
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700443 file = fget(fd);
444 if (!file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 *err = -EBADF;
446 return NULL;
447 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700448
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800449 sock = sock_from_file(file, err);
450 if (!sock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 fput(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 return sock;
453}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700454EXPORT_SYMBOL(sockfd_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800456static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed)
457{
458 struct file *file;
459 struct socket *sock;
460
Hua Zhong36725582006-04-19 15:25:02 -0700461 *err = -EBADF;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800462 file = fget_light(fd, fput_needed);
463 if (file) {
464 sock = sock_from_file(file, err);
465 if (sock)
466 return sock;
467 fput_light(file, *fput_needed);
468 }
469 return NULL;
470}
471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472/**
473 * sock_alloc - allocate a socket
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700474 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 * Allocate a new inode and socket object. The two are bound together
476 * and initialised. The socket is then returned. If we are out of inodes
477 * NULL is returned.
478 */
479
480static struct socket *sock_alloc(void)
481{
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700482 struct inode *inode;
483 struct socket *sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 inode = new_inode(sock_mnt->mnt_sb);
486 if (!inode)
487 return NULL;
488
489 sock = SOCKET_I(inode);
490
Eric Dumazet29a020d2009-09-15 02:39:20 -0700491 kmemcheck_annotate_bitfield(sock, type);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700492 inode->i_mode = S_IFSOCK | S_IRWXUGO;
David Howells8192b0c2008-11-14 10:39:10 +1100493 inode->i_uid = current_fsuid();
494 inode->i_gid = current_fsgid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Eric Dumazet4e694892009-04-04 16:41:09 -0700496 percpu_add(sockets_in_use, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 return sock;
498}
499
500/*
501 * In theory you can't get an open on this inode, but /proc provides
502 * a back door. Remember to keep it shut otherwise you'll let the
503 * creepy crawlies in.
504 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506static int sock_no_open(struct inode *irrelevant, struct file *dontcare)
507{
508 return -ENXIO;
509}
510
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800511const struct file_operations bad_sock_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 .owner = THIS_MODULE,
513 .open = sock_no_open,
514};
515
516/**
517 * sock_release - close a socket
518 * @sock: socket to close
519 *
520 * The socket is released from the protocol stack if it has a release
521 * callback, and the inode is then released if the socket is bound to
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700522 * an inode not a file.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525void sock_release(struct socket *sock)
526{
527 if (sock->ops) {
528 struct module *owner = sock->ops->owner;
529
530 sock->ops->release(sock);
531 sock->ops = NULL;
532 module_put(owner);
533 }
534
Eric Dumazet43815482010-04-29 11:01:49 +0000535 if (sock->wq->fasync_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 printk(KERN_ERR "sock_release: fasync list not empty!\n");
537
Eric Dumazet4e694892009-04-04 16:41:09 -0700538 percpu_sub(sockets_in_use, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if (!sock->file) {
540 iput(SOCK_INODE(sock));
541 return;
542 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700543 sock->file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700545EXPORT_SYMBOL(sock_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Patrick Ohly20d49472009-02-12 05:03:38 +0000547int sock_tx_timestamp(struct msghdr *msg, struct sock *sk,
548 union skb_shared_tx *shtx)
549{
550 shtx->flags = 0;
551 if (sock_flag(sk, SOCK_TIMESTAMPING_TX_HARDWARE))
552 shtx->hardware = 1;
553 if (sock_flag(sk, SOCK_TIMESTAMPING_TX_SOFTWARE))
554 shtx->software = 1;
555 return 0;
556}
557EXPORT_SYMBOL(sock_tx_timestamp);
558
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700559static inline int __sock_sendmsg(struct kiocb *iocb, struct socket *sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 struct msghdr *msg, size_t size)
561{
562 struct sock_iocb *si = kiocb_to_siocb(iocb);
563 int err;
564
Herbert Xuf8451722010-05-24 00:12:34 -0700565 sock_update_classid(sock->sk);
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 si->sock = sock;
568 si->scm = NULL;
569 si->msg = msg;
570 si->size = size;
571
572 err = security_socket_sendmsg(sock, msg, size);
573 if (err)
574 return err;
575
576 return sock->ops->sendmsg(iocb, sock, msg, size);
577}
578
579int sock_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
580{
581 struct kiocb iocb;
582 struct sock_iocb siocb;
583 int ret;
584
585 init_sync_kiocb(&iocb, NULL);
586 iocb.private = &siocb;
587 ret = __sock_sendmsg(&iocb, sock, msg, size);
588 if (-EIOCBQUEUED == ret)
589 ret = wait_on_sync_kiocb(&iocb);
590 return ret;
591}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700592EXPORT_SYMBOL(sock_sendmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
595 struct kvec *vec, size_t num, size_t size)
596{
597 mm_segment_t oldfs = get_fs();
598 int result;
599
600 set_fs(KERNEL_DS);
601 /*
602 * the following is safe, since for compiler definitions of kvec and
603 * iovec are identical, yielding the same in-core layout and alignment
604 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700605 msg->msg_iov = (struct iovec *)vec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 msg->msg_iovlen = num;
607 result = sock_sendmsg(sock, msg, size);
608 set_fs(oldfs);
609 return result;
610}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700611EXPORT_SYMBOL(kernel_sendmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Patrick Ohly20d49472009-02-12 05:03:38 +0000613static int ktime2ts(ktime_t kt, struct timespec *ts)
614{
615 if (kt.tv64) {
616 *ts = ktime_to_timespec(kt);
617 return 1;
618 } else {
619 return 0;
620 }
621}
622
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700623/*
624 * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
625 */
626void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
627 struct sk_buff *skb)
628{
Patrick Ohly20d49472009-02-12 05:03:38 +0000629 int need_software_tstamp = sock_flag(sk, SOCK_RCVTSTAMP);
630 struct timespec ts[3];
631 int empty = 1;
632 struct skb_shared_hwtstamps *shhwtstamps =
633 skb_hwtstamps(skb);
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700634
Patrick Ohly20d49472009-02-12 05:03:38 +0000635 /* Race occurred between timestamp enabling and packet
636 receiving. Fill in the current time for now. */
637 if (need_software_tstamp && skb->tstamp.tv64 == 0)
638 __net_timestamp(skb);
639
640 if (need_software_tstamp) {
641 if (!sock_flag(sk, SOCK_RCVTSTAMPNS)) {
642 struct timeval tv;
643 skb_get_timestamp(skb, &tv);
644 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMP,
645 sizeof(tv), &tv);
646 } else {
Hagen Paul Pfeifer842509b2010-04-06 05:39:52 +0000647 skb_get_timestampns(skb, &ts[0]);
Patrick Ohly20d49472009-02-12 05:03:38 +0000648 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPNS,
Hagen Paul Pfeifer842509b2010-04-06 05:39:52 +0000649 sizeof(ts[0]), &ts[0]);
Patrick Ohly20d49472009-02-12 05:03:38 +0000650 }
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700651 }
Patrick Ohly20d49472009-02-12 05:03:38 +0000652
653
654 memset(ts, 0, sizeof(ts));
655 if (skb->tstamp.tv64 &&
656 sock_flag(sk, SOCK_TIMESTAMPING_SOFTWARE)) {
657 skb_get_timestampns(skb, ts + 0);
658 empty = 0;
659 }
660 if (shhwtstamps) {
661 if (sock_flag(sk, SOCK_TIMESTAMPING_SYS_HARDWARE) &&
662 ktime2ts(shhwtstamps->syststamp, ts + 1))
663 empty = 0;
664 if (sock_flag(sk, SOCK_TIMESTAMPING_RAW_HARDWARE) &&
665 ktime2ts(shhwtstamps->hwtstamp, ts + 2))
666 empty = 0;
667 }
668 if (!empty)
669 put_cmsg(msg, SOL_SOCKET,
670 SCM_TIMESTAMPING, sizeof(ts), &ts);
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700671}
Arnaldo Carvalho de Melo7c81fd82007-03-10 00:39:35 -0300672EXPORT_SYMBOL_GPL(__sock_recv_timestamp);
673
Neil Horman3b885782009-10-12 13:26:31 -0700674inline void sock_recv_drops(struct msghdr *msg, struct sock *sk, struct sk_buff *skb)
675{
676 if (sock_flag(sk, SOCK_RXQ_OVFL) && skb && skb->dropcount)
677 put_cmsg(msg, SOL_SOCKET, SO_RXQ_OVFL,
678 sizeof(__u32), &skb->dropcount);
679}
680
Eric Dumazet767dd032010-04-28 19:14:43 +0000681void __sock_recv_ts_and_drops(struct msghdr *msg, struct sock *sk,
Neil Horman3b885782009-10-12 13:26:31 -0700682 struct sk_buff *skb)
683{
684 sock_recv_timestamp(msg, sk, skb);
685 sock_recv_drops(msg, sk, skb);
686}
Eric Dumazet767dd032010-04-28 19:14:43 +0000687EXPORT_SYMBOL_GPL(__sock_recv_ts_and_drops);
Neil Horman3b885782009-10-12 13:26:31 -0700688
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -0700689static inline int __sock_recvmsg_nosec(struct kiocb *iocb, struct socket *sock,
690 struct msghdr *msg, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 struct sock_iocb *si = kiocb_to_siocb(iocb);
693
Herbert Xuf8451722010-05-24 00:12:34 -0700694 sock_update_classid(sock->sk);
695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 si->sock = sock;
697 si->scm = NULL;
698 si->msg = msg;
699 si->size = size;
700 si->flags = flags;
701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 return sock->ops->recvmsg(iocb, sock, msg, size, flags);
703}
704
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -0700705static inline int __sock_recvmsg(struct kiocb *iocb, struct socket *sock,
706 struct msghdr *msg, size_t size, int flags)
707{
708 int err = security_socket_recvmsg(sock, msg, size, flags);
709
710 return err ?: __sock_recvmsg_nosec(iocb, sock, msg, size, flags);
711}
712
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700713int sock_recvmsg(struct socket *sock, struct msghdr *msg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 size_t size, int flags)
715{
716 struct kiocb iocb;
717 struct sock_iocb siocb;
718 int ret;
719
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700720 init_sync_kiocb(&iocb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 iocb.private = &siocb;
722 ret = __sock_recvmsg(&iocb, sock, msg, size, flags);
723 if (-EIOCBQUEUED == ret)
724 ret = wait_on_sync_kiocb(&iocb);
725 return ret;
726}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700727EXPORT_SYMBOL(sock_recvmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -0700729static int sock_recvmsg_nosec(struct socket *sock, struct msghdr *msg,
730 size_t size, int flags)
731{
732 struct kiocb iocb;
733 struct sock_iocb siocb;
734 int ret;
735
736 init_sync_kiocb(&iocb, NULL);
737 iocb.private = &siocb;
738 ret = __sock_recvmsg_nosec(&iocb, sock, msg, size, flags);
739 if (-EIOCBQUEUED == ret)
740 ret = wait_on_sync_kiocb(&iocb);
741 return ret;
742}
743
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700744int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
745 struct kvec *vec, size_t num, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
747 mm_segment_t oldfs = get_fs();
748 int result;
749
750 set_fs(KERNEL_DS);
751 /*
752 * the following is safe, since for compiler definitions of kvec and
753 * iovec are identical, yielding the same in-core layout and alignment
754 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700755 msg->msg_iov = (struct iovec *)vec, msg->msg_iovlen = num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 result = sock_recvmsg(sock, msg, size, flags);
757 set_fs(oldfs);
758 return result;
759}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700760EXPORT_SYMBOL(kernel_recvmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762static void sock_aio_dtor(struct kiocb *iocb)
763{
764 kfree(iocb->private);
765}
766
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -0300767static ssize_t sock_sendpage(struct file *file, struct page *page,
768 int offset, size_t size, loff_t *ppos, int more)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
770 struct socket *sock;
771 int flags;
772
Eric Dumazetb69aee02005-09-06 14:42:45 -0700773 sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
775 flags = !(file->f_flags & O_NONBLOCK) ? 0 : MSG_DONTWAIT;
776 if (more)
777 flags |= MSG_MORE;
778
Linus Torvaldse6949582009-08-13 08:28:36 -0700779 return kernel_sendpage(sock, page, offset, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780}
781
Jens Axboe9c55e012007-11-06 23:30:13 -0800782static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700783 struct pipe_inode_info *pipe, size_t len,
Jens Axboe9c55e012007-11-06 23:30:13 -0800784 unsigned int flags)
785{
786 struct socket *sock = file->private_data;
787
Rémi Denis-Courmont997b37d2008-02-15 02:35:45 -0800788 if (unlikely(!sock->ops->splice_read))
789 return -EINVAL;
790
Herbert Xuf8451722010-05-24 00:12:34 -0700791 sock_update_classid(sock->sk);
792
Jens Axboe9c55e012007-11-06 23:30:13 -0800793 return sock->ops->splice_read(sock, ppos, pipe, len, flags);
794}
795
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800796static struct sock_iocb *alloc_sock_iocb(struct kiocb *iocb,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700797 struct sock_iocb *siocb)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800798{
799 if (!is_sync_kiocb(iocb)) {
800 siocb = kmalloc(sizeof(*siocb), GFP_KERNEL);
801 if (!siocb)
802 return NULL;
803 iocb->ki_dtor = sock_aio_dtor;
804 }
805
806 siocb->kiocb = iocb;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800807 iocb->private = siocb;
808 return siocb;
809}
810
811static ssize_t do_sock_read(struct msghdr *msg, struct kiocb *iocb,
Badari Pulavarty027445c2006-09-30 23:28:46 -0700812 struct file *file, const struct iovec *iov,
813 unsigned long nr_segs)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800814{
815 struct socket *sock = file->private_data;
816 size_t size = 0;
817 int i;
818
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700819 for (i = 0; i < nr_segs; i++)
820 size += iov[i].iov_len;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800821
822 msg->msg_name = NULL;
823 msg->msg_namelen = 0;
824 msg->msg_control = NULL;
825 msg->msg_controllen = 0;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700826 msg->msg_iov = (struct iovec *)iov;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800827 msg->msg_iovlen = nr_segs;
828 msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
829
830 return __sock_recvmsg(iocb, sock, msg, size, msg->msg_flags);
831}
832
Badari Pulavarty027445c2006-09-30 23:28:46 -0700833static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov,
834 unsigned long nr_segs, loff_t pos)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800835{
836 struct sock_iocb siocb, *x;
837
838 if (pos != 0)
839 return -ESPIPE;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700840
841 if (iocb->ki_left == 0) /* Match SYS5 behaviour */
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800842 return 0;
843
Badari Pulavarty027445c2006-09-30 23:28:46 -0700844
845 x = alloc_sock_iocb(iocb, &siocb);
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800846 if (!x)
847 return -ENOMEM;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700848 return do_sock_read(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800849}
850
851static ssize_t do_sock_write(struct msghdr *msg, struct kiocb *iocb,
Badari Pulavarty027445c2006-09-30 23:28:46 -0700852 struct file *file, const struct iovec *iov,
853 unsigned long nr_segs)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800854{
855 struct socket *sock = file->private_data;
856 size_t size = 0;
857 int i;
858
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700859 for (i = 0; i < nr_segs; i++)
860 size += iov[i].iov_len;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800861
862 msg->msg_name = NULL;
863 msg->msg_namelen = 0;
864 msg->msg_control = NULL;
865 msg->msg_controllen = 0;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700866 msg->msg_iov = (struct iovec *)iov;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800867 msg->msg_iovlen = nr_segs;
868 msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
869 if (sock->type == SOCK_SEQPACKET)
870 msg->msg_flags |= MSG_EOR;
871
872 return __sock_sendmsg(iocb, sock, msg, size);
873}
874
Badari Pulavarty027445c2006-09-30 23:28:46 -0700875static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov,
876 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800878 struct sock_iocb siocb, *x;
879
880 if (pos != 0)
881 return -ESPIPE;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700882
Badari Pulavarty027445c2006-09-30 23:28:46 -0700883 x = alloc_sock_iocb(iocb, &siocb);
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800884 if (!x)
885 return -ENOMEM;
886
Badari Pulavarty027445c2006-09-30 23:28:46 -0700887 return do_sock_write(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888}
889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890/*
891 * Atomic setting of ioctl hooks to avoid race
892 * with module unload.
893 */
894
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800895static DEFINE_MUTEX(br_ioctl_mutex);
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700896static int (*br_ioctl_hook) (struct net *, unsigned int cmd, void __user *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Eric W. Biederman881d9662007-09-17 11:56:21 -0700898void brioctl_set(int (*hook) (struct net *, unsigned int, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800900 mutex_lock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 br_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800902 mutex_unlock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903}
904EXPORT_SYMBOL(brioctl_set);
905
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800906static DEFINE_MUTEX(vlan_ioctl_mutex);
Eric W. Biederman881d9662007-09-17 11:56:21 -0700907static int (*vlan_ioctl_hook) (struct net *, void __user *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Eric W. Biederman881d9662007-09-17 11:56:21 -0700909void vlan_ioctl_set(int (*hook) (struct net *, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800911 mutex_lock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 vlan_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800913 mutex_unlock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914}
915EXPORT_SYMBOL(vlan_ioctl_set);
916
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800917static DEFINE_MUTEX(dlci_ioctl_mutex);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700918static int (*dlci_ioctl_hook) (unsigned int, void __user *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700920void dlci_ioctl_set(int (*hook) (unsigned int, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800922 mutex_lock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 dlci_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800924 mutex_unlock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925}
926EXPORT_SYMBOL(dlci_ioctl_set);
927
Arnd Bergmann6b960182009-11-06 23:10:54 -0800928static long sock_do_ioctl(struct net *net, struct socket *sock,
929 unsigned int cmd, unsigned long arg)
930{
931 int err;
932 void __user *argp = (void __user *)arg;
933
934 err = sock->ops->ioctl(sock, cmd, arg);
935
936 /*
937 * If this ioctl is unknown try to hand it down
938 * to the NIC driver.
939 */
940 if (err == -ENOIOCTLCMD)
941 err = dev_ioctl(net, cmd, argp);
942
943 return err;
944}
945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946/*
947 * With an ioctl, arg may well be a user mode pointer, but we don't know
948 * what to do with it - that's up to the protocol still.
949 */
950
951static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
952{
953 struct socket *sock;
Eric W. Biederman881d9662007-09-17 11:56:21 -0700954 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 void __user *argp = (void __user *)arg;
956 int pid, err;
Eric W. Biederman881d9662007-09-17 11:56:21 -0700957 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Eric Dumazetb69aee02005-09-06 14:42:45 -0700959 sock = file->private_data;
Eric W. Biederman881d9662007-09-17 11:56:21 -0700960 sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900961 net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
Eric W. Biederman881d9662007-09-17 11:56:21 -0700963 err = dev_ioctl(net, cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 } else
Johannes Berg3d23e342009-09-29 23:27:28 +0200965#ifdef CONFIG_WEXT_CORE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
Eric W. Biederman881d9662007-09-17 11:56:21 -0700967 err = dev_ioctl(net, cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 } else
Johannes Berg3d23e342009-09-29 23:27:28 +0200969#endif
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700970 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 case FIOSETOWN:
972 case SIOCSPGRP:
973 err = -EFAULT;
974 if (get_user(pid, (int __user *)argp))
975 break;
976 err = f_setown(sock->file, pid, 1);
977 break;
978 case FIOGETOWN:
979 case SIOCGPGRP:
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700980 err = put_user(f_getown(sock->file),
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700981 (int __user *)argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 break;
983 case SIOCGIFBR:
984 case SIOCSIFBR:
985 case SIOCBRADDBR:
986 case SIOCBRDELBR:
987 err = -ENOPKG;
988 if (!br_ioctl_hook)
989 request_module("bridge");
990
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800991 mutex_lock(&br_ioctl_mutex);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700992 if (br_ioctl_hook)
Eric W. Biederman881d9662007-09-17 11:56:21 -0700993 err = br_ioctl_hook(net, cmd, argp);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800994 mutex_unlock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 break;
996 case SIOCGIFVLAN:
997 case SIOCSIFVLAN:
998 err = -ENOPKG;
999 if (!vlan_ioctl_hook)
1000 request_module("8021q");
1001
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001002 mutex_lock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 if (vlan_ioctl_hook)
Eric W. Biederman881d9662007-09-17 11:56:21 -07001004 err = vlan_ioctl_hook(net, argp);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001005 mutex_unlock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 case SIOCADDDLCI:
1008 case SIOCDELDLCI:
1009 err = -ENOPKG;
1010 if (!dlci_ioctl_hook)
1011 request_module("dlci");
1012
Pavel Emelyanov7512cbf2008-03-21 15:58:52 -07001013 mutex_lock(&dlci_ioctl_mutex);
1014 if (dlci_ioctl_hook)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 err = dlci_ioctl_hook(cmd, argp);
Pavel Emelyanov7512cbf2008-03-21 15:58:52 -07001016 mutex_unlock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 break;
1018 default:
Arnd Bergmann6b960182009-11-06 23:10:54 -08001019 err = sock_do_ioctl(net, sock, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 break;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001021 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 return err;
1023}
1024
1025int sock_create_lite(int family, int type, int protocol, struct socket **res)
1026{
1027 int err;
1028 struct socket *sock = NULL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001029
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 err = security_socket_create(family, type, protocol, 1);
1031 if (err)
1032 goto out;
1033
1034 sock = sock_alloc();
1035 if (!sock) {
1036 err = -ENOMEM;
1037 goto out;
1038 }
1039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 sock->type = type;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001041 err = security_socket_post_create(sock, family, type, protocol, 1);
1042 if (err)
1043 goto out_release;
1044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045out:
1046 *res = sock;
1047 return err;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001048out_release:
1049 sock_release(sock);
1050 sock = NULL;
1051 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001053EXPORT_SYMBOL(sock_create_lite);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
1055/* No kernel lock held - perfect */
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001056static unsigned int sock_poll(struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057{
1058 struct socket *sock;
1059
1060 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001061 * We can't return errors to poll, so it's either yes or no.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 */
Eric Dumazetb69aee02005-09-06 14:42:45 -07001063 sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 return sock->ops->poll(file, sock, wait);
1065}
1066
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001067static int sock_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{
Eric Dumazetb69aee02005-09-06 14:42:45 -07001069 struct socket *sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
1071 return sock->ops->mmap(file, sock, vma);
1072}
1073
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001074static int sock_close(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075{
1076 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001077 * It was possible the inode is NULL we were
1078 * closing an unfinished socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 */
1080
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001081 if (!inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 printk(KERN_DEBUG "sock_close: NULL inode\n");
1083 return 0;
1084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 sock_release(SOCKET_I(inode));
1086 return 0;
1087}
1088
1089/*
1090 * Update the socket async list
1091 *
1092 * Fasync_list locking strategy.
1093 *
1094 * 1. fasync_list is modified only under process context socket lock
1095 * i.e. under semaphore.
1096 * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
Eric Dumazet989a2972010-04-14 09:55:35 +00001097 * or under socket lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 */
1099
1100static int sock_fasync(int fd, struct file *filp, int on)
1101{
Eric Dumazet989a2972010-04-14 09:55:35 +00001102 struct socket *sock = filp->private_data;
1103 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Eric Dumazet989a2972010-04-14 09:55:35 +00001105 if (sk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
1108 lock_sock(sk);
1109
Eric Dumazet43815482010-04-29 11:01:49 +00001110 fasync_helper(fd, filp, on, &sock->wq->fasync_list);
Eric Dumazet989a2972010-04-14 09:55:35 +00001111
Eric Dumazet43815482010-04-29 11:01:49 +00001112 if (!sock->wq->fasync_list)
Eric Dumazet989a2972010-04-14 09:55:35 +00001113 sock_reset_flag(sk, SOCK_FASYNC);
Jonathan Corbet76398422009-02-01 14:26:59 -07001114 else
Eric Dumazetbcdce712009-10-06 17:28:29 -07001115 sock_set_flag(sk, SOCK_FASYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Eric Dumazet989a2972010-04-14 09:55:35 +00001117 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 return 0;
1119}
1120
Eric Dumazet43815482010-04-29 11:01:49 +00001121/* This function may be called only under socket lock or callback_lock or rcu_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
1123int sock_wake_async(struct socket *sock, int how, int band)
1124{
Eric Dumazet43815482010-04-29 11:01:49 +00001125 struct socket_wq *wq;
1126
1127 if (!sock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 return -1;
Eric Dumazet43815482010-04-29 11:01:49 +00001129 rcu_read_lock();
1130 wq = rcu_dereference(sock->wq);
1131 if (!wq || !wq->fasync_list) {
1132 rcu_read_unlock();
1133 return -1;
1134 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001135 switch (how) {
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001136 case SOCK_WAKE_WAITD:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 if (test_bit(SOCK_ASYNC_WAITDATA, &sock->flags))
1138 break;
1139 goto call_kill;
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001140 case SOCK_WAKE_SPACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags))
1142 break;
1143 /* fall through */
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001144 case SOCK_WAKE_IO:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001145call_kill:
Eric Dumazet43815482010-04-29 11:01:49 +00001146 kill_fasync(&wq->fasync_list, SIGIO, band);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 break;
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001148 case SOCK_WAKE_URG:
Eric Dumazet43815482010-04-29 11:01:49 +00001149 kill_fasync(&wq->fasync_list, SIGURG, band);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 }
Eric Dumazet43815482010-04-29 11:01:49 +00001151 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 return 0;
1153}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001154EXPORT_SYMBOL(sock_wake_async);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001156static int __sock_create(struct net *net, int family, int type, int protocol,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001157 struct socket **res, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158{
1159 int err;
1160 struct socket *sock;
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001161 const struct net_proto_family *pf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
1163 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001164 * Check protocol is in range
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 */
1166 if (family < 0 || family >= NPROTO)
1167 return -EAFNOSUPPORT;
1168 if (type < 0 || type >= SOCK_MAX)
1169 return -EINVAL;
1170
1171 /* Compatibility.
1172
1173 This uglymoron is moved from INET layer to here to avoid
1174 deadlock in module load.
1175 */
1176 if (family == PF_INET && type == SOCK_PACKET) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001177 static int warned;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 if (!warned) {
1179 warned = 1;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001180 printk(KERN_INFO "%s uses obsolete (PF_INET,SOCK_PACKET)\n",
1181 current->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 }
1183 family = PF_PACKET;
1184 }
1185
1186 err = security_socket_create(family, type, protocol, kern);
1187 if (err)
1188 return err;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001189
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001190 /*
1191 * Allocate the socket and allow the family to set things up. if
1192 * the protocol is 0, the family is instructed to select an appropriate
1193 * default.
1194 */
1195 sock = sock_alloc();
1196 if (!sock) {
1197 if (net_ratelimit())
1198 printk(KERN_WARNING "socket: no more sockets\n");
1199 return -ENFILE; /* Not exactly a match, but its the
1200 closest posix thing */
1201 }
1202
1203 sock->type = type;
1204
Johannes Berg95a5afc2008-10-16 15:24:51 -07001205#ifdef CONFIG_MODULES
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001206 /* Attempt to load a protocol module if the find failed.
1207 *
1208 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 * requested real, full-featured networking support upon configuration.
1210 * Otherwise module support will break!
1211 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001212 if (net_families[family] == NULL)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001213 request_module("net-pf-%d", family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214#endif
1215
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001216 rcu_read_lock();
1217 pf = rcu_dereference(net_families[family]);
1218 err = -EAFNOSUPPORT;
1219 if (!pf)
1220 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
1222 /*
1223 * We will call the ->create function, that possibly is in a loadable
1224 * module, so we have to bump that loadable module refcnt first.
1225 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001226 if (!try_module_get(pf->owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 goto out_release;
1228
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001229 /* Now protected by module ref count */
1230 rcu_read_unlock();
1231
Eric Paris3f378b62009-11-05 22:18:14 -08001232 err = pf->create(net, sock, protocol, kern);
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001233 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 goto out_module_put;
Frank Filza79af592005-09-27 15:23:38 -07001235
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 /*
1237 * Now to bump the refcnt of the [loadable] module that owns this
1238 * socket at sock_release time we decrement its refcnt.
1239 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001240 if (!try_module_get(sock->ops->owner))
1241 goto out_module_busy;
1242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 /*
1244 * Now that we're done with the ->create function, the [loadable]
1245 * module can have its refcnt decremented
1246 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001247 module_put(pf->owner);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001248 err = security_socket_post_create(sock, family, type, protocol, kern);
1249 if (err)
Herbert Xu3b185522007-08-15 14:46:02 -07001250 goto out_sock_release;
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001251 *res = sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001253 return 0;
1254
1255out_module_busy:
1256 err = -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257out_module_put:
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001258 sock->ops = NULL;
1259 module_put(pf->owner);
1260out_sock_release:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 sock_release(sock);
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001262 return err;
1263
1264out_release:
1265 rcu_read_unlock();
1266 goto out_sock_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267}
1268
1269int sock_create(int family, int type, int protocol, struct socket **res)
1270{
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001271 return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001273EXPORT_SYMBOL(sock_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
1275int sock_create_kern(int family, int type, int protocol, struct socket **res)
1276{
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001277 return __sock_create(&init_net, family, type, protocol, res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001279EXPORT_SYMBOL(sock_create_kern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001281SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282{
1283 int retval;
1284 struct socket *sock;
Ulrich Dreppera677a032008-07-23 21:29:17 -07001285 int flags;
1286
Ulrich Dreppere38b36f2008-07-23 21:29:42 -07001287 /* Check the SOCK_* constants for consistency. */
1288 BUILD_BUG_ON(SOCK_CLOEXEC != O_CLOEXEC);
1289 BUILD_BUG_ON((SOCK_MAX | SOCK_TYPE_MASK) != SOCK_TYPE_MASK);
1290 BUILD_BUG_ON(SOCK_CLOEXEC & SOCK_TYPE_MASK);
1291 BUILD_BUG_ON(SOCK_NONBLOCK & SOCK_TYPE_MASK);
1292
Ulrich Dreppera677a032008-07-23 21:29:17 -07001293 flags = type & ~SOCK_TYPE_MASK;
Ulrich Drepper77d27202008-07-23 21:29:35 -07001294 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Dreppera677a032008-07-23 21:29:17 -07001295 return -EINVAL;
1296 type &= SOCK_TYPE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001298 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1299 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1300
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 retval = sock_create(family, type, protocol, &sock);
1302 if (retval < 0)
1303 goto out;
1304
Ulrich Drepper77d27202008-07-23 21:29:35 -07001305 retval = sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 if (retval < 0)
1307 goto out_release;
1308
1309out:
1310 /* It may be already another descriptor 8) Not kernel problem. */
1311 return retval;
1312
1313out_release:
1314 sock_release(sock);
1315 return retval;
1316}
1317
1318/*
1319 * Create a pair of connected sockets.
1320 */
1321
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001322SYSCALL_DEFINE4(socketpair, int, family, int, type, int, protocol,
1323 int __user *, usockvec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324{
1325 struct socket *sock1, *sock2;
1326 int fd1, fd2, err;
Al Virodb349502007-02-07 01:48:00 -05001327 struct file *newfile1, *newfile2;
Ulrich Dreppera677a032008-07-23 21:29:17 -07001328 int flags;
1329
1330 flags = type & ~SOCK_TYPE_MASK;
Ulrich Drepper77d27202008-07-23 21:29:35 -07001331 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Dreppera677a032008-07-23 21:29:17 -07001332 return -EINVAL;
1333 type &= SOCK_TYPE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001335 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1336 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1337
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 /*
1339 * Obtain the first socket and check if the underlying protocol
1340 * supports the socketpair call.
1341 */
1342
1343 err = sock_create(family, type, protocol, &sock1);
1344 if (err < 0)
1345 goto out;
1346
1347 err = sock_create(family, type, protocol, &sock2);
1348 if (err < 0)
1349 goto out_release_1;
1350
1351 err = sock1->ops->socketpair(sock1, sock2);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001352 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 goto out_release_both;
1354
Al Viro7cbe66b2009-08-05 19:59:08 +04001355 fd1 = sock_alloc_file(sock1, &newfile1, flags);
David S. Millerbf3c23d2007-10-29 21:54:02 -07001356 if (unlikely(fd1 < 0)) {
1357 err = fd1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 goto out_release_both;
David S. Millerbf3c23d2007-10-29 21:54:02 -07001359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
Al Viro7cbe66b2009-08-05 19:59:08 +04001361 fd2 = sock_alloc_file(sock2, &newfile2, flags);
Al Viro198de4d2009-08-05 19:29:23 +04001362 if (unlikely(fd2 < 0)) {
1363 err = fd2;
1364 fput(newfile1);
1365 put_unused_fd(fd1);
1366 sock_release(sock2);
1367 goto out;
Al Virodb349502007-02-07 01:48:00 -05001368 }
1369
Al Viro157cf642008-12-14 04:57:47 -05001370 audit_fd_pair(fd1, fd2);
Al Virodb349502007-02-07 01:48:00 -05001371 fd_install(fd1, newfile1);
1372 fd_install(fd2, newfile2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 /* fd1 and fd2 may be already another descriptors.
1374 * Not kernel problem.
1375 */
1376
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001377 err = put_user(fd1, &usockvec[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 if (!err)
1379 err = put_user(fd2, &usockvec[1]);
1380 if (!err)
1381 return 0;
1382
1383 sys_close(fd2);
1384 sys_close(fd1);
1385 return err;
1386
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387out_release_both:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001388 sock_release(sock2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389out_release_1:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001390 sock_release(sock1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391out:
1392 return err;
1393}
1394
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395/*
1396 * Bind a name to a socket. Nothing much to do here since it's
1397 * the protocol's responsibility to handle the local address.
1398 *
1399 * We move the socket address to kernel space before we call
1400 * the protocol layer (having also checked the address is ok).
1401 */
1402
Heiko Carstens20f37032009-01-14 14:14:23 +01001403SYSCALL_DEFINE3(bind, int, fd, struct sockaddr __user *, umyaddr, int, addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
1405 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001406 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001407 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001409 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001410 if (sock) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001411 err = move_addr_to_kernel(umyaddr, addrlen, (struct sockaddr *)&address);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001412 if (err >= 0) {
1413 err = security_socket_bind(sock,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001414 (struct sockaddr *)&address,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001415 addrlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001416 if (!err)
1417 err = sock->ops->bind(sock,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001418 (struct sockaddr *)
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001419 &address, addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 }
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001421 fput_light(sock->file, fput_needed);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 return err;
1424}
1425
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426/*
1427 * Perform a listen. Basically, we allow the protocol to do anything
1428 * necessary for a listen, and if that works, we mark the socket as
1429 * ready for listening.
1430 */
1431
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001432SYSCALL_DEFINE2(listen, int, fd, int, backlog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{
1434 struct socket *sock;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001435 int err, fput_needed;
Pavel Emelyanovb8e1f9b2007-12-08 00:12:33 -08001436 int somaxconn;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001437
1438 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1439 if (sock) {
Pavel Emelyanov8efa6e92008-03-31 19:41:14 -07001440 somaxconn = sock_net(sock->sk)->core.sysctl_somaxconn;
Pavel Emelyanovb8e1f9b2007-12-08 00:12:33 -08001441 if ((unsigned)backlog > somaxconn)
1442 backlog = somaxconn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
1444 err = security_socket_listen(sock, backlog);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001445 if (!err)
1446 err = sock->ops->listen(sock, backlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001448 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 }
1450 return err;
1451}
1452
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453/*
1454 * For accept, we attempt to create a new socket, set up the link
1455 * with the client, wake up the client, then return the new
1456 * connected fd. We collect the address of the connector in kernel
1457 * space and move it to user at the very end. This is unclean because
1458 * we open the socket then return an error.
1459 *
1460 * 1003.1g adds the ability to recvmsg() to query connection pending
1461 * status to recvmsg. We need to add that support in a way thats
1462 * clean when we restucture accept also.
1463 */
1464
Heiko Carstens20f37032009-01-14 14:14:23 +01001465SYSCALL_DEFINE4(accept4, int, fd, struct sockaddr __user *, upeer_sockaddr,
1466 int __user *, upeer_addrlen, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467{
1468 struct socket *sock, *newsock;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001469 struct file *newfile;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001470 int err, len, newfd, fput_needed;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001471 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
Ulrich Drepper77d27202008-07-23 21:29:35 -07001473 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001474 return -EINVAL;
1475
1476 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1477 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1478
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001479 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 if (!sock)
1481 goto out;
1482
1483 err = -ENFILE;
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001484 newsock = sock_alloc();
1485 if (!newsock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 goto out_put;
1487
1488 newsock->type = sock->type;
1489 newsock->ops = sock->ops;
1490
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 /*
1492 * We don't need try_module_get here, as the listening socket (sock)
1493 * has the protocol module (sock->ops->owner) held.
1494 */
1495 __module_get(newsock->ops->owner);
1496
Al Viro7cbe66b2009-08-05 19:59:08 +04001497 newfd = sock_alloc_file(newsock, &newfile, flags);
David S. Miller39d8c1b2006-03-20 17:13:49 -08001498 if (unlikely(newfd < 0)) {
1499 err = newfd;
David S. Miller9a1875e2006-04-01 12:48:36 -08001500 sock_release(newsock);
1501 goto out_put;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001502 }
1503
Frank Filza79af592005-09-27 15:23:38 -07001504 err = security_socket_accept(sock, newsock);
1505 if (err)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001506 goto out_fd;
Frank Filza79af592005-09-27 15:23:38 -07001507
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 err = sock->ops->accept(sock, newsock, sock->file->f_flags);
1509 if (err < 0)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001510 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
1512 if (upeer_sockaddr) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001513 if (newsock->ops->getname(newsock, (struct sockaddr *)&address,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001514 &len, 2) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 err = -ECONNABORTED;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001516 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 }
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001518 err = move_addr_to_user((struct sockaddr *)&address,
1519 len, upeer_sockaddr, upeer_addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 if (err < 0)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001521 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 }
1523
1524 /* File flags are not inherited via accept() unlike another OSes. */
1525
David S. Miller39d8c1b2006-03-20 17:13:49 -08001526 fd_install(newfd, newfile);
1527 err = newfd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001530 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531out:
1532 return err;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001533out_fd:
David S. Miller9606a212006-04-01 01:00:14 -08001534 fput(newfile);
David S. Miller39d8c1b2006-03-20 17:13:49 -08001535 put_unused_fd(newfd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 goto out_put;
1537}
1538
Heiko Carstens20f37032009-01-14 14:14:23 +01001539SYSCALL_DEFINE3(accept, int, fd, struct sockaddr __user *, upeer_sockaddr,
1540 int __user *, upeer_addrlen)
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001541{
Ulrich Drepperde11def2008-11-19 15:36:14 -08001542 return sys_accept4(fd, upeer_sockaddr, upeer_addrlen, 0);
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001543}
1544
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545/*
1546 * Attempt to connect to a socket with the server address. The address
1547 * is in user space so we verify it is OK and move it to kernel space.
1548 *
1549 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
1550 * break bindings
1551 *
1552 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
1553 * other SEQPACKET protocols that take time to connect() as it doesn't
1554 * include the -EINPROGRESS status for such sockets.
1555 */
1556
Heiko Carstens20f37032009-01-14 14:14:23 +01001557SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
1558 int, addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559{
1560 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001561 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001562 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001564 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 if (!sock)
1566 goto out;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001567 err = move_addr_to_kernel(uservaddr, addrlen, (struct sockaddr *)&address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 if (err < 0)
1569 goto out_put;
1570
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001571 err =
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001572 security_socket_connect(sock, (struct sockaddr *)&address, addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 if (err)
1574 goto out_put;
1575
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001576 err = sock->ops->connect(sock, (struct sockaddr *)&address, addrlen,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 sock->file->f_flags);
1578out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001579 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580out:
1581 return err;
1582}
1583
1584/*
1585 * Get the local address ('name') of a socket object. Move the obtained
1586 * name to user space.
1587 */
1588
Heiko Carstens20f37032009-01-14 14:14:23 +01001589SYSCALL_DEFINE3(getsockname, int, fd, struct sockaddr __user *, usockaddr,
1590 int __user *, usockaddr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591{
1592 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001593 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001594 int len, err, fput_needed;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001595
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001596 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 if (!sock)
1598 goto out;
1599
1600 err = security_socket_getsockname(sock);
1601 if (err)
1602 goto out_put;
1603
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001604 err = sock->ops->getname(sock, (struct sockaddr *)&address, &len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 if (err)
1606 goto out_put;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001607 err = move_addr_to_user((struct sockaddr *)&address, len, usockaddr, usockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
1609out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001610 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611out:
1612 return err;
1613}
1614
1615/*
1616 * Get the remote address ('name') of a socket object. Move the obtained
1617 * name to user space.
1618 */
1619
Heiko Carstens20f37032009-01-14 14:14:23 +01001620SYSCALL_DEFINE3(getpeername, int, fd, struct sockaddr __user *, usockaddr,
1621 int __user *, usockaddr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622{
1623 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001624 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001625 int len, err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001627 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1628 if (sock != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 err = security_socket_getpeername(sock);
1630 if (err) {
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001631 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 return err;
1633 }
1634
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001635 err =
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001636 sock->ops->getname(sock, (struct sockaddr *)&address, &len,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001637 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 if (!err)
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001639 err = move_addr_to_user((struct sockaddr *)&address, len, usockaddr,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001640 usockaddr_len);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001641 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 }
1643 return err;
1644}
1645
1646/*
1647 * Send a datagram to a given address. We move the address into kernel
1648 * space and check the user space data area is readable before invoking
1649 * the protocol.
1650 */
1651
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001652SYSCALL_DEFINE6(sendto, int, fd, void __user *, buff, size_t, len,
1653 unsigned, flags, struct sockaddr __user *, addr,
1654 int, addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655{
1656 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001657 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 int err;
1659 struct msghdr msg;
1660 struct iovec iov;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001661 int fput_needed;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001662
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001663 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1664 if (!sock)
David S. Miller4387ff72007-02-08 15:06:08 -08001665 goto out;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001666
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001667 iov.iov_base = buff;
1668 iov.iov_len = len;
1669 msg.msg_name = NULL;
1670 msg.msg_iov = &iov;
1671 msg.msg_iovlen = 1;
1672 msg.msg_control = NULL;
1673 msg.msg_controllen = 0;
1674 msg.msg_namelen = 0;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001675 if (addr) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001676 err = move_addr_to_kernel(addr, addr_len, (struct sockaddr *)&address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 if (err < 0)
1678 goto out_put;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001679 msg.msg_name = (struct sockaddr *)&address;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001680 msg.msg_namelen = addr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 }
1682 if (sock->file->f_flags & O_NONBLOCK)
1683 flags |= MSG_DONTWAIT;
1684 msg.msg_flags = flags;
1685 err = sock_sendmsg(sock, &msg, len);
1686
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001687out_put:
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001688 fput_light(sock->file, fput_needed);
David S. Miller4387ff72007-02-08 15:06:08 -08001689out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 return err;
1691}
1692
1693/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001694 * Send a datagram down a socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 */
1696
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001697SYSCALL_DEFINE4(send, int, fd, void __user *, buff, size_t, len,
1698 unsigned, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699{
1700 return sys_sendto(fd, buff, len, flags, NULL, 0);
1701}
1702
1703/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001704 * Receive a frame from the socket and optionally record the address of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 * sender. We verify the buffers are writable and if needed move the
1706 * sender address from kernel to user space.
1707 */
1708
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001709SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, ubuf, size_t, size,
1710 unsigned, flags, struct sockaddr __user *, addr,
1711 int __user *, addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712{
1713 struct socket *sock;
1714 struct iovec iov;
1715 struct msghdr msg;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001716 struct sockaddr_storage address;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001717 int err, err2;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001718 int fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001720 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 if (!sock)
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001722 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001724 msg.msg_control = NULL;
1725 msg.msg_controllen = 0;
1726 msg.msg_iovlen = 1;
1727 msg.msg_iov = &iov;
1728 iov.iov_len = size;
1729 iov.iov_base = ubuf;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001730 msg.msg_name = (struct sockaddr *)&address;
1731 msg.msg_namelen = sizeof(address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 if (sock->file->f_flags & O_NONBLOCK)
1733 flags |= MSG_DONTWAIT;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001734 err = sock_recvmsg(sock, &msg, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001736 if (err >= 0 && addr != NULL) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001737 err2 = move_addr_to_user((struct sockaddr *)&address,
1738 msg.msg_namelen, addr, addr_len);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001739 if (err2 < 0)
1740 err = err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 }
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001742
1743 fput_light(sock->file, fput_needed);
David S. Miller4387ff72007-02-08 15:06:08 -08001744out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 return err;
1746}
1747
1748/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001749 * Receive a datagram from a socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 */
1751
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001752asmlinkage long sys_recv(int fd, void __user *ubuf, size_t size,
1753 unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754{
1755 return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
1756}
1757
1758/*
1759 * Set a socket option. Because we don't know the option lengths we have
1760 * to pass the user mode parameter for the protocols to sort out.
1761 */
1762
Heiko Carstens20f37032009-01-14 14:14:23 +01001763SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname,
1764 char __user *, optval, int, optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001766 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 struct socket *sock;
1768
1769 if (optlen < 0)
1770 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001771
1772 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1773 if (sock != NULL) {
1774 err = security_socket_setsockopt(sock, level, optname);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001775 if (err)
1776 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
1778 if (level == SOL_SOCKET)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001779 err =
1780 sock_setsockopt(sock, level, optname, optval,
1781 optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 else
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001783 err =
1784 sock->ops->setsockopt(sock, level, optname, optval,
1785 optlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001786out_put:
1787 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 }
1789 return err;
1790}
1791
1792/*
1793 * Get a socket option. Because we don't know the option lengths we have
1794 * to pass a user mode parameter for the protocols to sort out.
1795 */
1796
Heiko Carstens20f37032009-01-14 14:14:23 +01001797SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname,
1798 char __user *, optval, int __user *, optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001800 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 struct socket *sock;
1802
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001803 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1804 if (sock != NULL) {
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001805 err = security_socket_getsockopt(sock, level, optname);
1806 if (err)
1807 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
1809 if (level == SOL_SOCKET)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001810 err =
1811 sock_getsockopt(sock, level, optname, optval,
1812 optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 else
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001814 err =
1815 sock->ops->getsockopt(sock, level, optname, optval,
1816 optlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001817out_put:
1818 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 }
1820 return err;
1821}
1822
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823/*
1824 * Shutdown a socket.
1825 */
1826
Heiko Carstens754fe8d2009-01-14 14:14:09 +01001827SYSCALL_DEFINE2(shutdown, int, fd, int, how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001829 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 struct socket *sock;
1831
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001832 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1833 if (sock != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 err = security_socket_shutdown(sock, how);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001835 if (!err)
1836 err = sock->ops->shutdown(sock, how);
1837 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 }
1839 return err;
1840}
1841
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001842/* A couple of helpful macros for getting the address of the 32/64 bit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 * fields which are the same type (int / unsigned) on our platforms.
1844 */
1845#define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
1846#define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
1847#define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
1848
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849/*
1850 * BSD sendmsg interface
1851 */
1852
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001853SYSCALL_DEFINE3(sendmsg, int, fd, struct msghdr __user *, msg, unsigned, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854{
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001855 struct compat_msghdr __user *msg_compat =
1856 (struct compat_msghdr __user *)msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001858 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
Alex Williamsonb9d717a2005-09-26 14:28:02 -07001860 unsigned char ctl[sizeof(struct cmsghdr) + 20]
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001861 __attribute__ ((aligned(sizeof(__kernel_size_t))));
1862 /* 20 is size of ipv6_pktinfo */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 unsigned char *ctl_buf = ctl;
1864 struct msghdr msg_sys;
1865 int err, ctl_len, iov_size, total_len;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001866 int fput_needed;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001867
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 err = -EFAULT;
1869 if (MSG_CMSG_COMPAT & flags) {
1870 if (get_compat_msghdr(&msg_sys, msg_compat))
1871 return -EFAULT;
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001872 } else if (copy_from_user(&msg_sys, msg, sizeof(struct msghdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 return -EFAULT;
1874
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001875 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001876 if (!sock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 goto out;
1878
1879 /* do not move before msg_sys is valid */
1880 err = -EMSGSIZE;
1881 if (msg_sys.msg_iovlen > UIO_MAXIOV)
1882 goto out_put;
1883
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001884 /* Check whether to allocate the iovec area */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 err = -ENOMEM;
1886 iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
1887 if (msg_sys.msg_iovlen > UIO_FASTIOV) {
1888 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
1889 if (!iov)
1890 goto out_put;
1891 }
1892
1893 /* This will also move the address data into kernel space */
1894 if (MSG_CMSG_COMPAT & flags) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001895 err = verify_compat_iovec(&msg_sys, iov,
1896 (struct sockaddr *)&address,
1897 VERIFY_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 } else
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001899 err = verify_iovec(&msg_sys, iov,
1900 (struct sockaddr *)&address,
1901 VERIFY_READ);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001902 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 goto out_freeiov;
1904 total_len = err;
1905
1906 err = -ENOBUFS;
1907
1908 if (msg_sys.msg_controllen > INT_MAX)
1909 goto out_freeiov;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001910 ctl_len = msg_sys.msg_controllen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001912 err =
1913 cmsghdr_from_user_compat_to_kern(&msg_sys, sock->sk, ctl,
1914 sizeof(ctl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 if (err)
1916 goto out_freeiov;
1917 ctl_buf = msg_sys.msg_control;
Al Viro8920e8f2005-09-07 18:28:51 -07001918 ctl_len = msg_sys.msg_controllen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 } else if (ctl_len) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001920 if (ctl_len > sizeof(ctl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001922 if (ctl_buf == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 goto out_freeiov;
1924 }
1925 err = -EFAULT;
1926 /*
1927 * Careful! Before this, msg_sys.msg_control contains a user pointer.
1928 * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
1929 * checking falls down on this.
1930 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001931 if (copy_from_user(ctl_buf, (void __user *)msg_sys.msg_control,
1932 ctl_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 goto out_freectl;
1934 msg_sys.msg_control = ctl_buf;
1935 }
1936 msg_sys.msg_flags = flags;
1937
1938 if (sock->file->f_flags & O_NONBLOCK)
1939 msg_sys.msg_flags |= MSG_DONTWAIT;
1940 err = sock_sendmsg(sock, &msg_sys, total_len);
1941
1942out_freectl:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001943 if (ctl_buf != ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 sock_kfree_s(sock->sk, ctl_buf, ctl_len);
1945out_freeiov:
1946 if (iov != iovstack)
1947 sock_kfree_s(sock->sk, iov, iov_size);
1948out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001949 fput_light(sock->file, fput_needed);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001950out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 return err;
1952}
1953
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07001954static int __sys_recvmsg(struct socket *sock, struct msghdr __user *msg,
1955 struct msghdr *msg_sys, unsigned flags, int nosec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956{
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001957 struct compat_msghdr __user *msg_compat =
1958 (struct compat_msghdr __user *)msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 struct iovec iovstack[UIO_FASTIOV];
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001960 struct iovec *iov = iovstack;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 unsigned long cmsg_ptr;
1962 int err, iov_size, total_len, len;
1963
1964 /* kernel mode address */
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001965 struct sockaddr_storage addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
1967 /* user mode address pointers */
1968 struct sockaddr __user *uaddr;
1969 int __user *uaddr_len;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001970
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 if (MSG_CMSG_COMPAT & flags) {
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07001972 if (get_compat_msghdr(msg_sys, msg_compat))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 return -EFAULT;
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001974 } else if (copy_from_user(msg_sys, msg, sizeof(struct msghdr)))
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001975 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 err = -EMSGSIZE;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07001978 if (msg_sys->msg_iovlen > UIO_MAXIOV)
1979 goto out;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001980
1981 /* Check whether to allocate the iovec area */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 err = -ENOMEM;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07001983 iov_size = msg_sys->msg_iovlen * sizeof(struct iovec);
1984 if (msg_sys->msg_iovlen > UIO_FASTIOV) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
1986 if (!iov)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07001987 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 }
1989
1990 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001991 * Save the user-mode address (verify_iovec will change the
1992 * kernel msghdr to use the kernel address space)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001994
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07001995 uaddr = (__force void __user *)msg_sys->msg_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 uaddr_len = COMPAT_NAMELEN(msg);
1997 if (MSG_CMSG_COMPAT & flags) {
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07001998 err = verify_compat_iovec(msg_sys, iov,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001999 (struct sockaddr *)&addr,
2000 VERIFY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 } else
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002002 err = verify_iovec(msg_sys, iov,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07002003 (struct sockaddr *)&addr,
2004 VERIFY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 if (err < 0)
2006 goto out_freeiov;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002007 total_len = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002009 cmsg_ptr = (unsigned long)msg_sys->msg_control;
2010 msg_sys->msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002011
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 if (sock->file->f_flags & O_NONBLOCK)
2013 flags |= MSG_DONTWAIT;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002014 err = (nosec ? sock_recvmsg_nosec : sock_recvmsg)(sock, msg_sys,
2015 total_len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 if (err < 0)
2017 goto out_freeiov;
2018 len = err;
2019
2020 if (uaddr != NULL) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07002021 err = move_addr_to_user((struct sockaddr *)&addr,
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002022 msg_sys->msg_namelen, uaddr,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002023 uaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 if (err < 0)
2025 goto out_freeiov;
2026 }
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002027 err = __put_user((msg_sys->msg_flags & ~MSG_CMSG_COMPAT),
David S. Miller37f7f422005-09-16 16:51:01 -07002028 COMPAT_FLAGS(msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 if (err)
2030 goto out_freeiov;
2031 if (MSG_CMSG_COMPAT & flags)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002032 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 &msg_compat->msg_controllen);
2034 else
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002035 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 &msg->msg_controllen);
2037 if (err)
2038 goto out_freeiov;
2039 err = len;
2040
2041out_freeiov:
2042 if (iov != iovstack)
2043 sock_kfree_s(sock->sk, iov, iov_size);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002044out:
2045 return err;
2046}
2047
2048/*
2049 * BSD recvmsg interface
2050 */
2051
2052SYSCALL_DEFINE3(recvmsg, int, fd, struct msghdr __user *, msg,
2053 unsigned int, flags)
2054{
2055 int fput_needed, err;
2056 struct msghdr msg_sys;
2057 struct socket *sock = sockfd_lookup_light(fd, &err, &fput_needed);
2058
2059 if (!sock)
2060 goto out;
2061
2062 err = __sys_recvmsg(sock, msg, &msg_sys, flags, 0);
2063
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002064 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065out:
2066 return err;
2067}
2068
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002069/*
2070 * Linux recvmmsg interface
2071 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002073int __sys_recvmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
2074 unsigned int flags, struct timespec *timeout)
2075{
2076 int fput_needed, err, datagrams;
2077 struct socket *sock;
2078 struct mmsghdr __user *entry;
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002079 struct compat_mmsghdr __user *compat_entry;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002080 struct msghdr msg_sys;
2081 struct timespec end_time;
2082
2083 if (timeout &&
2084 poll_select_set_timeout(&end_time, timeout->tv_sec,
2085 timeout->tv_nsec))
2086 return -EINVAL;
2087
2088 datagrams = 0;
2089
2090 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2091 if (!sock)
2092 return err;
2093
2094 err = sock_error(sock->sk);
2095 if (err)
2096 goto out_put;
2097
2098 entry = mmsg;
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002099 compat_entry = (struct compat_mmsghdr __user *)mmsg;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002100
2101 while (datagrams < vlen) {
2102 /*
2103 * No need to ask LSM for more than the first datagram.
2104 */
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002105 if (MSG_CMSG_COMPAT & flags) {
2106 err = __sys_recvmsg(sock, (struct msghdr __user *)compat_entry,
2107 &msg_sys, flags, datagrams);
2108 if (err < 0)
2109 break;
2110 err = __put_user(err, &compat_entry->msg_len);
2111 ++compat_entry;
2112 } else {
2113 err = __sys_recvmsg(sock, (struct msghdr __user *)entry,
2114 &msg_sys, flags, datagrams);
2115 if (err < 0)
2116 break;
2117 err = put_user(err, &entry->msg_len);
2118 ++entry;
2119 }
2120
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002121 if (err)
2122 break;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002123 ++datagrams;
2124
Brandon L Black71c5c152010-03-26 16:18:03 +00002125 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
2126 if (flags & MSG_WAITFORONE)
2127 flags |= MSG_DONTWAIT;
2128
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002129 if (timeout) {
2130 ktime_get_ts(timeout);
2131 *timeout = timespec_sub(end_time, *timeout);
2132 if (timeout->tv_sec < 0) {
2133 timeout->tv_sec = timeout->tv_nsec = 0;
2134 break;
2135 }
2136
2137 /* Timeout, return less than vlen datagrams */
2138 if (timeout->tv_nsec == 0 && timeout->tv_sec == 0)
2139 break;
2140 }
2141
2142 /* Out of band data, return right away */
2143 if (msg_sys.msg_flags & MSG_OOB)
2144 break;
2145 }
2146
2147out_put:
2148 fput_light(sock->file, fput_needed);
2149
2150 if (err == 0)
2151 return datagrams;
2152
2153 if (datagrams != 0) {
2154 /*
2155 * We may return less entries than requested (vlen) if the
2156 * sock is non block and there aren't enough datagrams...
2157 */
2158 if (err != -EAGAIN) {
2159 /*
2160 * ... or if recvmsg returns an error after we
2161 * received some datagrams, where we record the
2162 * error to return on the next call or if the
2163 * app asks about it using getsockopt(SO_ERROR).
2164 */
2165 sock->sk->sk_err = -err;
2166 }
2167
2168 return datagrams;
2169 }
2170
2171 return err;
2172}
2173
2174SYSCALL_DEFINE5(recvmmsg, int, fd, struct mmsghdr __user *, mmsg,
2175 unsigned int, vlen, unsigned int, flags,
2176 struct timespec __user *, timeout)
2177{
2178 int datagrams;
2179 struct timespec timeout_sys;
2180
2181 if (!timeout)
2182 return __sys_recvmmsg(fd, mmsg, vlen, flags, NULL);
2183
2184 if (copy_from_user(&timeout_sys, timeout, sizeof(timeout_sys)))
2185 return -EFAULT;
2186
2187 datagrams = __sys_recvmmsg(fd, mmsg, vlen, flags, &timeout_sys);
2188
2189 if (datagrams > 0 &&
2190 copy_to_user(timeout, &timeout_sys, sizeof(timeout_sys)))
2191 datagrams = -EFAULT;
2192
2193 return datagrams;
2194}
2195
2196#ifdef __ARCH_WANT_SYS_SOCKETCALL
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197/* Argument list sizes for sys_socketcall */
2198#define AL(x) ((x) * sizeof(unsigned long))
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002199static const unsigned char nargs[20] = {
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002200 AL(0), AL(3), AL(3), AL(3), AL(2), AL(3),
2201 AL(3), AL(3), AL(4), AL(4), AL(4), AL(6),
2202 AL(6), AL(2), AL(5), AL(5), AL(3), AL(3),
2203 AL(4), AL(5)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002204};
2205
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206#undef AL
2207
2208/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002209 * System call vectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 *
2211 * Argument checking cleaned up. Saved 20% in size.
2212 * This function doesn't need to set the kernel lock because
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002213 * it is set by the callees.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 */
2215
Heiko Carstens3e0fa652009-01-14 14:14:24 +01002216SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217{
2218 unsigned long a[6];
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002219 unsigned long a0, a1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 int err;
Arjan van de Ven47379052009-09-28 12:57:44 -07002221 unsigned int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002223 if (call < 1 || call > SYS_RECVMMSG)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 return -EINVAL;
2225
Arjan van de Ven47379052009-09-28 12:57:44 -07002226 len = nargs[call];
2227 if (len > sizeof(a))
2228 return -EINVAL;
2229
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 /* copy_from_user should be SMP safe. */
Arjan van de Ven47379052009-09-28 12:57:44 -07002231 if (copy_from_user(a, args, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 return -EFAULT;
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002233
Al Virof3298dc2008-12-10 03:16:51 -05002234 audit_socketcall(nargs[call] / sizeof(unsigned long), a);
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002235
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002236 a0 = a[0];
2237 a1 = a[1];
2238
2239 switch (call) {
2240 case SYS_SOCKET:
2241 err = sys_socket(a0, a1, a[2]);
2242 break;
2243 case SYS_BIND:
2244 err = sys_bind(a0, (struct sockaddr __user *)a1, a[2]);
2245 break;
2246 case SYS_CONNECT:
2247 err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
2248 break;
2249 case SYS_LISTEN:
2250 err = sys_listen(a0, a1);
2251 break;
2252 case SYS_ACCEPT:
Ulrich Drepperde11def2008-11-19 15:36:14 -08002253 err = sys_accept4(a0, (struct sockaddr __user *)a1,
2254 (int __user *)a[2], 0);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002255 break;
2256 case SYS_GETSOCKNAME:
2257 err =
2258 sys_getsockname(a0, (struct sockaddr __user *)a1,
2259 (int __user *)a[2]);
2260 break;
2261 case SYS_GETPEERNAME:
2262 err =
2263 sys_getpeername(a0, (struct sockaddr __user *)a1,
2264 (int __user *)a[2]);
2265 break;
2266 case SYS_SOCKETPAIR:
2267 err = sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
2268 break;
2269 case SYS_SEND:
2270 err = sys_send(a0, (void __user *)a1, a[2], a[3]);
2271 break;
2272 case SYS_SENDTO:
2273 err = sys_sendto(a0, (void __user *)a1, a[2], a[3],
2274 (struct sockaddr __user *)a[4], a[5]);
2275 break;
2276 case SYS_RECV:
2277 err = sys_recv(a0, (void __user *)a1, a[2], a[3]);
2278 break;
2279 case SYS_RECVFROM:
2280 err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
2281 (struct sockaddr __user *)a[4],
2282 (int __user *)a[5]);
2283 break;
2284 case SYS_SHUTDOWN:
2285 err = sys_shutdown(a0, a1);
2286 break;
2287 case SYS_SETSOCKOPT:
2288 err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]);
2289 break;
2290 case SYS_GETSOCKOPT:
2291 err =
2292 sys_getsockopt(a0, a1, a[2], (char __user *)a[3],
2293 (int __user *)a[4]);
2294 break;
2295 case SYS_SENDMSG:
2296 err = sys_sendmsg(a0, (struct msghdr __user *)a1, a[2]);
2297 break;
2298 case SYS_RECVMSG:
2299 err = sys_recvmsg(a0, (struct msghdr __user *)a1, a[2]);
2300 break;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002301 case SYS_RECVMMSG:
2302 err = sys_recvmmsg(a0, (struct mmsghdr __user *)a1, a[2], a[3],
2303 (struct timespec __user *)a[4]);
2304 break;
Ulrich Drepperde11def2008-11-19 15:36:14 -08002305 case SYS_ACCEPT4:
2306 err = sys_accept4(a0, (struct sockaddr __user *)a1,
2307 (int __user *)a[2], a[3]);
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07002308 break;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002309 default:
2310 err = -EINVAL;
2311 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 }
2313 return err;
2314}
2315
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002316#endif /* __ARCH_WANT_SYS_SOCKETCALL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002318/**
2319 * sock_register - add a socket protocol handler
2320 * @ops: description of protocol
2321 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 * This function is called by a protocol handler that wants to
2323 * advertise its address family, and have it linked into the
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002324 * socket interface. The value ops->family coresponds to the
2325 * socket system call protocol family.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 */
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002327int sock_register(const struct net_proto_family *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328{
2329 int err;
2330
2331 if (ops->family >= NPROTO) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002332 printk(KERN_CRIT "protocol %d >= NPROTO(%d)\n", ops->family,
2333 NPROTO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 return -ENOBUFS;
2335 }
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002336
2337 spin_lock(&net_family_lock);
2338 if (net_families[ops->family])
2339 err = -EEXIST;
2340 else {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002341 net_families[ops->family] = ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 err = 0;
2343 }
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002344 spin_unlock(&net_family_lock);
2345
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002346 printk(KERN_INFO "NET: Registered protocol family %d\n", ops->family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 return err;
2348}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002349EXPORT_SYMBOL(sock_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002351/**
2352 * sock_unregister - remove a protocol handler
2353 * @family: protocol family to remove
2354 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 * This function is called by a protocol handler that wants to
2356 * remove its address family, and have it unlinked from the
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002357 * new socket creation.
2358 *
2359 * If protocol handler is a module, then it can use module reference
2360 * counts to protect against new references. If protocol handler is not
2361 * a module then it needs to provide its own protection in
2362 * the ops->create routine.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 */
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002364void sock_unregister(int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365{
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002366 BUG_ON(family < 0 || family >= NPROTO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002368 spin_lock(&net_family_lock);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002369 net_families[family] = NULL;
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002370 spin_unlock(&net_family_lock);
2371
2372 synchronize_rcu();
2373
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002374 printk(KERN_INFO "NET: Unregistered protocol family %d\n", family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002376EXPORT_SYMBOL(sock_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377
Andi Kleen77d76ea2005-12-22 12:43:42 -08002378static int __init sock_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379{
2380 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002381 * Initialize sock SLAB cache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002383
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 sk_init();
2385
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002387 * Initialize skbuff SLAB cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 */
2389 skb_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390
2391 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002392 * Initialize the protocols module.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 */
2394
2395 init_inodecache();
2396 register_filesystem(&sock_fs_type);
2397 sock_mnt = kern_mount(&sock_fs_type);
Andi Kleen77d76ea2005-12-22 12:43:42 -08002398
2399 /* The real protocol initialization is performed in later initcalls.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 */
2401
2402#ifdef CONFIG_NETFILTER
2403 netfilter_init();
2404#endif
David S. Millercbeb3212005-12-22 12:58:55 -08002405
2406 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407}
2408
Andi Kleen77d76ea2005-12-22 12:43:42 -08002409core_initcall(sock_init); /* early initcall */
2410
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411#ifdef CONFIG_PROC_FS
2412void socket_seq_show(struct seq_file *seq)
2413{
2414 int cpu;
2415 int counter = 0;
2416
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -07002417 for_each_possible_cpu(cpu)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002418 counter += per_cpu(sockets_in_use, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419
2420 /* It can be negative, by the way. 8) */
2421 if (counter < 0)
2422 counter = 0;
2423
2424 seq_printf(seq, "sockets: used %d\n", counter);
2425}
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002426#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
Shaun Pereira89bbfc92006-03-21 23:58:08 -08002428#ifdef CONFIG_COMPAT
Arnd Bergmann6b960182009-11-06 23:10:54 -08002429static int do_siocgstamp(struct net *net, struct socket *sock,
2430 unsigned int cmd, struct compat_timeval __user *up)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002431{
Arnd Bergmann7a229382009-11-06 23:00:29 -08002432 mm_segment_t old_fs = get_fs();
2433 struct timeval ktv;
2434 int err;
2435
2436 set_fs(KERNEL_DS);
Arnd Bergmann6b960182009-11-06 23:10:54 -08002437 err = sock_do_ioctl(net, sock, cmd, (unsigned long)&ktv);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002438 set_fs(old_fs);
2439 if (!err) {
2440 err = put_user(ktv.tv_sec, &up->tv_sec);
2441 err |= __put_user(ktv.tv_usec, &up->tv_usec);
2442 }
2443 return err;
2444}
2445
Arnd Bergmann6b960182009-11-06 23:10:54 -08002446static int do_siocgstampns(struct net *net, struct socket *sock,
2447 unsigned int cmd, struct compat_timespec __user *up)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002448{
Arnd Bergmann7a229382009-11-06 23:00:29 -08002449 mm_segment_t old_fs = get_fs();
2450 struct timespec kts;
2451 int err;
2452
2453 set_fs(KERNEL_DS);
Arnd Bergmann6b960182009-11-06 23:10:54 -08002454 err = sock_do_ioctl(net, sock, cmd, (unsigned long)&kts);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002455 set_fs(old_fs);
2456 if (!err) {
2457 err = put_user(kts.tv_sec, &up->tv_sec);
2458 err |= __put_user(kts.tv_nsec, &up->tv_nsec);
2459 }
2460 return err;
2461}
2462
Arnd Bergmann6b960182009-11-06 23:10:54 -08002463static int dev_ifname32(struct net *net, struct compat_ifreq __user *uifr32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002464{
2465 struct ifreq __user *uifr;
2466 int err;
2467
2468 uifr = compat_alloc_user_space(sizeof(struct ifreq));
Arnd Bergmann6b960182009-11-06 23:10:54 -08002469 if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002470 return -EFAULT;
2471
Arnd Bergmann6b960182009-11-06 23:10:54 -08002472 err = dev_ioctl(net, SIOCGIFNAME, uifr);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002473 if (err)
2474 return err;
2475
Arnd Bergmann6b960182009-11-06 23:10:54 -08002476 if (copy_in_user(uifr32, uifr, sizeof(struct compat_ifreq)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002477 return -EFAULT;
2478
2479 return 0;
2480}
2481
Arnd Bergmann6b960182009-11-06 23:10:54 -08002482static int dev_ifconf(struct net *net, struct compat_ifconf __user *uifc32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002483{
Arnd Bergmann6b960182009-11-06 23:10:54 -08002484 struct compat_ifconf ifc32;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002485 struct ifconf ifc;
2486 struct ifconf __user *uifc;
Arnd Bergmann6b960182009-11-06 23:10:54 -08002487 struct compat_ifreq __user *ifr32;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002488 struct ifreq __user *ifr;
2489 unsigned int i, j;
2490 int err;
2491
Arnd Bergmann6b960182009-11-06 23:10:54 -08002492 if (copy_from_user(&ifc32, uifc32, sizeof(struct compat_ifconf)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002493 return -EFAULT;
2494
2495 if (ifc32.ifcbuf == 0) {
2496 ifc32.ifc_len = 0;
2497 ifc.ifc_len = 0;
2498 ifc.ifc_req = NULL;
2499 uifc = compat_alloc_user_space(sizeof(struct ifconf));
2500 } else {
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002501 size_t len = ((ifc32.ifc_len / sizeof(struct compat_ifreq)) + 1) *
2502 sizeof(struct ifreq);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002503 uifc = compat_alloc_user_space(sizeof(struct ifconf) + len);
2504 ifc.ifc_len = len;
2505 ifr = ifc.ifc_req = (void __user *)(uifc + 1);
2506 ifr32 = compat_ptr(ifc32.ifcbuf);
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002507 for (i = 0; i < ifc32.ifc_len; i += sizeof(struct compat_ifreq)) {
Arnd Bergmann6b960182009-11-06 23:10:54 -08002508 if (copy_in_user(ifr, ifr32, sizeof(struct compat_ifreq)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002509 return -EFAULT;
2510 ifr++;
2511 ifr32++;
2512 }
2513 }
2514 if (copy_to_user(uifc, &ifc, sizeof(struct ifconf)))
2515 return -EFAULT;
2516
Arnd Bergmann6b960182009-11-06 23:10:54 -08002517 err = dev_ioctl(net, SIOCGIFCONF, uifc);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002518 if (err)
2519 return err;
2520
2521 if (copy_from_user(&ifc, uifc, sizeof(struct ifconf)))
2522 return -EFAULT;
2523
2524 ifr = ifc.ifc_req;
2525 ifr32 = compat_ptr(ifc32.ifcbuf);
2526 for (i = 0, j = 0;
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002527 i + sizeof(struct compat_ifreq) <= ifc32.ifc_len && j < ifc.ifc_len;
2528 i += sizeof(struct compat_ifreq), j += sizeof(struct ifreq)) {
2529 if (copy_in_user(ifr32, ifr, sizeof(struct compat_ifreq)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002530 return -EFAULT;
2531 ifr32++;
2532 ifr++;
2533 }
2534
2535 if (ifc32.ifcbuf == 0) {
2536 /* Translate from 64-bit structure multiple to
2537 * a 32-bit one.
2538 */
2539 i = ifc.ifc_len;
Arnd Bergmann6b960182009-11-06 23:10:54 -08002540 i = ((i / sizeof(struct ifreq)) * sizeof(struct compat_ifreq));
Arnd Bergmann7a229382009-11-06 23:00:29 -08002541 ifc32.ifc_len = i;
2542 } else {
2543 ifc32.ifc_len = i;
2544 }
Arnd Bergmann6b960182009-11-06 23:10:54 -08002545 if (copy_to_user(uifc32, &ifc32, sizeof(struct compat_ifconf)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002546 return -EFAULT;
2547
2548 return 0;
2549}
2550
Arnd Bergmann6b960182009-11-06 23:10:54 -08002551static int ethtool_ioctl(struct net *net, struct compat_ifreq __user *ifr32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002552{
2553 struct ifreq __user *ifr;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002554 u32 data;
2555 void __user *datap;
2556
2557 ifr = compat_alloc_user_space(sizeof(*ifr));
Arnd Bergmann7a229382009-11-06 23:00:29 -08002558
2559 if (copy_in_user(&ifr->ifr_name, &ifr32->ifr_name, IFNAMSIZ))
2560 return -EFAULT;
2561
2562 if (get_user(data, &ifr32->ifr_ifru.ifru_data))
2563 return -EFAULT;
2564
2565 datap = compat_ptr(data);
2566 if (put_user(datap, &ifr->ifr_ifru.ifru_data))
2567 return -EFAULT;
2568
Arnd Bergmann6b960182009-11-06 23:10:54 -08002569 return dev_ioctl(net, SIOCETHTOOL, ifr);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002570}
2571
Arnd Bergmann7a50a242009-11-08 20:57:03 -08002572static int compat_siocwandev(struct net *net, struct compat_ifreq __user *uifr32)
2573{
2574 void __user *uptr;
2575 compat_uptr_t uptr32;
2576 struct ifreq __user *uifr;
2577
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002578 uifr = compat_alloc_user_space(sizeof(*uifr));
Arnd Bergmann7a50a242009-11-08 20:57:03 -08002579 if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
2580 return -EFAULT;
2581
2582 if (get_user(uptr32, &uifr32->ifr_settings.ifs_ifsu))
2583 return -EFAULT;
2584
2585 uptr = compat_ptr(uptr32);
2586
2587 if (put_user(uptr, &uifr->ifr_settings.ifs_ifsu.raw_hdlc))
2588 return -EFAULT;
2589
2590 return dev_ioctl(net, SIOCWANDEV, uifr);
2591}
2592
Arnd Bergmann6b960182009-11-06 23:10:54 -08002593static int bond_ioctl(struct net *net, unsigned int cmd,
2594 struct compat_ifreq __user *ifr32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002595{
2596 struct ifreq kifr;
2597 struct ifreq __user *uifr;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002598 mm_segment_t old_fs;
2599 int err;
2600 u32 data;
2601 void __user *datap;
2602
2603 switch (cmd) {
2604 case SIOCBONDENSLAVE:
2605 case SIOCBONDRELEASE:
2606 case SIOCBONDSETHWADDR:
2607 case SIOCBONDCHANGEACTIVE:
Arnd Bergmann6b960182009-11-06 23:10:54 -08002608 if (copy_from_user(&kifr, ifr32, sizeof(struct compat_ifreq)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002609 return -EFAULT;
2610
2611 old_fs = get_fs();
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002612 set_fs(KERNEL_DS);
Arnd Bergmann6b960182009-11-06 23:10:54 -08002613 err = dev_ioctl(net, cmd, &kifr);
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002614 set_fs(old_fs);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002615
2616 return err;
2617 case SIOCBONDSLAVEINFOQUERY:
2618 case SIOCBONDINFOQUERY:
2619 uifr = compat_alloc_user_space(sizeof(*uifr));
2620 if (copy_in_user(&uifr->ifr_name, &ifr32->ifr_name, IFNAMSIZ))
2621 return -EFAULT;
2622
2623 if (get_user(data, &ifr32->ifr_ifru.ifru_data))
2624 return -EFAULT;
2625
2626 datap = compat_ptr(data);
2627 if (put_user(datap, &uifr->ifr_ifru.ifru_data))
2628 return -EFAULT;
2629
Arnd Bergmann6b960182009-11-06 23:10:54 -08002630 return dev_ioctl(net, cmd, uifr);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002631 default:
2632 return -EINVAL;
Joe Perchesccbd6a52010-05-14 10:58:26 +00002633 }
Arnd Bergmann7a229382009-11-06 23:00:29 -08002634}
2635
Arnd Bergmann6b960182009-11-06 23:10:54 -08002636static int siocdevprivate_ioctl(struct net *net, unsigned int cmd,
2637 struct compat_ifreq __user *u_ifreq32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002638{
2639 struct ifreq __user *u_ifreq64;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002640 char tmp_buf[IFNAMSIZ];
2641 void __user *data64;
2642 u32 data32;
2643
2644 if (copy_from_user(&tmp_buf[0], &(u_ifreq32->ifr_ifrn.ifrn_name[0]),
2645 IFNAMSIZ))
2646 return -EFAULT;
2647 if (__get_user(data32, &u_ifreq32->ifr_ifru.ifru_data))
2648 return -EFAULT;
2649 data64 = compat_ptr(data32);
2650
2651 u_ifreq64 = compat_alloc_user_space(sizeof(*u_ifreq64));
2652
2653 /* Don't check these user accesses, just let that get trapped
2654 * in the ioctl handler instead.
2655 */
2656 if (copy_to_user(&u_ifreq64->ifr_ifrn.ifrn_name[0], &tmp_buf[0],
2657 IFNAMSIZ))
2658 return -EFAULT;
2659 if (__put_user(data64, &u_ifreq64->ifr_ifru.ifru_data))
2660 return -EFAULT;
2661
Arnd Bergmann6b960182009-11-06 23:10:54 -08002662 return dev_ioctl(net, cmd, u_ifreq64);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002663}
2664
Arnd Bergmann6b960182009-11-06 23:10:54 -08002665static int dev_ifsioc(struct net *net, struct socket *sock,
2666 unsigned int cmd, struct compat_ifreq __user *uifr32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002667{
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00002668 struct ifreq __user *uifr;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002669 int err;
2670
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00002671 uifr = compat_alloc_user_space(sizeof(*uifr));
2672 if (copy_in_user(uifr, uifr32, sizeof(*uifr32)))
2673 return -EFAULT;
2674
2675 err = sock_do_ioctl(net, sock, cmd, (unsigned long)uifr);
2676
Arnd Bergmann7a229382009-11-06 23:00:29 -08002677 if (!err) {
2678 switch (cmd) {
2679 case SIOCGIFFLAGS:
2680 case SIOCGIFMETRIC:
2681 case SIOCGIFMTU:
2682 case SIOCGIFMEM:
2683 case SIOCGIFHWADDR:
2684 case SIOCGIFINDEX:
2685 case SIOCGIFADDR:
2686 case SIOCGIFBRDADDR:
2687 case SIOCGIFDSTADDR:
2688 case SIOCGIFNETMASK:
Arnd Bergmannfab25322009-11-08 20:56:21 -08002689 case SIOCGIFPFLAGS:
Arnd Bergmann7a229382009-11-06 23:00:29 -08002690 case SIOCGIFTXQLEN:
Arnd Bergmannfab25322009-11-08 20:56:21 -08002691 case SIOCGMIIPHY:
2692 case SIOCGMIIREG:
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00002693 if (copy_in_user(uifr32, uifr, sizeof(*uifr32)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002694 err = -EFAULT;
2695 break;
2696 }
2697 }
2698 return err;
2699}
2700
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00002701static int compat_sioc_ifmap(struct net *net, unsigned int cmd,
2702 struct compat_ifreq __user *uifr32)
2703{
2704 struct ifreq ifr;
2705 struct compat_ifmap __user *uifmap32;
2706 mm_segment_t old_fs;
2707 int err;
2708
2709 uifmap32 = &uifr32->ifr_ifru.ifru_map;
2710 err = copy_from_user(&ifr, uifr32, sizeof(ifr.ifr_name));
2711 err |= __get_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
2712 err |= __get_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
2713 err |= __get_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
2714 err |= __get_user(ifr.ifr_map.irq, &uifmap32->irq);
2715 err |= __get_user(ifr.ifr_map.dma, &uifmap32->dma);
2716 err |= __get_user(ifr.ifr_map.port, &uifmap32->port);
2717 if (err)
2718 return -EFAULT;
2719
2720 old_fs = get_fs();
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002721 set_fs(KERNEL_DS);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00002722 err = dev_ioctl(net, cmd, (void __user *)&ifr);
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002723 set_fs(old_fs);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00002724
2725 if (cmd == SIOCGIFMAP && !err) {
2726 err = copy_to_user(uifr32, &ifr, sizeof(ifr.ifr_name));
2727 err |= __put_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
2728 err |= __put_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
2729 err |= __put_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
2730 err |= __put_user(ifr.ifr_map.irq, &uifmap32->irq);
2731 err |= __put_user(ifr.ifr_map.dma, &uifmap32->dma);
2732 err |= __put_user(ifr.ifr_map.port, &uifmap32->port);
2733 if (err)
2734 err = -EFAULT;
2735 }
2736 return err;
2737}
2738
2739static int compat_siocshwtstamp(struct net *net, struct compat_ifreq __user *uifr32)
2740{
2741 void __user *uptr;
2742 compat_uptr_t uptr32;
2743 struct ifreq __user *uifr;
2744
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002745 uifr = compat_alloc_user_space(sizeof(*uifr));
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00002746 if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
2747 return -EFAULT;
2748
2749 if (get_user(uptr32, &uifr32->ifr_data))
2750 return -EFAULT;
2751
2752 uptr = compat_ptr(uptr32);
2753
2754 if (put_user(uptr, &uifr->ifr_data))
2755 return -EFAULT;
2756
2757 return dev_ioctl(net, SIOCSHWTSTAMP, uifr);
2758}
2759
Arnd Bergmann7a229382009-11-06 23:00:29 -08002760struct rtentry32 {
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002761 u32 rt_pad1;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002762 struct sockaddr rt_dst; /* target address */
2763 struct sockaddr rt_gateway; /* gateway addr (RTF_GATEWAY) */
2764 struct sockaddr rt_genmask; /* target network mask (IP) */
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002765 unsigned short rt_flags;
2766 short rt_pad2;
2767 u32 rt_pad3;
2768 unsigned char rt_tos;
2769 unsigned char rt_class;
2770 short rt_pad4;
2771 short rt_metric; /* +1 for binary compatibility! */
Arnd Bergmann7a229382009-11-06 23:00:29 -08002772 /* char * */ u32 rt_dev; /* forcing the device at add */
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002773 u32 rt_mtu; /* per route MTU/Window */
2774 u32 rt_window; /* Window clamping */
Arnd Bergmann7a229382009-11-06 23:00:29 -08002775 unsigned short rt_irtt; /* Initial RTT */
2776};
2777
2778struct in6_rtmsg32 {
2779 struct in6_addr rtmsg_dst;
2780 struct in6_addr rtmsg_src;
2781 struct in6_addr rtmsg_gateway;
2782 u32 rtmsg_type;
2783 u16 rtmsg_dst_len;
2784 u16 rtmsg_src_len;
2785 u32 rtmsg_metric;
2786 u32 rtmsg_info;
2787 u32 rtmsg_flags;
2788 s32 rtmsg_ifindex;
2789};
2790
Arnd Bergmann6b960182009-11-06 23:10:54 -08002791static int routing_ioctl(struct net *net, struct socket *sock,
2792 unsigned int cmd, void __user *argp)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002793{
2794 int ret;
2795 void *r = NULL;
2796 struct in6_rtmsg r6;
2797 struct rtentry r4;
2798 char devname[16];
2799 u32 rtdev;
2800 mm_segment_t old_fs = get_fs();
2801
Arnd Bergmann6b960182009-11-06 23:10:54 -08002802 if (sock && sock->sk && sock->sk->sk_family == AF_INET6) { /* ipv6 */
2803 struct in6_rtmsg32 __user *ur6 = argp;
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002804 ret = copy_from_user(&r6.rtmsg_dst, &(ur6->rtmsg_dst),
Arnd Bergmann7a229382009-11-06 23:00:29 -08002805 3 * sizeof(struct in6_addr));
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002806 ret |= __get_user(r6.rtmsg_type, &(ur6->rtmsg_type));
2807 ret |= __get_user(r6.rtmsg_dst_len, &(ur6->rtmsg_dst_len));
2808 ret |= __get_user(r6.rtmsg_src_len, &(ur6->rtmsg_src_len));
2809 ret |= __get_user(r6.rtmsg_metric, &(ur6->rtmsg_metric));
2810 ret |= __get_user(r6.rtmsg_info, &(ur6->rtmsg_info));
2811 ret |= __get_user(r6.rtmsg_flags, &(ur6->rtmsg_flags));
2812 ret |= __get_user(r6.rtmsg_ifindex, &(ur6->rtmsg_ifindex));
Arnd Bergmann7a229382009-11-06 23:00:29 -08002813
2814 r = (void *) &r6;
2815 } else { /* ipv4 */
Arnd Bergmann6b960182009-11-06 23:10:54 -08002816 struct rtentry32 __user *ur4 = argp;
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002817 ret = copy_from_user(&r4.rt_dst, &(ur4->rt_dst),
Arnd Bergmann7a229382009-11-06 23:00:29 -08002818 3 * sizeof(struct sockaddr));
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002819 ret |= __get_user(r4.rt_flags, &(ur4->rt_flags));
2820 ret |= __get_user(r4.rt_metric, &(ur4->rt_metric));
2821 ret |= __get_user(r4.rt_mtu, &(ur4->rt_mtu));
2822 ret |= __get_user(r4.rt_window, &(ur4->rt_window));
2823 ret |= __get_user(r4.rt_irtt, &(ur4->rt_irtt));
2824 ret |= __get_user(rtdev, &(ur4->rt_dev));
Arnd Bergmann7a229382009-11-06 23:00:29 -08002825 if (rtdev) {
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002826 ret |= copy_from_user(devname, compat_ptr(rtdev), 15);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002827 r4.rt_dev = devname; devname[15] = 0;
2828 } else
2829 r4.rt_dev = NULL;
2830
2831 r = (void *) &r4;
2832 }
2833
2834 if (ret) {
2835 ret = -EFAULT;
2836 goto out;
2837 }
2838
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002839 set_fs(KERNEL_DS);
Arnd Bergmann6b960182009-11-06 23:10:54 -08002840 ret = sock_do_ioctl(net, sock, cmd, (unsigned long) r);
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002841 set_fs(old_fs);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002842
2843out:
Arnd Bergmann7a229382009-11-06 23:00:29 -08002844 return ret;
2845}
2846
2847/* Since old style bridge ioctl's endup using SIOCDEVPRIVATE
2848 * for some operations; this forces use of the newer bridge-utils that
2849 * use compatiable ioctls
2850 */
Arnd Bergmann6b960182009-11-06 23:10:54 -08002851static int old_bridge_ioctl(compat_ulong_t __user *argp)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002852{
Arnd Bergmann6b960182009-11-06 23:10:54 -08002853 compat_ulong_t tmp;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002854
Arnd Bergmann6b960182009-11-06 23:10:54 -08002855 if (get_user(tmp, argp))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002856 return -EFAULT;
2857 if (tmp == BRCTL_GET_VERSION)
2858 return BRCTL_VERSION + 1;
2859 return -EINVAL;
2860}
2861
Arnd Bergmann6b960182009-11-06 23:10:54 -08002862static int compat_sock_ioctl_trans(struct file *file, struct socket *sock,
2863 unsigned int cmd, unsigned long arg)
2864{
2865 void __user *argp = compat_ptr(arg);
2866 struct sock *sk = sock->sk;
2867 struct net *net = sock_net(sk);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002868
Arnd Bergmann6b960182009-11-06 23:10:54 -08002869 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15))
2870 return siocdevprivate_ioctl(net, cmd, argp);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002871
Arnd Bergmann6b960182009-11-06 23:10:54 -08002872 switch (cmd) {
2873 case SIOCSIFBR:
2874 case SIOCGIFBR:
2875 return old_bridge_ioctl(argp);
2876 case SIOCGIFNAME:
2877 return dev_ifname32(net, argp);
2878 case SIOCGIFCONF:
2879 return dev_ifconf(net, argp);
2880 case SIOCETHTOOL:
2881 return ethtool_ioctl(net, argp);
Arnd Bergmann7a50a242009-11-08 20:57:03 -08002882 case SIOCWANDEV:
2883 return compat_siocwandev(net, argp);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00002884 case SIOCGIFMAP:
2885 case SIOCSIFMAP:
2886 return compat_sioc_ifmap(net, cmd, argp);
Arnd Bergmann6b960182009-11-06 23:10:54 -08002887 case SIOCBONDENSLAVE:
2888 case SIOCBONDRELEASE:
2889 case SIOCBONDSETHWADDR:
2890 case SIOCBONDSLAVEINFOQUERY:
2891 case SIOCBONDINFOQUERY:
2892 case SIOCBONDCHANGEACTIVE:
2893 return bond_ioctl(net, cmd, argp);
2894 case SIOCADDRT:
2895 case SIOCDELRT:
2896 return routing_ioctl(net, sock, cmd, argp);
2897 case SIOCGSTAMP:
2898 return do_siocgstamp(net, sock, cmd, argp);
2899 case SIOCGSTAMPNS:
2900 return do_siocgstampns(net, sock, cmd, argp);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00002901 case SIOCSHWTSTAMP:
2902 return compat_siocshwtstamp(net, argp);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002903
Arnd Bergmann6b960182009-11-06 23:10:54 -08002904 case FIOSETOWN:
2905 case SIOCSPGRP:
2906 case FIOGETOWN:
2907 case SIOCGPGRP:
2908 case SIOCBRADDBR:
2909 case SIOCBRDELBR:
2910 case SIOCGIFVLAN:
2911 case SIOCSIFVLAN:
2912 case SIOCADDDLCI:
2913 case SIOCDELDLCI:
2914 return sock_ioctl(file, cmd, arg);
2915
2916 case SIOCGIFFLAGS:
2917 case SIOCSIFFLAGS:
2918 case SIOCGIFMETRIC:
2919 case SIOCSIFMETRIC:
2920 case SIOCGIFMTU:
2921 case SIOCSIFMTU:
2922 case SIOCGIFMEM:
2923 case SIOCSIFMEM:
2924 case SIOCGIFHWADDR:
2925 case SIOCSIFHWADDR:
2926 case SIOCADDMULTI:
2927 case SIOCDELMULTI:
2928 case SIOCGIFINDEX:
Arnd Bergmann6b960182009-11-06 23:10:54 -08002929 case SIOCGIFADDR:
2930 case SIOCSIFADDR:
2931 case SIOCSIFHWBROADCAST:
Arnd Bergmann6b960182009-11-06 23:10:54 -08002932 case SIOCDIFADDR:
Arnd Bergmann6b960182009-11-06 23:10:54 -08002933 case SIOCGIFBRDADDR:
2934 case SIOCSIFBRDADDR:
2935 case SIOCGIFDSTADDR:
2936 case SIOCSIFDSTADDR:
2937 case SIOCGIFNETMASK:
2938 case SIOCSIFNETMASK:
2939 case SIOCSIFPFLAGS:
2940 case SIOCGIFPFLAGS:
2941 case SIOCGIFTXQLEN:
2942 case SIOCSIFTXQLEN:
2943 case SIOCBRADDIF:
2944 case SIOCBRDELIF:
Arnd Bergmann9177efd2009-11-06 08:09:09 +00002945 case SIOCSIFNAME:
2946 case SIOCGMIIPHY:
2947 case SIOCGMIIREG:
2948 case SIOCSMIIREG:
Arnd Bergmann6b960182009-11-06 23:10:54 -08002949 return dev_ifsioc(net, sock, cmd, argp);
Arnd Bergmann9177efd2009-11-06 08:09:09 +00002950
Arnd Bergmann6b960182009-11-06 23:10:54 -08002951 case SIOCSARP:
2952 case SIOCGARP:
2953 case SIOCDARP:
Arnd Bergmann6b960182009-11-06 23:10:54 -08002954 case SIOCATMARK:
Arnd Bergmann9177efd2009-11-06 08:09:09 +00002955 return sock_do_ioctl(net, sock, cmd, arg);
2956 }
2957
2958 /* Prevent warning from compat_sys_ioctl, these always
2959 * result in -EINVAL in the native case anyway. */
2960 switch (cmd) {
2961 case SIOCRTMSG:
2962 case SIOCGIFCOUNT:
Arnd Bergmann6b960182009-11-06 23:10:54 -08002963 case SIOCSRARP:
2964 case SIOCGRARP:
2965 case SIOCDRARP:
Arnd Bergmann9177efd2009-11-06 08:09:09 +00002966 case SIOCSIFLINK:
2967 case SIOCGIFSLAVE:
2968 case SIOCSIFSLAVE:
2969 return -EINVAL;
Arnd Bergmann6b960182009-11-06 23:10:54 -08002970 }
2971
2972 return -ENOIOCTLCMD;
2973}
Arnd Bergmann7a229382009-11-06 23:00:29 -08002974
Shaun Pereira89bbfc92006-03-21 23:58:08 -08002975static long compat_sock_ioctl(struct file *file, unsigned cmd,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002976 unsigned long arg)
Shaun Pereira89bbfc92006-03-21 23:58:08 -08002977{
2978 struct socket *sock = file->private_data;
2979 int ret = -ENOIOCTLCMD;
David S. Miller87de87d2008-06-03 09:14:03 -07002980 struct sock *sk;
2981 struct net *net;
2982
2983 sk = sock->sk;
2984 net = sock_net(sk);
Shaun Pereira89bbfc92006-03-21 23:58:08 -08002985
2986 if (sock->ops->compat_ioctl)
2987 ret = sock->ops->compat_ioctl(sock, cmd, arg);
2988
David S. Miller87de87d2008-06-03 09:14:03 -07002989 if (ret == -ENOIOCTLCMD &&
2990 (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST))
2991 ret = compat_wext_handle_ioctl(net, cmd, arg);
2992
Arnd Bergmann6b960182009-11-06 23:10:54 -08002993 if (ret == -ENOIOCTLCMD)
2994 ret = compat_sock_ioctl_trans(file, sock, cmd, arg);
2995
Shaun Pereira89bbfc92006-03-21 23:58:08 -08002996 return ret;
2997}
2998#endif
2999
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003000int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen)
3001{
3002 return sock->ops->bind(sock, addr, addrlen);
3003}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003004EXPORT_SYMBOL(kernel_bind);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003005
3006int kernel_listen(struct socket *sock, int backlog)
3007{
3008 return sock->ops->listen(sock, backlog);
3009}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003010EXPORT_SYMBOL(kernel_listen);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003011
3012int kernel_accept(struct socket *sock, struct socket **newsock, int flags)
3013{
3014 struct sock *sk = sock->sk;
3015 int err;
3016
3017 err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
3018 newsock);
3019 if (err < 0)
3020 goto done;
3021
3022 err = sock->ops->accept(sock, *newsock, flags);
3023 if (err < 0) {
3024 sock_release(*newsock);
Tony Battersbyfa8705b2007-10-10 21:09:04 -07003025 *newsock = NULL;
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003026 goto done;
3027 }
3028
3029 (*newsock)->ops = sock->ops;
Wei Yongjun1b085342008-12-18 19:35:10 -08003030 __module_get((*newsock)->ops->owner);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003031
3032done:
3033 return err;
3034}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003035EXPORT_SYMBOL(kernel_accept);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003036
3037int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
YOSHIFUJI Hideaki4768fbc2007-02-09 23:25:31 +09003038 int flags)
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003039{
3040 return sock->ops->connect(sock, addr, addrlen, flags);
3041}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003042EXPORT_SYMBOL(kernel_connect);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003043
3044int kernel_getsockname(struct socket *sock, struct sockaddr *addr,
3045 int *addrlen)
3046{
3047 return sock->ops->getname(sock, addr, addrlen, 0);
3048}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003049EXPORT_SYMBOL(kernel_getsockname);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003050
3051int kernel_getpeername(struct socket *sock, struct sockaddr *addr,
3052 int *addrlen)
3053{
3054 return sock->ops->getname(sock, addr, addrlen, 1);
3055}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003056EXPORT_SYMBOL(kernel_getpeername);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003057
3058int kernel_getsockopt(struct socket *sock, int level, int optname,
3059 char *optval, int *optlen)
3060{
3061 mm_segment_t oldfs = get_fs();
3062 int err;
3063
3064 set_fs(KERNEL_DS);
3065 if (level == SOL_SOCKET)
3066 err = sock_getsockopt(sock, level, optname, optval, optlen);
3067 else
3068 err = sock->ops->getsockopt(sock, level, optname, optval,
3069 optlen);
3070 set_fs(oldfs);
3071 return err;
3072}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003073EXPORT_SYMBOL(kernel_getsockopt);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003074
3075int kernel_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07003076 char *optval, unsigned int optlen)
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003077{
3078 mm_segment_t oldfs = get_fs();
3079 int err;
3080
3081 set_fs(KERNEL_DS);
3082 if (level == SOL_SOCKET)
3083 err = sock_setsockopt(sock, level, optname, optval, optlen);
3084 else
3085 err = sock->ops->setsockopt(sock, level, optname, optval,
3086 optlen);
3087 set_fs(oldfs);
3088 return err;
3089}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003090EXPORT_SYMBOL(kernel_setsockopt);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003091
3092int kernel_sendpage(struct socket *sock, struct page *page, int offset,
3093 size_t size, int flags)
3094{
Herbert Xuf8451722010-05-24 00:12:34 -07003095 sock_update_classid(sock->sk);
3096
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003097 if (sock->ops->sendpage)
3098 return sock->ops->sendpage(sock, page, offset, size, flags);
3099
3100 return sock_no_sendpage(sock, page, offset, size, flags);
3101}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003102EXPORT_SYMBOL(kernel_sendpage);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003103
3104int kernel_sock_ioctl(struct socket *sock, int cmd, unsigned long arg)
3105{
3106 mm_segment_t oldfs = get_fs();
3107 int err;
3108
3109 set_fs(KERNEL_DS);
3110 err = sock->ops->ioctl(sock, cmd, arg);
3111 set_fs(oldfs);
3112
3113 return err;
3114}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003115EXPORT_SYMBOL(kernel_sock_ioctl);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003116
Trond Myklebust91cf45f2007-11-12 18:10:39 -08003117int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how)
3118{
3119 return sock->ops->shutdown(sock, how);
3120}
Trond Myklebust91cf45f2007-11-12 18:10:39 -08003121EXPORT_SYMBOL(kernel_sock_shutdown);