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