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