blob: dae8c6b84a096c7b8066e719d22c6bcc9a7e8269 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * NET An implementation of the SOCKET network access protocol.
3 *
4 * Version: @(#)socket.c 1.1.93 18/02/95
5 *
6 * Authors: Orest Zborowski, <obz@Kodak.COM>
Jesper Juhl02c30a82005-05-05 16:16:16 -07007 * Ross Biro
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
9 *
10 * Fixes:
11 * Anonymous : NOTSOCK/BADF cleanup. Error fix in
12 * shutdown()
13 * Alan Cox : verify_area() fixes
14 * Alan Cox : Removed DDI
15 * Jonathan Kamens : SOCK_DGRAM reconnect bug
16 * Alan Cox : Moved a load of checks to the very
17 * top level.
18 * Alan Cox : Move address structures to/from user
19 * mode above the protocol layers.
20 * Rob Janssen : Allow 0 length sends.
21 * Alan Cox : Asynchronous I/O support (cribbed from the
22 * tty drivers).
23 * Niibe Yutaka : Asynchronous I/O for writes (4.4BSD style)
24 * Jeff Uphoff : Made max number of sockets command-line
25 * configurable.
26 * Matti Aarnio : Made the number of sockets dynamic,
27 * to be allocated when needed, and mr.
28 * Uphoff's max is used as max to be
29 * allowed to allocate.
30 * Linus : Argh. removed all the socket allocation
31 * altogether: it's in the inode now.
32 * Alan Cox : Made sock_alloc()/sock_release() public
33 * for NetROM and future kernel nfsd type
34 * stuff.
35 * Alan Cox : sendmsg/recvmsg basics.
36 * Tom Dyas : Export net symbols.
37 * Marcin Dalecki : Fixed problems with CONFIG_NET="n".
38 * Alan Cox : Added thread locking to sys_* calls
39 * for sockets. May have errors at the
40 * moment.
41 * Kevin Buhr : Fixed the dumb errors in the above.
42 * Andi Kleen : Some small cleanups, optimizations,
43 * and fixed a copy_from_user() bug.
44 * Tigran Aivazian : sys_send(args) calls sys_sendto(args, NULL, 0)
Stephen Hemminger89bddce2006-09-01 00:19:31 -070045 * Tigran Aivazian : Made listen(2) backlog sanity checks
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 * protocol-independent
47 *
48 *
49 * This program is free software; you can redistribute it and/or
50 * modify it under the terms of the GNU General Public License
51 * as published by the Free Software Foundation; either version
52 * 2 of the License, or (at your option) any later version.
53 *
54 *
55 * This module is effectively the top level interface to the BSD socket
Stephen Hemminger89bddce2006-09-01 00:19:31 -070056 * paradigm.
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 *
58 * Based upon Swansea University Computer Society NET3.039
59 */
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <linux/socket.h>
63#include <linux/file.h>
64#include <linux/net.h>
65#include <linux/interrupt.h>
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -070066#include <linux/thread_info.h>
Stephen Hemminger55737fd2006-09-01 00:23:39 -070067#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/netdevice.h>
69#include <linux/proc_fs.h>
70#include <linux/seq_file.h>
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080071#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include <linux/wanrouter.h>
73#include <linux/if_bridge.h>
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -030074#include <linux/if_frad.h>
75#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#include <linux/init.h>
77#include <linux/poll.h>
78#include <linux/cache.h>
79#include <linux/module.h>
80#include <linux/highmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#include <linux/mount.h>
82#include <linux/security.h>
83#include <linux/syscalls.h>
84#include <linux/compat.h>
85#include <linux/kmod.h>
David Woodhouse3ec3b2f2005-05-17 12:08:48 +010086#include <linux/audit.h>
Adrian Bunkd86b5e02006-01-21 00:46:55 +010087#include <linux/wireless.h>
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -070088#include <linux/nsproxy.h>
Nick Black1fd7317d2009-09-22 16:43:33 -070089#include <linux/magic.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090090#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92#include <asm/uaccess.h>
93#include <asm/unistd.h>
94
95#include <net/compat.h>
David S. Miller87de87d2008-06-03 09:14:03 -070096#include <net/wext.h>
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;
Eric Dumazet43815482010-04-29 11:01:49 +0000255 ei->socket.wq = kmalloc(sizeof(struct socket_wq), GFP_KERNEL);
256 if (!ei->socket.wq) {
257 kmem_cache_free(sock_inode_cachep, ei);
258 return NULL;
259 }
260 init_waitqueue_head(&ei->socket.wq->wait);
261 ei->socket.wq->fasync_list = NULL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 ei->socket.state = SS_UNCONNECTED;
264 ei->socket.flags = 0;
265 ei->socket.ops = NULL;
266 ei->socket.sk = NULL;
267 ei->socket.file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 return &ei->vfs_inode;
270}
271
Eric Dumazet43815482010-04-29 11:01:49 +0000272
273static void wq_free_rcu(struct rcu_head *head)
274{
275 struct socket_wq *wq = container_of(head, struct socket_wq, rcu);
276
277 kfree(wq);
278}
279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280static void sock_destroy_inode(struct inode *inode)
281{
Eric Dumazet43815482010-04-29 11:01:49 +0000282 struct socket_alloc *ei;
283
284 ei = container_of(inode, struct socket_alloc, vfs_inode);
285 call_rcu(&ei->socket.wq->rcu, wq_free_rcu);
286 kmem_cache_free(sock_inode_cachep, ei);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}
288
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700289static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700291 struct socket_alloc *ei = (struct socket_alloc *)foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Christoph Lametera35afb82007-05-16 22:10:57 -0700293 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296static int init_inodecache(void)
297{
298 sock_inode_cachep = kmem_cache_create("sock_inode_cache",
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700299 sizeof(struct socket_alloc),
300 0,
301 (SLAB_HWCACHE_ALIGN |
302 SLAB_RECLAIM_ACCOUNT |
303 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +0900304 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 if (sock_inode_cachep == NULL)
306 return -ENOMEM;
307 return 0;
308}
309
Alexey Dobriyanb87221d2009-09-21 17:01:09 -0700310static const struct super_operations sockfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 .alloc_inode = sock_alloc_inode,
312 .destroy_inode =sock_destroy_inode,
313 .statfs = simple_statfs,
314};
315
David Howells454e2392006-06-23 02:02:57 -0700316static int sockfs_get_sb(struct file_system_type *fs_type,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700317 int flags, const char *dev_name, void *data,
318 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
David Howells454e2392006-06-23 02:02:57 -0700320 return get_sb_pseudo(fs_type, "socket:", &sockfs_ops, SOCKFS_MAGIC,
321 mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
323
Eric Dumazetba899662005-08-26 12:05:31 -0700324static struct vfsmount *sock_mnt __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326static struct file_system_type sock_fs_type = {
327 .name = "sockfs",
328 .get_sb = sockfs_get_sb,
329 .kill_sb = kill_anon_super,
330};
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700331
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700332/*
333 * sockfs_dname() is called from d_path().
334 */
335static char *sockfs_dname(struct dentry *dentry, char *buffer, int buflen)
336{
337 return dynamic_dname(dentry, buffer, buflen, "socket:[%lu]",
338 dentry->d_inode->i_ino);
339}
340
Al Viro3ba13d12009-02-20 06:02:22 +0000341static const struct dentry_operations sockfs_dentry_operations = {
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700342 .d_dname = sockfs_dname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343};
344
345/*
346 * Obtains the first available file descriptor and sets it up for use.
347 *
David S. Miller39d8c1b2006-03-20 17:13:49 -0800348 * These functions create file structures and maps them to fd space
349 * of the current process. On success it returns file descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 * and file struct implicitly stored in sock->file.
351 * Note that another thread may close file descriptor before we return
352 * from this function. We use the fact that now we do not refer
353 * to socket after mapping. If one day we will need it, this
354 * function will increment ref. count on file by 1.
355 *
356 * In any case returned fd MAY BE not valid!
357 * This race condition is unavoidable
358 * with shared fd spaces, we cannot solve it inside kernel,
359 * but we take care of internal coherence yet.
360 */
361
Al Viro7cbe66b2009-08-05 19:59:08 +0400362static int sock_alloc_file(struct socket *sock, struct file **f, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
Al Viro7cbe66b2009-08-05 19:59:08 +0400364 struct qstr name = { .name = "" };
Al Viro2c48b9c2009-08-09 00:52:35 +0400365 struct path path;
Al Viro7cbe66b2009-08-05 19:59:08 +0400366 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 int fd;
David S. Miller39d8c1b2006-03-20 17:13:49 -0800368
Ulrich Dreppera677a032008-07-23 21:29:17 -0700369 fd = get_unused_fd_flags(flags);
Al Viro7cbe66b2009-08-05 19:59:08 +0400370 if (unlikely(fd < 0))
371 return fd;
David S. Miller39d8c1b2006-03-20 17:13:49 -0800372
Al Viro2c48b9c2009-08-09 00:52:35 +0400373 path.dentry = d_alloc(sock_mnt->mnt_sb->s_root, &name);
374 if (unlikely(!path.dentry)) {
Al Viro7cbe66b2009-08-05 19:59:08 +0400375 put_unused_fd(fd);
David S. Miller39d8c1b2006-03-20 17:13:49 -0800376 return -ENOMEM;
Al Viro7cbe66b2009-08-05 19:59:08 +0400377 }
Al Viro2c48b9c2009-08-09 00:52:35 +0400378 path.mnt = mntget(sock_mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Al Viro2c48b9c2009-08-09 00:52:35 +0400380 path.dentry->d_op = &sockfs_dentry_operations;
Al Viro2c48b9c2009-08-09 00:52:35 +0400381 d_instantiate(path.dentry, SOCK_INODE(sock));
Al Virocc3808f2009-08-06 09:43:59 +0400382 SOCK_INODE(sock)->i_fop = &socket_file_ops;
383
Al Viro2c48b9c2009-08-09 00:52:35 +0400384 file = alloc_file(&path, FMODE_READ | FMODE_WRITE,
Al Virocc3808f2009-08-06 09:43:59 +0400385 &socket_file_ops);
386 if (unlikely(!file)) {
387 /* drop dentry, keep inode */
388 atomic_inc(&path.dentry->d_inode->i_count);
Al Viro2c48b9c2009-08-09 00:52:35 +0400389 path_put(&path);
Al Virocc3808f2009-08-06 09:43:59 +0400390 put_unused_fd(fd);
391 return -ENFILE;
392 }
David S. Miller39d8c1b2006-03-20 17:13:49 -0800393
394 sock->file = file;
Ulrich Drepper77d27202008-07-23 21:29:35 -0700395 file->f_flags = O_RDWR | (flags & O_NONBLOCK);
David S. Miller39d8c1b2006-03-20 17:13:49 -0800396 file->f_pos = 0;
397 file->private_data = sock;
398
Al Viro7cbe66b2009-08-05 19:59:08 +0400399 *f = file;
400 return fd;
David S. Miller39d8c1b2006-03-20 17:13:49 -0800401}
402
Ulrich Dreppera677a032008-07-23 21:29:17 -0700403int sock_map_fd(struct socket *sock, int flags)
David S. Miller39d8c1b2006-03-20 17:13:49 -0800404{
405 struct file *newfile;
Al Viro7cbe66b2009-08-05 19:59:08 +0400406 int fd = sock_alloc_file(sock, &newfile, flags);
David S. Miller39d8c1b2006-03-20 17:13:49 -0800407
Al Viro7cbe66b2009-08-05 19:59:08 +0400408 if (likely(fd >= 0))
David S. Miller39d8c1b2006-03-20 17:13:49 -0800409 fd_install(fd, newfile);
Al Viro7cbe66b2009-08-05 19:59:08 +0400410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 return fd;
412}
413
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800414static struct socket *sock_from_file(struct file *file, int *err)
415{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800416 if (file->f_op == &socket_file_ops)
417 return file->private_data; /* set in sock_map_fd */
418
Eric Dumazet23bb80d2007-02-08 14:59:57 -0800419 *err = -ENOTSOCK;
420 return NULL;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800421}
422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423/**
424 * sockfd_lookup - Go from a file number to its socket slot
425 * @fd: file handle
426 * @err: pointer to an error code return
427 *
428 * The file handle passed in is locked and the socket it is bound
429 * too is returned. If an error occurs the err pointer is overwritten
430 * with a negative errno code and NULL is returned. The function checks
431 * for both invalid handles and passing a handle which is not a socket.
432 *
433 * On a success the socket object pointer is returned.
434 */
435
436struct socket *sockfd_lookup(int fd, int *err)
437{
438 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 struct socket *sock;
440
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700441 file = fget(fd);
442 if (!file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 *err = -EBADF;
444 return NULL;
445 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700446
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800447 sock = sock_from_file(file, err);
448 if (!sock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 fput(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 return sock;
451}
452
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800453static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed)
454{
455 struct file *file;
456 struct socket *sock;
457
Hua Zhong36725582006-04-19 15:25:02 -0700458 *err = -EBADF;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800459 file = fget_light(fd, fput_needed);
460 if (file) {
461 sock = sock_from_file(file, err);
462 if (sock)
463 return sock;
464 fput_light(file, *fput_needed);
465 }
466 return NULL;
467}
468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469/**
470 * sock_alloc - allocate a socket
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700471 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 * Allocate a new inode and socket object. The two are bound together
473 * and initialised. The socket is then returned. If we are out of inodes
474 * NULL is returned.
475 */
476
477static struct socket *sock_alloc(void)
478{
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700479 struct inode *inode;
480 struct socket *sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 inode = new_inode(sock_mnt->mnt_sb);
483 if (!inode)
484 return NULL;
485
486 sock = SOCKET_I(inode);
487
Eric Dumazet29a020d2009-09-15 02:39:20 -0700488 kmemcheck_annotate_bitfield(sock, type);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700489 inode->i_mode = S_IFSOCK | S_IRWXUGO;
David Howells8192b0c2008-11-14 10:39:10 +1100490 inode->i_uid = current_fsuid();
491 inode->i_gid = current_fsgid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Eric Dumazet4e694892009-04-04 16:41:09 -0700493 percpu_add(sockets_in_use, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 return sock;
495}
496
497/*
498 * In theory you can't get an open on this inode, but /proc provides
499 * a back door. Remember to keep it shut otherwise you'll let the
500 * creepy crawlies in.
501 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503static int sock_no_open(struct inode *irrelevant, struct file *dontcare)
504{
505 return -ENXIO;
506}
507
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800508const struct file_operations bad_sock_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 .owner = THIS_MODULE,
510 .open = sock_no_open,
511};
512
513/**
514 * sock_release - close a socket
515 * @sock: socket to close
516 *
517 * The socket is released from the protocol stack if it has a release
518 * callback, and the inode is then released if the socket is bound to
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700519 * an inode not a file.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522void sock_release(struct socket *sock)
523{
524 if (sock->ops) {
525 struct module *owner = sock->ops->owner;
526
527 sock->ops->release(sock);
528 sock->ops = NULL;
529 module_put(owner);
530 }
531
Eric Dumazet43815482010-04-29 11:01:49 +0000532 if (sock->wq->fasync_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 printk(KERN_ERR "sock_release: fasync list not empty!\n");
534
Eric Dumazet4e694892009-04-04 16:41:09 -0700535 percpu_sub(sockets_in_use, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 if (!sock->file) {
537 iput(SOCK_INODE(sock));
538 return;
539 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700540 sock->file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541}
542
Patrick Ohly20d49472009-02-12 05:03:38 +0000543int sock_tx_timestamp(struct msghdr *msg, struct sock *sk,
544 union skb_shared_tx *shtx)
545{
546 shtx->flags = 0;
547 if (sock_flag(sk, SOCK_TIMESTAMPING_TX_HARDWARE))
548 shtx->hardware = 1;
549 if (sock_flag(sk, SOCK_TIMESTAMPING_TX_SOFTWARE))
550 shtx->software = 1;
551 return 0;
552}
553EXPORT_SYMBOL(sock_tx_timestamp);
554
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700555static inline int __sock_sendmsg(struct kiocb *iocb, struct socket *sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 struct msghdr *msg, size_t size)
557{
558 struct sock_iocb *si = kiocb_to_siocb(iocb);
559 int err;
560
561 si->sock = sock;
562 si->scm = NULL;
563 si->msg = msg;
564 si->size = size;
565
566 err = security_socket_sendmsg(sock, msg, size);
567 if (err)
568 return err;
569
570 return sock->ops->sendmsg(iocb, sock, msg, size);
571}
572
573int sock_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
574{
575 struct kiocb iocb;
576 struct sock_iocb siocb;
577 int ret;
578
579 init_sync_kiocb(&iocb, NULL);
580 iocb.private = &siocb;
581 ret = __sock_sendmsg(&iocb, sock, msg, size);
582 if (-EIOCBQUEUED == ret)
583 ret = wait_on_sync_kiocb(&iocb);
584 return ret;
585}
586
587int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
588 struct kvec *vec, size_t num, size_t size)
589{
590 mm_segment_t oldfs = get_fs();
591 int result;
592
593 set_fs(KERNEL_DS);
594 /*
595 * the following is safe, since for compiler definitions of kvec and
596 * iovec are identical, yielding the same in-core layout and alignment
597 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700598 msg->msg_iov = (struct iovec *)vec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 msg->msg_iovlen = num;
600 result = sock_sendmsg(sock, msg, size);
601 set_fs(oldfs);
602 return result;
603}
604
Patrick Ohly20d49472009-02-12 05:03:38 +0000605static int ktime2ts(ktime_t kt, struct timespec *ts)
606{
607 if (kt.tv64) {
608 *ts = ktime_to_timespec(kt);
609 return 1;
610 } else {
611 return 0;
612 }
613}
614
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700615/*
616 * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
617 */
618void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
619 struct sk_buff *skb)
620{
Patrick Ohly20d49472009-02-12 05:03:38 +0000621 int need_software_tstamp = sock_flag(sk, SOCK_RCVTSTAMP);
622 struct timespec ts[3];
623 int empty = 1;
624 struct skb_shared_hwtstamps *shhwtstamps =
625 skb_hwtstamps(skb);
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700626
Patrick Ohly20d49472009-02-12 05:03:38 +0000627 /* Race occurred between timestamp enabling and packet
628 receiving. Fill in the current time for now. */
629 if (need_software_tstamp && skb->tstamp.tv64 == 0)
630 __net_timestamp(skb);
631
632 if (need_software_tstamp) {
633 if (!sock_flag(sk, SOCK_RCVTSTAMPNS)) {
634 struct timeval tv;
635 skb_get_timestamp(skb, &tv);
636 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMP,
637 sizeof(tv), &tv);
638 } else {
Hagen Paul Pfeifer842509b2010-04-06 05:39:52 +0000639 skb_get_timestampns(skb, &ts[0]);
Patrick Ohly20d49472009-02-12 05:03:38 +0000640 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPNS,
Hagen Paul Pfeifer842509b2010-04-06 05:39:52 +0000641 sizeof(ts[0]), &ts[0]);
Patrick Ohly20d49472009-02-12 05:03:38 +0000642 }
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700643 }
Patrick Ohly20d49472009-02-12 05:03:38 +0000644
645
646 memset(ts, 0, sizeof(ts));
647 if (skb->tstamp.tv64 &&
648 sock_flag(sk, SOCK_TIMESTAMPING_SOFTWARE)) {
649 skb_get_timestampns(skb, ts + 0);
650 empty = 0;
651 }
652 if (shhwtstamps) {
653 if (sock_flag(sk, SOCK_TIMESTAMPING_SYS_HARDWARE) &&
654 ktime2ts(shhwtstamps->syststamp, ts + 1))
655 empty = 0;
656 if (sock_flag(sk, SOCK_TIMESTAMPING_RAW_HARDWARE) &&
657 ktime2ts(shhwtstamps->hwtstamp, ts + 2))
658 empty = 0;
659 }
660 if (!empty)
661 put_cmsg(msg, SOL_SOCKET,
662 SCM_TIMESTAMPING, sizeof(ts), &ts);
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700663}
664
Arnaldo Carvalho de Melo7c81fd82007-03-10 00:39:35 -0300665EXPORT_SYMBOL_GPL(__sock_recv_timestamp);
666
Neil Horman3b885782009-10-12 13:26:31 -0700667inline void sock_recv_drops(struct msghdr *msg, struct sock *sk, struct sk_buff *skb)
668{
669 if (sock_flag(sk, SOCK_RXQ_OVFL) && skb && skb->dropcount)
670 put_cmsg(msg, SOL_SOCKET, SO_RXQ_OVFL,
671 sizeof(__u32), &skb->dropcount);
672}
673
Eric Dumazet767dd032010-04-28 19:14:43 +0000674void __sock_recv_ts_and_drops(struct msghdr *msg, struct sock *sk,
Neil Horman3b885782009-10-12 13:26:31 -0700675 struct sk_buff *skb)
676{
677 sock_recv_timestamp(msg, sk, skb);
678 sock_recv_drops(msg, sk, skb);
679}
Eric Dumazet767dd032010-04-28 19:14:43 +0000680EXPORT_SYMBOL_GPL(__sock_recv_ts_and_drops);
Neil Horman3b885782009-10-12 13:26:31 -0700681
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -0700682static inline int __sock_recvmsg_nosec(struct kiocb *iocb, struct socket *sock,
683 struct msghdr *msg, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 struct sock_iocb *si = kiocb_to_siocb(iocb);
686
687 si->sock = sock;
688 si->scm = NULL;
689 si->msg = msg;
690 si->size = size;
691 si->flags = flags;
692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 return sock->ops->recvmsg(iocb, sock, msg, size, flags);
694}
695
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -0700696static inline int __sock_recvmsg(struct kiocb *iocb, struct socket *sock,
697 struct msghdr *msg, size_t size, int flags)
698{
699 int err = security_socket_recvmsg(sock, msg, size, flags);
700
701 return err ?: __sock_recvmsg_nosec(iocb, sock, msg, size, flags);
702}
703
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700704int sock_recvmsg(struct socket *sock, struct msghdr *msg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 size_t size, int flags)
706{
707 struct kiocb iocb;
708 struct sock_iocb siocb;
709 int ret;
710
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700711 init_sync_kiocb(&iocb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 iocb.private = &siocb;
713 ret = __sock_recvmsg(&iocb, sock, msg, size, flags);
714 if (-EIOCBQUEUED == ret)
715 ret = wait_on_sync_kiocb(&iocb);
716 return ret;
717}
718
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -0700719static int sock_recvmsg_nosec(struct socket *sock, struct msghdr *msg,
720 size_t size, int flags)
721{
722 struct kiocb iocb;
723 struct sock_iocb siocb;
724 int ret;
725
726 init_sync_kiocb(&iocb, NULL);
727 iocb.private = &siocb;
728 ret = __sock_recvmsg_nosec(&iocb, sock, msg, size, flags);
729 if (-EIOCBQUEUED == ret)
730 ret = wait_on_sync_kiocb(&iocb);
731 return ret;
732}
733
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700734int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
735 struct kvec *vec, size_t num, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
737 mm_segment_t oldfs = get_fs();
738 int result;
739
740 set_fs(KERNEL_DS);
741 /*
742 * the following is safe, since for compiler definitions of kvec and
743 * iovec are identical, yielding the same in-core layout and alignment
744 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700745 msg->msg_iov = (struct iovec *)vec, msg->msg_iovlen = num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 result = sock_recvmsg(sock, msg, size, flags);
747 set_fs(oldfs);
748 return result;
749}
750
751static void sock_aio_dtor(struct kiocb *iocb)
752{
753 kfree(iocb->private);
754}
755
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -0300756static ssize_t sock_sendpage(struct file *file, struct page *page,
757 int offset, size_t size, loff_t *ppos, int more)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
759 struct socket *sock;
760 int flags;
761
Eric Dumazetb69aee02005-09-06 14:42:45 -0700762 sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764 flags = !(file->f_flags & O_NONBLOCK) ? 0 : MSG_DONTWAIT;
765 if (more)
766 flags |= MSG_MORE;
767
Linus Torvaldse6949582009-08-13 08:28:36 -0700768 return kernel_sendpage(sock, page, offset, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769}
770
Jens Axboe9c55e012007-11-06 23:30:13 -0800771static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
772 struct pipe_inode_info *pipe, size_t len,
773 unsigned int flags)
774{
775 struct socket *sock = file->private_data;
776
Rémi Denis-Courmont997b37d2008-02-15 02:35:45 -0800777 if (unlikely(!sock->ops->splice_read))
778 return -EINVAL;
779
Jens Axboe9c55e012007-11-06 23:30:13 -0800780 return sock->ops->splice_read(sock, ppos, pipe, len, flags);
781}
782
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800783static struct sock_iocb *alloc_sock_iocb(struct kiocb *iocb,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700784 struct sock_iocb *siocb)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800785{
786 if (!is_sync_kiocb(iocb)) {
787 siocb = kmalloc(sizeof(*siocb), GFP_KERNEL);
788 if (!siocb)
789 return NULL;
790 iocb->ki_dtor = sock_aio_dtor;
791 }
792
793 siocb->kiocb = iocb;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800794 iocb->private = siocb;
795 return siocb;
796}
797
798static ssize_t do_sock_read(struct msghdr *msg, struct kiocb *iocb,
Badari Pulavarty027445c2006-09-30 23:28:46 -0700799 struct file *file, const struct iovec *iov,
800 unsigned long nr_segs)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800801{
802 struct socket *sock = file->private_data;
803 size_t size = 0;
804 int i;
805
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700806 for (i = 0; i < nr_segs; i++)
807 size += iov[i].iov_len;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800808
809 msg->msg_name = NULL;
810 msg->msg_namelen = 0;
811 msg->msg_control = NULL;
812 msg->msg_controllen = 0;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700813 msg->msg_iov = (struct iovec *)iov;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800814 msg->msg_iovlen = nr_segs;
815 msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
816
817 return __sock_recvmsg(iocb, sock, msg, size, msg->msg_flags);
818}
819
Badari Pulavarty027445c2006-09-30 23:28:46 -0700820static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov,
821 unsigned long nr_segs, loff_t pos)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800822{
823 struct sock_iocb siocb, *x;
824
825 if (pos != 0)
826 return -ESPIPE;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700827
828 if (iocb->ki_left == 0) /* Match SYS5 behaviour */
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800829 return 0;
830
Badari Pulavarty027445c2006-09-30 23:28:46 -0700831
832 x = alloc_sock_iocb(iocb, &siocb);
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800833 if (!x)
834 return -ENOMEM;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700835 return do_sock_read(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800836}
837
838static ssize_t do_sock_write(struct msghdr *msg, struct kiocb *iocb,
Badari Pulavarty027445c2006-09-30 23:28:46 -0700839 struct file *file, const struct iovec *iov,
840 unsigned long nr_segs)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800841{
842 struct socket *sock = file->private_data;
843 size_t size = 0;
844 int i;
845
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700846 for (i = 0; i < nr_segs; i++)
847 size += iov[i].iov_len;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800848
849 msg->msg_name = NULL;
850 msg->msg_namelen = 0;
851 msg->msg_control = NULL;
852 msg->msg_controllen = 0;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700853 msg->msg_iov = (struct iovec *)iov;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800854 msg->msg_iovlen = nr_segs;
855 msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
856 if (sock->type == SOCK_SEQPACKET)
857 msg->msg_flags |= MSG_EOR;
858
859 return __sock_sendmsg(iocb, sock, msg, size);
860}
861
Badari Pulavarty027445c2006-09-30 23:28:46 -0700862static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov,
863 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800865 struct sock_iocb siocb, *x;
866
867 if (pos != 0)
868 return -ESPIPE;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700869
Badari Pulavarty027445c2006-09-30 23:28:46 -0700870 x = alloc_sock_iocb(iocb, &siocb);
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800871 if (!x)
872 return -ENOMEM;
873
Badari Pulavarty027445c2006-09-30 23:28:46 -0700874 return do_sock_write(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875}
876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877/*
878 * Atomic setting of ioctl hooks to avoid race
879 * with module unload.
880 */
881
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800882static DEFINE_MUTEX(br_ioctl_mutex);
Eric W. Biederman881d9662007-09-17 11:56:21 -0700883static int (*br_ioctl_hook) (struct net *, unsigned int cmd, void __user *arg) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Eric W. Biederman881d9662007-09-17 11:56:21 -0700885void brioctl_set(int (*hook) (struct net *, unsigned int, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800887 mutex_lock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 br_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800889 mutex_unlock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890}
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892EXPORT_SYMBOL(brioctl_set);
893
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800894static DEFINE_MUTEX(vlan_ioctl_mutex);
Eric W. Biederman881d9662007-09-17 11:56:21 -0700895static int (*vlan_ioctl_hook) (struct net *, void __user *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Eric W. Biederman881d9662007-09-17 11:56:21 -0700897void vlan_ioctl_set(int (*hook) (struct net *, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800899 mutex_lock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 vlan_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800901 mutex_unlock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902}
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700903
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904EXPORT_SYMBOL(vlan_ioctl_set);
905
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800906static DEFINE_MUTEX(dlci_ioctl_mutex);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700907static int (*dlci_ioctl_hook) (unsigned int, void __user *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700909void dlci_ioctl_set(int (*hook) (unsigned int, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800911 mutex_lock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 dlci_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800913 mutex_unlock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914}
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916EXPORT_SYMBOL(dlci_ioctl_set);
917
Arnd Bergmann6b960182009-11-06 23:10:54 -0800918static long sock_do_ioctl(struct net *net, struct socket *sock,
919 unsigned int cmd, unsigned long arg)
920{
921 int err;
922 void __user *argp = (void __user *)arg;
923
924 err = sock->ops->ioctl(sock, cmd, arg);
925
926 /*
927 * If this ioctl is unknown try to hand it down
928 * to the NIC driver.
929 */
930 if (err == -ENOIOCTLCMD)
931 err = dev_ioctl(net, cmd, argp);
932
933 return err;
934}
935
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936/*
937 * With an ioctl, arg may well be a user mode pointer, but we don't know
938 * what to do with it - that's up to the protocol still.
939 */
940
941static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
942{
943 struct socket *sock;
Eric W. Biederman881d9662007-09-17 11:56:21 -0700944 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 void __user *argp = (void __user *)arg;
946 int pid, err;
Eric W. Biederman881d9662007-09-17 11:56:21 -0700947 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Eric Dumazetb69aee02005-09-06 14:42:45 -0700949 sock = file->private_data;
Eric W. Biederman881d9662007-09-17 11:56:21 -0700950 sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900951 net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
Eric W. Biederman881d9662007-09-17 11:56:21 -0700953 err = dev_ioctl(net, cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 } else
Johannes Berg3d23e342009-09-29 23:27:28 +0200955#ifdef CONFIG_WEXT_CORE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
Eric W. Biederman881d9662007-09-17 11:56:21 -0700957 err = dev_ioctl(net, cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 } else
Johannes Berg3d23e342009-09-29 23:27:28 +0200959#endif
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700960 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 case FIOSETOWN:
962 case SIOCSPGRP:
963 err = -EFAULT;
964 if (get_user(pid, (int __user *)argp))
965 break;
966 err = f_setown(sock->file, pid, 1);
967 break;
968 case FIOGETOWN:
969 case SIOCGPGRP:
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700970 err = put_user(f_getown(sock->file),
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700971 (int __user *)argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 break;
973 case SIOCGIFBR:
974 case SIOCSIFBR:
975 case SIOCBRADDBR:
976 case SIOCBRDELBR:
977 err = -ENOPKG;
978 if (!br_ioctl_hook)
979 request_module("bridge");
980
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800981 mutex_lock(&br_ioctl_mutex);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700982 if (br_ioctl_hook)
Eric W. Biederman881d9662007-09-17 11:56:21 -0700983 err = br_ioctl_hook(net, cmd, argp);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800984 mutex_unlock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 break;
986 case SIOCGIFVLAN:
987 case SIOCSIFVLAN:
988 err = -ENOPKG;
989 if (!vlan_ioctl_hook)
990 request_module("8021q");
991
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800992 mutex_lock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 if (vlan_ioctl_hook)
Eric W. Biederman881d9662007-09-17 11:56:21 -0700994 err = vlan_ioctl_hook(net, argp);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800995 mutex_unlock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 case SIOCADDDLCI:
998 case SIOCDELDLCI:
999 err = -ENOPKG;
1000 if (!dlci_ioctl_hook)
1001 request_module("dlci");
1002
Pavel Emelyanov7512cbf2008-03-21 15:58:52 -07001003 mutex_lock(&dlci_ioctl_mutex);
1004 if (dlci_ioctl_hook)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 err = dlci_ioctl_hook(cmd, argp);
Pavel Emelyanov7512cbf2008-03-21 15:58:52 -07001006 mutex_unlock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 break;
1008 default:
Arnd Bergmann6b960182009-11-06 23:10:54 -08001009 err = sock_do_ioctl(net, sock, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 break;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001011 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 return err;
1013}
1014
1015int sock_create_lite(int family, int type, int protocol, struct socket **res)
1016{
1017 int err;
1018 struct socket *sock = NULL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 err = security_socket_create(family, type, protocol, 1);
1021 if (err)
1022 goto out;
1023
1024 sock = sock_alloc();
1025 if (!sock) {
1026 err = -ENOMEM;
1027 goto out;
1028 }
1029
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 sock->type = type;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001031 err = security_socket_post_create(sock, family, type, protocol, 1);
1032 if (err)
1033 goto out_release;
1034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035out:
1036 *res = sock;
1037 return err;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001038out_release:
1039 sock_release(sock);
1040 sock = NULL;
1041 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042}
1043
1044/* No kernel lock held - perfect */
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001045static unsigned int sock_poll(struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
1047 struct socket *sock;
1048
1049 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001050 * We can't return errors to poll, so it's either yes or no.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 */
Eric Dumazetb69aee02005-09-06 14:42:45 -07001052 sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 return sock->ops->poll(file, sock, wait);
1054}
1055
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001056static int sock_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057{
Eric Dumazetb69aee02005-09-06 14:42:45 -07001058 struct socket *sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
1060 return sock->ops->mmap(file, sock, vma);
1061}
1062
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001063static int sock_close(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
1065 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001066 * It was possible the inode is NULL we were
1067 * closing an unfinished socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 */
1069
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001070 if (!inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 printk(KERN_DEBUG "sock_close: NULL inode\n");
1072 return 0;
1073 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 sock_release(SOCKET_I(inode));
1075 return 0;
1076}
1077
1078/*
1079 * Update the socket async list
1080 *
1081 * Fasync_list locking strategy.
1082 *
1083 * 1. fasync_list is modified only under process context socket lock
1084 * i.e. under semaphore.
1085 * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
Eric Dumazet989a2972010-04-14 09:55:35 +00001086 * or under socket lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 */
1088
1089static int sock_fasync(int fd, struct file *filp, int on)
1090{
Eric Dumazet989a2972010-04-14 09:55:35 +00001091 struct socket *sock = filp->private_data;
1092 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
Eric Dumazet989a2972010-04-14 09:55:35 +00001094 if (sk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
1097 lock_sock(sk);
1098
Eric Dumazet43815482010-04-29 11:01:49 +00001099 fasync_helper(fd, filp, on, &sock->wq->fasync_list);
Eric Dumazet989a2972010-04-14 09:55:35 +00001100
Eric Dumazet43815482010-04-29 11:01:49 +00001101 if (!sock->wq->fasync_list)
Eric Dumazet989a2972010-04-14 09:55:35 +00001102 sock_reset_flag(sk, SOCK_FASYNC);
Jonathan Corbet76398422009-02-01 14:26:59 -07001103 else
Eric Dumazetbcdce712009-10-06 17:28:29 -07001104 sock_set_flag(sk, SOCK_FASYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
Eric Dumazet989a2972010-04-14 09:55:35 +00001106 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 return 0;
1108}
1109
Eric Dumazet43815482010-04-29 11:01:49 +00001110/* This function may be called only under socket lock or callback_lock or rcu_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
1112int sock_wake_async(struct socket *sock, int how, int band)
1113{
Eric Dumazet43815482010-04-29 11:01:49 +00001114 struct socket_wq *wq;
1115
1116 if (!sock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 return -1;
Eric Dumazet43815482010-04-29 11:01:49 +00001118 rcu_read_lock();
1119 wq = rcu_dereference(sock->wq);
1120 if (!wq || !wq->fasync_list) {
1121 rcu_read_unlock();
1122 return -1;
1123 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001124 switch (how) {
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001125 case SOCK_WAKE_WAITD:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 if (test_bit(SOCK_ASYNC_WAITDATA, &sock->flags))
1127 break;
1128 goto call_kill;
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001129 case SOCK_WAKE_SPACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags))
1131 break;
1132 /* fall through */
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001133 case SOCK_WAKE_IO:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001134call_kill:
Eric Dumazet43815482010-04-29 11:01:49 +00001135 kill_fasync(&wq->fasync_list, SIGIO, band);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 break;
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001137 case SOCK_WAKE_URG:
Eric Dumazet43815482010-04-29 11:01:49 +00001138 kill_fasync(&wq->fasync_list, SIGURG, band);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 }
Eric Dumazet43815482010-04-29 11:01:49 +00001140 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 return 0;
1142}
1143
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001144static int __sock_create(struct net *net, int family, int type, int protocol,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001145 struct socket **res, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146{
1147 int err;
1148 struct socket *sock;
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001149 const struct net_proto_family *pf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
1151 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001152 * Check protocol is in range
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 */
1154 if (family < 0 || family >= NPROTO)
1155 return -EAFNOSUPPORT;
1156 if (type < 0 || type >= SOCK_MAX)
1157 return -EINVAL;
1158
1159 /* Compatibility.
1160
1161 This uglymoron is moved from INET layer to here to avoid
1162 deadlock in module load.
1163 */
1164 if (family == PF_INET && type == SOCK_PACKET) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001165 static int warned;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 if (!warned) {
1167 warned = 1;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001168 printk(KERN_INFO "%s uses obsolete (PF_INET,SOCK_PACKET)\n",
1169 current->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 }
1171 family = PF_PACKET;
1172 }
1173
1174 err = security_socket_create(family, type, protocol, kern);
1175 if (err)
1176 return err;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001177
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001178 /*
1179 * Allocate the socket and allow the family to set things up. if
1180 * the protocol is 0, the family is instructed to select an appropriate
1181 * default.
1182 */
1183 sock = sock_alloc();
1184 if (!sock) {
1185 if (net_ratelimit())
1186 printk(KERN_WARNING "socket: no more sockets\n");
1187 return -ENFILE; /* Not exactly a match, but its the
1188 closest posix thing */
1189 }
1190
1191 sock->type = type;
1192
Johannes Berg95a5afc2008-10-16 15:24:51 -07001193#ifdef CONFIG_MODULES
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001194 /* Attempt to load a protocol module if the find failed.
1195 *
1196 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 * requested real, full-featured networking support upon configuration.
1198 * Otherwise module support will break!
1199 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001200 if (net_families[family] == NULL)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001201 request_module("net-pf-%d", family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202#endif
1203
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001204 rcu_read_lock();
1205 pf = rcu_dereference(net_families[family]);
1206 err = -EAFNOSUPPORT;
1207 if (!pf)
1208 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
1210 /*
1211 * We will call the ->create function, that possibly is in a loadable
1212 * module, so we have to bump that loadable module refcnt first.
1213 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001214 if (!try_module_get(pf->owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 goto out_release;
1216
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001217 /* Now protected by module ref count */
1218 rcu_read_unlock();
1219
Eric Paris3f378b62009-11-05 22:18:14 -08001220 err = pf->create(net, sock, protocol, kern);
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001221 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 goto out_module_put;
Frank Filza79af592005-09-27 15:23:38 -07001223
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 /*
1225 * Now to bump the refcnt of the [loadable] module that owns this
1226 * socket at sock_release time we decrement its refcnt.
1227 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001228 if (!try_module_get(sock->ops->owner))
1229 goto out_module_busy;
1230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 /*
1232 * Now that we're done with the ->create function, the [loadable]
1233 * module can have its refcnt decremented
1234 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001235 module_put(pf->owner);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001236 err = security_socket_post_create(sock, family, type, protocol, kern);
1237 if (err)
Herbert Xu3b185522007-08-15 14:46:02 -07001238 goto out_sock_release;
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001239 *res = sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001241 return 0;
1242
1243out_module_busy:
1244 err = -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245out_module_put:
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001246 sock->ops = NULL;
1247 module_put(pf->owner);
1248out_sock_release:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 sock_release(sock);
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001250 return err;
1251
1252out_release:
1253 rcu_read_unlock();
1254 goto out_sock_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255}
1256
1257int sock_create(int family, int type, int protocol, struct socket **res)
1258{
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001259 return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260}
1261
1262int sock_create_kern(int family, int type, int protocol, struct socket **res)
1263{
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001264 return __sock_create(&init_net, family, type, protocol, res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265}
1266
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001267SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268{
1269 int retval;
1270 struct socket *sock;
Ulrich Dreppera677a032008-07-23 21:29:17 -07001271 int flags;
1272
Ulrich Dreppere38b36f2008-07-23 21:29:42 -07001273 /* Check the SOCK_* constants for consistency. */
1274 BUILD_BUG_ON(SOCK_CLOEXEC != O_CLOEXEC);
1275 BUILD_BUG_ON((SOCK_MAX | SOCK_TYPE_MASK) != SOCK_TYPE_MASK);
1276 BUILD_BUG_ON(SOCK_CLOEXEC & SOCK_TYPE_MASK);
1277 BUILD_BUG_ON(SOCK_NONBLOCK & SOCK_TYPE_MASK);
1278
Ulrich Dreppera677a032008-07-23 21:29:17 -07001279 flags = type & ~SOCK_TYPE_MASK;
Ulrich Drepper77d27202008-07-23 21:29:35 -07001280 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Dreppera677a032008-07-23 21:29:17 -07001281 return -EINVAL;
1282 type &= SOCK_TYPE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001284 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1285 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1286
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 retval = sock_create(family, type, protocol, &sock);
1288 if (retval < 0)
1289 goto out;
1290
Ulrich Drepper77d27202008-07-23 21:29:35 -07001291 retval = sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 if (retval < 0)
1293 goto out_release;
1294
1295out:
1296 /* It may be already another descriptor 8) Not kernel problem. */
1297 return retval;
1298
1299out_release:
1300 sock_release(sock);
1301 return retval;
1302}
1303
1304/*
1305 * Create a pair of connected sockets.
1306 */
1307
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001308SYSCALL_DEFINE4(socketpair, int, family, int, type, int, protocol,
1309 int __user *, usockvec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310{
1311 struct socket *sock1, *sock2;
1312 int fd1, fd2, err;
Al Virodb349502007-02-07 01:48:00 -05001313 struct file *newfile1, *newfile2;
Ulrich Dreppera677a032008-07-23 21:29:17 -07001314 int flags;
1315
1316 flags = type & ~SOCK_TYPE_MASK;
Ulrich Drepper77d27202008-07-23 21:29:35 -07001317 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Dreppera677a032008-07-23 21:29:17 -07001318 return -EINVAL;
1319 type &= SOCK_TYPE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001321 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1322 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1323
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 /*
1325 * Obtain the first socket and check if the underlying protocol
1326 * supports the socketpair call.
1327 */
1328
1329 err = sock_create(family, type, protocol, &sock1);
1330 if (err < 0)
1331 goto out;
1332
1333 err = sock_create(family, type, protocol, &sock2);
1334 if (err < 0)
1335 goto out_release_1;
1336
1337 err = sock1->ops->socketpair(sock1, sock2);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001338 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 goto out_release_both;
1340
Al Viro7cbe66b2009-08-05 19:59:08 +04001341 fd1 = sock_alloc_file(sock1, &newfile1, flags);
David S. Millerbf3c23d2007-10-29 21:54:02 -07001342 if (unlikely(fd1 < 0)) {
1343 err = fd1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 goto out_release_both;
David S. Millerbf3c23d2007-10-29 21:54:02 -07001345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
Al Viro7cbe66b2009-08-05 19:59:08 +04001347 fd2 = sock_alloc_file(sock2, &newfile2, flags);
Al Viro198de4d2009-08-05 19:29:23 +04001348 if (unlikely(fd2 < 0)) {
1349 err = fd2;
1350 fput(newfile1);
1351 put_unused_fd(fd1);
1352 sock_release(sock2);
1353 goto out;
Al Virodb349502007-02-07 01:48:00 -05001354 }
1355
Al Viro157cf642008-12-14 04:57:47 -05001356 audit_fd_pair(fd1, fd2);
Al Virodb349502007-02-07 01:48:00 -05001357 fd_install(fd1, newfile1);
1358 fd_install(fd2, newfile2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 /* fd1 and fd2 may be already another descriptors.
1360 * Not kernel problem.
1361 */
1362
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001363 err = put_user(fd1, &usockvec[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 if (!err)
1365 err = put_user(fd2, &usockvec[1]);
1366 if (!err)
1367 return 0;
1368
1369 sys_close(fd2);
1370 sys_close(fd1);
1371 return err;
1372
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373out_release_both:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001374 sock_release(sock2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375out_release_1:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001376 sock_release(sock1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377out:
1378 return err;
1379}
1380
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381/*
1382 * Bind a name to a socket. Nothing much to do here since it's
1383 * the protocol's responsibility to handle the local address.
1384 *
1385 * We move the socket address to kernel space before we call
1386 * the protocol layer (having also checked the address is ok).
1387 */
1388
Heiko Carstens20f37032009-01-14 14:14:23 +01001389SYSCALL_DEFINE3(bind, int, fd, struct sockaddr __user *, umyaddr, int, addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390{
1391 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001392 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001393 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001395 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001396 if (sock) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001397 err = move_addr_to_kernel(umyaddr, addrlen, (struct sockaddr *)&address);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001398 if (err >= 0) {
1399 err = security_socket_bind(sock,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001400 (struct sockaddr *)&address,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001401 addrlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001402 if (!err)
1403 err = sock->ops->bind(sock,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001404 (struct sockaddr *)
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001405 &address, addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 }
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001407 fput_light(sock->file, fput_needed);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 return err;
1410}
1411
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412/*
1413 * Perform a listen. Basically, we allow the protocol to do anything
1414 * necessary for a listen, and if that works, we mark the socket as
1415 * ready for listening.
1416 */
1417
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001418SYSCALL_DEFINE2(listen, int, fd, int, backlog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419{
1420 struct socket *sock;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001421 int err, fput_needed;
Pavel Emelyanovb8e1f9b2007-12-08 00:12:33 -08001422 int somaxconn;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001423
1424 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1425 if (sock) {
Pavel Emelyanov8efa6e92008-03-31 19:41:14 -07001426 somaxconn = sock_net(sock->sk)->core.sysctl_somaxconn;
Pavel Emelyanovb8e1f9b2007-12-08 00:12:33 -08001427 if ((unsigned)backlog > somaxconn)
1428 backlog = somaxconn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
1430 err = security_socket_listen(sock, backlog);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001431 if (!err)
1432 err = sock->ops->listen(sock, backlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001434 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 }
1436 return err;
1437}
1438
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439/*
1440 * For accept, we attempt to create a new socket, set up the link
1441 * with the client, wake up the client, then return the new
1442 * connected fd. We collect the address of the connector in kernel
1443 * space and move it to user at the very end. This is unclean because
1444 * we open the socket then return an error.
1445 *
1446 * 1003.1g adds the ability to recvmsg() to query connection pending
1447 * status to recvmsg. We need to add that support in a way thats
1448 * clean when we restucture accept also.
1449 */
1450
Heiko Carstens20f37032009-01-14 14:14:23 +01001451SYSCALL_DEFINE4(accept4, int, fd, struct sockaddr __user *, upeer_sockaddr,
1452 int __user *, upeer_addrlen, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453{
1454 struct socket *sock, *newsock;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001455 struct file *newfile;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001456 int err, len, newfd, fput_needed;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001457 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
Ulrich Drepper77d27202008-07-23 21:29:35 -07001459 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001460 return -EINVAL;
1461
1462 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1463 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1464
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001465 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 if (!sock)
1467 goto out;
1468
1469 err = -ENFILE;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001470 if (!(newsock = sock_alloc()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 goto out_put;
1472
1473 newsock->type = sock->type;
1474 newsock->ops = sock->ops;
1475
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 /*
1477 * We don't need try_module_get here, as the listening socket (sock)
1478 * has the protocol module (sock->ops->owner) held.
1479 */
1480 __module_get(newsock->ops->owner);
1481
Al Viro7cbe66b2009-08-05 19:59:08 +04001482 newfd = sock_alloc_file(newsock, &newfile, flags);
David S. Miller39d8c1b2006-03-20 17:13:49 -08001483 if (unlikely(newfd < 0)) {
1484 err = newfd;
David S. Miller9a1875e2006-04-01 12:48:36 -08001485 sock_release(newsock);
1486 goto out_put;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001487 }
1488
Frank Filza79af592005-09-27 15:23:38 -07001489 err = security_socket_accept(sock, newsock);
1490 if (err)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001491 goto out_fd;
Frank Filza79af592005-09-27 15:23:38 -07001492
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 err = sock->ops->accept(sock, newsock, sock->file->f_flags);
1494 if (err < 0)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001495 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
1497 if (upeer_sockaddr) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001498 if (newsock->ops->getname(newsock, (struct sockaddr *)&address,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001499 &len, 2) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 err = -ECONNABORTED;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001501 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 }
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001503 err = move_addr_to_user((struct sockaddr *)&address,
1504 len, upeer_sockaddr, upeer_addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 if (err < 0)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001506 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 }
1508
1509 /* File flags are not inherited via accept() unlike another OSes. */
1510
David S. Miller39d8c1b2006-03-20 17:13:49 -08001511 fd_install(newfd, newfile);
1512 err = newfd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001515 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516out:
1517 return err;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001518out_fd:
David S. Miller9606a212006-04-01 01:00:14 -08001519 fput(newfile);
David S. Miller39d8c1b2006-03-20 17:13:49 -08001520 put_unused_fd(newfd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 goto out_put;
1522}
1523
Heiko Carstens20f37032009-01-14 14:14:23 +01001524SYSCALL_DEFINE3(accept, int, fd, struct sockaddr __user *, upeer_sockaddr,
1525 int __user *, upeer_addrlen)
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001526{
Ulrich Drepperde11def2008-11-19 15:36:14 -08001527 return sys_accept4(fd, upeer_sockaddr, upeer_addrlen, 0);
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001528}
1529
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530/*
1531 * Attempt to connect to a socket with the server address. The address
1532 * is in user space so we verify it is OK and move it to kernel space.
1533 *
1534 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
1535 * break bindings
1536 *
1537 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
1538 * other SEQPACKET protocols that take time to connect() as it doesn't
1539 * include the -EINPROGRESS status for such sockets.
1540 */
1541
Heiko Carstens20f37032009-01-14 14:14:23 +01001542SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
1543 int, addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544{
1545 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001546 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001547 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001549 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 if (!sock)
1551 goto out;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001552 err = move_addr_to_kernel(uservaddr, addrlen, (struct sockaddr *)&address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 if (err < 0)
1554 goto out_put;
1555
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001556 err =
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001557 security_socket_connect(sock, (struct sockaddr *)&address, addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 if (err)
1559 goto out_put;
1560
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001561 err = sock->ops->connect(sock, (struct sockaddr *)&address, addrlen,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 sock->file->f_flags);
1563out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001564 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565out:
1566 return err;
1567}
1568
1569/*
1570 * Get the local address ('name') of a socket object. Move the obtained
1571 * name to user space.
1572 */
1573
Heiko Carstens20f37032009-01-14 14:14:23 +01001574SYSCALL_DEFINE3(getsockname, int, fd, struct sockaddr __user *, usockaddr,
1575 int __user *, usockaddr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576{
1577 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001578 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001579 int len, err, fput_needed;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001580
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001581 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 if (!sock)
1583 goto out;
1584
1585 err = security_socket_getsockname(sock);
1586 if (err)
1587 goto out_put;
1588
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001589 err = sock->ops->getname(sock, (struct sockaddr *)&address, &len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 if (err)
1591 goto out_put;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001592 err = move_addr_to_user((struct sockaddr *)&address, len, usockaddr, usockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
1594out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001595 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596out:
1597 return err;
1598}
1599
1600/*
1601 * Get the remote address ('name') of a socket object. Move the obtained
1602 * name to user space.
1603 */
1604
Heiko Carstens20f37032009-01-14 14:14:23 +01001605SYSCALL_DEFINE3(getpeername, int, fd, struct sockaddr __user *, usockaddr,
1606 int __user *, usockaddr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607{
1608 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001609 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001610 int len, err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001612 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1613 if (sock != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 err = security_socket_getpeername(sock);
1615 if (err) {
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001616 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 return err;
1618 }
1619
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001620 err =
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001621 sock->ops->getname(sock, (struct sockaddr *)&address, &len,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001622 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 if (!err)
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001624 err = move_addr_to_user((struct sockaddr *)&address, len, usockaddr,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001625 usockaddr_len);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001626 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 }
1628 return err;
1629}
1630
1631/*
1632 * Send a datagram to a given address. We move the address into kernel
1633 * space and check the user space data area is readable before invoking
1634 * the protocol.
1635 */
1636
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001637SYSCALL_DEFINE6(sendto, int, fd, void __user *, buff, size_t, len,
1638 unsigned, flags, struct sockaddr __user *, addr,
1639 int, addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640{
1641 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001642 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 int err;
1644 struct msghdr msg;
1645 struct iovec iov;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001646 int fput_needed;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001647
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001648 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1649 if (!sock)
David S. Miller4387ff72007-02-08 15:06:08 -08001650 goto out;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001651
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001652 iov.iov_base = buff;
1653 iov.iov_len = len;
1654 msg.msg_name = NULL;
1655 msg.msg_iov = &iov;
1656 msg.msg_iovlen = 1;
1657 msg.msg_control = NULL;
1658 msg.msg_controllen = 0;
1659 msg.msg_namelen = 0;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001660 if (addr) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001661 err = move_addr_to_kernel(addr, addr_len, (struct sockaddr *)&address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 if (err < 0)
1663 goto out_put;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001664 msg.msg_name = (struct sockaddr *)&address;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001665 msg.msg_namelen = addr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 }
1667 if (sock->file->f_flags & O_NONBLOCK)
1668 flags |= MSG_DONTWAIT;
1669 msg.msg_flags = flags;
1670 err = sock_sendmsg(sock, &msg, len);
1671
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001672out_put:
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001673 fput_light(sock->file, fput_needed);
David S. Miller4387ff72007-02-08 15:06:08 -08001674out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 return err;
1676}
1677
1678/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001679 * Send a datagram down a socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 */
1681
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001682SYSCALL_DEFINE4(send, int, fd, void __user *, buff, size_t, len,
1683 unsigned, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684{
1685 return sys_sendto(fd, buff, len, flags, NULL, 0);
1686}
1687
1688/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001689 * Receive a frame from the socket and optionally record the address of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 * sender. We verify the buffers are writable and if needed move the
1691 * sender address from kernel to user space.
1692 */
1693
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001694SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, ubuf, size_t, size,
1695 unsigned, flags, struct sockaddr __user *, addr,
1696 int __user *, addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697{
1698 struct socket *sock;
1699 struct iovec iov;
1700 struct msghdr msg;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001701 struct sockaddr_storage address;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001702 int err, err2;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001703 int fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001705 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 if (!sock)
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001707 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001709 msg.msg_control = NULL;
1710 msg.msg_controllen = 0;
1711 msg.msg_iovlen = 1;
1712 msg.msg_iov = &iov;
1713 iov.iov_len = size;
1714 iov.iov_base = ubuf;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001715 msg.msg_name = (struct sockaddr *)&address;
1716 msg.msg_namelen = sizeof(address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 if (sock->file->f_flags & O_NONBLOCK)
1718 flags |= MSG_DONTWAIT;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001719 err = sock_recvmsg(sock, &msg, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001721 if (err >= 0 && addr != NULL) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001722 err2 = move_addr_to_user((struct sockaddr *)&address,
1723 msg.msg_namelen, addr, addr_len);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001724 if (err2 < 0)
1725 err = err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 }
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001727
1728 fput_light(sock->file, fput_needed);
David S. Miller4387ff72007-02-08 15:06:08 -08001729out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 return err;
1731}
1732
1733/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001734 * Receive a datagram from a socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 */
1736
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001737asmlinkage long sys_recv(int fd, void __user *ubuf, size_t size,
1738 unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739{
1740 return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
1741}
1742
1743/*
1744 * Set a socket option. Because we don't know the option lengths we have
1745 * to pass the user mode parameter for the protocols to sort out.
1746 */
1747
Heiko Carstens20f37032009-01-14 14:14:23 +01001748SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname,
1749 char __user *, optval, int, optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001751 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 struct socket *sock;
1753
1754 if (optlen < 0)
1755 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001756
1757 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1758 if (sock != NULL) {
1759 err = security_socket_setsockopt(sock, level, optname);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001760 if (err)
1761 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
1763 if (level == SOL_SOCKET)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001764 err =
1765 sock_setsockopt(sock, level, optname, optval,
1766 optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 else
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001768 err =
1769 sock->ops->setsockopt(sock, level, optname, optval,
1770 optlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001771out_put:
1772 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 }
1774 return err;
1775}
1776
1777/*
1778 * Get a socket option. Because we don't know the option lengths we have
1779 * to pass a user mode parameter for the protocols to sort out.
1780 */
1781
Heiko Carstens20f37032009-01-14 14:14:23 +01001782SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname,
1783 char __user *, optval, int __user *, optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001785 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 struct socket *sock;
1787
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001788 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1789 if (sock != NULL) {
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001790 err = security_socket_getsockopt(sock, level, optname);
1791 if (err)
1792 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
1794 if (level == SOL_SOCKET)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001795 err =
1796 sock_getsockopt(sock, level, optname, optval,
1797 optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 else
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001799 err =
1800 sock->ops->getsockopt(sock, level, optname, optval,
1801 optlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001802out_put:
1803 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 }
1805 return err;
1806}
1807
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808/*
1809 * Shutdown a socket.
1810 */
1811
Heiko Carstens754fe8d2009-01-14 14:14:09 +01001812SYSCALL_DEFINE2(shutdown, int, fd, int, how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001814 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 struct socket *sock;
1816
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001817 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1818 if (sock != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 err = security_socket_shutdown(sock, how);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001820 if (!err)
1821 err = sock->ops->shutdown(sock, how);
1822 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 }
1824 return err;
1825}
1826
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001827/* A couple of helpful macros for getting the address of the 32/64 bit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 * fields which are the same type (int / unsigned) on our platforms.
1829 */
1830#define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
1831#define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
1832#define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
1833
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834/*
1835 * BSD sendmsg interface
1836 */
1837
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001838SYSCALL_DEFINE3(sendmsg, int, fd, struct msghdr __user *, msg, unsigned, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839{
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001840 struct compat_msghdr __user *msg_compat =
1841 (struct compat_msghdr __user *)msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001843 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
Alex Williamsonb9d717a2005-09-26 14:28:02 -07001845 unsigned char ctl[sizeof(struct cmsghdr) + 20]
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001846 __attribute__ ((aligned(sizeof(__kernel_size_t))));
1847 /* 20 is size of ipv6_pktinfo */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 unsigned char *ctl_buf = ctl;
1849 struct msghdr msg_sys;
1850 int err, ctl_len, iov_size, total_len;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001851 int fput_needed;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001852
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 err = -EFAULT;
1854 if (MSG_CMSG_COMPAT & flags) {
1855 if (get_compat_msghdr(&msg_sys, msg_compat))
1856 return -EFAULT;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001857 }
1858 else if (copy_from_user(&msg_sys, msg, sizeof(struct msghdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 return -EFAULT;
1860
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001861 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001862 if (!sock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 goto out;
1864
1865 /* do not move before msg_sys is valid */
1866 err = -EMSGSIZE;
1867 if (msg_sys.msg_iovlen > UIO_MAXIOV)
1868 goto out_put;
1869
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001870 /* Check whether to allocate the iovec area */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 err = -ENOMEM;
1872 iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
1873 if (msg_sys.msg_iovlen > UIO_FASTIOV) {
1874 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
1875 if (!iov)
1876 goto out_put;
1877 }
1878
1879 /* This will also move the address data into kernel space */
1880 if (MSG_CMSG_COMPAT & flags) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001881 err = verify_compat_iovec(&msg_sys, iov,
1882 (struct sockaddr *)&address,
1883 VERIFY_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 } else
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001885 err = verify_iovec(&msg_sys, iov,
1886 (struct sockaddr *)&address,
1887 VERIFY_READ);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001888 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 goto out_freeiov;
1890 total_len = err;
1891
1892 err = -ENOBUFS;
1893
1894 if (msg_sys.msg_controllen > INT_MAX)
1895 goto out_freeiov;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001896 ctl_len = msg_sys.msg_controllen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001898 err =
1899 cmsghdr_from_user_compat_to_kern(&msg_sys, sock->sk, ctl,
1900 sizeof(ctl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 if (err)
1902 goto out_freeiov;
1903 ctl_buf = msg_sys.msg_control;
Al Viro8920e8f2005-09-07 18:28:51 -07001904 ctl_len = msg_sys.msg_controllen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 } else if (ctl_len) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001906 if (ctl_len > sizeof(ctl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001908 if (ctl_buf == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 goto out_freeiov;
1910 }
1911 err = -EFAULT;
1912 /*
1913 * Careful! Before this, msg_sys.msg_control contains a user pointer.
1914 * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
1915 * checking falls down on this.
1916 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001917 if (copy_from_user(ctl_buf, (void __user *)msg_sys.msg_control,
1918 ctl_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 goto out_freectl;
1920 msg_sys.msg_control = ctl_buf;
1921 }
1922 msg_sys.msg_flags = flags;
1923
1924 if (sock->file->f_flags & O_NONBLOCK)
1925 msg_sys.msg_flags |= MSG_DONTWAIT;
1926 err = sock_sendmsg(sock, &msg_sys, total_len);
1927
1928out_freectl:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001929 if (ctl_buf != ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 sock_kfree_s(sock->sk, ctl_buf, ctl_len);
1931out_freeiov:
1932 if (iov != iovstack)
1933 sock_kfree_s(sock->sk, iov, iov_size);
1934out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001935 fput_light(sock->file, fput_needed);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001936out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 return err;
1938}
1939
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07001940static int __sys_recvmsg(struct socket *sock, struct msghdr __user *msg,
1941 struct msghdr *msg_sys, unsigned flags, int nosec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942{
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001943 struct compat_msghdr __user *msg_compat =
1944 (struct compat_msghdr __user *)msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 struct iovec iovstack[UIO_FASTIOV];
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001946 struct iovec *iov = iovstack;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 unsigned long cmsg_ptr;
1948 int err, iov_size, total_len, len;
1949
1950 /* kernel mode address */
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001951 struct sockaddr_storage addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
1953 /* user mode address pointers */
1954 struct sockaddr __user *uaddr;
1955 int __user *uaddr_len;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001956
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 if (MSG_CMSG_COMPAT & flags) {
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07001958 if (get_compat_msghdr(msg_sys, msg_compat))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 return -EFAULT;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001960 }
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07001961 else if (copy_from_user(msg_sys, msg, sizeof(struct msghdr)))
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001962 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 err = -EMSGSIZE;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07001965 if (msg_sys->msg_iovlen > UIO_MAXIOV)
1966 goto out;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001967
1968 /* Check whether to allocate the iovec area */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 err = -ENOMEM;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07001970 iov_size = msg_sys->msg_iovlen * sizeof(struct iovec);
1971 if (msg_sys->msg_iovlen > UIO_FASTIOV) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
1973 if (!iov)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07001974 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 }
1976
1977 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001978 * Save the user-mode address (verify_iovec will change the
1979 * kernel msghdr to use the kernel address space)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001981
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07001982 uaddr = (__force void __user *)msg_sys->msg_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 uaddr_len = COMPAT_NAMELEN(msg);
1984 if (MSG_CMSG_COMPAT & flags) {
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07001985 err = verify_compat_iovec(msg_sys, iov,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001986 (struct sockaddr *)&addr,
1987 VERIFY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 } else
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07001989 err = verify_iovec(msg_sys, iov,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001990 (struct sockaddr *)&addr,
1991 VERIFY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 if (err < 0)
1993 goto out_freeiov;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001994 total_len = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07001996 cmsg_ptr = (unsigned long)msg_sys->msg_control;
1997 msg_sys->msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001998
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 if (sock->file->f_flags & O_NONBLOCK)
2000 flags |= MSG_DONTWAIT;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002001 err = (nosec ? sock_recvmsg_nosec : sock_recvmsg)(sock, msg_sys,
2002 total_len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 if (err < 0)
2004 goto out_freeiov;
2005 len = err;
2006
2007 if (uaddr != NULL) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07002008 err = move_addr_to_user((struct sockaddr *)&addr,
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002009 msg_sys->msg_namelen, uaddr,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002010 uaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 if (err < 0)
2012 goto out_freeiov;
2013 }
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002014 err = __put_user((msg_sys->msg_flags & ~MSG_CMSG_COMPAT),
David S. Miller37f7f422005-09-16 16:51:01 -07002015 COMPAT_FLAGS(msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 if (err)
2017 goto out_freeiov;
2018 if (MSG_CMSG_COMPAT & flags)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002019 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 &msg_compat->msg_controllen);
2021 else
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002022 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 &msg->msg_controllen);
2024 if (err)
2025 goto out_freeiov;
2026 err = len;
2027
2028out_freeiov:
2029 if (iov != iovstack)
2030 sock_kfree_s(sock->sk, iov, iov_size);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002031out:
2032 return err;
2033}
2034
2035/*
2036 * BSD recvmsg interface
2037 */
2038
2039SYSCALL_DEFINE3(recvmsg, int, fd, struct msghdr __user *, msg,
2040 unsigned int, flags)
2041{
2042 int fput_needed, err;
2043 struct msghdr msg_sys;
2044 struct socket *sock = sockfd_lookup_light(fd, &err, &fput_needed);
2045
2046 if (!sock)
2047 goto out;
2048
2049 err = __sys_recvmsg(sock, msg, &msg_sys, flags, 0);
2050
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002051 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052out:
2053 return err;
2054}
2055
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002056/*
2057 * Linux recvmmsg interface
2058 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002060int __sys_recvmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
2061 unsigned int flags, struct timespec *timeout)
2062{
2063 int fput_needed, err, datagrams;
2064 struct socket *sock;
2065 struct mmsghdr __user *entry;
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002066 struct compat_mmsghdr __user *compat_entry;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002067 struct msghdr msg_sys;
2068 struct timespec end_time;
2069
2070 if (timeout &&
2071 poll_select_set_timeout(&end_time, timeout->tv_sec,
2072 timeout->tv_nsec))
2073 return -EINVAL;
2074
2075 datagrams = 0;
2076
2077 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2078 if (!sock)
2079 return err;
2080
2081 err = sock_error(sock->sk);
2082 if (err)
2083 goto out_put;
2084
2085 entry = mmsg;
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002086 compat_entry = (struct compat_mmsghdr __user *)mmsg;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002087
2088 while (datagrams < vlen) {
2089 /*
2090 * No need to ask LSM for more than the first datagram.
2091 */
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002092 if (MSG_CMSG_COMPAT & flags) {
2093 err = __sys_recvmsg(sock, (struct msghdr __user *)compat_entry,
2094 &msg_sys, flags, datagrams);
2095 if (err < 0)
2096 break;
2097 err = __put_user(err, &compat_entry->msg_len);
2098 ++compat_entry;
2099 } else {
2100 err = __sys_recvmsg(sock, (struct msghdr __user *)entry,
2101 &msg_sys, flags, datagrams);
2102 if (err < 0)
2103 break;
2104 err = put_user(err, &entry->msg_len);
2105 ++entry;
2106 }
2107
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002108 if (err)
2109 break;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002110 ++datagrams;
2111
Brandon L Black71c5c1592010-03-26 16:18:03 +00002112 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
2113 if (flags & MSG_WAITFORONE)
2114 flags |= MSG_DONTWAIT;
2115
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002116 if (timeout) {
2117 ktime_get_ts(timeout);
2118 *timeout = timespec_sub(end_time, *timeout);
2119 if (timeout->tv_sec < 0) {
2120 timeout->tv_sec = timeout->tv_nsec = 0;
2121 break;
2122 }
2123
2124 /* Timeout, return less than vlen datagrams */
2125 if (timeout->tv_nsec == 0 && timeout->tv_sec == 0)
2126 break;
2127 }
2128
2129 /* Out of band data, return right away */
2130 if (msg_sys.msg_flags & MSG_OOB)
2131 break;
2132 }
2133
2134out_put:
2135 fput_light(sock->file, fput_needed);
2136
2137 if (err == 0)
2138 return datagrams;
2139
2140 if (datagrams != 0) {
2141 /*
2142 * We may return less entries than requested (vlen) if the
2143 * sock is non block and there aren't enough datagrams...
2144 */
2145 if (err != -EAGAIN) {
2146 /*
2147 * ... or if recvmsg returns an error after we
2148 * received some datagrams, where we record the
2149 * error to return on the next call or if the
2150 * app asks about it using getsockopt(SO_ERROR).
2151 */
2152 sock->sk->sk_err = -err;
2153 }
2154
2155 return datagrams;
2156 }
2157
2158 return err;
2159}
2160
2161SYSCALL_DEFINE5(recvmmsg, int, fd, struct mmsghdr __user *, mmsg,
2162 unsigned int, vlen, unsigned int, flags,
2163 struct timespec __user *, timeout)
2164{
2165 int datagrams;
2166 struct timespec timeout_sys;
2167
2168 if (!timeout)
2169 return __sys_recvmmsg(fd, mmsg, vlen, flags, NULL);
2170
2171 if (copy_from_user(&timeout_sys, timeout, sizeof(timeout_sys)))
2172 return -EFAULT;
2173
2174 datagrams = __sys_recvmmsg(fd, mmsg, vlen, flags, &timeout_sys);
2175
2176 if (datagrams > 0 &&
2177 copy_to_user(timeout, &timeout_sys, sizeof(timeout_sys)))
2178 datagrams = -EFAULT;
2179
2180 return datagrams;
2181}
2182
2183#ifdef __ARCH_WANT_SYS_SOCKETCALL
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184/* Argument list sizes for sys_socketcall */
2185#define AL(x) ((x) * sizeof(unsigned long))
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002186static const unsigned char nargs[20] = {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002187 AL(0),AL(3),AL(3),AL(3),AL(2),AL(3),
2188 AL(3),AL(3),AL(4),AL(4),AL(4),AL(6),
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07002189 AL(6),AL(2),AL(5),AL(5),AL(3),AL(3),
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002190 AL(4),AL(5)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002191};
2192
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193#undef AL
2194
2195/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002196 * System call vectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 *
2198 * Argument checking cleaned up. Saved 20% in size.
2199 * This function doesn't need to set the kernel lock because
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002200 * it is set by the callees.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 */
2202
Heiko Carstens3e0fa652009-01-14 14:14:24 +01002203SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204{
2205 unsigned long a[6];
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002206 unsigned long a0, a1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 int err;
Arjan van de Ven47379052009-09-28 12:57:44 -07002208 unsigned int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002210 if (call < 1 || call > SYS_RECVMMSG)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 return -EINVAL;
2212
Arjan van de Ven47379052009-09-28 12:57:44 -07002213 len = nargs[call];
2214 if (len > sizeof(a))
2215 return -EINVAL;
2216
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 /* copy_from_user should be SMP safe. */
Arjan van de Ven47379052009-09-28 12:57:44 -07002218 if (copy_from_user(a, args, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 return -EFAULT;
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002220
Al Virof3298dc2008-12-10 03:16:51 -05002221 audit_socketcall(nargs[call] / sizeof(unsigned long), a);
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002222
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002223 a0 = a[0];
2224 a1 = a[1];
2225
2226 switch (call) {
2227 case SYS_SOCKET:
2228 err = sys_socket(a0, a1, a[2]);
2229 break;
2230 case SYS_BIND:
2231 err = sys_bind(a0, (struct sockaddr __user *)a1, a[2]);
2232 break;
2233 case SYS_CONNECT:
2234 err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
2235 break;
2236 case SYS_LISTEN:
2237 err = sys_listen(a0, a1);
2238 break;
2239 case SYS_ACCEPT:
Ulrich Drepperde11def2008-11-19 15:36:14 -08002240 err = sys_accept4(a0, (struct sockaddr __user *)a1,
2241 (int __user *)a[2], 0);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002242 break;
2243 case SYS_GETSOCKNAME:
2244 err =
2245 sys_getsockname(a0, (struct sockaddr __user *)a1,
2246 (int __user *)a[2]);
2247 break;
2248 case SYS_GETPEERNAME:
2249 err =
2250 sys_getpeername(a0, (struct sockaddr __user *)a1,
2251 (int __user *)a[2]);
2252 break;
2253 case SYS_SOCKETPAIR:
2254 err = sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
2255 break;
2256 case SYS_SEND:
2257 err = sys_send(a0, (void __user *)a1, a[2], a[3]);
2258 break;
2259 case SYS_SENDTO:
2260 err = sys_sendto(a0, (void __user *)a1, a[2], a[3],
2261 (struct sockaddr __user *)a[4], a[5]);
2262 break;
2263 case SYS_RECV:
2264 err = sys_recv(a0, (void __user *)a1, a[2], a[3]);
2265 break;
2266 case SYS_RECVFROM:
2267 err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
2268 (struct sockaddr __user *)a[4],
2269 (int __user *)a[5]);
2270 break;
2271 case SYS_SHUTDOWN:
2272 err = sys_shutdown(a0, a1);
2273 break;
2274 case SYS_SETSOCKOPT:
2275 err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]);
2276 break;
2277 case SYS_GETSOCKOPT:
2278 err =
2279 sys_getsockopt(a0, a1, a[2], (char __user *)a[3],
2280 (int __user *)a[4]);
2281 break;
2282 case SYS_SENDMSG:
2283 err = sys_sendmsg(a0, (struct msghdr __user *)a1, a[2]);
2284 break;
2285 case SYS_RECVMSG:
2286 err = sys_recvmsg(a0, (struct msghdr __user *)a1, a[2]);
2287 break;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002288 case SYS_RECVMMSG:
2289 err = sys_recvmmsg(a0, (struct mmsghdr __user *)a1, a[2], a[3],
2290 (struct timespec __user *)a[4]);
2291 break;
Ulrich Drepperde11def2008-11-19 15:36:14 -08002292 case SYS_ACCEPT4:
2293 err = sys_accept4(a0, (struct sockaddr __user *)a1,
2294 (int __user *)a[2], a[3]);
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07002295 break;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002296 default:
2297 err = -EINVAL;
2298 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 }
2300 return err;
2301}
2302
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002303#endif /* __ARCH_WANT_SYS_SOCKETCALL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002305/**
2306 * sock_register - add a socket protocol handler
2307 * @ops: description of protocol
2308 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 * This function is called by a protocol handler that wants to
2310 * advertise its address family, and have it linked into the
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002311 * socket interface. The value ops->family coresponds to the
2312 * socket system call protocol family.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 */
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002314int sock_register(const struct net_proto_family *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315{
2316 int err;
2317
2318 if (ops->family >= NPROTO) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002319 printk(KERN_CRIT "protocol %d >= NPROTO(%d)\n", ops->family,
2320 NPROTO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 return -ENOBUFS;
2322 }
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002323
2324 spin_lock(&net_family_lock);
2325 if (net_families[ops->family])
2326 err = -EEXIST;
2327 else {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002328 net_families[ops->family] = ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 err = 0;
2330 }
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002331 spin_unlock(&net_family_lock);
2332
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002333 printk(KERN_INFO "NET: Registered protocol family %d\n", ops->family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 return err;
2335}
2336
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002337/**
2338 * sock_unregister - remove a protocol handler
2339 * @family: protocol family to remove
2340 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 * This function is called by a protocol handler that wants to
2342 * remove its address family, and have it unlinked from the
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002343 * new socket creation.
2344 *
2345 * If protocol handler is a module, then it can use module reference
2346 * counts to protect against new references. If protocol handler is not
2347 * a module then it needs to provide its own protection in
2348 * the ops->create routine.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 */
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002350void sock_unregister(int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351{
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002352 BUG_ON(family < 0 || family >= NPROTO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002354 spin_lock(&net_family_lock);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002355 net_families[family] = NULL;
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002356 spin_unlock(&net_family_lock);
2357
2358 synchronize_rcu();
2359
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002360 printk(KERN_INFO "NET: Unregistered protocol family %d\n", family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361}
2362
Andi Kleen77d76ea2005-12-22 12:43:42 -08002363static int __init sock_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364{
2365 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002366 * Initialize sock SLAB cache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002368
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 sk_init();
2370
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002372 * Initialize skbuff SLAB cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 */
2374 skb_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
2376 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002377 * Initialize the protocols module.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 */
2379
2380 init_inodecache();
2381 register_filesystem(&sock_fs_type);
2382 sock_mnt = kern_mount(&sock_fs_type);
Andi Kleen77d76ea2005-12-22 12:43:42 -08002383
2384 /* The real protocol initialization is performed in later initcalls.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 */
2386
2387#ifdef CONFIG_NETFILTER
2388 netfilter_init();
2389#endif
David S. Millercbeb3212005-12-22 12:58:55 -08002390
2391 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392}
2393
Andi Kleen77d76ea2005-12-22 12:43:42 -08002394core_initcall(sock_init); /* early initcall */
2395
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396#ifdef CONFIG_PROC_FS
2397void socket_seq_show(struct seq_file *seq)
2398{
2399 int cpu;
2400 int counter = 0;
2401
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -07002402 for_each_possible_cpu(cpu)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002403 counter += per_cpu(sockets_in_use, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404
2405 /* It can be negative, by the way. 8) */
2406 if (counter < 0)
2407 counter = 0;
2408
2409 seq_printf(seq, "sockets: used %d\n", counter);
2410}
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002411#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412
Shaun Pereira89bbfc92006-03-21 23:58:08 -08002413#ifdef CONFIG_COMPAT
Arnd Bergmann6b960182009-11-06 23:10:54 -08002414static int do_siocgstamp(struct net *net, struct socket *sock,
2415 unsigned int cmd, struct compat_timeval __user *up)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002416{
Arnd Bergmann7a229382009-11-06 23:00:29 -08002417 mm_segment_t old_fs = get_fs();
2418 struct timeval ktv;
2419 int err;
2420
2421 set_fs(KERNEL_DS);
Arnd Bergmann6b960182009-11-06 23:10:54 -08002422 err = sock_do_ioctl(net, sock, cmd, (unsigned long)&ktv);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002423 set_fs(old_fs);
2424 if (!err) {
2425 err = put_user(ktv.tv_sec, &up->tv_sec);
2426 err |= __put_user(ktv.tv_usec, &up->tv_usec);
2427 }
2428 return err;
2429}
2430
Arnd Bergmann6b960182009-11-06 23:10:54 -08002431static int do_siocgstampns(struct net *net, struct socket *sock,
2432 unsigned int cmd, struct compat_timespec __user *up)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002433{
Arnd Bergmann7a229382009-11-06 23:00:29 -08002434 mm_segment_t old_fs = get_fs();
2435 struct timespec kts;
2436 int err;
2437
2438 set_fs(KERNEL_DS);
Arnd Bergmann6b960182009-11-06 23:10:54 -08002439 err = sock_do_ioctl(net, sock, cmd, (unsigned long)&kts);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002440 set_fs(old_fs);
2441 if (!err) {
2442 err = put_user(kts.tv_sec, &up->tv_sec);
2443 err |= __put_user(kts.tv_nsec, &up->tv_nsec);
2444 }
2445 return err;
2446}
2447
Arnd Bergmann6b960182009-11-06 23:10:54 -08002448static int dev_ifname32(struct net *net, struct compat_ifreq __user *uifr32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002449{
2450 struct ifreq __user *uifr;
2451 int err;
2452
2453 uifr = compat_alloc_user_space(sizeof(struct ifreq));
Arnd Bergmann6b960182009-11-06 23:10:54 -08002454 if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002455 return -EFAULT;
2456
Arnd Bergmann6b960182009-11-06 23:10:54 -08002457 err = dev_ioctl(net, SIOCGIFNAME, uifr);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002458 if (err)
2459 return err;
2460
Arnd Bergmann6b960182009-11-06 23:10:54 -08002461 if (copy_in_user(uifr32, uifr, sizeof(struct compat_ifreq)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002462 return -EFAULT;
2463
2464 return 0;
2465}
2466
Arnd Bergmann6b960182009-11-06 23:10:54 -08002467static int dev_ifconf(struct net *net, struct compat_ifconf __user *uifc32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002468{
Arnd Bergmann6b960182009-11-06 23:10:54 -08002469 struct compat_ifconf ifc32;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002470 struct ifconf ifc;
2471 struct ifconf __user *uifc;
Arnd Bergmann6b960182009-11-06 23:10:54 -08002472 struct compat_ifreq __user *ifr32;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002473 struct ifreq __user *ifr;
2474 unsigned int i, j;
2475 int err;
2476
Arnd Bergmann6b960182009-11-06 23:10:54 -08002477 if (copy_from_user(&ifc32, uifc32, sizeof(struct compat_ifconf)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002478 return -EFAULT;
2479
2480 if (ifc32.ifcbuf == 0) {
2481 ifc32.ifc_len = 0;
2482 ifc.ifc_len = 0;
2483 ifc.ifc_req = NULL;
2484 uifc = compat_alloc_user_space(sizeof(struct ifconf));
2485 } else {
Arnd Bergmann6b960182009-11-06 23:10:54 -08002486 size_t len =((ifc32.ifc_len / sizeof (struct compat_ifreq)) + 1) *
Arnd Bergmann7a229382009-11-06 23:00:29 -08002487 sizeof (struct ifreq);
2488 uifc = compat_alloc_user_space(sizeof(struct ifconf) + len);
2489 ifc.ifc_len = len;
2490 ifr = ifc.ifc_req = (void __user *)(uifc + 1);
2491 ifr32 = compat_ptr(ifc32.ifcbuf);
Arnd Bergmann6b960182009-11-06 23:10:54 -08002492 for (i = 0; i < ifc32.ifc_len; i += sizeof (struct compat_ifreq)) {
2493 if (copy_in_user(ifr, ifr32, sizeof(struct compat_ifreq)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002494 return -EFAULT;
2495 ifr++;
2496 ifr32++;
2497 }
2498 }
2499 if (copy_to_user(uifc, &ifc, sizeof(struct ifconf)))
2500 return -EFAULT;
2501
Arnd Bergmann6b960182009-11-06 23:10:54 -08002502 err = dev_ioctl(net, SIOCGIFCONF, uifc);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002503 if (err)
2504 return err;
2505
2506 if (copy_from_user(&ifc, uifc, sizeof(struct ifconf)))
2507 return -EFAULT;
2508
2509 ifr = ifc.ifc_req;
2510 ifr32 = compat_ptr(ifc32.ifcbuf);
2511 for (i = 0, j = 0;
Arnd Bergmann6b960182009-11-06 23:10:54 -08002512 i + sizeof (struct compat_ifreq) <= ifc32.ifc_len && j < ifc.ifc_len;
2513 i += sizeof (struct compat_ifreq), j += sizeof (struct ifreq)) {
2514 if (copy_in_user(ifr32, ifr, sizeof (struct compat_ifreq)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002515 return -EFAULT;
2516 ifr32++;
2517 ifr++;
2518 }
2519
2520 if (ifc32.ifcbuf == 0) {
2521 /* Translate from 64-bit structure multiple to
2522 * a 32-bit one.
2523 */
2524 i = ifc.ifc_len;
Arnd Bergmann6b960182009-11-06 23:10:54 -08002525 i = ((i / sizeof(struct ifreq)) * sizeof(struct compat_ifreq));
Arnd Bergmann7a229382009-11-06 23:00:29 -08002526 ifc32.ifc_len = i;
2527 } else {
2528 ifc32.ifc_len = i;
2529 }
Arnd Bergmann6b960182009-11-06 23:10:54 -08002530 if (copy_to_user(uifc32, &ifc32, sizeof(struct compat_ifconf)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002531 return -EFAULT;
2532
2533 return 0;
2534}
2535
Arnd Bergmann6b960182009-11-06 23:10:54 -08002536static int ethtool_ioctl(struct net *net, struct compat_ifreq __user *ifr32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002537{
2538 struct ifreq __user *ifr;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002539 u32 data;
2540 void __user *datap;
2541
2542 ifr = compat_alloc_user_space(sizeof(*ifr));
Arnd Bergmann7a229382009-11-06 23:00:29 -08002543
2544 if (copy_in_user(&ifr->ifr_name, &ifr32->ifr_name, IFNAMSIZ))
2545 return -EFAULT;
2546
2547 if (get_user(data, &ifr32->ifr_ifru.ifru_data))
2548 return -EFAULT;
2549
2550 datap = compat_ptr(data);
2551 if (put_user(datap, &ifr->ifr_ifru.ifru_data))
2552 return -EFAULT;
2553
Arnd Bergmann6b960182009-11-06 23:10:54 -08002554 return dev_ioctl(net, SIOCETHTOOL, ifr);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002555}
2556
Arnd Bergmann7a50a242009-11-08 20:57:03 -08002557static int compat_siocwandev(struct net *net, struct compat_ifreq __user *uifr32)
2558{
2559 void __user *uptr;
2560 compat_uptr_t uptr32;
2561 struct ifreq __user *uifr;
2562
2563 uifr = compat_alloc_user_space(sizeof (*uifr));
2564 if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
2565 return -EFAULT;
2566
2567 if (get_user(uptr32, &uifr32->ifr_settings.ifs_ifsu))
2568 return -EFAULT;
2569
2570 uptr = compat_ptr(uptr32);
2571
2572 if (put_user(uptr, &uifr->ifr_settings.ifs_ifsu.raw_hdlc))
2573 return -EFAULT;
2574
2575 return dev_ioctl(net, SIOCWANDEV, uifr);
2576}
2577
Arnd Bergmann6b960182009-11-06 23:10:54 -08002578static int bond_ioctl(struct net *net, unsigned int cmd,
2579 struct compat_ifreq __user *ifr32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002580{
2581 struct ifreq kifr;
2582 struct ifreq __user *uifr;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002583 mm_segment_t old_fs;
2584 int err;
2585 u32 data;
2586 void __user *datap;
2587
2588 switch (cmd) {
2589 case SIOCBONDENSLAVE:
2590 case SIOCBONDRELEASE:
2591 case SIOCBONDSETHWADDR:
2592 case SIOCBONDCHANGEACTIVE:
Arnd Bergmann6b960182009-11-06 23:10:54 -08002593 if (copy_from_user(&kifr, ifr32, sizeof(struct compat_ifreq)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002594 return -EFAULT;
2595
2596 old_fs = get_fs();
2597 set_fs (KERNEL_DS);
Arnd Bergmann6b960182009-11-06 23:10:54 -08002598 err = dev_ioctl(net, cmd, &kifr);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002599 set_fs (old_fs);
2600
2601 return err;
2602 case SIOCBONDSLAVEINFOQUERY:
2603 case SIOCBONDINFOQUERY:
2604 uifr = compat_alloc_user_space(sizeof(*uifr));
2605 if (copy_in_user(&uifr->ifr_name, &ifr32->ifr_name, IFNAMSIZ))
2606 return -EFAULT;
2607
2608 if (get_user(data, &ifr32->ifr_ifru.ifru_data))
2609 return -EFAULT;
2610
2611 datap = compat_ptr(data);
2612 if (put_user(datap, &uifr->ifr_ifru.ifru_data))
2613 return -EFAULT;
2614
Arnd Bergmann6b960182009-11-06 23:10:54 -08002615 return dev_ioctl(net, cmd, uifr);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002616 default:
2617 return -EINVAL;
2618 };
2619}
2620
Arnd Bergmann6b960182009-11-06 23:10:54 -08002621static int siocdevprivate_ioctl(struct net *net, unsigned int cmd,
2622 struct compat_ifreq __user *u_ifreq32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002623{
2624 struct ifreq __user *u_ifreq64;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002625 char tmp_buf[IFNAMSIZ];
2626 void __user *data64;
2627 u32 data32;
2628
2629 if (copy_from_user(&tmp_buf[0], &(u_ifreq32->ifr_ifrn.ifrn_name[0]),
2630 IFNAMSIZ))
2631 return -EFAULT;
2632 if (__get_user(data32, &u_ifreq32->ifr_ifru.ifru_data))
2633 return -EFAULT;
2634 data64 = compat_ptr(data32);
2635
2636 u_ifreq64 = compat_alloc_user_space(sizeof(*u_ifreq64));
2637
2638 /* Don't check these user accesses, just let that get trapped
2639 * in the ioctl handler instead.
2640 */
2641 if (copy_to_user(&u_ifreq64->ifr_ifrn.ifrn_name[0], &tmp_buf[0],
2642 IFNAMSIZ))
2643 return -EFAULT;
2644 if (__put_user(data64, &u_ifreq64->ifr_ifru.ifru_data))
2645 return -EFAULT;
2646
Arnd Bergmann6b960182009-11-06 23:10:54 -08002647 return dev_ioctl(net, cmd, u_ifreq64);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002648}
2649
Arnd Bergmann6b960182009-11-06 23:10:54 -08002650static int dev_ifsioc(struct net *net, struct socket *sock,
2651 unsigned int cmd, struct compat_ifreq __user *uifr32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002652{
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00002653 struct ifreq __user *uifr;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002654 int err;
2655
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00002656 uifr = compat_alloc_user_space(sizeof(*uifr));
2657 if (copy_in_user(uifr, uifr32, sizeof(*uifr32)))
2658 return -EFAULT;
2659
2660 err = sock_do_ioctl(net, sock, cmd, (unsigned long)uifr);
2661
Arnd Bergmann7a229382009-11-06 23:00:29 -08002662 if (!err) {
2663 switch (cmd) {
2664 case SIOCGIFFLAGS:
2665 case SIOCGIFMETRIC:
2666 case SIOCGIFMTU:
2667 case SIOCGIFMEM:
2668 case SIOCGIFHWADDR:
2669 case SIOCGIFINDEX:
2670 case SIOCGIFADDR:
2671 case SIOCGIFBRDADDR:
2672 case SIOCGIFDSTADDR:
2673 case SIOCGIFNETMASK:
Arnd Bergmannfab25322009-11-08 20:56:21 -08002674 case SIOCGIFPFLAGS:
Arnd Bergmann7a229382009-11-06 23:00:29 -08002675 case SIOCGIFTXQLEN:
Arnd Bergmannfab25322009-11-08 20:56:21 -08002676 case SIOCGMIIPHY:
2677 case SIOCGMIIREG:
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00002678 if (copy_in_user(uifr32, uifr, sizeof(*uifr32)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002679 err = -EFAULT;
2680 break;
2681 }
2682 }
2683 return err;
2684}
2685
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00002686static int compat_sioc_ifmap(struct net *net, unsigned int cmd,
2687 struct compat_ifreq __user *uifr32)
2688{
2689 struct ifreq ifr;
2690 struct compat_ifmap __user *uifmap32;
2691 mm_segment_t old_fs;
2692 int err;
2693
2694 uifmap32 = &uifr32->ifr_ifru.ifru_map;
2695 err = copy_from_user(&ifr, uifr32, sizeof(ifr.ifr_name));
2696 err |= __get_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
2697 err |= __get_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
2698 err |= __get_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
2699 err |= __get_user(ifr.ifr_map.irq, &uifmap32->irq);
2700 err |= __get_user(ifr.ifr_map.dma, &uifmap32->dma);
2701 err |= __get_user(ifr.ifr_map.port, &uifmap32->port);
2702 if (err)
2703 return -EFAULT;
2704
2705 old_fs = get_fs();
2706 set_fs (KERNEL_DS);
2707 err = dev_ioctl(net, cmd, (void __user *)&ifr);
2708 set_fs (old_fs);
2709
2710 if (cmd == SIOCGIFMAP && !err) {
2711 err = copy_to_user(uifr32, &ifr, sizeof(ifr.ifr_name));
2712 err |= __put_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
2713 err |= __put_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
2714 err |= __put_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
2715 err |= __put_user(ifr.ifr_map.irq, &uifmap32->irq);
2716 err |= __put_user(ifr.ifr_map.dma, &uifmap32->dma);
2717 err |= __put_user(ifr.ifr_map.port, &uifmap32->port);
2718 if (err)
2719 err = -EFAULT;
2720 }
2721 return err;
2722}
2723
2724static int compat_siocshwtstamp(struct net *net, struct compat_ifreq __user *uifr32)
2725{
2726 void __user *uptr;
2727 compat_uptr_t uptr32;
2728 struct ifreq __user *uifr;
2729
2730 uifr = compat_alloc_user_space(sizeof (*uifr));
2731 if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
2732 return -EFAULT;
2733
2734 if (get_user(uptr32, &uifr32->ifr_data))
2735 return -EFAULT;
2736
2737 uptr = compat_ptr(uptr32);
2738
2739 if (put_user(uptr, &uifr->ifr_data))
2740 return -EFAULT;
2741
2742 return dev_ioctl(net, SIOCSHWTSTAMP, uifr);
2743}
2744
Arnd Bergmann7a229382009-11-06 23:00:29 -08002745struct rtentry32 {
2746 u32 rt_pad1;
2747 struct sockaddr rt_dst; /* target address */
2748 struct sockaddr rt_gateway; /* gateway addr (RTF_GATEWAY) */
2749 struct sockaddr rt_genmask; /* target network mask (IP) */
2750 unsigned short rt_flags;
2751 short rt_pad2;
2752 u32 rt_pad3;
2753 unsigned char rt_tos;
2754 unsigned char rt_class;
2755 short rt_pad4;
2756 short rt_metric; /* +1 for binary compatibility! */
2757 /* char * */ u32 rt_dev; /* forcing the device at add */
2758 u32 rt_mtu; /* per route MTU/Window */
2759 u32 rt_window; /* Window clamping */
2760 unsigned short rt_irtt; /* Initial RTT */
2761};
2762
2763struct in6_rtmsg32 {
2764 struct in6_addr rtmsg_dst;
2765 struct in6_addr rtmsg_src;
2766 struct in6_addr rtmsg_gateway;
2767 u32 rtmsg_type;
2768 u16 rtmsg_dst_len;
2769 u16 rtmsg_src_len;
2770 u32 rtmsg_metric;
2771 u32 rtmsg_info;
2772 u32 rtmsg_flags;
2773 s32 rtmsg_ifindex;
2774};
2775
Arnd Bergmann6b960182009-11-06 23:10:54 -08002776static int routing_ioctl(struct net *net, struct socket *sock,
2777 unsigned int cmd, void __user *argp)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002778{
2779 int ret;
2780 void *r = NULL;
2781 struct in6_rtmsg r6;
2782 struct rtentry r4;
2783 char devname[16];
2784 u32 rtdev;
2785 mm_segment_t old_fs = get_fs();
2786
Arnd Bergmann6b960182009-11-06 23:10:54 -08002787 if (sock && sock->sk && sock->sk->sk_family == AF_INET6) { /* ipv6 */
2788 struct in6_rtmsg32 __user *ur6 = argp;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002789 ret = copy_from_user (&r6.rtmsg_dst, &(ur6->rtmsg_dst),
2790 3 * sizeof(struct in6_addr));
2791 ret |= __get_user (r6.rtmsg_type, &(ur6->rtmsg_type));
2792 ret |= __get_user (r6.rtmsg_dst_len, &(ur6->rtmsg_dst_len));
2793 ret |= __get_user (r6.rtmsg_src_len, &(ur6->rtmsg_src_len));
2794 ret |= __get_user (r6.rtmsg_metric, &(ur6->rtmsg_metric));
2795 ret |= __get_user (r6.rtmsg_info, &(ur6->rtmsg_info));
2796 ret |= __get_user (r6.rtmsg_flags, &(ur6->rtmsg_flags));
2797 ret |= __get_user (r6.rtmsg_ifindex, &(ur6->rtmsg_ifindex));
2798
2799 r = (void *) &r6;
2800 } else { /* ipv4 */
Arnd Bergmann6b960182009-11-06 23:10:54 -08002801 struct rtentry32 __user *ur4 = argp;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002802 ret = copy_from_user (&r4.rt_dst, &(ur4->rt_dst),
2803 3 * sizeof(struct sockaddr));
2804 ret |= __get_user (r4.rt_flags, &(ur4->rt_flags));
2805 ret |= __get_user (r4.rt_metric, &(ur4->rt_metric));
2806 ret |= __get_user (r4.rt_mtu, &(ur4->rt_mtu));
2807 ret |= __get_user (r4.rt_window, &(ur4->rt_window));
2808 ret |= __get_user (r4.rt_irtt, &(ur4->rt_irtt));
2809 ret |= __get_user (rtdev, &(ur4->rt_dev));
2810 if (rtdev) {
2811 ret |= copy_from_user (devname, compat_ptr(rtdev), 15);
2812 r4.rt_dev = devname; devname[15] = 0;
2813 } else
2814 r4.rt_dev = NULL;
2815
2816 r = (void *) &r4;
2817 }
2818
2819 if (ret) {
2820 ret = -EFAULT;
2821 goto out;
2822 }
2823
2824 set_fs (KERNEL_DS);
Arnd Bergmann6b960182009-11-06 23:10:54 -08002825 ret = sock_do_ioctl(net, sock, cmd, (unsigned long) r);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002826 set_fs (old_fs);
2827
2828out:
Arnd Bergmann7a229382009-11-06 23:00:29 -08002829 return ret;
2830}
2831
2832/* Since old style bridge ioctl's endup using SIOCDEVPRIVATE
2833 * for some operations; this forces use of the newer bridge-utils that
2834 * use compatiable ioctls
2835 */
Arnd Bergmann6b960182009-11-06 23:10:54 -08002836static int old_bridge_ioctl(compat_ulong_t __user *argp)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002837{
Arnd Bergmann6b960182009-11-06 23:10:54 -08002838 compat_ulong_t tmp;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002839
Arnd Bergmann6b960182009-11-06 23:10:54 -08002840 if (get_user(tmp, argp))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002841 return -EFAULT;
2842 if (tmp == BRCTL_GET_VERSION)
2843 return BRCTL_VERSION + 1;
2844 return -EINVAL;
2845}
2846
Arnd Bergmann6b960182009-11-06 23:10:54 -08002847static int compat_sock_ioctl_trans(struct file *file, struct socket *sock,
2848 unsigned int cmd, unsigned long arg)
2849{
2850 void __user *argp = compat_ptr(arg);
2851 struct sock *sk = sock->sk;
2852 struct net *net = sock_net(sk);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002853
Arnd Bergmann6b960182009-11-06 23:10:54 -08002854 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15))
2855 return siocdevprivate_ioctl(net, cmd, argp);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002856
Arnd Bergmann6b960182009-11-06 23:10:54 -08002857 switch (cmd) {
2858 case SIOCSIFBR:
2859 case SIOCGIFBR:
2860 return old_bridge_ioctl(argp);
2861 case SIOCGIFNAME:
2862 return dev_ifname32(net, argp);
2863 case SIOCGIFCONF:
2864 return dev_ifconf(net, argp);
2865 case SIOCETHTOOL:
2866 return ethtool_ioctl(net, argp);
Arnd Bergmann7a50a242009-11-08 20:57:03 -08002867 case SIOCWANDEV:
2868 return compat_siocwandev(net, argp);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00002869 case SIOCGIFMAP:
2870 case SIOCSIFMAP:
2871 return compat_sioc_ifmap(net, cmd, argp);
Arnd Bergmann6b960182009-11-06 23:10:54 -08002872 case SIOCBONDENSLAVE:
2873 case SIOCBONDRELEASE:
2874 case SIOCBONDSETHWADDR:
2875 case SIOCBONDSLAVEINFOQUERY:
2876 case SIOCBONDINFOQUERY:
2877 case SIOCBONDCHANGEACTIVE:
2878 return bond_ioctl(net, cmd, argp);
2879 case SIOCADDRT:
2880 case SIOCDELRT:
2881 return routing_ioctl(net, sock, cmd, argp);
2882 case SIOCGSTAMP:
2883 return do_siocgstamp(net, sock, cmd, argp);
2884 case SIOCGSTAMPNS:
2885 return do_siocgstampns(net, sock, cmd, argp);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00002886 case SIOCSHWTSTAMP:
2887 return compat_siocshwtstamp(net, argp);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002888
Arnd Bergmann6b960182009-11-06 23:10:54 -08002889 case FIOSETOWN:
2890 case SIOCSPGRP:
2891 case FIOGETOWN:
2892 case SIOCGPGRP:
2893 case SIOCBRADDBR:
2894 case SIOCBRDELBR:
2895 case SIOCGIFVLAN:
2896 case SIOCSIFVLAN:
2897 case SIOCADDDLCI:
2898 case SIOCDELDLCI:
2899 return sock_ioctl(file, cmd, arg);
2900
2901 case SIOCGIFFLAGS:
2902 case SIOCSIFFLAGS:
2903 case SIOCGIFMETRIC:
2904 case SIOCSIFMETRIC:
2905 case SIOCGIFMTU:
2906 case SIOCSIFMTU:
2907 case SIOCGIFMEM:
2908 case SIOCSIFMEM:
2909 case SIOCGIFHWADDR:
2910 case SIOCSIFHWADDR:
2911 case SIOCADDMULTI:
2912 case SIOCDELMULTI:
2913 case SIOCGIFINDEX:
Arnd Bergmann6b960182009-11-06 23:10:54 -08002914 case SIOCGIFADDR:
2915 case SIOCSIFADDR:
2916 case SIOCSIFHWBROADCAST:
Arnd Bergmann6b960182009-11-06 23:10:54 -08002917 case SIOCDIFADDR:
Arnd Bergmann6b960182009-11-06 23:10:54 -08002918 case SIOCGIFBRDADDR:
2919 case SIOCSIFBRDADDR:
2920 case SIOCGIFDSTADDR:
2921 case SIOCSIFDSTADDR:
2922 case SIOCGIFNETMASK:
2923 case SIOCSIFNETMASK:
2924 case SIOCSIFPFLAGS:
2925 case SIOCGIFPFLAGS:
2926 case SIOCGIFTXQLEN:
2927 case SIOCSIFTXQLEN:
2928 case SIOCBRADDIF:
2929 case SIOCBRDELIF:
Arnd Bergmann9177efd2009-11-06 08:09:09 +00002930 case SIOCSIFNAME:
2931 case SIOCGMIIPHY:
2932 case SIOCGMIIREG:
2933 case SIOCSMIIREG:
Arnd Bergmann6b960182009-11-06 23:10:54 -08002934 return dev_ifsioc(net, sock, cmd, argp);
Arnd Bergmann9177efd2009-11-06 08:09:09 +00002935
Arnd Bergmann6b960182009-11-06 23:10:54 -08002936 case SIOCSARP:
2937 case SIOCGARP:
2938 case SIOCDARP:
Arnd Bergmann6b960182009-11-06 23:10:54 -08002939 case SIOCATMARK:
Arnd Bergmann9177efd2009-11-06 08:09:09 +00002940 return sock_do_ioctl(net, sock, cmd, arg);
2941 }
2942
2943 /* Prevent warning from compat_sys_ioctl, these always
2944 * result in -EINVAL in the native case anyway. */
2945 switch (cmd) {
2946 case SIOCRTMSG:
2947 case SIOCGIFCOUNT:
Arnd Bergmann6b960182009-11-06 23:10:54 -08002948 case SIOCSRARP:
2949 case SIOCGRARP:
2950 case SIOCDRARP:
Arnd Bergmann9177efd2009-11-06 08:09:09 +00002951 case SIOCSIFLINK:
2952 case SIOCGIFSLAVE:
2953 case SIOCSIFSLAVE:
2954 return -EINVAL;
Arnd Bergmann6b960182009-11-06 23:10:54 -08002955 }
2956
2957 return -ENOIOCTLCMD;
2958}
Arnd Bergmann7a229382009-11-06 23:00:29 -08002959
Shaun Pereira89bbfc92006-03-21 23:58:08 -08002960static long compat_sock_ioctl(struct file *file, unsigned cmd,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002961 unsigned long arg)
Shaun Pereira89bbfc92006-03-21 23:58:08 -08002962{
2963 struct socket *sock = file->private_data;
2964 int ret = -ENOIOCTLCMD;
David S. Miller87de87d2008-06-03 09:14:03 -07002965 struct sock *sk;
2966 struct net *net;
2967
2968 sk = sock->sk;
2969 net = sock_net(sk);
Shaun Pereira89bbfc92006-03-21 23:58:08 -08002970
2971 if (sock->ops->compat_ioctl)
2972 ret = sock->ops->compat_ioctl(sock, cmd, arg);
2973
David S. Miller87de87d2008-06-03 09:14:03 -07002974 if (ret == -ENOIOCTLCMD &&
2975 (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST))
2976 ret = compat_wext_handle_ioctl(net, cmd, arg);
2977
Arnd Bergmann6b960182009-11-06 23:10:54 -08002978 if (ret == -ENOIOCTLCMD)
2979 ret = compat_sock_ioctl_trans(file, sock, cmd, arg);
2980
Shaun Pereira89bbfc92006-03-21 23:58:08 -08002981 return ret;
2982}
2983#endif
2984
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07002985int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen)
2986{
2987 return sock->ops->bind(sock, addr, addrlen);
2988}
2989
2990int kernel_listen(struct socket *sock, int backlog)
2991{
2992 return sock->ops->listen(sock, backlog);
2993}
2994
2995int kernel_accept(struct socket *sock, struct socket **newsock, int flags)
2996{
2997 struct sock *sk = sock->sk;
2998 int err;
2999
3000 err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
3001 newsock);
3002 if (err < 0)
3003 goto done;
3004
3005 err = sock->ops->accept(sock, *newsock, flags);
3006 if (err < 0) {
3007 sock_release(*newsock);
Tony Battersbyfa8705b2007-10-10 21:09:04 -07003008 *newsock = NULL;
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003009 goto done;
3010 }
3011
3012 (*newsock)->ops = sock->ops;
Wei Yongjun1b085342008-12-18 19:35:10 -08003013 __module_get((*newsock)->ops->owner);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003014
3015done:
3016 return err;
3017}
3018
3019int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
YOSHIFUJI Hideaki4768fbc2007-02-09 23:25:31 +09003020 int flags)
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003021{
3022 return sock->ops->connect(sock, addr, addrlen, flags);
3023}
3024
3025int kernel_getsockname(struct socket *sock, struct sockaddr *addr,
3026 int *addrlen)
3027{
3028 return sock->ops->getname(sock, addr, addrlen, 0);
3029}
3030
3031int kernel_getpeername(struct socket *sock, struct sockaddr *addr,
3032 int *addrlen)
3033{
3034 return sock->ops->getname(sock, addr, addrlen, 1);
3035}
3036
3037int kernel_getsockopt(struct socket *sock, int level, int optname,
3038 char *optval, int *optlen)
3039{
3040 mm_segment_t oldfs = get_fs();
3041 int err;
3042
3043 set_fs(KERNEL_DS);
3044 if (level == SOL_SOCKET)
3045 err = sock_getsockopt(sock, level, optname, optval, optlen);
3046 else
3047 err = sock->ops->getsockopt(sock, level, optname, optval,
3048 optlen);
3049 set_fs(oldfs);
3050 return err;
3051}
3052
3053int kernel_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07003054 char *optval, unsigned int optlen)
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003055{
3056 mm_segment_t oldfs = get_fs();
3057 int err;
3058
3059 set_fs(KERNEL_DS);
3060 if (level == SOL_SOCKET)
3061 err = sock_setsockopt(sock, level, optname, optval, optlen);
3062 else
3063 err = sock->ops->setsockopt(sock, level, optname, optval,
3064 optlen);
3065 set_fs(oldfs);
3066 return err;
3067}
3068
3069int kernel_sendpage(struct socket *sock, struct page *page, int offset,
3070 size_t size, int flags)
3071{
3072 if (sock->ops->sendpage)
3073 return sock->ops->sendpage(sock, page, offset, size, flags);
3074
3075 return sock_no_sendpage(sock, page, offset, size, flags);
3076}
3077
3078int kernel_sock_ioctl(struct socket *sock, int cmd, unsigned long arg)
3079{
3080 mm_segment_t oldfs = get_fs();
3081 int err;
3082
3083 set_fs(KERNEL_DS);
3084 err = sock->ops->ioctl(sock, cmd, arg);
3085 set_fs(oldfs);
3086
3087 return err;
3088}
3089
Trond Myklebust91cf45f2007-11-12 18:10:39 -08003090int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how)
3091{
3092 return sock->ops->shutdown(sock, how);
3093}
3094
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095EXPORT_SYMBOL(sock_create);
3096EXPORT_SYMBOL(sock_create_kern);
3097EXPORT_SYMBOL(sock_create_lite);
3098EXPORT_SYMBOL(sock_map_fd);
3099EXPORT_SYMBOL(sock_recvmsg);
3100EXPORT_SYMBOL(sock_register);
3101EXPORT_SYMBOL(sock_release);
3102EXPORT_SYMBOL(sock_sendmsg);
3103EXPORT_SYMBOL(sock_unregister);
3104EXPORT_SYMBOL(sock_wake_async);
3105EXPORT_SYMBOL(sockfd_lookup);
3106EXPORT_SYMBOL(kernel_sendmsg);
3107EXPORT_SYMBOL(kernel_recvmsg);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003108EXPORT_SYMBOL(kernel_bind);
3109EXPORT_SYMBOL(kernel_listen);
3110EXPORT_SYMBOL(kernel_accept);
3111EXPORT_SYMBOL(kernel_connect);
3112EXPORT_SYMBOL(kernel_getsockname);
3113EXPORT_SYMBOL(kernel_getpeername);
3114EXPORT_SYMBOL(kernel_getsockopt);
3115EXPORT_SYMBOL(kernel_setsockopt);
3116EXPORT_SYMBOL(kernel_sendpage);
3117EXPORT_SYMBOL(kernel_sock_ioctl);
Trond Myklebust91cf45f2007-11-12 18:10:39 -08003118EXPORT_SYMBOL(kernel_sock_shutdown);