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