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