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