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