blob: 1bc4167e0da8da20c2364c14ea48b73a0cca28df [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>
Stephen Hemminger55737fd2006-09-01 00:23:39 -070066#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include <linux/netdevice.h>
68#include <linux/proc_fs.h>
69#include <linux/seq_file.h>
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080070#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#include <linux/wanrouter.h>
72#include <linux/if_bridge.h>
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -030073#include <linux/if_frad.h>
74#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#include <linux/init.h>
76#include <linux/poll.h>
77#include <linux/cache.h>
78#include <linux/module.h>
79#include <linux/highmem.h>
80#include <linux/divert.h>
81#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89#include <asm/uaccess.h>
90#include <asm/unistd.h>
91
92#include <net/compat.h>
93
94#include <net/sock.h>
95#include <linux/netfilter.h>
96
97static int sock_no_open(struct inode *irrelevant, struct file *dontcare);
98static ssize_t sock_aio_read(struct kiocb *iocb, char __user *buf,
Stephen Hemminger89bddce2006-09-01 00:19:31 -070099 size_t size, loff_t pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100static ssize_t sock_aio_write(struct kiocb *iocb, const char __user *buf,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700101 size_t size, loff_t pos);
102static int sock_mmap(struct file *file, struct vm_area_struct *vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104static int sock_close(struct inode *inode, struct file *file);
105static unsigned int sock_poll(struct file *file,
106 struct poll_table_struct *wait);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700107static long sock_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
Shaun Pereira89bbfc92006-03-21 23:58:08 -0800108#ifdef CONFIG_COMPAT
109static long compat_sock_ioctl(struct file *file,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700110 unsigned int cmd, unsigned long arg);
Shaun Pereira89bbfc92006-03-21 23:58:08 -0800111#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112static int sock_fasync(int fd, struct file *filp, int on);
113static ssize_t sock_readv(struct file *file, const struct iovec *vector,
114 unsigned long count, loff_t *ppos);
115static ssize_t sock_writev(struct file *file, const struct iovec *vector,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700116 unsigned long count, loff_t *ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117static ssize_t sock_sendpage(struct file *file, struct page *page,
118 int offset, size_t size, loff_t *ppos, int more);
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120/*
121 * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
122 * in the operation structures but are done directly via the socketcall() multiplexor.
123 */
124
125static struct file_operations socket_file_ops = {
126 .owner = THIS_MODULE,
127 .llseek = no_llseek,
128 .aio_read = sock_aio_read,
129 .aio_write = sock_aio_write,
130 .poll = sock_poll,
131 .unlocked_ioctl = sock_ioctl,
Shaun Pereira89bbfc92006-03-21 23:58:08 -0800132#ifdef CONFIG_COMPAT
133 .compat_ioctl = compat_sock_ioctl,
134#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 .mmap = sock_mmap,
136 .open = sock_no_open, /* special open code to disallow open via /proc */
137 .release = sock_close,
138 .fasync = sock_fasync,
139 .readv = sock_readv,
140 .writev = sock_writev,
Jens Axboe5274f052006-03-30 15:15:30 +0200141 .sendpage = sock_sendpage,
142 .splice_write = generic_splice_sendpage,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143};
144
145/*
146 * The protocol list. Each protocol is registered in here.
147 */
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149static DEFINE_SPINLOCK(net_family_lock);
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -0700150static const struct net_proto_family *net_families[NPROTO] __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152/*
153 * Statistics counters of the socket lists
154 */
155
156static DEFINE_PER_CPU(int, sockets_in_use) = 0;
157
158/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700159 * Support routines.
160 * Move socket addresses back and forth across the kernel/user
161 * divide and look after the messy bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 */
163
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700164#define MAX_SOCK_ADDR 128 /* 108 for Unix domain -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 16 for IP, 16 for IPX,
166 24 for IPv6,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700167 about 80 for AX.25
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 must be at least one bigger than
169 the AF_UNIX size (see net/unix/af_unix.c
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700170 :unix_mkname()).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173/**
174 * move_addr_to_kernel - copy a socket address into kernel space
175 * @uaddr: Address in user space
176 * @kaddr: Address in kernel space
177 * @ulen: Length in user space
178 *
179 * The address is copied into kernel space. If the provided address is
180 * too long an error code of -EINVAL is returned. If the copy gives
181 * invalid addresses -EFAULT is returned. On a success 0 is returned.
182 */
183
184int move_addr_to_kernel(void __user *uaddr, int ulen, void *kaddr)
185{
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700186 if (ulen < 0 || ulen > MAX_SOCK_ADDR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700188 if (ulen == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 return 0;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700190 if (copy_from_user(kaddr, uaddr, ulen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 return -EFAULT;
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100192 return audit_sockaddr(ulen, kaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
194
195/**
196 * move_addr_to_user - copy an address to user space
197 * @kaddr: kernel space address
198 * @klen: length of address in kernel
199 * @uaddr: user space address
200 * @ulen: pointer to user length field
201 *
202 * The value pointed to by ulen on entry is the buffer length available.
203 * This is overwritten with the buffer space used. -EINVAL is returned
204 * if an overlong buffer is specified or a negative buffer size. -EFAULT
205 * is returned if either the buffer or the length field are not
206 * accessible.
207 * After copying the data up to the limit the user specifies, the true
208 * length of the data is written over the length limit the user
209 * specified. Zero is returned for a success.
210 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700211
212int move_addr_to_user(void *kaddr, int klen, void __user *uaddr,
213 int __user *ulen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
215 int err;
216 int len;
217
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700218 err = get_user(len, ulen);
219 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 return err;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700221 if (len > klen)
222 len = klen;
223 if (len < 0 || len > MAX_SOCK_ADDR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700225 if (len) {
Steve Grubbd6fe3942006-03-30 12:20:22 -0500226 if (audit_sockaddr(klen, kaddr))
227 return -ENOMEM;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700228 if (copy_to_user(uaddr, kaddr, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 return -EFAULT;
230 }
231 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700232 * "fromlen shall refer to the value before truncation.."
233 * 1003.1g
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 */
235 return __put_user(klen, ulen);
236}
237
238#define SOCKFS_MAGIC 0x534F434B
239
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700240static kmem_cache_t *sock_inode_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242static struct inode *sock_alloc_inode(struct super_block *sb)
243{
244 struct socket_alloc *ei;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700245
246 ei = kmem_cache_alloc(sock_inode_cachep, SLAB_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 if (!ei)
248 return NULL;
249 init_waitqueue_head(&ei->socket.wait);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 ei->socket.fasync_list = NULL;
252 ei->socket.state = SS_UNCONNECTED;
253 ei->socket.flags = 0;
254 ei->socket.ops = NULL;
255 ei->socket.sk = NULL;
256 ei->socket.file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 return &ei->vfs_inode;
259}
260
261static void sock_destroy_inode(struct inode *inode)
262{
263 kmem_cache_free(sock_inode_cachep,
264 container_of(inode, struct socket_alloc, vfs_inode));
265}
266
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700267static void init_once(void *foo, kmem_cache_t *cachep, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700269 struct socket_alloc *ei = (struct socket_alloc *)foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700271 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR))
272 == SLAB_CTOR_CONSTRUCTOR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 inode_init_once(&ei->vfs_inode);
274}
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276static int init_inodecache(void)
277{
278 sock_inode_cachep = kmem_cache_create("sock_inode_cache",
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700279 sizeof(struct socket_alloc),
280 0,
281 (SLAB_HWCACHE_ALIGN |
282 SLAB_RECLAIM_ACCOUNT |
283 SLAB_MEM_SPREAD),
284 init_once,
285 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 if (sock_inode_cachep == NULL)
287 return -ENOMEM;
288 return 0;
289}
290
291static struct super_operations sockfs_ops = {
292 .alloc_inode = sock_alloc_inode,
293 .destroy_inode =sock_destroy_inode,
294 .statfs = simple_statfs,
295};
296
David Howells454e2392006-06-23 02:02:57 -0700297static int sockfs_get_sb(struct file_system_type *fs_type,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700298 int flags, const char *dev_name, void *data,
299 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
David Howells454e2392006-06-23 02:02:57 -0700301 return get_sb_pseudo(fs_type, "socket:", &sockfs_ops, SOCKFS_MAGIC,
302 mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
Eric Dumazetba899662005-08-26 12:05:31 -0700305static struct vfsmount *sock_mnt __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307static struct file_system_type sock_fs_type = {
308 .name = "sockfs",
309 .get_sb = sockfs_get_sb,
310 .kill_sb = kill_anon_super,
311};
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313static int sockfs_delete_dentry(struct dentry *dentry)
314{
315 return 1;
316}
317static struct dentry_operations sockfs_dentry_operations = {
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700318 .d_delete = sockfs_delete_dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319};
320
321/*
322 * Obtains the first available file descriptor and sets it up for use.
323 *
David S. Miller39d8c1b2006-03-20 17:13:49 -0800324 * These functions create file structures and maps them to fd space
325 * of the current process. On success it returns file descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 * and file struct implicitly stored in sock->file.
327 * Note that another thread may close file descriptor before we return
328 * from this function. We use the fact that now we do not refer
329 * to socket after mapping. If one day we will need it, this
330 * function will increment ref. count on file by 1.
331 *
332 * In any case returned fd MAY BE not valid!
333 * This race condition is unavoidable
334 * with shared fd spaces, we cannot solve it inside kernel,
335 * but we take care of internal coherence yet.
336 */
337
David S. Miller39d8c1b2006-03-20 17:13:49 -0800338static int sock_alloc_fd(struct file **filep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
340 int fd;
David S. Miller39d8c1b2006-03-20 17:13:49 -0800341
342 fd = get_unused_fd();
343 if (likely(fd >= 0)) {
344 struct file *file = get_empty_filp();
345
346 *filep = file;
347 if (unlikely(!file)) {
348 put_unused_fd(fd);
349 return -ENFILE;
350 }
351 } else
352 *filep = NULL;
353 return fd;
354}
355
356static int sock_attach_fd(struct socket *sock, struct file *file)
357{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 struct qstr this;
359 char name[32];
360
David S. Miller39d8c1b2006-03-20 17:13:49 -0800361 this.len = sprintf(name, "[%lu]", SOCK_INODE(sock)->i_ino);
362 this.name = name;
363 this.hash = SOCK_INODE(sock)->i_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
David S. Miller39d8c1b2006-03-20 17:13:49 -0800365 file->f_dentry = d_alloc(sock_mnt->mnt_sb->s_root, &this);
366 if (unlikely(!file->f_dentry))
367 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
David S. Miller39d8c1b2006-03-20 17:13:49 -0800369 file->f_dentry->d_op = &sockfs_dentry_operations;
370 d_add(file->f_dentry, SOCK_INODE(sock));
371 file->f_vfsmnt = mntget(sock_mnt);
372 file->f_mapping = file->f_dentry->d_inode->i_mapping;
373
374 sock->file = file;
375 file->f_op = SOCK_INODE(sock)->i_fop = &socket_file_ops;
376 file->f_mode = FMODE_READ | FMODE_WRITE;
377 file->f_flags = O_RDWR;
378 file->f_pos = 0;
379 file->private_data = sock;
380
381 return 0;
382}
383
384int sock_map_fd(struct socket *sock)
385{
386 struct file *newfile;
387 int fd = sock_alloc_fd(&newfile);
388
389 if (likely(fd >= 0)) {
390 int err = sock_attach_fd(sock, newfile);
391
392 if (unlikely(err < 0)) {
393 put_filp(newfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 put_unused_fd(fd);
David S. Miller39d8c1b2006-03-20 17:13:49 -0800395 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 }
David S. Miller39d8c1b2006-03-20 17:13:49 -0800397 fd_install(fd, newfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 return fd;
400}
401
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800402static struct socket *sock_from_file(struct file *file, int *err)
403{
404 struct inode *inode;
405 struct socket *sock;
406
407 if (file->f_op == &socket_file_ops)
408 return file->private_data; /* set in sock_map_fd */
409
410 inode = file->f_dentry->d_inode;
411 if (!S_ISSOCK(inode->i_mode)) {
412 *err = -ENOTSOCK;
413 return NULL;
414 }
415
416 sock = SOCKET_I(inode);
417 if (sock->file != file) {
418 printk(KERN_ERR "socki_lookup: socket file changed!\n");
419 sock->file = file;
420 }
421 return sock;
422}
423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424/**
425 * sockfd_lookup - Go from a file number to its socket slot
426 * @fd: file handle
427 * @err: pointer to an error code return
428 *
429 * The file handle passed in is locked and the socket it is bound
430 * too is returned. If an error occurs the err pointer is overwritten
431 * with a negative errno code and NULL is returned. The function checks
432 * for both invalid handles and passing a handle which is not a socket.
433 *
434 * On a success the socket object pointer is returned.
435 */
436
437struct socket *sockfd_lookup(int fd, int *err)
438{
439 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 struct socket *sock;
441
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700442 file = fget(fd);
443 if (!file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 *err = -EBADF;
445 return NULL;
446 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700447
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800448 sock = sock_from_file(file, err);
449 if (!sock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 fput(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 return sock;
452}
453
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800454static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed)
455{
456 struct file *file;
457 struct socket *sock;
458
Hua Zhong36725582006-04-19 15:25:02 -0700459 *err = -EBADF;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800460 file = fget_light(fd, fput_needed);
461 if (file) {
462 sock = sock_from_file(file, err);
463 if (sock)
464 return sock;
465 fput_light(file, *fput_needed);
466 }
467 return NULL;
468}
469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470/**
471 * sock_alloc - allocate a socket
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700472 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 * Allocate a new inode and socket object. The two are bound together
474 * and initialised. The socket is then returned. If we are out of inodes
475 * NULL is returned.
476 */
477
478static struct socket *sock_alloc(void)
479{
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700480 struct inode *inode;
481 struct socket *sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483 inode = new_inode(sock_mnt->mnt_sb);
484 if (!inode)
485 return NULL;
486
487 sock = SOCKET_I(inode);
488
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700489 inode->i_mode = S_IFSOCK | S_IRWXUGO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 inode->i_uid = current->fsuid;
491 inode->i_gid = current->fsgid;
492
493 get_cpu_var(sockets_in_use)++;
494 put_cpu_var(sockets_in_use);
495 return sock;
496}
497
498/*
499 * In theory you can't get an open on this inode, but /proc provides
500 * a back door. Remember to keep it shut otherwise you'll let the
501 * creepy crawlies in.
502 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504static int sock_no_open(struct inode *irrelevant, struct file *dontcare)
505{
506 return -ENXIO;
507}
508
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800509const struct file_operations bad_sock_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 .owner = THIS_MODULE,
511 .open = sock_no_open,
512};
513
514/**
515 * sock_release - close a socket
516 * @sock: socket to close
517 *
518 * The socket is released from the protocol stack if it has a release
519 * callback, and the inode is then released if the socket is bound to
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700520 * an inode not a file.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523void sock_release(struct socket *sock)
524{
525 if (sock->ops) {
526 struct module *owner = sock->ops->owner;
527
528 sock->ops->release(sock);
529 sock->ops = NULL;
530 module_put(owner);
531 }
532
533 if (sock->fasync_list)
534 printk(KERN_ERR "sock_release: fasync list not empty!\n");
535
536 get_cpu_var(sockets_in_use)--;
537 put_cpu_var(sockets_in_use);
538 if (!sock->file) {
539 iput(SOCK_INODE(sock));
540 return;
541 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700542 sock->file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700545static inline int __sock_sendmsg(struct kiocb *iocb, struct socket *sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 struct msghdr *msg, size_t size)
547{
548 struct sock_iocb *si = kiocb_to_siocb(iocb);
549 int err;
550
551 si->sock = sock;
552 si->scm = NULL;
553 si->msg = msg;
554 si->size = size;
555
556 err = security_socket_sendmsg(sock, msg, size);
557 if (err)
558 return err;
559
560 return sock->ops->sendmsg(iocb, sock, msg, size);
561}
562
563int sock_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
564{
565 struct kiocb iocb;
566 struct sock_iocb siocb;
567 int ret;
568
569 init_sync_kiocb(&iocb, NULL);
570 iocb.private = &siocb;
571 ret = __sock_sendmsg(&iocb, sock, msg, size);
572 if (-EIOCBQUEUED == ret)
573 ret = wait_on_sync_kiocb(&iocb);
574 return ret;
575}
576
577int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
578 struct kvec *vec, size_t num, size_t size)
579{
580 mm_segment_t oldfs = get_fs();
581 int result;
582
583 set_fs(KERNEL_DS);
584 /*
585 * the following is safe, since for compiler definitions of kvec and
586 * iovec are identical, yielding the same in-core layout and alignment
587 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700588 msg->msg_iov = (struct iovec *)vec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 msg->msg_iovlen = num;
590 result = sock_sendmsg(sock, msg, size);
591 set_fs(oldfs);
592 return result;
593}
594
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700595static inline int __sock_recvmsg(struct kiocb *iocb, struct socket *sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 struct msghdr *msg, size_t size, int flags)
597{
598 int err;
599 struct sock_iocb *si = kiocb_to_siocb(iocb);
600
601 si->sock = sock;
602 si->scm = NULL;
603 si->msg = msg;
604 si->size = size;
605 si->flags = flags;
606
607 err = security_socket_recvmsg(sock, msg, size, flags);
608 if (err)
609 return err;
610
611 return sock->ops->recvmsg(iocb, sock, msg, size, flags);
612}
613
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700614int sock_recvmsg(struct socket *sock, struct msghdr *msg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 size_t size, int flags)
616{
617 struct kiocb iocb;
618 struct sock_iocb siocb;
619 int ret;
620
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700621 init_sync_kiocb(&iocb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 iocb.private = &siocb;
623 ret = __sock_recvmsg(&iocb, sock, msg, size, flags);
624 if (-EIOCBQUEUED == ret)
625 ret = wait_on_sync_kiocb(&iocb);
626 return ret;
627}
628
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700629int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
630 struct kvec *vec, size_t num, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
632 mm_segment_t oldfs = get_fs();
633 int result;
634
635 set_fs(KERNEL_DS);
636 /*
637 * the following is safe, since for compiler definitions of kvec and
638 * iovec are identical, yielding the same in-core layout and alignment
639 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700640 msg->msg_iov = (struct iovec *)vec, msg->msg_iovlen = num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 result = sock_recvmsg(sock, msg, size, flags);
642 set_fs(oldfs);
643 return result;
644}
645
646static void sock_aio_dtor(struct kiocb *iocb)
647{
648 kfree(iocb->private);
649}
650
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -0300651static ssize_t sock_sendpage(struct file *file, struct page *page,
652 int offset, size_t size, loff_t *ppos, int more)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
654 struct socket *sock;
655 int flags;
656
Eric Dumazetb69aee02005-09-06 14:42:45 -0700657 sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659 flags = !(file->f_flags & O_NONBLOCK) ? 0 : MSG_DONTWAIT;
660 if (more)
661 flags |= MSG_MORE;
662
663 return sock->ops->sendpage(sock, page, offset, size, flags);
664}
665
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800666static struct sock_iocb *alloc_sock_iocb(struct kiocb *iocb,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700667 char __user *ubuf, size_t size,
668 struct sock_iocb *siocb)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800669{
670 if (!is_sync_kiocb(iocb)) {
671 siocb = kmalloc(sizeof(*siocb), GFP_KERNEL);
672 if (!siocb)
673 return NULL;
674 iocb->ki_dtor = sock_aio_dtor;
675 }
676
677 siocb->kiocb = iocb;
678 siocb->async_iov.iov_base = ubuf;
679 siocb->async_iov.iov_len = size;
680
681 iocb->private = siocb;
682 return siocb;
683}
684
685static ssize_t do_sock_read(struct msghdr *msg, struct kiocb *iocb,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700686 struct file *file, struct iovec *iov,
687 unsigned long nr_segs)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800688{
689 struct socket *sock = file->private_data;
690 size_t size = 0;
691 int i;
692
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700693 for (i = 0; i < nr_segs; i++)
694 size += iov[i].iov_len;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800695
696 msg->msg_name = NULL;
697 msg->msg_namelen = 0;
698 msg->msg_control = NULL;
699 msg->msg_controllen = 0;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700700 msg->msg_iov = (struct iovec *)iov;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800701 msg->msg_iovlen = nr_segs;
702 msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
703
704 return __sock_recvmsg(iocb, sock, msg, size, msg->msg_flags);
705}
706
707static ssize_t sock_readv(struct file *file, const struct iovec *iov,
708 unsigned long nr_segs, loff_t *ppos)
709{
710 struct kiocb iocb;
711 struct sock_iocb siocb;
712 struct msghdr msg;
713 int ret;
714
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700715 init_sync_kiocb(&iocb, NULL);
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800716 iocb.private = &siocb;
717
718 ret = do_sock_read(&msg, &iocb, file, (struct iovec *)iov, nr_segs);
719 if (-EIOCBQUEUED == ret)
720 ret = wait_on_sync_kiocb(&iocb);
721 return ret;
722}
723
724static ssize_t sock_aio_read(struct kiocb *iocb, char __user *ubuf,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700725 size_t count, loff_t pos)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800726{
727 struct sock_iocb siocb, *x;
728
729 if (pos != 0)
730 return -ESPIPE;
731 if (count == 0) /* Match SYS5 behaviour */
732 return 0;
733
734 x = alloc_sock_iocb(iocb, ubuf, count, &siocb);
735 if (!x)
736 return -ENOMEM;
737 return do_sock_read(&x->async_msg, iocb, iocb->ki_filp,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700738 &x->async_iov, 1);
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800739}
740
741static ssize_t do_sock_write(struct msghdr *msg, struct kiocb *iocb,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700742 struct file *file, struct iovec *iov,
743 unsigned long nr_segs)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800744{
745 struct socket *sock = file->private_data;
746 size_t size = 0;
747 int i;
748
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700749 for (i = 0; i < nr_segs; i++)
750 size += iov[i].iov_len;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800751
752 msg->msg_name = NULL;
753 msg->msg_namelen = 0;
754 msg->msg_control = NULL;
755 msg->msg_controllen = 0;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700756 msg->msg_iov = (struct iovec *)iov;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800757 msg->msg_iovlen = nr_segs;
758 msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
759 if (sock->type == SOCK_SEQPACKET)
760 msg->msg_flags |= MSG_EOR;
761
762 return __sock_sendmsg(iocb, sock, msg, size);
763}
764
765static ssize_t sock_writev(struct file *file, const struct iovec *iov,
766 unsigned long nr_segs, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
768 struct msghdr msg;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800769 struct kiocb iocb;
770 struct sock_iocb siocb;
771 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800773 init_sync_kiocb(&iocb, NULL);
774 iocb.private = &siocb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800776 ret = do_sock_write(&msg, &iocb, file, (struct iovec *)iov, nr_segs);
777 if (-EIOCBQUEUED == ret)
778 ret = wait_on_sync_kiocb(&iocb);
779 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780}
781
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800782static ssize_t sock_aio_write(struct kiocb *iocb, const char __user *ubuf,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700783 size_t count, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800785 struct sock_iocb siocb, *x;
786
787 if (pos != 0)
788 return -ESPIPE;
789 if (count == 0) /* Match SYS5 behaviour */
790 return 0;
791
792 x = alloc_sock_iocb(iocb, (void __user *)ubuf, count, &siocb);
793 if (!x)
794 return -ENOMEM;
795
796 return do_sock_write(&x->async_msg, iocb, iocb->ki_filp,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700797 &x->async_iov, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798}
799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800/*
801 * Atomic setting of ioctl hooks to avoid race
802 * with module unload.
803 */
804
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800805static DEFINE_MUTEX(br_ioctl_mutex);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700806static int (*br_ioctl_hook) (unsigned int cmd, void __user *arg) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700808void brioctl_set(int (*hook) (unsigned int, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800810 mutex_lock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 br_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800812 mutex_unlock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813}
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700814
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815EXPORT_SYMBOL(brioctl_set);
816
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800817static DEFINE_MUTEX(vlan_ioctl_mutex);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700818static int (*vlan_ioctl_hook) (void __user *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700820void vlan_ioctl_set(int (*hook) (void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800822 mutex_lock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 vlan_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800824 mutex_unlock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825}
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700826
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827EXPORT_SYMBOL(vlan_ioctl_set);
828
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800829static DEFINE_MUTEX(dlci_ioctl_mutex);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700830static int (*dlci_ioctl_hook) (unsigned int, void __user *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700832void dlci_ioctl_set(int (*hook) (unsigned int, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800834 mutex_lock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 dlci_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800836 mutex_unlock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837}
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839EXPORT_SYMBOL(dlci_ioctl_set);
840
841/*
842 * With an ioctl, arg may well be a user mode pointer, but we don't know
843 * what to do with it - that's up to the protocol still.
844 */
845
846static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
847{
848 struct socket *sock;
849 void __user *argp = (void __user *)arg;
850 int pid, err;
851
Eric Dumazetb69aee02005-09-06 14:42:45 -0700852 sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
854 err = dev_ioctl(cmd, argp);
855 } else
Adrian Bunkd86b5e02006-01-21 00:46:55 +0100856#ifdef CONFIG_WIRELESS_EXT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
858 err = dev_ioctl(cmd, argp);
859 } else
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700860#endif /* CONFIG_WIRELESS_EXT */
861 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 case FIOSETOWN:
863 case SIOCSPGRP:
864 err = -EFAULT;
865 if (get_user(pid, (int __user *)argp))
866 break;
867 err = f_setown(sock->file, pid, 1);
868 break;
869 case FIOGETOWN:
870 case SIOCGPGRP:
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700871 err = put_user(sock->file->f_owner.pid,
872 (int __user *)argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 break;
874 case SIOCGIFBR:
875 case SIOCSIFBR:
876 case SIOCBRADDBR:
877 case SIOCBRDELBR:
878 err = -ENOPKG;
879 if (!br_ioctl_hook)
880 request_module("bridge");
881
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800882 mutex_lock(&br_ioctl_mutex);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700883 if (br_ioctl_hook)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 err = br_ioctl_hook(cmd, argp);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800885 mutex_unlock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 break;
887 case SIOCGIFVLAN:
888 case SIOCSIFVLAN:
889 err = -ENOPKG;
890 if (!vlan_ioctl_hook)
891 request_module("8021q");
892
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800893 mutex_lock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 if (vlan_ioctl_hook)
895 err = vlan_ioctl_hook(argp);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800896 mutex_unlock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 break;
898 case SIOCGIFDIVERT:
899 case SIOCSIFDIVERT:
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700900 /* Convert this to call through a hook */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 err = divert_ioctl(cmd, argp);
902 break;
903 case SIOCADDDLCI:
904 case SIOCDELDLCI:
905 err = -ENOPKG;
906 if (!dlci_ioctl_hook)
907 request_module("dlci");
908
909 if (dlci_ioctl_hook) {
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800910 mutex_lock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 err = dlci_ioctl_hook(cmd, argp);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800912 mutex_unlock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 }
914 break;
915 default:
916 err = sock->ops->ioctl(sock, cmd, arg);
Christoph Hellwigb5e5fa52006-01-03 14:18:33 -0800917
918 /*
919 * If this ioctl is unknown try to hand it down
920 * to the NIC driver.
921 */
922 if (err == -ENOIOCTLCMD)
923 err = dev_ioctl(cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 break;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 return err;
927}
928
929int sock_create_lite(int family, int type, int protocol, struct socket **res)
930{
931 int err;
932 struct socket *sock = NULL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 err = security_socket_create(family, type, protocol, 1);
935 if (err)
936 goto out;
937
938 sock = sock_alloc();
939 if (!sock) {
940 err = -ENOMEM;
941 goto out;
942 }
943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 sock->type = type;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -0700945 err = security_socket_post_create(sock, family, type, protocol, 1);
946 if (err)
947 goto out_release;
948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949out:
950 *res = sock;
951 return err;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -0700952out_release:
953 sock_release(sock);
954 sock = NULL;
955 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956}
957
958/* No kernel lock held - perfect */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700959static unsigned int sock_poll(struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
961 struct socket *sock;
962
963 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700964 * We can't return errors to poll, so it's either yes or no.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 */
Eric Dumazetb69aee02005-09-06 14:42:45 -0700966 sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 return sock->ops->poll(file, sock, wait);
968}
969
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700970static int sock_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
Eric Dumazetb69aee02005-09-06 14:42:45 -0700972 struct socket *sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
974 return sock->ops->mmap(file, sock, vma);
975}
976
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -0300977static int sock_close(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978{
979 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700980 * It was possible the inode is NULL we were
981 * closing an unfinished socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 */
983
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700984 if (!inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 printk(KERN_DEBUG "sock_close: NULL inode\n");
986 return 0;
987 }
988 sock_fasync(-1, filp, 0);
989 sock_release(SOCKET_I(inode));
990 return 0;
991}
992
993/*
994 * Update the socket async list
995 *
996 * Fasync_list locking strategy.
997 *
998 * 1. fasync_list is modified only under process context socket lock
999 * i.e. under semaphore.
1000 * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
1001 * or under socket lock.
1002 * 3. fasync_list can be used from softirq context, so that
1003 * modification under socket lock have to be enhanced with
1004 * write_lock_bh(&sk->sk_callback_lock).
1005 * --ANK (990710)
1006 */
1007
1008static int sock_fasync(int fd, struct file *filp, int on)
1009{
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001010 struct fasync_struct *fa, *fna = NULL, **prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 struct socket *sock;
1012 struct sock *sk;
1013
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001014 if (on) {
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08001015 fna = kmalloc(sizeof(struct fasync_struct), GFP_KERNEL);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001016 if (fna == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 return -ENOMEM;
1018 }
1019
Eric Dumazetb69aee02005-09-06 14:42:45 -07001020 sock = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001022 sk = sock->sk;
1023 if (sk == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 kfree(fna);
1025 return -EINVAL;
1026 }
1027
1028 lock_sock(sk);
1029
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001030 prev = &(sock->fasync_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001032 for (fa = *prev; fa != NULL; prev = &fa->fa_next, fa = *prev)
1033 if (fa->fa_file == filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 break;
1035
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001036 if (on) {
1037 if (fa != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 write_lock_bh(&sk->sk_callback_lock);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001039 fa->fa_fd = fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 write_unlock_bh(&sk->sk_callback_lock);
1041
1042 kfree(fna);
1043 goto out;
1044 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001045 fna->fa_file = filp;
1046 fna->fa_fd = fd;
1047 fna->magic = FASYNC_MAGIC;
1048 fna->fa_next = sock->fasync_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 write_lock_bh(&sk->sk_callback_lock);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001050 sock->fasync_list = fna;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 write_unlock_bh(&sk->sk_callback_lock);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001052 } else {
1053 if (fa != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 write_lock_bh(&sk->sk_callback_lock);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001055 *prev = fa->fa_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 write_unlock_bh(&sk->sk_callback_lock);
1057 kfree(fa);
1058 }
1059 }
1060
1061out:
1062 release_sock(sock->sk);
1063 return 0;
1064}
1065
1066/* This function may be called only under socket lock or callback_lock */
1067
1068int sock_wake_async(struct socket *sock, int how, int band)
1069{
1070 if (!sock || !sock->fasync_list)
1071 return -1;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001072 switch (how) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 case 1:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 if (test_bit(SOCK_ASYNC_WAITDATA, &sock->flags))
1076 break;
1077 goto call_kill;
1078 case 2:
1079 if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags))
1080 break;
1081 /* fall through */
1082 case 0:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001083call_kill:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 __kill_fasync(sock->fasync_list, SIGIO, band);
1085 break;
1086 case 3:
1087 __kill_fasync(sock->fasync_list, SIGURG, band);
1088 }
1089 return 0;
1090}
1091
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001092static int __sock_create(int family, int type, int protocol,
1093 struct socket **res, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094{
1095 int err;
1096 struct socket *sock;
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001097 const struct net_proto_family *pf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
1099 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001100 * Check protocol is in range
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 */
1102 if (family < 0 || family >= NPROTO)
1103 return -EAFNOSUPPORT;
1104 if (type < 0 || type >= SOCK_MAX)
1105 return -EINVAL;
1106
1107 /* Compatibility.
1108
1109 This uglymoron is moved from INET layer to here to avoid
1110 deadlock in module load.
1111 */
1112 if (family == PF_INET && type == SOCK_PACKET) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001113 static int warned;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 if (!warned) {
1115 warned = 1;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001116 printk(KERN_INFO "%s uses obsolete (PF_INET,SOCK_PACKET)\n",
1117 current->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 }
1119 family = PF_PACKET;
1120 }
1121
1122 err = security_socket_create(family, type, protocol, kern);
1123 if (err)
1124 return err;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001125
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001126 /*
1127 * Allocate the socket and allow the family to set things up. if
1128 * the protocol is 0, the family is instructed to select an appropriate
1129 * default.
1130 */
1131 sock = sock_alloc();
1132 if (!sock) {
1133 if (net_ratelimit())
1134 printk(KERN_WARNING "socket: no more sockets\n");
1135 return -ENFILE; /* Not exactly a match, but its the
1136 closest posix thing */
1137 }
1138
1139 sock->type = type;
1140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141#if defined(CONFIG_KMOD)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001142 /* Attempt to load a protocol module if the find failed.
1143 *
1144 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 * requested real, full-featured networking support upon configuration.
1146 * Otherwise module support will break!
1147 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001148 if (net_families[family] == NULL)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001149 request_module("net-pf-%d", family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150#endif
1151
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001152 rcu_read_lock();
1153 pf = rcu_dereference(net_families[family]);
1154 err = -EAFNOSUPPORT;
1155 if (!pf)
1156 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
1158 /*
1159 * We will call the ->create function, that possibly is in a loadable
1160 * module, so we have to bump that loadable module refcnt first.
1161 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001162 if (!try_module_get(pf->owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 goto out_release;
1164
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001165 /* Now protected by module ref count */
1166 rcu_read_unlock();
1167
1168 err = pf->create(sock, protocol);
1169 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 goto out_module_put;
Frank Filza79af592005-09-27 15:23:38 -07001171
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 /*
1173 * Now to bump the refcnt of the [loadable] module that owns this
1174 * socket at sock_release time we decrement its refcnt.
1175 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001176 if (!try_module_get(sock->ops->owner))
1177 goto out_module_busy;
1178
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 /*
1180 * Now that we're done with the ->create function, the [loadable]
1181 * module can have its refcnt decremented
1182 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001183 module_put(pf->owner);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001184 err = security_socket_post_create(sock, family, type, protocol, kern);
1185 if (err)
1186 goto out_release;
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001187 *res = sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001189 return 0;
1190
1191out_module_busy:
1192 err = -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193out_module_put:
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001194 sock->ops = NULL;
1195 module_put(pf->owner);
1196out_sock_release:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 sock_release(sock);
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001198 return err;
1199
1200out_release:
1201 rcu_read_unlock();
1202 goto out_sock_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203}
1204
1205int sock_create(int family, int type, int protocol, struct socket **res)
1206{
1207 return __sock_create(family, type, protocol, res, 0);
1208}
1209
1210int sock_create_kern(int family, int type, int protocol, struct socket **res)
1211{
1212 return __sock_create(family, type, protocol, res, 1);
1213}
1214
1215asmlinkage long sys_socket(int family, int type, int protocol)
1216{
1217 int retval;
1218 struct socket *sock;
1219
1220 retval = sock_create(family, type, protocol, &sock);
1221 if (retval < 0)
1222 goto out;
1223
1224 retval = sock_map_fd(sock);
1225 if (retval < 0)
1226 goto out_release;
1227
1228out:
1229 /* It may be already another descriptor 8) Not kernel problem. */
1230 return retval;
1231
1232out_release:
1233 sock_release(sock);
1234 return retval;
1235}
1236
1237/*
1238 * Create a pair of connected sockets.
1239 */
1240
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001241asmlinkage long sys_socketpair(int family, int type, int protocol,
1242 int __user *usockvec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243{
1244 struct socket *sock1, *sock2;
1245 int fd1, fd2, err;
1246
1247 /*
1248 * Obtain the first socket and check if the underlying protocol
1249 * supports the socketpair call.
1250 */
1251
1252 err = sock_create(family, type, protocol, &sock1);
1253 if (err < 0)
1254 goto out;
1255
1256 err = sock_create(family, type, protocol, &sock2);
1257 if (err < 0)
1258 goto out_release_1;
1259
1260 err = sock1->ops->socketpair(sock1, sock2);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001261 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 goto out_release_both;
1263
1264 fd1 = fd2 = -1;
1265
1266 err = sock_map_fd(sock1);
1267 if (err < 0)
1268 goto out_release_both;
1269 fd1 = err;
1270
1271 err = sock_map_fd(sock2);
1272 if (err < 0)
1273 goto out_close_1;
1274 fd2 = err;
1275
1276 /* fd1 and fd2 may be already another descriptors.
1277 * Not kernel problem.
1278 */
1279
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001280 err = put_user(fd1, &usockvec[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 if (!err)
1282 err = put_user(fd2, &usockvec[1]);
1283 if (!err)
1284 return 0;
1285
1286 sys_close(fd2);
1287 sys_close(fd1);
1288 return err;
1289
1290out_close_1:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001291 sock_release(sock2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 sys_close(fd1);
1293 return err;
1294
1295out_release_both:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001296 sock_release(sock2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297out_release_1:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001298 sock_release(sock1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299out:
1300 return err;
1301}
1302
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303/*
1304 * Bind a name to a socket. Nothing much to do here since it's
1305 * the protocol's responsibility to handle the local address.
1306 *
1307 * We move the socket address to kernel space before we call
1308 * the protocol layer (having also checked the address is ok).
1309 */
1310
1311asmlinkage long sys_bind(int fd, struct sockaddr __user *umyaddr, int addrlen)
1312{
1313 struct socket *sock;
1314 char address[MAX_SOCK_ADDR];
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001315 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001317 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1318 if(sock) {
1319 err = move_addr_to_kernel(umyaddr, addrlen, address);
1320 if (err >= 0) {
1321 err = security_socket_bind(sock,
1322 (struct sockaddr *)address,
1323 addrlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001324 if (!err)
1325 err = sock->ops->bind(sock,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001326 (struct sockaddr *)
1327 address, addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 }
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001329 fput_light(sock->file, fput_needed);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 return err;
1332}
1333
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334/*
1335 * Perform a listen. Basically, we allow the protocol to do anything
1336 * necessary for a listen, and if that works, we mark the socket as
1337 * ready for listening.
1338 */
1339
Brian Haley7a42c212006-08-31 15:03:02 -07001340int sysctl_somaxconn __read_mostly = SOMAXCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
1342asmlinkage long sys_listen(int fd, int backlog)
1343{
1344 struct socket *sock;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001345 int err, fput_needed;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001346
1347 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1348 if (sock) {
1349 if ((unsigned)backlog > sysctl_somaxconn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 backlog = sysctl_somaxconn;
1351
1352 err = security_socket_listen(sock, backlog);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001353 if (!err)
1354 err = sock->ops->listen(sock, backlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001356 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 }
1358 return err;
1359}
1360
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361/*
1362 * For accept, we attempt to create a new socket, set up the link
1363 * with the client, wake up the client, then return the new
1364 * connected fd. We collect the address of the connector in kernel
1365 * space and move it to user at the very end. This is unclean because
1366 * we open the socket then return an error.
1367 *
1368 * 1003.1g adds the ability to recvmsg() to query connection pending
1369 * status to recvmsg. We need to add that support in a way thats
1370 * clean when we restucture accept also.
1371 */
1372
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001373asmlinkage long sys_accept(int fd, struct sockaddr __user *upeer_sockaddr,
1374 int __user *upeer_addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375{
1376 struct socket *sock, *newsock;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001377 struct file *newfile;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001378 int err, len, newfd, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 char address[MAX_SOCK_ADDR];
1380
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001381 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 if (!sock)
1383 goto out;
1384
1385 err = -ENFILE;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001386 if (!(newsock = sock_alloc()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 goto out_put;
1388
1389 newsock->type = sock->type;
1390 newsock->ops = sock->ops;
1391
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 /*
1393 * We don't need try_module_get here, as the listening socket (sock)
1394 * has the protocol module (sock->ops->owner) held.
1395 */
1396 __module_get(newsock->ops->owner);
1397
David S. Miller39d8c1b2006-03-20 17:13:49 -08001398 newfd = sock_alloc_fd(&newfile);
1399 if (unlikely(newfd < 0)) {
1400 err = newfd;
David S. Miller9a1875e2006-04-01 12:48:36 -08001401 sock_release(newsock);
1402 goto out_put;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001403 }
1404
1405 err = sock_attach_fd(newsock, newfile);
1406 if (err < 0)
1407 goto out_fd;
1408
Frank Filza79af592005-09-27 15:23:38 -07001409 err = security_socket_accept(sock, newsock);
1410 if (err)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001411 goto out_fd;
Frank Filza79af592005-09-27 15:23:38 -07001412
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 err = sock->ops->accept(sock, newsock, sock->file->f_flags);
1414 if (err < 0)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001415 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
1417 if (upeer_sockaddr) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001418 if (newsock->ops->getname(newsock, (struct sockaddr *)address,
1419 &len, 2) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 err = -ECONNABORTED;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001421 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001423 err = move_addr_to_user(address, len, upeer_sockaddr,
1424 upeer_addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 if (err < 0)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001426 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 }
1428
1429 /* File flags are not inherited via accept() unlike another OSes. */
1430
David S. Miller39d8c1b2006-03-20 17:13:49 -08001431 fd_install(newfd, newfile);
1432 err = newfd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
1434 security_socket_post_accept(sock, newsock);
1435
1436out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001437 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438out:
1439 return err;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001440out_fd:
David S. Miller9606a212006-04-01 01:00:14 -08001441 fput(newfile);
David S. Miller39d8c1b2006-03-20 17:13:49 -08001442 put_unused_fd(newfd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 goto out_put;
1444}
1445
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446/*
1447 * Attempt to connect to a socket with the server address. The address
1448 * is in user space so we verify it is OK and move it to kernel space.
1449 *
1450 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
1451 * break bindings
1452 *
1453 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
1454 * other SEQPACKET protocols that take time to connect() as it doesn't
1455 * include the -EINPROGRESS status for such sockets.
1456 */
1457
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001458asmlinkage long sys_connect(int fd, struct sockaddr __user *uservaddr,
1459 int addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460{
1461 struct socket *sock;
1462 char address[MAX_SOCK_ADDR];
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001463 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001465 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 if (!sock)
1467 goto out;
1468 err = move_addr_to_kernel(uservaddr, addrlen, address);
1469 if (err < 0)
1470 goto out_put;
1471
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001472 err =
1473 security_socket_connect(sock, (struct sockaddr *)address, addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 if (err)
1475 goto out_put;
1476
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001477 err = sock->ops->connect(sock, (struct sockaddr *)address, addrlen,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 sock->file->f_flags);
1479out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001480 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481out:
1482 return err;
1483}
1484
1485/*
1486 * Get the local address ('name') of a socket object. Move the obtained
1487 * name to user space.
1488 */
1489
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001490asmlinkage long sys_getsockname(int fd, struct sockaddr __user *usockaddr,
1491 int __user *usockaddr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492{
1493 struct socket *sock;
1494 char address[MAX_SOCK_ADDR];
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001495 int len, err, fput_needed;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001496
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001497 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 if (!sock)
1499 goto out;
1500
1501 err = security_socket_getsockname(sock);
1502 if (err)
1503 goto out_put;
1504
1505 err = sock->ops->getname(sock, (struct sockaddr *)address, &len, 0);
1506 if (err)
1507 goto out_put;
1508 err = move_addr_to_user(address, len, usockaddr, usockaddr_len);
1509
1510out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001511 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512out:
1513 return err;
1514}
1515
1516/*
1517 * Get the remote address ('name') of a socket object. Move the obtained
1518 * name to user space.
1519 */
1520
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001521asmlinkage long sys_getpeername(int fd, struct sockaddr __user *usockaddr,
1522 int __user *usockaddr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523{
1524 struct socket *sock;
1525 char address[MAX_SOCK_ADDR];
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001526 int len, err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001528 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1529 if (sock != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 err = security_socket_getpeername(sock);
1531 if (err) {
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001532 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 return err;
1534 }
1535
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001536 err =
1537 sock->ops->getname(sock, (struct sockaddr *)address, &len,
1538 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 if (!err)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001540 err = move_addr_to_user(address, len, usockaddr,
1541 usockaddr_len);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001542 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 }
1544 return err;
1545}
1546
1547/*
1548 * Send a datagram to a given address. We move the address into kernel
1549 * space and check the user space data area is readable before invoking
1550 * the protocol.
1551 */
1552
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001553asmlinkage long sys_sendto(int fd, void __user *buff, size_t len,
1554 unsigned flags, struct sockaddr __user *addr,
1555 int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556{
1557 struct socket *sock;
1558 char address[MAX_SOCK_ADDR];
1559 int err;
1560 struct msghdr msg;
1561 struct iovec iov;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001562 int fput_needed;
1563 struct file *sock_file;
1564
1565 sock_file = fget_light(fd, &fput_needed);
1566 if (!sock_file)
1567 return -EBADF;
1568
1569 sock = sock_from_file(sock_file, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 if (!sock)
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001571 goto out_put;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001572 iov.iov_base = buff;
1573 iov.iov_len = len;
1574 msg.msg_name = NULL;
1575 msg.msg_iov = &iov;
1576 msg.msg_iovlen = 1;
1577 msg.msg_control = NULL;
1578 msg.msg_controllen = 0;
1579 msg.msg_namelen = 0;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001580 if (addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 err = move_addr_to_kernel(addr, addr_len, address);
1582 if (err < 0)
1583 goto out_put;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001584 msg.msg_name = address;
1585 msg.msg_namelen = addr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 }
1587 if (sock->file->f_flags & O_NONBLOCK)
1588 flags |= MSG_DONTWAIT;
1589 msg.msg_flags = flags;
1590 err = sock_sendmsg(sock, &msg, len);
1591
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001592out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001593 fput_light(sock_file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 return err;
1595}
1596
1597/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001598 * Send a datagram down a socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 */
1600
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001601asmlinkage long sys_send(int fd, void __user *buff, size_t len, unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602{
1603 return sys_sendto(fd, buff, len, flags, NULL, 0);
1604}
1605
1606/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001607 * Receive a frame from the socket and optionally record the address of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 * sender. We verify the buffers are writable and if needed move the
1609 * sender address from kernel to user space.
1610 */
1611
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001612asmlinkage long sys_recvfrom(int fd, void __user *ubuf, size_t size,
1613 unsigned flags, struct sockaddr __user *addr,
1614 int __user *addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615{
1616 struct socket *sock;
1617 struct iovec iov;
1618 struct msghdr msg;
1619 char address[MAX_SOCK_ADDR];
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001620 int err, err2;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001621 struct file *sock_file;
1622 int fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001624 sock_file = fget_light(fd, &fput_needed);
1625 if (!sock_file)
1626 return -EBADF;
1627
1628 sock = sock_from_file(sock_file, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 if (!sock)
1630 goto out;
1631
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001632 msg.msg_control = NULL;
1633 msg.msg_controllen = 0;
1634 msg.msg_iovlen = 1;
1635 msg.msg_iov = &iov;
1636 iov.iov_len = size;
1637 iov.iov_base = ubuf;
1638 msg.msg_name = address;
1639 msg.msg_namelen = MAX_SOCK_ADDR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 if (sock->file->f_flags & O_NONBLOCK)
1641 flags |= MSG_DONTWAIT;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001642 err = sock_recvmsg(sock, &msg, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001644 if (err >= 0 && addr != NULL) {
1645 err2 = move_addr_to_user(address, msg.msg_namelen, addr, addr_len);
1646 if (err2 < 0)
1647 err = err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649out:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001650 fput_light(sock_file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 return err;
1652}
1653
1654/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001655 * Receive a datagram from a socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 */
1657
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001658asmlinkage long sys_recv(int fd, void __user *ubuf, size_t size,
1659 unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660{
1661 return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
1662}
1663
1664/*
1665 * Set a socket option. Because we don't know the option lengths we have
1666 * to pass the user mode parameter for the protocols to sort out.
1667 */
1668
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001669asmlinkage long sys_setsockopt(int fd, int level, int optname,
1670 char __user *optval, int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001672 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 struct socket *sock;
1674
1675 if (optlen < 0)
1676 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001677
1678 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1679 if (sock != NULL) {
1680 err = security_socket_setsockopt(sock, level, optname);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001681 if (err)
1682 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
1684 if (level == SOL_SOCKET)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001685 err =
1686 sock_setsockopt(sock, level, optname, optval,
1687 optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 else
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001689 err =
1690 sock->ops->setsockopt(sock, level, optname, optval,
1691 optlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001692out_put:
1693 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 }
1695 return err;
1696}
1697
1698/*
1699 * Get a socket option. Because we don't know the option lengths we have
1700 * to pass a user mode parameter for the protocols to sort out.
1701 */
1702
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001703asmlinkage long sys_getsockopt(int fd, int level, int optname,
1704 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001706 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 struct socket *sock;
1708
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001709 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1710 if (sock != NULL) {
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001711 err = security_socket_getsockopt(sock, level, optname);
1712 if (err)
1713 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
1715 if (level == SOL_SOCKET)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001716 err =
1717 sock_getsockopt(sock, level, optname, optval,
1718 optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 else
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001720 err =
1721 sock->ops->getsockopt(sock, level, optname, optval,
1722 optlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001723out_put:
1724 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 }
1726 return err;
1727}
1728
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729/*
1730 * Shutdown a socket.
1731 */
1732
1733asmlinkage long sys_shutdown(int fd, int how)
1734{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001735 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 struct socket *sock;
1737
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001738 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1739 if (sock != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 err = security_socket_shutdown(sock, how);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001741 if (!err)
1742 err = sock->ops->shutdown(sock, how);
1743 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 }
1745 return err;
1746}
1747
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001748/* A couple of helpful macros for getting the address of the 32/64 bit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 * fields which are the same type (int / unsigned) on our platforms.
1750 */
1751#define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
1752#define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
1753#define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
1754
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755/*
1756 * BSD sendmsg interface
1757 */
1758
1759asmlinkage long sys_sendmsg(int fd, struct msghdr __user *msg, unsigned flags)
1760{
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001761 struct compat_msghdr __user *msg_compat =
1762 (struct compat_msghdr __user *)msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 struct socket *sock;
1764 char address[MAX_SOCK_ADDR];
1765 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
Alex Williamsonb9d717a2005-09-26 14:28:02 -07001766 unsigned char ctl[sizeof(struct cmsghdr) + 20]
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001767 __attribute__ ((aligned(sizeof(__kernel_size_t))));
1768 /* 20 is size of ipv6_pktinfo */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 unsigned char *ctl_buf = ctl;
1770 struct msghdr msg_sys;
1771 int err, ctl_len, iov_size, total_len;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001772 int fput_needed;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001773
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 err = -EFAULT;
1775 if (MSG_CMSG_COMPAT & flags) {
1776 if (get_compat_msghdr(&msg_sys, msg_compat))
1777 return -EFAULT;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001778 }
1779 else if (copy_from_user(&msg_sys, msg, sizeof(struct msghdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 return -EFAULT;
1781
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001782 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001783 if (!sock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 goto out;
1785
1786 /* do not move before msg_sys is valid */
1787 err = -EMSGSIZE;
1788 if (msg_sys.msg_iovlen > UIO_MAXIOV)
1789 goto out_put;
1790
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001791 /* Check whether to allocate the iovec area */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 err = -ENOMEM;
1793 iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
1794 if (msg_sys.msg_iovlen > UIO_FASTIOV) {
1795 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
1796 if (!iov)
1797 goto out_put;
1798 }
1799
1800 /* This will also move the address data into kernel space */
1801 if (MSG_CMSG_COMPAT & flags) {
1802 err = verify_compat_iovec(&msg_sys, iov, address, VERIFY_READ);
1803 } else
1804 err = verify_iovec(&msg_sys, iov, address, VERIFY_READ);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001805 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 goto out_freeiov;
1807 total_len = err;
1808
1809 err = -ENOBUFS;
1810
1811 if (msg_sys.msg_controllen > INT_MAX)
1812 goto out_freeiov;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001813 ctl_len = msg_sys.msg_controllen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001815 err =
1816 cmsghdr_from_user_compat_to_kern(&msg_sys, sock->sk, ctl,
1817 sizeof(ctl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 if (err)
1819 goto out_freeiov;
1820 ctl_buf = msg_sys.msg_control;
Al Viro8920e8f2005-09-07 18:28:51 -07001821 ctl_len = msg_sys.msg_controllen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 } else if (ctl_len) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001823 if (ctl_len > sizeof(ctl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001825 if (ctl_buf == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 goto out_freeiov;
1827 }
1828 err = -EFAULT;
1829 /*
1830 * Careful! Before this, msg_sys.msg_control contains a user pointer.
1831 * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
1832 * checking falls down on this.
1833 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001834 if (copy_from_user(ctl_buf, (void __user *)msg_sys.msg_control,
1835 ctl_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 goto out_freectl;
1837 msg_sys.msg_control = ctl_buf;
1838 }
1839 msg_sys.msg_flags = flags;
1840
1841 if (sock->file->f_flags & O_NONBLOCK)
1842 msg_sys.msg_flags |= MSG_DONTWAIT;
1843 err = sock_sendmsg(sock, &msg_sys, total_len);
1844
1845out_freectl:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001846 if (ctl_buf != ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 sock_kfree_s(sock->sk, ctl_buf, ctl_len);
1848out_freeiov:
1849 if (iov != iovstack)
1850 sock_kfree_s(sock->sk, iov, iov_size);
1851out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001852 fput_light(sock->file, fput_needed);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001853out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 return err;
1855}
1856
1857/*
1858 * BSD recvmsg interface
1859 */
1860
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001861asmlinkage long sys_recvmsg(int fd, struct msghdr __user *msg,
1862 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863{
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001864 struct compat_msghdr __user *msg_compat =
1865 (struct compat_msghdr __user *)msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 struct socket *sock;
1867 struct iovec iovstack[UIO_FASTIOV];
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001868 struct iovec *iov = iovstack;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 struct msghdr msg_sys;
1870 unsigned long cmsg_ptr;
1871 int err, iov_size, total_len, len;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001872 int fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873
1874 /* kernel mode address */
1875 char addr[MAX_SOCK_ADDR];
1876
1877 /* user mode address pointers */
1878 struct sockaddr __user *uaddr;
1879 int __user *uaddr_len;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001880
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 if (MSG_CMSG_COMPAT & flags) {
1882 if (get_compat_msghdr(&msg_sys, msg_compat))
1883 return -EFAULT;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001884 }
1885 else if (copy_from_user(&msg_sys, msg, sizeof(struct msghdr)))
1886 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001888 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 if (!sock)
1890 goto out;
1891
1892 err = -EMSGSIZE;
1893 if (msg_sys.msg_iovlen > UIO_MAXIOV)
1894 goto out_put;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001895
1896 /* Check whether to allocate the iovec area */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 err = -ENOMEM;
1898 iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
1899 if (msg_sys.msg_iovlen > UIO_FASTIOV) {
1900 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
1901 if (!iov)
1902 goto out_put;
1903 }
1904
1905 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001906 * Save the user-mode address (verify_iovec will change the
1907 * kernel msghdr to use the kernel address space)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001909
1910 uaddr = (void __user *)msg_sys.msg_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 uaddr_len = COMPAT_NAMELEN(msg);
1912 if (MSG_CMSG_COMPAT & flags) {
1913 err = verify_compat_iovec(&msg_sys, iov, addr, VERIFY_WRITE);
1914 } else
1915 err = verify_iovec(&msg_sys, iov, addr, VERIFY_WRITE);
1916 if (err < 0)
1917 goto out_freeiov;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001918 total_len = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
1920 cmsg_ptr = (unsigned long)msg_sys.msg_control;
1921 msg_sys.msg_flags = 0;
1922 if (MSG_CMSG_COMPAT & flags)
1923 msg_sys.msg_flags = MSG_CMSG_COMPAT;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001924
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 if (sock->file->f_flags & O_NONBLOCK)
1926 flags |= MSG_DONTWAIT;
1927 err = sock_recvmsg(sock, &msg_sys, total_len, flags);
1928 if (err < 0)
1929 goto out_freeiov;
1930 len = err;
1931
1932 if (uaddr != NULL) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001933 err = move_addr_to_user(addr, msg_sys.msg_namelen, uaddr,
1934 uaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 if (err < 0)
1936 goto out_freeiov;
1937 }
David S. Miller37f7f422005-09-16 16:51:01 -07001938 err = __put_user((msg_sys.msg_flags & ~MSG_CMSG_COMPAT),
1939 COMPAT_FLAGS(msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 if (err)
1941 goto out_freeiov;
1942 if (MSG_CMSG_COMPAT & flags)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001943 err = __put_user((unsigned long)msg_sys.msg_control - cmsg_ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 &msg_compat->msg_controllen);
1945 else
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001946 err = __put_user((unsigned long)msg_sys.msg_control - cmsg_ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 &msg->msg_controllen);
1948 if (err)
1949 goto out_freeiov;
1950 err = len;
1951
1952out_freeiov:
1953 if (iov != iovstack)
1954 sock_kfree_s(sock->sk, iov, iov_size);
1955out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001956 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957out:
1958 return err;
1959}
1960
1961#ifdef __ARCH_WANT_SYS_SOCKETCALL
1962
1963/* Argument list sizes for sys_socketcall */
1964#define AL(x) ((x) * sizeof(unsigned long))
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001965static const unsigned char nargs[18]={
1966 AL(0),AL(3),AL(3),AL(3),AL(2),AL(3),
1967 AL(3),AL(3),AL(4),AL(4),AL(4),AL(6),
1968 AL(6),AL(2),AL(5),AL(5),AL(3),AL(3)
1969};
1970
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971#undef AL
1972
1973/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001974 * System call vectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 *
1976 * Argument checking cleaned up. Saved 20% in size.
1977 * This function doesn't need to set the kernel lock because
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001978 * it is set by the callees.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 */
1980
1981asmlinkage long sys_socketcall(int call, unsigned long __user *args)
1982{
1983 unsigned long a[6];
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001984 unsigned long a0, a1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 int err;
1986
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001987 if (call < 1 || call > SYS_RECVMSG)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 return -EINVAL;
1989
1990 /* copy_from_user should be SMP safe. */
1991 if (copy_from_user(a, args, nargs[call]))
1992 return -EFAULT;
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01001993
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001994 err = audit_socketcall(nargs[call] / sizeof(unsigned long), a);
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01001995 if (err)
1996 return err;
1997
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001998 a0 = a[0];
1999 a1 = a[1];
2000
2001 switch (call) {
2002 case SYS_SOCKET:
2003 err = sys_socket(a0, a1, a[2]);
2004 break;
2005 case SYS_BIND:
2006 err = sys_bind(a0, (struct sockaddr __user *)a1, a[2]);
2007 break;
2008 case SYS_CONNECT:
2009 err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
2010 break;
2011 case SYS_LISTEN:
2012 err = sys_listen(a0, a1);
2013 break;
2014 case SYS_ACCEPT:
2015 err =
2016 sys_accept(a0, (struct sockaddr __user *)a1,
2017 (int __user *)a[2]);
2018 break;
2019 case SYS_GETSOCKNAME:
2020 err =
2021 sys_getsockname(a0, (struct sockaddr __user *)a1,
2022 (int __user *)a[2]);
2023 break;
2024 case SYS_GETPEERNAME:
2025 err =
2026 sys_getpeername(a0, (struct sockaddr __user *)a1,
2027 (int __user *)a[2]);
2028 break;
2029 case SYS_SOCKETPAIR:
2030 err = sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
2031 break;
2032 case SYS_SEND:
2033 err = sys_send(a0, (void __user *)a1, a[2], a[3]);
2034 break;
2035 case SYS_SENDTO:
2036 err = sys_sendto(a0, (void __user *)a1, a[2], a[3],
2037 (struct sockaddr __user *)a[4], a[5]);
2038 break;
2039 case SYS_RECV:
2040 err = sys_recv(a0, (void __user *)a1, a[2], a[3]);
2041 break;
2042 case SYS_RECVFROM:
2043 err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
2044 (struct sockaddr __user *)a[4],
2045 (int __user *)a[5]);
2046 break;
2047 case SYS_SHUTDOWN:
2048 err = sys_shutdown(a0, a1);
2049 break;
2050 case SYS_SETSOCKOPT:
2051 err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]);
2052 break;
2053 case SYS_GETSOCKOPT:
2054 err =
2055 sys_getsockopt(a0, a1, a[2], (char __user *)a[3],
2056 (int __user *)a[4]);
2057 break;
2058 case SYS_SENDMSG:
2059 err = sys_sendmsg(a0, (struct msghdr __user *)a1, a[2]);
2060 break;
2061 case SYS_RECVMSG:
2062 err = sys_recvmsg(a0, (struct msghdr __user *)a1, a[2]);
2063 break;
2064 default:
2065 err = -EINVAL;
2066 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 }
2068 return err;
2069}
2070
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002071#endif /* __ARCH_WANT_SYS_SOCKETCALL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002073/**
2074 * sock_register - add a socket protocol handler
2075 * @ops: description of protocol
2076 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 * This function is called by a protocol handler that wants to
2078 * advertise its address family, and have it linked into the
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002079 * socket interface. The value ops->family coresponds to the
2080 * socket system call protocol family.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 */
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002082int sock_register(const struct net_proto_family *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083{
2084 int err;
2085
2086 if (ops->family >= NPROTO) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002087 printk(KERN_CRIT "protocol %d >= NPROTO(%d)\n", ops->family,
2088 NPROTO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 return -ENOBUFS;
2090 }
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002091
2092 spin_lock(&net_family_lock);
2093 if (net_families[ops->family])
2094 err = -EEXIST;
2095 else {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002096 net_families[ops->family] = ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 err = 0;
2098 }
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002099 spin_unlock(&net_family_lock);
2100
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002101 printk(KERN_INFO "NET: Registered protocol family %d\n", ops->family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 return err;
2103}
2104
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002105/**
2106 * sock_unregister - remove a protocol handler
2107 * @family: protocol family to remove
2108 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 * This function is called by a protocol handler that wants to
2110 * remove its address family, and have it unlinked from the
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002111 * new socket creation.
2112 *
2113 * If protocol handler is a module, then it can use module reference
2114 * counts to protect against new references. If protocol handler is not
2115 * a module then it needs to provide its own protection in
2116 * the ops->create routine.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 */
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002118void sock_unregister(int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119{
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002120 BUG_ON(family < 0 || family >= NPROTO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002122 spin_lock(&net_family_lock);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002123 net_families[family] = NULL;
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002124 spin_unlock(&net_family_lock);
2125
2126 synchronize_rcu();
2127
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002128 printk(KERN_INFO "NET: Unregistered protocol family %d\n", family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129}
2130
Andi Kleen77d76ea2005-12-22 12:43:42 -08002131static int __init sock_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132{
2133 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002134 * Initialize sock SLAB cache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002136
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 sk_init();
2138
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002140 * Initialize skbuff SLAB cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 */
2142 skb_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143
2144 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002145 * Initialize the protocols module.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 */
2147
2148 init_inodecache();
2149 register_filesystem(&sock_fs_type);
2150 sock_mnt = kern_mount(&sock_fs_type);
Andi Kleen77d76ea2005-12-22 12:43:42 -08002151
2152 /* The real protocol initialization is performed in later initcalls.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 */
2154
2155#ifdef CONFIG_NETFILTER
2156 netfilter_init();
2157#endif
David S. Millercbeb3212005-12-22 12:58:55 -08002158
2159 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160}
2161
Andi Kleen77d76ea2005-12-22 12:43:42 -08002162core_initcall(sock_init); /* early initcall */
2163
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164#ifdef CONFIG_PROC_FS
2165void socket_seq_show(struct seq_file *seq)
2166{
2167 int cpu;
2168 int counter = 0;
2169
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -07002170 for_each_possible_cpu(cpu)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002171 counter += per_cpu(sockets_in_use, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
2173 /* It can be negative, by the way. 8) */
2174 if (counter < 0)
2175 counter = 0;
2176
2177 seq_printf(seq, "sockets: used %d\n", counter);
2178}
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002179#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
Shaun Pereira89bbfc92006-03-21 23:58:08 -08002181#ifdef CONFIG_COMPAT
2182static long compat_sock_ioctl(struct file *file, unsigned cmd,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002183 unsigned long arg)
Shaun Pereira89bbfc92006-03-21 23:58:08 -08002184{
2185 struct socket *sock = file->private_data;
2186 int ret = -ENOIOCTLCMD;
2187
2188 if (sock->ops->compat_ioctl)
2189 ret = sock->ops->compat_ioctl(sock, cmd, arg);
2190
2191 return ret;
2192}
2193#endif
2194
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07002195int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen)
2196{
2197 return sock->ops->bind(sock, addr, addrlen);
2198}
2199
2200int kernel_listen(struct socket *sock, int backlog)
2201{
2202 return sock->ops->listen(sock, backlog);
2203}
2204
2205int kernel_accept(struct socket *sock, struct socket **newsock, int flags)
2206{
2207 struct sock *sk = sock->sk;
2208 int err;
2209
2210 err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
2211 newsock);
2212 if (err < 0)
2213 goto done;
2214
2215 err = sock->ops->accept(sock, *newsock, flags);
2216 if (err < 0) {
2217 sock_release(*newsock);
2218 goto done;
2219 }
2220
2221 (*newsock)->ops = sock->ops;
2222
2223done:
2224 return err;
2225}
2226
2227int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
2228 int flags)
2229{
2230 return sock->ops->connect(sock, addr, addrlen, flags);
2231}
2232
2233int kernel_getsockname(struct socket *sock, struct sockaddr *addr,
2234 int *addrlen)
2235{
2236 return sock->ops->getname(sock, addr, addrlen, 0);
2237}
2238
2239int kernel_getpeername(struct socket *sock, struct sockaddr *addr,
2240 int *addrlen)
2241{
2242 return sock->ops->getname(sock, addr, addrlen, 1);
2243}
2244
2245int kernel_getsockopt(struct socket *sock, int level, int optname,
2246 char *optval, int *optlen)
2247{
2248 mm_segment_t oldfs = get_fs();
2249 int err;
2250
2251 set_fs(KERNEL_DS);
2252 if (level == SOL_SOCKET)
2253 err = sock_getsockopt(sock, level, optname, optval, optlen);
2254 else
2255 err = sock->ops->getsockopt(sock, level, optname, optval,
2256 optlen);
2257 set_fs(oldfs);
2258 return err;
2259}
2260
2261int kernel_setsockopt(struct socket *sock, int level, int optname,
2262 char *optval, int optlen)
2263{
2264 mm_segment_t oldfs = get_fs();
2265 int err;
2266
2267 set_fs(KERNEL_DS);
2268 if (level == SOL_SOCKET)
2269 err = sock_setsockopt(sock, level, optname, optval, optlen);
2270 else
2271 err = sock->ops->setsockopt(sock, level, optname, optval,
2272 optlen);
2273 set_fs(oldfs);
2274 return err;
2275}
2276
2277int kernel_sendpage(struct socket *sock, struct page *page, int offset,
2278 size_t size, int flags)
2279{
2280 if (sock->ops->sendpage)
2281 return sock->ops->sendpage(sock, page, offset, size, flags);
2282
2283 return sock_no_sendpage(sock, page, offset, size, flags);
2284}
2285
2286int kernel_sock_ioctl(struct socket *sock, int cmd, unsigned long arg)
2287{
2288 mm_segment_t oldfs = get_fs();
2289 int err;
2290
2291 set_fs(KERNEL_DS);
2292 err = sock->ops->ioctl(sock, cmd, arg);
2293 set_fs(oldfs);
2294
2295 return err;
2296}
2297
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298/* ABI emulation layers need these two */
2299EXPORT_SYMBOL(move_addr_to_kernel);
2300EXPORT_SYMBOL(move_addr_to_user);
2301EXPORT_SYMBOL(sock_create);
2302EXPORT_SYMBOL(sock_create_kern);
2303EXPORT_SYMBOL(sock_create_lite);
2304EXPORT_SYMBOL(sock_map_fd);
2305EXPORT_SYMBOL(sock_recvmsg);
2306EXPORT_SYMBOL(sock_register);
2307EXPORT_SYMBOL(sock_release);
2308EXPORT_SYMBOL(sock_sendmsg);
2309EXPORT_SYMBOL(sock_unregister);
2310EXPORT_SYMBOL(sock_wake_async);
2311EXPORT_SYMBOL(sockfd_lookup);
2312EXPORT_SYMBOL(kernel_sendmsg);
2313EXPORT_SYMBOL(kernel_recvmsg);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07002314EXPORT_SYMBOL(kernel_bind);
2315EXPORT_SYMBOL(kernel_listen);
2316EXPORT_SYMBOL(kernel_accept);
2317EXPORT_SYMBOL(kernel_connect);
2318EXPORT_SYMBOL(kernel_getsockname);
2319EXPORT_SYMBOL(kernel_getpeername);
2320EXPORT_SYMBOL(kernel_getsockopt);
2321EXPORT_SYMBOL(kernel_setsockopt);
2322EXPORT_SYMBOL(kernel_sendpage);
2323EXPORT_SYMBOL(kernel_sock_ioctl);