]> The Tcpdump Group git mirrors - tcpdump/blob - print-gre.c
gre: clean up GRE "version 1" (PPTP) parsing of "key" field.
[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 /*
85 * Ethertype values used for GRE (but not elsewhere?).
86 */
87 #define GRE_CDP 0x2000 /* Cisco Discovery Protocol */
88 #define GRE_NHRP 0x2001 /* Next Hop Resolution Protocol */
89 #define GRE_ERSPAN_III 0x22eb
90 #define GRE_WCCP 0x883e /* Web Cache C* Protocol */
91 #define GRE_ERSPAN_I_II 0x88be
92
93 struct wccp_redirect {
94 nd_uint8_t flags;
95 #define WCCP_T (1 << 7)
96 #define WCCP_A (1 << 6)
97 #define WCCP_U (1 << 5)
98 nd_uint8_t ServiceId;
99 nd_uint8_t AltBucket;
100 nd_uint8_t PriBucket;
101 };
102
103 static void gre_print_0(netdissect_options *, const u_char *, u_int);
104 static void gre_print_1(netdissect_options *, const u_char *, u_int);
105 static int gre_sre_print(netdissect_options *, uint16_t, uint8_t, uint8_t, const u_char *, u_int);
106 static int gre_sre_ip_print(netdissect_options *, uint8_t, uint8_t, const u_char *, u_int);
107 static int gre_sre_asn_print(netdissect_options *, uint8_t, uint8_t, const u_char *, u_int);
108
109 void
110 gre_print(netdissect_options *ndo, const u_char *bp, u_int length)
111 {
112 u_int vers;
113
114 ndo->ndo_protocol = "gre";
115 nd_print_protocol_caps(ndo);
116 ND_ICHECK_U(length, <, 2);
117 vers = GET_BE_U_2(bp) & GRE_VERS_MASK;
118 ND_PRINT("v%u",vers);
119
120 switch(vers) {
121 case 0:
122 gre_print_0(ndo, bp, length);
123 break;
124 case 1:
125 gre_print_1(ndo, bp, length);
126 break;
127 default:
128 ND_PRINT(" ERROR: unknown-version");
129 break;
130 }
131 return;
132
133 invalid:
134 nd_print_invalid(ndo);
135 }
136
137 static void
138 gre_print_0(netdissect_options *ndo, const u_char *bp, u_int length)
139 {
140 u_int len = length;
141 uint16_t flags, prot;
142
143 ND_ICHECK_U(len, <, 2);
144 flags = GET_BE_U_2(bp);
145 if (ndo->ndo_vflag)
146 ND_PRINT(", Flags [%s]",
147 bittok2str(gre_flag_values,"none",flags));
148
149 len -= 2;
150 bp += 2;
151
152 ND_ICHECK_U(len, <, 2);
153 prot = GET_BE_U_2(bp);
154 len -= 2;
155 bp += 2;
156
157 if ((flags & GRE_CP) | (flags & GRE_RP)) {
158 uint16_t sum;
159
160 ND_ICHECK_U(len, <, 2);
161 sum = GET_BE_U_2(bp);
162 if (ndo->ndo_vflag)
163 ND_PRINT(", sum 0x%x", sum);
164 bp += 2;
165 len -= 2;
166
167 ND_ICHECK_U(len, <, 2);
168 ND_PRINT(", off 0x%x", GET_BE_U_2(bp));
169 bp += 2;
170 len -= 2;
171 }
172
173 if (flags & GRE_KP) {
174 uint32_t key;
175
176 ND_ICHECK_U(len, <, 4);
177 key = GET_BE_U_4(bp);
178 bp += 4;
179 len -= 4;
180
181 /*
182 * OpenBSD shows this as both a 32-bit
183 * (decimal) key value and a VSID+FlowID
184 * pair, with the VSID in decimal and
185 * the FlowID in hex, as key=<Key>|<VSID>+<FlowID>,
186 * in case this is NVGRE.
187 */
188 ND_PRINT(", key=0x%x", key);
189 }
190
191 if (flags & GRE_SP) {
192 ND_ICHECK_U(len, <, 4);
193 ND_PRINT(", seq %u", GET_BE_U_4(bp));
194 bp += 4;
195 len -= 4;
196 }
197
198 if (flags & GRE_RP) {
199 for (;;) {
200 uint16_t af;
201 uint8_t sreoff;
202 uint8_t srelen;
203
204 ND_ICHECK_U(len, <, 4);
205 af = GET_BE_U_2(bp);
206 sreoff = GET_U_1(bp + 2);
207 srelen = GET_U_1(bp + 3);
208 bp += 4;
209 len -= 4;
210
211 if (af == 0 && srelen == 0)
212 break;
213
214 if (!gre_sre_print(ndo, af, sreoff, srelen, bp, len))
215 goto invalid;
216
217 ND_ICHECK_U(len, <, srelen);
218 bp += srelen;
219 len -= srelen;
220 }
221 }
222
223 if (ndo->ndo_eflag)
224 ND_PRINT(", proto %s (0x%04x)",
225 tok2str(ethertype_values,"unknown",prot), prot);
226
227 ND_PRINT(", length %u",length);
228
229 if (ndo->ndo_vflag < 1)
230 ND_PRINT(": "); /* put in a colon as protocol demarc */
231 else
232 ND_PRINT("\n\t"); /* if verbose go multiline */
233
234 switch (prot) {
235 case 0x0000:
236 /*
237 * 0x0000 is reserved, but Cisco, at least, appears to
238 * use it for keep-alives; see, for example,
239 * https://www.cisco.com/c/en/us/support/docs/ip/generic-routing-encapsulation-gre/118370-technote-gre-00.html#anc1
240 */
241 printf("keep-alive");
242 break;
243 case GRE_WCCP:
244 /*
245 * This is a bit weird.
246 *
247 * This may either just mean "IPv4" or it may mean
248 * "IPv4 preceded by a WCCP redirect header". We
249 * check to see if the first octet looks like the
250 * beginning of an IPv4 header and, if not, dissect
251 * it "IPv4 preceded by a WCCP redirect header",
252 * otherwise we dissect it as just IPv4.
253 *
254 * See "Packet redirection" in draft-forster-wrec-wccp-v1-00,
255 * section 4.12 "Traffic Forwarding" in
256 * draft-wilson-wrec-wccp-v2-01, and section 3.12.1
257 * "Forwarding using GRE Encapsulation" in
258 * draft-param-wccp-v2rev1-01.
259 */
260 ND_PRINT("wccp ");
261
262 ND_ICHECK_U(len, <, 1);
263 if (GET_U_1(bp) >> 4 != 4) {
264 /*
265 * First octet isn't 0x4*, so it's not IPv4.
266 */
267 const struct wccp_redirect *wccp;
268 uint8_t wccp_flags;
269
270 ND_ICHECK_ZU(len, <, sizeof(*wccp));
271 wccp = (const struct wccp_redirect *)bp;
272 wccp_flags = GET_U_1(wccp->flags);
273
274 ND_PRINT("T:%c A:%c U:%c SId:%u Alt:%u Pri:%u",
275 (wccp_flags & WCCP_T) ? '1' : '0',
276 (wccp_flags & WCCP_A) ? '1' : '0',
277 (wccp_flags & WCCP_U) ? '1' : '0',
278 GET_U_1(wccp->ServiceId),
279 GET_U_1(wccp->AltBucket),
280 GET_U_1(wccp->PriBucket));
281
282 bp += sizeof(*wccp);
283 len -= sizeof(*wccp);
284
285 printf(": ");
286 }
287 /* FALLTHROUGH */
288 case ETHERTYPE_IP:
289 ip_print(ndo, bp, len);
290 break;
291 case ETHERTYPE_IPV6:
292 ip6_print(ndo, bp, len);
293 break;
294 case ETHERTYPE_MPLS:
295 case ETHERTYPE_MPLS_MULTI:
296 mpls_print(ndo, bp, len);
297 break;
298 case ETHERTYPE_IPX:
299 ipx_print(ndo, bp, len);
300 break;
301 case ETHERTYPE_ATALK:
302 atalk_print(ndo, bp, len);
303 break;
304 case ETHERTYPE_GRE_ISO:
305 isoclns_print(ndo, bp, len);
306 break;
307 case ETHERTYPE_TEB:
308 ether_print(ndo, bp, len, ND_BYTES_AVAILABLE_AFTER(bp), NULL, NULL);
309 break;
310 case ETHERTYPE_NSH:
311 nsh_print(ndo, bp, len);
312 break;
313 case GRE_ERSPAN_I_II:
314 erspan_print(ndo, flags, bp, len);
315 break;
316 case GRE_CDP:
317 cdp_print(ndo, bp, len);
318 break;
319 case GRE_NHRP:
320 nhrp_print(ndo, bp, len);
321 break;
322 default:
323 ND_PRINT("gre-proto-0x%x", prot);
324 }
325 return;
326
327 invalid:
328 nd_print_invalid(ndo);
329 }
330
331 static void
332 gre_print_1(netdissect_options *ndo, const u_char *bp, u_int length)
333 {
334 u_int len = length;
335 uint16_t flags, prot;
336
337 ND_ICHECK_U(len, <, 2);
338 flags = GET_BE_U_2(bp);
339 len -= 2;
340 bp += 2;
341
342 if (ndo->ndo_vflag)
343 ND_PRINT(", Flags [%s]",
344 bittok2str(gre_flag_values,"none",flags));
345
346 ND_ICHECK_U(len, <, 2);
347 prot = GET_BE_U_2(bp);
348 len -= 2;
349 bp += 2;
350
351 if (flags & GRE_KP) {
352 /* Skip payload length? */
353 ND_ICHECK_U(len, <, 2);
354 ND_TCHECK_LEN(bp, 2);
355 len -= 2;
356 bp += 2;
357
358 ND_ICHECK_U(len, <, 2);
359 ND_PRINT(", call %u", GET_BE_U_2(bp));
360 len -= 2;
361 bp += 2;
362 } else
363 ND_PRINT(", (ERROR: K flag not set)");
364
365 if (flags & GRE_SP) {
366 ND_ICHECK_U(len, <, 4);
367 ND_PRINT(", seq %u", GET_BE_U_4(bp));
368 bp += 4;
369 len -= 4;
370 }
371
372 if (flags & GRE_AP) {
373 ND_ICHECK_U(len, <, 4);
374 ND_PRINT(", ack %u", GET_BE_U_4(bp));
375 bp += 4;
376 len -= 4;
377 }
378
379 if ((flags & GRE_SP) == 0)
380 ND_PRINT(", no-payload");
381
382 if (ndo->ndo_eflag)
383 ND_PRINT(", proto %s (0x%04x)",
384 tok2str(ethertype_values,"unknown",prot), prot);
385
386 ND_PRINT(", length %u",length);
387
388 if ((flags & GRE_SP) == 0)
389 return;
390
391 if (ndo->ndo_vflag < 1)
392 ND_PRINT(": "); /* put in a colon as protocol demarc */
393 else
394 ND_PRINT("\n\t"); /* if verbose go multiline */
395
396 switch (prot) {
397 case ETHERTYPE_PPP:
398 ppp_print(ndo, bp, len);
399 break;
400 default:
401 ND_PRINT("gre-proto-0x%x", prot);
402 break;
403 }
404 return;
405
406 invalid:
407 nd_print_invalid(ndo);
408 }
409
410 static int
411 gre_sre_print(netdissect_options *ndo, uint16_t af, uint8_t sreoff,
412 uint8_t srelen, const u_char *bp, u_int len)
413 {
414 int ret;
415
416 switch (af) {
417 case GRESRE_IP:
418 ND_PRINT(", (rtaf=ip");
419 ret = gre_sre_ip_print(ndo, sreoff, srelen, bp, len);
420 ND_PRINT(")");
421 break;
422 case GRESRE_ASN:
423 ND_PRINT(", (rtaf=asn");
424 ret = gre_sre_asn_print(ndo, sreoff, srelen, bp, len);
425 ND_PRINT(")");
426 break;
427 default:
428 ND_PRINT(", (rtaf=0x%x)", af);
429 ret = 1;
430 }
431 return (ret);
432 }
433
434 static int
435 gre_sre_ip_print(netdissect_options *ndo, uint8_t sreoff, uint8_t srelen,
436 const u_char *bp, u_int len)
437 {
438 const u_char *up = bp;
439 char buf[INET_ADDRSTRLEN];
440
441 if (sreoff & 3) {
442 ND_PRINT(", badoffset=%u", sreoff);
443 goto invalid;
444 }
445 if (srelen & 3) {
446 ND_PRINT(", badlength=%u", srelen);
447 goto invalid;
448 }
449 if (sreoff >= srelen) {
450 ND_PRINT(", badoff/len=%u/%u", sreoff, srelen);
451 goto invalid;
452 }
453
454 while (srelen != 0) {
455 ND_ICHECK_U(len, <, 4);
456
457 ND_TCHECK_LEN(bp, sizeof(nd_ipv4));
458 addrtostr(bp, buf, sizeof(buf));
459 ND_PRINT(" %s%s",
460 ((bp - up) == sreoff) ? "*" : "", buf);
461
462 bp += 4;
463 len -= 4;
464 srelen -= 4;
465 }
466 return 1;
467
468 invalid:
469 return 0;
470 }
471
472 static int
473 gre_sre_asn_print(netdissect_options *ndo, uint8_t sreoff, uint8_t srelen,
474 const u_char *bp, u_int len)
475 {
476 const u_char *up = bp;
477
478 if (sreoff & 1) {
479 ND_PRINT(", badoffset=%u", sreoff);
480 goto invalid;
481 }
482 if (srelen & 1) {
483 ND_PRINT(", badlength=%u", srelen);
484 goto invalid;
485 }
486 if (sreoff >= srelen) {
487 ND_PRINT(", badoff/len=%u/%u", sreoff, srelen);
488 goto invalid;
489 }
490
491 while (srelen != 0) {
492 ND_ICHECK_U(len, <, 2);
493
494 ND_PRINT(" %s%x",
495 ((bp - up) == sreoff) ? "*" : "", GET_BE_U_2(bp));
496
497 bp += 2;
498 len -= 2;
499 srelen -= 2;
500 }
501 return 1;
502
503 invalid:
504 return 0;
505 }