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