]>
The Tcpdump Group git mirrors - tcpdump/blob - print-gre.c
1 /* $OpenBSD: print-gre.c,v 1.6 2002/10/30 03:04:04 fgsch Exp $ */
4 * Copyright (c) 2002 Jason L. Wright (jason@thought.net)
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
34 /* \summary: Generic Routing Encapsulation (GRE) printer */
37 * netdissect printer for GRE - Generic Routing Encapsulation
38 * RFC1701 (GRE), RFC1702 (GRE IPv4), and RFC2637 (Enhanced GRE)
45 #include "netdissect-stdinc.h"
49 #include "netdissect.h"
50 #include "addrtostr.h"
52 #include "ethertype.h"
54 static const char tstr
[] = "[|gre]";
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 */
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"},
75 #define GRE_VERS_MASK 0x0007 /* protocol version */
77 /* source route entry types */
78 #define GRESRE_IP 0x0800 /* IP */
79 #define GRESRE_ASN 0xfffe /* ASN */
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
);
88 gre_print(netdissect_options
*ndo
, const u_char
*bp
, u_int length
)
90 u_int len
= length
, vers
;
92 ndo
->ndo_protocol
= "gre";
96 vers
= EXTRACT_BE_U_2(bp
) & GRE_VERS_MASK
;
97 ND_PRINT("GREv%u",vers
);
101 gre_print_0(ndo
, bp
, len
);
104 gre_print_1(ndo
, bp
, len
);
107 ND_PRINT(" ERROR: unknown-version");
113 ND_PRINT("%s", tstr
);
118 gre_print_0(netdissect_options
*ndo
, const u_char
*bp
, u_int length
)
121 uint16_t flags
, prot
;
123 /* 16 bits ND_TCHECKed in gre_print() */
124 flags
= EXTRACT_BE_U_2(bp
);
126 ND_PRINT(", Flags [%s]",
127 bittok2str(gre_flag_values
,"none",flags
));
135 prot
= EXTRACT_BE_U_2(bp
);
139 if ((flags
& GRE_CP
) | (flags
& GRE_RP
)) {
144 ND_PRINT(", sum 0x%x", EXTRACT_BE_U_2(bp
));
151 ND_PRINT(", off 0x%x", EXTRACT_BE_U_2(bp
));
156 if (flags
& GRE_KP
) {
160 ND_PRINT(", key=0x%x", EXTRACT_BE_U_4(bp
));
165 if (flags
& GRE_SP
) {
169 ND_PRINT(", seq %u", EXTRACT_BE_U_4(bp
));
174 if (flags
& GRE_RP
) {
183 af
= EXTRACT_BE_U_2(bp
);
184 sreoff
= EXTRACT_U_1(bp
+ 2);
185 srelen
= EXTRACT_U_1(bp
+ 3);
189 if (af
== 0 && srelen
== 0)
192 if (!gre_sre_print(ndo
, af
, sreoff
, srelen
, bp
, len
))
203 ND_PRINT(", proto %s (0x%04x)",
204 tok2str(ethertype_values
,"unknown",prot
),
207 ND_PRINT(", length %u",length
);
209 if (ndo
->ndo_vflag
< 1)
210 ND_PRINT(": "); /* put in a colon as protocol demarc */
212 ND_PRINT("\n\t"); /* if verbose go multiline */
216 ip_print(ndo
, bp
, len
);
219 ip6_print(ndo
, bp
, len
);
222 mpls_print(ndo
, bp
, len
);
225 ipx_print(ndo
, bp
, len
);
227 case ETHERTYPE_ATALK
:
228 atalk_print(ndo
, bp
, len
);
230 case ETHERTYPE_GRE_ISO
:
231 isoclns_print(ndo
, bp
, len
);
234 ether_print(ndo
, bp
, len
, ndo
->ndo_snapend
- bp
, NULL
, NULL
);
237 ND_PRINT("gre-proto-0x%x", prot
);
242 ND_PRINT("%s", tstr
);
246 gre_print_1(netdissect_options
*ndo
, const u_char
*bp
, u_int length
)
249 uint16_t flags
, prot
;
251 /* 16 bits ND_TCHECKed in gre_print() */
252 flags
= EXTRACT_BE_U_2(bp
);
257 ND_PRINT(", Flags [%s]",
258 bittok2str(gre_flag_values
,"none",flags
));
263 prot
= EXTRACT_BE_U_2(bp
);
268 if (flags
& GRE_KP
) {
274 k
= EXTRACT_BE_U_4(bp
);
275 ND_PRINT(", call %u", k
& 0xffff);
280 if (flags
& GRE_SP
) {
284 ND_PRINT(", seq %u", EXTRACT_BE_U_4(bp
));
289 if (flags
& GRE_AP
) {
293 ND_PRINT(", ack %u", EXTRACT_BE_U_4(bp
));
298 if ((flags
& GRE_SP
) == 0)
299 ND_PRINT(", no-payload");
302 ND_PRINT(", proto %s (0x%04x)",
303 tok2str(ethertype_values
,"unknown",prot
),
306 ND_PRINT(", length %u",length
);
308 if ((flags
& GRE_SP
) == 0)
311 if (ndo
->ndo_vflag
< 1)
312 ND_PRINT(": "); /* put in a colon as protocol demarc */
314 ND_PRINT("\n\t"); /* if verbose go multiline */
318 ppp_print(ndo
, bp
, len
);
321 ND_PRINT("gre-proto-0x%x", prot
);
327 ND_PRINT("%s", tstr
);
331 gre_sre_print(netdissect_options
*ndo
, uint16_t af
, uint8_t sreoff
,
332 uint8_t srelen
, const u_char
*bp
, u_int len
)
338 ND_PRINT(", (rtaf=ip");
339 ret
= gre_sre_ip_print(ndo
, sreoff
, srelen
, bp
, len
);
343 ND_PRINT(", (rtaf=asn");
344 ret
= gre_sre_asn_print(ndo
, sreoff
, srelen
, bp
, len
);
348 ND_PRINT(", (rtaf=0x%x)", af
);
355 gre_sre_ip_print(netdissect_options
*ndo
, uint8_t sreoff
, uint8_t srelen
,
356 const u_char
*bp
, u_int len
)
358 const u_char
*up
= bp
;
359 char buf
[INET_ADDRSTRLEN
];
362 ND_PRINT(", badoffset=%u", sreoff
);
366 ND_PRINT(", badlength=%u", srelen
);
369 if (sreoff
>= srelen
) {
370 ND_PRINT(", badoff/len=%u/%u", sreoff
, srelen
);
374 while (srelen
!= 0) {
380 addrtostr(bp
, buf
, sizeof(buf
));
382 ((bp
- up
) == sreoff
) ? "*" : "", buf
);
392 gre_sre_asn_print(netdissect_options
*ndo
, uint8_t sreoff
, uint8_t srelen
,
393 const u_char
*bp
, u_int len
)
395 const u_char
*up
= bp
;
398 ND_PRINT(", badoffset=%u", sreoff
);
402 ND_PRINT(", badlength=%u", srelen
);
405 if (sreoff
>= srelen
) {
406 ND_PRINT(", badoff/len=%u/%u", sreoff
, srelen
);
410 while (srelen
!= 0) {
417 ((bp
- up
) == sreoff
) ? "*" : "",