blob: 807935693846661cf182bf7e777b6cc77b48dde6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * NET An implementation of the SOCKET network access protocol.
3 *
4 * Version: @(#)socket.c 1.1.93 18/02/95
5 *
6 * Authors: Orest Zborowski, <obz@Kodak.COM>
Jesper Juhl02c30a82005-05-05 16:16:16 -07007 * Ross Biro
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
9 *
10 * Fixes:
11 * Anonymous : NOTSOCK/BADF cleanup. Error fix in
12 * shutdown()
13 * Alan Cox : verify_area() fixes
14 * Alan Cox : Removed DDI
15 * Jonathan Kamens : SOCK_DGRAM reconnect bug
16 * Alan Cox : Moved a load of checks to the very
17 * top level.
18 * Alan Cox : Move address structures to/from user
19 * mode above the protocol layers.
20 * Rob Janssen : Allow 0 length sends.
21 * Alan Cox : Asynchronous I/O support (cribbed from the
22 * tty drivers).
23 * Niibe Yutaka : Asynchronous I/O for writes (4.4BSD style)
24 * Jeff Uphoff : Made max number of sockets command-line
25 * configurable.
26 * Matti Aarnio : Made the number of sockets dynamic,
27 * to be allocated when needed, and mr.
28 * Uphoff's max is used as max to be
29 * allowed to allocate.
30 * Linus : Argh. removed all the socket allocation
31 * altogether: it's in the inode now.
32 * Alan Cox : Made sock_alloc()/sock_release() public
33 * for NetROM and future kernel nfsd type
34 * stuff.
35 * Alan Cox : sendmsg/recvmsg basics.
36 * Tom Dyas : Export net symbols.
37 * Marcin Dalecki : Fixed problems with CONFIG_NET="n".
38 * Alan Cox : Added thread locking to sys_* calls
39 * for sockets. May have errors at the
40 * moment.
41 * Kevin Buhr : Fixed the dumb errors in the above.
42 * Andi Kleen : Some small cleanups, optimizations,
43 * and fixed a copy_from_user() bug.
44 * Tigran Aivazian : sys_send(args) calls sys_sendto(args, NULL, 0)
Stephen Hemminger89bddce2006-09-01 00:19:31 -070045 * Tigran Aivazian : Made listen(2) backlog sanity checks
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 * protocol-independent
47 *
48 *
49 * This program is free software; you can redistribute it and/or
50 * modify it under the terms of the GNU General Public License
51 * as published by the Free Software Foundation; either version
52 * 2 of the License, or (at your option) any later version.
53 *
54 *
55 * This module is effectively the top level interface to the BSD socket
Stephen Hemminger89bddce2006-09-01 00:19:31 -070056 * paradigm.
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 *
58 * Based upon Swansea University Computer Society NET3.039
59 */
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <linux/socket.h>
63#include <linux/file.h>
64#include <linux/net.h>
65#include <linux/interrupt.h>
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -070066#include <linux/thread_info.h>
Stephen Hemminger55737fd2006-09-01 00:23:39 -070067#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/netdevice.h>
69#include <linux/proc_fs.h>
70#include <linux/seq_file.h>
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080071#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include <linux/wanrouter.h>
73#include <linux/if_bridge.h>
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -030074#include <linux/if_frad.h>
75#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#include <linux/init.h>
77#include <linux/poll.h>
78#include <linux/cache.h>
79#include <linux/module.h>
80#include <linux/highmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#include <linux/mount.h>
82#include <linux/security.h>
83#include <linux/syscalls.h>
84#include <linux/compat.h>
85#include <linux/kmod.h>
David Woodhouse3ec3b2f2005-05-17 12:08:48 +010086#include <linux/audit.h>
Adrian Bunkd86b5e02006-01-21 00:46:55 +010087#include <linux/wireless.h>
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -070088#include <linux/nsproxy.h>
Nick Black1fd7317d2009-09-22 16:43:33 -070089#include <linux/magic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91#include <asm/uaccess.h>
92#include <asm/unistd.h>
93
94#include <net/compat.h>
David S. Miller87de87d2008-06-03 09:14:03 -070095#include <net/wext.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97#include <net/sock.h>
98#include <linux/netfilter.h>
99
100static int sock_no_open(struct inode *irrelevant, struct file *dontcare);
Badari Pulavarty027445c2006-09-30 23:28:46 -0700101static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov,
102 unsigned long nr_segs, loff_t pos);
103static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov,
104 unsigned long nr_segs, loff_t pos);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700105static int sock_mmap(struct file *file, struct vm_area_struct *vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107static int sock_close(struct inode *inode, struct file *file);
108static unsigned int sock_poll(struct file *file,
109 struct poll_table_struct *wait);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700110static long sock_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
Shaun Pereira89bbfc92006-03-21 23:58:08 -0800111#ifdef CONFIG_COMPAT
112static long compat_sock_ioctl(struct file *file,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700113 unsigned int cmd, unsigned long arg);
Shaun Pereira89bbfc92006-03-21 23:58:08 -0800114#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115static int sock_fasync(int fd, struct file *filp, int on);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116static ssize_t sock_sendpage(struct file *file, struct page *page,
117 int offset, size_t size, loff_t *ppos, int more);
Jens Axboe9c55e012007-11-06 23:30:13 -0800118static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
119 struct pipe_inode_info *pipe, size_t len,
120 unsigned int flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122/*
123 * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
124 * in the operation structures but are done directly via the socketcall() multiplexor.
125 */
126
Arjan van de Venda7071d2007-02-12 00:55:36 -0800127static const struct file_operations socket_file_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 .owner = THIS_MODULE,
129 .llseek = no_llseek,
130 .aio_read = sock_aio_read,
131 .aio_write = sock_aio_write,
132 .poll = sock_poll,
133 .unlocked_ioctl = sock_ioctl,
Shaun Pereira89bbfc92006-03-21 23:58:08 -0800134#ifdef CONFIG_COMPAT
135 .compat_ioctl = compat_sock_ioctl,
136#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 .mmap = sock_mmap,
138 .open = sock_no_open, /* special open code to disallow open via /proc */
139 .release = sock_close,
140 .fasync = sock_fasync,
Jens Axboe5274f052006-03-30 15:15:30 +0200141 .sendpage = sock_sendpage,
142 .splice_write = generic_splice_sendpage,
Jens Axboe9c55e012007-11-06 23:30:13 -0800143 .splice_read = sock_splice_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144};
145
146/*
147 * The protocol list. Each protocol is registered in here.
148 */
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150static DEFINE_SPINLOCK(net_family_lock);
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -0700151static const struct net_proto_family *net_families[NPROTO] __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153/*
154 * Statistics counters of the socket lists
155 */
156
157static DEFINE_PER_CPU(int, sockets_in_use) = 0;
158
159/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700160 * Support routines.
161 * Move socket addresses back and forth across the kernel/user
162 * divide and look after the messy bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 */
164
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700165#define MAX_SOCK_ADDR 128 /* 108 for Unix domain -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 16 for IP, 16 for IPX,
167 24 for IPv6,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700168 about 80 for AX.25
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 must be at least one bigger than
170 the AF_UNIX size (see net/unix/af_unix.c
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700171 :unix_mkname()).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174/**
175 * move_addr_to_kernel - copy a socket address into kernel space
176 * @uaddr: Address in user space
177 * @kaddr: Address in kernel space
178 * @ulen: Length in user space
179 *
180 * The address is copied into kernel space. If the provided address is
181 * too long an error code of -EINVAL is returned. If the copy gives
182 * invalid addresses -EFAULT is returned. On a success 0 is returned.
183 */
184
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -0700185int move_addr_to_kernel(void __user *uaddr, int ulen, struct sockaddr *kaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -0700187 if (ulen < 0 || ulen > sizeof(struct sockaddr_storage))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700189 if (ulen == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 return 0;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700191 if (copy_from_user(kaddr, uaddr, ulen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 return -EFAULT;
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100193 return audit_sockaddr(ulen, kaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
196/**
197 * move_addr_to_user - copy an address to user space
198 * @kaddr: kernel space address
199 * @klen: length of address in kernel
200 * @uaddr: user space address
201 * @ulen: pointer to user length field
202 *
203 * The value pointed to by ulen on entry is the buffer length available.
204 * This is overwritten with the buffer space used. -EINVAL is returned
205 * if an overlong buffer is specified or a negative buffer size. -EFAULT
206 * is returned if either the buffer or the length field are not
207 * accessible.
208 * After copying the data up to the limit the user specifies, the true
209 * length of the data is written over the length limit the user
210 * specified. Zero is returned for a success.
211 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700212
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -0700213int move_addr_to_user(struct sockaddr *kaddr, int klen, void __user *uaddr,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700214 int __user *ulen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
216 int err;
217 int len;
218
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700219 err = get_user(len, ulen);
220 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 return err;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700222 if (len > klen)
223 len = klen;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -0700224 if (len < 0 || len > sizeof(struct sockaddr_storage))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700226 if (len) {
Steve Grubbd6fe3942006-03-30 12:20:22 -0500227 if (audit_sockaddr(klen, kaddr))
228 return -ENOMEM;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700229 if (copy_to_user(uaddr, kaddr, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 return -EFAULT;
231 }
232 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700233 * "fromlen shall refer to the value before truncation.."
234 * 1003.1g
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 */
236 return __put_user(klen, ulen);
237}
238
Christoph Lametere18b8902006-12-06 20:33:20 -0800239static struct kmem_cache *sock_inode_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241static struct inode *sock_alloc_inode(struct super_block *sb)
242{
243 struct socket_alloc *ei;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700244
Christoph Lametere94b1762006-12-06 20:33:17 -0800245 ei = kmem_cache_alloc(sock_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 if (!ei)
247 return NULL;
248 init_waitqueue_head(&ei->socket.wait);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 ei->socket.fasync_list = NULL;
251 ei->socket.state = SS_UNCONNECTED;
252 ei->socket.flags = 0;
253 ei->socket.ops = NULL;
254 ei->socket.sk = NULL;
255 ei->socket.file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 return &ei->vfs_inode;
258}
259
260static void sock_destroy_inode(struct inode *inode)
261{
262 kmem_cache_free(sock_inode_cachep,
263 container_of(inode, struct socket_alloc, vfs_inode));
264}
265
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700266static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700268 struct socket_alloc *ei = (struct socket_alloc *)foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Christoph Lametera35afb82007-05-16 22:10:57 -0700270 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273static int init_inodecache(void)
274{
275 sock_inode_cachep = kmem_cache_create("sock_inode_cache",
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700276 sizeof(struct socket_alloc),
277 0,
278 (SLAB_HWCACHE_ALIGN |
279 SLAB_RECLAIM_ACCOUNT |
280 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +0900281 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (sock_inode_cachep == NULL)
283 return -ENOMEM;
284 return 0;
285}
286
Alexey Dobriyanb87221d2009-09-21 17:01:09 -0700287static const struct super_operations sockfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 .alloc_inode = sock_alloc_inode,
289 .destroy_inode =sock_destroy_inode,
290 .statfs = simple_statfs,
291};
292
David Howells454e2392006-06-23 02:02:57 -0700293static int sockfs_get_sb(struct file_system_type *fs_type,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700294 int flags, const char *dev_name, void *data,
295 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
David Howells454e2392006-06-23 02:02:57 -0700297 return get_sb_pseudo(fs_type, "socket:", &sockfs_ops, SOCKFS_MAGIC,
298 mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300
Eric Dumazetba899662005-08-26 12:05:31 -0700301static struct vfsmount *sock_mnt __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303static struct file_system_type sock_fs_type = {
304 .name = "sockfs",
305 .get_sb = sockfs_get_sb,
306 .kill_sb = kill_anon_super,
307};
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309static int sockfs_delete_dentry(struct dentry *dentry)
310{
Eric Dumazet304e61e2006-12-06 20:38:49 -0800311 /*
312 * At creation time, we pretended this dentry was hashed
313 * (by clearing DCACHE_UNHASHED bit in d_flags)
314 * At delete time, we restore the truth : not hashed.
315 * (so that dput() can proceed correctly)
316 */
317 dentry->d_flags |= DCACHE_UNHASHED;
318 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319}
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700320
321/*
322 * sockfs_dname() is called from d_path().
323 */
324static char *sockfs_dname(struct dentry *dentry, char *buffer, int buflen)
325{
326 return dynamic_dname(dentry, buffer, buflen, "socket:[%lu]",
327 dentry->d_inode->i_ino);
328}
329
Al Viro3ba13d12009-02-20 06:02:22 +0000330static const struct dentry_operations sockfs_dentry_operations = {
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700331 .d_delete = sockfs_delete_dentry,
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700332 .d_dname = sockfs_dname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333};
334
335/*
336 * Obtains the first available file descriptor and sets it up for use.
337 *
David S. Miller39d8c1b2006-03-20 17:13:49 -0800338 * These functions create file structures and maps them to fd space
339 * of the current process. On success it returns file descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 * and file struct implicitly stored in sock->file.
341 * Note that another thread may close file descriptor before we return
342 * from this function. We use the fact that now we do not refer
343 * to socket after mapping. If one day we will need it, this
344 * function will increment ref. count on file by 1.
345 *
346 * In any case returned fd MAY BE not valid!
347 * This race condition is unavoidable
348 * with shared fd spaces, we cannot solve it inside kernel,
349 * but we take care of internal coherence yet.
350 */
351
Ulrich Dreppera677a032008-07-23 21:29:17 -0700352static int sock_alloc_fd(struct file **filep, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
354 int fd;
David S. Miller39d8c1b2006-03-20 17:13:49 -0800355
Ulrich Dreppera677a032008-07-23 21:29:17 -0700356 fd = get_unused_fd_flags(flags);
David S. Miller39d8c1b2006-03-20 17:13:49 -0800357 if (likely(fd >= 0)) {
358 struct file *file = get_empty_filp();
359
360 *filep = file;
361 if (unlikely(!file)) {
362 put_unused_fd(fd);
363 return -ENFILE;
364 }
365 } else
366 *filep = NULL;
367 return fd;
368}
369
Ulrich Drepper77d27202008-07-23 21:29:35 -0700370static int sock_attach_fd(struct socket *sock, struct file *file, int flags)
David S. Miller39d8c1b2006-03-20 17:13:49 -0800371{
Dave Hansence8d2cd2007-10-16 23:31:13 -0700372 struct dentry *dentry;
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700373 struct qstr name = { .name = "" };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Dave Hansence8d2cd2007-10-16 23:31:13 -0700375 dentry = d_alloc(sock_mnt->mnt_sb->s_root, &name);
376 if (unlikely(!dentry))
David S. Miller39d8c1b2006-03-20 17:13:49 -0800377 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Dave Hansence8d2cd2007-10-16 23:31:13 -0700379 dentry->d_op = &sockfs_dentry_operations;
Eric Dumazet304e61e2006-12-06 20:38:49 -0800380 /*
381 * We dont want to push this dentry into global dentry hash table.
382 * We pretend dentry is already hashed, by unsetting DCACHE_UNHASHED
383 * This permits a working /proc/$pid/fd/XXX on sockets
384 */
Dave Hansence8d2cd2007-10-16 23:31:13 -0700385 dentry->d_flags &= ~DCACHE_UNHASHED;
386 d_instantiate(dentry, SOCK_INODE(sock));
David S. Miller39d8c1b2006-03-20 17:13:49 -0800387
388 sock->file = file;
Dave Hansence8d2cd2007-10-16 23:31:13 -0700389 init_file(file, sock_mnt, dentry, FMODE_READ | FMODE_WRITE,
390 &socket_file_ops);
391 SOCK_INODE(sock)->i_fop = &socket_file_ops;
Ulrich Drepper77d27202008-07-23 21:29:35 -0700392 file->f_flags = O_RDWR | (flags & O_NONBLOCK);
David S. Miller39d8c1b2006-03-20 17:13:49 -0800393 file->f_pos = 0;
394 file->private_data = sock;
395
396 return 0;
397}
398
Ulrich Dreppera677a032008-07-23 21:29:17 -0700399int sock_map_fd(struct socket *sock, int flags)
David S. Miller39d8c1b2006-03-20 17:13:49 -0800400{
401 struct file *newfile;
Ulrich Dreppera677a032008-07-23 21:29:17 -0700402 int fd = sock_alloc_fd(&newfile, flags);
David S. Miller39d8c1b2006-03-20 17:13:49 -0800403
404 if (likely(fd >= 0)) {
Ulrich Drepper77d27202008-07-23 21:29:35 -0700405 int err = sock_attach_fd(sock, newfile, flags);
David S. Miller39d8c1b2006-03-20 17:13:49 -0800406
407 if (unlikely(err < 0)) {
408 put_filp(newfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 put_unused_fd(fd);
David S. Miller39d8c1b2006-03-20 17:13:49 -0800410 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
David S. Miller39d8c1b2006-03-20 17:13:49 -0800412 fd_install(fd, newfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 return fd;
415}
416
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800417static struct socket *sock_from_file(struct file *file, int *err)
418{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800419 if (file->f_op == &socket_file_ops)
420 return file->private_data; /* set in sock_map_fd */
421
Eric Dumazet23bb80d2007-02-08 14:59:57 -0800422 *err = -ENOTSOCK;
423 return NULL;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800424}
425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426/**
427 * sockfd_lookup - Go from a file number to its socket slot
428 * @fd: file handle
429 * @err: pointer to an error code return
430 *
431 * The file handle passed in is locked and the socket it is bound
432 * too is returned. If an error occurs the err pointer is overwritten
433 * with a negative errno code and NULL is returned. The function checks
434 * for both invalid handles and passing a handle which is not a socket.
435 *
436 * On a success the socket object pointer is returned.
437 */
438
439struct socket *sockfd_lookup(int fd, int *err)
440{
441 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 struct socket *sock;
443
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700444 file = fget(fd);
445 if (!file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 *err = -EBADF;
447 return NULL;
448 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700449
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800450 sock = sock_from_file(file, err);
451 if (!sock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 fput(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 return sock;
454}
455
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800456static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed)
457{
458 struct file *file;
459 struct socket *sock;
460
Hua Zhong36725582006-04-19 15:25:02 -0700461 *err = -EBADF;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800462 file = fget_light(fd, fput_needed);
463 if (file) {
464 sock = sock_from_file(file, err);
465 if (sock)
466 return sock;
467 fput_light(file, *fput_needed);
468 }
469 return NULL;
470}
471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472/**
473 * sock_alloc - allocate a socket
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700474 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 * Allocate a new inode and socket object. The two are bound together
476 * and initialised. The socket is then returned. If we are out of inodes
477 * NULL is returned.
478 */
479
480static struct socket *sock_alloc(void)
481{
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700482 struct inode *inode;
483 struct socket *sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 inode = new_inode(sock_mnt->mnt_sb);
486 if (!inode)
487 return NULL;
488
489 sock = SOCKET_I(inode);
490
Eric Dumazet29a020d2009-09-15 02:39:20 -0700491 kmemcheck_annotate_bitfield(sock, type);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700492 inode->i_mode = S_IFSOCK | S_IRWXUGO;
David Howells8192b0c2008-11-14 10:39:10 +1100493 inode->i_uid = current_fsuid();
494 inode->i_gid = current_fsgid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Eric Dumazet4e694892009-04-04 16:41:09 -0700496 percpu_add(sockets_in_use, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 return sock;
498}
499
500/*
501 * In theory you can't get an open on this inode, but /proc provides
502 * a back door. Remember to keep it shut otherwise you'll let the
503 * creepy crawlies in.
504 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506static int sock_no_open(struct inode *irrelevant, struct file *dontcare)
507{
508 return -ENXIO;
509}
510
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800511const struct file_operations bad_sock_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 .owner = THIS_MODULE,
513 .open = sock_no_open,
514};
515
516/**
517 * sock_release - close a socket
518 * @sock: socket to close
519 *
520 * The socket is released from the protocol stack if it has a release
521 * callback, and the inode is then released if the socket is bound to
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700522 * an inode not a file.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525void sock_release(struct socket *sock)
526{
527 if (sock->ops) {
528 struct module *owner = sock->ops->owner;
529
530 sock->ops->release(sock);
531 sock->ops = NULL;
532 module_put(owner);
533 }
534
535 if (sock->fasync_list)
536 printk(KERN_ERR "sock_release: fasync list not empty!\n");
537
Eric Dumazet4e694892009-04-04 16:41:09 -0700538 percpu_sub(sockets_in_use, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if (!sock->file) {
540 iput(SOCK_INODE(sock));
541 return;
542 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700543 sock->file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544}
545
Patrick Ohly20d49472009-02-12 05:03:38 +0000546int sock_tx_timestamp(struct msghdr *msg, struct sock *sk,
547 union skb_shared_tx *shtx)
548{
549 shtx->flags = 0;
550 if (sock_flag(sk, SOCK_TIMESTAMPING_TX_HARDWARE))
551 shtx->hardware = 1;
552 if (sock_flag(sk, SOCK_TIMESTAMPING_TX_SOFTWARE))
553 shtx->software = 1;
554 return 0;
555}
556EXPORT_SYMBOL(sock_tx_timestamp);
557
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700558static inline int __sock_sendmsg(struct kiocb *iocb, struct socket *sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 struct msghdr *msg, size_t size)
560{
561 struct sock_iocb *si = kiocb_to_siocb(iocb);
562 int err;
563
564 si->sock = sock;
565 si->scm = NULL;
566 si->msg = msg;
567 si->size = size;
568
569 err = security_socket_sendmsg(sock, msg, size);
570 if (err)
571 return err;
572
573 return sock->ops->sendmsg(iocb, sock, msg, size);
574}
575
576int sock_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
577{
578 struct kiocb iocb;
579 struct sock_iocb siocb;
580 int ret;
581
582 init_sync_kiocb(&iocb, NULL);
583 iocb.private = &siocb;
584 ret = __sock_sendmsg(&iocb, sock, msg, size);
585 if (-EIOCBQUEUED == ret)
586 ret = wait_on_sync_kiocb(&iocb);
587 return ret;
588}
589
590int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
591 struct kvec *vec, size_t num, size_t size)
592{
593 mm_segment_t oldfs = get_fs();
594 int result;
595
596 set_fs(KERNEL_DS);
597 /*
598 * the following is safe, since for compiler definitions of kvec and
599 * iovec are identical, yielding the same in-core layout and alignment
600 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700601 msg->msg_iov = (struct iovec *)vec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 msg->msg_iovlen = num;
603 result = sock_sendmsg(sock, msg, size);
604 set_fs(oldfs);
605 return result;
606}
607
Patrick Ohly20d49472009-02-12 05:03:38 +0000608static int ktime2ts(ktime_t kt, struct timespec *ts)
609{
610 if (kt.tv64) {
611 *ts = ktime_to_timespec(kt);
612 return 1;
613 } else {
614 return 0;
615 }
616}
617
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700618/*
619 * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
620 */
621void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
622 struct sk_buff *skb)
623{
Patrick Ohly20d49472009-02-12 05:03:38 +0000624 int need_software_tstamp = sock_flag(sk, SOCK_RCVTSTAMP);
625 struct timespec ts[3];
626 int empty = 1;
627 struct skb_shared_hwtstamps *shhwtstamps =
628 skb_hwtstamps(skb);
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700629
Patrick Ohly20d49472009-02-12 05:03:38 +0000630 /* Race occurred between timestamp enabling and packet
631 receiving. Fill in the current time for now. */
632 if (need_software_tstamp && skb->tstamp.tv64 == 0)
633 __net_timestamp(skb);
634
635 if (need_software_tstamp) {
636 if (!sock_flag(sk, SOCK_RCVTSTAMPNS)) {
637 struct timeval tv;
638 skb_get_timestamp(skb, &tv);
639 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMP,
640 sizeof(tv), &tv);
641 } else {
642 struct timespec ts;
643 skb_get_timestampns(skb, &ts);
644 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPNS,
645 sizeof(ts), &ts);
646 }
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700647 }
Patrick Ohly20d49472009-02-12 05:03:38 +0000648
649
650 memset(ts, 0, sizeof(ts));
651 if (skb->tstamp.tv64 &&
652 sock_flag(sk, SOCK_TIMESTAMPING_SOFTWARE)) {
653 skb_get_timestampns(skb, ts + 0);
654 empty = 0;
655 }
656 if (shhwtstamps) {
657 if (sock_flag(sk, SOCK_TIMESTAMPING_SYS_HARDWARE) &&
658 ktime2ts(shhwtstamps->syststamp, ts + 1))
659 empty = 0;
660 if (sock_flag(sk, SOCK_TIMESTAMPING_RAW_HARDWARE) &&
661 ktime2ts(shhwtstamps->hwtstamp, ts + 2))
662 empty = 0;
663 }
664 if (!empty)
665 put_cmsg(msg, SOL_SOCKET,
666 SCM_TIMESTAMPING, sizeof(ts), &ts);
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700667}
668
Arnaldo Carvalho de Melo7c81fd82007-03-10 00:39:35 -0300669EXPORT_SYMBOL_GPL(__sock_recv_timestamp);
670
Neil Horman3b885782009-10-12 13:26:31 -0700671inline void sock_recv_drops(struct msghdr *msg, struct sock *sk, struct sk_buff *skb)
672{
673 if (sock_flag(sk, SOCK_RXQ_OVFL) && skb && skb->dropcount)
674 put_cmsg(msg, SOL_SOCKET, SO_RXQ_OVFL,
675 sizeof(__u32), &skb->dropcount);
676}
677
678void sock_recv_ts_and_drops(struct msghdr *msg, struct sock *sk,
679 struct sk_buff *skb)
680{
681 sock_recv_timestamp(msg, sk, skb);
682 sock_recv_drops(msg, sk, skb);
683}
684EXPORT_SYMBOL_GPL(sock_recv_ts_and_drops);
685
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700686static inline int __sock_recvmsg(struct kiocb *iocb, struct socket *sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 struct msghdr *msg, size_t size, int flags)
688{
689 int err;
690 struct sock_iocb *si = kiocb_to_siocb(iocb);
691
692 si->sock = sock;
693 si->scm = NULL;
694 si->msg = msg;
695 si->size = size;
696 si->flags = flags;
697
698 err = security_socket_recvmsg(sock, msg, size, flags);
699 if (err)
700 return err;
701
702 return sock->ops->recvmsg(iocb, sock, msg, size, flags);
703}
704
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700705int sock_recvmsg(struct socket *sock, struct msghdr *msg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 size_t size, int flags)
707{
708 struct kiocb iocb;
709 struct sock_iocb siocb;
710 int ret;
711
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700712 init_sync_kiocb(&iocb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 iocb.private = &siocb;
714 ret = __sock_recvmsg(&iocb, sock, msg, size, flags);
715 if (-EIOCBQUEUED == ret)
716 ret = wait_on_sync_kiocb(&iocb);
717 return ret;
718}
719
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700720int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
721 struct kvec *vec, size_t num, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
723 mm_segment_t oldfs = get_fs();
724 int result;
725
726 set_fs(KERNEL_DS);
727 /*
728 * the following is safe, since for compiler definitions of kvec and
729 * iovec are identical, yielding the same in-core layout and alignment
730 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700731 msg->msg_iov = (struct iovec *)vec, msg->msg_iovlen = num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 result = sock_recvmsg(sock, msg, size, flags);
733 set_fs(oldfs);
734 return result;
735}
736
737static void sock_aio_dtor(struct kiocb *iocb)
738{
739 kfree(iocb->private);
740}
741
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -0300742static ssize_t sock_sendpage(struct file *file, struct page *page,
743 int offset, size_t size, loff_t *ppos, int more)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
745 struct socket *sock;
746 int flags;
747
Eric Dumazetb69aee02005-09-06 14:42:45 -0700748 sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
750 flags = !(file->f_flags & O_NONBLOCK) ? 0 : MSG_DONTWAIT;
751 if (more)
752 flags |= MSG_MORE;
753
Linus Torvaldse6949582009-08-13 08:28:36 -0700754 return kernel_sendpage(sock, page, offset, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755}
756
Jens Axboe9c55e012007-11-06 23:30:13 -0800757static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
758 struct pipe_inode_info *pipe, size_t len,
759 unsigned int flags)
760{
761 struct socket *sock = file->private_data;
762
Rémi Denis-Courmont997b37d2008-02-15 02:35:45 -0800763 if (unlikely(!sock->ops->splice_read))
764 return -EINVAL;
765
Jens Axboe9c55e012007-11-06 23:30:13 -0800766 return sock->ops->splice_read(sock, ppos, pipe, len, flags);
767}
768
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800769static struct sock_iocb *alloc_sock_iocb(struct kiocb *iocb,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700770 struct sock_iocb *siocb)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800771{
772 if (!is_sync_kiocb(iocb)) {
773 siocb = kmalloc(sizeof(*siocb), GFP_KERNEL);
774 if (!siocb)
775 return NULL;
776 iocb->ki_dtor = sock_aio_dtor;
777 }
778
779 siocb->kiocb = iocb;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800780 iocb->private = siocb;
781 return siocb;
782}
783
784static ssize_t do_sock_read(struct msghdr *msg, struct kiocb *iocb,
Badari Pulavarty027445c2006-09-30 23:28:46 -0700785 struct file *file, const struct iovec *iov,
786 unsigned long nr_segs)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800787{
788 struct socket *sock = file->private_data;
789 size_t size = 0;
790 int i;
791
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700792 for (i = 0; i < nr_segs; i++)
793 size += iov[i].iov_len;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800794
795 msg->msg_name = NULL;
796 msg->msg_namelen = 0;
797 msg->msg_control = NULL;
798 msg->msg_controllen = 0;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700799 msg->msg_iov = (struct iovec *)iov;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800800 msg->msg_iovlen = nr_segs;
801 msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
802
803 return __sock_recvmsg(iocb, sock, msg, size, msg->msg_flags);
804}
805
Badari Pulavarty027445c2006-09-30 23:28:46 -0700806static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov,
807 unsigned long nr_segs, loff_t pos)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800808{
809 struct sock_iocb siocb, *x;
810
811 if (pos != 0)
812 return -ESPIPE;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700813
814 if (iocb->ki_left == 0) /* Match SYS5 behaviour */
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800815 return 0;
816
Badari Pulavarty027445c2006-09-30 23:28:46 -0700817
818 x = alloc_sock_iocb(iocb, &siocb);
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800819 if (!x)
820 return -ENOMEM;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700821 return do_sock_read(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800822}
823
824static ssize_t do_sock_write(struct msghdr *msg, struct kiocb *iocb,
Badari Pulavarty027445c2006-09-30 23:28:46 -0700825 struct file *file, const struct iovec *iov,
826 unsigned long nr_segs)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800827{
828 struct socket *sock = file->private_data;
829 size_t size = 0;
830 int i;
831
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700832 for (i = 0; i < nr_segs; i++)
833 size += iov[i].iov_len;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800834
835 msg->msg_name = NULL;
836 msg->msg_namelen = 0;
837 msg->msg_control = NULL;
838 msg->msg_controllen = 0;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700839 msg->msg_iov = (struct iovec *)iov;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800840 msg->msg_iovlen = nr_segs;
841 msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
842 if (sock->type == SOCK_SEQPACKET)
843 msg->msg_flags |= MSG_EOR;
844
845 return __sock_sendmsg(iocb, sock, msg, size);
846}
847
Badari Pulavarty027445c2006-09-30 23:28:46 -0700848static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov,
849 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800851 struct sock_iocb siocb, *x;
852
853 if (pos != 0)
854 return -ESPIPE;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700855
Badari Pulavarty027445c2006-09-30 23:28:46 -0700856 x = alloc_sock_iocb(iocb, &siocb);
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800857 if (!x)
858 return -ENOMEM;
859
Badari Pulavarty027445c2006-09-30 23:28:46 -0700860 return do_sock_write(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861}
862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863/*
864 * Atomic setting of ioctl hooks to avoid race
865 * with module unload.
866 */
867
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800868static DEFINE_MUTEX(br_ioctl_mutex);
Eric W. Biederman881d9662007-09-17 11:56:21 -0700869static int (*br_ioctl_hook) (struct net *, unsigned int cmd, void __user *arg) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Eric W. Biederman881d9662007-09-17 11:56:21 -0700871void brioctl_set(int (*hook) (struct net *, unsigned int, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800873 mutex_lock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 br_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800875 mutex_unlock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876}
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878EXPORT_SYMBOL(brioctl_set);
879
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800880static DEFINE_MUTEX(vlan_ioctl_mutex);
Eric W. Biederman881d9662007-09-17 11:56:21 -0700881static int (*vlan_ioctl_hook) (struct net *, void __user *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Eric W. Biederman881d9662007-09-17 11:56:21 -0700883void vlan_ioctl_set(int (*hook) (struct net *, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800885 mutex_lock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 vlan_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800887 mutex_unlock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888}
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890EXPORT_SYMBOL(vlan_ioctl_set);
891
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800892static DEFINE_MUTEX(dlci_ioctl_mutex);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700893static int (*dlci_ioctl_hook) (unsigned int, void __user *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700895void dlci_ioctl_set(int (*hook) (unsigned int, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800897 mutex_lock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 dlci_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800899 mutex_unlock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900}
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902EXPORT_SYMBOL(dlci_ioctl_set);
903
904/*
905 * With an ioctl, arg may well be a user mode pointer, but we don't know
906 * what to do with it - that's up to the protocol still.
907 */
908
909static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
910{
911 struct socket *sock;
Eric W. Biederman881d9662007-09-17 11:56:21 -0700912 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 void __user *argp = (void __user *)arg;
914 int pid, err;
Eric W. Biederman881d9662007-09-17 11:56:21 -0700915 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Eric Dumazetb69aee02005-09-06 14:42:45 -0700917 sock = file->private_data;
Eric W. Biederman881d9662007-09-17 11:56:21 -0700918 sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900919 net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
Eric W. Biederman881d9662007-09-17 11:56:21 -0700921 err = dev_ioctl(net, cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 } else
Johannes Berg3d23e342009-09-29 23:27:28 +0200923#ifdef CONFIG_WEXT_CORE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
Eric W. Biederman881d9662007-09-17 11:56:21 -0700925 err = dev_ioctl(net, cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 } else
Johannes Berg3d23e342009-09-29 23:27:28 +0200927#endif
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700928 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 case FIOSETOWN:
930 case SIOCSPGRP:
931 err = -EFAULT;
932 if (get_user(pid, (int __user *)argp))
933 break;
934 err = f_setown(sock->file, pid, 1);
935 break;
936 case FIOGETOWN:
937 case SIOCGPGRP:
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700938 err = put_user(f_getown(sock->file),
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700939 (int __user *)argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 break;
941 case SIOCGIFBR:
942 case SIOCSIFBR:
943 case SIOCBRADDBR:
944 case SIOCBRDELBR:
945 err = -ENOPKG;
946 if (!br_ioctl_hook)
947 request_module("bridge");
948
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800949 mutex_lock(&br_ioctl_mutex);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700950 if (br_ioctl_hook)
Eric W. Biederman881d9662007-09-17 11:56:21 -0700951 err = br_ioctl_hook(net, cmd, argp);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800952 mutex_unlock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 break;
954 case SIOCGIFVLAN:
955 case SIOCSIFVLAN:
956 err = -ENOPKG;
957 if (!vlan_ioctl_hook)
958 request_module("8021q");
959
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800960 mutex_lock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 if (vlan_ioctl_hook)
Eric W. Biederman881d9662007-09-17 11:56:21 -0700962 err = vlan_ioctl_hook(net, argp);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800963 mutex_unlock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 case SIOCADDDLCI:
966 case SIOCDELDLCI:
967 err = -ENOPKG;
968 if (!dlci_ioctl_hook)
969 request_module("dlci");
970
Pavel Emelyanov7512cbf2008-03-21 15:58:52 -0700971 mutex_lock(&dlci_ioctl_mutex);
972 if (dlci_ioctl_hook)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 err = dlci_ioctl_hook(cmd, argp);
Pavel Emelyanov7512cbf2008-03-21 15:58:52 -0700974 mutex_unlock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 break;
976 default:
977 err = sock->ops->ioctl(sock, cmd, arg);
Christoph Hellwigb5e5fa52006-01-03 14:18:33 -0800978
979 /*
980 * If this ioctl is unknown try to hand it down
981 * to the NIC driver.
982 */
983 if (err == -ENOIOCTLCMD)
Eric W. Biederman881d9662007-09-17 11:56:21 -0700984 err = dev_ioctl(net, cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 break;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700986 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 return err;
988}
989
990int sock_create_lite(int family, int type, int protocol, struct socket **res)
991{
992 int err;
993 struct socket *sock = NULL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 err = security_socket_create(family, type, protocol, 1);
996 if (err)
997 goto out;
998
999 sock = sock_alloc();
1000 if (!sock) {
1001 err = -ENOMEM;
1002 goto out;
1003 }
1004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 sock->type = type;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001006 err = security_socket_post_create(sock, family, type, protocol, 1);
1007 if (err)
1008 goto out_release;
1009
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010out:
1011 *res = sock;
1012 return err;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001013out_release:
1014 sock_release(sock);
1015 sock = NULL;
1016 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017}
1018
1019/* No kernel lock held - perfect */
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001020static unsigned int sock_poll(struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021{
1022 struct socket *sock;
1023
1024 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001025 * We can't return errors to poll, so it's either yes or no.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 */
Eric Dumazetb69aee02005-09-06 14:42:45 -07001027 sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 return sock->ops->poll(file, sock, wait);
1029}
1030
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001031static int sock_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032{
Eric Dumazetb69aee02005-09-06 14:42:45 -07001033 struct socket *sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
1035 return sock->ops->mmap(file, sock, vma);
1036}
1037
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001038static int sock_close(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
1040 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001041 * It was possible the inode is NULL we were
1042 * closing an unfinished socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 */
1044
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001045 if (!inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 printk(KERN_DEBUG "sock_close: NULL inode\n");
1047 return 0;
1048 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 sock_release(SOCKET_I(inode));
1050 return 0;
1051}
1052
1053/*
1054 * Update the socket async list
1055 *
1056 * Fasync_list locking strategy.
1057 *
1058 * 1. fasync_list is modified only under process context socket lock
1059 * i.e. under semaphore.
1060 * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
1061 * or under socket lock.
1062 * 3. fasync_list can be used from softirq context, so that
1063 * modification under socket lock have to be enhanced with
1064 * write_lock_bh(&sk->sk_callback_lock).
1065 * --ANK (990710)
1066 */
1067
1068static int sock_fasync(int fd, struct file *filp, int on)
1069{
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001070 struct fasync_struct *fa, *fna = NULL, **prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 struct socket *sock;
1072 struct sock *sk;
1073
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001074 if (on) {
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08001075 fna = kmalloc(sizeof(struct fasync_struct), GFP_KERNEL);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001076 if (fna == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 return -ENOMEM;
1078 }
1079
Eric Dumazetb69aee02005-09-06 14:42:45 -07001080 sock = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001082 sk = sock->sk;
1083 if (sk == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 kfree(fna);
1085 return -EINVAL;
1086 }
1087
1088 lock_sock(sk);
1089
Jonathan Corbet76398422009-02-01 14:26:59 -07001090 spin_lock(&filp->f_lock);
1091 if (on)
1092 filp->f_flags |= FASYNC;
1093 else
1094 filp->f_flags &= ~FASYNC;
1095 spin_unlock(&filp->f_lock);
1096
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001097 prev = &(sock->fasync_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001099 for (fa = *prev; fa != NULL; prev = &fa->fa_next, fa = *prev)
1100 if (fa->fa_file == filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 break;
1102
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001103 if (on) {
1104 if (fa != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 write_lock_bh(&sk->sk_callback_lock);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001106 fa->fa_fd = fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 write_unlock_bh(&sk->sk_callback_lock);
1108
1109 kfree(fna);
1110 goto out;
1111 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001112 fna->fa_file = filp;
1113 fna->fa_fd = fd;
1114 fna->magic = FASYNC_MAGIC;
1115 fna->fa_next = sock->fasync_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 write_lock_bh(&sk->sk_callback_lock);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001117 sock->fasync_list = fna;
Eric Dumazetbcdce712009-10-06 17:28:29 -07001118 sock_set_flag(sk, SOCK_FASYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 write_unlock_bh(&sk->sk_callback_lock);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001120 } else {
1121 if (fa != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 write_lock_bh(&sk->sk_callback_lock);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001123 *prev = fa->fa_next;
Eric Dumazetbcdce712009-10-06 17:28:29 -07001124 if (!sock->fasync_list)
1125 sock_reset_flag(sk, SOCK_FASYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 write_unlock_bh(&sk->sk_callback_lock);
1127 kfree(fa);
1128 }
1129 }
1130
1131out:
1132 release_sock(sock->sk);
1133 return 0;
1134}
1135
1136/* This function may be called only under socket lock or callback_lock */
1137
1138int sock_wake_async(struct socket *sock, int how, int band)
1139{
1140 if (!sock || !sock->fasync_list)
1141 return -1;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001142 switch (how) {
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001143 case SOCK_WAKE_WAITD:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 if (test_bit(SOCK_ASYNC_WAITDATA, &sock->flags))
1145 break;
1146 goto call_kill;
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001147 case SOCK_WAKE_SPACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags))
1149 break;
1150 /* fall through */
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001151 case SOCK_WAKE_IO:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001152call_kill:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 __kill_fasync(sock->fasync_list, SIGIO, band);
1154 break;
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001155 case SOCK_WAKE_URG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 __kill_fasync(sock->fasync_list, SIGURG, band);
1157 }
1158 return 0;
1159}
1160
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001161static int __sock_create(struct net *net, int family, int type, int protocol,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001162 struct socket **res, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163{
1164 int err;
1165 struct socket *sock;
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001166 const struct net_proto_family *pf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
1168 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001169 * Check protocol is in range
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 */
1171 if (family < 0 || family >= NPROTO)
1172 return -EAFNOSUPPORT;
1173 if (type < 0 || type >= SOCK_MAX)
1174 return -EINVAL;
1175
1176 /* Compatibility.
1177
1178 This uglymoron is moved from INET layer to here to avoid
1179 deadlock in module load.
1180 */
1181 if (family == PF_INET && type == SOCK_PACKET) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001182 static int warned;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 if (!warned) {
1184 warned = 1;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001185 printk(KERN_INFO "%s uses obsolete (PF_INET,SOCK_PACKET)\n",
1186 current->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 }
1188 family = PF_PACKET;
1189 }
1190
1191 err = security_socket_create(family, type, protocol, kern);
1192 if (err)
1193 return err;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001194
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001195 /*
1196 * Allocate the socket and allow the family to set things up. if
1197 * the protocol is 0, the family is instructed to select an appropriate
1198 * default.
1199 */
1200 sock = sock_alloc();
1201 if (!sock) {
1202 if (net_ratelimit())
1203 printk(KERN_WARNING "socket: no more sockets\n");
1204 return -ENFILE; /* Not exactly a match, but its the
1205 closest posix thing */
1206 }
1207
1208 sock->type = type;
1209
Johannes Berg95a5afc2008-10-16 15:24:51 -07001210#ifdef CONFIG_MODULES
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001211 /* Attempt to load a protocol module if the find failed.
1212 *
1213 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 * requested real, full-featured networking support upon configuration.
1215 * Otherwise module support will break!
1216 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001217 if (net_families[family] == NULL)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001218 request_module("net-pf-%d", family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219#endif
1220
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001221 rcu_read_lock();
1222 pf = rcu_dereference(net_families[family]);
1223 err = -EAFNOSUPPORT;
1224 if (!pf)
1225 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
1227 /*
1228 * We will call the ->create function, that possibly is in a loadable
1229 * module, so we have to bump that loadable module refcnt first.
1230 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001231 if (!try_module_get(pf->owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 goto out_release;
1233
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001234 /* Now protected by module ref count */
1235 rcu_read_unlock();
1236
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001237 err = pf->create(net, sock, protocol);
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001238 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 goto out_module_put;
Frank Filza79af592005-09-27 15:23:38 -07001240
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 /*
1242 * Now to bump the refcnt of the [loadable] module that owns this
1243 * socket at sock_release time we decrement its refcnt.
1244 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001245 if (!try_module_get(sock->ops->owner))
1246 goto out_module_busy;
1247
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 /*
1249 * Now that we're done with the ->create function, the [loadable]
1250 * module can have its refcnt decremented
1251 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001252 module_put(pf->owner);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001253 err = security_socket_post_create(sock, family, type, protocol, kern);
1254 if (err)
Herbert Xu3b185522007-08-15 14:46:02 -07001255 goto out_sock_release;
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001256 *res = sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001258 return 0;
1259
1260out_module_busy:
1261 err = -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262out_module_put:
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001263 sock->ops = NULL;
1264 module_put(pf->owner);
1265out_sock_release:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 sock_release(sock);
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001267 return err;
1268
1269out_release:
1270 rcu_read_unlock();
1271 goto out_sock_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272}
1273
1274int sock_create(int family, int type, int protocol, struct socket **res)
1275{
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001276 return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277}
1278
1279int sock_create_kern(int family, int type, int protocol, struct socket **res)
1280{
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001281 return __sock_create(&init_net, family, type, protocol, res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282}
1283
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001284SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285{
1286 int retval;
1287 struct socket *sock;
Ulrich Dreppera677a032008-07-23 21:29:17 -07001288 int flags;
1289
Ulrich Dreppere38b36f2008-07-23 21:29:42 -07001290 /* Check the SOCK_* constants for consistency. */
1291 BUILD_BUG_ON(SOCK_CLOEXEC != O_CLOEXEC);
1292 BUILD_BUG_ON((SOCK_MAX | SOCK_TYPE_MASK) != SOCK_TYPE_MASK);
1293 BUILD_BUG_ON(SOCK_CLOEXEC & SOCK_TYPE_MASK);
1294 BUILD_BUG_ON(SOCK_NONBLOCK & SOCK_TYPE_MASK);
1295
Ulrich Dreppera677a032008-07-23 21:29:17 -07001296 flags = type & ~SOCK_TYPE_MASK;
Ulrich Drepper77d27202008-07-23 21:29:35 -07001297 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Dreppera677a032008-07-23 21:29:17 -07001298 return -EINVAL;
1299 type &= SOCK_TYPE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001301 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1302 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1303
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 retval = sock_create(family, type, protocol, &sock);
1305 if (retval < 0)
1306 goto out;
1307
Ulrich Drepper77d27202008-07-23 21:29:35 -07001308 retval = sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 if (retval < 0)
1310 goto out_release;
1311
1312out:
1313 /* It may be already another descriptor 8) Not kernel problem. */
1314 return retval;
1315
1316out_release:
1317 sock_release(sock);
1318 return retval;
1319}
1320
1321/*
1322 * Create a pair of connected sockets.
1323 */
1324
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001325SYSCALL_DEFINE4(socketpair, int, family, int, type, int, protocol,
1326 int __user *, usockvec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327{
1328 struct socket *sock1, *sock2;
1329 int fd1, fd2, err;
Al Virodb349502007-02-07 01:48:00 -05001330 struct file *newfile1, *newfile2;
Ulrich Dreppera677a032008-07-23 21:29:17 -07001331 int flags;
1332
1333 flags = type & ~SOCK_TYPE_MASK;
Ulrich Drepper77d27202008-07-23 21:29:35 -07001334 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Dreppera677a032008-07-23 21:29:17 -07001335 return -EINVAL;
1336 type &= SOCK_TYPE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001338 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1339 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1340
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 /*
1342 * Obtain the first socket and check if the underlying protocol
1343 * supports the socketpair call.
1344 */
1345
1346 err = sock_create(family, type, protocol, &sock1);
1347 if (err < 0)
1348 goto out;
1349
1350 err = sock_create(family, type, protocol, &sock2);
1351 if (err < 0)
1352 goto out_release_1;
1353
1354 err = sock1->ops->socketpair(sock1, sock2);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001355 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 goto out_release_both;
1357
Ulrich Dreppera677a032008-07-23 21:29:17 -07001358 fd1 = sock_alloc_fd(&newfile1, flags & O_CLOEXEC);
David S. Millerbf3c23d2007-10-29 21:54:02 -07001359 if (unlikely(fd1 < 0)) {
1360 err = fd1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 goto out_release_both;
David S. Millerbf3c23d2007-10-29 21:54:02 -07001362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Ulrich Dreppera677a032008-07-23 21:29:17 -07001364 fd2 = sock_alloc_fd(&newfile2, flags & O_CLOEXEC);
Al Virodb349502007-02-07 01:48:00 -05001365 if (unlikely(fd2 < 0)) {
David S. Millerbf3c23d2007-10-29 21:54:02 -07001366 err = fd2;
Al Virodb349502007-02-07 01:48:00 -05001367 put_filp(newfile1);
1368 put_unused_fd(fd1);
1369 goto out_release_both;
1370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Ulrich Drepper77d27202008-07-23 21:29:35 -07001372 err = sock_attach_fd(sock1, newfile1, flags & O_NONBLOCK);
Al Virodb349502007-02-07 01:48:00 -05001373 if (unlikely(err < 0)) {
1374 goto out_fd2;
1375 }
1376
Ulrich Drepper77d27202008-07-23 21:29:35 -07001377 err = sock_attach_fd(sock2, newfile2, flags & O_NONBLOCK);
Al Virodb349502007-02-07 01:48:00 -05001378 if (unlikely(err < 0)) {
1379 fput(newfile1);
1380 goto out_fd1;
1381 }
1382
Al Viro157cf642008-12-14 04:57:47 -05001383 audit_fd_pair(fd1, fd2);
Al Virodb349502007-02-07 01:48:00 -05001384 fd_install(fd1, newfile1);
1385 fd_install(fd2, newfile2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 /* fd1 and fd2 may be already another descriptors.
1387 * Not kernel problem.
1388 */
1389
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001390 err = put_user(fd1, &usockvec[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 if (!err)
1392 err = put_user(fd2, &usockvec[1]);
1393 if (!err)
1394 return 0;
1395
1396 sys_close(fd2);
1397 sys_close(fd1);
1398 return err;
1399
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400out_release_both:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001401 sock_release(sock2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402out_release_1:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001403 sock_release(sock1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404out:
1405 return err;
Al Virodb349502007-02-07 01:48:00 -05001406
1407out_fd2:
1408 put_filp(newfile1);
1409 sock_release(sock1);
1410out_fd1:
1411 put_filp(newfile2);
1412 sock_release(sock2);
Al Virodb349502007-02-07 01:48:00 -05001413 put_unused_fd(fd1);
1414 put_unused_fd(fd2);
1415 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416}
1417
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418/*
1419 * Bind a name to a socket. Nothing much to do here since it's
1420 * the protocol's responsibility to handle the local address.
1421 *
1422 * We move the socket address to kernel space before we call
1423 * the protocol layer (having also checked the address is ok).
1424 */
1425
Heiko Carstens20f37032009-01-14 14:14:23 +01001426SYSCALL_DEFINE3(bind, int, fd, struct sockaddr __user *, umyaddr, int, addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427{
1428 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001429 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001430 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001432 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001433 if (sock) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001434 err = move_addr_to_kernel(umyaddr, addrlen, (struct sockaddr *)&address);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001435 if (err >= 0) {
1436 err = security_socket_bind(sock,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001437 (struct sockaddr *)&address,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001438 addrlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001439 if (!err)
1440 err = sock->ops->bind(sock,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001441 (struct sockaddr *)
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001442 &address, addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 }
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001444 fput_light(sock->file, fput_needed);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 return err;
1447}
1448
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449/*
1450 * Perform a listen. Basically, we allow the protocol to do anything
1451 * necessary for a listen, and if that works, we mark the socket as
1452 * ready for listening.
1453 */
1454
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001455SYSCALL_DEFINE2(listen, int, fd, int, backlog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456{
1457 struct socket *sock;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001458 int err, fput_needed;
Pavel Emelyanovb8e1f9b2007-12-08 00:12:33 -08001459 int somaxconn;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001460
1461 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1462 if (sock) {
Pavel Emelyanov8efa6e92008-03-31 19:41:14 -07001463 somaxconn = sock_net(sock->sk)->core.sysctl_somaxconn;
Pavel Emelyanovb8e1f9b2007-12-08 00:12:33 -08001464 if ((unsigned)backlog > somaxconn)
1465 backlog = somaxconn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
1467 err = security_socket_listen(sock, backlog);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001468 if (!err)
1469 err = sock->ops->listen(sock, backlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001471 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 }
1473 return err;
1474}
1475
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476/*
1477 * For accept, we attempt to create a new socket, set up the link
1478 * with the client, wake up the client, then return the new
1479 * connected fd. We collect the address of the connector in kernel
1480 * space and move it to user at the very end. This is unclean because
1481 * we open the socket then return an error.
1482 *
1483 * 1003.1g adds the ability to recvmsg() to query connection pending
1484 * status to recvmsg. We need to add that support in a way thats
1485 * clean when we restucture accept also.
1486 */
1487
Heiko Carstens20f37032009-01-14 14:14:23 +01001488SYSCALL_DEFINE4(accept4, int, fd, struct sockaddr __user *, upeer_sockaddr,
1489 int __user *, upeer_addrlen, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490{
1491 struct socket *sock, *newsock;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001492 struct file *newfile;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001493 int err, len, newfd, fput_needed;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001494 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
Ulrich Drepper77d27202008-07-23 21:29:35 -07001496 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001497 return -EINVAL;
1498
1499 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1500 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1501
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001502 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 if (!sock)
1504 goto out;
1505
1506 err = -ENFILE;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001507 if (!(newsock = sock_alloc()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 goto out_put;
1509
1510 newsock->type = sock->type;
1511 newsock->ops = sock->ops;
1512
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 /*
1514 * We don't need try_module_get here, as the listening socket (sock)
1515 * has the protocol module (sock->ops->owner) held.
1516 */
1517 __module_get(newsock->ops->owner);
1518
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001519 newfd = sock_alloc_fd(&newfile, flags & O_CLOEXEC);
David S. Miller39d8c1b2006-03-20 17:13:49 -08001520 if (unlikely(newfd < 0)) {
1521 err = newfd;
David S. Miller9a1875e2006-04-01 12:48:36 -08001522 sock_release(newsock);
1523 goto out_put;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001524 }
1525
Ulrich Drepper77d27202008-07-23 21:29:35 -07001526 err = sock_attach_fd(newsock, newfile, flags & O_NONBLOCK);
David S. Miller39d8c1b2006-03-20 17:13:49 -08001527 if (err < 0)
Alexey Dobriyan79f4f642007-03-26 14:09:52 -07001528 goto out_fd_simple;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001529
Frank Filza79af592005-09-27 15:23:38 -07001530 err = security_socket_accept(sock, newsock);
1531 if (err)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001532 goto out_fd;
Frank Filza79af592005-09-27 15:23:38 -07001533
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 err = sock->ops->accept(sock, newsock, sock->file->f_flags);
1535 if (err < 0)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001536 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
1538 if (upeer_sockaddr) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001539 if (newsock->ops->getname(newsock, (struct sockaddr *)&address,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001540 &len, 2) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 err = -ECONNABORTED;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001542 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 }
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001544 err = move_addr_to_user((struct sockaddr *)&address,
1545 len, upeer_sockaddr, upeer_addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 if (err < 0)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001547 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 }
1549
1550 /* File flags are not inherited via accept() unlike another OSes. */
1551
David S. Miller39d8c1b2006-03-20 17:13:49 -08001552 fd_install(newfd, newfile);
1553 err = newfd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001556 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557out:
1558 return err;
Alexey Dobriyan79f4f642007-03-26 14:09:52 -07001559out_fd_simple:
1560 sock_release(newsock);
1561 put_filp(newfile);
1562 put_unused_fd(newfd);
1563 goto out_put;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001564out_fd:
David S. Miller9606a212006-04-01 01:00:14 -08001565 fput(newfile);
David S. Miller39d8c1b2006-03-20 17:13:49 -08001566 put_unused_fd(newfd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 goto out_put;
1568}
1569
Heiko Carstens20f37032009-01-14 14:14:23 +01001570SYSCALL_DEFINE3(accept, int, fd, struct sockaddr __user *, upeer_sockaddr,
1571 int __user *, upeer_addrlen)
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001572{
Ulrich Drepperde11def2008-11-19 15:36:14 -08001573 return sys_accept4(fd, upeer_sockaddr, upeer_addrlen, 0);
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001574}
1575
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576/*
1577 * Attempt to connect to a socket with the server address. The address
1578 * is in user space so we verify it is OK and move it to kernel space.
1579 *
1580 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
1581 * break bindings
1582 *
1583 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
1584 * other SEQPACKET protocols that take time to connect() as it doesn't
1585 * include the -EINPROGRESS status for such sockets.
1586 */
1587
Heiko Carstens20f37032009-01-14 14:14:23 +01001588SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
1589 int, addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590{
1591 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001592 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001593 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001595 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 if (!sock)
1597 goto out;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001598 err = move_addr_to_kernel(uservaddr, addrlen, (struct sockaddr *)&address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 if (err < 0)
1600 goto out_put;
1601
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001602 err =
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001603 security_socket_connect(sock, (struct sockaddr *)&address, addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 if (err)
1605 goto out_put;
1606
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001607 err = sock->ops->connect(sock, (struct sockaddr *)&address, addrlen,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 sock->file->f_flags);
1609out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001610 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611out:
1612 return err;
1613}
1614
1615/*
1616 * Get the local address ('name') of a socket object. Move the obtained
1617 * name to user space.
1618 */
1619
Heiko Carstens20f37032009-01-14 14:14:23 +01001620SYSCALL_DEFINE3(getsockname, int, fd, struct sockaddr __user *, usockaddr,
1621 int __user *, usockaddr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622{
1623 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001624 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001625 int len, err, fput_needed;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001626
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001627 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 if (!sock)
1629 goto out;
1630
1631 err = security_socket_getsockname(sock);
1632 if (err)
1633 goto out_put;
1634
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001635 err = sock->ops->getname(sock, (struct sockaddr *)&address, &len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 if (err)
1637 goto out_put;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001638 err = move_addr_to_user((struct sockaddr *)&address, len, usockaddr, usockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
1640out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001641 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642out:
1643 return err;
1644}
1645
1646/*
1647 * Get the remote address ('name') of a socket object. Move the obtained
1648 * name to user space.
1649 */
1650
Heiko Carstens20f37032009-01-14 14:14:23 +01001651SYSCALL_DEFINE3(getpeername, int, fd, struct sockaddr __user *, usockaddr,
1652 int __user *, usockaddr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653{
1654 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001655 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001656 int len, err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001658 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1659 if (sock != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 err = security_socket_getpeername(sock);
1661 if (err) {
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001662 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 return err;
1664 }
1665
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001666 err =
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001667 sock->ops->getname(sock, (struct sockaddr *)&address, &len,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001668 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 if (!err)
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001670 err = move_addr_to_user((struct sockaddr *)&address, len, usockaddr,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001671 usockaddr_len);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001672 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 }
1674 return err;
1675}
1676
1677/*
1678 * Send a datagram to a given address. We move the address into kernel
1679 * space and check the user space data area is readable before invoking
1680 * the protocol.
1681 */
1682
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001683SYSCALL_DEFINE6(sendto, int, fd, void __user *, buff, size_t, len,
1684 unsigned, flags, struct sockaddr __user *, addr,
1685 int, addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686{
1687 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001688 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 int err;
1690 struct msghdr msg;
1691 struct iovec iov;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001692 int fput_needed;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001693
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001694 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1695 if (!sock)
David S. Miller4387ff72007-02-08 15:06:08 -08001696 goto out;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001697
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001698 iov.iov_base = buff;
1699 iov.iov_len = len;
1700 msg.msg_name = NULL;
1701 msg.msg_iov = &iov;
1702 msg.msg_iovlen = 1;
1703 msg.msg_control = NULL;
1704 msg.msg_controllen = 0;
1705 msg.msg_namelen = 0;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001706 if (addr) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001707 err = move_addr_to_kernel(addr, addr_len, (struct sockaddr *)&address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 if (err < 0)
1709 goto out_put;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001710 msg.msg_name = (struct sockaddr *)&address;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001711 msg.msg_namelen = addr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 }
1713 if (sock->file->f_flags & O_NONBLOCK)
1714 flags |= MSG_DONTWAIT;
1715 msg.msg_flags = flags;
1716 err = sock_sendmsg(sock, &msg, len);
1717
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001718out_put:
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001719 fput_light(sock->file, fput_needed);
David S. Miller4387ff72007-02-08 15:06:08 -08001720out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 return err;
1722}
1723
1724/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001725 * Send a datagram down a socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 */
1727
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001728SYSCALL_DEFINE4(send, int, fd, void __user *, buff, size_t, len,
1729 unsigned, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730{
1731 return sys_sendto(fd, buff, len, flags, NULL, 0);
1732}
1733
1734/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001735 * Receive a frame from the socket and optionally record the address of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 * sender. We verify the buffers are writable and if needed move the
1737 * sender address from kernel to user space.
1738 */
1739
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001740SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, ubuf, size_t, size,
1741 unsigned, flags, struct sockaddr __user *, addr,
1742 int __user *, addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743{
1744 struct socket *sock;
1745 struct iovec iov;
1746 struct msghdr msg;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001747 struct sockaddr_storage address;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001748 int err, err2;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001749 int fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001751 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 if (!sock)
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001753 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001755 msg.msg_control = NULL;
1756 msg.msg_controllen = 0;
1757 msg.msg_iovlen = 1;
1758 msg.msg_iov = &iov;
1759 iov.iov_len = size;
1760 iov.iov_base = ubuf;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001761 msg.msg_name = (struct sockaddr *)&address;
1762 msg.msg_namelen = sizeof(address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 if (sock->file->f_flags & O_NONBLOCK)
1764 flags |= MSG_DONTWAIT;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001765 err = sock_recvmsg(sock, &msg, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001767 if (err >= 0 && addr != NULL) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001768 err2 = move_addr_to_user((struct sockaddr *)&address,
1769 msg.msg_namelen, addr, addr_len);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001770 if (err2 < 0)
1771 err = err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 }
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001773
1774 fput_light(sock->file, fput_needed);
David S. Miller4387ff72007-02-08 15:06:08 -08001775out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 return err;
1777}
1778
1779/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001780 * Receive a datagram from a socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 */
1782
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001783asmlinkage long sys_recv(int fd, void __user *ubuf, size_t size,
1784 unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785{
1786 return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
1787}
1788
1789/*
1790 * Set a socket option. Because we don't know the option lengths we have
1791 * to pass the user mode parameter for the protocols to sort out.
1792 */
1793
Heiko Carstens20f37032009-01-14 14:14:23 +01001794SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname,
1795 char __user *, optval, int, optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001797 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 struct socket *sock;
1799
1800 if (optlen < 0)
1801 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001802
1803 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1804 if (sock != NULL) {
1805 err = security_socket_setsockopt(sock, level, optname);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001806 if (err)
1807 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
1809 if (level == SOL_SOCKET)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001810 err =
1811 sock_setsockopt(sock, level, optname, optval,
1812 optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 else
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001814 err =
1815 sock->ops->setsockopt(sock, level, optname, optval,
1816 optlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001817out_put:
1818 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 }
1820 return err;
1821}
1822
1823/*
1824 * Get a socket option. Because we don't know the option lengths we have
1825 * to pass a user mode parameter for the protocols to sort out.
1826 */
1827
Heiko Carstens20f37032009-01-14 14:14:23 +01001828SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname,
1829 char __user *, optval, int __user *, optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001831 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 struct socket *sock;
1833
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001834 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1835 if (sock != NULL) {
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001836 err = security_socket_getsockopt(sock, level, optname);
1837 if (err)
1838 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
1840 if (level == SOL_SOCKET)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001841 err =
1842 sock_getsockopt(sock, level, optname, optval,
1843 optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 else
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001845 err =
1846 sock->ops->getsockopt(sock, level, optname, optval,
1847 optlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001848out_put:
1849 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 }
1851 return err;
1852}
1853
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854/*
1855 * Shutdown a socket.
1856 */
1857
Heiko Carstens754fe8d2009-01-14 14:14:09 +01001858SYSCALL_DEFINE2(shutdown, int, fd, int, how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001860 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 struct socket *sock;
1862
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001863 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1864 if (sock != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 err = security_socket_shutdown(sock, how);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001866 if (!err)
1867 err = sock->ops->shutdown(sock, how);
1868 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 }
1870 return err;
1871}
1872
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001873/* A couple of helpful macros for getting the address of the 32/64 bit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 * fields which are the same type (int / unsigned) on our platforms.
1875 */
1876#define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
1877#define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
1878#define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
1879
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880/*
1881 * BSD sendmsg interface
1882 */
1883
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001884SYSCALL_DEFINE3(sendmsg, int, fd, struct msghdr __user *, msg, unsigned, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885{
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001886 struct compat_msghdr __user *msg_compat =
1887 (struct compat_msghdr __user *)msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001889 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
Alex Williamsonb9d717a2005-09-26 14:28:02 -07001891 unsigned char ctl[sizeof(struct cmsghdr) + 20]
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001892 __attribute__ ((aligned(sizeof(__kernel_size_t))));
1893 /* 20 is size of ipv6_pktinfo */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 unsigned char *ctl_buf = ctl;
1895 struct msghdr msg_sys;
1896 int err, ctl_len, iov_size, total_len;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001897 int fput_needed;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001898
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 err = -EFAULT;
1900 if (MSG_CMSG_COMPAT & flags) {
1901 if (get_compat_msghdr(&msg_sys, msg_compat))
1902 return -EFAULT;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001903 }
1904 else if (copy_from_user(&msg_sys, msg, sizeof(struct msghdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 return -EFAULT;
1906
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001907 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001908 if (!sock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 goto out;
1910
1911 /* do not move before msg_sys is valid */
1912 err = -EMSGSIZE;
1913 if (msg_sys.msg_iovlen > UIO_MAXIOV)
1914 goto out_put;
1915
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001916 /* Check whether to allocate the iovec area */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 err = -ENOMEM;
1918 iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
1919 if (msg_sys.msg_iovlen > UIO_FASTIOV) {
1920 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
1921 if (!iov)
1922 goto out_put;
1923 }
1924
1925 /* This will also move the address data into kernel space */
1926 if (MSG_CMSG_COMPAT & flags) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001927 err = verify_compat_iovec(&msg_sys, iov,
1928 (struct sockaddr *)&address,
1929 VERIFY_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 } else
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001931 err = verify_iovec(&msg_sys, iov,
1932 (struct sockaddr *)&address,
1933 VERIFY_READ);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001934 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 goto out_freeiov;
1936 total_len = err;
1937
1938 err = -ENOBUFS;
1939
1940 if (msg_sys.msg_controllen > INT_MAX)
1941 goto out_freeiov;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001942 ctl_len = msg_sys.msg_controllen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001944 err =
1945 cmsghdr_from_user_compat_to_kern(&msg_sys, sock->sk, ctl,
1946 sizeof(ctl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 if (err)
1948 goto out_freeiov;
1949 ctl_buf = msg_sys.msg_control;
Al Viro8920e8f2005-09-07 18:28:51 -07001950 ctl_len = msg_sys.msg_controllen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 } else if (ctl_len) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001952 if (ctl_len > sizeof(ctl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001954 if (ctl_buf == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 goto out_freeiov;
1956 }
1957 err = -EFAULT;
1958 /*
1959 * Careful! Before this, msg_sys.msg_control contains a user pointer.
1960 * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
1961 * checking falls down on this.
1962 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001963 if (copy_from_user(ctl_buf, (void __user *)msg_sys.msg_control,
1964 ctl_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 goto out_freectl;
1966 msg_sys.msg_control = ctl_buf;
1967 }
1968 msg_sys.msg_flags = flags;
1969
1970 if (sock->file->f_flags & O_NONBLOCK)
1971 msg_sys.msg_flags |= MSG_DONTWAIT;
1972 err = sock_sendmsg(sock, &msg_sys, total_len);
1973
1974out_freectl:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001975 if (ctl_buf != ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 sock_kfree_s(sock->sk, ctl_buf, ctl_len);
1977out_freeiov:
1978 if (iov != iovstack)
1979 sock_kfree_s(sock->sk, iov, iov_size);
1980out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001981 fput_light(sock->file, fput_needed);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001982out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 return err;
1984}
1985
1986/*
1987 * BSD recvmsg interface
1988 */
1989
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001990SYSCALL_DEFINE3(recvmsg, int, fd, struct msghdr __user *, msg,
1991 unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992{
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001993 struct compat_msghdr __user *msg_compat =
1994 (struct compat_msghdr __user *)msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 struct socket *sock;
1996 struct iovec iovstack[UIO_FASTIOV];
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001997 struct iovec *iov = iovstack;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 struct msghdr msg_sys;
1999 unsigned long cmsg_ptr;
2000 int err, iov_size, total_len, len;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002001 int fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
2003 /* kernel mode address */
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07002004 struct sockaddr_storage addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
2006 /* user mode address pointers */
2007 struct sockaddr __user *uaddr;
2008 int __user *uaddr_len;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002009
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 if (MSG_CMSG_COMPAT & flags) {
2011 if (get_compat_msghdr(&msg_sys, msg_compat))
2012 return -EFAULT;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002013 }
2014 else if (copy_from_user(&msg_sys, msg, sizeof(struct msghdr)))
2015 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002017 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 if (!sock)
2019 goto out;
2020
2021 err = -EMSGSIZE;
2022 if (msg_sys.msg_iovlen > UIO_MAXIOV)
2023 goto out_put;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002024
2025 /* Check whether to allocate the iovec area */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 err = -ENOMEM;
2027 iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
2028 if (msg_sys.msg_iovlen > UIO_FASTIOV) {
2029 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
2030 if (!iov)
2031 goto out_put;
2032 }
2033
2034 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002035 * Save the user-mode address (verify_iovec will change the
2036 * kernel msghdr to use the kernel address space)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002038
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002039 uaddr = (__force void __user *)msg_sys.msg_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 uaddr_len = COMPAT_NAMELEN(msg);
2041 if (MSG_CMSG_COMPAT & flags) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07002042 err = verify_compat_iovec(&msg_sys, iov,
2043 (struct sockaddr *)&addr,
2044 VERIFY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 } else
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07002046 err = verify_iovec(&msg_sys, iov,
2047 (struct sockaddr *)&addr,
2048 VERIFY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 if (err < 0)
2050 goto out_freeiov;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002051 total_len = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
2053 cmsg_ptr = (unsigned long)msg_sys.msg_control;
Ulrich Drepper4a195422007-07-15 23:40:34 -07002054 msg_sys.msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002055
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 if (sock->file->f_flags & O_NONBLOCK)
2057 flags |= MSG_DONTWAIT;
2058 err = sock_recvmsg(sock, &msg_sys, total_len, flags);
2059 if (err < 0)
2060 goto out_freeiov;
2061 len = err;
2062
2063 if (uaddr != NULL) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07002064 err = move_addr_to_user((struct sockaddr *)&addr,
2065 msg_sys.msg_namelen, uaddr,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002066 uaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 if (err < 0)
2068 goto out_freeiov;
2069 }
David S. Miller37f7f422005-09-16 16:51:01 -07002070 err = __put_user((msg_sys.msg_flags & ~MSG_CMSG_COMPAT),
2071 COMPAT_FLAGS(msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 if (err)
2073 goto out_freeiov;
2074 if (MSG_CMSG_COMPAT & flags)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002075 err = __put_user((unsigned long)msg_sys.msg_control - cmsg_ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 &msg_compat->msg_controllen);
2077 else
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002078 err = __put_user((unsigned long)msg_sys.msg_control - cmsg_ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 &msg->msg_controllen);
2080 if (err)
2081 goto out_freeiov;
2082 err = len;
2083
2084out_freeiov:
2085 if (iov != iovstack)
2086 sock_kfree_s(sock->sk, iov, iov_size);
2087out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002088 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089out:
2090 return err;
2091}
2092
2093#ifdef __ARCH_WANT_SYS_SOCKETCALL
2094
2095/* Argument list sizes for sys_socketcall */
2096#define AL(x) ((x) * sizeof(unsigned long))
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07002097static const unsigned char nargs[19]={
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002098 AL(0),AL(3),AL(3),AL(3),AL(2),AL(3),
2099 AL(3),AL(3),AL(4),AL(4),AL(4),AL(6),
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07002100 AL(6),AL(2),AL(5),AL(5),AL(3),AL(3),
Ulrich Drepperde11def2008-11-19 15:36:14 -08002101 AL(4)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002102};
2103
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104#undef AL
2105
2106/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002107 * System call vectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 *
2109 * Argument checking cleaned up. Saved 20% in size.
2110 * This function doesn't need to set the kernel lock because
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002111 * it is set by the callees.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 */
2113
Heiko Carstens3e0fa652009-01-14 14:14:24 +01002114SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115{
2116 unsigned long a[6];
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002117 unsigned long a0, a1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 int err;
Arjan van de Ven47379052009-09-28 12:57:44 -07002119 unsigned int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
Ulrich Drepperde11def2008-11-19 15:36:14 -08002121 if (call < 1 || call > SYS_ACCEPT4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 return -EINVAL;
2123
Arjan van de Ven47379052009-09-28 12:57:44 -07002124 len = nargs[call];
2125 if (len > sizeof(a))
2126 return -EINVAL;
2127
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 /* copy_from_user should be SMP safe. */
Arjan van de Ven47379052009-09-28 12:57:44 -07002129 if (copy_from_user(a, args, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 return -EFAULT;
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002131
Al Virof3298dc2008-12-10 03:16:51 -05002132 audit_socketcall(nargs[call] / sizeof(unsigned long), a);
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002133
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002134 a0 = a[0];
2135 a1 = a[1];
2136
2137 switch (call) {
2138 case SYS_SOCKET:
2139 err = sys_socket(a0, a1, a[2]);
2140 break;
2141 case SYS_BIND:
2142 err = sys_bind(a0, (struct sockaddr __user *)a1, a[2]);
2143 break;
2144 case SYS_CONNECT:
2145 err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
2146 break;
2147 case SYS_LISTEN:
2148 err = sys_listen(a0, a1);
2149 break;
2150 case SYS_ACCEPT:
Ulrich Drepperde11def2008-11-19 15:36:14 -08002151 err = sys_accept4(a0, (struct sockaddr __user *)a1,
2152 (int __user *)a[2], 0);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002153 break;
2154 case SYS_GETSOCKNAME:
2155 err =
2156 sys_getsockname(a0, (struct sockaddr __user *)a1,
2157 (int __user *)a[2]);
2158 break;
2159 case SYS_GETPEERNAME:
2160 err =
2161 sys_getpeername(a0, (struct sockaddr __user *)a1,
2162 (int __user *)a[2]);
2163 break;
2164 case SYS_SOCKETPAIR:
2165 err = sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
2166 break;
2167 case SYS_SEND:
2168 err = sys_send(a0, (void __user *)a1, a[2], a[3]);
2169 break;
2170 case SYS_SENDTO:
2171 err = sys_sendto(a0, (void __user *)a1, a[2], a[3],
2172 (struct sockaddr __user *)a[4], a[5]);
2173 break;
2174 case SYS_RECV:
2175 err = sys_recv(a0, (void __user *)a1, a[2], a[3]);
2176 break;
2177 case SYS_RECVFROM:
2178 err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
2179 (struct sockaddr __user *)a[4],
2180 (int __user *)a[5]);
2181 break;
2182 case SYS_SHUTDOWN:
2183 err = sys_shutdown(a0, a1);
2184 break;
2185 case SYS_SETSOCKOPT:
2186 err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]);
2187 break;
2188 case SYS_GETSOCKOPT:
2189 err =
2190 sys_getsockopt(a0, a1, a[2], (char __user *)a[3],
2191 (int __user *)a[4]);
2192 break;
2193 case SYS_SENDMSG:
2194 err = sys_sendmsg(a0, (struct msghdr __user *)a1, a[2]);
2195 break;
2196 case SYS_RECVMSG:
2197 err = sys_recvmsg(a0, (struct msghdr __user *)a1, a[2]);
2198 break;
Ulrich Drepperde11def2008-11-19 15:36:14 -08002199 case SYS_ACCEPT4:
2200 err = sys_accept4(a0, (struct sockaddr __user *)a1,
2201 (int __user *)a[2], a[3]);
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07002202 break;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002203 default:
2204 err = -EINVAL;
2205 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 }
2207 return err;
2208}
2209
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002210#endif /* __ARCH_WANT_SYS_SOCKETCALL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002212/**
2213 * sock_register - add a socket protocol handler
2214 * @ops: description of protocol
2215 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 * This function is called by a protocol handler that wants to
2217 * advertise its address family, and have it linked into the
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002218 * socket interface. The value ops->family coresponds to the
2219 * socket system call protocol family.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 */
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002221int sock_register(const struct net_proto_family *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222{
2223 int err;
2224
2225 if (ops->family >= NPROTO) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002226 printk(KERN_CRIT "protocol %d >= NPROTO(%d)\n", ops->family,
2227 NPROTO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 return -ENOBUFS;
2229 }
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002230
2231 spin_lock(&net_family_lock);
2232 if (net_families[ops->family])
2233 err = -EEXIST;
2234 else {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002235 net_families[ops->family] = ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 err = 0;
2237 }
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002238 spin_unlock(&net_family_lock);
2239
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002240 printk(KERN_INFO "NET: Registered protocol family %d\n", ops->family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 return err;
2242}
2243
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002244/**
2245 * sock_unregister - remove a protocol handler
2246 * @family: protocol family to remove
2247 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 * This function is called by a protocol handler that wants to
2249 * remove its address family, and have it unlinked from the
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002250 * new socket creation.
2251 *
2252 * If protocol handler is a module, then it can use module reference
2253 * counts to protect against new references. If protocol handler is not
2254 * a module then it needs to provide its own protection in
2255 * the ops->create routine.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 */
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002257void sock_unregister(int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258{
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002259 BUG_ON(family < 0 || family >= NPROTO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002261 spin_lock(&net_family_lock);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002262 net_families[family] = NULL;
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002263 spin_unlock(&net_family_lock);
2264
2265 synchronize_rcu();
2266
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002267 printk(KERN_INFO "NET: Unregistered protocol family %d\n", family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268}
2269
Andi Kleen77d76ea2005-12-22 12:43:42 -08002270static int __init sock_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271{
2272 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002273 * Initialize sock SLAB cache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002275
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 sk_init();
2277
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002279 * Initialize skbuff SLAB cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 */
2281 skb_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282
2283 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002284 * Initialize the protocols module.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 */
2286
2287 init_inodecache();
2288 register_filesystem(&sock_fs_type);
2289 sock_mnt = kern_mount(&sock_fs_type);
Andi Kleen77d76ea2005-12-22 12:43:42 -08002290
2291 /* The real protocol initialization is performed in later initcalls.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 */
2293
2294#ifdef CONFIG_NETFILTER
2295 netfilter_init();
2296#endif
David S. Millercbeb3212005-12-22 12:58:55 -08002297
2298 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299}
2300
Andi Kleen77d76ea2005-12-22 12:43:42 -08002301core_initcall(sock_init); /* early initcall */
2302
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303#ifdef CONFIG_PROC_FS
2304void socket_seq_show(struct seq_file *seq)
2305{
2306 int cpu;
2307 int counter = 0;
2308
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -07002309 for_each_possible_cpu(cpu)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002310 counter += per_cpu(sockets_in_use, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
2312 /* It can be negative, by the way. 8) */
2313 if (counter < 0)
2314 counter = 0;
2315
2316 seq_printf(seq, "sockets: used %d\n", counter);
2317}
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002318#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319
Shaun Pereira89bbfc92006-03-21 23:58:08 -08002320#ifdef CONFIG_COMPAT
2321static long compat_sock_ioctl(struct file *file, unsigned cmd,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002322 unsigned long arg)
Shaun Pereira89bbfc92006-03-21 23:58:08 -08002323{
2324 struct socket *sock = file->private_data;
2325 int ret = -ENOIOCTLCMD;
David S. Miller87de87d2008-06-03 09:14:03 -07002326 struct sock *sk;
2327 struct net *net;
2328
2329 sk = sock->sk;
2330 net = sock_net(sk);
Shaun Pereira89bbfc92006-03-21 23:58:08 -08002331
2332 if (sock->ops->compat_ioctl)
2333 ret = sock->ops->compat_ioctl(sock, cmd, arg);
2334
David S. Miller87de87d2008-06-03 09:14:03 -07002335 if (ret == -ENOIOCTLCMD &&
2336 (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST))
2337 ret = compat_wext_handle_ioctl(net, cmd, arg);
2338
Shaun Pereira89bbfc92006-03-21 23:58:08 -08002339 return ret;
2340}
2341#endif
2342
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07002343int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen)
2344{
2345 return sock->ops->bind(sock, addr, addrlen);
2346}
2347
2348int kernel_listen(struct socket *sock, int backlog)
2349{
2350 return sock->ops->listen(sock, backlog);
2351}
2352
2353int kernel_accept(struct socket *sock, struct socket **newsock, int flags)
2354{
2355 struct sock *sk = sock->sk;
2356 int err;
2357
2358 err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
2359 newsock);
2360 if (err < 0)
2361 goto done;
2362
2363 err = sock->ops->accept(sock, *newsock, flags);
2364 if (err < 0) {
2365 sock_release(*newsock);
Tony Battersbyfa8705b2007-10-10 21:09:04 -07002366 *newsock = NULL;
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07002367 goto done;
2368 }
2369
2370 (*newsock)->ops = sock->ops;
Wei Yongjun1b085342008-12-18 19:35:10 -08002371 __module_get((*newsock)->ops->owner);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07002372
2373done:
2374 return err;
2375}
2376
2377int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
YOSHIFUJI Hideaki4768fbc2007-02-09 23:25:31 +09002378 int flags)
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07002379{
2380 return sock->ops->connect(sock, addr, addrlen, flags);
2381}
2382
2383int kernel_getsockname(struct socket *sock, struct sockaddr *addr,
2384 int *addrlen)
2385{
2386 return sock->ops->getname(sock, addr, addrlen, 0);
2387}
2388
2389int kernel_getpeername(struct socket *sock, struct sockaddr *addr,
2390 int *addrlen)
2391{
2392 return sock->ops->getname(sock, addr, addrlen, 1);
2393}
2394
2395int kernel_getsockopt(struct socket *sock, int level, int optname,
2396 char *optval, int *optlen)
2397{
2398 mm_segment_t oldfs = get_fs();
2399 int err;
2400
2401 set_fs(KERNEL_DS);
2402 if (level == SOL_SOCKET)
2403 err = sock_getsockopt(sock, level, optname, optval, optlen);
2404 else
2405 err = sock->ops->getsockopt(sock, level, optname, optval,
2406 optlen);
2407 set_fs(oldfs);
2408 return err;
2409}
2410
2411int kernel_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07002412 char *optval, unsigned int optlen)
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07002413{
2414 mm_segment_t oldfs = get_fs();
2415 int err;
2416
2417 set_fs(KERNEL_DS);
2418 if (level == SOL_SOCKET)
2419 err = sock_setsockopt(sock, level, optname, optval, optlen);
2420 else
2421 err = sock->ops->setsockopt(sock, level, optname, optval,
2422 optlen);
2423 set_fs(oldfs);
2424 return err;
2425}
2426
2427int kernel_sendpage(struct socket *sock, struct page *page, int offset,
2428 size_t size, int flags)
2429{
2430 if (sock->ops->sendpage)
2431 return sock->ops->sendpage(sock, page, offset, size, flags);
2432
2433 return sock_no_sendpage(sock, page, offset, size, flags);
2434}
2435
2436int kernel_sock_ioctl(struct socket *sock, int cmd, unsigned long arg)
2437{
2438 mm_segment_t oldfs = get_fs();
2439 int err;
2440
2441 set_fs(KERNEL_DS);
2442 err = sock->ops->ioctl(sock, cmd, arg);
2443 set_fs(oldfs);
2444
2445 return err;
2446}
2447
Trond Myklebust91cf45f2007-11-12 18:10:39 -08002448int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how)
2449{
2450 return sock->ops->shutdown(sock, how);
2451}
2452
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453EXPORT_SYMBOL(sock_create);
2454EXPORT_SYMBOL(sock_create_kern);
2455EXPORT_SYMBOL(sock_create_lite);
2456EXPORT_SYMBOL(sock_map_fd);
2457EXPORT_SYMBOL(sock_recvmsg);
2458EXPORT_SYMBOL(sock_register);
2459EXPORT_SYMBOL(sock_release);
2460EXPORT_SYMBOL(sock_sendmsg);
2461EXPORT_SYMBOL(sock_unregister);
2462EXPORT_SYMBOL(sock_wake_async);
2463EXPORT_SYMBOL(sockfd_lookup);
2464EXPORT_SYMBOL(kernel_sendmsg);
2465EXPORT_SYMBOL(kernel_recvmsg);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07002466EXPORT_SYMBOL(kernel_bind);
2467EXPORT_SYMBOL(kernel_listen);
2468EXPORT_SYMBOL(kernel_accept);
2469EXPORT_SYMBOL(kernel_connect);
2470EXPORT_SYMBOL(kernel_getsockname);
2471EXPORT_SYMBOL(kernel_getpeername);
2472EXPORT_SYMBOL(kernel_getsockopt);
2473EXPORT_SYMBOL(kernel_setsockopt);
2474EXPORT_SYMBOL(kernel_sendpage);
2475EXPORT_SYMBOL(kernel_sock_ioctl);
Trond Myklebust91cf45f2007-11-12 18:10:39 -08002476EXPORT_SYMBOL(kernel_sock_shutdown);