xref: /freebsd-src/contrib/tcpdump/print-zeromq.c (revision 0a7e5f1f02aad2ff5fff1c60f44c6975fd07e1d9)
1d03c0883SXin LI /*
2d03c0883SXin LI  * Copyright (c) 2013 The TCPDUMP project
3d03c0883SXin LI  * All rights reserved.
4d03c0883SXin LI  *
5d03c0883SXin LI  * Redistribution and use in source and binary forms, with or without
6d03c0883SXin LI  * modification, are permitted provided that the following conditions
7d03c0883SXin LI  * are met:
8d03c0883SXin LI  * 1. Redistributions of source code must retain the above copyright
9d03c0883SXin LI  *    notice, this list of conditions and the following disclaimer.
10d03c0883SXin LI  * 2. Redistributions in binary form must reproduce the above copyright
11d03c0883SXin LI  *    notice, this list of conditions and the following disclaimer in the
12d03c0883SXin LI  *    documentation and/or other materials provided with the distribution.
13d03c0883SXin LI  *
14d03c0883SXin LI  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15d03c0883SXin LI  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16d03c0883SXin LI  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
17d03c0883SXin LI  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
18d03c0883SXin LI  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
19d03c0883SXin LI  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20d03c0883SXin LI  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21d03c0883SXin LI  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22d03c0883SXin LI  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23d03c0883SXin LI  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24d03c0883SXin LI  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25d03c0883SXin LI  * POSSIBILITY OF SUCH DAMAGE.
26d03c0883SXin LI  */
27d03c0883SXin LI 
283340d773SGleb Smirnoff /* \summary: ZeroMQ Message Transport Protocol (ZMTP) printer */
29*0a7e5f1fSJoseph Mingrone /* specification: https://rfc.zeromq.org/spec/13/ */
303340d773SGleb Smirnoff 
31ee67461eSJoseph Mingrone #include <config.h>
32d03c0883SXin LI 
33ee67461eSJoseph Mingrone #include "netdissect-stdinc.h"
34d03c0883SXin LI 
353340d773SGleb Smirnoff #include "netdissect.h"
36d03c0883SXin LI #include "extract.h"
37d03c0883SXin LI 
383c602fabSXin LI 
39d03c0883SXin LI /* Maximum number of ZMTP/1.0 frame body bytes (without the flags) to dump in
40d03c0883SXin LI  * hex and ASCII under a single "-v" flag.
41d03c0883SXin LI  */
42d03c0883SXin LI #define VBYTES 128
43d03c0883SXin LI 
44*0a7e5f1fSJoseph Mingrone static const struct tok flags_bm[] = {
45*0a7e5f1fSJoseph Mingrone 	{ 0x01, "MORE"  },
46*0a7e5f1fSJoseph Mingrone 	{ 0x02, "R1" },
47*0a7e5f1fSJoseph Mingrone 	{ 0x04, "R2" },
48*0a7e5f1fSJoseph Mingrone 	{ 0x08, "R3" },
49*0a7e5f1fSJoseph Mingrone 	{ 0x10, "R4" },
50*0a7e5f1fSJoseph Mingrone 	{ 0x20, "R5" },
51*0a7e5f1fSJoseph Mingrone 	{ 0x40, "R6" },
52*0a7e5f1fSJoseph Mingrone 	{ 0x80, "R7" },
53*0a7e5f1fSJoseph Mingrone 	{ 0, NULL }
54*0a7e5f1fSJoseph Mingrone };
55*0a7e5f1fSJoseph Mingrone 
56d03c0883SXin LI /*
57d03c0883SXin LI  * Below is an excerpt from the "13/ZMTP" specification:
58d03c0883SXin LI  *
59d03c0883SXin LI  * A ZMTP message consists of 1 or more frames.
60d03c0883SXin LI  *
61d03c0883SXin LI  * A ZMTP frame consists of a length, followed by a flags field and a frame
62d03c0883SXin LI  * body of (length - 1) octets. Note: the length includes the flags field, so
63d03c0883SXin LI  * an empty frame has a length of 1.
64d03c0883SXin LI  *
65d03c0883SXin LI  * For frames with a length of 1 to 254 octets, the length SHOULD BE encoded
66d03c0883SXin LI  * as a single octet. The minimum valid length of a frame is 1 octet, thus a
67d03c0883SXin LI  * length of 0 is invalid and such frames SHOULD be discarded silently.
68d03c0883SXin LI  *
69d03c0883SXin LI  * For frames with lengths of 255 and greater, the length SHALL BE encoded as
70d03c0883SXin LI  * a single octet with the value 255, followed by the length encoded as a
71d03c0883SXin LI  * 64-bit unsigned integer in network byte order. For frames with lengths of
72d03c0883SXin LI  * 1 to 254 octets this encoding MAY be also used.
73d03c0883SXin LI  *
74d03c0883SXin LI  * The flags field consists of a single octet containing various control
75d03c0883SXin LI  * flags. Bit 0 is the least significant bit.
76d03c0883SXin LI  *
77d03c0883SXin LI  * - Bit 0 (MORE): More frames to follow. A value of 0 indicates that there
78d03c0883SXin LI  *   are no more frames to follow. A value of 1 indicates that more frames
79d03c0883SXin LI  *   will follow. On messages consisting of a single frame the MORE flag MUST
80d03c0883SXin LI  *   be 0.
81d03c0883SXin LI  *
82d03c0883SXin LI  * - Bits 1-7: Reserved. Bits 1-7 are reserved for future use and SHOULD be
83d03c0883SXin LI  *   zero.
84d03c0883SXin LI  */
85d03c0883SXin LI 
86d03c0883SXin LI static const u_char *
878bdc5a62SPatrick Kelsey zmtp1_print_frame(netdissect_options *ndo, const u_char *cp, const u_char *ep)
888bdc5a62SPatrick Kelsey {
893c602fabSXin LI 	uint64_t body_len_declared, body_len_captured, header_len;
903c602fabSXin LI 	uint8_t flags;
91d03c0883SXin LI 
92ee67461eSJoseph Mingrone 	ND_PRINT("\n\t");
93d03c0883SXin LI 
94ee67461eSJoseph Mingrone 	if (GET_U_1(cp) != 0xFF) {	/* length/0xFF */
95d03c0883SXin LI 		header_len = 1; /* length */
96ee67461eSJoseph Mingrone 		body_len_declared = GET_U_1(cp);
97ee67461eSJoseph Mingrone 		ND_PRINT(" frame flags+body  (8-bit) length %" PRIu64, body_len_declared);
98d03c0883SXin LI 	} else {
99d03c0883SXin LI 		header_len = 1 + 8; /* 0xFF, length */
100ee67461eSJoseph Mingrone 		ND_PRINT(" frame flags+body (64-bit) length");
101ee67461eSJoseph Mingrone 		ND_TCHECK_LEN(cp, header_len); /* 0xFF, length */
102ee67461eSJoseph Mingrone 		body_len_declared = GET_BE_U_8(cp + 1);
103ee67461eSJoseph Mingrone 		ND_PRINT(" %" PRIu64, body_len_declared);
104d03c0883SXin LI 	}
1053340d773SGleb Smirnoff 	if (body_len_declared == 0)
1063340d773SGleb Smirnoff 		return cp + header_len; /* skip to the next frame */
107ee67461eSJoseph Mingrone 	ND_TCHECK_LEN(cp, header_len + 1); /* ..., flags */
108ee67461eSJoseph Mingrone 	flags = GET_U_1(cp + header_len);
109d03c0883SXin LI 
110d03c0883SXin LI 	body_len_captured = ep - cp - header_len;
111d03c0883SXin LI 	if (body_len_declared > body_len_captured)
112ee67461eSJoseph Mingrone 		ND_PRINT(" (%" PRIu64 " captured)", body_len_captured);
113ee67461eSJoseph Mingrone 	ND_PRINT(", flags 0x%02x", flags);
114d03c0883SXin LI 
1153c602fabSXin LI 	if (ndo->ndo_vflag) {
116ee67461eSJoseph Mingrone 		uint64_t body_len_printed = ND_MIN(body_len_captured, body_len_declared);
117d03c0883SXin LI 
118*0a7e5f1fSJoseph Mingrone 		ND_PRINT(" (%s)", bittok2str(flags_bm, "none", flags));
1193c602fabSXin LI 		if (ndo->ndo_vflag == 1)
120ee67461eSJoseph Mingrone 			body_len_printed = ND_MIN(VBYTES + 1, body_len_printed);
121d03c0883SXin LI 		if (body_len_printed > 1) {
122ee67461eSJoseph Mingrone 			ND_PRINT(", first %" PRIu64 " byte(s) of body:", body_len_printed - 1);
1233c602fabSXin LI 			hex_and_ascii_print(ndo, "\n\t ", cp + header_len + 1, body_len_printed - 1);
124d03c0883SXin LI 		}
125d03c0883SXin LI 	}
126d03c0883SXin LI 
1273340d773SGleb Smirnoff 	/*
1283340d773SGleb Smirnoff 	 * Do not advance cp by the sum of header_len and body_len_declared
129ee67461eSJoseph Mingrone 	 * before each offset has successfully passed ND_TCHECK_LEN() as the
1303340d773SGleb Smirnoff 	 * sum can roll over (9 + 0xfffffffffffffff7 = 0) and cause an
1313340d773SGleb Smirnoff 	 * infinite loop.
1323340d773SGleb Smirnoff 	 */
1333340d773SGleb Smirnoff 	cp += header_len;
134ee67461eSJoseph Mingrone 	ND_TCHECK_LEN(cp, body_len_declared); /* Next frame within the buffer ? */
1353340d773SGleb Smirnoff 	return cp + body_len_declared;
136d03c0883SXin LI 
137d03c0883SXin LI trunc:
138ee67461eSJoseph Mingrone 	nd_trunc_longjmp(ndo);
139d03c0883SXin LI }
140d03c0883SXin LI 
141d03c0883SXin LI void
1428bdc5a62SPatrick Kelsey zmtp1_print(netdissect_options *ndo, const u_char *cp, u_int len)
1438bdc5a62SPatrick Kelsey {
144ee67461eSJoseph Mingrone 	const u_char *ep = ND_MIN(ndo->ndo_snapend, cp + len);
145d03c0883SXin LI 
146ee67461eSJoseph Mingrone 	ndo->ndo_protocol = "zmtp1";
147ee67461eSJoseph Mingrone 	ND_PRINT(": ZMTP/1.0");
148d03c0883SXin LI 	while (cp < ep)
1493c602fabSXin LI 		cp = zmtp1_print_frame(ndo, cp, ep);
150d03c0883SXin LI }
151d03c0883SXin LI 
1523c602fabSXin LI /* The functions below decode a ZeroMQ datagram, supposedly stored in the "Data"
1533c602fabSXin LI  * field of an ODATA/RDATA [E]PGM packet. An excerpt from zmq_pgm(7) man page
1543c602fabSXin LI  * follows.
1553c602fabSXin LI  *
1563c602fabSXin LI  * In order for late joining consumers to be able to identify message
1573c602fabSXin LI  * boundaries, each PGM datagram payload starts with a 16-bit unsigned integer
1583c602fabSXin LI  * in network byte order specifying either the offset of the first message frame
1593c602fabSXin LI  * in the datagram or containing the value 0xFFFF if the datagram contains
1603c602fabSXin LI  * solely an intermediate part of a larger message.
1613c602fabSXin LI  *
1623c602fabSXin LI  * Note that offset specifies where the first message begins rather than the
1633c602fabSXin LI  * first message part. Thus, if there are trailing message parts at the
1643c602fabSXin LI  * beginning of the packet the offset ignores them and points to first initial
1653c602fabSXin LI  * message part in the packet.
1663c602fabSXin LI  */
1673c602fabSXin LI 
1683c602fabSXin LI static const u_char *
1698bdc5a62SPatrick Kelsey zmtp1_print_intermediate_part(netdissect_options *ndo, const u_char *cp, const u_int len)
1708bdc5a62SPatrick Kelsey {
1713c602fabSXin LI 	u_int frame_offset;
172ee67461eSJoseph Mingrone 	u_int remaining_len;
1733c602fabSXin LI 
174ee67461eSJoseph Mingrone 	frame_offset = GET_BE_U_2(cp);
175ee67461eSJoseph Mingrone 	ND_PRINT("\n\t frame offset 0x%04x", frame_offset);
1763c602fabSXin LI 	cp += 2;
177ee67461eSJoseph Mingrone 	remaining_len = ND_BYTES_AVAILABLE_AFTER(cp); /* without the frame length */
1783c602fabSXin LI 
1793c602fabSXin LI 	if (frame_offset == 0xFFFF)
1803c602fabSXin LI 		frame_offset = len - 2; /* always within the declared length */
1813c602fabSXin LI 	else if (2 + frame_offset > len) {
182ee67461eSJoseph Mingrone 		ND_PRINT(" (exceeds datagram declared length)");
1833c602fabSXin LI 		goto trunc;
1843c602fabSXin LI 	}
1853c602fabSXin LI 
1863c602fabSXin LI 	/* offset within declared length of the datagram */
1873c602fabSXin LI 	if (frame_offset) {
188ee67461eSJoseph Mingrone 		ND_PRINT("\n\t frame intermediate part, %u bytes", frame_offset);
1893c602fabSXin LI 		if (frame_offset > remaining_len)
190ee67461eSJoseph Mingrone 			ND_PRINT(" (%u captured)", remaining_len);
1913c602fabSXin LI 		if (ndo->ndo_vflag) {
192ee67461eSJoseph Mingrone 			u_int len_printed = ND_MIN(frame_offset, remaining_len);
1933c602fabSXin LI 
1943c602fabSXin LI 			if (ndo->ndo_vflag == 1)
195ee67461eSJoseph Mingrone 				len_printed = ND_MIN(VBYTES, len_printed);
1963c602fabSXin LI 			if (len_printed > 1) {
197ee67461eSJoseph Mingrone 				ND_PRINT(", first %u byte(s):", len_printed);
1983c602fabSXin LI 				hex_and_ascii_print(ndo, "\n\t ", cp, len_printed);
1993c602fabSXin LI 			}
2003c602fabSXin LI 		}
2013c602fabSXin LI 	}
2023c602fabSXin LI 	return cp + frame_offset;
2033c602fabSXin LI 
2043c602fabSXin LI trunc:
205ee67461eSJoseph Mingrone 	nd_trunc_longjmp(ndo);
2063c602fabSXin LI }
2073c602fabSXin LI 
2083c602fabSXin LI void
209ee67461eSJoseph Mingrone zmtp1_datagram_print(netdissect_options *ndo, const u_char *cp, const u_int len)
2108bdc5a62SPatrick Kelsey {
211ee67461eSJoseph Mingrone 	const u_char *ep = ND_MIN(ndo->ndo_snapend, cp + len);
2123c602fabSXin LI 
213ee67461eSJoseph Mingrone 	ndo->ndo_protocol = "zmtp1";
2143c602fabSXin LI 	cp = zmtp1_print_intermediate_part(ndo, cp, len);
2153c602fabSXin LI 	while (cp < ep)
2163c602fabSXin LI 		cp = zmtp1_print_frame(ndo, cp, ep);
2173c602fabSXin LI }
218