]> git.proxmox.com Git - mirror_edk2.git/blob - NetworkPkg/Ip6Dxe/Ip6Common.c
NetworkPkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / NetworkPkg / Ip6Dxe / Ip6Common.c
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 **/
28 EFI_STATUS
29 Ip6BuildEfiAddressList (
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 **/
102 EFI_STATUS
103 Ip6SetToAllNodeMulticast (
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 **/
141 UINT8 *
142 Ip6CreateInterfaceID (
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 **/
199 EFI_IPv6_ADDRESS *
200 Ip6CreateLinkLocalAddr (
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 manully 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 **/
290 VOID
291 Ip6CreateSNMulticastAddr (
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 **/
315 VOID
316 Ip6AddAddr (
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 **/
335 EFI_STATUS
336 EFIAPI
337 Ip6DestroyChildEntryByAddr (
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 **/
365 VOID
366 Ip6DestroyInstanceByAddress (
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 **/
410 EFI_STATUS
411 Ip6RemoveAddr (
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 if (IpSb != NULL) {
439 NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE);
440 Ip6CreateSNMulticastAddr (&AddrInfo->Address, &SnMCastAddr);
441 Ip6LeaveGroup (IpSb, &SnMCastAddr);
442
443 //
444 // Destroy any instance who is using the dying address as the source address.
445 //
446 Ip6DestroyInstanceByAddress (IpSb, &AddrInfo->Address);
447 }
448
449 RemoveEntryList (Entry);
450 FreePool (AddrInfo);
451 (*AddressCount)--;
452
453 Status = EFI_SUCCESS;
454 }
455 }
456
457 return Status;
458 }
459
460 /**
461 Check whether the incoming Ipv6 address is a solicited-node multicast address.
462
463 @param[in] Ip6 Ip6 address, in network order.
464
465 @retval TRUE Yes, solicited-node multicast address
466 @retval FALSE No
467
468 **/
469 BOOLEAN
470 Ip6IsSNMulticastAddr (
471 IN EFI_IPv6_ADDRESS *Ip6
472 )
473 {
474 EFI_IPv6_ADDRESS Sn;
475 BOOLEAN Flag;
476
477 Ip6CreateSNMulticastAddr (Ip6, &Sn);
478 Flag = FALSE;
479
480 if (CompareMem (Sn.Addr, Ip6->Addr, 13) == 0) {
481 Flag = TRUE;
482 }
483
484 return Flag;
485 }
486
487 /**
488 Check whether the incoming IPv6 address is one of the maintained addresses in
489 the IP6 service binding instance.
490
491 @param[in] IpSb Points to a IP6 service binding instance.
492 @param[in] Address The IP6 address to be checked.
493 @param[out] Interface If not NULL, output the IP6 interface which
494 maintains the Address.
495 @param[out] AddressInfo If not NULL, output the IP6 address information
496 of the Address.
497
498 @retval TRUE Yes, it is one of the maintained address.
499 @retval FALSE No, it is not one of the maintained address.
500
501 **/
502 BOOLEAN
503 Ip6IsOneOfSetAddress (
504 IN IP6_SERVICE *IpSb,
505 IN EFI_IPv6_ADDRESS *Address,
506 OUT IP6_INTERFACE **Interface OPTIONAL,
507 OUT IP6_ADDRESS_INFO **AddressInfo OPTIONAL
508 )
509 {
510 LIST_ENTRY *Entry;
511 LIST_ENTRY *Entry2;
512 IP6_INTERFACE *IpIf;
513 IP6_ADDRESS_INFO *TmpAddressInfo;
514
515 //
516 // Check link-local address first
517 //
518 if (IpSb->LinkLocalOk && EFI_IP6_EQUAL (&IpSb->LinkLocalAddr, Address)) {
519 if (Interface != NULL) {
520 *Interface = IpSb->DefaultInterface;
521 }
522
523 if (AddressInfo != NULL) {
524 *AddressInfo = NULL;
525 }
526
527 return TRUE;
528 }
529
530 NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) {
531 IpIf = NET_LIST_USER_STRUCT_S (Entry, IP6_INTERFACE, Link, IP6_INTERFACE_SIGNATURE);
532
533 NET_LIST_FOR_EACH (Entry2, &IpIf->AddressList) {
534 TmpAddressInfo = NET_LIST_USER_STRUCT_S (Entry2, IP6_ADDRESS_INFO, Link, IP6_ADDR_INFO_SIGNATURE);
535
536 if (EFI_IP6_EQUAL (&TmpAddressInfo->Address, Address)) {
537 if (Interface != NULL) {
538 *Interface = IpIf;
539 }
540
541 if (AddressInfo != NULL) {
542 *AddressInfo = TmpAddressInfo;
543 }
544
545 return TRUE;
546 }
547 }
548 }
549
550 return FALSE;
551 }
552
553 /**
554 Check whether the incoming MAC address is valid.
555
556 @param[in] IpSb Points to a IP6 service binding instance.
557 @param[in] LinkAddress The MAC address.
558
559 @retval TRUE Yes, it is valid.
560 @retval FALSE No, it is not valid.
561
562 **/
563 BOOLEAN
564 Ip6IsValidLinkAddress (
565 IN IP6_SERVICE *IpSb,
566 IN EFI_MAC_ADDRESS *LinkAddress
567 )
568 {
569 UINT32 Index;
570
571 //
572 // TODO: might be updated later to be more acceptable.
573 //
574 for (Index = IpSb->SnpMode.HwAddressSize; Index < sizeof (EFI_MAC_ADDRESS); Index++) {
575 if (LinkAddress->Addr[Index] != 0) {
576 return FALSE;
577 }
578 }
579
580 return TRUE;
581 }
582
583 /**
584 Copy the PrefixLength bits from Src to Dest.
585
586 @param[out] Dest A pointer to the buffer to copy to.
587 @param[in] Src A pointer to the buffer to copy from.
588 @param[in] PrefixLength The number of bits to copy.
589
590 **/
591 VOID
592 Ip6CopyAddressByPrefix (
593 OUT EFI_IPv6_ADDRESS *Dest,
594 IN EFI_IPv6_ADDRESS *Src,
595 IN UINT8 PrefixLength
596 )
597 {
598 UINT8 Byte;
599 UINT8 Bit;
600 UINT8 Mask;
601
602 ASSERT (Dest != NULL && Src != NULL);
603 ASSERT (PrefixLength <= IP6_PREFIX_MAX);
604
605 Byte = (UINT8) (PrefixLength / 8);
606 Bit = (UINT8) (PrefixLength % 8);
607
608 ZeroMem (Dest, sizeof (EFI_IPv6_ADDRESS));
609
610 CopyMem (Dest, Src, Byte);
611
612 if (Bit > 0) {
613 Mask = (UINT8) (0xFF << (8 - Bit));
614 ASSERT (Byte < 16);
615 Dest->Addr[Byte] = (UINT8) (Src->Addr[Byte] & Mask);
616 }
617 }
618
619 /**
620 Get the MAC address for a multicast IP address. Call
621 Mnp's McastIpToMac to find the MAC address instead of
622 hard-coding the NIC to be Ethernet.
623
624 @param[in] Mnp The Mnp instance to get the MAC address.
625 @param[in] Multicast The multicast IP address to translate.
626 @param[out] Mac The buffer to hold the translated address.
627
628 @retval EFI_SUCCESS The multicast IP successfully
629 translated to a multicast MAC address.
630 @retval Other The address is not converted because an error occurred.
631
632 **/
633 EFI_STATUS
634 Ip6GetMulticastMac (
635 IN EFI_MANAGED_NETWORK_PROTOCOL *Mnp,
636 IN EFI_IPv6_ADDRESS *Multicast,
637 OUT EFI_MAC_ADDRESS *Mac
638 )
639 {
640 EFI_IP_ADDRESS EfiIp;
641
642 IP6_COPY_ADDRESS (&EfiIp.v6, Multicast);
643
644 return Mnp->McastIpToMac (Mnp, TRUE, &EfiIp, Mac);
645 }
646
647 /**
648 Convert the multibyte field in IP header's byter order.
649 In spite of its name, it can also be used to convert from
650 host to network byte order.
651
652 @param[in, out] Head The IP head to convert.
653
654 @return Point to the converted IP head.
655
656 **/
657 EFI_IP6_HEADER *
658 Ip6NtohHead (
659 IN OUT EFI_IP6_HEADER *Head
660 )
661 {
662 Head->FlowLabelL = NTOHS (Head->FlowLabelL);
663 Head->PayloadLength = NTOHS (Head->PayloadLength);
664
665 return Head;
666 }
667