]>
git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Network/Ip4Dxe/Ip4Input.c
3 Copyright (c) 2005 - 2006, 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 a 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.
42 Ip4CreateAssembleEntry (
50 IP4_ASSEMBLE_ENTRY
*Assemble
;
52 Assemble
= NetAllocatePool (sizeof (IP4_ASSEMBLE_ENTRY
));
54 if (Assemble
== NULL
) {
58 NetListInit (&Assemble
->Link
);
59 NetListInit (&Assemble
->Fragments
);
64 Assemble
->Protocol
= Protocol
;
65 Assemble
->TotalLen
= 0;
67 Assemble
->Head
= NULL
;
68 Assemble
->Info
= NULL
;
69 Assemble
->Life
= IP4_FRAGMENT_LIFE
;
76 Release all the fragments of a packet, then free the assemble entry
78 @param Assemble The assemble entry to free
85 Ip4FreeAssembleEntry (
86 IN IP4_ASSEMBLE_ENTRY
*Assemble
89 NET_LIST_ENTRY
*Entry
;
93 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &Assemble
->Fragments
) {
94 Fragment
= NET_LIST_USER_STRUCT (Entry
, NET_BUF
, List
);
96 NetListRemoveEntry (Entry
);
97 NetbufFree (Fragment
);
100 NetFreePool (Assemble
);
105 Initialize an already allocated assemble table. This is generally
106 the assemble table embedded in the IP4 service instance.
108 @param Table The assemble table to initialize.
114 Ip4InitAssembleTable (
115 IN IP4_ASSEMBLE_TABLE
*Table
120 for (Index
= 0; Index
< IP4_ASSEMLE_HASH_SIZE
; Index
++) {
121 NetListInit (&Table
->Bucket
[Index
]);
127 Clean up the assemble table: remove all the fragments
128 and assemble entries.
130 @param Table The assemble table to clean up
136 Ip4CleanAssembleTable (
137 IN IP4_ASSEMBLE_TABLE
*Table
140 NET_LIST_ENTRY
*Entry
;
141 NET_LIST_ENTRY
*Next
;
142 IP4_ASSEMBLE_ENTRY
*Assemble
;
145 for (Index
= 0; Index
< IP4_ASSEMLE_HASH_SIZE
; Index
++) {
146 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &Table
->Bucket
[Index
]) {
147 Assemble
= NET_LIST_USER_STRUCT (Entry
, IP4_ASSEMBLE_ENTRY
, Link
);
149 NetListRemoveEntry (Entry
);
150 Ip4FreeAssembleEntry (Assemble
);
157 Trim the packet to fit in [Start, End), and update the per
160 @param Packet Packet to trim
161 @param Start The sequence of the first byte to fit in
162 @param End One beyond the sequence of last byte to fit in.
178 Info
= IP4_GET_CLIP_INFO (Packet
);
180 ASSERT (Info
->Start
+ Info
->Length
== Info
->End
);
181 ASSERT ((Info
->Start
< End
) && (Start
< Info
->End
));
183 if (Info
->Start
< Start
) {
184 Len
= Start
- Info
->Start
;
186 NetbufTrim (Packet
, (UINT32
) Len
, NET_BUF_HEAD
);
191 if (End
< Info
->End
) {
192 Len
= End
- Info
->End
;
194 NetbufTrim (Packet
, (UINT32
) Len
, NET_BUF_TAIL
);
202 Release all the fragments of the packet. This is the callback for
203 the assembled packet's OnFree. It will free the assemble entry,
204 which in turn will free all the fragments of the packet.
206 @param Arg The assemble entry to free
217 Ip4FreeAssembleEntry ((IP4_ASSEMBLE_ENTRY
*) Arg
);
222 Reassemble the IP fragments. If all the fragments of the packet
223 have been received, it will wrap the packet in a net buffer then
224 return it to caller. If the packet can't be assembled, NULL is
227 @param Table The assemble table used.
228 @param Packet The fragment to assemble
230 @return NULL if the packet can't be reassemble. The point to just assembled
231 @return packet if all the fragments of the packet have arrived.
237 IN IP4_ASSEMBLE_TABLE
*Table
,
244 IP4_ASSEMBLE_ENTRY
*Assemble
;
245 NET_LIST_ENTRY
*Head
;
246 NET_LIST_ENTRY
*Prev
;
253 This
= IP4_GET_CLIP_INFO (Packet
);
255 ASSERT (IpHead
!= NULL
);
258 // First: find the related assemble entry
261 Index
= IP4_ASSEMBLE_HASH (IpHead
->Dst
, IpHead
->Src
, IpHead
->Id
, IpHead
->Protocol
);
263 NET_LIST_FOR_EACH (Cur
, &Table
->Bucket
[Index
]) {
264 Assemble
= NET_LIST_USER_STRUCT (Cur
, IP4_ASSEMBLE_ENTRY
, Link
);
266 if ((Assemble
->Dst
== IpHead
->Dst
) && (Assemble
->Src
== IpHead
->Src
) &&
267 (Assemble
->Id
== IpHead
->Id
) && (Assemble
->Protocol
== IpHead
->Protocol
)) {
273 // Create a new assemble entry if no assemble entry is related to this packet
275 if (Cur
== &Table
->Bucket
[Index
]) {
276 Assemble
= Ip4CreateAssembleEntry (
283 if (Assemble
== NULL
) {
287 NetListInsertHead (&Table
->Bucket
[Index
], &Assemble
->Link
);
291 // Find the point to insert the packet: before the first
292 // fragment with THIS.Start < CUR.Start. the previous one
293 // has PREV.Start <= THIS.Start < CUR.Start.
295 Head
= &Assemble
->Fragments
;
297 NET_LIST_FOR_EACH (Cur
, Head
) {
298 Fragment
= NET_LIST_USER_STRUCT (Cur
, NET_BUF
, List
);
300 if (This
->Start
< IP4_GET_CLIP_INFO (Fragment
)->Start
) {
306 // Check whether the current fragment overlaps with the previous one.
307 // It holds that: PREV.Start <= THIS.Start < THIS.End. Only need to
308 // check whether THIS.Start < PREV.End for overlap. If two fragments
309 // overlaps, trim the overlapped part off THIS fragment.
311 if ((Prev
= Cur
->ForwardLink
) != Head
) {
312 Fragment
= NET_LIST_USER_STRUCT (Prev
, NET_BUF
, List
);
313 Node
= IP4_GET_CLIP_INFO (Fragment
);
315 if (This
->Start
< Node
->End
) {
316 if (This
->End
<= Node
->End
) {
321 Ip4TrimPacket (Packet
, Node
->End
, This
->End
);
326 // Insert the fragment into the packet. The fragment may be removed
327 // from the list by the following checks.
329 NetListInsertBefore (Cur
, &Packet
->List
);
332 // Check the packets after the insert point. It holds that:
333 // THIS.Start <= NODE.Start < NODE.End. The equality holds
334 // if PREV and NEXT are continuous. THIS fragment may fill
335 // several holes. Remove the completely overlapped fragments
337 while (Cur
!= Head
) {
338 Fragment
= NET_LIST_USER_STRUCT (Cur
, NET_BUF
, List
);
339 Node
= IP4_GET_CLIP_INFO (Fragment
);
342 // Remove fragments completely overlapped by this fragment
344 if (Node
->End
<= This
->End
) {
345 Cur
= Cur
->ForwardLink
;
347 NetListRemoveEntry (&Fragment
->List
);
348 Assemble
->CurLen
-= Node
->Length
;
350 NetbufFree (Fragment
);
355 // The conditions are: THIS.Start <= NODE.Start, and THIS.End <
356 // NODE.End. Two fragments overlaps if NODE.Start < THIS.End.
357 // If two fragments start at the same offset, remove THIS fragment
358 // because ((THIS.Start == NODE.Start) && (THIS.End < NODE.End)).
360 if (Node
->Start
< This
->End
) {
361 if (This
->Start
== Node
->Start
) {
362 NetListRemoveEntry (&Packet
->List
);
366 Ip4TrimPacket (Packet
, This
->Start
, Node
->Start
);
373 // Update the assemble info: increase the current length. If it is
374 // the frist fragment, update the packet's IP head and per packet
375 // info. If it is the last fragment, update the total length.
377 Assemble
->CurLen
+= This
->Length
;
379 if (This
->Start
== 0) {
381 // Once the first fragment is enqueued, it can't be removed
382 // from the fragment list. So, Assemble->Head always point
383 // to valid memory area.
385 ASSERT (Assemble
->Head
== NULL
);
387 Assemble
->Head
= IpHead
;
388 Assemble
->Info
= IP4_GET_CLIP_INFO (Packet
);
392 // Don't update the length more than once.
394 if (IP4_LAST_FRAGMENT (IpHead
->Fragment
) && (Assemble
->TotalLen
== 0)) {
395 Assemble
->TotalLen
= This
->End
;
399 // Deliver the whole packet if all the fragments received.
400 // All fragments received if:
401 // 1. received the last one, so, the totoal length is know
402 // 2. received all the data. If the last fragment on the
403 // queue ends at the total length, all data is received.
405 if ((Assemble
->TotalLen
!= 0) && (Assemble
->CurLen
>= Assemble
->TotalLen
)) {
407 NetListRemoveEntry (&Assemble
->Link
);
410 // If the packet is properly formated, the last fragment's End
411 // equals to the packet's total length. Otherwise, the packet
412 // is a fake, drop it now.
414 Fragment
= NET_LIST_USER_STRUCT (Head
->BackLink
, NET_BUF
, List
);
416 if (IP4_GET_CLIP_INFO (Fragment
)->End
!= Assemble
->TotalLen
) {
417 Ip4FreeAssembleEntry (Assemble
);
422 // Wrap the packet in a net buffer then deliver it up
424 NewPacket
= NetbufFromBufList (
425 &Assemble
->Fragments
,
432 if (NewPacket
== NULL
) {
433 Ip4FreeAssembleEntry (Assemble
);
437 NewPacket
->Ip
= Assemble
->Head
;
438 CopyMem (IP4_GET_CLIP_INFO (NewPacket
), Assemble
->Info
, sizeof (*IP4_GET_CLIP_INFO (NewPacket
)));
451 The IP4 input routine. It is called by the IP4_INTERFACE when a
452 IP4 fragment is received from MNP.
454 @param Ip4Instance The IP4 child that request the receive, most like
456 @param Packet The IP4 packet received.
457 @param IoStatus The return status of receive request.
458 @param Flag The link layer flag for the packet received, such
460 @param Context The IP4 service instance that own the MNP.
467 IN IP4_PROTOCOL
*Ip4Instance
,
469 IN EFI_STATUS IoStatus
,
482 IpSb
= (IP4_SERVICE
*) Context
;
484 if (EFI_ERROR (IoStatus
) || (IpSb
->State
== IP4_SERVICE_DESTORY
)) {
489 // Check that the IP4 header is correctly formated
491 if (Packet
->TotalSize
< IP4_MIN_HEADLEN
) {
495 Head
= (IP4_HEAD
*) NetbufGetByte (Packet
, 0, NULL
);
496 HeadLen
= (Head
->HeadLen
<< 2);
497 TotalLen
= NTOHS (Head
->TotalLen
);
500 // Mnp may deliver frame trailer sequence up, trim it off.
502 if (TotalLen
< Packet
->TotalSize
) {
503 NetbufTrim (Packet
, Packet
->TotalSize
- TotalLen
, FALSE
);
506 if ((Head
->Ver
!= 4) || (HeadLen
< IP4_MIN_HEADLEN
) ||
507 (TotalLen
< HeadLen
) || (TotalLen
!= Packet
->TotalSize
)) {
512 // Some OS may send IP packets without checksum.
514 Checksum
= (UINT16
) (~NetblockChecksum ((UINT8
*) Head
, HeadLen
));
516 if ((Head
->Checksum
!= 0) && (Checksum
!= 0)) {
521 // Convert the IP header to host byte order, then get the per packet info.
523 Packet
->Ip
= Ip4NtohHead (Head
);
525 Info
= IP4_GET_CLIP_INFO (Packet
);
526 Info
->LinkFlag
= Flag
;
527 Info
->CastType
= Ip4GetHostCast (IpSb
, Head
->Dst
, Head
->Src
);
528 Info
->Start
= (Head
->Fragment
& IP4_HEAD_OFFSET_MASK
) << 3;
529 Info
->Length
= Head
->TotalLen
- HeadLen
;
530 Info
->End
= Info
->Start
+ Info
->Length
;
531 Info
->Status
= EFI_SUCCESS
;
534 // The packet is destinated to us if the CastType is non-zero.
536 if ((Info
->CastType
== 0) || (Info
->End
> IP4_MAX_PACKET_SIZE
)) {
541 // Validate the options. Don't call the Ip4OptionIsValid if
542 // there is no option to save some CPU process.
544 OptionLen
= HeadLen
- IP4_MIN_HEADLEN
;
546 if ((OptionLen
> 0) && !Ip4OptionIsValid ((UINT8
*) (Head
+ 1), OptionLen
, TRUE
)) {
551 // Trim the head off, after this point, the packet is headless.
552 // and Packet->TotalLen == Info->Length.
554 NetbufTrim (Packet
, HeadLen
, TRUE
);
557 // Reassemble the packet if this is a fragment. The packet is a
558 // fragment if its head has MF (more fragment) set, or it starts
561 if ((Head
->Fragment
& IP4_HEAD_MF_MASK
) || (Info
->Start
!= 0)) {
563 // Drop the fragment if DF is set but it is fragmented. Gateway
564 // need to send a type 4 destination unreache ICMP message here.
566 if (Head
->Fragment
& IP4_HEAD_DF_MASK
) {
571 // The length of all but the last fragments is in the unit of 8 bytes.
573 if ((Head
->Fragment
& IP4_HEAD_MF_MASK
) && (Info
->Length
% 8 != 0)) {
577 Packet
= Ip4Reassemble (&IpSb
->Assemble
, Packet
);
580 // Packet assembly isn't complete, start receive more packet.
582 if (Packet
== NULL
) {
588 // Packet may have been changed. Head, HeadLen, TotalLen, and
589 // info must be reloaded bofore use. The ownership of the packet
590 // is transfered to the packet process logic.
593 IP4_GET_CLIP_INFO (Packet
)->Status
= EFI_SUCCESS
;
595 switch (Head
->Protocol
) {
597 Ip4IcmpHandle (IpSb
, Head
, Packet
);
601 Ip4IgmpHandle (IpSb
, Head
, Packet
);
605 Ip4Demultiplex (IpSb
, Head
, Packet
);
611 Ip4ReceiveFrame (IpSb
->DefaultInterface
, NULL
, Ip4AccpetFrame
, IpSb
);
614 if (Packet
!= NULL
) {
623 Check whether this IP child accepts the packet.
625 @param IpInstance The IP child to check
626 @param Head The IP header of the packet
627 @param Packet The data of the packet
629 @return TRUE if the child wants to receive the packet, otherwise return FALSE.
633 Ip4InstanceFrameAcceptable (
634 IN IP4_PROTOCOL
*IpInstance
,
639 IP4_ICMP_ERROR_HEAD Icmp
;
640 EFI_IP4_CONFIG_DATA
*Config
;
645 Config
= &IpInstance
->ConfigData
;
648 // Dirty trick for the Tiano UEFI network stack implmentation. If
649 // ReceiveTimeout == -1, the receive of the packet for this instance
650 // is disabled. The UEFI spec don't have such captibility. We add
651 // this to improve the performance because IP will make a copy of
652 // the received packet for each accepting instance. Some IP instances
653 // used by UDP/TCP only send packets, they don't wants to receive.
655 if (Config
->ReceiveTimeout
== (UINT32
)(-1)) {
659 if (Config
->AcceptPromiscuous
) {
664 // Use protocol from the IP header embedded in the ICMP error
665 // message to filter, instead of ICMP itself. ICMP handle will
666 // can Ip4Demultiplex to deliver ICMP errors.
668 Proto
= Head
->Protocol
;
670 if (Proto
== IP4_PROTO_ICMP
) {
671 NetbufCopy (Packet
, 0, sizeof (Icmp
.Head
), (UINT8
*) &Icmp
.Head
);
673 if (mIcmpClass
[Icmp
.Head
.Type
].IcmpClass
== ICMP_ERROR_MESSAGE
) {
674 if (!Config
->AcceptIcmpErrors
) {
678 NetbufCopy (Packet
, 0, sizeof (Icmp
), (UINT8
*) &Icmp
);
679 Proto
= Icmp
.IpHead
.Protocol
;
684 // Match the protocol
686 if (!Config
->AcceptAnyProtocol
&& (Proto
!= Config
->DefaultProtocol
)) {
691 // Check for broadcast, the caller has computed the packet's
692 // cast type for this child's interface.
694 Info
= IP4_GET_CLIP_INFO (Packet
);
696 if (IP4_IS_BROADCAST (Info
->CastType
)) {
697 return Config
->AcceptBroadcast
;
701 // If it is a multicast packet, check whether we are in the group.
703 if (Info
->CastType
== IP4_MULTICAST
) {
705 // Receive the multicast if the instance wants to receive all packets.
707 if (!IpInstance
->ConfigData
.UseDefaultAddress
&& (IpInstance
->Interface
->Ip
== 0)) {
711 for (Index
= 0; Index
< IpInstance
->GroupCount
; Index
++) {
712 if (IpInstance
->Groups
[Index
] == HTONL (Head
->Dst
)) {
717 return (BOOLEAN
)(Index
< IpInstance
->GroupCount
);
725 Enqueue a shared copy of the packet to the IP4 child if the
726 packet is acceptable to it. Here the data of the packet is
727 shared, but the net buffer isn't.
729 @param IpInstance The IP4 child to enqueue the packet to
730 @param Head The IP header of the received packet
731 @param Packet The data of the received packet
733 @retval EFI_NOT_STARTED The IP child hasn't been configured.
734 @retval EFI_INVALID_PARAMETER The child doesn't want to receive the packet
735 @retval EFI_OUT_OF_RESOURCES Failed to allocate some resource
736 @retval EFI_SUCCESS A shared copy the packet is enqueued to the child.
740 Ip4InstanceEnquePacket (
741 IN IP4_PROTOCOL
*IpInstance
,
750 // Check whether the packet is acceptable to this instance.
752 if (IpInstance
->State
!= IP4_STATE_CONFIGED
) {
753 return EFI_NOT_STARTED
;
756 if (!Ip4InstanceFrameAcceptable (IpInstance
, Head
, Packet
)) {
757 return EFI_INVALID_PARAMETER
;
761 // Enque a shared copy of the packet.
763 Clone
= NetbufClone (Packet
);
766 return EFI_OUT_OF_RESOURCES
;
770 // Set the receive time out for the assembled packet. If it expires,
771 // packet will be removed from the queue.
773 Info
= IP4_GET_CLIP_INFO (Clone
);
774 Info
->Life
= IP4_US_TO_SEC (IpInstance
->ConfigData
.ReceiveTimeout
);
776 NetListInsertTail (&IpInstance
->Received
, &Clone
->List
);
782 The signal handle of IP4's recycle event. It is called back
783 when the upper layer release the packet.
785 @param Event The IP4's recycle event.
786 @param Context The context of the handle, which is a
800 IP4_RXDATA_WRAP
*Wrap
;
802 Wrap
= (IP4_RXDATA_WRAP
*) Context
;
804 NET_TRYLOCK (&Wrap
->IpInstance
->RecycleLock
);
805 NetListRemoveEntry (&Wrap
->Link
);
806 NET_UNLOCK (&Wrap
->IpInstance
->RecycleLock
);
808 ASSERT (!NET_BUF_SHARED (Wrap
->Packet
));
809 NetbufFree (Wrap
->Packet
);
811 gBS
->CloseEvent (Wrap
->RxData
.RecycleSignal
);
817 Wrap the received packet to a IP4_RXDATA_WRAP, which will be
818 delivered to the upper layer. Each IP4 child that accepts the
819 packet will get a not-shared copy of the packet which is wrapped
820 in the IP4_RXDATA_WRAP. The IP4_RXDATA_WRAP->RxData is passed
821 to the upper layer. Upper layer will signal the recycle event in
822 it when it is done with the packet.
824 @param IpInstance The IP4 child to receive the packet
825 @param Packet The packet to deliver up.
827 @return NULL if failed to wrap the packet, otherwise the wrapper.
832 IN IP4_PROTOCOL
*IpInstance
,
836 IP4_RXDATA_WRAP
*Wrap
;
837 EFI_IP4_RECEIVE_DATA
*RxData
;
840 Wrap
= NetAllocatePool (IP4_RXDATA_WRAP_SIZE (Packet
->BlockOpNum
));
846 NetListInit (&Wrap
->Link
);
848 Wrap
->IpInstance
= IpInstance
;
849 Wrap
->Packet
= Packet
;
850 RxData
= &Wrap
->RxData
;
852 NetZeroMem (&RxData
->TimeStamp
, sizeof (EFI_TIME
));
854 Status
= gBS
->CreateEvent (
859 &RxData
->RecycleSignal
862 if (EFI_ERROR (Status
)) {
867 ASSERT (Packet
->Ip
!= NULL
);
870 // The application expects a network byte order header.
872 RxData
->HeaderLength
= (Packet
->Ip
->HeadLen
<< 2);
873 RxData
->Header
= (EFI_IP4_HEADER
*) Ip4NtohHead (Packet
->Ip
);
875 RxData
->OptionsLength
= RxData
->HeaderLength
- IP4_MIN_HEADLEN
;
876 RxData
->Options
= NULL
;
878 if (RxData
->OptionsLength
!= 0) {
879 RxData
->Options
= (VOID
*) (RxData
->Header
+ 1);
882 RxData
->DataLength
= Packet
->TotalSize
;
885 // Build the fragment table to be delivered up.
887 RxData
->FragmentCount
= Packet
->BlockOpNum
;
888 NetbufBuildExt (Packet
, (NET_FRAGMENT
*) RxData
->FragmentTable
, &RxData
->FragmentCount
);
895 Deliver the received packets to upper layer if there are both received
896 requests and enqueued packets. If the enqueued packet is shared, it will
897 duplicate it to a non-shared packet, release the shared packet, then
898 deliver the non-shared packet up.
900 @param IpInstance The IP child to deliver the packet up.
902 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources to deliver the
904 @retval EFI_SUCCESS All the enqueued packets that can be delivered
909 Ip4InstanceDeliverPacket (
910 IN IP4_PROTOCOL
*IpInstance
913 EFI_IP4_COMPLETION_TOKEN
*Token
;
914 IP4_RXDATA_WRAP
*Wrap
;
920 // Deliver a packet if there are both a packet and a receive token.
922 while (!NetListIsEmpty (&IpInstance
->Received
) &&
923 !NetMapIsEmpty (&IpInstance
->RxTokens
)) {
925 Packet
= NET_LIST_HEAD (&IpInstance
->Received
, NET_BUF
, List
);
927 if (!NET_BUF_SHARED (Packet
)) {
929 // If this is the only instance that wants the packet, wrap it up.
931 Wrap
= Ip4WrapRxData (IpInstance
, Packet
);
934 return EFI_OUT_OF_RESOURCES
;
937 NetListRemoveEntry (&Packet
->List
);
941 // Create a duplicated packet if this packet is shared
943 Dup
= NetbufDuplicate (Packet
, NULL
, IP4_MAX_HEADLEN
);
946 return EFI_OUT_OF_RESOURCES
;
950 // Copy the IP head over. The packet to deliver up is
951 // headless. Trim the head off after copy. The IP head
952 // may be not continuous before the data.
954 Head
= NetbufAllocSpace (Dup
, IP4_MAX_HEADLEN
, NET_BUF_HEAD
);
955 Dup
->Ip
= (IP4_HEAD
*) Head
;
957 NetCopyMem (Head
, Packet
->Ip
, Packet
->Ip
->HeadLen
<< 2);
958 NetbufTrim (Dup
, IP4_MAX_HEADLEN
, TRUE
);
960 Wrap
= Ip4WrapRxData (IpInstance
, Dup
);
964 return EFI_OUT_OF_RESOURCES
;
967 NetListRemoveEntry (&Packet
->List
);
974 // Insert it into the delivered packet, then get a user's
975 // receive token, pass the wrapped packet up.
977 NET_TRYLOCK (&IpInstance
->RecycleLock
);
978 NetListInsertHead (&IpInstance
->Delivered
, &Wrap
->Link
);
979 NET_UNLOCK (&IpInstance
->RecycleLock
);
981 Token
= NetMapRemoveHead (&IpInstance
->RxTokens
, NULL
);
982 Token
->Status
= IP4_GET_CLIP_INFO (Packet
)->Status
;
983 Token
->Packet
.RxData
= &Wrap
->RxData
;
985 gBS
->SignalEvent (Token
->Event
);
993 Enqueue a received packet to all the IP children that share
996 @param IpSb The IP4 service instance that receive the packet
997 @param Head The header of the received packet
998 @param Packet The data of the received packet
999 @param IpIf The interface to enqueue the packet to
1001 @return The number of the IP4 children that accepts the packet
1005 Ip4InterfaceEnquePacket (
1006 IN IP4_SERVICE
*IpSb
,
1009 IN IP4_INTERFACE
*IpIf
1012 IP4_PROTOCOL
*IpInstance
;
1013 IP4_CLIP_INFO
*Info
;
1014 NET_LIST_ENTRY
*Entry
;
1020 // First, check that the packet is acceptable to this interface
1021 // and find the local cast type for the interface. A packet sent
1022 // to say 192.168.1.1 should NOT be delliever to 10.0.0.1 unless
1023 // promiscuous receiving.
1026 Info
= IP4_GET_CLIP_INFO (Packet
);
1028 if ((Info
->CastType
== IP4_MULTICAST
) || (Info
->CastType
== IP4_LOCAL_BROADCAST
)) {
1030 // If the CastType is multicast, don't need to filter against
1031 // the group address here, Ip4InstanceFrameAcceptable will do
1034 LocalType
= Info
->CastType
;
1038 // Check the destination againist local IP. If the station
1039 // address is 0.0.0.0, it means receiving all the IP destined
1040 // to local non-zero IP. Otherwise, it is necessary to compare
1041 // the destination to the interface's IP address.
1043 if (IpIf
->Ip
== IP4_ALLZERO_ADDRESS
) {
1044 LocalType
= IP4_LOCAL_HOST
;
1047 LocalType
= Ip4GetNetCast (Head
->Dst
, IpIf
);
1049 if ((LocalType
== 0) && IpIf
->PromiscRecv
) {
1050 LocalType
= IP4_PROMISCUOUS
;
1055 if (LocalType
== 0) {
1060 // Iterate through the ip instances on the interface, enqueue
1061 // the packet if filter passed. Save the original cast type,
1062 // and pass the local cast type to the IP children on the
1063 // interface. The global cast type will be restored later.
1065 SavedType
= Info
->CastType
;
1066 Info
->CastType
= LocalType
;
1070 NET_LIST_FOR_EACH (Entry
, &IpIf
->IpInstances
) {
1071 IpInstance
= NET_LIST_USER_STRUCT (Entry
, IP4_PROTOCOL
, AddrLink
);
1072 NET_CHECK_SIGNATURE (IpInstance
, IP4_PROTOCOL_SIGNATURE
);
1074 if (Ip4InstanceEnquePacket (IpInstance
, Head
, Packet
) == EFI_SUCCESS
) {
1079 Info
->CastType
= SavedType
;
1085 Deliver the packet for each IP4 child on the interface.
1087 @param IpSb The IP4 service instance that received the packet
1088 @param IpIf The IP4 interface to deliver the packet.
1090 @retval EFI_SUCCESS It always returns EFI_SUCCESS now
1094 Ip4InterfaceDeliverPacket (
1095 IN IP4_SERVICE
*IpSb
,
1096 IN IP4_INTERFACE
*IpIf
1099 IP4_PROTOCOL
*Ip4Instance
;
1100 NET_LIST_ENTRY
*Entry
;
1102 NET_LIST_FOR_EACH (Entry
, &IpIf
->IpInstances
) {
1103 Ip4Instance
= NET_LIST_USER_STRUCT (Entry
, IP4_PROTOCOL
, AddrLink
);
1104 Ip4InstanceDeliverPacket (Ip4Instance
);
1112 Demultiple the packet. the packet delivery is processed in two
1113 passes. The first pass will enque a shared copy of the packet
1114 to each IP4 child that accepts the packet. The second pass will
1115 deliver a non-shared copy of the packet to each IP4 child that
1116 has pending receive requests. Data is copied if more than one
1117 child wants to consume the packet bacause each IP child need
1118 its own copy of the packet to make changes.
1120 @param IpSb The IP4 service instance that received the packet
1121 @param Head The header of the received packet
1122 @param Packet The data of the received packet
1124 @retval EFI_NOT_FOUND No IP child accepts the packet
1125 @retval EFI_SUCCESS The packet is enqueued or delivered to some IP
1131 IN IP4_SERVICE
*IpSb
,
1136 NET_LIST_ENTRY
*Entry
;
1137 IP4_INTERFACE
*IpIf
;
1141 // Two pass delivery: first, enque a shared copy of the packet
1142 // to each instance that accept the packet.
1146 NET_LIST_FOR_EACH (Entry
, &IpSb
->Interfaces
) {
1147 IpIf
= NET_LIST_USER_STRUCT (Entry
, IP4_INTERFACE
, Link
);
1149 if (IpIf
->Configured
) {
1150 Enqueued
+= Ip4InterfaceEnquePacket (IpSb
, Head
, Packet
, IpIf
);
1155 // Second: deliver a duplicate of the packet to each instance.
1156 // Release the local reference first, so that the last instance
1157 // getting the packet will not copy the data.
1159 NetbufFree (Packet
);
1161 if (Enqueued
== 0) {
1162 return EFI_NOT_FOUND
;
1165 NET_LIST_FOR_EACH (Entry
, &IpSb
->Interfaces
) {
1166 IpIf
= NET_LIST_USER_STRUCT (Entry
, IP4_INTERFACE
, Link
);
1168 if (IpIf
->Configured
) {
1169 Ip4InterfaceDeliverPacket (IpSb
, IpIf
);
1178 Timeout the fragment and enqueued packets.
1180 @param IpSb The IP4 service instance to timeout
1186 Ip4PacketTimerTicking (
1187 IN IP4_SERVICE
*IpSb
1190 NET_LIST_ENTRY
*InstanceEntry
;
1191 NET_LIST_ENTRY
*Entry
;
1192 NET_LIST_ENTRY
*Next
;
1193 IP4_PROTOCOL
*IpInstance
;
1194 IP4_ASSEMBLE_ENTRY
*Assemble
;
1196 IP4_CLIP_INFO
*Info
;
1200 // First, time out the fragments. The packet's life is counting down
1201 // once the first-arrived fragment was received.
1203 for (Index
= 0; Index
< IP4_ASSEMLE_HASH_SIZE
; Index
++) {
1204 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &IpSb
->Assemble
.Bucket
[Index
]) {
1205 Assemble
= NET_LIST_USER_STRUCT (Entry
, IP4_ASSEMBLE_ENTRY
, Link
);
1207 if ((Assemble
->Life
> 0) && (--Assemble
->Life
== 0)) {
1208 NetListRemoveEntry (Entry
);
1209 Ip4FreeAssembleEntry (Assemble
);
1214 NET_LIST_FOR_EACH (InstanceEntry
, &IpSb
->Children
) {
1215 IpInstance
= NET_LIST_USER_STRUCT (InstanceEntry
, IP4_PROTOCOL
, Link
);
1218 // Second, time out the assembled packets enqueued on each IP child.
1220 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &IpInstance
->Received
) {
1221 Packet
= NET_LIST_USER_STRUCT (Entry
, NET_BUF
, List
);
1222 Info
= IP4_GET_CLIP_INFO (Packet
);
1224 if ((Info
->Life
> 0) && (--Info
->Life
== 0)) {
1225 NetListRemoveEntry (Entry
);
1226 NetbufFree (Packet
);
1231 // Third: time out the transmitted packets.
1233 NetMapIterate (&IpInstance
->TxTokens
, Ip4SentPacketTicking
, NULL
);