VirtualBox

source: vbox/trunk/src/VBox/Devices/EFI/Firmware/NetworkPkg/Ip6Dxe/Ip6Common.c

Last change on this file was 99404, checked in by vboxsync, 21 months ago

Devices/EFI/FirmwareNew: Update to edk2-stable202302 and make it build, bugref:4643

  • Property svn:eol-style set to native
File size: 18.2 KB
Line 
1/** @file
2 The implementation of common functions shared by IP6 driver.
3
4 Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
5
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7
8**/
9
10#include "Ip6Impl.h"
11
12/**
13 Build a array of EFI_IP6_ADDRESS_INFO to be returned to the caller. The number
14 of EFI_IP6_ADDRESS_INFO is also returned. If AddressList is NULL,
15 only the address count is returned.
16
17 @param[in] IpSb The IP6 service binding instance.
18 @param[out] AddressCount The number of returned addresses.
19 @param[out] AddressList The pointer to the array of EFI_IP6_ADDRESS_INFO.
20 This is an optional parameter.
21
22
23 @retval EFI_SUCCESS The address array successfully built.
24 @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the address info.
25 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
26
27**/
28EFI_STATUS
29Ip6BuildEfiAddressList (
30 IN IP6_SERVICE *IpSb,
31 OUT UINT32 *AddressCount,
32 OUT EFI_IP6_ADDRESS_INFO **AddressList OPTIONAL
33 )
34{
35 UINT32 Count;
36 LIST_ENTRY *Entry;
37 EFI_IP6_ADDRESS_INFO *EfiAddrInfo;
38 IP6_ADDRESS_INFO *AddrInfo;
39
40 if (AddressCount == NULL) {
41 return EFI_INVALID_PARAMETER;
42 }
43
44 if (IpSb->LinkLocalOk) {
45 Count = 1 + IpSb->DefaultInterface->AddressCount;
46 } else {
47 Count = 0;
48 }
49
50 *AddressCount = Count;
51
52 if ((AddressList == NULL) || (Count == 0)) {
53 return EFI_SUCCESS;
54 }
55
56 if (*AddressList == NULL) {
57 *AddressList = AllocatePool (sizeof (EFI_IP6_ADDRESS_INFO) * Count);
58 if (*AddressList == NULL) {
59 return EFI_OUT_OF_RESOURCES;
60 }
61 }
62
63 EfiAddrInfo = *AddressList;
64
65 IP6_COPY_ADDRESS (&EfiAddrInfo->Address, &IpSb->LinkLocalAddr);
66 EfiAddrInfo->PrefixLength = IP6_LINK_LOCAL_PREFIX_LENGTH;
67
68 EfiAddrInfo++;
69 Count = 1;
70
71 NET_LIST_FOR_EACH (Entry, &IpSb->DefaultInterface->AddressList) {
72 AddrInfo = NET_LIST_USER_STRUCT_S (Entry, IP6_ADDRESS_INFO, Link, IP6_ADDR_INFO_SIGNATURE);
73
74 IP6_COPY_ADDRESS (&EfiAddrInfo->Address, &AddrInfo->Address);
75 EfiAddrInfo->PrefixLength = AddrInfo->PrefixLength;
76
77 EfiAddrInfo++;
78 Count++;
79 }
80
81 ASSERT (Count == *AddressCount);
82
83 return EFI_SUCCESS;
84}
85
86/**
87 Generate the multicast addresses identify the group of all IPv6 nodes or IPv6
88 routers defined in RFC4291.
89
90 All Nodes Addresses: FF01::1, FF02::1.
91 All Router Addresses: FF01::2, FF02::2, FF05::2.
92
93 @param[in] Router If TRUE, generate all routers addresses,
94 else generate all node addresses.
95 @param[in] Scope interface-local(1), link-local(2), or site-local(5)
96 @param[out] Ip6Addr The generated multicast address.
97
98 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
99 @retval EFI_SUCCESS The address is generated.
100
101**/
102EFI_STATUS
103Ip6SetToAllNodeMulticast (
104 IN BOOLEAN Router,
105 IN UINT8 Scope,
106 OUT EFI_IPv6_ADDRESS *Ip6Addr
107 )
108{
109 if (Ip6Addr == NULL) {
110 return EFI_INVALID_PARAMETER;
111 }
112
113 if (!Router && (Scope == IP6_SITE_LOCAL_SCOPE)) {
114 return EFI_INVALID_PARAMETER;
115 }
116
117 ZeroMem (Ip6Addr, sizeof (EFI_IPv6_ADDRESS));
118 Ip6Addr->Addr[0] = 0xFF;
119 Ip6Addr->Addr[1] = Scope;
120
121 if (!Router) {
122 Ip6Addr->Addr[15] = 0x1;
123 } else {
124 Ip6Addr->Addr[15] = 0x2;
125 }
126
127 return EFI_SUCCESS;
128}
129
130/**
131 This function converts MAC address to 64 bits interface ID according to RFC4291
132 and returns the interface ID. Currently only 48-bit MAC address is supported by
133 this function.
134
135 @param[in, out] IpSb The IP6 service binding instance.
136
137 @retval NULL The operation fails.
138 @return Pointer to the generated interface ID.
139
140**/
141UINT8 *
142Ip6CreateInterfaceID (
143 IN OUT IP6_SERVICE *IpSb
144 )
145{
146 UINT8 InterfaceId[8];
147 UINT8 Byte;
148 EFI_MAC_ADDRESS *MacAddr;
149 UINT32 AddrLen;
150
151 NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE);
152
153 AddrLen = IpSb->SnpMode.HwAddressSize;
154
155 //
156 // Currently only IEEE 802 48-bit MACs are supported to create link local address.
157 //
158 if ((AddrLen != IP6_MAC_LEN) || (IpSb->InterfaceIdLen != IP6_IF_ID_LEN)) {
159 return NULL;
160 }
161
162 MacAddr = &IpSb->SnpMode.CurrentAddress;
163
164 //
165 // Convert MAC address to 64 bits interface ID according to Appendix A of RFC4291:
166 // 1. Insert 0xFFFE to the middle
167 // 2. Invert the universal/local bit - bit 6 in network order
168 //
169 CopyMem (InterfaceId, MacAddr, 3);
170 InterfaceId[3] = 0xFF;
171 InterfaceId[4] = 0xFE;
172 CopyMem (&InterfaceId[5], &MacAddr->Addr[3], 3);
173
174 Byte = (UINT8)(InterfaceId[0] & IP6_U_BIT);
175 if (Byte == IP6_U_BIT) {
176 InterfaceId[0] &= ~IP6_U_BIT;
177 } else {
178 InterfaceId[0] |= IP6_U_BIT;
179 }
180
181 //
182 // Return the interface ID.
183 //
184 return AllocateCopyPool (IpSb->InterfaceIdLen, InterfaceId);
185}
186
187/**
188 This function creates link-local address from interface identifier. The
189 interface identifier is normally created from MAC address. It might be manually
190 configured by administrator if the link-local address created from MAC address
191 is a duplicate address.
192
193 @param[in, out] IpSb The IP6 service binding instance.
194
195 @retval NULL If the operation fails.
196 @return The generated Link Local address, in network order.
197
198**/
199EFI_IPv6_ADDRESS *
200Ip6CreateLinkLocalAddr (
201 IN OUT IP6_SERVICE *IpSb
202 )
203{
204 EFI_IPv6_ADDRESS *Ip6Addr;
205 EFI_IP6_CONFIG_PROTOCOL *Ip6Config;
206 UINTN DataSize;
207 EFI_IP6_CONFIG_INTERFACE_ID InterfaceId;
208 EFI_STATUS Status;
209
210 NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE);
211
212 if (IpSb->InterfaceId != NULL) {
213 FreePool (IpSb->InterfaceId);
214 }
215
216 //
217 // Get the interface id if it is manually configured.
218 //
219 Ip6Config = &IpSb->Ip6ConfigInstance.Ip6Config;
220 DataSize = sizeof (EFI_IP6_CONFIG_INTERFACE_ID);
221 ZeroMem (&InterfaceId, DataSize);
222
223 Status = Ip6Config->GetData (
224 Ip6Config,
225 Ip6ConfigDataTypeAltInterfaceId,
226 &DataSize,
227 &InterfaceId
228 );
229 if (Status == EFI_NOT_FOUND) {
230 //
231 // Since the interface id is not configured, generate the interface id from
232 // MAC address.
233 //
234 IpSb->InterfaceId = Ip6CreateInterfaceID (IpSb);
235 if (IpSb->InterfaceId == NULL) {
236 return NULL;
237 }
238
239 CopyMem (&InterfaceId, IpSb->InterfaceId, IpSb->InterfaceIdLen);
240 //
241 // Record the interface id.
242 //
243 Status = Ip6Config->SetData (
244 Ip6Config,
245 Ip6ConfigDataTypeAltInterfaceId,
246 DataSize,
247 &InterfaceId
248 );
249 if (EFI_ERROR (Status)) {
250 FreePool (IpSb->InterfaceId);
251 IpSb->InterfaceId = NULL;
252 return NULL;
253 }
254 } else if (!EFI_ERROR (Status)) {
255 IpSb->InterfaceId = AllocateCopyPool (DataSize, &InterfaceId);
256 if (IpSb->InterfaceId == NULL) {
257 return NULL;
258 }
259 } else {
260 return NULL;
261 }
262
263 //
264 // Append FE80::/64 to the left of IPv6 address then return.
265 //
266 Ip6Addr = AllocateZeroPool (sizeof (EFI_IPv6_ADDRESS));
267 if (Ip6Addr == NULL) {
268 FreePool (IpSb->InterfaceId);
269 IpSb->InterfaceId = NULL;
270 return NULL;
271 }
272
273 CopyMem (&Ip6Addr->Addr[8], IpSb->InterfaceId, IpSb->InterfaceIdLen);
274 Ip6Addr->Addr[1] = 0x80;
275 Ip6Addr->Addr[0] = 0xFE;
276
277 return Ip6Addr;
278}
279
280/**
281 Compute the solicited-node multicast address for an unicast or anycast address,
282 by taking the low-order 24 bits of this address, and appending those bits to
283 the prefix FF02:0:0:0:0:1:FF00::/104.
284
285 @param[in] Ip6Addr The unicast or anycast address, in network order.
286 @param[out] MulticastAddr The generated solicited-node multicast address,
287 in network order.
288
289**/
290VOID
291Ip6CreateSNMulticastAddr (
292 IN EFI_IPv6_ADDRESS *Ip6Addr,
293 OUT EFI_IPv6_ADDRESS *MulticastAddr
294 )
295{
296 ASSERT (Ip6Addr != NULL && MulticastAddr != NULL);
297
298 ZeroMem (MulticastAddr, sizeof (EFI_IPv6_ADDRESS));
299
300 MulticastAddr->Addr[0] = 0xFF;
301 MulticastAddr->Addr[1] = 0x02;
302 MulticastAddr->Addr[11] = 0x1;
303 MulticastAddr->Addr[12] = 0xFF;
304
305 CopyMem (&MulticastAddr->Addr[13], &Ip6Addr->Addr[13], 3);
306}
307
308/**
309 Insert a node IP6_ADDRESS_INFO to an IP6 interface.
310
311 @param[in, out] IpIf Points to an IP6 interface.
312 @param[in] AddrInfo Points to IP6_ADDRESS_INFO
313
314**/
315VOID
316Ip6AddAddr (
317 IN OUT IP6_INTERFACE *IpIf,
318 IN IP6_ADDRESS_INFO *AddrInfo
319 )
320{
321 InsertHeadList (&IpIf->AddressList, &AddrInfo->Link);
322 IpIf->AddressCount++;
323}
324
325/**
326 Callback function which provided by user to remove one node in NetDestroyLinkList process.
327
328 @param[in] Entry The entry to be removed.
329 @param[in] Context Pointer to the callback context corresponds to the Context in NetDestroyLinkList.
330
331 @retval EFI_SUCCESS The entry has been removed successfully.
332 @retval Others Fail to remove the entry.
333
334**/
335EFI_STATUS
336EFIAPI
337Ip6DestroyChildEntryByAddr (
338 IN LIST_ENTRY *Entry,
339 IN VOID *Context
340 )
341{
342 IP6_PROTOCOL *Instance;
343 EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding;
344 EFI_IPv6_ADDRESS *Address;
345
346 Instance = NET_LIST_USER_STRUCT_S (Entry, IP6_PROTOCOL, Link, IP6_PROTOCOL_SIGNATURE);
347 ServiceBinding = ((IP6_DESTROY_CHILD_BY_ADDR_CALLBACK_CONTEXT *)Context)->ServiceBinding;
348 Address = ((IP6_DESTROY_CHILD_BY_ADDR_CALLBACK_CONTEXT *)Context)->Address;
349
350 if ((Instance->State == IP6_STATE_CONFIGED) && EFI_IP6_EQUAL (&Instance->ConfigData.StationAddress, Address)) {
351 return ServiceBinding->DestroyChild (ServiceBinding, Instance->Handle);
352 }
353
354 return EFI_SUCCESS;
355}
356
357/**
358 Destroy the IP instance if its StationAddress is removed. It is the help function
359 for Ip6RemoveAddr().
360
361 @param[in, out] IpSb Points to an IP6 service binding instance.
362 @param[in] Address The to be removed address
363
364**/
365VOID
366Ip6DestroyInstanceByAddress (
367 IN OUT IP6_SERVICE *IpSb,
368 IN EFI_IPv6_ADDRESS *Address
369 )
370{
371 LIST_ENTRY *List;
372 IP6_DESTROY_CHILD_BY_ADDR_CALLBACK_CONTEXT Context;
373
374 NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE);
375
376 List = &IpSb->Children;
377 Context.ServiceBinding = &IpSb->ServiceBinding;
378 Context.Address = Address;
379 NetDestroyLinkList (
380 List,
381 Ip6DestroyChildEntryByAddr,
382 &Context,
383 NULL
384 );
385}
386
387/**
388 Remove the IPv6 address from the address list node points to IP6_ADDRESS_INFO.
389
390 This function removes the matching IPv6 addresses from the address list and
391 adjusts the address count of the address list. If IpSb is not NULL, this function
392 calls Ip6LeaveGroup to see whether it should call Mnp->Groups() to remove the
393 its solicited-node multicast MAC address from the filter list and sends out
394 a Multicast Listener Done. If Prefix is NULL, all address in the address list
395 will be removed. If Prefix is not NULL, the address that matching the Prefix
396 with PrefixLength in the address list will be removed.
397
398 @param[in] IpSb NULL or points to IP6 service binding instance.
399 @param[in, out] AddressList Address list array.
400 @param[in, out] AddressCount The count of addresses in address list array.
401 @param[in] Prefix NULL or an IPv6 address prefix.
402 @param[in] PrefixLength The length of Prefix.
403
404 @retval EFI_SUCCESS The operation completed successfully.
405 @retval EFI_NOT_FOUND The address matching the Prefix with PrefixLength
406 cannot be found in the address list.
407 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
408
409**/
410EFI_STATUS
411Ip6RemoveAddr (
412 IN IP6_SERVICE *IpSb OPTIONAL,
413 IN OUT LIST_ENTRY *AddressList,
414 IN OUT UINT32 *AddressCount,
415 IN EFI_IPv6_ADDRESS *Prefix OPTIONAL,
416 IN UINT8 PrefixLength
417 )
418{
419 EFI_STATUS Status;
420 LIST_ENTRY *Entry;
421 LIST_ENTRY *Next;
422 IP6_ADDRESS_INFO *AddrInfo;
423 EFI_IPv6_ADDRESS SnMCastAddr;
424
425 if (IsListEmpty (AddressList) || (*AddressCount < 1) || (PrefixLength > IP6_PREFIX_MAX)) {
426 return EFI_INVALID_PARAMETER;
427 }
428
429 Status = EFI_NOT_FOUND;
430
431 NET_LIST_FOR_EACH_SAFE (Entry, Next, AddressList) {
432 AddrInfo = NET_LIST_USER_STRUCT_S (Entry, IP6_ADDRESS_INFO, Link, IP6_ADDR_INFO_SIGNATURE);
433
434 if ((Prefix == NULL) ||
435 ((PrefixLength == 128) && EFI_IP6_EQUAL (Prefix, &AddrInfo->Address)) ||
436 ((PrefixLength == AddrInfo->PrefixLength) && NetIp6IsNetEqual (Prefix, &AddrInfo->Address, PrefixLength))
437 )
438 {
439 if (IpSb != NULL) {
440 NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE);
441 Ip6CreateSNMulticastAddr (&AddrInfo->Address, &SnMCastAddr);
442 Ip6LeaveGroup (IpSb, &SnMCastAddr);
443
444 //
445 // Destroy any instance who is using the dying address as the source address.
446 //
447 Ip6DestroyInstanceByAddress (IpSb, &AddrInfo->Address);
448 }
449
450 RemoveEntryList (Entry);
451 FreePool (AddrInfo);
452 (*AddressCount)--;
453
454 Status = EFI_SUCCESS;
455 }
456 }
457
458 return Status;
459}
460
461/**
462 Check whether the incoming Ipv6 address is a solicited-node multicast address.
463
464 @param[in] Ip6 Ip6 address, in network order.
465
466 @retval TRUE Yes, solicited-node multicast address
467 @retval FALSE No
468
469**/
470BOOLEAN
471Ip6IsSNMulticastAddr (
472 IN EFI_IPv6_ADDRESS *Ip6
473 )
474{
475 EFI_IPv6_ADDRESS Sn;
476 BOOLEAN Flag;
477
478 Ip6CreateSNMulticastAddr (Ip6, &Sn);
479 Flag = FALSE;
480
481 if (CompareMem (Sn.Addr, Ip6->Addr, 13) == 0) {
482 Flag = TRUE;
483 }
484
485 return Flag;
486}
487
488/**
489 Check whether the incoming IPv6 address is one of the maintained addresses in
490 the IP6 service binding instance.
491
492 @param[in] IpSb Points to a IP6 service binding instance.
493 @param[in] Address The IP6 address to be checked.
494 @param[out] Interface If not NULL, output the IP6 interface which
495 maintains the Address.
496 @param[out] AddressInfo If not NULL, output the IP6 address information
497 of the Address.
498
499 @retval TRUE Yes, it is one of the maintained address.
500 @retval FALSE No, it is not one of the maintained address.
501
502**/
503BOOLEAN
504Ip6IsOneOfSetAddress (
505 IN IP6_SERVICE *IpSb,
506 IN EFI_IPv6_ADDRESS *Address,
507 OUT IP6_INTERFACE **Interface OPTIONAL,
508 OUT IP6_ADDRESS_INFO **AddressInfo OPTIONAL
509 )
510{
511 LIST_ENTRY *Entry;
512 LIST_ENTRY *Entry2;
513 IP6_INTERFACE *IpIf;
514 IP6_ADDRESS_INFO *TmpAddressInfo;
515
516 //
517 // Check link-local address first
518 //
519 if (IpSb->LinkLocalOk && EFI_IP6_EQUAL (&IpSb->LinkLocalAddr, Address)) {
520 if (Interface != NULL) {
521 *Interface = IpSb->DefaultInterface;
522 }
523
524 if (AddressInfo != NULL) {
525 *AddressInfo = NULL;
526 }
527
528 return TRUE;
529 }
530
531 NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) {
532 IpIf = NET_LIST_USER_STRUCT_S (Entry, IP6_INTERFACE, Link, IP6_INTERFACE_SIGNATURE);
533
534 NET_LIST_FOR_EACH (Entry2, &IpIf->AddressList) {
535 TmpAddressInfo = NET_LIST_USER_STRUCT_S (Entry2, IP6_ADDRESS_INFO, Link, IP6_ADDR_INFO_SIGNATURE);
536
537 if (EFI_IP6_EQUAL (&TmpAddressInfo->Address, Address)) {
538 if (Interface != NULL) {
539 *Interface = IpIf;
540 }
541
542 if (AddressInfo != NULL) {
543 *AddressInfo = TmpAddressInfo;
544 }
545
546 return TRUE;
547 }
548 }
549 }
550
551 return FALSE;
552}
553
554/**
555 Check whether the incoming MAC address is valid.
556
557 @param[in] IpSb Points to a IP6 service binding instance.
558 @param[in] LinkAddress The MAC address.
559
560 @retval TRUE Yes, it is valid.
561 @retval FALSE No, it is not valid.
562
563**/
564BOOLEAN
565Ip6IsValidLinkAddress (
566 IN IP6_SERVICE *IpSb,
567 IN EFI_MAC_ADDRESS *LinkAddress
568 )
569{
570 UINT32 Index;
571
572 //
573 // TODO: might be updated later to be more acceptable.
574 //
575 for (Index = IpSb->SnpMode.HwAddressSize; Index < sizeof (EFI_MAC_ADDRESS); Index++) {
576 if (LinkAddress->Addr[Index] != 0) {
577 return FALSE;
578 }
579 }
580
581 return TRUE;
582}
583
584/**
585 Copy the PrefixLength bits from Src to Dest.
586
587 @param[out] Dest A pointer to the buffer to copy to.
588 @param[in] Src A pointer to the buffer to copy from.
589 @param[in] PrefixLength The number of bits to copy.
590
591**/
592VOID
593Ip6CopyAddressByPrefix (
594 OUT EFI_IPv6_ADDRESS *Dest,
595 IN EFI_IPv6_ADDRESS *Src,
596 IN UINT8 PrefixLength
597 )
598{
599 UINT8 Byte;
600 UINT8 Bit;
601 UINT8 Mask;
602
603 ASSERT (Dest != NULL && Src != NULL);
604 ASSERT (PrefixLength <= IP6_PREFIX_MAX);
605
606 Byte = (UINT8)(PrefixLength / 8);
607 Bit = (UINT8)(PrefixLength % 8);
608
609 ZeroMem (Dest, sizeof (EFI_IPv6_ADDRESS));
610
611 CopyMem (Dest, Src, Byte);
612
613 if (Bit > 0) {
614 Mask = (UINT8)(0xFF << (8 - Bit));
615 ASSERT (Byte < 16);
616 Dest->Addr[Byte] = (UINT8)(Src->Addr[Byte] & Mask);
617 }
618}
619
620/**
621 Get the MAC address for a multicast IP address. Call
622 Mnp's McastIpToMac to find the MAC address instead of
623 hard-coding the NIC to be Ethernet.
624
625 @param[in] Mnp The Mnp instance to get the MAC address.
626 @param[in] Multicast The multicast IP address to translate.
627 @param[out] Mac The buffer to hold the translated address.
628
629 @retval EFI_SUCCESS The multicast IP successfully
630 translated to a multicast MAC address.
631 @retval Other The address is not converted because an error occurred.
632
633**/
634EFI_STATUS
635Ip6GetMulticastMac (
636 IN EFI_MANAGED_NETWORK_PROTOCOL *Mnp,
637 IN EFI_IPv6_ADDRESS *Multicast,
638 OUT EFI_MAC_ADDRESS *Mac
639 )
640{
641 EFI_IP_ADDRESS EfiIp;
642
643 IP6_COPY_ADDRESS (&EfiIp.v6, Multicast);
644
645 return Mnp->McastIpToMac (Mnp, TRUE, &EfiIp, Mac);
646}
647
648/**
649 Convert the multibyte field in IP header's byter order.
650 In spite of its name, it can also be used to convert from
651 host to network byte order.
652
653 @param[in, out] Head The IP head to convert.
654
655 @return Point to the converted IP head.
656
657**/
658EFI_IP6_HEADER *
659Ip6NtohHead (
660 IN OUT EFI_IP6_HEADER *Head
661 )
662{
663 Head->FlowLabelL = NTOHS (Head->FlowLabelL);
664 Head->PayloadLength = NTOHS (Head->PayloadLength);
665
666 return Head;
667}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette