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