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