]> git.proxmox.com Git - mirror_edk2.git/blob - NetworkPkg/UefiPxeBcDxe/PxeBcDhcp6.c
NetworkPkg: Add error handling logic when using AllocateZeroPool
[mirror_edk2.git] / NetworkPkg / UefiPxeBcDxe / PxeBcDhcp6.c
1 /** @file
2 Functions implementation related with DHCPv6 for UefiPxeBc Driver.
3
4 (C) Copyright 2014 Hewlett-Packard Development Company, L.P.<BR>
5 Copyright (c) 2009 - 2016, Intel Corporation. All rights reserved.<BR>
6
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php.
11
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14
15 **/
16
17 #include "PxeBcImpl.h"
18
19 //
20 // Well-known multi-cast address defined in section-24.1 of rfc-3315
21 //
22 // ALL_DHCP_Relay_Agents_and_Servers address: FF02::1:2
23 //
24 EFI_IPv6_ADDRESS mAllDhcpRelayAndServersAddress = {{0xFF, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 2}};
25
26 /**
27 Parse out a DHCPv6 option by OptTag, and find the position in buffer.
28
29 @param[in] Buffer The pointer to the option buffer.
30 @param[in] Length Length of the option buffer.
31 @param[in] OptTag The required option tag.
32
33 @retval NULL Failed to parse the required option.
34 @retval Others The postion of the required option in buffer.
35
36 **/
37 EFI_DHCP6_PACKET_OPTION *
38 PxeBcParseDhcp6Options (
39 IN UINT8 *Buffer,
40 IN UINT32 Length,
41 IN UINT16 OptTag
42 )
43 {
44 EFI_DHCP6_PACKET_OPTION *Option;
45 UINT32 Offset;
46
47 Option = (EFI_DHCP6_PACKET_OPTION *) Buffer;
48 Offset = 0;
49
50 //
51 // OpLen and OpCode here are both stored in network order.
52 //
53 while (Offset < Length) {
54
55 if (NTOHS (Option->OpCode) == OptTag) {
56
57 return Option;
58 }
59
60 Offset += (NTOHS(Option->OpLen) + 4);
61 Option = (EFI_DHCP6_PACKET_OPTION *) (Buffer + Offset);
62 }
63
64 return NULL;
65 }
66
67
68 /**
69 Build the options buffer for the DHCPv6 request packet.
70
71 @param[in] Private The pointer to PxeBc private data.
72 @param[out] OptList The pointer to the option pointer array.
73 @param[in] Buffer The pointer to the buffer to contain the option list.
74
75 @return Index The count of the built-in options.
76
77 **/
78 UINT32
79 PxeBcBuildDhcp6Options (
80 IN PXEBC_PRIVATE_DATA *Private,
81 OUT EFI_DHCP6_PACKET_OPTION **OptList,
82 IN UINT8 *Buffer
83 )
84 {
85 PXEBC_DHCP6_OPTION_ENTRY OptEnt;
86 UINT32 Index;
87 UINT16 Value;
88
89 Index = 0;
90 OptList[0] = (EFI_DHCP6_PACKET_OPTION *) Buffer;
91
92 //
93 // Append client option request option
94 //
95 OptList[Index]->OpCode = HTONS (DHCP6_OPT_ORO);
96 OptList[Index]->OpLen = HTONS (6);
97 OptEnt.Oro = (PXEBC_DHCP6_OPTION_ORO *) OptList[Index]->Data;
98 OptEnt.Oro->OpCode[0] = HTONS(DHCP6_OPT_BOOT_FILE_URL);
99 OptEnt.Oro->OpCode[1] = HTONS(DHCP6_OPT_BOOT_FILE_PARAM);
100 OptEnt.Oro->OpCode[2] = HTONS(DHCP6_OPT_DNS_SERVERS);
101 Index++;
102 OptList[Index] = GET_NEXT_DHCP6_OPTION (OptList[Index - 1]);
103
104 //
105 // Append client network device interface option
106 //
107 OptList[Index]->OpCode = HTONS (DHCP6_OPT_UNDI);
108 OptList[Index]->OpLen = HTONS ((UINT16)3);
109 OptEnt.Undi = (PXEBC_DHCP6_OPTION_UNDI *) OptList[Index]->Data;
110
111 if (Private->Nii != NULL) {
112 OptEnt.Undi->Type = Private->Nii->Type;
113 OptEnt.Undi->MajorVer = Private->Nii->MajorVer;
114 OptEnt.Undi->MinorVer = Private->Nii->MinorVer;
115 } else {
116 OptEnt.Undi->Type = DEFAULT_UNDI_TYPE;
117 OptEnt.Undi->MajorVer = DEFAULT_UNDI_MAJOR;
118 OptEnt.Undi->MinorVer = DEFAULT_UNDI_MINOR;
119 }
120
121 Index++;
122 OptList[Index] = GET_NEXT_DHCP6_OPTION (OptList[Index - 1]);
123
124 //
125 // Append client system architecture option
126 //
127 OptList[Index]->OpCode = HTONS (DHCP6_OPT_ARCH);
128 OptList[Index]->OpLen = HTONS ((UINT16) sizeof (PXEBC_DHCP6_OPTION_ARCH));
129 OptEnt.Arch = (PXEBC_DHCP6_OPTION_ARCH *) OptList[Index]->Data;
130 Value = HTONS (EFI_PXE_CLIENT_SYSTEM_ARCHITECTURE);
131 CopyMem (&OptEnt.Arch->Type, &Value, sizeof (UINT16));
132 Index++;
133 OptList[Index] = GET_NEXT_DHCP6_OPTION (OptList[Index - 1]);
134
135 //
136 // Append vendor class option to store the PXE class identifier.
137 //
138 OptList[Index]->OpCode = HTONS (DHCP6_OPT_VENDOR_CLASS);
139 OptList[Index]->OpLen = HTONS ((UINT16) sizeof (PXEBC_DHCP6_OPTION_VENDOR_CLASS));
140 OptEnt.VendorClass = (PXEBC_DHCP6_OPTION_VENDOR_CLASS *) OptList[Index]->Data;
141 OptEnt.VendorClass->Vendor = HTONL (PXEBC_DHCP6_ENTERPRISE_NUM);
142 OptEnt.VendorClass->ClassLen = HTONS ((UINT16) sizeof (PXEBC_CLASS_ID));
143 CopyMem (
144 &OptEnt.VendorClass->ClassId,
145 DEFAULT_CLASS_ID_DATA,
146 sizeof (PXEBC_CLASS_ID)
147 );
148 PxeBcUintnToAscDecWithFormat (
149 EFI_PXE_CLIENT_SYSTEM_ARCHITECTURE,
150 OptEnt.VendorClass->ClassId.ArchitectureType,
151 sizeof (OptEnt.VendorClass->ClassId.ArchitectureType)
152 );
153
154 if (Private->Nii != NULL) {
155 CopyMem (
156 OptEnt.VendorClass->ClassId.InterfaceName,
157 Private->Nii->StringId,
158 sizeof (OptEnt.VendorClass->ClassId.InterfaceName)
159 );
160 PxeBcUintnToAscDecWithFormat (
161 Private->Nii->MajorVer,
162 OptEnt.VendorClass->ClassId.UndiMajor,
163 sizeof (OptEnt.VendorClass->ClassId.UndiMajor)
164 );
165 PxeBcUintnToAscDecWithFormat (
166 Private->Nii->MinorVer,
167 OptEnt.VendorClass->ClassId.UndiMinor,
168 sizeof (OptEnt.VendorClass->ClassId.UndiMinor)
169 );
170 }
171
172 Index++;
173
174 return Index;
175 }
176
177
178 /**
179 Cache the DHCPv6 packet.
180
181 @param[in] Dst The pointer to the cache buffer for DHCPv6 packet.
182 @param[in] Src The pointer to the DHCPv6 packet to be cached.
183
184 **/
185 VOID
186 PxeBcCacheDhcp6Packet (
187 IN EFI_DHCP6_PACKET *Dst,
188 IN EFI_DHCP6_PACKET *Src
189 )
190 {
191 ASSERT (Dst->Size >= Src->Length);
192
193 CopyMem (&Dst->Dhcp6, &Src->Dhcp6, Src->Length);
194 Dst->Length = Src->Length;
195 }
196
197
198 /**
199 Free all the nodes in the list for boot file.
200
201 @param[in] Head The pointer to the head of list.
202
203 **/
204 VOID
205 PxeBcFreeBootFileOption (
206 IN LIST_ENTRY *Head
207 )
208 {
209 LIST_ENTRY *Entry;
210 LIST_ENTRY *NextEntry;
211 PXEBC_DHCP6_OPTION_NODE *Node;
212
213 NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, Head) {
214 Node = NET_LIST_USER_STRUCT (Entry, PXEBC_DHCP6_OPTION_NODE, Link);
215 RemoveEntryList (Entry);
216 FreePool (Node);
217 }
218 }
219
220 /**
221 Retrieve the boot server address using the EFI_DNS6_PROTOCOL.
222
223 @param[in] Private Pointer to PxeBc private data.
224 @param[in] HostName Pointer to buffer containing hostname.
225 @param[out] IpAddress On output, pointer to buffer containing IPv6 address.
226
227 @retval EFI_SUCCESS Operation succeeded.
228 @retval EFI_OUT_OF_RESOURCES Failed to allocate needed resources.
229 @retval EFI_DEVICE_ERROR An unexpected network error occurred.
230 @retval Others Other errors as indicated.
231
232 **/
233 EFI_STATUS
234 PxeBcDns6 (
235 IN PXEBC_PRIVATE_DATA *Private,
236 IN CHAR16 *HostName,
237 OUT EFI_IPv6_ADDRESS *IpAddress
238 )
239 {
240 EFI_STATUS Status;
241 EFI_DNS6_PROTOCOL *Dns6;
242 EFI_DNS6_CONFIG_DATA Dns6ConfigData;
243 EFI_DNS6_COMPLETION_TOKEN Token;
244 EFI_HANDLE Dns6Handle;
245 EFI_IPv6_ADDRESS *DnsServerList;
246 BOOLEAN IsDone;
247
248 Dns6 = NULL;
249 Dns6Handle = NULL;
250 DnsServerList = Private->DnsServer;
251 ZeroMem (&Token, sizeof (EFI_DNS6_COMPLETION_TOKEN));
252
253 //
254 // Create a DNSv6 child instance and get the protocol.
255 //
256 Status = NetLibCreateServiceChild (
257 Private->Controller,
258 Private->Image,
259 &gEfiDns6ServiceBindingProtocolGuid,
260 &Dns6Handle
261 );
262 if (EFI_ERROR (Status)) {
263 goto Exit;
264 }
265
266 Status = gBS->OpenProtocol (
267 Dns6Handle,
268 &gEfiDns6ProtocolGuid,
269 (VOID **) &Dns6,
270 Private->Image,
271 Private->Controller,
272 EFI_OPEN_PROTOCOL_BY_DRIVER
273 );
274 if (EFI_ERROR (Status)) {
275 goto Exit;
276 }
277
278 //
279 // Configure DNS6 instance for the DNS server address and protocol.
280 //
281 ZeroMem (&Dns6ConfigData, sizeof (EFI_DNS6_CONFIG_DATA));
282 Dns6ConfigData.DnsServerCount = 1;
283 Dns6ConfigData.DnsServerList = DnsServerList;
284 Dns6ConfigData.EnableDnsCache = TRUE;
285 Dns6ConfigData.Protocol = EFI_IP_PROTO_UDP;
286 IP6_COPY_ADDRESS (&Dns6ConfigData.StationIp, &Private->TmpStationIp.v6);
287 Status = Dns6->Configure (
288 Dns6,
289 &Dns6ConfigData
290 );
291 if (EFI_ERROR (Status)) {
292 goto Exit;
293 }
294
295 Token.Status = EFI_NOT_READY;
296 IsDone = FALSE;
297 //
298 // Create event to set the IsDone flag when name resolution is finished.
299 //
300 Status = gBS->CreateEvent (
301 EVT_NOTIFY_SIGNAL,
302 TPL_NOTIFY,
303 PxeBcCommonNotify,
304 &IsDone,
305 &Token.Event
306 );
307 if (EFI_ERROR (Status)) {
308 goto Exit;
309 }
310
311 //
312 // Start asynchronous name resolution.
313 //
314 Status = Dns6->HostNameToIp (Dns6, HostName, &Token);
315 if (EFI_ERROR (Status)) {
316 goto Exit;
317 }
318
319 while (!IsDone) {
320 Dns6->Poll (Dns6);
321 }
322
323 //
324 // Name resolution is done, check result.
325 //
326 Status = Token.Status;
327 if (!EFI_ERROR (Status)) {
328 if (Token.RspData.H2AData == NULL) {
329 Status = EFI_DEVICE_ERROR;
330 goto Exit;
331 }
332 if (Token.RspData.H2AData->IpCount == 0 || Token.RspData.H2AData->IpList == NULL) {
333 Status = EFI_DEVICE_ERROR;
334 goto Exit;
335 }
336 //
337 // We just return the first IPv6 address from DNS protocol.
338 //
339 IP6_COPY_ADDRESS (IpAddress, Token.RspData.H2AData->IpList);
340 Status = EFI_SUCCESS;
341 }
342
343 Exit:
344 FreePool (HostName);
345
346 if (Token.Event != NULL) {
347 gBS->CloseEvent (Token.Event);
348 }
349 if (Token.RspData.H2AData != NULL) {
350 if (Token.RspData.H2AData->IpList != NULL) {
351 FreePool (Token.RspData.H2AData->IpList);
352 }
353 FreePool (Token.RspData.H2AData);
354 }
355
356 if (Dns6 != NULL) {
357 Dns6->Configure (Dns6, NULL);
358
359 gBS->CloseProtocol (
360 Dns6Handle,
361 &gEfiDns6ProtocolGuid,
362 Private->Image,
363 Private->Controller
364 );
365 }
366
367 if (Dns6Handle != NULL) {
368 NetLibDestroyServiceChild (
369 Private->Controller,
370 Private->Image,
371 &gEfiDns6ServiceBindingProtocolGuid,
372 Dns6Handle
373 );
374 }
375
376 if (DnsServerList != NULL) {
377 FreePool (DnsServerList);
378 }
379
380 return Status;
381 }
382
383 /**
384 Parse the Boot File URL option.
385
386 @param[in] Private Pointer to PxeBc private data.
387 @param[out] FileName The pointer to the boot file name.
388 @param[in, out] SrvAddr The pointer to the boot server address.
389 @param[in] BootFile The pointer to the boot file URL option data.
390 @param[in] Length The length of the boot file URL option data.
391
392 @retval EFI_ABORTED User cancel operation.
393 @retval EFI_SUCCESS Selected the boot menu successfully.
394 @retval EFI_NOT_READY Read the input key from the keybroad has not finish.
395
396 **/
397 EFI_STATUS
398 PxeBcExtractBootFileUrl (
399 IN PXEBC_PRIVATE_DATA *Private,
400 OUT UINT8 **FileName,
401 IN OUT EFI_IPv6_ADDRESS *SrvAddr,
402 IN CHAR8 *BootFile,
403 IN UINT16 Length
404 )
405 {
406 UINT16 PrefixLen;
407 CHAR8 *BootFileNamePtr;
408 CHAR8 *BootFileName;
409 UINT16 BootFileNameLen;
410 CHAR8 *TmpStr;
411 CHAR8 TmpChar;
412 CHAR8 *ServerAddressOption;
413 CHAR8 *ServerAddress;
414 CHAR8 *ModeStr;
415 CHAR16 *HostName;
416 BOOLEAN IpExpressedUrl;
417 UINTN Len;
418 EFI_STATUS Status;
419
420 IpExpressedUrl = TRUE;
421 //
422 // The format of the Boot File URL option is:
423 //
424 // 0 1 2 3
425 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
426 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
427 // | OPT_BOOTFILE_URL | option-len |
428 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
429 // | |
430 // . bootfile-url (variable length) .
431 // | |
432 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
433 //
434
435 //
436 // Based upon RFC 5970 and UEFI 2.6, bootfile-url format can be
437 // tftp://[SERVER_ADDRESS]/BOOTFILE_NAME or tftp://domain_name/BOOTFILE_NAME
438 // As an example where the BOOTFILE_NAME is the EFI loader and
439 // SERVER_ADDRESS is the ASCII encoding of an IPV6 address.
440 //
441 PrefixLen = (UINT16) AsciiStrLen (PXEBC_DHCP6_BOOT_FILE_URL_PREFIX);
442
443 if (Length <= PrefixLen ||
444 CompareMem (BootFile, PXEBC_DHCP6_BOOT_FILE_URL_PREFIX, PrefixLen) != 0) {
445 return EFI_NOT_FOUND;
446 }
447
448 BootFile = BootFile + PrefixLen;
449 Length = (UINT16) (Length - PrefixLen);
450
451 TmpStr = (CHAR8 *) AllocateZeroPool (Length + 1);
452 if (TmpStr == NULL) {
453 return EFI_OUT_OF_RESOURCES;
454 }
455
456 CopyMem (TmpStr, BootFile, Length);
457 TmpStr[Length] = '\0';
458
459 //
460 // Get the part of SERVER_ADDRESS string.
461 //
462 ServerAddressOption = TmpStr;
463 if (*ServerAddressOption == PXEBC_ADDR_START_DELIMITER) {
464 ServerAddressOption ++;
465 ServerAddress = ServerAddressOption;
466 while (*ServerAddress != '\0' && *ServerAddress != PXEBC_ADDR_END_DELIMITER) {
467 ServerAddress++;
468 }
469
470 if (*ServerAddress != PXEBC_ADDR_END_DELIMITER) {
471 FreePool (TmpStr);
472 return EFI_INVALID_PARAMETER;
473 }
474
475 *ServerAddress = '\0';
476
477 //
478 // Convert the string of server address to Ipv6 address format and store it.
479 //
480 Status = NetLibAsciiStrToIp6 (ServerAddressOption, SrvAddr);
481 if (EFI_ERROR (Status)) {
482 FreePool (TmpStr);
483 return Status;
484 }
485
486 } else {
487 IpExpressedUrl = FALSE;
488 ServerAddress = ServerAddressOption;
489 while (*ServerAddress != '\0' && *ServerAddress != PXEBC_TFTP_URL_SEPARATOR) {
490 ServerAddress++;
491 }
492
493 if (*ServerAddress != PXEBC_TFTP_URL_SEPARATOR) {
494 FreePool (TmpStr);
495 return EFI_INVALID_PARAMETER;
496 }
497 *ServerAddress = '\0';
498
499 Len = AsciiStrSize (ServerAddressOption);
500 HostName = AllocateZeroPool (Len * sizeof (CHAR16));
501 if (HostName == NULL) {
502 FreePool (TmpStr);
503 return EFI_OUT_OF_RESOURCES;
504 }
505 AsciiStrToUnicodeStrS (
506 ServerAddressOption,
507 HostName,
508 Len
509 );
510
511 //
512 // Perform DNS resolution.
513 //
514 Status = PxeBcDns6 (Private,HostName, SrvAddr);
515 if (EFI_ERROR (Status)) {
516 FreePool (TmpStr);
517 return Status;
518 }
519 }
520
521 //
522 // Get the part of BOOTFILE_NAME string.
523 //
524 BootFileNamePtr = (CHAR8*)((UINTN)ServerAddress + 1);
525 if (IpExpressedUrl) {
526 if (*BootFileNamePtr != PXEBC_TFTP_URL_SEPARATOR) {
527 FreePool (TmpStr);
528 return EFI_INVALID_PARAMETER;
529 }
530 ++BootFileNamePtr;
531 }
532
533 BootFileNameLen = (UINT16)(Length - (UINT16) ((UINTN)BootFileNamePtr - (UINTN)TmpStr) + 1);
534 if (BootFileNameLen != 0 || FileName != NULL) {
535 //
536 // Remove trailing mode=octet if present and ignore. All other modes are
537 // invalid for netboot6, so reject them.
538 //
539 ModeStr = AsciiStrStr (BootFileNamePtr, ";mode=octet");
540 if (ModeStr != NULL && *(ModeStr + AsciiStrLen (";mode=octet")) == '\0') {
541 *ModeStr = '\0';
542 } else if (AsciiStrStr (BootFileNamePtr, ";mode=") != NULL) {
543 return EFI_INVALID_PARAMETER;
544 }
545
546 //
547 // Extract boot file name from URL.
548 //
549 BootFileName = (CHAR8 *) AllocateZeroPool (BootFileNameLen);
550 if (BootFileName == NULL) {
551 FreePool (TmpStr);
552 return EFI_OUT_OF_RESOURCES;
553 }
554 *FileName = (UINT8*) BootFileName;
555
556 //
557 // Decode percent-encoding in boot file name.
558 //
559 while (*BootFileNamePtr != '\0') {
560 if (*BootFileNamePtr == '%') {
561 TmpChar = *(BootFileNamePtr+ 3);
562 *(BootFileNamePtr+ 3) = '\0';
563 *BootFileName = (UINT8) AsciiStrHexToUintn ((CHAR8*)(BootFileNamePtr + 1));
564 BootFileName++;
565 *(BootFileNamePtr+ 3) = TmpChar;
566 BootFileNamePtr += 3;
567 } else {
568 *BootFileName = *BootFileNamePtr;
569 BootFileName++;
570 BootFileNamePtr++;
571 }
572 }
573 *BootFileName = '\0';
574 }
575
576 FreePool (TmpStr);
577
578 return EFI_SUCCESS;
579 }
580
581
582 /**
583 Parse the Boot File Parameter option.
584
585 @param[in] BootFilePara The pointer to boot file parameter option data.
586 @param[out] BootFileSize The pointer to the parsed boot file size.
587
588 @retval EFI_SUCCESS Successfully obtained the boot file size from parameter option.
589 @retval EFI_NOT_FOUND Failed to extract the boot file size from parameter option.
590
591 **/
592 EFI_STATUS
593 PxeBcExtractBootFileParam (
594 IN CHAR8 *BootFilePara,
595 OUT UINT16 *BootFileSize
596 )
597 {
598 UINT16 Length;
599 UINT8 Index;
600 UINT8 Digit;
601 UINT32 Size;
602
603 CopyMem (&Length, BootFilePara, sizeof (UINT16));
604 Length = NTOHS (Length);
605
606 //
607 // The BootFile Size should be 1~5 byte ASCII strings
608 //
609 if (Length < 1 || Length > 5) {
610 return EFI_NOT_FOUND;
611 }
612
613 //
614 // Extract the value of BootFile Size.
615 //
616 BootFilePara = BootFilePara + sizeof (UINT16);
617 Size = 0;
618 for (Index = 0; Index < Length; Index++) {
619 if (EFI_ERROR (PxeBcUniHexToUint8 (&Digit, *(BootFilePara + Index)))) {
620 return EFI_NOT_FOUND;
621 }
622
623 Size = (Size + Digit) * 10;
624 }
625
626 Size = Size / 10;
627 if (Size > PXEBC_DHCP6_MAX_BOOT_FILE_SIZE) {
628 return EFI_NOT_FOUND;
629 }
630
631 *BootFileSize = (UINT16) Size;
632 return EFI_SUCCESS;
633 }
634
635
636 /**
637 Parse the cached DHCPv6 packet, including all the options.
638
639 @param[in] Cache6 The pointer to a cached DHCPv6 packet.
640
641 @retval EFI_SUCCESS Parsed the DHCPv6 packet successfully.
642 @retval EFI_DEVICE_ERROR Failed to parse and invalid the packet.
643
644 **/
645 EFI_STATUS
646 PxeBcParseDhcp6Packet (
647 IN PXEBC_DHCP6_PACKET_CACHE *Cache6
648 )
649 {
650 EFI_DHCP6_PACKET *Offer;
651 EFI_DHCP6_PACKET_OPTION **Options;
652 EFI_DHCP6_PACKET_OPTION *Option;
653 PXEBC_OFFER_TYPE OfferType;
654 BOOLEAN IsProxyOffer;
655 BOOLEAN IsPxeOffer;
656 UINT32 Offset;
657 UINT32 Length;
658 UINT32 EnterpriseNum;
659
660 IsProxyOffer = TRUE;
661 IsPxeOffer = FALSE;
662 Offer = &Cache6->Packet.Offer;
663 Options = Cache6->OptList;
664
665 ZeroMem (Cache6->OptList, sizeof (Cache6->OptList));
666
667 Option = (EFI_DHCP6_PACKET_OPTION *) (Offer->Dhcp6.Option);
668 Offset = 0;
669 Length = GET_DHCP6_OPTION_SIZE (Offer);
670
671 //
672 // OpLen and OpCode here are both stored in network order, since they are from original packet.
673 //
674 while (Offset < Length) {
675
676 if (NTOHS (Option->OpCode) == DHCP6_OPT_IA_NA) {
677 Options[PXEBC_DHCP6_IDX_IA_NA] = Option;
678 } else if (NTOHS (Option->OpCode) == DHCP6_OPT_BOOT_FILE_URL) {
679 //
680 // The server sends this option to inform the client about an URL to a boot file.
681 //
682 Options[PXEBC_DHCP6_IDX_BOOT_FILE_URL] = Option;
683 } else if (NTOHS (Option->OpCode) == DHCP6_OPT_BOOT_FILE_PARAM) {
684 Options[PXEBC_DHCP6_IDX_BOOT_FILE_PARAM] = Option;
685 } else if (NTOHS (Option->OpCode) == DHCP6_OPT_VENDOR_CLASS) {
686 Options[PXEBC_DHCP6_IDX_VENDOR_CLASS] = Option;
687 } else if (NTOHS (Option->OpCode) == DHCP6_OPT_DNS_SERVERS) {
688 Options[PXEBC_DHCP6_IDX_DNS_SERVER] = Option;
689 }
690
691 Offset += (NTOHS (Option->OpLen) + 4);
692 Option = (EFI_DHCP6_PACKET_OPTION *) (Offer->Dhcp6.Option + Offset);
693 }
694
695 //
696 // The offer with assigned client address is NOT a proxy offer.
697 // An ia_na option, embeded with valid ia_addr option and a status_code of success.
698 //
699 Option = Options[PXEBC_DHCP6_IDX_IA_NA];
700 if (Option != NULL) {
701 Option = PxeBcParseDhcp6Options (
702 Option->Data + 12,
703 NTOHS (Option->OpLen),
704 DHCP6_OPT_STATUS_CODE
705 );
706 if ((Option != NULL && Option->Data[0] == 0) || (Option == NULL)) {
707 IsProxyOffer = FALSE;
708 }
709 }
710
711 //
712 // The offer with "PXEClient" is a pxe offer.
713 //
714 Option = Options[PXEBC_DHCP6_IDX_VENDOR_CLASS];
715 EnterpriseNum = HTONL(PXEBC_DHCP6_ENTERPRISE_NUM);
716
717 if (Option != NULL &&
718 NTOHS(Option->OpLen) >= 13 &&
719 CompareMem (Option->Data, &EnterpriseNum, sizeof (UINT32)) == 0 &&
720 CompareMem (&Option->Data[6], DEFAULT_CLASS_ID_DATA, 9) == 0) {
721 IsPxeOffer = TRUE;
722 }
723
724 //
725 // Determine offer type of the dhcp6 packet.
726 //
727 if (IsPxeOffer) {
728 //
729 // It's a binl offer only with PXEClient.
730 //
731 OfferType = IsProxyOffer ? PxeOfferTypeProxyBinl : PxeOfferTypeDhcpBinl;
732 } else {
733 //
734 // It's a dhcp only offer, which is a pure dhcp6 offer packet.
735 //
736 OfferType = PxeOfferTypeDhcpOnly;
737 }
738
739 Cache6->OfferType = OfferType;
740
741 return EFI_SUCCESS;
742 }
743
744
745 /**
746 Cache the DHCPv6 ack packet, and parse it on demand.
747
748 @param[in] Private The pointer to PxeBc private data.
749 @param[in] Ack The pointer to the DHCPv6 ack packet.
750 @param[in] Verified If TRUE, parse the ACK packet and store info into mode data.
751
752 **/
753 VOID
754 PxeBcCopyDhcp6Ack (
755 IN PXEBC_PRIVATE_DATA *Private,
756 IN EFI_DHCP6_PACKET *Ack,
757 IN BOOLEAN Verified
758 )
759 {
760 EFI_PXE_BASE_CODE_MODE *Mode;
761
762 Mode = Private->PxeBc.Mode;
763
764 PxeBcCacheDhcp6Packet (&Private->DhcpAck.Dhcp6.Packet.Ack, Ack);
765
766 if (Verified) {
767 //
768 // Parse the ack packet and store it into mode data if needed.
769 //
770 PxeBcParseDhcp6Packet (&Private->DhcpAck.Dhcp6);
771 CopyMem (&Mode->DhcpAck.Dhcpv6, &Ack->Dhcp6, Ack->Length);
772 Mode->DhcpAckReceived = TRUE;
773 }
774 }
775
776
777 /**
778 Cache the DHCPv6 proxy offer packet according to the received order.
779
780 @param[in] Private The pointer to PxeBc private data.
781 @param[in] OfferIndex The received order of offer packets.
782
783 **/
784 VOID
785 PxeBcCopyDhcp6Proxy (
786 IN PXEBC_PRIVATE_DATA *Private,
787 IN UINT32 OfferIndex
788 )
789 {
790 EFI_PXE_BASE_CODE_MODE *Mode;
791 EFI_DHCP6_PACKET *Offer;
792
793 ASSERT (OfferIndex < Private->OfferNum);
794 ASSERT (OfferIndex < PXEBC_OFFER_MAX_NUM);
795
796 Mode = Private->PxeBc.Mode;
797 Offer = &Private->OfferBuffer[OfferIndex].Dhcp6.Packet.Offer;
798
799 //
800 // Cache the proxy offer packet and parse it.
801 //
802 PxeBcCacheDhcp6Packet (&Private->ProxyOffer.Dhcp6.Packet.Offer, Offer);
803 PxeBcParseDhcp6Packet (&Private->ProxyOffer.Dhcp6);
804
805 //
806 // Store this packet into mode data.
807 //
808 CopyMem (&Mode->ProxyOffer.Dhcpv6, &Offer->Dhcp6, Offer->Length);
809 Mode->ProxyOfferReceived = TRUE;
810 }
811
812 /**
813 Seek the address of the first byte of the option header.
814
815 @param[in] Buf The pointer to the buffer.
816 @param[in] SeekLen The length to seek.
817 @param[in] OptType The option type.
818
819 @retval NULL If it failed to seek the option.
820 @retval others The position to the option.
821
822 **/
823 UINT8 *
824 PxeBcDhcp6SeekOption (
825 IN UINT8 *Buf,
826 IN UINT32 SeekLen,
827 IN UINT16 OptType
828 )
829 {
830 UINT8 *Cursor;
831 UINT8 *Option;
832 UINT16 DataLen;
833 UINT16 OpCode;
834
835 Option = NULL;
836 Cursor = Buf;
837
838 while (Cursor < Buf + SeekLen) {
839 OpCode = ReadUnaligned16 ((UINT16 *) Cursor);
840 if (OpCode == HTONS (OptType)) {
841 Option = Cursor;
842 break;
843 }
844 DataLen = NTOHS (ReadUnaligned16 ((UINT16 *) (Cursor + 2)));
845 Cursor += (DataLen + 4);
846 }
847
848 return Option;
849 }
850
851
852 /**
853 Build and send out the request packet for the bootfile, and parse the reply.
854
855 @param[in] Private The pointer to PxeBc private data.
856 @param[in] Index PxeBc option boot item type.
857
858 @retval EFI_SUCCESS Successfully discovered the boot file.
859 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources.
860 @retval EFI_NOT_FOUND Can't get the PXE reply packet.
861 @retval Others Failed to discover the boot file.
862
863 **/
864 EFI_STATUS
865 PxeBcRequestBootService (
866 IN PXEBC_PRIVATE_DATA *Private,
867 IN UINT32 Index
868 )
869 {
870 EFI_PXE_BASE_CODE_UDP_PORT SrcPort;
871 EFI_PXE_BASE_CODE_UDP_PORT DestPort;
872 EFI_PXE_BASE_CODE_PROTOCOL *PxeBc;
873 EFI_PXE_BASE_CODE_DHCPV6_PACKET *Discover;
874 UINTN DiscoverLen;
875 EFI_DHCP6_PACKET *Request;
876 UINTN RequestLen;
877 EFI_DHCP6_PACKET *Reply;
878 UINT8 *RequestOpt;
879 UINT8 *DiscoverOpt;
880 UINTN ReadSize;
881 UINT16 OpFlags;
882 UINT16 OpCode;
883 UINT16 OpLen;
884 EFI_STATUS Status;
885 EFI_DHCP6_PACKET *ProxyOffer;
886 UINT8 *Option;
887
888 PxeBc = &Private->PxeBc;
889 Request = Private->Dhcp6Request;
890 ProxyOffer = &Private->OfferBuffer[Index].Dhcp6.Packet.Offer;
891 SrcPort = PXEBC_BS_DISCOVER_PORT;
892 DestPort = PXEBC_BS_DISCOVER_PORT;
893 OpFlags = 0;
894
895 if (Request == NULL) {
896 return EFI_DEVICE_ERROR;
897 }
898
899 Discover = AllocateZeroPool (sizeof (EFI_PXE_BASE_CODE_DHCPV6_PACKET));
900 if (Discover == NULL) {
901 return EFI_OUT_OF_RESOURCES;
902 }
903
904 //
905 // Build the request packet by the cached request packet before.
906 //
907 Discover->TransactionId = ProxyOffer->Dhcp6.Header.TransactionId;
908 Discover->MessageType = Request->Dhcp6.Header.MessageType;
909 RequestOpt = Request->Dhcp6.Option;
910 DiscoverOpt = Discover->DhcpOptions;
911 DiscoverLen = sizeof (EFI_DHCP6_HEADER);
912 RequestLen = DiscoverLen;
913
914 //
915 // Find Server ID Option from ProxyOffer.
916 //
917 Option = PxeBcDhcp6SeekOption (
918 ProxyOffer->Dhcp6.Option,
919 ProxyOffer->Length - 4,
920 DHCP6_OPT_SERVER_ID
921 );
922 if (Option == NULL) {
923 return EFI_NOT_FOUND;
924 }
925
926 //
927 // Add Server ID Option.
928 //
929 OpLen = NTOHS (((EFI_DHCP6_PACKET_OPTION *) Option)->OpLen);
930 CopyMem (DiscoverOpt, Option, OpLen + 4);
931 DiscoverOpt += (OpLen + 4);
932 DiscoverLen += (OpLen + 4);
933
934 while (RequestLen < Request->Length) {
935 OpCode = NTOHS (((EFI_DHCP6_PACKET_OPTION *) RequestOpt)->OpCode);
936 OpLen = NTOHS (((EFI_DHCP6_PACKET_OPTION *) RequestOpt)->OpLen);
937 if (OpCode != EFI_DHCP6_IA_TYPE_NA &&
938 OpCode != EFI_DHCP6_IA_TYPE_TA &&
939 OpCode != DHCP6_OPT_SERVER_ID
940 ) {
941 //
942 // Copy all the options except IA option and Server ID
943 //
944 CopyMem (DiscoverOpt, RequestOpt, OpLen + 4);
945 DiscoverOpt += (OpLen + 4);
946 DiscoverLen += (OpLen + 4);
947 }
948 RequestOpt += (OpLen + 4);
949 RequestLen += (OpLen + 4);
950 }
951
952 //
953 // Update Elapsed option in the package
954 //
955 Option = PxeBcDhcp6SeekOption (
956 Discover->DhcpOptions,
957 (UINT32)(RequestLen - 4),
958 DHCP6_OPT_ELAPSED_TIME
959 );
960 if (Option != NULL) {
961 CalcElapsedTime (Private);
962 WriteUnaligned16 ((UINT16*)(Option + 4), HTONS((UINT16) Private->ElapsedTime));
963 }
964
965 Status = PxeBc->UdpWrite (
966 PxeBc,
967 OpFlags,
968 &Private->ServerIp,
969 &DestPort,
970 NULL,
971 &Private->StationIp,
972 &SrcPort,
973 NULL,
974 NULL,
975 &DiscoverLen,
976 (VOID *) Discover
977 );
978
979 if (EFI_ERROR (Status)) {
980 return Status;
981 }
982
983 //
984 // Cache the right PXE reply packet here, set valid flag later.
985 // Especially for PXE discover packet, store it into mode data here.
986 //
987 Reply = &Private->ProxyOffer.Dhcp6.Packet.Offer;
988 ReadSize = (UINTN) Reply->Size;
989
990 //
991 // Start Udp6Read instance
992 //
993 Status = Private->Udp6Read->Configure (Private->Udp6Read, &Private->Udp6CfgData);
994 if (EFI_ERROR (Status)) {
995 return Status;
996 }
997
998 Status = PxeBc->UdpRead (
999 PxeBc,
1000 EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_IP | EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_DEST_IP,
1001 NULL,
1002 &SrcPort,
1003 &Private->ServerIp,
1004 &DestPort,
1005 NULL,
1006 NULL,
1007 &ReadSize,
1008 (VOID *) &Reply->Dhcp6
1009 );
1010 //
1011 // Stop Udp6Read instance
1012 //
1013 Private->Udp6Read->Configure (Private->Udp6Read, NULL);
1014
1015 if (EFI_ERROR (Status)) {
1016 return Status;
1017 }
1018
1019 //
1020 // Update length
1021 //
1022 Reply->Length = (UINT32) ReadSize;
1023
1024 return EFI_SUCCESS;
1025 }
1026
1027
1028 /**
1029 Retry to request bootfile name by the BINL offer.
1030
1031 @param[in] Private The pointer to PxeBc private data.
1032 @param[in] Index The received order of offer packets.
1033
1034 @retval EFI_SUCCESS Successfully retried a request for the bootfile name.
1035 @retval EFI_DEVICE_ERROR Failed to retry the bootfile name.
1036
1037 **/
1038 EFI_STATUS
1039 PxeBcRetryDhcp6Binl (
1040 IN PXEBC_PRIVATE_DATA *Private,
1041 IN UINT32 Index
1042 )
1043 {
1044 EFI_PXE_BASE_CODE_MODE *Mode;
1045 PXEBC_DHCP6_PACKET_CACHE *Offer;
1046 PXEBC_DHCP6_PACKET_CACHE *Cache6;
1047 EFI_STATUS Status;
1048
1049 ASSERT (Index < PXEBC_OFFER_MAX_NUM);
1050 ASSERT (Private->OfferBuffer[Index].Dhcp6.OfferType == PxeOfferTypeDhcpBinl ||
1051 Private->OfferBuffer[Index].Dhcp6.OfferType == PxeOfferTypeProxyBinl);
1052
1053 Mode = Private->PxeBc.Mode;
1054 Private->IsDoDiscover = FALSE;
1055 Offer = &Private->OfferBuffer[Index].Dhcp6;
1056 if (Offer->OfferType == PxeOfferTypeDhcpBinl) {
1057 //
1058 // There is no BootFileUrl option in dhcp6 offer, so use servers multi-cast address instead.
1059 //
1060 CopyMem (
1061 &Private->ServerIp.v6,
1062 &mAllDhcpRelayAndServersAddress,
1063 sizeof (EFI_IPv6_ADDRESS)
1064 );
1065 } else {
1066 ASSERT (Offer->OptList[PXEBC_DHCP6_IDX_BOOT_FILE_URL] != NULL);
1067 //
1068 // Parse out the next server address from the last offer, and store it
1069 //
1070 Status = PxeBcExtractBootFileUrl (
1071 Private,
1072 &Private->BootFileName,
1073 &Private->ServerIp.v6,
1074 (CHAR8 *) (Offer->OptList[PXEBC_DHCP6_IDX_BOOT_FILE_URL]->Data),
1075 NTOHS (Offer->OptList[PXEBC_DHCP6_IDX_BOOT_FILE_URL]->OpLen)
1076 );
1077 if (EFI_ERROR (Status)) {
1078 return Status;
1079 }
1080 }
1081
1082 //
1083 // Retry Dhcp6Binl again for the bootfile, and the reply cached into Private->ProxyOffer.
1084 //
1085 Status = PxeBcRequestBootService (Private, Index);
1086
1087 if (EFI_ERROR (Status)) {
1088 return Status;
1089 }
1090
1091 Cache6 = &Private->ProxyOffer.Dhcp6;
1092 Status = PxeBcParseDhcp6Packet (Cache6);
1093 if (EFI_ERROR (Status)) {
1094 return Status;
1095 }
1096
1097 if (Cache6->OfferType != PxeOfferTypeProxyPxe10 &&
1098 Cache6->OfferType != PxeOfferTypeProxyWfm11a &&
1099 Cache6->OptList[PXEBC_DHCP6_IDX_BOOT_FILE_URL] == NULL) {
1100 //
1101 // This BINL ack doesn't have discovery option set or multicast option set
1102 // or bootfile name specified.
1103 //
1104 return EFI_DEVICE_ERROR;
1105 }
1106
1107 Mode->ProxyOfferReceived = TRUE;
1108 CopyMem (
1109 &Mode->ProxyOffer.Dhcpv6,
1110 &Cache6->Packet.Offer.Dhcp6,
1111 Cache6->Packet.Offer.Length
1112 );
1113
1114 return EFI_SUCCESS;
1115 }
1116
1117
1118 /**
1119 Cache all the received DHCPv6 offers, and set OfferIndex and OfferCount.
1120
1121 @param[in] Private The pointer to PXEBC_PRIVATE_DATA.
1122 @param[in] RcvdOffer The pointer to the received offer packet.
1123
1124 **/
1125 VOID
1126 PxeBcCacheDhcp6Offer (
1127 IN PXEBC_PRIVATE_DATA *Private,
1128 IN EFI_DHCP6_PACKET *RcvdOffer
1129 )
1130 {
1131 PXEBC_DHCP6_PACKET_CACHE *Cache6;
1132 EFI_DHCP6_PACKET *Offer;
1133 PXEBC_OFFER_TYPE OfferType;
1134
1135 Cache6 = &Private->OfferBuffer[Private->OfferNum].Dhcp6;
1136 Offer = &Cache6->Packet.Offer;
1137
1138 //
1139 // Cache the content of DHCPv6 packet firstly.
1140 //
1141 PxeBcCacheDhcp6Packet (Offer, RcvdOffer);
1142
1143 //
1144 // Validate the DHCPv6 packet, and parse the options and offer type.
1145 //
1146 if (EFI_ERROR (PxeBcParseDhcp6Packet (Cache6))) {
1147 return ;
1148 }
1149
1150 //
1151 // Determine whether cache the current offer by type, and record OfferIndex and OfferCount.
1152 //
1153 OfferType = Cache6->OfferType;
1154 ASSERT (OfferType < PxeOfferTypeMax);
1155 ASSERT (Private->OfferCount[OfferType] < PXEBC_OFFER_MAX_NUM);
1156
1157 if (IS_PROXY_OFFER (OfferType)) {
1158 //
1159 // It's a proxy offer without yiaddr, including PXE10, WFM11a or BINL offer.
1160 //
1161 Private->IsProxyRecved = TRUE;
1162
1163 if (OfferType == PxeOfferTypeProxyBinl) {
1164 //
1165 // Cache all proxy BINL offers.
1166 //
1167 Private->OfferIndex[OfferType][Private->OfferCount[OfferType]] = Private->OfferNum;
1168 Private->OfferCount[OfferType]++;
1169 } else if (Private->OfferCount[OfferType] > 0) {
1170 //
1171 // Only cache the first PXE10/WFM11a offer, and discard the others.
1172 //
1173 Private->OfferIndex[OfferType][0] = Private->OfferNum;
1174 Private->OfferCount[OfferType] = 1;
1175 } else {
1176 return;
1177 }
1178 } else {
1179 //
1180 // It's a DHCPv6 offer with yiaddr, and cache them all.
1181 //
1182 Private->OfferIndex[OfferType][Private->OfferCount[OfferType]] = Private->OfferNum;
1183 Private->OfferCount[OfferType]++;
1184 }
1185
1186 Private->OfferNum++;
1187 }
1188
1189
1190 /**
1191 Select an DHCPv6 offer, and record SelectIndex and SelectProxyType.
1192
1193 @param[in] Private The pointer to PXEBC_PRIVATE_DATA.
1194
1195 **/
1196 VOID
1197 PxeBcSelectDhcp6Offer (
1198 IN PXEBC_PRIVATE_DATA *Private
1199 )
1200 {
1201 UINT32 Index;
1202 UINT32 OfferIndex;
1203 PXEBC_OFFER_TYPE OfferType;
1204
1205 Private->SelectIndex = 0;
1206
1207 if (Private->IsOfferSorted) {
1208 //
1209 // Select offer by default policy.
1210 //
1211 if (Private->OfferCount[PxeOfferTypeDhcpPxe10] > 0) {
1212 //
1213 // 1. DhcpPxe10 offer
1214 //
1215 Private->SelectIndex = Private->OfferIndex[PxeOfferTypeDhcpPxe10][0] + 1;
1216
1217 } else if (Private->OfferCount[PxeOfferTypeDhcpWfm11a] > 0) {
1218 //
1219 // 2. DhcpWfm11a offer
1220 //
1221 Private->SelectIndex = Private->OfferIndex[PxeOfferTypeDhcpWfm11a][0] + 1;
1222
1223 } else if (Private->OfferCount[PxeOfferTypeDhcpOnly] > 0 &&
1224 Private->OfferCount[PxeOfferTypeProxyPxe10] > 0) {
1225 //
1226 // 3. DhcpOnly offer and ProxyPxe10 offer.
1227 //
1228 Private->SelectIndex = Private->OfferIndex[PxeOfferTypeDhcpOnly][0] + 1;
1229 Private->SelectProxyType = PxeOfferTypeProxyPxe10;
1230
1231 } else if (Private->OfferCount[PxeOfferTypeDhcpOnly] > 0 &&
1232 Private->OfferCount[PxeOfferTypeProxyWfm11a] > 0) {
1233 //
1234 // 4. DhcpOnly offer and ProxyWfm11a offer.
1235 //
1236 Private->SelectIndex = Private->OfferIndex[PxeOfferTypeDhcpOnly][0] + 1;
1237 Private->SelectProxyType = PxeOfferTypeProxyWfm11a;
1238
1239 } else if (Private->OfferCount[PxeOfferTypeDhcpBinl] > 0) {
1240 //
1241 // 5. DhcpBinl offer.
1242 //
1243 Private->SelectIndex = Private->OfferIndex[PxeOfferTypeDhcpBinl][0] + 1;
1244
1245 } else if (Private->OfferCount[PxeOfferTypeDhcpOnly] > 0 &&
1246 Private->OfferCount[PxeOfferTypeProxyBinl] > 0) {
1247 //
1248 // 6. DhcpOnly offer and ProxyBinl offer.
1249 //
1250 Private->SelectIndex = Private->OfferIndex[PxeOfferTypeDhcpOnly][0] + 1;
1251 Private->SelectProxyType = PxeOfferTypeProxyBinl;
1252
1253 } else {
1254 //
1255 // 7. DhcpOnly offer with bootfilename.
1256 //
1257 for (Index = 0; Index < Private->OfferCount[PxeOfferTypeDhcpOnly]; Index++) {
1258 OfferIndex = Private->OfferIndex[PxeOfferTypeDhcpOnly][Index];
1259 if (Private->OfferBuffer[OfferIndex].Dhcp6.OptList[PXEBC_DHCP6_IDX_BOOT_FILE_URL] != NULL) {
1260 Private->SelectIndex = OfferIndex + 1;
1261 break;
1262 }
1263 }
1264 }
1265 } else {
1266 //
1267 // Select offer by received order.
1268 //
1269 for (Index = 0; Index < Private->OfferNum; Index++) {
1270
1271 OfferType = Private->OfferBuffer[Index].Dhcp6.OfferType;
1272
1273 if (IS_PROXY_OFFER (OfferType)) {
1274 //
1275 // Skip proxy offers
1276 //
1277 continue;
1278 }
1279
1280 if (!Private->IsProxyRecved &&
1281 OfferType == PxeOfferTypeDhcpOnly &&
1282 Private->OfferBuffer[Index].Dhcp6.OptList[PXEBC_DHCP6_IDX_BOOT_FILE_URL] == NULL) {
1283 //
1284 // Skip if DhcpOnly offer without any other proxy offers or bootfilename.
1285 //
1286 continue;
1287 }
1288
1289 Private->SelectIndex = Index + 1;
1290 break;
1291 }
1292 }
1293 }
1294
1295
1296 /**
1297 Handle the DHCPv6 offer packet.
1298
1299 @param[in] Private The pointer to PXEBC_PRIVATE_DATA.
1300
1301 @retval EFI_SUCCESS Handled the DHCPv6 offer packet successfully.
1302 @retval EFI_NO_RESPONSE No response to the following request packet.
1303 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources.
1304
1305 **/
1306 EFI_STATUS
1307 PxeBcHandleDhcp6Offer (
1308 IN PXEBC_PRIVATE_DATA *Private
1309 )
1310 {
1311 PXEBC_DHCP6_PACKET_CACHE *Cache6;
1312 EFI_STATUS Status;
1313 PXEBC_OFFER_TYPE OfferType;
1314 UINT32 ProxyIndex;
1315 UINT32 SelectIndex;
1316 UINT32 Index;
1317
1318 ASSERT (Private->SelectIndex > 0);
1319 SelectIndex = (UINT32) (Private->SelectIndex - 1);
1320 ASSERT (SelectIndex < PXEBC_OFFER_MAX_NUM);
1321 Cache6 = &Private->OfferBuffer[SelectIndex].Dhcp6;
1322 Status = EFI_SUCCESS;
1323
1324 //
1325 // First try to cache DNS server address if DHCP6 offer provides.
1326 //
1327 if (Cache6->OptList[PXEBC_DHCP6_IDX_DNS_SERVER] != NULL) {
1328 Private->DnsServer = AllocateZeroPool (NTOHS (Cache6->OptList[PXEBC_DHCP6_IDX_DNS_SERVER]->OpLen));
1329 if (Private->DnsServer == NULL) {
1330 return EFI_OUT_OF_RESOURCES;
1331 }
1332 CopyMem (Private->DnsServer, Cache6->OptList[PXEBC_DHCP6_IDX_DNS_SERVER]->Data, sizeof (EFI_IPv6_ADDRESS));
1333 }
1334
1335 if (Cache6->OfferType == PxeOfferTypeDhcpBinl) {
1336 //
1337 // DhcpBinl offer is selected, so need try to request bootfilename by this offer.
1338 //
1339 if (EFI_ERROR (PxeBcRetryDhcp6Binl (Private, SelectIndex))) {
1340 Status = EFI_NO_RESPONSE;
1341 }
1342 } else if (Cache6->OfferType == PxeOfferTypeDhcpOnly) {
1343
1344 if (Private->IsProxyRecved) {
1345 //
1346 // DhcpOnly offer is selected, so need try to request bootfilename.
1347 //
1348 ProxyIndex = 0;
1349 if (Private->IsOfferSorted) {
1350 //
1351 // The proxy offer should be determined if select by default policy.
1352 // IsOfferSorted means all offers are labeled by OfferIndex.
1353 //
1354 ASSERT (Private->OfferCount[Private->SelectProxyType] > 0);
1355
1356 if (Private->SelectProxyType == PxeOfferTypeProxyBinl) {
1357 //
1358 // Try all the cached ProxyBinl offer one by one to request bootfilename.
1359 //
1360 for (Index = 0; Index < Private->OfferCount[Private->SelectProxyType]; Index++) {
1361
1362 ProxyIndex = Private->OfferIndex[Private->SelectProxyType][Index];
1363 if (!EFI_ERROR (PxeBcRetryDhcp6Binl (Private, ProxyIndex))) {
1364 break;
1365 }
1366 }
1367 if (Index == Private->OfferCount[Private->SelectProxyType]) {
1368 Status = EFI_NO_RESPONSE;
1369 }
1370 } else {
1371 //
1372 // For other proxy offers (pxe10 or wfm11a), only one is buffered.
1373 //
1374 ProxyIndex = Private->OfferIndex[Private->SelectProxyType][0];
1375 }
1376 } else {
1377 //
1378 // The proxy offer should not be determined if select by received order.
1379 //
1380 Status = EFI_NO_RESPONSE;
1381
1382 for (Index = 0; Index < Private->OfferNum; Index++) {
1383
1384 OfferType = Private->OfferBuffer[Index].Dhcp6.OfferType;
1385
1386 if (!IS_PROXY_OFFER (OfferType)) {
1387 //
1388 // Skip non proxy dhcp offers.
1389 //
1390 continue;
1391 }
1392
1393 if (OfferType == PxeOfferTypeProxyBinl) {
1394 //
1395 // Try all the cached ProxyBinl offer one by one to request bootfilename.
1396 //
1397 if (EFI_ERROR (PxeBcRetryDhcp6Binl (Private, Index))) {
1398 continue;
1399 }
1400 }
1401
1402 Private->SelectProxyType = OfferType;
1403 ProxyIndex = Index;
1404 Status = EFI_SUCCESS;
1405 break;
1406 }
1407 }
1408
1409 if (!EFI_ERROR (Status) && Private->SelectProxyType != PxeOfferTypeProxyBinl) {
1410 //
1411 // Success to try to request by a ProxyPxe10 or ProxyWfm11a offer, copy and parse it.
1412 //
1413 PxeBcCopyDhcp6Proxy (Private, ProxyIndex);
1414 }
1415 } else {
1416 //
1417 // Othewise, the bootfilename must be included in DhcpOnly offer.
1418 //
1419 ASSERT (Cache6->OptList[PXEBC_DHCP6_IDX_BOOT_FILE_URL] != NULL);
1420 }
1421 }
1422
1423 if (!EFI_ERROR (Status)) {
1424 //
1425 // All PXE boot information is ready by now.
1426 //
1427 PxeBcCopyDhcp6Ack (Private, &Private->DhcpAck.Dhcp6.Packet.Ack, TRUE);
1428 Private->PxeBc.Mode->DhcpDiscoverValid = TRUE;
1429 }
1430
1431 return Status;
1432 }
1433
1434
1435 /**
1436 Unregister the address by Ip6Config protocol.
1437
1438 @param[in] Private The pointer to PXEBC_PRIVATE_DATA.
1439
1440 **/
1441 VOID
1442 PxeBcUnregisterIp6Address (
1443 IN PXEBC_PRIVATE_DATA *Private
1444 )
1445 {
1446 if (Private->Ip6Policy != PXEBC_IP6_POLICY_MAX) {
1447 //
1448 // PXE driver change the policy of IP6 driver, it's a chance to recover.
1449 // Keep the point and there is no enough requirements to do recovery.
1450 //
1451 }
1452 }
1453
1454 /**
1455 Check whether IP driver could route the message which will be sent to ServerIp address.
1456
1457 This function will check the IP6 route table every 1 seconds until specified timeout is expired, if a valid
1458 route is found in IP6 route table, the address will be filed in GatewayAddr and return.
1459
1460 @param[in] Private The pointer to PXEBC_PRIVATE_DATA.
1461 @param[in] TimeOutInSecond Timeout value in seconds.
1462 @param[out] GatewayAddr Pointer to store the gateway IP address.
1463
1464 @retval EFI_SUCCESS Found a valid gateway address successfully.
1465 @retval EFI_TIMEOUT The operation is time out.
1466 @retval Other Unexpect error happened.
1467
1468 **/
1469 EFI_STATUS
1470 PxeBcCheckRouteTable (
1471 IN PXEBC_PRIVATE_DATA *Private,
1472 IN UINTN TimeOutInSecond,
1473 OUT EFI_IPv6_ADDRESS *GatewayAddr
1474 )
1475 {
1476 EFI_STATUS Status;
1477 EFI_IP6_PROTOCOL *Ip6;
1478 EFI_IP6_MODE_DATA Ip6ModeData;
1479 UINTN Index;
1480 EFI_EVENT TimeOutEvt;
1481 UINTN RetryCount;
1482 BOOLEAN GatewayIsFound;
1483
1484 ASSERT (GatewayAddr != NULL);
1485 ASSERT (Private != NULL);
1486
1487 Ip6 = Private->Ip6;
1488 GatewayIsFound = FALSE;
1489 RetryCount = 0;
1490 TimeOutEvt = NULL;
1491 ZeroMem (GatewayAddr, sizeof (EFI_IPv6_ADDRESS));
1492
1493 while (TRUE) {
1494 Status = Ip6->GetModeData (Ip6, &Ip6ModeData, NULL, NULL);
1495 if (EFI_ERROR (Status)) {
1496 goto ON_EXIT;
1497 }
1498
1499 //
1500 // Find out the gateway address which can route the message which send to ServerIp.
1501 //
1502 for (Index = 0; Index < Ip6ModeData.RouteCount; Index++) {
1503 if (NetIp6IsNetEqual (&Private->ServerIp.v6, &Ip6ModeData.RouteTable[Index].Destination, Ip6ModeData.RouteTable[Index].PrefixLength)) {
1504 IP6_COPY_ADDRESS (GatewayAddr, &Ip6ModeData.RouteTable[Index].Gateway);
1505 GatewayIsFound = TRUE;
1506 break;
1507 }
1508 }
1509
1510 if (Ip6ModeData.AddressList != NULL) {
1511 FreePool (Ip6ModeData.AddressList);
1512 }
1513 if (Ip6ModeData.GroupTable != NULL) {
1514 FreePool (Ip6ModeData.GroupTable);
1515 }
1516 if (Ip6ModeData.RouteTable != NULL) {
1517 FreePool (Ip6ModeData.RouteTable);
1518 }
1519 if (Ip6ModeData.NeighborCache != NULL) {
1520 FreePool (Ip6ModeData.NeighborCache);
1521 }
1522 if (Ip6ModeData.PrefixTable != NULL) {
1523 FreePool (Ip6ModeData.PrefixTable);
1524 }
1525 if (Ip6ModeData.IcmpTypeList != NULL) {
1526 FreePool (Ip6ModeData.IcmpTypeList);
1527 }
1528
1529 if (GatewayIsFound || RetryCount == TimeOutInSecond) {
1530 break;
1531 }
1532
1533 RetryCount++;
1534
1535 //
1536 // Delay 1 second then recheck it again.
1537 //
1538 if (TimeOutEvt == NULL) {
1539 Status = gBS->CreateEvent (
1540 EVT_TIMER,
1541 TPL_CALLBACK,
1542 NULL,
1543 NULL,
1544 &TimeOutEvt
1545 );
1546 if (EFI_ERROR (Status)) {
1547 goto ON_EXIT;
1548 }
1549 }
1550
1551 Status = gBS->SetTimer (TimeOutEvt, TimerRelative, TICKS_PER_SECOND);
1552 if (EFI_ERROR (Status)) {
1553 goto ON_EXIT;
1554 }
1555 while (EFI_ERROR (gBS->CheckEvent (TimeOutEvt))) {
1556 Ip6->Poll (Ip6);
1557 }
1558 }
1559
1560 ON_EXIT:
1561 if (TimeOutEvt != NULL) {
1562 gBS->CloseEvent (TimeOutEvt);
1563 }
1564
1565 if (GatewayIsFound) {
1566 Status = EFI_SUCCESS;
1567 } else if (RetryCount == TimeOutInSecond) {
1568 Status = EFI_TIMEOUT;
1569 }
1570
1571 return Status;
1572 }
1573
1574 /**
1575 Register the ready station address and gateway by Ip6Config protocol.
1576
1577 @param[in] Private The pointer to PXEBC_PRIVATE_DATA.
1578 @param[in] Address The pointer to the ready address.
1579
1580 @retval EFI_SUCCESS Registered the address succesfully.
1581 @retval Others Failed to register the address.
1582
1583 **/
1584 EFI_STATUS
1585 PxeBcRegisterIp6Address (
1586 IN PXEBC_PRIVATE_DATA *Private,
1587 IN EFI_IPv6_ADDRESS *Address
1588 )
1589 {
1590 EFI_IP6_PROTOCOL *Ip6;
1591 EFI_IP6_CONFIG_PROTOCOL *Ip6Cfg;
1592 EFI_IP6_CONFIG_POLICY Policy;
1593 EFI_IP6_CONFIG_MANUAL_ADDRESS CfgAddr;
1594 EFI_IPv6_ADDRESS GatewayAddr;
1595 UINTN DataSize;
1596 EFI_EVENT MappedEvt;
1597 EFI_STATUS Status;
1598 BOOLEAN NoGateway;
1599 EFI_IPv6_ADDRESS *Ip6Addr;
1600 UINTN Index;
1601
1602 Status = EFI_SUCCESS;
1603 MappedEvt = NULL;
1604 Ip6Addr = NULL;
1605 DataSize = sizeof (EFI_IP6_CONFIG_POLICY);
1606 Ip6Cfg = Private->Ip6Cfg;
1607 Ip6 = Private->Ip6;
1608 NoGateway = FALSE;
1609
1610 ZeroMem (&CfgAddr, sizeof (EFI_IP6_CONFIG_MANUAL_ADDRESS));
1611 CopyMem (&CfgAddr.Address, Address, sizeof (EFI_IPv6_ADDRESS));
1612
1613 Status = Ip6->Configure (Ip6, &Private->Ip6CfgData);
1614 if (EFI_ERROR (Status)) {
1615 goto ON_EXIT;
1616 }
1617
1618 //
1619 // Retrieve the gateway address from IP6 route table.
1620 //
1621 Status = PxeBcCheckRouteTable (Private, PXEBC_IP6_ROUTE_TABLE_TIMEOUT, &GatewayAddr);
1622 if (EFI_ERROR (Status)) {
1623 NoGateway = TRUE;
1624 }
1625
1626 //
1627 // There is no channel between IP6 and PXE driver about address setting,
1628 // so it has to set the new address by Ip6ConfigProtocol manually.
1629 //
1630 Policy = Ip6ConfigPolicyManual;
1631 Status = Ip6Cfg->SetData (
1632 Ip6Cfg,
1633 Ip6ConfigDataTypePolicy,
1634 sizeof(EFI_IP6_CONFIG_POLICY),
1635 &Policy
1636 );
1637 if (EFI_ERROR (Status)) {
1638 //
1639 // There is no need to recover later.
1640 //
1641 Private->Ip6Policy = PXEBC_IP6_POLICY_MAX;
1642 goto ON_EXIT;
1643 }
1644
1645 //
1646 // Create a notify event to set address flag when DAD if IP6 driver succeeded.
1647 //
1648 Status = gBS->CreateEvent (
1649 EVT_NOTIFY_SIGNAL,
1650 TPL_NOTIFY,
1651 PxeBcCommonNotify,
1652 &Private->IsAddressOk,
1653 &MappedEvt
1654 );
1655 if (EFI_ERROR (Status)) {
1656 goto ON_EXIT;
1657 }
1658
1659 Private->IsAddressOk = FALSE;
1660 Status = Ip6Cfg->RegisterDataNotify (
1661 Ip6Cfg,
1662 Ip6ConfigDataTypeManualAddress,
1663 MappedEvt
1664 );
1665 if (EFI_ERROR(Status)) {
1666 goto ON_EXIT;
1667 }
1668
1669 Status = Ip6Cfg->SetData (
1670 Ip6Cfg,
1671 Ip6ConfigDataTypeManualAddress,
1672 sizeof(EFI_IP6_CONFIG_MANUAL_ADDRESS),
1673 &CfgAddr
1674 );
1675 if (EFI_ERROR(Status) && Status != EFI_NOT_READY) {
1676 goto ON_EXIT;
1677 } else if (Status == EFI_NOT_READY) {
1678 //
1679 // Poll the network until the asynchronous process is finished.
1680 //
1681 while (!Private->IsAddressOk) {
1682 Ip6->Poll (Ip6);
1683 }
1684 //
1685 // Check whether the IP6 address setting is successed.
1686 //
1687 DataSize = 0;
1688 Status = Ip6Cfg->GetData (
1689 Ip6Cfg,
1690 Ip6ConfigDataTypeManualAddress,
1691 &DataSize,
1692 NULL
1693 );
1694 if (Status != EFI_BUFFER_TOO_SMALL || DataSize == 0) {
1695 Status = EFI_DEVICE_ERROR;
1696 goto ON_EXIT;
1697 }
1698
1699 Ip6Addr = AllocatePool (DataSize);
1700 if (Ip6Addr == NULL) {
1701 return EFI_OUT_OF_RESOURCES;
1702 }
1703 Status = Ip6Cfg->GetData (
1704 Ip6Cfg,
1705 Ip6ConfigDataTypeManualAddress,
1706 &DataSize,
1707 (VOID*) Ip6Addr
1708 );
1709 if (EFI_ERROR (Status)) {
1710 Status = EFI_DEVICE_ERROR;
1711 goto ON_EXIT;
1712 }
1713
1714 for (Index = 0; Index < DataSize / sizeof (EFI_IPv6_ADDRESS); Index++) {
1715 if (CompareMem (Ip6Addr + Index, Address, sizeof (EFI_IPv6_ADDRESS)) == 0) {
1716 break;
1717 }
1718 }
1719 if (Index == DataSize / sizeof (EFI_IPv6_ADDRESS)) {
1720 Status = EFI_ABORTED;
1721 goto ON_EXIT;
1722 }
1723 }
1724
1725 //
1726 // Set the default gateway address back if needed.
1727 //
1728 if (!NoGateway && !NetIp6IsUnspecifiedAddr (&GatewayAddr)) {
1729 Status = Ip6Cfg->SetData (
1730 Ip6Cfg,
1731 Ip6ConfigDataTypeGateway,
1732 sizeof (EFI_IPv6_ADDRESS),
1733 &GatewayAddr
1734 );
1735 if (EFI_ERROR (Status)) {
1736 goto ON_EXIT;
1737 }
1738 }
1739
1740 ON_EXIT:
1741 if (MappedEvt != NULL) {
1742 Ip6Cfg->UnregisterDataNotify (
1743 Ip6Cfg,
1744 Ip6ConfigDataTypeManualAddress,
1745 MappedEvt
1746 );
1747 gBS->CloseEvent (MappedEvt);
1748 }
1749 if (Ip6Addr != NULL) {
1750 FreePool (Ip6Addr);
1751 }
1752 return Status;
1753 }
1754
1755 /**
1756 Set the IP6 policy to Automatic.
1757
1758 @param[in] Private The pointer to PXEBC_PRIVATE_DATA.
1759
1760 @retval EFI_SUCCESS Switch the IP policy succesfully.
1761 @retval Others Unexpect error happened.
1762
1763 **/
1764 EFI_STATUS
1765 PxeBcSetIp6Policy (
1766 IN PXEBC_PRIVATE_DATA *Private
1767 )
1768 {
1769 EFI_IP6_CONFIG_POLICY Policy;
1770 EFI_STATUS Status;
1771 EFI_IP6_CONFIG_PROTOCOL *Ip6Cfg;
1772 UINTN DataSize;
1773
1774 Ip6Cfg = Private->Ip6Cfg;
1775 DataSize = sizeof (EFI_IP6_CONFIG_POLICY);
1776
1777 //
1778 // Get and store the current policy of IP6 driver.
1779 //
1780 Status = Ip6Cfg->GetData (
1781 Ip6Cfg,
1782 Ip6ConfigDataTypePolicy,
1783 &DataSize,
1784 &Private->Ip6Policy
1785 );
1786 if (EFI_ERROR (Status)) {
1787 return Status;
1788 }
1789
1790 if (Private->Ip6Policy == Ip6ConfigPolicyManual) {
1791 Policy = Ip6ConfigPolicyAutomatic;
1792 Status = Ip6Cfg->SetData (
1793 Ip6Cfg,
1794 Ip6ConfigDataTypePolicy,
1795 sizeof(EFI_IP6_CONFIG_POLICY),
1796 &Policy
1797 );
1798 if (EFI_ERROR (Status)) {
1799 //
1800 // There is no need to recover later.
1801 //
1802 Private->Ip6Policy = PXEBC_IP6_POLICY_MAX;
1803 }
1804 }
1805
1806 return Status;
1807 }
1808
1809 /**
1810 This function will register the station IP address and flush IP instance to start using the new IP address.
1811
1812 @param[in] Private The pointer to PXEBC_PRIVATE_DATA.
1813
1814 @retval EFI_SUCCESS The new IP address has been configured successfully.
1815 @retval Others Failed to configure the address.
1816
1817 **/
1818 EFI_STATUS
1819 PxeBcSetIp6Address (
1820 IN PXEBC_PRIVATE_DATA *Private
1821 )
1822 {
1823 EFI_STATUS Status;
1824 EFI_DHCP6_PROTOCOL *Dhcp6;
1825
1826 Dhcp6 = Private->Dhcp6;
1827
1828 CopyMem (&Private->StationIp.v6, &Private->TmpStationIp.v6, sizeof (EFI_IPv6_ADDRESS));
1829 CopyMem (&Private->PxeBc.Mode->StationIp.v6, &Private->StationIp.v6, sizeof (EFI_IPv6_ADDRESS));
1830
1831 Status = PxeBcRegisterIp6Address (Private, &Private->StationIp.v6);
1832 if (EFI_ERROR (Status)) {
1833 Dhcp6->Stop (Dhcp6);
1834 return Status;
1835 }
1836
1837 Status = PxeBcFlushStationIp (Private, &Private->StationIp, NULL);
1838 if (EFI_ERROR (Status)) {
1839 PxeBcUnregisterIp6Address (Private);
1840 Dhcp6->Stop (Dhcp6);
1841 return Status;
1842 }
1843
1844 AsciiPrint ("\n Station IP address is ");
1845 PxeBcShowIp6Addr (&Private->StationIp.v6);
1846
1847 return EFI_SUCCESS;
1848 }
1849
1850 /**
1851 EFI_DHCP6_CALLBACK is provided by the consumer of the EFI DHCPv6 Protocol driver
1852 to intercept events that occurred in the configuration process.
1853
1854 @param[in] This The pointer to the EFI DHCPv6 Protocol.
1855 @param[in] Context The pointer to the context set by EFI_DHCP6_PROTOCOL.Configure().
1856 @param[in] CurrentState The current operational state of the EFI DHCPv Protocol driver.
1857 @param[in] Dhcp6Event The event that occurs in the current state, which usually means a
1858 state transition.
1859 @param[in] Packet The DHCPv6 packet that is going to be sent or was already received.
1860 @param[out] NewPacket The packet that is used to replace the Packet above.
1861
1862 @retval EFI_SUCCESS Told the EFI DHCPv6 Protocol driver to continue the DHCP process.
1863 @retval EFI_NOT_READY Only used in the Dhcp6Selecting state. The EFI DHCPv6 Protocol
1864 driver will continue to wait for more packets.
1865 @retval EFI_ABORTED Told the EFI DHCPv6 Protocol driver to abort the current process.
1866
1867 **/
1868 EFI_STATUS
1869 EFIAPI
1870 PxeBcDhcp6CallBack (
1871 IN EFI_DHCP6_PROTOCOL *This,
1872 IN VOID *Context,
1873 IN EFI_DHCP6_STATE CurrentState,
1874 IN EFI_DHCP6_EVENT Dhcp6Event,
1875 IN EFI_DHCP6_PACKET *Packet,
1876 OUT EFI_DHCP6_PACKET **NewPacket OPTIONAL
1877 )
1878 {
1879 PXEBC_PRIVATE_DATA *Private;
1880 EFI_PXE_BASE_CODE_MODE *Mode;
1881 EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL *Callback;
1882 EFI_DHCP6_PACKET *SelectAd;
1883 EFI_STATUS Status;
1884 BOOLEAN Received;
1885
1886 if ((Dhcp6Event != Dhcp6RcvdAdvertise) &&
1887 (Dhcp6Event != Dhcp6SelectAdvertise) &&
1888 (Dhcp6Event != Dhcp6SendSolicit) &&
1889 (Dhcp6Event != Dhcp6SendRequest) &&
1890 (Dhcp6Event != Dhcp6RcvdReply)) {
1891 return EFI_SUCCESS;
1892 }
1893
1894 ASSERT (Packet != NULL);
1895
1896 Private = (PXEBC_PRIVATE_DATA *) Context;
1897 Mode = Private->PxeBc.Mode;
1898 Callback = Private->PxeBcCallback;
1899
1900 //
1901 // Callback to user when any traffic ocurred if has.
1902 //
1903 if (Dhcp6Event != Dhcp6SelectAdvertise && Callback != NULL) {
1904 Received = (BOOLEAN) (Dhcp6Event == Dhcp6RcvdAdvertise || Dhcp6Event == Dhcp6RcvdReply);
1905 Status = Callback->Callback (
1906 Callback,
1907 Private->Function,
1908 Received,
1909 Packet->Length,
1910 (EFI_PXE_BASE_CODE_PACKET *) &Packet->Dhcp6
1911 );
1912 if (Status != EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE) {
1913 return EFI_ABORTED;
1914 }
1915 }
1916
1917 Status = EFI_SUCCESS;
1918
1919 switch (Dhcp6Event) {
1920
1921 case Dhcp6SendSolicit:
1922 //
1923 // Record the first Solicate msg time
1924 //
1925 if (Private->SolicitTimes == 0) {
1926 CalcElapsedTime (Private);
1927 Private->SolicitTimes++;
1928 }
1929 //
1930 // Cache the dhcp discover packet to mode data directly.
1931 //
1932 CopyMem (&Mode->DhcpDiscover.Dhcpv4, &Packet->Dhcp6, Packet->Length);
1933 break;
1934
1935 case Dhcp6RcvdAdvertise:
1936 Status = EFI_NOT_READY;
1937 if (Private->OfferNum < PXEBC_OFFER_MAX_NUM) {
1938 //
1939 // Cache the dhcp offers to OfferBuffer[] for select later, and record
1940 // the OfferIndex and OfferCount.
1941 //
1942 PxeBcCacheDhcp6Offer (Private, Packet);
1943 }
1944 break;
1945
1946 case Dhcp6SendRequest:
1947 //
1948 // Store the request packet as seed packet for discover.
1949 //
1950 if (Private->Dhcp6Request != NULL) {
1951 FreePool (Private->Dhcp6Request);
1952 }
1953 Private->Dhcp6Request = AllocateZeroPool (Packet->Size);
1954 if (Private->Dhcp6Request != NULL) {
1955 CopyMem (Private->Dhcp6Request, Packet, Packet->Size);
1956 }
1957 break;
1958
1959 case Dhcp6SelectAdvertise:
1960 //
1961 // Select offer by the default policy or by order, and record the SelectIndex
1962 // and SelectProxyType.
1963 //
1964 PxeBcSelectDhcp6Offer (Private);
1965
1966 if (Private->SelectIndex == 0) {
1967 Status = EFI_ABORTED;
1968 } else {
1969 ASSERT (NewPacket != NULL);
1970 SelectAd = &Private->OfferBuffer[Private->SelectIndex - 1].Dhcp6.Packet.Offer;
1971 *NewPacket = AllocateZeroPool (SelectAd->Size);
1972 ASSERT (*NewPacket != NULL);
1973 CopyMem (*NewPacket, SelectAd, SelectAd->Size);
1974 }
1975 break;
1976
1977 case Dhcp6RcvdReply:
1978 //
1979 // Cache the dhcp ack to Private->Dhcp6Ack, but it's not the final ack in mode data
1980 // without verification.
1981 //
1982 ASSERT (Private->SelectIndex != 0);
1983 PxeBcCopyDhcp6Ack (Private, Packet, FALSE);
1984 break;
1985
1986 default:
1987 ASSERT (0);
1988 }
1989
1990 return Status;
1991 }
1992
1993
1994 /**
1995 Build and send out the request packet for the bootfile, and parse the reply.
1996
1997 @param[in] Private The pointer to PxeBc private data.
1998 @param[in] Type PxeBc option boot item type.
1999 @param[in] Layer The pointer to option boot item layer.
2000 @param[in] UseBis Use BIS or not.
2001 @param[in] DestIp The pointer to the server address.
2002
2003 @retval EFI_SUCCESS Successfully discovered the boot file.
2004 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources.
2005 @retval EFI_NOT_FOUND Can't get the PXE reply packet.
2006 @retval Others Failed to discover the boot file.
2007
2008 **/
2009 EFI_STATUS
2010 PxeBcDhcp6Discover (
2011 IN PXEBC_PRIVATE_DATA *Private,
2012 IN UINT16 Type,
2013 IN UINT16 *Layer,
2014 IN BOOLEAN UseBis,
2015 IN EFI_IP_ADDRESS *DestIp
2016 )
2017 {
2018 EFI_PXE_BASE_CODE_UDP_PORT SrcPort;
2019 EFI_PXE_BASE_CODE_UDP_PORT DestPort;
2020 EFI_PXE_BASE_CODE_MODE *Mode;
2021 EFI_PXE_BASE_CODE_PROTOCOL *PxeBc;
2022 EFI_PXE_BASE_CODE_DHCPV6_PACKET *Discover;
2023 UINTN DiscoverLen;
2024 EFI_DHCP6_PACKET *Request;
2025 UINTN RequestLen;
2026 EFI_DHCP6_PACKET *Reply;
2027 UINT8 *RequestOpt;
2028 UINT8 *DiscoverOpt;
2029 UINTN ReadSize;
2030 UINT16 OpCode;
2031 UINT16 OpLen;
2032 UINT32 Xid;
2033 EFI_STATUS Status;
2034
2035 PxeBc = &Private->PxeBc;
2036 Mode = PxeBc->Mode;
2037 Request = Private->Dhcp6Request;
2038 SrcPort = PXEBC_BS_DISCOVER_PORT;
2039 DestPort = PXEBC_BS_DISCOVER_PORT;
2040
2041 if (!UseBis && Layer != NULL) {
2042 *Layer &= EFI_PXE_BASE_CODE_BOOT_LAYER_MASK;
2043 }
2044
2045 if (Request == NULL) {
2046 return EFI_DEVICE_ERROR;
2047 }
2048
2049 Discover = AllocateZeroPool (sizeof (EFI_PXE_BASE_CODE_DHCPV6_PACKET));
2050 if (Discover == NULL) {
2051 return EFI_OUT_OF_RESOURCES;
2052 }
2053
2054 //
2055 // Build the discover packet by the cached request packet before.
2056 //
2057 Xid = NET_RANDOM (NetRandomInitSeed ());
2058 Discover->TransactionId = HTONL (Xid);
2059 Discover->MessageType = Request->Dhcp6.Header.MessageType;
2060 RequestOpt = Request->Dhcp6.Option;
2061 DiscoverOpt = Discover->DhcpOptions;
2062 DiscoverLen = sizeof (EFI_DHCP6_HEADER);
2063 RequestLen = DiscoverLen;
2064
2065 while (RequestLen < Request->Length) {
2066 OpCode = NTOHS (((EFI_DHCP6_PACKET_OPTION *) RequestOpt)->OpCode);
2067 OpLen = NTOHS (((EFI_DHCP6_PACKET_OPTION *) RequestOpt)->OpLen);
2068 if (OpCode != EFI_DHCP6_IA_TYPE_NA &&
2069 OpCode != EFI_DHCP6_IA_TYPE_TA) {
2070 //
2071 // Copy all the options except IA option.
2072 //
2073 CopyMem (DiscoverOpt, RequestOpt, OpLen + 4);
2074 DiscoverOpt += (OpLen + 4);
2075 DiscoverLen += (OpLen + 4);
2076 }
2077 RequestOpt += (OpLen + 4);
2078 RequestLen += (OpLen + 4);
2079 }
2080
2081 Status = PxeBc->UdpWrite (
2082 PxeBc,
2083 0,
2084 &Private->ServerIp,
2085 &DestPort,
2086 NULL,
2087 &Private->StationIp,
2088 &SrcPort,
2089 NULL,
2090 NULL,
2091 &DiscoverLen,
2092 (VOID *) Discover
2093 );
2094 if (EFI_ERROR (Status)) {
2095 return Status;
2096 }
2097
2098 //
2099 // Cache the right PXE reply packet here, set valid flag later.
2100 // Especially for PXE discover packet, store it into mode data here.
2101 //
2102 if (Private->IsDoDiscover) {
2103 CopyMem (&Mode->PxeDiscover.Dhcpv6, Discover, DiscoverLen);
2104 Reply = &Private->PxeReply.Dhcp6.Packet.Ack;
2105 } else {
2106 Reply = &Private->ProxyOffer.Dhcp6.Packet.Offer;
2107 }
2108 ReadSize = (UINTN) Reply->Size;
2109
2110 //
2111 // Start Udp6Read instance
2112 //
2113 Status = Private->Udp6Read->Configure (Private->Udp6Read, &Private->Udp6CfgData);
2114 if (EFI_ERROR (Status)) {
2115 return Status;
2116 }
2117
2118 Status = PxeBc->UdpRead (
2119 PxeBc,
2120 EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_DEST_IP,
2121 NULL,
2122 &SrcPort,
2123 &Private->ServerIp,
2124 &DestPort,
2125 NULL,
2126 NULL,
2127 &ReadSize,
2128 (VOID *) &Reply->Dhcp6
2129 );
2130 //
2131 // Stop Udp6Read instance
2132 //
2133 Private->Udp6Read->Configure (Private->Udp6Read, NULL);
2134 if (EFI_ERROR (Status)) {
2135 return Status;
2136 }
2137
2138 return EFI_SUCCESS;
2139 }
2140
2141
2142 /**
2143 Start the DHCPv6 S.A.R.R. process to acquire the IPv6 address and other PXE boot information.
2144
2145 @param[in] Private The pointer to PxeBc private data.
2146 @param[in] Dhcp6 The pointer to the EFI_DHCP6_PROTOCOL
2147
2148 @retval EFI_SUCCESS The S.A.R.R. process successfully finished.
2149 @retval Others Failed to finish the S.A.R.R. process.
2150
2151 **/
2152 EFI_STATUS
2153 PxeBcDhcp6Sarr (
2154 IN PXEBC_PRIVATE_DATA *Private,
2155 IN EFI_DHCP6_PROTOCOL *Dhcp6
2156 )
2157 {
2158 EFI_PXE_BASE_CODE_MODE *PxeMode;
2159 EFI_DHCP6_CONFIG_DATA Config;
2160 EFI_DHCP6_MODE_DATA Mode;
2161 EFI_DHCP6_RETRANSMISSION *Retransmit;
2162 EFI_DHCP6_PACKET_OPTION *OptList[PXEBC_DHCP6_OPTION_MAX_NUM];
2163 UINT8 Buffer[PXEBC_DHCP6_OPTION_MAX_SIZE];
2164 UINT32 OptCount;
2165 EFI_STATUS Status;
2166 EFI_IP6_CONFIG_PROTOCOL *Ip6Cfg;
2167 EFI_STATUS TimerStatus;
2168 EFI_EVENT Timer;
2169 UINT64 GetMappingTimeOut;
2170 UINTN DataSize;
2171 EFI_IP6_CONFIG_DUP_ADDR_DETECT_TRANSMITS DadXmits;
2172
2173 Status = EFI_SUCCESS;
2174 PxeMode = Private->PxeBc.Mode;
2175 Ip6Cfg = Private->Ip6Cfg;
2176 Timer = NULL;
2177
2178 //
2179 // Build option list for the request packet.
2180 //
2181 OptCount = PxeBcBuildDhcp6Options (Private, OptList, Buffer);
2182 ASSERT (OptCount> 0);
2183
2184 Retransmit = AllocateZeroPool (sizeof (EFI_DHCP6_RETRANSMISSION));
2185 if (Retransmit == NULL) {
2186 return EFI_OUT_OF_RESOURCES;
2187 }
2188
2189 ZeroMem (&Mode, sizeof (EFI_DHCP6_MODE_DATA));
2190 ZeroMem (&Config, sizeof (EFI_DHCP6_CONFIG_DATA));
2191
2192 Config.OptionCount = OptCount;
2193 Config.OptionList = OptList;
2194 Config.Dhcp6Callback = PxeBcDhcp6CallBack;
2195 Config.CallbackContext = Private;
2196 Config.IaInfoEvent = NULL;
2197 Config.RapidCommit = FALSE;
2198 Config.ReconfigureAccept = FALSE;
2199 Config.IaDescriptor.IaId = Private->IaId;
2200 Config.IaDescriptor.Type = EFI_DHCP6_IA_TYPE_NA;
2201 Config.SolicitRetransmission = Retransmit;
2202 Retransmit->Irt = 4;
2203 Retransmit->Mrc = 4;
2204 Retransmit->Mrt = 32;
2205 Retransmit->Mrd = 60;
2206
2207 //
2208 // Configure the DHCPv6 instance for PXE boot.
2209 //
2210 Status = Dhcp6->Configure (Dhcp6, &Config);
2211 FreePool (Retransmit);
2212 if (EFI_ERROR (Status)) {
2213 return Status;
2214 }
2215
2216 //
2217 // Initialize the record fields for DHCPv6 offer in private data.
2218 //
2219 Private->IsProxyRecved = FALSE;
2220 Private->OfferNum = 0;
2221 Private->SelectIndex = 0;
2222 ZeroMem (Private->OfferCount, sizeof (Private->OfferCount));
2223 ZeroMem (Private->OfferIndex, sizeof (Private->OfferIndex));
2224
2225
2226 //
2227 // Start DHCPv6 S.A.R.R. process to acquire IPv6 address.
2228 //
2229 Status = Dhcp6->Start (Dhcp6);
2230 if (Status == EFI_NO_MAPPING) {
2231 //
2232 // IP6 Linklocal address is not available for use, so stop current Dhcp process
2233 // and wait for duplicate address detection to finish.
2234 //
2235 Dhcp6->Stop (Dhcp6);
2236
2237 //
2238 // Get Duplicate Address Detection Transmits count.
2239 //
2240 DataSize = sizeof (EFI_IP6_CONFIG_DUP_ADDR_DETECT_TRANSMITS);
2241 Status = Ip6Cfg->GetData (
2242 Ip6Cfg,
2243 Ip6ConfigDataTypeDupAddrDetectTransmits,
2244 &DataSize,
2245 &DadXmits
2246 );
2247 if (EFI_ERROR (Status)) {
2248 Dhcp6->Configure (Dhcp6, NULL);
2249 return Status;
2250 }
2251
2252 Status = gBS->CreateEvent (EVT_TIMER, TPL_CALLBACK, NULL, NULL, &Timer);
2253 if (EFI_ERROR (Status)) {
2254 Dhcp6->Configure (Dhcp6, NULL);
2255 return Status;
2256 }
2257
2258 GetMappingTimeOut = TICKS_PER_SECOND * DadXmits.DupAddrDetectTransmits + PXEBC_DAD_ADDITIONAL_DELAY;
2259 Status = gBS->SetTimer (Timer, TimerRelative, GetMappingTimeOut);
2260 if (EFI_ERROR (Status)) {
2261 gBS->CloseEvent (Timer);
2262 Dhcp6->Configure (Dhcp6, NULL);
2263 return Status;
2264 }
2265
2266 do {
2267
2268 TimerStatus = gBS->CheckEvent (Timer);
2269 if (!EFI_ERROR (TimerStatus)) {
2270 Status = Dhcp6->Start (Dhcp6);
2271 }
2272 } while (TimerStatus == EFI_NOT_READY);
2273
2274 gBS->CloseEvent (Timer);
2275 }
2276 if (EFI_ERROR (Status)) {
2277 if (Status == EFI_ICMP_ERROR) {
2278 PxeMode->IcmpErrorReceived = TRUE;
2279 }
2280 Dhcp6->Configure (Dhcp6, NULL);
2281 return Status;
2282 }
2283
2284 //
2285 // Get the acquired IPv6 address and store them.
2286 //
2287 Status = Dhcp6->GetModeData (Dhcp6, &Mode, NULL);
2288 if (EFI_ERROR (Status)) {
2289 Dhcp6->Stop (Dhcp6);
2290 return Status;
2291 }
2292
2293 ASSERT ((Mode.Ia != NULL) && (Mode.Ia->State == Dhcp6Bound));
2294 //
2295 // DHCP6 doesn't have an option to specify the router address on the subnet, the only way to get the
2296 // router address in IP6 is the router discovery mechanism (the RS and RA, which only be handled when
2297 // the IP policy is Automatic). So we just hold the station IP address here and leave the IP policy as
2298 // Automatic, until we get the server IP address. This could let IP6 driver finish the router discovery
2299 // to find a valid router address.
2300 //
2301 CopyMem (&Private->TmpStationIp.v6, &Mode.Ia->IaAddress[0].IpAddress, sizeof (EFI_IPv6_ADDRESS));
2302 if (Mode.ClientId != NULL) {
2303 FreePool (Mode.ClientId);
2304 }
2305 if (Mode.Ia != NULL) {
2306 FreePool (Mode.Ia);
2307 }
2308 //
2309 // Check the selected offer whether BINL retry is needed.
2310 //
2311 Status = PxeBcHandleDhcp6Offer (Private);
2312 if (EFI_ERROR (Status)) {
2313 Dhcp6->Stop (Dhcp6);
2314 return Status;
2315 }
2316
2317 return EFI_SUCCESS;
2318 }