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