1*05b00f60SXin Li /*
2*05b00f60SXin Li * Copyright (c) 2013 The TCPDUMP project
3*05b00f60SXin Li * All rights reserved.
4*05b00f60SXin Li *
5*05b00f60SXin Li * Redistribution and use in source and binary forms, with or without
6*05b00f60SXin Li * modification, are permitted provided that the following conditions
7*05b00f60SXin Li * are met:
8*05b00f60SXin Li * 1. Redistributions of source code must retain the above copyright
9*05b00f60SXin Li * notice, this list of conditions and the following disclaimer.
10*05b00f60SXin Li * 2. Redistributions in binary form must reproduce the above copyright
11*05b00f60SXin Li * notice, this list of conditions and the following disclaimer in the
12*05b00f60SXin Li * documentation and/or other materials provided with the distribution.
13*05b00f60SXin Li *
14*05b00f60SXin Li * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15*05b00f60SXin Li * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16*05b00f60SXin Li * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
17*05b00f60SXin Li * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
18*05b00f60SXin Li * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
19*05b00f60SXin Li * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20*05b00f60SXin Li * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21*05b00f60SXin Li * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22*05b00f60SXin Li * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23*05b00f60SXin Li * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24*05b00f60SXin Li * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25*05b00f60SXin Li * POSSIBILITY OF SUCH DAMAGE.
26*05b00f60SXin Li */
27*05b00f60SXin Li
28*05b00f60SXin Li /* \summary: ZeroMQ Message Transport Protocol (ZMTP) printer */
29*05b00f60SXin Li
30*05b00f60SXin Li #ifdef HAVE_CONFIG_H
31*05b00f60SXin Li #include <config.h>
32*05b00f60SXin Li #endif
33*05b00f60SXin Li
34*05b00f60SXin Li #include "netdissect-stdinc.h"
35*05b00f60SXin Li
36*05b00f60SXin Li #include "netdissect.h"
37*05b00f60SXin Li #include "extract.h"
38*05b00f60SXin Li
39*05b00f60SXin Li
40*05b00f60SXin Li /* Maximum number of ZMTP/1.0 frame body bytes (without the flags) to dump in
41*05b00f60SXin Li * hex and ASCII under a single "-v" flag.
42*05b00f60SXin Li */
43*05b00f60SXin Li #define VBYTES 128
44*05b00f60SXin Li
45*05b00f60SXin Li /*
46*05b00f60SXin Li * Below is an excerpt from the "13/ZMTP" specification:
47*05b00f60SXin Li *
48*05b00f60SXin Li * A ZMTP message consists of 1 or more frames.
49*05b00f60SXin Li *
50*05b00f60SXin Li * A ZMTP frame consists of a length, followed by a flags field and a frame
51*05b00f60SXin Li * body of (length - 1) octets. Note: the length includes the flags field, so
52*05b00f60SXin Li * an empty frame has a length of 1.
53*05b00f60SXin Li *
54*05b00f60SXin Li * For frames with a length of 1 to 254 octets, the length SHOULD BE encoded
55*05b00f60SXin Li * as a single octet. The minimum valid length of a frame is 1 octet, thus a
56*05b00f60SXin Li * length of 0 is invalid and such frames SHOULD be discarded silently.
57*05b00f60SXin Li *
58*05b00f60SXin Li * For frames with lengths of 255 and greater, the length SHALL BE encoded as
59*05b00f60SXin Li * a single octet with the value 255, followed by the length encoded as a
60*05b00f60SXin Li * 64-bit unsigned integer in network byte order. For frames with lengths of
61*05b00f60SXin Li * 1 to 254 octets this encoding MAY be also used.
62*05b00f60SXin Li *
63*05b00f60SXin Li * The flags field consists of a single octet containing various control
64*05b00f60SXin Li * flags. Bit 0 is the least significant bit.
65*05b00f60SXin Li *
66*05b00f60SXin Li * - Bit 0 (MORE): More frames to follow. A value of 0 indicates that there
67*05b00f60SXin Li * are no more frames to follow. A value of 1 indicates that more frames
68*05b00f60SXin Li * will follow. On messages consisting of a single frame the MORE flag MUST
69*05b00f60SXin Li * be 0.
70*05b00f60SXin Li *
71*05b00f60SXin Li * - Bits 1-7: Reserved. Bits 1-7 are reserved for future use and SHOULD be
72*05b00f60SXin Li * zero.
73*05b00f60SXin Li */
74*05b00f60SXin Li
75*05b00f60SXin Li static const u_char *
zmtp1_print_frame(netdissect_options * ndo,const u_char * cp,const u_char * ep)76*05b00f60SXin Li zmtp1_print_frame(netdissect_options *ndo, const u_char *cp, const u_char *ep)
77*05b00f60SXin Li {
78*05b00f60SXin Li uint64_t body_len_declared, body_len_captured, header_len;
79*05b00f60SXin Li uint8_t flags;
80*05b00f60SXin Li
81*05b00f60SXin Li ND_PRINT("\n\t");
82*05b00f60SXin Li
83*05b00f60SXin Li if (GET_U_1(cp) != 0xFF) { /* length/0xFF */
84*05b00f60SXin Li header_len = 1; /* length */
85*05b00f60SXin Li body_len_declared = GET_U_1(cp);
86*05b00f60SXin Li ND_PRINT(" frame flags+body (8-bit) length %" PRIu64, body_len_declared);
87*05b00f60SXin Li } else {
88*05b00f60SXin Li header_len = 1 + 8; /* 0xFF, length */
89*05b00f60SXin Li ND_PRINT(" frame flags+body (64-bit) length");
90*05b00f60SXin Li ND_TCHECK_LEN(cp, header_len); /* 0xFF, length */
91*05b00f60SXin Li body_len_declared = GET_BE_U_8(cp + 1);
92*05b00f60SXin Li ND_PRINT(" %" PRIu64, body_len_declared);
93*05b00f60SXin Li }
94*05b00f60SXin Li if (body_len_declared == 0)
95*05b00f60SXin Li return cp + header_len; /* skip to the next frame */
96*05b00f60SXin Li ND_TCHECK_LEN(cp, header_len + 1); /* ..., flags */
97*05b00f60SXin Li flags = GET_U_1(cp + header_len);
98*05b00f60SXin Li
99*05b00f60SXin Li body_len_captured = ep - cp - header_len;
100*05b00f60SXin Li if (body_len_declared > body_len_captured)
101*05b00f60SXin Li ND_PRINT(" (%" PRIu64 " captured)", body_len_captured);
102*05b00f60SXin Li ND_PRINT(", flags 0x%02x", flags);
103*05b00f60SXin Li
104*05b00f60SXin Li if (ndo->ndo_vflag) {
105*05b00f60SXin Li uint64_t body_len_printed = ND_MIN(body_len_captured, body_len_declared);
106*05b00f60SXin Li
107*05b00f60SXin Li ND_PRINT(" (%s|%s|%s|%s|%s|%s|%s|%s)",
108*05b00f60SXin Li flags & 0x80 ? "MBZ" : "-",
109*05b00f60SXin Li flags & 0x40 ? "MBZ" : "-",
110*05b00f60SXin Li flags & 0x20 ? "MBZ" : "-",
111*05b00f60SXin Li flags & 0x10 ? "MBZ" : "-",
112*05b00f60SXin Li flags & 0x08 ? "MBZ" : "-",
113*05b00f60SXin Li flags & 0x04 ? "MBZ" : "-",
114*05b00f60SXin Li flags & 0x02 ? "MBZ" : "-",
115*05b00f60SXin Li flags & 0x01 ? "MORE" : "-");
116*05b00f60SXin Li
117*05b00f60SXin Li if (ndo->ndo_vflag == 1)
118*05b00f60SXin Li body_len_printed = ND_MIN(VBYTES + 1, body_len_printed);
119*05b00f60SXin Li if (body_len_printed > 1) {
120*05b00f60SXin Li ND_PRINT(", first %" PRIu64 " byte(s) of body:", body_len_printed - 1);
121*05b00f60SXin Li hex_and_ascii_print(ndo, "\n\t ", cp + header_len + 1, body_len_printed - 1);
122*05b00f60SXin Li }
123*05b00f60SXin Li }
124*05b00f60SXin Li
125*05b00f60SXin Li /*
126*05b00f60SXin Li * Do not advance cp by the sum of header_len and body_len_declared
127*05b00f60SXin Li * before each offset has successfully passed ND_TCHECK_LEN() as the
128*05b00f60SXin Li * sum can roll over (9 + 0xfffffffffffffff7 = 0) and cause an
129*05b00f60SXin Li * infinite loop.
130*05b00f60SXin Li */
131*05b00f60SXin Li cp += header_len;
132*05b00f60SXin Li ND_TCHECK_LEN(cp, body_len_declared); /* Next frame within the buffer ? */
133*05b00f60SXin Li return cp + body_len_declared;
134*05b00f60SXin Li
135*05b00f60SXin Li trunc:
136*05b00f60SXin Li nd_trunc_longjmp(ndo);
137*05b00f60SXin Li }
138*05b00f60SXin Li
139*05b00f60SXin Li void
zmtp1_print(netdissect_options * ndo,const u_char * cp,u_int len)140*05b00f60SXin Li zmtp1_print(netdissect_options *ndo, const u_char *cp, u_int len)
141*05b00f60SXin Li {
142*05b00f60SXin Li const u_char *ep = ND_MIN(ndo->ndo_snapend, cp + len);
143*05b00f60SXin Li
144*05b00f60SXin Li ndo->ndo_protocol = "zmtp1";
145*05b00f60SXin Li ND_PRINT(": ZMTP/1.0");
146*05b00f60SXin Li while (cp < ep)
147*05b00f60SXin Li cp = zmtp1_print_frame(ndo, cp, ep);
148*05b00f60SXin Li }
149*05b00f60SXin Li
150*05b00f60SXin Li /* The functions below decode a ZeroMQ datagram, supposedly stored in the "Data"
151*05b00f60SXin Li * field of an ODATA/RDATA [E]PGM packet. An excerpt from zmq_pgm(7) man page
152*05b00f60SXin Li * follows.
153*05b00f60SXin Li *
154*05b00f60SXin Li * In order for late joining consumers to be able to identify message
155*05b00f60SXin Li * boundaries, each PGM datagram payload starts with a 16-bit unsigned integer
156*05b00f60SXin Li * in network byte order specifying either the offset of the first message frame
157*05b00f60SXin Li * in the datagram or containing the value 0xFFFF if the datagram contains
158*05b00f60SXin Li * solely an intermediate part of a larger message.
159*05b00f60SXin Li *
160*05b00f60SXin Li * Note that offset specifies where the first message begins rather than the
161*05b00f60SXin Li * first message part. Thus, if there are trailing message parts at the
162*05b00f60SXin Li * beginning of the packet the offset ignores them and points to first initial
163*05b00f60SXin Li * message part in the packet.
164*05b00f60SXin Li */
165*05b00f60SXin Li
166*05b00f60SXin Li static const u_char *
zmtp1_print_intermediate_part(netdissect_options * ndo,const u_char * cp,const u_int len)167*05b00f60SXin Li zmtp1_print_intermediate_part(netdissect_options *ndo, const u_char *cp, const u_int len)
168*05b00f60SXin Li {
169*05b00f60SXin Li u_int frame_offset;
170*05b00f60SXin Li u_int remaining_len;
171*05b00f60SXin Li
172*05b00f60SXin Li frame_offset = GET_BE_U_2(cp);
173*05b00f60SXin Li ND_PRINT("\n\t frame offset 0x%04x", frame_offset);
174*05b00f60SXin Li cp += 2;
175*05b00f60SXin Li remaining_len = ND_BYTES_AVAILABLE_AFTER(cp); /* without the frame length */
176*05b00f60SXin Li
177*05b00f60SXin Li if (frame_offset == 0xFFFF)
178*05b00f60SXin Li frame_offset = len - 2; /* always within the declared length */
179*05b00f60SXin Li else if (2 + frame_offset > len) {
180*05b00f60SXin Li ND_PRINT(" (exceeds datagram declared length)");
181*05b00f60SXin Li goto trunc;
182*05b00f60SXin Li }
183*05b00f60SXin Li
184*05b00f60SXin Li /* offset within declared length of the datagram */
185*05b00f60SXin Li if (frame_offset) {
186*05b00f60SXin Li ND_PRINT("\n\t frame intermediate part, %u bytes", frame_offset);
187*05b00f60SXin Li if (frame_offset > remaining_len)
188*05b00f60SXin Li ND_PRINT(" (%u captured)", remaining_len);
189*05b00f60SXin Li if (ndo->ndo_vflag) {
190*05b00f60SXin Li u_int len_printed = ND_MIN(frame_offset, remaining_len);
191*05b00f60SXin Li
192*05b00f60SXin Li if (ndo->ndo_vflag == 1)
193*05b00f60SXin Li len_printed = ND_MIN(VBYTES, len_printed);
194*05b00f60SXin Li if (len_printed > 1) {
195*05b00f60SXin Li ND_PRINT(", first %u byte(s):", len_printed);
196*05b00f60SXin Li hex_and_ascii_print(ndo, "\n\t ", cp, len_printed);
197*05b00f60SXin Li }
198*05b00f60SXin Li }
199*05b00f60SXin Li }
200*05b00f60SXin Li return cp + frame_offset;
201*05b00f60SXin Li
202*05b00f60SXin Li trunc:
203*05b00f60SXin Li nd_trunc_longjmp(ndo);
204*05b00f60SXin Li }
205*05b00f60SXin Li
206*05b00f60SXin Li void
zmtp1_datagram_print(netdissect_options * ndo,const u_char * cp,const u_int len)207*05b00f60SXin Li zmtp1_datagram_print(netdissect_options *ndo, const u_char *cp, const u_int len)
208*05b00f60SXin Li {
209*05b00f60SXin Li const u_char *ep = ND_MIN(ndo->ndo_snapend, cp + len);
210*05b00f60SXin Li
211*05b00f60SXin Li ndo->ndo_protocol = "zmtp1";
212*05b00f60SXin Li cp = zmtp1_print_intermediate_part(ndo, cp, len);
213*05b00f60SXin Li while (cp < ep)
214*05b00f60SXin Li cp = zmtp1_print_frame(ndo, cp, ep);
215*05b00f60SXin Li }
216