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