PageRenderTime 134ms CodeModel.GetById 3ms app.highlight 115ms RepoModel.GetById 1ms app.codeStats 1ms

/net/socket.c

https://bitbucket.org/digetx/picasso-kernel
C | 3505 lines | 2541 code | 501 blank | 463 comment | 401 complexity | 8d5656caf9ddf051858bf9f47c7d6911 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1/*
   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>
   7 *		Ross Biro
   8 *		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)
  45 *		Tigran Aivazian	:	Made listen(2) backlog sanity checks
  46 *					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
  56 *	paradigm.
  57 *
  58 *	Based upon Swansea University Computer Society NET3.039
  59 */
  60
  61#include <linux/mm.h>
  62#include <linux/socket.h>
  63#include <linux/file.h>
  64#include <linux/net.h>
  65#include <linux/interrupt.h>
  66#include <linux/thread_info.h>
  67#include <linux/rcupdate.h>
  68#include <linux/netdevice.h>
  69#include <linux/proc_fs.h>
  70#include <linux/seq_file.h>
  71#include <linux/mutex.h>
  72#include <linux/wanrouter.h>
  73#include <linux/if_bridge.h>
  74#include <linux/if_frad.h>
  75#include <linux/if_vlan.h>
  76#include <linux/init.h>
  77#include <linux/poll.h>
  78#include <linux/cache.h>
  79#include <linux/module.h>
  80#include <linux/highmem.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>
  86#include <linux/audit.h>
  87#include <linux/wireless.h>
  88#include <linux/nsproxy.h>
  89#include <linux/magic.h>
  90#include <linux/slab.h>
  91#include <linux/xattr.h>
  92
  93#include <asm/uaccess.h>
  94#include <asm/unistd.h>
  95
  96#include <net/compat.h>
  97#include <net/wext.h>
  98#include <net/cls_cgroup.h>
  99
 100#include <net/sock.h>
 101#include <linux/netfilter.h>
 102
 103#include <linux/if_tun.h>
 104#include <linux/ipv6_route.h>
 105#include <linux/route.h>
 106#include <linux/sockios.h>
 107#include <linux/atalk.h>
 108
 109static int sock_no_open(struct inode *irrelevant, struct file *dontcare);
 110static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov,
 111			 unsigned long nr_segs, loff_t pos);
 112static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov,
 113			  unsigned long nr_segs, loff_t pos);
 114static int sock_mmap(struct file *file, struct vm_area_struct *vma);
 115
 116static int sock_close(struct inode *inode, struct file *file);
 117static unsigned int sock_poll(struct file *file,
 118			      struct poll_table_struct *wait);
 119static long sock_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
 120#ifdef CONFIG_COMPAT
 121static long compat_sock_ioctl(struct file *file,
 122			      unsigned int cmd, unsigned long arg);
 123#endif
 124static int sock_fasync(int fd, struct file *filp, int on);
 125static ssize_t sock_sendpage(struct file *file, struct page *page,
 126			     int offset, size_t size, loff_t *ppos, int more);
 127static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
 128				struct pipe_inode_info *pipe, size_t len,
 129				unsigned int flags);
 130
 131/*
 132 *	Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
 133 *	in the operation structures but are done directly via the socketcall() multiplexor.
 134 */
 135
 136static const struct file_operations socket_file_ops = {
 137	.owner =	THIS_MODULE,
 138	.llseek =	no_llseek,
 139	.aio_read =	sock_aio_read,
 140	.aio_write =	sock_aio_write,
 141	.poll =		sock_poll,
 142	.unlocked_ioctl = sock_ioctl,
 143#ifdef CONFIG_COMPAT
 144	.compat_ioctl = compat_sock_ioctl,
 145#endif
 146	.mmap =		sock_mmap,
 147	.open =		sock_no_open,	/* special open code to disallow open via /proc */
 148	.release =	sock_close,
 149	.fasync =	sock_fasync,
 150	.sendpage =	sock_sendpage,
 151	.splice_write = generic_splice_sendpage,
 152	.splice_read =	sock_splice_read,
 153};
 154
 155/*
 156 *	The protocol list. Each protocol is registered in here.
 157 */
 158
 159static DEFINE_SPINLOCK(net_family_lock);
 160static const struct net_proto_family __rcu *net_families[NPROTO] __read_mostly;
 161
 162/*
 163 *	Statistics counters of the socket lists
 164 */
 165
 166static DEFINE_PER_CPU(int, sockets_in_use);
 167
 168/*
 169 * Support routines.
 170 * Move socket addresses back and forth across the kernel/user
 171 * divide and look after the messy bits.
 172 */
 173
 174/**
 175 *	move_addr_to_kernel	-	copy a socket address into kernel space
 176 *	@uaddr: Address in user space
 177 *	@kaddr: Address in kernel space
 178 *	@ulen: Length in user space
 179 *
 180 *	The address is copied into kernel space. If the provided address is
 181 *	too long an error code of -EINVAL is returned. If the copy gives
 182 *	invalid addresses -EFAULT is returned. On a success 0 is returned.
 183 */
 184
 185int move_addr_to_kernel(void __user *uaddr, int ulen, struct sockaddr_storage *kaddr)
 186{
 187	if (ulen < 0 || ulen > sizeof(struct sockaddr_storage))
 188		return -EINVAL;
 189	if (ulen == 0)
 190		return 0;
 191	if (copy_from_user(kaddr, uaddr, ulen))
 192		return -EFAULT;
 193	return audit_sockaddr(ulen, kaddr);
 194}
 195
 196/**
 197 *	move_addr_to_user	-	copy an address to user space
 198 *	@kaddr: kernel space address
 199 *	@klen: length of address in kernel
 200 *	@uaddr: user space address
 201 *	@ulen: pointer to user length field
 202 *
 203 *	The value pointed to by ulen on entry is the buffer length available.
 204 *	This is overwritten with the buffer space used. -EINVAL is returned
 205 *	if an overlong buffer is specified or a negative buffer size. -EFAULT
 206 *	is returned if either the buffer or the length field are not
 207 *	accessible.
 208 *	After copying the data up to the limit the user specifies, the true
 209 *	length of the data is written over the length limit the user
 210 *	specified. Zero is returned for a success.
 211 */
 212
 213static int move_addr_to_user(struct sockaddr_storage *kaddr, int klen,
 214			     void __user *uaddr, int __user *ulen)
 215{
 216	int err;
 217	int len;
 218
 219	BUG_ON(klen > sizeof(struct sockaddr_storage));
 220	err = get_user(len, ulen);
 221	if (err)
 222		return err;
 223	if (len > klen)
 224		len = klen;
 225	if (len < 0)
 226		return -EINVAL;
 227	if (len) {
 228		if (audit_sockaddr(klen, kaddr))
 229			return -ENOMEM;
 230		if (copy_to_user(uaddr, kaddr, len))
 231			return -EFAULT;
 232	}
 233	/*
 234	 *      "fromlen shall refer to the value before truncation.."
 235	 *                      1003.1g
 236	 */
 237	return __put_user(klen, ulen);
 238}
 239
 240static struct kmem_cache *sock_inode_cachep __read_mostly;
 241
 242static struct inode *sock_alloc_inode(struct super_block *sb)
 243{
 244	struct socket_alloc *ei;
 245	struct socket_wq *wq;
 246
 247	ei = kmem_cache_alloc(sock_inode_cachep, GFP_KERNEL);
 248	if (!ei)
 249		return NULL;
 250	wq = kmalloc(sizeof(*wq), GFP_KERNEL);
 251	if (!wq) {
 252		kmem_cache_free(sock_inode_cachep, ei);
 253		return NULL;
 254	}
 255	init_waitqueue_head(&wq->wait);
 256	wq->fasync_list = NULL;
 257	RCU_INIT_POINTER(ei->socket.wq, wq);
 258
 259	ei->socket.state = SS_UNCONNECTED;
 260	ei->socket.flags = 0;
 261	ei->socket.ops = NULL;
 262	ei->socket.sk = NULL;
 263	ei->socket.file = NULL;
 264
 265	return &ei->vfs_inode;
 266}
 267
 268static void sock_destroy_inode(struct inode *inode)
 269{
 270	struct socket_alloc *ei;
 271	struct socket_wq *wq;
 272
 273	ei = container_of(inode, struct socket_alloc, vfs_inode);
 274	wq = rcu_dereference_protected(ei->socket.wq, 1);
 275	kfree_rcu(wq, rcu);
 276	kmem_cache_free(sock_inode_cachep, ei);
 277}
 278
 279static void init_once(void *foo)
 280{
 281	struct socket_alloc *ei = (struct socket_alloc *)foo;
 282
 283	inode_init_once(&ei->vfs_inode);
 284}
 285
 286static int init_inodecache(void)
 287{
 288	sock_inode_cachep = kmem_cache_create("sock_inode_cache",
 289					      sizeof(struct socket_alloc),
 290					      0,
 291					      (SLAB_HWCACHE_ALIGN |
 292					       SLAB_RECLAIM_ACCOUNT |
 293					       SLAB_MEM_SPREAD),
 294					      init_once);
 295	if (sock_inode_cachep == NULL)
 296		return -ENOMEM;
 297	return 0;
 298}
 299
 300static const struct super_operations sockfs_ops = {
 301	.alloc_inode	= sock_alloc_inode,
 302	.destroy_inode	= sock_destroy_inode,
 303	.statfs		= simple_statfs,
 304};
 305
 306/*
 307 * sockfs_dname() is called from d_path().
 308 */
 309static char *sockfs_dname(struct dentry *dentry, char *buffer, int buflen)
 310{
 311	return dynamic_dname(dentry, buffer, buflen, "socket:[%lu]",
 312				dentry->d_inode->i_ino);
 313}
 314
 315static const struct dentry_operations sockfs_dentry_operations = {
 316	.d_dname  = sockfs_dname,
 317};
 318
 319static struct dentry *sockfs_mount(struct file_system_type *fs_type,
 320			 int flags, const char *dev_name, void *data)
 321{
 322	return mount_pseudo(fs_type, "socket:", &sockfs_ops,
 323		&sockfs_dentry_operations, SOCKFS_MAGIC);
 324}
 325
 326static struct vfsmount *sock_mnt __read_mostly;
 327
 328static struct file_system_type sock_fs_type = {
 329	.name =		"sockfs",
 330	.mount =	sockfs_mount,
 331	.kill_sb =	kill_anon_super,
 332};
 333
 334/*
 335 *	Obtains the first available file descriptor and sets it up for use.
 336 *
 337 *	These functions create file structures and maps them to fd space
 338 *	of the current process. On success it returns file descriptor
 339 *	and file struct implicitly stored in sock->file.
 340 *	Note that another thread may close file descriptor before we return
 341 *	from this function. We use the fact that now we do not refer
 342 *	to socket after mapping. If one day we will need it, this
 343 *	function will increment ref. count on file by 1.
 344 *
 345 *	In any case returned fd MAY BE not valid!
 346 *	This race condition is unavoidable
 347 *	with shared fd spaces, we cannot solve it inside kernel,
 348 *	but we take care of internal coherence yet.
 349 */
 350
 351struct file *sock_alloc_file(struct socket *sock, int flags, const char *dname)
 352{
 353	struct qstr name = { .name = "" };
 354	struct path path;
 355	struct file *file;
 356
 357	if (dname) {
 358		name.name = dname;
 359		name.len = strlen(name.name);
 360	} else if (sock->sk) {
 361		name.name = sock->sk->sk_prot_creator->name;
 362		name.len = strlen(name.name);
 363	}
 364	path.dentry = d_alloc_pseudo(sock_mnt->mnt_sb, &name);
 365	if (unlikely(!path.dentry))
 366		return ERR_PTR(-ENOMEM);
 367	path.mnt = mntget(sock_mnt);
 368
 369	d_instantiate(path.dentry, SOCK_INODE(sock));
 370	SOCK_INODE(sock)->i_fop = &socket_file_ops;
 371
 372	file = alloc_file(&path, FMODE_READ | FMODE_WRITE,
 373		  &socket_file_ops);
 374	if (unlikely(!file)) {
 375		/* drop dentry, keep inode */
 376		ihold(path.dentry->d_inode);
 377		path_put(&path);
 378		return ERR_PTR(-ENFILE);
 379	}
 380
 381	sock->file = file;
 382	file->f_flags = O_RDWR | (flags & O_NONBLOCK);
 383	file->f_pos = 0;
 384	file->private_data = sock;
 385	return file;
 386}
 387EXPORT_SYMBOL(sock_alloc_file);
 388
 389static int sock_map_fd(struct socket *sock, int flags)
 390{
 391	struct file *newfile;
 392	int fd = get_unused_fd_flags(flags);
 393	if (unlikely(fd < 0))
 394		return fd;
 395
 396	newfile = sock_alloc_file(sock, flags, NULL);
 397	if (likely(!IS_ERR(newfile))) {
 398		fd_install(fd, newfile);
 399		return fd;
 400	}
 401
 402	put_unused_fd(fd);
 403	return PTR_ERR(newfile);
 404}
 405
 406struct socket *sock_from_file(struct file *file, int *err)
 407{
 408	if (file->f_op == &socket_file_ops)
 409		return file->private_data;	/* set in sock_map_fd */
 410
 411	*err = -ENOTSOCK;
 412	return NULL;
 413}
 414EXPORT_SYMBOL(sock_from_file);
 415
 416/**
 417 *	sockfd_lookup - Go from a file number to its socket slot
 418 *	@fd: file handle
 419 *	@err: pointer to an error code return
 420 *
 421 *	The file handle passed in is locked and the socket it is bound
 422 *	too is returned. If an error occurs the err pointer is overwritten
 423 *	with a negative errno code and NULL is returned. The function checks
 424 *	for both invalid handles and passing a handle which is not a socket.
 425 *
 426 *	On a success the socket object pointer is returned.
 427 */
 428
 429struct socket *sockfd_lookup(int fd, int *err)
 430{
 431	struct file *file;
 432	struct socket *sock;
 433
 434	file = fget(fd);
 435	if (!file) {
 436		*err = -EBADF;
 437		return NULL;
 438	}
 439
 440	sock = sock_from_file(file, err);
 441	if (!sock)
 442		fput(file);
 443	return sock;
 444}
 445EXPORT_SYMBOL(sockfd_lookup);
 446
 447static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed)
 448{
 449	struct file *file;
 450	struct socket *sock;
 451
 452	*err = -EBADF;
 453	file = fget_light(fd, fput_needed);
 454	if (file) {
 455		sock = sock_from_file(file, err);
 456		if (sock)
 457			return sock;
 458		fput_light(file, *fput_needed);
 459	}
 460	return NULL;
 461}
 462
 463#define XATTR_SOCKPROTONAME_SUFFIX "sockprotoname"
 464#define XATTR_NAME_SOCKPROTONAME (XATTR_SYSTEM_PREFIX XATTR_SOCKPROTONAME_SUFFIX)
 465#define XATTR_NAME_SOCKPROTONAME_LEN (sizeof(XATTR_NAME_SOCKPROTONAME)-1)
 466static ssize_t sockfs_getxattr(struct dentry *dentry,
 467			       const char *name, void *value, size_t size)
 468{
 469	const char *proto_name;
 470	size_t proto_size;
 471	int error;
 472
 473	error = -ENODATA;
 474	if (!strncmp(name, XATTR_NAME_SOCKPROTONAME, XATTR_NAME_SOCKPROTONAME_LEN)) {
 475		proto_name = dentry->d_name.name;
 476		proto_size = strlen(proto_name);
 477
 478		if (value) {
 479			error = -ERANGE;
 480			if (proto_size + 1 > size)
 481				goto out;
 482
 483			strncpy(value, proto_name, proto_size + 1);
 484		}
 485		error = proto_size + 1;
 486	}
 487
 488out:
 489	return error;
 490}
 491
 492static ssize_t sockfs_listxattr(struct dentry *dentry, char *buffer,
 493				size_t size)
 494{
 495	ssize_t len;
 496	ssize_t used = 0;
 497
 498	len = security_inode_listsecurity(dentry->d_inode, buffer, size);
 499	if (len < 0)
 500		return len;
 501	used += len;
 502	if (buffer) {
 503		if (size < used)
 504			return -ERANGE;
 505		buffer += len;
 506	}
 507
 508	len = (XATTR_NAME_SOCKPROTONAME_LEN + 1);
 509	used += len;
 510	if (buffer) {
 511		if (size < used)
 512			return -ERANGE;
 513		memcpy(buffer, XATTR_NAME_SOCKPROTONAME, len);
 514		buffer += len;
 515	}
 516
 517	return used;
 518}
 519
 520static const struct inode_operations sockfs_inode_ops = {
 521	.getxattr = sockfs_getxattr,
 522	.listxattr = sockfs_listxattr,
 523};
 524
 525/**
 526 *	sock_alloc	-	allocate a socket
 527 *
 528 *	Allocate a new inode and socket object. The two are bound together
 529 *	and initialised. The socket is then returned. If we are out of inodes
 530 *	NULL is returned.
 531 */
 532
 533static struct socket *sock_alloc(void)
 534{
 535	struct inode *inode;
 536	struct socket *sock;
 537
 538	inode = new_inode_pseudo(sock_mnt->mnt_sb);
 539	if (!inode)
 540		return NULL;
 541
 542	sock = SOCKET_I(inode);
 543
 544	kmemcheck_annotate_bitfield(sock, type);
 545	inode->i_ino = get_next_ino();
 546	inode->i_mode = S_IFSOCK | S_IRWXUGO;
 547	inode->i_uid = current_fsuid();
 548	inode->i_gid = current_fsgid();
 549	inode->i_op = &sockfs_inode_ops;
 550
 551	this_cpu_add(sockets_in_use, 1);
 552	return sock;
 553}
 554
 555/*
 556 *	In theory you can't get an open on this inode, but /proc provides
 557 *	a back door. Remember to keep it shut otherwise you'll let the
 558 *	creepy crawlies in.
 559 */
 560
 561static int sock_no_open(struct inode *irrelevant, struct file *dontcare)
 562{
 563	return -ENXIO;
 564}
 565
 566const struct file_operations bad_sock_fops = {
 567	.owner = THIS_MODULE,
 568	.open = sock_no_open,
 569	.llseek = noop_llseek,
 570};
 571
 572/**
 573 *	sock_release	-	close a socket
 574 *	@sock: socket to close
 575 *
 576 *	The socket is released from the protocol stack if it has a release
 577 *	callback, and the inode is then released if the socket is bound to
 578 *	an inode not a file.
 579 */
 580
 581void sock_release(struct socket *sock)
 582{
 583	if (sock->ops) {
 584		struct module *owner = sock->ops->owner;
 585
 586		sock->ops->release(sock);
 587		sock->ops = NULL;
 588		module_put(owner);
 589	}
 590
 591	if (rcu_dereference_protected(sock->wq, 1)->fasync_list)
 592		printk(KERN_ERR "sock_release: fasync list not empty!\n");
 593
 594	if (test_bit(SOCK_EXTERNALLY_ALLOCATED, &sock->flags))
 595		return;
 596
 597	this_cpu_sub(sockets_in_use, 1);
 598	if (!sock->file) {
 599		iput(SOCK_INODE(sock));
 600		return;
 601	}
 602	sock->file = NULL;
 603}
 604EXPORT_SYMBOL(sock_release);
 605
 606int sock_tx_timestamp(struct sock *sk, __u8 *tx_flags)
 607{
 608	*tx_flags = 0;
 609	if (sock_flag(sk, SOCK_TIMESTAMPING_TX_HARDWARE))
 610		*tx_flags |= SKBTX_HW_TSTAMP;
 611	if (sock_flag(sk, SOCK_TIMESTAMPING_TX_SOFTWARE))
 612		*tx_flags |= SKBTX_SW_TSTAMP;
 613	if (sock_flag(sk, SOCK_WIFI_STATUS))
 614		*tx_flags |= SKBTX_WIFI_STATUS;
 615	return 0;
 616}
 617EXPORT_SYMBOL(sock_tx_timestamp);
 618
 619static inline int __sock_sendmsg_nosec(struct kiocb *iocb, struct socket *sock,
 620				       struct msghdr *msg, size_t size)
 621{
 622	struct sock_iocb *si = kiocb_to_siocb(iocb);
 623
 624	si->sock = sock;
 625	si->scm = NULL;
 626	si->msg = msg;
 627	si->size = size;
 628
 629	return sock->ops->sendmsg(iocb, sock, msg, size);
 630}
 631
 632static inline int __sock_sendmsg(struct kiocb *iocb, struct socket *sock,
 633				 struct msghdr *msg, size_t size)
 634{
 635	int err = security_socket_sendmsg(sock, msg, size);
 636
 637	return err ?: __sock_sendmsg_nosec(iocb, sock, msg, size);
 638}
 639
 640int sock_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
 641{
 642	struct kiocb iocb;
 643	struct sock_iocb siocb;
 644	int ret;
 645
 646	init_sync_kiocb(&iocb, NULL);
 647	iocb.private = &siocb;
 648	ret = __sock_sendmsg(&iocb, sock, msg, size);
 649	if (-EIOCBQUEUED == ret)
 650		ret = wait_on_sync_kiocb(&iocb);
 651	return ret;
 652}
 653EXPORT_SYMBOL(sock_sendmsg);
 654
 655static int sock_sendmsg_nosec(struct socket *sock, struct msghdr *msg, size_t size)
 656{
 657	struct kiocb iocb;
 658	struct sock_iocb siocb;
 659	int ret;
 660
 661	init_sync_kiocb(&iocb, NULL);
 662	iocb.private = &siocb;
 663	ret = __sock_sendmsg_nosec(&iocb, sock, msg, size);
 664	if (-EIOCBQUEUED == ret)
 665		ret = wait_on_sync_kiocb(&iocb);
 666	return ret;
 667}
 668
 669int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
 670		   struct kvec *vec, size_t num, size_t size)
 671{
 672	mm_segment_t oldfs = get_fs();
 673	int result;
 674
 675	set_fs(KERNEL_DS);
 676	/*
 677	 * the following is safe, since for compiler definitions of kvec and
 678	 * iovec are identical, yielding the same in-core layout and alignment
 679	 */
 680	msg->msg_iov = (struct iovec *)vec;
 681	msg->msg_iovlen = num;
 682	result = sock_sendmsg(sock, msg, size);
 683	set_fs(oldfs);
 684	return result;
 685}
 686EXPORT_SYMBOL(kernel_sendmsg);
 687
 688static int ktime2ts(ktime_t kt, struct timespec *ts)
 689{
 690	if (kt.tv64) {
 691		*ts = ktime_to_timespec(kt);
 692		return 1;
 693	} else {
 694		return 0;
 695	}
 696}
 697
 698/*
 699 * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
 700 */
 701void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
 702	struct sk_buff *skb)
 703{
 704	int need_software_tstamp = sock_flag(sk, SOCK_RCVTSTAMP);
 705	struct timespec ts[3];
 706	int empty = 1;
 707	struct skb_shared_hwtstamps *shhwtstamps =
 708		skb_hwtstamps(skb);
 709
 710	/* Race occurred between timestamp enabling and packet
 711	   receiving.  Fill in the current time for now. */
 712	if (need_software_tstamp && skb->tstamp.tv64 == 0)
 713		__net_timestamp(skb);
 714
 715	if (need_software_tstamp) {
 716		if (!sock_flag(sk, SOCK_RCVTSTAMPNS)) {
 717			struct timeval tv;
 718			skb_get_timestamp(skb, &tv);
 719			put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMP,
 720				 sizeof(tv), &tv);
 721		} else {
 722			skb_get_timestampns(skb, &ts[0]);
 723			put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPNS,
 724				 sizeof(ts[0]), &ts[0]);
 725		}
 726	}
 727
 728
 729	memset(ts, 0, sizeof(ts));
 730	if (skb->tstamp.tv64 &&
 731	    sock_flag(sk, SOCK_TIMESTAMPING_SOFTWARE)) {
 732		skb_get_timestampns(skb, ts + 0);
 733		empty = 0;
 734	}
 735	if (shhwtstamps) {
 736		if (sock_flag(sk, SOCK_TIMESTAMPING_SYS_HARDWARE) &&
 737		    ktime2ts(shhwtstamps->syststamp, ts + 1))
 738			empty = 0;
 739		if (sock_flag(sk, SOCK_TIMESTAMPING_RAW_HARDWARE) &&
 740		    ktime2ts(shhwtstamps->hwtstamp, ts + 2))
 741			empty = 0;
 742	}
 743	if (!empty)
 744		put_cmsg(msg, SOL_SOCKET,
 745			 SCM_TIMESTAMPING, sizeof(ts), &ts);
 746}
 747EXPORT_SYMBOL_GPL(__sock_recv_timestamp);
 748
 749void __sock_recv_wifi_status(struct msghdr *msg, struct sock *sk,
 750	struct sk_buff *skb)
 751{
 752	int ack;
 753
 754	if (!sock_flag(sk, SOCK_WIFI_STATUS))
 755		return;
 756	if (!skb->wifi_acked_valid)
 757		return;
 758
 759	ack = skb->wifi_acked;
 760
 761	put_cmsg(msg, SOL_SOCKET, SCM_WIFI_STATUS, sizeof(ack), &ack);
 762}
 763EXPORT_SYMBOL_GPL(__sock_recv_wifi_status);
 764
 765static inline void sock_recv_drops(struct msghdr *msg, struct sock *sk,
 766				   struct sk_buff *skb)
 767{
 768	if (sock_flag(sk, SOCK_RXQ_OVFL) && skb && skb->dropcount)
 769		put_cmsg(msg, SOL_SOCKET, SO_RXQ_OVFL,
 770			sizeof(__u32), &skb->dropcount);
 771}
 772
 773void __sock_recv_ts_and_drops(struct msghdr *msg, struct sock *sk,
 774	struct sk_buff *skb)
 775{
 776	sock_recv_timestamp(msg, sk, skb);
 777	sock_recv_drops(msg, sk, skb);
 778}
 779EXPORT_SYMBOL_GPL(__sock_recv_ts_and_drops);
 780
 781static inline int __sock_recvmsg_nosec(struct kiocb *iocb, struct socket *sock,
 782				       struct msghdr *msg, size_t size, int flags)
 783{
 784	struct sock_iocb *si = kiocb_to_siocb(iocb);
 785
 786	si->sock = sock;
 787	si->scm = NULL;
 788	si->msg = msg;
 789	si->size = size;
 790	si->flags = flags;
 791
 792	return sock->ops->recvmsg(iocb, sock, msg, size, flags);
 793}
 794
 795static inline int __sock_recvmsg(struct kiocb *iocb, struct socket *sock,
 796				 struct msghdr *msg, size_t size, int flags)
 797{
 798	int err = security_socket_recvmsg(sock, msg, size, flags);
 799
 800	return err ?: __sock_recvmsg_nosec(iocb, sock, msg, size, flags);
 801}
 802
 803int sock_recvmsg(struct socket *sock, struct msghdr *msg,
 804		 size_t size, int flags)
 805{
 806	struct kiocb iocb;
 807	struct sock_iocb siocb;
 808	int ret;
 809
 810	init_sync_kiocb(&iocb, NULL);
 811	iocb.private = &siocb;
 812	ret = __sock_recvmsg(&iocb, sock, msg, size, flags);
 813	if (-EIOCBQUEUED == ret)
 814		ret = wait_on_sync_kiocb(&iocb);
 815	return ret;
 816}
 817EXPORT_SYMBOL(sock_recvmsg);
 818
 819static int sock_recvmsg_nosec(struct socket *sock, struct msghdr *msg,
 820			      size_t size, int flags)
 821{
 822	struct kiocb iocb;
 823	struct sock_iocb siocb;
 824	int ret;
 825
 826	init_sync_kiocb(&iocb, NULL);
 827	iocb.private = &siocb;
 828	ret = __sock_recvmsg_nosec(&iocb, sock, msg, size, flags);
 829	if (-EIOCBQUEUED == ret)
 830		ret = wait_on_sync_kiocb(&iocb);
 831	return ret;
 832}
 833
 834/**
 835 * kernel_recvmsg - Receive a message from a socket (kernel space)
 836 * @sock:       The socket to receive the message from
 837 * @msg:        Received message
 838 * @vec:        Input s/g array for message data
 839 * @num:        Size of input s/g array
 840 * @size:       Number of bytes to read
 841 * @flags:      Message flags (MSG_DONTWAIT, etc...)
 842 *
 843 * On return the msg structure contains the scatter/gather array passed in the
 844 * vec argument. The array is modified so that it consists of the unfilled
 845 * portion of the original array.
 846 *
 847 * The returned value is the total number of bytes received, or an error.
 848 */
 849int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
 850		   struct kvec *vec, size_t num, size_t size, int flags)
 851{
 852	mm_segment_t oldfs = get_fs();
 853	int result;
 854
 855	set_fs(KERNEL_DS);
 856	/*
 857	 * the following is safe, since for compiler definitions of kvec and
 858	 * iovec are identical, yielding the same in-core layout and alignment
 859	 */
 860	msg->msg_iov = (struct iovec *)vec, msg->msg_iovlen = num;
 861	result = sock_recvmsg(sock, msg, size, flags);
 862	set_fs(oldfs);
 863	return result;
 864}
 865EXPORT_SYMBOL(kernel_recvmsg);
 866
 867static void sock_aio_dtor(struct kiocb *iocb)
 868{
 869	kfree(iocb->private);
 870}
 871
 872static ssize_t sock_sendpage(struct file *file, struct page *page,
 873			     int offset, size_t size, loff_t *ppos, int more)
 874{
 875	struct socket *sock;
 876	int flags;
 877
 878	sock = file->private_data;
 879
 880	flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
 881	/* more is a combination of MSG_MORE and MSG_SENDPAGE_NOTLAST */
 882	flags |= more;
 883
 884	return kernel_sendpage(sock, page, offset, size, flags);
 885}
 886
 887static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
 888				struct pipe_inode_info *pipe, size_t len,
 889				unsigned int flags)
 890{
 891	struct socket *sock = file->private_data;
 892
 893	if (unlikely(!sock->ops->splice_read))
 894		return -EINVAL;
 895
 896	return sock->ops->splice_read(sock, ppos, pipe, len, flags);
 897}
 898
 899static struct sock_iocb *alloc_sock_iocb(struct kiocb *iocb,
 900					 struct sock_iocb *siocb)
 901{
 902	if (!is_sync_kiocb(iocb)) {
 903		siocb = kmalloc(sizeof(*siocb), GFP_KERNEL);
 904		if (!siocb)
 905			return NULL;
 906		iocb->ki_dtor = sock_aio_dtor;
 907	}
 908
 909	siocb->kiocb = iocb;
 910	iocb->private = siocb;
 911	return siocb;
 912}
 913
 914static ssize_t do_sock_read(struct msghdr *msg, struct kiocb *iocb,
 915		struct file *file, const struct iovec *iov,
 916		unsigned long nr_segs)
 917{
 918	struct socket *sock = file->private_data;
 919	size_t size = 0;
 920	int i;
 921
 922	for (i = 0; i < nr_segs; i++)
 923		size += iov[i].iov_len;
 924
 925	msg->msg_name = NULL;
 926	msg->msg_namelen = 0;
 927	msg->msg_control = NULL;
 928	msg->msg_controllen = 0;
 929	msg->msg_iov = (struct iovec *)iov;
 930	msg->msg_iovlen = nr_segs;
 931	msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
 932
 933	return __sock_recvmsg(iocb, sock, msg, size, msg->msg_flags);
 934}
 935
 936static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov,
 937				unsigned long nr_segs, loff_t pos)
 938{
 939	struct sock_iocb siocb, *x;
 940
 941	if (pos != 0)
 942		return -ESPIPE;
 943
 944	if (iocb->ki_left == 0)	/* Match SYS5 behaviour */
 945		return 0;
 946
 947
 948	x = alloc_sock_iocb(iocb, &siocb);
 949	if (!x)
 950		return -ENOMEM;
 951	return do_sock_read(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
 952}
 953
 954static ssize_t do_sock_write(struct msghdr *msg, struct kiocb *iocb,
 955			struct file *file, const struct iovec *iov,
 956			unsigned long nr_segs)
 957{
 958	struct socket *sock = file->private_data;
 959	size_t size = 0;
 960	int i;
 961
 962	for (i = 0; i < nr_segs; i++)
 963		size += iov[i].iov_len;
 964
 965	msg->msg_name = NULL;
 966	msg->msg_namelen = 0;
 967	msg->msg_control = NULL;
 968	msg->msg_controllen = 0;
 969	msg->msg_iov = (struct iovec *)iov;
 970	msg->msg_iovlen = nr_segs;
 971	msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
 972	if (sock->type == SOCK_SEQPACKET)
 973		msg->msg_flags |= MSG_EOR;
 974
 975	return __sock_sendmsg(iocb, sock, msg, size);
 976}
 977
 978static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov,
 979			  unsigned long nr_segs, loff_t pos)
 980{
 981	struct sock_iocb siocb, *x;
 982
 983	if (pos != 0)
 984		return -ESPIPE;
 985
 986	x = alloc_sock_iocb(iocb, &siocb);
 987	if (!x)
 988		return -ENOMEM;
 989
 990	return do_sock_write(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
 991}
 992
 993/*
 994 * Atomic setting of ioctl hooks to avoid race
 995 * with module unload.
 996 */
 997
 998static DEFINE_MUTEX(br_ioctl_mutex);
 999static int (*br_ioctl_hook) (struct net *, unsigned int cmd, void __user *arg);
1000
1001void brioctl_set(int (*hook) (struct net *, unsigned int, void __user *))
1002{
1003	mutex_lock(&br_ioctl_mutex);
1004	br_ioctl_hook = hook;
1005	mutex_unlock(&br_ioctl_mutex);
1006}
1007EXPORT_SYMBOL(brioctl_set);
1008
1009static DEFINE_MUTEX(vlan_ioctl_mutex);
1010static int (*vlan_ioctl_hook) (struct net *, void __user *arg);
1011
1012void vlan_ioctl_set(int (*hook) (struct net *, void __user *))
1013{
1014	mutex_lock(&vlan_ioctl_mutex);
1015	vlan_ioctl_hook = hook;
1016	mutex_unlock(&vlan_ioctl_mutex);
1017}
1018EXPORT_SYMBOL(vlan_ioctl_set);
1019
1020static DEFINE_MUTEX(dlci_ioctl_mutex);
1021static int (*dlci_ioctl_hook) (unsigned int, void __user *);
1022
1023void dlci_ioctl_set(int (*hook) (unsigned int, void __user *))
1024{
1025	mutex_lock(&dlci_ioctl_mutex);
1026	dlci_ioctl_hook = hook;
1027	mutex_unlock(&dlci_ioctl_mutex);
1028}
1029EXPORT_SYMBOL(dlci_ioctl_set);
1030
1031static long sock_do_ioctl(struct net *net, struct socket *sock,
1032				 unsigned int cmd, unsigned long arg)
1033{
1034	int err;
1035	void __user *argp = (void __user *)arg;
1036
1037	err = sock->ops->ioctl(sock, cmd, arg);
1038
1039	/*
1040	 * If this ioctl is unknown try to hand it down
1041	 * to the NIC driver.
1042	 */
1043	if (err == -ENOIOCTLCMD)
1044		err = dev_ioctl(net, cmd, argp);
1045
1046	return err;
1047}
1048
1049/*
1050 *	With an ioctl, arg may well be a user mode pointer, but we don't know
1051 *	what to do with it - that's up to the protocol still.
1052 */
1053
1054static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
1055{
1056	struct socket *sock;
1057	struct sock *sk;
1058	void __user *argp = (void __user *)arg;
1059	int pid, err;
1060	struct net *net;
1061
1062	sock = file->private_data;
1063	sk = sock->sk;
1064	net = sock_net(sk);
1065	if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
1066		err = dev_ioctl(net, cmd, argp);
1067	} else
1068#ifdef CONFIG_WEXT_CORE
1069	if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
1070		err = dev_ioctl(net, cmd, argp);
1071	} else
1072#endif
1073		switch (cmd) {
1074		case FIOSETOWN:
1075		case SIOCSPGRP:
1076			err = -EFAULT;
1077			if (get_user(pid, (int __user *)argp))
1078				break;
1079			err = f_setown(sock->file, pid, 1);
1080			break;
1081		case FIOGETOWN:
1082		case SIOCGPGRP:
1083			err = put_user(f_getown(sock->file),
1084				       (int __user *)argp);
1085			break;
1086		case SIOCGIFBR:
1087		case SIOCSIFBR:
1088		case SIOCBRADDBR:
1089		case SIOCBRDELBR:
1090			err = -ENOPKG;
1091			if (!br_ioctl_hook)
1092				request_module("bridge");
1093
1094			mutex_lock(&br_ioctl_mutex);
1095			if (br_ioctl_hook)
1096				err = br_ioctl_hook(net, cmd, argp);
1097			mutex_unlock(&br_ioctl_mutex);
1098			break;
1099		case SIOCGIFVLAN:
1100		case SIOCSIFVLAN:
1101			err = -ENOPKG;
1102			if (!vlan_ioctl_hook)
1103				request_module("8021q");
1104
1105			mutex_lock(&vlan_ioctl_mutex);
1106			if (vlan_ioctl_hook)
1107				err = vlan_ioctl_hook(net, argp);
1108			mutex_unlock(&vlan_ioctl_mutex);
1109			break;
1110		case SIOCADDDLCI:
1111		case SIOCDELDLCI:
1112			err = -ENOPKG;
1113			if (!dlci_ioctl_hook)
1114				request_module("dlci");
1115
1116			mutex_lock(&dlci_ioctl_mutex);
1117			if (dlci_ioctl_hook)
1118				err = dlci_ioctl_hook(cmd, argp);
1119			mutex_unlock(&dlci_ioctl_mutex);
1120			break;
1121		default:
1122			err = sock_do_ioctl(net, sock, cmd, arg);
1123			break;
1124		}
1125	return err;
1126}
1127
1128int sock_create_lite(int family, int type, int protocol, struct socket **res)
1129{
1130	int err;
1131	struct socket *sock = NULL;
1132
1133	err = security_socket_create(family, type, protocol, 1);
1134	if (err)
1135		goto out;
1136
1137	sock = sock_alloc();
1138	if (!sock) {
1139		err = -ENOMEM;
1140		goto out;
1141	}
1142
1143	sock->type = type;
1144	err = security_socket_post_create(sock, family, type, protocol, 1);
1145	if (err)
1146		goto out_release;
1147
1148out:
1149	*res = sock;
1150	return err;
1151out_release:
1152	sock_release(sock);
1153	sock = NULL;
1154	goto out;
1155}
1156EXPORT_SYMBOL(sock_create_lite);
1157
1158/* No kernel lock held - perfect */
1159static unsigned int sock_poll(struct file *file, poll_table *wait)
1160{
1161	struct socket *sock;
1162
1163	/*
1164	 *      We can't return errors to poll, so it's either yes or no.
1165	 */
1166	sock = file->private_data;
1167	return sock->ops->poll(file, sock, wait);
1168}
1169
1170static int sock_mmap(struct file *file, struct vm_area_struct *vma)
1171{
1172	struct socket *sock = file->private_data;
1173
1174	return sock->ops->mmap(file, sock, vma);
1175}
1176
1177static int sock_close(struct inode *inode, struct file *filp)
1178{
1179	/*
1180	 *      It was possible the inode is NULL we were
1181	 *      closing an unfinished socket.
1182	 */
1183
1184	if (!inode) {
1185		printk(KERN_DEBUG "sock_close: NULL inode\n");
1186		return 0;
1187	}
1188	sock_release(SOCKET_I(inode));
1189	return 0;
1190}
1191
1192/*
1193 *	Update the socket async list
1194 *
1195 *	Fasync_list locking strategy.
1196 *
1197 *	1. fasync_list is modified only under process context socket lock
1198 *	   i.e. under semaphore.
1199 *	2. fasync_list is used under read_lock(&sk->sk_callback_lock)
1200 *	   or under socket lock
1201 */
1202
1203static int sock_fasync(int fd, struct file *filp, int on)
1204{
1205	struct socket *sock = filp->private_data;
1206	struct sock *sk = sock->sk;
1207	struct socket_wq *wq;
1208
1209	if (sk == NULL)
1210		return -EINVAL;
1211
1212	lock_sock(sk);
1213	wq = rcu_dereference_protected(sock->wq, sock_owned_by_user(sk));
1214	fasync_helper(fd, filp, on, &wq->fasync_list);
1215
1216	if (!wq->fasync_list)
1217		sock_reset_flag(sk, SOCK_FASYNC);
1218	else
1219		sock_set_flag(sk, SOCK_FASYNC);
1220
1221	release_sock(sk);
1222	return 0;
1223}
1224
1225/* This function may be called only under socket lock or callback_lock or rcu_lock */
1226
1227int sock_wake_async(struct socket *sock, int how, int band)
1228{
1229	struct socket_wq *wq;
1230
1231	if (!sock)
1232		return -1;
1233	rcu_read_lock();
1234	wq = rcu_dereference(sock->wq);
1235	if (!wq || !wq->fasync_list) {
1236		rcu_read_unlock();
1237		return -1;
1238	}
1239	switch (how) {
1240	case SOCK_WAKE_WAITD:
1241		if (test_bit(SOCK_ASYNC_WAITDATA, &sock->flags))
1242			break;
1243		goto call_kill;
1244	case SOCK_WAKE_SPACE:
1245		if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags))
1246			break;
1247		/* fall through */
1248	case SOCK_WAKE_IO:
1249call_kill:
1250		kill_fasync(&wq->fasync_list, SIGIO, band);
1251		break;
1252	case SOCK_WAKE_URG:
1253		kill_fasync(&wq->fasync_list, SIGURG, band);
1254	}
1255	rcu_read_unlock();
1256	return 0;
1257}
1258EXPORT_SYMBOL(sock_wake_async);
1259
1260int __sock_create(struct net *net, int family, int type, int protocol,
1261			 struct socket **res, int kern)
1262{
1263	int err;
1264	struct socket *sock;
1265	const struct net_proto_family *pf;
1266
1267	/*
1268	 *      Check protocol is in range
1269	 */
1270	if (family < 0 || family >= NPROTO)
1271		return -EAFNOSUPPORT;
1272	if (type < 0 || type >= SOCK_MAX)
1273		return -EINVAL;
1274
1275	/* Compatibility.
1276
1277	   This uglymoron is moved from INET layer to here to avoid
1278	   deadlock in module load.
1279	 */
1280	if (family == PF_INET && type == SOCK_PACKET) {
1281		static int warned;
1282		if (!warned) {
1283			warned = 1;
1284			printk(KERN_INFO "%s uses obsolete (PF_INET,SOCK_PACKET)\n",
1285			       current->comm);
1286		}
1287		family = PF_PACKET;
1288	}
1289
1290	err = security_socket_create(family, type, protocol, kern);
1291	if (err)
1292		return err;
1293
1294	/*
1295	 *	Allocate the socket and allow the family to set things up. if
1296	 *	the protocol is 0, the family is instructed to select an appropriate
1297	 *	default.
1298	 */
1299	sock = sock_alloc();
1300	if (!sock) {
1301		net_warn_ratelimited("socket: no more sockets\n");
1302		return -ENFILE;	/* Not exactly a match, but its the
1303				   closest posix thing */
1304	}
1305
1306	sock->type = type;
1307
1308#ifdef CONFIG_MODULES
1309	/* Attempt to load a protocol module if the find failed.
1310	 *
1311	 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
1312	 * requested real, full-featured networking support upon configuration.
1313	 * Otherwise module support will break!
1314	 */
1315	if (rcu_access_pointer(net_families[family]) == NULL)
1316		request_module("net-pf-%d", family);
1317#endif
1318
1319	rcu_read_lock();
1320	pf = rcu_dereference(net_families[family]);
1321	err = -EAFNOSUPPORT;
1322	if (!pf)
1323		goto out_release;
1324
1325	/*
1326	 * We will call the ->create function, that possibly is in a loadable
1327	 * module, so we have to bump that loadable module refcnt first.
1328	 */
1329	if (!try_module_get(pf->owner))
1330		goto out_release;
1331
1332	/* Now protected by module ref count */
1333	rcu_read_unlock();
1334
1335	err = pf->create(net, sock, protocol, kern);
1336	if (err < 0)
1337		goto out_module_put;
1338
1339	/*
1340	 * Now to bump the refcnt of the [loadable] module that owns this
1341	 * socket at sock_release time we decrement its refcnt.
1342	 */
1343	if (!try_module_get(sock->ops->owner))
1344		goto out_module_busy;
1345
1346	/*
1347	 * Now that we're done with the ->create function, the [loadable]
1348	 * module can have its refcnt decremented
1349	 */
1350	module_put(pf->owner);
1351	err = security_socket_post_create(sock, family, type, protocol, kern);
1352	if (err)
1353		goto out_sock_release;
1354	*res = sock;
1355
1356	return 0;
1357
1358out_module_busy:
1359	err = -EAFNOSUPPORT;
1360out_module_put:
1361	sock->ops = NULL;
1362	module_put(pf->owner);
1363out_sock_release:
1364	sock_release(sock);
1365	return err;
1366
1367out_release:
1368	rcu_read_unlock();
1369	goto out_sock_release;
1370}
1371EXPORT_SYMBOL(__sock_create);
1372
1373int sock_create(int family, int type, int protocol, struct socket **res)
1374{
1375	return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0);
1376}
1377EXPORT_SYMBOL(sock_create);
1378
1379int sock_create_kern(int family, int type, int protocol, struct socket **res)
1380{
1381	return __sock_create(&init_net, family, type, protocol, res, 1);
1382}
1383EXPORT_SYMBOL(sock_create_kern);
1384
1385SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol)
1386{
1387	int retval;
1388	struct socket *sock;
1389	int flags;
1390
1391	/* Check the SOCK_* constants for consistency.  */
1392	BUILD_BUG_ON(SOCK_CLOEXEC != O_CLOEXEC);
1393	BUILD_BUG_ON((SOCK_MAX | SOCK_TYPE_MASK) != SOCK_TYPE_MASK);
1394	BUILD_BUG_ON(SOCK_CLOEXEC & SOCK_TYPE_MASK);
1395	BUILD_BUG_ON(SOCK_NONBLOCK & SOCK_TYPE_MASK);
1396
1397	flags = type & ~SOCK_TYPE_MASK;
1398	if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1399		return -EINVAL;
1400	type &= SOCK_TYPE_MASK;
1401
1402	if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1403		flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1404
1405	retval = sock_create(family, type, protocol, &sock);
1406	if (retval < 0)
1407		goto out;
1408
1409	retval = sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK));
1410	if (retval < 0)
1411		goto out_release;
1412
1413out:
1414	/* It may be already another descriptor 8) Not kernel problem. */
1415	return retval;
1416
1417out_release:
1418	sock_release(sock);
1419	return retval;
1420}
1421
1422/*
1423 *	Create a pair of connected sockets.
1424 */
1425
1426SYSCALL_DEFINE4(socketpair, int, family, int, type, int, protocol,
1427		int __user *, usockvec)
1428{
1429	struct socket *sock1, *sock2;
1430	int fd1, fd2, err;
1431	struct file *newfile1, *newfile2;
1432	int flags;
1433
1434	flags = type & ~SOCK_TYPE_MASK;
1435	if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1436		return -EINVAL;
1437	type &= SOCK_TYPE_MASK;
1438
1439	if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1440		flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1441
1442	/*
1443	 * Obtain the first socket and check if the underlying protocol
1444	 * supports the socketpair call.
1445	 */
1446
1447	err = sock_create(family, type, protocol, &sock1);
1448	if (err < 0)
1449		goto out;
1450
1451	err = sock_create(family, type, protocol, &sock2);
1452	if (err < 0)
1453		goto out_release_1;
1454
1455	err = sock1->ops->socketpair(sock1, sock2);
1456	if (err < 0)
1457		goto out_release_both;
1458
1459	fd1 = get_unused_fd_flags(flags);
1460	if (unlikely(fd1 < 0)) {
1461		err = fd1;
1462		goto out_release_both;
1463	}
1464	fd2 = get_unused_fd_flags(flags);
1465	if (unlikely(fd2 < 0)) {
1466		err = fd2;
1467		put_unused_fd(fd1);
1468		goto out_release_both;
1469	}
1470
1471	newfile1 = sock_alloc_file(sock1, flags, NULL);
1472	if (unlikely(IS_ERR(newfile1))) {
1473		err = PTR_ERR(newfile1);
1474		put_unused_fd(fd1);
1475		put_unused_fd(fd2);
1476		goto out_release_both;
1477	}
1478
1479	newfile2 = sock_alloc_file(sock2, flags, NULL);
1480	if (IS_ERR(newfile2)) {
1481		err = PTR_ERR(newfile2);
1482		fput(newfile1);
1483		put_unused_fd(fd1);
1484		put_unused_fd(fd2);
1485		sock_release(sock2);
1486		goto out;
1487	}
1488
1489	audit_fd_pair(fd1, fd2);
1490	fd_install(fd1, newfile1);
1491	fd_install(fd2, newfile2);
1492	/* fd1 and fd2 may be already another descriptors.
1493	 * Not kernel problem.
1494	 */
1495
1496	err = put_user(fd1, &usockvec[0]);
1497	if (!err)
1498		err = put_user(fd2, &usockvec[1]);
1499	if (!err)
1500		return 0;
1501
1502	sys_close(fd2);
1503	sys_close(fd1);
1504	return err;
1505
1506out_release_both:
1507	sock_release(sock2);
1508out_release_1:
1509	sock_release(sock1);
1510out:
1511	return err;
1512}
1513
1514/*
1515 *	Bind a name to a socket. Nothing much to do here since it's
1516 *	the protocol's responsibility to handle the local address.
1517 *
1518 *	We move the socket address to kernel space before we call
1519 *	the protocol layer (having also checked the address is ok).
1520 */
1521
1522SYSCALL_DEFINE3(bind, int, fd, struct sockaddr __user *, umyaddr, int, addrlen)
1523{
1524	struct socket *sock;
1525	struct sockaddr_storage address;
1526	int err, fput_needed;
1527
1528	sock = sockfd_lookup_light(fd, &err, &fput_needed);
1529	if (sock) {
1530		err = move_addr_to_kernel(umyaddr, addrlen, &address);
1531		if (err >= 0) {
1532			err = security_socket_bind(sock,
1533						   (struct sockaddr *)&address,
1534						   addrlen);
1535			if (!err)
1536				err = sock->ops->bind(sock,
1537						      (struct sockaddr *)
1538						      &address, addrlen);
1539		}
1540		fput_light(sock->file, fput_needed);
1541	}
1542	return err;
1543}
1544
1545/*
1546 *	Perform a listen. Basically, we allow the protocol to do anything
1547 *	necessary for a listen, and if that works, we mark the socket as
1548 *	ready for listening.
1549 */
1550
1551SYSCALL_DEFINE2(listen, int, fd, int, backlog)
1552{
1553	struct socket *sock;
1554	int err, fput_needed;
1555	int somaxconn;
1556
1557	sock = sockfd_lookup_light(fd, &err, &fput_needed);
1558	if (sock) {
1559		somaxconn = sock_net(sock->sk)->core.sysctl_somaxconn;
1560		if ((unsigned int)backlog > somaxconn)
1561			backlog = somaxconn;
1562
1563		err = security_socket_listen(sock, backlog);
1564		if (!err)
1565			err = sock->ops->listen(sock, backlog);
1566
1567		fput_light(sock->file, fput_needed);
1568	}
1569	return err;
1570}
1571
1572/*
1573 *	For accept, we attempt to create a new socket, set up the link
1574 *	with the client, wake up the client, then return the new
1575 *	connected fd. We collect the address of the connector in kernel
1576 *	space and move it to user at the very end. This is unclean because
1577 *	we open the socket then return an error.
1578 *
1579 *	1003.1g adds the ability to recvmsg() to query connection pending
1580 *	status to recvmsg. We need to add that support in a way thats
1581 *	clean when we restucture accept also.
1582 */
1583
1584SYSCALL_DEFINE4(accept4, int, fd, struct sockaddr __user *, upeer_sockaddr,
1585		int __user *, upeer_addrlen, int, flags)
1586{
1587	struct socket *sock, *newsock;
1588	struct file *newfile;
1589	int err, len, newfd, fput_needed;
1590	struct sockaddr_storage address;
1591
1592	if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1593		return -EINVAL;
1594
1595	if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1596		flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1597
1598	sock = sockfd_lookup_light(fd, &err, &fput_needed);
1599	if (!sock)
1600		goto out;
1601
1602	err = -ENFILE;
1603	newsock = sock_alloc();
1604	if (!newsock)
1605		goto out_put;
1606
1607	newsock->type = sock->type;
1608	newsock->ops = sock->ops;
1609
1610	/*
1611	 * We don't need try_module_get here, as the listening socket (sock)
1612	 * has the protocol module (sock->ops->owner) held.
1613	 */
1614	__module_get(newsock->ops->owner);
1615
1616	newfd = get_unused_fd_flags(flags);
1617	if (unlikely(newfd < 0)) {
1618		err = newfd;
1619		sock_release(newsock);
1620		goto out_put;
1621	}
1622	newfile = sock_alloc_file(newsock, flags, sock->sk->sk_prot_creator->name);
1623	if (unlikely(IS_ERR(newfile))) {
1624		err = PTR_ERR(newfile);
1625		put_unused_fd(newfd);
1626		sock_release(newsock);
1627		goto out_put;
1628	}
1629
1630	err = security_socket_accept(sock, newsock);
1631	if (err)
1632		goto out_fd;
1633
1634	err = sock->ops->accept(sock, newsock, sock->file->f_flags);
1635	if (err < 0)
1636		goto out_fd;
1637
1638	if (upeer_sockaddr) {
1639		if (newsock->ops->getname(newsock, (struct sockaddr *)&address,
1640					  &len, 2) < 0) {
1641			err = -ECONNABORTED;
1642			goto out_fd;
1643		}
1644		err = move_addr_to_user(&address,
1645					len, upeer_sockaddr, upeer_addrlen);
1646		if (err < 0)
1647			goto out_fd;
1648	}
1649
1650	/* File flags are not inherited via accept() unlike another OSes. */
1651
1652	fd_install(newfd, newfile);
1653	err = newfd;
1654
1655out_put:
1656	fput_light(sock->file, fput_needed);
1657out:
1658	return err;
1659out_fd:
1660	fput(newfile);
1661	put_unused_fd(newfd);
1662	goto out_put;
1663}
1664
1665SYSCALL_DEFINE3(accept, int, fd, struct sockaddr __user *, upeer_sockaddr,
1666		int __user *, upeer_addrlen)
1667{
1668	return sys_accept4(fd, upeer_sockaddr, upeer_addrlen, 0);
1669}
1670
1671/*
1672 *	Attempt to connect to a socket with the server address.  The address
1673 *	is in user space so we verify it is OK and move it to kernel space.
1674 *
1675 *	For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
1676 *	break bindings
1677 *
1678 *	NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
1679 *	other SEQPACKET protocols that take time to connect() as it doesn't
1680 *	include the -EINPROGRESS status for such sockets.
1681 */
1682
1683SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
1684		int, addrlen)
1685{
1686	struct socket *sock;
1687	struct sockaddr_storage address;
1688	int err, fput_needed;
1689
1690	sock = sockfd_lookup_light(fd, &err, &fput_needed);
1691	if (!sock)
1692		goto out;
1693	err = move_addr_to_kernel(uservaddr, addrlen, &address);
1694	if (err < 0)
1695		goto out_put;
1696
1697	err =
1698	    security_socket_connect(sock, (struct sockaddr *)&address, addrlen);
1699	if (err)
1700		goto out_put;
1701
1702	err = sock->ops->connect(sock, (struct sockaddr *)&address, addrlen,
1703				 sock->file->f_flags);
1704out_put:
1705	fput_light(sock->file, fput_needed);
1706out:
1707	return err;
1708}
1709
1710/*
1711 *	Get the local address ('name') of a socket object. Move the obtained
1712 *	name to user space.
1713 */
1714
1715SYSCALL_DEFINE3(getsockname, int, fd, struct sockaddr __user *, usockaddr,
1716		int __user *, usockaddr_len)
1717{
1718	struct socket *sock;
1719	struct sockaddr_storage address;
1720	int len, err, fput_needed;
1721
1722	sock = sockfd_lookup_light(fd, &err, &fput_needed);
1723	if (!sock)
1724		goto out;
1725
1726	err = security_socket_getsockname(sock);
1727	if (err)
1728		goto out_put;
1729
1730	err = sock->ops->getname(sock, (struct sockaddr *)&address, &len, 0);
1731	if (err)
1732		goto out_put;
1733	err = move_addr_to_user(&address, len, usockaddr, usockaddr_len);
1734
1735out_put:
1736	fput_light(sock->file, fput_needed);
1737out:
1738	return err;
1739}
1740
1741/*
1742 *	Get the remote address ('name') of a socket object. Move the obtained
1743 *	name to user space.
1744 */
1745
1746SYSCALL_DEFINE3(getpeername, int, fd, struct sockaddr __user *, usockaddr,
1747		int __user *, usockaddr_len)
1748{
1749	struct socket *sock;
1750	struct sockaddr_storage address;
1751	int len, err, fput_needed;
1752
1753	sock = sockfd_lookup_light(fd, &err, &fput_needed);
1754	if (sock != NULL) {
1755		err = security_socket_getpeername(sock);
1756		if (err) {
1757			fput_light(sock->file, fput_needed);
1758			return err;
1759		}
1760
1761		err =
1762		    sock->ops->getname(sock, (struct sockaddr *)&address, &len,
1763				       1);
1764		if (!err)
1765			err = move_addr_to_user(&address, len, usockaddr,
1766						usockaddr_len);
1767		fput_light(sock->file, fput_needed);
1768	}
1769	return err;
1770}
1771
1772/*
1773 *	Send a datagram to a given address. We move the address into kernel
1774 *	space and check the user space data area is readable before invoking
1775 *	the protocol.
1776 */
1777
1778SYSCALL_DEFINE6(sendto, int, fd, void __user *, buff, size_t, len,
1779		unsigned int, flags, struct sockaddr __user *, addr,
1780		int, addr_len)
1781{
1782	struct socket *sock;
1783	struct sockaddr_storage address;
1784	int err;
1785	struct msghdr msg;
1786	struct iovec iov;
1787	int fput_needed;
1788
1789	if (len > INT_MAX)
1790		len = INT_MAX;
1791	sock = sockfd_lookup_light(fd, &err, &fput_needed);
1792	if (!sock)
1793		goto out;
1794
1795	iov.iov_base = buff;
1796	iov.iov_len = len;
1797	msg.msg_name = NULL;
1798	msg.msg_iov = &iov;
1799	msg.msg_iovlen = 1;
1800	msg.msg_control = NULL;
1801	msg.msg_controllen = 0;
1802	msg.msg_namelen = 0;
1803	if (addr) {
1804		err = move_addr_to_kernel(addr, addr_len, &address);
1805		if (err < 0)
1806			goto out_put;
1807		msg.msg_name = (struct sockaddr *)&address;
1808		msg.msg_namelen = addr_len;
1809	}
1810	if (sock->file->f_flags & O_NONBLOCK)
1811		flags |= MSG_DONTWAIT;
1812	msg.msg_flags = flags;
1813	err = sock_sendmsg(sock, &msg, len);
1814
1815out_put:
1816	fput_light(sock->file, fput_needed);
1817out:
1818	return err;
1819}
1820
1821/*
1822 *	Send a datagram down a socket.
1823 */
1824
1825SYSCALL_DEFINE4(send, int, fd, void __user *, buff, size_t, len,
1826		unsigned int, flags)
1827{
1828	return sys_sendto(fd, buff, len, flags, NULL, 0);
1829}
1830
1831/*
1832 *	Receive a frame from the socket and optionally record the address of the
1833 *	sender. We verify the buffers are writable and if needed move the
1834 *	sender address from kernel to user space.
1835 */
1836
1837SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, ubuf, size_t, size,
1838		unsigned int, flags, struct sockaddr __user *, addr,
1839		int __user *, addr_len)
1840{
1841	struct socket *sock;
1842	struct iovec iov;
1843	struct msghdr msg;
1844	struct sockaddr_storage address;
1845	int err, err2;
1846	int fput_needed;
1847
1848	if (size > INT_MAX)
1849		size = INT_MAX;
1850	sock = sockfd_lookup_light(fd, &err, &fput_needed);
1851	if (!sock)
1852		goto out;
1853
1854	msg.msg_control = NULL;
1855	msg.msg_controllen = 0;
1856	msg.msg_iovlen = 1;
1857	msg.msg_iov = &iov;
1858	iov.iov_len = size;
1859	iov.iov_base = ubuf;
1860	/* Save some cycles and don't copy the address if not needed */
1861	msg.msg_name = addr ? (struct sockaddr *)&address : NULL;
1862	/* We assume all kernel code knows the size of sockaddr_storage */
1863	msg.msg_namelen = 0;
1864	if (sock->file->f_flags & O_NONBLOCK)
1865		flags |= MSG_DONTWAIT;
1866	err = sock_recvmsg(sock, &msg, size, flags);
1867
1868	if (err >= 0 && addr != NULL) {
1869		err2 = move_addr_to_user(&address,
1870					 msg.msg_namelen, addr, addr_len);
1871		if (err2 < 0)
1872			err = err2;
1873	}
1874
1875	fput_light(sock->file, fput_needed);
1876out:
1877	return err;
1878}
1879
1880/*
1881 *	Receive a datagram from a socket.
1882 */
1883
1884asmlinkage long sys_recv(int fd, void __user *ubuf, size_t size,
1885			 unsigned int flags)
1886{
1887	return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
1888}
1889
1890/*
1891 *	Set a socket option. Because we don't know the option lengths we have
1892 *	to pass the user mode parameter for the protocols to sort out.
1893 */
1894
1895SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname,
1896		char __user *, optval, int, optlen)
1897{
1898	int err, fput_needed;
1899	struct socket *sock;
1900
1901	if (optlen < 0)
1902		return -EINVAL;
1903
1904	sock = sockfd_lookup_light(fd, &err, &fput_needed);
1905	if (sock != NULL) {
1906		err = security_socket_setsockopt(sock, level, optname);
1907		if (err)
1908			goto out_put;
1909
1910		if (level == SOL_SOCKET)
1911			err =
1912			    sock_setsockopt(sock, level, optname, optval,
1913					    optlen);
1914		else
1915			err =
1916			    sock->ops->setsockopt(sock, level, optname, optval,
1917						  optlen);
1918out_put:
1919		fput_light(sock->file, fput_needed);
1920	}
1921	return err;
1922}
1923
1924/*
1925 *	Get a socket option. Because we don't know the option lengths we have
1926 *	to pass a user mode parameter for the protocols to sort out.
1927 */
1928
1929SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname,
1930		char __user *, optval, int __user *, optlen)
1931{
1932	int err, fput_needed;
1933	struct socket *sock;
1934
1935	sock = sockfd_lookup_light(fd, &err, &fput_needed);
1936	if (sock != NULL) {
1937		err = security_socket_getsockopt(sock, level, optname);
1938		if (err)
1939			goto out_put;
1940
1941		if (level == SOL_SOCKET)
1942			err =
1943			    sock_getsockopt(sock, level, optname, optval,
1944					    optlen);
1945		else
1946			err =
1947			    sock->ops->getsockopt(sock, level, optname, optval,
1948						  optlen);
1949out_put:
1950		fput_light(sock->file, fput_needed);
1951	}
1952	return err;
1953}
1954
1955/*
1956 *	Shutdown a socket.
1957 */
1958
1959SYSCALL_DEFINE2(shutdown, int, fd, int, how)
1960{
1961	int err, fput_needed;
1962	struct socket *sock;
1963
1964	sock = sockfd_lookup_light(fd, &err, &fput_needed);
1965	if (sock != NULL) {
1966		err = security_socket_shutdown(sock, how);
1967		if (!err)
1968			err = sock->ops->shutdown(sock, how);
1969		fput_light(sock->file, fput_needed);
1970	}
1971	return err;
1972}
1973
1974/* A couple of helpful macros for getting the address of the 32/64 bit
1975 * fields which are the same type (int / unsigned) on our platforms.
1976 */
1977#define COMPAT_MSG(msg, member)	((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
1978#define COMPAT_NAMELEN(msg)	COMPAT_MSG(msg, msg_namelen)
1979#define COMPAT_FLAGS(msg)	COMPAT_MSG(msg, msg_flags)
1980
1981struct used_address {
1982	struct sockaddr_storage name;
1983	unsigned int name_len;
1984};
1985
1986static int copy_msghdr_from_user(struct msghdr *kmsg,
1987				 struct msghdr __user *umsg)
1988{
1989	if (copy_from_user(kmsg, umsg, sizeof(struct msghdr)))
1990		return -EFAULT;
1991
1992	if (kmsg->msg_namelen < 0)
1993		return -EINVAL;
1994
1995	if (kmsg->msg_namelen > sizeof(struct sockaddr_storage))
1996		kmsg->msg_namelen = sizeof(struct sockaddr_storage);
1997	return 0;
1998}
1999
2000static int ___sys_sendmsg(struct socket *sock, struct msghdr __user *msg,
2001			 struct msghdr *msg_sys, unsigned int flags,
2002			 struct used_address *used_address)
2003{
2004	struct compat_msghdr __user *msg_compat =
2005	    (struct compat_msghdr __user *)msg;
2006	struct sockaddr_storage address;
2007	struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
2008	unsigned char ctl[sizeof(struct cmsghdr) + 20]
2009	    __attribute__ ((aligned(sizeof(__kernel_size_t))));
2010	/* 20 is size of ipv6_pktinfo */
2011	unsigned char *ctl_buf = ctl;
2012	int err, ctl_len, total_len;
2013
2014	err = -EFAULT;
2015	if (MSG_CMSG_COMPAT & flags) {
2016		if (get_compat_msghdr(msg_sys, msg_compat))
2017			return -EFAULT;
2018	} else {
2019		err = copy_msghdr_from_user(msg_sys, msg);
2020		if (err)
2021			return err;
2022	}
2023
2024	if (msg_sys->msg_iovlen > UIO_FASTIOV) {
2025		err = -EMSGSIZ

Large files files are truncated, but you can click here to view the full file