4 Copyright (c) 2005 - 2010, Intel Corporation.<BR>
5 All rights reserved. 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
195 Ip4FreeAssembleEntry ((IP4_ASSEMBLE_ENTRY
*) Arg
);
200 Reassemble the IP fragments. If all the fragments of the packet
201 have been received, it will wrap the packet in a net buffer then
202 return it to caller. If the packet can't be assembled, NULL is
205 @param Table The assemble table used. New assemble entry will be created
206 if the Packet is from a new chain of fragments.
207 @param Packet The fragment to assemble. It might be freed if the fragment
208 can't be re-assembled.
210 @return NULL if the packet can't be reassemble. The point to just assembled
211 packet if all the fragments of the packet have arrived.
216 IN OUT IP4_ASSEMBLE_TABLE
*Table
,
217 IN OUT NET_BUF
*Packet
223 IP4_ASSEMBLE_ENTRY
*Assemble
;
231 IpHead
= Packet
->Ip
.Ip4
;
232 This
= IP4_GET_CLIP_INFO (Packet
);
234 ASSERT (IpHead
!= NULL
);
237 // First: find the related assemble entry
240 Index
= IP4_ASSEMBLE_HASH (IpHead
->Dst
, IpHead
->Src
, IpHead
->Id
, IpHead
->Protocol
);
242 NET_LIST_FOR_EACH (Cur
, &Table
->Bucket
[Index
]) {
243 Assemble
= NET_LIST_USER_STRUCT (Cur
, IP4_ASSEMBLE_ENTRY
, Link
);
245 if ((Assemble
->Dst
== IpHead
->Dst
) && (Assemble
->Src
== IpHead
->Src
) &&
246 (Assemble
->Id
== IpHead
->Id
) && (Assemble
->Protocol
== IpHead
->Protocol
)) {
252 // Create a new assemble entry if no assemble entry is related to this packet
254 if (Cur
== &Table
->Bucket
[Index
]) {
255 Assemble
= Ip4CreateAssembleEntry (
262 if (Assemble
== NULL
) {
266 InsertHeadList (&Table
->Bucket
[Index
], &Assemble
->Link
);
269 // Assemble shouldn't be NULL here
271 ASSERT (Assemble
!= NULL
);
274 // Find the point to insert the packet: before the first
275 // fragment with THIS.Start < CUR.Start. the previous one
276 // has PREV.Start <= THIS.Start < CUR.Start.
278 Head
= &Assemble
->Fragments
;
280 NET_LIST_FOR_EACH (Cur
, Head
) {
281 Fragment
= NET_LIST_USER_STRUCT (Cur
, NET_BUF
, List
);
283 if (This
->Start
< IP4_GET_CLIP_INFO (Fragment
)->Start
) {
289 // Check whether the current fragment overlaps with the previous one.
290 // It holds that: PREV.Start <= THIS.Start < THIS.End. Only need to
291 // check whether THIS.Start < PREV.End for overlap. If two fragments
292 // overlaps, trim the overlapped part off THIS fragment.
294 if ((Prev
= Cur
->ForwardLink
) != Head
) {
295 Fragment
= NET_LIST_USER_STRUCT (Prev
, NET_BUF
, List
);
296 Node
= IP4_GET_CLIP_INFO (Fragment
);
298 if (This
->Start
< Node
->End
) {
299 if (This
->End
<= Node
->End
) {
304 Ip4TrimPacket (Packet
, Node
->End
, This
->End
);
309 // Insert the fragment into the packet. The fragment may be removed
310 // from the list by the following checks.
312 NetListInsertBefore (Cur
, &Packet
->List
);
315 // Check the packets after the insert point. It holds that:
316 // THIS.Start <= NODE.Start < NODE.End. The equality holds
317 // if PREV and NEXT are continuous. THIS fragment may fill
318 // several holes. Remove the completely overlapped fragments
320 while (Cur
!= Head
) {
321 Fragment
= NET_LIST_USER_STRUCT (Cur
, NET_BUF
, List
);
322 Node
= IP4_GET_CLIP_INFO (Fragment
);
325 // Remove fragments completely overlapped by this fragment
327 if (Node
->End
<= This
->End
) {
328 Cur
= Cur
->ForwardLink
;
330 RemoveEntryList (&Fragment
->List
);
331 Assemble
->CurLen
-= Node
->Length
;
333 NetbufFree (Fragment
);
338 // The conditions are: THIS.Start <= NODE.Start, and THIS.End <
339 // NODE.End. Two fragments overlaps if NODE.Start < THIS.End.
340 // If two fragments start at the same offset, remove THIS fragment
341 // because ((THIS.Start == NODE.Start) && (THIS.End < NODE.End)).
343 if (Node
->Start
< This
->End
) {
344 if (This
->Start
== Node
->Start
) {
345 RemoveEntryList (&Packet
->List
);
349 Ip4TrimPacket (Packet
, This
->Start
, Node
->Start
);
356 // Update the assemble info: increase the current length. If it is
357 // the frist fragment, update the packet's IP head and per packet
358 // info. If it is the last fragment, update the total length.
360 Assemble
->CurLen
+= This
->Length
;
362 if (This
->Start
== 0) {
364 // Once the first fragment is enqueued, it can't be removed
365 // from the fragment list. So, Assemble->Head always point
366 // to valid memory area.
368 ASSERT (Assemble
->Head
== NULL
);
370 Assemble
->Head
= IpHead
;
371 Assemble
->Info
= IP4_GET_CLIP_INFO (Packet
);
375 // Don't update the length more than once.
377 if (IP4_LAST_FRAGMENT (IpHead
->Fragment
) && (Assemble
->TotalLen
== 0)) {
378 Assemble
->TotalLen
= This
->End
;
382 // Deliver the whole packet if all the fragments received.
383 // All fragments received if:
384 // 1. received the last one, so, the total length is know
385 // 2. received all the data. If the last fragment on the
386 // queue ends at the total length, all data is received.
388 if ((Assemble
->TotalLen
!= 0) && (Assemble
->CurLen
>= Assemble
->TotalLen
)) {
390 RemoveEntryList (&Assemble
->Link
);
393 // If the packet is properly formated, the last fragment's End
394 // equals to the packet's total length. Otherwise, the packet
395 // is a fake, drop it now.
397 Fragment
= NET_LIST_USER_STRUCT (Head
->BackLink
, NET_BUF
, List
);
399 if (IP4_GET_CLIP_INFO (Fragment
)->End
!= Assemble
->TotalLen
) {
400 Ip4FreeAssembleEntry (Assemble
);
405 // Wrap the packet in a net buffer then deliver it up
407 NewPacket
= NetbufFromBufList (
408 &Assemble
->Fragments
,
415 if (NewPacket
== NULL
) {
416 Ip4FreeAssembleEntry (Assemble
);
420 NewPacket
->Ip
.Ip4
= Assemble
->Head
;
421 CopyMem (IP4_GET_CLIP_INFO (NewPacket
), Assemble
->Info
, sizeof (*IP4_GET_CLIP_INFO (NewPacket
)));
433 The callback function for the net buffer which wraps the packet processed by
434 IPsec. It releases the wrap packet and also signals IPsec to free the resources.
436 @param[in] Arg The wrap context
444 IP4_IPSEC_WRAP
*Wrap
;
446 Wrap
= (IP4_IPSEC_WRAP
*) Arg
;
448 if (Wrap
->IpSecRecycleSignal
!= NULL
) {
449 gBS
->SignalEvent (Wrap
->IpSecRecycleSignal
);
452 NetbufFree (Wrap
->Packet
);
460 The work function to locate IPsec protocol to process the inbound or
461 outbound IP packets. The process routine handls the packet with following
462 actions: bypass the packet, discard the packet, or protect the packet.
464 @param[in] IpSb The IP4 service instance
465 @param[in] Head The The caller supplied IP4 header.
466 @param[in, out] Netbuf The IP4 packet to be processed by IPsec
467 @param[in] Options The caller supplied options
468 @param[in] OptionsLen The length of the option
469 @param[in] Direction The directionality in an SPD entry,
470 EfiIPsecInBound or EfiIPsecOutBound
471 @param[in] Context The token's wrap
473 @retval EFI_SUCCESS The IPsec protocol is not available or disabled.
474 @retval EFI_SUCCESS The packet was bypassed and all buffers remain the same.
475 @retval EFI_SUCCESS The packet was protected.
476 @retval EFI_ACCESS_DENIED The packet was discarded.
477 @retval EFI_OUT_OF_RESOURCES There is no suffcient resource to complete the operation.
478 @retval EFI_BUFFER_TOO_SMALL The number of non-empty block is bigger than the
479 number of input data blocks when build a fragment table.
483 Ip4IpSecProcessPacket (
484 IN IP4_SERVICE
*IpSb
,
486 IN OUT NET_BUF
**Netbuf
,
488 IN UINT32 OptionsLen
,
489 IN EFI_IPSEC_TRAFFIC_DIR Direction
,
493 NET_FRAGMENT
*FragmentTable
;
494 UINT32 FragmentCount
;
495 EFI_EVENT RecycleEvent
;
497 IP4_TXTOKEN_WRAP
*TxWrap
;
498 IP4_IPSEC_WRAP
*IpSecWrap
;
501 Status
= EFI_SUCCESS
;
505 FragmentTable
= NULL
;
506 TxWrap
= (IP4_TXTOKEN_WRAP
*) Context
;
507 FragmentCount
= Packet
->BlockOpNum
;
509 if (mIpSec
== NULL
) {
510 gBS
->LocateProtocol (&gEfiIpSecProtocolGuid
, NULL
, (VOID
**) &mIpSec
);
511 if (mIpSec
!= NULL
) {
513 // Save the original MTU
515 IpSb
->OldMaxPacketSize
= IpSb
->MaxPacketSize
;
520 // Check whether the IPsec protocol is available.
522 if (mIpSec
== NULL
) {
526 // Check whether the IPsec enable variable is set.
528 if (mIpSec
->DisabledFlag
) {
530 // If IPsec is disabled, restore the original MTU
532 IpSb
->MaxPacketSize
= IpSb
->OldMaxPacketSize
;
536 // If IPsec is enabled, use the MTU which reduce the IPsec header length.
538 IpSb
->MaxPacketSize
= IpSb
->OldMaxPacketSize
- IP4_MAX_IPSEC_HEADLEN
;
542 // Rebuild fragment table from netbuf to ease IPsec process.
544 FragmentTable
= AllocateZeroPool (FragmentCount
* sizeof (NET_FRAGMENT
));
546 if (FragmentTable
== NULL
) {
547 Status
= EFI_OUT_OF_RESOURCES
;
551 Status
= NetbufBuildExt (Packet
, FragmentTable
, &FragmentCount
);
553 if (EFI_ERROR (Status
)) {
554 FreePool (FragmentTable
);
559 // Convert host byte order to network byte order
563 Status
= mIpSec
->Process (
571 (EFI_IPSEC_FRAGMENT_DATA
**) (&FragmentTable
),
577 // Convert back to host byte order
581 if (EFI_ERROR (Status
)) {
585 if (Direction
== EfiIPsecOutBound
&& TxWrap
!= NULL
) {
587 TxWrap
->IpSecRecycleSignal
= RecycleEvent
;
588 TxWrap
->Packet
= NetbufFromExt (
596 if (TxWrap
->Packet
== NULL
) {
597 Status
= EFI_OUT_OF_RESOURCES
;
601 *Netbuf
= TxWrap
->Packet
;
605 IpSecWrap
= AllocateZeroPool (sizeof (IP4_IPSEC_WRAP
));
607 if (IpSecWrap
== NULL
) {
611 IpSecWrap
->IpSecRecycleSignal
= RecycleEvent
;
612 IpSecWrap
->Packet
= Packet
;
613 Packet
= NetbufFromExt (
622 if (Packet
== NULL
) {
623 Status
= EFI_OUT_OF_RESOURCES
;
627 if (Direction
== EfiIPsecInBound
) {
628 Ip4PrependHead (Packet
, Head
, Options
, OptionsLen
);
629 Ip4NtohHead (Packet
->Ip
.Ip4
);
630 NetbufTrim (Packet
, (Head
->HeadLen
<< 2), TRUE
);
633 IP4_GET_CLIP_INFO (Packet
),
634 IP4_GET_CLIP_INFO (IpSecWrap
->Packet
),
635 sizeof (IP4_CLIP_INFO
)
647 The IP4 input routine. It is called by the IP4_INTERFACE when a
648 IP4 fragment is received from MNP.
650 @param[in] Ip4Instance The IP4 child that request the receive, most like
652 @param[in] Packet The IP4 packet received.
653 @param[in] IoStatus The return status of receive request.
654 @param[in] Flag The link layer flag for the packet received, such
656 @param[in] Context The IP4 service instance that own the MNP.
661 IN IP4_PROTOCOL
*Ip4Instance
,
663 IN EFI_STATUS IoStatus
,
677 IpSb
= (IP4_SERVICE
*) Context
;
679 if (EFI_ERROR (IoStatus
) || (IpSb
->State
== IP4_SERVICE_DESTORY
)) {
684 // Check that the IP4 header is correctly formatted
686 if (Packet
->TotalSize
< IP4_MIN_HEADLEN
) {
690 Head
= (IP4_HEAD
*) NetbufGetByte (Packet
, 0, NULL
);
691 HeadLen
= (Head
->HeadLen
<< 2);
692 TotalLen
= NTOHS (Head
->TotalLen
);
695 // Mnp may deliver frame trailer sequence up, trim it off.
697 if (TotalLen
< Packet
->TotalSize
) {
698 NetbufTrim (Packet
, Packet
->TotalSize
- TotalLen
, FALSE
);
701 if ((Head
->Ver
!= 4) || (HeadLen
< IP4_MIN_HEADLEN
) ||
702 (TotalLen
< HeadLen
) || (TotalLen
!= Packet
->TotalSize
)) {
707 // Some OS may send IP packets without checksum.
709 Checksum
= (UINT16
) (~NetblockChecksum ((UINT8
*) Head
, HeadLen
));
711 if ((Head
->Checksum
!= 0) && (Checksum
!= 0)) {
716 // Convert the IP header to host byte order, then get the per packet info.
718 Packet
->Ip
.Ip4
= Ip4NtohHead (Head
);
720 Info
= IP4_GET_CLIP_INFO (Packet
);
721 Info
->LinkFlag
= Flag
;
722 Info
->CastType
= Ip4GetHostCast (IpSb
, Head
->Dst
, Head
->Src
);
723 Info
->Start
= (Head
->Fragment
& IP4_HEAD_OFFSET_MASK
) << 3;
724 Info
->Length
= Head
->TotalLen
- HeadLen
;
725 Info
->End
= Info
->Start
+ Info
->Length
;
726 Info
->Status
= EFI_SUCCESS
;
729 // The packet is destinated to us if the CastType is non-zero.
731 if ((Info
->CastType
== 0) || (Info
->End
> IP4_MAX_PACKET_SIZE
)) {
736 // Validate the options. Don't call the Ip4OptionIsValid if
737 // there is no option to save some CPU process.
739 OptionLen
= HeadLen
- IP4_MIN_HEADLEN
;
741 if ((OptionLen
> 0) && !Ip4OptionIsValid ((UINT8
*) (Head
+ 1), OptionLen
, TRUE
)) {
746 // Trim the head off, after this point, the packet is headless.
747 // and Packet->TotalLen == Info->Length.
749 NetbufTrim (Packet
, HeadLen
, TRUE
);
752 // Reassemble the packet if this is a fragment. The packet is a
753 // fragment if its head has MF (more fragment) set, or it starts
756 if (((Head
->Fragment
& IP4_HEAD_MF_MASK
) != 0) || (Info
->Start
!= 0)) {
758 // Drop the fragment if DF is set but it is fragmented. Gateway
759 // need to send a type 4 destination unreache ICMP message here.
761 if ((Head
->Fragment
& IP4_HEAD_DF_MASK
) != 0) {
766 // The length of all but the last fragments is in the unit of 8 bytes.
768 if (((Head
->Fragment
& IP4_HEAD_MF_MASK
) != 0) && (Info
->Length
% 8 != 0)) {
772 Packet
= Ip4Reassemble (&IpSb
->Assemble
, Packet
);
775 // Packet assembly isn't complete, start receive more packet.
777 if (Packet
== NULL
) {
783 // After trim off, the packet is a esp/ah/udp/tcp/icmp6 net buffer,
784 // and no need consider any other ahead ext headers.
786 Status
= Ip4IpSecProcessPacket (
796 if (EFI_ERROR(Status
)) {
799 // Packet may have been changed. Head, HeadLen, TotalLen, and
800 // info must be reloaded bofore use. The ownership of the packet
801 // is transfered to the packet process logic.
803 Head
= Packet
->Ip
.Ip4
;
804 IP4_GET_CLIP_INFO (Packet
)->Status
= EFI_SUCCESS
;
806 switch (Head
->Protocol
) {
807 case EFI_IP_PROTO_ICMP
:
808 Ip4IcmpHandle (IpSb
, Head
, Packet
);
812 Ip4IgmpHandle (IpSb
, Head
, Packet
);
816 Ip4Demultiplex (IpSb
, Head
, Packet
);
822 // Dispatch the DPCs queued by the NotifyFunction of the rx token's events
823 // which are signaled with received data.
828 Ip4ReceiveFrame (IpSb
->DefaultInterface
, NULL
, Ip4AccpetFrame
, IpSb
);
831 if (Packet
!= NULL
) {
840 Check whether this IP child accepts the packet.
842 @param[in] IpInstance The IP child to check
843 @param[in] Head The IP header of the packet
844 @param[in] Packet The data of the packet
846 @retval TRUE If the child wants to receive the packet.
847 @retval FALSE Otherwise.
851 Ip4InstanceFrameAcceptable (
852 IN IP4_PROTOCOL
*IpInstance
,
857 IP4_ICMP_ERROR_HEAD Icmp
;
858 EFI_IP4_CONFIG_DATA
*Config
;
863 Config
= &IpInstance
->ConfigData
;
866 // Dirty trick for the Tiano UEFI network stack implmentation. If
867 // ReceiveTimeout == -1, the receive of the packet for this instance
868 // is disabled. The UEFI spec don't have such capability. We add
869 // this to improve the performance because IP will make a copy of
870 // the received packet for each accepting instance. Some IP instances
871 // used by UDP/TCP only send packets, they don't wants to receive.
873 if (Config
->ReceiveTimeout
== (UINT32
)(-1)) {
877 if (Config
->AcceptPromiscuous
) {
882 // Use protocol from the IP header embedded in the ICMP error
883 // message to filter, instead of ICMP itself. ICMP handle will
884 // call Ip4Demultiplex to deliver ICMP errors.
886 Proto
= Head
->Protocol
;
888 if ((Proto
== EFI_IP_PROTO_ICMP
) && (!Config
->AcceptAnyProtocol
) && (Proto
!= Config
->DefaultProtocol
)) {
889 NetbufCopy (Packet
, 0, sizeof (Icmp
.Head
), (UINT8
*) &Icmp
.Head
);
891 if (mIcmpClass
[Icmp
.Head
.Type
].IcmpClass
== ICMP_ERROR_MESSAGE
) {
892 if (!Config
->AcceptIcmpErrors
) {
896 NetbufCopy (Packet
, 0, sizeof (Icmp
), (UINT8
*) &Icmp
);
897 Proto
= Icmp
.IpHead
.Protocol
;
902 // Match the protocol
904 if (!Config
->AcceptAnyProtocol
&& (Proto
!= Config
->DefaultProtocol
)) {
909 // Check for broadcast, the caller has computed the packet's
910 // cast type for this child's interface.
912 Info
= IP4_GET_CLIP_INFO (Packet
);
914 if (IP4_IS_BROADCAST (Info
->CastType
)) {
915 return Config
->AcceptBroadcast
;
919 // If it is a multicast packet, check whether we are in the group.
921 if (Info
->CastType
== IP4_MULTICAST
) {
923 // Receive the multicast if the instance wants to receive all packets.
925 if (!IpInstance
->ConfigData
.UseDefaultAddress
&& (IpInstance
->Interface
->Ip
== 0)) {
929 for (Index
= 0; Index
< IpInstance
->GroupCount
; Index
++) {
930 if (IpInstance
->Groups
[Index
] == HTONL (Head
->Dst
)) {
935 return (BOOLEAN
)(Index
< IpInstance
->GroupCount
);
943 Enqueue a shared copy of the packet to the IP4 child if the
944 packet is acceptable to it. Here the data of the packet is
945 shared, but the net buffer isn't.
947 @param[in] IpInstance The IP4 child to enqueue the packet to
948 @param[in] Head The IP header of the received packet
949 @param[in] Packet The data of the received packet
951 @retval EFI_NOT_STARTED The IP child hasn't been configured.
952 @retval EFI_INVALID_PARAMETER The child doesn't want to receive the packet
953 @retval EFI_OUT_OF_RESOURCES Failed to allocate some resource
954 @retval EFI_SUCCESS A shared copy the packet is enqueued to the child.
958 Ip4InstanceEnquePacket (
959 IN IP4_PROTOCOL
*IpInstance
,
968 // Check whether the packet is acceptable to this instance.
970 if (IpInstance
->State
!= IP4_STATE_CONFIGED
) {
971 return EFI_NOT_STARTED
;
974 if (!Ip4InstanceFrameAcceptable (IpInstance
, Head
, Packet
)) {
975 return EFI_INVALID_PARAMETER
;
979 // Enque a shared copy of the packet.
981 Clone
= NetbufClone (Packet
);
984 return EFI_OUT_OF_RESOURCES
;
988 // Set the receive time out for the assembled packet. If it expires,
989 // packet will be removed from the queue.
991 Info
= IP4_GET_CLIP_INFO (Clone
);
992 Info
->Life
= IP4_US_TO_SEC (IpInstance
->ConfigData
.ReceiveTimeout
);
994 InsertTailList (&IpInstance
->Received
, &Clone
->List
);
1000 The signal handle of IP4's recycle event. It is called back
1001 when the upper layer release the packet.
1003 @param Event The IP4's recycle event.
1004 @param Context The context of the handle, which is a
1010 Ip4OnRecyclePacket (
1015 IP4_RXDATA_WRAP
*Wrap
;
1017 Wrap
= (IP4_RXDATA_WRAP
*) Context
;
1019 EfiAcquireLockOrFail (&Wrap
->IpInstance
->RecycleLock
);
1020 RemoveEntryList (&Wrap
->Link
);
1021 EfiReleaseLock (&Wrap
->IpInstance
->RecycleLock
);
1023 ASSERT (!NET_BUF_SHARED (Wrap
->Packet
));
1024 NetbufFree (Wrap
->Packet
);
1026 gBS
->CloseEvent (Wrap
->RxData
.RecycleSignal
);
1032 Wrap the received packet to a IP4_RXDATA_WRAP, which will be
1033 delivered to the upper layer. Each IP4 child that accepts the
1034 packet will get a not-shared copy of the packet which is wrapped
1035 in the IP4_RXDATA_WRAP. The IP4_RXDATA_WRAP->RxData is passed
1036 to the upper layer. Upper layer will signal the recycle event in
1037 it when it is done with the packet.
1039 @param[in] IpInstance The IP4 child to receive the packet
1040 @param[in] Packet The packet to deliver up.
1042 @retval Wrap if warp the packet succeed.
1043 @retval NULL failed to wrap the packet .
1048 IN IP4_PROTOCOL
*IpInstance
,
1052 IP4_RXDATA_WRAP
*Wrap
;
1053 EFI_IP4_RECEIVE_DATA
*RxData
;
1056 Wrap
= AllocatePool (IP4_RXDATA_WRAP_SIZE (Packet
->BlockOpNum
));
1062 InitializeListHead (&Wrap
->Link
);
1064 Wrap
->IpInstance
= IpInstance
;
1065 Wrap
->Packet
= Packet
;
1066 RxData
= &Wrap
->RxData
;
1068 ZeroMem (&RxData
->TimeStamp
, sizeof (EFI_TIME
));
1070 Status
= gBS
->CreateEvent (
1075 &RxData
->RecycleSignal
1078 if (EFI_ERROR (Status
)) {
1083 ASSERT (Packet
->Ip
.Ip4
!= NULL
);
1086 // The application expects a network byte order header.
1088 RxData
->HeaderLength
= (Packet
->Ip
.Ip4
->HeadLen
<< 2);
1089 RxData
->Header
= (EFI_IP4_HEADER
*) Ip4NtohHead (Packet
->Ip
.Ip4
);
1091 RxData
->OptionsLength
= RxData
->HeaderLength
- IP4_MIN_HEADLEN
;
1092 RxData
->Options
= NULL
;
1094 if (RxData
->OptionsLength
!= 0) {
1095 RxData
->Options
= (VOID
*) (RxData
->Header
+ 1);
1098 RxData
->DataLength
= Packet
->TotalSize
;
1101 // Build the fragment table to be delivered up.
1103 RxData
->FragmentCount
= Packet
->BlockOpNum
;
1104 NetbufBuildExt (Packet
, (NET_FRAGMENT
*) RxData
->FragmentTable
, &RxData
->FragmentCount
);
1111 Deliver the received packets to upper layer if there are both received
1112 requests and enqueued packets. If the enqueued packet is shared, it will
1113 duplicate it to a non-shared packet, release the shared packet, then
1114 deliver the non-shared packet up.
1116 @param[in] IpInstance The IP child to deliver the packet up.
1118 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources to deliver the
1120 @retval EFI_SUCCESS All the enqueued packets that can be delivered
1125 Ip4InstanceDeliverPacket (
1126 IN IP4_PROTOCOL
*IpInstance
1129 EFI_IP4_COMPLETION_TOKEN
*Token
;
1130 IP4_RXDATA_WRAP
*Wrap
;
1136 // Deliver a packet if there are both a packet and a receive token.
1138 while (!IsListEmpty (&IpInstance
->Received
) &&
1139 !NetMapIsEmpty (&IpInstance
->RxTokens
)) {
1141 Packet
= NET_LIST_HEAD (&IpInstance
->Received
, NET_BUF
, List
);
1143 if (!NET_BUF_SHARED (Packet
)) {
1145 // If this is the only instance that wants the packet, wrap it up.
1147 Wrap
= Ip4WrapRxData (IpInstance
, Packet
);
1150 return EFI_OUT_OF_RESOURCES
;
1153 RemoveEntryList (&Packet
->List
);
1157 // Create a duplicated packet if this packet is shared
1159 Dup
= NetbufDuplicate (Packet
, NULL
, IP4_MAX_HEADLEN
);
1162 return EFI_OUT_OF_RESOURCES
;
1166 // Copy the IP head over. The packet to deliver up is
1167 // headless. Trim the head off after copy. The IP head
1168 // may be not continuous before the data.
1170 Head
= NetbufAllocSpace (Dup
, IP4_MAX_HEADLEN
, NET_BUF_HEAD
);
1171 Dup
->Ip
.Ip4
= (IP4_HEAD
*) Head
;
1173 CopyMem (Head
, Packet
->Ip
.Ip4
, Packet
->Ip
.Ip4
->HeadLen
<< 2);
1174 NetbufTrim (Dup
, IP4_MAX_HEADLEN
, TRUE
);
1176 Wrap
= Ip4WrapRxData (IpInstance
, Dup
);
1180 return EFI_OUT_OF_RESOURCES
;
1183 RemoveEntryList (&Packet
->List
);
1184 NetbufFree (Packet
);
1190 // Insert it into the delivered packet, then get a user's
1191 // receive token, pass the wrapped packet up.
1193 EfiAcquireLockOrFail (&IpInstance
->RecycleLock
);
1194 InsertHeadList (&IpInstance
->Delivered
, &Wrap
->Link
);
1195 EfiReleaseLock (&IpInstance
->RecycleLock
);
1197 Token
= NetMapRemoveHead (&IpInstance
->RxTokens
, NULL
);
1198 Token
->Status
= IP4_GET_CLIP_INFO (Packet
)->Status
;
1199 Token
->Packet
.RxData
= &Wrap
->RxData
;
1201 gBS
->SignalEvent (Token
->Event
);
1209 Enqueue a received packet to all the IP children that share
1212 @param[in] IpSb The IP4 service instance that receive the packet
1213 @param[in] Head The header of the received packet
1214 @param[in] Packet The data of the received packet
1215 @param[in] IpIf The interface to enqueue the packet to
1217 @return The number of the IP4 children that accepts the packet
1221 Ip4InterfaceEnquePacket (
1222 IN IP4_SERVICE
*IpSb
,
1225 IN IP4_INTERFACE
*IpIf
1228 IP4_PROTOCOL
*IpInstance
;
1229 IP4_CLIP_INFO
*Info
;
1236 // First, check that the packet is acceptable to this interface
1237 // and find the local cast type for the interface. A packet sent
1238 // to say 192.168.1.1 should NOT be delliever to 10.0.0.1 unless
1239 // promiscuous receiving.
1242 Info
= IP4_GET_CLIP_INFO (Packet
);
1244 if ((Info
->CastType
== IP4_MULTICAST
) || (Info
->CastType
== IP4_LOCAL_BROADCAST
)) {
1246 // If the CastType is multicast, don't need to filter against
1247 // the group address here, Ip4InstanceFrameAcceptable will do
1250 LocalType
= Info
->CastType
;
1254 // Check the destination againist local IP. If the station
1255 // address is 0.0.0.0, it means receiving all the IP destined
1256 // to local non-zero IP. Otherwise, it is necessary to compare
1257 // the destination to the interface's IP address.
1259 if (IpIf
->Ip
== IP4_ALLZERO_ADDRESS
) {
1260 LocalType
= IP4_LOCAL_HOST
;
1263 LocalType
= Ip4GetNetCast (Head
->Dst
, IpIf
);
1265 if ((LocalType
== 0) && IpIf
->PromiscRecv
) {
1266 LocalType
= IP4_PROMISCUOUS
;
1271 if (LocalType
== 0) {
1276 // Iterate through the ip instances on the interface, enqueue
1277 // the packet if filter passed. Save the original cast type,
1278 // and pass the local cast type to the IP children on the
1279 // interface. The global cast type will be restored later.
1281 SavedType
= Info
->CastType
;
1282 Info
->CastType
= LocalType
;
1286 NET_LIST_FOR_EACH (Entry
, &IpIf
->IpInstances
) {
1287 IpInstance
= NET_LIST_USER_STRUCT (Entry
, IP4_PROTOCOL
, AddrLink
);
1288 NET_CHECK_SIGNATURE (IpInstance
, IP4_PROTOCOL_SIGNATURE
);
1290 if (Ip4InstanceEnquePacket (IpInstance
, Head
, Packet
) == EFI_SUCCESS
) {
1295 Info
->CastType
= SavedType
;
1301 Deliver the packet for each IP4 child on the interface.
1303 @param[in] IpSb The IP4 service instance that received the packet
1304 @param[in] IpIf The IP4 interface to deliver the packet.
1306 @retval EFI_SUCCESS It always returns EFI_SUCCESS now
1310 Ip4InterfaceDeliverPacket (
1311 IN IP4_SERVICE
*IpSb
,
1312 IN IP4_INTERFACE
*IpIf
1315 IP4_PROTOCOL
*Ip4Instance
;
1318 NET_LIST_FOR_EACH (Entry
, &IpIf
->IpInstances
) {
1319 Ip4Instance
= NET_LIST_USER_STRUCT (Entry
, IP4_PROTOCOL
, AddrLink
);
1320 Ip4InstanceDeliverPacket (Ip4Instance
);
1328 Demultiple the packet. the packet delivery is processed in two
1329 passes. The first pass will enque a shared copy of the packet
1330 to each IP4 child that accepts the packet. The second pass will
1331 deliver a non-shared copy of the packet to each IP4 child that
1332 has pending receive requests. Data is copied if more than one
1333 child wants to consume the packet because each IP child needs
1334 its own copy of the packet to make changes.
1336 @param[in] IpSb The IP4 service instance that received the packet
1337 @param[in] Head The header of the received packet
1338 @param[in] Packet The data of the received packet
1340 @retval EFI_NOT_FOUND No IP child accepts the packet
1341 @retval EFI_SUCCESS The packet is enqueued or delivered to some IP
1347 IN IP4_SERVICE
*IpSb
,
1353 IP4_INTERFACE
*IpIf
;
1357 // Two pass delivery: first, enque a shared copy of the packet
1358 // to each instance that accept the packet.
1362 NET_LIST_FOR_EACH (Entry
, &IpSb
->Interfaces
) {
1363 IpIf
= NET_LIST_USER_STRUCT (Entry
, IP4_INTERFACE
, Link
);
1365 if (IpIf
->Configured
) {
1366 Enqueued
+= Ip4InterfaceEnquePacket (IpSb
, Head
, Packet
, IpIf
);
1371 // Second: deliver a duplicate of the packet to each instance.
1372 // Release the local reference first, so that the last instance
1373 // getting the packet will not copy the data.
1375 NetbufFree (Packet
);
1377 if (Enqueued
== 0) {
1378 return EFI_NOT_FOUND
;
1381 NET_LIST_FOR_EACH (Entry
, &IpSb
->Interfaces
) {
1382 IpIf
= NET_LIST_USER_STRUCT (Entry
, IP4_INTERFACE
, Link
);
1384 if (IpIf
->Configured
) {
1385 Ip4InterfaceDeliverPacket (IpSb
, IpIf
);
1394 Timeout the fragment and enqueued packets.
1396 @param[in] IpSb The IP4 service instance to timeout
1400 Ip4PacketTimerTicking (
1401 IN IP4_SERVICE
*IpSb
1404 LIST_ENTRY
*InstanceEntry
;
1407 IP4_PROTOCOL
*IpInstance
;
1408 IP4_ASSEMBLE_ENTRY
*Assemble
;
1410 IP4_CLIP_INFO
*Info
;
1414 // First, time out the fragments. The packet's life is counting down
1415 // once the first-arrived fragment was received.
1417 for (Index
= 0; Index
< IP4_ASSEMLE_HASH_SIZE
; Index
++) {
1418 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &IpSb
->Assemble
.Bucket
[Index
]) {
1419 Assemble
= NET_LIST_USER_STRUCT (Entry
, IP4_ASSEMBLE_ENTRY
, Link
);
1421 if ((Assemble
->Life
> 0) && (--Assemble
->Life
== 0)) {
1422 RemoveEntryList (Entry
);
1423 Ip4FreeAssembleEntry (Assemble
);
1428 NET_LIST_FOR_EACH (InstanceEntry
, &IpSb
->Children
) {
1429 IpInstance
= NET_LIST_USER_STRUCT (InstanceEntry
, IP4_PROTOCOL
, Link
);
1432 // Second, time out the assembled packets enqueued on each IP child.
1434 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &IpInstance
->Received
) {
1435 Packet
= NET_LIST_USER_STRUCT (Entry
, NET_BUF
, List
);
1436 Info
= IP4_GET_CLIP_INFO (Packet
);
1438 if ((Info
->Life
> 0) && (--Info
->Life
== 0)) {
1439 RemoveEntryList (Entry
);
1440 NetbufFree (Packet
);
1445 // Third: time out the transmitted packets.
1447 NetMapIterate (&IpInstance
->TxTokens
, Ip4SentPacketTicking
, NULL
);