]> git.proxmox.com Git - mirror_edk2.git/blob - NetworkPkg/Ip6Dxe/Ip6Common.c
Add NetworkPkg (P.UDK2010.UP3.Network.P1)
[mirror_edk2.git] / NetworkPkg / Ip6Dxe / Ip6Common.c
1 /** @file
2 The implementation of common functions shared by IP6 driver.
3
4 Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>
5
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php.
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #include "Ip6Impl.h"
17
18 /**
19 Build a array of EFI_IP6_ADDRESS_INFO to be returned to the caller. The number
20 of EFI_IP6_ADDRESS_INFO is also returned. If AddressList is NULL,
21 only the address count is returned.
22
23 @param[in] IpSb The IP6 service binding instance.
24 @param[out] AddressCount The number of returned addresses.
25 @param[out] AddressList The pointer to the array of EFI_IP6_ADDRESS_INFO.
26 This is an optional parameter.
27
28
29 @retval EFI_SUCCESS The address array successfully built.
30 @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the address info.
31 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
32
33 **/
34 EFI_STATUS
35 Ip6BuildEfiAddressList (
36 IN IP6_SERVICE *IpSb,
37 OUT UINT32 *AddressCount,
38 OUT EFI_IP6_ADDRESS_INFO **AddressList OPTIONAL
39 )
40 {
41 UINT32 Count;
42 LIST_ENTRY *Entry;
43 EFI_IP6_ADDRESS_INFO *EfiAddrInfo;
44 IP6_ADDRESS_INFO *AddrInfo;
45
46 if (AddressCount == NULL) {
47 return EFI_INVALID_PARAMETER;
48 }
49
50 if (IpSb->LinkLocalOk) {
51 Count = 1 + IpSb->DefaultInterface->AddressCount;
52 } else {
53 Count = 0;
54 }
55
56 *AddressCount = Count;
57
58 if ((AddressList == NULL) || (Count == 0)) {
59 return EFI_SUCCESS;
60 }
61
62 if (*AddressList == NULL) {
63 *AddressList = AllocatePool (sizeof (EFI_IP6_ADDRESS_INFO) * Count);
64 if (*AddressList == NULL) {
65 return EFI_OUT_OF_RESOURCES;
66 }
67 }
68
69 EfiAddrInfo = *AddressList;
70
71 IP6_COPY_ADDRESS (&EfiAddrInfo->Address, &IpSb->LinkLocalAddr);
72 EfiAddrInfo->PrefixLength = IP6_LINK_LOCAL_PREFIX_LENGTH;
73
74 EfiAddrInfo++;
75 Count = 1;
76
77 NET_LIST_FOR_EACH (Entry, &IpSb->DefaultInterface->AddressList) {
78 AddrInfo = NET_LIST_USER_STRUCT_S (Entry, IP6_ADDRESS_INFO, Link, IP6_ADDR_INFO_SIGNATURE);
79
80 IP6_COPY_ADDRESS (&EfiAddrInfo->Address, &AddrInfo->Address);
81 EfiAddrInfo->PrefixLength = AddrInfo->PrefixLength;
82
83 EfiAddrInfo++;
84 Count++;
85 }
86
87 ASSERT (Count == *AddressCount);
88
89 return EFI_SUCCESS;
90 }
91
92 /**
93 Generate the multicast addresses identify the group of all IPv6 nodes or IPv6
94 routers defined in RFC4291.
95
96 All Nodes Addresses: FF01::1, FF02::1.
97 All Router Addresses: FF01::2, FF02::2, FF05::2.
98
99 @param[in] Router If TRUE, generate all routers addresses,
100 else generate all node addresses.
101 @param[in] Scope interface-local(1), link-local(2), or site-local(5)
102 @param[out] Ip6Addr The generated multicast address.
103
104 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
105 @retval EFI_SUCCESS The address is generated.
106
107 **/
108 EFI_STATUS
109 Ip6SetToAllNodeMulticast (
110 IN BOOLEAN Router,
111 IN UINT8 Scope,
112 OUT EFI_IPv6_ADDRESS *Ip6Addr
113 )
114 {
115 if (Ip6Addr == NULL) {
116 return EFI_INVALID_PARAMETER;
117 }
118
119 if (!Router && Scope == IP6_SITE_LOCAL_SCOPE) {
120 return EFI_INVALID_PARAMETER;
121 }
122
123 ZeroMem (Ip6Addr, sizeof (EFI_IPv6_ADDRESS));
124 Ip6Addr->Addr[0] = 0xFF;
125 Ip6Addr->Addr[1] = Scope;
126
127 if (!Router) {
128 Ip6Addr->Addr[15] = 0x1;
129 } else {
130 Ip6Addr->Addr[15] = 0x2;
131 }
132
133 return EFI_SUCCESS;
134 }
135
136 /**
137 This function converts MAC address to 64 bits interface ID according to RFC4291
138 and returns the interface ID. Currently only 48-bit MAC address is supported by
139 this function.
140
141 @param[in, out] IpSb The IP6 service binding instance.
142
143 @retval NULL The operation fails.
144 @return Pointer to the generated interface ID.
145
146 **/
147 UINT8 *
148 Ip6CreateInterfaceID (
149 IN OUT IP6_SERVICE *IpSb
150 )
151 {
152 UINT8 InterfaceId[8];
153 UINT8 Byte;
154 EFI_MAC_ADDRESS *MacAddr;
155 UINT32 AddrLen;
156
157 NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE);
158
159 AddrLen = IpSb->SnpMode.HwAddressSize;
160
161 //
162 // Currently only IEEE 802 48-bit MACs are supported to create link local address.
163 //
164 if (AddrLen != IP6_MAC_LEN || IpSb->InterfaceIdLen != IP6_IF_ID_LEN) {
165 return NULL;
166 }
167
168 MacAddr = &IpSb->SnpMode.CurrentAddress;
169
170 //
171 // Convert MAC address to 64 bits interface ID according to Appendix A of RFC4291:
172 // 1. Insert 0xFFFE to the middle
173 // 2. Invert the universal/local bit - bit 6 in network order
174 //
175 CopyMem (InterfaceId, MacAddr, 3);
176 InterfaceId[3] = 0xFF;
177 InterfaceId[4] = 0xFE;
178 CopyMem (&InterfaceId[5], &MacAddr->Addr[3], 3);
179
180 Byte = (UINT8) (InterfaceId[0] & IP6_U_BIT);
181 if (Byte == IP6_U_BIT) {
182 InterfaceId[0] &= ~IP6_U_BIT;
183 } else {
184 InterfaceId[0] |= IP6_U_BIT;
185 }
186
187 //
188 // Return the interface ID.
189 //
190 return AllocateCopyPool (IpSb->InterfaceIdLen, InterfaceId);
191 }
192
193 /**
194 This function creates link-local address from interface identifier. The
195 interface identifier is normally created from MAC address. It might be manually
196 configured by administrator if the link-local address created from MAC address
197 is a duplicate address.
198
199 @param[in, out] IpSb The IP6 service binding instance.
200
201 @retval NULL If the operation fails.
202 @return The generated Link Local address, in network order.
203
204 **/
205 EFI_IPv6_ADDRESS *
206 Ip6CreateLinkLocalAddr (
207 IN OUT IP6_SERVICE *IpSb
208 )
209 {
210 EFI_IPv6_ADDRESS *Ip6Addr;
211 EFI_IP6_CONFIG_PROTOCOL *Ip6Config;
212 UINTN DataSize;
213 EFI_IP6_CONFIG_INTERFACE_ID InterfaceId;
214 EFI_STATUS Status;
215
216 NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE);
217
218 if (IpSb->InterfaceId != NULL) {
219 FreePool (IpSb->InterfaceId);
220 }
221
222 //
223 // Get the interface id if it is manully configured.
224 //
225 Ip6Config = &IpSb->Ip6ConfigInstance.Ip6Config;
226 DataSize = sizeof (EFI_IP6_CONFIG_INTERFACE_ID);
227 ZeroMem (&InterfaceId, DataSize);
228
229 Status = Ip6Config->GetData (
230 Ip6Config,
231 Ip6ConfigDataTypeAltInterfaceId,
232 &DataSize,
233 &InterfaceId
234 );
235 if (Status == EFI_NOT_FOUND) {
236 //
237 // Since the interface id is not configured, generate the interface id from
238 // MAC address.
239 //
240 IpSb->InterfaceId = Ip6CreateInterfaceID (IpSb);
241 if (IpSb->InterfaceId == NULL) {
242 return NULL;
243 }
244
245 CopyMem (&InterfaceId, IpSb->InterfaceId, IpSb->InterfaceIdLen);
246 //
247 // Record the interface id.
248 //
249 Status = Ip6Config->SetData (
250 Ip6Config,
251 Ip6ConfigDataTypeAltInterfaceId,
252 DataSize,
253 &InterfaceId
254 );
255 if (EFI_ERROR (Status)) {
256 FreePool (IpSb->InterfaceId);
257 IpSb->InterfaceId = NULL;
258 return NULL;
259 }
260 } else if (!EFI_ERROR (Status)) {
261 IpSb->InterfaceId = AllocateCopyPool (DataSize, &InterfaceId);
262 if (IpSb->InterfaceId == NULL) {
263 return NULL;
264 }
265 } else {
266 return NULL;
267 }
268
269 //
270 // Append FE80::/64 to the left of IPv6 address then return.
271 //
272 Ip6Addr = AllocateZeroPool (sizeof (EFI_IPv6_ADDRESS));
273 if (Ip6Addr == NULL) {
274 FreePool (IpSb->InterfaceId);
275 IpSb->InterfaceId = NULL;
276 return NULL;
277 }
278
279 CopyMem (&Ip6Addr->Addr[8], IpSb->InterfaceId, IpSb->InterfaceIdLen);
280 Ip6Addr->Addr[1] = 0x80;
281 Ip6Addr->Addr[0] = 0xFE;
282
283 return Ip6Addr;
284 }
285
286 /**
287 Compute the solicited-node multicast address for an unicast or anycast address,
288 by taking the low-order 24 bits of this address, and appending those bits to
289 the prefix FF02:0:0:0:0:1:FF00::/104.
290
291 @param[in] Ip6Addr The unicast or anycast address, in network order.
292 @param[out] MulticastAddr The generated solicited-node multicast address,
293 in network order.
294
295 **/
296 VOID
297 Ip6CreateSNMulticastAddr (
298 IN EFI_IPv6_ADDRESS *Ip6Addr,
299 OUT EFI_IPv6_ADDRESS *MulticastAddr
300 )
301 {
302 ASSERT (Ip6Addr != NULL && MulticastAddr != NULL);
303
304 ZeroMem (MulticastAddr, sizeof (EFI_IPv6_ADDRESS));
305
306 MulticastAddr->Addr[0] = 0xFF;
307 MulticastAddr->Addr[1] = 0x02;
308 MulticastAddr->Addr[11] = 0x1;
309 MulticastAddr->Addr[12] = 0xFF;
310
311 CopyMem (&MulticastAddr->Addr[13], &Ip6Addr->Addr[13], 3);
312 }
313
314 /**
315 Insert a node IP6_ADDRESS_INFO to an IP6 interface.
316
317 @param[in, out] IpIf Points to an IP6 interface.
318 @param[in] AddrInfo Points to IP6_ADDRESS_INFO
319
320 **/
321 VOID
322 Ip6AddAddr (
323 IN OUT IP6_INTERFACE *IpIf,
324 IN IP6_ADDRESS_INFO *AddrInfo
325 )
326 {
327 InsertHeadList (&IpIf->AddressList, &AddrInfo->Link);
328 IpIf->AddressCount++;
329 }
330
331 /**
332 Destroy the IP instance if its StationAddress is removed. It is the help function
333 for Ip6RemoveAddr().
334
335 @param[in, out] IpSb Points to an IP6 service binding instance.
336 @param[in] Address The to be removed address
337
338 **/
339 VOID
340 Ip6DestroyInstanceByAddress (
341 IN OUT IP6_SERVICE *IpSb,
342 IN EFI_IPv6_ADDRESS *Address
343 )
344 {
345 BOOLEAN OneDestroyed;
346 EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding;
347 LIST_ENTRY *Entry;
348 IP6_PROTOCOL *Instance;
349
350 NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE);
351
352 ServiceBinding = &IpSb->ServiceBinding;
353
354 //
355 // Upper layer IP protocol consumers may have tight relationship between several
356 // IP protocol instances, in other words, calling ServiceBinding->DestroyChild to
357 // destroy one IP child may cause other related IP children destroyed too. This
358 // will probably leave hole in the children list when we iterate it. So everytime
359 // we just destroy one child then back to the start point to iterate the list.
360 //
361 do {
362 OneDestroyed = FALSE;
363
364 NET_LIST_FOR_EACH (Entry, &IpSb->Children) {
365 Instance = NET_LIST_USER_STRUCT_S (Entry, IP6_PROTOCOL, Link, IP6_PROTOCOL_SIGNATURE);
366
367 if ((Instance->State == IP6_STATE_CONFIGED) && EFI_IP6_EQUAL (&Instance->ConfigData.StationAddress, Address)) {
368 ServiceBinding->DestroyChild (ServiceBinding, Instance->Handle);
369 OneDestroyed = TRUE;
370 break;
371 }
372 }
373 } while (OneDestroyed);
374 }
375
376 /**
377 Remove the IPv6 address from the address list node points to IP6_ADDRESS_INFO.
378
379 This function removes the matching IPv6 addresses from the address list and
380 adjusts the address count of the address list. If IpSb is not NULL, this function
381 calls Ip6LeaveGroup to see whether it should call Mnp->Groups() to remove the
382 its solicited-node multicast MAC address from the filter list and sends out
383 a Multicast Listener Done. If Prefix is NULL, all address in the address list
384 will be removed. If Prefix is not NULL, the address that matching the Prefix
385 with PrefixLength in the address list will be removed.
386
387 @param[in] IpSb NULL or points to IP6 service binding instance.
388 @param[in, out] AddressList Address list array.
389 @param[in, out] AddressCount The count of addresses in address list array.
390 @param[in] Prefix NULL or an IPv6 address prefix.
391 @param[in] PrefixLength The length of Prefix.
392
393 @retval EFI_SUCCESS The operation completed successfully.
394 @retval EFI_NOT_FOUND The address matching the Prefix with PrefixLength
395 cannot be found in the address list.
396 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
397
398 **/
399 EFI_STATUS
400 Ip6RemoveAddr (
401 IN IP6_SERVICE *IpSb OPTIONAL,
402 IN OUT LIST_ENTRY *AddressList,
403 IN OUT UINT32 *AddressCount,
404 IN EFI_IPv6_ADDRESS *Prefix OPTIONAL,
405 IN UINT8 PrefixLength
406 )
407 {
408 EFI_STATUS Status;
409 LIST_ENTRY *Entry;
410 LIST_ENTRY *Next;
411 IP6_ADDRESS_INFO *AddrInfo;
412 EFI_IPv6_ADDRESS SnMCastAddr;
413
414 if (IsListEmpty (AddressList) || *AddressCount < 1 || PrefixLength > IP6_PREFIX_NUM) {
415 return EFI_INVALID_PARAMETER;
416 }
417
418 Status = EFI_NOT_FOUND;
419
420 NET_LIST_FOR_EACH_SAFE (Entry, Next, AddressList) {
421 AddrInfo = NET_LIST_USER_STRUCT_S (Entry, IP6_ADDRESS_INFO, Link, IP6_ADDR_INFO_SIGNATURE);
422
423 if (Prefix == NULL ||
424 (PrefixLength == 128 && EFI_IP6_EQUAL (Prefix, &AddrInfo->Address)) ||
425 (PrefixLength == AddrInfo->PrefixLength && NetIp6IsNetEqual (Prefix, &AddrInfo->Address, PrefixLength))
426 ) {
427 if (IpSb != NULL) {
428 NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE);
429 Ip6CreateSNMulticastAddr (&AddrInfo->Address, &SnMCastAddr);
430 Ip6LeaveGroup (IpSb, &SnMCastAddr);
431
432 //
433 // Destroy any instance who is using the dying address as the source address.
434 //
435 Ip6DestroyInstanceByAddress (IpSb, &AddrInfo->Address);
436 }
437
438 RemoveEntryList (Entry);
439 FreePool (AddrInfo);
440 (*AddressCount)--;
441
442 Status = EFI_SUCCESS;
443 }
444 }
445
446 return Status;
447 }
448
449 /**
450 Check whether the incoming Ipv6 address is a solicited-node multicast address.
451
452 @param[in] Ip6 Ip6 address, in network order.
453
454 @retval TRUE Yes, solicited-node multicast address
455 @retval FALSE No
456
457 **/
458 BOOLEAN
459 Ip6IsSNMulticastAddr (
460 IN EFI_IPv6_ADDRESS *Ip6
461 )
462 {
463 EFI_IPv6_ADDRESS Sn;
464 BOOLEAN Flag;
465
466 Ip6CreateSNMulticastAddr (Ip6, &Sn);
467 Flag = FALSE;
468
469 if (CompareMem (Sn.Addr, Ip6->Addr, 13) == 0) {
470 Flag = TRUE;
471 }
472
473 return Flag;
474 }
475
476 /**
477 Check whether the incoming IPv6 address is one of the maintained addresses in
478 the IP6 service binding instance.
479
480 @param[in] IpSb Points to a IP6 service binding instance.
481 @param[in] Address The IP6 address to be checked.
482 @param[out] Interface If not NULL, output the IP6 interface which
483 maintains the Address.
484 @param[out] AddressInfo If not NULL, output the IP6 address information
485 of the Address.
486
487 @retval TRUE Yes, it is one of the maintained address.
488 @retval FALSE No, it is not one of the maintained address.
489
490 **/
491 BOOLEAN
492 Ip6IsOneOfSetAddress (
493 IN IP6_SERVICE *IpSb,
494 IN EFI_IPv6_ADDRESS *Address,
495 OUT IP6_INTERFACE **Interface OPTIONAL,
496 OUT IP6_ADDRESS_INFO **AddressInfo OPTIONAL
497 )
498 {
499 LIST_ENTRY *Entry;
500 LIST_ENTRY *Entry2;
501 IP6_INTERFACE *IpIf;
502 IP6_ADDRESS_INFO *TmpAddressInfo;
503
504 //
505 // Check link-local address first
506 //
507 if (IpSb->LinkLocalOk && EFI_IP6_EQUAL (&IpSb->LinkLocalAddr, Address)) {
508 if (Interface != NULL) {
509 *Interface = IpSb->DefaultInterface;
510 }
511
512 if (AddressInfo != NULL) {
513 *AddressInfo = NULL;
514 }
515
516 return TRUE;
517 }
518
519 NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) {
520 IpIf = NET_LIST_USER_STRUCT_S (Entry, IP6_INTERFACE, Link, IP6_INTERFACE_SIGNATURE);
521
522 NET_LIST_FOR_EACH (Entry2, &IpIf->AddressList) {
523 TmpAddressInfo = NET_LIST_USER_STRUCT_S (Entry2, IP6_ADDRESS_INFO, Link, IP6_ADDR_INFO_SIGNATURE);
524
525 if (EFI_IP6_EQUAL (&TmpAddressInfo->Address, Address)) {
526 if (Interface != NULL) {
527 *Interface = IpIf;
528 }
529
530 if (AddressInfo != NULL) {
531 *AddressInfo = TmpAddressInfo;
532 }
533
534 return TRUE;
535 }
536 }
537 }
538
539 return FALSE;
540 }
541
542 /**
543 Check whether the incoming MAC address is valid.
544
545 @param[in] IpSb Points to a IP6 service binding instance.
546 @param[in] LinkAddress The MAC address.
547
548 @retval TRUE Yes, it is valid.
549 @retval FALSE No, it is not valid.
550
551 **/
552 BOOLEAN
553 Ip6IsValidLinkAddress (
554 IN IP6_SERVICE *IpSb,
555 IN EFI_MAC_ADDRESS *LinkAddress
556 )
557 {
558 UINT32 Index;
559
560 //
561 // TODO: might be updated later to be more acceptable.
562 //
563 for (Index = IpSb->SnpMode.HwAddressSize; Index < sizeof (EFI_MAC_ADDRESS); Index++) {
564 if (LinkAddress->Addr[Index] != 0) {
565 return FALSE;
566 }
567 }
568
569 return TRUE;
570 }
571
572 /**
573 Copy the PrefixLength bits from Src to Dest.
574
575 @param[out] Dest A pointer to the buffer to copy to.
576 @param[in] Src A pointer to the buffer to copy from.
577 @param[in] PrefixLength The number of bits to copy.
578
579 **/
580 VOID
581 Ip6CopyAddressByPrefix (
582 OUT EFI_IPv6_ADDRESS *Dest,
583 IN EFI_IPv6_ADDRESS *Src,
584 IN UINT8 PrefixLength
585 )
586 {
587 UINT8 Byte;
588 UINT8 Bit;
589 UINT8 Mask;
590
591 ASSERT (Dest != NULL && Src != NULL);
592 ASSERT (PrefixLength < IP6_PREFIX_NUM);
593
594 Byte = (UINT8) (PrefixLength / 8);
595 Bit = (UINT8) (PrefixLength % 8);
596
597 ZeroMem (Dest, sizeof (EFI_IPv6_ADDRESS));
598
599 CopyMem (Dest, Src, Byte);
600
601 if (Bit > 0) {
602 Mask = (UINT8) (0xFF << (8 - Bit));
603 ASSERT (Byte < 16);
604 Dest->Addr[Byte] = (UINT8) (Src->Addr[Byte] & Mask);
605 }
606 }
607
608 /**
609 Get the MAC address for a multicast IP address. Call
610 Mnp's McastIpToMac to find the MAC address instead of
611 hard-coding the NIC to be Ethernet.
612
613 @param[in] Mnp The Mnp instance to get the MAC address.
614 @param[in] Multicast The multicast IP address to translate.
615 @param[out] Mac The buffer to hold the translated address.
616
617 @retval EFI_SUCCESS The multicast IP successfully
618 translated to a multicast MAC address.
619 @retval Other The address is not converted because an error occurred.
620
621 **/
622 EFI_STATUS
623 Ip6GetMulticastMac (
624 IN EFI_MANAGED_NETWORK_PROTOCOL *Mnp,
625 IN EFI_IPv6_ADDRESS *Multicast,
626 OUT EFI_MAC_ADDRESS *Mac
627 )
628 {
629 EFI_IP_ADDRESS EfiIp;
630
631 IP6_COPY_ADDRESS (&EfiIp.v6, Multicast);
632
633 return Mnp->McastIpToMac (Mnp, TRUE, &EfiIp, Mac);
634 }
635
636 /**
637 Set the Ip6 variable data.
638
639 @param[in] IpSb Points to an IP6 service binding instance.
640
641 @retval EFI_OUT_OF_RESOURCES There are not enough resources to set the variable.
642 @retval other Set variable failed.
643
644 **/
645 EFI_STATUS
646 Ip6SetVariableData (
647 IN IP6_SERVICE *IpSb
648 )
649 {
650 UINT32 NumConfiguredInstance;
651 LIST_ENTRY *Entry;
652 UINTN VariableDataSize;
653 EFI_IP6_VARIABLE_DATA *Ip6VariableData;
654 EFI_IP6_ADDRESS_PAIR *Ip6AddressPair;
655 IP6_PROTOCOL *IpInstance;
656 CHAR16 *NewMacString;
657 EFI_STATUS Status;
658
659 NumConfiguredInstance = 0;
660
661 //
662 // Go through the children list to count the configured children.
663 //
664 NET_LIST_FOR_EACH (Entry, &IpSb->Children) {
665 IpInstance = NET_LIST_USER_STRUCT_S (Entry, IP6_PROTOCOL, Link, IP6_PROTOCOL_SIGNATURE);
666
667 if (IpInstance->State == IP6_STATE_CONFIGED) {
668 NumConfiguredInstance++;
669 }
670 }
671
672 //
673 // Calculate the size of the Ip6VariableData. As there may be no IP child,
674 // we should add extra buffer for the address paris only if the number of configured
675 // children is more than 1.
676 //
677 VariableDataSize = sizeof (EFI_IP6_VARIABLE_DATA);
678
679 if (NumConfiguredInstance > 1) {
680 VariableDataSize += sizeof (EFI_IP6_ADDRESS_PAIR) * (NumConfiguredInstance - 1);
681 }
682
683 Ip6VariableData = AllocatePool (VariableDataSize);
684 if (Ip6VariableData == NULL) {
685 return EFI_OUT_OF_RESOURCES;
686 }
687
688 Ip6VariableData->DriverHandle = IpSb->Image;
689 Ip6VariableData->AddressCount = NumConfiguredInstance;
690
691 Ip6AddressPair = &Ip6VariableData->AddressPairs[0];
692
693 //
694 // Go through the children list to fill the configured children's address pairs.
695 //
696 NET_LIST_FOR_EACH (Entry, &IpSb->Children) {
697 IpInstance = NET_LIST_USER_STRUCT_S (Entry, IP6_PROTOCOL, Link, IP6_PROTOCOL_SIGNATURE);
698
699 if (IpInstance->State == IP6_STATE_CONFIGED) {
700 Ip6AddressPair->InstanceHandle = IpInstance->Handle;
701 Ip6AddressPair->PrefixLength = IpInstance->PrefixLength;
702 IP6_COPY_ADDRESS (&Ip6AddressPair->Ip6Address, &IpInstance->ConfigData.StationAddress);
703
704 Ip6AddressPair++;
705 }
706 }
707
708 //
709 // Get the mac string.
710 //
711 Status = NetLibGetMacString (IpSb->Controller, IpSb->Image, &NewMacString);
712 if (EFI_ERROR (Status)) {
713 goto Exit;
714 }
715
716 if (IpSb->MacString != NULL) {
717 //
718 // The variable is set already, we're going to update it.
719 //
720 if (StrCmp (IpSb->MacString, NewMacString) != 0) {
721 //
722 // The mac address is changed, delete the previous variable first.
723 //
724 gRT->SetVariable (
725 IpSb->MacString,
726 &gEfiIp6ServiceBindingProtocolGuid,
727 EFI_VARIABLE_BOOTSERVICE_ACCESS,
728 0,
729 NULL
730 );
731 }
732
733 FreePool (IpSb->MacString);
734 }
735
736 IpSb->MacString = NewMacString;
737
738 Status = gRT->SetVariable (
739 IpSb->MacString,
740 &gEfiIp6ServiceBindingProtocolGuid,
741 EFI_VARIABLE_BOOTSERVICE_ACCESS,
742 VariableDataSize,
743 (VOID *) Ip6VariableData
744 );
745
746 Exit:
747 FreePool (Ip6VariableData);
748 return Status;
749 }
750
751 /**
752 Clear the variable and free the resource.
753
754 @param[in] IpSb Ip6 service binding instance.
755
756 **/
757 VOID
758 Ip6ClearVariableData (
759 IN IP6_SERVICE *IpSb
760 )
761 {
762 ASSERT (IpSb->MacString != NULL);
763
764 gRT->SetVariable (
765 IpSb->MacString,
766 &gEfiIp6ServiceBindingProtocolGuid,
767 EFI_VARIABLE_BOOTSERVICE_ACCESS,
768 0,
769 NULL
770 );
771
772 FreePool (IpSb->MacString);
773 IpSb->MacString = NULL;
774 }
775
776 /**
777 Convert the multibyte field in IP header's byter order.
778 In spite of its name, it can also be used to convert from
779 host to network byte order.
780
781 @param[in, out] Head The IP head to convert.
782
783 @return Point to the converted IP head.
784
785 **/
786 EFI_IP6_HEADER *
787 Ip6NtohHead (
788 IN OUT EFI_IP6_HEADER *Head
789 )
790 {
791 Head->FlowLabelL = NTOHS (Head->FlowLabelL);
792 Head->PayloadLength = NTOHS (Head->PayloadLength);
793
794 return Head;
795 }
796