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

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