Blame SOURCES/0012-Add-printing-support-for-vsockmon-devices.patch

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