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