xref: /netbsd-src/external/bsd/tcpdump/dist/print-chdlc.c (revision c42dbd0ed2e61fe6eda8590caa852ccf34719964)
1 /*
2  * Copyright (c) 1990, 1991, 1993, 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 
22 #include <sys/cdefs.h>
23 #ifndef lint
24 __RCSID("$NetBSD: print-chdlc.c,v 1.10 2023/08/17 20:19:40 christos Exp $");
25 #endif
26 
27 /* \summary: Cisco HDLC printer */
28 
29 #ifdef HAVE_CONFIG_H
30 #include <config.h>
31 #endif
32 
33 #include "netdissect-stdinc.h"
34 
35 #include "netdissect.h"
36 #include "addrtoname.h"
37 #include "ethertype.h"
38 #include "extract.h"
39 #include "chdlc.h"
40 #include "nlpid.h"
41 
42 static void chdlc_slarp_print(netdissect_options *, const u_char *, u_int);
43 
44 static const struct tok chdlc_cast_values[] = {
45     { CHDLC_UNICAST, "unicast" },
46     { CHDLC_BCAST, "bcast" },
47     { 0, NULL}
48 };
49 
50 
51 /* Standard CHDLC printer */
52 void
53 chdlc_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, const u_char *p)
54 {
55 	ndo->ndo_protocol = "chdlc";
56 	ndo->ndo_ll_hdr_len += chdlc_print(ndo, p, h->len);
57 }
58 
59 u_int
60 chdlc_print(netdissect_options *ndo, const u_char *p, u_int length)
61 {
62 	u_int proto;
63 	const u_char *bp = p;
64 
65 	ndo->ndo_protocol = "chdlc";
66 	if (length < CHDLC_HDRLEN)
67 		goto trunc;
68 	proto = GET_BE_U_2(p + 2);
69 	if (ndo->ndo_eflag) {
70                 ND_PRINT("%s, ethertype %s (0x%04x), length %u: ",
71                        tok2str(chdlc_cast_values, "0x%02x", GET_U_1(p)),
72                        tok2str(ethertype_values, "Unknown", proto),
73                        proto,
74                        length);
75 	}
76 
77 	length -= CHDLC_HDRLEN;
78 	p += CHDLC_HDRLEN;
79 
80 	switch (proto) {
81 	case ETHERTYPE_IP:
82 		ip_print(ndo, p, length);
83 		break;
84 	case ETHERTYPE_IPV6:
85 		ip6_print(ndo, p, length);
86 		break;
87 	case CHDLC_TYPE_SLARP:
88 		chdlc_slarp_print(ndo, p, length);
89 		break;
90         case ETHERTYPE_MPLS:
91         case ETHERTYPE_MPLS_MULTI:
92                 mpls_print(ndo, p, length);
93 		break;
94         case ETHERTYPE_ISO:
95                 /* is the fudge byte set ? lets verify by spotting ISO headers */
96                 if (length < 2)
97                     goto trunc;
98                 if (GET_U_1(p + 1) == NLPID_CLNP ||
99                     GET_U_1(p + 1) == NLPID_ESIS ||
100                     GET_U_1(p + 1) == NLPID_ISIS)
101                     isoclns_print(ndo, p + 1, length - 1);
102                 else
103                     isoclns_print(ndo, p, length);
104                 break;
105 	default:
106                 if (!ndo->ndo_eflag)
107                         ND_PRINT("unknown CHDLC protocol (0x%04x)", proto);
108                 break;
109 	}
110 
111 	return (CHDLC_HDRLEN);
112 
113 trunc:
114 	nd_print_trunc(ndo);
115 	return (ND_BYTES_AVAILABLE_AFTER(bp));
116 }
117 
118 /*
119  * The fixed-length portion of a SLARP packet.
120  */
121 struct cisco_slarp {
122 	nd_uint32_t code;
123 #define SLARP_REQUEST	0
124 #define SLARP_REPLY	1
125 #define SLARP_KEEPALIVE	2
126 	union {
127 		struct {
128 			uint8_t addr[4];
129 			uint8_t mask[4];
130 		} addr;
131 		struct {
132 			nd_uint32_t myseq;
133 			nd_uint32_t yourseq;
134 			nd_uint16_t rel;
135 		} keep;
136 	} un;
137 };
138 
139 #define SLARP_MIN_LEN	14
140 #define SLARP_MAX_LEN	18
141 
142 static void
143 chdlc_slarp_print(netdissect_options *ndo, const u_char *cp, u_int length)
144 {
145 	const struct cisco_slarp *slarp;
146         u_int sec,min,hrs,days;
147 
148 	ndo->ndo_protocol = "chdlc_slarp";
149 	ND_PRINT("SLARP (length: %u), ",length);
150 	if (length < SLARP_MIN_LEN)
151 		goto trunc;
152 
153 	slarp = (const struct cisco_slarp *)cp;
154 	ND_TCHECK_LEN(slarp, SLARP_MIN_LEN);
155 	switch (GET_BE_U_4(slarp->code)) {
156 	case SLARP_REQUEST:
157 		ND_PRINT("request");
158 		/*
159 		 * At least according to William "Chops" Westfield's
160 		 * message in
161 		 *
162 		 *	https://web.archive.org/web/20190725151313/www.nethelp.no/net/cisco-hdlc.txt
163 		 *
164 		 * the address and mask aren't used in requests -
165 		 * they're just zero.
166 		 */
167 		break;
168 	case SLARP_REPLY:
169 		ND_PRINT("reply %s/%s",
170 			GET_IPADDR_STRING(slarp->un.addr.addr),
171 			GET_IPADDR_STRING(slarp->un.addr.mask));
172 		break;
173 	case SLARP_KEEPALIVE:
174 		ND_PRINT("keepalive: mineseen=0x%08x, yourseen=0x%08x, reliability=0x%04x",
175                        GET_BE_U_4(slarp->un.keep.myseq),
176                        GET_BE_U_4(slarp->un.keep.yourseq),
177                        GET_BE_U_2(slarp->un.keep.rel));
178 
179                 if (length >= SLARP_MAX_LEN) { /* uptime-stamp is optional */
180                         cp += SLARP_MIN_LEN;
181                         sec = GET_BE_U_4(cp) / 1000;
182                         min = sec / 60; sec -= min * 60;
183                         hrs = min / 60; min -= hrs * 60;
184                         days = hrs / 24; hrs -= days * 24;
185                         ND_PRINT(", link uptime=%ud%uh%um%us",days,hrs,min,sec);
186                 }
187 		break;
188 	default:
189 		ND_PRINT("0x%02x unknown", GET_BE_U_4(slarp->code));
190                 if (ndo->ndo_vflag <= 1)
191                     print_unknown_data(ndo,cp+4,"\n\t",length-4);
192 		break;
193 	}
194 
195 	if (SLARP_MAX_LEN < length && ndo->ndo_vflag)
196 		ND_PRINT(", (trailing junk: %u bytes)", length - SLARP_MAX_LEN);
197         if (ndo->ndo_vflag > 1)
198             print_unknown_data(ndo,cp+4,"\n\t",length-4);
199 	return;
200 
201 trunc:
202 	nd_print_trunc(ndo);
203 }
204