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