xref: /dflybsd-src/contrib/tcpdump/print-chdlc.c (revision 59c07fbdf8168fa08c76c515186d561b5a92690c)
141c99275SPeter Avalos /*
241c99275SPeter Avalos  * Copyright (c) 1990, 1991, 1993, 1994, 1995, 1996, 1997
341c99275SPeter Avalos  *	The Regents of the University of California.  All rights reserved.
441c99275SPeter Avalos  *
541c99275SPeter Avalos  * Redistribution and use in source and binary forms, with or without
641c99275SPeter Avalos  * modification, are permitted provided that: (1) source code distributions
741c99275SPeter Avalos  * retain the above copyright notice and this paragraph in its entirety, (2)
841c99275SPeter Avalos  * distributions including binary code include the above copyright notice and
941c99275SPeter Avalos  * this paragraph in its entirety in the documentation or other materials
1041c99275SPeter Avalos  * provided with the distribution, and (3) all advertising materials mentioning
1141c99275SPeter Avalos  * features or use of this software display the following acknowledgement:
1241c99275SPeter Avalos  * ``This product includes software developed by the University of California,
1341c99275SPeter Avalos  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
1441c99275SPeter Avalos  * the University nor the names of its contributors may be used to endorse
1541c99275SPeter Avalos  * or promote products derived from this software without specific prior
1641c99275SPeter Avalos  * written permission.
1741c99275SPeter Avalos  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
1841c99275SPeter Avalos  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
1941c99275SPeter Avalos  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
2041c99275SPeter Avalos  */
2141c99275SPeter Avalos 
22411677aeSAaron LI /* \summary: Cisco HDLC printer */
2341c99275SPeter Avalos 
2441c99275SPeter Avalos #ifdef HAVE_CONFIG_H
25*ed775ee7SAntonio Huete Jimenez #include <config.h>
2641c99275SPeter Avalos #endif
2741c99275SPeter Avalos 
28*ed775ee7SAntonio Huete Jimenez #include "netdissect-stdinc.h"
2941c99275SPeter Avalos 
30411677aeSAaron LI #include "netdissect.h"
3141c99275SPeter Avalos #include "addrtoname.h"
3241c99275SPeter Avalos #include "ethertype.h"
3341c99275SPeter Avalos #include "extract.h"
3441c99275SPeter Avalos #include "chdlc.h"
35*ed775ee7SAntonio Huete Jimenez #include "nlpid.h"
3641c99275SPeter Avalos 
37411677aeSAaron LI static void chdlc_slarp_print(netdissect_options *, const u_char *, u_int);
3841c99275SPeter Avalos 
39411677aeSAaron LI static const struct tok chdlc_cast_values[] = {
4041c99275SPeter Avalos     { CHDLC_UNICAST, "unicast" },
4141c99275SPeter Avalos     { CHDLC_BCAST, "bcast" },
4241c99275SPeter Avalos     { 0, NULL}
4341c99275SPeter Avalos };
4441c99275SPeter Avalos 
4541c99275SPeter Avalos 
4641c99275SPeter Avalos /* Standard CHDLC printer */
47*ed775ee7SAntonio Huete Jimenez void
chdlc_if_print(netdissect_options * ndo,const struct pcap_pkthdr * h,const u_char * p)48*ed775ee7SAntonio Huete Jimenez chdlc_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, const u_char *p)
4941c99275SPeter Avalos {
50*ed775ee7SAntonio Huete Jimenez 	ndo->ndo_protocol = "chdlc";
51*ed775ee7SAntonio Huete Jimenez 	ndo->ndo_ll_hdr_len += chdlc_print(ndo, p, h->len);
5241c99275SPeter Avalos }
5341c99275SPeter Avalos 
5441c99275SPeter Avalos u_int
chdlc_print(netdissect_options * ndo,const u_char * p,u_int length)55*ed775ee7SAntonio Huete Jimenez chdlc_print(netdissect_options *ndo, const u_char *p, u_int length)
56411677aeSAaron LI {
5741c99275SPeter Avalos 	u_int proto;
58411677aeSAaron LI 	const u_char *bp = p;
5941c99275SPeter Avalos 
60*ed775ee7SAntonio Huete Jimenez 	ndo->ndo_protocol = "chdlc";
61411677aeSAaron LI 	if (length < CHDLC_HDRLEN)
62411677aeSAaron LI 		goto trunc;
63*ed775ee7SAntonio Huete Jimenez 	proto = GET_BE_U_2(p + 2);
64411677aeSAaron LI 	if (ndo->ndo_eflag) {
65*ed775ee7SAntonio Huete Jimenez                 ND_PRINT("%s, ethertype %s (0x%04x), length %u: ",
66*ed775ee7SAntonio Huete Jimenez                        tok2str(chdlc_cast_values, "0x%02x", GET_U_1(p)),
6741c99275SPeter Avalos                        tok2str(ethertype_values, "Unknown", proto),
6841c99275SPeter Avalos                        proto,
69*ed775ee7SAntonio Huete Jimenez                        length);
7041c99275SPeter Avalos 	}
7141c99275SPeter Avalos 
7241c99275SPeter Avalos 	length -= CHDLC_HDRLEN;
7341c99275SPeter Avalos 	p += CHDLC_HDRLEN;
7441c99275SPeter Avalos 
7541c99275SPeter Avalos 	switch (proto) {
7641c99275SPeter Avalos 	case ETHERTYPE_IP:
77411677aeSAaron LI 		ip_print(ndo, p, length);
7841c99275SPeter Avalos 		break;
7941c99275SPeter Avalos 	case ETHERTYPE_IPV6:
80411677aeSAaron LI 		ip6_print(ndo, p, length);
8141c99275SPeter Avalos 		break;
8241c99275SPeter Avalos 	case CHDLC_TYPE_SLARP:
83411677aeSAaron LI 		chdlc_slarp_print(ndo, p, length);
8441c99275SPeter Avalos 		break;
8541c99275SPeter Avalos         case ETHERTYPE_MPLS:
8641c99275SPeter Avalos         case ETHERTYPE_MPLS_MULTI:
87411677aeSAaron LI                 mpls_print(ndo, p, length);
8841c99275SPeter Avalos 		break;
8941c99275SPeter Avalos         case ETHERTYPE_ISO:
9041c99275SPeter Avalos                 /* is the fudge byte set ? lets verify by spotting ISO headers */
91411677aeSAaron LI                 if (length < 2)
92411677aeSAaron LI                     goto trunc;
93*ed775ee7SAntonio Huete Jimenez                 if (GET_U_1(p + 1) == NLPID_CLNP ||
94*ed775ee7SAntonio Huete Jimenez                     GET_U_1(p + 1) == NLPID_ESIS ||
95*ed775ee7SAntonio Huete Jimenez                     GET_U_1(p + 1) == NLPID_ISIS)
96411677aeSAaron LI                     isoclns_print(ndo, p + 1, length - 1);
9741c99275SPeter Avalos                 else
98411677aeSAaron LI                     isoclns_print(ndo, p, length);
9941c99275SPeter Avalos                 break;
10041c99275SPeter Avalos 	default:
101411677aeSAaron LI                 if (!ndo->ndo_eflag)
102*ed775ee7SAntonio Huete Jimenez                         ND_PRINT("unknown CHDLC protocol (0x%04x)", proto);
10341c99275SPeter Avalos                 break;
10441c99275SPeter Avalos 	}
10541c99275SPeter Avalos 
10641c99275SPeter Avalos 	return (CHDLC_HDRLEN);
107411677aeSAaron LI 
108411677aeSAaron LI trunc:
109*ed775ee7SAntonio Huete Jimenez 	nd_print_trunc(ndo);
110*ed775ee7SAntonio Huete Jimenez 	return (ND_BYTES_AVAILABLE_AFTER(bp));
11141c99275SPeter Avalos }
11241c99275SPeter Avalos 
11341c99275SPeter Avalos /*
11441c99275SPeter Avalos  * The fixed-length portion of a SLARP packet.
11541c99275SPeter Avalos  */
11641c99275SPeter Avalos struct cisco_slarp {
117*ed775ee7SAntonio Huete Jimenez 	nd_uint32_t code;
11841c99275SPeter Avalos #define SLARP_REQUEST	0
11941c99275SPeter Avalos #define SLARP_REPLY	1
12041c99275SPeter Avalos #define SLARP_KEEPALIVE	2
12141c99275SPeter Avalos 	union {
12241c99275SPeter Avalos 		struct {
123411677aeSAaron LI 			uint8_t addr[4];
124411677aeSAaron LI 			uint8_t mask[4];
12541c99275SPeter Avalos 		} addr;
12641c99275SPeter Avalos 		struct {
127*ed775ee7SAntonio Huete Jimenez 			nd_uint32_t myseq;
128*ed775ee7SAntonio Huete Jimenez 			nd_uint32_t yourseq;
129*ed775ee7SAntonio Huete Jimenez 			nd_uint16_t rel;
13041c99275SPeter Avalos 		} keep;
13141c99275SPeter Avalos 	} un;
13241c99275SPeter Avalos };
13341c99275SPeter Avalos 
13441c99275SPeter Avalos #define SLARP_MIN_LEN	14
13541c99275SPeter Avalos #define SLARP_MAX_LEN	18
13641c99275SPeter Avalos 
13741c99275SPeter Avalos static void
chdlc_slarp_print(netdissect_options * ndo,const u_char * cp,u_int length)138411677aeSAaron LI chdlc_slarp_print(netdissect_options *ndo, const u_char *cp, u_int length)
13941c99275SPeter Avalos {
14041c99275SPeter Avalos 	const struct cisco_slarp *slarp;
14141c99275SPeter Avalos         u_int sec,min,hrs,days;
14241c99275SPeter Avalos 
143*ed775ee7SAntonio Huete Jimenez 	ndo->ndo_protocol = "chdlc_slarp";
144*ed775ee7SAntonio Huete Jimenez 	ND_PRINT("SLARP (length: %u), ",length);
14541c99275SPeter Avalos 	if (length < SLARP_MIN_LEN)
14641c99275SPeter Avalos 		goto trunc;
14741c99275SPeter Avalos 
14841c99275SPeter Avalos 	slarp = (const struct cisco_slarp *)cp;
149*ed775ee7SAntonio Huete Jimenez 	ND_TCHECK_LEN(slarp, SLARP_MIN_LEN);
150*ed775ee7SAntonio Huete Jimenez 	switch (GET_BE_U_4(slarp->code)) {
15141c99275SPeter Avalos 	case SLARP_REQUEST:
152*ed775ee7SAntonio Huete Jimenez 		ND_PRINT("request");
15341c99275SPeter Avalos 		/*
15441c99275SPeter Avalos 		 * At least according to William "Chops" Westfield's
15541c99275SPeter Avalos 		 * message in
15641c99275SPeter Avalos 		 *
157*ed775ee7SAntonio Huete Jimenez 		 *	https://web.archive.org/web/20190725151313/www.nethelp.no/net/cisco-hdlc.txt
15841c99275SPeter Avalos 		 *
15941c99275SPeter Avalos 		 * the address and mask aren't used in requests -
16041c99275SPeter Avalos 		 * they're just zero.
16141c99275SPeter Avalos 		 */
16241c99275SPeter Avalos 		break;
16341c99275SPeter Avalos 	case SLARP_REPLY:
164*ed775ee7SAntonio Huete Jimenez 		ND_PRINT("reply %s/%s",
165*ed775ee7SAntonio Huete Jimenez 			GET_IPADDR_STRING(slarp->un.addr.addr),
166*ed775ee7SAntonio Huete Jimenez 			GET_IPADDR_STRING(slarp->un.addr.mask));
16741c99275SPeter Avalos 		break;
16841c99275SPeter Avalos 	case SLARP_KEEPALIVE:
169*ed775ee7SAntonio Huete Jimenez 		ND_PRINT("keepalive: mineseen=0x%08x, yourseen=0x%08x, reliability=0x%04x",
170*ed775ee7SAntonio Huete Jimenez                        GET_BE_U_4(slarp->un.keep.myseq),
171*ed775ee7SAntonio Huete Jimenez                        GET_BE_U_4(slarp->un.keep.yourseq),
172*ed775ee7SAntonio Huete Jimenez                        GET_BE_U_2(slarp->un.keep.rel));
17341c99275SPeter Avalos 
17441c99275SPeter Avalos                 if (length >= SLARP_MAX_LEN) { /* uptime-stamp is optional */
17541c99275SPeter Avalos                         cp += SLARP_MIN_LEN;
176*ed775ee7SAntonio Huete Jimenez                         sec = GET_BE_U_4(cp) / 1000;
17741c99275SPeter Avalos                         min = sec / 60; sec -= min * 60;
17841c99275SPeter Avalos                         hrs = min / 60; min -= hrs * 60;
17941c99275SPeter Avalos                         days = hrs / 24; hrs -= days * 24;
180*ed775ee7SAntonio Huete Jimenez                         ND_PRINT(", link uptime=%ud%uh%um%us",days,hrs,min,sec);
18141c99275SPeter Avalos                 }
18241c99275SPeter Avalos 		break;
18341c99275SPeter Avalos 	default:
184*ed775ee7SAntonio Huete Jimenez 		ND_PRINT("0x%02x unknown", GET_BE_U_4(slarp->code));
185411677aeSAaron LI                 if (ndo->ndo_vflag <= 1)
186411677aeSAaron LI                     print_unknown_data(ndo,cp+4,"\n\t",length-4);
18741c99275SPeter Avalos 		break;
18841c99275SPeter Avalos 	}
18941c99275SPeter Avalos 
190411677aeSAaron LI 	if (SLARP_MAX_LEN < length && ndo->ndo_vflag)
191*ed775ee7SAntonio Huete Jimenez 		ND_PRINT(", (trailing junk: %u bytes)", length - SLARP_MAX_LEN);
192411677aeSAaron LI         if (ndo->ndo_vflag > 1)
193411677aeSAaron LI             print_unknown_data(ndo,cp+4,"\n\t",length-4);
19441c99275SPeter Avalos 	return;
19541c99275SPeter Avalos 
19641c99275SPeter Avalos trunc:
197*ed775ee7SAntonio Huete Jimenez 	nd_print_trunc(ndo);
19841c99275SPeter Avalos }
199