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