]> git.saurik.com Git - apple/network_cmds.git/blob - tcpdump.tproj/print-gre.c
network_cmds-76.tar.gz
[apple/network_cmds.git] / tcpdump.tproj / print-gre.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License."
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24 /*
25 * Copyright (c) 1996
26 * The Regents of the University of California. All rights reserved.
27 *
28 * Redistribution and use in source and binary forms are permitted
29 * provided that the above copyright notice and this paragraph are
30 * duplicated in all such forms and that any documentation,
31 * advertising materials, and other materials related to such
32 * distribution and use acknowledge that the software was developed
33 * by the University of California, Lawrence Berkeley Laboratory,
34 * Berkeley, CA. The name of the University may not be used to
35 * endorse or promote products derived from this software without
36 * specific prior written permission.
37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
38 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
39 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
40 *
41 * Initial contribution from John Hawkinson <jhawk@bbnplanet.com>
42 *
43 * This module implements support for decoding GRE (Generic Routing
44 * Encapsulation) tunnels; they're documented in RFC1701 and RFC1702.
45 * This code only supports the IP encapsulation thereof.
46 */
47
48 #ifndef lint
49 static const char rcsid[] =
50 "@(#) $Header: /cvs/Darwin/Commands/NeXT/network_cmds/tcpdump.tproj/print-gre.c,v 1.1.1.1 1999/05/02 03:58:33 wsanchez Exp $";
51 #endif
52
53 #include <sys/param.h>
54 #include <sys/time.h>
55 #include <sys/uio.h>
56 #include <sys/socket.h>
57
58 #include <netinet/in.h>
59 #include <netinet/in_systm.h>
60 #include <netinet/ip.h>
61
62 #include <netdb.h>
63 #include <stdio.h>
64
65 #include "interface.h"
66 #include "addrtoname.h"
67 #include "extract.h" /* must come after interface.h */
68
69 #define GRE_SIZE (20)
70
71 struct gre {
72 u_short flags;
73 u_short proto;
74 union {
75 struct gre_ckof {
76 u_short cksum;
77 u_short offset;
78 } gre_ckof;
79 u_int32_t key;
80 u_int32_t seq;
81 } gre_void1;
82 union {
83 u_int32_t key;
84 u_int32_t seq;
85 u_int32_t routing;
86 } gre_void2;
87 union {
88 u_int32_t seq;
89 u_int32_t routing;
90 } gre_void3;
91 union {
92 u_int32_t routing;
93 } gre_void4;
94 };
95
96 #define GRE_CP 0x8000 /* Checksum Present */
97 #define GRE_RP 0x4000 /* Routing Present */
98 #define GRE_KP 0x2000 /* Key Present */
99 #define GRE_SP 0x1000 /* Sequence Present */
100
101
102 #define GREPROTO_IP 0x0800
103
104
105 /*
106 * Deencapsulate and print a GRE-tunneled IP datagram
107 */
108 void
109 gre_print(const u_char *bp, u_int length)
110 {
111 const u_char *cp = bp + 4;
112 const struct gre *gre;
113 u_short flags, proto;
114
115 gre = (const struct gre *)bp;
116
117 if (length < GRE_SIZE) {
118 goto trunc;
119 }
120 flags = EXTRACT_16BITS(&gre->flags);
121 proto = EXTRACT_16BITS(&gre->proto);
122
123 if (vflag) {
124 /* Decode the flags */
125 putchar('[');
126 if (flags & GRE_CP)
127 putchar('C');
128 if (flags & GRE_RP)
129 putchar('R');
130 if (flags & GRE_KP)
131 putchar('K');
132 if (flags & GRE_SP)
133 putchar('S');
134 fputs("] ", stdout);
135 }
136 /* Checksum & Offset are present */
137 if ((flags & GRE_CP) | (flags & GRE_RP))
138 cp += 4;
139
140 /* We don't support routing fields (variable length) now. Punt. */
141 if (flags & GRE_RP)
142 return;
143
144 if (flags & GRE_KP)
145 cp += 4;
146 if (flags & GRE_SP)
147 cp += 4;
148
149 switch (proto) {
150
151 case GREPROTO_IP:
152 ip_print(cp, length - ((cp - bp) / sizeof(u_char)));
153 break;
154
155 default:
156 printf("gre-proto-0x%04X", proto);
157 break;
158 }
159 return;
160
161 trunc:
162 fputs("[|gre]", stdout);
163
164 }