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