4 Copyright (c) 2005 - 2020, Intel Corporation. All rights reserved.<BR>
5 (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>
7 SPDX-License-Identifier: BSD-2-Clause-Patent
15 Create an empty assemble entry for the packet identified by
16 (Dst, Src, Id, Protocol). The default life for the packet is
19 @param[in] Dst The destination address
20 @param[in] Src The source address
21 @param[in] Id The ID field in IP header
22 @param[in] Protocol The protocol field in IP header
24 @return NULL if failed to allocate memory for the entry, otherwise
25 the point to just created reassemble entry.
29 Ip4CreateAssembleEntry (
37 IP4_ASSEMBLE_ENTRY
*Assemble
;
39 Assemble
= AllocatePool (sizeof (IP4_ASSEMBLE_ENTRY
));
41 if (Assemble
== NULL
) {
45 InitializeListHead (&Assemble
->Link
);
46 InitializeListHead (&Assemble
->Fragments
);
51 Assemble
->Protocol
= Protocol
;
52 Assemble
->TotalLen
= 0;
54 Assemble
->Head
= NULL
;
55 Assemble
->Info
= NULL
;
56 Assemble
->Life
= IP4_FRAGMENT_LIFE
;
63 Release all the fragments of a packet, then free the assemble entry.
65 @param[in] Assemble The assemble entry to free
69 Ip4FreeAssembleEntry (
70 IN IP4_ASSEMBLE_ENTRY
*Assemble
77 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &Assemble
->Fragments
) {
78 Fragment
= NET_LIST_USER_STRUCT (Entry
, NET_BUF
, List
);
80 RemoveEntryList (Entry
);
81 NetbufFree (Fragment
);
89 Initialize an already allocated assemble table. This is generally
90 the assemble table embedded in the IP4 service instance.
92 @param[in, out] Table The assemble table to initialize.
96 Ip4InitAssembleTable (
97 IN OUT IP4_ASSEMBLE_TABLE
*Table
102 for (Index
= 0; Index
< IP4_ASSEMLE_HASH_SIZE
; Index
++) {
103 InitializeListHead (&Table
->Bucket
[Index
]);
109 Clean up the assemble table: remove all the fragments
110 and assemble entries.
112 @param[in] Table The assemble table to clean up
116 Ip4CleanAssembleTable (
117 IN IP4_ASSEMBLE_TABLE
*Table
122 IP4_ASSEMBLE_ENTRY
*Assemble
;
125 for (Index
= 0; Index
< IP4_ASSEMLE_HASH_SIZE
; Index
++) {
126 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &Table
->Bucket
[Index
]) {
127 Assemble
= NET_LIST_USER_STRUCT (Entry
, IP4_ASSEMBLE_ENTRY
, Link
);
129 RemoveEntryList (Entry
);
130 Ip4FreeAssembleEntry (Assemble
);
137 Trim the packet to fit in [Start, End), and update the per
140 @param Packet Packet to trim
141 @param Start The sequence of the first byte to fit in
142 @param End One beyond the sequence of last byte to fit in.
147 IN OUT NET_BUF
*Packet
,
155 Info
= IP4_GET_CLIP_INFO (Packet
);
157 ASSERT (Info
->Start
+ Info
->Length
== Info
->End
);
158 ASSERT ((Info
->Start
< End
) && (Start
< Info
->End
));
160 if (Info
->Start
< Start
) {
161 Len
= Start
- Info
->Start
;
163 NetbufTrim (Packet
, (UINT32
) Len
, NET_BUF_HEAD
);
168 if (End
< Info
->End
) {
169 Len
= End
- Info
->End
;
171 NetbufTrim (Packet
, (UINT32
) Len
, NET_BUF_TAIL
);
179 Release all the fragments of the packet. This is the callback for
180 the assembled packet's OnFree. It will free the assemble entry,
181 which in turn will free all the fragments of the packet.
183 @param[in] Arg The assemble entry to free
192 Ip4FreeAssembleEntry ((IP4_ASSEMBLE_ENTRY
*) Arg
);
197 Reassemble the IP fragments. If all the fragments of the packet
198 have been received, it will wrap the packet in a net buffer then
199 return it to caller. If the packet can't be assembled, NULL is
202 @param Table The assemble table used. New assemble entry will be created
203 if the Packet is from a new chain of fragments.
204 @param Packet The fragment to assemble. It might be freed if the fragment
205 can't be re-assembled.
207 @return NULL if the packet can't be reassemble. The point to just assembled
208 packet if all the fragments of the packet have arrived.
213 IN OUT IP4_ASSEMBLE_TABLE
*Table
,
214 IN OUT NET_BUF
*Packet
220 IP4_ASSEMBLE_ENTRY
*Assemble
;
228 IpHead
= Packet
->Ip
.Ip4
;
229 This
= IP4_GET_CLIP_INFO (Packet
);
231 ASSERT (IpHead
!= NULL
);
234 // First: find the related assemble entry
237 Index
= IP4_ASSEMBLE_HASH (IpHead
->Dst
, IpHead
->Src
, IpHead
->Id
, IpHead
->Protocol
);
239 NET_LIST_FOR_EACH (Cur
, &Table
->Bucket
[Index
]) {
240 Assemble
= NET_LIST_USER_STRUCT (Cur
, IP4_ASSEMBLE_ENTRY
, Link
);
242 if ((Assemble
->Dst
== IpHead
->Dst
) && (Assemble
->Src
== IpHead
->Src
) &&
243 (Assemble
->Id
== IpHead
->Id
) && (Assemble
->Protocol
== IpHead
->Protocol
)) {
249 // Create a new assemble entry if no assemble entry is related to this packet
251 if (Cur
== &Table
->Bucket
[Index
]) {
252 Assemble
= Ip4CreateAssembleEntry (
259 if (Assemble
== NULL
) {
263 InsertHeadList (&Table
->Bucket
[Index
], &Assemble
->Link
);
266 // Assemble shouldn't be NULL here
268 ASSERT (Assemble
!= NULL
);
271 // Find the point to insert the packet: before the first
272 // fragment with THIS.Start < CUR.Start. the previous one
273 // has PREV.Start <= THIS.Start < CUR.Start.
275 Head
= &Assemble
->Fragments
;
277 NET_LIST_FOR_EACH (Cur
, Head
) {
278 Fragment
= NET_LIST_USER_STRUCT (Cur
, NET_BUF
, List
);
280 if (This
->Start
< IP4_GET_CLIP_INFO (Fragment
)->Start
) {
286 // Check whether the current fragment overlaps with the previous one.
287 // It holds that: PREV.Start <= THIS.Start < THIS.End. Only need to
288 // check whether THIS.Start < PREV.End for overlap. If two fragments
289 // overlaps, trim the overlapped part off THIS fragment.
291 if ((Prev
= Cur
->BackLink
) != Head
) {
292 Fragment
= NET_LIST_USER_STRUCT (Prev
, NET_BUF
, List
);
293 Node
= IP4_GET_CLIP_INFO (Fragment
);
295 if (This
->Start
< Node
->End
) {
296 if (This
->End
<= Node
->End
) {
301 Ip4TrimPacket (Packet
, Node
->End
, This
->End
);
306 // Insert the fragment into the packet. The fragment may be removed
307 // from the list by the following checks.
309 NetListInsertBefore (Cur
, &Packet
->List
);
312 // Check the packets after the insert point. It holds that:
313 // THIS.Start <= NODE.Start < NODE.End. The equality holds
314 // if PREV and NEXT are continuous. THIS fragment may fill
315 // several holes. Remove the completely overlapped fragments
317 while (Cur
!= Head
) {
318 Fragment
= NET_LIST_USER_STRUCT (Cur
, NET_BUF
, List
);
319 Node
= IP4_GET_CLIP_INFO (Fragment
);
322 // Remove fragments completely overlapped by this fragment
324 if (Node
->End
<= This
->End
) {
325 Cur
= Cur
->ForwardLink
;
327 RemoveEntryList (&Fragment
->List
);
328 Assemble
->CurLen
-= Node
->Length
;
330 NetbufFree (Fragment
);
335 // The conditions are: THIS.Start <= NODE.Start, and THIS.End <
336 // NODE.End. Two fragments overlaps if NODE.Start < THIS.End.
337 // If two fragments start at the same offset, remove THIS fragment
338 // because ((THIS.Start == NODE.Start) && (THIS.End < NODE.End)).
340 if (Node
->Start
< This
->End
) {
341 if (This
->Start
== Node
->Start
) {
342 RemoveEntryList (&Packet
->List
);
346 Ip4TrimPacket (Packet
, This
->Start
, Node
->Start
);
353 // Update the assemble info: increase the current length. If it is
354 // the frist fragment, update the packet's IP head and per packet
355 // info. If it is the last fragment, update the total length.
357 Assemble
->CurLen
+= This
->Length
;
359 if (This
->Start
== 0) {
361 // Once the first fragment is enqueued, it can't be removed
362 // from the fragment list. So, Assemble->Head always point
363 // to valid memory area.
365 ASSERT (Assemble
->Head
== NULL
);
367 Assemble
->Head
= IpHead
;
368 Assemble
->Info
= IP4_GET_CLIP_INFO (Packet
);
372 // Don't update the length more than once.
374 if (IP4_LAST_FRAGMENT (IpHead
->Fragment
) && (Assemble
->TotalLen
== 0)) {
375 Assemble
->TotalLen
= This
->End
;
379 // Deliver the whole packet if all the fragments received.
380 // All fragments received if:
381 // 1. received the last one, so, the total length is know
382 // 2. received all the data. If the last fragment on the
383 // queue ends at the total length, all data is received.
385 if ((Assemble
->TotalLen
!= 0) && (Assemble
->CurLen
>= Assemble
->TotalLen
)) {
387 RemoveEntryList (&Assemble
->Link
);
390 // If the packet is properly formatted, the last fragment's End
391 // equals to the packet's total length. Otherwise, the packet
392 // is a fake, drop it now.
394 Fragment
= NET_LIST_USER_STRUCT (Head
->BackLink
, NET_BUF
, List
);
396 if (IP4_GET_CLIP_INFO (Fragment
)->End
!= Assemble
->TotalLen
) {
397 Ip4FreeAssembleEntry (Assemble
);
402 // Wrap the packet in a net buffer then deliver it up
404 NewPacket
= NetbufFromBufList (
405 &Assemble
->Fragments
,
412 if (NewPacket
== NULL
) {
413 Ip4FreeAssembleEntry (Assemble
);
417 NewPacket
->Ip
.Ip4
= Assemble
->Head
;
419 ASSERT (Assemble
->Info
!= NULL
);
422 IP4_GET_CLIP_INFO (NewPacket
),
424 sizeof (*IP4_GET_CLIP_INFO (NewPacket
))
438 The callback function for the net buffer which wraps the packet processed by
439 IPsec. It releases the wrap packet and also signals IPsec to free the resources.
441 @param[in] Arg The wrap context
450 IP4_IPSEC_WRAP
*Wrap
;
452 Wrap
= (IP4_IPSEC_WRAP
*) Arg
;
454 if (Wrap
->IpSecRecycleSignal
!= NULL
) {
455 gBS
->SignalEvent (Wrap
->IpSecRecycleSignal
);
458 NetbufFree (Wrap
->Packet
);
466 The work function to locate IPsec protocol to process the inbound or
467 outbound IP packets. The process routine handls the packet with following
468 actions: bypass the packet, discard the packet, or protect the packet.
470 @param[in] IpSb The IP4 service instance.
471 @param[in, out] Head The caller supplied IP4 header.
472 @param[in, out] Netbuf The IP4 packet to be processed by IPsec.
473 @param[in, out] Options The caller supplied options.
474 @param[in, out] OptionsLen The length of the option.
475 @param[in] Direction The directionality in an SPD entry,
476 EfiIPsecInBound or EfiIPsecOutBound.
477 @param[in] Context The token's wrap.
479 @retval EFI_SUCCESS The IPsec protocol is not available or disabled.
480 @retval EFI_SUCCESS The packet was bypassed and all buffers remain the same.
481 @retval EFI_SUCCESS The packet was protected.
482 @retval EFI_ACCESS_DENIED The packet was discarded.
483 @retval EFI_OUT_OF_RESOURCES There is no sufficient resource to complete the operation.
484 @retval EFI_BUFFER_TOO_SMALL The number of non-empty block is bigger than the
485 number of input data blocks when build a fragment table.
489 Ip4IpSecProcessPacket (
490 IN IP4_SERVICE
*IpSb
,
491 IN OUT IP4_HEAD
**Head
,
492 IN OUT NET_BUF
**Netbuf
,
493 IN OUT UINT8
**Options
,
494 IN OUT UINT32
*OptionsLen
,
495 IN EFI_IPSEC_TRAFFIC_DIR Direction
,
499 NET_FRAGMENT
*FragmentTable
;
500 NET_FRAGMENT
*OriginalFragmentTable
;
501 UINT32 FragmentCount
;
502 UINT32 OriginalFragmentCount
;
503 EFI_EVENT RecycleEvent
;
505 IP4_TXTOKEN_WRAP
*TxWrap
;
506 IP4_IPSEC_WRAP
*IpSecWrap
;
510 Status
= EFI_SUCCESS
;
512 if (!mIpSec2Installed
) {
515 ASSERT (mIpSec
!= NULL
);
520 FragmentTable
= NULL
;
521 TxWrap
= (IP4_TXTOKEN_WRAP
*) Context
;
522 FragmentCount
= Packet
->BlockOpNum
;
524 ZeroMem (&ZeroHead
, sizeof (IP4_HEAD
));
527 // Check whether the IPsec enable variable is set.
529 if (mIpSec
->DisabledFlag
) {
531 // If IPsec is disabled, restore the original MTU
533 IpSb
->MaxPacketSize
= IpSb
->OldMaxPacketSize
;
537 // If IPsec is enabled, use the MTU which reduce the IPsec header length.
539 IpSb
->MaxPacketSize
= IpSb
->OldMaxPacketSize
- IP4_MAX_IPSEC_HEADLEN
;
543 // Rebuild fragment table from netbuf to ease IPsec process.
545 FragmentTable
= AllocateZeroPool (FragmentCount
* sizeof (NET_FRAGMENT
));
547 if (FragmentTable
== NULL
) {
548 Status
= EFI_OUT_OF_RESOURCES
;
552 Status
= NetbufBuildExt (Packet
, FragmentTable
, &FragmentCount
);
555 // Record the original FragmentTable and count.
557 OriginalFragmentTable
= FragmentTable
;
558 OriginalFragmentCount
= FragmentCount
;
560 if (EFI_ERROR (Status
)) {
561 FreePool (FragmentTable
);
566 // Convert host byte order to network byte order
570 Status
= mIpSec
->ProcessExt (
578 (EFI_IPSEC_FRAGMENT_DATA
**) (&FragmentTable
),
584 // Convert back to host byte order
588 if (EFI_ERROR (Status
)) {
589 FreePool (OriginalFragmentTable
);
593 if (OriginalFragmentTable
== FragmentTable
&& OriginalFragmentCount
== FragmentCount
) {
597 FreePool (FragmentTable
);
601 // Free the FragmentTable which allocated before calling the IPsec.
603 FreePool (OriginalFragmentTable
);
606 if (Direction
== EfiIPsecOutBound
&& TxWrap
!= NULL
) {
608 TxWrap
->IpSecRecycleSignal
= RecycleEvent
;
609 TxWrap
->Packet
= NetbufFromExt (
617 if (TxWrap
->Packet
== NULL
) {
619 // Recover the TxWrap->Packet, if meet a error, and the caller will free
622 TxWrap
->Packet
= *Netbuf
;
623 Status
= EFI_OUT_OF_RESOURCES
;
628 // Free original Netbuf.
630 NetIpSecNetbufFree (*Netbuf
);
631 *Netbuf
= TxWrap
->Packet
;
635 IpSecWrap
= AllocateZeroPool (sizeof (IP4_IPSEC_WRAP
));
637 if (IpSecWrap
== NULL
) {
638 Status
= EFI_OUT_OF_RESOURCES
;
639 gBS
->SignalEvent (RecycleEvent
);
643 IpSecWrap
->IpSecRecycleSignal
= RecycleEvent
;
644 IpSecWrap
->Packet
= Packet
;
645 Packet
= NetbufFromExt (
654 if (Packet
== NULL
) {
655 Packet
= IpSecWrap
->Packet
;
656 gBS
->SignalEvent (RecycleEvent
);
657 FreePool (IpSecWrap
);
658 Status
= EFI_OUT_OF_RESOURCES
;
662 if (Direction
== EfiIPsecInBound
&& 0 != CompareMem (*Head
, &ZeroHead
, sizeof (IP4_HEAD
))) {
663 Ip4PrependHead (Packet
, *Head
, *Options
, *OptionsLen
);
664 Ip4NtohHead (Packet
->Ip
.Ip4
);
665 NetbufTrim (Packet
, ((*Head
)->HeadLen
<< 2), TRUE
);
668 IP4_GET_CLIP_INFO (Packet
),
669 IP4_GET_CLIP_INFO (IpSecWrap
->Packet
),
670 sizeof (IP4_CLIP_INFO
)
681 Pre-process the IPv4 packet. First validates the IPv4 packet, and
682 then reassembles packet if it is necessary.
684 @param[in] IpSb Pointer to IP4_SERVICE.
685 @param[in, out] Packet Pointer to the Packet to be processed.
686 @param[in] Head Pointer to the IP4_HEAD.
687 @param[in] Option Pointer to a buffer which contains the IPv4 option.
688 @param[in] OptionLen The length of Option in bytes.
689 @param[in] Flag The link layer flag for the packet received, such
692 @retval EFI_SUCCESS The received packet is in well form.
693 @retval EFI_INVALID_PARAMETER The received packet is malformed.
697 Ip4PreProcessPacket (
698 IN IP4_SERVICE
*IpSb
,
699 IN OUT NET_BUF
**Packet
,
712 // Check if the IP4 header is correctly formatted.
714 HeadLen
= (Head
->HeadLen
<< 2);
715 TotalLen
= NTOHS (Head
->TotalLen
);
718 // Mnp may deliver frame trailer sequence up, trim it off.
720 if (TotalLen
< (*Packet
)->TotalSize
) {
721 NetbufTrim (*Packet
, (*Packet
)->TotalSize
- TotalLen
, FALSE
);
724 if ((Head
->Ver
!= 4) || (HeadLen
< IP4_MIN_HEADLEN
) ||
725 (TotalLen
< HeadLen
) || (TotalLen
!= (*Packet
)->TotalSize
)) {
726 return EFI_INVALID_PARAMETER
;
730 // Some OS may send IP packets without checksum.
732 Checksum
= (UINT16
) (~NetblockChecksum ((UINT8
*) Head
, HeadLen
));
734 if ((Head
->Checksum
!= 0) && (Checksum
!= 0)) {
735 return EFI_INVALID_PARAMETER
;
739 // Convert the IP header to host byte order, then get the per packet info.
741 (*Packet
)->Ip
.Ip4
= Ip4NtohHead (Head
);
743 Info
= IP4_GET_CLIP_INFO (*Packet
);
744 Info
->LinkFlag
= Flag
;
745 Info
->CastType
= Ip4GetHostCast (IpSb
, Head
->Dst
, Head
->Src
);
746 Info
->Start
= (Head
->Fragment
& IP4_HEAD_OFFSET_MASK
) << 3;
747 Info
->Length
= Head
->TotalLen
- HeadLen
;
748 Info
->End
= Info
->Start
+ Info
->Length
;
749 Info
->Status
= EFI_SUCCESS
;
752 // The packet is destinated to us if the CastType is non-zero.
754 if ((Info
->CastType
== 0) || (Info
->End
> IP4_MAX_PACKET_SIZE
)) {
755 return EFI_INVALID_PARAMETER
;
759 // Validate the options. Don't call the Ip4OptionIsValid if
760 // there is no option to save some CPU process.
763 if ((OptionLen
> 0) && !Ip4OptionIsValid (Option
, OptionLen
, TRUE
)) {
764 return EFI_INVALID_PARAMETER
;
768 // Trim the head off, after this point, the packet is headless,
769 // and Packet->TotalLen == Info->Length.
771 NetbufTrim (*Packet
, HeadLen
, TRUE
);
774 // Reassemble the packet if this is a fragment. The packet is a
775 // fragment if its head has MF (more fragment) set, or it starts
778 if (((Head
->Fragment
& IP4_HEAD_MF_MASK
) != 0) || (Info
->Start
!= 0)) {
780 // Drop the fragment if DF is set but it is fragmented. Gateway
781 // need to send a type 4 destination unreache ICMP message here.
783 if ((Head
->Fragment
& IP4_HEAD_DF_MASK
) != 0) {
784 return EFI_INVALID_PARAMETER
;
788 // The length of all but the last fragments is in the unit of 8 bytes.
790 if (((Head
->Fragment
& IP4_HEAD_MF_MASK
) != 0) && (Info
->Length
% 8 != 0)) {
791 return EFI_INVALID_PARAMETER
;
794 *Packet
= Ip4Reassemble (&IpSb
->Assemble
, *Packet
);
797 // Packet assembly isn't complete, start receive more packet.
799 if (*Packet
== NULL
) {
800 return EFI_INVALID_PARAMETER
;
808 This function checks the IPv4 packet length.
810 @param[in] Packet Pointer to the IPv4 Packet to be checked.
812 @retval TRUE The input IPv4 packet length is valid.
813 @retval FALSE The input IPv4 packet length is invalid.
817 Ip4IsValidPacketLength (
822 // Check the IP4 packet length.
824 if (Packet
->TotalSize
< IP4_MIN_HEADLEN
) {
832 The IP4 input routine. It is called by the IP4_INTERFACE when a
833 IP4 fragment is received from MNP.
835 @param[in] Ip4Instance The IP4 child that request the receive, most like
837 @param[in] Packet The IP4 packet received.
838 @param[in] IoStatus The return status of receive request.
839 @param[in] Flag The link layer flag for the packet received, such
841 @param[in] Context The IP4 service instance that own the MNP.
846 IN IP4_PROTOCOL
*Ip4Instance
,
848 IN EFI_STATUS IoStatus
,
860 IpSb
= (IP4_SERVICE
*) Context
;
863 if (EFI_ERROR (IoStatus
) || (IpSb
->State
== IP4_SERVICE_DESTROY
)) {
867 if (!Ip4IsValidPacketLength (Packet
)) {
871 Head
= (IP4_HEAD
*) NetbufGetByte (Packet
, 0, NULL
);
872 ASSERT (Head
!= NULL
);
873 OptionLen
= (Head
->HeadLen
<< 2) - IP4_MIN_HEADLEN
;
875 Option
= (UINT8
*) (Head
+ 1);
879 // Validate packet format and reassemble packet if it is necessary.
881 Status
= Ip4PreProcessPacket (
890 if (EFI_ERROR (Status
)) {
895 // After trim off, the packet is a esp/ah/udp/tcp/icmp6 net buffer,
896 // and no need consider any other ahead ext headers.
898 Status
= Ip4IpSecProcessPacket (
908 if (EFI_ERROR (Status
)) {
913 // If the packet is protected by tunnel mode, parse the inner Ip Packet.
915 ZeroMem (&ZeroHead
, sizeof (IP4_HEAD
));
916 if (0 == CompareMem (Head
, &ZeroHead
, sizeof (IP4_HEAD
))) {
917 // Packet may have been changed. Head, HeadLen, TotalLen, and
918 // info must be reloaded before use. The ownership of the packet
919 // is transferred to the packet process logic.
921 if (!Ip4IsValidPacketLength (Packet
)) {
925 Head
= (IP4_HEAD
*) NetbufGetByte (Packet
, 0, NULL
);
926 ASSERT (Head
!= NULL
);
927 Status
= Ip4PreProcessPacket (
935 if (EFI_ERROR (Status
)) {
940 ASSERT (Packet
!= NULL
);
941 Head
= Packet
->Ip
.Ip4
;
942 IP4_GET_CLIP_INFO (Packet
)->Status
= EFI_SUCCESS
;
944 switch (Head
->Protocol
) {
945 case EFI_IP_PROTO_ICMP
:
946 Ip4IcmpHandle (IpSb
, Head
, Packet
);
950 Ip4IgmpHandle (IpSb
, Head
, Packet
);
954 Ip4Demultiplex (IpSb
, Head
, Packet
, Option
, OptionLen
);
960 // Dispatch the DPCs queued by the NotifyFunction of the rx token's events
961 // which are signaled with received data.
966 Ip4ReceiveFrame (IpSb
->DefaultInterface
, NULL
, Ip4AccpetFrame
, IpSb
);
969 if (Packet
!= NULL
) {
978 Check whether this IP child accepts the packet.
980 @param[in] IpInstance The IP child to check
981 @param[in] Head The IP header of the packet
982 @param[in] Packet The data of the packet
984 @retval TRUE If the child wants to receive the packet.
985 @retval FALSE Otherwise.
989 Ip4InstanceFrameAcceptable (
990 IN IP4_PROTOCOL
*IpInstance
,
995 IP4_ICMP_ERROR_HEAD Icmp
;
996 EFI_IP4_CONFIG_DATA
*Config
;
1001 Config
= &IpInstance
->ConfigData
;
1004 // Dirty trick for the Tiano UEFI network stack implementation. If
1005 // ReceiveTimeout == -1, the receive of the packet for this instance
1006 // is disabled. The UEFI spec don't have such capability. We add
1007 // this to improve the performance because IP will make a copy of
1008 // the received packet for each accepting instance. Some IP instances
1009 // used by UDP/TCP only send packets, they don't wants to receive.
1011 if (Config
->ReceiveTimeout
== (UINT32
)(-1)) {
1015 if (Config
->AcceptPromiscuous
) {
1020 // Use protocol from the IP header embedded in the ICMP error
1021 // message to filter, instead of ICMP itself. ICMP handle will
1022 // call Ip4Demultiplex to deliver ICMP errors.
1024 Proto
= Head
->Protocol
;
1026 if ((Proto
== EFI_IP_PROTO_ICMP
) && (!Config
->AcceptAnyProtocol
) && (Proto
!= Config
->DefaultProtocol
)) {
1027 NetbufCopy (Packet
, 0, sizeof (Icmp
.Head
), (UINT8
*) &Icmp
.Head
);
1029 if (mIcmpClass
[Icmp
.Head
.Type
].IcmpClass
== ICMP_ERROR_MESSAGE
) {
1030 if (!Config
->AcceptIcmpErrors
) {
1034 NetbufCopy (Packet
, 0, sizeof (Icmp
), (UINT8
*) &Icmp
);
1035 Proto
= Icmp
.IpHead
.Protocol
;
1040 // Match the protocol
1042 if (!Config
->AcceptAnyProtocol
&& (Proto
!= Config
->DefaultProtocol
)) {
1047 // Check for broadcast, the caller has computed the packet's
1048 // cast type for this child's interface.
1050 Info
= IP4_GET_CLIP_INFO (Packet
);
1052 if (IP4_IS_BROADCAST (Info
->CastType
)) {
1053 return Config
->AcceptBroadcast
;
1057 // If it is a multicast packet, check whether we are in the group.
1059 if (Info
->CastType
== IP4_MULTICAST
) {
1061 // Receive the multicast if the instance wants to receive all packets.
1063 if (!IpInstance
->ConfigData
.UseDefaultAddress
&& (IpInstance
->Interface
->Ip
== 0)) {
1067 for (Index
= 0; Index
< IpInstance
->GroupCount
; Index
++) {
1068 if (IpInstance
->Groups
[Index
] == HTONL (Head
->Dst
)) {
1073 return (BOOLEAN
)(Index
< IpInstance
->GroupCount
);
1081 Enqueue a shared copy of the packet to the IP4 child if the
1082 packet is acceptable to it. Here the data of the packet is
1083 shared, but the net buffer isn't.
1085 @param[in] IpInstance The IP4 child to enqueue the packet to
1086 @param[in] Head The IP header of the received packet
1087 @param[in] Packet The data of the received packet
1089 @retval EFI_NOT_STARTED The IP child hasn't been configured.
1090 @retval EFI_INVALID_PARAMETER The child doesn't want to receive the packet
1091 @retval EFI_OUT_OF_RESOURCES Failed to allocate some resource
1092 @retval EFI_SUCCESS A shared copy the packet is enqueued to the child.
1096 Ip4InstanceEnquePacket (
1097 IN IP4_PROTOCOL
*IpInstance
,
1102 IP4_CLIP_INFO
*Info
;
1106 // Check whether the packet is acceptable to this instance.
1108 if (IpInstance
->State
!= IP4_STATE_CONFIGED
) {
1109 return EFI_NOT_STARTED
;
1112 if (!Ip4InstanceFrameAcceptable (IpInstance
, Head
, Packet
)) {
1113 return EFI_INVALID_PARAMETER
;
1117 // Enqueue a shared copy of the packet.
1119 Clone
= NetbufClone (Packet
);
1121 if (Clone
== NULL
) {
1122 return EFI_OUT_OF_RESOURCES
;
1126 // Set the receive time out for the assembled packet. If it expires,
1127 // packet will be removed from the queue.
1129 Info
= IP4_GET_CLIP_INFO (Clone
);
1130 Info
->Life
= IP4_US_TO_SEC (IpInstance
->ConfigData
.ReceiveTimeout
);
1132 InsertTailList (&IpInstance
->Received
, &Clone
->List
);
1138 The signal handle of IP4's recycle event. It is called back
1139 when the upper layer release the packet.
1141 @param Event The IP4's recycle event.
1142 @param Context The context of the handle, which is a
1148 Ip4OnRecyclePacket (
1153 IP4_RXDATA_WRAP
*Wrap
;
1155 Wrap
= (IP4_RXDATA_WRAP
*) Context
;
1157 EfiAcquireLockOrFail (&Wrap
->IpInstance
->RecycleLock
);
1158 RemoveEntryList (&Wrap
->Link
);
1159 EfiReleaseLock (&Wrap
->IpInstance
->RecycleLock
);
1161 ASSERT (!NET_BUF_SHARED (Wrap
->Packet
));
1162 NetbufFree (Wrap
->Packet
);
1164 gBS
->CloseEvent (Wrap
->RxData
.RecycleSignal
);
1170 Wrap the received packet to a IP4_RXDATA_WRAP, which will be
1171 delivered to the upper layer. Each IP4 child that accepts the
1172 packet will get a not-shared copy of the packet which is wrapped
1173 in the IP4_RXDATA_WRAP. The IP4_RXDATA_WRAP->RxData is passed
1174 to the upper layer. Upper layer will signal the recycle event in
1175 it when it is done with the packet.
1177 @param[in] IpInstance The IP4 child to receive the packet.
1178 @param[in] Packet The packet to deliver up.
1180 @retval Wrap if warp the packet succeed.
1181 @retval NULL failed to wrap the packet .
1186 IN IP4_PROTOCOL
*IpInstance
,
1190 IP4_RXDATA_WRAP
*Wrap
;
1191 EFI_IP4_RECEIVE_DATA
*RxData
;
1195 Wrap
= AllocatePool (IP4_RXDATA_WRAP_SIZE (Packet
->BlockOpNum
));
1201 InitializeListHead (&Wrap
->Link
);
1203 Wrap
->IpInstance
= IpInstance
;
1204 Wrap
->Packet
= Packet
;
1205 RxData
= &Wrap
->RxData
;
1207 ZeroMem (RxData
, sizeof (EFI_IP4_RECEIVE_DATA
));
1209 Status
= gBS
->CreateEvent (
1214 &RxData
->RecycleSignal
1217 if (EFI_ERROR (Status
)) {
1222 ASSERT (Packet
->Ip
.Ip4
!= NULL
);
1224 ASSERT (IpInstance
!= NULL
);
1225 RawData
= IpInstance
->ConfigData
.RawData
;
1228 // The application expects a network byte order header.
1231 RxData
->HeaderLength
= (Packet
->Ip
.Ip4
->HeadLen
<< 2);
1232 RxData
->Header
= (EFI_IP4_HEADER
*) Ip4NtohHead (Packet
->Ip
.Ip4
);
1233 RxData
->OptionsLength
= RxData
->HeaderLength
- IP4_MIN_HEADLEN
;
1234 RxData
->Options
= NULL
;
1236 if (RxData
->OptionsLength
!= 0) {
1237 RxData
->Options
= (VOID
*) (RxData
->Header
+ 1);
1241 RxData
->DataLength
= Packet
->TotalSize
;
1244 // Build the fragment table to be delivered up.
1246 RxData
->FragmentCount
= Packet
->BlockOpNum
;
1247 NetbufBuildExt (Packet
, (NET_FRAGMENT
*) RxData
->FragmentTable
, &RxData
->FragmentCount
);
1254 Deliver the received packets to upper layer if there are both received
1255 requests and enqueued packets. If the enqueued packet is shared, it will
1256 duplicate it to a non-shared packet, release the shared packet, then
1257 deliver the non-shared packet up.
1259 @param[in] IpInstance The IP child to deliver the packet up.
1261 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources to deliver the
1263 @retval EFI_SUCCESS All the enqueued packets that can be delivered
1268 Ip4InstanceDeliverPacket (
1269 IN IP4_PROTOCOL
*IpInstance
1272 EFI_IP4_COMPLETION_TOKEN
*Token
;
1273 IP4_RXDATA_WRAP
*Wrap
;
1280 // Deliver a packet if there are both a packet and a receive token.
1282 while (!IsListEmpty (&IpInstance
->Received
) &&
1283 !NetMapIsEmpty (&IpInstance
->RxTokens
)) {
1285 Packet
= NET_LIST_HEAD (&IpInstance
->Received
, NET_BUF
, List
);
1287 if (!NET_BUF_SHARED (Packet
)) {
1289 // If this is the only instance that wants the packet, wrap it up.
1291 Wrap
= Ip4WrapRxData (IpInstance
, Packet
);
1294 return EFI_OUT_OF_RESOURCES
;
1297 RemoveEntryList (&Packet
->List
);
1301 // Create a duplicated packet if this packet is shared
1303 if (IpInstance
->ConfigData
.RawData
) {
1306 HeadLen
= IP4_MAX_HEADLEN
;
1309 Dup
= NetbufDuplicate (Packet
, NULL
, HeadLen
);
1312 return EFI_OUT_OF_RESOURCES
;
1315 if (!IpInstance
->ConfigData
.RawData
) {
1317 // Copy the IP head over. The packet to deliver up is
1318 // headless. Trim the head off after copy. The IP head
1319 // may be not continuous before the data.
1321 Head
= NetbufAllocSpace (Dup
, IP4_MAX_HEADLEN
, NET_BUF_HEAD
);
1322 ASSERT (Head
!= NULL
);
1324 Dup
->Ip
.Ip4
= (IP4_HEAD
*) Head
;
1326 CopyMem (Head
, Packet
->Ip
.Ip4
, Packet
->Ip
.Ip4
->HeadLen
<< 2);
1327 NetbufTrim (Dup
, IP4_MAX_HEADLEN
, TRUE
);
1330 Wrap
= Ip4WrapRxData (IpInstance
, Dup
);
1334 return EFI_OUT_OF_RESOURCES
;
1337 RemoveEntryList (&Packet
->List
);
1338 NetbufFree (Packet
);
1344 // Insert it into the delivered packet, then get a user's
1345 // receive token, pass the wrapped packet up.
1347 EfiAcquireLockOrFail (&IpInstance
->RecycleLock
);
1348 InsertHeadList (&IpInstance
->Delivered
, &Wrap
->Link
);
1349 EfiReleaseLock (&IpInstance
->RecycleLock
);
1351 Token
= NetMapRemoveHead (&IpInstance
->RxTokens
, NULL
);
1352 Token
->Status
= IP4_GET_CLIP_INFO (Packet
)->Status
;
1353 Token
->Packet
.RxData
= &Wrap
->RxData
;
1355 gBS
->SignalEvent (Token
->Event
);
1363 Enqueue a received packet to all the IP children that share
1366 @param[in] IpSb The IP4 service instance that receive the packet.
1367 @param[in] Head The header of the received packet.
1368 @param[in] Packet The data of the received packet.
1369 @param[in] Option Point to the IP4 packet header options.
1370 @param[in] OptionLen Length of the IP4 packet header options.
1371 @param[in] IpIf The interface to enqueue the packet to.
1373 @return The number of the IP4 children that accepts the packet
1377 Ip4InterfaceEnquePacket (
1378 IN IP4_SERVICE
*IpSb
,
1382 IN UINT32 OptionLen
,
1383 IN IP4_INTERFACE
*IpIf
1386 IP4_PROTOCOL
*IpInstance
;
1387 IP4_CLIP_INFO
*Info
;
1394 // First, check that the packet is acceptable to this interface
1395 // and find the local cast type for the interface. A packet sent
1396 // to say 192.168.1.1 should NOT be deliver to 10.0.0.1 unless
1397 // promiscuous receiving.
1400 Info
= IP4_GET_CLIP_INFO (Packet
);
1402 if ((Info
->CastType
== IP4_MULTICAST
) || (Info
->CastType
== IP4_LOCAL_BROADCAST
)) {
1404 // If the CastType is multicast, don't need to filter against
1405 // the group address here, Ip4InstanceFrameAcceptable will do
1408 LocalType
= Info
->CastType
;
1412 // Check the destination against local IP. If the station
1413 // address is 0.0.0.0, it means receiving all the IP destined
1414 // to local non-zero IP. Otherwise, it is necessary to compare
1415 // the destination to the interface's IP address.
1417 if (IpIf
->Ip
== IP4_ALLZERO_ADDRESS
) {
1418 LocalType
= IP4_LOCAL_HOST
;
1421 LocalType
= Ip4GetNetCast (Head
->Dst
, IpIf
);
1423 if ((LocalType
== 0) && IpIf
->PromiscRecv
) {
1424 LocalType
= IP4_PROMISCUOUS
;
1429 if (LocalType
== 0) {
1434 // Iterate through the ip instances on the interface, enqueue
1435 // the packet if filter passed. Save the original cast type,
1436 // and pass the local cast type to the IP children on the
1437 // interface. The global cast type will be restored later.
1439 SavedType
= Info
->CastType
;
1440 Info
->CastType
= LocalType
;
1444 NET_LIST_FOR_EACH (Entry
, &IpIf
->IpInstances
) {
1445 IpInstance
= NET_LIST_USER_STRUCT (Entry
, IP4_PROTOCOL
, AddrLink
);
1446 NET_CHECK_SIGNATURE (IpInstance
, IP4_PROTOCOL_SIGNATURE
);
1449 // In RawData mode, add IPv4 headers and options back to packet.
1451 if ((IpInstance
->ConfigData
.RawData
) && (Option
!= NULL
) && (OptionLen
!= 0)){
1452 Ip4PrependHead (Packet
, Head
, Option
, OptionLen
);
1455 if (Ip4InstanceEnquePacket (IpInstance
, Head
, Packet
) == EFI_SUCCESS
) {
1460 Info
->CastType
= SavedType
;
1466 Deliver the packet for each IP4 child on the interface.
1468 @param[in] IpSb The IP4 service instance that received the packet
1469 @param[in] IpIf The IP4 interface to deliver the packet.
1471 @retval EFI_SUCCESS It always returns EFI_SUCCESS now
1475 Ip4InterfaceDeliverPacket (
1476 IN IP4_SERVICE
*IpSb
,
1477 IN IP4_INTERFACE
*IpIf
1480 IP4_PROTOCOL
*Ip4Instance
;
1483 NET_LIST_FOR_EACH (Entry
, &IpIf
->IpInstances
) {
1484 Ip4Instance
= NET_LIST_USER_STRUCT (Entry
, IP4_PROTOCOL
, AddrLink
);
1485 Ip4InstanceDeliverPacket (Ip4Instance
);
1493 Demultiple the packet. the packet delivery is processed in two
1494 passes. The first pass will enqueue a shared copy of the packet
1495 to each IP4 child that accepts the packet. The second pass will
1496 deliver a non-shared copy of the packet to each IP4 child that
1497 has pending receive requests. Data is copied if more than one
1498 child wants to consume the packet because each IP child needs
1499 its own copy of the packet to make changes.
1501 @param[in] IpSb The IP4 service instance that received the packet.
1502 @param[in] Head The header of the received packet.
1503 @param[in] Packet The data of the received packet.
1504 @param[in] Option Point to the IP4 packet header options.
1505 @param[in] OptionLen Length of the IP4 packet header options.
1507 @retval EFI_NOT_FOUND No IP child accepts the packet.
1508 @retval EFI_SUCCESS The packet is enqueued or delivered to some IP
1514 IN IP4_SERVICE
*IpSb
,
1522 IP4_INTERFACE
*IpIf
;
1526 // Two pass delivery: first, enqueue a shared copy of the packet
1527 // to each instance that accept the packet.
1531 NET_LIST_FOR_EACH (Entry
, &IpSb
->Interfaces
) {
1532 IpIf
= NET_LIST_USER_STRUCT (Entry
, IP4_INTERFACE
, Link
);
1534 if (IpIf
->Configured
) {
1535 Enqueued
+= Ip4InterfaceEnquePacket (
1547 // Second: deliver a duplicate of the packet to each instance.
1548 // Release the local reference first, so that the last instance
1549 // getting the packet will not copy the data.
1551 NetbufFree (Packet
);
1553 if (Enqueued
== 0) {
1554 return EFI_NOT_FOUND
;
1557 NET_LIST_FOR_EACH (Entry
, &IpSb
->Interfaces
) {
1558 IpIf
= NET_LIST_USER_STRUCT (Entry
, IP4_INTERFACE
, Link
);
1560 if (IpIf
->Configured
) {
1561 Ip4InterfaceDeliverPacket (IpSb
, IpIf
);
1570 Timeout the fragment and enqueued packets.
1572 @param[in] IpSb The IP4 service instance to timeout
1576 Ip4PacketTimerTicking (
1577 IN IP4_SERVICE
*IpSb
1580 LIST_ENTRY
*InstanceEntry
;
1583 IP4_PROTOCOL
*IpInstance
;
1584 IP4_ASSEMBLE_ENTRY
*Assemble
;
1586 IP4_CLIP_INFO
*Info
;
1590 // First, time out the fragments. The packet's life is counting down
1591 // once the first-arrived fragment was received.
1593 for (Index
= 0; Index
< IP4_ASSEMLE_HASH_SIZE
; Index
++) {
1594 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &IpSb
->Assemble
.Bucket
[Index
]) {
1595 Assemble
= NET_LIST_USER_STRUCT (Entry
, IP4_ASSEMBLE_ENTRY
, Link
);
1597 if ((Assemble
->Life
> 0) && (--Assemble
->Life
== 0)) {
1598 RemoveEntryList (Entry
);
1599 Ip4FreeAssembleEntry (Assemble
);
1604 NET_LIST_FOR_EACH (InstanceEntry
, &IpSb
->Children
) {
1605 IpInstance
= NET_LIST_USER_STRUCT (InstanceEntry
, IP4_PROTOCOL
, Link
);
1608 // Second, time out the assembled packets enqueued on each IP child.
1610 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &IpInstance
->Received
) {
1611 Packet
= NET_LIST_USER_STRUCT (Entry
, NET_BUF
, List
);
1612 Info
= IP4_GET_CLIP_INFO (Packet
);
1614 if ((Info
->Life
> 0) && (--Info
->Life
== 0)) {
1615 RemoveEntryList (Entry
);
1616 NetbufFree (Packet
);
1621 // Third: time out the transmitted packets.
1623 NetMapIterate (&IpInstance
->TxTokens
, Ip4SentPacketTicking
, NULL
);