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