4 Copyright (c) 2005 - 2009, 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
);
88 gBS
->FreePool (Assemble
);
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
;
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
= Assemble
->Head
;
421 CopyMem (IP4_GET_CLIP_INFO (NewPacket
), Assemble
->Info
, sizeof (*IP4_GET_CLIP_INFO (NewPacket
)));
434 The IP4 input routine. It is called by the IP4_INTERFACE when a
435 IP4 fragment is received from MNP.
437 @param[in] Ip4Instance The IP4 child that request the receive, most like
439 @param[in] Packet The IP4 packet received.
440 @param[in] IoStatus The return status of receive request.
441 @param[in] Flag The link layer flag for the packet received, such
443 @param[in] Context The IP4 service instance that own the MNP.
448 IN IP4_PROTOCOL
*Ip4Instance
,
450 IN EFI_STATUS IoStatus
,
463 IpSb
= (IP4_SERVICE
*) Context
;
465 if (EFI_ERROR (IoStatus
) || (IpSb
->State
== IP4_SERVICE_DESTORY
)) {
470 // Check that the IP4 header is correctly formatted
472 if (Packet
->TotalSize
< IP4_MIN_HEADLEN
) {
476 Head
= (IP4_HEAD
*) NetbufGetByte (Packet
, 0, NULL
);
477 HeadLen
= (Head
->HeadLen
<< 2);
478 TotalLen
= NTOHS (Head
->TotalLen
);
481 // Mnp may deliver frame trailer sequence up, trim it off.
483 if (TotalLen
< Packet
->TotalSize
) {
484 NetbufTrim (Packet
, Packet
->TotalSize
- TotalLen
, FALSE
);
487 if ((Head
->Ver
!= 4) || (HeadLen
< IP4_MIN_HEADLEN
) ||
488 (TotalLen
< HeadLen
) || (TotalLen
!= Packet
->TotalSize
)) {
493 // Some OS may send IP packets without checksum.
495 Checksum
= (UINT16
) (~NetblockChecksum ((UINT8
*) Head
, HeadLen
));
497 if ((Head
->Checksum
!= 0) && (Checksum
!= 0)) {
502 // Convert the IP header to host byte order, then get the per packet info.
504 Packet
->Ip
= Ip4NtohHead (Head
);
506 Info
= IP4_GET_CLIP_INFO (Packet
);
507 Info
->LinkFlag
= Flag
;
508 Info
->CastType
= Ip4GetHostCast (IpSb
, Head
->Dst
, Head
->Src
);
509 Info
->Start
= (Head
->Fragment
& IP4_HEAD_OFFSET_MASK
) << 3;
510 Info
->Length
= Head
->TotalLen
- HeadLen
;
511 Info
->End
= Info
->Start
+ Info
->Length
;
512 Info
->Status
= EFI_SUCCESS
;
515 // The packet is destinated to us if the CastType is non-zero.
517 if ((Info
->CastType
== 0) || (Info
->End
> IP4_MAX_PACKET_SIZE
)) {
522 // Validate the options. Don't call the Ip4OptionIsValid if
523 // there is no option to save some CPU process.
525 OptionLen
= HeadLen
- IP4_MIN_HEADLEN
;
527 if ((OptionLen
> 0) && !Ip4OptionIsValid ((UINT8
*) (Head
+ 1), OptionLen
, TRUE
)) {
532 // Trim the head off, after this point, the packet is headless.
533 // and Packet->TotalLen == Info->Length.
535 NetbufTrim (Packet
, HeadLen
, TRUE
);
538 // Reassemble the packet if this is a fragment. The packet is a
539 // fragment if its head has MF (more fragment) set, or it starts
542 if (((Head
->Fragment
& IP4_HEAD_MF_MASK
) != 0) || (Info
->Start
!= 0)) {
544 // Drop the fragment if DF is set but it is fragmented. Gateway
545 // need to send a type 4 destination unreache ICMP message here.
547 if ((Head
->Fragment
& IP4_HEAD_DF_MASK
) != 0) {
552 // The length of all but the last fragments is in the unit of 8 bytes.
554 if (((Head
->Fragment
& IP4_HEAD_MF_MASK
) != 0) && (Info
->Length
% 8 != 0)) {
558 Packet
= Ip4Reassemble (&IpSb
->Assemble
, Packet
);
561 // Packet assembly isn't complete, start receive more packet.
563 if (Packet
== NULL
) {
569 // Packet may have been changed. Head, HeadLen, TotalLen, and
570 // info must be reloaded bofore use. The ownership of the packet
571 // is transfered to the packet process logic.
574 IP4_GET_CLIP_INFO (Packet
)->Status
= EFI_SUCCESS
;
576 switch (Head
->Protocol
) {
578 Ip4IcmpHandle (IpSb
, Head
, Packet
);
582 Ip4IgmpHandle (IpSb
, Head
, Packet
);
586 Ip4Demultiplex (IpSb
, Head
, Packet
);
592 // Dispatch the DPCs queued by the NotifyFunction of the rx token's events
593 // which are signaled with received data.
598 Ip4ReceiveFrame (IpSb
->DefaultInterface
, NULL
, Ip4AccpetFrame
, IpSb
);
601 if (Packet
!= NULL
) {
610 Check whether this IP child accepts the packet.
612 @param[in] IpInstance The IP child to check
613 @param[in] Head The IP header of the packet
614 @param[in] Packet The data of the packet
616 @retval TRUE If the child wants to receive the packet.
617 @retval FALSE Otherwise.
621 Ip4InstanceFrameAcceptable (
622 IN IP4_PROTOCOL
*IpInstance
,
627 IP4_ICMP_ERROR_HEAD Icmp
;
628 EFI_IP4_CONFIG_DATA
*Config
;
633 Config
= &IpInstance
->ConfigData
;
636 // Dirty trick for the Tiano UEFI network stack implmentation. If
637 // ReceiveTimeout == -1, the receive of the packet for this instance
638 // is disabled. The UEFI spec don't have such capability. We add
639 // this to improve the performance because IP will make a copy of
640 // the received packet for each accepting instance. Some IP instances
641 // used by UDP/TCP only send packets, they don't wants to receive.
643 if (Config
->ReceiveTimeout
== (UINT32
)(-1)) {
647 if (Config
->AcceptPromiscuous
) {
652 // Use protocol from the IP header embedded in the ICMP error
653 // message to filter, instead of ICMP itself. ICMP handle will
654 // can Ip4Demultiplex to deliver ICMP errors.
656 Proto
= Head
->Protocol
;
658 if (Proto
== IP4_PROTO_ICMP
) {
659 NetbufCopy (Packet
, 0, sizeof (Icmp
.Head
), (UINT8
*) &Icmp
.Head
);
661 if (mIcmpClass
[Icmp
.Head
.Type
].IcmpClass
== ICMP_ERROR_MESSAGE
) {
662 if (!Config
->AcceptIcmpErrors
) {
666 NetbufCopy (Packet
, 0, sizeof (Icmp
), (UINT8
*) &Icmp
);
667 Proto
= Icmp
.IpHead
.Protocol
;
672 // Match the protocol
674 if (!Config
->AcceptAnyProtocol
&& (Proto
!= Config
->DefaultProtocol
)) {
679 // Check for broadcast, the caller has computed the packet's
680 // cast type for this child's interface.
682 Info
= IP4_GET_CLIP_INFO (Packet
);
684 if (IP4_IS_BROADCAST (Info
->CastType
)) {
685 return Config
->AcceptBroadcast
;
689 // If it is a multicast packet, check whether we are in the group.
691 if (Info
->CastType
== IP4_MULTICAST
) {
693 // Receive the multicast if the instance wants to receive all packets.
695 if (!IpInstance
->ConfigData
.UseDefaultAddress
&& (IpInstance
->Interface
->Ip
== 0)) {
699 for (Index
= 0; Index
< IpInstance
->GroupCount
; Index
++) {
700 if (IpInstance
->Groups
[Index
] == HTONL (Head
->Dst
)) {
705 return (BOOLEAN
)(Index
< IpInstance
->GroupCount
);
713 Enqueue a shared copy of the packet to the IP4 child if the
714 packet is acceptable to it. Here the data of the packet is
715 shared, but the net buffer isn't.
717 @param[in] IpInstance The IP4 child to enqueue the packet to
718 @param[in] Head The IP header of the received packet
719 @param[in] Packet The data of the received packet
721 @retval EFI_NOT_STARTED The IP child hasn't been configured.
722 @retval EFI_INVALID_PARAMETER The child doesn't want to receive the packet
723 @retval EFI_OUT_OF_RESOURCES Failed to allocate some resource
724 @retval EFI_SUCCESS A shared copy the packet is enqueued to the child.
728 Ip4InstanceEnquePacket (
729 IN IP4_PROTOCOL
*IpInstance
,
738 // Check whether the packet is acceptable to this instance.
740 if (IpInstance
->State
!= IP4_STATE_CONFIGED
) {
741 return EFI_NOT_STARTED
;
744 if (!Ip4InstanceFrameAcceptable (IpInstance
, Head
, Packet
)) {
745 return EFI_INVALID_PARAMETER
;
749 // Enque a shared copy of the packet.
751 Clone
= NetbufClone (Packet
);
754 return EFI_OUT_OF_RESOURCES
;
758 // Set the receive time out for the assembled packet. If it expires,
759 // packet will be removed from the queue.
761 Info
= IP4_GET_CLIP_INFO (Clone
);
762 Info
->Life
= IP4_US_TO_SEC (IpInstance
->ConfigData
.ReceiveTimeout
);
764 InsertTailList (&IpInstance
->Received
, &Clone
->List
);
770 The signal handle of IP4's recycle event. It is called back
771 when the upper layer release the packet.
773 @param Event The IP4's recycle event.
774 @param Context The context of the handle, which is a
785 IP4_RXDATA_WRAP
*Wrap
;
787 Wrap
= (IP4_RXDATA_WRAP
*) Context
;
789 EfiAcquireLockOrFail (&Wrap
->IpInstance
->RecycleLock
);
790 RemoveEntryList (&Wrap
->Link
);
791 EfiReleaseLock (&Wrap
->IpInstance
->RecycleLock
);
793 ASSERT (!NET_BUF_SHARED (Wrap
->Packet
));
794 NetbufFree (Wrap
->Packet
);
796 gBS
->CloseEvent (Wrap
->RxData
.RecycleSignal
);
797 gBS
->FreePool (Wrap
);
802 Wrap the received packet to a IP4_RXDATA_WRAP, which will be
803 delivered to the upper layer. Each IP4 child that accepts the
804 packet will get a not-shared copy of the packet which is wrapped
805 in the IP4_RXDATA_WRAP. The IP4_RXDATA_WRAP->RxData is passed
806 to the upper layer. Upper layer will signal the recycle event in
807 it when it is done with the packet.
809 @param[in] IpInstance The IP4 child to receive the packet
810 @param[in] Packet The packet to deliver up.
812 @retval Wrap if warp the packet succeed.
813 @retval NULL failed to wrap the packet .
818 IN IP4_PROTOCOL
*IpInstance
,
822 IP4_RXDATA_WRAP
*Wrap
;
823 EFI_IP4_RECEIVE_DATA
*RxData
;
826 Wrap
= AllocatePool (IP4_RXDATA_WRAP_SIZE (Packet
->BlockOpNum
));
832 InitializeListHead (&Wrap
->Link
);
834 Wrap
->IpInstance
= IpInstance
;
835 Wrap
->Packet
= Packet
;
836 RxData
= &Wrap
->RxData
;
838 ZeroMem (&RxData
->TimeStamp
, sizeof (EFI_TIME
));
840 Status
= gBS
->CreateEvent (
845 &RxData
->RecycleSignal
848 if (EFI_ERROR (Status
)) {
849 gBS
->FreePool (Wrap
);
853 ASSERT (Packet
->Ip
!= NULL
);
856 // The application expects a network byte order header.
858 RxData
->HeaderLength
= (Packet
->Ip
->HeadLen
<< 2);
859 RxData
->Header
= (EFI_IP4_HEADER
*) Ip4NtohHead (Packet
->Ip
);
861 RxData
->OptionsLength
= RxData
->HeaderLength
- IP4_MIN_HEADLEN
;
862 RxData
->Options
= NULL
;
864 if (RxData
->OptionsLength
!= 0) {
865 RxData
->Options
= (VOID
*) (RxData
->Header
+ 1);
868 RxData
->DataLength
= Packet
->TotalSize
;
871 // Build the fragment table to be delivered up.
873 RxData
->FragmentCount
= Packet
->BlockOpNum
;
874 NetbufBuildExt (Packet
, (NET_FRAGMENT
*) RxData
->FragmentTable
, &RxData
->FragmentCount
);
881 Deliver the received packets to upper layer if there are both received
882 requests and enqueued packets. If the enqueued packet is shared, it will
883 duplicate it to a non-shared packet, release the shared packet, then
884 deliver the non-shared packet up.
886 @param[in] IpInstance The IP child to deliver the packet up.
888 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources to deliver the
890 @retval EFI_SUCCESS All the enqueued packets that can be delivered
895 Ip4InstanceDeliverPacket (
896 IN IP4_PROTOCOL
*IpInstance
899 EFI_IP4_COMPLETION_TOKEN
*Token
;
900 IP4_RXDATA_WRAP
*Wrap
;
906 // Deliver a packet if there are both a packet and a receive token.
908 while (!IsListEmpty (&IpInstance
->Received
) &&
909 !NetMapIsEmpty (&IpInstance
->RxTokens
)) {
911 Packet
= NET_LIST_HEAD (&IpInstance
->Received
, NET_BUF
, List
);
913 if (!NET_BUF_SHARED (Packet
)) {
915 // If this is the only instance that wants the packet, wrap it up.
917 Wrap
= Ip4WrapRxData (IpInstance
, Packet
);
920 return EFI_OUT_OF_RESOURCES
;
923 RemoveEntryList (&Packet
->List
);
927 // Create a duplicated packet if this packet is shared
929 Dup
= NetbufDuplicate (Packet
, NULL
, IP4_MAX_HEADLEN
);
932 return EFI_OUT_OF_RESOURCES
;
936 // Copy the IP head over. The packet to deliver up is
937 // headless. Trim the head off after copy. The IP head
938 // may be not continuous before the data.
940 Head
= NetbufAllocSpace (Dup
, IP4_MAX_HEADLEN
, NET_BUF_HEAD
);
941 Dup
->Ip
= (IP4_HEAD
*) Head
;
943 CopyMem (Head
, Packet
->Ip
, Packet
->Ip
->HeadLen
<< 2);
944 NetbufTrim (Dup
, IP4_MAX_HEADLEN
, TRUE
);
946 Wrap
= Ip4WrapRxData (IpInstance
, Dup
);
950 return EFI_OUT_OF_RESOURCES
;
953 RemoveEntryList (&Packet
->List
);
960 // Insert it into the delivered packet, then get a user's
961 // receive token, pass the wrapped packet up.
963 EfiAcquireLockOrFail (&IpInstance
->RecycleLock
);
964 InsertHeadList (&IpInstance
->Delivered
, &Wrap
->Link
);
965 EfiReleaseLock (&IpInstance
->RecycleLock
);
967 Token
= NetMapRemoveHead (&IpInstance
->RxTokens
, NULL
);
968 Token
->Status
= IP4_GET_CLIP_INFO (Packet
)->Status
;
969 Token
->Packet
.RxData
= &Wrap
->RxData
;
971 gBS
->SignalEvent (Token
->Event
);
979 Enqueue a received packet to all the IP children that share
982 @param[in] IpSb The IP4 service instance that receive the packet
983 @param[in] Head The header of the received packet
984 @param[in] Packet The data of the received packet
985 @param[in] IpIf The interface to enqueue the packet to
987 @return The number of the IP4 children that accepts the packet
991 Ip4InterfaceEnquePacket (
992 IN IP4_SERVICE
*IpSb
,
995 IN IP4_INTERFACE
*IpIf
998 IP4_PROTOCOL
*IpInstance
;
1006 // First, check that the packet is acceptable to this interface
1007 // and find the local cast type for the interface. A packet sent
1008 // to say 192.168.1.1 should NOT be delliever to 10.0.0.1 unless
1009 // promiscuous receiving.
1012 Info
= IP4_GET_CLIP_INFO (Packet
);
1014 if ((Info
->CastType
== IP4_MULTICAST
) || (Info
->CastType
== IP4_LOCAL_BROADCAST
)) {
1016 // If the CastType is multicast, don't need to filter against
1017 // the group address here, Ip4InstanceFrameAcceptable will do
1020 LocalType
= Info
->CastType
;
1024 // Check the destination againist local IP. If the station
1025 // address is 0.0.0.0, it means receiving all the IP destined
1026 // to local non-zero IP. Otherwise, it is necessary to compare
1027 // the destination to the interface's IP address.
1029 if (IpIf
->Ip
== IP4_ALLZERO_ADDRESS
) {
1030 LocalType
= IP4_LOCAL_HOST
;
1033 LocalType
= Ip4GetNetCast (Head
->Dst
, IpIf
);
1035 if ((LocalType
== 0) && IpIf
->PromiscRecv
) {
1036 LocalType
= IP4_PROMISCUOUS
;
1041 if (LocalType
== 0) {
1046 // Iterate through the ip instances on the interface, enqueue
1047 // the packet if filter passed. Save the original cast type,
1048 // and pass the local cast type to the IP children on the
1049 // interface. The global cast type will be restored later.
1051 SavedType
= Info
->CastType
;
1052 Info
->CastType
= LocalType
;
1056 NET_LIST_FOR_EACH (Entry
, &IpIf
->IpInstances
) {
1057 IpInstance
= NET_LIST_USER_STRUCT (Entry
, IP4_PROTOCOL
, AddrLink
);
1058 NET_CHECK_SIGNATURE (IpInstance
, IP4_PROTOCOL_SIGNATURE
);
1060 if (Ip4InstanceEnquePacket (IpInstance
, Head
, Packet
) == EFI_SUCCESS
) {
1065 Info
->CastType
= SavedType
;
1071 Deliver the packet for each IP4 child on the interface.
1073 @param[in] IpSb The IP4 service instance that received the packet
1074 @param[in] IpIf The IP4 interface to deliver the packet.
1076 @retval EFI_SUCCESS It always returns EFI_SUCCESS now
1080 Ip4InterfaceDeliverPacket (
1081 IN IP4_SERVICE
*IpSb
,
1082 IN IP4_INTERFACE
*IpIf
1085 IP4_PROTOCOL
*Ip4Instance
;
1088 NET_LIST_FOR_EACH (Entry
, &IpIf
->IpInstances
) {
1089 Ip4Instance
= NET_LIST_USER_STRUCT (Entry
, IP4_PROTOCOL
, AddrLink
);
1090 Ip4InstanceDeliverPacket (Ip4Instance
);
1098 Demultiple the packet. the packet delivery is processed in two
1099 passes. The first pass will enque a shared copy of the packet
1100 to each IP4 child that accepts the packet. The second pass will
1101 deliver a non-shared copy of the packet to each IP4 child that
1102 has pending receive requests. Data is copied if more than one
1103 child wants to consume the packet because each IP child needs
1104 its own copy of the packet to make changes.
1106 @param[in] IpSb The IP4 service instance that received the packet
1107 @param[in] Head The header of the received packet
1108 @param[in] Packet The data of the received packet
1110 @retval EFI_NOT_FOUND No IP child accepts the packet
1111 @retval EFI_SUCCESS The packet is enqueued or delivered to some IP
1117 IN IP4_SERVICE
*IpSb
,
1123 IP4_INTERFACE
*IpIf
;
1127 // Two pass delivery: first, enque a shared copy of the packet
1128 // to each instance that accept the packet.
1132 NET_LIST_FOR_EACH (Entry
, &IpSb
->Interfaces
) {
1133 IpIf
= NET_LIST_USER_STRUCT (Entry
, IP4_INTERFACE
, Link
);
1135 if (IpIf
->Configured
) {
1136 Enqueued
+= Ip4InterfaceEnquePacket (IpSb
, Head
, Packet
, IpIf
);
1141 // Second: deliver a duplicate of the packet to each instance.
1142 // Release the local reference first, so that the last instance
1143 // getting the packet will not copy the data.
1145 NetbufFree (Packet
);
1147 if (Enqueued
== 0) {
1148 return EFI_NOT_FOUND
;
1151 NET_LIST_FOR_EACH (Entry
, &IpSb
->Interfaces
) {
1152 IpIf
= NET_LIST_USER_STRUCT (Entry
, IP4_INTERFACE
, Link
);
1154 if (IpIf
->Configured
) {
1155 Ip4InterfaceDeliverPacket (IpSb
, IpIf
);
1164 Timeout the fragment and enqueued packets.
1166 @param[in] IpSb The IP4 service instance to timeout
1170 Ip4PacketTimerTicking (
1171 IN IP4_SERVICE
*IpSb
1174 LIST_ENTRY
*InstanceEntry
;
1177 IP4_PROTOCOL
*IpInstance
;
1178 IP4_ASSEMBLE_ENTRY
*Assemble
;
1180 IP4_CLIP_INFO
*Info
;
1184 // First, time out the fragments. The packet's life is counting down
1185 // once the first-arrived fragment was received.
1187 for (Index
= 0; Index
< IP4_ASSEMLE_HASH_SIZE
; Index
++) {
1188 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &IpSb
->Assemble
.Bucket
[Index
]) {
1189 Assemble
= NET_LIST_USER_STRUCT (Entry
, IP4_ASSEMBLE_ENTRY
, Link
);
1191 if ((Assemble
->Life
> 0) && (--Assemble
->Life
== 0)) {
1192 RemoveEntryList (Entry
);
1193 Ip4FreeAssembleEntry (Assemble
);
1198 NET_LIST_FOR_EACH (InstanceEntry
, &IpSb
->Children
) {
1199 IpInstance
= NET_LIST_USER_STRUCT (InstanceEntry
, IP4_PROTOCOL
, Link
);
1202 // Second, time out the assembled packets enqueued on each IP child.
1204 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &IpInstance
->Received
) {
1205 Packet
= NET_LIST_USER_STRUCT (Entry
, NET_BUF
, List
);
1206 Info
= IP4_GET_CLIP_INFO (Packet
);
1208 if ((Info
->Life
> 0) && (--Info
->Life
== 0)) {
1209 RemoveEntryList (Entry
);
1210 NetbufFree (Packet
);
1215 // Third: time out the transmitted packets.
1217 NetMapIterate (&IpInstance
->TxTokens
, Ip4SentPacketTicking
, NULL
);