9242350
From 66a5b93dee386bc2f57033a150341752923b8b41 Mon Sep 17 00:00:00 2001
9242350
From: Gerard Garcia <ggarcia@deic.uab.cat>
9242350
Date: Tue, 14 Jun 2016 16:45:44 +0200
9242350
Subject: [PATCH 13/13] Add printing support for vsockmon devices.
9242350
9242350
Print Linux 4.12 vsockmon captures:
9242350
9242350
  # modprobe vsockmon
9242350
  # ip link add type vsockmon
9242350
  # ip link set vsockmon0 up
9242350
  # tcpdump -i vsockmon0
9242350
  16:25:24.987917 VIRTIO 3.1025 > 2.1234 CONNECT, length 76
9242350
  16:25:24.987963 VIRTIO 2.1234 > 3.1025 CONNECT, length 76
9242350
  16:25:26.568271 VIRTIO 3.1025 > 2.1234 PAYLOAD, length 82
9242350
  16:25:26.568512 VIRTIO 2.1234 > 3.1025 CONTROL, length 76
9242350
  16:25:28.411335 VIRTIO 3.1025 > 2.1234 DISCONNECT, length 76
9242350
  16:25:28.411628 VIRTIO 2.1234 > 3.1025 DISCONNECT, length 76
9242350
9242350
For more information about vsock see:
9242350
http://wiki.qemu.org/Features/VirtioVsock
9242350
---
9242350
 Makefile.in   |   1 +
9242350
 netdissect.h  |   1 +
9242350
 print-vsock.c | 243 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
9242350
 print.c       |   3 +
9242350
 4 files changed, 248 insertions(+)
9242350
 create mode 100644 print-vsock.c
9242350
9242350
diff --git a/Makefile.in b/Makefile.in
9242350
index 0941f0e..a301878 100644
9242350
--- a/Makefile.in
9242350
+++ b/Makefile.in
9242350
@@ -226,6 +226,7 @@ LIBNETDISSECT_SRC=\
9242350
 	print-vjc.c \
9242350
 	print-vqp.c \
9242350
 	print-vrrp.c \
9242350
+	print-vsock.c \
9242350
 	print-vtp.c \
9242350
 	print-vxlan.c \
9242350
 	print-vxlan-gpe.c \
9242350
diff --git a/netdissect.h b/netdissect.h
9242350
index 089b040..c89fcf1 100644
9242350
--- a/netdissect.h
9242350
+++ b/netdissect.h
9242350
@@ -444,6 +444,7 @@ extern u_int symantec_if_print IF_PRINTER_ARGS;
9242350
 extern u_int token_if_print IF_PRINTER_ARGS;
9242350
 extern u_int usb_linux_48_byte_print IF_PRINTER_ARGS;
9242350
 extern u_int usb_linux_64_byte_print IF_PRINTER_ARGS;
9242350
+extern u_int vsock_print IF_PRINTER_ARGS;
9242350
 
