blob: ae904b58d9f516ef4cca81552d0bd011f19edde1 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91#include <asm/uaccess.h>
92#include <asm/unistd.h>
93
94#include <net/compat.h>
David S. Miller87de87d2008-06-03 09:14:03 -070095#include <net/wext.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97#include <net/sock.h>
98#include <linux/netfilter.h>
99
Arnd Bergmann6b960182009-11-06 23:10:54 -0800100#include <linux/if_tun.h>
101#include <linux/ipv6_route.h>
102#include <linux/route.h>
Arnd Bergmann6b960182009-11-06 23:10:54 -0800103#include <linux/sockios.h>
104#include <linux/atalk.h>
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106static int sock_no_open(struct inode *irrelevant, struct file *dontcare);
Badari Pulavarty027445c2006-09-30 23:28:46 -0700107static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov,
108 unsigned long nr_segs, loff_t pos);
109static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov,
110 unsigned long nr_segs, loff_t pos);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700111static int sock_mmap(struct file *file, struct vm_area_struct *vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113static int sock_close(struct inode *inode, struct file *file);
114static unsigned int sock_poll(struct file *file,
115 struct poll_table_struct *wait);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700116static long sock_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
Shaun Pereira89bbfc92006-03-21 23:58:08 -0800117#ifdef CONFIG_COMPAT
118static long compat_sock_ioctl(struct file *file,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700119 unsigned int cmd, unsigned long arg);
Shaun Pereira89bbfc92006-03-21 23:58:08 -0800120#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121static int sock_fasync(int fd, struct file *filp, int on);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122static ssize_t sock_sendpage(struct file *file, struct page *page,
123 int offset, size_t size, loff_t *ppos, int more);
Jens Axboe9c55e012007-11-06 23:30:13 -0800124static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
125 struct pipe_inode_info *pipe, size_t len,
126 unsigned int flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128/*
129 * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
130 * in the operation structures but are done directly via the socketcall() multiplexor.
131 */
132
Arjan van de Venda7071d2007-02-12 00:55:36 -0800133static const struct file_operations socket_file_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 .owner = THIS_MODULE,
135 .llseek = no_llseek,
136 .aio_read = sock_aio_read,
137 .aio_write = sock_aio_write,
138 .poll = sock_poll,
139 .unlocked_ioctl = sock_ioctl,
Shaun Pereira89bbfc92006-03-21 23:58:08 -0800140#ifdef CONFIG_COMPAT
141 .compat_ioctl = compat_sock_ioctl,
142#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 .mmap = sock_mmap,
144 .open = sock_no_open, /* special open code to disallow open via /proc */
145 .release = sock_close,
146 .fasync = sock_fasync,
Jens Axboe5274f052006-03-30 15:15:30 +0200147 .sendpage = sock_sendpage,
148 .splice_write = generic_splice_sendpage,
Jens Axboe9c55e012007-11-06 23:30:13 -0800149 .splice_read = sock_splice_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150};
151
152/*
153 * The protocol list. Each protocol is registered in here.
154 */
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156static DEFINE_SPINLOCK(net_family_lock);
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -0700157static const struct net_proto_family *net_families[NPROTO] __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159/*
160 * Statistics counters of the socket lists
161 */
162
163static DEFINE_PER_CPU(int, sockets_in_use) = 0;
164
165/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700166 * Support routines.
167 * Move socket addresses back and forth across the kernel/user
168 * divide and look after the messy bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 */
170
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700171#define MAX_SOCK_ADDR 128 /* 108 for Unix domain -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 16 for IP, 16 for IPX,
173 24 for IPv6,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700174 about 80 for AX.25
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 must be at least one bigger than
176 the AF_UNIX size (see net/unix/af_unix.c
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700177 :unix_mkname()).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180/**
181 * move_addr_to_kernel - copy a socket address into kernel space
182 * @uaddr: Address in user space
183 * @kaddr: Address in kernel space
184 * @ulen: Length in user space
185 *
186 * The address is copied into kernel space. If the provided address is
187 * too long an error code of -EINVAL is returned. If the copy gives
188 * invalid addresses -EFAULT is returned. On a success 0 is returned.
189 */
190
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -0700191int move_addr_to_kernel(void __user *uaddr, int ulen, struct sockaddr *kaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -0700193 if (ulen < 0 || ulen > sizeof(struct sockaddr_storage))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700195 if (ulen == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 return 0;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700197 if (copy_from_user(kaddr, uaddr, ulen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 return -EFAULT;
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100199 return audit_sockaddr(ulen, kaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
202/**
203 * move_addr_to_user - copy an address to user space
204 * @kaddr: kernel space address
205 * @klen: length of address in kernel
206 * @uaddr: user space address
207 * @ulen: pointer to user length field
208 *
209 * The value pointed to by ulen on entry is the buffer length available.
210 * This is overwritten with the buffer space used. -EINVAL is returned
211 * if an overlong buffer is specified or a negative buffer size. -EFAULT
212 * is returned if either the buffer or the length field are not
213 * accessible.
214 * After copying the data up to the limit the user specifies, the true
215 * length of the data is written over the length limit the user
216 * specified. Zero is returned for a success.
217 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700218
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -0700219int move_addr_to_user(struct sockaddr *kaddr, int klen, void __user *uaddr,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700220 int __user *ulen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
222 int err;
223 int len;
224
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700225 err = get_user(len, ulen);
226 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 return err;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700228 if (len > klen)
229 len = klen;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -0700230 if (len < 0 || len > sizeof(struct sockaddr_storage))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700232 if (len) {
Steve Grubbd6fe3942006-03-30 12:20:22 -0500233 if (audit_sockaddr(klen, kaddr))
234 return -ENOMEM;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700235 if (copy_to_user(uaddr, kaddr, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 return -EFAULT;
237 }
238 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700239 * "fromlen shall refer to the value before truncation.."
240 * 1003.1g
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 */
242 return __put_user(klen, ulen);
243}
244
Christoph Lametere18b8902006-12-06 20:33:20 -0800245static struct kmem_cache *sock_inode_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247static struct inode *sock_alloc_inode(struct super_block *sb)
248{
249 struct socket_alloc *ei;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700250
Christoph Lametere94b1762006-12-06 20:33:17 -0800251 ei = kmem_cache_alloc(sock_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 if (!ei)
253 return NULL;
254 init_waitqueue_head(&ei->socket.wait);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 ei->socket.fasync_list = NULL;
257 ei->socket.state = SS_UNCONNECTED;
258 ei->socket.flags = 0;
259 ei->socket.ops = NULL;
260 ei->socket.sk = NULL;
261 ei->socket.file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 return &ei->vfs_inode;
264}
265
266static void sock_destroy_inode(struct inode *inode)
267{
268 kmem_cache_free(sock_inode_cachep,
269 container_of(inode, struct socket_alloc, vfs_inode));
270}
271
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700272static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700274 struct socket_alloc *ei = (struct socket_alloc *)foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Christoph Lametera35afb82007-05-16 22:10:57 -0700276 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279static int init_inodecache(void)
280{
281 sock_inode_cachep = kmem_cache_create("sock_inode_cache",
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700282 sizeof(struct socket_alloc),
283 0,
284 (SLAB_HWCACHE_ALIGN |
285 SLAB_RECLAIM_ACCOUNT |
286 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +0900287 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 if (sock_inode_cachep == NULL)
289 return -ENOMEM;
290 return 0;
291}
292
Alexey Dobriyanb87221d2009-09-21 17:01:09 -0700293static const struct super_operations sockfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 .alloc_inode = sock_alloc_inode,
295 .destroy_inode =sock_destroy_inode,
296 .statfs = simple_statfs,
297};
298
David Howells454e2392006-06-23 02:02:57 -0700299static int sockfs_get_sb(struct file_system_type *fs_type,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700300 int flags, const char *dev_name, void *data,
301 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
David Howells454e2392006-06-23 02:02:57 -0700303 return get_sb_pseudo(fs_type, "socket:", &sockfs_ops, SOCKFS_MAGIC,
304 mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305}
306
Eric Dumazetba899662005-08-26 12:05:31 -0700307static struct vfsmount *sock_mnt __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309static struct file_system_type sock_fs_type = {
310 .name = "sockfs",
311 .get_sb = sockfs_get_sb,
312 .kill_sb = kill_anon_super,
313};
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700314
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700315/*
316 * sockfs_dname() is called from d_path().
317 */
318static char *sockfs_dname(struct dentry *dentry, char *buffer, int buflen)
319{
320 return dynamic_dname(dentry, buffer, buflen, "socket:[%lu]",
321 dentry->d_inode->i_ino);
322}
323
Al Viro3ba13d12009-02-20 06:02:22 +0000324static const struct dentry_operations sockfs_dentry_operations = {
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700325 .d_dname = sockfs_dname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326};
327
328/*
329 * Obtains the first available file descriptor and sets it up for use.
330 *
David S. Miller39d8c1b2006-03-20 17:13:49 -0800331 * These functions create file structures and maps them to fd space
332 * of the current process. On success it returns file descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 * and file struct implicitly stored in sock->file.
334 * Note that another thread may close file descriptor before we return
335 * from this function. We use the fact that now we do not refer
336 * to socket after mapping. If one day we will need it, this
337 * function will increment ref. count on file by 1.
338 *
339 * In any case returned fd MAY BE not valid!
340 * This race condition is unavoidable
341 * with shared fd spaces, we cannot solve it inside kernel,
342 * but we take care of internal coherence yet.
343 */
344
Al Viro7cbe66b2009-08-05 19:59:08 +0400345static int sock_alloc_file(struct socket *sock, struct file **f, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
Al Viro7cbe66b2009-08-05 19:59:08 +0400347 struct qstr name = { .name = "" };
Al Viro2c48b9c2009-08-09 00:52:35 +0400348 struct path path;
Al Viro7cbe66b2009-08-05 19:59:08 +0400349 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 int fd;
David S. Miller39d8c1b2006-03-20 17:13:49 -0800351
Ulrich Dreppera677a032008-07-23 21:29:17 -0700352 fd = get_unused_fd_flags(flags);
Al Viro7cbe66b2009-08-05 19:59:08 +0400353 if (unlikely(fd < 0))
354 return fd;
David S. Miller39d8c1b2006-03-20 17:13:49 -0800355
Al Viro2c48b9c2009-08-09 00:52:35 +0400356 path.dentry = d_alloc(sock_mnt->mnt_sb->s_root, &name);
357 if (unlikely(!path.dentry)) {
Al Viro7cbe66b2009-08-05 19:59:08 +0400358 put_unused_fd(fd);
David S. Miller39d8c1b2006-03-20 17:13:49 -0800359 return -ENOMEM;
Al Viro7cbe66b2009-08-05 19:59:08 +0400360 }
Al Viro2c48b9c2009-08-09 00:52:35 +0400361 path.mnt = mntget(sock_mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Al Viro2c48b9c2009-08-09 00:52:35 +0400363 path.dentry->d_op = &sockfs_dentry_operations;
Al Viro2c48b9c2009-08-09 00:52:35 +0400364 d_instantiate(path.dentry, SOCK_INODE(sock));
Al Virocc3808f2009-08-06 09:43:59 +0400365 SOCK_INODE(sock)->i_fop = &socket_file_ops;
366
Al Viro2c48b9c2009-08-09 00:52:35 +0400367 file = alloc_file(&path, FMODE_READ | FMODE_WRITE,
Al Virocc3808f2009-08-06 09:43:59 +0400368 &socket_file_ops);
369 if (unlikely(!file)) {
370 /* drop dentry, keep inode */
371 atomic_inc(&path.dentry->d_inode->i_count);
Al Viro2c48b9c2009-08-09 00:52:35 +0400372 path_put(&path);
Al Virocc3808f2009-08-06 09:43:59 +0400373 put_unused_fd(fd);
374 return -ENFILE;
375 }
David S. Miller39d8c1b2006-03-20 17:13:49 -0800376
377 sock->file = file;
Ulrich Drepper77d27202008-07-23 21:29:35 -0700378 file->f_flags = O_RDWR | (flags & O_NONBLOCK);
David S. Miller39d8c1b2006-03-20 17:13:49 -0800379 file->f_pos = 0;
380 file->private_data = sock;
381
Al Viro7cbe66b2009-08-05 19:59:08 +0400382 *f = file;
383 return fd;
David S. Miller39d8c1b2006-03-20 17:13:49 -0800384}
385
Ulrich Dreppera677a032008-07-23 21:29:17 -0700386int sock_map_fd(struct socket *sock, int flags)
David S. Miller39d8c1b2006-03-20 17:13:49 -0800387{
388 struct file *newfile;
Al Viro7cbe66b2009-08-05 19:59:08 +0400389 int fd = sock_alloc_file(sock, &newfile, flags);
David S. Miller39d8c1b2006-03-20 17:13:49 -0800390
Al Viro7cbe66b2009-08-05 19:59:08 +0400391 if (likely(fd >= 0))
David S. Miller39d8c1b2006-03-20 17:13:49 -0800392 fd_install(fd, newfile);
Al Viro7cbe66b2009-08-05 19:59:08 +0400393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 return fd;
395}
396
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800397static struct socket *sock_from_file(struct file *file, int *err)
398{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800399 if (file->f_op == &socket_file_ops)
400 return file->private_data; /* set in sock_map_fd */
401
Eric Dumazet23bb80d2007-02-08 14:59:57 -0800402 *err = -ENOTSOCK;
403 return NULL;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800404}
405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406/**
407 * sockfd_lookup - Go from a file number to its socket slot
408 * @fd: file handle
409 * @err: pointer to an error code return
410 *
411 * The file handle passed in is locked and the socket it is bound
412 * too is returned. If an error occurs the err pointer is overwritten
413 * with a negative errno code and NULL is returned. The function checks
414 * for both invalid handles and passing a handle which is not a socket.
415 *
416 * On a success the socket object pointer is returned.
417 */
418
419struct socket *sockfd_lookup(int fd, int *err)
420{
421 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 struct socket *sock;
423
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700424 file = fget(fd);
425 if (!file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 *err = -EBADF;
427 return NULL;
428 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700429
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800430 sock = sock_from_file(file, err);
431 if (!sock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 fput(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 return sock;
434}
435
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800436static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed)
437{
438 struct file *file;
439 struct socket *sock;
440
Hua Zhong36725582006-04-19 15:25:02 -0700441 *err = -EBADF;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800442 file = fget_light(fd, fput_needed);
443 if (file) {
444 sock = sock_from_file(file, err);
445 if (sock)
446 return sock;
447 fput_light(file, *fput_needed);
448 }
449 return NULL;
450}
451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452/**
453 * sock_alloc - allocate a socket
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700454 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 * Allocate a new inode and socket object. The two are bound together
456 * and initialised. The socket is then returned. If we are out of inodes
457 * NULL is returned.
458 */
459
460static struct socket *sock_alloc(void)
461{
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700462 struct inode *inode;
463 struct socket *sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465 inode = new_inode(sock_mnt->mnt_sb);
466 if (!inode)
467 return NULL;
468
469 sock = SOCKET_I(inode);
470
Eric Dumazet29a020d2009-09-15 02:39:20 -0700471 kmemcheck_annotate_bitfield(sock, type);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700472 inode->i_mode = S_IFSOCK | S_IRWXUGO;
David Howells8192b0c2008-11-14 10:39:10 +1100473 inode->i_uid = current_fsuid();
474 inode->i_gid = current_fsgid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Eric Dumazet4e694892009-04-04 16:41:09 -0700476 percpu_add(sockets_in_use, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 return sock;
478}
479
480/*
481 * In theory you can't get an open on this inode, but /proc provides
482 * a back door. Remember to keep it shut otherwise you'll let the
483 * creepy crawlies in.
484 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486static int sock_no_open(struct inode *irrelevant, struct file *dontcare)
487{
488 return -ENXIO;
489}
490
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800491const struct file_operations bad_sock_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 .owner = THIS_MODULE,
493 .open = sock_no_open,
494};
495
496/**
497 * sock_release - close a socket
498 * @sock: socket to close
499 *
500 * The socket is released from the protocol stack if it has a release
501 * callback, and the inode is then released if the socket is bound to
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700502 * an inode not a file.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505void sock_release(struct socket *sock)
506{
507 if (sock->ops) {
508 struct module *owner = sock->ops->owner;
509
510 sock->ops->release(sock);
511 sock->ops = NULL;
512 module_put(owner);
513 }
514
515 if (sock->fasync_list)
516 printk(KERN_ERR "sock_release: fasync list not empty!\n");
517
Eric Dumazet4e694892009-04-04 16:41:09 -0700518 percpu_sub(sockets_in_use, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 if (!sock->file) {
520 iput(SOCK_INODE(sock));
521 return;
522 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700523 sock->file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524}
525
Patrick Ohly20d49472009-02-12 05:03:38 +0000526int sock_tx_timestamp(struct msghdr *msg, struct sock *sk,
527 union skb_shared_tx *shtx)
528{
529 shtx->flags = 0;
530 if (sock_flag(sk, SOCK_TIMESTAMPING_TX_HARDWARE))
531 shtx->hardware = 1;
532 if (sock_flag(sk, SOCK_TIMESTAMPING_TX_SOFTWARE))
533 shtx->software = 1;
534 return 0;
535}
536EXPORT_SYMBOL(sock_tx_timestamp);
537
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700538static inline int __sock_sendmsg(struct kiocb *iocb, struct socket *sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 struct msghdr *msg, size_t size)
540{
541 struct sock_iocb *si = kiocb_to_siocb(iocb);
542 int err;
543
544 si->sock = sock;
545 si->scm = NULL;
546 si->msg = msg;
547 si->size = size;
548
549 err = security_socket_sendmsg(sock, msg, size);
550 if (err)
551 return err;
552
553 return sock->ops->sendmsg(iocb, sock, msg, size);
554}
555
556int sock_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
557{
558 struct kiocb iocb;
559 struct sock_iocb siocb;
560 int ret;
561
562 init_sync_kiocb(&iocb, NULL);
563 iocb.private = &siocb;
564 ret = __sock_sendmsg(&iocb, sock, msg, size);
565 if (-EIOCBQUEUED == ret)
566 ret = wait_on_sync_kiocb(&iocb);
567 return ret;
568}
569
570int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
571 struct kvec *vec, size_t num, size_t size)
572{
573 mm_segment_t oldfs = get_fs();
574 int result;
575
576 set_fs(KERNEL_DS);
577 /*
578 * the following is safe, since for compiler definitions of kvec and
579 * iovec are identical, yielding the same in-core layout and alignment
580 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700581 msg->msg_iov = (struct iovec *)vec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 msg->msg_iovlen = num;
583 result = sock_sendmsg(sock, msg, size);
584 set_fs(oldfs);
585 return result;
586}
587
Patrick Ohly20d49472009-02-12 05:03:38 +0000588static int ktime2ts(ktime_t kt, struct timespec *ts)
589{
590 if (kt.tv64) {
591 *ts = ktime_to_timespec(kt);
592 return 1;
593 } else {
594 return 0;
595 }
596}
597
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700598/*
599 * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
600 */
601void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
602 struct sk_buff *skb)
603{
Patrick Ohly20d49472009-02-12 05:03:38 +0000604 int need_software_tstamp = sock_flag(sk, SOCK_RCVTSTAMP);
605 struct timespec ts[3];
606 int empty = 1;
607 struct skb_shared_hwtstamps *shhwtstamps =
608 skb_hwtstamps(skb);
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700609
Patrick Ohly20d49472009-02-12 05:03:38 +0000610 /* Race occurred between timestamp enabling and packet
611 receiving. Fill in the current time for now. */
612 if (need_software_tstamp && skb->tstamp.tv64 == 0)
613 __net_timestamp(skb);
614
615 if (need_software_tstamp) {
616 if (!sock_flag(sk, SOCK_RCVTSTAMPNS)) {
617 struct timeval tv;
618 skb_get_timestamp(skb, &tv);
619 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMP,
620 sizeof(tv), &tv);
621 } else {
Hagen Paul Pfeifer842509b2010-04-06 05:39:52 +0000622 skb_get_timestampns(skb, &ts[0]);
Patrick Ohly20d49472009-02-12 05:03:38 +0000623 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPNS,
Hagen Paul Pfeifer842509b2010-04-06 05:39:52 +0000624 sizeof(ts[0]), &ts[0]);
Patrick Ohly20d49472009-02-12 05:03:38 +0000625 }
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700626 }
Patrick Ohly20d49472009-02-12 05:03:38 +0000627
628
629 memset(ts, 0, sizeof(ts));
630 if (skb->tstamp.tv64 &&
631 sock_flag(sk, SOCK_TIMESTAMPING_SOFTWARE)) {
632 skb_get_timestampns(skb, ts + 0);
633 empty = 0;
634 }
635 if (shhwtstamps) {
636 if (sock_flag(sk, SOCK_TIMESTAMPING_SYS_HARDWARE) &&
637 ktime2ts(shhwtstamps->syststamp, ts + 1))
638 empty = 0;
639 if (sock_flag(sk, SOCK_TIMESTAMPING_RAW_HARDWARE) &&
640 ktime2ts(shhwtstamps->hwtstamp, ts + 2))
641 empty = 0;
642 }
643 if (!empty)
644 put_cmsg(msg, SOL_SOCKET,
645 SCM_TIMESTAMPING, sizeof(ts), &ts);
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700646}
647
Arnaldo Carvalho de Melo7c81fd82007-03-10 00:39:35 -0300648EXPORT_SYMBOL_GPL(__sock_recv_timestamp);
649
Neil Horman3b885782009-10-12 13:26:31 -0700650inline void sock_recv_drops(struct msghdr *msg, struct sock *sk, struct sk_buff *skb)
651{
652 if (sock_flag(sk, SOCK_RXQ_OVFL) && skb && skb->dropcount)
653 put_cmsg(msg, SOL_SOCKET, SO_RXQ_OVFL,
654 sizeof(__u32), &skb->dropcount);
655}
656
657void sock_recv_ts_and_drops(struct msghdr *msg, struct sock *sk,
658 struct sk_buff *skb)
659{
660 sock_recv_timestamp(msg, sk, skb);
661 sock_recv_drops(msg, sk, skb);
662}
663EXPORT_SYMBOL_GPL(sock_recv_ts_and_drops);
664
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -0700665static inline int __sock_recvmsg_nosec(struct kiocb *iocb, struct socket *sock,
666 struct msghdr *msg, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 struct sock_iocb *si = kiocb_to_siocb(iocb);
669
670 si->sock = sock;
671 si->scm = NULL;
672 si->msg = msg;
673 si->size = size;
674 si->flags = flags;
675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 return sock->ops->recvmsg(iocb, sock, msg, size, flags);
677}
678
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -0700679static inline int __sock_recvmsg(struct kiocb *iocb, struct socket *sock,
680 struct msghdr *msg, size_t size, int flags)
681{
682 int err = security_socket_recvmsg(sock, msg, size, flags);
683
684 return err ?: __sock_recvmsg_nosec(iocb, sock, msg, size, flags);
685}
686
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700687int sock_recvmsg(struct socket *sock, struct msghdr *msg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 size_t size, int flags)
689{
690 struct kiocb iocb;
691 struct sock_iocb siocb;
692 int ret;
693
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700694 init_sync_kiocb(&iocb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 iocb.private = &siocb;
696 ret = __sock_recvmsg(&iocb, sock, msg, size, flags);
697 if (-EIOCBQUEUED == ret)
698 ret = wait_on_sync_kiocb(&iocb);
699 return ret;
700}
701
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -0700702static int sock_recvmsg_nosec(struct socket *sock, struct msghdr *msg,
703 size_t size, int flags)
704{
705 struct kiocb iocb;
706 struct sock_iocb siocb;
707 int ret;
708
709 init_sync_kiocb(&iocb, NULL);
710 iocb.private = &siocb;
711 ret = __sock_recvmsg_nosec(&iocb, sock, msg, size, flags);
712 if (-EIOCBQUEUED == ret)
713 ret = wait_on_sync_kiocb(&iocb);
714 return ret;
715}
716
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700717int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
718 struct kvec *vec, size_t num, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
720 mm_segment_t oldfs = get_fs();
721 int result;
722
723 set_fs(KERNEL_DS);
724 /*
725 * the following is safe, since for compiler definitions of kvec and
726 * iovec are identical, yielding the same in-core layout and alignment
727 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700728 msg->msg_iov = (struct iovec *)vec, msg->msg_iovlen = num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 result = sock_recvmsg(sock, msg, size, flags);
730 set_fs(oldfs);
731 return result;
732}
733
734static void sock_aio_dtor(struct kiocb *iocb)
735{
736 kfree(iocb->private);
737}
738
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -0300739static ssize_t sock_sendpage(struct file *file, struct page *page,
740 int offset, size_t size, loff_t *ppos, int more)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
742 struct socket *sock;
743 int flags;
744
Eric Dumazetb69aee02005-09-06 14:42:45 -0700745 sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 flags = !(file->f_flags & O_NONBLOCK) ? 0 : MSG_DONTWAIT;
748 if (more)
749 flags |= MSG_MORE;
750
Linus Torvaldse6949582009-08-13 08:28:36 -0700751 return kernel_sendpage(sock, page, offset, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752}
753
Jens Axboe9c55e012007-11-06 23:30:13 -0800754static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
755 struct pipe_inode_info *pipe, size_t len,
756 unsigned int flags)
757{
758 struct socket *sock = file->private_data;
759
Rémi Denis-Courmont997b37d2008-02-15 02:35:45 -0800760 if (unlikely(!sock->ops->splice_read))
761 return -EINVAL;
762
Jens Axboe9c55e012007-11-06 23:30:13 -0800763 return sock->ops->splice_read(sock, ppos, pipe, len, flags);
764}
765
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800766static struct sock_iocb *alloc_sock_iocb(struct kiocb *iocb,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700767 struct sock_iocb *siocb)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800768{
769 if (!is_sync_kiocb(iocb)) {
770 siocb = kmalloc(sizeof(*siocb), GFP_KERNEL);
771 if (!siocb)
772 return NULL;
773 iocb->ki_dtor = sock_aio_dtor;
774 }
775
776 siocb->kiocb = iocb;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800777 iocb->private = siocb;
778 return siocb;
779}
780
781static ssize_t do_sock_read(struct msghdr *msg, struct kiocb *iocb,
Badari Pulavarty027445c2006-09-30 23:28:46 -0700782 struct file *file, const struct iovec *iov,
783 unsigned long nr_segs)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800784{
785 struct socket *sock = file->private_data;
786 size_t size = 0;
787 int i;
788
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700789 for (i = 0; i < nr_segs; i++)
790 size += iov[i].iov_len;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800791
792 msg->msg_name = NULL;
793 msg->msg_namelen = 0;
794 msg->msg_control = NULL;
795 msg->msg_controllen = 0;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700796 msg->msg_iov = (struct iovec *)iov;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800797 msg->msg_iovlen = nr_segs;
798 msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
799
800 return __sock_recvmsg(iocb, sock, msg, size, msg->msg_flags);
801}
802
Badari Pulavarty027445c2006-09-30 23:28:46 -0700803static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov,
804 unsigned long nr_segs, loff_t pos)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800805{
806 struct sock_iocb siocb, *x;
807
808 if (pos != 0)
809 return -ESPIPE;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700810
811 if (iocb->ki_left == 0) /* Match SYS5 behaviour */
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800812 return 0;
813
Badari Pulavarty027445c2006-09-30 23:28:46 -0700814
815 x = alloc_sock_iocb(iocb, &siocb);
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800816 if (!x)
817 return -ENOMEM;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700818 return do_sock_read(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800819}
820
821static ssize_t do_sock_write(struct msghdr *msg, struct kiocb *iocb,
Badari Pulavarty027445c2006-09-30 23:28:46 -0700822 struct file *file, const struct iovec *iov,
823 unsigned long nr_segs)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800824{
825 struct socket *sock = file->private_data;
826 size_t size = 0;
827 int i;
828
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700829 for (i = 0; i < nr_segs; i++)
830 size += iov[i].iov_len;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800831
832 msg->msg_name = NULL;
833 msg->msg_namelen = 0;
834 msg->msg_control = NULL;
835 msg->msg_controllen = 0;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700836 msg->msg_iov = (struct iovec *)iov;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800837 msg->msg_iovlen = nr_segs;
838 msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
839 if (sock->type == SOCK_SEQPACKET)
840 msg->msg_flags |= MSG_EOR;
841
842 return __sock_sendmsg(iocb, sock, msg, size);
843}
844
Badari Pulavarty027445c2006-09-30 23:28:46 -0700845static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov,
846 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800848 struct sock_iocb siocb, *x;
849
850 if (pos != 0)
851 return -ESPIPE;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700852
Badari Pulavarty027445c2006-09-30 23:28:46 -0700853 x = alloc_sock_iocb(iocb, &siocb);
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800854 if (!x)
855 return -ENOMEM;
856
Badari Pulavarty027445c2006-09-30 23:28:46 -0700857 return do_sock_write(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858}
859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860/*
861 * Atomic setting of ioctl hooks to avoid race
862 * with module unload.
863 */
864
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800865static DEFINE_MUTEX(br_ioctl_mutex);
Eric W. Biederman881d9662007-09-17 11:56:21 -0700866static int (*br_ioctl_hook) (struct net *, unsigned int cmd, void __user *arg) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Eric W. Biederman881d9662007-09-17 11:56:21 -0700868void brioctl_set(int (*hook) (struct net *, unsigned int, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800870 mutex_lock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 br_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800872 mutex_unlock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873}
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875EXPORT_SYMBOL(brioctl_set);
876
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800877static DEFINE_MUTEX(vlan_ioctl_mutex);
Eric W. Biederman881d9662007-09-17 11:56:21 -0700878static int (*vlan_ioctl_hook) (struct net *, void __user *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Eric W. Biederman881d9662007-09-17 11:56:21 -0700880void vlan_ioctl_set(int (*hook) (struct net *, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800882 mutex_lock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 vlan_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800884 mutex_unlock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885}
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887EXPORT_SYMBOL(vlan_ioctl_set);
888
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800889static DEFINE_MUTEX(dlci_ioctl_mutex);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700890static int (*dlci_ioctl_hook) (unsigned int, void __user *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700892void dlci_ioctl_set(int (*hook) (unsigned int, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800894 mutex_lock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 dlci_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800896 mutex_unlock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897}
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899EXPORT_SYMBOL(dlci_ioctl_set);
900
Arnd Bergmann6b960182009-11-06 23:10:54 -0800901static long sock_do_ioctl(struct net *net, struct socket *sock,
902 unsigned int cmd, unsigned long arg)
903{
904 int err;
905 void __user *argp = (void __user *)arg;
906
907 err = sock->ops->ioctl(sock, cmd, arg);
908
909 /*
910 * If this ioctl is unknown try to hand it down
911 * to the NIC driver.
912 */
913 if (err == -ENOIOCTLCMD)
914 err = dev_ioctl(net, cmd, argp);
915
916 return err;
917}
918
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919/*
920 * With an ioctl, arg may well be a user mode pointer, but we don't know
921 * what to do with it - that's up to the protocol still.
922 */
923
924static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
925{
926 struct socket *sock;
Eric W. Biederman881d9662007-09-17 11:56:21 -0700927 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 void __user *argp = (void __user *)arg;
929 int pid, err;
Eric W. Biederman881d9662007-09-17 11:56:21 -0700930 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Eric Dumazetb69aee02005-09-06 14:42:45 -0700932 sock = file->private_data;
Eric W. Biederman881d9662007-09-17 11:56:21 -0700933 sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900934 net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
Eric W. Biederman881d9662007-09-17 11:56:21 -0700936 err = dev_ioctl(net, cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 } else
Johannes Berg3d23e342009-09-29 23:27:28 +0200938#ifdef CONFIG_WEXT_CORE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
Eric W. Biederman881d9662007-09-17 11:56:21 -0700940 err = dev_ioctl(net, cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 } else
Johannes Berg3d23e342009-09-29 23:27:28 +0200942#endif
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700943 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 case FIOSETOWN:
945 case SIOCSPGRP:
946 err = -EFAULT;
947 if (get_user(pid, (int __user *)argp))
948 break;
949 err = f_setown(sock->file, pid, 1);
950 break;
951 case FIOGETOWN:
952 case SIOCGPGRP:
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700953 err = put_user(f_getown(sock->file),
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700954 (int __user *)argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 break;
956 case SIOCGIFBR:
957 case SIOCSIFBR:
958 case SIOCBRADDBR:
959 case SIOCBRDELBR:
960 err = -ENOPKG;
961 if (!br_ioctl_hook)
962 request_module("bridge");
963
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800964 mutex_lock(&br_ioctl_mutex);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700965 if (br_ioctl_hook)
Eric W. Biederman881d9662007-09-17 11:56:21 -0700966 err = br_ioctl_hook(net, cmd, argp);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800967 mutex_unlock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 break;
969 case SIOCGIFVLAN:
970 case SIOCSIFVLAN:
971 err = -ENOPKG;
972 if (!vlan_ioctl_hook)
973 request_module("8021q");
974
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800975 mutex_lock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 if (vlan_ioctl_hook)
Eric W. Biederman881d9662007-09-17 11:56:21 -0700977 err = vlan_ioctl_hook(net, argp);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800978 mutex_unlock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 case SIOCADDDLCI:
981 case SIOCDELDLCI:
982 err = -ENOPKG;
983 if (!dlci_ioctl_hook)
984 request_module("dlci");
985
Pavel Emelyanov7512cbf2008-03-21 15:58:52 -0700986 mutex_lock(&dlci_ioctl_mutex);
987 if (dlci_ioctl_hook)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 err = dlci_ioctl_hook(cmd, argp);
Pavel Emelyanov7512cbf2008-03-21 15:58:52 -0700989 mutex_unlock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 break;
991 default:
Arnd Bergmann6b960182009-11-06 23:10:54 -0800992 err = sock_do_ioctl(net, sock, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 break;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700994 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 return err;
996}
997
998int sock_create_lite(int family, int type, int protocol, struct socket **res)
999{
1000 int err;
1001 struct socket *sock = NULL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001002
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 err = security_socket_create(family, type, protocol, 1);
1004 if (err)
1005 goto out;
1006
1007 sock = sock_alloc();
1008 if (!sock) {
1009 err = -ENOMEM;
1010 goto out;
1011 }
1012
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 sock->type = type;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001014 err = security_socket_post_create(sock, family, type, protocol, 1);
1015 if (err)
1016 goto out_release;
1017
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018out:
1019 *res = sock;
1020 return err;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001021out_release:
1022 sock_release(sock);
1023 sock = NULL;
1024 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025}
1026
1027/* No kernel lock held - perfect */
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001028static unsigned int sock_poll(struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
1030 struct socket *sock;
1031
1032 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001033 * We can't return errors to poll, so it's either yes or no.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 */
Eric Dumazetb69aee02005-09-06 14:42:45 -07001035 sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 return sock->ops->poll(file, sock, wait);
1037}
1038
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001039static int sock_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040{
Eric Dumazetb69aee02005-09-06 14:42:45 -07001041 struct socket *sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
1043 return sock->ops->mmap(file, sock, vma);
1044}
1045
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001046static int sock_close(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047{
1048 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001049 * It was possible the inode is NULL we were
1050 * closing an unfinished socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 */
1052
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001053 if (!inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 printk(KERN_DEBUG "sock_close: NULL inode\n");
1055 return 0;
1056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 sock_release(SOCKET_I(inode));
1058 return 0;
1059}
1060
1061/*
1062 * Update the socket async list
1063 *
1064 * Fasync_list locking strategy.
1065 *
1066 * 1. fasync_list is modified only under process context socket lock
1067 * i.e. under semaphore.
1068 * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
1069 * or under socket lock.
1070 * 3. fasync_list can be used from softirq context, so that
1071 * modification under socket lock have to be enhanced with
1072 * write_lock_bh(&sk->sk_callback_lock).
1073 * --ANK (990710)
1074 */
1075
1076static int sock_fasync(int fd, struct file *filp, int on)
1077{
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001078 struct fasync_struct *fa, *fna = NULL, **prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 struct socket *sock;
1080 struct sock *sk;
1081
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001082 if (on) {
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08001083 fna = kmalloc(sizeof(struct fasync_struct), GFP_KERNEL);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001084 if (fna == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 return -ENOMEM;
1086 }
1087
Eric Dumazetb69aee02005-09-06 14:42:45 -07001088 sock = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001090 sk = sock->sk;
1091 if (sk == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 kfree(fna);
1093 return -EINVAL;
1094 }
1095
1096 lock_sock(sk);
1097
Jonathan Corbet76398422009-02-01 14:26:59 -07001098 spin_lock(&filp->f_lock);
1099 if (on)
1100 filp->f_flags |= FASYNC;
1101 else
1102 filp->f_flags &= ~FASYNC;
1103 spin_unlock(&filp->f_lock);
1104
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001105 prev = &(sock->fasync_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001107 for (fa = *prev; fa != NULL; prev = &fa->fa_next, fa = *prev)
1108 if (fa->fa_file == filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 break;
1110
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001111 if (on) {
1112 if (fa != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 write_lock_bh(&sk->sk_callback_lock);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001114 fa->fa_fd = fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 write_unlock_bh(&sk->sk_callback_lock);
1116
1117 kfree(fna);
1118 goto out;
1119 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001120 fna->fa_file = filp;
1121 fna->fa_fd = fd;
1122 fna->magic = FASYNC_MAGIC;
1123 fna->fa_next = sock->fasync_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 write_lock_bh(&sk->sk_callback_lock);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001125 sock->fasync_list = fna;
Eric Dumazetbcdce712009-10-06 17:28:29 -07001126 sock_set_flag(sk, SOCK_FASYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 write_unlock_bh(&sk->sk_callback_lock);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001128 } else {
1129 if (fa != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 write_lock_bh(&sk->sk_callback_lock);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001131 *prev = fa->fa_next;
Eric Dumazetbcdce712009-10-06 17:28:29 -07001132 if (!sock->fasync_list)
1133 sock_reset_flag(sk, SOCK_FASYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 write_unlock_bh(&sk->sk_callback_lock);
1135 kfree(fa);
1136 }
1137 }
1138
1139out:
1140 release_sock(sock->sk);
1141 return 0;
1142}
1143
1144/* This function may be called only under socket lock or callback_lock */
1145
1146int sock_wake_async(struct socket *sock, int how, int band)
1147{
1148 if (!sock || !sock->fasync_list)
1149 return -1;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001150 switch (how) {
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001151 case SOCK_WAKE_WAITD:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 if (test_bit(SOCK_ASYNC_WAITDATA, &sock->flags))
1153 break;
1154 goto call_kill;
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001155 case SOCK_WAKE_SPACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags))
1157 break;
1158 /* fall through */
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001159 case SOCK_WAKE_IO:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001160call_kill:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 __kill_fasync(sock->fasync_list, SIGIO, band);
1162 break;
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001163 case SOCK_WAKE_URG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 __kill_fasync(sock->fasync_list, SIGURG, band);
1165 }
1166 return 0;
1167}
1168
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001169static int __sock_create(struct net *net, int family, int type, int protocol,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001170 struct socket **res, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171{
1172 int err;
1173 struct socket *sock;
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001174 const struct net_proto_family *pf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
1176 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001177 * Check protocol is in range
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 */
1179 if (family < 0 || family >= NPROTO)
1180 return -EAFNOSUPPORT;
1181 if (type < 0 || type >= SOCK_MAX)
1182 return -EINVAL;
1183
1184 /* Compatibility.
1185
1186 This uglymoron is moved from INET layer to here to avoid
1187 deadlock in module load.
1188 */
1189 if (family == PF_INET && type == SOCK_PACKET) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001190 static int warned;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 if (!warned) {
1192 warned = 1;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001193 printk(KERN_INFO "%s uses obsolete (PF_INET,SOCK_PACKET)\n",
1194 current->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 }
1196 family = PF_PACKET;
1197 }
1198
1199 err = security_socket_create(family, type, protocol, kern);
1200 if (err)
1201 return err;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001202
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001203 /*
1204 * Allocate the socket and allow the family to set things up. if
1205 * the protocol is 0, the family is instructed to select an appropriate
1206 * default.
1207 */
1208 sock = sock_alloc();
1209 if (!sock) {
1210 if (net_ratelimit())
1211 printk(KERN_WARNING "socket: no more sockets\n");
1212 return -ENFILE; /* Not exactly a match, but its the
1213 closest posix thing */
1214 }
1215
1216 sock->type = type;
1217
Johannes Berg95a5afc2008-10-16 15:24:51 -07001218#ifdef CONFIG_MODULES
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001219 /* Attempt to load a protocol module if the find failed.
1220 *
1221 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 * requested real, full-featured networking support upon configuration.
1223 * Otherwise module support will break!
1224 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001225 if (net_families[family] == NULL)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001226 request_module("net-pf-%d", family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227#endif
1228
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001229 rcu_read_lock();
1230 pf = rcu_dereference(net_families[family]);
1231 err = -EAFNOSUPPORT;
1232 if (!pf)
1233 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
1235 /*
1236 * We will call the ->create function, that possibly is in a loadable
1237 * module, so we have to bump that loadable module refcnt first.
1238 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001239 if (!try_module_get(pf->owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 goto out_release;
1241
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001242 /* Now protected by module ref count */
1243 rcu_read_unlock();
1244
Eric Paris3f378b62009-11-05 22:18:14 -08001245 err = pf->create(net, sock, protocol, kern);
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001246 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 goto out_module_put;
Frank Filza79af592005-09-27 15:23:38 -07001248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 /*
1250 * Now to bump the refcnt of the [loadable] module that owns this
1251 * socket at sock_release time we decrement its refcnt.
1252 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001253 if (!try_module_get(sock->ops->owner))
1254 goto out_module_busy;
1255
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 /*
1257 * Now that we're done with the ->create function, the [loadable]
1258 * module can have its refcnt decremented
1259 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001260 module_put(pf->owner);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001261 err = security_socket_post_create(sock, family, type, protocol, kern);
1262 if (err)
Herbert Xu3b185522007-08-15 14:46:02 -07001263 goto out_sock_release;
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001264 *res = sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001266 return 0;
1267
1268out_module_busy:
1269 err = -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270out_module_put:
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001271 sock->ops = NULL;
1272 module_put(pf->owner);
1273out_sock_release:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 sock_release(sock);
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001275 return err;
1276
1277out_release:
1278 rcu_read_unlock();
1279 goto out_sock_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280}
1281
1282int sock_create(int family, int type, int protocol, struct socket **res)
1283{
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001284 return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285}
1286
1287int sock_create_kern(int family, int type, int protocol, struct socket **res)
1288{
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001289 return __sock_create(&init_net, family, type, protocol, res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290}
1291
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001292SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293{
1294 int retval;
1295 struct socket *sock;
Ulrich Dreppera677a032008-07-23 21:29:17 -07001296 int flags;
1297
Ulrich Dreppere38b36f2008-07-23 21:29:42 -07001298 /* Check the SOCK_* constants for consistency. */
1299 BUILD_BUG_ON(SOCK_CLOEXEC != O_CLOEXEC);
1300 BUILD_BUG_ON((SOCK_MAX | SOCK_TYPE_MASK) != SOCK_TYPE_MASK);
1301 BUILD_BUG_ON(SOCK_CLOEXEC & SOCK_TYPE_MASK);
1302 BUILD_BUG_ON(SOCK_NONBLOCK & SOCK_TYPE_MASK);
1303
Ulrich Dreppera677a032008-07-23 21:29:17 -07001304 flags = type & ~SOCK_TYPE_MASK;
Ulrich Drepper77d27202008-07-23 21:29:35 -07001305 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Dreppera677a032008-07-23 21:29:17 -07001306 return -EINVAL;
1307 type &= SOCK_TYPE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001309 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1310 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1311
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 retval = sock_create(family, type, protocol, &sock);
1313 if (retval < 0)
1314 goto out;
1315
Ulrich Drepper77d27202008-07-23 21:29:35 -07001316 retval = sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 if (retval < 0)
1318 goto out_release;
1319
1320out:
1321 /* It may be already another descriptor 8) Not kernel problem. */
1322 return retval;
1323
1324out_release:
1325 sock_release(sock);
1326 return retval;
1327}
1328
1329/*
1330 * Create a pair of connected sockets.
1331 */
1332
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001333SYSCALL_DEFINE4(socketpair, int, family, int, type, int, protocol,
1334 int __user *, usockvec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335{
1336 struct socket *sock1, *sock2;
1337 int fd1, fd2, err;
Al Virodb349502007-02-07 01:48:00 -05001338 struct file *newfile1, *newfile2;
Ulrich Dreppera677a032008-07-23 21:29:17 -07001339 int flags;
1340
1341 flags = type & ~SOCK_TYPE_MASK;
Ulrich Drepper77d27202008-07-23 21:29:35 -07001342 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Dreppera677a032008-07-23 21:29:17 -07001343 return -EINVAL;
1344 type &= SOCK_TYPE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001346 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1347 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1348
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 /*
1350 * Obtain the first socket and check if the underlying protocol
1351 * supports the socketpair call.
1352 */
1353
1354 err = sock_create(family, type, protocol, &sock1);
1355 if (err < 0)
1356 goto out;
1357
1358 err = sock_create(family, type, protocol, &sock2);
1359 if (err < 0)
1360 goto out_release_1;
1361
1362 err = sock1->ops->socketpair(sock1, sock2);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001363 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 goto out_release_both;
1365
Al Viro7cbe66b2009-08-05 19:59:08 +04001366 fd1 = sock_alloc_file(sock1, &newfile1, flags);
David S. Millerbf3c23d2007-10-29 21:54:02 -07001367 if (unlikely(fd1 < 0)) {
1368 err = fd1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 goto out_release_both;
David S. Millerbf3c23d2007-10-29 21:54:02 -07001370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Al Viro7cbe66b2009-08-05 19:59:08 +04001372 fd2 = sock_alloc_file(sock2, &newfile2, flags);
Al Viro198de4d2009-08-05 19:29:23 +04001373 if (unlikely(fd2 < 0)) {
1374 err = fd2;
1375 fput(newfile1);
1376 put_unused_fd(fd1);
1377 sock_release(sock2);
1378 goto out;
Al Virodb349502007-02-07 01:48:00 -05001379 }
1380
Al Viro157cf642008-12-14 04:57:47 -05001381 audit_fd_pair(fd1, fd2);
Al Virodb349502007-02-07 01:48:00 -05001382 fd_install(fd1, newfile1);
1383 fd_install(fd2, newfile2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 /* fd1 and fd2 may be already another descriptors.
1385 * Not kernel problem.
1386 */
1387
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001388 err = put_user(fd1, &usockvec[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 if (!err)
1390 err = put_user(fd2, &usockvec[1]);
1391 if (!err)
1392 return 0;
1393
1394 sys_close(fd2);
1395 sys_close(fd1);
1396 return err;
1397
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398out_release_both:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001399 sock_release(sock2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400out_release_1:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001401 sock_release(sock1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402out:
1403 return err;
1404}
1405
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406/*
1407 * Bind a name to a socket. Nothing much to do here since it's
1408 * the protocol's responsibility to handle the local address.
1409 *
1410 * We move the socket address to kernel space before we call
1411 * the protocol layer (having also checked the address is ok).
1412 */
1413
Heiko Carstens20f37032009-01-14 14:14:23 +01001414SYSCALL_DEFINE3(bind, int, fd, struct sockaddr __user *, umyaddr, int, addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415{
1416 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001417 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001418 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001420 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001421 if (sock) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001422 err = move_addr_to_kernel(umyaddr, addrlen, (struct sockaddr *)&address);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001423 if (err >= 0) {
1424 err = security_socket_bind(sock,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001425 (struct sockaddr *)&address,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001426 addrlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001427 if (!err)
1428 err = sock->ops->bind(sock,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001429 (struct sockaddr *)
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001430 &address, addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 }
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001432 fput_light(sock->file, fput_needed);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 return err;
1435}
1436
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437/*
1438 * Perform a listen. Basically, we allow the protocol to do anything
1439 * necessary for a listen, and if that works, we mark the socket as
1440 * ready for listening.
1441 */
1442
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001443SYSCALL_DEFINE2(listen, int, fd, int, backlog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444{
1445 struct socket *sock;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001446 int err, fput_needed;
Pavel Emelyanovb8e1f9b2007-12-08 00:12:33 -08001447 int somaxconn;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001448
1449 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1450 if (sock) {
Pavel Emelyanov8efa6e92008-03-31 19:41:14 -07001451 somaxconn = sock_net(sock->sk)->core.sysctl_somaxconn;
Pavel Emelyanovb8e1f9b2007-12-08 00:12:33 -08001452 if ((unsigned)backlog > somaxconn)
1453 backlog = somaxconn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
1455 err = security_socket_listen(sock, backlog);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001456 if (!err)
1457 err = sock->ops->listen(sock, backlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001459 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 }
1461 return err;
1462}
1463
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464/*
1465 * For accept, we attempt to create a new socket, set up the link
1466 * with the client, wake up the client, then return the new
1467 * connected fd. We collect the address of the connector in kernel
1468 * space and move it to user at the very end. This is unclean because
1469 * we open the socket then return an error.
1470 *
1471 * 1003.1g adds the ability to recvmsg() to query connection pending
1472 * status to recvmsg. We need to add that support in a way thats
1473 * clean when we restucture accept also.
1474 */
1475
Heiko Carstens20f37032009-01-14 14:14:23 +01001476SYSCALL_DEFINE4(accept4, int, fd, struct sockaddr __user *, upeer_sockaddr,
1477 int __user *, upeer_addrlen, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478{
1479 struct socket *sock, *newsock;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001480 struct file *newfile;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001481 int err, len, newfd, fput_needed;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001482 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
Ulrich Drepper77d27202008-07-23 21:29:35 -07001484 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001485 return -EINVAL;
1486
1487 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1488 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1489
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001490 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 if (!sock)
1492 goto out;
1493
1494 err = -ENFILE;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001495 if (!(newsock = sock_alloc()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 goto out_put;
1497
1498 newsock->type = sock->type;
1499 newsock->ops = sock->ops;
1500
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 /*
1502 * We don't need try_module_get here, as the listening socket (sock)
1503 * has the protocol module (sock->ops->owner) held.
1504 */
1505 __module_get(newsock->ops->owner);
1506
Al Viro7cbe66b2009-08-05 19:59:08 +04001507 newfd = sock_alloc_file(newsock, &newfile, flags);
David S. Miller39d8c1b2006-03-20 17:13:49 -08001508 if (unlikely(newfd < 0)) {
1509 err = newfd;
David S. Miller9a1875e2006-04-01 12:48:36 -08001510 sock_release(newsock);
1511 goto out_put;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001512 }
1513
Frank Filza79af592005-09-27 15:23:38 -07001514 err = security_socket_accept(sock, newsock);
1515 if (err)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001516 goto out_fd;
Frank Filza79af592005-09-27 15:23:38 -07001517
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 err = sock->ops->accept(sock, newsock, sock->file->f_flags);
1519 if (err < 0)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001520 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
1522 if (upeer_sockaddr) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001523 if (newsock->ops->getname(newsock, (struct sockaddr *)&address,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001524 &len, 2) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 err = -ECONNABORTED;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001526 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 }
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001528 err = move_addr_to_user((struct sockaddr *)&address,
1529 len, upeer_sockaddr, upeer_addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 if (err < 0)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001531 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 }
1533
1534 /* File flags are not inherited via accept() unlike another OSes. */
1535
David S. Miller39d8c1b2006-03-20 17:13:49 -08001536 fd_install(newfd, newfile);
1537 err = newfd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001540 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541out:
1542 return err;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001543out_fd:
David S. Miller9606a212006-04-01 01:00:14 -08001544 fput(newfile);
David S. Miller39d8c1b2006-03-20 17:13:49 -08001545 put_unused_fd(newfd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 goto out_put;
1547}
1548
Heiko Carstens20f37032009-01-14 14:14:23 +01001549SYSCALL_DEFINE3(accept, int, fd, struct sockaddr __user *, upeer_sockaddr,
1550 int __user *, upeer_addrlen)
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001551{
Ulrich Drepperde11def2008-11-19 15:36:14 -08001552 return sys_accept4(fd, upeer_sockaddr, upeer_addrlen, 0);
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001553}
1554
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555/*
1556 * Attempt to connect to a socket with the server address. The address
1557 * is in user space so we verify it is OK and move it to kernel space.
1558 *
1559 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
1560 * break bindings
1561 *
1562 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
1563 * other SEQPACKET protocols that take time to connect() as it doesn't
1564 * include the -EINPROGRESS status for such sockets.
1565 */
1566
Heiko Carstens20f37032009-01-14 14:14:23 +01001567SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
1568 int, addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569{
1570 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001571 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001572 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001574 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 if (!sock)
1576 goto out;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001577 err = move_addr_to_kernel(uservaddr, addrlen, (struct sockaddr *)&address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 if (err < 0)
1579 goto out_put;
1580
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001581 err =
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001582 security_socket_connect(sock, (struct sockaddr *)&address, addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 if (err)
1584 goto out_put;
1585
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001586 err = sock->ops->connect(sock, (struct sockaddr *)&address, addrlen,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 sock->file->f_flags);
1588out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001589 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590out:
1591 return err;
1592}
1593
1594/*
1595 * Get the local address ('name') of a socket object. Move the obtained
1596 * name to user space.
1597 */
1598
Heiko Carstens20f37032009-01-14 14:14:23 +01001599SYSCALL_DEFINE3(getsockname, int, fd, struct sockaddr __user *, usockaddr,
1600 int __user *, usockaddr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601{
1602 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001603 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001604 int len, err, fput_needed;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001605
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001606 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 if (!sock)
1608 goto out;
1609
1610 err = security_socket_getsockname(sock);
1611 if (err)
1612 goto out_put;
1613
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001614 err = sock->ops->getname(sock, (struct sockaddr *)&address, &len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 if (err)
1616 goto out_put;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001617 err = move_addr_to_user((struct sockaddr *)&address, len, usockaddr, usockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
1619out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001620 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621out:
1622 return err;
1623}
1624
1625/*
1626 * Get the remote address ('name') of a socket object. Move the obtained
1627 * name to user space.
1628 */
1629
Heiko Carstens20f37032009-01-14 14:14:23 +01001630SYSCALL_DEFINE3(getpeername, int, fd, struct sockaddr __user *, usockaddr,
1631 int __user *, usockaddr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632{
1633 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001634 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001635 int len, err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001637 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1638 if (sock != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 err = security_socket_getpeername(sock);
1640 if (err) {
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001641 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 return err;
1643 }
1644
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001645 err =
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001646 sock->ops->getname(sock, (struct sockaddr *)&address, &len,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001647 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 if (!err)
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001649 err = move_addr_to_user((struct sockaddr *)&address, len, usockaddr,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001650 usockaddr_len);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001651 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 }
1653 return err;
1654}
1655
1656/*
1657 * Send a datagram to a given address. We move the address into kernel
1658 * space and check the user space data area is readable before invoking
1659 * the protocol.
1660 */
1661
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001662SYSCALL_DEFINE6(sendto, int, fd, void __user *, buff, size_t, len,
1663 unsigned, flags, struct sockaddr __user *, addr,
1664 int, addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665{
1666 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001667 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 int err;
1669 struct msghdr msg;
1670 struct iovec iov;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001671 int fput_needed;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001672
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001673 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1674 if (!sock)
David S. Miller4387ff72007-02-08 15:06:08 -08001675 goto out;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001676
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001677 iov.iov_base = buff;
1678 iov.iov_len = len;
1679 msg.msg_name = NULL;
1680 msg.msg_iov = &iov;
1681 msg.msg_iovlen = 1;
1682 msg.msg_control = NULL;
1683 msg.msg_controllen = 0;
1684 msg.msg_namelen = 0;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001685 if (addr) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001686 err = move_addr_to_kernel(addr, addr_len, (struct sockaddr *)&address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 if (err < 0)
1688 goto out_put;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001689 msg.msg_name = (struct sockaddr *)&address;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001690 msg.msg_namelen = addr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 }
1692 if (sock->file->f_flags & O_NONBLOCK)
1693 flags |= MSG_DONTWAIT;
1694 msg.msg_flags = flags;
1695 err = sock_sendmsg(sock, &msg, len);
1696
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001697out_put:
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001698 fput_light(sock->file, fput_needed);
David S. Miller4387ff72007-02-08 15:06:08 -08001699out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 return err;
1701}
1702
1703/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001704 * Send a datagram down a socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 */
1706
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001707SYSCALL_DEFINE4(send, int, fd, void __user *, buff, size_t, len,
1708 unsigned, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709{
1710 return sys_sendto(fd, buff, len, flags, NULL, 0);
1711}
1712
1713/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001714 * Receive a frame from the socket and optionally record the address of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 * sender. We verify the buffers are writable and if needed move the
1716 * sender address from kernel to user space.
1717 */
1718
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001719SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, ubuf, size_t, size,
1720 unsigned, flags, struct sockaddr __user *, addr,
1721 int __user *, addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722{
1723 struct socket *sock;
1724 struct iovec iov;
1725 struct msghdr msg;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001726 struct sockaddr_storage address;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001727 int err, err2;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001728 int fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001730 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 if (!sock)
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001732 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001734 msg.msg_control = NULL;
1735 msg.msg_controllen = 0;
1736 msg.msg_iovlen = 1;
1737 msg.msg_iov = &iov;
1738 iov.iov_len = size;
1739 iov.iov_base = ubuf;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001740 msg.msg_name = (struct sockaddr *)&address;
1741 msg.msg_namelen = sizeof(address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 if (sock->file->f_flags & O_NONBLOCK)
1743 flags |= MSG_DONTWAIT;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001744 err = sock_recvmsg(sock, &msg, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001746 if (err >= 0 && addr != NULL) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001747 err2 = move_addr_to_user((struct sockaddr *)&address,
1748 msg.msg_namelen, addr, addr_len);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001749 if (err2 < 0)
1750 err = err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 }
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001752
1753 fput_light(sock->file, fput_needed);
David S. Miller4387ff72007-02-08 15:06:08 -08001754out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 return err;
1756}
1757
1758/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001759 * Receive a datagram from a socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 */
1761
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001762asmlinkage long sys_recv(int fd, void __user *ubuf, size_t size,
1763 unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764{
1765 return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
1766}
1767
1768/*
1769 * Set a socket option. Because we don't know the option lengths we have
1770 * to pass the user mode parameter for the protocols to sort out.
1771 */
1772
Heiko Carstens20f37032009-01-14 14:14:23 +01001773SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname,
1774 char __user *, optval, int, optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001776 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 struct socket *sock;
1778
1779 if (optlen < 0)
1780 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001781
1782 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1783 if (sock != NULL) {
1784 err = security_socket_setsockopt(sock, level, optname);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001785 if (err)
1786 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
1788 if (level == SOL_SOCKET)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001789 err =
1790 sock_setsockopt(sock, level, optname, optval,
1791 optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 else
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001793 err =
1794 sock->ops->setsockopt(sock, level, optname, optval,
1795 optlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001796out_put:
1797 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 }
1799 return err;
1800}
1801
1802/*
1803 * Get a socket option. Because we don't know the option lengths we have
1804 * to pass a user mode parameter for the protocols to sort out.
1805 */
1806
Heiko Carstens20f37032009-01-14 14:14:23 +01001807SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname,
1808 char __user *, optval, int __user *, optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001810 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 struct socket *sock;
1812
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001813 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1814 if (sock != NULL) {
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001815 err = security_socket_getsockopt(sock, level, optname);
1816 if (err)
1817 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
1819 if (level == SOL_SOCKET)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001820 err =
1821 sock_getsockopt(sock, level, optname, optval,
1822 optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 else
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001824 err =
1825 sock->ops->getsockopt(sock, level, optname, optval,
1826 optlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001827out_put:
1828 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 }
1830 return err;
1831}
1832
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833/*
1834 * Shutdown a socket.
1835 */
1836
Heiko Carstens754fe8d2009-01-14 14:14:09 +01001837SYSCALL_DEFINE2(shutdown, int, fd, int, how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001839 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 struct socket *sock;
1841
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001842 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1843 if (sock != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 err = security_socket_shutdown(sock, how);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001845 if (!err)
1846 err = sock->ops->shutdown(sock, how);
1847 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 }
1849 return err;
1850}
1851
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001852/* A couple of helpful macros for getting the address of the 32/64 bit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 * fields which are the same type (int / unsigned) on our platforms.
1854 */
1855#define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
1856#define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
1857#define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
1858
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859/*
1860 * BSD sendmsg interface
1861 */
1862
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001863SYSCALL_DEFINE3(sendmsg, int, fd, struct msghdr __user *, msg, unsigned, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864{
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001865 struct compat_msghdr __user *msg_compat =
1866 (struct compat_msghdr __user *)msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001868 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
Alex Williamsonb9d717a2005-09-26 14:28:02 -07001870 unsigned char ctl[sizeof(struct cmsghdr) + 20]
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001871 __attribute__ ((aligned(sizeof(__kernel_size_t))));
1872 /* 20 is size of ipv6_pktinfo */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 unsigned char *ctl_buf = ctl;
1874 struct msghdr msg_sys;
1875 int err, ctl_len, iov_size, total_len;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001876 int fput_needed;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001877
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 err = -EFAULT;
1879 if (MSG_CMSG_COMPAT & flags) {
1880 if (get_compat_msghdr(&msg_sys, msg_compat))
1881 return -EFAULT;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001882 }
1883 else if (copy_from_user(&msg_sys, msg, sizeof(struct msghdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 return -EFAULT;
1885
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001886 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001887 if (!sock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 goto out;
1889
1890 /* do not move before msg_sys is valid */
1891 err = -EMSGSIZE;
1892 if (msg_sys.msg_iovlen > UIO_MAXIOV)
1893 goto out_put;
1894
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001895 /* Check whether to allocate the iovec area */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 err = -ENOMEM;
1897 iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
1898 if (msg_sys.msg_iovlen > UIO_FASTIOV) {
1899 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
1900 if (!iov)
1901 goto out_put;
1902 }
1903
1904 /* This will also move the address data into kernel space */
1905 if (MSG_CMSG_COMPAT & flags) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001906 err = verify_compat_iovec(&msg_sys, iov,
1907 (struct sockaddr *)&address,
1908 VERIFY_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 } else
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001910 err = verify_iovec(&msg_sys, iov,
1911 (struct sockaddr *)&address,
1912 VERIFY_READ);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001913 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 goto out_freeiov;
1915 total_len = err;
1916
1917 err = -ENOBUFS;
1918
1919 if (msg_sys.msg_controllen > INT_MAX)
1920 goto out_freeiov;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001921 ctl_len = msg_sys.msg_controllen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001923 err =
1924 cmsghdr_from_user_compat_to_kern(&msg_sys, sock->sk, ctl,
1925 sizeof(ctl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 if (err)
1927 goto out_freeiov;
1928 ctl_buf = msg_sys.msg_control;
Al Viro8920e8f2005-09-07 18:28:51 -07001929 ctl_len = msg_sys.msg_controllen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 } else if (ctl_len) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001931 if (ctl_len > sizeof(ctl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001933 if (ctl_buf == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 goto out_freeiov;
1935 }
1936 err = -EFAULT;
1937 /*
1938 * Careful! Before this, msg_sys.msg_control contains a user pointer.
1939 * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
1940 * checking falls down on this.
1941 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001942 if (copy_from_user(ctl_buf, (void __user *)msg_sys.msg_control,
1943 ctl_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 goto out_freectl;
1945 msg_sys.msg_control = ctl_buf;
1946 }
1947 msg_sys.msg_flags = flags;
1948
1949 if (sock->file->f_flags & O_NONBLOCK)
1950 msg_sys.msg_flags |= MSG_DONTWAIT;
1951 err = sock_sendmsg(sock, &msg_sys, total_len);
1952
1953out_freectl:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001954 if (ctl_buf != ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 sock_kfree_s(sock->sk, ctl_buf, ctl_len);
1956out_freeiov:
1957 if (iov != iovstack)
1958 sock_kfree_s(sock->sk, iov, iov_size);
1959out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001960 fput_light(sock->file, fput_needed);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001961out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 return err;
1963}
1964
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07001965static int __sys_recvmsg(struct socket *sock, struct msghdr __user *msg,
1966 struct msghdr *msg_sys, unsigned flags, int nosec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967{
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001968 struct compat_msghdr __user *msg_compat =
1969 (struct compat_msghdr __user *)msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 struct iovec iovstack[UIO_FASTIOV];
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001971 struct iovec *iov = iovstack;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 unsigned long cmsg_ptr;
1973 int err, iov_size, total_len, len;
1974
1975 /* kernel mode address */
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001976 struct sockaddr_storage addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
1978 /* user mode address pointers */
1979 struct sockaddr __user *uaddr;
1980 int __user *uaddr_len;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001981
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 if (MSG_CMSG_COMPAT & flags) {
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07001983 if (get_compat_msghdr(msg_sys, msg_compat))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 return -EFAULT;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001985 }
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07001986 else if (copy_from_user(msg_sys, msg, sizeof(struct msghdr)))
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001987 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 err = -EMSGSIZE;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07001990 if (msg_sys->msg_iovlen > UIO_MAXIOV)
1991 goto out;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001992
1993 /* Check whether to allocate the iovec area */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 err = -ENOMEM;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07001995 iov_size = msg_sys->msg_iovlen * sizeof(struct iovec);
1996 if (msg_sys->msg_iovlen > UIO_FASTIOV) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
1998 if (!iov)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07001999 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 }
2001
2002 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002003 * Save the user-mode address (verify_iovec will change the
2004 * kernel msghdr to use the kernel address space)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002006
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002007 uaddr = (__force void __user *)msg_sys->msg_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 uaddr_len = COMPAT_NAMELEN(msg);
2009 if (MSG_CMSG_COMPAT & flags) {
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002010 err = verify_compat_iovec(msg_sys, iov,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07002011 (struct sockaddr *)&addr,
2012 VERIFY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 } else
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002014 err = verify_iovec(msg_sys, iov,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07002015 (struct sockaddr *)&addr,
2016 VERIFY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 if (err < 0)
2018 goto out_freeiov;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002019 total_len = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002021 cmsg_ptr = (unsigned long)msg_sys->msg_control;
2022 msg_sys->msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002023
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 if (sock->file->f_flags & O_NONBLOCK)
2025 flags |= MSG_DONTWAIT;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002026 err = (nosec ? sock_recvmsg_nosec : sock_recvmsg)(sock, msg_sys,
2027 total_len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 if (err < 0)
2029 goto out_freeiov;
2030 len = err;
2031
2032 if (uaddr != NULL) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07002033 err = move_addr_to_user((struct sockaddr *)&addr,
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002034 msg_sys->msg_namelen, uaddr,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002035 uaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 if (err < 0)
2037 goto out_freeiov;
2038 }
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002039 err = __put_user((msg_sys->msg_flags & ~MSG_CMSG_COMPAT),
David S. Miller37f7f422005-09-16 16:51:01 -07002040 COMPAT_FLAGS(msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 if (err)
2042 goto out_freeiov;
2043 if (MSG_CMSG_COMPAT & flags)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002044 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 &msg_compat->msg_controllen);
2046 else
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002047 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 &msg->msg_controllen);
2049 if (err)
2050 goto out_freeiov;
2051 err = len;
2052
2053out_freeiov:
2054 if (iov != iovstack)
2055 sock_kfree_s(sock->sk, iov, iov_size);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002056out:
2057 return err;
2058}
2059
2060/*
2061 * BSD recvmsg interface
2062 */
2063
2064SYSCALL_DEFINE3(recvmsg, int, fd, struct msghdr __user *, msg,
2065 unsigned int, flags)
2066{
2067 int fput_needed, err;
2068 struct msghdr msg_sys;
2069 struct socket *sock = sockfd_lookup_light(fd, &err, &fput_needed);
2070
2071 if (!sock)
2072 goto out;
2073
2074 err = __sys_recvmsg(sock, msg, &msg_sys, flags, 0);
2075
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002076 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077out:
2078 return err;
2079}
2080
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002081/*
2082 * Linux recvmmsg interface
2083 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002085int __sys_recvmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
2086 unsigned int flags, struct timespec *timeout)
2087{
2088 int fput_needed, err, datagrams;
2089 struct socket *sock;
2090 struct mmsghdr __user *entry;
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002091 struct compat_mmsghdr __user *compat_entry;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002092 struct msghdr msg_sys;
2093 struct timespec end_time;
2094
2095 if (timeout &&
2096 poll_select_set_timeout(&end_time, timeout->tv_sec,
2097 timeout->tv_nsec))
2098 return -EINVAL;
2099
2100 datagrams = 0;
2101
2102 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2103 if (!sock)
2104 return err;
2105
2106 err = sock_error(sock->sk);
2107 if (err)
2108 goto out_put;
2109
2110 entry = mmsg;
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002111 compat_entry = (struct compat_mmsghdr __user *)mmsg;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002112
2113 while (datagrams < vlen) {
2114 /*
2115 * No need to ask LSM for more than the first datagram.
2116 */
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002117 if (MSG_CMSG_COMPAT & flags) {
2118 err = __sys_recvmsg(sock, (struct msghdr __user *)compat_entry,
2119 &msg_sys, flags, datagrams);
2120 if (err < 0)
2121 break;
2122 err = __put_user(err, &compat_entry->msg_len);
2123 ++compat_entry;
2124 } else {
2125 err = __sys_recvmsg(sock, (struct msghdr __user *)entry,
2126 &msg_sys, flags, datagrams);
2127 if (err < 0)
2128 break;
2129 err = put_user(err, &entry->msg_len);
2130 ++entry;
2131 }
2132
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002133 if (err)
2134 break;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002135 ++datagrams;
2136
2137 if (timeout) {
2138 ktime_get_ts(timeout);
2139 *timeout = timespec_sub(end_time, *timeout);
2140 if (timeout->tv_sec < 0) {
2141 timeout->tv_sec = timeout->tv_nsec = 0;
2142 break;
2143 }
2144
2145 /* Timeout, return less than vlen datagrams */
2146 if (timeout->tv_nsec == 0 && timeout->tv_sec == 0)
2147 break;
2148 }
2149
2150 /* Out of band data, return right away */
2151 if (msg_sys.msg_flags & MSG_OOB)
2152 break;
2153 }
2154
2155out_put:
2156 fput_light(sock->file, fput_needed);
2157
2158 if (err == 0)
2159 return datagrams;
2160
2161 if (datagrams != 0) {
2162 /*
2163 * We may return less entries than requested (vlen) if the
2164 * sock is non block and there aren't enough datagrams...
2165 */
2166 if (err != -EAGAIN) {
2167 /*
2168 * ... or if recvmsg returns an error after we
2169 * received some datagrams, where we record the
2170 * error to return on the next call or if the
2171 * app asks about it using getsockopt(SO_ERROR).
2172 */
2173 sock->sk->sk_err = -err;
2174 }
2175
2176 return datagrams;
2177 }
2178
2179 return err;
2180}
2181
2182SYSCALL_DEFINE5(recvmmsg, int, fd, struct mmsghdr __user *, mmsg,
2183 unsigned int, vlen, unsigned int, flags,
2184 struct timespec __user *, timeout)
2185{
2186 int datagrams;
2187 struct timespec timeout_sys;
2188
2189 if (!timeout)
2190 return __sys_recvmmsg(fd, mmsg, vlen, flags, NULL);
2191
2192 if (copy_from_user(&timeout_sys, timeout, sizeof(timeout_sys)))
2193 return -EFAULT;
2194
2195 datagrams = __sys_recvmmsg(fd, mmsg, vlen, flags, &timeout_sys);
2196
2197 if (datagrams > 0 &&
2198 copy_to_user(timeout, &timeout_sys, sizeof(timeout_sys)))
2199 datagrams = -EFAULT;
2200
2201 return datagrams;
2202}
2203
2204#ifdef __ARCH_WANT_SYS_SOCKETCALL
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205/* Argument list sizes for sys_socketcall */
2206#define AL(x) ((x) * sizeof(unsigned long))
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002207static const unsigned char nargs[20] = {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002208 AL(0),AL(3),AL(3),AL(3),AL(2),AL(3),
2209 AL(3),AL(3),AL(4),AL(4),AL(4),AL(6),
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07002210 AL(6),AL(2),AL(5),AL(5),AL(3),AL(3),
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002211 AL(4),AL(5)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002212};
2213
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214#undef AL
2215
2216/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002217 * System call vectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 *
2219 * Argument checking cleaned up. Saved 20% in size.
2220 * This function doesn't need to set the kernel lock because
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002221 * it is set by the callees.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 */
2223
Heiko Carstens3e0fa652009-01-14 14:14:24 +01002224SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225{
2226 unsigned long a[6];
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002227 unsigned long a0, a1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 int err;
Arjan van de Ven47379052009-09-28 12:57:44 -07002229 unsigned int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002231 if (call < 1 || call > SYS_RECVMMSG)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 return -EINVAL;
2233
Arjan van de Ven47379052009-09-28 12:57:44 -07002234 len = nargs[call];
2235 if (len > sizeof(a))
2236 return -EINVAL;
2237
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 /* copy_from_user should be SMP safe. */
Arjan van de Ven47379052009-09-28 12:57:44 -07002239 if (copy_from_user(a, args, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 return -EFAULT;
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002241
Al Virof3298dc2008-12-10 03:16:51 -05002242 audit_socketcall(nargs[call] / sizeof(unsigned long), a);
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002243
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002244 a0 = a[0];
2245 a1 = a[1];
2246
2247 switch (call) {
2248 case SYS_SOCKET:
2249 err = sys_socket(a0, a1, a[2]);
2250 break;
2251 case SYS_BIND:
2252 err = sys_bind(a0, (struct sockaddr __user *)a1, a[2]);
2253 break;
2254 case SYS_CONNECT:
2255 err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
2256 break;
2257 case SYS_LISTEN:
2258 err = sys_listen(a0, a1);
2259 break;
2260 case SYS_ACCEPT:
Ulrich Drepperde11def2008-11-19 15:36:14 -08002261 err = sys_accept4(a0, (struct sockaddr __user *)a1,
2262 (int __user *)a[2], 0);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002263 break;
2264 case SYS_GETSOCKNAME:
2265 err =
2266 sys_getsockname(a0, (struct sockaddr __user *)a1,
2267 (int __user *)a[2]);
2268 break;
2269 case SYS_GETPEERNAME:
2270 err =
2271 sys_getpeername(a0, (struct sockaddr __user *)a1,
2272 (int __user *)a[2]);
2273 break;
2274 case SYS_SOCKETPAIR:
2275 err = sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
2276 break;
2277 case SYS_SEND:
2278 err = sys_send(a0, (void __user *)a1, a[2], a[3]);
2279 break;
2280 case SYS_SENDTO:
2281 err = sys_sendto(a0, (void __user *)a1, a[2], a[3],
2282 (struct sockaddr __user *)a[4], a[5]);
2283 break;
2284 case SYS_RECV:
2285 err = sys_recv(a0, (void __user *)a1, a[2], a[3]);
2286 break;
2287 case SYS_RECVFROM:
2288 err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
2289 (struct sockaddr __user *)a[4],
2290 (int __user *)a[5]);
2291 break;
2292 case SYS_SHUTDOWN:
2293 err = sys_shutdown(a0, a1);
2294 break;
2295 case SYS_SETSOCKOPT:
2296 err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]);
2297 break;
2298 case SYS_GETSOCKOPT:
2299 err =
2300 sys_getsockopt(a0, a1, a[2], (char __user *)a[3],
2301 (int __user *)a[4]);
2302 break;
2303 case SYS_SENDMSG:
2304 err = sys_sendmsg(a0, (struct msghdr __user *)a1, a[2]);
2305 break;
2306 case SYS_RECVMSG:
2307 err = sys_recvmsg(a0, (struct msghdr __user *)a1, a[2]);
2308 break;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002309 case SYS_RECVMMSG:
2310 err = sys_recvmmsg(a0, (struct mmsghdr __user *)a1, a[2], a[3],
2311 (struct timespec __user *)a[4]);
2312 break;
Ulrich Drepperde11def2008-11-19 15:36:14 -08002313 case SYS_ACCEPT4:
2314 err = sys_accept4(a0, (struct sockaddr __user *)a1,
2315 (int __user *)a[2], a[3]);
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07002316 break;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002317 default:
2318 err = -EINVAL;
2319 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 }
2321 return err;
2322}
2323
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002324#endif /* __ARCH_WANT_SYS_SOCKETCALL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002326/**
2327 * sock_register - add a socket protocol handler
2328 * @ops: description of protocol
2329 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 * This function is called by a protocol handler that wants to
2331 * advertise its address family, and have it linked into the
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002332 * socket interface. The value ops->family coresponds to the
2333 * socket system call protocol family.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 */
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002335int sock_register(const struct net_proto_family *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336{
2337 int err;
2338
2339 if (ops->family >= NPROTO) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002340 printk(KERN_CRIT "protocol %d >= NPROTO(%d)\n", ops->family,
2341 NPROTO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 return -ENOBUFS;
2343 }
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002344
2345 spin_lock(&net_family_lock);
2346 if (net_families[ops->family])
2347 err = -EEXIST;
2348 else {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002349 net_families[ops->family] = ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 err = 0;
2351 }
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002352 spin_unlock(&net_family_lock);
2353
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002354 printk(KERN_INFO "NET: Registered protocol family %d\n", ops->family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 return err;
2356}
2357
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002358/**
2359 * sock_unregister - remove a protocol handler
2360 * @family: protocol family to remove
2361 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 * This function is called by a protocol handler that wants to
2363 * remove its address family, and have it unlinked from the
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002364 * new socket creation.
2365 *
2366 * If protocol handler is a module, then it can use module reference
2367 * counts to protect against new references. If protocol handler is not
2368 * a module then it needs to provide its own protection in
2369 * the ops->create routine.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 */
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002371void sock_unregister(int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372{
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002373 BUG_ON(family < 0 || family >= NPROTO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002375 spin_lock(&net_family_lock);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002376 net_families[family] = NULL;
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002377 spin_unlock(&net_family_lock);
2378
2379 synchronize_rcu();
2380
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002381 printk(KERN_INFO "NET: Unregistered protocol family %d\n", family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382}
2383
Andi Kleen77d76ea2005-12-22 12:43:42 -08002384static int __init sock_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385{
2386 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002387 * Initialize sock SLAB cache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002389
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 sk_init();
2391
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002393 * Initialize skbuff SLAB cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 */
2395 skb_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396
2397 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002398 * Initialize the protocols module.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 */
2400
2401 init_inodecache();
2402 register_filesystem(&sock_fs_type);
2403 sock_mnt = kern_mount(&sock_fs_type);
Andi Kleen77d76ea2005-12-22 12:43:42 -08002404
2405 /* The real protocol initialization is performed in later initcalls.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 */
2407
2408#ifdef CONFIG_NETFILTER
2409 netfilter_init();
2410#endif
David S. Millercbeb3212005-12-22 12:58:55 -08002411
2412 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413}
2414
Andi Kleen77d76ea2005-12-22 12:43:42 -08002415core_initcall(sock_init); /* early initcall */
2416
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417#ifdef CONFIG_PROC_FS
2418void socket_seq_show(struct seq_file *seq)
2419{
2420 int cpu;
2421 int counter = 0;
2422
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -07002423 for_each_possible_cpu(cpu)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002424 counter += per_cpu(sockets_in_use, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425
2426 /* It can be negative, by the way. 8) */
2427 if (counter < 0)
2428 counter = 0;
2429
2430 seq_printf(seq, "sockets: used %d\n", counter);
2431}
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002432#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433
Shaun Pereira89bbfc92006-03-21 23:58:08 -08002434#ifdef CONFIG_COMPAT
Arnd Bergmann6b960182009-11-06 23:10:54 -08002435static int do_siocgstamp(struct net *net, struct socket *sock,
2436 unsigned int cmd, struct compat_timeval __user *up)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002437{
Arnd Bergmann7a229382009-11-06 23:00:29 -08002438 mm_segment_t old_fs = get_fs();
2439 struct timeval ktv;
2440 int err;
2441
2442 set_fs(KERNEL_DS);
Arnd Bergmann6b960182009-11-06 23:10:54 -08002443 err = sock_do_ioctl(net, sock, cmd, (unsigned long)&ktv);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002444 set_fs(old_fs);
2445 if (!err) {
2446 err = put_user(ktv.tv_sec, &up->tv_sec);
2447 err |= __put_user(ktv.tv_usec, &up->tv_usec);
2448 }
2449 return err;
2450}
2451
Arnd Bergmann6b960182009-11-06 23:10:54 -08002452static int do_siocgstampns(struct net *net, struct socket *sock,
2453 unsigned int cmd, struct compat_timespec __user *up)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002454{
Arnd Bergmann7a229382009-11-06 23:00:29 -08002455 mm_segment_t old_fs = get_fs();
2456 struct timespec kts;
2457 int err;
2458
2459 set_fs(KERNEL_DS);
Arnd Bergmann6b960182009-11-06 23:10:54 -08002460 err = sock_do_ioctl(net, sock, cmd, (unsigned long)&kts);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002461 set_fs(old_fs);
2462 if (!err) {
2463 err = put_user(kts.tv_sec, &up->tv_sec);
2464 err |= __put_user(kts.tv_nsec, &up->tv_nsec);
2465 }
2466 return err;
2467}
2468
Arnd Bergmann6b960182009-11-06 23:10:54 -08002469static int dev_ifname32(struct net *net, struct compat_ifreq __user *uifr32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002470{
2471 struct ifreq __user *uifr;
2472 int err;
2473
2474 uifr = compat_alloc_user_space(sizeof(struct ifreq));
Arnd Bergmann6b960182009-11-06 23:10:54 -08002475 if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002476 return -EFAULT;
2477
Arnd Bergmann6b960182009-11-06 23:10:54 -08002478 err = dev_ioctl(net, SIOCGIFNAME, uifr);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002479 if (err)
2480 return err;
2481
Arnd Bergmann6b960182009-11-06 23:10:54 -08002482 if (copy_in_user(uifr32, uifr, sizeof(struct compat_ifreq)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002483 return -EFAULT;
2484
2485 return 0;
2486}
2487
Arnd Bergmann6b960182009-11-06 23:10:54 -08002488static int dev_ifconf(struct net *net, struct compat_ifconf __user *uifc32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002489{
Arnd Bergmann6b960182009-11-06 23:10:54 -08002490 struct compat_ifconf ifc32;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002491 struct ifconf ifc;
2492 struct ifconf __user *uifc;
Arnd Bergmann6b960182009-11-06 23:10:54 -08002493 struct compat_ifreq __user *ifr32;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002494 struct ifreq __user *ifr;
2495 unsigned int i, j;
2496 int err;
2497
Arnd Bergmann6b960182009-11-06 23:10:54 -08002498 if (copy_from_user(&ifc32, uifc32, sizeof(struct compat_ifconf)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002499 return -EFAULT;
2500
2501 if (ifc32.ifcbuf == 0) {
2502 ifc32.ifc_len = 0;
2503 ifc.ifc_len = 0;
2504 ifc.ifc_req = NULL;
2505 uifc = compat_alloc_user_space(sizeof(struct ifconf));
2506 } else {
Arnd Bergmann6b960182009-11-06 23:10:54 -08002507 size_t len =((ifc32.ifc_len / sizeof (struct compat_ifreq)) + 1) *
Arnd Bergmann7a229382009-11-06 23:00:29 -08002508 sizeof (struct ifreq);
2509 uifc = compat_alloc_user_space(sizeof(struct ifconf) + len);
2510 ifc.ifc_len = len;
2511 ifr = ifc.ifc_req = (void __user *)(uifc + 1);
2512 ifr32 = compat_ptr(ifc32.ifcbuf);
Arnd Bergmann6b960182009-11-06 23:10:54 -08002513 for (i = 0; i < ifc32.ifc_len; i += sizeof (struct compat_ifreq)) {
2514 if (copy_in_user(ifr, ifr32, sizeof(struct compat_ifreq)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002515 return -EFAULT;
2516 ifr++;
2517 ifr32++;
2518 }
2519 }
2520 if (copy_to_user(uifc, &ifc, sizeof(struct ifconf)))
2521 return -EFAULT;
2522
Arnd Bergmann6b960182009-11-06 23:10:54 -08002523 err = dev_ioctl(net, SIOCGIFCONF, uifc);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002524 if (err)
2525 return err;
2526
2527 if (copy_from_user(&ifc, uifc, sizeof(struct ifconf)))
2528 return -EFAULT;
2529
2530 ifr = ifc.ifc_req;
2531 ifr32 = compat_ptr(ifc32.ifcbuf);
2532 for (i = 0, j = 0;
Arnd Bergmann6b960182009-11-06 23:10:54 -08002533 i + sizeof (struct compat_ifreq) <= ifc32.ifc_len && j < ifc.ifc_len;
2534 i += sizeof (struct compat_ifreq), j += sizeof (struct ifreq)) {
2535 if (copy_in_user(ifr32, ifr, sizeof (struct compat_ifreq)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002536 return -EFAULT;
2537 ifr32++;
2538 ifr++;
2539 }
2540
2541 if (ifc32.ifcbuf == 0) {
2542 /* Translate from 64-bit structure multiple to
2543 * a 32-bit one.
2544 */
2545 i = ifc.ifc_len;
Arnd Bergmann6b960182009-11-06 23:10:54 -08002546 i = ((i / sizeof(struct ifreq)) * sizeof(struct compat_ifreq));
Arnd Bergmann7a229382009-11-06 23:00:29 -08002547 ifc32.ifc_len = i;
2548 } else {
2549 ifc32.ifc_len = i;
2550 }
Arnd Bergmann6b960182009-11-06 23:10:54 -08002551 if (copy_to_user(uifc32, &ifc32, sizeof(struct compat_ifconf)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002552 return -EFAULT;
2553
2554 return 0;
2555}
2556
Arnd Bergmann6b960182009-11-06 23:10:54 -08002557static int ethtool_ioctl(struct net *net, struct compat_ifreq __user *ifr32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002558{
2559 struct ifreq __user *ifr;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002560 u32 data;
2561 void __user *datap;
2562
2563 ifr = compat_alloc_user_space(sizeof(*ifr));
Arnd Bergmann7a229382009-11-06 23:00:29 -08002564
2565 if (copy_in_user(&ifr->ifr_name, &ifr32->ifr_name, IFNAMSIZ))
2566 return -EFAULT;
2567
2568 if (get_user(data, &ifr32->ifr_ifru.ifru_data))
2569 return -EFAULT;
2570
2571 datap = compat_ptr(data);
2572 if (put_user(datap, &ifr->ifr_ifru.ifru_data))
2573 return -EFAULT;
2574
Arnd Bergmann6b960182009-11-06 23:10:54 -08002575 return dev_ioctl(net, SIOCETHTOOL, ifr);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002576}
2577
Arnd Bergmann7a50a242009-11-08 20:57:03 -08002578static int compat_siocwandev(struct net *net, struct compat_ifreq __user *uifr32)
2579{
2580 void __user *uptr;
2581 compat_uptr_t uptr32;
2582 struct ifreq __user *uifr;
2583
2584 uifr = compat_alloc_user_space(sizeof (*uifr));
2585 if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
2586 return -EFAULT;
2587
2588 if (get_user(uptr32, &uifr32->ifr_settings.ifs_ifsu))
2589 return -EFAULT;
2590
2591 uptr = compat_ptr(uptr32);
2592
2593 if (put_user(uptr, &uifr->ifr_settings.ifs_ifsu.raw_hdlc))
2594 return -EFAULT;
2595
2596 return dev_ioctl(net, SIOCWANDEV, uifr);
2597}
2598
Arnd Bergmann6b960182009-11-06 23:10:54 -08002599static int bond_ioctl(struct net *net, unsigned int cmd,
2600 struct compat_ifreq __user *ifr32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002601{
2602 struct ifreq kifr;
2603 struct ifreq __user *uifr;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002604 mm_segment_t old_fs;
2605 int err;
2606 u32 data;
2607 void __user *datap;
2608
2609 switch (cmd) {
2610 case SIOCBONDENSLAVE:
2611 case SIOCBONDRELEASE:
2612 case SIOCBONDSETHWADDR:
2613 case SIOCBONDCHANGEACTIVE:
Arnd Bergmann6b960182009-11-06 23:10:54 -08002614 if (copy_from_user(&kifr, ifr32, sizeof(struct compat_ifreq)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002615 return -EFAULT;
2616
2617 old_fs = get_fs();
2618 set_fs (KERNEL_DS);
Arnd Bergmann6b960182009-11-06 23:10:54 -08002619 err = dev_ioctl(net, cmd, &kifr);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002620 set_fs (old_fs);
2621
2622 return err;
2623 case SIOCBONDSLAVEINFOQUERY:
2624 case SIOCBONDINFOQUERY:
2625 uifr = compat_alloc_user_space(sizeof(*uifr));
2626 if (copy_in_user(&uifr->ifr_name, &ifr32->ifr_name, IFNAMSIZ))
2627 return -EFAULT;
2628
2629 if (get_user(data, &ifr32->ifr_ifru.ifru_data))
2630 return -EFAULT;
2631
2632 datap = compat_ptr(data);
2633 if (put_user(datap, &uifr->ifr_ifru.ifru_data))
2634 return -EFAULT;
2635
Arnd Bergmann6b960182009-11-06 23:10:54 -08002636 return dev_ioctl(net, cmd, uifr);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002637 default:
2638 return -EINVAL;
2639 };
2640}
2641
Arnd Bergmann6b960182009-11-06 23:10:54 -08002642static int siocdevprivate_ioctl(struct net *net, unsigned int cmd,
2643 struct compat_ifreq __user *u_ifreq32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002644{
2645 struct ifreq __user *u_ifreq64;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002646 char tmp_buf[IFNAMSIZ];
2647 void __user *data64;
2648 u32 data32;
2649
2650 if (copy_from_user(&tmp_buf[0], &(u_ifreq32->ifr_ifrn.ifrn_name[0]),
2651 IFNAMSIZ))
2652 return -EFAULT;
2653 if (__get_user(data32, &u_ifreq32->ifr_ifru.ifru_data))
2654 return -EFAULT;
2655 data64 = compat_ptr(data32);
2656
2657 u_ifreq64 = compat_alloc_user_space(sizeof(*u_ifreq64));
2658
2659 /* Don't check these user accesses, just let that get trapped
2660 * in the ioctl handler instead.
2661 */
2662 if (copy_to_user(&u_ifreq64->ifr_ifrn.ifrn_name[0], &tmp_buf[0],
2663 IFNAMSIZ))
2664 return -EFAULT;
2665 if (__put_user(data64, &u_ifreq64->ifr_ifru.ifru_data))
2666 return -EFAULT;
2667
Arnd Bergmann6b960182009-11-06 23:10:54 -08002668 return dev_ioctl(net, cmd, u_ifreq64);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002669}
2670
Arnd Bergmann6b960182009-11-06 23:10:54 -08002671static int dev_ifsioc(struct net *net, struct socket *sock,
2672 unsigned int cmd, struct compat_ifreq __user *uifr32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002673{
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00002674 struct ifreq __user *uifr;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002675 int err;
2676
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00002677 uifr = compat_alloc_user_space(sizeof(*uifr));
2678 if (copy_in_user(uifr, uifr32, sizeof(*uifr32)))
2679 return -EFAULT;
2680
2681 err = sock_do_ioctl(net, sock, cmd, (unsigned long)uifr);
2682
Arnd Bergmann7a229382009-11-06 23:00:29 -08002683 if (!err) {
2684 switch (cmd) {
2685 case SIOCGIFFLAGS:
2686 case SIOCGIFMETRIC:
2687 case SIOCGIFMTU:
2688 case SIOCGIFMEM:
2689 case SIOCGIFHWADDR:
2690 case SIOCGIFINDEX:
2691 case SIOCGIFADDR:
2692 case SIOCGIFBRDADDR:
2693 case SIOCGIFDSTADDR:
2694 case SIOCGIFNETMASK:
Arnd Bergmannfab25322009-11-08 20:56:21 -08002695 case SIOCGIFPFLAGS:
Arnd Bergmann7a229382009-11-06 23:00:29 -08002696 case SIOCGIFTXQLEN:
Arnd Bergmannfab25322009-11-08 20:56:21 -08002697 case SIOCGMIIPHY:
2698 case SIOCGMIIREG:
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00002699 if (copy_in_user(uifr32, uifr, sizeof(*uifr32)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002700 err = -EFAULT;
2701 break;
2702 }
2703 }
2704 return err;
2705}
2706
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00002707static int compat_sioc_ifmap(struct net *net, unsigned int cmd,
2708 struct compat_ifreq __user *uifr32)
2709{
2710 struct ifreq ifr;
2711 struct compat_ifmap __user *uifmap32;
2712 mm_segment_t old_fs;
2713 int err;
2714
2715 uifmap32 = &uifr32->ifr_ifru.ifru_map;
2716 err = copy_from_user(&ifr, uifr32, sizeof(ifr.ifr_name));
2717 err |= __get_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
2718 err |= __get_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
2719 err |= __get_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
2720 err |= __get_user(ifr.ifr_map.irq, &uifmap32->irq);
2721 err |= __get_user(ifr.ifr_map.dma, &uifmap32->dma);
2722 err |= __get_user(ifr.ifr_map.port, &uifmap32->port);
2723 if (err)
2724 return -EFAULT;
2725
2726 old_fs = get_fs();
2727 set_fs (KERNEL_DS);
2728 err = dev_ioctl(net, cmd, (void __user *)&ifr);
2729 set_fs (old_fs);
2730
2731 if (cmd == SIOCGIFMAP && !err) {
2732 err = copy_to_user(uifr32, &ifr, sizeof(ifr.ifr_name));
2733 err |= __put_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
2734 err |= __put_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
2735 err |= __put_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
2736 err |= __put_user(ifr.ifr_map.irq, &uifmap32->irq);
2737 err |= __put_user(ifr.ifr_map.dma, &uifmap32->dma);
2738 err |= __put_user(ifr.ifr_map.port, &uifmap32->port);
2739 if (err)
2740 err = -EFAULT;
2741 }
2742 return err;
2743}
2744
2745static int compat_siocshwtstamp(struct net *net, struct compat_ifreq __user *uifr32)
2746{
2747 void __user *uptr;
2748 compat_uptr_t uptr32;
2749 struct ifreq __user *uifr;
2750
2751 uifr = compat_alloc_user_space(sizeof (*uifr));
2752 if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
2753 return -EFAULT;
2754
2755 if (get_user(uptr32, &uifr32->ifr_data))
2756 return -EFAULT;
2757
2758 uptr = compat_ptr(uptr32);
2759
2760 if (put_user(uptr, &uifr->ifr_data))
2761 return -EFAULT;
2762
2763 return dev_ioctl(net, SIOCSHWTSTAMP, uifr);
2764}
2765
Arnd Bergmann7a229382009-11-06 23:00:29 -08002766struct rtentry32 {
2767 u32 rt_pad1;
2768 struct sockaddr rt_dst; /* target address */
2769 struct sockaddr rt_gateway; /* gateway addr (RTF_GATEWAY) */
2770 struct sockaddr rt_genmask; /* target network mask (IP) */
2771 unsigned short rt_flags;
2772 short rt_pad2;
2773 u32 rt_pad3;
2774 unsigned char rt_tos;
2775 unsigned char rt_class;
2776 short rt_pad4;
2777 short rt_metric; /* +1 for binary compatibility! */
2778 /* char * */ u32 rt_dev; /* forcing the device at add */
2779 u32 rt_mtu; /* per route MTU/Window */
2780 u32 rt_window; /* Window clamping */
2781 unsigned short rt_irtt; /* Initial RTT */
2782};
2783
2784struct in6_rtmsg32 {
2785 struct in6_addr rtmsg_dst;
2786 struct in6_addr rtmsg_src;
2787 struct in6_addr rtmsg_gateway;
2788 u32 rtmsg_type;
2789 u16 rtmsg_dst_len;
2790 u16 rtmsg_src_len;
2791 u32 rtmsg_metric;
2792 u32 rtmsg_info;
2793 u32 rtmsg_flags;
2794 s32 rtmsg_ifindex;
2795};
2796
Arnd Bergmann6b960182009-11-06 23:10:54 -08002797static int routing_ioctl(struct net *net, struct socket *sock,
2798 unsigned int cmd, void __user *argp)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002799{
2800 int ret;
2801 void *r = NULL;
2802 struct in6_rtmsg r6;
2803 struct rtentry r4;
2804 char devname[16];
2805 u32 rtdev;
2806 mm_segment_t old_fs = get_fs();
2807
Arnd Bergmann6b960182009-11-06 23:10:54 -08002808 if (sock && sock->sk && sock->sk->sk_family == AF_INET6) { /* ipv6 */
2809 struct in6_rtmsg32 __user *ur6 = argp;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002810 ret = copy_from_user (&r6.rtmsg_dst, &(ur6->rtmsg_dst),
2811 3 * sizeof(struct in6_addr));
2812 ret |= __get_user (r6.rtmsg_type, &(ur6->rtmsg_type));
2813 ret |= __get_user (r6.rtmsg_dst_len, &(ur6->rtmsg_dst_len));
2814 ret |= __get_user (r6.rtmsg_src_len, &(ur6->rtmsg_src_len));
2815 ret |= __get_user (r6.rtmsg_metric, &(ur6->rtmsg_metric));
2816 ret |= __get_user (r6.rtmsg_info, &(ur6->rtmsg_info));
2817 ret |= __get_user (r6.rtmsg_flags, &(ur6->rtmsg_flags));
2818 ret |= __get_user (r6.rtmsg_ifindex, &(ur6->rtmsg_ifindex));
2819
2820 r = (void *) &r6;
2821 } else { /* ipv4 */
Arnd Bergmann6b960182009-11-06 23:10:54 -08002822 struct rtentry32 __user *ur4 = argp;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002823 ret = copy_from_user (&r4.rt_dst, &(ur4->rt_dst),
2824 3 * sizeof(struct sockaddr));
2825 ret |= __get_user (r4.rt_flags, &(ur4->rt_flags));
2826 ret |= __get_user (r4.rt_metric, &(ur4->rt_metric));
2827 ret |= __get_user (r4.rt_mtu, &(ur4->rt_mtu));
2828 ret |= __get_user (r4.rt_window, &(ur4->rt_window));
2829 ret |= __get_user (r4.rt_irtt, &(ur4->rt_irtt));
2830 ret |= __get_user (rtdev, &(ur4->rt_dev));
2831 if (rtdev) {
2832 ret |= copy_from_user (devname, compat_ptr(rtdev), 15);
2833 r4.rt_dev = devname; devname[15] = 0;
2834 } else
2835 r4.rt_dev = NULL;
2836
2837 r = (void *) &r4;
2838 }
2839
2840 if (ret) {
2841 ret = -EFAULT;
2842 goto out;
2843 }
2844
2845 set_fs (KERNEL_DS);
Arnd Bergmann6b960182009-11-06 23:10:54 -08002846 ret = sock_do_ioctl(net, sock, cmd, (unsigned long) r);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002847 set_fs (old_fs);
2848
2849out:
Arnd Bergmann7a229382009-11-06 23:00:29 -08002850 return ret;
2851}
2852
2853/* Since old style bridge ioctl's endup using SIOCDEVPRIVATE
2854 * for some operations; this forces use of the newer bridge-utils that
2855 * use compatiable ioctls
2856 */
Arnd Bergmann6b960182009-11-06 23:10:54 -08002857static int old_bridge_ioctl(compat_ulong_t __user *argp)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002858{
Arnd Bergmann6b960182009-11-06 23:10:54 -08002859 compat_ulong_t tmp;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002860
Arnd Bergmann6b960182009-11-06 23:10:54 -08002861 if (get_user(tmp, argp))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002862 return -EFAULT;
2863 if (tmp == BRCTL_GET_VERSION)
2864 return BRCTL_VERSION + 1;
2865 return -EINVAL;
2866}
2867
Arnd Bergmann6b960182009-11-06 23:10:54 -08002868static int compat_sock_ioctl_trans(struct file *file, struct socket *sock,
2869 unsigned int cmd, unsigned long arg)
2870{
2871 void __user *argp = compat_ptr(arg);
2872 struct sock *sk = sock->sk;
2873 struct net *net = sock_net(sk);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002874
Arnd Bergmann6b960182009-11-06 23:10:54 -08002875 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15))
2876 return siocdevprivate_ioctl(net, cmd, argp);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002877
Arnd Bergmann6b960182009-11-06 23:10:54 -08002878 switch (cmd) {
2879 case SIOCSIFBR:
2880 case SIOCGIFBR:
2881 return old_bridge_ioctl(argp);
2882 case SIOCGIFNAME:
2883 return dev_ifname32(net, argp);
2884 case SIOCGIFCONF:
2885 return dev_ifconf(net, argp);
2886 case SIOCETHTOOL:
2887 return ethtool_ioctl(net, argp);
Arnd Bergmann7a50a242009-11-08 20:57:03 -08002888 case SIOCWANDEV:
2889 return compat_siocwandev(net, argp);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00002890 case SIOCGIFMAP:
2891 case SIOCSIFMAP:
2892 return compat_sioc_ifmap(net, cmd, argp);
Arnd Bergmann6b960182009-11-06 23:10:54 -08002893 case SIOCBONDENSLAVE:
2894 case SIOCBONDRELEASE:
2895 case SIOCBONDSETHWADDR:
2896 case SIOCBONDSLAVEINFOQUERY:
2897 case SIOCBONDINFOQUERY:
2898 case SIOCBONDCHANGEACTIVE:
2899 return bond_ioctl(net, cmd, argp);
2900 case SIOCADDRT:
2901 case SIOCDELRT:
2902 return routing_ioctl(net, sock, cmd, argp);
2903 case SIOCGSTAMP:
2904 return do_siocgstamp(net, sock, cmd, argp);
2905 case SIOCGSTAMPNS:
2906 return do_siocgstampns(net, sock, cmd, argp);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00002907 case SIOCSHWTSTAMP:
2908 return compat_siocshwtstamp(net, argp);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002909
Arnd Bergmann6b960182009-11-06 23:10:54 -08002910 case FIOSETOWN:
2911 case SIOCSPGRP:
2912 case FIOGETOWN:
2913 case SIOCGPGRP:
2914 case SIOCBRADDBR:
2915 case SIOCBRDELBR:
2916 case SIOCGIFVLAN:
2917 case SIOCSIFVLAN:
2918 case SIOCADDDLCI:
2919 case SIOCDELDLCI:
2920 return sock_ioctl(file, cmd, arg);
2921
2922 case SIOCGIFFLAGS:
2923 case SIOCSIFFLAGS:
2924 case SIOCGIFMETRIC:
2925 case SIOCSIFMETRIC:
2926 case SIOCGIFMTU:
2927 case SIOCSIFMTU:
2928 case SIOCGIFMEM:
2929 case SIOCSIFMEM:
2930 case SIOCGIFHWADDR:
2931 case SIOCSIFHWADDR:
2932 case SIOCADDMULTI:
2933 case SIOCDELMULTI:
2934 case SIOCGIFINDEX:
Arnd Bergmann6b960182009-11-06 23:10:54 -08002935 case SIOCGIFADDR:
2936 case SIOCSIFADDR:
2937 case SIOCSIFHWBROADCAST:
Arnd Bergmann6b960182009-11-06 23:10:54 -08002938 case SIOCDIFADDR:
Arnd Bergmann6b960182009-11-06 23:10:54 -08002939 case SIOCGIFBRDADDR:
2940 case SIOCSIFBRDADDR:
2941 case SIOCGIFDSTADDR:
2942 case SIOCSIFDSTADDR:
2943 case SIOCGIFNETMASK:
2944 case SIOCSIFNETMASK:
2945 case SIOCSIFPFLAGS:
2946 case SIOCGIFPFLAGS:
2947 case SIOCGIFTXQLEN:
2948 case SIOCSIFTXQLEN:
2949 case SIOCBRADDIF:
2950 case SIOCBRDELIF:
Arnd Bergmann9177efd2009-11-06 08:09:09 +00002951 case SIOCSIFNAME:
2952 case SIOCGMIIPHY:
2953 case SIOCGMIIREG:
2954 case SIOCSMIIREG:
Arnd Bergmann6b960182009-11-06 23:10:54 -08002955 return dev_ifsioc(net, sock, cmd, argp);
Arnd Bergmann9177efd2009-11-06 08:09:09 +00002956
Arnd Bergmann6b960182009-11-06 23:10:54 -08002957 case SIOCSARP:
2958 case SIOCGARP:
2959 case SIOCDARP:
Arnd Bergmann6b960182009-11-06 23:10:54 -08002960 case SIOCATMARK:
Arnd Bergmann9177efd2009-11-06 08:09:09 +00002961 return sock_do_ioctl(net, sock, cmd, arg);
2962 }
2963
2964 /* Prevent warning from compat_sys_ioctl, these always
2965 * result in -EINVAL in the native case anyway. */
2966 switch (cmd) {
2967 case SIOCRTMSG:
2968 case SIOCGIFCOUNT:
Arnd Bergmann6b960182009-11-06 23:10:54 -08002969 case SIOCSRARP:
2970 case SIOCGRARP:
2971 case SIOCDRARP:
Arnd Bergmann9177efd2009-11-06 08:09:09 +00002972 case SIOCSIFLINK:
2973 case SIOCGIFSLAVE:
2974 case SIOCSIFSLAVE:
2975 return -EINVAL;
Arnd Bergmann6b960182009-11-06 23:10:54 -08002976 }
2977
2978 return -ENOIOCTLCMD;
2979}
Arnd Bergmann7a229382009-11-06 23:00:29 -08002980
Shaun Pereira89bbfc92006-03-21 23:58:08 -08002981static long compat_sock_ioctl(struct file *file, unsigned cmd,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002982 unsigned long arg)
Shaun Pereira89bbfc92006-03-21 23:58:08 -08002983{
2984 struct socket *sock = file->private_data;
2985 int ret = -ENOIOCTLCMD;
David S. Miller87de87d2008-06-03 09:14:03 -07002986 struct sock *sk;
2987 struct net *net;
2988
2989 sk = sock->sk;
2990 net = sock_net(sk);
Shaun Pereira89bbfc92006-03-21 23:58:08 -08002991
2992 if (sock->ops->compat_ioctl)
2993 ret = sock->ops->compat_ioctl(sock, cmd, arg);
2994
David S. Miller87de87d2008-06-03 09:14:03 -07002995 if (ret == -ENOIOCTLCMD &&
2996 (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST))
2997 ret = compat_wext_handle_ioctl(net, cmd, arg);
2998
Arnd Bergmann6b960182009-11-06 23:10:54 -08002999 if (ret == -ENOIOCTLCMD)
3000 ret = compat_sock_ioctl_trans(file, sock, cmd, arg);
3001
Shaun Pereira89bbfc92006-03-21 23:58:08 -08003002 return ret;
3003}
3004#endif
3005
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003006int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen)
3007{
3008 return sock->ops->bind(sock, addr, addrlen);
3009}
3010
3011int kernel_listen(struct socket *sock, int backlog)
3012{
3013 return sock->ops->listen(sock, backlog);
3014}
3015
3016int kernel_accept(struct socket *sock, struct socket **newsock, int flags)
3017{
3018 struct sock *sk = sock->sk;
3019 int err;
3020
3021 err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
3022 newsock);
3023 if (err < 0)
3024 goto done;
3025
3026 err = sock->ops->accept(sock, *newsock, flags);
3027 if (err < 0) {
3028 sock_release(*newsock);
Tony Battersbyfa8705b2007-10-10 21:09:04 -07003029 *newsock = NULL;
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003030 goto done;
3031 }
3032
3033 (*newsock)->ops = sock->ops;
Wei Yongjun1b085342008-12-18 19:35:10 -08003034 __module_get((*newsock)->ops->owner);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003035
3036done:
3037 return err;
3038}
3039
3040int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
YOSHIFUJI Hideaki4768fbc2007-02-09 23:25:31 +09003041 int flags)
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003042{
3043 return sock->ops->connect(sock, addr, addrlen, flags);
3044}
3045
3046int kernel_getsockname(struct socket *sock, struct sockaddr *addr,
3047 int *addrlen)
3048{
3049 return sock->ops->getname(sock, addr, addrlen, 0);
3050}
3051
3052int kernel_getpeername(struct socket *sock, struct sockaddr *addr,
3053 int *addrlen)
3054{
3055 return sock->ops->getname(sock, addr, addrlen, 1);
3056}
3057
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}
3073
3074int kernel_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07003075 char *optval, unsigned int optlen)
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003076{
3077 mm_segment_t oldfs = get_fs();
3078 int err;
3079
3080 set_fs(KERNEL_DS);
3081 if (level == SOL_SOCKET)
3082 err = sock_setsockopt(sock, level, optname, optval, optlen);
3083 else
3084 err = sock->ops->setsockopt(sock, level, optname, optval,
3085 optlen);
3086 set_fs(oldfs);
3087 return err;
3088}
3089
3090int kernel_sendpage(struct socket *sock, struct page *page, int offset,
3091 size_t size, int flags)
3092{
3093 if (sock->ops->sendpage)
3094 return sock->ops->sendpage(sock, page, offset, size, flags);
3095
3096 return sock_no_sendpage(sock, page, offset, size, flags);
3097}
3098
3099int kernel_sock_ioctl(struct socket *sock, int cmd, unsigned long arg)
3100{
3101 mm_segment_t oldfs = get_fs();
3102 int err;
3103
3104 set_fs(KERNEL_DS);
3105 err = sock->ops->ioctl(sock, cmd, arg);
3106 set_fs(oldfs);
3107
3108 return err;
3109}
3110
Trond Myklebust91cf45f2007-11-12 18:10:39 -08003111int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how)
3112{
3113 return sock->ops->shutdown(sock, how);
3114}
3115
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116EXPORT_SYMBOL(sock_create);
3117EXPORT_SYMBOL(sock_create_kern);
3118EXPORT_SYMBOL(sock_create_lite);
3119EXPORT_SYMBOL(sock_map_fd);
3120EXPORT_SYMBOL(sock_recvmsg);
3121EXPORT_SYMBOL(sock_register);
3122EXPORT_SYMBOL(sock_release);
3123EXPORT_SYMBOL(sock_sendmsg);
3124EXPORT_SYMBOL(sock_unregister);
3125EXPORT_SYMBOL(sock_wake_async);
3126EXPORT_SYMBOL(sockfd_lookup);
3127EXPORT_SYMBOL(kernel_sendmsg);
3128EXPORT_SYMBOL(kernel_recvmsg);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003129EXPORT_SYMBOL(kernel_bind);
3130EXPORT_SYMBOL(kernel_listen);
3131EXPORT_SYMBOL(kernel_accept);
3132EXPORT_SYMBOL(kernel_connect);
3133EXPORT_SYMBOL(kernel_getsockname);
3134EXPORT_SYMBOL(kernel_getpeername);
3135EXPORT_SYMBOL(kernel_getsockopt);
3136EXPORT_SYMBOL(kernel_setsockopt);
3137EXPORT_SYMBOL(kernel_sendpage);
3138EXPORT_SYMBOL(kernel_sock_ioctl);
Trond Myklebust91cf45f2007-11-12 18:10:39 -08003139EXPORT_SYMBOL(kernel_sock_shutdown);