]>
git.proxmox.com Git - mirror_edk2.git/blob - NetworkPkg/IpSecDxe/IkePacket.c
8fd395d43fa581c37e9ed91675b4a686107abe7a
2 IKE Packet related operation.
4 Copyright (c) 2010, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php.
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include "IpSecDebug.h"
17 #include "Ikev2/Utility.h"
20 Allocate a buffer for the IKE_PACKET and intitalize its Header and payloadlist.
22 @return The pointer of the IKE_PACKET.
30 IKE_PACKET
*IkePacket
;
32 IkePacket
= (IKE_PACKET
*) AllocateZeroPool (sizeof (IKE_PACKET
));
33 if (IkePacket
== NULL
) {
37 IkePacket
->RefCount
= 1;
38 InitializeListHead (&IkePacket
->PayloadList
);
40 IkePacket
->Header
= (IKE_HEADER
*) AllocateZeroPool (sizeof (IKE_HEADER
));
41 if (IkePacket
->Header
== NULL
) {
49 Free the IkePacket by the specified IKE_PACKET pointer.
51 @param[in] IkePacket The pointer of the IKE_PACKET to be freed.
56 IN IKE_PACKET
*IkePacket
60 IKE_PAYLOAD
*IkePayload
;
62 if (IkePacket
== NULL
) {
66 // Check if the Packet is referred by others.
68 if (--IkePacket
->RefCount
== 0) {
70 // Free IkePacket header
72 if (!IkePacket
->IsHdrExt
&& IkePacket
->Header
!= NULL
) {
73 FreePool (IkePacket
->Header
);
76 // Free the PayloadsBuff
78 if (!IkePacket
->IsPayloadsBufExt
&& IkePacket
->PayloadsBuf
!= NULL
) {
79 FreePool (IkePacket
->PayloadsBuf
);
82 // Iterate payloadlist and free all payloads
84 for (Entry
= (IkePacket
)->PayloadList
.ForwardLink
; Entry
!= &(IkePacket
)->PayloadList
;) {
85 IkePayload
= IKE_PAYLOAD_BY_PACKET (Entry
);
86 Entry
= Entry
->ForwardLink
;
88 IkePayloadFree (IkePayload
);
96 Callback funtion of NetbufFromExt()
98 @param[in] Arg The data passed from the NetBufFromExe().
103 IkePacketNetbufFree (
108 // TODO: add something if need.
113 Copy the NetBuf into a IKE_PACKET sturcture.
115 Create a IKE_PACKET and fill the received IKE header into the header of IKE_PACKET
116 and copy the recieved packet without IKE HEADER to the PayloadBuf of IKE_PACKET.
118 @param[in] Netbuf The pointer of the Netbuf which contains the whole received
121 @return The pointer of the IKE_PACKET which contains the received packet.
125 IkePacketFromNetbuf (
129 IKE_PACKET
*IkePacket
;
132 if (Netbuf
->TotalSize
< sizeof (IKE_HEADER
)) {
136 IkePacket
= IkePacketAlloc ();
137 if (IkePacket
== NULL
) {
141 // Copy the IKE header from Netbuf to IkePacket->Hdr
143 NetbufCopy (Netbuf
, 0, sizeof (IKE_HEADER
), (UINT8
*) IkePacket
->Header
);
145 // Net order to host order
147 IkeHdrNetToHost (IkePacket
->Header
);
148 if (IkePacket
->Header
->Length
< Netbuf
->TotalSize
) {
152 IkePacket
->PayloadTotalSize
= IkePacket
->Header
->Length
- sizeof (IKE_HEADER
);
153 IkePacket
->PayloadsBuf
= (UINT8
*) AllocateZeroPool (IkePacket
->PayloadTotalSize
);
155 if (IkePacket
->PayloadsBuf
== NULL
) {
159 // Copy the IKE packet without the header into the IkePacket->PayloadsBuf.
161 NetbufCopy (Netbuf
, sizeof (IKE_HEADER
), (UINT32
) IkePacket
->PayloadTotalSize
, IkePacket
->PayloadsBuf
);
165 if (IkePacket
!= NULL
) {
166 IkePacketFree (IkePacket
);
173 Convert the format from IKE_PACKET to NetBuf.
175 @param[in] SessionCommon Pointer of related IKE_COMMON_SESSION
176 @param[in] IkePacket Pointer of IKE_PACKET to be copy to NetBuf
177 @param[in] IkeType The IKE type to pointer the packet is for which IKE
178 phase. Now it supports IKE_SA_TYPE, IKE_CHILDSA_TYPE,
181 @return a pointer of Netbuff which contains the IKE_PACKE in network order.
185 IkeNetbufFromPacket (
186 IN UINT8
*SessionCommon
,
187 IN IKE_PACKET
*IkePacket
,
192 NET_FRAGMENT
*Fragments
;
195 LIST_ENTRY
*PacketEntry
;
197 IKE_PAYLOAD
*IkePayload
;
199 if (!IkePacket
->IsEncoded
) {
200 IkePacket
->IsEncoded
= TRUE
;
202 // Convert Host order to Network order for IKE_PACKET header and payloads
203 // Encryption payloads if needed
205 if (((IKEV2_SESSION_COMMON
*) SessionCommon
)->IkeVer
== 2) {
206 Ikev2EncodePacket ((IKEV2_SESSION_COMMON
*) SessionCommon
, IkePacket
, IkeType
);
209 //If IKEv1 support, check it here.
217 // Get the number of the payloads
219 NET_LIST_FOR_EACH (PacketEntry
, &(IkePacket
)->PayloadList
) {
224 // Allocate the Framgents according to the numbers of the IkePayload
226 Fragments
= (NET_FRAGMENT
*) AllocateZeroPool ((1 + NumPayloads
) * sizeof (NET_FRAGMENT
));
227 if (Fragments
== NULL
) {
231 Fragments
[0].Bulk
= (UINT8
*) IkePacket
->Header
;
232 Fragments
[0].Len
= sizeof (IKE_HEADER
);
236 // Set payloads to the Framgments.
238 NET_LIST_FOR_EACH (Entry
, &(IkePacket
)->PayloadList
) {
239 IkePayload
= IKE_PAYLOAD_BY_PACKET (Entry
);
241 Fragments
[Index
+ 1].Bulk
= IkePayload
->PayloadBuf
;
242 Fragments
[Index
+ 1].Len
= (UINT32
) IkePayload
->PayloadSize
;
246 Netbuf
= NetbufFromExt (
248 (UINT32
) (NumPayloads
+ 1),
255 FreePool (Fragments
);