9242350
 /*
9242350
  * Structure passed to some printers to allow them to print
9242350
diff --git a/print-vsock.c b/print-vsock.c
9242350
new file mode 100644
9242350
index 0000000..fc5694d
9242350
--- /dev/null
9242350
+++ b/print-vsock.c
9242350
@@ -0,0 +1,243 @@
9242350
+/*
9242350
+ * Copyright (c) 2016 Gerard Garcia <nouboh@gmail.com>
9242350
+ * Copyright (c) 2017 Red Hat, Inc.
9242350
+ *
9242350
+ * Redistribution and use in source and binary forms, with or without
9242350
+ * modification, are permitted provided that the following conditions
9242350
+ * are met:
9242350
+ *
9242350
+ *   1. Redistributions of source code must retain the above copyright
9242350
+ *      notice, this list of conditions and the following disclaimer.
9242350
+ *   2. Redistributions in binary form must reproduce the above copyright
9242350
+ *      notice, this list of conditions and the following disclaimer in
9242350
+ *      the documentation and/or other materials provided with the
9242350
+ *      distribution.
9242350
+ *   3. The names of the authors may not be used to endorse or promote
9242350
+ *      products derived from this software without specific prior
9242350
+ *      written permission.
9242350
+ *
9242350
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
9242350
+ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
9242350
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
9242350
+ */
9242350
+
9242350
+/* \summary: Linux vsock printer */
9242350
+
9242350
+#ifdef HAVE_CONFIG_H
9242350
+#include "config.h"
9242350
+#endif
9242350
+
9242350
+#include <netdissect-stdinc.h>
9242350
+#include <stddef.h>
9242350
+
9242350
+#include "netdissect.h"
9242350
+#include "extract.h"
9242350
+
9242350
+static const char tstr[] = " [|vsock]";
9242350
+
9242350
+enum af_vsockmon_transport {
9242350
+	AF_VSOCK_TRANSPORT_UNKNOWN = 0,
9242350
+	AF_VSOCK_TRANSPORT_NO_INFO = 1,		/* No transport information */
9242350
+	AF_VSOCK_TRANSPORT_VIRTIO = 2,		/* Virtio transport header */
9242350
+};
9242350
+
9242350
+static const struct tok vsock_transport[] = {
9242350
+	{AF_VSOCK_TRANSPORT_UNKNOWN, "UNKNOWN"},
9242350
+	{AF_VSOCK_TRANSPORT_NO_INFO, "NO_INFO"},
9242350
+	{AF_VSOCK_TRANSPORT_VIRTIO, "VIRTIO"},
9242350
+	{ 0, NULL }
9242350
+};
9242350
+
9242350
+enum af_vsockmon_op {
9242350
+	AF_VSOCK_OP_UNKNOWN = 0,
9242350
+	AF_VSOCK_OP_CONNECT = 1,
9242350
+	AF_VSOCK_OP_DISCONNECT = 2,
9242350
+	AF_VSOCK_OP_CONTROL = 3,
9242350
+	AF_VSOCK_OP_PAYLOAD = 4,
9242350
+};
9242350
+
9242350
+static const struct tok vsock_op[] = {
9242350
+	{AF_VSOCK_OP_UNKNOWN, "UNKNOWN"},
9242350
+	{AF_VSOCK_OP_CONNECT, "CONNECT"},
9242350
+	{AF_VSOCK_OP_DISCONNECT, "DISCONNECT"},
9242350
+	{AF_VSOCK_OP_CONTROL, "CONTROL"},
9242350
+	{AF_VSOCK_OP_PAYLOAD, "PAYLOAD"},
9242350
+	{ 0, NULL }
9242350
+};
9242350
+
9242350
+enum virtio_vsock_type {
9242350
+	VIRTIO_VSOCK_TYPE_STREAM = 1,
9242350
+};
9242350
+
9242350
+static const struct tok virtio_type[] = {
9242350
+	{VIRTIO_VSOCK_TYPE_STREAM, "STREAM"},
9242350
+	{ 0, NULL }
9242350
+};
9242350
+
9242350
+enum virtio_vsock_op {
9242350
+	VIRTIO_VSOCK_OP_INVALID = 0,
9242350
+	VIRTIO_VSOCK_OP_REQUEST = 1,
9242350
+	VIRTIO_VSOCK_OP_RESPONSE = 2,
9242350
+	VIRTIO_VSOCK_OP_RST = 3,
9242350
+	VIRTIO_VSOCK_OP_SHUTDOWN = 4,
9242350
+	VIRTIO_VSOCK_OP_RW = 5,
9242350
+	VIRTIO_VSOCK_OP_CREDIT_UPDATE = 6,
9242350
+	VIRTIO_VSOCK_OP_CREDIT_REQUEST = 7,
9242350
+};
9242350
+
9242350
+static const struct tok virtio_op[] = {
9242350
+	{VIRTIO_VSOCK_OP_INVALID, "INVALID"},
9242350
+	{VIRTIO_VSOCK_OP_REQUEST, "REQUEST"},
9242350
+	{VIRTIO_VSOCK_OP_RESPONSE, "RESPONSE"},
9242350
+	{VIRTIO_VSOCK_OP_RST, "RST"},
9242350
+	{VIRTIO_VSOCK_OP_SHUTDOWN, "SHUTDOWN"},
9242350
+	{VIRTIO_VSOCK_OP_RW, "RW"},
9242350
+	{VIRTIO_VSOCK_OP_CREDIT_UPDATE, "CREDIT UPDATE"},
9242350
+	{VIRTIO_VSOCK_OP_CREDIT_REQUEST, "CREDIT REQUEST"},
9242350
+	{ 0, NULL }
9242350
+};
9242350
+
9242350
+/* All fields are little-endian */
9242350
+
9242350
+struct virtio_vsock_hdr {
9242350
+	uint64_t	src_cid;
9242350
+	uint64_t	dst_cid;
9242350
+	uint32_t	src_port;
9242350
+	uint32_t	dst_port;
9242350
+	uint32_t	len;
9242350
+	uint16_t	type;		/* enum virtio_vsock_type */
9242350
+	uint16_t	op;		/* enum virtio_vsock_op */
9242350
+	uint32_t	flags;
9242350
+	uint32_t	buf_alloc;
9242350
+	uint32_t	fwd_cnt;
9242350
+} UNALIGNED;
9242350
+
9242350
+struct af_vsockmon_hdr {
9242350
+	uint64_t src_cid;
9242350
+	uint64_t dst_cid;
9242350
+	uint32_t src_port;
9242350
+	uint32_t dst_port;
9242350
+	uint16_t op;		/* enum af_vsockmon_op */
9242350
+	uint16_t transport;	/* enum af_vosckmon_transport */
9242350
+	uint16_t len;		/* size of transport header */
9242350
+	uint8_t reserved[2];
9242350
+};
9242350
+
9242350
+static void
9242350
+vsock_virtio_hdr_print(netdissect_options *ndo, const struct virtio_vsock_hdr *hdr)
9242350
+{
9242350
+	uint16_t u16_v;
9242350
+	uint32_t u32_v;
9242350
+
9242350
+	u32_v = EXTRACT_LE_32BITS(&hdr->len);
9242350
+	ND_PRINT((ndo, "len %u", u32_v));
9242350
+
9242350
+	u16_v = EXTRACT_LE_16BITS(&hdr->type);
9242350
+	ND_PRINT((ndo, ", type %s",
9242350
+		  tok2str(virtio_type, "Invalid type (%hu)", u16_v)));
9242350
+
9242350
+	u16_v = EXTRACT_LE_16BITS(&hdr->op);
9242350
+	ND_PRINT((ndo, ", op %s",
9242350
+		  tok2str(virtio_op, "Invalid op (%hu)", u16_v)));
9242350
+
9242350
+	u32_v = EXTRACT_LE_32BITS(&hdr->flags);
9242350
+	ND_PRINT((ndo, ", flags %x", u32_v));
9242350
+
9242350
+	u32_v = EXTRACT_LE_32BITS(&hdr->buf_alloc);
9242350
+	ND_PRINT((ndo, ", buf_alloc %u", u32_v));
9242350
+
9242350
+	u32_v = EXTRACT_LE_32BITS(&hdr->fwd_cnt);
9242350
+	ND_PRINT((ndo, ", fwd_cnt %u", u32_v));
9242350
+}
9242350
+
9242350
+static size_t
9242350
+vsock_transport_hdr_size(uint16_t transport)
9242350
+{
9242350
+	switch (transport) {
9242350
+		case AF_VSOCK_TRANSPORT_VIRTIO:
9242350
+			return sizeof(struct virtio_vsock_hdr);
9242350
+		default:
9242350
+			return 0;
9242350
+	}
9242350
+}
9242350
+
9242350
+static void
9242350
+vsock_transport_hdr_print(netdissect_options *ndo, uint16_t transport,
9242350
+                          const u_char *p, const u_int len)
9242350
+{
9242350
+	size_t transport_size = vsock_transport_hdr_size(transport);
9242350
+	const void *hdr;
9242350
+
9242350
+	if (len < sizeof(struct af_vsockmon_hdr) + transport_size)
9242350
+		return;
9242350
+
9242350
+	hdr = p + sizeof(struct af_vsockmon_hdr);
9242350
+	switch (transport) {
9242350
+		case AF_VSOCK_TRANSPORT_VIRTIO:
9242350
+			ND_PRINT((ndo, " ("));
9242350
+			vsock_virtio_hdr_print(ndo, hdr);
9242350
+			ND_PRINT((ndo, ")"));
9242350
+			break;
9242350
+		default:
9242350
+			break;
9242350
+	}
9242350
+}
9242350
+
9242350
+static void
9242350
+vsock_hdr_print(netdissect_options *ndo, const u_char *p, const u_int len)
9242350
+{
9242350
+	uint16_t hdr_transport, hdr_op;
9242350
+	uint32_t hdr_src_port, hdr_dst_port;
9242350
+	uint64_t hdr_src_cid, hdr_dst_cid;
9242350
+	size_t total_hdr_size;
9242350
+
9242350
+	const struct af_vsockmon_hdr *hdr = (struct af_vsockmon_hdr *)p;
9242350
+
9242350
+	hdr_transport = EXTRACT_LE_16BITS(&hdr->transport);
9242350
+	ND_PRINT((ndo, "%s",
9242350
+		  tok2str(vsock_transport, "Invalid transport (%u)",
9242350
+			  hdr_transport)));
9242350
+
9242350
+	/* If verbose level is more than 0 print transport details */
9242350
+	if (ndo->ndo_vflag) {
9242350
+		vsock_transport_hdr_print(ndo, hdr_transport, p, len);
9242350
+		ND_PRINT((ndo, "\n\t"));
9242350
+	} else
9242350
+		ND_PRINT((ndo, " "));
9242350
+
9242350
+	hdr_src_cid = EXTRACT_LE_64BITS(&hdr->src_cid);
9242350
+	hdr_dst_cid = EXTRACT_LE_64BITS(&hdr->dst_cid);
9242350
+	hdr_src_port = EXTRACT_LE_32BITS(&hdr->src_port);
9242350
+	hdr_dst_port = EXTRACT_LE_32BITS(&hdr->dst_port);
9242350
+	hdr_op = EXTRACT_LE_16BITS(&hdr->op);
9242350
+	ND_PRINT((ndo, "%lu.%hu > %lu.%hu %s, length %u",
9242350
+		  hdr_src_cid, hdr_src_port,
9242350
+		  hdr_dst_cid, hdr_dst_port,
9242350
+		  tok2str(vsock_op, " invalid op (%u)", hdr_op),
9242350
+		  len));
9242350
+
9242350
+	/* If debug level is more than 1 print payload contents */
9242350
+	total_hdr_size = sizeof(struct af_vsockmon_hdr) +
9242350
+			 vsock_transport_hdr_size(hdr_transport);
9242350
+	if (ndo->ndo_vflag > 1 &&
9242350
+	    hdr_op == AF_VSOCK_OP_PAYLOAD &&
9242350
+	    len > total_hdr_size) {
9242350
+		const u_char *payload = p + total_hdr_size;
9242350
+
9242350
+		ND_PRINT((ndo, "\n"));
9242350
+		print_unknown_data(ndo, payload, "\t", len - total_hdr_size);
9242350
+	}
9242350
+}
9242350
+
9242350
+u_int
9242350
+vsock_print(netdissect_options *ndo, const struct pcap_pkthdr *h, const u_char *cp)
9242350
+{
9242350
+	u_int len = h->len;
9242350
+
9242350
+	if (len < sizeof(struct af_vsockmon_hdr))
9242350
+		ND_PRINT((ndo, "%s", tstr));
9242350
+	else
9242350
+		vsock_hdr_print(ndo, cp, len);
9242350
+
9242350
+	return len;
9242350
+}
9242350
diff --git a/print.c b/print.c
9242350
index c76f344..1945cfd 100644
9242350
--- a/print.c
9242350
+++ b/print.c
9242350
@@ -220,6 +220,9 @@ static const struct printer printers[] = {
9242350
 #ifdef DLT_PPP_SERIAL
9242350
 	{ ppp_hdlc_if_print,	DLT_PPP_SERIAL },
9242350
 #endif
9242350
+#ifdef DLT_VSOCK
9242350
+	{ vsock_print,		DLT_VSOCK },
9242350
+#endif
9242350
 	{ NULL,			0 },
9242350
 };
9242350
 
9242350
-- 
9242350
2.13.5
9242350