xref: /netbsd-src/external/bsd/tcpdump/dist/print-chdlc.c (revision 4724848cf0da353df257f730694b7882798e5daf)
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.9 2017/09/08 14:01:13 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 
41 static void chdlc_slarp_print(netdissect_options *, const u_char *, u_int);
42 
43 static const struct tok chdlc_cast_values[] = {
44     { CHDLC_UNICAST, "unicast" },
45     { CHDLC_BCAST, "bcast" },
46     { 0, NULL}
47 };
48 
49 
50 /* Standard CHDLC printer */
51 u_int
52 chdlc_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, register const u_char *p)
53 {
54 	return chdlc_print(ndo, p, h->len);
55 }
56 
57 u_int
58 chdlc_print(netdissect_options *ndo, register const u_char *p, u_int length)
59 {
60 	u_int proto;
61 	const u_char *bp = p;
62 
63 	if (length < CHDLC_HDRLEN)
64 		goto trunc;
65 	ND_TCHECK2(*p, CHDLC_HDRLEN);
66 	proto = EXTRACT_16BITS(&p[2]);
67 	if (ndo->ndo_eflag) {
68                 ND_PRINT((ndo, "%s, ethertype %s (0x%04x), length %u: ",
69                        tok2str(chdlc_cast_values, "0x%02x", p[0]),
70                        tok2str(ethertype_values, "Unknown", proto),
71                        proto,
72                        length));
73 	}
74 
75 	length -= CHDLC_HDRLEN;
76 	p += CHDLC_HDRLEN;
77 
78 	switch (proto) {
79 	case ETHERTYPE_IP:
80 		ip_print(ndo, p, length);
81 		break;
82 	case ETHERTYPE_IPV6:
83 		ip6_print(ndo, p, length);
84 		break;
85 	case CHDLC_TYPE_SLARP:
86 		chdlc_slarp_print(ndo, p, length);
87 		break;
88 #if 0
89 	case CHDLC_TYPE_CDP:
90 		chdlc_cdp_print(p, length);
91 		break;
92 #endif
93         case ETHERTYPE_MPLS:
94         case ETHERTYPE_MPLS_MULTI:
95                 mpls_print(ndo, p, length);
96 		break;
97         case ETHERTYPE_ISO:
98                 /* is the fudge byte set ? lets verify by spotting ISO headers */
99                 if (length < 2)
100                     goto trunc;
101                 ND_TCHECK_16BITS(p);
102                 if (*(p+1) == 0x81 ||
103                     *(p+1) == 0x82 ||
104                     *(p+1) == 0x83)
105                     isoclns_print(ndo, p + 1, length - 1);
106                 else
107                     isoclns_print(ndo, p, length);
108                 break;
109 	default:
110                 if (!ndo->ndo_eflag)
111                         ND_PRINT((ndo, "unknown CHDLC protocol (0x%04x)", proto));
112                 break;
113 	}
114 
115 	return (CHDLC_HDRLEN);
116 
117 trunc:
118 	ND_PRINT((ndo, "[|chdlc]"));
119 	return ndo->ndo_snapend - bp;
120 }
121 
122 /*
123  * The fixed-length portion of a SLARP packet.
124  */
125 struct cisco_slarp {
126 	uint8_t code[4];
127 #define SLARP_REQUEST	0
128 #define SLARP_REPLY	1
129 #define SLARP_KEEPALIVE	2
130 	union {
131 		struct {
132 			uint8_t addr[4];
133 			uint8_t mask[4];
134 		} addr;
135 		struct {
136 			uint8_t myseq[4];
137 			uint8_t yourseq[4];
138 			uint8_t rel[2];
139 		} keep;
140 	} un;
141 };
142 
143 #define SLARP_MIN_LEN	14
144 #define SLARP_MAX_LEN	18
145 
146 static void
147 chdlc_slarp_print(netdissect_options *ndo, const u_char *cp, u_int length)
148 {
149 	const struct cisco_slarp *slarp;
150         u_int sec,min,hrs,days;
151 
152 	ND_PRINT((ndo, "SLARP (length: %u), ",length));
153 	if (length < SLARP_MIN_LEN)
154 		goto trunc;
155 
156 	slarp = (const struct cisco_slarp *)cp;
157 	ND_TCHECK2(*slarp, SLARP_MIN_LEN);
158 	switch (EXTRACT_32BITS(&slarp->code)) {
159 	case SLARP_REQUEST:
160 		ND_PRINT((ndo, "request"));
161 		/*
162 		 * At least according to William "Chops" Westfield's
163 		 * message in
164 		 *
165 		 *	http://www.nethelp.no/net/cisco-hdlc.txt
166 		 *
167 		 * the address and mask aren't used in requests -
168 		 * they're just zero.
169 		 */
170 		break;
171 	case SLARP_REPLY:
172 		ND_PRINT((ndo, "reply %s/%s",
173 			ipaddr_string(ndo, &slarp->un.addr.addr),
174 			ipaddr_string(ndo, &slarp->un.addr.mask)));
175 		break;
176 	case SLARP_KEEPALIVE:
177 		ND_PRINT((ndo, "keepalive: mineseen=0x%08x, yourseen=0x%08x, reliability=0x%04x",
178                        EXTRACT_32BITS(&slarp->un.keep.myseq),
179                        EXTRACT_32BITS(&slarp->un.keep.yourseq),
180                        EXTRACT_16BITS(&slarp->un.keep.rel)));
181 
182                 if (length >= SLARP_MAX_LEN) { /* uptime-stamp is optional */
183                         cp += SLARP_MIN_LEN;
184                         ND_TCHECK2(*cp, 4);
185                         sec = EXTRACT_32BITS(cp) / 1000;
186                         min = sec / 60; sec -= min * 60;
187                         hrs = min / 60; min -= hrs * 60;
188                         days = hrs / 24; hrs -= days * 24;
189                         ND_PRINT((ndo, ", link uptime=%ud%uh%um%us",days,hrs,min,sec));
190                 }
191 		break;
192 	default:
193 		ND_PRINT((ndo, "0x%02x unknown", EXTRACT_32BITS(&slarp->code)));
194                 if (ndo->ndo_vflag <= 1)
195                     print_unknown_data(ndo,cp+4,"\n\t",length-4);
196 		break;
197 	}
198 
199 	if (SLARP_MAX_LEN < length && ndo->ndo_vflag)
200 		ND_PRINT((ndo, ", (trailing junk: %d bytes)", length - SLARP_MAX_LEN));
201         if (ndo->ndo_vflag > 1)
202             print_unknown_data(ndo,cp+4,"\n\t",length-4);
203 	return;
204 
205 trunc:
206 	ND_PRINT((ndo, "[|slarp]"));
207 }
208 
209 
210 /*
211  * Local Variables:
212  * c-style: whitesmith
213  * c-basic-offset: 8
214  * End:
215  */
216