print-rip.c revision 1.9 1 1.1 christos /*
2 1.1 christos * Copyright (c) 1989, 1990, 1991, 1993, 1994, 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, with or without
6 1.1 christos * modification, are permitted provided that: (1) source code distributions
7 1.1 christos * retain the above copyright notice and this paragraph in its entirety, (2)
8 1.1 christos * distributions including binary code include the above copyright notice and
9 1.1 christos * this paragraph in its entirety in the documentation or other materials
10 1.1 christos * provided with the distribution, and (3) all advertising materials mentioning
11 1.1 christos * features or use of this software display the following acknowledgement:
12 1.1 christos * ``This product includes software developed by the University of California,
13 1.1 christos * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 1.1 christos * the University nor the names of its contributors may be used to endorse
15 1.1 christos * or promote products derived from this software without specific prior
16 1.1 christos * written permission.
17 1.1 christos * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 1.1 christos * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 1.1 christos * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 1.1 christos */
21 1.1 christos
22 1.2 christos #include <sys/cdefs.h>
23 1.1 christos #ifndef lint
24 1.9 christos __RCSID("$NetBSD: print-rip.c,v 1.9 2023/08/17 20:19:40 christos Exp $");
25 1.1 christos #endif
26 1.1 christos
27 1.7 spz /* \summary: Routing Information Protocol (RIP) printer */
28 1.7 spz
29 1.9 christos /* specification: RFC 1058, RFC 2453, RFC 4822 */
30 1.9 christos
31 1.1 christos #ifdef HAVE_CONFIG_H
32 1.9 christos #include <config.h>
33 1.1 christos #endif
34 1.1 christos
35 1.9 christos #include "netdissect-stdinc.h"
36 1.1 christos
37 1.6 christos #include "netdissect.h"
38 1.1 christos #include "addrtoname.h"
39 1.6 christos #include "extract.h"
40 1.1 christos
41 1.1 christos #include "af.h"
42 1.1 christos
43 1.5 christos
44 1.9 christos /*
45 1.9 christos * RFC 1058 and RFC 2453 header of packet.
46 1.9 christos *
47 1.9 christos * 0 1 2 3 3
48 1.9 christos * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
49 1.9 christos * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
50 1.9 christos * | Command (1) | Version (1) | unused |
51 1.9 christos * +---------------+---------------+-------------------------------+
52 1.9 christos */
53 1.1 christos struct rip {
54 1.9 christos nd_uint8_t rip_cmd; /* request/response */
55 1.9 christos nd_uint8_t rip_vers; /* protocol version # */
56 1.9 christos nd_byte unused[2]; /* unused */
57 1.1 christos };
58 1.1 christos
59 1.1 christos #define RIPCMD_REQUEST 1 /* want info */
60 1.1 christos #define RIPCMD_RESPONSE 2 /* responding to request */
61 1.1 christos #define RIPCMD_TRACEON 3 /* turn tracing on */
62 1.1 christos #define RIPCMD_TRACEOFF 4 /* turn it off */
63 1.1 christos #define RIPCMD_POLL 5 /* want info from everybody */
64 1.1 christos #define RIPCMD_POLLENTRY 6 /* poll for entry */
65 1.1 christos
66 1.1 christos static const struct tok rip_cmd_values[] = {
67 1.1 christos { RIPCMD_REQUEST, "Request" },
68 1.1 christos { RIPCMD_RESPONSE, "Response" },
69 1.1 christos { RIPCMD_TRACEON, "Trace on" },
70 1.1 christos { RIPCMD_TRACEOFF, "Trace off" },
71 1.1 christos { RIPCMD_POLL, "Poll" },
72 1.1 christos { RIPCMD_POLLENTRY, "Poll Entry" },
73 1.1 christos { 0, NULL}
74 1.1 christos };
75 1.1 christos
76 1.1 christos #define RIP_AUTHLEN 16
77 1.1 christos #define RIP_ROUTELEN 20
78 1.1 christos
79 1.1 christos /*
80 1.9 christos * First 4 bytes of all RIPv1/RIPv2 entries.
81 1.9 christos */
82 1.9 christos struct rip_entry_header {
83 1.9 christos nd_uint16_t rip_family;
84 1.9 christos nd_uint16_t rip_tag;
85 1.9 christos };
86 1.9 christos
87 1.9 christos /*
88 1.9 christos * RFC 1058 entry.
89 1.9 christos *
90 1.9 christos * 0 1 2 3 3
91 1.9 christos * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
92 1.9 christos * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
93 1.9 christos * | Address Family Identifier (2) | must be zero (2) |
94 1.9 christos * +-------------------------------+-------------------------------+
95 1.9 christos * | IP Address (4) |
96 1.9 christos * +---------------------------------------------------------------+
97 1.9 christos * | must be zero (4) |
98 1.9 christos * +---------------------------------------------------------------+
99 1.9 christos * | must be zero (4) |
100 1.9 christos * +---------------------------------------------------------------+
101 1.9 christos * | Metric (4) |
102 1.9 christos * +---------------------------------------------------------------+
103 1.9 christos */
104 1.9 christos struct rip_netinfo_v1 {
105 1.9 christos nd_uint16_t rip_family;
106 1.9 christos nd_byte rip_mbz1[2];
107 1.9 christos nd_ipv4 rip_dest;
108 1.9 christos nd_byte rip_mbz2[4];
109 1.9 christos nd_byte rip_mbz3[4];
110 1.9 christos nd_uint32_t rip_metric; /* cost of route */
111 1.9 christos };
112 1.9 christos
113 1.9 christos
114 1.9 christos /*
115 1.9 christos * RFC 2453 route entry
116 1.5 christos *
117 1.1 christos * 0 1 2 3 3
118 1.1 christos * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
119 1.1 christos * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
120 1.1 christos * | Address Family Identifier (2) | Route Tag (2) |
121 1.1 christos * +-------------------------------+-------------------------------+
122 1.1 christos * | IP Address (4) |
123 1.1 christos * +---------------------------------------------------------------+
124 1.1 christos * | Subnet Mask (4) |
125 1.1 christos * +---------------------------------------------------------------+
126 1.1 christos * | Next Hop (4) |
127 1.1 christos * +---------------------------------------------------------------+
128 1.1 christos * | Metric (4) |
129 1.1 christos * +---------------------------------------------------------------+
130 1.1 christos *
131 1.1 christos */
132 1.1 christos
133 1.9 christos struct rip_netinfo_v2 {
134 1.9 christos nd_uint16_t rip_family;
135 1.9 christos nd_uint16_t rip_tag;
136 1.9 christos nd_ipv4 rip_dest;
137 1.9 christos nd_uint32_t rip_dest_mask;
138 1.9 christos nd_ipv4 rip_router;
139 1.9 christos nd_uint32_t rip_metric; /* cost of route */
140 1.1 christos };
141 1.1 christos
142 1.9 christos /*
143 1.9 christos * RFC 2453 authentication entry
144 1.9 christos *
145 1.9 christos * 0 1 2 3 3
146 1.9 christos * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
147 1.9 christos * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
148 1.9 christos * | 0xFFFF | Authentication Type (2) |
149 1.9 christos * +-------------------------------+-------------------------------+
150 1.9 christos * - Authentication (16) -
151 1.9 christos * +---------------------------------------------------------------+
152 1.9 christos */
153 1.9 christos
154 1.9 christos struct rip_auth_v2 {
155 1.9 christos nd_uint16_t rip_family;
156 1.9 christos nd_uint16_t rip_tag;
157 1.9 christos nd_byte rip_auth[16];
158 1.9 christos };
159 1.9 christos
160 1.9 christos /*
161 1.9 christos * RFC 4822 Cryptographic Authentication entry.
162 1.9 christos *
163 1.9 christos * 0 1 2 3 3
164 1.9 christos * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
165 1.9 christos * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
166 1.9 christos * | RIPv2 Packet Length | Key ID | Auth Data Len |
167 1.9 christos * +---------------+---------------+---------------+---------------+
168 1.9 christos * | Sequence Number (non-decreasing) |
169 1.9 christos * +---------------+---------------+---------------+---------------+
170 1.9 christos * | reserved must be zero |
171 1.9 christos * +---------------+---------------+---------------+---------------+
172 1.9 christos * | reserved must be zero |
173 1.9 christos * +---------------+---------------+---------------+---------------+
174 1.9 christos */
175 1.9 christos struct rip_auth_crypto_v2 {
176 1.9 christos nd_uint16_t rip_packet_len;
177 1.9 christos nd_uint8_t rip_key_id;
178 1.9 christos nd_uint8_t rip_auth_data_len;
179 1.9 christos nd_uint32_t rip_seq_num;
180 1.9 christos nd_byte rip_mbz1[4];
181 1.9 christos nd_byte rip_mbz2[4];
182 1.9 christos };
183 1.9 christos
184 1.9 christos static unsigned
185 1.9 christos rip_entry_print_v1(netdissect_options *ndo, const u_char *p,
186 1.9 christos unsigned remaining)
187 1.1 christos {
188 1.9 christos const struct rip_entry_header *eh = (const struct rip_entry_header *)p;
189 1.9 christos u_short family;
190 1.9 christos const struct rip_netinfo_v1 *ni = (const struct rip_netinfo_v1 *)p;
191 1.1 christos
192 1.1 christos /* RFC 1058 */
193 1.9 christos if (remaining < RIP_ROUTELEN)
194 1.9 christos return (0);
195 1.9 christos ND_TCHECK_SIZE(ni);
196 1.9 christos family = GET_BE_U_2(ni->rip_family);
197 1.4 christos if (family != BSD_AFNUM_INET && family != 0) {
198 1.9 christos ND_PRINT("\n\t AFI %s, ", tok2str(bsd_af_values, "Unknown (%u)", family));
199 1.9 christos print_unknown_data(ndo, p + sizeof(*eh), "\n\t ", RIP_ROUTELEN - sizeof(*eh));
200 1.9 christos return (RIP_ROUTELEN);
201 1.1 christos }
202 1.9 christos if (GET_BE_U_2(ni->rip_mbz1) ||
203 1.9 christos GET_BE_U_4(ni->rip_mbz2) ||
204 1.9 christos GET_BE_U_4(ni->rip_mbz3)) {
205 1.1 christos /* MBZ fields not zero */
206 1.9 christos print_unknown_data(ndo, p, "\n\t ", RIP_ROUTELEN);
207 1.9 christos return (RIP_ROUTELEN);
208 1.4 christos }
209 1.4 christos if (family == 0) {
210 1.9 christos ND_PRINT("\n\t AFI 0, %s, metric: %u",
211 1.9 christos GET_IPADDR_STRING(ni->rip_dest),
212 1.9 christos GET_BE_U_4(ni->rip_metric));
213 1.9 christos return (RIP_ROUTELEN);
214 1.1 christos } /* BSD_AFNUM_INET */
215 1.9 christos ND_PRINT("\n\t %s, metric: %u",
216 1.9 christos GET_IPADDR_STRING(ni->rip_dest),
217 1.9 christos GET_BE_U_4(ni->rip_metric));
218 1.9 christos return (RIP_ROUTELEN);
219 1.9 christos trunc:
220 1.9 christos return 0;
221 1.1 christos }
222 1.1 christos
223 1.8 kamil UNALIGNED_OK
224 1.4 christos static unsigned
225 1.9 christos rip_entry_print_v2(netdissect_options *ndo, const u_char *p,
226 1.9 christos unsigned remaining)
227 1.1 christos {
228 1.9 christos const struct rip_entry_header *eh = (const struct rip_entry_header *)p;
229 1.9 christos u_short family;
230 1.9 christos const struct rip_netinfo_v2 *ni;
231 1.9 christos
232 1.9 christos if (remaining < sizeof(*eh))
233 1.9 christos return (0);
234 1.9 christos ND_TCHECK_SIZE(eh);
235 1.9 christos family = GET_BE_U_2(eh->rip_family);
236 1.9 christos if (family == 0xFFFF) { /* variable-sized authentication structures */
237 1.9 christos uint16_t auth_type = GET_BE_U_2(eh->rip_tag);
238 1.1 christos
239 1.9 christos p += sizeof(*eh);
240 1.9 christos remaining -= sizeof(*eh);
241 1.4 christos if (auth_type == 2) {
242 1.9 christos ND_PRINT("\n\t Simple Text Authentication data: ");
243 1.9 christos nd_printjnp(ndo, p, RIP_AUTHLEN);
244 1.4 christos } else if (auth_type == 3) {
245 1.9 christos const struct rip_auth_crypto_v2 *ch;
246 1.9 christos
247 1.9 christos ch = (const struct rip_auth_crypto_v2 *)p;
248 1.9 christos ND_TCHECK_SIZE(ch);
249 1.9 christos if (remaining < sizeof(*ch))
250 1.9 christos return (0);
251 1.9 christos ND_PRINT("\n\t Auth header:");
252 1.9 christos ND_PRINT(" Packet Len %u,",
253 1.9 christos GET_BE_U_2(ch->rip_packet_len));
254 1.9 christos ND_PRINT(" Key-ID %u,", GET_U_1(ch->rip_key_id));
255 1.9 christos ND_PRINT(" Auth Data Len %u,",
256 1.9 christos GET_U_1(ch->rip_auth_data_len));
257 1.9 christos ND_PRINT(" SeqNo %u,", GET_BE_U_4(ch->rip_seq_num));
258 1.9 christos ND_PRINT(" MBZ %u,", GET_BE_U_4(ch->rip_mbz1));
259 1.9 christos ND_PRINT(" MBZ %u", GET_BE_U_4(ch->rip_mbz2));
260 1.4 christos } else if (auth_type == 1) {
261 1.9 christos ND_PRINT("\n\t Auth trailer:");
262 1.9 christos print_unknown_data(ndo, p, "\n\t ", remaining);
263 1.9 christos return (sizeof(*eh) + remaining); /* AT spans till the packet end */
264 1.5 christos } else {
265 1.9 christos ND_PRINT("\n\t Unknown (%u) Authentication data:",
266 1.9 christos auth_type);
267 1.9 christos print_unknown_data(ndo, p, "\n\t ", remaining);
268 1.9 christos return (sizeof(*eh) + remaining); /* we don't know how long this is, so we go to the packet end */
269 1.1 christos }
270 1.4 christos } else if (family != BSD_AFNUM_INET && family != 0) {
271 1.9 christos ND_PRINT("\n\t AFI %s", tok2str(bsd_af_values, "Unknown (%u)", family));
272 1.9 christos print_unknown_data(ndo, p + sizeof(*eh), "\n\t ", RIP_ROUTELEN - sizeof(*eh));
273 1.4 christos } else { /* BSD_AFNUM_INET or AFI 0 */
274 1.9 christos ni = (const struct rip_netinfo_v2 *)p;
275 1.9 christos ND_TCHECK_SIZE(ni);
276 1.9 christos if (remaining < sizeof(*ni))
277 1.9 christos return (0);
278 1.9 christos ND_PRINT("\n\t AFI %s, %15s/%-2d, tag 0x%04x, metric: %u, next-hop: ",
279 1.9 christos tok2str(bsd_af_values, "%u", family),
280 1.9 christos GET_IPADDR_STRING(ni->rip_dest),
281 1.9 christos mask2plen(GET_BE_U_4(ni->rip_dest_mask)),
282 1.9 christos GET_BE_U_2(ni->rip_tag),
283 1.9 christos GET_BE_U_4(ni->rip_metric));
284 1.9 christos if (GET_BE_U_4(ni->rip_router))
285 1.9 christos ND_PRINT("%s", GET_IPADDR_STRING(ni->rip_router));
286 1.5 christos else
287 1.9 christos ND_PRINT("self");
288 1.1 christos }
289 1.9 christos return (RIP_ROUTELEN);
290 1.9 christos trunc:
291 1.9 christos return 0;
292 1.1 christos }
293 1.1 christos
294 1.1 christos void
295 1.5 christos rip_print(netdissect_options *ndo,
296 1.9 christos const u_char *dat, u_int length)
297 1.1 christos {
298 1.9 christos const struct rip *rp;
299 1.9 christos uint8_t vers, cmd;
300 1.9 christos const u_char *p;
301 1.9 christos u_int len, routecount;
302 1.9 christos unsigned entry_size;
303 1.1 christos
304 1.9 christos ndo->ndo_protocol = "rip";
305 1.5 christos if (ndo->ndo_snapend < dat) {
306 1.9 christos nd_print_trunc(ndo);
307 1.1 christos return;
308 1.1 christos }
309 1.9 christos len = ND_BYTES_AVAILABLE_AFTER(dat);
310 1.9 christos if (len > length)
311 1.9 christos len = length;
312 1.9 christos if (len < sizeof(*rp)) {
313 1.9 christos nd_print_trunc(ndo);
314 1.1 christos return;
315 1.1 christos }
316 1.9 christos len -= sizeof(*rp);
317 1.1 christos
318 1.6 christos rp = (const struct rip *)dat;
319 1.1 christos
320 1.9 christos ND_TCHECK_SIZE(rp);
321 1.9 christos vers = GET_U_1(rp->rip_vers);
322 1.9 christos ND_PRINT("%sRIPv%u",
323 1.9 christos (ndo->ndo_vflag >= 1) ? "\n\t" : "",
324 1.9 christos vers);
325 1.1 christos
326 1.9 christos if (vers == 0) {
327 1.1 christos /*
328 1.1 christos * RFC 1058.
329 1.1 christos *
330 1.1 christos * XXX - RFC 1058 says
331 1.1 christos *
332 1.1 christos * 0 Datagrams whose version number is zero are to be ignored.
333 1.1 christos * These are from a previous version of the protocol, whose
334 1.1 christos * packet format was machine-specific.
335 1.1 christos *
336 1.1 christos * so perhaps we should just dump the packet, in hex.
337 1.1 christos */
338 1.9 christos print_unknown_data(ndo, (const uint8_t *)&rp->rip_cmd, "\n\t", length);
339 1.9 christos return;
340 1.9 christos }
341 1.9 christos
342 1.9 christos /* dump version and lets see if we know the commands name*/
343 1.9 christos cmd = GET_U_1(rp->rip_cmd);
344 1.9 christos ND_PRINT(", %s, length: %u",
345 1.9 christos tok2str(rip_cmd_values, "unknown command (%u)", cmd),
346 1.9 christos length);
347 1.9 christos
348 1.9 christos if (ndo->ndo_vflag < 1)
349 1.9 christos return;
350 1.9 christos
351 1.9 christos switch (cmd) {
352 1.9 christos
353 1.9 christos case RIPCMD_REQUEST:
354 1.9 christos case RIPCMD_RESPONSE:
355 1.9 christos switch (vers) {
356 1.9 christos
357 1.9 christos case 1:
358 1.9 christos routecount = length / RIP_ROUTELEN;
359 1.9 christos ND_PRINT(", routes: %u", routecount);
360 1.9 christos p = (const u_char *)(rp + 1);
361 1.9 christos while (len != 0) {
362 1.9 christos entry_size = rip_entry_print_v1(ndo, p, len);
363 1.9 christos if (entry_size == 0) {
364 1.9 christos /* Error */
365 1.9 christos nd_print_trunc(ndo);
366 1.9 christos break;
367 1.9 christos }
368 1.9 christos if (len < entry_size) {
369 1.9 christos ND_PRINT(" [remaining entries length %u < %u]",
370 1.9 christos len, entry_size);
371 1.9 christos nd_print_invalid(ndo);
372 1.9 christos break;
373 1.9 christos }
374 1.9 christos p += entry_size;
375 1.9 christos len -= entry_size;
376 1.9 christos }
377 1.9 christos break;
378 1.9 christos
379 1.9 christos case 2:
380 1.9 christos routecount = length / RIP_ROUTELEN;
381 1.9 christos ND_PRINT(", routes: %u or less", routecount);
382 1.9 christos p = (const u_char *)(rp + 1);
383 1.9 christos while (len != 0) {
384 1.9 christos entry_size = rip_entry_print_v2(ndo, p, len);
385 1.9 christos if (entry_size == 0) {
386 1.9 christos /* Error */
387 1.9 christos nd_print_trunc(ndo);
388 1.9 christos break;
389 1.9 christos }
390 1.9 christos if (len < entry_size) {
391 1.9 christos ND_PRINT(" [remaining entries length %u < %u]",
392 1.9 christos len, entry_size);
393 1.9 christos nd_print_invalid(ndo);
394 1.9 christos break;
395 1.4 christos }
396 1.9 christos p += entry_size;
397 1.9 christos len -= entry_size;
398 1.1 christos }
399 1.1 christos break;
400 1.1 christos
401 1.9 christos default:
402 1.9 christos ND_PRINT(", unknown version");
403 1.1 christos break;
404 1.9 christos }
405 1.9 christos break;
406 1.9 christos
407 1.9 christos case RIPCMD_TRACEOFF:
408 1.9 christos case RIPCMD_POLL:
409 1.9 christos case RIPCMD_POLLENTRY:
410 1.9 christos break;
411 1.1 christos
412 1.9 christos case RIPCMD_TRACEON:
413 1.9 christos /* fall through */
414 1.9 christos default:
415 1.9 christos if (ndo->ndo_vflag <= 1) {
416 1.9 christos if (!print_unknown_data(ndo, (const uint8_t *)rp, "\n\t", length))
417 1.9 christos return;
418 1.9 christos }
419 1.9 christos break;
420 1.9 christos }
421 1.9 christos /* do we want to see an additionally hexdump ? */
422 1.9 christos if (ndo->ndo_vflag> 1) {
423 1.9 christos if (!print_unknown_data(ndo, (const uint8_t *)rp, "\n\t", length))
424 1.9 christos return;
425 1.9 christos }
426 1.9 christos trunc:
427 1.9 christos return;
428 1.1 christos }
429