1 /** @file
2   The implementation of construct ENTRY_INDEXER in IpSecConfig application.
3 
4   Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
5 
6   This program and the accompanying materials
7   are licensed and made available under the terms and conditions of the BSD License
8   which accompanies this distribution.  The full text of the license may be found at
9   http://opensource.org/licenses/bsd-license.php.
10 
11   THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12   WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13 
14 **/
15 
16 #include "IpSecConfig.h"
17 #include "Indexer.h"
18 #include "Helper.h"
19 
20 /**
21   Fill in SPD_ENTRY_INDEXER through ParamPackage list.
22 
23   @param[in, out] Indexer         The pointer to the SPD_ENTRY_INDEXER structure.
24   @param[in]      ParamPackage    The pointer to the ParamPackage list.
25 
26   @retval EFI_SUCCESS    Filled in SPD_ENTRY_INDEXER successfully.
27 **/
28 EFI_STATUS
ConstructSpdIndexer(IN OUT SPD_ENTRY_INDEXER * Indexer,IN LIST_ENTRY * ParamPackage)29 ConstructSpdIndexer (
30   IN OUT SPD_ENTRY_INDEXER    *Indexer,
31   IN     LIST_ENTRY           *ParamPackage
32   )
33 {
34   EFI_STATUS      Status;
35   UINT64          Value64;
36   CONST CHAR16    *ValueStr;
37 
38   ValueStr = NULL;
39 
40   if (ShellCommandLineGetFlag (ParamPackage, L"-i")) {
41     ValueStr = ShellCommandLineGetValue (ParamPackage, L"-i");
42   } else if (ShellCommandLineGetFlag (ParamPackage, L"-d")) {
43     ValueStr = ShellCommandLineGetValue (ParamPackage, L"-d");
44   } else if (ShellCommandLineGetFlag (ParamPackage, L"-e")) {
45     ValueStr = ShellCommandLineGetValue (ParamPackage, L"-e");
46   } else {
47     ASSERT (FALSE);
48   }
49 
50   ASSERT (ValueStr != NULL);
51 
52   Value64 = StrToUInteger (ValueStr, &Status);
53   if (!EFI_ERROR (Status)) {
54     Indexer->Index = (UINTN) Value64;
55     Indexer->Name  = NULL;
56   } else {
57     UnicodeStrToAsciiStr (ValueStr, (CHAR8 *) Indexer->Name);
58   }
59 
60   return EFI_SUCCESS;
61 }
62 
63 /**
64   Fill in SAD_ENTRY_INDEXER through ParamPackage list.
65 
66   @param[in, out] Indexer         The pointer to the SAD_ENTRY_INDEXER structure.
67   @param[in]      ParamPackage    The pointer to the ParamPackage list.
68 
69   @retval EFI_SUCCESS              Filled in SPD_ENTRY_INDEXER successfully.
70   @retval EFI_INVALID_PARAMETER    The mistaken user input in ParamPackage list.
71 **/
72 EFI_STATUS
ConstructSadIndexer(IN OUT SAD_ENTRY_INDEXER * Indexer,IN LIST_ENTRY * ParamPackage)73 ConstructSadIndexer (
74   IN OUT SAD_ENTRY_INDEXER    *Indexer,
75   IN     LIST_ENTRY           *ParamPackage
76   )
77 {
78   EFI_STATUS      Status;
79   EFI_STATUS      Status1;
80   UINT64          Value64;
81   CONST CHAR16    *ValueStr;
82 
83   ValueStr = NULL;
84 
85   if (ShellCommandLineGetFlag (ParamPackage, L"-i")) {
86     ValueStr = ShellCommandLineGetValue (ParamPackage, L"-i");
87   } else if (ShellCommandLineGetFlag (ParamPackage, L"-d")) {
88     ValueStr = ShellCommandLineGetValue (ParamPackage, L"-d");
89   } else if (ShellCommandLineGetFlag (ParamPackage, L"-e")) {
90     ValueStr = ShellCommandLineGetValue (ParamPackage, L"-e");
91   } else {
92     ASSERT (FALSE);
93   }
94 
95   ASSERT (ValueStr != NULL);
96 
97   Value64 = StrToUInteger (ValueStr, &Status);
98   if (!EFI_ERROR (Status)) {
99     Indexer->Index = (UINTN) Value64;
100     ZeroMem (&Indexer->SaId, sizeof (EFI_IPSEC_SA_ID));
101   } else {
102     if ((!ShellCommandLineGetFlag (ParamPackage, L"--lookup-spi")) ||
103         (!ShellCommandLineGetFlag (ParamPackage, L"--lookup-ipsec-proto")) ||
104         (!ShellCommandLineGetFlag (ParamPackage, L"--lookup-dest"))) {
105       ShellPrintHiiEx (
106         -1,
107         -1,
108         NULL,
109         STRING_TOKEN (STR_IPSEC_CONFIG_MISSING_ONE_OF_PARAMETERS),
110         mHiiHandle,
111         mAppName,
112         L"--lookup-spi --lookup-ipsec-proto --lookup-dest"
113         );
114       return EFI_INVALID_PARAMETER;
115     }
116 
117     Status = GetNumber (
118               L"--lookup-spi",
119               (UINT32) -1,
120               &Indexer->SaId.Spi,
121               sizeof (UINT32),
122               NULL,
123               ParamPackage,
124               FORMAT_NUMBER
125               );
126     Status1 = GetNumber (
127                 L"--lookup-ipsec-proto",
128                 0,
129                 &Indexer->SaId.Proto,
130                 sizeof (EFI_IPSEC_PROTOCOL_TYPE),
131                 mMapIpSecProtocol,
132                 ParamPackage,
133                 FORMAT_STRING
134                 );
135 
136     if (EFI_ERROR (Status) || EFI_ERROR (Status1)) {
137       return EFI_INVALID_PARAMETER;
138     }
139 
140     ValueStr = ShellCommandLineGetValue (ParamPackage, L"--lookup-dest");
141     ASSERT (ValueStr != NULL);
142 
143     Status = EfiInetAddr2 ((CHAR16 *) ValueStr, &Indexer->SaId.DestAddress);
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"--lookup-dest",
153         ValueStr
154         );
155       return EFI_INVALID_PARAMETER;
156     }
157   }
158 
159   return EFI_SUCCESS;
160 }
161 
162 /**
163   Fill in PAD_ENTRY_INDEXER through ParamPackage list.
164 
165   @param[in, out] Indexer         The pointer to the PAD_ENTRY_INDEXER structure.
166   @param[in]      ParamPackage    The pointer to the ParamPackage list.
167 
168   @retval EFI_SUCCESS              Filled in PAD_ENTRY_INDEXER successfully.
169   @retval EFI_INVALID_PARAMETER    The mistaken user input in ParamPackage list.
170 **/
171 EFI_STATUS
ConstructPadIndexer(IN OUT PAD_ENTRY_INDEXER * Indexer,IN LIST_ENTRY * ParamPackage)172 ConstructPadIndexer (
173   IN OUT PAD_ENTRY_INDEXER    *Indexer,
174   IN     LIST_ENTRY           *ParamPackage
175   )
176 {
177   EFI_STATUS      Status;
178   UINT64          Value64;
179   CONST CHAR16    *ValueStr;
180 
181   ValueStr = NULL;
182 
183   if (ShellCommandLineGetFlag (ParamPackage, L"-i")) {
184     ValueStr = ShellCommandLineGetValue (ParamPackage, L"-i");
185   } else if (ShellCommandLineGetFlag (ParamPackage, L"-d")) {
186     ValueStr = ShellCommandLineGetValue (ParamPackage, L"-d");
187   } else if (ShellCommandLineGetFlag (ParamPackage, L"-e")) {
188     ValueStr = ShellCommandLineGetValue (ParamPackage, L"-e");
189   } else {
190     ASSERT (FALSE);
191   }
192 
193   ASSERT (ValueStr != NULL);
194 
195   Value64 = StrToUInteger (ValueStr, &Status);
196 
197   if (!EFI_ERROR (Status)) {
198     Indexer->Index = (UINTN) Value64;
199     ZeroMem (&Indexer->PadId, sizeof (EFI_IPSEC_PAD_ID));
200   } else {
201 
202     if (ShellCommandLineGetFlag (ParamPackage, L"--lookup-peer-address")) {
203       ValueStr = ShellCommandLineGetValue (ParamPackage, L"--lookup-peer-address");
204       ASSERT (ValueStr != NULL);
205 
206       Indexer->PadId.PeerIdValid = FALSE;
207       Status = EfiInetAddrRange ((CHAR16 *) ValueStr, &Indexer->PadId.Id.IpAddress);
208       if (EFI_ERROR (Status)) {
209         ShellPrintHiiEx (
210           -1,
211           -1,
212           NULL,
213           STRING_TOKEN (STR_IPSEC_CONFIG_INCORRECT_PARAMETER_VALUE),
214           mHiiHandle,
215           mAppName,
216           L"--lookup-peer-address",
217           ValueStr
218           );
219         return EFI_INVALID_PARAMETER;
220       }
221     } else {
222       ValueStr = ShellCommandLineGetValue (ParamPackage, L"--lookup-peer-id");
223       if (ValueStr == NULL) {
224         ShellPrintHiiEx (
225           -1,
226           -1,
227           NULL,
228           STRING_TOKEN (STR_IPSEC_CONFIG_MISSING_ONE_OF_PARAMETERS),
229           mHiiHandle,
230           mAppName,
231           L"--lookup-peer-address --lookup-peer-id"
232           );
233         return EFI_INVALID_PARAMETER;
234       }
235 
236       Indexer->PadId.PeerIdValid = TRUE;
237       ZeroMem (Indexer->PadId.Id.PeerId, MAX_PEERID_LEN);
238       StrnCpy ((CHAR16 *) Indexer->PadId.Id.PeerId, ValueStr, ARRAY_SIZE (Indexer->PadId.Id.PeerId) - 1);
239     }
240   }
241 
242   return EFI_SUCCESS;
243 }
244 
245 CONSTRUCT_POLICY_ENTRY_INDEXER mConstructPolicyEntryIndexer[] = {
246   (CONSTRUCT_POLICY_ENTRY_INDEXER) ConstructSpdIndexer,
247   (CONSTRUCT_POLICY_ENTRY_INDEXER) ConstructSadIndexer,
248   (CONSTRUCT_POLICY_ENTRY_INDEXER) ConstructPadIndexer
249 };
250