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