print-atm.c revision 235530
1221163Sadrian/*
2221163Sadrian * Copyright (c) 1994, 1995, 1996, 1997
3221163Sadrian *	The Regents of the University of California.  All rights reserved.
4221163Sadrian *
5221163Sadrian * Redistribution and use in source and binary forms, with or without
6221163Sadrian * modification, are permitted provided that: (1) source code distributions
7221163Sadrian * retain the above copyright notice and this paragraph in its entirety, (2)
8221163Sadrian * distributions including binary code include the above copyright notice and
9221163Sadrian * this paragraph in its entirety in the documentation or other materials
10221163Sadrian * provided with the distribution, and (3) all advertising materials mentioning
11221163Sadrian * features or use of this software display the following acknowledgement:
12221163Sadrian * ``This product includes software developed by the University of California,
13221163Sadrian * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14221163Sadrian * the University nor the names of its contributors may be used to endorse
15221163Sadrian * or promote products derived from this software without specific prior
16221163Sadrian * written permission.
17221163Sadrian * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18221163Sadrian * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19221163Sadrian * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20221163Sadrian *
21221163Sadrian * $FreeBSD: head/contrib/tcpdump/print-atm.c 235530 2012-05-17 05:11:57Z delphij $
22221163Sadrian */
23221163Sadrian#ifndef lint
24221163Sadrianstatic const char rcsid[] _U_ =
25221163Sadrian    "@(#) $Header: /tcpdump/master/tcpdump/print-atm.c,v 1.49 2007-10-22 19:37:51 guy Exp $ (LBL)";
26221163Sadrian#endif
27221163Sadrian
28221163Sadrian#ifdef HAVE_CONFIG_H
29221163Sadrian#include "config.h"
30221163Sadrian#endif
31221163Sadrian
32221163Sadrian#include <tcpdump-stdinc.h>
33221163Sadrian
34221163Sadrian#include <stdio.h>
35221163Sadrian#include <pcap.h>
36221163Sadrian#include <string.h>
37221163Sadrian
38221163Sadrian#include "interface.h"
39221163Sadrian#include "extract.h"
40221163Sadrian#include "addrtoname.h"
41221163Sadrian#include "ethertype.h"
42221163Sadrian#include "atm.h"
43221163Sadrian#include "atmuni31.h"
44221163Sadrian#include "llc.h"
45221163Sadrian
46221163Sadrian#include "ether.h"
47221163Sadrian
48221163Sadrian#define OAM_CRC10_MASK 0x3ff
49221163Sadrian#define OAM_PAYLOAD_LEN 48
50221163Sadrian#define OAM_FUNCTION_SPECIFIC_LEN 45 /* this excludes crc10 and cell-type/function-type */
51221163Sadrian#define OAM_CELLTYPE_FUNCTYPE_LEN 1
52221163Sadrian
53221163Sadrianstruct tok oam_f_values[] = {
54221163Sadrian    { VCI_OAMF4SC, "OAM F4 (segment)" },
55221163Sadrian    { VCI_OAMF4EC, "OAM F4 (end)" },
56221163Sadrian    { 0, NULL }
57221163Sadrian};
58221163Sadrian
59221163Sadrianstruct tok atm_pty_values[] = {
60221163Sadrian    { 0x0, "user data, uncongested, SDU 0" },
61221163Sadrian    { 0x1, "user data, uncongested, SDU 1" },
62221163Sadrian    { 0x2, "user data, congested, SDU 0" },
63221163Sadrian    { 0x3, "user data, congested, SDU 1" },
64221163Sadrian    { 0x4, "VCC OAM F5 flow segment" },
65221163Sadrian    { 0x5, "VCC OAM F5 flow end-to-end" },
66221163Sadrian    { 0x6, "Traffic Control and resource Mgmt" },
67221163Sadrian    { 0, NULL }
68221163Sadrian};
69221163Sadrian
70221163Sadrian#define OAM_CELLTYPE_FM 0x1
71221163Sadrian#define OAM_CELLTYPE_PM 0x2
72221163Sadrian#define OAM_CELLTYPE_AD 0x8
73221163Sadrian#define OAM_CELLTYPE_SM 0xf
74221163Sadrian
75221163Sadrianstruct tok oam_celltype_values[] = {
76221163Sadrian    { OAM_CELLTYPE_FM, "Fault Management" },
77221163Sadrian    { OAM_CELLTYPE_PM, "Performance Management" },
78221163Sadrian    { OAM_CELLTYPE_AD, "activate/deactivate" },
79221163Sadrian    { OAM_CELLTYPE_SM, "System Management" },
80221163Sadrian    { 0, NULL }
81225883Sadrian};
82221163Sadrian
83221163Sadrian#define OAM_FM_FUNCTYPE_AIS       0x0
84221163Sadrian#define OAM_FM_FUNCTYPE_RDI       0x1
85221163Sadrian#define OAM_FM_FUNCTYPE_CONTCHECK 0x4
86221163Sadrian#define OAM_FM_FUNCTYPE_LOOPBACK  0x8
87225883Sadrian
88221163Sadrianstruct tok oam_fm_functype_values[] = {
89221163Sadrian    { OAM_FM_FUNCTYPE_AIS, "AIS" },
90221163Sadrian    { OAM_FM_FUNCTYPE_RDI, "RDI" },
91221163Sadrian    { OAM_FM_FUNCTYPE_CONTCHECK, "Continuity Check" },
92221163Sadrian    { OAM_FM_FUNCTYPE_LOOPBACK, "Loopback" },
93221163Sadrian    { 0, NULL }
94221163Sadrian};
95221163Sadrian
96221163Sadrianstruct tok oam_pm_functype_values[] = {
97221163Sadrian    { 0x0, "Forward Monitoring" },
98221163Sadrian    { 0x1, "Backward Reporting" },
99221163Sadrian    { 0x2, "Monitoring and Reporting" },
100221163Sadrian    { 0, NULL }
101221163Sadrian};
102221163Sadrian
103221163Sadrianstruct tok oam_ad_functype_values[] = {
104221163Sadrian    { 0x0, "Performance Monitoring" },
105221163Sadrian    { 0x1, "Continuity Check" },
106221163Sadrian    { 0, NULL }
107221163Sadrian};
108221163Sadrian
109221163Sadrian#define OAM_FM_LOOPBACK_INDICATOR_MASK 0x1
110221163Sadrian
111221163Sadrianstruct tok oam_fm_loopback_indicator_values[] = {
112221163Sadrian    { 0x0, "Reply" },
113221163Sadrian    { 0x1, "Request" },
114221163Sadrian    { 0, NULL }
115221163Sadrian};
116221163Sadrian
117221163Sadrianstatic const struct tok *oam_functype_values[16] = {
118221163Sadrian    NULL,
119221163Sadrian    oam_fm_functype_values, /* 1 */
120221163Sadrian    oam_pm_functype_values, /* 2 */
121221163Sadrian    NULL,
122221163Sadrian    NULL,
123221163Sadrian    NULL,
124221163Sadrian    NULL,
125221163Sadrian    NULL,
126221163Sadrian    oam_ad_functype_values, /* 8 */
127221163Sadrian    NULL,
128221163Sadrian    NULL,
129221163Sadrian    NULL,
130221163Sadrian    NULL,
131221163Sadrian    NULL,
132221163Sadrian    NULL,
133221163Sadrian    NULL
134221163Sadrian};
135221163Sadrian
136221163Sadrian/*
137221163Sadrian * Print an RFC 1483 LLC-encapsulated ATM frame.
138221163Sadrian */
139221163Sadrianstatic void
140221163Sadrianatm_llc_print(const u_char *p, int length, int caplen)
141221163Sadrian{
142221163Sadrian	u_short extracted_ethertype;
143221163Sadrian
144221163Sadrian	if (!llc_print(p, length, caplen, NULL, NULL,
145221163Sadrian	    &extracted_ethertype)) {
146221163Sadrian		/* ether_type not known, print raw packet */
147221163Sadrian		if (extracted_ethertype) {
148221163Sadrian			printf("(LLC %s) ",
149221163Sadrian		etherproto_string(htons(extracted_ethertype)));
150221163Sadrian		}
151221163Sadrian		if (!suppress_default_print)
152221163Sadrian			default_print(p, caplen);
153221163Sadrian	}
154221163Sadrian}
155221163Sadrian
156221163Sadrian/*
157221163Sadrian * Given a SAP value, generate the LLC header value for a UI packet
158221163Sadrian * with that SAP as the source and destination SAP.
159221163Sadrian */
160221163Sadrian#define LLC_UI_HDR(sap)	((sap)<<16 | (sap<<8) | 0x03)
161221163Sadrian
162221163Sadrian/*
163221163Sadrian * This is the top level routine of the printer.  'p' points
164221163Sadrian * to the LLC/SNAP header of the packet, 'h->ts' is the timestamp,
165221163Sadrian * 'h->len' is the length of the packet off the wire, and 'h->caplen'
166221163Sadrian * is the number of bytes actually captured.
167221163Sadrian */
168221163Sadrianu_int
169221163Sadrianatm_if_print(const struct pcap_pkthdr *h, const u_char *p)
170221163Sadrian{
171221163Sadrian	u_int caplen = h->caplen;
172221163Sadrian	u_int length = h->len;
173221163Sadrian	u_int32_t llchdr;
174221163Sadrian	u_int hdrlen = 0;
175221163Sadrian
176221163Sadrian	if (caplen < 8) {
177221163Sadrian		printf("[|atm]");
178221163Sadrian		return (caplen);
179221163Sadrian	}
180221163Sadrian
181221163Sadrian        /* Cisco Style NLPID ? */
182221163Sadrian        if (*p == LLC_UI) {
183221163Sadrian            if (eflag)
184221163Sadrian                printf("CNLPID ");
185221163Sadrian            isoclns_print(p+1, length-1, caplen-1);
186221163Sadrian            return hdrlen;
187221163Sadrian        }
188221163Sadrian
189221163Sadrian	/*
190221163Sadrian	 * Extract the presumed LLC header into a variable, for quick
191221163Sadrian	 * testing.
192221163Sadrian	 * Then check for a header that's neither a header for a SNAP
193221163Sadrian	 * packet nor an RFC 2684 routed NLPID-formatted PDU nor
194221163Sadrian	 * an 802.2-but-no-SNAP IP packet.
195221163Sadrian	 */
196221163Sadrian	llchdr = EXTRACT_24BITS(p);
197221163Sadrian	if (llchdr != LLC_UI_HDR(LLCSAP_SNAP) &&
198221163Sadrian	    llchdr != LLC_UI_HDR(LLCSAP_ISONS) &&
199221163Sadrian	    llchdr != LLC_UI_HDR(LLCSAP_IP)) {
200221163Sadrian		/*
201221163Sadrian		 * XXX - assume 802.6 MAC header from Fore driver.
202221163Sadrian		 *
203221163Sadrian		 * Unfortunately, the above list doesn't check for
204221163Sadrian		 * all known SAPs, doesn't check for headers where
205221163Sadrian		 * the source and destination SAP aren't the same,
206221163Sadrian		 * and doesn't check for non-UI frames.  It also
207221163Sadrian		 * runs the risk of an 802.6 MAC header that happens
208221163Sadrian		 * to begin with one of those values being
209221163Sadrian		 * incorrectly treated as an 802.2 header.
210221163Sadrian		 *
211221163Sadrian		 * So is that Fore driver still around?  And, if so,
212221163Sadrian		 * is it still putting 802.6 MAC headers on ATM
213221163Sadrian		 * packets?  If so, could it be changed to use a
214221163Sadrian		 * new DLT_IEEE802_6 value if we added it?
215221163Sadrian		 */
216221163Sadrian		if (eflag)
217221163Sadrian			printf("%08x%08x %08x%08x ",
218221163Sadrian			       EXTRACT_32BITS(p),
219221163Sadrian			       EXTRACT_32BITS(p+4),
220221163Sadrian			       EXTRACT_32BITS(p+8),
221221163Sadrian			       EXTRACT_32BITS(p+12));
222221163Sadrian		p += 20;
223221163Sadrian		length -= 20;
224221163Sadrian		caplen -= 20;
225221163Sadrian		hdrlen += 20;
226221163Sadrian	}
227221163Sadrian	atm_llc_print(p, length, caplen);
228221163Sadrian	return (hdrlen);
229221163Sadrian}
230221163Sadrian
231221163Sadrian/*
232221163Sadrian * ATM signalling.
233221163Sadrian */
234221163Sadrianstatic struct tok msgtype2str[] = {
235221163Sadrian	{ CALL_PROCEED,		"Call_proceeding" },
236221163Sadrian	{ CONNECT,		"Connect" },
237221163Sadrian	{ CONNECT_ACK,		"Connect_ack" },
238221163Sadrian	{ SETUP,		"Setup" },
239221596Sadrian	{ RELEASE,		"Release" },
240221596Sadrian	{ RELEASE_DONE,		"Release_complete" },
241221163Sadrian	{ RESTART,		"Restart" },
242221596Sadrian	{ RESTART_ACK,		"Restart_ack" },
243221163Sadrian	{ STATUS,		"Status" },
244221163Sadrian	{ STATUS_ENQ,		"Status_enquiry" },
245221163Sadrian	{ ADD_PARTY,		"Add_party" },
246221163Sadrian	{ ADD_PARTY_ACK,	"Add_party_ack" },
247221163Sadrian	{ ADD_PARTY_REJ,	"Add_party_reject" },
248221163Sadrian	{ DROP_PARTY,		"Drop_party" },
249221163Sadrian	{ DROP_PARTY_ACK,	"Drop_party_ack" },
250221163Sadrian	{ 0,			NULL }
251221163Sadrian};
252221163Sadrian
253221163Sadrianstatic void
254221163Sadriansig_print(const u_char *p, int caplen)
255221163Sadrian{
256221163Sadrian	bpf_u_int32 call_ref;
257221163Sadrian
258221163Sadrian	if (caplen < PROTO_POS) {
259221163Sadrian		printf("[|atm]");
260221163Sadrian		return;
261221163Sadrian	}
262221163Sadrian	if (p[PROTO_POS] == Q2931) {
263221163Sadrian		/*
264221163Sadrian		 * protocol:Q.2931 for User to Network Interface
265221163Sadrian		 * (UNI 3.1) signalling
266221163Sadrian		 */
267221163Sadrian		printf("Q.2931");
268221163Sadrian		if (caplen < MSG_TYPE_POS) {
269221163Sadrian			printf(" [|atm]");
270221163Sadrian			return;
271221163Sadrian		}
272221163Sadrian		printf(":%s ",
273221163Sadrian		    tok2str(msgtype2str, "msgtype#%d", p[MSG_TYPE_POS]));
274221163Sadrian
275221163Sadrian		/*
276221163Sadrian		 * The call reference comes before the message type,
277221163Sadrian		 * so if we know we have the message type, which we
278221163Sadrian		 * do from the caplen test above, we also know we have
279221163Sadrian		 * the call reference.
280221163Sadrian		 */
281221163Sadrian		call_ref = EXTRACT_24BITS(&p[CALL_REF_POS]);
282221163Sadrian		printf("CALL_REF:0x%06x", call_ref);
283221163Sadrian	} else {
284221163Sadrian		/* SCCOP with some unknown protocol atop it */
285221163Sadrian		printf("SSCOP, proto %d ", p[PROTO_POS]);
286221163Sadrian	}
287221163Sadrian}
288221163Sadrian
289221163Sadrian/*
290221163Sadrian * Print an ATM PDU (such as an AAL5 PDU).
291221163Sadrian */
292222584Sadrianvoid
293221163Sadrianatm_print(u_int vpi, u_int vci, u_int traftype, const u_char *p, u_int length,
294221603Sadrian    u_int caplen)
295221603Sadrian{
296221603Sadrian	if (eflag)
297221603Sadrian		printf("VPI:%u VCI:%u ", vpi, vci);
298221603Sadrian
299221603Sadrian	if (vpi == 0) {
300221603Sadrian		switch (vci) {
301226488Sadrian
302226488Sadrian		case VCI_PPC:
303221603Sadrian			sig_print(p, caplen);
304221163Sadrian			return;
305221163Sadrian
306221163Sadrian		case VCI_BCC:
307221163Sadrian			printf("broadcast sig: ");
308221163Sadrian			return;
309221163Sadrian
310221163Sadrian		case VCI_OAMF4SC: /* fall through */
311221163Sadrian		case VCI_OAMF4EC:
312221163Sadrian                        oam_print(p, length, ATM_OAM_HEC);
313221163Sadrian			return;
314221163Sadrian
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