]> git.proxmox.com Git - mirror_edk2.git/blame - NetworkPkg/IpSecDxe/IpSecConfigImpl.c
Before decrypting the packet, Ipsec will check if the inbound protected packet is...
[mirror_edk2.git] / NetworkPkg / IpSecDxe / IpSecConfigImpl.c
CommitLineData
a3bcde70
HT
1/** @file\r
2 The implementation of IPSEC_CONFIG_PROTOCOL.\r
3\r
4 Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>\r
5\r
6 This program and the accompanying materials\r
7 are licensed and made available under the terms and conditions of the BSD License\r
8 which accompanies this distribution. The full text of the license may be found at\r
9 http://opensource.org/licenses/bsd-license.php.\r
10\r
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14**/\r
15\r
16#include "IpSecConfigImpl.h"\r
17#include "IpSecDebug.h"\r
18\r
19LIST_ENTRY mConfigData[IPsecConfigDataTypeMaximum];\r
20BOOLEAN mSetBySelf = FALSE;\r
21\r
22//\r
9166f840 23// Common CompareSelector routine entry for SPD/SAD/PAD.\r
a3bcde70
HT
24//\r
25IPSEC_COMPARE_SELECTOR mCompareSelector[] = {\r
26 (IPSEC_COMPARE_SELECTOR) CompareSpdSelector,\r
27 (IPSEC_COMPARE_SELECTOR) CompareSaId,\r
28 (IPSEC_COMPARE_SELECTOR) ComparePadId\r
29};\r
30\r
31//\r
9166f840 32// Common IsZeroSelector routine entry for SPD/SAD/PAD.\r
a3bcde70
HT
33//\r
34IPSEC_IS_ZERO_SELECTOR mIsZeroSelector[] = {\r
35 (IPSEC_IS_ZERO_SELECTOR) IsZeroSpdSelector,\r
36 (IPSEC_IS_ZERO_SELECTOR) IsZeroSaId,\r
37 (IPSEC_IS_ZERO_SELECTOR) IsZeroPadId\r
38};\r
39\r
40//\r
9166f840 41// Common DuplicateSelector routine entry for SPD/SAD/PAD.\r
a3bcde70
HT
42//\r
43IPSEC_DUPLICATE_SELECTOR mDuplicateSelector[] = {\r
44 (IPSEC_DUPLICATE_SELECTOR) DuplicateSpdSelector,\r
45 (IPSEC_DUPLICATE_SELECTOR) DuplicateSaId,\r
46 (IPSEC_DUPLICATE_SELECTOR) DuplicatePadId\r
47};\r
48\r
49//\r
9166f840 50// Common FixPolicyEntry routine entry for SPD/SAD/PAD.\r
a3bcde70
HT
51//\r
52IPSEC_FIX_POLICY_ENTRY mFixPolicyEntry[] = {\r
53 (IPSEC_FIX_POLICY_ENTRY) FixSpdEntry,\r
54 (IPSEC_FIX_POLICY_ENTRY) FixSadEntry,\r
55 (IPSEC_FIX_POLICY_ENTRY) FixPadEntry\r
56};\r
57\r
58//\r
9166f840 59// Common UnfixPolicyEntry routine entry for SPD/SAD/PAD.\r
a3bcde70
HT
60//\r
61IPSEC_FIX_POLICY_ENTRY mUnfixPolicyEntry[] = {\r
62 (IPSEC_FIX_POLICY_ENTRY) UnfixSpdEntry,\r
63 (IPSEC_FIX_POLICY_ENTRY) UnfixSadEntry,\r
64 (IPSEC_FIX_POLICY_ENTRY) UnfixPadEntry\r
65};\r
66\r
67//\r
9166f840 68// Common SetPolicyEntry routine entry for SPD/SAD/PAD.\r
a3bcde70
HT
69//\r
70IPSEC_SET_POLICY_ENTRY mSetPolicyEntry[] = {\r
71 (IPSEC_SET_POLICY_ENTRY) SetSpdEntry,\r
72 (IPSEC_SET_POLICY_ENTRY) SetSadEntry,\r
73 (IPSEC_SET_POLICY_ENTRY) SetPadEntry\r
74};\r
75\r
76//\r
9166f840 77// Common GetPolicyEntry routine entry for SPD/SAD/PAD.\r
a3bcde70
HT
78//\r
79IPSEC_GET_POLICY_ENTRY mGetPolicyEntry[] = {\r
80 (IPSEC_GET_POLICY_ENTRY) GetSpdEntry,\r
81 (IPSEC_GET_POLICY_ENTRY) GetSadEntry,\r
82 (IPSEC_GET_POLICY_ENTRY) GetPadEntry\r
83};\r
84\r
85//\r
86// Routine entry for IpSecConfig protocol.\r
87//\r
88EFI_IPSEC_CONFIG_PROTOCOL mIpSecConfigInstance = {\r
89 EfiIpSecConfigSetData,\r
90 EfiIpSecConfigGetData,\r
91 EfiIpSecConfigGetNextSelector,\r
92 EfiIpSecConfigRegisterNotify,\r
93 EfiIpSecConfigUnregisterNotify\r
94};\r
95\r
96/**\r
97 Get the all IPSec configuration variables and store those variables\r
98 to the internal data structure.\r
99\r
9166f840 100 This founction is called by IpSecConfigInitialize() that is to intialize the \r
a3bcde70
HT
101 IPsecConfiguration Protocol.\r
102\r
103 @param[in] Private Point to IPSEC_PRIVATE_DATA.\r
104\r
105 @retval EFI_OUT_OF_RESOURCES The required system resource could not be allocated.\r
106 @retval EFI_SUCCESS Restore the IPsec Configuration successfully.\r
107 @retval others Other errors is found during the variable getting.\r
108\r
109**/\r
110EFI_STATUS\r
111IpSecConfigRestore (\r
112 IN IPSEC_PRIVATE_DATA *Private\r
113 );\r
114\r
115/**\r
116 Check if the specified EFI_IP_ADDRESS_INFO is in EFI_IP_ADDRESS_INFO list.\r
117\r
118 @param[in] AddressInfo Pointer of IP_ADDRESS_INFO to be search in AddressInfo list.\r
119 @param[in] AddressInfoList A list that contains IP_ADDRESS_INFOs.\r
120 @param[in] AddressCount Point out how many IP_ADDRESS_INFO in the list.\r
121\r
122 @retval TRUE The specified AddressInfo is in the AddressInfoList.\r
123 @retval FALSE The specified AddressInfo is not in the AddressInfoList.\r
9166f840 124 \r
a3bcde70
HT
125**/\r
126BOOLEAN\r
127IsInAddressInfoList(\r
128 IN EFI_IP_ADDRESS_INFO *AddressInfo,\r
129 IN EFI_IP_ADDRESS_INFO *AddressInfoList,\r
130 IN UINT32 AddressCount\r
131 )\r
132{\r
9166f840 133 UINT8 Index;\r
134 EFI_IP_ADDRESS ZeroAddress;\r
135\r
136 ZeroMem(&ZeroAddress, sizeof (EFI_IP_ADDRESS));\r
a3bcde70 137\r
9166f840 138 //\r
139 // Zero Address means any address is matched.\r
140 //\r
141 if (AddressCount == 1) {\r
142 if (CompareMem (\r
143 &AddressInfoList[0].Address,\r
144 &ZeroAddress,\r
145 sizeof (EFI_IP_ADDRESS)\r
146 ) == 0) {\r
147 return TRUE;\r
148 }\r
149 }\r
a3bcde70
HT
150 for (Index = 0; Index < AddressCount ; Index++) {\r
151 if (CompareMem (\r
152 AddressInfo,\r
153 &AddressInfoList[Index].Address,\r
154 sizeof (EFI_IP_ADDRESS)\r
9166f840 155 ) == 0 && \r
a3bcde70 156 AddressInfo->PrefixLength == AddressInfoList[Index].PrefixLength\r
9166f840 157 ) { \r
a3bcde70
HT
158 return TRUE;\r
159 }\r
160 }\r
161 return FALSE;\r
162}\r
9166f840 163 \r
a3bcde70
HT
164/**\r
165 Compare two SPD Selectors.\r
166\r
167 Compare two SPD Selector by the fields of LocalAddressCount/RemoteAddressCount/\r
9166f840 168 NextLayerProtocol/LocalPort/LocalPortRange/RemotePort/RemotePortRange and the \r
a3bcde70
HT
169 Local Addresses and remote Addresses.\r
170\r
171 @param[in] Selector1 Pointer of first SPD Selector.\r
172 @param[in] Selector2 Pointer of second SPD Selector.\r
173\r
174 @retval TRUE This two Selector have the same value in above fields.\r
175 @retval FALSE Not all above fields have the same value in these two Selectors.\r
9166f840 176 \r
a3bcde70
HT
177**/\r
178BOOLEAN\r
179CompareSpdSelector (\r
180 IN EFI_IPSEC_CONFIG_SELECTOR *Selector1,\r
181 IN EFI_IPSEC_CONFIG_SELECTOR *Selector2\r
182 )\r
183{\r
184 EFI_IPSEC_SPD_SELECTOR *SpdSel1;\r
185 EFI_IPSEC_SPD_SELECTOR *SpdSel2;\r
186 BOOLEAN IsMatch;\r
187 UINTN Index;\r
188\r
189 SpdSel1 = &Selector1->SpdSelector;\r
190 SpdSel2 = &Selector2->SpdSelector;\r
191 IsMatch = TRUE;\r
192\r
193 //\r
194 // Compare the LocalAddressCount/RemoteAddressCount/NextLayerProtocol/\r
195 // LocalPort/LocalPortRange/RemotePort/RemotePortRange fields in the\r
9166f840 196 // two Spdselectors. Since the SPD supports two directions, it needs to \r
a3bcde70
HT
197 // compare two directions.\r
198 //\r
199 if ((SpdSel1->LocalAddressCount != SpdSel2->LocalAddressCount &&\r
200 SpdSel1->LocalAddressCount != SpdSel2->RemoteAddressCount) ||\r
201 (SpdSel1->RemoteAddressCount != SpdSel2->RemoteAddressCount &&\r
202 SpdSel1->RemoteAddressCount != SpdSel2->LocalAddressCount) ||\r
203 SpdSel1->NextLayerProtocol != SpdSel2->NextLayerProtocol ||\r
204 SpdSel1->LocalPort != SpdSel2->LocalPort ||\r
205 SpdSel1->LocalPortRange != SpdSel2->LocalPortRange ||\r
206 SpdSel1->RemotePort != SpdSel2->RemotePort ||\r
207 SpdSel1->RemotePortRange != SpdSel2->RemotePortRange\r
208 ) {\r
209 IsMatch = FALSE;\r
210 return IsMatch;\r
211 }\r
9166f840 212 \r
a3bcde70
HT
213 //\r
214 // Compare the all LocalAddress fields in the two Spdselectors.\r
9166f840 215 // First, SpdSel1->LocalAddress to SpdSel2->LocalAddress && Compare \r
a3bcde70
HT
216 // SpdSel1->RemoteAddress to SpdSel2->RemoteAddress. If all match, return\r
217 // TRUE.\r
218 //\r
219 for (Index = 0; Index < SpdSel1->LocalAddressCount; Index++) {\r
220 if (!IsInAddressInfoList (\r
221 &SpdSel1->LocalAddress[Index],\r
222 SpdSel2->LocalAddress,\r
223 SpdSel2->LocalAddressCount\r
224 )) {\r
225 IsMatch = FALSE;\r
226 break;\r
227 }\r
228 }\r
229 if (IsMatch) {\r
230 for (Index = 0; Index < SpdSel2->LocalAddressCount; Index++) {\r
231 if (!IsInAddressInfoList (\r
232 &SpdSel2->LocalAddress[Index],\r
233 SpdSel1->LocalAddress,\r
234 SpdSel1->LocalAddressCount\r
235 )) {\r
236 IsMatch = FALSE;\r
237 break;\r
238 }\r
239 }\r
240 }\r
241 if (IsMatch) {\r
242 for (Index = 0; Index < SpdSel1->RemoteAddressCount; Index++) {\r
243 if (!IsInAddressInfoList (\r
244 &SpdSel1->RemoteAddress[Index],\r
245 SpdSel2->RemoteAddress,\r
246 SpdSel2->RemoteAddressCount\r
247 )) {\r
248 IsMatch = FALSE;\r
249 break;\r
250 }\r
251 }\r
252 }\r
253 if (IsMatch) {\r
254 for (Index = 0; Index < SpdSel2->RemoteAddressCount; Index++) {\r
255 if (!IsInAddressInfoList (\r
256 &SpdSel2->RemoteAddress[Index],\r
257 SpdSel1->RemoteAddress,\r
258 SpdSel1->RemoteAddressCount\r
259 )) {\r
260 IsMatch = FALSE;\r
261 break;\r
262 }\r
263 }\r
264 }\r
265 //\r
9166f840 266 // Finish the one direction compare. If it is matched, return; otherwise, \r
a3bcde70
HT
267 // compare the other direction.\r
268 //\r
269 if (IsMatch) {\r
270 return IsMatch;\r
271 }\r
272 //\r
9166f840 273 // Secondly, the SpdSel1->LocalAddress doesn't equal to SpdSel2->LocalAddress and \r
a3bcde70
HT
274 // SpdSel1->RemoteAddress doesn't equal to SpdSel2->RemoteAddress. Try to compare\r
275 // the RemoteAddress to LocalAddress.\r
276 //\r
277 IsMatch = TRUE;\r
278 for (Index = 0; Index < SpdSel1->RemoteAddressCount; Index++) {\r
279 if (!IsInAddressInfoList (\r
280 &SpdSel1->RemoteAddress[Index],\r
281 SpdSel2->LocalAddress,\r
282 SpdSel2->LocalAddressCount\r
283 )) {\r
284 IsMatch = FALSE;\r
285 break;\r
286 }\r
287 }\r
288 if (IsMatch) {\r
289 for (Index = 0; Index < SpdSel2->RemoteAddressCount; Index++) {\r
290 if (!IsInAddressInfoList (\r
291 &SpdSel2->RemoteAddress[Index],\r
292 SpdSel1->LocalAddress,\r
293 SpdSel1->LocalAddressCount\r
294 )) {\r
295 IsMatch = FALSE;\r
296 break;\r
297 }\r
298 }\r
299 }\r
300 if (IsMatch) {\r
301 for (Index = 0; Index < SpdSel1->LocalAddressCount; Index++) {\r
302 if (!IsInAddressInfoList (\r
303 &SpdSel1->LocalAddress[Index],\r
304 SpdSel2->RemoteAddress,\r
305 SpdSel2->RemoteAddressCount\r
306 )) {\r
307 IsMatch = FALSE;\r
308 break;\r
309 }\r
310 }\r
311 }\r
312 if (IsMatch) {\r
313 for (Index = 0; Index < SpdSel2->LocalAddressCount; Index++) {\r
314 if (!IsInAddressInfoList (\r
315 &SpdSel2->LocalAddress[Index],\r
316 SpdSel1->RemoteAddress,\r
317 SpdSel1->RemoteAddressCount\r
318 )) {\r
319 IsMatch = FALSE;\r
320 break;\r
321 }\r
322 }\r
323 }\r
324 return IsMatch;\r
325}\r
326\r
9166f840 327/**\r
328 Find if the two SPD Selectors has subordinative.\r
329\r
330 Compare two SPD Selector by the fields of LocalAddressCount/RemoteAddressCount/\r
331 NextLayerProtocol/LocalPort/LocalPortRange/RemotePort/RemotePortRange and the \r
332 Local Addresses and remote Addresses.\r
333\r
334 @param[in] Selector1 Pointer of first SPD Selector.\r
335 @param[in] Selector2 Pointer of second SPD Selector.\r
336\r
337 @retval TRUE The first SPD Selector is subordinate Selector of second SPD Selector.\r
338 @retval FALSE The first SPD Selector is not subordinate Selector of second \r
339 SPD Selector.\r
340 \r
341**/\r
342BOOLEAN\r
343IsSubSpdSelector (\r
344 IN EFI_IPSEC_CONFIG_SELECTOR *Selector1,\r
345 IN EFI_IPSEC_CONFIG_SELECTOR *Selector2\r
346 )\r
347{\r
348 EFI_IPSEC_SPD_SELECTOR *SpdSel1;\r
349 EFI_IPSEC_SPD_SELECTOR *SpdSel2;\r
350 BOOLEAN IsMatch;\r
351 UINTN Index;\r
352\r
353 SpdSel1 = &Selector1->SpdSelector;\r
354 SpdSel2 = &Selector2->SpdSelector;\r
355 IsMatch = TRUE;\r
356\r
357 //\r
358 // Compare the LocalAddressCount/RemoteAddressCount/NextLayerProtocol/\r
359 // LocalPort/LocalPortRange/RemotePort/RemotePortRange fields in the\r
360 // two Spdselectors. Since the SPD supports two directions, it needs to \r
361 // compare two directions.\r
362 //\r
363 if (SpdSel1->LocalAddressCount > SpdSel2->LocalAddressCount ||\r
364 SpdSel1->RemoteAddressCount > SpdSel2->RemoteAddressCount ||\r
365 (SpdSel1->NextLayerProtocol != SpdSel2->NextLayerProtocol && SpdSel2->NextLayerProtocol != 0xffff) ||\r
366 (SpdSel1->LocalPort > SpdSel2->LocalPort && SpdSel2->LocalPort != 0)||\r
367 (SpdSel1->LocalPortRange > SpdSel2->LocalPortRange && SpdSel1->LocalPort != 0)||\r
368 (SpdSel1->RemotePort > SpdSel2->RemotePort && SpdSel2->RemotePort != 0) ||\r
369 (SpdSel1->RemotePortRange > SpdSel2->RemotePortRange && SpdSel2->RemotePort != 0)\r
370 ) {\r
371 IsMatch = FALSE;\r
372 }\r
373 \r
374 //\r
375 // Compare the all LocalAddress fields in the two Spdselectors.\r
376 // First, SpdSel1->LocalAddress to SpdSel2->LocalAddress && Compare \r
377 // SpdSel1->RemoteAddress to SpdSel2->RemoteAddress. If all match, return\r
378 // TRUE.\r
379 //\r
380 if (IsMatch) {\r
381 for (Index = 0; Index < SpdSel1->LocalAddressCount; Index++) {\r
382 if (!IsInAddressInfoList (\r
383 &SpdSel1->LocalAddress[Index],\r
384 SpdSel2->LocalAddress,\r
385 SpdSel2->LocalAddressCount\r
386 )) {\r
387 IsMatch = FALSE;\r
388 break;\r
389 }\r
390 }\r
391\r
392 if (IsMatch) {\r
393 for (Index = 0; Index < SpdSel1->RemoteAddressCount; Index++) {\r
394 if (!IsInAddressInfoList (\r
395 &SpdSel1->RemoteAddress[Index],\r
396 SpdSel2->RemoteAddress,\r
397 SpdSel2->RemoteAddressCount\r
398 )) {\r
399 IsMatch = FALSE;\r
400 break;\r
401 }\r
402 }\r
403 }\r
404 }\r
405 if (IsMatch) {\r
406 return IsMatch;\r
407 }\r
408 \r
409 //\r
410 //\r
411 // The SPD selector in SPD entry is two way.\r
412 //\r
413 // Compare the LocalAddressCount/RemoteAddressCount/NextLayerProtocol/\r
414 // LocalPort/LocalPortRange/RemotePort/RemotePortRange fields in the\r
415 // two Spdselectors. Since the SPD supports two directions, it needs to \r
416 // compare two directions.\r
417 //\r
418 IsMatch = TRUE;\r
419 if (SpdSel1->LocalAddressCount > SpdSel2->RemoteAddressCount ||\r
420 SpdSel1->RemoteAddressCount > SpdSel2->LocalAddressCount ||\r
421 (SpdSel1->NextLayerProtocol != SpdSel2->NextLayerProtocol && SpdSel2->NextLayerProtocol != 0xffff) ||\r
422 (SpdSel1->LocalPort > SpdSel2->RemotePort && SpdSel2->RemotePort != 0)||\r
423 (SpdSel1->LocalPortRange > SpdSel2->RemotePortRange && SpdSel1->RemotePort != 0)||\r
424 (SpdSel1->RemotePort > SpdSel2->LocalPort && SpdSel2->LocalPort != 0) ||\r
425 (SpdSel1->RemotePortRange > SpdSel2->LocalPortRange && SpdSel2->LocalPort != 0)\r
426 ) {\r
427 IsMatch = FALSE;\r
428 return IsMatch;\r
429 }\r
430 \r
431 //\r
432 // Compare the all LocalAddress fields in the two Spdselectors.\r
433 // First, SpdSel1->LocalAddress to SpdSel2->LocalAddress && Compare \r
434 // SpdSel1->RemoteAddress to SpdSel2->RemoteAddress. If all match, return\r
435 // TRUE.\r
436 //\r
437 for (Index = 0; Index < SpdSel1->LocalAddressCount; Index++) {\r
438 if (!IsInAddressInfoList (\r
439 &SpdSel1->LocalAddress[Index],\r
440 SpdSel2->RemoteAddress,\r
441 SpdSel2->RemoteAddressCount\r
442 )) {\r
443 IsMatch = FALSE;\r
444 break;\r
445 }\r
446 }\r
447\r
448 if (IsMatch) {\r
449 for (Index = 0; Index < SpdSel1->RemoteAddressCount; Index++) {\r
450 if (!IsInAddressInfoList (\r
451 &SpdSel1->RemoteAddress[Index],\r
452 SpdSel2->LocalAddress,\r
453 SpdSel2->LocalAddressCount\r
454 )) {\r
455 IsMatch = FALSE;\r
456 break;\r
457 }\r
458 }\r
459 }\r
460 return IsMatch;\r
461 \r
462}\r
463\r
a3bcde70
HT
464/**\r
465 Compare two SA IDs.\r
466\r
467 @param[in] Selector1 Pointer of first SA ID.\r
468 @param[in] Selector2 Pointer of second SA ID.\r
469\r
470 @retval TRUE This two Selectors have the same SA ID.\r
471 @retval FALSE This two Selecotrs don't have the same SA ID.\r
9166f840 472 \r
a3bcde70
HT
473**/\r
474BOOLEAN\r
475CompareSaId (\r
476 IN EFI_IPSEC_CONFIG_SELECTOR *Selector1,\r
477 IN EFI_IPSEC_CONFIG_SELECTOR *Selector2\r
478 )\r
479{\r
480 EFI_IPSEC_SA_ID *SaId1;\r
481 EFI_IPSEC_SA_ID *SaId2;\r
482 BOOLEAN IsMatch;\r
483\r
484 SaId1 = &Selector1->SaId;\r
485 SaId2 = &Selector2->SaId;\r
486 IsMatch = TRUE;\r
487\r
488 if (CompareMem (SaId1, SaId2, sizeof (EFI_IPSEC_SA_ID)) != 0) {\r
489 IsMatch = FALSE;\r
490 }\r
491\r
492 return IsMatch;\r
493}\r
494\r
495/**\r
496 Compare two PAD IDs.\r
497\r
498 @param[in] Selector1 Pointer of first PAD ID.\r
499 @param[in] Selector2 Pointer of second PAD ID.\r
500\r
501 @retval TRUE This two Selectors have the same PAD ID.\r
502 @retval FALSE This two Selecotrs don't have the same PAD ID.\r
9166f840 503 \r
a3bcde70
HT
504**/\r
505BOOLEAN\r
506ComparePadId (\r
507 IN EFI_IPSEC_CONFIG_SELECTOR *Selector1,\r
508 IN EFI_IPSEC_CONFIG_SELECTOR *Selector2\r
509 )\r
510{\r
511 EFI_IPSEC_PAD_ID *PadId1;\r
512 EFI_IPSEC_PAD_ID *PadId2;\r
513 BOOLEAN IsMatch;\r
514\r
515 PadId1 = &Selector1->PadId;\r
516 PadId2 = &Selector2->PadId;\r
517 IsMatch = TRUE;\r
518\r
519 //\r
520 // Compare the PeerIdValid fields in PadId.\r
521 //\r
522 if (PadId1->PeerIdValid != PadId2->PeerIdValid) {\r
523 IsMatch = FALSE;\r
524 }\r
525 //\r
526 // Compare the PeerId fields in PadId if PeerIdValid is true.\r
527 //\r
528 if (IsMatch &&\r
529 PadId1->PeerIdValid &&\r
530 AsciiStriCmp ((CONST CHAR8 *) PadId1->Id.PeerId, (CONST CHAR8 *) PadId2->Id.PeerId) != 0\r
531 ) {\r
532 IsMatch = FALSE;\r
533 }\r
534 //\r
535 // Compare the IpAddress fields in PadId if PeerIdValid is false.\r
536 //\r
537 if (IsMatch &&\r
538 !PadId1->PeerIdValid &&\r
539 (PadId1->Id.IpAddress.PrefixLength != PadId2->Id.IpAddress.PrefixLength ||\r
540 CompareMem (&PadId1->Id.IpAddress.Address, &PadId2->Id.IpAddress.Address, sizeof (EFI_IP_ADDRESS)) != 0)\r
541 ) {\r
542 IsMatch = FALSE;\r
543 }\r
544\r
545 return IsMatch;\r
546}\r
547\r
548/**\r
549 Check if the SPD Selector is Zero by its LocalAddressCount and RemoteAddressCount\r
550 fields.\r
551\r
552 @param[in] Selector Pointer of the SPD Selector.\r
553\r
554 @retval TRUE If the SPD Selector is Zero.\r
555 @retval FALSE If the SPD Selector is not Zero.\r
556\r
557**/\r
558BOOLEAN\r
559IsZeroSpdSelector (\r
560 IN EFI_IPSEC_CONFIG_SELECTOR *Selector\r
561 )\r
562{\r
563 EFI_IPSEC_SPD_SELECTOR *SpdSel;\r
564 BOOLEAN IsZero;\r
565\r
566 SpdSel = &Selector->SpdSelector;\r
567 IsZero = FALSE;\r
568\r
569 if (SpdSel->LocalAddressCount == 0 && SpdSel->RemoteAddressCount == 0) {\r
570 IsZero = TRUE;\r
571 }\r
572\r
573 return IsZero;\r
574}\r
575\r
576/**\r
577 Check if the SA ID is Zero by its DestAddress.\r
578\r
579 @param[in] Selector Pointer of the SA ID.\r
580\r
581 @retval TRUE If the SA ID is Zero.\r
582 @retval FALSE If the SA ID is not Zero.\r
583\r
584**/\r
585BOOLEAN\r
586IsZeroSaId (\r
587 IN EFI_IPSEC_CONFIG_SELECTOR *Selector\r
588 )\r
589{\r
9166f840 590 BOOLEAN IsZero;\r
591 EFI_IPSEC_CONFIG_SELECTOR ZeroSelector;\r
592 \r
a3bcde70
HT
593 IsZero = FALSE;\r
594\r
9166f840 595 ZeroMem (&ZeroSelector, sizeof (EFI_IPSEC_CONFIG_SELECTOR));\r
a3bcde70 596\r
9166f840 597 if (CompareMem (&ZeroSelector, Selector, sizeof (EFI_IPSEC_CONFIG_SELECTOR)) == 0) {\r
a3bcde70
HT
598 IsZero = TRUE;\r
599 }\r
600\r
601 return IsZero;\r
602}\r
603\r
604/**\r
605 Check if the PAD ID is Zero.\r
606\r
607 @param[in] Selector Pointer of the PAD ID.\r
608\r
609 @retval TRUE If the PAD ID is Zero.\r
610 @retval FALSE If the PAD ID is not Zero.\r
611\r
612**/\r
613BOOLEAN\r
614IsZeroPadId (\r
615 IN EFI_IPSEC_CONFIG_SELECTOR *Selector\r
616 )\r
617{\r
618 EFI_IPSEC_PAD_ID *PadId;\r
619 EFI_IPSEC_PAD_ID ZeroId;\r
620 BOOLEAN IsZero;\r
621\r
622 PadId = &Selector->PadId;\r
623 IsZero = FALSE;\r
624\r
625 ZeroMem (&ZeroId, sizeof (EFI_IPSEC_PAD_ID));\r
626\r
627 if (CompareMem (PadId, &ZeroId, sizeof (EFI_IPSEC_PAD_ID)) == 0) {\r
628 IsZero = TRUE;\r
629 }\r
630\r
631 return IsZero;\r
632}\r
633\r
634/**\r
635 Copy Source SPD Selector to the Destination SPD Selector.\r
636\r
637 @param[in, out] DstSel Pointer of Destination SPD Selector.\r
638 @param[in] SrcSel Pointer of Source SPD Selector.\r
9166f840 639 @param[in, out] Size The size of the Destination SPD Selector. If it \r
640 not NULL and its value less than the size of \r
641 Source SPD Selector, the value of Source SPD \r
a3bcde70
HT
642 Selector's size will be passed to caller by this\r
643 parameter.\r
644\r
645 @retval EFI_INVALID_PARAMETER If the Destination or Source SPD Selector is NULL\r
9166f840 646 @retval EFI_BUFFER_TOO_SMALL If the input Size is less than size of the Source SPD Selector. \r
a3bcde70
HT
647 @retval EFI_SUCCESS Copy Source SPD Selector to the Destination SPD\r
648 Selector successfully.\r
649\r
650**/\r
651EFI_STATUS\r
652DuplicateSpdSelector (\r
653 IN OUT EFI_IPSEC_CONFIG_SELECTOR *DstSel,\r
654 IN EFI_IPSEC_CONFIG_SELECTOR *SrcSel,\r
655 IN OUT UINTN *Size\r
656 )\r
657{\r
658 EFI_IPSEC_SPD_SELECTOR *Dst;\r
659 EFI_IPSEC_SPD_SELECTOR *Src;\r
660\r
661 Dst = &DstSel->SpdSelector;\r
662 Src = &SrcSel->SpdSelector;\r
663\r
664 if (Dst == NULL || Src == NULL) {\r
665 return EFI_INVALID_PARAMETER;\r
666 }\r
667\r
668 if (Size != NULL && (*Size) < SIZE_OF_SPD_SELECTOR (Src)) {\r
669 *Size = SIZE_OF_SPD_SELECTOR (Src);\r
670 return EFI_BUFFER_TOO_SMALL;\r
671 }\r
672 //\r
9166f840 673 // Copy the base structure of SPD selector.\r
a3bcde70
HT
674 //\r
675 CopyMem (Dst, Src, sizeof (EFI_IPSEC_SPD_SELECTOR));\r
676\r
677 //\r
9166f840 678 // Copy the local address array of SPD selector.\r
a3bcde70
HT
679 //\r
680 Dst->LocalAddress = (EFI_IP_ADDRESS_INFO *) (Dst + 1);\r
681 CopyMem (\r
682 Dst->LocalAddress,\r
683 Src->LocalAddress,\r
684 sizeof (EFI_IP_ADDRESS_INFO) * Dst->LocalAddressCount\r
685 );\r
686\r
687 //\r
9166f840 688 // Copy the remote address array of SPD selector.\r
a3bcde70
HT
689 //\r
690 Dst->RemoteAddress = Dst->LocalAddress + Dst->LocalAddressCount;\r
691 CopyMem (\r
692 Dst->RemoteAddress,\r
693 Src->RemoteAddress,\r
694 sizeof (EFI_IP_ADDRESS_INFO) * Dst->RemoteAddressCount\r
695 );\r
696\r
697 return EFI_SUCCESS;\r
698}\r
699\r
700/**\r
701 Copy Source SA ID to the Destination SA ID.\r
702\r
703 @param[in, out] DstSel Pointer of Destination SA ID.\r
704 @param[in] SrcSel Pointer of Source SA ID.\r
9166f840 705 @param[in, out] Size The size of the Destination SA ID. If it \r
706 not NULL and its value less than the size of \r
707 Source SA ID, the value of Source SA ID's size \r
a3bcde70
HT
708 will be passed to caller by this parameter.\r
709\r
710 @retval EFI_INVALID_PARAMETER If the Destination or Source SA ID is NULL.\r
9166f840 711 @retval EFI_BUFFER_TOO_SMALL If the input Size less than size of source SA ID. \r
a3bcde70
HT
712 @retval EFI_SUCCESS Copy Source SA ID to the Destination SA ID successfully.\r
713\r
714**/\r
715EFI_STATUS\r
716DuplicateSaId (\r
717 IN OUT EFI_IPSEC_CONFIG_SELECTOR *DstSel,\r
718 IN EFI_IPSEC_CONFIG_SELECTOR *SrcSel,\r
719 IN OUT UINTN *Size\r
720 )\r
721{\r
722 EFI_IPSEC_SA_ID *Dst;\r
723 EFI_IPSEC_SA_ID *Src;\r
724\r
725 Dst = &DstSel->SaId;\r
726 Src = &SrcSel->SaId;\r
727\r
728 if (Dst == NULL || Src == NULL) {\r
729 return EFI_INVALID_PARAMETER;\r
730 }\r
731\r
732 if (Size != NULL && *Size < sizeof (EFI_IPSEC_SA_ID)) {\r
733 *Size = sizeof (EFI_IPSEC_SA_ID);\r
734 return EFI_BUFFER_TOO_SMALL;\r
735 }\r
736\r
737 CopyMem (Dst, Src, sizeof (EFI_IPSEC_SA_ID));\r
738\r
739 return EFI_SUCCESS;\r
740}\r
741\r
742/**\r
743 Copy Source PAD ID to the Destination PAD ID.\r
744\r
745 @param[in, out] DstSel Pointer of Destination PAD ID.\r
746 @param[in] SrcSel Pointer of Source PAD ID.\r
9166f840 747 @param[in, out] Size The size of the Destination PAD ID. If it \r
748 not NULL and its value less than the size of \r
749 Source PAD ID, the value of Source PAD ID's size \r
a3bcde70
HT
750 will be passed to caller by this parameter.\r
751\r
752 @retval EFI_INVALID_PARAMETER If the Destination or Source PAD ID is NULL.\r
753 @retval EFI_BUFFER_TOO_SMALL If the input Size less than size of source PAD ID .\r
754 @retval EFI_SUCCESS Copy Source PAD ID to the Destination PAD ID successfully.\r
755\r
756**/\r
757EFI_STATUS\r
758DuplicatePadId (\r
759 IN OUT EFI_IPSEC_CONFIG_SELECTOR *DstSel,\r
760 IN EFI_IPSEC_CONFIG_SELECTOR *SrcSel,\r
761 IN OUT UINTN *Size\r
762 )\r
763{\r
764 EFI_IPSEC_PAD_ID *Dst;\r
765 EFI_IPSEC_PAD_ID *Src;\r
766\r
767 Dst = &DstSel->PadId;\r
768 Src = &SrcSel->PadId;\r
769\r
770 if (Dst == NULL || Src == NULL) {\r
771 return EFI_INVALID_PARAMETER;\r
772 }\r
773\r
774 if (Size != NULL && *Size < sizeof (EFI_IPSEC_PAD_ID)) {\r
775 *Size = sizeof (EFI_IPSEC_PAD_ID);\r
776 return EFI_BUFFER_TOO_SMALL;\r
777 }\r
778\r
779 CopyMem (Dst, Src, sizeof (EFI_IPSEC_PAD_ID));\r
780\r
781 return EFI_SUCCESS;\r
782}\r
783\r
784/**\r
9166f840 785 Fix the value of some members of SPD Selector. \r
a3bcde70 786\r
9166f840 787 This function is called by IpSecCopyPolicyEntry()which copy the Policy \r
788 Entry into the Variable. Since some members in SPD Selector are pointers, \r
789 a physical address to relative address convertion is required before copying \r
a3bcde70
HT
790 this SPD entry into the variable.\r
791\r
792 @param[in] Selector Pointer of SPD Selector.\r
793 @param[in, out] Data Pointer of SPD Data.\r
794\r
795**/\r
796VOID\r
797FixSpdEntry (\r
798 IN EFI_IPSEC_SPD_SELECTOR *Selector,\r
799 IN OUT EFI_IPSEC_SPD_DATA *Data\r
800 )\r
801{\r
802 //\r
9166f840 803 // It assumes that all ref buffers in SPD selector and data are\r
a3bcde70
HT
804 // stored in the continous memory and close to the base structure.\r
805 //\r
806 FIX_REF_BUF_ADDR (Selector->LocalAddress, Selector);\r
807 FIX_REF_BUF_ADDR (Selector->RemoteAddress, Selector);\r
808\r
809 if (Data->ProcessingPolicy != NULL) {\r
810 if (Data->ProcessingPolicy->TunnelOption != NULL) {\r
811 FIX_REF_BUF_ADDR (Data->ProcessingPolicy->TunnelOption, Data);\r
812 }\r
813\r
814 FIX_REF_BUF_ADDR (Data->ProcessingPolicy, Data);\r
815 }\r
816\r
817}\r
818\r
819/**\r
9166f840 820 Fix the value of some members of SA ID. \r
a3bcde70 821\r
9166f840 822 This function is called by IpSecCopyPolicyEntry()which copy the Policy \r
823 Entry into the Variable. Since some members in SA ID are pointers, \r
824 a physical address to relative address conversion is required before copying \r
a3bcde70
HT
825 this SAD into the variable.\r
826\r
827 @param[in] SaId Pointer of SA ID\r
828 @param[in, out] Data Pointer of SA Data.\r
829\r
830**/\r
831VOID\r
832FixSadEntry (\r
833 IN EFI_IPSEC_SA_ID *SaId,\r
9166f840 834 IN OUT EFI_IPSEC_SA_DATA2 *Data\r
a3bcde70
HT
835 )\r
836{\r
837 //\r
9166f840 838 // It assumes that all ref buffers in SAD selector and data are\r
a3bcde70
HT
839 // stored in the continous memory and close to the base structure.\r
840 //\r
841 if (Data->AlgoInfo.EspAlgoInfo.AuthKey != NULL) {\r
842 FIX_REF_BUF_ADDR (Data->AlgoInfo.EspAlgoInfo.AuthKey, Data);\r
843 }\r
844\r
845 if (SaId->Proto == EfiIPsecESP && Data->AlgoInfo.EspAlgoInfo.EncKey != NULL) {\r
846 FIX_REF_BUF_ADDR (Data->AlgoInfo.EspAlgoInfo.EncKey, Data);\r
847 }\r
848\r
849 if (Data->SpdSelector != NULL) {\r
850 if (Data->SpdSelector->LocalAddress != NULL) {\r
851 FIX_REF_BUF_ADDR (Data->SpdSelector->LocalAddress, Data);\r
852 }\r
853\r
854 FIX_REF_BUF_ADDR (Data->SpdSelector->RemoteAddress, Data);\r
855 FIX_REF_BUF_ADDR (Data->SpdSelector, Data);\r
856 }\r
857\r
858}\r
859\r
860/**\r
9166f840 861 Fix the value of some members of PAD ID. \r
a3bcde70 862\r
9166f840 863 This function is called by IpSecCopyPolicyEntry()which copy the Policy \r
864 Entry into the Variable. Since some members in PAD ID are pointers, \r
a3bcde70
HT
865 a physical address to relative address conversion is required before copying\r
866 this PAD into the variable.\r
867\r
868 @param[in] PadId Pointer of PAD ID.\r
869 @param[in, out] Data Pointer of PAD Data.\r
870\r
871**/\r
872VOID\r
873FixPadEntry (\r
874 IN EFI_IPSEC_PAD_ID *PadId,\r
875 IN OUT EFI_IPSEC_PAD_DATA *Data\r
876 )\r
877{\r
878 //\r
879 // It assumes that all ref buffers in pad selector and data are\r
880 // stored in the continous memory and close to the base structure.\r
881 //\r
882 if (Data->AuthData != NULL) {\r
883 FIX_REF_BUF_ADDR (Data->AuthData, Data);\r
884 }\r
885\r
886 if (Data->RevocationData != NULL) {\r
887 FIX_REF_BUF_ADDR (Data->RevocationData, Data);\r
888 }\r
889\r
890}\r
891\r
892/**\r
9166f840 893 Recover the value of some members of SPD Selector. \r
a3bcde70
HT
894\r
895 This function is corresponding to FixSpdEntry(). It recovers the value of members\r
896 of SPD Selector that are fixed by FixSpdEntry().\r
897\r
898 @param[in, out] Selector Pointer of SPD Selector.\r
899 @param[in, out] Data Pointer of SPD Data.\r
900\r
901**/\r
902VOID\r
903UnfixSpdEntry (\r
904 IN OUT EFI_IPSEC_SPD_SELECTOR *Selector,\r
905 IN OUT EFI_IPSEC_SPD_DATA *Data\r
906 )\r
907{\r
908 //\r
9166f840 909 // It assumes that all ref buffers in SPD selector and data are\r
a3bcde70
HT
910 // stored in the continous memory and close to the base structure.\r
911 //\r
912 UNFIX_REF_BUF_ADDR (Selector->LocalAddress, Selector);\r
913 UNFIX_REF_BUF_ADDR (Selector->RemoteAddress, Selector);\r
914\r
915 if (Data->ProcessingPolicy != NULL) {\r
916 UNFIX_REF_BUF_ADDR (Data->ProcessingPolicy, Data);\r
917 if (Data->ProcessingPolicy->TunnelOption != NULL) {\r
918 UNFIX_REF_BUF_ADDR (Data->ProcessingPolicy->TunnelOption, Data);\r
919 }\r
920 }\r
9166f840 921 \r
a3bcde70
HT
922}\r
923\r
924/**\r
9166f840 925 Recover the value of some members of SA ID. \r
a3bcde70
HT
926\r
927 This function is corresponding to FixSadEntry(). It recovers the value of members\r
928 of SAD ID that are fixed by FixSadEntry().\r
929\r
930 @param[in, out] SaId Pointer of SAD ID.\r
931 @param[in, out] Data Pointer of SAD Data.\r
932\r
933**/\r
934VOID\r
935UnfixSadEntry (\r
936 IN OUT EFI_IPSEC_SA_ID *SaId,\r
9166f840 937 IN OUT EFI_IPSEC_SA_DATA2 *Data\r
a3bcde70
HT
938 )\r
939{\r
940 //\r
9166f840 941 // It assumes that all ref buffers in SAD selector and data are\r
a3bcde70
HT
942 // stored in the continous memory and close to the base structure.\r
943 //\r
944 if (Data->AlgoInfo.EspAlgoInfo.AuthKey != NULL) {\r
945 UNFIX_REF_BUF_ADDR (Data->AlgoInfo.EspAlgoInfo.AuthKey, Data);\r
946 }\r
947\r
948 if (SaId->Proto == EfiIPsecESP && Data->AlgoInfo.EspAlgoInfo.EncKey != NULL) {\r
949 UNFIX_REF_BUF_ADDR (Data->AlgoInfo.EspAlgoInfo.EncKey, Data);\r
950 }\r
951\r
952 if (Data->SpdSelector != NULL) {\r
953 UNFIX_REF_BUF_ADDR (Data->SpdSelector, Data);\r
954 if (Data->SpdSelector->LocalAddress != NULL) {\r
955 UNFIX_REF_BUF_ADDR (Data->SpdSelector->LocalAddress, Data);\r
956 }\r
957\r
958 UNFIX_REF_BUF_ADDR (Data->SpdSelector->RemoteAddress, Data);\r
959 }\r
960\r
961}\r
962\r
963/**\r
9166f840 964 Recover the value of some members of PAD ID. \r
a3bcde70
HT
965\r
966 This function is corresponding to FixPadEntry(). It recovers the value of members\r
967 of PAD ID that are fixed by FixPadEntry().\r
968\r
969 @param[in] PadId Pointer of PAD ID.\r
970 @param[in, out] Data Pointer of PAD Data.\r
971\r
972**/\r
973VOID\r
974UnfixPadEntry (\r
975 IN EFI_IPSEC_PAD_ID *PadId,\r
976 IN OUT EFI_IPSEC_PAD_DATA *Data\r
977 )\r
978{\r
979 //\r
980 // It assumes that all ref buffers in pad selector and data are\r
981 // stored in the continous memory and close to the base structure.\r
982 //\r
983 if (Data->AuthData != NULL) {\r
984 UNFIX_REF_BUF_ADDR (Data->AuthData, Data);\r
985 }\r
986\r
987 if (Data->RevocationData != NULL) {\r
988 UNFIX_REF_BUF_ADDR (Data->RevocationData, Data);\r
989 }\r
990\r
991}\r
992\r
993/**\r
994 Set the security policy information for the EFI IPsec driver.\r
995\r
9166f840 996 The IPsec configuration data has a unique selector/identifier separately to \r
a3bcde70
HT
997 identify a data entry.\r
998\r
9166f840 999 @param[in] Selector Pointer to an entry selector on operated \r
1000 configuration data specified by DataType. \r
1001 A NULL Selector causes the entire specified-type \r
a3bcde70 1002 configuration information to be flushed.\r
9166f840 1003 @param[in] Data The data buffer to be set. The structure \r
a3bcde70 1004 of the data buffer should be EFI_IPSEC_SPD_DATA.\r
9166f840 1005 @param[in] Context Pointer to one entry selector that describes \r
1006 the expected position the new data entry will \r
a3bcde70
HT
1007 be added. If Context is NULL, the new entry will\r
1008 be appended the end of database.\r
1009\r
1010 @retval EFI_INVALID_PARAMETER One or more of the following are TRUE:\r
9166f840 1011 - Selector is not NULL and its LocalAddress \r
a3bcde70 1012 is NULL or its RemoteAddress is NULL.\r
9166f840 1013 - Data is not NULL and its Action is Protected \r
a3bcde70
HT
1014 and its plolicy is NULL.\r
1015 - Data is not NULL, its Action is not protected,\r
1016 and its policy is not NULL.\r
9166f840 1017 - The Action of Data is Protected, its policy \r
a3bcde70 1018 mode is Tunnel, and its tunnel option is NULL.\r
9166f840 1019 - The Action of Data is protected and its policy \r
a3bcde70
HT
1020 mode is not Tunnel and it tunnel option is not NULL.\r
1021 @retval EFI_OUT_OF_RESOURCED The required system resource could not be allocated.\r
1022 @retval EFI_SUCCESS The specified configuration data was obtained successfully.\r
1023\r
1024**/\r
1025EFI_STATUS\r
1026SetSpdEntry (\r
1027 IN EFI_IPSEC_CONFIG_SELECTOR *Selector,\r
1028 IN VOID *Data,\r
1029 IN VOID *Context OPTIONAL\r
1030 )\r
1031{\r
1032 EFI_IPSEC_SPD_SELECTOR *SpdSel;\r
1033 EFI_IPSEC_SPD_DATA *SpdData;\r
1034 EFI_IPSEC_SPD_SELECTOR *InsertBefore;\r
1035 LIST_ENTRY *SpdList;\r
1036 LIST_ENTRY *SadList;\r
1037 LIST_ENTRY *SpdSas;\r
1038 LIST_ENTRY *EntryInsertBefore;\r
1039 LIST_ENTRY *Entry;\r
a3bcde70 1040 LIST_ENTRY *Entry2;\r
9166f840 1041 LIST_ENTRY *NextEntry;\r
a3bcde70
HT
1042 IPSEC_SPD_ENTRY *SpdEntry;\r
1043 IPSEC_SAD_ENTRY *SadEntry;\r
1044 UINTN SpdEntrySize;\r
1045 UINTN Index;\r
1046\r
1047 SpdSel = (Selector == NULL) ? NULL : &Selector->SpdSelector;\r
1048 SpdData = (Data == NULL) ? NULL : (EFI_IPSEC_SPD_DATA *) Data;\r
1049 InsertBefore = (Context == NULL) ? NULL : &((EFI_IPSEC_CONFIG_SELECTOR *) Context)->SpdSelector;\r
1050 SpdList = &mConfigData[IPsecConfigDataTypeSpd];\r
1051\r
1052 if (SpdSel != NULL) {\r
1053 if (SpdSel->LocalAddress == NULL || SpdSel->RemoteAddress == NULL) {\r
1054 return EFI_INVALID_PARAMETER;\r
1055 }\r
1056 }\r
1057\r
1058 if (SpdData != NULL) {\r
1059 if ((SpdData->Action == EfiIPsecActionProtect && SpdData->ProcessingPolicy == NULL) ||\r
1060 (SpdData->Action != EfiIPsecActionProtect && SpdData->ProcessingPolicy != NULL)\r
1061 ) {\r
1062 return EFI_INVALID_PARAMETER;\r
1063 }\r
1064\r
1065 if (SpdData->Action == EfiIPsecActionProtect) {\r
1066 if ((SpdData->ProcessingPolicy->Mode == EfiIPsecTunnel && SpdData->ProcessingPolicy->TunnelOption == NULL) ||\r
1067 (SpdData->ProcessingPolicy->Mode != EfiIPsecTunnel && SpdData->ProcessingPolicy->TunnelOption != NULL)\r
1068 ) {\r
1069 return EFI_INVALID_PARAMETER;\r
1070 }\r
1071 }\r
1072 }\r
1073 //\r
1074 // The default behavior is to insert the node ahead of the header.\r
1075 //\r
1076 EntryInsertBefore = SpdList;\r
1077\r
1078 //\r
9166f840 1079 // Remove the existed SPD entry.\r
a3bcde70
HT
1080 //\r
1081 NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, SpdList) {\r
1082\r
1083 SpdEntry = IPSEC_SPD_ENTRY_FROM_LIST (Entry);\r
1084\r
9166f840 1085 if (SpdSel == NULL || \r
a3bcde70
HT
1086 CompareSpdSelector ((EFI_IPSEC_CONFIG_SELECTOR *) SpdEntry->Selector, (EFI_IPSEC_CONFIG_SELECTOR *) SpdSel)\r
1087 ) {\r
1088 //\r
1089 // Record the existed entry position to keep the original order.\r
1090 //\r
1091 EntryInsertBefore = SpdEntry->List.ForwardLink;\r
1092 RemoveEntryList (&SpdEntry->List);\r
1093\r
1094 //\r
9166f840 1095 // Update the reverse ref of SAD entry in the SPD.sas list.\r
a3bcde70
HT
1096 //\r
1097 SpdSas = &SpdEntry->Data->Sas;\r
9166f840 1098 \r
1099 //\r
1100 // TODO: Deleted the related SAs.\r
1101 //\r
a3bcde70
HT
1102 NET_LIST_FOR_EACH (Entry2, SpdSas) {\r
1103 SadEntry = IPSEC_SAD_ENTRY_FROM_SPD (Entry2);\r
1104 SadEntry->Data->SpdEntry = NULL;\r
1105 }\r
9166f840 1106 \r
a3bcde70 1107 //\r
9166f840 1108 // Free the existed SPD entry\r
a3bcde70
HT
1109 //\r
1110 FreePool (SpdEntry);\r
1111 }\r
1112 }\r
1113 //\r
9166f840 1114 // Return success here if only want to remove the SPD entry.\r
a3bcde70
HT
1115 //\r
1116 if (SpdData == NULL || SpdSel == NULL) {\r
1117 return EFI_SUCCESS;\r
1118 }\r
1119 //\r
1120 // Search the appointed entry position if InsertBefore is not NULL.\r
1121 //\r
1122 if (InsertBefore != NULL) {\r
1123\r
1124 NET_LIST_FOR_EACH (Entry, SpdList) {\r
1125 SpdEntry = IPSEC_SPD_ENTRY_FROM_LIST (Entry);\r
1126\r
1127 if (CompareSpdSelector (\r
1128 (EFI_IPSEC_CONFIG_SELECTOR *) SpdEntry->Selector,\r
1129 (EFI_IPSEC_CONFIG_SELECTOR *) InsertBefore\r
1130 )) {\r
1131 EntryInsertBefore = Entry;\r
1132 break;\r
1133 }\r
1134 }\r
1135 }\r
1136\r
1137 //\r
1138 // Do Padding for the different Arch.\r
1139 //\r
1140 SpdEntrySize = ALIGN_VARIABLE (sizeof (IPSEC_SPD_ENTRY));\r
1141 SpdEntrySize = ALIGN_VARIABLE (SpdEntrySize + (UINTN)SIZE_OF_SPD_SELECTOR (SpdSel));\r
1142 SpdEntrySize += IpSecGetSizeOfEfiSpdData (SpdData);\r
1143\r
1144 SpdEntry = AllocateZeroPool (SpdEntrySize);\r
1145\r
1146 if (SpdEntry == NULL) {\r
1147 return EFI_OUT_OF_RESOURCES;\r
1148 }\r
1149 //\r
1150 // Fix the address of Selector and Data buffer and copy them, which is\r
9166f840 1151 // continous memory and close to the base structure of SPD entry.\r
a3bcde70
HT
1152 //\r
1153 SpdEntry->Selector = (EFI_IPSEC_SPD_SELECTOR *) ALIGN_POINTER ((SpdEntry + 1), sizeof (UINTN));\r
1154 SpdEntry->Data = (IPSEC_SPD_DATA *) ALIGN_POINTER (\r
1155 ((UINT8 *) SpdEntry->Selector + SIZE_OF_SPD_SELECTOR (SpdSel)),\r
1156 sizeof (UINTN)\r
1157 );\r
1158\r
1159 DuplicateSpdSelector (\r
1160 (EFI_IPSEC_CONFIG_SELECTOR *) SpdEntry->Selector,\r
1161 (EFI_IPSEC_CONFIG_SELECTOR *) SpdSel,\r
1162 NULL\r
1163 );\r
1164\r
1165 CopyMem (\r
1166 SpdEntry->Data->Name,\r
1167 SpdData->Name,\r
1168 sizeof (SpdData->Name)\r
1169 );\r
1170 SpdEntry->Data->PackageFlag = SpdData->PackageFlag;\r
1171 SpdEntry->Data->Action = SpdData->Action;\r
1172\r
1173 //\r
1174 // Fix the address of ProcessingPolicy and copy it if need, which is continous\r
9166f840 1175 // memory and close to the base structure of SAD data.\r
a3bcde70
HT
1176 //\r
1177 if (SpdData->Action != EfiIPsecActionProtect) {\r
1178 SpdEntry->Data->ProcessingPolicy = NULL;\r
1179 } else {\r
1180 SpdEntry->Data->ProcessingPolicy = (EFI_IPSEC_PROCESS_POLICY *) ALIGN_POINTER (\r
1181 SpdEntry->Data + 1,\r
1182 sizeof (UINTN)\r
1183 );\r
1184 IpSecDuplicateProcessPolicy (SpdEntry->Data->ProcessingPolicy, SpdData->ProcessingPolicy);\r
1185 }\r
1186 //\r
9166f840 1187 // Update the sas list of the new SPD entry.\r
a3bcde70
HT
1188 //\r
1189 InitializeListHead (&SpdEntry->Data->Sas);\r
1190\r
1191 SadList = &mConfigData[IPsecConfigDataTypeSad];\r
1192\r
1193 NET_LIST_FOR_EACH (Entry, SadList) {\r
1194 SadEntry = IPSEC_SAD_ENTRY_FROM_LIST (Entry);\r
1195\r
1196 for (Index = 0; Index < SpdData->SaIdCount; Index++) {\r
1197\r
1198 if (CompareSaId (\r
1199 (EFI_IPSEC_CONFIG_SELECTOR *) &SpdData->SaId[Index],\r
1200 (EFI_IPSEC_CONFIG_SELECTOR *) SadEntry->Id\r
1201 )) {\r
1202 InsertTailList (&SpdEntry->Data->Sas, &SadEntry->BySpd);\r
1203 SadEntry->Data->SpdEntry = SpdEntry;\r
9166f840 1204 DuplicateSpdSelector (\r
1205 (EFI_IPSEC_CONFIG_SELECTOR *)SadEntry->Data->SpdSelector,\r
1206 (EFI_IPSEC_CONFIG_SELECTOR *)SpdEntry->Selector,\r
1207 NULL\r
1208 ); \r
a3bcde70
HT
1209 }\r
1210 }\r
1211 }\r
1212 //\r
9166f840 1213 // Insert the new SPD entry.\r
a3bcde70
HT
1214 //\r
1215 InsertTailList (EntryInsertBefore, &SpdEntry->List);\r
1216\r
1217 return EFI_SUCCESS;\r
1218}\r
1219\r
1220/**\r
1221 Set the security association information for the EFI IPsec driver.\r
1222\r
9166f840 1223 The IPsec configuration data has a unique selector/identifier separately to \r
a3bcde70
HT
1224 identify a data entry.\r
1225\r
9166f840 1226 @param[in] Selector Pointer to an entry selector on operated \r
1227 configuration data specified by DataType. \r
1228 A NULL Selector causes the entire specified-type \r
a3bcde70 1229 configuration information to be flushed.\r
9166f840 1230 @param[in] Data The data buffer to be set. The structure \r
a3bcde70 1231 of the data buffer should be EFI_IPSEC_SA_DATA.\r
9166f840 1232 @param[in] Context Pointer to one entry selector which describes \r
1233 the expected position the new data entry will \r
a3bcde70
HT
1234 be added. If Context is NULL,the new entry will\r
1235 be appended the end of database.\r
1236\r
1237 @retval EFI_OUT_OF_RESOURCED The required system resource could not be allocated.\r
1238 @retval EFI_SUCCESS The specified configuration data was obtained successfully.\r
1239\r
1240**/\r
1241EFI_STATUS\r
1242SetSadEntry (\r
1243 IN EFI_IPSEC_CONFIG_SELECTOR *Selector,\r
1244 IN VOID *Data,\r
1245 IN VOID *Context OPTIONAL\r
1246 )\r
1247{\r
1248 IPSEC_SAD_ENTRY *SadEntry;\r
1249 IPSEC_SPD_ENTRY *SpdEntry;\r
1250 LIST_ENTRY *Entry;\r
1251 LIST_ENTRY *NextEntry;\r
1252 LIST_ENTRY *SadList;\r
1253 LIST_ENTRY *SpdList;\r
1254 EFI_IPSEC_SA_ID *SaId;\r
9166f840 1255 EFI_IPSEC_SA_DATA2 *SaData;\r
a3bcde70
HT
1256 EFI_IPSEC_SA_ID *InsertBefore;\r
1257 LIST_ENTRY *EntryInsertBefore;\r
1258 UINTN SadEntrySize;\r
9166f840 1259 \r
a3bcde70 1260 SaId = (Selector == NULL) ? NULL : &Selector->SaId;\r
9166f840 1261 SaData = (Data == NULL) ? NULL : (EFI_IPSEC_SA_DATA2 *) Data;\r
a3bcde70
HT
1262 InsertBefore = (Context == NULL) ? NULL : &((EFI_IPSEC_CONFIG_SELECTOR *) Context)->SaId;\r
1263 SadList = &mConfigData[IPsecConfigDataTypeSad];\r
1264\r
1265 //\r
1266 // The default behavior is to insert the node ahead of the header.\r
1267 //\r
1268 EntryInsertBefore = SadList;\r
1269\r
1270 //\r
9166f840 1271 // Remove the existed SAD entry.\r
a3bcde70
HT
1272 //\r
1273 NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, SadList) {\r
1274\r
1275 SadEntry = IPSEC_SAD_ENTRY_FROM_LIST (Entry);\r
1276\r
9166f840 1277 if (SaId == NULL || \r
a3bcde70
HT
1278 CompareSaId (\r
1279 (EFI_IPSEC_CONFIG_SELECTOR *) SadEntry->Id,\r
1280 (EFI_IPSEC_CONFIG_SELECTOR *) SaId\r
1281 )) {\r
1282 //\r
1283 // Record the existed entry position to keep the original order.\r
1284 //\r
1285 EntryInsertBefore = SadEntry->List.ForwardLink;\r
1286\r
1287 //\r
9166f840 1288 // Update the related SAD.byspd field.\r
a3bcde70
HT
1289 //\r
1290 if (SadEntry->Data->SpdEntry != NULL) {\r
1291 RemoveEntryList (&SadEntry->BySpd);\r
1292 }\r
1293\r
1294 RemoveEntryList (&SadEntry->List);\r
1295 FreePool (SadEntry);\r
1296 }\r
1297 }\r
1298 //\r
9166f840 1299 // Return success here if only want to remove the SAD entry\r
a3bcde70
HT
1300 //\r
1301 if (SaData == NULL || SaId == NULL) {\r
1302 return EFI_SUCCESS;\r
1303 }\r
1304 //\r
1305 // Search the appointed entry position if InsertBefore is not NULL.\r
1306 //\r
1307 if (InsertBefore != NULL) {\r
1308\r
1309 NET_LIST_FOR_EACH (Entry, SadList) {\r
1310 SadEntry = IPSEC_SAD_ENTRY_FROM_LIST (Entry);\r
1311\r
1312 if (CompareSaId (\r
1313 (EFI_IPSEC_CONFIG_SELECTOR *) SadEntry->Id,\r
1314 (EFI_IPSEC_CONFIG_SELECTOR *) InsertBefore\r
1315 )) {\r
1316 EntryInsertBefore = Entry;\r
1317 break;\r
1318 }\r
1319 }\r
1320 }\r
1321\r
1322 //\r
1323 // Do Padding for different Arch.\r
1324 //\r
1325 SadEntrySize = ALIGN_VARIABLE (sizeof (IPSEC_SAD_ENTRY));\r
9166f840 1326 SadEntrySize = ALIGN_VARIABLE (SadEntrySize + sizeof (EFI_IPSEC_SA_ID));\r
a3bcde70 1327 SadEntrySize = ALIGN_VARIABLE (SadEntrySize + sizeof (IPSEC_SAD_DATA));\r
9166f840 1328 \r
a3bcde70
HT
1329 if (SaId->Proto == EfiIPsecAH) {\r
1330 SadEntrySize += SaData->AlgoInfo.AhAlgoInfo.AuthKeyLength;\r
1331 } else {\r
1332 SadEntrySize = ALIGN_VARIABLE (SadEntrySize + SaData->AlgoInfo.EspAlgoInfo.AuthKeyLength);\r
9166f840 1333 SadEntrySize += ALIGN_VARIABLE (SaData->AlgoInfo.EspAlgoInfo.EncKeyLength);\r
a3bcde70
HT
1334 }\r
1335\r
9166f840 1336 if (SaData->SpdSelector != NULL) {\r
1337 SadEntrySize += SadEntrySize + (UINTN)SIZE_OF_SPD_SELECTOR (SaData->SpdSelector);\r
1338 }\r
a3bcde70
HT
1339 SadEntry = AllocateZeroPool (SadEntrySize);\r
1340\r
1341 if (SadEntry == NULL) {\r
1342 return EFI_OUT_OF_RESOURCES;\r
1343 }\r
1344 //\r
1345 // Fix the address of Id and Data buffer and copy them, which is\r
9166f840 1346 // continous memory and close to the base structure of SAD entry.\r
a3bcde70
HT
1347 //\r
1348 SadEntry->Id = (EFI_IPSEC_SA_ID *) ALIGN_POINTER ((SadEntry + 1), sizeof (UINTN));\r
1349 SadEntry->Data = (IPSEC_SAD_DATA *) ALIGN_POINTER ((SadEntry->Id + 1), sizeof (UINTN));\r
1350\r
1351 CopyMem (SadEntry->Id, SaId, sizeof (EFI_IPSEC_SA_ID));\r
1352\r
1353 SadEntry->Data->Mode = SaData->Mode;\r
1354 SadEntry->Data->SequenceNumber = SaData->SNCount;\r
1355 SadEntry->Data->AntiReplayWindowSize = SaData->AntiReplayWindows;\r
1356\r
1357 ZeroMem (\r
1358 &SadEntry->Data->AntiReplayBitmap,\r
1359 sizeof (SadEntry->Data->AntiReplayBitmap)\r
1360 );\r
1361\r
1362 ZeroMem (\r
1363 &SadEntry->Data->AlgoInfo,\r
1364 sizeof (EFI_IPSEC_ALGO_INFO)\r
1365 );\r
1366\r
1367 SadEntry->Data->AlgoInfo.EspAlgoInfo.AuthAlgoId = SaData->AlgoInfo.EspAlgoInfo.AuthAlgoId;\r
1368 SadEntry->Data->AlgoInfo.EspAlgoInfo.AuthKeyLength = SaData->AlgoInfo.EspAlgoInfo.AuthKeyLength;\r
1369\r
1370 if (SadEntry->Data->AlgoInfo.EspAlgoInfo.AuthKeyLength != 0) {\r
1371 SadEntry->Data->AlgoInfo.EspAlgoInfo.AuthKey = (VOID *) ALIGN_POINTER ((SadEntry->Data + 1), sizeof (UINTN));\r
1372 CopyMem (\r
1373 SadEntry->Data->AlgoInfo.EspAlgoInfo.AuthKey,\r
1374 SaData->AlgoInfo.EspAlgoInfo.AuthKey,\r
1375 SadEntry->Data->AlgoInfo.EspAlgoInfo.AuthKeyLength\r
1376 );\r
1377 }\r
1378\r
1379 if (SaId->Proto == EfiIPsecESP) {\r
1380 SadEntry->Data->AlgoInfo.EspAlgoInfo.EncAlgoId = SaData->AlgoInfo.EspAlgoInfo.EncAlgoId;\r
1381 SadEntry->Data->AlgoInfo.EspAlgoInfo.EncKeyLength = SaData->AlgoInfo.EspAlgoInfo.EncKeyLength;\r
1382\r
1383 if (SadEntry->Data->AlgoInfo.EspAlgoInfo.EncKeyLength != 0) {\r
1384 SadEntry->Data->AlgoInfo.EspAlgoInfo.EncKey = (VOID *) ALIGN_POINTER (\r
9166f840 1385 ((UINT8 *) (SadEntry->Data + 1) + \r
a3bcde70
HT
1386 SadEntry->Data->AlgoInfo.EspAlgoInfo.AuthKeyLength),\r
1387 sizeof (UINTN)\r
1388 );\r
1389 CopyMem (\r
1390 SadEntry->Data->AlgoInfo.EspAlgoInfo.EncKey,\r
1391 SaData->AlgoInfo.EspAlgoInfo.EncKey,\r
1392 SadEntry->Data->AlgoInfo.EspAlgoInfo.EncKeyLength\r
1393 );\r
1394 }\r
1395 }\r
1396\r
1397 CopyMem (\r
1398 &SadEntry->Data->SaLifetime,\r
1399 &SaData->SaLifetime,\r
1400 sizeof (EFI_IPSEC_SA_LIFETIME)\r
1401 );\r
1402\r
1403 SadEntry->Data->PathMTU = SaData->PathMTU;\r
9166f840 1404 SadEntry->Data->SpdSelector = NULL;\r
a3bcde70
HT
1405 SadEntry->Data->ESNEnabled = FALSE;\r
1406 SadEntry->Data->ManualSet = SaData->ManualSet;\r
1407\r
1408 //\r
9166f840 1409 // Copy Tunnel Source/Destination Address\r
1410 //\r
1411 if (SaData->Mode == EfiIPsecTunnel) {\r
1412 CopyMem (\r
1413 &SadEntry->Data->TunnelDestAddress,\r
1414 &SaData->TunnelDestinationAddress,\r
1415 sizeof (EFI_IP_ADDRESS)\r
1416 );\r
1417 CopyMem (\r
1418 &SadEntry->Data->TunnelSourceAddress,\r
1419 &SaData->TunnelSourceAddress,\r
1420 sizeof (EFI_IP_ADDRESS)\r
1421 );\r
1422 }\r
1423 //\r
1424 // Update the spd.sas list of the spd entry specified by SAD selector\r
a3bcde70
HT
1425 //\r
1426 SpdList = &mConfigData[IPsecConfigDataTypeSpd];\r
1427\r
1428 for (Entry = SpdList->ForwardLink; Entry != SpdList && SaData->SpdSelector != NULL; Entry = Entry->ForwardLink) {\r
1429\r
1430 SpdEntry = IPSEC_SPD_ENTRY_FROM_LIST (Entry);\r
9166f840 1431 if (IsSubSpdSelector (\r
1432 (EFI_IPSEC_CONFIG_SELECTOR *) SaData->SpdSelector,\r
1433 (EFI_IPSEC_CONFIG_SELECTOR *) SpdEntry->Selector\r
a3bcde70
HT
1434 ) && SpdEntry->Data->Action == EfiIPsecActionProtect) {\r
1435 SadEntry->Data->SpdEntry = SpdEntry;\r
9166f840 1436 SadEntry->Data->SpdSelector = (EFI_IPSEC_SPD_SELECTOR *)((UINT8 *)SadEntry +\r
1437 SadEntrySize -\r
1438 (UINTN)SIZE_OF_SPD_SELECTOR (SaData->SpdSelector)\r
1439 );\r
1440 DuplicateSpdSelector (\r
1441 (EFI_IPSEC_CONFIG_SELECTOR *) SadEntry->Data->SpdSelector,\r
1442 (EFI_IPSEC_CONFIG_SELECTOR *) SaData->SpdSelector,\r
1443 NULL\r
1444 );\r
a3bcde70
HT
1445 InsertTailList (&SpdEntry->Data->Sas, &SadEntry->BySpd);\r
1446 }\r
1447 }\r
1448 //\r
9166f840 1449 // Insert the new SAD entry.\r
a3bcde70
HT
1450 //\r
1451 InsertTailList (EntryInsertBefore, &SadEntry->List);\r
1452\r
1453 return EFI_SUCCESS;\r
1454}\r
1455\r
1456/**\r
1457 Set the peer authorization configuration information for the EFI IPsec driver.\r
1458\r
9166f840 1459 The IPsec configuration data has a unique selector/identifier separately to \r
a3bcde70
HT
1460 identify a data entry.\r
1461\r
9166f840 1462 @param[in] Selector Pointer to an entry selector on operated \r
1463 configuration data specified by DataType. \r
1464 A NULL Selector causes the entire specified-type \r
a3bcde70 1465 configuration information to be flushed.\r
9166f840 1466 @param[in] Data The data buffer to be set. The structure \r
a3bcde70 1467 of the data buffer should be EFI_IPSEC_PAD_DATA.\r
9166f840 1468 @param[in] Context Pointer to one entry selector that describes \r
1469 the expected position the new data entry will \r
a3bcde70
HT
1470 be added. If Context is NULL, the new entry will\r
1471 be appended the end of database.\r
1472\r
1473 @retval EFI_OUT_OF_RESOURCES The required system resources could not be allocated.\r
1474 @retval EFI_SUCCESS The specified configuration data was obtained successfully.\r
1475\r
1476**/\r
1477EFI_STATUS\r
1478SetPadEntry (\r
1479 IN EFI_IPSEC_CONFIG_SELECTOR *Selector,\r
1480 IN VOID *Data,\r
1481 IN VOID *Context OPTIONAL\r
1482 )\r
1483{\r
1484 IPSEC_PAD_ENTRY *PadEntry;\r
1485 EFI_IPSEC_PAD_ID *PadId;\r
1486 EFI_IPSEC_PAD_DATA *PadData;\r
1487 LIST_ENTRY *PadList;\r
1488 LIST_ENTRY *Entry;\r
1489 LIST_ENTRY *NextEntry;\r
1490 EFI_IPSEC_PAD_ID *InsertBefore;\r
1491 LIST_ENTRY *EntryInsertBefore;\r
1492 UINTN PadEntrySize;\r
9166f840 1493 \r
a3bcde70
HT
1494 PadId = (Selector == NULL) ? NULL : &Selector->PadId;\r
1495 PadData = (Data == NULL) ? NULL : (EFI_IPSEC_PAD_DATA *) Data;\r
1496 InsertBefore = (Context == NULL) ? NULL : &((EFI_IPSEC_CONFIG_SELECTOR *) Context)->PadId;\r
1497 PadList = &mConfigData[IPsecConfigDataTypePad];\r
1498\r
1499 //\r
1500 // The default behavior is to insert the node ahead of the header.\r
1501 //\r
1502 EntryInsertBefore = PadList;\r
1503\r
1504 //\r
1505 // Remove the existed pad entry.\r
1506 //\r
1507 NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, PadList) {\r
1508\r
1509 PadEntry = IPSEC_PAD_ENTRY_FROM_LIST (Entry);\r
1510\r
9166f840 1511 if (PadId == NULL || \r
a3bcde70
HT
1512 ComparePadId ((EFI_IPSEC_CONFIG_SELECTOR *) PadEntry->Id, (EFI_IPSEC_CONFIG_SELECTOR *) PadId)\r
1513 ) {\r
1514 //\r
1515 // Record the existed entry position to keep the original order.\r
1516 //\r
1517 EntryInsertBefore = PadEntry->List.ForwardLink;\r
1518 RemoveEntryList (&PadEntry->List);\r
1519\r
1520 FreePool (PadEntry);\r
1521 }\r
1522 }\r
1523 //\r
1524 // Return success here if only want to remove the pad entry\r
1525 //\r
1526 if (PadData == NULL || PadId == NULL) {\r
1527 return EFI_SUCCESS;\r
1528 }\r
1529 //\r
1530 // Search the appointed entry position if InsertBefore is not NULL.\r
1531 //\r
1532 if (InsertBefore != NULL) {\r
1533\r
1534 NET_LIST_FOR_EACH (Entry, PadList) {\r
1535 PadEntry = IPSEC_PAD_ENTRY_FROM_LIST (Entry);\r
1536\r
1537 if (ComparePadId (\r
1538 (EFI_IPSEC_CONFIG_SELECTOR *) PadEntry->Id,\r
1539 (EFI_IPSEC_CONFIG_SELECTOR *) InsertBefore\r
1540 )) {\r
1541 EntryInsertBefore = Entry;\r
1542 break;\r
1543 }\r
1544 }\r
1545 }\r
1546\r
1547 //\r
1548 // Do PADDING for different arch.\r
1549 //\r
1550 PadEntrySize = ALIGN_VARIABLE (sizeof (IPSEC_PAD_ENTRY));\r
1551 PadEntrySize = ALIGN_VARIABLE (PadEntrySize + sizeof (EFI_IPSEC_PAD_ID));\r
1552 PadEntrySize = ALIGN_VARIABLE (PadEntrySize + sizeof (EFI_IPSEC_PAD_DATA));\r
1553 PadEntrySize = ALIGN_VARIABLE (PadEntrySize + (PadData->AuthData != NULL ? PadData->AuthDataSize : 0));\r
1554 PadEntrySize += PadData->RevocationData != NULL ? PadData->RevocationDataSize : 0;\r
1555\r
1556 PadEntry = AllocateZeroPool (PadEntrySize);\r
1557\r
1558 if (PadEntry == NULL) {\r
1559 return EFI_OUT_OF_RESOURCES;\r
1560 }\r
1561 //\r
1562 // Fix the address of Id and Data buffer and copy them, which is\r
1563 // continous memory and close to the base structure of pad entry.\r
1564 //\r
1565 PadEntry->Id = (EFI_IPSEC_PAD_ID *) ALIGN_POINTER ((PadEntry + 1), sizeof (UINTN));\r
1566 PadEntry->Data = (EFI_IPSEC_PAD_DATA *) ALIGN_POINTER ((PadEntry->Id + 1), sizeof (UINTN));\r
1567\r
1568 CopyMem (PadEntry->Id, PadId, sizeof (EFI_IPSEC_PAD_ID));\r
1569\r
1570 PadEntry->Data->AuthProtocol = PadData->AuthProtocol;\r
1571 PadEntry->Data->AuthMethod = PadData->AuthMethod;\r
1572 PadEntry->Data->IkeIdFlag = PadData->IkeIdFlag;\r
1573\r
1574 if (PadData->AuthData != NULL) {\r
1575 PadEntry->Data->AuthDataSize = PadData->AuthDataSize;\r
1576 PadEntry->Data->AuthData = (VOID *) ALIGN_POINTER (PadEntry->Data + 1, sizeof (UINTN));\r
1577 CopyMem (\r
1578 PadEntry->Data->AuthData,\r
1579 PadData->AuthData,\r
1580 PadData->AuthDataSize\r
1581 );\r
1582 } else {\r
1583 PadEntry->Data->AuthDataSize = 0;\r
1584 PadEntry->Data->AuthData = NULL;\r
1585 }\r
1586\r
1587 if (PadData->RevocationData != NULL) {\r
1588 PadEntry->Data->RevocationDataSize = PadData->RevocationDataSize;\r
1589 PadEntry->Data->RevocationData = (VOID *) ALIGN_POINTER (\r
1590 ((UINT8 *) (PadEntry->Data + 1) + PadData->AuthDataSize),\r
1591 sizeof (UINTN)\r
1592 );\r
1593 CopyMem (\r
1594 PadEntry->Data->RevocationData,\r
1595 PadData->RevocationData,\r
1596 PadData->RevocationDataSize\r
1597 );\r
1598 } else {\r
1599 PadEntry->Data->RevocationDataSize = 0;\r
1600 PadEntry->Data->RevocationData = NULL;\r
1601 }\r
1602 //\r
1603 // Insert the new pad entry.\r
1604 //\r
1605 InsertTailList (EntryInsertBefore, &PadEntry->List);\r
1606\r
1607 return EFI_SUCCESS;\r
1608}\r
1609\r
1610/**\r
9166f840 1611 This function lookup the data entry from IPsec SPD. Return the configuration \r
a3bcde70
HT
1612 value of the specified SPD Entry.\r
1613\r
9166f840 1614 @param[in] Selector Pointer to an entry selector which is an identifier \r
a3bcde70
HT
1615 of the SPD entry.\r
1616 @param[in, out] DataSize On output the size of data returned in Data.\r
9166f840 1617 @param[out] Data The buffer to return the contents of the IPsec \r
1618 configuration data. The type of the data buffer \r
1619 is associated with the DataType. \r
1620 \r
a3bcde70
HT
1621 @retval EFI_SUCCESS The specified configuration data was obtained successfully.\r
1622 @retval EFI_INVALID_PARAMETER Data is NULL and *DataSize is not zero.\r
1623 @retval EFI_NOT_FOUND The configuration data specified by Selector is not found.\r
1624 @retval EFI_BUFFER_TOO_SMALL The DataSize is too small for the result. DataSize has been\r
1625 updated with the size needed to complete the request.\r
1626\r
1627**/\r
1628EFI_STATUS\r
1629GetSpdEntry (\r
1630 IN EFI_IPSEC_CONFIG_SELECTOR *Selector,\r
1631 IN OUT UINTN *DataSize,\r
1632 OUT VOID *Data\r
1633 )\r
1634{\r
1635 IPSEC_SPD_ENTRY *SpdEntry;\r
1636 IPSEC_SAD_ENTRY *SadEntry;\r
1637 EFI_IPSEC_SPD_SELECTOR *SpdSel;\r
1638 EFI_IPSEC_SPD_DATA *SpdData;\r
1639 LIST_ENTRY *SpdList;\r
1640 LIST_ENTRY *SpdSas;\r
1641 LIST_ENTRY *Entry;\r
1642 UINTN RequiredSize;\r
1643\r
1644 SpdSel = &Selector->SpdSelector;\r
1645 SpdData = (EFI_IPSEC_SPD_DATA *) Data;\r
1646 SpdList = &mConfigData[IPsecConfigDataTypeSpd];\r
1647\r
1648 NET_LIST_FOR_EACH (Entry, SpdList) {\r
1649 SpdEntry = IPSEC_SPD_ENTRY_FROM_LIST (Entry);\r
1650\r
1651 //\r
9166f840 1652 // Find the required SPD entry\r
a3bcde70
HT
1653 //\r
1654 if (CompareSpdSelector (\r
1655 (EFI_IPSEC_CONFIG_SELECTOR *) SpdSel,\r
1656 (EFI_IPSEC_CONFIG_SELECTOR *) SpdEntry->Selector\r
1657 )) {\r
1658\r
1659 RequiredSize = IpSecGetSizeOfSpdData (SpdEntry->Data);\r
1660 if (*DataSize < RequiredSize) {\r
1661 *DataSize = RequiredSize;\r
1662 return EFI_BUFFER_TOO_SMALL;\r
1663 }\r
1664\r
1665 if (SpdData == NULL) {\r
1666 return EFI_INVALID_PARAMETER;\r
1667 }\r
1668\r
1669 *DataSize = RequiredSize;\r
1670\r
1671 //\r
9166f840 1672 // Extract and fill all SaId array from the SPD.sas list\r
a3bcde70
HT
1673 //\r
1674 SpdSas = &SpdEntry->Data->Sas;\r
1675 SpdData->SaIdCount = 0;\r
1676\r
1677 NET_LIST_FOR_EACH (Entry, SpdSas) {\r
1678 SadEntry = IPSEC_SAD_ENTRY_FROM_SPD (Entry);\r
1679 CopyMem (\r
1680 &SpdData->SaId[SpdData->SaIdCount++],\r
1681 SadEntry->Id,\r
1682 sizeof (EFI_IPSEC_SA_ID)\r
1683 );\r
1684 }\r
1685 //\r
9166f840 1686 // Fill the other fields in SPD data.\r
a3bcde70
HT
1687 //\r
1688 CopyMem (SpdData->Name, SpdEntry->Data->Name, sizeof (SpdData->Name));\r
1689\r
1690 SpdData->PackageFlag = SpdEntry->Data->PackageFlag;\r
1691 SpdData->Action = SpdEntry->Data->Action;\r
1692\r
1693 if (SpdData->Action != EfiIPsecActionProtect) {\r
1694 SpdData->ProcessingPolicy = NULL;\r
1695 } else {\r
1696 SpdData->ProcessingPolicy = (EFI_IPSEC_PROCESS_POLICY *) ((UINT8 *) SpdData + sizeof (EFI_IPSEC_SPD_DATA) + (SpdData->SaIdCount - 1) * sizeof (EFI_IPSEC_SA_ID));\r
1697\r
1698 IpSecDuplicateProcessPolicy (\r
1699 SpdData->ProcessingPolicy,\r
1700 SpdEntry->Data->ProcessingPolicy\r
1701 );\r
1702 }\r
1703\r
1704 return EFI_SUCCESS;\r
1705 }\r
1706 }\r
1707\r
1708 return EFI_NOT_FOUND;\r
1709}\r
1710\r
1711/**\r
9166f840 1712 This function lookup the data entry from IPsec SAD. Return the configuration \r
a3bcde70
HT
1713 value of the specified SAD Entry.\r
1714\r
9166f840 1715 @param[in] Selector Pointer to an entry selector which is an identifier \r
a3bcde70
HT
1716 of the SAD entry.\r
1717 @param[in, out] DataSize On output, the size of data returned in Data.\r
9166f840 1718 @param[out] Data The buffer to return the contents of the IPsec \r
1719 configuration data. The type of the data buffer \r
1720 is associated with the DataType. \r
1721 \r
a3bcde70
HT
1722 @retval EFI_SUCCESS The specified configuration data was obtained successfully.\r
1723 @retval EFI_NOT_FOUND The configuration data specified by Selector is not found.\r
1724 @retval EFI_BUFFER_TOO_SMALL The DataSize is too small for the result. DataSize has been\r
1725 updated with the size needed to complete the request.\r
1726\r
1727**/\r
1728EFI_STATUS\r
1729GetSadEntry (\r
1730 IN EFI_IPSEC_CONFIG_SELECTOR *Selector,\r
1731 IN OUT UINTN *DataSize,\r
1732 OUT VOID *Data\r
1733 )\r
1734{\r
1735 IPSEC_SAD_ENTRY *SadEntry;\r
1736 LIST_ENTRY *Entry;\r
1737 LIST_ENTRY *SadList;\r
1738 EFI_IPSEC_SA_ID *SaId;\r
9166f840 1739 EFI_IPSEC_SA_DATA2 *SaData;\r
a3bcde70
HT
1740 UINTN RequiredSize;\r
1741\r
1742 SaId = &Selector->SaId;\r
9166f840 1743 SaData = (EFI_IPSEC_SA_DATA2 *) Data;\r
a3bcde70
HT
1744 SadList = &mConfigData[IPsecConfigDataTypeSad];\r
1745\r
1746 NET_LIST_FOR_EACH (Entry, SadList) {\r
1747 SadEntry = IPSEC_SAD_ENTRY_FROM_LIST (Entry);\r
1748\r
1749 //\r
9166f840 1750 // Find the required SAD entry.\r
a3bcde70
HT
1751 //\r
1752 if (CompareSaId (\r
1753 (EFI_IPSEC_CONFIG_SELECTOR *) SaId,\r
1754 (EFI_IPSEC_CONFIG_SELECTOR *) SadEntry->Id\r
1755 )) {\r
1756 //\r
9166f840 1757 // Calculate the required size of the SAD entry.\r
a3bcde70
HT
1758 // Data Layout is follows:\r
1759 // |EFI_IPSEC_SA_DATA\r
1760 // |AuthKey\r
1761 // |EncryptKey (Optional)\r
9166f840 1762 // |SpdSelector (Optional) \r
1763 // \r
1764 RequiredSize = ALIGN_VARIABLE (sizeof (EFI_IPSEC_SA_DATA2));\r
a3bcde70
HT
1765\r
1766 if (SaId->Proto == EfiIPsecAH) {\r
1767 RequiredSize = ALIGN_VARIABLE (RequiredSize + SadEntry->Data->AlgoInfo.AhAlgoInfo.AuthKeyLength);\r
1768 } else {\r
1769 RequiredSize = ALIGN_VARIABLE (RequiredSize + SadEntry->Data->AlgoInfo.EspAlgoInfo.AuthKeyLength);\r
1770 RequiredSize = ALIGN_VARIABLE (RequiredSize + SadEntry->Data->AlgoInfo.EspAlgoInfo.EncKeyLength);\r
1771 }\r
1772\r
9166f840 1773 if (SadEntry->Data->SpdSelector != NULL) {\r
1774 RequiredSize += SIZE_OF_SPD_SELECTOR (SadEntry->Data->SpdSelector);\r
a3bcde70 1775 }\r
9166f840 1776 \r
a3bcde70
HT
1777 if (*DataSize < RequiredSize) {\r
1778 *DataSize = RequiredSize;\r
1779 return EFI_BUFFER_TOO_SMALL;\r
1780 }\r
9166f840 1781 \r
a3bcde70 1782 //\r
9166f840 1783 // Fill the data fields of SAD entry.\r
a3bcde70
HT
1784 //\r
1785 *DataSize = RequiredSize;\r
1786 SaData->Mode = SadEntry->Data->Mode;\r
1787 SaData->SNCount = SadEntry->Data->SequenceNumber;\r
1788 SaData->AntiReplayWindows = SadEntry->Data->AntiReplayWindowSize;\r
1789\r
1790 CopyMem (\r
1791 &SaData->SaLifetime,\r
1792 &SadEntry->Data->SaLifetime,\r
1793 sizeof (EFI_IPSEC_SA_LIFETIME)\r
1794 );\r
1795\r
1796 ZeroMem (\r
1797 &SaData->AlgoInfo,\r
1798 sizeof (EFI_IPSEC_ALGO_INFO)\r
1799 );\r
1800\r
1801 if (SaId->Proto == EfiIPsecAH) {\r
1802 //\r
1803 // Copy AH alogrithm INFO to SaData\r
1804 //\r
1805 SaData->AlgoInfo.AhAlgoInfo.AuthAlgoId = SadEntry->Data->AlgoInfo.AhAlgoInfo.AuthAlgoId;\r
1806 SaData->AlgoInfo.AhAlgoInfo.AuthKeyLength = SadEntry->Data->AlgoInfo.AhAlgoInfo.AuthKeyLength;\r
1807 if (SaData->AlgoInfo.AhAlgoInfo.AuthKeyLength != 0) {\r
1808 SaData->AlgoInfo.AhAlgoInfo.AuthKey = (VOID *) ALIGN_POINTER ((SaData + 1), sizeof (UINTN));\r
1809 CopyMem (\r
1810 SaData->AlgoInfo.AhAlgoInfo.AuthKey,\r
1811 SadEntry->Data->AlgoInfo.AhAlgoInfo.AuthKey,\r
1812 SaData->AlgoInfo.AhAlgoInfo.AuthKeyLength\r
1813 );\r
1814 }\r
1815 } else if (SaId->Proto == EfiIPsecESP) {\r
1816 //\r
1817 // Copy ESP alogrithem INFO to SaData\r
1818 //\r
1819 SaData->AlgoInfo.EspAlgoInfo.AuthAlgoId = SadEntry->Data->AlgoInfo.EspAlgoInfo.AuthAlgoId;\r
1820 SaData->AlgoInfo.EspAlgoInfo.AuthKeyLength = SadEntry->Data->AlgoInfo.EspAlgoInfo.AuthKeyLength;\r
1821 if (SaData->AlgoInfo.EspAlgoInfo.AuthKeyLength != 0) {\r
1822 SaData->AlgoInfo.EspAlgoInfo.AuthKey = (VOID *) ALIGN_POINTER ((SaData + 1), sizeof (UINTN));\r
1823 CopyMem (\r
1824 SaData->AlgoInfo.EspAlgoInfo.AuthKey,\r
1825 SadEntry->Data->AlgoInfo.EspAlgoInfo.AuthKey,\r
1826 SaData->AlgoInfo.EspAlgoInfo.AuthKeyLength\r
1827 );\r
1828 }\r
1829\r
1830 SaData->AlgoInfo.EspAlgoInfo.EncAlgoId = SadEntry->Data->AlgoInfo.EspAlgoInfo.EncAlgoId;\r
1831 SaData->AlgoInfo.EspAlgoInfo.EncKeyLength = SadEntry->Data->AlgoInfo.EspAlgoInfo.EncKeyLength;\r
1832\r
1833 if (SaData->AlgoInfo.EspAlgoInfo.EncKeyLength != 0) {\r
1834 SaData->AlgoInfo.EspAlgoInfo.EncKey = (VOID *) ALIGN_POINTER (\r
1835 ((UINT8 *) (SaData + 1) +\r
1836 SaData->AlgoInfo.EspAlgoInfo.AuthKeyLength),\r
1837 sizeof (UINTN)\r
1838 );\r
1839 CopyMem (\r
1840 SaData->AlgoInfo.EspAlgoInfo.EncKey,\r
1841 SadEntry->Data->AlgoInfo.EspAlgoInfo.EncKey,\r
1842 SaData->AlgoInfo.EspAlgoInfo.EncKeyLength\r
1843 );\r
1844 }\r
1845 }\r
1846\r
1847 SaData->PathMTU = SadEntry->Data->PathMTU;\r
1848\r
1849 //\r
9166f840 1850 // Fill Tunnel Address if it is Tunnel Mode\r
a3bcde70 1851 //\r
9166f840 1852 if (SadEntry->Data->Mode == EfiIPsecTunnel) {\r
1853 CopyMem (\r
1854 &SaData->TunnelDestinationAddress,\r
1855 &SadEntry->Data->TunnelDestAddress,\r
1856 sizeof (EFI_IP_ADDRESS)\r
1857 );\r
1858 CopyMem (\r
1859 &SaData->TunnelSourceAddress,\r
1860 &SadEntry->Data->TunnelSourceAddress,\r
1861 sizeof (EFI_IP_ADDRESS)\r
1862 );\r
1863 }\r
1864 //\r
1865 // Fill the spd selector field of SAD data\r
1866 //\r
1867 if (SadEntry->Data->SpdSelector != NULL) {\r
a3bcde70
HT
1868\r
1869 SaData->SpdSelector = (EFI_IPSEC_SPD_SELECTOR *) (\r
1870 (UINT8 *)SaData +\r
1871 RequiredSize -\r
9166f840 1872 SIZE_OF_SPD_SELECTOR (SadEntry->Data->SpdSelector)\r
a3bcde70 1873 );\r
9166f840 1874 \r
a3bcde70
HT
1875 DuplicateSpdSelector (\r
1876 (EFI_IPSEC_CONFIG_SELECTOR *) SaData->SpdSelector,\r
9166f840 1877 (EFI_IPSEC_CONFIG_SELECTOR *) SadEntry->Data->SpdSelector,\r
a3bcde70
HT
1878 NULL\r
1879 );\r
1880\r
1881 } else {\r
1882\r
1883 SaData->SpdSelector = NULL;\r
1884 }\r
1885\r
1886 SaData->ManualSet = SadEntry->Data->ManualSet;\r
1887\r
1888 return EFI_SUCCESS;\r
1889 }\r
1890 }\r
1891\r
1892 return EFI_NOT_FOUND;\r
1893}\r
1894\r
1895/**\r
9166f840 1896 This function lookup the data entry from IPsec PAD. Return the configuration \r
a3bcde70
HT
1897 value of the specified PAD Entry.\r
1898\r
9166f840 1899 @param[in] Selector Pointer to an entry selector which is an identifier \r
a3bcde70
HT
1900 of the PAD entry.\r
1901 @param[in, out] DataSize On output the size of data returned in Data.\r
9166f840 1902 @param[out] Data The buffer to return the contents of the IPsec \r
1903 configuration data. The type of the data buffer \r
1904 is associated with the DataType. \r
1905 \r
a3bcde70
HT
1906 @retval EFI_SUCCESS The specified configuration data was obtained successfully.\r
1907 @retval EFI_NOT_FOUND The configuration data specified by Selector is not found.\r
1908 @retval EFI_BUFFER_TOO_SMALL The DataSize is too small for the result. DataSize has been\r
1909 updated with the size needed to complete the request.\r
1910\r
1911**/\r
1912EFI_STATUS\r
1913GetPadEntry (\r
1914 IN EFI_IPSEC_CONFIG_SELECTOR *Selector,\r
1915 IN OUT UINTN *DataSize,\r
1916 OUT VOID *Data\r
1917 )\r
1918{\r
1919 IPSEC_PAD_ENTRY *PadEntry;\r
1920 LIST_ENTRY *PadList;\r
1921 LIST_ENTRY *Entry;\r
1922 EFI_IPSEC_PAD_ID *PadId;\r
1923 EFI_IPSEC_PAD_DATA *PadData;\r
1924 UINTN RequiredSize;\r
1925\r
1926 PadId = &Selector->PadId;\r
1927 PadData = (EFI_IPSEC_PAD_DATA *) Data;\r
1928 PadList = &mConfigData[IPsecConfigDataTypePad];\r
1929\r
1930 NET_LIST_FOR_EACH (Entry, PadList) {\r
1931 PadEntry = IPSEC_PAD_ENTRY_FROM_LIST (Entry);\r
1932\r
1933 //\r
1934 // Find the required pad entry.\r
1935 //\r
1936 if (ComparePadId (\r
1937 (EFI_IPSEC_CONFIG_SELECTOR *) PadId,\r
1938 (EFI_IPSEC_CONFIG_SELECTOR *) PadEntry->Id\r
1939 )) {\r
1940 //\r
1941 // Calculate the required size of the pad entry.\r
1942 //\r
1943 RequiredSize = ALIGN_VARIABLE (sizeof (EFI_IPSEC_PAD_DATA));\r
1944 RequiredSize = ALIGN_VARIABLE (RequiredSize + PadEntry->Data->AuthDataSize);\r
1945 RequiredSize += PadEntry->Data->RevocationDataSize;\r
1946\r
1947 if (*DataSize < RequiredSize) {\r
1948 *DataSize = RequiredSize;\r
1949 return EFI_BUFFER_TOO_SMALL;\r
1950 }\r
1951 //\r
1952 // Fill the data fields of pad entry\r
1953 //\r
1954 *DataSize = RequiredSize;\r
1955 PadData->AuthProtocol = PadEntry->Data->AuthProtocol;\r
1956 PadData->AuthMethod = PadEntry->Data->AuthMethod;\r
1957 PadData->IkeIdFlag = PadEntry->Data->IkeIdFlag;\r
1958\r
1959 //\r
1960 // Copy Authentication data.\r
1961 //\r
1962 if (PadEntry->Data->AuthData != NULL) {\r
1963\r
1964 PadData->AuthDataSize = PadEntry->Data->AuthDataSize;\r
1965 PadData->AuthData = (VOID *) ALIGN_POINTER ((PadData + 1), sizeof (UINTN));\r
1966 CopyMem (\r
1967 PadData->AuthData,\r
1968 PadEntry->Data->AuthData,\r
1969 PadData->AuthDataSize\r
1970 );\r
1971 } else {\r
1972\r
1973 PadData->AuthDataSize = 0;\r
1974 PadData->AuthData = NULL;\r
1975 }\r
1976 //\r
1977 // Copy Revocation Data.\r
1978 //\r
1979 if (PadEntry->Data->RevocationData != NULL) {\r
1980\r
1981 PadData->RevocationDataSize = PadEntry->Data->RevocationDataSize;\r
1982 PadData->RevocationData = (VOID *) ALIGN_POINTER (\r
1983 ((UINT8 *) (PadData + 1) + PadData->AuthDataSize),\r
1984 sizeof (UINTN)\r
1985 );\r
1986 CopyMem (\r
1987 PadData->RevocationData,\r
1988 PadEntry->Data->RevocationData,\r
1989 PadData->RevocationDataSize\r
1990 );\r
1991 } else {\r
1992\r
1993 PadData->RevocationDataSize = 0;\r
1994 PadData->RevocationData = NULL;\r
1995 }\r
1996\r
1997 return EFI_SUCCESS;\r
1998 }\r
1999 }\r
2000\r
2001 return EFI_NOT_FOUND;\r
2002}\r
2003\r
2004/**\r
2005 Copy Source Process Policy to the Destination Process Policy.\r
2006\r
2007 @param[in] Dst Pointer to the Source Process Policy.\r
2008 @param[in] Src Pointer to the Destination Process Policy.\r
2009\r
2010**/\r
2011VOID\r
2012IpSecDuplicateProcessPolicy (\r
2013 IN EFI_IPSEC_PROCESS_POLICY *Dst,\r
2014 IN EFI_IPSEC_PROCESS_POLICY *Src\r
2015 )\r
2016{\r
2017 //\r
2018 // Firstly copy the structure content itself.\r
2019 //\r
2020 CopyMem (Dst, Src, sizeof (EFI_IPSEC_PROCESS_POLICY));\r
2021\r
2022 //\r
2023 // Recursively copy the tunnel option if needed.\r
2024 //\r
2025 if (Dst->Mode != EfiIPsecTunnel) {\r
2026 ASSERT (Dst->TunnelOption == NULL);\r
2027 } else {\r
2028 Dst->TunnelOption = (EFI_IPSEC_TUNNEL_OPTION *) ALIGN_POINTER ((Dst + 1), sizeof (UINTN));\r
2029 CopyMem (\r
2030 Dst->TunnelOption,\r
2031 Src->TunnelOption,\r
2032 sizeof (EFI_IPSEC_TUNNEL_OPTION)\r
2033 );\r
2034 }\r
2035}\r
2036\r
2037/**\r
2038 Calculate the a whole size of EFI_IPSEC_SPD_DATA, which includes the buffer size pointed\r
2039 to by the pointer members.\r
2040\r
2041 @param[in] SpdData Pointer to a specified EFI_IPSEC_SPD_DATA.\r
2042\r
2043 @return the whole size the specified EFI_IPSEC_SPD_DATA.\r
2044\r
2045**/\r
2046UINTN\r
2047IpSecGetSizeOfEfiSpdData (\r
2048 IN EFI_IPSEC_SPD_DATA *SpdData\r
2049 )\r
2050{\r
2051 UINTN Size;\r
2052\r
2053 Size = ALIGN_VARIABLE (sizeof (IPSEC_SPD_DATA));\r
2054\r
2055 if (SpdData->Action == EfiIPsecActionProtect) {\r
2056 Size = ALIGN_VARIABLE (Size + sizeof (EFI_IPSEC_PROCESS_POLICY));\r
2057\r
2058 if (SpdData->ProcessingPolicy->Mode == EfiIPsecTunnel) {\r
2059 Size = ALIGN_VARIABLE (Size + sizeof (EFI_IPSEC_TUNNEL_OPTION));\r
2060 }\r
2061 }\r
2062\r
2063 return Size;\r
2064}\r
2065\r
2066/**\r
2067 Calculate the a whole size of IPSEC_SPD_DATA which includes the buffer size pointed\r
9166f840 2068 to by the pointer members and the buffer size used by the Sa List. \r
a3bcde70
HT
2069\r
2070 @param[in] SpdData Pointer to the specified IPSEC_SPD_DATA.\r
2071\r
2072 @return the whole size of IPSEC_SPD_DATA.\r
2073\r
2074**/\r
2075UINTN\r
2076IpSecGetSizeOfSpdData (\r
2077 IN IPSEC_SPD_DATA *SpdData\r
2078 )\r
2079{\r
2080 UINTN Size;\r
2081 LIST_ENTRY *Link;\r
2082\r
2083 Size = sizeof (EFI_IPSEC_SPD_DATA) - sizeof (EFI_IPSEC_SA_ID);\r
2084\r
2085 if (SpdData->Action == EfiIPsecActionProtect) {\r
2086 Size += sizeof (EFI_IPSEC_PROCESS_POLICY);\r
2087\r
2088 if (SpdData->ProcessingPolicy->Mode == EfiIPsecTunnel) {\r
2089 Size += sizeof (EFI_IPSEC_TUNNEL_OPTION);\r
2090 }\r
2091 }\r
2092\r
2093 NET_LIST_FOR_EACH (Link, &SpdData->Sas) {\r
2094 Size += sizeof (EFI_IPSEC_SA_ID);\r
2095 }\r
2096\r
2097 return Size;\r
2098}\r
2099\r
2100/**\r
2101 Get the IPsec Variable.\r
2102\r
2103 Get the all variables which start with the string contained in VaraiableName.\r
9166f840 2104 Since all IPsec related variable store in continual space, those kinds of \r
2105 variable can be searched by the EfiGetNextVariableName. Those variables also are \r
a3bcde70 2106 returned in a continual buffer.\r
9166f840 2107 \r
a3bcde70
HT
2108 @param[in] VariableName Pointer to a specified Variable Name.\r
2109 @param[in] VendorGuid Pointer to a specified Vendor Guid.\r
9166f840 2110 @param[in] Attributes Point to memory location to return the attributes \r
2111 of variable. If the point is NULL, the parameter \r
a3bcde70 2112 would be ignored.\r
9166f840 2113 @param[in, out] DataSize As input, point to the maximum size of return \r
2114 Data-Buffer. As output, point to the actual \r
a3bcde70
HT
2115 size of the returned Data-Buffer.\r
2116 @param[in] Data Point to return Data-Buffer.\r
9166f840 2117 \r
a3bcde70 2118 @retval EFI_ABORTED If the Variable size which contained in the variable\r
9166f840 2119 structure doesn't match the variable size obtained \r
a3bcde70
HT
2120 from the EFIGetVariable.\r
2121 @retval EFI_BUFFER_TOO_SMALL The DataSize is too small for the result. DataSize has\r
9166f840 2122 been updated with the size needed to complete the request. \r
a3bcde70
HT
2123 @retval EFI_SUCCESS The function completed successfully.\r
2124 @retval others Other errors found during the variable getting.\r
2125**/\r
2126EFI_STATUS\r
2127IpSecGetVariable (\r
2128 IN CHAR16 *VariableName,\r
2129 IN EFI_GUID *VendorGuid,\r
2130 IN UINT32 *Attributes, OPTIONAL\r
2131 IN OUT UINTN *DataSize,\r
2132 IN VOID *Data\r
2133 )\r
2134{\r
2135 EFI_STATUS Status;\r
2136 EFI_GUID VendorGuidI;\r
2137 UINTN VariableNameLength;\r
2138 CHAR16 *VariableNameI;\r
2139 UINTN VariableNameISize;\r
2140 UINTN VariableNameISizeNew;\r
2141 UINTN VariableIndex;\r
2142 UINTN VariableCount;\r
2143 IP_SEC_VARIABLE_INFO IpSecVariableInfo;\r
2144 UINTN DataSizeI;\r
2145\r
2146 //\r
2147 // The variable name constructor is "VariableName + Info/0001/0002/... + NULL".\r
2148 // So the varialbe name is like "VariableNameInfo", "VariableName0001", ...\r
2149 // "VariableNameNULL".\r
2150 //\r
2151 VariableNameLength = StrLen (VariableName);\r
2152 VariableNameISize = (VariableNameLength + 5) * sizeof (CHAR16);\r
2153 VariableNameI = AllocateZeroPool (VariableNameISize);\r
2154 ASSERT (VariableNameI != NULL);\r
9166f840 2155 \r
a3bcde70
HT
2156 //\r
2157 // Construct the varible name of ipsecconfig meta data.\r
2158 //\r
2159 UnicodeSPrint (VariableNameI, VariableNameISize, L"%s%s", VariableName, L"Info");\r
2160\r
2161 DataSizeI = sizeof (IpSecVariableInfo);\r
2162\r
2163 Status = gRT->GetVariable (\r
2164 VariableNameI,\r
2165 VendorGuid,\r
2166 Attributes,\r
2167 &DataSizeI,\r
2168 &IpSecVariableInfo\r
2169 );\r
2170 if (EFI_ERROR (Status)) {\r
2171 goto ON_EXIT;\r
2172 }\r
2173\r
2174 if (*DataSize < IpSecVariableInfo.VariableSize) {\r
2175 *DataSize = IpSecVariableInfo.VariableSize;\r
2176 Status = EFI_BUFFER_TOO_SMALL;\r
2177 goto ON_EXIT;\r
2178 }\r
2179\r
2180 VariableCount = IpSecVariableInfo.VariableCount;\r
2181 VariableNameI[0] = L'\0';\r
2182\r
2183 while (VariableCount != 0) {\r
2184 //\r
2185 // Get the variable name one by one in the variable database.\r
2186 //\r
2187 VariableNameISizeNew = VariableNameISize;\r
2188 Status = gRT->GetNextVariableName (\r
2189 &VariableNameISizeNew,\r
2190 VariableNameI,\r
2191 &VendorGuidI\r
2192 );\r
2193 if (Status == EFI_BUFFER_TOO_SMALL) {\r
2194 VariableNameI = ReallocatePool (\r
2195 VariableNameISize,\r
2196 VariableNameISizeNew,\r
2197 VariableNameI\r
2198 );\r
2199 VariableNameISize = VariableNameISizeNew;\r
2200\r
2201 Status = gRT->GetNextVariableName (\r
2202 &VariableNameISizeNew,\r
2203 VariableNameI,\r
2204 &VendorGuidI\r
2205 );\r
2206 }\r
2207\r
2208 if (EFI_ERROR (Status)) {\r
2209 break;\r
2210 }\r
2211 //\r
2212 // Check whether the current variable is the required "ipsecconfig".\r
2213 //\r
2214 if (StrnCmp (VariableNameI, VariableName, VariableNameLength) == 0 ||\r
2215 CompareGuid (VendorGuid, &VendorGuidI)\r
2216 ) {\r
2217 //\r
2218 // Parse the variable count of the current ipsecconfig data.\r
2219 //\r
2220 VariableIndex = StrDecimalToUintn (VariableNameI + VariableNameLength);\r
2221 if (VariableIndex!= 0 && VariableIndex <= IpSecVariableInfo.VariableCount) {\r
2222 //\r
2223 // Get the variable size of the current ipsecconfig data.\r
2224 //\r
2225 DataSizeI = 0;\r
2226 Status = gRT->GetVariable (\r
2227 VariableNameI,\r
2228 VendorGuid,\r
2229 Attributes,\r
2230 &DataSizeI,\r
2231 NULL\r
2232 );\r
2233 ASSERT (Status == EFI_BUFFER_TOO_SMALL);\r
2234 //\r
2235 // Validate the variable count and variable size.\r
2236 //\r
2237 if (VariableIndex != IpSecVariableInfo.VariableCount) {\r
2238 //\r
2239 // If the varaibe is not the last one, its size should be the max\r
2240 // size of the single variable.\r
2241 //\r
2242 if (DataSizeI != IpSecVariableInfo.SingleVariableSize) {\r
2243 return EFI_ABORTED;\r
2244 }\r
2245 } else {\r
2246 if (DataSizeI != IpSecVariableInfo.VariableSize % IpSecVariableInfo.SingleVariableSize) {\r
2247 return EFI_ABORTED;\r
2248 }\r
2249 }\r
2250 //\r
2251 // Get the variable data of the current ipsecconfig data and\r
2252 // store it into user buffer continously.\r
2253 //\r
2254 Status = gRT->GetVariable (\r
2255 VariableNameI,\r
2256 VendorGuid,\r
2257 Attributes,\r
2258 &DataSizeI,\r
2259 (UINT8 *) Data + (VariableIndex - 1) * IpSecVariableInfo.SingleVariableSize\r
2260 );\r
2261 ASSERT_EFI_ERROR (Status);\r
2262 VariableCount--;\r
2263 }\r
2264 }\r
2265 }\r
2266 //\r
2267 // The VariableCount in "VariableNameInfo" varaible should have the correct\r
2268 // numbers of variables which name starts with VariableName.\r
2269 //\r
2270 if (VariableCount != 0) {\r
2271 Status = EFI_ABORTED;\r
2272 }\r
2273\r
2274ON_EXIT:\r
2275 FreePool (VariableNameI);\r
2276 return Status;\r
2277}\r
2278\r
2279/**\r
2280 Set the IPsec variables.\r
2281\r
2282 Set all IPsec variables which start with the specified variable name. Those variables\r
2283 are set one by one.\r
2284\r
2285 @param[in] VariableName The name of the vendor's variable. It is a\r
2286 Null-Terminated Unicode String.\r
2287 @param[in] VendorGuid Unify identifier for vendor.\r
9166f840 2288 @param[in] Attributes Point to memory location to return the attributes of \r
a3bcde70
HT
2289 variable. If the point is NULL, the parameter would be ignored.\r
2290 @param[in] DataSize The size in bytes of Data-Buffer.\r
2291 @param[in] Data Points to the content of the variable.\r
2292\r
2293 @retval EFI_SUCCESS The firmware successfully stored the variable and its data, as\r
2294 defined by the Attributes.\r
9166f840 2295 @retval others Storing the variables failed. \r
a3bcde70
HT
2296\r
2297**/\r
2298EFI_STATUS\r
2299IpSecSetVariable (\r
2300 IN CHAR16 *VariableName,\r
2301 IN EFI_GUID *VendorGuid,\r
2302 IN UINT32 Attributes,\r
2303 IN UINTN DataSize,\r
2304 IN VOID *Data\r
2305 )\r
2306{\r
2307 EFI_STATUS Status;\r
2308 CHAR16 *VariableNameI;\r
2309 UINTN VariableNameSize;\r
2310 UINTN VariableIndex;\r
2311 IP_SEC_VARIABLE_INFO IpSecVariableInfo;\r
2312 UINT64 MaximumVariableStorageSize;\r
2313 UINT64 RemainingVariableStorageSize;\r
2314 UINT64 MaximumVariableSize;\r
2315\r
2316 Status = gRT->QueryVariableInfo (\r
2317 Attributes,\r
2318 &MaximumVariableStorageSize,\r
2319 &RemainingVariableStorageSize,\r
2320 &MaximumVariableSize\r
2321 );\r
2322 if (EFI_ERROR (Status)) {\r
2323 return Status;\r
2324 }\r
9166f840 2325 \r
a3bcde70
HT
2326 //\r
2327 // "VariableName + Info/0001/0002/... + NULL"\r
2328 //\r
2329 VariableNameSize = (StrLen (VariableName) + 5) * sizeof (CHAR16);\r
2330 VariableNameI = AllocateZeroPool (VariableNameSize);\r
2331\r
2332 if (VariableNameI == NULL) {\r
2333 Status = EFI_OUT_OF_RESOURCES;\r
2334 goto ON_EXIT;\r
2335 }\r
2336 //\r
2337 // Construct the variable of ipsecconfig general information. Like the total\r
2338 // numbers of the Ipsecconfig variables, the total size of all ipsecconfig variables.\r
2339 //\r
2340 UnicodeSPrint (VariableNameI, VariableNameSize, L"%s%s", VariableName, L"Info");\r
2341 MaximumVariableSize -= VariableNameSize;\r
9166f840 2342 \r
a3bcde70
HT
2343 IpSecVariableInfo.VariableCount = (UINT32) ((DataSize + (UINTN) MaximumVariableSize - 1) / (UINTN) MaximumVariableSize);\r
2344 IpSecVariableInfo.VariableSize = (UINT32) DataSize;\r
2345 IpSecVariableInfo.SingleVariableSize = (UINT32) MaximumVariableSize;\r
2346\r
2347 //\r
2348 // Set the variable of ipsecconfig general information.\r
2349 //\r
2350 Status = gRT->SetVariable (\r
2351 VariableNameI,\r
2352 VendorGuid,\r
2353 Attributes,\r
2354 sizeof (IpSecVariableInfo),\r
2355 &IpSecVariableInfo\r
2356 );\r
2357 if (EFI_ERROR (Status)) {\r
2358 DEBUG ((DEBUG_ERROR, "Error set ipsecconfig meta data with %r\n", Status));\r
2359 goto ON_EXIT;\r
2360 }\r
2361\r
2362 for (VariableIndex = 0; VariableIndex < IpSecVariableInfo.VariableCount; VariableIndex++) {\r
2363 //\r
2364 // Construct and set the variable of ipsecconfig data one by one.\r
2365 // The index of variable name begin from 0001, and the varaible name\r
2366 // likes "VariableName0001", "VaraiableName0002"....\r
9166f840 2367 // \r
a3bcde70
HT
2368 UnicodeSPrint (VariableNameI, VariableNameSize, L"%s%04d", VariableName, VariableIndex + 1);\r
2369 Status = gRT->SetVariable (\r
2370 VariableNameI,\r
2371 VendorGuid,\r
2372 Attributes,\r
2373 (VariableIndex == IpSecVariableInfo.VariableCount - 1) ?\r
2374 (DataSize % (UINTN) MaximumVariableSize) :\r
2375 (UINTN) MaximumVariableSize,\r
2376 (UINT8 *) Data + VariableIndex * (UINTN) MaximumVariableSize\r
2377 );\r
2378\r
2379 if (EFI_ERROR (Status)) {\r
2380 DEBUG ((DEBUG_ERROR, "Error set ipsecconfig variable data with %r\n", Status));\r
2381 goto ON_EXIT;\r
2382 }\r
2383 }\r
2384\r
2385ON_EXIT:\r
2386 if (VariableNameI != NULL) {\r
2387 FreePool (VariableNameI);\r
2388 }\r
2389\r
2390 return Status;\r
2391}\r
2392\r
2393/**\r
9166f840 2394 Return the configuration value for the EFI IPsec driver. \r
a3bcde70
HT
2395\r
2396 This function lookup the data entry from IPsec database or IKEv2 configuration\r
2397 information. The expected data type and unique identification are described in\r
9166f840 2398 DataType and Selector parameters. \r
a3bcde70
HT
2399\r
2400 @param[in] This Pointer to the EFI_IPSEC_CONFIG_PROTOCOL instance.\r
2401 @param[in] DataType The type of data to retrieve.\r
9166f840 2402 @param[in] Selector Pointer to an entry selector that is an identifier of the IPsec \r
a3bcde70
HT
2403 configuration data entry.\r
2404 @param[in, out] DataSize On output the size of data returned in Data.\r
9166f840 2405 @param[out] Data The buffer to return the contents of the IPsec configuration data. \r
2406 The type of the data buffer associated with the DataType. \r
2407 \r
a3bcde70
HT
2408 @retval EFI_SUCCESS The specified configuration data was obtained successfully.\r
2409 @retval EFI_INVALID_PARAMETER One or more of the followings are TRUE:\r
2410 - This is NULL.\r
2411 - Selector is NULL.\r
2412 - DataSize is NULL.\r
2413 - Data is NULL and *DataSize is not zero\r
2414 @retval EFI_NOT_FOUND The configuration data specified by Selector is not found.\r
2415 @retval EFI_UNSUPPORTED The specified DataType is not supported.\r
2416 @retval EFI_BUFFER_TOO_SMALL The DataSize is too small for the result. DataSize has been\r
2417 updated with the size needed to complete the request.\r
2418\r
2419**/\r
2420EFI_STATUS\r
2421EFIAPI\r
2422EfiIpSecConfigGetData (\r
2423 IN EFI_IPSEC_CONFIG_PROTOCOL *This,\r
2424 IN EFI_IPSEC_CONFIG_DATA_TYPE DataType,\r
2425 IN EFI_IPSEC_CONFIG_SELECTOR *Selector,\r
2426 IN OUT UINTN *DataSize,\r
2427 OUT VOID *Data\r
2428 )\r
2429{\r
2430 if (This == NULL || Selector == NULL || DataSize == NULL) {\r
2431 return EFI_INVALID_PARAMETER;\r
2432 }\r
2433\r
2434 if (*DataSize != 0 && Data == NULL) {\r
2435 return EFI_INVALID_PARAMETER;\r
2436 }\r
2437\r
2438 if (DataType >= IPsecConfigDataTypeMaximum) {\r
2439 return EFI_UNSUPPORTED;\r
2440 }\r
2441\r
2442 return mGetPolicyEntry[DataType](Selector, DataSize, Data);\r
2443}\r
2444\r
2445/**\r
2446 Set the security association, security policy and peer authorization configuration\r
9166f840 2447 information for the EFI IPsec driver. \r
a3bcde70
HT
2448\r
2449 This function is used to set the IPsec configuration information of type DataType for\r
2450 the EFI IPsec driver.\r
2451 The IPsec configuration data has a unique selector/identifier separately to identify\r
2452 a data entry. The selector structure depends on DataType's definition.\r
2453 Using SetData() with a Data of NULL causes the IPsec configuration data entry identified\r
9166f840 2454 by DataType and Selector to be deleted. \r
a3bcde70
HT
2455\r
2456 @param[in] This Pointer to the EFI_IPSEC_CONFIG_PROTOCOL instance.\r
2457 @param[in] DataType The type of data to be set.\r
9166f840 2458 @param[in] Selector Pointer to an entry selector on operated configuration data \r
2459 specified by DataType. A NULL Selector causes the entire \r
a3bcde70 2460 specified-type configuration information to be flushed.\r
9166f840 2461 @param[in] Data The data buffer to be set. The structure of the data buffer is \r
a3bcde70
HT
2462 associated with the DataType.\r
2463 @param[in] InsertBefore Pointer to one entry selector which describes the expected\r
2464 position the new data entry will be added. If InsertBefore is NULL,\r
2465 the new entry will be appended to the end of the database.\r
9166f840 2466 \r
a3bcde70
HT
2467 @retval EFI_SUCCESS The specified configuration entry data was set successfully.\r
2468 @retval EFI_INVALID_PARAMETER One or more of the following are TRUE:\r
2469 - This is NULL.\r
2470 @retval EFI_UNSUPPORTED The specified DataType is not supported.\r
2471 @retval EFI_OUT_OF_RESOURCED The required system resource could not be allocated.\r
2472\r
2473**/\r
2474EFI_STATUS\r
2475EFIAPI\r
2476EfiIpSecConfigSetData (\r
2477 IN EFI_IPSEC_CONFIG_PROTOCOL *This,\r
2478 IN EFI_IPSEC_CONFIG_DATA_TYPE DataType,\r
2479 IN EFI_IPSEC_CONFIG_SELECTOR *Selector,\r
2480 IN VOID *Data,\r
2481 IN EFI_IPSEC_CONFIG_SELECTOR *InsertBefore OPTIONAL\r
2482 )\r
2483{\r
2484 EFI_STATUS Status;\r
2485\r
2486 if (This == NULL) {\r
2487 return EFI_INVALID_PARAMETER;\r
2488 }\r
2489\r
2490 if (DataType >= IPsecConfigDataTypeMaximum) {\r
2491 return EFI_UNSUPPORTED;\r
2492 }\r
9166f840 2493 \r
a3bcde70
HT
2494 Status = mSetPolicyEntry[DataType](Selector, Data, InsertBefore);\r
2495\r
2496 if (!EFI_ERROR (Status) && !mSetBySelf) {\r
2497 //\r
2498 // Save the updated config data into variable.\r
2499 //\r
2500 IpSecConfigSave ();\r
2501 }\r
2502\r
2503 return Status;\r
2504}\r
2505\r
2506/**\r
9166f840 2507 Enumerates the current selector for IPsec configuration data entry. \r
a3bcde70
HT
2508\r
2509 This function is called multiple times to retrieve the entry Selector in IPsec\r
9166f840 2510 configuration database. On each call to GetNextSelector(), the next entry \r
a3bcde70 2511 Selector are retrieved into the output interface.\r
9166f840 2512 \r
2513 If the entire IPsec configuration database has been iterated, the error \r
a3bcde70 2514 EFI_NOT_FOUND is returned.\r
9166f840 2515 If the Selector buffer is too small for the next Selector copy, an \r
2516 EFI_BUFFER_TOO_SMALL error is returned, and SelectorSize is updated to reflect \r
a3bcde70
HT
2517 the size of buffer needed.\r
2518\r
2519 On the initial call to GetNextSelector() to start the IPsec configuration database\r
9166f840 2520 search, a pointer to the buffer with all zero value is passed in Selector. Calls \r
2521 to SetData() between calls to GetNextSelector may produce unpredictable results. \r
a3bcde70
HT
2522\r
2523 @param[in] This Pointer to the EFI_IPSEC_CONFIG_PROTOCOL instance.\r
2524 @param[in] DataType The type of IPsec configuration data to retrieve.\r
2525 @param[in, out] SelectorSize The size of the Selector buffer.\r
9166f840 2526 @param[in, out] Selector On input, supplies the pointer to last Selector that was \r
a3bcde70
HT
2527 returned by GetNextSelector().\r
2528 On output, returns one copy of the current entry Selector\r
9166f840 2529 of a given DataType. \r
2530 \r
a3bcde70
HT
2531 @retval EFI_SUCCESS The specified configuration data was obtained successfully.\r
2532 @retval EFI_INVALID_PARAMETER One or more of the followings are TRUE:\r
2533 - This is NULL.\r
2534 - SelectorSize is NULL.\r
2535 - Selector is NULL.\r
2536 @retval EFI_NOT_FOUND The next configuration data entry was not found.\r
2537 @retval EFI_UNSUPPORTED The specified DataType is not supported.\r
2538 @retval EFI_BUFFER_TOO_SMALL The SelectorSize is too small for the result. This parameter\r
9166f840 2539 has been updated with the size needed to complete the search \r
a3bcde70
HT
2540 request.\r
2541\r
2542**/\r
2543EFI_STATUS\r
2544EFIAPI\r
2545EfiIpSecConfigGetNextSelector (\r
2546 IN EFI_IPSEC_CONFIG_PROTOCOL *This,\r
2547 IN EFI_IPSEC_CONFIG_DATA_TYPE DataType,\r
2548 IN OUT UINTN *SelectorSize,\r
2549 IN OUT EFI_IPSEC_CONFIG_SELECTOR *Selector\r
2550 )\r
2551{\r
2552 LIST_ENTRY *Link;\r
2553 IPSEC_COMMON_POLICY_ENTRY *CommonEntry;\r
2554 BOOLEAN IsFound;\r
2555\r
2556 if (This == NULL || Selector == NULL || SelectorSize == NULL) {\r
2557 return EFI_INVALID_PARAMETER;\r
2558 }\r
2559\r
2560 if (DataType >= IPsecConfigDataTypeMaximum) {\r
2561 return EFI_UNSUPPORTED;\r
2562 }\r
2563\r
2564 IsFound = FALSE;\r
2565\r
2566 NET_LIST_FOR_EACH (Link, &mConfigData[DataType]) {\r
2567 CommonEntry = BASE_CR (Link, IPSEC_COMMON_POLICY_ENTRY, List);\r
2568\r
68d3f2fb 2569 if (IsFound || (BOOLEAN)(mIsZeroSelector[DataType](Selector))) {\r
a3bcde70
HT
2570 //\r
2571 // If found the appointed entry, then duplicate the next one and return,\r
2572 // or if the appointed entry is zero, then return the first one directly.\r
2573 //\r
2574 return mDuplicateSelector[DataType](Selector, CommonEntry->Selector, SelectorSize);\r
2575 } else {\r
2576 //\r
2577 // Set the flag if find the appointed entry.\r
2578 //\r
2579 IsFound = mCompareSelector[DataType](Selector, CommonEntry->Selector);\r
2580 }\r
2581 }\r
2582\r
2583 return EFI_NOT_FOUND;\r
2584}\r
2585\r
2586/**\r
2587 Register an event that is to be signaled whenever a configuration process on the\r
9166f840 2588 specified IPsec configuration information is done. \r
a3bcde70
HT
2589\r
2590 The register function is not surpport now and always returns EFI_UNSUPPORTED.\r
9166f840 2591 \r
a3bcde70
HT
2592 @param[in] This Pointer to the EFI_IPSEC_CONFIG_PROTOCOL instance.\r
2593 @param[in] DataType The type of data to be registered the event for.\r
2594 @param[in] Event The event to be registered.\r
9166f840 2595 \r
a3bcde70
HT
2596 @retval EFI_SUCCESS The event is registered successfully.\r
2597 @retval EFI_INVALID_PARAMETER This is NULL or Event is NULL.\r
2598 @retval EFI_ACCESS_DENIED The Event is already registered for the DataType.\r
2599 @retval EFI_UNSUPPORTED The notify registration is unsupported, or the specified\r
2600 DataType is not supported.\r
2601\r
2602**/\r
2603EFI_STATUS\r
2604EFIAPI\r
2605EfiIpSecConfigRegisterNotify (\r
2606 IN EFI_IPSEC_CONFIG_PROTOCOL *This,\r
2607 IN EFI_IPSEC_CONFIG_DATA_TYPE DataType,\r
2608 IN EFI_EVENT Event\r
2609 )\r
2610{\r
2611 return EFI_UNSUPPORTED;\r
2612}\r
2613\r
2614/**\r
2615 Remove the specified event that was previously registered on the specified IPsec\r
9166f840 2616 configuration data. \r
a3bcde70
HT
2617\r
2618 This function is not support now and alwasy return EFI_UNSUPPORTED.\r
2619\r
2620 @param[in] This Pointer to the EFI_IPSEC_CONFIG_PROTOCOL instance.\r
2621 @param[in] DataType The configuration data type to remove the registered event for.\r
2622 @param[in] Event The event to be unregistered.\r
9166f840 2623 \r
a3bcde70 2624 @retval EFI_SUCCESS The event was removed successfully.\r
9166f840 2625 @retval EFI_NOT_FOUND The Event specified by DataType could not be found in the \r
a3bcde70
HT
2626 database.\r
2627 @retval EFI_INVALID_PARAMETER This is NULL or Event is NULL.\r
2628 @retval EFI_UNSUPPORTED The notify registration is unsupported, or the specified\r
2629 DataType is not supported.\r
2630\r
2631**/\r
2632EFI_STATUS\r
2633EFIAPI\r
2634EfiIpSecConfigUnregisterNotify (\r
2635 IN EFI_IPSEC_CONFIG_PROTOCOL *This,\r
2636 IN EFI_IPSEC_CONFIG_DATA_TYPE DataType,\r
2637 IN EFI_EVENT Event\r
2638 )\r
2639{\r
2640 return EFI_UNSUPPORTED;\r
2641}\r
2642\r
2643/**\r
2644 Copy whole data in specified EFI_SIPEC_CONFIG_SELECTOR and the Data to a buffer.\r
2645\r
2646 This function is a caller defined function, and it is called by the IpSecVisitConfigData().\r
9166f840 2647 The orignal caller is IpSecConfigSave(), which calls the IpsecVisitConfigData() to \r
a3bcde70
HT
2648 copy all types of IPsec Config datas into one buffer and store this buffer into firmware in\r
2649 the form of several variables.\r
9166f840 2650 \r
a3bcde70
HT
2651 @param[in] Type A specified IPSEC_CONFIG_DATA_TYPE.\r
2652 @param[in] Selector Points to a EFI_IPSEC_CONFIG_SELECTOR to be copied\r
2653 to the buffer.\r
2654 @param[in] Data Points to data to be copied to the buffer. The\r
2655 Data type is related to the Type.\r
2656 @param[in] SelectorSize The size of the Selector.\r
2657 @param[in] DataSize The size of the Data.\r
2658 @param[in, out] Buffer The buffer to store the Selector and Data.\r
2659\r
2660 @retval EFI_SUCCESS Copy the Selector and Data to a buffer successfully.\r
2661 @retval EFI_OUT_OF_RESOURCES The required system resource could not be allocated.\r
2662\r
2663**/\r
2664EFI_STATUS\r
2665IpSecCopyPolicyEntry (\r
2666 IN EFI_IPSEC_CONFIG_DATA_TYPE Type,\r
2667 IN EFI_IPSEC_CONFIG_SELECTOR *Selector,\r
2668 IN VOID *Data,\r
2669 IN UINTN SelectorSize,\r
2670 IN UINTN DataSize,\r
2671 IN OUT IPSEC_VARIABLE_BUFFER *Buffer\r
2672 )\r
2673{\r
2674 IPSEC_VAR_ITEM_HEADER SelectorHeader;\r
2675 IPSEC_VAR_ITEM_HEADER DataHeader;\r
2676 UINTN EntrySize;\r
2677 UINT8 *TempPoint;\r
9166f840 2678 \r
a3bcde70
HT
2679 if (Type == IPsecConfigDataTypeSad) {\r
2680 //\r
9166f840 2681 // Don't save automatically-generated SA entry into variable.\r
a3bcde70 2682 //\r
9166f840 2683 if (((EFI_IPSEC_SA_DATA2 *) Data)->ManualSet == FALSE) {\r
a3bcde70
HT
2684 return EFI_SUCCESS;\r
2685 }\r
2686 }\r
2687 //\r
2688 // Increase the capacity size of the buffer if needed.\r
2689 //\r
2690 EntrySize = ALIGN_VARIABLE (sizeof (SelectorHeader));\r
2691 EntrySize = ALIGN_VARIABLE (EntrySize + SelectorSize);\r
2692 EntrySize = ALIGN_VARIABLE (EntrySize + sizeof (SelectorHeader));\r
2693 EntrySize = ALIGN_VARIABLE (EntrySize + DataSize);\r
9166f840 2694 \r
a3bcde70
HT
2695 //EntrySize = SelectorSize + DataSize + 2 * sizeof (SelectorHeader);\r
2696 if (Buffer->Capacity - Buffer->Size < EntrySize) {\r
2697 //\r
2698 // Calculate the required buffer\r
2699 //\r
2700 Buffer->Capacity += EntrySize;\r
2701 TempPoint = AllocatePool (Buffer->Capacity);\r
9166f840 2702 \r
a3bcde70
HT
2703 if (Buffer->Ptr == NULL) {\r
2704 return EFI_OUT_OF_RESOURCES;\r
2705 }\r
2706 //\r
2707 // Copy the old Buffer to new buffer and free the old one.\r
2708 //\r
2709 CopyMem (TempPoint, Buffer->Ptr, Buffer->Size);\r
2710 FreePool (Buffer->Ptr);\r
9166f840 2711 \r
2712 Buffer->Ptr = TempPoint; \r
a3bcde70
HT
2713 }\r
2714\r
2715 mFixPolicyEntry[Type](Selector, Data);\r
2716\r
2717 //\r
2718 // Fill the selector header and copy it into buffer.\r
2719 //\r
2720 SelectorHeader.Type = (UINT8) (Type | IPSEC_VAR_ITEM_HEADER_LOGO_BIT);\r
2721 SelectorHeader.Size = (UINT16) SelectorSize;\r
2722\r
2723 CopyMem (\r
2724 Buffer->Ptr + Buffer->Size,\r
2725 &SelectorHeader,\r
2726 sizeof (SelectorHeader)\r
2727 );\r
2728 Buffer->Size = ALIGN_VARIABLE (Buffer->Size + sizeof (SelectorHeader));\r
9166f840 2729 \r
a3bcde70
HT
2730 //\r
2731 // Copy the selector into buffer.\r
2732 //\r
2733 CopyMem (\r
2734 Buffer->Ptr + Buffer->Size,\r
2735 Selector,\r
2736 SelectorSize\r
2737 );\r
2738 Buffer->Size = ALIGN_VARIABLE (Buffer->Size + SelectorSize);\r
2739\r
2740 //\r
2741 // Fill the data header and copy it into buffer.\r
2742 //\r
2743 DataHeader.Type = (UINT8) Type;\r
2744 DataHeader.Size = (UINT16) DataSize;\r
2745\r
2746 CopyMem (\r
2747 Buffer->Ptr + Buffer->Size,\r
2748 &DataHeader,\r
2749 sizeof (DataHeader)\r
2750 );\r
2751 Buffer->Size = ALIGN_VARIABLE (Buffer->Size + sizeof (DataHeader));\r
2752 //\r
2753 // Copy the data into buffer.\r
2754 //\r
2755 CopyMem (\r
2756 Buffer->Ptr + Buffer->Size,\r
2757 Data,\r
2758 DataSize\r
2759 );\r
2760 Buffer->Size = ALIGN_VARIABLE (Buffer->Size + DataSize);\r
9166f840 2761 \r
a3bcde70
HT
2762 mUnfixPolicyEntry[Type](Selector, Data);\r
2763\r
2764 return EFI_SUCCESS;\r
2765}\r
2766\r
2767/**\r
2768 Visit all IPsec Configurations of specified Type and call the caller defined\r
2769 interface.\r
2770\r
2771 @param[in] DataType The specified IPsec Config Data Type.\r
2772 @param[in] Routine The function defined by the caller.\r
2773 @param[in] Context The data passed to the Routine.\r
2774\r
2775 @retval EFI_OUT_OF_RESOURCES The required system resource could not be allocated\r
2776 @retval EFI_SUCCESS This function completed successfully.\r
2777\r
2778**/\r
2779EFI_STATUS\r
2780IpSecVisitConfigData (\r
2781 IN EFI_IPSEC_CONFIG_DATA_TYPE DataType,\r
2782 IN IPSEC_COPY_POLICY_ENTRY Routine,\r
2783 IN VOID *Context\r
2784 )\r
2785{\r
2786 EFI_STATUS GetNextStatus;\r
2787 EFI_STATUS GetDataStatus;\r
2788 EFI_STATUS RoutineStatus;\r
2789 EFI_IPSEC_CONFIG_SELECTOR *Selector;\r
2790 VOID *Data;\r
2791 UINTN SelectorSize;\r
2792 UINTN DataSize;\r
2793 UINTN SelectorBufferSize;\r
2794 UINTN DataBufferSize;\r
2795 BOOLEAN FirstGetNext;\r
2796\r
2797 FirstGetNext = TRUE;\r
2798 DataBufferSize = 0;\r
2799 Data = NULL;\r
2800 SelectorBufferSize = sizeof (EFI_IPSEC_CONFIG_SELECTOR);\r
2801 Selector = AllocateZeroPool (SelectorBufferSize);\r
2802\r
2803 if (Selector == NULL) {\r
2804 return EFI_OUT_OF_RESOURCES;\r
2805 }\r
2806\r
2807 while (TRUE) {\r
2808 //\r
2809 // Get the real size of the selector.\r
2810 //\r
2811 SelectorSize = SelectorBufferSize;\r
2812 GetNextStatus = EfiIpSecConfigGetNextSelector (\r
2813 &mIpSecConfigInstance,\r
2814 DataType,\r
2815 &SelectorSize,\r
2816 Selector\r
2817 );\r
2818 if (GetNextStatus == EFI_BUFFER_TOO_SMALL) {\r
2819 FreePool (Selector);\r
2820 SelectorBufferSize = SelectorSize;\r
2821 //\r
2822 // Allocate zero pool for the first selector, while store the last\r
2823 // selector content for the other selectors.\r
2824 //\r
2825 if (FirstGetNext) {\r
2826 Selector = AllocateZeroPool (SelectorBufferSize);\r
2827 } else {\r
2828 Selector = AllocateCopyPool (SelectorBufferSize, Selector);\r
2829 }\r
2830\r
2831 if (Selector == NULL) {\r
2832 return EFI_OUT_OF_RESOURCES;\r
2833 }\r
2834 //\r
2835 // Get the content of the selector.\r
2836 //\r
2837 GetNextStatus = EfiIpSecConfigGetNextSelector (\r
2838 &mIpSecConfigInstance,\r
2839 DataType,\r
2840 &SelectorSize,\r
2841 Selector\r
2842 );\r
2843 }\r
2844\r
2845 if (EFI_ERROR (GetNextStatus)) {\r
2846 break;\r
2847 }\r
2848\r
2849 FirstGetNext = FALSE;\r
2850\r
2851 //\r
2852 // Get the real size of the policy entry according to the selector.\r
2853 //\r
2854 DataSize = DataBufferSize;\r
2855 GetDataStatus = EfiIpSecConfigGetData (\r
2856 &mIpSecConfigInstance,\r
2857 DataType,\r
2858 Selector,\r
2859 &DataSize,\r
2860 Data\r
2861 );\r
2862 if (GetDataStatus == EFI_BUFFER_TOO_SMALL) {\r
2863 if (Data != NULL) {\r
2864 FreePool (Data);\r
2865 }\r
2866\r
2867 DataBufferSize = DataSize;\r
2868 Data = AllocateZeroPool (DataBufferSize);\r
2869\r
2870 if (Data == NULL) {\r
2871 return EFI_OUT_OF_RESOURCES;\r
2872 }\r
2873 //\r
2874 // Get the content of the policy entry according to the selector.\r
2875 //\r
2876 GetDataStatus = EfiIpSecConfigGetData (\r
2877 &mIpSecConfigInstance,\r
2878 DataType,\r
2879 Selector,\r
2880 &DataSize,\r
2881 Data\r
2882 );\r
2883 }\r
2884\r
2885 if (EFI_ERROR (GetDataStatus)) {\r
2886 break;\r
2887 }\r
2888 //\r
2889 // Prepare the buffer of updated policy entry, which is stored in\r
2890 // the continous memory, and then save into variable later.\r
2891 //\r
2892 RoutineStatus = Routine (\r
2893 DataType,\r
2894 Selector,\r
2895 Data,\r
2896 SelectorSize,\r
2897 DataSize,\r
2898 Context\r
2899 );\r
2900 if (EFI_ERROR (RoutineStatus)) {\r
2901 break;\r
2902 }\r
2903 }\r
2904\r
2905 if (Data != NULL) {\r
2906 FreePool (Data);\r
2907 }\r
2908\r
2909 if (Selector != NULL) {\r
2910 FreePool (Selector);\r
2911 }\r
2912\r
2913 return EFI_SUCCESS;\r
2914}\r
2915\r
2916/**\r
2917 This function is the subfunction of EFIIpSecConfigSetData.\r
2918\r
2919 This function call IpSecSetVaraible to set the IPsec Configuration into the firmware.\r
2920\r
2921 @retval EFI_OUT_OF_RESOURCES The required system resource could not be allocated.\r
2922 @retval EFI_SUCCESS Saved the configration successfully.\r
2923 @retval Others Other errors were found while obtaining the variable.\r
2924\r
2925**/\r
2926EFI_STATUS\r
2927IpSecConfigSave (\r
2928 VOID\r
2929 )\r
2930{\r
2931 IPSEC_VARIABLE_BUFFER Buffer;\r
2932 EFI_STATUS Status;\r
2933 EFI_IPSEC_CONFIG_DATA_TYPE Type;\r
2934\r
2935 Buffer.Size = 0;\r
2936 Buffer.Capacity = IPSEC_DEFAULT_VARIABLE_SIZE;\r
2937 Buffer.Ptr = AllocateZeroPool (Buffer.Capacity);\r
2938\r
2939 if (Buffer.Ptr == NULL) {\r
2940 return EFI_OUT_OF_RESOURCES;\r
2941 }\r
2942 //\r
2943 // For each policy database, prepare the contious buffer to save into variable.\r
2944 //\r
2945 for (Type = IPsecConfigDataTypeSpd; Type < IPsecConfigDataTypeMaximum; Type++) {\r
2946 IpSecVisitConfigData (\r
2947 Type,\r
2948 (IPSEC_COPY_POLICY_ENTRY) IpSecCopyPolicyEntry,\r
2949 &Buffer\r
2950 );\r
2951 }\r
2952 //\r
2953 // Save the updated policy database into variable.\r
2954 //\r
2955 Status = IpSecSetVariable (\r
2956 IPSECCONFIG_VARIABLE_NAME,\r
2957 &gEfiIpSecConfigProtocolGuid,\r
2958 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
2959 Buffer.Size,\r
2960 Buffer.Ptr\r
2961 );\r
2962\r
2963 FreePool (Buffer.Ptr);\r
2964\r
2965 return Status;\r
2966}\r
2967\r
2968/**\r
2969 Get the all IPSec configuration variables and store those variables\r
2970 to the internal data structure.\r
2971\r
9166f840 2972 This founction is called by IpSecConfigInitialize() which is to intialize the \r
a3bcde70
HT
2973 IPsecConfiguration Protocol.\r
2974\r
2975 @param[in] Private Point to IPSEC_PRIVATE_DATA.\r
2976\r
2977 @retval EFI_OUT_OF_RESOURCES The required system resource could not be allocated\r
2978 @retval EFI_SUCCESS Restore the IPsec Configuration successfully.\r
2979 @retval others Other errors is found while obtaining the variable.\r
2980\r
2981**/\r
2982EFI_STATUS\r
2983IpSecConfigRestore (\r
2984 IN IPSEC_PRIVATE_DATA *Private\r
2985 )\r
2986{\r
2987 EFI_STATUS Status;\r
2988 UINTN BufferSize;\r
2989 UINT8 *Buffer;\r
2990 IPSEC_VAR_ITEM_HEADER *Header;\r
2991 UINT8 *Ptr;\r
2992 EFI_IPSEC_CONFIG_SELECTOR *Selector;\r
2993 EFI_IPSEC_CONFIG_DATA_TYPE Type;\r
2994 VOID *Data;\r
2995 UINT8 Value;\r
2996 UINTN Size;\r
2997\r
2998 Value = 0;\r
2999 Size = sizeof (Value);\r
3000 BufferSize = 0;\r
3001 Buffer = NULL;\r
3002\r
3003 Status = gRT->GetVariable (\r
3004 IPSECCONFIG_STATUS_NAME,\r
3005 &gEfiIpSecConfigProtocolGuid,\r
3006 NULL,\r
3007 &Size,\r
3008 &Value\r
3009 );\r
3010\r
3011 if (!EFI_ERROR (Status) && Value == IPSEC_STATUS_ENABLED) {\r
3012 Private->IpSec.DisabledFlag = FALSE;\r
3013 }\r
3014 //\r
3015 // Get the real size of policy database in variable.\r
3016 //\r
3017 Status = IpSecGetVariable (\r
3018 IPSECCONFIG_VARIABLE_NAME,\r
3019 &gEfiIpSecConfigProtocolGuid,\r
3020 NULL,\r
3021 &BufferSize,\r
3022 Buffer\r
3023 );\r
3024 if (Status == EFI_BUFFER_TOO_SMALL) {\r
3025\r
3026 Buffer = AllocateZeroPool (BufferSize);\r
3027 if (Buffer == NULL) {\r
3028 return EFI_OUT_OF_RESOURCES;\r
3029 }\r
3030 //\r
3031 // Get the content of policy database in variable.\r
3032 //\r
3033 Status = IpSecGetVariable (\r
3034 IPSECCONFIG_VARIABLE_NAME,\r
3035 &gEfiIpSecConfigProtocolGuid,\r
3036 NULL,\r
3037 &BufferSize,\r
3038 Buffer\r
3039 );\r
3040 if (EFI_ERROR (Status)) {\r
3041 FreePool (Buffer);\r
3042 return Status;\r
3043 }\r
3044\r
3045 for (Ptr = Buffer; Ptr < Buffer + BufferSize;) {\r
3046\r
3047 Header = (IPSEC_VAR_ITEM_HEADER *) Ptr;\r
3048 Type = (EFI_IPSEC_CONFIG_DATA_TYPE) (Header->Type & IPSEC_VAR_ITEM_HEADER_CONTENT_BIT);\r
3049 ASSERT (((Header->Type & 0x80) == IPSEC_VAR_ITEM_HEADER_LOGO_BIT) && (Type < IPsecConfigDataTypeMaximum));\r
9166f840 3050 \r
a3bcde70
HT
3051 Selector = (EFI_IPSEC_CONFIG_SELECTOR *) ALIGN_POINTER (Header + 1, sizeof (UINTN));\r
3052 Header = (IPSEC_VAR_ITEM_HEADER *) ALIGN_POINTER (\r
9166f840 3053 (UINT8 *) Selector + Header->Size, \r
a3bcde70
HT
3054 sizeof (UINTN)\r
3055 );\r
3056 ASSERT (Header->Type == Type);\r
3057\r
3058 Data = ALIGN_POINTER (Header + 1, sizeof (UINTN));\r
3059\r
3060 mUnfixPolicyEntry[Type](Selector, Data);\r
3061\r
3062 //\r
3063 // Update each policy entry according to the content in variable.\r
3064 //\r
3065 mSetBySelf = TRUE;\r
3066 Status = EfiIpSecConfigSetData (\r
3067 &Private->IpSecConfig,\r
3068 Type,\r
3069 Selector,\r
3070 Data,\r
3071 NULL\r
3072 );\r
3073 mSetBySelf = FALSE;\r
3074\r
3075 if (EFI_ERROR (Status)) {\r
3076 FreePool (Buffer);\r
3077 return Status;\r
3078 }\r
3079\r
3080 Ptr = ALIGN_POINTER ((UINT8 *) Data + Header->Size, sizeof (UINTN));\r
3081 }\r
3082\r
3083 FreePool (Buffer);\r
3084 }\r
3085\r
3086 return EFI_SUCCESS;\r
3087}\r
3088\r
3089/**\r
3090 Install and Initialize IPsecConfig protocol\r
3091\r
3092 @param[in, out] Private Pointer to IPSEC_PRIVATE_DATA. After this function finish,\r
3093 the pointer of IPsecConfig Protocol implementation will copy\r
3094 into its IPsecConfig member.\r
3095\r
3096 @retval EFI_SUCCESS Initialized the IPsecConfig Protocol successfully.\r
3097 @retval Others Initializing the IPsecConfig Protocol failed.\r
3098**/\r
3099EFI_STATUS\r
3100IpSecConfigInitialize (\r
3101 IN OUT IPSEC_PRIVATE_DATA *Private\r
3102 )\r
3103{\r
3104 EFI_IPSEC_CONFIG_DATA_TYPE Type;\r
3105\r
3106 CopyMem (\r
3107 &Private->IpSecConfig,\r
3108 &mIpSecConfigInstance,\r
3109 sizeof (EFI_IPSEC_CONFIG_PROTOCOL)\r
3110 );\r
3111\r
3112 //\r
3113 // Initialize the list head of policy database.\r
3114 //\r
3115 for (Type = IPsecConfigDataTypeSpd; Type < IPsecConfigDataTypeMaximum; Type++) {\r
3116 InitializeListHead (&mConfigData[Type]);\r
3117 }\r
3118 //\r
3119 // Restore the content of policy database according to the variable.\r
3120 //\r
3121 IpSecConfigRestore (Private);\r
3122\r
3123 return gBS->InstallMultipleProtocolInterfaces (\r
3124 &Private->Handle,\r
3125 &gEfiIpSecConfigProtocolGuid,\r
3126 &Private->IpSecConfig,\r
3127 NULL\r
3128 );\r
3129}\r