print-rip.c revision 1.6 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 #include <sys/cdefs.h>
23 #ifndef lint
24 __RCSID("$NetBSD: print-rip.c,v 1.6 2017/01/24 23:29:14 christos Exp $");
25 #endif
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <netdissect-stdinc.h>
32
33 #include <stdio.h>
34
35 #include "netdissect.h"
36 #include "addrtoname.h"
37 #include "extract.h"
38
39 #include "af.h"
40
41 static const char tstr[] = "[|rip]";
42
43 struct rip {
44 uint8_t rip_cmd; /* request/response */
45 uint8_t rip_vers; /* protocol version # */
46 uint8_t unused[2]; /* unused */
47 };
48
49 #define RIPCMD_REQUEST 1 /* want info */
50 #define RIPCMD_RESPONSE 2 /* responding to request */
51 #define RIPCMD_TRACEON 3 /* turn tracing on */
52 #define RIPCMD_TRACEOFF 4 /* turn it off */
53 #define RIPCMD_POLL 5 /* want info from everybody */
54 #define RIPCMD_POLLENTRY 6 /* poll for entry */
55
56 static const struct tok rip_cmd_values[] = {
57 { RIPCMD_REQUEST, "Request" },
58 { RIPCMD_RESPONSE, "Response" },
59 { RIPCMD_TRACEON, "Trace on" },
60 { RIPCMD_TRACEOFF, "Trace off" },
61 { RIPCMD_POLL, "Poll" },
62 { RIPCMD_POLLENTRY, "Poll Entry" },
63 { 0, NULL}
64 };
65
66 #define RIP_AUTHLEN 16
67 #define RIP_ROUTELEN 20
68
69 /*
70 * rfc 1723
71 *
72 * 0 1 2 3 3
73 * 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
74 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
75 * | Command (1) | Version (1) | unused |
76 * +---------------+---------------+-------------------------------+
77 * | Address Family Identifier (2) | Route Tag (2) |
78 * +-------------------------------+-------------------------------+
79 * | IP Address (4) |
80 * +---------------------------------------------------------------+
81 * | Subnet Mask (4) |
82 * +---------------------------------------------------------------+
83 * | Next Hop (4) |
84 * +---------------------------------------------------------------+
85 * | Metric (4) |
86 * +---------------------------------------------------------------+
87 *
88 */
89
90 struct rip_netinfo {
91 uint16_t rip_family;
92 uint16_t rip_tag;
93 uint32_t rip_dest;
94 uint32_t rip_dest_mask;
95 uint32_t rip_router;
96 uint32_t rip_metric; /* cost of route */
97 };
98
99 static void
100 rip_entry_print_v1(netdissect_options *ndo,
101 register const struct rip_netinfo *ni)
102 {
103 register u_short family;
104
105 /* RFC 1058 */
106 family = EXTRACT_16BITS(&ni->rip_family);
107 if (family != BSD_AFNUM_INET && family != 0) {
108 ND_PRINT((ndo, "\n\t AFI %s, ", tok2str(bsd_af_values, "Unknown (%u)", family)));
109 print_unknown_data(ndo, (const uint8_t *)&ni->rip_family, "\n\t ", RIP_ROUTELEN);
110 return;
111 }
112 if (EXTRACT_16BITS(&ni->rip_tag) ||
113 EXTRACT_32BITS(&ni->rip_dest_mask) ||
114 EXTRACT_32BITS(&ni->rip_router)) {
115 /* MBZ fields not zero */
116 print_unknown_data(ndo, (const uint8_t *)&ni->rip_family, "\n\t ", RIP_ROUTELEN);
117 return;
118 }
119 if (family == 0) {
120 ND_PRINT((ndo, "\n\t AFI 0, %s, metric: %u",
121 ipaddr_string(ndo, &ni->rip_dest),
122 EXTRACT_32BITS(&ni->rip_metric)));
123 return;
124 } /* BSD_AFNUM_INET */
125 ND_PRINT((ndo, "\n\t %s, metric: %u",
126 ipaddr_string(ndo, &ni->rip_dest),
127 EXTRACT_32BITS(&ni->rip_metric)));
128 }
129
130 static unsigned
131 rip_entry_print_v2(netdissect_options *ndo,
132 register const struct rip_netinfo *ni, const unsigned remaining)
133 {
134 register u_short family;
135
136 family = EXTRACT_16BITS(&ni->rip_family);
137 if (family == 0xFFFF) { /* variable-sized authentication structures */
138 uint16_t auth_type = EXTRACT_16BITS(&ni->rip_tag);
139 if (auth_type == 2) {
140 register const u_char *p = (const u_char *)&ni->rip_dest;
141 u_int i = 0;
142 ND_PRINT((ndo, "\n\t Simple Text Authentication data: "));
143 for (; i < RIP_AUTHLEN; p++, i++)
144 ND_PRINT((ndo, "%c", ND_ISPRINT(*p) ? *p : '.'));
145 } else if (auth_type == 3) {
146 ND_PRINT((ndo, "\n\t Auth header:"));
147 ND_PRINT((ndo, " Packet Len %u,", EXTRACT_16BITS((const uint8_t *)ni + 4)));
148 ND_PRINT((ndo, " Key-ID %u,", *((const uint8_t *)ni + 6)));
149 ND_PRINT((ndo, " Auth Data Len %u,", *((const uint8_t *)ni + 7)));
150 ND_PRINT((ndo, " SeqNo %u,", EXTRACT_32BITS(&ni->rip_dest_mask)));
151 ND_PRINT((ndo, " MBZ %u,", EXTRACT_32BITS(&ni->rip_router)));
152 ND_PRINT((ndo, " MBZ %u", EXTRACT_32BITS(&ni->rip_metric)));
153 } else if (auth_type == 1) {
154 ND_PRINT((ndo, "\n\t Auth trailer:"));
155 print_unknown_data(ndo, (const uint8_t *)&ni->rip_dest, "\n\t ", remaining);
156 return remaining; /* AT spans till the packet end */
157 } else {
158 ND_PRINT((ndo, "\n\t Unknown (%u) Authentication data:",
159 EXTRACT_16BITS(&ni->rip_tag)));
160 print_unknown_data(ndo, (const uint8_t *)&ni->rip_dest, "\n\t ", remaining);
161 }
162 } else if (family != BSD_AFNUM_INET && family != 0) {
163 ND_PRINT((ndo, "\n\t AFI %s", tok2str(bsd_af_values, "Unknown (%u)", family)));
164 print_unknown_data(ndo, (const uint8_t *)&ni->rip_tag, "\n\t ", RIP_ROUTELEN-2);
165 } else { /* BSD_AFNUM_INET or AFI 0 */
166 ND_PRINT((ndo, "\n\t AFI %s, %15s/%-2d, tag 0x%04x, metric: %u, next-hop: ",
167 tok2str(bsd_af_values, "%u", family),
168 ipaddr_string(ndo, &ni->rip_dest),
169 mask2plen(EXTRACT_32BITS(&ni->rip_dest_mask)),
170 EXTRACT_16BITS(&ni->rip_tag),
171 EXTRACT_32BITS(&ni->rip_metric)));
172 if (EXTRACT_32BITS(&ni->rip_router))
173 ND_PRINT((ndo, "%s", ipaddr_string(ndo, &ni->rip_router)));
174 else
175 ND_PRINT((ndo, "self"));
176 }
177 return sizeof (*ni);
178 }
179
180 void
181 rip_print(netdissect_options *ndo,
182 const u_char *dat, u_int length)
183 {
184 register const struct rip *rp;
185 register const struct rip_netinfo *ni;
186 register u_int i, j;
187
188 if (ndo->ndo_snapend < dat) {
189 ND_PRINT((ndo, " %s", tstr));
190 return;
191 }
192 i = ndo->ndo_snapend - dat;
193 if (i > length)
194 i = length;
195 if (i < sizeof(*rp)) {
196 ND_PRINT((ndo, " %s", tstr));
197 return;
198 }
199 i -= sizeof(*rp);
200
201 rp = (const struct rip *)dat;
202
203 ND_PRINT((ndo, "%sRIPv%u",
204 (ndo->ndo_vflag >= 1) ? "\n\t" : "",
205 rp->rip_vers));
206
207 switch (rp->rip_vers) {
208 case 0:
209 /*
210 * RFC 1058.
211 *
212 * XXX - RFC 1058 says
213 *
214 * 0 Datagrams whose version number is zero are to be ignored.
215 * These are from a previous version of the protocol, whose
216 * packet format was machine-specific.
217 *
218 * so perhaps we should just dump the packet, in hex.
219 */
220 print_unknown_data(ndo, (const uint8_t *)&rp->rip_cmd, "\n\t", length);
221 break;
222 default:
223 /* dump version and lets see if we know the commands name*/
224 ND_PRINT((ndo, ", %s, length: %u",
225 tok2str(rip_cmd_values,
226 "unknown command (%u)",
227 rp->rip_cmd),
228 length));
229
230 if (ndo->ndo_vflag < 1)
231 return;
232
233 switch (rp->rip_cmd) {
234 case RIPCMD_REQUEST:
235 case RIPCMD_RESPONSE:
236 j = length / sizeof(*ni);
237 ND_PRINT((ndo, ", routes: %u%s", j, rp->rip_vers == 2 ? " or less" : ""));
238 ni = (const struct rip_netinfo *)(rp + 1);
239 for (; i >= sizeof(*ni); ++ni) {
240 if (rp->rip_vers == 1)
241 {
242 rip_entry_print_v1(ndo, ni);
243 i -= sizeof(*ni);
244 }
245 else if (rp->rip_vers == 2)
246 i -= rip_entry_print_v2(ndo, ni, i);
247 else
248 break;
249 }
250 if (i)
251 ND_PRINT((ndo, "%s", tstr));
252 break;
253
254 case RIPCMD_TRACEOFF:
255 case RIPCMD_POLL:
256 case RIPCMD_POLLENTRY:
257 break;
258
259 case RIPCMD_TRACEON:
260 /* fall through */
261 default:
262 if (ndo->ndo_vflag <= 1) {
263 if(!print_unknown_data(ndo, (const uint8_t *)rp, "\n\t", length))
264 return;
265 }
266 break;
267 }
268 /* do we want to see an additionally hexdump ? */
269 if (ndo->ndo_vflag> 1) {
270 if(!print_unknown_data(ndo, (const uint8_t *)rp, "\n\t", length))
271 return;
272 }
273 }
274 }
275
276
277