xref: /netbsd-src/external/bsd/tcpdump/dist/print-atm.c (revision 3117ece4fc4a4ca4489ba793710b60b0d26bab6c)
1 /*
2  * Copyright (c) 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-atm.c,v 1.11 2024/09/02 16:15:30 christos Exp $");
25 #endif
26 
27 /* \summary: Asynchronous Transfer Mode (ATM) printer */
28 
29 #include <config.h>
30 
31 #include "netdissect-stdinc.h"
32 
33 #define ND_LONGJMP_FROM_TCHECK
34 #include "netdissect.h"
35 #include "extract.h"
36 #include "addrtoname.h"
37 #include "atm.h"
38 #include "llc.h"
39 
40 /* start of the original atmuni31.h */
41 
42 /*
43  * Copyright (c) 1997 Yen Yen Lim and North Dakota State University
44  * All rights reserved.
45  *
46  * Redistribution and use in source and binary forms, with or without
47  * modification, are permitted provided that the following conditions
48  * are met:
49  * 1. Redistributions of source code must retain the above copyright
50  *    notice, this list of conditions and the following disclaimer.
51  * 2. Redistributions in binary form must reproduce the above copyright
52  *    notice, this list of conditions and the following disclaimer in the
53  *    documentation and/or other materials provided with the distribution.
54  * 3. All advertising materials mentioning features or use of this software
55  *    must display the following acknowledgement:
56  *      This product includes software developed by Yen Yen Lim and
57         North Dakota State University
58  * 4. The name of the author may not be used to endorse or promote products
59  *    derived from this software without specific prior written permission.
60  *
61  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
62  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
63  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
64  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
65  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
66  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
67  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
68  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
69  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
70  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
71  * POSSIBILITY OF SUCH DAMAGE.
72  */
73 
74 /* Based on UNI3.1 standard by ATM Forum */
75 
76 /* ATM traffic types based on VPI=0 and (the following VCI */
77 #define VCI_PPC			0x05	/* Point-to-point signal msg */
78 #define VCI_BCC			0x02	/* Broadcast signal msg */
79 #define VCI_OAMF4SC		0x03	/* Segment OAM F4 flow cell */
80 #define VCI_OAMF4EC		0x04	/* End-to-end OAM F4 flow cell */
81 #define VCI_METAC		0x01	/* Meta signal msg */
82 #define VCI_ILMIC		0x10	/* ILMI msg */
83 
84 /* Q.2931 signalling messages */
85 #define CALL_PROCEED		0x02	/* call proceeding */
86 #define CONNECT			0x07	/* connect */
87 #define CONNECT_ACK		0x0f	/* connect_ack */
88 #define SETUP			0x05	/* setup */
89 #define RELEASE			0x4d	/* release */
90 #define RELEASE_DONE		0x5a	/* release_done */
91 #define RESTART			0x46	/* restart */
92 #define RESTART_ACK		0x4e	/* restart ack */
93 #define STATUS			0x7d	/* status */
94 #define STATUS_ENQ		0x75	/* status ack */
95 #define ADD_PARTY		0x80	/* add party */
96 #define ADD_PARTY_ACK		0x81	/* add party ack */
97 #define ADD_PARTY_REJ		0x82	/* add party rej */
98 #define DROP_PARTY		0x83	/* drop party */
99 #define DROP_PARTY_ACK		0x84	/* drop party ack */
100 
101 /* Information Element Parameters in the signalling messages */
102 #define CAUSE			0x08	/* cause */
103 #define ENDPT_REF		0x54	/* endpoint reference */
104 #define AAL_PARA		0x58	/* ATM adaptation layer parameters */
105 #define TRAFF_DESCRIP		0x59	/* atm traffic descriptors */
106 #define CONNECT_ID		0x5a	/* connection identifier */
107 #define QOS_PARA		0x5c	/* quality of service parameters */
108 #define B_HIGHER		0x5d	/* broadband higher layer information */
109 #define B_BEARER		0x5e	/* broadband bearer capability */
110 #define B_LOWER			0x5f	/* broadband lower information */
111 #define CALLING_PARTY		0x6c	/* calling party number */
112 #define CALLED_PARTY		0x70	/* called party number */
113 
114 #define Q2931			0x09
115 
116 /* Q.2931 signalling general messages format */
117 #define PROTO_POS       0	/* offset of protocol discriminator */
118 #define CALL_REF_POS    2	/* offset of call reference value */
119 #define MSG_TYPE_POS    5	/* offset of message type */
120 #if 0
121 #define MSG_LEN_POS     7	/* offset of message length */
122 #define IE_BEGIN_POS    9	/* offset of first information element */
123 
124 /* format of signalling messages */
125 #define TYPE_POS	0
126 #define LEN_POS		2
127 #define FIELD_BEGIN_POS 4
128 #endif
129 
130 /* end of the original atmuni31.h */
131 
132 
133 #define OAM_CRC10_MASK 0x3ff
134 #define OAM_PAYLOAD_LEN 48
135 #define OAM_FUNCTION_SPECIFIC_LEN 45 /* this excludes crc10 and cell-type/function-type */
136 #define OAM_CELLTYPE_FUNCTYPE_LEN 1
137 
138 static const struct tok oam_f_values[] = {
139     { VCI_OAMF4SC, "OAM F4 (segment)" },
140     { VCI_OAMF4EC, "OAM F4 (end)" },
141     { 0, NULL }
142 };
143 
144 static const struct tok atm_pty_values[] = {
145     { 0x0, "user data, uncongested, SDU 0" },
146     { 0x1, "user data, uncongested, SDU 1" },
147     { 0x2, "user data, congested, SDU 0" },
148     { 0x3, "user data, congested, SDU 1" },
149     { 0x4, "VCC OAM F5 flow segment" },
150     { 0x5, "VCC OAM F5 flow end-to-end" },
151     { 0x6, "Traffic Control and resource Mgmt" },
152     { 0, NULL }
153 };
154 
155 #define OAM_CELLTYPE_FM 0x1
156 #define OAM_CELLTYPE_PM 0x2
157 #define OAM_CELLTYPE_AD 0x8
158 #define OAM_CELLTYPE_SM 0xf
159 
160 static const struct tok oam_celltype_values[] = {
161     { OAM_CELLTYPE_FM, "Fault Management" },
162     { OAM_CELLTYPE_PM, "Performance Management" },
163     { OAM_CELLTYPE_AD, "activate/deactivate" },
164     { OAM_CELLTYPE_SM, "System Management" },
165     { 0, NULL }
166 };
167 
168 #define OAM_FM_FUNCTYPE_AIS       0x0
169 #define OAM_FM_FUNCTYPE_RDI       0x1
170 #define OAM_FM_FUNCTYPE_CONTCHECK 0x4
171 #define OAM_FM_FUNCTYPE_LOOPBACK  0x8
172 
173 static const struct tok oam_fm_functype_values[] = {
174     { OAM_FM_FUNCTYPE_AIS, "AIS" },
175     { OAM_FM_FUNCTYPE_RDI, "RDI" },
176     { OAM_FM_FUNCTYPE_CONTCHECK, "Continuity Check" },
177     { OAM_FM_FUNCTYPE_LOOPBACK, "Loopback" },
178     { 0, NULL }
179 };
180 
181 static const struct tok oam_pm_functype_values[] = {
182     { 0x0, "Forward Monitoring" },
183     { 0x1, "Backward Reporting" },
184     { 0x2, "Monitoring and Reporting" },
185     { 0, NULL }
186 };
187 
188 static const struct tok oam_ad_functype_values[] = {
189     { 0x0, "Performance Monitoring" },
190     { 0x1, "Continuity Check" },
191     { 0, NULL }
192 };
193 
194 #define OAM_FM_LOOPBACK_INDICATOR_MASK 0x1
195 
196 static const struct tok oam_fm_loopback_indicator_values[] = {
197     { 0x0, "Reply" },
198     { 0x1, "Request" },
199     { 0, NULL }
200 };
201 
202 static const struct uint_tokary oam_celltype2tokary[] = {
203 	{ OAM_CELLTYPE_FM, oam_fm_functype_values },
204 	{ OAM_CELLTYPE_PM, oam_pm_functype_values },
205 	{ OAM_CELLTYPE_AD, oam_ad_functype_values },
206 	/* uint2tokary() does not use array termination. */
207 };
208 
209 /*
210  * Print an RFC 1483 LLC-encapsulated ATM frame.
211  */
212 static u_int
213 atm_llc_print(netdissect_options *ndo,
214               const u_char *p, int length, int caplen)
215 {
216 	int llc_hdrlen;
217 
218 	llc_hdrlen = llc_print(ndo, p, length, caplen, NULL, NULL);
219 	if (llc_hdrlen < 0) {
220 		/* packet not known, print raw packet */
221 		if (!ndo->ndo_suppress_default_print)
222 			ND_DEFAULTPRINT(p, caplen);
223 		llc_hdrlen = -llc_hdrlen;
224 	}
225 	return (llc_hdrlen);
226 }
227 
228 /*
229  * Given a SAP value, generate the LLC header value for a UI packet
230  * with that SAP as the source and destination SAP.
231  */
232 #define LLC_UI_HDR(sap)	((sap)<<16 | (sap<<8) | 0x03)
233 
234 /*
235  * This is the top level routine of the printer.  'p' points
236  * to the LLC/SNAP header of the packet, 'h->ts' is the timestamp,
237  * 'h->len' is the length of the packet off the wire, and 'h->caplen'
238  * is the number of bytes actually captured.
239  */
240 void
241 atm_if_print(netdissect_options *ndo,
242              const struct pcap_pkthdr *h, const u_char *p)
243 {
244 	u_int caplen = h->caplen;
245 	u_int length = h->len;
246 	uint32_t llchdr;
247 	u_int hdrlen = 0;
248 
249 	ndo->ndo_protocol = "atm";
250 
251         /* Cisco Style NLPID ? */
252         if (GET_U_1(p) == LLC_UI) {
253             if (ndo->ndo_eflag)
254                 ND_PRINT("CNLPID ");
255             ndo->ndo_ll_hdr_len += 1;
256             isoclns_print(ndo, p + 1, length - 1);
257             return;
258         }
259 
260 	/*
261 	 * Must have at least a DSAP, an SSAP, and the first byte of the
262 	 * control field.
263 	 */
264 
265 	/*
266 	 * Extract the presumed LLC header into a variable, for quick
267 	 * testing.
268 	 * Then check for a header that's neither a header for a SNAP
269 	 * packet nor an RFC 2684 routed NLPID-formatted PDU nor
270 	 * an 802.2-but-no-SNAP IP packet.
271 	 */
272 	llchdr = GET_BE_U_3(p);
273 	if (llchdr != LLC_UI_HDR(LLCSAP_SNAP) &&
274 	    llchdr != LLC_UI_HDR(LLCSAP_ISONS) &&
275 	    llchdr != LLC_UI_HDR(LLCSAP_IP)) {
276 		/*
277 		 * XXX - assume 802.6 MAC header from Fore driver.
278 		 *
279 		 * Unfortunately, the above list doesn't check for
280 		 * all known SAPs, doesn't check for headers where
281 		 * the source and destination SAP aren't the same,
282 		 * and doesn't check for non-UI frames.  It also
283 		 * runs the risk of an 802.6 MAC header that happens
284 		 * to begin with one of those values being
285 		 * incorrectly treated as an 802.2 header.
286 		 *
287 		 * So is that Fore driver still around?  And, if so,
288 		 * is it still putting 802.6 MAC headers on ATM
289 		 * packets?  If so, could it be changed to use a
290 		 * new DLT_IEEE802_6 value if we added it?
291 		 */
292 		if (ndo->ndo_eflag)
293 			ND_PRINT("%08x%08x %08x%08x ",
294 			       GET_BE_U_4(p),
295 			       GET_BE_U_4(p + 4),
296 			       GET_BE_U_4(p + 8),
297 			       GET_BE_U_4(p + 12));
298 		/* Always cover the full header. */
299 		ND_TCHECK_LEN(p, 20);
300 		p += 20;
301 		length -= 20;
302 		caplen -= 20;
303 		hdrlen += 20;
304 	}
305 	ndo->ndo_ll_hdr_len += hdrlen;
306 	ndo->ndo_ll_hdr_len += atm_llc_print(ndo, p, length, caplen);
307 }
308 
309 /*
310  * ATM signalling.
311  */
312 static const struct tok msgtype2str[] = {
313 	{ CALL_PROCEED,		"Call_proceeding" },
314 	{ CONNECT,		"Connect" },
315 	{ CONNECT_ACK,		"Connect_ack" },
316 	{ SETUP,		"Setup" },
317 	{ RELEASE,		"Release" },
318 	{ RELEASE_DONE,		"Release_complete" },
319 	{ RESTART,		"Restart" },
320 	{ RESTART_ACK,		"Restart_ack" },
321 	{ STATUS,		"Status" },
322 	{ STATUS_ENQ,		"Status_enquiry" },
323 	{ ADD_PARTY,		"Add_party" },
324 	{ ADD_PARTY_ACK,	"Add_party_ack" },
325 	{ ADD_PARTY_REJ,	"Add_party_reject" },
326 	{ DROP_PARTY,		"Drop_party" },
327 	{ DROP_PARTY_ACK,	"Drop_party_ack" },
328 	{ 0,			NULL }
329 };
330 
331 static void
332 sig_print(netdissect_options *ndo,
333           const u_char *p)
334 {
335 	uint32_t call_ref;
336 
337 	if (GET_U_1(p + PROTO_POS) == Q2931) {
338 		/*
339 		 * protocol:Q.2931 for User to Network Interface
340 		 * (UNI 3.1) signalling
341 		 */
342 		ND_PRINT("Q.2931");
343 		ND_PRINT(":%s ",
344 		    tok2str(msgtype2str, "msgtype#%u", GET_U_1(p + MSG_TYPE_POS)));
345 
346 		/*
347 		 * The call reference comes before the message type,
348 		 * so if we know we have the message type, which we
349 		 * do from the caplen test above, we also know we have
350 		 * the call reference.
351 		 */
352 		call_ref = GET_BE_U_3(p + CALL_REF_POS);
353 		ND_PRINT("CALL_REF:0x%06x", call_ref);
354 	} else {
355 		/* SSCOP with some unknown protocol atop it */
356 		ND_PRINT("SSCOP, proto %u ", GET_U_1(p + PROTO_POS));
357 	}
358 }
359 
360 /*
361  * Print an ATM PDU (such as an AAL5 PDU).
362  */
363 void
364 atm_print(netdissect_options *ndo,
365           u_int vpi, u_int vci, u_int traftype, const u_char *p, u_int length,
366           u_int caplen)
367 {
368 	ndo->ndo_protocol = "atm";
369 	if (ndo->ndo_eflag)
370 		ND_PRINT("VPI:%u VCI:%u ", vpi, vci);
371 
372 	if (vpi == 0) {
373 		switch (vci) {
374 
375 		case VCI_PPC:
376 			sig_print(ndo, p);
377 			return;
378 
379 		case VCI_BCC:
380 			ND_PRINT("broadcast sig: ");
381 			return;
382 
383 		case VCI_OAMF4SC: /* fall through */
384 		case VCI_OAMF4EC:
385 			oam_print(ndo, p, length, ATM_OAM_HEC);
386 			return;
387 
388 		case VCI_METAC:
389 			ND_PRINT("meta: ");
390 			return;
391 
392 		case VCI_ILMIC:
393 			ND_PRINT("ilmi: ");
394 			snmp_print(ndo, p, length);
395 			return;
396 		}
397 	}
398 
399 	switch (traftype) {
400 
401 	case ATM_LLC:
402 	default:
403 		/*
404 		 * Assumes traffic is LLC if unknown.
405 		 */
406 		atm_llc_print(ndo, p, length, caplen);
407 		break;
408 
409 	case ATM_LANE:
410 		lane_print(ndo, p, length, caplen);
411 		break;
412 	}
413 }
414 
415 struct oam_fm_loopback_t {
416     nd_uint8_t  loopback_indicator;
417     nd_uint32_t correlation_tag;
418     nd_byte     loopback_id[12];
419     nd_byte     source_id[12];
420     nd_byte     unused[16];
421 };
422 
423 struct oam_fm_ais_rdi_t {
424     nd_uint8_t  failure_type;
425     nd_byte     failure_location[16];
426     nd_byte     unused[28];
427 };
428 
429 void
430 oam_print(netdissect_options *ndo,
431           const u_char *p, u_int length, u_int hec)
432 {
433     uint32_t cell_header;
434     uint16_t vpi, vci, cksum, cksum_shouldbe, idx;
435     uint8_t  cell_type, func_type, payload, clp;
436     const struct tok *oam_functype_str;
437 
438     union {
439         const struct oam_fm_loopback_t *oam_fm_loopback;
440         const struct oam_fm_ais_rdi_t *oam_fm_ais_rdi;
441     } oam_ptr;
442 
443     ndo->ndo_protocol = "oam";
444     cell_header = GET_BE_U_4(p + hec);
445     cell_type = (GET_U_1((p + ATM_HDR_LEN_NOHEC + hec)) >> 4) & 0x0f;
446     func_type = GET_U_1((p + ATM_HDR_LEN_NOHEC + hec)) & 0x0f;
447 
448     vpi = (cell_header>>20)&0xff;
449     vci = (cell_header>>4)&0xffff;
450     payload = (cell_header>>1)&0x7;
451     clp = cell_header&0x1;
452 
453     ND_PRINT("%s, vpi %u, vci %u, payload [ %s ], clp %u, length %u",
454            tok2str(oam_f_values, "OAM F5", vci),
455            vpi, vci,
456            tok2str(atm_pty_values, "Unknown", payload),
457            clp, length);
458 
459     if (!ndo->ndo_vflag) {
460         return;
461     }
462 
463     ND_PRINT("\n\tcell-type %s (%u)",
464            tok2str(oam_celltype_values, "unknown", cell_type),
465            cell_type);
466 
467     oam_functype_str = uint2tokary(oam_celltype2tokary, cell_type);
468     if (oam_functype_str == NULL)
469         ND_PRINT(", func-type unknown (%u)", func_type);
470     else
471         ND_PRINT(", func-type %s (%u)",
472                tok2str(oam_functype_str, "none", func_type),
473                func_type);
474 
475     p += ATM_HDR_LEN_NOHEC + hec;
476 
477     switch (cell_type << 4 | func_type) {
478     case (OAM_CELLTYPE_FM << 4 | OAM_FM_FUNCTYPE_LOOPBACK):
479         oam_ptr.oam_fm_loopback = (const struct oam_fm_loopback_t *)(p + OAM_CELLTYPE_FUNCTYPE_LEN);
480         ND_TCHECK_SIZE(oam_ptr.oam_fm_loopback);
481         ND_PRINT("\n\tLoopback-Indicator %s, Correlation-Tag 0x%08x",
482                tok2str(oam_fm_loopback_indicator_values,
483                        "Unknown",
484                        GET_U_1(oam_ptr.oam_fm_loopback->loopback_indicator) & OAM_FM_LOOPBACK_INDICATOR_MASK),
485                GET_BE_U_4(oam_ptr.oam_fm_loopback->correlation_tag));
486         ND_PRINT("\n\tLocation-ID ");
487         for (idx = 0; idx < sizeof(oam_ptr.oam_fm_loopback->loopback_id); idx++) {
488             if (idx % 2) {
489                 ND_PRINT("%04x ",
490                          GET_BE_U_2(&oam_ptr.oam_fm_loopback->loopback_id[idx]));
491             }
492         }
493         ND_PRINT("\n\tSource-ID   ");
494         for (idx = 0; idx < sizeof(oam_ptr.oam_fm_loopback->source_id); idx++) {
495             if (idx % 2) {
496                 ND_PRINT("%04x ",
497                          GET_BE_U_2(&oam_ptr.oam_fm_loopback->source_id[idx]));
498             }
499         }
500         break;
501 
502     case (OAM_CELLTYPE_FM << 4 | OAM_FM_FUNCTYPE_AIS):
503     case (OAM_CELLTYPE_FM << 4 | OAM_FM_FUNCTYPE_RDI):
504         oam_ptr.oam_fm_ais_rdi = (const struct oam_fm_ais_rdi_t *)(p + OAM_CELLTYPE_FUNCTYPE_LEN);
505         ND_TCHECK_SIZE(oam_ptr.oam_fm_ais_rdi);
506         ND_PRINT("\n\tFailure-type 0x%02x",
507                  GET_U_1(oam_ptr.oam_fm_ais_rdi->failure_type));
508         ND_PRINT("\n\tLocation-ID ");
509         for (idx = 0; idx < sizeof(oam_ptr.oam_fm_ais_rdi->failure_location); idx++) {
510             if (idx % 2) {
511                 ND_PRINT("%04x ",
512                          GET_BE_U_2(&oam_ptr.oam_fm_ais_rdi->failure_location[idx]));
513             }
514         }
515         break;
516 
517     case (OAM_CELLTYPE_FM << 4 | OAM_FM_FUNCTYPE_CONTCHECK):
518         /* FIXME */
519         break;
520 
521     default:
522         break;
523     }
524 
525     /* crc10 checksum verification */
526     cksum = GET_BE_U_2(p + OAM_CELLTYPE_FUNCTYPE_LEN + OAM_FUNCTION_SPECIFIC_LEN)
527         & OAM_CRC10_MASK;
528     cksum_shouldbe = verify_crc10_cksum(0, p, OAM_PAYLOAD_LEN);
529 
530     ND_PRINT("\n\tcksum 0x%03x (%scorrect)",
531            cksum,
532            cksum_shouldbe == 0 ? "" : "in");
533 }
534