]> The Tcpdump Group git mirrors - tcpdump/blob - print-gre.c
Merge pull request #455 from brooksdavis/gndo-cleanup
[tcpdump] / print-gre.c
1 /* $OpenBSD: print-gre.c,v 1.6 2002/10/30 03:04:04 fgsch Exp $ */
2
3 /*
4 * Copyright (c) 2002 Jason L. Wright (jason@thought.net)
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Jason L. Wright
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 /*
35 * tcpdump filter for GRE - Generic Routing Encapsulation
36 * RFC1701 (GRE), RFC1702 (GRE IPv4), and RFC2637 (Enhanced GRE)
37 */
38
39 #ifdef HAVE_CONFIG_H
40 #include "config.h"
41 #endif
42
43 #include <tcpdump-stdinc.h>
44
45 #include <string.h>
46
47 #include "interface.h"
48 #include "extract.h"
49 #include "ethertype.h"
50
51 static const char tstr[] = "[|gre]";
52
53 #define GRE_CP 0x8000 /* checksum present */
54 #define GRE_RP 0x4000 /* routing present */
55 #define GRE_KP 0x2000 /* key present */
56 #define GRE_SP 0x1000 /* sequence# present */
57 #define GRE_sP 0x0800 /* source routing */
58 #define GRE_RECRS 0x0700 /* recursion count */
59 #define GRE_AP 0x0080 /* acknowledgment# present */
60
61 static const struct tok gre_flag_values[] = {
62 { GRE_CP, "checksum present"},
63 { GRE_RP, "routing present"},
64 { GRE_KP, "key present"},
65 { GRE_SP, "sequence# present"},
66 { GRE_sP, "source routing present"},
67 { GRE_RECRS, "recursion count"},
68 { GRE_AP, "ack present"},
69 { 0, NULL }
70 };
71
72 #define GRE_VERS_MASK 0x0007 /* protocol version */
73
74 /* source route entry types */
75 #define GRESRE_IP 0x0800 /* IP */
76 #define GRESRE_ASN 0xfffe /* ASN */
77
78 static void gre_print_0(netdissect_options *, const u_char *, u_int);
79 static void gre_print_1(netdissect_options *, const u_char *, u_int);
80 static void gre_sre_print(netdissect_options *, uint16_t, uint8_t, uint8_t, const u_char *, u_int);
81 static void gre_sre_ip_print(netdissect_options *, uint8_t, uint8_t, const u_char *, u_int);
82 static void gre_sre_asn_print(netdissect_options *, uint8_t, uint8_t, const u_char *, u_int);
83
84 void
85 gre_print(netdissect_options *ndo, const u_char *bp, u_int length)
86 {
87 u_int len = length, vers;
88
89 if (len < 2) {
90 ND_PRINT((ndo, "%s", tstr));
91 return;
92 }
93 vers = EXTRACT_16BITS(bp) & GRE_VERS_MASK;
94 ND_PRINT((ndo, "GREv%u",vers));
95
96 switch(vers) {
97 case 0:
98 gre_print_0(ndo, bp, len);
99 break;
100 case 1:
101 gre_print_1(ndo, bp, len);
102 break;
103 default:
104 ND_PRINT((ndo, " ERROR: unknown-version"));
105 break;
106 }
107 }
108
109 static void
110 gre_print_0(netdissect_options *ndo, const u_char *bp, u_int length)
111 {
112 u_int len = length;
113 uint16_t flags, prot;
114
115 flags = EXTRACT_16BITS(bp);
116 if (ndo->ndo_vflag)
117 ND_PRINT((ndo, ", Flags [%s]",
118 bittok2str(gre_flag_values,"none",flags)));
119
120 len -= 2;
121 bp += 2;
122
123 if (len < 2)
124 goto trunc;
125 prot = EXTRACT_16BITS(bp);
126 len -= 2;
127 bp += 2;
128
129 if ((flags & GRE_CP) | (flags & GRE_RP)) {
130 if (len < 2)
131 goto trunc;
132 if (ndo->ndo_vflag)
133 ND_PRINT((ndo, ", sum 0x%x", EXTRACT_16BITS(bp)));
134 bp += 2;
135 len -= 2;
136
137 if (len < 2)
138 goto trunc;
139 ND_PRINT((ndo, ", off 0x%x", EXTRACT_16BITS(bp)));
140 bp += 2;
141 len -= 2;
142 }
143
144 if (flags & GRE_KP) {
145 if (len < 4)
146 goto trunc;
147 ND_PRINT((ndo, ", key=0x%x", EXTRACT_32BITS(bp)));
148 bp += 4;
149 len -= 4;
150 }
151
152 if (flags & GRE_SP) {
153 if (len < 4)
154 goto trunc;
155 ND_PRINT((ndo, ", seq %u", EXTRACT_32BITS(bp)));
156 bp += 4;
157 len -= 4;
158 }
159
160 if (flags & GRE_RP) {
161 for (;;) {
162 uint16_t af;
163 uint8_t sreoff;
164 uint8_t srelen;
165
166 if (len < 4)
167 goto trunc;
168 af = EXTRACT_16BITS(bp);
169 sreoff = *(bp + 2);
170 srelen = *(bp + 3);
171 bp += 4;
172 len -= 4;
173
174 if (af == 0 && srelen == 0)
175 break;
176
177 gre_sre_print(ndo, af, sreoff, srelen, bp, len);
178
179 if (len < srelen)
180 goto trunc;
181 bp += srelen;
182 len -= srelen;
183 }
184 }
185
186 if (ndo->ndo_eflag)
187 ND_PRINT((ndo, ", proto %s (0x%04x)",
188 tok2str(ethertype_values,"unknown",prot),
189 prot));
190
191 ND_PRINT((ndo, ", length %u",length));
192
193 if (ndo->ndo_vflag < 1)
194 ND_PRINT((ndo, ": ")); /* put in a colon as protocol demarc */
195 else
196 ND_PRINT((ndo, "\n\t")); /* if verbose go multiline */
197
198 switch (prot) {
199 case ETHERTYPE_IP:
200 ip_print(ndo, bp, len);
201 break;
202 case ETHERTYPE_IPV6:
203 ip6_print(ndo, bp, len);
204 break;
205 case ETHERTYPE_MPLS:
206 mpls_print(ndo, bp, len);
207 break;
208 case ETHERTYPE_IPX:
209 ipx_print(ndo, bp, len);
210 break;
211 case ETHERTYPE_ATALK:
212 atalk_print(ndo, bp, len);
213 break;
214 case ETHERTYPE_GRE_ISO:
215 isoclns_print(ndo, bp, len, len);
216 break;
217 case ETHERTYPE_TEB:
218 ether_print(ndo, bp, len, len, NULL, NULL);
219 break;
220 default:
221 ND_PRINT((ndo, "gre-proto-0x%x", prot));
222 }
223 return;
224
225 trunc:
226 ND_PRINT((ndo, "%s", tstr));
227 }
228
229 static void
230 gre_print_1(netdissect_options *ndo, const u_char *bp, u_int length)
231 {
232 u_int len = length;
233 uint16_t flags, prot;
234
235 flags = EXTRACT_16BITS(bp);
236 len -= 2;
237 bp += 2;
238
239 if (ndo->ndo_vflag)
240 ND_PRINT((ndo, ", Flags [%s]",
241 bittok2str(gre_flag_values,"none",flags)));
242
243 if (len < 2)
244 goto trunc;
245 prot = EXTRACT_16BITS(bp);
246 len -= 2;
247 bp += 2;
248
249
250 if (flags & GRE_KP) {
251 uint32_t k;
252
253 if (len < 4)
254 goto trunc;
255 k = EXTRACT_32BITS(bp);
256 ND_PRINT((ndo, ", call %d", k & 0xffff));
257 len -= 4;
258 bp += 4;
259 }
260
261 if (flags & GRE_SP) {
262 if (len < 4)
263 goto trunc;
264 ND_PRINT((ndo, ", seq %u", EXTRACT_32BITS(bp)));
265 bp += 4;
266 len -= 4;
267 }
268
269 if (flags & GRE_AP) {
270 if (len < 4)
271 goto trunc;
272 ND_PRINT((ndo, ", ack %u", EXTRACT_32BITS(bp)));
273 bp += 4;
274 len -= 4;
275 }
276
277 if ((flags & GRE_SP) == 0)
278 ND_PRINT((ndo, ", no-payload"));
279
280 if (ndo->ndo_eflag)
281 ND_PRINT((ndo, ", proto %s (0x%04x)",
282 tok2str(ethertype_values,"unknown",prot),
283 prot));
284
285 ND_PRINT((ndo, ", length %u",length));
286
287 if ((flags & GRE_SP) == 0)
288 return;
289
290 if (ndo->ndo_vflag < 1)
291 ND_PRINT((ndo, ": ")); /* put in a colon as protocol demarc */
292 else
293 ND_PRINT((ndo, "\n\t")); /* if verbose go multiline */
294
295 switch (prot) {
296 case ETHERTYPE_PPP:
297 ppp_print(ndo, bp, len);
298 break;
299 default:
300 ND_PRINT((ndo, "gre-proto-0x%x", prot));
301 break;
302 }
303 return;
304
305 trunc:
306 ND_PRINT((ndo, "%s", tstr));
307 }
308
309 static void
310 gre_sre_print(netdissect_options *ndo, uint16_t af, uint8_t sreoff,
311 uint8_t srelen, const u_char *bp, u_int len)
312 {
313 switch (af) {
314 case GRESRE_IP:
315 ND_PRINT((ndo, ", (rtaf=ip"));
316 gre_sre_ip_print(ndo, sreoff, srelen, bp, len);
317 ND_PRINT((ndo, ")"));
318 break;
319 case GRESRE_ASN:
320 ND_PRINT((ndo, ", (rtaf=asn"));
321 gre_sre_asn_print(ndo, sreoff, srelen, bp, len);
322 ND_PRINT((ndo, ")"));
323 break;
324 default:
325 ND_PRINT((ndo, ", (rtaf=0x%x)", af));
326 }
327 }
328
329 static void
330 gre_sre_ip_print(netdissect_options *ndo, uint8_t sreoff, uint8_t srelen,
331 const u_char *bp, u_int len)
332 {
333 struct in_addr a;
334 const u_char *up = bp;
335
336 if (sreoff & 3) {
337 ND_PRINT((ndo, ", badoffset=%u", sreoff));
338 return;
339 }
340 if (srelen & 3) {
341 ND_PRINT((ndo, ", badlength=%u", srelen));
342 return;
343 }
344 if (sreoff >= srelen) {
345 ND_PRINT((ndo, ", badoff/len=%u/%u", sreoff, srelen));
346 return;
347 }
348
349 for (;;) {
350 if (len < 4 || srelen == 0)
351 return;
352
353 memcpy(&a, bp, sizeof(a));
354 ND_PRINT((ndo, " %s%s",
355 ((bp - up) == sreoff) ? "*" : "",
356 inet_ntoa(a)));
357
358 bp += 4;
359 len -= 4;
360 srelen -= 4;
361 }
362 }
363
364 static void
365 gre_sre_asn_print(netdissect_options *ndo, uint8_t sreoff, uint8_t srelen,
366 const u_char *bp, u_int len)
367 {
368 const u_char *up = bp;
369
370 if (sreoff & 1) {
371 ND_PRINT((ndo, ", badoffset=%u", sreoff));
372 return;
373 }
374 if (srelen & 1) {
375 ND_PRINT((ndo, ", badlength=%u", srelen));
376 return;
377 }
378 if (sreoff >= srelen) {
379 ND_PRINT((ndo, ", badoff/len=%u/%u", sreoff, srelen));
380 return;
381 }
382
383 for (;;) {
384 if (len < 2 || srelen == 0)
385 return;
386
387 ND_PRINT((ndo, " %s%x",
388 ((bp - up) == sreoff) ? "*" : "",
389 EXTRACT_16BITS(bp)));
390
391 bp += 2;
392 len -= 2;
393 srelen -= 2;
394 }
395 }