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