]> git.proxmox.com Git - mirror_edk2.git/blob - NetworkPkg/Ip6Dxe/Ip6Common.c
1. Add EFI_COMPONENT_NAME2_PROTOCOL.GetControllerName() support.
[mirror_edk2.git] / NetworkPkg / Ip6Dxe / Ip6Common.c
1 /** @file
2 The implementation of common functions shared by IP6 driver.
3
4 Copyright (c) 2009 - 2012, 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 Callback function which provided by user to remove one node in NetDestroyLinkList process.
333
334 @param[in] Entry The entry to be removed.
335 @param[in] Context Pointer to the callback context corresponds to the Context in NetDestroyLinkList.
336
337 @retval EFI_SUCCESS The entry has been removed successfully.
338 @retval Others Fail to remove the entry.
339
340 **/
341 EFI_STATUS
342 Ip6DestroyChildEntryByAddr (
343 IN LIST_ENTRY *Entry,
344 IN VOID *Context
345 )
346 {
347 IP6_PROTOCOL *Instance;
348 EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding;
349 EFI_IPv6_ADDRESS *Address;
350
351 Instance = NET_LIST_USER_STRUCT_S (Entry, IP6_PROTOCOL, Link, IP6_PROTOCOL_SIGNATURE);
352 ServiceBinding = ((IP6_DESTROY_CHILD_BY_ADDR_CALLBACK_CONTEXT*) Context)->ServiceBinding;
353 Address = ((IP6_DESTROY_CHILD_BY_ADDR_CALLBACK_CONTEXT*) Context)->Address;
354
355 if ((Instance->State == IP6_STATE_CONFIGED) && EFI_IP6_EQUAL (&Instance->ConfigData.StationAddress, Address)) {
356 return ServiceBinding->DestroyChild (ServiceBinding, Instance->Handle);
357 }
358
359 return EFI_SUCCESS;
360 }
361
362 /**
363 Destroy the IP instance if its StationAddress is removed. It is the help function
364 for Ip6RemoveAddr().
365
366 @param[in, out] IpSb Points to an IP6 service binding instance.
367 @param[in] Address The to be removed address
368
369 **/
370 VOID
371 Ip6DestroyInstanceByAddress (
372 IN OUT IP6_SERVICE *IpSb,
373 IN EFI_IPv6_ADDRESS *Address
374 )
375 {
376 LIST_ENTRY *List;
377 IP6_DESTROY_CHILD_BY_ADDR_CALLBACK_CONTEXT Context;
378
379 NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE);
380
381 List = &IpSb->Children;
382 Context.ServiceBinding = &IpSb->ServiceBinding;
383 Context.Address = Address;
384 NetDestroyLinkList (
385 List,
386 Ip6DestroyChildEntryByAddr,
387 &Context,
388 NULL
389 );
390 }
391
392 /**
393 Remove the IPv6 address from the address list node points to IP6_ADDRESS_INFO.
394
395 This function removes the matching IPv6 addresses from the address list and
396 adjusts the address count of the address list. If IpSb is not NULL, this function
397 calls Ip6LeaveGroup to see whether it should call Mnp->Groups() to remove the
398 its solicited-node multicast MAC address from the filter list and sends out
399 a Multicast Listener Done. If Prefix is NULL, all address in the address list
400 will be removed. If Prefix is not NULL, the address that matching the Prefix
401 with PrefixLength in the address list will be removed.
402
403 @param[in] IpSb NULL or points to IP6 service binding instance.
404 @param[in, out] AddressList Address list array.
405 @param[in, out] AddressCount The count of addresses in address list array.
406 @param[in] Prefix NULL or an IPv6 address prefix.
407 @param[in] PrefixLength The length of Prefix.
408
409 @retval EFI_SUCCESS The operation completed successfully.
410 @retval EFI_NOT_FOUND The address matching the Prefix with PrefixLength
411 cannot be found in the address list.
412 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
413
414 **/
415 EFI_STATUS
416 Ip6RemoveAddr (
417 IN IP6_SERVICE *IpSb OPTIONAL,
418 IN OUT LIST_ENTRY *AddressList,
419 IN OUT UINT32 *AddressCount,
420 IN EFI_IPv6_ADDRESS *Prefix OPTIONAL,
421 IN UINT8 PrefixLength
422 )
423 {
424 EFI_STATUS Status;
425 LIST_ENTRY *Entry;
426 LIST_ENTRY *Next;
427 IP6_ADDRESS_INFO *AddrInfo;
428 EFI_IPv6_ADDRESS SnMCastAddr;
429
430 if (IsListEmpty (AddressList) || *AddressCount < 1 || PrefixLength > IP6_PREFIX_NUM) {
431 return EFI_INVALID_PARAMETER;
432 }
433
434 Status = EFI_NOT_FOUND;
435
436 NET_LIST_FOR_EACH_SAFE (Entry, Next, AddressList) {
437 AddrInfo = NET_LIST_USER_STRUCT_S (Entry, IP6_ADDRESS_INFO, Link, IP6_ADDR_INFO_SIGNATURE);
438
439 if (Prefix == NULL ||
440 (PrefixLength == 128 && EFI_IP6_EQUAL (Prefix, &AddrInfo->Address)) ||
441 (PrefixLength == AddrInfo->PrefixLength && NetIp6IsNetEqual (Prefix, &AddrInfo->Address, PrefixLength))
442 ) {
443 if (IpSb != NULL) {
444 NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE);
445 Ip6CreateSNMulticastAddr (&AddrInfo->Address, &SnMCastAddr);
446 Ip6LeaveGroup (IpSb, &SnMCastAddr);
447
448 //
449 // Destroy any instance who is using the dying address as the source address.
450 //
451 Ip6DestroyInstanceByAddress (IpSb, &AddrInfo->Address);
452 }
453
454 RemoveEntryList (Entry);
455 FreePool (AddrInfo);
456 (*AddressCount)--;
457
458 Status = EFI_SUCCESS;
459 }
460 }
461
462 return Status;
463 }
464
465 /**
466 Check whether the incoming Ipv6 address is a solicited-node multicast address.
467
468 @param[in] Ip6 Ip6 address, in network order.
469
470 @retval TRUE Yes, solicited-node multicast address
471 @retval FALSE No
472
473 **/
474 BOOLEAN
475 Ip6IsSNMulticastAddr (
476 IN EFI_IPv6_ADDRESS *Ip6
477 )
478 {
479 EFI_IPv6_ADDRESS Sn;
480 BOOLEAN Flag;
481
482 Ip6CreateSNMulticastAddr (Ip6, &Sn);
483 Flag = FALSE;
484
485 if (CompareMem (Sn.Addr, Ip6->Addr, 13) == 0) {
486 Flag = TRUE;
487 }
488
489 return Flag;
490 }
491
492 /**
493 Check whether the incoming IPv6 address is one of the maintained addresses in
494 the IP6 service binding instance.
495
496 @param[in] IpSb Points to a IP6 service binding instance.
497 @param[in] Address The IP6 address to be checked.
498 @param[out] Interface If not NULL, output the IP6 interface which
499 maintains the Address.
500 @param[out] AddressInfo If not NULL, output the IP6 address information
501 of the Address.
502
503 @retval TRUE Yes, it is one of the maintained address.
504 @retval FALSE No, it is not one of the maintained address.
505
506 **/
507 BOOLEAN
508 Ip6IsOneOfSetAddress (
509 IN IP6_SERVICE *IpSb,
510 IN EFI_IPv6_ADDRESS *Address,
511 OUT IP6_INTERFACE **Interface OPTIONAL,
512 OUT IP6_ADDRESS_INFO **AddressInfo OPTIONAL
513 )
514 {
515 LIST_ENTRY *Entry;
516 LIST_ENTRY *Entry2;
517 IP6_INTERFACE *IpIf;
518 IP6_ADDRESS_INFO *TmpAddressInfo;
519
520 //
521 // Check link-local address first
522 //
523 if (IpSb->LinkLocalOk && EFI_IP6_EQUAL (&IpSb->LinkLocalAddr, Address)) {
524 if (Interface != NULL) {
525 *Interface = IpSb->DefaultInterface;
526 }
527
528 if (AddressInfo != NULL) {
529 *AddressInfo = NULL;
530 }
531
532 return TRUE;
533 }
534
535 NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) {
536 IpIf = NET_LIST_USER_STRUCT_S (Entry, IP6_INTERFACE, Link, IP6_INTERFACE_SIGNATURE);
537
538 NET_LIST_FOR_EACH (Entry2, &IpIf->AddressList) {
539 TmpAddressInfo = NET_LIST_USER_STRUCT_S (Entry2, IP6_ADDRESS_INFO, Link, IP6_ADDR_INFO_SIGNATURE);
540
541 if (EFI_IP6_EQUAL (&TmpAddressInfo->Address, Address)) {
542 if (Interface != NULL) {
543 *Interface = IpIf;
544 }
545
546 if (AddressInfo != NULL) {
547 *AddressInfo = TmpAddressInfo;
548 }
549
550 return TRUE;
551 }
552 }
553 }
554
555 return FALSE;
556 }
557
558 /**
559 Check whether the incoming MAC address is valid.
560
561 @param[in] IpSb Points to a IP6 service binding instance.
562 @param[in] LinkAddress The MAC address.
563
564 @retval TRUE Yes, it is valid.
565 @retval FALSE No, it is not valid.
566
567 **/
568 BOOLEAN
569 Ip6IsValidLinkAddress (
570 IN IP6_SERVICE *IpSb,
571 IN EFI_MAC_ADDRESS *LinkAddress
572 )
573 {
574 UINT32 Index;
575
576 //
577 // TODO: might be updated later to be more acceptable.
578 //
579 for (Index = IpSb->SnpMode.HwAddressSize; Index < sizeof (EFI_MAC_ADDRESS); Index++) {
580 if (LinkAddress->Addr[Index] != 0) {
581 return FALSE;
582 }
583 }
584
585 return TRUE;
586 }
587
588 /**
589 Copy the PrefixLength bits from Src to Dest.
590
591 @param[out] Dest A pointer to the buffer to copy to.
592 @param[in] Src A pointer to the buffer to copy from.
593 @param[in] PrefixLength The number of bits to copy.
594
595 **/
596 VOID
597 Ip6CopyAddressByPrefix (
598 OUT EFI_IPv6_ADDRESS *Dest,
599 IN EFI_IPv6_ADDRESS *Src,
600 IN UINT8 PrefixLength
601 )
602 {
603 UINT8 Byte;
604 UINT8 Bit;
605 UINT8 Mask;
606
607 ASSERT (Dest != NULL && Src != NULL);
608 ASSERT (PrefixLength < IP6_PREFIX_NUM);
609
610 Byte = (UINT8) (PrefixLength / 8);
611 Bit = (UINT8) (PrefixLength % 8);
612
613 ZeroMem (Dest, sizeof (EFI_IPv6_ADDRESS));
614
615 CopyMem (Dest, Src, Byte);
616
617 if (Bit > 0) {
618 Mask = (UINT8) (0xFF << (8 - Bit));
619 ASSERT (Byte < 16);
620 Dest->Addr[Byte] = (UINT8) (Src->Addr[Byte] & Mask);
621 }
622 }
623
624 /**
625 Get the MAC address for a multicast IP address. Call
626 Mnp's McastIpToMac to find the MAC address instead of
627 hard-coding the NIC to be Ethernet.
628
629 @param[in] Mnp The Mnp instance to get the MAC address.
630 @param[in] Multicast The multicast IP address to translate.
631 @param[out] Mac The buffer to hold the translated address.
632
633 @retval EFI_SUCCESS The multicast IP successfully
634 translated to a multicast MAC address.
635 @retval Other The address is not converted because an error occurred.
636
637 **/
638 EFI_STATUS
639 Ip6GetMulticastMac (
640 IN EFI_MANAGED_NETWORK_PROTOCOL *Mnp,
641 IN EFI_IPv6_ADDRESS *Multicast,
642 OUT EFI_MAC_ADDRESS *Mac
643 )
644 {
645 EFI_IP_ADDRESS EfiIp;
646
647 IP6_COPY_ADDRESS (&EfiIp.v6, Multicast);
648
649 return Mnp->McastIpToMac (Mnp, TRUE, &EfiIp, Mac);
650 }
651
652 /**
653 Set the Ip6 variable data.
654
655 @param[in] IpSb Points to an IP6 service binding instance.
656
657 @retval EFI_OUT_OF_RESOURCES There are not enough resources to set the variable.
658 @retval other Set variable failed.
659
660 **/
661 EFI_STATUS
662 Ip6SetVariableData (
663 IN IP6_SERVICE *IpSb
664 )
665 {
666 UINT32 NumConfiguredInstance;
667 LIST_ENTRY *Entry;
668 UINTN VariableDataSize;
669 EFI_IP6_VARIABLE_DATA *Ip6VariableData;
670 EFI_IP6_ADDRESS_PAIR *Ip6AddressPair;
671 IP6_PROTOCOL *IpInstance;
672 CHAR16 *NewMacString;
673 EFI_STATUS Status;
674
675 NumConfiguredInstance = 0;
676
677 //
678 // Go through the children list to count the configured children.
679 //
680 NET_LIST_FOR_EACH (Entry, &IpSb->Children) {
681 IpInstance = NET_LIST_USER_STRUCT_S (Entry, IP6_PROTOCOL, Link, IP6_PROTOCOL_SIGNATURE);
682
683 if (IpInstance->State == IP6_STATE_CONFIGED) {
684 NumConfiguredInstance++;
685 }
686 }
687
688 //
689 // Calculate the size of the Ip6VariableData. As there may be no IP child,
690 // we should add extra buffer for the address paris only if the number of configured
691 // children is more than 1.
692 //
693 VariableDataSize = sizeof (EFI_IP6_VARIABLE_DATA);
694
695 if (NumConfiguredInstance > 1) {
696 VariableDataSize += sizeof (EFI_IP6_ADDRESS_PAIR) * (NumConfiguredInstance - 1);
697 }
698
699 Ip6VariableData = AllocatePool (VariableDataSize);
700 if (Ip6VariableData == NULL) {
701 return EFI_OUT_OF_RESOURCES;
702 }
703
704 Ip6VariableData->DriverHandle = IpSb->Image;
705 Ip6VariableData->AddressCount = NumConfiguredInstance;
706
707 Ip6AddressPair = &Ip6VariableData->AddressPairs[0];
708
709 //
710 // Go through the children list to fill the configured children's address pairs.
711 //
712 NET_LIST_FOR_EACH (Entry, &IpSb->Children) {
713 IpInstance = NET_LIST_USER_STRUCT_S (Entry, IP6_PROTOCOL, Link, IP6_PROTOCOL_SIGNATURE);
714
715 if (IpInstance->State == IP6_STATE_CONFIGED) {
716 Ip6AddressPair->InstanceHandle = IpInstance->Handle;
717 Ip6AddressPair->PrefixLength = IpInstance->PrefixLength;
718 IP6_COPY_ADDRESS (&Ip6AddressPair->Ip6Address, &IpInstance->ConfigData.StationAddress);
719
720 Ip6AddressPair++;
721 }
722 }
723
724 //
725 // Get the mac string.
726 //
727 Status = NetLibGetMacString (IpSb->Controller, IpSb->Image, &NewMacString);
728 if (EFI_ERROR (Status)) {
729 goto Exit;
730 }
731
732 if (IpSb->MacString != NULL) {
733 //
734 // The variable is set already, we're going to update it.
735 //
736 if (StrCmp (IpSb->MacString, NewMacString) != 0) {
737 //
738 // The mac address is changed, delete the previous variable first.
739 //
740 gRT->SetVariable (
741 IpSb->MacString,
742 &gEfiIp6ServiceBindingProtocolGuid,
743 EFI_VARIABLE_BOOTSERVICE_ACCESS,
744 0,
745 NULL
746 );
747 }
748
749 FreePool (IpSb->MacString);
750 }
751
752 IpSb->MacString = NewMacString;
753
754 Status = gRT->SetVariable (
755 IpSb->MacString,
756 &gEfiIp6ServiceBindingProtocolGuid,
757 EFI_VARIABLE_BOOTSERVICE_ACCESS,
758 VariableDataSize,
759 (VOID *) Ip6VariableData
760 );
761
762 Exit:
763 FreePool (Ip6VariableData);
764 return Status;
765 }
766
767 /**
768 Clear the variable and free the resource.
769
770 @param[in] IpSb Ip6 service binding instance.
771
772 **/
773 VOID
774 Ip6ClearVariableData (
775 IN IP6_SERVICE *IpSb
776 )
777 {
778 ASSERT (IpSb->MacString != NULL);
779
780 gRT->SetVariable (
781 IpSb->MacString,
782 &gEfiIp6ServiceBindingProtocolGuid,
783 EFI_VARIABLE_BOOTSERVICE_ACCESS,
784 0,
785 NULL
786 );
787
788 FreePool (IpSb->MacString);
789 IpSb->MacString = NULL;
790 }
791
792 /**
793 Convert the multibyte field in IP header's byter order.
794 In spite of its name, it can also be used to convert from
795 host to network byte order.
796
797 @param[in, out] Head The IP head to convert.
798
799 @return Point to the converted IP head.
800
801 **/
802 EFI_IP6_HEADER *
803 Ip6NtohHead (
804 IN OUT EFI_IP6_HEADER *Head
805 )
806 {
807 Head->FlowLabelL = NTOHS (Head->FlowLabelL);
808 Head->PayloadLength = NTOHS (Head->PayloadLength);
809
810 return Head;
811 }
812