2 iSCSI DHCP4 related configuration routines.
4 Copyright (c) 2004 - 2018, 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 ConfigNvData
->DnsMode
= FALSE
;
138 ZeroMem(ConfigNvData
->TargetUrl
, sizeof (ConfigNvData
->TargetUrl
));
139 Status
= IScsiAsciiStrToIp (Field
->Str
, IpMode
, &Ip
);
140 CopyMem (&ConfigNvData
->TargetIp
, &Ip
, sizeof (EFI_IP_ADDRESS
));
142 if (EFI_ERROR (Status
)) {
147 // Check the protocol type.
149 Field
= &Fields
[RP_FIELD_IDX_PROTOCOL
];
150 if ((Field
->Str
!= NULL
) && ((*(Field
->Str
) - '0') != EFI_IP_PROTO_TCP
)) {
151 Status
= EFI_INVALID_PARAMETER
;
155 // Get the port of the iSCSI target.
157 Field
= &Fields
[RP_FIELD_IDX_PORT
];
158 if (Field
->Str
!= NULL
) {
159 ConfigNvData
->TargetPort
= (UINT16
) AsciiStrDecimalToUintn (Field
->Str
);
161 ConfigNvData
->TargetPort
= ISCSI_WELL_KNOWN_PORT
;
166 Field
= &Fields
[RP_FIELD_IDX_LUN
];
167 if (Field
->Str
!= NULL
) {
168 Status
= IScsiAsciiStrToLun (Field
->Str
, ConfigNvData
->BootLun
);
169 if (EFI_ERROR (Status
)) {
173 ZeroMem (ConfigNvData
->BootLun
, sizeof (ConfigNvData
->BootLun
));
176 // Get the target iSCSI Name.
178 Field
= &Fields
[RP_FIELD_IDX_TARGETNAME
];
180 if (AsciiStrLen (Field
->Str
) > ISCSI_NAME_MAX_SIZE
- 1) {
181 Status
= EFI_INVALID_PARAMETER
;
185 // Validate the iSCSI name.
187 Status
= IScsiNormalizeName (Field
->Str
, AsciiStrLen (Field
->Str
));
188 if (EFI_ERROR (Status
)) {
192 AsciiStrCpyS (ConfigNvData
->TargetName
, ISCSI_NAME_MAX_SIZE
, Field
->Str
);
202 The callback function registerd to the DHCP4 instance that is used to select
203 the qualified DHCP OFFER.
205 @param[in] This The DHCP4 protocol.
206 @param[in] Context The context set when configuring the DHCP4 protocol.
207 @param[in] CurrentState The current state of the DHCP4 protocol.
208 @param[in] Dhcp4Event The event occurs in the current state.
209 @param[in] Packet The DHCP packet that is to be sent or was already received.
210 @param[out] NewPacket The packet used to replace the above Packet.
212 @retval EFI_SUCCESS Either the DHCP OFFER is qualified or we're not intereseted
214 @retval EFI_NOT_READY The DHCP OFFER packet doesn't match our requirements.
215 @retval Others Other errors as indicated.
220 IScsiDhcpSelectOffer (
221 IN EFI_DHCP4_PROTOCOL
*This
,
223 IN EFI_DHCP4_STATE CurrentState
,
224 IN EFI_DHCP4_EVENT Dhcp4Event
,
225 IN EFI_DHCP4_PACKET
*Packet
, OPTIONAL
226 OUT EFI_DHCP4_PACKET
**NewPacket OPTIONAL
231 EFI_DHCP4_PACKET_OPTION
**OptionList
;
234 if ((Dhcp4Event
!= Dhcp4RcvdOffer
) && (Dhcp4Event
!= Dhcp4SelectOffer
)) {
240 Status
= This
->Parse (This
, Packet
, &OptionCount
, NULL
);
241 if (Status
!= EFI_BUFFER_TOO_SMALL
) {
242 return EFI_NOT_READY
;
245 OptionList
= AllocatePool (OptionCount
* sizeof (EFI_DHCP4_PACKET_OPTION
*));
246 if (OptionList
== NULL
) {
247 return EFI_NOT_READY
;
250 Status
= This
->Parse (This
, Packet
, &OptionCount
, OptionList
);
251 if (EFI_ERROR (Status
)) {
252 FreePool (OptionList
);
253 return EFI_NOT_READY
;
256 for (Index
= 0; Index
< OptionCount
; Index
++) {
257 if (OptionList
[Index
]->OpCode
!= DHCP4_TAG_ROOTPATH
) {
261 Status
= IScsiDhcpExtractRootPath (
262 (CHAR8
*) &OptionList
[Index
]->Data
[0],
263 OptionList
[Index
]->Length
,
264 (ISCSI_ATTEMPT_CONFIG_NVDATA
*) Context
270 if (Index
== OptionCount
) {
271 Status
= EFI_NOT_READY
;
274 FreePool (OptionList
);
280 Parse the DHCP ACK to get the address configuration and DNS information.
282 @param[in] Dhcp4 The DHCP4 protocol.
283 @param[in, out] ConfigData The session configuration data.
285 @retval EFI_SUCCESS The DNS information is got from the DHCP ACK.
286 @retval EFI_NO_MAPPING DHCP failed to acquire address and other information.
287 @retval EFI_INVALID_PARAMETER The DHCP ACK's DNS option is malformatted.
288 @retval EFI_DEVICE_ERROR Other errors as indicated.
289 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
294 IN EFI_DHCP4_PROTOCOL
*Dhcp4
,
295 IN OUT ISCSI_ATTEMPT_CONFIG_NVDATA
*ConfigData
299 EFI_DHCP4_MODE_DATA Dhcp4ModeData
;
301 EFI_DHCP4_PACKET_OPTION
**OptionList
;
303 ISCSI_SESSION_CONFIG_NVDATA
*NvData
;
305 Status
= Dhcp4
->GetModeData (Dhcp4
, &Dhcp4ModeData
);
306 if (EFI_ERROR (Status
)) {
310 if (Dhcp4ModeData
.State
!= Dhcp4Bound
) {
311 return EFI_NO_MAPPING
;
314 NvData
= &ConfigData
->SessionConfigData
;
316 CopyMem (&NvData
->LocalIp
, &Dhcp4ModeData
.ClientAddress
, sizeof (EFI_IPv4_ADDRESS
));
317 CopyMem (&NvData
->SubnetMask
, &Dhcp4ModeData
.SubnetMask
, sizeof (EFI_IPv4_ADDRESS
));
318 CopyMem (&NvData
->Gateway
, &Dhcp4ModeData
.RouterAddress
, sizeof (EFI_IPv4_ADDRESS
));
323 Status
= Dhcp4
->Parse (Dhcp4
, Dhcp4ModeData
.ReplyPacket
, &OptionCount
, OptionList
);
324 if (Status
!= EFI_BUFFER_TOO_SMALL
) {
325 return EFI_DEVICE_ERROR
;
328 OptionList
= AllocatePool (OptionCount
* sizeof (EFI_DHCP4_PACKET_OPTION
*));
329 if (OptionList
== NULL
) {
330 return EFI_OUT_OF_RESOURCES
;
333 Status
= Dhcp4
->Parse (Dhcp4
, Dhcp4ModeData
.ReplyPacket
, &OptionCount
, OptionList
);
334 if (EFI_ERROR (Status
)) {
335 FreePool (OptionList
);
336 return EFI_DEVICE_ERROR
;
339 for (Index
= 0; Index
< OptionCount
; Index
++) {
341 // Get DNS server addresses and DHCP server address from this offer.
343 if (OptionList
[Index
]->OpCode
== DHCP4_TAG_DNS_SERVER
) {
345 if (((OptionList
[Index
]->Length
& 0x3) != 0) || (OptionList
[Index
]->Length
== 0)) {
346 Status
= EFI_INVALID_PARAMETER
;
350 // Primary DNS server address.
352 CopyMem (&ConfigData
->PrimaryDns
, &OptionList
[Index
]->Data
[0], sizeof (EFI_IPv4_ADDRESS
));
354 if (OptionList
[Index
]->Length
> 4) {
356 // Secondary DNS server address.
358 CopyMem (&ConfigData
->SecondaryDns
, &OptionList
[Index
]->Data
[4], sizeof (EFI_IPv4_ADDRESS
));
360 } else if (OptionList
[Index
]->OpCode
== DHCP4_TAG_SERVER_ID
) {
361 if (OptionList
[Index
]->Length
!= 4) {
362 Status
= EFI_INVALID_PARAMETER
;
366 CopyMem (&ConfigData
->DhcpServer
, &OptionList
[Index
]->Data
[0], sizeof (EFI_IPv4_ADDRESS
));
370 FreePool (OptionList
);
376 This function will switch the IP4 configuration policy to Static.
378 @param[in] Ip4Config2 Pointer to the IP4 configuration protocol.
380 @retval EFI_SUCCESS The policy is already configured to static.
381 @retval Others Other error as indicated.
386 IN EFI_IP4_CONFIG2_PROTOCOL
*Ip4Config2
389 EFI_IP4_CONFIG2_POLICY Policy
;
393 DataSize
= sizeof (EFI_IP4_CONFIG2_POLICY
);
394 Status
= Ip4Config2
->GetData (
396 Ip4Config2DataTypePolicy
,
400 if (EFI_ERROR (Status
)) {
404 if (Policy
!= Ip4Config2PolicyStatic
) {
405 Policy
= Ip4Config2PolicyStatic
;
406 Status
= Ip4Config2
->SetData (
408 Ip4Config2DataTypePolicy
,
409 sizeof (EFI_IP4_CONFIG2_POLICY
),
412 if (EFI_ERROR (Status
)) {
421 Parse the DHCP ACK to get the address configuration and DNS information.
423 @param[in] Image The handle of the driver image.
424 @param[in] Controller The handle of the controller.
425 @param[in, out] ConfigData The attempt configuration data.
427 @retval EFI_SUCCESS The DNS information is got from the DHCP ACK.
428 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
429 @retval EFI_NO_MEDIA There was a media error.
430 @retval Others Other errors as indicated.
436 IN EFI_HANDLE Controller
,
437 IN OUT ISCSI_ATTEMPT_CONFIG_NVDATA
*ConfigData
440 EFI_HANDLE Dhcp4Handle
;
441 EFI_IP4_CONFIG2_PROTOCOL
*Ip4Config2
;
442 EFI_DHCP4_PROTOCOL
*Dhcp4
;
444 EFI_DHCP4_PACKET_OPTION
*ParaList
;
445 EFI_DHCP4_CONFIG_DATA Dhcp4ConfigData
;
446 ISCSI_SESSION_CONFIG_NVDATA
*NvData
;
447 EFI_STATUS MediaStatus
;
455 // Check media status before doing DHCP.
457 MediaStatus
= EFI_SUCCESS
;
458 NetLibDetectMediaWaitTimeout (Controller
, ISCSI_CHECK_MEDIA_GET_DHCP_WAITING_TIME
, &MediaStatus
);
459 if (MediaStatus
!= EFI_SUCCESS
) {
460 AsciiPrint ("\n Error: Could not detect network connection.\n");
465 // DHCP4 service allows only one of its children to be configured in
466 // the active state, If the DHCP4 D.O.R.A started by IP4 auto
467 // configuration and has not been completed, the Dhcp4 state machine
468 // will not be in the right state for the iSCSI to start a new round D.O.R.A.
469 // So, we need to switch it's policy to static.
471 Status
= gBS
->HandleProtocol (Controller
, &gEfiIp4Config2ProtocolGuid
, (VOID
**) &Ip4Config2
);
472 if (!EFI_ERROR (Status
)) {
473 Status
= IScsiSetIp4Policy (Ip4Config2
);
474 if (EFI_ERROR (Status
)) {
480 // Create a DHCP4 child instance and get the protocol.
482 Status
= NetLibCreateServiceChild (
485 &gEfiDhcp4ServiceBindingProtocolGuid
,
488 if (EFI_ERROR (Status
)) {
492 Status
= gBS
->OpenProtocol (
494 &gEfiDhcp4ProtocolGuid
,
498 EFI_OPEN_PROTOCOL_BY_DRIVER
500 if (EFI_ERROR (Status
)) {
504 NvData
= &ConfigData
->SessionConfigData
;
506 ParaList
= AllocatePool (sizeof (EFI_DHCP4_PACKET_OPTION
) + 3);
507 if (ParaList
== NULL
) {
508 Status
= EFI_OUT_OF_RESOURCES
;
513 // Ask the server to reply with Netmask, Router, DNS, and RootPath options.
515 ParaList
->OpCode
= DHCP4_TAG_PARA_LIST
;
516 ParaList
->Length
= (UINT8
) (NvData
->TargetInfoFromDhcp
? 4 : 3);
517 ParaList
->Data
[0] = DHCP4_TAG_NETMASK
;
518 ParaList
->Data
[1] = DHCP4_TAG_ROUTER
;
519 ParaList
->Data
[2] = DHCP4_TAG_DNS_SERVER
;
520 ParaList
->Data
[3] = DHCP4_TAG_ROOTPATH
;
522 ZeroMem (&Dhcp4ConfigData
, sizeof (EFI_DHCP4_CONFIG_DATA
));
523 Dhcp4ConfigData
.OptionCount
= 1;
524 Dhcp4ConfigData
.OptionList
= &ParaList
;
526 if (NvData
->TargetInfoFromDhcp
) {
528 // Use callback to select an offer that contains target information.
530 Dhcp4ConfigData
.Dhcp4Callback
= IScsiDhcpSelectOffer
;
531 Dhcp4ConfigData
.CallbackContext
= ConfigData
;
534 Status
= Dhcp4
->Configure (Dhcp4
, &Dhcp4ConfigData
);
535 if (EFI_ERROR (Status
)) {
539 Status
= Dhcp4
->Start (Dhcp4
, NULL
);
540 if (EFI_ERROR (Status
)) {
544 // Parse the ACK to get required information.
546 Status
= IScsiParseDhcpAck (Dhcp4
, ConfigData
);
550 if (ParaList
!= NULL
) {
556 Dhcp4
->Configure (Dhcp4
, NULL
);
560 &gEfiDhcp4ProtocolGuid
,
566 NetLibDestroyServiceChild (
569 &gEfiDhcp4ServiceBindingProtocolGuid
,