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