1 /** @file
2 Functions implementation related with DHCPv6 for HTTP boot driver.
3
4 Copyright (c) 2015 - 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
HttpBootBuildDhcp6Options(IN HTTP_BOOT_PRIVATE_DATA * Private,OUT EFI_DHCP6_PACKET_OPTION ** OptList,IN UINT8 * Buffer)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 (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(DHCP6_OPT_BOOT_FILE_URL);
48 OptEnt.Oro->OpCode[1] = HTONS(DHCP6_OPT_BOOT_FILE_PARAM);
49 OptEnt.Oro->OpCode[2] = HTONS(DHCP6_OPT_DNS_SERVERS);
50 OptEnt.Oro->OpCode[3] = HTONS(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 (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 (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 (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 *
HttpBootParseDhcp6Options(IN UINT8 * Buffer,IN UINT32 Length,IN UINT16 OptTag)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
HttpBootParseDhcp6Packet(IN HTTP_BOOT_DHCP6_PACKET_CACHE * Cache6)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) == DHCP6_OPT_IA_NA) {
215 Options[HTTP_BOOT_DHCP6_IDX_IA_NA] = Option;
216 } else if (NTOHS (Option->OpCode) == 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) == DHCP6_OPT_BOOT_FILE_PARAM) {
222 Options[HTTP_BOOT_DHCP6_IDX_BOOT_FILE_PARAM] = Option;
223 } else if (NTOHS (Option->OpCode) == DHCP6_OPT_VENDOR_CLASS) {
224 Options[HTTP_BOOT_DHCP6_IDX_VENDOR_CLASS] = Option;
225 } else if (NTOHS (Option->OpCode) == 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 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 @retval EFI_SUCCESS Packet is copied.
333 @retval EFI_BUFFER_TOO_SMALL Cache buffer is not big enough to hold the packet.
334
335 **/
336 EFI_STATUS
HttpBootCacheDhcp6Packet(IN EFI_DHCP6_PACKET * Dst,IN EFI_DHCP6_PACKET * Src)337 HttpBootCacheDhcp6Packet (
338 IN EFI_DHCP6_PACKET *Dst,
339 IN EFI_DHCP6_PACKET *Src
340 )
341 {
342 if (Dst->Size < Src->Length) {
343 return EFI_BUFFER_TOO_SMALL;
344 }
345
346 CopyMem (&Dst->Dhcp6, &Src->Dhcp6, Src->Length);
347 Dst->Length = Src->Length;
348
349 return EFI_SUCCESS;
350 }
351
352 /**
353 Cache all the received DHCPv6 offers, and set OfferIndex and OfferCount.
354
355 @param[in] Private The pointer to HTTP_BOOT_PRIVATE_DATA.
356 @param[in] RcvdOffer The pointer to the received offer packet.
357
358 @retval EFI_SUCCESS Cache and parse the packet successfully.
359 @retval Others Operation failed.
360
361 **/
362 EFI_STATUS
HttpBootCacheDhcp6Offer(IN HTTP_BOOT_PRIVATE_DATA * Private,IN EFI_DHCP6_PACKET * RcvdOffer)363 HttpBootCacheDhcp6Offer (
364 IN HTTP_BOOT_PRIVATE_DATA *Private,
365 IN EFI_DHCP6_PACKET *RcvdOffer
366 )
367 {
368 HTTP_BOOT_DHCP6_PACKET_CACHE *Cache6;
369 EFI_DHCP6_PACKET *Offer;
370 HTTP_BOOT_OFFER_TYPE OfferType;
371 EFI_STATUS Status;
372
373 Cache6 = &Private->OfferBuffer[Private->OfferNum].Dhcp6;
374 Offer = &Cache6->Packet.Offer;
375
376 //
377 // Cache the content of DHCPv6 packet firstly.
378 //
379 Status = HttpBootCacheDhcp6Packet(Offer, RcvdOffer);
380 if (EFI_ERROR (Status)) {
381 return Status;
382 }
383
384 //
385 // Validate the DHCPv6 packet, and parse the options and offer type.
386 //
387 if (EFI_ERROR (HttpBootParseDhcp6Packet (Cache6))) {
388 return EFI_ABORTED;
389 }
390
391 //
392 // Determine whether cache the current offer by type, and record OfferIndex and OfferCount.
393 //
394 OfferType = Cache6->OfferType;
395 ASSERT (OfferType < HttpOfferTypeMax);
396 ASSERT (Private->OfferCount[OfferType] < HTTP_BOOT_OFFER_MAX_NUM);
397 Private->OfferIndex[OfferType][Private->OfferCount[OfferType]] = Private->OfferNum;
398 Private->OfferCount[OfferType]++;
399 Private->OfferNum++;
400
401 return EFI_SUCCESS;
402 }
403
404 /**
405 EFI_DHCP6_CALLBACK is provided by the consumer of the EFI DHCPv6 Protocol driver
406 to intercept events that occurred in the configuration process.
407
408 @param[in] This The pointer to the EFI DHCPv6 Protocol.
409 @param[in] Context The pointer to the context set by EFI_DHCP6_PROTOCOL.Configure().
410 @param[in] CurrentState The current operational state of the EFI DHCPv Protocol driver.
411 @param[in] Dhcp6Event The event that occurs in the current state, which usually means a
412 state transition.
413 @param[in] Packet The DHCPv6 packet that is going to be sent or was already received.
414 @param[out] NewPacket The packet that is used to replace the Packet above.
415
416 @retval EFI_SUCCESS Told the EFI DHCPv6 Protocol driver to continue the DHCP process.
417 @retval EFI_NOT_READY Only used in the Dhcp6Selecting state. The EFI DHCPv6 Protocol
418 driver will continue to wait for more packets.
419 @retval EFI_ABORTED Told the EFI DHCPv6 Protocol driver to abort the current process.
420 @retval EFI_OUT_OF_RESOURCES There are not enough resources.
421
422 **/
423 EFI_STATUS
424 EFIAPI
HttpBootDhcp6CallBack(IN EFI_DHCP6_PROTOCOL * This,IN VOID * Context,IN EFI_DHCP6_STATE CurrentState,IN EFI_DHCP6_EVENT Dhcp6Event,IN EFI_DHCP6_PACKET * Packet,OUT EFI_DHCP6_PACKET ** NewPacket OPTIONAL)425 HttpBootDhcp6CallBack (
426 IN EFI_DHCP6_PROTOCOL *This,
427 IN VOID *Context,
428 IN EFI_DHCP6_STATE CurrentState,
429 IN EFI_DHCP6_EVENT Dhcp6Event,
430 IN EFI_DHCP6_PACKET *Packet,
431 OUT EFI_DHCP6_PACKET **NewPacket OPTIONAL
432 )
433 {
434 HTTP_BOOT_PRIVATE_DATA *Private;
435 EFI_DHCP6_PACKET *SelectAd;
436 EFI_STATUS Status;
437
438 ASSERT (Packet != NULL);
439
440 Private = (HTTP_BOOT_PRIVATE_DATA *) Context;
441 Status = EFI_SUCCESS;
442 switch (Dhcp6Event) {
443
444 case Dhcp6RcvdAdvertise:
445 Status = EFI_NOT_READY;
446 if (Packet->Length > HTTP_BOOT_DHCP6_PACKET_MAX_SIZE) {
447 //
448 // Ignore the incoming packets which exceed the maximum length.
449 //
450 break;
451 }
452 if (Private->OfferNum < HTTP_BOOT_OFFER_MAX_NUM) {
453 //
454 // Cache the dhcp offers to OfferBuffer[] for select later, and record
455 // the OfferIndex and OfferCount.
456 // If error happens, just ignore this packet and continue to wait more offer.
457 //
458 HttpBootCacheDhcp6Offer (Private, Packet);
459 }
460 break;
461
462 case Dhcp6SelectAdvertise:
463 //
464 // Select offer by the default policy or by order, and record the SelectIndex
465 // and SelectProxyType.
466 //
467 HttpBootSelectDhcpOffer (Private);
468
469 if (Private->SelectIndex == 0) {
470 Status = EFI_ABORTED;
471 } else {
472 ASSERT (NewPacket != NULL);
473 SelectAd = &Private->OfferBuffer[Private->SelectIndex - 1].Dhcp6.Packet.Offer;
474 *NewPacket = AllocateZeroPool (SelectAd->Size);
475 if (*NewPacket == NULL) {
476 return EFI_OUT_OF_RESOURCES;
477 }
478 CopyMem (*NewPacket, SelectAd, SelectAd->Size);
479 }
480 break;
481
482 default:
483 break;
484 }
485
486 return Status;
487 }
488
489 /**
490 Check whether IP driver could route the message which will be sent to ServerIp address.
491
492 This function will check the IP6 route table every 1 seconds until specified timeout is expired, if a valid
493 route is found in IP6 route table, the address will be filed in GatewayAddr and return.
494
495 @param[in] Private The pointer to HTTP_BOOT_PRIVATE_DATA.
496 @param[in] TimeOutInSecond Timeout value in seconds.
497 @param[out] GatewayAddr Pointer to store the gateway IP address.
498
499 @retval EFI_SUCCESS Found a valid gateway address successfully.
500 @retval EFI_TIMEOUT The operation is time out.
501 @retval Other Unexpect error happened.
502
503 **/
504 EFI_STATUS
HttpBootCheckRouteTable(IN HTTP_BOOT_PRIVATE_DATA * Private,IN UINTN TimeOutInSecond,OUT EFI_IPv6_ADDRESS * GatewayAddr)505 HttpBootCheckRouteTable (
506 IN HTTP_BOOT_PRIVATE_DATA *Private,
507 IN UINTN TimeOutInSecond,
508 OUT EFI_IPv6_ADDRESS *GatewayAddr
509 )
510 {
511 EFI_STATUS Status;
512 EFI_IP6_PROTOCOL *Ip6;
513 EFI_IP6_MODE_DATA Ip6ModeData;
514 UINTN Index;
515 EFI_EVENT TimeOutEvt;
516 UINTN RetryCount;
517 BOOLEAN GatewayIsFound;
518
519 ASSERT (GatewayAddr != NULL);
520 ASSERT (Private != NULL);
521
522 Ip6 = Private->Ip6;
523 GatewayIsFound = FALSE;
524 RetryCount = 0;
525 TimeOutEvt = NULL;
526 Status = EFI_SUCCESS;
527 ZeroMem (GatewayAddr, sizeof (EFI_IPv6_ADDRESS));
528
529 while (TRUE) {
530 Status = Ip6->GetModeData (Ip6, &Ip6ModeData, NULL, NULL);
531 if (EFI_ERROR (Status)) {
532 goto ON_EXIT;
533 }
534
535 //
536 // Find out the gateway address which can route the message which send to ServerIp.
537 //
538 for (Index = 0; Index < Ip6ModeData.RouteCount; Index++) {
539 if (NetIp6IsNetEqual (&Private->ServerIp.v6, &Ip6ModeData.RouteTable[Index].Destination, Ip6ModeData.RouteTable[Index].PrefixLength)) {
540 IP6_COPY_ADDRESS (GatewayAddr, &Ip6ModeData.RouteTable[Index].Gateway);
541 GatewayIsFound = TRUE;
542 break;
543 }
544 }
545
546 if (Ip6ModeData.AddressList != NULL) {
547 FreePool (Ip6ModeData.AddressList);
548 }
549 if (Ip6ModeData.GroupTable != NULL) {
550 FreePool (Ip6ModeData.GroupTable);
551 }
552 if (Ip6ModeData.RouteTable != NULL) {
553 FreePool (Ip6ModeData.RouteTable);
554 }
555 if (Ip6ModeData.NeighborCache != NULL) {
556 FreePool (Ip6ModeData.NeighborCache);
557 }
558 if (Ip6ModeData.PrefixTable != NULL) {
559 FreePool (Ip6ModeData.PrefixTable);
560 }
561 if (Ip6ModeData.IcmpTypeList != NULL) {
562 FreePool (Ip6ModeData.IcmpTypeList);
563 }
564
565 if (GatewayIsFound || RetryCount == TimeOutInSecond) {
566 break;
567 }
568
569 RetryCount++;
570
571 //
572 // Delay 1 second then recheck it again.
573 //
574 if (TimeOutEvt == NULL) {
575 Status = gBS->CreateEvent (
576 EVT_TIMER,
577 TPL_CALLBACK,
578 NULL,
579 NULL,
580 &TimeOutEvt
581 );
582 if (EFI_ERROR (Status)) {
583 goto ON_EXIT;
584 }
585 }
586
587 Status = gBS->SetTimer (TimeOutEvt, TimerRelative, TICKS_PER_SECOND);
588 if (EFI_ERROR (Status)) {
589 goto ON_EXIT;
590 }
591 while (EFI_ERROR (gBS->CheckEvent (TimeOutEvt))) {
592 Ip6->Poll (Ip6);
593 }
594 }
595
596 ON_EXIT:
597 if (TimeOutEvt != NULL) {
598 gBS->CloseEvent (TimeOutEvt);
599 }
600
601 if (GatewayIsFound) {
602 Status = EFI_SUCCESS;
603 } else if (RetryCount == TimeOutInSecond) {
604 Status = EFI_TIMEOUT;
605 }
606
607 return Status;
608 }
609
610 /**
611 Set the IP6 policy to Automatic.
612
613 @param[in] Private The pointer to HTTP_BOOT_PRIVATE_DATA.
614
615 @retval EFI_SUCCESS Switch the IP policy succesfully.
616 @retval Others Unexpect error happened.
617
618 **/
619 EFI_STATUS
HttpBootSetIp6Policy(IN HTTP_BOOT_PRIVATE_DATA * Private)620 HttpBootSetIp6Policy (
621 IN HTTP_BOOT_PRIVATE_DATA *Private
622 )
623 {
624 EFI_IP6_CONFIG_POLICY Policy;
625 EFI_IP6_CONFIG_PROTOCOL *Ip6Config;
626 EFI_STATUS Status;
627 UINTN DataSize;
628
629 Ip6Config = Private->Ip6Config;
630 DataSize = sizeof (EFI_IP6_CONFIG_POLICY);
631
632 //
633 // Get and store the current policy of IP6 driver.
634 //
635 Status = Ip6Config->GetData (
636 Ip6Config,
637 Ip6ConfigDataTypePolicy,
638 &DataSize,
639 &Policy
640 );
641 if (EFI_ERROR (Status)) {
642 return Status;
643 }
644
645 if (Policy == Ip6ConfigPolicyManual) {
646 Policy = Ip6ConfigPolicyAutomatic;
647 Status = Ip6Config->SetData (
648 Ip6Config,
649 Ip6ConfigDataTypePolicy,
650 sizeof(EFI_IP6_CONFIG_POLICY),
651 &Policy
652 );
653 if (EFI_ERROR (Status)) {
654 return Status;
655 }
656 }
657 return EFI_SUCCESS;
658 }
659
660 /**
661 This function will register the default DNS addresses to the network device.
662
663 @param[in] Private The pointer to HTTP_BOOT_PRIVATE_DATA.
664 @param[in] DataLength Size of the buffer pointed to by DnsServerData in bytes.
665 @param[in] DnsServerData Point a list of DNS server address in an array
666 of EFI_IPv6_ADDRESS instances.
667
668 @retval EFI_SUCCESS The DNS configuration has been configured successfully.
669 @retval Others Failed to configure the address.
670
671 **/
672 EFI_STATUS
HttpBootSetIp6Dns(IN HTTP_BOOT_PRIVATE_DATA * Private,IN UINTN DataLength,IN VOID * DnsServerData)673 HttpBootSetIp6Dns (
674 IN HTTP_BOOT_PRIVATE_DATA *Private,
675 IN UINTN DataLength,
676 IN VOID *DnsServerData
677 )
678 {
679 EFI_IP6_CONFIG_PROTOCOL *Ip6Config;
680
681 ASSERT (Private->UsingIpv6);
682
683 Ip6Config = Private->Ip6Config;
684
685 return Ip6Config->SetData (
686 Ip6Config,
687 Ip6ConfigDataTypeDnsServer,
688 DataLength,
689 DnsServerData
690 );
691 }
692
693 /**
694 This function will register the IPv6 gateway address to the network device.
695
696 @param[in] Private The pointer to HTTP_BOOT_PRIVATE_DATA.
697
698 @retval EFI_SUCCESS The new IP configuration has been configured successfully.
699 @retval Others Failed to configure the address.
700
701 **/
702 EFI_STATUS
HttpBootSetIp6Gateway(IN HTTP_BOOT_PRIVATE_DATA * Private)703 HttpBootSetIp6Gateway (
704 IN HTTP_BOOT_PRIVATE_DATA *Private
705 )
706 {
707 EFI_IP6_CONFIG_PROTOCOL *Ip6Config;
708 EFI_STATUS Status;
709
710 ASSERT (Private->UsingIpv6);
711 Ip6Config = Private->Ip6Config;
712
713 //
714 // Set the default gateway address.
715 //
716 if (!Private->NoGateway && !NetIp6IsUnspecifiedAddr (&Private->GatewayIp.v6)) {
717 Status = Ip6Config->SetData (
718 Ip6Config,
719 Ip6ConfigDataTypeGateway,
720 sizeof (EFI_IPv6_ADDRESS),
721 &Private->GatewayIp.v6
722 );
723 if (EFI_ERROR(Status)) {
724 return Status;
725 }
726 }
727
728 return EFI_SUCCESS;
729 }
730
731 /**
732 This function will register the station IP address.
733
734 @param[in] Private The pointer to HTTP_BOOT_PRIVATE_DATA.
735
736 @retval EFI_SUCCESS The new IP address has been configured successfully.
737 @retval Others Failed to configure the address.
738
739 **/
740 EFI_STATUS
HttpBootSetIp6Address(IN HTTP_BOOT_PRIVATE_DATA * Private)741 HttpBootSetIp6Address (
742 IN HTTP_BOOT_PRIVATE_DATA *Private
743 )
744 {
745 EFI_STATUS Status;
746 EFI_IP6_PROTOCOL *Ip6;
747 EFI_IP6_CONFIG_PROTOCOL *Ip6Cfg;
748 EFI_IP6_CONFIG_POLICY Policy;
749 EFI_IP6_CONFIG_MANUAL_ADDRESS CfgAddr;
750 EFI_IPv6_ADDRESS *Ip6Addr;
751 EFI_IPv6_ADDRESS GatewayAddr;
752 EFI_IP6_CONFIG_DATA Ip6CfgData;
753 EFI_EVENT MappedEvt;
754 UINTN DataSize;
755 BOOLEAN IsAddressOk;
756 UINTN Index;
757
758 ASSERT (Private->UsingIpv6);
759
760 MappedEvt = NULL;
761 IsAddressOk = FALSE;
762 Ip6Addr = NULL;
763 Ip6Cfg = Private->Ip6Config;
764 Ip6 = Private->Ip6;
765
766 ZeroMem (&CfgAddr, sizeof (EFI_IP6_CONFIG_MANUAL_ADDRESS));
767 CopyMem (&CfgAddr, &Private->StationIp.v6, sizeof (EFI_IPv6_ADDRESS));
768 ZeroMem (&Ip6CfgData, sizeof (EFI_IP6_CONFIG_DATA));
769
770 Ip6CfgData.AcceptIcmpErrors = TRUE;
771 Ip6CfgData.DefaultProtocol = IP6_ICMP;
772 Ip6CfgData.HopLimit = HTTP_BOOT_DEFAULT_HOPLIMIT;
773 Ip6CfgData.ReceiveTimeout = HTTP_BOOT_DEFAULT_LIFETIME;
774 Ip6CfgData.TransmitTimeout = HTTP_BOOT_DEFAULT_LIFETIME;
775
776 Status = Ip6->Configure (Ip6, &Ip6CfgData);
777 if (EFI_ERROR (Status)) {
778 goto ON_EXIT;
779 }
780
781 //
782 // Retrieve the gateway address from IP6 route table.
783 //
784 Status = HttpBootCheckRouteTable (Private, HTTP_BOOT_IP6_ROUTE_TABLE_TIMEOUT, &GatewayAddr);
785 if (EFI_ERROR (Status)) {
786 Private->NoGateway = TRUE;
787 } else {
788 IP6_COPY_ADDRESS (&Private->GatewayIp.v6, &GatewayAddr);
789 }
790
791 //
792 // Set the new address by Ip6ConfigProtocol manually.
793 //
794 Policy = Ip6ConfigPolicyManual;
795 Status = Ip6Cfg->SetData (
796 Ip6Cfg,
797 Ip6ConfigDataTypePolicy,
798 sizeof(EFI_IP6_CONFIG_POLICY),
799 &Policy
800 );
801 if (EFI_ERROR (Status)) {
802 goto ON_EXIT;
803 }
804
805 //
806 // Create a notify event to set address flag when DAD if IP6 driver succeeded.
807 //
808 Status = gBS->CreateEvent (
809 EVT_NOTIFY_SIGNAL,
810 TPL_NOTIFY,
811 HttpBootCommonNotify,
812 &IsAddressOk,
813 &MappedEvt
814 );
815 if (EFI_ERROR (Status)) {
816 goto ON_EXIT;
817 }
818
819 //
820 // Set static host ip6 address. This is a asynchronous process.
821 //
822 Status = Ip6Cfg->RegisterDataNotify (
823 Ip6Cfg,
824 Ip6ConfigDataTypeManualAddress,
825 MappedEvt
826 );
827 if (EFI_ERROR(Status)) {
828 goto ON_EXIT;
829 }
830
831 Status = Ip6Cfg->SetData (
832 Ip6Cfg,
833 Ip6ConfigDataTypeManualAddress,
834 sizeof (EFI_IP6_CONFIG_MANUAL_ADDRESS),
835 &CfgAddr
836 );
837 if (EFI_ERROR (Status) && Status != EFI_NOT_READY) {
838 goto ON_EXIT;
839 } else if (Status == EFI_NOT_READY) {
840 //
841 // Poll the network until the asynchronous process is finished.
842 //
843 while (!IsAddressOk) {
844 Ip6->Poll (Ip6);
845 }
846 //
847 // Check whether the Ip6 Address setting is successed.
848 //
849 DataSize = 0;
850 Status = Ip6Cfg->GetData (
851 Ip6Cfg,
852 Ip6ConfigDataTypeManualAddress,
853 &DataSize,
854 NULL
855 );
856 if (Status != EFI_BUFFER_TOO_SMALL || DataSize == 0) {
857 Status = EFI_DEVICE_ERROR;
858 goto ON_EXIT;
859 }
860
861 Ip6Addr = AllocatePool (DataSize);
862 if (Ip6Addr == NULL) {
863 return EFI_OUT_OF_RESOURCES;
864 }
865 Status = Ip6Cfg->GetData (
866 Ip6Cfg,
867 Ip6ConfigDataTypeManualAddress,
868 &DataSize,
869 (VOID *) Ip6Addr
870 );
871 if (EFI_ERROR (Status)) {
872 Status = EFI_DEVICE_ERROR;
873 goto ON_EXIT;
874 }
875
876 for (Index = 0; Index < DataSize / sizeof (EFI_IPv6_ADDRESS); Index ++) {
877 if (CompareMem (Ip6Addr + Index, &CfgAddr, sizeof (EFI_IPv6_ADDRESS)) == 0) {
878 break;
879 }
880 }
881 if (Index == DataSize / sizeof (EFI_IPv6_ADDRESS)) {
882 Status = EFI_ABORTED;
883 goto ON_EXIT;
884 }
885 }
886
887 ON_EXIT:
888 if (MappedEvt != NULL) {
889 Ip6Cfg->UnregisterDataNotify (
890 Ip6Cfg,
891 Ip6ConfigDataTypeManualAddress,
892 MappedEvt
893 );
894 gBS->CloseEvent (MappedEvt);
895 }
896
897 if (Ip6Addr != NULL) {
898 FreePool (Ip6Addr);
899 }
900
901 return Status;
902 }
903
904 /**
905 Start the S.A.R.R DHCPv6 process to acquire the IPv6 address and other Http boot information.
906
907 @param[in] Private Pointer to HTTP_BOOT private data.
908
909 @retval EFI_SUCCESS The S.A.R.R process successfully finished.
910 @retval Others Failed to finish the S.A.R.R process.
911
912 **/
913 EFI_STATUS
HttpBootDhcp6Sarr(IN HTTP_BOOT_PRIVATE_DATA * Private)914 HttpBootDhcp6Sarr (
915 IN HTTP_BOOT_PRIVATE_DATA *Private
916 )
917 {
918 EFI_DHCP6_PROTOCOL *Dhcp6;
919 EFI_DHCP6_CONFIG_DATA Config;
920 EFI_DHCP6_MODE_DATA Mode;
921 EFI_DHCP6_RETRANSMISSION *Retransmit;
922 EFI_DHCP6_PACKET_OPTION *OptList[HTTP_BOOT_DHCP6_OPTION_MAX_NUM];
923 UINT32 OptCount;
924 UINT8 Buffer[HTTP_BOOT_DHCP6_OPTION_MAX_SIZE];
925 EFI_STATUS Status;
926
927 Dhcp6 = Private->Dhcp6;
928 ASSERT (Dhcp6 != NULL);
929
930 //
931 // Build options list for the request packet.
932 //
933 OptCount = HttpBootBuildDhcp6Options (Private, OptList, Buffer);
934 ASSERT (OptCount >0);
935
936 Retransmit = AllocateZeroPool (sizeof (EFI_DHCP6_RETRANSMISSION));
937 if (Retransmit == NULL) {
938 return EFI_OUT_OF_RESOURCES;
939 }
940
941 ZeroMem (&Mode, sizeof (EFI_DHCP6_MODE_DATA));
942 ZeroMem (&Config, sizeof (EFI_DHCP6_CONFIG_DATA));
943
944 Config.OptionCount = OptCount;
945 Config.OptionList = OptList;
946 Config.Dhcp6Callback = HttpBootDhcp6CallBack;
947 Config.CallbackContext = Private;
948 Config.IaInfoEvent = NULL;
949 Config.RapidCommit = FALSE;
950 Config.ReconfigureAccept = FALSE;
951 Config.IaDescriptor.IaId = NET_RANDOM (NetRandomInitSeed ());
952 Config.IaDescriptor.Type = EFI_DHCP6_IA_TYPE_NA;
953 Config.SolicitRetransmission = Retransmit;
954 Retransmit->Irt = 4;
955 Retransmit->Mrc = 4;
956 Retransmit->Mrt = 32;
957 Retransmit->Mrd = 60;
958
959 //
960 // Configure the DHCPv6 instance for HTTP boot.
961 //
962 Status = Dhcp6->Configure (Dhcp6, &Config);
963 FreePool (Retransmit);
964 if (EFI_ERROR (Status)) {
965 goto ON_EXIT;
966 }
967 //
968 // Initialize the record fields for DHCPv6 offer in private data.
969 //
970 Private->OfferNum = 0;
971 Private->SelectIndex = 0;
972 ZeroMem (Private->OfferCount, sizeof (Private->OfferCount));
973 ZeroMem (Private->OfferIndex, sizeof (Private->OfferIndex));
974
975 //
976 // Start DHCPv6 S.A.R.R. process to acquire IPv6 address.
977 //
978 Status = Dhcp6->Start (Dhcp6);
979 if (EFI_ERROR (Status)) {
980 goto ON_EXIT;
981 }
982
983 //
984 // Get the acquired IPv6 address and store them.
985 //
986 Status = Dhcp6->GetModeData (Dhcp6, &Mode, NULL);
987 if (EFI_ERROR (Status)) {
988 goto ON_EXIT;
989 }
990
991 ASSERT (Mode.Ia->State == Dhcp6Bound);
992 CopyMem (&Private->StationIp.v6, &Mode.Ia->IaAddress[0].IpAddress, sizeof (EFI_IPv6_ADDRESS));
993
994 AsciiPrint ("\n Station IPv6 address is ");
995 HttpBootShowIp6Addr (&Private->StationIp.v6);
996 AsciiPrint ("\n");
997
998 ON_EXIT:
999 if (EFI_ERROR (Status)) {
1000 Dhcp6->Stop (Dhcp6);
1001 Dhcp6->Configure (Dhcp6, NULL);
1002 } else {
1003 ZeroMem (&Config, sizeof (EFI_DHCP6_CONFIG_DATA));
1004 Dhcp6->Configure (Dhcp6, &Config);
1005 if (Mode.ClientId != NULL) {
1006 FreePool (Mode.ClientId);
1007 }
1008 if (Mode.Ia != NULL) {
1009 FreePool (Mode.Ia);
1010 }
1011 }
1012
1013 return Status;
1014
1015 }
1016
1017