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