PageRenderTime 109ms CodeModel.GetById 4ms app.highlight 89ms RepoModel.GetById 1ms app.codeStats 1ms

/net/socket.c

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

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