PageRenderTime 136ms CodeModel.GetById 16ms app.highlight 105ms RepoModel.GetById 1ms app.codeStats 1ms

/net/socket.c

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

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