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