]>
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
;
95 vers
= EXTRACT_16BITS(bp
) & GRE_VERS_MASK
;
96 ND_PRINT((ndo
, "GREv%u",vers
));
100 gre_print_0(ndo
, bp
, len
);
103 gre_print_1(ndo
, bp
, len
);
106 ND_PRINT((ndo
, " ERROR: unknown-version"));
112 ND_PRINT((ndo
, "%s", tstr
));
117 gre_print_0(netdissect_options
*ndo
, const u_char
*bp
, u_int length
)
120 uint16_t flags
, prot
;
122 flags
= EXTRACT_16BITS(bp
);
124 ND_PRINT((ndo
, ", Flags [%s]",
125 bittok2str(gre_flag_values
,"none",flags
)));
133 prot
= EXTRACT_16BITS(bp
);
137 if ((flags
& GRE_CP
) | (flags
& GRE_RP
)) {
142 ND_PRINT((ndo
, ", sum 0x%x", EXTRACT_16BITS(bp
)));
149 ND_PRINT((ndo
, ", off 0x%x", EXTRACT_16BITS(bp
)));
154 if (flags
& GRE_KP
) {
158 ND_PRINT((ndo
, ", key=0x%x", EXTRACT_32BITS(bp
)));
163 if (flags
& GRE_SP
) {
167 ND_PRINT((ndo
, ", seq %u", EXTRACT_32BITS(bp
)));
172 if (flags
& GRE_RP
) {
181 af
= EXTRACT_16BITS(bp
);
187 if (af
== 0 && srelen
== 0)
190 if (!gre_sre_print(ndo
, af
, sreoff
, srelen
, bp
, len
))
201 ND_PRINT((ndo
, ", proto %s (0x%04x)",
202 tok2str(ethertype_values
,"unknown",prot
),
205 ND_PRINT((ndo
, ", length %u",length
));
207 if (ndo
->ndo_vflag
< 1)
208 ND_PRINT((ndo
, ": ")); /* put in a colon as protocol demarc */
210 ND_PRINT((ndo
, "\n\t")); /* if verbose go multiline */
214 ip_print(ndo
, bp
, len
);
217 ip6_print(ndo
, bp
, len
);
220 mpls_print(ndo
, bp
, len
);
223 ipx_print(ndo
, bp
, len
);
225 case ETHERTYPE_ATALK
:
226 atalk_print(ndo
, bp
, len
);
228 case ETHERTYPE_GRE_ISO
:
229 isoclns_print(ndo
, bp
, len
);
232 ether_print(ndo
, bp
, len
, ndo
->ndo_snapend
- bp
, NULL
, NULL
);
235 ND_PRINT((ndo
, "gre-proto-0x%x", prot
));
240 ND_PRINT((ndo
, "%s", tstr
));
244 gre_print_1(netdissect_options
*ndo
, const u_char
*bp
, u_int length
)
247 uint16_t flags
, prot
;
249 flags
= EXTRACT_16BITS(bp
);
254 ND_PRINT((ndo
, ", Flags [%s]",
255 bittok2str(gre_flag_values
,"none",flags
)));
260 prot
= EXTRACT_16BITS(bp
);
265 if (flags
& GRE_KP
) {
271 k
= EXTRACT_32BITS(bp
);
272 ND_PRINT((ndo
, ", call %d", k
& 0xffff));
277 if (flags
& GRE_SP
) {
281 ND_PRINT((ndo
, ", seq %u", EXTRACT_32BITS(bp
)));
286 if (flags
& GRE_AP
) {
290 ND_PRINT((ndo
, ", ack %u", EXTRACT_32BITS(bp
)));
295 if ((flags
& GRE_SP
) == 0)
296 ND_PRINT((ndo
, ", no-payload"));
299 ND_PRINT((ndo
, ", proto %s (0x%04x)",
300 tok2str(ethertype_values
,"unknown",prot
),
303 ND_PRINT((ndo
, ", length %u",length
));
305 if ((flags
& GRE_SP
) == 0)
308 if (ndo
->ndo_vflag
< 1)
309 ND_PRINT((ndo
, ": ")); /* put in a colon as protocol demarc */
311 ND_PRINT((ndo
, "\n\t")); /* if verbose go multiline */
315 ppp_print(ndo
, bp
, len
);
318 ND_PRINT((ndo
, "gre-proto-0x%x", prot
));
324 ND_PRINT((ndo
, "%s", tstr
));
328 gre_sre_print(netdissect_options
*ndo
, uint16_t af
, uint8_t sreoff
,
329 uint8_t srelen
, const u_char
*bp
, u_int len
)
335 ND_PRINT((ndo
, ", (rtaf=ip"));
336 ret
= gre_sre_ip_print(ndo
, sreoff
, srelen
, bp
, len
);
337 ND_PRINT((ndo
, ")"));
340 ND_PRINT((ndo
, ", (rtaf=asn"));
341 ret
= gre_sre_asn_print(ndo
, sreoff
, srelen
, bp
, len
);
342 ND_PRINT((ndo
, ")"));
345 ND_PRINT((ndo
, ", (rtaf=0x%x)", af
));
352 gre_sre_ip_print(netdissect_options
*ndo
, uint8_t sreoff
, uint8_t srelen
,
353 const u_char
*bp
, u_int len
)
355 const u_char
*up
= bp
;
356 char buf
[INET_ADDRSTRLEN
];
359 ND_PRINT((ndo
, ", badoffset=%u", sreoff
));
363 ND_PRINT((ndo
, ", badlength=%u", srelen
));
366 if (sreoff
>= srelen
) {
367 ND_PRINT((ndo
, ", badoff/len=%u/%u", sreoff
, srelen
));
371 while (srelen
!= 0) {
372 if (!ND_TTEST2(*bp
, 4))
377 addrtostr(bp
, buf
, sizeof(buf
));
378 ND_PRINT((ndo
, " %s%s",
379 ((bp
- up
) == sreoff
) ? "*" : "", buf
));
389 gre_sre_asn_print(netdissect_options
*ndo
, uint8_t sreoff
, uint8_t srelen
,
390 const u_char
*bp
, u_int len
)
392 const u_char
*up
= bp
;
395 ND_PRINT((ndo
, ", badoffset=%u", sreoff
));
399 ND_PRINT((ndo
, ", badlength=%u", srelen
));
402 if (sreoff
>= srelen
) {
403 ND_PRINT((ndo
, ", badoff/len=%u/%u", sreoff
, srelen
));
407 while (srelen
!= 0) {
408 if (!ND_TTEST2(*bp
, 2))
413 ND_PRINT((ndo
, " %s%x",
414 ((bp
- up
) == sreoff
) ? "*" : "",
415 EXTRACT_16BITS(bp
)));