3 Copyright (c) 2005 - 2007, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
27 Create an empty assemble entry for the packet identified by
28 (Dst, Src, Id, Protocol). The default life for the packet is
31 @param Dst The destination address
32 @param Src The source address
33 @param Id The ID field in IP header
34 @param Protocol The protocol field in IP header
36 @return NULL if failed to allocate memory for the entry, otherwise
37 @return the point to just created reassemble entry.
41 Ip4CreateAssembleEntry (
49 IP4_ASSEMBLE_ENTRY
*Assemble
;
51 Assemble
= AllocatePool (sizeof (IP4_ASSEMBLE_ENTRY
));
53 if (Assemble
== NULL
) {
57 InitializeListHead (&Assemble
->Link
);
58 InitializeListHead (&Assemble
->Fragments
);
63 Assemble
->Protocol
= Protocol
;
64 Assemble
->TotalLen
= 0;
66 Assemble
->Head
= NULL
;
67 Assemble
->Info
= NULL
;
68 Assemble
->Life
= IP4_FRAGMENT_LIFE
;
75 Release all the fragments of a packet, then free the assemble entry.
77 @param Assemble The assemble entry to free
83 Ip4FreeAssembleEntry (
84 IN IP4_ASSEMBLE_ENTRY
*Assemble
91 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &Assemble
->Fragments
) {
92 Fragment
= NET_LIST_USER_STRUCT (Entry
, NET_BUF
, List
);
94 RemoveEntryList (Entry
);
95 NetbufFree (Fragment
);
98 gBS
->FreePool (Assemble
);
103 Initialize an already allocated assemble table. This is generally
104 the assemble table embedded in the IP4 service instance.
106 @param Table The assemble table to initialize.
112 Ip4InitAssembleTable (
113 IN OUT IP4_ASSEMBLE_TABLE
*Table
118 for (Index
= 0; Index
< IP4_ASSEMLE_HASH_SIZE
; Index
++) {
119 InitializeListHead (&Table
->Bucket
[Index
]);
125 Clean up the assemble table: remove all the fragments
126 and assemble entries.
128 @param Table The assemble table to clean up
134 Ip4CleanAssembleTable (
135 IN IP4_ASSEMBLE_TABLE
*Table
140 IP4_ASSEMBLE_ENTRY
*Assemble
;
143 for (Index
= 0; Index
< IP4_ASSEMLE_HASH_SIZE
; Index
++) {
144 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &Table
->Bucket
[Index
]) {
145 Assemble
= NET_LIST_USER_STRUCT (Entry
, IP4_ASSEMBLE_ENTRY
, Link
);
147 RemoveEntryList (Entry
);
148 Ip4FreeAssembleEntry (Assemble
);
155 Trim the packet to fit in [Start, End), and update the per
158 @param Packet Packet to trim
159 @param Start The sequence of the first byte to fit in
160 @param End One beyond the sequence of last byte to fit in.
167 IN OUT NET_BUF
*Packet
,
175 Info
= IP4_GET_CLIP_INFO (Packet
);
177 ASSERT (Info
->Start
+ Info
->Length
== Info
->End
);
178 ASSERT ((Info
->Start
< End
) && (Start
< Info
->End
));
180 if (Info
->Start
< Start
) {
181 Len
= Start
- Info
->Start
;
183 NetbufTrim (Packet
, (UINT32
) Len
, NET_BUF_HEAD
);
188 if (End
< Info
->End
) {
189 Len
= End
- Info
->End
;
191 NetbufTrim (Packet
, (UINT32
) Len
, NET_BUF_TAIL
);
199 Release all the fragments of the packet. This is the callback for
200 the assembled packet's OnFree. It will free the assemble entry,
201 which in turn will free all the fragments of the packet.
203 @param Arg The assemble entry to free
213 Ip4FreeAssembleEntry ((IP4_ASSEMBLE_ENTRY
*) Arg
);
218 Reassemble the IP fragments. If all the fragments of the packet
219 have been received, it will wrap the packet in a net buffer then
220 return it to caller. If the packet can't be assembled, NULL is
223 @param Table The assemble table used. New assemble entry will be created
224 if the Packet is from a new chain of fragments.
225 @param Packet The fragment to assemble. It might be freed if the fragment
226 can't be re-assembled.
228 @return NULL if the packet can't be reassemble. The point to just assembled
229 packet if all the fragments of the packet have arrived.
234 IN OUT IP4_ASSEMBLE_TABLE
*Table
,
235 IN OUT NET_BUF
*Packet
241 IP4_ASSEMBLE_ENTRY
*Assemble
;
250 This
= IP4_GET_CLIP_INFO (Packet
);
252 ASSERT (IpHead
!= NULL
);
255 // First: find the related assemble entry
258 Index
= IP4_ASSEMBLE_HASH (IpHead
->Dst
, IpHead
->Src
, IpHead
->Id
, IpHead
->Protocol
);
260 NET_LIST_FOR_EACH (Cur
, &Table
->Bucket
[Index
]) {
261 Assemble
= NET_LIST_USER_STRUCT (Cur
, IP4_ASSEMBLE_ENTRY
, Link
);
263 if ((Assemble
->Dst
== IpHead
->Dst
) && (Assemble
->Src
== IpHead
->Src
) &&
264 (Assemble
->Id
== IpHead
->Id
) && (Assemble
->Protocol
== IpHead
->Protocol
)) {
270 // Create a new assemble entry if no assemble entry is related to this packet
272 if (Cur
== &Table
->Bucket
[Index
]) {
273 Assemble
= Ip4CreateAssembleEntry (
280 if (Assemble
== NULL
) {
284 InsertHeadList (&Table
->Bucket
[Index
], &Assemble
->Link
);
288 // Find the point to insert the packet: before the first
289 // fragment with THIS.Start < CUR.Start. the previous one
290 // has PREV.Start <= THIS.Start < CUR.Start.
292 Head
= &Assemble
->Fragments
;
294 NET_LIST_FOR_EACH (Cur
, Head
) {
295 Fragment
= NET_LIST_USER_STRUCT (Cur
, NET_BUF
, List
);
297 if (This
->Start
< IP4_GET_CLIP_INFO (Fragment
)->Start
) {
303 // Check whether the current fragment overlaps with the previous one.
304 // It holds that: PREV.Start <= THIS.Start < THIS.End. Only need to
305 // check whether THIS.Start < PREV.End for overlap. If two fragments
306 // overlaps, trim the overlapped part off THIS fragment.
308 if ((Prev
= Cur
->ForwardLink
) != Head
) {
309 Fragment
= NET_LIST_USER_STRUCT (Prev
, NET_BUF
, List
);
310 Node
= IP4_GET_CLIP_INFO (Fragment
);
312 if (This
->Start
< Node
->End
) {
313 if (This
->End
<= Node
->End
) {
318 Ip4TrimPacket (Packet
, Node
->End
, This
->End
);
323 // Insert the fragment into the packet. The fragment may be removed
324 // from the list by the following checks.
326 NetListInsertBefore (Cur
, &Packet
->List
);
329 // Check the packets after the insert point. It holds that:
330 // THIS.Start <= NODE.Start < NODE.End. The equality holds
331 // if PREV and NEXT are continuous. THIS fragment may fill
332 // several holes. Remove the completely overlapped fragments
334 while (Cur
!= Head
) {
335 Fragment
= NET_LIST_USER_STRUCT (Cur
, NET_BUF
, List
);
336 Node
= IP4_GET_CLIP_INFO (Fragment
);
339 // Remove fragments completely overlapped by this fragment
341 if (Node
->End
<= This
->End
) {
342 Cur
= Cur
->ForwardLink
;
344 RemoveEntryList (&Fragment
->List
);
345 Assemble
->CurLen
-= Node
->Length
;
347 NetbufFree (Fragment
);
352 // The conditions are: THIS.Start <= NODE.Start, and THIS.End <
353 // NODE.End. Two fragments overlaps if NODE.Start < THIS.End.
354 // If two fragments start at the same offset, remove THIS fragment
355 // because ((THIS.Start == NODE.Start) && (THIS.End < NODE.End)).
357 if (Node
->Start
< This
->End
) {
358 if (This
->Start
== Node
->Start
) {
359 RemoveEntryList (&Packet
->List
);
363 Ip4TrimPacket (Packet
, This
->Start
, Node
->Start
);
370 // Update the assemble info: increase the current length. If it is
371 // the frist fragment, update the packet's IP head and per packet
372 // info. If it is the last fragment, update the total length.
374 Assemble
->CurLen
+= This
->Length
;
376 if (This
->Start
== 0) {
378 // Once the first fragment is enqueued, it can't be removed
379 // from the fragment list. So, Assemble->Head always point
380 // to valid memory area.
382 ASSERT (Assemble
->Head
== NULL
);
384 Assemble
->Head
= IpHead
;
385 Assemble
->Info
= IP4_GET_CLIP_INFO (Packet
);
389 // Don't update the length more than once.
391 if (IP4_LAST_FRAGMENT (IpHead
->Fragment
) && (Assemble
->TotalLen
== 0)) {
392 Assemble
->TotalLen
= This
->End
;
396 // Deliver the whole packet if all the fragments received.
397 // All fragments received if:
398 // 1. received the last one, so, the total length is know
399 // 2. received all the data. If the last fragment on the
400 // queue ends at the total length, all data is received.
402 if ((Assemble
->TotalLen
!= 0) && (Assemble
->CurLen
>= Assemble
->TotalLen
)) {
404 RemoveEntryList (&Assemble
->Link
);
407 // If the packet is properly formated, the last fragment's End
408 // equals to the packet's total length. Otherwise, the packet
409 // is a fake, drop it now.
411 Fragment
= NET_LIST_USER_STRUCT (Head
->BackLink
, NET_BUF
, List
);
413 if (IP4_GET_CLIP_INFO (Fragment
)->End
!= Assemble
->TotalLen
) {
414 Ip4FreeAssembleEntry (Assemble
);
419 // Wrap the packet in a net buffer then deliver it up
421 NewPacket
= NetbufFromBufList (
422 &Assemble
->Fragments
,
429 if (NewPacket
== NULL
) {
430 Ip4FreeAssembleEntry (Assemble
);
434 NewPacket
->Ip
= Assemble
->Head
;
435 CopyMem (IP4_GET_CLIP_INFO (NewPacket
), Assemble
->Info
, sizeof (*IP4_GET_CLIP_INFO (NewPacket
)));
448 The IP4 input routine. It is called by the IP4_INTERFACE when a
449 IP4 fragment is received from MNP.
451 @param Ip4Instance The IP4 child that request the receive, most like
453 @param Packet The IP4 packet received.
454 @param IoStatus The return status of receive request.
455 @param Flag The link layer flag for the packet received, such
457 @param Context The IP4 service instance that own the MNP.
464 IN IP4_PROTOCOL
*Ip4Instance
,
466 IN EFI_STATUS IoStatus
,
479 IpSb
= (IP4_SERVICE
*) Context
;
481 if (EFI_ERROR (IoStatus
) || (IpSb
->State
== IP4_SERVICE_DESTORY
)) {
486 // Check that the IP4 header is correctly formatted
488 if (Packet
->TotalSize
< IP4_MIN_HEADLEN
) {
492 Head
= (IP4_HEAD
*) NetbufGetByte (Packet
, 0, NULL
);
493 HeadLen
= (Head
->HeadLen
<< 2);
494 TotalLen
= NTOHS (Head
->TotalLen
);
497 // Mnp may deliver frame trailer sequence up, trim it off.
499 if (TotalLen
< Packet
->TotalSize
) {
500 NetbufTrim (Packet
, Packet
->TotalSize
- TotalLen
, FALSE
);
503 if ((Head
->Ver
!= 4) || (HeadLen
< IP4_MIN_HEADLEN
) ||
504 (TotalLen
< HeadLen
) || (TotalLen
!= Packet
->TotalSize
)) {
509 // Some OS may send IP packets without checksum.
511 Checksum
= (UINT16
) (~NetblockChecksum ((UINT8
*) Head
, HeadLen
));
513 if ((Head
->Checksum
!= 0) && (Checksum
!= 0)) {
518 // Convert the IP header to host byte order, then get the per packet info.
520 Packet
->Ip
= Ip4NtohHead (Head
);
522 Info
= IP4_GET_CLIP_INFO (Packet
);
523 Info
->LinkFlag
= Flag
;
524 Info
->CastType
= Ip4GetHostCast (IpSb
, Head
->Dst
, Head
->Src
);
525 Info
->Start
= (Head
->Fragment
& IP4_HEAD_OFFSET_MASK
) << 3;
526 Info
->Length
= Head
->TotalLen
- HeadLen
;
527 Info
->End
= Info
->Start
+ Info
->Length
;
528 Info
->Status
= EFI_SUCCESS
;
531 // The packet is destinated to us if the CastType is non-zero.
533 if ((Info
->CastType
== 0) || (Info
->End
> IP4_MAX_PACKET_SIZE
)) {
538 // Validate the options. Don't call the Ip4OptionIsValid if
539 // there is no option to save some CPU process.
541 OptionLen
= HeadLen
- IP4_MIN_HEADLEN
;
543 if ((OptionLen
> 0) && !Ip4OptionIsValid ((UINT8
*) (Head
+ 1), OptionLen
, TRUE
)) {
548 // Trim the head off, after this point, the packet is headless.
549 // and Packet->TotalLen == Info->Length.
551 NetbufTrim (Packet
, HeadLen
, TRUE
);
554 // Reassemble the packet if this is a fragment. The packet is a
555 // fragment if its head has MF (more fragment) set, or it starts
558 if ((Head
->Fragment
& IP4_HEAD_MF_MASK
) || (Info
->Start
!= 0)) {
560 // Drop the fragment if DF is set but it is fragmented. Gateway
561 // need to send a type 4 destination unreache ICMP message here.
563 if (Head
->Fragment
& IP4_HEAD_DF_MASK
) {
568 // The length of all but the last fragments is in the unit of 8 bytes.
570 if ((Head
->Fragment
& IP4_HEAD_MF_MASK
) && (Info
->Length
% 8 != 0)) {
574 Packet
= Ip4Reassemble (&IpSb
->Assemble
, Packet
);
577 // Packet assembly isn't complete, start receive more packet.
579 if (Packet
== NULL
) {
585 // Packet may have been changed. Head, HeadLen, TotalLen, and
586 // info must be reloaded bofore use. The ownership of the packet
587 // is transfered to the packet process logic.
590 IP4_GET_CLIP_INFO (Packet
)->Status
= EFI_SUCCESS
;
592 switch (Head
->Protocol
) {
594 Ip4IcmpHandle (IpSb
, Head
, Packet
);
598 Ip4IgmpHandle (IpSb
, Head
, Packet
);
602 Ip4Demultiplex (IpSb
, Head
, Packet
);
608 // Dispatch the DPCs queued by the NotifyFunction of the rx token's events
609 // which are signaled with received data.
611 NetLibDispatchDpc ();
614 Ip4ReceiveFrame (IpSb
->DefaultInterface
, NULL
, Ip4AccpetFrame
, IpSb
);
617 if (Packet
!= NULL
) {
626 Check whether this IP child accepts the packet.
628 @param IpInstance The IP child to check
629 @param Head The IP header of the packet
630 @param Packet The data of the packet
632 @retval TRUE If the child wants to receive the packet.
633 @retval FALSE Otherwise.
637 Ip4InstanceFrameAcceptable (
638 IN IP4_PROTOCOL
*IpInstance
,
643 IP4_ICMP_ERROR_HEAD Icmp
;
644 EFI_IP4_CONFIG_DATA
*Config
;
649 Config
= &IpInstance
->ConfigData
;
652 // Dirty trick for the Tiano UEFI network stack implmentation. If
653 // ReceiveTimeout == -1, the receive of the packet for this instance
654 // is disabled. The UEFI spec don't have such capability. We add
655 // this to improve the performance because IP will make a copy of
656 // the received packet for each accepting instance. Some IP instances
657 // used by UDP/TCP only send packets, they don't wants to receive.
659 if (Config
->ReceiveTimeout
== (UINT32
)(-1)) {
663 if (Config
->AcceptPromiscuous
) {
668 // Use protocol from the IP header embedded in the ICMP error
669 // message to filter, instead of ICMP itself. ICMP handle will
670 // can Ip4Demultiplex to deliver ICMP errors.
672 Proto
= Head
->Protocol
;
674 if (Proto
== IP4_PROTO_ICMP
) {
675 NetbufCopy (Packet
, 0, sizeof (Icmp
.Head
), (UINT8
*) &Icmp
.Head
);
677 if (mIcmpClass
[Icmp
.Head
.Type
].IcmpClass
== ICMP_ERROR_MESSAGE
) {
678 if (!Config
->AcceptIcmpErrors
) {
682 NetbufCopy (Packet
, 0, sizeof (Icmp
), (UINT8
*) &Icmp
);
683 Proto
= Icmp
.IpHead
.Protocol
;
688 // Match the protocol
690 if (!Config
->AcceptAnyProtocol
&& (Proto
!= Config
->DefaultProtocol
)) {
695 // Check for broadcast, the caller has computed the packet's
696 // cast type for this child's interface.
698 Info
= IP4_GET_CLIP_INFO (Packet
);
700 if (IP4_IS_BROADCAST (Info
->CastType
)) {
701 return Config
->AcceptBroadcast
;
705 // If it is a multicast packet, check whether we are in the group.
707 if (Info
->CastType
== IP4_MULTICAST
) {
709 // Receive the multicast if the instance wants to receive all packets.
711 if (!IpInstance
->ConfigData
.UseDefaultAddress
&& (IpInstance
->Interface
->Ip
== 0)) {
715 for (Index
= 0; Index
< IpInstance
->GroupCount
; Index
++) {
716 if (IpInstance
->Groups
[Index
] == HTONL (Head
->Dst
)) {
721 return (BOOLEAN
)(Index
< IpInstance
->GroupCount
);
729 Enqueue a shared copy of the packet to the IP4 child if the
730 packet is acceptable to it. Here the data of the packet is
731 shared, but the net buffer isn't.
733 @param IpInstance The IP4 child to enqueue the packet to
734 @param Head The IP header of the received packet
735 @param Packet The data of the received packet
737 @retval EFI_NOT_STARTED The IP child hasn't been configured.
738 @retval EFI_INVALID_PARAMETER The child doesn't want to receive the packet
739 @retval EFI_OUT_OF_RESOURCES Failed to allocate some resource
740 @retval EFI_SUCCESS A shared copy the packet is enqueued to the child.
744 Ip4InstanceEnquePacket (
745 IN IP4_PROTOCOL
*IpInstance
,
754 // Check whether the packet is acceptable to this instance.
756 if (IpInstance
->State
!= IP4_STATE_CONFIGED
) {
757 return EFI_NOT_STARTED
;
760 if (!Ip4InstanceFrameAcceptable (IpInstance
, Head
, Packet
)) {
761 return EFI_INVALID_PARAMETER
;
765 // Enque a shared copy of the packet.
767 Clone
= NetbufClone (Packet
);
770 return EFI_OUT_OF_RESOURCES
;
774 // Set the receive time out for the assembled packet. If it expires,
775 // packet will be removed from the queue.
777 Info
= IP4_GET_CLIP_INFO (Clone
);
778 Info
->Life
= IP4_US_TO_SEC (IpInstance
->ConfigData
.ReceiveTimeout
);
780 InsertTailList (&IpInstance
->Received
, &Clone
->List
);
786 The signal handle of IP4's recycle event. It is called back
787 when the upper layer release the packet.
789 @param Event The IP4's recycle event.
790 @param Context The context of the handle, which is a
803 IP4_RXDATA_WRAP
*Wrap
;
805 Wrap
= (IP4_RXDATA_WRAP
*) Context
;
807 EfiAcquireLockOrFail (&Wrap
->IpInstance
->RecycleLock
);
808 RemoveEntryList (&Wrap
->Link
);
809 EfiReleaseLock (&Wrap
->IpInstance
->RecycleLock
);
811 ASSERT (!NET_BUF_SHARED (Wrap
->Packet
));
812 NetbufFree (Wrap
->Packet
);
814 gBS
->CloseEvent (Wrap
->RxData
.RecycleSignal
);
815 gBS
->FreePool (Wrap
);
820 Wrap the received packet to a IP4_RXDATA_WRAP, which will be
821 delivered to the upper layer. Each IP4 child that accepts the
822 packet will get a not-shared copy of the packet which is wrapped
823 in the IP4_RXDATA_WRAP. The IP4_RXDATA_WRAP->RxData is passed
824 to the upper layer. Upper layer will signal the recycle event in
825 it when it is done with the packet.
827 @param IpInstance The IP4 child to receive the packet
828 @param Packet The packet to deliver up.
830 @return NULL if failed to wrap the packet, otherwise the wrapper.
835 IN IP4_PROTOCOL
*IpInstance
,
839 IP4_RXDATA_WRAP
*Wrap
;
840 EFI_IP4_RECEIVE_DATA
*RxData
;
843 Wrap
= AllocatePool (IP4_RXDATA_WRAP_SIZE (Packet
->BlockOpNum
));
849 InitializeListHead (&Wrap
->Link
);
851 Wrap
->IpInstance
= IpInstance
;
852 Wrap
->Packet
= Packet
;
853 RxData
= &Wrap
->RxData
;
855 ZeroMem (&RxData
->TimeStamp
, sizeof (EFI_TIME
));
857 Status
= gBS
->CreateEvent (
862 &RxData
->RecycleSignal
865 if (EFI_ERROR (Status
)) {
866 gBS
->FreePool (Wrap
);
870 ASSERT (Packet
->Ip
!= NULL
);
873 // The application expects a network byte order header.
875 RxData
->HeaderLength
= (Packet
->Ip
->HeadLen
<< 2);
876 RxData
->Header
= (EFI_IP4_HEADER
*) Ip4NtohHead (Packet
->Ip
);
878 RxData
->OptionsLength
= RxData
->HeaderLength
- IP4_MIN_HEADLEN
;
879 RxData
->Options
= NULL
;
881 if (RxData
->OptionsLength
!= 0) {
882 RxData
->Options
= (VOID
*) (RxData
->Header
+ 1);
885 RxData
->DataLength
= Packet
->TotalSize
;
888 // Build the fragment table to be delivered up.
890 RxData
->FragmentCount
= Packet
->BlockOpNum
;
891 NetbufBuildExt (Packet
, (NET_FRAGMENT
*) RxData
->FragmentTable
, &RxData
->FragmentCount
);
898 Deliver the received packets to upper layer if there are both received
899 requests and enqueued packets. If the enqueued packet is shared, it will
900 duplicate it to a non-shared packet, release the shared packet, then
901 deliver the non-shared packet up.
903 @param IpInstance The IP child to deliver the packet up.
905 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources to deliver the
907 @retval EFI_SUCCESS All the enqueued packets that can be delivered
912 Ip4InstanceDeliverPacket (
913 IN IP4_PROTOCOL
*IpInstance
916 EFI_IP4_COMPLETION_TOKEN
*Token
;
917 IP4_RXDATA_WRAP
*Wrap
;
923 // Deliver a packet if there are both a packet and a receive token.
925 while (!IsListEmpty (&IpInstance
->Received
) &&
926 !NetMapIsEmpty (&IpInstance
->RxTokens
)) {
928 Packet
= NET_LIST_HEAD (&IpInstance
->Received
, NET_BUF
, List
);
930 if (!NET_BUF_SHARED (Packet
)) {
932 // If this is the only instance that wants the packet, wrap it up.
934 Wrap
= Ip4WrapRxData (IpInstance
, Packet
);
937 return EFI_OUT_OF_RESOURCES
;
940 RemoveEntryList (&Packet
->List
);
944 // Create a duplicated packet if this packet is shared
946 Dup
= NetbufDuplicate (Packet
, NULL
, IP4_MAX_HEADLEN
);
949 return EFI_OUT_OF_RESOURCES
;
953 // Copy the IP head over. The packet to deliver up is
954 // headless. Trim the head off after copy. The IP head
955 // may be not continuous before the data.
957 Head
= NetbufAllocSpace (Dup
, IP4_MAX_HEADLEN
, NET_BUF_HEAD
);
958 Dup
->Ip
= (IP4_HEAD
*) Head
;
960 CopyMem (Head
, Packet
->Ip
, Packet
->Ip
->HeadLen
<< 2);
961 NetbufTrim (Dup
, IP4_MAX_HEADLEN
, TRUE
);
963 Wrap
= Ip4WrapRxData (IpInstance
, Dup
);
967 return EFI_OUT_OF_RESOURCES
;
970 RemoveEntryList (&Packet
->List
);
977 // Insert it into the delivered packet, then get a user's
978 // receive token, pass the wrapped packet up.
980 EfiAcquireLockOrFail (&IpInstance
->RecycleLock
);
981 InsertHeadList (&IpInstance
->Delivered
, &Wrap
->Link
);
982 EfiReleaseLock (&IpInstance
->RecycleLock
);
984 Token
= NetMapRemoveHead (&IpInstance
->RxTokens
, NULL
);
985 Token
->Status
= IP4_GET_CLIP_INFO (Packet
)->Status
;
986 Token
->Packet
.RxData
= &Wrap
->RxData
;
988 gBS
->SignalEvent (Token
->Event
);
996 Enqueue a received packet to all the IP children that share
999 @param IpSb The IP4 service instance that receive the packet
1000 @param Head The header of the received packet
1001 @param Packet The data of the received packet
1002 @param IpIf The interface to enqueue the packet to
1004 @return The number of the IP4 children that accepts the packet
1008 Ip4InterfaceEnquePacket (
1009 IN IP4_SERVICE
*IpSb
,
1012 IN IP4_INTERFACE
*IpIf
1015 IP4_PROTOCOL
*IpInstance
;
1016 IP4_CLIP_INFO
*Info
;
1023 // First, check that the packet is acceptable to this interface
1024 // and find the local cast type for the interface. A packet sent
1025 // to say 192.168.1.1 should NOT be delliever to 10.0.0.1 unless
1026 // promiscuous receiving.
1029 Info
= IP4_GET_CLIP_INFO (Packet
);
1031 if ((Info
->CastType
== IP4_MULTICAST
) || (Info
->CastType
== IP4_LOCAL_BROADCAST
)) {
1033 // If the CastType is multicast, don't need to filter against
1034 // the group address here, Ip4InstanceFrameAcceptable will do
1037 LocalType
= Info
->CastType
;
1041 // Check the destination againist local IP. If the station
1042 // address is 0.0.0.0, it means receiving all the IP destined
1043 // to local non-zero IP. Otherwise, it is necessary to compare
1044 // the destination to the interface's IP address.
1046 if (IpIf
->Ip
== IP4_ALLZERO_ADDRESS
) {
1047 LocalType
= IP4_LOCAL_HOST
;
1050 LocalType
= Ip4GetNetCast (Head
->Dst
, IpIf
);
1052 if ((LocalType
== 0) && IpIf
->PromiscRecv
) {
1053 LocalType
= IP4_PROMISCUOUS
;
1058 if (LocalType
== 0) {
1063 // Iterate through the ip instances on the interface, enqueue
1064 // the packet if filter passed. Save the original cast type,
1065 // and pass the local cast type to the IP children on the
1066 // interface. The global cast type will be restored later.
1068 SavedType
= Info
->CastType
;
1069 Info
->CastType
= LocalType
;
1073 NET_LIST_FOR_EACH (Entry
, &IpIf
->IpInstances
) {
1074 IpInstance
= NET_LIST_USER_STRUCT (Entry
, IP4_PROTOCOL
, AddrLink
);
1075 NET_CHECK_SIGNATURE (IpInstance
, IP4_PROTOCOL_SIGNATURE
);
1077 if (Ip4InstanceEnquePacket (IpInstance
, Head
, Packet
) == EFI_SUCCESS
) {
1082 Info
->CastType
= SavedType
;
1088 Deliver the packet for each IP4 child on the interface.
1090 @param IpSb The IP4 service instance that received the packet
1091 @param IpIf The IP4 interface to deliver the packet.
1093 @retval EFI_SUCCESS It always returns EFI_SUCCESS now
1097 Ip4InterfaceDeliverPacket (
1098 IN IP4_SERVICE
*IpSb
,
1099 IN IP4_INTERFACE
*IpIf
1102 IP4_PROTOCOL
*Ip4Instance
;
1105 NET_LIST_FOR_EACH (Entry
, &IpIf
->IpInstances
) {
1106 Ip4Instance
= NET_LIST_USER_STRUCT (Entry
, IP4_PROTOCOL
, AddrLink
);
1107 Ip4InstanceDeliverPacket (Ip4Instance
);
1115 Demultiple the packet. the packet delivery is processed in two
1116 passes. The first pass will enque a shared copy of the packet
1117 to each IP4 child that accepts the packet. The second pass will
1118 deliver a non-shared copy of the packet to each IP4 child that
1119 has pending receive requests. Data is copied if more than one
1120 child wants to consume the packet because each IP child needs
1121 its own copy of the packet to make changes.
1123 @param IpSb The IP4 service instance that received the packet
1124 @param Head The header of the received packet
1125 @param Packet The data of the received packet
1127 @retval EFI_NOT_FOUND No IP child accepts the packet
1128 @retval EFI_SUCCESS The packet is enqueued or delivered to some IP
1134 IN IP4_SERVICE
*IpSb
,
1140 IP4_INTERFACE
*IpIf
;
1144 // Two pass delivery: first, enque a shared copy of the packet
1145 // to each instance that accept the packet.
1149 NET_LIST_FOR_EACH (Entry
, &IpSb
->Interfaces
) {
1150 IpIf
= NET_LIST_USER_STRUCT (Entry
, IP4_INTERFACE
, Link
);
1152 if (IpIf
->Configured
) {
1153 Enqueued
+= Ip4InterfaceEnquePacket (IpSb
, Head
, Packet
, IpIf
);
1158 // Second: deliver a duplicate of the packet to each instance.
1159 // Release the local reference first, so that the last instance
1160 // getting the packet will not copy the data.
1162 NetbufFree (Packet
);
1164 if (Enqueued
== 0) {
1165 return EFI_NOT_FOUND
;
1168 NET_LIST_FOR_EACH (Entry
, &IpSb
->Interfaces
) {
1169 IpIf
= NET_LIST_USER_STRUCT (Entry
, IP4_INTERFACE
, Link
);
1171 if (IpIf
->Configured
) {
1172 Ip4InterfaceDeliverPacket (IpSb
, IpIf
);
1181 Timeout the fragment and enqueued packets.
1183 @param IpSb The IP4 service instance to timeout
1189 Ip4PacketTimerTicking (
1190 IN IP4_SERVICE
*IpSb
1193 LIST_ENTRY
*InstanceEntry
;
1196 IP4_PROTOCOL
*IpInstance
;
1197 IP4_ASSEMBLE_ENTRY
*Assemble
;
1199 IP4_CLIP_INFO
*Info
;
1203 // First, time out the fragments. The packet's life is counting down
1204 // once the first-arrived fragment was received.
1206 for (Index
= 0; Index
< IP4_ASSEMLE_HASH_SIZE
; Index
++) {
1207 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &IpSb
->Assemble
.Bucket
[Index
]) {
1208 Assemble
= NET_LIST_USER_STRUCT (Entry
, IP4_ASSEMBLE_ENTRY
, Link
);
1210 if ((Assemble
->Life
> 0) && (--Assemble
->Life
== 0)) {
1211 RemoveEntryList (Entry
);
1212 Ip4FreeAssembleEntry (Assemble
);
1217 NET_LIST_FOR_EACH (InstanceEntry
, &IpSb
->Children
) {
1218 IpInstance
= NET_LIST_USER_STRUCT (InstanceEntry
, IP4_PROTOCOL
, Link
);
1221 // Second, time out the assembled packets enqueued on each IP child.
1223 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &IpInstance
->Received
) {
1224 Packet
= NET_LIST_USER_STRUCT (Entry
, NET_BUF
, List
);
1225 Info
= IP4_GET_CLIP_INFO (Packet
);
1227 if ((Info
->Life
> 0) && (--Info
->Life
== 0)) {
1228 RemoveEntryList (Entry
);
1229 NetbufFree (Packet
);
1234 // Third: time out the transmitted packets.
1236 NetMapIterate (&IpInstance
->TxTokens
, Ip4SentPacketTicking
, NULL
);