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