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