blob: 344bd230b8312b5b9385a933f7b92a5142ee933c [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
100static int sock_no_open(struct inode *irrelevant, struct file *dontcare);
Badari Pulavarty027445c2006-09-30 23:28:46 -0700101static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov,
102 unsigned long nr_segs, loff_t pos);
103static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov,
104 unsigned long nr_segs, loff_t pos);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700105static int sock_mmap(struct file *file, struct vm_area_struct *vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107static int sock_close(struct inode *inode, struct file *file);
108static unsigned int sock_poll(struct file *file,
109 struct poll_table_struct *wait);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700110static long sock_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
Shaun Pereira89bbfc92006-03-21 23:58:08 -0800111#ifdef CONFIG_COMPAT
112static long compat_sock_ioctl(struct file *file,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700113 unsigned int cmd, unsigned long arg);
Shaun Pereira89bbfc92006-03-21 23:58:08 -0800114#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115static int sock_fasync(int fd, struct file *filp, int on);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116static ssize_t sock_sendpage(struct file *file, struct page *page,
117 int offset, size_t size, loff_t *ppos, int more);
Jens Axboe9c55e012007-11-06 23:30:13 -0800118static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
119 struct pipe_inode_info *pipe, size_t len,
120 unsigned int flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122/*
123 * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
124 * in the operation structures but are done directly via the socketcall() multiplexor.
125 */
126
Arjan van de Venda7071d2007-02-12 00:55:36 -0800127static const struct file_operations socket_file_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 .owner = THIS_MODULE,
129 .llseek = no_llseek,
130 .aio_read = sock_aio_read,
131 .aio_write = sock_aio_write,
132 .poll = sock_poll,
133 .unlocked_ioctl = sock_ioctl,
Shaun Pereira89bbfc92006-03-21 23:58:08 -0800134#ifdef CONFIG_COMPAT
135 .compat_ioctl = compat_sock_ioctl,
136#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 .mmap = sock_mmap,
138 .open = sock_no_open, /* special open code to disallow open via /proc */
139 .release = sock_close,
140 .fasync = sock_fasync,
Jens Axboe5274f052006-03-30 15:15:30 +0200141 .sendpage = sock_sendpage,
142 .splice_write = generic_splice_sendpage,
Jens Axboe9c55e012007-11-06 23:30:13 -0800143 .splice_read = sock_splice_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144};
145
146/*
147 * The protocol list. Each protocol is registered in here.
148 */
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150static DEFINE_SPINLOCK(net_family_lock);
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -0700151static const struct net_proto_family *net_families[NPROTO] __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153/*
154 * Statistics counters of the socket lists
155 */
156
157static DEFINE_PER_CPU(int, sockets_in_use) = 0;
158
159/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700160 * Support routines.
161 * Move socket addresses back and forth across the kernel/user
162 * divide and look after the messy bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 */
164
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700165#define MAX_SOCK_ADDR 128 /* 108 for Unix domain -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 16 for IP, 16 for IPX,
167 24 for IPv6,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700168 about 80 for AX.25
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 must be at least one bigger than
170 the AF_UNIX size (see net/unix/af_unix.c
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700171 :unix_mkname()).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174/**
175 * move_addr_to_kernel - copy a socket address into kernel space
176 * @uaddr: Address in user space
177 * @kaddr: Address in kernel space
178 * @ulen: Length in user space
179 *
180 * The address is copied into kernel space. If the provided address is
181 * too long an error code of -EINVAL is returned. If the copy gives
182 * invalid addresses -EFAULT is returned. On a success 0 is returned.
183 */
184
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -0700185int move_addr_to_kernel(void __user *uaddr, int ulen, struct sockaddr *kaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -0700187 if (ulen < 0 || ulen > sizeof(struct sockaddr_storage))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700189 if (ulen == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 return 0;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700191 if (copy_from_user(kaddr, uaddr, ulen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 return -EFAULT;
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100193 return audit_sockaddr(ulen, kaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
196/**
197 * move_addr_to_user - copy an address to user space
198 * @kaddr: kernel space address
199 * @klen: length of address in kernel
200 * @uaddr: user space address
201 * @ulen: pointer to user length field
202 *
203 * The value pointed to by ulen on entry is the buffer length available.
204 * This is overwritten with the buffer space used. -EINVAL is returned
205 * if an overlong buffer is specified or a negative buffer size. -EFAULT
206 * is returned if either the buffer or the length field are not
207 * accessible.
208 * After copying the data up to the limit the user specifies, the true
209 * length of the data is written over the length limit the user
210 * specified. Zero is returned for a success.
211 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700212
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -0700213int move_addr_to_user(struct sockaddr *kaddr, int klen, void __user *uaddr,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700214 int __user *ulen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
216 int err;
217 int len;
218
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700219 err = get_user(len, ulen);
220 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 return err;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700222 if (len > klen)
223 len = klen;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -0700224 if (len < 0 || len > sizeof(struct sockaddr_storage))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700226 if (len) {
Steve Grubbd6fe3942006-03-30 12:20:22 -0500227 if (audit_sockaddr(klen, kaddr))
228 return -ENOMEM;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700229 if (copy_to_user(uaddr, kaddr, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 return -EFAULT;
231 }
232 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700233 * "fromlen shall refer to the value before truncation.."
234 * 1003.1g
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 */
236 return __put_user(klen, ulen);
237}
238
Christoph Lametere18b8902006-12-06 20:33:20 -0800239static struct kmem_cache *sock_inode_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241static struct inode *sock_alloc_inode(struct super_block *sb)
242{
243 struct socket_alloc *ei;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700244
Christoph Lametere94b1762006-12-06 20:33:17 -0800245 ei = kmem_cache_alloc(sock_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 if (!ei)
247 return NULL;
248 init_waitqueue_head(&ei->socket.wait);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 ei->socket.fasync_list = NULL;
251 ei->socket.state = SS_UNCONNECTED;
252 ei->socket.flags = 0;
253 ei->socket.ops = NULL;
254 ei->socket.sk = NULL;
255 ei->socket.file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 return &ei->vfs_inode;
258}
259
260static void sock_destroy_inode(struct inode *inode)
261{
262 kmem_cache_free(sock_inode_cachep,
263 container_of(inode, struct socket_alloc, vfs_inode));
264}
265
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700266static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700268 struct socket_alloc *ei = (struct socket_alloc *)foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Christoph Lametera35afb82007-05-16 22:10:57 -0700270 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273static int init_inodecache(void)
274{
275 sock_inode_cachep = kmem_cache_create("sock_inode_cache",
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700276 sizeof(struct socket_alloc),
277 0,
278 (SLAB_HWCACHE_ALIGN |
279 SLAB_RECLAIM_ACCOUNT |
280 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +0900281 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (sock_inode_cachep == NULL)
283 return -ENOMEM;
284 return 0;
285}
286
Alexey Dobriyanb87221d2009-09-21 17:01:09 -0700287static const struct super_operations sockfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 .alloc_inode = sock_alloc_inode,
289 .destroy_inode =sock_destroy_inode,
290 .statfs = simple_statfs,
291};
292
David Howells454e2392006-06-23 02:02:57 -0700293static int sockfs_get_sb(struct file_system_type *fs_type,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700294 int flags, const char *dev_name, void *data,
295 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
David Howells454e2392006-06-23 02:02:57 -0700297 return get_sb_pseudo(fs_type, "socket:", &sockfs_ops, SOCKFS_MAGIC,
298 mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300
Eric Dumazetba899662005-08-26 12:05:31 -0700301static struct vfsmount *sock_mnt __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303static struct file_system_type sock_fs_type = {
304 .name = "sockfs",
305 .get_sb = sockfs_get_sb,
306 .kill_sb = kill_anon_super,
307};
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309static int sockfs_delete_dentry(struct dentry *dentry)
310{
Eric Dumazet304e61e2006-12-06 20:38:49 -0800311 /*
312 * At creation time, we pretended this dentry was hashed
313 * (by clearing DCACHE_UNHASHED bit in d_flags)
314 * At delete time, we restore the truth : not hashed.
315 * (so that dput() can proceed correctly)
316 */
317 dentry->d_flags |= DCACHE_UNHASHED;
318 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319}
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700320
321/*
322 * sockfs_dname() is called from d_path().
323 */
324static char *sockfs_dname(struct dentry *dentry, char *buffer, int buflen)
325{
326 return dynamic_dname(dentry, buffer, buflen, "socket:[%lu]",
327 dentry->d_inode->i_ino);
328}
329
Al Viro3ba13d12009-02-20 06:02:22 +0000330static const struct dentry_operations sockfs_dentry_operations = {
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700331 .d_delete = sockfs_delete_dentry,
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700332 .d_dname = sockfs_dname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333};
334
335/*
336 * Obtains the first available file descriptor and sets it up for use.
337 *
David S. Miller39d8c1b2006-03-20 17:13:49 -0800338 * These functions create file structures and maps them to fd space
339 * of the current process. On success it returns file descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 * and file struct implicitly stored in sock->file.
341 * Note that another thread may close file descriptor before we return
342 * from this function. We use the fact that now we do not refer
343 * to socket after mapping. If one day we will need it, this
344 * function will increment ref. count on file by 1.
345 *
346 * In any case returned fd MAY BE not valid!
347 * This race condition is unavoidable
348 * with shared fd spaces, we cannot solve it inside kernel,
349 * but we take care of internal coherence yet.
350 */
351
Ulrich Dreppera677a032008-07-23 21:29:17 -0700352static int sock_alloc_fd(struct file **filep, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
354 int fd;
David S. Miller39d8c1b2006-03-20 17:13:49 -0800355
Ulrich Dreppera677a032008-07-23 21:29:17 -0700356 fd = get_unused_fd_flags(flags);
David S. Miller39d8c1b2006-03-20 17:13:49 -0800357 if (likely(fd >= 0)) {
358 struct file *file = get_empty_filp();
359
360 *filep = file;
361 if (unlikely(!file)) {
362 put_unused_fd(fd);
363 return -ENFILE;
364 }
365 } else
366 *filep = NULL;
367 return fd;
368}
369
Ulrich Drepper77d27202008-07-23 21:29:35 -0700370static int sock_attach_fd(struct socket *sock, struct file *file, int flags)
David S. Miller39d8c1b2006-03-20 17:13:49 -0800371{
Dave Hansence8d2cd2007-10-16 23:31:13 -0700372 struct dentry *dentry;
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700373 struct qstr name = { .name = "" };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Dave Hansence8d2cd2007-10-16 23:31:13 -0700375 dentry = d_alloc(sock_mnt->mnt_sb->s_root, &name);
376 if (unlikely(!dentry))
David S. Miller39d8c1b2006-03-20 17:13:49 -0800377 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Dave Hansence8d2cd2007-10-16 23:31:13 -0700379 dentry->d_op = &sockfs_dentry_operations;
Eric Dumazet304e61e2006-12-06 20:38:49 -0800380 /*
381 * We dont want to push this dentry into global dentry hash table.
382 * We pretend dentry is already hashed, by unsetting DCACHE_UNHASHED
383 * This permits a working /proc/$pid/fd/XXX on sockets
384 */
Dave Hansence8d2cd2007-10-16 23:31:13 -0700385 dentry->d_flags &= ~DCACHE_UNHASHED;
386 d_instantiate(dentry, SOCK_INODE(sock));
David S. Miller39d8c1b2006-03-20 17:13:49 -0800387
388 sock->file = file;
Dave Hansence8d2cd2007-10-16 23:31:13 -0700389 init_file(file, sock_mnt, dentry, FMODE_READ | FMODE_WRITE,
390 &socket_file_ops);
391 SOCK_INODE(sock)->i_fop = &socket_file_ops;
Ulrich Drepper77d27202008-07-23 21:29:35 -0700392 file->f_flags = O_RDWR | (flags & O_NONBLOCK);
David S. Miller39d8c1b2006-03-20 17:13:49 -0800393 file->f_pos = 0;
394 file->private_data = sock;
395
396 return 0;
397}
398
Ulrich Dreppera677a032008-07-23 21:29:17 -0700399int sock_map_fd(struct socket *sock, int flags)
David S. Miller39d8c1b2006-03-20 17:13:49 -0800400{
401 struct file *newfile;
Ulrich Dreppera677a032008-07-23 21:29:17 -0700402 int fd = sock_alloc_fd(&newfile, flags);
David S. Miller39d8c1b2006-03-20 17:13:49 -0800403
404 if (likely(fd >= 0)) {
Ulrich Drepper77d27202008-07-23 21:29:35 -0700405 int err = sock_attach_fd(sock, newfile, flags);
David S. Miller39d8c1b2006-03-20 17:13:49 -0800406
407 if (unlikely(err < 0)) {
408 put_filp(newfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 put_unused_fd(fd);
David S. Miller39d8c1b2006-03-20 17:13:49 -0800410 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
David S. Miller39d8c1b2006-03-20 17:13:49 -0800412 fd_install(fd, newfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 return fd;
415}
416
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800417static struct socket *sock_from_file(struct file *file, int *err)
418{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800419 if (file->f_op == &socket_file_ops)
420 return file->private_data; /* set in sock_map_fd */
421
Eric Dumazet23bb80d2007-02-08 14:59:57 -0800422 *err = -ENOTSOCK;
423 return NULL;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800424}
425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426/**
427 * sockfd_lookup - Go from a file number to its socket slot
428 * @fd: file handle
429 * @err: pointer to an error code return
430 *
431 * The file handle passed in is locked and the socket it is bound
432 * too is returned. If an error occurs the err pointer is overwritten
433 * with a negative errno code and NULL is returned. The function checks
434 * for both invalid handles and passing a handle which is not a socket.
435 *
436 * On a success the socket object pointer is returned.
437 */
438
439struct socket *sockfd_lookup(int fd, int *err)
440{
441 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 struct socket *sock;
443
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700444 file = fget(fd);
445 if (!file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 *err = -EBADF;
447 return NULL;
448 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700449
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800450 sock = sock_from_file(file, err);
451 if (!sock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 fput(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 return sock;
454}
455
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800456static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed)
457{
458 struct file *file;
459 struct socket *sock;
460
Hua Zhong36725582006-04-19 15:25:02 -0700461 *err = -EBADF;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800462 file = fget_light(fd, fput_needed);
463 if (file) {
464 sock = sock_from_file(file, err);
465 if (sock)
466 return sock;
467 fput_light(file, *fput_needed);
468 }
469 return NULL;
470}
471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472/**
473 * sock_alloc - allocate a socket
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700474 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 * Allocate a new inode and socket object. The two are bound together
476 * and initialised. The socket is then returned. If we are out of inodes
477 * NULL is returned.
478 */
479
480static struct socket *sock_alloc(void)
481{
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700482 struct inode *inode;
483 struct socket *sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 inode = new_inode(sock_mnt->mnt_sb);
486 if (!inode)
487 return NULL;
488
489 sock = SOCKET_I(inode);
490
Eric Dumazet29a020d2009-09-15 02:39:20 -0700491 kmemcheck_annotate_bitfield(sock, type);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700492 inode->i_mode = S_IFSOCK | S_IRWXUGO;
David Howells8192b0c2008-11-14 10:39:10 +1100493 inode->i_uid = current_fsuid();
494 inode->i_gid = current_fsgid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Eric Dumazet4e694892009-04-04 16:41:09 -0700496 percpu_add(sockets_in_use, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 return sock;
498}
499
500/*
501 * In theory you can't get an open on this inode, but /proc provides
502 * a back door. Remember to keep it shut otherwise you'll let the
503 * creepy crawlies in.
504 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506static int sock_no_open(struct inode *irrelevant, struct file *dontcare)
507{
508 return -ENXIO;
509}
510
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800511const struct file_operations bad_sock_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 .owner = THIS_MODULE,
513 .open = sock_no_open,
514};
515
516/**
517 * sock_release - close a socket
518 * @sock: socket to close
519 *
520 * The socket is released from the protocol stack if it has a release
521 * callback, and the inode is then released if the socket is bound to
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700522 * an inode not a file.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525void sock_release(struct socket *sock)
526{
527 if (sock->ops) {
528 struct module *owner = sock->ops->owner;
529
530 sock->ops->release(sock);
531 sock->ops = NULL;
532 module_put(owner);
533 }
534
535 if (sock->fasync_list)
536 printk(KERN_ERR "sock_release: fasync list not empty!\n");
537
Eric Dumazet4e694892009-04-04 16:41:09 -0700538 percpu_sub(sockets_in_use, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if (!sock->file) {
540 iput(SOCK_INODE(sock));
541 return;
542 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700543 sock->file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544}
545
Patrick Ohly20d49472009-02-12 05:03:38 +0000546int sock_tx_timestamp(struct msghdr *msg, struct sock *sk,
547 union skb_shared_tx *shtx)
548{
549 shtx->flags = 0;
550 if (sock_flag(sk, SOCK_TIMESTAMPING_TX_HARDWARE))
551 shtx->hardware = 1;
552 if (sock_flag(sk, SOCK_TIMESTAMPING_TX_SOFTWARE))
553 shtx->software = 1;
554 return 0;
555}
556EXPORT_SYMBOL(sock_tx_timestamp);
557
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700558static inline int __sock_sendmsg(struct kiocb *iocb, struct socket *sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 struct msghdr *msg, size_t size)
560{
561 struct sock_iocb *si = kiocb_to_siocb(iocb);
562 int err;
563
564 si->sock = sock;
565 si->scm = NULL;
566 si->msg = msg;
567 si->size = size;
568
569 err = security_socket_sendmsg(sock, msg, size);
570 if (err)
571 return err;
572
573 return sock->ops->sendmsg(iocb, sock, msg, size);
574}
575
576int sock_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
577{
578 struct kiocb iocb;
579 struct sock_iocb siocb;
580 int ret;
581
582 init_sync_kiocb(&iocb, NULL);
583 iocb.private = &siocb;
584 ret = __sock_sendmsg(&iocb, sock, msg, size);
585 if (-EIOCBQUEUED == ret)
586 ret = wait_on_sync_kiocb(&iocb);
587 return ret;
588}
589
590int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
591 struct kvec *vec, size_t num, size_t size)
592{
593 mm_segment_t oldfs = get_fs();
594 int result;
595
596 set_fs(KERNEL_DS);
597 /*
598 * the following is safe, since for compiler definitions of kvec and
599 * iovec are identical, yielding the same in-core layout and alignment
600 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700601 msg->msg_iov = (struct iovec *)vec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 msg->msg_iovlen = num;
603 result = sock_sendmsg(sock, msg, size);
604 set_fs(oldfs);
605 return result;
606}
607
Patrick Ohly20d49472009-02-12 05:03:38 +0000608static int ktime2ts(ktime_t kt, struct timespec *ts)
609{
610 if (kt.tv64) {
611 *ts = ktime_to_timespec(kt);
612 return 1;
613 } else {
614 return 0;
615 }
616}
617
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700618/*
619 * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
620 */
621void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
622 struct sk_buff *skb)
623{
Patrick Ohly20d49472009-02-12 05:03:38 +0000624 int need_software_tstamp = sock_flag(sk, SOCK_RCVTSTAMP);
625 struct timespec ts[3];
626 int empty = 1;
627 struct skb_shared_hwtstamps *shhwtstamps =
628 skb_hwtstamps(skb);
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700629
Patrick Ohly20d49472009-02-12 05:03:38 +0000630 /* Race occurred between timestamp enabling and packet
631 receiving. Fill in the current time for now. */
632 if (need_software_tstamp && skb->tstamp.tv64 == 0)
633 __net_timestamp(skb);
634
635 if (need_software_tstamp) {
636 if (!sock_flag(sk, SOCK_RCVTSTAMPNS)) {
637 struct timeval tv;
638 skb_get_timestamp(skb, &tv);
639 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMP,
640 sizeof(tv), &tv);
641 } else {
642 struct timespec ts;
643 skb_get_timestampns(skb, &ts);
644 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPNS,
645 sizeof(ts), &ts);
646 }
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700647 }
Patrick Ohly20d49472009-02-12 05:03:38 +0000648
649
650 memset(ts, 0, sizeof(ts));
651 if (skb->tstamp.tv64 &&
652 sock_flag(sk, SOCK_TIMESTAMPING_SOFTWARE)) {
653 skb_get_timestampns(skb, ts + 0);
654 empty = 0;
655 }
656 if (shhwtstamps) {
657 if (sock_flag(sk, SOCK_TIMESTAMPING_SYS_HARDWARE) &&
658 ktime2ts(shhwtstamps->syststamp, ts + 1))
659 empty = 0;
660 if (sock_flag(sk, SOCK_TIMESTAMPING_RAW_HARDWARE) &&
661 ktime2ts(shhwtstamps->hwtstamp, ts + 2))
662 empty = 0;
663 }
664 if (!empty)
665 put_cmsg(msg, SOL_SOCKET,
666 SCM_TIMESTAMPING, sizeof(ts), &ts);
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700667}
668
Arnaldo Carvalho de Melo7c81fd82007-03-10 00:39:35 -0300669EXPORT_SYMBOL_GPL(__sock_recv_timestamp);
670
Neil Horman3b885782009-10-12 13:26:31 -0700671inline void sock_recv_drops(struct msghdr *msg, struct sock *sk, struct sk_buff *skb)
672{
673 if (sock_flag(sk, SOCK_RXQ_OVFL) && skb && skb->dropcount)
674 put_cmsg(msg, SOL_SOCKET, SO_RXQ_OVFL,
675 sizeof(__u32), &skb->dropcount);
676}
677
678void sock_recv_ts_and_drops(struct msghdr *msg, struct sock *sk,
679 struct sk_buff *skb)
680{
681 sock_recv_timestamp(msg, sk, skb);
682 sock_recv_drops(msg, sk, skb);
683}
684EXPORT_SYMBOL_GPL(sock_recv_ts_and_drops);
685
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -0700686static inline int __sock_recvmsg_nosec(struct kiocb *iocb, struct socket *sock,
687 struct msghdr *msg, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 struct sock_iocb *si = kiocb_to_siocb(iocb);
690
691 si->sock = sock;
692 si->scm = NULL;
693 si->msg = msg;
694 si->size = size;
695 si->flags = flags;
696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 return sock->ops->recvmsg(iocb, sock, msg, size, flags);
698}
699
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -0700700static inline int __sock_recvmsg(struct kiocb *iocb, struct socket *sock,
701 struct msghdr *msg, size_t size, int flags)
702{
703 int err = security_socket_recvmsg(sock, msg, size, flags);
704
705 return err ?: __sock_recvmsg_nosec(iocb, sock, msg, size, flags);
706}
707
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700708int sock_recvmsg(struct socket *sock, struct msghdr *msg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 size_t size, int flags)
710{
711 struct kiocb iocb;
712 struct sock_iocb siocb;
713 int ret;
714
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700715 init_sync_kiocb(&iocb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 iocb.private = &siocb;
717 ret = __sock_recvmsg(&iocb, sock, msg, size, flags);
718 if (-EIOCBQUEUED == ret)
719 ret = wait_on_sync_kiocb(&iocb);
720 return ret;
721}
722
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -0700723static int sock_recvmsg_nosec(struct socket *sock, struct msghdr *msg,
724 size_t size, int flags)
725{
726 struct kiocb iocb;
727 struct sock_iocb siocb;
728 int ret;
729
730 init_sync_kiocb(&iocb, NULL);
731 iocb.private = &siocb;
732 ret = __sock_recvmsg_nosec(&iocb, sock, msg, size, flags);
733 if (-EIOCBQUEUED == ret)
734 ret = wait_on_sync_kiocb(&iocb);
735 return ret;
736}
737
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700738int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
739 struct kvec *vec, size_t num, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
741 mm_segment_t oldfs = get_fs();
742 int result;
743
744 set_fs(KERNEL_DS);
745 /*
746 * the following is safe, since for compiler definitions of kvec and
747 * iovec are identical, yielding the same in-core layout and alignment
748 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700749 msg->msg_iov = (struct iovec *)vec, msg->msg_iovlen = num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 result = sock_recvmsg(sock, msg, size, flags);
751 set_fs(oldfs);
752 return result;
753}
754
755static void sock_aio_dtor(struct kiocb *iocb)
756{
757 kfree(iocb->private);
758}
759
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -0300760static ssize_t sock_sendpage(struct file *file, struct page *page,
761 int offset, size_t size, loff_t *ppos, int more)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
763 struct socket *sock;
764 int flags;
765
Eric Dumazetb69aee02005-09-06 14:42:45 -0700766 sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
768 flags = !(file->f_flags & O_NONBLOCK) ? 0 : MSG_DONTWAIT;
769 if (more)
770 flags |= MSG_MORE;
771
Linus Torvaldse6949582009-08-13 08:28:36 -0700772 return kernel_sendpage(sock, page, offset, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773}
774
Jens Axboe9c55e012007-11-06 23:30:13 -0800775static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
776 struct pipe_inode_info *pipe, size_t len,
777 unsigned int flags)
778{
779 struct socket *sock = file->private_data;
780
Rémi Denis-Courmont997b37d2008-02-15 02:35:45 -0800781 if (unlikely(!sock->ops->splice_read))
782 return -EINVAL;
783
Jens Axboe9c55e012007-11-06 23:30:13 -0800784 return sock->ops->splice_read(sock, ppos, pipe, len, flags);
785}
786
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800787static struct sock_iocb *alloc_sock_iocb(struct kiocb *iocb,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700788 struct sock_iocb *siocb)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800789{
790 if (!is_sync_kiocb(iocb)) {
791 siocb = kmalloc(sizeof(*siocb), GFP_KERNEL);
792 if (!siocb)
793 return NULL;
794 iocb->ki_dtor = sock_aio_dtor;
795 }
796
797 siocb->kiocb = iocb;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800798 iocb->private = siocb;
799 return siocb;
800}
801
802static ssize_t do_sock_read(struct msghdr *msg, struct kiocb *iocb,
Badari Pulavarty027445c2006-09-30 23:28:46 -0700803 struct file *file, const struct iovec *iov,
804 unsigned long nr_segs)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800805{
806 struct socket *sock = file->private_data;
807 size_t size = 0;
808 int i;
809
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700810 for (i = 0; i < nr_segs; i++)
811 size += iov[i].iov_len;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800812
813 msg->msg_name = NULL;
814 msg->msg_namelen = 0;
815 msg->msg_control = NULL;
816 msg->msg_controllen = 0;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700817 msg->msg_iov = (struct iovec *)iov;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800818 msg->msg_iovlen = nr_segs;
819 msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
820
821 return __sock_recvmsg(iocb, sock, msg, size, msg->msg_flags);
822}
823
Badari Pulavarty027445c2006-09-30 23:28:46 -0700824static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov,
825 unsigned long nr_segs, loff_t pos)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800826{
827 struct sock_iocb siocb, *x;
828
829 if (pos != 0)
830 return -ESPIPE;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700831
832 if (iocb->ki_left == 0) /* Match SYS5 behaviour */
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800833 return 0;
834
Badari Pulavarty027445c2006-09-30 23:28:46 -0700835
836 x = alloc_sock_iocb(iocb, &siocb);
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800837 if (!x)
838 return -ENOMEM;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700839 return do_sock_read(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800840}
841
842static ssize_t do_sock_write(struct msghdr *msg, struct kiocb *iocb,
Badari Pulavarty027445c2006-09-30 23:28:46 -0700843 struct file *file, const struct iovec *iov,
844 unsigned long nr_segs)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800845{
846 struct socket *sock = file->private_data;
847 size_t size = 0;
848 int i;
849
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700850 for (i = 0; i < nr_segs; i++)
851 size += iov[i].iov_len;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800852
853 msg->msg_name = NULL;
854 msg->msg_namelen = 0;
855 msg->msg_control = NULL;
856 msg->msg_controllen = 0;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700857 msg->msg_iov = (struct iovec *)iov;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800858 msg->msg_iovlen = nr_segs;
859 msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
860 if (sock->type == SOCK_SEQPACKET)
861 msg->msg_flags |= MSG_EOR;
862
863 return __sock_sendmsg(iocb, sock, msg, size);
864}
865
Badari Pulavarty027445c2006-09-30 23:28:46 -0700866static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov,
867 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800869 struct sock_iocb siocb, *x;
870
871 if (pos != 0)
872 return -ESPIPE;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700873
Badari Pulavarty027445c2006-09-30 23:28:46 -0700874 x = alloc_sock_iocb(iocb, &siocb);
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800875 if (!x)
876 return -ENOMEM;
877
Badari Pulavarty027445c2006-09-30 23:28:46 -0700878 return do_sock_write(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879}
880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881/*
882 * Atomic setting of ioctl hooks to avoid race
883 * with module unload.
884 */
885
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800886static DEFINE_MUTEX(br_ioctl_mutex);
Eric W. Biederman881d9662007-09-17 11:56:21 -0700887static int (*br_ioctl_hook) (struct net *, unsigned int cmd, void __user *arg) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Eric W. Biederman881d9662007-09-17 11:56:21 -0700889void brioctl_set(int (*hook) (struct net *, unsigned int, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800891 mutex_lock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 br_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800893 mutex_unlock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894}
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896EXPORT_SYMBOL(brioctl_set);
897
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800898static DEFINE_MUTEX(vlan_ioctl_mutex);
Eric W. Biederman881d9662007-09-17 11:56:21 -0700899static int (*vlan_ioctl_hook) (struct net *, void __user *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Eric W. Biederman881d9662007-09-17 11:56:21 -0700901void vlan_ioctl_set(int (*hook) (struct net *, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800903 mutex_lock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 vlan_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800905 mutex_unlock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906}
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700907
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908EXPORT_SYMBOL(vlan_ioctl_set);
909
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800910static DEFINE_MUTEX(dlci_ioctl_mutex);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700911static int (*dlci_ioctl_hook) (unsigned int, void __user *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700913void dlci_ioctl_set(int (*hook) (unsigned int, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800915 mutex_lock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 dlci_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800917 mutex_unlock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918}
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920EXPORT_SYMBOL(dlci_ioctl_set);
921
922/*
923 * With an ioctl, arg may well be a user mode pointer, but we don't know
924 * what to do with it - that's up to the protocol still.
925 */
926
927static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
928{
929 struct socket *sock;
Eric W. Biederman881d9662007-09-17 11:56:21 -0700930 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 void __user *argp = (void __user *)arg;
932 int pid, err;
Eric W. Biederman881d9662007-09-17 11:56:21 -0700933 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Eric Dumazetb69aee02005-09-06 14:42:45 -0700935 sock = file->private_data;
Eric W. Biederman881d9662007-09-17 11:56:21 -0700936 sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900937 net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
Eric W. Biederman881d9662007-09-17 11:56:21 -0700939 err = dev_ioctl(net, cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 } else
Johannes Berg3d23e342009-09-29 23:27:28 +0200941#ifdef CONFIG_WEXT_CORE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
Eric W. Biederman881d9662007-09-17 11:56:21 -0700943 err = dev_ioctl(net, cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 } else
Johannes Berg3d23e342009-09-29 23:27:28 +0200945#endif
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700946 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 case FIOSETOWN:
948 case SIOCSPGRP:
949 err = -EFAULT;
950 if (get_user(pid, (int __user *)argp))
951 break;
952 err = f_setown(sock->file, pid, 1);
953 break;
954 case FIOGETOWN:
955 case SIOCGPGRP:
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700956 err = put_user(f_getown(sock->file),
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700957 (int __user *)argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 break;
959 case SIOCGIFBR:
960 case SIOCSIFBR:
961 case SIOCBRADDBR:
962 case SIOCBRDELBR:
963 err = -ENOPKG;
964 if (!br_ioctl_hook)
965 request_module("bridge");
966
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800967 mutex_lock(&br_ioctl_mutex);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700968 if (br_ioctl_hook)
Eric W. Biederman881d9662007-09-17 11:56:21 -0700969 err = br_ioctl_hook(net, cmd, argp);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800970 mutex_unlock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 break;
972 case SIOCGIFVLAN:
973 case SIOCSIFVLAN:
974 err = -ENOPKG;
975 if (!vlan_ioctl_hook)
976 request_module("8021q");
977
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800978 mutex_lock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 if (vlan_ioctl_hook)
Eric W. Biederman881d9662007-09-17 11:56:21 -0700980 err = vlan_ioctl_hook(net, argp);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800981 mutex_unlock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 case SIOCADDDLCI:
984 case SIOCDELDLCI:
985 err = -ENOPKG;
986 if (!dlci_ioctl_hook)
987 request_module("dlci");
988
Pavel Emelyanov7512cbf2008-03-21 15:58:52 -0700989 mutex_lock(&dlci_ioctl_mutex);
990 if (dlci_ioctl_hook)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 err = dlci_ioctl_hook(cmd, argp);
Pavel Emelyanov7512cbf2008-03-21 15:58:52 -0700992 mutex_unlock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 break;
994 default:
995 err = sock->ops->ioctl(sock, cmd, arg);
Christoph Hellwigb5e5fa52006-01-03 14:18:33 -0800996
997 /*
998 * If this ioctl is unknown try to hand it down
999 * to the NIC driver.
1000 */
1001 if (err == -ENOIOCTLCMD)
Eric W. Biederman881d9662007-09-17 11:56:21 -07001002 err = dev_ioctl(net, cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 break;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 return err;
1006}
1007
1008int sock_create_lite(int family, int type, int protocol, struct socket **res)
1009{
1010 int err;
1011 struct socket *sock = NULL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001012
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 err = security_socket_create(family, type, protocol, 1);
1014 if (err)
1015 goto out;
1016
1017 sock = sock_alloc();
1018 if (!sock) {
1019 err = -ENOMEM;
1020 goto out;
1021 }
1022
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 sock->type = type;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001024 err = security_socket_post_create(sock, family, type, protocol, 1);
1025 if (err)
1026 goto out_release;
1027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028out:
1029 *res = sock;
1030 return err;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001031out_release:
1032 sock_release(sock);
1033 sock = NULL;
1034 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035}
1036
1037/* No kernel lock held - perfect */
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001038static unsigned int sock_poll(struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
1040 struct socket *sock;
1041
1042 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001043 * We can't return errors to poll, so it's either yes or no.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 */
Eric Dumazetb69aee02005-09-06 14:42:45 -07001045 sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 return sock->ops->poll(file, sock, wait);
1047}
1048
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001049static int sock_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050{
Eric Dumazetb69aee02005-09-06 14:42:45 -07001051 struct socket *sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
1053 return sock->ops->mmap(file, sock, vma);
1054}
1055
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001056static int sock_close(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057{
1058 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001059 * It was possible the inode is NULL we were
1060 * closing an unfinished socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 */
1062
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001063 if (!inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 printk(KERN_DEBUG "sock_close: NULL inode\n");
1065 return 0;
1066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 sock_release(SOCKET_I(inode));
1068 return 0;
1069}
1070
1071/*
1072 * Update the socket async list
1073 *
1074 * Fasync_list locking strategy.
1075 *
1076 * 1. fasync_list is modified only under process context socket lock
1077 * i.e. under semaphore.
1078 * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
1079 * or under socket lock.
1080 * 3. fasync_list can be used from softirq context, so that
1081 * modification under socket lock have to be enhanced with
1082 * write_lock_bh(&sk->sk_callback_lock).
1083 * --ANK (990710)
1084 */
1085
1086static int sock_fasync(int fd, struct file *filp, int on)
1087{
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001088 struct fasync_struct *fa, *fna = NULL, **prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 struct socket *sock;
1090 struct sock *sk;
1091
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001092 if (on) {
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08001093 fna = kmalloc(sizeof(struct fasync_struct), GFP_KERNEL);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001094 if (fna == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 return -ENOMEM;
1096 }
1097
Eric Dumazetb69aee02005-09-06 14:42:45 -07001098 sock = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001100 sk = sock->sk;
1101 if (sk == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 kfree(fna);
1103 return -EINVAL;
1104 }
1105
1106 lock_sock(sk);
1107
Jonathan Corbet76398422009-02-01 14:26:59 -07001108 spin_lock(&filp->f_lock);
1109 if (on)
1110 filp->f_flags |= FASYNC;
1111 else
1112 filp->f_flags &= ~FASYNC;
1113 spin_unlock(&filp->f_lock);
1114
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001115 prev = &(sock->fasync_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001117 for (fa = *prev; fa != NULL; prev = &fa->fa_next, fa = *prev)
1118 if (fa->fa_file == filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 break;
1120
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001121 if (on) {
1122 if (fa != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 write_lock_bh(&sk->sk_callback_lock);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001124 fa->fa_fd = fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 write_unlock_bh(&sk->sk_callback_lock);
1126
1127 kfree(fna);
1128 goto out;
1129 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001130 fna->fa_file = filp;
1131 fna->fa_fd = fd;
1132 fna->magic = FASYNC_MAGIC;
1133 fna->fa_next = sock->fasync_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 write_lock_bh(&sk->sk_callback_lock);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001135 sock->fasync_list = fna;
Eric Dumazetbcdce712009-10-06 17:28:29 -07001136 sock_set_flag(sk, SOCK_FASYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 write_unlock_bh(&sk->sk_callback_lock);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001138 } else {
1139 if (fa != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 write_lock_bh(&sk->sk_callback_lock);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001141 *prev = fa->fa_next;
Eric Dumazetbcdce712009-10-06 17:28:29 -07001142 if (!sock->fasync_list)
1143 sock_reset_flag(sk, SOCK_FASYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 write_unlock_bh(&sk->sk_callback_lock);
1145 kfree(fa);
1146 }
1147 }
1148
1149out:
1150 release_sock(sock->sk);
1151 return 0;
1152}
1153
1154/* This function may be called only under socket lock or callback_lock */
1155
1156int sock_wake_async(struct socket *sock, int how, int band)
1157{
1158 if (!sock || !sock->fasync_list)
1159 return -1;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001160 switch (how) {
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001161 case SOCK_WAKE_WAITD:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 if (test_bit(SOCK_ASYNC_WAITDATA, &sock->flags))
1163 break;
1164 goto call_kill;
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001165 case SOCK_WAKE_SPACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags))
1167 break;
1168 /* fall through */
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001169 case SOCK_WAKE_IO:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001170call_kill:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 __kill_fasync(sock->fasync_list, SIGIO, band);
1172 break;
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001173 case SOCK_WAKE_URG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 __kill_fasync(sock->fasync_list, SIGURG, band);
1175 }
1176 return 0;
1177}
1178
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001179static int __sock_create(struct net *net, int family, int type, int protocol,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001180 struct socket **res, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181{
1182 int err;
1183 struct socket *sock;
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001184 const struct net_proto_family *pf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
1186 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001187 * Check protocol is in range
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 */
1189 if (family < 0 || family >= NPROTO)
1190 return -EAFNOSUPPORT;
1191 if (type < 0 || type >= SOCK_MAX)
1192 return -EINVAL;
1193
1194 /* Compatibility.
1195
1196 This uglymoron is moved from INET layer to here to avoid
1197 deadlock in module load.
1198 */
1199 if (family == PF_INET && type == SOCK_PACKET) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001200 static int warned;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 if (!warned) {
1202 warned = 1;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001203 printk(KERN_INFO "%s uses obsolete (PF_INET,SOCK_PACKET)\n",
1204 current->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 }
1206 family = PF_PACKET;
1207 }
1208
1209 err = security_socket_create(family, type, protocol, kern);
1210 if (err)
1211 return err;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001212
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001213 /*
1214 * Allocate the socket and allow the family to set things up. if
1215 * the protocol is 0, the family is instructed to select an appropriate
1216 * default.
1217 */
1218 sock = sock_alloc();
1219 if (!sock) {
1220 if (net_ratelimit())
1221 printk(KERN_WARNING "socket: no more sockets\n");
1222 return -ENFILE; /* Not exactly a match, but its the
1223 closest posix thing */
1224 }
1225
1226 sock->type = type;
1227
Johannes Berg95a5afc2008-10-16 15:24:51 -07001228#ifdef CONFIG_MODULES
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001229 /* Attempt to load a protocol module if the find failed.
1230 *
1231 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 * requested real, full-featured networking support upon configuration.
1233 * Otherwise module support will break!
1234 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001235 if (net_families[family] == NULL)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001236 request_module("net-pf-%d", family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237#endif
1238
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001239 rcu_read_lock();
1240 pf = rcu_dereference(net_families[family]);
1241 err = -EAFNOSUPPORT;
1242 if (!pf)
1243 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
1245 /*
1246 * We will call the ->create function, that possibly is in a loadable
1247 * module, so we have to bump that loadable module refcnt first.
1248 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001249 if (!try_module_get(pf->owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 goto out_release;
1251
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001252 /* Now protected by module ref count */
1253 rcu_read_unlock();
1254
Eric Paris3f378b62009-11-05 22:18:14 -08001255 err = pf->create(net, sock, protocol, kern);
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001256 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 goto out_module_put;
Frank Filza79af592005-09-27 15:23:38 -07001258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 /*
1260 * Now to bump the refcnt of the [loadable] module that owns this
1261 * socket at sock_release time we decrement its refcnt.
1262 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001263 if (!try_module_get(sock->ops->owner))
1264 goto out_module_busy;
1265
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 /*
1267 * Now that we're done with the ->create function, the [loadable]
1268 * module can have its refcnt decremented
1269 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001270 module_put(pf->owner);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001271 err = security_socket_post_create(sock, family, type, protocol, kern);
1272 if (err)
Herbert Xu3b185522007-08-15 14:46:02 -07001273 goto out_sock_release;
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001274 *res = sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001276 return 0;
1277
1278out_module_busy:
1279 err = -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280out_module_put:
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001281 sock->ops = NULL;
1282 module_put(pf->owner);
1283out_sock_release:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 sock_release(sock);
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001285 return err;
1286
1287out_release:
1288 rcu_read_unlock();
1289 goto out_sock_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290}
1291
1292int sock_create(int family, int type, int protocol, struct socket **res)
1293{
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001294 return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295}
1296
1297int sock_create_kern(int family, int type, int protocol, struct socket **res)
1298{
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001299 return __sock_create(&init_net, family, type, protocol, res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300}
1301
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001302SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303{
1304 int retval;
1305 struct socket *sock;
Ulrich Dreppera677a032008-07-23 21:29:17 -07001306 int flags;
1307
Ulrich Dreppere38b36f2008-07-23 21:29:42 -07001308 /* Check the SOCK_* constants for consistency. */
1309 BUILD_BUG_ON(SOCK_CLOEXEC != O_CLOEXEC);
1310 BUILD_BUG_ON((SOCK_MAX | SOCK_TYPE_MASK) != SOCK_TYPE_MASK);
1311 BUILD_BUG_ON(SOCK_CLOEXEC & SOCK_TYPE_MASK);
1312 BUILD_BUG_ON(SOCK_NONBLOCK & SOCK_TYPE_MASK);
1313
Ulrich Dreppera677a032008-07-23 21:29:17 -07001314 flags = type & ~SOCK_TYPE_MASK;
Ulrich Drepper77d27202008-07-23 21:29:35 -07001315 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Dreppera677a032008-07-23 21:29:17 -07001316 return -EINVAL;
1317 type &= SOCK_TYPE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001319 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1320 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1321
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 retval = sock_create(family, type, protocol, &sock);
1323 if (retval < 0)
1324 goto out;
1325
Ulrich Drepper77d27202008-07-23 21:29:35 -07001326 retval = sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 if (retval < 0)
1328 goto out_release;
1329
1330out:
1331 /* It may be already another descriptor 8) Not kernel problem. */
1332 return retval;
1333
1334out_release:
1335 sock_release(sock);
1336 return retval;
1337}
1338
1339/*
1340 * Create a pair of connected sockets.
1341 */
1342
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001343SYSCALL_DEFINE4(socketpair, int, family, int, type, int, protocol,
1344 int __user *, usockvec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345{
1346 struct socket *sock1, *sock2;
1347 int fd1, fd2, err;
Al Virodb349502007-02-07 01:48:00 -05001348 struct file *newfile1, *newfile2;
Ulrich Dreppera677a032008-07-23 21:29:17 -07001349 int flags;
1350
1351 flags = type & ~SOCK_TYPE_MASK;
Ulrich Drepper77d27202008-07-23 21:29:35 -07001352 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Dreppera677a032008-07-23 21:29:17 -07001353 return -EINVAL;
1354 type &= SOCK_TYPE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001356 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1357 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1358
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 /*
1360 * Obtain the first socket and check if the underlying protocol
1361 * supports the socketpair call.
1362 */
1363
1364 err = sock_create(family, type, protocol, &sock1);
1365 if (err < 0)
1366 goto out;
1367
1368 err = sock_create(family, type, protocol, &sock2);
1369 if (err < 0)
1370 goto out_release_1;
1371
1372 err = sock1->ops->socketpair(sock1, sock2);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001373 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 goto out_release_both;
1375
Ulrich Dreppera677a032008-07-23 21:29:17 -07001376 fd1 = sock_alloc_fd(&newfile1, flags & O_CLOEXEC);
David S. Millerbf3c23d2007-10-29 21:54:02 -07001377 if (unlikely(fd1 < 0)) {
1378 err = fd1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 goto out_release_both;
David S. Millerbf3c23d2007-10-29 21:54:02 -07001380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Ulrich Dreppera677a032008-07-23 21:29:17 -07001382 fd2 = sock_alloc_fd(&newfile2, flags & O_CLOEXEC);
Al Virodb349502007-02-07 01:48:00 -05001383 if (unlikely(fd2 < 0)) {
David S. Millerbf3c23d2007-10-29 21:54:02 -07001384 err = fd2;
Al Virodb349502007-02-07 01:48:00 -05001385 put_filp(newfile1);
1386 put_unused_fd(fd1);
1387 goto out_release_both;
1388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
Ulrich Drepper77d27202008-07-23 21:29:35 -07001390 err = sock_attach_fd(sock1, newfile1, flags & O_NONBLOCK);
Al Virodb349502007-02-07 01:48:00 -05001391 if (unlikely(err < 0)) {
1392 goto out_fd2;
1393 }
1394
Ulrich Drepper77d27202008-07-23 21:29:35 -07001395 err = sock_attach_fd(sock2, newfile2, flags & O_NONBLOCK);
Al Virodb349502007-02-07 01:48:00 -05001396 if (unlikely(err < 0)) {
1397 fput(newfile1);
1398 goto out_fd1;
1399 }
1400
Al Viro157cf642008-12-14 04:57:47 -05001401 audit_fd_pair(fd1, fd2);
Al Virodb349502007-02-07 01:48:00 -05001402 fd_install(fd1, newfile1);
1403 fd_install(fd2, newfile2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 /* fd1 and fd2 may be already another descriptors.
1405 * Not kernel problem.
1406 */
1407
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001408 err = put_user(fd1, &usockvec[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 if (!err)
1410 err = put_user(fd2, &usockvec[1]);
1411 if (!err)
1412 return 0;
1413
1414 sys_close(fd2);
1415 sys_close(fd1);
1416 return err;
1417
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418out_release_both:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001419 sock_release(sock2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420out_release_1:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001421 sock_release(sock1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422out:
1423 return err;
Al Virodb349502007-02-07 01:48:00 -05001424
1425out_fd2:
1426 put_filp(newfile1);
1427 sock_release(sock1);
1428out_fd1:
1429 put_filp(newfile2);
1430 sock_release(sock2);
Al Virodb349502007-02-07 01:48:00 -05001431 put_unused_fd(fd1);
1432 put_unused_fd(fd2);
1433 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434}
1435
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436/*
1437 * Bind a name to a socket. Nothing much to do here since it's
1438 * the protocol's responsibility to handle the local address.
1439 *
1440 * We move the socket address to kernel space before we call
1441 * the protocol layer (having also checked the address is ok).
1442 */
1443
Heiko Carstens20f37032009-01-14 14:14:23 +01001444SYSCALL_DEFINE3(bind, int, fd, struct sockaddr __user *, umyaddr, int, addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445{
1446 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001447 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001448 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001450 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001451 if (sock) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001452 err = move_addr_to_kernel(umyaddr, addrlen, (struct sockaddr *)&address);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001453 if (err >= 0) {
1454 err = security_socket_bind(sock,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001455 (struct sockaddr *)&address,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001456 addrlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001457 if (!err)
1458 err = sock->ops->bind(sock,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001459 (struct sockaddr *)
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001460 &address, addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 }
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001462 fput_light(sock->file, fput_needed);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 return err;
1465}
1466
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467/*
1468 * Perform a listen. Basically, we allow the protocol to do anything
1469 * necessary for a listen, and if that works, we mark the socket as
1470 * ready for listening.
1471 */
1472
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001473SYSCALL_DEFINE2(listen, int, fd, int, backlog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474{
1475 struct socket *sock;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001476 int err, fput_needed;
Pavel Emelyanovb8e1f9b2007-12-08 00:12:33 -08001477 int somaxconn;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001478
1479 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1480 if (sock) {
Pavel Emelyanov8efa6e92008-03-31 19:41:14 -07001481 somaxconn = sock_net(sock->sk)->core.sysctl_somaxconn;
Pavel Emelyanovb8e1f9b2007-12-08 00:12:33 -08001482 if ((unsigned)backlog > somaxconn)
1483 backlog = somaxconn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
1485 err = security_socket_listen(sock, backlog);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001486 if (!err)
1487 err = sock->ops->listen(sock, backlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001489 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 }
1491 return err;
1492}
1493
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494/*
1495 * For accept, we attempt to create a new socket, set up the link
1496 * with the client, wake up the client, then return the new
1497 * connected fd. We collect the address of the connector in kernel
1498 * space and move it to user at the very end. This is unclean because
1499 * we open the socket then return an error.
1500 *
1501 * 1003.1g adds the ability to recvmsg() to query connection pending
1502 * status to recvmsg. We need to add that support in a way thats
1503 * clean when we restucture accept also.
1504 */
1505
Heiko Carstens20f37032009-01-14 14:14:23 +01001506SYSCALL_DEFINE4(accept4, int, fd, struct sockaddr __user *, upeer_sockaddr,
1507 int __user *, upeer_addrlen, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508{
1509 struct socket *sock, *newsock;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001510 struct file *newfile;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001511 int err, len, newfd, fput_needed;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001512 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
Ulrich Drepper77d27202008-07-23 21:29:35 -07001514 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001515 return -EINVAL;
1516
1517 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1518 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1519
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001520 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 if (!sock)
1522 goto out;
1523
1524 err = -ENFILE;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001525 if (!(newsock = sock_alloc()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 goto out_put;
1527
1528 newsock->type = sock->type;
1529 newsock->ops = sock->ops;
1530
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 /*
1532 * We don't need try_module_get here, as the listening socket (sock)
1533 * has the protocol module (sock->ops->owner) held.
1534 */
1535 __module_get(newsock->ops->owner);
1536
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001537 newfd = sock_alloc_fd(&newfile, flags & O_CLOEXEC);
David S. Miller39d8c1b2006-03-20 17:13:49 -08001538 if (unlikely(newfd < 0)) {
1539 err = newfd;
David S. Miller9a1875e2006-04-01 12:48:36 -08001540 sock_release(newsock);
1541 goto out_put;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001542 }
1543
Ulrich Drepper77d27202008-07-23 21:29:35 -07001544 err = sock_attach_fd(newsock, newfile, flags & O_NONBLOCK);
David S. Miller39d8c1b2006-03-20 17:13:49 -08001545 if (err < 0)
Alexey Dobriyan79f4f642007-03-26 14:09:52 -07001546 goto out_fd_simple;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001547
Frank Filza79af592005-09-27 15:23:38 -07001548 err = security_socket_accept(sock, newsock);
1549 if (err)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001550 goto out_fd;
Frank Filza79af592005-09-27 15:23:38 -07001551
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 err = sock->ops->accept(sock, newsock, sock->file->f_flags);
1553 if (err < 0)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001554 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
1556 if (upeer_sockaddr) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001557 if (newsock->ops->getname(newsock, (struct sockaddr *)&address,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001558 &len, 2) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 err = -ECONNABORTED;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001560 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 }
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001562 err = move_addr_to_user((struct sockaddr *)&address,
1563 len, upeer_sockaddr, upeer_addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 if (err < 0)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001565 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 }
1567
1568 /* File flags are not inherited via accept() unlike another OSes. */
1569
David S. Miller39d8c1b2006-03-20 17:13:49 -08001570 fd_install(newfd, newfile);
1571 err = newfd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001574 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575out:
1576 return err;
Alexey Dobriyan79f4f642007-03-26 14:09:52 -07001577out_fd_simple:
1578 sock_release(newsock);
1579 put_filp(newfile);
1580 put_unused_fd(newfd);
1581 goto out_put;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001582out_fd:
David S. Miller9606a212006-04-01 01:00:14 -08001583 fput(newfile);
David S. Miller39d8c1b2006-03-20 17:13:49 -08001584 put_unused_fd(newfd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 goto out_put;
1586}
1587
Heiko Carstens20f37032009-01-14 14:14:23 +01001588SYSCALL_DEFINE3(accept, int, fd, struct sockaddr __user *, upeer_sockaddr,
1589 int __user *, upeer_addrlen)
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001590{
Ulrich Drepperde11def2008-11-19 15:36:14 -08001591 return sys_accept4(fd, upeer_sockaddr, upeer_addrlen, 0);
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001592}
1593
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594/*
1595 * Attempt to connect to a socket with the server address. The address
1596 * is in user space so we verify it is OK and move it to kernel space.
1597 *
1598 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
1599 * break bindings
1600 *
1601 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
1602 * other SEQPACKET protocols that take time to connect() as it doesn't
1603 * include the -EINPROGRESS status for such sockets.
1604 */
1605
Heiko Carstens20f37032009-01-14 14:14:23 +01001606SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
1607 int, addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608{
1609 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001610 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001611 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001613 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 if (!sock)
1615 goto out;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001616 err = move_addr_to_kernel(uservaddr, addrlen, (struct sockaddr *)&address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 if (err < 0)
1618 goto out_put;
1619
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001620 err =
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001621 security_socket_connect(sock, (struct sockaddr *)&address, addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 if (err)
1623 goto out_put;
1624
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001625 err = sock->ops->connect(sock, (struct sockaddr *)&address, addrlen,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 sock->file->f_flags);
1627out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001628 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629out:
1630 return err;
1631}
1632
1633/*
1634 * Get the local address ('name') of a socket object. Move the obtained
1635 * name to user space.
1636 */
1637
Heiko Carstens20f37032009-01-14 14:14:23 +01001638SYSCALL_DEFINE3(getsockname, int, fd, struct sockaddr __user *, usockaddr,
1639 int __user *, usockaddr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640{
1641 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001642 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001643 int len, err, fput_needed;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001644
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001645 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 if (!sock)
1647 goto out;
1648
1649 err = security_socket_getsockname(sock);
1650 if (err)
1651 goto out_put;
1652
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001653 err = sock->ops->getname(sock, (struct sockaddr *)&address, &len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 if (err)
1655 goto out_put;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001656 err = move_addr_to_user((struct sockaddr *)&address, len, usockaddr, usockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
1658out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001659 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660out:
1661 return err;
1662}
1663
1664/*
1665 * Get the remote address ('name') of a socket object. Move the obtained
1666 * name to user space.
1667 */
1668
Heiko Carstens20f37032009-01-14 14:14:23 +01001669SYSCALL_DEFINE3(getpeername, int, fd, struct sockaddr __user *, usockaddr,
1670 int __user *, usockaddr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671{
1672 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001673 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001674 int len, err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001676 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1677 if (sock != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 err = security_socket_getpeername(sock);
1679 if (err) {
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001680 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 return err;
1682 }
1683
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001684 err =
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001685 sock->ops->getname(sock, (struct sockaddr *)&address, &len,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001686 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 if (!err)
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001688 err = move_addr_to_user((struct sockaddr *)&address, len, usockaddr,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001689 usockaddr_len);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001690 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 }
1692 return err;
1693}
1694
1695/*
1696 * Send a datagram to a given address. We move the address into kernel
1697 * space and check the user space data area is readable before invoking
1698 * the protocol.
1699 */
1700
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001701SYSCALL_DEFINE6(sendto, int, fd, void __user *, buff, size_t, len,
1702 unsigned, flags, struct sockaddr __user *, addr,
1703 int, addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704{
1705 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001706 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 int err;
1708 struct msghdr msg;
1709 struct iovec iov;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001710 int fput_needed;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001711
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001712 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1713 if (!sock)
David S. Miller4387ff72007-02-08 15:06:08 -08001714 goto out;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001715
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001716 iov.iov_base = buff;
1717 iov.iov_len = len;
1718 msg.msg_name = NULL;
1719 msg.msg_iov = &iov;
1720 msg.msg_iovlen = 1;
1721 msg.msg_control = NULL;
1722 msg.msg_controllen = 0;
1723 msg.msg_namelen = 0;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001724 if (addr) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001725 err = move_addr_to_kernel(addr, addr_len, (struct sockaddr *)&address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 if (err < 0)
1727 goto out_put;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001728 msg.msg_name = (struct sockaddr *)&address;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001729 msg.msg_namelen = addr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 }
1731 if (sock->file->f_flags & O_NONBLOCK)
1732 flags |= MSG_DONTWAIT;
1733 msg.msg_flags = flags;
1734 err = sock_sendmsg(sock, &msg, len);
1735
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001736out_put:
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001737 fput_light(sock->file, fput_needed);
David S. Miller4387ff72007-02-08 15:06:08 -08001738out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 return err;
1740}
1741
1742/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001743 * Send a datagram down a socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 */
1745
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001746SYSCALL_DEFINE4(send, int, fd, void __user *, buff, size_t, len,
1747 unsigned, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748{
1749 return sys_sendto(fd, buff, len, flags, NULL, 0);
1750}
1751
1752/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001753 * Receive a frame from the socket and optionally record the address of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 * sender. We verify the buffers are writable and if needed move the
1755 * sender address from kernel to user space.
1756 */
1757
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001758SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, ubuf, size_t, size,
1759 unsigned, flags, struct sockaddr __user *, addr,
1760 int __user *, addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761{
1762 struct socket *sock;
1763 struct iovec iov;
1764 struct msghdr msg;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001765 struct sockaddr_storage address;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001766 int err, err2;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001767 int fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001769 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 if (!sock)
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001771 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001773 msg.msg_control = NULL;
1774 msg.msg_controllen = 0;
1775 msg.msg_iovlen = 1;
1776 msg.msg_iov = &iov;
1777 iov.iov_len = size;
1778 iov.iov_base = ubuf;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001779 msg.msg_name = (struct sockaddr *)&address;
1780 msg.msg_namelen = sizeof(address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 if (sock->file->f_flags & O_NONBLOCK)
1782 flags |= MSG_DONTWAIT;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001783 err = sock_recvmsg(sock, &msg, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001785 if (err >= 0 && addr != NULL) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001786 err2 = move_addr_to_user((struct sockaddr *)&address,
1787 msg.msg_namelen, addr, addr_len);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001788 if (err2 < 0)
1789 err = err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 }
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001791
1792 fput_light(sock->file, fput_needed);
David S. Miller4387ff72007-02-08 15:06:08 -08001793out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 return err;
1795}
1796
1797/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001798 * Receive a datagram from a socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 */
1800
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001801asmlinkage long sys_recv(int fd, void __user *ubuf, size_t size,
1802 unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803{
1804 return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
1805}
1806
1807/*
1808 * Set a socket option. Because we don't know the option lengths we have
1809 * to pass the user mode parameter for the protocols to sort out.
1810 */
1811
Heiko Carstens20f37032009-01-14 14:14:23 +01001812SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname,
1813 char __user *, optval, int, optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001815 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 struct socket *sock;
1817
1818 if (optlen < 0)
1819 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001820
1821 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1822 if (sock != NULL) {
1823 err = security_socket_setsockopt(sock, level, optname);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001824 if (err)
1825 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
1827 if (level == SOL_SOCKET)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001828 err =
1829 sock_setsockopt(sock, level, optname, optval,
1830 optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 else
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001832 err =
1833 sock->ops->setsockopt(sock, level, optname, optval,
1834 optlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001835out_put:
1836 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 }
1838 return err;
1839}
1840
1841/*
1842 * Get a socket option. Because we don't know the option lengths we have
1843 * to pass a user mode parameter for the protocols to sort out.
1844 */
1845
Heiko Carstens20f37032009-01-14 14:14:23 +01001846SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname,
1847 char __user *, optval, int __user *, optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001849 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 struct socket *sock;
1851
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001852 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1853 if (sock != NULL) {
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001854 err = security_socket_getsockopt(sock, level, optname);
1855 if (err)
1856 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
1858 if (level == SOL_SOCKET)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001859 err =
1860 sock_getsockopt(sock, level, optname, optval,
1861 optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 else
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001863 err =
1864 sock->ops->getsockopt(sock, level, optname, optval,
1865 optlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001866out_put:
1867 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 }
1869 return err;
1870}
1871
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872/*
1873 * Shutdown a socket.
1874 */
1875
Heiko Carstens754fe8d2009-01-14 14:14:09 +01001876SYSCALL_DEFINE2(shutdown, int, fd, int, how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001878 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 struct socket *sock;
1880
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001881 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1882 if (sock != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 err = security_socket_shutdown(sock, how);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001884 if (!err)
1885 err = sock->ops->shutdown(sock, how);
1886 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 }
1888 return err;
1889}
1890
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001891/* A couple of helpful macros for getting the address of the 32/64 bit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 * fields which are the same type (int / unsigned) on our platforms.
1893 */
1894#define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
1895#define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
1896#define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
1897
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898/*
1899 * BSD sendmsg interface
1900 */
1901
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001902SYSCALL_DEFINE3(sendmsg, int, fd, struct msghdr __user *, msg, unsigned, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903{
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001904 struct compat_msghdr __user *msg_compat =
1905 (struct compat_msghdr __user *)msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001907 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
Alex Williamsonb9d717a2005-09-26 14:28:02 -07001909 unsigned char ctl[sizeof(struct cmsghdr) + 20]
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001910 __attribute__ ((aligned(sizeof(__kernel_size_t))));
1911 /* 20 is size of ipv6_pktinfo */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 unsigned char *ctl_buf = ctl;
1913 struct msghdr msg_sys;
1914 int err, ctl_len, iov_size, total_len;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001915 int fput_needed;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001916
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 err = -EFAULT;
1918 if (MSG_CMSG_COMPAT & flags) {
1919 if (get_compat_msghdr(&msg_sys, msg_compat))
1920 return -EFAULT;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001921 }
1922 else if (copy_from_user(&msg_sys, msg, sizeof(struct msghdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 return -EFAULT;
1924
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001925 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001926 if (!sock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 goto out;
1928
1929 /* do not move before msg_sys is valid */
1930 err = -EMSGSIZE;
1931 if (msg_sys.msg_iovlen > UIO_MAXIOV)
1932 goto out_put;
1933
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001934 /* Check whether to allocate the iovec area */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 err = -ENOMEM;
1936 iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
1937 if (msg_sys.msg_iovlen > UIO_FASTIOV) {
1938 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
1939 if (!iov)
1940 goto out_put;
1941 }
1942
1943 /* This will also move the address data into kernel space */
1944 if (MSG_CMSG_COMPAT & flags) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001945 err = verify_compat_iovec(&msg_sys, iov,
1946 (struct sockaddr *)&address,
1947 VERIFY_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 } else
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001949 err = verify_iovec(&msg_sys, iov,
1950 (struct sockaddr *)&address,
1951 VERIFY_READ);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001952 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 goto out_freeiov;
1954 total_len = err;
1955
1956 err = -ENOBUFS;
1957
1958 if (msg_sys.msg_controllen > INT_MAX)
1959 goto out_freeiov;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001960 ctl_len = msg_sys.msg_controllen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001962 err =
1963 cmsghdr_from_user_compat_to_kern(&msg_sys, sock->sk, ctl,
1964 sizeof(ctl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 if (err)
1966 goto out_freeiov;
1967 ctl_buf = msg_sys.msg_control;
Al Viro8920e8f2005-09-07 18:28:51 -07001968 ctl_len = msg_sys.msg_controllen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 } else if (ctl_len) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001970 if (ctl_len > sizeof(ctl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001972 if (ctl_buf == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 goto out_freeiov;
1974 }
1975 err = -EFAULT;
1976 /*
1977 * Careful! Before this, msg_sys.msg_control contains a user pointer.
1978 * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
1979 * checking falls down on this.
1980 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001981 if (copy_from_user(ctl_buf, (void __user *)msg_sys.msg_control,
1982 ctl_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 goto out_freectl;
1984 msg_sys.msg_control = ctl_buf;
1985 }
1986 msg_sys.msg_flags = flags;
1987
1988 if (sock->file->f_flags & O_NONBLOCK)
1989 msg_sys.msg_flags |= MSG_DONTWAIT;
1990 err = sock_sendmsg(sock, &msg_sys, total_len);
1991
1992out_freectl:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001993 if (ctl_buf != ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 sock_kfree_s(sock->sk, ctl_buf, ctl_len);
1995out_freeiov:
1996 if (iov != iovstack)
1997 sock_kfree_s(sock->sk, iov, iov_size);
1998out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001999 fput_light(sock->file, fput_needed);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002000out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 return err;
2002}
2003
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002004static int __sys_recvmsg(struct socket *sock, struct msghdr __user *msg,
2005 struct msghdr *msg_sys, unsigned flags, int nosec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006{
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002007 struct compat_msghdr __user *msg_compat =
2008 (struct compat_msghdr __user *)msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 struct iovec iovstack[UIO_FASTIOV];
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002010 struct iovec *iov = iovstack;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 unsigned long cmsg_ptr;
2012 int err, iov_size, total_len, len;
2013
2014 /* kernel mode address */
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07002015 struct sockaddr_storage addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
2017 /* user mode address pointers */
2018 struct sockaddr __user *uaddr;
2019 int __user *uaddr_len;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002020
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 if (MSG_CMSG_COMPAT & flags) {
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002022 if (get_compat_msghdr(msg_sys, msg_compat))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 return -EFAULT;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002024 }
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002025 else if (copy_from_user(msg_sys, msg, sizeof(struct msghdr)))
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002026 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 err = -EMSGSIZE;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002029 if (msg_sys->msg_iovlen > UIO_MAXIOV)
2030 goto out;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002031
2032 /* Check whether to allocate the iovec area */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 err = -ENOMEM;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002034 iov_size = msg_sys->msg_iovlen * sizeof(struct iovec);
2035 if (msg_sys->msg_iovlen > UIO_FASTIOV) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
2037 if (!iov)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002038 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 }
2040
2041 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002042 * Save the user-mode address (verify_iovec will change the
2043 * kernel msghdr to use the kernel address space)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002045
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002046 uaddr = (__force void __user *)msg_sys->msg_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 uaddr_len = COMPAT_NAMELEN(msg);
2048 if (MSG_CMSG_COMPAT & flags) {
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002049 err = verify_compat_iovec(msg_sys, iov,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07002050 (struct sockaddr *)&addr,
2051 VERIFY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 } else
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002053 err = verify_iovec(msg_sys, iov,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07002054 (struct sockaddr *)&addr,
2055 VERIFY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 if (err < 0)
2057 goto out_freeiov;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002058 total_len = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002060 cmsg_ptr = (unsigned long)msg_sys->msg_control;
2061 msg_sys->msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002062
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 if (sock->file->f_flags & O_NONBLOCK)
2064 flags |= MSG_DONTWAIT;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002065 err = (nosec ? sock_recvmsg_nosec : sock_recvmsg)(sock, msg_sys,
2066 total_len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 if (err < 0)
2068 goto out_freeiov;
2069 len = err;
2070
2071 if (uaddr != NULL) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07002072 err = move_addr_to_user((struct sockaddr *)&addr,
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002073 msg_sys->msg_namelen, uaddr,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002074 uaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 if (err < 0)
2076 goto out_freeiov;
2077 }
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002078 err = __put_user((msg_sys->msg_flags & ~MSG_CMSG_COMPAT),
David S. Miller37f7f422005-09-16 16:51:01 -07002079 COMPAT_FLAGS(msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 if (err)
2081 goto out_freeiov;
2082 if (MSG_CMSG_COMPAT & flags)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002083 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 &msg_compat->msg_controllen);
2085 else
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002086 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 &msg->msg_controllen);
2088 if (err)
2089 goto out_freeiov;
2090 err = len;
2091
2092out_freeiov:
2093 if (iov != iovstack)
2094 sock_kfree_s(sock->sk, iov, iov_size);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002095out:
2096 return err;
2097}
2098
2099/*
2100 * BSD recvmsg interface
2101 */
2102
2103SYSCALL_DEFINE3(recvmsg, int, fd, struct msghdr __user *, msg,
2104 unsigned int, flags)
2105{
2106 int fput_needed, err;
2107 struct msghdr msg_sys;
2108 struct socket *sock = sockfd_lookup_light(fd, &err, &fput_needed);
2109
2110 if (!sock)
2111 goto out;
2112
2113 err = __sys_recvmsg(sock, msg, &msg_sys, flags, 0);
2114
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002115 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116out:
2117 return err;
2118}
2119
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002120/*
2121 * Linux recvmmsg interface
2122 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002124int __sys_recvmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
2125 unsigned int flags, struct timespec *timeout)
2126{
2127 int fput_needed, err, datagrams;
2128 struct socket *sock;
2129 struct mmsghdr __user *entry;
2130 struct msghdr msg_sys;
2131 struct timespec end_time;
2132
2133 if (timeout &&
2134 poll_select_set_timeout(&end_time, timeout->tv_sec,
2135 timeout->tv_nsec))
2136 return -EINVAL;
2137
2138 datagrams = 0;
2139
2140 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2141 if (!sock)
2142 return err;
2143
2144 err = sock_error(sock->sk);
2145 if (err)
2146 goto out_put;
2147
2148 entry = mmsg;
2149
2150 while (datagrams < vlen) {
2151 /*
2152 * No need to ask LSM for more than the first datagram.
2153 */
2154 err = __sys_recvmsg(sock, (struct msghdr __user *)entry,
2155 &msg_sys, flags, datagrams);
2156 if (err < 0)
2157 break;
2158 err = put_user(err, &entry->msg_len);
2159 if (err)
2160 break;
2161 ++entry;
2162 ++datagrams;
2163
2164 if (timeout) {
2165 ktime_get_ts(timeout);
2166 *timeout = timespec_sub(end_time, *timeout);
2167 if (timeout->tv_sec < 0) {
2168 timeout->tv_sec = timeout->tv_nsec = 0;
2169 break;
2170 }
2171
2172 /* Timeout, return less than vlen datagrams */
2173 if (timeout->tv_nsec == 0 && timeout->tv_sec == 0)
2174 break;
2175 }
2176
2177 /* Out of band data, return right away */
2178 if (msg_sys.msg_flags & MSG_OOB)
2179 break;
2180 }
2181
2182out_put:
2183 fput_light(sock->file, fput_needed);
2184
2185 if (err == 0)
2186 return datagrams;
2187
2188 if (datagrams != 0) {
2189 /*
2190 * We may return less entries than requested (vlen) if the
2191 * sock is non block and there aren't enough datagrams...
2192 */
2193 if (err != -EAGAIN) {
2194 /*
2195 * ... or if recvmsg returns an error after we
2196 * received some datagrams, where we record the
2197 * error to return on the next call or if the
2198 * app asks about it using getsockopt(SO_ERROR).
2199 */
2200 sock->sk->sk_err = -err;
2201 }
2202
2203 return datagrams;
2204 }
2205
2206 return err;
2207}
2208
2209SYSCALL_DEFINE5(recvmmsg, int, fd, struct mmsghdr __user *, mmsg,
2210 unsigned int, vlen, unsigned int, flags,
2211 struct timespec __user *, timeout)
2212{
2213 int datagrams;
2214 struct timespec timeout_sys;
2215
2216 if (!timeout)
2217 return __sys_recvmmsg(fd, mmsg, vlen, flags, NULL);
2218
2219 if (copy_from_user(&timeout_sys, timeout, sizeof(timeout_sys)))
2220 return -EFAULT;
2221
2222 datagrams = __sys_recvmmsg(fd, mmsg, vlen, flags, &timeout_sys);
2223
2224 if (datagrams > 0 &&
2225 copy_to_user(timeout, &timeout_sys, sizeof(timeout_sys)))
2226 datagrams = -EFAULT;
2227
2228 return datagrams;
2229}
2230
2231#ifdef __ARCH_WANT_SYS_SOCKETCALL
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232/* Argument list sizes for sys_socketcall */
2233#define AL(x) ((x) * sizeof(unsigned long))
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002234static const unsigned char nargs[20] = {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002235 AL(0),AL(3),AL(3),AL(3),AL(2),AL(3),
2236 AL(3),AL(3),AL(4),AL(4),AL(4),AL(6),
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07002237 AL(6),AL(2),AL(5),AL(5),AL(3),AL(3),
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002238 AL(4),AL(5)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002239};
2240
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241#undef AL
2242
2243/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002244 * System call vectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 *
2246 * Argument checking cleaned up. Saved 20% in size.
2247 * This function doesn't need to set the kernel lock because
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002248 * it is set by the callees.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 */
2250
Heiko Carstens3e0fa652009-01-14 14:14:24 +01002251SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252{
2253 unsigned long a[6];
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002254 unsigned long a0, a1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 int err;
Arjan van de Ven47379052009-09-28 12:57:44 -07002256 unsigned int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002258 if (call < 1 || call > SYS_RECVMMSG)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 return -EINVAL;
2260
Arjan van de Ven47379052009-09-28 12:57:44 -07002261 len = nargs[call];
2262 if (len > sizeof(a))
2263 return -EINVAL;
2264
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 /* copy_from_user should be SMP safe. */
Arjan van de Ven47379052009-09-28 12:57:44 -07002266 if (copy_from_user(a, args, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 return -EFAULT;
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002268
Al Virof3298dc2008-12-10 03:16:51 -05002269 audit_socketcall(nargs[call] / sizeof(unsigned long), a);
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002270
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002271 a0 = a[0];
2272 a1 = a[1];
2273
2274 switch (call) {
2275 case SYS_SOCKET:
2276 err = sys_socket(a0, a1, a[2]);
2277 break;
2278 case SYS_BIND:
2279 err = sys_bind(a0, (struct sockaddr __user *)a1, a[2]);
2280 break;
2281 case SYS_CONNECT:
2282 err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
2283 break;
2284 case SYS_LISTEN:
2285 err = sys_listen(a0, a1);
2286 break;
2287 case SYS_ACCEPT:
Ulrich Drepperde11def2008-11-19 15:36:14 -08002288 err = sys_accept4(a0, (struct sockaddr __user *)a1,
2289 (int __user *)a[2], 0);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002290 break;
2291 case SYS_GETSOCKNAME:
2292 err =
2293 sys_getsockname(a0, (struct sockaddr __user *)a1,
2294 (int __user *)a[2]);
2295 break;
2296 case SYS_GETPEERNAME:
2297 err =
2298 sys_getpeername(a0, (struct sockaddr __user *)a1,
2299 (int __user *)a[2]);
2300 break;
2301 case SYS_SOCKETPAIR:
2302 err = sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
2303 break;
2304 case SYS_SEND:
2305 err = sys_send(a0, (void __user *)a1, a[2], a[3]);
2306 break;
2307 case SYS_SENDTO:
2308 err = sys_sendto(a0, (void __user *)a1, a[2], a[3],
2309 (struct sockaddr __user *)a[4], a[5]);
2310 break;
2311 case SYS_RECV:
2312 err = sys_recv(a0, (void __user *)a1, a[2], a[3]);
2313 break;
2314 case SYS_RECVFROM:
2315 err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
2316 (struct sockaddr __user *)a[4],
2317 (int __user *)a[5]);
2318 break;
2319 case SYS_SHUTDOWN:
2320 err = sys_shutdown(a0, a1);
2321 break;
2322 case SYS_SETSOCKOPT:
2323 err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]);
2324 break;
2325 case SYS_GETSOCKOPT:
2326 err =
2327 sys_getsockopt(a0, a1, a[2], (char __user *)a[3],
2328 (int __user *)a[4]);
2329 break;
2330 case SYS_SENDMSG:
2331 err = sys_sendmsg(a0, (struct msghdr __user *)a1, a[2]);
2332 break;
2333 case SYS_RECVMSG:
2334 err = sys_recvmsg(a0, (struct msghdr __user *)a1, a[2]);
2335 break;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002336 case SYS_RECVMMSG:
2337 err = sys_recvmmsg(a0, (struct mmsghdr __user *)a1, a[2], a[3],
2338 (struct timespec __user *)a[4]);
2339 break;
Ulrich Drepperde11def2008-11-19 15:36:14 -08002340 case SYS_ACCEPT4:
2341 err = sys_accept4(a0, (struct sockaddr __user *)a1,
2342 (int __user *)a[2], a[3]);
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07002343 break;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002344 default:
2345 err = -EINVAL;
2346 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 }
2348 return err;
2349}
2350
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002351#endif /* __ARCH_WANT_SYS_SOCKETCALL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002353/**
2354 * sock_register - add a socket protocol handler
2355 * @ops: description of protocol
2356 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 * This function is called by a protocol handler that wants to
2358 * advertise its address family, and have it linked into the
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002359 * socket interface. The value ops->family coresponds to the
2360 * socket system call protocol family.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 */
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002362int sock_register(const struct net_proto_family *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363{
2364 int err;
2365
2366 if (ops->family >= NPROTO) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002367 printk(KERN_CRIT "protocol %d >= NPROTO(%d)\n", ops->family,
2368 NPROTO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 return -ENOBUFS;
2370 }
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002371
2372 spin_lock(&net_family_lock);
2373 if (net_families[ops->family])
2374 err = -EEXIST;
2375 else {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002376 net_families[ops->family] = ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 err = 0;
2378 }
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002379 spin_unlock(&net_family_lock);
2380
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002381 printk(KERN_INFO "NET: Registered protocol family %d\n", ops->family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 return err;
2383}
2384
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002385/**
2386 * sock_unregister - remove a protocol handler
2387 * @family: protocol family to remove
2388 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 * This function is called by a protocol handler that wants to
2390 * remove its address family, and have it unlinked from the
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002391 * new socket creation.
2392 *
2393 * If protocol handler is a module, then it can use module reference
2394 * counts to protect against new references. If protocol handler is not
2395 * a module then it needs to provide its own protection in
2396 * the ops->create routine.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 */
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002398void sock_unregister(int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399{
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002400 BUG_ON(family < 0 || family >= NPROTO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002402 spin_lock(&net_family_lock);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002403 net_families[family] = NULL;
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002404 spin_unlock(&net_family_lock);
2405
2406 synchronize_rcu();
2407
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002408 printk(KERN_INFO "NET: Unregistered protocol family %d\n", family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409}
2410
Andi Kleen77d76ea2005-12-22 12:43:42 -08002411static int __init sock_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412{
2413 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002414 * Initialize sock SLAB cache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002416
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 sk_init();
2418
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002420 * Initialize skbuff SLAB cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 */
2422 skb_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423
2424 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002425 * Initialize the protocols module.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 */
2427
2428 init_inodecache();
2429 register_filesystem(&sock_fs_type);
2430 sock_mnt = kern_mount(&sock_fs_type);
Andi Kleen77d76ea2005-12-22 12:43:42 -08002431
2432 /* The real protocol initialization is performed in later initcalls.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 */
2434
2435#ifdef CONFIG_NETFILTER
2436 netfilter_init();
2437#endif
David S. Millercbeb3212005-12-22 12:58:55 -08002438
2439 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440}
2441
Andi Kleen77d76ea2005-12-22 12:43:42 -08002442core_initcall(sock_init); /* early initcall */
2443
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444#ifdef CONFIG_PROC_FS
2445void socket_seq_show(struct seq_file *seq)
2446{
2447 int cpu;
2448 int counter = 0;
2449
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -07002450 for_each_possible_cpu(cpu)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002451 counter += per_cpu(sockets_in_use, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452
2453 /* It can be negative, by the way. 8) */
2454 if (counter < 0)
2455 counter = 0;
2456
2457 seq_printf(seq, "sockets: used %d\n", counter);
2458}
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002459#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460
Shaun Pereira89bbfc92006-03-21 23:58:08 -08002461#ifdef CONFIG_COMPAT
Arnd Bergmann7a229382009-11-06 23:00:29 -08002462#if 0
2463static int do_siocgstamp(unsigned int fd, unsigned int cmd, unsigned long arg)
2464{
2465 struct compat_timeval __user *up = compat_ptr(arg);
2466 mm_segment_t old_fs = get_fs();
2467 struct timeval ktv;
2468 int err;
2469
2470 set_fs(KERNEL_DS);
2471 err = sys_ioctl(fd, cmd, (unsigned long)&ktv);
2472 set_fs(old_fs);
2473 if (!err) {
2474 err = put_user(ktv.tv_sec, &up->tv_sec);
2475 err |= __put_user(ktv.tv_usec, &up->tv_usec);
2476 }
2477 return err;
2478}
2479
2480static int do_siocgstampns(unsigned int fd, unsigned int cmd, unsigned long arg)
2481{
2482 struct compat_timespec __user *up = compat_ptr(arg);
2483 mm_segment_t old_fs = get_fs();
2484 struct timespec kts;
2485 int err;
2486
2487 set_fs(KERNEL_DS);
2488 err = sys_ioctl(fd, cmd, (unsigned long)&kts);
2489 set_fs(old_fs);
2490 if (!err) {
2491 err = put_user(kts.tv_sec, &up->tv_sec);
2492 err |= __put_user(kts.tv_nsec, &up->tv_nsec);
2493 }
2494 return err;
2495}
2496
2497struct ifmap32 {
2498 compat_ulong_t mem_start;
2499 compat_ulong_t mem_end;
2500 unsigned short base_addr;
2501 unsigned char irq;
2502 unsigned char dma;
2503 unsigned char port;
2504};
2505
2506struct ifreq32 {
2507#define IFHWADDRLEN 6
2508#define IFNAMSIZ 16
2509 union {
2510 char ifrn_name[IFNAMSIZ]; /* if name, e.g. "en0" */
2511 } ifr_ifrn;
2512 union {
2513 struct sockaddr ifru_addr;
2514 struct sockaddr ifru_dstaddr;
2515 struct sockaddr ifru_broadaddr;
2516 struct sockaddr ifru_netmask;
2517 struct sockaddr ifru_hwaddr;
2518 short ifru_flags;
2519 compat_int_t ifru_ivalue;
2520 compat_int_t ifru_mtu;
2521 struct ifmap32 ifru_map;
2522 char ifru_slave[IFNAMSIZ]; /* Just fits the size */
2523 char ifru_newname[IFNAMSIZ];
2524 compat_caddr_t ifru_data;
2525 /* XXXX? ifru_settings should be here */
2526 } ifr_ifru;
2527};
2528
2529struct ifconf32 {
2530 compat_int_t ifc_len; /* size of buffer */
2531 compat_caddr_t ifcbuf;
2532};
2533
2534static int dev_ifname32(unsigned int fd, unsigned int cmd, unsigned long arg)
2535{
2536 struct ifreq __user *uifr;
2537 int err;
2538
2539 uifr = compat_alloc_user_space(sizeof(struct ifreq));
2540 if (copy_in_user(uifr, compat_ptr(arg), sizeof(struct ifreq32)))
2541 return -EFAULT;
2542
2543 err = sys_ioctl(fd, SIOCGIFNAME, (unsigned long)uifr);
2544 if (err)
2545 return err;
2546
2547 if (copy_in_user(compat_ptr(arg), uifr, sizeof(struct ifreq32)))
2548 return -EFAULT;
2549
2550 return 0;
2551}
2552
2553static int dev_ifconf(unsigned int fd, unsigned int cmd, unsigned long arg)
2554{
2555 struct ifconf32 ifc32;
2556 struct ifconf ifc;
2557 struct ifconf __user *uifc;
2558 struct ifreq32 __user *ifr32;
2559 struct ifreq __user *ifr;
2560 unsigned int i, j;
2561 int err;
2562
2563 if (copy_from_user(&ifc32, compat_ptr(arg), sizeof(struct ifconf32)))
2564 return -EFAULT;
2565
2566 if (ifc32.ifcbuf == 0) {
2567 ifc32.ifc_len = 0;
2568 ifc.ifc_len = 0;
2569 ifc.ifc_req = NULL;
2570 uifc = compat_alloc_user_space(sizeof(struct ifconf));
2571 } else {
2572 size_t len =((ifc32.ifc_len / sizeof (struct ifreq32)) + 1) *
2573 sizeof (struct ifreq);
2574 uifc = compat_alloc_user_space(sizeof(struct ifconf) + len);
2575 ifc.ifc_len = len;
2576 ifr = ifc.ifc_req = (void __user *)(uifc + 1);
2577 ifr32 = compat_ptr(ifc32.ifcbuf);
2578 for (i = 0; i < ifc32.ifc_len; i += sizeof (struct ifreq32)) {
2579 if (copy_in_user(ifr, ifr32, sizeof(struct ifreq32)))
2580 return -EFAULT;
2581 ifr++;
2582 ifr32++;
2583 }
2584 }
2585 if (copy_to_user(uifc, &ifc, sizeof(struct ifconf)))
2586 return -EFAULT;
2587
2588 err = sys_ioctl (fd, SIOCGIFCONF, (unsigned long)uifc);
2589 if (err)
2590 return err;
2591
2592 if (copy_from_user(&ifc, uifc, sizeof(struct ifconf)))
2593 return -EFAULT;
2594
2595 ifr = ifc.ifc_req;
2596 ifr32 = compat_ptr(ifc32.ifcbuf);
2597 for (i = 0, j = 0;
2598 i + sizeof (struct ifreq32) <= ifc32.ifc_len && j < ifc.ifc_len;
2599 i += sizeof (struct ifreq32), j += sizeof (struct ifreq)) {
2600 if (copy_in_user(ifr32, ifr, sizeof (struct ifreq32)))
2601 return -EFAULT;
2602 ifr32++;
2603 ifr++;
2604 }
2605
2606 if (ifc32.ifcbuf == 0) {
2607 /* Translate from 64-bit structure multiple to
2608 * a 32-bit one.
2609 */
2610 i = ifc.ifc_len;
2611 i = ((i / sizeof(struct ifreq)) * sizeof(struct ifreq32));
2612 ifc32.ifc_len = i;
2613 } else {
2614 ifc32.ifc_len = i;
2615 }
2616 if (copy_to_user(compat_ptr(arg), &ifc32, sizeof(struct ifconf32)))
2617 return -EFAULT;
2618
2619 return 0;
2620}
2621
2622static int ethtool_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg)
2623{
2624 struct ifreq __user *ifr;
2625 struct ifreq32 __user *ifr32;
2626 u32 data;
2627 void __user *datap;
2628
2629 ifr = compat_alloc_user_space(sizeof(*ifr));
2630 ifr32 = compat_ptr(arg);
2631
2632 if (copy_in_user(&ifr->ifr_name, &ifr32->ifr_name, IFNAMSIZ))
2633 return -EFAULT;
2634
2635 if (get_user(data, &ifr32->ifr_ifru.ifru_data))
2636 return -EFAULT;
2637
2638 datap = compat_ptr(data);
2639 if (put_user(datap, &ifr->ifr_ifru.ifru_data))
2640 return -EFAULT;
2641
2642 return sys_ioctl(fd, cmd, (unsigned long) ifr);
2643}
2644
2645static int bond_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg)
2646{
2647 struct ifreq kifr;
2648 struct ifreq __user *uifr;
2649 struct ifreq32 __user *ifr32 = compat_ptr(arg);
2650 mm_segment_t old_fs;
2651 int err;
2652 u32 data;
2653 void __user *datap;
2654
2655 switch (cmd) {
2656 case SIOCBONDENSLAVE:
2657 case SIOCBONDRELEASE:
2658 case SIOCBONDSETHWADDR:
2659 case SIOCBONDCHANGEACTIVE:
2660 if (copy_from_user(&kifr, ifr32, sizeof(struct ifreq32)))
2661 return -EFAULT;
2662
2663 old_fs = get_fs();
2664 set_fs (KERNEL_DS);
2665 err = sys_ioctl (fd, cmd, (unsigned long)&kifr);
2666 set_fs (old_fs);
2667
2668 return err;
2669 case SIOCBONDSLAVEINFOQUERY:
2670 case SIOCBONDINFOQUERY:
2671 uifr = compat_alloc_user_space(sizeof(*uifr));
2672 if (copy_in_user(&uifr->ifr_name, &ifr32->ifr_name, IFNAMSIZ))
2673 return -EFAULT;
2674
2675 if (get_user(data, &ifr32->ifr_ifru.ifru_data))
2676 return -EFAULT;
2677
2678 datap = compat_ptr(data);
2679 if (put_user(datap, &uifr->ifr_ifru.ifru_data))
2680 return -EFAULT;
2681
2682 return sys_ioctl (fd, cmd, (unsigned long)uifr);
2683 default:
2684 return -EINVAL;
2685 };
2686}
2687
2688static int siocdevprivate_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg)
2689{
2690 struct ifreq __user *u_ifreq64;
2691 struct ifreq32 __user *u_ifreq32 = compat_ptr(arg);
2692 char tmp_buf[IFNAMSIZ];
2693 void __user *data64;
2694 u32 data32;
2695
2696 if (copy_from_user(&tmp_buf[0], &(u_ifreq32->ifr_ifrn.ifrn_name[0]),
2697 IFNAMSIZ))
2698 return -EFAULT;
2699 if (__get_user(data32, &u_ifreq32->ifr_ifru.ifru_data))
2700 return -EFAULT;
2701 data64 = compat_ptr(data32);
2702
2703 u_ifreq64 = compat_alloc_user_space(sizeof(*u_ifreq64));
2704
2705 /* Don't check these user accesses, just let that get trapped
2706 * in the ioctl handler instead.
2707 */
2708 if (copy_to_user(&u_ifreq64->ifr_ifrn.ifrn_name[0], &tmp_buf[0],
2709 IFNAMSIZ))
2710 return -EFAULT;
2711 if (__put_user(data64, &u_ifreq64->ifr_ifru.ifru_data))
2712 return -EFAULT;
2713
2714 return sys_ioctl(fd, cmd, (unsigned long) u_ifreq64);
2715}
2716
2717static int dev_ifsioc(unsigned int fd, unsigned int cmd, unsigned long arg)
2718{
2719 struct ifreq ifr;
2720 struct ifreq32 __user *uifr32;
2721 struct ifmap32 __user *uifmap32;
2722 mm_segment_t old_fs;
2723 int err;
2724
2725 uifr32 = compat_ptr(arg);
2726 uifmap32 = &uifr32->ifr_ifru.ifru_map;
2727 switch (cmd) {
2728 case SIOCSIFMAP:
2729 err = copy_from_user(&ifr, uifr32, sizeof(ifr.ifr_name));
2730 err |= __get_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
2731 err |= __get_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
2732 err |= __get_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
2733 err |= __get_user(ifr.ifr_map.irq, &uifmap32->irq);
2734 err |= __get_user(ifr.ifr_map.dma, &uifmap32->dma);
2735 err |= __get_user(ifr.ifr_map.port, &uifmap32->port);
2736 if (err)
2737 return -EFAULT;
2738 break;
2739 case SIOCSHWTSTAMP:
2740 if (copy_from_user(&ifr, uifr32, sizeof(*uifr32)))
2741 return -EFAULT;
2742 ifr.ifr_data = compat_ptr(uifr32->ifr_ifru.ifru_data);
2743 break;
2744 default:
2745 if (copy_from_user(&ifr, uifr32, sizeof(*uifr32)))
2746 return -EFAULT;
2747 break;
2748 }
2749 old_fs = get_fs();
2750 set_fs (KERNEL_DS);
2751 err = sys_ioctl (fd, cmd, (unsigned long)&ifr);
2752 set_fs (old_fs);
2753 if (!err) {
2754 switch (cmd) {
2755 case SIOCGIFFLAGS:
2756 case SIOCGIFMETRIC:
2757 case SIOCGIFMTU:
2758 case SIOCGIFMEM:
2759 case SIOCGIFHWADDR:
2760 case SIOCGIFINDEX:
2761 case SIOCGIFADDR:
2762 case SIOCGIFBRDADDR:
2763 case SIOCGIFDSTADDR:
2764 case SIOCGIFNETMASK:
2765 case SIOCGIFTXQLEN:
2766 if (copy_to_user(uifr32, &ifr, sizeof(*uifr32)))
2767 return -EFAULT;
2768 break;
2769 case SIOCGIFMAP:
2770 err = copy_to_user(uifr32, &ifr, sizeof(ifr.ifr_name));
2771 err |= __put_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
2772 err |= __put_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
2773 err |= __put_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
2774 err |= __put_user(ifr.ifr_map.irq, &uifmap32->irq);
2775 err |= __put_user(ifr.ifr_map.dma, &uifmap32->dma);
2776 err |= __put_user(ifr.ifr_map.port, &uifmap32->port);
2777 if (err)
2778 err = -EFAULT;
2779 break;
2780 }
2781 }
2782 return err;
2783}
2784
2785struct rtentry32 {
2786 u32 rt_pad1;
2787 struct sockaddr rt_dst; /* target address */
2788 struct sockaddr rt_gateway; /* gateway addr (RTF_GATEWAY) */
2789 struct sockaddr rt_genmask; /* target network mask (IP) */
2790 unsigned short rt_flags;
2791 short rt_pad2;
2792 u32 rt_pad3;
2793 unsigned char rt_tos;
2794 unsigned char rt_class;
2795 short rt_pad4;
2796 short rt_metric; /* +1 for binary compatibility! */
2797 /* char * */ u32 rt_dev; /* forcing the device at add */
2798 u32 rt_mtu; /* per route MTU/Window */
2799 u32 rt_window; /* Window clamping */
2800 unsigned short rt_irtt; /* Initial RTT */
2801};
2802
2803struct in6_rtmsg32 {
2804 struct in6_addr rtmsg_dst;
2805 struct in6_addr rtmsg_src;
2806 struct in6_addr rtmsg_gateway;
2807 u32 rtmsg_type;
2808 u16 rtmsg_dst_len;
2809 u16 rtmsg_src_len;
2810 u32 rtmsg_metric;
2811 u32 rtmsg_info;
2812 u32 rtmsg_flags;
2813 s32 rtmsg_ifindex;
2814};
2815
2816static int routing_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg)
2817{
2818 int ret;
2819 void *r = NULL;
2820 struct in6_rtmsg r6;
2821 struct rtentry r4;
2822 char devname[16];
2823 u32 rtdev;
2824 mm_segment_t old_fs = get_fs();
2825
2826 struct socket *mysock = sockfd_lookup(fd, &ret);
2827
2828 if (mysock && mysock->sk && mysock->sk->sk_family == AF_INET6) { /* ipv6 */
2829 struct in6_rtmsg32 __user *ur6 = compat_ptr(arg);
2830 ret = copy_from_user (&r6.rtmsg_dst, &(ur6->rtmsg_dst),
2831 3 * sizeof(struct in6_addr));
2832 ret |= __get_user (r6.rtmsg_type, &(ur6->rtmsg_type));
2833 ret |= __get_user (r6.rtmsg_dst_len, &(ur6->rtmsg_dst_len));
2834 ret |= __get_user (r6.rtmsg_src_len, &(ur6->rtmsg_src_len));
2835 ret |= __get_user (r6.rtmsg_metric, &(ur6->rtmsg_metric));
2836 ret |= __get_user (r6.rtmsg_info, &(ur6->rtmsg_info));
2837 ret |= __get_user (r6.rtmsg_flags, &(ur6->rtmsg_flags));
2838 ret |= __get_user (r6.rtmsg_ifindex, &(ur6->rtmsg_ifindex));
2839
2840 r = (void *) &r6;
2841 } else { /* ipv4 */
2842 struct rtentry32 __user *ur4 = compat_ptr(arg);
2843 ret = copy_from_user (&r4.rt_dst, &(ur4->rt_dst),
2844 3 * sizeof(struct sockaddr));
2845 ret |= __get_user (r4.rt_flags, &(ur4->rt_flags));
2846 ret |= __get_user (r4.rt_metric, &(ur4->rt_metric));
2847 ret |= __get_user (r4.rt_mtu, &(ur4->rt_mtu));
2848 ret |= __get_user (r4.rt_window, &(ur4->rt_window));
2849 ret |= __get_user (r4.rt_irtt, &(ur4->rt_irtt));
2850 ret |= __get_user (rtdev, &(ur4->rt_dev));
2851 if (rtdev) {
2852 ret |= copy_from_user (devname, compat_ptr(rtdev), 15);
2853 r4.rt_dev = devname; devname[15] = 0;
2854 } else
2855 r4.rt_dev = NULL;
2856
2857 r = (void *) &r4;
2858 }
2859
2860 if (ret) {
2861 ret = -EFAULT;
2862 goto out;
2863 }
2864
2865 set_fs (KERNEL_DS);
2866 ret = sys_ioctl (fd, cmd, (unsigned long) r);
2867 set_fs (old_fs);
2868
2869out:
2870 if (mysock)
2871 sockfd_put(mysock);
2872
2873 return ret;
2874}
2875
2876/* Since old style bridge ioctl's endup using SIOCDEVPRIVATE
2877 * for some operations; this forces use of the newer bridge-utils that
2878 * use compatiable ioctls
2879 */
2880static int old_bridge_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg)
2881{
2882 u32 tmp;
2883
2884 if (get_user(tmp, (u32 __user *) arg))
2885 return -EFAULT;
2886 if (tmp == BRCTL_GET_VERSION)
2887 return BRCTL_VERSION + 1;
2888 return -EINVAL;
2889}
2890
2891struct atmif_sioc32 {
2892 compat_int_t number;
2893 compat_int_t length;
2894 compat_caddr_t arg;
2895};
2896
2897struct atm_iobuf32 {
2898 compat_int_t length;
2899 compat_caddr_t buffer;
2900};
2901
2902#define ATM_GETLINKRATE32 _IOW('a', ATMIOC_ITF+1, struct atmif_sioc32)
2903#define ATM_GETNAMES32 _IOW('a', ATMIOC_ITF+3, struct atm_iobuf32)
2904#define ATM_GETTYPE32 _IOW('a', ATMIOC_ITF+4, struct atmif_sioc32)
2905#define ATM_GETESI32 _IOW('a', ATMIOC_ITF+5, struct atmif_sioc32)
2906#define ATM_GETADDR32 _IOW('a', ATMIOC_ITF+6, struct atmif_sioc32)
2907#define ATM_RSTADDR32 _IOW('a', ATMIOC_ITF+7, struct atmif_sioc32)
2908#define ATM_ADDADDR32 _IOW('a', ATMIOC_ITF+8, struct atmif_sioc32)
2909#define ATM_DELADDR32 _IOW('a', ATMIOC_ITF+9, struct atmif_sioc32)
2910#define ATM_GETCIRANGE32 _IOW('a', ATMIOC_ITF+10, struct atmif_sioc32)
2911#define ATM_SETCIRANGE32 _IOW('a', ATMIOC_ITF+11, struct atmif_sioc32)
2912#define ATM_SETESI32 _IOW('a', ATMIOC_ITF+12, struct atmif_sioc32)
2913#define ATM_SETESIF32 _IOW('a', ATMIOC_ITF+13, struct atmif_sioc32)
2914#define ATM_GETSTAT32 _IOW('a', ATMIOC_SARCOM+0, struct atmif_sioc32)
2915#define ATM_GETSTATZ32 _IOW('a', ATMIOC_SARCOM+1, struct atmif_sioc32)
2916#define ATM_GETLOOP32 _IOW('a', ATMIOC_SARCOM+2, struct atmif_sioc32)
2917#define ATM_SETLOOP32 _IOW('a', ATMIOC_SARCOM+3, struct atmif_sioc32)
2918#define ATM_QUERYLOOP32 _IOW('a', ATMIOC_SARCOM+4, struct atmif_sioc32)
2919
2920static struct {
2921 unsigned int cmd32;
2922 unsigned int cmd;
2923} atm_ioctl_map[] = {
2924 { ATM_GETLINKRATE32, ATM_GETLINKRATE },
2925 { ATM_GETNAMES32, ATM_GETNAMES },
2926 { ATM_GETTYPE32, ATM_GETTYPE },
2927 { ATM_GETESI32, ATM_GETESI },
2928 { ATM_GETADDR32, ATM_GETADDR },
2929 { ATM_RSTADDR32, ATM_RSTADDR },
2930 { ATM_ADDADDR32, ATM_ADDADDR },
2931 { ATM_DELADDR32, ATM_DELADDR },
2932 { ATM_GETCIRANGE32, ATM_GETCIRANGE },
2933 { ATM_SETCIRANGE32, ATM_SETCIRANGE },
2934 { ATM_SETESI32, ATM_SETESI },
2935 { ATM_SETESIF32, ATM_SETESIF },
2936 { ATM_GETSTAT32, ATM_GETSTAT },
2937 { ATM_GETSTATZ32, ATM_GETSTATZ },
2938 { ATM_GETLOOP32, ATM_GETLOOP },
2939 { ATM_SETLOOP32, ATM_SETLOOP },
2940 { ATM_QUERYLOOP32, ATM_QUERYLOOP }
2941};
2942
2943#define NR_ATM_IOCTL ARRAY_SIZE(atm_ioctl_map)
2944
2945static int do_atm_iobuf(unsigned int fd, unsigned int cmd, unsigned long arg)
2946{
2947 struct atm_iobuf __user *iobuf;
2948 struct atm_iobuf32 __user *iobuf32;
2949 u32 data;
2950 void __user *datap;
2951 int len, err;
2952
2953 iobuf = compat_alloc_user_space(sizeof(*iobuf));
2954 iobuf32 = compat_ptr(arg);
2955
2956 if (get_user(len, &iobuf32->length) ||
2957 get_user(data, &iobuf32->buffer))
2958 return -EFAULT;
2959 datap = compat_ptr(data);
2960 if (put_user(len, &iobuf->length) ||
2961 put_user(datap, &iobuf->buffer))
2962 return -EFAULT;
2963
2964 err = sys_ioctl(fd, cmd, (unsigned long)iobuf);
2965
2966 if (!err) {
2967 if (copy_in_user(&iobuf32->length, &iobuf->length,
2968 sizeof(int)))
2969 err = -EFAULT;
2970 }
2971
2972 return err;
2973}
2974
2975static int do_atmif_sioc(unsigned int fd, unsigned int cmd, unsigned long arg)
2976{
2977 struct atmif_sioc __user *sioc;
2978 struct atmif_sioc32 __user *sioc32;
2979 u32 data;
2980 void __user *datap;
2981 int err;
2982
2983 sioc = compat_alloc_user_space(sizeof(*sioc));
2984 sioc32 = compat_ptr(arg);
2985
2986 if (copy_in_user(&sioc->number, &sioc32->number, 2 * sizeof(int)) ||
2987 get_user(data, &sioc32->arg))
2988 return -EFAULT;
2989 datap = compat_ptr(data);
2990 if (put_user(datap, &sioc->arg))
2991 return -EFAULT;
2992
2993 err = sys_ioctl(fd, cmd, (unsigned long) sioc);
2994
2995 if (!err) {
2996 if (copy_in_user(&sioc32->length, &sioc->length,
2997 sizeof(int)))
2998 err = -EFAULT;
2999 }
3000 return err;
3001}
3002
3003static int do_atm_ioctl(unsigned int fd, unsigned int cmd32, unsigned long arg)
3004{
3005 int i;
3006 unsigned int cmd = 0;
3007
3008 switch (cmd32) {
3009 case SONET_GETSTAT:
3010 case SONET_GETSTATZ:
3011 case SONET_GETDIAG:
3012 case SONET_SETDIAG:
3013 case SONET_CLRDIAG:
3014 case SONET_SETFRAMING:
3015 case SONET_GETFRAMING:
3016 case SONET_GETFRSENSE:
3017 return do_atmif_sioc(fd, cmd32, arg);
3018 }
3019
3020 for (i = 0; i < NR_ATM_IOCTL; i++) {
3021 if (cmd32 == atm_ioctl_map[i].cmd32) {
3022 cmd = atm_ioctl_map[i].cmd;
3023 break;
3024 }
3025 }
3026 if (i == NR_ATM_IOCTL)
3027 return -EINVAL;
3028
3029 switch (cmd) {
3030 case ATM_GETNAMES:
3031 return do_atm_iobuf(fd, cmd, arg);
3032
3033 case ATM_GETLINKRATE:
3034 case ATM_GETTYPE:
3035 case ATM_GETESI:
3036 case ATM_GETADDR:
3037 case ATM_RSTADDR:
3038 case ATM_ADDADDR:
3039 case ATM_DELADDR:
3040 case ATM_GETCIRANGE:
3041 case ATM_SETCIRANGE:
3042 case ATM_SETESI:
3043 case ATM_SETESIF:
3044 case ATM_GETSTAT:
3045 case ATM_GETSTATZ:
3046 case ATM_GETLOOP:
3047 case ATM_SETLOOP:
3048 case ATM_QUERYLOOP:
3049 return do_atmif_sioc(fd, cmd, arg);
3050 }
3051
3052 return -EINVAL;
3053}
3054
3055
3056/* bridge */
3057HANDLE_IOCTL(SIOCSIFBR, old_bridge_ioctl)
3058HANDLE_IOCTL(SIOCGIFBR, old_bridge_ioctl)
3059#ifdef CONFIG_NET
3060HANDLE_IOCTL(SIOCGIFNAME, dev_ifname32)
3061HANDLE_IOCTL(SIOCGIFCONF, dev_ifconf)
3062HANDLE_IOCTL(SIOCGIFFLAGS, dev_ifsioc)
3063HANDLE_IOCTL(SIOCSIFFLAGS, dev_ifsioc)
3064HANDLE_IOCTL(SIOCGIFMETRIC, dev_ifsioc)
3065HANDLE_IOCTL(SIOCSIFMETRIC, dev_ifsioc)
3066HANDLE_IOCTL(SIOCGIFMTU, dev_ifsioc)
3067HANDLE_IOCTL(SIOCSIFMTU, dev_ifsioc)
3068HANDLE_IOCTL(SIOCGIFMEM, dev_ifsioc)
3069HANDLE_IOCTL(SIOCSIFMEM, dev_ifsioc)
3070HANDLE_IOCTL(SIOCGIFHWADDR, dev_ifsioc)
3071HANDLE_IOCTL(SIOCSIFHWADDR, dev_ifsioc)
3072HANDLE_IOCTL(SIOCADDMULTI, dev_ifsioc)
3073HANDLE_IOCTL(SIOCDELMULTI, dev_ifsioc)
3074HANDLE_IOCTL(SIOCGIFINDEX, dev_ifsioc)
3075HANDLE_IOCTL(SIOCGIFMAP, dev_ifsioc)
3076HANDLE_IOCTL(SIOCSIFMAP, dev_ifsioc)
3077HANDLE_IOCTL(SIOCGIFADDR, dev_ifsioc)
3078HANDLE_IOCTL(SIOCSIFADDR, dev_ifsioc)
3079HANDLE_IOCTL(SIOCSIFHWBROADCAST, dev_ifsioc)
3080HANDLE_IOCTL(SIOCSHWTSTAMP, dev_ifsioc)
3081
3082HANDLE_IOCTL(SIOCDIFADDR, dev_ifsioc)
3083HANDLE_IOCTL(SIOCSARP, dev_ifsioc)
3084HANDLE_IOCTL(SIOCDARP, dev_ifsioc)
3085
3086HANDLE_IOCTL(SIOCGIFBRDADDR, dev_ifsioc)
3087HANDLE_IOCTL(SIOCSIFBRDADDR, dev_ifsioc)
3088HANDLE_IOCTL(SIOCGIFDSTADDR, dev_ifsioc)
3089HANDLE_IOCTL(SIOCSIFDSTADDR, dev_ifsioc)
3090HANDLE_IOCTL(SIOCGIFNETMASK, dev_ifsioc)
3091HANDLE_IOCTL(SIOCSIFNETMASK, dev_ifsioc)
3092HANDLE_IOCTL(SIOCSIFPFLAGS, dev_ifsioc)
3093HANDLE_IOCTL(SIOCGIFPFLAGS, dev_ifsioc)
3094HANDLE_IOCTL(SIOCGIFTXQLEN, dev_ifsioc)
3095HANDLE_IOCTL(SIOCSIFTXQLEN, dev_ifsioc)
3096HANDLE_IOCTL(SIOCETHTOOL, ethtool_ioctl)
3097HANDLE_IOCTL(SIOCBONDENSLAVE, bond_ioctl)
3098HANDLE_IOCTL(SIOCBONDRELEASE, bond_ioctl)
3099HANDLE_IOCTL(SIOCBONDSETHWADDR, bond_ioctl)
3100HANDLE_IOCTL(SIOCBONDSLAVEINFOQUERY, bond_ioctl)
3101HANDLE_IOCTL(SIOCBONDINFOQUERY, bond_ioctl)
3102HANDLE_IOCTL(SIOCBONDCHANGEACTIVE, bond_ioctl)
3103HANDLE_IOCTL(SIOCADDRT, routing_ioctl)
3104HANDLE_IOCTL(SIOCDELRT, routing_ioctl)
3105HANDLE_IOCTL(SIOCBRADDIF, dev_ifsioc)
3106HANDLE_IOCTL(SIOCBRDELIF, dev_ifsioc)
3107/* Note SIOCRTMSG is no longer, so this is safe and * the user would have seen just an -EINVAL anyways. */
3108HANDLE_IOCTL(SIOCRTMSG, ret_einval)
3109HANDLE_IOCTL(SIOCGSTAMP, do_siocgstamp)
3110HANDLE_IOCTL(SIOCGSTAMPNS, do_siocgstampns)
3111#endif
3112IGNORE_IOCTL(SIOCGIFCOUNT)
3113/* Little a */
3114COMPATIBLE_IOCTL(ATMSIGD_CTRL)
3115COMPATIBLE_IOCTL(ATMARPD_CTRL)
3116COMPATIBLE_IOCTL(ATMLEC_CTRL)
3117COMPATIBLE_IOCTL(ATMLEC_MCAST)
3118COMPATIBLE_IOCTL(ATMLEC_DATA)
3119COMPATIBLE_IOCTL(ATM_SETSC)
3120COMPATIBLE_IOCTL(SIOCSIFATMTCP)
3121COMPATIBLE_IOCTL(SIOCMKCLIP)
3122COMPATIBLE_IOCTL(ATMARP_MKIP)
3123COMPATIBLE_IOCTL(ATMARP_SETENTRY)
3124COMPATIBLE_IOCTL(ATMARP_ENCAP)
3125COMPATIBLE_IOCTL(ATMTCP_CREATE)
3126COMPATIBLE_IOCTL(ATMTCP_REMOVE)
3127COMPATIBLE_IOCTL(ATMMPC_CTRL)
3128COMPATIBLE_IOCTL(ATMMPC_DATA)
3129HANDLE_IOCTL(ATM_GETLINKRATE32, do_atm_ioctl)
3130HANDLE_IOCTL(ATM_GETNAMES32, do_atm_ioctl)
3131HANDLE_IOCTL(ATM_GETTYPE32, do_atm_ioctl)
3132HANDLE_IOCTL(ATM_GETESI32, do_atm_ioctl)
3133HANDLE_IOCTL(ATM_GETADDR32, do_atm_ioctl)
3134HANDLE_IOCTL(ATM_RSTADDR32, do_atm_ioctl)
3135HANDLE_IOCTL(ATM_ADDADDR32, do_atm_ioctl)
3136HANDLE_IOCTL(ATM_DELADDR32, do_atm_ioctl)
3137HANDLE_IOCTL(ATM_GETCIRANGE32, do_atm_ioctl)
3138HANDLE_IOCTL(ATM_SETCIRANGE32, do_atm_ioctl)
3139HANDLE_IOCTL(ATM_SETESI32, do_atm_ioctl)
3140HANDLE_IOCTL(ATM_SETESIF32, do_atm_ioctl)
3141HANDLE_IOCTL(ATM_GETSTAT32, do_atm_ioctl)
3142HANDLE_IOCTL(ATM_GETSTATZ32, do_atm_ioctl)
3143HANDLE_IOCTL(ATM_GETLOOP32, do_atm_ioctl)
3144HANDLE_IOCTL(ATM_SETLOOP32, do_atm_ioctl)
3145HANDLE_IOCTL(ATM_QUERYLOOP32, do_atm_ioctl)
3146HANDLE_IOCTL(SONET_GETSTAT, do_atm_ioctl)
3147HANDLE_IOCTL(SONET_GETSTATZ, do_atm_ioctl)
3148HANDLE_IOCTL(SONET_GETDIAG, do_atm_ioctl)
3149HANDLE_IOCTL(SONET_SETDIAG, do_atm_ioctl)
3150HANDLE_IOCTL(SONET_CLRDIAG, do_atm_ioctl)
3151HANDLE_IOCTL(SONET_SETFRAMING, do_atm_ioctl)
3152HANDLE_IOCTL(SONET_GETFRAMING, do_atm_ioctl)
3153HANDLE_IOCTL(SONET_GETFRSENSE, do_atm_ioctl)
3154COMPATIBLE_IOCTL(FIOSETOWN)
3155COMPATIBLE_IOCTL(SIOCSPGRP)
3156COMPATIBLE_IOCTL(FIOGETOWN)
3157COMPATIBLE_IOCTL(SIOCGPGRP)
3158COMPATIBLE_IOCTL(SIOCATMARK)
3159COMPATIBLE_IOCTL(SIOCSIFLINK)
3160COMPATIBLE_IOCTL(SIOCSIFNAME)
3161COMPATIBLE_IOCTL(SIOCSARP)
3162COMPATIBLE_IOCTL(SIOCGARP)
3163COMPATIBLE_IOCTL(SIOCDARP)
3164COMPATIBLE_IOCTL(SIOCSRARP)
3165COMPATIBLE_IOCTL(SIOCGRARP)
3166COMPATIBLE_IOCTL(SIOCDRARP)
3167COMPATIBLE_IOCTL(SIOCADDDLCI)
3168COMPATIBLE_IOCTL(SIOCDELDLCI)
3169COMPATIBLE_IOCTL(SIOCGMIIPHY)
3170COMPATIBLE_IOCTL(SIOCGMIIREG)
3171COMPATIBLE_IOCTL(SIOCSMIIREG)
3172COMPATIBLE_IOCTL(SIOCGIFVLAN)
3173COMPATIBLE_IOCTL(SIOCSIFVLAN)
3174COMPATIBLE_IOCTL(SIOCBRADDBR)
3175COMPATIBLE_IOCTL(SIOCBRDELBR)
3176#endif
3177
Shaun Pereira89bbfc92006-03-21 23:58:08 -08003178static long compat_sock_ioctl(struct file *file, unsigned cmd,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07003179 unsigned long arg)
Shaun Pereira89bbfc92006-03-21 23:58:08 -08003180{
3181 struct socket *sock = file->private_data;
3182 int ret = -ENOIOCTLCMD;
David S. Miller87de87d2008-06-03 09:14:03 -07003183 struct sock *sk;
3184 struct net *net;
3185
3186 sk = sock->sk;
3187 net = sock_net(sk);
Shaun Pereira89bbfc92006-03-21 23:58:08 -08003188
3189 if (sock->ops->compat_ioctl)
3190 ret = sock->ops->compat_ioctl(sock, cmd, arg);
3191
David S. Miller87de87d2008-06-03 09:14:03 -07003192 if (ret == -ENOIOCTLCMD &&
3193 (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST))
3194 ret = compat_wext_handle_ioctl(net, cmd, arg);
3195
Shaun Pereira89bbfc92006-03-21 23:58:08 -08003196 return ret;
3197}
3198#endif
3199
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003200int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen)
3201{
3202 return sock->ops->bind(sock, addr, addrlen);
3203}
3204
3205int kernel_listen(struct socket *sock, int backlog)
3206{
3207 return sock->ops->listen(sock, backlog);
3208}
3209
3210int kernel_accept(struct socket *sock, struct socket **newsock, int flags)
3211{
3212 struct sock *sk = sock->sk;
3213 int err;
3214
3215 err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
3216 newsock);
3217 if (err < 0)
3218 goto done;
3219
3220 err = sock->ops->accept(sock, *newsock, flags);
3221 if (err < 0) {
3222 sock_release(*newsock);
Tony Battersbyfa8705b2007-10-10 21:09:04 -07003223 *newsock = NULL;
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003224 goto done;
3225 }
3226
3227 (*newsock)->ops = sock->ops;
Wei Yongjun1b085342008-12-18 19:35:10 -08003228 __module_get((*newsock)->ops->owner);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003229
3230done:
3231 return err;
3232}
3233
3234int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
YOSHIFUJI Hideaki4768fbc2007-02-09 23:25:31 +09003235 int flags)
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003236{
3237 return sock->ops->connect(sock, addr, addrlen, flags);
3238}
3239
3240int kernel_getsockname(struct socket *sock, struct sockaddr *addr,
3241 int *addrlen)
3242{
3243 return sock->ops->getname(sock, addr, addrlen, 0);
3244}
3245
3246int kernel_getpeername(struct socket *sock, struct sockaddr *addr,
3247 int *addrlen)
3248{
3249 return sock->ops->getname(sock, addr, addrlen, 1);
3250}
3251
3252int kernel_getsockopt(struct socket *sock, int level, int optname,
3253 char *optval, int *optlen)
3254{
3255 mm_segment_t oldfs = get_fs();
3256 int err;
3257
3258 set_fs(KERNEL_DS);
3259 if (level == SOL_SOCKET)
3260 err = sock_getsockopt(sock, level, optname, optval, optlen);
3261 else
3262 err = sock->ops->getsockopt(sock, level, optname, optval,
3263 optlen);
3264 set_fs(oldfs);
3265 return err;
3266}
3267
3268int kernel_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07003269 char *optval, unsigned int optlen)
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003270{
3271 mm_segment_t oldfs = get_fs();
3272 int err;
3273
3274 set_fs(KERNEL_DS);
3275 if (level == SOL_SOCKET)
3276 err = sock_setsockopt(sock, level, optname, optval, optlen);
3277 else
3278 err = sock->ops->setsockopt(sock, level, optname, optval,
3279 optlen);
3280 set_fs(oldfs);
3281 return err;
3282}
3283
3284int kernel_sendpage(struct socket *sock, struct page *page, int offset,
3285 size_t size, int flags)
3286{
3287 if (sock->ops->sendpage)
3288 return sock->ops->sendpage(sock, page, offset, size, flags);
3289
3290 return sock_no_sendpage(sock, page, offset, size, flags);
3291}
3292
3293int kernel_sock_ioctl(struct socket *sock, int cmd, unsigned long arg)
3294{
3295 mm_segment_t oldfs = get_fs();
3296 int err;
3297
3298 set_fs(KERNEL_DS);
3299 err = sock->ops->ioctl(sock, cmd, arg);
3300 set_fs(oldfs);
3301
3302 return err;
3303}
3304
Trond Myklebust91cf45f2007-11-12 18:10:39 -08003305int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how)
3306{
3307 return sock->ops->shutdown(sock, how);
3308}
3309
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310EXPORT_SYMBOL(sock_create);
3311EXPORT_SYMBOL(sock_create_kern);
3312EXPORT_SYMBOL(sock_create_lite);
3313EXPORT_SYMBOL(sock_map_fd);
3314EXPORT_SYMBOL(sock_recvmsg);
3315EXPORT_SYMBOL(sock_register);
3316EXPORT_SYMBOL(sock_release);
3317EXPORT_SYMBOL(sock_sendmsg);
3318EXPORT_SYMBOL(sock_unregister);
3319EXPORT_SYMBOL(sock_wake_async);
3320EXPORT_SYMBOL(sockfd_lookup);
3321EXPORT_SYMBOL(kernel_sendmsg);
3322EXPORT_SYMBOL(kernel_recvmsg);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003323EXPORT_SYMBOL(kernel_bind);
3324EXPORT_SYMBOL(kernel_listen);
3325EXPORT_SYMBOL(kernel_accept);
3326EXPORT_SYMBOL(kernel_connect);
3327EXPORT_SYMBOL(kernel_getsockname);
3328EXPORT_SYMBOL(kernel_getpeername);
3329EXPORT_SYMBOL(kernel_getsockopt);
3330EXPORT_SYMBOL(kernel_setsockopt);
3331EXPORT_SYMBOL(kernel_sendpage);
3332EXPORT_SYMBOL(kernel_sock_ioctl);
Trond Myklebust91cf45f2007-11-12 18:10:39 -08003333EXPORT_SYMBOL(kernel_sock_shutdown);