xref: /freebsd/contrib/tcpdump/print-atm.c (revision db3cb3640f547c063293e9fdc4db69e9dc120951)
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  * $FreeBSD$
22  */
23 
24 #define NETDISSECT_REWORKED
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28 
29 #include <tcpdump-stdinc.h>
30 
31 #include "interface.h"
32 #include "extract.h"
33 #include "addrtoname.h"
34 #include "atm.h"
35 #include "atmuni31.h"
36 #include "llc.h"
37 
38 static const char tstr[] = "[|atm]";
39 
40 #define OAM_CRC10_MASK 0x3ff
41 #define OAM_PAYLOAD_LEN 48
42 #define OAM_FUNCTION_SPECIFIC_LEN 45 /* this excludes crc10 and cell-type/function-type */
43 #define OAM_CELLTYPE_FUNCTYPE_LEN 1
44 
45 static const struct tok oam_f_values[] = {
46     { VCI_OAMF4SC, "OAM F4 (segment)" },
47     { VCI_OAMF4EC, "OAM F4 (end)" },
48     { 0, NULL }
49 };
50 
51 static const struct tok atm_pty_values[] = {
52     { 0x0, "user data, uncongested, SDU 0" },
53     { 0x1, "user data, uncongested, SDU 1" },
54     { 0x2, "user data, congested, SDU 0" },
55     { 0x3, "user data, congested, SDU 1" },
56     { 0x4, "VCC OAM F5 flow segment" },
57     { 0x5, "VCC OAM F5 flow end-to-end" },
58     { 0x6, "Traffic Control and resource Mgmt" },
59     { 0, NULL }
60 };
61 
62 #define OAM_CELLTYPE_FM 0x1
63 #define OAM_CELLTYPE_PM 0x2
64 #define OAM_CELLTYPE_AD 0x8
65 #define OAM_CELLTYPE_SM 0xf
66 
67 static const struct tok oam_celltype_values[] = {
68     { OAM_CELLTYPE_FM, "Fault Management" },
69     { OAM_CELLTYPE_PM, "Performance Management" },
70     { OAM_CELLTYPE_AD, "activate/deactivate" },
71     { OAM_CELLTYPE_SM, "System Management" },
72     { 0, NULL }
73 };
74 
75 #define OAM_FM_FUNCTYPE_AIS       0x0
76 #define OAM_FM_FUNCTYPE_RDI       0x1
77 #define OAM_FM_FUNCTYPE_CONTCHECK 0x4
78 #define OAM_FM_FUNCTYPE_LOOPBACK  0x8
79 
80 static const struct tok oam_fm_functype_values[] = {
81     { OAM_FM_FUNCTYPE_AIS, "AIS" },
82     { OAM_FM_FUNCTYPE_RDI, "RDI" },
83     { OAM_FM_FUNCTYPE_CONTCHECK, "Continuity Check" },
84     { OAM_FM_FUNCTYPE_LOOPBACK, "Loopback" },
85     { 0, NULL }
86 };
87 
88 static const struct tok oam_pm_functype_values[] = {
89     { 0x0, "Forward Monitoring" },
90     { 0x1, "Backward Reporting" },
91     { 0x2, "Monitoring and Reporting" },
92     { 0, NULL }
93 };
94 
95 static const struct tok oam_ad_functype_values[] = {
96     { 0x0, "Performance Monitoring" },
97     { 0x1, "Continuity Check" },
98     { 0, NULL }
99 };
100 
101 #define OAM_FM_LOOPBACK_INDICATOR_MASK 0x1
102 
103 static const struct tok oam_fm_loopback_indicator_values[] = {
104     { 0x0, "Reply" },
105     { 0x1, "Request" },
106     { 0, NULL }
107 };
108 
109 static const struct tok *oam_functype_values[16] = {
110     NULL,
111     oam_fm_functype_values, /* 1 */
112     oam_pm_functype_values, /* 2 */
113     NULL,
114     NULL,
115     NULL,
116     NULL,
117     NULL,
118     oam_ad_functype_values, /* 8 */
119     NULL,
120     NULL,
121     NULL,
122     NULL,
123     NULL,
124     NULL,
125     NULL
126 };
127 
128 /*
129  * Print an RFC 1483 LLC-encapsulated ATM frame.
130  */
131 static void
132 atm_llc_print(netdissect_options *ndo,
133               const u_char *p, int length, int caplen)
134 {
135 	u_short extracted_ethertype;
136 
137 	if (!llc_print(ndo, p, length, caplen, NULL, NULL,
138 	    &extracted_ethertype)) {
139 		/* ether_type not known, print raw packet */
140 		if (extracted_ethertype) {
141 			ND_PRINT((ndo, "(LLC %s) ",
142 		etherproto_string(htons(extracted_ethertype))));
143 		}
144 		if (!ndo->ndo_suppress_default_print)
145 			ND_DEFAULTPRINT(p, caplen);
146 	}
147 }
148 
149 /*
150  * Given a SAP value, generate the LLC header value for a UI packet
151  * with that SAP as the source and destination SAP.
152  */
153 #define LLC_UI_HDR(sap)	((sap)<<16 | (sap<<8) | 0x03)
154 
155 /*
156  * This is the top level routine of the printer.  'p' points
157  * to the LLC/SNAP header of the packet, 'h->ts' is the timestamp,
158  * 'h->len' is the length of the packet off the wire, and 'h->caplen'
159  * is the number of bytes actually captured.
160  */
161 u_int
162 atm_if_print(netdissect_options *ndo,
163              const struct pcap_pkthdr *h, const u_char *p)
164 {
165 	u_int caplen = h->caplen;
166 	u_int length = h->len;
167 	uint32_t llchdr;
168 	u_int hdrlen = 0;
169 
170 	if (caplen < 8) {
171 		ND_PRINT((ndo, "%s", tstr));
172 		return (caplen);
173 	}
174 
175         /* Cisco Style NLPID ? */
176         if (*p == LLC_UI) {
177             if (ndo->ndo_eflag)
178                 ND_PRINT((ndo, "CNLPID "));
179             isoclns_print(ndo, p + 1, length - 1, caplen - 1);
180             return hdrlen;
181         }
182 
183 	/*
184 	 * Extract the presumed LLC header into a variable, for quick
185 	 * testing.
186 	 * Then check for a header that's neither a header for a SNAP
187 	 * packet nor an RFC 2684 routed NLPID-formatted PDU nor
188 	 * an 802.2-but-no-SNAP IP packet.
189 	 */
190 	llchdr = EXTRACT_24BITS(p);
191 	if (llchdr != LLC_UI_HDR(LLCSAP_SNAP) &&
192 	    llchdr != LLC_UI_HDR(LLCSAP_ISONS) &&
193 	    llchdr != LLC_UI_HDR(LLCSAP_IP)) {
194 		/*
195 		 * XXX - assume 802.6 MAC header from Fore driver.
196 		 *
197 		 * Unfortunately, the above list doesn't check for
198 		 * all known SAPs, doesn't check for headers where
199 		 * the source and destination SAP aren't the same,
200 		 * and doesn't check for non-UI frames.  It also
201 		 * runs the risk of an 802.6 MAC header that happens
202 		 * to begin with one of those values being
203 		 * incorrectly treated as an 802.2 header.
204 		 *
205 		 * So is that Fore driver still around?  And, if so,
206 		 * is it still putting 802.6 MAC headers on ATM
207 		 * packets?  If so, could it be changed to use a
208 		 * new DLT_IEEE802_6 value if we added it?
209 		 */
210 		if (ndo->ndo_eflag)
211 			ND_PRINT((ndo, "%08x%08x %08x%08x ",
212 			       EXTRACT_32BITS(p),
213 			       EXTRACT_32BITS(p+4),
214 			       EXTRACT_32BITS(p+8),
215 			       EXTRACT_32BITS(p+12)));
216 		p += 20;
217 		length -= 20;
218 		caplen -= 20;
219 		hdrlen += 20;
220 	}
221 	atm_llc_print(ndo, p, length, caplen);
222 	return (hdrlen);
223 }
224 
225 /*
226  * ATM signalling.
227  */
228 static const struct tok msgtype2str[] = {
229 	{ CALL_PROCEED,		"Call_proceeding" },
230 	{ CONNECT,		"Connect" },
231 	{ CONNECT_ACK,		"Connect_ack" },
232 	{ SETUP,		"Setup" },
233 	{ RELEASE,		"Release" },
234 	{ RELEASE_DONE,		"Release_complete" },
235 	{ RESTART,		"Restart" },
236 	{ RESTART_ACK,		"Restart_ack" },
237 	{ STATUS,		"Status" },
238 	{ STATUS_ENQ,		"Status_enquiry" },
239 	{ ADD_PARTY,		"Add_party" },
240 	{ ADD_PARTY_ACK,	"Add_party_ack" },
241 	{ ADD_PARTY_REJ,	"Add_party_reject" },
242 	{ DROP_PARTY,		"Drop_party" },
243 	{ DROP_PARTY_ACK,	"Drop_party_ack" },
244 	{ 0,			NULL }
245 };
246 
247 static void
248 sig_print(netdissect_options *ndo,
249           const u_char *p, int caplen)
250 {
251 	uint32_t call_ref;
252 
253 	if (caplen < PROTO_POS) {
254 		ND_PRINT((ndo, "%s", tstr));
255 		return;
256 	}
257 	if (p[PROTO_POS] == Q2931) {
258 		/*
259 		 * protocol:Q.2931 for User to Network Interface
260 		 * (UNI 3.1) signalling
261 		 */
262 		ND_PRINT((ndo, "Q.2931"));
263 		if (caplen < MSG_TYPE_POS) {
264 			ND_PRINT((ndo, " %s", tstr));
265 			return;
266 		}
267 		ND_PRINT((ndo, ":%s ",
268 		    tok2str(msgtype2str, "msgtype#%d", p[MSG_TYPE_POS])));
269 
270 		/*
271 		 * The call reference comes before the message type,
272 		 * so if we know we have the message type, which we
273 		 * do from the caplen test above, we also know we have
274 		 * the call reference.
275 		 */
276 		call_ref = EXTRACT_24BITS(&p[CALL_REF_POS]);
277 		ND_PRINT((ndo, "CALL_REF:0x%06x", call_ref));
278 	} else {
279 		/* SCCOP with some unknown protocol atop it */
280 		ND_PRINT((ndo, "SSCOP, proto %d ", p[PROTO_POS]));
281 	}
282 }
283 
284 /*
285  * Print an ATM PDU (such as an AAL5 PDU).
286  */
287 void
288 atm_print(netdissect_options *ndo,
289           u_int vpi, u_int vci, u_int traftype, const u_char *p, u_int length,
290           u_int caplen)
291 {
292 	if (ndo->ndo_eflag)
293 		ND_PRINT((ndo, "VPI:%u VCI:%u ", vpi, vci));
294 
295 	if (vpi == 0) {
296 		switch (vci) {
297 
298 		case VCI_PPC:
299 			sig_print(ndo, p, caplen);
300 			return;
301 
302 		case VCI_BCC:
303 			ND_PRINT((ndo, "broadcast sig: "));
304 			return;
305 
306 		case VCI_OAMF4SC: /* fall through */
307 		case VCI_OAMF4EC:
308 			oam_print(ndo, p, length, ATM_OAM_HEC);
309 			return;
310 
311 		case VCI_METAC:
312 			ND_PRINT((ndo, "meta: "));
313 			return;
314 
315 		case VCI_ILMIC:
316 			ND_PRINT((ndo, "ilmi: "));
317 			snmp_print(ndo, p, length);
318 			return;
319 		}
320 	}
321 
322 	switch (traftype) {
323 
324 	case ATM_LLC:
325 	default:
326 		/*
327 		 * Assumes traffic is LLC if unknown.
328 		 */
329 		atm_llc_print(ndo, p, length, caplen);
330 		break;
331 
332 	case ATM_LANE:
333 		lane_print(ndo, p, length, caplen);
334 		break;
335 	}
336 }
337 
338 struct oam_fm_loopback_t {
339     uint8_t loopback_indicator;
340     uint8_t correlation_tag[4];
341     uint8_t loopback_id[12];
342     uint8_t source_id[12];
343     uint8_t unused[16];
344 };
345 
346 struct oam_fm_ais_rdi_t {
347     uint8_t failure_type;
348     uint8_t failure_location[16];
349     uint8_t unused[28];
350 };
351 
352 int
353 oam_print (netdissect_options *ndo,
354            const u_char *p, u_int length, u_int hec) {
355 
356     uint32_t cell_header;
357     uint16_t vpi, vci, cksum, cksum_shouldbe, idx;
358     uint8_t  cell_type, func_type, payload, clp;
359 
360     union {
361         const struct oam_fm_loopback_t *oam_fm_loopback;
362         const struct oam_fm_ais_rdi_t *oam_fm_ais_rdi;
363     } oam_ptr;
364 
365 
366     cell_header = EXTRACT_32BITS(p+hec);
367     cell_type = ((*(p+ATM_HDR_LEN_NOHEC+hec))>>4) & 0x0f;
368     func_type = (*(p+ATM_HDR_LEN_NOHEC+hec)) & 0x0f;
369 
370     vpi = (cell_header>>20)&0xff;
371     vci = (cell_header>>4)&0xffff;
372     payload = (cell_header>>1)&0x7;
373     clp = cell_header&0x1;
374 
375     ND_PRINT((ndo, "%s, vpi %u, vci %u, payload [ %s ], clp %u, length %u",
376            tok2str(oam_f_values, "OAM F5", vci),
377            vpi, vci,
378            tok2str(atm_pty_values, "Unknown", payload),
379            clp, length));
380 
381     if (!ndo->ndo_vflag) {
382         return 1;
383     }
384 
385     ND_PRINT((ndo, "\n\tcell-type %s (%u)",
386            tok2str(oam_celltype_values, "unknown", cell_type),
387            cell_type));
388 
389     if (oam_functype_values[cell_type] == NULL)
390         ND_PRINT((ndo, ", func-type unknown (%u)", func_type));
391     else
392         ND_PRINT((ndo, ", func-type %s (%u)",
393                tok2str(oam_functype_values[cell_type],"none",func_type),
394                func_type));
395 
396     p += ATM_HDR_LEN_NOHEC + hec;
397 
398     switch (cell_type << 4 | func_type) {
399     case (OAM_CELLTYPE_FM << 4 | OAM_FM_FUNCTYPE_LOOPBACK):
400         oam_ptr.oam_fm_loopback = (const struct oam_fm_loopback_t *)(p + OAM_CELLTYPE_FUNCTYPE_LEN);
401         ND_PRINT((ndo, "\n\tLoopback-Indicator %s, Correlation-Tag 0x%08x",
402                tok2str(oam_fm_loopback_indicator_values,
403                        "Unknown",
404                        oam_ptr.oam_fm_loopback->loopback_indicator & OAM_FM_LOOPBACK_INDICATOR_MASK),
405                EXTRACT_32BITS(&oam_ptr.oam_fm_loopback->correlation_tag)));
406         ND_PRINT((ndo, "\n\tLocation-ID "));
407         for (idx = 0; idx < sizeof(oam_ptr.oam_fm_loopback->loopback_id); idx++) {
408             if (idx % 2) {
409                 ND_PRINT((ndo, "%04x ", EXTRACT_16BITS(&oam_ptr.oam_fm_loopback->loopback_id[idx])));
410             }
411         }
412         ND_PRINT((ndo, "\n\tSource-ID   "));
413         for (idx = 0; idx < sizeof(oam_ptr.oam_fm_loopback->source_id); idx++) {
414             if (idx % 2) {
415                 ND_PRINT((ndo, "%04x ", EXTRACT_16BITS(&oam_ptr.oam_fm_loopback->source_id[idx])));
416             }
417         }
418         break;
419 
420     case (OAM_CELLTYPE_FM << 4 | OAM_FM_FUNCTYPE_AIS):
421     case (OAM_CELLTYPE_FM << 4 | OAM_FM_FUNCTYPE_RDI):
422         oam_ptr.oam_fm_ais_rdi = (const struct oam_fm_ais_rdi_t *)(p + OAM_CELLTYPE_FUNCTYPE_LEN);
423         ND_PRINT((ndo, "\n\tFailure-type 0x%02x", oam_ptr.oam_fm_ais_rdi->failure_type));
424         ND_PRINT((ndo, "\n\tLocation-ID "));
425         for (idx = 0; idx < sizeof(oam_ptr.oam_fm_ais_rdi->failure_location); idx++) {
426             if (idx % 2) {
427                 ND_PRINT((ndo, "%04x ", EXTRACT_16BITS(&oam_ptr.oam_fm_ais_rdi->failure_location[idx])));
428             }
429         }
430         break;
431 
432     case (OAM_CELLTYPE_FM << 4 | OAM_FM_FUNCTYPE_CONTCHECK):
433         /* FIXME */
434         break;
435 
436     default:
437         break;
438     }
439 
440     /* crc10 checksum verification */
441     cksum = EXTRACT_16BITS(p + OAM_CELLTYPE_FUNCTYPE_LEN + OAM_FUNCTION_SPECIFIC_LEN)
442         & OAM_CRC10_MASK;
443     cksum_shouldbe = verify_crc10_cksum(0, p, OAM_PAYLOAD_LEN);
444 
445     ND_PRINT((ndo, "\n\tcksum 0x%03x (%scorrect)",
446            cksum,
447            cksum_shouldbe == 0 ? "" : "in"));
448 
449     return 1;
450 }
451