xref: /freebsd/contrib/tcpdump/print-chdlc.c (revision ee67461e56828dd1f8de165947ba83f6d9148a87)
1b0453382SBill Fenner /*
2b0453382SBill Fenner  * Copyright (c) 1990, 1991, 1993, 1994, 1995, 1996, 1997
3b0453382SBill Fenner  *	The Regents of the University of California.  All rights reserved.
4b0453382SBill Fenner  *
5b0453382SBill Fenner  * Redistribution and use in source and binary forms, with or without
6b0453382SBill Fenner  * modification, are permitted provided that: (1) source code distributions
7b0453382SBill Fenner  * retain the above copyright notice and this paragraph in its entirety, (2)
8b0453382SBill Fenner  * distributions including binary code include the above copyright notice and
9b0453382SBill Fenner  * this paragraph in its entirety in the documentation or other materials
10b0453382SBill Fenner  * provided with the distribution, and (3) all advertising materials mentioning
11b0453382SBill Fenner  * features or use of this software display the following acknowledgement:
12b0453382SBill Fenner  * ``This product includes software developed by the University of California,
13b0453382SBill Fenner  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14b0453382SBill Fenner  * the University nor the names of its contributors may be used to endorse
15b0453382SBill Fenner  * or promote products derived from this software without specific prior
16b0453382SBill Fenner  * written permission.
17b0453382SBill Fenner  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18b0453382SBill Fenner  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19b0453382SBill Fenner  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20b0453382SBill Fenner  */
21b0453382SBill Fenner 
223340d773SGleb Smirnoff /* \summary: Cisco HDLC printer */
233340d773SGleb Smirnoff 
24b0453382SBill Fenner #ifdef HAVE_CONFIG_H
25*ee67461eSJoseph Mingrone #include <config.h>
26b0453382SBill Fenner #endif
27b0453382SBill Fenner 
28*ee67461eSJoseph Mingrone #include "netdissect-stdinc.h"
29b0453382SBill Fenner 
303340d773SGleb Smirnoff #include "netdissect.h"
31b0453382SBill Fenner #include "addrtoname.h"
32685295f4SBill Fenner #include "ethertype.h"
33a90e161bSBill Fenner #include "extract.h"
34685295f4SBill Fenner #include "chdlc.h"
35*ee67461eSJoseph Mingrone #include "nlpid.h"
36b0453382SBill Fenner 
373c602fabSXin LI static void chdlc_slarp_print(netdissect_options *, const u_char *, u_int);
38b0453382SBill Fenner 
393c602fabSXin LI static const struct tok chdlc_cast_values[] = {
40b5bfcb5dSMax Laier     { CHDLC_UNICAST, "unicast" },
41b5bfcb5dSMax Laier     { CHDLC_BCAST, "bcast" },
42b5bfcb5dSMax Laier     { 0, NULL}
43b5bfcb5dSMax Laier };
44b5bfcb5dSMax Laier 
45b5bfcb5dSMax Laier 
46b0453382SBill Fenner /* Standard CHDLC printer */
47*ee67461eSJoseph Mingrone void
48*ee67461eSJoseph Mingrone chdlc_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, const u_char *p)
49b0453382SBill Fenner {
50*ee67461eSJoseph Mingrone 	ndo->ndo_protocol = "chdlc";
51*ee67461eSJoseph Mingrone 	ndo->ndo_ll_hdr_len += chdlc_print(ndo, p, h->len);
522ebc47dbSSam Leffler }
532ebc47dbSSam Leffler 
542ebc47dbSSam Leffler u_int
55*ee67461eSJoseph Mingrone chdlc_print(netdissect_options *ndo, const u_char *p, u_int length)
568bdc5a62SPatrick Kelsey {
572ebc47dbSSam Leffler 	u_int proto;
580bff6a5aSEd Maste 	const u_char *bp = p;
59a90e161bSBill Fenner 
60*ee67461eSJoseph Mingrone 	ndo->ndo_protocol = "chdlc";
610bff6a5aSEd Maste 	if (length < CHDLC_HDRLEN)
620bff6a5aSEd Maste 		goto trunc;
63*ee67461eSJoseph Mingrone 	proto = GET_BE_U_2(p + 2);
643c602fabSXin LI 	if (ndo->ndo_eflag) {
65*ee67461eSJoseph Mingrone                 ND_PRINT("%s, ethertype %s (0x%04x), length %u: ",
66*ee67461eSJoseph Mingrone                        tok2str(chdlc_cast_values, "0x%02x", GET_U_1(p)),
67b5bfcb5dSMax Laier                        tok2str(ethertype_values, "Unknown", proto),
68b5bfcb5dSMax Laier                        proto,
69*ee67461eSJoseph Mingrone                        length);
70b0453382SBill Fenner 	}
71b0453382SBill Fenner 
72b0453382SBill Fenner 	length -= CHDLC_HDRLEN;
73b5bfcb5dSMax Laier 	p += CHDLC_HDRLEN;
74b5bfcb5dSMax Laier 
75b0453382SBill Fenner 	switch (proto) {
76b0453382SBill Fenner 	case ETHERTYPE_IP:
773c602fabSXin LI 		ip_print(ndo, p, length);
78b0453382SBill Fenner 		break;
79b0453382SBill Fenner 	case ETHERTYPE_IPV6:
803c602fabSXin LI 		ip6_print(ndo, p, length);
81b0453382SBill Fenner 		break;
82b0453382SBill Fenner 	case CHDLC_TYPE_SLARP:
833c602fabSXin LI 		chdlc_slarp_print(ndo, p, length);
84b0453382SBill Fenner 		break;
855b0fe478SBruce M Simpson         case ETHERTYPE_MPLS:
865b0fe478SBruce M Simpson         case ETHERTYPE_MPLS_MULTI:
873c602fabSXin LI                 mpls_print(ndo, p, length);
885b0fe478SBruce M Simpson 		break;
895b0fe478SBruce M Simpson         case ETHERTYPE_ISO:
905b0fe478SBruce M Simpson                 /* is the fudge byte set ? lets verify by spotting ISO headers */
910bff6a5aSEd Maste                 if (length < 2)
920bff6a5aSEd Maste                     goto trunc;
93*ee67461eSJoseph Mingrone                 if (GET_U_1(p + 1) == NLPID_CLNP ||
94*ee67461eSJoseph Mingrone                     GET_U_1(p + 1) == NLPID_ESIS ||
95*ee67461eSJoseph Mingrone                     GET_U_1(p + 1) == NLPID_ISIS)
960bff6a5aSEd Maste                     isoclns_print(ndo, p + 1, length - 1);
975b0fe478SBruce M Simpson                 else
980bff6a5aSEd Maste                     isoclns_print(ndo, p, length);
995b0fe478SBruce M Simpson                 break;
1005b0fe478SBruce M Simpson 	default:
1013c602fabSXin LI                 if (!ndo->ndo_eflag)
102*ee67461eSJoseph Mingrone                         ND_PRINT("unknown CHDLC protocol (0x%04x)", proto);
1035b0fe478SBruce M Simpson                 break;
104b0453382SBill Fenner 	}
1055b0fe478SBruce M Simpson 
1065b0fe478SBruce M Simpson 	return (CHDLC_HDRLEN);
1070bff6a5aSEd Maste 
1080bff6a5aSEd Maste trunc:
109*ee67461eSJoseph Mingrone 	nd_print_trunc(ndo);
110*ee67461eSJoseph Mingrone 	return (ND_BYTES_AVAILABLE_AFTER(bp));
111b0453382SBill Fenner }
112b0453382SBill Fenner 
113f4d0c64aSSam Leffler /*
114f4d0c64aSSam Leffler  * The fixed-length portion of a SLARP packet.
115f4d0c64aSSam Leffler  */
116b0453382SBill Fenner struct cisco_slarp {
117*ee67461eSJoseph Mingrone 	nd_uint32_t code;
118b0453382SBill Fenner #define SLARP_REQUEST	0
119b0453382SBill Fenner #define SLARP_REPLY	1
120b0453382SBill Fenner #define SLARP_KEEPALIVE	2
121b0453382SBill Fenner 	union {
122b0453382SBill Fenner 		struct {
1233c602fabSXin LI 			uint8_t addr[4];
1243c602fabSXin LI 			uint8_t mask[4];
125b0453382SBill Fenner 		} addr;
126b0453382SBill Fenner 		struct {
127*ee67461eSJoseph Mingrone 			nd_uint32_t myseq;
128*ee67461eSJoseph Mingrone 			nd_uint32_t yourseq;
129*ee67461eSJoseph Mingrone 			nd_uint16_t rel;
130b0453382SBill Fenner 		} keep;
131b0453382SBill Fenner 	} un;
132b0453382SBill Fenner };
133b0453382SBill Fenner 
134f4d0c64aSSam Leffler #define SLARP_MIN_LEN	14
135f4d0c64aSSam Leffler #define SLARP_MAX_LEN	18
136b0453382SBill Fenner 
137b0453382SBill Fenner static void
1383c602fabSXin LI chdlc_slarp_print(netdissect_options *ndo, const u_char *cp, u_int length)
139b0453382SBill Fenner {
140a90e161bSBill Fenner 	const struct cisco_slarp *slarp;
141f4d0c64aSSam Leffler         u_int sec,min,hrs,days;
142b0453382SBill Fenner 
143*ee67461eSJoseph Mingrone 	ndo->ndo_protocol = "chdlc_slarp";
144*ee67461eSJoseph Mingrone 	ND_PRINT("SLARP (length: %u), ",length);
145f4d0c64aSSam Leffler 	if (length < SLARP_MIN_LEN)
1465b0fe478SBruce M Simpson 		goto trunc;
147b0453382SBill Fenner 
148a90e161bSBill Fenner 	slarp = (const struct cisco_slarp *)cp;
149*ee67461eSJoseph Mingrone 	ND_TCHECK_LEN(slarp, SLARP_MIN_LEN);
150*ee67461eSJoseph Mingrone 	switch (GET_BE_U_4(slarp->code)) {
151b0453382SBill Fenner 	case SLARP_REQUEST:
152*ee67461eSJoseph Mingrone 		ND_PRINT("request");
153f4d0c64aSSam Leffler 		/*
154f4d0c64aSSam Leffler 		 * At least according to William "Chops" Westfield's
155f4d0c64aSSam Leffler 		 * message in
156f4d0c64aSSam Leffler 		 *
157*ee67461eSJoseph Mingrone 		 *	https://web.archive.org/web/20190725151313/www.nethelp.no/net/cisco-hdlc.txt
158f4d0c64aSSam Leffler 		 *
159f4d0c64aSSam Leffler 		 * the address and mask aren't used in requests -
160f4d0c64aSSam Leffler 		 * they're just zero.
161f4d0c64aSSam Leffler 		 */
162b0453382SBill Fenner 		break;
163b0453382SBill Fenner 	case SLARP_REPLY:
164*ee67461eSJoseph Mingrone 		ND_PRINT("reply %s/%s",
165*ee67461eSJoseph Mingrone 			GET_IPADDR_STRING(slarp->un.addr.addr),
166*ee67461eSJoseph Mingrone 			GET_IPADDR_STRING(slarp->un.addr.mask));
167b0453382SBill Fenner 		break;
168b0453382SBill Fenner 	case SLARP_KEEPALIVE:
169*ee67461eSJoseph Mingrone 		ND_PRINT("keepalive: mineseen=0x%08x, yourseen=0x%08x, reliability=0x%04x",
170*ee67461eSJoseph Mingrone                        GET_BE_U_4(slarp->un.keep.myseq),
171*ee67461eSJoseph Mingrone                        GET_BE_U_4(slarp->un.keep.yourseq),
172*ee67461eSJoseph Mingrone                        GET_BE_U_2(slarp->un.keep.rel));
173f4d0c64aSSam Leffler 
174f4d0c64aSSam Leffler                 if (length >= SLARP_MAX_LEN) { /* uptime-stamp is optional */
175f4d0c64aSSam Leffler                         cp += SLARP_MIN_LEN;
176*ee67461eSJoseph Mingrone                         sec = GET_BE_U_4(cp) / 1000;
177f4d0c64aSSam Leffler                         min = sec / 60; sec -= min * 60;
178f4d0c64aSSam Leffler                         hrs = min / 60; min -= hrs * 60;
179f4d0c64aSSam Leffler                         days = hrs / 24; hrs -= days * 24;
180*ee67461eSJoseph Mingrone                         ND_PRINT(", link uptime=%ud%uh%um%us",days,hrs,min,sec);
181f4d0c64aSSam Leffler                 }
182b0453382SBill Fenner 		break;
183b0453382SBill Fenner 	default:
184*ee67461eSJoseph Mingrone 		ND_PRINT("0x%02x unknown", GET_BE_U_4(slarp->code));
1853c602fabSXin LI                 if (ndo->ndo_vflag <= 1)
1863c602fabSXin LI                     print_unknown_data(ndo,cp+4,"\n\t",length-4);
187b0453382SBill Fenner 		break;
188b0453382SBill Fenner 	}
189b0453382SBill Fenner 
1903c602fabSXin LI 	if (SLARP_MAX_LEN < length && ndo->ndo_vflag)
191*ee67461eSJoseph Mingrone 		ND_PRINT(", (trailing junk: %u bytes)", length - SLARP_MAX_LEN);
1923c602fabSXin LI         if (ndo->ndo_vflag > 1)
1933c602fabSXin LI             print_unknown_data(ndo,cp+4,"\n\t",length-4);
1945b0fe478SBruce M Simpson 	return;
1955b0fe478SBruce M Simpson 
1965b0fe478SBruce M Simpson trunc:
197*ee67461eSJoseph Mingrone 	nd_print_trunc(ndo);
198b0453382SBill Fenner }
199