PageRenderTime 6ms CodeModel.GetById 8ms app.highlight 144ms RepoModel.GetById 1ms app.codeStats 1ms

/net/socket.c

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

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