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