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