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 /* Returns a new packet on success, otherwise returns NULL. 41 * 42 * If NULL is returned, errp is set to a negative errno. 43 */ 44 static struct sk_buff * 45 virtio_transport_alloc_skb(struct virtio_vsock_pkt_info *info, 46 size_t len, 47 u32 src_cid, 48 u32 src_port, 49 u32 dst_cid, 50 u32 dst_port) 51 { 52 const size_t skb_len = VIRTIO_VSOCK_SKB_HEADROOM + len; 53 struct virtio_vsock_hdr *hdr; 54 struct sk_buff *skb; 55 void *payload; 56 int err; 57 58 skb = virtio_vsock_alloc_skb(skb_len, GFP_KERNEL); 59 if (!skb) 60 return NULL; 61 62 hdr = virtio_vsock_hdr(skb); 63 hdr->type = cpu_to_le16(info->type); 64 hdr->op = cpu_to_le16(info->op); 65 hdr->src_cid = cpu_to_le64(src_cid); 66 hdr->dst_cid = cpu_to_le64(dst_cid); 67 hdr->src_port = cpu_to_le32(src_port); 68 hdr->dst_port = cpu_to_le32(dst_port); 69 hdr->flags = cpu_to_le32(info->flags); 70 hdr->len = cpu_to_le32(len); 71 72 if (info->msg && len > 0) { 73 payload = skb_put(skb, len); 74 err = memcpy_from_msg(payload, info->msg, len); 75 if (err) 76 goto out; 77 78 if (msg_data_left(info->msg) == 0 && 79 info->type == VIRTIO_VSOCK_TYPE_SEQPACKET) { 80 hdr->flags |= cpu_to_le32(VIRTIO_VSOCK_SEQ_EOM); 81 82 if (info->msg->msg_flags & MSG_EOR) 83 hdr->flags |= cpu_to_le32(VIRTIO_VSOCK_SEQ_EOR); 84 } 85 } 86 87 if (info->reply) 88 virtio_vsock_skb_set_reply(skb); 89 90 trace_virtio_transport_alloc_pkt(src_cid, src_port, 91 dst_cid, dst_port, 92 len, 93 info->type, 94 info->op, 95 info->flags); 96 97 return skb; 98 99 out: 100 kfree_skb(skb); 101 return NULL; 102 } 103 104 /* Packet capture */ 105 static struct sk_buff *virtio_transport_build_skb(void *opaque) 106 { 107 struct virtio_vsock_hdr *pkt_hdr; 108 struct sk_buff *pkt = opaque; 109 struct af_vsockmon_hdr *hdr; 110 struct sk_buff *skb; 111 size_t payload_len; 112 void *payload_buf; 113 114 /* A packet could be split to fit the RX buffer, so we can retrieve 115 * the payload length from the header and the buffer pointer taking 116 * care of the offset in the original packet. 117 */ 118 pkt_hdr = virtio_vsock_hdr(pkt); 119 payload_len = pkt->len; 120 payload_buf = pkt->data; 121 122 skb = alloc_skb(sizeof(*hdr) + sizeof(*pkt_hdr) + payload_len, 123 GFP_ATOMIC); 124 if (!skb) 125 return NULL; 126 127 hdr = skb_put(skb, sizeof(*hdr)); 128 129 /* pkt->hdr is little-endian so no need to byteswap here */ 130 hdr->src_cid = pkt_hdr->src_cid; 131 hdr->src_port = pkt_hdr->src_port; 132 hdr->dst_cid = pkt_hdr->dst_cid; 133 hdr->dst_port = pkt_hdr->dst_port; 134 135 hdr->transport = cpu_to_le16(AF_VSOCK_TRANSPORT_VIRTIO); 136 hdr->len = cpu_to_le16(sizeof(*pkt_hdr)); 137 memset(hdr->reserved, 0, sizeof(hdr->reserved)); 138 139 switch (le16_to_cpu(pkt_hdr->op)) { 140 case VIRTIO_VSOCK_OP_REQUEST: 141 case VIRTIO_VSOCK_OP_RESPONSE: 142 hdr->op = cpu_to_le16(AF_VSOCK_OP_CONNECT); 143 break; 144 case VIRTIO_VSOCK_OP_RST: 145 case VIRTIO_VSOCK_OP_SHUTDOWN: 146 hdr->op = cpu_to_le16(AF_VSOCK_OP_DISCONNECT); 147 break; 148 case VIRTIO_VSOCK_OP_RW: 149 hdr->op = cpu_to_le16(AF_VSOCK_OP_PAYLOAD); 150 break; 151 case VIRTIO_VSOCK_OP_CREDIT_UPDATE: 152 case VIRTIO_VSOCK_OP_CREDIT_REQUEST: 153 hdr->op = cpu_to_le16(AF_VSOCK_OP_CONTROL); 154 break; 155 default: 156 hdr->op = cpu_to_le16(AF_VSOCK_OP_UNKNOWN); 157 break; 158 } 159 160 skb_put_data(skb, pkt_hdr, sizeof(*pkt_hdr)); 161 162 if (payload_len) { 163 skb_put_data(skb, payload_buf, payload_len); 164 } 165 166 return skb; 167 } 168 169 void virtio_transport_deliver_tap_pkt(struct sk_buff *skb) 170 { 171 if (virtio_vsock_skb_tap_delivered(skb)) 172 return; 173 174 vsock_deliver_tap(virtio_transport_build_skb, skb); 175 virtio_vsock_skb_set_tap_delivered(skb); 176 } 177 EXPORT_SYMBOL_GPL(virtio_transport_deliver_tap_pkt); 178 179 static u16 virtio_transport_get_type(struct sock *sk) 180 { 181 if (sk->sk_type == SOCK_STREAM) 182 return VIRTIO_VSOCK_TYPE_STREAM; 183 else 184 return VIRTIO_VSOCK_TYPE_SEQPACKET; 185 } 186 187 /* This function can only be used on connecting/connected sockets, 188 * since a socket assigned to a transport is required. 189 * 190 * Do not use on listener sockets! 191 */ 192 static int virtio_transport_send_pkt_info(struct vsock_sock *vsk, 193 struct virtio_vsock_pkt_info *info) 194 { 195 u32 src_cid, src_port, dst_cid, dst_port; 196 const struct virtio_transport *t_ops; 197 struct virtio_vsock_sock *vvs; 198 u32 pkt_len = info->pkt_len; 199 struct sk_buff *skb; 200 201 info->type = virtio_transport_get_type(sk_vsock(vsk)); 202 203 t_ops = virtio_transport_get_ops(vsk); 204 if (unlikely(!t_ops)) 205 return -EFAULT; 206 207 src_cid = t_ops->transport.get_local_cid(); 208 src_port = vsk->local_addr.svm_port; 209 if (!info->remote_cid) { 210 dst_cid = vsk->remote_addr.svm_cid; 211 dst_port = vsk->remote_addr.svm_port; 212 } else { 213 dst_cid = info->remote_cid; 214 dst_port = info->remote_port; 215 } 216 217 vvs = vsk->trans; 218 219 /* we can send less than pkt_len bytes */ 220 if (pkt_len > VIRTIO_VSOCK_MAX_PKT_BUF_SIZE) 221 pkt_len = VIRTIO_VSOCK_MAX_PKT_BUF_SIZE; 222 223 /* virtio_transport_get_credit might return less than pkt_len credit */ 224 pkt_len = virtio_transport_get_credit(vvs, pkt_len); 225 226 /* Do not send zero length OP_RW pkt */ 227 if (pkt_len == 0 && info->op == VIRTIO_VSOCK_OP_RW) 228 return pkt_len; 229 230 skb = virtio_transport_alloc_skb(info, pkt_len, 231 src_cid, src_port, 232 dst_cid, dst_port); 233 if (!skb) { 234 virtio_transport_put_credit(vvs, pkt_len); 235 return -ENOMEM; 236 } 237 238 virtio_transport_inc_tx_pkt(vvs, skb); 239 240 return t_ops->send_pkt(skb); 241 } 242 243 static bool virtio_transport_inc_rx_pkt(struct virtio_vsock_sock *vvs, 244 u32 len) 245 { 246 if (vvs->rx_bytes + len > vvs->buf_alloc) 247 return false; 248 249 vvs->rx_bytes += len; 250 return true; 251 } 252 253 static void virtio_transport_dec_rx_pkt(struct virtio_vsock_sock *vvs, 254 u32 len) 255 { 256 vvs->rx_bytes -= len; 257 vvs->fwd_cnt += len; 258 } 259 260 void virtio_transport_inc_tx_pkt(struct virtio_vsock_sock *vvs, struct sk_buff *skb) 261 { 262 struct virtio_vsock_hdr *hdr = virtio_vsock_hdr(skb); 263 264 spin_lock_bh(&vvs->rx_lock); 265 vvs->last_fwd_cnt = vvs->fwd_cnt; 266 hdr->fwd_cnt = cpu_to_le32(vvs->fwd_cnt); 267 hdr->buf_alloc = cpu_to_le32(vvs->buf_alloc); 268 spin_unlock_bh(&vvs->rx_lock); 269 } 270 EXPORT_SYMBOL_GPL(virtio_transport_inc_tx_pkt); 271 272 u32 virtio_transport_get_credit(struct virtio_vsock_sock *vvs, u32 credit) 273 { 274 u32 ret; 275 276 spin_lock_bh(&vvs->tx_lock); 277 ret = vvs->peer_buf_alloc - (vvs->tx_cnt - vvs->peer_fwd_cnt); 278 if (ret > credit) 279 ret = credit; 280 vvs->tx_cnt += ret; 281 spin_unlock_bh(&vvs->tx_lock); 282 283 return ret; 284 } 285 EXPORT_SYMBOL_GPL(virtio_transport_get_credit); 286 287 void virtio_transport_put_credit(struct virtio_vsock_sock *vvs, u32 credit) 288 { 289 spin_lock_bh(&vvs->tx_lock); 290 vvs->tx_cnt -= credit; 291 spin_unlock_bh(&vvs->tx_lock); 292 } 293 EXPORT_SYMBOL_GPL(virtio_transport_put_credit); 294 295 static int virtio_transport_send_credit_update(struct vsock_sock *vsk) 296 { 297 struct virtio_vsock_pkt_info info = { 298 .op = VIRTIO_VSOCK_OP_CREDIT_UPDATE, 299 .vsk = vsk, 300 }; 301 302 return virtio_transport_send_pkt_info(vsk, &info); 303 } 304 305 static ssize_t 306 virtio_transport_stream_do_peek(struct vsock_sock *vsk, 307 struct msghdr *msg, 308 size_t len) 309 { 310 struct virtio_vsock_sock *vvs = vsk->trans; 311 size_t bytes, total = 0, off; 312 struct sk_buff *skb, *tmp; 313 int err = -EFAULT; 314 315 spin_lock_bh(&vvs->rx_lock); 316 317 skb_queue_walk_safe(&vvs->rx_queue, skb, tmp) { 318 off = 0; 319 320 if (total == len) 321 break; 322 323 while (total < len && off < skb->len) { 324 bytes = len - total; 325 if (bytes > skb->len - off) 326 bytes = skb->len - off; 327 328 /* sk_lock is held by caller so no one else can dequeue. 329 * Unlock rx_lock since memcpy_to_msg() may sleep. 330 */ 331 spin_unlock_bh(&vvs->rx_lock); 332 333 err = memcpy_to_msg(msg, skb->data + off, bytes); 334 if (err) 335 goto out; 336 337 spin_lock_bh(&vvs->rx_lock); 338 339 total += bytes; 340 off += bytes; 341 } 342 } 343 344 spin_unlock_bh(&vvs->rx_lock); 345 346 return total; 347 348 out: 349 if (total) 350 err = total; 351 return err; 352 } 353 354 static ssize_t 355 virtio_transport_stream_do_dequeue(struct vsock_sock *vsk, 356 struct msghdr *msg, 357 size_t len) 358 { 359 struct virtio_vsock_sock *vvs = vsk->trans; 360 size_t bytes, total = 0; 361 struct sk_buff *skb; 362 int err = -EFAULT; 363 u32 free_space; 364 365 spin_lock_bh(&vvs->rx_lock); 366 367 if (WARN_ONCE(skb_queue_empty(&vvs->rx_queue) && vvs->rx_bytes, 368 "rx_queue is empty, but rx_bytes is non-zero\n")) { 369 spin_unlock_bh(&vvs->rx_lock); 370 return err; 371 } 372 373 while (total < len && !skb_queue_empty(&vvs->rx_queue)) { 374 skb = skb_peek(&vvs->rx_queue); 375 376 bytes = len - total; 377 if (bytes > skb->len) 378 bytes = skb->len; 379 380 /* sk_lock is held by caller so no one else can dequeue. 381 * Unlock rx_lock since memcpy_to_msg() may sleep. 382 */ 383 spin_unlock_bh(&vvs->rx_lock); 384 385 err = memcpy_to_msg(msg, skb->data, bytes); 386 if (err) 387 goto out; 388 389 spin_lock_bh(&vvs->rx_lock); 390 391 total += bytes; 392 skb_pull(skb, bytes); 393 394 if (skb->len == 0) { 395 u32 pkt_len = le32_to_cpu(virtio_vsock_hdr(skb)->len); 396 397 virtio_transport_dec_rx_pkt(vvs, pkt_len); 398 __skb_unlink(skb, &vvs->rx_queue); 399 consume_skb(skb); 400 } 401 } 402 403 free_space = vvs->buf_alloc - (vvs->fwd_cnt - vvs->last_fwd_cnt); 404 405 spin_unlock_bh(&vvs->rx_lock); 406 407 /* To reduce the number of credit update messages, 408 * don't update credits as long as lots of space is available. 409 * Note: the limit chosen here is arbitrary. Setting the limit 410 * too high causes extra messages. Too low causes transmitter 411 * stalls. As stalls are in theory more expensive than extra 412 * messages, we set the limit to a high value. TODO: experiment 413 * with different values. 414 */ 415 if (free_space < VIRTIO_VSOCK_MAX_PKT_BUF_SIZE) 416 virtio_transport_send_credit_update(vsk); 417 418 return total; 419 420 out: 421 if (total) 422 err = total; 423 return err; 424 } 425 426 static int virtio_transport_seqpacket_do_dequeue(struct vsock_sock *vsk, 427 struct msghdr *msg, 428 int flags) 429 { 430 struct virtio_vsock_sock *vvs = vsk->trans; 431 int dequeued_len = 0; 432 size_t user_buf_len = msg_data_left(msg); 433 bool msg_ready = false; 434 struct sk_buff *skb; 435 436 spin_lock_bh(&vvs->rx_lock); 437 438 if (vvs->msg_count == 0) { 439 spin_unlock_bh(&vvs->rx_lock); 440 return 0; 441 } 442 443 while (!msg_ready) { 444 struct virtio_vsock_hdr *hdr; 445 size_t pkt_len; 446 447 skb = __skb_dequeue(&vvs->rx_queue); 448 if (!skb) 449 break; 450 hdr = virtio_vsock_hdr(skb); 451 pkt_len = (size_t)le32_to_cpu(hdr->len); 452 453 if (dequeued_len >= 0) { 454 size_t bytes_to_copy; 455 456 bytes_to_copy = min(user_buf_len, pkt_len); 457 458 if (bytes_to_copy) { 459 int err; 460 461 /* sk_lock is held by caller so no one else can dequeue. 462 * Unlock rx_lock since memcpy_to_msg() may sleep. 463 */ 464 spin_unlock_bh(&vvs->rx_lock); 465 466 err = memcpy_to_msg(msg, skb->data, bytes_to_copy); 467 if (err) { 468 /* Copy of message failed. Rest of 469 * fragments will be freed without copy. 470 */ 471 dequeued_len = err; 472 } else { 473 user_buf_len -= bytes_to_copy; 474 } 475 476 spin_lock_bh(&vvs->rx_lock); 477 } 478 479 if (dequeued_len >= 0) 480 dequeued_len += pkt_len; 481 } 482 483 if (le32_to_cpu(hdr->flags) & VIRTIO_VSOCK_SEQ_EOM) { 484 msg_ready = true; 485 vvs->msg_count--; 486 487 if (le32_to_cpu(hdr->flags) & VIRTIO_VSOCK_SEQ_EOR) 488 msg->msg_flags |= MSG_EOR; 489 } 490 491 virtio_transport_dec_rx_pkt(vvs, pkt_len); 492 kfree_skb(skb); 493 } 494 495 spin_unlock_bh(&vvs->rx_lock); 496 497 virtio_transport_send_credit_update(vsk); 498 499 return dequeued_len; 500 } 501 502 ssize_t 503 virtio_transport_stream_dequeue(struct vsock_sock *vsk, 504 struct msghdr *msg, 505 size_t len, int flags) 506 { 507 if (flags & MSG_PEEK) 508 return virtio_transport_stream_do_peek(vsk, msg, len); 509 else 510 return virtio_transport_stream_do_dequeue(vsk, msg, len); 511 } 512 EXPORT_SYMBOL_GPL(virtio_transport_stream_dequeue); 513 514 ssize_t 515 virtio_transport_seqpacket_dequeue(struct vsock_sock *vsk, 516 struct msghdr *msg, 517 int flags) 518 { 519 if (flags & MSG_PEEK) 520 return -EOPNOTSUPP; 521 522 return virtio_transport_seqpacket_do_dequeue(vsk, msg, flags); 523 } 524 EXPORT_SYMBOL_GPL(virtio_transport_seqpacket_dequeue); 525 526 int 527 virtio_transport_seqpacket_enqueue(struct vsock_sock *vsk, 528 struct msghdr *msg, 529 size_t len) 530 { 531 struct virtio_vsock_sock *vvs = vsk->trans; 532 533 spin_lock_bh(&vvs->tx_lock); 534 535 if (len > vvs->peer_buf_alloc) { 536 spin_unlock_bh(&vvs->tx_lock); 537 return -EMSGSIZE; 538 } 539 540 spin_unlock_bh(&vvs->tx_lock); 541 542 return virtio_transport_stream_enqueue(vsk, msg, len); 543 } 544 EXPORT_SYMBOL_GPL(virtio_transport_seqpacket_enqueue); 545 546 int 547 virtio_transport_dgram_dequeue(struct vsock_sock *vsk, 548 struct msghdr *msg, 549 size_t len, int flags) 550 { 551 return -EOPNOTSUPP; 552 } 553 EXPORT_SYMBOL_GPL(virtio_transport_dgram_dequeue); 554 555 s64 virtio_transport_stream_has_data(struct vsock_sock *vsk) 556 { 557 struct virtio_vsock_sock *vvs = vsk->trans; 558 s64 bytes; 559 560 spin_lock_bh(&vvs->rx_lock); 561 bytes = vvs->rx_bytes; 562 spin_unlock_bh(&vvs->rx_lock); 563 564 return bytes; 565 } 566 EXPORT_SYMBOL_GPL(virtio_transport_stream_has_data); 567 568 u32 virtio_transport_seqpacket_has_data(struct vsock_sock *vsk) 569 { 570 struct virtio_vsock_sock *vvs = vsk->trans; 571 u32 msg_count; 572 573 spin_lock_bh(&vvs->rx_lock); 574 msg_count = vvs->msg_count; 575 spin_unlock_bh(&vvs->rx_lock); 576 577 return msg_count; 578 } 579 EXPORT_SYMBOL_GPL(virtio_transport_seqpacket_has_data); 580 581 static s64 virtio_transport_has_space(struct vsock_sock *vsk) 582 { 583 struct virtio_vsock_sock *vvs = vsk->trans; 584 s64 bytes; 585 586 bytes = vvs->peer_buf_alloc - (vvs->tx_cnt - vvs->peer_fwd_cnt); 587 if (bytes < 0) 588 bytes = 0; 589 590 return bytes; 591 } 592 593 s64 virtio_transport_stream_has_space(struct vsock_sock *vsk) 594 { 595 struct virtio_vsock_sock *vvs = vsk->trans; 596 s64 bytes; 597 598 spin_lock_bh(&vvs->tx_lock); 599 bytes = virtio_transport_has_space(vsk); 600 spin_unlock_bh(&vvs->tx_lock); 601 602 return bytes; 603 } 604 EXPORT_SYMBOL_GPL(virtio_transport_stream_has_space); 605 606 int virtio_transport_do_socket_init(struct vsock_sock *vsk, 607 struct vsock_sock *psk) 608 { 609 struct virtio_vsock_sock *vvs; 610 611 vvs = kzalloc(sizeof(*vvs), GFP_KERNEL); 612 if (!vvs) 613 return -ENOMEM; 614 615 vsk->trans = vvs; 616 vvs->vsk = vsk; 617 if (psk && psk->trans) { 618 struct virtio_vsock_sock *ptrans = psk->trans; 619 620 vvs->peer_buf_alloc = ptrans->peer_buf_alloc; 621 } 622 623 if (vsk->buffer_size > VIRTIO_VSOCK_MAX_BUF_SIZE) 624 vsk->buffer_size = VIRTIO_VSOCK_MAX_BUF_SIZE; 625 626 vvs->buf_alloc = vsk->buffer_size; 627 628 spin_lock_init(&vvs->rx_lock); 629 spin_lock_init(&vvs->tx_lock); 630 skb_queue_head_init(&vvs->rx_queue); 631 632 return 0; 633 } 634 EXPORT_SYMBOL_GPL(virtio_transport_do_socket_init); 635 636 /* sk_lock held by the caller */ 637 void virtio_transport_notify_buffer_size(struct vsock_sock *vsk, u64 *val) 638 { 639 struct virtio_vsock_sock *vvs = vsk->trans; 640 641 if (*val > VIRTIO_VSOCK_MAX_BUF_SIZE) 642 *val = VIRTIO_VSOCK_MAX_BUF_SIZE; 643 644 vvs->buf_alloc = *val; 645 646 virtio_transport_send_credit_update(vsk); 647 } 648 EXPORT_SYMBOL_GPL(virtio_transport_notify_buffer_size); 649 650 int 651 virtio_transport_notify_poll_in(struct vsock_sock *vsk, 652 size_t target, 653 bool *data_ready_now) 654 { 655 *data_ready_now = vsock_stream_has_data(vsk) >= target; 656 657 return 0; 658 } 659 EXPORT_SYMBOL_GPL(virtio_transport_notify_poll_in); 660 661 int 662 virtio_transport_notify_poll_out(struct vsock_sock *vsk, 663 size_t target, 664 bool *space_avail_now) 665 { 666 s64 free_space; 667 668 free_space = vsock_stream_has_space(vsk); 669 if (free_space > 0) 670 *space_avail_now = true; 671 else if (free_space == 0) 672 *space_avail_now = false; 673 674 return 0; 675 } 676 EXPORT_SYMBOL_GPL(virtio_transport_notify_poll_out); 677 678 int virtio_transport_notify_recv_init(struct vsock_sock *vsk, 679 size_t target, struct vsock_transport_recv_notify_data *data) 680 { 681 return 0; 682 } 683 EXPORT_SYMBOL_GPL(virtio_transport_notify_recv_init); 684 685 int virtio_transport_notify_recv_pre_block(struct vsock_sock *vsk, 686 size_t target, struct vsock_transport_recv_notify_data *data) 687 { 688 return 0; 689 } 690 EXPORT_SYMBOL_GPL(virtio_transport_notify_recv_pre_block); 691 692 int virtio_transport_notify_recv_pre_dequeue(struct vsock_sock *vsk, 693 size_t target, struct vsock_transport_recv_notify_data *data) 694 { 695 return 0; 696 } 697 EXPORT_SYMBOL_GPL(virtio_transport_notify_recv_pre_dequeue); 698 699 int virtio_transport_notify_recv_post_dequeue(struct vsock_sock *vsk, 700 size_t target, ssize_t copied, bool data_read, 701 struct vsock_transport_recv_notify_data *data) 702 { 703 return 0; 704 } 705 EXPORT_SYMBOL_GPL(virtio_transport_notify_recv_post_dequeue); 706 707 int virtio_transport_notify_send_init(struct vsock_sock *vsk, 708 struct vsock_transport_send_notify_data *data) 709 { 710 return 0; 711 } 712 EXPORT_SYMBOL_GPL(virtio_transport_notify_send_init); 713 714 int virtio_transport_notify_send_pre_block(struct vsock_sock *vsk, 715 struct vsock_transport_send_notify_data *data) 716 { 717 return 0; 718 } 719 EXPORT_SYMBOL_GPL(virtio_transport_notify_send_pre_block); 720 721 int virtio_transport_notify_send_pre_enqueue(struct vsock_sock *vsk, 722 struct vsock_transport_send_notify_data *data) 723 { 724 return 0; 725 } 726 EXPORT_SYMBOL_GPL(virtio_transport_notify_send_pre_enqueue); 727 728 int virtio_transport_notify_send_post_enqueue(struct vsock_sock *vsk, 729 ssize_t written, struct vsock_transport_send_notify_data *data) 730 { 731 return 0; 732 } 733 EXPORT_SYMBOL_GPL(virtio_transport_notify_send_post_enqueue); 734 735 u64 virtio_transport_stream_rcvhiwat(struct vsock_sock *vsk) 736 { 737 return vsk->buffer_size; 738 } 739 EXPORT_SYMBOL_GPL(virtio_transport_stream_rcvhiwat); 740 741 bool virtio_transport_stream_is_active(struct vsock_sock *vsk) 742 { 743 return true; 744 } 745 EXPORT_SYMBOL_GPL(virtio_transport_stream_is_active); 746 747 bool virtio_transport_stream_allow(u32 cid, u32 port) 748 { 749 return true; 750 } 751 EXPORT_SYMBOL_GPL(virtio_transport_stream_allow); 752 753 int virtio_transport_dgram_bind(struct vsock_sock *vsk, 754 struct sockaddr_vm *addr) 755 { 756 return -EOPNOTSUPP; 757 } 758 EXPORT_SYMBOL_GPL(virtio_transport_dgram_bind); 759 760 bool virtio_transport_dgram_allow(u32 cid, u32 port) 761 { 762 return false; 763 } 764 EXPORT_SYMBOL_GPL(virtio_transport_dgram_allow); 765 766 int virtio_transport_connect(struct vsock_sock *vsk) 767 { 768 struct virtio_vsock_pkt_info info = { 769 .op = VIRTIO_VSOCK_OP_REQUEST, 770 .vsk = vsk, 771 }; 772 773 return virtio_transport_send_pkt_info(vsk, &info); 774 } 775 EXPORT_SYMBOL_GPL(virtio_transport_connect); 776 777 int virtio_transport_shutdown(struct vsock_sock *vsk, int mode) 778 { 779 struct virtio_vsock_pkt_info info = { 780 .op = VIRTIO_VSOCK_OP_SHUTDOWN, 781 .flags = (mode & RCV_SHUTDOWN ? 782 VIRTIO_VSOCK_SHUTDOWN_RCV : 0) | 783 (mode & SEND_SHUTDOWN ? 784 VIRTIO_VSOCK_SHUTDOWN_SEND : 0), 785 .vsk = vsk, 786 }; 787 788 return virtio_transport_send_pkt_info(vsk, &info); 789 } 790 EXPORT_SYMBOL_GPL(virtio_transport_shutdown); 791 792 int 793 virtio_transport_dgram_enqueue(struct vsock_sock *vsk, 794 struct sockaddr_vm *remote_addr, 795 struct msghdr *msg, 796 size_t dgram_len) 797 { 798 return -EOPNOTSUPP; 799 } 800 EXPORT_SYMBOL_GPL(virtio_transport_dgram_enqueue); 801 802 ssize_t 803 virtio_transport_stream_enqueue(struct vsock_sock *vsk, 804 struct msghdr *msg, 805 size_t len) 806 { 807 struct virtio_vsock_pkt_info info = { 808 .op = VIRTIO_VSOCK_OP_RW, 809 .msg = msg, 810 .pkt_len = len, 811 .vsk = vsk, 812 }; 813 814 return virtio_transport_send_pkt_info(vsk, &info); 815 } 816 EXPORT_SYMBOL_GPL(virtio_transport_stream_enqueue); 817 818 void virtio_transport_destruct(struct vsock_sock *vsk) 819 { 820 struct virtio_vsock_sock *vvs = vsk->trans; 821 822 kfree(vvs); 823 } 824 EXPORT_SYMBOL_GPL(virtio_transport_destruct); 825 826 static int virtio_transport_reset(struct vsock_sock *vsk, 827 struct sk_buff *skb) 828 { 829 struct virtio_vsock_pkt_info info = { 830 .op = VIRTIO_VSOCK_OP_RST, 831 .reply = !!skb, 832 .vsk = vsk, 833 }; 834 835 /* Send RST only if the original pkt is not a RST pkt */ 836 if (skb && le16_to_cpu(virtio_vsock_hdr(skb)->op) == VIRTIO_VSOCK_OP_RST) 837 return 0; 838 839 return virtio_transport_send_pkt_info(vsk, &info); 840 } 841 842 /* Normally packets are associated with a socket. There may be no socket if an 843 * attempt was made to connect to a socket that does not exist. 844 */ 845 static int virtio_transport_reset_no_sock(const struct virtio_transport *t, 846 struct sk_buff *skb) 847 { 848 struct virtio_vsock_hdr *hdr = virtio_vsock_hdr(skb); 849 struct virtio_vsock_pkt_info info = { 850 .op = VIRTIO_VSOCK_OP_RST, 851 .type = le16_to_cpu(hdr->type), 852 .reply = true, 853 }; 854 struct sk_buff *reply; 855 856 /* Send RST only if the original pkt is not a RST pkt */ 857 if (le16_to_cpu(hdr->op) == VIRTIO_VSOCK_OP_RST) 858 return 0; 859 860 reply = virtio_transport_alloc_skb(&info, 0, 861 le64_to_cpu(hdr->dst_cid), 862 le32_to_cpu(hdr->dst_port), 863 le64_to_cpu(hdr->src_cid), 864 le32_to_cpu(hdr->src_port)); 865 if (!reply) 866 return -ENOMEM; 867 868 if (!t) { 869 kfree_skb(reply); 870 return -ENOTCONN; 871 } 872 873 return t->send_pkt(reply); 874 } 875 876 /* This function should be called with sk_lock held and SOCK_DONE set */ 877 static void virtio_transport_remove_sock(struct vsock_sock *vsk) 878 { 879 struct virtio_vsock_sock *vvs = vsk->trans; 880 881 /* We don't need to take rx_lock, as the socket is closing and we are 882 * removing it. 883 */ 884 __skb_queue_purge(&vvs->rx_queue); 885 vsock_remove_sock(vsk); 886 } 887 888 static void virtio_transport_wait_close(struct sock *sk, long timeout) 889 { 890 if (timeout) { 891 DEFINE_WAIT_FUNC(wait, woken_wake_function); 892 893 add_wait_queue(sk_sleep(sk), &wait); 894 895 do { 896 if (sk_wait_event(sk, &timeout, 897 sock_flag(sk, SOCK_DONE), &wait)) 898 break; 899 } while (!signal_pending(current) && timeout); 900 901 remove_wait_queue(sk_sleep(sk), &wait); 902 } 903 } 904 905 static void virtio_transport_do_close(struct vsock_sock *vsk, 906 bool cancel_timeout) 907 { 908 struct sock *sk = sk_vsock(vsk); 909 910 sock_set_flag(sk, SOCK_DONE); 911 vsk->peer_shutdown = SHUTDOWN_MASK; 912 if (vsock_stream_has_data(vsk) <= 0) 913 sk->sk_state = TCP_CLOSING; 914 sk->sk_state_change(sk); 915 916 if (vsk->close_work_scheduled && 917 (!cancel_timeout || cancel_delayed_work(&vsk->close_work))) { 918 vsk->close_work_scheduled = false; 919 920 virtio_transport_remove_sock(vsk); 921 922 /* Release refcnt obtained when we scheduled the timeout */ 923 sock_put(sk); 924 } 925 } 926 927 static void virtio_transport_close_timeout(struct work_struct *work) 928 { 929 struct vsock_sock *vsk = 930 container_of(work, struct vsock_sock, close_work.work); 931 struct sock *sk = sk_vsock(vsk); 932 933 sock_hold(sk); 934 lock_sock(sk); 935 936 if (!sock_flag(sk, SOCK_DONE)) { 937 (void)virtio_transport_reset(vsk, NULL); 938 939 virtio_transport_do_close(vsk, false); 940 } 941 942 vsk->close_work_scheduled = false; 943 944 release_sock(sk); 945 sock_put(sk); 946 } 947 948 /* User context, vsk->sk is locked */ 949 static bool virtio_transport_close(struct vsock_sock *vsk) 950 { 951 struct sock *sk = &vsk->sk; 952 953 if (!(sk->sk_state == TCP_ESTABLISHED || 954 sk->sk_state == TCP_CLOSING)) 955 return true; 956 957 /* Already received SHUTDOWN from peer, reply with RST */ 958 if ((vsk->peer_shutdown & SHUTDOWN_MASK) == SHUTDOWN_MASK) { 959 (void)virtio_transport_reset(vsk, NULL); 960 return true; 961 } 962 963 if ((sk->sk_shutdown & SHUTDOWN_MASK) != SHUTDOWN_MASK) 964 (void)virtio_transport_shutdown(vsk, SHUTDOWN_MASK); 965 966 if (sock_flag(sk, SOCK_LINGER) && !(current->flags & PF_EXITING)) 967 virtio_transport_wait_close(sk, sk->sk_lingertime); 968 969 if (sock_flag(sk, SOCK_DONE)) { 970 return true; 971 } 972 973 sock_hold(sk); 974 INIT_DELAYED_WORK(&vsk->close_work, 975 virtio_transport_close_timeout); 976 vsk->close_work_scheduled = true; 977 schedule_delayed_work(&vsk->close_work, VSOCK_CLOSE_TIMEOUT); 978 return false; 979 } 980 981 void virtio_transport_release(struct vsock_sock *vsk) 982 { 983 struct sock *sk = &vsk->sk; 984 bool remove_sock = true; 985 986 if (sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET) 987 remove_sock = virtio_transport_close(vsk); 988 989 if (remove_sock) { 990 sock_set_flag(sk, SOCK_DONE); 991 virtio_transport_remove_sock(vsk); 992 } 993 } 994 EXPORT_SYMBOL_GPL(virtio_transport_release); 995 996 static int 997 virtio_transport_recv_connecting(struct sock *sk, 998 struct sk_buff *skb) 999 { 1000 struct virtio_vsock_hdr *hdr = virtio_vsock_hdr(skb); 1001 struct vsock_sock *vsk = vsock_sk(sk); 1002 int skerr; 1003 int err; 1004 1005 switch (le16_to_cpu(hdr->op)) { 1006 case VIRTIO_VSOCK_OP_RESPONSE: 1007 sk->sk_state = TCP_ESTABLISHED; 1008 sk->sk_socket->state = SS_CONNECTED; 1009 vsock_insert_connected(vsk); 1010 sk->sk_state_change(sk); 1011 break; 1012 case VIRTIO_VSOCK_OP_INVALID: 1013 break; 1014 case VIRTIO_VSOCK_OP_RST: 1015 skerr = ECONNRESET; 1016 err = 0; 1017 goto destroy; 1018 default: 1019 skerr = EPROTO; 1020 err = -EINVAL; 1021 goto destroy; 1022 } 1023 return 0; 1024 1025 destroy: 1026 virtio_transport_reset(vsk, skb); 1027 sk->sk_state = TCP_CLOSE; 1028 sk->sk_err = skerr; 1029 sk_error_report(sk); 1030 return err; 1031 } 1032 1033 static void 1034 virtio_transport_recv_enqueue(struct vsock_sock *vsk, 1035 struct sk_buff *skb) 1036 { 1037 struct virtio_vsock_sock *vvs = vsk->trans; 1038 bool can_enqueue, free_pkt = false; 1039 struct virtio_vsock_hdr *hdr; 1040 u32 len; 1041 1042 hdr = virtio_vsock_hdr(skb); 1043 len = le32_to_cpu(hdr->len); 1044 1045 spin_lock_bh(&vvs->rx_lock); 1046 1047 can_enqueue = virtio_transport_inc_rx_pkt(vvs, len); 1048 if (!can_enqueue) { 1049 free_pkt = true; 1050 goto out; 1051 } 1052 1053 if (le32_to_cpu(hdr->flags) & VIRTIO_VSOCK_SEQ_EOM) 1054 vvs->msg_count++; 1055 1056 /* Try to copy small packets into the buffer of last packet queued, 1057 * to avoid wasting memory queueing the entire buffer with a small 1058 * payload. 1059 */ 1060 if (len <= GOOD_COPY_LEN && !skb_queue_empty(&vvs->rx_queue)) { 1061 struct virtio_vsock_hdr *last_hdr; 1062 struct sk_buff *last_skb; 1063 1064 last_skb = skb_peek_tail(&vvs->rx_queue); 1065 last_hdr = virtio_vsock_hdr(last_skb); 1066 1067 /* If there is space in the last packet queued, we copy the 1068 * new packet in its buffer. We avoid this if the last packet 1069 * queued has VIRTIO_VSOCK_SEQ_EOM set, because this is 1070 * delimiter of SEQPACKET message, so 'pkt' is the first packet 1071 * of a new message. 1072 */ 1073 if (skb->len < skb_tailroom(last_skb) && 1074 !(le32_to_cpu(last_hdr->flags) & VIRTIO_VSOCK_SEQ_EOM)) { 1075 memcpy(skb_put(last_skb, skb->len), skb->data, skb->len); 1076 free_pkt = true; 1077 last_hdr->flags |= hdr->flags; 1078 le32_add_cpu(&last_hdr->len, len); 1079 goto out; 1080 } 1081 } 1082 1083 __skb_queue_tail(&vvs->rx_queue, skb); 1084 1085 out: 1086 spin_unlock_bh(&vvs->rx_lock); 1087 if (free_pkt) 1088 kfree_skb(skb); 1089 } 1090 1091 static int 1092 virtio_transport_recv_connected(struct sock *sk, 1093 struct sk_buff *skb) 1094 { 1095 struct virtio_vsock_hdr *hdr = virtio_vsock_hdr(skb); 1096 struct vsock_sock *vsk = vsock_sk(sk); 1097 int err = 0; 1098 1099 switch (le16_to_cpu(hdr->op)) { 1100 case VIRTIO_VSOCK_OP_RW: 1101 virtio_transport_recv_enqueue(vsk, skb); 1102 vsock_data_ready(sk); 1103 return err; 1104 case VIRTIO_VSOCK_OP_CREDIT_REQUEST: 1105 virtio_transport_send_credit_update(vsk); 1106 break; 1107 case VIRTIO_VSOCK_OP_CREDIT_UPDATE: 1108 sk->sk_write_space(sk); 1109 break; 1110 case VIRTIO_VSOCK_OP_SHUTDOWN: 1111 if (le32_to_cpu(hdr->flags) & VIRTIO_VSOCK_SHUTDOWN_RCV) 1112 vsk->peer_shutdown |= RCV_SHUTDOWN; 1113 if (le32_to_cpu(hdr->flags) & VIRTIO_VSOCK_SHUTDOWN_SEND) 1114 vsk->peer_shutdown |= SEND_SHUTDOWN; 1115 if (vsk->peer_shutdown == SHUTDOWN_MASK && 1116 vsock_stream_has_data(vsk) <= 0 && 1117 !sock_flag(sk, SOCK_DONE)) { 1118 (void)virtio_transport_reset(vsk, NULL); 1119 virtio_transport_do_close(vsk, true); 1120 } 1121 if (le32_to_cpu(virtio_vsock_hdr(skb)->flags)) 1122 sk->sk_state_change(sk); 1123 break; 1124 case VIRTIO_VSOCK_OP_RST: 1125 virtio_transport_do_close(vsk, true); 1126 break; 1127 default: 1128 err = -EINVAL; 1129 break; 1130 } 1131 1132 kfree_skb(skb); 1133 return err; 1134 } 1135 1136 static void 1137 virtio_transport_recv_disconnecting(struct sock *sk, 1138 struct sk_buff *skb) 1139 { 1140 struct virtio_vsock_hdr *hdr = virtio_vsock_hdr(skb); 1141 struct vsock_sock *vsk = vsock_sk(sk); 1142 1143 if (le16_to_cpu(hdr->op) == VIRTIO_VSOCK_OP_RST) 1144 virtio_transport_do_close(vsk, true); 1145 } 1146 1147 static int 1148 virtio_transport_send_response(struct vsock_sock *vsk, 1149 struct sk_buff *skb) 1150 { 1151 struct virtio_vsock_hdr *hdr = virtio_vsock_hdr(skb); 1152 struct virtio_vsock_pkt_info info = { 1153 .op = VIRTIO_VSOCK_OP_RESPONSE, 1154 .remote_cid = le64_to_cpu(hdr->src_cid), 1155 .remote_port = le32_to_cpu(hdr->src_port), 1156 .reply = true, 1157 .vsk = vsk, 1158 }; 1159 1160 return virtio_transport_send_pkt_info(vsk, &info); 1161 } 1162 1163 static bool virtio_transport_space_update(struct sock *sk, 1164 struct sk_buff *skb) 1165 { 1166 struct virtio_vsock_hdr *hdr = virtio_vsock_hdr(skb); 1167 struct vsock_sock *vsk = vsock_sk(sk); 1168 struct virtio_vsock_sock *vvs = vsk->trans; 1169 bool space_available; 1170 1171 /* Listener sockets are not associated with any transport, so we are 1172 * not able to take the state to see if there is space available in the 1173 * remote peer, but since they are only used to receive requests, we 1174 * can assume that there is always space available in the other peer. 1175 */ 1176 if (!vvs) 1177 return true; 1178 1179 /* buf_alloc and fwd_cnt is always included in the hdr */ 1180 spin_lock_bh(&vvs->tx_lock); 1181 vvs->peer_buf_alloc = le32_to_cpu(hdr->buf_alloc); 1182 vvs->peer_fwd_cnt = le32_to_cpu(hdr->fwd_cnt); 1183 space_available = virtio_transport_has_space(vsk); 1184 spin_unlock_bh(&vvs->tx_lock); 1185 return space_available; 1186 } 1187 1188 /* Handle server socket */ 1189 static int 1190 virtio_transport_recv_listen(struct sock *sk, struct sk_buff *skb, 1191 struct virtio_transport *t) 1192 { 1193 struct virtio_vsock_hdr *hdr = virtio_vsock_hdr(skb); 1194 struct vsock_sock *vsk = vsock_sk(sk); 1195 struct vsock_sock *vchild; 1196 struct sock *child; 1197 int ret; 1198 1199 if (le16_to_cpu(hdr->op) != VIRTIO_VSOCK_OP_REQUEST) { 1200 virtio_transport_reset_no_sock(t, skb); 1201 return -EINVAL; 1202 } 1203 1204 if (sk_acceptq_is_full(sk)) { 1205 virtio_transport_reset_no_sock(t, skb); 1206 return -ENOMEM; 1207 } 1208 1209 child = vsock_create_connected(sk); 1210 if (!child) { 1211 virtio_transport_reset_no_sock(t, skb); 1212 return -ENOMEM; 1213 } 1214 1215 sk_acceptq_added(sk); 1216 1217 lock_sock_nested(child, SINGLE_DEPTH_NESTING); 1218 1219 child->sk_state = TCP_ESTABLISHED; 1220 1221 vchild = vsock_sk(child); 1222 vsock_addr_init(&vchild->local_addr, le64_to_cpu(hdr->dst_cid), 1223 le32_to_cpu(hdr->dst_port)); 1224 vsock_addr_init(&vchild->remote_addr, le64_to_cpu(hdr->src_cid), 1225 le32_to_cpu(hdr->src_port)); 1226 1227 ret = vsock_assign_transport(vchild, vsk); 1228 /* Transport assigned (looking at remote_addr) must be the same 1229 * where we received the request. 1230 */ 1231 if (ret || vchild->transport != &t->transport) { 1232 release_sock(child); 1233 virtio_transport_reset_no_sock(t, skb); 1234 sock_put(child); 1235 return ret; 1236 } 1237 1238 if (virtio_transport_space_update(child, skb)) 1239 child->sk_write_space(child); 1240 1241 vsock_insert_connected(vchild); 1242 vsock_enqueue_accept(sk, child); 1243 virtio_transport_send_response(vchild, skb); 1244 1245 release_sock(child); 1246 1247 sk->sk_data_ready(sk); 1248 return 0; 1249 } 1250 1251 static bool virtio_transport_valid_type(u16 type) 1252 { 1253 return (type == VIRTIO_VSOCK_TYPE_STREAM) || 1254 (type == VIRTIO_VSOCK_TYPE_SEQPACKET); 1255 } 1256 1257 /* We are under the virtio-vsock's vsock->rx_lock or vhost-vsock's vq->mutex 1258 * lock. 1259 */ 1260 void virtio_transport_recv_pkt(struct virtio_transport *t, 1261 struct sk_buff *skb) 1262 { 1263 struct virtio_vsock_hdr *hdr = virtio_vsock_hdr(skb); 1264 struct sockaddr_vm src, dst; 1265 struct vsock_sock *vsk; 1266 struct sock *sk; 1267 bool space_available; 1268 1269 vsock_addr_init(&src, le64_to_cpu(hdr->src_cid), 1270 le32_to_cpu(hdr->src_port)); 1271 vsock_addr_init(&dst, le64_to_cpu(hdr->dst_cid), 1272 le32_to_cpu(hdr->dst_port)); 1273 1274 trace_virtio_transport_recv_pkt(src.svm_cid, src.svm_port, 1275 dst.svm_cid, dst.svm_port, 1276 le32_to_cpu(hdr->len), 1277 le16_to_cpu(hdr->type), 1278 le16_to_cpu(hdr->op), 1279 le32_to_cpu(hdr->flags), 1280 le32_to_cpu(hdr->buf_alloc), 1281 le32_to_cpu(hdr->fwd_cnt)); 1282 1283 if (!virtio_transport_valid_type(le16_to_cpu(hdr->type))) { 1284 (void)virtio_transport_reset_no_sock(t, skb); 1285 goto free_pkt; 1286 } 1287 1288 /* The socket must be in connected or bound table 1289 * otherwise send reset back 1290 */ 1291 sk = vsock_find_connected_socket(&src, &dst); 1292 if (!sk) { 1293 sk = vsock_find_bound_socket(&dst); 1294 if (!sk) { 1295 (void)virtio_transport_reset_no_sock(t, skb); 1296 goto free_pkt; 1297 } 1298 } 1299 1300 if (virtio_transport_get_type(sk) != le16_to_cpu(hdr->type)) { 1301 (void)virtio_transport_reset_no_sock(t, skb); 1302 sock_put(sk); 1303 goto free_pkt; 1304 } 1305 1306 vsk = vsock_sk(sk); 1307 1308 lock_sock(sk); 1309 1310 /* Check if sk has been closed before lock_sock */ 1311 if (sock_flag(sk, SOCK_DONE)) { 1312 (void)virtio_transport_reset_no_sock(t, skb); 1313 release_sock(sk); 1314 sock_put(sk); 1315 goto free_pkt; 1316 } 1317 1318 space_available = virtio_transport_space_update(sk, skb); 1319 1320 /* Update CID in case it has changed after a transport reset event */ 1321 if (vsk->local_addr.svm_cid != VMADDR_CID_ANY) 1322 vsk->local_addr.svm_cid = dst.svm_cid; 1323 1324 if (space_available) 1325 sk->sk_write_space(sk); 1326 1327 switch (sk->sk_state) { 1328 case TCP_LISTEN: 1329 virtio_transport_recv_listen(sk, skb, t); 1330 kfree_skb(skb); 1331 break; 1332 case TCP_SYN_SENT: 1333 virtio_transport_recv_connecting(sk, skb); 1334 kfree_skb(skb); 1335 break; 1336 case TCP_ESTABLISHED: 1337 virtio_transport_recv_connected(sk, skb); 1338 break; 1339 case TCP_CLOSING: 1340 virtio_transport_recv_disconnecting(sk, skb); 1341 kfree_skb(skb); 1342 break; 1343 default: 1344 (void)virtio_transport_reset_no_sock(t, skb); 1345 kfree_skb(skb); 1346 break; 1347 } 1348 1349 release_sock(sk); 1350 1351 /* Release refcnt obtained when we fetched this socket out of the 1352 * bound or connected list. 1353 */ 1354 sock_put(sk); 1355 return; 1356 1357 free_pkt: 1358 kfree_skb(skb); 1359 } 1360 EXPORT_SYMBOL_GPL(virtio_transport_recv_pkt); 1361 1362 /* Remove skbs found in a queue that have a vsk that matches. 1363 * 1364 * Each skb is freed. 1365 * 1366 * Returns the count of skbs that were reply packets. 1367 */ 1368 int virtio_transport_purge_skbs(void *vsk, struct sk_buff_head *queue) 1369 { 1370 struct sk_buff_head freeme; 1371 struct sk_buff *skb, *tmp; 1372 int cnt = 0; 1373 1374 skb_queue_head_init(&freeme); 1375 1376 spin_lock_bh(&queue->lock); 1377 skb_queue_walk_safe(queue, skb, tmp) { 1378 if (vsock_sk(skb->sk) != vsk) 1379 continue; 1380 1381 __skb_unlink(skb, queue); 1382 __skb_queue_tail(&freeme, skb); 1383 1384 if (virtio_vsock_skb_reply(skb)) 1385 cnt++; 1386 } 1387 spin_unlock_bh(&queue->lock); 1388 1389 __skb_queue_purge(&freeme); 1390 1391 return cnt; 1392 } 1393 EXPORT_SYMBOL_GPL(virtio_transport_purge_skbs); 1394 1395 MODULE_LICENSE("GPL v2"); 1396 MODULE_AUTHOR("Asias He"); 1397 MODULE_DESCRIPTION("common code for virtio vsock"); 1398