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