NetworkPkg: Fix connection issue after correct SPD and re-enable IPsec
[mirror_edk2.git] / NetworkPkg / IpSecDxe / IpSecConfigImpl.c
CommitLineData
a3bcde70
HT
1/** @file\r
2 The implementation of IPSEC_CONFIG_PROTOCOL.\r
3\r
60cbd7fb 4 Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.<BR>\r
a3bcde70
HT
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
96702f88
FS
1170 SpdEntry->Data->PackageFlag = SpdData->PackageFlag;\r
1171 SpdEntry->Data->TrafficDirection = SpdData->TrafficDirection;\r
1172 SpdEntry->Data->Action = SpdData->Action;\r
1173 \r
a3bcde70
HT
1174 //\r
1175 // Fix the address of ProcessingPolicy and copy it if need, which is continous\r
9166f840 1176 // memory and close to the base structure of SAD data.\r
a3bcde70
HT
1177 //\r
1178 if (SpdData->Action != EfiIPsecActionProtect) {\r
1179 SpdEntry->Data->ProcessingPolicy = NULL;\r
1180 } else {\r
1181 SpdEntry->Data->ProcessingPolicy = (EFI_IPSEC_PROCESS_POLICY *) ALIGN_POINTER (\r
1182 SpdEntry->Data + 1,\r
1183 sizeof (UINTN)\r
1184 );\r
1185 IpSecDuplicateProcessPolicy (SpdEntry->Data->ProcessingPolicy, SpdData->ProcessingPolicy);\r
1186 }\r
1187 //\r
9166f840 1188 // Update the sas list of the new SPD entry.\r
a3bcde70
HT
1189 //\r
1190 InitializeListHead (&SpdEntry->Data->Sas);\r
1191\r
1192 SadList = &mConfigData[IPsecConfigDataTypeSad];\r
1193\r
1194 NET_LIST_FOR_EACH (Entry, SadList) {\r
1195 SadEntry = IPSEC_SAD_ENTRY_FROM_LIST (Entry);\r
1196\r
1197 for (Index = 0; Index < SpdData->SaIdCount; Index++) {\r
1198\r
1199 if (CompareSaId (\r
1200 (EFI_IPSEC_CONFIG_SELECTOR *) &SpdData->SaId[Index],\r
1201 (EFI_IPSEC_CONFIG_SELECTOR *) SadEntry->Id\r
1202 )) {\r
60cbd7fb
JW
1203 if (SadEntry->Data->SpdEntry != NULL) { \r
1204 RemoveEntryList (&SadEntry->BySpd);\r
1205 }\r
a3bcde70 1206 InsertTailList (&SpdEntry->Data->Sas, &SadEntry->BySpd);\r
2f667c54 1207 SadEntry->Data->SpdEntry = SpdEntry; \r
a3bcde70
HT
1208 }\r
1209 }\r
1210 }\r
1211 //\r
9166f840 1212 // Insert the new SPD entry.\r
a3bcde70
HT
1213 //\r
1214 InsertTailList (EntryInsertBefore, &SpdEntry->List);\r
1215\r
1216 return EFI_SUCCESS;\r
1217}\r
1218\r
1219/**\r
1220 Set the security association information for the EFI IPsec driver.\r
1221\r
9166f840 1222 The IPsec configuration data has a unique selector/identifier separately to \r
a3bcde70
HT
1223 identify a data entry.\r
1224\r
9166f840 1225 @param[in] Selector Pointer to an entry selector on operated \r
1226 configuration data specified by DataType. \r
1227 A NULL Selector causes the entire specified-type \r
a3bcde70 1228 configuration information to be flushed.\r
9166f840 1229 @param[in] Data The data buffer to be set. The structure \r
a3bcde70 1230 of the data buffer should be EFI_IPSEC_SA_DATA.\r
9166f840 1231 @param[in] Context Pointer to one entry selector which describes \r
1232 the expected position the new data entry will \r
a3bcde70
HT
1233 be added. If Context is NULL,the new entry will\r
1234 be appended the end of database.\r
1235\r
1236 @retval EFI_OUT_OF_RESOURCED The required system resource could not be allocated.\r
1237 @retval EFI_SUCCESS The specified configuration data was obtained successfully.\r
1238\r
1239**/\r
1240EFI_STATUS\r
1241SetSadEntry (\r
1242 IN EFI_IPSEC_CONFIG_SELECTOR *Selector,\r
1243 IN VOID *Data,\r
1244 IN VOID *Context OPTIONAL\r
1245 )\r
1246{\r
1247 IPSEC_SAD_ENTRY *SadEntry;\r
1248 IPSEC_SPD_ENTRY *SpdEntry;\r
1249 LIST_ENTRY *Entry;\r
1250 LIST_ENTRY *NextEntry;\r
1251 LIST_ENTRY *SadList;\r
1252 LIST_ENTRY *SpdList;\r
1253 EFI_IPSEC_SA_ID *SaId;\r
9166f840 1254 EFI_IPSEC_SA_DATA2 *SaData;\r
a3bcde70
HT
1255 EFI_IPSEC_SA_ID *InsertBefore;\r
1256 LIST_ENTRY *EntryInsertBefore;\r
1257 UINTN SadEntrySize;\r
9166f840 1258 \r
a3bcde70 1259 SaId = (Selector == NULL) ? NULL : &Selector->SaId;\r
9166f840 1260 SaData = (Data == NULL) ? NULL : (EFI_IPSEC_SA_DATA2 *) Data;\r
a3bcde70
HT
1261 InsertBefore = (Context == NULL) ? NULL : &((EFI_IPSEC_CONFIG_SELECTOR *) Context)->SaId;\r
1262 SadList = &mConfigData[IPsecConfigDataTypeSad];\r
1263\r
1264 //\r
1265 // The default behavior is to insert the node ahead of the header.\r
1266 //\r
1267 EntryInsertBefore = SadList;\r
1268\r
1269 //\r
9166f840 1270 // Remove the existed SAD entry.\r
a3bcde70
HT
1271 //\r
1272 NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, SadList) {\r
1273\r
1274 SadEntry = IPSEC_SAD_ENTRY_FROM_LIST (Entry);\r
1275\r
9166f840 1276 if (SaId == NULL || \r
a3bcde70
HT
1277 CompareSaId (\r
1278 (EFI_IPSEC_CONFIG_SELECTOR *) SadEntry->Id,\r
1279 (EFI_IPSEC_CONFIG_SELECTOR *) SaId\r
1280 )) {\r
1281 //\r
1282 // Record the existed entry position to keep the original order.\r
1283 //\r
1284 EntryInsertBefore = SadEntry->List.ForwardLink;\r
1285\r
1286 //\r
9166f840 1287 // Update the related SAD.byspd field.\r
a3bcde70
HT
1288 //\r
1289 if (SadEntry->Data->SpdEntry != NULL) {\r
1290 RemoveEntryList (&SadEntry->BySpd);\r
1291 }\r
1292\r
1293 RemoveEntryList (&SadEntry->List);\r
1294 FreePool (SadEntry);\r
1295 }\r
1296 }\r
1297 //\r
9166f840 1298 // Return success here if only want to remove the SAD entry\r
a3bcde70
HT
1299 //\r
1300 if (SaData == NULL || SaId == NULL) {\r
1301 return EFI_SUCCESS;\r
1302 }\r
1303 //\r
1304 // Search the appointed entry position if InsertBefore is not NULL.\r
1305 //\r
1306 if (InsertBefore != NULL) {\r
1307\r
1308 NET_LIST_FOR_EACH (Entry, SadList) {\r
1309 SadEntry = IPSEC_SAD_ENTRY_FROM_LIST (Entry);\r
1310\r
1311 if (CompareSaId (\r
1312 (EFI_IPSEC_CONFIG_SELECTOR *) SadEntry->Id,\r
1313 (EFI_IPSEC_CONFIG_SELECTOR *) InsertBefore\r
1314 )) {\r
1315 EntryInsertBefore = Entry;\r
1316 break;\r
1317 }\r
1318 }\r
1319 }\r
1320\r
1321 //\r
1322 // Do Padding for different Arch.\r
1323 //\r
1324 SadEntrySize = ALIGN_VARIABLE (sizeof (IPSEC_SAD_ENTRY));\r
9166f840 1325 SadEntrySize = ALIGN_VARIABLE (SadEntrySize + sizeof (EFI_IPSEC_SA_ID));\r
a3bcde70 1326 SadEntrySize = ALIGN_VARIABLE (SadEntrySize + sizeof (IPSEC_SAD_DATA));\r
9166f840 1327 \r
a3bcde70
HT
1328 if (SaId->Proto == EfiIPsecAH) {\r
1329 SadEntrySize += SaData->AlgoInfo.AhAlgoInfo.AuthKeyLength;\r
1330 } else {\r
1331 SadEntrySize = ALIGN_VARIABLE (SadEntrySize + SaData->AlgoInfo.EspAlgoInfo.AuthKeyLength);\r
9166f840 1332 SadEntrySize += ALIGN_VARIABLE (SaData->AlgoInfo.EspAlgoInfo.EncKeyLength);\r
a3bcde70
HT
1333 }\r
1334\r
9166f840 1335 if (SaData->SpdSelector != NULL) {\r
1336 SadEntrySize += SadEntrySize + (UINTN)SIZE_OF_SPD_SELECTOR (SaData->SpdSelector);\r
1337 }\r
a3bcde70
HT
1338 SadEntry = AllocateZeroPool (SadEntrySize);\r
1339\r
1340 if (SadEntry == NULL) {\r
1341 return EFI_OUT_OF_RESOURCES;\r
1342 }\r
1343 //\r
1344 // Fix the address of Id and Data buffer and copy them, which is\r
9166f840 1345 // continous memory and close to the base structure of SAD entry.\r
a3bcde70
HT
1346 //\r
1347 SadEntry->Id = (EFI_IPSEC_SA_ID *) ALIGN_POINTER ((SadEntry + 1), sizeof (UINTN));\r
1348 SadEntry->Data = (IPSEC_SAD_DATA *) ALIGN_POINTER ((SadEntry->Id + 1), sizeof (UINTN));\r
1349\r
1350 CopyMem (SadEntry->Id, SaId, sizeof (EFI_IPSEC_SA_ID));\r
1351\r
1352 SadEntry->Data->Mode = SaData->Mode;\r
1353 SadEntry->Data->SequenceNumber = SaData->SNCount;\r
1354 SadEntry->Data->AntiReplayWindowSize = SaData->AntiReplayWindows;\r
1355\r
1356 ZeroMem (\r
1357 &SadEntry->Data->AntiReplayBitmap,\r
1358 sizeof (SadEntry->Data->AntiReplayBitmap)\r
1359 );\r
1360\r
1361 ZeroMem (\r
1362 &SadEntry->Data->AlgoInfo,\r
1363 sizeof (EFI_IPSEC_ALGO_INFO)\r
1364 );\r
1365\r
1366 SadEntry->Data->AlgoInfo.EspAlgoInfo.AuthAlgoId = SaData->AlgoInfo.EspAlgoInfo.AuthAlgoId;\r
1367 SadEntry->Data->AlgoInfo.EspAlgoInfo.AuthKeyLength = SaData->AlgoInfo.EspAlgoInfo.AuthKeyLength;\r
1368\r
1369 if (SadEntry->Data->AlgoInfo.EspAlgoInfo.AuthKeyLength != 0) {\r
1370 SadEntry->Data->AlgoInfo.EspAlgoInfo.AuthKey = (VOID *) ALIGN_POINTER ((SadEntry->Data + 1), sizeof (UINTN));\r
1371 CopyMem (\r
1372 SadEntry->Data->AlgoInfo.EspAlgoInfo.AuthKey,\r
1373 SaData->AlgoInfo.EspAlgoInfo.AuthKey,\r
1374 SadEntry->Data->AlgoInfo.EspAlgoInfo.AuthKeyLength\r
1375 );\r
1376 }\r
1377\r
1378 if (SaId->Proto == EfiIPsecESP) {\r
1379 SadEntry->Data->AlgoInfo.EspAlgoInfo.EncAlgoId = SaData->AlgoInfo.EspAlgoInfo.EncAlgoId;\r
1380 SadEntry->Data->AlgoInfo.EspAlgoInfo.EncKeyLength = SaData->AlgoInfo.EspAlgoInfo.EncKeyLength;\r
1381\r
1382 if (SadEntry->Data->AlgoInfo.EspAlgoInfo.EncKeyLength != 0) {\r
1383 SadEntry->Data->AlgoInfo.EspAlgoInfo.EncKey = (VOID *) ALIGN_POINTER (\r
9166f840 1384 ((UINT8 *) (SadEntry->Data + 1) + \r
a3bcde70
HT
1385 SadEntry->Data->AlgoInfo.EspAlgoInfo.AuthKeyLength),\r
1386 sizeof (UINTN)\r
1387 );\r
1388 CopyMem (\r
1389 SadEntry->Data->AlgoInfo.EspAlgoInfo.EncKey,\r
1390 SaData->AlgoInfo.EspAlgoInfo.EncKey,\r
1391 SadEntry->Data->AlgoInfo.EspAlgoInfo.EncKeyLength\r
1392 );\r
1393 }\r
1394 }\r
1395\r
1396 CopyMem (\r
1397 &SadEntry->Data->SaLifetime,\r
1398 &SaData->SaLifetime,\r
1399 sizeof (EFI_IPSEC_SA_LIFETIME)\r
1400 );\r
1401\r
1402 SadEntry->Data->PathMTU = SaData->PathMTU;\r
9166f840 1403 SadEntry->Data->SpdSelector = NULL;\r
a3bcde70
HT
1404 SadEntry->Data->ESNEnabled = FALSE;\r
1405 SadEntry->Data->ManualSet = SaData->ManualSet;\r
1406\r
1407 //\r
9166f840 1408 // Copy Tunnel Source/Destination Address\r
1409 //\r
1410 if (SaData->Mode == EfiIPsecTunnel) {\r
1411 CopyMem (\r
1412 &SadEntry->Data->TunnelDestAddress,\r
1413 &SaData->TunnelDestinationAddress,\r
1414 sizeof (EFI_IP_ADDRESS)\r
1415 );\r
1416 CopyMem (\r
1417 &SadEntry->Data->TunnelSourceAddress,\r
1418 &SaData->TunnelSourceAddress,\r
1419 sizeof (EFI_IP_ADDRESS)\r
1420 );\r
1421 }\r
1422 //\r
1423 // Update the spd.sas list of the spd entry specified by SAD selector\r
a3bcde70
HT
1424 //\r
1425 SpdList = &mConfigData[IPsecConfigDataTypeSpd];\r
1426\r
1427 for (Entry = SpdList->ForwardLink; Entry != SpdList && SaData->SpdSelector != NULL; Entry = Entry->ForwardLink) {\r
1428\r
1429 SpdEntry = IPSEC_SPD_ENTRY_FROM_LIST (Entry);\r
9166f840 1430 if (IsSubSpdSelector (\r
1431 (EFI_IPSEC_CONFIG_SELECTOR *) SaData->SpdSelector,\r
1432 (EFI_IPSEC_CONFIG_SELECTOR *) SpdEntry->Selector\r
a3bcde70
HT
1433 ) && SpdEntry->Data->Action == EfiIPsecActionProtect) {\r
1434 SadEntry->Data->SpdEntry = SpdEntry;\r
9166f840 1435 SadEntry->Data->SpdSelector = (EFI_IPSEC_SPD_SELECTOR *)((UINT8 *)SadEntry +\r
1436 SadEntrySize -\r
1437 (UINTN)SIZE_OF_SPD_SELECTOR (SaData->SpdSelector)\r
1438 );\r
1439 DuplicateSpdSelector (\r
1440 (EFI_IPSEC_CONFIG_SELECTOR *) SadEntry->Data->SpdSelector,\r
1441 (EFI_IPSEC_CONFIG_SELECTOR *) SaData->SpdSelector,\r
1442 NULL\r
1443 );\r
a3bcde70
HT
1444 InsertTailList (&SpdEntry->Data->Sas, &SadEntry->BySpd);\r
1445 }\r
1446 }\r
1447 //\r
9166f840 1448 // Insert the new SAD entry.\r
a3bcde70
HT
1449 //\r
1450 InsertTailList (EntryInsertBefore, &SadEntry->List);\r
1451\r
1452 return EFI_SUCCESS;\r
1453}\r
1454\r
1455/**\r
1456 Set the peer authorization configuration information for the EFI IPsec driver.\r
1457\r
9166f840 1458 The IPsec configuration data has a unique selector/identifier separately to \r
a3bcde70
HT
1459 identify a data entry.\r
1460\r
9166f840 1461 @param[in] Selector Pointer to an entry selector on operated \r
1462 configuration data specified by DataType. \r
1463 A NULL Selector causes the entire specified-type \r
a3bcde70 1464 configuration information to be flushed.\r
9166f840 1465 @param[in] Data The data buffer to be set. The structure \r
a3bcde70 1466 of the data buffer should be EFI_IPSEC_PAD_DATA.\r
9166f840 1467 @param[in] Context Pointer to one entry selector that describes \r
1468 the expected position the new data entry will \r
a3bcde70
HT
1469 be added. If Context is NULL, the new entry will\r
1470 be appended the end of database.\r
1471\r
1472 @retval EFI_OUT_OF_RESOURCES The required system resources could not be allocated.\r
1473 @retval EFI_SUCCESS The specified configuration data was obtained successfully.\r
1474\r
1475**/\r
1476EFI_STATUS\r
1477SetPadEntry (\r
1478 IN EFI_IPSEC_CONFIG_SELECTOR *Selector,\r
1479 IN VOID *Data,\r
1480 IN VOID *Context OPTIONAL\r
1481 )\r
1482{\r
1483 IPSEC_PAD_ENTRY *PadEntry;\r
1484 EFI_IPSEC_PAD_ID *PadId;\r
1485 EFI_IPSEC_PAD_DATA *PadData;\r
1486 LIST_ENTRY *PadList;\r
1487 LIST_ENTRY *Entry;\r
1488 LIST_ENTRY *NextEntry;\r
1489 EFI_IPSEC_PAD_ID *InsertBefore;\r
1490 LIST_ENTRY *EntryInsertBefore;\r
1491 UINTN PadEntrySize;\r
9166f840 1492 \r
a3bcde70
HT
1493 PadId = (Selector == NULL) ? NULL : &Selector->PadId;\r
1494 PadData = (Data == NULL) ? NULL : (EFI_IPSEC_PAD_DATA *) Data;\r
1495 InsertBefore = (Context == NULL) ? NULL : &((EFI_IPSEC_CONFIG_SELECTOR *) Context)->PadId;\r
1496 PadList = &mConfigData[IPsecConfigDataTypePad];\r
1497\r
1498 //\r
1499 // The default behavior is to insert the node ahead of the header.\r
1500 //\r
1501 EntryInsertBefore = PadList;\r
1502\r
1503 //\r
1504 // Remove the existed pad entry.\r
1505 //\r
1506 NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, PadList) {\r
1507\r
1508 PadEntry = IPSEC_PAD_ENTRY_FROM_LIST (Entry);\r
1509\r
9166f840 1510 if (PadId == NULL || \r
a3bcde70
HT
1511 ComparePadId ((EFI_IPSEC_CONFIG_SELECTOR *) PadEntry->Id, (EFI_IPSEC_CONFIG_SELECTOR *) PadId)\r
1512 ) {\r
1513 //\r
1514 // Record the existed entry position to keep the original order.\r
1515 //\r
1516 EntryInsertBefore = PadEntry->List.ForwardLink;\r
1517 RemoveEntryList (&PadEntry->List);\r
1518\r
1519 FreePool (PadEntry);\r
1520 }\r
1521 }\r
1522 //\r
1523 // Return success here if only want to remove the pad entry\r
1524 //\r
1525 if (PadData == NULL || PadId == NULL) {\r
1526 return EFI_SUCCESS;\r
1527 }\r
1528 //\r
1529 // Search the appointed entry position if InsertBefore is not NULL.\r
1530 //\r
1531 if (InsertBefore != NULL) {\r
1532\r
1533 NET_LIST_FOR_EACH (Entry, PadList) {\r
1534 PadEntry = IPSEC_PAD_ENTRY_FROM_LIST (Entry);\r
1535\r
1536 if (ComparePadId (\r
1537 (EFI_IPSEC_CONFIG_SELECTOR *) PadEntry->Id,\r
1538 (EFI_IPSEC_CONFIG_SELECTOR *) InsertBefore\r
1539 )) {\r
1540 EntryInsertBefore = Entry;\r
1541 break;\r
1542 }\r
1543 }\r
1544 }\r
1545\r
1546 //\r
1547 // Do PADDING for different arch.\r
1548 //\r
1549 PadEntrySize = ALIGN_VARIABLE (sizeof (IPSEC_PAD_ENTRY));\r
1550 PadEntrySize = ALIGN_VARIABLE (PadEntrySize + sizeof (EFI_IPSEC_PAD_ID));\r
1551 PadEntrySize = ALIGN_VARIABLE (PadEntrySize + sizeof (EFI_IPSEC_PAD_DATA));\r
1552 PadEntrySize = ALIGN_VARIABLE (PadEntrySize + (PadData->AuthData != NULL ? PadData->AuthDataSize : 0));\r
1553 PadEntrySize += PadData->RevocationData != NULL ? PadData->RevocationDataSize : 0;\r
1554\r
1555 PadEntry = AllocateZeroPool (PadEntrySize);\r
1556\r
1557 if (PadEntry == NULL) {\r
1558 return EFI_OUT_OF_RESOURCES;\r
1559 }\r
1560 //\r
1561 // Fix the address of Id and Data buffer and copy them, which is\r
1562 // continous memory and close to the base structure of pad entry.\r
1563 //\r
1564 PadEntry->Id = (EFI_IPSEC_PAD_ID *) ALIGN_POINTER ((PadEntry + 1), sizeof (UINTN));\r
1565 PadEntry->Data = (EFI_IPSEC_PAD_DATA *) ALIGN_POINTER ((PadEntry->Id + 1), sizeof (UINTN));\r
1566\r
1567 CopyMem (PadEntry->Id, PadId, sizeof (EFI_IPSEC_PAD_ID));\r
1568\r
1569 PadEntry->Data->AuthProtocol = PadData->AuthProtocol;\r
1570 PadEntry->Data->AuthMethod = PadData->AuthMethod;\r
1571 PadEntry->Data->IkeIdFlag = PadData->IkeIdFlag;\r
1572\r
1573 if (PadData->AuthData != NULL) {\r
1574 PadEntry->Data->AuthDataSize = PadData->AuthDataSize;\r
1575 PadEntry->Data->AuthData = (VOID *) ALIGN_POINTER (PadEntry->Data + 1, sizeof (UINTN));\r
1576 CopyMem (\r
1577 PadEntry->Data->AuthData,\r
1578 PadData->AuthData,\r
1579 PadData->AuthDataSize\r
1580 );\r
1581 } else {\r
1582 PadEntry->Data->AuthDataSize = 0;\r
1583 PadEntry->Data->AuthData = NULL;\r
1584 }\r
1585\r
1586 if (PadData->RevocationData != NULL) {\r
1587 PadEntry->Data->RevocationDataSize = PadData->RevocationDataSize;\r
1588 PadEntry->Data->RevocationData = (VOID *) ALIGN_POINTER (\r
1589 ((UINT8 *) (PadEntry->Data + 1) + PadData->AuthDataSize),\r
1590 sizeof (UINTN)\r
1591 );\r
1592 CopyMem (\r
1593 PadEntry->Data->RevocationData,\r
1594 PadData->RevocationData,\r
1595 PadData->RevocationDataSize\r
1596 );\r
1597 } else {\r
1598 PadEntry->Data->RevocationDataSize = 0;\r
1599 PadEntry->Data->RevocationData = NULL;\r
1600 }\r
1601 //\r
1602 // Insert the new pad entry.\r
1603 //\r
1604 InsertTailList (EntryInsertBefore, &PadEntry->List);\r
1605\r
1606 return EFI_SUCCESS;\r
1607}\r
1608\r
1609/**\r
9166f840 1610 This function lookup the data entry from IPsec SPD. Return the configuration \r
a3bcde70
HT
1611 value of the specified SPD Entry.\r
1612\r
9166f840 1613 @param[in] Selector Pointer to an entry selector which is an identifier \r
a3bcde70
HT
1614 of the SPD entry.\r
1615 @param[in, out] DataSize On output the size of data returned in Data.\r
9166f840 1616 @param[out] Data The buffer to return the contents of the IPsec \r
1617 configuration data. The type of the data buffer \r
1618 is associated with the DataType. \r
1619 \r
a3bcde70
HT
1620 @retval EFI_SUCCESS The specified configuration data was obtained successfully.\r
1621 @retval EFI_INVALID_PARAMETER Data is NULL and *DataSize is not zero.\r
1622 @retval EFI_NOT_FOUND The configuration data specified by Selector is not found.\r
1623 @retval EFI_BUFFER_TOO_SMALL The DataSize is too small for the result. DataSize has been\r
1624 updated with the size needed to complete the request.\r
1625\r
1626**/\r
1627EFI_STATUS\r
1628GetSpdEntry (\r
1629 IN EFI_IPSEC_CONFIG_SELECTOR *Selector,\r
1630 IN OUT UINTN *DataSize,\r
1631 OUT VOID *Data\r
1632 )\r
1633{\r
1634 IPSEC_SPD_ENTRY *SpdEntry;\r
1635 IPSEC_SAD_ENTRY *SadEntry;\r
1636 EFI_IPSEC_SPD_SELECTOR *SpdSel;\r
1637 EFI_IPSEC_SPD_DATA *SpdData;\r
1638 LIST_ENTRY *SpdList;\r
1639 LIST_ENTRY *SpdSas;\r
1640 LIST_ENTRY *Entry;\r
1641 UINTN RequiredSize;\r
1642\r
1643 SpdSel = &Selector->SpdSelector;\r
1644 SpdData = (EFI_IPSEC_SPD_DATA *) Data;\r
1645 SpdList = &mConfigData[IPsecConfigDataTypeSpd];\r
1646\r
1647 NET_LIST_FOR_EACH (Entry, SpdList) {\r
1648 SpdEntry = IPSEC_SPD_ENTRY_FROM_LIST (Entry);\r
1649\r
1650 //\r
9166f840 1651 // Find the required SPD entry\r
a3bcde70
HT
1652 //\r
1653 if (CompareSpdSelector (\r
1654 (EFI_IPSEC_CONFIG_SELECTOR *) SpdSel,\r
1655 (EFI_IPSEC_CONFIG_SELECTOR *) SpdEntry->Selector\r
1656 )) {\r
1657\r
1658 RequiredSize = IpSecGetSizeOfSpdData (SpdEntry->Data);\r
1659 if (*DataSize < RequiredSize) {\r
1660 *DataSize = RequiredSize;\r
1661 return EFI_BUFFER_TOO_SMALL;\r
1662 }\r
1663\r
1664 if (SpdData == NULL) {\r
1665 return EFI_INVALID_PARAMETER;\r
1666 }\r
1667\r
1668 *DataSize = RequiredSize;\r
1669\r
1670 //\r
9166f840 1671 // Extract and fill all SaId array from the SPD.sas list\r
a3bcde70
HT
1672 //\r
1673 SpdSas = &SpdEntry->Data->Sas;\r
1674 SpdData->SaIdCount = 0;\r
1675\r
1676 NET_LIST_FOR_EACH (Entry, SpdSas) {\r
1677 SadEntry = IPSEC_SAD_ENTRY_FROM_SPD (Entry);\r
1678 CopyMem (\r
1679 &SpdData->SaId[SpdData->SaIdCount++],\r
1680 SadEntry->Id,\r
1681 sizeof (EFI_IPSEC_SA_ID)\r
1682 );\r
1683 }\r
1684 //\r
9166f840 1685 // Fill the other fields in SPD data.\r
a3bcde70
HT
1686 //\r
1687 CopyMem (SpdData->Name, SpdEntry->Data->Name, sizeof (SpdData->Name));\r
1688\r
96702f88
FS
1689 SpdData->PackageFlag = SpdEntry->Data->PackageFlag;\r
1690 SpdData->TrafficDirection = SpdEntry->Data->TrafficDirection;\r
1691 SpdData->Action = SpdEntry->Data->Action;\r
1692 \r
a3bcde70
HT
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
02a758cb 2199 if (VariableNameI == NULL) {\r
2200 Status = EFI_OUT_OF_RESOURCES;\r
2201 break;\r
2202 }\r
a3bcde70
HT
2203 VariableNameISize = VariableNameISizeNew;\r
2204\r
2205 Status = gRT->GetNextVariableName (\r
2206 &VariableNameISizeNew,\r
2207 VariableNameI,\r
2208 &VendorGuidI\r
2209 );\r
2210 }\r
2211\r
2212 if (EFI_ERROR (Status)) {\r
2213 break;\r
2214 }\r
2215 //\r
2216 // Check whether the current variable is the required "ipsecconfig".\r
2217 //\r
2218 if (StrnCmp (VariableNameI, VariableName, VariableNameLength) == 0 ||\r
2219 CompareGuid (VendorGuid, &VendorGuidI)\r
2220 ) {\r
2221 //\r
2222 // Parse the variable count of the current ipsecconfig data.\r
2223 //\r
2224 VariableIndex = StrDecimalToUintn (VariableNameI + VariableNameLength);\r
2225 if (VariableIndex!= 0 && VariableIndex <= IpSecVariableInfo.VariableCount) {\r
2226 //\r
2227 // Get the variable size of the current ipsecconfig data.\r
2228 //\r
2229 DataSizeI = 0;\r
2230 Status = gRT->GetVariable (\r
2231 VariableNameI,\r
2232 VendorGuid,\r
2233 Attributes,\r
2234 &DataSizeI,\r
2235 NULL\r
2236 );\r
2237 ASSERT (Status == EFI_BUFFER_TOO_SMALL);\r
2238 //\r
2239 // Validate the variable count and variable size.\r
2240 //\r
2241 if (VariableIndex != IpSecVariableInfo.VariableCount) {\r
2242 //\r
2243 // If the varaibe is not the last one, its size should be the max\r
2244 // size of the single variable.\r
2245 //\r
2246 if (DataSizeI != IpSecVariableInfo.SingleVariableSize) {\r
2247 return EFI_ABORTED;\r
2248 }\r
2249 } else {\r
2250 if (DataSizeI != IpSecVariableInfo.VariableSize % IpSecVariableInfo.SingleVariableSize) {\r
2251 return EFI_ABORTED;\r
2252 }\r
2253 }\r
2254 //\r
2255 // Get the variable data of the current ipsecconfig data and\r
2256 // store it into user buffer continously.\r
2257 //\r
2258 Status = gRT->GetVariable (\r
2259 VariableNameI,\r
2260 VendorGuid,\r
2261 Attributes,\r
2262 &DataSizeI,\r
2263 (UINT8 *) Data + (VariableIndex - 1) * IpSecVariableInfo.SingleVariableSize\r
2264 );\r
2265 ASSERT_EFI_ERROR (Status);\r
2266 VariableCount--;\r
2267 }\r
2268 }\r
2269 }\r
2270 //\r
2271 // The VariableCount in "VariableNameInfo" varaible should have the correct\r
2272 // numbers of variables which name starts with VariableName.\r
2273 //\r
2274 if (VariableCount != 0) {\r
2275 Status = EFI_ABORTED;\r
2276 }\r
2277\r
2278ON_EXIT:\r
02a758cb 2279 if (VariableNameI != NULL) {\r
2280 FreePool (VariableNameI);\r
2281 }\r
a3bcde70
HT
2282 return Status;\r
2283}\r
2284\r
2285/**\r
2286 Set the IPsec variables.\r
2287\r
2288 Set all IPsec variables which start with the specified variable name. Those variables\r
2289 are set one by one.\r
2290\r
2291 @param[in] VariableName The name of the vendor's variable. It is a\r
2292 Null-Terminated Unicode String.\r
2293 @param[in] VendorGuid Unify identifier for vendor.\r
9166f840 2294 @param[in] Attributes Point to memory location to return the attributes of \r
a3bcde70
HT
2295 variable. If the point is NULL, the parameter would be ignored.\r
2296 @param[in] DataSize The size in bytes of Data-Buffer.\r
2297 @param[in] Data Points to the content of the variable.\r
2298\r
2299 @retval EFI_SUCCESS The firmware successfully stored the variable and its data, as\r
2300 defined by the Attributes.\r
9166f840 2301 @retval others Storing the variables failed. \r
a3bcde70
HT
2302\r
2303**/\r
2304EFI_STATUS\r
2305IpSecSetVariable (\r
2306 IN CHAR16 *VariableName,\r
2307 IN EFI_GUID *VendorGuid,\r
2308 IN UINT32 Attributes,\r
2309 IN UINTN DataSize,\r
2310 IN VOID *Data\r
2311 )\r
2312{\r
2313 EFI_STATUS Status;\r
2314 CHAR16 *VariableNameI;\r
2315 UINTN VariableNameSize;\r
2316 UINTN VariableIndex;\r
2317 IP_SEC_VARIABLE_INFO IpSecVariableInfo;\r
2318 UINT64 MaximumVariableStorageSize;\r
2319 UINT64 RemainingVariableStorageSize;\r
2320 UINT64 MaximumVariableSize;\r
2321\r
2322 Status = gRT->QueryVariableInfo (\r
2323 Attributes,\r
2324 &MaximumVariableStorageSize,\r
2325 &RemainingVariableStorageSize,\r
2326 &MaximumVariableSize\r
2327 );\r
2328 if (EFI_ERROR (Status)) {\r
2329 return Status;\r
2330 }\r
9166f840 2331 \r
a3bcde70
HT
2332 //\r
2333 // "VariableName + Info/0001/0002/... + NULL"\r
2334 //\r
2335 VariableNameSize = (StrLen (VariableName) + 5) * sizeof (CHAR16);\r
2336 VariableNameI = AllocateZeroPool (VariableNameSize);\r
2337\r
2338 if (VariableNameI == NULL) {\r
2339 Status = EFI_OUT_OF_RESOURCES;\r
2340 goto ON_EXIT;\r
2341 }\r
2342 //\r
2343 // Construct the variable of ipsecconfig general information. Like the total\r
2344 // numbers of the Ipsecconfig variables, the total size of all ipsecconfig variables.\r
2345 //\r
2346 UnicodeSPrint (VariableNameI, VariableNameSize, L"%s%s", VariableName, L"Info");\r
2347 MaximumVariableSize -= VariableNameSize;\r
9166f840 2348 \r
a3bcde70
HT
2349 IpSecVariableInfo.VariableCount = (UINT32) ((DataSize + (UINTN) MaximumVariableSize - 1) / (UINTN) MaximumVariableSize);\r
2350 IpSecVariableInfo.VariableSize = (UINT32) DataSize;\r
2351 IpSecVariableInfo.SingleVariableSize = (UINT32) MaximumVariableSize;\r
2352\r
2353 //\r
2354 // Set the variable of ipsecconfig general information.\r
2355 //\r
2356 Status = gRT->SetVariable (\r
2357 VariableNameI,\r
2358 VendorGuid,\r
2359 Attributes,\r
2360 sizeof (IpSecVariableInfo),\r
2361 &IpSecVariableInfo\r
2362 );\r
2363 if (EFI_ERROR (Status)) {\r
2364 DEBUG ((DEBUG_ERROR, "Error set ipsecconfig meta data with %r\n", Status));\r
2365 goto ON_EXIT;\r
2366 }\r
2367\r
2368 for (VariableIndex = 0; VariableIndex < IpSecVariableInfo.VariableCount; VariableIndex++) {\r
2369 //\r
2370 // Construct and set the variable of ipsecconfig data one by one.\r
2371 // The index of variable name begin from 0001, and the varaible name\r
2372 // likes "VariableName0001", "VaraiableName0002"....\r
9166f840 2373 // \r
a3bcde70
HT
2374 UnicodeSPrint (VariableNameI, VariableNameSize, L"%s%04d", VariableName, VariableIndex + 1);\r
2375 Status = gRT->SetVariable (\r
2376 VariableNameI,\r
2377 VendorGuid,\r
2378 Attributes,\r
2379 (VariableIndex == IpSecVariableInfo.VariableCount - 1) ?\r
2380 (DataSize % (UINTN) MaximumVariableSize) :\r
2381 (UINTN) MaximumVariableSize,\r
2382 (UINT8 *) Data + VariableIndex * (UINTN) MaximumVariableSize\r
2383 );\r
2384\r
2385 if (EFI_ERROR (Status)) {\r
2386 DEBUG ((DEBUG_ERROR, "Error set ipsecconfig variable data with %r\n", Status));\r
2387 goto ON_EXIT;\r
2388 }\r
2389 }\r
2390\r
2391ON_EXIT:\r
2392 if (VariableNameI != NULL) {\r
2393 FreePool (VariableNameI);\r
2394 }\r
2395\r
2396 return Status;\r
2397}\r
2398\r
2399/**\r
9166f840 2400 Return the configuration value for the EFI IPsec driver. \r
a3bcde70
HT
2401\r
2402 This function lookup the data entry from IPsec database or IKEv2 configuration\r
2403 information. The expected data type and unique identification are described in\r
9166f840 2404 DataType and Selector parameters. \r
a3bcde70
HT
2405\r
2406 @param[in] This Pointer to the EFI_IPSEC_CONFIG_PROTOCOL instance.\r
2407 @param[in] DataType The type of data to retrieve.\r
9166f840 2408 @param[in] Selector Pointer to an entry selector that is an identifier of the IPsec \r
a3bcde70
HT
2409 configuration data entry.\r
2410 @param[in, out] DataSize On output the size of data returned in Data.\r
9166f840 2411 @param[out] Data The buffer to return the contents of the IPsec configuration data. \r
2412 The type of the data buffer associated with the DataType. \r
2413 \r
a3bcde70
HT
2414 @retval EFI_SUCCESS The specified configuration data was obtained successfully.\r
2415 @retval EFI_INVALID_PARAMETER One or more of the followings are TRUE:\r
2416 - This is NULL.\r
2417 - Selector is NULL.\r
2418 - DataSize is NULL.\r
2419 - Data is NULL and *DataSize is not zero\r
2420 @retval EFI_NOT_FOUND The configuration data specified by Selector is not found.\r
2421 @retval EFI_UNSUPPORTED The specified DataType is not supported.\r
2422 @retval EFI_BUFFER_TOO_SMALL The DataSize is too small for the result. DataSize has been\r
2423 updated with the size needed to complete the request.\r
2424\r
2425**/\r
2426EFI_STATUS\r
2427EFIAPI\r
2428EfiIpSecConfigGetData (\r
2429 IN EFI_IPSEC_CONFIG_PROTOCOL *This,\r
2430 IN EFI_IPSEC_CONFIG_DATA_TYPE DataType,\r
2431 IN EFI_IPSEC_CONFIG_SELECTOR *Selector,\r
2432 IN OUT UINTN *DataSize,\r
2433 OUT VOID *Data\r
2434 )\r
2435{\r
2436 if (This == NULL || Selector == NULL || DataSize == NULL) {\r
2437 return EFI_INVALID_PARAMETER;\r
2438 }\r
2439\r
2440 if (*DataSize != 0 && Data == NULL) {\r
2441 return EFI_INVALID_PARAMETER;\r
2442 }\r
2443\r
2444 if (DataType >= IPsecConfigDataTypeMaximum) {\r
2445 return EFI_UNSUPPORTED;\r
2446 }\r
2447\r
2448 return mGetPolicyEntry[DataType](Selector, DataSize, Data);\r
2449}\r
2450\r
2451/**\r
2452 Set the security association, security policy and peer authorization configuration\r
9166f840 2453 information for the EFI IPsec driver. \r
a3bcde70
HT
2454\r
2455 This function is used to set the IPsec configuration information of type DataType for\r
2456 the EFI IPsec driver.\r
2457 The IPsec configuration data has a unique selector/identifier separately to identify\r
2458 a data entry. The selector structure depends on DataType's definition.\r
2459 Using SetData() with a Data of NULL causes the IPsec configuration data entry identified\r
9166f840 2460 by DataType and Selector to be deleted. \r
a3bcde70
HT
2461\r
2462 @param[in] This Pointer to the EFI_IPSEC_CONFIG_PROTOCOL instance.\r
2463 @param[in] DataType The type of data to be set.\r
9166f840 2464 @param[in] Selector Pointer to an entry selector on operated configuration data \r
2465 specified by DataType. A NULL Selector causes the entire \r
a3bcde70 2466 specified-type configuration information to be flushed.\r
9166f840 2467 @param[in] Data The data buffer to be set. The structure of the data buffer is \r
a3bcde70
HT
2468 associated with the DataType.\r
2469 @param[in] InsertBefore Pointer to one entry selector which describes the expected\r
2470 position the new data entry will be added. If InsertBefore is NULL,\r
2471 the new entry will be appended to the end of the database.\r
9166f840 2472 \r
a3bcde70
HT
2473 @retval EFI_SUCCESS The specified configuration entry data was set successfully.\r
2474 @retval EFI_INVALID_PARAMETER One or more of the following are TRUE:\r
2475 - This is NULL.\r
2476 @retval EFI_UNSUPPORTED The specified DataType is not supported.\r
2477 @retval EFI_OUT_OF_RESOURCED The required system resource could not be allocated.\r
2478\r
2479**/\r
2480EFI_STATUS\r
2481EFIAPI\r
2482EfiIpSecConfigSetData (\r
2483 IN EFI_IPSEC_CONFIG_PROTOCOL *This,\r
2484 IN EFI_IPSEC_CONFIG_DATA_TYPE DataType,\r
2485 IN EFI_IPSEC_CONFIG_SELECTOR *Selector,\r
2486 IN VOID *Data,\r
2487 IN EFI_IPSEC_CONFIG_SELECTOR *InsertBefore OPTIONAL\r
2488 )\r
2489{\r
2490 EFI_STATUS Status;\r
2491\r
2492 if (This == NULL) {\r
2493 return EFI_INVALID_PARAMETER;\r
2494 }\r
2495\r
2496 if (DataType >= IPsecConfigDataTypeMaximum) {\r
2497 return EFI_UNSUPPORTED;\r
2498 }\r
9166f840 2499 \r
a3bcde70
HT
2500 Status = mSetPolicyEntry[DataType](Selector, Data, InsertBefore);\r
2501\r
2502 if (!EFI_ERROR (Status) && !mSetBySelf) {\r
2503 //\r
2504 // Save the updated config data into variable.\r
2505 //\r
2506 IpSecConfigSave ();\r
2507 }\r
2508\r
2509 return Status;\r
2510}\r
2511\r
2512/**\r
9166f840 2513 Enumerates the current selector for IPsec configuration data entry. \r
a3bcde70
HT
2514\r
2515 This function is called multiple times to retrieve the entry Selector in IPsec\r
9166f840 2516 configuration database. On each call to GetNextSelector(), the next entry \r
a3bcde70 2517 Selector are retrieved into the output interface.\r
9166f840 2518 \r
2519 If the entire IPsec configuration database has been iterated, the error \r
a3bcde70 2520 EFI_NOT_FOUND is returned.\r
9166f840 2521 If the Selector buffer is too small for the next Selector copy, an \r
2522 EFI_BUFFER_TOO_SMALL error is returned, and SelectorSize is updated to reflect \r
a3bcde70
HT
2523 the size of buffer needed.\r
2524\r
2525 On the initial call to GetNextSelector() to start the IPsec configuration database\r
9166f840 2526 search, a pointer to the buffer with all zero value is passed in Selector. Calls \r
2527 to SetData() between calls to GetNextSelector may produce unpredictable results. \r
a3bcde70
HT
2528\r
2529 @param[in] This Pointer to the EFI_IPSEC_CONFIG_PROTOCOL instance.\r
2530 @param[in] DataType The type of IPsec configuration data to retrieve.\r
2531 @param[in, out] SelectorSize The size of the Selector buffer.\r
9166f840 2532 @param[in, out] Selector On input, supplies the pointer to last Selector that was \r
a3bcde70
HT
2533 returned by GetNextSelector().\r
2534 On output, returns one copy of the current entry Selector\r
9166f840 2535 of a given DataType. \r
2536 \r
a3bcde70
HT
2537 @retval EFI_SUCCESS The specified configuration data was obtained successfully.\r
2538 @retval EFI_INVALID_PARAMETER One or more of the followings are TRUE:\r
2539 - This is NULL.\r
2540 - SelectorSize is NULL.\r
2541 - Selector is NULL.\r
2542 @retval EFI_NOT_FOUND The next configuration data entry was not found.\r
2543 @retval EFI_UNSUPPORTED The specified DataType is not supported.\r
2544 @retval EFI_BUFFER_TOO_SMALL The SelectorSize is too small for the result. This parameter\r
9166f840 2545 has been updated with the size needed to complete the search \r
a3bcde70
HT
2546 request.\r
2547\r
2548**/\r
2549EFI_STATUS\r
2550EFIAPI\r
2551EfiIpSecConfigGetNextSelector (\r
2552 IN EFI_IPSEC_CONFIG_PROTOCOL *This,\r
2553 IN EFI_IPSEC_CONFIG_DATA_TYPE DataType,\r
2554 IN OUT UINTN *SelectorSize,\r
2555 IN OUT EFI_IPSEC_CONFIG_SELECTOR *Selector\r
2556 )\r
2557{\r
2558 LIST_ENTRY *Link;\r
2559 IPSEC_COMMON_POLICY_ENTRY *CommonEntry;\r
2560 BOOLEAN IsFound;\r
2561\r
2562 if (This == NULL || Selector == NULL || SelectorSize == NULL) {\r
2563 return EFI_INVALID_PARAMETER;\r
2564 }\r
2565\r
2566 if (DataType >= IPsecConfigDataTypeMaximum) {\r
2567 return EFI_UNSUPPORTED;\r
2568 }\r
2569\r
2570 IsFound = FALSE;\r
2571\r
2572 NET_LIST_FOR_EACH (Link, &mConfigData[DataType]) {\r
2573 CommonEntry = BASE_CR (Link, IPSEC_COMMON_POLICY_ENTRY, List);\r
2574\r
68d3f2fb 2575 if (IsFound || (BOOLEAN)(mIsZeroSelector[DataType](Selector))) {\r
a3bcde70
HT
2576 //\r
2577 // If found the appointed entry, then duplicate the next one and return,\r
2578 // or if the appointed entry is zero, then return the first one directly.\r
2579 //\r
2580 return mDuplicateSelector[DataType](Selector, CommonEntry->Selector, SelectorSize);\r
2581 } else {\r
2582 //\r
2583 // Set the flag if find the appointed entry.\r
2584 //\r
2585 IsFound = mCompareSelector[DataType](Selector, CommonEntry->Selector);\r
2586 }\r
2587 }\r
2588\r
2589 return EFI_NOT_FOUND;\r
2590}\r
2591\r
2592/**\r
2593 Register an event that is to be signaled whenever a configuration process on the\r
9166f840 2594 specified IPsec configuration information is done. \r
a3bcde70
HT
2595\r
2596 The register function is not surpport now and always returns EFI_UNSUPPORTED.\r
9166f840 2597 \r
a3bcde70
HT
2598 @param[in] This Pointer to the EFI_IPSEC_CONFIG_PROTOCOL instance.\r
2599 @param[in] DataType The type of data to be registered the event for.\r
2600 @param[in] Event The event to be registered.\r
9166f840 2601 \r
a3bcde70
HT
2602 @retval EFI_SUCCESS The event is registered successfully.\r
2603 @retval EFI_INVALID_PARAMETER This is NULL or Event is NULL.\r
2604 @retval EFI_ACCESS_DENIED The Event is already registered for the DataType.\r
2605 @retval EFI_UNSUPPORTED The notify registration is unsupported, or the specified\r
2606 DataType is not supported.\r
2607\r
2608**/\r
2609EFI_STATUS\r
2610EFIAPI\r
2611EfiIpSecConfigRegisterNotify (\r
2612 IN EFI_IPSEC_CONFIG_PROTOCOL *This,\r
2613 IN EFI_IPSEC_CONFIG_DATA_TYPE DataType,\r
2614 IN EFI_EVENT Event\r
2615 )\r
2616{\r
2617 return EFI_UNSUPPORTED;\r
2618}\r
2619\r
2620/**\r
2621 Remove the specified event that was previously registered on the specified IPsec\r
9166f840 2622 configuration data. \r
a3bcde70
HT
2623\r
2624 This function is not support now and alwasy return EFI_UNSUPPORTED.\r
2625\r
2626 @param[in] This Pointer to the EFI_IPSEC_CONFIG_PROTOCOL instance.\r
2627 @param[in] DataType The configuration data type to remove the registered event for.\r
2628 @param[in] Event The event to be unregistered.\r
9166f840 2629 \r
a3bcde70 2630 @retval EFI_SUCCESS The event was removed successfully.\r
9166f840 2631 @retval EFI_NOT_FOUND The Event specified by DataType could not be found in the \r
a3bcde70
HT
2632 database.\r
2633 @retval EFI_INVALID_PARAMETER This is NULL or Event is NULL.\r
2634 @retval EFI_UNSUPPORTED The notify registration is unsupported, or the specified\r
2635 DataType is not supported.\r
2636\r
2637**/\r
2638EFI_STATUS\r
2639EFIAPI\r
2640EfiIpSecConfigUnregisterNotify (\r
2641 IN EFI_IPSEC_CONFIG_PROTOCOL *This,\r
2642 IN EFI_IPSEC_CONFIG_DATA_TYPE DataType,\r
2643 IN EFI_EVENT Event\r
2644 )\r
2645{\r
2646 return EFI_UNSUPPORTED;\r
2647}\r
2648\r
2649/**\r
2650 Copy whole data in specified EFI_SIPEC_CONFIG_SELECTOR and the Data to a buffer.\r
2651\r
2652 This function is a caller defined function, and it is called by the IpSecVisitConfigData().\r
9166f840 2653 The orignal caller is IpSecConfigSave(), which calls the IpsecVisitConfigData() to \r
a3bcde70
HT
2654 copy all types of IPsec Config datas into one buffer and store this buffer into firmware in\r
2655 the form of several variables.\r
9166f840 2656 \r
a3bcde70
HT
2657 @param[in] Type A specified IPSEC_CONFIG_DATA_TYPE.\r
2658 @param[in] Selector Points to a EFI_IPSEC_CONFIG_SELECTOR to be copied\r
2659 to the buffer.\r
2660 @param[in] Data Points to data to be copied to the buffer. The\r
2661 Data type is related to the Type.\r
2662 @param[in] SelectorSize The size of the Selector.\r
2663 @param[in] DataSize The size of the Data.\r
2664 @param[in, out] Buffer The buffer to store the Selector and Data.\r
2665\r
2666 @retval EFI_SUCCESS Copy the Selector and Data to a buffer successfully.\r
2667 @retval EFI_OUT_OF_RESOURCES The required system resource could not be allocated.\r
2668\r
2669**/\r
2670EFI_STATUS\r
2671IpSecCopyPolicyEntry (\r
2672 IN EFI_IPSEC_CONFIG_DATA_TYPE Type,\r
2673 IN EFI_IPSEC_CONFIG_SELECTOR *Selector,\r
2674 IN VOID *Data,\r
2675 IN UINTN SelectorSize,\r
2676 IN UINTN DataSize,\r
2677 IN OUT IPSEC_VARIABLE_BUFFER *Buffer\r
2678 )\r
2679{\r
2680 IPSEC_VAR_ITEM_HEADER SelectorHeader;\r
2681 IPSEC_VAR_ITEM_HEADER DataHeader;\r
2682 UINTN EntrySize;\r
2683 UINT8 *TempPoint;\r
9166f840 2684 \r
a3bcde70
HT
2685 if (Type == IPsecConfigDataTypeSad) {\r
2686 //\r
9166f840 2687 // Don't save automatically-generated SA entry into variable.\r
a3bcde70 2688 //\r
9166f840 2689 if (((EFI_IPSEC_SA_DATA2 *) Data)->ManualSet == FALSE) {\r
a3bcde70
HT
2690 return EFI_SUCCESS;\r
2691 }\r
2692 }\r
2693 //\r
2694 // Increase the capacity size of the buffer if needed.\r
2695 //\r
2696 EntrySize = ALIGN_VARIABLE (sizeof (SelectorHeader));\r
2697 EntrySize = ALIGN_VARIABLE (EntrySize + SelectorSize);\r
2698 EntrySize = ALIGN_VARIABLE (EntrySize + sizeof (SelectorHeader));\r
2699 EntrySize = ALIGN_VARIABLE (EntrySize + DataSize);\r
9166f840 2700 \r
a3bcde70
HT
2701 //EntrySize = SelectorSize + DataSize + 2 * sizeof (SelectorHeader);\r
2702 if (Buffer->Capacity - Buffer->Size < EntrySize) {\r
2703 //\r
2704 // Calculate the required buffer\r
2705 //\r
2706 Buffer->Capacity += EntrySize;\r
2707 TempPoint = AllocatePool (Buffer->Capacity);\r
9166f840 2708 \r
02a758cb 2709 if (TempPoint == NULL) {\r
a3bcde70
HT
2710 return EFI_OUT_OF_RESOURCES;\r
2711 }\r
2712 //\r
2713 // Copy the old Buffer to new buffer and free the old one.\r
2714 //\r
2715 CopyMem (TempPoint, Buffer->Ptr, Buffer->Size);\r
2716 FreePool (Buffer->Ptr);\r
9166f840 2717 \r
2718 Buffer->Ptr = TempPoint; \r
a3bcde70
HT
2719 }\r
2720\r
2721 mFixPolicyEntry[Type](Selector, Data);\r
2722\r
2723 //\r
2724 // Fill the selector header and copy it into buffer.\r
2725 //\r
2726 SelectorHeader.Type = (UINT8) (Type | IPSEC_VAR_ITEM_HEADER_LOGO_BIT);\r
2727 SelectorHeader.Size = (UINT16) SelectorSize;\r
2728\r
2729 CopyMem (\r
2730 Buffer->Ptr + Buffer->Size,\r
2731 &SelectorHeader,\r
2732 sizeof (SelectorHeader)\r
2733 );\r
2734 Buffer->Size = ALIGN_VARIABLE (Buffer->Size + sizeof (SelectorHeader));\r
9166f840 2735 \r
a3bcde70
HT
2736 //\r
2737 // Copy the selector into buffer.\r
2738 //\r
2739 CopyMem (\r
2740 Buffer->Ptr + Buffer->Size,\r
2741 Selector,\r
2742 SelectorSize\r
2743 );\r
2744 Buffer->Size = ALIGN_VARIABLE (Buffer->Size + SelectorSize);\r
2745\r
2746 //\r
2747 // Fill the data header and copy it into buffer.\r
2748 //\r
2749 DataHeader.Type = (UINT8) Type;\r
2750 DataHeader.Size = (UINT16) DataSize;\r
2751\r
2752 CopyMem (\r
2753 Buffer->Ptr + Buffer->Size,\r
2754 &DataHeader,\r
2755 sizeof (DataHeader)\r
2756 );\r
2757 Buffer->Size = ALIGN_VARIABLE (Buffer->Size + sizeof (DataHeader));\r
2758 //\r
2759 // Copy the data into buffer.\r
2760 //\r
2761 CopyMem (\r
2762 Buffer->Ptr + Buffer->Size,\r
2763 Data,\r
2764 DataSize\r
2765 );\r
2766 Buffer->Size = ALIGN_VARIABLE (Buffer->Size + DataSize);\r
9166f840 2767 \r
a3bcde70
HT
2768 mUnfixPolicyEntry[Type](Selector, Data);\r
2769\r
2770 return EFI_SUCCESS;\r
2771}\r
2772\r
2773/**\r
2774 Visit all IPsec Configurations of specified Type and call the caller defined\r
2775 interface.\r
2776\r
2777 @param[in] DataType The specified IPsec Config Data Type.\r
2778 @param[in] Routine The function defined by the caller.\r
2779 @param[in] Context The data passed to the Routine.\r
2780\r
2781 @retval EFI_OUT_OF_RESOURCES The required system resource could not be allocated\r
2782 @retval EFI_SUCCESS This function completed successfully.\r
2783\r
2784**/\r
2785EFI_STATUS\r
2786IpSecVisitConfigData (\r
2787 IN EFI_IPSEC_CONFIG_DATA_TYPE DataType,\r
2788 IN IPSEC_COPY_POLICY_ENTRY Routine,\r
2789 IN VOID *Context\r
2790 )\r
2791{\r
2792 EFI_STATUS GetNextStatus;\r
2793 EFI_STATUS GetDataStatus;\r
2794 EFI_STATUS RoutineStatus;\r
2795 EFI_IPSEC_CONFIG_SELECTOR *Selector;\r
2796 VOID *Data;\r
2797 UINTN SelectorSize;\r
2798 UINTN DataSize;\r
2799 UINTN SelectorBufferSize;\r
2800 UINTN DataBufferSize;\r
2801 BOOLEAN FirstGetNext;\r
2802\r
2803 FirstGetNext = TRUE;\r
2804 DataBufferSize = 0;\r
2805 Data = NULL;\r
2806 SelectorBufferSize = sizeof (EFI_IPSEC_CONFIG_SELECTOR);\r
2807 Selector = AllocateZeroPool (SelectorBufferSize);\r
2808\r
2809 if (Selector == NULL) {\r
2810 return EFI_OUT_OF_RESOURCES;\r
2811 }\r
2812\r
2813 while (TRUE) {\r
2814 //\r
2815 // Get the real size of the selector.\r
2816 //\r
2817 SelectorSize = SelectorBufferSize;\r
2818 GetNextStatus = EfiIpSecConfigGetNextSelector (\r
2819 &mIpSecConfigInstance,\r
2820 DataType,\r
2821 &SelectorSize,\r
2822 Selector\r
2823 );\r
2824 if (GetNextStatus == EFI_BUFFER_TOO_SMALL) {\r
2825 FreePool (Selector);\r
2826 SelectorBufferSize = SelectorSize;\r
2827 //\r
2828 // Allocate zero pool for the first selector, while store the last\r
2829 // selector content for the other selectors.\r
2830 //\r
2831 if (FirstGetNext) {\r
2832 Selector = AllocateZeroPool (SelectorBufferSize);\r
2833 } else {\r
2834 Selector = AllocateCopyPool (SelectorBufferSize, Selector);\r
2835 }\r
2836\r
2837 if (Selector == NULL) {\r
2838 return EFI_OUT_OF_RESOURCES;\r
2839 }\r
2840 //\r
2841 // Get the content of the selector.\r
2842 //\r
2843 GetNextStatus = EfiIpSecConfigGetNextSelector (\r
2844 &mIpSecConfigInstance,\r
2845 DataType,\r
2846 &SelectorSize,\r
2847 Selector\r
2848 );\r
2849 }\r
2850\r
2851 if (EFI_ERROR (GetNextStatus)) {\r
2852 break;\r
2853 }\r
2854\r
2855 FirstGetNext = FALSE;\r
2856\r
2857 //\r
2858 // Get the real size of the policy entry according to the selector.\r
2859 //\r
2860 DataSize = DataBufferSize;\r
2861 GetDataStatus = EfiIpSecConfigGetData (\r
2862 &mIpSecConfigInstance,\r
2863 DataType,\r
2864 Selector,\r
2865 &DataSize,\r
2866 Data\r
2867 );\r
2868 if (GetDataStatus == EFI_BUFFER_TOO_SMALL) {\r
2869 if (Data != NULL) {\r
2870 FreePool (Data);\r
2871 }\r
2872\r
2873 DataBufferSize = DataSize;\r
2874 Data = AllocateZeroPool (DataBufferSize);\r
2875\r
2876 if (Data == NULL) {\r
2877 return EFI_OUT_OF_RESOURCES;\r
2878 }\r
2879 //\r
2880 // Get the content of the policy entry according to the selector.\r
2881 //\r
2882 GetDataStatus = EfiIpSecConfigGetData (\r
2883 &mIpSecConfigInstance,\r
2884 DataType,\r
2885 Selector,\r
2886 &DataSize,\r
2887 Data\r
2888 );\r
2889 }\r
2890\r
2891 if (EFI_ERROR (GetDataStatus)) {\r
2892 break;\r
2893 }\r
2894 //\r
2895 // Prepare the buffer of updated policy entry, which is stored in\r
2896 // the continous memory, and then save into variable later.\r
2897 //\r
2898 RoutineStatus = Routine (\r
2899 DataType,\r
2900 Selector,\r
2901 Data,\r
2902 SelectorSize,\r
2903 DataSize,\r
2904 Context\r
2905 );\r
2906 if (EFI_ERROR (RoutineStatus)) {\r
2907 break;\r
2908 }\r
2909 }\r
2910\r
2911 if (Data != NULL) {\r
2912 FreePool (Data);\r
2913 }\r
2914\r
2915 if (Selector != NULL) {\r
2916 FreePool (Selector);\r
2917 }\r
2918\r
2919 return EFI_SUCCESS;\r
2920}\r
2921\r
2922/**\r
2923 This function is the subfunction of EFIIpSecConfigSetData.\r
2924\r
2925 This function call IpSecSetVaraible to set the IPsec Configuration into the firmware.\r
2926\r
2927 @retval EFI_OUT_OF_RESOURCES The required system resource could not be allocated.\r
2928 @retval EFI_SUCCESS Saved the configration successfully.\r
2929 @retval Others Other errors were found while obtaining the variable.\r
2930\r
2931**/\r
2932EFI_STATUS\r
2933IpSecConfigSave (\r
2934 VOID\r
2935 )\r
2936{\r
2937 IPSEC_VARIABLE_BUFFER Buffer;\r
2938 EFI_STATUS Status;\r
2939 EFI_IPSEC_CONFIG_DATA_TYPE Type;\r
2940\r
2941 Buffer.Size = 0;\r
2942 Buffer.Capacity = IPSEC_DEFAULT_VARIABLE_SIZE;\r
2943 Buffer.Ptr = AllocateZeroPool (Buffer.Capacity);\r
2944\r
2945 if (Buffer.Ptr == NULL) {\r
2946 return EFI_OUT_OF_RESOURCES;\r
2947 }\r
2948 //\r
2949 // For each policy database, prepare the contious buffer to save into variable.\r
2950 //\r
2951 for (Type = IPsecConfigDataTypeSpd; Type < IPsecConfigDataTypeMaximum; Type++) {\r
2952 IpSecVisitConfigData (\r
2953 Type,\r
2954 (IPSEC_COPY_POLICY_ENTRY) IpSecCopyPolicyEntry,\r
2955 &Buffer\r
2956 );\r
2957 }\r
2958 //\r
2959 // Save the updated policy database into variable.\r
2960 //\r
2961 Status = IpSecSetVariable (\r
2962 IPSECCONFIG_VARIABLE_NAME,\r
2963 &gEfiIpSecConfigProtocolGuid,\r
2964 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
2965 Buffer.Size,\r
2966 Buffer.Ptr\r
2967 );\r
2968\r
2969 FreePool (Buffer.Ptr);\r
2970\r
2971 return Status;\r
2972}\r
2973\r
2974/**\r
2975 Get the all IPSec configuration variables and store those variables\r
2976 to the internal data structure.\r
2977\r
9166f840 2978 This founction is called by IpSecConfigInitialize() which is to intialize the \r
a3bcde70
HT
2979 IPsecConfiguration Protocol.\r
2980\r
2981 @param[in] Private Point to IPSEC_PRIVATE_DATA.\r
2982\r
2983 @retval EFI_OUT_OF_RESOURCES The required system resource could not be allocated\r
2984 @retval EFI_SUCCESS Restore the IPsec Configuration successfully.\r
2985 @retval others Other errors is found while obtaining the variable.\r
2986\r
2987**/\r
2988EFI_STATUS\r
2989IpSecConfigRestore (\r
2990 IN IPSEC_PRIVATE_DATA *Private\r
2991 )\r
2992{\r
2993 EFI_STATUS Status;\r
2994 UINTN BufferSize;\r
2995 UINT8 *Buffer;\r
2996 IPSEC_VAR_ITEM_HEADER *Header;\r
2997 UINT8 *Ptr;\r
2998 EFI_IPSEC_CONFIG_SELECTOR *Selector;\r
2999 EFI_IPSEC_CONFIG_DATA_TYPE Type;\r
3000 VOID *Data;\r
3001 UINT8 Value;\r
3002 UINTN Size;\r
3003\r
3004 Value = 0;\r
3005 Size = sizeof (Value);\r
3006 BufferSize = 0;\r
3007 Buffer = NULL;\r
3008\r
3009 Status = gRT->GetVariable (\r
3010 IPSECCONFIG_STATUS_NAME,\r
3011 &gEfiIpSecConfigProtocolGuid,\r
3012 NULL,\r
3013 &Size,\r
3014 &Value\r
3015 );\r
3016\r
3017 if (!EFI_ERROR (Status) && Value == IPSEC_STATUS_ENABLED) {\r
3018 Private->IpSec.DisabledFlag = FALSE;\r
3019 }\r
3020 //\r
3021 // Get the real size of policy database in variable.\r
3022 //\r
3023 Status = IpSecGetVariable (\r
3024 IPSECCONFIG_VARIABLE_NAME,\r
3025 &gEfiIpSecConfigProtocolGuid,\r
3026 NULL,\r
3027 &BufferSize,\r
3028 Buffer\r
3029 );\r
3030 if (Status == EFI_BUFFER_TOO_SMALL) {\r
3031\r
3032 Buffer = AllocateZeroPool (BufferSize);\r
3033 if (Buffer == NULL) {\r
3034 return EFI_OUT_OF_RESOURCES;\r
3035 }\r
3036 //\r
3037 // Get the content of policy database in variable.\r
3038 //\r
3039 Status = IpSecGetVariable (\r
3040 IPSECCONFIG_VARIABLE_NAME,\r
3041 &gEfiIpSecConfigProtocolGuid,\r
3042 NULL,\r
3043 &BufferSize,\r
3044 Buffer\r
3045 );\r
3046 if (EFI_ERROR (Status)) {\r
3047 FreePool (Buffer);\r
3048 return Status;\r
3049 }\r
3050\r
3051 for (Ptr = Buffer; Ptr < Buffer + BufferSize;) {\r
3052\r
3053 Header = (IPSEC_VAR_ITEM_HEADER *) Ptr;\r
3054 Type = (EFI_IPSEC_CONFIG_DATA_TYPE) (Header->Type & IPSEC_VAR_ITEM_HEADER_CONTENT_BIT);\r
3055 ASSERT (((Header->Type & 0x80) == IPSEC_VAR_ITEM_HEADER_LOGO_BIT) && (Type < IPsecConfigDataTypeMaximum));\r
9166f840 3056 \r
a3bcde70
HT
3057 Selector = (EFI_IPSEC_CONFIG_SELECTOR *) ALIGN_POINTER (Header + 1, sizeof (UINTN));\r
3058 Header = (IPSEC_VAR_ITEM_HEADER *) ALIGN_POINTER (\r
9166f840 3059 (UINT8 *) Selector + Header->Size, \r
a3bcde70
HT
3060 sizeof (UINTN)\r
3061 );\r
3062 ASSERT (Header->Type == Type);\r
3063\r
3064 Data = ALIGN_POINTER (Header + 1, sizeof (UINTN));\r
3065\r
3066 mUnfixPolicyEntry[Type](Selector, Data);\r
3067\r
3068 //\r
3069 // Update each policy entry according to the content in variable.\r
3070 //\r
3071 mSetBySelf = TRUE;\r
3072 Status = EfiIpSecConfigSetData (\r
3073 &Private->IpSecConfig,\r
3074 Type,\r
3075 Selector,\r
3076 Data,\r
3077 NULL\r
3078 );\r
3079 mSetBySelf = FALSE;\r
3080\r
3081 if (EFI_ERROR (Status)) {\r
3082 FreePool (Buffer);\r
3083 return Status;\r
3084 }\r
3085\r
3086 Ptr = ALIGN_POINTER ((UINT8 *) Data + Header->Size, sizeof (UINTN));\r
3087 }\r
3088\r
3089 FreePool (Buffer);\r
3090 }\r
3091\r
3092 return EFI_SUCCESS;\r
3093}\r
3094\r
3095/**\r
3096 Install and Initialize IPsecConfig protocol\r
3097\r
3098 @param[in, out] Private Pointer to IPSEC_PRIVATE_DATA. After this function finish,\r
3099 the pointer of IPsecConfig Protocol implementation will copy\r
3100 into its IPsecConfig member.\r
3101\r
3102 @retval EFI_SUCCESS Initialized the IPsecConfig Protocol successfully.\r
3103 @retval Others Initializing the IPsecConfig Protocol failed.\r
3104**/\r
3105EFI_STATUS\r
3106IpSecConfigInitialize (\r
3107 IN OUT IPSEC_PRIVATE_DATA *Private\r
3108 )\r
3109{\r
3110 EFI_IPSEC_CONFIG_DATA_TYPE Type;\r
3111\r
3112 CopyMem (\r
3113 &Private->IpSecConfig,\r
3114 &mIpSecConfigInstance,\r
3115 sizeof (EFI_IPSEC_CONFIG_PROTOCOL)\r
3116 );\r
3117\r
3118 //\r
3119 // Initialize the list head of policy database.\r
3120 //\r
3121 for (Type = IPsecConfigDataTypeSpd; Type < IPsecConfigDataTypeMaximum; Type++) {\r
3122 InitializeListHead (&mConfigData[Type]);\r
3123 }\r
3124 //\r
3125 // Restore the content of policy database according to the variable.\r
3126 //\r
3127 IpSecConfigRestore (Private);\r
3128\r
3129 return gBS->InstallMultipleProtocolInterfaces (\r
3130 &Private->Handle,\r
3131 &gEfiIpSecConfigProtocolGuid,\r
3132 &Private->IpSecConfig,\r
3133 NULL\r
3134 );\r
3135}\r