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