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
12 UINT16 mUdp4RandomPort
;
15 This function checks and timeouts the I/O datagrams holding by the corresponding
18 @param[in] Event The event this function registered to.
19 @param[in] Context The context data registered during the creation of
31 This function finds the udp instance by the specified <Address, Port> pair.
33 @param[in] InstanceList Pointer to the head of the list linking the udp
35 @param[in] Address Pointer to the specified IPv4 address.
36 @param[in] Port The udp port number.
38 @retval TRUE The specified <Address, Port> pair is found.
39 @retval FALSE Otherwise.
43 Udp4FindInstanceByPort (
44 IN LIST_ENTRY
*InstanceList
,
45 IN EFI_IPv4_ADDRESS
*Address
,
50 This function is the packet transmitting notify function registered to the IpIo
51 interface. It's called to signal the udp TxToken when IpIo layer completes the
52 transmitting of the udp datagram.
54 @param[in] Status The completion status of the output udp datagram.
55 @param[in] Context Pointer to the context data.
56 @param[in] Sender Specify a pointer of EFI_IP4_PROTOCOL for sending.
57 @param[in] NotifyData Pointer to the notify data.
65 IN IP_IO_IP_PROTOCOL Sender
,
70 This function processes the received datagram passed up by the IpIo layer.
72 @param[in] Status The status of this udp datagram.
73 @param[in] IcmpError The IcmpError code, only available when Status is
75 @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA.
76 @param[in] Packet Pointer to the NET_BUF containing the received udp
78 @param[in] Context Pointer to the context data.
86 IN EFI_NET_SESSION_DATA
*NetSession
,
92 This function cancels the token specified by Arg in the Map. This is a callback
93 used by Udp4InstanceCancelToken().
95 @param[in] Map Pointer to the NET_MAP.
96 @param[in] Item Pointer to the NET_MAP_ITEM.
97 @param[in] Arg Pointer to the token to be cancelled, if NULL,
98 the token specified by Item is cancelled.
100 @retval EFI_SUCCESS The token is cancelled if Arg is NULL or the token
101 is not the same as that in the Item if Arg is not
103 @retval EFI_ABORTED Arg is not NULL, and the token specified by Arg is
111 IN NET_MAP_ITEM
*Item
,
112 IN VOID
*Arg OPTIONAL
116 This function matches the received udp datagram with the Instance.
118 @param[in] Instance Pointer to the udp instance context data.
119 @param[in] Udp4Session Pointer to the EFI_UDP4_SESSION_DATA abstracted
120 from the received udp datagram.
122 @retval TRUE The udp datagram matches the receiving requirments of the
124 @retval FALSE Otherwise.
129 IN UDP4_INSTANCE_DATA
*Instance
,
130 IN EFI_UDP4_SESSION_DATA
*Udp4Session
134 This function removes the Wrap specified by Context and release relevant resources.
136 @param[in] Event The Event this notify function registered to.
137 @param[in] Context Pointer to the context data.
142 Udp4RecycleRxDataWrap (
148 This function wraps the Packet and the RxData.
150 @param[in] Instance Pointer to the instance context data.
151 @param[in] Packet Pointer to the buffer containing the received
153 @param[in] RxData Pointer to the EFI_UDP4_RECEIVE_DATA of this
156 @return Pointer to the structure wrapping the RxData and the Packet.
161 IN UDP4_INSTANCE_DATA
*Instance
,
163 IN EFI_UDP4_RECEIVE_DATA
*RxData
167 This function enqueues the received datagram into the instances' receiving queues.
169 @param[in] Udp4Service Pointer to the udp service context data.
170 @param[in] Packet Pointer to the buffer containing the received
172 @param[in] RxData Pointer to the EFI_UDP4_RECEIVE_DATA of this
175 @return The times this datagram is enqueued.
180 IN UDP4_SERVICE_DATA
*Udp4Service
,
182 IN EFI_UDP4_RECEIVE_DATA
*RxData
186 This function delivers the datagrams enqueued in the instances.
188 @param[in] Udp4Service Pointer to the udp service context data.
193 IN UDP4_SERVICE_DATA
*Udp4Service
197 This function demultiplexes the received udp datagram to the appropriate instances.
199 @param[in] Udp4Service Pointer to the udp service context data.
200 @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA abstracted from
201 the received datagram.
202 @param[in] Packet Pointer to the buffer containing the received udp
208 IN UDP4_SERVICE_DATA
*Udp4Service
,
209 IN EFI_NET_SESSION_DATA
*NetSession
,
214 This function handles the received Icmp Error message and demultiplexes it to the
217 @param[in] Udp4Service Pointer to the udp service context data.
218 @param[in] IcmpError The icmp error code.
219 @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA abstracted
220 from the received Icmp Error packet.
221 @param[in] Packet Pointer to the Icmp Error packet.
226 IN UDP4_SERVICE_DATA
*Udp4Service
,
228 IN EFI_NET_SESSION_DATA
*NetSession
,
233 This function builds and sends out a icmp port unreachable message.
235 @param[in] IpIo Pointer to the IP_IO instance.
236 @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA of the packet
237 causes this icmp error message.
238 @param[in] Udp4Header Pointer to the udp header of the datagram causes
239 this icmp error message.
243 Udp4SendPortUnreach (
245 IN EFI_NET_SESSION_DATA
*NetSession
,
251 Create the Udp service context data.
253 @param[in, out] Udp4Service Pointer to the UDP4_SERVICE_DATA.
254 @param[in] ImageHandle The image handle of this udp4 driver.
255 @param[in] ControllerHandle The controller handle this udp4 driver binds on.
257 @retval EFI_SUCCESS The udp4 service context data is created and
259 @retval EFI_OUT_OF_RESOURCES Cannot allocate memory.
260 @retval other Other error occurs.
265 IN OUT UDP4_SERVICE_DATA
*Udp4Service
,
266 IN EFI_HANDLE ImageHandle
,
267 IN EFI_HANDLE ControllerHandle
271 IP_IO_OPEN_DATA OpenData
;
272 EFI_IP4_CONFIG_DATA
*Ip4ConfigData
;
274 ZeroMem (Udp4Service
, sizeof (UDP4_SERVICE_DATA
));
276 Udp4Service
->Signature
= UDP4_SERVICE_DATA_SIGNATURE
;
277 Udp4Service
->ServiceBinding
= mUdp4ServiceBinding
;
278 Udp4Service
->ImageHandle
= ImageHandle
;
279 Udp4Service
->ControllerHandle
= ControllerHandle
;
280 Udp4Service
->ChildrenNumber
= 0;
282 InitializeListHead (&Udp4Service
->ChildrenList
);
285 // Create the IpIo for this service context.
287 Udp4Service
->IpIo
= IpIoCreate (ImageHandle
, ControllerHandle
, IP_VERSION_4
);
288 if (Udp4Service
->IpIo
== NULL
) {
289 return EFI_OUT_OF_RESOURCES
;
293 // Set the OpenData used to open the IpIo.
295 Ip4ConfigData
= &OpenData
.IpConfigData
.Ip4CfgData
;
296 CopyMem (Ip4ConfigData
, &mIp4IoDefaultIpConfigData
, sizeof (EFI_IP4_CONFIG_DATA
));
297 Ip4ConfigData
->AcceptBroadcast
= TRUE
;
298 OpenData
.RcvdContext
= (VOID
*) Udp4Service
;
299 OpenData
.SndContext
= NULL
;
300 OpenData
.PktRcvdNotify
= Udp4DgramRcvd
;
301 OpenData
.PktSentNotify
= Udp4DgramSent
;
304 // Configure and start the IpIo.
306 Status
= IpIoOpen (Udp4Service
->IpIo
, &OpenData
);
307 if (EFI_ERROR (Status
)) {
312 // Create the event for Udp timeout checking.
314 Status
= gBS
->CreateEvent (
315 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
319 &Udp4Service
->TimeoutEvent
321 if (EFI_ERROR (Status
)) {
326 // Start the timeout timer event.
328 Status
= gBS
->SetTimer (
329 Udp4Service
->TimeoutEvent
,
331 UDP4_TIMEOUT_INTERVAL
333 if (EFI_ERROR (Status
)) {
341 if (Udp4Service
->TimeoutEvent
!= NULL
) {
342 gBS
->CloseEvent (Udp4Service
->TimeoutEvent
);
345 IpIoDestroy (Udp4Service
->IpIo
);
352 Clean the Udp service context data.
354 @param[in] Udp4Service Pointer to the UDP4_SERVICE_DATA.
359 IN UDP4_SERVICE_DATA
*Udp4Service
363 // Cancel the TimeoutEvent timer.
365 gBS
->SetTimer (Udp4Service
->TimeoutEvent
, TimerCancel
, 0);
368 // Close the TimeoutEvent timer.
370 gBS
->CloseEvent (Udp4Service
->TimeoutEvent
);
375 IpIoDestroy (Udp4Service
->IpIo
);
380 This function checks and timeouts the I/O datagrams holding by the corresponding
383 @param[in] Event The event this function registered to.
384 @param[in] Context The context data registered during the creation of
395 UDP4_SERVICE_DATA
*Udp4Service
;
397 UDP4_INSTANCE_DATA
*Instance
;
398 LIST_ENTRY
*WrapEntry
;
399 LIST_ENTRY
*NextEntry
;
400 UDP4_RXDATA_WRAP
*Wrap
;
402 Udp4Service
= (UDP4_SERVICE_DATA
*) Context
;
403 NET_CHECK_SIGNATURE (Udp4Service
, UDP4_SERVICE_DATA_SIGNATURE
);
405 NET_LIST_FOR_EACH (Entry
, &Udp4Service
->ChildrenList
) {
407 // Iterate all the instances belonging to this service context.
409 Instance
= NET_LIST_USER_STRUCT (Entry
, UDP4_INSTANCE_DATA
, Link
);
410 NET_CHECK_SIGNATURE (Instance
, UDP4_INSTANCE_DATA_SIGNATURE
);
412 if (!Instance
->Configured
|| (Instance
->ConfigData
.ReceiveTimeout
== 0)) {
414 // Skip this instance if it's not configured or no receive timeout.
419 NET_LIST_FOR_EACH_SAFE (WrapEntry
, NextEntry
, &Instance
->RcvdDgramQue
) {
421 // Iterate all the rxdatas belonging to this udp instance.
423 Wrap
= NET_LIST_USER_STRUCT (WrapEntry
, UDP4_RXDATA_WRAP
, Link
);
426 // TimeoutTick unit is microsecond, MNP_TIMEOUT_CHECK_INTERVAL unit is 100ns.
428 if (Wrap
->TimeoutTick
< (UDP4_TIMEOUT_INTERVAL
/ 10)) {
430 // Remove this RxData if it timeouts.
432 Udp4RecycleRxDataWrap (NULL
, (VOID
*) Wrap
);
434 Wrap
->TimeoutTick
-= (UDP4_TIMEOUT_INTERVAL
/ 10);
442 This function intializes the new created udp instance.
444 @param[in] Udp4Service Pointer to the UDP4_SERVICE_DATA.
445 @param[in, out] Instance Pointer to the un-initialized UDP4_INSTANCE_DATA.
450 IN UDP4_SERVICE_DATA
*Udp4Service
,
451 IN OUT UDP4_INSTANCE_DATA
*Instance
455 // Set the signature.
457 Instance
->Signature
= UDP4_INSTANCE_DATA_SIGNATURE
;
462 InitializeListHead (&Instance
->Link
);
463 InitializeListHead (&Instance
->RcvdDgramQue
);
464 InitializeListHead (&Instance
->DeliveredDgramQue
);
467 // Init the NET_MAPs.
469 NetMapInit (&Instance
->TxTokens
);
470 NetMapInit (&Instance
->RxTokens
);
471 NetMapInit (&Instance
->McastIps
);
474 // Save the pointer to the UDP4_SERVICE_DATA, and initialize other members.
476 Instance
->Udp4Service
= Udp4Service
;
477 CopyMem (&Instance
->Udp4Proto
, &mUdp4Protocol
, sizeof (Instance
->Udp4Proto
));
478 Instance
->IcmpError
= EFI_SUCCESS
;
479 Instance
->Configured
= FALSE
;
480 Instance
->IsNoMapping
= FALSE
;
481 Instance
->InDestroy
= FALSE
;
486 This function cleans the udp instance.
488 @param[in] Instance Pointer to the UDP4_INSTANCE_DATA to clean.
493 IN UDP4_INSTANCE_DATA
*Instance
496 NetMapClean (&Instance
->McastIps
);
497 NetMapClean (&Instance
->RxTokens
);
498 NetMapClean (&Instance
->TxTokens
);
503 This function finds the udp instance by the specified <Address, Port> pair.
505 @param[in] InstanceList Pointer to the head of the list linking the udp
507 @param[in] Address Pointer to the specified IPv4 address.
508 @param[in] Port The udp port number.
510 @retval TRUE The specified <Address, Port> pair is found.
511 @retval FALSE Otherwise.
515 Udp4FindInstanceByPort (
516 IN LIST_ENTRY
*InstanceList
,
517 IN EFI_IPv4_ADDRESS
*Address
,
522 UDP4_INSTANCE_DATA
*Instance
;
523 EFI_UDP4_CONFIG_DATA
*ConfigData
;
525 NET_LIST_FOR_EACH (Entry
, InstanceList
) {
527 // Iterate all the udp instances.
529 Instance
= NET_LIST_USER_STRUCT (Entry
, UDP4_INSTANCE_DATA
, Link
);
530 ConfigData
= &Instance
->ConfigData
;
532 if (!Instance
->Configured
|| ConfigData
->AcceptAnyPort
) {
534 // If the instance is not configured or the configdata of the instance indicates
535 // this instance accepts any port, skip it.
540 if (EFI_IP4_EQUAL (&ConfigData
->StationAddress
, Address
) &&
541 (ConfigData
->StationPort
== Port
)) {
543 // if both the address and the port are the same, return TRUE.
550 // return FALSE when matching fails.
557 This function tries to bind the udp instance according to the configured port
560 @param[in] InstanceList Pointer to the head of the list linking the udp
562 @param[in, out] ConfigData Pointer to the ConfigData of the instance to be
563 bound. ConfigData->StationPort will be assigned
564 with an available port value on success.
566 @retval EFI_SUCCESS The bound operation is completed successfully.
567 @retval EFI_ACCESS_DENIED The <Address, Port> specified by the ConfigData is
568 already used by other instance.
569 @retval EFI_OUT_OF_RESOURCES No available port resources.
574 IN LIST_ENTRY
*InstanceList
,
575 IN OUT EFI_UDP4_CONFIG_DATA
*ConfigData
578 EFI_IPv4_ADDRESS
*StationAddress
;
581 if (ConfigData
->AcceptAnyPort
) {
585 StationAddress
= &ConfigData
->StationAddress
;
587 if (ConfigData
->StationPort
!= 0) {
589 if (!ConfigData
->AllowDuplicatePort
&&
590 Udp4FindInstanceByPort (InstanceList
, StationAddress
, ConfigData
->StationPort
)) {
592 // Do not allow duplicate port and the port is already used by other instance.
594 return EFI_ACCESS_DENIED
;
598 // select a random port for this instance;
601 if (ConfigData
->AllowDuplicatePort
) {
603 // Just pick up the random port if the instance allows duplicate port.
605 ConfigData
->StationPort
= mUdp4RandomPort
;
608 StartPort
= mUdp4RandomPort
;
610 while (Udp4FindInstanceByPort(InstanceList
, StationAddress
, mUdp4RandomPort
)) {
613 if (mUdp4RandomPort
== 0) {
614 mUdp4RandomPort
= UDP4_PORT_KNOWN
;
617 if (mUdp4RandomPort
== StartPort
) {
619 // No available port.
621 return EFI_OUT_OF_RESOURCES
;
625 ConfigData
->StationPort
= mUdp4RandomPort
;
629 if (mUdp4RandomPort
== 0) {
630 mUdp4RandomPort
= UDP4_PORT_KNOWN
;
639 This function is used to check whether the NewConfigData has any un-reconfigurable
640 parameters changed compared to the OldConfigData.
642 @param[in] OldConfigData Pointer to the current ConfigData the udp instance
644 @param[in] NewConfigData Pointer to the new ConfigData.
646 @retval TRUE The instance is reconfigurable.
647 @retval FALSE Otherwise.
651 Udp4IsReconfigurable (
652 IN EFI_UDP4_CONFIG_DATA
*OldConfigData
,
653 IN EFI_UDP4_CONFIG_DATA
*NewConfigData
656 if ((NewConfigData
->AcceptAnyPort
!= OldConfigData
->AcceptAnyPort
) ||
657 (NewConfigData
->AcceptBroadcast
!= OldConfigData
->AcceptBroadcast
) ||
658 (NewConfigData
->AcceptPromiscuous
!= OldConfigData
->AcceptPromiscuous
) ||
659 (NewConfigData
->AllowDuplicatePort
!= OldConfigData
->AllowDuplicatePort
)
662 // The receiving filter parameters cannot be changed.
667 if ((!NewConfigData
->AcceptAnyPort
) &&
668 (NewConfigData
->StationPort
!= OldConfigData
->StationPort
)
671 // The port is not changeable.
676 if (!NewConfigData
->AcceptPromiscuous
) {
678 if (NewConfigData
->UseDefaultAddress
!= OldConfigData
->UseDefaultAddress
) {
680 // The NewConfigData differs to the old one on the UseDefaultAddress.
685 if (!NewConfigData
->UseDefaultAddress
&&
686 (!EFI_IP4_EQUAL (&NewConfigData
->StationAddress
, &OldConfigData
->StationAddress
) ||
687 !EFI_IP4_EQUAL (&NewConfigData
->SubnetMask
, &OldConfigData
->SubnetMask
))
690 // If the instance doesn't use the default address, and the new address or
691 // new subnet mask is different from the old values.
697 if (!EFI_IP4_EQUAL (&NewConfigData
->RemoteAddress
, &OldConfigData
->RemoteAddress
)) {
699 // The remoteaddress is not the same.
704 if (!EFI_IP4_EQUAL (&NewConfigData
->RemoteAddress
, &mZeroIp4Addr
) &&
705 NewConfigData
->RemotePort
!= OldConfigData
->RemotePort
708 // The RemotePort differs if it's designated in the configdata.
714 // All checks pass, return TRUE.
721 This function builds the Ip4 configdata from the Udp4ConfigData.
723 @param[in] Udp4ConfigData Pointer to the EFI_UDP4_CONFIG_DATA.
724 @param[in, out] Ip4ConfigData Pointer to the EFI_IP4_CONFIG_DATA.
728 Udp4BuildIp4ConfigData (
729 IN EFI_UDP4_CONFIG_DATA
*Udp4ConfigData
,
730 IN OUT EFI_IP4_CONFIG_DATA
*Ip4ConfigData
733 CopyMem (Ip4ConfigData
, &mIp4IoDefaultIpConfigData
, sizeof (*Ip4ConfigData
));
735 Ip4ConfigData
->DefaultProtocol
= EFI_IP_PROTO_UDP
;
736 Ip4ConfigData
->AcceptBroadcast
= Udp4ConfigData
->AcceptBroadcast
;
737 Ip4ConfigData
->AcceptPromiscuous
= Udp4ConfigData
->AcceptPromiscuous
;
738 Ip4ConfigData
->UseDefaultAddress
= Udp4ConfigData
->UseDefaultAddress
;
739 CopyMem (&Ip4ConfigData
->StationAddress
, &Udp4ConfigData
->StationAddress
, sizeof (EFI_IPv4_ADDRESS
));
740 CopyMem (&Ip4ConfigData
->SubnetMask
, &Udp4ConfigData
->SubnetMask
, sizeof (EFI_IPv4_ADDRESS
));
743 // use the -1 magic number to disable the receiving process of the ip instance.
745 Ip4ConfigData
->ReceiveTimeout
= (UINT32
) (-1);
750 This function validates the TxToken, it returns the error code according to the spec.
752 @param[in] Instance Pointer to the udp instance context data.
753 @param[in] TxToken Pointer to the token to be checked.
755 @retval EFI_SUCCESS The TxToken is valid.
756 @retval EFI_INVALID_PARAMETER One or more of the following are TRUE: This is
757 NULL. Token is NULL. Token.Event is NULL.
758 Token.Packet.TxData is NULL.
759 Token.Packet.TxData.FragmentCount is zero.
760 Token.Packet.TxData.DataLength is not equal to the
761 sum of fragment lengths. One or more of the
762 Token.Packet.TxData.FragmentTable[].
763 FragmentLength fields is zero. One or more of the
764 Token.Packet.TxData.FragmentTable[].
765 FragmentBuffer fields is NULL.
766 Token.Packet.TxData. GatewayAddress is not a
767 unicast IPv4 address if it is not NULL. One or
768 more IPv4 addresses in Token.Packet.TxData.
769 UdpSessionData are not valid unicast IPv4
770 addresses if the UdpSessionData is not NULL.
771 @retval EFI_BAD_BUFFER_SIZE The data length is greater than the maximum UDP
776 Udp4ValidateTxToken (
777 IN UDP4_INSTANCE_DATA
*Instance
,
778 IN EFI_UDP4_COMPLETION_TOKEN
*TxToken
781 EFI_UDP4_TRANSMIT_DATA
*TxData
;
784 EFI_UDP4_CONFIG_DATA
*ConfigData
;
785 EFI_UDP4_SESSION_DATA
*UdpSessionData
;
786 IP4_ADDR SourceAddress
;
787 IP4_ADDR GatewayAddress
;
789 if (TxToken
->Event
== NULL
) {
790 return EFI_INVALID_PARAMETER
;
793 TxData
= TxToken
->Packet
.TxData
;
795 if ((TxData
== NULL
) || (TxData
->FragmentCount
== 0)) {
796 return EFI_INVALID_PARAMETER
;
800 for (Index
= 0; Index
< TxData
->FragmentCount
; Index
++) {
802 if ((TxData
->FragmentTable
[Index
].FragmentBuffer
== NULL
) ||
803 (TxData
->FragmentTable
[Index
].FragmentLength
== 0)) {
805 // if the FragmentBuffer is NULL or the FragmentLeng is zero.
807 return EFI_INVALID_PARAMETER
;
810 TotalLen
+= TxData
->FragmentTable
[Index
].FragmentLength
;
813 if (TotalLen
!= TxData
->DataLength
) {
815 // The TotalLen calculated by adding all the FragmentLeng doesn't equal to the
818 return EFI_INVALID_PARAMETER
;
821 if (TxData
->GatewayAddress
!= NULL
) {
822 CopyMem (&GatewayAddress
, TxData
->GatewayAddress
, sizeof (IP4_ADDR
));
824 if (!Instance
->ConfigData
.UseDefaultAddress
&&
825 (EFI_NTOHL(Instance
->ConfigData
.SubnetMask
) != 0) &&
826 !NetIp4IsUnicast (NTOHL (GatewayAddress
), EFI_NTOHL(Instance
->ConfigData
.SubnetMask
))) {
828 // The specified GatewayAddress is not a unicast IPv4 address while it's not 0.
830 return EFI_INVALID_PARAMETER
;
834 ConfigData
= &Instance
->ConfigData
;
835 UdpSessionData
= TxData
->UdpSessionData
;
837 if (UdpSessionData
!= NULL
) {
839 CopyMem (&SourceAddress
, &UdpSessionData
->SourceAddress
, sizeof (IP4_ADDR
));
841 if ((SourceAddress
!= 0) &&
842 !Instance
->ConfigData
.UseDefaultAddress
&&
843 (EFI_NTOHL(Instance
->ConfigData
.SubnetMask
) != 0) &&
844 !NetIp4IsUnicast (HTONL (SourceAddress
), EFI_NTOHL(Instance
->ConfigData
.SubnetMask
))) {
846 // Check whether SourceAddress is a valid IPv4 address in case it's not zero.
847 // The configured station address is used if SourceAddress is zero.
849 return EFI_INVALID_PARAMETER
;
852 if ((UdpSessionData
->DestinationPort
== 0) && (ConfigData
->RemotePort
== 0)) {
854 // Ambiguous, no avalaible DestinationPort for this token.
856 return EFI_INVALID_PARAMETER
;
859 if (EFI_IP4_EQUAL (&UdpSessionData
->DestinationAddress
, &mZeroIp4Addr
)) {
861 // The DestinationAddress specified in the UdpSessionData is 0.
863 return EFI_INVALID_PARAMETER
;
865 } else if (EFI_IP4_EQUAL (&ConfigData
->RemoteAddress
, &mZeroIp4Addr
)) {
867 // the configured RemoteAddress is all zero, and the user doens't override the
868 // destination address.
870 return EFI_INVALID_PARAMETER
;
873 if (TxData
->DataLength
> UDP4_MAX_DATA_SIZE
) {
874 return EFI_BAD_BUFFER_SIZE
;
882 This function checks whether the specified Token duplicates with the one in the Map.
884 @param[in] Map Pointer to the NET_MAP.
885 @param[in] Item Pointer to the NET_MAP_ITEM contain the pointer to
887 @param[in] Context Pointer to the Token to be checked.
889 @retval EFI_SUCCESS The Token specified by Context differs from the
891 @retval EFI_ACCESS_DENIED The Token duplicates with the one in the Item.
898 IN NET_MAP_ITEM
*Item
,
902 EFI_UDP4_COMPLETION_TOKEN
*Token
;
903 EFI_UDP4_COMPLETION_TOKEN
*TokenInItem
;
905 Token
= (EFI_UDP4_COMPLETION_TOKEN
*) Context
;
906 TokenInItem
= (EFI_UDP4_COMPLETION_TOKEN
*) Item
->Key
;
908 if ((Token
== TokenInItem
) || (Token
->Event
== TokenInItem
->Event
)) {
910 // The Token duplicates with the TokenInItem in case either the two pointers are the
911 // same or the Events of these two tokens are the same.
913 return EFI_ACCESS_DENIED
;
921 This function calculates the checksum for the Packet, utilizing the pre-calculated
922 pseudo HeadSum to reduce some overhead.
924 @param[in] Packet Pointer to the NET_BUF contains the udp datagram.
925 @param[in] HeadSum Checksum of the pseudo header execpt the length
928 @retval The 16-bit checksum of this udp datagram.
939 Checksum
= NetbufChecksum (Packet
);
940 Checksum
= NetAddChecksum (Checksum
, HeadSum
);
942 Checksum
= NetAddChecksum (Checksum
, HTONS ((UINT16
) Packet
->TotalSize
));
944 return (UINT16
) ~Checksum
;
949 This function removes the specified Token from the TokenMap.
951 @param[in, out] TokenMap Pointer to the NET_MAP containing the tokens.
952 @param[in] Token Pointer to the Token to be removed.
954 @retval EFI_SUCCESS The specified Token is removed from the TokenMap.
955 @retval EFI_NOT_FOUND The specified Token is not found in the TokenMap.
960 IN OUT NET_MAP
*TokenMap
,
961 IN EFI_UDP4_COMPLETION_TOKEN
*Token
967 // Find the Token first.
969 Item
= NetMapFindKey (TokenMap
, (VOID
*) Token
);
973 // Remove the token if it's found in the map.
975 NetMapRemoveItem (TokenMap
, Item
, NULL
);
980 return EFI_NOT_FOUND
;
985 This function is the packet transmitting notify function registered to the IpIo
986 interface. It's called to signal the udp TxToken when IpIo layer completes the
987 transmitting of the udp datagram.
989 @param[in] Status The completion status of the output udp datagram.
990 @param[in] Context Pointer to the context data.
991 @param[in] Sender Specify a pointer of EFI_IP4_PROTOCOL for sending.
992 @param[in] NotifyData Pointer to the notify data.
998 IN EFI_STATUS Status
,
1000 IN IP_IO_IP_PROTOCOL Sender
,
1004 UDP4_INSTANCE_DATA
*Instance
;
1005 EFI_UDP4_COMPLETION_TOKEN
*Token
;
1007 Instance
= (UDP4_INSTANCE_DATA
*) Context
;
1008 Token
= (EFI_UDP4_COMPLETION_TOKEN
*) NotifyData
;
1010 if (Udp4RemoveToken (&Instance
->TxTokens
, Token
) == EFI_SUCCESS
) {
1012 // The token may be cancelled. Only signal it if the remove operation succeeds.
1014 Token
->Status
= Status
;
1015 gBS
->SignalEvent (Token
->Event
);
1022 This function processes the received datagram passed up by the IpIo layer.
1024 @param[in] Status The status of this udp datagram.
1025 @param[in] IcmpError The IcmpError code, only available when Status is
1027 @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA.
1028 @param[in] Packet Pointer to the NET_BUF containing the received udp
1030 @param[in] Context Pointer to the context data.
1036 IN EFI_STATUS Status
,
1038 IN EFI_NET_SESSION_DATA
*NetSession
,
1043 NET_CHECK_SIGNATURE (Packet
, NET_BUF_SIGNATURE
);
1046 // IpIo only passes received packets with Status EFI_SUCCESS or EFI_ICMP_ERROR.
1048 if (Status
== EFI_SUCCESS
) {
1050 // Demultiplex the received datagram.
1052 Udp4Demultiplex ((UDP4_SERVICE_DATA
*) Context
, NetSession
, Packet
);
1055 // Handle the ICMP_ERROR packet.
1057 Udp4IcmpHandler ((UDP4_SERVICE_DATA
*) Context
, IcmpError
, NetSession
, Packet
);
1061 // Dispatch the DPC queued by the NotifyFunction of the rx token's events
1062 // which are signaled with received data.
1069 This function removes the multicast group specified by Arg from the Map.
1071 @param[in, out] Map Pointer to the NET_MAP.
1072 @param[in] Item Pointer to the NET_MAP_ITEM.
1073 @param[in] Arg Pointer to the Arg, it's the pointer to a
1074 multicast IPv4 Address.
1076 @retval EFI_SUCCESS The multicast address is removed.
1077 @retval EFI_ABORTED The specified multicast address is removed and the
1084 IN OUT NET_MAP
*Map
,
1085 IN NET_MAP_ITEM
*Item
,
1086 IN VOID
*Arg OPTIONAL
1089 EFI_IPv4_ADDRESS
*McastIp
;
1093 if ((McastIp
!= NULL
) && (!EFI_IP4_EQUAL (McastIp
, &(Item
->Key
)))) {
1095 // McastIp is not NULL and the multicast address contained in the Item
1096 // is not the same as McastIp.
1102 // Remove this Item.
1104 NetMapRemoveItem (Map
, Item
, NULL
);
1106 if (McastIp
!= NULL
) {
1108 // Return EFI_ABORTED in case McastIp is not NULL to terminate the iteration.
1118 This function cancels the token specified by Arg in the Map. This is a callback
1119 used by Udp4InstanceCancelToken().
1121 @param[in] Map Pointer to the NET_MAP.
1122 @param[in] Item Pointer to the NET_MAP_ITEM.
1123 @param[in] Arg Pointer to the token to be cancelled, if NULL,
1124 the token specified by Item is cancelled.
1126 @retval EFI_SUCCESS The token is cancelled if Arg is NULL or the token
1127 is not the same as that in the Item if Arg is not
1129 @retval EFI_ABORTED Arg is not NULL, and the token specified by Arg is
1137 IN NET_MAP_ITEM
*Item
,
1138 IN VOID
*Arg OPTIONAL
1141 EFI_UDP4_COMPLETION_TOKEN
*TokenToCancel
;
1145 if ((Arg
!= NULL
) && (Item
->Key
!= Arg
)) {
1149 if (Item
->Value
!= NULL
) {
1151 // If the token is a transmit token, the corresponding Packet is recorded in
1152 // Item->Value, invoke IpIo to cancel this packet first. The IpIoCancelTxToken
1153 // will invoke Udp4DgramSent, the token will be signaled and this Item will
1154 // be removed from the Map there.
1156 Packet
= (NET_BUF
*) (Item
->Value
);
1157 IpIo
= (IP_IO
*) (*((UINTN
*) &Packet
->ProtoData
[0]));
1159 IpIoCancelTxToken (IpIo
, Packet
);
1162 // The token is a receive token. Abort it and remove it from the Map.
1164 TokenToCancel
= (EFI_UDP4_COMPLETION_TOKEN
*) Item
->Key
;
1165 NetMapRemoveItem (Map
, Item
, NULL
);
1167 TokenToCancel
->Status
= EFI_ABORTED
;
1168 gBS
->SignalEvent (TokenToCancel
->Event
);
1180 This function removes all the Wrap datas in the RcvdDgramQue.
1182 @param[in] Instance Pointer to the udp instance context data.
1186 Udp4FlushRcvdDgram (
1187 IN UDP4_INSTANCE_DATA
*Instance
1190 UDP4_RXDATA_WRAP
*Wrap
;
1192 while (!IsListEmpty (&Instance
->RcvdDgramQue
)) {
1194 // Iterate all the Wraps in the RcvdDgramQue.
1196 Wrap
= NET_LIST_HEAD (&Instance
->RcvdDgramQue
, UDP4_RXDATA_WRAP
, Link
);
1199 // The Wrap will be removed from the RcvdDgramQue by this function call.
1201 Udp4RecycleRxDataWrap (NULL
, (VOID
*) Wrap
);
1208 Cancel Udp4 tokens from the Udp4 instance.
1210 @param[in] Instance Pointer to the udp instance context data.
1211 @param[in] Token Pointer to the token to be canceled, if NULL, all
1212 tokens in this instance will be cancelled.
1214 @retval EFI_SUCCESS The Token is cancelled.
1215 @retval EFI_NOT_FOUND The Token is not found.
1219 Udp4InstanceCancelToken (
1220 IN UDP4_INSTANCE_DATA
*Instance
,
1221 IN EFI_UDP4_COMPLETION_TOKEN
*Token OPTIONAL
1227 // Cancel this token from the TxTokens map.
1229 Status
= NetMapIterate (&Instance
->TxTokens
, Udp4CancelTokens
, Token
);
1231 if ((Token
!= NULL
) && (Status
== EFI_ABORTED
)) {
1233 // If Token isn't NULL and Status is EFI_ABORTED, the token is cancelled from
1234 // the TxTokens, just return success.
1240 // Try to cancel this token from the RxTokens map in condition either the Token
1241 // is NULL or the specified Token is not in TxTokens.
1243 Status
= NetMapIterate (&Instance
->RxTokens
, Udp4CancelTokens
, Token
);
1245 if ((Token
!= NULL
) && (Status
== EFI_SUCCESS
)) {
1247 // If Token isn't NULL and Status is EFI_SUCCESS, the token is neither in the
1248 // TxTokens nor the RxTokens, or say, it's not found.
1250 return EFI_NOT_FOUND
;
1253 ASSERT ((Token
!= NULL
) || ((0 == NetMapGetCount (&Instance
->TxTokens
))
1254 && (0 == NetMapGetCount (&Instance
->RxTokens
))));
1261 This function matches the received udp datagram with the Instance.
1263 @param[in] Instance Pointer to the udp instance context data.
1264 @param[in] Udp4Session Pointer to the EFI_UDP4_SESSION_DATA abstracted
1265 from the received udp datagram.
1267 @retval TRUE The udp datagram matches the receiving requirments of the
1269 @retval FALSE Otherwise.
1274 IN UDP4_INSTANCE_DATA
*Instance
,
1275 IN EFI_UDP4_SESSION_DATA
*Udp4Session
1278 EFI_UDP4_CONFIG_DATA
*ConfigData
;
1279 IP4_ADDR Destination
;
1281 ConfigData
= &Instance
->ConfigData
;
1283 if (ConfigData
->AcceptPromiscuous
) {
1285 // Always matches if this instance is in the promiscuous state.
1290 if ((!ConfigData
->AcceptAnyPort
&& (Udp4Session
->DestinationPort
!= ConfigData
->StationPort
)) ||
1291 ((ConfigData
->RemotePort
!= 0) && (Udp4Session
->SourcePort
!= ConfigData
->RemotePort
))
1294 // The local port or the remote port doesn't match.
1299 if (!EFI_IP4_EQUAL (&ConfigData
->RemoteAddress
, &mZeroIp4Addr
) &&
1300 !EFI_IP4_EQUAL (&ConfigData
->RemoteAddress
, &Udp4Session
->SourceAddress
)
1303 // This datagram doesn't come from the instance's specified sender.
1308 if (EFI_IP4_EQUAL (&ConfigData
->StationAddress
, &mZeroIp4Addr
) ||
1309 EFI_IP4_EQUAL (&Udp4Session
->DestinationAddress
, &ConfigData
->StationAddress
)
1312 // The instance is configured to receive datagrams destined to any station IP or
1313 // the destination address of this datagram matches the configured station IP.
1318 CopyMem (&Destination
, &Udp4Session
->DestinationAddress
, sizeof (IP4_ADDR
));
1320 if (IP4_IS_LOCAL_BROADCAST (Destination
) && ConfigData
->AcceptBroadcast
) {
1322 // The instance is configured to receive broadcast and this is a broadcast packet.
1327 if (IP4_IS_MULTICAST (NTOHL (Destination
)) &&
1328 NetMapFindKey (&Instance
->McastIps
, (VOID
*) (UINTN
) Destination
) != NULL
1331 // It's a multicast packet and the multicast address is accepted by this instance.
1341 This function removes the Wrap specified by Context and release relevant resources.
1343 @param[in] Event The Event this notify function registered to.
1344 @param[in] Context Pointer to the context data.
1349 Udp4RecycleRxDataWrap (
1354 UDP4_RXDATA_WRAP
*Wrap
;
1356 Wrap
= (UDP4_RXDATA_WRAP
*) Context
;
1359 // Remove the Wrap from the list it belongs to.
1361 RemoveEntryList (&Wrap
->Link
);
1364 // Free the Packet associated with this Wrap.
1366 NetbufFree (Wrap
->Packet
);
1371 gBS
->CloseEvent (Wrap
->RxData
.RecycleSignal
);
1378 This function wraps the Packet and the RxData.
1380 @param[in] Instance Pointer to the instance context data.
1381 @param[in] Packet Pointer to the buffer containing the received
1383 @param[in] RxData Pointer to the EFI_UDP4_RECEIVE_DATA of this
1386 @return Pointer to the structure wrapping the RxData and the Packet.
1391 IN UDP4_INSTANCE_DATA
*Instance
,
1393 IN EFI_UDP4_RECEIVE_DATA
*RxData
1397 UDP4_RXDATA_WRAP
*Wrap
;
1400 // Allocate buffer for the Wrap.
1402 Wrap
= AllocatePool (sizeof (UDP4_RXDATA_WRAP
) +
1403 (Packet
->BlockOpNum
- 1) * sizeof (EFI_UDP4_FRAGMENT_DATA
));
1408 InitializeListHead (&Wrap
->Link
);
1410 CopyMem (&Wrap
->RxData
, RxData
, sizeof (Wrap
->RxData
));
1413 // Create the Recycle event.
1415 Status
= gBS
->CreateEvent (
1418 Udp4RecycleRxDataWrap
,
1420 &Wrap
->RxData
.RecycleSignal
1422 if (EFI_ERROR (Status
)) {
1427 Wrap
->Packet
= Packet
;
1428 Wrap
->TimeoutTick
= Instance
->ConfigData
.ReceiveTimeout
;
1435 This function enqueues the received datagram into the instances' receiving queues.
1437 @param[in] Udp4Service Pointer to the udp service context data.
1438 @param[in] Packet Pointer to the buffer containing the received
1440 @param[in] RxData Pointer to the EFI_UDP4_RECEIVE_DATA of this
1443 @return The times this datagram is enqueued.
1448 IN UDP4_SERVICE_DATA
*Udp4Service
,
1450 IN EFI_UDP4_RECEIVE_DATA
*RxData
1454 UDP4_INSTANCE_DATA
*Instance
;
1455 UDP4_RXDATA_WRAP
*Wrap
;
1460 NET_LIST_FOR_EACH (Entry
, &Udp4Service
->ChildrenList
) {
1462 // Iterate the instances.
1464 Instance
= NET_LIST_USER_STRUCT (Entry
, UDP4_INSTANCE_DATA
, Link
);
1466 if (!Instance
->Configured
) {
1470 if (Udp4MatchDgram (Instance
, &RxData
->UdpSession
)) {
1472 // Wrap the RxData and put this Wrap into the instances RcvdDgramQue.
1474 Wrap
= Udp4WrapRxData (Instance
, Packet
, RxData
);
1479 NET_GET_REF (Packet
);
1481 InsertTailList (&Instance
->RcvdDgramQue
, &Wrap
->Link
);
1492 This function delivers the received datagrams for the specified instance.
1494 @param[in] Instance Pointer to the instance context data.
1498 Udp4InstanceDeliverDgram (
1499 IN UDP4_INSTANCE_DATA
*Instance
1502 UDP4_RXDATA_WRAP
*Wrap
;
1503 EFI_UDP4_COMPLETION_TOKEN
*Token
;
1505 EFI_UDP4_RECEIVE_DATA
*RxData
;
1508 if (!IsListEmpty (&Instance
->RcvdDgramQue
) &&
1509 !NetMapIsEmpty (&Instance
->RxTokens
)) {
1511 Wrap
= NET_LIST_HEAD (&Instance
->RcvdDgramQue
, UDP4_RXDATA_WRAP
, Link
);
1513 if (NET_BUF_SHARED (Wrap
->Packet
)) {
1515 // Duplicate the Packet if it is shared between instances.
1517 Dup
= NetbufDuplicate (Wrap
->Packet
, NULL
, 0);
1522 NetbufFree (Wrap
->Packet
);
1527 NetListRemoveHead (&Instance
->RcvdDgramQue
);
1529 Token
= (EFI_UDP4_COMPLETION_TOKEN
*) NetMapRemoveHead (&Instance
->RxTokens
, NULL
);
1532 // Build the FragmentTable and set the FragmentCount in RxData.
1534 RxData
= &Wrap
->RxData
;
1535 RxData
->FragmentCount
= Wrap
->Packet
->BlockOpNum
;
1539 (NET_FRAGMENT
*) RxData
->FragmentTable
,
1540 &RxData
->FragmentCount
1543 Token
->Status
= EFI_SUCCESS
;
1544 Token
->Packet
.RxData
= &Wrap
->RxData
;
1546 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1547 InsertTailList (&Instance
->DeliveredDgramQue
, &Wrap
->Link
);
1548 gBS
->RestoreTPL (OldTpl
);
1550 gBS
->SignalEvent (Token
->Event
);
1556 This function delivers the datagrams enqueued in the instances.
1558 @param[in] Udp4Service Pointer to the udp service context data.
1563 IN UDP4_SERVICE_DATA
*Udp4Service
1567 UDP4_INSTANCE_DATA
*Instance
;
1569 NET_LIST_FOR_EACH (Entry
, &Udp4Service
->ChildrenList
) {
1571 // Iterate the instances.
1573 Instance
= NET_LIST_USER_STRUCT (Entry
, UDP4_INSTANCE_DATA
, Link
);
1575 if (!Instance
->Configured
) {
1580 // Deliver the datagrams of this instance.
1582 Udp4InstanceDeliverDgram (Instance
);
1588 This function demultiplexes the received udp datagram to the appropriate instances.
1590 @param[in] Udp4Service Pointer to the udp service context data.
1591 @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA abstracted from
1592 the received datagram.
1593 @param[in] Packet Pointer to the buffer containing the received udp
1599 IN UDP4_SERVICE_DATA
*Udp4Service
,
1600 IN EFI_NET_SESSION_DATA
*NetSession
,
1604 EFI_UDP_HEADER
*Udp4Header
;
1606 EFI_UDP4_RECEIVE_DATA RxData
;
1607 EFI_UDP4_SESSION_DATA
*Udp4Session
;
1610 if (Packet
->TotalSize
< sizeof (EFI_UDP_HEADER
)) {
1611 NetbufFree (Packet
);
1616 // Get the datagram header from the packet buffer.
1618 Udp4Header
= (EFI_UDP_HEADER
*) NetbufGetByte (Packet
, 0, NULL
);
1619 ASSERT (Udp4Header
!= NULL
);
1621 if (Udp4Header
->Checksum
!= 0) {
1623 // check the checksum.
1625 HeadSum
= NetPseudoHeadChecksum (
1626 NetSession
->Source
.Addr
[0],
1627 NetSession
->Dest
.Addr
[0],
1632 if (Udp4Checksum (Packet
, HeadSum
) != 0) {
1636 NetbufFree (Packet
);
1641 Udp4Session
= &RxData
.UdpSession
;
1642 Udp4Session
->SourcePort
= NTOHS (Udp4Header
->SrcPort
);
1643 Udp4Session
->DestinationPort
= NTOHS (Udp4Header
->DstPort
);
1645 CopyMem (&Udp4Session
->SourceAddress
, &NetSession
->Source
, sizeof (EFI_IPv4_ADDRESS
));
1646 CopyMem (&Udp4Session
->DestinationAddress
, &NetSession
->Dest
, sizeof (EFI_IPv4_ADDRESS
));
1649 // Trim the UDP header.
1651 NetbufTrim (Packet
, UDP4_HEADER_SIZE
, TRUE
);
1653 RxData
.DataLength
= (UINT32
) Packet
->TotalSize
;
1656 // Try to enqueue this datagram into the instances.
1658 Enqueued
= Udp4EnqueueDgram (Udp4Service
, Packet
, &RxData
);
1660 if (Enqueued
== 0) {
1662 // Send the port unreachable ICMP packet before we free this NET_BUF
1664 Udp4SendPortUnreach (Udp4Service
->IpIo
, NetSession
, Udp4Header
);
1668 // Try to free the packet before deliver it.
1670 NetbufFree (Packet
);
1674 // Deliver the datagram.
1676 Udp4DeliverDgram (Udp4Service
);
1682 This function builds and sends out a icmp port unreachable message.
1684 @param[in] IpIo Pointer to the IP_IO instance.
1685 @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA of the packet
1686 causes this icmp error message.
1687 @param[in] Udp4Header Pointer to the udp header of the datagram causes
1688 this icmp error message.
1692 Udp4SendPortUnreach (
1694 IN EFI_NET_SESSION_DATA
*NetSession
,
1700 IP4_ICMP_ERROR_HEAD
*IcmpErrHdr
;
1701 EFI_IP4_HEADER
*IpHdr
;
1703 IP_IO_OVERRIDE Override
;
1704 IP_IO_IP_INFO
*IpSender
;
1706 IpSender
= IpIoFindSender (&IpIo
, NetSession
->IpVersion
, &NetSession
->Dest
);
1707 if (IpSender
== NULL
) {
1709 // No appropriate sender, since we cannot send out the ICMP message through
1710 // the default zero station address IP instance, abort.
1715 IpHdr
= NetSession
->IpHdr
.Ip4Hdr
;
1718 // Calculate the required length of the icmp error message.
1720 Len
= sizeof (IP4_ICMP_ERROR_HEAD
) + (EFI_IP4_HEADER_LEN (IpHdr
) -
1721 sizeof (IP4_HEAD
)) + ICMP_ERROR_PACKET_LENGTH
;
1724 // Allocate buffer for the icmp error message.
1726 Packet
= NetbufAlloc (Len
);
1727 if (Packet
== NULL
) {
1732 // Allocate space for the IP4_ICMP_ERROR_HEAD.
1734 IcmpErrHdr
= (IP4_ICMP_ERROR_HEAD
*) NetbufAllocSpace (Packet
, Len
, FALSE
);
1735 ASSERT (IcmpErrHdr
!= NULL
);
1738 // Set the required fields for the icmp port unreachable message.
1740 IcmpErrHdr
->Head
.Type
= ICMP_TYPE_UNREACH
;
1741 IcmpErrHdr
->Head
.Code
= ICMP_CODE_UNREACH_PORT
;
1742 IcmpErrHdr
->Head
.Checksum
= 0;
1743 IcmpErrHdr
->Fourth
= 0;
1746 // Copy the IP header of the datagram tragged the error.
1748 CopyMem (&IcmpErrHdr
->IpHead
, IpHdr
, EFI_IP4_HEADER_LEN (IpHdr
));
1751 // Copy the UDP header.
1753 Ptr
= (UINT8
*) &IcmpErrHdr
->IpHead
+ EFI_IP4_HEADER_LEN (IpHdr
);
1754 CopyMem (Ptr
, Udp4Header
, ICMP_ERROR_PACKET_LENGTH
);
1757 // Calculate the checksum.
1759 IcmpErrHdr
->Head
.Checksum
= (UINT16
) ~(NetbufChecksum (Packet
));
1762 // Fill the override data.
1764 Override
.Ip4OverrideData
.DoNotFragment
= FALSE
;
1765 Override
.Ip4OverrideData
.TypeOfService
= 0;
1766 Override
.Ip4OverrideData
.TimeToLive
= 255;
1767 Override
.Ip4OverrideData
.Protocol
= EFI_IP_PROTO_ICMP
;
1769 CopyMem (&Override
.Ip4OverrideData
.SourceAddress
, &NetSession
->Dest
, sizeof (EFI_IPv4_ADDRESS
));
1770 ZeroMem (&Override
.Ip4OverrideData
.GatewayAddress
, sizeof (EFI_IPv4_ADDRESS
));
1773 // Send out this icmp packet.
1775 IpIoSend (IpIo
, Packet
, IpSender
, NULL
, NULL
, &NetSession
->Source
, &Override
);
1777 NetbufFree (Packet
);
1782 This function handles the received Icmp Error message and demultiplexes it to the
1785 @param[in] Udp4Service Pointer to the udp service context data.
1786 @param[in] IcmpError The icmp error code.
1787 @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA abstracted
1788 from the received Icmp Error packet.
1789 @param[in] Packet Pointer to the Icmp Error packet.
1794 IN UDP4_SERVICE_DATA
*Udp4Service
,
1796 IN EFI_NET_SESSION_DATA
*NetSession
,
1800 EFI_UDP_HEADER
*Udp4Header
;
1801 EFI_UDP4_SESSION_DATA Udp4Session
;
1803 UDP4_INSTANCE_DATA
*Instance
;
1805 if (Packet
->TotalSize
< sizeof (EFI_UDP_HEADER
)) {
1806 NetbufFree (Packet
);
1810 Udp4Header
= (EFI_UDP_HEADER
*) NetbufGetByte (Packet
, 0, NULL
);
1811 ASSERT (Udp4Header
!= NULL
);
1813 CopyMem (&Udp4Session
.SourceAddress
, &NetSession
->Source
, sizeof (EFI_IPv4_ADDRESS
));
1814 CopyMem (&Udp4Session
.DestinationAddress
, &NetSession
->Dest
, sizeof (EFI_IPv4_ADDRESS
));
1816 Udp4Session
.SourcePort
= NTOHS (Udp4Header
->DstPort
);
1817 Udp4Session
.DestinationPort
= NTOHS (Udp4Header
->SrcPort
);
1819 NET_LIST_FOR_EACH (Entry
, &Udp4Service
->ChildrenList
) {
1821 // Iterate all the instances.
1823 Instance
= NET_LIST_USER_STRUCT (Entry
, UDP4_INSTANCE_DATA
, Link
);
1825 if (!Instance
->Configured
) {
1829 if (Udp4MatchDgram (Instance
, &Udp4Session
)) {
1831 // Translate the Icmp Error code according to the udp spec.
1833 Instance
->IcmpError
= IpIoGetIcmpErrStatus (IcmpError
, IP_VERSION_4
, NULL
, NULL
);
1835 if (IcmpError
> ICMP_ERR_UNREACH_PORT
) {
1836 Instance
->IcmpError
= EFI_ICMP_ERROR
;
1840 // Notify the instance with the received Icmp Error.
1842 Udp4ReportIcmpError (Instance
);
1848 NetbufFree (Packet
);
1853 This function reports the received ICMP error.
1855 @param[in] Instance Pointer to the udp instance context data.
1859 Udp4ReportIcmpError (
1860 IN UDP4_INSTANCE_DATA
*Instance
1863 EFI_UDP4_COMPLETION_TOKEN
*Token
;
1865 if (NetMapIsEmpty (&Instance
->RxTokens
)) {
1867 // There are no receive tokens to deliver the ICMP error.
1872 if (EFI_ERROR (Instance
->IcmpError
)) {
1874 // Try to get a RxToken from the RxTokens map.
1876 Token
= (EFI_UDP4_COMPLETION_TOKEN
*) NetMapRemoveHead (&Instance
->RxTokens
, NULL
);
1878 if (Token
!= NULL
) {
1880 // Report the error through the Token.
1882 Token
->Status
= Instance
->IcmpError
;
1883 gBS
->SignalEvent (Token
->Event
);
1886 // Clear the IcmpError.
1888 Instance
->IcmpError
= EFI_SUCCESS
;
1895 This function is a dummy ext-free function for the NET_BUF created for the output
1898 @param[in] Context Pointer to the context data.
1903 Udp4NetVectorExtFree (