• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /** @file
2   The implementation of common functions shared by IP6 driver.
3 
4   Copyright (c) 2009 - 2016, 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
Ip6BuildEfiAddressList(IN IP6_SERVICE * IpSb,OUT UINT32 * AddressCount,OUT EFI_IP6_ADDRESS_INFO ** AddressList OPTIONAL)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
Ip6SetToAllNodeMulticast(IN BOOLEAN Router,IN UINT8 Scope,OUT EFI_IPv6_ADDRESS * Ip6Addr)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 *
Ip6CreateInterfaceID(IN OUT IP6_SERVICE * IpSb)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 *
Ip6CreateLinkLocalAddr(IN OUT IP6_SERVICE * IpSb)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
Ip6CreateSNMulticastAddr(IN EFI_IPv6_ADDRESS * Ip6Addr,OUT EFI_IPv6_ADDRESS * MulticastAddr)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
Ip6AddAddr(IN OUT IP6_INTERFACE * IpIf,IN IP6_ADDRESS_INFO * AddrInfo)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 EFIAPI
Ip6DestroyChildEntryByAddr(IN LIST_ENTRY * Entry,IN VOID * Context)343 Ip6DestroyChildEntryByAddr (
344   IN LIST_ENTRY         *Entry,
345   IN VOID               *Context
346   )
347 {
348   IP6_PROTOCOL                  *Instance;
349   EFI_SERVICE_BINDING_PROTOCOL  *ServiceBinding;
350   EFI_IPv6_ADDRESS              *Address;
351 
352   Instance = NET_LIST_USER_STRUCT_S (Entry, IP6_PROTOCOL, Link, IP6_PROTOCOL_SIGNATURE);
353   ServiceBinding = ((IP6_DESTROY_CHILD_BY_ADDR_CALLBACK_CONTEXT*) Context)->ServiceBinding;
354   Address = ((IP6_DESTROY_CHILD_BY_ADDR_CALLBACK_CONTEXT*) Context)->Address;
355 
356   if ((Instance->State == IP6_STATE_CONFIGED) && EFI_IP6_EQUAL (&Instance->ConfigData.StationAddress, Address)) {
357     return ServiceBinding->DestroyChild (ServiceBinding, Instance->Handle);
358   }
359 
360   return EFI_SUCCESS;
361 }
362 
363 /**
364   Destroy the IP instance if its StationAddress is removed. It is the help function
365   for Ip6RemoveAddr().
366 
367   @param[in, out]  IpSb             Points to an IP6 service binding instance.
368   @param[in]       Address          The to be removed address
369 
370 **/
371 VOID
Ip6DestroyInstanceByAddress(IN OUT IP6_SERVICE * IpSb,IN EFI_IPv6_ADDRESS * Address)372 Ip6DestroyInstanceByAddress (
373   IN OUT IP6_SERVICE   *IpSb,
374   IN EFI_IPv6_ADDRESS  *Address
375   )
376 {
377   LIST_ENTRY                    *List;
378   IP6_DESTROY_CHILD_BY_ADDR_CALLBACK_CONTEXT  Context;
379 
380   NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE);
381 
382   List = &IpSb->Children;
383   Context.ServiceBinding = &IpSb->ServiceBinding;
384   Context.Address = Address;
385   NetDestroyLinkList (
386     List,
387     Ip6DestroyChildEntryByAddr,
388     &Context,
389     NULL
390     );
391 }
392 
393 /**
394   Remove the IPv6 address from the address list node points to IP6_ADDRESS_INFO.
395 
396   This function removes the matching IPv6 addresses from the address list and
397   adjusts the address count of the address list. If IpSb is not NULL, this function
398   calls Ip6LeaveGroup to see whether it should call Mnp->Groups() to remove the
399   its solicited-node multicast MAC address from the filter list and sends out
400   a Multicast Listener Done. If Prefix is NULL, all address in the address list
401   will be removed. If Prefix is not NULL, the address that matching the Prefix
402   with PrefixLength in the address list will be removed.
403 
404   @param[in]       IpSb             NULL or points to IP6 service binding instance.
405   @param[in, out]  AddressList      Address list array.
406   @param[in, out]  AddressCount     The count of addresses in address list array.
407   @param[in]       Prefix           NULL or an IPv6 address prefix.
408   @param[in]       PrefixLength     The length of Prefix.
409 
410   @retval    EFI_SUCCESS            The operation completed successfully.
411   @retval    EFI_NOT_FOUND          The address matching the Prefix with PrefixLength
412                                     cannot be found in the address list.
413   @retval    EFI_INVALID_PARAMETER  Any input parameter is invalid.
414 
415 **/
416 EFI_STATUS
Ip6RemoveAddr(IN IP6_SERVICE * IpSb OPTIONAL,IN OUT LIST_ENTRY * AddressList,IN OUT UINT32 * AddressCount,IN EFI_IPv6_ADDRESS * Prefix OPTIONAL,IN UINT8 PrefixLength)417 Ip6RemoveAddr (
418   IN IP6_SERVICE       *IpSb          OPTIONAL,
419   IN OUT LIST_ENTRY    *AddressList,
420   IN OUT UINT32        *AddressCount,
421   IN EFI_IPv6_ADDRESS  *Prefix        OPTIONAL,
422   IN UINT8             PrefixLength
423   )
424 {
425   EFI_STATUS           Status;
426   LIST_ENTRY           *Entry;
427   LIST_ENTRY           *Next;
428   IP6_ADDRESS_INFO     *AddrInfo;
429   EFI_IPv6_ADDRESS     SnMCastAddr;
430 
431   if (IsListEmpty (AddressList) || *AddressCount < 1 || PrefixLength > IP6_PREFIX_MAX) {
432     return EFI_INVALID_PARAMETER;
433   }
434 
435   Status = EFI_NOT_FOUND;
436 
437   NET_LIST_FOR_EACH_SAFE (Entry, Next, AddressList) {
438     AddrInfo = NET_LIST_USER_STRUCT_S (Entry, IP6_ADDRESS_INFO, Link, IP6_ADDR_INFO_SIGNATURE);
439 
440     if (Prefix == NULL ||
441         (PrefixLength == 128 && EFI_IP6_EQUAL (Prefix, &AddrInfo->Address)) ||
442         (PrefixLength == AddrInfo->PrefixLength && NetIp6IsNetEqual (Prefix, &AddrInfo->Address, PrefixLength))
443         ) {
444       if (IpSb != NULL) {
445         NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE);
446         Ip6CreateSNMulticastAddr (&AddrInfo->Address, &SnMCastAddr);
447         Ip6LeaveGroup (IpSb, &SnMCastAddr);
448 
449         //
450         // Destroy any instance who is using the dying address as the source address.
451         //
452         Ip6DestroyInstanceByAddress (IpSb, &AddrInfo->Address);
453       }
454 
455       RemoveEntryList (Entry);
456       FreePool (AddrInfo);
457       (*AddressCount)--;
458 
459       Status = EFI_SUCCESS;
460     }
461   }
462 
463   return Status;
464 }
465 
466 /**
467   Check whether the incoming Ipv6 address is a solicited-node multicast address.
468 
469   @param[in]  Ip6               Ip6 address, in network order.
470 
471   @retval TRUE                  Yes, solicited-node multicast address
472   @retval FALSE                 No
473 
474 **/
475 BOOLEAN
Ip6IsSNMulticastAddr(IN EFI_IPv6_ADDRESS * Ip6)476 Ip6IsSNMulticastAddr (
477   IN EFI_IPv6_ADDRESS *Ip6
478   )
479 {
480   EFI_IPv6_ADDRESS    Sn;
481   BOOLEAN             Flag;
482 
483   Ip6CreateSNMulticastAddr (Ip6, &Sn);
484   Flag = FALSE;
485 
486   if (CompareMem (Sn.Addr, Ip6->Addr, 13) == 0) {
487     Flag = TRUE;
488   }
489 
490   return Flag;
491 }
492 
493 /**
494   Check whether the incoming IPv6 address is one of the maintained addresses in
495   the IP6 service binding instance.
496 
497   @param[in]  IpSb              Points to a IP6 service binding instance.
498   @param[in]  Address           The IP6 address to be checked.
499   @param[out] Interface         If not NULL, output the IP6 interface which
500                                 maintains the Address.
501   @param[out] AddressInfo       If not NULL, output the IP6 address information
502                                 of the Address.
503 
504   @retval TRUE                  Yes, it is one of the maintained address.
505   @retval FALSE                 No, it is not one of the maintained address.
506 
507 **/
508 BOOLEAN
Ip6IsOneOfSetAddress(IN IP6_SERVICE * IpSb,IN EFI_IPv6_ADDRESS * Address,OUT IP6_INTERFACE ** Interface OPTIONAL,OUT IP6_ADDRESS_INFO ** AddressInfo OPTIONAL)509 Ip6IsOneOfSetAddress (
510   IN  IP6_SERVICE           *IpSb,
511   IN  EFI_IPv6_ADDRESS      *Address,
512   OUT IP6_INTERFACE         **Interface   OPTIONAL,
513   OUT IP6_ADDRESS_INFO      **AddressInfo OPTIONAL
514   )
515 {
516   LIST_ENTRY                *Entry;
517   LIST_ENTRY                *Entry2;
518   IP6_INTERFACE             *IpIf;
519   IP6_ADDRESS_INFO          *TmpAddressInfo;
520 
521   //
522   // Check link-local address first
523   //
524   if (IpSb->LinkLocalOk && EFI_IP6_EQUAL (&IpSb->LinkLocalAddr, Address)) {
525     if (Interface != NULL) {
526       *Interface = IpSb->DefaultInterface;
527     }
528 
529     if (AddressInfo != NULL) {
530       *AddressInfo = NULL;
531     }
532 
533     return TRUE;
534   }
535 
536   NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) {
537     IpIf = NET_LIST_USER_STRUCT_S (Entry, IP6_INTERFACE, Link, IP6_INTERFACE_SIGNATURE);
538 
539     NET_LIST_FOR_EACH (Entry2, &IpIf->AddressList) {
540       TmpAddressInfo = NET_LIST_USER_STRUCT_S (Entry2, IP6_ADDRESS_INFO, Link, IP6_ADDR_INFO_SIGNATURE);
541 
542       if (EFI_IP6_EQUAL (&TmpAddressInfo->Address, Address)) {
543         if (Interface != NULL) {
544           *Interface = IpIf;
545         }
546 
547         if (AddressInfo != NULL) {
548           *AddressInfo = TmpAddressInfo;
549         }
550 
551         return TRUE;
552       }
553     }
554   }
555 
556   return FALSE;
557 }
558 
559 /**
560   Check whether the incoming MAC address is valid.
561 
562   @param[in]  IpSb              Points to a IP6 service binding instance.
563   @param[in]  LinkAddress       The MAC address.
564 
565   @retval TRUE                  Yes, it is valid.
566   @retval FALSE                 No, it is not valid.
567 
568 **/
569 BOOLEAN
Ip6IsValidLinkAddress(IN IP6_SERVICE * IpSb,IN EFI_MAC_ADDRESS * LinkAddress)570 Ip6IsValidLinkAddress (
571   IN  IP6_SERVICE      *IpSb,
572   IN  EFI_MAC_ADDRESS  *LinkAddress
573   )
574 {
575   UINT32               Index;
576 
577   //
578   // TODO: might be updated later to be more acceptable.
579   //
580   for (Index = IpSb->SnpMode.HwAddressSize; Index < sizeof (EFI_MAC_ADDRESS); Index++) {
581     if (LinkAddress->Addr[Index] != 0) {
582       return FALSE;
583     }
584   }
585 
586   return TRUE;
587 }
588 
589 /**
590   Copy the PrefixLength bits from Src to Dest.
591 
592   @param[out] Dest              A pointer to the buffer to copy to.
593   @param[in]  Src               A pointer to the buffer to copy from.
594   @param[in]  PrefixLength      The number of bits to copy.
595 
596 **/
597 VOID
Ip6CopyAddressByPrefix(OUT EFI_IPv6_ADDRESS * Dest,IN EFI_IPv6_ADDRESS * Src,IN UINT8 PrefixLength)598 Ip6CopyAddressByPrefix (
599   OUT EFI_IPv6_ADDRESS *Dest,
600   IN  EFI_IPv6_ADDRESS *Src,
601   IN  UINT8            PrefixLength
602   )
603 {
604   UINT8 Byte;
605   UINT8 Bit;
606   UINT8 Mask;
607 
608   ASSERT (Dest != NULL && Src != NULL);
609   ASSERT (PrefixLength <= IP6_PREFIX_MAX);
610 
611   Byte = (UINT8) (PrefixLength / 8);
612   Bit  = (UINT8) (PrefixLength % 8);
613 
614   ZeroMem (Dest, sizeof (EFI_IPv6_ADDRESS));
615 
616   CopyMem (Dest, Src, Byte);
617 
618   if (Bit > 0) {
619     Mask = (UINT8) (0xFF << (8 - Bit));
620     ASSERT (Byte < 16);
621     Dest->Addr[Byte] = (UINT8) (Src->Addr[Byte] & Mask);
622   }
623 }
624 
625 /**
626   Get the MAC address for a multicast IP address. Call
627   Mnp's McastIpToMac to find the MAC address instead of
628   hard-coding the NIC to be Ethernet.
629 
630   @param[in]  Mnp                   The Mnp instance to get the MAC address.
631   @param[in]  Multicast             The multicast IP address to translate.
632   @param[out] Mac                   The buffer to hold the translated address.
633 
634   @retval EFI_SUCCESS               The multicast IP successfully
635                                     translated to a multicast MAC address.
636   @retval Other                     The address is not converted because an error occurred.
637 
638 **/
639 EFI_STATUS
Ip6GetMulticastMac(IN EFI_MANAGED_NETWORK_PROTOCOL * Mnp,IN EFI_IPv6_ADDRESS * Multicast,OUT EFI_MAC_ADDRESS * Mac)640 Ip6GetMulticastMac (
641   IN  EFI_MANAGED_NETWORK_PROTOCOL *Mnp,
642   IN  EFI_IPv6_ADDRESS             *Multicast,
643   OUT EFI_MAC_ADDRESS              *Mac
644   )
645 {
646   EFI_IP_ADDRESS        EfiIp;
647 
648   IP6_COPY_ADDRESS (&EfiIp.v6, Multicast);
649 
650   return Mnp->McastIpToMac (Mnp, TRUE, &EfiIp, Mac);
651 }
652 
653 /**
654   Convert the multibyte field in IP header's byter order.
655   In spite of its name, it can also be used to convert from
656   host to network byte order.
657 
658   @param[in, out]  Head                  The IP head to convert.
659 
660   @return Point to the converted IP head.
661 
662 **/
663 EFI_IP6_HEADER *
Ip6NtohHead(IN OUT EFI_IP6_HEADER * Head)664 Ip6NtohHead (
665   IN OUT EFI_IP6_HEADER *Head
666   )
667 {
668   Head->FlowLabelL    = NTOHS (Head->FlowLabelL);
669   Head->PayloadLength = NTOHS (Head->PayloadLength);
670 
671   return Head;
672 }
673 
674