]> The Tcpdump Group git mirrors - tcpdump/blob - print-gre.c
Change update-test.sh to update multiple tests
[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 /* \summary: Generic Routing Encapsulation (GRE) printer */
35
36 /*
37 * netdissect printer for GRE - Generic Routing Encapsulation
38 * RFC1701 (GRE), RFC1702 (GRE IPv4), and RFC2637 (Enhanced GRE)
39 */
40
41 #ifdef HAVE_CONFIG_H
42 #include <config.h>
43 #endif
44
45 #include "netdissect-stdinc.h"
46
47 #include <string.h>
48
49 #include "netdissect.h"
50 #include "addrtostr.h"
51 #include "extract.h"
52 #include "ethertype.h"
53
54 static const char tstr[] = "[|gre]";
55
56 #define GRE_CP 0x8000 /* checksum present */
57 #define GRE_RP 0x4000 /* routing present */
58 #define GRE_KP 0x2000 /* key present */
59 #define GRE_SP 0x1000 /* sequence# present */
60 #define GRE_sP 0x0800 /* source routing */
61 #define GRE_RECRS 0x0700 /* recursion count */
62 #define GRE_AP 0x0080 /* acknowledgment# present */
63
64 static const struct tok gre_flag_values[] = {
65 { GRE_CP, "checksum present"},
66 { GRE_RP, "routing present"},
67 { GRE_KP, "key present"},
68 { GRE_SP, "sequence# present"},
69 { GRE_sP, "source routing present"},
70 { GRE_RECRS, "recursion count"},
71 { GRE_AP, "ack present"},
72 { 0, NULL }
73 };
74
75 #define GRE_VERS_MASK 0x0007 /* protocol version */
76
77 /* source route entry types */
78 #define GRESRE_IP 0x0800 /* IP */
79 #define GRESRE_ASN 0xfffe /* ASN */
80
81 static void gre_print_0(netdissect_options *, const u_char *, u_int);
82 static void gre_print_1(netdissect_options *, const u_char *, u_int);
83 static int gre_sre_print(netdissect_options *, uint16_t, uint8_t, uint8_t, const u_char *, u_int);
84 static int gre_sre_ip_print(netdissect_options *, uint8_t, uint8_t, const u_char *, u_int);
85 static int gre_sre_asn_print(netdissect_options *, uint8_t, uint8_t, const u_char *, u_int);
86
87 void
88 gre_print(netdissect_options *ndo, const u_char *bp, u_int length)
89 {
90 u_int len = length, vers;
91
92 ndo->ndo_protocol = "gre";
93 ND_TCHECK_2(bp);
94 if (len < 2)
95 goto trunc;
96 vers = EXTRACT_BE_U_2(bp) & GRE_VERS_MASK;
97 ND_PRINT("GREv%u",vers);
98
99 switch(vers) {
100 case 0:
101 gre_print_0(ndo, bp, len);
102 break;
103 case 1:
104 gre_print_1(ndo, bp, len);
105 break;
106 default:
107 ND_PRINT(" ERROR: unknown-version");
108 break;
109 }
110 return;
111
112 trunc:
113 ND_PRINT("%s", tstr);
114 return;
115 }
116
117 static void
118 gre_print_0(netdissect_options *ndo, const u_char *bp, u_int length)
119 {
120 u_int len = length;
121 uint16_t flags, prot;
122
123 /* 16 bits ND_TCHECKed in gre_print() */
124 flags = EXTRACT_BE_U_2(bp);
125 if (ndo->ndo_vflag)
126 ND_PRINT(", Flags [%s]",
127 bittok2str(gre_flag_values,"none",flags));
128
129 len -= 2;
130 bp += 2;
131
132 ND_TCHECK_2(bp);
133 if (len < 2)
134 goto trunc;
135 prot = EXTRACT_BE_U_2(bp);
136 len -= 2;
137 bp += 2;
138
139 if ((flags & GRE_CP) | (flags & GRE_RP)) {
140 ND_TCHECK_2(bp);
141 if (len < 2)
142 goto trunc;
143 if (ndo->ndo_vflag)
144 ND_PRINT(", sum 0x%x", EXTRACT_BE_U_2(bp));
145 bp += 2;
146 len -= 2;
147
148 ND_TCHECK_2(bp);
149 if (len < 2)
150 goto trunc;
151 ND_PRINT(", off 0x%x", EXTRACT_BE_U_2(bp));
152 bp += 2;
153 len -= 2;
154 }
155
156 if (flags & GRE_KP) {
157 ND_TCHECK_4(bp);
158 if (len < 4)
159 goto trunc;
160 ND_PRINT(", key=0x%x", EXTRACT_BE_U_4(bp));
161 bp += 4;
162 len -= 4;
163 }
164
165 if (flags & GRE_SP) {
166 ND_TCHECK_4(bp);
167 if (len < 4)
168 goto trunc;
169 ND_PRINT(", seq %u", EXTRACT_BE_U_4(bp));
170 bp += 4;
171 len -= 4;
172 }
173
174 if (flags & GRE_RP) {
175 for (;;) {
176 uint16_t af;
177 uint8_t sreoff;
178 uint8_t srelen;
179
180 ND_TCHECK_4(bp);
181 if (len < 4)
182 goto trunc;
183 af = EXTRACT_BE_U_2(bp);
184 sreoff = EXTRACT_U_1(bp + 2);
185 srelen = EXTRACT_U_1(bp + 3);
186 bp += 4;
187 len -= 4;
188
189 if (af == 0 && srelen == 0)
190 break;
191
192 if (!gre_sre_print(ndo, af, sreoff, srelen, bp, len))
193 goto trunc;
194
195 if (len < srelen)
196 goto trunc;
197 bp += srelen;
198 len -= srelen;
199 }
200 }
201
202 if (ndo->ndo_eflag)
203 ND_PRINT(", proto %s (0x%04x)",
204 tok2str(ethertype_values,"unknown",prot),
205 prot);
206
207 ND_PRINT(", length %u",length);
208
209 if (ndo->ndo_vflag < 1)
210 ND_PRINT(": "); /* put in a colon as protocol demarc */
211 else
212 ND_PRINT("\n\t"); /* if verbose go multiline */
213
214 switch (prot) {
215 case ETHERTYPE_IP:
216 ip_print(ndo, bp, len);
217 break;
218 case ETHERTYPE_IPV6:
219 ip6_print(ndo, bp, len);
220 break;
221 case ETHERTYPE_MPLS:
222 mpls_print(ndo, bp, len);
223 break;
224 case ETHERTYPE_IPX:
225 ipx_print(ndo, bp, len);
226 break;
227 case ETHERTYPE_ATALK:
228 atalk_print(ndo, bp, len);
229 break;
230 case ETHERTYPE_GRE_ISO:
231 isoclns_print(ndo, bp, len);
232 break;
233 case ETHERTYPE_TEB:
234 ether_print(ndo, bp, len, ndo->ndo_snapend - bp, NULL, NULL);
235 break;
236 default:
237 ND_PRINT("gre-proto-0x%x", prot);
238 }
239 return;
240
241 trunc:
242 ND_PRINT("%s", tstr);
243 }
244
245 static void
246 gre_print_1(netdissect_options *ndo, const u_char *bp, u_int length)
247 {
248 u_int len = length;
249 uint16_t flags, prot;
250
251 /* 16 bits ND_TCHECKed in gre_print() */
252 flags = EXTRACT_BE_U_2(bp);
253 len -= 2;
254 bp += 2;
255
256 if (ndo->ndo_vflag)
257 ND_PRINT(", Flags [%s]",
258 bittok2str(gre_flag_values,"none",flags));
259
260 ND_TCHECK_2(bp);
261 if (len < 2)
262 goto trunc;
263 prot = EXTRACT_BE_U_2(bp);
264 len -= 2;
265 bp += 2;
266
267
268 if (flags & GRE_KP) {
269 uint32_t k;
270
271 ND_TCHECK_4(bp);
272 if (len < 4)
273 goto trunc;
274 k = EXTRACT_BE_U_4(bp);
275 ND_PRINT(", call %u", k & 0xffff);
276 len -= 4;
277 bp += 4;
278 }
279
280 if (flags & GRE_SP) {
281 ND_TCHECK_4(bp);
282 if (len < 4)
283 goto trunc;
284 ND_PRINT(", seq %u", EXTRACT_BE_U_4(bp));
285 bp += 4;
286 len -= 4;
287 }
288
289 if (flags & GRE_AP) {
290 ND_TCHECK_4(bp);
291 if (len < 4)
292 goto trunc;
293 ND_PRINT(", ack %u", EXTRACT_BE_U_4(bp));
294 bp += 4;
295 len -= 4;
296 }
297
298 if ((flags & GRE_SP) == 0)
299 ND_PRINT(", no-payload");
300
301 if (ndo->ndo_eflag)
302 ND_PRINT(", proto %s (0x%04x)",
303 tok2str(ethertype_values,"unknown",prot),
304 prot);
305
306 ND_PRINT(", length %u",length);
307
308 if ((flags & GRE_SP) == 0)
309 return;
310
311 if (ndo->ndo_vflag < 1)
312 ND_PRINT(": "); /* put in a colon as protocol demarc */
313 else
314 ND_PRINT("\n\t"); /* if verbose go multiline */
315
316 switch (prot) {
317 case ETHERTYPE_PPP:
318 ppp_print(ndo, bp, len);
319 break;
320 default:
321 ND_PRINT("gre-proto-0x%x", prot);
322 break;
323 }
324 return;
325
326 trunc:
327 ND_PRINT("%s", tstr);
328 }
329
330 static int
331 gre_sre_print(netdissect_options *ndo, uint16_t af, uint8_t sreoff,
332 uint8_t srelen, const u_char *bp, u_int len)
333 {
334 int ret;
335
336 switch (af) {
337 case GRESRE_IP:
338 ND_PRINT(", (rtaf=ip");
339 ret = gre_sre_ip_print(ndo, sreoff, srelen, bp, len);
340 ND_PRINT(")");
341 break;
342 case GRESRE_ASN:
343 ND_PRINT(", (rtaf=asn");
344 ret = gre_sre_asn_print(ndo, sreoff, srelen, bp, len);
345 ND_PRINT(")");
346 break;
347 default:
348 ND_PRINT(", (rtaf=0x%x)", af);
349 ret = 1;
350 }
351 return (ret);
352 }
353
354 static int
355 gre_sre_ip_print(netdissect_options *ndo, uint8_t sreoff, uint8_t srelen,
356 const u_char *bp, u_int len)
357 {
358 const u_char *up = bp;
359 char buf[INET_ADDRSTRLEN];
360
361 if (sreoff & 3) {
362 ND_PRINT(", badoffset=%u", sreoff);
363 return (1);
364 }
365 if (srelen & 3) {
366 ND_PRINT(", badlength=%u", srelen);
367 return (1);
368 }
369 if (sreoff >= srelen) {
370 ND_PRINT(", badoff/len=%u/%u", sreoff, srelen);
371 return (1);
372 }
373
374 while (srelen != 0) {
375 if (!ND_TTEST_4(bp))
376 return (0);
377 if (len < 4)
378 return (0);
379
380 addrtostr(bp, buf, sizeof(buf));
381 ND_PRINT(" %s%s",
382 ((bp - up) == sreoff) ? "*" : "", buf);
383
384 bp += 4;
385 len -= 4;
386 srelen -= 4;
387 }
388 return (1);
389 }
390
391 static int
392 gre_sre_asn_print(netdissect_options *ndo, uint8_t sreoff, uint8_t srelen,
393 const u_char *bp, u_int len)
394 {
395 const u_char *up = bp;
396
397 if (sreoff & 1) {
398 ND_PRINT(", badoffset=%u", sreoff);
399 return (1);
400 }
401 if (srelen & 1) {
402 ND_PRINT(", badlength=%u", srelen);
403 return (1);
404 }
405 if (sreoff >= srelen) {
406 ND_PRINT(", badoff/len=%u/%u", sreoff, srelen);
407 return (1);
408 }
409
410 while (srelen != 0) {
411 if (!ND_TTEST_2(bp))
412 return (0);
413 if (len < 2)
414 return (0);
415
416 ND_PRINT(" %s%x",
417 ((bp - up) == sreoff) ? "*" : "",
418 EXTRACT_BE_U_2(bp));
419
420 bp += 2;
421 len -= 2;
422 srelen -= 2;
423 }
424 return (1);
425 }