2 iSCSI DHCP4 related configuration routines.
4 Copyright (c) 2004 - 2017, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "IScsiImpl.h"
19 Extract the Root Path option and get the required target information.
21 @param[in] RootPath The RootPath.
22 @param[in] Length Length of the RootPath option payload.
23 @param[in, out] ConfigData The iSCSI attempt configuration data read
24 from a nonvolatile device.
26 @retval EFI_SUCCESS All required information is extracted from the RootPath option.
27 @retval EFI_NOT_FOUND The RootPath is not an iSCSI RootPath.
28 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
29 @retval EFI_INVALID_PARAMETER The RootPath is malformatted.
33 IScsiDhcpExtractRootPath (
36 IN OUT ISCSI_ATTEMPT_CONFIG_NVDATA
*ConfigData
40 UINT8 IScsiRootPathIdLen
;
42 ISCSI_ROOT_PATH_FIELD Fields
[RP_FIELD_IDX_MAX
];
43 ISCSI_ROOT_PATH_FIELD
*Field
;
46 ISCSI_SESSION_CONFIG_NVDATA
*ConfigNvData
;
50 ConfigNvData
= &ConfigData
->SessionConfigData
;
53 // "iscsi:"<servername>":"<protocol>":"<port>":"<LUN>":"<targetname>
55 IScsiRootPathIdLen
= (UINT8
) AsciiStrLen (ISCSI_ROOT_PATH_ID
);
57 if ((Length
<= IScsiRootPathIdLen
) || (CompareMem (RootPath
, ISCSI_ROOT_PATH_ID
, IScsiRootPathIdLen
) != 0)) {
61 // Skip the iSCSI RootPath ID "iscsi:".
63 RootPath
+= IScsiRootPathIdLen
;
64 Length
= (UINT8
) (Length
- IScsiRootPathIdLen
);
66 TmpStr
= (CHAR8
*) AllocatePool (Length
+ 1);
68 return EFI_OUT_OF_RESOURCES
;
71 CopyMem (TmpStr
, RootPath
, Length
);
72 TmpStr
[Length
] = '\0';
75 FieldIndex
= RP_FIELD_IDX_SERVERNAME
;
76 ZeroMem (&Fields
[0], sizeof (Fields
));
79 // Extract the fields in the Root Path option string.
81 for (FieldIndex
= RP_FIELD_IDX_SERVERNAME
; (FieldIndex
< RP_FIELD_IDX_MAX
) && (Index
< Length
); FieldIndex
++) {
82 if (TmpStr
[Index
] != ISCSI_ROOT_PATH_FIELD_DELIMITER
) {
83 Fields
[FieldIndex
].Str
= &TmpStr
[Index
];
86 while ((TmpStr
[Index
] != ISCSI_ROOT_PATH_FIELD_DELIMITER
) && (Index
< Length
)) {
90 if (TmpStr
[Index
] == ISCSI_ROOT_PATH_FIELD_DELIMITER
) {
91 if (FieldIndex
!= RP_FIELD_IDX_TARGETNAME
) {
96 if (Fields
[FieldIndex
].Str
!= NULL
) {
97 Fields
[FieldIndex
].Len
= (UINT8
) AsciiStrLen (Fields
[FieldIndex
].Str
);
102 if (FieldIndex
!= RP_FIELD_IDX_MAX
) {
103 Status
= EFI_INVALID_PARAMETER
;
107 if ((Fields
[RP_FIELD_IDX_SERVERNAME
].Str
== NULL
) ||
108 (Fields
[RP_FIELD_IDX_TARGETNAME
].Str
== NULL
) ||
109 (Fields
[RP_FIELD_IDX_PROTOCOL
].Len
> 1)
112 Status
= EFI_INVALID_PARAMETER
;
116 // Get the IP address of the target.
118 Field
= &Fields
[RP_FIELD_IDX_SERVERNAME
];
120 if (ConfigNvData
->IpMode
< IP_MODE_AUTOCONFIG
) {
121 IpMode
= ConfigNvData
->IpMode
;
123 IpMode
= ConfigData
->AutoConfigureMode
;
127 // Server name is expressed as domain name, just save it.
129 if ((!NET_IS_DIGIT (*(Field
->Str
))) && (*(Field
->Str
) != '[')) {
130 ConfigNvData
->DnsMode
= TRUE
;
131 if (Field
->Len
> sizeof (ConfigNvData
->TargetUrl
)) {
132 return EFI_INVALID_PARAMETER
;
134 CopyMem (&ConfigNvData
->TargetUrl
, Field
->Str
, Field
->Len
);
135 ConfigNvData
->TargetUrl
[Field
->Len
+ 1] = '\0';
137 ZeroMem(ConfigNvData
->TargetUrl
, sizeof (ConfigNvData
->TargetUrl
));
138 Status
= IScsiAsciiStrToIp (Field
->Str
, IpMode
, &Ip
);
139 CopyMem (&ConfigNvData
->TargetIp
, &Ip
, sizeof (EFI_IP_ADDRESS
));
141 if (EFI_ERROR (Status
)) {
146 // Check the protocol type.
148 Field
= &Fields
[RP_FIELD_IDX_PROTOCOL
];
149 if ((Field
->Str
!= NULL
) && ((*(Field
->Str
) - '0') != EFI_IP_PROTO_TCP
)) {
150 Status
= EFI_INVALID_PARAMETER
;
154 // Get the port of the iSCSI target.
156 Field
= &Fields
[RP_FIELD_IDX_PORT
];
157 if (Field
->Str
!= NULL
) {
158 ConfigNvData
->TargetPort
= (UINT16
) AsciiStrDecimalToUintn (Field
->Str
);
160 ConfigNvData
->TargetPort
= ISCSI_WELL_KNOWN_PORT
;
165 Field
= &Fields
[RP_FIELD_IDX_LUN
];
166 if (Field
->Str
!= NULL
) {
167 Status
= IScsiAsciiStrToLun (Field
->Str
, ConfigNvData
->BootLun
);
168 if (EFI_ERROR (Status
)) {
172 ZeroMem (ConfigNvData
->BootLun
, sizeof (ConfigNvData
->BootLun
));
175 // Get the target iSCSI Name.
177 Field
= &Fields
[RP_FIELD_IDX_TARGETNAME
];
179 if (AsciiStrLen (Field
->Str
) > ISCSI_NAME_MAX_SIZE
- 1) {
180 Status
= EFI_INVALID_PARAMETER
;
184 // Validate the iSCSI name.
186 Status
= IScsiNormalizeName (Field
->Str
, AsciiStrLen (Field
->Str
));
187 if (EFI_ERROR (Status
)) {
191 AsciiStrCpyS (ConfigNvData
->TargetName
, ISCSI_NAME_MAX_SIZE
, Field
->Str
);
201 The callback function registerd to the DHCP4 instance that is used to select
202 the qualified DHCP OFFER.
204 @param[in] This The DHCP4 protocol.
205 @param[in] Context The context set when configuring the DHCP4 protocol.
206 @param[in] CurrentState The current state of the DHCP4 protocol.
207 @param[in] Dhcp4Event The event occurs in the current state.
208 @param[in] Packet The DHCP packet that is to be sent or was already received.
209 @param[out] NewPacket The packet used to replace the above Packet.
211 @retval EFI_SUCCESS Either the DHCP OFFER is qualified or we're not intereseted
213 @retval EFI_NOT_READY The DHCP OFFER packet doesn't match our requirements.
214 @retval Others Other errors as indicated.
219 IScsiDhcpSelectOffer (
220 IN EFI_DHCP4_PROTOCOL
*This
,
222 IN EFI_DHCP4_STATE CurrentState
,
223 IN EFI_DHCP4_EVENT Dhcp4Event
,
224 IN EFI_DHCP4_PACKET
*Packet
, OPTIONAL
225 OUT EFI_DHCP4_PACKET
**NewPacket OPTIONAL
230 EFI_DHCP4_PACKET_OPTION
**OptionList
;
233 if ((Dhcp4Event
!= Dhcp4RcvdOffer
) && (Dhcp4Event
!= Dhcp4SelectOffer
)) {
239 Status
= This
->Parse (This
, Packet
, &OptionCount
, NULL
);
240 if (Status
!= EFI_BUFFER_TOO_SMALL
) {
241 return EFI_NOT_READY
;
244 OptionList
= AllocatePool (OptionCount
* sizeof (EFI_DHCP4_PACKET_OPTION
*));
245 if (OptionList
== NULL
) {
246 return EFI_NOT_READY
;
249 Status
= This
->Parse (This
, Packet
, &OptionCount
, OptionList
);
250 if (EFI_ERROR (Status
)) {
251 FreePool (OptionList
);
252 return EFI_NOT_READY
;
255 for (Index
= 0; Index
< OptionCount
; Index
++) {
256 if (OptionList
[Index
]->OpCode
!= DHCP4_TAG_ROOTPATH
) {
260 Status
= IScsiDhcpExtractRootPath (
261 (CHAR8
*) &OptionList
[Index
]->Data
[0],
262 OptionList
[Index
]->Length
,
263 (ISCSI_ATTEMPT_CONFIG_NVDATA
*) Context
269 if ((Index
== OptionCount
)) {
270 Status
= EFI_NOT_READY
;
273 FreePool (OptionList
);
279 Parse the DHCP ACK to get the address configuration and DNS information.
281 @param[in] Dhcp4 The DHCP4 protocol.
282 @param[in, out] ConfigData The session configuration data.
284 @retval EFI_SUCCESS The DNS information is got from the DHCP ACK.
285 @retval EFI_NO_MAPPING DHCP failed to acquire address and other information.
286 @retval EFI_INVALID_PARAMETER The DHCP ACK's DNS option is malformatted.
287 @retval EFI_DEVICE_ERROR Other errors as indicated.
288 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
293 IN EFI_DHCP4_PROTOCOL
*Dhcp4
,
294 IN OUT ISCSI_ATTEMPT_CONFIG_NVDATA
*ConfigData
298 EFI_DHCP4_MODE_DATA Dhcp4ModeData
;
300 EFI_DHCP4_PACKET_OPTION
**OptionList
;
302 ISCSI_SESSION_CONFIG_NVDATA
*NvData
;
304 Status
= Dhcp4
->GetModeData (Dhcp4
, &Dhcp4ModeData
);
305 if (EFI_ERROR (Status
)) {
309 if (Dhcp4ModeData
.State
!= Dhcp4Bound
) {
310 return EFI_NO_MAPPING
;
313 NvData
= &ConfigData
->SessionConfigData
;
315 CopyMem (&NvData
->LocalIp
, &Dhcp4ModeData
.ClientAddress
, sizeof (EFI_IPv4_ADDRESS
));
316 CopyMem (&NvData
->SubnetMask
, &Dhcp4ModeData
.SubnetMask
, sizeof (EFI_IPv4_ADDRESS
));
317 CopyMem (&NvData
->Gateway
, &Dhcp4ModeData
.RouterAddress
, sizeof (EFI_IPv4_ADDRESS
));
322 Status
= Dhcp4
->Parse (Dhcp4
, Dhcp4ModeData
.ReplyPacket
, &OptionCount
, OptionList
);
323 if (Status
!= EFI_BUFFER_TOO_SMALL
) {
324 return EFI_DEVICE_ERROR
;
327 OptionList
= AllocatePool (OptionCount
* sizeof (EFI_DHCP4_PACKET_OPTION
*));
328 if (OptionList
== NULL
) {
329 return EFI_OUT_OF_RESOURCES
;
332 Status
= Dhcp4
->Parse (Dhcp4
, Dhcp4ModeData
.ReplyPacket
, &OptionCount
, OptionList
);
333 if (EFI_ERROR (Status
)) {
334 FreePool (OptionList
);
335 return EFI_DEVICE_ERROR
;
338 for (Index
= 0; Index
< OptionCount
; Index
++) {
340 // Get DNS server addresses and DHCP server address from this offer.
342 if (OptionList
[Index
]->OpCode
== DHCP4_TAG_DNS_SERVER
) {
344 if (((OptionList
[Index
]->Length
& 0x3) != 0) || (OptionList
[Index
]->Length
== 0)) {
345 Status
= EFI_INVALID_PARAMETER
;
349 // Primary DNS server address.
351 CopyMem (&ConfigData
->PrimaryDns
, &OptionList
[Index
]->Data
[0], sizeof (EFI_IPv4_ADDRESS
));
353 if (OptionList
[Index
]->Length
> 4) {
355 // Secondary DNS server address.
357 CopyMem (&ConfigData
->SecondaryDns
, &OptionList
[Index
]->Data
[4], sizeof (EFI_IPv4_ADDRESS
));
359 } else if (OptionList
[Index
]->OpCode
== DHCP4_TAG_SERVER_ID
) {
360 if (OptionList
[Index
]->Length
!= 4) {
361 Status
= EFI_INVALID_PARAMETER
;
365 CopyMem (&ConfigData
->DhcpServer
, &OptionList
[Index
]->Data
[0], sizeof (EFI_IPv4_ADDRESS
));
369 FreePool (OptionList
);
375 This function will switch the IP4 configuration policy to Static.
377 @param[in] Ip4Config2 Pointer to the IP4 configuration protocol.
379 @retval EFI_SUCCESS The policy is already configured to static.
380 @retval Others Other error as indicated.
385 IN EFI_IP4_CONFIG2_PROTOCOL
*Ip4Config2
388 EFI_IP4_CONFIG2_POLICY Policy
;
392 DataSize
= sizeof (EFI_IP4_CONFIG2_POLICY
);
393 Status
= Ip4Config2
->GetData (
395 Ip4Config2DataTypePolicy
,
399 if (EFI_ERROR (Status
)) {
403 if (Policy
!= Ip4Config2PolicyStatic
) {
404 Policy
= Ip4Config2PolicyStatic
;
405 Status
= Ip4Config2
->SetData (
407 Ip4Config2DataTypePolicy
,
408 sizeof (EFI_IP4_CONFIG2_POLICY
),
411 if (EFI_ERROR (Status
)) {
420 Parse the DHCP ACK to get the address configuration and DNS information.
422 @param[in] Image The handle of the driver image.
423 @param[in] Controller The handle of the controller.
424 @param[in, out] ConfigData The attempt configuration data.
426 @retval EFI_SUCCESS The DNS information is got from the DHCP ACK.
427 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
428 @retval EFI_NO_MEDIA There was a media error.
429 @retval Others Other errors as indicated.
435 IN EFI_HANDLE Controller
,
436 IN OUT ISCSI_ATTEMPT_CONFIG_NVDATA
*ConfigData
439 EFI_HANDLE Dhcp4Handle
;
440 EFI_IP4_CONFIG2_PROTOCOL
*Ip4Config2
;
441 EFI_DHCP4_PROTOCOL
*Dhcp4
;
443 EFI_DHCP4_PACKET_OPTION
*ParaList
;
444 EFI_DHCP4_CONFIG_DATA Dhcp4ConfigData
;
445 ISCSI_SESSION_CONFIG_NVDATA
*NvData
;
446 BOOLEAN MediaPresent
;
454 // Check media status before doing DHCP.
457 NetLibDetectMedia (Controller
, &MediaPresent
);
463 // DHCP4 service allows only one of its children to be configured in
464 // the active state, If the DHCP4 D.O.R.A started by IP4 auto
465 // configuration and has not been completed, the Dhcp4 state machine
466 // will not be in the right state for the iSCSI to start a new round D.O.R.A.
467 // So, we need to switch it's policy to static.
469 Status
= gBS
->HandleProtocol (Controller
, &gEfiIp4Config2ProtocolGuid
, (VOID
**) &Ip4Config2
);
470 if (!EFI_ERROR (Status
)) {
471 Status
= IScsiSetIp4Policy (Ip4Config2
);
472 if (EFI_ERROR (Status
)) {
478 // Create a DHCP4 child instance and get the protocol.
480 Status
= NetLibCreateServiceChild (
483 &gEfiDhcp4ServiceBindingProtocolGuid
,
486 if (EFI_ERROR (Status
)) {
490 Status
= gBS
->OpenProtocol (
492 &gEfiDhcp4ProtocolGuid
,
496 EFI_OPEN_PROTOCOL_BY_DRIVER
498 if (EFI_ERROR (Status
)) {
502 NvData
= &ConfigData
->SessionConfigData
;
504 ParaList
= AllocatePool (sizeof (EFI_DHCP4_PACKET_OPTION
) + 3);
505 if (ParaList
== NULL
) {
506 Status
= EFI_OUT_OF_RESOURCES
;
511 // Ask the server to reply with Netmask, Router, DNS, and RootPath options.
513 ParaList
->OpCode
= DHCP4_TAG_PARA_LIST
;
514 ParaList
->Length
= (UINT8
) (NvData
->TargetInfoFromDhcp
? 4 : 3);
515 ParaList
->Data
[0] = DHCP4_TAG_NETMASK
;
516 ParaList
->Data
[1] = DHCP4_TAG_ROUTER
;
517 ParaList
->Data
[2] = DHCP4_TAG_DNS_SERVER
;
518 ParaList
->Data
[3] = DHCP4_TAG_ROOTPATH
;
520 ZeroMem (&Dhcp4ConfigData
, sizeof (EFI_DHCP4_CONFIG_DATA
));
521 Dhcp4ConfigData
.OptionCount
= 1;
522 Dhcp4ConfigData
.OptionList
= &ParaList
;
524 if (NvData
->TargetInfoFromDhcp
) {
526 // Use callback to select an offer that contains target information.
528 Dhcp4ConfigData
.Dhcp4Callback
= IScsiDhcpSelectOffer
;
529 Dhcp4ConfigData
.CallbackContext
= ConfigData
;
532 Status
= Dhcp4
->Configure (Dhcp4
, &Dhcp4ConfigData
);
533 if (EFI_ERROR (Status
)) {
537 Status
= Dhcp4
->Start (Dhcp4
, NULL
);
538 if (EFI_ERROR (Status
)) {
542 // Parse the ACK to get required information.
544 Status
= IScsiParseDhcpAck (Dhcp4
, ConfigData
);
548 if (ParaList
!= NULL
) {
554 Dhcp4
->Configure (Dhcp4
, NULL
);
558 &gEfiDhcp4ProtocolGuid
,
564 NetLibDestroyServiceChild (
567 &gEfiDhcp4ServiceBindingProtocolGuid
,