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

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