1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * common code for virtio vsock 4 * 5 * Copyright (C) 2013-2015 Red Hat, Inc. 6 * Author: Asias He <asias@redhat.com> 7 * Stefan Hajnoczi <stefanha@redhat.com> 8 */ 9 #include <linux/spinlock.h> 10 #include <linux/module.h> 11 #include <linux/sched/signal.h> 12 #include <linux/ctype.h> 13 #include <linux/list.h> 14 #include <linux/virtio_vsock.h> 15 #include <uapi/linux/vsockmon.h> 16 17 #include <net/sock.h> 18 #include <net/af_vsock.h> 19 20 #define CREATE_TRACE_POINTS 21 #include <trace/events/vsock_virtio_transport_common.h> 22 23 /* How long to wait for graceful shutdown of a connection */ 24 #define VSOCK_CLOSE_TIMEOUT (8 * HZ) 25 26 /* Threshold for detecting small packets to copy */ 27 #define GOOD_COPY_LEN 128 28 29 static const struct virtio_transport * 30 virtio_transport_get_ops(struct vsock_sock *vsk) 31 { 32 const struct vsock_transport *t = vsock_core_get_transport(vsk); 33 34 if (WARN_ON(!t)) 35 return NULL; 36 37 return container_of(t, struct virtio_transport, transport); 38 } 39 40 static bool virtio_transport_can_zcopy(const struct virtio_transport *t_ops, 41 struct virtio_vsock_pkt_info *info, 42 size_t pkt_len) 43 { 44 struct iov_iter *iov_iter; 45 46 if (!info->msg) 47 return false; 48 49 iov_iter = &info->msg->msg_iter; 50 51 if (iov_iter->iov_offset) 52 return false; 53 54 /* We can't send whole iov. */ 55 if (iov_iter->count > pkt_len) 56 return false; 57 58 /* Check that transport can send data in zerocopy mode. */ 59 t_ops = virtio_transport_get_ops(info->vsk); 60 61 if (t_ops->can_msgzerocopy) { 62 int pages_to_send = iov_iter_npages(iov_iter, MAX_SKB_FRAGS); 63 64 /* +1 is for packet header. */ 65 return t_ops->can_msgzerocopy(pages_to_send + 1); 66 } 67 68 return true; 69 } 70 71 static int virtio_transport_init_zcopy_skb(struct vsock_sock *vsk, 72 struct sk_buff *skb, 73 struct msghdr *msg, 74 bool zerocopy) 75 { 76 struct ubuf_info *uarg; 77 78 if (msg->msg_ubuf) { 79 uarg = msg->msg_ubuf; 80 net_zcopy_get(uarg); 81 } else { 82 struct iov_iter *iter = &msg->msg_iter; 83 struct ubuf_info_msgzc *uarg_zc; 84 85 uarg = msg_zerocopy_realloc(sk_vsock(vsk), 86 iter->count, 87 NULL); 88 if (!uarg) 89 return -1; 90 91 uarg_zc = uarg_to_msgzc(uarg); 92 uarg_zc->zerocopy = zerocopy ? 1 : 0; 93 } 94 95 skb_zcopy_init(skb, uarg); 96 97 return 0; 98 } 99 100 static int virtio_transport_fill_skb(struct sk_buff *skb, 101 struct virtio_vsock_pkt_info *info, 102 size_t len, 103 bool zcopy) 104 { 105 if (zcopy) 106 return __zerocopy_sg_from_iter(info->msg, NULL, skb, 107 &info->msg->msg_iter, 108 len); 109 110 return memcpy_from_msg(skb_put(skb, len), info->msg, len); 111 } 112 113 static void virtio_transport_init_hdr(struct sk_buff *skb, 114 struct virtio_vsock_pkt_info *info, 115 size_t payload_len, 116 u32 src_cid, 117 u32 src_port, 118 u32 dst_cid, 119 u32 dst_port) 120 { 121 struct virtio_vsock_hdr *hdr; 122 123 hdr = virtio_vsock_hdr(skb); 124 hdr->type = cpu_to_le16(info->type); 125 hdr->op = cpu_to_le16(info->op); 126 hdr->src_cid = cpu_to_le64(src_cid); 127 hdr->dst_cid = cpu_to_le64(dst_cid); 128 hdr->src_port = cpu_to_le32(src_port); 129 hdr->dst_port = cpu_to_le32(dst_port); 130 hdr->flags = cpu_to_le32(info->flags); 131 hdr->len = cpu_to_le32(payload_len); 132 hdr->buf_alloc = cpu_to_le32(0); 133 hdr->fwd_cnt = cpu_to_le32(0); 134 } 135 136 static void virtio_transport_copy_nonlinear_skb(const struct sk_buff *skb, 137 void *dst, 138 size_t len) 139 { 140 struct iov_iter iov_iter = { 0 }; 141 struct kvec kvec; 142 size_t to_copy; 143 144 kvec.iov_base = dst; 145 kvec.iov_len = len; 146 147 iov_iter.iter_type = ITER_KVEC; 148 iov_iter.kvec = &kvec; 149 iov_iter.nr_segs = 1; 150 151 to_copy = min_t(size_t, len, skb->len); 152 153 skb_copy_datagram_iter(skb, VIRTIO_VSOCK_SKB_CB(skb)->offset, 154 &iov_iter, to_copy); 155 } 156 157 /* Packet capture */ 158 static struct sk_buff *virtio_transport_build_skb(void *opaque) 159 { 160 struct virtio_vsock_hdr *pkt_hdr; 161 struct sk_buff *pkt = opaque; 162 struct af_vsockmon_hdr *hdr; 163 struct sk_buff *skb; 164 size_t payload_len; 165 166 /* A packet could be split to fit the RX buffer, so we can retrieve 167 * the payload length from the header and the buffer pointer taking 168 * care of the offset in the original packet. 169 */ 170 pkt_hdr = virtio_vsock_hdr(pkt); 171 payload_len = pkt->len; 172 173 skb = alloc_skb(sizeof(*hdr) + sizeof(*pkt_hdr) + payload_len, 174 GFP_ATOMIC); 175 if (!skb) 176 return NULL; 177 178 hdr = skb_put(skb, sizeof(*hdr)); 179 180 /* pkt->hdr is little-endian so no need to byteswap here */ 181 hdr->src_cid = pkt_hdr->src_cid; 182 hdr->src_port = pkt_hdr->src_port; 183 hdr->dst_cid = pkt_hdr->dst_cid; 184 hdr->dst_port = pkt_hdr->dst_port; 185 186 hdr->transport = cpu_to_le16(AF_VSOCK_TRANSPORT_VIRTIO); 187 hdr->len = cpu_to_le16(sizeof(*pkt_hdr)); 188 memset(hdr->reserved, 0, sizeof(hdr->reserved)); 189 190 switch (le16_to_cpu(pkt_hdr->op)) { 191 case VIRTIO_VSOCK_OP_REQUEST: 192 case VIRTIO_VSOCK_OP_RESPONSE: 193 hdr->op = cpu_to_le16(AF_VSOCK_OP_CONNECT); 194 break; 195 case VIRTIO_VSOCK_OP_RST: 196 case VIRTIO_VSOCK_OP_SHUTDOWN: 197 hdr->op = cpu_to_le16(AF_VSOCK_OP_DISCONNECT); 198 break; 199 case VIRTIO_VSOCK_OP_RW: 200 hdr->op = cpu_to_le16(AF_VSOCK_OP_PAYLOAD); 201 break; 202 case VIRTIO_VSOCK_OP_CREDIT_UPDATE: 203 case VIRTIO_VSOCK_OP_CREDIT_REQUEST: 204 hdr->op = cpu_to_le16(AF_VSOCK_OP_CONTROL); 205 break; 206 default: 207 hdr->op = cpu_to_le16(AF_VSOCK_OP_UNKNOWN); 208 break; 209 } 210 211 skb_put_data(skb, pkt_hdr, sizeof(*pkt_hdr)); 212 213 if (payload_len) { 214 if (skb_is_nonlinear(pkt)) { 215 void *data = skb_put(skb, payload_len); 216 217 virtio_transport_copy_nonlinear_skb(pkt, data, payload_len); 218 } else { 219 skb_put_data(skb, pkt->data, payload_len); 220 } 221 } 222 223 return skb; 224 } 225 226 void virtio_transport_deliver_tap_pkt(struct sk_buff *skb) 227 { 228 if (virtio_vsock_skb_tap_delivered(skb)) 229 return; 230 231 vsock_deliver_tap(virtio_transport_build_skb, skb); 232 virtio_vsock_skb_set_tap_delivered(skb); 233 } 234 EXPORT_SYMBOL_GPL(virtio_transport_deliver_tap_pkt); 235 236 static u16 virtio_transport_get_type(struct sock *sk) 237 { 238 if (sk->sk_type == SOCK_STREAM) 239 return VIRTIO_VSOCK_TYPE_STREAM; 240 else 241 return VIRTIO_VSOCK_TYPE_SEQPACKET; 242 } 243 244 /* Returns new sk_buff on success, otherwise returns NULL. */ 245 static struct sk_buff *virtio_transport_alloc_skb(struct virtio_vsock_pkt_info *info, 246 size_t payload_len, 247 bool zcopy, 248 u32 src_cid, 249 u32 src_port, 250 u32 dst_cid, 251 u32 dst_port) 252 { 253 struct vsock_sock *vsk; 254 struct sk_buff *skb; 255 size_t skb_len; 256 257 skb_len = VIRTIO_VSOCK_SKB_HEADROOM; 258 259 if (!zcopy) 260 skb_len += payload_len; 261 262 skb = virtio_vsock_alloc_skb(skb_len, GFP_KERNEL); 263 if (!skb) 264 return NULL; 265 266 virtio_transport_init_hdr(skb, info, payload_len, src_cid, src_port, 267 dst_cid, dst_port); 268 269 vsk = info->vsk; 270 271 /* If 'vsk' != NULL then payload is always present, so we 272 * will never call '__zerocopy_sg_from_iter()' below without 273 * setting skb owner in 'skb_set_owner_w()'. The only case 274 * when 'vsk' == NULL is VIRTIO_VSOCK_OP_RST control message 275 * without payload. 276 */ 277 WARN_ON_ONCE(!(vsk && (info->msg && payload_len)) && zcopy); 278 279 /* Set owner here, because '__zerocopy_sg_from_iter()' uses 280 * owner of skb without check to update 'sk_wmem_alloc'. 281 */ 282 if (vsk) 283 skb_set_owner_w(skb, sk_vsock(vsk)); 284 285 if (info->msg && payload_len > 0) { 286 int err; 287 288 err = virtio_transport_fill_skb(skb, info, payload_len, zcopy); 289 if (err) 290 goto out; 291 292 if (msg_data_left(info->msg) == 0 && 293 info->type == VIRTIO_VSOCK_TYPE_SEQPACKET) { 294 struct virtio_vsock_hdr *hdr = virtio_vsock_hdr(skb); 295 296 hdr->flags |= cpu_to_le32(VIRTIO_VSOCK_SEQ_EOM); 297 298 if (info->msg->msg_flags & MSG_EOR) 299 hdr->flags |= cpu_to_le32(VIRTIO_VSOCK_SEQ_EOR); 300 } 301 } 302 303 if (info->reply) 304 virtio_vsock_skb_set_reply(skb); 305 306 trace_virtio_transport_alloc_pkt(src_cid, src_port, 307 dst_cid, dst_port, 308 payload_len, 309 info->type, 310 info->op, 311 info->flags, 312 zcopy); 313 314 return skb; 315 out: 316 kfree_skb(skb); 317 return NULL; 318 } 319 320 /* This function can only be used on connecting/connected sockets, 321 * since a socket assigned to a transport is required. 322 * 323 * Do not use on listener sockets! 324 */ 325 static int virtio_transport_send_pkt_info(struct vsock_sock *vsk, 326 struct virtio_vsock_pkt_info *info) 327 { 328 u32 max_skb_len = VIRTIO_VSOCK_MAX_PKT_BUF_SIZE; 329 u32 src_cid, src_port, dst_cid, dst_port; 330 const struct virtio_transport *t_ops; 331 struct virtio_vsock_sock *vvs; 332 u32 pkt_len = info->pkt_len; 333 bool can_zcopy = false; 334 u32 rest_len; 335 int ret; 336 337 info->type = virtio_transport_get_type(sk_vsock(vsk)); 338 339 t_ops = virtio_transport_get_ops(vsk); 340 if (unlikely(!t_ops)) 341 return -EFAULT; 342 343 src_cid = t_ops->transport.get_local_cid(); 344 src_port = vsk->local_addr.svm_port; 345 if (!info->remote_cid) { 346 dst_cid = vsk->remote_addr.svm_cid; 347 dst_port = vsk->remote_addr.svm_port; 348 } else { 349 dst_cid = info->remote_cid; 350 dst_port = info->remote_port; 351 } 352 353 vvs = vsk->trans; 354 355 /* virtio_transport_get_credit might return less than pkt_len credit */ 356 pkt_len = virtio_transport_get_credit(vvs, pkt_len); 357 358 /* Do not send zero length OP_RW pkt */ 359 if (pkt_len == 0 && info->op == VIRTIO_VSOCK_OP_RW) 360 return pkt_len; 361 362 if (info->msg) { 363 /* If zerocopy is not enabled by 'setsockopt()', we behave as 364 * there is no MSG_ZEROCOPY flag set. 365 */ 366 if (!sock_flag(sk_vsock(vsk), SOCK_ZEROCOPY)) 367 info->msg->msg_flags &= ~MSG_ZEROCOPY; 368 369 if (info->msg->msg_flags & MSG_ZEROCOPY) 370 can_zcopy = virtio_transport_can_zcopy(t_ops, info, pkt_len); 371 372 if (can_zcopy) 373 max_skb_len = min_t(u32, VIRTIO_VSOCK_MAX_PKT_BUF_SIZE, 374 (MAX_SKB_FRAGS * PAGE_SIZE)); 375 } 376 377 rest_len = pkt_len; 378 379 do { 380 struct sk_buff *skb; 381 size_t skb_len; 382 383 skb_len = min(max_skb_len, rest_len); 384 385 skb = virtio_transport_alloc_skb(info, skb_len, can_zcopy, 386 src_cid, src_port, 387 dst_cid, dst_port); 388 if (!skb) { 389 ret = -ENOMEM; 390 break; 391 } 392 393 /* We process buffer part by part, allocating skb on 394 * each iteration. If this is last skb for this buffer 395 * and MSG_ZEROCOPY mode is in use - we must allocate 396 * completion for the current syscall. 397 */ 398 if (info->msg && info->msg->msg_flags & MSG_ZEROCOPY && 399 skb_len == rest_len && info->op == VIRTIO_VSOCK_OP_RW) { 400 if (virtio_transport_init_zcopy_skb(vsk, skb, 401 info->msg, 402 can_zcopy)) { 403 ret = -ENOMEM; 404 break; 405 } 406 } 407 408 virtio_transport_inc_tx_pkt(vvs, skb); 409 410 ret = t_ops->send_pkt(skb); 411 if (ret < 0) 412 break; 413 414 /* Both virtio and vhost 'send_pkt()' returns 'skb_len', 415 * but for reliability use 'ret' instead of 'skb_len'. 416 * Also if partial send happens (e.g. 'ret' != 'skb_len') 417 * somehow, we break this loop, but account such returned 418 * value in 'virtio_transport_put_credit()'. 419 */ 420 rest_len -= ret; 421 422 if (WARN_ONCE(ret != skb_len, 423 "'send_pkt()' returns %i, but %zu expected\n", 424 ret, skb_len)) 425 break; 426 } while (rest_len); 427 428 virtio_transport_put_credit(vvs, rest_len); 429 430 /* Return number of bytes, if any data has been sent. */ 431 if (rest_len != pkt_len) 432 ret = pkt_len - rest_len; 433 434 return ret; 435 } 436 437 static bool virtio_transport_inc_rx_pkt(struct virtio_vsock_sock *vvs, 438 u32 len) 439 { 440 if (vvs->rx_bytes + len > vvs->buf_alloc) 441 return false; 442 443 vvs->rx_bytes += len; 444 return true; 445 } 446 447 static void virtio_transport_dec_rx_pkt(struct virtio_vsock_sock *vvs, 448 u32 len) 449 { 450 vvs->rx_bytes -= len; 451 vvs->fwd_cnt += len; 452 } 453 454 void virtio_transport_inc_tx_pkt(struct virtio_vsock_sock *vvs, struct sk_buff *skb) 455 { 456 struct virtio_vsock_hdr *hdr = virtio_vsock_hdr(skb); 457 458 spin_lock_bh(&vvs->rx_lock); 459 vvs->last_fwd_cnt = vvs->fwd_cnt; 460 hdr->fwd_cnt = cpu_to_le32(vvs->fwd_cnt); 461 hdr->buf_alloc = cpu_to_le32(vvs->buf_alloc); 462 spin_unlock_bh(&vvs->rx_lock); 463 } 464 EXPORT_SYMBOL_GPL(virtio_transport_inc_tx_pkt); 465 466 u32 virtio_transport_get_credit(struct virtio_vsock_sock *vvs, u32 credit) 467 { 468 u32 ret; 469 470 if (!credit) 471 return 0; 472 473 spin_lock_bh(&vvs->tx_lock); 474 ret = vvs->peer_buf_alloc - (vvs->tx_cnt - vvs->peer_fwd_cnt); 475 if (ret > credit) 476 ret = credit; 477 vvs->tx_cnt += ret; 478 spin_unlock_bh(&vvs->tx_lock); 479 480 return ret; 481 } 482 EXPORT_SYMBOL_GPL(virtio_transport_get_credit); 483 484 void virtio_transport_put_credit(struct virtio_vsock_sock *vvs, u32 credit) 485 { 486 if (!credit) 487 return; 488 489 spin_lock_bh(&vvs->tx_lock); 490 vvs->tx_cnt -= credit; 491 spin_unlock_bh(&vvs->tx_lock); 492 } 493 EXPORT_SYMBOL_GPL(virtio_transport_put_credit); 494 495 static int virtio_transport_send_credit_update(struct vsock_sock *vsk) 496 { 497 struct virtio_vsock_pkt_info info = { 498 .op = VIRTIO_VSOCK_OP_CREDIT_UPDATE, 499 .vsk = vsk, 500 }; 501 502 return virtio_transport_send_pkt_info(vsk, &info); 503 } 504 505 static ssize_t 506 virtio_transport_stream_do_peek(struct vsock_sock *vsk, 507 struct msghdr *msg, 508 size_t len) 509 { 510 struct virtio_vsock_sock *vvs = vsk->trans; 511 struct sk_buff *skb; 512 size_t total = 0; 513 int err; 514 515 spin_lock_bh(&vvs->rx_lock); 516 517 skb_queue_walk(&vvs->rx_queue, skb) { 518 size_t bytes; 519 520 bytes = len - total; 521 if (bytes > skb->len) 522 bytes = skb->len; 523 524 spin_unlock_bh(&vvs->rx_lock); 525 526 /* sk_lock is held by caller so no one else can dequeue. 527 * Unlock rx_lock since skb_copy_datagram_iter() may sleep. 528 */ 529 err = skb_copy_datagram_iter(skb, VIRTIO_VSOCK_SKB_CB(skb)->offset, 530 &msg->msg_iter, bytes); 531 if (err) 532 goto out; 533 534 total += bytes; 535 536 spin_lock_bh(&vvs->rx_lock); 537 538 if (total == len) 539 break; 540 } 541 542 spin_unlock_bh(&vvs->rx_lock); 543 544 return total; 545 546 out: 547 if (total) 548 err = total; 549 return err; 550 } 551 552 static ssize_t 553 virtio_transport_stream_do_dequeue(struct vsock_sock *vsk, 554 struct msghdr *msg, 555 size_t len) 556 { 557 struct virtio_vsock_sock *vvs = vsk->trans; 558 size_t bytes, total = 0; 559 struct sk_buff *skb; 560 int err = -EFAULT; 561 u32 free_space; 562 563 spin_lock_bh(&vvs->rx_lock); 564 565 if (WARN_ONCE(skb_queue_empty(&vvs->rx_queue) && vvs->rx_bytes, 566 "rx_queue is empty, but rx_bytes is non-zero\n")) { 567 spin_unlock_bh(&vvs->rx_lock); 568 return err; 569 } 570 571 while (total < len && !skb_queue_empty(&vvs->rx_queue)) { 572 skb = skb_peek(&vvs->rx_queue); 573 574 bytes = min_t(size_t, len - total, 575 skb->len - VIRTIO_VSOCK_SKB_CB(skb)->offset); 576 577 /* sk_lock is held by caller so no one else can dequeue. 578 * Unlock rx_lock since skb_copy_datagram_iter() may sleep. 579 */ 580 spin_unlock_bh(&vvs->rx_lock); 581 582 err = skb_copy_datagram_iter(skb, 583 VIRTIO_VSOCK_SKB_CB(skb)->offset, 584 &msg->msg_iter, bytes); 585 if (err) 586 goto out; 587 588 spin_lock_bh(&vvs->rx_lock); 589 590 total += bytes; 591 592 VIRTIO_VSOCK_SKB_CB(skb)->offset += bytes; 593 594 if (skb->len == VIRTIO_VSOCK_SKB_CB(skb)->offset) { 595 u32 pkt_len = le32_to_cpu(virtio_vsock_hdr(skb)->len); 596 597 virtio_transport_dec_rx_pkt(vvs, pkt_len); 598 __skb_unlink(skb, &vvs->rx_queue); 599 consume_skb(skb); 600 } 601 } 602 603 free_space = vvs->buf_alloc - (vvs->fwd_cnt - vvs->last_fwd_cnt); 604 605 spin_unlock_bh(&vvs->rx_lock); 606 607 /* To reduce the number of credit update messages, 608 * don't update credits as long as lots of space is available. 609 * Note: the limit chosen here is arbitrary. Setting the limit 610 * too high causes extra messages. Too low causes transmitter 611 * stalls. As stalls are in theory more expensive than extra 612 * messages, we set the limit to a high value. TODO: experiment 613 * with different values. 614 */ 615 if (free_space < VIRTIO_VSOCK_MAX_PKT_BUF_SIZE) 616 virtio_transport_send_credit_update(vsk); 617 618 return total; 619 620 out: 621 if (total) 622 err = total; 623 return err; 624 } 625 626 static ssize_t 627 virtio_transport_seqpacket_do_peek(struct vsock_sock *vsk, 628 struct msghdr *msg) 629 { 630 struct virtio_vsock_sock *vvs = vsk->trans; 631 struct sk_buff *skb; 632 size_t total, len; 633 634 spin_lock_bh(&vvs->rx_lock); 635 636 if (!vvs->msg_count) { 637 spin_unlock_bh(&vvs->rx_lock); 638 return 0; 639 } 640 641 total = 0; 642 len = msg_data_left(msg); 643 644 skb_queue_walk(&vvs->rx_queue, skb) { 645 struct virtio_vsock_hdr *hdr; 646 647 if (total < len) { 648 size_t bytes; 649 int err; 650 651 bytes = len - total; 652 if (bytes > skb->len) 653 bytes = skb->len; 654 655 spin_unlock_bh(&vvs->rx_lock); 656 657 /* sk_lock is held by caller so no one else can dequeue. 658 * Unlock rx_lock since skb_copy_datagram_iter() may sleep. 659 */ 660 err = skb_copy_datagram_iter(skb, VIRTIO_VSOCK_SKB_CB(skb)->offset, 661 &msg->msg_iter, bytes); 662 if (err) 663 return err; 664 665 spin_lock_bh(&vvs->rx_lock); 666 } 667 668 total += skb->len; 669 hdr = virtio_vsock_hdr(skb); 670 671 if (le32_to_cpu(hdr->flags) & VIRTIO_VSOCK_SEQ_EOM) { 672 if (le32_to_cpu(hdr->flags) & VIRTIO_VSOCK_SEQ_EOR) 673 msg->msg_flags |= MSG_EOR; 674 675 break; 676 } 677 } 678 679 spin_unlock_bh(&vvs->rx_lock); 680 681 return total; 682 } 683 684 static int virtio_transport_seqpacket_do_dequeue(struct vsock_sock *vsk, 685 struct msghdr *msg, 686 int flags) 687 { 688 struct virtio_vsock_sock *vvs = vsk->trans; 689 int dequeued_len = 0; 690 size_t user_buf_len = msg_data_left(msg); 691 bool msg_ready = false; 692 struct sk_buff *skb; 693 694 spin_lock_bh(&vvs->rx_lock); 695 696 if (vvs->msg_count == 0) { 697 spin_unlock_bh(&vvs->rx_lock); 698 return 0; 699 } 700 701 while (!msg_ready) { 702 struct virtio_vsock_hdr *hdr; 703 size_t pkt_len; 704 705 skb = __skb_dequeue(&vvs->rx_queue); 706 if (!skb) 707 break; 708 hdr = virtio_vsock_hdr(skb); 709 pkt_len = (size_t)le32_to_cpu(hdr->len); 710 711 if (dequeued_len >= 0) { 712 size_t bytes_to_copy; 713 714 bytes_to_copy = min(user_buf_len, pkt_len); 715 716 if (bytes_to_copy) { 717 int err; 718 719 /* sk_lock is held by caller so no one else can dequeue. 720 * Unlock rx_lock since skb_copy_datagram_iter() may sleep. 721 */ 722 spin_unlock_bh(&vvs->rx_lock); 723 724 err = skb_copy_datagram_iter(skb, 0, 725 &msg->msg_iter, 726 bytes_to_copy); 727 if (err) { 728 /* Copy of message failed. Rest of 729 * fragments will be freed without copy. 730 */ 731 dequeued_len = err; 732 } else { 733 user_buf_len -= bytes_to_copy; 734 } 735 736 spin_lock_bh(&vvs->rx_lock); 737 } 738 739 if (dequeued_len >= 0) 740 dequeued_len += pkt_len; 741 } 742 743 if (le32_to_cpu(hdr->flags) & VIRTIO_VSOCK_SEQ_EOM) { 744 msg_ready = true; 745 vvs->msg_count--; 746 747 if (le32_to_cpu(hdr->flags) & VIRTIO_VSOCK_SEQ_EOR) 748 msg->msg_flags |= MSG_EOR; 749 } 750 751 virtio_transport_dec_rx_pkt(vvs, pkt_len); 752 kfree_skb(skb); 753 } 754 755 spin_unlock_bh(&vvs->rx_lock); 756 757 virtio_transport_send_credit_update(vsk); 758 759 return dequeued_len; 760 } 761 762 ssize_t 763 virtio_transport_stream_dequeue(struct vsock_sock *vsk, 764 struct msghdr *msg, 765 size_t len, int flags) 766 { 767 if (flags & MSG_PEEK) 768 return virtio_transport_stream_do_peek(vsk, msg, len); 769 else 770 return virtio_transport_stream_do_dequeue(vsk, msg, len); 771 } 772 EXPORT_SYMBOL_GPL(virtio_transport_stream_dequeue); 773 774 ssize_t 775 virtio_transport_seqpacket_dequeue(struct vsock_sock *vsk, 776 struct msghdr *msg, 777 int flags) 778 { 779 if (flags & MSG_PEEK) 780 return virtio_transport_seqpacket_do_peek(vsk, msg); 781 else 782 return virtio_transport_seqpacket_do_dequeue(vsk, msg, flags); 783 } 784 EXPORT_SYMBOL_GPL(virtio_transport_seqpacket_dequeue); 785 786 int 787 virtio_transport_seqpacket_enqueue(struct vsock_sock *vsk, 788 struct msghdr *msg, 789 size_t len) 790 { 791 struct virtio_vsock_sock *vvs = vsk->trans; 792 793 spin_lock_bh(&vvs->tx_lock); 794 795 if (len > vvs->peer_buf_alloc) { 796 spin_unlock_bh(&vvs->tx_lock); 797 return -EMSGSIZE; 798 } 799 800 spin_unlock_bh(&vvs->tx_lock); 801 802 return virtio_transport_stream_enqueue(vsk, msg, len); 803 } 804 EXPORT_SYMBOL_GPL(virtio_transport_seqpacket_enqueue); 805 806 int 807 virtio_transport_dgram_dequeue(struct vsock_sock *vsk, 808 struct msghdr *msg, 809 size_t len, int flags) 810 { 811 return -EOPNOTSUPP; 812 } 813 EXPORT_SYMBOL_GPL(virtio_transport_dgram_dequeue); 814 815 s64 virtio_transport_stream_has_data(struct vsock_sock *vsk) 816 { 817 struct virtio_vsock_sock *vvs = vsk->trans; 818 s64 bytes; 819 820 spin_lock_bh(&vvs->rx_lock); 821 bytes = vvs->rx_bytes; 822 spin_unlock_bh(&vvs->rx_lock); 823 824 return bytes; 825 } 826 EXPORT_SYMBOL_GPL(virtio_transport_stream_has_data); 827 828 u32 virtio_transport_seqpacket_has_data(struct vsock_sock *vsk) 829 { 830 struct virtio_vsock_sock *vvs = vsk->trans; 831 u32 msg_count; 832 833 spin_lock_bh(&vvs->rx_lock); 834 msg_count = vvs->msg_count; 835 spin_unlock_bh(&vvs->rx_lock); 836 837 return msg_count; 838 } 839 EXPORT_SYMBOL_GPL(virtio_transport_seqpacket_has_data); 840 841 static s64 virtio_transport_has_space(struct vsock_sock *vsk) 842 { 843 struct virtio_vsock_sock *vvs = vsk->trans; 844 s64 bytes; 845 846 bytes = (s64)vvs->peer_buf_alloc - (vvs->tx_cnt - vvs->peer_fwd_cnt); 847 if (bytes < 0) 848 bytes = 0; 849 850 return bytes; 851 } 852 853 s64 virtio_transport_stream_has_space(struct vsock_sock *vsk) 854 { 855 struct virtio_vsock_sock *vvs = vsk->trans; 856 s64 bytes; 857 858 spin_lock_bh(&vvs->tx_lock); 859 bytes = virtio_transport_has_space(vsk); 860 spin_unlock_bh(&vvs->tx_lock); 861 862 return bytes; 863 } 864 EXPORT_SYMBOL_GPL(virtio_transport_stream_has_space); 865 866 int virtio_transport_do_socket_init(struct vsock_sock *vsk, 867 struct vsock_sock *psk) 868 { 869 struct virtio_vsock_sock *vvs; 870 871 vvs = kzalloc(sizeof(*vvs), GFP_KERNEL); 872 if (!vvs) 873 return -ENOMEM; 874 875 vsk->trans = vvs; 876 vvs->vsk = vsk; 877 if (psk && psk->trans) { 878 struct virtio_vsock_sock *ptrans = psk->trans; 879 880 vvs->peer_buf_alloc = ptrans->peer_buf_alloc; 881 } 882 883 if (vsk->buffer_size > VIRTIO_VSOCK_MAX_BUF_SIZE) 884 vsk->buffer_size = VIRTIO_VSOCK_MAX_BUF_SIZE; 885 886 vvs->buf_alloc = vsk->buffer_size; 887 888 spin_lock_init(&vvs->rx_lock); 889 spin_lock_init(&vvs->tx_lock); 890 skb_queue_head_init(&vvs->rx_queue); 891 892 return 0; 893 } 894 EXPORT_SYMBOL_GPL(virtio_transport_do_socket_init); 895 896 /* sk_lock held by the caller */ 897 void virtio_transport_notify_buffer_size(struct vsock_sock *vsk, u64 *val) 898 { 899 struct virtio_vsock_sock *vvs = vsk->trans; 900 901 if (*val > VIRTIO_VSOCK_MAX_BUF_SIZE) 902 *val = VIRTIO_VSOCK_MAX_BUF_SIZE; 903 904 vvs->buf_alloc = *val; 905 906 virtio_transport_send_credit_update(vsk); 907 } 908 EXPORT_SYMBOL_GPL(virtio_transport_notify_buffer_size); 909 910 int 911 virtio_transport_notify_poll_in(struct vsock_sock *vsk, 912 size_t target, 913 bool *data_ready_now) 914 { 915 *data_ready_now = vsock_stream_has_data(vsk) >= target; 916 917 return 0; 918 } 919 EXPORT_SYMBOL_GPL(virtio_transport_notify_poll_in); 920 921 int 922 virtio_transport_notify_poll_out(struct vsock_sock *vsk, 923 size_t target, 924 bool *space_avail_now) 925 { 926 s64 free_space; 927 928 free_space = vsock_stream_has_space(vsk); 929 if (free_space > 0) 930 *space_avail_now = true; 931 else if (free_space == 0) 932 *space_avail_now = false; 933 934 return 0; 935 } 936 EXPORT_SYMBOL_GPL(virtio_transport_notify_poll_out); 937 938 int virtio_transport_notify_recv_init(struct vsock_sock *vsk, 939 size_t target, struct vsock_transport_recv_notify_data *data) 940 { 941 return 0; 942 } 943 EXPORT_SYMBOL_GPL(virtio_transport_notify_recv_init); 944 945 int virtio_transport_notify_recv_pre_block(struct vsock_sock *vsk, 946 size_t target, struct vsock_transport_recv_notify_data *data) 947 { 948 return 0; 949 } 950 EXPORT_SYMBOL_GPL(virtio_transport_notify_recv_pre_block); 951 952 int virtio_transport_notify_recv_pre_dequeue(struct vsock_sock *vsk, 953 size_t target, struct vsock_transport_recv_notify_data *data) 954 { 955 return 0; 956 } 957 EXPORT_SYMBOL_GPL(virtio_transport_notify_recv_pre_dequeue); 958 959 int virtio_transport_notify_recv_post_dequeue(struct vsock_sock *vsk, 960 size_t target, ssize_t copied, bool data_read, 961 struct vsock_transport_recv_notify_data *data) 962 { 963 return 0; 964 } 965 EXPORT_SYMBOL_GPL(virtio_transport_notify_recv_post_dequeue); 966 967 int virtio_transport_notify_send_init(struct vsock_sock *vsk, 968 struct vsock_transport_send_notify_data *data) 969 { 970 return 0; 971 } 972 EXPORT_SYMBOL_GPL(virtio_transport_notify_send_init); 973 974 int virtio_transport_notify_send_pre_block(struct vsock_sock *vsk, 975 struct vsock_transport_send_notify_data *data) 976 { 977 return 0; 978 } 979 EXPORT_SYMBOL_GPL(virtio_transport_notify_send_pre_block); 980 981 int virtio_transport_notify_send_pre_enqueue(struct vsock_sock *vsk, 982 struct vsock_transport_send_notify_data *data) 983 { 984 return 0; 985 } 986 EXPORT_SYMBOL_GPL(virtio_transport_notify_send_pre_enqueue); 987 988 int virtio_transport_notify_send_post_enqueue(struct vsock_sock *vsk, 989 ssize_t written, struct vsock_transport_send_notify_data *data) 990 { 991 return 0; 992 } 993 EXPORT_SYMBOL_GPL(virtio_transport_notify_send_post_enqueue); 994 995 u64 virtio_transport_stream_rcvhiwat(struct vsock_sock *vsk) 996 { 997 return vsk->buffer_size; 998 } 999 EXPORT_SYMBOL_GPL(virtio_transport_stream_rcvhiwat); 1000 1001 bool virtio_transport_stream_is_active(struct vsock_sock *vsk) 1002 { 1003 return true; 1004 } 1005 EXPORT_SYMBOL_GPL(virtio_transport_stream_is_active); 1006 1007 bool virtio_transport_stream_allow(u32 cid, u32 port) 1008 { 1009 return true; 1010 } 1011 EXPORT_SYMBOL_GPL(virtio_transport_stream_allow); 1012 1013 int virtio_transport_dgram_bind(struct vsock_sock *vsk, 1014 struct sockaddr_vm *addr) 1015 { 1016 return -EOPNOTSUPP; 1017 } 1018 EXPORT_SYMBOL_GPL(virtio_transport_dgram_bind); 1019 1020 bool virtio_transport_dgram_allow(u32 cid, u32 port) 1021 { 1022 return false; 1023 } 1024 EXPORT_SYMBOL_GPL(virtio_transport_dgram_allow); 1025 1026 int virtio_transport_connect(struct vsock_sock *vsk) 1027 { 1028 struct virtio_vsock_pkt_info info = { 1029 .op = VIRTIO_VSOCK_OP_REQUEST, 1030 .vsk = vsk, 1031 }; 1032 1033 return virtio_transport_send_pkt_info(vsk, &info); 1034 } 1035 EXPORT_SYMBOL_GPL(virtio_transport_connect); 1036 1037 int virtio_transport_shutdown(struct vsock_sock *vsk, int mode) 1038 { 1039 struct virtio_vsock_pkt_info info = { 1040 .op = VIRTIO_VSOCK_OP_SHUTDOWN, 1041 .flags = (mode & RCV_SHUTDOWN ? 1042 VIRTIO_VSOCK_SHUTDOWN_RCV : 0) | 1043 (mode & SEND_SHUTDOWN ? 1044 VIRTIO_VSOCK_SHUTDOWN_SEND : 0), 1045 .vsk = vsk, 1046 }; 1047 1048 return virtio_transport_send_pkt_info(vsk, &info); 1049 } 1050 EXPORT_SYMBOL_GPL(virtio_transport_shutdown); 1051 1052 int 1053 virtio_transport_dgram_enqueue(struct vsock_sock *vsk, 1054 struct sockaddr_vm *remote_addr, 1055 struct msghdr *msg, 1056 size_t dgram_len) 1057 { 1058 return -EOPNOTSUPP; 1059 } 1060 EXPORT_SYMBOL_GPL(virtio_transport_dgram_enqueue); 1061 1062 ssize_t 1063 virtio_transport_stream_enqueue(struct vsock_sock *vsk, 1064 struct msghdr *msg, 1065 size_t len) 1066 { 1067 struct virtio_vsock_pkt_info info = { 1068 .op = VIRTIO_VSOCK_OP_RW, 1069 .msg = msg, 1070 .pkt_len = len, 1071 .vsk = vsk, 1072 }; 1073 1074 return virtio_transport_send_pkt_info(vsk, &info); 1075 } 1076 EXPORT_SYMBOL_GPL(virtio_transport_stream_enqueue); 1077 1078 void virtio_transport_destruct(struct vsock_sock *vsk) 1079 { 1080 struct virtio_vsock_sock *vvs = vsk->trans; 1081 1082 kfree(vvs); 1083 } 1084 EXPORT_SYMBOL_GPL(virtio_transport_destruct); 1085 1086 static int virtio_transport_reset(struct vsock_sock *vsk, 1087 struct sk_buff *skb) 1088 { 1089 struct virtio_vsock_pkt_info info = { 1090 .op = VIRTIO_VSOCK_OP_RST, 1091 .reply = !!skb, 1092 .vsk = vsk, 1093 }; 1094 1095 /* Send RST only if the original pkt is not a RST pkt */ 1096 if (skb && le16_to_cpu(virtio_vsock_hdr(skb)->op) == VIRTIO_VSOCK_OP_RST) 1097 return 0; 1098 1099 return virtio_transport_send_pkt_info(vsk, &info); 1100 } 1101 1102 /* Normally packets are associated with a socket. There may be no socket if an 1103 * attempt was made to connect to a socket that does not exist. 1104 */ 1105 static int virtio_transport_reset_no_sock(const struct virtio_transport *t, 1106 struct sk_buff *skb) 1107 { 1108 struct virtio_vsock_hdr *hdr = virtio_vsock_hdr(skb); 1109 struct virtio_vsock_pkt_info info = { 1110 .op = VIRTIO_VSOCK_OP_RST, 1111 .type = le16_to_cpu(hdr->type), 1112 .reply = true, 1113 }; 1114 struct sk_buff *reply; 1115 1116 /* Send RST only if the original pkt is not a RST pkt */ 1117 if (le16_to_cpu(hdr->op) == VIRTIO_VSOCK_OP_RST) 1118 return 0; 1119 1120 if (!t) 1121 return -ENOTCONN; 1122 1123 reply = virtio_transport_alloc_skb(&info, 0, false, 1124 le64_to_cpu(hdr->dst_cid), 1125 le32_to_cpu(hdr->dst_port), 1126 le64_to_cpu(hdr->src_cid), 1127 le32_to_cpu(hdr->src_port)); 1128 if (!reply) 1129 return -ENOMEM; 1130 1131 return t->send_pkt(reply); 1132 } 1133 1134 /* This function should be called with sk_lock held and SOCK_DONE set */ 1135 static void virtio_transport_remove_sock(struct vsock_sock *vsk) 1136 { 1137 struct virtio_vsock_sock *vvs = vsk->trans; 1138 1139 /* We don't need to take rx_lock, as the socket is closing and we are 1140 * removing it. 1141 */ 1142 __skb_queue_purge(&vvs->rx_queue); 1143 vsock_remove_sock(vsk); 1144 } 1145 1146 static void virtio_transport_wait_close(struct sock *sk, long timeout) 1147 { 1148 if (timeout) { 1149 DEFINE_WAIT_FUNC(wait, woken_wake_function); 1150 1151 add_wait_queue(sk_sleep(sk), &wait); 1152 1153 do { 1154 if (sk_wait_event(sk, &timeout, 1155 sock_flag(sk, SOCK_DONE), &wait)) 1156 break; 1157 } while (!signal_pending(current) && timeout); 1158 1159 remove_wait_queue(sk_sleep(sk), &wait); 1160 } 1161 } 1162 1163 static void virtio_transport_do_close(struct vsock_sock *vsk, 1164 bool cancel_timeout) 1165 { 1166 struct sock *sk = sk_vsock(vsk); 1167 1168 sock_set_flag(sk, SOCK_DONE); 1169 vsk->peer_shutdown = SHUTDOWN_MASK; 1170 if (vsock_stream_has_data(vsk) <= 0) 1171 sk->sk_state = TCP_CLOSING; 1172 sk->sk_state_change(sk); 1173 1174 if (vsk->close_work_scheduled && 1175 (!cancel_timeout || cancel_delayed_work(&vsk->close_work))) { 1176 vsk->close_work_scheduled = false; 1177 1178 virtio_transport_remove_sock(vsk); 1179 1180 /* Release refcnt obtained when we scheduled the timeout */ 1181 sock_put(sk); 1182 } 1183 } 1184 1185 static void virtio_transport_close_timeout(struct work_struct *work) 1186 { 1187 struct vsock_sock *vsk = 1188 container_of(work, struct vsock_sock, close_work.work); 1189 struct sock *sk = sk_vsock(vsk); 1190 1191 sock_hold(sk); 1192 lock_sock(sk); 1193 1194 if (!sock_flag(sk, SOCK_DONE)) { 1195 (void)virtio_transport_reset(vsk, NULL); 1196 1197 virtio_transport_do_close(vsk, false); 1198 } 1199 1200 vsk->close_work_scheduled = false; 1201 1202 release_sock(sk); 1203 sock_put(sk); 1204 } 1205 1206 /* User context, vsk->sk is locked */ 1207 static bool virtio_transport_close(struct vsock_sock *vsk) 1208 { 1209 struct sock *sk = &vsk->sk; 1210 1211 if (!(sk->sk_state == TCP_ESTABLISHED || 1212 sk->sk_state == TCP_CLOSING)) 1213 return true; 1214 1215 /* Already received SHUTDOWN from peer, reply with RST */ 1216 if ((vsk->peer_shutdown & SHUTDOWN_MASK) == SHUTDOWN_MASK) { 1217 (void)virtio_transport_reset(vsk, NULL); 1218 return true; 1219 } 1220 1221 if ((sk->sk_shutdown & SHUTDOWN_MASK) != SHUTDOWN_MASK) 1222 (void)virtio_transport_shutdown(vsk, SHUTDOWN_MASK); 1223 1224 if (sock_flag(sk, SOCK_LINGER) && !(current->flags & PF_EXITING)) 1225 virtio_transport_wait_close(sk, sk->sk_lingertime); 1226 1227 if (sock_flag(sk, SOCK_DONE)) { 1228 return true; 1229 } 1230 1231 sock_hold(sk); 1232 INIT_DELAYED_WORK(&vsk->close_work, 1233 virtio_transport_close_timeout); 1234 vsk->close_work_scheduled = true; 1235 schedule_delayed_work(&vsk->close_work, VSOCK_CLOSE_TIMEOUT); 1236 return false; 1237 } 1238 1239 void virtio_transport_release(struct vsock_sock *vsk) 1240 { 1241 struct sock *sk = &vsk->sk; 1242 bool remove_sock = true; 1243 1244 if (sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET) 1245 remove_sock = virtio_transport_close(vsk); 1246 1247 if (remove_sock) { 1248 sock_set_flag(sk, SOCK_DONE); 1249 virtio_transport_remove_sock(vsk); 1250 } 1251 } 1252 EXPORT_SYMBOL_GPL(virtio_transport_release); 1253 1254 static int 1255 virtio_transport_recv_connecting(struct sock *sk, 1256 struct sk_buff *skb) 1257 { 1258 struct virtio_vsock_hdr *hdr = virtio_vsock_hdr(skb); 1259 struct vsock_sock *vsk = vsock_sk(sk); 1260 int skerr; 1261 int err; 1262 1263 switch (le16_to_cpu(hdr->op)) { 1264 case VIRTIO_VSOCK_OP_RESPONSE: 1265 sk->sk_state = TCP_ESTABLISHED; 1266 sk->sk_socket->state = SS_CONNECTED; 1267 vsock_insert_connected(vsk); 1268 sk->sk_state_change(sk); 1269 break; 1270 case VIRTIO_VSOCK_OP_INVALID: 1271 break; 1272 case VIRTIO_VSOCK_OP_RST: 1273 skerr = ECONNRESET; 1274 err = 0; 1275 goto destroy; 1276 default: 1277 skerr = EPROTO; 1278 err = -EINVAL; 1279 goto destroy; 1280 } 1281 return 0; 1282 1283 destroy: 1284 virtio_transport_reset(vsk, skb); 1285 sk->sk_state = TCP_CLOSE; 1286 sk->sk_err = skerr; 1287 sk_error_report(sk); 1288 return err; 1289 } 1290 1291 static void 1292 virtio_transport_recv_enqueue(struct vsock_sock *vsk, 1293 struct sk_buff *skb) 1294 { 1295 struct virtio_vsock_sock *vvs = vsk->trans; 1296 bool can_enqueue, free_pkt = false; 1297 struct virtio_vsock_hdr *hdr; 1298 u32 len; 1299 1300 hdr = virtio_vsock_hdr(skb); 1301 len = le32_to_cpu(hdr->len); 1302 1303 spin_lock_bh(&vvs->rx_lock); 1304 1305 can_enqueue = virtio_transport_inc_rx_pkt(vvs, len); 1306 if (!can_enqueue) { 1307 free_pkt = true; 1308 goto out; 1309 } 1310 1311 if (le32_to_cpu(hdr->flags) & VIRTIO_VSOCK_SEQ_EOM) 1312 vvs->msg_count++; 1313 1314 /* Try to copy small packets into the buffer of last packet queued, 1315 * to avoid wasting memory queueing the entire buffer with a small 1316 * payload. 1317 */ 1318 if (len <= GOOD_COPY_LEN && !skb_queue_empty(&vvs->rx_queue)) { 1319 struct virtio_vsock_hdr *last_hdr; 1320 struct sk_buff *last_skb; 1321 1322 last_skb = skb_peek_tail(&vvs->rx_queue); 1323 last_hdr = virtio_vsock_hdr(last_skb); 1324 1325 /* If there is space in the last packet queued, we copy the 1326 * new packet in its buffer. We avoid this if the last packet 1327 * queued has VIRTIO_VSOCK_SEQ_EOM set, because this is 1328 * delimiter of SEQPACKET message, so 'pkt' is the first packet 1329 * of a new message. 1330 */ 1331 if (skb->len < skb_tailroom(last_skb) && 1332 !(le32_to_cpu(last_hdr->flags) & VIRTIO_VSOCK_SEQ_EOM)) { 1333 memcpy(skb_put(last_skb, skb->len), skb->data, skb->len); 1334 free_pkt = true; 1335 last_hdr->flags |= hdr->flags; 1336 le32_add_cpu(&last_hdr->len, len); 1337 goto out; 1338 } 1339 } 1340 1341 __skb_queue_tail(&vvs->rx_queue, skb); 1342 1343 out: 1344 spin_unlock_bh(&vvs->rx_lock); 1345 if (free_pkt) 1346 kfree_skb(skb); 1347 } 1348 1349 static int 1350 virtio_transport_recv_connected(struct sock *sk, 1351 struct sk_buff *skb) 1352 { 1353 struct virtio_vsock_hdr *hdr = virtio_vsock_hdr(skb); 1354 struct vsock_sock *vsk = vsock_sk(sk); 1355 int err = 0; 1356 1357 switch (le16_to_cpu(hdr->op)) { 1358 case VIRTIO_VSOCK_OP_RW: 1359 virtio_transport_recv_enqueue(vsk, skb); 1360 vsock_data_ready(sk); 1361 return err; 1362 case VIRTIO_VSOCK_OP_CREDIT_REQUEST: 1363 virtio_transport_send_credit_update(vsk); 1364 break; 1365 case VIRTIO_VSOCK_OP_CREDIT_UPDATE: 1366 sk->sk_write_space(sk); 1367 break; 1368 case VIRTIO_VSOCK_OP_SHUTDOWN: 1369 if (le32_to_cpu(hdr->flags) & VIRTIO_VSOCK_SHUTDOWN_RCV) 1370 vsk->peer_shutdown |= RCV_SHUTDOWN; 1371 if (le32_to_cpu(hdr->flags) & VIRTIO_VSOCK_SHUTDOWN_SEND) 1372 vsk->peer_shutdown |= SEND_SHUTDOWN; 1373 if (vsk->peer_shutdown == SHUTDOWN_MASK) { 1374 if (vsock_stream_has_data(vsk) <= 0 && !sock_flag(sk, SOCK_DONE)) { 1375 (void)virtio_transport_reset(vsk, NULL); 1376 virtio_transport_do_close(vsk, true); 1377 } 1378 /* Remove this socket anyway because the remote peer sent 1379 * the shutdown. This way a new connection will succeed 1380 * if the remote peer uses the same source port, 1381 * even if the old socket is still unreleased, but now disconnected. 1382 */ 1383 vsock_remove_sock(vsk); 1384 } 1385 if (le32_to_cpu(virtio_vsock_hdr(skb)->flags)) 1386 sk->sk_state_change(sk); 1387 break; 1388 case VIRTIO_VSOCK_OP_RST: 1389 virtio_transport_do_close(vsk, true); 1390 break; 1391 default: 1392 err = -EINVAL; 1393 break; 1394 } 1395 1396 kfree_skb(skb); 1397 return err; 1398 } 1399 1400 static void 1401 virtio_transport_recv_disconnecting(struct sock *sk, 1402 struct sk_buff *skb) 1403 { 1404 struct virtio_vsock_hdr *hdr = virtio_vsock_hdr(skb); 1405 struct vsock_sock *vsk = vsock_sk(sk); 1406 1407 if (le16_to_cpu(hdr->op) == VIRTIO_VSOCK_OP_RST) 1408 virtio_transport_do_close(vsk, true); 1409 } 1410 1411 static int 1412 virtio_transport_send_response(struct vsock_sock *vsk, 1413 struct sk_buff *skb) 1414 { 1415 struct virtio_vsock_hdr *hdr = virtio_vsock_hdr(skb); 1416 struct virtio_vsock_pkt_info info = { 1417 .op = VIRTIO_VSOCK_OP_RESPONSE, 1418 .remote_cid = le64_to_cpu(hdr->src_cid), 1419 .remote_port = le32_to_cpu(hdr->src_port), 1420 .reply = true, 1421 .vsk = vsk, 1422 }; 1423 1424 return virtio_transport_send_pkt_info(vsk, &info); 1425 } 1426 1427 static bool virtio_transport_space_update(struct sock *sk, 1428 struct sk_buff *skb) 1429 { 1430 struct virtio_vsock_hdr *hdr = virtio_vsock_hdr(skb); 1431 struct vsock_sock *vsk = vsock_sk(sk); 1432 struct virtio_vsock_sock *vvs = vsk->trans; 1433 bool space_available; 1434 1435 /* Listener sockets are not associated with any transport, so we are 1436 * not able to take the state to see if there is space available in the 1437 * remote peer, but since they are only used to receive requests, we 1438 * can assume that there is always space available in the other peer. 1439 */ 1440 if (!vvs) 1441 return true; 1442 1443 /* buf_alloc and fwd_cnt is always included in the hdr */ 1444 spin_lock_bh(&vvs->tx_lock); 1445 vvs->peer_buf_alloc = le32_to_cpu(hdr->buf_alloc); 1446 vvs->peer_fwd_cnt = le32_to_cpu(hdr->fwd_cnt); 1447 space_available = virtio_transport_has_space(vsk); 1448 spin_unlock_bh(&vvs->tx_lock); 1449 return space_available; 1450 } 1451 1452 /* Handle server socket */ 1453 static int 1454 virtio_transport_recv_listen(struct sock *sk, struct sk_buff *skb, 1455 struct virtio_transport *t) 1456 { 1457 struct virtio_vsock_hdr *hdr = virtio_vsock_hdr(skb); 1458 struct vsock_sock *vsk = vsock_sk(sk); 1459 struct vsock_sock *vchild; 1460 struct sock *child; 1461 int ret; 1462 1463 if (le16_to_cpu(hdr->op) != VIRTIO_VSOCK_OP_REQUEST) { 1464 virtio_transport_reset_no_sock(t, skb); 1465 return -EINVAL; 1466 } 1467 1468 if (sk_acceptq_is_full(sk)) { 1469 virtio_transport_reset_no_sock(t, skb); 1470 return -ENOMEM; 1471 } 1472 1473 child = vsock_create_connected(sk); 1474 if (!child) { 1475 virtio_transport_reset_no_sock(t, skb); 1476 return -ENOMEM; 1477 } 1478 1479 sk_acceptq_added(sk); 1480 1481 lock_sock_nested(child, SINGLE_DEPTH_NESTING); 1482 1483 child->sk_state = TCP_ESTABLISHED; 1484 1485 vchild = vsock_sk(child); 1486 vsock_addr_init(&vchild->local_addr, le64_to_cpu(hdr->dst_cid), 1487 le32_to_cpu(hdr->dst_port)); 1488 vsock_addr_init(&vchild->remote_addr, le64_to_cpu(hdr->src_cid), 1489 le32_to_cpu(hdr->src_port)); 1490 1491 ret = vsock_assign_transport(vchild, vsk); 1492 /* Transport assigned (looking at remote_addr) must be the same 1493 * where we received the request. 1494 */ 1495 if (ret || vchild->transport != &t->transport) { 1496 release_sock(child); 1497 virtio_transport_reset_no_sock(t, skb); 1498 sock_put(child); 1499 return ret; 1500 } 1501 1502 if (virtio_transport_space_update(child, skb)) 1503 child->sk_write_space(child); 1504 1505 vsock_insert_connected(vchild); 1506 vsock_enqueue_accept(sk, child); 1507 virtio_transport_send_response(vchild, skb); 1508 1509 release_sock(child); 1510 1511 sk->sk_data_ready(sk); 1512 return 0; 1513 } 1514 1515 static bool virtio_transport_valid_type(u16 type) 1516 { 1517 return (type == VIRTIO_VSOCK_TYPE_STREAM) || 1518 (type == VIRTIO_VSOCK_TYPE_SEQPACKET); 1519 } 1520 1521 /* We are under the virtio-vsock's vsock->rx_lock or vhost-vsock's vq->mutex 1522 * lock. 1523 */ 1524 void virtio_transport_recv_pkt(struct virtio_transport *t, 1525 struct sk_buff *skb) 1526 { 1527 struct virtio_vsock_hdr *hdr = virtio_vsock_hdr(skb); 1528 struct sockaddr_vm src, dst; 1529 struct vsock_sock *vsk; 1530 struct sock *sk; 1531 bool space_available; 1532 1533 vsock_addr_init(&src, le64_to_cpu(hdr->src_cid), 1534 le32_to_cpu(hdr->src_port)); 1535 vsock_addr_init(&dst, le64_to_cpu(hdr->dst_cid), 1536 le32_to_cpu(hdr->dst_port)); 1537 1538 trace_virtio_transport_recv_pkt(src.svm_cid, src.svm_port, 1539 dst.svm_cid, dst.svm_port, 1540 le32_to_cpu(hdr->len), 1541 le16_to_cpu(hdr->type), 1542 le16_to_cpu(hdr->op), 1543 le32_to_cpu(hdr->flags), 1544 le32_to_cpu(hdr->buf_alloc), 1545 le32_to_cpu(hdr->fwd_cnt)); 1546 1547 if (!virtio_transport_valid_type(le16_to_cpu(hdr->type))) { 1548 (void)virtio_transport_reset_no_sock(t, skb); 1549 goto free_pkt; 1550 } 1551 1552 /* The socket must be in connected or bound table 1553 * otherwise send reset back 1554 */ 1555 sk = vsock_find_connected_socket(&src, &dst); 1556 if (!sk) { 1557 sk = vsock_find_bound_socket(&dst); 1558 if (!sk) { 1559 (void)virtio_transport_reset_no_sock(t, skb); 1560 goto free_pkt; 1561 } 1562 } 1563 1564 if (virtio_transport_get_type(sk) != le16_to_cpu(hdr->type)) { 1565 (void)virtio_transport_reset_no_sock(t, skb); 1566 sock_put(sk); 1567 goto free_pkt; 1568 } 1569 1570 if (!skb_set_owner_sk_safe(skb, sk)) { 1571 WARN_ONCE(1, "receiving vsock socket has sk_refcnt == 0\n"); 1572 goto free_pkt; 1573 } 1574 1575 vsk = vsock_sk(sk); 1576 1577 lock_sock(sk); 1578 1579 /* Check if sk has been closed before lock_sock */ 1580 if (sock_flag(sk, SOCK_DONE)) { 1581 (void)virtio_transport_reset_no_sock(t, skb); 1582 release_sock(sk); 1583 sock_put(sk); 1584 goto free_pkt; 1585 } 1586 1587 space_available = virtio_transport_space_update(sk, skb); 1588 1589 /* Update CID in case it has changed after a transport reset event */ 1590 if (vsk->local_addr.svm_cid != VMADDR_CID_ANY) 1591 vsk->local_addr.svm_cid = dst.svm_cid; 1592 1593 if (space_available) 1594 sk->sk_write_space(sk); 1595 1596 switch (sk->sk_state) { 1597 case TCP_LISTEN: 1598 virtio_transport_recv_listen(sk, skb, t); 1599 kfree_skb(skb); 1600 break; 1601 case TCP_SYN_SENT: 1602 virtio_transport_recv_connecting(sk, skb); 1603 kfree_skb(skb); 1604 break; 1605 case TCP_ESTABLISHED: 1606 virtio_transport_recv_connected(sk, skb); 1607 break; 1608 case TCP_CLOSING: 1609 virtio_transport_recv_disconnecting(sk, skb); 1610 kfree_skb(skb); 1611 break; 1612 default: 1613 (void)virtio_transport_reset_no_sock(t, skb); 1614 kfree_skb(skb); 1615 break; 1616 } 1617 1618 release_sock(sk); 1619 1620 /* Release refcnt obtained when we fetched this socket out of the 1621 * bound or connected list. 1622 */ 1623 sock_put(sk); 1624 return; 1625 1626 free_pkt: 1627 kfree_skb(skb); 1628 } 1629 EXPORT_SYMBOL_GPL(virtio_transport_recv_pkt); 1630 1631 /* Remove skbs found in a queue that have a vsk that matches. 1632 * 1633 * Each skb is freed. 1634 * 1635 * Returns the count of skbs that were reply packets. 1636 */ 1637 int virtio_transport_purge_skbs(void *vsk, struct sk_buff_head *queue) 1638 { 1639 struct sk_buff_head freeme; 1640 struct sk_buff *skb, *tmp; 1641 int cnt = 0; 1642 1643 skb_queue_head_init(&freeme); 1644 1645 spin_lock_bh(&queue->lock); 1646 skb_queue_walk_safe(queue, skb, tmp) { 1647 if (vsock_sk(skb->sk) != vsk) 1648 continue; 1649 1650 __skb_unlink(skb, queue); 1651 __skb_queue_tail(&freeme, skb); 1652 1653 if (virtio_vsock_skb_reply(skb)) 1654 cnt++; 1655 } 1656 spin_unlock_bh(&queue->lock); 1657 1658 __skb_queue_purge(&freeme); 1659 1660 return cnt; 1661 } 1662 EXPORT_SYMBOL_GPL(virtio_transport_purge_skbs); 1663 1664 int virtio_transport_read_skb(struct vsock_sock *vsk, skb_read_actor_t recv_actor) 1665 { 1666 struct virtio_vsock_sock *vvs = vsk->trans; 1667 struct sock *sk = sk_vsock(vsk); 1668 struct sk_buff *skb; 1669 int off = 0; 1670 int err; 1671 1672 spin_lock_bh(&vvs->rx_lock); 1673 /* Use __skb_recv_datagram() for race-free handling of the receive. It 1674 * works for types other than dgrams. 1675 */ 1676 skb = __skb_recv_datagram(sk, &vvs->rx_queue, MSG_DONTWAIT, &off, &err); 1677 spin_unlock_bh(&vvs->rx_lock); 1678 1679 if (!skb) 1680 return err; 1681 1682 return recv_actor(sk, skb); 1683 } 1684 EXPORT_SYMBOL_GPL(virtio_transport_read_skb); 1685 1686 MODULE_LICENSE("GPL v2"); 1687 MODULE_AUTHOR("Asias He"); 1688 MODULE_DESCRIPTION("common code for virtio vsock"); 1689