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