blob: 901d709a7be08fc89058c3b54ad8fae7ae7212b2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * NET An implementation of the SOCKET network access protocol.
3 *
4 * Version: @(#)socket.c 1.1.93 18/02/95
5 *
6 * Authors: Orest Zborowski, <obz@Kodak.COM>
Jesper Juhl02c30a82005-05-05 16:16:16 -07007 * Ross Biro
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
9 *
10 * Fixes:
11 * Anonymous : NOTSOCK/BADF cleanup. Error fix in
12 * shutdown()
13 * Alan Cox : verify_area() fixes
14 * Alan Cox : Removed DDI
15 * Jonathan Kamens : SOCK_DGRAM reconnect bug
16 * Alan Cox : Moved a load of checks to the very
17 * top level.
18 * Alan Cox : Move address structures to/from user
19 * mode above the protocol layers.
20 * Rob Janssen : Allow 0 length sends.
21 * Alan Cox : Asynchronous I/O support (cribbed from the
22 * tty drivers).
23 * Niibe Yutaka : Asynchronous I/O for writes (4.4BSD style)
24 * Jeff Uphoff : Made max number of sockets command-line
25 * configurable.
26 * Matti Aarnio : Made the number of sockets dynamic,
27 * to be allocated when needed, and mr.
28 * Uphoff's max is used as max to be
29 * allowed to allocate.
30 * Linus : Argh. removed all the socket allocation
31 * altogether: it's in the inode now.
32 * Alan Cox : Made sock_alloc()/sock_release() public
33 * for NetROM and future kernel nfsd type
34 * stuff.
35 * Alan Cox : sendmsg/recvmsg basics.
36 * Tom Dyas : Export net symbols.
37 * Marcin Dalecki : Fixed problems with CONFIG_NET="n".
38 * Alan Cox : Added thread locking to sys_* calls
39 * for sockets. May have errors at the
40 * moment.
41 * Kevin Buhr : Fixed the dumb errors in the above.
42 * Andi Kleen : Some small cleanups, optimizations,
43 * and fixed a copy_from_user() bug.
44 * Tigran Aivazian : sys_send(args) calls sys_sendto(args, NULL, 0)
Stephen Hemminger89bddce2006-09-01 00:19:31 -070045 * Tigran Aivazian : Made listen(2) backlog sanity checks
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 * protocol-independent
47 *
48 *
49 * This program is free software; you can redistribute it and/or
50 * modify it under the terms of the GNU General Public License
51 * as published by the Free Software Foundation; either version
52 * 2 of the License, or (at your option) any later version.
53 *
54 *
55 * This module is effectively the top level interface to the BSD socket
Stephen Hemminger89bddce2006-09-01 00:19:31 -070056 * paradigm.
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 *
58 * Based upon Swansea University Computer Society NET3.039
59 */
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <linux/socket.h>
63#include <linux/file.h>
64#include <linux/net.h>
65#include <linux/interrupt.h>
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -070066#include <linux/thread_info.h>
Stephen Hemminger55737fd2006-09-01 00:23:39 -070067#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/netdevice.h>
69#include <linux/proc_fs.h>
70#include <linux/seq_file.h>
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080071#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include <linux/wanrouter.h>
73#include <linux/if_bridge.h>
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -030074#include <linux/if_frad.h>
75#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#include <linux/init.h>
77#include <linux/poll.h>
78#include <linux/cache.h>
79#include <linux/module.h>
80#include <linux/highmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#include <linux/mount.h>
82#include <linux/security.h>
83#include <linux/syscalls.h>
84#include <linux/compat.h>
85#include <linux/kmod.h>
David Woodhouse3ec3b2f2005-05-17 12:08:48 +010086#include <linux/audit.h>
Adrian Bunkd86b5e02006-01-21 00:46:55 +010087#include <linux/wireless.h>
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -070088#include <linux/nsproxy.h>
Nick Black1fd7317d2009-09-22 16:43:33 -070089#include <linux/magic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91#include <asm/uaccess.h>
92#include <asm/unistd.h>
93
94#include <net/compat.h>
David S. Miller87de87d2008-06-03 09:14:03 -070095#include <net/wext.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97#include <net/sock.h>
98#include <linux/netfilter.h>
99
Arnd Bergmann6b960182009-11-06 23:10:54 -0800100#include <linux/if_tun.h>
101#include <linux/ipv6_route.h>
102#include <linux/route.h>
103#include <linux/atmdev.h>
104#include <linux/atmarp.h>
105#include <linux/atmsvc.h>
106#include <linux/atmlec.h>
107#include <linux/atmclip.h>
108#include <linux/atmmpc.h>
109#include <linux/atm_tcp.h>
110#include <linux/sonet.h>
111#include <linux/sockios.h>
112#include <linux/atalk.h>
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114static int sock_no_open(struct inode *irrelevant, struct file *dontcare);
Badari Pulavarty027445c2006-09-30 23:28:46 -0700115static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov,
116 unsigned long nr_segs, loff_t pos);
117static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov,
118 unsigned long nr_segs, loff_t pos);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700119static int sock_mmap(struct file *file, struct vm_area_struct *vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121static int sock_close(struct inode *inode, struct file *file);
122static unsigned int sock_poll(struct file *file,
123 struct poll_table_struct *wait);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700124static long sock_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
Shaun Pereira89bbfc92006-03-21 23:58:08 -0800125#ifdef CONFIG_COMPAT
126static long compat_sock_ioctl(struct file *file,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700127 unsigned int cmd, unsigned long arg);
Shaun Pereira89bbfc92006-03-21 23:58:08 -0800128#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129static int sock_fasync(int fd, struct file *filp, int on);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130static ssize_t sock_sendpage(struct file *file, struct page *page,
131 int offset, size_t size, loff_t *ppos, int more);
Jens Axboe9c55e012007-11-06 23:30:13 -0800132static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
133 struct pipe_inode_info *pipe, size_t len,
134 unsigned int flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136/*
137 * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
138 * in the operation structures but are done directly via the socketcall() multiplexor.
139 */
140
Arjan van de Venda7071d2007-02-12 00:55:36 -0800141static const struct file_operations socket_file_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 .owner = THIS_MODULE,
143 .llseek = no_llseek,
144 .aio_read = sock_aio_read,
145 .aio_write = sock_aio_write,
146 .poll = sock_poll,
147 .unlocked_ioctl = sock_ioctl,
Shaun Pereira89bbfc92006-03-21 23:58:08 -0800148#ifdef CONFIG_COMPAT
149 .compat_ioctl = compat_sock_ioctl,
150#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 .mmap = sock_mmap,
152 .open = sock_no_open, /* special open code to disallow open via /proc */
153 .release = sock_close,
154 .fasync = sock_fasync,
Jens Axboe5274f052006-03-30 15:15:30 +0200155 .sendpage = sock_sendpage,
156 .splice_write = generic_splice_sendpage,
Jens Axboe9c55e012007-11-06 23:30:13 -0800157 .splice_read = sock_splice_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158};
159
160/*
161 * The protocol list. Each protocol is registered in here.
162 */
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164static DEFINE_SPINLOCK(net_family_lock);
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -0700165static const struct net_proto_family *net_families[NPROTO] __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167/*
168 * Statistics counters of the socket lists
169 */
170
171static DEFINE_PER_CPU(int, sockets_in_use) = 0;
172
173/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700174 * Support routines.
175 * Move socket addresses back and forth across the kernel/user
176 * divide and look after the messy bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 */
178
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700179#define MAX_SOCK_ADDR 128 /* 108 for Unix domain -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 16 for IP, 16 for IPX,
181 24 for IPv6,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700182 about 80 for AX.25
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 must be at least one bigger than
184 the AF_UNIX size (see net/unix/af_unix.c
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700185 :unix_mkname()).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188/**
189 * move_addr_to_kernel - copy a socket address into kernel space
190 * @uaddr: Address in user space
191 * @kaddr: Address in kernel space
192 * @ulen: Length in user space
193 *
194 * The address is copied into kernel space. If the provided address is
195 * too long an error code of -EINVAL is returned. If the copy gives
196 * invalid addresses -EFAULT is returned. On a success 0 is returned.
197 */
198
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -0700199int move_addr_to_kernel(void __user *uaddr, int ulen, struct sockaddr *kaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -0700201 if (ulen < 0 || ulen > sizeof(struct sockaddr_storage))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700203 if (ulen == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 return 0;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700205 if (copy_from_user(kaddr, uaddr, ulen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 return -EFAULT;
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100207 return audit_sockaddr(ulen, kaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208}
209
210/**
211 * move_addr_to_user - copy an address to user space
212 * @kaddr: kernel space address
213 * @klen: length of address in kernel
214 * @uaddr: user space address
215 * @ulen: pointer to user length field
216 *
217 * The value pointed to by ulen on entry is the buffer length available.
218 * This is overwritten with the buffer space used. -EINVAL is returned
219 * if an overlong buffer is specified or a negative buffer size. -EFAULT
220 * is returned if either the buffer or the length field are not
221 * accessible.
222 * After copying the data up to the limit the user specifies, the true
223 * length of the data is written over the length limit the user
224 * specified. Zero is returned for a success.
225 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700226
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -0700227int move_addr_to_user(struct sockaddr *kaddr, int klen, void __user *uaddr,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700228 int __user *ulen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
230 int err;
231 int len;
232
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700233 err = get_user(len, ulen);
234 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 return err;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700236 if (len > klen)
237 len = klen;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -0700238 if (len < 0 || len > sizeof(struct sockaddr_storage))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700240 if (len) {
Steve Grubbd6fe3942006-03-30 12:20:22 -0500241 if (audit_sockaddr(klen, kaddr))
242 return -ENOMEM;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700243 if (copy_to_user(uaddr, kaddr, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 return -EFAULT;
245 }
246 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700247 * "fromlen shall refer to the value before truncation.."
248 * 1003.1g
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 */
250 return __put_user(klen, ulen);
251}
252
Christoph Lametere18b8902006-12-06 20:33:20 -0800253static struct kmem_cache *sock_inode_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255static struct inode *sock_alloc_inode(struct super_block *sb)
256{
257 struct socket_alloc *ei;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700258
Christoph Lametere94b1762006-12-06 20:33:17 -0800259 ei = kmem_cache_alloc(sock_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 if (!ei)
261 return NULL;
262 init_waitqueue_head(&ei->socket.wait);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 ei->socket.fasync_list = NULL;
265 ei->socket.state = SS_UNCONNECTED;
266 ei->socket.flags = 0;
267 ei->socket.ops = NULL;
268 ei->socket.sk = NULL;
269 ei->socket.file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271 return &ei->vfs_inode;
272}
273
274static void sock_destroy_inode(struct inode *inode)
275{
276 kmem_cache_free(sock_inode_cachep,
277 container_of(inode, struct socket_alloc, vfs_inode));
278}
279
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700280static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700282 struct socket_alloc *ei = (struct socket_alloc *)foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Christoph Lametera35afb82007-05-16 22:10:57 -0700284 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287static int init_inodecache(void)
288{
289 sock_inode_cachep = kmem_cache_create("sock_inode_cache",
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700290 sizeof(struct socket_alloc),
291 0,
292 (SLAB_HWCACHE_ALIGN |
293 SLAB_RECLAIM_ACCOUNT |
294 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +0900295 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 if (sock_inode_cachep == NULL)
297 return -ENOMEM;
298 return 0;
299}
300
Alexey Dobriyanb87221d2009-09-21 17:01:09 -0700301static const struct super_operations sockfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 .alloc_inode = sock_alloc_inode,
303 .destroy_inode =sock_destroy_inode,
304 .statfs = simple_statfs,
305};
306
David Howells454e2392006-06-23 02:02:57 -0700307static int sockfs_get_sb(struct file_system_type *fs_type,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700308 int flags, const char *dev_name, void *data,
309 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
David Howells454e2392006-06-23 02:02:57 -0700311 return get_sb_pseudo(fs_type, "socket:", &sockfs_ops, SOCKFS_MAGIC,
312 mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
314
Eric Dumazetba899662005-08-26 12:05:31 -0700315static struct vfsmount *sock_mnt __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317static struct file_system_type sock_fs_type = {
318 .name = "sockfs",
319 .get_sb = sockfs_get_sb,
320 .kill_sb = kill_anon_super,
321};
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323static int sockfs_delete_dentry(struct dentry *dentry)
324{
Eric Dumazet304e61e2006-12-06 20:38:49 -0800325 /*
326 * At creation time, we pretended this dentry was hashed
327 * (by clearing DCACHE_UNHASHED bit in d_flags)
328 * At delete time, we restore the truth : not hashed.
329 * (so that dput() can proceed correctly)
330 */
331 dentry->d_flags |= DCACHE_UNHASHED;
332 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333}
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700334
335/*
336 * sockfs_dname() is called from d_path().
337 */
338static char *sockfs_dname(struct dentry *dentry, char *buffer, int buflen)
339{
340 return dynamic_dname(dentry, buffer, buflen, "socket:[%lu]",
341 dentry->d_inode->i_ino);
342}
343
Al Viro3ba13d12009-02-20 06:02:22 +0000344static const struct dentry_operations sockfs_dentry_operations = {
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700345 .d_delete = sockfs_delete_dentry,
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700346 .d_dname = sockfs_dname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347};
348
349/*
350 * Obtains the first available file descriptor and sets it up for use.
351 *
David S. Miller39d8c1b2006-03-20 17:13:49 -0800352 * These functions create file structures and maps them to fd space
353 * of the current process. On success it returns file descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 * and file struct implicitly stored in sock->file.
355 * Note that another thread may close file descriptor before we return
356 * from this function. We use the fact that now we do not refer
357 * to socket after mapping. If one day we will need it, this
358 * function will increment ref. count on file by 1.
359 *
360 * In any case returned fd MAY BE not valid!
361 * This race condition is unavoidable
362 * with shared fd spaces, we cannot solve it inside kernel,
363 * but we take care of internal coherence yet.
364 */
365
Ulrich Dreppera677a032008-07-23 21:29:17 -0700366static int sock_alloc_fd(struct file **filep, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
368 int fd;
David S. Miller39d8c1b2006-03-20 17:13:49 -0800369
Ulrich Dreppera677a032008-07-23 21:29:17 -0700370 fd = get_unused_fd_flags(flags);
David S. Miller39d8c1b2006-03-20 17:13:49 -0800371 if (likely(fd >= 0)) {
372 struct file *file = get_empty_filp();
373
374 *filep = file;
375 if (unlikely(!file)) {
376 put_unused_fd(fd);
377 return -ENFILE;
378 }
379 } else
380 *filep = NULL;
381 return fd;
382}
383
Ulrich Drepper77d27202008-07-23 21:29:35 -0700384static int sock_attach_fd(struct socket *sock, struct file *file, int flags)
David S. Miller39d8c1b2006-03-20 17:13:49 -0800385{
Dave Hansence8d2cd2007-10-16 23:31:13 -0700386 struct dentry *dentry;
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700387 struct qstr name = { .name = "" };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Dave Hansence8d2cd2007-10-16 23:31:13 -0700389 dentry = d_alloc(sock_mnt->mnt_sb->s_root, &name);
390 if (unlikely(!dentry))
David S. Miller39d8c1b2006-03-20 17:13:49 -0800391 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Dave Hansence8d2cd2007-10-16 23:31:13 -0700393 dentry->d_op = &sockfs_dentry_operations;
Eric Dumazet304e61e2006-12-06 20:38:49 -0800394 /*
395 * We dont want to push this dentry into global dentry hash table.
396 * We pretend dentry is already hashed, by unsetting DCACHE_UNHASHED
397 * This permits a working /proc/$pid/fd/XXX on sockets
398 */
Dave Hansence8d2cd2007-10-16 23:31:13 -0700399 dentry->d_flags &= ~DCACHE_UNHASHED;
400 d_instantiate(dentry, SOCK_INODE(sock));
David S. Miller39d8c1b2006-03-20 17:13:49 -0800401
402 sock->file = file;
Dave Hansence8d2cd2007-10-16 23:31:13 -0700403 init_file(file, sock_mnt, dentry, FMODE_READ | FMODE_WRITE,
404 &socket_file_ops);
405 SOCK_INODE(sock)->i_fop = &socket_file_ops;
Ulrich Drepper77d27202008-07-23 21:29:35 -0700406 file->f_flags = O_RDWR | (flags & O_NONBLOCK);
David S. Miller39d8c1b2006-03-20 17:13:49 -0800407 file->f_pos = 0;
408 file->private_data = sock;
409
410 return 0;
411}
412
Ulrich Dreppera677a032008-07-23 21:29:17 -0700413int sock_map_fd(struct socket *sock, int flags)
David S. Miller39d8c1b2006-03-20 17:13:49 -0800414{
415 struct file *newfile;
Ulrich Dreppera677a032008-07-23 21:29:17 -0700416 int fd = sock_alloc_fd(&newfile, flags);
David S. Miller39d8c1b2006-03-20 17:13:49 -0800417
418 if (likely(fd >= 0)) {
Ulrich Drepper77d27202008-07-23 21:29:35 -0700419 int err = sock_attach_fd(sock, newfile, flags);
David S. Miller39d8c1b2006-03-20 17:13:49 -0800420
421 if (unlikely(err < 0)) {
422 put_filp(newfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 put_unused_fd(fd);
David S. Miller39d8c1b2006-03-20 17:13:49 -0800424 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 }
David S. Miller39d8c1b2006-03-20 17:13:49 -0800426 fd_install(fd, newfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 return fd;
429}
430
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800431static struct socket *sock_from_file(struct file *file, int *err)
432{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800433 if (file->f_op == &socket_file_ops)
434 return file->private_data; /* set in sock_map_fd */
435
Eric Dumazet23bb80d2007-02-08 14:59:57 -0800436 *err = -ENOTSOCK;
437 return NULL;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800438}
439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440/**
441 * sockfd_lookup - Go from a file number to its socket slot
442 * @fd: file handle
443 * @err: pointer to an error code return
444 *
445 * The file handle passed in is locked and the socket it is bound
446 * too is returned. If an error occurs the err pointer is overwritten
447 * with a negative errno code and NULL is returned. The function checks
448 * for both invalid handles and passing a handle which is not a socket.
449 *
450 * On a success the socket object pointer is returned.
451 */
452
453struct socket *sockfd_lookup(int fd, int *err)
454{
455 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 struct socket *sock;
457
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700458 file = fget(fd);
459 if (!file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 *err = -EBADF;
461 return NULL;
462 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700463
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800464 sock = sock_from_file(file, err);
465 if (!sock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 fput(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 return sock;
468}
469
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800470static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed)
471{
472 struct file *file;
473 struct socket *sock;
474
Hua Zhong36725582006-04-19 15:25:02 -0700475 *err = -EBADF;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800476 file = fget_light(fd, fput_needed);
477 if (file) {
478 sock = sock_from_file(file, err);
479 if (sock)
480 return sock;
481 fput_light(file, *fput_needed);
482 }
483 return NULL;
484}
485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486/**
487 * sock_alloc - allocate a socket
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700488 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 * Allocate a new inode and socket object. The two are bound together
490 * and initialised. The socket is then returned. If we are out of inodes
491 * NULL is returned.
492 */
493
494static struct socket *sock_alloc(void)
495{
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700496 struct inode *inode;
497 struct socket *sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 inode = new_inode(sock_mnt->mnt_sb);
500 if (!inode)
501 return NULL;
502
503 sock = SOCKET_I(inode);
504
Eric Dumazet29a020d2009-09-15 02:39:20 -0700505 kmemcheck_annotate_bitfield(sock, type);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700506 inode->i_mode = S_IFSOCK | S_IRWXUGO;
David Howells8192b0c2008-11-14 10:39:10 +1100507 inode->i_uid = current_fsuid();
508 inode->i_gid = current_fsgid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Eric Dumazet4e694892009-04-04 16:41:09 -0700510 percpu_add(sockets_in_use, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 return sock;
512}
513
514/*
515 * In theory you can't get an open on this inode, but /proc provides
516 * a back door. Remember to keep it shut otherwise you'll let the
517 * creepy crawlies in.
518 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520static int sock_no_open(struct inode *irrelevant, struct file *dontcare)
521{
522 return -ENXIO;
523}
524
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800525const struct file_operations bad_sock_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 .owner = THIS_MODULE,
527 .open = sock_no_open,
528};
529
530/**
531 * sock_release - close a socket
532 * @sock: socket to close
533 *
534 * The socket is released from the protocol stack if it has a release
535 * callback, and the inode is then released if the socket is bound to
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700536 * an inode not a file.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539void sock_release(struct socket *sock)
540{
541 if (sock->ops) {
542 struct module *owner = sock->ops->owner;
543
544 sock->ops->release(sock);
545 sock->ops = NULL;
546 module_put(owner);
547 }
548
549 if (sock->fasync_list)
550 printk(KERN_ERR "sock_release: fasync list not empty!\n");
551
Eric Dumazet4e694892009-04-04 16:41:09 -0700552 percpu_sub(sockets_in_use, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 if (!sock->file) {
554 iput(SOCK_INODE(sock));
555 return;
556 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700557 sock->file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
559
Patrick Ohly20d49472009-02-12 05:03:38 +0000560int sock_tx_timestamp(struct msghdr *msg, struct sock *sk,
561 union skb_shared_tx *shtx)
562{
563 shtx->flags = 0;
564 if (sock_flag(sk, SOCK_TIMESTAMPING_TX_HARDWARE))
565 shtx->hardware = 1;
566 if (sock_flag(sk, SOCK_TIMESTAMPING_TX_SOFTWARE))
567 shtx->software = 1;
568 return 0;
569}
570EXPORT_SYMBOL(sock_tx_timestamp);
571
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700572static inline int __sock_sendmsg(struct kiocb *iocb, struct socket *sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 struct msghdr *msg, size_t size)
574{
575 struct sock_iocb *si = kiocb_to_siocb(iocb);
576 int err;
577
578 si->sock = sock;
579 si->scm = NULL;
580 si->msg = msg;
581 si->size = size;
582
583 err = security_socket_sendmsg(sock, msg, size);
584 if (err)
585 return err;
586
587 return sock->ops->sendmsg(iocb, sock, msg, size);
588}
589
590int sock_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
591{
592 struct kiocb iocb;
593 struct sock_iocb siocb;
594 int ret;
595
596 init_sync_kiocb(&iocb, NULL);
597 iocb.private = &siocb;
598 ret = __sock_sendmsg(&iocb, sock, msg, size);
599 if (-EIOCBQUEUED == ret)
600 ret = wait_on_sync_kiocb(&iocb);
601 return ret;
602}
603
604int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
605 struct kvec *vec, size_t num, size_t size)
606{
607 mm_segment_t oldfs = get_fs();
608 int result;
609
610 set_fs(KERNEL_DS);
611 /*
612 * the following is safe, since for compiler definitions of kvec and
613 * iovec are identical, yielding the same in-core layout and alignment
614 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700615 msg->msg_iov = (struct iovec *)vec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 msg->msg_iovlen = num;
617 result = sock_sendmsg(sock, msg, size);
618 set_fs(oldfs);
619 return result;
620}
621
Patrick Ohly20d49472009-02-12 05:03:38 +0000622static int ktime2ts(ktime_t kt, struct timespec *ts)
623{
624 if (kt.tv64) {
625 *ts = ktime_to_timespec(kt);
626 return 1;
627 } else {
628 return 0;
629 }
630}
631
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700632/*
633 * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
634 */
635void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
636 struct sk_buff *skb)
637{
Patrick Ohly20d49472009-02-12 05:03:38 +0000638 int need_software_tstamp = sock_flag(sk, SOCK_RCVTSTAMP);
639 struct timespec ts[3];
640 int empty = 1;
641 struct skb_shared_hwtstamps *shhwtstamps =
642 skb_hwtstamps(skb);
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700643
Patrick Ohly20d49472009-02-12 05:03:38 +0000644 /* Race occurred between timestamp enabling and packet
645 receiving. Fill in the current time for now. */
646 if (need_software_tstamp && skb->tstamp.tv64 == 0)
647 __net_timestamp(skb);
648
649 if (need_software_tstamp) {
650 if (!sock_flag(sk, SOCK_RCVTSTAMPNS)) {
651 struct timeval tv;
652 skb_get_timestamp(skb, &tv);
653 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMP,
654 sizeof(tv), &tv);
655 } else {
656 struct timespec ts;
657 skb_get_timestampns(skb, &ts);
658 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPNS,
659 sizeof(ts), &ts);
660 }
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700661 }
Patrick Ohly20d49472009-02-12 05:03:38 +0000662
663
664 memset(ts, 0, sizeof(ts));
665 if (skb->tstamp.tv64 &&
666 sock_flag(sk, SOCK_TIMESTAMPING_SOFTWARE)) {
667 skb_get_timestampns(skb, ts + 0);
668 empty = 0;
669 }
670 if (shhwtstamps) {
671 if (sock_flag(sk, SOCK_TIMESTAMPING_SYS_HARDWARE) &&
672 ktime2ts(shhwtstamps->syststamp, ts + 1))
673 empty = 0;
674 if (sock_flag(sk, SOCK_TIMESTAMPING_RAW_HARDWARE) &&
675 ktime2ts(shhwtstamps->hwtstamp, ts + 2))
676 empty = 0;
677 }
678 if (!empty)
679 put_cmsg(msg, SOL_SOCKET,
680 SCM_TIMESTAMPING, sizeof(ts), &ts);
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700681}
682
Arnaldo Carvalho de Melo7c81fd82007-03-10 00:39:35 -0300683EXPORT_SYMBOL_GPL(__sock_recv_timestamp);
684
Neil Horman3b885782009-10-12 13:26:31 -0700685inline void sock_recv_drops(struct msghdr *msg, struct sock *sk, struct sk_buff *skb)
686{
687 if (sock_flag(sk, SOCK_RXQ_OVFL) && skb && skb->dropcount)
688 put_cmsg(msg, SOL_SOCKET, SO_RXQ_OVFL,
689 sizeof(__u32), &skb->dropcount);
690}
691
692void sock_recv_ts_and_drops(struct msghdr *msg, struct sock *sk,
693 struct sk_buff *skb)
694{
695 sock_recv_timestamp(msg, sk, skb);
696 sock_recv_drops(msg, sk, skb);
697}
698EXPORT_SYMBOL_GPL(sock_recv_ts_and_drops);
699
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -0700700static inline int __sock_recvmsg_nosec(struct kiocb *iocb, struct socket *sock,
701 struct msghdr *msg, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 struct sock_iocb *si = kiocb_to_siocb(iocb);
704
705 si->sock = sock;
706 si->scm = NULL;
707 si->msg = msg;
708 si->size = size;
709 si->flags = flags;
710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 return sock->ops->recvmsg(iocb, sock, msg, size, flags);
712}
713
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -0700714static inline int __sock_recvmsg(struct kiocb *iocb, struct socket *sock,
715 struct msghdr *msg, size_t size, int flags)
716{
717 int err = security_socket_recvmsg(sock, msg, size, flags);
718
719 return err ?: __sock_recvmsg_nosec(iocb, sock, msg, size, flags);
720}
721
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700722int sock_recvmsg(struct socket *sock, struct msghdr *msg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 size_t size, int flags)
724{
725 struct kiocb iocb;
726 struct sock_iocb siocb;
727 int ret;
728
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700729 init_sync_kiocb(&iocb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 iocb.private = &siocb;
731 ret = __sock_recvmsg(&iocb, sock, msg, size, flags);
732 if (-EIOCBQUEUED == ret)
733 ret = wait_on_sync_kiocb(&iocb);
734 return ret;
735}
736
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -0700737static int sock_recvmsg_nosec(struct socket *sock, struct msghdr *msg,
738 size_t size, int flags)
739{
740 struct kiocb iocb;
741 struct sock_iocb siocb;
742 int ret;
743
744 init_sync_kiocb(&iocb, NULL);
745 iocb.private = &siocb;
746 ret = __sock_recvmsg_nosec(&iocb, sock, msg, size, flags);
747 if (-EIOCBQUEUED == ret)
748 ret = wait_on_sync_kiocb(&iocb);
749 return ret;
750}
751
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700752int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
753 struct kvec *vec, size_t num, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754{
755 mm_segment_t oldfs = get_fs();
756 int result;
757
758 set_fs(KERNEL_DS);
759 /*
760 * the following is safe, since for compiler definitions of kvec and
761 * iovec are identical, yielding the same in-core layout and alignment
762 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700763 msg->msg_iov = (struct iovec *)vec, msg->msg_iovlen = num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 result = sock_recvmsg(sock, msg, size, flags);
765 set_fs(oldfs);
766 return result;
767}
768
769static void sock_aio_dtor(struct kiocb *iocb)
770{
771 kfree(iocb->private);
772}
773
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -0300774static ssize_t sock_sendpage(struct file *file, struct page *page,
775 int offset, size_t size, loff_t *ppos, int more)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
777 struct socket *sock;
778 int flags;
779
Eric Dumazetb69aee02005-09-06 14:42:45 -0700780 sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782 flags = !(file->f_flags & O_NONBLOCK) ? 0 : MSG_DONTWAIT;
783 if (more)
784 flags |= MSG_MORE;
785
Linus Torvaldse6949582009-08-13 08:28:36 -0700786 return kernel_sendpage(sock, page, offset, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787}
788
Jens Axboe9c55e012007-11-06 23:30:13 -0800789static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
790 struct pipe_inode_info *pipe, size_t len,
791 unsigned int flags)
792{
793 struct socket *sock = file->private_data;
794
Rémi Denis-Courmont997b37d2008-02-15 02:35:45 -0800795 if (unlikely(!sock->ops->splice_read))
796 return -EINVAL;
797
Jens Axboe9c55e012007-11-06 23:30:13 -0800798 return sock->ops->splice_read(sock, ppos, pipe, len, flags);
799}
800
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800801static struct sock_iocb *alloc_sock_iocb(struct kiocb *iocb,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700802 struct sock_iocb *siocb)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800803{
804 if (!is_sync_kiocb(iocb)) {
805 siocb = kmalloc(sizeof(*siocb), GFP_KERNEL);
806 if (!siocb)
807 return NULL;
808 iocb->ki_dtor = sock_aio_dtor;
809 }
810
811 siocb->kiocb = iocb;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800812 iocb->private = siocb;
813 return siocb;
814}
815
816static ssize_t do_sock_read(struct msghdr *msg, struct kiocb *iocb,
Badari Pulavarty027445c2006-09-30 23:28:46 -0700817 struct file *file, const struct iovec *iov,
818 unsigned long nr_segs)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800819{
820 struct socket *sock = file->private_data;
821 size_t size = 0;
822 int i;
823
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700824 for (i = 0; i < nr_segs; i++)
825 size += iov[i].iov_len;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800826
827 msg->msg_name = NULL;
828 msg->msg_namelen = 0;
829 msg->msg_control = NULL;
830 msg->msg_controllen = 0;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700831 msg->msg_iov = (struct iovec *)iov;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800832 msg->msg_iovlen = nr_segs;
833 msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
834
835 return __sock_recvmsg(iocb, sock, msg, size, msg->msg_flags);
836}
837
Badari Pulavarty027445c2006-09-30 23:28:46 -0700838static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov,
839 unsigned long nr_segs, loff_t pos)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800840{
841 struct sock_iocb siocb, *x;
842
843 if (pos != 0)
844 return -ESPIPE;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700845
846 if (iocb->ki_left == 0) /* Match SYS5 behaviour */
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800847 return 0;
848
Badari Pulavarty027445c2006-09-30 23:28:46 -0700849
850 x = alloc_sock_iocb(iocb, &siocb);
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800851 if (!x)
852 return -ENOMEM;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700853 return do_sock_read(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800854}
855
856static ssize_t do_sock_write(struct msghdr *msg, struct kiocb *iocb,
Badari Pulavarty027445c2006-09-30 23:28:46 -0700857 struct file *file, const struct iovec *iov,
858 unsigned long nr_segs)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800859{
860 struct socket *sock = file->private_data;
861 size_t size = 0;
862 int i;
863
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700864 for (i = 0; i < nr_segs; i++)
865 size += iov[i].iov_len;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800866
867 msg->msg_name = NULL;
868 msg->msg_namelen = 0;
869 msg->msg_control = NULL;
870 msg->msg_controllen = 0;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700871 msg->msg_iov = (struct iovec *)iov;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800872 msg->msg_iovlen = nr_segs;
873 msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
874 if (sock->type == SOCK_SEQPACKET)
875 msg->msg_flags |= MSG_EOR;
876
877 return __sock_sendmsg(iocb, sock, msg, size);
878}
879
Badari Pulavarty027445c2006-09-30 23:28:46 -0700880static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov,
881 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800883 struct sock_iocb siocb, *x;
884
885 if (pos != 0)
886 return -ESPIPE;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700887
Badari Pulavarty027445c2006-09-30 23:28:46 -0700888 x = alloc_sock_iocb(iocb, &siocb);
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800889 if (!x)
890 return -ENOMEM;
891
Badari Pulavarty027445c2006-09-30 23:28:46 -0700892 return do_sock_write(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893}
894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895/*
896 * Atomic setting of ioctl hooks to avoid race
897 * with module unload.
898 */
899
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800900static DEFINE_MUTEX(br_ioctl_mutex);
Eric W. Biederman881d9662007-09-17 11:56:21 -0700901static int (*br_ioctl_hook) (struct net *, unsigned int cmd, void __user *arg) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Eric W. Biederman881d9662007-09-17 11:56:21 -0700903void brioctl_set(int (*hook) (struct net *, unsigned int, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800905 mutex_lock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 br_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800907 mutex_unlock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908}
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700909
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910EXPORT_SYMBOL(brioctl_set);
911
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800912static DEFINE_MUTEX(vlan_ioctl_mutex);
Eric W. Biederman881d9662007-09-17 11:56:21 -0700913static int (*vlan_ioctl_hook) (struct net *, void __user *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Eric W. Biederman881d9662007-09-17 11:56:21 -0700915void vlan_ioctl_set(int (*hook) (struct net *, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800917 mutex_lock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 vlan_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800919 mutex_unlock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920}
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700921
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922EXPORT_SYMBOL(vlan_ioctl_set);
923
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800924static DEFINE_MUTEX(dlci_ioctl_mutex);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700925static int (*dlci_ioctl_hook) (unsigned int, void __user *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700927void dlci_ioctl_set(int (*hook) (unsigned int, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800929 mutex_lock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 dlci_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800931 mutex_unlock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932}
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934EXPORT_SYMBOL(dlci_ioctl_set);
935
Arnd Bergmann6b960182009-11-06 23:10:54 -0800936static long sock_do_ioctl(struct net *net, struct socket *sock,
937 unsigned int cmd, unsigned long arg)
938{
939 int err;
940 void __user *argp = (void __user *)arg;
941
942 err = sock->ops->ioctl(sock, cmd, arg);
943
944 /*
945 * If this ioctl is unknown try to hand it down
946 * to the NIC driver.
947 */
948 if (err == -ENOIOCTLCMD)
949 err = dev_ioctl(net, cmd, argp);
950
951 return err;
952}
953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954/*
955 * With an ioctl, arg may well be a user mode pointer, but we don't know
956 * what to do with it - that's up to the protocol still.
957 */
958
959static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
960{
961 struct socket *sock;
Eric W. Biederman881d9662007-09-17 11:56:21 -0700962 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 void __user *argp = (void __user *)arg;
964 int pid, err;
Eric W. Biederman881d9662007-09-17 11:56:21 -0700965 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
Eric Dumazetb69aee02005-09-06 14:42:45 -0700967 sock = file->private_data;
Eric W. Biederman881d9662007-09-17 11:56:21 -0700968 sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900969 net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
Eric W. Biederman881d9662007-09-17 11:56:21 -0700971 err = dev_ioctl(net, cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 } else
Johannes Berg3d23e342009-09-29 23:27:28 +0200973#ifdef CONFIG_WEXT_CORE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
Eric W. Biederman881d9662007-09-17 11:56:21 -0700975 err = dev_ioctl(net, cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 } else
Johannes Berg3d23e342009-09-29 23:27:28 +0200977#endif
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700978 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 case FIOSETOWN:
980 case SIOCSPGRP:
981 err = -EFAULT;
982 if (get_user(pid, (int __user *)argp))
983 break;
984 err = f_setown(sock->file, pid, 1);
985 break;
986 case FIOGETOWN:
987 case SIOCGPGRP:
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700988 err = put_user(f_getown(sock->file),
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700989 (int __user *)argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 break;
991 case SIOCGIFBR:
992 case SIOCSIFBR:
993 case SIOCBRADDBR:
994 case SIOCBRDELBR:
995 err = -ENOPKG;
996 if (!br_ioctl_hook)
997 request_module("bridge");
998
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800999 mutex_lock(&br_ioctl_mutex);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001000 if (br_ioctl_hook)
Eric W. Biederman881d9662007-09-17 11:56:21 -07001001 err = br_ioctl_hook(net, cmd, argp);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001002 mutex_unlock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 break;
1004 case SIOCGIFVLAN:
1005 case SIOCSIFVLAN:
1006 err = -ENOPKG;
1007 if (!vlan_ioctl_hook)
1008 request_module("8021q");
1009
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001010 mutex_lock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 if (vlan_ioctl_hook)
Eric W. Biederman881d9662007-09-17 11:56:21 -07001012 err = vlan_ioctl_hook(net, argp);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001013 mutex_unlock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 case SIOCADDDLCI:
1016 case SIOCDELDLCI:
1017 err = -ENOPKG;
1018 if (!dlci_ioctl_hook)
1019 request_module("dlci");
1020
Pavel Emelyanov7512cbf2008-03-21 15:58:52 -07001021 mutex_lock(&dlci_ioctl_mutex);
1022 if (dlci_ioctl_hook)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 err = dlci_ioctl_hook(cmd, argp);
Pavel Emelyanov7512cbf2008-03-21 15:58:52 -07001024 mutex_unlock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 break;
1026 default:
Arnd Bergmann6b960182009-11-06 23:10:54 -08001027 err = sock_do_ioctl(net, sock, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 break;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001029 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 return err;
1031}
1032
1033int sock_create_lite(int family, int type, int protocol, struct socket **res)
1034{
1035 int err;
1036 struct socket *sock = NULL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 err = security_socket_create(family, type, protocol, 1);
1039 if (err)
1040 goto out;
1041
1042 sock = sock_alloc();
1043 if (!sock) {
1044 err = -ENOMEM;
1045 goto out;
1046 }
1047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 sock->type = type;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001049 err = security_socket_post_create(sock, family, type, protocol, 1);
1050 if (err)
1051 goto out_release;
1052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053out:
1054 *res = sock;
1055 return err;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001056out_release:
1057 sock_release(sock);
1058 sock = NULL;
1059 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060}
1061
1062/* No kernel lock held - perfect */
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001063static unsigned int sock_poll(struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
1065 struct socket *sock;
1066
1067 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001068 * We can't return errors to poll, so it's either yes or no.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 */
Eric Dumazetb69aee02005-09-06 14:42:45 -07001070 sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 return sock->ops->poll(file, sock, wait);
1072}
1073
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001074static int sock_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075{
Eric Dumazetb69aee02005-09-06 14:42:45 -07001076 struct socket *sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
1078 return sock->ops->mmap(file, sock, vma);
1079}
1080
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001081static int sock_close(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082{
1083 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001084 * It was possible the inode is NULL we were
1085 * closing an unfinished socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 */
1087
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001088 if (!inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 printk(KERN_DEBUG "sock_close: NULL inode\n");
1090 return 0;
1091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 sock_release(SOCKET_I(inode));
1093 return 0;
1094}
1095
1096/*
1097 * Update the socket async list
1098 *
1099 * Fasync_list locking strategy.
1100 *
1101 * 1. fasync_list is modified only under process context socket lock
1102 * i.e. under semaphore.
1103 * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
1104 * or under socket lock.
1105 * 3. fasync_list can be used from softirq context, so that
1106 * modification under socket lock have to be enhanced with
1107 * write_lock_bh(&sk->sk_callback_lock).
1108 * --ANK (990710)
1109 */
1110
1111static int sock_fasync(int fd, struct file *filp, int on)
1112{
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001113 struct fasync_struct *fa, *fna = NULL, **prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 struct socket *sock;
1115 struct sock *sk;
1116
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001117 if (on) {
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08001118 fna = kmalloc(sizeof(struct fasync_struct), GFP_KERNEL);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001119 if (fna == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 return -ENOMEM;
1121 }
1122
Eric Dumazetb69aee02005-09-06 14:42:45 -07001123 sock = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001125 sk = sock->sk;
1126 if (sk == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 kfree(fna);
1128 return -EINVAL;
1129 }
1130
1131 lock_sock(sk);
1132
Jonathan Corbet76398422009-02-01 14:26:59 -07001133 spin_lock(&filp->f_lock);
1134 if (on)
1135 filp->f_flags |= FASYNC;
1136 else
1137 filp->f_flags &= ~FASYNC;
1138 spin_unlock(&filp->f_lock);
1139
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001140 prev = &(sock->fasync_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001142 for (fa = *prev; fa != NULL; prev = &fa->fa_next, fa = *prev)
1143 if (fa->fa_file == filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 break;
1145
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001146 if (on) {
1147 if (fa != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 write_lock_bh(&sk->sk_callback_lock);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001149 fa->fa_fd = fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 write_unlock_bh(&sk->sk_callback_lock);
1151
1152 kfree(fna);
1153 goto out;
1154 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001155 fna->fa_file = filp;
1156 fna->fa_fd = fd;
1157 fna->magic = FASYNC_MAGIC;
1158 fna->fa_next = sock->fasync_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 write_lock_bh(&sk->sk_callback_lock);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001160 sock->fasync_list = fna;
Eric Dumazetbcdce712009-10-06 17:28:29 -07001161 sock_set_flag(sk, SOCK_FASYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 write_unlock_bh(&sk->sk_callback_lock);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001163 } else {
1164 if (fa != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 write_lock_bh(&sk->sk_callback_lock);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001166 *prev = fa->fa_next;
Eric Dumazetbcdce712009-10-06 17:28:29 -07001167 if (!sock->fasync_list)
1168 sock_reset_flag(sk, SOCK_FASYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 write_unlock_bh(&sk->sk_callback_lock);
1170 kfree(fa);
1171 }
1172 }
1173
1174out:
1175 release_sock(sock->sk);
1176 return 0;
1177}
1178
1179/* This function may be called only under socket lock or callback_lock */
1180
1181int sock_wake_async(struct socket *sock, int how, int band)
1182{
1183 if (!sock || !sock->fasync_list)
1184 return -1;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001185 switch (how) {
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001186 case SOCK_WAKE_WAITD:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 if (test_bit(SOCK_ASYNC_WAITDATA, &sock->flags))
1188 break;
1189 goto call_kill;
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001190 case SOCK_WAKE_SPACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags))
1192 break;
1193 /* fall through */
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001194 case SOCK_WAKE_IO:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001195call_kill:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 __kill_fasync(sock->fasync_list, SIGIO, band);
1197 break;
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001198 case SOCK_WAKE_URG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 __kill_fasync(sock->fasync_list, SIGURG, band);
1200 }
1201 return 0;
1202}
1203
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001204static int __sock_create(struct net *net, int family, int type, int protocol,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001205 struct socket **res, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
1207 int err;
1208 struct socket *sock;
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001209 const struct net_proto_family *pf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
1211 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001212 * Check protocol is in range
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 */
1214 if (family < 0 || family >= NPROTO)
1215 return -EAFNOSUPPORT;
1216 if (type < 0 || type >= SOCK_MAX)
1217 return -EINVAL;
1218
1219 /* Compatibility.
1220
1221 This uglymoron is moved from INET layer to here to avoid
1222 deadlock in module load.
1223 */
1224 if (family == PF_INET && type == SOCK_PACKET) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001225 static int warned;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 if (!warned) {
1227 warned = 1;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001228 printk(KERN_INFO "%s uses obsolete (PF_INET,SOCK_PACKET)\n",
1229 current->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 }
1231 family = PF_PACKET;
1232 }
1233
1234 err = security_socket_create(family, type, protocol, kern);
1235 if (err)
1236 return err;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001237
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001238 /*
1239 * Allocate the socket and allow the family to set things up. if
1240 * the protocol is 0, the family is instructed to select an appropriate
1241 * default.
1242 */
1243 sock = sock_alloc();
1244 if (!sock) {
1245 if (net_ratelimit())
1246 printk(KERN_WARNING "socket: no more sockets\n");
1247 return -ENFILE; /* Not exactly a match, but its the
1248 closest posix thing */
1249 }
1250
1251 sock->type = type;
1252
Johannes Berg95a5afc2008-10-16 15:24:51 -07001253#ifdef CONFIG_MODULES
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001254 /* Attempt to load a protocol module if the find failed.
1255 *
1256 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 * requested real, full-featured networking support upon configuration.
1258 * Otherwise module support will break!
1259 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001260 if (net_families[family] == NULL)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001261 request_module("net-pf-%d", family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262#endif
1263
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001264 rcu_read_lock();
1265 pf = rcu_dereference(net_families[family]);
1266 err = -EAFNOSUPPORT;
1267 if (!pf)
1268 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
1270 /*
1271 * We will call the ->create function, that possibly is in a loadable
1272 * module, so we have to bump that loadable module refcnt first.
1273 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001274 if (!try_module_get(pf->owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 goto out_release;
1276
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001277 /* Now protected by module ref count */
1278 rcu_read_unlock();
1279
Eric Paris3f378b62009-11-05 22:18:14 -08001280 err = pf->create(net, sock, protocol, kern);
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001281 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 goto out_module_put;
Frank Filza79af592005-09-27 15:23:38 -07001283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 /*
1285 * Now to bump the refcnt of the [loadable] module that owns this
1286 * socket at sock_release time we decrement its refcnt.
1287 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001288 if (!try_module_get(sock->ops->owner))
1289 goto out_module_busy;
1290
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 /*
1292 * Now that we're done with the ->create function, the [loadable]
1293 * module can have its refcnt decremented
1294 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001295 module_put(pf->owner);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001296 err = security_socket_post_create(sock, family, type, protocol, kern);
1297 if (err)
Herbert Xu3b185522007-08-15 14:46:02 -07001298 goto out_sock_release;
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001299 *res = sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001301 return 0;
1302
1303out_module_busy:
1304 err = -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305out_module_put:
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001306 sock->ops = NULL;
1307 module_put(pf->owner);
1308out_sock_release:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 sock_release(sock);
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001310 return err;
1311
1312out_release:
1313 rcu_read_unlock();
1314 goto out_sock_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315}
1316
1317int sock_create(int family, int type, int protocol, struct socket **res)
1318{
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001319 return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320}
1321
1322int sock_create_kern(int family, int type, int protocol, struct socket **res)
1323{
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001324 return __sock_create(&init_net, family, type, protocol, res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325}
1326
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001327SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328{
1329 int retval;
1330 struct socket *sock;
Ulrich Dreppera677a032008-07-23 21:29:17 -07001331 int flags;
1332
Ulrich Dreppere38b36f2008-07-23 21:29:42 -07001333 /* Check the SOCK_* constants for consistency. */
1334 BUILD_BUG_ON(SOCK_CLOEXEC != O_CLOEXEC);
1335 BUILD_BUG_ON((SOCK_MAX | SOCK_TYPE_MASK) != SOCK_TYPE_MASK);
1336 BUILD_BUG_ON(SOCK_CLOEXEC & SOCK_TYPE_MASK);
1337 BUILD_BUG_ON(SOCK_NONBLOCK & SOCK_TYPE_MASK);
1338
Ulrich Dreppera677a032008-07-23 21:29:17 -07001339 flags = type & ~SOCK_TYPE_MASK;
Ulrich Drepper77d27202008-07-23 21:29:35 -07001340 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Dreppera677a032008-07-23 21:29:17 -07001341 return -EINVAL;
1342 type &= SOCK_TYPE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001344 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1345 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1346
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 retval = sock_create(family, type, protocol, &sock);
1348 if (retval < 0)
1349 goto out;
1350
Ulrich Drepper77d27202008-07-23 21:29:35 -07001351 retval = sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 if (retval < 0)
1353 goto out_release;
1354
1355out:
1356 /* It may be already another descriptor 8) Not kernel problem. */
1357 return retval;
1358
1359out_release:
1360 sock_release(sock);
1361 return retval;
1362}
1363
1364/*
1365 * Create a pair of connected sockets.
1366 */
1367
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001368SYSCALL_DEFINE4(socketpair, int, family, int, type, int, protocol,
1369 int __user *, usockvec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370{
1371 struct socket *sock1, *sock2;
1372 int fd1, fd2, err;
Al Virodb349502007-02-07 01:48:00 -05001373 struct file *newfile1, *newfile2;
Ulrich Dreppera677a032008-07-23 21:29:17 -07001374 int flags;
1375
1376 flags = type & ~SOCK_TYPE_MASK;
Ulrich Drepper77d27202008-07-23 21:29:35 -07001377 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Dreppera677a032008-07-23 21:29:17 -07001378 return -EINVAL;
1379 type &= SOCK_TYPE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001381 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1382 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1383
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 /*
1385 * Obtain the first socket and check if the underlying protocol
1386 * supports the socketpair call.
1387 */
1388
1389 err = sock_create(family, type, protocol, &sock1);
1390 if (err < 0)
1391 goto out;
1392
1393 err = sock_create(family, type, protocol, &sock2);
1394 if (err < 0)
1395 goto out_release_1;
1396
1397 err = sock1->ops->socketpair(sock1, sock2);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001398 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 goto out_release_both;
1400
Ulrich Dreppera677a032008-07-23 21:29:17 -07001401 fd1 = sock_alloc_fd(&newfile1, flags & O_CLOEXEC);
David S. Millerbf3c23d2007-10-29 21:54:02 -07001402 if (unlikely(fd1 < 0)) {
1403 err = fd1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 goto out_release_both;
David S. Millerbf3c23d2007-10-29 21:54:02 -07001405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Ulrich Dreppera677a032008-07-23 21:29:17 -07001407 fd2 = sock_alloc_fd(&newfile2, flags & O_CLOEXEC);
Al Virodb349502007-02-07 01:48:00 -05001408 if (unlikely(fd2 < 0)) {
David S. Millerbf3c23d2007-10-29 21:54:02 -07001409 err = fd2;
Al Virodb349502007-02-07 01:48:00 -05001410 put_filp(newfile1);
1411 put_unused_fd(fd1);
1412 goto out_release_both;
1413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
Ulrich Drepper77d27202008-07-23 21:29:35 -07001415 err = sock_attach_fd(sock1, newfile1, flags & O_NONBLOCK);
Al Virodb349502007-02-07 01:48:00 -05001416 if (unlikely(err < 0)) {
1417 goto out_fd2;
1418 }
1419
Ulrich Drepper77d27202008-07-23 21:29:35 -07001420 err = sock_attach_fd(sock2, newfile2, flags & O_NONBLOCK);
Al Virodb349502007-02-07 01:48:00 -05001421 if (unlikely(err < 0)) {
1422 fput(newfile1);
1423 goto out_fd1;
1424 }
1425
Al Viro157cf642008-12-14 04:57:47 -05001426 audit_fd_pair(fd1, fd2);
Al Virodb349502007-02-07 01:48:00 -05001427 fd_install(fd1, newfile1);
1428 fd_install(fd2, newfile2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 /* fd1 and fd2 may be already another descriptors.
1430 * Not kernel problem.
1431 */
1432
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001433 err = put_user(fd1, &usockvec[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 if (!err)
1435 err = put_user(fd2, &usockvec[1]);
1436 if (!err)
1437 return 0;
1438
1439 sys_close(fd2);
1440 sys_close(fd1);
1441 return err;
1442
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443out_release_both:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001444 sock_release(sock2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445out_release_1:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001446 sock_release(sock1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447out:
1448 return err;
Al Virodb349502007-02-07 01:48:00 -05001449
1450out_fd2:
1451 put_filp(newfile1);
1452 sock_release(sock1);
1453out_fd1:
1454 put_filp(newfile2);
1455 sock_release(sock2);
Al Virodb349502007-02-07 01:48:00 -05001456 put_unused_fd(fd1);
1457 put_unused_fd(fd2);
1458 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459}
1460
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461/*
1462 * Bind a name to a socket. Nothing much to do here since it's
1463 * the protocol's responsibility to handle the local address.
1464 *
1465 * We move the socket address to kernel space before we call
1466 * the protocol layer (having also checked the address is ok).
1467 */
1468
Heiko Carstens20f37032009-01-14 14:14:23 +01001469SYSCALL_DEFINE3(bind, int, fd, struct sockaddr __user *, umyaddr, int, addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470{
1471 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001472 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001473 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001475 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001476 if (sock) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001477 err = move_addr_to_kernel(umyaddr, addrlen, (struct sockaddr *)&address);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001478 if (err >= 0) {
1479 err = security_socket_bind(sock,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001480 (struct sockaddr *)&address,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001481 addrlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001482 if (!err)
1483 err = sock->ops->bind(sock,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001484 (struct sockaddr *)
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001485 &address, addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 }
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001487 fput_light(sock->file, fput_needed);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 return err;
1490}
1491
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492/*
1493 * Perform a listen. Basically, we allow the protocol to do anything
1494 * necessary for a listen, and if that works, we mark the socket as
1495 * ready for listening.
1496 */
1497
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001498SYSCALL_DEFINE2(listen, int, fd, int, backlog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499{
1500 struct socket *sock;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001501 int err, fput_needed;
Pavel Emelyanovb8e1f9b2007-12-08 00:12:33 -08001502 int somaxconn;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001503
1504 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1505 if (sock) {
Pavel Emelyanov8efa6e92008-03-31 19:41:14 -07001506 somaxconn = sock_net(sock->sk)->core.sysctl_somaxconn;
Pavel Emelyanovb8e1f9b2007-12-08 00:12:33 -08001507 if ((unsigned)backlog > somaxconn)
1508 backlog = somaxconn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
1510 err = security_socket_listen(sock, backlog);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001511 if (!err)
1512 err = sock->ops->listen(sock, backlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001514 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 }
1516 return err;
1517}
1518
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519/*
1520 * For accept, we attempt to create a new socket, set up the link
1521 * with the client, wake up the client, then return the new
1522 * connected fd. We collect the address of the connector in kernel
1523 * space and move it to user at the very end. This is unclean because
1524 * we open the socket then return an error.
1525 *
1526 * 1003.1g adds the ability to recvmsg() to query connection pending
1527 * status to recvmsg. We need to add that support in a way thats
1528 * clean when we restucture accept also.
1529 */
1530
Heiko Carstens20f37032009-01-14 14:14:23 +01001531SYSCALL_DEFINE4(accept4, int, fd, struct sockaddr __user *, upeer_sockaddr,
1532 int __user *, upeer_addrlen, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533{
1534 struct socket *sock, *newsock;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001535 struct file *newfile;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001536 int err, len, newfd, fput_needed;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001537 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
Ulrich Drepper77d27202008-07-23 21:29:35 -07001539 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001540 return -EINVAL;
1541
1542 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1543 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1544
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001545 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 if (!sock)
1547 goto out;
1548
1549 err = -ENFILE;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001550 if (!(newsock = sock_alloc()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 goto out_put;
1552
1553 newsock->type = sock->type;
1554 newsock->ops = sock->ops;
1555
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 /*
1557 * We don't need try_module_get here, as the listening socket (sock)
1558 * has the protocol module (sock->ops->owner) held.
1559 */
1560 __module_get(newsock->ops->owner);
1561
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001562 newfd = sock_alloc_fd(&newfile, flags & O_CLOEXEC);
David S. Miller39d8c1b2006-03-20 17:13:49 -08001563 if (unlikely(newfd < 0)) {
1564 err = newfd;
David S. Miller9a1875e2006-04-01 12:48:36 -08001565 sock_release(newsock);
1566 goto out_put;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001567 }
1568
Ulrich Drepper77d27202008-07-23 21:29:35 -07001569 err = sock_attach_fd(newsock, newfile, flags & O_NONBLOCK);
David S. Miller39d8c1b2006-03-20 17:13:49 -08001570 if (err < 0)
Alexey Dobriyan79f4f642007-03-26 14:09:52 -07001571 goto out_fd_simple;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001572
Frank Filza79af592005-09-27 15:23:38 -07001573 err = security_socket_accept(sock, newsock);
1574 if (err)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001575 goto out_fd;
Frank Filza79af592005-09-27 15:23:38 -07001576
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 err = sock->ops->accept(sock, newsock, sock->file->f_flags);
1578 if (err < 0)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001579 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
1581 if (upeer_sockaddr) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001582 if (newsock->ops->getname(newsock, (struct sockaddr *)&address,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001583 &len, 2) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 err = -ECONNABORTED;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001585 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 }
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001587 err = move_addr_to_user((struct sockaddr *)&address,
1588 len, upeer_sockaddr, upeer_addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 if (err < 0)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001590 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 }
1592
1593 /* File flags are not inherited via accept() unlike another OSes. */
1594
David S. Miller39d8c1b2006-03-20 17:13:49 -08001595 fd_install(newfd, newfile);
1596 err = newfd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001599 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600out:
1601 return err;
Alexey Dobriyan79f4f642007-03-26 14:09:52 -07001602out_fd_simple:
1603 sock_release(newsock);
1604 put_filp(newfile);
1605 put_unused_fd(newfd);
1606 goto out_put;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001607out_fd:
David S. Miller9606a212006-04-01 01:00:14 -08001608 fput(newfile);
David S. Miller39d8c1b2006-03-20 17:13:49 -08001609 put_unused_fd(newfd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 goto out_put;
1611}
1612
Heiko Carstens20f37032009-01-14 14:14:23 +01001613SYSCALL_DEFINE3(accept, int, fd, struct sockaddr __user *, upeer_sockaddr,
1614 int __user *, upeer_addrlen)
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001615{
Ulrich Drepperde11def2008-11-19 15:36:14 -08001616 return sys_accept4(fd, upeer_sockaddr, upeer_addrlen, 0);
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001617}
1618
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619/*
1620 * Attempt to connect to a socket with the server address. The address
1621 * is in user space so we verify it is OK and move it to kernel space.
1622 *
1623 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
1624 * break bindings
1625 *
1626 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
1627 * other SEQPACKET protocols that take time to connect() as it doesn't
1628 * include the -EINPROGRESS status for such sockets.
1629 */
1630
Heiko Carstens20f37032009-01-14 14:14:23 +01001631SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
1632 int, addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633{
1634 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001635 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001636 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001638 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 if (!sock)
1640 goto out;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001641 err = move_addr_to_kernel(uservaddr, addrlen, (struct sockaddr *)&address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 if (err < 0)
1643 goto out_put;
1644
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001645 err =
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001646 security_socket_connect(sock, (struct sockaddr *)&address, addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 if (err)
1648 goto out_put;
1649
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001650 err = sock->ops->connect(sock, (struct sockaddr *)&address, addrlen,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 sock->file->f_flags);
1652out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001653 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654out:
1655 return err;
1656}
1657
1658/*
1659 * Get the local address ('name') of a socket object. Move the obtained
1660 * name to user space.
1661 */
1662
Heiko Carstens20f37032009-01-14 14:14:23 +01001663SYSCALL_DEFINE3(getsockname, int, fd, struct sockaddr __user *, usockaddr,
1664 int __user *, usockaddr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665{
1666 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001667 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001668 int len, err, fput_needed;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001669
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001670 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 if (!sock)
1672 goto out;
1673
1674 err = security_socket_getsockname(sock);
1675 if (err)
1676 goto out_put;
1677
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001678 err = sock->ops->getname(sock, (struct sockaddr *)&address, &len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 if (err)
1680 goto out_put;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001681 err = move_addr_to_user((struct sockaddr *)&address, len, usockaddr, usockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
1683out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001684 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685out:
1686 return err;
1687}
1688
1689/*
1690 * Get the remote address ('name') of a socket object. Move the obtained
1691 * name to user space.
1692 */
1693
Heiko Carstens20f37032009-01-14 14:14:23 +01001694SYSCALL_DEFINE3(getpeername, int, fd, struct sockaddr __user *, usockaddr,
1695 int __user *, usockaddr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696{
1697 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001698 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001699 int len, err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001701 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1702 if (sock != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 err = security_socket_getpeername(sock);
1704 if (err) {
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001705 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 return err;
1707 }
1708
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001709 err =
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001710 sock->ops->getname(sock, (struct sockaddr *)&address, &len,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001711 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 if (!err)
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001713 err = move_addr_to_user((struct sockaddr *)&address, len, usockaddr,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001714 usockaddr_len);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001715 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 }
1717 return err;
1718}
1719
1720/*
1721 * Send a datagram to a given address. We move the address into kernel
1722 * space and check the user space data area is readable before invoking
1723 * the protocol.
1724 */
1725
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001726SYSCALL_DEFINE6(sendto, int, fd, void __user *, buff, size_t, len,
1727 unsigned, flags, struct sockaddr __user *, addr,
1728 int, addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729{
1730 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001731 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 int err;
1733 struct msghdr msg;
1734 struct iovec iov;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001735 int fput_needed;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001736
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001737 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1738 if (!sock)
David S. Miller4387ff72007-02-08 15:06:08 -08001739 goto out;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001740
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001741 iov.iov_base = buff;
1742 iov.iov_len = len;
1743 msg.msg_name = NULL;
1744 msg.msg_iov = &iov;
1745 msg.msg_iovlen = 1;
1746 msg.msg_control = NULL;
1747 msg.msg_controllen = 0;
1748 msg.msg_namelen = 0;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001749 if (addr) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001750 err = move_addr_to_kernel(addr, addr_len, (struct sockaddr *)&address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 if (err < 0)
1752 goto out_put;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001753 msg.msg_name = (struct sockaddr *)&address;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001754 msg.msg_namelen = addr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 }
1756 if (sock->file->f_flags & O_NONBLOCK)
1757 flags |= MSG_DONTWAIT;
1758 msg.msg_flags = flags;
1759 err = sock_sendmsg(sock, &msg, len);
1760
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001761out_put:
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001762 fput_light(sock->file, fput_needed);
David S. Miller4387ff72007-02-08 15:06:08 -08001763out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 return err;
1765}
1766
1767/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001768 * Send a datagram down a socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 */
1770
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001771SYSCALL_DEFINE4(send, int, fd, void __user *, buff, size_t, len,
1772 unsigned, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773{
1774 return sys_sendto(fd, buff, len, flags, NULL, 0);
1775}
1776
1777/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001778 * Receive a frame from the socket and optionally record the address of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 * sender. We verify the buffers are writable and if needed move the
1780 * sender address from kernel to user space.
1781 */
1782
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001783SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, ubuf, size_t, size,
1784 unsigned, flags, struct sockaddr __user *, addr,
1785 int __user *, addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786{
1787 struct socket *sock;
1788 struct iovec iov;
1789 struct msghdr msg;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001790 struct sockaddr_storage address;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001791 int err, err2;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001792 int fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001794 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 if (!sock)
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001796 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001798 msg.msg_control = NULL;
1799 msg.msg_controllen = 0;
1800 msg.msg_iovlen = 1;
1801 msg.msg_iov = &iov;
1802 iov.iov_len = size;
1803 iov.iov_base = ubuf;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001804 msg.msg_name = (struct sockaddr *)&address;
1805 msg.msg_namelen = sizeof(address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 if (sock->file->f_flags & O_NONBLOCK)
1807 flags |= MSG_DONTWAIT;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001808 err = sock_recvmsg(sock, &msg, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001810 if (err >= 0 && addr != NULL) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001811 err2 = move_addr_to_user((struct sockaddr *)&address,
1812 msg.msg_namelen, addr, addr_len);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001813 if (err2 < 0)
1814 err = err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 }
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001816
1817 fput_light(sock->file, fput_needed);
David S. Miller4387ff72007-02-08 15:06:08 -08001818out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 return err;
1820}
1821
1822/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001823 * Receive a datagram from a socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 */
1825
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001826asmlinkage long sys_recv(int fd, void __user *ubuf, size_t size,
1827 unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828{
1829 return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
1830}
1831
1832/*
1833 * Set a socket option. Because we don't know the option lengths we have
1834 * to pass the user mode parameter for the protocols to sort out.
1835 */
1836
Heiko Carstens20f37032009-01-14 14:14:23 +01001837SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname,
1838 char __user *, optval, int, optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001840 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 struct socket *sock;
1842
1843 if (optlen < 0)
1844 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001845
1846 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1847 if (sock != NULL) {
1848 err = security_socket_setsockopt(sock, level, optname);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001849 if (err)
1850 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
1852 if (level == SOL_SOCKET)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001853 err =
1854 sock_setsockopt(sock, level, optname, optval,
1855 optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 else
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001857 err =
1858 sock->ops->setsockopt(sock, level, optname, optval,
1859 optlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001860out_put:
1861 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 }
1863 return err;
1864}
1865
1866/*
1867 * Get a socket option. Because we don't know the option lengths we have
1868 * to pass a user mode parameter for the protocols to sort out.
1869 */
1870
Heiko Carstens20f37032009-01-14 14:14:23 +01001871SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname,
1872 char __user *, optval, int __user *, optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001874 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 struct socket *sock;
1876
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001877 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1878 if (sock != NULL) {
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001879 err = security_socket_getsockopt(sock, level, optname);
1880 if (err)
1881 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
1883 if (level == SOL_SOCKET)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001884 err =
1885 sock_getsockopt(sock, level, optname, optval,
1886 optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 else
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001888 err =
1889 sock->ops->getsockopt(sock, level, optname, optval,
1890 optlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001891out_put:
1892 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 }
1894 return err;
1895}
1896
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897/*
1898 * Shutdown a socket.
1899 */
1900
Heiko Carstens754fe8d2009-01-14 14:14:09 +01001901SYSCALL_DEFINE2(shutdown, int, fd, int, how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001903 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 struct socket *sock;
1905
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001906 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1907 if (sock != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 err = security_socket_shutdown(sock, how);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001909 if (!err)
1910 err = sock->ops->shutdown(sock, how);
1911 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 }
1913 return err;
1914}
1915
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001916/* A couple of helpful macros for getting the address of the 32/64 bit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 * fields which are the same type (int / unsigned) on our platforms.
1918 */
1919#define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
1920#define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
1921#define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
1922
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923/*
1924 * BSD sendmsg interface
1925 */
1926
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001927SYSCALL_DEFINE3(sendmsg, int, fd, struct msghdr __user *, msg, unsigned, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928{
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001929 struct compat_msghdr __user *msg_compat =
1930 (struct compat_msghdr __user *)msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001932 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
Alex Williamsonb9d717a2005-09-26 14:28:02 -07001934 unsigned char ctl[sizeof(struct cmsghdr) + 20]
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001935 __attribute__ ((aligned(sizeof(__kernel_size_t))));
1936 /* 20 is size of ipv6_pktinfo */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 unsigned char *ctl_buf = ctl;
1938 struct msghdr msg_sys;
1939 int err, ctl_len, iov_size, total_len;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001940 int fput_needed;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001941
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 err = -EFAULT;
1943 if (MSG_CMSG_COMPAT & flags) {
1944 if (get_compat_msghdr(&msg_sys, msg_compat))
1945 return -EFAULT;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001946 }
1947 else if (copy_from_user(&msg_sys, msg, sizeof(struct msghdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 return -EFAULT;
1949
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001950 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001951 if (!sock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 goto out;
1953
1954 /* do not move before msg_sys is valid */
1955 err = -EMSGSIZE;
1956 if (msg_sys.msg_iovlen > UIO_MAXIOV)
1957 goto out_put;
1958
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001959 /* Check whether to allocate the iovec area */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 err = -ENOMEM;
1961 iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
1962 if (msg_sys.msg_iovlen > UIO_FASTIOV) {
1963 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
1964 if (!iov)
1965 goto out_put;
1966 }
1967
1968 /* This will also move the address data into kernel space */
1969 if (MSG_CMSG_COMPAT & flags) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001970 err = verify_compat_iovec(&msg_sys, iov,
1971 (struct sockaddr *)&address,
1972 VERIFY_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 } else
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001974 err = verify_iovec(&msg_sys, iov,
1975 (struct sockaddr *)&address,
1976 VERIFY_READ);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001977 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 goto out_freeiov;
1979 total_len = err;
1980
1981 err = -ENOBUFS;
1982
1983 if (msg_sys.msg_controllen > INT_MAX)
1984 goto out_freeiov;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001985 ctl_len = msg_sys.msg_controllen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001987 err =
1988 cmsghdr_from_user_compat_to_kern(&msg_sys, sock->sk, ctl,
1989 sizeof(ctl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 if (err)
1991 goto out_freeiov;
1992 ctl_buf = msg_sys.msg_control;
Al Viro8920e8f2005-09-07 18:28:51 -07001993 ctl_len = msg_sys.msg_controllen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 } else if (ctl_len) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001995 if (ctl_len > sizeof(ctl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001997 if (ctl_buf == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 goto out_freeiov;
1999 }
2000 err = -EFAULT;
2001 /*
2002 * Careful! Before this, msg_sys.msg_control contains a user pointer.
2003 * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
2004 * checking falls down on this.
2005 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002006 if (copy_from_user(ctl_buf, (void __user *)msg_sys.msg_control,
2007 ctl_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 goto out_freectl;
2009 msg_sys.msg_control = ctl_buf;
2010 }
2011 msg_sys.msg_flags = flags;
2012
2013 if (sock->file->f_flags & O_NONBLOCK)
2014 msg_sys.msg_flags |= MSG_DONTWAIT;
2015 err = sock_sendmsg(sock, &msg_sys, total_len);
2016
2017out_freectl:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002018 if (ctl_buf != ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 sock_kfree_s(sock->sk, ctl_buf, ctl_len);
2020out_freeiov:
2021 if (iov != iovstack)
2022 sock_kfree_s(sock->sk, iov, iov_size);
2023out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002024 fput_light(sock->file, fput_needed);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002025out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 return err;
2027}
2028
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002029static int __sys_recvmsg(struct socket *sock, struct msghdr __user *msg,
2030 struct msghdr *msg_sys, unsigned flags, int nosec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031{
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002032 struct compat_msghdr __user *msg_compat =
2033 (struct compat_msghdr __user *)msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 struct iovec iovstack[UIO_FASTIOV];
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002035 struct iovec *iov = iovstack;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 unsigned long cmsg_ptr;
2037 int err, iov_size, total_len, len;
2038
2039 /* kernel mode address */
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07002040 struct sockaddr_storage addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
2042 /* user mode address pointers */
2043 struct sockaddr __user *uaddr;
2044 int __user *uaddr_len;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002045
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 if (MSG_CMSG_COMPAT & flags) {
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002047 if (get_compat_msghdr(msg_sys, msg_compat))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 return -EFAULT;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002049 }
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002050 else if (copy_from_user(msg_sys, msg, sizeof(struct msghdr)))
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002051 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 err = -EMSGSIZE;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002054 if (msg_sys->msg_iovlen > UIO_MAXIOV)
2055 goto out;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002056
2057 /* Check whether to allocate the iovec area */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 err = -ENOMEM;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002059 iov_size = msg_sys->msg_iovlen * sizeof(struct iovec);
2060 if (msg_sys->msg_iovlen > UIO_FASTIOV) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
2062 if (!iov)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002063 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 }
2065
2066 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002067 * Save the user-mode address (verify_iovec will change the
2068 * kernel msghdr to use the kernel address space)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002070
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002071 uaddr = (__force void __user *)msg_sys->msg_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 uaddr_len = COMPAT_NAMELEN(msg);
2073 if (MSG_CMSG_COMPAT & flags) {
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002074 err = verify_compat_iovec(msg_sys, iov,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07002075 (struct sockaddr *)&addr,
2076 VERIFY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 } else
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002078 err = verify_iovec(msg_sys, iov,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07002079 (struct sockaddr *)&addr,
2080 VERIFY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 if (err < 0)
2082 goto out_freeiov;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002083 total_len = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002085 cmsg_ptr = (unsigned long)msg_sys->msg_control;
2086 msg_sys->msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002087
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 if (sock->file->f_flags & O_NONBLOCK)
2089 flags |= MSG_DONTWAIT;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002090 err = (nosec ? sock_recvmsg_nosec : sock_recvmsg)(sock, msg_sys,
2091 total_len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 if (err < 0)
2093 goto out_freeiov;
2094 len = err;
2095
2096 if (uaddr != NULL) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07002097 err = move_addr_to_user((struct sockaddr *)&addr,
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002098 msg_sys->msg_namelen, uaddr,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002099 uaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 if (err < 0)
2101 goto out_freeiov;
2102 }
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002103 err = __put_user((msg_sys->msg_flags & ~MSG_CMSG_COMPAT),
David S. Miller37f7f422005-09-16 16:51:01 -07002104 COMPAT_FLAGS(msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 if (err)
2106 goto out_freeiov;
2107 if (MSG_CMSG_COMPAT & flags)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002108 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 &msg_compat->msg_controllen);
2110 else
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002111 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 &msg->msg_controllen);
2113 if (err)
2114 goto out_freeiov;
2115 err = len;
2116
2117out_freeiov:
2118 if (iov != iovstack)
2119 sock_kfree_s(sock->sk, iov, iov_size);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002120out:
2121 return err;
2122}
2123
2124/*
2125 * BSD recvmsg interface
2126 */
2127
2128SYSCALL_DEFINE3(recvmsg, int, fd, struct msghdr __user *, msg,
2129 unsigned int, flags)
2130{
2131 int fput_needed, err;
2132 struct msghdr msg_sys;
2133 struct socket *sock = sockfd_lookup_light(fd, &err, &fput_needed);
2134
2135 if (!sock)
2136 goto out;
2137
2138 err = __sys_recvmsg(sock, msg, &msg_sys, flags, 0);
2139
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002140 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141out:
2142 return err;
2143}
2144
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002145/*
2146 * Linux recvmmsg interface
2147 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002149int __sys_recvmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
2150 unsigned int flags, struct timespec *timeout)
2151{
2152 int fput_needed, err, datagrams;
2153 struct socket *sock;
2154 struct mmsghdr __user *entry;
2155 struct msghdr msg_sys;
2156 struct timespec end_time;
2157
2158 if (timeout &&
2159 poll_select_set_timeout(&end_time, timeout->tv_sec,
2160 timeout->tv_nsec))
2161 return -EINVAL;
2162
2163 datagrams = 0;
2164
2165 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2166 if (!sock)
2167 return err;
2168
2169 err = sock_error(sock->sk);
2170 if (err)
2171 goto out_put;
2172
2173 entry = mmsg;
2174
2175 while (datagrams < vlen) {
2176 /*
2177 * No need to ask LSM for more than the first datagram.
2178 */
2179 err = __sys_recvmsg(sock, (struct msghdr __user *)entry,
2180 &msg_sys, flags, datagrams);
2181 if (err < 0)
2182 break;
2183 err = put_user(err, &entry->msg_len);
2184 if (err)
2185 break;
2186 ++entry;
2187 ++datagrams;
2188
2189 if (timeout) {
2190 ktime_get_ts(timeout);
2191 *timeout = timespec_sub(end_time, *timeout);
2192 if (timeout->tv_sec < 0) {
2193 timeout->tv_sec = timeout->tv_nsec = 0;
2194 break;
2195 }
2196
2197 /* Timeout, return less than vlen datagrams */
2198 if (timeout->tv_nsec == 0 && timeout->tv_sec == 0)
2199 break;
2200 }
2201
2202 /* Out of band data, return right away */
2203 if (msg_sys.msg_flags & MSG_OOB)
2204 break;
2205 }
2206
2207out_put:
2208 fput_light(sock->file, fput_needed);
2209
2210 if (err == 0)
2211 return datagrams;
2212
2213 if (datagrams != 0) {
2214 /*
2215 * We may return less entries than requested (vlen) if the
2216 * sock is non block and there aren't enough datagrams...
2217 */
2218 if (err != -EAGAIN) {
2219 /*
2220 * ... or if recvmsg returns an error after we
2221 * received some datagrams, where we record the
2222 * error to return on the next call or if the
2223 * app asks about it using getsockopt(SO_ERROR).
2224 */
2225 sock->sk->sk_err = -err;
2226 }
2227
2228 return datagrams;
2229 }
2230
2231 return err;
2232}
2233
2234SYSCALL_DEFINE5(recvmmsg, int, fd, struct mmsghdr __user *, mmsg,
2235 unsigned int, vlen, unsigned int, flags,
2236 struct timespec __user *, timeout)
2237{
2238 int datagrams;
2239 struct timespec timeout_sys;
2240
2241 if (!timeout)
2242 return __sys_recvmmsg(fd, mmsg, vlen, flags, NULL);
2243
2244 if (copy_from_user(&timeout_sys, timeout, sizeof(timeout_sys)))
2245 return -EFAULT;
2246
2247 datagrams = __sys_recvmmsg(fd, mmsg, vlen, flags, &timeout_sys);
2248
2249 if (datagrams > 0 &&
2250 copy_to_user(timeout, &timeout_sys, sizeof(timeout_sys)))
2251 datagrams = -EFAULT;
2252
2253 return datagrams;
2254}
2255
2256#ifdef __ARCH_WANT_SYS_SOCKETCALL
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257/* Argument list sizes for sys_socketcall */
2258#define AL(x) ((x) * sizeof(unsigned long))
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002259static const unsigned char nargs[20] = {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002260 AL(0),AL(3),AL(3),AL(3),AL(2),AL(3),
2261 AL(3),AL(3),AL(4),AL(4),AL(4),AL(6),
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07002262 AL(6),AL(2),AL(5),AL(5),AL(3),AL(3),
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002263 AL(4),AL(5)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002264};
2265
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266#undef AL
2267
2268/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002269 * System call vectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 *
2271 * Argument checking cleaned up. Saved 20% in size.
2272 * This function doesn't need to set the kernel lock because
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002273 * it is set by the callees.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 */
2275
Heiko Carstens3e0fa652009-01-14 14:14:24 +01002276SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277{
2278 unsigned long a[6];
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002279 unsigned long a0, a1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 int err;
Arjan van de Ven47379052009-09-28 12:57:44 -07002281 unsigned int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002283 if (call < 1 || call > SYS_RECVMMSG)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 return -EINVAL;
2285
Arjan van de Ven47379052009-09-28 12:57:44 -07002286 len = nargs[call];
2287 if (len > sizeof(a))
2288 return -EINVAL;
2289
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 /* copy_from_user should be SMP safe. */
Arjan van de Ven47379052009-09-28 12:57:44 -07002291 if (copy_from_user(a, args, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 return -EFAULT;
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002293
Al Virof3298dc2008-12-10 03:16:51 -05002294 audit_socketcall(nargs[call] / sizeof(unsigned long), a);
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002295
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002296 a0 = a[0];
2297 a1 = a[1];
2298
2299 switch (call) {
2300 case SYS_SOCKET:
2301 err = sys_socket(a0, a1, a[2]);
2302 break;
2303 case SYS_BIND:
2304 err = sys_bind(a0, (struct sockaddr __user *)a1, a[2]);
2305 break;
2306 case SYS_CONNECT:
2307 err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
2308 break;
2309 case SYS_LISTEN:
2310 err = sys_listen(a0, a1);
2311 break;
2312 case SYS_ACCEPT:
Ulrich Drepperde11def2008-11-19 15:36:14 -08002313 err = sys_accept4(a0, (struct sockaddr __user *)a1,
2314 (int __user *)a[2], 0);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002315 break;
2316 case SYS_GETSOCKNAME:
2317 err =
2318 sys_getsockname(a0, (struct sockaddr __user *)a1,
2319 (int __user *)a[2]);
2320 break;
2321 case SYS_GETPEERNAME:
2322 err =
2323 sys_getpeername(a0, (struct sockaddr __user *)a1,
2324 (int __user *)a[2]);
2325 break;
2326 case SYS_SOCKETPAIR:
2327 err = sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
2328 break;
2329 case SYS_SEND:
2330 err = sys_send(a0, (void __user *)a1, a[2], a[3]);
2331 break;
2332 case SYS_SENDTO:
2333 err = sys_sendto(a0, (void __user *)a1, a[2], a[3],
2334 (struct sockaddr __user *)a[4], a[5]);
2335 break;
2336 case SYS_RECV:
2337 err = sys_recv(a0, (void __user *)a1, a[2], a[3]);
2338 break;
2339 case SYS_RECVFROM:
2340 err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
2341 (struct sockaddr __user *)a[4],
2342 (int __user *)a[5]);
2343 break;
2344 case SYS_SHUTDOWN:
2345 err = sys_shutdown(a0, a1);
2346 break;
2347 case SYS_SETSOCKOPT:
2348 err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]);
2349 break;
2350 case SYS_GETSOCKOPT:
2351 err =
2352 sys_getsockopt(a0, a1, a[2], (char __user *)a[3],
2353 (int __user *)a[4]);
2354 break;
2355 case SYS_SENDMSG:
2356 err = sys_sendmsg(a0, (struct msghdr __user *)a1, a[2]);
2357 break;
2358 case SYS_RECVMSG:
2359 err = sys_recvmsg(a0, (struct msghdr __user *)a1, a[2]);
2360 break;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002361 case SYS_RECVMMSG:
2362 err = sys_recvmmsg(a0, (struct mmsghdr __user *)a1, a[2], a[3],
2363 (struct timespec __user *)a[4]);
2364 break;
Ulrich Drepperde11def2008-11-19 15:36:14 -08002365 case SYS_ACCEPT4:
2366 err = sys_accept4(a0, (struct sockaddr __user *)a1,
2367 (int __user *)a[2], a[3]);
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07002368 break;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002369 default:
2370 err = -EINVAL;
2371 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 }
2373 return err;
2374}
2375
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002376#endif /* __ARCH_WANT_SYS_SOCKETCALL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002378/**
2379 * sock_register - add a socket protocol handler
2380 * @ops: description of protocol
2381 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 * This function is called by a protocol handler that wants to
2383 * advertise its address family, and have it linked into the
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002384 * socket interface. The value ops->family coresponds to the
2385 * socket system call protocol family.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 */
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002387int sock_register(const struct net_proto_family *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388{
2389 int err;
2390
2391 if (ops->family >= NPROTO) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002392 printk(KERN_CRIT "protocol %d >= NPROTO(%d)\n", ops->family,
2393 NPROTO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 return -ENOBUFS;
2395 }
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002396
2397 spin_lock(&net_family_lock);
2398 if (net_families[ops->family])
2399 err = -EEXIST;
2400 else {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002401 net_families[ops->family] = ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 err = 0;
2403 }
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002404 spin_unlock(&net_family_lock);
2405
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002406 printk(KERN_INFO "NET: Registered protocol family %d\n", ops->family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 return err;
2408}
2409
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002410/**
2411 * sock_unregister - remove a protocol handler
2412 * @family: protocol family to remove
2413 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 * This function is called by a protocol handler that wants to
2415 * remove its address family, and have it unlinked from the
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002416 * new socket creation.
2417 *
2418 * If protocol handler is a module, then it can use module reference
2419 * counts to protect against new references. If protocol handler is not
2420 * a module then it needs to provide its own protection in
2421 * the ops->create routine.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 */
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002423void sock_unregister(int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424{
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002425 BUG_ON(family < 0 || family >= NPROTO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002427 spin_lock(&net_family_lock);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002428 net_families[family] = NULL;
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002429 spin_unlock(&net_family_lock);
2430
2431 synchronize_rcu();
2432
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002433 printk(KERN_INFO "NET: Unregistered protocol family %d\n", family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434}
2435
Andi Kleen77d76ea2005-12-22 12:43:42 -08002436static int __init sock_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437{
2438 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002439 * Initialize sock SLAB cache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002441
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 sk_init();
2443
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002445 * Initialize skbuff SLAB cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 */
2447 skb_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448
2449 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002450 * Initialize the protocols module.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 */
2452
2453 init_inodecache();
2454 register_filesystem(&sock_fs_type);
2455 sock_mnt = kern_mount(&sock_fs_type);
Andi Kleen77d76ea2005-12-22 12:43:42 -08002456
2457 /* The real protocol initialization is performed in later initcalls.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 */
2459
2460#ifdef CONFIG_NETFILTER
2461 netfilter_init();
2462#endif
David S. Millercbeb3212005-12-22 12:58:55 -08002463
2464 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465}
2466
Andi Kleen77d76ea2005-12-22 12:43:42 -08002467core_initcall(sock_init); /* early initcall */
2468
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469#ifdef CONFIG_PROC_FS
2470void socket_seq_show(struct seq_file *seq)
2471{
2472 int cpu;
2473 int counter = 0;
2474
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -07002475 for_each_possible_cpu(cpu)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002476 counter += per_cpu(sockets_in_use, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477
2478 /* It can be negative, by the way. 8) */
2479 if (counter < 0)
2480 counter = 0;
2481
2482 seq_printf(seq, "sockets: used %d\n", counter);
2483}
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002484#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485
Shaun Pereira89bbfc92006-03-21 23:58:08 -08002486#ifdef CONFIG_COMPAT
Arnd Bergmann6b960182009-11-06 23:10:54 -08002487static int do_siocgstamp(struct net *net, struct socket *sock,
2488 unsigned int cmd, struct compat_timeval __user *up)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002489{
Arnd Bergmann7a229382009-11-06 23:00:29 -08002490 mm_segment_t old_fs = get_fs();
2491 struct timeval ktv;
2492 int err;
2493
2494 set_fs(KERNEL_DS);
Arnd Bergmann6b960182009-11-06 23:10:54 -08002495 err = sock_do_ioctl(net, sock, cmd, (unsigned long)&ktv);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002496 set_fs(old_fs);
2497 if (!err) {
2498 err = put_user(ktv.tv_sec, &up->tv_sec);
2499 err |= __put_user(ktv.tv_usec, &up->tv_usec);
2500 }
2501 return err;
2502}
2503
Arnd Bergmann6b960182009-11-06 23:10:54 -08002504static int do_siocgstampns(struct net *net, struct socket *sock,
2505 unsigned int cmd, struct compat_timespec __user *up)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002506{
Arnd Bergmann7a229382009-11-06 23:00:29 -08002507 mm_segment_t old_fs = get_fs();
2508 struct timespec kts;
2509 int err;
2510
2511 set_fs(KERNEL_DS);
Arnd Bergmann6b960182009-11-06 23:10:54 -08002512 err = sock_do_ioctl(net, sock, cmd, (unsigned long)&kts);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002513 set_fs(old_fs);
2514 if (!err) {
2515 err = put_user(kts.tv_sec, &up->tv_sec);
2516 err |= __put_user(kts.tv_nsec, &up->tv_nsec);
2517 }
2518 return err;
2519}
2520
Arnd Bergmann6b960182009-11-06 23:10:54 -08002521static int dev_ifname32(struct net *net, struct compat_ifreq __user *uifr32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002522{
2523 struct ifreq __user *uifr;
2524 int err;
2525
2526 uifr = compat_alloc_user_space(sizeof(struct ifreq));
Arnd Bergmann6b960182009-11-06 23:10:54 -08002527 if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002528 return -EFAULT;
2529
Arnd Bergmann6b960182009-11-06 23:10:54 -08002530 err = dev_ioctl(net, SIOCGIFNAME, uifr);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002531 if (err)
2532 return err;
2533
Arnd Bergmann6b960182009-11-06 23:10:54 -08002534 if (copy_in_user(uifr32, uifr, sizeof(struct compat_ifreq)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002535 return -EFAULT;
2536
2537 return 0;
2538}
2539
Arnd Bergmann6b960182009-11-06 23:10:54 -08002540static int dev_ifconf(struct net *net, struct compat_ifconf __user *uifc32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002541{
Arnd Bergmann6b960182009-11-06 23:10:54 -08002542 struct compat_ifconf ifc32;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002543 struct ifconf ifc;
2544 struct ifconf __user *uifc;
Arnd Bergmann6b960182009-11-06 23:10:54 -08002545 struct compat_ifreq __user *ifr32;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002546 struct ifreq __user *ifr;
2547 unsigned int i, j;
2548 int err;
2549
Arnd Bergmann6b960182009-11-06 23:10:54 -08002550 if (copy_from_user(&ifc32, uifc32, sizeof(struct compat_ifconf)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002551 return -EFAULT;
2552
2553 if (ifc32.ifcbuf == 0) {
2554 ifc32.ifc_len = 0;
2555 ifc.ifc_len = 0;
2556 ifc.ifc_req = NULL;
2557 uifc = compat_alloc_user_space(sizeof(struct ifconf));
2558 } else {
Arnd Bergmann6b960182009-11-06 23:10:54 -08002559 size_t len =((ifc32.ifc_len / sizeof (struct compat_ifreq)) + 1) *
Arnd Bergmann7a229382009-11-06 23:00:29 -08002560 sizeof (struct ifreq);
2561 uifc = compat_alloc_user_space(sizeof(struct ifconf) + len);
2562 ifc.ifc_len = len;
2563 ifr = ifc.ifc_req = (void __user *)(uifc + 1);
2564 ifr32 = compat_ptr(ifc32.ifcbuf);
Arnd Bergmann6b960182009-11-06 23:10:54 -08002565 for (i = 0; i < ifc32.ifc_len; i += sizeof (struct compat_ifreq)) {
2566 if (copy_in_user(ifr, ifr32, sizeof(struct compat_ifreq)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002567 return -EFAULT;
2568 ifr++;
2569 ifr32++;
2570 }
2571 }
2572 if (copy_to_user(uifc, &ifc, sizeof(struct ifconf)))
2573 return -EFAULT;
2574
Arnd Bergmann6b960182009-11-06 23:10:54 -08002575 err = dev_ioctl(net, SIOCGIFCONF, uifc);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002576 if (err)
2577 return err;
2578
2579 if (copy_from_user(&ifc, uifc, sizeof(struct ifconf)))
2580 return -EFAULT;
2581
2582 ifr = ifc.ifc_req;
2583 ifr32 = compat_ptr(ifc32.ifcbuf);
2584 for (i = 0, j = 0;
Arnd Bergmann6b960182009-11-06 23:10:54 -08002585 i + sizeof (struct compat_ifreq) <= ifc32.ifc_len && j < ifc.ifc_len;
2586 i += sizeof (struct compat_ifreq), j += sizeof (struct ifreq)) {
2587 if (copy_in_user(ifr32, ifr, sizeof (struct compat_ifreq)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002588 return -EFAULT;
2589 ifr32++;
2590 ifr++;
2591 }
2592
2593 if (ifc32.ifcbuf == 0) {
2594 /* Translate from 64-bit structure multiple to
2595 * a 32-bit one.
2596 */
2597 i = ifc.ifc_len;
Arnd Bergmann6b960182009-11-06 23:10:54 -08002598 i = ((i / sizeof(struct ifreq)) * sizeof(struct compat_ifreq));
Arnd Bergmann7a229382009-11-06 23:00:29 -08002599 ifc32.ifc_len = i;
2600 } else {
2601 ifc32.ifc_len = i;
2602 }
Arnd Bergmann6b960182009-11-06 23:10:54 -08002603 if (copy_to_user(uifc32, &ifc32, sizeof(struct compat_ifconf)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002604 return -EFAULT;
2605
2606 return 0;
2607}
2608
Arnd Bergmann6b960182009-11-06 23:10:54 -08002609static int ethtool_ioctl(struct net *net, struct compat_ifreq __user *ifr32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002610{
2611 struct ifreq __user *ifr;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002612 u32 data;
2613 void __user *datap;
2614
2615 ifr = compat_alloc_user_space(sizeof(*ifr));
Arnd Bergmann7a229382009-11-06 23:00:29 -08002616
2617 if (copy_in_user(&ifr->ifr_name, &ifr32->ifr_name, IFNAMSIZ))
2618 return -EFAULT;
2619
2620 if (get_user(data, &ifr32->ifr_ifru.ifru_data))
2621 return -EFAULT;
2622
2623 datap = compat_ptr(data);
2624 if (put_user(datap, &ifr->ifr_ifru.ifru_data))
2625 return -EFAULT;
2626
Arnd Bergmann6b960182009-11-06 23:10:54 -08002627 return dev_ioctl(net, SIOCETHTOOL, ifr);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002628}
2629
Arnd Bergmann6b960182009-11-06 23:10:54 -08002630static int bond_ioctl(struct net *net, unsigned int cmd,
2631 struct compat_ifreq __user *ifr32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002632{
2633 struct ifreq kifr;
2634 struct ifreq __user *uifr;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002635 mm_segment_t old_fs;
2636 int err;
2637 u32 data;
2638 void __user *datap;
2639
2640 switch (cmd) {
2641 case SIOCBONDENSLAVE:
2642 case SIOCBONDRELEASE:
2643 case SIOCBONDSETHWADDR:
2644 case SIOCBONDCHANGEACTIVE:
Arnd Bergmann6b960182009-11-06 23:10:54 -08002645 if (copy_from_user(&kifr, ifr32, sizeof(struct compat_ifreq)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002646 return -EFAULT;
2647
2648 old_fs = get_fs();
2649 set_fs (KERNEL_DS);
Arnd Bergmann6b960182009-11-06 23:10:54 -08002650 err = dev_ioctl(net, cmd, &kifr);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002651 set_fs (old_fs);
2652
2653 return err;
2654 case SIOCBONDSLAVEINFOQUERY:
2655 case SIOCBONDINFOQUERY:
2656 uifr = compat_alloc_user_space(sizeof(*uifr));
2657 if (copy_in_user(&uifr->ifr_name, &ifr32->ifr_name, IFNAMSIZ))
2658 return -EFAULT;
2659
2660 if (get_user(data, &ifr32->ifr_ifru.ifru_data))
2661 return -EFAULT;
2662
2663 datap = compat_ptr(data);
2664 if (put_user(datap, &uifr->ifr_ifru.ifru_data))
2665 return -EFAULT;
2666
Arnd Bergmann6b960182009-11-06 23:10:54 -08002667 return dev_ioctl(net, cmd, uifr);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002668 default:
2669 return -EINVAL;
2670 };
2671}
2672
Arnd Bergmann6b960182009-11-06 23:10:54 -08002673static int siocdevprivate_ioctl(struct net *net, unsigned int cmd,
2674 struct compat_ifreq __user *u_ifreq32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002675{
2676 struct ifreq __user *u_ifreq64;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002677 char tmp_buf[IFNAMSIZ];
2678 void __user *data64;
2679 u32 data32;
2680
2681 if (copy_from_user(&tmp_buf[0], &(u_ifreq32->ifr_ifrn.ifrn_name[0]),
2682 IFNAMSIZ))
2683 return -EFAULT;
2684 if (__get_user(data32, &u_ifreq32->ifr_ifru.ifru_data))
2685 return -EFAULT;
2686 data64 = compat_ptr(data32);
2687
2688 u_ifreq64 = compat_alloc_user_space(sizeof(*u_ifreq64));
2689
2690 /* Don't check these user accesses, just let that get trapped
2691 * in the ioctl handler instead.
2692 */
2693 if (copy_to_user(&u_ifreq64->ifr_ifrn.ifrn_name[0], &tmp_buf[0],
2694 IFNAMSIZ))
2695 return -EFAULT;
2696 if (__put_user(data64, &u_ifreq64->ifr_ifru.ifru_data))
2697 return -EFAULT;
2698
Arnd Bergmann6b960182009-11-06 23:10:54 -08002699 return dev_ioctl(net, cmd, u_ifreq64);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002700}
2701
Arnd Bergmann6b960182009-11-06 23:10:54 -08002702static int dev_ifsioc(struct net *net, struct socket *sock,
2703 unsigned int cmd, struct compat_ifreq __user *uifr32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002704{
2705 struct ifreq ifr;
Arnd Bergmann6b960182009-11-06 23:10:54 -08002706 struct compat_ifmap __user *uifmap32;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002707 mm_segment_t old_fs;
2708 int err;
2709
Arnd Bergmann7a229382009-11-06 23:00:29 -08002710 uifmap32 = &uifr32->ifr_ifru.ifru_map;
2711 switch (cmd) {
2712 case SIOCSIFMAP:
2713 err = copy_from_user(&ifr, uifr32, sizeof(ifr.ifr_name));
2714 err |= __get_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
2715 err |= __get_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
2716 err |= __get_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
2717 err |= __get_user(ifr.ifr_map.irq, &uifmap32->irq);
2718 err |= __get_user(ifr.ifr_map.dma, &uifmap32->dma);
2719 err |= __get_user(ifr.ifr_map.port, &uifmap32->port);
2720 if (err)
2721 return -EFAULT;
2722 break;
2723 case SIOCSHWTSTAMP:
2724 if (copy_from_user(&ifr, uifr32, sizeof(*uifr32)))
2725 return -EFAULT;
2726 ifr.ifr_data = compat_ptr(uifr32->ifr_ifru.ifru_data);
2727 break;
2728 default:
2729 if (copy_from_user(&ifr, uifr32, sizeof(*uifr32)))
2730 return -EFAULT;
2731 break;
2732 }
2733 old_fs = get_fs();
2734 set_fs (KERNEL_DS);
Arnd Bergmann6b960182009-11-06 23:10:54 -08002735 err = sock_do_ioctl(net, sock, cmd, (unsigned long)&ifr);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002736 set_fs (old_fs);
2737 if (!err) {
2738 switch (cmd) {
2739 case SIOCGIFFLAGS:
2740 case SIOCGIFMETRIC:
2741 case SIOCGIFMTU:
2742 case SIOCGIFMEM:
2743 case SIOCGIFHWADDR:
2744 case SIOCGIFINDEX:
2745 case SIOCGIFADDR:
2746 case SIOCGIFBRDADDR:
2747 case SIOCGIFDSTADDR:
2748 case SIOCGIFNETMASK:
2749 case SIOCGIFTXQLEN:
2750 if (copy_to_user(uifr32, &ifr, sizeof(*uifr32)))
2751 return -EFAULT;
2752 break;
2753 case SIOCGIFMAP:
2754 err = copy_to_user(uifr32, &ifr, sizeof(ifr.ifr_name));
2755 err |= __put_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
2756 err |= __put_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
2757 err |= __put_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
2758 err |= __put_user(ifr.ifr_map.irq, &uifmap32->irq);
2759 err |= __put_user(ifr.ifr_map.dma, &uifmap32->dma);
2760 err |= __put_user(ifr.ifr_map.port, &uifmap32->port);
2761 if (err)
2762 err = -EFAULT;
2763 break;
2764 }
2765 }
2766 return err;
2767}
2768
2769struct rtentry32 {
2770 u32 rt_pad1;
2771 struct sockaddr rt_dst; /* target address */
2772 struct sockaddr rt_gateway; /* gateway addr (RTF_GATEWAY) */
2773 struct sockaddr rt_genmask; /* target network mask (IP) */
2774 unsigned short rt_flags;
2775 short rt_pad2;
2776 u32 rt_pad3;
2777 unsigned char rt_tos;
2778 unsigned char rt_class;
2779 short rt_pad4;
2780 short rt_metric; /* +1 for binary compatibility! */
2781 /* char * */ u32 rt_dev; /* forcing the device at add */
2782 u32 rt_mtu; /* per route MTU/Window */
2783 u32 rt_window; /* Window clamping */
2784 unsigned short rt_irtt; /* Initial RTT */
2785};
2786
2787struct in6_rtmsg32 {
2788 struct in6_addr rtmsg_dst;
2789 struct in6_addr rtmsg_src;
2790 struct in6_addr rtmsg_gateway;
2791 u32 rtmsg_type;
2792 u16 rtmsg_dst_len;
2793 u16 rtmsg_src_len;
2794 u32 rtmsg_metric;
2795 u32 rtmsg_info;
2796 u32 rtmsg_flags;
2797 s32 rtmsg_ifindex;
2798};
2799
Arnd Bergmann6b960182009-11-06 23:10:54 -08002800static int routing_ioctl(struct net *net, struct socket *sock,
2801 unsigned int cmd, void __user *argp)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002802{
2803 int ret;
2804 void *r = NULL;
2805 struct in6_rtmsg r6;
2806 struct rtentry r4;
2807 char devname[16];
2808 u32 rtdev;
2809 mm_segment_t old_fs = get_fs();
2810
Arnd Bergmann6b960182009-11-06 23:10:54 -08002811 if (sock && sock->sk && sock->sk->sk_family == AF_INET6) { /* ipv6 */
2812 struct in6_rtmsg32 __user *ur6 = argp;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002813 ret = copy_from_user (&r6.rtmsg_dst, &(ur6->rtmsg_dst),
2814 3 * sizeof(struct in6_addr));
2815 ret |= __get_user (r6.rtmsg_type, &(ur6->rtmsg_type));
2816 ret |= __get_user (r6.rtmsg_dst_len, &(ur6->rtmsg_dst_len));
2817 ret |= __get_user (r6.rtmsg_src_len, &(ur6->rtmsg_src_len));
2818 ret |= __get_user (r6.rtmsg_metric, &(ur6->rtmsg_metric));
2819 ret |= __get_user (r6.rtmsg_info, &(ur6->rtmsg_info));
2820 ret |= __get_user (r6.rtmsg_flags, &(ur6->rtmsg_flags));
2821 ret |= __get_user (r6.rtmsg_ifindex, &(ur6->rtmsg_ifindex));
2822
2823 r = (void *) &r6;
2824 } else { /* ipv4 */
Arnd Bergmann6b960182009-11-06 23:10:54 -08002825 struct rtentry32 __user *ur4 = argp;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002826 ret = copy_from_user (&r4.rt_dst, &(ur4->rt_dst),
2827 3 * sizeof(struct sockaddr));
2828 ret |= __get_user (r4.rt_flags, &(ur4->rt_flags));
2829 ret |= __get_user (r4.rt_metric, &(ur4->rt_metric));
2830 ret |= __get_user (r4.rt_mtu, &(ur4->rt_mtu));
2831 ret |= __get_user (r4.rt_window, &(ur4->rt_window));
2832 ret |= __get_user (r4.rt_irtt, &(ur4->rt_irtt));
2833 ret |= __get_user (rtdev, &(ur4->rt_dev));
2834 if (rtdev) {
2835 ret |= copy_from_user (devname, compat_ptr(rtdev), 15);
2836 r4.rt_dev = devname; devname[15] = 0;
2837 } else
2838 r4.rt_dev = NULL;
2839
2840 r = (void *) &r4;
2841 }
2842
2843 if (ret) {
2844 ret = -EFAULT;
2845 goto out;
2846 }
2847
2848 set_fs (KERNEL_DS);
Arnd Bergmann6b960182009-11-06 23:10:54 -08002849 ret = sock_do_ioctl(net, sock, cmd, (unsigned long) r);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002850 set_fs (old_fs);
2851
2852out:
Arnd Bergmann7a229382009-11-06 23:00:29 -08002853 return ret;
2854}
2855
2856/* Since old style bridge ioctl's endup using SIOCDEVPRIVATE
2857 * for some operations; this forces use of the newer bridge-utils that
2858 * use compatiable ioctls
2859 */
Arnd Bergmann6b960182009-11-06 23:10:54 -08002860static int old_bridge_ioctl(compat_ulong_t __user *argp)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002861{
Arnd Bergmann6b960182009-11-06 23:10:54 -08002862 compat_ulong_t tmp;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002863
Arnd Bergmann6b960182009-11-06 23:10:54 -08002864 if (get_user(tmp, argp))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002865 return -EFAULT;
2866 if (tmp == BRCTL_GET_VERSION)
2867 return BRCTL_VERSION + 1;
2868 return -EINVAL;
2869}
2870
2871struct atmif_sioc32 {
2872 compat_int_t number;
2873 compat_int_t length;
2874 compat_caddr_t arg;
2875};
2876
2877struct atm_iobuf32 {
2878 compat_int_t length;
2879 compat_caddr_t buffer;
2880};
2881
2882#define ATM_GETLINKRATE32 _IOW('a', ATMIOC_ITF+1, struct atmif_sioc32)
2883#define ATM_GETNAMES32 _IOW('a', ATMIOC_ITF+3, struct atm_iobuf32)
2884#define ATM_GETTYPE32 _IOW('a', ATMIOC_ITF+4, struct atmif_sioc32)
2885#define ATM_GETESI32 _IOW('a', ATMIOC_ITF+5, struct atmif_sioc32)
2886#define ATM_GETADDR32 _IOW('a', ATMIOC_ITF+6, struct atmif_sioc32)
2887#define ATM_RSTADDR32 _IOW('a', ATMIOC_ITF+7, struct atmif_sioc32)
2888#define ATM_ADDADDR32 _IOW('a', ATMIOC_ITF+8, struct atmif_sioc32)
2889#define ATM_DELADDR32 _IOW('a', ATMIOC_ITF+9, struct atmif_sioc32)
2890#define ATM_GETCIRANGE32 _IOW('a', ATMIOC_ITF+10, struct atmif_sioc32)
2891#define ATM_SETCIRANGE32 _IOW('a', ATMIOC_ITF+11, struct atmif_sioc32)
2892#define ATM_SETESI32 _IOW('a', ATMIOC_ITF+12, struct atmif_sioc32)
2893#define ATM_SETESIF32 _IOW('a', ATMIOC_ITF+13, struct atmif_sioc32)
2894#define ATM_GETSTAT32 _IOW('a', ATMIOC_SARCOM+0, struct atmif_sioc32)
2895#define ATM_GETSTATZ32 _IOW('a', ATMIOC_SARCOM+1, struct atmif_sioc32)
2896#define ATM_GETLOOP32 _IOW('a', ATMIOC_SARCOM+2, struct atmif_sioc32)
2897#define ATM_SETLOOP32 _IOW('a', ATMIOC_SARCOM+3, struct atmif_sioc32)
2898#define ATM_QUERYLOOP32 _IOW('a', ATMIOC_SARCOM+4, struct atmif_sioc32)
2899
2900static struct {
2901 unsigned int cmd32;
2902 unsigned int cmd;
2903} atm_ioctl_map[] = {
2904 { ATM_GETLINKRATE32, ATM_GETLINKRATE },
2905 { ATM_GETNAMES32, ATM_GETNAMES },
2906 { ATM_GETTYPE32, ATM_GETTYPE },
2907 { ATM_GETESI32, ATM_GETESI },
2908 { ATM_GETADDR32, ATM_GETADDR },
2909 { ATM_RSTADDR32, ATM_RSTADDR },
2910 { ATM_ADDADDR32, ATM_ADDADDR },
2911 { ATM_DELADDR32, ATM_DELADDR },
2912 { ATM_GETCIRANGE32, ATM_GETCIRANGE },
2913 { ATM_SETCIRANGE32, ATM_SETCIRANGE },
2914 { ATM_SETESI32, ATM_SETESI },
2915 { ATM_SETESIF32, ATM_SETESIF },
2916 { ATM_GETSTAT32, ATM_GETSTAT },
2917 { ATM_GETSTATZ32, ATM_GETSTATZ },
2918 { ATM_GETLOOP32, ATM_GETLOOP },
2919 { ATM_SETLOOP32, ATM_SETLOOP },
2920 { ATM_QUERYLOOP32, ATM_QUERYLOOP }
2921};
2922
2923#define NR_ATM_IOCTL ARRAY_SIZE(atm_ioctl_map)
2924
Arnd Bergmann6b960182009-11-06 23:10:54 -08002925static int do_atm_iobuf(struct net *net, struct socket *sock,
2926 unsigned int cmd, unsigned long arg)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002927{
2928 struct atm_iobuf __user *iobuf;
2929 struct atm_iobuf32 __user *iobuf32;
2930 u32 data;
2931 void __user *datap;
2932 int len, err;
2933
2934 iobuf = compat_alloc_user_space(sizeof(*iobuf));
2935 iobuf32 = compat_ptr(arg);
2936
2937 if (get_user(len, &iobuf32->length) ||
2938 get_user(data, &iobuf32->buffer))
2939 return -EFAULT;
2940 datap = compat_ptr(data);
2941 if (put_user(len, &iobuf->length) ||
2942 put_user(datap, &iobuf->buffer))
2943 return -EFAULT;
2944
Arnd Bergmann6b960182009-11-06 23:10:54 -08002945 err = sock_do_ioctl(net, sock, cmd, (unsigned long)iobuf);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002946
2947 if (!err) {
2948 if (copy_in_user(&iobuf32->length, &iobuf->length,
2949 sizeof(int)))
2950 err = -EFAULT;
2951 }
2952
2953 return err;
2954}
2955
Arnd Bergmann6b960182009-11-06 23:10:54 -08002956static int do_atmif_sioc(struct net *net, struct socket *sock,
2957 unsigned int cmd, unsigned long arg)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002958{
2959 struct atmif_sioc __user *sioc;
2960 struct atmif_sioc32 __user *sioc32;
2961 u32 data;
2962 void __user *datap;
2963 int err;
2964
2965 sioc = compat_alloc_user_space(sizeof(*sioc));
2966 sioc32 = compat_ptr(arg);
2967
2968 if (copy_in_user(&sioc->number, &sioc32->number, 2 * sizeof(int)) ||
2969 get_user(data, &sioc32->arg))
2970 return -EFAULT;
2971 datap = compat_ptr(data);
2972 if (put_user(datap, &sioc->arg))
2973 return -EFAULT;
2974
Arnd Bergmann6b960182009-11-06 23:10:54 -08002975 err = sock_do_ioctl(net, sock, cmd, (unsigned long) sioc);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002976
2977 if (!err) {
2978 if (copy_in_user(&sioc32->length, &sioc->length,
2979 sizeof(int)))
2980 err = -EFAULT;
2981 }
2982 return err;
2983}
2984
Arnd Bergmann6b960182009-11-06 23:10:54 -08002985static int do_atm_ioctl(struct net *net, struct socket *sock,
2986 unsigned int cmd32, unsigned long arg)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002987{
2988 int i;
2989 unsigned int cmd = 0;
2990
2991 switch (cmd32) {
2992 case SONET_GETSTAT:
2993 case SONET_GETSTATZ:
2994 case SONET_GETDIAG:
2995 case SONET_SETDIAG:
2996 case SONET_CLRDIAG:
2997 case SONET_SETFRAMING:
2998 case SONET_GETFRAMING:
2999 case SONET_GETFRSENSE:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003000 return do_atmif_sioc(net, sock, cmd32, arg);
Arnd Bergmann7a229382009-11-06 23:00:29 -08003001 }
3002
3003 for (i = 0; i < NR_ATM_IOCTL; i++) {
3004 if (cmd32 == atm_ioctl_map[i].cmd32) {
3005 cmd = atm_ioctl_map[i].cmd;
3006 break;
3007 }
3008 }
3009 if (i == NR_ATM_IOCTL)
3010 return -EINVAL;
3011
3012 switch (cmd) {
3013 case ATM_GETNAMES:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003014 return do_atm_iobuf(net, sock, cmd, arg);
Arnd Bergmann7a229382009-11-06 23:00:29 -08003015
3016 case ATM_GETLINKRATE:
3017 case ATM_GETTYPE:
3018 case ATM_GETESI:
3019 case ATM_GETADDR:
3020 case ATM_RSTADDR:
3021 case ATM_ADDADDR:
3022 case ATM_DELADDR:
3023 case ATM_GETCIRANGE:
3024 case ATM_SETCIRANGE:
3025 case ATM_SETESI:
3026 case ATM_SETESIF:
3027 case ATM_GETSTAT:
3028 case ATM_GETSTATZ:
3029 case ATM_GETLOOP:
3030 case ATM_SETLOOP:
3031 case ATM_QUERYLOOP:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003032 return do_atmif_sioc(net, sock, cmd, arg);
Arnd Bergmann7a229382009-11-06 23:00:29 -08003033 }
3034
3035 return -EINVAL;
3036}
3037
Arnd Bergmann6b960182009-11-06 23:10:54 -08003038static int compat_sock_ioctl_trans(struct file *file, struct socket *sock,
3039 unsigned int cmd, unsigned long arg)
3040{
3041 void __user *argp = compat_ptr(arg);
3042 struct sock *sk = sock->sk;
3043 struct net *net = sock_net(sk);
Arnd Bergmann7a229382009-11-06 23:00:29 -08003044
Arnd Bergmann6b960182009-11-06 23:10:54 -08003045 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15))
3046 return siocdevprivate_ioctl(net, cmd, argp);
Arnd Bergmann7a229382009-11-06 23:00:29 -08003047
Arnd Bergmann6b960182009-11-06 23:10:54 -08003048 switch (cmd) {
3049 case SIOCSIFBR:
3050 case SIOCGIFBR:
3051 return old_bridge_ioctl(argp);
3052 case SIOCGIFNAME:
3053 return dev_ifname32(net, argp);
3054 case SIOCGIFCONF:
3055 return dev_ifconf(net, argp);
3056 case SIOCETHTOOL:
3057 return ethtool_ioctl(net, argp);
3058 case SIOCBONDENSLAVE:
3059 case SIOCBONDRELEASE:
3060 case SIOCBONDSETHWADDR:
3061 case SIOCBONDSLAVEINFOQUERY:
3062 case SIOCBONDINFOQUERY:
3063 case SIOCBONDCHANGEACTIVE:
3064 return bond_ioctl(net, cmd, argp);
3065 case SIOCADDRT:
3066 case SIOCDELRT:
3067 return routing_ioctl(net, sock, cmd, argp);
3068 case SIOCGSTAMP:
3069 return do_siocgstamp(net, sock, cmd, argp);
3070 case SIOCGSTAMPNS:
3071 return do_siocgstampns(net, sock, cmd, argp);
3072/* Note SIOCRTMSG is no longer, so this is safe and
3073 * the user would have seen just an -EINVAL anyways. */
3074 case SIOCRTMSG:
3075 case SIOCGIFCOUNT:
3076 return -EINVAL;
Arnd Bergmann7a229382009-11-06 23:00:29 -08003077
Arnd Bergmann6b960182009-11-06 23:10:54 -08003078 case FIOSETOWN:
3079 case SIOCSPGRP:
3080 case FIOGETOWN:
3081 case SIOCGPGRP:
3082 case SIOCBRADDBR:
3083 case SIOCBRDELBR:
3084 case SIOCGIFVLAN:
3085 case SIOCSIFVLAN:
3086 case SIOCADDDLCI:
3087 case SIOCDELDLCI:
3088 return sock_ioctl(file, cmd, arg);
3089
3090 case SIOCGIFFLAGS:
3091 case SIOCSIFFLAGS:
3092 case SIOCGIFMETRIC:
3093 case SIOCSIFMETRIC:
3094 case SIOCGIFMTU:
3095 case SIOCSIFMTU:
3096 case SIOCGIFMEM:
3097 case SIOCSIFMEM:
3098 case SIOCGIFHWADDR:
3099 case SIOCSIFHWADDR:
3100 case SIOCADDMULTI:
3101 case SIOCDELMULTI:
3102 case SIOCGIFINDEX:
3103 case SIOCGIFMAP:
3104 case SIOCSIFMAP:
3105 case SIOCGIFADDR:
3106 case SIOCSIFADDR:
3107 case SIOCSIFHWBROADCAST:
3108 case SIOCSHWTSTAMP:
3109 case SIOCDIFADDR:
3110/* case SIOCSARP: duplicate */
3111/* case SIOCDARP: duplicate */
3112 case SIOCGIFBRDADDR:
3113 case SIOCSIFBRDADDR:
3114 case SIOCGIFDSTADDR:
3115 case SIOCSIFDSTADDR:
3116 case SIOCGIFNETMASK:
3117 case SIOCSIFNETMASK:
3118 case SIOCSIFPFLAGS:
3119 case SIOCGIFPFLAGS:
3120 case SIOCGIFTXQLEN:
3121 case SIOCSIFTXQLEN:
3122 case SIOCBRADDIF:
3123 case SIOCBRDELIF:
3124 return dev_ifsioc(net, sock, cmd, argp);
3125 case ATM_GETLINKRATE32:
3126 case ATM_GETNAMES32:
3127 case ATM_GETTYPE32:
3128 case ATM_GETESI32:
3129 case ATM_GETADDR32:
3130 case ATM_RSTADDR32:
3131 case ATM_ADDADDR32:
3132 case ATM_DELADDR32:
3133 case ATM_GETCIRANGE32:
3134 case ATM_SETCIRANGE32:
3135 case ATM_SETESI32:
3136 case ATM_SETESIF32:
3137 case ATM_GETSTAT32:
3138 case ATM_GETSTATZ32:
3139 case ATM_GETLOOP32:
3140 case ATM_SETLOOP32:
3141 case ATM_QUERYLOOP32:
3142 case SONET_GETSTAT:
3143 case SONET_GETSTATZ:
3144 case SONET_GETDIAG:
3145 case SONET_SETDIAG:
3146 case SONET_CLRDIAG:
3147 case SONET_SETFRAMING:
3148 case SONET_GETFRAMING:
3149 case SONET_GETFRSENSE:
3150 return do_atm_ioctl(net, sock, cmd, arg);
3151
3152 case ATMSIGD_CTRL:
3153 case ATMARPD_CTRL:
3154 case ATMLEC_CTRL:
3155 case ATMLEC_MCAST:
3156 case ATMLEC_DATA:
3157 case ATM_SETSC:
3158 case SIOCSIFATMTCP:
3159 case SIOCMKCLIP:
3160 case ATMARP_MKIP:
3161 case ATMARP_SETENTRY:
3162 case ATMARP_ENCAP:
3163 case ATMTCP_CREATE:
3164 case ATMTCP_REMOVE:
3165 case ATMMPC_CTRL:
3166 case ATMMPC_DATA:
3167
3168 case SIOCSARP:
3169 case SIOCGARP:
3170 case SIOCDARP:
3171
3172 case SIOCATMARK:
3173 case SIOCSIFLINK:
3174 case SIOCSIFNAME:
3175 case SIOCSRARP:
3176 case SIOCGRARP:
3177 case SIOCDRARP:
3178 case SIOCGMIIPHY:
3179 case SIOCGMIIREG:
3180 case SIOCSMIIREG:
3181 return sock_do_ioctl(net, sock, cmd, arg);
3182 }
3183
3184 return -ENOIOCTLCMD;
3185}
Arnd Bergmann7a229382009-11-06 23:00:29 -08003186
Shaun Pereira89bbfc92006-03-21 23:58:08 -08003187static long compat_sock_ioctl(struct file *file, unsigned cmd,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07003188 unsigned long arg)
Shaun Pereira89bbfc92006-03-21 23:58:08 -08003189{
3190 struct socket *sock = file->private_data;
3191 int ret = -ENOIOCTLCMD;
David S. Miller87de87d2008-06-03 09:14:03 -07003192 struct sock *sk;
3193 struct net *net;
3194
3195 sk = sock->sk;
3196 net = sock_net(sk);
Shaun Pereira89bbfc92006-03-21 23:58:08 -08003197
3198 if (sock->ops->compat_ioctl)
3199 ret = sock->ops->compat_ioctl(sock, cmd, arg);
3200
David S. Miller87de87d2008-06-03 09:14:03 -07003201 if (ret == -ENOIOCTLCMD &&
3202 (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST))
3203 ret = compat_wext_handle_ioctl(net, cmd, arg);
3204
Arnd Bergmann6b960182009-11-06 23:10:54 -08003205 if (ret == -ENOIOCTLCMD)
3206 ret = compat_sock_ioctl_trans(file, sock, cmd, arg);
3207
Shaun Pereira89bbfc92006-03-21 23:58:08 -08003208 return ret;
3209}
3210#endif
3211
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003212int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen)
3213{
3214 return sock->ops->bind(sock, addr, addrlen);
3215}
3216
3217int kernel_listen(struct socket *sock, int backlog)
3218{
3219 return sock->ops->listen(sock, backlog);
3220}
3221
3222int kernel_accept(struct socket *sock, struct socket **newsock, int flags)
3223{
3224 struct sock *sk = sock->sk;
3225 int err;
3226
3227 err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
3228 newsock);
3229 if (err < 0)
3230 goto done;
3231
3232 err = sock->ops->accept(sock, *newsock, flags);
3233 if (err < 0) {
3234 sock_release(*newsock);
Tony Battersbyfa8705b2007-10-10 21:09:04 -07003235 *newsock = NULL;
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003236 goto done;
3237 }
3238
3239 (*newsock)->ops = sock->ops;
Wei Yongjun1b085342008-12-18 19:35:10 -08003240 __module_get((*newsock)->ops->owner);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003241
3242done:
3243 return err;
3244}
3245
3246int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
YOSHIFUJI Hideaki4768fbc2007-02-09 23:25:31 +09003247 int flags)
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003248{
3249 return sock->ops->connect(sock, addr, addrlen, flags);
3250}
3251
3252int kernel_getsockname(struct socket *sock, struct sockaddr *addr,
3253 int *addrlen)
3254{
3255 return sock->ops->getname(sock, addr, addrlen, 0);
3256}
3257
3258int kernel_getpeername(struct socket *sock, struct sockaddr *addr,
3259 int *addrlen)
3260{
3261 return sock->ops->getname(sock, addr, addrlen, 1);
3262}
3263
3264int kernel_getsockopt(struct socket *sock, int level, int optname,
3265 char *optval, int *optlen)
3266{
3267 mm_segment_t oldfs = get_fs();
3268 int err;
3269
3270 set_fs(KERNEL_DS);
3271 if (level == SOL_SOCKET)
3272 err = sock_getsockopt(sock, level, optname, optval, optlen);
3273 else
3274 err = sock->ops->getsockopt(sock, level, optname, optval,
3275 optlen);
3276 set_fs(oldfs);
3277 return err;
3278}
3279
3280int kernel_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07003281 char *optval, unsigned int optlen)
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003282{
3283 mm_segment_t oldfs = get_fs();
3284 int err;
3285
3286 set_fs(KERNEL_DS);
3287 if (level == SOL_SOCKET)
3288 err = sock_setsockopt(sock, level, optname, optval, optlen);
3289 else
3290 err = sock->ops->setsockopt(sock, level, optname, optval,
3291 optlen);
3292 set_fs(oldfs);
3293 return err;
3294}
3295
3296int kernel_sendpage(struct socket *sock, struct page *page, int offset,
3297 size_t size, int flags)
3298{
3299 if (sock->ops->sendpage)
3300 return sock->ops->sendpage(sock, page, offset, size, flags);
3301
3302 return sock_no_sendpage(sock, page, offset, size, flags);
3303}
3304
3305int kernel_sock_ioctl(struct socket *sock, int cmd, unsigned long arg)
3306{
3307 mm_segment_t oldfs = get_fs();
3308 int err;
3309
3310 set_fs(KERNEL_DS);
3311 err = sock->ops->ioctl(sock, cmd, arg);
3312 set_fs(oldfs);
3313
3314 return err;
3315}
3316
Trond Myklebust91cf45f2007-11-12 18:10:39 -08003317int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how)
3318{
3319 return sock->ops->shutdown(sock, how);
3320}
3321
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322EXPORT_SYMBOL(sock_create);
3323EXPORT_SYMBOL(sock_create_kern);
3324EXPORT_SYMBOL(sock_create_lite);
3325EXPORT_SYMBOL(sock_map_fd);
3326EXPORT_SYMBOL(sock_recvmsg);
3327EXPORT_SYMBOL(sock_register);
3328EXPORT_SYMBOL(sock_release);
3329EXPORT_SYMBOL(sock_sendmsg);
3330EXPORT_SYMBOL(sock_unregister);
3331EXPORT_SYMBOL(sock_wake_async);
3332EXPORT_SYMBOL(sockfd_lookup);
3333EXPORT_SYMBOL(kernel_sendmsg);
3334EXPORT_SYMBOL(kernel_recvmsg);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003335EXPORT_SYMBOL(kernel_bind);
3336EXPORT_SYMBOL(kernel_listen);
3337EXPORT_SYMBOL(kernel_accept);
3338EXPORT_SYMBOL(kernel_connect);
3339EXPORT_SYMBOL(kernel_getsockname);
3340EXPORT_SYMBOL(kernel_getpeername);
3341EXPORT_SYMBOL(kernel_getsockopt);
3342EXPORT_SYMBOL(kernel_setsockopt);
3343EXPORT_SYMBOL(kernel_sendpage);
3344EXPORT_SYMBOL(kernel_sock_ioctl);
Trond Myklebust91cf45f2007-11-12 18:10:39 -08003345EXPORT_SYMBOL(kernel_sock_shutdown);