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