blob: a708e7f5a38cdc94a20a5779d41ee939618adcaa [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90#include <asm/uaccess.h>
91#include <asm/unistd.h>
92
93#include <net/compat.h>
David S. Miller87de87d2008-06-03 09:14:03 -070094#include <net/wext.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96#include <net/sock.h>
97#include <linux/netfilter.h>
98
Mike Chan979c1982009-01-07 11:40:42 -080099#ifdef CONFIG_UID_STAT
100#include <linux/uid_stat.h>
101#endif
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103static int sock_no_open(struct inode *irrelevant, struct file *dontcare);
Badari Pulavarty027445c2006-09-30 23:28:46 -0700104static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov,
105 unsigned long nr_segs, loff_t pos);
106static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov,
107 unsigned long nr_segs, loff_t pos);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700108static int sock_mmap(struct file *file, struct vm_area_struct *vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110static int sock_close(struct inode *inode, struct file *file);
111static unsigned int sock_poll(struct file *file,
112 struct poll_table_struct *wait);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700113static long sock_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
Shaun Pereira89bbfc92006-03-21 23:58:08 -0800114#ifdef CONFIG_COMPAT
115static long compat_sock_ioctl(struct file *file,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700116 unsigned int cmd, unsigned long arg);
Shaun Pereira89bbfc92006-03-21 23:58:08 -0800117#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118static int sock_fasync(int fd, struct file *filp, int on);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119static ssize_t sock_sendpage(struct file *file, struct page *page,
120 int offset, size_t size, loff_t *ppos, int more);
Jens Axboe9c55e012007-11-06 23:30:13 -0800121static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
122 struct pipe_inode_info *pipe, size_t len,
123 unsigned int flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125/*
126 * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
127 * in the operation structures but are done directly via the socketcall() multiplexor.
128 */
129
Arjan van de Venda7071d2007-02-12 00:55:36 -0800130static const struct file_operations socket_file_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 .owner = THIS_MODULE,
132 .llseek = no_llseek,
133 .aio_read = sock_aio_read,
134 .aio_write = sock_aio_write,
135 .poll = sock_poll,
136 .unlocked_ioctl = sock_ioctl,
Shaun Pereira89bbfc92006-03-21 23:58:08 -0800137#ifdef CONFIG_COMPAT
138 .compat_ioctl = compat_sock_ioctl,
139#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 .mmap = sock_mmap,
141 .open = sock_no_open, /* special open code to disallow open via /proc */
142 .release = sock_close,
143 .fasync = sock_fasync,
Jens Axboe5274f052006-03-30 15:15:30 +0200144 .sendpage = sock_sendpage,
145 .splice_write = generic_splice_sendpage,
Jens Axboe9c55e012007-11-06 23:30:13 -0800146 .splice_read = sock_splice_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147};
148
149/*
150 * The protocol list. Each protocol is registered in here.
151 */
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153static DEFINE_SPINLOCK(net_family_lock);
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -0700154static const struct net_proto_family *net_families[NPROTO] __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156/*
157 * Statistics counters of the socket lists
158 */
159
160static DEFINE_PER_CPU(int, sockets_in_use) = 0;
161
162/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700163 * Support routines.
164 * Move socket addresses back and forth across the kernel/user
165 * divide and look after the messy bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 */
167
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700168#define MAX_SOCK_ADDR 128 /* 108 for Unix domain -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 16 for IP, 16 for IPX,
170 24 for IPv6,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700171 about 80 for AX.25
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 must be at least one bigger than
173 the AF_UNIX size (see net/unix/af_unix.c
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700174 :unix_mkname()).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177/**
178 * move_addr_to_kernel - copy a socket address into kernel space
179 * @uaddr: Address in user space
180 * @kaddr: Address in kernel space
181 * @ulen: Length in user space
182 *
183 * The address is copied into kernel space. If the provided address is
184 * too long an error code of -EINVAL is returned. If the copy gives
185 * invalid addresses -EFAULT is returned. On a success 0 is returned.
186 */
187
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -0700188int move_addr_to_kernel(void __user *uaddr, int ulen, struct sockaddr *kaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -0700190 if (ulen < 0 || ulen > sizeof(struct sockaddr_storage))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700192 if (ulen == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 return 0;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700194 if (copy_from_user(kaddr, uaddr, ulen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 return -EFAULT;
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100196 return audit_sockaddr(ulen, kaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197}
198
199/**
200 * move_addr_to_user - copy an address to user space
201 * @kaddr: kernel space address
202 * @klen: length of address in kernel
203 * @uaddr: user space address
204 * @ulen: pointer to user length field
205 *
206 * The value pointed to by ulen on entry is the buffer length available.
207 * This is overwritten with the buffer space used. -EINVAL is returned
208 * if an overlong buffer is specified or a negative buffer size. -EFAULT
209 * is returned if either the buffer or the length field are not
210 * accessible.
211 * After copying the data up to the limit the user specifies, the true
212 * length of the data is written over the length limit the user
213 * specified. Zero is returned for a success.
214 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700215
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -0700216int move_addr_to_user(struct sockaddr *kaddr, int klen, void __user *uaddr,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700217 int __user *ulen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
219 int err;
220 int len;
221
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700222 err = get_user(len, ulen);
223 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 return err;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700225 if (len > klen)
226 len = klen;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -0700227 if (len < 0 || len > sizeof(struct sockaddr_storage))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700229 if (len) {
Steve Grubbd6fe3942006-03-30 12:20:22 -0500230 if (audit_sockaddr(klen, kaddr))
231 return -ENOMEM;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700232 if (copy_to_user(uaddr, kaddr, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 return -EFAULT;
234 }
235 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700236 * "fromlen shall refer to the value before truncation.."
237 * 1003.1g
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 */
239 return __put_user(klen, ulen);
240}
241
242#define SOCKFS_MAGIC 0x534F434B
243
Christoph Lametere18b8902006-12-06 20:33:20 -0800244static struct kmem_cache *sock_inode_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246static struct inode *sock_alloc_inode(struct super_block *sb)
247{
248 struct socket_alloc *ei;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700249
Christoph Lametere94b1762006-12-06 20:33:17 -0800250 ei = kmem_cache_alloc(sock_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 if (!ei)
252 return NULL;
253 init_waitqueue_head(&ei->socket.wait);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 ei->socket.fasync_list = NULL;
256 ei->socket.state = SS_UNCONNECTED;
257 ei->socket.flags = 0;
258 ei->socket.ops = NULL;
259 ei->socket.sk = NULL;
260 ei->socket.file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 return &ei->vfs_inode;
263}
264
265static void sock_destroy_inode(struct inode *inode)
266{
267 kmem_cache_free(sock_inode_cachep,
268 container_of(inode, struct socket_alloc, vfs_inode));
269}
270
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700271static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700273 struct socket_alloc *ei = (struct socket_alloc *)foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Christoph Lametera35afb82007-05-16 22:10:57 -0700275 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278static int init_inodecache(void)
279{
280 sock_inode_cachep = kmem_cache_create("sock_inode_cache",
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700281 sizeof(struct socket_alloc),
282 0,
283 (SLAB_HWCACHE_ALIGN |
284 SLAB_RECLAIM_ACCOUNT |
285 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +0900286 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 if (sock_inode_cachep == NULL)
288 return -ENOMEM;
289 return 0;
290}
291
292static struct super_operations sockfs_ops = {
293 .alloc_inode = sock_alloc_inode,
294 .destroy_inode =sock_destroy_inode,
295 .statfs = simple_statfs,
296};
297
David Howells454e2392006-06-23 02:02:57 -0700298static int sockfs_get_sb(struct file_system_type *fs_type,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700299 int flags, const char *dev_name, void *data,
300 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
David Howells454e2392006-06-23 02:02:57 -0700302 return get_sb_pseudo(fs_type, "socket:", &sockfs_ops, SOCKFS_MAGIC,
303 mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
305
Eric Dumazetba899662005-08-26 12:05:31 -0700306static struct vfsmount *sock_mnt __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308static struct file_system_type sock_fs_type = {
309 .name = "sockfs",
310 .get_sb = sockfs_get_sb,
311 .kill_sb = kill_anon_super,
312};
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314static int sockfs_delete_dentry(struct dentry *dentry)
315{
Eric Dumazet304e61e2006-12-06 20:38:49 -0800316 /*
317 * At creation time, we pretended this dentry was hashed
318 * (by clearing DCACHE_UNHASHED bit in d_flags)
319 * At delete time, we restore the truth : not hashed.
320 * (so that dput() can proceed correctly)
321 */
322 dentry->d_flags |= DCACHE_UNHASHED;
323 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324}
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700325
326/*
327 * sockfs_dname() is called from d_path().
328 */
329static char *sockfs_dname(struct dentry *dentry, char *buffer, int buflen)
330{
331 return dynamic_dname(dentry, buffer, buflen, "socket:[%lu]",
332 dentry->d_inode->i_ino);
333}
334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335static struct dentry_operations sockfs_dentry_operations = {
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700336 .d_delete = sockfs_delete_dentry,
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700337 .d_dname = sockfs_dname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338};
339
340/*
341 * Obtains the first available file descriptor and sets it up for use.
342 *
David S. Miller39d8c1b2006-03-20 17:13:49 -0800343 * These functions create file structures and maps them to fd space
344 * of the current process. On success it returns file descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 * and file struct implicitly stored in sock->file.
346 * Note that another thread may close file descriptor before we return
347 * from this function. We use the fact that now we do not refer
348 * to socket after mapping. If one day we will need it, this
349 * function will increment ref. count on file by 1.
350 *
351 * In any case returned fd MAY BE not valid!
352 * This race condition is unavoidable
353 * with shared fd spaces, we cannot solve it inside kernel,
354 * but we take care of internal coherence yet.
355 */
356
Ulrich Dreppera677a032008-07-23 21:29:17 -0700357static int sock_alloc_fd(struct file **filep, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
359 int fd;
David S. Miller39d8c1b2006-03-20 17:13:49 -0800360
Ulrich Dreppera677a032008-07-23 21:29:17 -0700361 fd = get_unused_fd_flags(flags);
David S. Miller39d8c1b2006-03-20 17:13:49 -0800362 if (likely(fd >= 0)) {
363 struct file *file = get_empty_filp();
364
365 *filep = file;
366 if (unlikely(!file)) {
367 put_unused_fd(fd);
368 return -ENFILE;
369 }
370 } else
371 *filep = NULL;
372 return fd;
373}
374
Ulrich Drepper77d27202008-07-23 21:29:35 -0700375static int sock_attach_fd(struct socket *sock, struct file *file, int flags)
David S. Miller39d8c1b2006-03-20 17:13:49 -0800376{
Dave Hansence8d2cd2007-10-16 23:31:13 -0700377 struct dentry *dentry;
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700378 struct qstr name = { .name = "" };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Dave Hansence8d2cd2007-10-16 23:31:13 -0700380 dentry = d_alloc(sock_mnt->mnt_sb->s_root, &name);
381 if (unlikely(!dentry))
David S. Miller39d8c1b2006-03-20 17:13:49 -0800382 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Dave Hansence8d2cd2007-10-16 23:31:13 -0700384 dentry->d_op = &sockfs_dentry_operations;
Eric Dumazet304e61e2006-12-06 20:38:49 -0800385 /*
386 * We dont want to push this dentry into global dentry hash table.
387 * We pretend dentry is already hashed, by unsetting DCACHE_UNHASHED
388 * This permits a working /proc/$pid/fd/XXX on sockets
389 */
Dave Hansence8d2cd2007-10-16 23:31:13 -0700390 dentry->d_flags &= ~DCACHE_UNHASHED;
391 d_instantiate(dentry, SOCK_INODE(sock));
David S. Miller39d8c1b2006-03-20 17:13:49 -0800392
393 sock->file = file;
Dave Hansence8d2cd2007-10-16 23:31:13 -0700394 init_file(file, sock_mnt, dentry, FMODE_READ | FMODE_WRITE,
395 &socket_file_ops);
396 SOCK_INODE(sock)->i_fop = &socket_file_ops;
Ulrich Drepper77d27202008-07-23 21:29:35 -0700397 file->f_flags = O_RDWR | (flags & O_NONBLOCK);
David S. Miller39d8c1b2006-03-20 17:13:49 -0800398 file->f_pos = 0;
399 file->private_data = sock;
400
401 return 0;
402}
403
Ulrich Dreppera677a032008-07-23 21:29:17 -0700404int sock_map_fd(struct socket *sock, int flags)
David S. Miller39d8c1b2006-03-20 17:13:49 -0800405{
406 struct file *newfile;
Ulrich Dreppera677a032008-07-23 21:29:17 -0700407 int fd = sock_alloc_fd(&newfile, flags);
David S. Miller39d8c1b2006-03-20 17:13:49 -0800408
409 if (likely(fd >= 0)) {
Ulrich Drepper77d27202008-07-23 21:29:35 -0700410 int err = sock_attach_fd(sock, newfile, flags);
David S. Miller39d8c1b2006-03-20 17:13:49 -0800411
412 if (unlikely(err < 0)) {
413 put_filp(newfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 put_unused_fd(fd);
David S. Miller39d8c1b2006-03-20 17:13:49 -0800415 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
David S. Miller39d8c1b2006-03-20 17:13:49 -0800417 fd_install(fd, newfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 return fd;
420}
421
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800422static struct socket *sock_from_file(struct file *file, int *err)
423{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800424 if (file->f_op == &socket_file_ops)
425 return file->private_data; /* set in sock_map_fd */
426
Eric Dumazet23bb80d2007-02-08 14:59:57 -0800427 *err = -ENOTSOCK;
428 return NULL;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800429}
430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431/**
432 * sockfd_lookup - Go from a file number to its socket slot
433 * @fd: file handle
434 * @err: pointer to an error code return
435 *
436 * The file handle passed in is locked and the socket it is bound
437 * too is returned. If an error occurs the err pointer is overwritten
438 * with a negative errno code and NULL is returned. The function checks
439 * for both invalid handles and passing a handle which is not a socket.
440 *
441 * On a success the socket object pointer is returned.
442 */
443
444struct socket *sockfd_lookup(int fd, int *err)
445{
446 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 struct socket *sock;
448
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700449 file = fget(fd);
450 if (!file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 *err = -EBADF;
452 return NULL;
453 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700454
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800455 sock = sock_from_file(file, err);
456 if (!sock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 fput(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 return sock;
459}
460
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800461static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed)
462{
463 struct file *file;
464 struct socket *sock;
465
Hua Zhong36725582006-04-19 15:25:02 -0700466 *err = -EBADF;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800467 file = fget_light(fd, fput_needed);
468 if (file) {
469 sock = sock_from_file(file, err);
470 if (sock)
471 return sock;
472 fput_light(file, *fput_needed);
473 }
474 return NULL;
475}
476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477/**
478 * sock_alloc - allocate a socket
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700479 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 * Allocate a new inode and socket object. The two are bound together
481 * and initialised. The socket is then returned. If we are out of inodes
482 * NULL is returned.
483 */
484
485static struct socket *sock_alloc(void)
486{
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700487 struct inode *inode;
488 struct socket *sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 inode = new_inode(sock_mnt->mnt_sb);
491 if (!inode)
492 return NULL;
493
494 sock = SOCKET_I(inode);
495
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700496 inode->i_mode = S_IFSOCK | S_IRWXUGO;
David Howells8192b0c2008-11-14 10:39:10 +1100497 inode->i_uid = current_fsuid();
498 inode->i_gid = current_fsgid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 get_cpu_var(sockets_in_use)++;
501 put_cpu_var(sockets_in_use);
502 return sock;
503}
504
505/*
506 * In theory you can't get an open on this inode, but /proc provides
507 * a back door. Remember to keep it shut otherwise you'll let the
508 * creepy crawlies in.
509 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511static int sock_no_open(struct inode *irrelevant, struct file *dontcare)
512{
513 return -ENXIO;
514}
515
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800516const struct file_operations bad_sock_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 .owner = THIS_MODULE,
518 .open = sock_no_open,
519};
520
521/**
522 * sock_release - close a socket
523 * @sock: socket to close
524 *
525 * The socket is released from the protocol stack if it has a release
526 * callback, and the inode is then released if the socket is bound to
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700527 * an inode not a file.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530void sock_release(struct socket *sock)
531{
532 if (sock->ops) {
533 struct module *owner = sock->ops->owner;
534
535 sock->ops->release(sock);
536 sock->ops = NULL;
537 module_put(owner);
538 }
539
540 if (sock->fasync_list)
541 printk(KERN_ERR "sock_release: fasync list not empty!\n");
542
543 get_cpu_var(sockets_in_use)--;
544 put_cpu_var(sockets_in_use);
545 if (!sock->file) {
546 iput(SOCK_INODE(sock));
547 return;
548 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700549 sock->file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550}
551
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700552static inline int __sock_sendmsg(struct kiocb *iocb, struct socket *sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 struct msghdr *msg, size_t size)
554{
555 struct sock_iocb *si = kiocb_to_siocb(iocb);
556 int err;
557
558 si->sock = sock;
559 si->scm = NULL;
560 si->msg = msg;
561 si->size = size;
562
563 err = security_socket_sendmsg(sock, msg, size);
564 if (err)
565 return err;
566
Mike Chan979c1982009-01-07 11:40:42 -0800567 err = sock->ops->sendmsg(iocb, sock, msg, size);
568#ifdef CONFIG_UID_STAT
569 if (err > 0)
570 update_tcp_snd(current_uid(), err);
571#endif
572 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573}
574
575int sock_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
576{
577 struct kiocb iocb;
578 struct sock_iocb siocb;
579 int ret;
580
581 init_sync_kiocb(&iocb, NULL);
582 iocb.private = &siocb;
583 ret = __sock_sendmsg(&iocb, sock, msg, size);
584 if (-EIOCBQUEUED == ret)
585 ret = wait_on_sync_kiocb(&iocb);
586 return ret;
587}
588
589int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
590 struct kvec *vec, size_t num, size_t size)
591{
592 mm_segment_t oldfs = get_fs();
593 int result;
594
595 set_fs(KERNEL_DS);
596 /*
597 * the following is safe, since for compiler definitions of kvec and
598 * iovec are identical, yielding the same in-core layout and alignment
599 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700600 msg->msg_iov = (struct iovec *)vec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 msg->msg_iovlen = num;
602 result = sock_sendmsg(sock, msg, size);
603 set_fs(oldfs);
604 return result;
605}
606
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700607/*
608 * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
609 */
610void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
611 struct sk_buff *skb)
612{
613 ktime_t kt = skb->tstamp;
614
615 if (!sock_flag(sk, SOCK_RCVTSTAMPNS)) {
616 struct timeval tv;
617 /* Race occurred between timestamp enabling and packet
618 receiving. Fill in the current time for now. */
619 if (kt.tv64 == 0)
620 kt = ktime_get_real();
621 skb->tstamp = kt;
622 tv = ktime_to_timeval(kt);
623 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMP, sizeof(tv), &tv);
624 } else {
625 struct timespec ts;
626 /* Race occurred between timestamp enabling and packet
627 receiving. Fill in the current time for now. */
628 if (kt.tv64 == 0)
629 kt = ktime_get_real();
630 skb->tstamp = kt;
631 ts = ktime_to_timespec(kt);
632 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPNS, sizeof(ts), &ts);
633 }
634}
635
Arnaldo Carvalho de Melo7c81fd82007-03-10 00:39:35 -0300636EXPORT_SYMBOL_GPL(__sock_recv_timestamp);
637
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700638static inline int __sock_recvmsg(struct kiocb *iocb, struct socket *sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 struct msghdr *msg, size_t size, int flags)
640{
641 int err;
642 struct sock_iocb *si = kiocb_to_siocb(iocb);
643
644 si->sock = sock;
645 si->scm = NULL;
646 si->msg = msg;
647 si->size = size;
648 si->flags = flags;
649
650 err = security_socket_recvmsg(sock, msg, size, flags);
651 if (err)
652 return err;
653
Mike Chan979c1982009-01-07 11:40:42 -0800654 err = sock->ops->recvmsg(iocb, sock, msg, size, flags);
655#ifdef CONFIG_UID_STAT
656 if (err > 0)
657 update_tcp_rcv(current_uid(), err);
658#endif
659 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660}
661
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700662int sock_recvmsg(struct socket *sock, struct msghdr *msg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 size_t size, int flags)
664{
665 struct kiocb iocb;
666 struct sock_iocb siocb;
667 int ret;
668
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700669 init_sync_kiocb(&iocb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 iocb.private = &siocb;
671 ret = __sock_recvmsg(&iocb, sock, msg, size, flags);
672 if (-EIOCBQUEUED == ret)
673 ret = wait_on_sync_kiocb(&iocb);
674 return ret;
675}
676
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700677int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
678 struct kvec *vec, size_t num, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
680 mm_segment_t oldfs = get_fs();
681 int result;
682
683 set_fs(KERNEL_DS);
684 /*
685 * the following is safe, since for compiler definitions of kvec and
686 * iovec are identical, yielding the same in-core layout and alignment
687 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700688 msg->msg_iov = (struct iovec *)vec, msg->msg_iovlen = num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 result = sock_recvmsg(sock, msg, size, flags);
690 set_fs(oldfs);
691 return result;
692}
693
694static void sock_aio_dtor(struct kiocb *iocb)
695{
696 kfree(iocb->private);
697}
698
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -0300699static ssize_t sock_sendpage(struct file *file, struct page *page,
700 int offset, size_t size, loff_t *ppos, int more)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
702 struct socket *sock;
703 int flags;
704
Eric Dumazetb69aee02005-09-06 14:42:45 -0700705 sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707 flags = !(file->f_flags & O_NONBLOCK) ? 0 : MSG_DONTWAIT;
708 if (more)
709 flags |= MSG_MORE;
710
Linus Torvaldsfcaf5972009-08-13 10:00:15 -0700711 return kernel_sendpage(sock, page, offset, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712}
713
Jens Axboe9c55e012007-11-06 23:30:13 -0800714static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
715 struct pipe_inode_info *pipe, size_t len,
716 unsigned int flags)
717{
718 struct socket *sock = file->private_data;
719
Rémi Denis-Courmont997b37d2008-02-15 02:35:45 -0800720 if (unlikely(!sock->ops->splice_read))
721 return -EINVAL;
722
Jens Axboe9c55e012007-11-06 23:30:13 -0800723 return sock->ops->splice_read(sock, ppos, pipe, len, flags);
724}
725
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800726static struct sock_iocb *alloc_sock_iocb(struct kiocb *iocb,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700727 struct sock_iocb *siocb)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800728{
729 if (!is_sync_kiocb(iocb)) {
730 siocb = kmalloc(sizeof(*siocb), GFP_KERNEL);
731 if (!siocb)
732 return NULL;
733 iocb->ki_dtor = sock_aio_dtor;
734 }
735
736 siocb->kiocb = iocb;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800737 iocb->private = siocb;
738 return siocb;
739}
740
741static ssize_t do_sock_read(struct msghdr *msg, struct kiocb *iocb,
Badari Pulavarty027445c2006-09-30 23:28:46 -0700742 struct file *file, const struct iovec *iov,
743 unsigned long nr_segs)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800744{
745 struct socket *sock = file->private_data;
746 size_t size = 0;
747 int i;
748
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700749 for (i = 0; i < nr_segs; i++)
750 size += iov[i].iov_len;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800751
752 msg->msg_name = NULL;
753 msg->msg_namelen = 0;
754 msg->msg_control = NULL;
755 msg->msg_controllen = 0;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700756 msg->msg_iov = (struct iovec *)iov;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800757 msg->msg_iovlen = nr_segs;
758 msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
759
760 return __sock_recvmsg(iocb, sock, msg, size, msg->msg_flags);
761}
762
Badari Pulavarty027445c2006-09-30 23:28:46 -0700763static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov,
764 unsigned long nr_segs, loff_t pos)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800765{
766 struct sock_iocb siocb, *x;
767
768 if (pos != 0)
769 return -ESPIPE;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700770
771 if (iocb->ki_left == 0) /* Match SYS5 behaviour */
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800772 return 0;
773
Badari Pulavarty027445c2006-09-30 23:28:46 -0700774
775 x = alloc_sock_iocb(iocb, &siocb);
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800776 if (!x)
777 return -ENOMEM;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700778 return do_sock_read(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800779}
780
781static ssize_t do_sock_write(struct msghdr *msg, struct kiocb *iocb,
Badari Pulavarty027445c2006-09-30 23:28:46 -0700782 struct file *file, const struct iovec *iov,
783 unsigned long nr_segs)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800784{
785 struct socket *sock = file->private_data;
786 size_t size = 0;
787 int i;
788
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700789 for (i = 0; i < nr_segs; i++)
790 size += iov[i].iov_len;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800791
792 msg->msg_name = NULL;
793 msg->msg_namelen = 0;
794 msg->msg_control = NULL;
795 msg->msg_controllen = 0;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700796 msg->msg_iov = (struct iovec *)iov;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800797 msg->msg_iovlen = nr_segs;
798 msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
799 if (sock->type == SOCK_SEQPACKET)
800 msg->msg_flags |= MSG_EOR;
801
802 return __sock_sendmsg(iocb, sock, msg, size);
803}
804
Badari Pulavarty027445c2006-09-30 23:28:46 -0700805static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov,
806 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800808 struct sock_iocb siocb, *x;
809
810 if (pos != 0)
811 return -ESPIPE;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700812
Badari Pulavarty027445c2006-09-30 23:28:46 -0700813 x = alloc_sock_iocb(iocb, &siocb);
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800814 if (!x)
815 return -ENOMEM;
816
Badari Pulavarty027445c2006-09-30 23:28:46 -0700817 return do_sock_write(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818}
819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820/*
821 * Atomic setting of ioctl hooks to avoid race
822 * with module unload.
823 */
824
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800825static DEFINE_MUTEX(br_ioctl_mutex);
Eric W. Biederman881d9662007-09-17 11:56:21 -0700826static int (*br_ioctl_hook) (struct net *, unsigned int cmd, void __user *arg) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Eric W. Biederman881d9662007-09-17 11:56:21 -0700828void brioctl_set(int (*hook) (struct net *, unsigned int, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800830 mutex_lock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 br_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800832 mutex_unlock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833}
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700834
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835EXPORT_SYMBOL(brioctl_set);
836
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800837static DEFINE_MUTEX(vlan_ioctl_mutex);
Eric W. Biederman881d9662007-09-17 11:56:21 -0700838static int (*vlan_ioctl_hook) (struct net *, void __user *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Eric W. Biederman881d9662007-09-17 11:56:21 -0700840void vlan_ioctl_set(int (*hook) (struct net *, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800842 mutex_lock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 vlan_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800844 mutex_unlock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845}
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847EXPORT_SYMBOL(vlan_ioctl_set);
848
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800849static DEFINE_MUTEX(dlci_ioctl_mutex);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700850static int (*dlci_ioctl_hook) (unsigned int, void __user *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700852void dlci_ioctl_set(int (*hook) (unsigned int, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800854 mutex_lock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 dlci_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800856 mutex_unlock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857}
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859EXPORT_SYMBOL(dlci_ioctl_set);
860
861/*
862 * With an ioctl, arg may well be a user mode pointer, but we don't know
863 * what to do with it - that's up to the protocol still.
864 */
865
866static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
867{
868 struct socket *sock;
Eric W. Biederman881d9662007-09-17 11:56:21 -0700869 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 void __user *argp = (void __user *)arg;
871 int pid, err;
Eric W. Biederman881d9662007-09-17 11:56:21 -0700872 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Eric Dumazetb69aee02005-09-06 14:42:45 -0700874 sock = file->private_data;
Eric W. Biederman881d9662007-09-17 11:56:21 -0700875 sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900876 net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
Eric W. Biederman881d9662007-09-17 11:56:21 -0700878 err = dev_ioctl(net, cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 } else
Adrian Bunkd86b5e02006-01-21 00:46:55 +0100880#ifdef CONFIG_WIRELESS_EXT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
Eric W. Biederman881d9662007-09-17 11:56:21 -0700882 err = dev_ioctl(net, cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 } else
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700884#endif /* CONFIG_WIRELESS_EXT */
885 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 case FIOSETOWN:
887 case SIOCSPGRP:
888 err = -EFAULT;
889 if (get_user(pid, (int __user *)argp))
890 break;
891 err = f_setown(sock->file, pid, 1);
892 break;
893 case FIOGETOWN:
894 case SIOCGPGRP:
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700895 err = put_user(f_getown(sock->file),
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700896 (int __user *)argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 break;
898 case SIOCGIFBR:
899 case SIOCSIFBR:
900 case SIOCBRADDBR:
901 case SIOCBRDELBR:
902 err = -ENOPKG;
903 if (!br_ioctl_hook)
904 request_module("bridge");
905
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800906 mutex_lock(&br_ioctl_mutex);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700907 if (br_ioctl_hook)
Eric W. Biederman881d9662007-09-17 11:56:21 -0700908 err = br_ioctl_hook(net, cmd, argp);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800909 mutex_unlock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 break;
911 case SIOCGIFVLAN:
912 case SIOCSIFVLAN:
913 err = -ENOPKG;
914 if (!vlan_ioctl_hook)
915 request_module("8021q");
916
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800917 mutex_lock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 if (vlan_ioctl_hook)
Eric W. Biederman881d9662007-09-17 11:56:21 -0700919 err = vlan_ioctl_hook(net, argp);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800920 mutex_unlock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 case SIOCADDDLCI:
923 case SIOCDELDLCI:
924 err = -ENOPKG;
925 if (!dlci_ioctl_hook)
926 request_module("dlci");
927
Pavel Emelyanov7512cbf2008-03-21 15:58:52 -0700928 mutex_lock(&dlci_ioctl_mutex);
929 if (dlci_ioctl_hook)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 err = dlci_ioctl_hook(cmd, argp);
Pavel Emelyanov7512cbf2008-03-21 15:58:52 -0700931 mutex_unlock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 break;
933 default:
934 err = sock->ops->ioctl(sock, cmd, arg);
Christoph Hellwigb5e5fa52006-01-03 14:18:33 -0800935
936 /*
937 * If this ioctl is unknown try to hand it down
938 * to the NIC driver.
939 */
940 if (err == -ENOIOCTLCMD)
Eric W. Biederman881d9662007-09-17 11:56:21 -0700941 err = dev_ioctl(net, cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 break;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 return err;
945}
946
947int sock_create_lite(int family, int type, int protocol, struct socket **res)
948{
949 int err;
950 struct socket *sock = NULL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700951
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 err = security_socket_create(family, type, protocol, 1);
953 if (err)
954 goto out;
955
956 sock = sock_alloc();
957 if (!sock) {
958 err = -ENOMEM;
959 goto out;
960 }
961
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 sock->type = type;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -0700963 err = security_socket_post_create(sock, family, type, protocol, 1);
964 if (err)
965 goto out_release;
966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967out:
968 *res = sock;
969 return err;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -0700970out_release:
971 sock_release(sock);
972 sock = NULL;
973 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974}
975
976/* No kernel lock held - perfect */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700977static unsigned int sock_poll(struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978{
979 struct socket *sock;
980
981 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700982 * We can't return errors to poll, so it's either yes or no.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 */
Eric Dumazetb69aee02005-09-06 14:42:45 -0700984 sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 return sock->ops->poll(file, sock, wait);
986}
987
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700988static int sock_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989{
Eric Dumazetb69aee02005-09-06 14:42:45 -0700990 struct socket *sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
992 return sock->ops->mmap(file, sock, vma);
993}
994
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -0300995static int sock_close(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996{
997 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700998 * It was possible the inode is NULL we were
999 * closing an unfinished socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 */
1001
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001002 if (!inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 printk(KERN_DEBUG "sock_close: NULL inode\n");
1004 return 0;
1005 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 sock_release(SOCKET_I(inode));
1007 return 0;
1008}
1009
1010/*
1011 * Update the socket async list
1012 *
1013 * Fasync_list locking strategy.
1014 *
1015 * 1. fasync_list is modified only under process context socket lock
1016 * i.e. under semaphore.
1017 * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
1018 * or under socket lock.
1019 * 3. fasync_list can be used from softirq context, so that
1020 * modification under socket lock have to be enhanced with
1021 * write_lock_bh(&sk->sk_callback_lock).
1022 * --ANK (990710)
1023 */
1024
1025static int sock_fasync(int fd, struct file *filp, int on)
1026{
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001027 struct fasync_struct *fa, *fna = NULL, **prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 struct socket *sock;
1029 struct sock *sk;
1030
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001031 if (on) {
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08001032 fna = kmalloc(sizeof(struct fasync_struct), GFP_KERNEL);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001033 if (fna == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 return -ENOMEM;
1035 }
1036
Eric Dumazetb69aee02005-09-06 14:42:45 -07001037 sock = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001039 sk = sock->sk;
1040 if (sk == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 kfree(fna);
1042 return -EINVAL;
1043 }
1044
1045 lock_sock(sk);
1046
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001047 prev = &(sock->fasync_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001049 for (fa = *prev; fa != NULL; prev = &fa->fa_next, fa = *prev)
1050 if (fa->fa_file == filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 break;
1052
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001053 if (on) {
1054 if (fa != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 write_lock_bh(&sk->sk_callback_lock);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001056 fa->fa_fd = fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 write_unlock_bh(&sk->sk_callback_lock);
1058
1059 kfree(fna);
1060 goto out;
1061 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001062 fna->fa_file = filp;
1063 fna->fa_fd = fd;
1064 fna->magic = FASYNC_MAGIC;
1065 fna->fa_next = sock->fasync_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 write_lock_bh(&sk->sk_callback_lock);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001067 sock->fasync_list = fna;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 write_unlock_bh(&sk->sk_callback_lock);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001069 } else {
1070 if (fa != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 write_lock_bh(&sk->sk_callback_lock);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001072 *prev = fa->fa_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 write_unlock_bh(&sk->sk_callback_lock);
1074 kfree(fa);
1075 }
1076 }
1077
1078out:
1079 release_sock(sock->sk);
1080 return 0;
1081}
1082
1083/* This function may be called only under socket lock or callback_lock */
1084
1085int sock_wake_async(struct socket *sock, int how, int band)
1086{
1087 if (!sock || !sock->fasync_list)
1088 return -1;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001089 switch (how) {
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001090 case SOCK_WAKE_WAITD:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 if (test_bit(SOCK_ASYNC_WAITDATA, &sock->flags))
1092 break;
1093 goto call_kill;
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001094 case SOCK_WAKE_SPACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags))
1096 break;
1097 /* fall through */
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001098 case SOCK_WAKE_IO:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001099call_kill:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 __kill_fasync(sock->fasync_list, SIGIO, band);
1101 break;
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001102 case SOCK_WAKE_URG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 __kill_fasync(sock->fasync_list, SIGURG, band);
1104 }
1105 return 0;
1106}
1107
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001108static int __sock_create(struct net *net, int family, int type, int protocol,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001109 struct socket **res, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110{
1111 int err;
1112 struct socket *sock;
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001113 const struct net_proto_family *pf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
1115 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001116 * Check protocol is in range
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 */
1118 if (family < 0 || family >= NPROTO)
1119 return -EAFNOSUPPORT;
1120 if (type < 0 || type >= SOCK_MAX)
1121 return -EINVAL;
1122
1123 /* Compatibility.
1124
1125 This uglymoron is moved from INET layer to here to avoid
1126 deadlock in module load.
1127 */
1128 if (family == PF_INET && type == SOCK_PACKET) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001129 static int warned;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 if (!warned) {
1131 warned = 1;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001132 printk(KERN_INFO "%s uses obsolete (PF_INET,SOCK_PACKET)\n",
1133 current->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 }
1135 family = PF_PACKET;
1136 }
1137
1138 err = security_socket_create(family, type, protocol, kern);
1139 if (err)
1140 return err;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001141
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001142 /*
1143 * Allocate the socket and allow the family to set things up. if
1144 * the protocol is 0, the family is instructed to select an appropriate
1145 * default.
1146 */
1147 sock = sock_alloc();
1148 if (!sock) {
1149 if (net_ratelimit())
1150 printk(KERN_WARNING "socket: no more sockets\n");
1151 return -ENFILE; /* Not exactly a match, but its the
1152 closest posix thing */
1153 }
1154
1155 sock->type = type;
1156
Johannes Berg95a5afc2008-10-16 15:24:51 -07001157#ifdef CONFIG_MODULES
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001158 /* Attempt to load a protocol module if the find failed.
1159 *
1160 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 * requested real, full-featured networking support upon configuration.
1162 * Otherwise module support will break!
1163 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001164 if (net_families[family] == NULL)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001165 request_module("net-pf-%d", family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166#endif
1167
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001168 rcu_read_lock();
1169 pf = rcu_dereference(net_families[family]);
1170 err = -EAFNOSUPPORT;
1171 if (!pf)
1172 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
1174 /*
1175 * We will call the ->create function, that possibly is in a loadable
1176 * module, so we have to bump that loadable module refcnt first.
1177 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001178 if (!try_module_get(pf->owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 goto out_release;
1180
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001181 /* Now protected by module ref count */
1182 rcu_read_unlock();
1183
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001184 err = pf->create(net, sock, protocol);
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001185 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 goto out_module_put;
Frank Filza79af592005-09-27 15:23:38 -07001187
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 /*
1189 * Now to bump the refcnt of the [loadable] module that owns this
1190 * socket at sock_release time we decrement its refcnt.
1191 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001192 if (!try_module_get(sock->ops->owner))
1193 goto out_module_busy;
1194
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 /*
1196 * Now that we're done with the ->create function, the [loadable]
1197 * module can have its refcnt decremented
1198 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001199 module_put(pf->owner);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001200 err = security_socket_post_create(sock, family, type, protocol, kern);
1201 if (err)
Herbert Xu3b185522007-08-15 14:46:02 -07001202 goto out_sock_release;
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001203 *res = sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001205 return 0;
1206
1207out_module_busy:
1208 err = -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209out_module_put:
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001210 sock->ops = NULL;
1211 module_put(pf->owner);
1212out_sock_release:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 sock_release(sock);
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001214 return err;
1215
1216out_release:
1217 rcu_read_unlock();
1218 goto out_sock_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219}
1220
1221int sock_create(int family, int type, int protocol, struct socket **res)
1222{
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001223 return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224}
1225
1226int sock_create_kern(int family, int type, int protocol, struct socket **res)
1227{
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001228 return __sock_create(&init_net, family, type, protocol, res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229}
1230
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001231SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232{
1233 int retval;
1234 struct socket *sock;
Ulrich Dreppera677a032008-07-23 21:29:17 -07001235 int flags;
1236
Ulrich Dreppere38b36f2008-07-23 21:29:42 -07001237 /* Check the SOCK_* constants for consistency. */
1238 BUILD_BUG_ON(SOCK_CLOEXEC != O_CLOEXEC);
1239 BUILD_BUG_ON((SOCK_MAX | SOCK_TYPE_MASK) != SOCK_TYPE_MASK);
1240 BUILD_BUG_ON(SOCK_CLOEXEC & SOCK_TYPE_MASK);
1241 BUILD_BUG_ON(SOCK_NONBLOCK & SOCK_TYPE_MASK);
1242
Ulrich Dreppera677a032008-07-23 21:29:17 -07001243 flags = type & ~SOCK_TYPE_MASK;
Ulrich Drepper77d27202008-07-23 21:29:35 -07001244 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Dreppera677a032008-07-23 21:29:17 -07001245 return -EINVAL;
1246 type &= SOCK_TYPE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001248 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1249 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1250
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 retval = sock_create(family, type, protocol, &sock);
1252 if (retval < 0)
1253 goto out;
1254
Ulrich Drepper77d27202008-07-23 21:29:35 -07001255 retval = sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 if (retval < 0)
1257 goto out_release;
1258
1259out:
1260 /* It may be already another descriptor 8) Not kernel problem. */
1261 return retval;
1262
1263out_release:
1264 sock_release(sock);
1265 return retval;
1266}
1267
1268/*
1269 * Create a pair of connected sockets.
1270 */
1271
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001272SYSCALL_DEFINE4(socketpair, int, family, int, type, int, protocol,
1273 int __user *, usockvec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274{
1275 struct socket *sock1, *sock2;
1276 int fd1, fd2, err;
Al Virodb349502007-02-07 01:48:00 -05001277 struct file *newfile1, *newfile2;
Ulrich Dreppera677a032008-07-23 21:29:17 -07001278 int flags;
1279
1280 flags = type & ~SOCK_TYPE_MASK;
Ulrich Drepper77d27202008-07-23 21:29:35 -07001281 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Dreppera677a032008-07-23 21:29:17 -07001282 return -EINVAL;
1283 type &= SOCK_TYPE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001285 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1286 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1287
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 /*
1289 * Obtain the first socket and check if the underlying protocol
1290 * supports the socketpair call.
1291 */
1292
1293 err = sock_create(family, type, protocol, &sock1);
1294 if (err < 0)
1295 goto out;
1296
1297 err = sock_create(family, type, protocol, &sock2);
1298 if (err < 0)
1299 goto out_release_1;
1300
1301 err = sock1->ops->socketpair(sock1, sock2);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001302 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 goto out_release_both;
1304
Ulrich Dreppera677a032008-07-23 21:29:17 -07001305 fd1 = sock_alloc_fd(&newfile1, flags & O_CLOEXEC);
David S. Millerbf3c23d2007-10-29 21:54:02 -07001306 if (unlikely(fd1 < 0)) {
1307 err = fd1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 goto out_release_both;
David S. Millerbf3c23d2007-10-29 21:54:02 -07001309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
Ulrich Dreppera677a032008-07-23 21:29:17 -07001311 fd2 = sock_alloc_fd(&newfile2, flags & O_CLOEXEC);
Al Virodb349502007-02-07 01:48:00 -05001312 if (unlikely(fd2 < 0)) {
David S. Millerbf3c23d2007-10-29 21:54:02 -07001313 err = fd2;
Al Virodb349502007-02-07 01:48:00 -05001314 put_filp(newfile1);
1315 put_unused_fd(fd1);
1316 goto out_release_both;
1317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
Ulrich Drepper77d27202008-07-23 21:29:35 -07001319 err = sock_attach_fd(sock1, newfile1, flags & O_NONBLOCK);
Al Virodb349502007-02-07 01:48:00 -05001320 if (unlikely(err < 0)) {
1321 goto out_fd2;
1322 }
1323
Ulrich Drepper77d27202008-07-23 21:29:35 -07001324 err = sock_attach_fd(sock2, newfile2, flags & O_NONBLOCK);
Al Virodb349502007-02-07 01:48:00 -05001325 if (unlikely(err < 0)) {
1326 fput(newfile1);
1327 goto out_fd1;
1328 }
1329
Al Viro157cf642008-12-14 04:57:47 -05001330 audit_fd_pair(fd1, fd2);
Al Virodb349502007-02-07 01:48:00 -05001331 fd_install(fd1, newfile1);
1332 fd_install(fd2, newfile2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 /* fd1 and fd2 may be already another descriptors.
1334 * Not kernel problem.
1335 */
1336
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001337 err = put_user(fd1, &usockvec[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 if (!err)
1339 err = put_user(fd2, &usockvec[1]);
1340 if (!err)
1341 return 0;
1342
1343 sys_close(fd2);
1344 sys_close(fd1);
1345 return err;
1346
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347out_release_both:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001348 sock_release(sock2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349out_release_1:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001350 sock_release(sock1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351out:
1352 return err;
Al Virodb349502007-02-07 01:48:00 -05001353
1354out_fd2:
1355 put_filp(newfile1);
1356 sock_release(sock1);
1357out_fd1:
1358 put_filp(newfile2);
1359 sock_release(sock2);
Al Virodb349502007-02-07 01:48:00 -05001360 put_unused_fd(fd1);
1361 put_unused_fd(fd2);
1362 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363}
1364
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365/*
1366 * Bind a name to a socket. Nothing much to do here since it's
1367 * the protocol's responsibility to handle the local address.
1368 *
1369 * We move the socket address to kernel space before we call
1370 * the protocol layer (having also checked the address is ok).
1371 */
1372
Heiko Carstens20f37032009-01-14 14:14:23 +01001373SYSCALL_DEFINE3(bind, int, fd, struct sockaddr __user *, umyaddr, int, addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374{
1375 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001376 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001377 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001379 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001380 if (sock) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001381 err = move_addr_to_kernel(umyaddr, addrlen, (struct sockaddr *)&address);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001382 if (err >= 0) {
1383 err = security_socket_bind(sock,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001384 (struct sockaddr *)&address,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001385 addrlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001386 if (!err)
1387 err = sock->ops->bind(sock,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001388 (struct sockaddr *)
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001389 &address, addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 }
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001391 fput_light(sock->file, fput_needed);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 return err;
1394}
1395
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396/*
1397 * Perform a listen. Basically, we allow the protocol to do anything
1398 * necessary for a listen, and if that works, we mark the socket as
1399 * ready for listening.
1400 */
1401
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001402SYSCALL_DEFINE2(listen, int, fd, int, backlog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403{
1404 struct socket *sock;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001405 int err, fput_needed;
Pavel Emelyanovb8e1f9b2007-12-08 00:12:33 -08001406 int somaxconn;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001407
1408 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1409 if (sock) {
Pavel Emelyanov8efa6e92008-03-31 19:41:14 -07001410 somaxconn = sock_net(sock->sk)->core.sysctl_somaxconn;
Pavel Emelyanovb8e1f9b2007-12-08 00:12:33 -08001411 if ((unsigned)backlog > somaxconn)
1412 backlog = somaxconn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
1414 err = security_socket_listen(sock, backlog);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001415 if (!err)
1416 err = sock->ops->listen(sock, backlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001418 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 }
1420 return err;
1421}
1422
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423/*
1424 * For accept, we attempt to create a new socket, set up the link
1425 * with the client, wake up the client, then return the new
1426 * connected fd. We collect the address of the connector in kernel
1427 * space and move it to user at the very end. This is unclean because
1428 * we open the socket then return an error.
1429 *
1430 * 1003.1g adds the ability to recvmsg() to query connection pending
1431 * status to recvmsg. We need to add that support in a way thats
1432 * clean when we restucture accept also.
1433 */
1434
Heiko Carstens20f37032009-01-14 14:14:23 +01001435SYSCALL_DEFINE4(accept4, int, fd, struct sockaddr __user *, upeer_sockaddr,
1436 int __user *, upeer_addrlen, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437{
1438 struct socket *sock, *newsock;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001439 struct file *newfile;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001440 int err, len, newfd, fput_needed;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001441 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
Ulrich Drepper77d27202008-07-23 21:29:35 -07001443 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001444 return -EINVAL;
1445
1446 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1447 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1448
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001449 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 if (!sock)
1451 goto out;
1452
1453 err = -ENFILE;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001454 if (!(newsock = sock_alloc()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 goto out_put;
1456
1457 newsock->type = sock->type;
1458 newsock->ops = sock->ops;
1459
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 /*
1461 * We don't need try_module_get here, as the listening socket (sock)
1462 * has the protocol module (sock->ops->owner) held.
1463 */
1464 __module_get(newsock->ops->owner);
1465
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001466 newfd = sock_alloc_fd(&newfile, flags & O_CLOEXEC);
David S. Miller39d8c1b2006-03-20 17:13:49 -08001467 if (unlikely(newfd < 0)) {
1468 err = newfd;
David S. Miller9a1875e2006-04-01 12:48:36 -08001469 sock_release(newsock);
1470 goto out_put;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001471 }
1472
Ulrich Drepper77d27202008-07-23 21:29:35 -07001473 err = sock_attach_fd(newsock, newfile, flags & O_NONBLOCK);
David S. Miller39d8c1b2006-03-20 17:13:49 -08001474 if (err < 0)
Alexey Dobriyan79f4f642007-03-26 14:09:52 -07001475 goto out_fd_simple;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001476
Frank Filza79af592005-09-27 15:23:38 -07001477 err = security_socket_accept(sock, newsock);
1478 if (err)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001479 goto out_fd;
Frank Filza79af592005-09-27 15:23:38 -07001480
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 err = sock->ops->accept(sock, newsock, sock->file->f_flags);
1482 if (err < 0)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001483 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
1485 if (upeer_sockaddr) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001486 if (newsock->ops->getname(newsock, (struct sockaddr *)&address,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001487 &len, 2) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 err = -ECONNABORTED;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001489 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 }
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001491 err = move_addr_to_user((struct sockaddr *)&address,
1492 len, upeer_sockaddr, upeer_addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 if (err < 0)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001494 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 }
1496
1497 /* File flags are not inherited via accept() unlike another OSes. */
1498
David S. Miller39d8c1b2006-03-20 17:13:49 -08001499 fd_install(newfd, newfile);
1500 err = newfd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
1502 security_socket_post_accept(sock, newsock);
1503
1504out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001505 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506out:
1507 return err;
Alexey Dobriyan79f4f642007-03-26 14:09:52 -07001508out_fd_simple:
1509 sock_release(newsock);
1510 put_filp(newfile);
1511 put_unused_fd(newfd);
1512 goto out_put;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001513out_fd:
David S. Miller9606a212006-04-01 01:00:14 -08001514 fput(newfile);
David S. Miller39d8c1b2006-03-20 17:13:49 -08001515 put_unused_fd(newfd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 goto out_put;
1517}
1518
Heiko Carstens20f37032009-01-14 14:14:23 +01001519SYSCALL_DEFINE3(accept, int, fd, struct sockaddr __user *, upeer_sockaddr,
1520 int __user *, upeer_addrlen)
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001521{
Ulrich Drepperde11def2008-11-19 15:36:14 -08001522 return sys_accept4(fd, upeer_sockaddr, upeer_addrlen, 0);
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001523}
1524
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525/*
1526 * Attempt to connect to a socket with the server address. The address
1527 * is in user space so we verify it is OK and move it to kernel space.
1528 *
1529 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
1530 * break bindings
1531 *
1532 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
1533 * other SEQPACKET protocols that take time to connect() as it doesn't
1534 * include the -EINPROGRESS status for such sockets.
1535 */
1536
Heiko Carstens20f37032009-01-14 14:14:23 +01001537SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
1538 int, addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539{
1540 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001541 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001542 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001544 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 if (!sock)
1546 goto out;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001547 err = move_addr_to_kernel(uservaddr, addrlen, (struct sockaddr *)&address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 if (err < 0)
1549 goto out_put;
1550
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001551 err =
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001552 security_socket_connect(sock, (struct sockaddr *)&address, addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 if (err)
1554 goto out_put;
1555
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001556 err = sock->ops->connect(sock, (struct sockaddr *)&address, addrlen,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 sock->file->f_flags);
1558out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001559 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560out:
1561 return err;
1562}
1563
1564/*
1565 * Get the local address ('name') of a socket object. Move the obtained
1566 * name to user space.
1567 */
1568
Heiko Carstens20f37032009-01-14 14:14:23 +01001569SYSCALL_DEFINE3(getsockname, int, fd, struct sockaddr __user *, usockaddr,
1570 int __user *, usockaddr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571{
1572 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001573 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001574 int len, err, fput_needed;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001575
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001576 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 if (!sock)
1578 goto out;
1579
1580 err = security_socket_getsockname(sock);
1581 if (err)
1582 goto out_put;
1583
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001584 err = sock->ops->getname(sock, (struct sockaddr *)&address, &len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 if (err)
1586 goto out_put;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001587 err = move_addr_to_user((struct sockaddr *)&address, len, usockaddr, usockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
1589out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001590 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591out:
1592 return err;
1593}
1594
1595/*
1596 * Get the remote address ('name') of a socket object. Move the obtained
1597 * name to user space.
1598 */
1599
Heiko Carstens20f37032009-01-14 14:14:23 +01001600SYSCALL_DEFINE3(getpeername, int, fd, struct sockaddr __user *, usockaddr,
1601 int __user *, usockaddr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602{
1603 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001604 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001605 int len, err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001607 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1608 if (sock != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 err = security_socket_getpeername(sock);
1610 if (err) {
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001611 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 return err;
1613 }
1614
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001615 err =
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001616 sock->ops->getname(sock, (struct sockaddr *)&address, &len,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001617 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 if (!err)
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001619 err = move_addr_to_user((struct sockaddr *)&address, len, usockaddr,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001620 usockaddr_len);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001621 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 }
1623 return err;
1624}
1625
1626/*
1627 * Send a datagram to a given address. We move the address into kernel
1628 * space and check the user space data area is readable before invoking
1629 * the protocol.
1630 */
1631
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001632SYSCALL_DEFINE6(sendto, int, fd, void __user *, buff, size_t, len,
1633 unsigned, flags, struct sockaddr __user *, addr,
1634 int, addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635{
1636 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001637 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 int err;
1639 struct msghdr msg;
1640 struct iovec iov;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001641 int fput_needed;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001642
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001643 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1644 if (!sock)
David S. Miller4387ff72007-02-08 15:06:08 -08001645 goto out;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001646
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001647 iov.iov_base = buff;
1648 iov.iov_len = len;
1649 msg.msg_name = NULL;
1650 msg.msg_iov = &iov;
1651 msg.msg_iovlen = 1;
1652 msg.msg_control = NULL;
1653 msg.msg_controllen = 0;
1654 msg.msg_namelen = 0;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001655 if (addr) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001656 err = move_addr_to_kernel(addr, addr_len, (struct sockaddr *)&address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 if (err < 0)
1658 goto out_put;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001659 msg.msg_name = (struct sockaddr *)&address;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001660 msg.msg_namelen = addr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 }
1662 if (sock->file->f_flags & O_NONBLOCK)
1663 flags |= MSG_DONTWAIT;
1664 msg.msg_flags = flags;
1665 err = sock_sendmsg(sock, &msg, len);
1666
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001667out_put:
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001668 fput_light(sock->file, fput_needed);
David S. Miller4387ff72007-02-08 15:06:08 -08001669out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 return err;
1671}
1672
1673/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001674 * Send a datagram down a socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 */
1676
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001677SYSCALL_DEFINE4(send, int, fd, void __user *, buff, size_t, len,
1678 unsigned, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679{
1680 return sys_sendto(fd, buff, len, flags, NULL, 0);
1681}
1682
1683/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001684 * Receive a frame from the socket and optionally record the address of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 * sender. We verify the buffers are writable and if needed move the
1686 * sender address from kernel to user space.
1687 */
1688
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001689SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, ubuf, size_t, size,
1690 unsigned, flags, struct sockaddr __user *, addr,
1691 int __user *, addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692{
1693 struct socket *sock;
1694 struct iovec iov;
1695 struct msghdr msg;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001696 struct sockaddr_storage address;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001697 int err, err2;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001698 int fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001700 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 if (!sock)
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001702 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001704 msg.msg_control = NULL;
1705 msg.msg_controllen = 0;
1706 msg.msg_iovlen = 1;
1707 msg.msg_iov = &iov;
1708 iov.iov_len = size;
1709 iov.iov_base = ubuf;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001710 msg.msg_name = (struct sockaddr *)&address;
1711 msg.msg_namelen = sizeof(address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 if (sock->file->f_flags & O_NONBLOCK)
1713 flags |= MSG_DONTWAIT;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001714 err = sock_recvmsg(sock, &msg, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001716 if (err >= 0 && addr != NULL) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001717 err2 = move_addr_to_user((struct sockaddr *)&address,
1718 msg.msg_namelen, addr, addr_len);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001719 if (err2 < 0)
1720 err = err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 }
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001722
1723 fput_light(sock->file, fput_needed);
David S. Miller4387ff72007-02-08 15:06:08 -08001724out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 return err;
1726}
1727
1728/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001729 * Receive a datagram from a socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 */
1731
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001732asmlinkage long sys_recv(int fd, void __user *ubuf, size_t size,
1733 unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734{
1735 return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
1736}
1737
1738/*
1739 * Set a socket option. Because we don't know the option lengths we have
1740 * to pass the user mode parameter for the protocols to sort out.
1741 */
1742
Heiko Carstens20f37032009-01-14 14:14:23 +01001743SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname,
1744 char __user *, optval, int, optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001746 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 struct socket *sock;
1748
1749 if (optlen < 0)
1750 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001751
1752 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1753 if (sock != NULL) {
1754 err = security_socket_setsockopt(sock, level, optname);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001755 if (err)
1756 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757
1758 if (level == SOL_SOCKET)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001759 err =
1760 sock_setsockopt(sock, level, optname, optval,
1761 optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 else
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001763 err =
1764 sock->ops->setsockopt(sock, level, optname, optval,
1765 optlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001766out_put:
1767 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 }
1769 return err;
1770}
1771
1772/*
1773 * Get a socket option. Because we don't know the option lengths we have
1774 * to pass a user mode parameter for the protocols to sort out.
1775 */
1776
Heiko Carstens20f37032009-01-14 14:14:23 +01001777SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname,
1778 char __user *, optval, int __user *, optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001780 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 struct socket *sock;
1782
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001783 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1784 if (sock != NULL) {
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001785 err = security_socket_getsockopt(sock, level, optname);
1786 if (err)
1787 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
1789 if (level == SOL_SOCKET)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001790 err =
1791 sock_getsockopt(sock, level, optname, optval,
1792 optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 else
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001794 err =
1795 sock->ops->getsockopt(sock, level, optname, optval,
1796 optlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001797out_put:
1798 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 }
1800 return err;
1801}
1802
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803/*
1804 * Shutdown a socket.
1805 */
1806
Heiko Carstens754fe8d2009-01-14 14:14:09 +01001807SYSCALL_DEFINE2(shutdown, int, fd, int, how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001809 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 struct socket *sock;
1811
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001812 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1813 if (sock != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 err = security_socket_shutdown(sock, how);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001815 if (!err)
1816 err = sock->ops->shutdown(sock, how);
1817 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 }
1819 return err;
1820}
1821
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001822/* A couple of helpful macros for getting the address of the 32/64 bit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 * fields which are the same type (int / unsigned) on our platforms.
1824 */
1825#define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
1826#define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
1827#define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
1828
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829/*
1830 * BSD sendmsg interface
1831 */
1832
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001833SYSCALL_DEFINE3(sendmsg, int, fd, struct msghdr __user *, msg, unsigned, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834{
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001835 struct compat_msghdr __user *msg_compat =
1836 (struct compat_msghdr __user *)msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001838 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
Alex Williamsonb9d717a2005-09-26 14:28:02 -07001840 unsigned char ctl[sizeof(struct cmsghdr) + 20]
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001841 __attribute__ ((aligned(sizeof(__kernel_size_t))));
1842 /* 20 is size of ipv6_pktinfo */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 unsigned char *ctl_buf = ctl;
1844 struct msghdr msg_sys;
1845 int err, ctl_len, iov_size, total_len;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001846 int fput_needed;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001847
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 err = -EFAULT;
1849 if (MSG_CMSG_COMPAT & flags) {
1850 if (get_compat_msghdr(&msg_sys, msg_compat))
1851 return -EFAULT;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001852 }
1853 else if (copy_from_user(&msg_sys, msg, sizeof(struct msghdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 return -EFAULT;
1855
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001856 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001857 if (!sock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 goto out;
1859
1860 /* do not move before msg_sys is valid */
1861 err = -EMSGSIZE;
1862 if (msg_sys.msg_iovlen > UIO_MAXIOV)
1863 goto out_put;
1864
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001865 /* Check whether to allocate the iovec area */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 err = -ENOMEM;
1867 iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
1868 if (msg_sys.msg_iovlen > UIO_FASTIOV) {
1869 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
1870 if (!iov)
1871 goto out_put;
1872 }
1873
1874 /* This will also move the address data into kernel space */
1875 if (MSG_CMSG_COMPAT & flags) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001876 err = verify_compat_iovec(&msg_sys, iov,
1877 (struct sockaddr *)&address,
1878 VERIFY_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 } else
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001880 err = verify_iovec(&msg_sys, iov,
1881 (struct sockaddr *)&address,
1882 VERIFY_READ);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001883 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 goto out_freeiov;
1885 total_len = err;
1886
1887 err = -ENOBUFS;
1888
1889 if (msg_sys.msg_controllen > INT_MAX)
1890 goto out_freeiov;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001891 ctl_len = msg_sys.msg_controllen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001893 err =
1894 cmsghdr_from_user_compat_to_kern(&msg_sys, sock->sk, ctl,
1895 sizeof(ctl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 if (err)
1897 goto out_freeiov;
1898 ctl_buf = msg_sys.msg_control;
Al Viro8920e8f2005-09-07 18:28:51 -07001899 ctl_len = msg_sys.msg_controllen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 } else if (ctl_len) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001901 if (ctl_len > sizeof(ctl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001903 if (ctl_buf == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 goto out_freeiov;
1905 }
1906 err = -EFAULT;
1907 /*
1908 * Careful! Before this, msg_sys.msg_control contains a user pointer.
1909 * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
1910 * checking falls down on this.
1911 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001912 if (copy_from_user(ctl_buf, (void __user *)msg_sys.msg_control,
1913 ctl_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 goto out_freectl;
1915 msg_sys.msg_control = ctl_buf;
1916 }
1917 msg_sys.msg_flags = flags;
1918
1919 if (sock->file->f_flags & O_NONBLOCK)
1920 msg_sys.msg_flags |= MSG_DONTWAIT;
1921 err = sock_sendmsg(sock, &msg_sys, total_len);
1922
1923out_freectl:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001924 if (ctl_buf != ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 sock_kfree_s(sock->sk, ctl_buf, ctl_len);
1926out_freeiov:
1927 if (iov != iovstack)
1928 sock_kfree_s(sock->sk, iov, iov_size);
1929out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001930 fput_light(sock->file, fput_needed);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001931out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 return err;
1933}
1934
1935/*
1936 * BSD recvmsg interface
1937 */
1938
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001939SYSCALL_DEFINE3(recvmsg, int, fd, struct msghdr __user *, msg,
1940 unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941{
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001942 struct compat_msghdr __user *msg_compat =
1943 (struct compat_msghdr __user *)msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 struct socket *sock;
1945 struct iovec iovstack[UIO_FASTIOV];
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001946 struct iovec *iov = iovstack;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 struct msghdr msg_sys;
1948 unsigned long cmsg_ptr;
1949 int err, iov_size, total_len, len;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001950 int fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
1952 /* kernel mode address */
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001953 struct sockaddr_storage addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
1955 /* user mode address pointers */
1956 struct sockaddr __user *uaddr;
1957 int __user *uaddr_len;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001958
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 if (MSG_CMSG_COMPAT & flags) {
1960 if (get_compat_msghdr(&msg_sys, msg_compat))
1961 return -EFAULT;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001962 }
1963 else if (copy_from_user(&msg_sys, msg, sizeof(struct msghdr)))
1964 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001966 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 if (!sock)
1968 goto out;
1969
1970 err = -EMSGSIZE;
1971 if (msg_sys.msg_iovlen > UIO_MAXIOV)
1972 goto out_put;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001973
1974 /* Check whether to allocate the iovec area */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 err = -ENOMEM;
1976 iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
1977 if (msg_sys.msg_iovlen > UIO_FASTIOV) {
1978 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
1979 if (!iov)
1980 goto out_put;
1981 }
1982
1983 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001984 * Save the user-mode address (verify_iovec will change the
1985 * kernel msghdr to use the kernel address space)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001987
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001988 uaddr = (__force void __user *)msg_sys.msg_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 uaddr_len = COMPAT_NAMELEN(msg);
1990 if (MSG_CMSG_COMPAT & flags) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001991 err = verify_compat_iovec(&msg_sys, iov,
1992 (struct sockaddr *)&addr,
1993 VERIFY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 } else
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001995 err = verify_iovec(&msg_sys, iov,
1996 (struct sockaddr *)&addr,
1997 VERIFY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 if (err < 0)
1999 goto out_freeiov;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002000 total_len = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
2002 cmsg_ptr = (unsigned long)msg_sys.msg_control;
Ulrich Drepper4a195422007-07-15 23:40:34 -07002003 msg_sys.msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002004
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 if (sock->file->f_flags & O_NONBLOCK)
2006 flags |= MSG_DONTWAIT;
2007 err = sock_recvmsg(sock, &msg_sys, total_len, flags);
2008 if (err < 0)
2009 goto out_freeiov;
2010 len = err;
2011
2012 if (uaddr != NULL) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07002013 err = move_addr_to_user((struct sockaddr *)&addr,
2014 msg_sys.msg_namelen, uaddr,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002015 uaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 if (err < 0)
2017 goto out_freeiov;
2018 }
David S. Miller37f7f422005-09-16 16:51:01 -07002019 err = __put_user((msg_sys.msg_flags & ~MSG_CMSG_COMPAT),
2020 COMPAT_FLAGS(msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 if (err)
2022 goto out_freeiov;
2023 if (MSG_CMSG_COMPAT & flags)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002024 err = __put_user((unsigned long)msg_sys.msg_control - cmsg_ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 &msg_compat->msg_controllen);
2026 else
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002027 err = __put_user((unsigned long)msg_sys.msg_control - cmsg_ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 &msg->msg_controllen);
2029 if (err)
2030 goto out_freeiov;
2031 err = len;
2032
2033out_freeiov:
2034 if (iov != iovstack)
2035 sock_kfree_s(sock->sk, iov, iov_size);
2036out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002037 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038out:
2039 return err;
2040}
2041
2042#ifdef __ARCH_WANT_SYS_SOCKETCALL
2043
2044/* Argument list sizes for sys_socketcall */
2045#define AL(x) ((x) * sizeof(unsigned long))
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07002046static const unsigned char nargs[19]={
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002047 AL(0),AL(3),AL(3),AL(3),AL(2),AL(3),
2048 AL(3),AL(3),AL(4),AL(4),AL(4),AL(6),
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07002049 AL(6),AL(2),AL(5),AL(5),AL(3),AL(3),
Ulrich Drepperde11def2008-11-19 15:36:14 -08002050 AL(4)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002051};
2052
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053#undef AL
2054
2055/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002056 * System call vectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 *
2058 * Argument checking cleaned up. Saved 20% in size.
2059 * This function doesn't need to set the kernel lock because
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002060 * it is set by the callees.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 */
2062
Heiko Carstens3e0fa652009-01-14 14:14:24 +01002063SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064{
2065 unsigned long a[6];
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002066 unsigned long a0, a1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 int err;
2068
Ulrich Drepperde11def2008-11-19 15:36:14 -08002069 if (call < 1 || call > SYS_ACCEPT4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 return -EINVAL;
2071
2072 /* copy_from_user should be SMP safe. */
2073 if (copy_from_user(a, args, nargs[call]))
2074 return -EFAULT;
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002075
Al Virof3298dc2008-12-10 03:16:51 -05002076 audit_socketcall(nargs[call] / sizeof(unsigned long), a);
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002077
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002078 a0 = a[0];
2079 a1 = a[1];
2080
2081 switch (call) {
2082 case SYS_SOCKET:
2083 err = sys_socket(a0, a1, a[2]);
2084 break;
2085 case SYS_BIND:
2086 err = sys_bind(a0, (struct sockaddr __user *)a1, a[2]);
2087 break;
2088 case SYS_CONNECT:
2089 err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
2090 break;
2091 case SYS_LISTEN:
2092 err = sys_listen(a0, a1);
2093 break;
2094 case SYS_ACCEPT:
Ulrich Drepperde11def2008-11-19 15:36:14 -08002095 err = sys_accept4(a0, (struct sockaddr __user *)a1,
2096 (int __user *)a[2], 0);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002097 break;
2098 case SYS_GETSOCKNAME:
2099 err =
2100 sys_getsockname(a0, (struct sockaddr __user *)a1,
2101 (int __user *)a[2]);
2102 break;
2103 case SYS_GETPEERNAME:
2104 err =
2105 sys_getpeername(a0, (struct sockaddr __user *)a1,
2106 (int __user *)a[2]);
2107 break;
2108 case SYS_SOCKETPAIR:
2109 err = sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
2110 break;
2111 case SYS_SEND:
2112 err = sys_send(a0, (void __user *)a1, a[2], a[3]);
2113 break;
2114 case SYS_SENDTO:
2115 err = sys_sendto(a0, (void __user *)a1, a[2], a[3],
2116 (struct sockaddr __user *)a[4], a[5]);
2117 break;
2118 case SYS_RECV:
2119 err = sys_recv(a0, (void __user *)a1, a[2], a[3]);
2120 break;
2121 case SYS_RECVFROM:
2122 err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
2123 (struct sockaddr __user *)a[4],
2124 (int __user *)a[5]);
2125 break;
2126 case SYS_SHUTDOWN:
2127 err = sys_shutdown(a0, a1);
2128 break;
2129 case SYS_SETSOCKOPT:
2130 err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]);
2131 break;
2132 case SYS_GETSOCKOPT:
2133 err =
2134 sys_getsockopt(a0, a1, a[2], (char __user *)a[3],
2135 (int __user *)a[4]);
2136 break;
2137 case SYS_SENDMSG:
2138 err = sys_sendmsg(a0, (struct msghdr __user *)a1, a[2]);
2139 break;
2140 case SYS_RECVMSG:
2141 err = sys_recvmsg(a0, (struct msghdr __user *)a1, a[2]);
2142 break;
Ulrich Drepperde11def2008-11-19 15:36:14 -08002143 case SYS_ACCEPT4:
2144 err = sys_accept4(a0, (struct sockaddr __user *)a1,
2145 (int __user *)a[2], a[3]);
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07002146 break;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002147 default:
2148 err = -EINVAL;
2149 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 }
2151 return err;
2152}
2153
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002154#endif /* __ARCH_WANT_SYS_SOCKETCALL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002156/**
2157 * sock_register - add a socket protocol handler
2158 * @ops: description of protocol
2159 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 * This function is called by a protocol handler that wants to
2161 * advertise its address family, and have it linked into the
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002162 * socket interface. The value ops->family coresponds to the
2163 * socket system call protocol family.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 */
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002165int sock_register(const struct net_proto_family *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166{
2167 int err;
2168
2169 if (ops->family >= NPROTO) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002170 printk(KERN_CRIT "protocol %d >= NPROTO(%d)\n", ops->family,
2171 NPROTO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 return -ENOBUFS;
2173 }
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002174
2175 spin_lock(&net_family_lock);
2176 if (net_families[ops->family])
2177 err = -EEXIST;
2178 else {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002179 net_families[ops->family] = ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 err = 0;
2181 }
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002182 spin_unlock(&net_family_lock);
2183
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002184 printk(KERN_INFO "NET: Registered protocol family %d\n", ops->family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 return err;
2186}
2187
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002188/**
2189 * sock_unregister - remove a protocol handler
2190 * @family: protocol family to remove
2191 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 * This function is called by a protocol handler that wants to
2193 * remove its address family, and have it unlinked from the
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002194 * new socket creation.
2195 *
2196 * If protocol handler is a module, then it can use module reference
2197 * counts to protect against new references. If protocol handler is not
2198 * a module then it needs to provide its own protection in
2199 * the ops->create routine.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 */
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002201void sock_unregister(int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202{
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002203 BUG_ON(family < 0 || family >= NPROTO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002205 spin_lock(&net_family_lock);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002206 net_families[family] = NULL;
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002207 spin_unlock(&net_family_lock);
2208
2209 synchronize_rcu();
2210
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002211 printk(KERN_INFO "NET: Unregistered protocol family %d\n", family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212}
2213
Andi Kleen77d76ea2005-12-22 12:43:42 -08002214static int __init sock_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215{
2216 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002217 * Initialize sock SLAB cache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002219
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 sk_init();
2221
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002223 * Initialize skbuff SLAB cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 */
2225 skb_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
2227 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002228 * Initialize the protocols module.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 */
2230
2231 init_inodecache();
2232 register_filesystem(&sock_fs_type);
2233 sock_mnt = kern_mount(&sock_fs_type);
Andi Kleen77d76ea2005-12-22 12:43:42 -08002234
2235 /* The real protocol initialization is performed in later initcalls.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 */
2237
2238#ifdef CONFIG_NETFILTER
2239 netfilter_init();
2240#endif
David S. Millercbeb3212005-12-22 12:58:55 -08002241
2242 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243}
2244
Andi Kleen77d76ea2005-12-22 12:43:42 -08002245core_initcall(sock_init); /* early initcall */
2246
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247#ifdef CONFIG_PROC_FS
2248void socket_seq_show(struct seq_file *seq)
2249{
2250 int cpu;
2251 int counter = 0;
2252
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -07002253 for_each_possible_cpu(cpu)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002254 counter += per_cpu(sockets_in_use, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
2256 /* It can be negative, by the way. 8) */
2257 if (counter < 0)
2258 counter = 0;
2259
2260 seq_printf(seq, "sockets: used %d\n", counter);
2261}
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002262#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263
Shaun Pereira89bbfc92006-03-21 23:58:08 -08002264#ifdef CONFIG_COMPAT
2265static long compat_sock_ioctl(struct file *file, unsigned cmd,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002266 unsigned long arg)
Shaun Pereira89bbfc92006-03-21 23:58:08 -08002267{
2268 struct socket *sock = file->private_data;
2269 int ret = -ENOIOCTLCMD;
David S. Miller87de87d2008-06-03 09:14:03 -07002270 struct sock *sk;
2271 struct net *net;
2272
2273 sk = sock->sk;
2274 net = sock_net(sk);
Shaun Pereira89bbfc92006-03-21 23:58:08 -08002275
2276 if (sock->ops->compat_ioctl)
2277 ret = sock->ops->compat_ioctl(sock, cmd, arg);
2278
David S. Miller87de87d2008-06-03 09:14:03 -07002279 if (ret == -ENOIOCTLCMD &&
2280 (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST))
2281 ret = compat_wext_handle_ioctl(net, cmd, arg);
2282
Shaun Pereira89bbfc92006-03-21 23:58:08 -08002283 return ret;
2284}
2285#endif
2286
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07002287int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen)
2288{
2289 return sock->ops->bind(sock, addr, addrlen);
2290}
2291
2292int kernel_listen(struct socket *sock, int backlog)
2293{
2294 return sock->ops->listen(sock, backlog);
2295}
2296
2297int kernel_accept(struct socket *sock, struct socket **newsock, int flags)
2298{
2299 struct sock *sk = sock->sk;
2300 int err;
2301
2302 err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
2303 newsock);
2304 if (err < 0)
2305 goto done;
2306
2307 err = sock->ops->accept(sock, *newsock, flags);
2308 if (err < 0) {
2309 sock_release(*newsock);
Tony Battersbyfa8705b2007-10-10 21:09:04 -07002310 *newsock = NULL;
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07002311 goto done;
2312 }
2313
2314 (*newsock)->ops = sock->ops;
Wei Yongjun1b085342008-12-18 19:35:10 -08002315 __module_get((*newsock)->ops->owner);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07002316
2317done:
2318 return err;
2319}
2320
2321int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
YOSHIFUJI Hideaki4768fbc2007-02-09 23:25:31 +09002322 int flags)
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07002323{
2324 return sock->ops->connect(sock, addr, addrlen, flags);
2325}
2326
2327int kernel_getsockname(struct socket *sock, struct sockaddr *addr,
2328 int *addrlen)
2329{
2330 return sock->ops->getname(sock, addr, addrlen, 0);
2331}
2332
2333int kernel_getpeername(struct socket *sock, struct sockaddr *addr,
2334 int *addrlen)
2335{
2336 return sock->ops->getname(sock, addr, addrlen, 1);
2337}
2338
2339int kernel_getsockopt(struct socket *sock, int level, int optname,
2340 char *optval, int *optlen)
2341{
2342 mm_segment_t oldfs = get_fs();
2343 int err;
2344
2345 set_fs(KERNEL_DS);
2346 if (level == SOL_SOCKET)
2347 err = sock_getsockopt(sock, level, optname, optval, optlen);
2348 else
2349 err = sock->ops->getsockopt(sock, level, optname, optval,
2350 optlen);
2351 set_fs(oldfs);
2352 return err;
2353}
2354
2355int kernel_setsockopt(struct socket *sock, int level, int optname,
2356 char *optval, int optlen)
2357{
2358 mm_segment_t oldfs = get_fs();
2359 int err;
2360
2361 set_fs(KERNEL_DS);
2362 if (level == SOL_SOCKET)
2363 err = sock_setsockopt(sock, level, optname, optval, optlen);
2364 else
2365 err = sock->ops->setsockopt(sock, level, optname, optval,
2366 optlen);
2367 set_fs(oldfs);
2368 return err;
2369}
2370
2371int kernel_sendpage(struct socket *sock, struct page *page, int offset,
2372 size_t size, int flags)
2373{
2374 if (sock->ops->sendpage)
2375 return sock->ops->sendpage(sock, page, offset, size, flags);
2376
2377 return sock_no_sendpage(sock, page, offset, size, flags);
2378}
2379
2380int kernel_sock_ioctl(struct socket *sock, int cmd, unsigned long arg)
2381{
2382 mm_segment_t oldfs = get_fs();
2383 int err;
2384
2385 set_fs(KERNEL_DS);
2386 err = sock->ops->ioctl(sock, cmd, arg);
2387 set_fs(oldfs);
2388
2389 return err;
2390}
2391
Trond Myklebust91cf45f2007-11-12 18:10:39 -08002392int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how)
2393{
2394 return sock->ops->shutdown(sock, how);
2395}
2396
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397EXPORT_SYMBOL(sock_create);
2398EXPORT_SYMBOL(sock_create_kern);
2399EXPORT_SYMBOL(sock_create_lite);
2400EXPORT_SYMBOL(sock_map_fd);
2401EXPORT_SYMBOL(sock_recvmsg);
2402EXPORT_SYMBOL(sock_register);
2403EXPORT_SYMBOL(sock_release);
2404EXPORT_SYMBOL(sock_sendmsg);
2405EXPORT_SYMBOL(sock_unregister);
2406EXPORT_SYMBOL(sock_wake_async);
2407EXPORT_SYMBOL(sockfd_lookup);
2408EXPORT_SYMBOL(kernel_sendmsg);
2409EXPORT_SYMBOL(kernel_recvmsg);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07002410EXPORT_SYMBOL(kernel_bind);
2411EXPORT_SYMBOL(kernel_listen);
2412EXPORT_SYMBOL(kernel_accept);
2413EXPORT_SYMBOL(kernel_connect);
2414EXPORT_SYMBOL(kernel_getsockname);
2415EXPORT_SYMBOL(kernel_getpeername);
2416EXPORT_SYMBOL(kernel_getsockopt);
2417EXPORT_SYMBOL(kernel_setsockopt);
2418EXPORT_SYMBOL(kernel_sendpage);
2419EXPORT_SYMBOL(kernel_sock_ioctl);
Trond Myklebust91cf45f2007-11-12 18:10:39 -08002420EXPORT_SYMBOL(kernel_sock_shutdown);