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