4 Copyright (c) 2005 - 2007, 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
);
270 // Find the point to insert the packet: before the first
271 // fragment with THIS.Start < CUR.Start. the previous one
272 // has PREV.Start <= THIS.Start < CUR.Start.
274 Head
= &Assemble
->Fragments
;
276 NET_LIST_FOR_EACH (Cur
, Head
) {
277 Fragment
= NET_LIST_USER_STRUCT (Cur
, NET_BUF
, List
);
279 if (This
->Start
< IP4_GET_CLIP_INFO (Fragment
)->Start
) {
285 // Check whether the current fragment overlaps with the previous one.
286 // It holds that: PREV.Start <= THIS.Start < THIS.End. Only need to
287 // check whether THIS.Start < PREV.End for overlap. If two fragments
288 // overlaps, trim the overlapped part off THIS fragment.
290 if ((Prev
= Cur
->ForwardLink
) != Head
) {
291 Fragment
= NET_LIST_USER_STRUCT (Prev
, NET_BUF
, List
);
292 Node
= IP4_GET_CLIP_INFO (Fragment
);
294 if (This
->Start
< Node
->End
) {
295 if (This
->End
<= Node
->End
) {
300 Ip4TrimPacket (Packet
, Node
->End
, This
->End
);
305 // Insert the fragment into the packet. The fragment may be removed
306 // from the list by the following checks.
308 NetListInsertBefore (Cur
, &Packet
->List
);
311 // Check the packets after the insert point. It holds that:
312 // THIS.Start <= NODE.Start < NODE.End. The equality holds
313 // if PREV and NEXT are continuous. THIS fragment may fill
314 // several holes. Remove the completely overlapped fragments
316 while (Cur
!= Head
) {
317 Fragment
= NET_LIST_USER_STRUCT (Cur
, NET_BUF
, List
);
318 Node
= IP4_GET_CLIP_INFO (Fragment
);
321 // Remove fragments completely overlapped by this fragment
323 if (Node
->End
<= This
->End
) {
324 Cur
= Cur
->ForwardLink
;
326 RemoveEntryList (&Fragment
->List
);
327 Assemble
->CurLen
-= Node
->Length
;
329 NetbufFree (Fragment
);
334 // The conditions are: THIS.Start <= NODE.Start, and THIS.End <
335 // NODE.End. Two fragments overlaps if NODE.Start < THIS.End.
336 // If two fragments start at the same offset, remove THIS fragment
337 // because ((THIS.Start == NODE.Start) && (THIS.End < NODE.End)).
339 if (Node
->Start
< This
->End
) {
340 if (This
->Start
== Node
->Start
) {
341 RemoveEntryList (&Packet
->List
);
345 Ip4TrimPacket (Packet
, This
->Start
, Node
->Start
);
352 // Update the assemble info: increase the current length. If it is
353 // the frist fragment, update the packet's IP head and per packet
354 // info. If it is the last fragment, update the total length.
356 Assemble
->CurLen
+= This
->Length
;
358 if (This
->Start
== 0) {
360 // Once the first fragment is enqueued, it can't be removed
361 // from the fragment list. So, Assemble->Head always point
362 // to valid memory area.
364 ASSERT (Assemble
->Head
== NULL
);
366 Assemble
->Head
= IpHead
;
367 Assemble
->Info
= IP4_GET_CLIP_INFO (Packet
);
371 // Don't update the length more than once.
373 if (IP4_LAST_FRAGMENT (IpHead
->Fragment
) && (Assemble
->TotalLen
== 0)) {
374 Assemble
->TotalLen
= This
->End
;
378 // Deliver the whole packet if all the fragments received.
379 // All fragments received if:
380 // 1. received the last one, so, the total length is know
381 // 2. received all the data. If the last fragment on the
382 // queue ends at the total length, all data is received.
384 if ((Assemble
->TotalLen
!= 0) && (Assemble
->CurLen
>= Assemble
->TotalLen
)) {
386 RemoveEntryList (&Assemble
->Link
);
389 // If the packet is properly formated, the last fragment's End
390 // equals to the packet's total length. Otherwise, the packet
391 // is a fake, drop it now.
393 Fragment
= NET_LIST_USER_STRUCT (Head
->BackLink
, NET_BUF
, List
);
395 if (IP4_GET_CLIP_INFO (Fragment
)->End
!= Assemble
->TotalLen
) {
396 Ip4FreeAssembleEntry (Assemble
);
401 // Wrap the packet in a net buffer then deliver it up
403 NewPacket
= NetbufFromBufList (
404 &Assemble
->Fragments
,
411 if (NewPacket
== NULL
) {
412 Ip4FreeAssembleEntry (Assemble
);
416 NewPacket
->Ip
= Assemble
->Head
;
417 CopyMem (IP4_GET_CLIP_INFO (NewPacket
), Assemble
->Info
, sizeof (*IP4_GET_CLIP_INFO (NewPacket
)));
430 The IP4 input routine. It is called by the IP4_INTERFACE when a
431 IP4 fragment is received from MNP.
433 @param[in] Ip4Instance The IP4 child that request the receive, most like
435 @param[in] Packet The IP4 packet received.
436 @param[in] IoStatus The return status of receive request.
437 @param[in] Flag The link layer flag for the packet received, such
439 @param[in] Context The IP4 service instance that own the MNP.
444 IN IP4_PROTOCOL
*Ip4Instance
,
446 IN EFI_STATUS IoStatus
,
459 IpSb
= (IP4_SERVICE
*) Context
;
461 if (EFI_ERROR (IoStatus
) || (IpSb
->State
== IP4_SERVICE_DESTORY
)) {
466 // Check that the IP4 header is correctly formatted
468 if (Packet
->TotalSize
< IP4_MIN_HEADLEN
) {
472 Head
= (IP4_HEAD
*) NetbufGetByte (Packet
, 0, NULL
);
473 HeadLen
= (Head
->HeadLen
<< 2);
474 TotalLen
= NTOHS (Head
->TotalLen
);
477 // Mnp may deliver frame trailer sequence up, trim it off.
479 if (TotalLen
< Packet
->TotalSize
) {
480 NetbufTrim (Packet
, Packet
->TotalSize
- TotalLen
, FALSE
);
483 if ((Head
->Ver
!= 4) || (HeadLen
< IP4_MIN_HEADLEN
) ||
484 (TotalLen
< HeadLen
) || (TotalLen
!= Packet
->TotalSize
)) {
489 // Some OS may send IP packets without checksum.
491 Checksum
= (UINT16
) (~NetblockChecksum ((UINT8
*) Head
, HeadLen
));
493 if ((Head
->Checksum
!= 0) && (Checksum
!= 0)) {
498 // Convert the IP header to host byte order, then get the per packet info.
500 Packet
->Ip
= Ip4NtohHead (Head
);
502 Info
= IP4_GET_CLIP_INFO (Packet
);
503 Info
->LinkFlag
= Flag
;
504 Info
->CastType
= Ip4GetHostCast (IpSb
, Head
->Dst
, Head
->Src
);
505 Info
->Start
= (Head
->Fragment
& IP4_HEAD_OFFSET_MASK
) << 3;
506 Info
->Length
= Head
->TotalLen
- HeadLen
;
507 Info
->End
= Info
->Start
+ Info
->Length
;
508 Info
->Status
= EFI_SUCCESS
;
511 // The packet is destinated to us if the CastType is non-zero.
513 if ((Info
->CastType
== 0) || (Info
->End
> IP4_MAX_PACKET_SIZE
)) {
518 // Validate the options. Don't call the Ip4OptionIsValid if
519 // there is no option to save some CPU process.
521 OptionLen
= HeadLen
- IP4_MIN_HEADLEN
;
523 if ((OptionLen
> 0) && !Ip4OptionIsValid ((UINT8
*) (Head
+ 1), OptionLen
, TRUE
)) {
528 // Trim the head off, after this point, the packet is headless.
529 // and Packet->TotalLen == Info->Length.
531 NetbufTrim (Packet
, HeadLen
, TRUE
);
534 // Reassemble the packet if this is a fragment. The packet is a
535 // fragment if its head has MF (more fragment) set, or it starts
538 if (((Head
->Fragment
& IP4_HEAD_MF_MASK
) != 0) || (Info
->Start
!= 0)) {
540 // Drop the fragment if DF is set but it is fragmented. Gateway
541 // need to send a type 4 destination unreache ICMP message here.
543 if ((Head
->Fragment
& IP4_HEAD_DF_MASK
) != 0) {
548 // The length of all but the last fragments is in the unit of 8 bytes.
550 if (((Head
->Fragment
& IP4_HEAD_MF_MASK
) != 0) && (Info
->Length
% 8 != 0)) {
554 Packet
= Ip4Reassemble (&IpSb
->Assemble
, Packet
);
557 // Packet assembly isn't complete, start receive more packet.
559 if (Packet
== NULL
) {
565 // Packet may have been changed. Head, HeadLen, TotalLen, and
566 // info must be reloaded bofore use. The ownership of the packet
567 // is transfered to the packet process logic.
570 IP4_GET_CLIP_INFO (Packet
)->Status
= EFI_SUCCESS
;
572 switch (Head
->Protocol
) {
574 Ip4IcmpHandle (IpSb
, Head
, Packet
);
578 Ip4IgmpHandle (IpSb
, Head
, Packet
);
582 Ip4Demultiplex (IpSb
, Head
, Packet
);
588 // Dispatch the DPCs queued by the NotifyFunction of the rx token's events
589 // which are signaled with received data.
591 NetLibDispatchDpc ();
594 Ip4ReceiveFrame (IpSb
->DefaultInterface
, NULL
, Ip4AccpetFrame
, IpSb
);
597 if (Packet
!= NULL
) {
606 Check whether this IP child accepts the packet.
608 @param[in] IpInstance The IP child to check
609 @param[in] Head The IP header of the packet
610 @param[in] Packet The data of the packet
612 @retval TRUE If the child wants to receive the packet.
613 @retval FALSE Otherwise.
617 Ip4InstanceFrameAcceptable (
618 IN IP4_PROTOCOL
*IpInstance
,
623 IP4_ICMP_ERROR_HEAD Icmp
;
624 EFI_IP4_CONFIG_DATA
*Config
;
629 Config
= &IpInstance
->ConfigData
;
632 // Dirty trick for the Tiano UEFI network stack implmentation. If
633 // ReceiveTimeout == -1, the receive of the packet for this instance
634 // is disabled. The UEFI spec don't have such capability. We add
635 // this to improve the performance because IP will make a copy of
636 // the received packet for each accepting instance. Some IP instances
637 // used by UDP/TCP only send packets, they don't wants to receive.
639 if (Config
->ReceiveTimeout
== (UINT32
)(-1)) {
643 if (Config
->AcceptPromiscuous
) {
648 // Use protocol from the IP header embedded in the ICMP error
649 // message to filter, instead of ICMP itself. ICMP handle will
650 // can Ip4Demultiplex to deliver ICMP errors.
652 Proto
= Head
->Protocol
;
654 if (Proto
== IP4_PROTO_ICMP
) {
655 NetbufCopy (Packet
, 0, sizeof (Icmp
.Head
), (UINT8
*) &Icmp
.Head
);
657 if (mIcmpClass
[Icmp
.Head
.Type
].IcmpClass
== ICMP_ERROR_MESSAGE
) {
658 if (!Config
->AcceptIcmpErrors
) {
662 NetbufCopy (Packet
, 0, sizeof (Icmp
), (UINT8
*) &Icmp
);
663 Proto
= Icmp
.IpHead
.Protocol
;
668 // Match the protocol
670 if (!Config
->AcceptAnyProtocol
&& (Proto
!= Config
->DefaultProtocol
)) {
675 // Check for broadcast, the caller has computed the packet's
676 // cast type for this child's interface.
678 Info
= IP4_GET_CLIP_INFO (Packet
);
680 if (IP4_IS_BROADCAST (Info
->CastType
)) {
681 return Config
->AcceptBroadcast
;
685 // If it is a multicast packet, check whether we are in the group.
687 if (Info
->CastType
== IP4_MULTICAST
) {
689 // Receive the multicast if the instance wants to receive all packets.
691 if (!IpInstance
->ConfigData
.UseDefaultAddress
&& (IpInstance
->Interface
->Ip
== 0)) {
695 for (Index
= 0; Index
< IpInstance
->GroupCount
; Index
++) {
696 if (IpInstance
->Groups
[Index
] == HTONL (Head
->Dst
)) {
701 return (BOOLEAN
)(Index
< IpInstance
->GroupCount
);
709 Enqueue a shared copy of the packet to the IP4 child if the
710 packet is acceptable to it. Here the data of the packet is
711 shared, but the net buffer isn't.
713 @param[in] IpInstance The IP4 child to enqueue the packet to
714 @param[in] Head The IP header of the received packet
715 @param[in] Packet The data of the received packet
717 @retval EFI_NOT_STARTED The IP child hasn't been configured.
718 @retval EFI_INVALID_PARAMETER The child doesn't want to receive the packet
719 @retval EFI_OUT_OF_RESOURCES Failed to allocate some resource
720 @retval EFI_SUCCESS A shared copy the packet is enqueued to the child.
724 Ip4InstanceEnquePacket (
725 IN IP4_PROTOCOL
*IpInstance
,
734 // Check whether the packet is acceptable to this instance.
736 if (IpInstance
->State
!= IP4_STATE_CONFIGED
) {
737 return EFI_NOT_STARTED
;
740 if (!Ip4InstanceFrameAcceptable (IpInstance
, Head
, Packet
)) {
741 return EFI_INVALID_PARAMETER
;
745 // Enque a shared copy of the packet.
747 Clone
= NetbufClone (Packet
);
750 return EFI_OUT_OF_RESOURCES
;
754 // Set the receive time out for the assembled packet. If it expires,
755 // packet will be removed from the queue.
757 Info
= IP4_GET_CLIP_INFO (Clone
);
758 Info
->Life
= IP4_US_TO_SEC (IpInstance
->ConfigData
.ReceiveTimeout
);
760 InsertTailList (&IpInstance
->Received
, &Clone
->List
);
766 The signal handle of IP4's recycle event. It is called back
767 when the upper layer release the packet.
769 @param Event The IP4's recycle event.
770 @param Context The context of the handle, which is a
781 IP4_RXDATA_WRAP
*Wrap
;
783 Wrap
= (IP4_RXDATA_WRAP
*) Context
;
785 EfiAcquireLockOrFail (&Wrap
->IpInstance
->RecycleLock
);
786 RemoveEntryList (&Wrap
->Link
);
787 EfiReleaseLock (&Wrap
->IpInstance
->RecycleLock
);
789 ASSERT (!NET_BUF_SHARED (Wrap
->Packet
));
790 NetbufFree (Wrap
->Packet
);
792 gBS
->CloseEvent (Wrap
->RxData
.RecycleSignal
);
793 gBS
->FreePool (Wrap
);
798 Wrap the received packet to a IP4_RXDATA_WRAP, which will be
799 delivered to the upper layer. Each IP4 child that accepts the
800 packet will get a not-shared copy of the packet which is wrapped
801 in the IP4_RXDATA_WRAP. The IP4_RXDATA_WRAP->RxData is passed
802 to the upper layer. Upper layer will signal the recycle event in
803 it when it is done with the packet.
805 @param[in] IpInstance The IP4 child to receive the packet
806 @param[in] Packet The packet to deliver up.
808 @retval Wrap if warp the packet succeed.
809 @retval NULL failed to wrap the packet .
814 IN IP4_PROTOCOL
*IpInstance
,
818 IP4_RXDATA_WRAP
*Wrap
;
819 EFI_IP4_RECEIVE_DATA
*RxData
;
822 Wrap
= AllocatePool (IP4_RXDATA_WRAP_SIZE (Packet
->BlockOpNum
));
828 InitializeListHead (&Wrap
->Link
);
830 Wrap
->IpInstance
= IpInstance
;
831 Wrap
->Packet
= Packet
;
832 RxData
= &Wrap
->RxData
;
834 ZeroMem (&RxData
->TimeStamp
, sizeof (EFI_TIME
));
836 Status
= gBS
->CreateEvent (
841 &RxData
->RecycleSignal
844 if (EFI_ERROR (Status
)) {
845 gBS
->FreePool (Wrap
);
849 ASSERT (Packet
->Ip
!= NULL
);
852 // The application expects a network byte order header.
854 RxData
->HeaderLength
= (Packet
->Ip
->HeadLen
<< 2);
855 RxData
->Header
= (EFI_IP4_HEADER
*) Ip4NtohHead (Packet
->Ip
);
857 RxData
->OptionsLength
= RxData
->HeaderLength
- IP4_MIN_HEADLEN
;
858 RxData
->Options
= NULL
;
860 if (RxData
->OptionsLength
!= 0) {
861 RxData
->Options
= (VOID
*) (RxData
->Header
+ 1);
864 RxData
->DataLength
= Packet
->TotalSize
;
867 // Build the fragment table to be delivered up.
869 RxData
->FragmentCount
= Packet
->BlockOpNum
;
870 NetbufBuildExt (Packet
, (NET_FRAGMENT
*) RxData
->FragmentTable
, &RxData
->FragmentCount
);
877 Deliver the received packets to upper layer if there are both received
878 requests and enqueued packets. If the enqueued packet is shared, it will
879 duplicate it to a non-shared packet, release the shared packet, then
880 deliver the non-shared packet up.
882 @param[in] IpInstance The IP child to deliver the packet up.
884 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources to deliver the
886 @retval EFI_SUCCESS All the enqueued packets that can be delivered
891 Ip4InstanceDeliverPacket (
892 IN IP4_PROTOCOL
*IpInstance
895 EFI_IP4_COMPLETION_TOKEN
*Token
;
896 IP4_RXDATA_WRAP
*Wrap
;
902 // Deliver a packet if there are both a packet and a receive token.
904 while (!IsListEmpty (&IpInstance
->Received
) &&
905 !NetMapIsEmpty (&IpInstance
->RxTokens
)) {
907 Packet
= NET_LIST_HEAD (&IpInstance
->Received
, NET_BUF
, List
);
909 if (!NET_BUF_SHARED (Packet
)) {
911 // If this is the only instance that wants the packet, wrap it up.
913 Wrap
= Ip4WrapRxData (IpInstance
, Packet
);
916 return EFI_OUT_OF_RESOURCES
;
919 RemoveEntryList (&Packet
->List
);
923 // Create a duplicated packet if this packet is shared
925 Dup
= NetbufDuplicate (Packet
, NULL
, IP4_MAX_HEADLEN
);
928 return EFI_OUT_OF_RESOURCES
;
932 // Copy the IP head over. The packet to deliver up is
933 // headless. Trim the head off after copy. The IP head
934 // may be not continuous before the data.
936 Head
= NetbufAllocSpace (Dup
, IP4_MAX_HEADLEN
, NET_BUF_HEAD
);
937 Dup
->Ip
= (IP4_HEAD
*) Head
;
939 CopyMem (Head
, Packet
->Ip
, Packet
->Ip
->HeadLen
<< 2);
940 NetbufTrim (Dup
, IP4_MAX_HEADLEN
, TRUE
);
942 Wrap
= Ip4WrapRxData (IpInstance
, Dup
);
946 return EFI_OUT_OF_RESOURCES
;
949 RemoveEntryList (&Packet
->List
);
956 // Insert it into the delivered packet, then get a user's
957 // receive token, pass the wrapped packet up.
959 EfiAcquireLockOrFail (&IpInstance
->RecycleLock
);
960 InsertHeadList (&IpInstance
->Delivered
, &Wrap
->Link
);
961 EfiReleaseLock (&IpInstance
->RecycleLock
);
963 Token
= NetMapRemoveHead (&IpInstance
->RxTokens
, NULL
);
964 Token
->Status
= IP4_GET_CLIP_INFO (Packet
)->Status
;
965 Token
->Packet
.RxData
= &Wrap
->RxData
;
967 gBS
->SignalEvent (Token
->Event
);
975 Enqueue a received packet to all the IP children that share
978 @param[in] IpSb The IP4 service instance that receive the packet
979 @param[in] Head The header of the received packet
980 @param[in] Packet The data of the received packet
981 @param[in] IpIf The interface to enqueue the packet to
983 @return The number of the IP4 children that accepts the packet
987 Ip4InterfaceEnquePacket (
988 IN IP4_SERVICE
*IpSb
,
991 IN IP4_INTERFACE
*IpIf
994 IP4_PROTOCOL
*IpInstance
;
1002 // First, check that the packet is acceptable to this interface
1003 // and find the local cast type for the interface. A packet sent
1004 // to say 192.168.1.1 should NOT be delliever to 10.0.0.1 unless
1005 // promiscuous receiving.
1008 Info
= IP4_GET_CLIP_INFO (Packet
);
1010 if ((Info
->CastType
== IP4_MULTICAST
) || (Info
->CastType
== IP4_LOCAL_BROADCAST
)) {
1012 // If the CastType is multicast, don't need to filter against
1013 // the group address here, Ip4InstanceFrameAcceptable will do
1016 LocalType
= Info
->CastType
;
1020 // Check the destination againist local IP. If the station
1021 // address is 0.0.0.0, it means receiving all the IP destined
1022 // to local non-zero IP. Otherwise, it is necessary to compare
1023 // the destination to the interface's IP address.
1025 if (IpIf
->Ip
== IP4_ALLZERO_ADDRESS
) {
1026 LocalType
= IP4_LOCAL_HOST
;
1029 LocalType
= Ip4GetNetCast (Head
->Dst
, IpIf
);
1031 if ((LocalType
== 0) && IpIf
->PromiscRecv
) {
1032 LocalType
= IP4_PROMISCUOUS
;
1037 if (LocalType
== 0) {
1042 // Iterate through the ip instances on the interface, enqueue
1043 // the packet if filter passed. Save the original cast type,
1044 // and pass the local cast type to the IP children on the
1045 // interface. The global cast type will be restored later.
1047 SavedType
= Info
->CastType
;
1048 Info
->CastType
= LocalType
;
1052 NET_LIST_FOR_EACH (Entry
, &IpIf
->IpInstances
) {
1053 IpInstance
= NET_LIST_USER_STRUCT (Entry
, IP4_PROTOCOL
, AddrLink
);
1054 NET_CHECK_SIGNATURE (IpInstance
, IP4_PROTOCOL_SIGNATURE
);
1056 if (Ip4InstanceEnquePacket (IpInstance
, Head
, Packet
) == EFI_SUCCESS
) {
1061 Info
->CastType
= SavedType
;
1067 Deliver the packet for each IP4 child on the interface.
1069 @param[in] IpSb The IP4 service instance that received the packet
1070 @param[in] IpIf The IP4 interface to deliver the packet.
1072 @retval EFI_SUCCESS It always returns EFI_SUCCESS now
1076 Ip4InterfaceDeliverPacket (
1077 IN IP4_SERVICE
*IpSb
,
1078 IN IP4_INTERFACE
*IpIf
1081 IP4_PROTOCOL
*Ip4Instance
;
1084 NET_LIST_FOR_EACH (Entry
, &IpIf
->IpInstances
) {
1085 Ip4Instance
= NET_LIST_USER_STRUCT (Entry
, IP4_PROTOCOL
, AddrLink
);
1086 Ip4InstanceDeliverPacket (Ip4Instance
);
1094 Demultiple the packet. the packet delivery is processed in two
1095 passes. The first pass will enque a shared copy of the packet
1096 to each IP4 child that accepts the packet. The second pass will
1097 deliver a non-shared copy of the packet to each IP4 child that
1098 has pending receive requests. Data is copied if more than one
1099 child wants to consume the packet because each IP child needs
1100 its own copy of the packet to make changes.
1102 @param[in] IpSb The IP4 service instance that received the packet
1103 @param[in] Head The header of the received packet
1104 @param[in] Packet The data of the received packet
1106 @retval EFI_NOT_FOUND No IP child accepts the packet
1107 @retval EFI_SUCCESS The packet is enqueued or delivered to some IP
1113 IN IP4_SERVICE
*IpSb
,
1119 IP4_INTERFACE
*IpIf
;
1123 // Two pass delivery: first, enque a shared copy of the packet
1124 // to each instance that accept the packet.
1128 NET_LIST_FOR_EACH (Entry
, &IpSb
->Interfaces
) {
1129 IpIf
= NET_LIST_USER_STRUCT (Entry
, IP4_INTERFACE
, Link
);
1131 if (IpIf
->Configured
) {
1132 Enqueued
+= Ip4InterfaceEnquePacket (IpSb
, Head
, Packet
, IpIf
);
1137 // Second: deliver a duplicate of the packet to each instance.
1138 // Release the local reference first, so that the last instance
1139 // getting the packet will not copy the data.
1141 NetbufFree (Packet
);
1143 if (Enqueued
== 0) {
1144 return EFI_NOT_FOUND
;
1147 NET_LIST_FOR_EACH (Entry
, &IpSb
->Interfaces
) {
1148 IpIf
= NET_LIST_USER_STRUCT (Entry
, IP4_INTERFACE
, Link
);
1150 if (IpIf
->Configured
) {
1151 Ip4InterfaceDeliverPacket (IpSb
, IpIf
);
1160 Timeout the fragment and enqueued packets.
1162 @param[in] IpSb The IP4 service instance to timeout
1166 Ip4PacketTimerTicking (
1167 IN IP4_SERVICE
*IpSb
1170 LIST_ENTRY
*InstanceEntry
;
1173 IP4_PROTOCOL
*IpInstance
;
1174 IP4_ASSEMBLE_ENTRY
*Assemble
;
1176 IP4_CLIP_INFO
*Info
;
1180 // First, time out the fragments. The packet's life is counting down
1181 // once the first-arrived fragment was received.
1183 for (Index
= 0; Index
< IP4_ASSEMLE_HASH_SIZE
; Index
++) {
1184 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &IpSb
->Assemble
.Bucket
[Index
]) {
1185 Assemble
= NET_LIST_USER_STRUCT (Entry
, IP4_ASSEMBLE_ENTRY
, Link
);
1187 if ((Assemble
->Life
> 0) && (--Assemble
->Life
== 0)) {
1188 RemoveEntryList (Entry
);
1189 Ip4FreeAssembleEntry (Assemble
);
1194 NET_LIST_FOR_EACH (InstanceEntry
, &IpSb
->Children
) {
1195 IpInstance
= NET_LIST_USER_STRUCT (InstanceEntry
, IP4_PROTOCOL
, Link
);
1198 // Second, time out the assembled packets enqueued on each IP child.
1200 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &IpInstance
->Received
) {
1201 Packet
= NET_LIST_USER_STRUCT (Entry
, NET_BUF
, List
);
1202 Info
= IP4_GET_CLIP_INFO (Packet
);
1204 if ((Info
->Life
> 0) && (--Info
->Life
== 0)) {
1205 RemoveEntryList (Entry
);
1206 NetbufFree (Packet
);
1211 // Third: time out the transmitted packets.
1213 NetMapIterate (&IpInstance
->TxTokens
, Ip4SentPacketTicking
, NULL
);