]> git.proxmox.com Git - mirror_edk2.git/blame - NetworkPkg/IpSecDxe/IpSecConfigImpl.c
NetworkPkg: TrafficDirection not saved in IPsecConfig.
[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
HT
1206 InsertTailList (&SpdEntry->Data->Sas, &SadEntry->BySpd);\r
1207 SadEntry->Data->SpdEntry = SpdEntry;\r
9166f840 1208 DuplicateSpdSelector (\r
1209 (EFI_IPSEC_CONFIG_SELECTOR *)SadEntry->Data->SpdSelector,\r
1210 (EFI_IPSEC_CONFIG_SELECTOR *)SpdEntry->Selector,\r
1211 NULL\r
1212 ); \r
a3bcde70
HT
1213 }\r
1214 }\r
1215 }\r
1216 //\r
9166f840 1217 // Insert the new SPD entry.\r
a3bcde70
HT
1218 //\r
1219 InsertTailList (EntryInsertBefore, &SpdEntry->List);\r
1220\r
1221 return EFI_SUCCESS;\r
1222}\r
1223\r
1224/**\r
1225 Set the security association information for the EFI IPsec driver.\r
1226\r
9166f840 1227 The IPsec configuration data has a unique selector/identifier separately to \r
a3bcde70
HT
1228 identify a data entry.\r
1229\r
9166f840 1230 @param[in] Selector Pointer to an entry selector on operated \r
1231 configuration data specified by DataType. \r
1232 A NULL Selector causes the entire specified-type \r
a3bcde70 1233 configuration information to be flushed.\r
9166f840 1234 @param[in] Data The data buffer to be set. The structure \r
a3bcde70 1235 of the data buffer should be EFI_IPSEC_SA_DATA.\r
9166f840 1236 @param[in] Context Pointer to one entry selector which describes \r
1237 the expected position the new data entry will \r
a3bcde70
HT
1238 be added. If Context is NULL,the new entry will\r
1239 be appended the end of database.\r
1240\r
1241 @retval EFI_OUT_OF_RESOURCED The required system resource could not be allocated.\r
1242 @retval EFI_SUCCESS The specified configuration data was obtained successfully.\r
1243\r
1244**/\r
1245EFI_STATUS\r
1246SetSadEntry (\r
1247 IN EFI_IPSEC_CONFIG_SELECTOR *Selector,\r
1248 IN VOID *Data,\r
1249 IN VOID *Context OPTIONAL\r
1250 )\r
1251{\r
1252 IPSEC_SAD_ENTRY *SadEntry;\r
1253 IPSEC_SPD_ENTRY *SpdEntry;\r
1254 LIST_ENTRY *Entry;\r
1255 LIST_ENTRY *NextEntry;\r
1256 LIST_ENTRY *SadList;\r
1257 LIST_ENTRY *SpdList;\r
1258 EFI_IPSEC_SA_ID *SaId;\r
9166f840 1259 EFI_IPSEC_SA_DATA2 *SaData;\r
a3bcde70
HT
1260 EFI_IPSEC_SA_ID *InsertBefore;\r
1261 LIST_ENTRY *EntryInsertBefore;\r
1262 UINTN SadEntrySize;\r
9166f840 1263 \r
a3bcde70 1264 SaId = (Selector == NULL) ? NULL : &Selector->SaId;\r
9166f840 1265 SaData = (Data == NULL) ? NULL : (EFI_IPSEC_SA_DATA2 *) Data;\r
a3bcde70
HT
1266 InsertBefore = (Context == NULL) ? NULL : &((EFI_IPSEC_CONFIG_SELECTOR *) Context)->SaId;\r
1267 SadList = &mConfigData[IPsecConfigDataTypeSad];\r
1268\r
1269 //\r
1270 // The default behavior is to insert the node ahead of the header.\r
1271 //\r
1272 EntryInsertBefore = SadList;\r
1273\r
1274 //\r
9166f840 1275 // Remove the existed SAD entry.\r
a3bcde70
HT
1276 //\r
1277 NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, SadList) {\r
1278\r
1279 SadEntry = IPSEC_SAD_ENTRY_FROM_LIST (Entry);\r
1280\r
9166f840 1281 if (SaId == NULL || \r
a3bcde70
HT
1282 CompareSaId (\r
1283 (EFI_IPSEC_CONFIG_SELECTOR *) SadEntry->Id,\r
1284 (EFI_IPSEC_CONFIG_SELECTOR *) SaId\r
1285 )) {\r
1286 //\r
1287 // Record the existed entry position to keep the original order.\r
1288 //\r
1289 EntryInsertBefore = SadEntry->List.ForwardLink;\r
1290\r
1291 //\r
9166f840 1292 // Update the related SAD.byspd field.\r
a3bcde70
HT
1293 //\r
1294 if (SadEntry->Data->SpdEntry != NULL) {\r
1295 RemoveEntryList (&SadEntry->BySpd);\r
1296 }\r
1297\r
1298 RemoveEntryList (&SadEntry->List);\r
1299 FreePool (SadEntry);\r
1300 }\r
1301 }\r
1302 //\r
9166f840 1303 // Return success here if only want to remove the SAD entry\r
a3bcde70
HT
1304 //\r
1305 if (SaData == NULL || SaId == NULL) {\r
1306 return EFI_SUCCESS;\r
1307 }\r
1308 //\r
1309 // Search the appointed entry position if InsertBefore is not NULL.\r
1310 //\r
1311 if (InsertBefore != NULL) {\r
1312\r
1313 NET_LIST_FOR_EACH (Entry, SadList) {\r
1314 SadEntry = IPSEC_SAD_ENTRY_FROM_LIST (Entry);\r
1315\r
1316 if (CompareSaId (\r
1317 (EFI_IPSEC_CONFIG_SELECTOR *) SadEntry->Id,\r
1318 (EFI_IPSEC_CONFIG_SELECTOR *) InsertBefore\r
1319 )) {\r
1320 EntryInsertBefore = Entry;\r
1321 break;\r
1322 }\r
1323 }\r
1324 }\r
1325\r
1326 //\r
1327 // Do Padding for different Arch.\r
1328 //\r
1329 SadEntrySize = ALIGN_VARIABLE (sizeof (IPSEC_SAD_ENTRY));\r
9166f840 1330 SadEntrySize = ALIGN_VARIABLE (SadEntrySize + sizeof (EFI_IPSEC_SA_ID));\r
a3bcde70 1331 SadEntrySize = ALIGN_VARIABLE (SadEntrySize + sizeof (IPSEC_SAD_DATA));\r
9166f840 1332 \r
a3bcde70
HT
1333 if (SaId->Proto == EfiIPsecAH) {\r
1334 SadEntrySize += SaData->AlgoInfo.AhAlgoInfo.AuthKeyLength;\r
1335 } else {\r
1336 SadEntrySize = ALIGN_VARIABLE (SadEntrySize + SaData->AlgoInfo.EspAlgoInfo.AuthKeyLength);\r
9166f840 1337 SadEntrySize += ALIGN_VARIABLE (SaData->AlgoInfo.EspAlgoInfo.EncKeyLength);\r
a3bcde70
HT
1338 }\r
1339\r
9166f840 1340 if (SaData->SpdSelector != NULL) {\r
1341 SadEntrySize += SadEntrySize + (UINTN)SIZE_OF_SPD_SELECTOR (SaData->SpdSelector);\r
1342 }\r
a3bcde70
HT
1343 SadEntry = AllocateZeroPool (SadEntrySize);\r
1344\r
1345 if (SadEntry == NULL) {\r
1346 return EFI_OUT_OF_RESOURCES;\r
1347 }\r
1348 //\r
1349 // Fix the address of Id and Data buffer and copy them, which is\r
9166f840 1350 // continous memory and close to the base structure of SAD entry.\r
a3bcde70
HT
1351 //\r
1352 SadEntry->Id = (EFI_IPSEC_SA_ID *) ALIGN_POINTER ((SadEntry + 1), sizeof (UINTN));\r
1353 SadEntry->Data = (IPSEC_SAD_DATA *) ALIGN_POINTER ((SadEntry->Id + 1), sizeof (UINTN));\r
1354\r
1355 CopyMem (SadEntry->Id, SaId, sizeof (EFI_IPSEC_SA_ID));\r
1356\r
1357 SadEntry->Data->Mode = SaData->Mode;\r
1358 SadEntry->Data->SequenceNumber = SaData->SNCount;\r
1359 SadEntry->Data->AntiReplayWindowSize = SaData->AntiReplayWindows;\r
1360\r
1361 ZeroMem (\r
1362 &SadEntry->Data->AntiReplayBitmap,\r
1363 sizeof (SadEntry->Data->AntiReplayBitmap)\r
1364 );\r
1365\r
1366 ZeroMem (\r
1367 &SadEntry->Data->AlgoInfo,\r
1368 sizeof (EFI_IPSEC_ALGO_INFO)\r
1369 );\r
1370\r
1371 SadEntry->Data->AlgoInfo.EspAlgoInfo.AuthAlgoId = SaData->AlgoInfo.EspAlgoInfo.AuthAlgoId;\r
1372 SadEntry->Data->AlgoInfo.EspAlgoInfo.AuthKeyLength = SaData->AlgoInfo.EspAlgoInfo.AuthKeyLength;\r
1373\r
1374 if (SadEntry->Data->AlgoInfo.EspAlgoInfo.AuthKeyLength != 0) {\r
1375 SadEntry->Data->AlgoInfo.EspAlgoInfo.AuthKey = (VOID *) ALIGN_POINTER ((SadEntry->Data + 1), sizeof (UINTN));\r
1376 CopyMem (\r
1377 SadEntry->Data->AlgoInfo.EspAlgoInfo.AuthKey,\r
1378 SaData->AlgoInfo.EspAlgoInfo.AuthKey,\r
1379 SadEntry->Data->AlgoInfo.EspAlgoInfo.AuthKeyLength\r
1380 );\r
1381 }\r
1382\r
1383 if (SaId->Proto == EfiIPsecESP) {\r
1384 SadEntry->Data->AlgoInfo.EspAlgoInfo.EncAlgoId = SaData->AlgoInfo.EspAlgoInfo.EncAlgoId;\r
1385 SadEntry->Data->AlgoInfo.EspAlgoInfo.EncKeyLength = SaData->AlgoInfo.EspAlgoInfo.EncKeyLength;\r
1386\r
1387 if (SadEntry->Data->AlgoInfo.EspAlgoInfo.EncKeyLength != 0) {\r
1388 SadEntry->Data->AlgoInfo.EspAlgoInfo.EncKey = (VOID *) ALIGN_POINTER (\r
9166f840 1389 ((UINT8 *) (SadEntry->Data + 1) + \r
a3bcde70
HT
1390 SadEntry->Data->AlgoInfo.EspAlgoInfo.AuthKeyLength),\r
1391 sizeof (UINTN)\r
1392 );\r
1393 CopyMem (\r
1394 SadEntry->Data->AlgoInfo.EspAlgoInfo.EncKey,\r
1395 SaData->AlgoInfo.EspAlgoInfo.EncKey,\r
1396 SadEntry->Data->AlgoInfo.EspAlgoInfo.EncKeyLength\r
1397 );\r
1398 }\r
1399 }\r
1400\r
1401 CopyMem (\r
1402 &SadEntry->Data->SaLifetime,\r
1403 &SaData->SaLifetime,\r
1404 sizeof (EFI_IPSEC_SA_LIFETIME)\r
1405 );\r
1406\r
1407 SadEntry->Data->PathMTU = SaData->PathMTU;\r
9166f840 1408 SadEntry->Data->SpdSelector = NULL;\r
a3bcde70
HT
1409 SadEntry->Data->ESNEnabled = FALSE;\r
1410 SadEntry->Data->ManualSet = SaData->ManualSet;\r
1411\r
1412 //\r
9166f840 1413 // Copy Tunnel Source/Destination Address\r
1414 //\r
1415 if (SaData->Mode == EfiIPsecTunnel) {\r
1416 CopyMem (\r
1417 &SadEntry->Data->TunnelDestAddress,\r
1418 &SaData->TunnelDestinationAddress,\r
1419 sizeof (EFI_IP_ADDRESS)\r
1420 );\r
1421 CopyMem (\r
1422 &SadEntry->Data->TunnelSourceAddress,\r
1423 &SaData->TunnelSourceAddress,\r
1424 sizeof (EFI_IP_ADDRESS)\r
1425 );\r
1426 }\r
1427 //\r
1428 // Update the spd.sas list of the spd entry specified by SAD selector\r
a3bcde70
HT
1429 //\r
1430 SpdList = &mConfigData[IPsecConfigDataTypeSpd];\r
1431\r
1432 for (Entry = SpdList->ForwardLink; Entry != SpdList && SaData->SpdSelector != NULL; Entry = Entry->ForwardLink) {\r
1433\r
1434 SpdEntry = IPSEC_SPD_ENTRY_FROM_LIST (Entry);\r
9166f840 1435 if (IsSubSpdSelector (\r
1436 (EFI_IPSEC_CONFIG_SELECTOR *) SaData->SpdSelector,\r
1437 (EFI_IPSEC_CONFIG_SELECTOR *) SpdEntry->Selector\r
a3bcde70
HT
1438 ) && SpdEntry->Data->Action == EfiIPsecActionProtect) {\r
1439 SadEntry->Data->SpdEntry = SpdEntry;\r
9166f840 1440 SadEntry->Data->SpdSelector = (EFI_IPSEC_SPD_SELECTOR *)((UINT8 *)SadEntry +\r
1441 SadEntrySize -\r
1442 (UINTN)SIZE_OF_SPD_SELECTOR (SaData->SpdSelector)\r
1443 );\r
1444 DuplicateSpdSelector (\r
1445 (EFI_IPSEC_CONFIG_SELECTOR *) SadEntry->Data->SpdSelector,\r
1446 (EFI_IPSEC_CONFIG_SELECTOR *) SaData->SpdSelector,\r
1447 NULL\r
1448 );\r
a3bcde70
HT
1449 InsertTailList (&SpdEntry->Data->Sas, &SadEntry->BySpd);\r
1450 }\r
1451 }\r
1452 //\r
9166f840 1453 // Insert the new SAD entry.\r
a3bcde70
HT
1454 //\r
1455 InsertTailList (EntryInsertBefore, &SadEntry->List);\r
1456\r
1457 return EFI_SUCCESS;\r
1458}\r
1459\r
1460/**\r
1461 Set the peer authorization configuration information for the EFI IPsec driver.\r
1462\r
9166f840 1463 The IPsec configuration data has a unique selector/identifier separately to \r
a3bcde70
HT
1464 identify a data entry.\r
1465\r
9166f840 1466 @param[in] Selector Pointer to an entry selector on operated \r
1467 configuration data specified by DataType. \r
1468 A NULL Selector causes the entire specified-type \r
a3bcde70 1469 configuration information to be flushed.\r
9166f840 1470 @param[in] Data The data buffer to be set. The structure \r
a3bcde70 1471 of the data buffer should be EFI_IPSEC_PAD_DATA.\r
9166f840 1472 @param[in] Context Pointer to one entry selector that describes \r
1473 the expected position the new data entry will \r
a3bcde70
HT
1474 be added. If Context is NULL, the new entry will\r
1475 be appended the end of database.\r
1476\r
1477 @retval EFI_OUT_OF_RESOURCES The required system resources could not be allocated.\r
1478 @retval EFI_SUCCESS The specified configuration data was obtained successfully.\r
1479\r
1480**/\r
1481EFI_STATUS\r
1482SetPadEntry (\r
1483 IN EFI_IPSEC_CONFIG_SELECTOR *Selector,\r
1484 IN VOID *Data,\r
1485 IN VOID *Context OPTIONAL\r
1486 )\r
1487{\r
1488 IPSEC_PAD_ENTRY *PadEntry;\r
1489 EFI_IPSEC_PAD_ID *PadId;\r
1490 EFI_IPSEC_PAD_DATA *PadData;\r
1491 LIST_ENTRY *PadList;\r
1492 LIST_ENTRY *Entry;\r
1493 LIST_ENTRY *NextEntry;\r
1494 EFI_IPSEC_PAD_ID *InsertBefore;\r
1495 LIST_ENTRY *EntryInsertBefore;\r
1496 UINTN PadEntrySize;\r
9166f840 1497 \r
a3bcde70
HT
1498 PadId = (Selector == NULL) ? NULL : &Selector->PadId;\r
1499 PadData = (Data == NULL) ? NULL : (EFI_IPSEC_PAD_DATA *) Data;\r
1500 InsertBefore = (Context == NULL) ? NULL : &((EFI_IPSEC_CONFIG_SELECTOR *) Context)->PadId;\r
1501 PadList = &mConfigData[IPsecConfigDataTypePad];\r
1502\r
1503 //\r
1504 // The default behavior is to insert the node ahead of the header.\r
1505 //\r
1506 EntryInsertBefore = PadList;\r
1507\r
1508 //\r
1509 // Remove the existed pad entry.\r
1510 //\r
1511 NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, PadList) {\r
1512\r
1513 PadEntry = IPSEC_PAD_ENTRY_FROM_LIST (Entry);\r
1514\r
9166f840 1515 if (PadId == NULL || \r
a3bcde70
HT
1516 ComparePadId ((EFI_IPSEC_CONFIG_SELECTOR *) PadEntry->Id, (EFI_IPSEC_CONFIG_SELECTOR *) PadId)\r
1517 ) {\r
1518 //\r
1519 // Record the existed entry position to keep the original order.\r
1520 //\r
1521 EntryInsertBefore = PadEntry->List.ForwardLink;\r
1522 RemoveEntryList (&PadEntry->List);\r
1523\r
1524 FreePool (PadEntry);\r
1525 }\r
1526 }\r
1527 //\r
1528 // Return success here if only want to remove the pad entry\r
1529 //\r
1530 if (PadData == NULL || PadId == NULL) {\r
1531 return EFI_SUCCESS;\r
1532 }\r
1533 //\r
1534 // Search the appointed entry position if InsertBefore is not NULL.\r
1535 //\r
1536 if (InsertBefore != NULL) {\r
1537\r
1538 NET_LIST_FOR_EACH (Entry, PadList) {\r
1539 PadEntry = IPSEC_PAD_ENTRY_FROM_LIST (Entry);\r
1540\r
1541 if (ComparePadId (\r
1542 (EFI_IPSEC_CONFIG_SELECTOR *) PadEntry->Id,\r
1543 (EFI_IPSEC_CONFIG_SELECTOR *) InsertBefore\r
1544 )) {\r
1545 EntryInsertBefore = Entry;\r
1546 break;\r
1547 }\r
1548 }\r
1549 }\r
1550\r
1551 //\r
1552 // Do PADDING for different arch.\r
1553 //\r
1554 PadEntrySize = ALIGN_VARIABLE (sizeof (IPSEC_PAD_ENTRY));\r
1555 PadEntrySize = ALIGN_VARIABLE (PadEntrySize + sizeof (EFI_IPSEC_PAD_ID));\r
1556 PadEntrySize = ALIGN_VARIABLE (PadEntrySize + sizeof (EFI_IPSEC_PAD_DATA));\r
1557 PadEntrySize = ALIGN_VARIABLE (PadEntrySize + (PadData->AuthData != NULL ? PadData->AuthDataSize : 0));\r
1558 PadEntrySize += PadData->RevocationData != NULL ? PadData->RevocationDataSize : 0;\r
1559\r
1560 PadEntry = AllocateZeroPool (PadEntrySize);\r
1561\r
1562 if (PadEntry == NULL) {\r
1563 return EFI_OUT_OF_RESOURCES;\r
1564 }\r
1565 //\r
1566 // Fix the address of Id and Data buffer and copy them, which is\r
1567 // continous memory and close to the base structure of pad entry.\r
1568 //\r
1569 PadEntry->Id = (EFI_IPSEC_PAD_ID *) ALIGN_POINTER ((PadEntry + 1), sizeof (UINTN));\r
1570 PadEntry->Data = (EFI_IPSEC_PAD_DATA *) ALIGN_POINTER ((PadEntry->Id + 1), sizeof (UINTN));\r
1571\r
1572 CopyMem (PadEntry->Id, PadId, sizeof (EFI_IPSEC_PAD_ID));\r
1573\r
1574 PadEntry->Data->AuthProtocol = PadData->AuthProtocol;\r
1575 PadEntry->Data->AuthMethod = PadData->AuthMethod;\r
1576 PadEntry->Data->IkeIdFlag = PadData->IkeIdFlag;\r
1577\r
1578 if (PadData->AuthData != NULL) {\r
1579 PadEntry->Data->AuthDataSize = PadData->AuthDataSize;\r
1580 PadEntry->Data->AuthData = (VOID *) ALIGN_POINTER (PadEntry->Data + 1, sizeof (UINTN));\r
1581 CopyMem (\r
1582 PadEntry->Data->AuthData,\r
1583 PadData->AuthData,\r
1584 PadData->AuthDataSize\r
1585 );\r
1586 } else {\r
1587 PadEntry->Data->AuthDataSize = 0;\r
1588 PadEntry->Data->AuthData = NULL;\r
1589 }\r
1590\r
1591 if (PadData->RevocationData != NULL) {\r
1592 PadEntry->Data->RevocationDataSize = PadData->RevocationDataSize;\r
1593 PadEntry->Data->RevocationData = (VOID *) ALIGN_POINTER (\r
1594 ((UINT8 *) (PadEntry->Data + 1) + PadData->AuthDataSize),\r
1595 sizeof (UINTN)\r
1596 );\r
1597 CopyMem (\r
1598 PadEntry->Data->RevocationData,\r
1599 PadData->RevocationData,\r
1600 PadData->RevocationDataSize\r
1601 );\r
1602 } else {\r
1603 PadEntry->Data->RevocationDataSize = 0;\r
1604 PadEntry->Data->RevocationData = NULL;\r
1605 }\r
1606 //\r
1607 // Insert the new pad entry.\r
1608 //\r
1609 InsertTailList (EntryInsertBefore, &PadEntry->List);\r
1610\r
1611 return EFI_SUCCESS;\r
1612}\r
1613\r
1614/**\r
9166f840 1615 This function lookup the data entry from IPsec SPD. Return the configuration \r
a3bcde70
HT
1616 value of the specified SPD Entry.\r
1617\r
9166f840 1618 @param[in] Selector Pointer to an entry selector which is an identifier \r
a3bcde70
HT
1619 of the SPD entry.\r
1620 @param[in, out] DataSize On output the size of data returned in Data.\r
9166f840 1621 @param[out] Data The buffer to return the contents of the IPsec \r
1622 configuration data. The type of the data buffer \r
1623 is associated with the DataType. \r
1624 \r
a3bcde70
HT
1625 @retval EFI_SUCCESS The specified configuration data was obtained successfully.\r
1626 @retval EFI_INVALID_PARAMETER Data is NULL and *DataSize is not zero.\r
1627 @retval EFI_NOT_FOUND The configuration data specified by Selector is not found.\r
1628 @retval EFI_BUFFER_TOO_SMALL The DataSize is too small for the result. DataSize has been\r
1629 updated with the size needed to complete the request.\r
1630\r
1631**/\r
1632EFI_STATUS\r
1633GetSpdEntry (\r
1634 IN EFI_IPSEC_CONFIG_SELECTOR *Selector,\r
1635 IN OUT UINTN *DataSize,\r
1636 OUT VOID *Data\r
1637 )\r
1638{\r
1639 IPSEC_SPD_ENTRY *SpdEntry;\r
1640 IPSEC_SAD_ENTRY *SadEntry;\r
1641 EFI_IPSEC_SPD_SELECTOR *SpdSel;\r
1642 EFI_IPSEC_SPD_DATA *SpdData;\r
1643 LIST_ENTRY *SpdList;\r
1644 LIST_ENTRY *SpdSas;\r
1645 LIST_ENTRY *Entry;\r
1646 UINTN RequiredSize;\r
1647\r
1648 SpdSel = &Selector->SpdSelector;\r
1649 SpdData = (EFI_IPSEC_SPD_DATA *) Data;\r
1650 SpdList = &mConfigData[IPsecConfigDataTypeSpd];\r
1651\r
1652 NET_LIST_FOR_EACH (Entry, SpdList) {\r
1653 SpdEntry = IPSEC_SPD_ENTRY_FROM_LIST (Entry);\r
1654\r
1655 //\r
9166f840 1656 // Find the required SPD entry\r
a3bcde70
HT
1657 //\r
1658 if (CompareSpdSelector (\r
1659 (EFI_IPSEC_CONFIG_SELECTOR *) SpdSel,\r
1660 (EFI_IPSEC_CONFIG_SELECTOR *) SpdEntry->Selector\r
1661 )) {\r
1662\r
1663 RequiredSize = IpSecGetSizeOfSpdData (SpdEntry->Data);\r
1664 if (*DataSize < RequiredSize) {\r
1665 *DataSize = RequiredSize;\r
1666 return EFI_BUFFER_TOO_SMALL;\r
1667 }\r
1668\r
1669 if (SpdData == NULL) {\r
1670 return EFI_INVALID_PARAMETER;\r
1671 }\r
1672\r
1673 *DataSize = RequiredSize;\r
1674\r
1675 //\r
9166f840 1676 // Extract and fill all SaId array from the SPD.sas list\r
a3bcde70
HT
1677 //\r
1678 SpdSas = &SpdEntry->Data->Sas;\r
1679 SpdData->SaIdCount = 0;\r
1680\r
1681 NET_LIST_FOR_EACH (Entry, SpdSas) {\r
1682 SadEntry = IPSEC_SAD_ENTRY_FROM_SPD (Entry);\r
1683 CopyMem (\r
1684 &SpdData->SaId[SpdData->SaIdCount++],\r
1685 SadEntry->Id,\r
1686 sizeof (EFI_IPSEC_SA_ID)\r
1687 );\r
1688 }\r
1689 //\r
9166f840 1690 // Fill the other fields in SPD data.\r
a3bcde70
HT
1691 //\r
1692 CopyMem (SpdData->Name, SpdEntry->Data->Name, sizeof (SpdData->Name));\r
1693\r
96702f88
FS
1694 SpdData->PackageFlag = SpdEntry->Data->PackageFlag;\r
1695 SpdData->TrafficDirection = SpdEntry->Data->TrafficDirection;\r
1696 SpdData->Action = SpdEntry->Data->Action;\r
1697 \r
a3bcde70
HT
1698 if (SpdData->Action != EfiIPsecActionProtect) {\r
1699 SpdData->ProcessingPolicy = NULL;\r
1700 } else {\r
1701 SpdData->ProcessingPolicy = (EFI_IPSEC_PROCESS_POLICY *) ((UINT8 *) SpdData + sizeof (EFI_IPSEC_SPD_DATA) + (SpdData->SaIdCount - 1) * sizeof (EFI_IPSEC_SA_ID));\r
1702\r
1703 IpSecDuplicateProcessPolicy (\r
1704 SpdData->ProcessingPolicy,\r
1705 SpdEntry->Data->ProcessingPolicy\r
1706 );\r
1707 }\r
1708\r
1709 return EFI_SUCCESS;\r
1710 }\r
1711 }\r
1712\r
1713 return EFI_NOT_FOUND;\r
1714}\r
1715\r
1716/**\r
9166f840 1717 This function lookup the data entry from IPsec SAD. Return the configuration \r
a3bcde70
HT
1718 value of the specified SAD Entry.\r
1719\r
9166f840 1720 @param[in] Selector Pointer to an entry selector which is an identifier \r
a3bcde70
HT
1721 of the SAD entry.\r
1722 @param[in, out] DataSize On output, the size of data returned in Data.\r
9166f840 1723 @param[out] Data The buffer to return the contents of the IPsec \r
1724 configuration data. The type of the data buffer \r
1725 is associated with the DataType. \r
1726 \r
a3bcde70
HT
1727 @retval EFI_SUCCESS The specified configuration data was obtained successfully.\r
1728 @retval EFI_NOT_FOUND The configuration data specified by Selector is not found.\r
1729 @retval EFI_BUFFER_TOO_SMALL The DataSize is too small for the result. DataSize has been\r
1730 updated with the size needed to complete the request.\r
1731\r
1732**/\r
1733EFI_STATUS\r
1734GetSadEntry (\r
1735 IN EFI_IPSEC_CONFIG_SELECTOR *Selector,\r
1736 IN OUT UINTN *DataSize,\r
1737 OUT VOID *Data\r
1738 )\r
1739{\r
1740 IPSEC_SAD_ENTRY *SadEntry;\r
1741 LIST_ENTRY *Entry;\r
1742 LIST_ENTRY *SadList;\r
1743 EFI_IPSEC_SA_ID *SaId;\r
9166f840 1744 EFI_IPSEC_SA_DATA2 *SaData;\r
a3bcde70
HT
1745 UINTN RequiredSize;\r
1746\r
1747 SaId = &Selector->SaId;\r
9166f840 1748 SaData = (EFI_IPSEC_SA_DATA2 *) Data;\r
a3bcde70
HT
1749 SadList = &mConfigData[IPsecConfigDataTypeSad];\r
1750\r
1751 NET_LIST_FOR_EACH (Entry, SadList) {\r
1752 SadEntry = IPSEC_SAD_ENTRY_FROM_LIST (Entry);\r
1753\r
1754 //\r
9166f840 1755 // Find the required SAD entry.\r
a3bcde70
HT
1756 //\r
1757 if (CompareSaId (\r
1758 (EFI_IPSEC_CONFIG_SELECTOR *) SaId,\r
1759 (EFI_IPSEC_CONFIG_SELECTOR *) SadEntry->Id\r
1760 )) {\r
1761 //\r
9166f840 1762 // Calculate the required size of the SAD entry.\r
a3bcde70
HT
1763 // Data Layout is follows:\r
1764 // |EFI_IPSEC_SA_DATA\r
1765 // |AuthKey\r
1766 // |EncryptKey (Optional)\r
9166f840 1767 // |SpdSelector (Optional) \r
1768 // \r
1769 RequiredSize = ALIGN_VARIABLE (sizeof (EFI_IPSEC_SA_DATA2));\r
a3bcde70
HT
1770\r
1771 if (SaId->Proto == EfiIPsecAH) {\r
1772 RequiredSize = ALIGN_VARIABLE (RequiredSize + SadEntry->Data->AlgoInfo.AhAlgoInfo.AuthKeyLength);\r
1773 } else {\r
1774 RequiredSize = ALIGN_VARIABLE (RequiredSize + SadEntry->Data->AlgoInfo.EspAlgoInfo.AuthKeyLength);\r
1775 RequiredSize = ALIGN_VARIABLE (RequiredSize + SadEntry->Data->AlgoInfo.EspAlgoInfo.EncKeyLength);\r
1776 }\r
1777\r
9166f840 1778 if (SadEntry->Data->SpdSelector != NULL) {\r
1779 RequiredSize += SIZE_OF_SPD_SELECTOR (SadEntry->Data->SpdSelector);\r
a3bcde70 1780 }\r
9166f840 1781 \r
a3bcde70
HT
1782 if (*DataSize < RequiredSize) {\r
1783 *DataSize = RequiredSize;\r
1784 return EFI_BUFFER_TOO_SMALL;\r
1785 }\r
9166f840 1786 \r
a3bcde70 1787 //\r
9166f840 1788 // Fill the data fields of SAD entry.\r
a3bcde70
HT
1789 //\r
1790 *DataSize = RequiredSize;\r
1791 SaData->Mode = SadEntry->Data->Mode;\r
1792 SaData->SNCount = SadEntry->Data->SequenceNumber;\r
1793 SaData->AntiReplayWindows = SadEntry->Data->AntiReplayWindowSize;\r
1794\r
1795 CopyMem (\r
1796 &SaData->SaLifetime,\r
1797 &SadEntry->Data->SaLifetime,\r
1798 sizeof (EFI_IPSEC_SA_LIFETIME)\r
1799 );\r
1800\r
1801 ZeroMem (\r
1802 &SaData->AlgoInfo,\r
1803 sizeof (EFI_IPSEC_ALGO_INFO)\r
1804 );\r
1805\r
1806 if (SaId->Proto == EfiIPsecAH) {\r
1807 //\r
1808 // Copy AH alogrithm INFO to SaData\r
1809 //\r
1810 SaData->AlgoInfo.AhAlgoInfo.AuthAlgoId = SadEntry->Data->AlgoInfo.AhAlgoInfo.AuthAlgoId;\r
1811 SaData->AlgoInfo.AhAlgoInfo.AuthKeyLength = SadEntry->Data->AlgoInfo.AhAlgoInfo.AuthKeyLength;\r
1812 if (SaData->AlgoInfo.AhAlgoInfo.AuthKeyLength != 0) {\r
1813 SaData->AlgoInfo.AhAlgoInfo.AuthKey = (VOID *) ALIGN_POINTER ((SaData + 1), sizeof (UINTN));\r
1814 CopyMem (\r
1815 SaData->AlgoInfo.AhAlgoInfo.AuthKey,\r
1816 SadEntry->Data->AlgoInfo.AhAlgoInfo.AuthKey,\r
1817 SaData->AlgoInfo.AhAlgoInfo.AuthKeyLength\r
1818 );\r
1819 }\r
1820 } else if (SaId->Proto == EfiIPsecESP) {\r
1821 //\r
1822 // Copy ESP alogrithem INFO to SaData\r
1823 //\r
1824 SaData->AlgoInfo.EspAlgoInfo.AuthAlgoId = SadEntry->Data->AlgoInfo.EspAlgoInfo.AuthAlgoId;\r
1825 SaData->AlgoInfo.EspAlgoInfo.AuthKeyLength = SadEntry->Data->AlgoInfo.EspAlgoInfo.AuthKeyLength;\r
1826 if (SaData->AlgoInfo.EspAlgoInfo.AuthKeyLength != 0) {\r
1827 SaData->AlgoInfo.EspAlgoInfo.AuthKey = (VOID *) ALIGN_POINTER ((SaData + 1), sizeof (UINTN));\r
1828 CopyMem (\r
1829 SaData->AlgoInfo.EspAlgoInfo.AuthKey,\r
1830 SadEntry->Data->AlgoInfo.EspAlgoInfo.AuthKey,\r
1831 SaData->AlgoInfo.EspAlgoInfo.AuthKeyLength\r
1832 );\r
1833 }\r
1834\r
1835 SaData->AlgoInfo.EspAlgoInfo.EncAlgoId = SadEntry->Data->AlgoInfo.EspAlgoInfo.EncAlgoId;\r
1836 SaData->AlgoInfo.EspAlgoInfo.EncKeyLength = SadEntry->Data->AlgoInfo.EspAlgoInfo.EncKeyLength;\r
1837\r
1838 if (SaData->AlgoInfo.EspAlgoInfo.EncKeyLength != 0) {\r
1839 SaData->AlgoInfo.EspAlgoInfo.EncKey = (VOID *) ALIGN_POINTER (\r
1840 ((UINT8 *) (SaData + 1) +\r
1841 SaData->AlgoInfo.EspAlgoInfo.AuthKeyLength),\r
1842 sizeof (UINTN)\r
1843 );\r
1844 CopyMem (\r
1845 SaData->AlgoInfo.EspAlgoInfo.EncKey,\r
1846 SadEntry->Data->AlgoInfo.EspAlgoInfo.EncKey,\r
1847 SaData->AlgoInfo.EspAlgoInfo.EncKeyLength\r
1848 );\r
1849 }\r
1850 }\r
1851\r
1852 SaData->PathMTU = SadEntry->Data->PathMTU;\r
1853\r
1854 //\r
9166f840 1855 // Fill Tunnel Address if it is Tunnel Mode\r
a3bcde70 1856 //\r
9166f840 1857 if (SadEntry->Data->Mode == EfiIPsecTunnel) {\r
1858 CopyMem (\r
1859 &SaData->TunnelDestinationAddress,\r
1860 &SadEntry->Data->TunnelDestAddress,\r
1861 sizeof (EFI_IP_ADDRESS)\r
1862 );\r
1863 CopyMem (\r
1864 &SaData->TunnelSourceAddress,\r
1865 &SadEntry->Data->TunnelSourceAddress,\r
1866 sizeof (EFI_IP_ADDRESS)\r
1867 );\r
1868 }\r
1869 //\r
1870 // Fill the spd selector field of SAD data\r
1871 //\r
1872 if (SadEntry->Data->SpdSelector != NULL) {\r
a3bcde70
HT
1873\r
1874 SaData->SpdSelector = (EFI_IPSEC_SPD_SELECTOR *) (\r
1875 (UINT8 *)SaData +\r
1876 RequiredSize -\r
9166f840 1877 SIZE_OF_SPD_SELECTOR (SadEntry->Data->SpdSelector)\r
a3bcde70 1878 );\r
9166f840 1879 \r
a3bcde70
HT
1880 DuplicateSpdSelector (\r
1881 (EFI_IPSEC_CONFIG_SELECTOR *) SaData->SpdSelector,\r
9166f840 1882 (EFI_IPSEC_CONFIG_SELECTOR *) SadEntry->Data->SpdSelector,\r
a3bcde70
HT
1883 NULL\r
1884 );\r
1885\r
1886 } else {\r
1887\r
1888 SaData->SpdSelector = NULL;\r
1889 }\r
1890\r
1891 SaData->ManualSet = SadEntry->Data->ManualSet;\r
1892\r
1893 return EFI_SUCCESS;\r
1894 }\r
1895 }\r
1896\r
1897 return EFI_NOT_FOUND;\r
1898}\r
1899\r
1900/**\r
9166f840 1901 This function lookup the data entry from IPsec PAD. Return the configuration \r
a3bcde70
HT
1902 value of the specified PAD Entry.\r
1903\r
9166f840 1904 @param[in] Selector Pointer to an entry selector which is an identifier \r
a3bcde70
HT
1905 of the PAD entry.\r
1906 @param[in, out] DataSize On output the size of data returned in Data.\r
9166f840 1907 @param[out] Data The buffer to return the contents of the IPsec \r
1908 configuration data. The type of the data buffer \r
1909 is associated with the DataType. \r
1910 \r
a3bcde70
HT
1911 @retval EFI_SUCCESS The specified configuration data was obtained successfully.\r
1912 @retval EFI_NOT_FOUND The configuration data specified by Selector is not found.\r
1913 @retval EFI_BUFFER_TOO_SMALL The DataSize is too small for the result. DataSize has been\r
1914 updated with the size needed to complete the request.\r
1915\r
1916**/\r
1917EFI_STATUS\r
1918GetPadEntry (\r
1919 IN EFI_IPSEC_CONFIG_SELECTOR *Selector,\r
1920 IN OUT UINTN *DataSize,\r
1921 OUT VOID *Data\r
1922 )\r
1923{\r
1924 IPSEC_PAD_ENTRY *PadEntry;\r
1925 LIST_ENTRY *PadList;\r
1926 LIST_ENTRY *Entry;\r
1927 EFI_IPSEC_PAD_ID *PadId;\r
1928 EFI_IPSEC_PAD_DATA *PadData;\r
1929 UINTN RequiredSize;\r
1930\r
1931 PadId = &Selector->PadId;\r
1932 PadData = (EFI_IPSEC_PAD_DATA *) Data;\r
1933 PadList = &mConfigData[IPsecConfigDataTypePad];\r
1934\r
1935 NET_LIST_FOR_EACH (Entry, PadList) {\r
1936 PadEntry = IPSEC_PAD_ENTRY_FROM_LIST (Entry);\r
1937\r
1938 //\r
1939 // Find the required pad entry.\r
1940 //\r
1941 if (ComparePadId (\r
1942 (EFI_IPSEC_CONFIG_SELECTOR *) PadId,\r
1943 (EFI_IPSEC_CONFIG_SELECTOR *) PadEntry->Id\r
1944 )) {\r
1945 //\r
1946 // Calculate the required size of the pad entry.\r
1947 //\r
1948 RequiredSize = ALIGN_VARIABLE (sizeof (EFI_IPSEC_PAD_DATA));\r
1949 RequiredSize = ALIGN_VARIABLE (RequiredSize + PadEntry->Data->AuthDataSize);\r
1950 RequiredSize += PadEntry->Data->RevocationDataSize;\r
1951\r
1952 if (*DataSize < RequiredSize) {\r
1953 *DataSize = RequiredSize;\r
1954 return EFI_BUFFER_TOO_SMALL;\r
1955 }\r
1956 //\r
1957 // Fill the data fields of pad entry\r
1958 //\r
1959 *DataSize = RequiredSize;\r
1960 PadData->AuthProtocol = PadEntry->Data->AuthProtocol;\r
1961 PadData->AuthMethod = PadEntry->Data->AuthMethod;\r
1962 PadData->IkeIdFlag = PadEntry->Data->IkeIdFlag;\r
1963\r
1964 //\r
1965 // Copy Authentication data.\r
1966 //\r
1967 if (PadEntry->Data->AuthData != NULL) {\r
1968\r
1969 PadData->AuthDataSize = PadEntry->Data->AuthDataSize;\r
1970 PadData->AuthData = (VOID *) ALIGN_POINTER ((PadData + 1), sizeof (UINTN));\r
1971 CopyMem (\r
1972 PadData->AuthData,\r
1973 PadEntry->Data->AuthData,\r
1974 PadData->AuthDataSize\r
1975 );\r
1976 } else {\r
1977\r
1978 PadData->AuthDataSize = 0;\r
1979 PadData->AuthData = NULL;\r
1980 }\r
1981 //\r
1982 // Copy Revocation Data.\r
1983 //\r
1984 if (PadEntry->Data->RevocationData != NULL) {\r
1985\r
1986 PadData->RevocationDataSize = PadEntry->Data->RevocationDataSize;\r
1987 PadData->RevocationData = (VOID *) ALIGN_POINTER (\r
1988 ((UINT8 *) (PadData + 1) + PadData->AuthDataSize),\r
1989 sizeof (UINTN)\r
1990 );\r
1991 CopyMem (\r
1992 PadData->RevocationData,\r
1993 PadEntry->Data->RevocationData,\r
1994 PadData->RevocationDataSize\r
1995 );\r
1996 } else {\r
1997\r
1998 PadData->RevocationDataSize = 0;\r
1999 PadData->RevocationData = NULL;\r
2000 }\r
2001\r
2002 return EFI_SUCCESS;\r
2003 }\r
2004 }\r
2005\r
2006 return EFI_NOT_FOUND;\r
2007}\r
2008\r
2009/**\r
2010 Copy Source Process Policy to the Destination Process Policy.\r
2011\r
2012 @param[in] Dst Pointer to the Source Process Policy.\r
2013 @param[in] Src Pointer to the Destination Process Policy.\r
2014\r
2015**/\r
2016VOID\r
2017IpSecDuplicateProcessPolicy (\r
2018 IN EFI_IPSEC_PROCESS_POLICY *Dst,\r
2019 IN EFI_IPSEC_PROCESS_POLICY *Src\r
2020 )\r
2021{\r
2022 //\r
2023 // Firstly copy the structure content itself.\r
2024 //\r
2025 CopyMem (Dst, Src, sizeof (EFI_IPSEC_PROCESS_POLICY));\r
2026\r
2027 //\r
2028 // Recursively copy the tunnel option if needed.\r
2029 //\r
2030 if (Dst->Mode != EfiIPsecTunnel) {\r
2031 ASSERT (Dst->TunnelOption == NULL);\r
2032 } else {\r
2033 Dst->TunnelOption = (EFI_IPSEC_TUNNEL_OPTION *) ALIGN_POINTER ((Dst + 1), sizeof (UINTN));\r
2034 CopyMem (\r
2035 Dst->TunnelOption,\r
2036 Src->TunnelOption,\r
2037 sizeof (EFI_IPSEC_TUNNEL_OPTION)\r
2038 );\r
2039 }\r
2040}\r
2041\r
2042/**\r
2043 Calculate the a whole size of EFI_IPSEC_SPD_DATA, which includes the buffer size pointed\r
2044 to by the pointer members.\r
2045\r
2046 @param[in] SpdData Pointer to a specified EFI_IPSEC_SPD_DATA.\r
2047\r
2048 @return the whole size the specified EFI_IPSEC_SPD_DATA.\r
2049\r
2050**/\r
2051UINTN\r
2052IpSecGetSizeOfEfiSpdData (\r
2053 IN EFI_IPSEC_SPD_DATA *SpdData\r
2054 )\r
2055{\r
2056 UINTN Size;\r
2057\r
2058 Size = ALIGN_VARIABLE (sizeof (IPSEC_SPD_DATA));\r
2059\r
2060 if (SpdData->Action == EfiIPsecActionProtect) {\r
2061 Size = ALIGN_VARIABLE (Size + sizeof (EFI_IPSEC_PROCESS_POLICY));\r
2062\r
2063 if (SpdData->ProcessingPolicy->Mode == EfiIPsecTunnel) {\r
2064 Size = ALIGN_VARIABLE (Size + sizeof (EFI_IPSEC_TUNNEL_OPTION));\r
2065 }\r
2066 }\r
2067\r
2068 return Size;\r
2069}\r
2070\r
2071/**\r
2072 Calculate the a whole size of IPSEC_SPD_DATA which includes the buffer size pointed\r
9166f840 2073 to by the pointer members and the buffer size used by the Sa List. \r
a3bcde70
HT
2074\r
2075 @param[in] SpdData Pointer to the specified IPSEC_SPD_DATA.\r
2076\r
2077 @return the whole size of IPSEC_SPD_DATA.\r
2078\r
2079**/\r
2080UINTN\r
2081IpSecGetSizeOfSpdData (\r
2082 IN IPSEC_SPD_DATA *SpdData\r
2083 )\r
2084{\r
2085 UINTN Size;\r
2086 LIST_ENTRY *Link;\r
2087\r
2088 Size = sizeof (EFI_IPSEC_SPD_DATA) - sizeof (EFI_IPSEC_SA_ID);\r
2089\r
2090 if (SpdData->Action == EfiIPsecActionProtect) {\r
2091 Size += sizeof (EFI_IPSEC_PROCESS_POLICY);\r
2092\r
2093 if (SpdData->ProcessingPolicy->Mode == EfiIPsecTunnel) {\r
2094 Size += sizeof (EFI_IPSEC_TUNNEL_OPTION);\r
2095 }\r
2096 }\r
2097\r
2098 NET_LIST_FOR_EACH (Link, &SpdData->Sas) {\r
2099 Size += sizeof (EFI_IPSEC_SA_ID);\r
2100 }\r
2101\r
2102 return Size;\r
2103}\r
2104\r
2105/**\r
2106 Get the IPsec Variable.\r
2107\r
2108 Get the all variables which start with the string contained in VaraiableName.\r
9166f840 2109 Since all IPsec related variable store in continual space, those kinds of \r
2110 variable can be searched by the EfiGetNextVariableName. Those variables also are \r
a3bcde70 2111 returned in a continual buffer.\r
9166f840 2112 \r
a3bcde70
HT
2113 @param[in] VariableName Pointer to a specified Variable Name.\r
2114 @param[in] VendorGuid Pointer to a specified Vendor Guid.\r
9166f840 2115 @param[in] Attributes Point to memory location to return the attributes \r
2116 of variable. If the point is NULL, the parameter \r
a3bcde70 2117 would be ignored.\r
9166f840 2118 @param[in, out] DataSize As input, point to the maximum size of return \r
2119 Data-Buffer. As output, point to the actual \r
a3bcde70
HT
2120 size of the returned Data-Buffer.\r
2121 @param[in] Data Point to return Data-Buffer.\r
9166f840 2122 \r
a3bcde70 2123 @retval EFI_ABORTED If the Variable size which contained in the variable\r
9166f840 2124 structure doesn't match the variable size obtained \r
a3bcde70
HT
2125 from the EFIGetVariable.\r
2126 @retval EFI_BUFFER_TOO_SMALL The DataSize is too small for the result. DataSize has\r
9166f840 2127 been updated with the size needed to complete the request. \r
a3bcde70
HT
2128 @retval EFI_SUCCESS The function completed successfully.\r
2129 @retval others Other errors found during the variable getting.\r
2130**/\r
2131EFI_STATUS\r
2132IpSecGetVariable (\r
2133 IN CHAR16 *VariableName,\r
2134 IN EFI_GUID *VendorGuid,\r
2135 IN UINT32 *Attributes, OPTIONAL\r
2136 IN OUT UINTN *DataSize,\r
2137 IN VOID *Data\r
2138 )\r
2139{\r
2140 EFI_STATUS Status;\r
2141 EFI_GUID VendorGuidI;\r
2142 UINTN VariableNameLength;\r
2143 CHAR16 *VariableNameI;\r
2144 UINTN VariableNameISize;\r
2145 UINTN VariableNameISizeNew;\r
2146 UINTN VariableIndex;\r
2147 UINTN VariableCount;\r
2148 IP_SEC_VARIABLE_INFO IpSecVariableInfo;\r
2149 UINTN DataSizeI;\r
2150\r
2151 //\r
2152 // The variable name constructor is "VariableName + Info/0001/0002/... + NULL".\r
2153 // So the varialbe name is like "VariableNameInfo", "VariableName0001", ...\r
2154 // "VariableNameNULL".\r
2155 //\r
2156 VariableNameLength = StrLen (VariableName);\r
2157 VariableNameISize = (VariableNameLength + 5) * sizeof (CHAR16);\r
2158 VariableNameI = AllocateZeroPool (VariableNameISize);\r
2159 ASSERT (VariableNameI != NULL);\r
9166f840 2160 \r
a3bcde70
HT
2161 //\r
2162 // Construct the varible name of ipsecconfig meta data.\r
2163 //\r
2164 UnicodeSPrint (VariableNameI, VariableNameISize, L"%s%s", VariableName, L"Info");\r
2165\r
2166 DataSizeI = sizeof (IpSecVariableInfo);\r
2167\r
2168 Status = gRT->GetVariable (\r
2169 VariableNameI,\r
2170 VendorGuid,\r
2171 Attributes,\r
2172 &DataSizeI,\r
2173 &IpSecVariableInfo\r
2174 );\r
2175 if (EFI_ERROR (Status)) {\r
2176 goto ON_EXIT;\r
2177 }\r
2178\r
2179 if (*DataSize < IpSecVariableInfo.VariableSize) {\r
2180 *DataSize = IpSecVariableInfo.VariableSize;\r
2181 Status = EFI_BUFFER_TOO_SMALL;\r
2182 goto ON_EXIT;\r
2183 }\r
2184\r
2185 VariableCount = IpSecVariableInfo.VariableCount;\r
2186 VariableNameI[0] = L'\0';\r
2187\r
2188 while (VariableCount != 0) {\r
2189 //\r
2190 // Get the variable name one by one in the variable database.\r
2191 //\r
2192 VariableNameISizeNew = VariableNameISize;\r
2193 Status = gRT->GetNextVariableName (\r
2194 &VariableNameISizeNew,\r
2195 VariableNameI,\r
2196 &VendorGuidI\r
2197 );\r
2198 if (Status == EFI_BUFFER_TOO_SMALL) {\r
2199 VariableNameI = ReallocatePool (\r
2200 VariableNameISize,\r
2201 VariableNameISizeNew,\r
2202 VariableNameI\r
2203 );\r
02a758cb 2204 if (VariableNameI == NULL) {\r
2205 Status = EFI_OUT_OF_RESOURCES;\r
2206 break;\r
2207 }\r
a3bcde70
HT
2208 VariableNameISize = VariableNameISizeNew;\r
2209\r
2210 Status = gRT->GetNextVariableName (\r
2211 &VariableNameISizeNew,\r
2212 VariableNameI,\r
2213 &VendorGuidI\r
2214 );\r
2215 }\r
2216\r
2217 if (EFI_ERROR (Status)) {\r
2218 break;\r
2219 }\r
2220 //\r
2221 // Check whether the current variable is the required "ipsecconfig".\r
2222 //\r
2223 if (StrnCmp (VariableNameI, VariableName, VariableNameLength) == 0 ||\r
2224 CompareGuid (VendorGuid, &VendorGuidI)\r
2225 ) {\r
2226 //\r
2227 // Parse the variable count of the current ipsecconfig data.\r
2228 //\r
2229 VariableIndex = StrDecimalToUintn (VariableNameI + VariableNameLength);\r
2230 if (VariableIndex!= 0 && VariableIndex <= IpSecVariableInfo.VariableCount) {\r
2231 //\r
2232 // Get the variable size of the current ipsecconfig data.\r
2233 //\r
2234 DataSizeI = 0;\r
2235 Status = gRT->GetVariable (\r
2236 VariableNameI,\r
2237 VendorGuid,\r
2238 Attributes,\r
2239 &DataSizeI,\r
2240 NULL\r
2241 );\r
2242 ASSERT (Status == EFI_BUFFER_TOO_SMALL);\r
2243 //\r
2244 // Validate the variable count and variable size.\r
2245 //\r
2246 if (VariableIndex != IpSecVariableInfo.VariableCount) {\r
2247 //\r
2248 // If the varaibe is not the last one, its size should be the max\r
2249 // size of the single variable.\r
2250 //\r
2251 if (DataSizeI != IpSecVariableInfo.SingleVariableSize) {\r
2252 return EFI_ABORTED;\r
2253 }\r
2254 } else {\r
2255 if (DataSizeI != IpSecVariableInfo.VariableSize % IpSecVariableInfo.SingleVariableSize) {\r
2256 return EFI_ABORTED;\r
2257 }\r
2258 }\r
2259 //\r
2260 // Get the variable data of the current ipsecconfig data and\r
2261 // store it into user buffer continously.\r
2262 //\r
2263 Status = gRT->GetVariable (\r
2264 VariableNameI,\r
2265 VendorGuid,\r
2266 Attributes,\r
2267 &DataSizeI,\r
2268 (UINT8 *) Data + (VariableIndex - 1) * IpSecVariableInfo.SingleVariableSize\r
2269 );\r
2270 ASSERT_EFI_ERROR (Status);\r
2271 VariableCount--;\r
2272 }\r
2273 }\r
2274 }\r
2275 //\r
2276 // The VariableCount in "VariableNameInfo" varaible should have the correct\r
2277 // numbers of variables which name starts with VariableName.\r
2278 //\r
2279 if (VariableCount != 0) {\r
2280 Status = EFI_ABORTED;\r
2281 }\r
2282\r
2283ON_EXIT:\r
02a758cb 2284 if (VariableNameI != NULL) {\r
2285 FreePool (VariableNameI);\r
2286 }\r
a3bcde70
HT
2287 return Status;\r
2288}\r
2289\r
2290/**\r
2291 Set the IPsec variables.\r
2292\r
2293 Set all IPsec variables which start with the specified variable name. Those variables\r
2294 are set one by one.\r
2295\r
2296 @param[in] VariableName The name of the vendor's variable. It is a\r
2297 Null-Terminated Unicode String.\r
2298 @param[in] VendorGuid Unify identifier for vendor.\r
9166f840 2299 @param[in] Attributes Point to memory location to return the attributes of \r
a3bcde70
HT
2300 variable. If the point is NULL, the parameter would be ignored.\r
2301 @param[in] DataSize The size in bytes of Data-Buffer.\r
2302 @param[in] Data Points to the content of the variable.\r
2303\r
2304 @retval EFI_SUCCESS The firmware successfully stored the variable and its data, as\r
2305 defined by the Attributes.\r
9166f840 2306 @retval others Storing the variables failed. \r
a3bcde70
HT
2307\r
2308**/\r
2309EFI_STATUS\r
2310IpSecSetVariable (\r
2311 IN CHAR16 *VariableName,\r
2312 IN EFI_GUID *VendorGuid,\r
2313 IN UINT32 Attributes,\r
2314 IN UINTN DataSize,\r
2315 IN VOID *Data\r
2316 )\r
2317{\r
2318 EFI_STATUS Status;\r
2319 CHAR16 *VariableNameI;\r
2320 UINTN VariableNameSize;\r
2321 UINTN VariableIndex;\r
2322 IP_SEC_VARIABLE_INFO IpSecVariableInfo;\r
2323 UINT64 MaximumVariableStorageSize;\r
2324 UINT64 RemainingVariableStorageSize;\r
2325 UINT64 MaximumVariableSize;\r
2326\r
2327 Status = gRT->QueryVariableInfo (\r
2328 Attributes,\r
2329 &MaximumVariableStorageSize,\r
2330 &RemainingVariableStorageSize,\r
2331 &MaximumVariableSize\r
2332 );\r
2333 if (EFI_ERROR (Status)) {\r
2334 return Status;\r
2335 }\r
9166f840 2336 \r
a3bcde70
HT
2337 //\r
2338 // "VariableName + Info/0001/0002/... + NULL"\r
2339 //\r
2340 VariableNameSize = (StrLen (VariableName) + 5) * sizeof (CHAR16);\r
2341 VariableNameI = AllocateZeroPool (VariableNameSize);\r
2342\r
2343 if (VariableNameI == NULL) {\r
2344 Status = EFI_OUT_OF_RESOURCES;\r
2345 goto ON_EXIT;\r
2346 }\r
2347 //\r
2348 // Construct the variable of ipsecconfig general information. Like the total\r
2349 // numbers of the Ipsecconfig variables, the total size of all ipsecconfig variables.\r
2350 //\r
2351 UnicodeSPrint (VariableNameI, VariableNameSize, L"%s%s", VariableName, L"Info");\r
2352 MaximumVariableSize -= VariableNameSize;\r
9166f840 2353 \r
a3bcde70
HT
2354 IpSecVariableInfo.VariableCount = (UINT32) ((DataSize + (UINTN) MaximumVariableSize - 1) / (UINTN) MaximumVariableSize);\r
2355 IpSecVariableInfo.VariableSize = (UINT32) DataSize;\r
2356 IpSecVariableInfo.SingleVariableSize = (UINT32) MaximumVariableSize;\r
2357\r
2358 //\r
2359 // Set the variable of ipsecconfig general information.\r
2360 //\r
2361 Status = gRT->SetVariable (\r
2362 VariableNameI,\r
2363 VendorGuid,\r
2364 Attributes,\r
2365 sizeof (IpSecVariableInfo),\r
2366 &IpSecVariableInfo\r
2367 );\r
2368 if (EFI_ERROR (Status)) {\r
2369 DEBUG ((DEBUG_ERROR, "Error set ipsecconfig meta data with %r\n", Status));\r
2370 goto ON_EXIT;\r
2371 }\r
2372\r
2373 for (VariableIndex = 0; VariableIndex < IpSecVariableInfo.VariableCount; VariableIndex++) {\r
2374 //\r
2375 // Construct and set the variable of ipsecconfig data one by one.\r
2376 // The index of variable name begin from 0001, and the varaible name\r
2377 // likes "VariableName0001", "VaraiableName0002"....\r
9166f840 2378 // \r
a3bcde70
HT
2379 UnicodeSPrint (VariableNameI, VariableNameSize, L"%s%04d", VariableName, VariableIndex + 1);\r
2380 Status = gRT->SetVariable (\r
2381 VariableNameI,\r
2382 VendorGuid,\r
2383 Attributes,\r
2384 (VariableIndex == IpSecVariableInfo.VariableCount - 1) ?\r
2385 (DataSize % (UINTN) MaximumVariableSize) :\r
2386 (UINTN) MaximumVariableSize,\r
2387 (UINT8 *) Data + VariableIndex * (UINTN) MaximumVariableSize\r
2388 );\r
2389\r
2390 if (EFI_ERROR (Status)) {\r
2391 DEBUG ((DEBUG_ERROR, "Error set ipsecconfig variable data with %r\n", Status));\r
2392 goto ON_EXIT;\r
2393 }\r
2394 }\r
2395\r
2396ON_EXIT:\r
2397 if (VariableNameI != NULL) {\r
2398 FreePool (VariableNameI);\r
2399 }\r
2400\r
2401 return Status;\r
2402}\r
2403\r
2404/**\r
9166f840 2405 Return the configuration value for the EFI IPsec driver. \r
a3bcde70
HT
2406\r
2407 This function lookup the data entry from IPsec database or IKEv2 configuration\r
2408 information. The expected data type and unique identification are described in\r
9166f840 2409 DataType and Selector parameters. \r
a3bcde70
HT
2410\r
2411 @param[in] This Pointer to the EFI_IPSEC_CONFIG_PROTOCOL instance.\r
2412 @param[in] DataType The type of data to retrieve.\r
9166f840 2413 @param[in] Selector Pointer to an entry selector that is an identifier of the IPsec \r
a3bcde70
HT
2414 configuration data entry.\r
2415 @param[in, out] DataSize On output the size of data returned in Data.\r
9166f840 2416 @param[out] Data The buffer to return the contents of the IPsec configuration data. \r
2417 The type of the data buffer associated with the DataType. \r
2418 \r
a3bcde70
HT
2419 @retval EFI_SUCCESS The specified configuration data was obtained successfully.\r
2420 @retval EFI_INVALID_PARAMETER One or more of the followings are TRUE:\r
2421 - This is NULL.\r
2422 - Selector is NULL.\r
2423 - DataSize is NULL.\r
2424 - Data is NULL and *DataSize is not zero\r
2425 @retval EFI_NOT_FOUND The configuration data specified by Selector is not found.\r
2426 @retval EFI_UNSUPPORTED The specified DataType is not supported.\r
2427 @retval EFI_BUFFER_TOO_SMALL The DataSize is too small for the result. DataSize has been\r
2428 updated with the size needed to complete the request.\r
2429\r
2430**/\r
2431EFI_STATUS\r
2432EFIAPI\r
2433EfiIpSecConfigGetData (\r
2434 IN EFI_IPSEC_CONFIG_PROTOCOL *This,\r
2435 IN EFI_IPSEC_CONFIG_DATA_TYPE DataType,\r
2436 IN EFI_IPSEC_CONFIG_SELECTOR *Selector,\r
2437 IN OUT UINTN *DataSize,\r
2438 OUT VOID *Data\r
2439 )\r
2440{\r
2441 if (This == NULL || Selector == NULL || DataSize == NULL) {\r
2442 return EFI_INVALID_PARAMETER;\r
2443 }\r
2444\r
2445 if (*DataSize != 0 && Data == NULL) {\r
2446 return EFI_INVALID_PARAMETER;\r
2447 }\r
2448\r
2449 if (DataType >= IPsecConfigDataTypeMaximum) {\r
2450 return EFI_UNSUPPORTED;\r
2451 }\r
2452\r
2453 return mGetPolicyEntry[DataType](Selector, DataSize, Data);\r
2454}\r
2455\r
2456/**\r
2457 Set the security association, security policy and peer authorization configuration\r
9166f840 2458 information for the EFI IPsec driver. \r
a3bcde70
HT
2459\r
2460 This function is used to set the IPsec configuration information of type DataType for\r
2461 the EFI IPsec driver.\r
2462 The IPsec configuration data has a unique selector/identifier separately to identify\r
2463 a data entry. The selector structure depends on DataType's definition.\r
2464 Using SetData() with a Data of NULL causes the IPsec configuration data entry identified\r
9166f840 2465 by DataType and Selector to be deleted. \r
a3bcde70
HT
2466\r
2467 @param[in] This Pointer to the EFI_IPSEC_CONFIG_PROTOCOL instance.\r
2468 @param[in] DataType The type of data to be set.\r
9166f840 2469 @param[in] Selector Pointer to an entry selector on operated configuration data \r
2470 specified by DataType. A NULL Selector causes the entire \r
a3bcde70 2471 specified-type configuration information to be flushed.\r
9166f840 2472 @param[in] Data The data buffer to be set. The structure of the data buffer is \r
a3bcde70
HT
2473 associated with the DataType.\r
2474 @param[in] InsertBefore Pointer to one entry selector which describes the expected\r
2475 position the new data entry will be added. If InsertBefore is NULL,\r
2476 the new entry will be appended to the end of the database.\r
9166f840 2477 \r
a3bcde70
HT
2478 @retval EFI_SUCCESS The specified configuration entry data was set successfully.\r
2479 @retval EFI_INVALID_PARAMETER One or more of the following are TRUE:\r
2480 - This is NULL.\r
2481 @retval EFI_UNSUPPORTED The specified DataType is not supported.\r
2482 @retval EFI_OUT_OF_RESOURCED The required system resource could not be allocated.\r
2483\r
2484**/\r
2485EFI_STATUS\r
2486EFIAPI\r
2487EfiIpSecConfigSetData (\r
2488 IN EFI_IPSEC_CONFIG_PROTOCOL *This,\r
2489 IN EFI_IPSEC_CONFIG_DATA_TYPE DataType,\r
2490 IN EFI_IPSEC_CONFIG_SELECTOR *Selector,\r
2491 IN VOID *Data,\r
2492 IN EFI_IPSEC_CONFIG_SELECTOR *InsertBefore OPTIONAL\r
2493 )\r
2494{\r
2495 EFI_STATUS Status;\r
2496\r
2497 if (This == NULL) {\r
2498 return EFI_INVALID_PARAMETER;\r
2499 }\r
2500\r
2501 if (DataType >= IPsecConfigDataTypeMaximum) {\r
2502 return EFI_UNSUPPORTED;\r
2503 }\r
9166f840 2504 \r
a3bcde70
HT
2505 Status = mSetPolicyEntry[DataType](Selector, Data, InsertBefore);\r
2506\r
2507 if (!EFI_ERROR (Status) && !mSetBySelf) {\r
2508 //\r
2509 // Save the updated config data into variable.\r
2510 //\r
2511 IpSecConfigSave ();\r
2512 }\r
2513\r
2514 return Status;\r
2515}\r
2516\r
2517/**\r
9166f840 2518 Enumerates the current selector for IPsec configuration data entry. \r
a3bcde70
HT
2519\r
2520 This function is called multiple times to retrieve the entry Selector in IPsec\r
9166f840 2521 configuration database. On each call to GetNextSelector(), the next entry \r
a3bcde70 2522 Selector are retrieved into the output interface.\r
9166f840 2523 \r
2524 If the entire IPsec configuration database has been iterated, the error \r
a3bcde70 2525 EFI_NOT_FOUND is returned.\r
9166f840 2526 If the Selector buffer is too small for the next Selector copy, an \r
2527 EFI_BUFFER_TOO_SMALL error is returned, and SelectorSize is updated to reflect \r
a3bcde70
HT
2528 the size of buffer needed.\r
2529\r
2530 On the initial call to GetNextSelector() to start the IPsec configuration database\r
9166f840 2531 search, a pointer to the buffer with all zero value is passed in Selector. Calls \r
2532 to SetData() between calls to GetNextSelector may produce unpredictable results. \r
a3bcde70
HT
2533\r
2534 @param[in] This Pointer to the EFI_IPSEC_CONFIG_PROTOCOL instance.\r
2535 @param[in] DataType The type of IPsec configuration data to retrieve.\r
2536 @param[in, out] SelectorSize The size of the Selector buffer.\r
9166f840 2537 @param[in, out] Selector On input, supplies the pointer to last Selector that was \r
a3bcde70
HT
2538 returned by GetNextSelector().\r
2539 On output, returns one copy of the current entry Selector\r
9166f840 2540 of a given DataType. \r
2541 \r
a3bcde70
HT
2542 @retval EFI_SUCCESS The specified configuration data was obtained successfully.\r
2543 @retval EFI_INVALID_PARAMETER One or more of the followings are TRUE:\r
2544 - This is NULL.\r
2545 - SelectorSize is NULL.\r
2546 - Selector is NULL.\r
2547 @retval EFI_NOT_FOUND The next configuration data entry was not found.\r
2548 @retval EFI_UNSUPPORTED The specified DataType is not supported.\r
2549 @retval EFI_BUFFER_TOO_SMALL The SelectorSize is too small for the result. This parameter\r
9166f840 2550 has been updated with the size needed to complete the search \r
a3bcde70
HT
2551 request.\r
2552\r
2553**/\r
2554EFI_STATUS\r
2555EFIAPI\r
2556EfiIpSecConfigGetNextSelector (\r
2557 IN EFI_IPSEC_CONFIG_PROTOCOL *This,\r
2558 IN EFI_IPSEC_CONFIG_DATA_TYPE DataType,\r
2559 IN OUT UINTN *SelectorSize,\r
2560 IN OUT EFI_IPSEC_CONFIG_SELECTOR *Selector\r
2561 )\r
2562{\r
2563 LIST_ENTRY *Link;\r
2564 IPSEC_COMMON_POLICY_ENTRY *CommonEntry;\r
2565 BOOLEAN IsFound;\r
2566\r
2567 if (This == NULL || Selector == NULL || SelectorSize == NULL) {\r
2568 return EFI_INVALID_PARAMETER;\r
2569 }\r
2570\r
2571 if (DataType >= IPsecConfigDataTypeMaximum) {\r
2572 return EFI_UNSUPPORTED;\r
2573 }\r
2574\r
2575 IsFound = FALSE;\r
2576\r
2577 NET_LIST_FOR_EACH (Link, &mConfigData[DataType]) {\r
2578 CommonEntry = BASE_CR (Link, IPSEC_COMMON_POLICY_ENTRY, List);\r
2579\r
68d3f2fb 2580 if (IsFound || (BOOLEAN)(mIsZeroSelector[DataType](Selector))) {\r
a3bcde70
HT
2581 //\r
2582 // If found the appointed entry, then duplicate the next one and return,\r
2583 // or if the appointed entry is zero, then return the first one directly.\r
2584 //\r
2585 return mDuplicateSelector[DataType](Selector, CommonEntry->Selector, SelectorSize);\r
2586 } else {\r
2587 //\r
2588 // Set the flag if find the appointed entry.\r
2589 //\r
2590 IsFound = mCompareSelector[DataType](Selector, CommonEntry->Selector);\r
2591 }\r
2592 }\r
2593\r
2594 return EFI_NOT_FOUND;\r
2595}\r
2596\r
2597/**\r
2598 Register an event that is to be signaled whenever a configuration process on the\r
9166f840 2599 specified IPsec configuration information is done. \r
a3bcde70
HT
2600\r
2601 The register function is not surpport now and always returns EFI_UNSUPPORTED.\r
9166f840 2602 \r
a3bcde70
HT
2603 @param[in] This Pointer to the EFI_IPSEC_CONFIG_PROTOCOL instance.\r
2604 @param[in] DataType The type of data to be registered the event for.\r
2605 @param[in] Event The event to be registered.\r
9166f840 2606 \r
a3bcde70
HT
2607 @retval EFI_SUCCESS The event is registered successfully.\r
2608 @retval EFI_INVALID_PARAMETER This is NULL or Event is NULL.\r
2609 @retval EFI_ACCESS_DENIED The Event is already registered for the DataType.\r
2610 @retval EFI_UNSUPPORTED The notify registration is unsupported, or the specified\r
2611 DataType is not supported.\r
2612\r
2613**/\r
2614EFI_STATUS\r
2615EFIAPI\r
2616EfiIpSecConfigRegisterNotify (\r
2617 IN EFI_IPSEC_CONFIG_PROTOCOL *This,\r
2618 IN EFI_IPSEC_CONFIG_DATA_TYPE DataType,\r
2619 IN EFI_EVENT Event\r
2620 )\r
2621{\r
2622 return EFI_UNSUPPORTED;\r
2623}\r
2624\r
2625/**\r
2626 Remove the specified event that was previously registered on the specified IPsec\r
9166f840 2627 configuration data. \r
a3bcde70
HT
2628\r
2629 This function is not support now and alwasy return EFI_UNSUPPORTED.\r
2630\r
2631 @param[in] This Pointer to the EFI_IPSEC_CONFIG_PROTOCOL instance.\r
2632 @param[in] DataType The configuration data type to remove the registered event for.\r
2633 @param[in] Event The event to be unregistered.\r
9166f840 2634 \r
a3bcde70 2635 @retval EFI_SUCCESS The event was removed successfully.\r
9166f840 2636 @retval EFI_NOT_FOUND The Event specified by DataType could not be found in the \r
a3bcde70
HT
2637 database.\r
2638 @retval EFI_INVALID_PARAMETER This is NULL or Event is NULL.\r
2639 @retval EFI_UNSUPPORTED The notify registration is unsupported, or the specified\r
2640 DataType is not supported.\r
2641\r
2642**/\r
2643EFI_STATUS\r
2644EFIAPI\r
2645EfiIpSecConfigUnregisterNotify (\r
2646 IN EFI_IPSEC_CONFIG_PROTOCOL *This,\r
2647 IN EFI_IPSEC_CONFIG_DATA_TYPE DataType,\r
2648 IN EFI_EVENT Event\r
2649 )\r
2650{\r
2651 return EFI_UNSUPPORTED;\r
2652}\r
2653\r
2654/**\r
2655 Copy whole data in specified EFI_SIPEC_CONFIG_SELECTOR and the Data to a buffer.\r
2656\r
2657 This function is a caller defined function, and it is called by the IpSecVisitConfigData().\r
9166f840 2658 The orignal caller is IpSecConfigSave(), which calls the IpsecVisitConfigData() to \r
a3bcde70
HT
2659 copy all types of IPsec Config datas into one buffer and store this buffer into firmware in\r
2660 the form of several variables.\r
9166f840 2661 \r
a3bcde70
HT
2662 @param[in] Type A specified IPSEC_CONFIG_DATA_TYPE.\r
2663 @param[in] Selector Points to a EFI_IPSEC_CONFIG_SELECTOR to be copied\r
2664 to the buffer.\r
2665 @param[in] Data Points to data to be copied to the buffer. The\r
2666 Data type is related to the Type.\r
2667 @param[in] SelectorSize The size of the Selector.\r
2668 @param[in] DataSize The size of the Data.\r
2669 @param[in, out] Buffer The buffer to store the Selector and Data.\r
2670\r
2671 @retval EFI_SUCCESS Copy the Selector and Data to a buffer successfully.\r
2672 @retval EFI_OUT_OF_RESOURCES The required system resource could not be allocated.\r
2673\r
2674**/\r
2675EFI_STATUS\r
2676IpSecCopyPolicyEntry (\r
2677 IN EFI_IPSEC_CONFIG_DATA_TYPE Type,\r
2678 IN EFI_IPSEC_CONFIG_SELECTOR *Selector,\r
2679 IN VOID *Data,\r
2680 IN UINTN SelectorSize,\r
2681 IN UINTN DataSize,\r
2682 IN OUT IPSEC_VARIABLE_BUFFER *Buffer\r
2683 )\r
2684{\r
2685 IPSEC_VAR_ITEM_HEADER SelectorHeader;\r
2686 IPSEC_VAR_ITEM_HEADER DataHeader;\r
2687 UINTN EntrySize;\r
2688 UINT8 *TempPoint;\r
9166f840 2689 \r
a3bcde70
HT
2690 if (Type == IPsecConfigDataTypeSad) {\r
2691 //\r
9166f840 2692 // Don't save automatically-generated SA entry into variable.\r
a3bcde70 2693 //\r
9166f840 2694 if (((EFI_IPSEC_SA_DATA2 *) Data)->ManualSet == FALSE) {\r
a3bcde70
HT
2695 return EFI_SUCCESS;\r
2696 }\r
2697 }\r
2698 //\r
2699 // Increase the capacity size of the buffer if needed.\r
2700 //\r
2701 EntrySize = ALIGN_VARIABLE (sizeof (SelectorHeader));\r
2702 EntrySize = ALIGN_VARIABLE (EntrySize + SelectorSize);\r
2703 EntrySize = ALIGN_VARIABLE (EntrySize + sizeof (SelectorHeader));\r
2704 EntrySize = ALIGN_VARIABLE (EntrySize + DataSize);\r
9166f840 2705 \r
a3bcde70
HT
2706 //EntrySize = SelectorSize + DataSize + 2 * sizeof (SelectorHeader);\r
2707 if (Buffer->Capacity - Buffer->Size < EntrySize) {\r
2708 //\r
2709 // Calculate the required buffer\r
2710 //\r
2711 Buffer->Capacity += EntrySize;\r
2712 TempPoint = AllocatePool (Buffer->Capacity);\r
9166f840 2713 \r
02a758cb 2714 if (TempPoint == NULL) {\r
a3bcde70
HT
2715 return EFI_OUT_OF_RESOURCES;\r
2716 }\r
2717 //\r
2718 // Copy the old Buffer to new buffer and free the old one.\r
2719 //\r
2720 CopyMem (TempPoint, Buffer->Ptr, Buffer->Size);\r
2721 FreePool (Buffer->Ptr);\r
9166f840 2722 \r
2723 Buffer->Ptr = TempPoint; \r
a3bcde70
HT
2724 }\r
2725\r
2726 mFixPolicyEntry[Type](Selector, Data);\r
2727\r
2728 //\r
2729 // Fill the selector header and copy it into buffer.\r
2730 //\r
2731 SelectorHeader.Type = (UINT8) (Type | IPSEC_VAR_ITEM_HEADER_LOGO_BIT);\r
2732 SelectorHeader.Size = (UINT16) SelectorSize;\r
2733\r
2734 CopyMem (\r
2735 Buffer->Ptr + Buffer->Size,\r
2736 &SelectorHeader,\r
2737 sizeof (SelectorHeader)\r
2738 );\r
2739 Buffer->Size = ALIGN_VARIABLE (Buffer->Size + sizeof (SelectorHeader));\r
9166f840 2740 \r
a3bcde70
HT
2741 //\r
2742 // Copy the selector into buffer.\r
2743 //\r
2744 CopyMem (\r
2745 Buffer->Ptr + Buffer->Size,\r
2746 Selector,\r
2747 SelectorSize\r
2748 );\r
2749 Buffer->Size = ALIGN_VARIABLE (Buffer->Size + SelectorSize);\r
2750\r
2751 //\r
2752 // Fill the data header and copy it into buffer.\r
2753 //\r
2754 DataHeader.Type = (UINT8) Type;\r
2755 DataHeader.Size = (UINT16) DataSize;\r
2756\r
2757 CopyMem (\r
2758 Buffer->Ptr + Buffer->Size,\r
2759 &DataHeader,\r
2760 sizeof (DataHeader)\r
2761 );\r
2762 Buffer->Size = ALIGN_VARIABLE (Buffer->Size + sizeof (DataHeader));\r
2763 //\r
2764 // Copy the data into buffer.\r
2765 //\r
2766 CopyMem (\r
2767 Buffer->Ptr + Buffer->Size,\r
2768 Data,\r
2769 DataSize\r
2770 );\r
2771 Buffer->Size = ALIGN_VARIABLE (Buffer->Size + DataSize);\r
9166f840 2772 \r
a3bcde70
HT
2773 mUnfixPolicyEntry[Type](Selector, Data);\r
2774\r
2775 return EFI_SUCCESS;\r
2776}\r
2777\r
2778/**\r
2779 Visit all IPsec Configurations of specified Type and call the caller defined\r
2780 interface.\r
2781\r
2782 @param[in] DataType The specified IPsec Config Data Type.\r
2783 @param[in] Routine The function defined by the caller.\r
2784 @param[in] Context The data passed to the Routine.\r
2785\r
2786 @retval EFI_OUT_OF_RESOURCES The required system resource could not be allocated\r
2787 @retval EFI_SUCCESS This function completed successfully.\r
2788\r
2789**/\r
2790EFI_STATUS\r
2791IpSecVisitConfigData (\r
2792 IN EFI_IPSEC_CONFIG_DATA_TYPE DataType,\r
2793 IN IPSEC_COPY_POLICY_ENTRY Routine,\r
2794 IN VOID *Context\r
2795 )\r
2796{\r
2797 EFI_STATUS GetNextStatus;\r
2798 EFI_STATUS GetDataStatus;\r
2799 EFI_STATUS RoutineStatus;\r
2800 EFI_IPSEC_CONFIG_SELECTOR *Selector;\r
2801 VOID *Data;\r
2802 UINTN SelectorSize;\r
2803 UINTN DataSize;\r
2804 UINTN SelectorBufferSize;\r
2805 UINTN DataBufferSize;\r
2806 BOOLEAN FirstGetNext;\r
2807\r
2808 FirstGetNext = TRUE;\r
2809 DataBufferSize = 0;\r
2810 Data = NULL;\r
2811 SelectorBufferSize = sizeof (EFI_IPSEC_CONFIG_SELECTOR);\r
2812 Selector = AllocateZeroPool (SelectorBufferSize);\r
2813\r
2814 if (Selector == NULL) {\r
2815 return EFI_OUT_OF_RESOURCES;\r
2816 }\r
2817\r
2818 while (TRUE) {\r
2819 //\r
2820 // Get the real size of the selector.\r
2821 //\r
2822 SelectorSize = SelectorBufferSize;\r
2823 GetNextStatus = EfiIpSecConfigGetNextSelector (\r
2824 &mIpSecConfigInstance,\r
2825 DataType,\r
2826 &SelectorSize,\r
2827 Selector\r
2828 );\r
2829 if (GetNextStatus == EFI_BUFFER_TOO_SMALL) {\r
2830 FreePool (Selector);\r
2831 SelectorBufferSize = SelectorSize;\r
2832 //\r
2833 // Allocate zero pool for the first selector, while store the last\r
2834 // selector content for the other selectors.\r
2835 //\r
2836 if (FirstGetNext) {\r
2837 Selector = AllocateZeroPool (SelectorBufferSize);\r
2838 } else {\r
2839 Selector = AllocateCopyPool (SelectorBufferSize, Selector);\r
2840 }\r
2841\r
2842 if (Selector == NULL) {\r
2843 return EFI_OUT_OF_RESOURCES;\r
2844 }\r
2845 //\r
2846 // Get the content of the selector.\r
2847 //\r
2848 GetNextStatus = EfiIpSecConfigGetNextSelector (\r
2849 &mIpSecConfigInstance,\r
2850 DataType,\r
2851 &SelectorSize,\r
2852 Selector\r
2853 );\r
2854 }\r
2855\r
2856 if (EFI_ERROR (GetNextStatus)) {\r
2857 break;\r
2858 }\r
2859\r
2860 FirstGetNext = FALSE;\r
2861\r
2862 //\r
2863 // Get the real size of the policy entry according to the selector.\r
2864 //\r
2865 DataSize = DataBufferSize;\r
2866 GetDataStatus = EfiIpSecConfigGetData (\r
2867 &mIpSecConfigInstance,\r
2868 DataType,\r
2869 Selector,\r
2870 &DataSize,\r
2871 Data\r
2872 );\r
2873 if (GetDataStatus == EFI_BUFFER_TOO_SMALL) {\r
2874 if (Data != NULL) {\r
2875 FreePool (Data);\r
2876 }\r
2877\r
2878 DataBufferSize = DataSize;\r
2879 Data = AllocateZeroPool (DataBufferSize);\r
2880\r
2881 if (Data == NULL) {\r
2882 return EFI_OUT_OF_RESOURCES;\r
2883 }\r
2884 //\r
2885 // Get the content of the policy entry according to the selector.\r
2886 //\r
2887 GetDataStatus = EfiIpSecConfigGetData (\r
2888 &mIpSecConfigInstance,\r
2889 DataType,\r
2890 Selector,\r
2891 &DataSize,\r
2892 Data\r
2893 );\r
2894 }\r
2895\r
2896 if (EFI_ERROR (GetDataStatus)) {\r
2897 break;\r
2898 }\r
2899 //\r
2900 // Prepare the buffer of updated policy entry, which is stored in\r
2901 // the continous memory, and then save into variable later.\r
2902 //\r
2903 RoutineStatus = Routine (\r
2904 DataType,\r
2905 Selector,\r
2906 Data,\r
2907 SelectorSize,\r
2908 DataSize,\r
2909 Context\r
2910 );\r
2911 if (EFI_ERROR (RoutineStatus)) {\r
2912 break;\r
2913 }\r
2914 }\r
2915\r
2916 if (Data != NULL) {\r
2917 FreePool (Data);\r
2918 }\r
2919\r
2920 if (Selector != NULL) {\r
2921 FreePool (Selector);\r
2922 }\r
2923\r
2924 return EFI_SUCCESS;\r
2925}\r
2926\r
2927/**\r
2928 This function is the subfunction of EFIIpSecConfigSetData.\r
2929\r
2930 This function call IpSecSetVaraible to set the IPsec Configuration into the firmware.\r
2931\r
2932 @retval EFI_OUT_OF_RESOURCES The required system resource could not be allocated.\r
2933 @retval EFI_SUCCESS Saved the configration successfully.\r
2934 @retval Others Other errors were found while obtaining the variable.\r
2935\r
2936**/\r
2937EFI_STATUS\r
2938IpSecConfigSave (\r
2939 VOID\r
2940 )\r
2941{\r
2942 IPSEC_VARIABLE_BUFFER Buffer;\r
2943 EFI_STATUS Status;\r
2944 EFI_IPSEC_CONFIG_DATA_TYPE Type;\r
2945\r
2946 Buffer.Size = 0;\r
2947 Buffer.Capacity = IPSEC_DEFAULT_VARIABLE_SIZE;\r
2948 Buffer.Ptr = AllocateZeroPool (Buffer.Capacity);\r
2949\r
2950 if (Buffer.Ptr == NULL) {\r
2951 return EFI_OUT_OF_RESOURCES;\r
2952 }\r
2953 //\r
2954 // For each policy database, prepare the contious buffer to save into variable.\r
2955 //\r
2956 for (Type = IPsecConfigDataTypeSpd; Type < IPsecConfigDataTypeMaximum; Type++) {\r
2957 IpSecVisitConfigData (\r
2958 Type,\r
2959 (IPSEC_COPY_POLICY_ENTRY) IpSecCopyPolicyEntry,\r
2960 &Buffer\r
2961 );\r
2962 }\r
2963 //\r
2964 // Save the updated policy database into variable.\r
2965 //\r
2966 Status = IpSecSetVariable (\r
2967 IPSECCONFIG_VARIABLE_NAME,\r
2968 &gEfiIpSecConfigProtocolGuid,\r
2969 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
2970 Buffer.Size,\r
2971 Buffer.Ptr\r
2972 );\r
2973\r
2974 FreePool (Buffer.Ptr);\r
2975\r
2976 return Status;\r
2977}\r
2978\r
2979/**\r
2980 Get the all IPSec configuration variables and store those variables\r
2981 to the internal data structure.\r
2982\r
9166f840 2983 This founction is called by IpSecConfigInitialize() which is to intialize the \r
a3bcde70
HT
2984 IPsecConfiguration Protocol.\r
2985\r
2986 @param[in] Private Point to IPSEC_PRIVATE_DATA.\r
2987\r
2988 @retval EFI_OUT_OF_RESOURCES The required system resource could not be allocated\r
2989 @retval EFI_SUCCESS Restore the IPsec Configuration successfully.\r
2990 @retval others Other errors is found while obtaining the variable.\r
2991\r
2992**/\r
2993EFI_STATUS\r
2994IpSecConfigRestore (\r
2995 IN IPSEC_PRIVATE_DATA *Private\r
2996 )\r
2997{\r
2998 EFI_STATUS Status;\r
2999 UINTN BufferSize;\r
3000 UINT8 *Buffer;\r
3001 IPSEC_VAR_ITEM_HEADER *Header;\r
3002 UINT8 *Ptr;\r
3003 EFI_IPSEC_CONFIG_SELECTOR *Selector;\r
3004 EFI_IPSEC_CONFIG_DATA_TYPE Type;\r
3005 VOID *Data;\r
3006 UINT8 Value;\r
3007 UINTN Size;\r
3008\r
3009 Value = 0;\r
3010 Size = sizeof (Value);\r
3011 BufferSize = 0;\r
3012 Buffer = NULL;\r
3013\r
3014 Status = gRT->GetVariable (\r
3015 IPSECCONFIG_STATUS_NAME,\r
3016 &gEfiIpSecConfigProtocolGuid,\r
3017 NULL,\r
3018 &Size,\r
3019 &Value\r
3020 );\r
3021\r
3022 if (!EFI_ERROR (Status) && Value == IPSEC_STATUS_ENABLED) {\r
3023 Private->IpSec.DisabledFlag = FALSE;\r
3024 }\r
3025 //\r
3026 // Get the real size of policy database in variable.\r
3027 //\r
3028 Status = IpSecGetVariable (\r
3029 IPSECCONFIG_VARIABLE_NAME,\r
3030 &gEfiIpSecConfigProtocolGuid,\r
3031 NULL,\r
3032 &BufferSize,\r
3033 Buffer\r
3034 );\r
3035 if (Status == EFI_BUFFER_TOO_SMALL) {\r
3036\r
3037 Buffer = AllocateZeroPool (BufferSize);\r
3038 if (Buffer == NULL) {\r
3039 return EFI_OUT_OF_RESOURCES;\r
3040 }\r
3041 //\r
3042 // Get the content of policy database in variable.\r
3043 //\r
3044 Status = IpSecGetVariable (\r
3045 IPSECCONFIG_VARIABLE_NAME,\r
3046 &gEfiIpSecConfigProtocolGuid,\r
3047 NULL,\r
3048 &BufferSize,\r
3049 Buffer\r
3050 );\r
3051 if (EFI_ERROR (Status)) {\r
3052 FreePool (Buffer);\r
3053 return Status;\r
3054 }\r
3055\r
3056 for (Ptr = Buffer; Ptr < Buffer + BufferSize;) {\r
3057\r
3058 Header = (IPSEC_VAR_ITEM_HEADER *) Ptr;\r
3059 Type = (EFI_IPSEC_CONFIG_DATA_TYPE) (Header->Type & IPSEC_VAR_ITEM_HEADER_CONTENT_BIT);\r
3060 ASSERT (((Header->Type & 0x80) == IPSEC_VAR_ITEM_HEADER_LOGO_BIT) && (Type < IPsecConfigDataTypeMaximum));\r
9166f840 3061 \r
a3bcde70
HT
3062 Selector = (EFI_IPSEC_CONFIG_SELECTOR *) ALIGN_POINTER (Header + 1, sizeof (UINTN));\r
3063 Header = (IPSEC_VAR_ITEM_HEADER *) ALIGN_POINTER (\r
9166f840 3064 (UINT8 *) Selector + Header->Size, \r
a3bcde70
HT
3065 sizeof (UINTN)\r
3066 );\r
3067 ASSERT (Header->Type == Type);\r
3068\r
3069 Data = ALIGN_POINTER (Header + 1, sizeof (UINTN));\r
3070\r
3071 mUnfixPolicyEntry[Type](Selector, Data);\r
3072\r
3073 //\r
3074 // Update each policy entry according to the content in variable.\r
3075 //\r
3076 mSetBySelf = TRUE;\r
3077 Status = EfiIpSecConfigSetData (\r
3078 &Private->IpSecConfig,\r
3079 Type,\r
3080 Selector,\r
3081 Data,\r
3082 NULL\r
3083 );\r
3084 mSetBySelf = FALSE;\r
3085\r
3086 if (EFI_ERROR (Status)) {\r
3087 FreePool (Buffer);\r
3088 return Status;\r
3089 }\r
3090\r
3091 Ptr = ALIGN_POINTER ((UINT8 *) Data + Header->Size, sizeof (UINTN));\r
3092 }\r
3093\r
3094 FreePool (Buffer);\r
3095 }\r
3096\r
3097 return EFI_SUCCESS;\r
3098}\r
3099\r
3100/**\r
3101 Install and Initialize IPsecConfig protocol\r
3102\r
3103 @param[in, out] Private Pointer to IPSEC_PRIVATE_DATA. After this function finish,\r
3104 the pointer of IPsecConfig Protocol implementation will copy\r
3105 into its IPsecConfig member.\r
3106\r
3107 @retval EFI_SUCCESS Initialized the IPsecConfig Protocol successfully.\r
3108 @retval Others Initializing the IPsecConfig Protocol failed.\r
3109**/\r
3110EFI_STATUS\r
3111IpSecConfigInitialize (\r
3112 IN OUT IPSEC_PRIVATE_DATA *Private\r
3113 )\r
3114{\r
3115 EFI_IPSEC_CONFIG_DATA_TYPE Type;\r
3116\r
3117 CopyMem (\r
3118 &Private->IpSecConfig,\r
3119 &mIpSecConfigInstance,\r
3120 sizeof (EFI_IPSEC_CONFIG_PROTOCOL)\r
3121 );\r
3122\r
3123 //\r
3124 // Initialize the list head of policy database.\r
3125 //\r
3126 for (Type = IPsecConfigDataTypeSpd; Type < IPsecConfigDataTypeMaximum; Type++) {\r
3127 InitializeListHead (&mConfigData[Type]);\r
3128 }\r
3129 //\r
3130 // Restore the content of policy database according to the variable.\r
3131 //\r
3132 IpSecConfigRestore (Private);\r
3133\r
3134 return gBS->InstallMultipleProtocolInterfaces (\r
3135 &Private->Handle,\r
3136 &gEfiIpSecConfigProtocolGuid,\r
3137 &Private->IpSecConfig,\r
3138 NULL\r
3139 );\r
3140}\r