4 Copyright (c) 2005 - 2018, 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 formated, 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 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 suffcient 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 orginal 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_SEUCCESS The recieved packet is in well form.
693 @retval EFI_INVAILD_PARAMETER The recieved 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 if ((*Packet
)->TotalSize
< IP4_MIN_HEADLEN
) {
715 return EFI_INVALID_PARAMETER
;
718 HeadLen
= (Head
->HeadLen
<< 2);
719 TotalLen
= NTOHS (Head
->TotalLen
);
722 // Mnp may deliver frame trailer sequence up, trim it off.
724 if (TotalLen
< (*Packet
)->TotalSize
) {
725 NetbufTrim (*Packet
, (*Packet
)->TotalSize
- TotalLen
, FALSE
);
728 if ((Head
->Ver
!= 4) || (HeadLen
< IP4_MIN_HEADLEN
) ||
729 (TotalLen
< HeadLen
) || (TotalLen
!= (*Packet
)->TotalSize
)) {
730 return EFI_INVALID_PARAMETER
;
734 // Some OS may send IP packets without checksum.
736 Checksum
= (UINT16
) (~NetblockChecksum ((UINT8
*) Head
, HeadLen
));
738 if ((Head
->Checksum
!= 0) && (Checksum
!= 0)) {
739 return EFI_INVALID_PARAMETER
;
743 // Convert the IP header to host byte order, then get the per packet info.
745 (*Packet
)->Ip
.Ip4
= Ip4NtohHead (Head
);
747 Info
= IP4_GET_CLIP_INFO (*Packet
);
748 Info
->LinkFlag
= Flag
;
749 Info
->CastType
= Ip4GetHostCast (IpSb
, Head
->Dst
, Head
->Src
);
750 Info
->Start
= (Head
->Fragment
& IP4_HEAD_OFFSET_MASK
) << 3;
751 Info
->Length
= Head
->TotalLen
- HeadLen
;
752 Info
->End
= Info
->Start
+ Info
->Length
;
753 Info
->Status
= EFI_SUCCESS
;
756 // The packet is destinated to us if the CastType is non-zero.
758 if ((Info
->CastType
== 0) || (Info
->End
> IP4_MAX_PACKET_SIZE
)) {
759 return EFI_INVALID_PARAMETER
;
763 // Validate the options. Don't call the Ip4OptionIsValid if
764 // there is no option to save some CPU process.
767 if ((OptionLen
> 0) && !Ip4OptionIsValid (Option
, OptionLen
, TRUE
)) {
768 return EFI_INVALID_PARAMETER
;
772 // Trim the head off, after this point, the packet is headless,
773 // and Packet->TotalLen == Info->Length.
775 NetbufTrim (*Packet
, HeadLen
, TRUE
);
778 // Reassemble the packet if this is a fragment. The packet is a
779 // fragment if its head has MF (more fragment) set, or it starts
782 if (((Head
->Fragment
& IP4_HEAD_MF_MASK
) != 0) || (Info
->Start
!= 0)) {
784 // Drop the fragment if DF is set but it is fragmented. Gateway
785 // need to send a type 4 destination unreache ICMP message here.
787 if ((Head
->Fragment
& IP4_HEAD_DF_MASK
) != 0) {
788 return EFI_INVALID_PARAMETER
;
792 // The length of all but the last fragments is in the unit of 8 bytes.
794 if (((Head
->Fragment
& IP4_HEAD_MF_MASK
) != 0) && (Info
->Length
% 8 != 0)) {
795 return EFI_INVALID_PARAMETER
;
798 *Packet
= Ip4Reassemble (&IpSb
->Assemble
, *Packet
);
801 // Packet assembly isn't complete, start receive more packet.
803 if (*Packet
== NULL
) {
804 return EFI_INVALID_PARAMETER
;
812 The IP4 input routine. It is called by the IP4_INTERFACE when a
813 IP4 fragment is received from MNP.
815 @param[in] Ip4Instance The IP4 child that request the receive, most like
817 @param[in] Packet The IP4 packet received.
818 @param[in] IoStatus The return status of receive request.
819 @param[in] Flag The link layer flag for the packet received, such
821 @param[in] Context The IP4 service instance that own the MNP.
826 IN IP4_PROTOCOL
*Ip4Instance
,
828 IN EFI_STATUS IoStatus
,
840 IpSb
= (IP4_SERVICE
*) Context
;
843 if (EFI_ERROR (IoStatus
) || (IpSb
->State
== IP4_SERVICE_DESTROY
)) {
847 Head
= (IP4_HEAD
*) NetbufGetByte (Packet
, 0, NULL
);
848 ASSERT (Head
!= NULL
);
849 OptionLen
= (Head
->HeadLen
<< 2) - IP4_MIN_HEADLEN
;
851 Option
= (UINT8
*) (Head
+ 1);
855 // Validate packet format and reassemble packet if it is necessary.
857 Status
= Ip4PreProcessPacket (
866 if (EFI_ERROR (Status
)) {
871 // After trim off, the packet is a esp/ah/udp/tcp/icmp6 net buffer,
872 // and no need consider any other ahead ext headers.
874 Status
= Ip4IpSecProcessPacket (
884 if (EFI_ERROR (Status
)) {
889 // If the packet is protected by tunnel mode, parse the inner Ip Packet.
891 ZeroMem (&ZeroHead
, sizeof (IP4_HEAD
));
892 if (0 == CompareMem (Head
, &ZeroHead
, sizeof (IP4_HEAD
))) {
893 // Packet may have been changed. Head, HeadLen, TotalLen, and
894 // info must be reloaded bofore use. The ownership of the packet
895 // is transfered to the packet process logic.
897 Head
= (IP4_HEAD
*) NetbufGetByte (Packet
, 0, NULL
);
898 ASSERT (Head
!= NULL
);
899 Status
= Ip4PreProcessPacket (
907 if (EFI_ERROR (Status
)) {
912 ASSERT (Packet
!= NULL
);
913 Head
= Packet
->Ip
.Ip4
;
914 IP4_GET_CLIP_INFO (Packet
)->Status
= EFI_SUCCESS
;
916 switch (Head
->Protocol
) {
917 case EFI_IP_PROTO_ICMP
:
918 Ip4IcmpHandle (IpSb
, Head
, Packet
);
922 Ip4IgmpHandle (IpSb
, Head
, Packet
);
926 Ip4Demultiplex (IpSb
, Head
, Packet
, Option
, OptionLen
);
932 // Dispatch the DPCs queued by the NotifyFunction of the rx token's events
933 // which are signaled with received data.
938 Ip4ReceiveFrame (IpSb
->DefaultInterface
, NULL
, Ip4AccpetFrame
, IpSb
);
941 if (Packet
!= NULL
) {
950 Check whether this IP child accepts the packet.
952 @param[in] IpInstance The IP child to check
953 @param[in] Head The IP header of the packet
954 @param[in] Packet The data of the packet
956 @retval TRUE If the child wants to receive the packet.
957 @retval FALSE Otherwise.
961 Ip4InstanceFrameAcceptable (
962 IN IP4_PROTOCOL
*IpInstance
,
967 IP4_ICMP_ERROR_HEAD Icmp
;
968 EFI_IP4_CONFIG_DATA
*Config
;
973 Config
= &IpInstance
->ConfigData
;
976 // Dirty trick for the Tiano UEFI network stack implmentation. If
977 // ReceiveTimeout == -1, the receive of the packet for this instance
978 // is disabled. The UEFI spec don't have such capability. We add
979 // this to improve the performance because IP will make a copy of
980 // the received packet for each accepting instance. Some IP instances
981 // used by UDP/TCP only send packets, they don't wants to receive.
983 if (Config
->ReceiveTimeout
== (UINT32
)(-1)) {
987 if (Config
->AcceptPromiscuous
) {
992 // Use protocol from the IP header embedded in the ICMP error
993 // message to filter, instead of ICMP itself. ICMP handle will
994 // call Ip4Demultiplex to deliver ICMP errors.
996 Proto
= Head
->Protocol
;
998 if ((Proto
== EFI_IP_PROTO_ICMP
) && (!Config
->AcceptAnyProtocol
) && (Proto
!= Config
->DefaultProtocol
)) {
999 NetbufCopy (Packet
, 0, sizeof (Icmp
.Head
), (UINT8
*) &Icmp
.Head
);
1001 if (mIcmpClass
[Icmp
.Head
.Type
].IcmpClass
== ICMP_ERROR_MESSAGE
) {
1002 if (!Config
->AcceptIcmpErrors
) {
1006 NetbufCopy (Packet
, 0, sizeof (Icmp
), (UINT8
*) &Icmp
);
1007 Proto
= Icmp
.IpHead
.Protocol
;
1012 // Match the protocol
1014 if (!Config
->AcceptAnyProtocol
&& (Proto
!= Config
->DefaultProtocol
)) {
1019 // Check for broadcast, the caller has computed the packet's
1020 // cast type for this child's interface.
1022 Info
= IP4_GET_CLIP_INFO (Packet
);
1024 if (IP4_IS_BROADCAST (Info
->CastType
)) {
1025 return Config
->AcceptBroadcast
;
1029 // If it is a multicast packet, check whether we are in the group.
1031 if (Info
->CastType
== IP4_MULTICAST
) {
1033 // Receive the multicast if the instance wants to receive all packets.
1035 if (!IpInstance
->ConfigData
.UseDefaultAddress
&& (IpInstance
->Interface
->Ip
== 0)) {
1039 for (Index
= 0; Index
< IpInstance
->GroupCount
; Index
++) {
1040 if (IpInstance
->Groups
[Index
] == HTONL (Head
->Dst
)) {
1045 return (BOOLEAN
)(Index
< IpInstance
->GroupCount
);
1053 Enqueue a shared copy of the packet to the IP4 child if the
1054 packet is acceptable to it. Here the data of the packet is
1055 shared, but the net buffer isn't.
1057 @param[in] IpInstance The IP4 child to enqueue the packet to
1058 @param[in] Head The IP header of the received packet
1059 @param[in] Packet The data of the received packet
1061 @retval EFI_NOT_STARTED The IP child hasn't been configured.
1062 @retval EFI_INVALID_PARAMETER The child doesn't want to receive the packet
1063 @retval EFI_OUT_OF_RESOURCES Failed to allocate some resource
1064 @retval EFI_SUCCESS A shared copy the packet is enqueued to the child.
1068 Ip4InstanceEnquePacket (
1069 IN IP4_PROTOCOL
*IpInstance
,
1074 IP4_CLIP_INFO
*Info
;
1078 // Check whether the packet is acceptable to this instance.
1080 if (IpInstance
->State
!= IP4_STATE_CONFIGED
) {
1081 return EFI_NOT_STARTED
;
1084 if (!Ip4InstanceFrameAcceptable (IpInstance
, Head
, Packet
)) {
1085 return EFI_INVALID_PARAMETER
;
1089 // Enque a shared copy of the packet.
1091 Clone
= NetbufClone (Packet
);
1093 if (Clone
== NULL
) {
1094 return EFI_OUT_OF_RESOURCES
;
1098 // Set the receive time out for the assembled packet. If it expires,
1099 // packet will be removed from the queue.
1101 Info
= IP4_GET_CLIP_INFO (Clone
);
1102 Info
->Life
= IP4_US_TO_SEC (IpInstance
->ConfigData
.ReceiveTimeout
);
1104 InsertTailList (&IpInstance
->Received
, &Clone
->List
);
1110 The signal handle of IP4's recycle event. It is called back
1111 when the upper layer release the packet.
1113 @param Event The IP4's recycle event.
1114 @param Context The context of the handle, which is a
1120 Ip4OnRecyclePacket (
1125 IP4_RXDATA_WRAP
*Wrap
;
1127 Wrap
= (IP4_RXDATA_WRAP
*) Context
;
1129 EfiAcquireLockOrFail (&Wrap
->IpInstance
->RecycleLock
);
1130 RemoveEntryList (&Wrap
->Link
);
1131 EfiReleaseLock (&Wrap
->IpInstance
->RecycleLock
);
1133 ASSERT (!NET_BUF_SHARED (Wrap
->Packet
));
1134 NetbufFree (Wrap
->Packet
);
1136 gBS
->CloseEvent (Wrap
->RxData
.RecycleSignal
);
1142 Wrap the received packet to a IP4_RXDATA_WRAP, which will be
1143 delivered to the upper layer. Each IP4 child that accepts the
1144 packet will get a not-shared copy of the packet which is wrapped
1145 in the IP4_RXDATA_WRAP. The IP4_RXDATA_WRAP->RxData is passed
1146 to the upper layer. Upper layer will signal the recycle event in
1147 it when it is done with the packet.
1149 @param[in] IpInstance The IP4 child to receive the packet.
1150 @param[in] Packet The packet to deliver up.
1152 @retval Wrap if warp the packet succeed.
1153 @retval NULL failed to wrap the packet .
1158 IN IP4_PROTOCOL
*IpInstance
,
1162 IP4_RXDATA_WRAP
*Wrap
;
1163 EFI_IP4_RECEIVE_DATA
*RxData
;
1167 Wrap
= AllocatePool (IP4_RXDATA_WRAP_SIZE (Packet
->BlockOpNum
));
1173 InitializeListHead (&Wrap
->Link
);
1175 Wrap
->IpInstance
= IpInstance
;
1176 Wrap
->Packet
= Packet
;
1177 RxData
= &Wrap
->RxData
;
1179 ZeroMem (RxData
, sizeof (EFI_IP4_RECEIVE_DATA
));
1181 Status
= gBS
->CreateEvent (
1186 &RxData
->RecycleSignal
1189 if (EFI_ERROR (Status
)) {
1194 ASSERT (Packet
->Ip
.Ip4
!= NULL
);
1196 ASSERT (IpInstance
!= NULL
);
1197 RawData
= IpInstance
->ConfigData
.RawData
;
1200 // The application expects a network byte order header.
1203 RxData
->HeaderLength
= (Packet
->Ip
.Ip4
->HeadLen
<< 2);
1204 RxData
->Header
= (EFI_IP4_HEADER
*) Ip4NtohHead (Packet
->Ip
.Ip4
);
1205 RxData
->OptionsLength
= RxData
->HeaderLength
- IP4_MIN_HEADLEN
;
1206 RxData
->Options
= NULL
;
1208 if (RxData
->OptionsLength
!= 0) {
1209 RxData
->Options
= (VOID
*) (RxData
->Header
+ 1);
1213 RxData
->DataLength
= Packet
->TotalSize
;
1216 // Build the fragment table to be delivered up.
1218 RxData
->FragmentCount
= Packet
->BlockOpNum
;
1219 NetbufBuildExt (Packet
, (NET_FRAGMENT
*) RxData
->FragmentTable
, &RxData
->FragmentCount
);
1226 Deliver the received packets to upper layer if there are both received
1227 requests and enqueued packets. If the enqueued packet is shared, it will
1228 duplicate it to a non-shared packet, release the shared packet, then
1229 deliver the non-shared packet up.
1231 @param[in] IpInstance The IP child to deliver the packet up.
1233 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources to deliver the
1235 @retval EFI_SUCCESS All the enqueued packets that can be delivered
1240 Ip4InstanceDeliverPacket (
1241 IN IP4_PROTOCOL
*IpInstance
1244 EFI_IP4_COMPLETION_TOKEN
*Token
;
1245 IP4_RXDATA_WRAP
*Wrap
;
1252 // Deliver a packet if there are both a packet and a receive token.
1254 while (!IsListEmpty (&IpInstance
->Received
) &&
1255 !NetMapIsEmpty (&IpInstance
->RxTokens
)) {
1257 Packet
= NET_LIST_HEAD (&IpInstance
->Received
, NET_BUF
, List
);
1259 if (!NET_BUF_SHARED (Packet
)) {
1261 // If this is the only instance that wants the packet, wrap it up.
1263 Wrap
= Ip4WrapRxData (IpInstance
, Packet
);
1266 return EFI_OUT_OF_RESOURCES
;
1269 RemoveEntryList (&Packet
->List
);
1273 // Create a duplicated packet if this packet is shared
1275 if (IpInstance
->ConfigData
.RawData
) {
1278 HeadLen
= IP4_MAX_HEADLEN
;
1281 Dup
= NetbufDuplicate (Packet
, NULL
, HeadLen
);
1284 return EFI_OUT_OF_RESOURCES
;
1287 if (!IpInstance
->ConfigData
.RawData
) {
1289 // Copy the IP head over. The packet to deliver up is
1290 // headless. Trim the head off after copy. The IP head
1291 // may be not continuous before the data.
1293 Head
= NetbufAllocSpace (Dup
, IP4_MAX_HEADLEN
, NET_BUF_HEAD
);
1294 ASSERT (Head
!= NULL
);
1296 Dup
->Ip
.Ip4
= (IP4_HEAD
*) Head
;
1298 CopyMem (Head
, Packet
->Ip
.Ip4
, Packet
->Ip
.Ip4
->HeadLen
<< 2);
1299 NetbufTrim (Dup
, IP4_MAX_HEADLEN
, TRUE
);
1302 Wrap
= Ip4WrapRxData (IpInstance
, Dup
);
1306 return EFI_OUT_OF_RESOURCES
;
1309 RemoveEntryList (&Packet
->List
);
1310 NetbufFree (Packet
);
1316 // Insert it into the delivered packet, then get a user's
1317 // receive token, pass the wrapped packet up.
1319 EfiAcquireLockOrFail (&IpInstance
->RecycleLock
);
1320 InsertHeadList (&IpInstance
->Delivered
, &Wrap
->Link
);
1321 EfiReleaseLock (&IpInstance
->RecycleLock
);
1323 Token
= NetMapRemoveHead (&IpInstance
->RxTokens
, NULL
);
1324 Token
->Status
= IP4_GET_CLIP_INFO (Packet
)->Status
;
1325 Token
->Packet
.RxData
= &Wrap
->RxData
;
1327 gBS
->SignalEvent (Token
->Event
);
1335 Enqueue a received packet to all the IP children that share
1338 @param[in] IpSb The IP4 service instance that receive the packet.
1339 @param[in] Head The header of the received packet.
1340 @param[in] Packet The data of the received packet.
1341 @param[in] Option Point to the IP4 packet header options.
1342 @param[in] OptionLen Length of the IP4 packet header options.
1343 @param[in] IpIf The interface to enqueue the packet to.
1345 @return The number of the IP4 children that accepts the packet
1349 Ip4InterfaceEnquePacket (
1350 IN IP4_SERVICE
*IpSb
,
1354 IN UINT32 OptionLen
,
1355 IN IP4_INTERFACE
*IpIf
1358 IP4_PROTOCOL
*IpInstance
;
1359 IP4_CLIP_INFO
*Info
;
1366 // First, check that the packet is acceptable to this interface
1367 // and find the local cast type for the interface. A packet sent
1368 // to say 192.168.1.1 should NOT be delliever to 10.0.0.1 unless
1369 // promiscuous receiving.
1372 Info
= IP4_GET_CLIP_INFO (Packet
);
1374 if ((Info
->CastType
== IP4_MULTICAST
) || (Info
->CastType
== IP4_LOCAL_BROADCAST
)) {
1376 // If the CastType is multicast, don't need to filter against
1377 // the group address here, Ip4InstanceFrameAcceptable will do
1380 LocalType
= Info
->CastType
;
1384 // Check the destination againist local IP. If the station
1385 // address is 0.0.0.0, it means receiving all the IP destined
1386 // to local non-zero IP. Otherwise, it is necessary to compare
1387 // the destination to the interface's IP address.
1389 if (IpIf
->Ip
== IP4_ALLZERO_ADDRESS
) {
1390 LocalType
= IP4_LOCAL_HOST
;
1393 LocalType
= Ip4GetNetCast (Head
->Dst
, IpIf
);
1395 if ((LocalType
== 0) && IpIf
->PromiscRecv
) {
1396 LocalType
= IP4_PROMISCUOUS
;
1401 if (LocalType
== 0) {
1406 // Iterate through the ip instances on the interface, enqueue
1407 // the packet if filter passed. Save the original cast type,
1408 // and pass the local cast type to the IP children on the
1409 // interface. The global cast type will be restored later.
1411 SavedType
= Info
->CastType
;
1412 Info
->CastType
= LocalType
;
1416 NET_LIST_FOR_EACH (Entry
, &IpIf
->IpInstances
) {
1417 IpInstance
= NET_LIST_USER_STRUCT (Entry
, IP4_PROTOCOL
, AddrLink
);
1418 NET_CHECK_SIGNATURE (IpInstance
, IP4_PROTOCOL_SIGNATURE
);
1421 // In RawData mode, add IPv4 headers and options back to packet.
1423 if ((IpInstance
->ConfigData
.RawData
) && (Option
!= NULL
) && (OptionLen
!= 0)){
1424 Ip4PrependHead (Packet
, Head
, Option
, OptionLen
);
1427 if (Ip4InstanceEnquePacket (IpInstance
, Head
, Packet
) == EFI_SUCCESS
) {
1432 Info
->CastType
= SavedType
;
1438 Deliver the packet for each IP4 child on the interface.
1440 @param[in] IpSb The IP4 service instance that received the packet
1441 @param[in] IpIf The IP4 interface to deliver the packet.
1443 @retval EFI_SUCCESS It always returns EFI_SUCCESS now
1447 Ip4InterfaceDeliverPacket (
1448 IN IP4_SERVICE
*IpSb
,
1449 IN IP4_INTERFACE
*IpIf
1452 IP4_PROTOCOL
*Ip4Instance
;
1455 NET_LIST_FOR_EACH (Entry
, &IpIf
->IpInstances
) {
1456 Ip4Instance
= NET_LIST_USER_STRUCT (Entry
, IP4_PROTOCOL
, AddrLink
);
1457 Ip4InstanceDeliverPacket (Ip4Instance
);
1465 Demultiple the packet. the packet delivery is processed in two
1466 passes. The first pass will enque a shared copy of the packet
1467 to each IP4 child that accepts the packet. The second pass will
1468 deliver a non-shared copy of the packet to each IP4 child that
1469 has pending receive requests. Data is copied if more than one
1470 child wants to consume the packet because each IP child needs
1471 its own copy of the packet to make changes.
1473 @param[in] IpSb The IP4 service instance that received the packet.
1474 @param[in] Head The header of the received packet.
1475 @param[in] Packet The data of the received packet.
1476 @param[in] Option Point to the IP4 packet header options.
1477 @param[in] OptionLen Length of the IP4 packet header options.
1479 @retval EFI_NOT_FOUND No IP child accepts the packet.
1480 @retval EFI_SUCCESS The packet is enqueued or delivered to some IP
1486 IN IP4_SERVICE
*IpSb
,
1494 IP4_INTERFACE
*IpIf
;
1498 // Two pass delivery: first, enque a shared copy of the packet
1499 // to each instance that accept the packet.
1503 NET_LIST_FOR_EACH (Entry
, &IpSb
->Interfaces
) {
1504 IpIf
= NET_LIST_USER_STRUCT (Entry
, IP4_INTERFACE
, Link
);
1506 if (IpIf
->Configured
) {
1507 Enqueued
+= Ip4InterfaceEnquePacket (
1519 // Second: deliver a duplicate of the packet to each instance.
1520 // Release the local reference first, so that the last instance
1521 // getting the packet will not copy the data.
1523 NetbufFree (Packet
);
1525 if (Enqueued
== 0) {
1526 return EFI_NOT_FOUND
;
1529 NET_LIST_FOR_EACH (Entry
, &IpSb
->Interfaces
) {
1530 IpIf
= NET_LIST_USER_STRUCT (Entry
, IP4_INTERFACE
, Link
);
1532 if (IpIf
->Configured
) {
1533 Ip4InterfaceDeliverPacket (IpSb
, IpIf
);
1542 Timeout the fragment and enqueued packets.
1544 @param[in] IpSb The IP4 service instance to timeout
1548 Ip4PacketTimerTicking (
1549 IN IP4_SERVICE
*IpSb
1552 LIST_ENTRY
*InstanceEntry
;
1555 IP4_PROTOCOL
*IpInstance
;
1556 IP4_ASSEMBLE_ENTRY
*Assemble
;
1558 IP4_CLIP_INFO
*Info
;
1562 // First, time out the fragments. The packet's life is counting down
1563 // once the first-arrived fragment was received.
1565 for (Index
= 0; Index
< IP4_ASSEMLE_HASH_SIZE
; Index
++) {
1566 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &IpSb
->Assemble
.Bucket
[Index
]) {
1567 Assemble
= NET_LIST_USER_STRUCT (Entry
, IP4_ASSEMBLE_ENTRY
, Link
);
1569 if ((Assemble
->Life
> 0) && (--Assemble
->Life
== 0)) {
1570 RemoveEntryList (Entry
);
1571 Ip4FreeAssembleEntry (Assemble
);
1576 NET_LIST_FOR_EACH (InstanceEntry
, &IpSb
->Children
) {
1577 IpInstance
= NET_LIST_USER_STRUCT (InstanceEntry
, IP4_PROTOCOL
, Link
);
1580 // Second, time out the assembled packets enqueued on each IP child.
1582 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &IpInstance
->Received
) {
1583 Packet
= NET_LIST_USER_STRUCT (Entry
, NET_BUF
, List
);
1584 Info
= IP4_GET_CLIP_INFO (Packet
);
1586 if ((Info
->Life
> 0) && (--Info
->Life
== 0)) {
1587 RemoveEntryList (Entry
);
1588 NetbufFree (Packet
);
1593 // Third: time out the transmitted packets.
1595 NetMapIterate (&IpInstance
->TxTokens
, Ip4SentPacketTicking
, NULL
);