PageRenderTime 205ms CodeModel.GetById 4ms app.highlight 179ms RepoModel.GetById 1ms app.codeStats 1ms

/net/socket.c

https://bitbucket.org/digetx/picasso-kernel
C | 3505 lines | 2541 code | 501 blank | 463 comment | 401 complexity | 8d5656caf9ddf051858bf9f47c7d6911 MD5 | raw file
   1/*
   2 * NET		An implementation of the SOCKET network access protocol.
   3 *
   4 * Version:	@(#)socket.c	1.1.93	18/02/95
   5 *
   6 * Authors:	Orest Zborowski, <obz@Kodak.COM>
   7 *		Ross Biro
   8 *		Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
   9 *
  10 * Fixes:
  11 *		Anonymous	:	NOTSOCK/BADF cleanup. Error fix in
  12 *					shutdown()
  13 *		Alan Cox	:	verify_area() fixes
  14 *		Alan Cox	:	Removed DDI
  15 *		Jonathan Kamens	:	SOCK_DGRAM reconnect bug
  16 *		Alan Cox	:	Moved a load of checks to the very
  17 *					top level.
  18 *		Alan Cox	:	Move address structures to/from user
  19 *					mode above the protocol layers.
  20 *		Rob Janssen	:	Allow 0 length sends.
  21 *		Alan Cox	:	Asynchronous I/O support (cribbed from the
  22 *					tty drivers).
  23 *		Niibe Yutaka	:	Asynchronous I/O for writes (4.4BSD style)
  24 *		Jeff Uphoff	:	Made max number of sockets command-line
  25 *					configurable.
  26 *		Matti Aarnio	:	Made the number of sockets dynamic,
  27 *					to be allocated when needed, and mr.
  28 *					Uphoff's max is used as max to be
  29 *					allowed to allocate.
  30 *		Linus		:	Argh. removed all the socket allocation
  31 *					altogether: it's in the inode now.
  32 *		Alan Cox	:	Made sock_alloc()/sock_release() public
  33 *					for NetROM and future kernel nfsd type
  34 *					stuff.
  35 *		Alan Cox	:	sendmsg/recvmsg basics.
  36 *		Tom Dyas	:	Export net symbols.
  37 *		Marcin Dalecki	:	Fixed problems with CONFIG_NET="n".
  38 *		Alan Cox	:	Added thread locking to sys_* calls
  39 *					for sockets. May have errors at the
  40 *					moment.
  41 *		Kevin Buhr	:	Fixed the dumb errors in the above.
  42 *		Andi Kleen	:	Some small cleanups, optimizations,
  43 *					and fixed a copy_from_user() bug.
  44 *		Tigran Aivazian	:	sys_send(args) calls sys_sendto(args, NULL, 0)
  45 *		Tigran Aivazian	:	Made listen(2) backlog sanity checks
  46 *					protocol-independent
  47 *
  48 *
  49 *		This program is free software; you can redistribute it and/or
  50 *		modify it under the terms of the GNU General Public License
  51 *		as published by the Free Software Foundation; either version
  52 *		2 of the License, or (at your option) any later version.
  53 *
  54 *
  55 *	This module is effectively the top level interface to the BSD socket
  56 *	paradigm.
  57 *
  58 *	Based upon Swansea University Computer Society NET3.039
  59 */
  60
  61#include <linux/mm.h>
  62#include <linux/socket.h>
  63#include <linux/file.h>
  64#include <linux/net.h>
  65#include <linux/interrupt.h>
  66#include <linux/thread_info.h>
  67#include <linux/rcupdate.h>
  68#include <linux/netdevice.h>
  69#include <linux/proc_fs.h>
  70#include <linux/seq_file.h>
  71#include <linux/mutex.h>
  72#include <linux/wanrouter.h>
  73#include <linux/if_bridge.h>
  74#include <linux/if_frad.h>
  75#include <linux/if_vlan.h>
  76#include <linux/init.h>
  77#include <linux/poll.h>
  78#include <linux/cache.h>
  79#include <linux/module.h>
  80#include <linux/highmem.h>
  81#include <linux/mount.h>
  82#include <linux/security.h>
  83#include <linux/syscalls.h>
  84#include <linux/compat.h>
  85#include <linux/kmod.h>
  86#include <linux/audit.h>
  87#include <linux/wireless.h>
  88#include <linux/nsproxy.h>
  89#include <linux/magic.h>
  90#include <linux/slab.h>
  91#include <linux/xattr.h>
  92
  93#include <asm/uaccess.h>
  94#include <asm/unistd.h>
  95
  96#include <net/compat.h>
  97#include <net/wext.h>
  98#include <net/cls_cgroup.h>
  99
 100#include <net/sock.h>
 101#include <linux/netfilter.h>
 102
 103#include <linux/if_tun.h>
 104#include <linux/ipv6_route.h>
 105#include <linux/route.h>
 106#include <linux/sockios.h>
 107#include <linux/atalk.h>
 108
 109static int sock_no_open(struct inode *irrelevant, struct file *dontcare);
 110static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov,
 111			 unsigned long nr_segs, loff_t pos);
 112static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov,
 113			  unsigned long nr_segs, loff_t pos);
 114static int sock_mmap(struct file *file, struct vm_area_struct *vma);
 115
 116static int sock_close(struct inode *inode, struct file *file);
 117static unsigned int sock_poll(struct file *file,
 118			      struct poll_table_struct *wait);
 119static long sock_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
 120#ifdef CONFIG_COMPAT
 121static long compat_sock_ioctl(struct file *file,
 122			      unsigned int cmd, unsigned long arg);
 123#endif
 124static int sock_fasync(int fd, struct file *filp, int on);
 125static ssize_t sock_sendpage(struct file *file, struct page *page,
 126			     int offset, size_t size, loff_t *ppos, int more);
 127static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
 128				struct pipe_inode_info *pipe, size_t len,
 129				unsigned int flags);
 130
 131/*
 132 *	Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
 133 *	in the operation structures but are done directly via the socketcall() multiplexor.
 134 */
 135
 136static const struct file_operations socket_file_ops = {
 137	.owner =	THIS_MODULE,
 138	.llseek =	no_llseek,
 139	.aio_read =	sock_aio_read,
 140	.aio_write =	sock_aio_write,
 141	.poll =		sock_poll,
 142	.unlocked_ioctl = sock_ioctl,
 143#ifdef CONFIG_COMPAT
 144	.compat_ioctl = compat_sock_ioctl,
 145#endif
 146	.mmap =		sock_mmap,
 147	.open =		sock_no_open,	/* special open code to disallow open via /proc */
 148	.release =	sock_close,
 149	.fasync =	sock_fasync,
 150	.sendpage =	sock_sendpage,
 151	.splice_write = generic_splice_sendpage,
 152	.splice_read =	sock_splice_read,
 153};
 154
 155/*
 156 *	The protocol list. Each protocol is registered in here.
 157 */
 158
 159static DEFINE_SPINLOCK(net_family_lock);
 160static const struct net_proto_family __rcu *net_families[NPROTO] __read_mostly;
 161
 162/*
 163 *	Statistics counters of the socket lists
 164 */
 165
 166static DEFINE_PER_CPU(int, sockets_in_use);
 167
 168/*
 169 * Support routines.
 170 * Move socket addresses back and forth across the kernel/user
 171 * divide and look after the messy bits.
 172 */
 173
 174/**
 175 *	move_addr_to_kernel	-	copy a socket address into kernel space
 176 *	@uaddr: Address in user space
 177 *	@kaddr: Address in kernel space
 178 *	@ulen: Length in user space
 179 *
 180 *	The address is copied into kernel space. If the provided address is
 181 *	too long an error code of -EINVAL is returned. If the copy gives
 182 *	invalid addresses -EFAULT is returned. On a success 0 is returned.
 183 */
 184
 185int move_addr_to_kernel(void __user *uaddr, int ulen, struct sockaddr_storage *kaddr)
 186{
 187	if (ulen < 0 || ulen > sizeof(struct sockaddr_storage))
 188		return -EINVAL;
 189	if (ulen == 0)
 190		return 0;
 191	if (copy_from_user(kaddr, uaddr, ulen))
 192		return -EFAULT;
 193	return audit_sockaddr(ulen, kaddr);
 194}
 195
 196/**
 197 *	move_addr_to_user	-	copy an address to user space
 198 *	@kaddr: kernel space address
 199 *	@klen: length of address in kernel
 200 *	@uaddr: user space address
 201 *	@ulen: pointer to user length field
 202 *
 203 *	The value pointed to by ulen on entry is the buffer length available.
 204 *	This is overwritten with the buffer space used. -EINVAL is returned
 205 *	if an overlong buffer is specified or a negative buffer size. -EFAULT
 206 *	is returned if either the buffer or the length field are not
 207 *	accessible.
 208 *	After copying the data up to the limit the user specifies, the true
 209 *	length of the data is written over the length limit the user
 210 *	specified. Zero is returned for a success.
 211 */
 212
 213static int move_addr_to_user(struct sockaddr_storage *kaddr, int klen,
 214			     void __user *uaddr, int __user *ulen)
 215{
 216	int err;
 217	int len;
 218
 219	BUG_ON(klen > sizeof(struct sockaddr_storage));
 220	err = get_user(len, ulen);
 221	if (err)
 222		return err;
 223	if (len > klen)
 224		len = klen;
 225	if (len < 0)
 226		return -EINVAL;
 227	if (len) {
 228		if (audit_sockaddr(klen, kaddr))
 229			return -ENOMEM;
 230		if (copy_to_user(uaddr, kaddr, len))
 231			return -EFAULT;
 232	}
 233	/*
 234	 *      "fromlen shall refer to the value before truncation.."
 235	 *                      1003.1g
 236	 */
 237	return __put_user(klen, ulen);
 238}
 239
 240static struct kmem_cache *sock_inode_cachep __read_mostly;
 241
 242static struct inode *sock_alloc_inode(struct super_block *sb)
 243{
 244	struct socket_alloc *ei;
 245	struct socket_wq *wq;
 246
 247	ei = kmem_cache_alloc(sock_inode_cachep, GFP_KERNEL);
 248	if (!ei)
 249		return NULL;
 250	wq = kmalloc(sizeof(*wq), GFP_KERNEL);
 251	if (!wq) {
 252		kmem_cache_free(sock_inode_cachep, ei);
 253		return NULL;
 254	}
 255	init_waitqueue_head(&wq->wait);
 256	wq->fasync_list = NULL;
 257	RCU_INIT_POINTER(ei->socket.wq, wq);
 258
 259	ei->socket.state = SS_UNCONNECTED;
 260	ei->socket.flags = 0;
 261	ei->socket.ops = NULL;
 262	ei->socket.sk = NULL;
 263	ei->socket.file = NULL;
 264
 265	return &ei->vfs_inode;
 266}
 267
 268static void sock_destroy_inode(struct inode *inode)
 269{
 270	struct socket_alloc *ei;
 271	struct socket_wq *wq;
 272
 273	ei = container_of(inode, struct socket_alloc, vfs_inode);
 274	wq = rcu_dereference_protected(ei->socket.wq, 1);
 275	kfree_rcu(wq, rcu);
 276	kmem_cache_free(sock_inode_cachep, ei);
 277}
 278
 279static void init_once(void *foo)
 280{
 281	struct socket_alloc *ei = (struct socket_alloc *)foo;
 282
 283	inode_init_once(&ei->vfs_inode);
 284}
 285
 286static int init_inodecache(void)
 287{
 288	sock_inode_cachep = kmem_cache_create("sock_inode_cache",
 289					      sizeof(struct socket_alloc),
 290					      0,
 291					      (SLAB_HWCACHE_ALIGN |
 292					       SLAB_RECLAIM_ACCOUNT |
 293					       SLAB_MEM_SPREAD),
 294					      init_once);
 295	if (sock_inode_cachep == NULL)
 296		return -ENOMEM;
 297	return 0;
 298}
 299
 300static const struct super_operations sockfs_ops = {
 301	.alloc_inode	= sock_alloc_inode,
 302	.destroy_inode	= sock_destroy_inode,
 303	.statfs		= simple_statfs,
 304};
 305
 306/*
 307 * sockfs_dname() is called from d_path().
 308 */
 309static char *sockfs_dname(struct dentry *dentry, char *buffer, int buflen)
 310{
 311	return dynamic_dname(dentry, buffer, buflen, "socket:[%lu]",
 312				dentry->d_inode->i_ino);
 313}
 314
 315static const struct dentry_operations sockfs_dentry_operations = {
 316	.d_dname  = sockfs_dname,
 317};
 318
 319static struct dentry *sockfs_mount(struct file_system_type *fs_type,
 320			 int flags, const char *dev_name, void *data)
 321{
 322	return mount_pseudo(fs_type, "socket:", &sockfs_ops,
 323		&sockfs_dentry_operations, SOCKFS_MAGIC);
 324}
 325
 326static struct vfsmount *sock_mnt __read_mostly;
 327
 328static struct file_system_type sock_fs_type = {
 329	.name =		"sockfs",
 330	.mount =	sockfs_mount,
 331	.kill_sb =	kill_anon_super,
 332};
 333
 334/*
 335 *	Obtains the first available file descriptor and sets it up for use.
 336 *
 337 *	These functions create file structures and maps them to fd space
 338 *	of the current process. On success it returns file descriptor
 339 *	and file struct implicitly stored in sock->file.
 340 *	Note that another thread may close file descriptor before we return
 341 *	from this function. We use the fact that now we do not refer
 342 *	to socket after mapping. If one day we will need it, this
 343 *	function will increment ref. count on file by 1.
 344 *
 345 *	In any case returned fd MAY BE not valid!
 346 *	This race condition is unavoidable
 347 *	with shared fd spaces, we cannot solve it inside kernel,
 348 *	but we take care of internal coherence yet.
 349 */
 350
 351struct file *sock_alloc_file(struct socket *sock, int flags, const char *dname)
 352{
 353	struct qstr name = { .name = "" };
 354	struct path path;
 355	struct file *file;
 356
 357	if (dname) {
 358		name.name = dname;
 359		name.len = strlen(name.name);
 360	} else if (sock->sk) {
 361		name.name = sock->sk->sk_prot_creator->name;
 362		name.len = strlen(name.name);
 363	}
 364	path.dentry = d_alloc_pseudo(sock_mnt->mnt_sb, &name);
 365	if (unlikely(!path.dentry))
 366		return ERR_PTR(-ENOMEM);
 367	path.mnt = mntget(sock_mnt);
 368
 369	d_instantiate(path.dentry, SOCK_INODE(sock));
 370	SOCK_INODE(sock)->i_fop = &socket_file_ops;
 371
 372	file = alloc_file(&path, FMODE_READ | FMODE_WRITE,
 373		  &socket_file_ops);
 374	if (unlikely(!file)) {
 375		/* drop dentry, keep inode */
 376		ihold(path.dentry->d_inode);
 377		path_put(&path);
 378		return ERR_PTR(-ENFILE);
 379	}
 380
 381	sock->file = file;
 382	file->f_flags = O_RDWR | (flags & O_NONBLOCK);
 383	file->f_pos = 0;
 384	file->private_data = sock;
 385	return file;
 386}
 387EXPORT_SYMBOL(sock_alloc_file);
 388
 389static int sock_map_fd(struct socket *sock, int flags)
 390{
 391	struct file *newfile;
 392	int fd = get_unused_fd_flags(flags);
 393	if (unlikely(fd < 0))
 394		return fd;
 395
 396	newfile = sock_alloc_file(sock, flags, NULL);
 397	if (likely(!IS_ERR(newfile))) {
 398		fd_install(fd, newfile);
 399		return fd;
 400	}
 401
 402	put_unused_fd(fd);
 403	return PTR_ERR(newfile);
 404}
 405
 406struct socket *sock_from_file(struct file *file, int *err)
 407{
 408	if (file->f_op == &socket_file_ops)
 409		return file->private_data;	/* set in sock_map_fd */
 410
 411	*err = -ENOTSOCK;
 412	return NULL;
 413}
 414EXPORT_SYMBOL(sock_from_file);
 415
 416/**
 417 *	sockfd_lookup - Go from a file number to its socket slot
 418 *	@fd: file handle
 419 *	@err: pointer to an error code return
 420 *
 421 *	The file handle passed in is locked and the socket it is bound
 422 *	too is returned. If an error occurs the err pointer is overwritten
 423 *	with a negative errno code and NULL is returned. The function checks
 424 *	for both invalid handles and passing a handle which is not a socket.
 425 *
 426 *	On a success the socket object pointer is returned.
 427 */
 428
 429struct socket *sockfd_lookup(int fd, int *err)
 430{
 431	struct file *file;
 432	struct socket *sock;
 433
 434	file = fget(fd);
 435	if (!file) {
 436		*err = -EBADF;
 437		return NULL;
 438	}
 439
 440	sock = sock_from_file(file, err);
 441	if (!sock)
 442		fput(file);
 443	return sock;
 444}
 445EXPORT_SYMBOL(sockfd_lookup);
 446
 447static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed)
 448{
 449	struct file *file;
 450	struct socket *sock;
 451
 452	*err = -EBADF;
 453	file = fget_light(fd, fput_needed);
 454	if (file) {
 455		sock = sock_from_file(file, err);
 456		if (sock)
 457			return sock;
 458		fput_light(file, *fput_needed);
 459	}
 460	return NULL;
 461}
 462
 463#define XATTR_SOCKPROTONAME_SUFFIX "sockprotoname"
 464#define XATTR_NAME_SOCKPROTONAME (XATTR_SYSTEM_PREFIX XATTR_SOCKPROTONAME_SUFFIX)
 465#define XATTR_NAME_SOCKPROTONAME_LEN (sizeof(XATTR_NAME_SOCKPROTONAME)-1)
 466static ssize_t sockfs_getxattr(struct dentry *dentry,
 467			       const char *name, void *value, size_t size)
 468{
 469	const char *proto_name;
 470	size_t proto_size;
 471	int error;
 472
 473	error = -ENODATA;
 474	if (!strncmp(name, XATTR_NAME_SOCKPROTONAME, XATTR_NAME_SOCKPROTONAME_LEN)) {
 475		proto_name = dentry->d_name.name;
 476		proto_size = strlen(proto_name);
 477
 478		if (value) {
 479			error = -ERANGE;
 480			if (proto_size + 1 > size)
 481				goto out;
 482
 483			strncpy(value, proto_name, proto_size + 1);
 484		}
 485		error = proto_size + 1;
 486	}
 487
 488out:
 489	return error;
 490}
 491
 492static ssize_t sockfs_listxattr(struct dentry *dentry, char *buffer,
 493				size_t size)
 494{
 495	ssize_t len;
 496	ssize_t used = 0;
 497
 498	len = security_inode_listsecurity(dentry->d_inode, buffer, size);
 499	if (len < 0)
 500		return len;
 501	used += len;
 502	if (buffer) {
 503		if (size < used)
 504			return -ERANGE;
 505		buffer += len;
 506	}
 507
 508	len = (XATTR_NAME_SOCKPROTONAME_LEN + 1);
 509	used += len;
 510	if (buffer) {
 511		if (size < used)
 512			return -ERANGE;
 513		memcpy(buffer, XATTR_NAME_SOCKPROTONAME, len);
 514		buffer += len;
 515	}
 516
 517	return used;
 518}
 519
 520static const struct inode_operations sockfs_inode_ops = {
 521	.getxattr = sockfs_getxattr,
 522	.listxattr = sockfs_listxattr,
 523};
 524
 525/**
 526 *	sock_alloc	-	allocate a socket
 527 *
 528 *	Allocate a new inode and socket object. The two are bound together
 529 *	and initialised. The socket is then returned. If we are out of inodes
 530 *	NULL is returned.
 531 */
 532
 533static struct socket *sock_alloc(void)
 534{
 535	struct inode *inode;
 536	struct socket *sock;
 537
 538	inode = new_inode_pseudo(sock_mnt->mnt_sb);
 539	if (!inode)
 540		return NULL;
 541
 542	sock = SOCKET_I(inode);
 543
 544	kmemcheck_annotate_bitfield(sock, type);
 545	inode->i_ino = get_next_ino();
 546	inode->i_mode = S_IFSOCK | S_IRWXUGO;
 547	inode->i_uid = current_fsuid();
 548	inode->i_gid = current_fsgid();
 549	inode->i_op = &sockfs_inode_ops;
 550
 551	this_cpu_add(sockets_in_use, 1);
 552	return sock;
 553}
 554
 555/*
 556 *	In theory you can't get an open on this inode, but /proc provides
 557 *	a back door. Remember to keep it shut otherwise you'll let the
 558 *	creepy crawlies in.
 559 */
 560
 561static int sock_no_open(struct inode *irrelevant, struct file *dontcare)
 562{
 563	return -ENXIO;
 564}
 565
 566const struct file_operations bad_sock_fops = {
 567	.owner = THIS_MODULE,
 568	.open = sock_no_open,
 569	.llseek = noop_llseek,
 570};
 571
 572/**
 573 *	sock_release	-	close a socket
 574 *	@sock: socket to close
 575 *
 576 *	The socket is released from the protocol stack if it has a release
 577 *	callback, and the inode is then released if the socket is bound to
 578 *	an inode not a file.
 579 */
 580
 581void sock_release(struct socket *sock)
 582{
 583	if (sock->ops) {
 584		struct module *owner = sock->ops->owner;
 585
 586		sock->ops->release(sock);
 587		sock->ops = NULL;
 588		module_put(owner);
 589	}
 590
 591	if (rcu_dereference_protected(sock->wq, 1)->fasync_list)
 592		printk(KERN_ERR "sock_release: fasync list not empty!\n");
 593
 594	if (test_bit(SOCK_EXTERNALLY_ALLOCATED, &sock->flags))
 595		return;
 596
 597	this_cpu_sub(sockets_in_use, 1);
 598	if (!sock->file) {
 599		iput(SOCK_INODE(sock));
 600		return;
 601	}
 602	sock->file = NULL;
 603}
 604EXPORT_SYMBOL(sock_release);
 605
 606int sock_tx_timestamp(struct sock *sk, __u8 *tx_flags)
 607{
 608	*tx_flags = 0;
 609	if (sock_flag(sk, SOCK_TIMESTAMPING_TX_HARDWARE))
 610		*tx_flags |= SKBTX_HW_TSTAMP;
 611	if (sock_flag(sk, SOCK_TIMESTAMPING_TX_SOFTWARE))
 612		*tx_flags |= SKBTX_SW_TSTAMP;
 613	if (sock_flag(sk, SOCK_WIFI_STATUS))
 614		*tx_flags |= SKBTX_WIFI_STATUS;
 615	return 0;
 616}
 617EXPORT_SYMBOL(sock_tx_timestamp);
 618
 619static inline int __sock_sendmsg_nosec(struct kiocb *iocb, struct socket *sock,
 620				       struct msghdr *msg, size_t size)
 621{
 622	struct sock_iocb *si = kiocb_to_siocb(iocb);
 623
 624	si->sock = sock;
 625	si->scm = NULL;
 626	si->msg = msg;
 627	si->size = size;
 628
 629	return sock->ops->sendmsg(iocb, sock, msg, size);
 630}
 631
 632static inline int __sock_sendmsg(struct kiocb *iocb, struct socket *sock,
 633				 struct msghdr *msg, size_t size)
 634{
 635	int err = security_socket_sendmsg(sock, msg, size);
 636
 637	return err ?: __sock_sendmsg_nosec(iocb, sock, msg, size);
 638}
 639
 640int sock_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
 641{
 642	struct kiocb iocb;
 643	struct sock_iocb siocb;
 644	int ret;
 645
 646	init_sync_kiocb(&iocb, NULL);
 647	iocb.private = &siocb;
 648	ret = __sock_sendmsg(&iocb, sock, msg, size);
 649	if (-EIOCBQUEUED == ret)
 650		ret = wait_on_sync_kiocb(&iocb);
 651	return ret;
 652}
 653EXPORT_SYMBOL(sock_sendmsg);
 654
 655static int sock_sendmsg_nosec(struct socket *sock, struct msghdr *msg, size_t size)
 656{
 657	struct kiocb iocb;
 658	struct sock_iocb siocb;
 659	int ret;
 660
 661	init_sync_kiocb(&iocb, NULL);
 662	iocb.private = &siocb;
 663	ret = __sock_sendmsg_nosec(&iocb, sock, msg, size);
 664	if (-EIOCBQUEUED == ret)
 665		ret = wait_on_sync_kiocb(&iocb);
 666	return ret;
 667}
 668
 669int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
 670		   struct kvec *vec, size_t num, size_t size)
 671{
 672	mm_segment_t oldfs = get_fs();
 673	int result;
 674
 675	set_fs(KERNEL_DS);
 676	/*
 677	 * the following is safe, since for compiler definitions of kvec and
 678	 * iovec are identical, yielding the same in-core layout and alignment
 679	 */
 680	msg->msg_iov = (struct iovec *)vec;
 681	msg->msg_iovlen = num;
 682	result = sock_sendmsg(sock, msg, size);
 683	set_fs(oldfs);
 684	return result;
 685}
 686EXPORT_SYMBOL(kernel_sendmsg);
 687
 688static int ktime2ts(ktime_t kt, struct timespec *ts)
 689{
 690	if (kt.tv64) {
 691		*ts = ktime_to_timespec(kt);
 692		return 1;
 693	} else {
 694		return 0;
 695	}
 696}
 697
 698/*
 699 * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
 700 */
 701void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
 702	struct sk_buff *skb)
 703{
 704	int need_software_tstamp = sock_flag(sk, SOCK_RCVTSTAMP);
 705	struct timespec ts[3];
 706	int empty = 1;
 707	struct skb_shared_hwtstamps *shhwtstamps =
 708		skb_hwtstamps(skb);
 709
 710	/* Race occurred between timestamp enabling and packet
 711	   receiving.  Fill in the current time for now. */
 712	if (need_software_tstamp && skb->tstamp.tv64 == 0)
 713		__net_timestamp(skb);
 714
 715	if (need_software_tstamp) {
 716		if (!sock_flag(sk, SOCK_RCVTSTAMPNS)) {
 717			struct timeval tv;
 718			skb_get_timestamp(skb, &tv);
 719			put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMP,
 720				 sizeof(tv), &tv);
 721		} else {
 722			skb_get_timestampns(skb, &ts[0]);
 723			put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPNS,
 724				 sizeof(ts[0]), &ts[0]);
 725		}
 726	}
 727
 728
 729	memset(ts, 0, sizeof(ts));
 730	if (skb->tstamp.tv64 &&
 731	    sock_flag(sk, SOCK_TIMESTAMPING_SOFTWARE)) {
 732		skb_get_timestampns(skb, ts + 0);
 733		empty = 0;
 734	}
 735	if (shhwtstamps) {
 736		if (sock_flag(sk, SOCK_TIMESTAMPING_SYS_HARDWARE) &&
 737		    ktime2ts(shhwtstamps->syststamp, ts + 1))
 738			empty = 0;
 739		if (sock_flag(sk, SOCK_TIMESTAMPING_RAW_HARDWARE) &&
 740		    ktime2ts(shhwtstamps->hwtstamp, ts + 2))
 741			empty = 0;
 742	}
 743	if (!empty)
 744		put_cmsg(msg, SOL_SOCKET,
 745			 SCM_TIMESTAMPING, sizeof(ts), &ts);
 746}
 747EXPORT_SYMBOL_GPL(__sock_recv_timestamp);
 748
 749void __sock_recv_wifi_status(struct msghdr *msg, struct sock *sk,
 750	struct sk_buff *skb)
 751{
 752	int ack;
 753
 754	if (!sock_flag(sk, SOCK_WIFI_STATUS))
 755		return;
 756	if (!skb->wifi_acked_valid)
 757		return;
 758
 759	ack = skb->wifi_acked;
 760
 761	put_cmsg(msg, SOL_SOCKET, SCM_WIFI_STATUS, sizeof(ack), &ack);
 762}
 763EXPORT_SYMBOL_GPL(__sock_recv_wifi_status);
 764
 765static inline void sock_recv_drops(struct msghdr *msg, struct sock *sk,
 766				   struct sk_buff *skb)
 767{
 768	if (sock_flag(sk, SOCK_RXQ_OVFL) && skb && skb->dropcount)
 769		put_cmsg(msg, SOL_SOCKET, SO_RXQ_OVFL,
 770			sizeof(__u32), &skb->dropcount);
 771}
 772
 773void __sock_recv_ts_and_drops(struct msghdr *msg, struct sock *sk,
 774	struct sk_buff *skb)
 775{
 776	sock_recv_timestamp(msg, sk, skb);
 777	sock_recv_drops(msg, sk, skb);
 778}
 779EXPORT_SYMBOL_GPL(__sock_recv_ts_and_drops);
 780
 781static inline int __sock_recvmsg_nosec(struct kiocb *iocb, struct socket *sock,
 782				       struct msghdr *msg, size_t size, int flags)
 783{
 784	struct sock_iocb *si = kiocb_to_siocb(iocb);
 785
 786	si->sock = sock;
 787	si->scm = NULL;
 788	si->msg = msg;
 789	si->size = size;
 790	si->flags = flags;
 791
 792	return sock->ops->recvmsg(iocb, sock, msg, size, flags);
 793}
 794
 795static inline int __sock_recvmsg(struct kiocb *iocb, struct socket *sock,
 796				 struct msghdr *msg, size_t size, int flags)
 797{
 798	int err = security_socket_recvmsg(sock, msg, size, flags);
 799
 800	return err ?: __sock_recvmsg_nosec(iocb, sock, msg, size, flags);
 801}
 802
 803int sock_recvmsg(struct socket *sock, struct msghdr *msg,
 804		 size_t size, int flags)
 805{
 806	struct kiocb iocb;
 807	struct sock_iocb siocb;
 808	int ret;
 809
 810	init_sync_kiocb(&iocb, NULL);
 811	iocb.private = &siocb;
 812	ret = __sock_recvmsg(&iocb, sock, msg, size, flags);
 813	if (-EIOCBQUEUED == ret)
 814		ret = wait_on_sync_kiocb(&iocb);
 815	return ret;
 816}
 817EXPORT_SYMBOL(sock_recvmsg);
 818
 819static int sock_recvmsg_nosec(struct socket *sock, struct msghdr *msg,
 820			      size_t size, int flags)
 821{
 822	struct kiocb iocb;
 823	struct sock_iocb siocb;
 824	int ret;
 825
 826	init_sync_kiocb(&iocb, NULL);
 827	iocb.private = &siocb;
 828	ret = __sock_recvmsg_nosec(&iocb, sock, msg, size, flags);
 829	if (-EIOCBQUEUED == ret)
 830		ret = wait_on_sync_kiocb(&iocb);
 831	return ret;
 832}
 833
 834/**
 835 * kernel_recvmsg - Receive a message from a socket (kernel space)
 836 * @sock:       The socket to receive the message from
 837 * @msg:        Received message
 838 * @vec:        Input s/g array for message data
 839 * @num:        Size of input s/g array
 840 * @size:       Number of bytes to read
 841 * @flags:      Message flags (MSG_DONTWAIT, etc...)
 842 *
 843 * On return the msg structure contains the scatter/gather array passed in the
 844 * vec argument. The array is modified so that it consists of the unfilled
 845 * portion of the original array.
 846 *
 847 * The returned value is the total number of bytes received, or an error.
 848 */
 849int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
 850		   struct kvec *vec, size_t num, size_t size, int flags)
 851{
 852	mm_segment_t oldfs = get_fs();
 853	int result;
 854
 855	set_fs(KERNEL_DS);
 856	/*
 857	 * the following is safe, since for compiler definitions of kvec and
 858	 * iovec are identical, yielding the same in-core layout and alignment
 859	 */
 860	msg->msg_iov = (struct iovec *)vec, msg->msg_iovlen = num;
 861	result = sock_recvmsg(sock, msg, size, flags);
 862	set_fs(oldfs);
 863	return result;
 864}
 865EXPORT_SYMBOL(kernel_recvmsg);
 866
 867static void sock_aio_dtor(struct kiocb *iocb)
 868{
 869	kfree(iocb->private);
 870}
 871
 872static ssize_t sock_sendpage(struct file *file, struct page *page,
 873			     int offset, size_t size, loff_t *ppos, int more)
 874{
 875	struct socket *sock;
 876	int flags;
 877
 878	sock = file->private_data;
 879
 880	flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
 881	/* more is a combination of MSG_MORE and MSG_SENDPAGE_NOTLAST */
 882	flags |= more;
 883
 884	return kernel_sendpage(sock, page, offset, size, flags);
 885}
 886
 887static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
 888				struct pipe_inode_info *pipe, size_t len,
 889				unsigned int flags)
 890{
 891	struct socket *sock = file->private_data;
 892
 893	if (unlikely(!sock->ops->splice_read))
 894		return -EINVAL;
 895
 896	return sock->ops->splice_read(sock, ppos, pipe, len, flags);
 897}
 898
 899static struct sock_iocb *alloc_sock_iocb(struct kiocb *iocb,
 900					 struct sock_iocb *siocb)
 901{
 902	if (!is_sync_kiocb(iocb)) {
 903		siocb = kmalloc(sizeof(*siocb), GFP_KERNEL);
 904		if (!siocb)
 905			return NULL;
 906		iocb->ki_dtor = sock_aio_dtor;
 907	}
 908
 909	siocb->kiocb = iocb;
 910	iocb->private = siocb;
 911	return siocb;
 912}
 913
 914static ssize_t do_sock_read(struct msghdr *msg, struct kiocb *iocb,
 915		struct file *file, const struct iovec *iov,
 916		unsigned long nr_segs)
 917{
 918	struct socket *sock = file->private_data;
 919	size_t size = 0;
 920	int i;
 921
 922	for (i = 0; i < nr_segs; i++)
 923		size += iov[i].iov_len;
 924
 925	msg->msg_name = NULL;
 926	msg->msg_namelen = 0;
 927	msg->msg_control = NULL;
 928	msg->msg_controllen = 0;
 929	msg->msg_iov = (struct iovec *)iov;
 930	msg->msg_iovlen = nr_segs;
 931	msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
 932
 933	return __sock_recvmsg(iocb, sock, msg, size, msg->msg_flags);
 934}
 935
 936static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov,
 937				unsigned long nr_segs, loff_t pos)
 938{
 939	struct sock_iocb siocb, *x;
 940
 941	if (pos != 0)
 942		return -ESPIPE;
 943
 944	if (iocb->ki_left == 0)	/* Match SYS5 behaviour */
 945		return 0;
 946
 947
 948	x = alloc_sock_iocb(iocb, &siocb);
 949	if (!x)
 950		return -ENOMEM;
 951	return do_sock_read(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
 952}
 953
 954static ssize_t do_sock_write(struct msghdr *msg, struct kiocb *iocb,
 955			struct file *file, const struct iovec *iov,
 956			unsigned long nr_segs)
 957{
 958	struct socket *sock = file->private_data;
 959	size_t size = 0;
 960	int i;
 961
 962	for (i = 0; i < nr_segs; i++)
 963		size += iov[i].iov_len;
 964
 965	msg->msg_name = NULL;
 966	msg->msg_namelen = 0;
 967	msg->msg_control = NULL;
 968	msg->msg_controllen = 0;
 969	msg->msg_iov = (struct iovec *)iov;
 970	msg->msg_iovlen = nr_segs;
 971	msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
 972	if (sock->type == SOCK_SEQPACKET)
 973		msg->msg_flags |= MSG_EOR;
 974
 975	return __sock_sendmsg(iocb, sock, msg, size);
 976}
 977
 978static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov,
 979			  unsigned long nr_segs, loff_t pos)
 980{
 981	struct sock_iocb siocb, *x;
 982
 983	if (pos != 0)
 984		return -ESPIPE;
 985
 986	x = alloc_sock_iocb(iocb, &siocb);
 987	if (!x)
 988		return -ENOMEM;
 989
 990	return do_sock_write(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
 991}
 992
 993/*
 994 * Atomic setting of ioctl hooks to avoid race
 995 * with module unload.
 996 */
 997
 998static DEFINE_MUTEX(br_ioctl_mutex);
 999static int (*br_ioctl_hook) (struct net *, unsigned int cmd, void __user *arg);
1000
1001void brioctl_set(int (*hook) (struct net *, unsigned int, void __user *))
1002{
1003	mutex_lock(&br_ioctl_mutex);
1004	br_ioctl_hook = hook;
1005	mutex_unlock(&br_ioctl_mutex);
1006}
1007EXPORT_SYMBOL(brioctl_set);
1008
1009static DEFINE_MUTEX(vlan_ioctl_mutex);
1010static int (*vlan_ioctl_hook) (struct net *, void __user *arg);
1011
1012void vlan_ioctl_set(int (*hook) (struct net *, void __user *))
1013{
1014	mutex_lock(&vlan_ioctl_mutex);
1015	vlan_ioctl_hook = hook;
1016	mutex_unlock(&vlan_ioctl_mutex);
1017}
1018EXPORT_SYMBOL(vlan_ioctl_set);
1019
1020static DEFINE_MUTEX(dlci_ioctl_mutex);
1021static int (*dlci_ioctl_hook) (unsigned int, void __user *);
1022
1023void dlci_ioctl_set(int (*hook) (unsigned int, void __user *))
1024{
1025	mutex_lock(&dlci_ioctl_mutex);
1026	dlci_ioctl_hook = hook;
1027	mutex_unlock(&dlci_ioctl_mutex);
1028}
1029EXPORT_SYMBOL(dlci_ioctl_set);
1030
1031static long sock_do_ioctl(struct net *net, struct socket *sock,
1032				 unsigned int cmd, unsigned long arg)
1033{
1034	int err;
1035	void __user *argp = (void __user *)arg;
1036
1037	err = sock->ops->ioctl(sock, cmd, arg);
1038
1039	/*
1040	 * If this ioctl is unknown try to hand it down
1041	 * to the NIC driver.
1042	 */
1043	if (err == -ENOIOCTLCMD)
1044		err = dev_ioctl(net, cmd, argp);
1045
1046	return err;
1047}
1048
1049/*
1050 *	With an ioctl, arg may well be a user mode pointer, but we don't know
1051 *	what to do with it - that's up to the protocol still.
1052 */
1053
1054static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
1055{
1056	struct socket *sock;
1057	struct sock *sk;
1058	void __user *argp = (void __user *)arg;
1059	int pid, err;
1060	struct net *net;
1061
1062	sock = file->private_data;
1063	sk = sock->sk;
1064	net = sock_net(sk);
1065	if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
1066		err = dev_ioctl(net, cmd, argp);
1067	} else
1068#ifdef CONFIG_WEXT_CORE
1069	if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
1070		err = dev_ioctl(net, cmd, argp);
1071	} else
1072#endif
1073		switch (cmd) {
1074		case FIOSETOWN:
1075		case SIOCSPGRP:
1076			err = -EFAULT;
1077			if (get_user(pid, (int __user *)argp))
1078				break;
1079			err = f_setown(sock->file, pid, 1);
1080			break;
1081		case FIOGETOWN:
1082		case SIOCGPGRP:
1083			err = put_user(f_getown(sock->file),
1084				       (int __user *)argp);
1085			break;
1086		case SIOCGIFBR:
1087		case SIOCSIFBR:
1088		case SIOCBRADDBR:
1089		case SIOCBRDELBR:
1090			err = -ENOPKG;
1091			if (!br_ioctl_hook)
1092				request_module("bridge");
1093
1094			mutex_lock(&br_ioctl_mutex);
1095			if (br_ioctl_hook)
1096				err = br_ioctl_hook(net, cmd, argp);
1097			mutex_unlock(&br_ioctl_mutex);
1098			break;
1099		case SIOCGIFVLAN:
1100		case SIOCSIFVLAN:
1101			err = -ENOPKG;
1102			if (!vlan_ioctl_hook)
1103				request_module("8021q");
1104
1105			mutex_lock(&vlan_ioctl_mutex);
1106			if (vlan_ioctl_hook)
1107				err = vlan_ioctl_hook(net, argp);
1108			mutex_unlock(&vlan_ioctl_mutex);
1109			break;
1110		case SIOCADDDLCI:
1111		case SIOCDELDLCI:
1112			err = -ENOPKG;
1113			if (!dlci_ioctl_hook)
1114				request_module("dlci");
1115
1116			mutex_lock(&dlci_ioctl_mutex);
1117			if (dlci_ioctl_hook)
1118				err = dlci_ioctl_hook(cmd, argp);
1119			mutex_unlock(&dlci_ioctl_mutex);
1120			break;
1121		default:
1122			err = sock_do_ioctl(net, sock, cmd, arg);
1123			break;
1124		}
1125	return err;
1126}
1127
1128int sock_create_lite(int family, int type, int protocol, struct socket **res)
1129{
1130	int err;
1131	struct socket *sock = NULL;
1132
1133	err = security_socket_create(family, type, protocol, 1);
1134	if (err)
1135		goto out;
1136
1137	sock = sock_alloc();
1138	if (!sock) {
1139		err = -ENOMEM;
1140		goto out;
1141	}
1142
1143	sock->type = type;
1144	err = security_socket_post_create(sock, family, type, protocol, 1);
1145	if (err)
1146		goto out_release;
1147
1148out:
1149	*res = sock;
1150	return err;
1151out_release:
1152	sock_release(sock);
1153	sock = NULL;
1154	goto out;
1155}
1156EXPORT_SYMBOL(sock_create_lite);
1157
1158/* No kernel lock held - perfect */
1159static unsigned int sock_poll(struct file *file, poll_table *wait)
1160{
1161	struct socket *sock;
1162
1163	/*
1164	 *      We can't return errors to poll, so it's either yes or no.
1165	 */
1166	sock = file->private_data;
1167	return sock->ops->poll(file, sock, wait);
1168}
1169
1170static int sock_mmap(struct file *file, struct vm_area_struct *vma)
1171{
1172	struct socket *sock = file->private_data;
1173
1174	return sock->ops->mmap(file, sock, vma);
1175}
1176
1177static int sock_close(struct inode *inode, struct file *filp)
1178{
1179	/*
1180	 *      It was possible the inode is NULL we were
1181	 *      closing an unfinished socket.
1182	 */
1183
1184	if (!inode) {
1185		printk(KERN_DEBUG "sock_close: NULL inode\n");
1186		return 0;
1187	}
1188	sock_release(SOCKET_I(inode));
1189	return 0;
1190}
1191
1192/*
1193 *	Update the socket async list
1194 *
1195 *	Fasync_list locking strategy.
1196 *
1197 *	1. fasync_list is modified only under process context socket lock
1198 *	   i.e. under semaphore.
1199 *	2. fasync_list is used under read_lock(&sk->sk_callback_lock)
1200 *	   or under socket lock
1201 */
1202
1203static int sock_fasync(int fd, struct file *filp, int on)
1204{
1205	struct socket *sock = filp->private_data;
1206	struct sock *sk = sock->sk;
1207	struct socket_wq *wq;
1208
1209	if (sk == NULL)
1210		return -EINVAL;
1211
1212	lock_sock(sk);
1213	wq = rcu_dereference_protected(sock->wq, sock_owned_by_user(sk));
1214	fasync_helper(fd, filp, on, &wq->fasync_list);
1215
1216	if (!wq->fasync_list)
1217		sock_reset_flag(sk, SOCK_FASYNC);
1218	else
1219		sock_set_flag(sk, SOCK_FASYNC);
1220
1221	release_sock(sk);
1222	return 0;
1223}
1224
1225/* This function may be called only under socket lock or callback_lock or rcu_lock */
1226
1227int sock_wake_async(struct socket *sock, int how, int band)
1228{
1229	struct socket_wq *wq;
1230
1231	if (!sock)
1232		return -1;
1233	rcu_read_lock();
1234	wq = rcu_dereference(sock->wq);
1235	if (!wq || !wq->fasync_list) {
1236		rcu_read_unlock();
1237		return -1;
1238	}
1239	switch (how) {
1240	case SOCK_WAKE_WAITD:
1241		if (test_bit(SOCK_ASYNC_WAITDATA, &sock->flags))
1242			break;
1243		goto call_kill;
1244	case SOCK_WAKE_SPACE:
1245		if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags))
1246			break;
1247		/* fall through */
1248	case SOCK_WAKE_IO:
1249call_kill:
1250		kill_fasync(&wq->fasync_list, SIGIO, band);
1251		break;
1252	case SOCK_WAKE_URG:
1253		kill_fasync(&wq->fasync_list, SIGURG, band);
1254	}
1255	rcu_read_unlock();
1256	return 0;
1257}
1258EXPORT_SYMBOL(sock_wake_async);
1259
1260int __sock_create(struct net *net, int family, int type, int protocol,
1261			 struct socket **res, int kern)
1262{
1263	int err;
1264	struct socket *sock;
1265	const struct net_proto_family *pf;
1266
1267	/*
1268	 *      Check protocol is in range
1269	 */
1270	if (family < 0 || family >= NPROTO)
1271		return -EAFNOSUPPORT;
1272	if (type < 0 || type >= SOCK_MAX)
1273		return -EINVAL;
1274
1275	/* Compatibility.
1276
1277	   This uglymoron is moved from INET layer to here to avoid
1278	   deadlock in module load.
1279	 */
1280	if (family == PF_INET && type == SOCK_PACKET) {
1281		static int warned;
1282		if (!warned) {
1283			warned = 1;
1284			printk(KERN_INFO "%s uses obsolete (PF_INET,SOCK_PACKET)\n",
1285			       current->comm);
1286		}
1287		family = PF_PACKET;
1288	}
1289
1290	err = security_socket_create(family, type, protocol, kern);
1291	if (err)
1292		return err;
1293
1294	/*
1295	 *	Allocate the socket and allow the family to set things up. if
1296	 *	the protocol is 0, the family is instructed to select an appropriate
1297	 *	default.
1298	 */
1299	sock = sock_alloc();
1300	if (!sock) {
1301		net_warn_ratelimited("socket: no more sockets\n");
1302		return -ENFILE;	/* Not exactly a match, but its the
1303				   closest posix thing */
1304	}
1305
1306	sock->type = type;
1307
1308#ifdef CONFIG_MODULES
1309	/* Attempt to load a protocol module if the find failed.
1310	 *
1311	 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
1312	 * requested real, full-featured networking support upon configuration.
1313	 * Otherwise module support will break!
1314	 */
1315	if (rcu_access_pointer(net_families[family]) == NULL)
1316		request_module("net-pf-%d", family);
1317#endif
1318
1319	rcu_read_lock();
1320	pf = rcu_dereference(net_families[family]);
1321	err = -EAFNOSUPPORT;
1322	if (!pf)
1323		goto out_release;
1324
1325	/*
1326	 * We will call the ->create function, that possibly is in a loadable
1327	 * module, so we have to bump that loadable module refcnt first.
1328	 */
1329	if (!try_module_get(pf->owner))
1330		goto out_release;
1331
1332	/* Now protected by module ref count */
1333	rcu_read_unlock();
1334
1335	err = pf->create(net, sock, protocol, kern);
1336	if (err < 0)
1337		goto out_module_put;
1338
1339	/*
1340	 * Now to bump the refcnt of the [loadable] module that owns this
1341	 * socket at sock_release time we decrement its refcnt.
1342	 */
1343	if (!try_module_get(sock->ops->owner))
1344		goto out_module_busy;
1345
1346	/*
1347	 * Now that we're done with the ->create function, the [loadable]
1348	 * module can have its refcnt decremented
1349	 */
1350	module_put(pf->owner);
1351	err = security_socket_post_create(sock, family, type, protocol, kern);
1352	if (err)
1353		goto out_sock_release;
1354	*res = sock;
1355
1356	return 0;
1357
1358out_module_busy:
1359	err = -EAFNOSUPPORT;
1360out_module_put:
1361	sock->ops = NULL;
1362	module_put(pf->owner);
1363out_sock_release:
1364	sock_release(sock);
1365	return err;
1366
1367out_release:
1368	rcu_read_unlock();
1369	goto out_sock_release;
1370}
1371EXPORT_SYMBOL(__sock_create);
1372
1373int sock_create(int family, int type, int protocol, struct socket **res)
1374{
1375	return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0);
1376}
1377EXPORT_SYMBOL(sock_create);
1378
1379int sock_create_kern(int family, int type, int protocol, struct socket **res)
1380{
1381	return __sock_create(&init_net, family, type, protocol, res, 1);
1382}
1383EXPORT_SYMBOL(sock_create_kern);
1384
1385SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol)
1386{
1387	int retval;
1388	struct socket *sock;
1389	int flags;
1390
1391	/* Check the SOCK_* constants for consistency.  */
1392	BUILD_BUG_ON(SOCK_CLOEXEC != O_CLOEXEC);
1393	BUILD_BUG_ON((SOCK_MAX | SOCK_TYPE_MASK) != SOCK_TYPE_MASK);
1394	BUILD_BUG_ON(SOCK_CLOEXEC & SOCK_TYPE_MASK);
1395	BUILD_BUG_ON(SOCK_NONBLOCK & SOCK_TYPE_MASK);
1396
1397	flags = type & ~SOCK_TYPE_MASK;
1398	if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1399		return -EINVAL;
1400	type &= SOCK_TYPE_MASK;
1401
1402	if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1403		flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1404
1405	retval = sock_create(family, type, protocol, &sock);
1406	if (retval < 0)
1407		goto out;
1408
1409	retval = sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK));
1410	if (retval < 0)
1411		goto out_release;
1412
1413out:
1414	/* It may be already another descriptor 8) Not kernel problem. */
1415	return retval;
1416
1417out_release:
1418	sock_release(sock);
1419	return retval;
1420}
1421
1422/*
1423 *	Create a pair of connected sockets.
1424 */
1425
1426SYSCALL_DEFINE4(socketpair, int, family, int, type, int, protocol,
1427		int __user *, usockvec)
1428{
1429	struct socket *sock1, *sock2;
1430	int fd1, fd2, err;
1431	struct file *newfile1, *newfile2;
1432	int flags;
1433
1434	flags = type & ~SOCK_TYPE_MASK;
1435	if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1436		return -EINVAL;
1437	type &= SOCK_TYPE_MASK;
1438
1439	if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1440		flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1441
1442	/*
1443	 * Obtain the first socket and check if the underlying protocol
1444	 * supports the socketpair call.
1445	 */
1446
1447	err = sock_create(family, type, protocol, &sock1);
1448	if (err < 0)
1449		goto out;
1450
1451	err = sock_create(family, type, protocol, &sock2);
1452	if (err < 0)
1453		goto out_release_1;
1454
1455	err = sock1->ops->socketpair(sock1, sock2);
1456	if (err < 0)
1457		goto out_release_both;
1458
1459	fd1 = get_unused_fd_flags(flags);
1460	if (unlikely(fd1 < 0)) {
1461		err = fd1;
1462		goto out_release_both;
1463	}
1464	fd2 = get_unused_fd_flags(flags);
1465	if (unlikely(fd2 < 0)) {
1466		err = fd2;
1467		put_unused_fd(fd1);
1468		goto out_release_both;
1469	}
1470
1471	newfile1 = sock_alloc_file(sock1, flags, NULL);
1472	if (unlikely(IS_ERR(newfile1))) {
1473		err = PTR_ERR(newfile1);
1474		put_unused_fd(fd1);
1475		put_unused_fd(fd2);
1476		goto out_release_both;
1477	}
1478
1479	newfile2 = sock_alloc_file(sock2, flags, NULL);
1480	if (IS_ERR(newfile2)) {
1481		err = PTR_ERR(newfile2);
1482		fput(newfile1);
1483		put_unused_fd(fd1);
1484		put_unused_fd(fd2);
1485		sock_release(sock2);
1486		goto out;
1487	}
1488
1489	audit_fd_pair(fd1, fd2);
1490	fd_install(fd1, newfile1);
1491	fd_install(fd2, newfile2);
1492	/* fd1 and fd2 may be already another descriptors.
1493	 * Not kernel problem.
1494	 */
1495
1496	err = put_user(fd1, &usockvec[0]);
1497	if (!err)
1498		err = put_user(fd2, &usockvec[1]);
1499	if (!err)
1500		return 0;
1501
1502	sys_close(fd2);
1503	sys_close(fd1);
1504	return err;
1505
1506out_release_both:
1507	sock_release(sock2);
1508out_release_1:
1509	sock_release(sock1);
1510out:
1511	return err;
1512}
1513
1514/*
1515 *	Bind a name to a socket. Nothing much to do here since it's
1516 *	the protocol's responsibility to handle the local address.
1517 *
1518 *	We move the socket address to kernel space before we call
1519 *	the protocol layer (having also checked the address is ok).
1520 */
1521
1522SYSCALL_DEFINE3(bind, int, fd, struct sockaddr __user *, umyaddr, int, addrlen)
1523{
1524	struct socket *sock;
1525	struct sockaddr_storage address;
1526	int err, fput_needed;
1527
1528	sock = sockfd_lookup_light(fd, &err, &fput_needed);
1529	if (sock) {
1530		err = move_addr_to_kernel(umyaddr, addrlen, &address);
1531		if (err >= 0) {
1532			err = security_socket_bind(sock,
1533						   (struct sockaddr *)&address,
1534						   addrlen);
1535			if (!err)
1536				err = sock->ops->bind(sock,
1537						      (struct sockaddr *)
1538						      &address, addrlen);
1539		}
1540		fput_light(sock->file, fput_needed);
1541	}
1542	return err;
1543}
1544
1545/*
1546 *	Perform a listen. Basically, we allow the protocol to do anything
1547 *	necessary for a listen, and if that works, we mark the socket as
1548 *	ready for listening.
1549 */
1550
1551SYSCALL_DEFINE2(listen, int, fd, int, backlog)
1552{
1553	struct socket *sock;
1554	int err, fput_needed;
1555	int somaxconn;
1556
1557	sock = sockfd_lookup_light(fd, &err, &fput_needed);
1558	if (sock) {
1559		somaxconn = sock_net(sock->sk)->core.sysctl_somaxconn;
1560		if ((unsigned int)backlog > somaxconn)
1561			backlog = somaxconn;
1562
1563		err = security_socket_listen(sock, backlog);
1564		if (!err)
1565			err = sock->ops->listen(sock, backlog);
1566
1567		fput_light(sock->file, fput_needed);
1568	}
1569	return err;
1570}
1571
1572/*
1573 *	For accept, we attempt to create a new socket, set up the link
1574 *	with the client, wake up the client, then return the new
1575 *	connected fd. We collect the address of the connector in kernel
1576 *	space and move it to user at the very end. This is unclean because
1577 *	we open the socket then return an error.
1578 *
1579 *	1003.1g adds the ability to recvmsg() to query connection pending
1580 *	status to recvmsg. We need to add that support in a way thats
1581 *	clean when we restucture accept also.
1582 */
1583
1584SYSCALL_DEFINE4(accept4, int, fd, struct sockaddr __user *, upeer_sockaddr,
1585		int __user *, upeer_addrlen, int, flags)
1586{
1587	struct socket *sock, *newsock;
1588	struct file *newfile;
1589	int err, len, newfd, fput_needed;
1590	struct sockaddr_storage address;
1591
1592	if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1593		return -EINVAL;
1594
1595	if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1596		flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1597
1598	sock = sockfd_lookup_light(fd, &err, &fput_needed);
1599	if (!sock)
1600		goto out;
1601
1602	err = -ENFILE;
1603	newsock = sock_alloc();
1604	if (!newsock)
1605		goto out_put;
1606
1607	newsock->type = sock->type;
1608	newsock->ops = sock->ops;
1609
1610	/*
1611	 * We don't need try_module_get here, as the listening socket (sock)
1612	 * has the protocol module (sock->ops->owner) held.
1613	 */
1614	__module_get(newsock->ops->owner);
1615
1616	newfd = get_unused_fd_flags(flags);
1617	if (unlikely(newfd < 0)) {
1618		err = newfd;
1619		sock_release(newsock);
1620		goto out_put;
1621	}
1622	newfile = sock_alloc_file(newsock, flags, sock->sk->sk_prot_creator->name);
1623	if (unlikely(IS_ERR(newfile))) {
1624		err = PTR_ERR(newfile);
1625		put_unused_fd(newfd);
1626		sock_release(newsock);
1627		goto out_put;
1628	}
1629
1630	err = security_socket_accept(sock, newsock);
1631	if (err)
1632		goto out_fd;
1633
1634	err = sock->ops->accept(sock, newsock, sock->file->f_flags);
1635	if (err < 0)
1636		goto out_fd;
1637
1638	if (upeer_sockaddr) {
1639		if (newsock->ops->getname(newsock, (struct sockaddr *)&address,
1640					  &len, 2) < 0) {
1641			err = -ECONNABORTED;
1642			goto out_fd;
1643		}
1644		err = move_addr_to_user(&address,
1645					len, upeer_sockaddr, upeer_addrlen);
1646		if (err < 0)
1647			goto out_fd;
1648	}
1649
1650	/* File flags are not inherited via accept() unlike another OSes. */
1651
1652	fd_install(newfd, newfile);
1653	err = newfd;
1654
1655out_put:
1656	fput_light(sock->file, fput_needed);
1657out:
1658	return err;
1659out_fd:
1660	fput(newfile);
1661	put_unused_fd(newfd);
1662	goto out_put;
1663}
1664
1665SYSCALL_DEFINE3(accept, int, fd, struct sockaddr __user *, upeer_sockaddr,
1666		int __user *, upeer_addrlen)
1667{
1668	return sys_accept4(fd, upeer_sockaddr, upeer_addrlen, 0);
1669}
1670
1671/*
1672 *	Attempt to connect to a socket with the server address.  The address
1673 *	is in user space so we verify it is OK and move it to kernel space.
1674 *
1675 *	For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
1676 *	break bindings
1677 *
1678 *	NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
1679 *	other SEQPACKET protocols that take time to connect() as it doesn't
1680 *	include the -EINPROGRESS status for such sockets.
1681 */
1682
1683SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
1684		int, addrlen)
1685{
1686	struct socket *sock;
1687	struct sockaddr_storage address;
1688	int err, fput_needed;
1689
1690	sock = sockfd_lookup_light(fd, &err, &fput_needed);
1691	if (!sock)
1692		goto out;
1693	err = move_addr_to_kernel(uservaddr, addrlen, &address);
1694	if (err < 0)
1695		goto out_put;
1696
1697	err =
1698	    security_socket_connect(sock, (struct sockaddr *)&address, addrlen);
1699	if (err)
1700		goto out_put;
1701
1702	err = sock->ops->connect(sock, (struct sockaddr *)&address, addrlen,
1703				 sock->file->f_flags);
1704out_put:
1705	fput_light(sock->file, fput_needed);
1706out:
1707	return err;
1708}
1709
1710/*
1711 *	Get the local address ('name') of a socket object. Move the obtained
1712 *	name to user space.
1713 */
1714
1715SYSCALL_DEFINE3(getsockname, int, fd, struct sockaddr __user *, usockaddr,
1716		int __user *, usockaddr_len)
1717{
1718	struct socket *sock;
1719	struct sockaddr_storage address;
1720	int len, err, fput_needed;
1721
1722	sock = sockfd_lookup_light(fd, &err, &fput_needed);
1723	if (!sock)
1724		goto out;
1725
1726	err = security_socket_getsockname(sock);
1727	if (err)
1728		goto out_put;
1729
1730	err = sock->ops->getname(sock, (struct sockaddr *)&address, &len, 0);
1731	if (err)
1732		goto out_put;
1733	err = move_addr_to_user(&address, len, usockaddr, usockaddr_len);
1734
1735out_put:
1736	fput_light(sock->file, fput_needed);
1737out:
1738	return err;
1739}
1740
1741/*
1742 *	Get the remote address ('name') of a socket object. Move the obtained
1743 *	name to user space.
1744 */
1745
1746SYSCALL_DEFINE3(getpeername, int, fd, struct sockaddr __user *, usockaddr,
1747		int __user *, usockaddr_len)
1748{
1749	struct socket *sock;
1750	struct sockaddr_storage address;
1751	int len, err, fput_needed;
1752
1753	sock = sockfd_lookup_light(fd, &err, &fput_needed);
1754	if (sock != NULL) {
1755		err = security_socket_getpeername(sock);
1756		if (err) {
1757			fput_light(sock->file, fput_needed);
1758			return err;
1759		}
1760
1761		err =
1762		    sock->ops->getname(sock, (struct sockaddr *)&address, &len,
1763				       1);
1764		if (!err)
1765			err = move_addr_to_user(&address, len, usockaddr,
1766						usockaddr_len);
1767		fput_light(sock->file, fput_needed);
1768	}
1769	return err;
1770}
1771
1772/*
1773 *	Send a datagram to a given address. We move the address into kernel
1774 *	space and check the user space data area is readable before invoking
1775 *	the protocol.
1776 */
1777
1778SYSCALL_DEFINE6(sendto, int, fd, void __user *, buff, size_t, len,
1779		unsigned int, flags, struct sockaddr __user *, addr,
1780		int, addr_len)
1781{
1782	struct socket *sock;
1783	struct sockaddr_storage address;
1784	int err;
1785	struct msghdr msg;
1786	struct iovec iov;
1787	int fput_needed;
1788
1789	if (len > INT_MAX)
1790		len = INT_MAX;
1791	sock = sockfd_lookup_light(fd, &err, &fput_needed);
1792	if (!sock)
1793		goto out;
1794
1795	iov.iov_base = buff;
1796	iov.iov_len = len;
1797	msg.msg_name = NULL;
1798	msg.msg_iov = &iov;
1799	msg.msg_iovlen = 1;
1800	msg.msg_control = NULL;
1801	msg.msg_controllen = 0;
1802	msg.msg_namelen = 0;
1803	if (addr) {
1804		err = move_addr_to_kernel(addr, addr_len, &address);
1805		if (err < 0)
1806			goto out_put;
1807		msg.msg_name = (struct sockaddr *)&address;
1808		msg.msg_namelen = addr_len;
1809	}
1810	if (sock->file->f_flags & O_NONBLOCK)
1811		flags |= MSG_DONTWAIT;
1812	msg.msg_flags = flags;
1813	err = sock_sendmsg(sock, &msg, len);
1814
1815out_put:
1816	fput_light(sock->file, fput_needed);
1817out:
1818	return err;
1819}
1820
1821/*
1822 *	Send a datagram down a socket.
1823 */
1824
1825SYSCALL_DEFINE4(send, int, fd, void __user *, buff, size_t, len,
1826		unsigned int, flags)
1827{
1828	return sys_sendto(fd, buff, len, flags, NULL, 0);
1829}
1830
1831/*
1832 *	Receive a frame from the socket and optionally record the address of the
1833 *	sender. We verify the buffers are writable and if needed move the
1834 *	sender address from kernel to user space.
1835 */
1836
1837SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, ubuf, size_t, size,
1838		unsigned int, flags, struct sockaddr __user *, addr,
1839		int __user *, addr_len)
1840{
1841	struct socket *sock;
1842	struct iovec iov;
1843	struct msghdr msg;
1844	struct sockaddr_storage address;
1845	int err, err2;
1846	int fput_needed;
1847
1848	if (size > INT_MAX)
1849		size = INT_MAX;
1850	sock = sockfd_lookup_light(fd, &err, &fput_needed);
1851	if (!sock)
1852		goto out;
1853
1854	msg.msg_control = NULL;
1855	msg.msg_controllen = 0;
1856	msg.msg_iovlen = 1;
1857	msg.msg_iov = &iov;
1858	iov.iov_len = size;
1859	iov.iov_base = ubuf;
1860	/* Save some cycles and don't copy the address if not needed */
1861	msg.msg_name = addr ? (struct sockaddr *)&address : NULL;
1862	/* We assume all kernel code knows the size of sockaddr_storage */
1863	msg.msg_namelen = 0;
1864	if (sock->file->f_flags & O_NONBLOCK)
1865		flags |= MSG_DONTWAIT;
1866	err = sock_recvmsg(sock, &msg, size, flags);
1867
1868	if (err >= 0 && addr != NULL) {
1869		err2 = move_addr_to_user(&address,
1870					 msg.msg_namelen, addr, addr_len);
1871		if (err2 < 0)
1872			err = err2;
1873	}
1874
1875	fput_light(sock->file, fput_needed);
1876out:
1877	return err;
1878}
1879
1880/*
1881 *	Receive a datagram from a socket.
1882 */
1883
1884asmlinkage long sys_recv(int fd, void __user *ubuf, size_t size,
1885			 unsigned int flags)
1886{
1887	return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
1888}
1889
1890/*
1891 *	Set a socket option. Because we don't know the option lengths we have
1892 *	to pass the user mode parameter for the protocols to sort out.
1893 */
1894
1895SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname,
1896		char __user *, optval, int, optlen)
1897{
1898	int err, fput_needed;
1899	struct socket *sock;
1900
1901	if (optlen < 0)
1902		return -EINVAL;
1903
1904	sock = sockfd_lookup_light(fd, &err, &fput_needed);
1905	if (sock != NULL) {
1906		err = security_socket_setsockopt(sock, level, optname);
1907		if (err)
1908			goto out_put;
1909
1910		if (level == SOL_SOCKET)
1911			err =
1912			    sock_setsockopt(sock, level, optname, optval,
1913					    optlen);
1914		else
1915			err =
1916			    sock->ops->setsockopt(sock, level, optname, optval,
1917						  optlen);
1918out_put:
1919		fput_light(sock->file, fput_needed);
1920	}
1921	return err;
1922}
1923
1924/*
1925 *	Get a socket option. Because we don't know the option lengths we have
1926 *	to pass a user mode parameter for the protocols to sort out.
1927 */
1928
1929SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname,
1930		char __user *, optval, int __user *, optlen)
1931{
1932	int err, fput_needed;
1933	struct socket *sock;
1934
1935	sock = sockfd_lookup_light(fd, &err, &fput_needed);
1936	if (sock != NULL) {
1937		err = security_socket_getsockopt(sock, level, optname);
1938		if (err)
1939			goto out_put;
1940
1941		if (level == SOL_SOCKET)
1942			err =
1943			    sock_getsockopt(sock, level, optname, optval,
1944					    optlen);
1945		else
1946			err =
1947			    sock->ops->getsockopt(sock, level, optname, optval,
1948						  optlen);
1949out_put:
1950		fput_light(sock->file, fput_needed);
1951	}
1952	return err;
1953}
1954
1955/*
1956 *	Shutdown a socket.
1957 */
1958
1959SYSCALL_DEFINE2(shutdown, int, fd, int, how)
1960{
1961	int err, fput_needed;
1962	struct socket *sock;
1963
1964	sock = sockfd_lookup_light(fd, &err, &fput_needed);
1965	if (sock != NULL) {
1966		err = security_socket_shutdown(sock, how);
1967		if (!err)
1968			err = sock->ops->shutdown(sock, how);
1969		fput_light(sock->file, fput_needed);
1970	}
1971	return err;
1972}
1973
1974/* A couple of helpful macros for getting the address of the 32/64 bit
1975 * fields which are the same type (int / unsigned) on our platforms.
1976 */
1977#define COMPAT_MSG(msg, member)	((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
1978#define COMPAT_NAMELEN(msg)	COMPAT_MSG(msg, msg_namelen)
1979#define COMPAT_FLAGS(msg)	COMPAT_MSG(msg, msg_flags)
1980
1981struct used_address {
1982	struct sockaddr_storage name;
1983	unsigned int name_len;
1984};
1985
1986static int copy_msghdr_from_user(struct msghdr *kmsg,
1987				 struct msghdr __user *umsg)
1988{
1989	if (copy_from_user(kmsg, umsg, sizeof(struct msghdr)))
1990		return -EFAULT;
1991
1992	if (kmsg->msg_namelen < 0)
1993		return -EINVAL;
1994
1995	if (kmsg->msg_namelen > sizeof(struct sockaddr_storage))
1996		kmsg->msg_namelen = sizeof(struct sockaddr_storage);
1997	return 0;
1998}
1999
2000static int ___sys_sendmsg(struct socket *sock, struct msghdr __user *msg,
2001			 struct msghdr *msg_sys, unsigned int flags,
2002			 struct used_address *used_address)
2003{
2004	struct compat_msghdr __user *msg_compat =
2005	    (struct compat_msghdr __user *)msg;
2006	struct sockaddr_storage address;
2007	struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
2008	unsigned char ctl[sizeof(struct cmsghdr) + 20]
2009	    __attribute__ ((aligned(sizeof(__kernel_size_t))));
2010	/* 20 is size of ipv6_pktinfo */
2011	unsigned char *ctl_buf = ctl;
2012	int err, ctl_len, total_len;
2013
2014	err = -EFAULT;
2015	if (MSG_CMSG_COMPAT & flags) {
2016		if (get_compat_msghdr(msg_sys, msg_compat))
2017			return -EFAULT;
2018	} else {
2019		err = copy_msghdr_from_user(msg_sys, msg);
2020		if (err)
2021			return err;
2022	}
2023
2024	if (msg_sys->msg_iovlen > UIO_FASTIOV) {
2025		err = -EMSGSIZE;
2026		if (msg_sys->msg_iovlen > UIO_MAXIOV)
2027			goto out;
2028		err = -ENOMEM;
2029		iov = kmalloc(msg_sys->msg_iovlen * sizeof(struct iovec),
2030			      GFP_KERNEL);
2031		if (!iov)
2032			goto out;
2033	}
2034
2035	/* This will also move the address data into kernel space */
2036	if (MSG_CMSG_COMPAT & flags) {
2037		err = verify_compat_iovec(msg_sys, iov, &address, VERIFY_READ);
2038	} else
2039		err = verify_iovec(msg_sys, iov, &address, VERIFY_READ);
2040	if (err < 0)
2041		goto out_freeiov;
2042	total_len = err;
2043
2044	err = -ENOBUFS;
2045
2046	if (msg_sys->msg_controllen > INT_MAX)
2047		goto out_freeiov;
2048	ctl_len = msg_sys->msg_controllen;
2049	if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
2050		err =
2051		    cmsghdr_from_user_compat_to_kern(msg_sys, sock->sk, ctl,
2052						     sizeof(ctl));
2053		if (err)
2054			goto out_freeiov;
2055		ctl_buf = msg_sys->msg_control;
2056		ctl_len = msg_sys->msg_controllen;
2057	} else if (ctl_len) {
2058		if (ctl_len > sizeof(ctl)) {
2059			ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
2060			if (ctl_buf == NULL)
2061				goto out_freeiov;
2062		}
2063		err = -EFAULT;
2064		/*
2065		 * Careful! Before this, msg_sys->msg_control contains a user pointer.
2066		 * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
2067		 * checking falls down on this.
2068		 */
2069		if (copy_from_user(ctl_buf,
2070				   (void __user __force *)msg_sys->msg_control,
2071				   ctl_len))
2072			goto out_freectl;
2073		msg_sys->msg_control = ctl_buf;
2074	}
2075	msg_sys->msg_flags = flags;
2076
2077	if (sock->file->f_flags & O_NONBLOCK)
2078		msg_sys->msg_flags |= MSG_DONTWAIT;
2079	/*
2080	 * If this is sendmmsg() and current destination address is same as
2081	 * previously succeeded address, omit asking LSM's decision.
2082	 * used_address->name_len is initialized to UINT_MAX so that the first
2083	 * destination address never matches.
2084	 */
2085	if (used_address && msg_sys->msg_name &&
2086	    used_address->name_len == msg_sys->msg_namelen &&
2087	    !memcmp(&used_address->name, msg_sys->msg_name,
2088		    used_address->name_len)) {
2089		err = sock_sendmsg_nosec(sock, msg_sys, total_len);
2090		goto out_freectl;
2091	}
2092	err = sock_sendmsg(sock, msg_sys, total_len);
2093	/*
2094	 * If this is sendmmsg() and sending to current destination address was
2095	 * successful, remember it.
2096	 */
2097	if (used_address && err >= 0) {
2098		used_address->name_len = msg_sys->msg_namelen;
2099		if (msg_sys->msg_name)
2100			memcpy(&used_address->name, msg_sys->msg_name,
2101			       used_address->name_len);
2102	}
2103
2104out_freectl:
2105	if (ctl_buf != ctl)
2106		sock_kfree_s(sock->sk, ctl_buf, ctl_len);
2107out_freeiov:
2108	if (iov != iovstack)
2109		kfree(iov);
2110out:
2111	return err;
2112}
2113
2114/*
2115 *	BSD sendmsg interface
2116 */
2117
2118long __sys_sendmsg(int fd, struct msghdr __user *msg, unsigned flags)
2119{
2120	int fput_needed, err;
2121	struct msghdr msg_sys;
2122	struct socket *sock;
2123
2124	sock = sockfd_lookup_light(fd, &err, &fput_needed);
2125	if (!sock)
2126		goto out;
2127
2128	err = ___sys_sendmsg(sock, msg, &msg_sys, flags, NULL);
2129
2130	fput_light(sock->file, fput_needed);
2131out:
2132	return err;
2133}
2134
2135SYSCALL_DEFINE3(sendmsg, int, fd, struct msghdr __user *, msg, unsigned int, flags)
2136{
2137	if (flags & MSG_CMSG_COMPAT)
2138		return -EINVAL;
2139	return __sys_sendmsg(fd, msg, flags);
2140}
2141
2142/*
2143 *	Linux sendmmsg interface
2144 */
2145
2146int __sys_sendmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
2147		   unsigned int flags)
2148{
2149	int fput_needed, err, datagrams;
2150	struct socket *sock;
2151	struct mmsghdr __user *entry;
2152	struct compat_mmsghdr __user *compat_entry;
2153	struct msghdr msg_sys;
2154	struct used_address used_address;
2155
2156	if (vlen > UIO_MAXIOV)
2157		vlen = UIO_MAXIOV;
2158
2159	datagrams = 0;
2160
2161	sock = sockfd_lookup_light(fd, &err, &fput_needed);
2162	if (!sock)
2163		return err;
2164
2165	used_address.name_len = UINT_MAX;
2166	entry = mmsg;
2167	compat_entry = (struct compat_mmsghdr __user *)mmsg;
2168	err = 0;
2169
2170	while (datagrams < vlen) {
2171		if (MSG_CMSG_COMPAT & flags) {
2172			err = ___sys_sendmsg(sock, (struct msghdr __user *)compat_entry,
2173					     &msg_sys, flags, &used_address);
2174			if (err < 0)
2175				break;
2176			err = __put_user(err, &compat_entry->msg_len);
2177			++compat_entry;
2178		} else {
2179			err = ___sys_sendmsg(sock,
2180					     (struct msghdr __user *)entry,
2181					     &msg_sys, flags, &used_address);
2182			if (err < 0)
2183				break;
2184			err = put_user(err, &entry->msg_len);
2185			++entry;
2186		}
2187
2188		if (err)
2189			break;
2190		++datagrams;
2191	}
2192
2193	fput_light(sock->file, fput_needed);
2194
2195	/* We only return an error if no datagrams were able to be sent */
2196	if (datagrams != 0)
2197		return datagrams;
2198
2199	return err;
2200}
2201
2202SYSCALL_DEFINE4(sendmmsg, int, fd, struct mmsghdr __user *, mmsg,
2203		unsigned int, vlen, unsigned int, flags)
2204{
2205	if (flags & MSG_CMSG_COMPAT)
2206		return -EINVAL;
2207	return __sys_sendmmsg(fd, mmsg, vlen, flags);
2208}
2209
2210static int ___sys_recvmsg(struct socket *sock, struct msghdr __user *msg,
2211			 struct msghdr *msg_sys, unsigned int flags, int nosec)
2212{
2213	struct compat_msghdr __user *msg_compat =
2214	    (struct compat_msghdr __user *)msg;
2215	struct iovec iovstack[UIO_FASTIOV];
2216	struct iovec *iov = iovstack;
2217	unsigned long cmsg_ptr;
2218	int err, total_len, len;
2219
2220	/* kernel mode address */
2221	struct sockaddr_storage addr;
2222
2223	/* user mode address pointers */
2224	struct sockaddr __user *uaddr;
2225	int __user *uaddr_len;
2226
2227	if (MSG_CMSG_COMPAT & flags) {
2228		if (get_compat_msghdr(msg_sys, msg_compat))
2229			return -EFAULT;
2230	} else {
2231		err = copy_msghdr_from_user(msg_sys, msg);
2232		if (err)
2233			return err;
2234	}
2235
2236	if (msg_sys->msg_iovlen > UIO_FASTIOV) {
2237		err = -EMSGSIZE;
2238		if (msg_sys->msg_iovlen > UIO_MAXIOV)
2239			goto out;
2240		err = -ENOMEM;
2241		iov = kmalloc(msg_sys->msg_iovlen * sizeof(struct iovec),
2242			      GFP_KERNEL);
2243		if (!iov)
2244			goto out;
2245	}
2246
2247	/* Save the user-mode address (verify_iovec will change the
2248	 * kernel msghdr to use the kernel address space)
2249	 */
2250	uaddr = (__force void __user *)msg_sys->msg_name;
2251	uaddr_len = COMPAT_NAMELEN(msg);
2252	if (MSG_CMSG_COMPAT & flags)
2253		err = verify_compat_iovec(msg_sys, iov, &addr, VERIFY_WRITE);
2254	else
2255		err = verify_iovec(msg_sys, iov, &addr, VERIFY_WRITE);
2256	if (err < 0)
2257		goto out_freeiov;
2258	total_len = err;
2259
2260	cmsg_ptr = (unsigned long)msg_sys->msg_control;
2261	msg_sys->msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT);
2262
2263	/* We assume all kernel code knows the size of sockaddr_storage */
2264	msg_sys->msg_namelen = 0;
2265
2266	if (sock->file->f_flags & O_NONBLOCK)
2267		flags |= MSG_DONTWAIT;
2268	err = (nosec ? sock_recvmsg_nosec : sock_recvmsg)(sock, msg_sys,
2269							  total_len, flags);
2270	if (err < 0)
2271		goto out_freeiov;
2272	len = err;
2273
2274	if (uaddr != NULL) {
2275		err = move_addr_to_user(&addr,
2276					msg_sys->msg_namelen, uaddr,
2277					uaddr_len);
2278		if (err < 0)
2279			goto out_freeiov;
2280	}
2281	err = __put_user((msg_sys->msg_flags & ~MSG_CMSG_COMPAT),
2282			 COMPAT_FLAGS(msg));
2283	if (err)
2284		goto out_freeiov;
2285	if (MSG_CMSG_COMPAT & flags)
2286		err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
2287				 &msg_compat->msg_controllen);
2288	else
2289		err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
2290				 &msg->msg_controllen);
2291	if (err)
2292		goto out_freeiov;
2293	err = len;
2294
2295out_freeiov:
2296	if (iov != iovstack)
2297		kfree(iov);
2298out:
2299	return err;
2300}
2301
2302/*
2303 *	BSD recvmsg interface
2304 */
2305
2306long __sys_recvmsg(int fd, struct msghdr __user *msg, unsigned flags)
2307{
2308	int fput_needed, err;
2309	struct msghdr msg_sys;
2310	struct socket *sock;
2311
2312	sock = sockfd_lookup_light(fd, &err, &fput_needed);
2313	if (!sock)
2314		goto out;
2315
2316	err = ___sys_recvmsg(sock, msg, &msg_sys, flags, 0);
2317
2318	fput_light(sock->file, fput_needed);
2319out:
2320	return err;
2321}
2322
2323SYSCALL_DEFINE3(recvmsg, int, fd, struct msghdr __user *, msg,
2324		unsigned int, flags)
2325{
2326	if (flags & MSG_CMSG_COMPAT)
2327		return -EINVAL;
2328	return __sys_recvmsg(fd, msg, flags);
2329}
2330
2331/*
2332 *     Linux recvmmsg interface
2333 */
2334
2335int __sys_recvmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
2336		   unsigned int flags, struct timespec *timeout)
2337{
2338	int fput_needed, err, datagrams;
2339	struct socket *sock;
2340	struct mmsghdr __user *entry;
2341	struct compat_mmsghdr __user *compat_entry;
2342	struct msghdr msg_sys;
2343	struct timespec end_time;
2344
2345	if (timeout &&
2346	    poll_select_set_timeout(&end_time, timeout->tv_sec,
2347				    timeout->tv_nsec))
2348		return -EINVAL;
2349
2350	datagrams = 0;
2351
2352	sock = sockfd_lookup_light(fd, &err, &fput_needed);
2353	if (!sock)
2354		return err;
2355
2356	err = sock_error(sock->sk);
2357	if (err)
2358		goto out_put;
2359
2360	entry = mmsg;
2361	compat_entry = (struct compat_mmsghdr __user *)mmsg;
2362
2363	while (datagrams < vlen) {
2364		/*
2365		 * No need to ask LSM for more than the first datagram.
2366		 */
2367		if (MSG_CMSG_COMPAT & flags) {
2368			err = ___sys_recvmsg(sock, (struct msghdr __user *)compat_entry,
2369					     &msg_sys, flags & ~MSG_WAITFORONE,
2370					     datagrams);
2371			if (err < 0)
2372				break;
2373			err = __put_user(err, &compat_entry->msg_len);
2374			++compat_entry;
2375		} else {
2376			err = ___sys_recvmsg(sock,
2377					     (struct msghdr __user *)entry,
2378					     &msg_sys, flags & ~MSG_WAITFORONE,
2379					     datagrams);
2380			if (err < 0)
2381				break;
2382			err = put_user(err, &entry->msg_len);
2383			++entry;
2384		}
2385
2386		if (err)
2387			break;
2388		++datagrams;
2389
2390		/* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
2391		if (flags & MSG_WAITFORONE)
2392			flags |= MSG_DONTWAIT;
2393
2394		if (timeout) {
2395			ktime_get_ts(timeout);
2396			*timeout = timespec_sub(end_time, *timeout);
2397			if (timeout->tv_sec < 0) {
2398				timeout->tv_sec = timeout->tv_nsec = 0;
2399				break;
2400			}
2401
2402			/* Timeout, return less than vlen datagrams */
2403			if (timeout->tv_nsec == 0 && timeout->tv_sec == 0)
2404				break;
2405		}
2406
2407		/* Out of band data, return right away */
2408		if (msg_sys.msg_flags & MSG_OOB)
2409			break;
2410	}
2411
2412out_put:
2413	fput_light(sock->file, fput_needed);
2414
2415	if (err == 0)
2416		return datagrams;
2417
2418	if (datagrams != 0) {
2419		/*
2420		 * We may return less entries than requested (vlen) if the
2421		 * sock is non block and there aren't enough datagrams...
2422		 */
2423		if (err != -EAGAIN) {
2424			/*
2425			 * ... or  if recvmsg returns an error after we
2426			 * received some datagrams, where we record the
2427			 * error to return on the next call or if the
2428			 * app asks about it using getsockopt(SO_ERROR).
2429			 */
2430			sock->sk->sk_err = -err;
2431		}
2432
2433		return datagrams;
2434	}
2435
2436	return err;
2437}
2438
2439SYSCALL_DEFINE5(recvmmsg, int, fd, struct mmsghdr __user *, mmsg,
2440		unsigned int, vlen, unsigned int, flags,
2441		struct timespec __user *, timeout)
2442{
2443	int datagrams;
2444	struct timespec timeout_sys;
2445
2446	if (flags & MSG_CMSG_COMPAT)
2447		return -EINVAL;
2448
2449	if (!timeout)
2450		return __sys_recvmmsg(fd, mmsg, vlen, flags, NULL);
2451
2452	if (copy_from_user(&timeout_sys, timeout, sizeof(timeout_sys)))
2453		return -EFAULT;
2454
2455	datagrams = __sys_recvmmsg(fd, mmsg, vlen, flags, &timeout_sys);
2456
2457	if (datagrams > 0 &&
2458	    copy_to_user(timeout, &timeout_sys, sizeof(timeout_sys)))
2459		datagrams = -EFAULT;
2460
2461	return datagrams;
2462}
2463
2464#ifdef __ARCH_WANT_SYS_SOCKETCALL
2465/* Argument list sizes for sys_socketcall */
2466#define AL(x) ((x) * sizeof(unsigned long))
2467static const unsigned char nargs[21] = {
2468	AL(0), AL(3), AL(3), AL(3), AL(2), AL(3),
2469	AL(3), AL(3), AL(4), AL(4), AL(4), AL(6),
2470	AL(6), AL(2), AL(5), AL(5), AL(3), AL(3),
2471	AL(4), AL(5), AL(4)
2472};
2473
2474#undef AL
2475
2476/*
2477 *	System call vectors.
2478 *
2479 *	Argument checking cleaned up. Saved 20% in size.
2480 *  This function doesn't need to set the kernel lock because
2481 *  it is set by the callees.
2482 */
2483
2484SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args)
2485{
2486	unsigned long a[6];
2487	unsigned long a0, a1;
2488	int err;
2489	unsigned int len;
2490
2491	if (call < 1 || call > SYS_SENDMMSG)
2492		return -EINVAL;
2493
2494	len = nargs[call];
2495	if (len > sizeof(a))
2496		return -EINVAL;
2497
2498	/* copy_from_user should be SMP safe. */
2499	if (copy_from_user(a, args, len))
2500		return -EFAULT;
2501
2502	audit_socketcall(nargs[call] / sizeof(unsigned long), a);
2503
2504	a0 = a[0];
2505	a1 = a[1];
2506
2507	switch (call) {
2508	case SYS_SOCKET:
2509		err = sys_socket(a0, a1, a[2]);
2510		break;
2511	case SYS_BIND:
2512		err = sys_bind(a0, (struct sockaddr __user *)a1, a[2]);
2513		break;
2514	case SYS_CONNECT:
2515		err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
2516		break;
2517	case SYS_LISTEN:
2518		err = sys_listen(a0, a1);
2519		break;
2520	case SYS_ACCEPT:
2521		err = sys_accept4(a0, (struct sockaddr __user *)a1,
2522				  (int __user *)a[2], 0);
2523		break;
2524	case SYS_GETSOCKNAME:
2525		err =
2526		    sys_getsockname(a0, (struct sockaddr __user *)a1,
2527				    (int __user *)a[2]);
2528		break;
2529	case SYS_GETPEERNAME:
2530		err =
2531		    sys_getpeername(a0, (struct sockaddr __user *)a1,
2532				    (int __user *)a[2]);
2533		break;
2534	case SYS_SOCKETPAIR:
2535		err = sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
2536		break;
2537	case SYS_SEND:
2538		err = sys_send(a0, (void __user *)a1, a[2], a[3]);
2539		break;
2540	case SYS_SENDTO:
2541		err = sys_sendto(a0, (void __user *)a1, a[2], a[3],
2542				 (struct sockaddr __user *)a[4], a[5]);
2543		break;
2544	case SYS_RECV:
2545		err = sys_recv(a0, (void __user *)a1, a[2], a[3]);
2546		break;
2547	case SYS_RECVFROM:
2548		err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
2549				   (struct sockaddr __user *)a[4],
2550				   (int __user *)a[5]);
2551		break;
2552	case SYS_SHUTDOWN:
2553		err = sys_shutdown(a0, a1);
2554		break;
2555	case SYS_SETSOCKOPT:
2556		err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]);
2557		break;
2558	case SYS_GETSOCKOPT:
2559		err =
2560		    sys_getsockopt(a0, a1, a[2], (char __user *)a[3],
2561				   (int __user *)a[4]);
2562		break;
2563	case SYS_SENDMSG:
2564		err = sys_sendmsg(a0, (struct msghdr __user *)a1, a[2]);
2565		break;
2566	case SYS_SENDMMSG:
2567		err = sys_sendmmsg(a0, (struct mmsghdr __user *)a1, a[2], a[3]);
2568		break;
2569	case SYS_RECVMSG:
2570		err = sys_recvmsg(a0, (struct msghdr __user *)a1, a[2]);
2571		break;
2572	case SYS_RECVMMSG:
2573		err = sys_recvmmsg(a0, (struct mmsghdr __user *)a1, a[2], a[3],
2574				   (struct timespec __user *)a[4]);
2575		break;
2576	case SYS_ACCEPT4:
2577		err = sys_accept4(a0, (struct sockaddr __user *)a1,
2578				  (int __user *)a[2], a[3]);
2579		break;
2580	default:
2581		err = -EINVAL;
2582		break;
2583	}
2584	return err;
2585}
2586
2587#endif				/* __ARCH_WANT_SYS_SOCKETCALL */
2588
2589/**
2590 *	sock_register - add a socket protocol handler
2591 *	@ops: description of protocol
2592 *
2593 *	This function is called by a protocol handler that wants to
2594 *	advertise its address family, and have it linked into the
2595 *	socket interface. The value ops->family coresponds to the
2596 *	socket system call protocol family.
2597 */
2598int sock_register(const struct net_proto_family *ops)
2599{
2600	int err;
2601
2602	if (ops->family >= NPROTO) {
2603		printk(KERN_CRIT "protocol %d >= NPROTO(%d)\n", ops->family,
2604		       NPROTO);
2605		return -ENOBUFS;
2606	}
2607
2608	spin_lock(&net_family_lock);
2609	if (rcu_dereference_protected(net_families[ops->family],
2610				      lockdep_is_held(&net_family_lock)))
2611		err = -EEXIST;
2612	else {
2613		rcu_assign_pointer(net_families[ops->family], ops);
2614		err = 0;
2615	}
2616	spin_unlock(&net_family_lock);
2617
2618	printk(KERN_INFO "NET: Registered protocol family %d\n", ops->family);
2619	return err;
2620}
2621EXPORT_SYMBOL(sock_register);
2622
2623/**
2624 *	sock_unregister - remove a protocol handler
2625 *	@family: protocol family to remove
2626 *
2627 *	This function is called by a protocol handler that wants to
2628 *	remove its address family, and have it unlinked from the
2629 *	new socket creation.
2630 *
2631 *	If protocol handler is a module, then it can use module reference
2632 *	counts to protect against new references. If protocol handler is not
2633 *	a module then it needs to provide its own protection in
2634 *	the ops->create routine.
2635 */
2636void sock_unregister(int family)
2637{
2638	BUG_ON(family < 0 || family >= NPROTO);
2639
2640	spin_lock(&net_family_lock);
2641	RCU_INIT_POINTER(net_families[family], NULL);
2642	spin_unlock(&net_family_lock);
2643
2644	synchronize_rcu();
2645
2646	printk(KERN_INFO "NET: Unregistered protocol family %d\n", family);
2647}
2648EXPORT_SYMBOL(sock_unregister);
2649
2650static int __init sock_init(void)
2651{
2652	int err;
2653	/*
2654	 *      Initialize the network sysctl infrastructure.
2655	 */
2656	err = net_sysctl_init();
2657	if (err)
2658		goto out;
2659
2660	/*
2661	 *      Initialize skbuff SLAB cache
2662	 */
2663	skb_init();
2664
2665	/*
2666	 *      Initialize the protocols module.
2667	 */
2668
2669	init_inodecache();
2670
2671	err = register_filesystem(&sock_fs_type);
2672	if (err)
2673		goto out_fs;
2674	sock_mnt = kern_mount(&sock_fs_type);
2675	if (IS_ERR(sock_mnt)) {
2676		err = PTR_ERR(sock_mnt);
2677		goto out_mount;
2678	}
2679
2680	/* The real protocol initialization is performed in later initcalls.
2681	 */
2682
2683#ifdef CONFIG_NETFILTER
2684	netfilter_init();
2685#endif
2686
2687#ifdef CONFIG_NETWORK_PHY_TIMESTAMPING
2688	skb_timestamping_init();
2689#endif
2690
2691out:
2692	return err;
2693
2694out_mount:
2695	unregister_filesystem(&sock_fs_type);
2696out_fs:
2697	goto out;
2698}
2699
2700core_initcall(sock_init);	/* early initcall */
2701
2702#ifdef CONFIG_PROC_FS
2703void socket_seq_show(struct seq_file *seq)
2704{
2705	int cpu;
2706	int counter = 0;
2707
2708	for_each_possible_cpu(cpu)
2709	    counter += per_cpu(sockets_in_use, cpu);
2710
2711	/* It can be negative, by the way. 8) */
2712	if (counter < 0)
2713		counter = 0;
2714
2715	seq_printf(seq, "sockets: used %d\n", counter);
2716}
2717#endif				/* CONFIG_PROC_FS */
2718
2719#ifdef CONFIG_COMPAT
2720static int do_siocgstamp(struct net *net, struct socket *sock,
2721			 unsigned int cmd, void __user *up)
2722{
2723	mm_segment_t old_fs = get_fs();
2724	struct timeval ktv;
2725	int err;
2726
2727	set_fs(KERNEL_DS);
2728	err = sock_do_ioctl(net, sock, cmd, (unsigned long)&ktv);
2729	set_fs(old_fs);
2730	if (!err)
2731		err = compat_put_timeval(&ktv, up);
2732
2733	return err;
2734}
2735
2736static int do_siocgstampns(struct net *net, struct socket *sock,
2737			   unsigned int cmd, void __user *up)
2738{
2739	mm_segment_t old_fs = get_fs();
2740	struct timespec kts;
2741	int err;
2742
2743	set_fs(KERNEL_DS);
2744	err = sock_do_ioctl(net, sock, cmd, (unsigned long)&kts);
2745	set_fs(old_fs);
2746	if (!err)
2747		err = compat_put_timespec(&kts, up);
2748
2749	return err;
2750}
2751
2752static int dev_ifname32(struct net *net, struct compat_ifreq __user *uifr32)
2753{
2754	struct ifreq __user *uifr;
2755	int err;
2756
2757	uifr = compat_alloc_user_space(sizeof(struct ifreq));
2758	if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
2759		return -EFAULT;
2760
2761	err = dev_ioctl(net, SIOCGIFNAME, uifr);
2762	if (err)
2763		return err;
2764
2765	if (copy_in_user(uifr32, uifr, sizeof(struct compat_ifreq)))
2766		return -EFAULT;
2767
2768	return 0;
2769}
2770
2771static int dev_ifconf(struct net *net, struct compat_ifconf __user *uifc32)
2772{
2773	struct compat_ifconf ifc32;
2774	struct ifconf ifc;
2775	struct ifconf __user *uifc;
2776	struct compat_ifreq __user *ifr32;
2777	struct ifreq __user *ifr;
2778	unsigned int i, j;
2779	int err;
2780
2781	if (copy_from_user(&ifc32, uifc32, sizeof(struct compat_ifconf)))
2782		return -EFAULT;
2783
2784	memset(&ifc, 0, sizeof(ifc));
2785	if (ifc32.ifcbuf == 0) {
2786		ifc32.ifc_len = 0;
2787		ifc.ifc_len = 0;
2788		ifc.ifc_req = NULL;
2789		uifc = compat_alloc_user_space(sizeof(struct ifconf));
2790	} else {
2791		size_t len = ((ifc32.ifc_len / sizeof(struct compat_ifreq)) + 1) *
2792			sizeof(struct ifreq);
2793		uifc = compat_alloc_user_space(sizeof(struct ifconf) + len);
2794		ifc.ifc_len = len;
2795		ifr = ifc.ifc_req = (void __user *)(uifc + 1);
2796		ifr32 = compat_ptr(ifc32.ifcbuf);
2797		for (i = 0; i < ifc32.ifc_len; i += sizeof(struct compat_ifreq)) {
2798			if (copy_in_user(ifr, ifr32, sizeof(struct compat_ifreq)))
2799				return -EFAULT;
2800			ifr++;
2801			ifr32++;
2802		}
2803	}
2804	if (copy_to_user(uifc, &ifc, sizeof(struct ifconf)))
2805		return -EFAULT;
2806
2807	err = dev_ioctl(net, SIOCGIFCONF, uifc);
2808	if (err)
2809		return err;
2810
2811	if (copy_from_user(&ifc, uifc, sizeof(struct ifconf)))
2812		return -EFAULT;
2813
2814	ifr = ifc.ifc_req;
2815	ifr32 = compat_ptr(ifc32.ifcbuf);
2816	for (i = 0, j = 0;
2817	     i + sizeof(struct compat_ifreq) <= ifc32.ifc_len && j < ifc.ifc_len;
2818	     i += sizeof(struct compat_ifreq), j += sizeof(struct ifreq)) {
2819		if (copy_in_user(ifr32, ifr, sizeof(struct compat_ifreq)))
2820			return -EFAULT;
2821		ifr32++;
2822		ifr++;
2823	}
2824
2825	if (ifc32.ifcbuf == 0) {
2826		/* Translate from 64-bit structure multiple to
2827		 * a 32-bit one.
2828		 */
2829		i = ifc.ifc_len;
2830		i = ((i / sizeof(struct ifreq)) * sizeof(struct compat_ifreq));
2831		ifc32.ifc_len = i;
2832	} else {
2833		ifc32.ifc_len = i;
2834	}
2835	if (copy_to_user(uifc32, &ifc32, sizeof(struct compat_ifconf)))
2836		return -EFAULT;
2837
2838	return 0;
2839}
2840
2841static int ethtool_ioctl(struct net *net, struct compat_ifreq __user *ifr32)
2842{
2843	struct compat_ethtool_rxnfc __user *compat_rxnfc;
2844	bool convert_in = false, convert_out = false;
2845	size_t buf_size = ALIGN(sizeof(struct ifreq), 8);
2846	struct ethtool_rxnfc __user *rxnfc;
2847	struct ifreq __user *ifr;
2848	u32 rule_cnt = 0, actual_rule_cnt;
2849	u32 ethcmd;
2850	u32 data;
2851	int ret;
2852
2853	if (get_user(data, &ifr32->ifr_ifru.ifru_data))
2854		return -EFAULT;
2855
2856	compat_rxnfc = compat_ptr(data);
2857
2858	if (get_user(ethcmd, &compat_rxnfc->cmd))
2859		return -EFAULT;
2860
2861	/* Most ethtool structures are defined without padding.
2862	 * Unfortunately struct ethtool_rxnfc is an exception.
2863	 */
2864	switch (ethcmd) {
2865	default:
2866		break;
2867	case ETHTOOL_GRXCLSRLALL:
2868		/* Buffer size is variable */
2869		if (get_user(rule_cnt, &compat_rxnfc->rule_cnt))
2870			return -EFAULT;
2871		if (rule_cnt > KMALLOC_MAX_SIZE / sizeof(u32))
2872			return -ENOMEM;
2873		buf_size += rule_cnt * sizeof(u32);
2874		/* fall through */
2875	case ETHTOOL_GRXRINGS:
2876	case ETHTOOL_GRXCLSRLCNT:
2877	case ETHTOOL_GRXCLSRULE:
2878	case ETHTOOL_SRXCLSRLINS:
2879		convert_out = true;
2880		/* fall through */
2881	case ETHTOOL_SRXCLSRLDEL:
2882		buf_size += sizeof(struct ethtool_rxnfc);
2883		convert_in = true;
2884		break;
2885	}
2886
2887	ifr = compat_alloc_user_space(buf_size);
2888	rxnfc = (void *)ifr + ALIGN(sizeof(struct ifreq), 8);
2889
2890	if (copy_in_user(&ifr->ifr_name, &ifr32->ifr_name, IFNAMSIZ))
2891		return -EFAULT;
2892
2893	if (put_user(convert_in ? rxnfc : compat_ptr(data),
2894		     &ifr->ifr_ifru.ifru_data))
2895		return -EFAULT;
2896
2897	if (convert_in) {
2898		/* We expect there to be holes between fs.m_ext and
2899		 * fs.ring_cookie and at the end of fs, but nowhere else.
2900		 */
2901		BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc, fs.m_ext) +
2902			     sizeof(compat_rxnfc->fs.m_ext) !=
2903			     offsetof(struct ethtool_rxnfc, fs.m_ext) +
2904			     sizeof(rxnfc->fs.m_ext));
2905		BUILD_BUG_ON(
2906			offsetof(struct compat_ethtool_rxnfc, fs.location) -
2907			offsetof(struct compat_ethtool_rxnfc, fs.ring_cookie) !=
2908			offsetof(struct ethtool_rxnfc, fs.location) -
2909			offsetof(struct ethtool_rxnfc, fs.ring_cookie));
2910
2911		if (copy_in_user(rxnfc, compat_rxnfc,
2912				 (void *)(&rxnfc->fs.m_ext + 1) -
2913				 (void *)rxnfc) ||
2914		    copy_in_user(&rxnfc->fs.ring_cookie,
2915				 &compat_rxnfc->fs.ring_cookie,
2916				 (void *)(&rxnfc->fs.location + 1) -
2917				 (void *)&rxnfc->fs.ring_cookie) ||
2918		    copy_in_user(&rxnfc->rule_cnt, &compat_rxnfc->rule_cnt,
2919				 sizeof(rxnfc->rule_cnt)))
2920			return -EFAULT;
2921	}
2922
2923	ret = dev_ioctl(net, SIOCETHTOOL, ifr);
2924	if (ret)
2925		return ret;
2926
2927	if (convert_out) {
2928		if (copy_in_user(compat_rxnfc, rxnfc,
2929				 (const void *)(&rxnfc->fs.m_ext + 1) -
2930				 (const void *)rxnfc) ||
2931		    copy_in_user(&compat_rxnfc->fs.ring_cookie,
2932				 &rxnfc->fs.ring_cookie,
2933				 (const void *)(&rxnfc->fs.location + 1) -
2934				 (const void *)&rxnfc->fs.ring_cookie) ||
2935		    copy_in_user(&compat_rxnfc->rule_cnt, &rxnfc->rule_cnt,
2936				 sizeof(rxnfc->rule_cnt)))
2937			return -EFAULT;
2938
2939		if (ethcmd == ETHTOOL_GRXCLSRLALL) {
2940			/* As an optimisation, we only copy the actual
2941			 * number of rules that the underlying
2942			 * function returned.  Since Mallory might
2943			 * change the rule count in user memory, we
2944			 * check that it is less than the rule count
2945			 * originally given (as the user buffer size),
2946			 * which has been range-checked.
2947			 */
2948			if (get_user(actual_rule_cnt, &rxnfc->rule_cnt))
2949				return -EFAULT;
2950			if (actual_rule_cnt < rule_cnt)
2951				rule_cnt = actual_rule_cnt;
2952			if (copy_in_user(&compat_rxnfc->rule_locs[0],
2953					 &rxnfc->rule_locs[0],
2954					 rule_cnt * sizeof(u32)))
2955				return -EFAULT;
2956		}
2957	}
2958
2959	return 0;
2960}
2961
2962static int compat_siocwandev(struct net *net, struct compat_ifreq __user *uifr32)
2963{
2964	void __user *uptr;
2965	compat_uptr_t uptr32;
2966	struct ifreq __user *uifr;
2967
2968	uifr = compat_alloc_user_space(sizeof(*uifr));
2969	if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
2970		return -EFAULT;
2971
2972	if (get_user(uptr32, &uifr32->ifr_settings.ifs_ifsu))
2973		return -EFAULT;
2974
2975	uptr = compat_ptr(uptr32);
2976
2977	if (put_user(uptr, &uifr->ifr_settings.ifs_ifsu.raw_hdlc))
2978		return -EFAULT;
2979
2980	return dev_ioctl(net, SIOCWANDEV, uifr);
2981}
2982
2983static int bond_ioctl(struct net *net, unsigned int cmd,
2984			 struct compat_ifreq __user *ifr32)
2985{
2986	struct ifreq kifr;
2987	struct ifreq __user *uifr;
2988	mm_segment_t old_fs;
2989	int err;
2990	u32 data;
2991	void __user *datap;
2992
2993	switch (cmd) {
2994	case SIOCBONDENSLAVE:
2995	case SIOCBONDRELEASE:
2996	case SIOCBONDSETHWADDR:
2997	case SIOCBONDCHANGEACTIVE:
2998		if (copy_from_user(&kifr, ifr32, sizeof(struct compat_ifreq)))
2999			return -EFAULT;
3000
3001		old_fs = get_fs();
3002		set_fs(KERNEL_DS);
3003		err = dev_ioctl(net, cmd,
3004				(struct ifreq __user __force *) &kifr);
3005		set_fs(old_fs);
3006
3007		return err;
3008	case SIOCBONDSLAVEINFOQUERY:
3009	case SIOCBONDINFOQUERY:
3010		uifr = compat_alloc_user_space(sizeof(*uifr));
3011		if (copy_in_user(&uifr->ifr_name, &ifr32->ifr_name, IFNAMSIZ))
3012			return -EFAULT;
3013
3014		if (get_user(data, &ifr32->ifr_ifru.ifru_data))
3015			return -EFAULT;
3016
3017		datap = compat_ptr(data);
3018		if (put_user(datap, &uifr->ifr_ifru.ifru_data))
3019			return -EFAULT;
3020
3021		return dev_ioctl(net, cmd, uifr);
3022	default:
3023		return -ENOIOCTLCMD;
3024	}
3025}
3026
3027static int siocdevprivate_ioctl(struct net *net, unsigned int cmd,
3028				 struct compat_ifreq __user *u_ifreq32)
3029{
3030	struct ifreq __user *u_ifreq64;
3031	char tmp_buf[IFNAMSIZ];
3032	void __user *data64;
3033	u32 data32;
3034
3035	if (copy_from_user(&tmp_buf[0], &(u_ifreq32->ifr_ifrn.ifrn_name[0]),
3036			   IFNAMSIZ))
3037		return -EFAULT;
3038	if (__get_user(data32, &u_ifreq32->ifr_ifru.ifru_data))
3039		return -EFAULT;
3040	data64 = compat_ptr(data32);
3041
3042	u_ifreq64 = compat_alloc_user_space(sizeof(*u_ifreq64));
3043
3044	/* Don't check these user accesses, just let that get trapped
3045	 * in the ioctl handler instead.
3046	 */
3047	if (copy_to_user(&u_ifreq64->ifr_ifrn.ifrn_name[0], &tmp_buf[0],
3048			 IFNAMSIZ))
3049		return -EFAULT;
3050	if (__put_user(data64, &u_ifreq64->ifr_ifru.ifru_data))
3051		return -EFAULT;
3052
3053	return dev_ioctl(net, cmd, u_ifreq64);
3054}
3055
3056static int dev_ifsioc(struct net *net, struct socket *sock,
3057			 unsigned int cmd, struct compat_ifreq __user *uifr32)
3058{
3059	struct ifreq __user *uifr;
3060	int err;
3061
3062	uifr = compat_alloc_user_space(sizeof(*uifr));
3063	if (copy_in_user(uifr, uifr32, sizeof(*uifr32)))
3064		return -EFAULT;
3065
3066	err = sock_do_ioctl(net, sock, cmd, (unsigned long)uifr);
3067
3068	if (!err) {
3069		switch (cmd) {
3070		case SIOCGIFFLAGS:
3071		case SIOCGIFMETRIC:
3072		case SIOCGIFMTU:
3073		case SIOCGIFMEM:
3074		case SIOCGIFHWADDR:
3075		case SIOCGIFINDEX:
3076		case SIOCGIFADDR:
3077		case SIOCGIFBRDADDR:
3078		case SIOCGIFDSTADDR:
3079		case SIOCGIFNETMASK:
3080		case SIOCGIFPFLAGS:
3081		case SIOCGIFTXQLEN:
3082		case SIOCGMIIPHY:
3083		case SIOCGMIIREG:
3084			if (copy_in_user(uifr32, uifr, sizeof(*uifr32)))
3085				err = -EFAULT;
3086			break;
3087		}
3088	}
3089	return err;
3090}
3091
3092static int compat_sioc_ifmap(struct net *net, unsigned int cmd,
3093			struct compat_ifreq __user *uifr32)
3094{
3095	struct ifreq ifr;
3096	struct compat_ifmap __user *uifmap32;
3097	mm_segment_t old_fs;
3098	int err;
3099
3100	uifmap32 = &uifr32->ifr_ifru.ifru_map;
3101	err = copy_from_user(&ifr, uifr32, sizeof(ifr.ifr_name));
3102	err |= __get_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
3103	err |= __get_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
3104	err |= __get_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
3105	err |= __get_user(ifr.ifr_map.irq, &uifmap32->irq);
3106	err |= __get_user(ifr.ifr_map.dma, &uifmap32->dma);
3107	err |= __get_user(ifr.ifr_map.port, &uifmap32->port);
3108	if (err)
3109		return -EFAULT;
3110
3111	old_fs = get_fs();
3112	set_fs(KERNEL_DS);
3113	err = dev_ioctl(net, cmd, (void  __user __force *)&ifr);
3114	set_fs(old_fs);
3115
3116	if (cmd == SIOCGIFMAP && !err) {
3117		err = copy_to_user(uifr32, &ifr, sizeof(ifr.ifr_name));
3118		err |= __put_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
3119		err |= __put_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
3120		err |= __put_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
3121		err |= __put_user(ifr.ifr_map.irq, &uifmap32->irq);
3122		err |= __put_user(ifr.ifr_map.dma, &uifmap32->dma);
3123		err |= __put_user(ifr.ifr_map.port, &uifmap32->port);
3124		if (err)
3125			err = -EFAULT;
3126	}
3127	return err;
3128}
3129
3130static int compat_siocshwtstamp(struct net *net, struct compat_ifreq __user *uifr32)
3131{
3132	void __user *uptr;
3133	compat_uptr_t uptr32;
3134	struct ifreq __user *uifr;
3135
3136	uifr = compat_alloc_user_space(sizeof(*uifr));
3137	if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
3138		return -EFAULT;
3139
3140	if (get_user(uptr32, &uifr32->ifr_data))
3141		return -EFAULT;
3142
3143	uptr = compat_ptr(uptr32);
3144
3145	if (put_user(uptr, &uifr->ifr_data))
3146		return -EFAULT;
3147
3148	return dev_ioctl(net, SIOCSHWTSTAMP, uifr);
3149}
3150
3151struct rtentry32 {
3152	u32		rt_pad1;
3153	struct sockaddr rt_dst;         /* target address               */
3154	struct sockaddr rt_gateway;     /* gateway addr (RTF_GATEWAY)   */
3155	struct sockaddr rt_genmask;     /* target network mask (IP)     */
3156	unsigned short	rt_flags;
3157	short		rt_pad2;
3158	u32		rt_pad3;
3159	unsigned char	rt_tos;
3160	unsigned char	rt_class;
3161	short		rt_pad4;
3162	short		rt_metric;      /* +1 for binary compatibility! */
3163	/* char * */ u32 rt_dev;        /* forcing the device at add    */
3164	u32		rt_mtu;         /* per route MTU/Window         */
3165	u32		rt_window;      /* Window clamping              */
3166	unsigned short  rt_irtt;        /* Initial RTT                  */
3167};
3168
3169struct in6_rtmsg32 {
3170	struct in6_addr		rtmsg_dst;
3171	struct in6_addr		rtmsg_src;
3172	struct in6_addr		rtmsg_gateway;
3173	u32			rtmsg_type;
3174	u16			rtmsg_dst_len;
3175	u16			rtmsg_src_len;
3176	u32			rtmsg_metric;
3177	u32			rtmsg_info;
3178	u32			rtmsg_flags;
3179	s32			rtmsg_ifindex;
3180};
3181
3182static int routing_ioctl(struct net *net, struct socket *sock,
3183			 unsigned int cmd, void __user *argp)
3184{
3185	int ret;
3186	void *r = NULL;
3187	struct in6_rtmsg r6;
3188	struct rtentry r4;
3189	char devname[16];
3190	u32 rtdev;
3191	mm_segment_t old_fs = get_fs();
3192
3193	if (sock && sock->sk && sock->sk->sk_family == AF_INET6) { /* ipv6 */
3194		struct in6_rtmsg32 __user *ur6 = argp;
3195		ret = copy_from_user(&r6.rtmsg_dst, &(ur6->rtmsg_dst),
3196			3 * sizeof(struct in6_addr));
3197		ret |= __get_user(r6.rtmsg_type, &(ur6->rtmsg_type));
3198		ret |= __get_user(r6.rtmsg_dst_len, &(ur6->rtmsg_dst_len));
3199		ret |= __get_user(r6.rtmsg_src_len, &(ur6->rtmsg_src_len));
3200		ret |= __get_user(r6.rtmsg_metric, &(ur6->rtmsg_metric));
3201		ret |= __get_user(r6.rtmsg_info, &(ur6->rtmsg_info));
3202		ret |= __get_user(r6.rtmsg_flags, &(ur6->rtmsg_flags));
3203		ret |= __get_user(r6.rtmsg_ifindex, &(ur6->rtmsg_ifindex));
3204
3205		r = (void *) &r6;
3206	} else { /* ipv4 */
3207		struct rtentry32 __user *ur4 = argp;
3208		ret = copy_from_user(&r4.rt_dst, &(ur4->rt_dst),
3209					3 * sizeof(struct sockaddr));
3210		ret |= __get_user(r4.rt_flags, &(ur4->rt_flags));
3211		ret |= __get_user(r4.rt_metric, &(ur4->rt_metric));
3212		ret |= __get_user(r4.rt_mtu, &(ur4->rt_mtu));
3213		ret |= __get_user(r4.rt_window, &(ur4->rt_window));
3214		ret |= __get_user(r4.rt_irtt, &(ur4->rt_irtt));
3215		ret |= __get_user(rtdev, &(ur4->rt_dev));
3216		if (rtdev) {
3217			ret |= copy_from_user(devname, compat_ptr(rtdev), 15);
3218			r4.rt_dev = (char __user __force *)devname;
3219			devname[15] = 0;
3220		} else
3221			r4.rt_dev = NULL;
3222
3223		r = (void *) &r4;
3224	}
3225
3226	if (ret) {
3227		ret = -EFAULT;
3228		goto out;
3229	}
3230
3231	set_fs(KERNEL_DS);
3232	ret = sock_do_ioctl(net, sock, cmd, (unsigned long) r);
3233	set_fs(old_fs);
3234
3235out:
3236	return ret;
3237}
3238
3239/* Since old style bridge ioctl's endup using SIOCDEVPRIVATE
3240 * for some operations; this forces use of the newer bridge-utils that
3241 * use compatible ioctls
3242 */
3243static int old_bridge_ioctl(compat_ulong_t __user *argp)
3244{
3245	compat_ulong_t tmp;
3246
3247	if (get_user(tmp, argp))
3248		return -EFAULT;
3249	if (tmp == BRCTL_GET_VERSION)
3250		return BRCTL_VERSION + 1;
3251	return -EINVAL;
3252}
3253
3254static int compat_sock_ioctl_trans(struct file *file, struct socket *sock,
3255			 unsigned int cmd, unsigned long arg)
3256{
3257	void __user *argp = compat_ptr(arg);
3258	struct sock *sk = sock->sk;
3259	struct net *net = sock_net(sk);
3260
3261	if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15))
3262		return siocdevprivate_ioctl(net, cmd, argp);
3263
3264	switch (cmd) {
3265	case SIOCSIFBR:
3266	case SIOCGIFBR:
3267		return old_bridge_ioctl(argp);
3268	case SIOCGIFNAME:
3269		return dev_ifname32(net, argp);
3270	case SIOCGIFCONF:
3271		return dev_ifconf(net, argp);
3272	case SIOCETHTOOL:
3273		return ethtool_ioctl(net, argp);
3274	case SIOCWANDEV:
3275		return compat_siocwandev(net, argp);
3276	case SIOCGIFMAP:
3277	case SIOCSIFMAP:
3278		return compat_sioc_ifmap(net, cmd, argp);
3279	case SIOCBONDENSLAVE:
3280	case SIOCBONDRELEASE:
3281	case SIOCBONDSETHWADDR:
3282	case SIOCBONDSLAVEINFOQUERY:
3283	case SIOCBONDINFOQUERY:
3284	case SIOCBONDCHANGEACTIVE:
3285		return bond_ioctl(net, cmd, argp);
3286	case SIOCADDRT:
3287	case SIOCDELRT:
3288		return routing_ioctl(net, sock, cmd, argp);
3289	case SIOCGSTAMP:
3290		return do_siocgstamp(net, sock, cmd, argp);
3291	case SIOCGSTAMPNS:
3292		return do_siocgstampns(net, sock, cmd, argp);
3293	case SIOCSHWTSTAMP:
3294		return compat_siocshwtstamp(net, argp);
3295
3296	case FIOSETOWN:
3297	case SIOCSPGRP:
3298	case FIOGETOWN:
3299	case SIOCGPGRP:
3300	case SIOCBRADDBR:
3301	case SIOCBRDELBR:
3302	case SIOCGIFVLAN:
3303	case SIOCSIFVLAN:
3304	case SIOCADDDLCI:
3305	case SIOCDELDLCI:
3306		return sock_ioctl(file, cmd, arg);
3307
3308	case SIOCGIFFLAGS:
3309	case SIOCSIFFLAGS:
3310	case SIOCGIFMETRIC:
3311	case SIOCSIFMETRIC:
3312	case SIOCGIFMTU:
3313	case SIOCSIFMTU:
3314	case SIOCGIFMEM:
3315	case SIOCSIFMEM:
3316	case SIOCGIFHWADDR:
3317	case SIOCSIFHWADDR:
3318	case SIOCADDMULTI:
3319	case SIOCDELMULTI:
3320	case SIOCGIFINDEX:
3321	case SIOCGIFADDR:
3322	case SIOCSIFADDR:
3323	case SIOCSIFHWBROADCAST:
3324	case SIOCDIFADDR:
3325	case SIOCGIFBRDADDR:
3326	case SIOCSIFBRDADDR:
3327	case SIOCGIFDSTADDR:
3328	case SIOCSIFDSTADDR:
3329	case SIOCGIFNETMASK:
3330	case SIOCSIFNETMASK:
3331	case SIOCSIFPFLAGS:
3332	case SIOCGIFPFLAGS:
3333	case SIOCGIFTXQLEN:
3334	case SIOCSIFTXQLEN:
3335	case SIOCBRADDIF:
3336	case SIOCBRDELIF:
3337	case SIOCSIFNAME:
3338	case SIOCGMIIPHY:
3339	case SIOCGMIIREG:
3340	case SIOCSMIIREG:
3341		return dev_ifsioc(net, sock, cmd, argp);
3342
3343	case SIOCSARP:
3344	case SIOCGARP:
3345	case SIOCDARP:
3346	case SIOCATMARK:
3347		return sock_do_ioctl(net, sock, cmd, arg);
3348	}
3349
3350	return -ENOIOCTLCMD;
3351}
3352
3353static long compat_sock_ioctl(struct file *file, unsigned int cmd,
3354			      unsigned long arg)
3355{
3356	struct socket *sock = file->private_data;
3357	int ret = -ENOIOCTLCMD;
3358	struct sock *sk;
3359	struct net *net;
3360
3361	sk = sock->sk;
3362	net = sock_net(sk);
3363
3364	if (sock->ops->compat_ioctl)
3365		ret = sock->ops->compat_ioctl(sock, cmd, arg);
3366
3367	if (ret == -ENOIOCTLCMD &&
3368	    (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST))
3369		ret = compat_wext_handle_ioctl(net, cmd, arg);
3370
3371	if (ret == -ENOIOCTLCMD)
3372		ret = compat_sock_ioctl_trans(file, sock, cmd, arg);
3373
3374	return ret;
3375}
3376#endif
3377
3378int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen)
3379{
3380	return sock->ops->bind(sock, addr, addrlen);
3381}
3382EXPORT_SYMBOL(kernel_bind);
3383
3384int kernel_listen(struct socket *sock, int backlog)
3385{
3386	return sock->ops->listen(sock, backlog);
3387}
3388EXPORT_SYMBOL(kernel_listen);
3389
3390int kernel_accept(struct socket *sock, struct socket **newsock, int flags)
3391{
3392	struct sock *sk = sock->sk;
3393	int err;
3394
3395	err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
3396			       newsock);
3397	if (err < 0)
3398		goto done;
3399
3400	err = sock->ops->accept(sock, *newsock, flags);
3401	if (err < 0) {
3402		sock_release(*newsock);
3403		*newsock = NULL;
3404		goto done;
3405	}
3406
3407	(*newsock)->ops = sock->ops;
3408	__module_get((*newsock)->ops->owner);
3409
3410done:
3411	return err;
3412}
3413EXPORT_SYMBOL(kernel_accept);
3414
3415int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
3416		   int flags)
3417{
3418	return sock->ops->connect(sock, addr, addrlen, flags);
3419}
3420EXPORT_SYMBOL(kernel_connect);
3421
3422int kernel_getsockname(struct socket *sock, struct sockaddr *addr,
3423			 int *addrlen)
3424{
3425	return sock->ops->getname(sock, addr, addrlen, 0);
3426}
3427EXPORT_SYMBOL(kernel_getsockname);
3428
3429int kernel_getpeername(struct socket *sock, struct sockaddr *addr,
3430			 int *addrlen)
3431{
3432	return sock->ops->getname(sock, addr, addrlen, 1);
3433}
3434EXPORT_SYMBOL(kernel_getpeername);
3435
3436int kernel_getsockopt(struct socket *sock, int level, int optname,
3437			char *optval, int *optlen)
3438{
3439	mm_segment_t oldfs = get_fs();
3440	char __user *uoptval;
3441	int __user *uoptlen;
3442	int err;
3443
3444	uoptval = (char __user __force *) optval;
3445	uoptlen = (int __user __force *) optlen;
3446
3447	set_fs(KERNEL_DS);
3448	if (level == SOL_SOCKET)
3449		err = sock_getsockopt(sock, level, optname, uoptval, uoptlen);
3450	else
3451		err = sock->ops->getsockopt(sock, level, optname, uoptval,
3452					    uoptlen);
3453	set_fs(oldfs);
3454	return err;
3455}
3456EXPORT_SYMBOL(kernel_getsockopt);
3457
3458int kernel_setsockopt(struct socket *sock, int level, int optname,
3459			char *optval, unsigned int optlen)
3460{
3461	mm_segment_t oldfs = get_fs();
3462	char __user *uoptval;
3463	int err;
3464
3465	uoptval = (char __user __force *) optval;
3466
3467	set_fs(KERNEL_DS);
3468	if (level == SOL_SOCKET)
3469		err = sock_setsockopt(sock, level, optname, uoptval, optlen);
3470	else
3471		err = sock->ops->setsockopt(sock, level, optname, uoptval,
3472					    optlen);
3473	set_fs(oldfs);
3474	return err;
3475}
3476EXPORT_SYMBOL(kernel_setsockopt);
3477
3478int kernel_sendpage(struct socket *sock, struct page *page, int offset,
3479		    size_t size, int flags)
3480{
3481	if (sock->ops->sendpage)
3482		return sock->ops->sendpage(sock, page, offset, size, flags);
3483
3484	return sock_no_sendpage(sock, page, offset, size, flags);
3485}
3486EXPORT_SYMBOL(kernel_sendpage);
3487
3488int kernel_sock_ioctl(struct socket *sock, int cmd, unsigned long arg)
3489{
3490	mm_segment_t oldfs = get_fs();
3491	int err;
3492
3493	set_fs(KERNEL_DS);
3494	err = sock->ops->ioctl(sock, cmd, arg);
3495	set_fs(oldfs);
3496
3497	return err;
3498}
3499EXPORT_SYMBOL(kernel_sock_ioctl);
3500
3501int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how)
3502{
3503	return sock->ops->shutdown(sock, how);
3504}
3505EXPORT_SYMBOL(kernel_sock_shutdown);