2 * Copyright (c) 2015, 2016 Cloudbase Solutions Srl
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
27 #include "PacketParser.h"
36 #define OVS_DBG_MOD OVS_DBG_GRE
39 OvsDoEncapGre(POVS_VPORT_ENTRY vport, PNET_BUFFER_LIST curNbl,
40 const OvsIPv4TunnelKey *tunKey,
41 const POVS_FWD_INFO fwdInfo,
42 POVS_PACKET_HDR_INFO layers,
43 POVS_SWITCH_CONTEXT switchContext,
44 PNET_BUFFER_LIST *newNbl);
47 * --------------------------------------------------------------------------
49 * Initialize GRE tunnel module.
50 * --------------------------------------------------------------------------
53 OvsInitGreTunnel(POVS_VPORT_ENTRY vport)
55 POVS_GRE_VPORT grePort;
57 grePort = (POVS_GRE_VPORT)OvsAllocateMemoryWithTag(sizeof(*grePort),
60 OVS_LOG_ERROR("Insufficient memory, can't allocate OVS_GRE_VPORT");
61 return STATUS_INSUFFICIENT_RESOURCES;
64 RtlZeroMemory(grePort, sizeof(*grePort));
65 vport->priv = (PVOID)grePort;
66 return STATUS_SUCCESS;
70 * --------------------------------------------------------------------------
71 * OvsCleanupGreTunnel --
72 * Cleanup GRE Tunnel module.
73 * --------------------------------------------------------------------------
76 OvsCleanupGreTunnel(POVS_VPORT_ENTRY vport)
78 if (vport->ovsType != OVS_VPORT_TYPE_GRE ||
79 vport->priv == NULL) {
83 OvsFreeMemoryWithTag(vport->priv, OVS_GRE_POOL_TAG);
88 * --------------------------------------------------------------------------
90 * Encapsulates a packet with an GRE header.
91 * --------------------------------------------------------------------------
94 OvsEncapGre(POVS_VPORT_ENTRY vport,
95 PNET_BUFFER_LIST curNbl,
96 OvsIPv4TunnelKey *tunKey,
97 POVS_SWITCH_CONTEXT switchContext,
98 POVS_PACKET_HDR_INFO layers,
99 PNET_BUFFER_LIST *newNbl)
101 OVS_FWD_INFO fwdInfo;
104 status = OvsLookupIPFwdInfo(tunKey->dst, &fwdInfo);
105 if (status != STATUS_SUCCESS) {
106 OvsFwdIPHelperRequest(NULL, 0, tunKey, NULL, NULL, NULL);
107 return NDIS_STATUS_FAILURE;
110 status = OvsDoEncapGre(vport, curNbl, tunKey, &fwdInfo, layers,
111 switchContext, newNbl);
116 * --------------------------------------------------------------------------
118 * Internal utility function which actually does the GRE encap.
119 * --------------------------------------------------------------------------
122 OvsDoEncapGre(POVS_VPORT_ENTRY vport,
123 PNET_BUFFER_LIST curNbl,
124 const OvsIPv4TunnelKey *tunKey,
125 const POVS_FWD_INFO fwdInfo,
126 POVS_PACKET_HDR_INFO layers,
127 POVS_SWITCH_CONTEXT switchContext,
128 PNET_BUFFER_LIST *newNbl)
137 POVS_GRE_VPORT vportGre;
138 UINT32 headRoom = GreTunHdrSize(tunKey->flags);
140 UINT32 counterHeadRoom;
144 ASSERT(*newNbl == NULL);
146 curNb = NET_BUFFER_LIST_FIRST_NB(curNbl);
147 packetLength = NET_BUFFER_DATA_LENGTH(curNb);
150 mss = OVSGetTcpMSS(curNbl);
152 OVS_LOG_TRACE("MSS %u packet len %u", mss,
155 OVS_LOG_TRACE("l4Offset %d", layers->l4Offset);
156 *newNbl = OvsTcpSegmentNBL(switchContext, curNbl, layers,
158 if (*newNbl == NULL) {
159 OVS_LOG_ERROR("Unable to segment NBL");
160 return NDIS_STATUS_FAILURE;
162 /* Clear out LSO flags after this point */
163 NET_BUFFER_LIST_INFO(*newNbl, TcpLargeSendNetBufferListInfo) = 0;
167 vportGre = (POVS_GRE_VPORT)GetOvsVportPriv(vport);
170 /* If we didn't split the packet above, make a copy now */
171 if (*newNbl == NULL) {
172 *newNbl = OvsPartialCopyNBL(switchContext, curNbl, 0, headRoom,
174 if (*newNbl == NULL) {
175 OVS_LOG_ERROR("Unable to copy NBL");
176 return NDIS_STATUS_FAILURE;
179 NDIS_TCP_IP_CHECKSUM_NET_BUFFER_LIST_INFO csumInfo;
180 csumInfo.Value = NET_BUFFER_LIST_INFO(curNbl,
181 TcpIpChecksumNetBufferListInfo);
183 status = OvsApplySWChecksumOnNB(layers, *newNbl, &csumInfo);
184 if (status != NDIS_STATUS_SUCCESS) {
190 for (curNb = NET_BUFFER_LIST_FIRST_NB(curNbl); curNb != NULL;
191 curNb = curNb->Next) {
193 counterHeadRoom = headRoom;
195 status = NdisRetreatNetBufferDataStart(curNb, headRoom, 0, NULL);
196 if (status != NDIS_STATUS_SUCCESS) {
200 curMdl = NET_BUFFER_CURRENT_MDL(curNb);
201 bufferStart = (PUINT8)MmGetSystemAddressForMdlSafe(curMdl,
204 status = NDIS_STATUS_RESOURCES;
208 bufferStart += NET_BUFFER_CURRENT_MDL_OFFSET(curNb);
209 if (NET_BUFFER_NEXT_NB(curNb)) {
210 OVS_LOG_TRACE("nb length %u next %u",
211 NET_BUFFER_DATA_LENGTH(curNb),
212 NET_BUFFER_DATA_LENGTH(curNb->Next));
216 ethHdr = (EthHdr *)bufferStart;
217 ASSERT(((PCHAR)&fwdInfo->dstMacAddr + sizeof fwdInfo->dstMacAddr) ==
218 (PCHAR)&fwdInfo->srcMacAddr);
219 NdisMoveMemory(ethHdr->Destination, fwdInfo->dstMacAddr,
220 sizeof ethHdr->Destination + sizeof ethHdr->Source);
221 ethHdr->Type = htons(ETH_TYPE_IPV4);
223 counterHeadRoom -= sizeof *ethHdr;
227 ipHdr = (IPHdr *)((PCHAR)ethHdr + sizeof *ethHdr);
229 ipHdr->ihl = sizeof *ipHdr / 4;
230 ipHdr->version = IPPROTO_IPV4;
231 ipHdr->tos = tunKey->tos;
232 ipHdr->tot_len = htons(NET_BUFFER_DATA_LENGTH(curNb) - sizeof *ethHdr);
233 ipHdr->id = (uint16)atomic_add64(&vportGre->ipId,
234 NET_BUFFER_DATA_LENGTH(curNb));
235 ipHdr->frag_off = (tunKey->flags & OVS_TNL_F_DONT_FRAGMENT) ?
237 ipHdr->ttl = tunKey->ttl ? tunKey->ttl : 64;
238 ipHdr->protocol = IPPROTO_GRE;
239 ASSERT(tunKey->dst == fwdInfo->dstIpAddr);
240 ASSERT(tunKey->src == fwdInfo->srcIpAddr || tunKey->src == 0);
241 ipHdr->saddr = fwdInfo->srcIpAddr;
242 ipHdr->daddr = fwdInfo->dstIpAddr;
245 ipHdr->check = IPChecksum((UINT8 *)ipHdr, sizeof *ipHdr, 0);
247 counterHeadRoom -= sizeof *ipHdr;
251 greHdr = (GREHdr *)((PCHAR)ipHdr + sizeof *ipHdr);
252 greHdr->flags = OvsTunnelFlagsToGreFlags(tunKey->flags);
253 greHdr->protocolType = GRE_NET_TEB;
255 counterHeadRoom -= sizeof *greHdr;
258 PCHAR currentOffset = (PCHAR)greHdr + sizeof *greHdr;
260 if (tunKey->flags & OVS_TNL_F_CSUM) {
261 RtlZeroMemory(currentOffset, 4);
264 counterHeadRoom -= 4;
268 if (tunKey->flags & OVS_TNL_F_KEY) {
269 RtlZeroMemory(currentOffset, 4);
270 UINT32 key = (tunKey->tunnelId >> 32);
271 RtlCopyMemory(currentOffset, &key, sizeof key);
274 counterHeadRoom -= 4;
279 ASSERT(counterHeadRoom == 0);
283 return STATUS_SUCCESS;
286 OvsCompleteNBL(switchContext, *newNbl, TRUE);
292 OvsDecapGre(POVS_SWITCH_CONTEXT switchContext,
293 PNET_BUFFER_LIST curNbl,
294 OvsIPv4TunnelKey *tunKey,
295 PNET_BUFFER_LIST *newNbl)
302 UINT32 tunnelSize = 0, packetLength = 0;
307 curNb = NET_BUFFER_LIST_FIRST_NB(curNbl);
308 packetLength = NET_BUFFER_DATA_LENGTH(curNb);
309 tunnelSize = GreTunHdrSize(tunKey->flags);
310 if (packetLength <= tunnelSize) {
311 return NDIS_STATUS_INVALID_LENGTH;
315 * Create a copy of the NBL so that we have all the headers in one MDL.
317 *newNbl = OvsPartialCopyNBL(switchContext, curNbl,
318 tunnelSize + OVS_DEFAULT_COPY_SIZE, 0,
319 TRUE /*copy NBL info */);
321 if (*newNbl == NULL) {
322 return NDIS_STATUS_RESOURCES;
326 curNb = NET_BUFFER_LIST_FIRST_NB(curNbl);
327 curMdl = NET_BUFFER_CURRENT_MDL(curNb);
328 bufferStart = (PUINT8)MmGetSystemAddressForMdlSafe(curMdl, LowPagePriority) +
329 NET_BUFFER_CURRENT_MDL_OFFSET(curNb);
331 status = NDIS_STATUS_RESOURCES;
335 ethHdr = (EthHdr *)bufferStart;
336 headRoom += sizeof *ethHdr;
338 ipHdr = (IPHdr *)((PCHAR)ethHdr + sizeof *ethHdr);
339 tunKey->src = ipHdr->saddr;
340 tunKey->dst = ipHdr->daddr;
341 tunKey->tos = ipHdr->tos;
342 tunKey->ttl = ipHdr->ttl;
344 headRoom += sizeof *ipHdr;
346 greHdr = (GREHdr *)((PCHAR)ipHdr + sizeof *ipHdr);
347 headRoom += sizeof *greHdr;
349 /* Validate if GRE header protocol type. */
350 if (greHdr->protocolType != GRE_NET_TEB) {
351 status = STATUS_NDIS_INVALID_PACKET;
355 PCHAR currentOffset = (PCHAR)greHdr + sizeof *greHdr;
357 if (greHdr->flags & GRE_CSUM) {
358 tunKey->flags |= OVS_TNL_F_CSUM;
363 if (greHdr->flags & GRE_KEY) {
364 tunKey->flags |= OVS_TNL_F_KEY;
366 RtlCopyMemory(&key, currentOffset, 4);
367 tunKey->tunnelId = (UINT64)key << 32;
372 /* Clear out the receive flag for the inner packet. */
373 NET_BUFFER_LIST_INFO(curNbl, TcpIpChecksumNetBufferListInfo) = 0;
374 NdisAdvanceNetBufferDataStart(curNb, GreTunHdrSize(tunKey->flags), FALSE,
376 ASSERT(headRoom == GreTunHdrSize(tunKey->flags));
377 return NDIS_STATUS_SUCCESS;
380 OvsCompleteNBL(switchContext, *newNbl, TRUE);