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