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