xref: /freebsd/contrib/tcpdump/print-tipc.c (revision ba3c1f5972d7b90feb6e6da47905ff2757e0fe57)
1 /*
2  * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that: (1) source code distributions
7  * retain the above copyright notice and this paragraph in its entirety, (2)
8  * distributions including binary code include the above copyright notice and
9  * this paragraph in its entirety in the documentation or other materials
10  * provided with the distribution, and (3) all advertising materials mentioning
11  * features or use of this software display the following acknowledgement:
12  * ``This product includes software developed by the University of California,
13  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14  * the University nor the names of its contributors may be used to endorse
15  * or promote products derived from this software without specific prior
16  * written permission.
17  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20  */
21 
22 /* \summary: Transparent Inter-Process Communication (TIPC) protocol printer */
23 
24 /*
25  * specification:
26  *     https://web.archive.org/web/20150302152944/http://tipc.sourceforge.net/doc/draft-spec-tipc-07.html
27  *     https://web.archive.org/web/20161025110514/http://tipc.sourceforge.net/doc/tipc_message_formats.html
28  */
29 
30 #ifdef HAVE_CONFIG_H
31 #include <config.h>
32 #endif
33 
34 #include "netdissect-stdinc.h"
35 
36 #include "netdissect.h"
37 #include "ethertype.h"
38 #include "extract.h"
39 
40 
41 #define TIPC_USER_LOW_IMPORTANCE	0
42 #define TIPC_USER_MEDIUM_IMPORTANCE	1
43 #define TIPC_USER_HIGH_IMPORTANCE	2
44 #define TIPC_USER_CRITICAL_IMPORTANCE	3
45 #define TIPC_USER_BCAST_PROTOCOL	5
46 #define TIPC_USER_MSG_BUNDLER		6
47 #define TIPC_USER_LINK_PROTOCOL		7
48 #define TIPC_USER_CONN_MANAGER		8
49 #define TIPC_USER_CHANGEOVER_PROTOCOL	10
50 #define TIPC_USER_NAME_DISTRIBUTOR	11
51 #define TIPC_USER_MSG_FRAGMENTER	12
52 #define TIPC_USER_LINK_CONFIG		13
53 
54 #define TIPC_CONN_MSG			0
55 #define TIPC_DIRECT_MSG			1
56 #define TIPC_NAMED_MSG			2
57 #define TIPC_MCAST_MSG			3
58 
59 #define TIPC_ZONE(addr)		(((addr) >> 24) & 0xFF)
60 #define TIPC_CLUSTER(addr)	(((addr) >> 12) & 0xFFF)
61 #define TIPC_NODE(addr)		(((addr) >> 0) & 0xFFF)
62 
63 struct tipc_pkthdr {
64 	nd_uint32_t w0;
65 	nd_uint32_t w1;
66 };
67 
68 #define TIPC_VER(w0)		(((w0) >> 29) & 0x07)
69 #define TIPC_USER(w0)		(((w0) >> 25) & 0x0F)
70 #define TIPC_HSIZE(w0)		(((w0) >> 21) & 0x0F)
71 #define TIPC_MSIZE(w0)		(((w0) >> 0) & 0x1FFFF)
72 #define TIPC_MTYPE(w1)		(((w1) >> 29) & 0x07)
73 #define TIPC_BROADCAST_ACK(w1)	(((w1) >> 0) & 0xFFFF)
74 #define TIPC_LINK_ACK(w2)	(((w2) >> 16) & 0xFFFF)
75 #define TIPC_LINK_SEQ(w2)	(((w2) >> 0) & 0xFFFF)
76 
77 static const struct tok tipcuser_values[] = {
78     { TIPC_USER_LOW_IMPORTANCE,      "Low Importance Data payload" },
79     { TIPC_USER_MEDIUM_IMPORTANCE,   "Medium Importance Data payload" },
80     { TIPC_USER_HIGH_IMPORTANCE,     "High Importance Data payload" },
81     { TIPC_USER_CRITICAL_IMPORTANCE, "Critical Importance Data payload" },
82     { TIPC_USER_BCAST_PROTOCOL,      "Broadcast Link Protocol internal" },
83     { TIPC_USER_MSG_BUNDLER,         "Message Bundler Protocol internal" },
84     { TIPC_USER_LINK_PROTOCOL,       "Link State Protocol internal" },
85     { TIPC_USER_CONN_MANAGER,        "Connection Manager internal" },
86     { TIPC_USER_CHANGEOVER_PROTOCOL, "Link Changeover Protocol internal" },
87     { TIPC_USER_NAME_DISTRIBUTOR,    "Name Table Update Protocol internal" },
88     { TIPC_USER_MSG_FRAGMENTER,      "Message Fragmentation Protocol internal" },
89     { TIPC_USER_LINK_CONFIG,         "Neighbor Detection Protocol internal" },
90     { 0, NULL }
91 };
92 
93 static const struct tok tipcmtype_values[] = {
94     { TIPC_CONN_MSG,   "CONN_MSG" },
95     { TIPC_DIRECT_MSG, "MCAST_MSG" },
96     { TIPC_NAMED_MSG,  "NAMED_MSG" },
97     { TIPC_MCAST_MSG,  "DIRECT_MSG" },
98     { 0, NULL }
99 };
100 
101 static const struct tok tipc_linkconf_mtype_values[] = {
102     { 0,   "Link request" },
103     { 1,   "Link response" },
104     { 0, NULL }
105 };
106 
107 struct payload_tipc_pkthdr {
108 	nd_uint32_t w0;
109 	nd_uint32_t w1;
110 	nd_uint32_t w2;
111 	nd_uint32_t prev_node;
112 	nd_uint32_t orig_port;
113 	nd_uint32_t dest_port;
114 	nd_uint32_t orig_node;
115 	nd_uint32_t dest_node;
116 	nd_uint32_t name_type;
117 	nd_uint32_t w9;
118 	nd_uint32_t wA;
119 };
120 
121 struct  internal_tipc_pkthdr {
122 	nd_uint32_t w0;
123 	nd_uint32_t w1;
124 	nd_uint32_t w2;
125 	nd_uint32_t prev_node;
126 	nd_uint32_t w4;
127 	nd_uint32_t w5;
128 	nd_uint32_t orig_node;
129 	nd_uint32_t dest_node;
130 	nd_uint32_t trans_seq;
131 	nd_uint32_t w9;
132 };
133 
134 #define TIPC_SEQ_GAP(w1)	(((w1) >> 16) & 0x1FFF)
135 #define TIPC_BC_GAP_AFTER(w2)	(((w2) >> 16) & 0xFFFF)
136 #define TIPC_BC_GAP_TO(w2)	(((w2) >> 0) & 0xFFFF)
137 #define TIPC_LAST_SENT_FRAG(w4)	(((w4) >> 16) & 0xFFFF)
138 #define TIPC_NEXT_SENT_FRAG(w4)	(((w4) >> 0) & 0xFFFF)
139 #define TIPC_SESS_NO(w5)	(((w5) >> 16) & 0xFFFF)
140 #define TIPC_MSG_CNT(w9)	(((w9) >> 16) & 0xFFFF)
141 #define TIPC_LINK_TOL(w9)	(((w9) >> 0) & 0xFFFF)
142 
143 struct link_conf_tipc_pkthdr {
144 	nd_uint32_t w0;
145 	nd_uint32_t w1;
146 	nd_uint32_t dest_domain;
147 	nd_uint32_t prev_node;
148 	nd_uint32_t ntwrk_id;
149 	nd_uint32_t w5;
150 	nd_byte     media_address[16];
151 };
152 
153 #define TIPC_NODE_SIG(w1)	(((w1) >> 0) & 0xFFFF)
154 #define TIPC_MEDIA_ID(w5)	(((w5) >> 0) & 0xFF)
155 
156 static void
157 print_payload(netdissect_options *ndo, const struct payload_tipc_pkthdr *ap)
158 {
159 	uint32_t w0, w1, w2;
160 	u_int user;
161 	u_int hsize;
162 	u_int msize;
163 	u_int mtype;
164 	u_int broadcast_ack;
165 	u_int link_ack;
166 	u_int link_seq;
167 	u_int prev_node;
168 	u_int orig_port;
169 	u_int dest_port;
170 	u_int orig_node;
171 	u_int dest_node;
172 
173 	w0 = GET_BE_U_4(ap->w0);
174 	user = TIPC_USER(w0);
175 	hsize = TIPC_HSIZE(w0);
176 	msize = TIPC_MSIZE(w0);
177 	w1 = GET_BE_U_4(ap->w1);
178 	mtype = TIPC_MTYPE(w1);
179 	prev_node = GET_BE_U_4(ap->prev_node);
180 	orig_port = GET_BE_U_4(ap->orig_port);
181 	dest_port = GET_BE_U_4(ap->dest_port);
182 	if (hsize <= 6) {
183 		ND_PRINT("TIPC v%u.0 %u.%u.%u:%u > %u, headerlength %u bytes, MessageSize %u bytes, %s, messageType %s",
184 		    TIPC_VER(w0),
185 		    TIPC_ZONE(prev_node), TIPC_CLUSTER(prev_node), TIPC_NODE(prev_node),
186 		    orig_port, dest_port,
187 		    hsize*4, msize,
188 		    tok2str(tipcuser_values, "unknown", user),
189 		    tok2str(tipcmtype_values, "Unknown", mtype));
190 	} else {
191 		orig_node = GET_BE_U_4(ap->orig_node);
192 		dest_node = GET_BE_U_4(ap->dest_node);
193 		ND_PRINT("TIPC v%u.0 %u.%u.%u:%u > %u.%u.%u:%u, headerlength %u bytes, MessageSize %u bytes, %s, messageType %s",
194 		    TIPC_VER(w0),
195 		    TIPC_ZONE(orig_node), TIPC_CLUSTER(orig_node), TIPC_NODE(orig_node),
196 		    orig_port,
197 		    TIPC_ZONE(dest_node), TIPC_CLUSTER(dest_node), TIPC_NODE(dest_node),
198 		    dest_port,
199 		    hsize*4, msize,
200 		    tok2str(tipcuser_values, "unknown", user),
201 		    tok2str(tipcmtype_values, "Unknown", mtype));
202 
203 		if (ndo->ndo_vflag) {
204 			broadcast_ack = TIPC_BROADCAST_ACK(w1);
205 			w2 = GET_BE_U_4(ap->w2);
206 			link_ack = TIPC_LINK_ACK(w2);
207 			link_seq = TIPC_LINK_SEQ(w2);
208 			ND_PRINT("\n\tPrevious Node %u.%u.%u, Broadcast Ack %u, Link Ack %u, Link Sequence %u",
209 			    TIPC_ZONE(prev_node), TIPC_CLUSTER(prev_node), TIPC_NODE(prev_node),
210 			    broadcast_ack, link_ack, link_seq);
211 		}
212 	}
213 }
214 
215 static void
216 print_internal(netdissect_options *ndo, const struct internal_tipc_pkthdr *ap)
217 {
218 	uint32_t w0, w1, w2, w4, w5, w9;
219 	u_int user;
220 	u_int hsize;
221 	u_int msize;
222 	u_int mtype;
223 	u_int seq_gap;
224 	u_int broadcast_ack;
225 	u_int bc_gap_after;
226 	u_int bc_gap_to;
227 	u_int prev_node;
228 	u_int last_sent_frag;
229 	u_int next_sent_frag;
230 	u_int sess_no;
231 	u_int orig_node;
232 	u_int dest_node;
233 	u_int trans_seq;
234 	u_int msg_cnt;
235 	u_int link_tol;
236 
237 	w0 = GET_BE_U_4(ap->w0);
238 	user = TIPC_USER(w0);
239 	hsize = TIPC_HSIZE(w0);
240 	msize = TIPC_MSIZE(w0);
241 	w1 = GET_BE_U_4(ap->w1);
242 	mtype = TIPC_MTYPE(w1);
243 	orig_node = GET_BE_U_4(ap->orig_node);
244 	dest_node = GET_BE_U_4(ap->dest_node);
245 	ND_PRINT("TIPC v%u.0 %u.%u.%u > %u.%u.%u, headerlength %u bytes, MessageSize %u bytes, %s, messageType %s (0x%08x)",
246 	    TIPC_VER(w0),
247 	    TIPC_ZONE(orig_node), TIPC_CLUSTER(orig_node), TIPC_NODE(orig_node),
248 	    TIPC_ZONE(dest_node), TIPC_CLUSTER(dest_node), TIPC_NODE(dest_node),
249 	    hsize*4, msize,
250 	    tok2str(tipcuser_values, "unknown", user),
251 	    tok2str(tipcmtype_values, "Unknown", mtype), w1);
252 
253 	if (ndo->ndo_vflag) {
254 		seq_gap = TIPC_SEQ_GAP(w1);
255 		broadcast_ack = TIPC_BROADCAST_ACK(w1);
256 		w2 = GET_BE_U_4(ap->w2);
257 		bc_gap_after = TIPC_BC_GAP_AFTER(w2);
258 		bc_gap_to = TIPC_BC_GAP_TO(w2);
259 		prev_node = GET_BE_U_4(ap->prev_node);
260 		w4 = GET_BE_U_4(ap->w4);
261 		last_sent_frag = TIPC_LAST_SENT_FRAG(w4);
262 		next_sent_frag = TIPC_NEXT_SENT_FRAG(w4);
263 		w5 = GET_BE_U_4(ap->w5);
264 		sess_no = TIPC_SESS_NO(w5);
265 		trans_seq = GET_BE_U_4(ap->trans_seq);
266 		w9 = GET_BE_U_4(ap->w9);
267 		msg_cnt = TIPC_MSG_CNT(w9);
268 		link_tol = TIPC_LINK_TOL(w9);
269 		ND_PRINT("\n\tPrevious Node %u.%u.%u, Session No. %u, Broadcast Ack %u, Sequence Gap %u,  Broadcast Gap After %u, Broadcast Gap To %u, Last Sent Packet No. %u, Next sent Packet No. %u, Transport Sequence %u, msg_count %u, Link Tolerance %u",
270 		    TIPC_ZONE(prev_node), TIPC_CLUSTER(prev_node), TIPC_NODE(prev_node),
271 		    sess_no, broadcast_ack, seq_gap, bc_gap_after, bc_gap_to,
272 		    last_sent_frag, next_sent_frag, trans_seq, msg_cnt,
273 		    link_tol);
274 	}
275 }
276 
277 static void
278 print_link_conf(netdissect_options *ndo, const struct link_conf_tipc_pkthdr *ap)
279 {
280 	uint32_t w0, w1, w5;
281 	u_int user;
282 	u_int hsize;
283 	u_int msize;
284 	u_int mtype;
285 	u_int node_sig;
286 	u_int prev_node;
287 	u_int dest_domain;
288 	u_int ntwrk_id;
289 	u_int media_id;
290 
291 	w0 = GET_BE_U_4(ap->w0);
292 	user = TIPC_USER(w0);
293 	hsize = TIPC_HSIZE(w0);
294 	msize = TIPC_MSIZE(w0);
295 	w1 = GET_BE_U_4(ap->w1);
296 	mtype = TIPC_MTYPE(w1);
297 	dest_domain = GET_BE_U_4(ap->dest_domain);
298 	prev_node = GET_BE_U_4(ap->prev_node);
299 
300 	ND_PRINT("TIPC v%u.0 %u.%u.%u > %u.%u.%u, headerlength %u bytes, MessageSize %u bytes, %s, messageType %s",
301 	    TIPC_VER(w0),
302 	    TIPC_ZONE(prev_node), TIPC_CLUSTER(prev_node), TIPC_NODE(prev_node),
303 	    TIPC_ZONE(dest_domain), TIPC_CLUSTER(dest_domain), TIPC_NODE(dest_domain),
304 	    hsize*4, msize,
305 	    tok2str(tipcuser_values, "unknown", user),
306 	    tok2str(tipc_linkconf_mtype_values, "Unknown", mtype));
307 	if (ndo->ndo_vflag) {
308 		node_sig = TIPC_NODE_SIG(w1);
309 		ntwrk_id = GET_BE_U_4(ap->ntwrk_id);
310 		w5 = GET_BE_U_4(ap->w5);
311 		media_id = TIPC_MEDIA_ID(w5);
312 		ND_PRINT("\n\tNodeSignature %u, network_id %u, media_id %u",
313 		    node_sig, ntwrk_id, media_id);
314 	}
315 }
316 
317 void
318 tipc_print(netdissect_options *ndo, const u_char *bp, u_int length _U_,
319     u_int caplen _U_)
320 {
321 	const struct tipc_pkthdr *ap;
322 	uint32_t w0;
323 	u_int user;
324 
325 	ndo->ndo_protocol = "tipc";
326 	ap = (const struct tipc_pkthdr *)bp;
327 	w0 = GET_BE_U_4(ap->w0);
328 	user = TIPC_USER(w0);
329 
330 	switch (user)
331 	{
332 		case TIPC_USER_LOW_IMPORTANCE:
333 		case TIPC_USER_MEDIUM_IMPORTANCE:
334 		case TIPC_USER_HIGH_IMPORTANCE:
335 		case TIPC_USER_CRITICAL_IMPORTANCE:
336 		case TIPC_USER_NAME_DISTRIBUTOR:
337 		case TIPC_USER_CONN_MANAGER:
338 			print_payload(ndo, (const struct payload_tipc_pkthdr *)bp);
339 			break;
340 
341 		case TIPC_USER_LINK_CONFIG:
342 			print_link_conf(ndo, (const struct link_conf_tipc_pkthdr *)bp);
343 			break;
344 
345 		case TIPC_USER_BCAST_PROTOCOL:
346 		case TIPC_USER_MSG_BUNDLER:
347 		case TIPC_USER_LINK_PROTOCOL:
348 		case TIPC_USER_CHANGEOVER_PROTOCOL:
349 		case TIPC_USER_MSG_FRAGMENTER:
350 			print_internal(ndo, (const struct internal_tipc_pkthdr *)bp);
351 			break;
352 
353 	}
354 }
355