2 The internal functions and routines to transmit the IP6 packet.
4 Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
15 Output all the available source addresses to a list entry head SourceList. The
16 number of source addresses are also returned.
18 @param[in] IpSb Points to an IP6 service binding instance.
19 @param[out] SourceList The list entry head of all source addresses.
20 It is the caller's responsibility to free the
22 @param[out] SourceCount The number of source addresses.
24 @retval EFI_SUCCESS The source addresses were copied to a list entry head
26 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources to complete the operation.
32 OUT LIST_ENTRY
*SourceList
,
33 OUT UINT32
*SourceCount
39 IP6_ADDRESS_INFO
*AddrInfo
;
40 IP6_ADDRESS_INFO
*Copy
;
44 if (IpSb
->LinkLocalOk
) {
45 Copy
= AllocatePool (sizeof (IP6_ADDRESS_INFO
));
47 return EFI_OUT_OF_RESOURCES
;
50 Copy
->Signature
= IP6_ADDR_INFO_SIGNATURE
;
51 IP6_COPY_ADDRESS (&Copy
->Address
, &IpSb
->LinkLocalAddr
);
52 Copy
->IsAnycast
= FALSE
;
53 Copy
->PrefixLength
= IP6_LINK_LOCAL_PREFIX_LENGTH
;
54 Copy
->ValidLifetime
= (UINT32
)IP6_INFINIT_LIFETIME
;
55 Copy
->PreferredLifetime
= (UINT32
)IP6_INFINIT_LIFETIME
;
57 InsertTailList (SourceList
, &Copy
->Link
);
61 NET_LIST_FOR_EACH (Entry
, &IpSb
->Interfaces
) {
62 IpIf
= NET_LIST_USER_STRUCT (Entry
, IP6_INTERFACE
, Link
);
64 NET_LIST_FOR_EACH (Entry2
, &IpIf
->AddressList
) {
65 AddrInfo
= NET_LIST_USER_STRUCT_S (Entry2
, IP6_ADDRESS_INFO
, Link
, IP6_ADDR_INFO_SIGNATURE
);
67 if (AddrInfo
->IsAnycast
) {
69 // Never use an anycast address.
74 Copy
= AllocateCopyPool (sizeof (IP6_ADDRESS_INFO
), AddrInfo
);
76 return EFI_OUT_OF_RESOURCES
;
79 InsertTailList (SourceList
, &Copy
->Link
);
88 Calculate how many bits are the same between two IPv6 addresses.
90 @param[in] AddressA Points to an IPv6 address.
91 @param[in] AddressB Points to another IPv6 address.
93 @return The common bits of the AddressA and AddressB.
98 IN EFI_IPv6_ADDRESS
*AddressA
,
99 IN EFI_IPv6_ADDRESS
*AddressB
112 ByteA
= AddressA
->Addr
[Index
];
113 ByteB
= AddressB
->Addr
[Index
];
115 if (ByteA
== ByteB
) {
122 // Check how many bits are common between the two bytes.
125 ByteA
= (UINT8
)(ByteA
^ ByteB
);
129 ByteA
= (UINT8
)(ByteA
>> 1);
132 return (UINT8
)(Count
+ NumBits
);
139 Output all the available source addresses to a list entry head SourceList. The
140 number of source addresses are also returned.
142 @param[in] IpSb Points to a IP6 service binding instance.
143 @param[in] Destination The IPv6 destination address.
144 @param[out] Source The selected IPv6 source address according to
147 @retval EFI_SUCCESS The source addresses were copied to a list entry
149 @retval EFI_NO_MAPPING The IPv6 stack is not auto configured.
153 Ip6SelectSourceAddress (
154 IN IP6_SERVICE
*IpSb
,
155 IN EFI_IPv6_ADDRESS
*Destination
,
156 OUT EFI_IPv6_ADDRESS
*Source
160 LIST_ENTRY SourceList
;
164 IP6_ADDRESS_INFO
*AddrInfo
;
165 IP6_PREFIX_LIST_ENTRY
*Prefix
;
166 UINT8 LastCommonLength
;
167 UINT8 CurrentCommonLength
;
168 EFI_IPv6_ADDRESS
*TmpAddress
;
170 NET_CHECK_SIGNATURE (IpSb
, IP6_SERVICE_SIGNATURE
);
172 Status
= EFI_SUCCESS
;
173 InitializeListHead (&SourceList
);
175 if (!IpSb
->LinkLocalOk
) {
176 return EFI_NO_MAPPING
;
180 // Rule 1: Prefer same address.
182 if (Ip6IsOneOfSetAddress (IpSb
, Destination
, NULL
, NULL
)) {
183 IP6_COPY_ADDRESS (Source
, Destination
);
188 // Rule 2: Prefer appropriate scope.
190 if (IP6_IS_MULTICAST (Destination
)) {
191 ScopeD
= (UINT8
)(Destination
->Addr
[1] >> 4);
192 } else if (NetIp6IsLinkLocalAddr (Destination
)) {
200 // Return the link-local address if it exists
201 // One IP6_SERVICE only has one link-local address.
203 IP6_COPY_ADDRESS (Source
, &IpSb
->LinkLocalAddr
);
208 // All candidate source addresses are global unicast address.
210 Ip6CandidateSource (IpSb
, &SourceList
, &SourceCount
);
212 if (SourceCount
== 0) {
213 Status
= EFI_NO_MAPPING
;
217 IP6_COPY_ADDRESS (Source
, &IpSb
->LinkLocalAddr
);
219 if (SourceCount
== 1) {
224 // Rule 3: Avoid deprecated addresses.
225 // TODO: check the "deprecated" state of the stateful configured address
227 NET_LIST_FOR_EACH (Entry
, &IpSb
->AutonomousPrefix
) {
228 Prefix
= NET_LIST_USER_STRUCT (Entry
, IP6_PREFIX_LIST_ENTRY
, Link
);
229 if (Prefix
->PreferredLifetime
== 0) {
230 Ip6RemoveAddr (NULL
, &SourceList
, &SourceCount
, &Prefix
->Prefix
, Prefix
->PrefixLength
);
232 if (SourceCount
== 1) {
239 // TODO: Rule 4: Prefer home addresses.
240 // TODO: Rule 5: Prefer outgoing interface.
241 // TODO: Rule 6: Prefer matching label.
242 // TODO: Rule 7: Prefer public addresses.
246 // Rule 8: Use longest matching prefix.
248 LastCommonLength
= Ip6CommonPrefixLen (Source
, Destination
);
251 for (Entry
= SourceList
.ForwardLink
; Entry
!= &SourceList
; Entry
= Entry
->ForwardLink
) {
252 AddrInfo
= NET_LIST_USER_STRUCT_S (Entry
, IP6_ADDRESS_INFO
, Link
, IP6_ADDR_INFO_SIGNATURE
);
254 CurrentCommonLength
= Ip6CommonPrefixLen (&AddrInfo
->Address
, Destination
);
255 if (CurrentCommonLength
> LastCommonLength
) {
256 LastCommonLength
= CurrentCommonLength
;
257 TmpAddress
= &AddrInfo
->Address
;
261 if (TmpAddress
!= NULL
) {
262 IP6_COPY_ADDRESS (Source
, TmpAddress
);
267 Ip6RemoveAddr (NULL
, &SourceList
, &SourceCount
, NULL
, 0);
273 Select an interface to send the packet generated in the IP6 driver
274 itself: that is, not by the requests of the IP6 child's consumer. Such
275 packets include the ICMPv6 echo replies and other ICMPv6 error packets.
277 @param[in] IpSb The IP4 service that wants to send the packets.
278 @param[in] Destination The destination of the packet.
279 @param[in, out] Source The source of the packet.
281 @return NULL if no proper interface is found, otherwise, the interface that
282 can be used to send the system packet from.
287 IN IP6_SERVICE
*IpSb
,
288 IN EFI_IPv6_ADDRESS
*Destination
,
289 IN OUT EFI_IPv6_ADDRESS
*Source
293 EFI_IPv6_ADDRESS SelectedSource
;
297 NET_CHECK_SIGNATURE (IpSb
, IP6_SERVICE_SIGNATURE
);
298 ASSERT (Destination
!= NULL
&& Source
!= NULL
);
300 if (NetIp6IsUnspecifiedAddr (Destination
)) {
304 if (!NetIp6IsUnspecifiedAddr (Source
)) {
305 Exist
= Ip6IsOneOfSetAddress (IpSb
, Source
, &IpIf
, NULL
);
312 // If source is unspecified, select a source according to the destination.
314 Status
= Ip6SelectSourceAddress (IpSb
, Destination
, &SelectedSource
);
315 if (EFI_ERROR (Status
)) {
316 return IpSb
->DefaultInterface
;
319 Ip6IsOneOfSetAddress (IpSb
, &SelectedSource
, &IpIf
, NULL
);
320 IP6_COPY_ADDRESS (Source
, &SelectedSource
);
326 The default callback function for the system generated packet.
327 It will free the packet.
329 @param[in] Packet The packet that transmitted.
330 @param[in] IoStatus The result of the transmission, succeeded or failed.
331 @param[in] LinkFlag Not used when transmitted. Check IP6_FRAME_CALLBACK
333 @param[in] Context The context provided by us.
349 Prefix an IP6 basic head and unfragmentable extension headers and a fragment header
350 to the Packet. Used for IP6 fragmentation.
352 @param[in] IpSb The IP6 service instance to transmit the packet.
353 @param[in] Packet The packet to prefix the IP6 header to.
354 @param[in] Head The caller supplied header.
355 @param[in] FragmentOffset The fragment offset of the data following the header.
356 @param[in] ExtHdrs The length of the original extension header.
357 @param[in] ExtHdrsLen The length of the extension headers.
358 @param[in] LastHeader The pointer of next header of last extension header.
359 @param[in] HeadLen The length of the unfragmented part of the IP6 header.
361 @retval EFI_BAD_BUFFER_SIZE There is no enough room in the head space of
363 @retval EFI_SUCCESS The operation performed successfully.
368 IN IP6_SERVICE
*IpSb
,
370 IN EFI_IP6_HEADER
*Head
,
371 IN UINT16 FragmentOffset
,
373 IN UINT32 ExtHdrsLen
,
379 UINT32 UnFragExtHdrsLen
;
380 EFI_IP6_HEADER
*PacketHead
;
381 UINT8
*UpdatedExtHdrs
;
385 UpdatedExtHdrs
= NULL
;
388 // HeadLen is the length of the fixed part of the sequences of fragments, i.e.
389 // the unfragment part.
391 PacketHead
= (EFI_IP6_HEADER
*)NetbufAllocSpace (Packet
, HeadLen
, NET_BUF_HEAD
);
392 if (PacketHead
== NULL
) {
393 return EFI_BAD_BUFFER_SIZE
;
397 // Set the head up, convert the host byte order to network byte order
399 CopyMem (PacketHead
, Head
, sizeof (EFI_IP6_HEADER
));
400 PacketHead
->PayloadLength
= HTONS ((UINT16
)(Packet
->TotalSize
- sizeof (EFI_IP6_HEADER
)));
401 Packet
->Ip
.Ip6
= PacketHead
;
403 Len
= HeadLen
- sizeof (EFI_IP6_HEADER
);
404 UnFragExtHdrsLen
= Len
- sizeof (IP6_FRAGMENT_HEADER
);
406 if (UnFragExtHdrsLen
== 0) {
407 PacketHead
->NextHeader
= IP6_FRAGMENT
;
411 // Append the extension headers: firstly copy the unfragmentable headers, then append
412 // fragmentation header.
414 if ((FragmentOffset
& IP6_FRAGMENT_OFFSET_MASK
) == 0) {
415 NextHeader
= Head
->NextHeader
;
417 NextHeader
= PacketHead
->NextHeader
;
420 Status
= Ip6FillFragmentHeader (
429 if (EFI_ERROR (Status
)) {
434 (UINT8
*)(PacketHead
+ 1),
436 UnFragExtHdrsLen
+ sizeof (IP6_FRAGMENT_HEADER
)
439 FreePool (UpdatedExtHdrs
);
444 Transmit an IP6 packet. The packet comes either from the IP6
445 child's consumer (IpInstance != NULL) or the IP6 driver itself
446 (IpInstance == NULL). It will route the packet, fragment it,
447 then transmit all the fragments through an interface.
449 @param[in] IpSb The IP6 service instance to transmit the packet.
450 @param[in] Interface The IP6 interface to transmit the packet. Ignored
452 @param[in] IpInstance The IP6 child that issues the transmission. It is
453 NULL if the packet is from the system.
454 @param[in] Packet The user data to send, excluding the IP header.
455 @param[in] Head The caller supplied header. The caller should set
456 the following header fields: NextHeader, HopLimit,
457 Src, Dest, FlowLabel, PayloadLength. This function
458 will fill in the Ver, TrafficClass.
459 @param[in] ExtHdrs The extension headers to append to the IPv6 basic
461 @param[in] ExtHdrsLen The length of the extension headers.
462 @param[in] Callback The callback function to issue when transmission
464 @param[in] Context The opaque context for the callback.
466 @retval EFI_INVALID_PARAMETER Any input parameter or the packet is invalid.
467 @retval EFI_NO_MAPPING There is no interface to the destination.
468 @retval EFI_NOT_FOUND There is no route to the destination.
469 @retval EFI_SUCCESS The packet successfully transmitted.
470 @retval EFI_OUT_OF_RESOURCES Failed to finish the operation due to lack of
472 @retval Others Failed to transmit the packet.
477 IN IP6_SERVICE
*IpSb
,
478 IN IP6_INTERFACE
*Interface OPTIONAL
,
479 IN IP6_PROTOCOL
*IpInstance OPTIONAL
,
481 IN EFI_IP6_HEADER
*Head
,
483 IN UINT32 ExtHdrsLen
,
484 IN IP6_FRAME_CALLBACK Callback
,
489 EFI_IPv6_ADDRESS NextHop
;
490 IP6_NEIGHBOR_ENTRY
*NeighborCache
;
491 IP6_ROUTE_CACHE_ENTRY
*RouteCache
;
495 UINT16 FragmentOffset
;
497 UINT32 UnFragmentLen
;
498 UINT32 UnFragmentHdrsLen
;
499 UINT32 FragmentHdrsLen
;
501 UINT16 PacketChecksum
;
502 UINT16 PseudoChecksum
;
511 EFI_IP6_HEADER
*PacketHead
;
512 IP6_ICMP_HEAD
*IcmpHead
;
513 IP6_TXTOKEN_WRAP
*Wrap
;
514 IP6_ROUTE_ENTRY
*RouteEntry
;
515 UINT8
*UpdatedExtHdrs
;
517 UINT8 LastHeaderBackup
;
518 BOOLEAN FragmentHeadInserted
;
519 UINT8
*ExtHdrsBackup
;
520 UINT8 NextHeaderBackup
;
521 EFI_IPv6_ADDRESS Source
;
522 EFI_IPv6_ADDRESS Destination
;
524 NET_CHECK_SIGNATURE (IpSb
, IP6_SERVICE_SIGNATURE
);
527 // RFC2460: Each extension header is an integer multiple of 8 octets long,
528 // in order to retain 8-octet alignment for subsequent headers.
530 if ((ExtHdrsLen
& 0x7) != 0) {
531 return EFI_INVALID_PARAMETER
;
551 // Select an interface/source for system packet, application
552 // should select them itself.
557 // IpInstance->Interface is NULL when IpInstance is configured with both stationaddress
558 // and destinationaddress is unspecified.
560 if ((IpInstance
== NULL
) || (IpInstance
->Interface
== NULL
)) {
561 IpIf
= Ip6SelectInterface (IpSb
, &Head
->DestinationAddress
, &Head
->SourceAddress
);
562 if (IpInstance
!= NULL
) {
563 IpInstance
->Interface
= IpIf
;
566 IpIf
= IpInstance
->Interface
;
571 return EFI_NO_MAPPING
;
575 // Update the common field in Head here.
578 Head
->TrafficClassL
= 0;
579 Head
->TrafficClassH
= 0;
582 NextHeader
= *LastHeader
;
584 switch (NextHeader
) {
585 case EFI_IP_PROTO_UDP
:
586 Packet
->Udp
= (EFI_UDP_HEADER
*)NetbufGetByte (Packet
, 0, NULL
);
587 ASSERT (Packet
->Udp
!= NULL
);
588 if (Packet
->Udp
->Checksum
== 0) {
589 Checksum
= &Packet
->Udp
->Checksum
;
594 case EFI_IP_PROTO_TCP
:
595 Packet
->Tcp
= (TCP_HEAD
*)NetbufGetByte (Packet
, 0, NULL
);
596 ASSERT (Packet
->Tcp
!= NULL
);
597 if (Packet
->Tcp
->Checksum
== 0) {
598 Checksum
= &Packet
->Tcp
->Checksum
;
605 // Don't send ICMP packet to an IPv6 anycast address.
607 if (Ip6IsAnycast (IpSb
, &Head
->DestinationAddress
)) {
608 return EFI_INVALID_PARAMETER
;
611 IcmpHead
= (IP6_ICMP_HEAD
*)NetbufGetByte (Packet
, 0, NULL
);
612 ASSERT (IcmpHead
!= NULL
);
613 if (IcmpHead
->Checksum
== 0) {
614 Checksum
= &IcmpHead
->Checksum
;
623 if (Checksum
!= NULL
) {
625 // Calculate the checksum for upper layer protocol if it is not calculated due to lack of
626 // IPv6 source address.
628 PacketChecksum
= NetbufChecksum (Packet
);
629 PseudoChecksum
= NetIp6PseudoHeadChecksum (
630 &Head
->SourceAddress
,
631 &Head
->DestinationAddress
,
635 *Checksum
= (UINT16
) ~NetAddChecksum (PacketChecksum
, PseudoChecksum
);
638 Status
= Ip6IpSecProcessPacket (
641 LastHeader
, // no need get the lasthead value for output
649 if (EFI_ERROR (Status
)) {
655 // Check incoming parameters.
657 if (!Ip6IsExtsValid (
671 return EFI_INVALID_PARAMETER
;
674 if ((RealExtLen
& 0x7) != 0) {
675 return EFI_INVALID_PARAMETER
;
678 LastHeaderBackup
= *LastHeader
;
681 // Perform next hop determination:
682 // For multicast packets, the next-hop is always the destination address and
683 // is considered to be on-link.
685 if (IP6_IS_MULTICAST (&Head
->DestinationAddress
)) {
686 IP6_COPY_ADDRESS (&NextHop
, &Head
->DestinationAddress
);
689 // For unicast packets, use a combination of the Destination Cache, the Prefix List
690 // and the Default Router List to determine the IP address of the appropriate next hop.
693 NeighborCache
= Ip6FindNeighborEntry (IpSb
, &Head
->DestinationAddress
);
694 if (NeighborCache
!= NULL
) {
696 // Hit Neighbor Cache.
698 IP6_COPY_ADDRESS (&NextHop
, &Head
->DestinationAddress
);
701 // Not in Neighbor Cache, check Router cache
703 RouteCache
= Ip6Route (IpSb
, &Head
->DestinationAddress
, &Head
->SourceAddress
);
704 if (RouteCache
== NULL
) {
705 return EFI_NOT_FOUND
;
708 IP6_COPY_ADDRESS (&NextHop
, &RouteCache
->NextHop
);
709 Ip6FreeRouteCacheEntry (RouteCache
);
714 // Examines the Neighbor Cache for link-layer information about that neighbor.
715 // DO NOT create neighbor cache if neighbor is itself - when reporting ICMP error.
717 if (!IP6_IS_MULTICAST (&NextHop
) && !EFI_IP6_EQUAL (&Head
->DestinationAddress
, &Head
->SourceAddress
)) {
718 NeighborCache
= Ip6FindNeighborEntry (IpSb
, &NextHop
);
719 if (NeighborCache
== NULL
) {
720 NeighborCache
= Ip6CreateNeighborEntry (IpSb
, Ip6OnArpResolved
, &NextHop
, NULL
);
722 if (NeighborCache
== NULL
) {
723 return EFI_OUT_OF_RESOURCES
;
727 // Send out multicast neighbor solicitation for address resolution immediately.
729 Ip6CreateSNMulticastAddr (&NeighborCache
->Neighbor
, &Destination
);
730 Status
= Ip6SelectSourceAddress (IpSb
, &NeighborCache
->Neighbor
, &Source
);
731 if (EFI_ERROR (Status
)) {
735 Status
= Ip6SendNeighborSolicit (
739 &NeighborCache
->Neighbor
,
740 &IpSb
->SnpMode
.CurrentAddress
742 if (EFI_ERROR (Status
)) {
746 --NeighborCache
->Transmit
;
747 NeighborCache
->Ticks
= IP6_GET_TICKS (IpSb
->RetransTimer
) + 1;
750 NeighborCache
->Interface
= IpIf
;
753 UpdatedExtHdrs
= NULL
;
754 ExtHdrsBackup
= NULL
;
755 NextHeaderBackup
= 0;
756 FragmentHeadInserted
= FALSE
;
759 // Check whether we received Packet Too Big message for the packet sent to the
760 // Destination. If yes include a Fragment Header in the subsequent packets.
762 RouteEntry
= Ip6FindRouteEntry (
764 &Head
->DestinationAddress
,
767 if (RouteEntry
!= NULL
) {
768 if ((RouteEntry
->Flag
& IP6_PACKET_TOO_BIG
) == IP6_PACKET_TOO_BIG
) {
770 // FragmentHead is inserted after Hop-by-Hop Options header, Destination
771 // Options header (first occur), Routing header, and before Fragment header,
772 // Authentication header, Encapsulating Security Payload header, and
773 // Destination Options header (last occur), and upper-layer header.
775 Status
= Ip6FillFragmentHeader (
784 if (EFI_ERROR (Status
)) {
788 if ((ExtHdrs
== NULL
) && (ExtHdrsLen
== 0)) {
789 NextHeaderBackup
= Head
->NextHeader
;
790 Head
->NextHeader
= IP6_FRAGMENT
;
793 ExtHdrsBackup
= ExtHdrs
;
794 ExtHdrs
= UpdatedExtHdrs
;
795 ExtHdrsLen
= ExtHdrsLen
+ sizeof (IP6_FRAGMENT_HEADER
);
796 RealExtLen
= RealExtLen
+ sizeof (IP6_FRAGMENT_HEADER
);
800 FragmentHeadInserted
= TRUE
;
803 Ip6FreeRouteEntry (RouteEntry
);
807 // OK, selected the source and route, fragment the packet then send
808 // them. Tag each fragment other than the first one as spawn from it.
809 // Each extension header is an integer multiple of 8 octets long, in
810 // order to retain 8-octet alignment for subsequent headers.
812 Mtu
= IpSb
->MaxPacketSize
+ sizeof (EFI_IP6_HEADER
);
813 HeadLen
= sizeof (EFI_IP6_HEADER
) + RealExtLen
;
815 if (Packet
->TotalSize
+ HeadLen
> Mtu
) {
817 // Remove the inserted Fragment Header since we need fragment the packet.
819 if (FragmentHeadInserted
) {
820 ExtHdrs
= ExtHdrsBackup
;
821 ExtHdrsLen
= ExtHdrsLen
- sizeof (IP6_FRAGMENT_HEADER
);
823 if ((ExtHdrs
== NULL
) && (ExtHdrsLen
== 0)) {
824 Head
->NextHeader
= NextHeaderBackup
;
828 FragmentHdrsLen
= ExtHdrsLen
- UnFragmentHdrsLen
;
831 // The packet is beyond the maximum which can be described through the
832 // fragment offset field in Fragment header.
834 if ((((Packet
->TotalSize
+ FragmentHdrsLen
) >> 3) & (~0x1fff)) != 0) {
835 Status
= EFI_BAD_BUFFER_SIZE
;
839 if (FragmentHdrsLen
!= 0) {
841 // Append the fragmentable extension hdrs before the upper layer payload
842 // to form a new NET_BUF. This NET_BUF contains all the buffer which will
843 // be fragmented below.
845 TmpPacket
= NetbufGetFragment (Packet
, 0, Packet
->TotalSize
, FragmentHdrsLen
);
846 ASSERT (TmpPacket
!= NULL
);
849 // Allocate the space to contain the fragmentable hdrs and copy the data.
851 Buf
= NetbufAllocSpace (TmpPacket
, FragmentHdrsLen
, TRUE
);
852 ASSERT (Buf
!= NULL
);
853 CopyMem (Buf
, ExtHdrs
+ UnFragmentHdrsLen
, FragmentHdrsLen
);
856 // Free the old Packet.
863 // The unfragment part which appears in every fragmented IPv6 packet includes
864 // the IPv6 header, the unfragmentable extension hdrs and the fragment header.
866 UnFragmentLen
= sizeof (EFI_IP6_HEADER
) + UnFragmentHdrsLen
+ sizeof (IP6_FRAGMENT_HEADER
);
869 // Mtu now is the length of the fragment part in a full-length fragment.
871 Mtu
= (Mtu
- UnFragmentLen
) & (~0x07);
872 Num
= (Packet
->TotalSize
+ Mtu
- 1) / Mtu
;
874 for (Index
= 0, Offset
= 0, PacketLen
= Mtu
; Index
< Num
; Index
++) {
876 // Get fragment from the Packet, append UnFragmentLen spare buffer
877 // before the fragmented data, the corresponding data is filled in later.
879 Fragment
= NetbufGetFragment (Packet
, Offset
, PacketLen
, UnFragmentLen
);
880 if (Fragment
== NULL
) {
881 Status
= EFI_OUT_OF_RESOURCES
;
885 FragmentOffset
= (UINT16
)((UINT16
)Offset
| 0x1);
886 if (Index
== Num
- 1) {
888 // The last fragment, clear the M flag.
890 FragmentOffset
&= (~0x1);
893 Status
= Ip6PrependHead (
903 ASSERT (Status
== EFI_SUCCESS
);
905 Status
= Ip6SendFrame (
913 if (EFI_ERROR (Status
)) {
918 // The last fragment of upper layer packet, update the IP6 token status.
920 if ((Index
== Num
-1) && (Context
!= NULL
)) {
921 Wrap
= (IP6_TXTOKEN_WRAP
*)Context
;
922 Wrap
->Token
->Status
= Status
;
926 PacketLen
= Packet
->TotalSize
- Offset
;
927 if (PacketLen
> Mtu
) {
935 if (UpdatedExtHdrs
!= NULL
) {
936 FreePool (UpdatedExtHdrs
);
943 // Need not fragment the packet, send it in one frame.
945 PacketHead
= (EFI_IP6_HEADER
*)NetbufAllocSpace (Packet
, HeadLen
, NET_BUF_HEAD
);
946 if (PacketHead
== NULL
) {
947 Status
= EFI_BAD_BUFFER_SIZE
;
951 CopyMem (PacketHead
, Head
, sizeof (EFI_IP6_HEADER
));
952 Packet
->Ip
.Ip6
= PacketHead
;
954 if (ExtHdrs
!= NULL
) {
955 Buf
= (UINT8
*)(PacketHead
+ 1);
956 CopyMem (Buf
, ExtHdrs
, ExtHdrsLen
);
959 if (UpdatedExtHdrs
!= NULL
) {
961 // A Fragment Header is inserted to the packet, update the payload length.
963 PacketHead
->PayloadLength
= (UINT16
)(NTOHS (PacketHead
->PayloadLength
) +
964 sizeof (IP6_FRAGMENT_HEADER
));
965 PacketHead
->PayloadLength
= HTONS (PacketHead
->PayloadLength
);
966 FreePool (UpdatedExtHdrs
);
969 return Ip6SendFrame (
979 if (UpdatedExtHdrs
!= NULL
) {
980 FreePool (UpdatedExtHdrs
);
983 Ip6CancelPacket (IpIf
, Packet
, Status
);
988 The filter function to find a packet and all its fragments.
989 The packet's fragments have their Context set to the packet.
991 @param[in] Frame The frames hold by the low level interface.
992 @param[in] Context Context to the function, which is the packet.
994 @retval TRUE This is the packet to cancel or its fragments.
995 @retval FALSE This is an unrelated packet.
999 Ip6CancelPacketFragments (
1000 IN IP6_LINK_TX_TOKEN
*Frame
,
1004 if ((Frame
->Packet
== (NET_BUF
*)Context
) || (Frame
->Context
== Context
)) {
1012 Remove all the frames on the interface that pass the FrameToCancel,
1013 either queued on ARP queues or that have already been delivered to
1014 MNP and not yet recycled.
1016 @param[in] Interface Interface to remove the frames from.
1017 @param[in] IoStatus The transmit status returned to the frames' callback.
1018 @param[in] FrameToCancel Function to select the frame to cancel; NULL to select all.
1019 @param[in] Context Opaque parameters passed to FrameToCancel. Ignored if
1020 FrameToCancel is NULL.
1025 IN IP6_INTERFACE
*Interface
,
1026 IN EFI_STATUS IoStatus
,
1027 IN IP6_FRAME_TO_CANCEL FrameToCancel OPTIONAL
,
1028 IN VOID
*Context OPTIONAL
1033 IP6_LINK_TX_TOKEN
*Token
;
1035 IP6_NEIGHBOR_ENTRY
*ArpQue
;
1038 IpSb
= Interface
->Service
;
1039 NET_CHECK_SIGNATURE (IpSb
, IP6_SERVICE_SIGNATURE
);
1042 // Cancel all the pending frames on ARP requests
1044 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &Interface
->ArpQues
) {
1045 ArpQue
= NET_LIST_USER_STRUCT (Entry
, IP6_NEIGHBOR_ENTRY
, ArpList
);
1047 Status
= Ip6FreeNeighborEntry (
1056 ASSERT_EFI_ERROR (Status
);
1060 // Cancel all the frames that have been delivered to MNP
1061 // but not yet recycled.
1063 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &Interface
->SentFrames
) {
1064 Token
= NET_LIST_USER_STRUCT (Entry
, IP6_LINK_TX_TOKEN
, Link
);
1066 if ((FrameToCancel
== NULL
) || FrameToCancel (Token
, Context
)) {
1067 IpSb
->Mnp
->Cancel (IpSb
->Mnp
, &Token
->MnpToken
);
1073 Cancel the Packet and all its fragments.
1075 @param[in] IpIf The interface from which the Packet is sent.
1076 @param[in] Packet The Packet to cancel.
1077 @param[in] IoStatus The status returns to the sender.
1082 IN IP6_INTERFACE
*IpIf
,
1084 IN EFI_STATUS IoStatus
1087 Ip6CancelFrames (IpIf
, IoStatus
, Ip6CancelPacketFragments
, Packet
);