]>
git.proxmox.com Git - mirror_edk2.git/blob - NetworkPkg/IpSecDxe/IkePacket.c
fa29d548768d2e02911f5039f480be2702449c14
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().
102 IkePacketNetbufFree (
107 // TODO: add something if need.
112 Copy the NetBuf into a IKE_PACKET sturcture.
114 Create a IKE_PACKET and fill the received IKE header into the header of IKE_PACKET
115 and copy the recieved packet without IKE HEADER to the PayloadBuf of IKE_PACKET.
117 @param[in] Netbuf The pointer of the Netbuf which contains the whole received
120 @return The pointer of the IKE_PACKET which contains the received packet.
124 IkePacketFromNetbuf (
128 IKE_PACKET
*IkePacket
;
131 if (Netbuf
->TotalSize
< sizeof (IKE_HEADER
)) {
135 IkePacket
= IkePacketAlloc ();
136 if (IkePacket
== NULL
) {
140 // Copy the IKE header from Netbuf to IkePacket->Hdr
142 NetbufCopy (Netbuf
, 0, sizeof (IKE_HEADER
), (UINT8
*) IkePacket
->Header
);
144 // Net order to host order
146 IkeHdrNetToHost (IkePacket
->Header
);
147 if (IkePacket
->Header
->Length
< Netbuf
->TotalSize
) {
151 IkePacket
->PayloadTotalSize
= IkePacket
->Header
->Length
- sizeof (IKE_HEADER
);
152 IkePacket
->PayloadsBuf
= (UINT8
*) AllocateZeroPool (IkePacket
->PayloadTotalSize
);
154 if (IkePacket
->PayloadsBuf
== NULL
) {
158 // Copy the IKE packet without the header into the IkePacket->PayloadsBuf.
160 NetbufCopy (Netbuf
, sizeof (IKE_HEADER
), (UINT32
) IkePacket
->PayloadTotalSize
, IkePacket
->PayloadsBuf
);
164 if (IkePacket
!= NULL
) {
165 IkePacketFree (IkePacket
);
172 Convert the format from IKE_PACKET to NetBuf.
174 @param[in] SessionCommon Pointer of related IKE_COMMON_SESSION
175 @param[in] IkePacket Pointer of IKE_PACKET to be copy to NetBuf
176 @param[in] IkeType The IKE type to pointer the packet is for which IKE
177 phase. Now it supports IKE_SA_TYPE, IKE_CHILDSA_TYPE,
180 @return a pointer of Netbuff which contains the IKE_PACKE in network order.
184 IkeNetbufFromPacket (
185 IN UINT8
*SessionCommon
,
186 IN IKE_PACKET
*IkePacket
,
191 NET_FRAGMENT
*Fragments
;
194 LIST_ENTRY
*PacketEntry
;
196 IKE_PAYLOAD
*IkePayload
;
198 if (!IkePacket
->IsEncoded
) {
199 IkePacket
->IsEncoded
= TRUE
;
201 // Convert Host order to Network order for IKE_PACKET header and payloads
202 // Encryption payloads if needed
204 if (((IKEV2_SESSION_COMMON
*) SessionCommon
)->IkeVer
== 2) {
205 Ikev2EncodePacket ((IKEV2_SESSION_COMMON
*) SessionCommon
, IkePacket
, IkeType
);
208 //If IKEv1 support, check it here.
216 // Get the number of the payloads
218 NET_LIST_FOR_EACH (PacketEntry
, &(IkePacket
)->PayloadList
) {
223 // Allocate the Framgents according to the numbers of the IkePayload
225 Fragments
= (NET_FRAGMENT
*) AllocateZeroPool ((1 + NumPayloads
) * sizeof (NET_FRAGMENT
));
226 if (Fragments
== NULL
) {
230 Fragments
[0].Bulk
= (UINT8
*) IkePacket
->Header
;
231 Fragments
[0].Len
= sizeof (IKE_HEADER
);
235 // Set payloads to the Framgments.
237 NET_LIST_FOR_EACH (Entry
, &(IkePacket
)->PayloadList
) {
238 IkePayload
= IKE_PAYLOAD_BY_PACKET (Entry
);
240 Fragments
[Index
+ 1].Bulk
= IkePayload
->PayloadBuf
;
241 Fragments
[Index
+ 1].Len
= (UINT32
) IkePayload
->PayloadSize
;
245 Netbuf
= NetbufFromExt (
247 (UINT32
) (NumPayloads
+ 1),
254 FreePool (Fragments
);