PageRenderTime 123ms CodeModel.GetById 4ms app.highlight 104ms RepoModel.GetById 1ms app.codeStats 0ms

/net/socket.c

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

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