NetworkPkg:Fix bug when parsing the dhcp6 option 16
[mirror_edk2.git] / NetworkPkg / HttpBootDxe / HttpBootDhcp6.c
1 /** @file
2 Functions implementation related with DHCPv6 for HTTP boot driver.
3
4 Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials are licensed and made available under
6 the terms and conditions of the BSD License that accompanies this distribution.
7 The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php.
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "HttpBootDxe.h"
16
17 /**
18 Build the options buffer for the DHCPv6 request packet.
19
20 @param[in] Private The pointer to HTTP BOOT driver private data.
21 @param[out] OptList The pointer to the option pointer array.
22 @param[in] Buffer The pointer to the buffer to contain the option list.
23
24 @return Index The count of the built-in options.
25
26 **/
27 UINT32
28 HttpBootBuildDhcp6Options (
29 IN HTTP_BOOT_PRIVATE_DATA *Private,
30 OUT EFI_DHCP6_PACKET_OPTION **OptList,
31 IN UINT8 *Buffer
32 )
33 {
34 HTTP_BOOT_DHCP6_OPTION_ENTRY OptEnt;
35 UINT16 Value;
36 UINT32 Index;
37
38 Index = 0;
39 OptList[0] = (EFI_DHCP6_PACKET_OPTION *) Buffer;
40
41 //
42 // Append client option request option
43 //
44 OptList[Index]->OpCode = HTONS (HTTP_BOOT_DHCP6_OPT_ORO);
45 OptList[Index]->OpLen = HTONS (8);
46 OptEnt.Oro = (HTTP_BOOT_DHCP6_OPTION_ORO *) OptList[Index]->Data;
47 OptEnt.Oro->OpCode[0] = HTONS(HTTP_BOOT_DHCP6_OPT_BOOT_FILE_URL);
48 OptEnt.Oro->OpCode[1] = HTONS(HTTP_BOOT_DHCP6_OPT_BOOT_FILE_PARAM);
49 OptEnt.Oro->OpCode[2] = HTONS(HTTP_BOOT_DHCP6_OPT_DNS_SERVERS);
50 OptEnt.Oro->OpCode[3] = HTONS(HTTP_BOOT_DHCP6_OPT_VENDOR_CLASS);
51 Index++;
52 OptList[Index] = GET_NEXT_DHCP6_OPTION (OptList[Index - 1]);
53
54 //
55 // Append client network device interface option
56 //
57 OptList[Index]->OpCode = HTONS (HTTP_BOOT_DHCP6_OPT_UNDI);
58 OptList[Index]->OpLen = HTONS ((UINT16)3);
59 OptEnt.Undi = (HTTP_BOOT_DHCP6_OPTION_UNDI *) OptList[Index]->Data;
60
61 if (Private->Nii != NULL) {
62 OptEnt.Undi->Type = Private->Nii->Type;
63 OptEnt.Undi->MajorVer = Private->Nii->MajorVer;
64 OptEnt.Undi->MinorVer = Private->Nii->MinorVer;
65 } else {
66 OptEnt.Undi->Type = DEFAULT_UNDI_TYPE;
67 OptEnt.Undi->MajorVer = DEFAULT_UNDI_MAJOR;
68 OptEnt.Undi->MinorVer = DEFAULT_UNDI_MINOR;
69 }
70
71 Index++;
72 OptList[Index] = GET_NEXT_DHCP6_OPTION (OptList[Index - 1]);
73
74 //
75 // Append client system architecture option
76 //
77 OptList[Index]->OpCode = HTONS (HTTP_BOOT_DHCP6_OPT_ARCH);
78 OptList[Index]->OpLen = HTONS ((UINT16) sizeof (HTTP_BOOT_DHCP6_OPTION_ARCH));
79 OptEnt.Arch = (HTTP_BOOT_DHCP6_OPTION_ARCH *) OptList[Index]->Data;
80 Value = HTONS (EFI_HTTP_BOOT_CLIENT_SYSTEM_ARCHITECTURE);
81 CopyMem (&OptEnt.Arch->Type, &Value, sizeof (UINT16));
82 Index++;
83 OptList[Index] = GET_NEXT_DHCP6_OPTION (OptList[Index - 1]);
84
85 //
86 // Append vendor class identify option.
87 //
88 OptList[Index]->OpCode = HTONS (HTTP_BOOT_DHCP6_OPT_VENDOR_CLASS);
89 OptList[Index]->OpLen = HTONS ((UINT16) sizeof (HTTP_BOOT_DHCP6_OPTION_VENDOR_CLASS));
90 OptEnt.VendorClass = (HTTP_BOOT_DHCP6_OPTION_VENDOR_CLASS *) OptList[Index]->Data;
91 OptEnt.VendorClass->Vendor = HTONL (HTTP_BOOT_DHCP6_ENTERPRISE_NUM);
92 OptEnt.VendorClass->ClassLen = HTONS ((UINT16) sizeof (HTTP_BOOT_CLASS_ID));
93 CopyMem (
94 &OptEnt.VendorClass->ClassId,
95 DEFAULT_CLASS_ID_DATA,
96 sizeof (HTTP_BOOT_CLASS_ID)
97 );
98 HttpBootUintnToAscDecWithFormat (
99 EFI_HTTP_BOOT_CLIENT_SYSTEM_ARCHITECTURE,
100 OptEnt.VendorClass->ClassId.ArchitectureType,
101 sizeof (OptEnt.VendorClass->ClassId.ArchitectureType)
102 );
103
104 if (Private->Nii != NULL) {
105 CopyMem (
106 OptEnt.VendorClass->ClassId.InterfaceName,
107 Private->Nii->StringId,
108 sizeof (OptEnt.VendorClass->ClassId.InterfaceName)
109 );
110 HttpBootUintnToAscDecWithFormat (
111 Private->Nii->MajorVer,
112 OptEnt.VendorClass->ClassId.UndiMajor,
113 sizeof (OptEnt.VendorClass->ClassId.UndiMajor)
114 );
115 HttpBootUintnToAscDecWithFormat (
116 Private->Nii->MinorVer,
117 OptEnt.VendorClass->ClassId.UndiMinor,
118 sizeof (OptEnt.VendorClass->ClassId.UndiMinor)
119 );
120 }
121
122 Index++;
123
124 return Index;
125 }
126
127 /**
128 Parse out a DHCPv6 option by OptTag, and find the position in buffer.
129
130 @param[in] Buffer The pointer to the option buffer.
131 @param[in] Length Length of the option buffer.
132 @param[in] OptTag The required option tag.
133
134 @retval NULL Failed to parse the required option.
135 @retval Others The postion of the required option in buffer.
136
137 **/
138 EFI_DHCP6_PACKET_OPTION *
139 HttpBootParseDhcp6Options (
140 IN UINT8 *Buffer,
141 IN UINT32 Length,
142 IN UINT16 OptTag
143 )
144 {
145 EFI_DHCP6_PACKET_OPTION *Option;
146 UINT32 Offset;
147
148 Option = (EFI_DHCP6_PACKET_OPTION *) Buffer;
149 Offset = 0;
150
151 //
152 // OpLen and OpCode here are both stored in network order.
153 //
154 while (Offset < Length) {
155
156 if (NTOHS (Option->OpCode) == OptTag) {
157
158 return Option;
159 }
160
161 Offset += (NTOHS(Option->OpLen) + 4);
162 Option = (EFI_DHCP6_PACKET_OPTION *) (Buffer + Offset);
163 }
164
165 return NULL;
166
167 }
168
169 /**
170 Parse the cached DHCPv6 packet, including all the options.
171
172 @param[in] Cache6 The pointer to a cached DHCPv6 packet.
173
174 @retval EFI_SUCCESS Parsed the DHCPv6 packet successfully.
175 @retval EFI_DEVICE_ERROR Failed to parse and invalid the packet.
176
177 **/
178 EFI_STATUS
179 HttpBootParseDhcp6Packet (
180 IN HTTP_BOOT_DHCP6_PACKET_CACHE *Cache6
181 )
182 {
183 EFI_DHCP6_PACKET *Offer;
184 EFI_DHCP6_PACKET_OPTION **Options;
185 EFI_DHCP6_PACKET_OPTION *Option;
186 HTTP_BOOT_OFFER_TYPE OfferType;
187 EFI_IPv6_ADDRESS IpAddr;
188 BOOLEAN IsProxyOffer;
189 BOOLEAN IsHttpOffer;
190 BOOLEAN IsDnsOffer;
191 BOOLEAN IpExpressedUri;
192 EFI_STATUS Status;
193 UINT32 Offset;
194 UINT32 Length;
195
196 IsDnsOffer = FALSE;
197 IpExpressedUri = FALSE;
198 IsProxyOffer = TRUE;
199 IsHttpOffer = FALSE;
200 Offer = &Cache6->Packet.Offer;
201 Options = Cache6->OptList;
202
203 ZeroMem (Cache6->OptList, sizeof (Cache6->OptList));
204
205 Option = (EFI_DHCP6_PACKET_OPTION *) (Offer->Dhcp6.Option);
206 Offset = 0;
207 Length = GET_DHCP6_OPTION_SIZE (Offer);
208
209 //
210 // OpLen and OpCode here are both stored in network order, since they are from original packet.
211 //
212 while (Offset < Length) {
213
214 if (NTOHS (Option->OpCode) == HTTP_BOOT_DHCP6_OPT_IA_NA) {
215 Options[HTTP_BOOT_DHCP6_IDX_IA_NA] = Option;
216 } else if (NTOHS (Option->OpCode) == HTTP_BOOT_DHCP6_OPT_BOOT_FILE_URL) {
217 //
218 // The server sends this option to inform the client about an URL to a boot file.
219 //
220 Options[HTTP_BOOT_DHCP6_IDX_BOOT_FILE_URL] = Option;
221 } else if (NTOHS (Option->OpCode) == HTTP_BOOT_DHCP6_OPT_BOOT_FILE_PARAM) {
222 Options[HTTP_BOOT_DHCP6_IDX_BOOT_FILE_PARAM] = Option;
223 } else if (NTOHS (Option->OpCode) == HTTP_BOOT_DHCP6_OPT_VENDOR_CLASS) {
224 Options[HTTP_BOOT_DHCP6_IDX_VENDOR_CLASS] = Option;
225 } else if (NTOHS (Option->OpCode) == HTTP_BOOT_DHCP6_OPT_DNS_SERVERS) {
226 Options[HTTP_BOOT_DHCP6_IDX_DNS_SERVER] = Option;
227 }
228
229 Offset += (NTOHS (Option->OpLen) + 4);
230 Option = (EFI_DHCP6_PACKET_OPTION *) (Offer->Dhcp6.Option + Offset);
231 }
232 //
233 // The offer with assigned client address is NOT a proxy offer.
234 // An ia_na option, embeded with valid ia_addr option and a status_code of success.
235 //
236 Option = Options[HTTP_BOOT_DHCP6_IDX_IA_NA];
237 if (Option != NULL) {
238 Option = HttpBootParseDhcp6Options (
239 Option->Data + 12,
240 NTOHS (Option->OpLen),
241 HTTP_BOOT_DHCP6_OPT_STATUS_CODE
242 );
243 if ((Option != NULL && Option->Data[0] == 0) || (Option == NULL)) {
244 IsProxyOffer = FALSE;
245 }
246 }
247
248 //
249 // The offer with "HTTPClient" is a Http offer.
250 //
251 Option = Options[HTTP_BOOT_DHCP6_IDX_VENDOR_CLASS];
252
253 if (Option != NULL &&
254 NTOHS(Option->OpLen) >= 16 &&
255 CompareMem ((Option->Data + 6), DEFAULT_CLASS_ID_DATA, 10) == 0) {
256 IsHttpOffer = TRUE;
257 }
258
259 //
260 // The offer with Domain Server is a DNS offer.
261 //
262 Option = Options[HTTP_BOOT_DHCP6_IDX_DNS_SERVER];
263 if (Option != NULL) {
264 IsDnsOffer = TRUE;
265 }
266
267 //
268 // Http offer must have a boot URI.
269 //
270 if (IsHttpOffer && Options[HTTP_BOOT_DHCP6_IDX_BOOT_FILE_URL] == NULL) {
271 return EFI_DEVICE_ERROR;
272 }
273
274 //
275 // Try to retrieve the IP of HTTP server from URI.
276 //
277 if (IsHttpOffer) {
278 Status = HttpParseUrl (
279 (CHAR8*) Options[HTTP_BOOT_DHCP6_IDX_BOOT_FILE_URL]->Data,
280 (UINT32) AsciiStrLen ((CHAR8*) Options[HTTP_BOOT_DHCP6_IDX_BOOT_FILE_URL]->Data),
281 FALSE,
282 &Cache6->UriParser
283 );
284 if (EFI_ERROR (Status)) {
285 return EFI_DEVICE_ERROR;
286 }
287
288 Status = HttpUrlGetIp6 (
289 (CHAR8*) Options[HTTP_BOOT_DHCP6_IDX_BOOT_FILE_URL]->Data,
290 Cache6->UriParser,
291 &IpAddr
292 );
293 IpExpressedUri = !EFI_ERROR (Status);
294 }
295
296 //
297 // Determine offer type of the DHCPv6 packet.
298 //
299 if (IsHttpOffer) {
300 if (IpExpressedUri) {
301 if (IsProxyOffer) {
302 OfferType = HttpOfferTypeProxyIpUri;
303 } else {
304 OfferType = IsDnsOffer ? HttpOfferTypeDhcpIpUriDns : HttpOfferTypeDhcpIpUri;
305 }
306 } else {
307 if (!IsProxyOffer) {
308 OfferType = IsDnsOffer ? HttpOfferTypeDhcpNameUriDns : HttpOfferTypeDhcpNameUri;
309 } else {
310 OfferType = HttpOfferTypeProxyNameUri;
311 }
312 }
313
314 } else {
315 if (!IsProxyOffer) {
316 OfferType = IsDnsOffer ? HttpOfferTypeDhcpDns : HttpOfferTypeDhcpOnly;
317 } else {
318 return EFI_DEVICE_ERROR;
319 }
320 }
321
322 Cache6->OfferType = OfferType;
323 return EFI_SUCCESS;
324 }
325
326 /**
327 Cache the DHCPv6 packet.
328
329 @param[in] Dst The pointer to the cache buffer for DHCPv6 packet.
330 @param[in] Src The pointer to the DHCPv6 packet to be cached.
331
332 **/
333 VOID
334 HttpBootCacheDhcp6Packet (
335 IN EFI_DHCP6_PACKET *Dst,
336 IN EFI_DHCP6_PACKET *Src
337 )
338 {
339 ASSERT (Dst->Size >= Src->Length);
340
341 CopyMem (&Dst->Dhcp6, &Src->Dhcp6, Src->Length);
342 Dst->Length = Src->Length;
343 }
344
345 /**
346 Cache all the received DHCPv6 offers, and set OfferIndex and OfferCount.
347
348 @param[in] Private The pointer to HTTP_BOOT_PRIVATE_DATA.
349 @param[in] RcvdOffer The pointer to the received offer packet.
350
351 **/
352 VOID
353 HttpBootCacheDhcp6Offer (
354 IN HTTP_BOOT_PRIVATE_DATA *Private,
355 IN EFI_DHCP6_PACKET *RcvdOffer
356 )
357 {
358 HTTP_BOOT_DHCP6_PACKET_CACHE *Cache6;
359 EFI_DHCP6_PACKET *Offer;
360 HTTP_BOOT_OFFER_TYPE OfferType;
361
362 Cache6 = &Private->OfferBuffer[Private->OfferNum].Dhcp6;
363 Offer = &Cache6->Packet.Offer;
364
365 //
366 // Cache the content of DHCPv6 packet firstly.
367 //
368 HttpBootCacheDhcp6Packet(Offer, RcvdOffer);
369
370 //
371 // Validate the DHCPv6 packet, and parse the options and offer type.
372 //
373 if (EFI_ERROR (HttpBootParseDhcp6Packet (Cache6))) {
374 return ;
375 }
376
377 //
378 // Determine whether cache the current offer by type, and record OfferIndex and OfferCount.
379 //
380 OfferType = Cache6->OfferType;
381 ASSERT (OfferType < HttpOfferTypeMax);
382 ASSERT (Private->OfferCount[OfferType] < HTTP_BOOT_OFFER_MAX_NUM);
383 Private->OfferIndex[OfferType][Private->OfferCount[OfferType]] = Private->OfferNum;
384 Private->OfferCount[OfferType]++;
385 Private->OfferNum++;
386 }
387
388 /**
389 EFI_DHCP6_CALLBACK is provided by the consumer of the EFI DHCPv6 Protocol driver
390 to intercept events that occurred in the configuration process.
391
392 @param[in] This The pointer to the EFI DHCPv6 Protocol.
393 @param[in] Context The pointer to the context set by EFI_DHCP6_PROTOCOL.Configure().
394 @param[in] CurrentState The current operational state of the EFI DHCPv Protocol driver.
395 @param[in] Dhcp6Event The event that occurs in the current state, which usually means a
396 state transition.
397 @param[in] Packet The DHCPv6 packet that is going to be sent or was already received.
398 @param[out] NewPacket The packet that is used to replace the Packet above.
399
400 @retval EFI_SUCCESS Told the EFI DHCPv6 Protocol driver to continue the DHCP process.
401 @retval EFI_NOT_READY Only used in the Dhcp6Selecting state. The EFI DHCPv6 Protocol
402 driver will continue to wait for more packets.
403 @retval EFI_ABORTED Told the EFI DHCPv6 Protocol driver to abort the current process.
404
405 **/
406 EFI_STATUS
407 EFIAPI
408 HttpBootDhcp6CallBack (
409 IN EFI_DHCP6_PROTOCOL *This,
410 IN VOID *Context,
411 IN EFI_DHCP6_STATE CurrentState,
412 IN EFI_DHCP6_EVENT Dhcp6Event,
413 IN EFI_DHCP6_PACKET *Packet,
414 OUT EFI_DHCP6_PACKET **NewPacket OPTIONAL
415 )
416 {
417 HTTP_BOOT_PRIVATE_DATA *Private;
418 EFI_DHCP6_PACKET *SelectAd;
419 EFI_STATUS Status;
420 if ((Dhcp6Event != Dhcp6RcvdAdvertise) && (Dhcp6Event != Dhcp6SelectAdvertise)) {
421 return EFI_SUCCESS;
422 }
423
424 ASSERT (Packet != NULL);
425
426 Private = (HTTP_BOOT_PRIVATE_DATA *) Context;
427 Status = EFI_SUCCESS;
428 switch (Dhcp6Event) {
429
430 case Dhcp6RcvdAdvertise:
431 Status = EFI_NOT_READY;
432 if (Private->OfferNum < HTTP_BOOT_OFFER_MAX_NUM) {
433 //
434 // Cache the dhcp offers to OfferBuffer[] for select later, and record
435 // the OfferIndex and OfferCount.
436 //
437 HttpBootCacheDhcp6Offer (Private, Packet);
438 }
439 break;
440
441 case Dhcp6SelectAdvertise:
442 //
443 // Select offer by the default policy or by order, and record the SelectIndex
444 // and SelectProxyType.
445 //
446 HttpBootSelectDhcpOffer (Private);
447
448 if (Private->SelectIndex == 0) {
449 Status = EFI_ABORTED;
450 } else {
451 ASSERT (NewPacket != NULL);
452 SelectAd = &Private->OfferBuffer[Private->SelectIndex - 1].Dhcp6.Packet.Offer;
453 *NewPacket = AllocateZeroPool (SelectAd->Size);
454 ASSERT (*NewPacket != NULL);
455 CopyMem (*NewPacket, SelectAd, SelectAd->Size);
456 }
457 break;
458
459 default:
460 break;
461 }
462
463 return Status;
464 }
465
466 /**
467 Check whether IP driver could route the message which will be sent to ServerIp address.
468
469 This function will check the IP6 route table every 1 seconds until specified timeout is expired, if a valid
470 route is found in IP6 route table, the address will be filed in GatewayAddr and return.
471
472 @param[in] Private The pointer to HTTP_BOOT_PRIVATE_DATA.
473 @param[in] TimeOutInSecond Timeout value in seconds.
474 @param[out] GatewayAddr Pointer to store the gateway IP address.
475
476 @retval EFI_SUCCESS Found a valid gateway address successfully.
477 @retval EFI_TIMEOUT The operation is time out.
478 @retval Other Unexpect error happened.
479
480 **/
481 EFI_STATUS
482 HttpBootCheckRouteTable (
483 IN HTTP_BOOT_PRIVATE_DATA *Private,
484 IN UINTN TimeOutInSecond,
485 OUT EFI_IPv6_ADDRESS *GatewayAddr
486 )
487 {
488 EFI_STATUS Status;
489 EFI_IP6_PROTOCOL *Ip6;
490 EFI_IP6_MODE_DATA Ip6ModeData;
491 UINTN Index;
492 EFI_EVENT TimeOutEvt;
493 UINTN RetryCount;
494 BOOLEAN GatewayIsFound;
495
496 ASSERT (GatewayAddr != NULL);
497 ASSERT (Private != NULL);
498
499 Ip6 = Private->Ip6;
500 GatewayIsFound = FALSE;
501 RetryCount = 0;
502 TimeOutEvt = NULL;
503 Status = EFI_SUCCESS;
504 ZeroMem (GatewayAddr, sizeof (EFI_IPv6_ADDRESS));
505
506 while (TRUE) {
507 Status = Ip6->GetModeData (Ip6, &Ip6ModeData, NULL, NULL);
508 if (EFI_ERROR (Status)) {
509 goto ON_EXIT;
510 }
511
512 //
513 // Find out the gateway address which can route the message which send to ServerIp.
514 //
515 for (Index = 0; Index < Ip6ModeData.RouteCount; Index++) {
516 if (NetIp6IsNetEqual (&Private->ServerIp.v6, &Ip6ModeData.RouteTable[Index].Destination, Ip6ModeData.RouteTable[Index].PrefixLength)) {
517 IP6_COPY_ADDRESS (GatewayAddr, &Ip6ModeData.RouteTable[Index].Gateway);
518 GatewayIsFound = TRUE;
519 break;
520 }
521 }
522
523 if (Ip6ModeData.AddressList != NULL) {
524 FreePool (Ip6ModeData.AddressList);
525 }
526 if (Ip6ModeData.GroupTable != NULL) {
527 FreePool (Ip6ModeData.GroupTable);
528 }
529 if (Ip6ModeData.RouteTable != NULL) {
530 FreePool (Ip6ModeData.RouteTable);
531 }
532 if (Ip6ModeData.NeighborCache != NULL) {
533 FreePool (Ip6ModeData.NeighborCache);
534 }
535 if (Ip6ModeData.PrefixTable != NULL) {
536 FreePool (Ip6ModeData.PrefixTable);
537 }
538 if (Ip6ModeData.IcmpTypeList != NULL) {
539 FreePool (Ip6ModeData.IcmpTypeList);
540 }
541
542 if (GatewayIsFound || RetryCount == TimeOutInSecond) {
543 break;
544 }
545
546 RetryCount++;
547
548 //
549 // Delay 1 second then recheck it again.
550 //
551 if (TimeOutEvt == NULL) {
552 Status = gBS->CreateEvent (
553 EVT_TIMER,
554 TPL_CALLBACK,
555 NULL,
556 NULL,
557 &TimeOutEvt
558 );
559 if (EFI_ERROR (Status)) {
560 goto ON_EXIT;
561 }
562 }
563
564 Status = gBS->SetTimer (TimeOutEvt, TimerRelative, TICKS_PER_SECOND);
565 if (EFI_ERROR (Status)) {
566 goto ON_EXIT;
567 }
568 while (EFI_ERROR (gBS->CheckEvent (TimeOutEvt))) {
569 Ip6->Poll (Ip6);
570 }
571 }
572
573 ON_EXIT:
574 if (TimeOutEvt != NULL) {
575 gBS->CloseEvent (TimeOutEvt);
576 }
577
578 if (GatewayIsFound) {
579 Status = EFI_SUCCESS;
580 } else if (RetryCount == TimeOutInSecond) {
581 Status = EFI_TIMEOUT;
582 }
583
584 return Status;
585 }
586
587 /**
588 Set the IP6 policy to Automatic.
589
590 @param[in] Private The pointer to HTTP_BOOT_PRIVATE_DATA.
591
592 @retval EFI_SUCCESS Switch the IP policy succesfully.
593 @retval Others Unexpect error happened.
594
595 **/
596 EFI_STATUS
597 HttpBootSetIp6Policy (
598 IN HTTP_BOOT_PRIVATE_DATA *Private
599 )
600 {
601 EFI_IP6_CONFIG_POLICY Policy;
602 EFI_IP6_CONFIG_PROTOCOL *Ip6Config;
603 EFI_STATUS Status;
604 UINTN DataSize;
605
606 Ip6Config = Private->Ip6Config;
607 DataSize = sizeof (EFI_IP6_CONFIG_POLICY);
608
609 //
610 // Get and store the current policy of IP6 driver.
611 //
612 Status = Ip6Config->GetData (
613 Ip6Config,
614 Ip6ConfigDataTypePolicy,
615 &DataSize,
616 &Policy
617 );
618 if (EFI_ERROR (Status)) {
619 return Status;
620 }
621
622 if (Policy == Ip6ConfigPolicyManual) {
623 Policy = Ip6ConfigPolicyAutomatic;
624 Status = Ip6Config->SetData (
625 Ip6Config,
626 Ip6ConfigDataTypePolicy,
627 sizeof(EFI_IP6_CONFIG_POLICY),
628 &Policy
629 );
630 if (EFI_ERROR (Status)) {
631 return Status;
632 }
633 }
634 return EFI_SUCCESS;
635 }
636
637 /**
638 This function will register the default DNS addresses to the network device.
639
640 @param[in] Private The pointer to HTTP_BOOT_PRIVATE_DATA.
641 @param[in] DataLength Size of the buffer pointed to by DnsServerData in bytes.
642 @param[in] DnsServerData Point a list of DNS server address in an array
643 of EFI_IPv6_ADDRESS instances.
644
645 @retval EFI_SUCCESS The DNS configuration has been configured successfully.
646 @retval Others Failed to configure the address.
647
648 **/
649 EFI_STATUS
650 HttpBootSetIp6Dns (
651 IN HTTP_BOOT_PRIVATE_DATA *Private,
652 IN UINTN DataLength,
653 IN VOID *DnsServerData
654 )
655 {
656 EFI_IP6_CONFIG_PROTOCOL *Ip6Config;
657
658 ASSERT (Private->UsingIpv6);
659
660 Ip6Config = Private->Ip6Config;
661
662 return Ip6Config->SetData (
663 Ip6Config,
664 Ip6ConfigDataTypeDnsServer,
665 DataLength,
666 DnsServerData
667 );
668 }
669
670 /**
671 This function will register the IPv6 gateway address to the network device.
672
673 @param[in] Private The pointer to HTTP_BOOT_PRIVATE_DATA.
674
675 @retval EFI_SUCCESS The new IP configuration has been configured successfully.
676 @retval Others Failed to configure the address.
677
678 **/
679 EFI_STATUS
680 HttpBootSetIp6Gateway (
681 IN HTTP_BOOT_PRIVATE_DATA *Private
682 )
683 {
684 EFI_IP6_CONFIG_PROTOCOL *Ip6Config;
685 EFI_STATUS Status;
686
687 ASSERT (Private->UsingIpv6);
688 Ip6Config = Private->Ip6Config;
689
690 //
691 // Set the default gateway address.
692 //
693 if (!Private->NoGateway && !NetIp6IsUnspecifiedAddr (&Private->GatewayIp.v6)) {
694 Status = Ip6Config->SetData (
695 Ip6Config,
696 Ip6ConfigDataTypeGateway,
697 sizeof (EFI_IPv6_ADDRESS),
698 &Private->GatewayIp.v6
699 );
700 if (EFI_ERROR(Status)) {
701 return Status;
702 }
703 }
704
705 return EFI_SUCCESS;
706 }
707
708 /**
709 This function will register the station IP address.
710
711 @param[in] Private The pointer to HTTP_BOOT_PRIVATE_DATA.
712
713 @retval EFI_SUCCESS The new IP address has been configured successfully.
714 @retval Others Failed to configure the address.
715
716 **/
717 EFI_STATUS
718 HttpBootSetIp6Address (
719 IN HTTP_BOOT_PRIVATE_DATA *Private
720 )
721 {
722 EFI_STATUS Status;
723 EFI_IP6_PROTOCOL *Ip6;
724 EFI_IP6_CONFIG_PROTOCOL *Ip6Cfg;
725 EFI_IP6_CONFIG_POLICY Policy;
726 EFI_IP6_CONFIG_MANUAL_ADDRESS CfgAddr;
727 EFI_IPv6_ADDRESS *Ip6Addr;
728 EFI_IPv6_ADDRESS GatewayAddr;
729 EFI_IP6_CONFIG_DATA Ip6CfgData;
730 EFI_EVENT MappedEvt;
731 UINTN DataSize;
732 BOOLEAN IsAddressOk;
733 UINTN Index;
734
735 ASSERT (Private->UsingIpv6);
736
737 MappedEvt = NULL;
738 IsAddressOk = FALSE;
739 Ip6Addr = NULL;
740 Ip6Cfg = Private->Ip6Config;
741 Ip6 = Private->Ip6;
742
743 ZeroMem (&CfgAddr, sizeof (EFI_IP6_CONFIG_MANUAL_ADDRESS));
744 CopyMem (&CfgAddr, &Private->StationIp.v6, sizeof (EFI_IPv6_ADDRESS));
745 ZeroMem (&Ip6CfgData, sizeof (EFI_IP6_CONFIG_DATA));
746
747 Ip6CfgData.AcceptIcmpErrors = TRUE;
748 Ip6CfgData.DefaultProtocol = IP6_ICMP;
749 Ip6CfgData.HopLimit = HTTP_BOOT_DEFAULT_HOPLIMIT;
750 Ip6CfgData.ReceiveTimeout = HTTP_BOOT_DEFAULT_LIFETIME;
751 Ip6CfgData.TransmitTimeout = HTTP_BOOT_DEFAULT_LIFETIME;
752
753 Status = Ip6->Configure (Ip6, &Ip6CfgData);
754 if (EFI_ERROR (Status)) {
755 goto ON_EXIT;
756 }
757
758 //
759 // Retrieve the gateway address from IP6 route table.
760 //
761 Status = HttpBootCheckRouteTable (Private, HTTP_BOOT_IP6_ROUTE_TABLE_TIMEOUT, &GatewayAddr);
762 if (EFI_ERROR (Status)) {
763 Private->NoGateway = TRUE;
764 } else {
765 IP6_COPY_ADDRESS (&Private->GatewayIp.v6, &GatewayAddr);
766 }
767
768 //
769 // Set the new address by Ip6ConfigProtocol manually.
770 //
771 Policy = Ip6ConfigPolicyManual;
772 Status = Ip6Cfg->SetData (
773 Ip6Cfg,
774 Ip6ConfigDataTypePolicy,
775 sizeof(EFI_IP6_CONFIG_POLICY),
776 &Policy
777 );
778 if (EFI_ERROR (Status)) {
779 goto ON_EXIT;
780 }
781
782 //
783 // Create a notify event to set address flag when DAD if IP6 driver succeeded.
784 //
785 Status = gBS->CreateEvent (
786 EVT_NOTIFY_SIGNAL,
787 TPL_NOTIFY,
788 HttpBootCommonNotify,
789 &IsAddressOk,
790 &MappedEvt
791 );
792 if (EFI_ERROR (Status)) {
793 goto ON_EXIT;
794 }
795
796 //
797 // Set static host ip6 address. This is a asynchronous process.
798 //
799 Status = Ip6Cfg->RegisterDataNotify (
800 Ip6Cfg,
801 Ip6ConfigDataTypeManualAddress,
802 MappedEvt
803 );
804 if (EFI_ERROR(Status)) {
805 goto ON_EXIT;
806 }
807
808 Status = Ip6Cfg->SetData (
809 Ip6Cfg,
810 Ip6ConfigDataTypeManualAddress,
811 sizeof (EFI_IP6_CONFIG_MANUAL_ADDRESS),
812 &CfgAddr
813 );
814 if (EFI_ERROR (Status) && Status != EFI_NOT_READY) {
815 goto ON_EXIT;
816 } else if (Status == EFI_NOT_READY) {
817 //
818 // Poll the network until the asynchronous process is finished.
819 //
820 while (!IsAddressOk) {
821 Ip6->Poll (Ip6);
822 }
823 //
824 // Check whether the Ip6 Address setting is successed.
825 //
826 DataSize = 0;
827 Status = Ip6Cfg->GetData (
828 Ip6Cfg,
829 Ip6ConfigDataTypeManualAddress,
830 &DataSize,
831 NULL
832 );
833 if (Status != EFI_BUFFER_TOO_SMALL || DataSize == 0) {
834 Status = EFI_DEVICE_ERROR;
835 goto ON_EXIT;
836 }
837
838 Ip6Addr = AllocatePool (DataSize);
839 if (Ip6Addr == NULL) {
840 return EFI_OUT_OF_RESOURCES;
841 }
842 Status = Ip6Cfg->GetData (
843 Ip6Cfg,
844 Ip6ConfigDataTypeManualAddress,
845 &DataSize,
846 (VOID *) Ip6Addr
847 );
848 if (EFI_ERROR (Status)) {
849 Status = EFI_DEVICE_ERROR;
850 goto ON_EXIT;
851 }
852
853 for (Index = 0; Index < DataSize / sizeof (EFI_IPv6_ADDRESS); Index ++) {
854 if (CompareMem (Ip6Addr + Index, &CfgAddr, sizeof (EFI_IPv6_ADDRESS)) == 0) {
855 break;
856 }
857 }
858 if (Index == DataSize / sizeof (EFI_IPv6_ADDRESS)) {
859 Status = EFI_ABORTED;
860 goto ON_EXIT;
861 }
862 }
863
864 ON_EXIT:
865 if (MappedEvt != NULL) {
866 Ip6Cfg->UnregisterDataNotify (
867 Ip6Cfg,
868 Ip6ConfigDataTypeManualAddress,
869 MappedEvt
870 );
871 gBS->CloseEvent (MappedEvt);
872 }
873
874 if (Ip6Addr != NULL) {
875 FreePool (Ip6Addr);
876 }
877
878 return Status;
879 }
880
881 /**
882 Start the S.A.R.R DHCPv6 process to acquire the IPv6 address and other Http boot information.
883
884 @param[in] Private Pointer to HTTP_BOOT private data.
885
886 @retval EFI_SUCCESS The S.A.R.R process successfully finished.
887 @retval Others Failed to finish the S.A.R.R process.
888
889 **/
890 EFI_STATUS
891 HttpBootDhcp6Sarr (
892 IN HTTP_BOOT_PRIVATE_DATA *Private
893 )
894 {
895 EFI_DHCP6_PROTOCOL *Dhcp6;
896 EFI_DHCP6_CONFIG_DATA Config;
897 EFI_DHCP6_MODE_DATA Mode;
898 EFI_DHCP6_RETRANSMISSION *Retransmit;
899 EFI_DHCP6_PACKET_OPTION *OptList[HTTP_BOOT_DHCP6_OPTION_MAX_NUM];
900 UINT32 OptCount;
901 UINT8 Buffer[HTTP_BOOT_DHCP6_OPTION_MAX_SIZE];
902 EFI_STATUS Status;
903
904 Dhcp6 = Private->Dhcp6;
905 ASSERT (Dhcp6 != NULL);
906
907 //
908 // Build options list for the request packet.
909 //
910 OptCount = HttpBootBuildDhcp6Options (Private, OptList, Buffer);
911 ASSERT (OptCount >0);
912
913 Retransmit = AllocateZeroPool (sizeof (EFI_DHCP6_RETRANSMISSION));
914 if (Retransmit == NULL) {
915 return EFI_OUT_OF_RESOURCES;
916 }
917
918 ZeroMem (&Mode, sizeof (EFI_DHCP6_MODE_DATA));
919 ZeroMem (&Config, sizeof (EFI_DHCP6_CONFIG_DATA));
920
921 Config.OptionCount = OptCount;
922 Config.OptionList = OptList;
923 Config.Dhcp6Callback = HttpBootDhcp6CallBack;
924 Config.CallbackContext = Private;
925 Config.IaInfoEvent = NULL;
926 Config.RapidCommit = FALSE;
927 Config.ReconfigureAccept = FALSE;
928 Config.IaDescriptor.IaId = NET_RANDOM (NetRandomInitSeed ());
929 Config.IaDescriptor.Type = EFI_DHCP6_IA_TYPE_NA;
930 Config.SolicitRetransmission = Retransmit;
931 Retransmit->Irt = 4;
932 Retransmit->Mrc = 4;
933 Retransmit->Mrt = 32;
934 Retransmit->Mrd = 60;
935
936 //
937 // Configure the DHCPv6 instance for HTTP boot.
938 //
939 Status = Dhcp6->Configure (Dhcp6, &Config);
940 FreePool (Retransmit);
941 if (EFI_ERROR (Status)) {
942 goto ON_EXIT;
943 }
944 //
945 // Initialize the record fields for DHCPv6 offer in private data.
946 //
947 Private->OfferNum = 0;
948 Private->SelectIndex = 0;
949 ZeroMem (Private->OfferCount, sizeof (Private->OfferCount));
950 ZeroMem (Private->OfferIndex, sizeof (Private->OfferIndex));
951
952 //
953 // Start DHCPv6 S.A.R.R. process to acquire IPv6 address.
954 //
955 Status = Dhcp6->Start (Dhcp6);
956 if (EFI_ERROR (Status)) {
957 goto ON_EXIT;
958 }
959
960 //
961 // Get the acquired IPv6 address and store them.
962 //
963 Status = Dhcp6->GetModeData (Dhcp6, &Mode, NULL);
964 if (EFI_ERROR (Status)) {
965 goto ON_EXIT;
966 }
967
968 ASSERT (Mode.Ia->State == Dhcp6Bound);
969 CopyMem (&Private->StationIp.v6, &Mode.Ia->IaAddress[0].IpAddress, sizeof (EFI_IPv6_ADDRESS));
970
971 AsciiPrint ("\n Station IPv6 address is ");
972 HttpBootShowIp6Addr (&Private->StationIp.v6);
973 AsciiPrint ("\n");
974
975 ON_EXIT:
976 if (EFI_ERROR (Status)) {
977 Dhcp6->Stop (Dhcp6);
978 Dhcp6->Configure (Dhcp6, NULL);
979 } else {
980 ZeroMem (&Config, sizeof (EFI_DHCP6_CONFIG_DATA));
981 Dhcp6->Configure (Dhcp6, &Config);
982 if (Mode.ClientId != NULL) {
983 FreePool (Mode.ClientId);
984 }
985 if (Mode.Ia != NULL) {
986 FreePool (Mode.Ia);
987 }
988 }
989
990 return Status;
991
992 }
993