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