-
Notifications
You must be signed in to change notification settings - Fork 872
/
Copy pathprint-erspan.c
293 lines (248 loc) · 7.34 KB
/
print-erspan.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/* $OpenBSD: print-gre.c,v 1.6 2002/10/30 03:04:04 fgsch Exp $ */
/*
* Copyright (c) 2002 Jason L. Wright ([email protected])
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/* \summary: Cisco ERSPAN printer */
/*
* Specifications: I-D draft-foschiano-erspan-03.
*/
#include <config.h>
#include "netdissect-stdinc.h"
#define ND_LONGJMP_FROM_TCHECK
#include "netdissect.h"
#include "extract.h"
#include "gre.h"
/*
* ERSPAN Type II.
*/
#define ERSPAN2_VER_SHIFT 28
#define ERSPAN2_VER_MASK (0xfU << ERSPAN2_VER_SHIFT)
#define ERSPAN2_VER (0x1U << ERSPAN2_VER_SHIFT)
#define ERSPAN2_VLAN_SHIFT 16
#define ERSPAN2_VLAN_MASK (0xfffU << ERSPAN2_VLAN_SHIFT)
#define ERSPAN2_COS_SHIFT 13
#define ERSPAN2_COS_MASK (0x7U << ERSPAN2_COS_SHIFT)
#define ERSPAN2_EN_SHIFT 11
#define ERSPAN2_EN_MASK (0x3U << ERSPAN2_EN_SHIFT)
#define ERSPAN2_EN_NONE (0x0U << ERSPAN2_EN_SHIFT)
#define ERSPAN2_EN_ISL (0x1U << ERSPAN2_EN_SHIFT)
#define ERSPAN2_EN_DOT1Q (0x2U << ERSPAN2_EN_SHIFT)
#define ERSPAN2_EN_VLAN (0x3U << ERSPAN2_EN_SHIFT)
#define ERSPAN2_T_SHIFT 10
#define ERSPAN2_T_MASK (0x1U << ERSPAN2_T_SHIFT)
#define ERSPAN2_SID_SHIFT 0
#define ERSPAN2_SID_MASK (0x3ffU << ERSPAN2_SID_SHIFT)
#define ERSPAN2_INDEX_SHIFT 0
#define ERSPAN2_INDEX_MASK (0xfffffU << ERSPAN2_INDEX_SHIFT)
void
erspan_i_ii_print(netdissect_options *ndo, uint16_t flags, const u_char *bp, u_int len)
{
uint32_t hdr, ver, vlan, cos, en, sid, index;
ndo->ndo_protocol = "erspan";
nd_print_protocol(ndo);
if (!(flags & GRE_SP)) {
/*
* ERSPAN Type I; no header, just a raw Ethernet frame.
*/
ND_PRINT(" type1: ");
ether_print(ndo, bp, len, ND_BYTES_AVAILABLE_AFTER(bp), NULL, NULL);
return;
}
/*
* ERSPAN Type II.
*/
ND_ICHECK_U(len, <, 4);
hdr = GET_BE_U_4(bp);
bp += 4;
len -= 4;
ver = hdr & ERSPAN2_VER_MASK;
if (ver != ERSPAN2_VER) {
/*
* Not Type II.
*/
ver >>= ERSPAN2_VER_SHIFT;
ND_PRINT(" erspan-unknown-version-%x", ver);
return;
}
if (ndo->ndo_vflag)
ND_PRINT(" type2");
sid = (hdr & ERSPAN2_SID_MASK) >> ERSPAN2_SID_SHIFT;
ND_PRINT(" session %u", sid);
en = hdr & ERSPAN2_EN_MASK;
vlan = (hdr & ERSPAN2_VLAN_MASK) >> ERSPAN2_VLAN_SHIFT;
switch (en) {
case ERSPAN2_EN_NONE:
break;
case ERSPAN2_EN_ISL:
ND_PRINT(" isl %u", vlan);
break;
case ERSPAN2_EN_DOT1Q:
ND_PRINT(" vlan %u", vlan);
break;
case ERSPAN2_EN_VLAN:
ND_PRINT(" vlan payload");
break;
}
if (ndo->ndo_vflag) {
cos = (hdr & ERSPAN2_COS_MASK) >> ERSPAN2_COS_SHIFT;
ND_PRINT(" cos %u", cos);
if (hdr & ERSPAN2_T_MASK)
ND_PRINT(" truncated");
}
ND_ICHECK_U(len, <, 4);
hdr = GET_BE_U_4(bp);
bp += 4;
len -= 4;
if (ndo->ndo_vflag) {
index = (hdr & ERSPAN2_INDEX_MASK) >> ERSPAN2_INDEX_SHIFT;
ND_PRINT(" index %u", index);
}
ND_PRINT(": ");
ether_print(ndo, bp, len, ND_BYTES_AVAILABLE_AFTER(bp), NULL, NULL);
return;
invalid:
nd_print_invalid(ndo);
}
/*
* ERSPAN Type III.
*/
#define ERSPAN3_VER_SHIFT 28
#define ERSPAN3_VER_MASK (0xfU << ERSPAN3_VER_SHIFT)
#define ERSPAN3_VER (0x2U << ERSPAN3_VER_SHIFT)
#define ERSPAN3_VLAN_SHIFT 16
#define ERSPAN3_VLAN_MASK (0xfffU << ERSPAN3_VLAN_SHIFT)
#define ERSPAN3_COS_SHIFT 13
#define ERSPAN3_COS_MASK (0x7U << ERSPAN3_COS_SHIFT)
#define ERSPAN3_BSO_SHIFT 11
#define ERSPAN3_BSO_MASK (0x3U << ERSPAN3_BSO_SHIFT)
#define ERSPAN3_BSO_GOOD_UNKNOWN 0x0U
#define ERSPAN3_BSO_BAD 0x3U
#define ERSPAN3_BSO_SHORT 0x1U
#define ERSPAN3_BSO_OVERSIZED 0x2U
#define ERSPAN3_T_SHIFT 10
#define ERSPAN3_T_MASK (0x1U << ERSPAN3_T_SHIFT)
#define ERSPAN3_SID_SHIFT 0
#define ERSPAN3_SID_MASK (0x3ffU << ERSPAN3_SID_SHIFT)
#define ERSPAN3_P_SHIFT 15
#define ERSPAN3_P_MASK (0x1U << ERSPAN3_P_SHIFT)
#define ERSPAN3_FT_SHIFT 10
#define ERSPAN3_FT_MASK (0x1fU << ERSPAN3_FT_SHIFT)
#define ERSPAN3_FT_ETHERNET 0
#define ERSPAN3_FT_IP 2
#define ERSPAN3_HW_ID_SHIFT 4
#define ERSPAN3_HW_ID_MASK (0x3fU << ERSPAN3_HW_ID_SHIFT)
#define ERSPAN3_D_SHIFT 3
#define ERSPAN3_D_MASK (0x1U << ERSPAN3_D_SHIFT)
#define ERSPAN3_GRA_SHIFT 1
#define ERSPAN3_GRA_MASK (0x3U << ERSPAN3_GRA_SHIFT)
#define ERSPAN3_O_SHIFT 0
#define ERSPAN3_O_MASK (0x1U << ERSPAN3_O_SHIFT)
static const struct tok erspan3_bso_values[] = {
{ ERSPAN3_BSO_GOOD_UNKNOWN, "Good/unknown" },
{ ERSPAN3_BSO_BAD, "Bad" },
{ ERSPAN3_BSO_SHORT, "Short" },
{ ERSPAN3_BSO_OVERSIZED, "Oversized" },
{ 0, NULL }
};
static const struct tok erspan3_ft_values[] = {
{ ERSPAN3_FT_ETHERNET, "Ethernet" },
{ ERSPAN3_FT_IP, "IP" },
{ 0, NULL }
};
void
erspan_iii_print(netdissect_options *ndo, const u_char *bp, u_int len)
{
uint32_t hdr, hdr2, ver, cos, sid, ft;
ndo->ndo_protocol = "erspan";
nd_print_protocol(ndo);
/*
* We do not check the GRE flags; ERSPAN Type III always
* has an ERSPAN header.
*/
ND_ICHECK_U(len, <, 4);
hdr = GET_BE_U_4(bp);
bp += 4;
len -= 4;
ver = hdr & ERSPAN3_VER_MASK;
if (ver != ERSPAN3_VER) {
/*
* Not Type III.
*/
ver >>= ERSPAN3_VER_SHIFT;
ND_PRINT(" erspan-unknown-version-%x", ver);
return;
}
if (ndo->ndo_vflag)
ND_PRINT(" type3");
sid = (hdr & ERSPAN3_SID_MASK) >> ERSPAN3_SID_SHIFT;
ND_PRINT(" session %u", sid);
ND_PRINT(" bso %s",
tok2str(erspan3_bso_values, "unknown %x",
(hdr & ERSPAN3_BSO_MASK) >> ERSPAN3_BSO_SHIFT));
if (ndo->ndo_vflag) {
cos = (hdr & ERSPAN3_COS_MASK) >> ERSPAN3_COS_SHIFT;
ND_PRINT(" cos %u", cos);
if (hdr & ERSPAN3_T_MASK)
ND_PRINT(" truncated");
}
/* Skip timestamp */
ND_ICHECK_U(len, <, 4);
ND_TCHECK_4(bp);
bp += 4;
len -= 4;
/* Skip SGT */
ND_ICHECK_U(len, <, 2);
ND_TCHECK_2(bp);
bp += 2;
len -= 2;
/* Additional fields */
ND_ICHECK_U(len, <, 2);
hdr2 = GET_BE_U_2(bp);
bp += 2;
len -= 2;
ft = (hdr2 & ERSPAN3_FT_MASK) >> ERSPAN3_FT_SHIFT;
ND_PRINT(" ft %s",
tok2str(erspan3_ft_values, "unknown %x", ft));
/* Do we have the platform-specific header? */
if (hdr2 & ERSPAN3_O_MASK) {
/* Yes. Skip it. */
ND_ICHECK_U(len, <, 8);
ND_TCHECK_8(bp);
bp += 8;
len -= 8;
}
ND_PRINT(": ");
switch (ft) {
case ERSPAN3_FT_ETHERNET:
ether_print(ndo, bp, len, ND_BYTES_AVAILABLE_AFTER(bp), NULL, NULL);
break;
default:
ND_PRINT("Frame type unknown");
break;
}
return;
invalid:
nd_print_invalid(ndo);
}