xref: /netbsd-src/external/bsd/tcpdump/dist/print-tipc.c (revision 53b02e147d4ed531c0d2a5ca9b3e8026ba3e99b5)
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 #include <sys/cdefs.h>
23 #ifndef lint
24 __RCSID("$NetBSD: print-tipc.c,v 1.4 2020/02/24 18:39:47 kamil Exp $");
25 #endif
26 
27 /* \summary: Transparent Inter-Process Communication (TIPC) protocol printer */
28 
29 /*
30  * specification:
31  *	http://tipc.sourceforge.net/doc/draft-spec-tipc-07.html
32  *	http://tipc.sourceforge.net/doc/tipc_message_formats.html
33  */
34 
35 #ifdef HAVE_CONFIG_H
36 #include "config.h"
37 #endif
38 
39 #include <netdissect-stdinc.h>
40 
41 #include "netdissect.h"
42 #include "ether.h"
43 #include "ethertype.h"
44 #include "extract.h"
45 
46 static const char tstr[] = "[|TIPC]";
47 
48 #define TIPC_USER_LOW_IMPORTANCE	0
49 #define TIPC_USER_MEDIUM_IMPORTANCE	1
50 #define TIPC_USER_HIGH_IMPORTANCE	2
51 #define TIPC_USER_CRITICAL_IMPORTANCE	3
52 #define TIPC_USER_BCAST_PROTOCOL	5
53 #define TIPC_USER_MSG_BUNDLER		6
54 #define TIPC_USER_LINK_PROTOCOL		7
55 #define TIPC_USER_CONN_MANAGER		8
56 #define TIPC_USER_CHANGEOVER_PROTOCOL	10
57 #define TIPC_USER_NAME_DISTRIBUTOR	11
58 #define TIPC_USER_MSG_FRAGMENTER	12
59 #define TIPC_USER_LINK_CONFIG		13
60 
61 #define TIPC_CONN_MSG			0
62 #define TIPC_DIRECT_MSG			1
63 #define TIPC_NAMED_MSG			2
64 #define TIPC_MCAST_MSG			3
65 
66 #define TIPC_ZONE(addr)		(((addr) >> 24) & 0xFF)
67 #define TIPC_CLUSTER(addr)	(((addr) >> 12) & 0xFFF)
68 #define TIPC_NODE(addr)		(((addr) >> 0) & 0xFFF)
69 
70 struct tipc_pkthdr {
71 	uint32_t w0;
72 	uint32_t w1;
73 };
74 
75 #define TIPC_VER(w0)		(((w0) >> 29) & 0x07)
76 #define TIPC_USER(w0)		(((w0) >> 25) & 0x0F)
77 #define TIPC_HSIZE(w0)		(((w0) >> 21) & 0x0F)
78 #define TIPC_MSIZE(w0)		(((w0) >> 0) & 0xFFFF)
79 #define TIPC_MTYPE(w1)		(((w1) >> 29) & 0x07)
80 #define TIPC_BROADCAST_ACK(w1)	(((w1) >> 0) & 0xFFFF)
81 #define TIPC_LINK_ACK(w2)	(((w2) >> 16) & 0xFFFF)
82 #define TIPC_LINK_SEQ(w2)	(((w2) >> 0) & 0xFFFF)
83 
84 static const struct tok tipcuser_values[] = {
85     { TIPC_USER_LOW_IMPORTANCE,      "Low Importance Data payload" },
86     { TIPC_USER_MEDIUM_IMPORTANCE,   "Medium Importance Data payload" },
87     { TIPC_USER_HIGH_IMPORTANCE,     "High Importance Data payload" },
88     { TIPC_USER_CRITICAL_IMPORTANCE, "Critical Importance Data payload" },
89     { TIPC_USER_BCAST_PROTOCOL,      "Broadcast Link Protocol internal" },
90     { TIPC_USER_MSG_BUNDLER,         "Message Bundler Protocol internal" },
91     { TIPC_USER_LINK_PROTOCOL,       "Link State Protocol internal" },
92     { TIPC_USER_CONN_MANAGER,        "Connection Manager internal" },
93     { TIPC_USER_CHANGEOVER_PROTOCOL, "Link Changeover Protocol internal" },
94     { TIPC_USER_NAME_DISTRIBUTOR,    "Name Table Update Protocol internal" },
95     { TIPC_USER_MSG_FRAGMENTER,      "Message Fragmentation Protocol internal" },
96     { TIPC_USER_LINK_CONFIG,         "Neighbor Detection Protocol internal" },
97     { 0, NULL }
98 };
99 
100 static const struct tok tipcmtype_values[] = {
101     { TIPC_CONN_MSG,   "CONN_MSG" },
102     { TIPC_DIRECT_MSG, "MCAST_MSG" },
103     { TIPC_NAMED_MSG,  "NAMED_MSG" },
104     { TIPC_MCAST_MSG,  "DIRECT_MSG" },
105     { 0, NULL }
106 };
107 
108 static const struct tok tipc_linkconf_mtype_values[] = {
109     { 0,   "Link request" },
110     { 1,   "Link response" },
111     { 0, NULL }
112 };
113 
114 struct payload_tipc_pkthdr {
115 	uint32_t w0;
116 	uint32_t w1;
117 	uint32_t w2;
118 	uint32_t prev_node;
119 	uint32_t orig_port;
120 	uint32_t dest_port;
121 	uint32_t orig_node;
122 	uint32_t dest_node;
123 	uint32_t name_type;
124 	uint32_t w9;
125 	uint32_t wA;
126 };
127 
128 struct  internal_tipc_pkthdr {
129 	uint32_t w0;
130 	uint32_t w1;
131 	uint32_t w2;
132 	uint32_t prev_node;
133 	uint32_t w4;
134 	uint32_t w5;
135 	uint32_t orig_node;
136 	uint32_t dest_node;
137 	uint32_t trans_seq;
138 	uint32_t w9;
139 };
140 
141 #define TIPC_SEQ_GAP(w1)	(((w1) >> 16) & 0x1FFF)
142 #define TIPC_BC_GAP_AFTER(w2)	(((w2) >> 16) & 0xFFFF)
143 #define TIPC_BC_GAP_TO(w2)	(((w2) >> 0) & 0xFFFF)
144 #define TIPC_LAST_SENT_FRAG(w4)	(((w4) >> 16) & 0xFFFF)
145 #define TIPC_NEXT_SENT_FRAG(w4)	(((w4) >> 0) & 0xFFFF)
146 #define TIPC_SESS_NO(w5)	(((w5) >> 16) & 0xFFFF)
147 #define TIPC_MSG_CNT(w9)	(((w9) >> 16) & 0xFFFF)
148 #define TIPC_LINK_TOL(w9)	(((w9) >> 0) & 0xFFFF)
149 
150 struct link_conf_tipc_pkthdr {
151 	uint32_t w0;
152 	uint32_t w1;
153 	uint32_t dest_domain;
154 	uint32_t prev_node;
155 	uint32_t ntwrk_id;
156 	uint32_t w5;
157 	uint8_t media_address[16];
158 };
159 
160 #define TIPC_NODE_SIG(w1)	(((w1) >> 0) & 0xFFFF)
161 #define TIPC_MEDIA_ID(w5)	(((w5) >> 0) & 0xFF)
162 
163 static void
164 print_payload(netdissect_options *ndo, const struct payload_tipc_pkthdr *ap)
165 {
166 	uint32_t w0, w1, w2;
167 	u_int user;
168 	u_int hsize;
169 	u_int msize;
170 	u_int mtype;
171 	u_int broadcast_ack;
172 	u_int link_ack;
173 	u_int link_seq;
174 	u_int prev_node;
175 	u_int orig_port;
176 	u_int dest_port;
177 	u_int orig_node;
178 	u_int dest_node;
179 
180 	ND_TCHECK(ap->dest_port);
181 	w0 = EXTRACT_32BITS(&ap->w0);
182 	user = TIPC_USER(w0);
183 	hsize = TIPC_HSIZE(w0);
184 	msize = TIPC_MSIZE(w0);
185 	w1 = EXTRACT_32BITS(&ap->w1);
186 	mtype = TIPC_MTYPE(w1);
187 	prev_node = EXTRACT_32BITS(&ap->prev_node);
188 	orig_port = EXTRACT_32BITS(&ap->orig_port);
189 	dest_port = EXTRACT_32BITS(&ap->dest_port);
190 	if (hsize <= 6) {
191 		ND_PRINT((ndo, "TIPC v%u.0 %u.%u.%u:%u > %u, headerlength %u bytes, MessageSize %u bytes, %s, messageType %s",
192 		    TIPC_VER(w0),
193 		    TIPC_ZONE(prev_node), TIPC_CLUSTER(prev_node), TIPC_NODE(prev_node),
194 		    orig_port, dest_port,
195 		    hsize*4, msize,
196 		    tok2str(tipcuser_values, "unknown", user),
197 		    tok2str(tipcmtype_values, "Unknown", mtype)));
198 	} else {
199 		ND_TCHECK(ap->dest_node);
200 		orig_node = EXTRACT_32BITS(&ap->orig_node);
201 		dest_node = EXTRACT_32BITS(&ap->dest_node);
202 		ND_PRINT((ndo, "TIPC v%u.0 %u.%u.%u:%u > %u.%u.%u:%u, headerlength %u bytes, MessageSize %u bytes, %s, messageType %s",
203 		    TIPC_VER(w0),
204 		    TIPC_ZONE(orig_node), TIPC_CLUSTER(orig_node), TIPC_NODE(orig_node),
205 		    orig_port,
206 		    TIPC_ZONE(dest_node), TIPC_CLUSTER(dest_node), TIPC_NODE(dest_node),
207 		    dest_port,
208 		    hsize*4, msize,
209 		    tok2str(tipcuser_values, "unknown", user),
210 		    tok2str(tipcmtype_values, "Unknown", mtype)));
211 
212 		if (ndo->ndo_vflag) {
213 			broadcast_ack = TIPC_BROADCAST_ACK(w1);
214 			w2 = EXTRACT_32BITS(&ap->w2);
215 			link_ack = TIPC_LINK_ACK(w2);
216 			link_seq = TIPC_LINK_SEQ(w2);
217 			ND_PRINT((ndo, "\n\tPrevious Node %u.%u.%u, Broadcast Ack %u, Link Ack %u, Link Sequence %u",
218 			    TIPC_ZONE(prev_node), TIPC_CLUSTER(prev_node), TIPC_NODE(prev_node),
219 			    broadcast_ack, link_ack, link_seq));
220 		}
221 	}
222 	return;
223 
224 trunc:
225 	ND_PRINT((ndo, "%s", tstr));
226 }
227 
228 UNALIGNED_OK
229 static void
230 print_internal(netdissect_options *ndo, const struct internal_tipc_pkthdr *ap)
231 {
232 	uint32_t w0, w1, w2, w4, w5, w9;
233 	u_int user;
234 	u_int hsize;
235 	u_int msize;
236 	u_int mtype;
237 	u_int seq_gap;
238 	u_int broadcast_ack;
239 	u_int bc_gap_after;
240 	u_int bc_gap_to;
241 	u_int prev_node;
242 	u_int last_sent_frag;
243 	u_int next_sent_frag;
244 	u_int sess_no;
245 	u_int orig_node;
246 	u_int dest_node;
247 	u_int trans_seq;
248 	u_int msg_cnt;
249 	u_int link_tol;
250 
251 	ND_TCHECK(ap->dest_node);
252 	w0 = EXTRACT_32BITS(&ap->w0);
253 	user = TIPC_USER(w0);
254 	hsize = TIPC_HSIZE(w0);
255 	msize = TIPC_MSIZE(w0);
256 	w1 = EXTRACT_32BITS(&ap->w1);
257 	mtype = TIPC_MTYPE(w1);
258 	orig_node = EXTRACT_32BITS(&ap->orig_node);
259 	dest_node = EXTRACT_32BITS(&ap->dest_node);
260 	ND_PRINT((ndo, "TIPC v%u.0 %u.%u.%u > %u.%u.%u, headerlength %u bytes, MessageSize %u bytes, %s, messageType %s (0x%08x)",
261 	    TIPC_VER(w0),
262 	    TIPC_ZONE(orig_node), TIPC_CLUSTER(orig_node), TIPC_NODE(orig_node),
263 	    TIPC_ZONE(dest_node), TIPC_CLUSTER(dest_node), TIPC_NODE(dest_node),
264 	    hsize*4, msize,
265 	    tok2str(tipcuser_values, "unknown", user),
266 	    tok2str(tipcmtype_values, "Unknown", mtype), w1));
267 
268 	if (ndo->ndo_vflag) {
269 		ND_TCHECK(*ap);
270 		seq_gap = TIPC_SEQ_GAP(w1);
271 		broadcast_ack = TIPC_BROADCAST_ACK(w1);
272 		w2 = EXTRACT_32BITS(&ap->w2);
273 		bc_gap_after = TIPC_BC_GAP_AFTER(w2);
274 		bc_gap_to = TIPC_BC_GAP_TO(w2);
275 		prev_node = EXTRACT_32BITS(&ap->prev_node);
276 		w4 = EXTRACT_32BITS(&ap->w4);
277 		last_sent_frag = TIPC_LAST_SENT_FRAG(w4);
278 		next_sent_frag = TIPC_NEXT_SENT_FRAG(w4);
279 		w5 = EXTRACT_32BITS(&ap->w5);
280 		sess_no = TIPC_SESS_NO(w5);
281 		trans_seq = EXTRACT_32BITS(&ap->trans_seq);
282 		w9 = EXTRACT_32BITS(&ap->w9);
283 		msg_cnt = TIPC_MSG_CNT(w9);
284 		link_tol = TIPC_LINK_TOL(w9);
285 		ND_PRINT((ndo, "\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",
286 		    TIPC_ZONE(prev_node), TIPC_CLUSTER(prev_node), TIPC_NODE(prev_node),
287 		    sess_no, broadcast_ack, seq_gap, bc_gap_after, bc_gap_to,
288 		    last_sent_frag, next_sent_frag, trans_seq, msg_cnt,
289 		    link_tol));
290 	}
291 	return;
292 
293 trunc:
294 	ND_PRINT((ndo, "%s", tstr));
295 }
296 
297 static void
298 print_link_conf(netdissect_options *ndo, const struct link_conf_tipc_pkthdr *ap)
299 {
300 	uint32_t w0, w1, w5;
301 	u_int user;
302 	u_int hsize;
303 	u_int msize;
304 	u_int mtype;
305 	u_int node_sig;
306 	u_int prev_node;
307 	u_int dest_domain;
308 	u_int ntwrk_id;
309 	u_int media_id;
310 
311 	ND_TCHECK(ap->prev_node);
312 	w0 = EXTRACT_32BITS(&ap->w0);
313 	user = TIPC_USER(w0);
314 	hsize = TIPC_HSIZE(w0);
315 	msize = TIPC_MSIZE(w0);
316 	w1 = EXTRACT_32BITS(&ap->w1);
317 	mtype = TIPC_MTYPE(w1);
318 	dest_domain = EXTRACT_32BITS(&ap->dest_domain);
319 	prev_node = EXTRACT_32BITS(&ap->prev_node);
320 
321 	ND_PRINT((ndo, "TIPC v%u.0 %u.%u.%u > %u.%u.%u, headerlength %u bytes, MessageSize %u bytes, %s, messageType %s",
322 	    TIPC_VER(w0),
323 	    TIPC_ZONE(prev_node), TIPC_CLUSTER(prev_node), TIPC_NODE(prev_node),
324 	    TIPC_ZONE(dest_domain), TIPC_CLUSTER(dest_domain), TIPC_NODE(dest_domain),
325 	    hsize*4, msize,
326 	    tok2str(tipcuser_values, "unknown", user),
327 	    tok2str(tipc_linkconf_mtype_values, "Unknown", mtype)));
328 	if (ndo->ndo_vflag) {
329 		ND_TCHECK(ap->w5);
330 		node_sig = TIPC_NODE_SIG(w1);
331 		ntwrk_id = EXTRACT_32BITS(&ap->ntwrk_id);
332 		w5 = EXTRACT_32BITS(&ap->w5);
333 		media_id = TIPC_MEDIA_ID(w5);
334 		ND_PRINT((ndo, "\n\tNodeSignature %u, network_id %u, media_id %u",
335 		    node_sig, ntwrk_id, media_id));
336 	}
337 	return;
338 
339 trunc:
340 	ND_PRINT((ndo, "%s", tstr));
341 }
342 
343 UNALIGNED_OK
344 void
345 tipc_print(netdissect_options *ndo, const u_char *bp, u_int length _U_,
346     u_int caplen _U_)
347 {
348 	const struct tipc_pkthdr *ap;
349 	uint32_t w0;
350 	u_int user;
351 
352 	ap = (const struct tipc_pkthdr *)bp;
353 	ND_TCHECK(ap->w0);
354 	w0 = EXTRACT_32BITS(&ap->w0);
355 	user = TIPC_USER(w0);
356 
357 	switch (user)
358 	{
359 		case TIPC_USER_LOW_IMPORTANCE:
360 		case TIPC_USER_MEDIUM_IMPORTANCE:
361 		case TIPC_USER_HIGH_IMPORTANCE:
362 		case TIPC_USER_CRITICAL_IMPORTANCE:
363 		case TIPC_USER_NAME_DISTRIBUTOR:
364 		case TIPC_USER_CONN_MANAGER:
365 			print_payload(ndo, (const struct payload_tipc_pkthdr *)bp);
366 			break;
367 
368 		case TIPC_USER_LINK_CONFIG:
369 			print_link_conf(ndo, (const struct link_conf_tipc_pkthdr *)bp);
370 			break;
371 
372 		case TIPC_USER_BCAST_PROTOCOL:
373 		case TIPC_USER_MSG_BUNDLER:
374 		case TIPC_USER_LINK_PROTOCOL:
375 		case TIPC_USER_CHANGEOVER_PROTOCOL:
376 		case TIPC_USER_MSG_FRAGMENTER:
377 			print_internal(ndo, (const struct internal_tipc_pkthdr *)bp);
378 			break;
379 
380 	}
381 	return;
382 
383 trunc:
384 	ND_PRINT((ndo, "%s", tstr));
385 }
386 
387 /*
388  * Local Variables:
389  * c-style: bsd
390  * End:
391  */
392