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