xref: /aosp_15_r20/external/tcpdump/print-chdlc.c (revision 05b00f6010a2396e3db2409989fc67270046269f)
1*05b00f60SXin Li /*
2*05b00f60SXin Li  * Copyright (c) 1990, 1991, 1993, 1994, 1995, 1996, 1997
3*05b00f60SXin Li  *	The Regents of the University of California.  All rights reserved.
4*05b00f60SXin Li  *
5*05b00f60SXin Li  * Redistribution and use in source and binary forms, with or without
6*05b00f60SXin Li  * modification, are permitted provided that: (1) source code distributions
7*05b00f60SXin Li  * retain the above copyright notice and this paragraph in its entirety, (2)
8*05b00f60SXin Li  * distributions including binary code include the above copyright notice and
9*05b00f60SXin Li  * this paragraph in its entirety in the documentation or other materials
10*05b00f60SXin Li  * provided with the distribution, and (3) all advertising materials mentioning
11*05b00f60SXin Li  * features or use of this software display the following acknowledgement:
12*05b00f60SXin Li  * ``This product includes software developed by the University of California,
13*05b00f60SXin Li  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14*05b00f60SXin Li  * the University nor the names of its contributors may be used to endorse
15*05b00f60SXin Li  * or promote products derived from this software without specific prior
16*05b00f60SXin Li  * written permission.
17*05b00f60SXin Li  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18*05b00f60SXin Li  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19*05b00f60SXin Li  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20*05b00f60SXin Li  */
21*05b00f60SXin Li 
22*05b00f60SXin Li /* \summary: Cisco HDLC printer */
23*05b00f60SXin Li 
24*05b00f60SXin Li #ifdef HAVE_CONFIG_H
25*05b00f60SXin Li #include <config.h>
26*05b00f60SXin Li #endif
27*05b00f60SXin Li 
28*05b00f60SXin Li #include "netdissect-stdinc.h"
29*05b00f60SXin Li 
30*05b00f60SXin Li #include "netdissect.h"
31*05b00f60SXin Li #include "addrtoname.h"
32*05b00f60SXin Li #include "ethertype.h"
33*05b00f60SXin Li #include "extract.h"
34*05b00f60SXin Li #include "chdlc.h"
35*05b00f60SXin Li #include "nlpid.h"
36*05b00f60SXin Li 
37*05b00f60SXin Li static void chdlc_slarp_print(netdissect_options *, const u_char *, u_int);
38*05b00f60SXin Li 
39*05b00f60SXin Li static const struct tok chdlc_cast_values[] = {
40*05b00f60SXin Li     { CHDLC_UNICAST, "unicast" },
41*05b00f60SXin Li     { CHDLC_BCAST, "bcast" },
42*05b00f60SXin Li     { 0, NULL}
43*05b00f60SXin Li };
44*05b00f60SXin Li 
45*05b00f60SXin Li 
46*05b00f60SXin Li /* Standard CHDLC printer */
47*05b00f60SXin Li void
chdlc_if_print(netdissect_options * ndo,const struct pcap_pkthdr * h,const u_char * p)48*05b00f60SXin Li chdlc_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, const u_char *p)
49*05b00f60SXin Li {
50*05b00f60SXin Li 	ndo->ndo_protocol = "chdlc";
51*05b00f60SXin Li 	ndo->ndo_ll_hdr_len += chdlc_print(ndo, p, h->len);
52*05b00f60SXin Li }
53*05b00f60SXin Li 
54*05b00f60SXin Li u_int
chdlc_print(netdissect_options * ndo,const u_char * p,u_int length)55*05b00f60SXin Li chdlc_print(netdissect_options *ndo, const u_char *p, u_int length)
56*05b00f60SXin Li {
57*05b00f60SXin Li 	u_int proto;
58*05b00f60SXin Li 	const u_char *bp = p;
59*05b00f60SXin Li 
60*05b00f60SXin Li 	ndo->ndo_protocol = "chdlc";
61*05b00f60SXin Li 	if (length < CHDLC_HDRLEN)
62*05b00f60SXin Li 		goto trunc;
63*05b00f60SXin Li 	proto = GET_BE_U_2(p + 2);
64*05b00f60SXin Li 	if (ndo->ndo_eflag) {
65*05b00f60SXin Li                 ND_PRINT("%s, ethertype %s (0x%04x), length %u: ",
66*05b00f60SXin Li                        tok2str(chdlc_cast_values, "0x%02x", GET_U_1(p)),
67*05b00f60SXin Li                        tok2str(ethertype_values, "Unknown", proto),
68*05b00f60SXin Li                        proto,
69*05b00f60SXin Li                        length);
70*05b00f60SXin Li 	}
71*05b00f60SXin Li 
72*05b00f60SXin Li 	length -= CHDLC_HDRLEN;
73*05b00f60SXin Li 	p += CHDLC_HDRLEN;
74*05b00f60SXin Li 
75*05b00f60SXin Li 	switch (proto) {
76*05b00f60SXin Li 	case ETHERTYPE_IP:
77*05b00f60SXin Li 		ip_print(ndo, p, length);
78*05b00f60SXin Li 		break;
79*05b00f60SXin Li 	case ETHERTYPE_IPV6:
80*05b00f60SXin Li 		ip6_print(ndo, p, length);
81*05b00f60SXin Li 		break;
82*05b00f60SXin Li 	case CHDLC_TYPE_SLARP:
83*05b00f60SXin Li 		chdlc_slarp_print(ndo, p, length);
84*05b00f60SXin Li 		break;
85*05b00f60SXin Li         case ETHERTYPE_MPLS:
86*05b00f60SXin Li         case ETHERTYPE_MPLS_MULTI:
87*05b00f60SXin Li                 mpls_print(ndo, p, length);
88*05b00f60SXin Li 		break;
89*05b00f60SXin Li         case ETHERTYPE_ISO:
90*05b00f60SXin Li                 /* is the fudge byte set ? lets verify by spotting ISO headers */
91*05b00f60SXin Li                 if (length < 2)
92*05b00f60SXin Li                     goto trunc;
93*05b00f60SXin Li                 if (GET_U_1(p + 1) == NLPID_CLNP ||
94*05b00f60SXin Li                     GET_U_1(p + 1) == NLPID_ESIS ||
95*05b00f60SXin Li                     GET_U_1(p + 1) == NLPID_ISIS)
96*05b00f60SXin Li                     isoclns_print(ndo, p + 1, length - 1);
97*05b00f60SXin Li                 else
98*05b00f60SXin Li                     isoclns_print(ndo, p, length);
99*05b00f60SXin Li                 break;
100*05b00f60SXin Li 	default:
101*05b00f60SXin Li                 if (!ndo->ndo_eflag)
102*05b00f60SXin Li                         ND_PRINT("unknown CHDLC protocol (0x%04x)", proto);
103*05b00f60SXin Li                 break;
104*05b00f60SXin Li 	}
105*05b00f60SXin Li 
106*05b00f60SXin Li 	return (CHDLC_HDRLEN);
107*05b00f60SXin Li 
108*05b00f60SXin Li trunc:
109*05b00f60SXin Li 	nd_print_trunc(ndo);
110*05b00f60SXin Li 	return (ND_BYTES_AVAILABLE_AFTER(bp));
111*05b00f60SXin Li }
112*05b00f60SXin Li 
113*05b00f60SXin Li /*
114*05b00f60SXin Li  * The fixed-length portion of a SLARP packet.
115*05b00f60SXin Li  */
116*05b00f60SXin Li struct cisco_slarp {
117*05b00f60SXin Li 	nd_uint32_t code;
118*05b00f60SXin Li #define SLARP_REQUEST	0
119*05b00f60SXin Li #define SLARP_REPLY	1
120*05b00f60SXin Li #define SLARP_KEEPALIVE	2
121*05b00f60SXin Li 	union {
122*05b00f60SXin Li 		struct {
123*05b00f60SXin Li 			uint8_t addr[4];
124*05b00f60SXin Li 			uint8_t mask[4];
125*05b00f60SXin Li 		} addr;
126*05b00f60SXin Li 		struct {
127*05b00f60SXin Li 			nd_uint32_t myseq;
128*05b00f60SXin Li 			nd_uint32_t yourseq;
129*05b00f60SXin Li 			nd_uint16_t rel;
130*05b00f60SXin Li 		} keep;
131*05b00f60SXin Li 	} un;
132*05b00f60SXin Li };
133*05b00f60SXin Li 
134*05b00f60SXin Li #define SLARP_MIN_LEN	14
135*05b00f60SXin Li #define SLARP_MAX_LEN	18
136*05b00f60SXin Li 
137*05b00f60SXin Li static void
chdlc_slarp_print(netdissect_options * ndo,const u_char * cp,u_int length)138*05b00f60SXin Li chdlc_slarp_print(netdissect_options *ndo, const u_char *cp, u_int length)
139*05b00f60SXin Li {
140*05b00f60SXin Li 	const struct cisco_slarp *slarp;
141*05b00f60SXin Li         u_int sec,min,hrs,days;
142*05b00f60SXin Li 
143*05b00f60SXin Li 	ndo->ndo_protocol = "chdlc_slarp";
144*05b00f60SXin Li 	ND_PRINT("SLARP (length: %u), ",length);
145*05b00f60SXin Li 	if (length < SLARP_MIN_LEN)
146*05b00f60SXin Li 		goto trunc;
147*05b00f60SXin Li 
148*05b00f60SXin Li 	slarp = (const struct cisco_slarp *)cp;
149*05b00f60SXin Li 	ND_TCHECK_LEN(slarp, SLARP_MIN_LEN);
150*05b00f60SXin Li 	switch (GET_BE_U_4(slarp->code)) {
151*05b00f60SXin Li 	case SLARP_REQUEST:
152*05b00f60SXin Li 		ND_PRINT("request");
153*05b00f60SXin Li 		/*
154*05b00f60SXin Li 		 * At least according to William "Chops" Westfield's
155*05b00f60SXin Li 		 * message in
156*05b00f60SXin Li 		 *
157*05b00f60SXin Li 		 *	https://web.archive.org/web/20190725151313/www.nethelp.no/net/cisco-hdlc.txt
158*05b00f60SXin Li 		 *
159*05b00f60SXin Li 		 * the address and mask aren't used in requests -
160*05b00f60SXin Li 		 * they're just zero.
161*05b00f60SXin Li 		 */
162*05b00f60SXin Li 		break;
163*05b00f60SXin Li 	case SLARP_REPLY:
164*05b00f60SXin Li 		ND_PRINT("reply %s/%s",
165*05b00f60SXin Li 			GET_IPADDR_STRING(slarp->un.addr.addr),
166*05b00f60SXin Li 			GET_IPADDR_STRING(slarp->un.addr.mask));
167*05b00f60SXin Li 		break;
168*05b00f60SXin Li 	case SLARP_KEEPALIVE:
169*05b00f60SXin Li 		ND_PRINT("keepalive: mineseen=0x%08x, yourseen=0x%08x, reliability=0x%04x",
170*05b00f60SXin Li                        GET_BE_U_4(slarp->un.keep.myseq),
171*05b00f60SXin Li                        GET_BE_U_4(slarp->un.keep.yourseq),
172*05b00f60SXin Li                        GET_BE_U_2(slarp->un.keep.rel));
173*05b00f60SXin Li 
174*05b00f60SXin Li                 if (length >= SLARP_MAX_LEN) { /* uptime-stamp is optional */
175*05b00f60SXin Li                         cp += SLARP_MIN_LEN;
176*05b00f60SXin Li                         sec = GET_BE_U_4(cp) / 1000;
177*05b00f60SXin Li                         min = sec / 60; sec -= min * 60;
178*05b00f60SXin Li                         hrs = min / 60; min -= hrs * 60;
179*05b00f60SXin Li                         days = hrs / 24; hrs -= days * 24;
180*05b00f60SXin Li                         ND_PRINT(", link uptime=%ud%uh%um%us",days,hrs,min,sec);
181*05b00f60SXin Li                 }
182*05b00f60SXin Li 		break;
183*05b00f60SXin Li 	default:
184*05b00f60SXin Li 		ND_PRINT("0x%02x unknown", GET_BE_U_4(slarp->code));
185*05b00f60SXin Li                 if (ndo->ndo_vflag <= 1)
186*05b00f60SXin Li                     print_unknown_data(ndo,cp+4,"\n\t",length-4);
187*05b00f60SXin Li 		break;
188*05b00f60SXin Li 	}
189*05b00f60SXin Li 
190*05b00f60SXin Li 	if (SLARP_MAX_LEN < length && ndo->ndo_vflag)
191*05b00f60SXin Li 		ND_PRINT(", (trailing junk: %u bytes)", length - SLARP_MAX_LEN);
192*05b00f60SXin Li         if (ndo->ndo_vflag > 1)
193*05b00f60SXin Li             print_unknown_data(ndo,cp+4,"\n\t",length-4);
194*05b00f60SXin Li 	return;
195*05b00f60SXin Li 
196*05b00f60SXin Li trunc:
197*05b00f60SXin Li 	nd_print_trunc(ndo);
198*05b00f60SXin Li }
199