4 Copyright (c) 2005 - 2014, 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
) {
525 FragmentTable
= NULL
;
526 TxWrap
= (IP4_TXTOKEN_WRAP
*) Context
;
527 FragmentCount
= Packet
->BlockOpNum
;
529 ZeroMem (&ZeroHead
, sizeof (IP4_HEAD
));
531 if (mIpSec
== NULL
) {
532 gBS
->LocateProtocol (&gEfiIpSec2ProtocolGuid
, NULL
, (VOID
**) &mIpSec
);
533 if (mIpSec
== NULL
) {
539 // Check whether the IPsec enable variable is set.
541 if (mIpSec
->DisabledFlag
) {
543 // If IPsec is disabled, restore the original MTU
545 IpSb
->MaxPacketSize
= IpSb
->OldMaxPacketSize
;
549 // If IPsec is enabled, use the MTU which reduce the IPsec header length.
551 IpSb
->MaxPacketSize
= IpSb
->OldMaxPacketSize
- IP4_MAX_IPSEC_HEADLEN
;
555 // Rebuild fragment table from netbuf to ease IPsec process.
557 FragmentTable
= AllocateZeroPool (FragmentCount
* sizeof (NET_FRAGMENT
));
559 if (FragmentTable
== NULL
) {
560 Status
= EFI_OUT_OF_RESOURCES
;
564 Status
= NetbufBuildExt (Packet
, FragmentTable
, &FragmentCount
);
567 // Record the original FragmentTable and count.
569 OriginalFragmentTable
= FragmentTable
;
570 OriginalFragmentCount
= FragmentCount
;
572 if (EFI_ERROR (Status
)) {
573 FreePool (FragmentTable
);
578 // Convert host byte order to network byte order
582 Status
= mIpSec
->ProcessExt (
590 (EFI_IPSEC_FRAGMENT_DATA
**) (&FragmentTable
),
596 // Convert back to host byte order
600 if (EFI_ERROR (Status
)) {
601 FreePool (OriginalFragmentTable
);
605 if (OriginalFragmentTable
== FragmentTable
&& OriginalFragmentCount
== FragmentCount
) {
609 FreePool (FragmentTable
);
613 // Free the FragmentTable which allocated before calling the IPsec.
615 FreePool (OriginalFragmentTable
);
618 if (Direction
== EfiIPsecOutBound
&& TxWrap
!= NULL
) {
620 TxWrap
->IpSecRecycleSignal
= RecycleEvent
;
621 TxWrap
->Packet
= NetbufFromExt (
629 if (TxWrap
->Packet
== NULL
) {
631 // Recover the TxWrap->Packet, if meet a error, and the caller will free
634 TxWrap
->Packet
= *Netbuf
;
635 Status
= EFI_OUT_OF_RESOURCES
;
640 // Free orginal Netbuf.
642 NetIpSecNetbufFree (*Netbuf
);
643 *Netbuf
= TxWrap
->Packet
;
647 IpSecWrap
= AllocateZeroPool (sizeof (IP4_IPSEC_WRAP
));
649 if (IpSecWrap
== NULL
) {
650 Status
= EFI_OUT_OF_RESOURCES
;
651 gBS
->SignalEvent (RecycleEvent
);
655 IpSecWrap
->IpSecRecycleSignal
= RecycleEvent
;
656 IpSecWrap
->Packet
= Packet
;
657 Packet
= NetbufFromExt (
666 if (Packet
== NULL
) {
667 Packet
= IpSecWrap
->Packet
;
668 gBS
->SignalEvent (RecycleEvent
);
669 FreePool (IpSecWrap
);
670 Status
= EFI_OUT_OF_RESOURCES
;
674 if (Direction
== EfiIPsecInBound
&& 0 != CompareMem (*Head
, &ZeroHead
, sizeof (IP4_HEAD
))) {
675 Ip4PrependHead (Packet
, *Head
, *Options
, *OptionsLen
);
676 Ip4NtohHead (Packet
->Ip
.Ip4
);
677 NetbufTrim (Packet
, ((*Head
)->HeadLen
<< 2), TRUE
);
680 IP4_GET_CLIP_INFO (Packet
),
681 IP4_GET_CLIP_INFO (IpSecWrap
->Packet
),
682 sizeof (IP4_CLIP_INFO
)
693 Pre-process the IPv4 packet. First validates the IPv4 packet, and
694 then reassembles packet if it is necessary.
696 @param[in] IpSb Pointer to IP4_SERVICE.
697 @param[in, out] Packet Pointer to the Packet to be processed.
698 @param[in] Head Pointer to the IP4_HEAD.
699 @param[in] Option Pointer to a buffer which contains the IPv4 option.
700 @param[in] OptionLen The length of Option in bytes.
701 @param[in] Flag The link layer flag for the packet received, such
704 @retval EFI_SEUCCESS The recieved packet is in well form.
705 @retval EFI_INVAILD_PARAMETER The recieved packet is malformed.
709 Ip4PreProcessPacket (
710 IN IP4_SERVICE
*IpSb
,
711 IN OUT NET_BUF
**Packet
,
724 // Check if the IP4 header is correctly formatted.
726 if ((*Packet
)->TotalSize
< IP4_MIN_HEADLEN
) {
727 return EFI_INVALID_PARAMETER
;
730 HeadLen
= (Head
->HeadLen
<< 2);
731 TotalLen
= NTOHS (Head
->TotalLen
);
734 // Mnp may deliver frame trailer sequence up, trim it off.
736 if (TotalLen
< (*Packet
)->TotalSize
) {
737 NetbufTrim (*Packet
, (*Packet
)->TotalSize
- TotalLen
, FALSE
);
740 if ((Head
->Ver
!= 4) || (HeadLen
< IP4_MIN_HEADLEN
) ||
741 (TotalLen
< HeadLen
) || (TotalLen
!= (*Packet
)->TotalSize
)) {
742 return EFI_INVALID_PARAMETER
;
746 // Some OS may send IP packets without checksum.
748 Checksum
= (UINT16
) (~NetblockChecksum ((UINT8
*) Head
, HeadLen
));
750 if ((Head
->Checksum
!= 0) && (Checksum
!= 0)) {
751 return EFI_INVALID_PARAMETER
;
755 // Convert the IP header to host byte order, then get the per packet info.
757 (*Packet
)->Ip
.Ip4
= Ip4NtohHead (Head
);
759 Info
= IP4_GET_CLIP_INFO (*Packet
);
760 Info
->LinkFlag
= Flag
;
761 Info
->CastType
= Ip4GetHostCast (IpSb
, Head
->Dst
, Head
->Src
);
762 Info
->Start
= (Head
->Fragment
& IP4_HEAD_OFFSET_MASK
) << 3;
763 Info
->Length
= Head
->TotalLen
- HeadLen
;
764 Info
->End
= Info
->Start
+ Info
->Length
;
765 Info
->Status
= EFI_SUCCESS
;
768 // The packet is destinated to us if the CastType is non-zero.
770 if ((Info
->CastType
== 0) || (Info
->End
> IP4_MAX_PACKET_SIZE
)) {
771 return EFI_INVALID_PARAMETER
;
775 // Validate the options. Don't call the Ip4OptionIsValid if
776 // there is no option to save some CPU process.
779 if ((OptionLen
> 0) && !Ip4OptionIsValid (Option
, OptionLen
, TRUE
)) {
780 return EFI_INVALID_PARAMETER
;
784 // Trim the head off, after this point, the packet is headless,
785 // and Packet->TotalLen == Info->Length.
787 NetbufTrim (*Packet
, HeadLen
, TRUE
);
790 // Reassemble the packet if this is a fragment. The packet is a
791 // fragment if its head has MF (more fragment) set, or it starts
794 if (((Head
->Fragment
& IP4_HEAD_MF_MASK
) != 0) || (Info
->Start
!= 0)) {
796 // Drop the fragment if DF is set but it is fragmented. Gateway
797 // need to send a type 4 destination unreache ICMP message here.
799 if ((Head
->Fragment
& IP4_HEAD_DF_MASK
) != 0) {
800 return EFI_INVALID_PARAMETER
;
804 // The length of all but the last fragments is in the unit of 8 bytes.
806 if (((Head
->Fragment
& IP4_HEAD_MF_MASK
) != 0) && (Info
->Length
% 8 != 0)) {
807 return EFI_INVALID_PARAMETER
;
810 *Packet
= Ip4Reassemble (&IpSb
->Assemble
, *Packet
);
813 // Packet assembly isn't complete, start receive more packet.
815 if (*Packet
== NULL
) {
816 return EFI_INVALID_PARAMETER
;
824 The IP4 input routine. It is called by the IP4_INTERFACE when a
825 IP4 fragment is received from MNP.
827 @param[in] Ip4Instance The IP4 child that request the receive, most like
829 @param[in] Packet The IP4 packet received.
830 @param[in] IoStatus The return status of receive request.
831 @param[in] Flag The link layer flag for the packet received, such
833 @param[in] Context The IP4 service instance that own the MNP.
838 IN IP4_PROTOCOL
*Ip4Instance
,
840 IN EFI_STATUS IoStatus
,
852 IpSb
= (IP4_SERVICE
*) Context
;
855 if (EFI_ERROR (IoStatus
) || (IpSb
->State
== IP4_SERVICE_DESTROY
)) {
859 Head
= (IP4_HEAD
*) NetbufGetByte (Packet
, 0, NULL
);
860 ASSERT (Head
!= NULL
);
861 OptionLen
= (Head
->HeadLen
<< 2) - IP4_MIN_HEADLEN
;
863 Option
= (UINT8
*) (Head
+ 1);
867 // Validate packet format and reassemble packet if it is necessary.
869 Status
= Ip4PreProcessPacket (
878 if (EFI_ERROR (Status
)) {
883 // After trim off, the packet is a esp/ah/udp/tcp/icmp6 net buffer,
884 // and no need consider any other ahead ext headers.
886 Status
= Ip4IpSecProcessPacket (
896 if (EFI_ERROR (Status
)) {
901 // If the packet is protected by tunnel mode, parse the inner Ip Packet.
903 ZeroMem (&ZeroHead
, sizeof (IP4_HEAD
));
904 if (0 == CompareMem (Head
, &ZeroHead
, sizeof (IP4_HEAD
))) {
905 // Packet may have been changed. Head, HeadLen, TotalLen, and
906 // info must be reloaded bofore use. The ownership of the packet
907 // is transfered to the packet process logic.
909 Head
= (IP4_HEAD
*) NetbufGetByte (Packet
, 0, NULL
);
910 ASSERT (Head
!= NULL
);
911 Status
= Ip4PreProcessPacket (
919 if (EFI_ERROR (Status
)) {
924 ASSERT (Packet
!= NULL
);
925 Head
= Packet
->Ip
.Ip4
;
926 IP4_GET_CLIP_INFO (Packet
)->Status
= EFI_SUCCESS
;
928 switch (Head
->Protocol
) {
929 case EFI_IP_PROTO_ICMP
:
930 Ip4IcmpHandle (IpSb
, Head
, Packet
);
934 Ip4IgmpHandle (IpSb
, Head
, Packet
);
938 Ip4Demultiplex (IpSb
, Head
, Packet
, Option
, OptionLen
);
944 // Dispatch the DPCs queued by the NotifyFunction of the rx token's events
945 // which are signaled with received data.
950 Ip4ReceiveFrame (IpSb
->DefaultInterface
, NULL
, Ip4AccpetFrame
, IpSb
);
953 if (Packet
!= NULL
) {
962 Check whether this IP child accepts the packet.
964 @param[in] IpInstance The IP child to check
965 @param[in] Head The IP header of the packet
966 @param[in] Packet The data of the packet
968 @retval TRUE If the child wants to receive the packet.
969 @retval FALSE Otherwise.
973 Ip4InstanceFrameAcceptable (
974 IN IP4_PROTOCOL
*IpInstance
,
979 IP4_ICMP_ERROR_HEAD Icmp
;
980 EFI_IP4_CONFIG_DATA
*Config
;
985 Config
= &IpInstance
->ConfigData
;
988 // Dirty trick for the Tiano UEFI network stack implmentation. If
989 // ReceiveTimeout == -1, the receive of the packet for this instance
990 // is disabled. The UEFI spec don't have such capability. We add
991 // this to improve the performance because IP will make a copy of
992 // the received packet for each accepting instance. Some IP instances
993 // used by UDP/TCP only send packets, they don't wants to receive.
995 if (Config
->ReceiveTimeout
== (UINT32
)(-1)) {
999 if (Config
->AcceptPromiscuous
) {
1004 // Use protocol from the IP header embedded in the ICMP error
1005 // message to filter, instead of ICMP itself. ICMP handle will
1006 // call Ip4Demultiplex to deliver ICMP errors.
1008 Proto
= Head
->Protocol
;
1010 if ((Proto
== EFI_IP_PROTO_ICMP
) && (!Config
->AcceptAnyProtocol
) && (Proto
!= Config
->DefaultProtocol
)) {
1011 NetbufCopy (Packet
, 0, sizeof (Icmp
.Head
), (UINT8
*) &Icmp
.Head
);
1013 if (mIcmpClass
[Icmp
.Head
.Type
].IcmpClass
== ICMP_ERROR_MESSAGE
) {
1014 if (!Config
->AcceptIcmpErrors
) {
1018 NetbufCopy (Packet
, 0, sizeof (Icmp
), (UINT8
*) &Icmp
);
1019 Proto
= Icmp
.IpHead
.Protocol
;
1024 // Match the protocol
1026 if (!Config
->AcceptAnyProtocol
&& (Proto
!= Config
->DefaultProtocol
)) {
1031 // Check for broadcast, the caller has computed the packet's
1032 // cast type for this child's interface.
1034 Info
= IP4_GET_CLIP_INFO (Packet
);
1036 if (IP4_IS_BROADCAST (Info
->CastType
)) {
1037 return Config
->AcceptBroadcast
;
1041 // If it is a multicast packet, check whether we are in the group.
1043 if (Info
->CastType
== IP4_MULTICAST
) {
1045 // Receive the multicast if the instance wants to receive all packets.
1047 if (!IpInstance
->ConfigData
.UseDefaultAddress
&& (IpInstance
->Interface
->Ip
== 0)) {
1051 for (Index
= 0; Index
< IpInstance
->GroupCount
; Index
++) {
1052 if (IpInstance
->Groups
[Index
] == HTONL (Head
->Dst
)) {
1057 return (BOOLEAN
)(Index
< IpInstance
->GroupCount
);
1065 Enqueue a shared copy of the packet to the IP4 child if the
1066 packet is acceptable to it. Here the data of the packet is
1067 shared, but the net buffer isn't.
1069 @param[in] IpInstance The IP4 child to enqueue the packet to
1070 @param[in] Head The IP header of the received packet
1071 @param[in] Packet The data of the received packet
1073 @retval EFI_NOT_STARTED The IP child hasn't been configured.
1074 @retval EFI_INVALID_PARAMETER The child doesn't want to receive the packet
1075 @retval EFI_OUT_OF_RESOURCES Failed to allocate some resource
1076 @retval EFI_SUCCESS A shared copy the packet is enqueued to the child.
1080 Ip4InstanceEnquePacket (
1081 IN IP4_PROTOCOL
*IpInstance
,
1086 IP4_CLIP_INFO
*Info
;
1090 // Check whether the packet is acceptable to this instance.
1092 if (IpInstance
->State
!= IP4_STATE_CONFIGED
) {
1093 return EFI_NOT_STARTED
;
1096 if (!Ip4InstanceFrameAcceptable (IpInstance
, Head
, Packet
)) {
1097 return EFI_INVALID_PARAMETER
;
1101 // Enque a shared copy of the packet.
1103 Clone
= NetbufClone (Packet
);
1105 if (Clone
== NULL
) {
1106 return EFI_OUT_OF_RESOURCES
;
1110 // Set the receive time out for the assembled packet. If it expires,
1111 // packet will be removed from the queue.
1113 Info
= IP4_GET_CLIP_INFO (Clone
);
1114 Info
->Life
= IP4_US_TO_SEC (IpInstance
->ConfigData
.ReceiveTimeout
);
1116 InsertTailList (&IpInstance
->Received
, &Clone
->List
);
1122 The signal handle of IP4's recycle event. It is called back
1123 when the upper layer release the packet.
1125 @param Event The IP4's recycle event.
1126 @param Context The context of the handle, which is a
1132 Ip4OnRecyclePacket (
1137 IP4_RXDATA_WRAP
*Wrap
;
1139 Wrap
= (IP4_RXDATA_WRAP
*) Context
;
1141 EfiAcquireLockOrFail (&Wrap
->IpInstance
->RecycleLock
);
1142 RemoveEntryList (&Wrap
->Link
);
1143 EfiReleaseLock (&Wrap
->IpInstance
->RecycleLock
);
1145 ASSERT (!NET_BUF_SHARED (Wrap
->Packet
));
1146 NetbufFree (Wrap
->Packet
);
1148 gBS
->CloseEvent (Wrap
->RxData
.RecycleSignal
);
1154 Wrap the received packet to a IP4_RXDATA_WRAP, which will be
1155 delivered to the upper layer. Each IP4 child that accepts the
1156 packet will get a not-shared copy of the packet which is wrapped
1157 in the IP4_RXDATA_WRAP. The IP4_RXDATA_WRAP->RxData is passed
1158 to the upper layer. Upper layer will signal the recycle event in
1159 it when it is done with the packet.
1161 @param[in] IpInstance The IP4 child to receive the packet.
1162 @param[in] Packet The packet to deliver up.
1164 @retval Wrap if warp the packet succeed.
1165 @retval NULL failed to wrap the packet .
1170 IN IP4_PROTOCOL
*IpInstance
,
1174 IP4_RXDATA_WRAP
*Wrap
;
1175 EFI_IP4_RECEIVE_DATA
*RxData
;
1179 Wrap
= AllocatePool (IP4_RXDATA_WRAP_SIZE (Packet
->BlockOpNum
));
1185 InitializeListHead (&Wrap
->Link
);
1187 Wrap
->IpInstance
= IpInstance
;
1188 Wrap
->Packet
= Packet
;
1189 RxData
= &Wrap
->RxData
;
1191 ZeroMem (RxData
, sizeof (EFI_IP4_RECEIVE_DATA
));
1193 Status
= gBS
->CreateEvent (
1198 &RxData
->RecycleSignal
1201 if (EFI_ERROR (Status
)) {
1206 ASSERT (Packet
->Ip
.Ip4
!= NULL
);
1208 ASSERT (IpInstance
!= NULL
);
1209 RawData
= IpInstance
->ConfigData
.RawData
;
1212 // The application expects a network byte order header.
1215 RxData
->HeaderLength
= (Packet
->Ip
.Ip4
->HeadLen
<< 2);
1216 RxData
->Header
= (EFI_IP4_HEADER
*) Ip4NtohHead (Packet
->Ip
.Ip4
);
1217 RxData
->OptionsLength
= RxData
->HeaderLength
- IP4_MIN_HEADLEN
;
1218 RxData
->Options
= NULL
;
1220 if (RxData
->OptionsLength
!= 0) {
1221 RxData
->Options
= (VOID
*) (RxData
->Header
+ 1);
1225 RxData
->DataLength
= Packet
->TotalSize
;
1228 // Build the fragment table to be delivered up.
1230 RxData
->FragmentCount
= Packet
->BlockOpNum
;
1231 NetbufBuildExt (Packet
, (NET_FRAGMENT
*) RxData
->FragmentTable
, &RxData
->FragmentCount
);
1238 Deliver the received packets to upper layer if there are both received
1239 requests and enqueued packets. If the enqueued packet is shared, it will
1240 duplicate it to a non-shared packet, release the shared packet, then
1241 deliver the non-shared packet up.
1243 @param[in] IpInstance The IP child to deliver the packet up.
1245 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources to deliver the
1247 @retval EFI_SUCCESS All the enqueued packets that can be delivered
1252 Ip4InstanceDeliverPacket (
1253 IN IP4_PROTOCOL
*IpInstance
1256 EFI_IP4_COMPLETION_TOKEN
*Token
;
1257 IP4_RXDATA_WRAP
*Wrap
;
1264 // Deliver a packet if there are both a packet and a receive token.
1266 while (!IsListEmpty (&IpInstance
->Received
) &&
1267 !NetMapIsEmpty (&IpInstance
->RxTokens
)) {
1269 Packet
= NET_LIST_HEAD (&IpInstance
->Received
, NET_BUF
, List
);
1271 if (!NET_BUF_SHARED (Packet
)) {
1273 // If this is the only instance that wants the packet, wrap it up.
1275 Wrap
= Ip4WrapRxData (IpInstance
, Packet
);
1278 return EFI_OUT_OF_RESOURCES
;
1281 RemoveEntryList (&Packet
->List
);
1285 // Create a duplicated packet if this packet is shared
1287 if (IpInstance
->ConfigData
.RawData
) {
1290 HeadLen
= IP4_MAX_HEADLEN
;
1293 Dup
= NetbufDuplicate (Packet
, NULL
, HeadLen
);
1296 return EFI_OUT_OF_RESOURCES
;
1299 if (!IpInstance
->ConfigData
.RawData
) {
1301 // Copy the IP head over. The packet to deliver up is
1302 // headless. Trim the head off after copy. The IP head
1303 // may be not continuous before the data.
1305 Head
= NetbufAllocSpace (Dup
, IP4_MAX_HEADLEN
, NET_BUF_HEAD
);
1306 ASSERT (Head
!= NULL
);
1308 Dup
->Ip
.Ip4
= (IP4_HEAD
*) Head
;
1310 CopyMem (Head
, Packet
->Ip
.Ip4
, Packet
->Ip
.Ip4
->HeadLen
<< 2);
1311 NetbufTrim (Dup
, IP4_MAX_HEADLEN
, TRUE
);
1314 Wrap
= Ip4WrapRxData (IpInstance
, Dup
);
1318 return EFI_OUT_OF_RESOURCES
;
1321 RemoveEntryList (&Packet
->List
);
1322 NetbufFree (Packet
);
1328 // Insert it into the delivered packet, then get a user's
1329 // receive token, pass the wrapped packet up.
1331 EfiAcquireLockOrFail (&IpInstance
->RecycleLock
);
1332 InsertHeadList (&IpInstance
->Delivered
, &Wrap
->Link
);
1333 EfiReleaseLock (&IpInstance
->RecycleLock
);
1335 Token
= NetMapRemoveHead (&IpInstance
->RxTokens
, NULL
);
1336 Token
->Status
= IP4_GET_CLIP_INFO (Packet
)->Status
;
1337 Token
->Packet
.RxData
= &Wrap
->RxData
;
1339 gBS
->SignalEvent (Token
->Event
);
1347 Enqueue a received packet to all the IP children that share
1350 @param[in] IpSb The IP4 service instance that receive the packet.
1351 @param[in] Head The header of the received packet.
1352 @param[in] Packet The data of the received packet.
1353 @param[in] Option Point to the IP4 packet header options.
1354 @param[in] OptionLen Length of the IP4 packet header options.
1355 @param[in] IpIf The interface to enqueue the packet to.
1357 @return The number of the IP4 children that accepts the packet
1361 Ip4InterfaceEnquePacket (
1362 IN IP4_SERVICE
*IpSb
,
1366 IN UINT32 OptionLen
,
1367 IN IP4_INTERFACE
*IpIf
1370 IP4_PROTOCOL
*IpInstance
;
1371 IP4_CLIP_INFO
*Info
;
1378 // First, check that the packet is acceptable to this interface
1379 // and find the local cast type for the interface. A packet sent
1380 // to say 192.168.1.1 should NOT be delliever to 10.0.0.1 unless
1381 // promiscuous receiving.
1384 Info
= IP4_GET_CLIP_INFO (Packet
);
1386 if ((Info
->CastType
== IP4_MULTICAST
) || (Info
->CastType
== IP4_LOCAL_BROADCAST
)) {
1388 // If the CastType is multicast, don't need to filter against
1389 // the group address here, Ip4InstanceFrameAcceptable will do
1392 LocalType
= Info
->CastType
;
1396 // Check the destination againist local IP. If the station
1397 // address is 0.0.0.0, it means receiving all the IP destined
1398 // to local non-zero IP. Otherwise, it is necessary to compare
1399 // the destination to the interface's IP address.
1401 if (IpIf
->Ip
== IP4_ALLZERO_ADDRESS
) {
1402 LocalType
= IP4_LOCAL_HOST
;
1405 LocalType
= Ip4GetNetCast (Head
->Dst
, IpIf
);
1407 if ((LocalType
== 0) && IpIf
->PromiscRecv
) {
1408 LocalType
= IP4_PROMISCUOUS
;
1413 if (LocalType
== 0) {
1418 // Iterate through the ip instances on the interface, enqueue
1419 // the packet if filter passed. Save the original cast type,
1420 // and pass the local cast type to the IP children on the
1421 // interface. The global cast type will be restored later.
1423 SavedType
= Info
->CastType
;
1424 Info
->CastType
= LocalType
;
1428 NET_LIST_FOR_EACH (Entry
, &IpIf
->IpInstances
) {
1429 IpInstance
= NET_LIST_USER_STRUCT (Entry
, IP4_PROTOCOL
, AddrLink
);
1430 NET_CHECK_SIGNATURE (IpInstance
, IP4_PROTOCOL_SIGNATURE
);
1433 // In RawData mode, add IPv4 headers and options back to packet.
1435 if ((IpInstance
->ConfigData
.RawData
) && (Option
!= NULL
) && (OptionLen
!= 0)){
1436 Ip4PrependHead (Packet
, Head
, Option
, OptionLen
);
1439 if (Ip4InstanceEnquePacket (IpInstance
, Head
, Packet
) == EFI_SUCCESS
) {
1444 Info
->CastType
= SavedType
;
1450 Deliver the packet for each IP4 child on the interface.
1452 @param[in] IpSb The IP4 service instance that received the packet
1453 @param[in] IpIf The IP4 interface to deliver the packet.
1455 @retval EFI_SUCCESS It always returns EFI_SUCCESS now
1459 Ip4InterfaceDeliverPacket (
1460 IN IP4_SERVICE
*IpSb
,
1461 IN IP4_INTERFACE
*IpIf
1464 IP4_PROTOCOL
*Ip4Instance
;
1467 NET_LIST_FOR_EACH (Entry
, &IpIf
->IpInstances
) {
1468 Ip4Instance
= NET_LIST_USER_STRUCT (Entry
, IP4_PROTOCOL
, AddrLink
);
1469 Ip4InstanceDeliverPacket (Ip4Instance
);
1477 Demultiple the packet. the packet delivery is processed in two
1478 passes. The first pass will enque a shared copy of the packet
1479 to each IP4 child that accepts the packet. The second pass will
1480 deliver a non-shared copy of the packet to each IP4 child that
1481 has pending receive requests. Data is copied if more than one
1482 child wants to consume the packet because each IP child needs
1483 its own copy of the packet to make changes.
1485 @param[in] IpSb The IP4 service instance that received the packet.
1486 @param[in] Head The header of the received packet.
1487 @param[in] Packet The data of the received packet.
1488 @param[in] Option Point to the IP4 packet header options.
1489 @param[in] OptionLen Length of the IP4 packet header options.
1491 @retval EFI_NOT_FOUND No IP child accepts the packet.
1492 @retval EFI_SUCCESS The packet is enqueued or delivered to some IP
1498 IN IP4_SERVICE
*IpSb
,
1506 IP4_INTERFACE
*IpIf
;
1510 // Two pass delivery: first, enque a shared copy of the packet
1511 // to each instance that accept the packet.
1515 NET_LIST_FOR_EACH (Entry
, &IpSb
->Interfaces
) {
1516 IpIf
= NET_LIST_USER_STRUCT (Entry
, IP4_INTERFACE
, Link
);
1518 if (IpIf
->Configured
) {
1519 Enqueued
+= Ip4InterfaceEnquePacket (
1531 // Second: deliver a duplicate of the packet to each instance.
1532 // Release the local reference first, so that the last instance
1533 // getting the packet will not copy the data.
1535 NetbufFree (Packet
);
1537 if (Enqueued
== 0) {
1538 return EFI_NOT_FOUND
;
1541 NET_LIST_FOR_EACH (Entry
, &IpSb
->Interfaces
) {
1542 IpIf
= NET_LIST_USER_STRUCT (Entry
, IP4_INTERFACE
, Link
);
1544 if (IpIf
->Configured
) {
1545 Ip4InterfaceDeliverPacket (IpSb
, IpIf
);
1554 Timeout the fragment and enqueued packets.
1556 @param[in] IpSb The IP4 service instance to timeout
1560 Ip4PacketTimerTicking (
1561 IN IP4_SERVICE
*IpSb
1564 LIST_ENTRY
*InstanceEntry
;
1567 IP4_PROTOCOL
*IpInstance
;
1568 IP4_ASSEMBLE_ENTRY
*Assemble
;
1570 IP4_CLIP_INFO
*Info
;
1574 // First, time out the fragments. The packet's life is counting down
1575 // once the first-arrived fragment was received.
1577 for (Index
= 0; Index
< IP4_ASSEMLE_HASH_SIZE
; Index
++) {
1578 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &IpSb
->Assemble
.Bucket
[Index
]) {
1579 Assemble
= NET_LIST_USER_STRUCT (Entry
, IP4_ASSEMBLE_ENTRY
, Link
);
1581 if ((Assemble
->Life
> 0) && (--Assemble
->Life
== 0)) {
1582 RemoveEntryList (Entry
);
1583 Ip4FreeAssembleEntry (Assemble
);
1588 NET_LIST_FOR_EACH (InstanceEntry
, &IpSb
->Children
) {
1589 IpInstance
= NET_LIST_USER_STRUCT (InstanceEntry
, IP4_PROTOCOL
, Link
);
1592 // Second, time out the assembled packets enqueued on each IP child.
1594 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &IpInstance
->Received
) {
1595 Packet
= NET_LIST_USER_STRUCT (Entry
, NET_BUF
, List
);
1596 Info
= IP4_GET_CLIP_INFO (Packet
);
1598 if ((Info
->Life
> 0) && (--Info
->Life
== 0)) {
1599 RemoveEntryList (Entry
);
1600 NetbufFree (Packet
);
1605 // Third: time out the transmitted packets.
1607 NetMapIterate (&IpInstance
->TxTokens
, Ip4SentPacketTicking
, NULL
);