]> git.proxmox.com Git - mirror_edk2.git/blob - NetworkPkg/IpSecDxe/IkePacket.c
MdeModulePkg/StatusCodeHandlerRuntimeDxe: make global variable static
[mirror_edk2.git] / NetworkPkg / IpSecDxe / IkePacket.c
1 /** @file
2 IKE Packet related operation.
3
4 Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>
5
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7
8 **/
9
10 #include "IpSecDebug.h"
11 #include "Ikev2/Utility.h"
12
13 /**
14 Allocate a buffer for the IKE_PACKET and intitalize its Header and payloadlist.
15
16 @return The pointer of the IKE_PACKET.
17
18 **/
19 IKE_PACKET *
20 IkePacketAlloc (
21 VOID
22 )
23 {
24 IKE_PACKET *IkePacket;
25
26 IkePacket = (IKE_PACKET *) AllocateZeroPool (sizeof (IKE_PACKET));
27 if (IkePacket == NULL) {
28 return NULL;
29 }
30
31 IkePacket->RefCount = 1;
32 InitializeListHead (&IkePacket->PayloadList);
33
34 IkePacket->Header = (IKE_HEADER *) AllocateZeroPool (sizeof (IKE_HEADER));
35 if (IkePacket->Header == NULL) {
36 FreePool (IkePacket);
37 return NULL;
38 }
39 return IkePacket;
40 }
41
42 /**
43 Free the IkePacket by the specified IKE_PACKET pointer.
44
45 @param[in] IkePacket The pointer of the IKE_PACKET to be freed.
46
47 **/
48 VOID
49 IkePacketFree (
50 IN IKE_PACKET *IkePacket
51 )
52 {
53 LIST_ENTRY *Entry;
54 IKE_PAYLOAD *IkePayload;
55
56 if (IkePacket == NULL) {
57 return;
58 }
59 //
60 // Check if the Packet is referred by others.
61 //
62 if (--IkePacket->RefCount == 0) {
63 //
64 // Free IkePacket header
65 //
66 if (!IkePacket->IsHdrExt && IkePacket->Header != NULL) {
67 FreePool (IkePacket->Header);
68 }
69 //
70 // Free the PayloadsBuff
71 //
72 if (!IkePacket->IsPayloadsBufExt && IkePacket->PayloadsBuf != NULL) {
73 FreePool (IkePacket->PayloadsBuf);
74 }
75 //
76 // Iterate payloadlist and free all payloads
77 //
78 for (Entry = (IkePacket)->PayloadList.ForwardLink; Entry != &(IkePacket)->PayloadList;) {
79 IkePayload = IKE_PAYLOAD_BY_PACKET (Entry);
80 Entry = Entry->ForwardLink;
81
82 IkePayloadFree (IkePayload);
83 }
84
85 FreePool (IkePacket);
86 }
87 }
88
89 /**
90 Callback funtion of NetbufFromExt()
91
92 @param[in] Arg The data passed from the NetBufFromExe().
93
94 **/
95 VOID
96 EFIAPI
97 IkePacketNetbufFree (
98 IN VOID *Arg
99 )
100 {
101 //
102 // TODO: add something if need.
103 //
104 }
105
106 /**
107 Copy the NetBuf into a IKE_PACKET sturcture.
108
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.
111
112 @param[in] Netbuf The pointer of the Netbuf which contains the whole received
113 IKE packet.
114
115 @return The pointer of the IKE_PACKET which contains the received packet.
116
117 **/
118 IKE_PACKET *
119 IkePacketFromNetbuf (
120 IN NET_BUF *Netbuf
121 )
122 {
123 IKE_PACKET *IkePacket;
124
125 IkePacket = NULL;
126 if (Netbuf->TotalSize < sizeof (IKE_HEADER)) {
127 goto Error;
128 }
129
130 IkePacket = IkePacketAlloc ();
131 if (IkePacket == NULL) {
132 return NULL;
133 }
134 //
135 // Copy the IKE header from Netbuf to IkePacket->Hdr
136 //
137 NetbufCopy (Netbuf, 0, sizeof (IKE_HEADER), (UINT8 *) IkePacket->Header);
138 //
139 // Net order to host order
140 //
141 IkeHdrNetToHost (IkePacket->Header);
142 if (IkePacket->Header->Length < Netbuf->TotalSize) {
143 goto Error;
144 }
145
146 IkePacket->PayloadTotalSize = IkePacket->Header->Length - sizeof (IKE_HEADER);
147 IkePacket->PayloadsBuf = (UINT8 *) AllocateZeroPool (IkePacket->PayloadTotalSize);
148
149 if (IkePacket->PayloadsBuf == NULL) {
150 goto Error;
151 }
152 //
153 // Copy the IKE packet without the header into the IkePacket->PayloadsBuf.
154 //
155 NetbufCopy (Netbuf, sizeof (IKE_HEADER), (UINT32) IkePacket->PayloadTotalSize, IkePacket->PayloadsBuf);
156 return IkePacket;
157
158 Error:
159 if (IkePacket != NULL) {
160 IkePacketFree (IkePacket);
161 }
162
163 return NULL;
164 }
165
166 /**
167 Convert the format from IKE_PACKET to NetBuf.
168
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,
173 IKE_INFO_TYPE.
174
175 @return a pointer of Netbuff which contains the IKE_PACKE in network order.
176
177 **/
178 NET_BUF *
179 IkeNetbufFromPacket (
180 IN UINT8 *SessionCommon,
181 IN IKE_PACKET *IkePacket,
182 IN UINTN IkeType
183 )
184 {
185 NET_BUF *Netbuf;
186 NET_FRAGMENT *Fragments;
187 UINTN Index;
188 UINTN NumPayloads;
189 LIST_ENTRY *PacketEntry;
190 LIST_ENTRY *Entry;
191 IKE_PAYLOAD *IkePayload;
192 EFI_STATUS RetStatus;
193
194 RetStatus = EFI_SUCCESS;
195
196 if (!IkePacket->IsEncoded) {
197 IkePacket->IsEncoded = TRUE;
198 //
199 // Convert Host order to Network order for IKE_PACKET header and payloads
200 // Encryption payloads if needed
201 //
202 if (((IKEV2_SESSION_COMMON *) SessionCommon)->IkeVer == 2) {
203 RetStatus = Ikev2EncodePacket ((IKEV2_SESSION_COMMON *) SessionCommon, IkePacket, IkeType);
204 if (EFI_ERROR (RetStatus)) {
205 return NULL;
206 }
207
208 } else {
209 //
210 // If IKEv1 support, check it here.
211 //
212 return NULL;
213 }
214 }
215
216 NumPayloads = 0;
217 //
218 // Get the number of the payloads
219 //
220 NET_LIST_FOR_EACH (PacketEntry, &(IkePacket)->PayloadList) {
221
222 NumPayloads++;
223 }
224 //
225 // Allocate the Framgents according to the numbers of the IkePayload
226 //
227 Fragments = (NET_FRAGMENT *) AllocateZeroPool ((1 + NumPayloads) * sizeof (NET_FRAGMENT));
228 if (Fragments == NULL) {
229 return NULL;
230 }
231
232 Fragments[0].Bulk = (UINT8 *) IkePacket->Header;
233 Fragments[0].Len = sizeof (IKE_HEADER);
234 Index = 0;
235
236 //
237 // Set payloads to the Framgments.
238 //
239 NET_LIST_FOR_EACH (Entry, &(IkePacket)->PayloadList) {
240 IkePayload = IKE_PAYLOAD_BY_PACKET (Entry);
241
242 Fragments[Index + 1].Bulk = IkePayload->PayloadBuf;
243 Fragments[Index + 1].Len = (UINT32) IkePayload->PayloadSize;
244 Index++;
245 }
246
247 Netbuf = NetbufFromExt (
248 Fragments,
249 (UINT32) (NumPayloads + 1),
250 0,
251 0,
252 IkePacketNetbufFree,
253 NULL
254 );
255
256 FreePool (Fragments);
257 return Netbuf;
258 }
259