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