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

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