]> The Tcpdump Group git mirrors - tcpdump/blob - print-egp.c
51d78599abb2b738ec3fe5931c7aed27dd337bee
[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 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <netdissect-stdinc.h>
28
29 #include "netdissect.h"
30 #include "addrtoname.h"
31 #include "extract.h"
32
33 struct egp_packet {
34 uint8_t egp_version;
35 #define EGP_VERSION 2
36 uint8_t egp_type;
37 #define EGPT_ACQUIRE 3
38 #define EGPT_REACH 5
39 #define EGPT_POLL 2
40 #define EGPT_UPDATE 1
41 #define EGPT_ERROR 8
42 uint8_t egp_code;
43 #define EGPC_REQUEST 0
44 #define EGPC_CONFIRM 1
45 #define EGPC_REFUSE 2
46 #define EGPC_CEASE 3
47 #define EGPC_CEASEACK 4
48 #define EGPC_HELLO 0
49 #define EGPC_HEARDU 1
50 uint8_t egp_status;
51 #define EGPS_UNSPEC 0
52 #define EGPS_ACTIVE 1
53 #define EGPS_PASSIVE 2
54 #define EGPS_NORES 3
55 #define EGPS_ADMIN 4
56 #define EGPS_GODOWN 5
57 #define EGPS_PARAM 6
58 #define EGPS_PROTO 7
59 #define EGPS_INDET 0
60 #define EGPS_UP 1
61 #define EGPS_DOWN 2
62 #define EGPS_UNSOL 0x80
63 uint16_t egp_checksum;
64 uint16_t egp_as;
65 uint16_t egp_sequence;
66 union {
67 uint16_t egpu_hello;
68 uint8_t egpu_gws[2];
69 uint16_t egpu_reason;
70 #define EGPR_UNSPEC 0
71 #define EGPR_BADHEAD 1
72 #define EGPR_BADDATA 2
73 #define EGPR_NOREACH 3
74 #define EGPR_XSPOLL 4
75 #define EGPR_NORESP 5
76 #define EGPR_UVERSION 6
77 } egp_handg;
78 #define egp_hello egp_handg.egpu_hello
79 #define egp_intgw egp_handg.egpu_gws[0]
80 #define egp_extgw egp_handg.egpu_gws[1]
81 #define egp_reason egp_handg.egpu_reason
82 union {
83 uint16_t egpu_poll;
84 uint32_t egpu_sourcenet;
85 } egp_pands;
86 #define egp_poll egp_pands.egpu_poll
87 #define egp_sourcenet egp_pands.egpu_sourcenet
88 };
89
90 static const char *egp_acquire_codes[] = {
91 "request",
92 "confirm",
93 "refuse",
94 "cease",
95 "cease_ack"
96 };
97
98 static const char *egp_acquire_status[] = {
99 "unspecified",
100 "active_mode",
101 "passive_mode",
102 "insufficient_resources",
103 "administratively_prohibited",
104 "going_down",
105 "parameter_violation",
106 "protocol_violation"
107 };
108
109 static const char *egp_reach_codes[] = {
110 "hello",
111 "i-h-u"
112 };
113
114 static const char *egp_status_updown[] = {
115 "indeterminate",
116 "up",
117 "down"
118 };
119
120 static const char *egp_reasons[] = {
121 "unspecified",
122 "bad_EGP_header_format",
123 "bad_EGP_data_field_format",
124 "reachability_info_unavailable",
125 "excessive_polling_rate",
126 "no_response",
127 "unsupported_version"
128 };
129
130 static void
131 egpnrprint(netdissect_options *ndo,
132 register const struct egp_packet *egp, u_int length)
133 {
134 register const uint8_t *cp;
135 uint32_t addr;
136 register uint32_t net;
137 register u_int netlen;
138 int gateways, distances, networks;
139 int t_gateways;
140 const char *comma;
141
142 addr = egp->egp_sourcenet;
143 if (IN_CLASSA(addr)) {
144 net = addr & IN_CLASSA_NET;
145 netlen = 1;
146 } else if (IN_CLASSB(addr)) {
147 net = addr & IN_CLASSB_NET;
148 netlen = 2;
149 } else if (IN_CLASSC(addr)) {
150 net = addr & IN_CLASSC_NET;
151 netlen = 3;
152 } else {
153 net = 0;
154 netlen = 0;
155 }
156 cp = (const uint8_t *)(egp + 1);
157 length -= sizeof(*egp);
158
159 t_gateways = egp->egp_intgw + egp->egp_extgw;
160 for (gateways = 0; gateways < t_gateways; ++gateways) {
161 /* Pickup host part of gateway address */
162 addr = 0;
163 if (length < 4 - netlen)
164 goto trunc;
165 ND_TCHECK2(cp[0], 4 - netlen);
166 switch (netlen) {
167
168 case 1:
169 addr = EXTRACT_8BITS(cp);
170 cp++;
171 /* fall through */
172 case 2:
173 addr = (addr << 8) | *cp++;
174 /* fall through */
175 case 3:
176 addr = (addr << 8) | *cp++;
177 }
178 addr |= net;
179 length -= 4 - netlen;
180 if (length < 1)
181 goto trunc;
182 ND_TCHECK2(cp[0], 1);
183 distances = EXTRACT_8BITS(cp);
184 cp++;
185 length--;
186 ND_PRINT((ndo, " %s %s ",
187 gateways < (int)egp->egp_intgw ? "int" : "ext",
188 ipaddr_string(ndo, &addr)));
189
190 comma = "";
191 ND_PRINT((ndo, "("));
192 while (--distances >= 0) {
193 if (length < 2)
194 goto trunc;
195 ND_TCHECK2(cp[0], 2);
196 ND_PRINT((ndo, "%sd%d:", comma, (int)*cp++));
197 comma = ", ";
198 networks = EXTRACT_8BITS(cp);
199 cp++;
200 length -= 2;
201 while (--networks >= 0) {
202 /* Pickup network number */
203 if (length < 1)
204 goto trunc;
205 ND_TCHECK2(cp[0], 1);
206 addr = (uint32_t)*cp++ << 24;
207 length--;
208 if (IN_CLASSB(addr)) {
209 if (length < 1)
210 goto trunc;
211 ND_TCHECK2(cp[0], 1);
212 addr |= (uint32_t)*cp++ << 16;
213 length--;
214 } else if (!IN_CLASSA(addr)) {
215 if (length < 2)
216 goto trunc;
217 ND_TCHECK2(cp[0], 2);
218 addr |= (uint32_t)*cp++ << 16;
219 addr |= (uint32_t)*cp++ << 8;
220 length -= 2;
221 }
222 ND_PRINT((ndo, " %s", ipaddr_string(ndo, &addr)));
223 }
224 }
225 ND_PRINT((ndo, ")"));
226 }
227 return;
228 trunc:
229 ND_PRINT((ndo, "[|]"));
230 }
231
232 void
233 egp_print(netdissect_options *ndo,
234 register const uint8_t *bp, register u_int length)
235 {
236 register const struct egp_packet *egp;
237 register int status;
238 register int code;
239 register int type;
240
241 egp = (const struct egp_packet *)bp;
242 if (length < sizeof(*egp) || !ND_TTEST(*egp)) {
243 ND_PRINT((ndo, "[|egp]"));
244 return;
245 }
246
247 if (!ndo->ndo_vflag) {
248 ND_PRINT((ndo, "EGPv%u, AS %u, seq %u, length %u",
249 egp->egp_version,
250 EXTRACT_BE_16BITS(&egp->egp_as),
251 EXTRACT_BE_16BITS(&egp->egp_sequence),
252 length));
253 return;
254 } else
255 ND_PRINT((ndo, "EGPv%u, length %u",
256 egp->egp_version,
257 length));
258
259 if (egp->egp_version != EGP_VERSION) {
260 ND_PRINT((ndo, "[version %d]", egp->egp_version));
261 return;
262 }
263
264 type = egp->egp_type;
265 code = egp->egp_code;
266 status = egp->egp_status;
267
268 switch (type) {
269 case EGPT_ACQUIRE:
270 ND_PRINT((ndo, " acquire"));
271 switch (code) {
272 case EGPC_REQUEST:
273 case EGPC_CONFIRM:
274 ND_PRINT((ndo, " %s", egp_acquire_codes[code]));
275 switch (status) {
276 case EGPS_UNSPEC:
277 case EGPS_ACTIVE:
278 case EGPS_PASSIVE:
279 ND_PRINT((ndo, " %s", egp_acquire_status[status]));
280 break;
281
282 default:
283 ND_PRINT((ndo, " [status %d]", status));
284 break;
285 }
286 ND_PRINT((ndo, " hello:%d poll:%d",
287 EXTRACT_BE_16BITS(&egp->egp_hello),
288 EXTRACT_BE_16BITS(&egp->egp_poll)));
289 break;
290
291 case EGPC_REFUSE:
292 case EGPC_CEASE:
293 case EGPC_CEASEACK:
294 ND_PRINT((ndo, " %s", egp_acquire_codes[code]));
295 switch (status ) {
296 case EGPS_UNSPEC:
297 case EGPS_NORES:
298 case EGPS_ADMIN:
299 case EGPS_GODOWN:
300 case EGPS_PARAM:
301 case EGPS_PROTO:
302 ND_PRINT((ndo, " %s", egp_acquire_status[status]));
303 break;
304
305 default:
306 ND_PRINT((ndo, "[status %d]", status));
307 break;
308 }
309 break;
310
311 default:
312 ND_PRINT((ndo, "[code %d]", code));
313 break;
314 }
315 break;
316
317 case EGPT_REACH:
318 switch (code) {
319
320 case EGPC_HELLO:
321 case EGPC_HEARDU:
322 ND_PRINT((ndo, " %s", egp_reach_codes[code]));
323 if (status <= EGPS_DOWN)
324 ND_PRINT((ndo, " state:%s", egp_status_updown[status]));
325 else
326 ND_PRINT((ndo, " [status %d]", status));
327 break;
328
329 default:
330 ND_PRINT((ndo, "[reach code %d]", code));
331 break;
332 }
333 break;
334
335 case EGPT_POLL:
336 ND_PRINT((ndo, " poll"));
337 if (egp->egp_status <= EGPS_DOWN)
338 ND_PRINT((ndo, " state:%s", egp_status_updown[status]));
339 else
340 ND_PRINT((ndo, " [status %d]", status));
341 ND_PRINT((ndo, " net:%s", ipaddr_string(ndo, &egp->egp_sourcenet)));
342 break;
343
344 case EGPT_UPDATE:
345 ND_PRINT((ndo, " update"));
346 if (status & EGPS_UNSOL) {
347 status &= ~EGPS_UNSOL;
348 ND_PRINT((ndo, " unsolicited"));
349 }
350 if (status <= EGPS_DOWN)
351 ND_PRINT((ndo, " state:%s", egp_status_updown[status]));
352 else
353 ND_PRINT((ndo, " [status %d]", status));
354 ND_PRINT((ndo, " %s int %d ext %d",
355 ipaddr_string(ndo, &egp->egp_sourcenet),
356 egp->egp_intgw,
357 egp->egp_extgw));
358 if (ndo->ndo_vflag)
359 egpnrprint(ndo, egp, length);
360 break;
361
362 case EGPT_ERROR:
363 ND_PRINT((ndo, " error"));
364 if (status <= EGPS_DOWN)
365 ND_PRINT((ndo, " state:%s", egp_status_updown[status]));
366 else
367 ND_PRINT((ndo, " [status %d]", status));
368
369 if (EXTRACT_BE_16BITS(&egp->egp_reason) <= EGPR_UVERSION)
370 ND_PRINT((ndo, " %s", egp_reasons[EXTRACT_BE_16BITS(&egp->egp_reason)]));
371 else
372 ND_PRINT((ndo, " [reason %d]", EXTRACT_BE_16BITS(&egp->egp_reason)));
373 break;
374
375 default:
376 ND_PRINT((ndo, "[type %d]", type));
377 break;
378 }
379 }