]> git.proxmox.com Git - mirror_edk2.git/blob - NetworkPkg/Application/IpsecConfig/PolicyEntryOperation.c
16f35909776140b5ee87fb8f8966fc4303c13729
[mirror_edk2.git] / NetworkPkg / Application / IpsecConfig / PolicyEntryOperation.c
1 /** @file
2 The implementation of policy entry operation function in IpSecConfig application.
3
4 Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
5
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7
8 **/
9
10 #include "IpSecConfig.h"
11 #include "Indexer.h"
12 #include "Match.h"
13 #include "Helper.h"
14 #include "ForEach.h"
15 #include "PolicyEntryOperation.h"
16
17 /**
18 Fill in EFI_IPSEC_SPD_SELECTOR through ParamPackage list.
19
20 @param[out] Selector The pointer to the EFI_IPSEC_SPD_SELECTOR structure.
21 @param[in] ParamPackage The pointer to the ParamPackage list.
22 @param[in, out] Mask The pointer to the Mask.
23
24 @retval EFI_SUCCESS Fill in EFI_IPSEC_SPD_SELECTOR successfully.
25 @retval EFI_INVALID_PARAMETER Invalid user input parameter.
26
27 **/
28 EFI_STATUS
29 CreateSpdSelector (
30 OUT EFI_IPSEC_SPD_SELECTOR *Selector,
31 IN LIST_ENTRY *ParamPackage,
32 IN OUT UINT32 *Mask
33 )
34 {
35 EFI_STATUS Status;
36 EFI_STATUS ReturnStatus;
37 CONST CHAR16 *ValueStr;
38
39 Status = EFI_SUCCESS;
40 ReturnStatus = EFI_SUCCESS;
41
42 //
43 // Convert user imput from string to integer, and fill in the member in EFI_IPSEC_SPD_SELECTOR.
44 //
45 ValueStr = ShellCommandLineGetValue (ParamPackage, L"--local");
46 if (ValueStr != NULL) {
47 Selector->LocalAddressCount = 1;
48 Status = EfiInetAddrRange ((CHAR16 *) ValueStr, Selector->LocalAddress);
49 if (EFI_ERROR (Status)) {
50 ShellPrintHiiEx (
51 -1,
52 -1,
53 NULL,
54 STRING_TOKEN (STR_IPSEC_CONFIG_INCORRECT_PARAMETER_VALUE),
55 mHiiHandle,
56 mAppName,
57 L"--local",
58 ValueStr
59 );
60 ReturnStatus = EFI_INVALID_PARAMETER;
61 } else {
62 *Mask |= LOCAL;
63 }
64 }
65
66 //
67 // Convert user imput from string to integer, and fill in the member in EFI_IPSEC_SPD_SELECTOR.
68 //
69 ValueStr = ShellCommandLineGetValue (ParamPackage, L"--remote");
70 if (ValueStr != NULL) {
71 Selector->RemoteAddressCount = 1;
72 Status = EfiInetAddrRange ((CHAR16 *) ValueStr, Selector->RemoteAddress);
73 if (EFI_ERROR (Status)) {
74 ShellPrintHiiEx (
75 -1,
76 -1,
77 NULL,
78 STRING_TOKEN (STR_IPSEC_CONFIG_INCORRECT_PARAMETER_VALUE),
79 mHiiHandle,
80 mAppName,
81 L"--remote",
82 ValueStr
83 );
84 ReturnStatus = EFI_INVALID_PARAMETER;
85 } else {
86 *Mask |= REMOTE;
87 }
88 }
89
90 Selector->NextLayerProtocol = EFI_IPSEC_ANY_PROTOCOL;
91
92 //
93 // Convert user imput from string to integer, and fill in the member in EFI_IPSEC_SPD_SELECTOR.
94 //
95 Status = GetNumber (
96 L"--proto",
97 (UINT16) -1,
98 &Selector->NextLayerProtocol,
99 sizeof (UINT16),
100 mMapIpProtocol,
101 ParamPackage,
102 FORMAT_NUMBER | FORMAT_STRING
103 );
104 if (!EFI_ERROR (Status)) {
105 *Mask |= PROTO;
106 }
107
108 if (Status == EFI_INVALID_PARAMETER) {
109 ReturnStatus = EFI_INVALID_PARAMETER;
110 }
111
112 Selector->LocalPort = EFI_IPSEC_ANY_PORT;
113 Selector->RemotePort = EFI_IPSEC_ANY_PORT;
114
115 //
116 // Convert user imput from string to integer, and fill in the member in EFI_IPSEC_SPD_SELECTOR.
117 //
118 ValueStr = ShellCommandLineGetValue (ParamPackage, L"--local-port");
119 if (ValueStr != NULL) {
120 Status = EfiInetPortRange ((CHAR16 *) ValueStr, &Selector->LocalPort, &Selector->LocalPortRange);
121 if (EFI_ERROR (Status)) {
122 ShellPrintHiiEx (
123 -1,
124 -1,
125 NULL,
126 STRING_TOKEN (STR_IPSEC_CONFIG_INCORRECT_PARAMETER_VALUE),
127 mHiiHandle,
128 mAppName,
129 L"--local-port",
130 ValueStr
131 );
132 ReturnStatus = EFI_INVALID_PARAMETER;
133 } else {
134 *Mask |= LOCAL_PORT;
135 }
136 }
137
138 //
139 // Convert user imput from string to integer, and fill in the member in EFI_IPSEC_SPD_SELECTOR.
140 //
141 ValueStr = ShellCommandLineGetValue (ParamPackage, L"--remote-port");
142 if (ValueStr != NULL) {
143 Status = EfiInetPortRange ((CHAR16 *) ValueStr, &Selector->RemotePort, &Selector->RemotePortRange);
144 if (EFI_ERROR (Status)) {
145 ShellPrintHiiEx (
146 -1,
147 -1,
148 NULL,
149 STRING_TOKEN (STR_IPSEC_CONFIG_INCORRECT_PARAMETER_VALUE),
150 mHiiHandle,
151 mAppName,
152 L"--remote-port",
153 ValueStr
154 );
155 ReturnStatus = EFI_INVALID_PARAMETER;
156 } else {
157 *Mask |= REMOTE_PORT;
158 }
159 }
160
161 //
162 // Convert user imput from string to integer, and fill in the member in EFI_IPSEC_SPD_SELECTOR.
163 //
164 Status = GetNumber (
165 L"--icmp-type",
166 (UINT8) -1,
167 &Selector->LocalPort,
168 sizeof (UINT16),
169 NULL,
170 ParamPackage,
171 FORMAT_NUMBER
172 );
173 if (!EFI_ERROR (Status)) {
174 *Mask |= ICMP_TYPE;
175 }
176
177 if (Status == EFI_INVALID_PARAMETER) {
178 ReturnStatus = EFI_INVALID_PARAMETER;
179 }
180
181 //
182 // Convert user imput from string to integer, and fill in the member in EFI_IPSEC_SPD_SELECTOR.
183 //
184 Status = GetNumber (
185 L"--icmp-code",
186 (UINT8) -1,
187 &Selector->RemotePort,
188 sizeof (UINT16),
189 NULL,
190 ParamPackage,
191 FORMAT_NUMBER
192 );
193 if (!EFI_ERROR (Status)) {
194 *Mask |= ICMP_CODE;
195 }
196
197 if (Status == EFI_INVALID_PARAMETER) {
198 ReturnStatus = EFI_INVALID_PARAMETER;
199 }
200
201 return ReturnStatus;
202 }
203
204 /**
205 Fill in EFI_IPSEC_SPD_SELECTOR and EFI_IPSEC_SPD_DATA through ParamPackage list.
206
207 @param[out] Selector The pointer to the EFI_IPSEC_SPD_SELECTOR structure.
208 @param[out] Data The pointer to the EFI_IPSEC_SPD_DATA structure.
209 @param[in] ParamPackage The pointer to the ParamPackage list.
210 @param[out] Mask The pointer to the Mask.
211 @param[in] CreateNew The switch to create new.
212
213 @retval EFI_SUCCESS Fill in EFI_IPSEC_SPD_SELECTOR and EFI_IPSEC_SPD_DATA successfully.
214 @retval EFI_INVALID_PARAMETER Invalid user input parameter.
215
216 **/
217 EFI_STATUS
218 CreateSpdEntry (
219 OUT EFI_IPSEC_SPD_SELECTOR **Selector,
220 OUT EFI_IPSEC_SPD_DATA **Data,
221 IN LIST_ENTRY *ParamPackage,
222 OUT UINT32 *Mask,
223 IN BOOLEAN CreateNew
224 )
225 {
226 EFI_STATUS Status;
227 EFI_STATUS ReturnStatus;
228 CONST CHAR16 *ValueStr;
229 UINTN DataSize;
230
231 Status = EFI_SUCCESS;
232 *Mask = 0;
233
234 *Selector = AllocateZeroPool (sizeof (EFI_IPSEC_SPD_SELECTOR) + 2 * sizeof (EFI_IP_ADDRESS_INFO));
235 ASSERT (*Selector != NULL);
236
237 (*Selector)->LocalAddress = (EFI_IP_ADDRESS_INFO *) (*Selector + 1);
238 (*Selector)->RemoteAddress = (*Selector)->LocalAddress + 1;
239
240 ReturnStatus = CreateSpdSelector (*Selector, ParamPackage, Mask);
241
242 //
243 // SPD DATA
244 // NOTE: Allocate enough memory and add padding for different arch.
245 //
246 DataSize = ALIGN_VARIABLE (sizeof (EFI_IPSEC_SPD_DATA));
247 DataSize = ALIGN_VARIABLE (DataSize + sizeof (EFI_IPSEC_PROCESS_POLICY));
248 DataSize += sizeof (EFI_IPSEC_TUNNEL_OPTION);
249
250 *Data = AllocateZeroPool (DataSize);
251 ASSERT (*Data != NULL);
252
253 (*Data)->ProcessingPolicy = (EFI_IPSEC_PROCESS_POLICY *) ALIGN_POINTER (
254 (*Data + 1),
255 sizeof (UINTN)
256 );
257 (*Data)->ProcessingPolicy->TunnelOption = (EFI_IPSEC_TUNNEL_OPTION *) ALIGN_POINTER (
258 ((*Data)->ProcessingPolicy + 1),
259 sizeof (UINTN)
260 );
261
262
263 //
264 // Convert user imput from string to integer, and fill in the Name in EFI_IPSEC_SPD_DATA.
265 //
266 ValueStr = ShellCommandLineGetValue (ParamPackage, L"--name");
267 if (ValueStr != NULL) {
268 UnicodeStrToAsciiStrS (ValueStr, (CHAR8 *) (*Data)->Name, sizeof ((*Data)->Name));
269 *Mask |= NAME;
270 }
271
272 //
273 // Convert user imput from string to integer, and fill in the PackageFlag in EFI_IPSEC_SPD_DATA.
274 //
275 Status = GetNumber (
276 L"--packet-flag",
277 (UINT8) -1,
278 &(*Data)->PackageFlag,
279 sizeof (UINT32),
280 NULL,
281 ParamPackage,
282 FORMAT_NUMBER
283 );
284 if (!EFI_ERROR (Status)) {
285 *Mask |= PACKET_FLAG;
286 }
287
288 if (Status == EFI_INVALID_PARAMETER) {
289 ReturnStatus = EFI_INVALID_PARAMETER;
290 }
291
292 //
293 // Convert user imput from string to integer, and fill in the Action in EFI_IPSEC_SPD_DATA.
294 //
295 Status = GetNumber (
296 L"--action",
297 (UINT8) -1,
298 &(*Data)->Action,
299 sizeof (UINT32),
300 mMapIpSecAction,
301 ParamPackage,
302 FORMAT_STRING
303 );
304 if (!EFI_ERROR (Status)) {
305 *Mask |= ACTION;
306 }
307
308 if (Status == EFI_INVALID_PARAMETER) {
309 ReturnStatus = EFI_INVALID_PARAMETER;
310 }
311
312 //
313 // Convert user imput from string to integer, and fill in the ExtSeqNum in EFI_IPSEC_SPD_DATA.
314 //
315 if (ShellCommandLineGetFlag (ParamPackage, L"--ext-sequence")) {
316 (*Data)->ProcessingPolicy->ExtSeqNum = TRUE;
317 *Mask |= EXT_SEQUENCE;
318 } else if (ShellCommandLineGetFlag (ParamPackage, L"--ext-sequence-")) {
319 (*Data)->ProcessingPolicy->ExtSeqNum = FALSE;
320 *Mask |= EXT_SEQUENCE;
321 }
322
323 //
324 // Convert user imput from string to integer, and fill in the SeqOverflow in EFI_IPSEC_SPD_DATA.
325 //
326 if (ShellCommandLineGetFlag (ParamPackage, L"--sequence-overflow")) {
327 (*Data)->ProcessingPolicy->SeqOverflow = TRUE;
328 *Mask |= SEQUENCE_OVERFLOW;
329 } else if (ShellCommandLineGetFlag (ParamPackage, L"--sequence-overflow-")) {
330 (*Data)->ProcessingPolicy->SeqOverflow = FALSE;
331 *Mask |= SEQUENCE_OVERFLOW;
332 }
333
334 //
335 // Convert user imput from string to integer, and fill in the FragCheck in EFI_IPSEC_SPD_DATA.
336 //
337 if (ShellCommandLineGetFlag (ParamPackage, L"--fragment-check")) {
338 (*Data)->ProcessingPolicy->FragCheck = TRUE;
339 *Mask |= FRAGMENT_CHECK;
340 } else if (ShellCommandLineGetFlag (ParamPackage, L"--fragment-check-")) {
341 (*Data)->ProcessingPolicy->FragCheck = FALSE;
342 *Mask |= FRAGMENT_CHECK;
343 }
344
345 //
346 // Convert user imput from string to integer, and fill in the ProcessingPolicy in EFI_IPSEC_SPD_DATA.
347 //
348 Status = GetNumber (
349 L"--lifebyte",
350 (UINT64) -1,
351 &(*Data)->ProcessingPolicy->SaLifetime.ByteCount,
352 sizeof (UINT64),
353 NULL,
354 ParamPackage,
355 FORMAT_NUMBER
356 );
357 if (!EFI_ERROR (Status)) {
358 *Mask |= LIFEBYTE;
359 }
360
361 if (Status == EFI_INVALID_PARAMETER) {
362 ReturnStatus = EFI_INVALID_PARAMETER;
363 }
364
365 Status = GetNumber (
366 L"--lifetime",
367 (UINT64) -1,
368 &(*Data)->ProcessingPolicy->SaLifetime.HardLifetime,
369 sizeof (UINT64),
370 NULL,
371 ParamPackage,
372 FORMAT_NUMBER
373 );
374 if (!EFI_ERROR (Status)) {
375 *Mask |= LIFETIME;
376 }
377 if (Status == EFI_INVALID_PARAMETER) {
378 ReturnStatus = EFI_INVALID_PARAMETER;
379 }
380
381 Status = GetNumber (
382 L"--lifetime-soft",
383 (UINT64) -1,
384 &(*Data)->ProcessingPolicy->SaLifetime.SoftLifetime,
385 sizeof (UINT64),
386 NULL,
387 ParamPackage,
388 FORMAT_NUMBER
389 );
390 if (!EFI_ERROR (Status)) {
391 *Mask |= LIFETIME_SOFT;
392 }
393
394 if (Status == EFI_INVALID_PARAMETER) {
395 ReturnStatus = EFI_INVALID_PARAMETER;
396 }
397
398 (*Data)->ProcessingPolicy->Mode = EfiIPsecTransport;
399 Status = GetNumber (
400 L"--mode",
401 0,
402 &(*Data)->ProcessingPolicy->Mode,
403 sizeof (UINT32),
404 mMapIpSecMode,
405 ParamPackage,
406 FORMAT_STRING
407 );
408 if (!EFI_ERROR (Status)) {
409 *Mask |= MODE;
410 }
411
412 if (Status == EFI_INVALID_PARAMETER) {
413 ReturnStatus = EFI_INVALID_PARAMETER;
414 }
415
416 ValueStr = ShellCommandLineGetValue (ParamPackage, L"--tunnel-local");
417 if (ValueStr != NULL) {
418 Status = EfiInetAddr2 ((CHAR16 *) ValueStr, &(*Data)->ProcessingPolicy->TunnelOption->LocalTunnelAddress);
419 if (EFI_ERROR (Status)) {
420 ShellPrintHiiEx (
421 -1,
422 -1,
423 NULL,
424 STRING_TOKEN (STR_IPSEC_CONFIG_INCORRECT_PARAMETER_VALUE),
425 mHiiHandle,
426 mAppName,
427 L"--tunnel-local",
428 ValueStr
429 );
430 ReturnStatus = EFI_INVALID_PARAMETER;
431 } else {
432 *Mask |= TUNNEL_LOCAL;
433 }
434 }
435
436 ValueStr = ShellCommandLineGetValue (ParamPackage, L"--tunnel-remote");
437 if (ValueStr != NULL) {
438 Status = EfiInetAddr2 ((CHAR16 *) ValueStr, &(*Data)->ProcessingPolicy->TunnelOption->RemoteTunnelAddress);
439 if (EFI_ERROR (Status)) {
440 ShellPrintHiiEx (
441 -1,
442 -1,
443 NULL,
444 STRING_TOKEN (STR_IPSEC_CONFIG_INCORRECT_PARAMETER_VALUE),
445 mHiiHandle,
446 mAppName,
447 L"--tunnel-remote",
448 ValueStr
449 );
450 ReturnStatus = EFI_INVALID_PARAMETER;
451 } else {
452 *Mask |= TUNNEL_REMOTE;
453 }
454 }
455
456 (*Data)->ProcessingPolicy->TunnelOption->DF = EfiIPsecTunnelCopyDf;
457 Status = GetNumber (
458 L"--dont-fragment",
459 0,
460 &(*Data)->ProcessingPolicy->TunnelOption->DF,
461 sizeof (UINT32),
462 mMapDfOption,
463 ParamPackage,
464 FORMAT_STRING
465 );
466 if (!EFI_ERROR (Status)) {
467 *Mask |= DONT_FRAGMENT;
468 }
469
470 if (Status == EFI_INVALID_PARAMETER) {
471 ReturnStatus = EFI_INVALID_PARAMETER;
472 }
473
474 (*Data)->ProcessingPolicy->Proto = EfiIPsecESP;
475 Status = GetNumber (
476 L"--ipsec-proto",
477 0,
478 &(*Data)->ProcessingPolicy->Proto,
479 sizeof (UINT32),
480 mMapIpSecProtocol,
481 ParamPackage,
482 FORMAT_STRING
483 );
484 if (!EFI_ERROR (Status)) {
485 *Mask |= IPSEC_PROTO;
486 }
487
488 if (Status == EFI_INVALID_PARAMETER) {
489 ReturnStatus = EFI_INVALID_PARAMETER;
490 }
491
492 Status = GetNumber (
493 L"--encrypt-algo",
494 0,
495 &(*Data)->ProcessingPolicy->EncAlgoId,
496 sizeof (UINT8),
497 mMapEncAlgo,
498 ParamPackage,
499 FORMAT_STRING
500 );
501 if (!EFI_ERROR (Status)) {
502 *Mask |= ENCRYPT_ALGO;
503 }
504
505 if (Status == EFI_INVALID_PARAMETER) {
506 ReturnStatus = EFI_INVALID_PARAMETER;
507 }
508
509 Status = GetNumber (
510 L"--auth-algo",
511 0,
512 &(*Data)->ProcessingPolicy->AuthAlgoId,
513 sizeof (UINT8),
514 mMapAuthAlgo,
515 ParamPackage,
516 FORMAT_STRING
517 );
518 if (!EFI_ERROR (Status)) {
519 *Mask |= AUTH_ALGO;
520 }
521
522 if (Status == EFI_INVALID_PARAMETER) {
523 ReturnStatus = EFI_INVALID_PARAMETER;
524 }
525
526 //
527 // Cannot check Mode against EfiIPsecTunnel, because user may want to change tunnel_remote only so the Mode is not set.
528 //
529 if ((*Mask & (TUNNEL_LOCAL | TUNNEL_REMOTE | DONT_FRAGMENT)) == 0) {
530 (*Data)->ProcessingPolicy->TunnelOption = NULL;
531 }
532
533 if ((*Mask & (EXT_SEQUENCE | SEQUENCE_OVERFLOW | FRAGMENT_CHECK | LIFEBYTE |
534 LIFETIME_SOFT | LIFETIME | MODE | TUNNEL_LOCAL | TUNNEL_REMOTE |
535 DONT_FRAGMENT | IPSEC_PROTO | AUTH_ALGO | ENCRYPT_ALGO)) == 0) {
536 if ((*Data)->Action != EfiIPsecActionProtect) {
537 //
538 // User may not provide additional parameter for Protect action, so we cannot simply set ProcessingPolicy to NULL.
539 //
540 (*Data)->ProcessingPolicy = NULL;
541 }
542 }
543
544 if (CreateNew) {
545 if ((*Mask & (LOCAL | REMOTE | PROTO | ACTION)) != (LOCAL | REMOTE | PROTO | ACTION)) {
546 ShellPrintHiiEx (
547 -1,
548 -1,
549 NULL,
550 STRING_TOKEN (STR_IPSEC_CONFIG_MISSING_ONE_OF_PARAMETERS),
551 mHiiHandle,
552 mAppName,
553 L"--local --remote --proto --action"
554 );
555 ReturnStatus = EFI_INVALID_PARAMETER;
556 } else if (((*Data)->Action == EfiIPsecActionProtect) &&
557 ((*Data)->ProcessingPolicy->Mode == EfiIPsecTunnel) &&
558 ((*Mask & (TUNNEL_LOCAL | TUNNEL_REMOTE)) != (TUNNEL_LOCAL | TUNNEL_REMOTE))) {
559 ShellPrintHiiEx (
560 -1,
561 -1,
562 NULL,
563 STRING_TOKEN (STR_IPSEC_CONFIG_MISSING_ONE_OF_PARAMETERS),
564 mHiiHandle,
565 mAppName,
566 L"--tunnel-local --tunnel-remote"
567 );
568 ReturnStatus = EFI_INVALID_PARAMETER;
569 }
570 }
571
572 return ReturnStatus;
573 }
574
575 /**
576 Fill in EFI_IPSEC_SA_ID and EFI_IPSEC_SA_DATA2 through ParamPackage list.
577
578 @param[out] SaId The pointer to the EFI_IPSEC_SA_ID structure.
579 @param[out] Data The pointer to the EFI_IPSEC_SA_DATA2 structure.
580 @param[in] ParamPackage The pointer to the ParamPackage list.
581 @param[out] Mask The pointer to the Mask.
582 @param[in] CreateNew The switch to create new.
583
584 @retval EFI_SUCCESS Fill in EFI_IPSEC_SA_ID and EFI_IPSEC_SA_DATA2 successfully.
585 @retval EFI_INVALID_PARAMETER Invalid user input parameter.
586
587 **/
588 EFI_STATUS
589 CreateSadEntry (
590 OUT EFI_IPSEC_SA_ID **SaId,
591 OUT EFI_IPSEC_SA_DATA2 **Data,
592 IN LIST_ENTRY *ParamPackage,
593 OUT UINT32 *Mask,
594 IN BOOLEAN CreateNew
595 )
596 {
597 EFI_STATUS Status;
598 EFI_STATUS ReturnStatus;
599 UINTN AuthKeyLength;
600 UINTN EncKeyLength;
601 CONST CHAR16 *ValueStr;
602 CHAR8 *AsciiStr;
603 UINTN DataSize;
604
605 Status = EFI_SUCCESS;
606 ReturnStatus = EFI_SUCCESS;
607 *Mask = 0;
608 AuthKeyLength = 0;
609 EncKeyLength = 0;
610
611 *SaId = AllocateZeroPool (sizeof (EFI_IPSEC_SA_ID));
612 ASSERT (*SaId != NULL);
613
614 //
615 // Convert user imput from string to integer, and fill in the Spi in EFI_IPSEC_SA_ID.
616 //
617 Status = GetNumber (L"--spi", (UINT32) -1, &(*SaId)->Spi, sizeof (UINT32), NULL, ParamPackage, FORMAT_NUMBER);
618 if (!EFI_ERROR (Status)) {
619 *Mask |= SPI;
620 }
621
622 if (Status == EFI_INVALID_PARAMETER) {
623 ReturnStatus = EFI_INVALID_PARAMETER;
624 }
625
626 //
627 // Convert user imput from string to integer, and fill in the Proto in EFI_IPSEC_SA_ID.
628 //
629 Status = GetNumber (
630 L"--ipsec-proto",
631 0,
632 &(*SaId)->Proto,
633 sizeof (EFI_IPSEC_PROTOCOL_TYPE),
634 mMapIpSecProtocol,
635 ParamPackage,
636 FORMAT_STRING
637 );
638 if (!EFI_ERROR (Status)) {
639 *Mask |= IPSEC_PROTO;
640 }
641
642 if (Status == EFI_INVALID_PARAMETER) {
643 ReturnStatus = EFI_INVALID_PARAMETER;
644 }
645
646 //
647 // Convert user imput from string to integer, and fill in EFI_IPSEC_SA_DATA2.
648 //
649 ValueStr = ShellCommandLineGetValue (ParamPackage, L"--auth-key");
650 if (ValueStr != NULL) {
651 AuthKeyLength = StrLen (ValueStr);
652 }
653
654 ValueStr = ShellCommandLineGetValue (ParamPackage, L"--encrypt-key");
655 if (ValueStr != NULL) {
656 EncKeyLength = StrLen (ValueStr);
657 }
658
659 //
660 // EFI_IPSEC_SA_DATA2:
661 // +------------
662 // | EFI_IPSEC_SA_DATA2
663 // +-----------------------
664 // | AuthKey
665 // +-------------------------
666 // | EncKey
667 // +-------------------------
668 // | SpdSelector
669 //
670 // Notes: To make sure the address alignment add padding after each data if needed.
671 //
672 DataSize = ALIGN_VARIABLE (sizeof (EFI_IPSEC_SA_DATA2));
673 DataSize = ALIGN_VARIABLE (DataSize + AuthKeyLength);
674 DataSize = ALIGN_VARIABLE (DataSize + EncKeyLength);
675 DataSize = ALIGN_VARIABLE (DataSize + sizeof (EFI_IPSEC_SPD_SELECTOR));
676 DataSize = ALIGN_VARIABLE (DataSize + sizeof (EFI_IP_ADDRESS_INFO));
677 DataSize += sizeof (EFI_IP_ADDRESS_INFO);
678
679
680
681 *Data = AllocateZeroPool (DataSize);
682 ASSERT (*Data != NULL);
683
684 (*Data)->ManualSet = TRUE;
685 (*Data)->AlgoInfo.EspAlgoInfo.AuthKey = (VOID *) ALIGN_POINTER (((*Data) + 1), sizeof (UINTN));
686 (*Data)->AlgoInfo.EspAlgoInfo.EncKey = (VOID *) ALIGN_POINTER (
687 ((UINT8 *) (*Data)->AlgoInfo.EspAlgoInfo.AuthKey + AuthKeyLength),
688 sizeof (UINTN)
689 );
690 (*Data)->SpdSelector = (EFI_IPSEC_SPD_SELECTOR *) ALIGN_POINTER (
691 ((UINT8 *) (*Data)->AlgoInfo.EspAlgoInfo.EncKey + EncKeyLength),
692 sizeof (UINTN)
693 );
694 (*Data)->SpdSelector->LocalAddress = (EFI_IP_ADDRESS_INFO *) ALIGN_POINTER (
695 ((UINT8 *) (*Data)->SpdSelector + sizeof (EFI_IPSEC_SPD_SELECTOR)),
696 sizeof (UINTN));
697 (*Data)->SpdSelector->RemoteAddress = (EFI_IP_ADDRESS_INFO *) ALIGN_POINTER (
698 (*Data)->SpdSelector->LocalAddress + 1,
699 sizeof (UINTN)
700 );
701
702 (*Data)->Mode = EfiIPsecTransport;
703 Status = GetNumber (
704 L"--mode",
705 0,
706 &(*Data)->Mode,
707 sizeof (EFI_IPSEC_MODE),
708 mMapIpSecMode,
709 ParamPackage,
710 FORMAT_STRING
711 );
712 if (!EFI_ERROR (Status)) {
713 *Mask |= MODE;
714 }
715
716 if (Status == EFI_INVALID_PARAMETER) {
717 ReturnStatus = EFI_INVALID_PARAMETER;
718 }
719
720 //
721 // According to RFC 4303-3.3.3. The first packet sent using a given SA
722 // will contain a sequence number of 1.
723 //
724 (*Data)->SNCount = 1;
725 Status = GetNumber (
726 L"--sequence-number",
727 (UINT64) -1,
728 &(*Data)->SNCount,
729 sizeof (UINT64),
730 NULL,
731 ParamPackage,
732 FORMAT_NUMBER
733 );
734 if (!EFI_ERROR (Status)) {
735 *Mask |= SEQUENCE_NUMBER;
736 }
737
738 if (Status == EFI_INVALID_PARAMETER) {
739 ReturnStatus = EFI_INVALID_PARAMETER;
740 }
741
742 (*Data)->AntiReplayWindows = 0;
743 Status = GetNumber (
744 L"--antireplay-window",
745 (UINT8) -1,
746 &(*Data)->AntiReplayWindows,
747 sizeof (UINT8),
748 NULL,
749 ParamPackage,
750 FORMAT_NUMBER
751 );
752 if (!EFI_ERROR (Status)) {
753 *Mask |= SEQUENCE_NUMBER;
754 }
755
756 if (Status == EFI_INVALID_PARAMETER) {
757 ReturnStatus = EFI_INVALID_PARAMETER;
758 }
759
760 Status = GetNumber (
761 L"--encrypt-algo",
762 0,
763 &(*Data)->AlgoInfo.EspAlgoInfo.EncAlgoId,
764 sizeof (UINT8),
765 mMapEncAlgo,
766 ParamPackage,
767 FORMAT_STRING
768 );
769 if (!EFI_ERROR (Status)) {
770 *Mask |= ENCRYPT_ALGO;
771 }
772
773 if (Status == EFI_INVALID_PARAMETER) {
774 ReturnStatus = EFI_INVALID_PARAMETER;
775 }
776
777 ValueStr = ShellCommandLineGetValue (ParamPackage, L"--encrypt-key");
778 if (ValueStr != NULL ) {
779 (*Data)->AlgoInfo.EspAlgoInfo.EncKeyLength = EncKeyLength;
780 AsciiStr = AllocateZeroPool (EncKeyLength + 1);
781 ASSERT (AsciiStr != NULL);
782 UnicodeStrToAsciiStrS (ValueStr, AsciiStr, EncKeyLength + 1);
783 CopyMem ((*Data)->AlgoInfo.EspAlgoInfo.EncKey, AsciiStr, EncKeyLength);
784 FreePool (AsciiStr);
785 *Mask |= ENCRYPT_KEY;
786 } else {
787 (*Data)->AlgoInfo.EspAlgoInfo.EncKey = NULL;
788 }
789
790 Status = GetNumber (
791 L"--auth-algo",
792 0,
793 &(*Data)->AlgoInfo.EspAlgoInfo.AuthAlgoId,
794 sizeof (UINT8),
795 mMapAuthAlgo,
796 ParamPackage,
797 FORMAT_STRING
798 );
799 if (!EFI_ERROR (Status)) {
800 *Mask |= AUTH_ALGO;
801 }
802
803 if (Status == EFI_INVALID_PARAMETER) {
804 ReturnStatus = EFI_INVALID_PARAMETER;
805 }
806
807 ValueStr = ShellCommandLineGetValue (ParamPackage, L"--auth-key");
808 if (ValueStr != NULL) {
809 (*Data)->AlgoInfo.EspAlgoInfo.AuthKeyLength = AuthKeyLength;
810 AsciiStr = AllocateZeroPool (AuthKeyLength + 1);
811 ASSERT (AsciiStr != NULL);
812 UnicodeStrToAsciiStrS (ValueStr, AsciiStr, AuthKeyLength + 1);
813 CopyMem ((*Data)->AlgoInfo.EspAlgoInfo.AuthKey, AsciiStr, AuthKeyLength);
814 FreePool (AsciiStr);
815 *Mask |= AUTH_KEY;
816 } else {
817 (*Data)->AlgoInfo.EspAlgoInfo.AuthKey = NULL;
818 }
819
820 Status = GetNumber (
821 L"--lifebyte",
822 (UINT64) -1,
823 &(*Data)->SaLifetime.ByteCount,
824 sizeof (UINT64),
825 NULL,
826 ParamPackage,
827 FORMAT_NUMBER
828 );
829 if (!EFI_ERROR (Status)) {
830 *Mask |= LIFEBYTE;
831 }
832
833 if (Status == EFI_INVALID_PARAMETER) {
834 ReturnStatus = EFI_INVALID_PARAMETER;
835 }
836
837 Status = GetNumber (
838 L"--lifetime",
839 (UINT64) -1,
840 &(*Data)->SaLifetime.HardLifetime,
841 sizeof (UINT64),
842 NULL,
843 ParamPackage,
844 FORMAT_NUMBER
845 );
846 if (!EFI_ERROR (Status)) {
847 *Mask |= LIFETIME;
848 }
849
850 if (Status == EFI_INVALID_PARAMETER) {
851 ReturnStatus = EFI_INVALID_PARAMETER;
852 }
853
854 Status = GetNumber (
855 L"--lifetime-soft",
856 (UINT64) -1,
857 &(*Data)->SaLifetime.SoftLifetime,
858 sizeof (UINT64),
859 NULL,
860 ParamPackage,
861 FORMAT_NUMBER
862 );
863 if (!EFI_ERROR (Status)) {
864 *Mask |= LIFETIME_SOFT;
865 }
866
867 if (Status == EFI_INVALID_PARAMETER) {
868 ReturnStatus = EFI_INVALID_PARAMETER;
869 }
870
871 Status = GetNumber (
872 L"--path-mtu",
873 (UINT32) -1,
874 &(*Data)->PathMTU,
875 sizeof (UINT32),
876 NULL,
877 ParamPackage,
878 FORMAT_NUMBER
879 );
880 if (!EFI_ERROR (Status)) {
881 *Mask |= PATH_MTU;
882 }
883
884 if (Status == EFI_INVALID_PARAMETER) {
885 ReturnStatus = EFI_INVALID_PARAMETER;
886 }
887
888 //
889 // Convert user imput from string to integer, and fill in the DestAddress in EFI_IPSEC_SA_ID.
890 //
891 ValueStr = ShellCommandLineGetValue (ParamPackage, L"--tunnel-dest");
892 if (ValueStr != NULL) {
893 Status = EfiInetAddr2 ((CHAR16 *) ValueStr, &(*Data)->TunnelDestinationAddress);
894 if (EFI_ERROR (Status)) {
895 ShellPrintHiiEx (
896 -1,
897 -1,
898 NULL,
899 STRING_TOKEN (STR_IPSEC_CONFIG_INCORRECT_PARAMETER_VALUE),
900 mHiiHandle,
901 mAppName,
902 L"--tunnel-dest",
903 ValueStr
904 );
905 ReturnStatus = EFI_INVALID_PARAMETER;
906 } else {
907 *Mask |= DEST;
908 }
909 }
910
911 //
912 // Convert user input from string to integer, and fill in the DestAddress in EFI_IPSEC_SA_ID.
913 //
914 ValueStr = ShellCommandLineGetValue (ParamPackage, L"--tunnel-source");
915 if (ValueStr != NULL) {
916 Status = EfiInetAddr2 ((CHAR16 *) ValueStr, &(*Data)->TunnelSourceAddress);
917 if (EFI_ERROR (Status)) {
918 ShellPrintHiiEx (
919 -1,
920 -1,
921 NULL,
922 STRING_TOKEN (STR_IPSEC_CONFIG_INCORRECT_PARAMETER_VALUE),
923 mHiiHandle,
924 mAppName,
925 L"--tunnel-source",
926 ValueStr
927 );
928 ReturnStatus = EFI_INVALID_PARAMETER;
929 } else {
930 *Mask |= SOURCE;
931 }
932 }
933
934 //
935 // If it is TunnelMode, then check if the tunnel-source and --tunnel-dest are set
936 //
937 if ((*Data)->Mode == EfiIPsecTunnel) {
938 if ((*Mask & (DEST|SOURCE)) != (DEST|SOURCE)) {
939 ShellPrintHiiEx (
940 -1,
941 -1,
942 NULL,
943 STRING_TOKEN (STR_IPSEC_CONFIG_MISSING_ONE_OF_PARAMETERS),
944 mHiiHandle,
945 mAppName,
946 L"--tunnel-source --tunnel-dest"
947 );
948 ReturnStatus = EFI_INVALID_PARAMETER;
949 }
950 }
951 ReturnStatus = CreateSpdSelector ((*Data)->SpdSelector, ParamPackage, Mask);
952
953 if (CreateNew) {
954 if ((*Mask & (SPI|IPSEC_PROTO|LOCAL|REMOTE)) != (SPI|IPSEC_PROTO|LOCAL|REMOTE)) {
955 ShellPrintHiiEx (
956 -1,
957 -1,
958 NULL,
959 STRING_TOKEN (STR_IPSEC_CONFIG_MISSING_ONE_OF_PARAMETERS),
960 mHiiHandle,
961 mAppName,
962 L"--spi --ipsec-proto --local --remote"
963 );
964 ReturnStatus = EFI_INVALID_PARAMETER;
965 } else {
966 if ((*SaId)->Proto == EfiIPsecAH) {
967 if ((*Mask & AUTH_ALGO) == 0) {
968 ShellPrintHiiEx (
969 -1,
970 -1,
971 NULL,
972 STRING_TOKEN (STR_IPSEC_CONFIG_MISSING_PARAMETER),
973 mHiiHandle,
974 mAppName,
975 L"--auth-algo"
976 );
977 ReturnStatus = EFI_INVALID_PARAMETER;
978 } else if ((*Data)->AlgoInfo.EspAlgoInfo.AuthAlgoId != IPSEC_AALG_NONE && (*Mask & AUTH_KEY) == 0) {
979 ShellPrintHiiEx (
980 -1,
981 -1,
982 NULL,
983 STRING_TOKEN (STR_IPSEC_CONFIG_MISSING_PARAMETER),
984 mHiiHandle,
985 mAppName,
986 L"--auth-key"
987 );
988 ReturnStatus = EFI_INVALID_PARAMETER;
989 }
990 } else {
991 if ((*Mask & (ENCRYPT_ALGO|AUTH_ALGO)) != (ENCRYPT_ALGO|AUTH_ALGO) ) {
992 ShellPrintHiiEx (
993 -1,
994 -1,
995 NULL,
996 STRING_TOKEN (STR_IPSEC_CONFIG_MISSING_PARAMETER),
997 mHiiHandle,
998 mAppName,
999 L"--encrypt-algo --auth-algo"
1000 );
1001 ReturnStatus = EFI_INVALID_PARAMETER;
1002 } else if ((*Data)->AlgoInfo.EspAlgoInfo.EncAlgoId != IPSEC_EALG_NONE && (*Mask & ENCRYPT_KEY) == 0) {
1003 ShellPrintHiiEx (
1004 -1,
1005 -1,
1006 NULL,
1007 STRING_TOKEN (STR_IPSEC_CONFIG_MISSING_PARAMETER),
1008 mHiiHandle,
1009 mAppName,
1010 L"--encrypt-key"
1011 );
1012 ReturnStatus = EFI_INVALID_PARAMETER;
1013 } else if ((*Data)->AlgoInfo.EspAlgoInfo.AuthAlgoId != IPSEC_AALG_NONE && (*Mask & AUTH_KEY) == 0) {
1014 ShellPrintHiiEx (
1015 -1,
1016 -1,
1017 NULL,
1018 STRING_TOKEN (STR_IPSEC_CONFIG_MISSING_PARAMETER),
1019 mHiiHandle,
1020 mAppName,
1021 L"--auth-key"
1022 );
1023 ReturnStatus = EFI_INVALID_PARAMETER;
1024 }
1025 }
1026 }
1027 }
1028
1029 return ReturnStatus;
1030 }
1031
1032 /**
1033 Fill in EFI_IPSEC_PAD_ID and EFI_IPSEC_PAD_DATA through ParamPackage list.
1034
1035 @param[out] PadId The pointer to the EFI_IPSEC_PAD_ID structure.
1036 @param[out] Data The pointer to the EFI_IPSEC_PAD_DATA structure.
1037 @param[in] ParamPackage The pointer to the ParamPackage list.
1038 @param[out] Mask The pointer to the Mask.
1039 @param[in] CreateNew The switch to create new.
1040
1041 @retval EFI_SUCCESS Fill in EFI_IPSEC_PAD_ID and EFI_IPSEC_PAD_DATA successfully.
1042 @retval EFI_INVALID_PARAMETER Invalid user input parameter.
1043
1044 **/
1045 EFI_STATUS
1046 CreatePadEntry (
1047 OUT EFI_IPSEC_PAD_ID **PadId,
1048 OUT EFI_IPSEC_PAD_DATA **Data,
1049 IN LIST_ENTRY *ParamPackage,
1050 OUT UINT32 *Mask,
1051 IN BOOLEAN CreateNew
1052 )
1053 {
1054 EFI_STATUS Status;
1055 EFI_STATUS ReturnStatus;
1056 SHELL_FILE_HANDLE FileHandle;
1057 UINT64 FileSize;
1058 UINTN AuthDataLength;
1059 UINTN RevocationDataLength;
1060 UINTN DataLength;
1061 UINTN Index;
1062 CONST CHAR16 *ValueStr;
1063 UINTN DataSize;
1064
1065 Status = EFI_SUCCESS;
1066 ReturnStatus = EFI_SUCCESS;
1067 *Mask = 0;
1068 AuthDataLength = 0;
1069 RevocationDataLength = 0;
1070
1071 *PadId = AllocateZeroPool (sizeof (EFI_IPSEC_PAD_ID));
1072 ASSERT (*PadId != NULL);
1073
1074 //
1075 // Convert user imput from string to integer, and fill in EFI_IPSEC_PAD_ID.
1076 //
1077 ValueStr = ShellCommandLineGetValue (ParamPackage, L"--peer-address");
1078 if (ValueStr != NULL) {
1079 (*PadId)->PeerIdValid = FALSE;
1080 Status = EfiInetAddrRange ((CHAR16 *) ValueStr, &(*PadId)->Id.IpAddress);
1081 if (EFI_ERROR (Status)) {
1082 ShellPrintHiiEx (
1083 -1,
1084 -1,
1085 NULL,
1086 STRING_TOKEN (STR_IPSEC_CONFIG_INCORRECT_PARAMETER_VALUE),
1087 mHiiHandle,
1088 mAppName,
1089 L"--peer-address",
1090 ValueStr
1091 );
1092 ReturnStatus = EFI_INVALID_PARAMETER;
1093 } else {
1094 *Mask |= PEER_ADDRESS;
1095 }
1096 }
1097
1098 ValueStr = ShellCommandLineGetValue (ParamPackage, L"--peer-id");
1099 if (ValueStr != NULL) {
1100 (*PadId)->PeerIdValid = TRUE;
1101 StrnCpyS ((CHAR16 *) (*PadId)->Id.PeerId, MAX_PEERID_LEN / sizeof (CHAR16), ValueStr, MAX_PEERID_LEN / sizeof (CHAR16) - 1);
1102 *Mask |= PEER_ID;
1103 }
1104
1105 ValueStr = ShellCommandLineGetValue (ParamPackage, L"--auth-data");
1106 if (ValueStr != NULL) {
1107 if (ValueStr[0] == L'@') {
1108 //
1109 // Input is a file: --auth-data "@fs1:\My Certificates\tom.dat"
1110 //
1111 Status = ShellOpenFileByName (&ValueStr[1], &FileHandle, EFI_FILE_MODE_READ, 0);
1112 if (EFI_ERROR (Status)) {
1113 ShellPrintHiiEx (
1114 -1,
1115 -1,
1116 NULL,
1117 STRING_TOKEN (STR_IPSEC_CONFIG_FILE_OPEN_FAILED),
1118 mHiiHandle,
1119 mAppName,
1120 &ValueStr[1]
1121 );
1122 ReturnStatus = EFI_INVALID_PARAMETER;
1123 } else {
1124 Status = ShellGetFileSize (FileHandle, &FileSize);
1125 ShellCloseFile (&FileHandle);
1126 if (EFI_ERROR (Status)) {
1127 ShellPrintHiiEx (
1128 -1,
1129 -1,
1130 NULL,
1131 STRING_TOKEN (STR_IPSEC_CONFIG_FILE_OPEN_FAILED),
1132 mHiiHandle,
1133 mAppName,
1134 &ValueStr[1]
1135 );
1136 ReturnStatus = EFI_INVALID_PARAMETER;
1137 } else {
1138 AuthDataLength = (UINTN) FileSize;
1139 }
1140 }
1141 } else {
1142 AuthDataLength = StrLen (ValueStr);
1143 }
1144 }
1145
1146 ValueStr = ShellCommandLineGetValue (ParamPackage, L"--revocation-data");
1147 if (ValueStr != NULL) {
1148 RevocationDataLength = (StrLen (ValueStr) + 1) * sizeof (CHAR16);
1149 }
1150
1151 //
1152 // Allocate Buffer for Data. Add padding after each struct to make sure the alignment
1153 // in different Arch.
1154 //
1155 DataSize = ALIGN_VARIABLE (sizeof (EFI_IPSEC_PAD_DATA));
1156 DataSize = ALIGN_VARIABLE (DataSize + AuthDataLength);
1157 DataSize += RevocationDataLength;
1158
1159 *Data = AllocateZeroPool (DataSize);
1160 ASSERT (*Data != NULL);
1161
1162 (*Data)->AuthData = (VOID *) ALIGN_POINTER ((*Data + 1), sizeof (UINTN));
1163 (*Data)->RevocationData = (VOID *) ALIGN_POINTER (((UINT8 *) (*Data + 1) + AuthDataLength), sizeof (UINTN));
1164 (*Data)->AuthProtocol = EfiIPsecAuthProtocolIKEv1;
1165
1166 //
1167 // Convert user imput from string to integer, and fill in EFI_IPSEC_PAD_DATA.
1168 //
1169 Status = GetNumber (
1170 L"--auth-proto",
1171 0,
1172 &(*Data)->AuthProtocol,
1173 sizeof (EFI_IPSEC_AUTH_PROTOCOL_TYPE),
1174 mMapAuthProto,
1175 ParamPackage,
1176 FORMAT_STRING
1177 );
1178 if (!EFI_ERROR (Status)) {
1179 *Mask |= AUTH_PROTO;
1180 }
1181
1182 if (Status == EFI_INVALID_PARAMETER) {
1183 ReturnStatus = EFI_INVALID_PARAMETER;
1184 }
1185
1186 Status = GetNumber (
1187 L"--auth-method",
1188 0,
1189 &(*Data)->AuthMethod,
1190 sizeof (EFI_IPSEC_AUTH_METHOD),
1191 mMapAuthMethod,
1192 ParamPackage,
1193 FORMAT_STRING
1194 );
1195 if (!EFI_ERROR (Status)) {
1196 *Mask |= AUTH_METHOD;
1197 }
1198
1199 if (Status == EFI_INVALID_PARAMETER) {
1200 ReturnStatus = EFI_INVALID_PARAMETER;
1201 }
1202
1203 if (ShellCommandLineGetFlag (ParamPackage, L"--ike-id")) {
1204 (*Data)->IkeIdFlag = TRUE;
1205 *Mask |= IKE_ID;
1206 }
1207
1208 if (ShellCommandLineGetFlag (ParamPackage, L"--ike-id-")) {
1209 (*Data)->IkeIdFlag = FALSE;
1210 *Mask |= IKE_ID;
1211 }
1212
1213 ValueStr = ShellCommandLineGetValue (ParamPackage, L"--auth-data");
1214 if (ValueStr != NULL) {
1215 if (ValueStr[0] == L'@') {
1216 //
1217 // Input is a file: --auth-data "@fs1:\My Certificates\tom.dat"
1218 //
1219
1220 Status = ShellOpenFileByName (&ValueStr[1], &FileHandle, EFI_FILE_MODE_READ, 0);
1221 if (EFI_ERROR (Status)) {
1222 ShellPrintHiiEx (
1223 -1,
1224 -1,
1225 NULL,
1226 STRING_TOKEN (STR_IPSEC_CONFIG_FILE_OPEN_FAILED),
1227 mHiiHandle,
1228 mAppName,
1229 &ValueStr[1]
1230 );
1231 ReturnStatus = EFI_INVALID_PARAMETER;
1232 (*Data)->AuthData = NULL;
1233 } else {
1234 DataLength = AuthDataLength;
1235 Status = ShellReadFile (FileHandle, &DataLength, (*Data)->AuthData);
1236 ShellCloseFile (&FileHandle);
1237 if (EFI_ERROR (Status)) {
1238 ShellPrintHiiEx (
1239 -1,
1240 -1,
1241 NULL,
1242 STRING_TOKEN (STR_IPSEC_CONFIG_FILE_OPEN_FAILED),
1243 mHiiHandle,
1244 mAppName,
1245 &ValueStr[1]
1246 );
1247 ReturnStatus = EFI_INVALID_PARAMETER;
1248 (*Data)->AuthData = NULL;
1249 } else {
1250 ASSERT (DataLength == AuthDataLength);
1251 *Mask |= AUTH_DATA;
1252 }
1253 }
1254 } else {
1255 for (Index = 0; Index < AuthDataLength; Index++) {
1256 ((CHAR8 *) (*Data)->AuthData)[Index] = (CHAR8) ValueStr[Index];
1257 }
1258 (*Data)->AuthDataSize = AuthDataLength;
1259 *Mask |= AUTH_DATA;
1260 }
1261 }
1262
1263 ValueStr = ShellCommandLineGetValue (ParamPackage, L"--revocation-data");
1264 if (ValueStr != NULL) {
1265 CopyMem ((*Data)->RevocationData, ValueStr, RevocationDataLength);
1266 (*Data)->RevocationDataSize = RevocationDataLength;
1267 *Mask |= REVOCATION_DATA;
1268 } else {
1269 (*Data)->RevocationData = NULL;
1270 }
1271
1272 if (CreateNew) {
1273 if ((*Mask & (PEER_ID | PEER_ADDRESS)) == 0) {
1274 ShellPrintHiiEx (
1275 -1,
1276 -1,
1277 NULL,
1278 STRING_TOKEN (STR_IPSEC_CONFIG_MISSING_ONE_OF_PARAMETERS),
1279 mHiiHandle,
1280 mAppName,
1281 L"--peer-id --peer-address"
1282 );
1283 ReturnStatus = EFI_INVALID_PARAMETER;
1284 } else if ((*Mask & (AUTH_METHOD | AUTH_DATA)) != (AUTH_METHOD | AUTH_DATA)) {
1285 ShellPrintHiiEx (
1286 -1,
1287 -1,
1288 NULL,
1289 STRING_TOKEN (STR_IPSEC_CONFIG_MISSING_ONE_OF_PARAMETERS),
1290 mHiiHandle,
1291 mAppName,
1292 L"--auth-method --auth-data"
1293 );
1294 ReturnStatus = EFI_INVALID_PARAMETER;
1295 }
1296 }
1297
1298 return ReturnStatus;
1299 }
1300
1301 CREATE_POLICY_ENTRY mCreatePolicyEntry[] = {
1302 (CREATE_POLICY_ENTRY) CreateSpdEntry,
1303 (CREATE_POLICY_ENTRY) CreateSadEntry,
1304 (CREATE_POLICY_ENTRY) CreatePadEntry
1305 };
1306
1307 /**
1308 Combine old SPD entry with new SPD entry.
1309
1310 @param[in, out] OldSelector The pointer to the EFI_IPSEC_SPD_SELECTOR structure.
1311 @param[in, out] OldData The pointer to the EFI_IPSEC_SPD_DATA structure.
1312 @param[in] NewSelector The pointer to the EFI_IPSEC_SPD_SELECTOR structure.
1313 @param[in] NewData The pointer to the EFI_IPSEC_SPD_DATA structure.
1314 @param[in] Mask The pointer to the Mask.
1315 @param[out] CreateNew The switch to create new.
1316
1317 @retval EFI_SUCCESS Combined successfully.
1318 @retval EFI_INVALID_PARAMETER Invalid user input parameter.
1319
1320 **/
1321 EFI_STATUS
1322 CombineSpdEntry (
1323 IN OUT EFI_IPSEC_SPD_SELECTOR *OldSelector,
1324 IN OUT EFI_IPSEC_SPD_DATA *OldData,
1325 IN EFI_IPSEC_SPD_SELECTOR *NewSelector,
1326 IN EFI_IPSEC_SPD_DATA *NewData,
1327 IN UINT32 Mask,
1328 OUT BOOLEAN *CreateNew
1329 )
1330 {
1331
1332 //
1333 // Process Selector
1334 //
1335 *CreateNew = FALSE;
1336 if ((Mask & LOCAL) == 0) {
1337 NewSelector->LocalAddressCount = OldSelector->LocalAddressCount;
1338 NewSelector->LocalAddress = OldSelector->LocalAddress;
1339 } else if ((NewSelector->LocalAddressCount != OldSelector->LocalAddressCount) ||
1340 (CompareMem (NewSelector->LocalAddress, OldSelector->LocalAddress, NewSelector->LocalAddressCount * sizeof (EFI_IP_ADDRESS_INFO)) != 0)) {
1341 *CreateNew = TRUE;
1342 }
1343
1344 if ((Mask & REMOTE) == 0) {
1345 NewSelector->RemoteAddressCount = OldSelector->RemoteAddressCount;
1346 NewSelector->RemoteAddress = OldSelector->RemoteAddress;
1347 } else if ((NewSelector->RemoteAddressCount != OldSelector->RemoteAddressCount) ||
1348 (CompareMem (NewSelector->RemoteAddress, OldSelector->RemoteAddress, NewSelector->RemoteAddressCount * sizeof (EFI_IP_ADDRESS_INFO)) != 0)) {
1349 *CreateNew = TRUE;
1350 }
1351
1352 if ((Mask & PROTO) == 0) {
1353 NewSelector->NextLayerProtocol = OldSelector->NextLayerProtocol;
1354 } else if (NewSelector->NextLayerProtocol != OldSelector->NextLayerProtocol) {
1355 *CreateNew = TRUE;
1356 }
1357
1358 switch (NewSelector->NextLayerProtocol) {
1359 case EFI_IP4_PROTO_TCP:
1360 case EFI_IP4_PROTO_UDP:
1361 if ((Mask & LOCAL_PORT) == 0) {
1362 NewSelector->LocalPort = OldSelector->LocalPort;
1363 NewSelector->LocalPortRange = OldSelector->LocalPortRange;
1364 } else if ((NewSelector->LocalPort != OldSelector->LocalPort) ||
1365 (NewSelector->LocalPortRange != OldSelector->LocalPortRange)) {
1366 *CreateNew = TRUE;
1367 }
1368
1369 if ((Mask & REMOTE_PORT) == 0) {
1370 NewSelector->RemotePort = OldSelector->RemotePort;
1371 NewSelector->RemotePortRange = OldSelector->RemotePortRange;
1372 } else if ((NewSelector->RemotePort != OldSelector->RemotePort) ||
1373 (NewSelector->RemotePortRange != OldSelector->RemotePortRange)) {
1374 *CreateNew = TRUE;
1375 }
1376 break;
1377
1378 case EFI_IP4_PROTO_ICMP:
1379 if ((Mask & ICMP_TYPE) == 0) {
1380 NewSelector->LocalPort = OldSelector->LocalPort;
1381 } else if (NewSelector->LocalPort != OldSelector->LocalPort) {
1382 *CreateNew = TRUE;
1383 }
1384
1385 if ((Mask & ICMP_CODE) == 0) {
1386 NewSelector->RemotePort = OldSelector->RemotePort;
1387 } else if (NewSelector->RemotePort != OldSelector->RemotePort) {
1388 *CreateNew = TRUE;
1389 }
1390 break;
1391 }
1392 //
1393 // Process Data
1394 //
1395 OldData->SaIdCount = 0;
1396
1397 if ((Mask & NAME) != 0) {
1398 AsciiStrCpyS ((CHAR8 *) OldData->Name, MAX_PEERID_LEN, (CHAR8 *) NewData->Name);
1399 }
1400
1401 if ((Mask & PACKET_FLAG) != 0) {
1402 OldData->PackageFlag = NewData->PackageFlag;
1403 }
1404
1405 if ((Mask & ACTION) != 0) {
1406 OldData->Action = NewData->Action;
1407 }
1408
1409 if (OldData->Action != EfiIPsecActionProtect) {
1410 OldData->ProcessingPolicy = NULL;
1411 } else {
1412 //
1413 // Protect
1414 //
1415 if (OldData->ProcessingPolicy == NULL) {
1416 //
1417 // Just point to new data if originally NULL.
1418 //
1419 OldData->ProcessingPolicy = NewData->ProcessingPolicy;
1420 if (OldData->ProcessingPolicy->Mode == EfiIPsecTunnel &&
1421 (Mask & (TUNNEL_LOCAL | TUNNEL_REMOTE)) != (TUNNEL_LOCAL | TUNNEL_REMOTE)
1422 ) {
1423 //
1424 // Change to Protect action and Tunnel mode, but without providing local/remote tunnel address.
1425 //
1426 ShellPrintHiiEx (
1427 -1,
1428 -1,
1429 NULL,
1430 STRING_TOKEN (STR_IPSEC_CONFIG_MISSING_ONE_OF_PARAMETERS),
1431 mHiiHandle,
1432 mAppName,
1433 L"--tunnel-local --tunnel-remote"
1434 );
1435 return EFI_INVALID_PARAMETER;
1436 }
1437 } else {
1438 //
1439 // Modify some of the data.
1440 //
1441 if ((Mask & EXT_SEQUENCE) != 0) {
1442 OldData->ProcessingPolicy->ExtSeqNum = NewData->ProcessingPolicy->ExtSeqNum;
1443 }
1444
1445 if ((Mask & SEQUENCE_OVERFLOW) != 0) {
1446 OldData->ProcessingPolicy->SeqOverflow = NewData->ProcessingPolicy->SeqOverflow;
1447 }
1448
1449 if ((Mask & FRAGMENT_CHECK) != 0) {
1450 OldData->ProcessingPolicy->FragCheck = NewData->ProcessingPolicy->FragCheck;
1451 }
1452
1453 if ((Mask & LIFEBYTE) != 0) {
1454 OldData->ProcessingPolicy->SaLifetime.ByteCount = NewData->ProcessingPolicy->SaLifetime.ByteCount;
1455 }
1456
1457 if ((Mask & LIFETIME_SOFT) != 0) {
1458 OldData->ProcessingPolicy->SaLifetime.SoftLifetime = NewData->ProcessingPolicy->SaLifetime.SoftLifetime;
1459 }
1460
1461 if ((Mask & LIFETIME) != 0) {
1462 OldData->ProcessingPolicy->SaLifetime.HardLifetime = NewData->ProcessingPolicy->SaLifetime.HardLifetime;
1463 }
1464
1465 if ((Mask & MODE) != 0) {
1466 OldData->ProcessingPolicy->Mode = NewData->ProcessingPolicy->Mode;
1467 }
1468
1469 if ((Mask & IPSEC_PROTO) != 0) {
1470 OldData->ProcessingPolicy->Proto = NewData->ProcessingPolicy->Proto;
1471 }
1472
1473 if ((Mask & AUTH_ALGO) != 0) {
1474 OldData->ProcessingPolicy->AuthAlgoId = NewData->ProcessingPolicy->AuthAlgoId;
1475 }
1476
1477 if ((Mask & ENCRYPT_ALGO) != 0) {
1478 OldData->ProcessingPolicy->EncAlgoId = NewData->ProcessingPolicy->EncAlgoId;
1479 }
1480
1481 if (OldData->ProcessingPolicy->Mode != EfiIPsecTunnel) {
1482 OldData->ProcessingPolicy->TunnelOption = NULL;
1483 } else {
1484 if (OldData->ProcessingPolicy->TunnelOption == NULL) {
1485 //
1486 // Set from Transport mode to Tunnel mode, should ensure TUNNEL_LOCAL & TUNNEL_REMOTE both exists.
1487 //
1488 if ((Mask & (TUNNEL_LOCAL | TUNNEL_REMOTE)) != (TUNNEL_LOCAL | TUNNEL_REMOTE)) {
1489 ShellPrintHiiEx (
1490 -1,
1491 -1,
1492 NULL,
1493 STRING_TOKEN (STR_IPSEC_CONFIG_MISSING_ONE_OF_PARAMETERS),
1494 mHiiHandle,
1495 mAppName,
1496 L"--tunnel-local --tunnel-remote"
1497 );
1498 return EFI_INVALID_PARAMETER;
1499 }
1500
1501 OldData->ProcessingPolicy->TunnelOption = NewData->ProcessingPolicy->TunnelOption;
1502 } else {
1503 if ((Mask & TUNNEL_LOCAL) != 0) {
1504 CopyMem (
1505 &OldData->ProcessingPolicy->TunnelOption->LocalTunnelAddress,
1506 &NewData->ProcessingPolicy->TunnelOption->LocalTunnelAddress,
1507 sizeof (EFI_IP_ADDRESS)
1508 );
1509 }
1510
1511 if ((Mask & TUNNEL_REMOTE) != 0) {
1512 CopyMem (
1513 &OldData->ProcessingPolicy->TunnelOption->RemoteTunnelAddress,
1514 &NewData->ProcessingPolicy->TunnelOption->RemoteTunnelAddress,
1515 sizeof (EFI_IP_ADDRESS)
1516 );
1517 }
1518
1519 if ((Mask & DONT_FRAGMENT) != 0) {
1520 OldData->ProcessingPolicy->TunnelOption->DF = NewData->ProcessingPolicy->TunnelOption->DF;
1521 }
1522 }
1523 }
1524 }
1525 }
1526
1527 return EFI_SUCCESS;
1528 }
1529
1530 /**
1531 Combine old SAD entry with new SAD entry.
1532
1533 @param[in, out] OldSaId The pointer to the EFI_IPSEC_SA_ID structure.
1534 @param[in, out] OldData The pointer to the EFI_IPSEC_SA_DATA2 structure.
1535 @param[in] NewSaId The pointer to the EFI_IPSEC_SA_ID structure.
1536 @param[in] NewData The pointer to the EFI_IPSEC_SA_DATA2 structure.
1537 @param[in] Mask The pointer to the Mask.
1538 @param[out] CreateNew The switch to create new.
1539
1540 @retval EFI_SUCCESS Combined successfully.
1541 @retval EFI_INVALID_PARAMETER Invalid user input parameter.
1542
1543 **/
1544 EFI_STATUS
1545 CombineSadEntry (
1546 IN OUT EFI_IPSEC_SA_ID *OldSaId,
1547 IN OUT EFI_IPSEC_SA_DATA2 *OldData,
1548 IN EFI_IPSEC_SA_ID *NewSaId,
1549 IN EFI_IPSEC_SA_DATA2 *NewData,
1550 IN UINT32 Mask,
1551 OUT BOOLEAN *CreateNew
1552 )
1553 {
1554
1555 *CreateNew = FALSE;
1556
1557 if ((Mask & SPI) == 0) {
1558 NewSaId->Spi = OldSaId->Spi;
1559 } else if (NewSaId->Spi != OldSaId->Spi) {
1560 *CreateNew = TRUE;
1561 }
1562
1563 if ((Mask & IPSEC_PROTO) == 0) {
1564 NewSaId->Proto = OldSaId->Proto;
1565 } else if (NewSaId->Proto != OldSaId->Proto) {
1566 *CreateNew = TRUE;
1567 }
1568
1569 if ((Mask & DEST) == 0) {
1570 CopyMem (&NewData->TunnelDestinationAddress, &OldData->TunnelDestinationAddress, sizeof (EFI_IP_ADDRESS));
1571 } else if (CompareMem (&NewData->TunnelDestinationAddress, &OldData->TunnelDestinationAddress, sizeof (EFI_IP_ADDRESS)) != 0) {
1572 *CreateNew = TRUE;
1573 }
1574
1575 if ((Mask & SOURCE) == 0) {
1576 CopyMem (&NewData->TunnelSourceAddress, &OldData->TunnelSourceAddress, sizeof (EFI_IP_ADDRESS));
1577 } else if (CompareMem (&NewData->TunnelSourceAddress, &OldData->TunnelSourceAddress, sizeof (EFI_IP_ADDRESS)) != 0) {
1578 *CreateNew = TRUE;
1579 }
1580 //
1581 // Process SA_DATA.
1582 //
1583 if ((Mask & MODE) != 0) {
1584 OldData->Mode = NewData->Mode;
1585 }
1586
1587 if ((Mask & SEQUENCE_NUMBER) != 0) {
1588 OldData->SNCount = NewData->SNCount;
1589 }
1590
1591 if ((Mask & ANTIREPLAY_WINDOW) != 0) {
1592 OldData->AntiReplayWindows = NewData->AntiReplayWindows;
1593 }
1594
1595 if ((Mask & AUTH_ALGO) != 0) {
1596 OldData->AlgoInfo.EspAlgoInfo.AuthAlgoId = NewData->AlgoInfo.EspAlgoInfo.AuthAlgoId;
1597 }
1598
1599 if ((Mask & AUTH_KEY) != 0) {
1600 OldData->AlgoInfo.EspAlgoInfo.AuthKey = NewData->AlgoInfo.EspAlgoInfo.AuthKey;
1601 OldData->AlgoInfo.EspAlgoInfo.AuthKeyLength = NewData->AlgoInfo.EspAlgoInfo.AuthKeyLength;
1602 }
1603
1604 if ((Mask & ENCRYPT_ALGO) != 0) {
1605 OldData->AlgoInfo.EspAlgoInfo.EncAlgoId = NewData->AlgoInfo.EspAlgoInfo.EncAlgoId;
1606 }
1607
1608 if ((Mask & ENCRYPT_KEY) != 0) {
1609 OldData->AlgoInfo.EspAlgoInfo.EncKey = NewData->AlgoInfo.EspAlgoInfo.EncKey;
1610 OldData->AlgoInfo.EspAlgoInfo.EncKeyLength = NewData->AlgoInfo.EspAlgoInfo.EncKeyLength;
1611 }
1612
1613 if (NewSaId->Proto == EfiIPsecAH) {
1614 if ((Mask & (ENCRYPT_ALGO | ENCRYPT_KEY)) != 0) {
1615 //
1616 // Should not provide encrypt_* if AH.
1617 //
1618 ShellPrintHiiEx (
1619 -1,
1620 -1,
1621 NULL,
1622 STRING_TOKEN (STR_IPSEC_CONFIG_UNWANTED_PARAMETER),
1623 mHiiHandle,
1624 mAppName,
1625 L"--encrypt-algo --encrypt-key"
1626 );
1627 return EFI_INVALID_PARAMETER;
1628 }
1629 }
1630
1631 if (NewSaId->Proto == EfiIPsecESP && OldSaId->Proto == EfiIPsecAH) {
1632 //
1633 // AH -> ESP
1634 // Should provide encrypt_algo at least.
1635 //
1636 if ((Mask & ENCRYPT_ALGO) == 0) {
1637 ShellPrintHiiEx (
1638 -1,
1639 -1,
1640 NULL,
1641 STRING_TOKEN (STR_IPSEC_CONFIG_MISSING_PARAMETER),
1642 mHiiHandle,
1643 mAppName,
1644 L"--encrypt-algo"
1645 );
1646 return EFI_INVALID_PARAMETER;
1647 }
1648
1649 //
1650 // Encrypt_key should be provided if algorithm is not NONE.
1651 //
1652 if (NewData->AlgoInfo.EspAlgoInfo.EncAlgoId != IPSEC_EALG_NONE && (Mask & ENCRYPT_KEY) == 0) {
1653 ShellPrintHiiEx (
1654 -1,
1655 -1,
1656 NULL,
1657 STRING_TOKEN (STR_IPSEC_CONFIG_MISSING_PARAMETER),
1658 mHiiHandle,
1659 mAppName,
1660 L"--encrypt-algo"
1661 );
1662 return EFI_INVALID_PARAMETER;
1663 }
1664 }
1665
1666 if ((Mask & LIFEBYTE) != 0) {
1667 OldData->SaLifetime.ByteCount = NewData->SaLifetime.ByteCount;
1668 }
1669
1670 if ((Mask & LIFETIME_SOFT) != 0) {
1671 OldData->SaLifetime.SoftLifetime = NewData->SaLifetime.SoftLifetime;
1672 }
1673
1674 if ((Mask & LIFETIME) != 0) {
1675 OldData->SaLifetime.HardLifetime = NewData->SaLifetime.HardLifetime;
1676 }
1677
1678 if ((Mask & PATH_MTU) != 0) {
1679 OldData->PathMTU = NewData->PathMTU;
1680 }
1681 //
1682 // Process SpdSelector.
1683 //
1684 if (OldData->SpdSelector == NULL) {
1685 if ((Mask & (LOCAL | REMOTE | PROTO | LOCAL_PORT | REMOTE_PORT | ICMP_TYPE | ICMP_CODE)) != 0) {
1686 if ((Mask & (LOCAL | REMOTE | PROTO)) != (LOCAL | REMOTE | PROTO)) {
1687 ShellPrintHiiEx (
1688 -1,
1689 -1,
1690 NULL,
1691 STRING_TOKEN (STR_IPSEC_CONFIG_MISSING_ONE_OF_PARAMETERS),
1692 mHiiHandle,
1693 mAppName,
1694 L"--local --remote --proto"
1695 );
1696 return EFI_INVALID_PARAMETER;
1697 }
1698
1699 OldData->SpdSelector = NewData->SpdSelector;
1700 }
1701 } else {
1702 if ((Mask & LOCAL) != 0) {
1703 OldData->SpdSelector->LocalAddressCount = NewData->SpdSelector->LocalAddressCount;
1704 OldData->SpdSelector->LocalAddress = NewData->SpdSelector->LocalAddress;
1705 }
1706
1707 if ((Mask & REMOTE) != 0) {
1708 OldData->SpdSelector->RemoteAddressCount = NewData->SpdSelector->RemoteAddressCount;
1709 OldData->SpdSelector->RemoteAddress = NewData->SpdSelector->RemoteAddress;
1710 }
1711
1712 if ((Mask & PROTO) != 0) {
1713 OldData->SpdSelector->NextLayerProtocol = NewData->SpdSelector->NextLayerProtocol;
1714 }
1715
1716 if (OldData->SpdSelector != NULL) {
1717 switch (OldData->SpdSelector->NextLayerProtocol) {
1718 case EFI_IP4_PROTO_TCP:
1719 case EFI_IP4_PROTO_UDP:
1720 if ((Mask & LOCAL_PORT) != 0) {
1721 OldData->SpdSelector->LocalPort = NewData->SpdSelector->LocalPort;
1722 }
1723
1724 if ((Mask & REMOTE_PORT) != 0) {
1725 OldData->SpdSelector->RemotePort = NewData->SpdSelector->RemotePort;
1726 }
1727 break;
1728
1729 case EFI_IP4_PROTO_ICMP:
1730 if ((Mask & ICMP_TYPE) != 0) {
1731 OldData->SpdSelector->LocalPort = (UINT8) NewData->SpdSelector->LocalPort;
1732 }
1733
1734 if ((Mask & ICMP_CODE) != 0) {
1735 OldData->SpdSelector->RemotePort = (UINT8) NewData->SpdSelector->RemotePort;
1736 }
1737 break;
1738 }
1739 }
1740 }
1741
1742 return EFI_SUCCESS;
1743 }
1744
1745 /**
1746 Combine old PAD entry with new PAD entry.
1747
1748 @param[in, out] OldPadId The pointer to the EFI_IPSEC_PAD_ID structure.
1749 @param[in, out] OldData The pointer to the EFI_IPSEC_PAD_DATA structure.
1750 @param[in] NewPadId The pointer to the EFI_IPSEC_PAD_ID structure.
1751 @param[in] NewData The pointer to the EFI_IPSEC_PAD_DATA structure.
1752 @param[in] Mask The pointer to the Mask.
1753 @param[out] CreateNew The switch to create new.
1754
1755 @retval EFI_SUCCESS Combined successfully.
1756 @retval EFI_INVALID_PARAMETER Invalid user input parameter.
1757
1758 **/
1759 EFI_STATUS
1760 CombinePadEntry (
1761 IN OUT EFI_IPSEC_PAD_ID *OldPadId,
1762 IN OUT EFI_IPSEC_PAD_DATA *OldData,
1763 IN EFI_IPSEC_PAD_ID *NewPadId,
1764 IN EFI_IPSEC_PAD_DATA *NewData,
1765 IN UINT32 Mask,
1766 OUT BOOLEAN *CreateNew
1767 )
1768 {
1769
1770 *CreateNew = FALSE;
1771
1772 if ((Mask & (PEER_ID | PEER_ADDRESS)) == 0) {
1773 CopyMem (NewPadId, OldPadId, sizeof (EFI_IPSEC_PAD_ID));
1774 } else {
1775 if ((Mask & PEER_ID) != 0) {
1776 if (OldPadId->PeerIdValid) {
1777 if (StrCmp ((CONST CHAR16 *) OldPadId->Id.PeerId, (CONST CHAR16 *) NewPadId->Id.PeerId) != 0) {
1778 *CreateNew = TRUE;
1779 }
1780 } else {
1781 *CreateNew = TRUE;
1782 }
1783 } else {
1784 //
1785 // MASK & PEER_ADDRESS
1786 //
1787 if (OldPadId->PeerIdValid) {
1788 *CreateNew = TRUE;
1789 } else {
1790 if ((CompareMem (&OldPadId->Id.IpAddress.Address, &NewPadId->Id.IpAddress.Address, sizeof (EFI_IP_ADDRESS)) != 0) ||
1791 (OldPadId->Id.IpAddress.PrefixLength != NewPadId->Id.IpAddress.PrefixLength)) {
1792 *CreateNew = TRUE;
1793 }
1794 }
1795 }
1796 }
1797
1798 if ((Mask & AUTH_PROTO) != 0) {
1799 OldData->AuthProtocol = NewData->AuthProtocol;
1800 }
1801
1802 if ((Mask & AUTH_METHOD) != 0) {
1803 OldData->AuthMethod = NewData->AuthMethod;
1804 }
1805
1806 if ((Mask & IKE_ID) != 0) {
1807 OldData->IkeIdFlag = NewData->IkeIdFlag;
1808 }
1809
1810 if ((Mask & AUTH_DATA) != 0) {
1811 OldData->AuthDataSize = NewData->AuthDataSize;
1812 OldData->AuthData = NewData->AuthData;
1813 }
1814
1815 if ((Mask & REVOCATION_DATA) != 0) {
1816 OldData->RevocationDataSize = NewData->RevocationDataSize;
1817 OldData->RevocationData = NewData->RevocationData;
1818 }
1819
1820 return EFI_SUCCESS;
1821 }
1822
1823 COMBINE_POLICY_ENTRY mCombinePolicyEntry[] = {
1824 (COMBINE_POLICY_ENTRY) CombineSpdEntry,
1825 (COMBINE_POLICY_ENTRY) CombineSadEntry,
1826 (COMBINE_POLICY_ENTRY) CombinePadEntry
1827 };
1828
1829 /**
1830 Edit entry information in the database.
1831
1832 @param[in] Selector The pointer to the EFI_IPSEC_CONFIG_SELECTOR structure.
1833 @param[in] Data The pointer to the data.
1834 @param[in] Context The pointer to the INSERT_POLICY_ENTRY_CONTEXT structure.
1835
1836 @retval EFI_SUCCESS Continue the iteration.
1837 @retval EFI_ABORTED Abort the iteration.
1838 **/
1839 EFI_STATUS
1840 EditOperatePolicyEntry (
1841 IN EFI_IPSEC_CONFIG_SELECTOR *Selector,
1842 IN VOID *Data,
1843 IN EDIT_POLICY_ENTRY_CONTEXT *Context
1844 )
1845 {
1846 EFI_STATUS Status;
1847 BOOLEAN CreateNew;
1848
1849 if (mMatchPolicyEntry[Context->DataType] (Selector, Data, &Context->Indexer)) {
1850 ASSERT (Context->DataType < 3);
1851
1852 Status = mCombinePolicyEntry[Context->DataType] (
1853 Selector,
1854 Data,
1855 Context->Selector,
1856 Context->Data,
1857 Context->Mask,
1858 &CreateNew
1859 );
1860 if (!EFI_ERROR (Status)) {
1861 //
1862 // If the Selector already existed, this Entry will be updated by set data.
1863 //
1864 Status = mIpSecConfig->SetData (
1865 mIpSecConfig,
1866 Context->DataType,
1867 Context->Selector, /// New created selector.
1868 Data, /// Old date which has been modified, need to be set data.
1869 Selector
1870 );
1871 ASSERT_EFI_ERROR (Status);
1872
1873 if (CreateNew) {
1874 //
1875 // Edit the entry to a new one. So, we need delete the old entry.
1876 //
1877 Status = mIpSecConfig->SetData (
1878 mIpSecConfig,
1879 Context->DataType,
1880 Selector, /// Old selector.
1881 NULL, /// NULL means to delete this Entry specified by Selector.
1882 NULL
1883 );
1884 ASSERT_EFI_ERROR (Status);
1885 }
1886 }
1887
1888 Context->Status = Status;
1889 return EFI_ABORTED;
1890 }
1891
1892 return EFI_SUCCESS;
1893 }
1894
1895 /**
1896 Edit entry information in database according to datatype.
1897
1898 @param[in] DataType The value of EFI_IPSEC_CONFIG_DATA_TYPE.
1899 @param[in] ParamPackage The pointer to the ParamPackage list.
1900
1901 @retval EFI_SUCCESS Edit entry information successfully.
1902 @retval EFI_NOT_FOUND Can't find the specified entry.
1903 @retval Others Some mistaken case.
1904 **/
1905 EFI_STATUS
1906 EditPolicyEntry (
1907 IN EFI_IPSEC_CONFIG_DATA_TYPE DataType,
1908 IN LIST_ENTRY *ParamPackage
1909 )
1910 {
1911 EFI_STATUS Status;
1912 EDIT_POLICY_ENTRY_CONTEXT Context;
1913 CONST CHAR16 *ValueStr;
1914
1915 ValueStr = ShellCommandLineGetValue (ParamPackage, L"-e");
1916 if (ValueStr == NULL) {
1917 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_IPSEC_CONFIG_INDEX_NOT_SPECIFIED), mHiiHandle, mAppName, ValueStr);
1918 return EFI_NOT_FOUND;
1919 }
1920
1921 Status = mConstructPolicyEntryIndexer[DataType] (&Context.Indexer, ParamPackage);
1922 if (!EFI_ERROR (Status)) {
1923 Context.DataType = DataType;
1924 Context.Status = EFI_NOT_FOUND;
1925 Status = mCreatePolicyEntry[DataType] (&Context.Selector, &Context.Data, ParamPackage, &Context.Mask, FALSE);
1926 if (!EFI_ERROR (Status)) {
1927 ForeachPolicyEntry (DataType, (VISIT_POLICY_ENTRY) EditOperatePolicyEntry, &Context);
1928 Status = Context.Status;
1929 }
1930
1931 if (Context.Selector != NULL) {
1932 gBS->FreePool (Context.Selector);
1933 }
1934
1935 if (Context.Data != NULL) {
1936 gBS->FreePool (Context.Data);
1937 }
1938 }
1939
1940 if (Status == EFI_NOT_FOUND) {
1941 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_IPSEC_CONFIG_INDEX_NOT_FOUND), mHiiHandle, mAppName, ValueStr);
1942 } else if (EFI_ERROR (Status)) {
1943 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_IPSEC_CONFIG_EDIT_FAILED), mHiiHandle, mAppName);
1944 }
1945
1946 return Status;
1947
1948 }
1949
1950 /**
1951 Insert entry information in database.
1952
1953 @param[in] Selector The pointer to the EFI_IPSEC_CONFIG_SELECTOR structure.
1954 @param[in] Data The pointer to the data.
1955 @param[in] Context The pointer to the INSERT_POLICY_ENTRY_CONTEXT structure.
1956
1957 @retval EFI_SUCCESS Continue the iteration.
1958 @retval EFI_ABORTED Abort the iteration.
1959 **/
1960 EFI_STATUS
1961 InsertPolicyEntry (
1962 IN EFI_IPSEC_CONFIG_SELECTOR *Selector,
1963 IN VOID *Data,
1964 IN INSERT_POLICY_ENTRY_CONTEXT *Context
1965 )
1966 {
1967 //
1968 // Found the entry which we want to insert before.
1969 //
1970 if (mMatchPolicyEntry[Context->DataType] (Selector, Data, &Context->Indexer)) {
1971
1972 Context->Status = mIpSecConfig->SetData (
1973 mIpSecConfig,
1974 Context->DataType,
1975 Context->Selector,
1976 Context->Data,
1977 Selector
1978 );
1979 //
1980 // Abort the iteration after the insertion.
1981 //
1982 return EFI_ABORTED;
1983 }
1984
1985 return EFI_SUCCESS;
1986 }
1987
1988 /**
1989 Insert or add entry information in database according to datatype.
1990
1991 @param[in] DataType The value of EFI_IPSEC_CONFIG_DATA_TYPE.
1992 @param[in] ParamPackage The pointer to the ParamPackage list.
1993
1994 @retval EFI_SUCCESS Insert or add entry information successfully.
1995 @retval EFI_NOT_FOUND Can't find the specified entry.
1996 @retval EFI_BUFFER_TOO_SMALL The entry already existed.
1997 @retval EFI_UNSUPPORTED The operation is not supported.
1998 @retval Others Some mistaken case.
1999 **/
2000 EFI_STATUS
2001 AddOrInsertPolicyEntry (
2002 IN EFI_IPSEC_CONFIG_DATA_TYPE DataType,
2003 IN LIST_ENTRY *ParamPackage
2004 )
2005 {
2006 EFI_STATUS Status;
2007 EFI_IPSEC_CONFIG_SELECTOR *Selector;
2008 VOID *Data;
2009 INSERT_POLICY_ENTRY_CONTEXT Context;
2010 UINT32 Mask;
2011 UINTN DataSize;
2012 CONST CHAR16 *ValueStr;
2013
2014 Status = mCreatePolicyEntry[DataType] (&Selector, &Data, ParamPackage, &Mask, TRUE);
2015 if (!EFI_ERROR (Status)) {
2016 //
2017 // Find if the Selector to be inserted already exists.
2018 //
2019 DataSize = 0;
2020 Status = mIpSecConfig->GetData (
2021 mIpSecConfig,
2022 DataType,
2023 Selector,
2024 &DataSize,
2025 NULL
2026 );
2027 if (Status == EFI_BUFFER_TOO_SMALL) {
2028 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_IPSEC_CONFIG_ALREADY_EXISTS), mHiiHandle, mAppName);
2029 } else if (ShellCommandLineGetFlag (ParamPackage, L"-a")) {
2030 Status = mIpSecConfig->SetData (
2031 mIpSecConfig,
2032 DataType,
2033 Selector,
2034 Data,
2035 NULL
2036 );
2037 } else {
2038 ValueStr = ShellCommandLineGetValue (ParamPackage, L"-i");
2039 if (ValueStr == NULL) {
2040 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_IPSEC_CONFIG_INDEX_NOT_SPECIFIED), mHiiHandle, mAppName, ValueStr);
2041 return EFI_NOT_FOUND;
2042 }
2043
2044 Status = mConstructPolicyEntryIndexer[DataType] (&Context.Indexer, ParamPackage);
2045 if (!EFI_ERROR (Status)) {
2046 Context.DataType = DataType;
2047 Context.Status = EFI_NOT_FOUND;
2048 Context.Selector = Selector;
2049 Context.Data = Data;
2050
2051 ForeachPolicyEntry (DataType, (VISIT_POLICY_ENTRY) InsertPolicyEntry, &Context);
2052 Status = Context.Status;
2053 if (Status == EFI_NOT_FOUND) {
2054 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_IPSEC_CONFIG_INDEX_NOT_FOUND), mHiiHandle, mAppName, ValueStr);
2055 }
2056 }
2057 }
2058
2059 gBS->FreePool (Selector);
2060 gBS->FreePool (Data);
2061 }
2062
2063 if (Status == EFI_UNSUPPORTED) {
2064 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_IPSEC_CONFIG_INSERT_UNSUPPORT), mHiiHandle, mAppName);
2065 } else if (EFI_ERROR (Status)) {
2066 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_IPSEC_CONFIG_INSERT_FAILED), mHiiHandle, mAppName);
2067 }
2068
2069 return Status;
2070 }