4 Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.<BR>
5 (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
21 Create an empty assemble entry for the packet identified by
22 (Dst, Src, Id, Protocol). The default life for the packet is
25 @param[in] Dst The destination address
26 @param[in] Src The source address
27 @param[in] Id The ID field in IP header
28 @param[in] Protocol The protocol field in IP header
30 @return NULL if failed to allocate memory for the entry, otherwise
31 the point to just created reassemble entry.
35 Ip4CreateAssembleEntry (
43 IP4_ASSEMBLE_ENTRY
*Assemble
;
45 Assemble
= AllocatePool (sizeof (IP4_ASSEMBLE_ENTRY
));
47 if (Assemble
== NULL
) {
51 InitializeListHead (&Assemble
->Link
);
52 InitializeListHead (&Assemble
->Fragments
);
57 Assemble
->Protocol
= Protocol
;
58 Assemble
->TotalLen
= 0;
60 Assemble
->Head
= NULL
;
61 Assemble
->Info
= NULL
;
62 Assemble
->Life
= IP4_FRAGMENT_LIFE
;
69 Release all the fragments of a packet, then free the assemble entry.
71 @param[in] Assemble The assemble entry to free
75 Ip4FreeAssembleEntry (
76 IN IP4_ASSEMBLE_ENTRY
*Assemble
83 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &Assemble
->Fragments
) {
84 Fragment
= NET_LIST_USER_STRUCT (Entry
, NET_BUF
, List
);
86 RemoveEntryList (Entry
);
87 NetbufFree (Fragment
);
95 Initialize an already allocated assemble table. This is generally
96 the assemble table embedded in the IP4 service instance.
98 @param[in, out] Table The assemble table to initialize.
102 Ip4InitAssembleTable (
103 IN OUT IP4_ASSEMBLE_TABLE
*Table
108 for (Index
= 0; Index
< IP4_ASSEMLE_HASH_SIZE
; Index
++) {
109 InitializeListHead (&Table
->Bucket
[Index
]);
115 Clean up the assemble table: remove all the fragments
116 and assemble entries.
118 @param[in] Table The assemble table to clean up
122 Ip4CleanAssembleTable (
123 IN IP4_ASSEMBLE_TABLE
*Table
128 IP4_ASSEMBLE_ENTRY
*Assemble
;
131 for (Index
= 0; Index
< IP4_ASSEMLE_HASH_SIZE
; Index
++) {
132 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &Table
->Bucket
[Index
]) {
133 Assemble
= NET_LIST_USER_STRUCT (Entry
, IP4_ASSEMBLE_ENTRY
, Link
);
135 RemoveEntryList (Entry
);
136 Ip4FreeAssembleEntry (Assemble
);
143 Trim the packet to fit in [Start, End), and update the per
146 @param Packet Packet to trim
147 @param Start The sequence of the first byte to fit in
148 @param End One beyond the sequence of last byte to fit in.
153 IN OUT NET_BUF
*Packet
,
161 Info
= IP4_GET_CLIP_INFO (Packet
);
163 ASSERT (Info
->Start
+ Info
->Length
== Info
->End
);
164 ASSERT ((Info
->Start
< End
) && (Start
< Info
->End
));
166 if (Info
->Start
< Start
) {
167 Len
= Start
- Info
->Start
;
169 NetbufTrim (Packet
, (UINT32
) Len
, NET_BUF_HEAD
);
174 if (End
< Info
->End
) {
175 Len
= End
- Info
->End
;
177 NetbufTrim (Packet
, (UINT32
) Len
, NET_BUF_TAIL
);
185 Release all the fragments of the packet. This is the callback for
186 the assembled packet's OnFree. It will free the assemble entry,
187 which in turn will free all the fragments of the packet.
189 @param[in] Arg The assemble entry to free
198 Ip4FreeAssembleEntry ((IP4_ASSEMBLE_ENTRY
*) Arg
);
203 Reassemble the IP fragments. If all the fragments of the packet
204 have been received, it will wrap the packet in a net buffer then
205 return it to caller. If the packet can't be assembled, NULL is
208 @param Table The assemble table used. New assemble entry will be created
209 if the Packet is from a new chain of fragments.
210 @param Packet The fragment to assemble. It might be freed if the fragment
211 can't be re-assembled.
213 @return NULL if the packet can't be reassemble. The point to just assembled
214 packet if all the fragments of the packet have arrived.
219 IN OUT IP4_ASSEMBLE_TABLE
*Table
,
220 IN OUT NET_BUF
*Packet
226 IP4_ASSEMBLE_ENTRY
*Assemble
;
234 IpHead
= Packet
->Ip
.Ip4
;
235 This
= IP4_GET_CLIP_INFO (Packet
);
237 ASSERT (IpHead
!= NULL
);
240 // First: find the related assemble entry
243 Index
= IP4_ASSEMBLE_HASH (IpHead
->Dst
, IpHead
->Src
, IpHead
->Id
, IpHead
->Protocol
);
245 NET_LIST_FOR_EACH (Cur
, &Table
->Bucket
[Index
]) {
246 Assemble
= NET_LIST_USER_STRUCT (Cur
, IP4_ASSEMBLE_ENTRY
, Link
);
248 if ((Assemble
->Dst
== IpHead
->Dst
) && (Assemble
->Src
== IpHead
->Src
) &&
249 (Assemble
->Id
== IpHead
->Id
) && (Assemble
->Protocol
== IpHead
->Protocol
)) {
255 // Create a new assemble entry if no assemble entry is related to this packet
257 if (Cur
== &Table
->Bucket
[Index
]) {
258 Assemble
= Ip4CreateAssembleEntry (
265 if (Assemble
== NULL
) {
269 InsertHeadList (&Table
->Bucket
[Index
], &Assemble
->Link
);
272 // Assemble shouldn't be NULL here
274 ASSERT (Assemble
!= NULL
);
277 // Find the point to insert the packet: before the first
278 // fragment with THIS.Start < CUR.Start. the previous one
279 // has PREV.Start <= THIS.Start < CUR.Start.
281 Head
= &Assemble
->Fragments
;
283 NET_LIST_FOR_EACH (Cur
, Head
) {
284 Fragment
= NET_LIST_USER_STRUCT (Cur
, NET_BUF
, List
);
286 if (This
->Start
< IP4_GET_CLIP_INFO (Fragment
)->Start
) {
292 // Check whether the current fragment overlaps with the previous one.
293 // It holds that: PREV.Start <= THIS.Start < THIS.End. Only need to
294 // check whether THIS.Start < PREV.End for overlap. If two fragments
295 // overlaps, trim the overlapped part off THIS fragment.
297 if ((Prev
= Cur
->BackLink
) != Head
) {
298 Fragment
= NET_LIST_USER_STRUCT (Prev
, NET_BUF
, List
);
299 Node
= IP4_GET_CLIP_INFO (Fragment
);
301 if (This
->Start
< Node
->End
) {
302 if (This
->End
<= Node
->End
) {
307 Ip4TrimPacket (Packet
, Node
->End
, This
->End
);
312 // Insert the fragment into the packet. The fragment may be removed
313 // from the list by the following checks.
315 NetListInsertBefore (Cur
, &Packet
->List
);
318 // Check the packets after the insert point. It holds that:
319 // THIS.Start <= NODE.Start < NODE.End. The equality holds
320 // if PREV and NEXT are continuous. THIS fragment may fill
321 // several holes. Remove the completely overlapped fragments
323 while (Cur
!= Head
) {
324 Fragment
= NET_LIST_USER_STRUCT (Cur
, NET_BUF
, List
);
325 Node
= IP4_GET_CLIP_INFO (Fragment
);
328 // Remove fragments completely overlapped by this fragment
330 if (Node
->End
<= This
->End
) {
331 Cur
= Cur
->ForwardLink
;
333 RemoveEntryList (&Fragment
->List
);
334 Assemble
->CurLen
-= Node
->Length
;
336 NetbufFree (Fragment
);
341 // The conditions are: THIS.Start <= NODE.Start, and THIS.End <
342 // NODE.End. Two fragments overlaps if NODE.Start < THIS.End.
343 // If two fragments start at the same offset, remove THIS fragment
344 // because ((THIS.Start == NODE.Start) && (THIS.End < NODE.End)).
346 if (Node
->Start
< This
->End
) {
347 if (This
->Start
== Node
->Start
) {
348 RemoveEntryList (&Packet
->List
);
352 Ip4TrimPacket (Packet
, This
->Start
, Node
->Start
);
359 // Update the assemble info: increase the current length. If it is
360 // the frist fragment, update the packet's IP head and per packet
361 // info. If it is the last fragment, update the total length.
363 Assemble
->CurLen
+= This
->Length
;
365 if (This
->Start
== 0) {
367 // Once the first fragment is enqueued, it can't be removed
368 // from the fragment list. So, Assemble->Head always point
369 // to valid memory area.
371 ASSERT (Assemble
->Head
== NULL
);
373 Assemble
->Head
= IpHead
;
374 Assemble
->Info
= IP4_GET_CLIP_INFO (Packet
);
378 // Don't update the length more than once.
380 if (IP4_LAST_FRAGMENT (IpHead
->Fragment
) && (Assemble
->TotalLen
== 0)) {
381 Assemble
->TotalLen
= This
->End
;
385 // Deliver the whole packet if all the fragments received.
386 // All fragments received if:
387 // 1. received the last one, so, the total length is know
388 // 2. received all the data. If the last fragment on the
389 // queue ends at the total length, all data is received.
391 if ((Assemble
->TotalLen
!= 0) && (Assemble
->CurLen
>= Assemble
->TotalLen
)) {
393 RemoveEntryList (&Assemble
->Link
);
396 // If the packet is properly formated, the last fragment's End
397 // equals to the packet's total length. Otherwise, the packet
398 // is a fake, drop it now.
400 Fragment
= NET_LIST_USER_STRUCT (Head
->BackLink
, NET_BUF
, List
);
402 if (IP4_GET_CLIP_INFO (Fragment
)->End
!= Assemble
->TotalLen
) {
403 Ip4FreeAssembleEntry (Assemble
);
408 // Wrap the packet in a net buffer then deliver it up
410 NewPacket
= NetbufFromBufList (
411 &Assemble
->Fragments
,
418 if (NewPacket
== NULL
) {
419 Ip4FreeAssembleEntry (Assemble
);
423 NewPacket
->Ip
.Ip4
= Assemble
->Head
;
425 ASSERT (Assemble
->Info
!= NULL
);
428 IP4_GET_CLIP_INFO (NewPacket
),
430 sizeof (*IP4_GET_CLIP_INFO (NewPacket
))
444 The callback function for the net buffer which wraps the packet processed by
445 IPsec. It releases the wrap packet and also signals IPsec to free the resources.
447 @param[in] Arg The wrap context
456 IP4_IPSEC_WRAP
*Wrap
;
458 Wrap
= (IP4_IPSEC_WRAP
*) Arg
;
460 if (Wrap
->IpSecRecycleSignal
!= NULL
) {
461 gBS
->SignalEvent (Wrap
->IpSecRecycleSignal
);
464 NetbufFree (Wrap
->Packet
);
472 The work function to locate IPsec protocol to process the inbound or
473 outbound IP packets. The process routine handls the packet with following
474 actions: bypass the packet, discard the packet, or protect the packet.
476 @param[in] IpSb The IP4 service instance.
477 @param[in, out] Head The The caller supplied IP4 header.
478 @param[in, out] Netbuf The IP4 packet to be processed by IPsec.
479 @param[in, out] Options The caller supplied options.
480 @param[in, out] OptionsLen The length of the option.
481 @param[in] Direction The directionality in an SPD entry,
482 EfiIPsecInBound or EfiIPsecOutBound.
483 @param[in] Context The token's wrap.
485 @retval EFI_SUCCESS The IPsec protocol is not available or disabled.
486 @retval EFI_SUCCESS The packet was bypassed and all buffers remain the same.
487 @retval EFI_SUCCESS The packet was protected.
488 @retval EFI_ACCESS_DENIED The packet was discarded.
489 @retval EFI_OUT_OF_RESOURCES There is no suffcient resource to complete the operation.
490 @retval EFI_BUFFER_TOO_SMALL The number of non-empty block is bigger than the
491 number of input data blocks when build a fragment table.
495 Ip4IpSecProcessPacket (
496 IN IP4_SERVICE
*IpSb
,
497 IN OUT IP4_HEAD
**Head
,
498 IN OUT NET_BUF
**Netbuf
,
499 IN OUT UINT8
**Options
,
500 IN OUT UINT32
*OptionsLen
,
501 IN EFI_IPSEC_TRAFFIC_DIR Direction
,
505 NET_FRAGMENT
*FragmentTable
;
506 NET_FRAGMENT
*OriginalFragmentTable
;
507 UINT32 FragmentCount
;
508 UINT32 OriginalFragmentCount
;
509 EFI_EVENT RecycleEvent
;
511 IP4_TXTOKEN_WRAP
*TxWrap
;
512 IP4_IPSEC_WRAP
*IpSecWrap
;
516 Status
= EFI_SUCCESS
;
518 if (!mIpSec2Installed
) {
521 ASSERT (mIpSec
!= NULL
);
526 FragmentTable
= NULL
;
527 TxWrap
= (IP4_TXTOKEN_WRAP
*) Context
;
528 FragmentCount
= Packet
->BlockOpNum
;
530 ZeroMem (&ZeroHead
, sizeof (IP4_HEAD
));
533 // Check whether the IPsec enable variable is set.
535 if (mIpSec
->DisabledFlag
) {
537 // If IPsec is disabled, restore the original MTU
539 IpSb
->MaxPacketSize
= IpSb
->OldMaxPacketSize
;
543 // If IPsec is enabled, use the MTU which reduce the IPsec header length.
545 IpSb
->MaxPacketSize
= IpSb
->OldMaxPacketSize
- IP4_MAX_IPSEC_HEADLEN
;
549 // Rebuild fragment table from netbuf to ease IPsec process.
551 FragmentTable
= AllocateZeroPool (FragmentCount
* sizeof (NET_FRAGMENT
));
553 if (FragmentTable
== NULL
) {
554 Status
= EFI_OUT_OF_RESOURCES
;
558 Status
= NetbufBuildExt (Packet
, FragmentTable
, &FragmentCount
);
561 // Record the original FragmentTable and count.
563 OriginalFragmentTable
= FragmentTable
;
564 OriginalFragmentCount
= FragmentCount
;
566 if (EFI_ERROR (Status
)) {
567 FreePool (FragmentTable
);
572 // Convert host byte order to network byte order
576 Status
= mIpSec
->ProcessExt (
584 (EFI_IPSEC_FRAGMENT_DATA
**) (&FragmentTable
),
590 // Convert back to host byte order
594 if (EFI_ERROR (Status
)) {
595 FreePool (OriginalFragmentTable
);
599 if (OriginalFragmentTable
== FragmentTable
&& OriginalFragmentCount
== FragmentCount
) {
603 FreePool (FragmentTable
);
607 // Free the FragmentTable which allocated before calling the IPsec.
609 FreePool (OriginalFragmentTable
);
612 if (Direction
== EfiIPsecOutBound
&& TxWrap
!= NULL
) {
614 TxWrap
->IpSecRecycleSignal
= RecycleEvent
;
615 TxWrap
->Packet
= NetbufFromExt (
623 if (TxWrap
->Packet
== NULL
) {
625 // Recover the TxWrap->Packet, if meet a error, and the caller will free
628 TxWrap
->Packet
= *Netbuf
;
629 Status
= EFI_OUT_OF_RESOURCES
;
634 // Free orginal Netbuf.
636 NetIpSecNetbufFree (*Netbuf
);
637 *Netbuf
= TxWrap
->Packet
;
641 IpSecWrap
= AllocateZeroPool (sizeof (IP4_IPSEC_WRAP
));
643 if (IpSecWrap
== NULL
) {
644 Status
= EFI_OUT_OF_RESOURCES
;
645 gBS
->SignalEvent (RecycleEvent
);
649 IpSecWrap
->IpSecRecycleSignal
= RecycleEvent
;
650 IpSecWrap
->Packet
= Packet
;
651 Packet
= NetbufFromExt (
660 if (Packet
== NULL
) {
661 Packet
= IpSecWrap
->Packet
;
662 gBS
->SignalEvent (RecycleEvent
);
663 FreePool (IpSecWrap
);
664 Status
= EFI_OUT_OF_RESOURCES
;
668 if (Direction
== EfiIPsecInBound
&& 0 != CompareMem (*Head
, &ZeroHead
, sizeof (IP4_HEAD
))) {
669 Ip4PrependHead (Packet
, *Head
, *Options
, *OptionsLen
);
670 Ip4NtohHead (Packet
->Ip
.Ip4
);
671 NetbufTrim (Packet
, ((*Head
)->HeadLen
<< 2), TRUE
);
674 IP4_GET_CLIP_INFO (Packet
),
675 IP4_GET_CLIP_INFO (IpSecWrap
->Packet
),
676 sizeof (IP4_CLIP_INFO
)
687 Pre-process the IPv4 packet. First validates the IPv4 packet, and
688 then reassembles packet if it is necessary.
690 @param[in] IpSb Pointer to IP4_SERVICE.
691 @param[in, out] Packet Pointer to the Packet to be processed.
692 @param[in] Head Pointer to the IP4_HEAD.
693 @param[in] Option Pointer to a buffer which contains the IPv4 option.
694 @param[in] OptionLen The length of Option in bytes.
695 @param[in] Flag The link layer flag for the packet received, such
698 @retval EFI_SEUCCESS The recieved packet is in well form.
699 @retval EFI_INVAILD_PARAMETER The recieved packet is malformed.
703 Ip4PreProcessPacket (
704 IN IP4_SERVICE
*IpSb
,
705 IN OUT NET_BUF
**Packet
,
718 // Check if the IP4 header is correctly formatted.
720 if ((*Packet
)->TotalSize
< IP4_MIN_HEADLEN
) {
721 return EFI_INVALID_PARAMETER
;
724 HeadLen
= (Head
->HeadLen
<< 2);
725 TotalLen
= NTOHS (Head
->TotalLen
);
728 // Mnp may deliver frame trailer sequence up, trim it off.
730 if (TotalLen
< (*Packet
)->TotalSize
) {
731 NetbufTrim (*Packet
, (*Packet
)->TotalSize
- TotalLen
, FALSE
);
734 if ((Head
->Ver
!= 4) || (HeadLen
< IP4_MIN_HEADLEN
) ||
735 (TotalLen
< HeadLen
) || (TotalLen
!= (*Packet
)->TotalSize
)) {
736 return EFI_INVALID_PARAMETER
;
740 // Some OS may send IP packets without checksum.
742 Checksum
= (UINT16
) (~NetblockChecksum ((UINT8
*) Head
, HeadLen
));
744 if ((Head
->Checksum
!= 0) && (Checksum
!= 0)) {
745 return EFI_INVALID_PARAMETER
;
749 // Convert the IP header to host byte order, then get the per packet info.
751 (*Packet
)->Ip
.Ip4
= Ip4NtohHead (Head
);
753 Info
= IP4_GET_CLIP_INFO (*Packet
);
754 Info
->LinkFlag
= Flag
;
755 Info
->CastType
= Ip4GetHostCast (IpSb
, Head
->Dst
, Head
->Src
);
756 Info
->Start
= (Head
->Fragment
& IP4_HEAD_OFFSET_MASK
) << 3;
757 Info
->Length
= Head
->TotalLen
- HeadLen
;
758 Info
->End
= Info
->Start
+ Info
->Length
;
759 Info
->Status
= EFI_SUCCESS
;
762 // The packet is destinated to us if the CastType is non-zero.
764 if ((Info
->CastType
== 0) || (Info
->End
> IP4_MAX_PACKET_SIZE
)) {
765 return EFI_INVALID_PARAMETER
;
769 // Validate the options. Don't call the Ip4OptionIsValid if
770 // there is no option to save some CPU process.
773 if ((OptionLen
> 0) && !Ip4OptionIsValid (Option
, OptionLen
, TRUE
)) {
774 return EFI_INVALID_PARAMETER
;
778 // Trim the head off, after this point, the packet is headless,
779 // and Packet->TotalLen == Info->Length.
781 NetbufTrim (*Packet
, HeadLen
, TRUE
);
784 // Reassemble the packet if this is a fragment. The packet is a
785 // fragment if its head has MF (more fragment) set, or it starts
788 if (((Head
->Fragment
& IP4_HEAD_MF_MASK
) != 0) || (Info
->Start
!= 0)) {
790 // Drop the fragment if DF is set but it is fragmented. Gateway
791 // need to send a type 4 destination unreache ICMP message here.
793 if ((Head
->Fragment
& IP4_HEAD_DF_MASK
) != 0) {
794 return EFI_INVALID_PARAMETER
;
798 // The length of all but the last fragments is in the unit of 8 bytes.
800 if (((Head
->Fragment
& IP4_HEAD_MF_MASK
) != 0) && (Info
->Length
% 8 != 0)) {
801 return EFI_INVALID_PARAMETER
;
804 *Packet
= Ip4Reassemble (&IpSb
->Assemble
, *Packet
);
807 // Packet assembly isn't complete, start receive more packet.
809 if (*Packet
== NULL
) {
810 return EFI_INVALID_PARAMETER
;
818 The IP4 input routine. It is called by the IP4_INTERFACE when a
819 IP4 fragment is received from MNP.
821 @param[in] Ip4Instance The IP4 child that request the receive, most like
823 @param[in] Packet The IP4 packet received.
824 @param[in] IoStatus The return status of receive request.
825 @param[in] Flag The link layer flag for the packet received, such
827 @param[in] Context The IP4 service instance that own the MNP.
832 IN IP4_PROTOCOL
*Ip4Instance
,
834 IN EFI_STATUS IoStatus
,
846 IpSb
= (IP4_SERVICE
*) Context
;
849 if (EFI_ERROR (IoStatus
) || (IpSb
->State
== IP4_SERVICE_DESTROY
)) {
853 Head
= (IP4_HEAD
*) NetbufGetByte (Packet
, 0, NULL
);
854 ASSERT (Head
!= NULL
);
855 OptionLen
= (Head
->HeadLen
<< 2) - IP4_MIN_HEADLEN
;
857 Option
= (UINT8
*) (Head
+ 1);
861 // Validate packet format and reassemble packet if it is necessary.
863 Status
= Ip4PreProcessPacket (
872 if (EFI_ERROR (Status
)) {
877 // After trim off, the packet is a esp/ah/udp/tcp/icmp6 net buffer,
878 // and no need consider any other ahead ext headers.
880 Status
= Ip4IpSecProcessPacket (
890 if (EFI_ERROR (Status
)) {
895 // If the packet is protected by tunnel mode, parse the inner Ip Packet.
897 ZeroMem (&ZeroHead
, sizeof (IP4_HEAD
));
898 if (0 == CompareMem (Head
, &ZeroHead
, sizeof (IP4_HEAD
))) {
899 // Packet may have been changed. Head, HeadLen, TotalLen, and
900 // info must be reloaded bofore use. The ownership of the packet
901 // is transfered to the packet process logic.
903 Head
= (IP4_HEAD
*) NetbufGetByte (Packet
, 0, NULL
);
904 ASSERT (Head
!= NULL
);
905 Status
= Ip4PreProcessPacket (
913 if (EFI_ERROR (Status
)) {
918 ASSERT (Packet
!= NULL
);
919 Head
= Packet
->Ip
.Ip4
;
920 IP4_GET_CLIP_INFO (Packet
)->Status
= EFI_SUCCESS
;
922 switch (Head
->Protocol
) {
923 case EFI_IP_PROTO_ICMP
:
924 Ip4IcmpHandle (IpSb
, Head
, Packet
);
928 Ip4IgmpHandle (IpSb
, Head
, Packet
);
932 Ip4Demultiplex (IpSb
, Head
, Packet
, Option
, OptionLen
);
938 // Dispatch the DPCs queued by the NotifyFunction of the rx token's events
939 // which are signaled with received data.
944 Ip4ReceiveFrame (IpSb
->DefaultInterface
, NULL
, Ip4AccpetFrame
, IpSb
);
947 if (Packet
!= NULL
) {
956 Check whether this IP child accepts the packet.
958 @param[in] IpInstance The IP child to check
959 @param[in] Head The IP header of the packet
960 @param[in] Packet The data of the packet
962 @retval TRUE If the child wants to receive the packet.
963 @retval FALSE Otherwise.
967 Ip4InstanceFrameAcceptable (
968 IN IP4_PROTOCOL
*IpInstance
,
973 IP4_ICMP_ERROR_HEAD Icmp
;
974 EFI_IP4_CONFIG_DATA
*Config
;
979 Config
= &IpInstance
->ConfigData
;
982 // Dirty trick for the Tiano UEFI network stack implmentation. If
983 // ReceiveTimeout == -1, the receive of the packet for this instance
984 // is disabled. The UEFI spec don't have such capability. We add
985 // this to improve the performance because IP will make a copy of
986 // the received packet for each accepting instance. Some IP instances
987 // used by UDP/TCP only send packets, they don't wants to receive.
989 if (Config
->ReceiveTimeout
== (UINT32
)(-1)) {
993 if (Config
->AcceptPromiscuous
) {
998 // Use protocol from the IP header embedded in the ICMP error
999 // message to filter, instead of ICMP itself. ICMP handle will
1000 // call Ip4Demultiplex to deliver ICMP errors.
1002 Proto
= Head
->Protocol
;
1004 if ((Proto
== EFI_IP_PROTO_ICMP
) && (!Config
->AcceptAnyProtocol
) && (Proto
!= Config
->DefaultProtocol
)) {
1005 NetbufCopy (Packet
, 0, sizeof (Icmp
.Head
), (UINT8
*) &Icmp
.Head
);
1007 if (mIcmpClass
[Icmp
.Head
.Type
].IcmpClass
== ICMP_ERROR_MESSAGE
) {
1008 if (!Config
->AcceptIcmpErrors
) {
1012 NetbufCopy (Packet
, 0, sizeof (Icmp
), (UINT8
*) &Icmp
);
1013 Proto
= Icmp
.IpHead
.Protocol
;
1018 // Match the protocol
1020 if (!Config
->AcceptAnyProtocol
&& (Proto
!= Config
->DefaultProtocol
)) {
1025 // Check for broadcast, the caller has computed the packet's
1026 // cast type for this child's interface.
1028 Info
= IP4_GET_CLIP_INFO (Packet
);
1030 if (IP4_IS_BROADCAST (Info
->CastType
)) {
1031 return Config
->AcceptBroadcast
;
1035 // If it is a multicast packet, check whether we are in the group.
1037 if (Info
->CastType
== IP4_MULTICAST
) {
1039 // Receive the multicast if the instance wants to receive all packets.
1041 if (!IpInstance
->ConfigData
.UseDefaultAddress
&& (IpInstance
->Interface
->Ip
== 0)) {
1045 for (Index
= 0; Index
< IpInstance
->GroupCount
; Index
++) {
1046 if (IpInstance
->Groups
[Index
] == HTONL (Head
->Dst
)) {
1051 return (BOOLEAN
)(Index
< IpInstance
->GroupCount
);
1059 Enqueue a shared copy of the packet to the IP4 child if the
1060 packet is acceptable to it. Here the data of the packet is
1061 shared, but the net buffer isn't.
1063 @param[in] IpInstance The IP4 child to enqueue the packet to
1064 @param[in] Head The IP header of the received packet
1065 @param[in] Packet The data of the received packet
1067 @retval EFI_NOT_STARTED The IP child hasn't been configured.
1068 @retval EFI_INVALID_PARAMETER The child doesn't want to receive the packet
1069 @retval EFI_OUT_OF_RESOURCES Failed to allocate some resource
1070 @retval EFI_SUCCESS A shared copy the packet is enqueued to the child.
1074 Ip4InstanceEnquePacket (
1075 IN IP4_PROTOCOL
*IpInstance
,
1080 IP4_CLIP_INFO
*Info
;
1084 // Check whether the packet is acceptable to this instance.
1086 if (IpInstance
->State
!= IP4_STATE_CONFIGED
) {
1087 return EFI_NOT_STARTED
;
1090 if (!Ip4InstanceFrameAcceptable (IpInstance
, Head
, Packet
)) {
1091 return EFI_INVALID_PARAMETER
;
1095 // Enque a shared copy of the packet.
1097 Clone
= NetbufClone (Packet
);
1099 if (Clone
== NULL
) {
1100 return EFI_OUT_OF_RESOURCES
;
1104 // Set the receive time out for the assembled packet. If it expires,
1105 // packet will be removed from the queue.
1107 Info
= IP4_GET_CLIP_INFO (Clone
);
1108 Info
->Life
= IP4_US_TO_SEC (IpInstance
->ConfigData
.ReceiveTimeout
);
1110 InsertTailList (&IpInstance
->Received
, &Clone
->List
);
1116 The signal handle of IP4's recycle event. It is called back
1117 when the upper layer release the packet.
1119 @param Event The IP4's recycle event.
1120 @param Context The context of the handle, which is a
1126 Ip4OnRecyclePacket (
1131 IP4_RXDATA_WRAP
*Wrap
;
1133 Wrap
= (IP4_RXDATA_WRAP
*) Context
;
1135 EfiAcquireLockOrFail (&Wrap
->IpInstance
->RecycleLock
);
1136 RemoveEntryList (&Wrap
->Link
);
1137 EfiReleaseLock (&Wrap
->IpInstance
->RecycleLock
);
1139 ASSERT (!NET_BUF_SHARED (Wrap
->Packet
));
1140 NetbufFree (Wrap
->Packet
);
1142 gBS
->CloseEvent (Wrap
->RxData
.RecycleSignal
);
1148 Wrap the received packet to a IP4_RXDATA_WRAP, which will be
1149 delivered to the upper layer. Each IP4 child that accepts the
1150 packet will get a not-shared copy of the packet which is wrapped
1151 in the IP4_RXDATA_WRAP. The IP4_RXDATA_WRAP->RxData is passed
1152 to the upper layer. Upper layer will signal the recycle event in
1153 it when it is done with the packet.
1155 @param[in] IpInstance The IP4 child to receive the packet.
1156 @param[in] Packet The packet to deliver up.
1158 @retval Wrap if warp the packet succeed.
1159 @retval NULL failed to wrap the packet .
1164 IN IP4_PROTOCOL
*IpInstance
,
1168 IP4_RXDATA_WRAP
*Wrap
;
1169 EFI_IP4_RECEIVE_DATA
*RxData
;
1173 Wrap
= AllocatePool (IP4_RXDATA_WRAP_SIZE (Packet
->BlockOpNum
));
1179 InitializeListHead (&Wrap
->Link
);
1181 Wrap
->IpInstance
= IpInstance
;
1182 Wrap
->Packet
= Packet
;
1183 RxData
= &Wrap
->RxData
;
1185 ZeroMem (RxData
, sizeof (EFI_IP4_RECEIVE_DATA
));
1187 Status
= gBS
->CreateEvent (
1192 &RxData
->RecycleSignal
1195 if (EFI_ERROR (Status
)) {
1200 ASSERT (Packet
->Ip
.Ip4
!= NULL
);
1202 ASSERT (IpInstance
!= NULL
);
1203 RawData
= IpInstance
->ConfigData
.RawData
;
1206 // The application expects a network byte order header.
1209 RxData
->HeaderLength
= (Packet
->Ip
.Ip4
->HeadLen
<< 2);
1210 RxData
->Header
= (EFI_IP4_HEADER
*) Ip4NtohHead (Packet
->Ip
.Ip4
);
1211 RxData
->OptionsLength
= RxData
->HeaderLength
- IP4_MIN_HEADLEN
;
1212 RxData
->Options
= NULL
;
1214 if (RxData
->OptionsLength
!= 0) {
1215 RxData
->Options
= (VOID
*) (RxData
->Header
+ 1);
1219 RxData
->DataLength
= Packet
->TotalSize
;
1222 // Build the fragment table to be delivered up.
1224 RxData
->FragmentCount
= Packet
->BlockOpNum
;
1225 NetbufBuildExt (Packet
, (NET_FRAGMENT
*) RxData
->FragmentTable
, &RxData
->FragmentCount
);
1232 Deliver the received packets to upper layer if there are both received
1233 requests and enqueued packets. If the enqueued packet is shared, it will
1234 duplicate it to a non-shared packet, release the shared packet, then
1235 deliver the non-shared packet up.
1237 @param[in] IpInstance The IP child to deliver the packet up.
1239 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources to deliver the
1241 @retval EFI_SUCCESS All the enqueued packets that can be delivered
1246 Ip4InstanceDeliverPacket (
1247 IN IP4_PROTOCOL
*IpInstance
1250 EFI_IP4_COMPLETION_TOKEN
*Token
;
1251 IP4_RXDATA_WRAP
*Wrap
;
1258 // Deliver a packet if there are both a packet and a receive token.
1260 while (!IsListEmpty (&IpInstance
->Received
) &&
1261 !NetMapIsEmpty (&IpInstance
->RxTokens
)) {
1263 Packet
= NET_LIST_HEAD (&IpInstance
->Received
, NET_BUF
, List
);
1265 if (!NET_BUF_SHARED (Packet
)) {
1267 // If this is the only instance that wants the packet, wrap it up.
1269 Wrap
= Ip4WrapRxData (IpInstance
, Packet
);
1272 return EFI_OUT_OF_RESOURCES
;
1275 RemoveEntryList (&Packet
->List
);
1279 // Create a duplicated packet if this packet is shared
1281 if (IpInstance
->ConfigData
.RawData
) {
1284 HeadLen
= IP4_MAX_HEADLEN
;
1287 Dup
= NetbufDuplicate (Packet
, NULL
, HeadLen
);
1290 return EFI_OUT_OF_RESOURCES
;
1293 if (!IpInstance
->ConfigData
.RawData
) {
1295 // Copy the IP head over. The packet to deliver up is
1296 // headless. Trim the head off after copy. The IP head
1297 // may be not continuous before the data.
1299 Head
= NetbufAllocSpace (Dup
, IP4_MAX_HEADLEN
, NET_BUF_HEAD
);
1300 ASSERT (Head
!= NULL
);
1302 Dup
->Ip
.Ip4
= (IP4_HEAD
*) Head
;
1304 CopyMem (Head
, Packet
->Ip
.Ip4
, Packet
->Ip
.Ip4
->HeadLen
<< 2);
1305 NetbufTrim (Dup
, IP4_MAX_HEADLEN
, TRUE
);
1308 Wrap
= Ip4WrapRxData (IpInstance
, Dup
);
1312 return EFI_OUT_OF_RESOURCES
;
1315 RemoveEntryList (&Packet
->List
);
1316 NetbufFree (Packet
);
1322 // Insert it into the delivered packet, then get a user's
1323 // receive token, pass the wrapped packet up.
1325 EfiAcquireLockOrFail (&IpInstance
->RecycleLock
);
1326 InsertHeadList (&IpInstance
->Delivered
, &Wrap
->Link
);
1327 EfiReleaseLock (&IpInstance
->RecycleLock
);
1329 Token
= NetMapRemoveHead (&IpInstance
->RxTokens
, NULL
);
1330 Token
->Status
= IP4_GET_CLIP_INFO (Packet
)->Status
;
1331 Token
->Packet
.RxData
= &Wrap
->RxData
;
1333 gBS
->SignalEvent (Token
->Event
);
1341 Enqueue a received packet to all the IP children that share
1344 @param[in] IpSb The IP4 service instance that receive the packet.
1345 @param[in] Head The header of the received packet.
1346 @param[in] Packet The data of the received packet.
1347 @param[in] Option Point to the IP4 packet header options.
1348 @param[in] OptionLen Length of the IP4 packet header options.
1349 @param[in] IpIf The interface to enqueue the packet to.
1351 @return The number of the IP4 children that accepts the packet
1355 Ip4InterfaceEnquePacket (
1356 IN IP4_SERVICE
*IpSb
,
1360 IN UINT32 OptionLen
,
1361 IN IP4_INTERFACE
*IpIf
1364 IP4_PROTOCOL
*IpInstance
;
1365 IP4_CLIP_INFO
*Info
;
1372 // First, check that the packet is acceptable to this interface
1373 // and find the local cast type for the interface. A packet sent
1374 // to say 192.168.1.1 should NOT be delliever to 10.0.0.1 unless
1375 // promiscuous receiving.
1378 Info
= IP4_GET_CLIP_INFO (Packet
);
1380 if ((Info
->CastType
== IP4_MULTICAST
) || (Info
->CastType
== IP4_LOCAL_BROADCAST
)) {
1382 // If the CastType is multicast, don't need to filter against
1383 // the group address here, Ip4InstanceFrameAcceptable will do
1386 LocalType
= Info
->CastType
;
1390 // Check the destination againist local IP. If the station
1391 // address is 0.0.0.0, it means receiving all the IP destined
1392 // to local non-zero IP. Otherwise, it is necessary to compare
1393 // the destination to the interface's IP address.
1395 if (IpIf
->Ip
== IP4_ALLZERO_ADDRESS
) {
1396 LocalType
= IP4_LOCAL_HOST
;
1399 LocalType
= Ip4GetNetCast (Head
->Dst
, IpIf
);
1401 if ((LocalType
== 0) && IpIf
->PromiscRecv
) {
1402 LocalType
= IP4_PROMISCUOUS
;
1407 if (LocalType
== 0) {
1412 // Iterate through the ip instances on the interface, enqueue
1413 // the packet if filter passed. Save the original cast type,
1414 // and pass the local cast type to the IP children on the
1415 // interface. The global cast type will be restored later.
1417 SavedType
= Info
->CastType
;
1418 Info
->CastType
= LocalType
;
1422 NET_LIST_FOR_EACH (Entry
, &IpIf
->IpInstances
) {
1423 IpInstance
= NET_LIST_USER_STRUCT (Entry
, IP4_PROTOCOL
, AddrLink
);
1424 NET_CHECK_SIGNATURE (IpInstance
, IP4_PROTOCOL_SIGNATURE
);
1427 // In RawData mode, add IPv4 headers and options back to packet.
1429 if ((IpInstance
->ConfigData
.RawData
) && (Option
!= NULL
) && (OptionLen
!= 0)){
1430 Ip4PrependHead (Packet
, Head
, Option
, OptionLen
);
1433 if (Ip4InstanceEnquePacket (IpInstance
, Head
, Packet
) == EFI_SUCCESS
) {
1438 Info
->CastType
= SavedType
;
1444 Deliver the packet for each IP4 child on the interface.
1446 @param[in] IpSb The IP4 service instance that received the packet
1447 @param[in] IpIf The IP4 interface to deliver the packet.
1449 @retval EFI_SUCCESS It always returns EFI_SUCCESS now
1453 Ip4InterfaceDeliverPacket (
1454 IN IP4_SERVICE
*IpSb
,
1455 IN IP4_INTERFACE
*IpIf
1458 IP4_PROTOCOL
*Ip4Instance
;
1461 NET_LIST_FOR_EACH (Entry
, &IpIf
->IpInstances
) {
1462 Ip4Instance
= NET_LIST_USER_STRUCT (Entry
, IP4_PROTOCOL
, AddrLink
);
1463 Ip4InstanceDeliverPacket (Ip4Instance
);
1471 Demultiple the packet. the packet delivery is processed in two
1472 passes. The first pass will enque a shared copy of the packet
1473 to each IP4 child that accepts the packet. The second pass will
1474 deliver a non-shared copy of the packet to each IP4 child that
1475 has pending receive requests. Data is copied if more than one
1476 child wants to consume the packet because each IP child needs
1477 its own copy of the packet to make changes.
1479 @param[in] IpSb The IP4 service instance that received the packet.
1480 @param[in] Head The header of the received packet.
1481 @param[in] Packet The data of the received packet.
1482 @param[in] Option Point to the IP4 packet header options.
1483 @param[in] OptionLen Length of the IP4 packet header options.
1485 @retval EFI_NOT_FOUND No IP child accepts the packet.
1486 @retval EFI_SUCCESS The packet is enqueued or delivered to some IP
1492 IN IP4_SERVICE
*IpSb
,
1500 IP4_INTERFACE
*IpIf
;
1504 // Two pass delivery: first, enque a shared copy of the packet
1505 // to each instance that accept the packet.
1509 NET_LIST_FOR_EACH (Entry
, &IpSb
->Interfaces
) {
1510 IpIf
= NET_LIST_USER_STRUCT (Entry
, IP4_INTERFACE
, Link
);
1512 if (IpIf
->Configured
) {
1513 Enqueued
+= Ip4InterfaceEnquePacket (
1525 // Second: deliver a duplicate of the packet to each instance.
1526 // Release the local reference first, so that the last instance
1527 // getting the packet will not copy the data.
1529 NetbufFree (Packet
);
1531 if (Enqueued
== 0) {
1532 return EFI_NOT_FOUND
;
1535 NET_LIST_FOR_EACH (Entry
, &IpSb
->Interfaces
) {
1536 IpIf
= NET_LIST_USER_STRUCT (Entry
, IP4_INTERFACE
, Link
);
1538 if (IpIf
->Configured
) {
1539 Ip4InterfaceDeliverPacket (IpSb
, IpIf
);
1548 Timeout the fragment and enqueued packets.
1550 @param[in] IpSb The IP4 service instance to timeout
1554 Ip4PacketTimerTicking (
1555 IN IP4_SERVICE
*IpSb
1558 LIST_ENTRY
*InstanceEntry
;
1561 IP4_PROTOCOL
*IpInstance
;
1562 IP4_ASSEMBLE_ENTRY
*Assemble
;
1564 IP4_CLIP_INFO
*Info
;
1568 // First, time out the fragments. The packet's life is counting down
1569 // once the first-arrived fragment was received.
1571 for (Index
= 0; Index
< IP4_ASSEMLE_HASH_SIZE
; Index
++) {
1572 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &IpSb
->Assemble
.Bucket
[Index
]) {
1573 Assemble
= NET_LIST_USER_STRUCT (Entry
, IP4_ASSEMBLE_ENTRY
, Link
);
1575 if ((Assemble
->Life
> 0) && (--Assemble
->Life
== 0)) {
1576 RemoveEntryList (Entry
);
1577 Ip4FreeAssembleEntry (Assemble
);
1582 NET_LIST_FOR_EACH (InstanceEntry
, &IpSb
->Children
) {
1583 IpInstance
= NET_LIST_USER_STRUCT (InstanceEntry
, IP4_PROTOCOL
, Link
);
1586 // Second, time out the assembled packets enqueued on each IP child.
1588 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &IpInstance
->Received
) {
1589 Packet
= NET_LIST_USER_STRUCT (Entry
, NET_BUF
, List
);
1590 Info
= IP4_GET_CLIP_INFO (Packet
);
1592 if ((Info
->Life
> 0) && (--Info
->Life
== 0)) {
1593 RemoveEntryList (Entry
);
1594 NetbufFree (Packet
);
1599 // Third: time out the transmitted packets.
1601 NetMapIterate (&IpInstance
->TxTokens
, Ip4SentPacketTicking
, NULL
);