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