4 Copyright (c) 2005 - 2014, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 Create an empty assemble entry for the packet identified by
20 (Dst, Src, Id, Protocol). The default life for the packet is
23 @param[in] Dst The destination address
24 @param[in] Src The source address
25 @param[in] Id The ID field in IP header
26 @param[in] Protocol The protocol field in IP header
28 @return NULL if failed to allocate memory for the entry, otherwise
29 the point to just created reassemble entry.
33 Ip4CreateAssembleEntry (
41 IP4_ASSEMBLE_ENTRY
*Assemble
;
43 Assemble
= AllocatePool (sizeof (IP4_ASSEMBLE_ENTRY
));
45 if (Assemble
== NULL
) {
49 InitializeListHead (&Assemble
->Link
);
50 InitializeListHead (&Assemble
->Fragments
);
55 Assemble
->Protocol
= Protocol
;
56 Assemble
->TotalLen
= 0;
58 Assemble
->Head
= NULL
;
59 Assemble
->Info
= NULL
;
60 Assemble
->Life
= IP4_FRAGMENT_LIFE
;
67 Release all the fragments of a packet, then free the assemble entry.
69 @param[in] Assemble The assemble entry to free
73 Ip4FreeAssembleEntry (
74 IN IP4_ASSEMBLE_ENTRY
*Assemble
81 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &Assemble
->Fragments
) {
82 Fragment
= NET_LIST_USER_STRUCT (Entry
, NET_BUF
, List
);
84 RemoveEntryList (Entry
);
85 NetbufFree (Fragment
);
93 Initialize an already allocated assemble table. This is generally
94 the assemble table embedded in the IP4 service instance.
96 @param[in, out] Table The assemble table to initialize.
100 Ip4InitAssembleTable (
101 IN OUT IP4_ASSEMBLE_TABLE
*Table
106 for (Index
= 0; Index
< IP4_ASSEMLE_HASH_SIZE
; Index
++) {
107 InitializeListHead (&Table
->Bucket
[Index
]);
113 Clean up the assemble table: remove all the fragments
114 and assemble entries.
116 @param[in] Table The assemble table to clean up
120 Ip4CleanAssembleTable (
121 IN IP4_ASSEMBLE_TABLE
*Table
126 IP4_ASSEMBLE_ENTRY
*Assemble
;
129 for (Index
= 0; Index
< IP4_ASSEMLE_HASH_SIZE
; Index
++) {
130 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &Table
->Bucket
[Index
]) {
131 Assemble
= NET_LIST_USER_STRUCT (Entry
, IP4_ASSEMBLE_ENTRY
, Link
);
133 RemoveEntryList (Entry
);
134 Ip4FreeAssembleEntry (Assemble
);
141 Trim the packet to fit in [Start, End), and update the per
144 @param Packet Packet to trim
145 @param Start The sequence of the first byte to fit in
146 @param End One beyond the sequence of last byte to fit in.
151 IN OUT NET_BUF
*Packet
,
159 Info
= IP4_GET_CLIP_INFO (Packet
);
161 ASSERT (Info
->Start
+ Info
->Length
== Info
->End
);
162 ASSERT ((Info
->Start
< End
) && (Start
< Info
->End
));
164 if (Info
->Start
< Start
) {
165 Len
= Start
- Info
->Start
;
167 NetbufTrim (Packet
, (UINT32
) Len
, NET_BUF_HEAD
);
172 if (End
< Info
->End
) {
173 Len
= End
- Info
->End
;
175 NetbufTrim (Packet
, (UINT32
) Len
, NET_BUF_TAIL
);
183 Release all the fragments of the packet. This is the callback for
184 the assembled packet's OnFree. It will free the assemble entry,
185 which in turn will free all the fragments of the packet.
187 @param[in] Arg The assemble entry to free
196 Ip4FreeAssembleEntry ((IP4_ASSEMBLE_ENTRY
*) Arg
);
201 Reassemble the IP fragments. If all the fragments of the packet
202 have been received, it will wrap the packet in a net buffer then
203 return it to caller. If the packet can't be assembled, NULL is
206 @param Table The assemble table used. New assemble entry will be created
207 if the Packet is from a new chain of fragments.
208 @param Packet The fragment to assemble. It might be freed if the fragment
209 can't be re-assembled.
211 @return NULL if the packet can't be reassemble. The point to just assembled
212 packet if all the fragments of the packet have arrived.
217 IN OUT IP4_ASSEMBLE_TABLE
*Table
,
218 IN OUT NET_BUF
*Packet
224 IP4_ASSEMBLE_ENTRY
*Assemble
;
232 IpHead
= Packet
->Ip
.Ip4
;
233 This
= IP4_GET_CLIP_INFO (Packet
);
235 ASSERT (IpHead
!= NULL
);
238 // First: find the related assemble entry
241 Index
= IP4_ASSEMBLE_HASH (IpHead
->Dst
, IpHead
->Src
, IpHead
->Id
, IpHead
->Protocol
);
243 NET_LIST_FOR_EACH (Cur
, &Table
->Bucket
[Index
]) {
244 Assemble
= NET_LIST_USER_STRUCT (Cur
, IP4_ASSEMBLE_ENTRY
, Link
);
246 if ((Assemble
->Dst
== IpHead
->Dst
) && (Assemble
->Src
== IpHead
->Src
) &&
247 (Assemble
->Id
== IpHead
->Id
) && (Assemble
->Protocol
== IpHead
->Protocol
)) {
253 // Create a new assemble entry if no assemble entry is related to this packet
255 if (Cur
== &Table
->Bucket
[Index
]) {
256 Assemble
= Ip4CreateAssembleEntry (
263 if (Assemble
== NULL
) {
267 InsertHeadList (&Table
->Bucket
[Index
], &Assemble
->Link
);
270 // Assemble shouldn't be NULL here
272 ASSERT (Assemble
!= NULL
);
275 // Find the point to insert the packet: before the first
276 // fragment with THIS.Start < CUR.Start. the previous one
277 // has PREV.Start <= THIS.Start < CUR.Start.
279 Head
= &Assemble
->Fragments
;
281 NET_LIST_FOR_EACH (Cur
, Head
) {
282 Fragment
= NET_LIST_USER_STRUCT (Cur
, NET_BUF
, List
);
284 if (This
->Start
< IP4_GET_CLIP_INFO (Fragment
)->Start
) {
290 // Check whether the current fragment overlaps with the previous one.
291 // It holds that: PREV.Start <= THIS.Start < THIS.End. Only need to
292 // check whether THIS.Start < PREV.End for overlap. If two fragments
293 // overlaps, trim the overlapped part off THIS fragment.
295 if ((Prev
= Cur
->BackLink
) != Head
) {
296 Fragment
= NET_LIST_USER_STRUCT (Prev
, NET_BUF
, List
);
297 Node
= IP4_GET_CLIP_INFO (Fragment
);
299 if (This
->Start
< Node
->End
) {
300 if (This
->End
<= Node
->End
) {
305 Ip4TrimPacket (Packet
, Node
->End
, This
->End
);
310 // Insert the fragment into the packet. The fragment may be removed
311 // from the list by the following checks.
313 NetListInsertBefore (Cur
, &Packet
->List
);
316 // Check the packets after the insert point. It holds that:
317 // THIS.Start <= NODE.Start < NODE.End. The equality holds
318 // if PREV and NEXT are continuous. THIS fragment may fill
319 // several holes. Remove the completely overlapped fragments
321 while (Cur
!= Head
) {
322 Fragment
= NET_LIST_USER_STRUCT (Cur
, NET_BUF
, List
);
323 Node
= IP4_GET_CLIP_INFO (Fragment
);
326 // Remove fragments completely overlapped by this fragment
328 if (Node
->End
<= This
->End
) {
329 Cur
= Cur
->ForwardLink
;
331 RemoveEntryList (&Fragment
->List
);
332 Assemble
->CurLen
-= Node
->Length
;
334 NetbufFree (Fragment
);
339 // The conditions are: THIS.Start <= NODE.Start, and THIS.End <
340 // NODE.End. Two fragments overlaps if NODE.Start < THIS.End.
341 // If two fragments start at the same offset, remove THIS fragment
342 // because ((THIS.Start == NODE.Start) && (THIS.End < NODE.End)).
344 if (Node
->Start
< This
->End
) {
345 if (This
->Start
== Node
->Start
) {
346 RemoveEntryList (&Packet
->List
);
350 Ip4TrimPacket (Packet
, This
->Start
, Node
->Start
);
357 // Update the assemble info: increase the current length. If it is
358 // the frist fragment, update the packet's IP head and per packet
359 // info. If it is the last fragment, update the total length.
361 Assemble
->CurLen
+= This
->Length
;
363 if (This
->Start
== 0) {
365 // Once the first fragment is enqueued, it can't be removed
366 // from the fragment list. So, Assemble->Head always point
367 // to valid memory area.
369 ASSERT (Assemble
->Head
== NULL
);
371 Assemble
->Head
= IpHead
;
372 Assemble
->Info
= IP4_GET_CLIP_INFO (Packet
);
376 // Don't update the length more than once.
378 if (IP4_LAST_FRAGMENT (IpHead
->Fragment
) && (Assemble
->TotalLen
== 0)) {
379 Assemble
->TotalLen
= This
->End
;
383 // Deliver the whole packet if all the fragments received.
384 // All fragments received if:
385 // 1. received the last one, so, the total length is know
386 // 2. received all the data. If the last fragment on the
387 // queue ends at the total length, all data is received.
389 if ((Assemble
->TotalLen
!= 0) && (Assemble
->CurLen
>= Assemble
->TotalLen
)) {
391 RemoveEntryList (&Assemble
->Link
);
394 // If the packet is properly formated, the last fragment's End
395 // equals to the packet's total length. Otherwise, the packet
396 // is a fake, drop it now.
398 Fragment
= NET_LIST_USER_STRUCT (Head
->BackLink
, NET_BUF
, List
);
400 if (IP4_GET_CLIP_INFO (Fragment
)->End
!= Assemble
->TotalLen
) {
401 Ip4FreeAssembleEntry (Assemble
);
406 // Wrap the packet in a net buffer then deliver it up
408 NewPacket
= NetbufFromBufList (
409 &Assemble
->Fragments
,
416 if (NewPacket
== NULL
) {
417 Ip4FreeAssembleEntry (Assemble
);
421 NewPacket
->Ip
.Ip4
= Assemble
->Head
;
423 ASSERT (Assemble
->Info
!= NULL
);
426 IP4_GET_CLIP_INFO (NewPacket
),
428 sizeof (*IP4_GET_CLIP_INFO (NewPacket
))
442 The callback function for the net buffer which wraps the packet processed by
443 IPsec. It releases the wrap packet and also signals IPsec to free the resources.
445 @param[in] Arg The wrap context
454 IP4_IPSEC_WRAP
*Wrap
;
456 Wrap
= (IP4_IPSEC_WRAP
*) Arg
;
458 if (Wrap
->IpSecRecycleSignal
!= NULL
) {
459 gBS
->SignalEvent (Wrap
->IpSecRecycleSignal
);
462 NetbufFree (Wrap
->Packet
);
470 The work function to locate IPsec protocol to process the inbound or
471 outbound IP packets. The process routine handls the packet with following
472 actions: bypass the packet, discard the packet, or protect the packet.
474 @param[in] IpSb The IP4 service instance.
475 @param[in, out] Head The The caller supplied IP4 header.
476 @param[in, out] Netbuf The IP4 packet to be processed by IPsec.
477 @param[in, out] Options The caller supplied options.
478 @param[in, out] OptionsLen The length of the option.
479 @param[in] Direction The directionality in an SPD entry,
480 EfiIPsecInBound or EfiIPsecOutBound.
481 @param[in] Context The token's wrap.
483 @retval EFI_SUCCESS The IPsec protocol is not available or disabled.
484 @retval EFI_SUCCESS The packet was bypassed and all buffers remain the same.
485 @retval EFI_SUCCESS The packet was protected.
486 @retval EFI_ACCESS_DENIED The packet was discarded.
487 @retval EFI_OUT_OF_RESOURCES There is no suffcient resource to complete the operation.
488 @retval EFI_BUFFER_TOO_SMALL The number of non-empty block is bigger than the
489 number of input data blocks when build a fragment table.
493 Ip4IpSecProcessPacket (
494 IN IP4_SERVICE
*IpSb
,
495 IN OUT IP4_HEAD
**Head
,
496 IN OUT NET_BUF
**Netbuf
,
497 IN OUT UINT8
**Options
,
498 IN OUT UINT32
*OptionsLen
,
499 IN EFI_IPSEC_TRAFFIC_DIR Direction
,
503 NET_FRAGMENT
*FragmentTable
;
504 NET_FRAGMENT
*OriginalFragmentTable
;
505 UINT32 FragmentCount
;
506 UINT32 OriginalFragmentCount
;
507 EFI_EVENT RecycleEvent
;
509 IP4_TXTOKEN_WRAP
*TxWrap
;
510 IP4_IPSEC_WRAP
*IpSecWrap
;
514 Status
= EFI_SUCCESS
;
518 FragmentTable
= NULL
;
519 TxWrap
= (IP4_TXTOKEN_WRAP
*) Context
;
520 FragmentCount
= Packet
->BlockOpNum
;
522 ZeroMem (&ZeroHead
, sizeof (IP4_HEAD
));
524 if (mIpSec
== NULL
) {
525 gBS
->LocateProtocol (&gEfiIpSec2ProtocolGuid
, NULL
, (VOID
**) &mIpSec
);
526 if (mIpSec
== NULL
) {
532 // Check whether the IPsec enable variable is set.
534 if (mIpSec
->DisabledFlag
) {
536 // If IPsec is disabled, restore the original MTU
538 IpSb
->MaxPacketSize
= IpSb
->OldMaxPacketSize
;
542 // If IPsec is enabled, use the MTU which reduce the IPsec header length.
544 IpSb
->MaxPacketSize
= IpSb
->OldMaxPacketSize
- IP4_MAX_IPSEC_HEADLEN
;
548 // Rebuild fragment table from netbuf to ease IPsec process.
550 FragmentTable
= AllocateZeroPool (FragmentCount
* sizeof (NET_FRAGMENT
));
552 if (FragmentTable
== NULL
) {
553 Status
= EFI_OUT_OF_RESOURCES
;
557 Status
= NetbufBuildExt (Packet
, FragmentTable
, &FragmentCount
);
560 // Record the original FragmentTable and count.
562 OriginalFragmentTable
= FragmentTable
;
563 OriginalFragmentCount
= FragmentCount
;
565 if (EFI_ERROR (Status
)) {
566 FreePool (FragmentTable
);
571 // Convert host byte order to network byte order
575 Status
= mIpSec
->ProcessExt (
583 (EFI_IPSEC_FRAGMENT_DATA
**) (&FragmentTable
),
589 // Convert back to host byte order
593 if (EFI_ERROR (Status
)) {
594 FreePool (OriginalFragmentTable
);
598 if (OriginalFragmentTable
== FragmentTable
&& OriginalFragmentCount
== FragmentCount
) {
602 FreePool (FragmentTable
);
606 // Free the FragmentTable which allocated before calling the IPsec.
608 FreePool (OriginalFragmentTable
);
611 if (Direction
== EfiIPsecOutBound
&& TxWrap
!= NULL
) {
613 TxWrap
->IpSecRecycleSignal
= RecycleEvent
;
614 TxWrap
->Packet
= NetbufFromExt (
622 if (TxWrap
->Packet
== NULL
) {
624 // Recover the TxWrap->Packet, if meet a error, and the caller will free
627 TxWrap
->Packet
= *Netbuf
;
628 Status
= EFI_OUT_OF_RESOURCES
;
633 // Free orginal Netbuf.
635 NetIpSecNetbufFree (*Netbuf
);
636 *Netbuf
= TxWrap
->Packet
;
640 IpSecWrap
= AllocateZeroPool (sizeof (IP4_IPSEC_WRAP
));
642 if (IpSecWrap
== NULL
) {
643 Status
= EFI_OUT_OF_RESOURCES
;
644 gBS
->SignalEvent (RecycleEvent
);
648 IpSecWrap
->IpSecRecycleSignal
= RecycleEvent
;
649 IpSecWrap
->Packet
= Packet
;
650 Packet
= NetbufFromExt (
659 if (Packet
== NULL
) {
660 Packet
= IpSecWrap
->Packet
;
661 gBS
->SignalEvent (RecycleEvent
);
662 FreePool (IpSecWrap
);
663 Status
= EFI_OUT_OF_RESOURCES
;
667 if (Direction
== EfiIPsecInBound
&& 0 != CompareMem (*Head
, &ZeroHead
, sizeof (IP4_HEAD
))) {
668 Ip4PrependHead (Packet
, *Head
, *Options
, *OptionsLen
);
669 Ip4NtohHead (Packet
->Ip
.Ip4
);
670 NetbufTrim (Packet
, ((*Head
)->HeadLen
<< 2), TRUE
);
673 IP4_GET_CLIP_INFO (Packet
),
674 IP4_GET_CLIP_INFO (IpSecWrap
->Packet
),
675 sizeof (IP4_CLIP_INFO
)
686 Pre-process the IPv4 packet. First validates the IPv4 packet, and
687 then reassembles packet if it is necessary.
689 @param[in] IpSb Pointer to IP4_SERVICE.
690 @param[in, out] Packet Pointer to the Packet to be processed.
691 @param[in] Head Pointer to the IP4_HEAD.
692 @param[in] Option Pointer to a buffer which contains the IPv4 option.
693 @param[in] OptionLen The length of Option in bytes.
694 @param[in] Flag The link layer flag for the packet received, such
697 @retval EFI_SEUCCESS The recieved packet is in well form.
698 @retval EFI_INVAILD_PARAMETER The recieved packet is malformed.
702 Ip4PreProcessPacket (
703 IN IP4_SERVICE
*IpSb
,
704 IN OUT NET_BUF
**Packet
,
717 // Check if the IP4 header is correctly formatted.
719 if ((*Packet
)->TotalSize
< IP4_MIN_HEADLEN
) {
720 return EFI_INVALID_PARAMETER
;
723 HeadLen
= (Head
->HeadLen
<< 2);
724 TotalLen
= NTOHS (Head
->TotalLen
);
727 // Mnp may deliver frame trailer sequence up, trim it off.
729 if (TotalLen
< (*Packet
)->TotalSize
) {
730 NetbufTrim (*Packet
, (*Packet
)->TotalSize
- TotalLen
, FALSE
);
733 if ((Head
->Ver
!= 4) || (HeadLen
< IP4_MIN_HEADLEN
) ||
734 (TotalLen
< HeadLen
) || (TotalLen
!= (*Packet
)->TotalSize
)) {
735 return EFI_INVALID_PARAMETER
;
739 // Some OS may send IP packets without checksum.
741 Checksum
= (UINT16
) (~NetblockChecksum ((UINT8
*) Head
, HeadLen
));
743 if ((Head
->Checksum
!= 0) && (Checksum
!= 0)) {
744 return EFI_INVALID_PARAMETER
;
748 // Convert the IP header to host byte order, then get the per packet info.
750 (*Packet
)->Ip
.Ip4
= Ip4NtohHead (Head
);
752 Info
= IP4_GET_CLIP_INFO (*Packet
);
753 Info
->LinkFlag
= Flag
;
754 Info
->CastType
= Ip4GetHostCast (IpSb
, Head
->Dst
, Head
->Src
);
755 Info
->Start
= (Head
->Fragment
& IP4_HEAD_OFFSET_MASK
) << 3;
756 Info
->Length
= Head
->TotalLen
- HeadLen
;
757 Info
->End
= Info
->Start
+ Info
->Length
;
758 Info
->Status
= EFI_SUCCESS
;
761 // The packet is destinated to us if the CastType is non-zero.
763 if ((Info
->CastType
== 0) || (Info
->End
> IP4_MAX_PACKET_SIZE
)) {
764 return EFI_INVALID_PARAMETER
;
768 // Validate the options. Don't call the Ip4OptionIsValid if
769 // there is no option to save some CPU process.
772 if ((OptionLen
> 0) && !Ip4OptionIsValid (Option
, OptionLen
, TRUE
)) {
773 return EFI_INVALID_PARAMETER
;
777 // Trim the head off, after this point, the packet is headless,
778 // and Packet->TotalLen == Info->Length.
780 NetbufTrim (*Packet
, HeadLen
, TRUE
);
783 // Reassemble the packet if this is a fragment. The packet is a
784 // fragment if its head has MF (more fragment) set, or it starts
787 if (((Head
->Fragment
& IP4_HEAD_MF_MASK
) != 0) || (Info
->Start
!= 0)) {
789 // Drop the fragment if DF is set but it is fragmented. Gateway
790 // need to send a type 4 destination unreache ICMP message here.
792 if ((Head
->Fragment
& IP4_HEAD_DF_MASK
) != 0) {
793 return EFI_INVALID_PARAMETER
;
797 // The length of all but the last fragments is in the unit of 8 bytes.
799 if (((Head
->Fragment
& IP4_HEAD_MF_MASK
) != 0) && (Info
->Length
% 8 != 0)) {
800 return EFI_INVALID_PARAMETER
;
803 *Packet
= Ip4Reassemble (&IpSb
->Assemble
, *Packet
);
806 // Packet assembly isn't complete, start receive more packet.
808 if (*Packet
== NULL
) {
809 return EFI_INVALID_PARAMETER
;
817 The IP4 input routine. It is called by the IP4_INTERFACE when a
818 IP4 fragment is received from MNP.
820 @param[in] Ip4Instance The IP4 child that request the receive, most like
822 @param[in] Packet The IP4 packet received.
823 @param[in] IoStatus The return status of receive request.
824 @param[in] Flag The link layer flag for the packet received, such
826 @param[in] Context The IP4 service instance that own the MNP.
831 IN IP4_PROTOCOL
*Ip4Instance
,
833 IN EFI_STATUS IoStatus
,
845 IpSb
= (IP4_SERVICE
*) Context
;
848 if (EFI_ERROR (IoStatus
) || (IpSb
->State
== IP4_SERVICE_DESTROY
)) {
852 Head
= (IP4_HEAD
*) NetbufGetByte (Packet
, 0, NULL
);
853 ASSERT (Head
!= NULL
);
854 OptionLen
= (Head
->HeadLen
<< 2) - IP4_MIN_HEADLEN
;
856 Option
= (UINT8
*) (Head
+ 1);
860 // Validate packet format and reassemble packet if it is necessary.
862 Status
= Ip4PreProcessPacket (
871 if (EFI_ERROR (Status
)) {
876 // After trim off, the packet is a esp/ah/udp/tcp/icmp6 net buffer,
877 // and no need consider any other ahead ext headers.
879 Status
= Ip4IpSecProcessPacket (
889 if (EFI_ERROR (Status
)) {
894 // If the packet is protected by tunnel mode, parse the inner Ip Packet.
896 ZeroMem (&ZeroHead
, sizeof (IP4_HEAD
));
897 if (0 == CompareMem (Head
, &ZeroHead
, sizeof (IP4_HEAD
))) {
898 // Packet may have been changed. Head, HeadLen, TotalLen, and
899 // info must be reloaded bofore use. The ownership of the packet
900 // is transfered to the packet process logic.
902 Head
= (IP4_HEAD
*) NetbufGetByte (Packet
, 0, NULL
);
903 ASSERT (Head
!= NULL
);
904 Status
= Ip4PreProcessPacket (
912 if (EFI_ERROR (Status
)) {
917 ASSERT (Packet
!= NULL
);
918 Head
= Packet
->Ip
.Ip4
;
919 IP4_GET_CLIP_INFO (Packet
)->Status
= EFI_SUCCESS
;
921 switch (Head
->Protocol
) {
922 case EFI_IP_PROTO_ICMP
:
923 Ip4IcmpHandle (IpSb
, Head
, Packet
);
927 Ip4IgmpHandle (IpSb
, Head
, Packet
);
931 Ip4Demultiplex (IpSb
, Head
, Packet
, Option
, OptionLen
);
937 // Dispatch the DPCs queued by the NotifyFunction of the rx token's events
938 // which are signaled with received data.
943 Ip4ReceiveFrame (IpSb
->DefaultInterface
, NULL
, Ip4AccpetFrame
, IpSb
);
946 if (Packet
!= NULL
) {
955 Check whether this IP child accepts the packet.
957 @param[in] IpInstance The IP child to check
958 @param[in] Head The IP header of the packet
959 @param[in] Packet The data of the packet
961 @retval TRUE If the child wants to receive the packet.
962 @retval FALSE Otherwise.
966 Ip4InstanceFrameAcceptable (
967 IN IP4_PROTOCOL
*IpInstance
,
972 IP4_ICMP_ERROR_HEAD Icmp
;
973 EFI_IP4_CONFIG_DATA
*Config
;
978 Config
= &IpInstance
->ConfigData
;
981 // Dirty trick for the Tiano UEFI network stack implmentation. If
982 // ReceiveTimeout == -1, the receive of the packet for this instance
983 // is disabled. The UEFI spec don't have such capability. We add
984 // this to improve the performance because IP will make a copy of
985 // the received packet for each accepting instance. Some IP instances
986 // used by UDP/TCP only send packets, they don't wants to receive.
988 if (Config
->ReceiveTimeout
== (UINT32
)(-1)) {
992 if (Config
->AcceptPromiscuous
) {
997 // Use protocol from the IP header embedded in the ICMP error
998 // message to filter, instead of ICMP itself. ICMP handle will
999 // call Ip4Demultiplex to deliver ICMP errors.
1001 Proto
= Head
->Protocol
;
1003 if ((Proto
== EFI_IP_PROTO_ICMP
) && (!Config
->AcceptAnyProtocol
) && (Proto
!= Config
->DefaultProtocol
)) {
1004 NetbufCopy (Packet
, 0, sizeof (Icmp
.Head
), (UINT8
*) &Icmp
.Head
);
1006 if (mIcmpClass
[Icmp
.Head
.Type
].IcmpClass
== ICMP_ERROR_MESSAGE
) {
1007 if (!Config
->AcceptIcmpErrors
) {
1011 NetbufCopy (Packet
, 0, sizeof (Icmp
), (UINT8
*) &Icmp
);
1012 Proto
= Icmp
.IpHead
.Protocol
;
1017 // Match the protocol
1019 if (!Config
->AcceptAnyProtocol
&& (Proto
!= Config
->DefaultProtocol
)) {
1024 // Check for broadcast, the caller has computed the packet's
1025 // cast type for this child's interface.
1027 Info
= IP4_GET_CLIP_INFO (Packet
);
1029 if (IP4_IS_BROADCAST (Info
->CastType
)) {
1030 return Config
->AcceptBroadcast
;
1034 // If it is a multicast packet, check whether we are in the group.
1036 if (Info
->CastType
== IP4_MULTICAST
) {
1038 // Receive the multicast if the instance wants to receive all packets.
1040 if (!IpInstance
->ConfigData
.UseDefaultAddress
&& (IpInstance
->Interface
->Ip
== 0)) {
1044 for (Index
= 0; Index
< IpInstance
->GroupCount
; Index
++) {
1045 if (IpInstance
->Groups
[Index
] == HTONL (Head
->Dst
)) {
1050 return (BOOLEAN
)(Index
< IpInstance
->GroupCount
);
1058 Enqueue a shared copy of the packet to the IP4 child if the
1059 packet is acceptable to it. Here the data of the packet is
1060 shared, but the net buffer isn't.
1062 @param[in] IpInstance The IP4 child to enqueue the packet to
1063 @param[in] Head The IP header of the received packet
1064 @param[in] Packet The data of the received packet
1066 @retval EFI_NOT_STARTED The IP child hasn't been configured.
1067 @retval EFI_INVALID_PARAMETER The child doesn't want to receive the packet
1068 @retval EFI_OUT_OF_RESOURCES Failed to allocate some resource
1069 @retval EFI_SUCCESS A shared copy the packet is enqueued to the child.
1073 Ip4InstanceEnquePacket (
1074 IN IP4_PROTOCOL
*IpInstance
,
1079 IP4_CLIP_INFO
*Info
;
1083 // Check whether the packet is acceptable to this instance.
1085 if (IpInstance
->State
!= IP4_STATE_CONFIGED
) {
1086 return EFI_NOT_STARTED
;
1089 if (!Ip4InstanceFrameAcceptable (IpInstance
, Head
, Packet
)) {
1090 return EFI_INVALID_PARAMETER
;
1094 // Enque a shared copy of the packet.
1096 Clone
= NetbufClone (Packet
);
1098 if (Clone
== NULL
) {
1099 return EFI_OUT_OF_RESOURCES
;
1103 // Set the receive time out for the assembled packet. If it expires,
1104 // packet will be removed from the queue.
1106 Info
= IP4_GET_CLIP_INFO (Clone
);
1107 Info
->Life
= IP4_US_TO_SEC (IpInstance
->ConfigData
.ReceiveTimeout
);
1109 InsertTailList (&IpInstance
->Received
, &Clone
->List
);
1115 The signal handle of IP4's recycle event. It is called back
1116 when the upper layer release the packet.
1118 @param Event The IP4's recycle event.
1119 @param Context The context of the handle, which is a
1125 Ip4OnRecyclePacket (
1130 IP4_RXDATA_WRAP
*Wrap
;
1132 Wrap
= (IP4_RXDATA_WRAP
*) Context
;
1134 EfiAcquireLockOrFail (&Wrap
->IpInstance
->RecycleLock
);
1135 RemoveEntryList (&Wrap
->Link
);
1136 EfiReleaseLock (&Wrap
->IpInstance
->RecycleLock
);
1138 ASSERT (!NET_BUF_SHARED (Wrap
->Packet
));
1139 NetbufFree (Wrap
->Packet
);
1141 gBS
->CloseEvent (Wrap
->RxData
.RecycleSignal
);
1147 Wrap the received packet to a IP4_RXDATA_WRAP, which will be
1148 delivered to the upper layer. Each IP4 child that accepts the
1149 packet will get a not-shared copy of the packet which is wrapped
1150 in the IP4_RXDATA_WRAP. The IP4_RXDATA_WRAP->RxData is passed
1151 to the upper layer. Upper layer will signal the recycle event in
1152 it when it is done with the packet.
1154 @param[in] IpInstance The IP4 child to receive the packet.
1155 @param[in] Packet The packet to deliver up.
1157 @retval Wrap if warp the packet succeed.
1158 @retval NULL failed to wrap the packet .
1163 IN IP4_PROTOCOL
*IpInstance
,
1167 IP4_RXDATA_WRAP
*Wrap
;
1168 EFI_IP4_RECEIVE_DATA
*RxData
;
1172 Wrap
= AllocatePool (IP4_RXDATA_WRAP_SIZE (Packet
->BlockOpNum
));
1178 InitializeListHead (&Wrap
->Link
);
1180 Wrap
->IpInstance
= IpInstance
;
1181 Wrap
->Packet
= Packet
;
1182 RxData
= &Wrap
->RxData
;
1184 ZeroMem (RxData
, sizeof (EFI_IP4_RECEIVE_DATA
));
1186 Status
= gBS
->CreateEvent (
1191 &RxData
->RecycleSignal
1194 if (EFI_ERROR (Status
)) {
1199 ASSERT (Packet
->Ip
.Ip4
!= NULL
);
1201 ASSERT (IpInstance
!= NULL
);
1202 RawData
= IpInstance
->ConfigData
.RawData
;
1205 // The application expects a network byte order header.
1208 RxData
->HeaderLength
= (Packet
->Ip
.Ip4
->HeadLen
<< 2);
1209 RxData
->Header
= (EFI_IP4_HEADER
*) Ip4NtohHead (Packet
->Ip
.Ip4
);
1210 RxData
->OptionsLength
= RxData
->HeaderLength
- IP4_MIN_HEADLEN
;
1211 RxData
->Options
= NULL
;
1213 if (RxData
->OptionsLength
!= 0) {
1214 RxData
->Options
= (VOID
*) (RxData
->Header
+ 1);
1218 RxData
->DataLength
= Packet
->TotalSize
;
1221 // Build the fragment table to be delivered up.
1223 RxData
->FragmentCount
= Packet
->BlockOpNum
;
1224 NetbufBuildExt (Packet
, (NET_FRAGMENT
*) RxData
->FragmentTable
, &RxData
->FragmentCount
);
1231 Deliver the received packets to upper layer if there are both received
1232 requests and enqueued packets. If the enqueued packet is shared, it will
1233 duplicate it to a non-shared packet, release the shared packet, then
1234 deliver the non-shared packet up.
1236 @param[in] IpInstance The IP child to deliver the packet up.
1238 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources to deliver the
1240 @retval EFI_SUCCESS All the enqueued packets that can be delivered
1245 Ip4InstanceDeliverPacket (
1246 IN IP4_PROTOCOL
*IpInstance
1249 EFI_IP4_COMPLETION_TOKEN
*Token
;
1250 IP4_RXDATA_WRAP
*Wrap
;
1257 // Deliver a packet if there are both a packet and a receive token.
1259 while (!IsListEmpty (&IpInstance
->Received
) &&
1260 !NetMapIsEmpty (&IpInstance
->RxTokens
)) {
1262 Packet
= NET_LIST_HEAD (&IpInstance
->Received
, NET_BUF
, List
);
1264 if (!NET_BUF_SHARED (Packet
)) {
1266 // If this is the only instance that wants the packet, wrap it up.
1268 Wrap
= Ip4WrapRxData (IpInstance
, Packet
);
1271 return EFI_OUT_OF_RESOURCES
;
1274 RemoveEntryList (&Packet
->List
);
1278 // Create a duplicated packet if this packet is shared
1280 if (IpInstance
->ConfigData
.RawData
) {
1283 HeadLen
= IP4_MAX_HEADLEN
;
1286 Dup
= NetbufDuplicate (Packet
, NULL
, HeadLen
);
1289 return EFI_OUT_OF_RESOURCES
;
1292 if (!IpInstance
->ConfigData
.RawData
) {
1294 // Copy the IP head over. The packet to deliver up is
1295 // headless. Trim the head off after copy. The IP head
1296 // may be not continuous before the data.
1298 Head
= NetbufAllocSpace (Dup
, IP4_MAX_HEADLEN
, NET_BUF_HEAD
);
1299 ASSERT (Head
!= NULL
);
1301 Dup
->Ip
.Ip4
= (IP4_HEAD
*) Head
;
1303 CopyMem (Head
, Packet
->Ip
.Ip4
, Packet
->Ip
.Ip4
->HeadLen
<< 2);
1304 NetbufTrim (Dup
, IP4_MAX_HEADLEN
, TRUE
);
1307 Wrap
= Ip4WrapRxData (IpInstance
, Dup
);
1311 return EFI_OUT_OF_RESOURCES
;
1314 RemoveEntryList (&Packet
->List
);
1315 NetbufFree (Packet
);
1321 // Insert it into the delivered packet, then get a user's
1322 // receive token, pass the wrapped packet up.
1324 EfiAcquireLockOrFail (&IpInstance
->RecycleLock
);
1325 InsertHeadList (&IpInstance
->Delivered
, &Wrap
->Link
);
1326 EfiReleaseLock (&IpInstance
->RecycleLock
);
1328 Token
= NetMapRemoveHead (&IpInstance
->RxTokens
, NULL
);
1329 Token
->Status
= IP4_GET_CLIP_INFO (Packet
)->Status
;
1330 Token
->Packet
.RxData
= &Wrap
->RxData
;
1332 gBS
->SignalEvent (Token
->Event
);
1340 Enqueue a received packet to all the IP children that share
1343 @param[in] IpSb The IP4 service instance that receive the packet.
1344 @param[in] Head The header of the received packet.
1345 @param[in] Packet The data of the received packet.
1346 @param[in] Option Point to the IP4 packet header options.
1347 @param[in] OptionLen Length of the IP4 packet header options.
1348 @param[in] IpIf The interface to enqueue the packet to.
1350 @return The number of the IP4 children that accepts the packet
1354 Ip4InterfaceEnquePacket (
1355 IN IP4_SERVICE
*IpSb
,
1359 IN UINT32 OptionLen
,
1360 IN IP4_INTERFACE
*IpIf
1363 IP4_PROTOCOL
*IpInstance
;
1364 IP4_CLIP_INFO
*Info
;
1371 // First, check that the packet is acceptable to this interface
1372 // and find the local cast type for the interface. A packet sent
1373 // to say 192.168.1.1 should NOT be delliever to 10.0.0.1 unless
1374 // promiscuous receiving.
1377 Info
= IP4_GET_CLIP_INFO (Packet
);
1379 if ((Info
->CastType
== IP4_MULTICAST
) || (Info
->CastType
== IP4_LOCAL_BROADCAST
)) {
1381 // If the CastType is multicast, don't need to filter against
1382 // the group address here, Ip4InstanceFrameAcceptable will do
1385 LocalType
= Info
->CastType
;
1389 // Check the destination againist local IP. If the station
1390 // address is 0.0.0.0, it means receiving all the IP destined
1391 // to local non-zero IP. Otherwise, it is necessary to compare
1392 // the destination to the interface's IP address.
1394 if (IpIf
->Ip
== IP4_ALLZERO_ADDRESS
) {
1395 LocalType
= IP4_LOCAL_HOST
;
1398 LocalType
= Ip4GetNetCast (Head
->Dst
, IpIf
);
1400 if ((LocalType
== 0) && IpIf
->PromiscRecv
) {
1401 LocalType
= IP4_PROMISCUOUS
;
1406 if (LocalType
== 0) {
1411 // Iterate through the ip instances on the interface, enqueue
1412 // the packet if filter passed. Save the original cast type,
1413 // and pass the local cast type to the IP children on the
1414 // interface. The global cast type will be restored later.
1416 SavedType
= Info
->CastType
;
1417 Info
->CastType
= LocalType
;
1421 NET_LIST_FOR_EACH (Entry
, &IpIf
->IpInstances
) {
1422 IpInstance
= NET_LIST_USER_STRUCT (Entry
, IP4_PROTOCOL
, AddrLink
);
1423 NET_CHECK_SIGNATURE (IpInstance
, IP4_PROTOCOL_SIGNATURE
);
1426 // In RawData mode, add IPv4 headers and options back to packet.
1428 if ((IpInstance
->ConfigData
.RawData
) && (Option
!= NULL
) && (OptionLen
!= 0)){
1429 Ip4PrependHead (Packet
, Head
, Option
, OptionLen
);
1432 if (Ip4InstanceEnquePacket (IpInstance
, Head
, Packet
) == EFI_SUCCESS
) {
1437 Info
->CastType
= SavedType
;
1443 Deliver the packet for each IP4 child on the interface.
1445 @param[in] IpSb The IP4 service instance that received the packet
1446 @param[in] IpIf The IP4 interface to deliver the packet.
1448 @retval EFI_SUCCESS It always returns EFI_SUCCESS now
1452 Ip4InterfaceDeliverPacket (
1453 IN IP4_SERVICE
*IpSb
,
1454 IN IP4_INTERFACE
*IpIf
1457 IP4_PROTOCOL
*Ip4Instance
;
1460 NET_LIST_FOR_EACH (Entry
, &IpIf
->IpInstances
) {
1461 Ip4Instance
= NET_LIST_USER_STRUCT (Entry
, IP4_PROTOCOL
, AddrLink
);
1462 Ip4InstanceDeliverPacket (Ip4Instance
);
1470 Demultiple the packet. the packet delivery is processed in two
1471 passes. The first pass will enque a shared copy of the packet
1472 to each IP4 child that accepts the packet. The second pass will
1473 deliver a non-shared copy of the packet to each IP4 child that
1474 has pending receive requests. Data is copied if more than one
1475 child wants to consume the packet because each IP child needs
1476 its own copy of the packet to make changes.
1478 @param[in] IpSb The IP4 service instance that received the packet.
1479 @param[in] Head The header of the received packet.
1480 @param[in] Packet The data of the received packet.
1481 @param[in] Option Point to the IP4 packet header options.
1482 @param[in] OptionLen Length of the IP4 packet header options.
1484 @retval EFI_NOT_FOUND No IP child accepts the packet.
1485 @retval EFI_SUCCESS The packet is enqueued or delivered to some IP
1491 IN IP4_SERVICE
*IpSb
,
1499 IP4_INTERFACE
*IpIf
;
1503 // Two pass delivery: first, enque a shared copy of the packet
1504 // to each instance that accept the packet.
1508 NET_LIST_FOR_EACH (Entry
, &IpSb
->Interfaces
) {
1509 IpIf
= NET_LIST_USER_STRUCT (Entry
, IP4_INTERFACE
, Link
);
1511 if (IpIf
->Configured
) {
1512 Enqueued
+= Ip4InterfaceEnquePacket (
1524 // Second: deliver a duplicate of the packet to each instance.
1525 // Release the local reference first, so that the last instance
1526 // getting the packet will not copy the data.
1528 NetbufFree (Packet
);
1530 if (Enqueued
== 0) {
1531 return EFI_NOT_FOUND
;
1534 NET_LIST_FOR_EACH (Entry
, &IpSb
->Interfaces
) {
1535 IpIf
= NET_LIST_USER_STRUCT (Entry
, IP4_INTERFACE
, Link
);
1537 if (IpIf
->Configured
) {
1538 Ip4InterfaceDeliverPacket (IpSb
, IpIf
);
1547 Timeout the fragment and enqueued packets.
1549 @param[in] IpSb The IP4 service instance to timeout
1553 Ip4PacketTimerTicking (
1554 IN IP4_SERVICE
*IpSb
1557 LIST_ENTRY
*InstanceEntry
;
1560 IP4_PROTOCOL
*IpInstance
;
1561 IP4_ASSEMBLE_ENTRY
*Assemble
;
1563 IP4_CLIP_INFO
*Info
;
1567 // First, time out the fragments. The packet's life is counting down
1568 // once the first-arrived fragment was received.
1570 for (Index
= 0; Index
< IP4_ASSEMLE_HASH_SIZE
; Index
++) {
1571 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &IpSb
->Assemble
.Bucket
[Index
]) {
1572 Assemble
= NET_LIST_USER_STRUCT (Entry
, IP4_ASSEMBLE_ENTRY
, Link
);
1574 if ((Assemble
->Life
> 0) && (--Assemble
->Life
== 0)) {
1575 RemoveEntryList (Entry
);
1576 Ip4FreeAssembleEntry (Assemble
);
1581 NET_LIST_FOR_EACH (InstanceEntry
, &IpSb
->Children
) {
1582 IpInstance
= NET_LIST_USER_STRUCT (InstanceEntry
, IP4_PROTOCOL
, Link
);
1585 // Second, time out the assembled packets enqueued on each IP child.
1587 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &IpInstance
->Received
) {
1588 Packet
= NET_LIST_USER_STRUCT (Entry
, NET_BUF
, List
);
1589 Info
= IP4_GET_CLIP_INFO (Packet
);
1591 if ((Info
->Life
> 0) && (--Info
->Life
== 0)) {
1592 RemoveEntryList (Entry
);
1593 NetbufFree (Packet
);
1598 // Third: time out the transmitted packets.
1600 NetMapIterate (&IpInstance
->TxTokens
, Ip4SentPacketTicking
, NULL
);