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