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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
|
/*
* Copyright (c) 2014 VMware, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "PacketParser.h"
//XXX consider moving to NdisGetDataBuffer.
const VOID *
OvsGetPacketBytes(const NET_BUFFER_LIST *nbl,
UINT32 len,
UINT32 srcOffset,
VOID *storage)
{
NDIS_STATUS status = NDIS_STATUS_SUCCESS;
PNET_BUFFER netBuffer = NET_BUFFER_LIST_FIRST_NB(nbl);
PMDL currentMdl;
BOOLEAN firstMDL = TRUE;
ULONG destOffset = 0;
VOID *dest = storage;
const UINT32 copyLen = len;
ULONG packetLen;
packetLen = NET_BUFFER_DATA_LENGTH(netBuffer);
// Start copy from current MDL
currentMdl = NET_BUFFER_CURRENT_MDL(netBuffer);
// Data on current MDL may be offset from start of MDL
while (destOffset < copyLen && currentMdl) {
PUCHAR srcMemory = MmGetSystemAddressForMdlSafe(currentMdl,
LowPagePriority);
ULONG length = MmGetMdlByteCount(currentMdl);
if (!srcMemory) {
status = NDIS_STATUS_RESOURCES;
break;
}
if (firstMDL) {
ULONG mdlOffset = NET_BUFFER_CURRENT_MDL_OFFSET(netBuffer);
srcMemory += mdlOffset;
length -= mdlOffset;
firstMDL = FALSE;
}
length = MIN(length, packetLen);
packetLen -= length;
ASSERT((INT)packetLen >= 0);
if (srcOffset >= length) {
currentMdl = NDIS_MDL_LINKAGE(currentMdl);
srcOffset -= length;
continue;
} else {
srcMemory += srcOffset;
length -= srcOffset;
srcOffset = 0;
}
length = min(length, copyLen-destOffset);
NdisMoveMemory((PUCHAR)dest+destOffset, srcMemory, length);
destOffset += length;
currentMdl = NDIS_MDL_LINKAGE(currentMdl);
}
if (destOffset == copyLen) {
ASSERT(status == NDIS_STATUS_SUCCESS);
return storage;
}
return NULL;
}
NDIS_STATUS
OvsParseIPv6(const NET_BUFFER_LIST *packet,
OvsFlowKey *key,
POVS_PACKET_HDR_INFO layers)
{
UINT16 ofs = layers->l3Offset;
IPv6Hdr ipv6HdrStorage;
const IPv6Hdr *nh;
UINT32 nextHdr;
Ipv6Key *flow= &key->ipv6Key;
ofs = layers->l3Offset;
nh = OvsGetPacketBytes(packet, sizeof *nh, ofs, &ipv6HdrStorage);
if (!nh) {
return NDIS_STATUS_FAILURE;
}
nextHdr = nh->nexthdr;
memcpy(&flow->ipv6Src, nh->saddr.s6_addr, 16);
memcpy(&flow->ipv6Dst, nh->daddr.s6_addr, 16);
flow->nwTos = ((nh->flow_lbl[0] & 0xF0) >> 4) | (nh->priority << 4);
flow->ipv6Label =
((nh->flow_lbl[0] & 0x0F) << 16) | (nh->flow_lbl[1] << 8) | nh->flow_lbl[2];
flow->nwTtl = nh->hop_limit;
flow->nwProto = SOCKET_IPPROTO_NONE;
flow->nwFrag = OVS_FRAG_TYPE_NONE;
// Parse extended headers and compute L4 offset
ofs += sizeof(IPv6Hdr);
for (;;) {
if ((nextHdr != SOCKET_IPPROTO_HOPOPTS)
&& (nextHdr != SOCKET_IPPROTO_ROUTING)
&& (nextHdr != SOCKET_IPPROTO_DSTOPTS)
&& (nextHdr != SOCKET_IPPROTO_AH)
&& (nextHdr != SOCKET_IPPROTO_FRAGMENT)) {
/*
* It's either a terminal header (e.g., TCP, UDP) or one we
* don't understand. In either case, we're done with the
* packet, so use it to fill in 'nw_proto'.
*/
break;
}
if (nextHdr == SOCKET_IPPROTO_HOPOPTS
|| nextHdr == SOCKET_IPPROTO_ROUTING
|| nextHdr == SOCKET_IPPROTO_DSTOPTS
|| nextHdr == SOCKET_IPPROTO_AH) {
IPv6ExtHdr extHdrStorage;
const IPv6ExtHdr *extHdr;
UINT8 len;
extHdr = OvsGetPacketBytes(packet, sizeof *extHdr, ofs, &extHdrStorage);
if (!extHdr) {
return NDIS_STATUS_FAILURE;
}
len = extHdr->hdrExtLen;
ofs += nextHdr == SOCKET_IPPROTO_AH ? (len + 2) * 4 : (len + 1) * 8;
nextHdr = extHdr->nextHeader;
if (OvsPacketLenNBL(packet) < ofs) {
return NDIS_STATUS_FAILURE;
}
} else if (nextHdr == SOCKET_IPPROTO_FRAGMENT) {
IPv6FragHdr fragHdrStorage;
const IPv6FragHdr *fragHdr;
fragHdr = OvsGetPacketBytes(packet, sizeof *fragHdr, ofs,
&fragHdrStorage);
if (!fragHdr) {
return NDIS_STATUS_FAILURE;
}
nextHdr = fragHdr->nextHeader;
ofs += sizeof *fragHdr;
/* We only process the first fragment. */
if (fragHdr->offlg != htons(0)) {
if ((fragHdr->offlg & IP6F_OFF_HOST_ORDER_MASK) == htons(0)) {
flow->nwFrag = OVS_FRAG_TYPE_FIRST;
} else {
flow->nwFrag = OVS_FRAG_TYPE_LATER;
nextHdr = SOCKET_IPPROTO_FRAGMENT;
break;
}
}
}
}
flow->nwProto = (UINT8)nextHdr;
layers->l4Offset = ofs;
return NDIS_STATUS_SUCCESS;
}
VOID
OvsParseTcp(const NET_BUFFER_LIST *packet,
L4Key *flow,
POVS_PACKET_HDR_INFO layers)
{
TCPHdr tcpStorage;
const TCPHdr *tcp = OvsGetTcp(packet, layers->l4Offset, &tcpStorage);
if (tcp) {
flow->tpSrc = tcp->source;
flow->tpDst = tcp->dest;
layers->isTcp = 1;
layers->l7Offset = layers->l4Offset + 4 * tcp->doff;
}
}
VOID
OvsParseSctp(const NET_BUFFER_LIST *packet,
L4Key *flow,
POVS_PACKET_HDR_INFO layers)
{
SCTPHdr sctpStorage;
const SCTPHdr *sctp = OvsGetSctp(packet, layers->l4Offset, &sctpStorage);
if (sctp) {
flow->tpSrc = sctp->source;
flow->tpDst = sctp->dest;
layers->isSctp = 1;
layers->l7Offset = layers->l4Offset + sizeof *sctp;
}
}
VOID
OvsParseUdp(const NET_BUFFER_LIST *packet,
L4Key *flow,
POVS_PACKET_HDR_INFO layers)
{
UDPHdr udpStorage;
const UDPHdr *udp = OvsGetUdp(packet, layers->l4Offset, &udpStorage);
if (udp) {
flow->tpSrc = udp->source;
flow->tpDst = udp->dest;
layers->isUdp = 1;
if (udp->check == 0) {
layers->udpCsumZero = 1;
}
layers->l7Offset = layers->l4Offset + sizeof *udp;
}
}
NDIS_STATUS
OvsParseIcmpV6(const NET_BUFFER_LIST *packet,
OvsFlowKey *key,
POVS_PACKET_HDR_INFO layers)
{
UINT16 ofs = layers->l4Offset;
ICMPHdr icmpStorage;
const ICMPHdr *icmp;
Icmp6Key *flow = &key->icmp6Key;
memset(&flow->ndTarget, 0, sizeof(flow->ndTarget));
memset(flow->arpSha, 0, sizeof(flow->arpSha));
memset(flow->arpTha, 0, sizeof(flow->arpTha));
icmp = OvsGetIcmp(packet, ofs, &icmpStorage);
if (!icmp) {
return NDIS_STATUS_FAILURE;
}
ofs += sizeof *icmp;
/*
* The ICMPv6 type and code fields use the 16-bit transport port
* fields, so we need to store them in 16-bit network byte order.
*/
key->ipv6Key.l4.tpSrc = htons(icmp->type);
key->ipv6Key.l4.tpDst = htons(icmp->code);
if (icmp->code == 0 &&
(icmp->type == ND_NEIGHBOR_SOLICIT ||
icmp->type == ND_NEIGHBOR_ADVERT)) {
struct in6_addr ndTargetStorage;
const struct in6_addr *ndTarget;
ndTarget = OvsGetPacketBytes(packet, sizeof *ndTarget, ofs,
&ndTargetStorage);
if (!ndTarget) {
return NDIS_STATUS_FAILURE;
}
flow->ndTarget = *ndTarget;
while ((UINT32)(ofs + 8) <= OvsPacketLenNBL(packet)) {
/*
* The minimum size of an option is 8 bytes, which also is
* the size of Ethernet link-layer options.
*/
IPv6NdOptHdr ndOptStorage;
const IPv6NdOptHdr *ndOpt;
UINT16 optLen;
ndOpt = OvsGetPacketBytes(packet, sizeof *ndOpt, ofs, &ndOptStorage);
if (!ndOpt) {
return NDIS_STATUS_FAILURE;
}
optLen = ndOpt->len * 8;
if (!optLen || (UINT32)(ofs + optLen) > OvsPacketLenNBL(packet)) {
goto invalid;
}
/*
* Store the link layer address if the appropriate option is
* provided. It is considered an error if the same link
* layer option is specified twice.
*/
if (ndOpt->type == ND_OPT_SOURCE_LINKADDR && optLen == 8) {
if (Eth_IsNullAddr(flow->arpSha)) {
memcpy(flow->arpSha, ndOpt + 1, ETH_ADDR_LENGTH);
} else {
goto invalid;
}
} else if (ndOpt->type == ND_OPT_TARGET_LINKADDR && optLen == 8) {
if (Eth_IsNullAddr(flow->arpTha)) {
memcpy(flow->arpTha, ndOpt + 1, ETH_ADDR_LENGTH);
} else {
goto invalid;
}
}
ofs += optLen;
}
}
layers->l7Offset = ofs;
return NDIS_STATUS_SUCCESS;
invalid:
memset(&flow->ndTarget, 0, sizeof(flow->ndTarget));
memset(flow->arpSha, 0, sizeof(flow->arpSha));
memset(flow->arpTha, 0, sizeof(flow->arpTha));
return NDIS_STATUS_FAILURE;
}
|