3 Copyright (c) 2006 - 2008, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 The implementation of the Udp4 protocol.
26 UINT16 mUdp4RandomPort
;
29 This function checks and timeouts the I/O datagrams holding by the corresponding
32 @param Event The event this function registered to.
33 @param Context The context data registered during the creation of
47 This function finds the udp instance by the specified <Address, Port> pair.
49 @param InstanceList Pointer to the head of the list linking the udp
51 @param Address Pointer to the specified IPv4 address.
52 @param Port The udp port number.
54 @retval TRUE The specified <Address, Port> pair is found.
55 @retval FALSE Otherwise.
59 Udp4FindInstanceByPort (
60 IN LIST_ENTRY
*InstanceList
,
61 IN EFI_IPv4_ADDRESS
*Address
,
66 This function is the packet transmitting notify function registered to the IpIo
67 interface. It's called to signal the udp TxToken when IpIo layer completes the
68 transmitting of the udp datagram.
70 @param Status The completion status of the output udp datagram.
71 @param Context Pointer to the context data.
72 @param Sender Pointer to the Ip sender of the udp datagram.
73 @param NotifyData Pointer to the notify data.
87 This function processes the received datagram passed up by the IpIo layer.
89 @param Status The status of this udp datagram.
90 @param IcmpError The IcmpError code, only available when Status is
92 @param NetSession Pointer to the EFI_NET_SESSION_DATA.
93 @param Packet Pointer to the NET_BUF containing the received udp
95 @param Context Pointer to the context data.
102 IN EFI_STATUS Status
,
103 IN ICMP_ERROR IcmpError
,
104 IN EFI_NET_SESSION_DATA
*NetSession
,
110 This function cancels the token specified by Arg in the Map. This is a callback
111 used by Udp4InstanceCancelToken().
113 @param Map Pointer to the NET_MAP.
114 @param Item Pointer to the NET_MAP_ITEM.
115 @param Arg Pointer to the token to be cancelled, if NULL,
116 the token specified by Item is cancelled.
118 @retval EFI_SUCCESS The token is cancelled if Arg is NULL or the token
119 is not the same as that in the Item if Arg is not
121 @retval EFI_ABORTED Arg is not NULL, and the token specified by Arg is
128 IN NET_MAP_ITEM
*Item
,
129 IN VOID
*Arg OPTIONAL
133 This function matches the received udp datagram with the Instance.
135 @param Instance Pointer to the udp instance context data.
136 @param Udp4Session Pointer to the EFI_UDP4_SESSION_DATA abstracted
137 from the received udp datagram.
139 @retval TRUE The udp datagram matches the receiving requirments of the
141 @retval FALSE Otherwise.
146 IN UDP4_INSTANCE_DATA
*Instance
,
147 IN EFI_UDP4_SESSION_DATA
*Udp4Session
151 This function removes the Wrap specified by Context and release relevant resources.
153 @param Event The Event this notify function registered to.
154 @param Context Pointer to the context data.
161 Udp4RecycleRxDataWrap (
167 This function wraps the Packet and the RxData.
169 @param Instance Pointer to the instance context data.
170 @param Packet Pointer to the buffer containing the received
172 @param RxData Pointer to the EFI_UDP4_RECEIVE_DATA of this
175 @return Pointer to the structure wrapping the RxData and the Packet.
180 IN UDP4_INSTANCE_DATA
*Instance
,
182 IN EFI_UDP4_RECEIVE_DATA
*RxData
186 This function enqueues the received datagram into the instances' receiving queues.
188 @param Udp4Service Pointer to the udp service context data.
189 @param Packet Pointer to the buffer containing the received
191 @param RxData Pointer to the EFI_UDP4_RECEIVE_DATA of this
194 @return The times this datagram is enqueued.
199 IN UDP4_SERVICE_DATA
*Udp4Service
,
201 IN EFI_UDP4_RECEIVE_DATA
*RxData
205 This function delivers the datagrams enqueued in the instances.
207 @param Udp4Service Pointer to the udp service context data.
214 IN UDP4_SERVICE_DATA
*Udp4Service
218 This function demultiplexes the received udp datagram to the apropriate instances.
220 @param Udp4Service Pointer to the udp service context data.
221 @param NetSession Pointer to the EFI_NET_SESSION_DATA abstrated from
222 the received datagram.
223 @param Packet Pointer to the buffer containing the received udp
231 IN UDP4_SERVICE_DATA
*Udp4Service
,
232 IN EFI_NET_SESSION_DATA
*NetSession
,
237 This function handles the received Icmp Error message and demultiplexes it to the
240 @param Udp4Service Pointer to the udp service context data.
241 @param IcmpError The icmp error code.
242 @param NetSession Pointer to the EFI_NET_SESSION_DATA abstracted
243 from the received Icmp Error packet.
244 @param Packet Pointer to the Icmp Error packet.
251 IN UDP4_SERVICE_DATA
*Udp4Service
,
252 IN ICMP_ERROR IcmpError
,
253 IN EFI_NET_SESSION_DATA
*NetSession
,
258 This function builds and sends out a icmp port unreachable message.
260 @param IpIo Pointer to the IP_IO instance.
261 @param NetSession Pointer to the EFI_NET_SESSION_DATA of the packet
262 causes this icmp error message.
263 @param Udp4Header Pointer to the udp header of the datagram causes
264 this icmp error message.
270 Udp4SendPortUnreach (
272 IN EFI_NET_SESSION_DATA
*NetSession
,
278 Create the Udp service context data.
280 @param Udp4Service Pointer to the UDP4_SERVICE_DATA.
281 @param ImageHandle The image handle of this udp4 driver.
282 @param ControllerHandle The controller handle this udp4 driver binds on.
284 @retval EFI_SUCCESS The udp4 service context data is created and
286 @retval EFI_OUT_OF_RESOURCES Cannot allocate memory.
287 @retval other Other error occurs.
292 IN OUT UDP4_SERVICE_DATA
*Udp4Service
,
293 IN EFI_HANDLE ImageHandle
,
294 IN EFI_HANDLE ControllerHandle
298 IP_IO_OPEN_DATA OpenData
;
300 ZeroMem (Udp4Service
, sizeof (UDP4_SERVICE_DATA
));
302 Udp4Service
->Signature
= UDP4_SERVICE_DATA_SIGNATURE
;
303 Udp4Service
->ServiceBinding
= mUdp4ServiceBinding
;
304 Udp4Service
->ImageHandle
= ImageHandle
;
305 Udp4Service
->ControllerHandle
= ControllerHandle
;
306 Udp4Service
->ChildrenNumber
= 0;
308 InitializeListHead (&Udp4Service
->ChildrenList
);
311 // Create the IpIo for this service context.
313 Udp4Service
->IpIo
= IpIoCreate (ImageHandle
, ControllerHandle
);
314 if (Udp4Service
->IpIo
== NULL
) {
315 return EFI_OUT_OF_RESOURCES
;
319 // Set the OpenData used to open the IpIo.
321 CopyMem (&OpenData
.IpConfigData
, &mIpIoDefaultIpConfigData
, sizeof (OpenData
.IpConfigData
));
322 OpenData
.IpConfigData
.AcceptBroadcast
= TRUE
;
323 OpenData
.RcvdContext
= (VOID
*) Udp4Service
;
324 OpenData
.SndContext
= NULL
;
325 OpenData
.PktRcvdNotify
= Udp4DgramRcvd
;
326 OpenData
.PktSentNotify
= Udp4DgramSent
;
329 // Configure and start the IpIo.
331 Status
= IpIoOpen (Udp4Service
->IpIo
, &OpenData
);
332 if (EFI_ERROR (Status
)) {
337 // Create the event for Udp timeout checking.
339 Status
= gBS
->CreateEvent (
340 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
344 &Udp4Service
->TimeoutEvent
346 if (EFI_ERROR (Status
)) {
351 // Start the timeout timer event.
353 Status
= gBS
->SetTimer (
354 Udp4Service
->TimeoutEvent
,
356 UDP4_TIMEOUT_INTERVAL
358 if (EFI_ERROR (Status
)) {
366 if (Udp4Service
->TimeoutEvent
!= NULL
) {
367 gBS
->CloseEvent (Udp4Service
->TimeoutEvent
);
370 IpIoDestroy (Udp4Service
->IpIo
);
377 Clean the Udp service context data.
379 @param Udp4Service Pointer to the UDP4_SERVICE_DATA.
386 IN UDP4_SERVICE_DATA
*Udp4Service
390 // Cancel the TimeoutEvent timer.
392 gBS
->SetTimer (Udp4Service
->TimeoutEvent
, TimerCancel
, 0);
395 // Close the TimeoutEvent timer.
397 gBS
->CloseEvent (Udp4Service
->TimeoutEvent
);
402 IpIoDestroy (Udp4Service
->IpIo
);
407 This function checks and timeouts the I/O datagrams holding by the corresponding
410 @param Event The event this function registered to.
411 @param Context The context data registered during the creation of
424 UDP4_SERVICE_DATA
*Udp4Service
;
426 UDP4_INSTANCE_DATA
*Instance
;
427 LIST_ENTRY
*WrapEntry
;
428 LIST_ENTRY
*NextEntry
;
429 UDP4_RXDATA_WRAP
*Wrap
;
431 Udp4Service
= (UDP4_SERVICE_DATA
*) Context
;
432 NET_CHECK_SIGNATURE (Udp4Service
, UDP4_SERVICE_DATA_SIGNATURE
);
434 NET_LIST_FOR_EACH (Entry
, &Udp4Service
->ChildrenList
) {
436 // Iterate all the instances belonging to this service context.
438 Instance
= NET_LIST_USER_STRUCT (Entry
, UDP4_INSTANCE_DATA
, Link
);
439 NET_CHECK_SIGNATURE (Instance
, UDP4_INSTANCE_DATA_SIGNATURE
);
441 if (!Instance
->Configured
|| (Instance
->ConfigData
.ReceiveTimeout
== 0)) {
443 // Skip this instance if it's not configured or no receive timeout.
448 NET_LIST_FOR_EACH_SAFE (WrapEntry
, NextEntry
, &Instance
->RcvdDgramQue
) {
450 // Iterate all the rxdatas belonging to this udp instance.
452 Wrap
= NET_LIST_USER_STRUCT (WrapEntry
, UDP4_RXDATA_WRAP
, Link
);
454 if (Wrap
->TimeoutTick
<= UDP4_TIMEOUT_INTERVAL
/ 10) {
456 // Remove this RxData if it timeouts.
458 Udp4RecycleRxDataWrap (NULL
, (VOID
*) Wrap
);
460 Wrap
->TimeoutTick
-= UDP4_TIMEOUT_INTERVAL
/ 10;
468 This function intializes the new created udp instance.
470 @param Udp4Service Pointer to the UDP4_SERVICE_DATA.
471 @param Instance Pointer to the un-initialized UDP4_INSTANCE_DATA.
478 IN UDP4_SERVICE_DATA
*Udp4Service
,
479 IN OUT UDP4_INSTANCE_DATA
*Instance
483 // Set the signature.
485 Instance
->Signature
= UDP4_INSTANCE_DATA_SIGNATURE
;
490 InitializeListHead (&Instance
->Link
);
491 InitializeListHead (&Instance
->RcvdDgramQue
);
492 InitializeListHead (&Instance
->DeliveredDgramQue
);
495 // Init the NET_MAPs.
497 NetMapInit (&Instance
->TxTokens
);
498 NetMapInit (&Instance
->RxTokens
);
499 NetMapInit (&Instance
->McastIps
);
502 // Save the pointer to the UDP4_SERVICE_DATA, and initialize other members.
504 Instance
->Udp4Service
= Udp4Service
;
505 CopyMem (&Instance
->Udp4Proto
, &mUdp4Protocol
, sizeof (Instance
->Udp4Proto
));
506 Instance
->IcmpError
= EFI_SUCCESS
;
507 Instance
->Configured
= FALSE
;
508 Instance
->IsNoMapping
= FALSE
;
509 Instance
->Destroyed
= FALSE
;
514 This function cleans the udp instance.
516 @param Instance Pointer to the UDP4_INSTANCE_DATA to clean.
523 IN UDP4_INSTANCE_DATA
*Instance
526 NetMapClean (&Instance
->McastIps
);
527 NetMapClean (&Instance
->RxTokens
);
528 NetMapClean (&Instance
->TxTokens
);
533 This function finds the udp instance by the specified <Address, Port> pair.
535 @param InstanceList Pointer to the head of the list linking the udp
537 @param Address Pointer to the specified IPv4 address.
538 @param Port The udp port number.
540 @retval TRUE The specified <Address, Port> pair is found.
541 @retval FALSE Otherwise.
545 Udp4FindInstanceByPort (
546 IN LIST_ENTRY
*InstanceList
,
547 IN EFI_IPv4_ADDRESS
*Address
,
552 UDP4_INSTANCE_DATA
*Instance
;
553 EFI_UDP4_CONFIG_DATA
*ConfigData
;
555 NET_LIST_FOR_EACH (Entry
, InstanceList
) {
557 // Iterate all the udp instances.
559 Instance
= NET_LIST_USER_STRUCT (Entry
, UDP4_INSTANCE_DATA
, Link
);
560 ConfigData
= &Instance
->ConfigData
;
562 if (!Instance
->Configured
|| ConfigData
->AcceptAnyPort
) {
564 // If the instance is not configured or the configdata of the instance indicates
565 // this instance accepts any port, skip it.
570 if (EFI_IP4_EQUAL (&ConfigData
->StationAddress
, Address
) &&
571 (ConfigData
->StationPort
== Port
)) {
573 // if both the address and the port are the same, return TRUE.
580 // return FALSE when matching fails.
587 This function tries to bind the udp instance according to the configured port
590 @param InstanceList Pointer to the head of the list linking the udp
592 @param ConfigData Pointer to the ConfigData of the instance to be
593 bound. ConfigData->StationPort will be assigned
594 with an available port value on success.
596 @retval EFI_SUCCESS The bound operation is completed successfully.
597 @retval EFI_ACCESS_DENIED The <Address, Port> specified by the ConfigData is
598 already used by other instance.
599 @retval EFI_OUT_OF_RESOURCES No available port resources.
604 IN LIST_ENTRY
*InstanceList
,
605 IN OUT EFI_UDP4_CONFIG_DATA
*ConfigData
608 EFI_IPv4_ADDRESS
*StationAddress
;
611 if (ConfigData
->AcceptAnyPort
) {
615 StationAddress
= &ConfigData
->StationAddress
;
617 if (ConfigData
->StationPort
!= 0) {
619 if (!ConfigData
->AllowDuplicatePort
&&
620 Udp4FindInstanceByPort (InstanceList
, StationAddress
, ConfigData
->StationPort
)) {
622 // Do not allow duplicate port and the port is already used by other instance.
624 return EFI_ACCESS_DENIED
;
628 // select a random port for this instance;
631 if (ConfigData
->AllowDuplicatePort
) {
633 // Just pick up the random port if the instance allows duplicate port.
635 ConfigData
->StationPort
= mUdp4RandomPort
;
638 StartPort
= mUdp4RandomPort
;
640 while (Udp4FindInstanceByPort(InstanceList
, StationAddress
, mUdp4RandomPort
)) {
643 if (mUdp4RandomPort
== 0) {
644 mUdp4RandomPort
= UDP4_PORT_KNOWN
;
647 if (mUdp4RandomPort
== StartPort
) {
649 // No available port.
651 return EFI_OUT_OF_RESOURCES
;
655 ConfigData
->StationPort
= mUdp4RandomPort
;
659 if (mUdp4RandomPort
== 0) {
660 mUdp4RandomPort
= UDP4_PORT_KNOWN
;
669 This function is used to check whether the NewConfigData has any un-reconfigurable
670 parameters changed compared to the OldConfigData.
672 @param OldConfigData Pointer to the current ConfigData the udp instance
674 @param NewConfigData Pointer to the new ConfigData.
676 @retval TRUE The instance is reconfigurable.
677 @retval FALSE Otherwise.
681 Udp4IsReconfigurable (
682 IN EFI_UDP4_CONFIG_DATA
*OldConfigData
,
683 IN EFI_UDP4_CONFIG_DATA
*NewConfigData
686 if ((NewConfigData
->AcceptAnyPort
!= OldConfigData
->AcceptAnyPort
) ||
687 (NewConfigData
->AcceptBroadcast
!= OldConfigData
->AcceptBroadcast
) ||
688 (NewConfigData
->AcceptPromiscuous
!= OldConfigData
->AcceptPromiscuous
) ||
689 (NewConfigData
->AllowDuplicatePort
!= OldConfigData
->AllowDuplicatePort
)
692 // The receiving filter parameters cannot be changed.
697 if ((!NewConfigData
->AcceptAnyPort
) &&
698 (NewConfigData
->StationPort
!= OldConfigData
->StationPort
)
701 // The port is not changeable.
706 if (!NewConfigData
->AcceptPromiscuous
) {
708 if (NewConfigData
->UseDefaultAddress
!= OldConfigData
->UseDefaultAddress
) {
710 // The NewConfigData differs to the old one on the UseDefaultAddress.
715 if (!NewConfigData
->UseDefaultAddress
&&
716 (!EFI_IP4_EQUAL (&NewConfigData
->StationAddress
, &OldConfigData
->StationAddress
) ||
717 !EFI_IP4_EQUAL (&NewConfigData
->SubnetMask
, &OldConfigData
->SubnetMask
))
720 // If the instance doesn't use the default address, and the new address or
721 // new subnet mask is different from the old values.
727 if (!EFI_IP4_EQUAL (&NewConfigData
->RemoteAddress
, &OldConfigData
->RemoteAddress
)) {
729 // The remoteaddress is not the same.
734 if (!EFI_IP4_EQUAL (&NewConfigData
->RemoteAddress
, &mZeroIp4Addr
) &&
735 NewConfigData
->RemotePort
!= OldConfigData
->RemotePort
738 // The RemotePort differs if it's designated in the configdata.
744 // All checks pass, return TRUE.
751 This function builds the Ip4 configdata from the Udp4ConfigData.
753 @param Udp4ConfigData Pointer to the EFI_UDP4_CONFIG_DATA.
754 @param Ip4ConfigData Pointer to the EFI_IP4_CONFIG_DATA.
760 Udp4BuildIp4ConfigData (
761 IN EFI_UDP4_CONFIG_DATA
*Udp4ConfigData
,
762 IN OUT EFI_IP4_CONFIG_DATA
*Ip4ConfigData
765 CopyMem (Ip4ConfigData
, &mIpIoDefaultIpConfigData
, sizeof (*Ip4ConfigData
));
767 Ip4ConfigData
->DefaultProtocol
= EFI_IP_PROTO_UDP
;
768 Ip4ConfigData
->AcceptBroadcast
= Udp4ConfigData
->AcceptBroadcast
;
769 Ip4ConfigData
->AcceptPromiscuous
= Udp4ConfigData
->AcceptPromiscuous
;
770 Ip4ConfigData
->UseDefaultAddress
= Udp4ConfigData
->UseDefaultAddress
;
771 CopyMem (&Ip4ConfigData
->StationAddress
, &Udp4ConfigData
->StationAddress
, sizeof (EFI_IPv4_ADDRESS
));
772 CopyMem (&Ip4ConfigData
->SubnetMask
, &Udp4ConfigData
->SubnetMask
, sizeof (EFI_IPv4_ADDRESS
));
775 // use the -1 magic number to disable the receiving process of the ip instance.
777 Ip4ConfigData
->ReceiveTimeout
= (UINT32
) (-1);
782 This function validates the TxToken, it returns the error code according to the spec.
784 @param Instance Pointer to the udp instance context data.
785 @param TxToken Pointer to the token to be checked.
787 @retval EFI_SUCCESS The TxToken is valid.
788 @retval EFI_INVALID_PARAMETER One or more of the following are TRUE: This is
789 NULL. Token is NULL. Token.Event is NULL.
790 Token.Packet.TxData is NULL.
791 Token.Packet.TxData.FragmentCount is zero.
792 Token.Packet.TxData.DataLength is not equal to the
793 sum of fragment lengths. One or more of the
794 Token.Packet.TxData.FragmentTable[].
795 FragmentLength fields is zero. One or more of the
796 Token.Packet.TxData.FragmentTable[].
797 FragmentBuffer fields is NULL.
798 Token.Packet.TxData. GatewayAddress is not a
799 unicast IPv4 address if it is not NULL. One or
800 more IPv4 addresses in Token.Packet.TxData.
801 UdpSessionData are not valid unicast IPv4
802 addresses if the UdpSessionData is not NULL.
803 @retval EFI_BAD_BUFFER_SIZE The data length is greater than the maximum UDP
808 Udp4ValidateTxToken (
809 IN UDP4_INSTANCE_DATA
*Instance
,
810 IN EFI_UDP4_COMPLETION_TOKEN
*TxToken
813 EFI_UDP4_TRANSMIT_DATA
*TxData
;
816 EFI_UDP4_CONFIG_DATA
*ConfigData
;
817 EFI_UDP4_SESSION_DATA
*UdpSessionData
;
818 IP4_ADDR SourceAddress
;
819 IP4_ADDR GatewayAddress
;
821 if (TxToken
->Event
== NULL
) {
822 return EFI_INVALID_PARAMETER
;
825 TxData
= TxToken
->Packet
.TxData
;
827 if ((TxData
== NULL
) || (TxData
->FragmentCount
== 0)) {
828 return EFI_INVALID_PARAMETER
;
832 for (Index
= 0; Index
< TxData
->FragmentCount
; Index
++) {
834 if ((TxData
->FragmentTable
[Index
].FragmentBuffer
== NULL
) ||
835 (TxData
->FragmentTable
[Index
].FragmentLength
== 0)) {
837 // if the FragmentBuffer is NULL or the FragmentLeng is zero.
839 return EFI_INVALID_PARAMETER
;
842 TotalLen
+= TxData
->FragmentTable
[Index
].FragmentLength
;
845 if (TotalLen
!= TxData
->DataLength
) {
847 // The TotalLen calculated by adding all the FragmentLeng doesn't equal to the
850 return EFI_INVALID_PARAMETER
;
853 if (TxData
->GatewayAddress
!= NULL
) {
854 CopyMem (&GatewayAddress
, TxData
->GatewayAddress
, sizeof (IP4_ADDR
));
856 if (!Ip4IsUnicast (NTOHL (GatewayAddress
), 0)) {
858 // The specified GatewayAddress is not a unicast IPv4 address while it's not 0.
860 return EFI_INVALID_PARAMETER
;
864 ConfigData
= &Instance
->ConfigData
;
865 UdpSessionData
= TxData
->UdpSessionData
;
867 if (UdpSessionData
!= NULL
) {
869 CopyMem (&SourceAddress
, &UdpSessionData
->SourceAddress
, sizeof (IP4_ADDR
));
871 if ((SourceAddress
!= 0) && !Ip4IsUnicast (HTONL (SourceAddress
), 0)) {
873 // Check whether SourceAddress is a valid IPv4 address in case it's not zero.
874 // The configured station address is used if SourceAddress is zero.
876 return EFI_INVALID_PARAMETER
;
879 if ((UdpSessionData
->DestinationPort
== 0) && (ConfigData
->RemotePort
== 0)) {
881 // Ambiguous, no avalaible DestinationPort for this token.
883 return EFI_INVALID_PARAMETER
;
886 if (EFI_IP4_EQUAL (&UdpSessionData
->DestinationAddress
, &mZeroIp4Addr
)) {
888 // The DestinationAddress specified in the UdpSessionData is 0.
890 return EFI_INVALID_PARAMETER
;
892 } else if (EFI_IP4_EQUAL (&ConfigData
->RemoteAddress
, &mZeroIp4Addr
)) {
894 // the configured RemoteAddress is all zero, and the user doens't override the
895 // destination address.
897 return EFI_INVALID_PARAMETER
;
900 if (TxData
->DataLength
> UDP4_MAX_DATA_SIZE
) {
901 return EFI_BAD_BUFFER_SIZE
;
909 This function checks whether the specified Token duplicates with the one in the Map.
911 @param Map Pointer to the NET_MAP.
912 @param Item Pointer to the NET_MAP_ITEM contain the pointer to
914 @param Context Pointer to the Token to be checked.
916 @retval EFI_SUCCESS The Token specified by Context differs from the
918 @retval EFI_ACCESS_DENIED The Token duplicates with the one in the Item.
924 IN NET_MAP_ITEM
*Item
,
928 EFI_UDP4_COMPLETION_TOKEN
*Token
;
929 EFI_UDP4_COMPLETION_TOKEN
*TokenInItem
;
931 Token
= (EFI_UDP4_COMPLETION_TOKEN
*) Context
;
932 TokenInItem
= (EFI_UDP4_COMPLETION_TOKEN
*) Item
->Key
;
934 if ((Token
== TokenInItem
) || (Token
->Event
== TokenInItem
->Event
)) {
936 // The Token duplicates with the TokenInItem in case either the two pointers are the
937 // same or the Events of these two tokens are the same.
939 return EFI_ACCESS_DENIED
;
947 This function calculates the checksum for the Packet, utilizing the pre-calculated
948 pseudo HeadSum to reduce some overhead.
950 @param Packet Pointer to the NET_BUF contains the udp datagram.
951 @param HeadSum Checksum of the pseudo header execpt the length
954 @return The 16-bit checksum of this udp datagram.
965 Checksum
= NetbufChecksum (Packet
);
966 Checksum
= NetAddChecksum (Checksum
, HeadSum
);
968 Checksum
= NetAddChecksum (Checksum
, HTONS ((UINT16
) Packet
->TotalSize
));
970 return (UINT16
) ~Checksum
;
975 This function removes the specified Token from the TokenMap.
977 @param TokenMap Pointer to the NET_MAP containing the tokens.
978 @param Token Pointer to the Token to be removed.
980 @retval EFI_SUCCESS The specified Token is removed from the TokenMap.
981 @retval EFI_NOT_FOUND The specified Token is not found in the TokenMap.
986 IN OUT NET_MAP
*TokenMap
,
987 IN EFI_UDP4_COMPLETION_TOKEN
*Token
993 // Find the Token first.
995 Item
= NetMapFindKey (TokenMap
, (VOID
*) Token
);
999 // Remove the token if it's found in the map.
1001 NetMapRemoveItem (TokenMap
, Item
, NULL
);
1006 return EFI_NOT_FOUND
;
1011 This function is the packet transmitting notify function registered to the IpIo
1012 interface. It's called to signal the udp TxToken when IpIo layer completes the
1013 transmitting of the udp datagram.
1015 @param Status The completion status of the output udp datagram.
1016 @param Context Pointer to the context data.
1017 @param Sender Pointer to the Ip sender of the udp datagram.
1018 @param NotifyData Pointer to the notify data.
1025 IN EFI_STATUS Status
,
1031 UDP4_INSTANCE_DATA
*Instance
;
1032 EFI_UDP4_COMPLETION_TOKEN
*Token
;
1034 Instance
= (UDP4_INSTANCE_DATA
*) Context
;
1035 Token
= (EFI_UDP4_COMPLETION_TOKEN
*) NotifyData
;
1037 if (Udp4RemoveToken (&Instance
->TxTokens
, Token
) == EFI_SUCCESS
) {
1039 // The token may be cancelled. Only signal it if the remove operation succeeds.
1041 Token
->Status
= Status
;
1042 gBS
->SignalEvent (Token
->Event
);
1043 NetLibDispatchDpc ();
1049 This function processes the received datagram passed up by the IpIo layer.
1051 @param Status The status of this udp datagram.
1052 @param IcmpError The IcmpError code, only available when Status is
1054 @param NetSession Pointer to the EFI_NET_SESSION_DATA.
1055 @param Packet Pointer to the NET_BUF containing the received udp
1057 @param Context Pointer to the context data.
1064 IN EFI_STATUS Status
,
1065 IN ICMP_ERROR IcmpError
,
1066 IN EFI_NET_SESSION_DATA
*NetSession
,
1071 NET_CHECK_SIGNATURE (Packet
, NET_BUF_SIGNATURE
);
1074 // IpIo only passes received packets with Status EFI_SUCCESS or EFI_ICMP_ERROR.
1076 if (Status
== EFI_SUCCESS
) {
1078 // Demultiplex the received datagram.
1080 Udp4Demultiplex ((UDP4_SERVICE_DATA
*) Context
, NetSession
, Packet
);
1083 // Handle the ICMP_ERROR packet.
1085 Udp4IcmpHandler ((UDP4_SERVICE_DATA
*) Context
, IcmpError
, NetSession
, Packet
);
1089 // Dispatch the DPC queued by the NotifyFunction of the rx token's events
1090 // which are signaled with received data.
1092 NetLibDispatchDpc ();
1097 This function removes the multicast group specified by Arg from the Map.
1099 @param Map Pointer to the NET_MAP.
1100 @param Item Pointer to the NET_MAP_ITEM.
1101 @param Arg Pointer to the Arg, it's the pointer to a
1102 multicast IPv4 Address.
1104 @retval EFI_SUCCESS The multicast address is removed.
1105 @retval EFI_ABORTED The specified multicast address is removed and the
1111 IN OUT NET_MAP
*Map
,
1112 IN NET_MAP_ITEM
*Item
,
1113 IN VOID
*Arg OPTIONAL
1116 EFI_IPv4_ADDRESS
*McastIp
;
1120 if ((McastIp
!= NULL
) && (!EFI_IP4_EQUAL (McastIp
, &(Item
->Key
)))) {
1122 // McastIp is not NULL and the multicast address contained in the Item
1123 // is not the same as McastIp.
1129 // Remove this Item.
1131 NetMapRemoveItem (Map
, Item
, NULL
);
1133 if (McastIp
!= NULL
) {
1135 // Return EFI_ABORTED in case McastIp is not NULL to terminate the iteration.
1145 This function cancels the token specified by Arg in the Map. This is a callback
1146 used by Udp4InstanceCancelToken().
1148 @param Map Pointer to the NET_MAP.
1149 @param Item Pointer to the NET_MAP_ITEM.
1150 @param Arg Pointer to the token to be cancelled, if NULL,
1151 the token specified by Item is cancelled.
1153 @retval EFI_SUCCESS The token is cancelled if Arg is NULL or the token
1154 is not the same as that in the Item if Arg is not
1156 @retval EFI_ABORTED Arg is not NULL, and the token specified by Arg is
1163 IN NET_MAP_ITEM
*Item
,
1164 IN VOID
*Arg OPTIONAL
1167 EFI_UDP4_COMPLETION_TOKEN
*TokenToCancel
;
1171 if ((Arg
!= NULL
) && (Item
->Key
!= Arg
)) {
1175 if (Item
->Value
!= NULL
) {
1177 // If the token is a transmit token, the corresponding Packet is recorded in
1178 // Item->Value, invoke IpIo to cancel this packet first. The IpIoCancelTxToken
1179 // will invoke Udp4DgramSent, the token will be signaled and this Item will
1180 // be removed from the Map there.
1182 Packet
= (NET_BUF
*) (Item
->Value
);
1183 IpIo
= (IP_IO
*) (*((UINTN
*) &Packet
->ProtoData
[0]));
1185 IpIoCancelTxToken (IpIo
, Packet
);
1188 // The token is a receive token. Abort it and remove it from the Map.
1190 TokenToCancel
= (EFI_UDP4_COMPLETION_TOKEN
*) Item
->Key
;
1191 NetMapRemoveItem (Map
, Item
, NULL
);
1193 TokenToCancel
->Status
= EFI_ABORTED
;
1194 gBS
->SignalEvent (TokenToCancel
->Event
);
1206 This function removes all the Wrap datas in the RcvdDgramQue.
1208 @param Instance Pointer to the udp instance context data.
1214 Udp4FlushRcvdDgram (
1215 IN UDP4_INSTANCE_DATA
*Instance
1218 UDP4_RXDATA_WRAP
*Wrap
;
1220 while (!IsListEmpty (&Instance
->RcvdDgramQue
)) {
1222 // Iterate all the Wraps in the RcvdDgramQue.
1224 Wrap
= NET_LIST_HEAD (&Instance
->RcvdDgramQue
, UDP4_RXDATA_WRAP
, Link
);
1227 // The Wrap will be removed from the RcvdDgramQue by this function call.
1229 Udp4RecycleRxDataWrap (NULL
, (VOID
*) Wrap
);
1236 Cancel Udp4 tokens from the Udp4 instance.
1238 @param Instance Pointer to the udp instance context data.
1239 @param Token Pointer to the token to be canceled, if NULL, all
1240 tokens in this instance will be cancelled.
1242 @retval EFI_SUCCESS The Token is cancelled.
1243 @retval EFI_NOT_FOUND The Token is not found.
1247 Udp4InstanceCancelToken (
1248 IN UDP4_INSTANCE_DATA
*Instance
,
1249 IN EFI_UDP4_COMPLETION_TOKEN
*Token OPTIONAL
1255 // Cancel this token from the TxTokens map.
1257 Status
= NetMapIterate (&Instance
->TxTokens
, Udp4CancelTokens
, Token
);
1259 if ((Token
!= NULL
) && (Status
== EFI_ABORTED
)) {
1261 // If Token isn't NULL and Status is EFI_ABORTED, the token is cancelled from
1262 // the TxTokens, just return success.
1268 // Try to cancel this token from the RxTokens map in condition either the Token
1269 // is NULL or the specified Token is not in TxTokens.
1271 Status
= NetMapIterate (&Instance
->RxTokens
, Udp4CancelTokens
, Token
);
1273 if ((Token
!= NULL
) && (Status
== EFI_SUCCESS
)) {
1275 // If Token isn't NULL and Status is EFI_SUCCESS, the token is neither in the
1276 // TxTokens nor the RxTokens, or say, it's not found.
1278 return EFI_NOT_FOUND
;
1281 ASSERT ((Token
!= NULL
) || ((0 == NetMapGetCount (&Instance
->TxTokens
))
1282 && (0 == NetMapGetCount (&Instance
->RxTokens
))));
1289 This function matches the received udp datagram with the Instance.
1291 @param Instance Pointer to the udp instance context data.
1292 @param Udp4Session Pointer to the EFI_UDP4_SESSION_DATA abstracted
1293 from the received udp datagram.
1295 @retval TRUE The udp datagram matches the receiving requirments of the
1297 @retval FALSE Otherwise.
1302 IN UDP4_INSTANCE_DATA
*Instance
,
1303 IN EFI_UDP4_SESSION_DATA
*Udp4Session
1306 EFI_UDP4_CONFIG_DATA
*ConfigData
;
1307 IP4_ADDR Destination
;
1309 ConfigData
= &Instance
->ConfigData
;
1311 if (ConfigData
->AcceptPromiscuous
) {
1313 // Always matches if this instance is in the promiscuous state.
1318 if ((!ConfigData
->AcceptAnyPort
&& (Udp4Session
->DestinationPort
!= ConfigData
->StationPort
)) ||
1319 ((ConfigData
->RemotePort
!= 0) && (Udp4Session
->SourcePort
!= ConfigData
->RemotePort
))
1322 // The local port or the remote port doesn't match.
1327 if (!EFI_IP4_EQUAL (&ConfigData
->RemoteAddress
, &mZeroIp4Addr
) &&
1328 !EFI_IP4_EQUAL (&ConfigData
->RemoteAddress
, &Udp4Session
->SourceAddress
)
1331 // This datagram doesn't come from the instance's specified sender.
1336 if (EFI_IP4_EQUAL (&ConfigData
->StationAddress
, &mZeroIp4Addr
) ||
1337 EFI_IP4_EQUAL (&Udp4Session
->DestinationAddress
, &ConfigData
->StationAddress
)
1340 // The instance is configured to receive datagrams destined to any station IP or
1341 // the destination address of this datagram matches the configured station IP.
1346 CopyMem (&Destination
, &Udp4Session
->DestinationAddress
, sizeof (IP4_ADDR
));
1348 if (IP4_IS_LOCAL_BROADCAST (Destination
) && ConfigData
->AcceptBroadcast
) {
1350 // The instance is configured to receive broadcast and this is a broadcast packet.
1355 if (IP4_IS_MULTICAST (NTOHL (Destination
)) &&
1356 NetMapFindKey (&Instance
->McastIps
, (VOID
*) (UINTN
) Destination
) != NULL
1359 // It's a multicast packet and the multicast address is accepted by this instance.
1369 This function removes the Wrap specified by Context and release relevant resources.
1371 @param Event The Event this notify function registered to.
1372 @param Context Pointer to the context data.
1379 Udp4RecycleRxDataWrap (
1384 UDP4_RXDATA_WRAP
*Wrap
;
1386 Wrap
= (UDP4_RXDATA_WRAP
*) Context
;
1389 // Remove the Wrap from the list it belongs to.
1391 RemoveEntryList (&Wrap
->Link
);
1394 // Free the Packet associated with this Wrap.
1396 NetbufFree (Wrap
->Packet
);
1401 gBS
->CloseEvent (Wrap
->RxData
.RecycleSignal
);
1403 gBS
->FreePool (Wrap
);
1408 This function wraps the Packet and the RxData.
1410 @param Instance Pointer to the instance context data.
1411 @param Packet Pointer to the buffer containing the received
1413 @param RxData Pointer to the EFI_UDP4_RECEIVE_DATA of this
1416 @return Pointer to the structure wrapping the RxData and the Packet.
1421 IN UDP4_INSTANCE_DATA
*Instance
,
1423 IN EFI_UDP4_RECEIVE_DATA
*RxData
1427 UDP4_RXDATA_WRAP
*Wrap
;
1430 // Allocate buffer for the Wrap.
1432 Wrap
= AllocatePool (sizeof (UDP4_RXDATA_WRAP
) +
1433 (Packet
->BlockOpNum
- 1) * sizeof (EFI_UDP4_FRAGMENT_DATA
));
1438 InitializeListHead (&Wrap
->Link
);
1440 CopyMem (&Wrap
->RxData
, RxData
, sizeof (Wrap
->RxData
));
1443 // Create the Recycle event.
1445 Status
= gBS
->CreateEvent (
1448 Udp4RecycleRxDataWrap
,
1450 &Wrap
->RxData
.RecycleSignal
1452 if (EFI_ERROR (Status
)) {
1453 gBS
->FreePool (Wrap
);
1457 Wrap
->Packet
= Packet
;
1458 Wrap
->TimeoutTick
= Instance
->ConfigData
.ReceiveTimeout
;
1465 This function enqueues the received datagram into the instances' receiving queues.
1467 @param Udp4Service Pointer to the udp service context data.
1468 @param Packet Pointer to the buffer containing the received
1470 @param RxData Pointer to the EFI_UDP4_RECEIVE_DATA of this
1473 @return The times this datagram is enqueued.
1478 IN UDP4_SERVICE_DATA
*Udp4Service
,
1480 IN EFI_UDP4_RECEIVE_DATA
*RxData
1484 UDP4_INSTANCE_DATA
*Instance
;
1485 UDP4_RXDATA_WRAP
*Wrap
;
1490 NET_LIST_FOR_EACH (Entry
, &Udp4Service
->ChildrenList
) {
1492 // Iterate the instances.
1494 Instance
= NET_LIST_USER_STRUCT (Entry
, UDP4_INSTANCE_DATA
, Link
);
1496 if (!Instance
->Configured
) {
1500 if (Udp4MatchDgram (Instance
, &RxData
->UdpSession
)) {
1502 // Wrap the RxData and put this Wrap into the instances RcvdDgramQue.
1504 Wrap
= Udp4WrapRxData (Instance
, Packet
, RxData
);
1509 NET_GET_REF (Packet
);
1511 InsertTailList (&Instance
->RcvdDgramQue
, &Wrap
->Link
);
1522 This function delivers the received datagrams for the specified instance.
1524 @param Instance Pointer to the instance context data.
1530 Udp4InstanceDeliverDgram (
1531 IN UDP4_INSTANCE_DATA
*Instance
1534 UDP4_RXDATA_WRAP
*Wrap
;
1535 EFI_UDP4_COMPLETION_TOKEN
*Token
;
1537 EFI_UDP4_RECEIVE_DATA
*RxData
;
1540 if (!IsListEmpty (&Instance
->RcvdDgramQue
) &&
1541 !NetMapIsEmpty (&Instance
->RxTokens
)) {
1543 Wrap
= NET_LIST_HEAD (&Instance
->RcvdDgramQue
, UDP4_RXDATA_WRAP
, Link
);
1545 if (NET_BUF_SHARED (Wrap
->Packet
)) {
1547 // Duplicate the Packet if it is shared between instances.
1549 Dup
= NetbufDuplicate (Wrap
->Packet
, NULL
, 0);
1554 NetbufFree (Wrap
->Packet
);
1559 NetListRemoveHead (&Instance
->RcvdDgramQue
);
1561 Token
= (EFI_UDP4_COMPLETION_TOKEN
*) NetMapRemoveHead (&Instance
->RxTokens
, NULL
);
1564 // Build the FragmentTable and set the FragmentCount in RxData.
1566 RxData
= &Wrap
->RxData
;
1567 RxData
->FragmentCount
= Wrap
->Packet
->BlockOpNum
;
1571 (NET_FRAGMENT
*) RxData
->FragmentTable
,
1572 &RxData
->FragmentCount
1575 Token
->Status
= EFI_SUCCESS
;
1576 Token
->Packet
.RxData
= &Wrap
->RxData
;
1578 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1579 InsertTailList (&Instance
->DeliveredDgramQue
, &Wrap
->Link
);
1580 gBS
->RestoreTPL (OldTpl
);
1582 gBS
->SignalEvent (Token
->Event
);
1588 This function delivers the datagrams enqueued in the instances.
1590 @param Udp4Service Pointer to the udp service context data.
1597 IN UDP4_SERVICE_DATA
*Udp4Service
1601 UDP4_INSTANCE_DATA
*Instance
;
1603 NET_LIST_FOR_EACH (Entry
, &Udp4Service
->ChildrenList
) {
1605 // Iterate the instances.
1607 Instance
= NET_LIST_USER_STRUCT (Entry
, UDP4_INSTANCE_DATA
, Link
);
1609 if (!Instance
->Configured
) {
1614 // Deliver the datagrams of this instance.
1616 Udp4InstanceDeliverDgram (Instance
);
1622 This function demultiplexes the received udp datagram to the apropriate instances.
1624 @param Udp4Service Pointer to the udp service context data.
1625 @param NetSession Pointer to the EFI_NET_SESSION_DATA abstrated from
1626 the received datagram.
1627 @param Packet Pointer to the buffer containing the received udp
1635 IN UDP4_SERVICE_DATA
*Udp4Service
,
1636 IN EFI_NET_SESSION_DATA
*NetSession
,
1640 EFI_UDP4_HEADER
*Udp4Header
;
1642 EFI_UDP4_RECEIVE_DATA RxData
;
1643 EFI_UDP4_SESSION_DATA
*Udp4Session
;
1647 // Get the datagram header from the packet buffer.
1649 Udp4Header
= (EFI_UDP4_HEADER
*) NetbufGetByte (Packet
, 0, NULL
);
1651 if (Udp4Header
->Checksum
!= 0) {
1653 // check the checksum.
1655 HeadSum
= NetPseudoHeadChecksum (
1662 if (Udp4Checksum (Packet
, HeadSum
) != 0) {
1670 gRT
->GetTime (&RxData
.TimeStamp
, NULL
);
1672 Udp4Session
= &RxData
.UdpSession
;
1673 Udp4Session
->SourcePort
= NTOHS (Udp4Header
->SrcPort
);
1674 Udp4Session
->DestinationPort
= NTOHS (Udp4Header
->DstPort
);
1676 CopyMem (&Udp4Session
->SourceAddress
, &NetSession
->Source
, sizeof (EFI_IPv4_ADDRESS
));
1677 CopyMem (&Udp4Session
->DestinationAddress
, &NetSession
->Dest
, sizeof (EFI_IPv4_ADDRESS
));
1680 // Trim the UDP header.
1682 NetbufTrim (Packet
, UDP4_HEADER_SIZE
, TRUE
);
1684 RxData
.DataLength
= (UINT32
) Packet
->TotalSize
;
1687 // Try to enqueue this datagram into the instances.
1689 Enqueued
= Udp4EnqueueDgram (Udp4Service
, Packet
, &RxData
);
1691 if (Enqueued
== 0) {
1693 // Send the port unreachable ICMP packet before we free this NET_BUF
1695 Udp4SendPortUnreach (Udp4Service
->IpIo
, NetSession
, Udp4Header
);
1699 // Try to free the packet before deliver it.
1701 NetbufFree (Packet
);
1705 // Deliver the datagram.
1707 Udp4DeliverDgram (Udp4Service
);
1713 This function builds and sends out a icmp port unreachable message.
1715 @param IpIo Pointer to the IP_IO instance.
1716 @param NetSession Pointer to the EFI_NET_SESSION_DATA of the packet
1717 causes this icmp error message.
1718 @param Udp4Header Pointer to the udp header of the datagram causes
1719 this icmp error message.
1725 Udp4SendPortUnreach (
1727 IN EFI_NET_SESSION_DATA
*NetSession
,
1733 IP4_ICMP_ERROR_HEAD
*IcmpErrHdr
;
1734 EFI_IP4_HEADER
*IpHdr
;
1736 IP_IO_OVERRIDE Override
;
1737 IP_IO_IP_INFO
*IpSender
;
1739 IpSender
= IpIoFindSender (&IpIo
, NetSession
->Dest
);
1740 if (IpSender
== NULL
) {
1742 // No apropriate sender, since we cannot send out the ICMP message through
1743 // the default zero station address IP instance, abort.
1748 IpHdr
= NetSession
->IpHdr
;
1751 // Calculate the requried length of the icmp error message.
1753 Len
= sizeof (IP4_ICMP_ERROR_HEAD
) + (EFI_IP4_HEADER_LEN (IpHdr
) -
1754 sizeof (IP4_HEAD
)) + ICMP_ERROR_PACKET_LENGTH
;
1757 // Allocate buffer for the icmp error message.
1759 Packet
= NetbufAlloc (Len
);
1760 if (Packet
== NULL
) {
1765 // Allocate space for the IP4_ICMP_ERROR_HEAD.
1767 IcmpErrHdr
= (IP4_ICMP_ERROR_HEAD
*) NetbufAllocSpace (Packet
, Len
, FALSE
);
1770 // Set the required fields for the icmp port unreachable message.
1772 IcmpErrHdr
->Head
.Type
= ICMP_TYPE_UNREACH
;
1773 IcmpErrHdr
->Head
.Code
= ICMP_CODE_UNREACH_PORT
;
1774 IcmpErrHdr
->Head
.Checksum
= 0;
1775 IcmpErrHdr
->Fourth
= 0;
1778 // Copy the IP header of the datagram tragged the error.
1780 CopyMem (&IcmpErrHdr
->IpHead
, IpHdr
, EFI_IP4_HEADER_LEN (IpHdr
));
1783 // Copy the UDP header.
1785 Ptr
= (UINT8
*) &IcmpErrHdr
->IpHead
+ EFI_IP4_HEADER_LEN (IpHdr
);
1786 CopyMem (Ptr
, Udp4Header
, ICMP_ERROR_PACKET_LENGTH
);
1789 // Calculate the checksum.
1791 IcmpErrHdr
->Head
.Checksum
= (UINT16
) ~(NetbufChecksum (Packet
));
1794 // Fill the override data.
1796 Override
.DoNotFragment
= FALSE
;
1797 Override
.TypeOfService
= 0;
1798 Override
.TimeToLive
= 255;
1799 Override
.Protocol
= EFI_IP_PROTO_ICMP
;
1801 CopyMem (&Override
.SourceAddress
, &NetSession
->Dest
, sizeof (EFI_IPv4_ADDRESS
));
1802 ZeroMem (&Override
.GatewayAddress
, sizeof (EFI_IPv4_ADDRESS
));
1805 // Send out this icmp packet.
1807 IpIoSend (IpIo
, Packet
, IpSender
, NULL
, NULL
, NetSession
->Source
, &Override
);
1809 NetbufFree (Packet
);
1814 This function handles the received Icmp Error message and demultiplexes it to the
1817 @param Udp4Service Pointer to the udp service context data.
1818 @param IcmpError The icmp error code.
1819 @param NetSession Pointer to the EFI_NET_SESSION_DATA abstracted
1820 from the received Icmp Error packet.
1821 @param Packet Pointer to the Icmp Error packet.
1828 IN UDP4_SERVICE_DATA
*Udp4Service
,
1829 IN ICMP_ERROR IcmpError
,
1830 IN EFI_NET_SESSION_DATA
*NetSession
,
1834 EFI_UDP4_HEADER
*Udp4Header
;
1835 EFI_UDP4_SESSION_DATA Udp4Session
;
1837 UDP4_INSTANCE_DATA
*Instance
;
1839 Udp4Header
= (EFI_UDP4_HEADER
*) NetbufGetByte (Packet
, 0, NULL
);
1841 CopyMem (&Udp4Session
.SourceAddress
, &NetSession
->Source
, sizeof (EFI_IPv4_ADDRESS
));
1842 CopyMem (&Udp4Session
.DestinationAddress
, &NetSession
->Dest
, sizeof (EFI_IPv4_ADDRESS
));
1844 Udp4Session
.SourcePort
= NTOHS (Udp4Header
->DstPort
);
1845 Udp4Session
.DestinationPort
= NTOHS (Udp4Header
->SrcPort
);
1847 NET_LIST_FOR_EACH (Entry
, &Udp4Service
->ChildrenList
) {
1849 // Iterate all the instances.
1851 Instance
= NET_LIST_USER_STRUCT (Entry
, UDP4_INSTANCE_DATA
, Link
);
1853 if (!Instance
->Configured
||
1854 Instance
->ConfigData
.AcceptPromiscuous
||
1855 Instance
->ConfigData
.AcceptAnyPort
||
1856 EFI_IP4_EQUAL (&Instance
->ConfigData
.StationAddress
, &mZeroIp4Addr
)
1859 // Don't try to deliver the ICMP error to this instance if it is not configured,
1860 // or it's configured to be promiscuous or accept any port or accept all the
1866 if (Udp4MatchDgram (Instance
, &Udp4Session
)) {
1868 // Translate the Icmp Error code according to the udp spec.
1870 Instance
->IcmpError
= IpIoGetIcmpErrStatus (IcmpError
, NULL
, NULL
);
1872 if (IcmpError
> ICMP_ERR_UNREACH_PORT
) {
1873 Instance
->IcmpError
= EFI_ICMP_ERROR
;
1877 // Notify the instance with the received Icmp Error.
1879 Udp4ReportIcmpError (Instance
);
1885 NetbufFree (Packet
);
1890 This function reports the received ICMP error.
1892 @param Instance Pointer to the udp instance context data.
1898 Udp4ReportIcmpError (
1899 IN UDP4_INSTANCE_DATA
*Instance
1902 EFI_UDP4_COMPLETION_TOKEN
*Token
;
1904 if (NetMapIsEmpty (&Instance
->RxTokens
)) {
1906 // There are no receive tokens to deliver the ICMP error.
1911 if (EFI_ERROR (Instance
->IcmpError
)) {
1913 // Try to get a RxToken from the RxTokens map.
1915 Token
= (EFI_UDP4_COMPLETION_TOKEN
*) NetMapRemoveHead (&Instance
->RxTokens
, NULL
);
1917 if (Token
!= NULL
) {
1919 // Report the error through the Token.
1921 Token
->Status
= Instance
->IcmpError
;
1922 gBS
->SignalEvent (Token
->Event
);
1925 // Clear the IcmpError.
1927 Instance
->IcmpError
= EFI_SUCCESS
;
1934 This function is a dummy ext-free function for the NET_BUF created for the output
1937 @param Context Pointer to the context data.
1943 Udp4NetVectorExtFree (
1951 Set the Udp4 variable data.
1953 @param Udp4Service Udp4 service data.
1955 @retval EFI_OUT_OF_RESOURCES There are not enough resources to set the
1957 @retval EFI_SUCCESS Set variable successfully.
1958 @retval other Set variable failed.
1962 Udp4SetVariableData (
1963 IN UDP4_SERVICE_DATA
*Udp4Service
1966 UINT32 NumConfiguredInstance
;
1968 UINTN VariableDataSize
;
1969 EFI_UDP4_VARIABLE_DATA
*Udp4VariableData
;
1970 EFI_UDP4_SERVICE_POINT
*Udp4ServicePoint
;
1971 UDP4_INSTANCE_DATA
*Udp4Instance
;
1972 CHAR16
*NewMacString
;
1975 NumConfiguredInstance
= 0;
1978 // Go through the children list to count the configured children.
1980 NET_LIST_FOR_EACH (Entry
, &Udp4Service
->ChildrenList
) {
1981 Udp4Instance
= NET_LIST_USER_STRUCT_S (
1985 UDP4_INSTANCE_DATA_SIGNATURE
1988 if (Udp4Instance
->Configured
) {
1989 NumConfiguredInstance
++;
1994 // Calculate the size of the Udp4VariableData. As there may be no Udp4 child,
1995 // we should add extra buffer for the service points only if the number of configured
1996 // children is more than 1.
1998 VariableDataSize
= sizeof (EFI_UDP4_VARIABLE_DATA
);
2000 if (NumConfiguredInstance
> 1) {
2001 VariableDataSize
+= sizeof (EFI_UDP4_SERVICE_POINT
) * (NumConfiguredInstance
- 1);
2004 Udp4VariableData
= AllocatePool (VariableDataSize
);
2005 if (Udp4VariableData
== NULL
) {
2006 return EFI_OUT_OF_RESOURCES
;
2009 Udp4VariableData
->DriverHandle
= Udp4Service
->ImageHandle
;
2010 Udp4VariableData
->ServiceCount
= NumConfiguredInstance
;
2012 Udp4ServicePoint
= &Udp4VariableData
->Services
[0];
2015 // Go through the children list to fill the configured children's address pairs.
2017 NET_LIST_FOR_EACH (Entry
, &Udp4Service
->ChildrenList
) {
2018 Udp4Instance
= NET_LIST_USER_STRUCT_S (
2022 UDP4_INSTANCE_DATA_SIGNATURE
2025 if (Udp4Instance
->Configured
) {
2026 Udp4ServicePoint
->InstanceHandle
= Udp4Instance
->ChildHandle
;
2027 Udp4ServicePoint
->LocalAddress
= Udp4Instance
->ConfigData
.StationAddress
;
2028 Udp4ServicePoint
->LocalPort
= Udp4Instance
->ConfigData
.StationPort
;
2029 Udp4ServicePoint
->RemoteAddress
= Udp4Instance
->ConfigData
.RemoteAddress
;
2030 Udp4ServicePoint
->RemotePort
= Udp4Instance
->ConfigData
.RemotePort
;
2037 // Get the mac string.
2039 Status
= NetLibGetMacString (
2040 Udp4Service
->ControllerHandle
,
2041 Udp4Service
->ImageHandle
,
2044 if (EFI_ERROR (Status
)) {
2048 if (Udp4Service
->MacString
!= NULL
) {
2050 // The variable is set already, we're going to update it.
2052 if (StrCmp (Udp4Service
->MacString
, NewMacString
) != 0) {
2054 // The mac address is changed, delete the previous variable first.
2057 Udp4Service
->MacString
,
2058 &gEfiUdp4ServiceBindingProtocolGuid
,
2059 EFI_VARIABLE_BOOTSERVICE_ACCESS
,
2065 gBS
->FreePool (Udp4Service
->MacString
);
2068 Udp4Service
->MacString
= NewMacString
;
2070 Status
= gRT
->SetVariable (
2071 Udp4Service
->MacString
,
2072 &gEfiUdp4ServiceBindingProtocolGuid
,
2073 EFI_VARIABLE_BOOTSERVICE_ACCESS
,
2075 (VOID
*) Udp4VariableData
2080 gBS
->FreePool (Udp4VariableData
);
2087 Clear the variable and free the resource.
2089 @param Udp4Service Udp4 service data.
2095 Udp4ClearVariableData (
2096 IN UDP4_SERVICE_DATA
*Udp4Service
2099 ASSERT (Udp4Service
->MacString
!= NULL
);
2102 Udp4Service
->MacString
,
2103 &gEfiUdp4ServiceBindingProtocolGuid
,
2104 EFI_VARIABLE_BOOTSERVICE_ACCESS
,
2109 gBS
->FreePool (Udp4Service
->MacString
);
2110 Udp4Service
->MacString
= NULL
;