2 The implementation of the Udp4 protocol.
4 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
11 UINT16 mUdp4RandomPort
;
14 This function checks and timeouts the I/O datagrams holding by the corresponding
17 @param[in] Event The event this function registered to.
18 @param[in] Context The context data registered during the creation of
30 This function finds the udp instance by the specified <Address, Port> pair.
32 @param[in] InstanceList Pointer to the head of the list linking the udp
34 @param[in] Address Pointer to the specified IPv4 address.
35 @param[in] Port The udp port number.
37 @retval TRUE The specified <Address, Port> pair is found.
38 @retval FALSE Otherwise.
42 Udp4FindInstanceByPort (
43 IN LIST_ENTRY
*InstanceList
,
44 IN EFI_IPv4_ADDRESS
*Address
,
49 This function is the packet transmitting notify function registered to the IpIo
50 interface. It's called to signal the udp TxToken when IpIo layer completes the
51 transmitting of the udp datagram.
53 @param[in] Status The completion status of the output udp datagram.
54 @param[in] Context Pointer to the context data.
55 @param[in] Sender Specify a pointer of EFI_IP4_PROTOCOL for sending.
56 @param[in] NotifyData Pointer to the notify data.
64 IN IP_IO_IP_PROTOCOL Sender
,
69 This function processes the received datagram passed up by the IpIo layer.
71 @param[in] Status The status of this udp datagram.
72 @param[in] IcmpError The IcmpError code, only available when Status is
74 @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA.
75 @param[in] Packet Pointer to the NET_BUF containing the received udp
77 @param[in] Context Pointer to the context data.
85 IN EFI_NET_SESSION_DATA
*NetSession
,
91 This function cancels the token specified by Arg in the Map. This is a callback
92 used by Udp4InstanceCancelToken().
94 @param[in] Map Pointer to the NET_MAP.
95 @param[in] Item Pointer to the NET_MAP_ITEM.
96 @param[in] Arg Pointer to the token to be cancelled, if NULL,
97 the token specified by Item is cancelled.
99 @retval EFI_SUCCESS The token is cancelled if Arg is NULL or the token
100 is not the same as that in the Item if Arg is not
102 @retval EFI_ABORTED Arg is not NULL, and the token specified by Arg is
110 IN NET_MAP_ITEM
*Item
,
111 IN VOID
*Arg OPTIONAL
115 This function matches the received udp datagram with the Instance.
117 @param[in] Instance Pointer to the udp instance context data.
118 @param[in] Udp4Session Pointer to the EFI_UDP4_SESSION_DATA abstracted
119 from the received udp datagram.
121 @retval TRUE The udp datagram matches the receiving requirements of the
123 @retval FALSE Otherwise.
128 IN UDP4_INSTANCE_DATA
*Instance
,
129 IN EFI_UDP4_SESSION_DATA
*Udp4Session
133 This function removes the Wrap specified by Context and release relevant resources.
135 @param[in] Event The Event this notify function registered to.
136 @param[in] Context Pointer to the context data.
141 Udp4RecycleRxDataWrap (
147 This function wraps the Packet and the RxData.
149 @param[in] Instance Pointer to the instance context data.
150 @param[in] Packet Pointer to the buffer containing the received
152 @param[in] RxData Pointer to the EFI_UDP4_RECEIVE_DATA of this
155 @return Pointer to the structure wrapping the RxData and the Packet.
160 IN UDP4_INSTANCE_DATA
*Instance
,
162 IN EFI_UDP4_RECEIVE_DATA
*RxData
166 This function enqueues the received datagram into the instances' receiving queues.
168 @param[in] Udp4Service Pointer to the udp service context data.
169 @param[in] Packet Pointer to the buffer containing the received
171 @param[in] RxData Pointer to the EFI_UDP4_RECEIVE_DATA of this
174 @return The times this datagram is enqueued.
179 IN UDP4_SERVICE_DATA
*Udp4Service
,
181 IN EFI_UDP4_RECEIVE_DATA
*RxData
185 This function delivers the datagrams enqueued in the instances.
187 @param[in] Udp4Service Pointer to the udp service context data.
192 IN UDP4_SERVICE_DATA
*Udp4Service
196 This function demultiplexes the received udp datagram to the appropriate instances.
198 @param[in] Udp4Service Pointer to the udp service context data.
199 @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA abstracted from
200 the received datagram.
201 @param[in] Packet Pointer to the buffer containing the received udp
207 IN UDP4_SERVICE_DATA
*Udp4Service
,
208 IN EFI_NET_SESSION_DATA
*NetSession
,
213 This function handles the received Icmp Error message and demultiplexes it to the
216 @param[in] Udp4Service Pointer to the udp service context data.
217 @param[in] IcmpError The icmp error code.
218 @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA abstracted
219 from the received Icmp Error packet.
220 @param[in] Packet Pointer to the Icmp Error packet.
225 IN UDP4_SERVICE_DATA
*Udp4Service
,
227 IN EFI_NET_SESSION_DATA
*NetSession
,
232 This function builds and sends out a icmp port unreachable message.
234 @param[in] IpIo Pointer to the IP_IO instance.
235 @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA of the packet
236 causes this icmp error message.
237 @param[in] Udp4Header Pointer to the udp header of the datagram causes
238 this icmp error message.
242 Udp4SendPortUnreach (
244 IN EFI_NET_SESSION_DATA
*NetSession
,
249 Create the Udp service context data.
251 @param[in, out] Udp4Service Pointer to the UDP4_SERVICE_DATA.
252 @param[in] ImageHandle The image handle of this udp4 driver.
253 @param[in] ControllerHandle The controller handle this udp4 driver binds on.
255 @retval EFI_SUCCESS The udp4 service context data is created and
257 @retval EFI_OUT_OF_RESOURCES Cannot allocate memory.
258 @retval other Other error occurs.
263 IN OUT UDP4_SERVICE_DATA
*Udp4Service
,
264 IN EFI_HANDLE ImageHandle
,
265 IN EFI_HANDLE ControllerHandle
269 IP_IO_OPEN_DATA OpenData
;
270 EFI_IP4_CONFIG_DATA
*Ip4ConfigData
;
272 ZeroMem (Udp4Service
, sizeof (UDP4_SERVICE_DATA
));
274 Udp4Service
->Signature
= UDP4_SERVICE_DATA_SIGNATURE
;
275 Udp4Service
->ServiceBinding
= mUdp4ServiceBinding
;
276 Udp4Service
->ImageHandle
= ImageHandle
;
277 Udp4Service
->ControllerHandle
= ControllerHandle
;
278 Udp4Service
->ChildrenNumber
= 0;
280 InitializeListHead (&Udp4Service
->ChildrenList
);
283 // Create the IpIo for this service context.
285 Udp4Service
->IpIo
= IpIoCreate (ImageHandle
, ControllerHandle
, IP_VERSION_4
);
286 if (Udp4Service
->IpIo
== NULL
) {
287 return EFI_OUT_OF_RESOURCES
;
291 // Set the OpenData used to open the IpIo.
293 Ip4ConfigData
= &OpenData
.IpConfigData
.Ip4CfgData
;
294 CopyMem (Ip4ConfigData
, &mIp4IoDefaultIpConfigData
, sizeof (EFI_IP4_CONFIG_DATA
));
295 Ip4ConfigData
->AcceptBroadcast
= TRUE
;
296 OpenData
.RcvdContext
= (VOID
*)Udp4Service
;
297 OpenData
.SndContext
= NULL
;
298 OpenData
.PktRcvdNotify
= Udp4DgramRcvd
;
299 OpenData
.PktSentNotify
= Udp4DgramSent
;
302 // Configure and start the IpIo.
304 Status
= IpIoOpen (Udp4Service
->IpIo
, &OpenData
);
305 if (EFI_ERROR (Status
)) {
310 // Create the event for Udp timeout checking.
312 Status
= gBS
->CreateEvent (
313 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
317 &Udp4Service
->TimeoutEvent
319 if (EFI_ERROR (Status
)) {
324 // Start the timeout timer event.
326 Status
= gBS
->SetTimer (
327 Udp4Service
->TimeoutEvent
,
329 UDP4_TIMEOUT_INTERVAL
331 if (EFI_ERROR (Status
)) {
339 if (Udp4Service
->TimeoutEvent
!= NULL
) {
340 gBS
->CloseEvent (Udp4Service
->TimeoutEvent
);
343 IpIoDestroy (Udp4Service
->IpIo
);
349 Clean the Udp service context data.
351 @param[in] Udp4Service Pointer to the UDP4_SERVICE_DATA.
356 IN UDP4_SERVICE_DATA
*Udp4Service
360 // Cancel the TimeoutEvent timer.
362 gBS
->SetTimer (Udp4Service
->TimeoutEvent
, TimerCancel
, 0);
365 // Close the TimeoutEvent timer.
367 gBS
->CloseEvent (Udp4Service
->TimeoutEvent
);
372 IpIoDestroy (Udp4Service
->IpIo
);
376 This function checks and timeouts the I/O datagrams holding by the corresponding
379 @param[in] Event The event this function registered to.
380 @param[in] Context The context data registered during the creation of
391 UDP4_SERVICE_DATA
*Udp4Service
;
393 UDP4_INSTANCE_DATA
*Instance
;
394 LIST_ENTRY
*WrapEntry
;
395 LIST_ENTRY
*NextEntry
;
396 UDP4_RXDATA_WRAP
*Wrap
;
398 Udp4Service
= (UDP4_SERVICE_DATA
*)Context
;
399 NET_CHECK_SIGNATURE (Udp4Service
, UDP4_SERVICE_DATA_SIGNATURE
);
401 NET_LIST_FOR_EACH (Entry
, &Udp4Service
->ChildrenList
) {
403 // Iterate all the instances belonging to this service context.
405 Instance
= NET_LIST_USER_STRUCT (Entry
, UDP4_INSTANCE_DATA
, Link
);
406 NET_CHECK_SIGNATURE (Instance
, UDP4_INSTANCE_DATA_SIGNATURE
);
408 if (!Instance
->Configured
|| (Instance
->ConfigData
.ReceiveTimeout
== 0)) {
410 // Skip this instance if it's not configured or no receive timeout.
415 NET_LIST_FOR_EACH_SAFE (WrapEntry
, NextEntry
, &Instance
->RcvdDgramQue
) {
417 // Iterate all the rxdatas belonging to this udp instance.
419 Wrap
= NET_LIST_USER_STRUCT (WrapEntry
, UDP4_RXDATA_WRAP
, Link
);
422 // TimeoutTick unit is microsecond, MNP_TIMEOUT_CHECK_INTERVAL unit is 100ns.
424 if (Wrap
->TimeoutTick
< (UDP4_TIMEOUT_INTERVAL
/ 10)) {
426 // Remove this RxData if it timeouts.
428 Udp4RecycleRxDataWrap (NULL
, (VOID
*)Wrap
);
430 Wrap
->TimeoutTick
-= (UDP4_TIMEOUT_INTERVAL
/ 10);
437 This function initializes the new created udp instance.
439 @param[in] Udp4Service Pointer to the UDP4_SERVICE_DATA.
440 @param[in, out] Instance Pointer to the un-initialized UDP4_INSTANCE_DATA.
445 IN UDP4_SERVICE_DATA
*Udp4Service
,
446 IN OUT UDP4_INSTANCE_DATA
*Instance
450 // Set the signature.
452 Instance
->Signature
= UDP4_INSTANCE_DATA_SIGNATURE
;
457 InitializeListHead (&Instance
->Link
);
458 InitializeListHead (&Instance
->RcvdDgramQue
);
459 InitializeListHead (&Instance
->DeliveredDgramQue
);
462 // Init the NET_MAPs.
464 NetMapInit (&Instance
->TxTokens
);
465 NetMapInit (&Instance
->RxTokens
);
466 NetMapInit (&Instance
->McastIps
);
469 // Save the pointer to the UDP4_SERVICE_DATA, and initialize other members.
471 Instance
->Udp4Service
= Udp4Service
;
472 CopyMem (&Instance
->Udp4Proto
, &mUdp4Protocol
, sizeof (Instance
->Udp4Proto
));
473 Instance
->IcmpError
= EFI_SUCCESS
;
474 Instance
->Configured
= FALSE
;
475 Instance
->IsNoMapping
= FALSE
;
476 Instance
->InDestroy
= FALSE
;
480 This function cleans the udp instance.
482 @param[in] Instance Pointer to the UDP4_INSTANCE_DATA to clean.
487 IN UDP4_INSTANCE_DATA
*Instance
490 NetMapClean (&Instance
->McastIps
);
491 NetMapClean (&Instance
->RxTokens
);
492 NetMapClean (&Instance
->TxTokens
);
496 This function finds the udp instance by the specified <Address, Port> pair.
498 @param[in] InstanceList Pointer to the head of the list linking the udp
500 @param[in] Address Pointer to the specified IPv4 address.
501 @param[in] Port The udp port number.
503 @retval TRUE The specified <Address, Port> pair is found.
504 @retval FALSE Otherwise.
508 Udp4FindInstanceByPort (
509 IN LIST_ENTRY
*InstanceList
,
510 IN EFI_IPv4_ADDRESS
*Address
,
515 UDP4_INSTANCE_DATA
*Instance
;
516 EFI_UDP4_CONFIG_DATA
*ConfigData
;
518 NET_LIST_FOR_EACH (Entry
, InstanceList
) {
520 // Iterate all the udp instances.
522 Instance
= NET_LIST_USER_STRUCT (Entry
, UDP4_INSTANCE_DATA
, Link
);
523 ConfigData
= &Instance
->ConfigData
;
525 if (!Instance
->Configured
|| ConfigData
->AcceptAnyPort
) {
527 // If the instance is not configured or the configdata of the instance indicates
528 // this instance accepts any port, skip it.
533 if (EFI_IP4_EQUAL (&ConfigData
->StationAddress
, Address
) &&
534 (ConfigData
->StationPort
== Port
))
537 // if both the address and the port are the same, return TRUE.
544 // return FALSE when matching fails.
550 This function tries to bind the udp instance according to the configured port
553 @param[in] InstanceList Pointer to the head of the list linking the udp
555 @param[in, out] ConfigData Pointer to the ConfigData of the instance to be
556 bound. ConfigData->StationPort will be assigned
557 with an available port value on success.
559 @retval EFI_SUCCESS The bound operation is completed successfully.
560 @retval EFI_ACCESS_DENIED The <Address, Port> specified by the ConfigData is
561 already used by other instance.
562 @retval EFI_OUT_OF_RESOURCES No available port resources.
567 IN LIST_ENTRY
*InstanceList
,
568 IN OUT EFI_UDP4_CONFIG_DATA
*ConfigData
571 EFI_IPv4_ADDRESS
*StationAddress
;
574 if (ConfigData
->AcceptAnyPort
) {
578 StationAddress
= &ConfigData
->StationAddress
;
580 if (ConfigData
->StationPort
!= 0) {
581 if (!ConfigData
->AllowDuplicatePort
&&
582 Udp4FindInstanceByPort (InstanceList
, StationAddress
, ConfigData
->StationPort
))
585 // Do not allow duplicate port and the port is already used by other instance.
587 return EFI_ACCESS_DENIED
;
591 // select a random port for this instance;
594 if (ConfigData
->AllowDuplicatePort
) {
596 // Just pick up the random port if the instance allows duplicate port.
598 ConfigData
->StationPort
= mUdp4RandomPort
;
600 StartPort
= mUdp4RandomPort
;
602 while (Udp4FindInstanceByPort (InstanceList
, StationAddress
, mUdp4RandomPort
)) {
604 if (mUdp4RandomPort
== 0) {
605 mUdp4RandomPort
= UDP4_PORT_KNOWN
;
608 if (mUdp4RandomPort
== StartPort
) {
610 // No available port.
612 return EFI_OUT_OF_RESOURCES
;
616 ConfigData
->StationPort
= mUdp4RandomPort
;
620 if (mUdp4RandomPort
== 0) {
621 mUdp4RandomPort
= UDP4_PORT_KNOWN
;
629 This function is used to check whether the NewConfigData has any un-reconfigurable
630 parameters changed compared to the OldConfigData.
632 @param[in] OldConfigData Pointer to the current ConfigData the udp instance
634 @param[in] NewConfigData Pointer to the new ConfigData.
636 @retval TRUE The instance is reconfigurable.
637 @retval FALSE Otherwise.
641 Udp4IsReconfigurable (
642 IN EFI_UDP4_CONFIG_DATA
*OldConfigData
,
643 IN EFI_UDP4_CONFIG_DATA
*NewConfigData
646 if ((NewConfigData
->AcceptAnyPort
!= OldConfigData
->AcceptAnyPort
) ||
647 (NewConfigData
->AcceptBroadcast
!= OldConfigData
->AcceptBroadcast
) ||
648 (NewConfigData
->AcceptPromiscuous
!= OldConfigData
->AcceptPromiscuous
) ||
649 (NewConfigData
->AllowDuplicatePort
!= OldConfigData
->AllowDuplicatePort
)
653 // The receiving filter parameters cannot be changed.
658 if ((!NewConfigData
->AcceptAnyPort
) &&
659 (NewConfigData
->StationPort
!= OldConfigData
->StationPort
)
663 // The port is not changeable.
668 if (!NewConfigData
->AcceptPromiscuous
) {
669 if (NewConfigData
->UseDefaultAddress
!= OldConfigData
->UseDefaultAddress
) {
671 // The NewConfigData differs to the old one on the UseDefaultAddress.
676 if (!NewConfigData
->UseDefaultAddress
&&
677 (!EFI_IP4_EQUAL (&NewConfigData
->StationAddress
, &OldConfigData
->StationAddress
) ||
678 !EFI_IP4_EQUAL (&NewConfigData
->SubnetMask
, &OldConfigData
->SubnetMask
))
682 // If the instance doesn't use the default address, and the new address or
683 // new subnet mask is different from the old values.
689 if (!EFI_IP4_EQUAL (&NewConfigData
->RemoteAddress
, &OldConfigData
->RemoteAddress
)) {
691 // The remoteaddress is not the same.
696 if (!EFI_IP4_EQUAL (&NewConfigData
->RemoteAddress
, &mZeroIp4Addr
) &&
697 (NewConfigData
->RemotePort
!= OldConfigData
->RemotePort
)
701 // The RemotePort differs if it's designated in the configdata.
707 // All checks pass, return TRUE.
713 This function builds the Ip4 configdata from the Udp4ConfigData.
715 @param[in] Udp4ConfigData Pointer to the EFI_UDP4_CONFIG_DATA.
716 @param[in, out] Ip4ConfigData Pointer to the EFI_IP4_CONFIG_DATA.
720 Udp4BuildIp4ConfigData (
721 IN EFI_UDP4_CONFIG_DATA
*Udp4ConfigData
,
722 IN OUT EFI_IP4_CONFIG_DATA
*Ip4ConfigData
725 CopyMem (Ip4ConfigData
, &mIp4IoDefaultIpConfigData
, sizeof (*Ip4ConfigData
));
727 Ip4ConfigData
->DefaultProtocol
= EFI_IP_PROTO_UDP
;
728 Ip4ConfigData
->AcceptBroadcast
= Udp4ConfigData
->AcceptBroadcast
;
729 Ip4ConfigData
->AcceptPromiscuous
= Udp4ConfigData
->AcceptPromiscuous
;
730 Ip4ConfigData
->UseDefaultAddress
= Udp4ConfigData
->UseDefaultAddress
;
731 CopyMem (&Ip4ConfigData
->StationAddress
, &Udp4ConfigData
->StationAddress
, sizeof (EFI_IPv4_ADDRESS
));
732 CopyMem (&Ip4ConfigData
->SubnetMask
, &Udp4ConfigData
->SubnetMask
, sizeof (EFI_IPv4_ADDRESS
));
735 // use the -1 magic number to disable the receiving process of the ip instance.
737 Ip4ConfigData
->ReceiveTimeout
= (UINT32
)(-1);
741 This function validates the TxToken, it returns the error code according to the spec.
743 @param[in] Instance Pointer to the udp instance context data.
744 @param[in] TxToken Pointer to the token to be checked.
746 @retval EFI_SUCCESS The TxToken is valid.
747 @retval EFI_INVALID_PARAMETER One or more of the following are TRUE: This is
748 NULL. Token is NULL. Token.Event is NULL.
749 Token.Packet.TxData is NULL.
750 Token.Packet.TxData.FragmentCount is zero.
751 Token.Packet.TxData.DataLength is not equal to the
752 sum of fragment lengths. One or more of the
753 Token.Packet.TxData.FragmentTable[].
754 FragmentLength fields is zero. One or more of the
755 Token.Packet.TxData.FragmentTable[].
756 FragmentBuffer fields is NULL.
757 Token.Packet.TxData. GatewayAddress is not a
758 unicast IPv4 address if it is not NULL. One or
759 more IPv4 addresses in Token.Packet.TxData.
760 UdpSessionData are not valid unicast IPv4
761 addresses if the UdpSessionData is not NULL.
762 @retval EFI_BAD_BUFFER_SIZE The data length is greater than the maximum UDP
767 Udp4ValidateTxToken (
768 IN UDP4_INSTANCE_DATA
*Instance
,
769 IN EFI_UDP4_COMPLETION_TOKEN
*TxToken
772 EFI_UDP4_TRANSMIT_DATA
*TxData
;
775 EFI_UDP4_CONFIG_DATA
*ConfigData
;
776 EFI_UDP4_SESSION_DATA
*UdpSessionData
;
777 IP4_ADDR SourceAddress
;
778 IP4_ADDR GatewayAddress
;
780 if (TxToken
->Event
== NULL
) {
781 return EFI_INVALID_PARAMETER
;
784 TxData
= TxToken
->Packet
.TxData
;
786 if ((TxData
== NULL
) || (TxData
->FragmentCount
== 0)) {
787 return EFI_INVALID_PARAMETER
;
791 for (Index
= 0; Index
< TxData
->FragmentCount
; Index
++) {
792 if ((TxData
->FragmentTable
[Index
].FragmentBuffer
== NULL
) ||
793 (TxData
->FragmentTable
[Index
].FragmentLength
== 0))
796 // if the FragmentBuffer is NULL or the FragmentLeng is zero.
798 return EFI_INVALID_PARAMETER
;
801 TotalLen
+= TxData
->FragmentTable
[Index
].FragmentLength
;
804 if (TotalLen
!= TxData
->DataLength
) {
806 // The TotalLen calculated by adding all the FragmentLeng doesn't equal to the
809 return EFI_INVALID_PARAMETER
;
812 if (TxData
->GatewayAddress
!= NULL
) {
813 CopyMem (&GatewayAddress
, TxData
->GatewayAddress
, sizeof (IP4_ADDR
));
815 if (!Instance
->ConfigData
.UseDefaultAddress
&&
816 (EFI_NTOHL (Instance
->ConfigData
.SubnetMask
) != 0) &&
817 !NetIp4IsUnicast (NTOHL (GatewayAddress
), EFI_NTOHL (Instance
->ConfigData
.SubnetMask
)))
820 // The specified GatewayAddress is not a unicast IPv4 address while it's not 0.
822 return EFI_INVALID_PARAMETER
;
826 ConfigData
= &Instance
->ConfigData
;
827 UdpSessionData
= TxData
->UdpSessionData
;
829 if (UdpSessionData
!= NULL
) {
830 CopyMem (&SourceAddress
, &UdpSessionData
->SourceAddress
, sizeof (IP4_ADDR
));
832 if ((SourceAddress
!= 0) &&
833 !Instance
->ConfigData
.UseDefaultAddress
&&
834 (EFI_NTOHL (Instance
->ConfigData
.SubnetMask
) != 0) &&
835 !NetIp4IsUnicast (HTONL (SourceAddress
), EFI_NTOHL (Instance
->ConfigData
.SubnetMask
)))
838 // Check whether SourceAddress is a valid IPv4 address in case it's not zero.
839 // The configured station address is used if SourceAddress is zero.
841 return EFI_INVALID_PARAMETER
;
844 if ((UdpSessionData
->DestinationPort
== 0) && (ConfigData
->RemotePort
== 0)) {
846 // Ambiguous, no available DestinationPort for this token.
848 return EFI_INVALID_PARAMETER
;
851 if (EFI_IP4_EQUAL (&UdpSessionData
->DestinationAddress
, &mZeroIp4Addr
)) {
853 // The DestinationAddress specified in the UdpSessionData is 0.
855 return EFI_INVALID_PARAMETER
;
857 } else if (EFI_IP4_EQUAL (&ConfigData
->RemoteAddress
, &mZeroIp4Addr
)) {
859 // the configured RemoteAddress is all zero, and the user doesn't override the
860 // destination address.
862 return EFI_INVALID_PARAMETER
;
865 if (TxData
->DataLength
> UDP4_MAX_DATA_SIZE
) {
866 return EFI_BAD_BUFFER_SIZE
;
873 This function checks whether the specified Token duplicates with the one in the Map.
875 @param[in] Map Pointer to the NET_MAP.
876 @param[in] Item Pointer to the NET_MAP_ITEM contain the pointer to
878 @param[in] Context Pointer to the Token to be checked.
880 @retval EFI_SUCCESS The Token specified by Context differs from the
882 @retval EFI_ACCESS_DENIED The Token duplicates with the one in the Item.
889 IN NET_MAP_ITEM
*Item
,
893 EFI_UDP4_COMPLETION_TOKEN
*Token
;
894 EFI_UDP4_COMPLETION_TOKEN
*TokenInItem
;
896 Token
= (EFI_UDP4_COMPLETION_TOKEN
*)Context
;
897 TokenInItem
= (EFI_UDP4_COMPLETION_TOKEN
*)Item
->Key
;
899 if ((Token
== TokenInItem
) || (Token
->Event
== TokenInItem
->Event
)) {
901 // The Token duplicates with the TokenInItem in case either the two pointers are the
902 // same or the Events of these two tokens are the same.
904 return EFI_ACCESS_DENIED
;
911 This function calculates the checksum for the Packet, utilizing the pre-calculated
912 pseudo HeadSum to reduce some overhead.
914 @param[in] Packet Pointer to the NET_BUF contains the udp datagram.
915 @param[in] HeadSum Checksum of the pseudo header except the length
918 @retval The 16-bit checksum of this udp datagram.
929 Checksum
= NetbufChecksum (Packet
);
930 Checksum
= NetAddChecksum (Checksum
, HeadSum
);
932 Checksum
= NetAddChecksum (Checksum
, HTONS ((UINT16
)Packet
->TotalSize
));
934 return (UINT16
) ~Checksum
;
938 This function removes the specified Token from the TokenMap.
940 @param[in, out] TokenMap Pointer to the NET_MAP containing the tokens.
941 @param[in] Token Pointer to the Token to be removed.
943 @retval EFI_SUCCESS The specified Token is removed from the TokenMap.
944 @retval EFI_NOT_FOUND The specified Token is not found in the TokenMap.
949 IN OUT NET_MAP
*TokenMap
,
950 IN EFI_UDP4_COMPLETION_TOKEN
*Token
956 // Find the Token first.
958 Item
= NetMapFindKey (TokenMap
, (VOID
*)Token
);
962 // Remove the token if it's found in the map.
964 NetMapRemoveItem (TokenMap
, Item
, NULL
);
969 return EFI_NOT_FOUND
;
973 This function is the packet transmitting notify function registered to the IpIo
974 interface. It's called to signal the udp TxToken when IpIo layer completes the
975 transmitting of the udp datagram.
977 @param[in] Status The completion status of the output udp datagram.
978 @param[in] Context Pointer to the context data.
979 @param[in] Sender Specify a pointer of EFI_IP4_PROTOCOL for sending.
980 @param[in] NotifyData Pointer to the notify data.
986 IN EFI_STATUS Status
,
988 IN IP_IO_IP_PROTOCOL Sender
,
992 UDP4_INSTANCE_DATA
*Instance
;
993 EFI_UDP4_COMPLETION_TOKEN
*Token
;
995 Instance
= (UDP4_INSTANCE_DATA
*)Context
;
996 Token
= (EFI_UDP4_COMPLETION_TOKEN
*)NotifyData
;
998 if (Udp4RemoveToken (&Instance
->TxTokens
, Token
) == EFI_SUCCESS
) {
1000 // The token may be cancelled. Only signal it if the remove operation succeeds.
1002 Token
->Status
= Status
;
1003 gBS
->SignalEvent (Token
->Event
);
1009 This function processes the received datagram passed up by the IpIo layer.
1011 @param[in] Status The status of this udp datagram.
1012 @param[in] IcmpError The IcmpError code, only available when Status is
1014 @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA.
1015 @param[in] Packet Pointer to the NET_BUF containing the received udp
1017 @param[in] Context Pointer to the context data.
1023 IN EFI_STATUS Status
,
1025 IN EFI_NET_SESSION_DATA
*NetSession
,
1030 NET_CHECK_SIGNATURE (Packet
, NET_BUF_SIGNATURE
);
1033 // IpIo only passes received packets with Status EFI_SUCCESS or EFI_ICMP_ERROR.
1035 if (Status
== EFI_SUCCESS
) {
1037 // Demultiplex the received datagram.
1039 Udp4Demultiplex ((UDP4_SERVICE_DATA
*)Context
, NetSession
, Packet
);
1042 // Handle the ICMP_ERROR packet.
1044 Udp4IcmpHandler ((UDP4_SERVICE_DATA
*)Context
, IcmpError
, NetSession
, Packet
);
1048 // Dispatch the DPC queued by the NotifyFunction of the rx token's events
1049 // which are signaled with received data.
1055 This function removes the multicast group specified by Arg from the Map.
1057 @param[in, out] Map Pointer to the NET_MAP.
1058 @param[in] Item Pointer to the NET_MAP_ITEM.
1059 @param[in] Arg Pointer to the Arg, it's the pointer to a
1060 multicast IPv4 Address.
1062 @retval EFI_SUCCESS The multicast address is removed.
1063 @retval EFI_ABORTED The specified multicast address is removed and the
1070 IN OUT NET_MAP
*Map
,
1071 IN NET_MAP_ITEM
*Item
,
1072 IN VOID
*Arg OPTIONAL
1075 EFI_IPv4_ADDRESS
*McastIp
;
1079 if ((McastIp
!= NULL
) && (!EFI_IP4_EQUAL (McastIp
, &(Item
->Key
)))) {
1081 // McastIp is not NULL and the multicast address contained in the Item
1082 // is not the same as McastIp.
1088 // Remove this Item.
1090 NetMapRemoveItem (Map
, Item
, NULL
);
1092 if (McastIp
!= NULL
) {
1094 // Return EFI_ABORTED in case McastIp is not NULL to terminate the iteration.
1103 This function cancels the token specified by Arg in the Map. This is a callback
1104 used by Udp4InstanceCancelToken().
1106 @param[in] Map Pointer to the NET_MAP.
1107 @param[in] Item Pointer to the NET_MAP_ITEM.
1108 @param[in] Arg Pointer to the token to be cancelled, if NULL,
1109 the token specified by Item is cancelled.
1111 @retval EFI_SUCCESS The token is cancelled if Arg is NULL or the token
1112 is not the same as that in the Item if Arg is not
1114 @retval EFI_ABORTED Arg is not NULL, and the token specified by Arg is
1122 IN NET_MAP_ITEM
*Item
,
1123 IN VOID
*Arg OPTIONAL
1126 EFI_UDP4_COMPLETION_TOKEN
*TokenToCancel
;
1130 if ((Arg
!= NULL
) && (Item
->Key
!= Arg
)) {
1134 if (Item
->Value
!= NULL
) {
1136 // If the token is a transmit token, the corresponding Packet is recorded in
1137 // Item->Value, invoke IpIo to cancel this packet first. The IpIoCancelTxToken
1138 // will invoke Udp4DgramSent, the token will be signaled and this Item will
1139 // be removed from the Map there.
1141 Packet
= (NET_BUF
*)(Item
->Value
);
1142 IpIo
= (IP_IO
*)(*((UINTN
*)&Packet
->ProtoData
[0]));
1144 IpIoCancelTxToken (IpIo
, Packet
);
1147 // The token is a receive token. Abort it and remove it from the Map.
1149 TokenToCancel
= (EFI_UDP4_COMPLETION_TOKEN
*)Item
->Key
;
1150 NetMapRemoveItem (Map
, Item
, NULL
);
1152 TokenToCancel
->Status
= EFI_ABORTED
;
1153 gBS
->SignalEvent (TokenToCancel
->Event
);
1164 This function removes all the Wrap datas in the RcvdDgramQue.
1166 @param[in] Instance Pointer to the udp instance context data.
1170 Udp4FlushRcvdDgram (
1171 IN UDP4_INSTANCE_DATA
*Instance
1174 UDP4_RXDATA_WRAP
*Wrap
;
1176 while (!IsListEmpty (&Instance
->RcvdDgramQue
)) {
1178 // Iterate all the Wraps in the RcvdDgramQue.
1180 Wrap
= NET_LIST_HEAD (&Instance
->RcvdDgramQue
, UDP4_RXDATA_WRAP
, Link
);
1183 // The Wrap will be removed from the RcvdDgramQue by this function call.
1185 Udp4RecycleRxDataWrap (NULL
, (VOID
*)Wrap
);
1190 Cancel Udp4 tokens from the Udp4 instance.
1192 @param[in] Instance Pointer to the udp instance context data.
1193 @param[in] Token Pointer to the token to be canceled, if NULL, all
1194 tokens in this instance will be cancelled.
1196 @retval EFI_SUCCESS The Token is cancelled.
1197 @retval EFI_NOT_FOUND The Token is not found.
1201 Udp4InstanceCancelToken (
1202 IN UDP4_INSTANCE_DATA
*Instance
,
1203 IN EFI_UDP4_COMPLETION_TOKEN
*Token OPTIONAL
1209 // Cancel this token from the TxTokens map.
1211 Status
= NetMapIterate (&Instance
->TxTokens
, Udp4CancelTokens
, Token
);
1213 if ((Token
!= NULL
) && (Status
== EFI_ABORTED
)) {
1215 // If Token isn't NULL and Status is EFI_ABORTED, the token is cancelled from
1216 // the TxTokens, just return success.
1222 // Try to cancel this token from the RxTokens map in condition either the Token
1223 // is NULL or the specified Token is not in TxTokens.
1225 Status
= NetMapIterate (&Instance
->RxTokens
, Udp4CancelTokens
, Token
);
1227 if ((Token
!= NULL
) && (Status
== EFI_SUCCESS
)) {
1229 // If Token isn't NULL and Status is EFI_SUCCESS, the token is neither in the
1230 // TxTokens nor the RxTokens, or say, it's not found.
1232 return EFI_NOT_FOUND
;
1236 (Token
!= NULL
) || ( (0 == NetMapGetCount (&Instance
->TxTokens
))
1237 && (0 == NetMapGetCount (&Instance
->RxTokens
)))
1244 This function matches the received udp datagram with the Instance.
1246 @param[in] Instance Pointer to the udp instance context data.
1247 @param[in] Udp4Session Pointer to the EFI_UDP4_SESSION_DATA abstracted
1248 from the received udp datagram.
1250 @retval TRUE The udp datagram matches the receiving requirements of the
1252 @retval FALSE Otherwise.
1257 IN UDP4_INSTANCE_DATA
*Instance
,
1258 IN EFI_UDP4_SESSION_DATA
*Udp4Session
1261 EFI_UDP4_CONFIG_DATA
*ConfigData
;
1262 IP4_ADDR Destination
;
1264 ConfigData
= &Instance
->ConfigData
;
1266 if (ConfigData
->AcceptPromiscuous
) {
1268 // Always matches if this instance is in the promiscuous state.
1273 if ((!ConfigData
->AcceptAnyPort
&& (Udp4Session
->DestinationPort
!= ConfigData
->StationPort
)) ||
1274 ((ConfigData
->RemotePort
!= 0) && (Udp4Session
->SourcePort
!= ConfigData
->RemotePort
))
1278 // The local port or the remote port doesn't match.
1283 if (!EFI_IP4_EQUAL (&ConfigData
->RemoteAddress
, &mZeroIp4Addr
) &&
1284 !EFI_IP4_EQUAL (&ConfigData
->RemoteAddress
, &Udp4Session
->SourceAddress
)
1288 // This datagram doesn't come from the instance's specified sender.
1293 if (EFI_IP4_EQUAL (&ConfigData
->StationAddress
, &mZeroIp4Addr
) ||
1294 EFI_IP4_EQUAL (&Udp4Session
->DestinationAddress
, &ConfigData
->StationAddress
)
1298 // The instance is configured to receive datagrams destined to any station IP or
1299 // the destination address of this datagram matches the configured station IP.
1304 CopyMem (&Destination
, &Udp4Session
->DestinationAddress
, sizeof (IP4_ADDR
));
1306 if (IP4_IS_LOCAL_BROADCAST (Destination
) && ConfigData
->AcceptBroadcast
) {
1308 // The instance is configured to receive broadcast and this is a broadcast packet.
1313 if (IP4_IS_MULTICAST (NTOHL (Destination
)) &&
1314 (NetMapFindKey (&Instance
->McastIps
, (VOID
*)(UINTN
)Destination
) != NULL
)
1318 // It's a multicast packet and the multicast address is accepted by this instance.
1327 This function removes the Wrap specified by Context and release relevant resources.
1329 @param[in] Event The Event this notify function registered to.
1330 @param[in] Context Pointer to the context data.
1335 Udp4RecycleRxDataWrap (
1340 UDP4_RXDATA_WRAP
*Wrap
;
1342 Wrap
= (UDP4_RXDATA_WRAP
*)Context
;
1345 // Remove the Wrap from the list it belongs to.
1347 RemoveEntryList (&Wrap
->Link
);
1350 // Free the Packet associated with this Wrap.
1352 NetbufFree (Wrap
->Packet
);
1357 gBS
->CloseEvent (Wrap
->RxData
.RecycleSignal
);
1363 This function wraps the Packet and the RxData.
1365 @param[in] Instance Pointer to the instance context data.
1366 @param[in] Packet Pointer to the buffer containing the received
1368 @param[in] RxData Pointer to the EFI_UDP4_RECEIVE_DATA of this
1371 @return Pointer to the structure wrapping the RxData and the Packet.
1376 IN UDP4_INSTANCE_DATA
*Instance
,
1378 IN EFI_UDP4_RECEIVE_DATA
*RxData
1382 UDP4_RXDATA_WRAP
*Wrap
;
1385 // Allocate buffer for the Wrap.
1387 Wrap
= AllocatePool (
1388 sizeof (UDP4_RXDATA_WRAP
) +
1389 (Packet
->BlockOpNum
- 1) * sizeof (EFI_UDP4_FRAGMENT_DATA
)
1395 InitializeListHead (&Wrap
->Link
);
1397 CopyMem (&Wrap
->RxData
, RxData
, sizeof (Wrap
->RxData
));
1400 // Create the Recycle event.
1402 Status
= gBS
->CreateEvent (
1405 Udp4RecycleRxDataWrap
,
1407 &Wrap
->RxData
.RecycleSignal
1409 if (EFI_ERROR (Status
)) {
1414 Wrap
->Packet
= Packet
;
1415 Wrap
->TimeoutTick
= Instance
->ConfigData
.ReceiveTimeout
;
1421 This function enqueues the received datagram into the instances' receiving queues.
1423 @param[in] Udp4Service Pointer to the udp service context data.
1424 @param[in] Packet Pointer to the buffer containing the received
1426 @param[in] RxData Pointer to the EFI_UDP4_RECEIVE_DATA of this
1429 @return The times this datagram is enqueued.
1434 IN UDP4_SERVICE_DATA
*Udp4Service
,
1436 IN EFI_UDP4_RECEIVE_DATA
*RxData
1440 UDP4_INSTANCE_DATA
*Instance
;
1441 UDP4_RXDATA_WRAP
*Wrap
;
1446 NET_LIST_FOR_EACH (Entry
, &Udp4Service
->ChildrenList
) {
1448 // Iterate the instances.
1450 Instance
= NET_LIST_USER_STRUCT (Entry
, UDP4_INSTANCE_DATA
, Link
);
1452 if (!Instance
->Configured
) {
1456 if (Udp4MatchDgram (Instance
, &RxData
->UdpSession
)) {
1458 // Wrap the RxData and put this Wrap into the instances RcvdDgramQue.
1460 Wrap
= Udp4WrapRxData (Instance
, Packet
, RxData
);
1465 NET_GET_REF (Packet
);
1467 InsertTailList (&Instance
->RcvdDgramQue
, &Wrap
->Link
);
1477 This function delivers the received datagrams for the specified instance.
1479 @param[in] Instance Pointer to the instance context data.
1483 Udp4InstanceDeliverDgram (
1484 IN UDP4_INSTANCE_DATA
*Instance
1487 UDP4_RXDATA_WRAP
*Wrap
;
1488 EFI_UDP4_COMPLETION_TOKEN
*Token
;
1490 EFI_UDP4_RECEIVE_DATA
*RxData
;
1493 if (!IsListEmpty (&Instance
->RcvdDgramQue
) &&
1494 !NetMapIsEmpty (&Instance
->RxTokens
))
1496 Wrap
= NET_LIST_HEAD (&Instance
->RcvdDgramQue
, UDP4_RXDATA_WRAP
, Link
);
1498 if (NET_BUF_SHARED (Wrap
->Packet
)) {
1500 // Duplicate the Packet if it is shared between instances.
1502 Dup
= NetbufDuplicate (Wrap
->Packet
, NULL
, 0);
1507 NetbufFree (Wrap
->Packet
);
1512 NetListRemoveHead (&Instance
->RcvdDgramQue
);
1514 Token
= (EFI_UDP4_COMPLETION_TOKEN
*)NetMapRemoveHead (&Instance
->RxTokens
, NULL
);
1517 // Build the FragmentTable and set the FragmentCount in RxData.
1519 RxData
= &Wrap
->RxData
;
1520 RxData
->FragmentCount
= Wrap
->Packet
->BlockOpNum
;
1524 (NET_FRAGMENT
*)RxData
->FragmentTable
,
1525 &RxData
->FragmentCount
1528 Token
->Status
= EFI_SUCCESS
;
1529 Token
->Packet
.RxData
= &Wrap
->RxData
;
1531 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1532 InsertTailList (&Instance
->DeliveredDgramQue
, &Wrap
->Link
);
1533 gBS
->RestoreTPL (OldTpl
);
1535 gBS
->SignalEvent (Token
->Event
);
1540 This function delivers the datagrams enqueued in the instances.
1542 @param[in] Udp4Service Pointer to the udp service context data.
1547 IN UDP4_SERVICE_DATA
*Udp4Service
1551 UDP4_INSTANCE_DATA
*Instance
;
1553 NET_LIST_FOR_EACH (Entry
, &Udp4Service
->ChildrenList
) {
1555 // Iterate the instances.
1557 Instance
= NET_LIST_USER_STRUCT (Entry
, UDP4_INSTANCE_DATA
, Link
);
1559 if (!Instance
->Configured
) {
1564 // Deliver the datagrams of this instance.
1566 Udp4InstanceDeliverDgram (Instance
);
1571 This function demultiplexes the received udp datagram to the appropriate instances.
1573 @param[in] Udp4Service Pointer to the udp service context data.
1574 @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA abstracted from
1575 the received datagram.
1576 @param[in] Packet Pointer to the buffer containing the received udp
1582 IN UDP4_SERVICE_DATA
*Udp4Service
,
1583 IN EFI_NET_SESSION_DATA
*NetSession
,
1587 EFI_UDP_HEADER
*Udp4Header
;
1589 EFI_UDP4_RECEIVE_DATA RxData
;
1590 EFI_UDP4_SESSION_DATA
*Udp4Session
;
1593 if (Packet
->TotalSize
< sizeof (EFI_UDP_HEADER
)) {
1594 NetbufFree (Packet
);
1599 // Get the datagram header from the packet buffer.
1601 Udp4Header
= (EFI_UDP_HEADER
*)NetbufGetByte (Packet
, 0, NULL
);
1602 ASSERT (Udp4Header
!= NULL
);
1604 if (Udp4Header
->Checksum
!= 0) {
1606 // check the checksum.
1608 HeadSum
= NetPseudoHeadChecksum (
1609 NetSession
->Source
.Addr
[0],
1610 NetSession
->Dest
.Addr
[0],
1615 if (Udp4Checksum (Packet
, HeadSum
) != 0) {
1619 NetbufFree (Packet
);
1624 Udp4Session
= &RxData
.UdpSession
;
1625 Udp4Session
->SourcePort
= NTOHS (Udp4Header
->SrcPort
);
1626 Udp4Session
->DestinationPort
= NTOHS (Udp4Header
->DstPort
);
1628 CopyMem (&Udp4Session
->SourceAddress
, &NetSession
->Source
, sizeof (EFI_IPv4_ADDRESS
));
1629 CopyMem (&Udp4Session
->DestinationAddress
, &NetSession
->Dest
, sizeof (EFI_IPv4_ADDRESS
));
1632 // Trim the UDP header.
1634 NetbufTrim (Packet
, UDP4_HEADER_SIZE
, TRUE
);
1636 RxData
.DataLength
= (UINT32
)Packet
->TotalSize
;
1639 // Try to enqueue this datagram into the instances.
1641 Enqueued
= Udp4EnqueueDgram (Udp4Service
, Packet
, &RxData
);
1643 if (Enqueued
== 0) {
1645 // Send the port unreachable ICMP packet before we free this NET_BUF
1647 Udp4SendPortUnreach (Udp4Service
->IpIo
, NetSession
, Udp4Header
);
1651 // Try to free the packet before deliver it.
1653 NetbufFree (Packet
);
1657 // Deliver the datagram.
1659 Udp4DeliverDgram (Udp4Service
);
1664 This function builds and sends out a icmp port unreachable message.
1666 @param[in] IpIo Pointer to the IP_IO instance.
1667 @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA of the packet
1668 causes this icmp error message.
1669 @param[in] Udp4Header Pointer to the udp header of the datagram causes
1670 this icmp error message.
1674 Udp4SendPortUnreach (
1676 IN EFI_NET_SESSION_DATA
*NetSession
,
1682 IP4_ICMP_ERROR_HEAD
*IcmpErrHdr
;
1683 EFI_IP4_HEADER
*IpHdr
;
1685 IP_IO_OVERRIDE Override
;
1686 IP_IO_IP_INFO
*IpSender
;
1688 IpSender
= IpIoFindSender (&IpIo
, NetSession
->IpVersion
, &NetSession
->Dest
);
1689 if (IpSender
== NULL
) {
1691 // No appropriate sender, since we cannot send out the ICMP message through
1692 // the default zero station address IP instance, abort.
1697 IpHdr
= NetSession
->IpHdr
.Ip4Hdr
;
1700 // Calculate the required length of the icmp error message.
1702 Len
= sizeof (IP4_ICMP_ERROR_HEAD
) + (EFI_IP4_HEADER_LEN (IpHdr
) -
1703 sizeof (IP4_HEAD
)) + ICMP_ERROR_PACKET_LENGTH
;
1706 // Allocate buffer for the icmp error message.
1708 Packet
= NetbufAlloc (Len
);
1709 if (Packet
== NULL
) {
1714 // Allocate space for the IP4_ICMP_ERROR_HEAD.
1716 IcmpErrHdr
= (IP4_ICMP_ERROR_HEAD
*)NetbufAllocSpace (Packet
, Len
, FALSE
);
1717 ASSERT (IcmpErrHdr
!= NULL
);
1720 // Set the required fields for the icmp port unreachable message.
1722 IcmpErrHdr
->Head
.Type
= ICMP_TYPE_UNREACH
;
1723 IcmpErrHdr
->Head
.Code
= ICMP_CODE_UNREACH_PORT
;
1724 IcmpErrHdr
->Head
.Checksum
= 0;
1725 IcmpErrHdr
->Fourth
= 0;
1728 // Copy the IP header of the datagram tragged the error.
1730 CopyMem (&IcmpErrHdr
->IpHead
, IpHdr
, EFI_IP4_HEADER_LEN (IpHdr
));
1733 // Copy the UDP header.
1735 Ptr
= (UINT8
*)&IcmpErrHdr
->IpHead
+ EFI_IP4_HEADER_LEN (IpHdr
);
1736 CopyMem (Ptr
, Udp4Header
, ICMP_ERROR_PACKET_LENGTH
);
1739 // Calculate the checksum.
1741 IcmpErrHdr
->Head
.Checksum
= (UINT16
) ~(NetbufChecksum (Packet
));
1744 // Fill the override data.
1746 Override
.Ip4OverrideData
.DoNotFragment
= FALSE
;
1747 Override
.Ip4OverrideData
.TypeOfService
= 0;
1748 Override
.Ip4OverrideData
.TimeToLive
= 255;
1749 Override
.Ip4OverrideData
.Protocol
= EFI_IP_PROTO_ICMP
;
1751 CopyMem (&Override
.Ip4OverrideData
.SourceAddress
, &NetSession
->Dest
, sizeof (EFI_IPv4_ADDRESS
));
1752 ZeroMem (&Override
.Ip4OverrideData
.GatewayAddress
, sizeof (EFI_IPv4_ADDRESS
));
1755 // Send out this icmp packet.
1757 IpIoSend (IpIo
, Packet
, IpSender
, NULL
, NULL
, &NetSession
->Source
, &Override
);
1759 NetbufFree (Packet
);
1763 This function handles the received Icmp Error message and demultiplexes it to the
1766 @param[in] Udp4Service Pointer to the udp service context data.
1767 @param[in] IcmpError The icmp error code.
1768 @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA abstracted
1769 from the received Icmp Error packet.
1770 @param[in] Packet Pointer to the Icmp Error packet.
1775 IN UDP4_SERVICE_DATA
*Udp4Service
,
1777 IN EFI_NET_SESSION_DATA
*NetSession
,
1781 EFI_UDP_HEADER
*Udp4Header
;
1782 EFI_UDP4_SESSION_DATA Udp4Session
;
1784 UDP4_INSTANCE_DATA
*Instance
;
1786 if (Packet
->TotalSize
< sizeof (EFI_UDP_HEADER
)) {
1787 NetbufFree (Packet
);
1791 Udp4Header
= (EFI_UDP_HEADER
*)NetbufGetByte (Packet
, 0, NULL
);
1792 ASSERT (Udp4Header
!= NULL
);
1794 CopyMem (&Udp4Session
.SourceAddress
, &NetSession
->Source
, sizeof (EFI_IPv4_ADDRESS
));
1795 CopyMem (&Udp4Session
.DestinationAddress
, &NetSession
->Dest
, sizeof (EFI_IPv4_ADDRESS
));
1797 Udp4Session
.SourcePort
= NTOHS (Udp4Header
->DstPort
);
1798 Udp4Session
.DestinationPort
= NTOHS (Udp4Header
->SrcPort
);
1800 NET_LIST_FOR_EACH (Entry
, &Udp4Service
->ChildrenList
) {
1802 // Iterate all the instances.
1804 Instance
= NET_LIST_USER_STRUCT (Entry
, UDP4_INSTANCE_DATA
, Link
);
1806 if (!Instance
->Configured
) {
1810 if (Udp4MatchDgram (Instance
, &Udp4Session
)) {
1812 // Translate the Icmp Error code according to the udp spec.
1814 Instance
->IcmpError
= IpIoGetIcmpErrStatus (IcmpError
, IP_VERSION_4
, NULL
, NULL
);
1816 if (IcmpError
> ICMP_ERR_UNREACH_PORT
) {
1817 Instance
->IcmpError
= EFI_ICMP_ERROR
;
1821 // Notify the instance with the received Icmp Error.
1823 Udp4ReportIcmpError (Instance
);
1829 NetbufFree (Packet
);
1833 This function reports the received ICMP error.
1835 @param[in] Instance Pointer to the udp instance context data.
1839 Udp4ReportIcmpError (
1840 IN UDP4_INSTANCE_DATA
*Instance
1843 EFI_UDP4_COMPLETION_TOKEN
*Token
;
1845 if (NetMapIsEmpty (&Instance
->RxTokens
)) {
1847 // There are no receive tokens to deliver the ICMP error.
1852 if (EFI_ERROR (Instance
->IcmpError
)) {
1854 // Try to get a RxToken from the RxTokens map.
1856 Token
= (EFI_UDP4_COMPLETION_TOKEN
*)NetMapRemoveHead (&Instance
->RxTokens
, NULL
);
1858 if (Token
!= NULL
) {
1860 // Report the error through the Token.
1862 Token
->Status
= Instance
->IcmpError
;
1863 gBS
->SignalEvent (Token
->Event
);
1866 // Clear the IcmpError.
1868 Instance
->IcmpError
= EFI_SUCCESS
;
1874 This function is a dummy ext-free function for the NET_BUF created for the output
1877 @param[in] Context Pointer to the context data.
1882 Udp4NetVectorExtFree (