]>
git.proxmox.com Git - mirror_edk2.git/blob - NetworkPkg/IpSecDxe/IkePacket.c
2 IKE Packet related operation.
4 Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include "IpSecDebug.h"
11 #include "Ikev2/Utility.h"
14 Allocate a buffer for the IKE_PACKET and intitalize its Header and payloadlist.
16 @return The pointer of the IKE_PACKET.
24 IKE_PACKET
*IkePacket
;
26 IkePacket
= (IKE_PACKET
*) AllocateZeroPool (sizeof (IKE_PACKET
));
27 if (IkePacket
== NULL
) {
31 IkePacket
->RefCount
= 1;
32 InitializeListHead (&IkePacket
->PayloadList
);
34 IkePacket
->Header
= (IKE_HEADER
*) AllocateZeroPool (sizeof (IKE_HEADER
));
35 if (IkePacket
->Header
== NULL
) {
43 Free the IkePacket by the specified IKE_PACKET pointer.
45 @param[in] IkePacket The pointer of the IKE_PACKET to be freed.
50 IN IKE_PACKET
*IkePacket
54 IKE_PAYLOAD
*IkePayload
;
56 if (IkePacket
== NULL
) {
60 // Check if the Packet is referred by others.
62 if (--IkePacket
->RefCount
== 0) {
64 // Free IkePacket header
66 if (!IkePacket
->IsHdrExt
&& IkePacket
->Header
!= NULL
) {
67 FreePool (IkePacket
->Header
);
70 // Free the PayloadsBuff
72 if (!IkePacket
->IsPayloadsBufExt
&& IkePacket
->PayloadsBuf
!= NULL
) {
73 FreePool (IkePacket
->PayloadsBuf
);
76 // Iterate payloadlist and free all payloads
78 for (Entry
= (IkePacket
)->PayloadList
.ForwardLink
; Entry
!= &(IkePacket
)->PayloadList
;) {
79 IkePayload
= IKE_PAYLOAD_BY_PACKET (Entry
);
80 Entry
= Entry
->ForwardLink
;
82 IkePayloadFree (IkePayload
);
90 Callback funtion of NetbufFromExt()
92 @param[in] Arg The data passed from the NetBufFromExe().
102 // TODO: add something if need.
107 Copy the NetBuf into a IKE_PACKET sturcture.
109 Create a IKE_PACKET and fill the received IKE header into the header of IKE_PACKET
110 and copy the recieved packet without IKE HEADER to the PayloadBuf of IKE_PACKET.
112 @param[in] Netbuf The pointer of the Netbuf which contains the whole received
115 @return The pointer of the IKE_PACKET which contains the received packet.
119 IkePacketFromNetbuf (
123 IKE_PACKET
*IkePacket
;
126 if (Netbuf
->TotalSize
< sizeof (IKE_HEADER
)) {
130 IkePacket
= IkePacketAlloc ();
131 if (IkePacket
== NULL
) {
135 // Copy the IKE header from Netbuf to IkePacket->Hdr
137 NetbufCopy (Netbuf
, 0, sizeof (IKE_HEADER
), (UINT8
*) IkePacket
->Header
);
139 // Net order to host order
141 IkeHdrNetToHost (IkePacket
->Header
);
142 if (IkePacket
->Header
->Length
< Netbuf
->TotalSize
) {
146 IkePacket
->PayloadTotalSize
= IkePacket
->Header
->Length
- sizeof (IKE_HEADER
);
147 IkePacket
->PayloadsBuf
= (UINT8
*) AllocateZeroPool (IkePacket
->PayloadTotalSize
);
149 if (IkePacket
->PayloadsBuf
== NULL
) {
153 // Copy the IKE packet without the header into the IkePacket->PayloadsBuf.
155 NetbufCopy (Netbuf
, sizeof (IKE_HEADER
), (UINT32
) IkePacket
->PayloadTotalSize
, IkePacket
->PayloadsBuf
);
159 if (IkePacket
!= NULL
) {
160 IkePacketFree (IkePacket
);
167 Convert the format from IKE_PACKET to NetBuf.
169 @param[in] SessionCommon Pointer of related IKE_COMMON_SESSION
170 @param[in] IkePacket Pointer of IKE_PACKET to be copy to NetBuf
171 @param[in] IkeType The IKE type to pointer the packet is for which IKE
172 phase. Now it supports IKE_SA_TYPE, IKE_CHILDSA_TYPE,
175 @return a pointer of Netbuff which contains the IKE_PACKE in network order.
179 IkeNetbufFromPacket (
180 IN UINT8
*SessionCommon
,
181 IN IKE_PACKET
*IkePacket
,
186 NET_FRAGMENT
*Fragments
;
189 LIST_ENTRY
*PacketEntry
;
191 IKE_PAYLOAD
*IkePayload
;
192 EFI_STATUS RetStatus
;
194 RetStatus
= EFI_SUCCESS
;
196 if (!IkePacket
->IsEncoded
) {
197 IkePacket
->IsEncoded
= TRUE
;
199 // Convert Host order to Network order for IKE_PACKET header and payloads
200 // Encryption payloads if needed
202 if (((IKEV2_SESSION_COMMON
*) SessionCommon
)->IkeVer
== 2) {
203 RetStatus
= Ikev2EncodePacket ((IKEV2_SESSION_COMMON
*) SessionCommon
, IkePacket
, IkeType
);
204 if (EFI_ERROR (RetStatus
)) {
210 // If IKEv1 support, check it here.
218 // Get the number of the payloads
220 NET_LIST_FOR_EACH (PacketEntry
, &(IkePacket
)->PayloadList
) {
225 // Allocate the Framgents according to the numbers of the IkePayload
227 Fragments
= (NET_FRAGMENT
*) AllocateZeroPool ((1 + NumPayloads
) * sizeof (NET_FRAGMENT
));
228 if (Fragments
== NULL
) {
232 Fragments
[0].Bulk
= (UINT8
*) IkePacket
->Header
;
233 Fragments
[0].Len
= sizeof (IKE_HEADER
);
237 // Set payloads to the Framgments.
239 NET_LIST_FOR_EACH (Entry
, &(IkePacket
)->PayloadList
) {
240 IkePayload
= IKE_PAYLOAD_BY_PACKET (Entry
);
242 Fragments
[Index
+ 1].Bulk
= IkePayload
->PayloadBuf
;
243 Fragments
[Index
+ 1].Len
= (UINT32
) IkePayload
->PayloadSize
;
247 Netbuf
= NetbufFromExt (
249 (UINT32
) (NumPayloads
+ 1),
256 FreePool (Fragments
);