blob: cb7c1f6c0d6e62a9c930322b6d780ae7c0913eb9 [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;
255 init_waitqueue_head(&ei->socket.wait);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 ei->socket.fasync_list = NULL;
258 ei->socket.state = SS_UNCONNECTED;
259 ei->socket.flags = 0;
260 ei->socket.ops = NULL;
261 ei->socket.sk = NULL;
262 ei->socket.file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 return &ei->vfs_inode;
265}
266
267static void sock_destroy_inode(struct inode *inode)
268{
269 kmem_cache_free(sock_inode_cachep,
270 container_of(inode, struct socket_alloc, vfs_inode));
271}
272
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700273static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700275 struct socket_alloc *ei = (struct socket_alloc *)foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Christoph Lametera35afb82007-05-16 22:10:57 -0700277 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280static int init_inodecache(void)
281{
282 sock_inode_cachep = kmem_cache_create("sock_inode_cache",
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700283 sizeof(struct socket_alloc),
284 0,
285 (SLAB_HWCACHE_ALIGN |
286 SLAB_RECLAIM_ACCOUNT |
287 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +0900288 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 if (sock_inode_cachep == NULL)
290 return -ENOMEM;
291 return 0;
292}
293
Alexey Dobriyanb87221d2009-09-21 17:01:09 -0700294static const struct super_operations sockfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 .alloc_inode = sock_alloc_inode,
296 .destroy_inode =sock_destroy_inode,
297 .statfs = simple_statfs,
298};
299
David Howells454e2392006-06-23 02:02:57 -0700300static int sockfs_get_sb(struct file_system_type *fs_type,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700301 int flags, const char *dev_name, void *data,
302 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
David Howells454e2392006-06-23 02:02:57 -0700304 return get_sb_pseudo(fs_type, "socket:", &sockfs_ops, SOCKFS_MAGIC,
305 mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
Eric Dumazetba899662005-08-26 12:05:31 -0700308static struct vfsmount *sock_mnt __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310static struct file_system_type sock_fs_type = {
311 .name = "sockfs",
312 .get_sb = sockfs_get_sb,
313 .kill_sb = kill_anon_super,
314};
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700315
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700316/*
317 * sockfs_dname() is called from d_path().
318 */
319static char *sockfs_dname(struct dentry *dentry, char *buffer, int buflen)
320{
321 return dynamic_dname(dentry, buffer, buflen, "socket:[%lu]",
322 dentry->d_inode->i_ino);
323}
324
Al Viro3ba13d12009-02-20 06:02:22 +0000325static const struct dentry_operations sockfs_dentry_operations = {
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700326 .d_dname = sockfs_dname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327};
328
329/*
330 * Obtains the first available file descriptor and sets it up for use.
331 *
David S. Miller39d8c1b2006-03-20 17:13:49 -0800332 * These functions create file structures and maps them to fd space
333 * of the current process. On success it returns file descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 * and file struct implicitly stored in sock->file.
335 * Note that another thread may close file descriptor before we return
336 * from this function. We use the fact that now we do not refer
337 * to socket after mapping. If one day we will need it, this
338 * function will increment ref. count on file by 1.
339 *
340 * In any case returned fd MAY BE not valid!
341 * This race condition is unavoidable
342 * with shared fd spaces, we cannot solve it inside kernel,
343 * but we take care of internal coherence yet.
344 */
345
Al Viro7cbe66b2009-08-05 19:59:08 +0400346static int sock_alloc_file(struct socket *sock, struct file **f, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
Al Viro7cbe66b2009-08-05 19:59:08 +0400348 struct qstr name = { .name = "" };
Al Viro2c48b9c2009-08-09 00:52:35 +0400349 struct path path;
Al Viro7cbe66b2009-08-05 19:59:08 +0400350 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 int fd;
David S. Miller39d8c1b2006-03-20 17:13:49 -0800352
Ulrich Dreppera677a032008-07-23 21:29:17 -0700353 fd = get_unused_fd_flags(flags);
Al Viro7cbe66b2009-08-05 19:59:08 +0400354 if (unlikely(fd < 0))
355 return fd;
David S. Miller39d8c1b2006-03-20 17:13:49 -0800356
Al Viro2c48b9c2009-08-09 00:52:35 +0400357 path.dentry = d_alloc(sock_mnt->mnt_sb->s_root, &name);
358 if (unlikely(!path.dentry)) {
Al Viro7cbe66b2009-08-05 19:59:08 +0400359 put_unused_fd(fd);
David S. Miller39d8c1b2006-03-20 17:13:49 -0800360 return -ENOMEM;
Al Viro7cbe66b2009-08-05 19:59:08 +0400361 }
Al Viro2c48b9c2009-08-09 00:52:35 +0400362 path.mnt = mntget(sock_mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Al Viro2c48b9c2009-08-09 00:52:35 +0400364 path.dentry->d_op = &sockfs_dentry_operations;
Al Viro2c48b9c2009-08-09 00:52:35 +0400365 d_instantiate(path.dentry, SOCK_INODE(sock));
Al Virocc3808f2009-08-06 09:43:59 +0400366 SOCK_INODE(sock)->i_fop = &socket_file_ops;
367
Al Viro2c48b9c2009-08-09 00:52:35 +0400368 file = alloc_file(&path, FMODE_READ | FMODE_WRITE,
Al Virocc3808f2009-08-06 09:43:59 +0400369 &socket_file_ops);
370 if (unlikely(!file)) {
371 /* drop dentry, keep inode */
372 atomic_inc(&path.dentry->d_inode->i_count);
Al Viro2c48b9c2009-08-09 00:52:35 +0400373 path_put(&path);
Al Virocc3808f2009-08-06 09:43:59 +0400374 put_unused_fd(fd);
375 return -ENFILE;
376 }
David S. Miller39d8c1b2006-03-20 17:13:49 -0800377
378 sock->file = file;
Ulrich Drepper77d27202008-07-23 21:29:35 -0700379 file->f_flags = O_RDWR | (flags & O_NONBLOCK);
David S. Miller39d8c1b2006-03-20 17:13:49 -0800380 file->f_pos = 0;
381 file->private_data = sock;
382
Al Viro7cbe66b2009-08-05 19:59:08 +0400383 *f = file;
384 return fd;
David S. Miller39d8c1b2006-03-20 17:13:49 -0800385}
386
Ulrich Dreppera677a032008-07-23 21:29:17 -0700387int sock_map_fd(struct socket *sock, int flags)
David S. Miller39d8c1b2006-03-20 17:13:49 -0800388{
389 struct file *newfile;
Al Viro7cbe66b2009-08-05 19:59:08 +0400390 int fd = sock_alloc_file(sock, &newfile, flags);
David S. Miller39d8c1b2006-03-20 17:13:49 -0800391
Al Viro7cbe66b2009-08-05 19:59:08 +0400392 if (likely(fd >= 0))
David S. Miller39d8c1b2006-03-20 17:13:49 -0800393 fd_install(fd, newfile);
Al Viro7cbe66b2009-08-05 19:59:08 +0400394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 return fd;
396}
397
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800398static struct socket *sock_from_file(struct file *file, int *err)
399{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800400 if (file->f_op == &socket_file_ops)
401 return file->private_data; /* set in sock_map_fd */
402
Eric Dumazet23bb80d2007-02-08 14:59:57 -0800403 *err = -ENOTSOCK;
404 return NULL;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800405}
406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407/**
408 * sockfd_lookup - Go from a file number to its socket slot
409 * @fd: file handle
410 * @err: pointer to an error code return
411 *
412 * The file handle passed in is locked and the socket it is bound
413 * too is returned. If an error occurs the err pointer is overwritten
414 * with a negative errno code and NULL is returned. The function checks
415 * for both invalid handles and passing a handle which is not a socket.
416 *
417 * On a success the socket object pointer is returned.
418 */
419
420struct socket *sockfd_lookup(int fd, int *err)
421{
422 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 struct socket *sock;
424
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700425 file = fget(fd);
426 if (!file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 *err = -EBADF;
428 return NULL;
429 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700430
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800431 sock = sock_from_file(file, err);
432 if (!sock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 fput(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return sock;
435}
436
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800437static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed)
438{
439 struct file *file;
440 struct socket *sock;
441
Hua Zhong36725582006-04-19 15:25:02 -0700442 *err = -EBADF;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800443 file = fget_light(fd, fput_needed);
444 if (file) {
445 sock = sock_from_file(file, err);
446 if (sock)
447 return sock;
448 fput_light(file, *fput_needed);
449 }
450 return NULL;
451}
452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453/**
454 * sock_alloc - allocate a socket
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700455 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 * Allocate a new inode and socket object. The two are bound together
457 * and initialised. The socket is then returned. If we are out of inodes
458 * NULL is returned.
459 */
460
461static struct socket *sock_alloc(void)
462{
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700463 struct inode *inode;
464 struct socket *sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 inode = new_inode(sock_mnt->mnt_sb);
467 if (!inode)
468 return NULL;
469
470 sock = SOCKET_I(inode);
471
Eric Dumazet29a020d2009-09-15 02:39:20 -0700472 kmemcheck_annotate_bitfield(sock, type);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700473 inode->i_mode = S_IFSOCK | S_IRWXUGO;
David Howells8192b0c2008-11-14 10:39:10 +1100474 inode->i_uid = current_fsuid();
475 inode->i_gid = current_fsgid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Eric Dumazet4e694892009-04-04 16:41:09 -0700477 percpu_add(sockets_in_use, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 return sock;
479}
480
481/*
482 * In theory you can't get an open on this inode, but /proc provides
483 * a back door. Remember to keep it shut otherwise you'll let the
484 * creepy crawlies in.
485 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487static int sock_no_open(struct inode *irrelevant, struct file *dontcare)
488{
489 return -ENXIO;
490}
491
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800492const struct file_operations bad_sock_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 .owner = THIS_MODULE,
494 .open = sock_no_open,
495};
496
497/**
498 * sock_release - close a socket
499 * @sock: socket to close
500 *
501 * The socket is released from the protocol stack if it has a release
502 * callback, and the inode is then released if the socket is bound to
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700503 * an inode not a file.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506void sock_release(struct socket *sock)
507{
508 if (sock->ops) {
509 struct module *owner = sock->ops->owner;
510
511 sock->ops->release(sock);
512 sock->ops = NULL;
513 module_put(owner);
514 }
515
516 if (sock->fasync_list)
517 printk(KERN_ERR "sock_release: fasync list not empty!\n");
518
Eric Dumazet4e694892009-04-04 16:41:09 -0700519 percpu_sub(sockets_in_use, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 if (!sock->file) {
521 iput(SOCK_INODE(sock));
522 return;
523 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700524 sock->file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525}
526
Patrick Ohly20d49472009-02-12 05:03:38 +0000527int sock_tx_timestamp(struct msghdr *msg, struct sock *sk,
528 union skb_shared_tx *shtx)
529{
530 shtx->flags = 0;
531 if (sock_flag(sk, SOCK_TIMESTAMPING_TX_HARDWARE))
532 shtx->hardware = 1;
533 if (sock_flag(sk, SOCK_TIMESTAMPING_TX_SOFTWARE))
534 shtx->software = 1;
535 return 0;
536}
537EXPORT_SYMBOL(sock_tx_timestamp);
538
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700539static inline int __sock_sendmsg(struct kiocb *iocb, struct socket *sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 struct msghdr *msg, size_t size)
541{
542 struct sock_iocb *si = kiocb_to_siocb(iocb);
543 int err;
544
545 si->sock = sock;
546 si->scm = NULL;
547 si->msg = msg;
548 si->size = size;
549
550 err = security_socket_sendmsg(sock, msg, size);
551 if (err)
552 return err;
553
554 return sock->ops->sendmsg(iocb, sock, msg, size);
555}
556
557int sock_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
558{
559 struct kiocb iocb;
560 struct sock_iocb siocb;
561 int ret;
562
563 init_sync_kiocb(&iocb, NULL);
564 iocb.private = &siocb;
565 ret = __sock_sendmsg(&iocb, sock, msg, size);
566 if (-EIOCBQUEUED == ret)
567 ret = wait_on_sync_kiocb(&iocb);
568 return ret;
569}
570
571int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
572 struct kvec *vec, size_t num, size_t size)
573{
574 mm_segment_t oldfs = get_fs();
575 int result;
576
577 set_fs(KERNEL_DS);
578 /*
579 * the following is safe, since for compiler definitions of kvec and
580 * iovec are identical, yielding the same in-core layout and alignment
581 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700582 msg->msg_iov = (struct iovec *)vec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 msg->msg_iovlen = num;
584 result = sock_sendmsg(sock, msg, size);
585 set_fs(oldfs);
586 return result;
587}
588
Patrick Ohly20d49472009-02-12 05:03:38 +0000589static int ktime2ts(ktime_t kt, struct timespec *ts)
590{
591 if (kt.tv64) {
592 *ts = ktime_to_timespec(kt);
593 return 1;
594 } else {
595 return 0;
596 }
597}
598
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700599/*
600 * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
601 */
602void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
603 struct sk_buff *skb)
604{
Patrick Ohly20d49472009-02-12 05:03:38 +0000605 int need_software_tstamp = sock_flag(sk, SOCK_RCVTSTAMP);
606 struct timespec ts[3];
607 int empty = 1;
608 struct skb_shared_hwtstamps *shhwtstamps =
609 skb_hwtstamps(skb);
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700610
Patrick Ohly20d49472009-02-12 05:03:38 +0000611 /* Race occurred between timestamp enabling and packet
612 receiving. Fill in the current time for now. */
613 if (need_software_tstamp && skb->tstamp.tv64 == 0)
614 __net_timestamp(skb);
615
616 if (need_software_tstamp) {
617 if (!sock_flag(sk, SOCK_RCVTSTAMPNS)) {
618 struct timeval tv;
619 skb_get_timestamp(skb, &tv);
620 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMP,
621 sizeof(tv), &tv);
622 } else {
Hagen Paul Pfeifer842509b2010-04-06 05:39:52 +0000623 skb_get_timestampns(skb, &ts[0]);
Patrick Ohly20d49472009-02-12 05:03:38 +0000624 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPNS,
Hagen Paul Pfeifer842509b2010-04-06 05:39:52 +0000625 sizeof(ts[0]), &ts[0]);
Patrick Ohly20d49472009-02-12 05:03:38 +0000626 }
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700627 }
Patrick Ohly20d49472009-02-12 05:03:38 +0000628
629
630 memset(ts, 0, sizeof(ts));
631 if (skb->tstamp.tv64 &&
632 sock_flag(sk, SOCK_TIMESTAMPING_SOFTWARE)) {
633 skb_get_timestampns(skb, ts + 0);
634 empty = 0;
635 }
636 if (shhwtstamps) {
637 if (sock_flag(sk, SOCK_TIMESTAMPING_SYS_HARDWARE) &&
638 ktime2ts(shhwtstamps->syststamp, ts + 1))
639 empty = 0;
640 if (sock_flag(sk, SOCK_TIMESTAMPING_RAW_HARDWARE) &&
641 ktime2ts(shhwtstamps->hwtstamp, ts + 2))
642 empty = 0;
643 }
644 if (!empty)
645 put_cmsg(msg, SOL_SOCKET,
646 SCM_TIMESTAMPING, sizeof(ts), &ts);
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700647}
648
Arnaldo Carvalho de Melo7c81fd82007-03-10 00:39:35 -0300649EXPORT_SYMBOL_GPL(__sock_recv_timestamp);
650
Neil Horman3b885782009-10-12 13:26:31 -0700651inline void sock_recv_drops(struct msghdr *msg, struct sock *sk, struct sk_buff *skb)
652{
653 if (sock_flag(sk, SOCK_RXQ_OVFL) && skb && skb->dropcount)
654 put_cmsg(msg, SOL_SOCKET, SO_RXQ_OVFL,
655 sizeof(__u32), &skb->dropcount);
656}
657
Eric Dumazet767dd032010-04-28 19:14:43 +0000658void __sock_recv_ts_and_drops(struct msghdr *msg, struct sock *sk,
Neil Horman3b885782009-10-12 13:26:31 -0700659 struct sk_buff *skb)
660{
661 sock_recv_timestamp(msg, sk, skb);
662 sock_recv_drops(msg, sk, skb);
663}
Eric Dumazet767dd032010-04-28 19:14:43 +0000664EXPORT_SYMBOL_GPL(__sock_recv_ts_and_drops);
Neil Horman3b885782009-10-12 13:26:31 -0700665
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -0700666static inline int __sock_recvmsg_nosec(struct kiocb *iocb, struct socket *sock,
667 struct msghdr *msg, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 struct sock_iocb *si = kiocb_to_siocb(iocb);
670
671 si->sock = sock;
672 si->scm = NULL;
673 si->msg = msg;
674 si->size = size;
675 si->flags = flags;
676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 return sock->ops->recvmsg(iocb, sock, msg, size, flags);
678}
679
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -0700680static inline int __sock_recvmsg(struct kiocb *iocb, struct socket *sock,
681 struct msghdr *msg, size_t size, int flags)
682{
683 int err = security_socket_recvmsg(sock, msg, size, flags);
684
685 return err ?: __sock_recvmsg_nosec(iocb, sock, msg, size, flags);
686}
687
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700688int sock_recvmsg(struct socket *sock, struct msghdr *msg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 size_t size, int flags)
690{
691 struct kiocb iocb;
692 struct sock_iocb siocb;
693 int ret;
694
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700695 init_sync_kiocb(&iocb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 iocb.private = &siocb;
697 ret = __sock_recvmsg(&iocb, sock, msg, size, flags);
698 if (-EIOCBQUEUED == ret)
699 ret = wait_on_sync_kiocb(&iocb);
700 return ret;
701}
702
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -0700703static int sock_recvmsg_nosec(struct socket *sock, struct msghdr *msg,
704 size_t size, int flags)
705{
706 struct kiocb iocb;
707 struct sock_iocb siocb;
708 int ret;
709
710 init_sync_kiocb(&iocb, NULL);
711 iocb.private = &siocb;
712 ret = __sock_recvmsg_nosec(&iocb, sock, msg, size, flags);
713 if (-EIOCBQUEUED == ret)
714 ret = wait_on_sync_kiocb(&iocb);
715 return ret;
716}
717
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700718int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
719 struct kvec *vec, size_t num, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
721 mm_segment_t oldfs = get_fs();
722 int result;
723
724 set_fs(KERNEL_DS);
725 /*
726 * the following is safe, since for compiler definitions of kvec and
727 * iovec are identical, yielding the same in-core layout and alignment
728 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700729 msg->msg_iov = (struct iovec *)vec, msg->msg_iovlen = num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 result = sock_recvmsg(sock, msg, size, flags);
731 set_fs(oldfs);
732 return result;
733}
734
735static void sock_aio_dtor(struct kiocb *iocb)
736{
737 kfree(iocb->private);
738}
739
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -0300740static ssize_t sock_sendpage(struct file *file, struct page *page,
741 int offset, size_t size, loff_t *ppos, int more)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
743 struct socket *sock;
744 int flags;
745
Eric Dumazetb69aee02005-09-06 14:42:45 -0700746 sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748 flags = !(file->f_flags & O_NONBLOCK) ? 0 : MSG_DONTWAIT;
749 if (more)
750 flags |= MSG_MORE;
751
Linus Torvaldse6949582009-08-13 08:28:36 -0700752 return kernel_sendpage(sock, page, offset, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753}
754
Jens Axboe9c55e012007-11-06 23:30:13 -0800755static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
756 struct pipe_inode_info *pipe, size_t len,
757 unsigned int flags)
758{
759 struct socket *sock = file->private_data;
760
Rémi Denis-Courmont997b37d2008-02-15 02:35:45 -0800761 if (unlikely(!sock->ops->splice_read))
762 return -EINVAL;
763
Jens Axboe9c55e012007-11-06 23:30:13 -0800764 return sock->ops->splice_read(sock, ppos, pipe, len, flags);
765}
766
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800767static struct sock_iocb *alloc_sock_iocb(struct kiocb *iocb,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700768 struct sock_iocb *siocb)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800769{
770 if (!is_sync_kiocb(iocb)) {
771 siocb = kmalloc(sizeof(*siocb), GFP_KERNEL);
772 if (!siocb)
773 return NULL;
774 iocb->ki_dtor = sock_aio_dtor;
775 }
776
777 siocb->kiocb = iocb;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800778 iocb->private = siocb;
779 return siocb;
780}
781
782static ssize_t do_sock_read(struct msghdr *msg, struct kiocb *iocb,
Badari Pulavarty027445c2006-09-30 23:28:46 -0700783 struct file *file, const struct iovec *iov,
784 unsigned long nr_segs)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800785{
786 struct socket *sock = file->private_data;
787 size_t size = 0;
788 int i;
789
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700790 for (i = 0; i < nr_segs; i++)
791 size += iov[i].iov_len;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800792
793 msg->msg_name = NULL;
794 msg->msg_namelen = 0;
795 msg->msg_control = NULL;
796 msg->msg_controllen = 0;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700797 msg->msg_iov = (struct iovec *)iov;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800798 msg->msg_iovlen = nr_segs;
799 msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
800
801 return __sock_recvmsg(iocb, sock, msg, size, msg->msg_flags);
802}
803
Badari Pulavarty027445c2006-09-30 23:28:46 -0700804static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov,
805 unsigned long nr_segs, loff_t pos)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800806{
807 struct sock_iocb siocb, *x;
808
809 if (pos != 0)
810 return -ESPIPE;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700811
812 if (iocb->ki_left == 0) /* Match SYS5 behaviour */
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800813 return 0;
814
Badari Pulavarty027445c2006-09-30 23:28:46 -0700815
816 x = alloc_sock_iocb(iocb, &siocb);
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800817 if (!x)
818 return -ENOMEM;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700819 return do_sock_read(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800820}
821
822static ssize_t do_sock_write(struct msghdr *msg, struct kiocb *iocb,
Badari Pulavarty027445c2006-09-30 23:28:46 -0700823 struct file *file, const struct iovec *iov,
824 unsigned long nr_segs)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800825{
826 struct socket *sock = file->private_data;
827 size_t size = 0;
828 int i;
829
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700830 for (i = 0; i < nr_segs; i++)
831 size += iov[i].iov_len;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800832
833 msg->msg_name = NULL;
834 msg->msg_namelen = 0;
835 msg->msg_control = NULL;
836 msg->msg_controllen = 0;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700837 msg->msg_iov = (struct iovec *)iov;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800838 msg->msg_iovlen = nr_segs;
839 msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
840 if (sock->type == SOCK_SEQPACKET)
841 msg->msg_flags |= MSG_EOR;
842
843 return __sock_sendmsg(iocb, sock, msg, size);
844}
845
Badari Pulavarty027445c2006-09-30 23:28:46 -0700846static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov,
847 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800849 struct sock_iocb siocb, *x;
850
851 if (pos != 0)
852 return -ESPIPE;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700853
Badari Pulavarty027445c2006-09-30 23:28:46 -0700854 x = alloc_sock_iocb(iocb, &siocb);
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800855 if (!x)
856 return -ENOMEM;
857
Badari Pulavarty027445c2006-09-30 23:28:46 -0700858 return do_sock_write(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859}
860
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861/*
862 * Atomic setting of ioctl hooks to avoid race
863 * with module unload.
864 */
865
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800866static DEFINE_MUTEX(br_ioctl_mutex);
Eric W. Biederman881d9662007-09-17 11:56:21 -0700867static int (*br_ioctl_hook) (struct net *, unsigned int cmd, void __user *arg) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Eric W. Biederman881d9662007-09-17 11:56:21 -0700869void brioctl_set(int (*hook) (struct net *, unsigned int, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800871 mutex_lock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 br_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800873 mutex_unlock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874}
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700875
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876EXPORT_SYMBOL(brioctl_set);
877
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800878static DEFINE_MUTEX(vlan_ioctl_mutex);
Eric W. Biederman881d9662007-09-17 11:56:21 -0700879static int (*vlan_ioctl_hook) (struct net *, void __user *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Eric W. Biederman881d9662007-09-17 11:56:21 -0700881void vlan_ioctl_set(int (*hook) (struct net *, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800883 mutex_lock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 vlan_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800885 mutex_unlock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886}
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888EXPORT_SYMBOL(vlan_ioctl_set);
889
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800890static DEFINE_MUTEX(dlci_ioctl_mutex);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700891static int (*dlci_ioctl_hook) (unsigned int, void __user *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700893void dlci_ioctl_set(int (*hook) (unsigned int, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800895 mutex_lock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 dlci_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800897 mutex_unlock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898}
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900EXPORT_SYMBOL(dlci_ioctl_set);
901
Arnd Bergmann6b960182009-11-06 23:10:54 -0800902static long sock_do_ioctl(struct net *net, struct socket *sock,
903 unsigned int cmd, unsigned long arg)
904{
905 int err;
906 void __user *argp = (void __user *)arg;
907
908 err = sock->ops->ioctl(sock, cmd, arg);
909
910 /*
911 * If this ioctl is unknown try to hand it down
912 * to the NIC driver.
913 */
914 if (err == -ENOIOCTLCMD)
915 err = dev_ioctl(net, cmd, argp);
916
917 return err;
918}
919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920/*
921 * With an ioctl, arg may well be a user mode pointer, but we don't know
922 * what to do with it - that's up to the protocol still.
923 */
924
925static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
926{
927 struct socket *sock;
Eric W. Biederman881d9662007-09-17 11:56:21 -0700928 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 void __user *argp = (void __user *)arg;
930 int pid, err;
Eric W. Biederman881d9662007-09-17 11:56:21 -0700931 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Eric Dumazetb69aee02005-09-06 14:42:45 -0700933 sock = file->private_data;
Eric W. Biederman881d9662007-09-17 11:56:21 -0700934 sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900935 net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
Eric W. Biederman881d9662007-09-17 11:56:21 -0700937 err = dev_ioctl(net, cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 } else
Johannes Berg3d23e342009-09-29 23:27:28 +0200939#ifdef CONFIG_WEXT_CORE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
Eric W. Biederman881d9662007-09-17 11:56:21 -0700941 err = dev_ioctl(net, cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 } else
Johannes Berg3d23e342009-09-29 23:27:28 +0200943#endif
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700944 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 case FIOSETOWN:
946 case SIOCSPGRP:
947 err = -EFAULT;
948 if (get_user(pid, (int __user *)argp))
949 break;
950 err = f_setown(sock->file, pid, 1);
951 break;
952 case FIOGETOWN:
953 case SIOCGPGRP:
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700954 err = put_user(f_getown(sock->file),
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700955 (int __user *)argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 break;
957 case SIOCGIFBR:
958 case SIOCSIFBR:
959 case SIOCBRADDBR:
960 case SIOCBRDELBR:
961 err = -ENOPKG;
962 if (!br_ioctl_hook)
963 request_module("bridge");
964
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800965 mutex_lock(&br_ioctl_mutex);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700966 if (br_ioctl_hook)
Eric W. Biederman881d9662007-09-17 11:56:21 -0700967 err = br_ioctl_hook(net, cmd, argp);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800968 mutex_unlock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 break;
970 case SIOCGIFVLAN:
971 case SIOCSIFVLAN:
972 err = -ENOPKG;
973 if (!vlan_ioctl_hook)
974 request_module("8021q");
975
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800976 mutex_lock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 if (vlan_ioctl_hook)
Eric W. Biederman881d9662007-09-17 11:56:21 -0700978 err = vlan_ioctl_hook(net, argp);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800979 mutex_unlock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 case SIOCADDDLCI:
982 case SIOCDELDLCI:
983 err = -ENOPKG;
984 if (!dlci_ioctl_hook)
985 request_module("dlci");
986
Pavel Emelyanov7512cbf2008-03-21 15:58:52 -0700987 mutex_lock(&dlci_ioctl_mutex);
988 if (dlci_ioctl_hook)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 err = dlci_ioctl_hook(cmd, argp);
Pavel Emelyanov7512cbf2008-03-21 15:58:52 -0700990 mutex_unlock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 break;
992 default:
Arnd Bergmann6b960182009-11-06 23:10:54 -0800993 err = sock_do_ioctl(net, sock, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 break;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700995 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 return err;
997}
998
999int sock_create_lite(int family, int type, int protocol, struct socket **res)
1000{
1001 int err;
1002 struct socket *sock = NULL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 err = security_socket_create(family, type, protocol, 1);
1005 if (err)
1006 goto out;
1007
1008 sock = sock_alloc();
1009 if (!sock) {
1010 err = -ENOMEM;
1011 goto out;
1012 }
1013
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 sock->type = type;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001015 err = security_socket_post_create(sock, family, type, protocol, 1);
1016 if (err)
1017 goto out_release;
1018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019out:
1020 *res = sock;
1021 return err;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001022out_release:
1023 sock_release(sock);
1024 sock = NULL;
1025 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026}
1027
1028/* No kernel lock held - perfect */
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001029static unsigned int sock_poll(struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030{
1031 struct socket *sock;
1032
1033 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001034 * We can't return errors to poll, so it's either yes or no.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 */
Eric Dumazetb69aee02005-09-06 14:42:45 -07001036 sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 return sock->ops->poll(file, sock, wait);
1038}
1039
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001040static int sock_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041{
Eric Dumazetb69aee02005-09-06 14:42:45 -07001042 struct socket *sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
1044 return sock->ops->mmap(file, sock, vma);
1045}
1046
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001047static int sock_close(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048{
1049 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001050 * It was possible the inode is NULL we were
1051 * closing an unfinished socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 */
1053
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001054 if (!inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 printk(KERN_DEBUG "sock_close: NULL inode\n");
1056 return 0;
1057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 sock_release(SOCKET_I(inode));
1059 return 0;
1060}
1061
1062/*
1063 * Update the socket async list
1064 *
1065 * Fasync_list locking strategy.
1066 *
1067 * 1. fasync_list is modified only under process context socket lock
1068 * i.e. under semaphore.
1069 * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
Eric Dumazet989a2972010-04-14 09:55:35 +00001070 * or under socket lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 */
1072
1073static int sock_fasync(int fd, struct file *filp, int on)
1074{
Eric Dumazet989a2972010-04-14 09:55:35 +00001075 struct socket *sock = filp->private_data;
1076 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Eric Dumazet989a2972010-04-14 09:55:35 +00001078 if (sk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
1081 lock_sock(sk);
1082
Eric Dumazet989a2972010-04-14 09:55:35 +00001083 fasync_helper(fd, filp, on, &sock->fasync_list);
1084
1085 if (!sock->fasync_list)
1086 sock_reset_flag(sk, SOCK_FASYNC);
Jonathan Corbet76398422009-02-01 14:26:59 -07001087 else
Eric Dumazetbcdce712009-10-06 17:28:29 -07001088 sock_set_flag(sk, SOCK_FASYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
Eric Dumazet989a2972010-04-14 09:55:35 +00001090 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 return 0;
1092}
1093
1094/* This function may be called only under socket lock or callback_lock */
1095
1096int sock_wake_async(struct socket *sock, int how, int band)
1097{
1098 if (!sock || !sock->fasync_list)
1099 return -1;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001100 switch (how) {
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001101 case SOCK_WAKE_WAITD:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 if (test_bit(SOCK_ASYNC_WAITDATA, &sock->flags))
1103 break;
1104 goto call_kill;
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001105 case SOCK_WAKE_SPACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags))
1107 break;
1108 /* fall through */
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001109 case SOCK_WAKE_IO:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001110call_kill:
Eric Dumazet989a2972010-04-14 09:55:35 +00001111 kill_fasync(&sock->fasync_list, SIGIO, band);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 break;
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001113 case SOCK_WAKE_URG:
Eric Dumazet989a2972010-04-14 09:55:35 +00001114 kill_fasync(&sock->fasync_list, SIGURG, band);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 }
1116 return 0;
1117}
1118
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001119static int __sock_create(struct net *net, int family, int type, int protocol,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001120 struct socket **res, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121{
1122 int err;
1123 struct socket *sock;
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001124 const struct net_proto_family *pf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
1126 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001127 * Check protocol is in range
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 */
1129 if (family < 0 || family >= NPROTO)
1130 return -EAFNOSUPPORT;
1131 if (type < 0 || type >= SOCK_MAX)
1132 return -EINVAL;
1133
1134 /* Compatibility.
1135
1136 This uglymoron is moved from INET layer to here to avoid
1137 deadlock in module load.
1138 */
1139 if (family == PF_INET && type == SOCK_PACKET) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001140 static int warned;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 if (!warned) {
1142 warned = 1;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001143 printk(KERN_INFO "%s uses obsolete (PF_INET,SOCK_PACKET)\n",
1144 current->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 }
1146 family = PF_PACKET;
1147 }
1148
1149 err = security_socket_create(family, type, protocol, kern);
1150 if (err)
1151 return err;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001152
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001153 /*
1154 * Allocate the socket and allow the family to set things up. if
1155 * the protocol is 0, the family is instructed to select an appropriate
1156 * default.
1157 */
1158 sock = sock_alloc();
1159 if (!sock) {
1160 if (net_ratelimit())
1161 printk(KERN_WARNING "socket: no more sockets\n");
1162 return -ENFILE; /* Not exactly a match, but its the
1163 closest posix thing */
1164 }
1165
1166 sock->type = type;
1167
Johannes Berg95a5afc2008-10-16 15:24:51 -07001168#ifdef CONFIG_MODULES
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001169 /* Attempt to load a protocol module if the find failed.
1170 *
1171 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 * requested real, full-featured networking support upon configuration.
1173 * Otherwise module support will break!
1174 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001175 if (net_families[family] == NULL)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001176 request_module("net-pf-%d", family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177#endif
1178
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001179 rcu_read_lock();
1180 pf = rcu_dereference(net_families[family]);
1181 err = -EAFNOSUPPORT;
1182 if (!pf)
1183 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
1185 /*
1186 * We will call the ->create function, that possibly is in a loadable
1187 * module, so we have to bump that loadable module refcnt first.
1188 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001189 if (!try_module_get(pf->owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 goto out_release;
1191
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001192 /* Now protected by module ref count */
1193 rcu_read_unlock();
1194
Eric Paris3f378b62009-11-05 22:18:14 -08001195 err = pf->create(net, sock, protocol, kern);
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001196 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 goto out_module_put;
Frank Filza79af592005-09-27 15:23:38 -07001198
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 /*
1200 * Now to bump the refcnt of the [loadable] module that owns this
1201 * socket at sock_release time we decrement its refcnt.
1202 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001203 if (!try_module_get(sock->ops->owner))
1204 goto out_module_busy;
1205
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 /*
1207 * Now that we're done with the ->create function, the [loadable]
1208 * module can have its refcnt decremented
1209 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001210 module_put(pf->owner);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001211 err = security_socket_post_create(sock, family, type, protocol, kern);
1212 if (err)
Herbert Xu3b185522007-08-15 14:46:02 -07001213 goto out_sock_release;
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001214 *res = sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001216 return 0;
1217
1218out_module_busy:
1219 err = -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220out_module_put:
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001221 sock->ops = NULL;
1222 module_put(pf->owner);
1223out_sock_release:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 sock_release(sock);
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001225 return err;
1226
1227out_release:
1228 rcu_read_unlock();
1229 goto out_sock_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230}
1231
1232int sock_create(int family, int type, int protocol, struct socket **res)
1233{
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001234 return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235}
1236
1237int sock_create_kern(int family, int type, int protocol, struct socket **res)
1238{
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001239 return __sock_create(&init_net, family, type, protocol, res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240}
1241
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001242SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243{
1244 int retval;
1245 struct socket *sock;
Ulrich Dreppera677a032008-07-23 21:29:17 -07001246 int flags;
1247
Ulrich Dreppere38b36f2008-07-23 21:29:42 -07001248 /* Check the SOCK_* constants for consistency. */
1249 BUILD_BUG_ON(SOCK_CLOEXEC != O_CLOEXEC);
1250 BUILD_BUG_ON((SOCK_MAX | SOCK_TYPE_MASK) != SOCK_TYPE_MASK);
1251 BUILD_BUG_ON(SOCK_CLOEXEC & SOCK_TYPE_MASK);
1252 BUILD_BUG_ON(SOCK_NONBLOCK & SOCK_TYPE_MASK);
1253
Ulrich Dreppera677a032008-07-23 21:29:17 -07001254 flags = type & ~SOCK_TYPE_MASK;
Ulrich Drepper77d27202008-07-23 21:29:35 -07001255 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Dreppera677a032008-07-23 21:29:17 -07001256 return -EINVAL;
1257 type &= SOCK_TYPE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001259 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1260 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1261
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 retval = sock_create(family, type, protocol, &sock);
1263 if (retval < 0)
1264 goto out;
1265
Ulrich Drepper77d27202008-07-23 21:29:35 -07001266 retval = sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 if (retval < 0)
1268 goto out_release;
1269
1270out:
1271 /* It may be already another descriptor 8) Not kernel problem. */
1272 return retval;
1273
1274out_release:
1275 sock_release(sock);
1276 return retval;
1277}
1278
1279/*
1280 * Create a pair of connected sockets.
1281 */
1282
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001283SYSCALL_DEFINE4(socketpair, int, family, int, type, int, protocol,
1284 int __user *, usockvec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285{
1286 struct socket *sock1, *sock2;
1287 int fd1, fd2, err;
Al Virodb349502007-02-07 01:48:00 -05001288 struct file *newfile1, *newfile2;
Ulrich Dreppera677a032008-07-23 21:29:17 -07001289 int flags;
1290
1291 flags = type & ~SOCK_TYPE_MASK;
Ulrich Drepper77d27202008-07-23 21:29:35 -07001292 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Dreppera677a032008-07-23 21:29:17 -07001293 return -EINVAL;
1294 type &= SOCK_TYPE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001296 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1297 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1298
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 /*
1300 * Obtain the first socket and check if the underlying protocol
1301 * supports the socketpair call.
1302 */
1303
1304 err = sock_create(family, type, protocol, &sock1);
1305 if (err < 0)
1306 goto out;
1307
1308 err = sock_create(family, type, protocol, &sock2);
1309 if (err < 0)
1310 goto out_release_1;
1311
1312 err = sock1->ops->socketpair(sock1, sock2);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001313 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 goto out_release_both;
1315
Al Viro7cbe66b2009-08-05 19:59:08 +04001316 fd1 = sock_alloc_file(sock1, &newfile1, flags);
David S. Millerbf3c23d2007-10-29 21:54:02 -07001317 if (unlikely(fd1 < 0)) {
1318 err = fd1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 goto out_release_both;
David S. Millerbf3c23d2007-10-29 21:54:02 -07001320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
Al Viro7cbe66b2009-08-05 19:59:08 +04001322 fd2 = sock_alloc_file(sock2, &newfile2, flags);
Al Viro198de4d2009-08-05 19:29:23 +04001323 if (unlikely(fd2 < 0)) {
1324 err = fd2;
1325 fput(newfile1);
1326 put_unused_fd(fd1);
1327 sock_release(sock2);
1328 goto out;
Al Virodb349502007-02-07 01:48:00 -05001329 }
1330
Al Viro157cf642008-12-14 04:57:47 -05001331 audit_fd_pair(fd1, fd2);
Al Virodb349502007-02-07 01:48:00 -05001332 fd_install(fd1, newfile1);
1333 fd_install(fd2, newfile2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 /* fd1 and fd2 may be already another descriptors.
1335 * Not kernel problem.
1336 */
1337
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001338 err = put_user(fd1, &usockvec[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 if (!err)
1340 err = put_user(fd2, &usockvec[1]);
1341 if (!err)
1342 return 0;
1343
1344 sys_close(fd2);
1345 sys_close(fd1);
1346 return err;
1347
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348out_release_both:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001349 sock_release(sock2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350out_release_1:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001351 sock_release(sock1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352out:
1353 return err;
1354}
1355
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356/*
1357 * Bind a name to a socket. Nothing much to do here since it's
1358 * the protocol's responsibility to handle the local address.
1359 *
1360 * We move the socket address to kernel space before we call
1361 * the protocol layer (having also checked the address is ok).
1362 */
1363
Heiko Carstens20f37032009-01-14 14:14:23 +01001364SYSCALL_DEFINE3(bind, int, fd, struct sockaddr __user *, umyaddr, int, addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365{
1366 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001367 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001368 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001370 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001371 if (sock) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001372 err = move_addr_to_kernel(umyaddr, addrlen, (struct sockaddr *)&address);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001373 if (err >= 0) {
1374 err = security_socket_bind(sock,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001375 (struct sockaddr *)&address,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001376 addrlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001377 if (!err)
1378 err = sock->ops->bind(sock,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001379 (struct sockaddr *)
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001380 &address, addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 }
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001382 fput_light(sock->file, fput_needed);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 return err;
1385}
1386
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387/*
1388 * Perform a listen. Basically, we allow the protocol to do anything
1389 * necessary for a listen, and if that works, we mark the socket as
1390 * ready for listening.
1391 */
1392
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001393SYSCALL_DEFINE2(listen, int, fd, int, backlog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394{
1395 struct socket *sock;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001396 int err, fput_needed;
Pavel Emelyanovb8e1f9b2007-12-08 00:12:33 -08001397 int somaxconn;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001398
1399 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1400 if (sock) {
Pavel Emelyanov8efa6e92008-03-31 19:41:14 -07001401 somaxconn = sock_net(sock->sk)->core.sysctl_somaxconn;
Pavel Emelyanovb8e1f9b2007-12-08 00:12:33 -08001402 if ((unsigned)backlog > somaxconn)
1403 backlog = somaxconn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
1405 err = security_socket_listen(sock, backlog);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001406 if (!err)
1407 err = sock->ops->listen(sock, backlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001409 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 }
1411 return err;
1412}
1413
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414/*
1415 * For accept, we attempt to create a new socket, set up the link
1416 * with the client, wake up the client, then return the new
1417 * connected fd. We collect the address of the connector in kernel
1418 * space and move it to user at the very end. This is unclean because
1419 * we open the socket then return an error.
1420 *
1421 * 1003.1g adds the ability to recvmsg() to query connection pending
1422 * status to recvmsg. We need to add that support in a way thats
1423 * clean when we restucture accept also.
1424 */
1425
Heiko Carstens20f37032009-01-14 14:14:23 +01001426SYSCALL_DEFINE4(accept4, int, fd, struct sockaddr __user *, upeer_sockaddr,
1427 int __user *, upeer_addrlen, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428{
1429 struct socket *sock, *newsock;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001430 struct file *newfile;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001431 int err, len, newfd, fput_needed;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001432 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
Ulrich Drepper77d27202008-07-23 21:29:35 -07001434 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001435 return -EINVAL;
1436
1437 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1438 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1439
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001440 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 if (!sock)
1442 goto out;
1443
1444 err = -ENFILE;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001445 if (!(newsock = sock_alloc()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 goto out_put;
1447
1448 newsock->type = sock->type;
1449 newsock->ops = sock->ops;
1450
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 /*
1452 * We don't need try_module_get here, as the listening socket (sock)
1453 * has the protocol module (sock->ops->owner) held.
1454 */
1455 __module_get(newsock->ops->owner);
1456
Al Viro7cbe66b2009-08-05 19:59:08 +04001457 newfd = sock_alloc_file(newsock, &newfile, flags);
David S. Miller39d8c1b2006-03-20 17:13:49 -08001458 if (unlikely(newfd < 0)) {
1459 err = newfd;
David S. Miller9a1875e2006-04-01 12:48:36 -08001460 sock_release(newsock);
1461 goto out_put;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001462 }
1463
Frank Filza79af592005-09-27 15:23:38 -07001464 err = security_socket_accept(sock, newsock);
1465 if (err)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001466 goto out_fd;
Frank Filza79af592005-09-27 15:23:38 -07001467
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 err = sock->ops->accept(sock, newsock, sock->file->f_flags);
1469 if (err < 0)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001470 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
1472 if (upeer_sockaddr) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001473 if (newsock->ops->getname(newsock, (struct sockaddr *)&address,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001474 &len, 2) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 err = -ECONNABORTED;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001476 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 }
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001478 err = move_addr_to_user((struct sockaddr *)&address,
1479 len, upeer_sockaddr, upeer_addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 if (err < 0)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001481 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 }
1483
1484 /* File flags are not inherited via accept() unlike another OSes. */
1485
David S. Miller39d8c1b2006-03-20 17:13:49 -08001486 fd_install(newfd, newfile);
1487 err = newfd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001490 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491out:
1492 return err;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001493out_fd:
David S. Miller9606a212006-04-01 01:00:14 -08001494 fput(newfile);
David S. Miller39d8c1b2006-03-20 17:13:49 -08001495 put_unused_fd(newfd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 goto out_put;
1497}
1498
Heiko Carstens20f37032009-01-14 14:14:23 +01001499SYSCALL_DEFINE3(accept, int, fd, struct sockaddr __user *, upeer_sockaddr,
1500 int __user *, upeer_addrlen)
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001501{
Ulrich Drepperde11def2008-11-19 15:36:14 -08001502 return sys_accept4(fd, upeer_sockaddr, upeer_addrlen, 0);
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001503}
1504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505/*
1506 * Attempt to connect to a socket with the server address. The address
1507 * is in user space so we verify it is OK and move it to kernel space.
1508 *
1509 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
1510 * break bindings
1511 *
1512 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
1513 * other SEQPACKET protocols that take time to connect() as it doesn't
1514 * include the -EINPROGRESS status for such sockets.
1515 */
1516
Heiko Carstens20f37032009-01-14 14:14:23 +01001517SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
1518 int, addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519{
1520 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001521 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001522 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001524 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 if (!sock)
1526 goto out;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001527 err = move_addr_to_kernel(uservaddr, addrlen, (struct sockaddr *)&address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 if (err < 0)
1529 goto out_put;
1530
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001531 err =
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001532 security_socket_connect(sock, (struct sockaddr *)&address, addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 if (err)
1534 goto out_put;
1535
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001536 err = sock->ops->connect(sock, (struct sockaddr *)&address, addrlen,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 sock->file->f_flags);
1538out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001539 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540out:
1541 return err;
1542}
1543
1544/*
1545 * Get the local address ('name') of a socket object. Move the obtained
1546 * name to user space.
1547 */
1548
Heiko Carstens20f37032009-01-14 14:14:23 +01001549SYSCALL_DEFINE3(getsockname, int, fd, struct sockaddr __user *, usockaddr,
1550 int __user *, usockaddr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551{
1552 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001553 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001554 int len, err, fput_needed;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001555
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001556 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 if (!sock)
1558 goto out;
1559
1560 err = security_socket_getsockname(sock);
1561 if (err)
1562 goto out_put;
1563
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001564 err = sock->ops->getname(sock, (struct sockaddr *)&address, &len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 if (err)
1566 goto out_put;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001567 err = move_addr_to_user((struct sockaddr *)&address, len, usockaddr, usockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
1569out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001570 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571out:
1572 return err;
1573}
1574
1575/*
1576 * Get the remote address ('name') of a socket object. Move the obtained
1577 * name to user space.
1578 */
1579
Heiko Carstens20f37032009-01-14 14:14:23 +01001580SYSCALL_DEFINE3(getpeername, int, fd, struct sockaddr __user *, usockaddr,
1581 int __user *, usockaddr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582{
1583 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001584 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001585 int len, err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001587 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1588 if (sock != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 err = security_socket_getpeername(sock);
1590 if (err) {
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001591 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 return err;
1593 }
1594
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001595 err =
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001596 sock->ops->getname(sock, (struct sockaddr *)&address, &len,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001597 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 if (!err)
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001599 err = move_addr_to_user((struct sockaddr *)&address, len, usockaddr,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001600 usockaddr_len);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001601 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 }
1603 return err;
1604}
1605
1606/*
1607 * Send a datagram to a given address. We move the address into kernel
1608 * space and check the user space data area is readable before invoking
1609 * the protocol.
1610 */
1611
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001612SYSCALL_DEFINE6(sendto, int, fd, void __user *, buff, size_t, len,
1613 unsigned, flags, struct sockaddr __user *, addr,
1614 int, addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615{
1616 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001617 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 int err;
1619 struct msghdr msg;
1620 struct iovec iov;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001621 int fput_needed;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001622
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001623 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1624 if (!sock)
David S. Miller4387ff72007-02-08 15:06:08 -08001625 goto out;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001626
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001627 iov.iov_base = buff;
1628 iov.iov_len = len;
1629 msg.msg_name = NULL;
1630 msg.msg_iov = &iov;
1631 msg.msg_iovlen = 1;
1632 msg.msg_control = NULL;
1633 msg.msg_controllen = 0;
1634 msg.msg_namelen = 0;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001635 if (addr) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001636 err = move_addr_to_kernel(addr, addr_len, (struct sockaddr *)&address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 if (err < 0)
1638 goto out_put;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001639 msg.msg_name = (struct sockaddr *)&address;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001640 msg.msg_namelen = addr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 }
1642 if (sock->file->f_flags & O_NONBLOCK)
1643 flags |= MSG_DONTWAIT;
1644 msg.msg_flags = flags;
1645 err = sock_sendmsg(sock, &msg, len);
1646
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001647out_put:
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001648 fput_light(sock->file, fput_needed);
David S. Miller4387ff72007-02-08 15:06:08 -08001649out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 return err;
1651}
1652
1653/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001654 * Send a datagram down a socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 */
1656
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001657SYSCALL_DEFINE4(send, int, fd, void __user *, buff, size_t, len,
1658 unsigned, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659{
1660 return sys_sendto(fd, buff, len, flags, NULL, 0);
1661}
1662
1663/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001664 * Receive a frame from the socket and optionally record the address of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 * sender. We verify the buffers are writable and if needed move the
1666 * sender address from kernel to user space.
1667 */
1668
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001669SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, ubuf, size_t, size,
1670 unsigned, flags, struct sockaddr __user *, addr,
1671 int __user *, addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672{
1673 struct socket *sock;
1674 struct iovec iov;
1675 struct msghdr msg;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001676 struct sockaddr_storage address;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001677 int err, err2;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001678 int fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001680 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 if (!sock)
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001682 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001684 msg.msg_control = NULL;
1685 msg.msg_controllen = 0;
1686 msg.msg_iovlen = 1;
1687 msg.msg_iov = &iov;
1688 iov.iov_len = size;
1689 iov.iov_base = ubuf;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001690 msg.msg_name = (struct sockaddr *)&address;
1691 msg.msg_namelen = sizeof(address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 if (sock->file->f_flags & O_NONBLOCK)
1693 flags |= MSG_DONTWAIT;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001694 err = sock_recvmsg(sock, &msg, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001696 if (err >= 0 && addr != NULL) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001697 err2 = move_addr_to_user((struct sockaddr *)&address,
1698 msg.msg_namelen, addr, addr_len);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001699 if (err2 < 0)
1700 err = err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 }
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001702
1703 fput_light(sock->file, fput_needed);
David S. Miller4387ff72007-02-08 15:06:08 -08001704out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 return err;
1706}
1707
1708/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001709 * Receive a datagram from a socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 */
1711
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001712asmlinkage long sys_recv(int fd, void __user *ubuf, size_t size,
1713 unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714{
1715 return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
1716}
1717
1718/*
1719 * Set a socket option. Because we don't know the option lengths we have
1720 * to pass the user mode parameter for the protocols to sort out.
1721 */
1722
Heiko Carstens20f37032009-01-14 14:14:23 +01001723SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname,
1724 char __user *, optval, int, optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001726 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 struct socket *sock;
1728
1729 if (optlen < 0)
1730 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001731
1732 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1733 if (sock != NULL) {
1734 err = security_socket_setsockopt(sock, level, optname);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001735 if (err)
1736 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
1738 if (level == SOL_SOCKET)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001739 err =
1740 sock_setsockopt(sock, level, optname, optval,
1741 optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 else
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001743 err =
1744 sock->ops->setsockopt(sock, level, optname, optval,
1745 optlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001746out_put:
1747 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 }
1749 return err;
1750}
1751
1752/*
1753 * Get a socket option. Because we don't know the option lengths we have
1754 * to pass a user mode parameter for the protocols to sort out.
1755 */
1756
Heiko Carstens20f37032009-01-14 14:14:23 +01001757SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname,
1758 char __user *, optval, int __user *, optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001760 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 struct socket *sock;
1762
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001763 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1764 if (sock != NULL) {
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001765 err = security_socket_getsockopt(sock, level, optname);
1766 if (err)
1767 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
1769 if (level == SOL_SOCKET)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001770 err =
1771 sock_getsockopt(sock, level, optname, optval,
1772 optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 else
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001774 err =
1775 sock->ops->getsockopt(sock, level, optname, optval,
1776 optlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001777out_put:
1778 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 }
1780 return err;
1781}
1782
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783/*
1784 * Shutdown a socket.
1785 */
1786
Heiko Carstens754fe8d2009-01-14 14:14:09 +01001787SYSCALL_DEFINE2(shutdown, int, fd, int, how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001789 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 struct socket *sock;
1791
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001792 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1793 if (sock != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 err = security_socket_shutdown(sock, how);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001795 if (!err)
1796 err = sock->ops->shutdown(sock, how);
1797 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 }
1799 return err;
1800}
1801
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001802/* A couple of helpful macros for getting the address of the 32/64 bit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 * fields which are the same type (int / unsigned) on our platforms.
1804 */
1805#define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
1806#define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
1807#define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
1808
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809/*
1810 * BSD sendmsg interface
1811 */
1812
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001813SYSCALL_DEFINE3(sendmsg, int, fd, struct msghdr __user *, msg, unsigned, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814{
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001815 struct compat_msghdr __user *msg_compat =
1816 (struct compat_msghdr __user *)msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001818 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
Alex Williamsonb9d717a2005-09-26 14:28:02 -07001820 unsigned char ctl[sizeof(struct cmsghdr) + 20]
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001821 __attribute__ ((aligned(sizeof(__kernel_size_t))));
1822 /* 20 is size of ipv6_pktinfo */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 unsigned char *ctl_buf = ctl;
1824 struct msghdr msg_sys;
1825 int err, ctl_len, iov_size, total_len;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001826 int fput_needed;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001827
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 err = -EFAULT;
1829 if (MSG_CMSG_COMPAT & flags) {
1830 if (get_compat_msghdr(&msg_sys, msg_compat))
1831 return -EFAULT;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001832 }
1833 else if (copy_from_user(&msg_sys, msg, sizeof(struct msghdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 return -EFAULT;
1835
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001836 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001837 if (!sock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 goto out;
1839
1840 /* do not move before msg_sys is valid */
1841 err = -EMSGSIZE;
1842 if (msg_sys.msg_iovlen > UIO_MAXIOV)
1843 goto out_put;
1844
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001845 /* Check whether to allocate the iovec area */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 err = -ENOMEM;
1847 iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
1848 if (msg_sys.msg_iovlen > UIO_FASTIOV) {
1849 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
1850 if (!iov)
1851 goto out_put;
1852 }
1853
1854 /* This will also move the address data into kernel space */
1855 if (MSG_CMSG_COMPAT & flags) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001856 err = verify_compat_iovec(&msg_sys, iov,
1857 (struct sockaddr *)&address,
1858 VERIFY_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 } else
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001860 err = verify_iovec(&msg_sys, iov,
1861 (struct sockaddr *)&address,
1862 VERIFY_READ);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001863 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 goto out_freeiov;
1865 total_len = err;
1866
1867 err = -ENOBUFS;
1868
1869 if (msg_sys.msg_controllen > INT_MAX)
1870 goto out_freeiov;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001871 ctl_len = msg_sys.msg_controllen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001873 err =
1874 cmsghdr_from_user_compat_to_kern(&msg_sys, sock->sk, ctl,
1875 sizeof(ctl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 if (err)
1877 goto out_freeiov;
1878 ctl_buf = msg_sys.msg_control;
Al Viro8920e8f2005-09-07 18:28:51 -07001879 ctl_len = msg_sys.msg_controllen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 } else if (ctl_len) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001881 if (ctl_len > sizeof(ctl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001883 if (ctl_buf == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 goto out_freeiov;
1885 }
1886 err = -EFAULT;
1887 /*
1888 * Careful! Before this, msg_sys.msg_control contains a user pointer.
1889 * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
1890 * checking falls down on this.
1891 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001892 if (copy_from_user(ctl_buf, (void __user *)msg_sys.msg_control,
1893 ctl_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 goto out_freectl;
1895 msg_sys.msg_control = ctl_buf;
1896 }
1897 msg_sys.msg_flags = flags;
1898
1899 if (sock->file->f_flags & O_NONBLOCK)
1900 msg_sys.msg_flags |= MSG_DONTWAIT;
1901 err = sock_sendmsg(sock, &msg_sys, total_len);
1902
1903out_freectl:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001904 if (ctl_buf != ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 sock_kfree_s(sock->sk, ctl_buf, ctl_len);
1906out_freeiov:
1907 if (iov != iovstack)
1908 sock_kfree_s(sock->sk, iov, iov_size);
1909out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001910 fput_light(sock->file, fput_needed);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001911out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 return err;
1913}
1914
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07001915static int __sys_recvmsg(struct socket *sock, struct msghdr __user *msg,
1916 struct msghdr *msg_sys, unsigned flags, int nosec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917{
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001918 struct compat_msghdr __user *msg_compat =
1919 (struct compat_msghdr __user *)msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 struct iovec iovstack[UIO_FASTIOV];
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001921 struct iovec *iov = iovstack;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 unsigned long cmsg_ptr;
1923 int err, iov_size, total_len, len;
1924
1925 /* kernel mode address */
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001926 struct sockaddr_storage addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
1928 /* user mode address pointers */
1929 struct sockaddr __user *uaddr;
1930 int __user *uaddr_len;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001931
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 if (MSG_CMSG_COMPAT & flags) {
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07001933 if (get_compat_msghdr(msg_sys, msg_compat))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 return -EFAULT;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001935 }
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07001936 else if (copy_from_user(msg_sys, msg, sizeof(struct msghdr)))
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001937 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 err = -EMSGSIZE;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07001940 if (msg_sys->msg_iovlen > UIO_MAXIOV)
1941 goto out;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001942
1943 /* Check whether to allocate the iovec area */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 err = -ENOMEM;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07001945 iov_size = msg_sys->msg_iovlen * sizeof(struct iovec);
1946 if (msg_sys->msg_iovlen > UIO_FASTIOV) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
1948 if (!iov)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07001949 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 }
1951
1952 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001953 * Save the user-mode address (verify_iovec will change the
1954 * kernel msghdr to use the kernel address space)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001956
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07001957 uaddr = (__force void __user *)msg_sys->msg_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 uaddr_len = COMPAT_NAMELEN(msg);
1959 if (MSG_CMSG_COMPAT & flags) {
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07001960 err = verify_compat_iovec(msg_sys, iov,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001961 (struct sockaddr *)&addr,
1962 VERIFY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 } else
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07001964 err = verify_iovec(msg_sys, iov,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001965 (struct sockaddr *)&addr,
1966 VERIFY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 if (err < 0)
1968 goto out_freeiov;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001969 total_len = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07001971 cmsg_ptr = (unsigned long)msg_sys->msg_control;
1972 msg_sys->msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001973
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 if (sock->file->f_flags & O_NONBLOCK)
1975 flags |= MSG_DONTWAIT;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07001976 err = (nosec ? sock_recvmsg_nosec : sock_recvmsg)(sock, msg_sys,
1977 total_len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 if (err < 0)
1979 goto out_freeiov;
1980 len = err;
1981
1982 if (uaddr != NULL) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001983 err = move_addr_to_user((struct sockaddr *)&addr,
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07001984 msg_sys->msg_namelen, uaddr,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001985 uaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 if (err < 0)
1987 goto out_freeiov;
1988 }
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07001989 err = __put_user((msg_sys->msg_flags & ~MSG_CMSG_COMPAT),
David S. Miller37f7f422005-09-16 16:51:01 -07001990 COMPAT_FLAGS(msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 if (err)
1992 goto out_freeiov;
1993 if (MSG_CMSG_COMPAT & flags)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07001994 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 &msg_compat->msg_controllen);
1996 else
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07001997 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 &msg->msg_controllen);
1999 if (err)
2000 goto out_freeiov;
2001 err = len;
2002
2003out_freeiov:
2004 if (iov != iovstack)
2005 sock_kfree_s(sock->sk, iov, iov_size);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002006out:
2007 return err;
2008}
2009
2010/*
2011 * BSD recvmsg interface
2012 */
2013
2014SYSCALL_DEFINE3(recvmsg, int, fd, struct msghdr __user *, msg,
2015 unsigned int, flags)
2016{
2017 int fput_needed, err;
2018 struct msghdr msg_sys;
2019 struct socket *sock = sockfd_lookup_light(fd, &err, &fput_needed);
2020
2021 if (!sock)
2022 goto out;
2023
2024 err = __sys_recvmsg(sock, msg, &msg_sys, flags, 0);
2025
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002026 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027out:
2028 return err;
2029}
2030
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002031/*
2032 * Linux recvmmsg interface
2033 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002035int __sys_recvmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
2036 unsigned int flags, struct timespec *timeout)
2037{
2038 int fput_needed, err, datagrams;
2039 struct socket *sock;
2040 struct mmsghdr __user *entry;
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002041 struct compat_mmsghdr __user *compat_entry;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002042 struct msghdr msg_sys;
2043 struct timespec end_time;
2044
2045 if (timeout &&
2046 poll_select_set_timeout(&end_time, timeout->tv_sec,
2047 timeout->tv_nsec))
2048 return -EINVAL;
2049
2050 datagrams = 0;
2051
2052 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2053 if (!sock)
2054 return err;
2055
2056 err = sock_error(sock->sk);
2057 if (err)
2058 goto out_put;
2059
2060 entry = mmsg;
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002061 compat_entry = (struct compat_mmsghdr __user *)mmsg;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002062
2063 while (datagrams < vlen) {
2064 /*
2065 * No need to ask LSM for more than the first datagram.
2066 */
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002067 if (MSG_CMSG_COMPAT & flags) {
2068 err = __sys_recvmsg(sock, (struct msghdr __user *)compat_entry,
2069 &msg_sys, flags, datagrams);
2070 if (err < 0)
2071 break;
2072 err = __put_user(err, &compat_entry->msg_len);
2073 ++compat_entry;
2074 } else {
2075 err = __sys_recvmsg(sock, (struct msghdr __user *)entry,
2076 &msg_sys, flags, datagrams);
2077 if (err < 0)
2078 break;
2079 err = put_user(err, &entry->msg_len);
2080 ++entry;
2081 }
2082
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002083 if (err)
2084 break;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002085 ++datagrams;
2086
Brandon L Black71c5c152010-03-26 16:18:03 +00002087 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
2088 if (flags & MSG_WAITFORONE)
2089 flags |= MSG_DONTWAIT;
2090
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002091 if (timeout) {
2092 ktime_get_ts(timeout);
2093 *timeout = timespec_sub(end_time, *timeout);
2094 if (timeout->tv_sec < 0) {
2095 timeout->tv_sec = timeout->tv_nsec = 0;
2096 break;
2097 }
2098
2099 /* Timeout, return less than vlen datagrams */
2100 if (timeout->tv_nsec == 0 && timeout->tv_sec == 0)
2101 break;
2102 }
2103
2104 /* Out of band data, return right away */
2105 if (msg_sys.msg_flags & MSG_OOB)
2106 break;
2107 }
2108
2109out_put:
2110 fput_light(sock->file, fput_needed);
2111
2112 if (err == 0)
2113 return datagrams;
2114
2115 if (datagrams != 0) {
2116 /*
2117 * We may return less entries than requested (vlen) if the
2118 * sock is non block and there aren't enough datagrams...
2119 */
2120 if (err != -EAGAIN) {
2121 /*
2122 * ... or if recvmsg returns an error after we
2123 * received some datagrams, where we record the
2124 * error to return on the next call or if the
2125 * app asks about it using getsockopt(SO_ERROR).
2126 */
2127 sock->sk->sk_err = -err;
2128 }
2129
2130 return datagrams;
2131 }
2132
2133 return err;
2134}
2135
2136SYSCALL_DEFINE5(recvmmsg, int, fd, struct mmsghdr __user *, mmsg,
2137 unsigned int, vlen, unsigned int, flags,
2138 struct timespec __user *, timeout)
2139{
2140 int datagrams;
2141 struct timespec timeout_sys;
2142
2143 if (!timeout)
2144 return __sys_recvmmsg(fd, mmsg, vlen, flags, NULL);
2145
2146 if (copy_from_user(&timeout_sys, timeout, sizeof(timeout_sys)))
2147 return -EFAULT;
2148
2149 datagrams = __sys_recvmmsg(fd, mmsg, vlen, flags, &timeout_sys);
2150
2151 if (datagrams > 0 &&
2152 copy_to_user(timeout, &timeout_sys, sizeof(timeout_sys)))
2153 datagrams = -EFAULT;
2154
2155 return datagrams;
2156}
2157
2158#ifdef __ARCH_WANT_SYS_SOCKETCALL
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159/* Argument list sizes for sys_socketcall */
2160#define AL(x) ((x) * sizeof(unsigned long))
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002161static const unsigned char nargs[20] = {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002162 AL(0),AL(3),AL(3),AL(3),AL(2),AL(3),
2163 AL(3),AL(3),AL(4),AL(4),AL(4),AL(6),
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07002164 AL(6),AL(2),AL(5),AL(5),AL(3),AL(3),
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002165 AL(4),AL(5)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002166};
2167
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168#undef AL
2169
2170/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002171 * System call vectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 *
2173 * Argument checking cleaned up. Saved 20% in size.
2174 * This function doesn't need to set the kernel lock because
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002175 * it is set by the callees.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 */
2177
Heiko Carstens3e0fa652009-01-14 14:14:24 +01002178SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179{
2180 unsigned long a[6];
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002181 unsigned long a0, a1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 int err;
Arjan van de Ven47379052009-09-28 12:57:44 -07002183 unsigned int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002185 if (call < 1 || call > SYS_RECVMMSG)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 return -EINVAL;
2187
Arjan van de Ven47379052009-09-28 12:57:44 -07002188 len = nargs[call];
2189 if (len > sizeof(a))
2190 return -EINVAL;
2191
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 /* copy_from_user should be SMP safe. */
Arjan van de Ven47379052009-09-28 12:57:44 -07002193 if (copy_from_user(a, args, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 return -EFAULT;
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002195
Al Virof3298dc2008-12-10 03:16:51 -05002196 audit_socketcall(nargs[call] / sizeof(unsigned long), a);
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002197
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002198 a0 = a[0];
2199 a1 = a[1];
2200
2201 switch (call) {
2202 case SYS_SOCKET:
2203 err = sys_socket(a0, a1, a[2]);
2204 break;
2205 case SYS_BIND:
2206 err = sys_bind(a0, (struct sockaddr __user *)a1, a[2]);
2207 break;
2208 case SYS_CONNECT:
2209 err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
2210 break;
2211 case SYS_LISTEN:
2212 err = sys_listen(a0, a1);
2213 break;
2214 case SYS_ACCEPT:
Ulrich Drepperde11def2008-11-19 15:36:14 -08002215 err = sys_accept4(a0, (struct sockaddr __user *)a1,
2216 (int __user *)a[2], 0);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002217 break;
2218 case SYS_GETSOCKNAME:
2219 err =
2220 sys_getsockname(a0, (struct sockaddr __user *)a1,
2221 (int __user *)a[2]);
2222 break;
2223 case SYS_GETPEERNAME:
2224 err =
2225 sys_getpeername(a0, (struct sockaddr __user *)a1,
2226 (int __user *)a[2]);
2227 break;
2228 case SYS_SOCKETPAIR:
2229 err = sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
2230 break;
2231 case SYS_SEND:
2232 err = sys_send(a0, (void __user *)a1, a[2], a[3]);
2233 break;
2234 case SYS_SENDTO:
2235 err = sys_sendto(a0, (void __user *)a1, a[2], a[3],
2236 (struct sockaddr __user *)a[4], a[5]);
2237 break;
2238 case SYS_RECV:
2239 err = sys_recv(a0, (void __user *)a1, a[2], a[3]);
2240 break;
2241 case SYS_RECVFROM:
2242 err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
2243 (struct sockaddr __user *)a[4],
2244 (int __user *)a[5]);
2245 break;
2246 case SYS_SHUTDOWN:
2247 err = sys_shutdown(a0, a1);
2248 break;
2249 case SYS_SETSOCKOPT:
2250 err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]);
2251 break;
2252 case SYS_GETSOCKOPT:
2253 err =
2254 sys_getsockopt(a0, a1, a[2], (char __user *)a[3],
2255 (int __user *)a[4]);
2256 break;
2257 case SYS_SENDMSG:
2258 err = sys_sendmsg(a0, (struct msghdr __user *)a1, a[2]);
2259 break;
2260 case SYS_RECVMSG:
2261 err = sys_recvmsg(a0, (struct msghdr __user *)a1, a[2]);
2262 break;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002263 case SYS_RECVMMSG:
2264 err = sys_recvmmsg(a0, (struct mmsghdr __user *)a1, a[2], a[3],
2265 (struct timespec __user *)a[4]);
2266 break;
Ulrich Drepperde11def2008-11-19 15:36:14 -08002267 case SYS_ACCEPT4:
2268 err = sys_accept4(a0, (struct sockaddr __user *)a1,
2269 (int __user *)a[2], a[3]);
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07002270 break;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002271 default:
2272 err = -EINVAL;
2273 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 }
2275 return err;
2276}
2277
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002278#endif /* __ARCH_WANT_SYS_SOCKETCALL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002280/**
2281 * sock_register - add a socket protocol handler
2282 * @ops: description of protocol
2283 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 * This function is called by a protocol handler that wants to
2285 * advertise its address family, and have it linked into the
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002286 * socket interface. The value ops->family coresponds to the
2287 * socket system call protocol family.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 */
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002289int sock_register(const struct net_proto_family *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290{
2291 int err;
2292
2293 if (ops->family >= NPROTO) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002294 printk(KERN_CRIT "protocol %d >= NPROTO(%d)\n", ops->family,
2295 NPROTO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 return -ENOBUFS;
2297 }
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002298
2299 spin_lock(&net_family_lock);
2300 if (net_families[ops->family])
2301 err = -EEXIST;
2302 else {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002303 net_families[ops->family] = ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 err = 0;
2305 }
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002306 spin_unlock(&net_family_lock);
2307
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002308 printk(KERN_INFO "NET: Registered protocol family %d\n", ops->family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 return err;
2310}
2311
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002312/**
2313 * sock_unregister - remove a protocol handler
2314 * @family: protocol family to remove
2315 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 * This function is called by a protocol handler that wants to
2317 * remove its address family, and have it unlinked from the
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002318 * new socket creation.
2319 *
2320 * If protocol handler is a module, then it can use module reference
2321 * counts to protect against new references. If protocol handler is not
2322 * a module then it needs to provide its own protection in
2323 * the ops->create routine.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 */
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002325void sock_unregister(int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326{
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002327 BUG_ON(family < 0 || family >= NPROTO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002329 spin_lock(&net_family_lock);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002330 net_families[family] = NULL;
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002331 spin_unlock(&net_family_lock);
2332
2333 synchronize_rcu();
2334
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002335 printk(KERN_INFO "NET: Unregistered protocol family %d\n", family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336}
2337
Andi Kleen77d76ea2005-12-22 12:43:42 -08002338static int __init sock_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339{
2340 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002341 * Initialize sock SLAB cache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002343
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 sk_init();
2345
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002347 * Initialize skbuff SLAB cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 */
2349 skb_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350
2351 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002352 * Initialize the protocols module.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 */
2354
2355 init_inodecache();
2356 register_filesystem(&sock_fs_type);
2357 sock_mnt = kern_mount(&sock_fs_type);
Andi Kleen77d76ea2005-12-22 12:43:42 -08002358
2359 /* The real protocol initialization is performed in later initcalls.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 */
2361
2362#ifdef CONFIG_NETFILTER
2363 netfilter_init();
2364#endif
David S. Millercbeb3212005-12-22 12:58:55 -08002365
2366 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367}
2368
Andi Kleen77d76ea2005-12-22 12:43:42 -08002369core_initcall(sock_init); /* early initcall */
2370
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371#ifdef CONFIG_PROC_FS
2372void socket_seq_show(struct seq_file *seq)
2373{
2374 int cpu;
2375 int counter = 0;
2376
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -07002377 for_each_possible_cpu(cpu)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002378 counter += per_cpu(sockets_in_use, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379
2380 /* It can be negative, by the way. 8) */
2381 if (counter < 0)
2382 counter = 0;
2383
2384 seq_printf(seq, "sockets: used %d\n", counter);
2385}
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002386#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387
Shaun Pereira89bbfc92006-03-21 23:58:08 -08002388#ifdef CONFIG_COMPAT
Arnd Bergmann6b960182009-11-06 23:10:54 -08002389static int do_siocgstamp(struct net *net, struct socket *sock,
2390 unsigned int cmd, struct compat_timeval __user *up)
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002391{
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002392 mm_segment_t old_fs = get_fs();
2393 struct timeval ktv;
2394 int err;
2395
2396 set_fs(KERNEL_DS);
Arnd Bergmann6b960182009-11-06 23:10:54 -08002397 err = sock_do_ioctl(net, sock, cmd, (unsigned long)&ktv);
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002398 set_fs(old_fs);
2399 if (!err) {
2400 err = put_user(ktv.tv_sec, &up->tv_sec);
2401 err |= __put_user(ktv.tv_usec, &up->tv_usec);
2402 }
2403 return err;
2404}
2405
Arnd Bergmann6b960182009-11-06 23:10:54 -08002406static int do_siocgstampns(struct net *net, struct socket *sock,
2407 unsigned int cmd, struct compat_timespec __user *up)
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002408{
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002409 mm_segment_t old_fs = get_fs();
2410 struct timespec kts;
2411 int err;
2412
2413 set_fs(KERNEL_DS);
Arnd Bergmann6b960182009-11-06 23:10:54 -08002414 err = sock_do_ioctl(net, sock, cmd, (unsigned long)&kts);
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002415 set_fs(old_fs);
2416 if (!err) {
2417 err = put_user(kts.tv_sec, &up->tv_sec);
2418 err |= __put_user(kts.tv_nsec, &up->tv_nsec);
2419 }
2420 return err;
2421}
2422
Arnd Bergmann6b960182009-11-06 23:10:54 -08002423static int dev_ifname32(struct net *net, struct compat_ifreq __user *uifr32)
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002424{
2425 struct ifreq __user *uifr;
2426 int err;
2427
2428 uifr = compat_alloc_user_space(sizeof(struct ifreq));
Arnd Bergmann6b960182009-11-06 23:10:54 -08002429 if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002430 return -EFAULT;
2431
Arnd Bergmann6b960182009-11-06 23:10:54 -08002432 err = dev_ioctl(net, SIOCGIFNAME, uifr);
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002433 if (err)
2434 return err;
2435
Arnd Bergmann6b960182009-11-06 23:10:54 -08002436 if (copy_in_user(uifr32, uifr, sizeof(struct compat_ifreq)))
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002437 return -EFAULT;
2438
2439 return 0;
2440}
2441
Arnd Bergmann6b960182009-11-06 23:10:54 -08002442static int dev_ifconf(struct net *net, struct compat_ifconf __user *uifc32)
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002443{
Arnd Bergmann6b960182009-11-06 23:10:54 -08002444 struct compat_ifconf ifc32;
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002445 struct ifconf ifc;
2446 struct ifconf __user *uifc;
Arnd Bergmann6b960182009-11-06 23:10:54 -08002447 struct compat_ifreq __user *ifr32;
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002448 struct ifreq __user *ifr;
2449 unsigned int i, j;
2450 int err;
2451
Arnd Bergmann6b960182009-11-06 23:10:54 -08002452 if (copy_from_user(&ifc32, uifc32, sizeof(struct compat_ifconf)))
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002453 return -EFAULT;
2454
2455 if (ifc32.ifcbuf == 0) {
2456 ifc32.ifc_len = 0;
2457 ifc.ifc_len = 0;
2458 ifc.ifc_req = NULL;
2459 uifc = compat_alloc_user_space(sizeof(struct ifconf));
2460 } else {
Arnd Bergmann6b960182009-11-06 23:10:54 -08002461 size_t len =((ifc32.ifc_len / sizeof (struct compat_ifreq)) + 1) *
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002462 sizeof (struct ifreq);
2463 uifc = compat_alloc_user_space(sizeof(struct ifconf) + len);
2464 ifc.ifc_len = len;
2465 ifr = ifc.ifc_req = (void __user *)(uifc + 1);
2466 ifr32 = compat_ptr(ifc32.ifcbuf);
Arnd Bergmann6b960182009-11-06 23:10:54 -08002467 for (i = 0; i < ifc32.ifc_len; i += sizeof (struct compat_ifreq)) {
2468 if (copy_in_user(ifr, ifr32, sizeof(struct compat_ifreq)))
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002469 return -EFAULT;
2470 ifr++;
2471 ifr32++;
2472 }
2473 }
2474 if (copy_to_user(uifc, &ifc, sizeof(struct ifconf)))
2475 return -EFAULT;
2476
Arnd Bergmann6b960182009-11-06 23:10:54 -08002477 err = dev_ioctl(net, SIOCGIFCONF, uifc);
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002478 if (err)
2479 return err;
2480
2481 if (copy_from_user(&ifc, uifc, sizeof(struct ifconf)))
2482 return -EFAULT;
2483
2484 ifr = ifc.ifc_req;
2485 ifr32 = compat_ptr(ifc32.ifcbuf);
2486 for (i = 0, j = 0;
Arnd Bergmann6b960182009-11-06 23:10:54 -08002487 i + sizeof (struct compat_ifreq) <= ifc32.ifc_len && j < ifc.ifc_len;
2488 i += sizeof (struct compat_ifreq), j += sizeof (struct ifreq)) {
2489 if (copy_in_user(ifr32, ifr, sizeof (struct compat_ifreq)))
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002490 return -EFAULT;
2491 ifr32++;
2492 ifr++;
2493 }
2494
2495 if (ifc32.ifcbuf == 0) {
2496 /* Translate from 64-bit structure multiple to
2497 * a 32-bit one.
2498 */
2499 i = ifc.ifc_len;
Arnd Bergmann6b960182009-11-06 23:10:54 -08002500 i = ((i / sizeof(struct ifreq)) * sizeof(struct compat_ifreq));
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002501 ifc32.ifc_len = i;
2502 } else {
2503 ifc32.ifc_len = i;
2504 }
Arnd Bergmann6b960182009-11-06 23:10:54 -08002505 if (copy_to_user(uifc32, &ifc32, sizeof(struct compat_ifconf)))
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002506 return -EFAULT;
2507
2508 return 0;
2509}
2510
Arnd Bergmann6b960182009-11-06 23:10:54 -08002511static int ethtool_ioctl(struct net *net, struct compat_ifreq __user *ifr32)
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002512{
2513 struct ifreq __user *ifr;
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002514 u32 data;
2515 void __user *datap;
2516
2517 ifr = compat_alloc_user_space(sizeof(*ifr));
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002518
2519 if (copy_in_user(&ifr->ifr_name, &ifr32->ifr_name, IFNAMSIZ))
2520 return -EFAULT;
2521
2522 if (get_user(data, &ifr32->ifr_ifru.ifru_data))
2523 return -EFAULT;
2524
2525 datap = compat_ptr(data);
2526 if (put_user(datap, &ifr->ifr_ifru.ifru_data))
2527 return -EFAULT;
2528
Arnd Bergmann6b960182009-11-06 23:10:54 -08002529 return dev_ioctl(net, SIOCETHTOOL, ifr);
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002530}
2531
Arnd Bergmann7a50a242009-11-08 20:57:03 -08002532static int compat_siocwandev(struct net *net, struct compat_ifreq __user *uifr32)
2533{
2534 void __user *uptr;
2535 compat_uptr_t uptr32;
2536 struct ifreq __user *uifr;
2537
2538 uifr = compat_alloc_user_space(sizeof (*uifr));
2539 if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
2540 return -EFAULT;
2541
2542 if (get_user(uptr32, &uifr32->ifr_settings.ifs_ifsu))
2543 return -EFAULT;
2544
2545 uptr = compat_ptr(uptr32);
2546
2547 if (put_user(uptr, &uifr->ifr_settings.ifs_ifsu.raw_hdlc))
2548 return -EFAULT;
2549
2550 return dev_ioctl(net, SIOCWANDEV, uifr);
2551}
2552
Arnd Bergmann6b960182009-11-06 23:10:54 -08002553static int bond_ioctl(struct net *net, unsigned int cmd,
2554 struct compat_ifreq __user *ifr32)
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002555{
2556 struct ifreq kifr;
2557 struct ifreq __user *uifr;
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002558 mm_segment_t old_fs;
2559 int err;
2560 u32 data;
2561 void __user *datap;
2562
2563 switch (cmd) {
2564 case SIOCBONDENSLAVE:
2565 case SIOCBONDRELEASE:
2566 case SIOCBONDSETHWADDR:
2567 case SIOCBONDCHANGEACTIVE:
Arnd Bergmann6b960182009-11-06 23:10:54 -08002568 if (copy_from_user(&kifr, ifr32, sizeof(struct compat_ifreq)))
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002569 return -EFAULT;
2570
2571 old_fs = get_fs();
2572 set_fs (KERNEL_DS);
Arnd Bergmann6b960182009-11-06 23:10:54 -08002573 err = dev_ioctl(net, cmd, &kifr);
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002574 set_fs (old_fs);
2575
2576 return err;
2577 case SIOCBONDSLAVEINFOQUERY:
2578 case SIOCBONDINFOQUERY:
2579 uifr = compat_alloc_user_space(sizeof(*uifr));
2580 if (copy_in_user(&uifr->ifr_name, &ifr32->ifr_name, IFNAMSIZ))
2581 return -EFAULT;
2582
2583 if (get_user(data, &ifr32->ifr_ifru.ifru_data))
2584 return -EFAULT;
2585
2586 datap = compat_ptr(data);
2587 if (put_user(datap, &uifr->ifr_ifru.ifru_data))
2588 return -EFAULT;
2589
Arnd Bergmann6b960182009-11-06 23:10:54 -08002590 return dev_ioctl(net, cmd, uifr);
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002591 default:
2592 return -EINVAL;
2593 };
2594}
2595
Arnd Bergmann6b960182009-11-06 23:10:54 -08002596static int siocdevprivate_ioctl(struct net *net, unsigned int cmd,
2597 struct compat_ifreq __user *u_ifreq32)
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002598{
2599 struct ifreq __user *u_ifreq64;
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002600 char tmp_buf[IFNAMSIZ];
2601 void __user *data64;
2602 u32 data32;
2603
2604 if (copy_from_user(&tmp_buf[0], &(u_ifreq32->ifr_ifrn.ifrn_name[0]),
2605 IFNAMSIZ))
2606 return -EFAULT;
2607 if (__get_user(data32, &u_ifreq32->ifr_ifru.ifru_data))
2608 return -EFAULT;
2609 data64 = compat_ptr(data32);
2610
2611 u_ifreq64 = compat_alloc_user_space(sizeof(*u_ifreq64));
2612
2613 /* Don't check these user accesses, just let that get trapped
2614 * in the ioctl handler instead.
2615 */
2616 if (copy_to_user(&u_ifreq64->ifr_ifrn.ifrn_name[0], &tmp_buf[0],
2617 IFNAMSIZ))
2618 return -EFAULT;
2619 if (__put_user(data64, &u_ifreq64->ifr_ifru.ifru_data))
2620 return -EFAULT;
2621
Arnd Bergmann6b960182009-11-06 23:10:54 -08002622 return dev_ioctl(net, cmd, u_ifreq64);
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002623}
2624
Arnd Bergmann6b960182009-11-06 23:10:54 -08002625static int dev_ifsioc(struct net *net, struct socket *sock,
2626 unsigned int cmd, struct compat_ifreq __user *uifr32)
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002627{
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00002628 struct ifreq __user *uifr;
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002629 int err;
2630
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00002631 uifr = compat_alloc_user_space(sizeof(*uifr));
2632 if (copy_in_user(uifr, uifr32, sizeof(*uifr32)))
2633 return -EFAULT;
2634
2635 err = sock_do_ioctl(net, sock, cmd, (unsigned long)uifr);
2636
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002637 if (!err) {
2638 switch (cmd) {
2639 case SIOCGIFFLAGS:
2640 case SIOCGIFMETRIC:
2641 case SIOCGIFMTU:
2642 case SIOCGIFMEM:
2643 case SIOCGIFHWADDR:
2644 case SIOCGIFINDEX:
2645 case SIOCGIFADDR:
2646 case SIOCGIFBRDADDR:
2647 case SIOCGIFDSTADDR:
2648 case SIOCGIFNETMASK:
Arnd Bergmannfab25322009-11-08 20:56:21 -08002649 case SIOCGIFPFLAGS:
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002650 case SIOCGIFTXQLEN:
Arnd Bergmannfab25322009-11-08 20:56:21 -08002651 case SIOCGMIIPHY:
2652 case SIOCGMIIREG:
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00002653 if (copy_in_user(uifr32, uifr, sizeof(*uifr32)))
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002654 err = -EFAULT;
2655 break;
2656 }
2657 }
2658 return err;
2659}
2660
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00002661static int compat_sioc_ifmap(struct net *net, unsigned int cmd,
2662 struct compat_ifreq __user *uifr32)
2663{
2664 struct ifreq ifr;
2665 struct compat_ifmap __user *uifmap32;
2666 mm_segment_t old_fs;
2667 int err;
2668
2669 uifmap32 = &uifr32->ifr_ifru.ifru_map;
2670 err = copy_from_user(&ifr, uifr32, sizeof(ifr.ifr_name));
2671 err |= __get_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
2672 err |= __get_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
2673 err |= __get_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
2674 err |= __get_user(ifr.ifr_map.irq, &uifmap32->irq);
2675 err |= __get_user(ifr.ifr_map.dma, &uifmap32->dma);
2676 err |= __get_user(ifr.ifr_map.port, &uifmap32->port);
2677 if (err)
2678 return -EFAULT;
2679
2680 old_fs = get_fs();
2681 set_fs (KERNEL_DS);
2682 err = dev_ioctl(net, cmd, (void __user *)&ifr);
2683 set_fs (old_fs);
2684
2685 if (cmd == SIOCGIFMAP && !err) {
2686 err = copy_to_user(uifr32, &ifr, sizeof(ifr.ifr_name));
2687 err |= __put_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
2688 err |= __put_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
2689 err |= __put_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
2690 err |= __put_user(ifr.ifr_map.irq, &uifmap32->irq);
2691 err |= __put_user(ifr.ifr_map.dma, &uifmap32->dma);
2692 err |= __put_user(ifr.ifr_map.port, &uifmap32->port);
2693 if (err)
2694 err = -EFAULT;
2695 }
2696 return err;
2697}
2698
2699static int compat_siocshwtstamp(struct net *net, struct compat_ifreq __user *uifr32)
2700{
2701 void __user *uptr;
2702 compat_uptr_t uptr32;
2703 struct ifreq __user *uifr;
2704
2705 uifr = compat_alloc_user_space(sizeof (*uifr));
2706 if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
2707 return -EFAULT;
2708
2709 if (get_user(uptr32, &uifr32->ifr_data))
2710 return -EFAULT;
2711
2712 uptr = compat_ptr(uptr32);
2713
2714 if (put_user(uptr, &uifr->ifr_data))
2715 return -EFAULT;
2716
2717 return dev_ioctl(net, SIOCSHWTSTAMP, uifr);
2718}
2719
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002720struct rtentry32 {
2721 u32 rt_pad1;
2722 struct sockaddr rt_dst; /* target address */
2723 struct sockaddr rt_gateway; /* gateway addr (RTF_GATEWAY) */
2724 struct sockaddr rt_genmask; /* target network mask (IP) */
2725 unsigned short rt_flags;
2726 short rt_pad2;
2727 u32 rt_pad3;
2728 unsigned char rt_tos;
2729 unsigned char rt_class;
2730 short rt_pad4;
2731 short rt_metric; /* +1 for binary compatibility! */
2732 /* char * */ u32 rt_dev; /* forcing the device at add */
2733 u32 rt_mtu; /* per route MTU/Window */
2734 u32 rt_window; /* Window clamping */
2735 unsigned short rt_irtt; /* Initial RTT */
2736};
2737
2738struct in6_rtmsg32 {
2739 struct in6_addr rtmsg_dst;
2740 struct in6_addr rtmsg_src;
2741 struct in6_addr rtmsg_gateway;
2742 u32 rtmsg_type;
2743 u16 rtmsg_dst_len;
2744 u16 rtmsg_src_len;
2745 u32 rtmsg_metric;
2746 u32 rtmsg_info;
2747 u32 rtmsg_flags;
2748 s32 rtmsg_ifindex;
2749};
2750
Arnd Bergmann6b960182009-11-06 23:10:54 -08002751static int routing_ioctl(struct net *net, struct socket *sock,
2752 unsigned int cmd, void __user *argp)
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002753{
2754 int ret;
2755 void *r = NULL;
2756 struct in6_rtmsg r6;
2757 struct rtentry r4;
2758 char devname[16];
2759 u32 rtdev;
2760 mm_segment_t old_fs = get_fs();
2761
Arnd Bergmann6b960182009-11-06 23:10:54 -08002762 if (sock && sock->sk && sock->sk->sk_family == AF_INET6) { /* ipv6 */
2763 struct in6_rtmsg32 __user *ur6 = argp;
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002764 ret = copy_from_user (&r6.rtmsg_dst, &(ur6->rtmsg_dst),
2765 3 * sizeof(struct in6_addr));
2766 ret |= __get_user (r6.rtmsg_type, &(ur6->rtmsg_type));
2767 ret |= __get_user (r6.rtmsg_dst_len, &(ur6->rtmsg_dst_len));
2768 ret |= __get_user (r6.rtmsg_src_len, &(ur6->rtmsg_src_len));
2769 ret |= __get_user (r6.rtmsg_metric, &(ur6->rtmsg_metric));
2770 ret |= __get_user (r6.rtmsg_info, &(ur6->rtmsg_info));
2771 ret |= __get_user (r6.rtmsg_flags, &(ur6->rtmsg_flags));
2772 ret |= __get_user (r6.rtmsg_ifindex, &(ur6->rtmsg_ifindex));
2773
2774 r = (void *) &r6;
2775 } else { /* ipv4 */
Arnd Bergmann6b960182009-11-06 23:10:54 -08002776 struct rtentry32 __user *ur4 = argp;
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002777 ret = copy_from_user (&r4.rt_dst, &(ur4->rt_dst),
2778 3 * sizeof(struct sockaddr));
2779 ret |= __get_user (r4.rt_flags, &(ur4->rt_flags));
2780 ret |= __get_user (r4.rt_metric, &(ur4->rt_metric));
2781 ret |= __get_user (r4.rt_mtu, &(ur4->rt_mtu));
2782 ret |= __get_user (r4.rt_window, &(ur4->rt_window));
2783 ret |= __get_user (r4.rt_irtt, &(ur4->rt_irtt));
2784 ret |= __get_user (rtdev, &(ur4->rt_dev));
2785 if (rtdev) {
2786 ret |= copy_from_user (devname, compat_ptr(rtdev), 15);
2787 r4.rt_dev = devname; devname[15] = 0;
2788 } else
2789 r4.rt_dev = NULL;
2790
2791 r = (void *) &r4;
2792 }
2793
2794 if (ret) {
2795 ret = -EFAULT;
2796 goto out;
2797 }
2798
2799 set_fs (KERNEL_DS);
Arnd Bergmann6b960182009-11-06 23:10:54 -08002800 ret = sock_do_ioctl(net, sock, cmd, (unsigned long) r);
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002801 set_fs (old_fs);
2802
2803out:
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002804 return ret;
2805}
2806
2807/* Since old style bridge ioctl's endup using SIOCDEVPRIVATE
2808 * for some operations; this forces use of the newer bridge-utils that
2809 * use compatiable ioctls
2810 */
Arnd Bergmann6b960182009-11-06 23:10:54 -08002811static int old_bridge_ioctl(compat_ulong_t __user *argp)
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002812{
Arnd Bergmann6b960182009-11-06 23:10:54 -08002813 compat_ulong_t tmp;
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002814
Arnd Bergmann6b960182009-11-06 23:10:54 -08002815 if (get_user(tmp, argp))
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002816 return -EFAULT;
2817 if (tmp == BRCTL_GET_VERSION)
2818 return BRCTL_VERSION + 1;
2819 return -EINVAL;
2820}
2821
Arnd Bergmann6b960182009-11-06 23:10:54 -08002822static int compat_sock_ioctl_trans(struct file *file, struct socket *sock,
2823 unsigned int cmd, unsigned long arg)
2824{
2825 void __user *argp = compat_ptr(arg);
2826 struct sock *sk = sock->sk;
2827 struct net *net = sock_net(sk);
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002828
Arnd Bergmann6b960182009-11-06 23:10:54 -08002829 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15))
2830 return siocdevprivate_ioctl(net, cmd, argp);
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002831
Arnd Bergmann6b960182009-11-06 23:10:54 -08002832 switch (cmd) {
2833 case SIOCSIFBR:
2834 case SIOCGIFBR:
2835 return old_bridge_ioctl(argp);
2836 case SIOCGIFNAME:
2837 return dev_ifname32(net, argp);
2838 case SIOCGIFCONF:
2839 return dev_ifconf(net, argp);
2840 case SIOCETHTOOL:
2841 return ethtool_ioctl(net, argp);
Arnd Bergmann7a50a242009-11-08 20:57:03 -08002842 case SIOCWANDEV:
2843 return compat_siocwandev(net, argp);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00002844 case SIOCGIFMAP:
2845 case SIOCSIFMAP:
2846 return compat_sioc_ifmap(net, cmd, argp);
Arnd Bergmann6b960182009-11-06 23:10:54 -08002847 case SIOCBONDENSLAVE:
2848 case SIOCBONDRELEASE:
2849 case SIOCBONDSETHWADDR:
2850 case SIOCBONDSLAVEINFOQUERY:
2851 case SIOCBONDINFOQUERY:
2852 case SIOCBONDCHANGEACTIVE:
2853 return bond_ioctl(net, cmd, argp);
2854 case SIOCADDRT:
2855 case SIOCDELRT:
2856 return routing_ioctl(net, sock, cmd, argp);
2857 case SIOCGSTAMP:
2858 return do_siocgstamp(net, sock, cmd, argp);
2859 case SIOCGSTAMPNS:
2860 return do_siocgstampns(net, sock, cmd, argp);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00002861 case SIOCSHWTSTAMP:
2862 return compat_siocshwtstamp(net, argp);
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002863
Arnd Bergmann6b960182009-11-06 23:10:54 -08002864 case FIOSETOWN:
2865 case SIOCSPGRP:
2866 case FIOGETOWN:
2867 case SIOCGPGRP:
2868 case SIOCBRADDBR:
2869 case SIOCBRDELBR:
2870 case SIOCGIFVLAN:
2871 case SIOCSIFVLAN:
2872 case SIOCADDDLCI:
2873 case SIOCDELDLCI:
2874 return sock_ioctl(file, cmd, arg);
2875
2876 case SIOCGIFFLAGS:
2877 case SIOCSIFFLAGS:
2878 case SIOCGIFMETRIC:
2879 case SIOCSIFMETRIC:
2880 case SIOCGIFMTU:
2881 case SIOCSIFMTU:
2882 case SIOCGIFMEM:
2883 case SIOCSIFMEM:
2884 case SIOCGIFHWADDR:
2885 case SIOCSIFHWADDR:
2886 case SIOCADDMULTI:
2887 case SIOCDELMULTI:
2888 case SIOCGIFINDEX:
Arnd Bergmann6b960182009-11-06 23:10:54 -08002889 case SIOCGIFADDR:
2890 case SIOCSIFADDR:
2891 case SIOCSIFHWBROADCAST:
Arnd Bergmann6b960182009-11-06 23:10:54 -08002892 case SIOCDIFADDR:
Arnd Bergmann6b960182009-11-06 23:10:54 -08002893 case SIOCGIFBRDADDR:
2894 case SIOCSIFBRDADDR:
2895 case SIOCGIFDSTADDR:
2896 case SIOCSIFDSTADDR:
2897 case SIOCGIFNETMASK:
2898 case SIOCSIFNETMASK:
2899 case SIOCSIFPFLAGS:
2900 case SIOCGIFPFLAGS:
2901 case SIOCGIFTXQLEN:
2902 case SIOCSIFTXQLEN:
2903 case SIOCBRADDIF:
2904 case SIOCBRDELIF:
Arnd Bergmann9177efd2009-11-06 08:09:09 +00002905 case SIOCSIFNAME:
2906 case SIOCGMIIPHY:
2907 case SIOCGMIIREG:
2908 case SIOCSMIIREG:
Arnd Bergmann6b960182009-11-06 23:10:54 -08002909 return dev_ifsioc(net, sock, cmd, argp);
Arnd Bergmann9177efd2009-11-06 08:09:09 +00002910
Arnd Bergmann6b960182009-11-06 23:10:54 -08002911 case SIOCSARP:
2912 case SIOCGARP:
2913 case SIOCDARP:
Arnd Bergmann6b960182009-11-06 23:10:54 -08002914 case SIOCATMARK:
Arnd Bergmann9177efd2009-11-06 08:09:09 +00002915 return sock_do_ioctl(net, sock, cmd, arg);
2916 }
2917
2918 /* Prevent warning from compat_sys_ioctl, these always
2919 * result in -EINVAL in the native case anyway. */
2920 switch (cmd) {
2921 case SIOCRTMSG:
2922 case SIOCGIFCOUNT:
Arnd Bergmann6b960182009-11-06 23:10:54 -08002923 case SIOCSRARP:
2924 case SIOCGRARP:
2925 case SIOCDRARP:
Arnd Bergmann9177efd2009-11-06 08:09:09 +00002926 case SIOCSIFLINK:
2927 case SIOCGIFSLAVE:
2928 case SIOCSIFSLAVE:
2929 return -EINVAL;
Arnd Bergmann6b960182009-11-06 23:10:54 -08002930 }
2931
2932 return -ENOIOCTLCMD;
2933}
Arnd Bergmann7a2293872009-11-06 23:00:29 -08002934
Shaun Pereira89bbfc92006-03-21 23:58:08 -08002935static long compat_sock_ioctl(struct file *file, unsigned cmd,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002936 unsigned long arg)
Shaun Pereira89bbfc92006-03-21 23:58:08 -08002937{
2938 struct socket *sock = file->private_data;
2939 int ret = -ENOIOCTLCMD;
David S. Miller87de87d2008-06-03 09:14:03 -07002940 struct sock *sk;
2941 struct net *net;
2942
2943 sk = sock->sk;
2944 net = sock_net(sk);
Shaun Pereira89bbfc92006-03-21 23:58:08 -08002945
2946 if (sock->ops->compat_ioctl)
2947 ret = sock->ops->compat_ioctl(sock, cmd, arg);
2948
David S. Miller87de87d2008-06-03 09:14:03 -07002949 if (ret == -ENOIOCTLCMD &&
2950 (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST))
2951 ret = compat_wext_handle_ioctl(net, cmd, arg);
2952
Arnd Bergmann6b960182009-11-06 23:10:54 -08002953 if (ret == -ENOIOCTLCMD)
2954 ret = compat_sock_ioctl_trans(file, sock, cmd, arg);
2955
Shaun Pereira89bbfc92006-03-21 23:58:08 -08002956 return ret;
2957}
2958#endif
2959
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07002960int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen)
2961{
2962 return sock->ops->bind(sock, addr, addrlen);
2963}
2964
2965int kernel_listen(struct socket *sock, int backlog)
2966{
2967 return sock->ops->listen(sock, backlog);
2968}
2969
2970int kernel_accept(struct socket *sock, struct socket **newsock, int flags)
2971{
2972 struct sock *sk = sock->sk;
2973 int err;
2974
2975 err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
2976 newsock);
2977 if (err < 0)
2978 goto done;
2979
2980 err = sock->ops->accept(sock, *newsock, flags);
2981 if (err < 0) {
2982 sock_release(*newsock);
Tony Battersbyfa8705b2007-10-10 21:09:04 -07002983 *newsock = NULL;
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07002984 goto done;
2985 }
2986
2987 (*newsock)->ops = sock->ops;
Wei Yongjun1b085342008-12-18 19:35:10 -08002988 __module_get((*newsock)->ops->owner);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07002989
2990done:
2991 return err;
2992}
2993
2994int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
YOSHIFUJI Hideaki4768fbc2007-02-09 23:25:31 +09002995 int flags)
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07002996{
2997 return sock->ops->connect(sock, addr, addrlen, flags);
2998}
2999
3000int kernel_getsockname(struct socket *sock, struct sockaddr *addr,
3001 int *addrlen)
3002{
3003 return sock->ops->getname(sock, addr, addrlen, 0);
3004}
3005
3006int kernel_getpeername(struct socket *sock, struct sockaddr *addr,
3007 int *addrlen)
3008{
3009 return sock->ops->getname(sock, addr, addrlen, 1);
3010}
3011
3012int kernel_getsockopt(struct socket *sock, int level, int optname,
3013 char *optval, int *optlen)
3014{
3015 mm_segment_t oldfs = get_fs();
3016 int err;
3017
3018 set_fs(KERNEL_DS);
3019 if (level == SOL_SOCKET)
3020 err = sock_getsockopt(sock, level, optname, optval, optlen);
3021 else
3022 err = sock->ops->getsockopt(sock, level, optname, optval,
3023 optlen);
3024 set_fs(oldfs);
3025 return err;
3026}
3027
3028int kernel_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07003029 char *optval, unsigned int optlen)
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003030{
3031 mm_segment_t oldfs = get_fs();
3032 int err;
3033
3034 set_fs(KERNEL_DS);
3035 if (level == SOL_SOCKET)
3036 err = sock_setsockopt(sock, level, optname, optval, optlen);
3037 else
3038 err = sock->ops->setsockopt(sock, level, optname, optval,
3039 optlen);
3040 set_fs(oldfs);
3041 return err;
3042}
3043
3044int kernel_sendpage(struct socket *sock, struct page *page, int offset,
3045 size_t size, int flags)
3046{
3047 if (sock->ops->sendpage)
3048 return sock->ops->sendpage(sock, page, offset, size, flags);
3049
3050 return sock_no_sendpage(sock, page, offset, size, flags);
3051}
3052
3053int kernel_sock_ioctl(struct socket *sock, int cmd, unsigned long arg)
3054{
3055 mm_segment_t oldfs = get_fs();
3056 int err;
3057
3058 set_fs(KERNEL_DS);
3059 err = sock->ops->ioctl(sock, cmd, arg);
3060 set_fs(oldfs);
3061
3062 return err;
3063}
3064
Trond Myklebust91cf45f2007-11-12 18:10:39 -08003065int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how)
3066{
3067 return sock->ops->shutdown(sock, how);
3068}
3069
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070EXPORT_SYMBOL(sock_create);
3071EXPORT_SYMBOL(sock_create_kern);
3072EXPORT_SYMBOL(sock_create_lite);
3073EXPORT_SYMBOL(sock_map_fd);
3074EXPORT_SYMBOL(sock_recvmsg);
3075EXPORT_SYMBOL(sock_register);
3076EXPORT_SYMBOL(sock_release);
3077EXPORT_SYMBOL(sock_sendmsg);
3078EXPORT_SYMBOL(sock_unregister);
3079EXPORT_SYMBOL(sock_wake_async);
3080EXPORT_SYMBOL(sockfd_lookup);
3081EXPORT_SYMBOL(kernel_sendmsg);
3082EXPORT_SYMBOL(kernel_recvmsg);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003083EXPORT_SYMBOL(kernel_bind);
3084EXPORT_SYMBOL(kernel_listen);
3085EXPORT_SYMBOL(kernel_accept);
3086EXPORT_SYMBOL(kernel_connect);
3087EXPORT_SYMBOL(kernel_getsockname);
3088EXPORT_SYMBOL(kernel_getpeername);
3089EXPORT_SYMBOL(kernel_getsockopt);
3090EXPORT_SYMBOL(kernel_setsockopt);
3091EXPORT_SYMBOL(kernel_sendpage);
3092EXPORT_SYMBOL(kernel_sock_ioctl);
Trond Myklebust91cf45f2007-11-12 18:10:39 -08003093EXPORT_SYMBOL(kernel_sock_shutdown);