2 The implementation of the Udp4 protocol.
4 Copyright (c) 2006 - 2009, Intel Corporation.<BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 UINT16 mUdp4RandomPort
;
21 This function checks and timeouts the I/O datagrams holding by the corresponding
24 @param[in] Event The event this function registered to.
25 @param[in] Context The context data registered during the creation of
37 This function finds the udp instance by the specified <Address, Port> pair.
39 @param[in] InstanceList Pointer to the head of the list linking the udp
41 @param[in] Address Pointer to the specified IPv4 address.
42 @param[in] Port The udp port number.
44 @retval TRUE The specified <Address, Port> pair is found.
45 @retval FALSE Otherwise.
49 Udp4FindInstanceByPort (
50 IN LIST_ENTRY
*InstanceList
,
51 IN EFI_IPv4_ADDRESS
*Address
,
56 This function is the packet transmitting notify function registered to the IpIo
57 interface. It's called to signal the udp TxToken when IpIo layer completes the
58 transmitting of the udp datagram.
60 @param[in] Status The completion status of the output udp datagram.
61 @param[in] Context Pointer to the context data.
62 @param[in] Sender Pointer to the Ip sender of the udp datagram.
63 @param[in] NotifyData Pointer to the notify data.
75 This function processes the received datagram passed up by the IpIo layer.
77 @param[in] Status The status of this udp datagram.
78 @param[in] IcmpError The IcmpError code, only available when Status is
80 @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA.
81 @param[in] Packet Pointer to the NET_BUF containing the received udp
83 @param[in] Context Pointer to the context data.
89 IN ICMP_ERROR IcmpError
,
90 IN EFI_NET_SESSION_DATA
*NetSession
,
96 This function cancels the token specified by Arg in the Map. This is a callback
97 used by Udp4InstanceCancelToken().
99 @param[in] Map Pointer to the NET_MAP.
100 @param[in] Item Pointer to the NET_MAP_ITEM.
101 @param[in] Arg Pointer to the token to be cancelled, if NULL,
102 the token specified by Item is cancelled.
104 @retval EFI_SUCCESS The token is cancelled if Arg is NULL or the token
105 is not the same as that in the Item if Arg is not
107 @retval EFI_ABORTED Arg is not NULL, and the token specified by Arg is
114 IN NET_MAP_ITEM
*Item
,
115 IN VOID
*Arg OPTIONAL
119 This function matches the received udp datagram with the Instance.
121 @param[in] Instance Pointer to the udp instance context data.
122 @param[in] Udp4Session Pointer to the EFI_UDP4_SESSION_DATA abstracted
123 from the received udp datagram.
125 @retval TRUE The udp datagram matches the receiving requirments of the
127 @retval FALSE Otherwise.
132 IN UDP4_INSTANCE_DATA
*Instance
,
133 IN EFI_UDP4_SESSION_DATA
*Udp4Session
137 This function removes the Wrap specified by Context and release relevant resources.
139 @param[in] Event The Event this notify function registered to.
140 @param[in] Context Pointer to the context data.
145 Udp4RecycleRxDataWrap (
151 This function wraps the Packet and the RxData.
153 @param[in] Instance Pointer to the instance context data.
154 @param[in] Packet Pointer to the buffer containing the received
156 @param[in] RxData Pointer to the EFI_UDP4_RECEIVE_DATA of this
159 @return Pointer to the structure wrapping the RxData and the Packet.
164 IN UDP4_INSTANCE_DATA
*Instance
,
166 IN EFI_UDP4_RECEIVE_DATA
*RxData
170 This function enqueues the received datagram into the instances' receiving queues.
172 @param[in] Udp4Service Pointer to the udp service context data.
173 @param[in] Packet Pointer to the buffer containing the received
175 @param[in] RxData Pointer to the EFI_UDP4_RECEIVE_DATA of this
178 @return The times this datagram is enqueued.
183 IN UDP4_SERVICE_DATA
*Udp4Service
,
185 IN EFI_UDP4_RECEIVE_DATA
*RxData
189 This function delivers the datagrams enqueued in the instances.
191 @param[in] Udp4Service Pointer to the udp service context data.
196 IN UDP4_SERVICE_DATA
*Udp4Service
200 This function demultiplexes the received udp datagram to the apropriate instances.
202 @param[in] Udp4Service Pointer to the udp service context data.
203 @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA abstrated from
204 the received datagram.
205 @param[in] Packet Pointer to the buffer containing the received udp
211 IN UDP4_SERVICE_DATA
*Udp4Service
,
212 IN EFI_NET_SESSION_DATA
*NetSession
,
217 This function handles the received Icmp Error message and demultiplexes it to the
220 @param[in] Udp4Service Pointer to the udp service context data.
221 @param[in] IcmpError The icmp error code.
222 @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA abstracted
223 from the received Icmp Error packet.
224 @param[in] Packet Pointer to the Icmp Error packet.
229 IN UDP4_SERVICE_DATA
*Udp4Service
,
230 IN ICMP_ERROR IcmpError
,
231 IN EFI_NET_SESSION_DATA
*NetSession
,
236 This function builds and sends out a icmp port unreachable message.
238 @param[in] IpIo Pointer to the IP_IO instance.
239 @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA of the packet
240 causes this icmp error message.
241 @param[in] Udp4Header Pointer to the udp header of the datagram causes
242 this icmp error message.
246 Udp4SendPortUnreach (
248 IN EFI_NET_SESSION_DATA
*NetSession
,
254 Create the Udp service context data.
256 @param Udp4Service Pointer to the UDP4_SERVICE_DATA.
257 @param ImageHandle The image handle of this udp4 driver.
258 @param ControllerHandle The controller handle this udp4 driver binds on.
260 @retval EFI_SUCCESS The udp4 service context data is created and
262 @retval EFI_OUT_OF_RESOURCES Cannot allocate memory.
263 @retval other Other error occurs.
268 IN OUT UDP4_SERVICE_DATA
*Udp4Service
,
269 IN EFI_HANDLE ImageHandle
,
270 IN EFI_HANDLE ControllerHandle
274 IP_IO_OPEN_DATA OpenData
;
275 EFI_IP4_CONFIG_DATA
*Ip4ConfigData
;
277 ZeroMem (Udp4Service
, sizeof (UDP4_SERVICE_DATA
));
279 Udp4Service
->Signature
= UDP4_SERVICE_DATA_SIGNATURE
;
280 Udp4Service
->ServiceBinding
= mUdp4ServiceBinding
;
281 Udp4Service
->ImageHandle
= ImageHandle
;
282 Udp4Service
->ControllerHandle
= ControllerHandle
;
283 Udp4Service
->ChildrenNumber
= 0;
285 InitializeListHead (&Udp4Service
->ChildrenList
);
288 // Create the IpIo for this service context.
290 Udp4Service
->IpIo
= IpIoCreate (ImageHandle
, ControllerHandle
, IP_VERSION_4
);
291 if (Udp4Service
->IpIo
== NULL
) {
292 return EFI_OUT_OF_RESOURCES
;
296 // Set the OpenData used to open the IpIo.
298 Ip4ConfigData
= &OpenData
.IpConfigData
.Ip4CfgData
;
299 CopyMem (Ip4ConfigData
, &mIp4IoDefaultIpConfigData
, sizeof (EFI_IP4_CONFIG_DATA
));
300 Ip4ConfigData
->AcceptBroadcast
= TRUE
;
301 OpenData
.RcvdContext
= (VOID
*) Udp4Service
;
302 OpenData
.SndContext
= NULL
;
303 OpenData
.PktRcvdNotify
= Udp4DgramRcvd
;
304 OpenData
.PktSentNotify
= Udp4DgramSent
;
307 // Configure and start the IpIo.
309 Status
= IpIoOpen (Udp4Service
->IpIo
, &OpenData
);
310 if (EFI_ERROR (Status
)) {
315 // Create the event for Udp timeout checking.
317 Status
= gBS
->CreateEvent (
318 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
322 &Udp4Service
->TimeoutEvent
324 if (EFI_ERROR (Status
)) {
329 // Start the timeout timer event.
331 Status
= gBS
->SetTimer (
332 Udp4Service
->TimeoutEvent
,
334 UDP4_TIMEOUT_INTERVAL
336 if (EFI_ERROR (Status
)) {
344 if (Udp4Service
->TimeoutEvent
!= NULL
) {
345 gBS
->CloseEvent (Udp4Service
->TimeoutEvent
);
348 IpIoDestroy (Udp4Service
->IpIo
);
355 Clean the Udp service context data.
357 @param[in] Udp4Service Pointer to the UDP4_SERVICE_DATA.
362 IN UDP4_SERVICE_DATA
*Udp4Service
366 // Cancel the TimeoutEvent timer.
368 gBS
->SetTimer (Udp4Service
->TimeoutEvent
, TimerCancel
, 0);
371 // Close the TimeoutEvent timer.
373 gBS
->CloseEvent (Udp4Service
->TimeoutEvent
);
378 IpIoDestroy (Udp4Service
->IpIo
);
383 This function checks and timeouts the I/O datagrams holding by the corresponding
386 @param[in] Event The event this function registered to.
387 @param[in] Context The context data registered during the creation of
398 UDP4_SERVICE_DATA
*Udp4Service
;
400 UDP4_INSTANCE_DATA
*Instance
;
401 LIST_ENTRY
*WrapEntry
;
402 LIST_ENTRY
*NextEntry
;
403 UDP4_RXDATA_WRAP
*Wrap
;
405 Udp4Service
= (UDP4_SERVICE_DATA
*) Context
;
406 NET_CHECK_SIGNATURE (Udp4Service
, UDP4_SERVICE_DATA_SIGNATURE
);
408 NET_LIST_FOR_EACH (Entry
, &Udp4Service
->ChildrenList
) {
410 // Iterate all the instances belonging to this service context.
412 Instance
= NET_LIST_USER_STRUCT (Entry
, UDP4_INSTANCE_DATA
, Link
);
413 NET_CHECK_SIGNATURE (Instance
, UDP4_INSTANCE_DATA_SIGNATURE
);
415 if (!Instance
->Configured
|| (Instance
->ConfigData
.ReceiveTimeout
== 0)) {
417 // Skip this instance if it's not configured or no receive timeout.
422 NET_LIST_FOR_EACH_SAFE (WrapEntry
, NextEntry
, &Instance
->RcvdDgramQue
) {
424 // Iterate all the rxdatas belonging to this udp instance.
426 Wrap
= NET_LIST_USER_STRUCT (WrapEntry
, UDP4_RXDATA_WRAP
, Link
);
429 // TimeoutTick unit is microsecond, MNP_TIMEOUT_CHECK_INTERVAL unit is 100ns.
431 if (Wrap
->TimeoutTick
<= (UDP4_TIMEOUT_INTERVAL
/ 10)) {
433 // Remove this RxData if it timeouts.
435 Udp4RecycleRxDataWrap (NULL
, (VOID
*) Wrap
);
437 Wrap
->TimeoutTick
-= (UDP4_TIMEOUT_INTERVAL
/ 10);
445 This function intializes the new created udp instance.
447 @param Udp4Service Pointer to the UDP4_SERVICE_DATA.
448 @param Instance Pointer to the un-initialized UDP4_INSTANCE_DATA.
453 IN UDP4_SERVICE_DATA
*Udp4Service
,
454 IN OUT UDP4_INSTANCE_DATA
*Instance
458 // Set the signature.
460 Instance
->Signature
= UDP4_INSTANCE_DATA_SIGNATURE
;
465 InitializeListHead (&Instance
->Link
);
466 InitializeListHead (&Instance
->RcvdDgramQue
);
467 InitializeListHead (&Instance
->DeliveredDgramQue
);
470 // Init the NET_MAPs.
472 NetMapInit (&Instance
->TxTokens
);
473 NetMapInit (&Instance
->RxTokens
);
474 NetMapInit (&Instance
->McastIps
);
477 // Save the pointer to the UDP4_SERVICE_DATA, and initialize other members.
479 Instance
->Udp4Service
= Udp4Service
;
480 CopyMem (&Instance
->Udp4Proto
, &mUdp4Protocol
, sizeof (Instance
->Udp4Proto
));
481 Instance
->IcmpError
= EFI_SUCCESS
;
482 Instance
->Configured
= FALSE
;
483 Instance
->IsNoMapping
= FALSE
;
484 Instance
->Destroyed
= FALSE
;
489 This function cleans the udp instance.
491 @param[in] Instance Pointer to the UDP4_INSTANCE_DATA to clean.
496 IN UDP4_INSTANCE_DATA
*Instance
499 NetMapClean (&Instance
->McastIps
);
500 NetMapClean (&Instance
->RxTokens
);
501 NetMapClean (&Instance
->TxTokens
);
506 This function finds the udp instance by the specified <Address, Port> pair.
508 @param[in] InstanceList Pointer to the head of the list linking the udp
510 @param[in] Address Pointer to the specified IPv4 address.
511 @param[in] Port The udp port number.
513 @retval TRUE The specified <Address, Port> pair is found.
514 @retval FALSE Otherwise.
518 Udp4FindInstanceByPort (
519 IN LIST_ENTRY
*InstanceList
,
520 IN EFI_IPv4_ADDRESS
*Address
,
525 UDP4_INSTANCE_DATA
*Instance
;
526 EFI_UDP4_CONFIG_DATA
*ConfigData
;
528 NET_LIST_FOR_EACH (Entry
, InstanceList
) {
530 // Iterate all the udp instances.
532 Instance
= NET_LIST_USER_STRUCT (Entry
, UDP4_INSTANCE_DATA
, Link
);
533 ConfigData
= &Instance
->ConfigData
;
535 if (!Instance
->Configured
|| ConfigData
->AcceptAnyPort
) {
537 // If the instance is not configured or the configdata of the instance indicates
538 // this instance accepts any port, skip it.
543 if (EFI_IP4_EQUAL (&ConfigData
->StationAddress
, Address
) &&
544 (ConfigData
->StationPort
== Port
)) {
546 // if both the address and the port are the same, return TRUE.
553 // return FALSE when matching fails.
560 This function tries to bind the udp instance according to the configured port
563 @param InstanceList Pointer to the head of the list linking the udp
565 @param ConfigData Pointer to the ConfigData of the instance to be
566 bound. ConfigData->StationPort will be assigned
567 with an available port value on success.
569 @retval EFI_SUCCESS The bound operation is completed successfully.
570 @retval EFI_ACCESS_DENIED The <Address, Port> specified by the ConfigData is
571 already used by other instance.
572 @retval EFI_OUT_OF_RESOURCES No available port resources.
577 IN LIST_ENTRY
*InstanceList
,
578 IN OUT EFI_UDP4_CONFIG_DATA
*ConfigData
581 EFI_IPv4_ADDRESS
*StationAddress
;
584 if (ConfigData
->AcceptAnyPort
) {
588 StationAddress
= &ConfigData
->StationAddress
;
590 if (ConfigData
->StationPort
!= 0) {
592 if (!ConfigData
->AllowDuplicatePort
&&
593 Udp4FindInstanceByPort (InstanceList
, StationAddress
, ConfigData
->StationPort
)) {
595 // Do not allow duplicate port and the port is already used by other instance.
597 return EFI_ACCESS_DENIED
;
601 // select a random port for this instance;
604 if (ConfigData
->AllowDuplicatePort
) {
606 // Just pick up the random port if the instance allows duplicate port.
608 ConfigData
->StationPort
= mUdp4RandomPort
;
611 StartPort
= mUdp4RandomPort
;
613 while (Udp4FindInstanceByPort(InstanceList
, StationAddress
, mUdp4RandomPort
)) {
616 if (mUdp4RandomPort
== 0) {
617 mUdp4RandomPort
= UDP4_PORT_KNOWN
;
620 if (mUdp4RandomPort
== StartPort
) {
622 // No available port.
624 return EFI_OUT_OF_RESOURCES
;
628 ConfigData
->StationPort
= mUdp4RandomPort
;
632 if (mUdp4RandomPort
== 0) {
633 mUdp4RandomPort
= UDP4_PORT_KNOWN
;
642 This function is used to check whether the NewConfigData has any un-reconfigurable
643 parameters changed compared to the OldConfigData.
645 @param[in] OldConfigData Pointer to the current ConfigData the udp instance
647 @param[in] NewConfigData Pointer to the new ConfigData.
649 @retval TRUE The instance is reconfigurable.
650 @retval FALSE Otherwise.
654 Udp4IsReconfigurable (
655 IN EFI_UDP4_CONFIG_DATA
*OldConfigData
,
656 IN EFI_UDP4_CONFIG_DATA
*NewConfigData
659 if ((NewConfigData
->AcceptAnyPort
!= OldConfigData
->AcceptAnyPort
) ||
660 (NewConfigData
->AcceptBroadcast
!= OldConfigData
->AcceptBroadcast
) ||
661 (NewConfigData
->AcceptPromiscuous
!= OldConfigData
->AcceptPromiscuous
) ||
662 (NewConfigData
->AllowDuplicatePort
!= OldConfigData
->AllowDuplicatePort
)
665 // The receiving filter parameters cannot be changed.
670 if ((!NewConfigData
->AcceptAnyPort
) &&
671 (NewConfigData
->StationPort
!= OldConfigData
->StationPort
)
674 // The port is not changeable.
679 if (!NewConfigData
->AcceptPromiscuous
) {
681 if (NewConfigData
->UseDefaultAddress
!= OldConfigData
->UseDefaultAddress
) {
683 // The NewConfigData differs to the old one on the UseDefaultAddress.
688 if (!NewConfigData
->UseDefaultAddress
&&
689 (!EFI_IP4_EQUAL (&NewConfigData
->StationAddress
, &OldConfigData
->StationAddress
) ||
690 !EFI_IP4_EQUAL (&NewConfigData
->SubnetMask
, &OldConfigData
->SubnetMask
))
693 // If the instance doesn't use the default address, and the new address or
694 // new subnet mask is different from the old values.
700 if (!EFI_IP4_EQUAL (&NewConfigData
->RemoteAddress
, &OldConfigData
->RemoteAddress
)) {
702 // The remoteaddress is not the same.
707 if (!EFI_IP4_EQUAL (&NewConfigData
->RemoteAddress
, &mZeroIp4Addr
) &&
708 NewConfigData
->RemotePort
!= OldConfigData
->RemotePort
711 // The RemotePort differs if it's designated in the configdata.
717 // All checks pass, return TRUE.
724 This function builds the Ip4 configdata from the Udp4ConfigData.
726 @param[in] Udp4ConfigData Pointer to the EFI_UDP4_CONFIG_DATA.
727 @param[in, out] Ip4ConfigData Pointer to the EFI_IP4_CONFIG_DATA.
731 Udp4BuildIp4ConfigData (
732 IN EFI_UDP4_CONFIG_DATA
*Udp4ConfigData
,
733 IN OUT EFI_IP4_CONFIG_DATA
*Ip4ConfigData
736 CopyMem (Ip4ConfigData
, &mIp4IoDefaultIpConfigData
, sizeof (*Ip4ConfigData
));
738 Ip4ConfigData
->DefaultProtocol
= EFI_IP_PROTO_UDP
;
739 Ip4ConfigData
->AcceptBroadcast
= Udp4ConfigData
->AcceptBroadcast
;
740 Ip4ConfigData
->AcceptPromiscuous
= Udp4ConfigData
->AcceptPromiscuous
;
741 Ip4ConfigData
->UseDefaultAddress
= Udp4ConfigData
->UseDefaultAddress
;
742 CopyMem (&Ip4ConfigData
->StationAddress
, &Udp4ConfigData
->StationAddress
, sizeof (EFI_IPv4_ADDRESS
));
743 CopyMem (&Ip4ConfigData
->SubnetMask
, &Udp4ConfigData
->SubnetMask
, sizeof (EFI_IPv4_ADDRESS
));
746 // use the -1 magic number to disable the receiving process of the ip instance.
748 Ip4ConfigData
->ReceiveTimeout
= (UINT32
) (-1);
753 This function validates the TxToken, it returns the error code according to the spec.
755 @param[in] Instance Pointer to the udp instance context data.
756 @param[in] TxToken Pointer to the token to be checked.
758 @retval EFI_SUCCESS The TxToken is valid.
759 @retval EFI_INVALID_PARAMETER One or more of the following are TRUE: This is
760 NULL. Token is NULL. Token.Event is NULL.
761 Token.Packet.TxData is NULL.
762 Token.Packet.TxData.FragmentCount is zero.
763 Token.Packet.TxData.DataLength is not equal to the
764 sum of fragment lengths. One or more of the
765 Token.Packet.TxData.FragmentTable[].
766 FragmentLength fields is zero. One or more of the
767 Token.Packet.TxData.FragmentTable[].
768 FragmentBuffer fields is NULL.
769 Token.Packet.TxData. GatewayAddress is not a
770 unicast IPv4 address if it is not NULL. One or
771 more IPv4 addresses in Token.Packet.TxData.
772 UdpSessionData are not valid unicast IPv4
773 addresses if the UdpSessionData is not NULL.
774 @retval EFI_BAD_BUFFER_SIZE The data length is greater than the maximum UDP
779 Udp4ValidateTxToken (
780 IN UDP4_INSTANCE_DATA
*Instance
,
781 IN EFI_UDP4_COMPLETION_TOKEN
*TxToken
784 EFI_UDP4_TRANSMIT_DATA
*TxData
;
787 EFI_UDP4_CONFIG_DATA
*ConfigData
;
788 EFI_UDP4_SESSION_DATA
*UdpSessionData
;
789 IP4_ADDR SourceAddress
;
790 IP4_ADDR GatewayAddress
;
792 if (TxToken
->Event
== NULL
) {
793 return EFI_INVALID_PARAMETER
;
796 TxData
= TxToken
->Packet
.TxData
;
798 if ((TxData
== NULL
) || (TxData
->FragmentCount
== 0)) {
799 return EFI_INVALID_PARAMETER
;
803 for (Index
= 0; Index
< TxData
->FragmentCount
; Index
++) {
805 if ((TxData
->FragmentTable
[Index
].FragmentBuffer
== NULL
) ||
806 (TxData
->FragmentTable
[Index
].FragmentLength
== 0)) {
808 // if the FragmentBuffer is NULL or the FragmentLeng is zero.
810 return EFI_INVALID_PARAMETER
;
813 TotalLen
+= TxData
->FragmentTable
[Index
].FragmentLength
;
816 if (TotalLen
!= TxData
->DataLength
) {
818 // The TotalLen calculated by adding all the FragmentLeng doesn't equal to the
821 return EFI_INVALID_PARAMETER
;
824 if (TxData
->GatewayAddress
!= NULL
) {
825 CopyMem (&GatewayAddress
, TxData
->GatewayAddress
, sizeof (IP4_ADDR
));
827 if (!Ip4IsUnicast (NTOHL (GatewayAddress
), 0)) {
829 // The specified GatewayAddress is not a unicast IPv4 address while it's not 0.
831 return EFI_INVALID_PARAMETER
;
835 ConfigData
= &Instance
->ConfigData
;
836 UdpSessionData
= TxData
->UdpSessionData
;
838 if (UdpSessionData
!= NULL
) {
840 CopyMem (&SourceAddress
, &UdpSessionData
->SourceAddress
, sizeof (IP4_ADDR
));
842 if ((SourceAddress
!= 0) && !Ip4IsUnicast (HTONL (SourceAddress
), 0)) {
844 // Check whether SourceAddress is a valid IPv4 address in case it's not zero.
845 // The configured station address is used if SourceAddress is zero.
847 return EFI_INVALID_PARAMETER
;
850 if ((UdpSessionData
->DestinationPort
== 0) && (ConfigData
->RemotePort
== 0)) {
852 // Ambiguous, no avalaible DestinationPort for this token.
854 return EFI_INVALID_PARAMETER
;
857 if (EFI_IP4_EQUAL (&UdpSessionData
->DestinationAddress
, &mZeroIp4Addr
)) {
859 // The DestinationAddress specified in the UdpSessionData is 0.
861 return EFI_INVALID_PARAMETER
;
863 } else if (EFI_IP4_EQUAL (&ConfigData
->RemoteAddress
, &mZeroIp4Addr
)) {
865 // the configured RemoteAddress is all zero, and the user doens't override the
866 // destination address.
868 return EFI_INVALID_PARAMETER
;
871 if (TxData
->DataLength
> UDP4_MAX_DATA_SIZE
) {
872 return EFI_BAD_BUFFER_SIZE
;
880 This function checks whether the specified Token duplicates with the one in the Map.
882 @param[in] Map Pointer to the NET_MAP.
883 @param[in] Item Pointer to the NET_MAP_ITEM contain the pointer to
885 @param[in] Context Pointer to the Token to be checked.
887 @retval EFI_SUCCESS The Token specified by Context differs from the
889 @retval EFI_ACCESS_DENIED The Token duplicates with the one in the Item.
895 IN NET_MAP_ITEM
*Item
,
899 EFI_UDP4_COMPLETION_TOKEN
*Token
;
900 EFI_UDP4_COMPLETION_TOKEN
*TokenInItem
;
902 Token
= (EFI_UDP4_COMPLETION_TOKEN
*) Context
;
903 TokenInItem
= (EFI_UDP4_COMPLETION_TOKEN
*) Item
->Key
;
905 if ((Token
== TokenInItem
) || (Token
->Event
== TokenInItem
->Event
)) {
907 // The Token duplicates with the TokenInItem in case either the two pointers are the
908 // same or the Events of these two tokens are the same.
910 return EFI_ACCESS_DENIED
;
918 This function calculates the checksum for the Packet, utilizing the pre-calculated
919 pseudo HeadSum to reduce some overhead.
921 @param[in] Packet Pointer to the NET_BUF contains the udp datagram.
922 @param[in] HeadSum Checksum of the pseudo header execpt the length
925 @retval The 16-bit checksum of this udp datagram.
936 Checksum
= NetbufChecksum (Packet
);
937 Checksum
= NetAddChecksum (Checksum
, HeadSum
);
939 Checksum
= NetAddChecksum (Checksum
, HTONS ((UINT16
) Packet
->TotalSize
));
941 return (UINT16
) ~Checksum
;
946 This function removes the specified Token from the TokenMap.
948 @param TokenMap Pointer to the NET_MAP containing the tokens.
949 @param Token Pointer to the Token to be removed.
951 @retval EFI_SUCCESS The specified Token is removed from the TokenMap.
952 @retval EFI_NOT_FOUND The specified Token is not found in the TokenMap.
957 IN OUT NET_MAP
*TokenMap
,
958 IN EFI_UDP4_COMPLETION_TOKEN
*Token
964 // Find the Token first.
966 Item
= NetMapFindKey (TokenMap
, (VOID
*) Token
);
970 // Remove the token if it's found in the map.
972 NetMapRemoveItem (TokenMap
, Item
, NULL
);
977 return EFI_NOT_FOUND
;
982 This function is the packet transmitting notify function registered to the IpIo
983 interface. It's called to signal the udp TxToken when IpIo layer completes the
984 transmitting of the udp datagram.
986 @param[in] Status The completion status of the output udp datagram.
987 @param[in] Context Pointer to the context data.
988 @param[in] Sender Pointer to the Ip sender of the udp datagram.
989 @param[in] NotifyData Pointer to the notify data.
994 IN EFI_STATUS Status
,
1000 UDP4_INSTANCE_DATA
*Instance
;
1001 EFI_UDP4_COMPLETION_TOKEN
*Token
;
1003 Instance
= (UDP4_INSTANCE_DATA
*) Context
;
1004 Token
= (EFI_UDP4_COMPLETION_TOKEN
*) NotifyData
;
1006 if (Udp4RemoveToken (&Instance
->TxTokens
, Token
) == EFI_SUCCESS
) {
1008 // The token may be cancelled. Only signal it if the remove operation succeeds.
1010 Token
->Status
= Status
;
1011 gBS
->SignalEvent (Token
->Event
);
1018 This function processes the received datagram passed up by the IpIo layer.
1020 @param[in] Status The status of this udp datagram.
1021 @param[in] IcmpError The IcmpError code, only available when Status is
1023 @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA.
1024 @param[in] Packet Pointer to the NET_BUF containing the received udp
1026 @param[in] Context Pointer to the context data.
1031 IN EFI_STATUS Status
,
1032 IN ICMP_ERROR IcmpError
,
1033 IN EFI_NET_SESSION_DATA
*NetSession
,
1038 NET_CHECK_SIGNATURE (Packet
, NET_BUF_SIGNATURE
);
1041 // IpIo only passes received packets with Status EFI_SUCCESS or EFI_ICMP_ERROR.
1043 if (Status
== EFI_SUCCESS
) {
1045 // Demultiplex the received datagram.
1047 Udp4Demultiplex ((UDP4_SERVICE_DATA
*) Context
, NetSession
, Packet
);
1050 // Handle the ICMP_ERROR packet.
1052 Udp4IcmpHandler ((UDP4_SERVICE_DATA
*) Context
, IcmpError
, NetSession
, Packet
);
1056 // Dispatch the DPC queued by the NotifyFunction of the rx token's events
1057 // which are signaled with received data.
1064 This function removes the multicast group specified by Arg from the Map.
1066 @param Map Pointer to the NET_MAP.
1067 @param Item Pointer to the NET_MAP_ITEM.
1068 @param Arg Pointer to the Arg, it's the pointer to a
1069 multicast IPv4 Address.
1071 @retval EFI_SUCCESS The multicast address is removed.
1072 @retval EFI_ABORTED The specified multicast address is removed and the
1078 IN OUT NET_MAP
*Map
,
1079 IN NET_MAP_ITEM
*Item
,
1080 IN VOID
*Arg OPTIONAL
1083 EFI_IPv4_ADDRESS
*McastIp
;
1087 if ((McastIp
!= NULL
) && (!EFI_IP4_EQUAL (McastIp
, &(Item
->Key
)))) {
1089 // McastIp is not NULL and the multicast address contained in the Item
1090 // is not the same as McastIp.
1096 // Remove this Item.
1098 NetMapRemoveItem (Map
, Item
, NULL
);
1100 if (McastIp
!= NULL
) {
1102 // Return EFI_ABORTED in case McastIp is not NULL to terminate the iteration.
1112 This function cancels the token specified by Arg in the Map. This is a callback
1113 used by Udp4InstanceCancelToken().
1115 @param[in] Map Pointer to the NET_MAP.
1116 @param[in] Item Pointer to the NET_MAP_ITEM.
1117 @param[in] Arg Pointer to the token to be cancelled, if NULL,
1118 the token specified by Item is cancelled.
1120 @retval EFI_SUCCESS The token is cancelled if Arg is NULL or the token
1121 is not the same as that in the Item if Arg is not
1123 @retval EFI_ABORTED Arg is not NULL, and the token specified by Arg is
1130 IN NET_MAP_ITEM
*Item
,
1131 IN VOID
*Arg OPTIONAL
1134 EFI_UDP4_COMPLETION_TOKEN
*TokenToCancel
;
1138 if ((Arg
!= NULL
) && (Item
->Key
!= Arg
)) {
1142 if (Item
->Value
!= NULL
) {
1144 // If the token is a transmit token, the corresponding Packet is recorded in
1145 // Item->Value, invoke IpIo to cancel this packet first. The IpIoCancelTxToken
1146 // will invoke Udp4DgramSent, the token will be signaled and this Item will
1147 // be removed from the Map there.
1149 Packet
= (NET_BUF
*) (Item
->Value
);
1150 IpIo
= (IP_IO
*) (*((UINTN
*) &Packet
->ProtoData
[0]));
1152 IpIoCancelTxToken (IpIo
, Packet
);
1155 // The token is a receive token. Abort it and remove it from the Map.
1157 TokenToCancel
= (EFI_UDP4_COMPLETION_TOKEN
*) Item
->Key
;
1158 NetMapRemoveItem (Map
, Item
, NULL
);
1160 TokenToCancel
->Status
= EFI_ABORTED
;
1161 gBS
->SignalEvent (TokenToCancel
->Event
);
1173 This function removes all the Wrap datas in the RcvdDgramQue.
1175 @param[in] Instance Pointer to the udp instance context data.
1179 Udp4FlushRcvdDgram (
1180 IN UDP4_INSTANCE_DATA
*Instance
1183 UDP4_RXDATA_WRAP
*Wrap
;
1185 while (!IsListEmpty (&Instance
->RcvdDgramQue
)) {
1187 // Iterate all the Wraps in the RcvdDgramQue.
1189 Wrap
= NET_LIST_HEAD (&Instance
->RcvdDgramQue
, UDP4_RXDATA_WRAP
, Link
);
1192 // The Wrap will be removed from the RcvdDgramQue by this function call.
1194 Udp4RecycleRxDataWrap (NULL
, (VOID
*) Wrap
);
1201 Cancel Udp4 tokens from the Udp4 instance.
1203 @param[in] Instance Pointer to the udp instance context data.
1204 @param[in] Token Pointer to the token to be canceled, if NULL, all
1205 tokens in this instance will be cancelled.
1207 @retval EFI_SUCCESS The Token is cancelled.
1208 @retval EFI_NOT_FOUND The Token is not found.
1212 Udp4InstanceCancelToken (
1213 IN UDP4_INSTANCE_DATA
*Instance
,
1214 IN EFI_UDP4_COMPLETION_TOKEN
*Token OPTIONAL
1220 // Cancel this token from the TxTokens map.
1222 Status
= NetMapIterate (&Instance
->TxTokens
, Udp4CancelTokens
, Token
);
1224 if ((Token
!= NULL
) && (Status
== EFI_ABORTED
)) {
1226 // If Token isn't NULL and Status is EFI_ABORTED, the token is cancelled from
1227 // the TxTokens, just return success.
1233 // Try to cancel this token from the RxTokens map in condition either the Token
1234 // is NULL or the specified Token is not in TxTokens.
1236 Status
= NetMapIterate (&Instance
->RxTokens
, Udp4CancelTokens
, Token
);
1238 if ((Token
!= NULL
) && (Status
== EFI_SUCCESS
)) {
1240 // If Token isn't NULL and Status is EFI_SUCCESS, the token is neither in the
1241 // TxTokens nor the RxTokens, or say, it's not found.
1243 return EFI_NOT_FOUND
;
1246 ASSERT ((Token
!= NULL
) || ((0 == NetMapGetCount (&Instance
->TxTokens
))
1247 && (0 == NetMapGetCount (&Instance
->RxTokens
))));
1254 This function matches the received udp datagram with the Instance.
1256 @param[in] Instance Pointer to the udp instance context data.
1257 @param[in] Udp4Session Pointer to the EFI_UDP4_SESSION_DATA abstracted
1258 from the received udp datagram.
1260 @retval TRUE The udp datagram matches the receiving requirments of the
1262 @retval FALSE Otherwise.
1267 IN UDP4_INSTANCE_DATA
*Instance
,
1268 IN EFI_UDP4_SESSION_DATA
*Udp4Session
1271 EFI_UDP4_CONFIG_DATA
*ConfigData
;
1272 IP4_ADDR Destination
;
1274 ConfigData
= &Instance
->ConfigData
;
1276 if (ConfigData
->AcceptPromiscuous
) {
1278 // Always matches if this instance is in the promiscuous state.
1283 if ((!ConfigData
->AcceptAnyPort
&& (Udp4Session
->DestinationPort
!= ConfigData
->StationPort
)) ||
1284 ((ConfigData
->RemotePort
!= 0) && (Udp4Session
->SourcePort
!= ConfigData
->RemotePort
))
1287 // The local port or the remote port doesn't match.
1292 if (!EFI_IP4_EQUAL (&ConfigData
->RemoteAddress
, &mZeroIp4Addr
) &&
1293 !EFI_IP4_EQUAL (&ConfigData
->RemoteAddress
, &Udp4Session
->SourceAddress
)
1296 // This datagram doesn't come from the instance's specified sender.
1301 if (EFI_IP4_EQUAL (&ConfigData
->StationAddress
, &mZeroIp4Addr
) ||
1302 EFI_IP4_EQUAL (&Udp4Session
->DestinationAddress
, &ConfigData
->StationAddress
)
1305 // The instance is configured to receive datagrams destined to any station IP or
1306 // the destination address of this datagram matches the configured station IP.
1311 CopyMem (&Destination
, &Udp4Session
->DestinationAddress
, sizeof (IP4_ADDR
));
1313 if (IP4_IS_LOCAL_BROADCAST (Destination
) && ConfigData
->AcceptBroadcast
) {
1315 // The instance is configured to receive broadcast and this is a broadcast packet.
1320 if (IP4_IS_MULTICAST (NTOHL (Destination
)) &&
1321 NetMapFindKey (&Instance
->McastIps
, (VOID
*) (UINTN
) Destination
) != NULL
1324 // It's a multicast packet and the multicast address is accepted by this instance.
1334 This function removes the Wrap specified by Context and release relevant resources.
1336 @param[in] Event The Event this notify function registered to.
1337 @param[in] Context Pointer to the context data.
1342 Udp4RecycleRxDataWrap (
1347 UDP4_RXDATA_WRAP
*Wrap
;
1349 Wrap
= (UDP4_RXDATA_WRAP
*) Context
;
1352 // Remove the Wrap from the list it belongs to.
1354 RemoveEntryList (&Wrap
->Link
);
1357 // Free the Packet associated with this Wrap.
1359 NetbufFree (Wrap
->Packet
);
1364 gBS
->CloseEvent (Wrap
->RxData
.RecycleSignal
);
1366 gBS
->FreePool (Wrap
);
1371 This function wraps the Packet and the RxData.
1373 @param[in] Instance Pointer to the instance context data.
1374 @param[in] Packet Pointer to the buffer containing the received
1376 @param[in] RxData Pointer to the EFI_UDP4_RECEIVE_DATA of this
1379 @return Pointer to the structure wrapping the RxData and the Packet.
1384 IN UDP4_INSTANCE_DATA
*Instance
,
1386 IN EFI_UDP4_RECEIVE_DATA
*RxData
1390 UDP4_RXDATA_WRAP
*Wrap
;
1393 // Allocate buffer for the Wrap.
1395 Wrap
= AllocatePool (sizeof (UDP4_RXDATA_WRAP
) +
1396 (Packet
->BlockOpNum
- 1) * sizeof (EFI_UDP4_FRAGMENT_DATA
));
1401 InitializeListHead (&Wrap
->Link
);
1403 CopyMem (&Wrap
->RxData
, RxData
, sizeof (Wrap
->RxData
));
1406 // Create the Recycle event.
1408 Status
= gBS
->CreateEvent (
1411 Udp4RecycleRxDataWrap
,
1413 &Wrap
->RxData
.RecycleSignal
1415 if (EFI_ERROR (Status
)) {
1416 gBS
->FreePool (Wrap
);
1420 Wrap
->Packet
= Packet
;
1421 Wrap
->TimeoutTick
= Instance
->ConfigData
.ReceiveTimeout
;
1428 This function enqueues the received datagram into the instances' receiving queues.
1430 @param[in] Udp4Service Pointer to the udp service context data.
1431 @param[in] Packet Pointer to the buffer containing the received
1433 @param[in] RxData Pointer to the EFI_UDP4_RECEIVE_DATA of this
1436 @return The times this datagram is enqueued.
1441 IN UDP4_SERVICE_DATA
*Udp4Service
,
1443 IN EFI_UDP4_RECEIVE_DATA
*RxData
1447 UDP4_INSTANCE_DATA
*Instance
;
1448 UDP4_RXDATA_WRAP
*Wrap
;
1453 NET_LIST_FOR_EACH (Entry
, &Udp4Service
->ChildrenList
) {
1455 // Iterate the instances.
1457 Instance
= NET_LIST_USER_STRUCT (Entry
, UDP4_INSTANCE_DATA
, Link
);
1459 if (!Instance
->Configured
) {
1463 if (Udp4MatchDgram (Instance
, &RxData
->UdpSession
)) {
1465 // Wrap the RxData and put this Wrap into the instances RcvdDgramQue.
1467 Wrap
= Udp4WrapRxData (Instance
, Packet
, RxData
);
1472 NET_GET_REF (Packet
);
1474 InsertTailList (&Instance
->RcvdDgramQue
, &Wrap
->Link
);
1485 This function delivers the received datagrams for the specified instance.
1487 @param[in] Instance Pointer to the instance context data.
1491 Udp4InstanceDeliverDgram (
1492 IN UDP4_INSTANCE_DATA
*Instance
1495 UDP4_RXDATA_WRAP
*Wrap
;
1496 EFI_UDP4_COMPLETION_TOKEN
*Token
;
1498 EFI_UDP4_RECEIVE_DATA
*RxData
;
1501 if (!IsListEmpty (&Instance
->RcvdDgramQue
) &&
1502 !NetMapIsEmpty (&Instance
->RxTokens
)) {
1504 Wrap
= NET_LIST_HEAD (&Instance
->RcvdDgramQue
, UDP4_RXDATA_WRAP
, Link
);
1506 if (NET_BUF_SHARED (Wrap
->Packet
)) {
1508 // Duplicate the Packet if it is shared between instances.
1510 Dup
= NetbufDuplicate (Wrap
->Packet
, NULL
, 0);
1515 NetbufFree (Wrap
->Packet
);
1520 NetListRemoveHead (&Instance
->RcvdDgramQue
);
1522 Token
= (EFI_UDP4_COMPLETION_TOKEN
*) NetMapRemoveHead (&Instance
->RxTokens
, NULL
);
1525 // Build the FragmentTable and set the FragmentCount in RxData.
1527 RxData
= &Wrap
->RxData
;
1528 RxData
->FragmentCount
= Wrap
->Packet
->BlockOpNum
;
1532 (NET_FRAGMENT
*) RxData
->FragmentTable
,
1533 &RxData
->FragmentCount
1536 Token
->Status
= EFI_SUCCESS
;
1537 Token
->Packet
.RxData
= &Wrap
->RxData
;
1539 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1540 InsertTailList (&Instance
->DeliveredDgramQue
, &Wrap
->Link
);
1541 gBS
->RestoreTPL (OldTpl
);
1543 gBS
->SignalEvent (Token
->Event
);
1549 This function delivers the datagrams enqueued in the instances.
1551 @param[in] Udp4Service Pointer to the udp service context data.
1556 IN UDP4_SERVICE_DATA
*Udp4Service
1560 UDP4_INSTANCE_DATA
*Instance
;
1562 NET_LIST_FOR_EACH (Entry
, &Udp4Service
->ChildrenList
) {
1564 // Iterate the instances.
1566 Instance
= NET_LIST_USER_STRUCT (Entry
, UDP4_INSTANCE_DATA
, Link
);
1568 if (!Instance
->Configured
) {
1573 // Deliver the datagrams of this instance.
1575 Udp4InstanceDeliverDgram (Instance
);
1581 This function demultiplexes the received udp datagram to the apropriate instances.
1583 @param[in] Udp4Service Pointer to the udp service context data.
1584 @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA abstrated from
1585 the received datagram.
1586 @param[in] Packet Pointer to the buffer containing the received udp
1592 IN UDP4_SERVICE_DATA
*Udp4Service
,
1593 IN EFI_NET_SESSION_DATA
*NetSession
,
1597 EFI_UDP_HEADER
*Udp4Header
;
1599 EFI_UDP4_RECEIVE_DATA RxData
;
1600 EFI_UDP4_SESSION_DATA
*Udp4Session
;
1604 // Get the datagram header from the packet buffer.
1606 Udp4Header
= (EFI_UDP_HEADER
*) NetbufGetByte (Packet
, 0, NULL
);
1608 if (Udp4Header
->Checksum
!= 0) {
1610 // check the checksum.
1612 HeadSum
= NetPseudoHeadChecksum (
1613 NetSession
->Source
.Addr
[0],
1614 NetSession
->Dest
.Addr
[0],
1619 if (Udp4Checksum (Packet
, HeadSum
) != 0) {
1627 gRT
->GetTime (&RxData
.TimeStamp
, NULL
);
1629 Udp4Session
= &RxData
.UdpSession
;
1630 Udp4Session
->SourcePort
= NTOHS (Udp4Header
->SrcPort
);
1631 Udp4Session
->DestinationPort
= NTOHS (Udp4Header
->DstPort
);
1633 CopyMem (&Udp4Session
->SourceAddress
, &NetSession
->Source
, sizeof (EFI_IPv4_ADDRESS
));
1634 CopyMem (&Udp4Session
->DestinationAddress
, &NetSession
->Dest
, sizeof (EFI_IPv4_ADDRESS
));
1637 // Trim the UDP header.
1639 NetbufTrim (Packet
, UDP4_HEADER_SIZE
, TRUE
);
1641 RxData
.DataLength
= (UINT32
) Packet
->TotalSize
;
1644 // Try to enqueue this datagram into the instances.
1646 Enqueued
= Udp4EnqueueDgram (Udp4Service
, Packet
, &RxData
);
1648 if (Enqueued
== 0) {
1650 // Send the port unreachable ICMP packet before we free this NET_BUF
1652 Udp4SendPortUnreach (Udp4Service
->IpIo
, NetSession
, Udp4Header
);
1656 // Try to free the packet before deliver it.
1658 NetbufFree (Packet
);
1662 // Deliver the datagram.
1664 Udp4DeliverDgram (Udp4Service
);
1670 This function builds and sends out a icmp port unreachable message.
1672 @param[in] IpIo Pointer to the IP_IO instance.
1673 @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA of the packet
1674 causes this icmp error message.
1675 @param[in] Udp4Header Pointer to the udp header of the datagram causes
1676 this icmp error message.
1680 Udp4SendPortUnreach (
1682 IN EFI_NET_SESSION_DATA
*NetSession
,
1688 IP4_ICMP_ERROR_HEAD
*IcmpErrHdr
;
1689 EFI_IP4_HEADER
*IpHdr
;
1691 IP_IO_OVERRIDE Override
;
1692 IP_IO_IP_INFO
*IpSender
;
1694 IpSender
= IpIoFindSender (&IpIo
, NetSession
->IpVersion
, &NetSession
->Dest
);
1695 if (IpSender
== NULL
) {
1697 // No apropriate sender, since we cannot send out the ICMP message through
1698 // the default zero station address IP instance, abort.
1703 IpHdr
= NetSession
->IpHdr
.Ip4Hdr
;
1706 // Calculate the requried length of the icmp error message.
1708 Len
= sizeof (IP4_ICMP_ERROR_HEAD
) + (EFI_IP4_HEADER_LEN (IpHdr
) -
1709 sizeof (IP4_HEAD
)) + ICMP_ERROR_PACKET_LENGTH
;
1712 // Allocate buffer for the icmp error message.
1714 Packet
= NetbufAlloc (Len
);
1715 if (Packet
== NULL
) {
1720 // Allocate space for the IP4_ICMP_ERROR_HEAD.
1722 IcmpErrHdr
= (IP4_ICMP_ERROR_HEAD
*) NetbufAllocSpace (Packet
, Len
, FALSE
);
1723 ASSERT (IcmpErrHdr
!= NULL
);
1726 // Set the required fields for the icmp port unreachable message.
1728 IcmpErrHdr
->Head
.Type
= ICMP_TYPE_UNREACH
;
1729 IcmpErrHdr
->Head
.Code
= ICMP_CODE_UNREACH_PORT
;
1730 IcmpErrHdr
->Head
.Checksum
= 0;
1731 IcmpErrHdr
->Fourth
= 0;
1734 // Copy the IP header of the datagram tragged the error.
1736 CopyMem (&IcmpErrHdr
->IpHead
, IpHdr
, EFI_IP4_HEADER_LEN (IpHdr
));
1739 // Copy the UDP header.
1741 Ptr
= (UINT8
*) &IcmpErrHdr
->IpHead
+ EFI_IP4_HEADER_LEN (IpHdr
);
1742 CopyMem (Ptr
, Udp4Header
, ICMP_ERROR_PACKET_LENGTH
);
1745 // Calculate the checksum.
1747 IcmpErrHdr
->Head
.Checksum
= (UINT16
) ~(NetbufChecksum (Packet
));
1750 // Fill the override data.
1752 Override
.Ip4OverrideData
.DoNotFragment
= FALSE
;
1753 Override
.Ip4OverrideData
.TypeOfService
= 0;
1754 Override
.Ip4OverrideData
.TimeToLive
= 255;
1755 Override
.Ip4OverrideData
.Protocol
= EFI_IP_PROTO_ICMP
;
1757 CopyMem (&Override
.Ip4OverrideData
.SourceAddress
, &NetSession
->Dest
, sizeof (EFI_IPv4_ADDRESS
));
1758 ZeroMem (&Override
.Ip4OverrideData
.GatewayAddress
, sizeof (EFI_IPv4_ADDRESS
));
1761 // Send out this icmp packet.
1763 IpIoSend (IpIo
, Packet
, IpSender
, NULL
, NULL
, &NetSession
->Source
, &Override
);
1765 NetbufFree (Packet
);
1770 This function handles the received Icmp Error message and demultiplexes it to the
1773 @param[in] Udp4Service Pointer to the udp service context data.
1774 @param[in] IcmpError The icmp error code.
1775 @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA abstracted
1776 from the received Icmp Error packet.
1777 @param[in] Packet Pointer to the Icmp Error packet.
1782 IN UDP4_SERVICE_DATA
*Udp4Service
,
1783 IN ICMP_ERROR IcmpError
,
1784 IN EFI_NET_SESSION_DATA
*NetSession
,
1788 EFI_UDP_HEADER
*Udp4Header
;
1789 EFI_UDP4_SESSION_DATA Udp4Session
;
1791 UDP4_INSTANCE_DATA
*Instance
;
1793 Udp4Header
= (EFI_UDP_HEADER
*) NetbufGetByte (Packet
, 0, NULL
);
1795 CopyMem (&Udp4Session
.SourceAddress
, &NetSession
->Source
, sizeof (EFI_IPv4_ADDRESS
));
1796 CopyMem (&Udp4Session
.DestinationAddress
, &NetSession
->Dest
, sizeof (EFI_IPv4_ADDRESS
));
1798 Udp4Session
.SourcePort
= NTOHS (Udp4Header
->DstPort
);
1799 Udp4Session
.DestinationPort
= NTOHS (Udp4Header
->SrcPort
);
1801 NET_LIST_FOR_EACH (Entry
, &Udp4Service
->ChildrenList
) {
1803 // Iterate all the instances.
1805 Instance
= NET_LIST_USER_STRUCT (Entry
, UDP4_INSTANCE_DATA
, Link
);
1807 if (!Instance
->Configured
||
1808 Instance
->ConfigData
.AcceptPromiscuous
||
1809 Instance
->ConfigData
.AcceptAnyPort
||
1810 EFI_IP4_EQUAL (&Instance
->ConfigData
.StationAddress
, &mZeroIp4Addr
)
1813 // Don't try to deliver the ICMP error to this instance if it is not configured,
1814 // or it's configured to be promiscuous or accept any port or accept all the
1820 if (Udp4MatchDgram (Instance
, &Udp4Session
)) {
1822 // Translate the Icmp Error code according to the udp spec.
1824 Instance
->IcmpError
= IpIoGetIcmpErrStatus (IcmpError
, IP_VERSION_4
, NULL
, NULL
);
1826 if (IcmpError
> ICMP_ERR_UNREACH_PORT
) {
1827 Instance
->IcmpError
= EFI_ICMP_ERROR
;
1831 // Notify the instance with the received Icmp Error.
1833 Udp4ReportIcmpError (Instance
);
1839 NetbufFree (Packet
);
1844 This function reports the received ICMP error.
1846 @param[in] Instance Pointer to the udp instance context data.
1850 Udp4ReportIcmpError (
1851 IN UDP4_INSTANCE_DATA
*Instance
1854 EFI_UDP4_COMPLETION_TOKEN
*Token
;
1856 if (NetMapIsEmpty (&Instance
->RxTokens
)) {
1858 // There are no receive tokens to deliver the ICMP error.
1863 if (EFI_ERROR (Instance
->IcmpError
)) {
1865 // Try to get a RxToken from the RxTokens map.
1867 Token
= (EFI_UDP4_COMPLETION_TOKEN
*) NetMapRemoveHead (&Instance
->RxTokens
, NULL
);
1869 if (Token
!= NULL
) {
1871 // Report the error through the Token.
1873 Token
->Status
= Instance
->IcmpError
;
1874 gBS
->SignalEvent (Token
->Event
);
1877 // Clear the IcmpError.
1879 Instance
->IcmpError
= EFI_SUCCESS
;
1886 This function is a dummy ext-free function for the NET_BUF created for the output
1889 @param[in] Context Pointer to the context data.
1893 Udp4NetVectorExtFree (
1901 Set the Udp4 variable data.
1903 @param Udp4Service Udp4 service data.
1905 @retval EFI_OUT_OF_RESOURCES There are not enough resources to set the
1907 @retval EFI_SUCCESS Set variable successfully.
1908 @retval other Set variable failed.
1912 Udp4SetVariableData (
1913 IN UDP4_SERVICE_DATA
*Udp4Service
1916 UINT32 NumConfiguredInstance
;
1918 UINTN VariableDataSize
;
1919 EFI_UDP4_VARIABLE_DATA
*Udp4VariableData
;
1920 EFI_UDP4_SERVICE_POINT
*Udp4ServicePoint
;
1921 UDP4_INSTANCE_DATA
*Udp4Instance
;
1922 CHAR16
*NewMacString
;
1925 NumConfiguredInstance
= 0;
1928 // Go through the children list to count the configured children.
1930 NET_LIST_FOR_EACH (Entry
, &Udp4Service
->ChildrenList
) {
1931 Udp4Instance
= NET_LIST_USER_STRUCT_S (
1935 UDP4_INSTANCE_DATA_SIGNATURE
1938 if (Udp4Instance
->Configured
) {
1939 NumConfiguredInstance
++;
1944 // Calculate the size of the Udp4VariableData. As there may be no Udp4 child,
1945 // we should add extra buffer for the service points only if the number of configured
1946 // children is more than 1.
1948 VariableDataSize
= sizeof (EFI_UDP4_VARIABLE_DATA
);
1950 if (NumConfiguredInstance
> 1) {
1951 VariableDataSize
+= sizeof (EFI_UDP4_SERVICE_POINT
) * (NumConfiguredInstance
- 1);
1954 Udp4VariableData
= AllocatePool (VariableDataSize
);
1955 if (Udp4VariableData
== NULL
) {
1956 return EFI_OUT_OF_RESOURCES
;
1959 Udp4VariableData
->DriverHandle
= Udp4Service
->ImageHandle
;
1960 Udp4VariableData
->ServiceCount
= NumConfiguredInstance
;
1962 Udp4ServicePoint
= &Udp4VariableData
->Services
[0];
1965 // Go through the children list to fill the configured children's address pairs.
1967 NET_LIST_FOR_EACH (Entry
, &Udp4Service
->ChildrenList
) {
1968 Udp4Instance
= NET_LIST_USER_STRUCT_S (
1972 UDP4_INSTANCE_DATA_SIGNATURE
1975 if (Udp4Instance
->Configured
) {
1976 Udp4ServicePoint
->InstanceHandle
= Udp4Instance
->ChildHandle
;
1977 Udp4ServicePoint
->LocalAddress
= Udp4Instance
->ConfigData
.StationAddress
;
1978 Udp4ServicePoint
->LocalPort
= Udp4Instance
->ConfigData
.StationPort
;
1979 Udp4ServicePoint
->RemoteAddress
= Udp4Instance
->ConfigData
.RemoteAddress
;
1980 Udp4ServicePoint
->RemotePort
= Udp4Instance
->ConfigData
.RemotePort
;
1987 // Get the mac string.
1989 Status
= NetLibGetMacString (
1990 Udp4Service
->ControllerHandle
,
1991 Udp4Service
->ImageHandle
,
1994 if (EFI_ERROR (Status
)) {
1998 if (Udp4Service
->MacString
!= NULL
) {
2000 // The variable is set already, we're going to update it.
2002 if (StrCmp (Udp4Service
->MacString
, NewMacString
) != 0) {
2004 // The mac address is changed, delete the previous variable first.
2007 Udp4Service
->MacString
,
2008 &gEfiUdp4ServiceBindingProtocolGuid
,
2009 EFI_VARIABLE_BOOTSERVICE_ACCESS
,
2015 gBS
->FreePool (Udp4Service
->MacString
);
2018 Udp4Service
->MacString
= NewMacString
;
2020 Status
= gRT
->SetVariable (
2021 Udp4Service
->MacString
,
2022 &gEfiUdp4ServiceBindingProtocolGuid
,
2023 EFI_VARIABLE_BOOTSERVICE_ACCESS
,
2025 (VOID
*) Udp4VariableData
2030 gBS
->FreePool (Udp4VariableData
);
2037 Clear the variable and free the resource.
2039 @param[[in] Udp4Service Udp4 service data.
2043 Udp4ClearVariableData (
2044 IN UDP4_SERVICE_DATA
*Udp4Service
2047 ASSERT (Udp4Service
->MacString
!= NULL
);
2050 Udp4Service
->MacString
,
2051 &gEfiUdp4ServiceBindingProtocolGuid
,
2052 EFI_VARIABLE_BOOTSERVICE_ACCESS
,
2057 gBS
->FreePool (Udp4Service
->MacString
);
2058 Udp4Service
->MacString
= NULL
;