]> The Tcpdump Group git mirrors - tcpdump/blob - print-egp.c
More bounds checking when fetching addresses and converting to strings.
[tcpdump] / print-egp.c
1 /*
2 * Copyright (c) 1991, 1992, 1993, 1994, 1995, 1996
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Lawrence Berkeley Laboratory,
11 * Berkeley, CA. The name of the University may not be used to
12 * endorse or promote products derived from this software without
13 * specific prior written permission.
14 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
17 *
18 * Initial contribution from Jeff Honig (jch@MITCHELL.CIT.CORNELL.EDU).
19 */
20
21 /* \summary: Exterior Gateway Protocol (EGP) printer */
22
23 /* specification: RFC 827 */
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <string.h>
30
31 #include "netdissect-stdinc.h"
32
33 #include "netdissect.h"
34 #include "addrtoname.h"
35 #include "extract.h"
36
37 struct egp_packet {
38 nd_uint8_t egp_version;
39 #define EGP_VERSION 2
40 nd_uint8_t egp_type;
41 #define EGPT_ACQUIRE 3
42 #define EGPT_REACH 5
43 #define EGPT_POLL 2
44 #define EGPT_UPDATE 1
45 #define EGPT_ERROR 8
46 nd_uint8_t egp_code;
47 #define EGPC_REQUEST 0
48 #define EGPC_CONFIRM 1
49 #define EGPC_REFUSE 2
50 #define EGPC_CEASE 3
51 #define EGPC_CEASEACK 4
52 #define EGPC_HELLO 0
53 #define EGPC_HEARDU 1
54 nd_uint8_t egp_status;
55 #define EGPS_UNSPEC 0
56 #define EGPS_ACTIVE 1
57 #define EGPS_PASSIVE 2
58 #define EGPS_NORES 3
59 #define EGPS_ADMIN 4
60 #define EGPS_GODOWN 5
61 #define EGPS_PARAM 6
62 #define EGPS_PROTO 7
63 #define EGPS_INDET 0
64 #define EGPS_UP 1
65 #define EGPS_DOWN 2
66 #define EGPS_UNSOL 0x80
67 nd_uint16_t egp_checksum;
68 nd_uint16_t egp_as;
69 nd_uint16_t egp_sequence;
70 union {
71 nd_uint16_t egpu_hello;
72 nd_uint8_t egpu_gws[2];
73 nd_uint16_t egpu_reason;
74 #define EGPR_UNSPEC 0
75 #define EGPR_BADHEAD 1
76 #define EGPR_BADDATA 2
77 #define EGPR_NOREACH 3
78 #define EGPR_XSPOLL 4
79 #define EGPR_NORESP 5
80 #define EGPR_UVERSION 6
81 } egp_handg;
82 #define egp_hello egp_handg.egpu_hello
83 #define egp_intgw egp_handg.egpu_gws[0]
84 #define egp_extgw egp_handg.egpu_gws[1]
85 #define egp_reason egp_handg.egpu_reason
86 union {
87 nd_uint16_t egpu_poll;
88 nd_ipv4 egpu_sourcenet;
89 } egp_pands;
90 #define egp_poll egp_pands.egpu_poll
91 #define egp_sourcenet egp_pands.egpu_sourcenet
92 };
93
94 static const char *egp_acquire_codes[] = {
95 "request",
96 "confirm",
97 "refuse",
98 "cease",
99 "cease_ack"
100 };
101
102 static const char *egp_acquire_status[] = {
103 "unspecified",
104 "active_mode",
105 "passive_mode",
106 "insufficient_resources",
107 "administratively_prohibited",
108 "going_down",
109 "parameter_violation",
110 "protocol_violation"
111 };
112
113 static const char *egp_reach_codes[] = {
114 "hello",
115 "i-h-u"
116 };
117
118 static const char *egp_status_updown[] = {
119 "indeterminate",
120 "up",
121 "down"
122 };
123
124 static const char *egp_reasons[] = {
125 "unspecified",
126 "bad_EGP_header_format",
127 "bad_EGP_data_field_format",
128 "reachability_info_unavailable",
129 "excessive_polling_rate",
130 "no_response",
131 "unsupported_version"
132 };
133
134 static void
135 egpnr_print(netdissect_options *ndo,
136 const struct egp_packet *egp, u_int length)
137 {
138 const uint8_t *cp;
139 uint32_t addr;
140 uint32_t net;
141 u_int netlen;
142 u_int gateways, distances, networks;
143 u_int intgw, extgw, t_gateways;
144 const char *comma;
145
146 addr = GET_IPV4_TO_NETWORK_ORDER(egp->egp_sourcenet);
147 if (IN_CLASSA(addr)) {
148 net = addr & IN_CLASSA_NET;
149 netlen = 1;
150 } else if (IN_CLASSB(addr)) {
151 net = addr & IN_CLASSB_NET;
152 netlen = 2;
153 } else if (IN_CLASSC(addr)) {
154 net = addr & IN_CLASSC_NET;
155 netlen = 3;
156 } else {
157 net = 0;
158 netlen = 0;
159 }
160 cp = (const uint8_t *)(egp + 1);
161 length -= sizeof(*egp);
162
163 intgw = GET_U_1(egp->egp_intgw);
164 extgw = GET_U_1(egp->egp_extgw);
165 t_gateways = intgw + extgw;
166 for (gateways = 0; gateways < t_gateways; ++gateways) {
167 /* Pickup host part of gateway address */
168 addr = 0;
169 if (length < 4 - netlen)
170 goto trunc;
171 ND_TCHECK_LEN(cp, 4 - netlen);
172 switch (netlen) {
173
174 case 1:
175 addr = GET_U_1(cp);
176 cp++;
177 /* fall through */
178 case 2:
179 addr = (addr << 8) | GET_U_1(cp);
180 cp++;
181 /* fall through */
182 case 3:
183 addr = (addr << 8) | GET_U_1(cp);
184 cp++;
185 break;
186 }
187 addr |= net;
188 length -= 4 - netlen;
189 if (length < 1)
190 goto trunc;
191 ND_TCHECK_1(cp);
192 distances = GET_U_1(cp);
193 cp++;
194 length--;
195 ND_PRINT(" %s %s ",
196 gateways < intgw ? "int" : "ext",
197 ipaddr_string(ndo, (const u_char *)&addr));
198
199 comma = "";
200 ND_PRINT("(");
201 while (distances != 0) {
202 if (length < 2)
203 goto trunc;
204 ND_TCHECK_2(cp);
205 ND_PRINT("%sd%u:", comma, GET_U_1(cp));
206 cp++;
207 comma = ", ";
208 networks = GET_U_1(cp);
209 cp++;
210 length -= 2;
211 while (networks != 0) {
212 /* Pickup network number */
213 if (length < 1)
214 goto trunc;
215 ND_TCHECK_1(cp);
216 addr = ((uint32_t) GET_U_1(cp)) << 24;
217 cp++;
218 length--;
219 if (IN_CLASSB(addr)) {
220 if (length < 1)
221 goto trunc;
222 ND_TCHECK_1(cp);
223 addr |= ((uint32_t) GET_U_1(cp)) << 16;
224 cp++;
225 length--;
226 } else if (!IN_CLASSA(addr)) {
227 if (length < 2)
228 goto trunc;
229 ND_TCHECK_2(cp);
230 addr |= ((uint32_t) GET_U_1(cp)) << 16;
231 cp++;
232 addr |= ((uint32_t) GET_U_1(cp)) << 8;
233 cp++;
234 length -= 2;
235 }
236 ND_PRINT(" %s", ipaddr_string(ndo, (const u_char *)&addr));
237 networks--;
238 }
239 distances--;
240 }
241 ND_PRINT(")");
242 }
243 return;
244 trunc:
245 nd_print_trunc(ndo);
246 }
247
248 void
249 egp_print(netdissect_options *ndo,
250 const uint8_t *bp, u_int length)
251 {
252 const struct egp_packet *egp;
253 u_int version;
254 u_int type;
255 u_int code;
256 u_int status;
257
258 ndo->ndo_protocol = "egp";
259 egp = (const struct egp_packet *)bp;
260 if (length < sizeof(*egp) || !ND_TTEST_SIZE(egp)) {
261 nd_print_trunc(ndo);
262 return;
263 }
264
265 version = GET_U_1(egp->egp_version);
266 if (!ndo->ndo_vflag) {
267 ND_PRINT("EGPv%u, AS %u, seq %u, length %u",
268 version,
269 GET_BE_U_2(egp->egp_as),
270 GET_BE_U_2(egp->egp_sequence),
271 length);
272 return;
273 } else
274 ND_PRINT("EGPv%u, length %u",
275 version,
276 length);
277
278 if (version != EGP_VERSION) {
279 ND_PRINT("[version %u]", version);
280 return;
281 }
282
283 type = GET_U_1(egp->egp_type);
284 code = GET_U_1(egp->egp_code);
285 status = GET_U_1(egp->egp_status);
286
287 switch (type) {
288 case EGPT_ACQUIRE:
289 ND_PRINT(" acquire");
290 switch (code) {
291 case EGPC_REQUEST:
292 case EGPC_CONFIRM:
293 ND_PRINT(" %s", egp_acquire_codes[code]);
294 switch (status) {
295 case EGPS_UNSPEC:
296 case EGPS_ACTIVE:
297 case EGPS_PASSIVE:
298 ND_PRINT(" %s", egp_acquire_status[status]);
299 break;
300
301 default:
302 ND_PRINT(" [status %u]", status);
303 break;
304 }
305 ND_PRINT(" hello:%u poll:%u",
306 GET_BE_U_2(egp->egp_hello),
307 GET_BE_U_2(egp->egp_poll));
308 break;
309
310 case EGPC_REFUSE:
311 case EGPC_CEASE:
312 case EGPC_CEASEACK:
313 ND_PRINT(" %s", egp_acquire_codes[code]);
314 switch (status ) {
315 case EGPS_UNSPEC:
316 case EGPS_NORES:
317 case EGPS_ADMIN:
318 case EGPS_GODOWN:
319 case EGPS_PARAM:
320 case EGPS_PROTO:
321 ND_PRINT(" %s", egp_acquire_status[status]);
322 break;
323
324 default:
325 ND_PRINT("[status %u]", status);
326 break;
327 }
328 break;
329
330 default:
331 ND_PRINT("[code %u]", code);
332 break;
333 }
334 break;
335
336 case EGPT_REACH:
337 switch (code) {
338
339 case EGPC_HELLO:
340 case EGPC_HEARDU:
341 ND_PRINT(" %s", egp_reach_codes[code]);
342 if (status <= EGPS_DOWN)
343 ND_PRINT(" state:%s", egp_status_updown[status]);
344 else
345 ND_PRINT(" [status %u]", status);
346 break;
347
348 default:
349 ND_PRINT("[reach code %u]", code);
350 break;
351 }
352 break;
353
354 case EGPT_POLL:
355 ND_PRINT(" poll");
356 if (status <= EGPS_DOWN)
357 ND_PRINT(" state:%s", egp_status_updown[status]);
358 else
359 ND_PRINT(" [status %u]", status);
360 ND_PRINT(" net:%s", GET_IPADDR_STRING(egp->egp_sourcenet));
361 break;
362
363 case EGPT_UPDATE:
364 ND_PRINT(" update");
365 if (status & EGPS_UNSOL) {
366 status &= ~EGPS_UNSOL;
367 ND_PRINT(" unsolicited");
368 }
369 if (status <= EGPS_DOWN)
370 ND_PRINT(" state:%s", egp_status_updown[status]);
371 else
372 ND_PRINT(" [status %u]", status);
373 ND_PRINT(" %s int %u ext %u",
374 GET_IPADDR_STRING(egp->egp_sourcenet),
375 GET_U_1(egp->egp_intgw),
376 GET_U_1(egp->egp_extgw));
377 if (ndo->ndo_vflag)
378 egpnr_print(ndo, egp, length);
379 break;
380
381 case EGPT_ERROR:
382 ND_PRINT(" error");
383 if (status <= EGPS_DOWN)
384 ND_PRINT(" state:%s", egp_status_updown[status]);
385 else
386 ND_PRINT(" [status %u]", status);
387
388 if (GET_BE_U_2(egp->egp_reason) <= EGPR_UVERSION)
389 ND_PRINT(" %s",
390 egp_reasons[GET_BE_U_2(egp->egp_reason)]);
391 else
392 ND_PRINT(" [reason %u]", GET_BE_U_2(egp->egp_reason));
393 break;
394
395 default:
396 ND_PRINT("[type %u]", type);
397 break;
398 }
399 }