]>
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.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
29 /* \summary: Generic Routing Encapsulation (GRE) printer */
32 * netdissect printer for GRE - Generic Routing Encapsulation
33 * RFC1701 (GRE), RFC1702 (GRE IPv4), and RFC2637 (Enhanced GRE)
38 #include "netdissect-stdinc.h"
40 #include "netdissect.h"
41 #include "addrtostr.h"
43 #include "ethertype.h"
46 #define GRE_CP 0x8000 /* checksum present */
47 #define GRE_RP 0x4000 /* routing present */
48 #define GRE_KP 0x2000 /* key present */
49 #define GRE_SP 0x1000 /* sequence# present */
50 #define GRE_sP 0x0800 /* source routing */
51 #define GRE_AP 0x0080 /* acknowledgment# present */
53 static const struct tok gre_flag_values
[] = {
54 { GRE_CP
, "checksum present"},
55 { GRE_RP
, "routing present"},
56 { GRE_KP
, "key present"},
57 { GRE_SP
, "sequence# present"},
58 { GRE_sP
, "source routing present"},
59 { GRE_AP
, "ack present"},
63 #define GRE_RECRS_MASK 0x0700 /* recursion count */
64 #define GRE_VERS_MASK 0x0007 /* protocol version */
66 /* source route entry types */
67 #define GRESRE_IP 0x0800 /* IP */
68 #define GRESRE_ASN 0xfffe /* ASN */
70 static void gre_print_0(netdissect_options
*, const u_char
*, u_int
);
71 static void gre_print_1(netdissect_options
*, const u_char
*, u_int
);
72 static int gre_sre_print(netdissect_options
*, uint16_t, uint8_t, uint8_t, const u_char
*, u_int
);
73 static int gre_sre_ip_print(netdissect_options
*, uint8_t, uint8_t, const u_char
*, u_int
);
74 static int gre_sre_asn_print(netdissect_options
*, uint8_t, uint8_t, const u_char
*, u_int
);
77 gre_print(netdissect_options
*ndo
, const u_char
*bp
, u_int length
)
79 u_int len
= length
, vers
;
81 ndo
->ndo_protocol
= "gre";
85 vers
= GET_BE_U_2(bp
) & GRE_VERS_MASK
;
86 ND_PRINT("GREv%u",vers
);
90 gre_print_0(ndo
, bp
, len
);
93 gre_print_1(ndo
, bp
, len
);
96 ND_PRINT(" ERROR: unknown-version");
106 gre_print_0(netdissect_options
*ndo
, const u_char
*bp
, u_int length
)
109 uint16_t flags
, prot
;
111 /* 16 bits ND_TCHECKed in gre_print() */
112 flags
= GET_BE_U_2(bp
);
114 ND_PRINT(", Flags [%s]",
115 bittok2str(gre_flag_values
,"none",flags
));
123 prot
= GET_BE_U_2(bp
);
127 if ((flags
& GRE_CP
) | (flags
& GRE_RP
)) {
132 ND_PRINT(", sum 0x%x", GET_BE_U_2(bp
));
139 ND_PRINT(", off 0x%x", GET_BE_U_2(bp
));
144 if (flags
& GRE_KP
) {
148 ND_PRINT(", key=0x%x", GET_BE_U_4(bp
));
153 if (flags
& GRE_SP
) {
157 ND_PRINT(", seq %u", GET_BE_U_4(bp
));
162 if (flags
& GRE_RP
) {
172 sreoff
= GET_U_1(bp
+ 2);
173 srelen
= GET_U_1(bp
+ 3);
177 if (af
== 0 && srelen
== 0)
180 if (!gre_sre_print(ndo
, af
, sreoff
, srelen
, bp
, len
))
191 ND_PRINT(", proto %s (0x%04x)",
192 tok2str(ethertype_values
,"unknown",prot
), prot
);
194 ND_PRINT(", length %u",length
);
196 if (ndo
->ndo_vflag
< 1)
197 ND_PRINT(": "); /* put in a colon as protocol demarc */
199 ND_PRINT("\n\t"); /* if verbose go multiline */
203 ip_print(ndo
, bp
, len
);
206 ip6_print(ndo
, bp
, len
);
209 mpls_print(ndo
, bp
, len
);
212 ipx_print(ndo
, bp
, len
);
214 case ETHERTYPE_ATALK
:
215 atalk_print(ndo
, bp
, len
);
217 case ETHERTYPE_GRE_ISO
:
218 isoclns_print(ndo
, bp
, len
);
221 ether_print(ndo
, bp
, len
, ND_BYTES_AVAILABLE_AFTER(bp
), NULL
, NULL
);
224 ND_PRINT("gre-proto-0x%x", prot
);
233 gre_print_1(netdissect_options
*ndo
, const u_char
*bp
, u_int length
)
236 uint16_t flags
, prot
;
238 /* 16 bits ND_TCHECKed in gre_print() */
239 flags
= GET_BE_U_2(bp
);
244 ND_PRINT(", Flags [%s]",
245 bittok2str(gre_flag_values
,"none",flags
));
250 prot
= GET_BE_U_2(bp
);
255 if (flags
& GRE_KP
) {
262 ND_PRINT(", call %u", k
& 0xffff);
267 if (flags
& GRE_SP
) {
271 ND_PRINT(", seq %u", GET_BE_U_4(bp
));
276 if (flags
& GRE_AP
) {
280 ND_PRINT(", ack %u", GET_BE_U_4(bp
));
285 if ((flags
& GRE_SP
) == 0)
286 ND_PRINT(", no-payload");
289 ND_PRINT(", proto %s (0x%04x)",
290 tok2str(ethertype_values
,"unknown",prot
), prot
);
292 ND_PRINT(", length %u",length
);
294 if ((flags
& GRE_SP
) == 0)
297 if (ndo
->ndo_vflag
< 1)
298 ND_PRINT(": "); /* put in a colon as protocol demarc */
300 ND_PRINT("\n\t"); /* if verbose go multiline */
304 ppp_print(ndo
, bp
, len
);
307 ND_PRINT("gre-proto-0x%x", prot
);
317 gre_sre_print(netdissect_options
*ndo
, uint16_t af
, uint8_t sreoff
,
318 uint8_t srelen
, const u_char
*bp
, u_int len
)
324 ND_PRINT(", (rtaf=ip");
325 ret
= gre_sre_ip_print(ndo
, sreoff
, srelen
, bp
, len
);
329 ND_PRINT(", (rtaf=asn");
330 ret
= gre_sre_asn_print(ndo
, sreoff
, srelen
, bp
, len
);
334 ND_PRINT(", (rtaf=0x%x)", af
);
341 gre_sre_ip_print(netdissect_options
*ndo
, uint8_t sreoff
, uint8_t srelen
,
342 const u_char
*bp
, u_int len
)
344 const u_char
*up
= bp
;
345 char buf
[INET_ADDRSTRLEN
];
348 ND_PRINT(", badoffset=%u", sreoff
);
352 ND_PRINT(", badlength=%u", srelen
);
355 if (sreoff
>= srelen
) {
356 ND_PRINT(", badoff/len=%u/%u", sreoff
, srelen
);
360 while (srelen
!= 0) {
365 addrtostr(bp
, buf
, sizeof(buf
));
367 ((bp
- up
) == sreoff
) ? "*" : "", buf
);
379 gre_sre_asn_print(netdissect_options
*ndo
, uint8_t sreoff
, uint8_t srelen
,
380 const u_char
*bp
, u_int len
)
382 const u_char
*up
= bp
;
385 ND_PRINT(", badoffset=%u", sreoff
);
389 ND_PRINT(", badlength=%u", srelen
);
392 if (sreoff
>= srelen
) {
393 ND_PRINT(", badoff/len=%u/%u", sreoff
, srelen
);
397 while (srelen
!= 0) {
403 ((bp
- up
) == sreoff
) ? "*" : "", GET_BE_U_2(bp
));