blob: 5e8d0af3c0e73b3537c1852eee2c15a917e4a605 [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 Black1fd73172009-09-22 16:43:33 -070089#include <linux/magic.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090090#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92#include <asm/uaccess.h>
93#include <asm/unistd.h>
94
95#include <net/compat.h>
David S. Miller87de87d2008-06-03 09:14:03 -070096#include <net/wext.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98#include <net/sock.h>
99#include <linux/netfilter.h>
100
Arnd Bergmann6b960182009-11-06 23:10:54 -0800101#include <linux/if_tun.h>
102#include <linux/ipv6_route.h>
103#include <linux/route.h>
Arnd Bergmann6b960182009-11-06 23:10:54 -0800104#include <linux/sockios.h>
105#include <linux/atalk.h>
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107static int sock_no_open(struct inode *irrelevant, struct file *dontcare);
Badari Pulavarty027445c2006-09-30 23:28:46 -0700108static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov,
109 unsigned long nr_segs, loff_t pos);
110static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov,
111 unsigned long nr_segs, loff_t pos);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700112static int sock_mmap(struct file *file, struct vm_area_struct *vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114static int sock_close(struct inode *inode, struct file *file);
115static unsigned int sock_poll(struct file *file,
116 struct poll_table_struct *wait);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700117static long sock_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
Shaun Pereira89bbfc92006-03-21 23:58:08 -0800118#ifdef CONFIG_COMPAT
119static long compat_sock_ioctl(struct file *file,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700120 unsigned int cmd, unsigned long arg);
Shaun Pereira89bbfc92006-03-21 23:58:08 -0800121#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122static int sock_fasync(int fd, struct file *filp, int on);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123static ssize_t sock_sendpage(struct file *file, struct page *page,
124 int offset, size_t size, loff_t *ppos, int more);
Jens Axboe9c55e012007-11-06 23:30:13 -0800125static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
126 struct pipe_inode_info *pipe, size_t len,
127 unsigned int flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129/*
130 * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
131 * in the operation structures but are done directly via the socketcall() multiplexor.
132 */
133
Arjan van de Venda7071d2007-02-12 00:55:36 -0800134static const struct file_operations socket_file_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 .owner = THIS_MODULE,
136 .llseek = no_llseek,
137 .aio_read = sock_aio_read,
138 .aio_write = sock_aio_write,
139 .poll = sock_poll,
140 .unlocked_ioctl = sock_ioctl,
Shaun Pereira89bbfc92006-03-21 23:58:08 -0800141#ifdef CONFIG_COMPAT
142 .compat_ioctl = compat_sock_ioctl,
143#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 .mmap = sock_mmap,
145 .open = sock_no_open, /* special open code to disallow open via /proc */
146 .release = sock_close,
147 .fasync = sock_fasync,
Jens Axboe5274f052006-03-30 15:15:30 +0200148 .sendpage = sock_sendpage,
149 .splice_write = generic_splice_sendpage,
Jens Axboe9c55e012007-11-06 23:30:13 -0800150 .splice_read = sock_splice_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151};
152
153/*
154 * The protocol list. Each protocol is registered in here.
155 */
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157static DEFINE_SPINLOCK(net_family_lock);
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -0700158static const struct net_proto_family *net_families[NPROTO] __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160/*
161 * Statistics counters of the socket lists
162 */
163
164static DEFINE_PER_CPU(int, sockets_in_use) = 0;
165
166/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700167 * Support routines.
168 * Move socket addresses back and forth across the kernel/user
169 * divide and look after the messy bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 */
171
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700172#define MAX_SOCK_ADDR 128 /* 108 for Unix domain -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 16 for IP, 16 for IPX,
174 24 for IPv6,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700175 about 80 for AX.25
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 must be at least one bigger than
177 the AF_UNIX size (see net/unix/af_unix.c
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700178 :unix_mkname()).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181/**
182 * move_addr_to_kernel - copy a socket address into kernel space
183 * @uaddr: Address in user space
184 * @kaddr: Address in kernel space
185 * @ulen: Length in user space
186 *
187 * The address is copied into kernel space. If the provided address is
188 * too long an error code of -EINVAL is returned. If the copy gives
189 * invalid addresses -EFAULT is returned. On a success 0 is returned.
190 */
191
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -0700192int move_addr_to_kernel(void __user *uaddr, int ulen, struct sockaddr *kaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -0700194 if (ulen < 0 || ulen > sizeof(struct sockaddr_storage))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700196 if (ulen == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 return 0;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700198 if (copy_from_user(kaddr, uaddr, ulen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 return -EFAULT;
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100200 return audit_sockaddr(ulen, kaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201}
202
203/**
204 * move_addr_to_user - copy an address to user space
205 * @kaddr: kernel space address
206 * @klen: length of address in kernel
207 * @uaddr: user space address
208 * @ulen: pointer to user length field
209 *
210 * The value pointed to by ulen on entry is the buffer length available.
211 * This is overwritten with the buffer space used. -EINVAL is returned
212 * if an overlong buffer is specified or a negative buffer size. -EFAULT
213 * is returned if either the buffer or the length field are not
214 * accessible.
215 * After copying the data up to the limit the user specifies, the true
216 * length of the data is written over the length limit the user
217 * specified. Zero is returned for a success.
218 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700219
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -0700220int move_addr_to_user(struct sockaddr *kaddr, int klen, void __user *uaddr,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700221 int __user *ulen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
223 int err;
224 int len;
225
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700226 err = get_user(len, ulen);
227 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 return err;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700229 if (len > klen)
230 len = klen;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -0700231 if (len < 0 || len > sizeof(struct sockaddr_storage))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700233 if (len) {
Steve Grubbd6fe3942006-03-30 12:20:22 -0500234 if (audit_sockaddr(klen, kaddr))
235 return -ENOMEM;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700236 if (copy_to_user(uaddr, kaddr, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 return -EFAULT;
238 }
239 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700240 * "fromlen shall refer to the value before truncation.."
241 * 1003.1g
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 */
243 return __put_user(klen, ulen);
244}
245
Christoph Lametere18b8902006-12-06 20:33:20 -0800246static struct kmem_cache *sock_inode_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248static struct inode *sock_alloc_inode(struct super_block *sb)
249{
250 struct socket_alloc *ei;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700251
Christoph Lametere94b1762006-12-06 20:33:17 -0800252 ei = kmem_cache_alloc(sock_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 if (!ei)
254 return NULL;
255 init_waitqueue_head(&ei->socket.wait);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 ei->socket.fasync_list = NULL;
258 ei->socket.state = SS_UNCONNECTED;
259 ei->socket.flags = 0;
260 ei->socket.ops = NULL;
261 ei->socket.sk = NULL;
262 ei->socket.file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 return &ei->vfs_inode;
265}
266
267static void sock_destroy_inode(struct inode *inode)
268{
269 kmem_cache_free(sock_inode_cachep,
270 container_of(inode, struct socket_alloc, vfs_inode));
271}
272
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700273static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700275 struct socket_alloc *ei = (struct socket_alloc *)foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Christoph Lametera35afb82007-05-16 22:10:57 -0700277 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280static int init_inodecache(void)
281{
282 sock_inode_cachep = kmem_cache_create("sock_inode_cache",
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700283 sizeof(struct socket_alloc),
284 0,
285 (SLAB_HWCACHE_ALIGN |
286 SLAB_RECLAIM_ACCOUNT |
287 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +0900288 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 if (sock_inode_cachep == NULL)
290 return -ENOMEM;
291 return 0;
292}
293
Alexey Dobriyanb87221d2009-09-21 17:01:09 -0700294static const struct super_operations sockfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 .alloc_inode = sock_alloc_inode,
296 .destroy_inode =sock_destroy_inode,
297 .statfs = simple_statfs,
298};
299
David Howells454e2392006-06-23 02:02:57 -0700300static int sockfs_get_sb(struct file_system_type *fs_type,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700301 int flags, const char *dev_name, void *data,
302 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
David Howells454e2392006-06-23 02:02:57 -0700304 return get_sb_pseudo(fs_type, "socket:", &sockfs_ops, SOCKFS_MAGIC,
305 mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
Eric Dumazetba899662005-08-26 12:05:31 -0700308static struct vfsmount *sock_mnt __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310static struct file_system_type sock_fs_type = {
311 .name = "sockfs",
312 .get_sb = sockfs_get_sb,
313 .kill_sb = kill_anon_super,
314};
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700315
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700316/*
317 * sockfs_dname() is called from d_path().
318 */
319static char *sockfs_dname(struct dentry *dentry, char *buffer, int buflen)
320{
321 return dynamic_dname(dentry, buffer, buflen, "socket:[%lu]",
322 dentry->d_inode->i_ino);
323}
324
Al Viro3ba13d12009-02-20 06:02:22 +0000325static const struct dentry_operations sockfs_dentry_operations = {
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700326 .d_dname = sockfs_dname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327};
328
329/*
330 * Obtains the first available file descriptor and sets it up for use.
331 *
David S. Miller39d8c1b2006-03-20 17:13:49 -0800332 * These functions create file structures and maps them to fd space
333 * of the current process. On success it returns file descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 * and file struct implicitly stored in sock->file.
335 * Note that another thread may close file descriptor before we return
336 * from this function. We use the fact that now we do not refer
337 * to socket after mapping. If one day we will need it, this
338 * function will increment ref. count on file by 1.
339 *
340 * In any case returned fd MAY BE not valid!
341 * This race condition is unavoidable
342 * with shared fd spaces, we cannot solve it inside kernel,
343 * but we take care of internal coherence yet.
344 */
345
Al Viro7cbe66b2009-08-05 19:59:08 +0400346static int sock_alloc_file(struct socket *sock, struct file **f, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
Al Viro7cbe66b2009-08-05 19:59:08 +0400348 struct qstr name = { .name = "" };
Al Viro2c48b9c2009-08-09 00:52:35 +0400349 struct path path;
Al Viro7cbe66b2009-08-05 19:59:08 +0400350 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 int fd;
David S. Miller39d8c1b2006-03-20 17:13:49 -0800352
Ulrich Dreppera677a032008-07-23 21:29:17 -0700353 fd = get_unused_fd_flags(flags);
Al Viro7cbe66b2009-08-05 19:59:08 +0400354 if (unlikely(fd < 0))
355 return fd;
David S. Miller39d8c1b2006-03-20 17:13:49 -0800356
Al Viro2c48b9c2009-08-09 00:52:35 +0400357 path.dentry = d_alloc(sock_mnt->mnt_sb->s_root, &name);
358 if (unlikely(!path.dentry)) {
Al Viro7cbe66b2009-08-05 19:59:08 +0400359 put_unused_fd(fd);
David S. Miller39d8c1b2006-03-20 17:13:49 -0800360 return -ENOMEM;
Al Viro7cbe66b2009-08-05 19:59:08 +0400361 }
Al Viro2c48b9c2009-08-09 00:52:35 +0400362 path.mnt = mntget(sock_mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Al Viro2c48b9c2009-08-09 00:52:35 +0400364 path.dentry->d_op = &sockfs_dentry_operations;
Al Viro2c48b9c2009-08-09 00:52:35 +0400365 d_instantiate(path.dentry, SOCK_INODE(sock));
Al Virocc3808f2009-08-06 09:43:59 +0400366 SOCK_INODE(sock)->i_fop = &socket_file_ops;
367
Al Viro2c48b9c2009-08-09 00:52:35 +0400368 file = alloc_file(&path, FMODE_READ | FMODE_WRITE,
Al Virocc3808f2009-08-06 09:43:59 +0400369 &socket_file_ops);
370 if (unlikely(!file)) {
371 /* drop dentry, keep inode */
372 atomic_inc(&path.dentry->d_inode->i_count);
Al Viro2c48b9c2009-08-09 00:52:35 +0400373 path_put(&path);
Al Virocc3808f2009-08-06 09:43:59 +0400374 put_unused_fd(fd);
375 return -ENFILE;
376 }
David S. Miller39d8c1b2006-03-20 17:13:49 -0800377
378 sock->file = file;
Ulrich Drepper77d27202008-07-23 21:29:35 -0700379 file->f_flags = O_RDWR | (flags & O_NONBLOCK);
David S. Miller39d8c1b2006-03-20 17:13:49 -0800380 file->f_pos = 0;
381 file->private_data = sock;
382
Al Viro7cbe66b2009-08-05 19:59:08 +0400383 *f = file;
384 return fd;
David S. Miller39d8c1b2006-03-20 17:13:49 -0800385}
386
Ulrich Dreppera677a032008-07-23 21:29:17 -0700387int sock_map_fd(struct socket *sock, int flags)
David S. Miller39d8c1b2006-03-20 17:13:49 -0800388{
389 struct file *newfile;
Al Viro7cbe66b2009-08-05 19:59:08 +0400390 int fd = sock_alloc_file(sock, &newfile, flags);
David S. Miller39d8c1b2006-03-20 17:13:49 -0800391
Al Viro7cbe66b2009-08-05 19:59:08 +0400392 if (likely(fd >= 0))
David S. Miller39d8c1b2006-03-20 17:13:49 -0800393 fd_install(fd, newfile);
Al Viro7cbe66b2009-08-05 19:59:08 +0400394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 return fd;
396}
397
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800398static struct socket *sock_from_file(struct file *file, int *err)
399{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800400 if (file->f_op == &socket_file_ops)
401 return file->private_data; /* set in sock_map_fd */
402
Eric Dumazet23bb80d2007-02-08 14:59:57 -0800403 *err = -ENOTSOCK;
404 return NULL;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800405}
406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407/**
408 * sockfd_lookup - Go from a file number to its socket slot
409 * @fd: file handle
410 * @err: pointer to an error code return
411 *
412 * The file handle passed in is locked and the socket it is bound
413 * too is returned. If an error occurs the err pointer is overwritten
414 * with a negative errno code and NULL is returned. The function checks
415 * for both invalid handles and passing a handle which is not a socket.
416 *
417 * On a success the socket object pointer is returned.
418 */
419
420struct socket *sockfd_lookup(int fd, int *err)
421{
422 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 struct socket *sock;
424
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700425 file = fget(fd);
426 if (!file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 *err = -EBADF;
428 return NULL;
429 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700430
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800431 sock = sock_from_file(file, err);
432 if (!sock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 fput(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return sock;
435}
436
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800437static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed)
438{
439 struct file *file;
440 struct socket *sock;
441
Hua Zhong36725582006-04-19 15:25:02 -0700442 *err = -EBADF;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800443 file = fget_light(fd, fput_needed);
444 if (file) {
445 sock = sock_from_file(file, err);
446 if (sock)
447 return sock;
448 fput_light(file, *fput_needed);
449 }
450 return NULL;
451}
452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453/**
454 * sock_alloc - allocate a socket
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700455 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 * Allocate a new inode and socket object. The two are bound together
457 * and initialised. The socket is then returned. If we are out of inodes
458 * NULL is returned.
459 */
460
461static struct socket *sock_alloc(void)
462{
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700463 struct inode *inode;
464 struct socket *sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 inode = new_inode(sock_mnt->mnt_sb);
467 if (!inode)
468 return NULL;
469
470 sock = SOCKET_I(inode);
471
Eric Dumazet29a020d2009-09-15 02:39:20 -0700472 kmemcheck_annotate_bitfield(sock, type);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700473 inode->i_mode = S_IFSOCK | S_IRWXUGO;
David Howells8192b0c2008-11-14 10:39:10 +1100474 inode->i_uid = current_fsuid();
475 inode->i_gid = current_fsgid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Eric Dumazet4e694892009-04-04 16:41:09 -0700477 percpu_add(sockets_in_use, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 return sock;
479}
480
481/*
482 * In theory you can't get an open on this inode, but /proc provides
483 * a back door. Remember to keep it shut otherwise you'll let the
484 * creepy crawlies in.
485 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487static int sock_no_open(struct inode *irrelevant, struct file *dontcare)
488{
489 return -ENXIO;
490}
491
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800492const struct file_operations bad_sock_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 .owner = THIS_MODULE,
494 .open = sock_no_open,
495};
496
497/**
498 * sock_release - close a socket
499 * @sock: socket to close
500 *
501 * The socket is released from the protocol stack if it has a release
502 * callback, and the inode is then released if the socket is bound to
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700503 * an inode not a file.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506void sock_release(struct socket *sock)
507{
508 if (sock->ops) {
509 struct module *owner = sock->ops->owner;
510
511 sock->ops->release(sock);
512 sock->ops = NULL;
513 module_put(owner);
514 }
515
516 if (sock->fasync_list)
517 printk(KERN_ERR "sock_release: fasync list not empty!\n");
518
Eric Dumazet4e694892009-04-04 16:41:09 -0700519 percpu_sub(sockets_in_use, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 if (!sock->file) {
521 iput(SOCK_INODE(sock));
522 return;
523 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700524 sock->file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525}
526
Patrick Ohly20d49472009-02-12 05:03:38 +0000527int sock_tx_timestamp(struct msghdr *msg, struct sock *sk,
528 union skb_shared_tx *shtx)
529{
530 shtx->flags = 0;
531 if (sock_flag(sk, SOCK_TIMESTAMPING_TX_HARDWARE))
532 shtx->hardware = 1;
533 if (sock_flag(sk, SOCK_TIMESTAMPING_TX_SOFTWARE))
534 shtx->software = 1;
535 return 0;
536}
537EXPORT_SYMBOL(sock_tx_timestamp);
538
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700539static inline int __sock_sendmsg(struct kiocb *iocb, struct socket *sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 struct msghdr *msg, size_t size)
541{
542 struct sock_iocb *si = kiocb_to_siocb(iocb);
543 int err;
544
545 si->sock = sock;
546 si->scm = NULL;
547 si->msg = msg;
548 si->size = size;
549
550 err = security_socket_sendmsg(sock, msg, size);
551 if (err)
552 return err;
553
554 return sock->ops->sendmsg(iocb, sock, msg, size);
555}
556
557int sock_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
558{
559 struct kiocb iocb;
560 struct sock_iocb siocb;
561 int ret;
562
563 init_sync_kiocb(&iocb, NULL);
564 iocb.private = &siocb;
565 ret = __sock_sendmsg(&iocb, sock, msg, size);
566 if (-EIOCBQUEUED == ret)
567 ret = wait_on_sync_kiocb(&iocb);
568 return ret;
569}
570
571int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
572 struct kvec *vec, size_t num, size_t size)
573{
574 mm_segment_t oldfs = get_fs();
575 int result;
576
577 set_fs(KERNEL_DS);
578 /*
579 * the following is safe, since for compiler definitions of kvec and
580 * iovec are identical, yielding the same in-core layout and alignment
581 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700582 msg->msg_iov = (struct iovec *)vec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 msg->msg_iovlen = num;
584 result = sock_sendmsg(sock, msg, size);
585 set_fs(oldfs);
586 return result;
587}
588
Patrick Ohly20d49472009-02-12 05:03:38 +0000589static int ktime2ts(ktime_t kt, struct timespec *ts)
590{
591 if (kt.tv64) {
592 *ts = ktime_to_timespec(kt);
593 return 1;
594 } else {
595 return 0;
596 }
597}
598
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700599/*
600 * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
601 */
602void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
603 struct sk_buff *skb)
604{
Patrick Ohly20d49472009-02-12 05:03:38 +0000605 int need_software_tstamp = sock_flag(sk, SOCK_RCVTSTAMP);
606 struct timespec ts[3];
607 int empty = 1;
608 struct skb_shared_hwtstamps *shhwtstamps =
609 skb_hwtstamps(skb);
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700610
Patrick Ohly20d49472009-02-12 05:03:38 +0000611 /* Race occurred between timestamp enabling and packet
612 receiving. Fill in the current time for now. */
613 if (need_software_tstamp && skb->tstamp.tv64 == 0)
614 __net_timestamp(skb);
615
616 if (need_software_tstamp) {
617 if (!sock_flag(sk, SOCK_RCVTSTAMPNS)) {
618 struct timeval tv;
619 skb_get_timestamp(skb, &tv);
620 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMP,
621 sizeof(tv), &tv);
622 } else {
623 struct timespec ts;
624 skb_get_timestampns(skb, &ts);
625 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPNS,
626 sizeof(ts), &ts);
627 }
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700628 }
Patrick Ohly20d49472009-02-12 05:03:38 +0000629
630
631 memset(ts, 0, sizeof(ts));
632 if (skb->tstamp.tv64 &&
633 sock_flag(sk, SOCK_TIMESTAMPING_SOFTWARE)) {
634 skb_get_timestampns(skb, ts + 0);
635 empty = 0;
636 }
637 if (shhwtstamps) {
638 if (sock_flag(sk, SOCK_TIMESTAMPING_SYS_HARDWARE) &&
639 ktime2ts(shhwtstamps->syststamp, ts + 1))
640 empty = 0;
641 if (sock_flag(sk, SOCK_TIMESTAMPING_RAW_HARDWARE) &&
642 ktime2ts(shhwtstamps->hwtstamp, ts + 2))
643 empty = 0;
644 }
645 if (!empty)
646 put_cmsg(msg, SOL_SOCKET,
647 SCM_TIMESTAMPING, sizeof(ts), &ts);
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700648}
649
Arnaldo Carvalho de Melo7c81fd82007-03-10 00:39:35 -0300650EXPORT_SYMBOL_GPL(__sock_recv_timestamp);
651
Neil Horman3b885782009-10-12 13:26:31 -0700652inline void sock_recv_drops(struct msghdr *msg, struct sock *sk, struct sk_buff *skb)
653{
654 if (sock_flag(sk, SOCK_RXQ_OVFL) && skb && skb->dropcount)
655 put_cmsg(msg, SOL_SOCKET, SO_RXQ_OVFL,
656 sizeof(__u32), &skb->dropcount);
657}
658
659void sock_recv_ts_and_drops(struct msghdr *msg, struct sock *sk,
660 struct sk_buff *skb)
661{
662 sock_recv_timestamp(msg, sk, skb);
663 sock_recv_drops(msg, sk, skb);
664}
665EXPORT_SYMBOL_GPL(sock_recv_ts_and_drops);
666
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -0700667static inline int __sock_recvmsg_nosec(struct kiocb *iocb, struct socket *sock,
668 struct msghdr *msg, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 struct sock_iocb *si = kiocb_to_siocb(iocb);
671
672 si->sock = sock;
673 si->scm = NULL;
674 si->msg = msg;
675 si->size = size;
676 si->flags = flags;
677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return sock->ops->recvmsg(iocb, sock, msg, size, flags);
679}
680
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -0700681static inline int __sock_recvmsg(struct kiocb *iocb, struct socket *sock,
682 struct msghdr *msg, size_t size, int flags)
683{
684 int err = security_socket_recvmsg(sock, msg, size, flags);
685
686 return err ?: __sock_recvmsg_nosec(iocb, sock, msg, size, flags);
687}
688
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700689int sock_recvmsg(struct socket *sock, struct msghdr *msg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 size_t size, int flags)
691{
692 struct kiocb iocb;
693 struct sock_iocb siocb;
694 int ret;
695
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700696 init_sync_kiocb(&iocb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 iocb.private = &siocb;
698 ret = __sock_recvmsg(&iocb, sock, msg, size, flags);
699 if (-EIOCBQUEUED == ret)
700 ret = wait_on_sync_kiocb(&iocb);
701 return ret;
702}
703
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -0700704static int sock_recvmsg_nosec(struct socket *sock, struct msghdr *msg,
705 size_t size, int flags)
706{
707 struct kiocb iocb;
708 struct sock_iocb siocb;
709 int ret;
710
711 init_sync_kiocb(&iocb, NULL);
712 iocb.private = &siocb;
713 ret = __sock_recvmsg_nosec(&iocb, sock, msg, size, flags);
714 if (-EIOCBQUEUED == ret)
715 ret = wait_on_sync_kiocb(&iocb);
716 return ret;
717}
718
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700719int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
720 struct kvec *vec, size_t num, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
722 mm_segment_t oldfs = get_fs();
723 int result;
724
725 set_fs(KERNEL_DS);
726 /*
727 * the following is safe, since for compiler definitions of kvec and
728 * iovec are identical, yielding the same in-core layout and alignment
729 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700730 msg->msg_iov = (struct iovec *)vec, msg->msg_iovlen = num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 result = sock_recvmsg(sock, msg, size, flags);
732 set_fs(oldfs);
733 return result;
734}
735
736static void sock_aio_dtor(struct kiocb *iocb)
737{
738 kfree(iocb->private);
739}
740
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -0300741static ssize_t sock_sendpage(struct file *file, struct page *page,
742 int offset, size_t size, loff_t *ppos, int more)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
744 struct socket *sock;
745 int flags;
746
Eric Dumazetb69aee02005-09-06 14:42:45 -0700747 sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749 flags = !(file->f_flags & O_NONBLOCK) ? 0 : MSG_DONTWAIT;
750 if (more)
751 flags |= MSG_MORE;
752
Linus Torvaldse6949582009-08-13 08:28:36 -0700753 return kernel_sendpage(sock, page, offset, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754}
755
Jens Axboe9c55e012007-11-06 23:30:13 -0800756static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
757 struct pipe_inode_info *pipe, size_t len,
758 unsigned int flags)
759{
760 struct socket *sock = file->private_data;
761
Rémi Denis-Courmont997b37d2008-02-15 02:35:45 -0800762 if (unlikely(!sock->ops->splice_read))
763 return -EINVAL;
764
Jens Axboe9c55e012007-11-06 23:30:13 -0800765 return sock->ops->splice_read(sock, ppos, pipe, len, flags);
766}
767
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800768static struct sock_iocb *alloc_sock_iocb(struct kiocb *iocb,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700769 struct sock_iocb *siocb)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800770{
771 if (!is_sync_kiocb(iocb)) {
772 siocb = kmalloc(sizeof(*siocb), GFP_KERNEL);
773 if (!siocb)
774 return NULL;
775 iocb->ki_dtor = sock_aio_dtor;
776 }
777
778 siocb->kiocb = iocb;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800779 iocb->private = siocb;
780 return siocb;
781}
782
783static ssize_t do_sock_read(struct msghdr *msg, struct kiocb *iocb,
Badari Pulavarty027445c2006-09-30 23:28:46 -0700784 struct file *file, const struct iovec *iov,
785 unsigned long nr_segs)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800786{
787 struct socket *sock = file->private_data;
788 size_t size = 0;
789 int i;
790
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700791 for (i = 0; i < nr_segs; i++)
792 size += iov[i].iov_len;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800793
794 msg->msg_name = NULL;
795 msg->msg_namelen = 0;
796 msg->msg_control = NULL;
797 msg->msg_controllen = 0;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700798 msg->msg_iov = (struct iovec *)iov;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800799 msg->msg_iovlen = nr_segs;
800 msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
801
802 return __sock_recvmsg(iocb, sock, msg, size, msg->msg_flags);
803}
804
Badari Pulavarty027445c2006-09-30 23:28:46 -0700805static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov,
806 unsigned long nr_segs, loff_t pos)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800807{
808 struct sock_iocb siocb, *x;
809
810 if (pos != 0)
811 return -ESPIPE;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700812
813 if (iocb->ki_left == 0) /* Match SYS5 behaviour */
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800814 return 0;
815
Badari Pulavarty027445c2006-09-30 23:28:46 -0700816
817 x = alloc_sock_iocb(iocb, &siocb);
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800818 if (!x)
819 return -ENOMEM;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700820 return do_sock_read(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800821}
822
823static ssize_t do_sock_write(struct msghdr *msg, struct kiocb *iocb,
Badari Pulavarty027445c2006-09-30 23:28:46 -0700824 struct file *file, const struct iovec *iov,
825 unsigned long nr_segs)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800826{
827 struct socket *sock = file->private_data;
828 size_t size = 0;
829 int i;
830
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700831 for (i = 0; i < nr_segs; i++)
832 size += iov[i].iov_len;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800833
834 msg->msg_name = NULL;
835 msg->msg_namelen = 0;
836 msg->msg_control = NULL;
837 msg->msg_controllen = 0;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700838 msg->msg_iov = (struct iovec *)iov;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800839 msg->msg_iovlen = nr_segs;
840 msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
841 if (sock->type == SOCK_SEQPACKET)
842 msg->msg_flags |= MSG_EOR;
843
844 return __sock_sendmsg(iocb, sock, msg, size);
845}
846
Badari Pulavarty027445c2006-09-30 23:28:46 -0700847static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov,
848 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800850 struct sock_iocb siocb, *x;
851
852 if (pos != 0)
853 return -ESPIPE;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700854
Badari Pulavarty027445c2006-09-30 23:28:46 -0700855 x = alloc_sock_iocb(iocb, &siocb);
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800856 if (!x)
857 return -ENOMEM;
858
Badari Pulavarty027445c2006-09-30 23:28:46 -0700859 return do_sock_write(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860}
861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862/*
863 * Atomic setting of ioctl hooks to avoid race
864 * with module unload.
865 */
866
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800867static DEFINE_MUTEX(br_ioctl_mutex);
Eric W. Biederman881d9662007-09-17 11:56:21 -0700868static int (*br_ioctl_hook) (struct net *, unsigned int cmd, void __user *arg) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Eric W. Biederman881d9662007-09-17 11:56:21 -0700870void brioctl_set(int (*hook) (struct net *, unsigned int, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800872 mutex_lock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 br_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800874 mutex_unlock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875}
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877EXPORT_SYMBOL(brioctl_set);
878
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800879static DEFINE_MUTEX(vlan_ioctl_mutex);
Eric W. Biederman881d9662007-09-17 11:56:21 -0700880static int (*vlan_ioctl_hook) (struct net *, void __user *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
Eric W. Biederman881d9662007-09-17 11:56:21 -0700882void vlan_ioctl_set(int (*hook) (struct net *, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800884 mutex_lock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 vlan_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800886 mutex_unlock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887}
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889EXPORT_SYMBOL(vlan_ioctl_set);
890
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800891static DEFINE_MUTEX(dlci_ioctl_mutex);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700892static int (*dlci_ioctl_hook) (unsigned int, void __user *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700894void dlci_ioctl_set(int (*hook) (unsigned int, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800896 mutex_lock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 dlci_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800898 mutex_unlock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899}
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901EXPORT_SYMBOL(dlci_ioctl_set);
902
Arnd Bergmann6b960182009-11-06 23:10:54 -0800903static long sock_do_ioctl(struct net *net, struct socket *sock,
904 unsigned int cmd, unsigned long arg)
905{
906 int err;
907 void __user *argp = (void __user *)arg;
908
909 err = sock->ops->ioctl(sock, cmd, arg);
910
911 /*
912 * If this ioctl is unknown try to hand it down
913 * to the NIC driver.
914 */
915 if (err == -ENOIOCTLCMD)
916 err = dev_ioctl(net, cmd, argp);
917
918 return err;
919}
920
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921/*
922 * With an ioctl, arg may well be a user mode pointer, but we don't know
923 * what to do with it - that's up to the protocol still.
924 */
925
926static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
927{
928 struct socket *sock;
Eric W. Biederman881d9662007-09-17 11:56:21 -0700929 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 void __user *argp = (void __user *)arg;
931 int pid, err;
Eric W. Biederman881d9662007-09-17 11:56:21 -0700932 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Eric Dumazetb69aee02005-09-06 14:42:45 -0700934 sock = file->private_data;
Eric W. Biederman881d9662007-09-17 11:56:21 -0700935 sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900936 net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
Eric W. Biederman881d9662007-09-17 11:56:21 -0700938 err = dev_ioctl(net, cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 } else
Johannes Berg3d23e342009-09-29 23:27:28 +0200940#ifdef CONFIG_WEXT_CORE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
Eric W. Biederman881d9662007-09-17 11:56:21 -0700942 err = dev_ioctl(net, cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 } else
Johannes Berg3d23e342009-09-29 23:27:28 +0200944#endif
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700945 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 case FIOSETOWN:
947 case SIOCSPGRP:
948 err = -EFAULT;
949 if (get_user(pid, (int __user *)argp))
950 break;
951 err = f_setown(sock->file, pid, 1);
952 break;
953 case FIOGETOWN:
954 case SIOCGPGRP:
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700955 err = put_user(f_getown(sock->file),
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700956 (int __user *)argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 break;
958 case SIOCGIFBR:
959 case SIOCSIFBR:
960 case SIOCBRADDBR:
961 case SIOCBRDELBR:
962 err = -ENOPKG;
963 if (!br_ioctl_hook)
964 request_module("bridge");
965
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800966 mutex_lock(&br_ioctl_mutex);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700967 if (br_ioctl_hook)
Eric W. Biederman881d9662007-09-17 11:56:21 -0700968 err = br_ioctl_hook(net, cmd, argp);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800969 mutex_unlock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 break;
971 case SIOCGIFVLAN:
972 case SIOCSIFVLAN:
973 err = -ENOPKG;
974 if (!vlan_ioctl_hook)
975 request_module("8021q");
976
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800977 mutex_lock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 if (vlan_ioctl_hook)
Eric W. Biederman881d9662007-09-17 11:56:21 -0700979 err = vlan_ioctl_hook(net, argp);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800980 mutex_unlock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 case SIOCADDDLCI:
983 case SIOCDELDLCI:
984 err = -ENOPKG;
985 if (!dlci_ioctl_hook)
986 request_module("dlci");
987
Pavel Emelyanov7512cbf2008-03-21 15:58:52 -0700988 mutex_lock(&dlci_ioctl_mutex);
989 if (dlci_ioctl_hook)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 err = dlci_ioctl_hook(cmd, argp);
Pavel Emelyanov7512cbf2008-03-21 15:58:52 -0700991 mutex_unlock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 break;
993 default:
Arnd Bergmann6b960182009-11-06 23:10:54 -0800994 err = sock_do_ioctl(net, sock, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 break;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700996 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 return err;
998}
999
1000int sock_create_lite(int family, int type, int protocol, struct socket **res)
1001{
1002 int err;
1003 struct socket *sock = NULL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 err = security_socket_create(family, type, protocol, 1);
1006 if (err)
1007 goto out;
1008
1009 sock = sock_alloc();
1010 if (!sock) {
1011 err = -ENOMEM;
1012 goto out;
1013 }
1014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 sock->type = type;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001016 err = security_socket_post_create(sock, family, type, protocol, 1);
1017 if (err)
1018 goto out_release;
1019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020out:
1021 *res = sock;
1022 return err;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001023out_release:
1024 sock_release(sock);
1025 sock = NULL;
1026 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027}
1028
1029/* No kernel lock held - perfect */
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001030static unsigned int sock_poll(struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
1032 struct socket *sock;
1033
1034 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001035 * We can't return errors to poll, so it's either yes or no.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 */
Eric Dumazetb69aee02005-09-06 14:42:45 -07001037 sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 return sock->ops->poll(file, sock, wait);
1039}
1040
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001041static int sock_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
Eric Dumazetb69aee02005-09-06 14:42:45 -07001043 struct socket *sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
1045 return sock->ops->mmap(file, sock, vma);
1046}
1047
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001048static int sock_close(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049{
1050 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001051 * It was possible the inode is NULL we were
1052 * closing an unfinished socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 */
1054
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001055 if (!inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 printk(KERN_DEBUG "sock_close: NULL inode\n");
1057 return 0;
1058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 sock_release(SOCKET_I(inode));
1060 return 0;
1061}
1062
1063/*
1064 * Update the socket async list
1065 *
1066 * Fasync_list locking strategy.
1067 *
1068 * 1. fasync_list is modified only under process context socket lock
1069 * i.e. under semaphore.
1070 * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
1071 * or under socket lock.
1072 * 3. fasync_list can be used from softirq context, so that
1073 * modification under socket lock have to be enhanced with
1074 * write_lock_bh(&sk->sk_callback_lock).
1075 * --ANK (990710)
1076 */
1077
1078static int sock_fasync(int fd, struct file *filp, int on)
1079{
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001080 struct fasync_struct *fa, *fna = NULL, **prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 struct socket *sock;
1082 struct sock *sk;
1083
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001084 if (on) {
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08001085 fna = kmalloc(sizeof(struct fasync_struct), GFP_KERNEL);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001086 if (fna == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 return -ENOMEM;
1088 }
1089
Eric Dumazetb69aee02005-09-06 14:42:45 -07001090 sock = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001092 sk = sock->sk;
1093 if (sk == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 kfree(fna);
1095 return -EINVAL;
1096 }
1097
1098 lock_sock(sk);
1099
Jonathan Corbet76398422009-02-01 14:26:59 -07001100 spin_lock(&filp->f_lock);
1101 if (on)
1102 filp->f_flags |= FASYNC;
1103 else
1104 filp->f_flags &= ~FASYNC;
1105 spin_unlock(&filp->f_lock);
1106
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001107 prev = &(sock->fasync_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001109 for (fa = *prev; fa != NULL; prev = &fa->fa_next, fa = *prev)
1110 if (fa->fa_file == filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 break;
1112
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001113 if (on) {
1114 if (fa != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 write_lock_bh(&sk->sk_callback_lock);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001116 fa->fa_fd = fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 write_unlock_bh(&sk->sk_callback_lock);
1118
1119 kfree(fna);
1120 goto out;
1121 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001122 fna->fa_file = filp;
1123 fna->fa_fd = fd;
1124 fna->magic = FASYNC_MAGIC;
1125 fna->fa_next = sock->fasync_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 write_lock_bh(&sk->sk_callback_lock);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001127 sock->fasync_list = fna;
Eric Dumazetbcdce712009-10-06 17:28:29 -07001128 sock_set_flag(sk, SOCK_FASYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 write_unlock_bh(&sk->sk_callback_lock);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001130 } else {
1131 if (fa != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 write_lock_bh(&sk->sk_callback_lock);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001133 *prev = fa->fa_next;
Eric Dumazetbcdce712009-10-06 17:28:29 -07001134 if (!sock->fasync_list)
1135 sock_reset_flag(sk, SOCK_FASYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 write_unlock_bh(&sk->sk_callback_lock);
1137 kfree(fa);
1138 }
1139 }
1140
1141out:
1142 release_sock(sock->sk);
1143 return 0;
1144}
1145
1146/* This function may be called only under socket lock or callback_lock */
1147
1148int sock_wake_async(struct socket *sock, int how, int band)
1149{
1150 if (!sock || !sock->fasync_list)
1151 return -1;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001152 switch (how) {
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001153 case SOCK_WAKE_WAITD:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 if (test_bit(SOCK_ASYNC_WAITDATA, &sock->flags))
1155 break;
1156 goto call_kill;
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001157 case SOCK_WAKE_SPACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags))
1159 break;
1160 /* fall through */
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001161 case SOCK_WAKE_IO:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001162call_kill:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 __kill_fasync(sock->fasync_list, SIGIO, band);
1164 break;
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001165 case SOCK_WAKE_URG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 __kill_fasync(sock->fasync_list, SIGURG, band);
1167 }
1168 return 0;
1169}
1170
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001171static int __sock_create(struct net *net, int family, int type, int protocol,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001172 struct socket **res, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173{
1174 int err;
1175 struct socket *sock;
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001176 const struct net_proto_family *pf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
1178 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001179 * Check protocol is in range
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 */
1181 if (family < 0 || family >= NPROTO)
1182 return -EAFNOSUPPORT;
1183 if (type < 0 || type >= SOCK_MAX)
1184 return -EINVAL;
1185
1186 /* Compatibility.
1187
1188 This uglymoron is moved from INET layer to here to avoid
1189 deadlock in module load.
1190 */
1191 if (family == PF_INET && type == SOCK_PACKET) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001192 static int warned;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 if (!warned) {
1194 warned = 1;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001195 printk(KERN_INFO "%s uses obsolete (PF_INET,SOCK_PACKET)\n",
1196 current->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 }
1198 family = PF_PACKET;
1199 }
1200
1201 err = security_socket_create(family, type, protocol, kern);
1202 if (err)
1203 return err;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001204
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001205 /*
1206 * Allocate the socket and allow the family to set things up. if
1207 * the protocol is 0, the family is instructed to select an appropriate
1208 * default.
1209 */
1210 sock = sock_alloc();
1211 if (!sock) {
1212 if (net_ratelimit())
1213 printk(KERN_WARNING "socket: no more sockets\n");
1214 return -ENFILE; /* Not exactly a match, but its the
1215 closest posix thing */
1216 }
1217
1218 sock->type = type;
1219
Johannes Berg95a5afc2008-10-16 15:24:51 -07001220#ifdef CONFIG_MODULES
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001221 /* Attempt to load a protocol module if the find failed.
1222 *
1223 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 * requested real, full-featured networking support upon configuration.
1225 * Otherwise module support will break!
1226 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001227 if (net_families[family] == NULL)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001228 request_module("net-pf-%d", family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229#endif
1230
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001231 rcu_read_lock();
1232 pf = rcu_dereference(net_families[family]);
1233 err = -EAFNOSUPPORT;
1234 if (!pf)
1235 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
1237 /*
1238 * We will call the ->create function, that possibly is in a loadable
1239 * module, so we have to bump that loadable module refcnt first.
1240 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001241 if (!try_module_get(pf->owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 goto out_release;
1243
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001244 /* Now protected by module ref count */
1245 rcu_read_unlock();
1246
Eric Paris3f378b62009-11-05 22:18:14 -08001247 err = pf->create(net, sock, protocol, kern);
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001248 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 goto out_module_put;
Frank Filza79af592005-09-27 15:23:38 -07001250
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 /*
1252 * Now to bump the refcnt of the [loadable] module that owns this
1253 * socket at sock_release time we decrement its refcnt.
1254 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001255 if (!try_module_get(sock->ops->owner))
1256 goto out_module_busy;
1257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 /*
1259 * Now that we're done with the ->create function, the [loadable]
1260 * module can have its refcnt decremented
1261 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001262 module_put(pf->owner);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001263 err = security_socket_post_create(sock, family, type, protocol, kern);
1264 if (err)
Herbert Xu3b185522007-08-15 14:46:02 -07001265 goto out_sock_release;
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001266 *res = sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001268 return 0;
1269
1270out_module_busy:
1271 err = -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272out_module_put:
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001273 sock->ops = NULL;
1274 module_put(pf->owner);
1275out_sock_release:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 sock_release(sock);
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001277 return err;
1278
1279out_release:
1280 rcu_read_unlock();
1281 goto out_sock_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282}
1283
1284int sock_create(int family, int type, int protocol, struct socket **res)
1285{
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001286 return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287}
1288
1289int sock_create_kern(int family, int type, int protocol, struct socket **res)
1290{
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001291 return __sock_create(&init_net, family, type, protocol, res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292}
1293
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001294SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295{
1296 int retval;
1297 struct socket *sock;
Ulrich Dreppera677a032008-07-23 21:29:17 -07001298 int flags;
1299
Ulrich Dreppere38b36f2008-07-23 21:29:42 -07001300 /* Check the SOCK_* constants for consistency. */
1301 BUILD_BUG_ON(SOCK_CLOEXEC != O_CLOEXEC);
1302 BUILD_BUG_ON((SOCK_MAX | SOCK_TYPE_MASK) != SOCK_TYPE_MASK);
1303 BUILD_BUG_ON(SOCK_CLOEXEC & SOCK_TYPE_MASK);
1304 BUILD_BUG_ON(SOCK_NONBLOCK & SOCK_TYPE_MASK);
1305
Ulrich Dreppera677a032008-07-23 21:29:17 -07001306 flags = type & ~SOCK_TYPE_MASK;
Ulrich Drepper77d27202008-07-23 21:29:35 -07001307 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Dreppera677a032008-07-23 21:29:17 -07001308 return -EINVAL;
1309 type &= SOCK_TYPE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001311 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1312 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1313
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 retval = sock_create(family, type, protocol, &sock);
1315 if (retval < 0)
1316 goto out;
1317
Ulrich Drepper77d27202008-07-23 21:29:35 -07001318 retval = sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 if (retval < 0)
1320 goto out_release;
1321
1322out:
1323 /* It may be already another descriptor 8) Not kernel problem. */
1324 return retval;
1325
1326out_release:
1327 sock_release(sock);
1328 return retval;
1329}
1330
1331/*
1332 * Create a pair of connected sockets.
1333 */
1334
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001335SYSCALL_DEFINE4(socketpair, int, family, int, type, int, protocol,
1336 int __user *, usockvec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337{
1338 struct socket *sock1, *sock2;
1339 int fd1, fd2, err;
Al Virodb349502007-02-07 01:48:00 -05001340 struct file *newfile1, *newfile2;
Ulrich Dreppera677a032008-07-23 21:29:17 -07001341 int flags;
1342
1343 flags = type & ~SOCK_TYPE_MASK;
Ulrich Drepper77d27202008-07-23 21:29:35 -07001344 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Dreppera677a032008-07-23 21:29:17 -07001345 return -EINVAL;
1346 type &= SOCK_TYPE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001348 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1349 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1350
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 /*
1352 * Obtain the first socket and check if the underlying protocol
1353 * supports the socketpair call.
1354 */
1355
1356 err = sock_create(family, type, protocol, &sock1);
1357 if (err < 0)
1358 goto out;
1359
1360 err = sock_create(family, type, protocol, &sock2);
1361 if (err < 0)
1362 goto out_release_1;
1363
1364 err = sock1->ops->socketpair(sock1, sock2);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001365 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 goto out_release_both;
1367
Al Viro7cbe66b2009-08-05 19:59:08 +04001368 fd1 = sock_alloc_file(sock1, &newfile1, flags);
David S. Millerbf3c23d2007-10-29 21:54:02 -07001369 if (unlikely(fd1 < 0)) {
1370 err = fd1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 goto out_release_both;
David S. Millerbf3c23d2007-10-29 21:54:02 -07001372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Al Viro7cbe66b2009-08-05 19:59:08 +04001374 fd2 = sock_alloc_file(sock2, &newfile2, flags);
Al Viro198de4d2009-08-05 19:29:23 +04001375 if (unlikely(fd2 < 0)) {
1376 err = fd2;
1377 fput(newfile1);
1378 put_unused_fd(fd1);
1379 sock_release(sock2);
1380 goto out;
Al Virodb349502007-02-07 01:48:00 -05001381 }
1382
Al Viro157cf642008-12-14 04:57:47 -05001383 audit_fd_pair(fd1, fd2);
Al Virodb349502007-02-07 01:48:00 -05001384 fd_install(fd1, newfile1);
1385 fd_install(fd2, newfile2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 /* fd1 and fd2 may be already another descriptors.
1387 * Not kernel problem.
1388 */
1389
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001390 err = put_user(fd1, &usockvec[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 if (!err)
1392 err = put_user(fd2, &usockvec[1]);
1393 if (!err)
1394 return 0;
1395
1396 sys_close(fd2);
1397 sys_close(fd1);
1398 return err;
1399
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400out_release_both:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001401 sock_release(sock2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402out_release_1:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001403 sock_release(sock1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404out:
1405 return err;
1406}
1407
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408/*
1409 * Bind a name to a socket. Nothing much to do here since it's
1410 * the protocol's responsibility to handle the local address.
1411 *
1412 * We move the socket address to kernel space before we call
1413 * the protocol layer (having also checked the address is ok).
1414 */
1415
Heiko Carstens20f37032009-01-14 14:14:23 +01001416SYSCALL_DEFINE3(bind, int, fd, struct sockaddr __user *, umyaddr, int, addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417{
1418 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001419 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001420 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001422 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001423 if (sock) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001424 err = move_addr_to_kernel(umyaddr, addrlen, (struct sockaddr *)&address);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001425 if (err >= 0) {
1426 err = security_socket_bind(sock,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001427 (struct sockaddr *)&address,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001428 addrlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001429 if (!err)
1430 err = sock->ops->bind(sock,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001431 (struct sockaddr *)
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001432 &address, addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 }
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001434 fput_light(sock->file, fput_needed);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 return err;
1437}
1438
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439/*
1440 * Perform a listen. Basically, we allow the protocol to do anything
1441 * necessary for a listen, and if that works, we mark the socket as
1442 * ready for listening.
1443 */
1444
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001445SYSCALL_DEFINE2(listen, int, fd, int, backlog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446{
1447 struct socket *sock;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001448 int err, fput_needed;
Pavel Emelyanovb8e1f9b2007-12-08 00:12:33 -08001449 int somaxconn;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001450
1451 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1452 if (sock) {
Pavel Emelyanov8efa6e92008-03-31 19:41:14 -07001453 somaxconn = sock_net(sock->sk)->core.sysctl_somaxconn;
Pavel Emelyanovb8e1f9b2007-12-08 00:12:33 -08001454 if ((unsigned)backlog > somaxconn)
1455 backlog = somaxconn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
1457 err = security_socket_listen(sock, backlog);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001458 if (!err)
1459 err = sock->ops->listen(sock, backlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001461 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 }
1463 return err;
1464}
1465
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466/*
1467 * For accept, we attempt to create a new socket, set up the link
1468 * with the client, wake up the client, then return the new
1469 * connected fd. We collect the address of the connector in kernel
1470 * space and move it to user at the very end. This is unclean because
1471 * we open the socket then return an error.
1472 *
1473 * 1003.1g adds the ability to recvmsg() to query connection pending
1474 * status to recvmsg. We need to add that support in a way thats
1475 * clean when we restucture accept also.
1476 */
1477
Heiko Carstens20f37032009-01-14 14:14:23 +01001478SYSCALL_DEFINE4(accept4, int, fd, struct sockaddr __user *, upeer_sockaddr,
1479 int __user *, upeer_addrlen, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480{
1481 struct socket *sock, *newsock;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001482 struct file *newfile;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001483 int err, len, newfd, fput_needed;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001484 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Ulrich Drepper77d27202008-07-23 21:29:35 -07001486 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001487 return -EINVAL;
1488
1489 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1490 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1491
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001492 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 if (!sock)
1494 goto out;
1495
1496 err = -ENFILE;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001497 if (!(newsock = sock_alloc()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 goto out_put;
1499
1500 newsock->type = sock->type;
1501 newsock->ops = sock->ops;
1502
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 /*
1504 * We don't need try_module_get here, as the listening socket (sock)
1505 * has the protocol module (sock->ops->owner) held.
1506 */
1507 __module_get(newsock->ops->owner);
1508
Al Viro7cbe66b2009-08-05 19:59:08 +04001509 newfd = sock_alloc_file(newsock, &newfile, flags);
David S. Miller39d8c1b2006-03-20 17:13:49 -08001510 if (unlikely(newfd < 0)) {
1511 err = newfd;
David S. Miller9a1875e2006-04-01 12:48:36 -08001512 sock_release(newsock);
1513 goto out_put;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001514 }
1515
Frank Filza79af592005-09-27 15:23:38 -07001516 err = security_socket_accept(sock, newsock);
1517 if (err)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001518 goto out_fd;
Frank Filza79af592005-09-27 15:23:38 -07001519
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 err = sock->ops->accept(sock, newsock, sock->file->f_flags);
1521 if (err < 0)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001522 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
1524 if (upeer_sockaddr) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001525 if (newsock->ops->getname(newsock, (struct sockaddr *)&address,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001526 &len, 2) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 err = -ECONNABORTED;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001528 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 }
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001530 err = move_addr_to_user((struct sockaddr *)&address,
1531 len, upeer_sockaddr, upeer_addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 if (err < 0)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001533 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 }
1535
1536 /* File flags are not inherited via accept() unlike another OSes. */
1537
David S. Miller39d8c1b2006-03-20 17:13:49 -08001538 fd_install(newfd, newfile);
1539 err = newfd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001542 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543out:
1544 return err;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001545out_fd:
David S. Miller9606a212006-04-01 01:00:14 -08001546 fput(newfile);
David S. Miller39d8c1b2006-03-20 17:13:49 -08001547 put_unused_fd(newfd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 goto out_put;
1549}
1550
Heiko Carstens20f37032009-01-14 14:14:23 +01001551SYSCALL_DEFINE3(accept, int, fd, struct sockaddr __user *, upeer_sockaddr,
1552 int __user *, upeer_addrlen)
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001553{
Ulrich Drepperde11def2008-11-19 15:36:14 -08001554 return sys_accept4(fd, upeer_sockaddr, upeer_addrlen, 0);
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001555}
1556
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557/*
1558 * Attempt to connect to a socket with the server address. The address
1559 * is in user space so we verify it is OK and move it to kernel space.
1560 *
1561 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
1562 * break bindings
1563 *
1564 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
1565 * other SEQPACKET protocols that take time to connect() as it doesn't
1566 * include the -EINPROGRESS status for such sockets.
1567 */
1568
Heiko Carstens20f37032009-01-14 14:14:23 +01001569SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
1570 int, addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571{
1572 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001573 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001574 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001576 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 if (!sock)
1578 goto out;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001579 err = move_addr_to_kernel(uservaddr, addrlen, (struct sockaddr *)&address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 if (err < 0)
1581 goto out_put;
1582
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001583 err =
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001584 security_socket_connect(sock, (struct sockaddr *)&address, addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 if (err)
1586 goto out_put;
1587
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001588 err = sock->ops->connect(sock, (struct sockaddr *)&address, addrlen,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 sock->file->f_flags);
1590out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001591 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592out:
1593 return err;
1594}
1595
1596/*
1597 * Get the local address ('name') of a socket object. Move the obtained
1598 * name to user space.
1599 */
1600
Heiko Carstens20f37032009-01-14 14:14:23 +01001601SYSCALL_DEFINE3(getsockname, int, fd, struct sockaddr __user *, usockaddr,
1602 int __user *, usockaddr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603{
1604 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001605 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001606 int len, err, fput_needed;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001607
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001608 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 if (!sock)
1610 goto out;
1611
1612 err = security_socket_getsockname(sock);
1613 if (err)
1614 goto out_put;
1615
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001616 err = sock->ops->getname(sock, (struct sockaddr *)&address, &len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 if (err)
1618 goto out_put;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001619 err = move_addr_to_user((struct sockaddr *)&address, len, usockaddr, usockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
1621out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001622 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623out:
1624 return err;
1625}
1626
1627/*
1628 * Get the remote address ('name') of a socket object. Move the obtained
1629 * name to user space.
1630 */
1631
Heiko Carstens20f37032009-01-14 14:14:23 +01001632SYSCALL_DEFINE3(getpeername, int, fd, struct sockaddr __user *, usockaddr,
1633 int __user *, usockaddr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634{
1635 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001636 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001637 int len, err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001639 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1640 if (sock != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 err = security_socket_getpeername(sock);
1642 if (err) {
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001643 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 return err;
1645 }
1646
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001647 err =
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001648 sock->ops->getname(sock, (struct sockaddr *)&address, &len,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001649 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 if (!err)
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001651 err = move_addr_to_user((struct sockaddr *)&address, len, usockaddr,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001652 usockaddr_len);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001653 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 }
1655 return err;
1656}
1657
1658/*
1659 * Send a datagram to a given address. We move the address into kernel
1660 * space and check the user space data area is readable before invoking
1661 * the protocol.
1662 */
1663
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001664SYSCALL_DEFINE6(sendto, int, fd, void __user *, buff, size_t, len,
1665 unsigned, flags, struct sockaddr __user *, addr,
1666 int, addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667{
1668 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001669 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 int err;
1671 struct msghdr msg;
1672 struct iovec iov;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001673 int fput_needed;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001674
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001675 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1676 if (!sock)
David S. Miller4387ff72007-02-08 15:06:08 -08001677 goto out;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001678
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001679 iov.iov_base = buff;
1680 iov.iov_len = len;
1681 msg.msg_name = NULL;
1682 msg.msg_iov = &iov;
1683 msg.msg_iovlen = 1;
1684 msg.msg_control = NULL;
1685 msg.msg_controllen = 0;
1686 msg.msg_namelen = 0;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001687 if (addr) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001688 err = move_addr_to_kernel(addr, addr_len, (struct sockaddr *)&address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 if (err < 0)
1690 goto out_put;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001691 msg.msg_name = (struct sockaddr *)&address;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001692 msg.msg_namelen = addr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 }
1694 if (sock->file->f_flags & O_NONBLOCK)
1695 flags |= MSG_DONTWAIT;
1696 msg.msg_flags = flags;
1697 err = sock_sendmsg(sock, &msg, len);
1698
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001699out_put:
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001700 fput_light(sock->file, fput_needed);
David S. Miller4387ff72007-02-08 15:06:08 -08001701out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 return err;
1703}
1704
1705/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001706 * Send a datagram down a socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 */
1708
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001709SYSCALL_DEFINE4(send, int, fd, void __user *, buff, size_t, len,
1710 unsigned, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711{
1712 return sys_sendto(fd, buff, len, flags, NULL, 0);
1713}
1714
1715/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001716 * Receive a frame from the socket and optionally record the address of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 * sender. We verify the buffers are writable and if needed move the
1718 * sender address from kernel to user space.
1719 */
1720
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001721SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, ubuf, size_t, size,
1722 unsigned, flags, struct sockaddr __user *, addr,
1723 int __user *, addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724{
1725 struct socket *sock;
1726 struct iovec iov;
1727 struct msghdr msg;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001728 struct sockaddr_storage address;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001729 int err, err2;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001730 int fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001732 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 if (!sock)
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001734 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001736 msg.msg_control = NULL;
1737 msg.msg_controllen = 0;
1738 msg.msg_iovlen = 1;
1739 msg.msg_iov = &iov;
1740 iov.iov_len = size;
1741 iov.iov_base = ubuf;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001742 msg.msg_name = (struct sockaddr *)&address;
1743 msg.msg_namelen = sizeof(address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 if (sock->file->f_flags & O_NONBLOCK)
1745 flags |= MSG_DONTWAIT;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001746 err = sock_recvmsg(sock, &msg, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001748 if (err >= 0 && addr != NULL) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001749 err2 = move_addr_to_user((struct sockaddr *)&address,
1750 msg.msg_namelen, addr, addr_len);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001751 if (err2 < 0)
1752 err = err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 }
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001754
1755 fput_light(sock->file, fput_needed);
David S. Miller4387ff72007-02-08 15:06:08 -08001756out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 return err;
1758}
1759
1760/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001761 * Receive a datagram from a socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 */
1763
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001764asmlinkage long sys_recv(int fd, void __user *ubuf, size_t size,
1765 unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766{
1767 return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
1768}
1769
1770/*
1771 * Set a socket option. Because we don't know the option lengths we have
1772 * to pass the user mode parameter for the protocols to sort out.
1773 */
1774
Heiko Carstens20f37032009-01-14 14:14:23 +01001775SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname,
1776 char __user *, optval, int, optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001778 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 struct socket *sock;
1780
1781 if (optlen < 0)
1782 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001783
1784 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1785 if (sock != NULL) {
1786 err = security_socket_setsockopt(sock, level, optname);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001787 if (err)
1788 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
1790 if (level == SOL_SOCKET)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001791 err =
1792 sock_setsockopt(sock, level, optname, optval,
1793 optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 else
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001795 err =
1796 sock->ops->setsockopt(sock, level, optname, optval,
1797 optlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001798out_put:
1799 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 }
1801 return err;
1802}
1803
1804/*
1805 * Get a socket option. Because we don't know the option lengths we have
1806 * to pass a user mode parameter for the protocols to sort out.
1807 */
1808
Heiko Carstens20f37032009-01-14 14:14:23 +01001809SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname,
1810 char __user *, optval, int __user *, optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001812 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 struct socket *sock;
1814
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001815 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1816 if (sock != NULL) {
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001817 err = security_socket_getsockopt(sock, level, optname);
1818 if (err)
1819 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
1821 if (level == SOL_SOCKET)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001822 err =
1823 sock_getsockopt(sock, level, optname, optval,
1824 optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 else
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001826 err =
1827 sock->ops->getsockopt(sock, level, optname, optval,
1828 optlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001829out_put:
1830 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 }
1832 return err;
1833}
1834
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835/*
1836 * Shutdown a socket.
1837 */
1838
Heiko Carstens754fe8d2009-01-14 14:14:09 +01001839SYSCALL_DEFINE2(shutdown, int, fd, int, how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001841 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 struct socket *sock;
1843
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001844 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1845 if (sock != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 err = security_socket_shutdown(sock, how);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001847 if (!err)
1848 err = sock->ops->shutdown(sock, how);
1849 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 }
1851 return err;
1852}
1853
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001854/* A couple of helpful macros for getting the address of the 32/64 bit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 * fields which are the same type (int / unsigned) on our platforms.
1856 */
1857#define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
1858#define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
1859#define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
1860
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861/*
1862 * BSD sendmsg interface
1863 */
1864
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001865SYSCALL_DEFINE3(sendmsg, int, fd, struct msghdr __user *, msg, unsigned, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866{
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001867 struct compat_msghdr __user *msg_compat =
1868 (struct compat_msghdr __user *)msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001870 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
Alex Williamsonb9d717a2005-09-26 14:28:02 -07001872 unsigned char ctl[sizeof(struct cmsghdr) + 20]
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001873 __attribute__ ((aligned(sizeof(__kernel_size_t))));
1874 /* 20 is size of ipv6_pktinfo */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 unsigned char *ctl_buf = ctl;
1876 struct msghdr msg_sys;
1877 int err, ctl_len, iov_size, total_len;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001878 int fput_needed;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001879
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 err = -EFAULT;
1881 if (MSG_CMSG_COMPAT & flags) {
1882 if (get_compat_msghdr(&msg_sys, msg_compat))
1883 return -EFAULT;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001884 }
1885 else if (copy_from_user(&msg_sys, msg, sizeof(struct msghdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 return -EFAULT;
1887
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001888 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001889 if (!sock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 goto out;
1891
1892 /* do not move before msg_sys is valid */
1893 err = -EMSGSIZE;
1894 if (msg_sys.msg_iovlen > UIO_MAXIOV)
1895 goto out_put;
1896
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001897 /* Check whether to allocate the iovec area */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 err = -ENOMEM;
1899 iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
1900 if (msg_sys.msg_iovlen > UIO_FASTIOV) {
1901 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
1902 if (!iov)
1903 goto out_put;
1904 }
1905
1906 /* This will also move the address data into kernel space */
1907 if (MSG_CMSG_COMPAT & flags) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001908 err = verify_compat_iovec(&msg_sys, iov,
1909 (struct sockaddr *)&address,
1910 VERIFY_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 } else
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001912 err = verify_iovec(&msg_sys, iov,
1913 (struct sockaddr *)&address,
1914 VERIFY_READ);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001915 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 goto out_freeiov;
1917 total_len = err;
1918
1919 err = -ENOBUFS;
1920
1921 if (msg_sys.msg_controllen > INT_MAX)
1922 goto out_freeiov;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001923 ctl_len = msg_sys.msg_controllen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001925 err =
1926 cmsghdr_from_user_compat_to_kern(&msg_sys, sock->sk, ctl,
1927 sizeof(ctl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 if (err)
1929 goto out_freeiov;
1930 ctl_buf = msg_sys.msg_control;
Al Viro8920e8f2005-09-07 18:28:51 -07001931 ctl_len = msg_sys.msg_controllen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 } else if (ctl_len) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001933 if (ctl_len > sizeof(ctl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001935 if (ctl_buf == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 goto out_freeiov;
1937 }
1938 err = -EFAULT;
1939 /*
1940 * Careful! Before this, msg_sys.msg_control contains a user pointer.
1941 * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
1942 * checking falls down on this.
1943 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001944 if (copy_from_user(ctl_buf, (void __user *)msg_sys.msg_control,
1945 ctl_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 goto out_freectl;
1947 msg_sys.msg_control = ctl_buf;
1948 }
1949 msg_sys.msg_flags = flags;
1950
1951 if (sock->file->f_flags & O_NONBLOCK)
1952 msg_sys.msg_flags |= MSG_DONTWAIT;
1953 err = sock_sendmsg(sock, &msg_sys, total_len);
1954
1955out_freectl:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001956 if (ctl_buf != ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 sock_kfree_s(sock->sk, ctl_buf, ctl_len);
1958out_freeiov:
1959 if (iov != iovstack)
1960 sock_kfree_s(sock->sk, iov, iov_size);
1961out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001962 fput_light(sock->file, fput_needed);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001963out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 return err;
1965}
1966
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07001967static int __sys_recvmsg(struct socket *sock, struct msghdr __user *msg,
1968 struct msghdr *msg_sys, unsigned flags, int nosec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969{
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001970 struct compat_msghdr __user *msg_compat =
1971 (struct compat_msghdr __user *)msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 struct iovec iovstack[UIO_FASTIOV];
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001973 struct iovec *iov = iovstack;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 unsigned long cmsg_ptr;
1975 int err, iov_size, total_len, len;
1976
1977 /* kernel mode address */
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001978 struct sockaddr_storage addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
1980 /* user mode address pointers */
1981 struct sockaddr __user *uaddr;
1982 int __user *uaddr_len;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001983
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 if (MSG_CMSG_COMPAT & flags) {
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07001985 if (get_compat_msghdr(msg_sys, msg_compat))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 return -EFAULT;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001987 }
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07001988 else if (copy_from_user(msg_sys, msg, sizeof(struct msghdr)))
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001989 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 err = -EMSGSIZE;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07001992 if (msg_sys->msg_iovlen > UIO_MAXIOV)
1993 goto out;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001994
1995 /* Check whether to allocate the iovec area */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 err = -ENOMEM;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07001997 iov_size = msg_sys->msg_iovlen * sizeof(struct iovec);
1998 if (msg_sys->msg_iovlen > UIO_FASTIOV) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
2000 if (!iov)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002001 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 }
2003
2004 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002005 * Save the user-mode address (verify_iovec will change the
2006 * kernel msghdr to use the kernel address space)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002008
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002009 uaddr = (__force void __user *)msg_sys->msg_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 uaddr_len = COMPAT_NAMELEN(msg);
2011 if (MSG_CMSG_COMPAT & flags) {
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002012 err = verify_compat_iovec(msg_sys, iov,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07002013 (struct sockaddr *)&addr,
2014 VERIFY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 } else
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002016 err = verify_iovec(msg_sys, iov,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07002017 (struct sockaddr *)&addr,
2018 VERIFY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 if (err < 0)
2020 goto out_freeiov;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002021 total_len = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002023 cmsg_ptr = (unsigned long)msg_sys->msg_control;
2024 msg_sys->msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002025
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 if (sock->file->f_flags & O_NONBLOCK)
2027 flags |= MSG_DONTWAIT;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002028 err = (nosec ? sock_recvmsg_nosec : sock_recvmsg)(sock, msg_sys,
2029 total_len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 if (err < 0)
2031 goto out_freeiov;
2032 len = err;
2033
2034 if (uaddr != NULL) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07002035 err = move_addr_to_user((struct sockaddr *)&addr,
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002036 msg_sys->msg_namelen, uaddr,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002037 uaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 if (err < 0)
2039 goto out_freeiov;
2040 }
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002041 err = __put_user((msg_sys->msg_flags & ~MSG_CMSG_COMPAT),
David S. Miller37f7f422005-09-16 16:51:01 -07002042 COMPAT_FLAGS(msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 if (err)
2044 goto out_freeiov;
2045 if (MSG_CMSG_COMPAT & flags)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002046 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 &msg_compat->msg_controllen);
2048 else
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002049 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 &msg->msg_controllen);
2051 if (err)
2052 goto out_freeiov;
2053 err = len;
2054
2055out_freeiov:
2056 if (iov != iovstack)
2057 sock_kfree_s(sock->sk, iov, iov_size);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002058out:
2059 return err;
2060}
2061
2062/*
2063 * BSD recvmsg interface
2064 */
2065
2066SYSCALL_DEFINE3(recvmsg, int, fd, struct msghdr __user *, msg,
2067 unsigned int, flags)
2068{
2069 int fput_needed, err;
2070 struct msghdr msg_sys;
2071 struct socket *sock = sockfd_lookup_light(fd, &err, &fput_needed);
2072
2073 if (!sock)
2074 goto out;
2075
2076 err = __sys_recvmsg(sock, msg, &msg_sys, flags, 0);
2077
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002078 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079out:
2080 return err;
2081}
2082
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002083/*
2084 * Linux recvmmsg interface
2085 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002087int __sys_recvmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
2088 unsigned int flags, struct timespec *timeout)
2089{
2090 int fput_needed, err, datagrams;
2091 struct socket *sock;
2092 struct mmsghdr __user *entry;
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002093 struct compat_mmsghdr __user *compat_entry;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002094 struct msghdr msg_sys;
2095 struct timespec end_time;
2096
2097 if (timeout &&
2098 poll_select_set_timeout(&end_time, timeout->tv_sec,
2099 timeout->tv_nsec))
2100 return -EINVAL;
2101
2102 datagrams = 0;
2103
2104 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2105 if (!sock)
2106 return err;
2107
2108 err = sock_error(sock->sk);
2109 if (err)
2110 goto out_put;
2111
2112 entry = mmsg;
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002113 compat_entry = (struct compat_mmsghdr __user *)mmsg;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002114
2115 while (datagrams < vlen) {
2116 /*
2117 * No need to ask LSM for more than the first datagram.
2118 */
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002119 if (MSG_CMSG_COMPAT & flags) {
2120 err = __sys_recvmsg(sock, (struct msghdr __user *)compat_entry,
2121 &msg_sys, flags, datagrams);
2122 if (err < 0)
2123 break;
2124 err = __put_user(err, &compat_entry->msg_len);
2125 ++compat_entry;
2126 } else {
2127 err = __sys_recvmsg(sock, (struct msghdr __user *)entry,
2128 &msg_sys, flags, datagrams);
2129 if (err < 0)
2130 break;
2131 err = put_user(err, &entry->msg_len);
2132 ++entry;
2133 }
2134
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002135 if (err)
2136 break;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002137 ++datagrams;
2138
Brandon L Black71c5c152010-03-26 16:18:03 +00002139 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
2140 if (flags & MSG_WAITFORONE)
2141 flags |= MSG_DONTWAIT;
2142
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002143 if (timeout) {
2144 ktime_get_ts(timeout);
2145 *timeout = timespec_sub(end_time, *timeout);
2146 if (timeout->tv_sec < 0) {
2147 timeout->tv_sec = timeout->tv_nsec = 0;
2148 break;
2149 }
2150
2151 /* Timeout, return less than vlen datagrams */
2152 if (timeout->tv_nsec == 0 && timeout->tv_sec == 0)
2153 break;
2154 }
2155
2156 /* Out of band data, return right away */
2157 if (msg_sys.msg_flags & MSG_OOB)
2158 break;
2159 }
2160
2161out_put:
2162 fput_light(sock->file, fput_needed);
2163
2164 if (err == 0)
2165 return datagrams;
2166
2167 if (datagrams != 0) {
2168 /*
2169 * We may return less entries than requested (vlen) if the
2170 * sock is non block and there aren't enough datagrams...
2171 */
2172 if (err != -EAGAIN) {
2173 /*
2174 * ... or if recvmsg returns an error after we
2175 * received some datagrams, where we record the
2176 * error to return on the next call or if the
2177 * app asks about it using getsockopt(SO_ERROR).
2178 */
2179 sock->sk->sk_err = -err;
2180 }
2181
2182 return datagrams;
2183 }
2184
2185 return err;
2186}
2187
2188SYSCALL_DEFINE5(recvmmsg, int, fd, struct mmsghdr __user *, mmsg,
2189 unsigned int, vlen, unsigned int, flags,
2190 struct timespec __user *, timeout)
2191{
2192 int datagrams;
2193 struct timespec timeout_sys;
2194
2195 if (!timeout)
2196 return __sys_recvmmsg(fd, mmsg, vlen, flags, NULL);
2197
2198 if (copy_from_user(&timeout_sys, timeout, sizeof(timeout_sys)))
2199 return -EFAULT;
2200
2201 datagrams = __sys_recvmmsg(fd, mmsg, vlen, flags, &timeout_sys);
2202
2203 if (datagrams > 0 &&
2204 copy_to_user(timeout, &timeout_sys, sizeof(timeout_sys)))
2205 datagrams = -EFAULT;
2206
2207 return datagrams;
2208}
2209
2210#ifdef __ARCH_WANT_SYS_SOCKETCALL
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211/* Argument list sizes for sys_socketcall */
2212#define AL(x) ((x) * sizeof(unsigned long))
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002213static const unsigned char nargs[20] = {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002214 AL(0),AL(3),AL(3),AL(3),AL(2),AL(3),
2215 AL(3),AL(3),AL(4),AL(4),AL(4),AL(6),
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07002216 AL(6),AL(2),AL(5),AL(5),AL(3),AL(3),
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002217 AL(4),AL(5)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002218};
2219
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220#undef AL
2221
2222/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002223 * System call vectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 *
2225 * Argument checking cleaned up. Saved 20% in size.
2226 * This function doesn't need to set the kernel lock because
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002227 * it is set by the callees.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 */
2229
Heiko Carstens3e0fa652009-01-14 14:14:24 +01002230SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231{
2232 unsigned long a[6];
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002233 unsigned long a0, a1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 int err;
Arjan van de Ven47379052009-09-28 12:57:44 -07002235 unsigned int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002237 if (call < 1 || call > SYS_RECVMMSG)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 return -EINVAL;
2239
Arjan van de Ven47379052009-09-28 12:57:44 -07002240 len = nargs[call];
2241 if (len > sizeof(a))
2242 return -EINVAL;
2243
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 /* copy_from_user should be SMP safe. */
Arjan van de Ven47379052009-09-28 12:57:44 -07002245 if (copy_from_user(a, args, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 return -EFAULT;
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002247
Al Virof3298dc2008-12-10 03:16:51 -05002248 audit_socketcall(nargs[call] / sizeof(unsigned long), a);
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002249
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002250 a0 = a[0];
2251 a1 = a[1];
2252
2253 switch (call) {
2254 case SYS_SOCKET:
2255 err = sys_socket(a0, a1, a[2]);
2256 break;
2257 case SYS_BIND:
2258 err = sys_bind(a0, (struct sockaddr __user *)a1, a[2]);
2259 break;
2260 case SYS_CONNECT:
2261 err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
2262 break;
2263 case SYS_LISTEN:
2264 err = sys_listen(a0, a1);
2265 break;
2266 case SYS_ACCEPT:
Ulrich Drepperde11def2008-11-19 15:36:14 -08002267 err = sys_accept4(a0, (struct sockaddr __user *)a1,
2268 (int __user *)a[2], 0);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002269 break;
2270 case SYS_GETSOCKNAME:
2271 err =
2272 sys_getsockname(a0, (struct sockaddr __user *)a1,
2273 (int __user *)a[2]);
2274 break;
2275 case SYS_GETPEERNAME:
2276 err =
2277 sys_getpeername(a0, (struct sockaddr __user *)a1,
2278 (int __user *)a[2]);
2279 break;
2280 case SYS_SOCKETPAIR:
2281 err = sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
2282 break;
2283 case SYS_SEND:
2284 err = sys_send(a0, (void __user *)a1, a[2], a[3]);
2285 break;
2286 case SYS_SENDTO:
2287 err = sys_sendto(a0, (void __user *)a1, a[2], a[3],
2288 (struct sockaddr __user *)a[4], a[5]);
2289 break;
2290 case SYS_RECV:
2291 err = sys_recv(a0, (void __user *)a1, a[2], a[3]);
2292 break;
2293 case SYS_RECVFROM:
2294 err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
2295 (struct sockaddr __user *)a[4],
2296 (int __user *)a[5]);
2297 break;
2298 case SYS_SHUTDOWN:
2299 err = sys_shutdown(a0, a1);
2300 break;
2301 case SYS_SETSOCKOPT:
2302 err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]);
2303 break;
2304 case SYS_GETSOCKOPT:
2305 err =
2306 sys_getsockopt(a0, a1, a[2], (char __user *)a[3],
2307 (int __user *)a[4]);
2308 break;
2309 case SYS_SENDMSG:
2310 err = sys_sendmsg(a0, (struct msghdr __user *)a1, a[2]);
2311 break;
2312 case SYS_RECVMSG:
2313 err = sys_recvmsg(a0, (struct msghdr __user *)a1, a[2]);
2314 break;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002315 case SYS_RECVMMSG:
2316 err = sys_recvmmsg(a0, (struct mmsghdr __user *)a1, a[2], a[3],
2317 (struct timespec __user *)a[4]);
2318 break;
Ulrich Drepperde11def2008-11-19 15:36:14 -08002319 case SYS_ACCEPT4:
2320 err = sys_accept4(a0, (struct sockaddr __user *)a1,
2321 (int __user *)a[2], a[3]);
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07002322 break;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002323 default:
2324 err = -EINVAL;
2325 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 }
2327 return err;
2328}
2329
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002330#endif /* __ARCH_WANT_SYS_SOCKETCALL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002332/**
2333 * sock_register - add a socket protocol handler
2334 * @ops: description of protocol
2335 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 * This function is called by a protocol handler that wants to
2337 * advertise its address family, and have it linked into the
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002338 * socket interface. The value ops->family coresponds to the
2339 * socket system call protocol family.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 */
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002341int sock_register(const struct net_proto_family *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342{
2343 int err;
2344
2345 if (ops->family >= NPROTO) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002346 printk(KERN_CRIT "protocol %d >= NPROTO(%d)\n", ops->family,
2347 NPROTO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 return -ENOBUFS;
2349 }
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002350
2351 spin_lock(&net_family_lock);
2352 if (net_families[ops->family])
2353 err = -EEXIST;
2354 else {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002355 net_families[ops->family] = ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 err = 0;
2357 }
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002358 spin_unlock(&net_family_lock);
2359
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002360 printk(KERN_INFO "NET: Registered protocol family %d\n", ops->family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 return err;
2362}
2363
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002364/**
2365 * sock_unregister - remove a protocol handler
2366 * @family: protocol family to remove
2367 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 * This function is called by a protocol handler that wants to
2369 * remove its address family, and have it unlinked from the
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002370 * new socket creation.
2371 *
2372 * If protocol handler is a module, then it can use module reference
2373 * counts to protect against new references. If protocol handler is not
2374 * a module then it needs to provide its own protection in
2375 * the ops->create routine.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 */
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002377void sock_unregister(int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378{
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002379 BUG_ON(family < 0 || family >= NPROTO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002381 spin_lock(&net_family_lock);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002382 net_families[family] = NULL;
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002383 spin_unlock(&net_family_lock);
2384
2385 synchronize_rcu();
2386
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002387 printk(KERN_INFO "NET: Unregistered protocol family %d\n", family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388}
2389
Andi Kleen77d76ea2005-12-22 12:43:42 -08002390static int __init sock_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391{
2392 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002393 * Initialize sock SLAB cache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002395
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 sk_init();
2397
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002399 * Initialize skbuff SLAB cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 */
2401 skb_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402
2403 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002404 * Initialize the protocols module.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 */
2406
2407 init_inodecache();
2408 register_filesystem(&sock_fs_type);
2409 sock_mnt = kern_mount(&sock_fs_type);
Andi Kleen77d76ea2005-12-22 12:43:42 -08002410
2411 /* The real protocol initialization is performed in later initcalls.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 */
2413
2414#ifdef CONFIG_NETFILTER
2415 netfilter_init();
2416#endif
David S. Millercbeb3212005-12-22 12:58:55 -08002417
2418 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419}
2420
Andi Kleen77d76ea2005-12-22 12:43:42 -08002421core_initcall(sock_init); /* early initcall */
2422
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423#ifdef CONFIG_PROC_FS
2424void socket_seq_show(struct seq_file *seq)
2425{
2426 int cpu;
2427 int counter = 0;
2428
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -07002429 for_each_possible_cpu(cpu)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002430 counter += per_cpu(sockets_in_use, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431
2432 /* It can be negative, by the way. 8) */
2433 if (counter < 0)
2434 counter = 0;
2435
2436 seq_printf(seq, "sockets: used %d\n", counter);
2437}
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002438#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
Shaun Pereira89bbfc92006-03-21 23:58:08 -08002440#ifdef CONFIG_COMPAT
Arnd Bergmann6b960182009-11-06 23:10:54 -08002441static int do_siocgstamp(struct net *net, struct socket *sock,
2442 unsigned int cmd, struct compat_timeval __user *up)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002443{
Arnd Bergmann7a229382009-11-06 23:00:29 -08002444 mm_segment_t old_fs = get_fs();
2445 struct timeval ktv;
2446 int err;
2447
2448 set_fs(KERNEL_DS);
Arnd Bergmann6b960182009-11-06 23:10:54 -08002449 err = sock_do_ioctl(net, sock, cmd, (unsigned long)&ktv);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002450 set_fs(old_fs);
2451 if (!err) {
2452 err = put_user(ktv.tv_sec, &up->tv_sec);
2453 err |= __put_user(ktv.tv_usec, &up->tv_usec);
2454 }
2455 return err;
2456}
2457
Arnd Bergmann6b960182009-11-06 23:10:54 -08002458static int do_siocgstampns(struct net *net, struct socket *sock,
2459 unsigned int cmd, struct compat_timespec __user *up)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002460{
Arnd Bergmann7a229382009-11-06 23:00:29 -08002461 mm_segment_t old_fs = get_fs();
2462 struct timespec kts;
2463 int err;
2464
2465 set_fs(KERNEL_DS);
Arnd Bergmann6b960182009-11-06 23:10:54 -08002466 err = sock_do_ioctl(net, sock, cmd, (unsigned long)&kts);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002467 set_fs(old_fs);
2468 if (!err) {
2469 err = put_user(kts.tv_sec, &up->tv_sec);
2470 err |= __put_user(kts.tv_nsec, &up->tv_nsec);
2471 }
2472 return err;
2473}
2474
Arnd Bergmann6b960182009-11-06 23:10:54 -08002475static int dev_ifname32(struct net *net, struct compat_ifreq __user *uifr32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002476{
2477 struct ifreq __user *uifr;
2478 int err;
2479
2480 uifr = compat_alloc_user_space(sizeof(struct ifreq));
Arnd Bergmann6b960182009-11-06 23:10:54 -08002481 if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002482 return -EFAULT;
2483
Arnd Bergmann6b960182009-11-06 23:10:54 -08002484 err = dev_ioctl(net, SIOCGIFNAME, uifr);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002485 if (err)
2486 return err;
2487
Arnd Bergmann6b960182009-11-06 23:10:54 -08002488 if (copy_in_user(uifr32, uifr, sizeof(struct compat_ifreq)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002489 return -EFAULT;
2490
2491 return 0;
2492}
2493
Arnd Bergmann6b960182009-11-06 23:10:54 -08002494static int dev_ifconf(struct net *net, struct compat_ifconf __user *uifc32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002495{
Arnd Bergmann6b960182009-11-06 23:10:54 -08002496 struct compat_ifconf ifc32;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002497 struct ifconf ifc;
2498 struct ifconf __user *uifc;
Arnd Bergmann6b960182009-11-06 23:10:54 -08002499 struct compat_ifreq __user *ifr32;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002500 struct ifreq __user *ifr;
2501 unsigned int i, j;
2502 int err;
2503
Arnd Bergmann6b960182009-11-06 23:10:54 -08002504 if (copy_from_user(&ifc32, uifc32, sizeof(struct compat_ifconf)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002505 return -EFAULT;
2506
2507 if (ifc32.ifcbuf == 0) {
2508 ifc32.ifc_len = 0;
2509 ifc.ifc_len = 0;
2510 ifc.ifc_req = NULL;
2511 uifc = compat_alloc_user_space(sizeof(struct ifconf));
2512 } else {
Arnd Bergmann6b960182009-11-06 23:10:54 -08002513 size_t len =((ifc32.ifc_len / sizeof (struct compat_ifreq)) + 1) *
Arnd Bergmann7a229382009-11-06 23:00:29 -08002514 sizeof (struct ifreq);
2515 uifc = compat_alloc_user_space(sizeof(struct ifconf) + len);
2516 ifc.ifc_len = len;
2517 ifr = ifc.ifc_req = (void __user *)(uifc + 1);
2518 ifr32 = compat_ptr(ifc32.ifcbuf);
Arnd Bergmann6b960182009-11-06 23:10:54 -08002519 for (i = 0; i < ifc32.ifc_len; i += sizeof (struct compat_ifreq)) {
2520 if (copy_in_user(ifr, ifr32, sizeof(struct compat_ifreq)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002521 return -EFAULT;
2522 ifr++;
2523 ifr32++;
2524 }
2525 }
2526 if (copy_to_user(uifc, &ifc, sizeof(struct ifconf)))
2527 return -EFAULT;
2528
Arnd Bergmann6b960182009-11-06 23:10:54 -08002529 err = dev_ioctl(net, SIOCGIFCONF, uifc);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002530 if (err)
2531 return err;
2532
2533 if (copy_from_user(&ifc, uifc, sizeof(struct ifconf)))
2534 return -EFAULT;
2535
2536 ifr = ifc.ifc_req;
2537 ifr32 = compat_ptr(ifc32.ifcbuf);
2538 for (i = 0, j = 0;
Arnd Bergmann6b960182009-11-06 23:10:54 -08002539 i + sizeof (struct compat_ifreq) <= ifc32.ifc_len && j < ifc.ifc_len;
2540 i += sizeof (struct compat_ifreq), j += sizeof (struct ifreq)) {
2541 if (copy_in_user(ifr32, ifr, sizeof (struct compat_ifreq)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002542 return -EFAULT;
2543 ifr32++;
2544 ifr++;
2545 }
2546
2547 if (ifc32.ifcbuf == 0) {
2548 /* Translate from 64-bit structure multiple to
2549 * a 32-bit one.
2550 */
2551 i = ifc.ifc_len;
Arnd Bergmann6b960182009-11-06 23:10:54 -08002552 i = ((i / sizeof(struct ifreq)) * sizeof(struct compat_ifreq));
Arnd Bergmann7a229382009-11-06 23:00:29 -08002553 ifc32.ifc_len = i;
2554 } else {
2555 ifc32.ifc_len = i;
2556 }
Arnd Bergmann6b960182009-11-06 23:10:54 -08002557 if (copy_to_user(uifc32, &ifc32, sizeof(struct compat_ifconf)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002558 return -EFAULT;
2559
2560 return 0;
2561}
2562
Arnd Bergmann6b960182009-11-06 23:10:54 -08002563static int ethtool_ioctl(struct net *net, struct compat_ifreq __user *ifr32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002564{
2565 struct ifreq __user *ifr;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002566 u32 data;
2567 void __user *datap;
2568
2569 ifr = compat_alloc_user_space(sizeof(*ifr));
Arnd Bergmann7a229382009-11-06 23:00:29 -08002570
2571 if (copy_in_user(&ifr->ifr_name, &ifr32->ifr_name, IFNAMSIZ))
2572 return -EFAULT;
2573
2574 if (get_user(data, &ifr32->ifr_ifru.ifru_data))
2575 return -EFAULT;
2576
2577 datap = compat_ptr(data);
2578 if (put_user(datap, &ifr->ifr_ifru.ifru_data))
2579 return -EFAULT;
2580
Arnd Bergmann6b960182009-11-06 23:10:54 -08002581 return dev_ioctl(net, SIOCETHTOOL, ifr);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002582}
2583
Arnd Bergmann7a50a242009-11-08 20:57:03 -08002584static int compat_siocwandev(struct net *net, struct compat_ifreq __user *uifr32)
2585{
2586 void __user *uptr;
2587 compat_uptr_t uptr32;
2588 struct ifreq __user *uifr;
2589
2590 uifr = compat_alloc_user_space(sizeof (*uifr));
2591 if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
2592 return -EFAULT;
2593
2594 if (get_user(uptr32, &uifr32->ifr_settings.ifs_ifsu))
2595 return -EFAULT;
2596
2597 uptr = compat_ptr(uptr32);
2598
2599 if (put_user(uptr, &uifr->ifr_settings.ifs_ifsu.raw_hdlc))
2600 return -EFAULT;
2601
2602 return dev_ioctl(net, SIOCWANDEV, uifr);
2603}
2604
Arnd Bergmann6b960182009-11-06 23:10:54 -08002605static int bond_ioctl(struct net *net, unsigned int cmd,
2606 struct compat_ifreq __user *ifr32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002607{
2608 struct ifreq kifr;
2609 struct ifreq __user *uifr;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002610 mm_segment_t old_fs;
2611 int err;
2612 u32 data;
2613 void __user *datap;
2614
2615 switch (cmd) {
2616 case SIOCBONDENSLAVE:
2617 case SIOCBONDRELEASE:
2618 case SIOCBONDSETHWADDR:
2619 case SIOCBONDCHANGEACTIVE:
Arnd Bergmann6b960182009-11-06 23:10:54 -08002620 if (copy_from_user(&kifr, ifr32, sizeof(struct compat_ifreq)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002621 return -EFAULT;
2622
2623 old_fs = get_fs();
2624 set_fs (KERNEL_DS);
Arnd Bergmann6b960182009-11-06 23:10:54 -08002625 err = dev_ioctl(net, cmd, &kifr);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002626 set_fs (old_fs);
2627
2628 return err;
2629 case SIOCBONDSLAVEINFOQUERY:
2630 case SIOCBONDINFOQUERY:
2631 uifr = compat_alloc_user_space(sizeof(*uifr));
2632 if (copy_in_user(&uifr->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, &uifr->ifr_ifru.ifru_data))
2640 return -EFAULT;
2641
Arnd Bergmann6b960182009-11-06 23:10:54 -08002642 return dev_ioctl(net, cmd, uifr);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002643 default:
2644 return -EINVAL;
2645 };
2646}
2647
Arnd Bergmann6b960182009-11-06 23:10:54 -08002648static int siocdevprivate_ioctl(struct net *net, unsigned int cmd,
2649 struct compat_ifreq __user *u_ifreq32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002650{
2651 struct ifreq __user *u_ifreq64;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002652 char tmp_buf[IFNAMSIZ];
2653 void __user *data64;
2654 u32 data32;
2655
2656 if (copy_from_user(&tmp_buf[0], &(u_ifreq32->ifr_ifrn.ifrn_name[0]),
2657 IFNAMSIZ))
2658 return -EFAULT;
2659 if (__get_user(data32, &u_ifreq32->ifr_ifru.ifru_data))
2660 return -EFAULT;
2661 data64 = compat_ptr(data32);
2662
2663 u_ifreq64 = compat_alloc_user_space(sizeof(*u_ifreq64));
2664
2665 /* Don't check these user accesses, just let that get trapped
2666 * in the ioctl handler instead.
2667 */
2668 if (copy_to_user(&u_ifreq64->ifr_ifrn.ifrn_name[0], &tmp_buf[0],
2669 IFNAMSIZ))
2670 return -EFAULT;
2671 if (__put_user(data64, &u_ifreq64->ifr_ifru.ifru_data))
2672 return -EFAULT;
2673
Arnd Bergmann6b960182009-11-06 23:10:54 -08002674 return dev_ioctl(net, cmd, u_ifreq64);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002675}
2676
Arnd Bergmann6b960182009-11-06 23:10:54 -08002677static int dev_ifsioc(struct net *net, struct socket *sock,
2678 unsigned int cmd, struct compat_ifreq __user *uifr32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002679{
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00002680 struct ifreq __user *uifr;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002681 int err;
2682
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00002683 uifr = compat_alloc_user_space(sizeof(*uifr));
2684 if (copy_in_user(uifr, uifr32, sizeof(*uifr32)))
2685 return -EFAULT;
2686
2687 err = sock_do_ioctl(net, sock, cmd, (unsigned long)uifr);
2688
Arnd Bergmann7a229382009-11-06 23:00:29 -08002689 if (!err) {
2690 switch (cmd) {
2691 case SIOCGIFFLAGS:
2692 case SIOCGIFMETRIC:
2693 case SIOCGIFMTU:
2694 case SIOCGIFMEM:
2695 case SIOCGIFHWADDR:
2696 case SIOCGIFINDEX:
2697 case SIOCGIFADDR:
2698 case SIOCGIFBRDADDR:
2699 case SIOCGIFDSTADDR:
2700 case SIOCGIFNETMASK:
Arnd Bergmannfab25322009-11-08 20:56:21 -08002701 case SIOCGIFPFLAGS:
Arnd Bergmann7a229382009-11-06 23:00:29 -08002702 case SIOCGIFTXQLEN:
Arnd Bergmannfab25322009-11-08 20:56:21 -08002703 case SIOCGMIIPHY:
2704 case SIOCGMIIREG:
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00002705 if (copy_in_user(uifr32, uifr, sizeof(*uifr32)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002706 err = -EFAULT;
2707 break;
2708 }
2709 }
2710 return err;
2711}
2712
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00002713static int compat_sioc_ifmap(struct net *net, unsigned int cmd,
2714 struct compat_ifreq __user *uifr32)
2715{
2716 struct ifreq ifr;
2717 struct compat_ifmap __user *uifmap32;
2718 mm_segment_t old_fs;
2719 int err;
2720
2721 uifmap32 = &uifr32->ifr_ifru.ifru_map;
2722 err = copy_from_user(&ifr, uifr32, sizeof(ifr.ifr_name));
2723 err |= __get_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
2724 err |= __get_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
2725 err |= __get_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
2726 err |= __get_user(ifr.ifr_map.irq, &uifmap32->irq);
2727 err |= __get_user(ifr.ifr_map.dma, &uifmap32->dma);
2728 err |= __get_user(ifr.ifr_map.port, &uifmap32->port);
2729 if (err)
2730 return -EFAULT;
2731
2732 old_fs = get_fs();
2733 set_fs (KERNEL_DS);
2734 err = dev_ioctl(net, cmd, (void __user *)&ifr);
2735 set_fs (old_fs);
2736
2737 if (cmd == SIOCGIFMAP && !err) {
2738 err = copy_to_user(uifr32, &ifr, sizeof(ifr.ifr_name));
2739 err |= __put_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
2740 err |= __put_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
2741 err |= __put_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
2742 err |= __put_user(ifr.ifr_map.irq, &uifmap32->irq);
2743 err |= __put_user(ifr.ifr_map.dma, &uifmap32->dma);
2744 err |= __put_user(ifr.ifr_map.port, &uifmap32->port);
2745 if (err)
2746 err = -EFAULT;
2747 }
2748 return err;
2749}
2750
2751static int compat_siocshwtstamp(struct net *net, struct compat_ifreq __user *uifr32)
2752{
2753 void __user *uptr;
2754 compat_uptr_t uptr32;
2755 struct ifreq __user *uifr;
2756
2757 uifr = compat_alloc_user_space(sizeof (*uifr));
2758 if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
2759 return -EFAULT;
2760
2761 if (get_user(uptr32, &uifr32->ifr_data))
2762 return -EFAULT;
2763
2764 uptr = compat_ptr(uptr32);
2765
2766 if (put_user(uptr, &uifr->ifr_data))
2767 return -EFAULT;
2768
2769 return dev_ioctl(net, SIOCSHWTSTAMP, uifr);
2770}
2771
Arnd Bergmann7a229382009-11-06 23:00:29 -08002772struct rtentry32 {
2773 u32 rt_pad1;
2774 struct sockaddr rt_dst; /* target address */
2775 struct sockaddr rt_gateway; /* gateway addr (RTF_GATEWAY) */
2776 struct sockaddr rt_genmask; /* target network mask (IP) */
2777 unsigned short rt_flags;
2778 short rt_pad2;
2779 u32 rt_pad3;
2780 unsigned char rt_tos;
2781 unsigned char rt_class;
2782 short rt_pad4;
2783 short rt_metric; /* +1 for binary compatibility! */
2784 /* char * */ u32 rt_dev; /* forcing the device at add */
2785 u32 rt_mtu; /* per route MTU/Window */
2786 u32 rt_window; /* Window clamping */
2787 unsigned short rt_irtt; /* Initial RTT */
2788};
2789
2790struct in6_rtmsg32 {
2791 struct in6_addr rtmsg_dst;
2792 struct in6_addr rtmsg_src;
2793 struct in6_addr rtmsg_gateway;
2794 u32 rtmsg_type;
2795 u16 rtmsg_dst_len;
2796 u16 rtmsg_src_len;
2797 u32 rtmsg_metric;
2798 u32 rtmsg_info;
2799 u32 rtmsg_flags;
2800 s32 rtmsg_ifindex;
2801};
2802
Arnd Bergmann6b960182009-11-06 23:10:54 -08002803static int routing_ioctl(struct net *net, struct socket *sock,
2804 unsigned int cmd, void __user *argp)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002805{
2806 int ret;
2807 void *r = NULL;
2808 struct in6_rtmsg r6;
2809 struct rtentry r4;
2810 char devname[16];
2811 u32 rtdev;
2812 mm_segment_t old_fs = get_fs();
2813
Arnd Bergmann6b960182009-11-06 23:10:54 -08002814 if (sock && sock->sk && sock->sk->sk_family == AF_INET6) { /* ipv6 */
2815 struct in6_rtmsg32 __user *ur6 = argp;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002816 ret = copy_from_user (&r6.rtmsg_dst, &(ur6->rtmsg_dst),
2817 3 * sizeof(struct in6_addr));
2818 ret |= __get_user (r6.rtmsg_type, &(ur6->rtmsg_type));
2819 ret |= __get_user (r6.rtmsg_dst_len, &(ur6->rtmsg_dst_len));
2820 ret |= __get_user (r6.rtmsg_src_len, &(ur6->rtmsg_src_len));
2821 ret |= __get_user (r6.rtmsg_metric, &(ur6->rtmsg_metric));
2822 ret |= __get_user (r6.rtmsg_info, &(ur6->rtmsg_info));
2823 ret |= __get_user (r6.rtmsg_flags, &(ur6->rtmsg_flags));
2824 ret |= __get_user (r6.rtmsg_ifindex, &(ur6->rtmsg_ifindex));
2825
2826 r = (void *) &r6;
2827 } else { /* ipv4 */
Arnd Bergmann6b960182009-11-06 23:10:54 -08002828 struct rtentry32 __user *ur4 = argp;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002829 ret = copy_from_user (&r4.rt_dst, &(ur4->rt_dst),
2830 3 * sizeof(struct sockaddr));
2831 ret |= __get_user (r4.rt_flags, &(ur4->rt_flags));
2832 ret |= __get_user (r4.rt_metric, &(ur4->rt_metric));
2833 ret |= __get_user (r4.rt_mtu, &(ur4->rt_mtu));
2834 ret |= __get_user (r4.rt_window, &(ur4->rt_window));
2835 ret |= __get_user (r4.rt_irtt, &(ur4->rt_irtt));
2836 ret |= __get_user (rtdev, &(ur4->rt_dev));
2837 if (rtdev) {
2838 ret |= copy_from_user (devname, compat_ptr(rtdev), 15);
2839 r4.rt_dev = devname; devname[15] = 0;
2840 } else
2841 r4.rt_dev = NULL;
2842
2843 r = (void *) &r4;
2844 }
2845
2846 if (ret) {
2847 ret = -EFAULT;
2848 goto out;
2849 }
2850
2851 set_fs (KERNEL_DS);
Arnd Bergmann6b960182009-11-06 23:10:54 -08002852 ret = sock_do_ioctl(net, sock, cmd, (unsigned long) r);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002853 set_fs (old_fs);
2854
2855out:
Arnd Bergmann7a229382009-11-06 23:00:29 -08002856 return ret;
2857}
2858
2859/* Since old style bridge ioctl's endup using SIOCDEVPRIVATE
2860 * for some operations; this forces use of the newer bridge-utils that
2861 * use compatiable ioctls
2862 */
Arnd Bergmann6b960182009-11-06 23:10:54 -08002863static int old_bridge_ioctl(compat_ulong_t __user *argp)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002864{
Arnd Bergmann6b960182009-11-06 23:10:54 -08002865 compat_ulong_t tmp;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002866
Arnd Bergmann6b960182009-11-06 23:10:54 -08002867 if (get_user(tmp, argp))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002868 return -EFAULT;
2869 if (tmp == BRCTL_GET_VERSION)
2870 return BRCTL_VERSION + 1;
2871 return -EINVAL;
2872}
2873
Arnd Bergmann6b960182009-11-06 23:10:54 -08002874static int compat_sock_ioctl_trans(struct file *file, struct socket *sock,
2875 unsigned int cmd, unsigned long arg)
2876{
2877 void __user *argp = compat_ptr(arg);
2878 struct sock *sk = sock->sk;
2879 struct net *net = sock_net(sk);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002880
Arnd Bergmann6b960182009-11-06 23:10:54 -08002881 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15))
2882 return siocdevprivate_ioctl(net, cmd, argp);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002883
Arnd Bergmann6b960182009-11-06 23:10:54 -08002884 switch (cmd) {
2885 case SIOCSIFBR:
2886 case SIOCGIFBR:
2887 return old_bridge_ioctl(argp);
2888 case SIOCGIFNAME:
2889 return dev_ifname32(net, argp);
2890 case SIOCGIFCONF:
2891 return dev_ifconf(net, argp);
2892 case SIOCETHTOOL:
2893 return ethtool_ioctl(net, argp);
Arnd Bergmann7a50a242009-11-08 20:57:03 -08002894 case SIOCWANDEV:
2895 return compat_siocwandev(net, argp);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00002896 case SIOCGIFMAP:
2897 case SIOCSIFMAP:
2898 return compat_sioc_ifmap(net, cmd, argp);
Arnd Bergmann6b960182009-11-06 23:10:54 -08002899 case SIOCBONDENSLAVE:
2900 case SIOCBONDRELEASE:
2901 case SIOCBONDSETHWADDR:
2902 case SIOCBONDSLAVEINFOQUERY:
2903 case SIOCBONDINFOQUERY:
2904 case SIOCBONDCHANGEACTIVE:
2905 return bond_ioctl(net, cmd, argp);
2906 case SIOCADDRT:
2907 case SIOCDELRT:
2908 return routing_ioctl(net, sock, cmd, argp);
2909 case SIOCGSTAMP:
2910 return do_siocgstamp(net, sock, cmd, argp);
2911 case SIOCGSTAMPNS:
2912 return do_siocgstampns(net, sock, cmd, argp);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00002913 case SIOCSHWTSTAMP:
2914 return compat_siocshwtstamp(net, argp);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002915
Arnd Bergmann6b960182009-11-06 23:10:54 -08002916 case FIOSETOWN:
2917 case SIOCSPGRP:
2918 case FIOGETOWN:
2919 case SIOCGPGRP:
2920 case SIOCBRADDBR:
2921 case SIOCBRDELBR:
2922 case SIOCGIFVLAN:
2923 case SIOCSIFVLAN:
2924 case SIOCADDDLCI:
2925 case SIOCDELDLCI:
2926 return sock_ioctl(file, cmd, arg);
2927
2928 case SIOCGIFFLAGS:
2929 case SIOCSIFFLAGS:
2930 case SIOCGIFMETRIC:
2931 case SIOCSIFMETRIC:
2932 case SIOCGIFMTU:
2933 case SIOCSIFMTU:
2934 case SIOCGIFMEM:
2935 case SIOCSIFMEM:
2936 case SIOCGIFHWADDR:
2937 case SIOCSIFHWADDR:
2938 case SIOCADDMULTI:
2939 case SIOCDELMULTI:
2940 case SIOCGIFINDEX:
Arnd Bergmann6b960182009-11-06 23:10:54 -08002941 case SIOCGIFADDR:
2942 case SIOCSIFADDR:
2943 case SIOCSIFHWBROADCAST:
Arnd Bergmann6b960182009-11-06 23:10:54 -08002944 case SIOCDIFADDR:
Arnd Bergmann6b960182009-11-06 23:10:54 -08002945 case SIOCGIFBRDADDR:
2946 case SIOCSIFBRDADDR:
2947 case SIOCGIFDSTADDR:
2948 case SIOCSIFDSTADDR:
2949 case SIOCGIFNETMASK:
2950 case SIOCSIFNETMASK:
2951 case SIOCSIFPFLAGS:
2952 case SIOCGIFPFLAGS:
2953 case SIOCGIFTXQLEN:
2954 case SIOCSIFTXQLEN:
2955 case SIOCBRADDIF:
2956 case SIOCBRDELIF:
Arnd Bergmann9177efd2009-11-06 08:09:09 +00002957 case SIOCSIFNAME:
2958 case SIOCGMIIPHY:
2959 case SIOCGMIIREG:
2960 case SIOCSMIIREG:
Arnd Bergmann6b960182009-11-06 23:10:54 -08002961 return dev_ifsioc(net, sock, cmd, argp);
Arnd Bergmann9177efd2009-11-06 08:09:09 +00002962
Arnd Bergmann6b960182009-11-06 23:10:54 -08002963 case SIOCSARP:
2964 case SIOCGARP:
2965 case SIOCDARP:
Arnd Bergmann6b960182009-11-06 23:10:54 -08002966 case SIOCATMARK:
Arnd Bergmann9177efd2009-11-06 08:09:09 +00002967 return sock_do_ioctl(net, sock, cmd, arg);
2968 }
2969
2970 /* Prevent warning from compat_sys_ioctl, these always
2971 * result in -EINVAL in the native case anyway. */
2972 switch (cmd) {
2973 case SIOCRTMSG:
2974 case SIOCGIFCOUNT:
Arnd Bergmann6b960182009-11-06 23:10:54 -08002975 case SIOCSRARP:
2976 case SIOCGRARP:
2977 case SIOCDRARP:
Arnd Bergmann9177efd2009-11-06 08:09:09 +00002978 case SIOCSIFLINK:
2979 case SIOCGIFSLAVE:
2980 case SIOCSIFSLAVE:
2981 return -EINVAL;
Arnd Bergmann6b960182009-11-06 23:10:54 -08002982 }
2983
2984 return -ENOIOCTLCMD;
2985}
Arnd Bergmann7a229382009-11-06 23:00:29 -08002986
Shaun Pereira89bbfc92006-03-21 23:58:08 -08002987static long compat_sock_ioctl(struct file *file, unsigned cmd,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002988 unsigned long arg)
Shaun Pereira89bbfc92006-03-21 23:58:08 -08002989{
2990 struct socket *sock = file->private_data;
2991 int ret = -ENOIOCTLCMD;
David S. Miller87de87d2008-06-03 09:14:03 -07002992 struct sock *sk;
2993 struct net *net;
2994
2995 sk = sock->sk;
2996 net = sock_net(sk);
Shaun Pereira89bbfc92006-03-21 23:58:08 -08002997
2998 if (sock->ops->compat_ioctl)
2999 ret = sock->ops->compat_ioctl(sock, cmd, arg);
3000
David S. Miller87de87d2008-06-03 09:14:03 -07003001 if (ret == -ENOIOCTLCMD &&
3002 (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST))
3003 ret = compat_wext_handle_ioctl(net, cmd, arg);
3004
Arnd Bergmann6b960182009-11-06 23:10:54 -08003005 if (ret == -ENOIOCTLCMD)
3006 ret = compat_sock_ioctl_trans(file, sock, cmd, arg);
3007
Shaun Pereira89bbfc92006-03-21 23:58:08 -08003008 return ret;
3009}
3010#endif
3011
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003012int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen)
3013{
3014 return sock->ops->bind(sock, addr, addrlen);
3015}
3016
3017int kernel_listen(struct socket *sock, int backlog)
3018{
3019 return sock->ops->listen(sock, backlog);
3020}
3021
3022int kernel_accept(struct socket *sock, struct socket **newsock, int flags)
3023{
3024 struct sock *sk = sock->sk;
3025 int err;
3026
3027 err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
3028 newsock);
3029 if (err < 0)
3030 goto done;
3031
3032 err = sock->ops->accept(sock, *newsock, flags);
3033 if (err < 0) {
3034 sock_release(*newsock);
Tony Battersbyfa8705b2007-10-10 21:09:04 -07003035 *newsock = NULL;
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003036 goto done;
3037 }
3038
3039 (*newsock)->ops = sock->ops;
Wei Yongjun1b085342008-12-18 19:35:10 -08003040 __module_get((*newsock)->ops->owner);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003041
3042done:
3043 return err;
3044}
3045
3046int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
YOSHIFUJI Hideaki4768fbc2007-02-09 23:25:31 +09003047 int flags)
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003048{
3049 return sock->ops->connect(sock, addr, addrlen, flags);
3050}
3051
3052int kernel_getsockname(struct socket *sock, struct sockaddr *addr,
3053 int *addrlen)
3054{
3055 return sock->ops->getname(sock, addr, addrlen, 0);
3056}
3057
3058int kernel_getpeername(struct socket *sock, struct sockaddr *addr,
3059 int *addrlen)
3060{
3061 return sock->ops->getname(sock, addr, addrlen, 1);
3062}
3063
3064int kernel_getsockopt(struct socket *sock, int level, int optname,
3065 char *optval, int *optlen)
3066{
3067 mm_segment_t oldfs = get_fs();
3068 int err;
3069
3070 set_fs(KERNEL_DS);
3071 if (level == SOL_SOCKET)
3072 err = sock_getsockopt(sock, level, optname, optval, optlen);
3073 else
3074 err = sock->ops->getsockopt(sock, level, optname, optval,
3075 optlen);
3076 set_fs(oldfs);
3077 return err;
3078}
3079
3080int kernel_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07003081 char *optval, unsigned int optlen)
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003082{
3083 mm_segment_t oldfs = get_fs();
3084 int err;
3085
3086 set_fs(KERNEL_DS);
3087 if (level == SOL_SOCKET)
3088 err = sock_setsockopt(sock, level, optname, optval, optlen);
3089 else
3090 err = sock->ops->setsockopt(sock, level, optname, optval,
3091 optlen);
3092 set_fs(oldfs);
3093 return err;
3094}
3095
3096int kernel_sendpage(struct socket *sock, struct page *page, int offset,
3097 size_t size, int flags)
3098{
3099 if (sock->ops->sendpage)
3100 return sock->ops->sendpage(sock, page, offset, size, flags);
3101
3102 return sock_no_sendpage(sock, page, offset, size, flags);
3103}
3104
3105int kernel_sock_ioctl(struct socket *sock, int cmd, unsigned long arg)
3106{
3107 mm_segment_t oldfs = get_fs();
3108 int err;
3109
3110 set_fs(KERNEL_DS);
3111 err = sock->ops->ioctl(sock, cmd, arg);
3112 set_fs(oldfs);
3113
3114 return err;
3115}
3116
Trond Myklebust91cf45f2007-11-12 18:10:39 -08003117int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how)
3118{
3119 return sock->ops->shutdown(sock, how);
3120}
3121
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122EXPORT_SYMBOL(sock_create);
3123EXPORT_SYMBOL(sock_create_kern);
3124EXPORT_SYMBOL(sock_create_lite);
3125EXPORT_SYMBOL(sock_map_fd);
3126EXPORT_SYMBOL(sock_recvmsg);
3127EXPORT_SYMBOL(sock_register);
3128EXPORT_SYMBOL(sock_release);
3129EXPORT_SYMBOL(sock_sendmsg);
3130EXPORT_SYMBOL(sock_unregister);
3131EXPORT_SYMBOL(sock_wake_async);
3132EXPORT_SYMBOL(sockfd_lookup);
3133EXPORT_SYMBOL(kernel_sendmsg);
3134EXPORT_SYMBOL(kernel_recvmsg);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003135EXPORT_SYMBOL(kernel_bind);
3136EXPORT_SYMBOL(kernel_listen);
3137EXPORT_SYMBOL(kernel_accept);
3138EXPORT_SYMBOL(kernel_connect);
3139EXPORT_SYMBOL(kernel_getsockname);
3140EXPORT_SYMBOL(kernel_getpeername);
3141EXPORT_SYMBOL(kernel_getsockopt);
3142EXPORT_SYMBOL(kernel_setsockopt);
3143EXPORT_SYMBOL(kernel_sendpage);
3144EXPORT_SYMBOL(kernel_sock_ioctl);
Trond Myklebust91cf45f2007-11-12 18:10:39 -08003145EXPORT_SYMBOL(kernel_sock_shutdown);