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