2 iSCSI DHCP6 related configuration routines.
4 Copyright (c) 2009 - 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 from
20 Boot File Uniform Resource Locator (URL) Option.
22 @param[in] RootPath The RootPath string.
23 @param[in] Length Length of the RootPath option payload.
24 @param[in, out] ConfigData The iSCSI session configuration data read from
27 @retval EFI_SUCCESS All required information is extracted from the
29 @retval EFI_NOT_FOUND The RootPath is not an iSCSI RootPath.
30 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
31 @retval EFI_INVALID_PARAMETER The RootPath is malformatted.
35 IScsiDhcp6ExtractRootPath (
38 IN OUT ISCSI_ATTEMPT_CONFIG_NVDATA
*ConfigData
42 UINT16 IScsiRootPathIdLen
;
44 ISCSI_ROOT_PATH_FIELD Fields
[RP_FIELD_IDX_MAX
];
45 ISCSI_ROOT_PATH_FIELD
*Field
;
48 ISCSI_SESSION_CONFIG_NVDATA
*ConfigNvData
;
52 ConfigNvData
= &ConfigData
->SessionConfigData
;
53 ConfigNvData
->DnsMode
= FALSE
;
55 // "iscsi:"<servername>":"<protocol>":"<port>":"<LUN>":"<targetname>
57 IScsiRootPathIdLen
= (UINT16
) AsciiStrLen (ISCSI_ROOT_PATH_ID
);
59 if ((Length
<= IScsiRootPathIdLen
) ||
60 (CompareMem (RootPath
, ISCSI_ROOT_PATH_ID
, IScsiRootPathIdLen
) != 0)) {
64 // Skip the iSCSI RootPath ID "iscsi:".
66 RootPath
= RootPath
+ IScsiRootPathIdLen
;
67 Length
= (UINT16
) (Length
- IScsiRootPathIdLen
);
69 TmpStr
= (CHAR8
*) AllocatePool (Length
+ 1);
71 return EFI_OUT_OF_RESOURCES
;
74 CopyMem (TmpStr
, RootPath
, Length
);
75 TmpStr
[Length
] = '\0';
79 ZeroMem (&Fields
[0], sizeof (Fields
));
82 // Extract SERVERNAME field in the Root Path option.
84 if (TmpStr
[Index
] != ISCSI_ROOT_PATH_ADDR_START_DELIMITER
) {
86 // The servername is expressed as domain name.
88 ConfigNvData
->DnsMode
= TRUE
;
93 Fields
[RP_FIELD_IDX_SERVERNAME
].Str
= &TmpStr
[Index
];
95 if (!ConfigNvData
->DnsMode
) {
96 while ((TmpStr
[Index
] != ISCSI_ROOT_PATH_ADDR_END_DELIMITER
)&& (Index
< Length
)) {
103 TmpStr
[Index
] = '\0';
106 while ((TmpStr
[Index
] != ISCSI_ROOT_PATH_FIELD_DELIMITER
) && (Index
< Length
)) {
112 TmpStr
[Index
] = '\0';
116 Fields
[RP_FIELD_IDX_SERVERNAME
].Len
= (UINT8
) AsciiStrLen (Fields
[RP_FIELD_IDX_SERVERNAME
].Str
);
119 // Extract others fields in the Root Path option string.
121 for (FieldIndex
= 1; (FieldIndex
< RP_FIELD_IDX_MAX
) && (Index
< Length
); FieldIndex
++) {
123 if (TmpStr
[Index
] != ISCSI_ROOT_PATH_FIELD_DELIMITER
) {
124 Fields
[FieldIndex
].Str
= &TmpStr
[Index
];
127 while ((TmpStr
[Index
] != ISCSI_ROOT_PATH_FIELD_DELIMITER
) && (Index
< Length
)) {
131 if (TmpStr
[Index
] == ISCSI_ROOT_PATH_FIELD_DELIMITER
) {
132 if (FieldIndex
!= RP_FIELD_IDX_TARGETNAME
) {
133 TmpStr
[Index
] = '\0';
137 if (Fields
[FieldIndex
].Str
!= NULL
) {
138 Fields
[FieldIndex
].Len
= (UINT8
) AsciiStrLen (Fields
[FieldIndex
].Str
);
143 if (FieldIndex
!= RP_FIELD_IDX_MAX
) {
144 Status
= EFI_INVALID_PARAMETER
;
148 if ((Fields
[RP_FIELD_IDX_SERVERNAME
].Str
== NULL
) ||
149 (Fields
[RP_FIELD_IDX_TARGETNAME
].Str
== NULL
) ||
150 (Fields
[RP_FIELD_IDX_PROTOCOL
].Len
> 1)
153 Status
= EFI_INVALID_PARAMETER
;
157 // Get the IP address of the target.
159 Field
= &Fields
[RP_FIELD_IDX_SERVERNAME
];
160 if (ConfigNvData
->IpMode
< IP_MODE_AUTOCONFIG
) {
161 IpMode
= ConfigNvData
->IpMode
;
163 IpMode
= ConfigData
->AutoConfigureMode
;
167 // Server name is expressed as domain name, just save it.
169 if (ConfigNvData
->DnsMode
) {
170 if (Field
->Len
> sizeof (ConfigNvData
->TargetUrl
)) {
171 return EFI_INVALID_PARAMETER
;
173 CopyMem (&ConfigNvData
->TargetUrl
, Field
->Str
, Field
->Len
);
174 ConfigNvData
->TargetUrl
[Field
->Len
+ 1] = '\0';
176 ZeroMem(&ConfigNvData
->TargetUrl
, sizeof (ConfigNvData
->TargetUrl
));
177 Status
= IScsiAsciiStrToIp (Field
->Str
, IpMode
, &Ip
);
178 CopyMem (&ConfigNvData
->TargetIp
, &Ip
, sizeof (EFI_IP_ADDRESS
));
180 if (EFI_ERROR (Status
)) {
186 // Check the protocol type.
188 Field
= &Fields
[RP_FIELD_IDX_PROTOCOL
];
189 if ((Field
->Str
!= NULL
) && ((*(Field
->Str
) - '0') != EFI_IP_PROTO_TCP
)) {
190 Status
= EFI_INVALID_PARAMETER
;
194 // Get the port of the iSCSI target.
196 Field
= &Fields
[RP_FIELD_IDX_PORT
];
197 if (Field
->Str
!= NULL
) {
198 ConfigNvData
->TargetPort
= (UINT16
) AsciiStrDecimalToUintn (Field
->Str
);
200 ConfigNvData
->TargetPort
= ISCSI_WELL_KNOWN_PORT
;
205 Field
= &Fields
[RP_FIELD_IDX_LUN
];
206 if (Field
->Str
!= NULL
) {
207 Status
= IScsiAsciiStrToLun (Field
->Str
, ConfigNvData
->BootLun
);
208 if (EFI_ERROR (Status
)) {
212 ZeroMem (ConfigNvData
->BootLun
, sizeof (ConfigNvData
->BootLun
));
215 // Get the target iSCSI Name.
217 Field
= &Fields
[RP_FIELD_IDX_TARGETNAME
];
219 if (AsciiStrLen (Field
->Str
) > ISCSI_NAME_MAX_SIZE
- 1) {
220 Status
= EFI_INVALID_PARAMETER
;
224 // Validate the iSCSI name.
226 Status
= IScsiNormalizeName (Field
->Str
, AsciiStrLen (Field
->Str
));
227 if (EFI_ERROR (Status
)) {
231 AsciiStrCpyS (ConfigNvData
->TargetName
, ISCSI_NAME_MAX_SIZE
, Field
->Str
);
241 EFI_DHCP6_INFO_CALLBACK is provided by the consumer of the EFI DHCPv6 Protocol
242 instance to intercept events that occurs in the DHCPv6 Information Request
245 @param[in] This Pointer to the EFI_DHCP6_PROTOCOL instance that
246 is used to configure this callback function.
247 @param[in] Context Pointer to the context that is initialized in
248 the EFI_DHCP6_PROTOCOL.InfoRequest().
249 @param[in] Packet Pointer to Reply packet that has been received.
250 The EFI DHCPv6 Protocol instance is responsible
251 for freeing the buffer.
253 @retval EFI_SUCCESS Tell the EFI DHCPv6 Protocol instance to finish
254 Information Request exchange process.
255 @retval EFI_NOT_READY Tell the EFI DHCPv6 Protocol instance to continue
256 Information Request exchange process.
257 @retval EFI_ABORTED Tell the EFI DHCPv6 Protocol instance to abort
258 the Information Request exchange process.
259 @retval EFI_UNSUPPORTED Tell the EFI DHCPv6 Protocol instance to finish
260 the Information Request exchange process because some
261 request information are not received.
266 IScsiDhcp6ParseReply (
267 IN EFI_DHCP6_PROTOCOL
*This
,
269 IN EFI_DHCP6_PACKET
*Packet
275 EFI_DHCP6_PACKET_OPTION
*BootFileOpt
;
276 EFI_DHCP6_PACKET_OPTION
**OptionList
;
277 ISCSI_ATTEMPT_CONFIG_NVDATA
*ConfigData
;
283 Status
= This
->Parse (This
, Packet
, &OptionCount
, NULL
);
284 if (Status
!= EFI_BUFFER_TOO_SMALL
) {
285 return EFI_NOT_READY
;
288 OptionList
= AllocateZeroPool (OptionCount
* sizeof (EFI_DHCP6_PACKET_OPTION
*));
289 if (OptionList
== NULL
) {
290 return EFI_NOT_READY
;
293 Status
= This
->Parse (This
, Packet
, &OptionCount
, OptionList
);
294 if (EFI_ERROR (Status
)) {
295 Status
= EFI_NOT_READY
;
299 ConfigData
= (ISCSI_ATTEMPT_CONFIG_NVDATA
*) Context
;
301 for (Index
= 0; Index
< OptionCount
; Index
++) {
302 OptionList
[Index
]->OpCode
= NTOHS (OptionList
[Index
]->OpCode
);
303 OptionList
[Index
]->OpLen
= NTOHS (OptionList
[Index
]->OpLen
);
306 // Get DNS server addresses from this reply packet.
308 if (OptionList
[Index
]->OpCode
== DHCP6_OPT_DNS_SERVERS
) {
310 if (((OptionList
[Index
]->OpLen
& 0xf) != 0) || (OptionList
[Index
]->OpLen
== 0)) {
311 Status
= EFI_UNSUPPORTED
;
315 // Primary DNS server address.
317 CopyMem (&ConfigData
->PrimaryDns
, &OptionList
[Index
]->Data
[0], sizeof (EFI_IPv6_ADDRESS
));
319 if (OptionList
[Index
]->OpLen
> 16) {
321 // Secondary DNS server address
323 CopyMem (&ConfigData
->SecondaryDns
, &OptionList
[Index
]->Data
[16], sizeof (EFI_IPv6_ADDRESS
));
326 } else if (OptionList
[Index
]->OpCode
== DHCP6_OPT_BOOT_FILE_URL
) {
328 // The server sends this option to inform the client about an URL to a boot file.
330 BootFileOpt
= OptionList
[Index
];
331 } else if (OptionList
[Index
]->OpCode
== DHCP6_OPT_BOOT_FILE_PARAM
) {
333 // The server sends this option to inform the client about DHCP6 server address.
335 if (OptionList
[Index
]->OpLen
< 18) {
336 Status
= EFI_UNSUPPORTED
;
340 // Check param-len 1, should be 16 bytes.
342 CopyMem (&ParaLen
, &OptionList
[Index
]->Data
[0], sizeof (UINT16
));
343 if (NTOHS (ParaLen
) != 16) {
344 Status
= EFI_UNSUPPORTED
;
348 CopyMem (&ConfigData
->DhcpServer
, &OptionList
[Index
]->Data
[2], sizeof (EFI_IPv6_ADDRESS
));
352 if (BootFileOpt
== NULL
) {
353 Status
= EFI_UNSUPPORTED
;
358 // Get iSCSI root path from Boot File Uniform Resource Locator (URL) Option
360 Status
= IScsiDhcp6ExtractRootPath (
361 (CHAR8
*) BootFileOpt
->Data
,
368 FreePool (OptionList
);
374 Parse the DHCP ACK to get the address configuration and DNS information.
376 @param[in] Image The handle of the driver image.
377 @param[in] Controller The handle of the controller;
378 @param[in, out] ConfigData The attempt configuration data.
380 @retval EFI_SUCCESS The DNS information is got from the DHCP ACK.
381 @retval EFI_NO_MAPPING DHCP failed to acquire address and other
383 @retval EFI_INVALID_PARAMETER The DHCP ACK's DNS option is malformatted.
384 @retval EFI_DEVICE_ERROR Some unexpected error occurred.
385 @retval EFI_OUT_OF_RESOURCES There is no sufficient resource to finish the
387 @retval EFI_NO_MEDIA There was a media error.
393 IN EFI_HANDLE Controller
,
394 IN OUT ISCSI_ATTEMPT_CONFIG_NVDATA
*ConfigData
397 EFI_HANDLE Dhcp6Handle
;
398 EFI_DHCP6_PROTOCOL
*Dhcp6
;
400 EFI_STATUS TimerStatus
;
401 EFI_DHCP6_PACKET_OPTION
*Oro
;
402 EFI_DHCP6_RETRANSMISSION InfoReqReXmit
;
404 BOOLEAN MediaPresent
;
407 // Check media status before doing DHCP.
410 NetLibDetectMedia (Controller
, &MediaPresent
);
416 // iSCSI will only request target info from DHCPv6 server.
418 if (!ConfigData
->SessionConfigData
.TargetInfoFromDhcp
) {
428 // Create a DHCP6 child instance and get the protocol.
430 Status
= NetLibCreateServiceChild (
433 &gEfiDhcp6ServiceBindingProtocolGuid
,
436 if (EFI_ERROR (Status
)) {
440 Status
= gBS
->OpenProtocol (
442 &gEfiDhcp6ProtocolGuid
,
446 EFI_OPEN_PROTOCOL_BY_DRIVER
448 if (EFI_ERROR (Status
)) {
452 Oro
= AllocateZeroPool (sizeof (EFI_DHCP6_PACKET_OPTION
) + 5);
454 Status
= EFI_OUT_OF_RESOURCES
;
459 // Ask the server to reply with DNS and Boot File URL options by info request.
460 // All members in EFI_DHCP6_PACKET_OPTION are in network order.
462 Oro
->OpCode
= HTONS (DHCP6_OPT_ORO
);
463 Oro
->OpLen
= HTONS (2 * 3);
464 Oro
->Data
[1] = DHCP6_OPT_DNS_SERVERS
;
465 Oro
->Data
[3] = DHCP6_OPT_BOOT_FILE_URL
;
466 Oro
->Data
[5] = DHCP6_OPT_BOOT_FILE_PARAM
;
468 InfoReqReXmit
.Irt
= 4;
469 InfoReqReXmit
.Mrc
= 1;
470 InfoReqReXmit
.Mrt
= 10;
471 InfoReqReXmit
.Mrd
= 30;
473 Status
= Dhcp6
->InfoRequest (
481 IScsiDhcp6ParseReply
,
484 if (Status
== EFI_NO_MAPPING
) {
485 Status
= gBS
->CreateEvent (EVT_TIMER
, TPL_CALLBACK
, NULL
, NULL
, &Timer
);
486 if (EFI_ERROR (Status
)) {
490 Status
= gBS
->SetTimer (
493 ISCSI_GET_MAPPING_TIMEOUT
496 if (EFI_ERROR (Status
)) {
502 TimerStatus
= gBS
->CheckEvent (Timer
);
504 if (!EFI_ERROR (TimerStatus
)) {
505 Status
= Dhcp6
->InfoRequest (
513 IScsiDhcp6ParseReply
,
518 } while (TimerStatus
== EFI_NOT_READY
);
529 gBS
->CloseEvent (Timer
);
535 &gEfiDhcp6ProtocolGuid
,
541 NetLibDestroyServiceChild (
544 &gEfiDhcp6ServiceBindingProtocolGuid
,