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