blob: 3e33959f3ce5711863fca7ce1805a4d1cf4ed6e0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Jesper Juhl02c30a82005-05-05 16:16:16 -07007 * Ross Biro
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * 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)
Stephen Hemminger89bddce2006-09-01 00:19:31 -070045 * Tigran Aivazian : Made listen(2) backlog sanity checks
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 * 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
Stephen Hemminger89bddce2006-09-01 00:19:31 -070056 * paradigm.
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 *
58 * Based upon Swansea University Computer Society NET3.039
59 */
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <linux/socket.h>
63#include <linux/file.h>
64#include <linux/net.h>
65#include <linux/interrupt.h>
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -070066#include <linux/thread_info.h>
Stephen Hemminger55737fd2006-09-01 00:23:39 -070067#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/netdevice.h>
69#include <linux/proc_fs.h>
70#include <linux/seq_file.h>
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080071#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include <linux/if_bridge.h>
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -030073#include <linux/if_frad.h>
74#include <linux/if_vlan.h>
Daniel Borkmann408eccc2014-04-01 16:20:23 +020075#include <linux/ptp_classify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#include <linux/init.h>
77#include <linux/poll.h>
78#include <linux/cache.h>
79#include <linux/module.h>
80#include <linux/highmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#include <linux/mount.h>
82#include <linux/security.h>
83#include <linux/syscalls.h>
84#include <linux/compat.h>
85#include <linux/kmod.h>
David Woodhouse3ec3b2f2005-05-17 12:08:48 +010086#include <linux/audit.h>
Adrian Bunkd86b5e02006-01-21 00:46:55 +010087#include <linux/wireless.h>
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -070088#include <linux/nsproxy.h>
Nick Black1fd7317d2009-09-22 16:43:33 -070089#include <linux/magic.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090090#include <linux/slab.h>
Masatake YAMATO600e1772012-08-29 10:44:29 +000091#include <linux/xattr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93#include <asm/uaccess.h>
94#include <asm/unistd.h>
95
96#include <net/compat.h>
David S. Miller87de87d2008-06-03 09:14:03 -070097#include <net/wext.h>
Herbert Xuf8451722010-05-24 00:12:34 -070098#include <net/cls_cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100#include <net/sock.h>
101#include <linux/netfilter.h>
102
Arnd Bergmann6b960182009-11-06 23:10:54 -0800103#include <linux/if_tun.h>
104#include <linux/ipv6_route.h>
105#include <linux/route.h>
Arnd Bergmann6b960182009-11-06 23:10:54 -0800106#include <linux/sockios.h>
107#include <linux/atalk.h>
Eliezer Tamir076bb0c2013-07-10 17:13:17 +0300108#include <net/busy_poll.h>
Willem de Bruijnf24b9be2014-08-04 22:11:45 -0400109#include <linux/errqueue.h>
Eliezer Tamir06021292013-06-10 11:39:50 +0300110
Cong Wange0d10952013-08-01 11:10:25 +0800111#ifdef CONFIG_NET_RX_BUSY_POLL
Eliezer Tamir64b0dc52013-07-10 17:13:36 +0300112unsigned int sysctl_net_busy_read __read_mostly;
113unsigned int sysctl_net_busy_poll __read_mostly;
Eliezer Tamir06021292013-06-10 11:39:50 +0300114#endif
Arnd Bergmann6b960182009-11-06 23:10:54 -0800115
Al Viro8ae5e032014-11-28 19:40:50 -0500116static ssize_t sock_read_iter(struct kiocb *iocb, struct iov_iter *to);
117static ssize_t sock_write_iter(struct kiocb *iocb, struct iov_iter *from);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700118static int sock_mmap(struct file *file, struct vm_area_struct *vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120static int sock_close(struct inode *inode, struct file *file);
121static unsigned int sock_poll(struct file *file,
122 struct poll_table_struct *wait);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700123static long sock_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
Shaun Pereira89bbfc92006-03-21 23:58:08 -0800124#ifdef CONFIG_COMPAT
125static long compat_sock_ioctl(struct file *file,
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700126 unsigned int cmd, unsigned long arg);
Shaun Pereira89bbfc92006-03-21 23:58:08 -0800127#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128static int sock_fasync(int fd, struct file *filp, int on);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129static ssize_t sock_sendpage(struct file *file, struct page *page,
130 int offset, size_t size, loff_t *ppos, int more);
Jens Axboe9c55e012007-11-06 23:30:13 -0800131static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700132 struct pipe_inode_info *pipe, size_t len,
Jens Axboe9c55e012007-11-06 23:30:13 -0800133 unsigned int flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135/*
136 * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
137 * in the operation structures but are done directly via the socketcall() multiplexor.
138 */
139
Arjan van de Venda7071d2007-02-12 00:55:36 -0800140static const struct file_operations socket_file_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 .owner = THIS_MODULE,
142 .llseek = no_llseek,
Al Viro8ae5e032014-11-28 19:40:50 -0500143 .read_iter = sock_read_iter,
144 .write_iter = sock_write_iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 .poll = sock_poll,
146 .unlocked_ioctl = sock_ioctl,
Shaun Pereira89bbfc92006-03-21 23:58:08 -0800147#ifdef CONFIG_COMPAT
148 .compat_ioctl = compat_sock_ioctl,
149#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 .mmap = sock_mmap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 .release = sock_close,
152 .fasync = sock_fasync,
Jens Axboe5274f052006-03-30 15:15:30 +0200153 .sendpage = sock_sendpage,
154 .splice_write = generic_splice_sendpage,
Jens Axboe9c55e012007-11-06 23:30:13 -0800155 .splice_read = sock_splice_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156};
157
158/*
159 * The protocol list. Each protocol is registered in here.
160 */
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162static DEFINE_SPINLOCK(net_family_lock);
Eric Dumazet190683a2010-11-10 10:50:44 +0000163static const struct net_proto_family __rcu *net_families[NPROTO] __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165/*
166 * Statistics counters of the socket lists
167 */
168
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700169static DEFINE_PER_CPU(int, sockets_in_use);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700172 * Support routines.
173 * Move socket addresses back and forth across the kernel/user
174 * divide and look after the messy bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 */
176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177/**
178 * move_addr_to_kernel - copy a socket address into kernel space
179 * @uaddr: Address in user space
180 * @kaddr: Address in kernel space
181 * @ulen: Length in user space
182 *
183 * The address is copied into kernel space. If the provided address is
184 * too long an error code of -EINVAL is returned. If the copy gives
185 * invalid addresses -EFAULT is returned. On a success 0 is returned.
186 */
187
Maciej Żenczykowski43db3622012-03-11 12:51:50 +0000188int move_addr_to_kernel(void __user *uaddr, int ulen, struct sockaddr_storage *kaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -0700190 if (ulen < 0 || ulen > sizeof(struct sockaddr_storage))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700192 if (ulen == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 return 0;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700194 if (copy_from_user(kaddr, uaddr, ulen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 return -EFAULT;
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100196 return audit_sockaddr(ulen, kaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197}
198
199/**
200 * move_addr_to_user - copy an address to user space
201 * @kaddr: kernel space address
202 * @klen: length of address in kernel
203 * @uaddr: user space address
204 * @ulen: pointer to user length field
205 *
206 * The value pointed to by ulen on entry is the buffer length available.
207 * This is overwritten with the buffer space used. -EINVAL is returned
208 * if an overlong buffer is specified or a negative buffer size. -EFAULT
209 * is returned if either the buffer or the length field are not
210 * accessible.
211 * After copying the data up to the limit the user specifies, the true
212 * length of the data is written over the length limit the user
213 * specified. Zero is returned for a success.
214 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700215
Maciej Żenczykowski43db3622012-03-11 12:51:50 +0000216static int move_addr_to_user(struct sockaddr_storage *kaddr, int klen,
stephen hemminger11165f12010-10-18 14:27:29 +0000217 void __user *uaddr, int __user *ulen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
219 int err;
220 int len;
221
Hannes Frederic Sowa68c6beb2013-11-21 03:14:34 +0100222 BUG_ON(klen > sizeof(struct sockaddr_storage));
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700223 err = get_user(len, ulen);
224 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 return err;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700226 if (len > klen)
227 len = klen;
Hannes Frederic Sowa68c6beb2013-11-21 03:14:34 +0100228 if (len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700230 if (len) {
Steve Grubbd6fe3942006-03-30 12:20:22 -0500231 if (audit_sockaddr(klen, kaddr))
232 return -ENOMEM;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700233 if (copy_to_user(uaddr, kaddr, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 return -EFAULT;
235 }
236 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700237 * "fromlen shall refer to the value before truncation.."
238 * 1003.1g
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 */
240 return __put_user(klen, ulen);
241}
242
Christoph Lametere18b8902006-12-06 20:33:20 -0800243static struct kmem_cache *sock_inode_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245static struct inode *sock_alloc_inode(struct super_block *sb)
246{
247 struct socket_alloc *ei;
Eric Dumazeteaefd112011-02-18 03:26:36 +0000248 struct socket_wq *wq;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700249
Christoph Lametere94b1762006-12-06 20:33:17 -0800250 ei = kmem_cache_alloc(sock_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 if (!ei)
252 return NULL;
Eric Dumazeteaefd112011-02-18 03:26:36 +0000253 wq = kmalloc(sizeof(*wq), GFP_KERNEL);
254 if (!wq) {
Eric Dumazet43815482010-04-29 11:01:49 +0000255 kmem_cache_free(sock_inode_cachep, ei);
256 return NULL;
257 }
Eric Dumazeteaefd112011-02-18 03:26:36 +0000258 init_waitqueue_head(&wq->wait);
259 wq->fasync_list = NULL;
260 RCU_INIT_POINTER(ei->socket.wq, wq);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 ei->socket.state = SS_UNCONNECTED;
263 ei->socket.flags = 0;
264 ei->socket.ops = NULL;
265 ei->socket.sk = NULL;
266 ei->socket.file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 return &ei->vfs_inode;
269}
270
271static void sock_destroy_inode(struct inode *inode)
272{
Eric Dumazet43815482010-04-29 11:01:49 +0000273 struct socket_alloc *ei;
Eric Dumazeteaefd112011-02-18 03:26:36 +0000274 struct socket_wq *wq;
Eric Dumazet43815482010-04-29 11:01:49 +0000275
276 ei = container_of(inode, struct socket_alloc, vfs_inode);
Eric Dumazeteaefd112011-02-18 03:26:36 +0000277 wq = rcu_dereference_protected(ei->socket.wq, 1);
Lai Jiangshan61845222011-03-18 12:10:25 +0800278 kfree_rcu(wq, rcu);
Eric Dumazet43815482010-04-29 11:01:49 +0000279 kmem_cache_free(sock_inode_cachep, ei);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
281
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700282static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700284 struct socket_alloc *ei = (struct socket_alloc *)foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Christoph Lametera35afb82007-05-16 22:10:57 -0700286 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289static int init_inodecache(void)
290{
291 sock_inode_cachep = kmem_cache_create("sock_inode_cache",
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700292 sizeof(struct socket_alloc),
293 0,
294 (SLAB_HWCACHE_ALIGN |
295 SLAB_RECLAIM_ACCOUNT |
296 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +0900297 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 if (sock_inode_cachep == NULL)
299 return -ENOMEM;
300 return 0;
301}
302
Alexey Dobriyanb87221d2009-09-21 17:01:09 -0700303static const struct super_operations sockfs_ops = {
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700304 .alloc_inode = sock_alloc_inode,
305 .destroy_inode = sock_destroy_inode,
306 .statfs = simple_statfs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307};
308
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700309/*
310 * sockfs_dname() is called from d_path().
311 */
312static char *sockfs_dname(struct dentry *dentry, char *buffer, int buflen)
313{
314 return dynamic_dname(dentry, buffer, buflen, "socket:[%lu]",
315 dentry->d_inode->i_ino);
316}
317
Al Viro3ba13d12009-02-20 06:02:22 +0000318static const struct dentry_operations sockfs_dentry_operations = {
Eric Dumazetc23fbb62007-05-08 00:26:18 -0700319 .d_dname = sockfs_dname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320};
321
Al Viroc74a1cb2011-01-12 16:59:34 -0500322static struct dentry *sockfs_mount(struct file_system_type *fs_type,
323 int flags, const char *dev_name, void *data)
324{
325 return mount_pseudo(fs_type, "socket:", &sockfs_ops,
326 &sockfs_dentry_operations, SOCKFS_MAGIC);
327}
328
329static struct vfsmount *sock_mnt __read_mostly;
330
331static struct file_system_type sock_fs_type = {
332 .name = "sockfs",
333 .mount = sockfs_mount,
334 .kill_sb = kill_anon_super,
335};
336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337/*
338 * Obtains the first available file descriptor and sets it up for use.
339 *
David S. Miller39d8c1b2006-03-20 17:13:49 -0800340 * These functions create file structures and maps them to fd space
341 * of the current process. On success it returns file descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 * and file struct implicitly stored in sock->file.
343 * Note that another thread may close file descriptor before we return
344 * from this function. We use the fact that now we do not refer
345 * to socket after mapping. If one day we will need it, this
346 * function will increment ref. count on file by 1.
347 *
348 * In any case returned fd MAY BE not valid!
349 * This race condition is unavoidable
350 * with shared fd spaces, we cannot solve it inside kernel,
351 * but we take care of internal coherence yet.
352 */
353
Linus Torvaldsaab174f2012-10-02 20:25:04 -0700354struct file *sock_alloc_file(struct socket *sock, int flags, const char *dname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
Al Viro7cbe66b2009-08-05 19:59:08 +0400356 struct qstr name = { .name = "" };
Al Viro2c48b9c2009-08-09 00:52:35 +0400357 struct path path;
Al Viro7cbe66b2009-08-05 19:59:08 +0400358 struct file *file;
David S. Miller39d8c1b2006-03-20 17:13:49 -0800359
Masatake YAMATO600e1772012-08-29 10:44:29 +0000360 if (dname) {
361 name.name = dname;
362 name.len = strlen(name.name);
363 } else if (sock->sk) {
364 name.name = sock->sk->sk_prot_creator->name;
365 name.len = strlen(name.name);
366 }
Nick Piggin4b936882011-01-07 17:50:07 +1100367 path.dentry = d_alloc_pseudo(sock_mnt->mnt_sb, &name);
Al Viro28407632012-08-17 23:54:15 -0400368 if (unlikely(!path.dentry))
369 return ERR_PTR(-ENOMEM);
Al Viro2c48b9c2009-08-09 00:52:35 +0400370 path.mnt = mntget(sock_mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Al Viro2c48b9c2009-08-09 00:52:35 +0400372 d_instantiate(path.dentry, SOCK_INODE(sock));
Al Virocc3808f2009-08-06 09:43:59 +0400373
Al Viro2c48b9c2009-08-09 00:52:35 +0400374 file = alloc_file(&path, FMODE_READ | FMODE_WRITE,
Al Virocc3808f2009-08-06 09:43:59 +0400375 &socket_file_ops);
Anatol Pomozov39b65252012-09-12 20:11:55 -0700376 if (unlikely(IS_ERR(file))) {
Al Virocc3808f2009-08-06 09:43:59 +0400377 /* drop dentry, keep inode */
Al Viro7de9c6ee2010-10-23 11:11:40 -0400378 ihold(path.dentry->d_inode);
Al Viro2c48b9c2009-08-09 00:52:35 +0400379 path_put(&path);
Anatol Pomozov39b65252012-09-12 20:11:55 -0700380 return file;
Al Virocc3808f2009-08-06 09:43:59 +0400381 }
David S. Miller39d8c1b2006-03-20 17:13:49 -0800382
383 sock->file = file;
Ulrich Drepper77d27202008-07-23 21:29:35 -0700384 file->f_flags = O_RDWR | (flags & O_NONBLOCK);
David S. Miller39d8c1b2006-03-20 17:13:49 -0800385 file->private_data = sock;
Al Viro28407632012-08-17 23:54:15 -0400386 return file;
David S. Miller39d8c1b2006-03-20 17:13:49 -0800387}
Al Viro56b31d12012-08-18 00:25:51 -0400388EXPORT_SYMBOL(sock_alloc_file);
David S. Miller39d8c1b2006-03-20 17:13:49 -0800389
Al Viro56b31d12012-08-18 00:25:51 -0400390static int sock_map_fd(struct socket *sock, int flags)
David S. Miller39d8c1b2006-03-20 17:13:49 -0800391{
392 struct file *newfile;
Al Viro28407632012-08-17 23:54:15 -0400393 int fd = get_unused_fd_flags(flags);
394 if (unlikely(fd < 0))
395 return fd;
David S. Miller39d8c1b2006-03-20 17:13:49 -0800396
Linus Torvaldsaab174f2012-10-02 20:25:04 -0700397 newfile = sock_alloc_file(sock, flags, NULL);
Al Viro28407632012-08-17 23:54:15 -0400398 if (likely(!IS_ERR(newfile))) {
David S. Miller39d8c1b2006-03-20 17:13:49 -0800399 fd_install(fd, newfile);
Al Viro28407632012-08-17 23:54:15 -0400400 return fd;
401 }
Al Viro7cbe66b2009-08-05 19:59:08 +0400402
Al Viro28407632012-08-17 23:54:15 -0400403 put_unused_fd(fd);
404 return PTR_ERR(newfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
John Fastabend406a3c62012-07-20 10:39:25 +0000407struct socket *sock_from_file(struct file *file, int *err)
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800408{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800409 if (file->f_op == &socket_file_ops)
410 return file->private_data; /* set in sock_map_fd */
411
Eric Dumazet23bb80d2007-02-08 14:59:57 -0800412 *err = -ENOTSOCK;
413 return NULL;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800414}
John Fastabend406a3c62012-07-20 10:39:25 +0000415EXPORT_SYMBOL(sock_from_file);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417/**
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700418 * sockfd_lookup - Go from a file number to its socket slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 * @fd: file handle
420 * @err: pointer to an error code return
421 *
422 * The file handle passed in is locked and the socket it is bound
423 * too is returned. If an error occurs the err pointer is overwritten
424 * with a negative errno code and NULL is returned. The function checks
425 * for both invalid handles and passing a handle which is not a socket.
426 *
427 * On a success the socket object pointer is returned.
428 */
429
430struct socket *sockfd_lookup(int fd, int *err)
431{
432 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 struct socket *sock;
434
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700435 file = fget(fd);
436 if (!file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 *err = -EBADF;
438 return NULL;
439 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700440
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800441 sock = sock_from_file(file, err);
442 if (!sock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 fput(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 return sock;
445}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700446EXPORT_SYMBOL(sockfd_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800448static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed)
449{
Al Viro00e188e2014-03-03 23:48:18 -0500450 struct fd f = fdget(fd);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800451 struct socket *sock;
452
Hua Zhong36725582006-04-19 15:25:02 -0700453 *err = -EBADF;
Al Viro00e188e2014-03-03 23:48:18 -0500454 if (f.file) {
455 sock = sock_from_file(f.file, err);
456 if (likely(sock)) {
457 *fput_needed = f.flags;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800458 return sock;
Al Viro00e188e2014-03-03 23:48:18 -0500459 }
460 fdput(f);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -0800461 }
462 return NULL;
463}
464
Masatake YAMATO600e1772012-08-29 10:44:29 +0000465#define XATTR_SOCKPROTONAME_SUFFIX "sockprotoname"
466#define XATTR_NAME_SOCKPROTONAME (XATTR_SYSTEM_PREFIX XATTR_SOCKPROTONAME_SUFFIX)
467#define XATTR_NAME_SOCKPROTONAME_LEN (sizeof(XATTR_NAME_SOCKPROTONAME)-1)
468static ssize_t sockfs_getxattr(struct dentry *dentry,
469 const char *name, void *value, size_t size)
470{
471 const char *proto_name;
472 size_t proto_size;
473 int error;
474
475 error = -ENODATA;
476 if (!strncmp(name, XATTR_NAME_SOCKPROTONAME, XATTR_NAME_SOCKPROTONAME_LEN)) {
477 proto_name = dentry->d_name.name;
478 proto_size = strlen(proto_name);
479
480 if (value) {
481 error = -ERANGE;
482 if (proto_size + 1 > size)
483 goto out;
484
485 strncpy(value, proto_name, proto_size + 1);
486 }
487 error = proto_size + 1;
488 }
489
490out:
491 return error;
492}
493
494static ssize_t sockfs_listxattr(struct dentry *dentry, char *buffer,
495 size_t size)
496{
497 ssize_t len;
498 ssize_t used = 0;
499
500 len = security_inode_listsecurity(dentry->d_inode, buffer, size);
501 if (len < 0)
502 return len;
503 used += len;
504 if (buffer) {
505 if (size < used)
506 return -ERANGE;
507 buffer += len;
508 }
509
510 len = (XATTR_NAME_SOCKPROTONAME_LEN + 1);
511 used += len;
512 if (buffer) {
513 if (size < used)
514 return -ERANGE;
515 memcpy(buffer, XATTR_NAME_SOCKPROTONAME, len);
516 buffer += len;
517 }
518
519 return used;
520}
521
522static const struct inode_operations sockfs_inode_ops = {
523 .getxattr = sockfs_getxattr,
524 .listxattr = sockfs_listxattr,
525};
526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527/**
528 * sock_alloc - allocate a socket
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700529 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 * Allocate a new inode and socket object. The two are bound together
531 * and initialised. The socket is then returned. If we are out of inodes
532 * NULL is returned.
533 */
534
535static struct socket *sock_alloc(void)
536{
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700537 struct inode *inode;
538 struct socket *sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Eric Dumazeta209dfc2011-07-26 11:36:34 +0200540 inode = new_inode_pseudo(sock_mnt->mnt_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 if (!inode)
542 return NULL;
543
544 sock = SOCKET_I(inode);
545
Eric Dumazet29a020d2009-09-15 02:39:20 -0700546 kmemcheck_annotate_bitfield(sock, type);
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400547 inode->i_ino = get_next_ino();
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700548 inode->i_mode = S_IFSOCK | S_IRWXUGO;
David Howells8192b0c2008-11-14 10:39:10 +1100549 inode->i_uid = current_fsuid();
550 inode->i_gid = current_fsgid();
Masatake YAMATO600e1772012-08-29 10:44:29 +0000551 inode->i_op = &sockfs_inode_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Alex Shi19e8d692012-05-14 14:15:31 -0700553 this_cpu_add(sockets_in_use, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 return sock;
555}
556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557/**
558 * sock_release - close a socket
559 * @sock: socket to close
560 *
561 * The socket is released from the protocol stack if it has a release
562 * callback, and the inode is then released if the socket is bound to
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700563 * an inode not a file.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566void sock_release(struct socket *sock)
567{
568 if (sock->ops) {
569 struct module *owner = sock->ops->owner;
570
571 sock->ops->release(sock);
572 sock->ops = NULL;
573 module_put(owner);
574 }
575
Eric Dumazeteaefd112011-02-18 03:26:36 +0000576 if (rcu_dereference_protected(sock->wq, 1)->fasync_list)
Yang Yingliang3410f222014-02-12 17:09:55 +0800577 pr_err("%s: fasync list not empty!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Mikulas Patockab09e7862012-07-19 06:13:36 +0000579 if (test_bit(SOCK_EXTERNALLY_ALLOCATED, &sock->flags))
580 return;
581
Alex Shi19e8d692012-05-14 14:15:31 -0700582 this_cpu_sub(sockets_in_use, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 if (!sock->file) {
584 iput(SOCK_INODE(sock));
585 return;
586 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700587 sock->file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700589EXPORT_SYMBOL(sock_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Willem de Bruijn67cc0d42014-09-08 19:58:58 -0400591void __sock_tx_timestamp(const struct sock *sk, __u8 *tx_flags)
Patrick Ohly20d49472009-02-12 05:03:38 +0000592{
Eric Dumazet140c55d2014-08-06 11:49:29 +0200593 u8 flags = *tx_flags;
594
Willem de Bruijnb9f40e22014-08-04 22:11:46 -0400595 if (sk->sk_tsflags & SOF_TIMESTAMPING_TX_HARDWARE)
Eric Dumazet140c55d2014-08-06 11:49:29 +0200596 flags |= SKBTX_HW_TSTAMP;
597
Willem de Bruijnb9f40e22014-08-04 22:11:46 -0400598 if (sk->sk_tsflags & SOF_TIMESTAMPING_TX_SOFTWARE)
Eric Dumazet140c55d2014-08-06 11:49:29 +0200599 flags |= SKBTX_SW_TSTAMP;
600
Willem de Bruijne7fd2882014-08-04 22:11:48 -0400601 if (sk->sk_tsflags & SOF_TIMESTAMPING_TX_SCHED)
Eric Dumazet140c55d2014-08-06 11:49:29 +0200602 flags |= SKBTX_SCHED_TSTAMP;
603
Willem de Bruijne1c8a602014-08-04 22:11:50 -0400604 if (sk->sk_tsflags & SOF_TIMESTAMPING_TX_ACK)
Eric Dumazet140c55d2014-08-06 11:49:29 +0200605 flags |= SKBTX_ACK_TSTAMP;
Willem de Bruijne7fd2882014-08-04 22:11:48 -0400606
Eric Dumazet140c55d2014-08-06 11:49:29 +0200607 *tx_flags = flags;
Patrick Ohly20d49472009-02-12 05:03:38 +0000608}
Willem de Bruijn67cc0d42014-09-08 19:58:58 -0400609EXPORT_SYMBOL(__sock_tx_timestamp);
Patrick Ohly20d49472009-02-12 05:03:38 +0000610
Al Virod8725c82014-12-11 00:02:50 -0500611static inline int sock_sendmsg_nosec(struct socket *sock, struct msghdr *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
Al Viro01e97e62014-12-15 21:39:31 -0500613 int ret = sock->ops->sendmsg(sock, msg, msg_data_left(msg));
Al Virod8725c82014-12-11 00:02:50 -0500614 BUG_ON(ret == -EIOCBQUEUED);
615 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616}
Gu Zheng0cf00c62014-12-05 15:14:23 +0800617
Al Virod8725c82014-12-11 00:02:50 -0500618int sock_sendmsg(struct socket *sock, struct msghdr *msg)
Gu Zheng0cf00c62014-12-05 15:14:23 +0800619{
Al Virod8725c82014-12-11 00:02:50 -0500620 int err = security_socket_sendmsg(sock, msg,
Al Viro01e97e62014-12-15 21:39:31 -0500621 msg_data_left(msg));
Ying Xue1b784142015-03-02 15:37:48 +0800622
Al Virod8725c82014-12-11 00:02:50 -0500623 return err ?: sock_sendmsg_nosec(sock, msg);
Gu Zheng0cf00c62014-12-05 15:14:23 +0800624}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700625EXPORT_SYMBOL(sock_sendmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
628 struct kvec *vec, size_t num, size_t size)
629{
Al Viro6aa24812015-03-21 19:56:16 -0400630 iov_iter_kvec(&msg->msg_iter, WRITE | ITER_KVEC, vec, num, size);
Al Virod8725c82014-12-11 00:02:50 -0500631 return sock_sendmsg(sock, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700633EXPORT_SYMBOL(kernel_sendmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700635/*
636 * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
637 */
638void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
639 struct sk_buff *skb)
640{
Patrick Ohly20d49472009-02-12 05:03:38 +0000641 int need_software_tstamp = sock_flag(sk, SOCK_RCVTSTAMP);
Willem de Bruijnf24b9be2014-08-04 22:11:45 -0400642 struct scm_timestamping tss;
Patrick Ohly20d49472009-02-12 05:03:38 +0000643 int empty = 1;
644 struct skb_shared_hwtstamps *shhwtstamps =
645 skb_hwtstamps(skb);
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700646
Patrick Ohly20d49472009-02-12 05:03:38 +0000647 /* Race occurred between timestamp enabling and packet
648 receiving. Fill in the current time for now. */
649 if (need_software_tstamp && skb->tstamp.tv64 == 0)
650 __net_timestamp(skb);
651
652 if (need_software_tstamp) {
653 if (!sock_flag(sk, SOCK_RCVTSTAMPNS)) {
654 struct timeval tv;
655 skb_get_timestamp(skb, &tv);
656 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMP,
657 sizeof(tv), &tv);
658 } else {
Willem de Bruijnf24b9be2014-08-04 22:11:45 -0400659 struct timespec ts;
660 skb_get_timestampns(skb, &ts);
Patrick Ohly20d49472009-02-12 05:03:38 +0000661 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPNS,
Willem de Bruijnf24b9be2014-08-04 22:11:45 -0400662 sizeof(ts), &ts);
Patrick Ohly20d49472009-02-12 05:03:38 +0000663 }
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700664 }
Patrick Ohly20d49472009-02-12 05:03:38 +0000665
Willem de Bruijnf24b9be2014-08-04 22:11:45 -0400666 memset(&tss, 0, sizeof(tss));
Willem de Bruijnc1991052014-09-03 12:01:18 -0400667 if ((sk->sk_tsflags & SOF_TIMESTAMPING_SOFTWARE) &&
Willem de Bruijnf24b9be2014-08-04 22:11:45 -0400668 ktime_to_timespec_cond(skb->tstamp, tss.ts + 0))
Patrick Ohly20d49472009-02-12 05:03:38 +0000669 empty = 0;
Willem de Bruijn4d276eb2014-07-25 18:01:32 -0400670 if (shhwtstamps &&
Willem de Bruijnb9f40e22014-08-04 22:11:46 -0400671 (sk->sk_tsflags & SOF_TIMESTAMPING_RAW_HARDWARE) &&
Willem de Bruijnf24b9be2014-08-04 22:11:45 -0400672 ktime_to_timespec_cond(shhwtstamps->hwtstamp, tss.ts + 2))
Willem de Bruijn4d276eb2014-07-25 18:01:32 -0400673 empty = 0;
Patrick Ohly20d49472009-02-12 05:03:38 +0000674 if (!empty)
675 put_cmsg(msg, SOL_SOCKET,
Willem de Bruijnf24b9be2014-08-04 22:11:45 -0400676 SCM_TIMESTAMPING, sizeof(tss), &tss);
Eric Dumazet92f37fd2007-03-25 22:14:49 -0700677}
Arnaldo Carvalho de Melo7c81fd82007-03-10 00:39:35 -0300678EXPORT_SYMBOL_GPL(__sock_recv_timestamp);
679
Johannes Berg6e3e9392011-11-09 10:15:42 +0100680void __sock_recv_wifi_status(struct msghdr *msg, struct sock *sk,
681 struct sk_buff *skb)
682{
683 int ack;
684
685 if (!sock_flag(sk, SOCK_WIFI_STATUS))
686 return;
687 if (!skb->wifi_acked_valid)
688 return;
689
690 ack = skb->wifi_acked;
691
692 put_cmsg(msg, SOL_SOCKET, SCM_WIFI_STATUS, sizeof(ack), &ack);
693}
694EXPORT_SYMBOL_GPL(__sock_recv_wifi_status);
695
stephen hemminger11165f12010-10-18 14:27:29 +0000696static inline void sock_recv_drops(struct msghdr *msg, struct sock *sk,
697 struct sk_buff *skb)
Neil Horman3b885782009-10-12 13:26:31 -0700698{
Eyal Birger744d5a32015-03-01 14:58:31 +0200699 if (sock_flag(sk, SOCK_RXQ_OVFL) && skb && SOCK_SKB_CB(skb)->dropcount)
Neil Horman3b885782009-10-12 13:26:31 -0700700 put_cmsg(msg, SOL_SOCKET, SO_RXQ_OVFL,
Eyal Birger744d5a32015-03-01 14:58:31 +0200701 sizeof(__u32), &SOCK_SKB_CB(skb)->dropcount);
Neil Horman3b885782009-10-12 13:26:31 -0700702}
703
Eric Dumazet767dd032010-04-28 19:14:43 +0000704void __sock_recv_ts_and_drops(struct msghdr *msg, struct sock *sk,
Neil Horman3b885782009-10-12 13:26:31 -0700705 struct sk_buff *skb)
706{
707 sock_recv_timestamp(msg, sk, skb);
708 sock_recv_drops(msg, sk, skb);
709}
Eric Dumazet767dd032010-04-28 19:14:43 +0000710EXPORT_SYMBOL_GPL(__sock_recv_ts_and_drops);
Neil Horman3b885782009-10-12 13:26:31 -0700711
Ying Xue1b784142015-03-02 15:37:48 +0800712static inline int sock_recvmsg_nosec(struct socket *sock, struct msghdr *msg,
713 size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
Ying Xue1b784142015-03-02 15:37:48 +0800715 return sock->ops->recvmsg(sock, msg, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716}
717
Ying Xue1b784142015-03-02 15:37:48 +0800718int sock_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
719 int flags)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -0700720{
721 int err = security_socket_recvmsg(sock, msg, size, flags);
722
Ying Xue1b784142015-03-02 15:37:48 +0800723 return err ?: sock_recvmsg_nosec(sock, msg, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700725EXPORT_SYMBOL(sock_recvmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Martin Lucinac1249c02010-12-10 00:04:05 +0000727/**
728 * kernel_recvmsg - Receive a message from a socket (kernel space)
729 * @sock: The socket to receive the message from
730 * @msg: Received message
731 * @vec: Input s/g array for message data
732 * @num: Size of input s/g array
733 * @size: Number of bytes to read
734 * @flags: Message flags (MSG_DONTWAIT, etc...)
735 *
736 * On return the msg structure contains the scatter/gather array passed in the
737 * vec argument. The array is modified so that it consists of the unfilled
738 * portion of the original array.
739 *
740 * The returned value is the total number of bytes received, or an error.
741 */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700742int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
743 struct kvec *vec, size_t num, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
745 mm_segment_t oldfs = get_fs();
746 int result;
747
Al Viro6aa24812015-03-21 19:56:16 -0400748 iov_iter_kvec(&msg->msg_iter, READ | ITER_KVEC, vec, num, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 set_fs(KERNEL_DS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 result = sock_recvmsg(sock, msg, size, flags);
751 set_fs(oldfs);
752 return result;
753}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700754EXPORT_SYMBOL(kernel_recvmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -0300756static ssize_t sock_sendpage(struct file *file, struct page *page,
757 int offset, size_t size, loff_t *ppos, int more)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
759 struct socket *sock;
760 int flags;
761
Eric Dumazetb69aee02005-09-06 14:42:45 -0700762 sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Eric Dumazet35f9c092012-04-05 03:05:35 +0000764 flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
765 /* more is a combination of MSG_MORE and MSG_SENDPAGE_NOTLAST */
766 flags |= more;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Linus Torvaldse6949582009-08-13 08:28:36 -0700768 return kernel_sendpage(sock, page, offset, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769}
770
Jens Axboe9c55e012007-11-06 23:30:13 -0800771static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700772 struct pipe_inode_info *pipe, size_t len,
Jens Axboe9c55e012007-11-06 23:30:13 -0800773 unsigned int flags)
774{
775 struct socket *sock = file->private_data;
776
Rémi Denis-Courmont997b37d2008-02-15 02:35:45 -0800777 if (unlikely(!sock->ops->splice_read))
778 return -EINVAL;
779
Jens Axboe9c55e012007-11-06 23:30:13 -0800780 return sock->ops->splice_read(sock, ppos, pipe, len, flags);
781}
782
Al Viro8ae5e032014-11-28 19:40:50 -0500783static ssize_t sock_read_iter(struct kiocb *iocb, struct iov_iter *to)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800784{
Al Viro6d652332015-01-30 16:12:56 -0500785 struct file *file = iocb->ki_filp;
786 struct socket *sock = file->private_data;
tadeusz.struk@intel.com0345f932015-03-19 12:31:25 -0700787 struct msghdr msg = {.msg_iter = *to,
788 .msg_iocb = iocb};
Al Viro8ae5e032014-11-28 19:40:50 -0500789 ssize_t res;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800790
Al Viro8ae5e032014-11-28 19:40:50 -0500791 if (file->f_flags & O_NONBLOCK)
792 msg.msg_flags = MSG_DONTWAIT;
793
794 if (iocb->ki_pos != 0)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800795 return -ESPIPE;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700796
Christoph Hellwig66ee59a2015-02-11 19:56:46 +0100797 if (!iov_iter_count(to)) /* Match SYS5 behaviour */
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800798 return 0;
799
Al Viro237dae82015-04-09 00:00:30 -0400800 res = sock_recvmsg(sock, &msg, iov_iter_count(to), msg.msg_flags);
Al Viro8ae5e032014-11-28 19:40:50 -0500801 *to = msg.msg_iter;
802 return res;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800803}
804
Al Viro8ae5e032014-11-28 19:40:50 -0500805static ssize_t sock_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
Al Viro6d652332015-01-30 16:12:56 -0500807 struct file *file = iocb->ki_filp;
808 struct socket *sock = file->private_data;
tadeusz.struk@intel.com0345f932015-03-19 12:31:25 -0700809 struct msghdr msg = {.msg_iter = *from,
810 .msg_iocb = iocb};
Al Viro8ae5e032014-11-28 19:40:50 -0500811 ssize_t res;
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800812
Al Viro8ae5e032014-11-28 19:40:50 -0500813 if (iocb->ki_pos != 0)
Christoph Hellwigce1d4d32005-12-22 21:08:46 -0800814 return -ESPIPE;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700815
Al Viro8ae5e032014-11-28 19:40:50 -0500816 if (file->f_flags & O_NONBLOCK)
817 msg.msg_flags = MSG_DONTWAIT;
818
Al Viro6d652332015-01-30 16:12:56 -0500819 if (sock->type == SOCK_SEQPACKET)
820 msg.msg_flags |= MSG_EOR;
821
Al Virod8725c82014-12-11 00:02:50 -0500822 res = sock_sendmsg(sock, &msg);
Al Viro8ae5e032014-11-28 19:40:50 -0500823 *from = msg.msg_iter;
824 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825}
826
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827/*
828 * Atomic setting of ioctl hooks to avoid race
829 * with module unload.
830 */
831
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800832static DEFINE_MUTEX(br_ioctl_mutex);
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700833static int (*br_ioctl_hook) (struct net *, unsigned int cmd, void __user *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Eric W. Biederman881d9662007-09-17 11:56:21 -0700835void brioctl_set(int (*hook) (struct net *, unsigned int, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800837 mutex_lock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 br_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800839 mutex_unlock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840}
841EXPORT_SYMBOL(brioctl_set);
842
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800843static DEFINE_MUTEX(vlan_ioctl_mutex);
Eric W. Biederman881d9662007-09-17 11:56:21 -0700844static int (*vlan_ioctl_hook) (struct net *, void __user *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Eric W. Biederman881d9662007-09-17 11:56:21 -0700846void vlan_ioctl_set(int (*hook) (struct net *, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800848 mutex_lock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 vlan_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800850 mutex_unlock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851}
852EXPORT_SYMBOL(vlan_ioctl_set);
853
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800854static DEFINE_MUTEX(dlci_ioctl_mutex);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700855static int (*dlci_ioctl_hook) (unsigned int, void __user *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700857void dlci_ioctl_set(int (*hook) (unsigned int, void __user *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800859 mutex_lock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 dlci_ioctl_hook = hook;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800861 mutex_unlock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862}
863EXPORT_SYMBOL(dlci_ioctl_set);
864
Arnd Bergmann6b960182009-11-06 23:10:54 -0800865static long sock_do_ioctl(struct net *net, struct socket *sock,
866 unsigned int cmd, unsigned long arg)
867{
868 int err;
869 void __user *argp = (void __user *)arg;
870
871 err = sock->ops->ioctl(sock, cmd, arg);
872
873 /*
874 * If this ioctl is unknown try to hand it down
875 * to the NIC driver.
876 */
877 if (err == -ENOIOCTLCMD)
878 err = dev_ioctl(net, cmd, argp);
879
880 return err;
881}
882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883/*
884 * With an ioctl, arg may well be a user mode pointer, but we don't know
885 * what to do with it - that's up to the protocol still.
886 */
887
888static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
889{
890 struct socket *sock;
Eric W. Biederman881d9662007-09-17 11:56:21 -0700891 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 void __user *argp = (void __user *)arg;
893 int pid, err;
Eric W. Biederman881d9662007-09-17 11:56:21 -0700894 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Eric Dumazetb69aee02005-09-06 14:42:45 -0700896 sock = file->private_data;
Eric W. Biederman881d9662007-09-17 11:56:21 -0700897 sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900898 net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
Eric W. Biederman881d9662007-09-17 11:56:21 -0700900 err = dev_ioctl(net, cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 } else
Johannes Berg3d23e342009-09-29 23:27:28 +0200902#ifdef CONFIG_WEXT_CORE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
Eric W. Biederman881d9662007-09-17 11:56:21 -0700904 err = dev_ioctl(net, cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 } else
Johannes Berg3d23e342009-09-29 23:27:28 +0200906#endif
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700907 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 case FIOSETOWN:
909 case SIOCSPGRP:
910 err = -EFAULT;
911 if (get_user(pid, (int __user *)argp))
912 break;
Jeff Laytone0b93ed2014-08-22 11:27:32 -0400913 f_setown(sock->file, pid, 1);
914 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 break;
916 case FIOGETOWN:
917 case SIOCGPGRP:
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700918 err = put_user(f_getown(sock->file),
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700919 (int __user *)argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 break;
921 case SIOCGIFBR:
922 case SIOCSIFBR:
923 case SIOCBRADDBR:
924 case SIOCBRDELBR:
925 err = -ENOPKG;
926 if (!br_ioctl_hook)
927 request_module("bridge");
928
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800929 mutex_lock(&br_ioctl_mutex);
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700930 if (br_ioctl_hook)
Eric W. Biederman881d9662007-09-17 11:56:21 -0700931 err = br_ioctl_hook(net, cmd, argp);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800932 mutex_unlock(&br_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 break;
934 case SIOCGIFVLAN:
935 case SIOCSIFVLAN:
936 err = -ENOPKG;
937 if (!vlan_ioctl_hook)
938 request_module("8021q");
939
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800940 mutex_lock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 if (vlan_ioctl_hook)
Eric W. Biederman881d9662007-09-17 11:56:21 -0700942 err = vlan_ioctl_hook(net, argp);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800943 mutex_unlock(&vlan_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 case SIOCADDDLCI:
946 case SIOCDELDLCI:
947 err = -ENOPKG;
948 if (!dlci_ioctl_hook)
949 request_module("dlci");
950
Pavel Emelyanov7512cbf2008-03-21 15:58:52 -0700951 mutex_lock(&dlci_ioctl_mutex);
952 if (dlci_ioctl_hook)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 err = dlci_ioctl_hook(cmd, argp);
Pavel Emelyanov7512cbf2008-03-21 15:58:52 -0700954 mutex_unlock(&dlci_ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 break;
956 default:
Arnd Bergmann6b960182009-11-06 23:10:54 -0800957 err = sock_do_ioctl(net, sock, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 break;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 return err;
961}
962
963int sock_create_lite(int family, int type, int protocol, struct socket **res)
964{
965 int err;
966 struct socket *sock = NULL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700967
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 err = security_socket_create(family, type, protocol, 1);
969 if (err)
970 goto out;
971
972 sock = sock_alloc();
973 if (!sock) {
974 err = -ENOMEM;
975 goto out;
976 }
977
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 sock->type = type;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -0700979 err = security_socket_post_create(sock, family, type, protocol, 1);
980 if (err)
981 goto out_release;
982
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983out:
984 *res = sock;
985 return err;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -0700986out_release:
987 sock_release(sock);
988 sock = NULL;
989 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990}
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700991EXPORT_SYMBOL(sock_create_lite);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
993/* No kernel lock held - perfect */
Stephen Hemminger89bddce2006-09-01 00:19:31 -0700994static unsigned int sock_poll(struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995{
Eliezer Tamircbf55002013-07-08 16:20:34 +0300996 unsigned int busy_flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 struct socket *sock;
998
999 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001000 * We can't return errors to poll, so it's either yes or no.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 */
Eric Dumazetb69aee02005-09-06 14:42:45 -07001002 sock = file->private_data;
Eliezer Tamir2d48d672013-06-24 10:28:03 +03001003
Eliezer Tamircbf55002013-07-08 16:20:34 +03001004 if (sk_can_busy_loop(sock->sk)) {
Eliezer Tamir2d48d672013-06-24 10:28:03 +03001005 /* this socket can poll_ll so tell the system call */
Eliezer Tamircbf55002013-07-08 16:20:34 +03001006 busy_flag = POLL_BUSY_LOOP;
Eliezer Tamir2d48d672013-06-24 10:28:03 +03001007
1008 /* once, only if requested by syscall */
Eliezer Tamircbf55002013-07-08 16:20:34 +03001009 if (wait && (wait->_key & POLL_BUSY_LOOP))
1010 sk_busy_loop(sock->sk, 1);
Eliezer Tamir2d48d672013-06-24 10:28:03 +03001011 }
1012
Eliezer Tamircbf55002013-07-08 16:20:34 +03001013 return busy_flag | sock->ops->poll(file, sock, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014}
1015
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001016static int sock_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017{
Eric Dumazetb69aee02005-09-06 14:42:45 -07001018 struct socket *sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
1020 return sock->ops->mmap(file, sock, vma);
1021}
1022
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001023static int sock_close(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 sock_release(SOCKET_I(inode));
1026 return 0;
1027}
1028
1029/*
1030 * Update the socket async list
1031 *
1032 * Fasync_list locking strategy.
1033 *
1034 * 1. fasync_list is modified only under process context socket lock
1035 * i.e. under semaphore.
1036 * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
Eric Dumazet989a2972010-04-14 09:55:35 +00001037 * or under socket lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 */
1039
1040static int sock_fasync(int fd, struct file *filp, int on)
1041{
Eric Dumazet989a2972010-04-14 09:55:35 +00001042 struct socket *sock = filp->private_data;
1043 struct sock *sk = sock->sk;
Eric Dumazeteaefd112011-02-18 03:26:36 +00001044 struct socket_wq *wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
Eric Dumazet989a2972010-04-14 09:55:35 +00001046 if (sk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
1049 lock_sock(sk);
Eric Dumazeteaefd112011-02-18 03:26:36 +00001050 wq = rcu_dereference_protected(sock->wq, sock_owned_by_user(sk));
1051 fasync_helper(fd, filp, on, &wq->fasync_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
Eric Dumazeteaefd112011-02-18 03:26:36 +00001053 if (!wq->fasync_list)
Eric Dumazet989a2972010-04-14 09:55:35 +00001054 sock_reset_flag(sk, SOCK_FASYNC);
Jonathan Corbet76398422009-02-01 14:26:59 -07001055 else
Eric Dumazetbcdce712009-10-06 17:28:29 -07001056 sock_set_flag(sk, SOCK_FASYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
Eric Dumazet989a2972010-04-14 09:55:35 +00001058 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 return 0;
1060}
1061
Eric Dumazet43815482010-04-29 11:01:49 +00001062/* This function may be called only under socket lock or callback_lock or rcu_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
1064int sock_wake_async(struct socket *sock, int how, int band)
1065{
Eric Dumazet43815482010-04-29 11:01:49 +00001066 struct socket_wq *wq;
1067
1068 if (!sock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 return -1;
Eric Dumazet43815482010-04-29 11:01:49 +00001070 rcu_read_lock();
1071 wq = rcu_dereference(sock->wq);
1072 if (!wq || !wq->fasync_list) {
1073 rcu_read_unlock();
1074 return -1;
1075 }
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001076 switch (how) {
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001077 case SOCK_WAKE_WAITD:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 if (test_bit(SOCK_ASYNC_WAITDATA, &sock->flags))
1079 break;
1080 goto call_kill;
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001081 case SOCK_WAKE_SPACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags))
1083 break;
1084 /* fall through */
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001085 case SOCK_WAKE_IO:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001086call_kill:
Eric Dumazet43815482010-04-29 11:01:49 +00001087 kill_fasync(&wq->fasync_list, SIGIO, band);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 break;
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08001089 case SOCK_WAKE_URG:
Eric Dumazet43815482010-04-29 11:01:49 +00001090 kill_fasync(&wq->fasync_list, SIGURG, band);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 }
Eric Dumazet43815482010-04-29 11:01:49 +00001092 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 return 0;
1094}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001095EXPORT_SYMBOL(sock_wake_async);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Pavel Emelyanov721db932010-09-29 16:06:32 +04001097int __sock_create(struct net *net, int family, int type, int protocol,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001098 struct socket **res, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099{
1100 int err;
1101 struct socket *sock;
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001102 const struct net_proto_family *pf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
1104 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001105 * Check protocol is in range
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 */
1107 if (family < 0 || family >= NPROTO)
1108 return -EAFNOSUPPORT;
1109 if (type < 0 || type >= SOCK_MAX)
1110 return -EINVAL;
1111
1112 /* Compatibility.
1113
1114 This uglymoron is moved from INET layer to here to avoid
1115 deadlock in module load.
1116 */
1117 if (family == PF_INET && type == SOCK_PACKET) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001118 static int warned;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 if (!warned) {
1120 warned = 1;
Yang Yingliang3410f222014-02-12 17:09:55 +08001121 pr_info("%s uses obsolete (PF_INET,SOCK_PACKET)\n",
1122 current->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 }
1124 family = PF_PACKET;
1125 }
1126
1127 err = security_socket_create(family, type, protocol, kern);
1128 if (err)
1129 return err;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001130
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001131 /*
1132 * Allocate the socket and allow the family to set things up. if
1133 * the protocol is 0, the family is instructed to select an appropriate
1134 * default.
1135 */
1136 sock = sock_alloc();
1137 if (!sock) {
Joe Perchese87cc472012-05-13 21:56:26 +00001138 net_warn_ratelimited("socket: no more sockets\n");
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001139 return -ENFILE; /* Not exactly a match, but its the
1140 closest posix thing */
1141 }
1142
1143 sock->type = type;
1144
Johannes Berg95a5afc2008-10-16 15:24:51 -07001145#ifdef CONFIG_MODULES
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001146 /* Attempt to load a protocol module if the find failed.
1147 *
1148 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 * requested real, full-featured networking support upon configuration.
1150 * Otherwise module support will break!
1151 */
Eric Dumazet190683a2010-11-10 10:50:44 +00001152 if (rcu_access_pointer(net_families[family]) == NULL)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001153 request_module("net-pf-%d", family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154#endif
1155
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001156 rcu_read_lock();
1157 pf = rcu_dereference(net_families[family]);
1158 err = -EAFNOSUPPORT;
1159 if (!pf)
1160 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
1162 /*
1163 * We will call the ->create function, that possibly is in a loadable
1164 * module, so we have to bump that loadable module refcnt first.
1165 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001166 if (!try_module_get(pf->owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 goto out_release;
1168
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001169 /* Now protected by module ref count */
1170 rcu_read_unlock();
1171
Eric Paris3f378b62009-11-05 22:18:14 -08001172 err = pf->create(net, sock, protocol, kern);
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001173 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 goto out_module_put;
Frank Filza79af592005-09-27 15:23:38 -07001175
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 /*
1177 * Now to bump the refcnt of the [loadable] module that owns this
1178 * socket at sock_release time we decrement its refcnt.
1179 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001180 if (!try_module_get(sock->ops->owner))
1181 goto out_module_busy;
1182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 /*
1184 * Now that we're done with the ->create function, the [loadable]
1185 * module can have its refcnt decremented
1186 */
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001187 module_put(pf->owner);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001188 err = security_socket_post_create(sock, family, type, protocol, kern);
1189 if (err)
Herbert Xu3b185522007-08-15 14:46:02 -07001190 goto out_sock_release;
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001191 *res = sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001193 return 0;
1194
1195out_module_busy:
1196 err = -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197out_module_put:
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001198 sock->ops = NULL;
1199 module_put(pf->owner);
1200out_sock_release:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 sock_release(sock);
Stephen Hemminger55737fd2006-09-01 00:23:39 -07001202 return err;
1203
1204out_release:
1205 rcu_read_unlock();
1206 goto out_sock_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207}
Pavel Emelyanov721db932010-09-29 16:06:32 +04001208EXPORT_SYMBOL(__sock_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
1210int sock_create(int family, int type, int protocol, struct socket **res)
1211{
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001212 return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001214EXPORT_SYMBOL(sock_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
1216int sock_create_kern(int family, int type, int protocol, struct socket **res)
1217{
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001218 return __sock_create(&init_net, family, type, protocol, res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001220EXPORT_SYMBOL(sock_create_kern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001222SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
1224 int retval;
1225 struct socket *sock;
Ulrich Dreppera677a032008-07-23 21:29:17 -07001226 int flags;
1227
Ulrich Dreppere38b36f2008-07-23 21:29:42 -07001228 /* Check the SOCK_* constants for consistency. */
1229 BUILD_BUG_ON(SOCK_CLOEXEC != O_CLOEXEC);
1230 BUILD_BUG_ON((SOCK_MAX | SOCK_TYPE_MASK) != SOCK_TYPE_MASK);
1231 BUILD_BUG_ON(SOCK_CLOEXEC & SOCK_TYPE_MASK);
1232 BUILD_BUG_ON(SOCK_NONBLOCK & SOCK_TYPE_MASK);
1233
Ulrich Dreppera677a032008-07-23 21:29:17 -07001234 flags = type & ~SOCK_TYPE_MASK;
Ulrich Drepper77d27202008-07-23 21:29:35 -07001235 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Dreppera677a032008-07-23 21:29:17 -07001236 return -EINVAL;
1237 type &= SOCK_TYPE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001239 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1240 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1241
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 retval = sock_create(family, type, protocol, &sock);
1243 if (retval < 0)
1244 goto out;
1245
Ulrich Drepper77d27202008-07-23 21:29:35 -07001246 retval = sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 if (retval < 0)
1248 goto out_release;
1249
1250out:
1251 /* It may be already another descriptor 8) Not kernel problem. */
1252 return retval;
1253
1254out_release:
1255 sock_release(sock);
1256 return retval;
1257}
1258
1259/*
1260 * Create a pair of connected sockets.
1261 */
1262
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001263SYSCALL_DEFINE4(socketpair, int, family, int, type, int, protocol,
1264 int __user *, usockvec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265{
1266 struct socket *sock1, *sock2;
1267 int fd1, fd2, err;
Al Virodb349502007-02-07 01:48:00 -05001268 struct file *newfile1, *newfile2;
Ulrich Dreppera677a032008-07-23 21:29:17 -07001269 int flags;
1270
1271 flags = type & ~SOCK_TYPE_MASK;
Ulrich Drepper77d27202008-07-23 21:29:35 -07001272 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Dreppera677a032008-07-23 21:29:17 -07001273 return -EINVAL;
1274 type &= SOCK_TYPE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001276 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1277 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1278
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 /*
1280 * Obtain the first socket and check if the underlying protocol
1281 * supports the socketpair call.
1282 */
1283
1284 err = sock_create(family, type, protocol, &sock1);
1285 if (err < 0)
1286 goto out;
1287
1288 err = sock_create(family, type, protocol, &sock2);
1289 if (err < 0)
1290 goto out_release_1;
1291
1292 err = sock1->ops->socketpair(sock1, sock2);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001293 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 goto out_release_both;
1295
Al Viro28407632012-08-17 23:54:15 -04001296 fd1 = get_unused_fd_flags(flags);
David S. Millerbf3c23d2007-10-29 21:54:02 -07001297 if (unlikely(fd1 < 0)) {
1298 err = fd1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 goto out_release_both;
David S. Millerbf3c23d2007-10-29 21:54:02 -07001300 }
Yann Droneaudd73aa282013-12-09 22:42:20 +01001301
Al Viro28407632012-08-17 23:54:15 -04001302 fd2 = get_unused_fd_flags(flags);
Al Viro198de4d2009-08-05 19:29:23 +04001303 if (unlikely(fd2 < 0)) {
1304 err = fd2;
Yann Droneaudd73aa282013-12-09 22:42:20 +01001305 goto out_put_unused_1;
Al Viro28407632012-08-17 23:54:15 -04001306 }
1307
Linus Torvaldsaab174f2012-10-02 20:25:04 -07001308 newfile1 = sock_alloc_file(sock1, flags, NULL);
Al Viro28407632012-08-17 23:54:15 -04001309 if (unlikely(IS_ERR(newfile1))) {
1310 err = PTR_ERR(newfile1);
Yann Droneaudd73aa282013-12-09 22:42:20 +01001311 goto out_put_unused_both;
Al Viro28407632012-08-17 23:54:15 -04001312 }
1313
Linus Torvaldsaab174f2012-10-02 20:25:04 -07001314 newfile2 = sock_alloc_file(sock2, flags, NULL);
Al Viro28407632012-08-17 23:54:15 -04001315 if (IS_ERR(newfile2)) {
1316 err = PTR_ERR(newfile2);
Yann Droneaudd73aa282013-12-09 22:42:20 +01001317 goto out_fput_1;
Al Virodb349502007-02-07 01:48:00 -05001318 }
1319
Yann Droneaudd73aa282013-12-09 22:42:20 +01001320 err = put_user(fd1, &usockvec[0]);
1321 if (err)
1322 goto out_fput_both;
1323
1324 err = put_user(fd2, &usockvec[1]);
1325 if (err)
1326 goto out_fput_both;
1327
Al Viro157cf642008-12-14 04:57:47 -05001328 audit_fd_pair(fd1, fd2);
Yann Droneaudd73aa282013-12-09 22:42:20 +01001329
Al Virodb349502007-02-07 01:48:00 -05001330 fd_install(fd1, newfile1);
1331 fd_install(fd2, newfile2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 /* fd1 and fd2 may be already another descriptors.
1333 * Not kernel problem.
1334 */
1335
Yann Droneaudd73aa282013-12-09 22:42:20 +01001336 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Yann Droneaudd73aa282013-12-09 22:42:20 +01001338out_fput_both:
1339 fput(newfile2);
1340 fput(newfile1);
1341 put_unused_fd(fd2);
1342 put_unused_fd(fd1);
1343 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
Yann Droneaudd73aa282013-12-09 22:42:20 +01001345out_fput_1:
1346 fput(newfile1);
1347 put_unused_fd(fd2);
1348 put_unused_fd(fd1);
1349 sock_release(sock2);
1350 goto out;
1351
1352out_put_unused_both:
1353 put_unused_fd(fd2);
1354out_put_unused_1:
1355 put_unused_fd(fd1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356out_release_both:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001357 sock_release(sock2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358out_release_1:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001359 sock_release(sock1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360out:
1361 return err;
1362}
1363
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364/*
1365 * Bind a name to a socket. Nothing much to do here since it's
1366 * the protocol's responsibility to handle the local address.
1367 *
1368 * We move the socket address to kernel space before we call
1369 * the protocol layer (having also checked the address is ok).
1370 */
1371
Heiko Carstens20f37032009-01-14 14:14:23 +01001372SYSCALL_DEFINE3(bind, int, fd, struct sockaddr __user *, umyaddr, int, addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373{
1374 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001375 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001376 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001378 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001379 if (sock) {
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001380 err = move_addr_to_kernel(umyaddr, addrlen, &address);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001381 if (err >= 0) {
1382 err = security_socket_bind(sock,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001383 (struct sockaddr *)&address,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001384 addrlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001385 if (!err)
1386 err = sock->ops->bind(sock,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001387 (struct sockaddr *)
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001388 &address, addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 }
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001390 fput_light(sock->file, fput_needed);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 return err;
1393}
1394
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395/*
1396 * Perform a listen. Basically, we allow the protocol to do anything
1397 * necessary for a listen, and if that works, we mark the socket as
1398 * ready for listening.
1399 */
1400
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001401SYSCALL_DEFINE2(listen, int, fd, int, backlog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402{
1403 struct socket *sock;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001404 int err, fput_needed;
Pavel Emelyanovb8e1f9b2007-12-08 00:12:33 -08001405 int somaxconn;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001406
1407 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1408 if (sock) {
Pavel Emelyanov8efa6e92008-03-31 19:41:14 -07001409 somaxconn = sock_net(sock->sk)->core.sysctl_somaxconn;
Eric Dumazet95c96172012-04-15 05:58:06 +00001410 if ((unsigned int)backlog > somaxconn)
Pavel Emelyanovb8e1f9b2007-12-08 00:12:33 -08001411 backlog = somaxconn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
1413 err = security_socket_listen(sock, backlog);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001414 if (!err)
1415 err = sock->ops->listen(sock, backlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001417 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 }
1419 return err;
1420}
1421
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422/*
1423 * For accept, we attempt to create a new socket, set up the link
1424 * with the client, wake up the client, then return the new
1425 * connected fd. We collect the address of the connector in kernel
1426 * space and move it to user at the very end. This is unclean because
1427 * we open the socket then return an error.
1428 *
1429 * 1003.1g adds the ability to recvmsg() to query connection pending
1430 * status to recvmsg. We need to add that support in a way thats
1431 * clean when we restucture accept also.
1432 */
1433
Heiko Carstens20f37032009-01-14 14:14:23 +01001434SYSCALL_DEFINE4(accept4, int, fd, struct sockaddr __user *, upeer_sockaddr,
1435 int __user *, upeer_addrlen, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436{
1437 struct socket *sock, *newsock;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001438 struct file *newfile;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001439 int err, len, newfd, fput_needed;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001440 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Ulrich Drepper77d27202008-07-23 21:29:35 -07001442 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001443 return -EINVAL;
1444
1445 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1446 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1447
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001448 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 if (!sock)
1450 goto out;
1451
1452 err = -ENFILE;
Eric Dumazetc6d409c2010-06-03 20:03:40 -07001453 newsock = sock_alloc();
1454 if (!newsock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 goto out_put;
1456
1457 newsock->type = sock->type;
1458 newsock->ops = sock->ops;
1459
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 /*
1461 * We don't need try_module_get here, as the listening socket (sock)
1462 * has the protocol module (sock->ops->owner) held.
1463 */
1464 __module_get(newsock->ops->owner);
1465
Al Viro28407632012-08-17 23:54:15 -04001466 newfd = get_unused_fd_flags(flags);
David S. Miller39d8c1b2006-03-20 17:13:49 -08001467 if (unlikely(newfd < 0)) {
1468 err = newfd;
David S. Miller9a1875e2006-04-01 12:48:36 -08001469 sock_release(newsock);
1470 goto out_put;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001471 }
Linus Torvaldsaab174f2012-10-02 20:25:04 -07001472 newfile = sock_alloc_file(newsock, flags, sock->sk->sk_prot_creator->name);
Al Viro28407632012-08-17 23:54:15 -04001473 if (unlikely(IS_ERR(newfile))) {
1474 err = PTR_ERR(newfile);
1475 put_unused_fd(newfd);
1476 sock_release(newsock);
1477 goto out_put;
1478 }
David S. Miller39d8c1b2006-03-20 17:13:49 -08001479
Frank Filza79af592005-09-27 15:23:38 -07001480 err = security_socket_accept(sock, newsock);
1481 if (err)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001482 goto out_fd;
Frank Filza79af592005-09-27 15:23:38 -07001483
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 err = sock->ops->accept(sock, newsock, sock->file->f_flags);
1485 if (err < 0)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001486 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
1488 if (upeer_sockaddr) {
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001489 if (newsock->ops->getname(newsock, (struct sockaddr *)&address,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001490 &len, 2) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 err = -ECONNABORTED;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001492 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 }
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001494 err = move_addr_to_user(&address,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001495 len, upeer_sockaddr, upeer_addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 if (err < 0)
David S. Miller39d8c1b2006-03-20 17:13:49 -08001497 goto out_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 }
1499
1500 /* File flags are not inherited via accept() unlike another OSes. */
1501
David S. Miller39d8c1b2006-03-20 17:13:49 -08001502 fd_install(newfd, newfile);
1503 err = newfd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001506 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507out:
1508 return err;
David S. Miller39d8c1b2006-03-20 17:13:49 -08001509out_fd:
David S. Miller9606a212006-04-01 01:00:14 -08001510 fput(newfile);
David S. Miller39d8c1b2006-03-20 17:13:49 -08001511 put_unused_fd(newfd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 goto out_put;
1513}
1514
Heiko Carstens20f37032009-01-14 14:14:23 +01001515SYSCALL_DEFINE3(accept, int, fd, struct sockaddr __user *, upeer_sockaddr,
1516 int __user *, upeer_addrlen)
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001517{
Ulrich Drepperde11def2008-11-19 15:36:14 -08001518 return sys_accept4(fd, upeer_sockaddr, upeer_addrlen, 0);
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07001519}
1520
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521/*
1522 * Attempt to connect to a socket with the server address. The address
1523 * is in user space so we verify it is OK and move it to kernel space.
1524 *
1525 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
1526 * break bindings
1527 *
1528 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
1529 * other SEQPACKET protocols that take time to connect() as it doesn't
1530 * include the -EINPROGRESS status for such sockets.
1531 */
1532
Heiko Carstens20f37032009-01-14 14:14:23 +01001533SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
1534 int, addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535{
1536 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001537 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001538 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001540 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 if (!sock)
1542 goto out;
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001543 err = move_addr_to_kernel(uservaddr, addrlen, &address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 if (err < 0)
1545 goto out_put;
1546
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001547 err =
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001548 security_socket_connect(sock, (struct sockaddr *)&address, addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 if (err)
1550 goto out_put;
1551
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001552 err = sock->ops->connect(sock, (struct sockaddr *)&address, addrlen,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 sock->file->f_flags);
1554out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001555 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556out:
1557 return err;
1558}
1559
1560/*
1561 * Get the local address ('name') of a socket object. Move the obtained
1562 * name to user space.
1563 */
1564
Heiko Carstens20f37032009-01-14 14:14:23 +01001565SYSCALL_DEFINE3(getsockname, int, fd, struct sockaddr __user *, usockaddr,
1566 int __user *, usockaddr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567{
1568 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001569 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001570 int len, err, fput_needed;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001571
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001572 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 if (!sock)
1574 goto out;
1575
1576 err = security_socket_getsockname(sock);
1577 if (err)
1578 goto out_put;
1579
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001580 err = sock->ops->getname(sock, (struct sockaddr *)&address, &len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 if (err)
1582 goto out_put;
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001583 err = move_addr_to_user(&address, len, usockaddr, usockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
1585out_put:
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001586 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587out:
1588 return err;
1589}
1590
1591/*
1592 * Get the remote address ('name') of a socket object. Move the obtained
1593 * name to user space.
1594 */
1595
Heiko Carstens20f37032009-01-14 14:14:23 +01001596SYSCALL_DEFINE3(getpeername, int, fd, struct sockaddr __user *, usockaddr,
1597 int __user *, usockaddr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598{
1599 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001600 struct sockaddr_storage address;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001601 int len, err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001603 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1604 if (sock != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 err = security_socket_getpeername(sock);
1606 if (err) {
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001607 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 return err;
1609 }
1610
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001611 err =
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001612 sock->ops->getname(sock, (struct sockaddr *)&address, &len,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001613 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 if (!err)
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001615 err = move_addr_to_user(&address, len, usockaddr,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001616 usockaddr_len);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001617 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 }
1619 return err;
1620}
1621
1622/*
1623 * Send a datagram to a given address. We move the address into kernel
1624 * space and check the user space data area is readable before invoking
1625 * the protocol.
1626 */
1627
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001628SYSCALL_DEFINE6(sendto, int, fd, void __user *, buff, size_t, len,
Eric Dumazet95c96172012-04-15 05:58:06 +00001629 unsigned int, flags, struct sockaddr __user *, addr,
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001630 int, addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631{
1632 struct socket *sock;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001633 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 int err;
1635 struct msghdr msg;
1636 struct iovec iov;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001637 int fput_needed;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001638
Al Viro602bd0e2015-03-21 19:12:32 -04001639 err = import_single_range(WRITE, buff, len, &iov, &msg.msg_iter);
1640 if (unlikely(err))
1641 return err;
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001642 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1643 if (!sock)
David S. Miller4387ff72007-02-08 15:06:08 -08001644 goto out;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001645
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001646 msg.msg_name = NULL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001647 msg.msg_control = NULL;
1648 msg.msg_controllen = 0;
1649 msg.msg_namelen = 0;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001650 if (addr) {
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001651 err = move_addr_to_kernel(addr, addr_len, &address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 if (err < 0)
1653 goto out_put;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001654 msg.msg_name = (struct sockaddr *)&address;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001655 msg.msg_namelen = addr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 }
1657 if (sock->file->f_flags & O_NONBLOCK)
1658 flags |= MSG_DONTWAIT;
1659 msg.msg_flags = flags;
Al Virod8725c82014-12-11 00:02:50 -05001660 err = sock_sendmsg(sock, &msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001662out_put:
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001663 fput_light(sock->file, fput_needed);
David S. Miller4387ff72007-02-08 15:06:08 -08001664out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 return err;
1666}
1667
1668/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001669 * Send a datagram down a socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 */
1671
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001672SYSCALL_DEFINE4(send, int, fd, void __user *, buff, size_t, len,
Eric Dumazet95c96172012-04-15 05:58:06 +00001673 unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674{
1675 return sys_sendto(fd, buff, len, flags, NULL, 0);
1676}
1677
1678/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001679 * Receive a frame from the socket and optionally record the address of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 * sender. We verify the buffers are writable and if needed move the
1681 * sender address from kernel to user space.
1682 */
1683
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001684SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, ubuf, size_t, size,
Eric Dumazet95c96172012-04-15 05:58:06 +00001685 unsigned int, flags, struct sockaddr __user *, addr,
Heiko Carstens3e0fa652009-01-14 14:14:24 +01001686 int __user *, addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687{
1688 struct socket *sock;
1689 struct iovec iov;
1690 struct msghdr msg;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001691 struct sockaddr_storage address;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001692 int err, err2;
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001693 int fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
Al Viro602bd0e2015-03-21 19:12:32 -04001695 err = import_single_range(READ, ubuf, size, &iov, &msg.msg_iter);
1696 if (unlikely(err))
1697 return err;
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001698 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 if (!sock)
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001700 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001702 msg.msg_control = NULL;
1703 msg.msg_controllen = 0;
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01001704 /* Save some cycles and don't copy the address if not needed */
1705 msg.msg_name = addr ? (struct sockaddr *)&address : NULL;
1706 /* We assume all kernel code knows the size of sockaddr_storage */
1707 msg.msg_namelen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 if (sock->file->f_flags & O_NONBLOCK)
1709 flags |= MSG_DONTWAIT;
Al Viro602bd0e2015-03-21 19:12:32 -04001710 err = sock_recvmsg(sock, &msg, iov_iter_count(&msg.msg_iter), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001712 if (err >= 0 && addr != NULL) {
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00001713 err2 = move_addr_to_user(&address,
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001714 msg.msg_namelen, addr, addr_len);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001715 if (err2 < 0)
1716 err = err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 }
Pavel Emelyanovde0fa952007-11-14 16:01:43 -08001718
1719 fput_light(sock->file, fput_needed);
David S. Miller4387ff72007-02-08 15:06:08 -08001720out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 return err;
1722}
1723
1724/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001725 * Receive a datagram from a socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 */
1727
Jan Glauberb7c0ddf2014-04-16 09:32:48 +02001728SYSCALL_DEFINE4(recv, int, fd, void __user *, ubuf, size_t, size,
1729 unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730{
1731 return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
1732}
1733
1734/*
1735 * Set a socket option. Because we don't know the option lengths we have
1736 * to pass the user mode parameter for the protocols to sort out.
1737 */
1738
Heiko Carstens20f37032009-01-14 14:14:23 +01001739SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname,
1740 char __user *, optval, int, optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001742 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 struct socket *sock;
1744
1745 if (optlen < 0)
1746 return -EINVAL;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001747
1748 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1749 if (sock != NULL) {
1750 err = security_socket_setsockopt(sock, level, optname);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001751 if (err)
1752 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
1754 if (level == SOL_SOCKET)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001755 err =
1756 sock_setsockopt(sock, level, optname, optval,
1757 optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 else
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001759 err =
1760 sock->ops->setsockopt(sock, level, optname, optval,
1761 optlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001762out_put:
1763 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 }
1765 return err;
1766}
1767
1768/*
1769 * Get a socket option. Because we don't know the option lengths we have
1770 * to pass a user mode parameter for the protocols to sort out.
1771 */
1772
Heiko Carstens20f37032009-01-14 14:14:23 +01001773SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname,
1774 char __user *, optval, int __user *, optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001776 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 struct socket *sock;
1778
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001779 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1780 if (sock != NULL) {
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001781 err = security_socket_getsockopt(sock, level, optname);
1782 if (err)
1783 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
1785 if (level == SOL_SOCKET)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001786 err =
1787 sock_getsockopt(sock, level, optname, optval,
1788 optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 else
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001790 err =
1791 sock->ops->getsockopt(sock, level, optname, optval,
1792 optlen);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001793out_put:
1794 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 }
1796 return err;
1797}
1798
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799/*
1800 * Shutdown a socket.
1801 */
1802
Heiko Carstens754fe8d2009-01-14 14:14:09 +01001803SYSCALL_DEFINE2(shutdown, int, fd, int, how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804{
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001805 int err, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 struct socket *sock;
1807
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001808 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1809 if (sock != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 err = security_socket_shutdown(sock, how);
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001811 if (!err)
1812 err = sock->ops->shutdown(sock, how);
1813 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 }
1815 return err;
1816}
1817
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001818/* A couple of helpful macros for getting the address of the 32/64 bit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 * fields which are the same type (int / unsigned) on our platforms.
1820 */
1821#define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
1822#define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
1823#define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
1824
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00001825struct used_address {
1826 struct sockaddr_storage name;
1827 unsigned int name_len;
1828};
1829
Al Viroda184282015-03-21 19:29:06 -04001830static int copy_msghdr_from_user(struct msghdr *kmsg,
1831 struct user_msghdr __user *umsg,
1832 struct sockaddr __user **save_addr,
1833 struct iovec **iov)
Dan Carpenter1661bf32013-10-03 00:27:20 +03001834{
Al Viro08adb7d2014-11-10 20:23:13 -05001835 struct sockaddr __user *uaddr;
1836 struct iovec __user *uiov;
Al Viroc0371da2014-11-24 10:42:55 -05001837 size_t nr_segs;
Al Viro08adb7d2014-11-10 20:23:13 -05001838 ssize_t err;
1839
1840 if (!access_ok(VERIFY_READ, umsg, sizeof(*umsg)) ||
1841 __get_user(uaddr, &umsg->msg_name) ||
1842 __get_user(kmsg->msg_namelen, &umsg->msg_namelen) ||
1843 __get_user(uiov, &umsg->msg_iov) ||
Al Viroc0371da2014-11-24 10:42:55 -05001844 __get_user(nr_segs, &umsg->msg_iovlen) ||
Al Viro08adb7d2014-11-10 20:23:13 -05001845 __get_user(kmsg->msg_control, &umsg->msg_control) ||
1846 __get_user(kmsg->msg_controllen, &umsg->msg_controllen) ||
1847 __get_user(kmsg->msg_flags, &umsg->msg_flags))
Dan Carpenter1661bf32013-10-03 00:27:20 +03001848 return -EFAULT;
Matthew Leachdbb490b2014-03-11 11:58:27 +00001849
Al Viro08adb7d2014-11-10 20:23:13 -05001850 if (!uaddr)
Ani Sinha6a2a2b32014-09-08 14:49:59 -07001851 kmsg->msg_namelen = 0;
1852
Matthew Leachdbb490b2014-03-11 11:58:27 +00001853 if (kmsg->msg_namelen < 0)
1854 return -EINVAL;
1855
Dan Carpenter1661bf32013-10-03 00:27:20 +03001856 if (kmsg->msg_namelen > sizeof(struct sockaddr_storage))
Dan Carpenterdb31c552013-11-27 15:40:21 +03001857 kmsg->msg_namelen = sizeof(struct sockaddr_storage);
Al Viro08adb7d2014-11-10 20:23:13 -05001858
1859 if (save_addr)
1860 *save_addr = uaddr;
1861
1862 if (uaddr && kmsg->msg_namelen) {
1863 if (!save_addr) {
1864 err = move_addr_to_kernel(uaddr, kmsg->msg_namelen,
1865 kmsg->msg_name);
1866 if (err < 0)
1867 return err;
1868 }
1869 } else {
1870 kmsg->msg_name = NULL;
1871 kmsg->msg_namelen = 0;
1872 }
1873
Al Viroc0371da2014-11-24 10:42:55 -05001874 if (nr_segs > UIO_MAXIOV)
Al Viro08adb7d2014-11-10 20:23:13 -05001875 return -EMSGSIZE;
1876
tadeusz.struk@intel.com0345f932015-03-19 12:31:25 -07001877 kmsg->msg_iocb = NULL;
1878
Al Viroda184282015-03-21 19:29:06 -04001879 return import_iovec(save_addr ? READ : WRITE, uiov, nr_segs,
1880 UIO_FASTIOV, iov, &kmsg->msg_iter);
Dan Carpenter1661bf32013-10-03 00:27:20 +03001881}
1882
Al Viro666547f2014-04-06 14:03:05 -04001883static int ___sys_sendmsg(struct socket *sock, struct user_msghdr __user *msg,
Eric Dumazet95c96172012-04-15 05:58:06 +00001884 struct msghdr *msg_sys, unsigned int flags,
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00001885 struct used_address *used_address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886{
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001887 struct compat_msghdr __user *msg_compat =
1888 (struct compat_msghdr __user *)msg;
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07001889 struct sockaddr_storage address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
Alex Williamsonb9d717a2005-09-26 14:28:02 -07001891 unsigned char ctl[sizeof(struct cmsghdr) + 20]
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001892 __attribute__ ((aligned(sizeof(__kernel_size_t))));
1893 /* 20 is size of ipv6_pktinfo */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 unsigned char *ctl_buf = ctl;
Al Virod8725c82014-12-11 00:02:50 -05001895 int ctl_len;
Al Viro08adb7d2014-11-10 20:23:13 -05001896 ssize_t err;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001897
Al Viro08adb7d2014-11-10 20:23:13 -05001898 msg_sys->msg_name = &address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
Al Viro08449322014-11-09 22:33:45 -05001900 if (MSG_CMSG_COMPAT & flags)
Al Viro08adb7d2014-11-10 20:23:13 -05001901 err = get_compat_msghdr(msg_sys, msg_compat, NULL, &iov);
Al Viro08449322014-11-09 22:33:45 -05001902 else
Al Viro08adb7d2014-11-10 20:23:13 -05001903 err = copy_msghdr_from_user(msg_sys, msg, NULL, &iov);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001904 if (err < 0)
Al Viroda184282015-03-21 19:29:06 -04001905 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
1907 err = -ENOBUFS;
1908
Anton Blanchard228e5482011-05-02 20:21:35 +00001909 if (msg_sys->msg_controllen > INT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 goto out_freeiov;
Anton Blanchard228e5482011-05-02 20:21:35 +00001911 ctl_len = msg_sys->msg_controllen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001913 err =
Anton Blanchard228e5482011-05-02 20:21:35 +00001914 cmsghdr_from_user_compat_to_kern(msg_sys, sock->sk, ctl,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001915 sizeof(ctl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 if (err)
1917 goto out_freeiov;
Anton Blanchard228e5482011-05-02 20:21:35 +00001918 ctl_buf = msg_sys->msg_control;
1919 ctl_len = msg_sys->msg_controllen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 } else if (ctl_len) {
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001921 if (ctl_len > sizeof(ctl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001923 if (ctl_buf == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 goto out_freeiov;
1925 }
1926 err = -EFAULT;
1927 /*
Anton Blanchard228e5482011-05-02 20:21:35 +00001928 * Careful! Before this, msg_sys->msg_control contains a user pointer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
1930 * checking falls down on this.
1931 */
Namhyung Kimfb8621b2010-09-07 03:55:00 +00001932 if (copy_from_user(ctl_buf,
Anton Blanchard228e5482011-05-02 20:21:35 +00001933 (void __user __force *)msg_sys->msg_control,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001934 ctl_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 goto out_freectl;
Anton Blanchard228e5482011-05-02 20:21:35 +00001936 msg_sys->msg_control = ctl_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 }
Anton Blanchard228e5482011-05-02 20:21:35 +00001938 msg_sys->msg_flags = flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
1940 if (sock->file->f_flags & O_NONBLOCK)
Anton Blanchard228e5482011-05-02 20:21:35 +00001941 msg_sys->msg_flags |= MSG_DONTWAIT;
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00001942 /*
1943 * If this is sendmmsg() and current destination address is same as
1944 * previously succeeded address, omit asking LSM's decision.
1945 * used_address->name_len is initialized to UINT_MAX so that the first
1946 * destination address never matches.
1947 */
Mathieu Desnoyersbc909d92011-08-24 19:45:03 -07001948 if (used_address && msg_sys->msg_name &&
1949 used_address->name_len == msg_sys->msg_namelen &&
1950 !memcmp(&used_address->name, msg_sys->msg_name,
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00001951 used_address->name_len)) {
Al Virod8725c82014-12-11 00:02:50 -05001952 err = sock_sendmsg_nosec(sock, msg_sys);
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00001953 goto out_freectl;
1954 }
Al Virod8725c82014-12-11 00:02:50 -05001955 err = sock_sendmsg(sock, msg_sys);
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00001956 /*
1957 * If this is sendmmsg() and sending to current destination address was
1958 * successful, remember it.
1959 */
1960 if (used_address && err >= 0) {
1961 used_address->name_len = msg_sys->msg_namelen;
Mathieu Desnoyersbc909d92011-08-24 19:45:03 -07001962 if (msg_sys->msg_name)
1963 memcpy(&used_address->name, msg_sys->msg_name,
1964 used_address->name_len);
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00001965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
1967out_freectl:
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001968 if (ctl_buf != ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 sock_kfree_s(sock->sk, ctl_buf, ctl_len);
1970out_freeiov:
Al Viroda184282015-03-21 19:29:06 -04001971 kfree(iov);
Anton Blanchard228e5482011-05-02 20:21:35 +00001972 return err;
1973}
1974
1975/*
1976 * BSD sendmsg interface
1977 */
1978
Al Viro666547f2014-04-06 14:03:05 -04001979long __sys_sendmsg(int fd, struct user_msghdr __user *msg, unsigned flags)
Anton Blanchard228e5482011-05-02 20:21:35 +00001980{
1981 int fput_needed, err;
1982 struct msghdr msg_sys;
Andy Lutomirski1be374a2013-05-22 14:07:44 -07001983 struct socket *sock;
Anton Blanchard228e5482011-05-02 20:21:35 +00001984
Andy Lutomirski1be374a2013-05-22 14:07:44 -07001985 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Anton Blanchard228e5482011-05-02 20:21:35 +00001986 if (!sock)
1987 goto out;
1988
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00001989 err = ___sys_sendmsg(sock, msg, &msg_sys, flags, NULL);
Anton Blanchard228e5482011-05-02 20:21:35 +00001990
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08001991 fput_light(sock->file, fput_needed);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07001992out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 return err;
1994}
1995
Al Viro666547f2014-04-06 14:03:05 -04001996SYSCALL_DEFINE3(sendmsg, int, fd, struct user_msghdr __user *, msg, unsigned int, flags)
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00001997{
1998 if (flags & MSG_CMSG_COMPAT)
1999 return -EINVAL;
2000 return __sys_sendmsg(fd, msg, flags);
2001}
2002
Anton Blanchard228e5482011-05-02 20:21:35 +00002003/*
2004 * Linux sendmmsg interface
2005 */
2006
2007int __sys_sendmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
2008 unsigned int flags)
2009{
2010 int fput_needed, err, datagrams;
2011 struct socket *sock;
2012 struct mmsghdr __user *entry;
2013 struct compat_mmsghdr __user *compat_entry;
2014 struct msghdr msg_sys;
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002015 struct used_address used_address;
Anton Blanchard228e5482011-05-02 20:21:35 +00002016
Anton Blanchard98382f42011-08-04 14:07:39 +00002017 if (vlen > UIO_MAXIOV)
2018 vlen = UIO_MAXIOV;
Anton Blanchard228e5482011-05-02 20:21:35 +00002019
2020 datagrams = 0;
2021
2022 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2023 if (!sock)
2024 return err;
2025
Tetsuo Handac71d8eb2011-08-04 14:07:40 +00002026 used_address.name_len = UINT_MAX;
Anton Blanchard228e5482011-05-02 20:21:35 +00002027 entry = mmsg;
2028 compat_entry = (struct compat_mmsghdr __user *)mmsg;
Anton Blanchard728ffb82011-08-04 14:07:38 +00002029 err = 0;
Anton Blanchard228e5482011-05-02 20:21:35 +00002030
2031 while (datagrams < vlen) {
Anton Blanchard228e5482011-05-02 20:21:35 +00002032 if (MSG_CMSG_COMPAT & flags) {
Al Viro666547f2014-04-06 14:03:05 -04002033 err = ___sys_sendmsg(sock, (struct user_msghdr __user *)compat_entry,
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002034 &msg_sys, flags, &used_address);
Anton Blanchard228e5482011-05-02 20:21:35 +00002035 if (err < 0)
2036 break;
2037 err = __put_user(err, &compat_entry->msg_len);
2038 ++compat_entry;
2039 } else {
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002040 err = ___sys_sendmsg(sock,
Al Viro666547f2014-04-06 14:03:05 -04002041 (struct user_msghdr __user *)entry,
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002042 &msg_sys, flags, &used_address);
Anton Blanchard228e5482011-05-02 20:21:35 +00002043 if (err < 0)
2044 break;
2045 err = put_user(err, &entry->msg_len);
2046 ++entry;
2047 }
2048
2049 if (err)
2050 break;
2051 ++datagrams;
2052 }
2053
Anton Blanchard228e5482011-05-02 20:21:35 +00002054 fput_light(sock->file, fput_needed);
2055
Anton Blanchard728ffb82011-08-04 14:07:38 +00002056 /* We only return an error if no datagrams were able to be sent */
2057 if (datagrams != 0)
Anton Blanchard228e5482011-05-02 20:21:35 +00002058 return datagrams;
2059
Anton Blanchard228e5482011-05-02 20:21:35 +00002060 return err;
2061}
2062
2063SYSCALL_DEFINE4(sendmmsg, int, fd, struct mmsghdr __user *, mmsg,
2064 unsigned int, vlen, unsigned int, flags)
2065{
Andy Lutomirski1be374a2013-05-22 14:07:44 -07002066 if (flags & MSG_CMSG_COMPAT)
2067 return -EINVAL;
Anton Blanchard228e5482011-05-02 20:21:35 +00002068 return __sys_sendmmsg(fd, mmsg, vlen, flags);
2069}
2070
Al Viro666547f2014-04-06 14:03:05 -04002071static int ___sys_recvmsg(struct socket *sock, struct user_msghdr __user *msg,
Eric Dumazet95c96172012-04-15 05:58:06 +00002072 struct msghdr *msg_sys, unsigned int flags, int nosec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073{
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002074 struct compat_msghdr __user *msg_compat =
2075 (struct compat_msghdr __user *)msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 struct iovec iovstack[UIO_FASTIOV];
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002077 struct iovec *iov = iovstack;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 unsigned long cmsg_ptr;
Al Viro08adb7d2014-11-10 20:23:13 -05002079 int total_len, len;
2080 ssize_t err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
2082 /* kernel mode address */
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -07002083 struct sockaddr_storage addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
2085 /* user mode address pointers */
2086 struct sockaddr __user *uaddr;
Al Viro08adb7d2014-11-10 20:23:13 -05002087 int __user *uaddr_len = COMPAT_NAMELEN(msg);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002088
Al Viro08adb7d2014-11-10 20:23:13 -05002089 msg_sys->msg_name = &addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01002091 if (MSG_CMSG_COMPAT & flags)
Al Viro08adb7d2014-11-10 20:23:13 -05002092 err = get_compat_msghdr(msg_sys, msg_compat, &uaddr, &iov);
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01002093 else
Al Viro08adb7d2014-11-10 20:23:13 -05002094 err = copy_msghdr_from_user(msg_sys, msg, &uaddr, &iov);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 if (err < 0)
Al Viroda184282015-03-21 19:29:06 -04002096 return err;
2097 total_len = iov_iter_count(&msg_sys->msg_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002099 cmsg_ptr = (unsigned long)msg_sys->msg_control;
2100 msg_sys->msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002101
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01002102 /* We assume all kernel code knows the size of sockaddr_storage */
2103 msg_sys->msg_namelen = 0;
2104
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 if (sock->file->f_flags & O_NONBLOCK)
2106 flags |= MSG_DONTWAIT;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002107 err = (nosec ? sock_recvmsg_nosec : sock_recvmsg)(sock, msg_sys,
2108 total_len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 if (err < 0)
2110 goto out_freeiov;
2111 len = err;
2112
2113 if (uaddr != NULL) {
Maciej Żenczykowski43db3622012-03-11 12:51:50 +00002114 err = move_addr_to_user(&addr,
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002115 msg_sys->msg_namelen, uaddr,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002116 uaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 if (err < 0)
2118 goto out_freeiov;
2119 }
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002120 err = __put_user((msg_sys->msg_flags & ~MSG_CMSG_COMPAT),
David S. Miller37f7f422005-09-16 16:51:01 -07002121 COMPAT_FLAGS(msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 if (err)
2123 goto out_freeiov;
2124 if (MSG_CMSG_COMPAT & flags)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002125 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 &msg_compat->msg_controllen);
2127 else
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002128 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 &msg->msg_controllen);
2130 if (err)
2131 goto out_freeiov;
2132 err = len;
2133
2134out_freeiov:
Al Viroda184282015-03-21 19:29:06 -04002135 kfree(iov);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002136 return err;
2137}
2138
2139/*
2140 * BSD recvmsg interface
2141 */
2142
Al Viro666547f2014-04-06 14:03:05 -04002143long __sys_recvmsg(int fd, struct user_msghdr __user *msg, unsigned flags)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002144{
2145 int fput_needed, err;
2146 struct msghdr msg_sys;
Andy Lutomirski1be374a2013-05-22 14:07:44 -07002147 struct socket *sock;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002148
Andy Lutomirski1be374a2013-05-22 14:07:44 -07002149 sock = sockfd_lookup_light(fd, &err, &fput_needed);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002150 if (!sock)
2151 goto out;
2152
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002153 err = ___sys_recvmsg(sock, msg, &msg_sys, flags, 0);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002154
Benjamin LaHaise6cb153c2006-03-20 22:27:12 -08002155 fput_light(sock->file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156out:
2157 return err;
2158}
2159
Al Viro666547f2014-04-06 14:03:05 -04002160SYSCALL_DEFINE3(recvmsg, int, fd, struct user_msghdr __user *, msg,
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002161 unsigned int, flags)
2162{
2163 if (flags & MSG_CMSG_COMPAT)
2164 return -EINVAL;
2165 return __sys_recvmsg(fd, msg, flags);
2166}
2167
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002168/*
2169 * Linux recvmmsg interface
2170 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002172int __sys_recvmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
2173 unsigned int flags, struct timespec *timeout)
2174{
2175 int fput_needed, err, datagrams;
2176 struct socket *sock;
2177 struct mmsghdr __user *entry;
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002178 struct compat_mmsghdr __user *compat_entry;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002179 struct msghdr msg_sys;
2180 struct timespec end_time;
2181
2182 if (timeout &&
2183 poll_select_set_timeout(&end_time, timeout->tv_sec,
2184 timeout->tv_nsec))
2185 return -EINVAL;
2186
2187 datagrams = 0;
2188
2189 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2190 if (!sock)
2191 return err;
2192
2193 err = sock_error(sock->sk);
2194 if (err)
2195 goto out_put;
2196
2197 entry = mmsg;
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002198 compat_entry = (struct compat_mmsghdr __user *)mmsg;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002199
2200 while (datagrams < vlen) {
2201 /*
2202 * No need to ask LSM for more than the first datagram.
2203 */
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002204 if (MSG_CMSG_COMPAT & flags) {
Al Viro666547f2014-04-06 14:03:05 -04002205 err = ___sys_recvmsg(sock, (struct user_msghdr __user *)compat_entry,
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002206 &msg_sys, flags & ~MSG_WAITFORONE,
2207 datagrams);
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002208 if (err < 0)
2209 break;
2210 err = __put_user(err, &compat_entry->msg_len);
2211 ++compat_entry;
2212 } else {
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002213 err = ___sys_recvmsg(sock,
Al Viro666547f2014-04-06 14:03:05 -04002214 (struct user_msghdr __user *)entry,
Andy Lutomirskia7526eb2013-06-05 19:38:26 +00002215 &msg_sys, flags & ~MSG_WAITFORONE,
2216 datagrams);
Jean-Mickael Guerind7256d02009-12-01 08:47:26 +00002217 if (err < 0)
2218 break;
2219 err = put_user(err, &entry->msg_len);
2220 ++entry;
2221 }
2222
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002223 if (err)
2224 break;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002225 ++datagrams;
2226
Brandon L Black71c5c152010-03-26 16:18:03 +00002227 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
2228 if (flags & MSG_WAITFORONE)
2229 flags |= MSG_DONTWAIT;
2230
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002231 if (timeout) {
2232 ktime_get_ts(timeout);
2233 *timeout = timespec_sub(end_time, *timeout);
2234 if (timeout->tv_sec < 0) {
2235 timeout->tv_sec = timeout->tv_nsec = 0;
2236 break;
2237 }
2238
2239 /* Timeout, return less than vlen datagrams */
2240 if (timeout->tv_nsec == 0 && timeout->tv_sec == 0)
2241 break;
2242 }
2243
2244 /* Out of band data, return right away */
2245 if (msg_sys.msg_flags & MSG_OOB)
2246 break;
2247 }
2248
2249out_put:
2250 fput_light(sock->file, fput_needed);
2251
2252 if (err == 0)
2253 return datagrams;
2254
2255 if (datagrams != 0) {
2256 /*
2257 * We may return less entries than requested (vlen) if the
2258 * sock is non block and there aren't enough datagrams...
2259 */
2260 if (err != -EAGAIN) {
2261 /*
2262 * ... or if recvmsg returns an error after we
2263 * received some datagrams, where we record the
2264 * error to return on the next call or if the
2265 * app asks about it using getsockopt(SO_ERROR).
2266 */
2267 sock->sk->sk_err = -err;
2268 }
2269
2270 return datagrams;
2271 }
2272
2273 return err;
2274}
2275
2276SYSCALL_DEFINE5(recvmmsg, int, fd, struct mmsghdr __user *, mmsg,
2277 unsigned int, vlen, unsigned int, flags,
2278 struct timespec __user *, timeout)
2279{
2280 int datagrams;
2281 struct timespec timeout_sys;
2282
Andy Lutomirski1be374a2013-05-22 14:07:44 -07002283 if (flags & MSG_CMSG_COMPAT)
2284 return -EINVAL;
2285
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002286 if (!timeout)
2287 return __sys_recvmmsg(fd, mmsg, vlen, flags, NULL);
2288
2289 if (copy_from_user(&timeout_sys, timeout, sizeof(timeout_sys)))
2290 return -EFAULT;
2291
2292 datagrams = __sys_recvmmsg(fd, mmsg, vlen, flags, &timeout_sys);
2293
2294 if (datagrams > 0 &&
2295 copy_to_user(timeout, &timeout_sys, sizeof(timeout_sys)))
2296 datagrams = -EFAULT;
2297
2298 return datagrams;
2299}
2300
2301#ifdef __ARCH_WANT_SYS_SOCKETCALL
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302/* Argument list sizes for sys_socketcall */
2303#define AL(x) ((x) * sizeof(unsigned long))
Anton Blanchard228e5482011-05-02 20:21:35 +00002304static const unsigned char nargs[21] = {
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002305 AL(0), AL(3), AL(3), AL(3), AL(2), AL(3),
2306 AL(3), AL(3), AL(4), AL(4), AL(4), AL(6),
2307 AL(6), AL(2), AL(5), AL(5), AL(3), AL(3),
Anton Blanchard228e5482011-05-02 20:21:35 +00002308 AL(4), AL(5), AL(4)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002309};
2310
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311#undef AL
2312
2313/*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002314 * System call vectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 *
2316 * Argument checking cleaned up. Saved 20% in size.
2317 * This function doesn't need to set the kernel lock because
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002318 * it is set by the callees.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 */
2320
Heiko Carstens3e0fa652009-01-14 14:14:24 +01002321SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322{
Chen Gang2950fa92013-04-07 16:55:23 +08002323 unsigned long a[AUDITSC_ARGS];
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002324 unsigned long a0, a1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 int err;
Arjan van de Ven47379052009-09-28 12:57:44 -07002326 unsigned int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
Anton Blanchard228e5482011-05-02 20:21:35 +00002328 if (call < 1 || call > SYS_SENDMMSG)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 return -EINVAL;
2330
Arjan van de Ven47379052009-09-28 12:57:44 -07002331 len = nargs[call];
2332 if (len > sizeof(a))
2333 return -EINVAL;
2334
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 /* copy_from_user should be SMP safe. */
Arjan van de Ven47379052009-09-28 12:57:44 -07002336 if (copy_from_user(a, args, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 return -EFAULT;
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002338
Chen Gang2950fa92013-04-07 16:55:23 +08002339 err = audit_socketcall(nargs[call] / sizeof(unsigned long), a);
2340 if (err)
2341 return err;
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002342
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002343 a0 = a[0];
2344 a1 = a[1];
2345
2346 switch (call) {
2347 case SYS_SOCKET:
2348 err = sys_socket(a0, a1, a[2]);
2349 break;
2350 case SYS_BIND:
2351 err = sys_bind(a0, (struct sockaddr __user *)a1, a[2]);
2352 break;
2353 case SYS_CONNECT:
2354 err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
2355 break;
2356 case SYS_LISTEN:
2357 err = sys_listen(a0, a1);
2358 break;
2359 case SYS_ACCEPT:
Ulrich Drepperde11def2008-11-19 15:36:14 -08002360 err = sys_accept4(a0, (struct sockaddr __user *)a1,
2361 (int __user *)a[2], 0);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002362 break;
2363 case SYS_GETSOCKNAME:
2364 err =
2365 sys_getsockname(a0, (struct sockaddr __user *)a1,
2366 (int __user *)a[2]);
2367 break;
2368 case SYS_GETPEERNAME:
2369 err =
2370 sys_getpeername(a0, (struct sockaddr __user *)a1,
2371 (int __user *)a[2]);
2372 break;
2373 case SYS_SOCKETPAIR:
2374 err = sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
2375 break;
2376 case SYS_SEND:
2377 err = sys_send(a0, (void __user *)a1, a[2], a[3]);
2378 break;
2379 case SYS_SENDTO:
2380 err = sys_sendto(a0, (void __user *)a1, a[2], a[3],
2381 (struct sockaddr __user *)a[4], a[5]);
2382 break;
2383 case SYS_RECV:
2384 err = sys_recv(a0, (void __user *)a1, a[2], a[3]);
2385 break;
2386 case SYS_RECVFROM:
2387 err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
2388 (struct sockaddr __user *)a[4],
2389 (int __user *)a[5]);
2390 break;
2391 case SYS_SHUTDOWN:
2392 err = sys_shutdown(a0, a1);
2393 break;
2394 case SYS_SETSOCKOPT:
2395 err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]);
2396 break;
2397 case SYS_GETSOCKOPT:
2398 err =
2399 sys_getsockopt(a0, a1, a[2], (char __user *)a[3],
2400 (int __user *)a[4]);
2401 break;
2402 case SYS_SENDMSG:
Al Viro666547f2014-04-06 14:03:05 -04002403 err = sys_sendmsg(a0, (struct user_msghdr __user *)a1, a[2]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002404 break;
Anton Blanchard228e5482011-05-02 20:21:35 +00002405 case SYS_SENDMMSG:
2406 err = sys_sendmmsg(a0, (struct mmsghdr __user *)a1, a[2], a[3]);
2407 break;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002408 case SYS_RECVMSG:
Al Viro666547f2014-04-06 14:03:05 -04002409 err = sys_recvmsg(a0, (struct user_msghdr __user *)a1, a[2]);
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002410 break;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -07002411 case SYS_RECVMMSG:
2412 err = sys_recvmmsg(a0, (struct mmsghdr __user *)a1, a[2], a[3],
2413 (struct timespec __user *)a[4]);
2414 break;
Ulrich Drepperde11def2008-11-19 15:36:14 -08002415 case SYS_ACCEPT4:
2416 err = sys_accept4(a0, (struct sockaddr __user *)a1,
2417 (int __user *)a[2], a[3]);
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -07002418 break;
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002419 default:
2420 err = -EINVAL;
2421 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 }
2423 return err;
2424}
2425
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002426#endif /* __ARCH_WANT_SYS_SOCKETCALL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002428/**
2429 * sock_register - add a socket protocol handler
2430 * @ops: description of protocol
2431 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 * This function is called by a protocol handler that wants to
2433 * advertise its address family, and have it linked into the
Masanari Iidae793c0f2014-09-04 23:44:36 +09002434 * socket interface. The value ops->family corresponds to the
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002435 * socket system call protocol family.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 */
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002437int sock_register(const struct net_proto_family *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438{
2439 int err;
2440
2441 if (ops->family >= NPROTO) {
Yang Yingliang3410f222014-02-12 17:09:55 +08002442 pr_crit("protocol %d >= NPROTO(%d)\n", ops->family, NPROTO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 return -ENOBUFS;
2444 }
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002445
2446 spin_lock(&net_family_lock);
Eric Dumazet190683a2010-11-10 10:50:44 +00002447 if (rcu_dereference_protected(net_families[ops->family],
2448 lockdep_is_held(&net_family_lock)))
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002449 err = -EEXIST;
2450 else {
Eric Dumazetcf778b02012-01-12 04:41:32 +00002451 rcu_assign_pointer(net_families[ops->family], ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 err = 0;
2453 }
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002454 spin_unlock(&net_family_lock);
2455
Yang Yingliang3410f222014-02-12 17:09:55 +08002456 pr_info("NET: Registered protocol family %d\n", ops->family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 return err;
2458}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002459EXPORT_SYMBOL(sock_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002461/**
2462 * sock_unregister - remove a protocol handler
2463 * @family: protocol family to remove
2464 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 * This function is called by a protocol handler that wants to
2466 * remove its address family, and have it unlinked from the
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002467 * new socket creation.
2468 *
2469 * If protocol handler is a module, then it can use module reference
2470 * counts to protect against new references. If protocol handler is not
2471 * a module then it needs to provide its own protection in
2472 * the ops->create routine.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 */
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002474void sock_unregister(int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475{
Stephen Hemmingerf0fd27d2006-08-09 21:03:17 -07002476 BUG_ON(family < 0 || family >= NPROTO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002478 spin_lock(&net_family_lock);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00002479 RCU_INIT_POINTER(net_families[family], NULL);
Stephen Hemminger55737fd2006-09-01 00:23:39 -07002480 spin_unlock(&net_family_lock);
2481
2482 synchronize_rcu();
2483
Yang Yingliang3410f222014-02-12 17:09:55 +08002484 pr_info("NET: Unregistered protocol family %d\n", family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002486EXPORT_SYMBOL(sock_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487
Andi Kleen77d76ea2005-12-22 12:43:42 -08002488static int __init sock_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489{
Nick Pigginb3e19d92011-01-07 17:50:11 +11002490 int err;
Eric W. Biederman2ca794e2012-04-19 13:20:32 +00002491 /*
2492 * Initialize the network sysctl infrastructure.
2493 */
2494 err = net_sysctl_init();
2495 if (err)
2496 goto out;
Nick Pigginb3e19d92011-01-07 17:50:11 +11002497
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002499 * Initialize skbuff SLAB cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 */
2501 skb_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502
2503 /*
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002504 * Initialize the protocols module.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 */
2506
2507 init_inodecache();
Nick Pigginb3e19d92011-01-07 17:50:11 +11002508
2509 err = register_filesystem(&sock_fs_type);
2510 if (err)
2511 goto out_fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 sock_mnt = kern_mount(&sock_fs_type);
Nick Pigginb3e19d92011-01-07 17:50:11 +11002513 if (IS_ERR(sock_mnt)) {
2514 err = PTR_ERR(sock_mnt);
2515 goto out_mount;
2516 }
Andi Kleen77d76ea2005-12-22 12:43:42 -08002517
2518 /* The real protocol initialization is performed in later initcalls.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 */
2520
2521#ifdef CONFIG_NETFILTER
Pablo Neira Ayuso6d11cfd2013-05-22 22:42:36 +00002522 err = netfilter_init();
2523 if (err)
2524 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525#endif
David S. Millercbeb3212005-12-22 12:58:55 -08002526
Daniel Borkmann408eccc2014-04-01 16:20:23 +02002527 ptp_classifier_init();
Richard Cochranc1f19b52010-07-17 08:49:36 +00002528
Nick Pigginb3e19d92011-01-07 17:50:11 +11002529out:
2530 return err;
2531
2532out_mount:
2533 unregister_filesystem(&sock_fs_type);
2534out_fs:
2535 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536}
2537
Andi Kleen77d76ea2005-12-22 12:43:42 -08002538core_initcall(sock_init); /* early initcall */
2539
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540#ifdef CONFIG_PROC_FS
2541void socket_seq_show(struct seq_file *seq)
2542{
2543 int cpu;
2544 int counter = 0;
2545
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -07002546 for_each_possible_cpu(cpu)
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002547 counter += per_cpu(sockets_in_use, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548
2549 /* It can be negative, by the way. 8) */
2550 if (counter < 0)
2551 counter = 0;
2552
2553 seq_printf(seq, "sockets: used %d\n", counter);
2554}
Stephen Hemminger89bddce2006-09-01 00:19:31 -07002555#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556
Shaun Pereira89bbfc92006-03-21 23:58:08 -08002557#ifdef CONFIG_COMPAT
Arnd Bergmann6b960182009-11-06 23:10:54 -08002558static int do_siocgstamp(struct net *net, struct socket *sock,
H. Peter Anvin644595f2012-02-19 17:51:59 -08002559 unsigned int cmd, void __user *up)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002560{
Arnd Bergmann7a229382009-11-06 23:00:29 -08002561 mm_segment_t old_fs = get_fs();
2562 struct timeval ktv;
2563 int err;
2564
2565 set_fs(KERNEL_DS);
Arnd Bergmann6b960182009-11-06 23:10:54 -08002566 err = sock_do_ioctl(net, sock, cmd, (unsigned long)&ktv);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002567 set_fs(old_fs);
H. Peter Anvin644595f2012-02-19 17:51:59 -08002568 if (!err)
Mikulas Patockaed6fe9d2012-09-01 12:34:07 -04002569 err = compat_put_timeval(&ktv, up);
H. Peter Anvin644595f2012-02-19 17:51:59 -08002570
Arnd Bergmann7a229382009-11-06 23:00:29 -08002571 return err;
2572}
2573
Arnd Bergmann6b960182009-11-06 23:10:54 -08002574static int do_siocgstampns(struct net *net, struct socket *sock,
H. Peter Anvin644595f2012-02-19 17:51:59 -08002575 unsigned int cmd, void __user *up)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002576{
Arnd Bergmann7a229382009-11-06 23:00:29 -08002577 mm_segment_t old_fs = get_fs();
2578 struct timespec kts;
2579 int err;
2580
2581 set_fs(KERNEL_DS);
Arnd Bergmann6b960182009-11-06 23:10:54 -08002582 err = sock_do_ioctl(net, sock, cmd, (unsigned long)&kts);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002583 set_fs(old_fs);
H. Peter Anvin644595f2012-02-19 17:51:59 -08002584 if (!err)
Mikulas Patockaed6fe9d2012-09-01 12:34:07 -04002585 err = compat_put_timespec(&kts, up);
H. Peter Anvin644595f2012-02-19 17:51:59 -08002586
Arnd Bergmann7a229382009-11-06 23:00:29 -08002587 return err;
2588}
2589
Arnd Bergmann6b960182009-11-06 23:10:54 -08002590static int dev_ifname32(struct net *net, struct compat_ifreq __user *uifr32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002591{
2592 struct ifreq __user *uifr;
2593 int err;
2594
2595 uifr = compat_alloc_user_space(sizeof(struct ifreq));
Arnd Bergmann6b960182009-11-06 23:10:54 -08002596 if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002597 return -EFAULT;
2598
Arnd Bergmann6b960182009-11-06 23:10:54 -08002599 err = dev_ioctl(net, SIOCGIFNAME, uifr);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002600 if (err)
2601 return err;
2602
Arnd Bergmann6b960182009-11-06 23:10:54 -08002603 if (copy_in_user(uifr32, uifr, sizeof(struct compat_ifreq)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002604 return -EFAULT;
2605
2606 return 0;
2607}
2608
Arnd Bergmann6b960182009-11-06 23:10:54 -08002609static int dev_ifconf(struct net *net, struct compat_ifconf __user *uifc32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002610{
Arnd Bergmann6b960182009-11-06 23:10:54 -08002611 struct compat_ifconf ifc32;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002612 struct ifconf ifc;
2613 struct ifconf __user *uifc;
Arnd Bergmann6b960182009-11-06 23:10:54 -08002614 struct compat_ifreq __user *ifr32;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002615 struct ifreq __user *ifr;
2616 unsigned int i, j;
2617 int err;
2618
Arnd Bergmann6b960182009-11-06 23:10:54 -08002619 if (copy_from_user(&ifc32, uifc32, sizeof(struct compat_ifconf)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002620 return -EFAULT;
2621
Mathias Krause43da5f22012-08-15 11:31:57 +00002622 memset(&ifc, 0, sizeof(ifc));
Arnd Bergmann7a229382009-11-06 23:00:29 -08002623 if (ifc32.ifcbuf == 0) {
2624 ifc32.ifc_len = 0;
2625 ifc.ifc_len = 0;
2626 ifc.ifc_req = NULL;
2627 uifc = compat_alloc_user_space(sizeof(struct ifconf));
2628 } else {
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002629 size_t len = ((ifc32.ifc_len / sizeof(struct compat_ifreq)) + 1) *
2630 sizeof(struct ifreq);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002631 uifc = compat_alloc_user_space(sizeof(struct ifconf) + len);
2632 ifc.ifc_len = len;
2633 ifr = ifc.ifc_req = (void __user *)(uifc + 1);
2634 ifr32 = compat_ptr(ifc32.ifcbuf);
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002635 for (i = 0; i < ifc32.ifc_len; i += sizeof(struct compat_ifreq)) {
Arnd Bergmann6b960182009-11-06 23:10:54 -08002636 if (copy_in_user(ifr, ifr32, sizeof(struct compat_ifreq)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002637 return -EFAULT;
2638 ifr++;
2639 ifr32++;
2640 }
2641 }
2642 if (copy_to_user(uifc, &ifc, sizeof(struct ifconf)))
2643 return -EFAULT;
2644
Arnd Bergmann6b960182009-11-06 23:10:54 -08002645 err = dev_ioctl(net, SIOCGIFCONF, uifc);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002646 if (err)
2647 return err;
2648
2649 if (copy_from_user(&ifc, uifc, sizeof(struct ifconf)))
2650 return -EFAULT;
2651
2652 ifr = ifc.ifc_req;
2653 ifr32 = compat_ptr(ifc32.ifcbuf);
2654 for (i = 0, j = 0;
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002655 i + sizeof(struct compat_ifreq) <= ifc32.ifc_len && j < ifc.ifc_len;
2656 i += sizeof(struct compat_ifreq), j += sizeof(struct ifreq)) {
2657 if (copy_in_user(ifr32, ifr, sizeof(struct compat_ifreq)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002658 return -EFAULT;
2659 ifr32++;
2660 ifr++;
2661 }
2662
2663 if (ifc32.ifcbuf == 0) {
2664 /* Translate from 64-bit structure multiple to
2665 * a 32-bit one.
2666 */
2667 i = ifc.ifc_len;
Arnd Bergmann6b960182009-11-06 23:10:54 -08002668 i = ((i / sizeof(struct ifreq)) * sizeof(struct compat_ifreq));
Arnd Bergmann7a229382009-11-06 23:00:29 -08002669 ifc32.ifc_len = i;
2670 } else {
2671 ifc32.ifc_len = i;
2672 }
Arnd Bergmann6b960182009-11-06 23:10:54 -08002673 if (copy_to_user(uifc32, &ifc32, sizeof(struct compat_ifconf)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002674 return -EFAULT;
2675
2676 return 0;
2677}
2678
Arnd Bergmann6b960182009-11-06 23:10:54 -08002679static int ethtool_ioctl(struct net *net, struct compat_ifreq __user *ifr32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002680{
Ben Hutchings3a7da392011-03-17 07:34:32 +00002681 struct compat_ethtool_rxnfc __user *compat_rxnfc;
2682 bool convert_in = false, convert_out = false;
2683 size_t buf_size = ALIGN(sizeof(struct ifreq), 8);
2684 struct ethtool_rxnfc __user *rxnfc;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002685 struct ifreq __user *ifr;
Ben Hutchings3a7da392011-03-17 07:34:32 +00002686 u32 rule_cnt = 0, actual_rule_cnt;
2687 u32 ethcmd;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002688 u32 data;
Ben Hutchings3a7da392011-03-17 07:34:32 +00002689 int ret;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002690
2691 if (get_user(data, &ifr32->ifr_ifru.ifru_data))
2692 return -EFAULT;
2693
Ben Hutchings3a7da392011-03-17 07:34:32 +00002694 compat_rxnfc = compat_ptr(data);
2695
2696 if (get_user(ethcmd, &compat_rxnfc->cmd))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002697 return -EFAULT;
2698
Ben Hutchings3a7da392011-03-17 07:34:32 +00002699 /* Most ethtool structures are defined without padding.
2700 * Unfortunately struct ethtool_rxnfc is an exception.
2701 */
2702 switch (ethcmd) {
2703 default:
2704 break;
2705 case ETHTOOL_GRXCLSRLALL:
2706 /* Buffer size is variable */
2707 if (get_user(rule_cnt, &compat_rxnfc->rule_cnt))
2708 return -EFAULT;
2709 if (rule_cnt > KMALLOC_MAX_SIZE / sizeof(u32))
2710 return -ENOMEM;
2711 buf_size += rule_cnt * sizeof(u32);
2712 /* fall through */
2713 case ETHTOOL_GRXRINGS:
2714 case ETHTOOL_GRXCLSRLCNT:
2715 case ETHTOOL_GRXCLSRULE:
Ben Hutchings55664f32012-01-03 12:04:51 +00002716 case ETHTOOL_SRXCLSRLINS:
Ben Hutchings3a7da392011-03-17 07:34:32 +00002717 convert_out = true;
2718 /* fall through */
2719 case ETHTOOL_SRXCLSRLDEL:
Ben Hutchings3a7da392011-03-17 07:34:32 +00002720 buf_size += sizeof(struct ethtool_rxnfc);
2721 convert_in = true;
2722 break;
2723 }
2724
2725 ifr = compat_alloc_user_space(buf_size);
Stephen Hemminger954b1242013-02-11 06:22:28 +00002726 rxnfc = (void __user *)ifr + ALIGN(sizeof(struct ifreq), 8);
Ben Hutchings3a7da392011-03-17 07:34:32 +00002727
2728 if (copy_in_user(&ifr->ifr_name, &ifr32->ifr_name, IFNAMSIZ))
2729 return -EFAULT;
2730
2731 if (put_user(convert_in ? rxnfc : compat_ptr(data),
2732 &ifr->ifr_ifru.ifru_data))
2733 return -EFAULT;
2734
2735 if (convert_in) {
Alexander Duyck127fe532011-04-08 18:01:59 +00002736 /* We expect there to be holes between fs.m_ext and
Ben Hutchings3a7da392011-03-17 07:34:32 +00002737 * fs.ring_cookie and at the end of fs, but nowhere else.
2738 */
Alexander Duyck127fe532011-04-08 18:01:59 +00002739 BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc, fs.m_ext) +
2740 sizeof(compat_rxnfc->fs.m_ext) !=
2741 offsetof(struct ethtool_rxnfc, fs.m_ext) +
2742 sizeof(rxnfc->fs.m_ext));
Ben Hutchings3a7da392011-03-17 07:34:32 +00002743 BUILD_BUG_ON(
2744 offsetof(struct compat_ethtool_rxnfc, fs.location) -
2745 offsetof(struct compat_ethtool_rxnfc, fs.ring_cookie) !=
2746 offsetof(struct ethtool_rxnfc, fs.location) -
2747 offsetof(struct ethtool_rxnfc, fs.ring_cookie));
2748
2749 if (copy_in_user(rxnfc, compat_rxnfc,
Stephen Hemminger954b1242013-02-11 06:22:28 +00002750 (void __user *)(&rxnfc->fs.m_ext + 1) -
2751 (void __user *)rxnfc) ||
Ben Hutchings3a7da392011-03-17 07:34:32 +00002752 copy_in_user(&rxnfc->fs.ring_cookie,
2753 &compat_rxnfc->fs.ring_cookie,
Stephen Hemminger954b1242013-02-11 06:22:28 +00002754 (void __user *)(&rxnfc->fs.location + 1) -
2755 (void __user *)&rxnfc->fs.ring_cookie) ||
Ben Hutchings3a7da392011-03-17 07:34:32 +00002756 copy_in_user(&rxnfc->rule_cnt, &compat_rxnfc->rule_cnt,
2757 sizeof(rxnfc->rule_cnt)))
2758 return -EFAULT;
2759 }
2760
2761 ret = dev_ioctl(net, SIOCETHTOOL, ifr);
2762 if (ret)
2763 return ret;
2764
2765 if (convert_out) {
2766 if (copy_in_user(compat_rxnfc, rxnfc,
Stephen Hemminger954b1242013-02-11 06:22:28 +00002767 (const void __user *)(&rxnfc->fs.m_ext + 1) -
2768 (const void __user *)rxnfc) ||
Ben Hutchings3a7da392011-03-17 07:34:32 +00002769 copy_in_user(&compat_rxnfc->fs.ring_cookie,
2770 &rxnfc->fs.ring_cookie,
Stephen Hemminger954b1242013-02-11 06:22:28 +00002771 (const void __user *)(&rxnfc->fs.location + 1) -
2772 (const void __user *)&rxnfc->fs.ring_cookie) ||
Ben Hutchings3a7da392011-03-17 07:34:32 +00002773 copy_in_user(&compat_rxnfc->rule_cnt, &rxnfc->rule_cnt,
2774 sizeof(rxnfc->rule_cnt)))
2775 return -EFAULT;
2776
2777 if (ethcmd == ETHTOOL_GRXCLSRLALL) {
2778 /* As an optimisation, we only copy the actual
2779 * number of rules that the underlying
2780 * function returned. Since Mallory might
2781 * change the rule count in user memory, we
2782 * check that it is less than the rule count
2783 * originally given (as the user buffer size),
2784 * which has been range-checked.
2785 */
2786 if (get_user(actual_rule_cnt, &rxnfc->rule_cnt))
2787 return -EFAULT;
2788 if (actual_rule_cnt < rule_cnt)
2789 rule_cnt = actual_rule_cnt;
2790 if (copy_in_user(&compat_rxnfc->rule_locs[0],
2791 &rxnfc->rule_locs[0],
2792 rule_cnt * sizeof(u32)))
2793 return -EFAULT;
2794 }
2795 }
2796
2797 return 0;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002798}
2799
Arnd Bergmann7a50a242009-11-08 20:57:03 -08002800static int compat_siocwandev(struct net *net, struct compat_ifreq __user *uifr32)
2801{
2802 void __user *uptr;
2803 compat_uptr_t uptr32;
2804 struct ifreq __user *uifr;
2805
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002806 uifr = compat_alloc_user_space(sizeof(*uifr));
Arnd Bergmann7a50a242009-11-08 20:57:03 -08002807 if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
2808 return -EFAULT;
2809
2810 if (get_user(uptr32, &uifr32->ifr_settings.ifs_ifsu))
2811 return -EFAULT;
2812
2813 uptr = compat_ptr(uptr32);
2814
2815 if (put_user(uptr, &uifr->ifr_settings.ifs_ifsu.raw_hdlc))
2816 return -EFAULT;
2817
2818 return dev_ioctl(net, SIOCWANDEV, uifr);
2819}
2820
Arnd Bergmann6b960182009-11-06 23:10:54 -08002821static int bond_ioctl(struct net *net, unsigned int cmd,
2822 struct compat_ifreq __user *ifr32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002823{
2824 struct ifreq kifr;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002825 mm_segment_t old_fs;
2826 int err;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002827
2828 switch (cmd) {
2829 case SIOCBONDENSLAVE:
2830 case SIOCBONDRELEASE:
2831 case SIOCBONDSETHWADDR:
2832 case SIOCBONDCHANGEACTIVE:
Arnd Bergmann6b960182009-11-06 23:10:54 -08002833 if (copy_from_user(&kifr, ifr32, sizeof(struct compat_ifreq)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002834 return -EFAULT;
2835
2836 old_fs = get_fs();
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002837 set_fs(KERNEL_DS);
stephen hemmingerc3f52ae2011-02-23 09:06:48 +00002838 err = dev_ioctl(net, cmd,
2839 (struct ifreq __user __force *) &kifr);
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002840 set_fs(old_fs);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002841
2842 return err;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002843 default:
Linus Torvalds07d106d2012-01-05 15:40:12 -08002844 return -ENOIOCTLCMD;
Joe Perchesccbd6a52010-05-14 10:58:26 +00002845 }
Arnd Bergmann7a229382009-11-06 23:00:29 -08002846}
2847
Ben Hutchings590d4692013-11-18 17:04:13 +00002848/* Handle ioctls that use ifreq::ifr_data and just need struct ifreq converted */
2849static int compat_ifr_data_ioctl(struct net *net, unsigned int cmd,
Arnd Bergmann6b960182009-11-06 23:10:54 -08002850 struct compat_ifreq __user *u_ifreq32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002851{
2852 struct ifreq __user *u_ifreq64;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002853 char tmp_buf[IFNAMSIZ];
2854 void __user *data64;
2855 u32 data32;
2856
2857 if (copy_from_user(&tmp_buf[0], &(u_ifreq32->ifr_ifrn.ifrn_name[0]),
2858 IFNAMSIZ))
2859 return -EFAULT;
Ben Hutchings417c3522013-11-18 17:04:58 +00002860 if (get_user(data32, &u_ifreq32->ifr_ifru.ifru_data))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002861 return -EFAULT;
2862 data64 = compat_ptr(data32);
2863
2864 u_ifreq64 = compat_alloc_user_space(sizeof(*u_ifreq64));
2865
Arnd Bergmann7a229382009-11-06 23:00:29 -08002866 if (copy_to_user(&u_ifreq64->ifr_ifrn.ifrn_name[0], &tmp_buf[0],
2867 IFNAMSIZ))
2868 return -EFAULT;
Ben Hutchings417c3522013-11-18 17:04:58 +00002869 if (put_user(data64, &u_ifreq64->ifr_ifru.ifru_data))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002870 return -EFAULT;
2871
Arnd Bergmann6b960182009-11-06 23:10:54 -08002872 return dev_ioctl(net, cmd, u_ifreq64);
Arnd Bergmann7a229382009-11-06 23:00:29 -08002873}
2874
Arnd Bergmann6b960182009-11-06 23:10:54 -08002875static int dev_ifsioc(struct net *net, struct socket *sock,
2876 unsigned int cmd, struct compat_ifreq __user *uifr32)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002877{
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00002878 struct ifreq __user *uifr;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002879 int err;
2880
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00002881 uifr = compat_alloc_user_space(sizeof(*uifr));
2882 if (copy_in_user(uifr, uifr32, sizeof(*uifr32)))
2883 return -EFAULT;
2884
2885 err = sock_do_ioctl(net, sock, cmd, (unsigned long)uifr);
2886
Arnd Bergmann7a229382009-11-06 23:00:29 -08002887 if (!err) {
2888 switch (cmd) {
2889 case SIOCGIFFLAGS:
2890 case SIOCGIFMETRIC:
2891 case SIOCGIFMTU:
2892 case SIOCGIFMEM:
2893 case SIOCGIFHWADDR:
2894 case SIOCGIFINDEX:
2895 case SIOCGIFADDR:
2896 case SIOCGIFBRDADDR:
2897 case SIOCGIFDSTADDR:
2898 case SIOCGIFNETMASK:
Arnd Bergmannfab25322009-11-08 20:56:21 -08002899 case SIOCGIFPFLAGS:
Arnd Bergmann7a229382009-11-06 23:00:29 -08002900 case SIOCGIFTXQLEN:
Arnd Bergmannfab25322009-11-08 20:56:21 -08002901 case SIOCGMIIPHY:
2902 case SIOCGMIIREG:
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00002903 if (copy_in_user(uifr32, uifr, sizeof(*uifr32)))
Arnd Bergmann7a229382009-11-06 23:00:29 -08002904 err = -EFAULT;
2905 break;
2906 }
2907 }
2908 return err;
2909}
2910
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00002911static int compat_sioc_ifmap(struct net *net, unsigned int cmd,
2912 struct compat_ifreq __user *uifr32)
2913{
2914 struct ifreq ifr;
2915 struct compat_ifmap __user *uifmap32;
2916 mm_segment_t old_fs;
2917 int err;
2918
2919 uifmap32 = &uifr32->ifr_ifru.ifru_map;
2920 err = copy_from_user(&ifr, uifr32, sizeof(ifr.ifr_name));
Mathieu Desnoyers3ddc5b42013-09-11 14:23:18 -07002921 err |= get_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
2922 err |= get_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
2923 err |= get_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
2924 err |= get_user(ifr.ifr_map.irq, &uifmap32->irq);
2925 err |= get_user(ifr.ifr_map.dma, &uifmap32->dma);
2926 err |= get_user(ifr.ifr_map.port, &uifmap32->port);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00002927 if (err)
2928 return -EFAULT;
2929
2930 old_fs = get_fs();
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002931 set_fs(KERNEL_DS);
stephen hemmingerc3f52ae2011-02-23 09:06:48 +00002932 err = dev_ioctl(net, cmd, (void __user __force *)&ifr);
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002933 set_fs(old_fs);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00002934
2935 if (cmd == SIOCGIFMAP && !err) {
2936 err = copy_to_user(uifr32, &ifr, sizeof(ifr.ifr_name));
Mathieu Desnoyers3ddc5b42013-09-11 14:23:18 -07002937 err |= put_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
2938 err |= put_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
2939 err |= put_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
2940 err |= put_user(ifr.ifr_map.irq, &uifmap32->irq);
2941 err |= put_user(ifr.ifr_map.dma, &uifmap32->dma);
2942 err |= put_user(ifr.ifr_map.port, &uifmap32->port);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00002943 if (err)
2944 err = -EFAULT;
2945 }
2946 return err;
2947}
2948
Arnd Bergmann7a229382009-11-06 23:00:29 -08002949struct rtentry32 {
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002950 u32 rt_pad1;
Arnd Bergmann7a229382009-11-06 23:00:29 -08002951 struct sockaddr rt_dst; /* target address */
2952 struct sockaddr rt_gateway; /* gateway addr (RTF_GATEWAY) */
2953 struct sockaddr rt_genmask; /* target network mask (IP) */
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002954 unsigned short rt_flags;
2955 short rt_pad2;
2956 u32 rt_pad3;
2957 unsigned char rt_tos;
2958 unsigned char rt_class;
2959 short rt_pad4;
2960 short rt_metric; /* +1 for binary compatibility! */
Arnd Bergmann7a229382009-11-06 23:00:29 -08002961 /* char * */ u32 rt_dev; /* forcing the device at add */
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002962 u32 rt_mtu; /* per route MTU/Window */
2963 u32 rt_window; /* Window clamping */
Arnd Bergmann7a229382009-11-06 23:00:29 -08002964 unsigned short rt_irtt; /* Initial RTT */
2965};
2966
2967struct in6_rtmsg32 {
2968 struct in6_addr rtmsg_dst;
2969 struct in6_addr rtmsg_src;
2970 struct in6_addr rtmsg_gateway;
2971 u32 rtmsg_type;
2972 u16 rtmsg_dst_len;
2973 u16 rtmsg_src_len;
2974 u32 rtmsg_metric;
2975 u32 rtmsg_info;
2976 u32 rtmsg_flags;
2977 s32 rtmsg_ifindex;
2978};
2979
Arnd Bergmann6b960182009-11-06 23:10:54 -08002980static int routing_ioctl(struct net *net, struct socket *sock,
2981 unsigned int cmd, void __user *argp)
Arnd Bergmann7a229382009-11-06 23:00:29 -08002982{
2983 int ret;
2984 void *r = NULL;
2985 struct in6_rtmsg r6;
2986 struct rtentry r4;
2987 char devname[16];
2988 u32 rtdev;
2989 mm_segment_t old_fs = get_fs();
2990
Arnd Bergmann6b960182009-11-06 23:10:54 -08002991 if (sock && sock->sk && sock->sk->sk_family == AF_INET6) { /* ipv6 */
2992 struct in6_rtmsg32 __user *ur6 = argp;
Eric Dumazetc6d409c2010-06-03 20:03:40 -07002993 ret = copy_from_user(&r6.rtmsg_dst, &(ur6->rtmsg_dst),
Arnd Bergmann7a229382009-11-06 23:00:29 -08002994 3 * sizeof(struct in6_addr));
Mathieu Desnoyers3ddc5b42013-09-11 14:23:18 -07002995 ret |= get_user(r6.rtmsg_type, &(ur6->rtmsg_type));
2996 ret |= get_user(r6.rtmsg_dst_len, &(ur6->rtmsg_dst_len));
2997 ret |= get_user(r6.rtmsg_src_len, &(ur6->rtmsg_src_len));
2998 ret |= get_user(r6.rtmsg_metric, &(ur6->rtmsg_metric));
2999 ret |= get_user(r6.rtmsg_info, &(ur6->rtmsg_info));
3000 ret |= get_user(r6.rtmsg_flags, &(ur6->rtmsg_flags));
3001 ret |= get_user(r6.rtmsg_ifindex, &(ur6->rtmsg_ifindex));
Arnd Bergmann7a229382009-11-06 23:00:29 -08003002
3003 r = (void *) &r6;
3004 } else { /* ipv4 */
Arnd Bergmann6b960182009-11-06 23:10:54 -08003005 struct rtentry32 __user *ur4 = argp;
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003006 ret = copy_from_user(&r4.rt_dst, &(ur4->rt_dst),
Arnd Bergmann7a229382009-11-06 23:00:29 -08003007 3 * sizeof(struct sockaddr));
Mathieu Desnoyers3ddc5b42013-09-11 14:23:18 -07003008 ret |= get_user(r4.rt_flags, &(ur4->rt_flags));
3009 ret |= get_user(r4.rt_metric, &(ur4->rt_metric));
3010 ret |= get_user(r4.rt_mtu, &(ur4->rt_mtu));
3011 ret |= get_user(r4.rt_window, &(ur4->rt_window));
3012 ret |= get_user(r4.rt_irtt, &(ur4->rt_irtt));
3013 ret |= get_user(rtdev, &(ur4->rt_dev));
Arnd Bergmann7a229382009-11-06 23:00:29 -08003014 if (rtdev) {
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003015 ret |= copy_from_user(devname, compat_ptr(rtdev), 15);
stephen hemmingerc3f52ae2011-02-23 09:06:48 +00003016 r4.rt_dev = (char __user __force *)devname;
3017 devname[15] = 0;
Arnd Bergmann7a229382009-11-06 23:00:29 -08003018 } else
3019 r4.rt_dev = NULL;
3020
3021 r = (void *) &r4;
3022 }
3023
3024 if (ret) {
3025 ret = -EFAULT;
3026 goto out;
3027 }
3028
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003029 set_fs(KERNEL_DS);
Arnd Bergmann6b960182009-11-06 23:10:54 -08003030 ret = sock_do_ioctl(net, sock, cmd, (unsigned long) r);
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003031 set_fs(old_fs);
Arnd Bergmann7a229382009-11-06 23:00:29 -08003032
3033out:
Arnd Bergmann7a229382009-11-06 23:00:29 -08003034 return ret;
3035}
3036
3037/* Since old style bridge ioctl's endup using SIOCDEVPRIVATE
3038 * for some operations; this forces use of the newer bridge-utils that
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003039 * use compatible ioctls
Arnd Bergmann7a229382009-11-06 23:00:29 -08003040 */
Arnd Bergmann6b960182009-11-06 23:10:54 -08003041static int old_bridge_ioctl(compat_ulong_t __user *argp)
Arnd Bergmann7a229382009-11-06 23:00:29 -08003042{
Arnd Bergmann6b960182009-11-06 23:10:54 -08003043 compat_ulong_t tmp;
Arnd Bergmann7a229382009-11-06 23:00:29 -08003044
Arnd Bergmann6b960182009-11-06 23:10:54 -08003045 if (get_user(tmp, argp))
Arnd Bergmann7a229382009-11-06 23:00:29 -08003046 return -EFAULT;
3047 if (tmp == BRCTL_GET_VERSION)
3048 return BRCTL_VERSION + 1;
3049 return -EINVAL;
3050}
3051
Arnd Bergmann6b960182009-11-06 23:10:54 -08003052static int compat_sock_ioctl_trans(struct file *file, struct socket *sock,
3053 unsigned int cmd, unsigned long arg)
3054{
3055 void __user *argp = compat_ptr(arg);
3056 struct sock *sk = sock->sk;
3057 struct net *net = sock_net(sk);
Arnd Bergmann7a229382009-11-06 23:00:29 -08003058
Arnd Bergmann6b960182009-11-06 23:10:54 -08003059 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15))
Ben Hutchings590d4692013-11-18 17:04:13 +00003060 return compat_ifr_data_ioctl(net, cmd, argp);
Arnd Bergmann7a229382009-11-06 23:00:29 -08003061
Arnd Bergmann6b960182009-11-06 23:10:54 -08003062 switch (cmd) {
3063 case SIOCSIFBR:
3064 case SIOCGIFBR:
3065 return old_bridge_ioctl(argp);
3066 case SIOCGIFNAME:
3067 return dev_ifname32(net, argp);
3068 case SIOCGIFCONF:
3069 return dev_ifconf(net, argp);
3070 case SIOCETHTOOL:
3071 return ethtool_ioctl(net, argp);
Arnd Bergmann7a50a242009-11-08 20:57:03 -08003072 case SIOCWANDEV:
3073 return compat_siocwandev(net, argp);
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003074 case SIOCGIFMAP:
3075 case SIOCSIFMAP:
3076 return compat_sioc_ifmap(net, cmd, argp);
Arnd Bergmann6b960182009-11-06 23:10:54 -08003077 case SIOCBONDENSLAVE:
3078 case SIOCBONDRELEASE:
3079 case SIOCBONDSETHWADDR:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003080 case SIOCBONDCHANGEACTIVE:
3081 return bond_ioctl(net, cmd, argp);
3082 case SIOCADDRT:
3083 case SIOCDELRT:
3084 return routing_ioctl(net, sock, cmd, argp);
3085 case SIOCGSTAMP:
3086 return do_siocgstamp(net, sock, cmd, argp);
3087 case SIOCGSTAMPNS:
3088 return do_siocgstampns(net, sock, cmd, argp);
Ben Hutchings590d4692013-11-18 17:04:13 +00003089 case SIOCBONDSLAVEINFOQUERY:
3090 case SIOCBONDINFOQUERY:
Arnd Bergmanna2116ed2009-11-11 03:39:40 +00003091 case SIOCSHWTSTAMP:
Ben Hutchingsfd468c72013-11-14 01:19:29 +00003092 case SIOCGHWTSTAMP:
Ben Hutchings590d4692013-11-18 17:04:13 +00003093 return compat_ifr_data_ioctl(net, cmd, argp);
Arnd Bergmann7a229382009-11-06 23:00:29 -08003094
Arnd Bergmann6b960182009-11-06 23:10:54 -08003095 case FIOSETOWN:
3096 case SIOCSPGRP:
3097 case FIOGETOWN:
3098 case SIOCGPGRP:
3099 case SIOCBRADDBR:
3100 case SIOCBRDELBR:
3101 case SIOCGIFVLAN:
3102 case SIOCSIFVLAN:
3103 case SIOCADDDLCI:
3104 case SIOCDELDLCI:
3105 return sock_ioctl(file, cmd, arg);
3106
3107 case SIOCGIFFLAGS:
3108 case SIOCSIFFLAGS:
3109 case SIOCGIFMETRIC:
3110 case SIOCSIFMETRIC:
3111 case SIOCGIFMTU:
3112 case SIOCSIFMTU:
3113 case SIOCGIFMEM:
3114 case SIOCSIFMEM:
3115 case SIOCGIFHWADDR:
3116 case SIOCSIFHWADDR:
3117 case SIOCADDMULTI:
3118 case SIOCDELMULTI:
3119 case SIOCGIFINDEX:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003120 case SIOCGIFADDR:
3121 case SIOCSIFADDR:
3122 case SIOCSIFHWBROADCAST:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003123 case SIOCDIFADDR:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003124 case SIOCGIFBRDADDR:
3125 case SIOCSIFBRDADDR:
3126 case SIOCGIFDSTADDR:
3127 case SIOCSIFDSTADDR:
3128 case SIOCGIFNETMASK:
3129 case SIOCSIFNETMASK:
3130 case SIOCSIFPFLAGS:
3131 case SIOCGIFPFLAGS:
3132 case SIOCGIFTXQLEN:
3133 case SIOCSIFTXQLEN:
3134 case SIOCBRADDIF:
3135 case SIOCBRDELIF:
Arnd Bergmann9177efd2009-11-06 08:09:09 +00003136 case SIOCSIFNAME:
3137 case SIOCGMIIPHY:
3138 case SIOCGMIIREG:
3139 case SIOCSMIIREG:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003140 return dev_ifsioc(net, sock, cmd, argp);
Arnd Bergmann9177efd2009-11-06 08:09:09 +00003141
Arnd Bergmann6b960182009-11-06 23:10:54 -08003142 case SIOCSARP:
3143 case SIOCGARP:
3144 case SIOCDARP:
Arnd Bergmann6b960182009-11-06 23:10:54 -08003145 case SIOCATMARK:
Arnd Bergmann9177efd2009-11-06 08:09:09 +00003146 return sock_do_ioctl(net, sock, cmd, arg);
3147 }
3148
Arnd Bergmann6b960182009-11-06 23:10:54 -08003149 return -ENOIOCTLCMD;
3150}
Arnd Bergmann7a229382009-11-06 23:00:29 -08003151
Eric Dumazet95c96172012-04-15 05:58:06 +00003152static long compat_sock_ioctl(struct file *file, unsigned int cmd,
Stephen Hemminger89bddce2006-09-01 00:19:31 -07003153 unsigned long arg)
Shaun Pereira89bbfc92006-03-21 23:58:08 -08003154{
3155 struct socket *sock = file->private_data;
3156 int ret = -ENOIOCTLCMD;
David S. Miller87de87d2008-06-03 09:14:03 -07003157 struct sock *sk;
3158 struct net *net;
3159
3160 sk = sock->sk;
3161 net = sock_net(sk);
Shaun Pereira89bbfc92006-03-21 23:58:08 -08003162
3163 if (sock->ops->compat_ioctl)
3164 ret = sock->ops->compat_ioctl(sock, cmd, arg);
3165
David S. Miller87de87d2008-06-03 09:14:03 -07003166 if (ret == -ENOIOCTLCMD &&
3167 (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST))
3168 ret = compat_wext_handle_ioctl(net, cmd, arg);
3169
Arnd Bergmann6b960182009-11-06 23:10:54 -08003170 if (ret == -ENOIOCTLCMD)
3171 ret = compat_sock_ioctl_trans(file, sock, cmd, arg);
3172
Shaun Pereira89bbfc92006-03-21 23:58:08 -08003173 return ret;
3174}
3175#endif
3176
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003177int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen)
3178{
3179 return sock->ops->bind(sock, addr, addrlen);
3180}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003181EXPORT_SYMBOL(kernel_bind);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003182
3183int kernel_listen(struct socket *sock, int backlog)
3184{
3185 return sock->ops->listen(sock, backlog);
3186}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003187EXPORT_SYMBOL(kernel_listen);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003188
3189int kernel_accept(struct socket *sock, struct socket **newsock, int flags)
3190{
3191 struct sock *sk = sock->sk;
3192 int err;
3193
3194 err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
3195 newsock);
3196 if (err < 0)
3197 goto done;
3198
3199 err = sock->ops->accept(sock, *newsock, flags);
3200 if (err < 0) {
3201 sock_release(*newsock);
Tony Battersbyfa8705b2007-10-10 21:09:04 -07003202 *newsock = NULL;
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003203 goto done;
3204 }
3205
3206 (*newsock)->ops = sock->ops;
Wei Yongjun1b085342008-12-18 19:35:10 -08003207 __module_get((*newsock)->ops->owner);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003208
3209done:
3210 return err;
3211}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003212EXPORT_SYMBOL(kernel_accept);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003213
3214int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
YOSHIFUJI Hideaki4768fbc2007-02-09 23:25:31 +09003215 int flags)
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003216{
3217 return sock->ops->connect(sock, addr, addrlen, flags);
3218}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003219EXPORT_SYMBOL(kernel_connect);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003220
3221int kernel_getsockname(struct socket *sock, struct sockaddr *addr,
3222 int *addrlen)
3223{
3224 return sock->ops->getname(sock, addr, addrlen, 0);
3225}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003226EXPORT_SYMBOL(kernel_getsockname);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003227
3228int kernel_getpeername(struct socket *sock, struct sockaddr *addr,
3229 int *addrlen)
3230{
3231 return sock->ops->getname(sock, addr, addrlen, 1);
3232}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003233EXPORT_SYMBOL(kernel_getpeername);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003234
3235int kernel_getsockopt(struct socket *sock, int level, int optname,
3236 char *optval, int *optlen)
3237{
3238 mm_segment_t oldfs = get_fs();
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003239 char __user *uoptval;
3240 int __user *uoptlen;
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003241 int err;
3242
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003243 uoptval = (char __user __force *) optval;
3244 uoptlen = (int __user __force *) optlen;
3245
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003246 set_fs(KERNEL_DS);
3247 if (level == SOL_SOCKET)
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003248 err = sock_getsockopt(sock, level, optname, uoptval, uoptlen);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003249 else
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003250 err = sock->ops->getsockopt(sock, level, optname, uoptval,
3251 uoptlen);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003252 set_fs(oldfs);
3253 return err;
3254}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003255EXPORT_SYMBOL(kernel_getsockopt);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003256
3257int kernel_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07003258 char *optval, unsigned int optlen)
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003259{
3260 mm_segment_t oldfs = get_fs();
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003261 char __user *uoptval;
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003262 int err;
3263
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003264 uoptval = (char __user __force *) optval;
3265
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003266 set_fs(KERNEL_DS);
3267 if (level == SOL_SOCKET)
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003268 err = sock_setsockopt(sock, level, optname, uoptval, optlen);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003269 else
Namhyung Kimfb8621b2010-09-07 03:55:00 +00003270 err = sock->ops->setsockopt(sock, level, optname, uoptval,
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003271 optlen);
3272 set_fs(oldfs);
3273 return err;
3274}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003275EXPORT_SYMBOL(kernel_setsockopt);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003276
3277int kernel_sendpage(struct socket *sock, struct page *page, int offset,
3278 size_t size, int flags)
3279{
3280 if (sock->ops->sendpage)
3281 return sock->ops->sendpage(sock, page, offset, size, flags);
3282
3283 return sock_no_sendpage(sock, page, offset, size, flags);
3284}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003285EXPORT_SYMBOL(kernel_sendpage);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003286
3287int kernel_sock_ioctl(struct socket *sock, int cmd, unsigned long arg)
3288{
3289 mm_segment_t oldfs = get_fs();
3290 int err;
3291
3292 set_fs(KERNEL_DS);
3293 err = sock->ops->ioctl(sock, cmd, arg);
3294 set_fs(oldfs);
3295
3296 return err;
3297}
Eric Dumazetc6d409c2010-06-03 20:03:40 -07003298EXPORT_SYMBOL(kernel_sock_ioctl);
Sridhar Samudralaac5a4882006-08-07 20:57:31 -07003299
Trond Myklebust91cf45f2007-11-12 18:10:39 -08003300int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how)
3301{
3302 return sock->ops->shutdown(sock, how);
3303}
Trond Myklebust91cf45f2007-11-12 18:10:39 -08003304EXPORT_SYMBOL(kernel_sock_shutdown);