Thomas Gleixner | 685a6bf | 2019-05-29 16:57:36 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 2 | /* |
| 3 | * VMware vSockets Driver |
| 4 | * |
| 5 | * Copyright (C) 2007-2013 VMware, Inc. All rights reserved. |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/types.h> |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 9 | #include <linux/bitops.h> |
| 10 | #include <linux/cred.h> |
| 11 | #include <linux/init.h> |
| 12 | #include <linux/io.h> |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/kmod.h> |
| 15 | #include <linux/list.h> |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 16 | #include <linux/module.h> |
| 17 | #include <linux/mutex.h> |
| 18 | #include <linux/net.h> |
| 19 | #include <linux/poll.h> |
| 20 | #include <linux/skbuff.h> |
| 21 | #include <linux/smp.h> |
| 22 | #include <linux/socket.h> |
| 23 | #include <linux/stddef.h> |
| 24 | #include <linux/unistd.h> |
| 25 | #include <linux/wait.h> |
| 26 | #include <linux/workqueue.h> |
| 27 | #include <net/sock.h> |
Asias He | 82a54d0 | 2013-07-25 17:39:34 +0800 | [diff] [blame] | 28 | #include <net/af_vsock.h> |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 29 | |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 30 | #include "vmci_transport_notify.h" |
| 31 | |
| 32 | static int vmci_transport_recv_dgram_cb(void *data, struct vmci_datagram *dg); |
| 33 | static int vmci_transport_recv_stream_cb(void *data, struct vmci_datagram *dg); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 34 | static void vmci_transport_peer_detach_cb(u32 sub_id, |
| 35 | const struct vmci_event_data *ed, |
| 36 | void *client_data); |
| 37 | static void vmci_transport_recv_pkt_work(struct work_struct *work); |
Jorgen Hansen | 4ef7ea9 | 2015-10-21 04:53:56 -0700 | [diff] [blame] | 38 | static void vmci_transport_cleanup(struct work_struct *work); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 39 | static int vmci_transport_recv_listen(struct sock *sk, |
| 40 | struct vmci_transport_packet *pkt); |
| 41 | static int vmci_transport_recv_connecting_server( |
| 42 | struct sock *sk, |
| 43 | struct sock *pending, |
| 44 | struct vmci_transport_packet *pkt); |
| 45 | static int vmci_transport_recv_connecting_client( |
| 46 | struct sock *sk, |
| 47 | struct vmci_transport_packet *pkt); |
| 48 | static int vmci_transport_recv_connecting_client_negotiate( |
| 49 | struct sock *sk, |
| 50 | struct vmci_transport_packet *pkt); |
| 51 | static int vmci_transport_recv_connecting_client_invalid( |
| 52 | struct sock *sk, |
| 53 | struct vmci_transport_packet *pkt); |
| 54 | static int vmci_transport_recv_connected(struct sock *sk, |
| 55 | struct vmci_transport_packet *pkt); |
| 56 | static bool vmci_transport_old_proto_override(bool *old_pkt_proto); |
| 57 | static u16 vmci_transport_new_proto_supported_versions(void); |
| 58 | static bool vmci_transport_proto_to_notify_struct(struct sock *sk, u16 *proto, |
| 59 | bool old_pkt_proto); |
Stefano Garzarella | c0cfa2d | 2019-11-14 10:57:46 +0100 | [diff] [blame] | 60 | static bool vmci_check_transport(struct vsock_sock *vsk); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 61 | |
| 62 | struct vmci_transport_recv_pkt_info { |
| 63 | struct work_struct work; |
| 64 | struct sock *sk; |
| 65 | struct vmci_transport_packet pkt; |
| 66 | }; |
| 67 | |
Jorgen Hansen | 4ef7ea9 | 2015-10-21 04:53:56 -0700 | [diff] [blame] | 68 | static LIST_HEAD(vmci_transport_cleanup_list); |
| 69 | static DEFINE_SPINLOCK(vmci_transport_cleanup_lock); |
| 70 | static DECLARE_WORK(vmci_transport_cleanup_work, vmci_transport_cleanup); |
| 71 | |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 72 | static struct vmci_handle vmci_transport_stream_handle = { VMCI_INVALID_ID, |
| 73 | VMCI_INVALID_ID }; |
| 74 | static u32 vmci_transport_qp_resumed_sub_id = VMCI_INVALID_ID; |
| 75 | |
| 76 | static int PROTOCOL_OVERRIDE = -1; |
| 77 | |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 78 | /* Helper function to convert from a VMCI error code to a VSock error code. */ |
| 79 | |
| 80 | static s32 vmci_transport_error_to_vsock_error(s32 vmci_error) |
| 81 | { |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 82 | switch (vmci_error) { |
| 83 | case VMCI_ERROR_NO_MEM: |
Colin Ian King | ed8bfd5 | 2017-03-29 16:33:55 +0100 | [diff] [blame] | 84 | return -ENOMEM; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 85 | case VMCI_ERROR_DUPLICATE_ENTRY: |
| 86 | case VMCI_ERROR_ALREADY_EXISTS: |
Colin Ian King | ed8bfd5 | 2017-03-29 16:33:55 +0100 | [diff] [blame] | 87 | return -EADDRINUSE; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 88 | case VMCI_ERROR_NO_ACCESS: |
Colin Ian King | ed8bfd5 | 2017-03-29 16:33:55 +0100 | [diff] [blame] | 89 | return -EPERM; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 90 | case VMCI_ERROR_NO_RESOURCES: |
Colin Ian King | ed8bfd5 | 2017-03-29 16:33:55 +0100 | [diff] [blame] | 91 | return -ENOBUFS; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 92 | case VMCI_ERROR_INVALID_RESOURCE: |
Colin Ian King | ed8bfd5 | 2017-03-29 16:33:55 +0100 | [diff] [blame] | 93 | return -EHOSTUNREACH; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 94 | case VMCI_ERROR_INVALID_ARGS: |
| 95 | default: |
Colin Ian King | ed8bfd5 | 2017-03-29 16:33:55 +0100 | [diff] [blame] | 96 | break; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 97 | } |
Colin Ian King | ed8bfd5 | 2017-03-29 16:33:55 +0100 | [diff] [blame] | 98 | return -EINVAL; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Reilly Grant | 2a89f92 | 2013-03-14 11:55:41 +0000 | [diff] [blame] | 101 | static u32 vmci_transport_peer_rid(u32 peer_cid) |
| 102 | { |
| 103 | if (VMADDR_CID_HYPERVISOR == peer_cid) |
| 104 | return VMCI_TRANSPORT_HYPERVISOR_PACKET_RID; |
| 105 | |
| 106 | return VMCI_TRANSPORT_PACKET_RID; |
| 107 | } |
| 108 | |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 109 | static inline void |
| 110 | vmci_transport_packet_init(struct vmci_transport_packet *pkt, |
| 111 | struct sockaddr_vm *src, |
| 112 | struct sockaddr_vm *dst, |
| 113 | u8 type, |
| 114 | u64 size, |
| 115 | u64 mode, |
| 116 | struct vmci_transport_waiting_info *wait, |
| 117 | u16 proto, |
| 118 | struct vmci_handle handle) |
| 119 | { |
| 120 | /* We register the stream control handler as an any cid handle so we |
| 121 | * must always send from a source address of VMADDR_CID_ANY |
| 122 | */ |
| 123 | pkt->dg.src = vmci_make_handle(VMADDR_CID_ANY, |
| 124 | VMCI_TRANSPORT_PACKET_RID); |
| 125 | pkt->dg.dst = vmci_make_handle(dst->svm_cid, |
Reilly Grant | 2a89f92 | 2013-03-14 11:55:41 +0000 | [diff] [blame] | 126 | vmci_transport_peer_rid(dst->svm_cid)); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 127 | pkt->dg.payload_size = sizeof(*pkt) - sizeof(pkt->dg); |
| 128 | pkt->version = VMCI_TRANSPORT_PACKET_VERSION; |
| 129 | pkt->type = type; |
| 130 | pkt->src_port = src->svm_port; |
| 131 | pkt->dst_port = dst->svm_port; |
| 132 | memset(&pkt->proto, 0, sizeof(pkt->proto)); |
| 133 | memset(&pkt->_reserved2, 0, sizeof(pkt->_reserved2)); |
| 134 | |
| 135 | switch (pkt->type) { |
| 136 | case VMCI_TRANSPORT_PACKET_TYPE_INVALID: |
| 137 | pkt->u.size = 0; |
| 138 | break; |
| 139 | |
| 140 | case VMCI_TRANSPORT_PACKET_TYPE_REQUEST: |
| 141 | case VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE: |
| 142 | pkt->u.size = size; |
| 143 | break; |
| 144 | |
| 145 | case VMCI_TRANSPORT_PACKET_TYPE_OFFER: |
| 146 | case VMCI_TRANSPORT_PACKET_TYPE_ATTACH: |
| 147 | pkt->u.handle = handle; |
| 148 | break; |
| 149 | |
| 150 | case VMCI_TRANSPORT_PACKET_TYPE_WROTE: |
| 151 | case VMCI_TRANSPORT_PACKET_TYPE_READ: |
| 152 | case VMCI_TRANSPORT_PACKET_TYPE_RST: |
| 153 | pkt->u.size = 0; |
| 154 | break; |
| 155 | |
| 156 | case VMCI_TRANSPORT_PACKET_TYPE_SHUTDOWN: |
| 157 | pkt->u.mode = mode; |
| 158 | break; |
| 159 | |
| 160 | case VMCI_TRANSPORT_PACKET_TYPE_WAITING_READ: |
| 161 | case VMCI_TRANSPORT_PACKET_TYPE_WAITING_WRITE: |
| 162 | memcpy(&pkt->u.wait, wait, sizeof(pkt->u.wait)); |
| 163 | break; |
| 164 | |
| 165 | case VMCI_TRANSPORT_PACKET_TYPE_REQUEST2: |
| 166 | case VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE2: |
| 167 | pkt->u.size = size; |
| 168 | pkt->proto = proto; |
| 169 | break; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | static inline void |
| 174 | vmci_transport_packet_get_addresses(struct vmci_transport_packet *pkt, |
| 175 | struct sockaddr_vm *local, |
| 176 | struct sockaddr_vm *remote) |
| 177 | { |
| 178 | vsock_addr_init(local, pkt->dg.dst.context, pkt->dst_port); |
| 179 | vsock_addr_init(remote, pkt->dg.src.context, pkt->src_port); |
| 180 | } |
| 181 | |
| 182 | static int |
| 183 | __vmci_transport_send_control_pkt(struct vmci_transport_packet *pkt, |
| 184 | struct sockaddr_vm *src, |
| 185 | struct sockaddr_vm *dst, |
| 186 | enum vmci_transport_packet_type type, |
| 187 | u64 size, |
| 188 | u64 mode, |
| 189 | struct vmci_transport_waiting_info *wait, |
| 190 | u16 proto, |
| 191 | struct vmci_handle handle, |
| 192 | bool convert_error) |
| 193 | { |
| 194 | int err; |
| 195 | |
| 196 | vmci_transport_packet_init(pkt, src, dst, type, size, mode, wait, |
| 197 | proto, handle); |
| 198 | err = vmci_datagram_send(&pkt->dg); |
| 199 | if (convert_error && (err < 0)) |
| 200 | return vmci_transport_error_to_vsock_error(err); |
| 201 | |
| 202 | return err; |
| 203 | } |
| 204 | |
| 205 | static int |
| 206 | vmci_transport_reply_control_pkt_fast(struct vmci_transport_packet *pkt, |
| 207 | enum vmci_transport_packet_type type, |
| 208 | u64 size, |
| 209 | u64 mode, |
| 210 | struct vmci_transport_waiting_info *wait, |
| 211 | struct vmci_handle handle) |
| 212 | { |
| 213 | struct vmci_transport_packet reply; |
| 214 | struct sockaddr_vm src, dst; |
| 215 | |
| 216 | if (pkt->type == VMCI_TRANSPORT_PACKET_TYPE_RST) { |
| 217 | return 0; |
| 218 | } else { |
| 219 | vmci_transport_packet_get_addresses(pkt, &src, &dst); |
| 220 | return __vmci_transport_send_control_pkt(&reply, &src, &dst, |
| 221 | type, |
| 222 | size, mode, wait, |
| 223 | VSOCK_PROTO_INVALID, |
| 224 | handle, true); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | static int |
| 229 | vmci_transport_send_control_pkt_bh(struct sockaddr_vm *src, |
| 230 | struct sockaddr_vm *dst, |
| 231 | enum vmci_transport_packet_type type, |
| 232 | u64 size, |
| 233 | u64 mode, |
| 234 | struct vmci_transport_waiting_info *wait, |
| 235 | struct vmci_handle handle) |
| 236 | { |
| 237 | /* Note that it is safe to use a single packet across all CPUs since |
| 238 | * two tasklets of the same type are guaranteed to not ever run |
| 239 | * simultaneously. If that ever changes, or VMCI stops using tasklets, |
| 240 | * we can use per-cpu packets. |
| 241 | */ |
| 242 | static struct vmci_transport_packet pkt; |
| 243 | |
| 244 | return __vmci_transport_send_control_pkt(&pkt, src, dst, type, |
| 245 | size, mode, wait, |
| 246 | VSOCK_PROTO_INVALID, handle, |
| 247 | false); |
| 248 | } |
| 249 | |
| 250 | static int |
Jorgen Hansen | a915b98 | 2018-12-18 00:34:06 -0800 | [diff] [blame] | 251 | vmci_transport_alloc_send_control_pkt(struct sockaddr_vm *src, |
| 252 | struct sockaddr_vm *dst, |
| 253 | enum vmci_transport_packet_type type, |
| 254 | u64 size, |
| 255 | u64 mode, |
| 256 | struct vmci_transport_waiting_info *wait, |
| 257 | u16 proto, |
| 258 | struct vmci_handle handle) |
| 259 | { |
| 260 | struct vmci_transport_packet *pkt; |
| 261 | int err; |
| 262 | |
| 263 | pkt = kmalloc(sizeof(*pkt), GFP_KERNEL); |
| 264 | if (!pkt) |
| 265 | return -ENOMEM; |
| 266 | |
| 267 | err = __vmci_transport_send_control_pkt(pkt, src, dst, type, size, |
| 268 | mode, wait, proto, handle, |
| 269 | true); |
| 270 | kfree(pkt); |
| 271 | |
| 272 | return err; |
| 273 | } |
| 274 | |
| 275 | static int |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 276 | vmci_transport_send_control_pkt(struct sock *sk, |
| 277 | enum vmci_transport_packet_type type, |
| 278 | u64 size, |
| 279 | u64 mode, |
| 280 | struct vmci_transport_waiting_info *wait, |
| 281 | u16 proto, |
| 282 | struct vmci_handle handle) |
| 283 | { |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 284 | struct vsock_sock *vsk; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 285 | |
| 286 | vsk = vsock_sk(sk); |
| 287 | |
| 288 | if (!vsock_addr_bound(&vsk->local_addr)) |
| 289 | return -EINVAL; |
| 290 | |
| 291 | if (!vsock_addr_bound(&vsk->remote_addr)) |
| 292 | return -EINVAL; |
| 293 | |
Jorgen Hansen | a915b98 | 2018-12-18 00:34:06 -0800 | [diff] [blame] | 294 | return vmci_transport_alloc_send_control_pkt(&vsk->local_addr, |
| 295 | &vsk->remote_addr, |
| 296 | type, size, mode, |
| 297 | wait, proto, handle); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | static int vmci_transport_send_reset_bh(struct sockaddr_vm *dst, |
| 301 | struct sockaddr_vm *src, |
| 302 | struct vmci_transport_packet *pkt) |
| 303 | { |
| 304 | if (pkt->type == VMCI_TRANSPORT_PACKET_TYPE_RST) |
| 305 | return 0; |
| 306 | return vmci_transport_send_control_pkt_bh( |
| 307 | dst, src, |
| 308 | VMCI_TRANSPORT_PACKET_TYPE_RST, 0, |
| 309 | 0, NULL, VMCI_INVALID_HANDLE); |
| 310 | } |
| 311 | |
| 312 | static int vmci_transport_send_reset(struct sock *sk, |
| 313 | struct vmci_transport_packet *pkt) |
| 314 | { |
Jorgen Hansen | a915b98 | 2018-12-18 00:34:06 -0800 | [diff] [blame] | 315 | struct sockaddr_vm *dst_ptr; |
| 316 | struct sockaddr_vm dst; |
| 317 | struct vsock_sock *vsk; |
| 318 | |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 319 | if (pkt->type == VMCI_TRANSPORT_PACKET_TYPE_RST) |
| 320 | return 0; |
Jorgen Hansen | a915b98 | 2018-12-18 00:34:06 -0800 | [diff] [blame] | 321 | |
| 322 | vsk = vsock_sk(sk); |
| 323 | |
| 324 | if (!vsock_addr_bound(&vsk->local_addr)) |
| 325 | return -EINVAL; |
| 326 | |
| 327 | if (vsock_addr_bound(&vsk->remote_addr)) { |
| 328 | dst_ptr = &vsk->remote_addr; |
| 329 | } else { |
| 330 | vsock_addr_init(&dst, pkt->dg.src.context, |
| 331 | pkt->src_port); |
| 332 | dst_ptr = &dst; |
| 333 | } |
| 334 | return vmci_transport_alloc_send_control_pkt(&vsk->local_addr, dst_ptr, |
| 335 | VMCI_TRANSPORT_PACKET_TYPE_RST, |
| 336 | 0, 0, NULL, VSOCK_PROTO_INVALID, |
| 337 | VMCI_INVALID_HANDLE); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | static int vmci_transport_send_negotiate(struct sock *sk, size_t size) |
| 341 | { |
| 342 | return vmci_transport_send_control_pkt( |
| 343 | sk, |
| 344 | VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE, |
| 345 | size, 0, NULL, |
| 346 | VSOCK_PROTO_INVALID, |
| 347 | VMCI_INVALID_HANDLE); |
| 348 | } |
| 349 | |
| 350 | static int vmci_transport_send_negotiate2(struct sock *sk, size_t size, |
| 351 | u16 version) |
| 352 | { |
| 353 | return vmci_transport_send_control_pkt( |
| 354 | sk, |
| 355 | VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE2, |
| 356 | size, 0, NULL, version, |
| 357 | VMCI_INVALID_HANDLE); |
| 358 | } |
| 359 | |
| 360 | static int vmci_transport_send_qp_offer(struct sock *sk, |
| 361 | struct vmci_handle handle) |
| 362 | { |
| 363 | return vmci_transport_send_control_pkt( |
| 364 | sk, VMCI_TRANSPORT_PACKET_TYPE_OFFER, 0, |
| 365 | 0, NULL, |
| 366 | VSOCK_PROTO_INVALID, handle); |
| 367 | } |
| 368 | |
| 369 | static int vmci_transport_send_attach(struct sock *sk, |
| 370 | struct vmci_handle handle) |
| 371 | { |
| 372 | return vmci_transport_send_control_pkt( |
| 373 | sk, VMCI_TRANSPORT_PACKET_TYPE_ATTACH, |
| 374 | 0, 0, NULL, VSOCK_PROTO_INVALID, |
| 375 | handle); |
| 376 | } |
| 377 | |
| 378 | static int vmci_transport_reply_reset(struct vmci_transport_packet *pkt) |
| 379 | { |
| 380 | return vmci_transport_reply_control_pkt_fast( |
| 381 | pkt, |
| 382 | VMCI_TRANSPORT_PACKET_TYPE_RST, |
| 383 | 0, 0, NULL, |
| 384 | VMCI_INVALID_HANDLE); |
| 385 | } |
| 386 | |
| 387 | static int vmci_transport_send_invalid_bh(struct sockaddr_vm *dst, |
| 388 | struct sockaddr_vm *src) |
| 389 | { |
| 390 | return vmci_transport_send_control_pkt_bh( |
| 391 | dst, src, |
| 392 | VMCI_TRANSPORT_PACKET_TYPE_INVALID, |
| 393 | 0, 0, NULL, VMCI_INVALID_HANDLE); |
| 394 | } |
| 395 | |
| 396 | int vmci_transport_send_wrote_bh(struct sockaddr_vm *dst, |
| 397 | struct sockaddr_vm *src) |
| 398 | { |
| 399 | return vmci_transport_send_control_pkt_bh( |
| 400 | dst, src, |
| 401 | VMCI_TRANSPORT_PACKET_TYPE_WROTE, 0, |
| 402 | 0, NULL, VMCI_INVALID_HANDLE); |
| 403 | } |
| 404 | |
| 405 | int vmci_transport_send_read_bh(struct sockaddr_vm *dst, |
| 406 | struct sockaddr_vm *src) |
| 407 | { |
| 408 | return vmci_transport_send_control_pkt_bh( |
| 409 | dst, src, |
| 410 | VMCI_TRANSPORT_PACKET_TYPE_READ, 0, |
| 411 | 0, NULL, VMCI_INVALID_HANDLE); |
| 412 | } |
| 413 | |
| 414 | int vmci_transport_send_wrote(struct sock *sk) |
| 415 | { |
| 416 | return vmci_transport_send_control_pkt( |
| 417 | sk, VMCI_TRANSPORT_PACKET_TYPE_WROTE, 0, |
| 418 | 0, NULL, VSOCK_PROTO_INVALID, |
| 419 | VMCI_INVALID_HANDLE); |
| 420 | } |
| 421 | |
| 422 | int vmci_transport_send_read(struct sock *sk) |
| 423 | { |
| 424 | return vmci_transport_send_control_pkt( |
| 425 | sk, VMCI_TRANSPORT_PACKET_TYPE_READ, 0, |
| 426 | 0, NULL, VSOCK_PROTO_INVALID, |
| 427 | VMCI_INVALID_HANDLE); |
| 428 | } |
| 429 | |
| 430 | int vmci_transport_send_waiting_write(struct sock *sk, |
| 431 | struct vmci_transport_waiting_info *wait) |
| 432 | { |
| 433 | return vmci_transport_send_control_pkt( |
| 434 | sk, VMCI_TRANSPORT_PACKET_TYPE_WAITING_WRITE, |
| 435 | 0, 0, wait, VSOCK_PROTO_INVALID, |
| 436 | VMCI_INVALID_HANDLE); |
| 437 | } |
| 438 | |
| 439 | int vmci_transport_send_waiting_read(struct sock *sk, |
| 440 | struct vmci_transport_waiting_info *wait) |
| 441 | { |
| 442 | return vmci_transport_send_control_pkt( |
| 443 | sk, VMCI_TRANSPORT_PACKET_TYPE_WAITING_READ, |
| 444 | 0, 0, wait, VSOCK_PROTO_INVALID, |
| 445 | VMCI_INVALID_HANDLE); |
| 446 | } |
| 447 | |
| 448 | static int vmci_transport_shutdown(struct vsock_sock *vsk, int mode) |
| 449 | { |
| 450 | return vmci_transport_send_control_pkt( |
| 451 | &vsk->sk, |
| 452 | VMCI_TRANSPORT_PACKET_TYPE_SHUTDOWN, |
| 453 | 0, mode, NULL, |
| 454 | VSOCK_PROTO_INVALID, |
| 455 | VMCI_INVALID_HANDLE); |
| 456 | } |
| 457 | |
| 458 | static int vmci_transport_send_conn_request(struct sock *sk, size_t size) |
| 459 | { |
| 460 | return vmci_transport_send_control_pkt(sk, |
| 461 | VMCI_TRANSPORT_PACKET_TYPE_REQUEST, |
| 462 | size, 0, NULL, |
| 463 | VSOCK_PROTO_INVALID, |
| 464 | VMCI_INVALID_HANDLE); |
| 465 | } |
| 466 | |
| 467 | static int vmci_transport_send_conn_request2(struct sock *sk, size_t size, |
| 468 | u16 version) |
| 469 | { |
| 470 | return vmci_transport_send_control_pkt( |
| 471 | sk, VMCI_TRANSPORT_PACKET_TYPE_REQUEST2, |
| 472 | size, 0, NULL, version, |
| 473 | VMCI_INVALID_HANDLE); |
| 474 | } |
| 475 | |
| 476 | static struct sock *vmci_transport_get_pending( |
| 477 | struct sock *listener, |
| 478 | struct vmci_transport_packet *pkt) |
| 479 | { |
| 480 | struct vsock_sock *vlistener; |
| 481 | struct vsock_sock *vpending; |
| 482 | struct sock *pending; |
Reilly Grant | 990454b | 2013-04-01 11:41:52 -0700 | [diff] [blame] | 483 | struct sockaddr_vm src; |
| 484 | |
| 485 | vsock_addr_init(&src, pkt->dg.src.context, pkt->src_port); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 486 | |
| 487 | vlistener = vsock_sk(listener); |
| 488 | |
| 489 | list_for_each_entry(vpending, &vlistener->pending_links, |
| 490 | pending_links) { |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 491 | if (vsock_addr_equals_addr(&src, &vpending->remote_addr) && |
Reilly Grant | 990454b | 2013-04-01 11:41:52 -0700 | [diff] [blame] | 492 | pkt->dst_port == vpending->local_addr.svm_port) { |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 493 | pending = sk_vsock(vpending); |
| 494 | sock_hold(pending); |
| 495 | goto found; |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | pending = NULL; |
| 500 | found: |
| 501 | return pending; |
| 502 | |
| 503 | } |
| 504 | |
| 505 | static void vmci_transport_release_pending(struct sock *pending) |
| 506 | { |
| 507 | sock_put(pending); |
| 508 | } |
| 509 | |
| 510 | /* We allow two kinds of sockets to communicate with a restricted VM: 1) |
| 511 | * trusted sockets 2) sockets from applications running as the same user as the |
| 512 | * VM (this is only true for the host side and only when using hosted products) |
| 513 | */ |
| 514 | |
| 515 | static bool vmci_transport_is_trusted(struct vsock_sock *vsock, u32 peer_cid) |
| 516 | { |
| 517 | return vsock->trusted || |
| 518 | vmci_is_context_owner(peer_cid, vsock->owner->uid); |
| 519 | } |
| 520 | |
| 521 | /* We allow sending datagrams to and receiving datagrams from a restricted VM |
| 522 | * only if it is trusted as described in vmci_transport_is_trusted. |
| 523 | */ |
| 524 | |
| 525 | static bool vmci_transport_allow_dgram(struct vsock_sock *vsock, u32 peer_cid) |
| 526 | { |
Reilly Grant | 2a89f92 | 2013-03-14 11:55:41 +0000 | [diff] [blame] | 527 | if (VMADDR_CID_HYPERVISOR == peer_cid) |
| 528 | return true; |
| 529 | |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 530 | if (vsock->cached_peer != peer_cid) { |
| 531 | vsock->cached_peer = peer_cid; |
| 532 | if (!vmci_transport_is_trusted(vsock, peer_cid) && |
| 533 | (vmci_context_get_priv_flags(peer_cid) & |
| 534 | VMCI_PRIVILEGE_FLAG_RESTRICTED)) { |
| 535 | vsock->cached_peer_allow_dgram = false; |
| 536 | } else { |
| 537 | vsock->cached_peer_allow_dgram = true; |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | return vsock->cached_peer_allow_dgram; |
| 542 | } |
| 543 | |
| 544 | static int |
| 545 | vmci_transport_queue_pair_alloc(struct vmci_qp **qpair, |
| 546 | struct vmci_handle *handle, |
| 547 | u64 produce_size, |
| 548 | u64 consume_size, |
| 549 | u32 peer, u32 flags, bool trusted) |
| 550 | { |
| 551 | int err = 0; |
| 552 | |
| 553 | if (trusted) { |
| 554 | /* Try to allocate our queue pair as trusted. This will only |
| 555 | * work if vsock is running in the host. |
| 556 | */ |
| 557 | |
| 558 | err = vmci_qpair_alloc(qpair, handle, produce_size, |
| 559 | consume_size, |
| 560 | peer, flags, |
| 561 | VMCI_PRIVILEGE_FLAG_TRUSTED); |
| 562 | if (err != VMCI_ERROR_NO_ACCESS) |
| 563 | goto out; |
| 564 | |
| 565 | } |
| 566 | |
| 567 | err = vmci_qpair_alloc(qpair, handle, produce_size, consume_size, |
| 568 | peer, flags, VMCI_NO_PRIVILEGE_FLAGS); |
| 569 | out: |
| 570 | if (err < 0) { |
Stefano Garzarella | e16edc9 | 2021-04-16 12:44:16 +0200 | [diff] [blame] | 571 | pr_err_once("Could not attach to queue pair with %d\n", err); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 572 | err = vmci_transport_error_to_vsock_error(err); |
| 573 | } |
| 574 | |
| 575 | return err; |
| 576 | } |
| 577 | |
| 578 | static int |
| 579 | vmci_transport_datagram_create_hnd(u32 resource_id, |
| 580 | u32 flags, |
| 581 | vmci_datagram_recv_cb recv_cb, |
| 582 | void *client_data, |
| 583 | struct vmci_handle *out_handle) |
| 584 | { |
| 585 | int err = 0; |
| 586 | |
| 587 | /* Try to allocate our datagram handler as trusted. This will only work |
| 588 | * if vsock is running in the host. |
| 589 | */ |
| 590 | |
| 591 | err = vmci_datagram_create_handle_priv(resource_id, flags, |
| 592 | VMCI_PRIVILEGE_FLAG_TRUSTED, |
| 593 | recv_cb, |
| 594 | client_data, out_handle); |
| 595 | |
| 596 | if (err == VMCI_ERROR_NO_ACCESS) |
| 597 | err = vmci_datagram_create_handle(resource_id, flags, |
| 598 | recv_cb, client_data, |
| 599 | out_handle); |
| 600 | |
| 601 | return err; |
| 602 | } |
| 603 | |
| 604 | /* This is invoked as part of a tasklet that's scheduled when the VMCI |
| 605 | * interrupt fires. This is run in bottom-half context and if it ever needs to |
| 606 | * sleep it should defer that work to a work queue. |
| 607 | */ |
| 608 | |
| 609 | static int vmci_transport_recv_dgram_cb(void *data, struct vmci_datagram *dg) |
| 610 | { |
| 611 | struct sock *sk; |
| 612 | size_t size; |
| 613 | struct sk_buff *skb; |
| 614 | struct vsock_sock *vsk; |
| 615 | |
| 616 | sk = (struct sock *)data; |
| 617 | |
| 618 | /* This handler is privileged when this module is running on the host. |
| 619 | * We will get datagrams from all endpoints (even VMs that are in a |
| 620 | * restricted context). If we get one from a restricted context then |
| 621 | * the destination socket must be trusted. |
| 622 | * |
| 623 | * NOTE: We access the socket struct without holding the lock here. |
| 624 | * This is ok because the field we are interested is never modified |
| 625 | * outside of the create and destruct socket functions. |
| 626 | */ |
| 627 | vsk = vsock_sk(sk); |
| 628 | if (!vmci_transport_allow_dgram(vsk, dg->src.context)) |
| 629 | return VMCI_ERROR_NO_ACCESS; |
| 630 | |
| 631 | size = VMCI_DG_SIZE(dg); |
| 632 | |
| 633 | /* Attach the packet to the socket's receive queue as an sk_buff. */ |
| 634 | skb = alloc_skb(size, GFP_ATOMIC); |
Asias He | dce1a28 | 2013-06-20 17:20:31 +0800 | [diff] [blame] | 635 | if (!skb) |
| 636 | return VMCI_ERROR_NO_MEM; |
| 637 | |
| 638 | /* sk_receive_skb() will do a sock_put(), so hold here. */ |
| 639 | sock_hold(sk); |
| 640 | skb_put(skb, size); |
| 641 | memcpy(skb->data, dg, size); |
| 642 | sk_receive_skb(sk, skb, 0); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 643 | |
| 644 | return VMCI_SUCCESS; |
| 645 | } |
| 646 | |
| 647 | static bool vmci_transport_stream_allow(u32 cid, u32 port) |
| 648 | { |
| 649 | static const u32 non_socket_contexts[] = { |
Stefano Garzarella | ef343b3 | 2019-12-10 11:43:03 +0100 | [diff] [blame] | 650 | VMADDR_CID_LOCAL, |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 651 | }; |
| 652 | int i; |
| 653 | |
| 654 | BUILD_BUG_ON(sizeof(cid) != sizeof(*non_socket_contexts)); |
| 655 | |
| 656 | for (i = 0; i < ARRAY_SIZE(non_socket_contexts); i++) { |
| 657 | if (cid == non_socket_contexts[i]) |
| 658 | return false; |
| 659 | } |
| 660 | |
| 661 | return true; |
| 662 | } |
| 663 | |
| 664 | /* This is invoked as part of a tasklet that's scheduled when the VMCI |
| 665 | * interrupt fires. This is run in bottom-half context but it defers most of |
| 666 | * its work to the packet handling work queue. |
| 667 | */ |
| 668 | |
| 669 | static int vmci_transport_recv_stream_cb(void *data, struct vmci_datagram *dg) |
| 670 | { |
| 671 | struct sock *sk; |
| 672 | struct sockaddr_vm dst; |
| 673 | struct sockaddr_vm src; |
| 674 | struct vmci_transport_packet *pkt; |
| 675 | struct vsock_sock *vsk; |
| 676 | bool bh_process_pkt; |
| 677 | int err; |
| 678 | |
| 679 | sk = NULL; |
| 680 | err = VMCI_SUCCESS; |
| 681 | bh_process_pkt = false; |
| 682 | |
| 683 | /* Ignore incoming packets from contexts without sockets, or resources |
| 684 | * that aren't vsock implementations. |
| 685 | */ |
| 686 | |
| 687 | if (!vmci_transport_stream_allow(dg->src.context, -1) |
Reilly Grant | 2a89f92 | 2013-03-14 11:55:41 +0000 | [diff] [blame] | 688 | || vmci_transport_peer_rid(dg->src.context) != dg->src.resource) |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 689 | return VMCI_ERROR_NO_ACCESS; |
| 690 | |
| 691 | if (VMCI_DG_SIZE(dg) < sizeof(*pkt)) |
| 692 | /* Drop datagrams that do not contain full VSock packets. */ |
| 693 | return VMCI_ERROR_INVALID_ARGS; |
| 694 | |
| 695 | pkt = (struct vmci_transport_packet *)dg; |
| 696 | |
| 697 | /* Find the socket that should handle this packet. First we look for a |
| 698 | * connected socket and if there is none we look for a socket bound to |
| 699 | * the destintation address. |
| 700 | */ |
| 701 | vsock_addr_init(&src, pkt->dg.src.context, pkt->src_port); |
| 702 | vsock_addr_init(&dst, pkt->dg.dst.context, pkt->dst_port); |
| 703 | |
| 704 | sk = vsock_find_connected_socket(&src, &dst); |
| 705 | if (!sk) { |
| 706 | sk = vsock_find_bound_socket(&dst); |
| 707 | if (!sk) { |
| 708 | /* We could not find a socket for this specified |
| 709 | * address. If this packet is a RST, we just drop it. |
| 710 | * If it is another packet, we send a RST. Note that |
| 711 | * we do not send a RST reply to RSTs so that we do not |
| 712 | * continually send RSTs between two endpoints. |
| 713 | * |
| 714 | * Note that since this is a reply, dst is src and src |
| 715 | * is dst. |
| 716 | */ |
| 717 | if (vmci_transport_send_reset_bh(&dst, &src, pkt) < 0) |
| 718 | pr_err("unable to send reset\n"); |
| 719 | |
| 720 | err = VMCI_ERROR_NOT_FOUND; |
| 721 | goto out; |
| 722 | } |
| 723 | } |
| 724 | |
| 725 | /* If the received packet type is beyond all types known to this |
| 726 | * implementation, reply with an invalid message. Hopefully this will |
| 727 | * help when implementing backwards compatibility in the future. |
| 728 | */ |
| 729 | if (pkt->type >= VMCI_TRANSPORT_PACKET_TYPE_MAX) { |
| 730 | vmci_transport_send_invalid_bh(&dst, &src); |
| 731 | err = VMCI_ERROR_INVALID_ARGS; |
| 732 | goto out; |
| 733 | } |
| 734 | |
| 735 | /* This handler is privileged when this module is running on the host. |
| 736 | * We will get datagram connect requests from all endpoints (even VMs |
| 737 | * that are in a restricted context). If we get one from a restricted |
| 738 | * context then the destination socket must be trusted. |
| 739 | * |
| 740 | * NOTE: We access the socket struct without holding the lock here. |
| 741 | * This is ok because the field we are interested is never modified |
| 742 | * outside of the create and destruct socket functions. |
| 743 | */ |
| 744 | vsk = vsock_sk(sk); |
| 745 | if (!vmci_transport_allow_dgram(vsk, pkt->dg.src.context)) { |
| 746 | err = VMCI_ERROR_NO_ACCESS; |
| 747 | goto out; |
| 748 | } |
| 749 | |
| 750 | /* We do most everything in a work queue, but let's fast path the |
| 751 | * notification of reads and writes to help data transfer performance. |
| 752 | * We can only do this if there is no process context code executing |
| 753 | * for this socket since that may change the state. |
| 754 | */ |
| 755 | bh_lock_sock(sk); |
| 756 | |
Reilly Grant | 990454b | 2013-04-01 11:41:52 -0700 | [diff] [blame] | 757 | if (!sock_owned_by_user(sk)) { |
| 758 | /* The local context ID may be out of date, update it. */ |
| 759 | vsk->local_addr.svm_cid = dst.svm_cid; |
| 760 | |
Stefan Hajnoczi | 3b4477d | 2017-10-05 16:46:52 -0400 | [diff] [blame] | 761 | if (sk->sk_state == TCP_ESTABLISHED) |
Reilly Grant | 990454b | 2013-04-01 11:41:52 -0700 | [diff] [blame] | 762 | vmci_trans(vsk)->notify_ops->handle_notify_pkt( |
| 763 | sk, pkt, true, &dst, &src, |
| 764 | &bh_process_pkt); |
| 765 | } |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 766 | |
| 767 | bh_unlock_sock(sk); |
| 768 | |
| 769 | if (!bh_process_pkt) { |
| 770 | struct vmci_transport_recv_pkt_info *recv_pkt_info; |
| 771 | |
| 772 | recv_pkt_info = kmalloc(sizeof(*recv_pkt_info), GFP_ATOMIC); |
| 773 | if (!recv_pkt_info) { |
| 774 | if (vmci_transport_send_reset_bh(&dst, &src, pkt) < 0) |
| 775 | pr_err("unable to send reset\n"); |
| 776 | |
| 777 | err = VMCI_ERROR_NO_MEM; |
| 778 | goto out; |
| 779 | } |
| 780 | |
| 781 | recv_pkt_info->sk = sk; |
| 782 | memcpy(&recv_pkt_info->pkt, pkt, sizeof(recv_pkt_info->pkt)); |
| 783 | INIT_WORK(&recv_pkt_info->work, vmci_transport_recv_pkt_work); |
| 784 | |
| 785 | schedule_work(&recv_pkt_info->work); |
| 786 | /* Clear sk so that the reference count incremented by one of |
| 787 | * the Find functions above is not decremented below. We need |
| 788 | * that reference count for the packet handler we've scheduled |
| 789 | * to run. |
| 790 | */ |
| 791 | sk = NULL; |
| 792 | } |
| 793 | |
| 794 | out: |
| 795 | if (sk) |
| 796 | sock_put(sk); |
| 797 | |
| 798 | return err; |
| 799 | } |
| 800 | |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 801 | static void vmci_transport_handle_detach(struct sock *sk) |
| 802 | { |
| 803 | struct vsock_sock *vsk; |
| 804 | |
| 805 | vsk = vsock_sk(sk); |
| 806 | if (!vmci_handle_is_invalid(vmci_trans(vsk)->qp_handle)) { |
| 807 | sock_set_flag(sk, SOCK_DONE); |
| 808 | |
| 809 | /* On a detach the peer will not be sending or receiving |
| 810 | * anymore. |
| 811 | */ |
| 812 | vsk->peer_shutdown = SHUTDOWN_MASK; |
| 813 | |
| 814 | /* We should not be sending anymore since the peer won't be |
| 815 | * there to receive, but we can still receive if there is data |
Jorgen Hansen | afbea2c | 2017-11-24 06:25:28 -0800 | [diff] [blame] | 816 | * left in our consume queue. If the local endpoint is a host, |
| 817 | * we can't call vsock_stream_has_data, since that may block, |
| 818 | * but a host endpoint can't read data once the VM has |
| 819 | * detached, so there is no available data in that case. |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 820 | */ |
Jorgen Hansen | afbea2c | 2017-11-24 06:25:28 -0800 | [diff] [blame] | 821 | if (vsk->local_addr.svm_cid == VMADDR_CID_HOST || |
| 822 | vsock_stream_has_data(vsk) <= 0) { |
Stefan Hajnoczi | 3b4477d | 2017-10-05 16:46:52 -0400 | [diff] [blame] | 823 | if (sk->sk_state == TCP_SYN_SENT) { |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 824 | /* The peer may detach from a queue pair while |
| 825 | * we are still in the connecting state, i.e., |
| 826 | * if the peer VM is killed after attaching to |
| 827 | * a queue pair, but before we complete the |
| 828 | * handshake. In that case, we treat the detach |
| 829 | * event like a reset. |
| 830 | */ |
| 831 | |
Jorgen Hansen | 4a5def7 | 2017-11-27 05:29:32 -0800 | [diff] [blame] | 832 | sk->sk_state = TCP_CLOSE; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 833 | sk->sk_err = ECONNRESET; |
Alexander Aring | e3ae236 | 2021-06-27 18:48:21 -0400 | [diff] [blame] | 834 | sk_error_report(sk); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 835 | return; |
| 836 | } |
Jorgen Hansen | 4a5def7 | 2017-11-27 05:29:32 -0800 | [diff] [blame] | 837 | sk->sk_state = TCP_CLOSE; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 838 | } |
| 839 | sk->sk_state_change(sk); |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | static void vmci_transport_peer_detach_cb(u32 sub_id, |
| 844 | const struct vmci_event_data *e_data, |
| 845 | void *client_data) |
| 846 | { |
Jorgen Hansen | 4ef7ea9 | 2015-10-21 04:53:56 -0700 | [diff] [blame] | 847 | struct vmci_transport *trans = client_data; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 848 | const struct vmci_event_payload_qp *e_payload; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 849 | |
| 850 | e_payload = vmci_event_data_const_payload(e_data); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 851 | |
| 852 | /* XXX This is lame, we should provide a way to lookup sockets by |
| 853 | * qp_handle. |
| 854 | */ |
Jorgen Hansen | 4ef7ea9 | 2015-10-21 04:53:56 -0700 | [diff] [blame] | 855 | if (vmci_handle_is_invalid(e_payload->handle) || |
Jorgen Hansen | 8ab18d7 | 2016-04-05 01:59:32 -0700 | [diff] [blame] | 856 | !vmci_handle_is_equal(trans->qp_handle, e_payload->handle)) |
Jorgen Hansen | 4ef7ea9 | 2015-10-21 04:53:56 -0700 | [diff] [blame] | 857 | return; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 858 | |
Jorgen Hansen | 4ef7ea9 | 2015-10-21 04:53:56 -0700 | [diff] [blame] | 859 | /* We don't ask for delayed CBs when we subscribe to this event (we |
| 860 | * pass 0 as flags to vmci_event_subscribe()). VMCI makes no |
| 861 | * guarantees in that case about what context we might be running in, |
| 862 | * so it could be BH or process, blockable or non-blockable. So we |
| 863 | * need to account for all possible contexts here. |
| 864 | */ |
| 865 | spin_lock_bh(&trans->lock); |
| 866 | if (!trans->sk) |
| 867 | goto out; |
| 868 | |
| 869 | /* Apart from here, trans->lock is only grabbed as part of sk destruct, |
| 870 | * where trans->sk isn't locked. |
| 871 | */ |
| 872 | bh_lock_sock(trans->sk); |
| 873 | |
| 874 | vmci_transport_handle_detach(trans->sk); |
| 875 | |
| 876 | bh_unlock_sock(trans->sk); |
| 877 | out: |
| 878 | spin_unlock_bh(&trans->lock); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 879 | } |
| 880 | |
| 881 | static void vmci_transport_qp_resumed_cb(u32 sub_id, |
| 882 | const struct vmci_event_data *e_data, |
| 883 | void *client_data) |
| 884 | { |
| 885 | vsock_for_each_connected_socket(vmci_transport_handle_detach); |
| 886 | } |
| 887 | |
| 888 | static void vmci_transport_recv_pkt_work(struct work_struct *work) |
| 889 | { |
| 890 | struct vmci_transport_recv_pkt_info *recv_pkt_info; |
| 891 | struct vmci_transport_packet *pkt; |
| 892 | struct sock *sk; |
| 893 | |
| 894 | recv_pkt_info = |
| 895 | container_of(work, struct vmci_transport_recv_pkt_info, work); |
| 896 | sk = recv_pkt_info->sk; |
| 897 | pkt = &recv_pkt_info->pkt; |
| 898 | |
| 899 | lock_sock(sk); |
| 900 | |
Reilly Grant | 990454b | 2013-04-01 11:41:52 -0700 | [diff] [blame] | 901 | /* The local context ID may be out of date. */ |
| 902 | vsock_sk(sk)->local_addr.svm_cid = pkt->dg.dst.context; |
| 903 | |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 904 | switch (sk->sk_state) { |
Stefan Hajnoczi | 3b4477d | 2017-10-05 16:46:52 -0400 | [diff] [blame] | 905 | case TCP_LISTEN: |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 906 | vmci_transport_recv_listen(sk, pkt); |
| 907 | break; |
Stefan Hajnoczi | 3b4477d | 2017-10-05 16:46:52 -0400 | [diff] [blame] | 908 | case TCP_SYN_SENT: |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 909 | /* Processing of pending connections for servers goes through |
| 910 | * the listening socket, so see vmci_transport_recv_listen() |
| 911 | * for that path. |
| 912 | */ |
| 913 | vmci_transport_recv_connecting_client(sk, pkt); |
| 914 | break; |
Stefan Hajnoczi | 3b4477d | 2017-10-05 16:46:52 -0400 | [diff] [blame] | 915 | case TCP_ESTABLISHED: |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 916 | vmci_transport_recv_connected(sk, pkt); |
| 917 | break; |
| 918 | default: |
| 919 | /* Because this function does not run in the same context as |
| 920 | * vmci_transport_recv_stream_cb it is possible that the |
| 921 | * socket has closed. We need to let the other side know or it |
| 922 | * could be sitting in a connect and hang forever. Send a |
| 923 | * reset to prevent that. |
| 924 | */ |
| 925 | vmci_transport_send_reset(sk, pkt); |
Asias He | 0fc9324 | 2013-06-20 17:20:32 +0800 | [diff] [blame] | 926 | break; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 927 | } |
| 928 | |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 929 | release_sock(sk); |
| 930 | kfree(recv_pkt_info); |
| 931 | /* Release reference obtained in the stream callback when we fetched |
| 932 | * this socket out of the bound or connected list. |
| 933 | */ |
| 934 | sock_put(sk); |
| 935 | } |
| 936 | |
| 937 | static int vmci_transport_recv_listen(struct sock *sk, |
| 938 | struct vmci_transport_packet *pkt) |
| 939 | { |
| 940 | struct sock *pending; |
| 941 | struct vsock_sock *vpending; |
| 942 | int err; |
| 943 | u64 qp_size; |
| 944 | bool old_request = false; |
| 945 | bool old_pkt_proto = false; |
| 946 | |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 947 | /* Because we are in the listen state, we could be receiving a packet |
| 948 | * for ourself or any previous connection requests that we received. |
| 949 | * If it's the latter, we try to find a socket in our list of pending |
| 950 | * connections and, if we do, call the appropriate handler for the |
| 951 | * state that that socket is in. Otherwise we try to service the |
| 952 | * connection request. |
| 953 | */ |
| 954 | pending = vmci_transport_get_pending(sk, pkt); |
| 955 | if (pending) { |
| 956 | lock_sock(pending); |
Reilly Grant | 990454b | 2013-04-01 11:41:52 -0700 | [diff] [blame] | 957 | |
| 958 | /* The local context ID may be out of date. */ |
| 959 | vsock_sk(pending)->local_addr.svm_cid = pkt->dg.dst.context; |
| 960 | |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 961 | switch (pending->sk_state) { |
Stefan Hajnoczi | 3b4477d | 2017-10-05 16:46:52 -0400 | [diff] [blame] | 962 | case TCP_SYN_SENT: |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 963 | err = vmci_transport_recv_connecting_server(sk, |
| 964 | pending, |
| 965 | pkt); |
| 966 | break; |
| 967 | default: |
| 968 | vmci_transport_send_reset(pending, pkt); |
| 969 | err = -EINVAL; |
| 970 | } |
| 971 | |
| 972 | if (err < 0) |
| 973 | vsock_remove_pending(sk, pending); |
| 974 | |
| 975 | release_sock(pending); |
| 976 | vmci_transport_release_pending(pending); |
| 977 | |
| 978 | return err; |
| 979 | } |
| 980 | |
| 981 | /* The listen state only accepts connection requests. Reply with a |
| 982 | * reset unless we received a reset. |
| 983 | */ |
| 984 | |
| 985 | if (!(pkt->type == VMCI_TRANSPORT_PACKET_TYPE_REQUEST || |
| 986 | pkt->type == VMCI_TRANSPORT_PACKET_TYPE_REQUEST2)) { |
| 987 | vmci_transport_reply_reset(pkt); |
| 988 | return -EINVAL; |
| 989 | } |
| 990 | |
| 991 | if (pkt->u.size == 0) { |
| 992 | vmci_transport_reply_reset(pkt); |
| 993 | return -EINVAL; |
| 994 | } |
| 995 | |
| 996 | /* If this socket can't accommodate this connection request, we send a |
| 997 | * reset. Otherwise we create and initialize a child socket and reply |
| 998 | * with a connection negotiation. |
| 999 | */ |
| 1000 | if (sk->sk_ack_backlog >= sk->sk_max_ack_backlog) { |
| 1001 | vmci_transport_reply_reset(pkt); |
| 1002 | return -ECONNREFUSED; |
| 1003 | } |
| 1004 | |
Stefano Garzarella | b9ca2f5 | 2019-11-14 10:57:43 +0100 | [diff] [blame] | 1005 | pending = vsock_create_connected(sk); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1006 | if (!pending) { |
| 1007 | vmci_transport_send_reset(sk, pkt); |
| 1008 | return -ENOMEM; |
| 1009 | } |
| 1010 | |
| 1011 | vpending = vsock_sk(pending); |
| 1012 | |
| 1013 | vsock_addr_init(&vpending->local_addr, pkt->dg.dst.context, |
| 1014 | pkt->dst_port); |
| 1015 | vsock_addr_init(&vpending->remote_addr, pkt->dg.src.context, |
| 1016 | pkt->src_port); |
| 1017 | |
Stefano Garzarella | c0cfa2d | 2019-11-14 10:57:46 +0100 | [diff] [blame] | 1018 | err = vsock_assign_transport(vpending, vsock_sk(sk)); |
| 1019 | /* Transport assigned (looking at remote_addr) must be the same |
| 1020 | * where we received the request. |
| 1021 | */ |
| 1022 | if (err || !vmci_check_transport(vpending)) { |
| 1023 | vmci_transport_send_reset(sk, pkt); |
| 1024 | sock_put(pending); |
| 1025 | return err; |
| 1026 | } |
| 1027 | |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1028 | /* If the proposed size fits within our min/max, accept it. Otherwise |
| 1029 | * propose our own size. |
| 1030 | */ |
Stefano Garzarella | b9f2b0f | 2019-11-14 10:57:42 +0100 | [diff] [blame] | 1031 | if (pkt->u.size >= vpending->buffer_min_size && |
| 1032 | pkt->u.size <= vpending->buffer_max_size) { |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1033 | qp_size = pkt->u.size; |
| 1034 | } else { |
Stefano Garzarella | b9f2b0f | 2019-11-14 10:57:42 +0100 | [diff] [blame] | 1035 | qp_size = vpending->buffer_size; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1036 | } |
| 1037 | |
| 1038 | /* Figure out if we are using old or new requests based on the |
| 1039 | * overrides pkt types sent by our peer. |
| 1040 | */ |
| 1041 | if (vmci_transport_old_proto_override(&old_pkt_proto)) { |
| 1042 | old_request = old_pkt_proto; |
| 1043 | } else { |
| 1044 | if (pkt->type == VMCI_TRANSPORT_PACKET_TYPE_REQUEST) |
| 1045 | old_request = true; |
| 1046 | else if (pkt->type == VMCI_TRANSPORT_PACKET_TYPE_REQUEST2) |
| 1047 | old_request = false; |
| 1048 | |
| 1049 | } |
| 1050 | |
| 1051 | if (old_request) { |
| 1052 | /* Handle a REQUEST (or override) */ |
| 1053 | u16 version = VSOCK_PROTO_INVALID; |
| 1054 | if (vmci_transport_proto_to_notify_struct( |
| 1055 | pending, &version, true)) |
| 1056 | err = vmci_transport_send_negotiate(pending, qp_size); |
| 1057 | else |
| 1058 | err = -EINVAL; |
| 1059 | |
| 1060 | } else { |
| 1061 | /* Handle a REQUEST2 (or override) */ |
| 1062 | int proto_int = pkt->proto; |
| 1063 | int pos; |
| 1064 | u16 active_proto_version = 0; |
| 1065 | |
| 1066 | /* The list of possible protocols is the intersection of all |
| 1067 | * protocols the client supports ... plus all the protocols we |
| 1068 | * support. |
| 1069 | */ |
| 1070 | proto_int &= vmci_transport_new_proto_supported_versions(); |
| 1071 | |
| 1072 | /* We choose the highest possible protocol version and use that |
| 1073 | * one. |
| 1074 | */ |
| 1075 | pos = fls(proto_int); |
| 1076 | if (pos) { |
| 1077 | active_proto_version = (1 << (pos - 1)); |
| 1078 | if (vmci_transport_proto_to_notify_struct( |
| 1079 | pending, &active_proto_version, false)) |
| 1080 | err = vmci_transport_send_negotiate2(pending, |
| 1081 | qp_size, |
| 1082 | active_proto_version); |
| 1083 | else |
| 1084 | err = -EINVAL; |
| 1085 | |
| 1086 | } else { |
| 1087 | err = -EINVAL; |
| 1088 | } |
| 1089 | } |
| 1090 | |
| 1091 | if (err < 0) { |
| 1092 | vmci_transport_send_reset(sk, pkt); |
| 1093 | sock_put(pending); |
| 1094 | err = vmci_transport_error_to_vsock_error(err); |
| 1095 | goto out; |
| 1096 | } |
| 1097 | |
| 1098 | vsock_add_pending(sk, pending); |
Eric Dumazet | 7976a11 | 2019-11-05 14:11:52 -0800 | [diff] [blame] | 1099 | sk_acceptq_added(sk); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1100 | |
Stefan Hajnoczi | 3b4477d | 2017-10-05 16:46:52 -0400 | [diff] [blame] | 1101 | pending->sk_state = TCP_SYN_SENT; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1102 | vmci_trans(vpending)->produce_size = |
| 1103 | vmci_trans(vpending)->consume_size = qp_size; |
Stefano Garzarella | b9f2b0f | 2019-11-14 10:57:42 +0100 | [diff] [blame] | 1104 | vpending->buffer_size = qp_size; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1105 | |
| 1106 | vmci_trans(vpending)->notify_ops->process_request(pending); |
| 1107 | |
| 1108 | /* We might never receive another message for this socket and it's not |
| 1109 | * connected to any process, so we have to ensure it gets cleaned up |
| 1110 | * ourself. Our delayed work function will take care of that. Note |
| 1111 | * that we do not ever cancel this function since we have few |
| 1112 | * guarantees about its state when calling cancel_delayed_work(). |
| 1113 | * Instead we hold a reference on the socket for that function and make |
| 1114 | * it capable of handling cases where it needs to do nothing but |
| 1115 | * release that reference. |
| 1116 | */ |
| 1117 | vpending->listener = sk; |
| 1118 | sock_hold(sk); |
| 1119 | sock_hold(pending); |
Cong Wang | 455f05e | 2018-08-06 11:06:02 -0700 | [diff] [blame] | 1120 | schedule_delayed_work(&vpending->pending_work, HZ); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1121 | |
| 1122 | out: |
| 1123 | return err; |
| 1124 | } |
| 1125 | |
| 1126 | static int |
| 1127 | vmci_transport_recv_connecting_server(struct sock *listener, |
| 1128 | struct sock *pending, |
| 1129 | struct vmci_transport_packet *pkt) |
| 1130 | { |
| 1131 | struct vsock_sock *vpending; |
| 1132 | struct vmci_handle handle; |
| 1133 | struct vmci_qp *qpair; |
| 1134 | bool is_local; |
| 1135 | u32 flags; |
| 1136 | u32 detach_sub_id; |
| 1137 | int err; |
| 1138 | int skerr; |
| 1139 | |
| 1140 | vpending = vsock_sk(pending); |
| 1141 | detach_sub_id = VMCI_INVALID_ID; |
| 1142 | |
| 1143 | switch (pkt->type) { |
| 1144 | case VMCI_TRANSPORT_PACKET_TYPE_OFFER: |
| 1145 | if (vmci_handle_is_invalid(pkt->u.handle)) { |
| 1146 | vmci_transport_send_reset(pending, pkt); |
| 1147 | skerr = EPROTO; |
| 1148 | err = -EINVAL; |
| 1149 | goto destroy; |
| 1150 | } |
| 1151 | break; |
| 1152 | default: |
| 1153 | /* Close and cleanup the connection. */ |
| 1154 | vmci_transport_send_reset(pending, pkt); |
| 1155 | skerr = EPROTO; |
| 1156 | err = pkt->type == VMCI_TRANSPORT_PACKET_TYPE_RST ? 0 : -EINVAL; |
| 1157 | goto destroy; |
| 1158 | } |
| 1159 | |
| 1160 | /* In order to complete the connection we need to attach to the offered |
| 1161 | * queue pair and send an attach notification. We also subscribe to the |
| 1162 | * detach event so we know when our peer goes away, and we do that |
| 1163 | * before attaching so we don't miss an event. If all this succeeds, |
| 1164 | * we update our state and wakeup anything waiting in accept() for a |
| 1165 | * connection. |
| 1166 | */ |
| 1167 | |
| 1168 | /* We don't care about attach since we ensure the other side has |
| 1169 | * attached by specifying the ATTACH_ONLY flag below. |
| 1170 | */ |
| 1171 | err = vmci_event_subscribe(VMCI_EVENT_QP_PEER_DETACH, |
| 1172 | vmci_transport_peer_detach_cb, |
Jorgen Hansen | 4ef7ea9 | 2015-10-21 04:53:56 -0700 | [diff] [blame] | 1173 | vmci_trans(vpending), &detach_sub_id); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1174 | if (err < VMCI_SUCCESS) { |
| 1175 | vmci_transport_send_reset(pending, pkt); |
| 1176 | err = vmci_transport_error_to_vsock_error(err); |
| 1177 | skerr = -err; |
| 1178 | goto destroy; |
| 1179 | } |
| 1180 | |
| 1181 | vmci_trans(vpending)->detach_sub_id = detach_sub_id; |
| 1182 | |
| 1183 | /* Now attach to the queue pair the client created. */ |
| 1184 | handle = pkt->u.handle; |
| 1185 | |
| 1186 | /* vpending->local_addr always has a context id so we do not need to |
| 1187 | * worry about VMADDR_CID_ANY in this case. |
| 1188 | */ |
| 1189 | is_local = |
| 1190 | vpending->remote_addr.svm_cid == vpending->local_addr.svm_cid; |
| 1191 | flags = VMCI_QPFLAG_ATTACH_ONLY; |
| 1192 | flags |= is_local ? VMCI_QPFLAG_LOCAL : 0; |
| 1193 | |
| 1194 | err = vmci_transport_queue_pair_alloc( |
| 1195 | &qpair, |
| 1196 | &handle, |
| 1197 | vmci_trans(vpending)->produce_size, |
| 1198 | vmci_trans(vpending)->consume_size, |
| 1199 | pkt->dg.src.context, |
| 1200 | flags, |
| 1201 | vmci_transport_is_trusted( |
| 1202 | vpending, |
| 1203 | vpending->remote_addr.svm_cid)); |
| 1204 | if (err < 0) { |
| 1205 | vmci_transport_send_reset(pending, pkt); |
| 1206 | skerr = -err; |
| 1207 | goto destroy; |
| 1208 | } |
| 1209 | |
| 1210 | vmci_trans(vpending)->qp_handle = handle; |
| 1211 | vmci_trans(vpending)->qpair = qpair; |
| 1212 | |
| 1213 | /* When we send the attach message, we must be ready to handle incoming |
| 1214 | * control messages on the newly connected socket. So we move the |
| 1215 | * pending socket to the connected state before sending the attach |
| 1216 | * message. Otherwise, an incoming packet triggered by the attach being |
| 1217 | * received by the peer may be processed concurrently with what happens |
| 1218 | * below after sending the attach message, and that incoming packet |
| 1219 | * will find the listening socket instead of the (currently) pending |
| 1220 | * socket. Note that enqueueing the socket increments the reference |
| 1221 | * count, so even if a reset comes before the connection is accepted, |
| 1222 | * the socket will be valid until it is removed from the queue. |
| 1223 | * |
| 1224 | * If we fail sending the attach below, we remove the socket from the |
Stefan Hajnoczi | 3b4477d | 2017-10-05 16:46:52 -0400 | [diff] [blame] | 1225 | * connected list and move the socket to TCP_CLOSE before |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1226 | * releasing the lock, so a pending slow path processing of an incoming |
| 1227 | * packet will not see the socket in the connected state in that case. |
| 1228 | */ |
Stefan Hajnoczi | 3b4477d | 2017-10-05 16:46:52 -0400 | [diff] [blame] | 1229 | pending->sk_state = TCP_ESTABLISHED; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1230 | |
| 1231 | vsock_insert_connected(vpending); |
| 1232 | |
| 1233 | /* Notify our peer of our attach. */ |
| 1234 | err = vmci_transport_send_attach(pending, handle); |
| 1235 | if (err < 0) { |
| 1236 | vsock_remove_connected(vpending); |
| 1237 | pr_err("Could not send attach\n"); |
| 1238 | vmci_transport_send_reset(pending, pkt); |
| 1239 | err = vmci_transport_error_to_vsock_error(err); |
| 1240 | skerr = -err; |
| 1241 | goto destroy; |
| 1242 | } |
| 1243 | |
| 1244 | /* We have a connection. Move the now connected socket from the |
| 1245 | * listener's pending list to the accept queue so callers of accept() |
| 1246 | * can find it. |
| 1247 | */ |
| 1248 | vsock_remove_pending(listener, pending); |
| 1249 | vsock_enqueue_accept(listener, pending); |
| 1250 | |
gushengxian | 2685515 | 2021-06-09 18:11:59 -0700 | [diff] [blame] | 1251 | /* Callers of accept() will be waiting on the listening socket, not |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1252 | * the pending socket. |
| 1253 | */ |
Stefan Hajnoczi | 7362945 | 2015-11-04 12:58:42 +0000 | [diff] [blame] | 1254 | listener->sk_data_ready(listener); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1255 | |
| 1256 | return 0; |
| 1257 | |
| 1258 | destroy: |
| 1259 | pending->sk_err = skerr; |
Stefan Hajnoczi | 3b4477d | 2017-10-05 16:46:52 -0400 | [diff] [blame] | 1260 | pending->sk_state = TCP_CLOSE; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1261 | /* As long as we drop our reference, all necessary cleanup will handle |
| 1262 | * when the cleanup function drops its reference and our destruct |
| 1263 | * implementation is called. Note that since the listen handler will |
| 1264 | * remove pending from the pending list upon our failure, the cleanup |
| 1265 | * function won't drop the additional reference, which is why we do it |
| 1266 | * here. |
| 1267 | */ |
| 1268 | sock_put(pending); |
| 1269 | |
| 1270 | return err; |
| 1271 | } |
| 1272 | |
| 1273 | static int |
| 1274 | vmci_transport_recv_connecting_client(struct sock *sk, |
| 1275 | struct vmci_transport_packet *pkt) |
| 1276 | { |
| 1277 | struct vsock_sock *vsk; |
| 1278 | int err; |
| 1279 | int skerr; |
| 1280 | |
| 1281 | vsk = vsock_sk(sk); |
| 1282 | |
| 1283 | switch (pkt->type) { |
| 1284 | case VMCI_TRANSPORT_PACKET_TYPE_ATTACH: |
| 1285 | if (vmci_handle_is_invalid(pkt->u.handle) || |
| 1286 | !vmci_handle_is_equal(pkt->u.handle, |
| 1287 | vmci_trans(vsk)->qp_handle)) { |
| 1288 | skerr = EPROTO; |
| 1289 | err = -EINVAL; |
| 1290 | goto destroy; |
| 1291 | } |
| 1292 | |
| 1293 | /* Signify the socket is connected and wakeup the waiter in |
| 1294 | * connect(). Also place the socket in the connected table for |
| 1295 | * accounting (it can already be found since it's in the bound |
| 1296 | * table). |
| 1297 | */ |
Stefan Hajnoczi | 3b4477d | 2017-10-05 16:46:52 -0400 | [diff] [blame] | 1298 | sk->sk_state = TCP_ESTABLISHED; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1299 | sk->sk_socket->state = SS_CONNECTED; |
| 1300 | vsock_insert_connected(vsk); |
| 1301 | sk->sk_state_change(sk); |
| 1302 | |
| 1303 | break; |
| 1304 | case VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE: |
| 1305 | case VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE2: |
| 1306 | if (pkt->u.size == 0 |
| 1307 | || pkt->dg.src.context != vsk->remote_addr.svm_cid |
| 1308 | || pkt->src_port != vsk->remote_addr.svm_port |
| 1309 | || !vmci_handle_is_invalid(vmci_trans(vsk)->qp_handle) |
| 1310 | || vmci_trans(vsk)->qpair |
| 1311 | || vmci_trans(vsk)->produce_size != 0 |
| 1312 | || vmci_trans(vsk)->consume_size != 0 |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1313 | || vmci_trans(vsk)->detach_sub_id != VMCI_INVALID_ID) { |
| 1314 | skerr = EPROTO; |
| 1315 | err = -EINVAL; |
| 1316 | |
| 1317 | goto destroy; |
| 1318 | } |
| 1319 | |
| 1320 | err = vmci_transport_recv_connecting_client_negotiate(sk, pkt); |
| 1321 | if (err) { |
| 1322 | skerr = -err; |
| 1323 | goto destroy; |
| 1324 | } |
| 1325 | |
| 1326 | break; |
| 1327 | case VMCI_TRANSPORT_PACKET_TYPE_INVALID: |
| 1328 | err = vmci_transport_recv_connecting_client_invalid(sk, pkt); |
| 1329 | if (err) { |
| 1330 | skerr = -err; |
| 1331 | goto destroy; |
| 1332 | } |
| 1333 | |
| 1334 | break; |
| 1335 | case VMCI_TRANSPORT_PACKET_TYPE_RST: |
| 1336 | /* Older versions of the linux code (WS 6.5 / ESX 4.0) used to |
| 1337 | * continue processing here after they sent an INVALID packet. |
| 1338 | * This meant that we got a RST after the INVALID. We ignore a |
| 1339 | * RST after an INVALID. The common code doesn't send the RST |
| 1340 | * ... so we can hang if an old version of the common code |
| 1341 | * fails between getting a REQUEST and sending an OFFER back. |
| 1342 | * Not much we can do about it... except hope that it doesn't |
| 1343 | * happen. |
| 1344 | */ |
| 1345 | if (vsk->ignore_connecting_rst) { |
| 1346 | vsk->ignore_connecting_rst = false; |
| 1347 | } else { |
| 1348 | skerr = ECONNRESET; |
| 1349 | err = 0; |
| 1350 | goto destroy; |
| 1351 | } |
| 1352 | |
| 1353 | break; |
| 1354 | default: |
| 1355 | /* Close and cleanup the connection. */ |
| 1356 | skerr = EPROTO; |
| 1357 | err = -EINVAL; |
| 1358 | goto destroy; |
| 1359 | } |
| 1360 | |
| 1361 | return 0; |
| 1362 | |
| 1363 | destroy: |
| 1364 | vmci_transport_send_reset(sk, pkt); |
| 1365 | |
Stefan Hajnoczi | 3b4477d | 2017-10-05 16:46:52 -0400 | [diff] [blame] | 1366 | sk->sk_state = TCP_CLOSE; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1367 | sk->sk_err = skerr; |
Alexander Aring | e3ae236 | 2021-06-27 18:48:21 -0400 | [diff] [blame] | 1368 | sk_error_report(sk); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1369 | return err; |
| 1370 | } |
| 1371 | |
| 1372 | static int vmci_transport_recv_connecting_client_negotiate( |
| 1373 | struct sock *sk, |
| 1374 | struct vmci_transport_packet *pkt) |
| 1375 | { |
| 1376 | int err; |
| 1377 | struct vsock_sock *vsk; |
| 1378 | struct vmci_handle handle; |
| 1379 | struct vmci_qp *qpair; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1380 | u32 detach_sub_id; |
| 1381 | bool is_local; |
| 1382 | u32 flags; |
| 1383 | bool old_proto = true; |
| 1384 | bool old_pkt_proto; |
| 1385 | u16 version; |
| 1386 | |
| 1387 | vsk = vsock_sk(sk); |
| 1388 | handle = VMCI_INVALID_HANDLE; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1389 | detach_sub_id = VMCI_INVALID_ID; |
| 1390 | |
| 1391 | /* If we have gotten here then we should be past the point where old |
| 1392 | * linux vsock could have sent the bogus rst. |
| 1393 | */ |
| 1394 | vsk->sent_request = false; |
| 1395 | vsk->ignore_connecting_rst = false; |
| 1396 | |
| 1397 | /* Verify that we're OK with the proposed queue pair size */ |
Stefano Garzarella | b9f2b0f | 2019-11-14 10:57:42 +0100 | [diff] [blame] | 1398 | if (pkt->u.size < vsk->buffer_min_size || |
| 1399 | pkt->u.size > vsk->buffer_max_size) { |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1400 | err = -EINVAL; |
| 1401 | goto destroy; |
| 1402 | } |
| 1403 | |
| 1404 | /* At this point we know the CID the peer is using to talk to us. */ |
| 1405 | |
| 1406 | if (vsk->local_addr.svm_cid == VMADDR_CID_ANY) |
| 1407 | vsk->local_addr.svm_cid = pkt->dg.dst.context; |
| 1408 | |
| 1409 | /* Setup the notify ops to be the highest supported version that both |
| 1410 | * the server and the client support. |
| 1411 | */ |
| 1412 | |
| 1413 | if (vmci_transport_old_proto_override(&old_pkt_proto)) { |
| 1414 | old_proto = old_pkt_proto; |
| 1415 | } else { |
| 1416 | if (pkt->type == VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE) |
| 1417 | old_proto = true; |
| 1418 | else if (pkt->type == VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE2) |
| 1419 | old_proto = false; |
| 1420 | |
| 1421 | } |
| 1422 | |
| 1423 | if (old_proto) |
| 1424 | version = VSOCK_PROTO_INVALID; |
| 1425 | else |
| 1426 | version = pkt->proto; |
| 1427 | |
| 1428 | if (!vmci_transport_proto_to_notify_struct(sk, &version, old_proto)) { |
| 1429 | err = -EINVAL; |
| 1430 | goto destroy; |
| 1431 | } |
| 1432 | |
Jorgen Hansen | 4ef7ea9 | 2015-10-21 04:53:56 -0700 | [diff] [blame] | 1433 | /* Subscribe to detach events first. |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1434 | * |
| 1435 | * XXX We attach once for each queue pair created for now so it is easy |
| 1436 | * to find the socket (it's provided), but later we should only |
| 1437 | * subscribe once and add a way to lookup sockets by queue pair handle. |
| 1438 | */ |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1439 | err = vmci_event_subscribe(VMCI_EVENT_QP_PEER_DETACH, |
| 1440 | vmci_transport_peer_detach_cb, |
Jorgen Hansen | 4ef7ea9 | 2015-10-21 04:53:56 -0700 | [diff] [blame] | 1441 | vmci_trans(vsk), &detach_sub_id); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1442 | if (err < VMCI_SUCCESS) { |
| 1443 | err = vmci_transport_error_to_vsock_error(err); |
| 1444 | goto destroy; |
| 1445 | } |
| 1446 | |
| 1447 | /* Make VMCI select the handle for us. */ |
| 1448 | handle = VMCI_INVALID_HANDLE; |
| 1449 | is_local = vsk->remote_addr.svm_cid == vsk->local_addr.svm_cid; |
| 1450 | flags = is_local ? VMCI_QPFLAG_LOCAL : 0; |
| 1451 | |
| 1452 | err = vmci_transport_queue_pair_alloc(&qpair, |
| 1453 | &handle, |
| 1454 | pkt->u.size, |
| 1455 | pkt->u.size, |
| 1456 | vsk->remote_addr.svm_cid, |
| 1457 | flags, |
| 1458 | vmci_transport_is_trusted( |
| 1459 | vsk, |
| 1460 | vsk-> |
| 1461 | remote_addr.svm_cid)); |
| 1462 | if (err < 0) |
| 1463 | goto destroy; |
| 1464 | |
| 1465 | err = vmci_transport_send_qp_offer(sk, handle); |
| 1466 | if (err < 0) { |
| 1467 | err = vmci_transport_error_to_vsock_error(err); |
| 1468 | goto destroy; |
| 1469 | } |
| 1470 | |
| 1471 | vmci_trans(vsk)->qp_handle = handle; |
| 1472 | vmci_trans(vsk)->qpair = qpair; |
| 1473 | |
| 1474 | vmci_trans(vsk)->produce_size = vmci_trans(vsk)->consume_size = |
| 1475 | pkt->u.size; |
| 1476 | |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1477 | vmci_trans(vsk)->detach_sub_id = detach_sub_id; |
| 1478 | |
| 1479 | vmci_trans(vsk)->notify_ops->process_negotiate(sk); |
| 1480 | |
| 1481 | return 0; |
| 1482 | |
| 1483 | destroy: |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1484 | if (detach_sub_id != VMCI_INVALID_ID) |
| 1485 | vmci_event_unsubscribe(detach_sub_id); |
| 1486 | |
| 1487 | if (!vmci_handle_is_invalid(handle)) |
| 1488 | vmci_qpair_detach(&qpair); |
| 1489 | |
| 1490 | return err; |
| 1491 | } |
| 1492 | |
| 1493 | static int |
| 1494 | vmci_transport_recv_connecting_client_invalid(struct sock *sk, |
| 1495 | struct vmci_transport_packet *pkt) |
| 1496 | { |
| 1497 | int err = 0; |
| 1498 | struct vsock_sock *vsk = vsock_sk(sk); |
| 1499 | |
| 1500 | if (vsk->sent_request) { |
| 1501 | vsk->sent_request = false; |
| 1502 | vsk->ignore_connecting_rst = true; |
| 1503 | |
Stefano Garzarella | b9f2b0f | 2019-11-14 10:57:42 +0100 | [diff] [blame] | 1504 | err = vmci_transport_send_conn_request(sk, vsk->buffer_size); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1505 | if (err < 0) |
| 1506 | err = vmci_transport_error_to_vsock_error(err); |
| 1507 | else |
| 1508 | err = 0; |
| 1509 | |
| 1510 | } |
| 1511 | |
| 1512 | return err; |
| 1513 | } |
| 1514 | |
| 1515 | static int vmci_transport_recv_connected(struct sock *sk, |
| 1516 | struct vmci_transport_packet *pkt) |
| 1517 | { |
| 1518 | struct vsock_sock *vsk; |
| 1519 | bool pkt_processed = false; |
| 1520 | |
| 1521 | /* In cases where we are closing the connection, it's sufficient to |
| 1522 | * mark the state change (and maybe error) and wake up any waiting |
| 1523 | * threads. Since this is a connected socket, it's owned by a user |
| 1524 | * process and will be cleaned up when the failure is passed back on |
| 1525 | * the current or next system call. Our system call implementations |
| 1526 | * must therefore check for error and state changes on entry and when |
| 1527 | * being awoken. |
| 1528 | */ |
| 1529 | switch (pkt->type) { |
| 1530 | case VMCI_TRANSPORT_PACKET_TYPE_SHUTDOWN: |
| 1531 | if (pkt->u.mode) { |
| 1532 | vsk = vsock_sk(sk); |
| 1533 | |
| 1534 | vsk->peer_shutdown |= pkt->u.mode; |
| 1535 | sk->sk_state_change(sk); |
| 1536 | } |
| 1537 | break; |
| 1538 | |
| 1539 | case VMCI_TRANSPORT_PACKET_TYPE_RST: |
| 1540 | vsk = vsock_sk(sk); |
| 1541 | /* It is possible that we sent our peer a message (e.g a |
| 1542 | * WAITING_READ) right before we got notified that the peer had |
| 1543 | * detached. If that happens then we can get a RST pkt back |
| 1544 | * from our peer even though there is data available for us to |
| 1545 | * read. In that case, don't shutdown the socket completely but |
| 1546 | * instead allow the local client to finish reading data off |
| 1547 | * the queuepair. Always treat a RST pkt in connected mode like |
| 1548 | * a clean shutdown. |
| 1549 | */ |
| 1550 | sock_set_flag(sk, SOCK_DONE); |
| 1551 | vsk->peer_shutdown = SHUTDOWN_MASK; |
| 1552 | if (vsock_stream_has_data(vsk) <= 0) |
Stefan Hajnoczi | 3b4477d | 2017-10-05 16:46:52 -0400 | [diff] [blame] | 1553 | sk->sk_state = TCP_CLOSING; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1554 | |
| 1555 | sk->sk_state_change(sk); |
| 1556 | break; |
| 1557 | |
| 1558 | default: |
| 1559 | vsk = vsock_sk(sk); |
| 1560 | vmci_trans(vsk)->notify_ops->handle_notify_pkt( |
| 1561 | sk, pkt, false, NULL, NULL, |
| 1562 | &pkt_processed); |
| 1563 | if (!pkt_processed) |
| 1564 | return -EINVAL; |
| 1565 | |
| 1566 | break; |
| 1567 | } |
| 1568 | |
| 1569 | return 0; |
| 1570 | } |
| 1571 | |
| 1572 | static int vmci_transport_socket_init(struct vsock_sock *vsk, |
| 1573 | struct vsock_sock *psk) |
| 1574 | { |
| 1575 | vsk->trans = kmalloc(sizeof(struct vmci_transport), GFP_KERNEL); |
| 1576 | if (!vsk->trans) |
| 1577 | return -ENOMEM; |
| 1578 | |
| 1579 | vmci_trans(vsk)->dg_handle = VMCI_INVALID_HANDLE; |
| 1580 | vmci_trans(vsk)->qp_handle = VMCI_INVALID_HANDLE; |
| 1581 | vmci_trans(vsk)->qpair = NULL; |
| 1582 | vmci_trans(vsk)->produce_size = vmci_trans(vsk)->consume_size = 0; |
Jorgen Hansen | 4ef7ea9 | 2015-10-21 04:53:56 -0700 | [diff] [blame] | 1583 | vmci_trans(vsk)->detach_sub_id = VMCI_INVALID_ID; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1584 | vmci_trans(vsk)->notify_ops = NULL; |
Jorgen Hansen | 4ef7ea9 | 2015-10-21 04:53:56 -0700 | [diff] [blame] | 1585 | INIT_LIST_HEAD(&vmci_trans(vsk)->elem); |
| 1586 | vmci_trans(vsk)->sk = &vsk->sk; |
Jorgen Hansen | 8566b86 | 2015-10-22 08:25:25 -0700 | [diff] [blame] | 1587 | spin_lock_init(&vmci_trans(vsk)->lock); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1588 | |
| 1589 | return 0; |
| 1590 | } |
| 1591 | |
Jorgen Hansen | 4ef7ea9 | 2015-10-21 04:53:56 -0700 | [diff] [blame] | 1592 | static void vmci_transport_free_resources(struct list_head *transport_list) |
| 1593 | { |
| 1594 | while (!list_empty(transport_list)) { |
| 1595 | struct vmci_transport *transport = |
| 1596 | list_first_entry(transport_list, struct vmci_transport, |
| 1597 | elem); |
| 1598 | list_del(&transport->elem); |
| 1599 | |
| 1600 | if (transport->detach_sub_id != VMCI_INVALID_ID) { |
| 1601 | vmci_event_unsubscribe(transport->detach_sub_id); |
| 1602 | transport->detach_sub_id = VMCI_INVALID_ID; |
| 1603 | } |
| 1604 | |
| 1605 | if (!vmci_handle_is_invalid(transport->qp_handle)) { |
| 1606 | vmci_qpair_detach(&transport->qpair); |
| 1607 | transport->qp_handle = VMCI_INVALID_HANDLE; |
| 1608 | transport->produce_size = 0; |
| 1609 | transport->consume_size = 0; |
| 1610 | } |
| 1611 | |
| 1612 | kfree(transport); |
| 1613 | } |
| 1614 | } |
| 1615 | |
| 1616 | static void vmci_transport_cleanup(struct work_struct *work) |
| 1617 | { |
| 1618 | LIST_HEAD(pending); |
| 1619 | |
| 1620 | spin_lock_bh(&vmci_transport_cleanup_lock); |
| 1621 | list_replace_init(&vmci_transport_cleanup_list, &pending); |
| 1622 | spin_unlock_bh(&vmci_transport_cleanup_lock); |
| 1623 | vmci_transport_free_resources(&pending); |
| 1624 | } |
| 1625 | |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1626 | static void vmci_transport_destruct(struct vsock_sock *vsk) |
| 1627 | { |
Paolo Abeni | 225d946 | 2019-02-07 14:13:18 +0100 | [diff] [blame] | 1628 | /* transport can be NULL if we hit a failure at init() time */ |
| 1629 | if (!vmci_trans(vsk)) |
| 1630 | return; |
| 1631 | |
Jorgen Hansen | 4ef7ea9 | 2015-10-21 04:53:56 -0700 | [diff] [blame] | 1632 | /* Ensure that the detach callback doesn't use the sk/vsk |
| 1633 | * we are about to destruct. |
| 1634 | */ |
| 1635 | spin_lock_bh(&vmci_trans(vsk)->lock); |
| 1636 | vmci_trans(vsk)->sk = NULL; |
| 1637 | spin_unlock_bh(&vmci_trans(vsk)->lock); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1638 | |
| 1639 | if (vmci_trans(vsk)->notify_ops) |
| 1640 | vmci_trans(vsk)->notify_ops->socket_destruct(vsk); |
| 1641 | |
Jorgen Hansen | 4ef7ea9 | 2015-10-21 04:53:56 -0700 | [diff] [blame] | 1642 | spin_lock_bh(&vmci_transport_cleanup_lock); |
| 1643 | list_add(&vmci_trans(vsk)->elem, &vmci_transport_cleanup_list); |
| 1644 | spin_unlock_bh(&vmci_transport_cleanup_lock); |
| 1645 | schedule_work(&vmci_transport_cleanup_work); |
| 1646 | |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1647 | vsk->trans = NULL; |
| 1648 | } |
| 1649 | |
| 1650 | static void vmci_transport_release(struct vsock_sock *vsk) |
| 1651 | { |
Stefan Hajnoczi | 6773b7d | 2016-07-28 15:36:31 +0100 | [diff] [blame] | 1652 | vsock_remove_sock(vsk); |
| 1653 | |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1654 | if (!vmci_handle_is_invalid(vmci_trans(vsk)->dg_handle)) { |
| 1655 | vmci_datagram_destroy_handle(vmci_trans(vsk)->dg_handle); |
| 1656 | vmci_trans(vsk)->dg_handle = VMCI_INVALID_HANDLE; |
| 1657 | } |
| 1658 | } |
| 1659 | |
| 1660 | static int vmci_transport_dgram_bind(struct vsock_sock *vsk, |
| 1661 | struct sockaddr_vm *addr) |
| 1662 | { |
| 1663 | u32 port; |
| 1664 | u32 flags; |
| 1665 | int err; |
| 1666 | |
| 1667 | /* VMCI will select a resource ID for us if we provide |
| 1668 | * VMCI_INVALID_ID. |
| 1669 | */ |
| 1670 | port = addr->svm_port == VMADDR_PORT_ANY ? |
| 1671 | VMCI_INVALID_ID : addr->svm_port; |
| 1672 | |
| 1673 | if (port <= LAST_RESERVED_PORT && !capable(CAP_NET_BIND_SERVICE)) |
| 1674 | return -EACCES; |
| 1675 | |
| 1676 | flags = addr->svm_cid == VMADDR_CID_ANY ? |
| 1677 | VMCI_FLAG_ANYCID_DG_HND : 0; |
| 1678 | |
| 1679 | err = vmci_transport_datagram_create_hnd(port, flags, |
| 1680 | vmci_transport_recv_dgram_cb, |
| 1681 | &vsk->sk, |
| 1682 | &vmci_trans(vsk)->dg_handle); |
| 1683 | if (err < VMCI_SUCCESS) |
| 1684 | return vmci_transport_error_to_vsock_error(err); |
| 1685 | vsock_addr_init(&vsk->local_addr, addr->svm_cid, |
| 1686 | vmci_trans(vsk)->dg_handle.resource); |
| 1687 | |
| 1688 | return 0; |
| 1689 | } |
| 1690 | |
| 1691 | static int vmci_transport_dgram_enqueue( |
| 1692 | struct vsock_sock *vsk, |
| 1693 | struct sockaddr_vm *remote_addr, |
Al Viro | 0f7db23 | 2014-11-20 04:05:34 -0500 | [diff] [blame] | 1694 | struct msghdr *msg, |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1695 | size_t len) |
| 1696 | { |
| 1697 | int err; |
| 1698 | struct vmci_datagram *dg; |
| 1699 | |
| 1700 | if (len > VMCI_MAX_DG_PAYLOAD_SIZE) |
| 1701 | return -EMSGSIZE; |
| 1702 | |
| 1703 | if (!vmci_transport_allow_dgram(vsk, remote_addr->svm_cid)) |
| 1704 | return -EPERM; |
| 1705 | |
| 1706 | /* Allocate a buffer for the user's message and our packet header. */ |
| 1707 | dg = kmalloc(len + sizeof(*dg), GFP_KERNEL); |
| 1708 | if (!dg) |
| 1709 | return -ENOMEM; |
| 1710 | |
Al Viro | 0f7db23 | 2014-11-20 04:05:34 -0500 | [diff] [blame] | 1711 | memcpy_from_msg(VMCI_DG_PAYLOAD(dg), msg, len); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1712 | |
| 1713 | dg->dst = vmci_make_handle(remote_addr->svm_cid, |
| 1714 | remote_addr->svm_port); |
| 1715 | dg->src = vmci_make_handle(vsk->local_addr.svm_cid, |
| 1716 | vsk->local_addr.svm_port); |
| 1717 | dg->payload_size = len; |
| 1718 | |
| 1719 | err = vmci_datagram_send(dg); |
| 1720 | kfree(dg); |
| 1721 | if (err < 0) |
| 1722 | return vmci_transport_error_to_vsock_error(err); |
| 1723 | |
| 1724 | return err - sizeof(*dg); |
| 1725 | } |
| 1726 | |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 1727 | static int vmci_transport_dgram_dequeue(struct vsock_sock *vsk, |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1728 | struct msghdr *msg, size_t len, |
| 1729 | int flags) |
| 1730 | { |
| 1731 | int err; |
| 1732 | int noblock; |
| 1733 | struct vmci_datagram *dg; |
| 1734 | size_t payload_len; |
| 1735 | struct sk_buff *skb; |
| 1736 | |
| 1737 | noblock = flags & MSG_DONTWAIT; |
| 1738 | |
| 1739 | if (flags & MSG_OOB || flags & MSG_ERRQUEUE) |
| 1740 | return -EOPNOTSUPP; |
| 1741 | |
| 1742 | /* Retrieve the head sk_buff from the socket's receive queue. */ |
| 1743 | err = 0; |
| 1744 | skb = skb_recv_datagram(&vsk->sk, flags, noblock, &err); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1745 | if (!skb) |
Jorgen Hansen | 9c995cc | 2016-04-18 23:58:52 -0700 | [diff] [blame] | 1746 | return err; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1747 | |
| 1748 | dg = (struct vmci_datagram *)skb->data; |
| 1749 | if (!dg) |
| 1750 | /* err is 0, meaning we read zero bytes. */ |
| 1751 | goto out; |
| 1752 | |
| 1753 | payload_len = dg->payload_size; |
| 1754 | /* Ensure the sk_buff matches the payload size claimed in the packet. */ |
| 1755 | if (payload_len != skb->len - sizeof(*dg)) { |
| 1756 | err = -EINVAL; |
| 1757 | goto out; |
| 1758 | } |
| 1759 | |
| 1760 | if (payload_len > len) { |
| 1761 | payload_len = len; |
| 1762 | msg->msg_flags |= MSG_TRUNC; |
| 1763 | } |
| 1764 | |
| 1765 | /* Place the datagram payload in the user's iovec. */ |
David S. Miller | 51f3d02 | 2014-11-05 16:46:40 -0500 | [diff] [blame] | 1766 | err = skb_copy_datagram_msg(skb, sizeof(*dg), msg, payload_len); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1767 | if (err) |
| 1768 | goto out; |
| 1769 | |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1770 | if (msg->msg_name) { |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1771 | /* Provide the address of the sender. */ |
Steffen Hurrle | 342dfc3 | 2014-01-17 22:53:15 +0100 | [diff] [blame] | 1772 | DECLARE_SOCKADDR(struct sockaddr_vm *, vm_addr, msg->msg_name); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1773 | vsock_addr_init(vm_addr, dg->src.context, dg->src.resource); |
| 1774 | msg->msg_namelen = sizeof(*vm_addr); |
| 1775 | } |
| 1776 | err = payload_len; |
| 1777 | |
| 1778 | out: |
| 1779 | skb_free_datagram(&vsk->sk, skb); |
| 1780 | return err; |
| 1781 | } |
| 1782 | |
| 1783 | static bool vmci_transport_dgram_allow(u32 cid, u32 port) |
| 1784 | { |
| 1785 | if (cid == VMADDR_CID_HYPERVISOR) { |
| 1786 | /* Registrations of PBRPC Servers do not modify VMX/Hypervisor |
| 1787 | * state and are allowed. |
| 1788 | */ |
| 1789 | return port == VMCI_UNITY_PBRPC_REGISTER; |
| 1790 | } |
| 1791 | |
| 1792 | return true; |
| 1793 | } |
| 1794 | |
| 1795 | static int vmci_transport_connect(struct vsock_sock *vsk) |
| 1796 | { |
| 1797 | int err; |
| 1798 | bool old_pkt_proto = false; |
| 1799 | struct sock *sk = &vsk->sk; |
| 1800 | |
| 1801 | if (vmci_transport_old_proto_override(&old_pkt_proto) && |
| 1802 | old_pkt_proto) { |
Stefano Garzarella | b9f2b0f | 2019-11-14 10:57:42 +0100 | [diff] [blame] | 1803 | err = vmci_transport_send_conn_request(sk, vsk->buffer_size); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1804 | if (err < 0) { |
Stefan Hajnoczi | 3b4477d | 2017-10-05 16:46:52 -0400 | [diff] [blame] | 1805 | sk->sk_state = TCP_CLOSE; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1806 | return err; |
| 1807 | } |
| 1808 | } else { |
| 1809 | int supported_proto_versions = |
| 1810 | vmci_transport_new_proto_supported_versions(); |
Stefano Garzarella | b9f2b0f | 2019-11-14 10:57:42 +0100 | [diff] [blame] | 1811 | err = vmci_transport_send_conn_request2(sk, vsk->buffer_size, |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1812 | supported_proto_versions); |
| 1813 | if (err < 0) { |
Stefan Hajnoczi | 3b4477d | 2017-10-05 16:46:52 -0400 | [diff] [blame] | 1814 | sk->sk_state = TCP_CLOSE; |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1815 | return err; |
| 1816 | } |
| 1817 | |
| 1818 | vsk->sent_request = true; |
| 1819 | } |
| 1820 | |
| 1821 | return err; |
| 1822 | } |
| 1823 | |
| 1824 | static ssize_t vmci_transport_stream_dequeue( |
| 1825 | struct vsock_sock *vsk, |
Al Viro | 0f7db23 | 2014-11-20 04:05:34 -0500 | [diff] [blame] | 1826 | struct msghdr *msg, |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1827 | size_t len, |
| 1828 | int flags) |
| 1829 | { |
| 1830 | if (flags & MSG_PEEK) |
Al Viro | d838df2 | 2014-11-24 19:32:50 -0500 | [diff] [blame] | 1831 | return vmci_qpair_peekv(vmci_trans(vsk)->qpair, msg, len, 0); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1832 | else |
Al Viro | d838df2 | 2014-11-24 19:32:50 -0500 | [diff] [blame] | 1833 | return vmci_qpair_dequev(vmci_trans(vsk)->qpair, msg, len, 0); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1834 | } |
| 1835 | |
| 1836 | static ssize_t vmci_transport_stream_enqueue( |
| 1837 | struct vsock_sock *vsk, |
Al Viro | 0f7db23 | 2014-11-20 04:05:34 -0500 | [diff] [blame] | 1838 | struct msghdr *msg, |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1839 | size_t len) |
| 1840 | { |
Al Viro | 4c946d9 | 2014-11-27 19:52:04 -0500 | [diff] [blame] | 1841 | return vmci_qpair_enquev(vmci_trans(vsk)->qpair, msg, len, 0); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1842 | } |
| 1843 | |
| 1844 | static s64 vmci_transport_stream_has_data(struct vsock_sock *vsk) |
| 1845 | { |
| 1846 | return vmci_qpair_consume_buf_ready(vmci_trans(vsk)->qpair); |
| 1847 | } |
| 1848 | |
| 1849 | static s64 vmci_transport_stream_has_space(struct vsock_sock *vsk) |
| 1850 | { |
| 1851 | return vmci_qpair_produce_free_space(vmci_trans(vsk)->qpair); |
| 1852 | } |
| 1853 | |
| 1854 | static u64 vmci_transport_stream_rcvhiwat(struct vsock_sock *vsk) |
| 1855 | { |
| 1856 | return vmci_trans(vsk)->consume_size; |
| 1857 | } |
| 1858 | |
| 1859 | static bool vmci_transport_stream_is_active(struct vsock_sock *vsk) |
| 1860 | { |
| 1861 | return !vmci_handle_is_invalid(vmci_trans(vsk)->qp_handle); |
| 1862 | } |
| 1863 | |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 1864 | static int vmci_transport_notify_poll_in( |
| 1865 | struct vsock_sock *vsk, |
| 1866 | size_t target, |
| 1867 | bool *data_ready_now) |
| 1868 | { |
| 1869 | return vmci_trans(vsk)->notify_ops->poll_in( |
| 1870 | &vsk->sk, target, data_ready_now); |
| 1871 | } |
| 1872 | |
| 1873 | static int vmci_transport_notify_poll_out( |
| 1874 | struct vsock_sock *vsk, |
| 1875 | size_t target, |
| 1876 | bool *space_available_now) |
| 1877 | { |
| 1878 | return vmci_trans(vsk)->notify_ops->poll_out( |
| 1879 | &vsk->sk, target, space_available_now); |
| 1880 | } |
| 1881 | |
| 1882 | static int vmci_transport_notify_recv_init( |
| 1883 | struct vsock_sock *vsk, |
| 1884 | size_t target, |
| 1885 | struct vsock_transport_recv_notify_data *data) |
| 1886 | { |
| 1887 | return vmci_trans(vsk)->notify_ops->recv_init( |
| 1888 | &vsk->sk, target, |
| 1889 | (struct vmci_transport_recv_notify_data *)data); |
| 1890 | } |
| 1891 | |
| 1892 | static int vmci_transport_notify_recv_pre_block( |
| 1893 | struct vsock_sock *vsk, |
| 1894 | size_t target, |
| 1895 | struct vsock_transport_recv_notify_data *data) |
| 1896 | { |
| 1897 | return vmci_trans(vsk)->notify_ops->recv_pre_block( |
| 1898 | &vsk->sk, target, |
| 1899 | (struct vmci_transport_recv_notify_data *)data); |
| 1900 | } |
| 1901 | |
| 1902 | static int vmci_transport_notify_recv_pre_dequeue( |
| 1903 | struct vsock_sock *vsk, |
| 1904 | size_t target, |
| 1905 | struct vsock_transport_recv_notify_data *data) |
| 1906 | { |
| 1907 | return vmci_trans(vsk)->notify_ops->recv_pre_dequeue( |
| 1908 | &vsk->sk, target, |
| 1909 | (struct vmci_transport_recv_notify_data *)data); |
| 1910 | } |
| 1911 | |
| 1912 | static int vmci_transport_notify_recv_post_dequeue( |
| 1913 | struct vsock_sock *vsk, |
| 1914 | size_t target, |
| 1915 | ssize_t copied, |
| 1916 | bool data_read, |
| 1917 | struct vsock_transport_recv_notify_data *data) |
| 1918 | { |
| 1919 | return vmci_trans(vsk)->notify_ops->recv_post_dequeue( |
| 1920 | &vsk->sk, target, copied, data_read, |
| 1921 | (struct vmci_transport_recv_notify_data *)data); |
| 1922 | } |
| 1923 | |
| 1924 | static int vmci_transport_notify_send_init( |
| 1925 | struct vsock_sock *vsk, |
| 1926 | struct vsock_transport_send_notify_data *data) |
| 1927 | { |
| 1928 | return vmci_trans(vsk)->notify_ops->send_init( |
| 1929 | &vsk->sk, |
| 1930 | (struct vmci_transport_send_notify_data *)data); |
| 1931 | } |
| 1932 | |
| 1933 | static int vmci_transport_notify_send_pre_block( |
| 1934 | struct vsock_sock *vsk, |
| 1935 | struct vsock_transport_send_notify_data *data) |
| 1936 | { |
| 1937 | return vmci_trans(vsk)->notify_ops->send_pre_block( |
| 1938 | &vsk->sk, |
| 1939 | (struct vmci_transport_send_notify_data *)data); |
| 1940 | } |
| 1941 | |
| 1942 | static int vmci_transport_notify_send_pre_enqueue( |
| 1943 | struct vsock_sock *vsk, |
| 1944 | struct vsock_transport_send_notify_data *data) |
| 1945 | { |
| 1946 | return vmci_trans(vsk)->notify_ops->send_pre_enqueue( |
| 1947 | &vsk->sk, |
| 1948 | (struct vmci_transport_send_notify_data *)data); |
| 1949 | } |
| 1950 | |
| 1951 | static int vmci_transport_notify_send_post_enqueue( |
| 1952 | struct vsock_sock *vsk, |
| 1953 | ssize_t written, |
| 1954 | struct vsock_transport_send_notify_data *data) |
| 1955 | { |
| 1956 | return vmci_trans(vsk)->notify_ops->send_post_enqueue( |
| 1957 | &vsk->sk, written, |
| 1958 | (struct vmci_transport_send_notify_data *)data); |
| 1959 | } |
| 1960 | |
| 1961 | static bool vmci_transport_old_proto_override(bool *old_pkt_proto) |
| 1962 | { |
| 1963 | if (PROTOCOL_OVERRIDE != -1) { |
| 1964 | if (PROTOCOL_OVERRIDE == 0) |
| 1965 | *old_pkt_proto = true; |
| 1966 | else |
| 1967 | *old_pkt_proto = false; |
| 1968 | |
| 1969 | pr_info("Proto override in use\n"); |
| 1970 | return true; |
| 1971 | } |
| 1972 | |
| 1973 | return false; |
| 1974 | } |
| 1975 | |
| 1976 | static bool vmci_transport_proto_to_notify_struct(struct sock *sk, |
| 1977 | u16 *proto, |
| 1978 | bool old_pkt_proto) |
| 1979 | { |
| 1980 | struct vsock_sock *vsk = vsock_sk(sk); |
| 1981 | |
| 1982 | if (old_pkt_proto) { |
| 1983 | if (*proto != VSOCK_PROTO_INVALID) { |
| 1984 | pr_err("Can't set both an old and new protocol\n"); |
| 1985 | return false; |
| 1986 | } |
| 1987 | vmci_trans(vsk)->notify_ops = &vmci_transport_notify_pkt_ops; |
| 1988 | goto exit; |
| 1989 | } |
| 1990 | |
| 1991 | switch (*proto) { |
| 1992 | case VSOCK_PROTO_PKT_ON_NOTIFY: |
| 1993 | vmci_trans(vsk)->notify_ops = |
| 1994 | &vmci_transport_notify_pkt_q_state_ops; |
| 1995 | break; |
| 1996 | default: |
| 1997 | pr_err("Unknown notify protocol version\n"); |
| 1998 | return false; |
| 1999 | } |
| 2000 | |
| 2001 | exit: |
| 2002 | vmci_trans(vsk)->notify_ops->socket_init(sk); |
| 2003 | return true; |
| 2004 | } |
| 2005 | |
| 2006 | static u16 vmci_transport_new_proto_supported_versions(void) |
| 2007 | { |
| 2008 | if (PROTOCOL_OVERRIDE != -1) |
| 2009 | return PROTOCOL_OVERRIDE; |
| 2010 | |
| 2011 | return VSOCK_PROTO_ALL_SUPPORTED; |
| 2012 | } |
| 2013 | |
| 2014 | static u32 vmci_transport_get_local_cid(void) |
| 2015 | { |
| 2016 | return vmci_get_context_id(); |
| 2017 | } |
| 2018 | |
Stefano Garzarella | c0cfa2d | 2019-11-14 10:57:46 +0100 | [diff] [blame] | 2019 | static struct vsock_transport vmci_transport = { |
Stefano Garzarella | 6a2c0962 | 2019-11-14 10:57:48 +0100 | [diff] [blame] | 2020 | .module = THIS_MODULE, |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 2021 | .init = vmci_transport_socket_init, |
| 2022 | .destruct = vmci_transport_destruct, |
| 2023 | .release = vmci_transport_release, |
| 2024 | .connect = vmci_transport_connect, |
| 2025 | .dgram_bind = vmci_transport_dgram_bind, |
| 2026 | .dgram_dequeue = vmci_transport_dgram_dequeue, |
| 2027 | .dgram_enqueue = vmci_transport_dgram_enqueue, |
| 2028 | .dgram_allow = vmci_transport_dgram_allow, |
| 2029 | .stream_dequeue = vmci_transport_stream_dequeue, |
| 2030 | .stream_enqueue = vmci_transport_stream_enqueue, |
| 2031 | .stream_has_data = vmci_transport_stream_has_data, |
| 2032 | .stream_has_space = vmci_transport_stream_has_space, |
| 2033 | .stream_rcvhiwat = vmci_transport_stream_rcvhiwat, |
| 2034 | .stream_is_active = vmci_transport_stream_is_active, |
| 2035 | .stream_allow = vmci_transport_stream_allow, |
| 2036 | .notify_poll_in = vmci_transport_notify_poll_in, |
| 2037 | .notify_poll_out = vmci_transport_notify_poll_out, |
| 2038 | .notify_recv_init = vmci_transport_notify_recv_init, |
| 2039 | .notify_recv_pre_block = vmci_transport_notify_recv_pre_block, |
| 2040 | .notify_recv_pre_dequeue = vmci_transport_notify_recv_pre_dequeue, |
| 2041 | .notify_recv_post_dequeue = vmci_transport_notify_recv_post_dequeue, |
| 2042 | .notify_send_init = vmci_transport_notify_send_init, |
| 2043 | .notify_send_pre_block = vmci_transport_notify_send_pre_block, |
| 2044 | .notify_send_pre_enqueue = vmci_transport_notify_send_pre_enqueue, |
| 2045 | .notify_send_post_enqueue = vmci_transport_notify_send_post_enqueue, |
| 2046 | .shutdown = vmci_transport_shutdown, |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 2047 | .get_local_cid = vmci_transport_get_local_cid, |
| 2048 | }; |
| 2049 | |
Stefano Garzarella | c0cfa2d | 2019-11-14 10:57:46 +0100 | [diff] [blame] | 2050 | static bool vmci_check_transport(struct vsock_sock *vsk) |
| 2051 | { |
| 2052 | return vsk->transport == &vmci_transport; |
| 2053 | } |
| 2054 | |
Stefano Garzarella | fdb4276 | 2020-06-05 17:12:41 +0200 | [diff] [blame] | 2055 | static void vmci_vsock_transport_cb(bool is_host) |
Stefano Garzarella | b1bba80 | 2019-11-14 10:57:47 +0100 | [diff] [blame] | 2056 | { |
| 2057 | int features; |
| 2058 | |
| 2059 | if (is_host) |
| 2060 | features = VSOCK_TRANSPORT_F_H2G; |
| 2061 | else |
| 2062 | features = VSOCK_TRANSPORT_F_G2H; |
| 2063 | |
| 2064 | vsock_core_register(&vmci_transport, features); |
| 2065 | } |
| 2066 | |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 2067 | static int __init vmci_transport_init(void) |
| 2068 | { |
| 2069 | int err; |
| 2070 | |
| 2071 | /* Create the datagram handle that we will use to send and receive all |
| 2072 | * VSocket control messages for this context. |
| 2073 | */ |
| 2074 | err = vmci_transport_datagram_create_hnd(VMCI_TRANSPORT_PACKET_RID, |
| 2075 | VMCI_FLAG_ANYCID_DG_HND, |
| 2076 | vmci_transport_recv_stream_cb, |
| 2077 | NULL, |
| 2078 | &vmci_transport_stream_handle); |
| 2079 | if (err < VMCI_SUCCESS) { |
| 2080 | pr_err("Unable to create datagram handle. (%d)\n", err); |
| 2081 | return vmci_transport_error_to_vsock_error(err); |
| 2082 | } |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 2083 | err = vmci_event_subscribe(VMCI_EVENT_QP_RESUMED, |
| 2084 | vmci_transport_qp_resumed_cb, |
| 2085 | NULL, &vmci_transport_qp_resumed_sub_id); |
| 2086 | if (err < VMCI_SUCCESS) { |
| 2087 | pr_err("Unable to subscribe to resumed event. (%d)\n", err); |
| 2088 | err = vmci_transport_error_to_vsock_error(err); |
| 2089 | vmci_transport_qp_resumed_sub_id = VMCI_INVALID_ID; |
| 2090 | goto err_destroy_stream_handle; |
| 2091 | } |
| 2092 | |
Stefano Garzarella | b1bba80 | 2019-11-14 10:57:47 +0100 | [diff] [blame] | 2093 | /* Register only with dgram feature, other features (H2G, G2H) will be |
| 2094 | * registered when the first host or guest becomes active. |
| 2095 | */ |
| 2096 | err = vsock_core_register(&vmci_transport, VSOCK_TRANSPORT_F_DGRAM); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 2097 | if (err < 0) |
| 2098 | goto err_unsubscribe; |
| 2099 | |
Stefano Garzarella | b1bba80 | 2019-11-14 10:57:47 +0100 | [diff] [blame] | 2100 | err = vmci_register_vsock_callback(vmci_vsock_transport_cb); |
| 2101 | if (err < 0) |
| 2102 | goto err_unregister; |
| 2103 | |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 2104 | return 0; |
| 2105 | |
Stefano Garzarella | b1bba80 | 2019-11-14 10:57:47 +0100 | [diff] [blame] | 2106 | err_unregister: |
| 2107 | vsock_core_unregister(&vmci_transport); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 2108 | err_unsubscribe: |
| 2109 | vmci_event_unsubscribe(vmci_transport_qp_resumed_sub_id); |
| 2110 | err_destroy_stream_handle: |
| 2111 | vmci_datagram_destroy_handle(vmci_transport_stream_handle); |
| 2112 | return err; |
| 2113 | } |
| 2114 | module_init(vmci_transport_init); |
| 2115 | |
| 2116 | static void __exit vmci_transport_exit(void) |
| 2117 | { |
Jorgen Hansen | 4ef7ea9 | 2015-10-21 04:53:56 -0700 | [diff] [blame] | 2118 | cancel_work_sync(&vmci_transport_cleanup_work); |
| 2119 | vmci_transport_free_resources(&vmci_transport_cleanup_list); |
| 2120 | |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 2121 | if (!vmci_handle_is_invalid(vmci_transport_stream_handle)) { |
| 2122 | if (vmci_datagram_destroy_handle( |
| 2123 | vmci_transport_stream_handle) != VMCI_SUCCESS) |
| 2124 | pr_err("Couldn't destroy datagram handle\n"); |
| 2125 | vmci_transport_stream_handle = VMCI_INVALID_HANDLE; |
| 2126 | } |
| 2127 | |
| 2128 | if (vmci_transport_qp_resumed_sub_id != VMCI_INVALID_ID) { |
| 2129 | vmci_event_unsubscribe(vmci_transport_qp_resumed_sub_id); |
| 2130 | vmci_transport_qp_resumed_sub_id = VMCI_INVALID_ID; |
| 2131 | } |
| 2132 | |
Stefano Garzarella | b1bba80 | 2019-11-14 10:57:47 +0100 | [diff] [blame] | 2133 | vmci_register_vsock_callback(NULL); |
Stefano Garzarella | c0cfa2d | 2019-11-14 10:57:46 +0100 | [diff] [blame] | 2134 | vsock_core_unregister(&vmci_transport); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 2135 | } |
| 2136 | module_exit(vmci_transport_exit); |
| 2137 | |
| 2138 | MODULE_AUTHOR("VMware, Inc."); |
| 2139 | MODULE_DESCRIPTION("VMCI transport for Virtual Sockets"); |
Jorgen Hansen | afbea2c | 2017-11-24 06:25:28 -0800 | [diff] [blame] | 2140 | MODULE_VERSION("1.0.5.0-k"); |
Andy King | d021c34 | 2013-02-06 14:23:56 +0000 | [diff] [blame] | 2141 | MODULE_LICENSE("GPL v2"); |
| 2142 | MODULE_ALIAS("vmware_vsock"); |
| 2143 | MODULE_ALIAS_NETPROTO(PF_VSOCK); |