2 The implementation of the Udp4 protocol.
4 Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
5 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 Specify a pointer of EFI_IP4_PROTOCOL for sending.
63 @param[in] NotifyData Pointer to the notify data.
71 IN IP_IO_IP_PROTOCOL Sender
,
76 This function processes the received datagram passed up by the IpIo layer.
78 @param[in] Status The status of this udp datagram.
79 @param[in] IcmpError The IcmpError code, only available when Status is
81 @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA.
82 @param[in] Packet Pointer to the NET_BUF containing the received udp
84 @param[in] Context Pointer to the context data.
92 IN EFI_NET_SESSION_DATA
*NetSession
,
98 This function cancels the token specified by Arg in the Map. This is a callback
99 used by Udp4InstanceCancelToken().
101 @param[in] Map Pointer to the NET_MAP.
102 @param[in] Item Pointer to the NET_MAP_ITEM.
103 @param[in] Arg Pointer to the token to be cancelled, if NULL,
104 the token specified by Item is cancelled.
106 @retval EFI_SUCCESS The token is cancelled if Arg is NULL or the token
107 is not the same as that in the Item if Arg is not
109 @retval EFI_ABORTED Arg is not NULL, and the token specified by Arg is
117 IN NET_MAP_ITEM
*Item
,
118 IN VOID
*Arg OPTIONAL
122 This function matches the received udp datagram with the Instance.
124 @param[in] Instance Pointer to the udp instance context data.
125 @param[in] Udp4Session Pointer to the EFI_UDP4_SESSION_DATA abstracted
126 from the received udp datagram.
128 @retval TRUE The udp datagram matches the receiving requirments of the
130 @retval FALSE Otherwise.
135 IN UDP4_INSTANCE_DATA
*Instance
,
136 IN EFI_UDP4_SESSION_DATA
*Udp4Session
140 This function removes the Wrap specified by Context and release relevant resources.
142 @param[in] Event The Event this notify function registered to.
143 @param[in] Context Pointer to the context data.
148 Udp4RecycleRxDataWrap (
154 This function wraps the Packet and the RxData.
156 @param[in] Instance Pointer to the instance context data.
157 @param[in] Packet Pointer to the buffer containing the received
159 @param[in] RxData Pointer to the EFI_UDP4_RECEIVE_DATA of this
162 @return Pointer to the structure wrapping the RxData and the Packet.
167 IN UDP4_INSTANCE_DATA
*Instance
,
169 IN EFI_UDP4_RECEIVE_DATA
*RxData
173 This function enqueues the received datagram into the instances' receiving queues.
175 @param[in] Udp4Service Pointer to the udp service context data.
176 @param[in] Packet Pointer to the buffer containing the received
178 @param[in] RxData Pointer to the EFI_UDP4_RECEIVE_DATA of this
181 @return The times this datagram is enqueued.
186 IN UDP4_SERVICE_DATA
*Udp4Service
,
188 IN EFI_UDP4_RECEIVE_DATA
*RxData
192 This function delivers the datagrams enqueued in the instances.
194 @param[in] Udp4Service Pointer to the udp service context data.
199 IN UDP4_SERVICE_DATA
*Udp4Service
203 This function demultiplexes the received udp datagram to the apropriate instances.
205 @param[in] Udp4Service Pointer to the udp service context data.
206 @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA abstrated from
207 the received datagram.
208 @param[in] Packet Pointer to the buffer containing the received udp
214 IN UDP4_SERVICE_DATA
*Udp4Service
,
215 IN EFI_NET_SESSION_DATA
*NetSession
,
220 This function handles the received Icmp Error message and demultiplexes it to the
223 @param[in] Udp4Service Pointer to the udp service context data.
224 @param[in] IcmpError The icmp error code.
225 @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA abstracted
226 from the received Icmp Error packet.
227 @param[in] Packet Pointer to the Icmp Error packet.
232 IN UDP4_SERVICE_DATA
*Udp4Service
,
234 IN EFI_NET_SESSION_DATA
*NetSession
,
239 This function builds and sends out a icmp port unreachable message.
241 @param[in] IpIo Pointer to the IP_IO instance.
242 @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA of the packet
243 causes this icmp error message.
244 @param[in] Udp4Header Pointer to the udp header of the datagram causes
245 this icmp error message.
249 Udp4SendPortUnreach (
251 IN EFI_NET_SESSION_DATA
*NetSession
,
257 Create the Udp service context data.
259 @param[in, out] Udp4Service Pointer to the UDP4_SERVICE_DATA.
260 @param[in] ImageHandle The image handle of this udp4 driver.
261 @param[in] ControllerHandle The controller handle this udp4 driver binds on.
263 @retval EFI_SUCCESS The udp4 service context data is created and
265 @retval EFI_OUT_OF_RESOURCES Cannot allocate memory.
266 @retval other Other error occurs.
271 IN OUT UDP4_SERVICE_DATA
*Udp4Service
,
272 IN EFI_HANDLE ImageHandle
,
273 IN EFI_HANDLE ControllerHandle
277 IP_IO_OPEN_DATA OpenData
;
278 EFI_IP4_CONFIG_DATA
*Ip4ConfigData
;
280 ZeroMem (Udp4Service
, sizeof (UDP4_SERVICE_DATA
));
282 Udp4Service
->Signature
= UDP4_SERVICE_DATA_SIGNATURE
;
283 Udp4Service
->ServiceBinding
= mUdp4ServiceBinding
;
284 Udp4Service
->ImageHandle
= ImageHandle
;
285 Udp4Service
->ControllerHandle
= ControllerHandle
;
286 Udp4Service
->ChildrenNumber
= 0;
288 InitializeListHead (&Udp4Service
->ChildrenList
);
291 // Create the IpIo for this service context.
293 Udp4Service
->IpIo
= IpIoCreate (ImageHandle
, ControllerHandle
, IP_VERSION_4
);
294 if (Udp4Service
->IpIo
== NULL
) {
295 return EFI_OUT_OF_RESOURCES
;
299 // Set the OpenData used to open the IpIo.
301 Ip4ConfigData
= &OpenData
.IpConfigData
.Ip4CfgData
;
302 CopyMem (Ip4ConfigData
, &mIp4IoDefaultIpConfigData
, sizeof (EFI_IP4_CONFIG_DATA
));
303 Ip4ConfigData
->AcceptBroadcast
= TRUE
;
304 OpenData
.RcvdContext
= (VOID
*) Udp4Service
;
305 OpenData
.SndContext
= NULL
;
306 OpenData
.PktRcvdNotify
= Udp4DgramRcvd
;
307 OpenData
.PktSentNotify
= Udp4DgramSent
;
310 // Configure and start the IpIo.
312 Status
= IpIoOpen (Udp4Service
->IpIo
, &OpenData
);
313 if (EFI_ERROR (Status
)) {
318 // Create the event for Udp timeout checking.
320 Status
= gBS
->CreateEvent (
321 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
325 &Udp4Service
->TimeoutEvent
327 if (EFI_ERROR (Status
)) {
332 // Start the timeout timer event.
334 Status
= gBS
->SetTimer (
335 Udp4Service
->TimeoutEvent
,
337 UDP4_TIMEOUT_INTERVAL
339 if (EFI_ERROR (Status
)) {
347 if (Udp4Service
->TimeoutEvent
!= NULL
) {
348 gBS
->CloseEvent (Udp4Service
->TimeoutEvent
);
351 IpIoDestroy (Udp4Service
->IpIo
);
358 Clean the Udp service context data.
360 @param[in] Udp4Service Pointer to the UDP4_SERVICE_DATA.
365 IN UDP4_SERVICE_DATA
*Udp4Service
369 // Cancel the TimeoutEvent timer.
371 gBS
->SetTimer (Udp4Service
->TimeoutEvent
, TimerCancel
, 0);
374 // Close the TimeoutEvent timer.
376 gBS
->CloseEvent (Udp4Service
->TimeoutEvent
);
381 IpIoDestroy (Udp4Service
->IpIo
);
386 This function checks and timeouts the I/O datagrams holding by the corresponding
389 @param[in] Event The event this function registered to.
390 @param[in] Context The context data registered during the creation of
401 UDP4_SERVICE_DATA
*Udp4Service
;
403 UDP4_INSTANCE_DATA
*Instance
;
404 LIST_ENTRY
*WrapEntry
;
405 LIST_ENTRY
*NextEntry
;
406 UDP4_RXDATA_WRAP
*Wrap
;
408 Udp4Service
= (UDP4_SERVICE_DATA
*) Context
;
409 NET_CHECK_SIGNATURE (Udp4Service
, UDP4_SERVICE_DATA_SIGNATURE
);
411 NET_LIST_FOR_EACH (Entry
, &Udp4Service
->ChildrenList
) {
413 // Iterate all the instances belonging to this service context.
415 Instance
= NET_LIST_USER_STRUCT (Entry
, UDP4_INSTANCE_DATA
, Link
);
416 NET_CHECK_SIGNATURE (Instance
, UDP4_INSTANCE_DATA_SIGNATURE
);
418 if (!Instance
->Configured
|| (Instance
->ConfigData
.ReceiveTimeout
== 0)) {
420 // Skip this instance if it's not configured or no receive timeout.
425 NET_LIST_FOR_EACH_SAFE (WrapEntry
, NextEntry
, &Instance
->RcvdDgramQue
) {
427 // Iterate all the rxdatas belonging to this udp instance.
429 Wrap
= NET_LIST_USER_STRUCT (WrapEntry
, UDP4_RXDATA_WRAP
, Link
);
432 // TimeoutTick unit is microsecond, MNP_TIMEOUT_CHECK_INTERVAL unit is 100ns.
434 if (Wrap
->TimeoutTick
< (UDP4_TIMEOUT_INTERVAL
/ 10)) {
436 // Remove this RxData if it timeouts.
438 Udp4RecycleRxDataWrap (NULL
, (VOID
*) Wrap
);
440 Wrap
->TimeoutTick
-= (UDP4_TIMEOUT_INTERVAL
/ 10);
448 This function intializes the new created udp instance.
450 @param[in] Udp4Service Pointer to the UDP4_SERVICE_DATA.
451 @param[in, out] Instance Pointer to the un-initialized UDP4_INSTANCE_DATA.
456 IN UDP4_SERVICE_DATA
*Udp4Service
,
457 IN OUT UDP4_INSTANCE_DATA
*Instance
461 // Set the signature.
463 Instance
->Signature
= UDP4_INSTANCE_DATA_SIGNATURE
;
468 InitializeListHead (&Instance
->Link
);
469 InitializeListHead (&Instance
->RcvdDgramQue
);
470 InitializeListHead (&Instance
->DeliveredDgramQue
);
473 // Init the NET_MAPs.
475 NetMapInit (&Instance
->TxTokens
);
476 NetMapInit (&Instance
->RxTokens
);
477 NetMapInit (&Instance
->McastIps
);
480 // Save the pointer to the UDP4_SERVICE_DATA, and initialize other members.
482 Instance
->Udp4Service
= Udp4Service
;
483 CopyMem (&Instance
->Udp4Proto
, &mUdp4Protocol
, sizeof (Instance
->Udp4Proto
));
484 Instance
->IcmpError
= EFI_SUCCESS
;
485 Instance
->Configured
= FALSE
;
486 Instance
->IsNoMapping
= FALSE
;
487 Instance
->Destroyed
= FALSE
;
492 This function cleans the udp instance.
494 @param[in] Instance Pointer to the UDP4_INSTANCE_DATA to clean.
499 IN UDP4_INSTANCE_DATA
*Instance
502 NetMapClean (&Instance
->McastIps
);
503 NetMapClean (&Instance
->RxTokens
);
504 NetMapClean (&Instance
->TxTokens
);
509 This function finds the udp instance by the specified <Address, Port> pair.
511 @param[in] InstanceList Pointer to the head of the list linking the udp
513 @param[in] Address Pointer to the specified IPv4 address.
514 @param[in] Port The udp port number.
516 @retval TRUE The specified <Address, Port> pair is found.
517 @retval FALSE Otherwise.
521 Udp4FindInstanceByPort (
522 IN LIST_ENTRY
*InstanceList
,
523 IN EFI_IPv4_ADDRESS
*Address
,
528 UDP4_INSTANCE_DATA
*Instance
;
529 EFI_UDP4_CONFIG_DATA
*ConfigData
;
531 NET_LIST_FOR_EACH (Entry
, InstanceList
) {
533 // Iterate all the udp instances.
535 Instance
= NET_LIST_USER_STRUCT (Entry
, UDP4_INSTANCE_DATA
, Link
);
536 ConfigData
= &Instance
->ConfigData
;
538 if (!Instance
->Configured
|| ConfigData
->AcceptAnyPort
) {
540 // If the instance is not configured or the configdata of the instance indicates
541 // this instance accepts any port, skip it.
546 if (EFI_IP4_EQUAL (&ConfigData
->StationAddress
, Address
) &&
547 (ConfigData
->StationPort
== Port
)) {
549 // if both the address and the port are the same, return TRUE.
556 // return FALSE when matching fails.
563 This function tries to bind the udp instance according to the configured port
566 @param[in] InstanceList Pointer to the head of the list linking the udp
568 @param[in, out] ConfigData Pointer to the ConfigData of the instance to be
569 bound. ConfigData->StationPort will be assigned
570 with an available port value on success.
572 @retval EFI_SUCCESS The bound operation is completed successfully.
573 @retval EFI_ACCESS_DENIED The <Address, Port> specified by the ConfigData is
574 already used by other instance.
575 @retval EFI_OUT_OF_RESOURCES No available port resources.
580 IN LIST_ENTRY
*InstanceList
,
581 IN OUT EFI_UDP4_CONFIG_DATA
*ConfigData
584 EFI_IPv4_ADDRESS
*StationAddress
;
587 if (ConfigData
->AcceptAnyPort
) {
591 StationAddress
= &ConfigData
->StationAddress
;
593 if (ConfigData
->StationPort
!= 0) {
595 if (!ConfigData
->AllowDuplicatePort
&&
596 Udp4FindInstanceByPort (InstanceList
, StationAddress
, ConfigData
->StationPort
)) {
598 // Do not allow duplicate port and the port is already used by other instance.
600 return EFI_ACCESS_DENIED
;
604 // select a random port for this instance;
607 if (ConfigData
->AllowDuplicatePort
) {
609 // Just pick up the random port if the instance allows duplicate port.
611 ConfigData
->StationPort
= mUdp4RandomPort
;
614 StartPort
= mUdp4RandomPort
;
616 while (Udp4FindInstanceByPort(InstanceList
, StationAddress
, mUdp4RandomPort
)) {
619 if (mUdp4RandomPort
== 0) {
620 mUdp4RandomPort
= UDP4_PORT_KNOWN
;
623 if (mUdp4RandomPort
== StartPort
) {
625 // No available port.
627 return EFI_OUT_OF_RESOURCES
;
631 ConfigData
->StationPort
= mUdp4RandomPort
;
635 if (mUdp4RandomPort
== 0) {
636 mUdp4RandomPort
= UDP4_PORT_KNOWN
;
645 This function is used to check whether the NewConfigData has any un-reconfigurable
646 parameters changed compared to the OldConfigData.
648 @param[in] OldConfigData Pointer to the current ConfigData the udp instance
650 @param[in] NewConfigData Pointer to the new ConfigData.
652 @retval TRUE The instance is reconfigurable.
653 @retval FALSE Otherwise.
657 Udp4IsReconfigurable (
658 IN EFI_UDP4_CONFIG_DATA
*OldConfigData
,
659 IN EFI_UDP4_CONFIG_DATA
*NewConfigData
662 if ((NewConfigData
->AcceptAnyPort
!= OldConfigData
->AcceptAnyPort
) ||
663 (NewConfigData
->AcceptBroadcast
!= OldConfigData
->AcceptBroadcast
) ||
664 (NewConfigData
->AcceptPromiscuous
!= OldConfigData
->AcceptPromiscuous
) ||
665 (NewConfigData
->AllowDuplicatePort
!= OldConfigData
->AllowDuplicatePort
)
668 // The receiving filter parameters cannot be changed.
673 if ((!NewConfigData
->AcceptAnyPort
) &&
674 (NewConfigData
->StationPort
!= OldConfigData
->StationPort
)
677 // The port is not changeable.
682 if (!NewConfigData
->AcceptPromiscuous
) {
684 if (NewConfigData
->UseDefaultAddress
!= OldConfigData
->UseDefaultAddress
) {
686 // The NewConfigData differs to the old one on the UseDefaultAddress.
691 if (!NewConfigData
->UseDefaultAddress
&&
692 (!EFI_IP4_EQUAL (&NewConfigData
->StationAddress
, &OldConfigData
->StationAddress
) ||
693 !EFI_IP4_EQUAL (&NewConfigData
->SubnetMask
, &OldConfigData
->SubnetMask
))
696 // If the instance doesn't use the default address, and the new address or
697 // new subnet mask is different from the old values.
703 if (!EFI_IP4_EQUAL (&NewConfigData
->RemoteAddress
, &OldConfigData
->RemoteAddress
)) {
705 // The remoteaddress is not the same.
710 if (!EFI_IP4_EQUAL (&NewConfigData
->RemoteAddress
, &mZeroIp4Addr
) &&
711 NewConfigData
->RemotePort
!= OldConfigData
->RemotePort
714 // The RemotePort differs if it's designated in the configdata.
720 // All checks pass, return TRUE.
727 This function builds the Ip4 configdata from the Udp4ConfigData.
729 @param[in] Udp4ConfigData Pointer to the EFI_UDP4_CONFIG_DATA.
730 @param[in, out] Ip4ConfigData Pointer to the EFI_IP4_CONFIG_DATA.
734 Udp4BuildIp4ConfigData (
735 IN EFI_UDP4_CONFIG_DATA
*Udp4ConfigData
,
736 IN OUT EFI_IP4_CONFIG_DATA
*Ip4ConfigData
739 CopyMem (Ip4ConfigData
, &mIp4IoDefaultIpConfigData
, sizeof (*Ip4ConfigData
));
741 Ip4ConfigData
->DefaultProtocol
= EFI_IP_PROTO_UDP
;
742 Ip4ConfigData
->AcceptBroadcast
= Udp4ConfigData
->AcceptBroadcast
;
743 Ip4ConfigData
->AcceptPromiscuous
= Udp4ConfigData
->AcceptPromiscuous
;
744 Ip4ConfigData
->UseDefaultAddress
= Udp4ConfigData
->UseDefaultAddress
;
745 CopyMem (&Ip4ConfigData
->StationAddress
, &Udp4ConfigData
->StationAddress
, sizeof (EFI_IPv4_ADDRESS
));
746 CopyMem (&Ip4ConfigData
->SubnetMask
, &Udp4ConfigData
->SubnetMask
, sizeof (EFI_IPv4_ADDRESS
));
749 // use the -1 magic number to disable the receiving process of the ip instance.
751 Ip4ConfigData
->ReceiveTimeout
= (UINT32
) (-1);
756 This function validates the TxToken, it returns the error code according to the spec.
758 @param[in] Instance Pointer to the udp instance context data.
759 @param[in] TxToken Pointer to the token to be checked.
761 @retval EFI_SUCCESS The TxToken is valid.
762 @retval EFI_INVALID_PARAMETER One or more of the following are TRUE: This is
763 NULL. Token is NULL. Token.Event is NULL.
764 Token.Packet.TxData is NULL.
765 Token.Packet.TxData.FragmentCount is zero.
766 Token.Packet.TxData.DataLength is not equal to the
767 sum of fragment lengths. One or more of the
768 Token.Packet.TxData.FragmentTable[].
769 FragmentLength fields is zero. One or more of the
770 Token.Packet.TxData.FragmentTable[].
771 FragmentBuffer fields is NULL.
772 Token.Packet.TxData. GatewayAddress is not a
773 unicast IPv4 address if it is not NULL. One or
774 more IPv4 addresses in Token.Packet.TxData.
775 UdpSessionData are not valid unicast IPv4
776 addresses if the UdpSessionData is not NULL.
777 @retval EFI_BAD_BUFFER_SIZE The data length is greater than the maximum UDP
782 Udp4ValidateTxToken (
783 IN UDP4_INSTANCE_DATA
*Instance
,
784 IN EFI_UDP4_COMPLETION_TOKEN
*TxToken
787 EFI_UDP4_TRANSMIT_DATA
*TxData
;
790 EFI_UDP4_CONFIG_DATA
*ConfigData
;
791 EFI_UDP4_SESSION_DATA
*UdpSessionData
;
792 IP4_ADDR SourceAddress
;
793 IP4_ADDR GatewayAddress
;
795 if (TxToken
->Event
== NULL
) {
796 return EFI_INVALID_PARAMETER
;
799 TxData
= TxToken
->Packet
.TxData
;
801 if ((TxData
== NULL
) || (TxData
->FragmentCount
== 0)) {
802 return EFI_INVALID_PARAMETER
;
806 for (Index
= 0; Index
< TxData
->FragmentCount
; Index
++) {
808 if ((TxData
->FragmentTable
[Index
].FragmentBuffer
== NULL
) ||
809 (TxData
->FragmentTable
[Index
].FragmentLength
== 0)) {
811 // if the FragmentBuffer is NULL or the FragmentLeng is zero.
813 return EFI_INVALID_PARAMETER
;
816 TotalLen
+= TxData
->FragmentTable
[Index
].FragmentLength
;
819 if (TotalLen
!= TxData
->DataLength
) {
821 // The TotalLen calculated by adding all the FragmentLeng doesn't equal to the
824 return EFI_INVALID_PARAMETER
;
827 if (TxData
->GatewayAddress
!= NULL
) {
828 CopyMem (&GatewayAddress
, TxData
->GatewayAddress
, sizeof (IP4_ADDR
));
830 if (!NetIp4IsUnicast (NTOHL (GatewayAddress
), 0)) {
832 // The specified GatewayAddress is not a unicast IPv4 address while it's not 0.
834 return EFI_INVALID_PARAMETER
;
838 ConfigData
= &Instance
->ConfigData
;
839 UdpSessionData
= TxData
->UdpSessionData
;
841 if (UdpSessionData
!= NULL
) {
843 CopyMem (&SourceAddress
, &UdpSessionData
->SourceAddress
, sizeof (IP4_ADDR
));
845 if ((SourceAddress
!= 0) && !NetIp4IsUnicast (HTONL (SourceAddress
), 0)) {
847 // Check whether SourceAddress is a valid IPv4 address in case it's not zero.
848 // The configured station address is used if SourceAddress is zero.
850 return EFI_INVALID_PARAMETER
;
853 if ((UdpSessionData
->DestinationPort
== 0) && (ConfigData
->RemotePort
== 0)) {
855 // Ambiguous, no avalaible DestinationPort for this token.
857 return EFI_INVALID_PARAMETER
;
860 if (EFI_IP4_EQUAL (&UdpSessionData
->DestinationAddress
, &mZeroIp4Addr
)) {
862 // The DestinationAddress specified in the UdpSessionData is 0.
864 return EFI_INVALID_PARAMETER
;
866 } else if (EFI_IP4_EQUAL (&ConfigData
->RemoteAddress
, &mZeroIp4Addr
)) {
868 // the configured RemoteAddress is all zero, and the user doens't override the
869 // destination address.
871 return EFI_INVALID_PARAMETER
;
874 if (TxData
->DataLength
> UDP4_MAX_DATA_SIZE
) {
875 return EFI_BAD_BUFFER_SIZE
;
883 This function checks whether the specified Token duplicates with the one in the Map.
885 @param[in] Map Pointer to the NET_MAP.
886 @param[in] Item Pointer to the NET_MAP_ITEM contain the pointer to
888 @param[in] Context Pointer to the Token to be checked.
890 @retval EFI_SUCCESS The Token specified by Context differs from the
892 @retval EFI_ACCESS_DENIED The Token duplicates with the one in the Item.
899 IN NET_MAP_ITEM
*Item
,
903 EFI_UDP4_COMPLETION_TOKEN
*Token
;
904 EFI_UDP4_COMPLETION_TOKEN
*TokenInItem
;
906 Token
= (EFI_UDP4_COMPLETION_TOKEN
*) Context
;
907 TokenInItem
= (EFI_UDP4_COMPLETION_TOKEN
*) Item
->Key
;
909 if ((Token
== TokenInItem
) || (Token
->Event
== TokenInItem
->Event
)) {
911 // The Token duplicates with the TokenInItem in case either the two pointers are the
912 // same or the Events of these two tokens are the same.
914 return EFI_ACCESS_DENIED
;
922 This function calculates the checksum for the Packet, utilizing the pre-calculated
923 pseudo HeadSum to reduce some overhead.
925 @param[in] Packet Pointer to the NET_BUF contains the udp datagram.
926 @param[in] HeadSum Checksum of the pseudo header execpt the length
929 @retval The 16-bit checksum of this udp datagram.
940 Checksum
= NetbufChecksum (Packet
);
941 Checksum
= NetAddChecksum (Checksum
, HeadSum
);
943 Checksum
= NetAddChecksum (Checksum
, HTONS ((UINT16
) Packet
->TotalSize
));
945 return (UINT16
) ~Checksum
;
950 This function removes the specified Token from the TokenMap.
952 @param[in, out] TokenMap Pointer to the NET_MAP containing the tokens.
953 @param[in] Token Pointer to the Token to be removed.
955 @retval EFI_SUCCESS The specified Token is removed from the TokenMap.
956 @retval EFI_NOT_FOUND The specified Token is not found in the TokenMap.
961 IN OUT NET_MAP
*TokenMap
,
962 IN EFI_UDP4_COMPLETION_TOKEN
*Token
968 // Find the Token first.
970 Item
= NetMapFindKey (TokenMap
, (VOID
*) Token
);
974 // Remove the token if it's found in the map.
976 NetMapRemoveItem (TokenMap
, Item
, NULL
);
981 return EFI_NOT_FOUND
;
986 This function is the packet transmitting notify function registered to the IpIo
987 interface. It's called to signal the udp TxToken when IpIo layer completes the
988 transmitting of the udp datagram.
990 @param[in] Status The completion status of the output udp datagram.
991 @param[in] Context Pointer to the context data.
992 @param[in] Sender Specify a pointer of EFI_IP4_PROTOCOL for sending.
993 @param[in] NotifyData Pointer to the notify data.
999 IN EFI_STATUS Status
,
1001 IN IP_IO_IP_PROTOCOL Sender
,
1005 UDP4_INSTANCE_DATA
*Instance
;
1006 EFI_UDP4_COMPLETION_TOKEN
*Token
;
1008 Instance
= (UDP4_INSTANCE_DATA
*) Context
;
1009 Token
= (EFI_UDP4_COMPLETION_TOKEN
*) NotifyData
;
1011 if (Udp4RemoveToken (&Instance
->TxTokens
, Token
) == EFI_SUCCESS
) {
1013 // The token may be cancelled. Only signal it if the remove operation succeeds.
1015 Token
->Status
= Status
;
1016 gBS
->SignalEvent (Token
->Event
);
1023 This function processes the received datagram passed up by the IpIo layer.
1025 @param[in] Status The status of this udp datagram.
1026 @param[in] IcmpError The IcmpError code, only available when Status is
1028 @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA.
1029 @param[in] Packet Pointer to the NET_BUF containing the received udp
1031 @param[in] Context Pointer to the context data.
1037 IN EFI_STATUS Status
,
1039 IN EFI_NET_SESSION_DATA
*NetSession
,
1044 NET_CHECK_SIGNATURE (Packet
, NET_BUF_SIGNATURE
);
1047 // IpIo only passes received packets with Status EFI_SUCCESS or EFI_ICMP_ERROR.
1049 if (Status
== EFI_SUCCESS
) {
1051 // Demultiplex the received datagram.
1053 Udp4Demultiplex ((UDP4_SERVICE_DATA
*) Context
, NetSession
, Packet
);
1056 // Handle the ICMP_ERROR packet.
1058 Udp4IcmpHandler ((UDP4_SERVICE_DATA
*) Context
, IcmpError
, NetSession
, Packet
);
1062 // Dispatch the DPC queued by the NotifyFunction of the rx token's events
1063 // which are signaled with received data.
1070 This function removes the multicast group specified by Arg from the Map.
1072 @param[in, out] Map Pointer to the NET_MAP.
1073 @param[in] Item Pointer to the NET_MAP_ITEM.
1074 @param[in] Arg Pointer to the Arg, it's the pointer to a
1075 multicast IPv4 Address.
1077 @retval EFI_SUCCESS The multicast address is removed.
1078 @retval EFI_ABORTED The specified multicast address is removed and the
1085 IN OUT NET_MAP
*Map
,
1086 IN NET_MAP_ITEM
*Item
,
1087 IN VOID
*Arg OPTIONAL
1090 EFI_IPv4_ADDRESS
*McastIp
;
1094 if ((McastIp
!= NULL
) && (!EFI_IP4_EQUAL (McastIp
, &(Item
->Key
)))) {
1096 // McastIp is not NULL and the multicast address contained in the Item
1097 // is not the same as McastIp.
1103 // Remove this Item.
1105 NetMapRemoveItem (Map
, Item
, NULL
);
1107 if (McastIp
!= NULL
) {
1109 // Return EFI_ABORTED in case McastIp is not NULL to terminate the iteration.
1119 This function cancels the token specified by Arg in the Map. This is a callback
1120 used by Udp4InstanceCancelToken().
1122 @param[in] Map Pointer to the NET_MAP.
1123 @param[in] Item Pointer to the NET_MAP_ITEM.
1124 @param[in] Arg Pointer to the token to be cancelled, if NULL,
1125 the token specified by Item is cancelled.
1127 @retval EFI_SUCCESS The token is cancelled if Arg is NULL or the token
1128 is not the same as that in the Item if Arg is not
1130 @retval EFI_ABORTED Arg is not NULL, and the token specified by Arg is
1138 IN NET_MAP_ITEM
*Item
,
1139 IN VOID
*Arg OPTIONAL
1142 EFI_UDP4_COMPLETION_TOKEN
*TokenToCancel
;
1146 if ((Arg
!= NULL
) && (Item
->Key
!= Arg
)) {
1150 if (Item
->Value
!= NULL
) {
1152 // If the token is a transmit token, the corresponding Packet is recorded in
1153 // Item->Value, invoke IpIo to cancel this packet first. The IpIoCancelTxToken
1154 // will invoke Udp4DgramSent, the token will be signaled and this Item will
1155 // be removed from the Map there.
1157 Packet
= (NET_BUF
*) (Item
->Value
);
1158 IpIo
= (IP_IO
*) (*((UINTN
*) &Packet
->ProtoData
[0]));
1160 IpIoCancelTxToken (IpIo
, Packet
);
1163 // The token is a receive token. Abort it and remove it from the Map.
1165 TokenToCancel
= (EFI_UDP4_COMPLETION_TOKEN
*) Item
->Key
;
1166 NetMapRemoveItem (Map
, Item
, NULL
);
1168 TokenToCancel
->Status
= EFI_ABORTED
;
1169 gBS
->SignalEvent (TokenToCancel
->Event
);
1181 This function removes all the Wrap datas in the RcvdDgramQue.
1183 @param[in] Instance Pointer to the udp instance context data.
1187 Udp4FlushRcvdDgram (
1188 IN UDP4_INSTANCE_DATA
*Instance
1191 UDP4_RXDATA_WRAP
*Wrap
;
1193 while (!IsListEmpty (&Instance
->RcvdDgramQue
)) {
1195 // Iterate all the Wraps in the RcvdDgramQue.
1197 Wrap
= NET_LIST_HEAD (&Instance
->RcvdDgramQue
, UDP4_RXDATA_WRAP
, Link
);
1200 // The Wrap will be removed from the RcvdDgramQue by this function call.
1202 Udp4RecycleRxDataWrap (NULL
, (VOID
*) Wrap
);
1209 Cancel Udp4 tokens from the Udp4 instance.
1211 @param[in] Instance Pointer to the udp instance context data.
1212 @param[in] Token Pointer to the token to be canceled, if NULL, all
1213 tokens in this instance will be cancelled.
1215 @retval EFI_SUCCESS The Token is cancelled.
1216 @retval EFI_NOT_FOUND The Token is not found.
1220 Udp4InstanceCancelToken (
1221 IN UDP4_INSTANCE_DATA
*Instance
,
1222 IN EFI_UDP4_COMPLETION_TOKEN
*Token OPTIONAL
1228 // Cancel this token from the TxTokens map.
1230 Status
= NetMapIterate (&Instance
->TxTokens
, Udp4CancelTokens
, Token
);
1232 if ((Token
!= NULL
) && (Status
== EFI_ABORTED
)) {
1234 // If Token isn't NULL and Status is EFI_ABORTED, the token is cancelled from
1235 // the TxTokens, just return success.
1241 // Try to cancel this token from the RxTokens map in condition either the Token
1242 // is NULL or the specified Token is not in TxTokens.
1244 Status
= NetMapIterate (&Instance
->RxTokens
, Udp4CancelTokens
, Token
);
1246 if ((Token
!= NULL
) && (Status
== EFI_SUCCESS
)) {
1248 // If Token isn't NULL and Status is EFI_SUCCESS, the token is neither in the
1249 // TxTokens nor the RxTokens, or say, it's not found.
1251 return EFI_NOT_FOUND
;
1254 ASSERT ((Token
!= NULL
) || ((0 == NetMapGetCount (&Instance
->TxTokens
))
1255 && (0 == NetMapGetCount (&Instance
->RxTokens
))));
1262 This function matches the received udp datagram with the Instance.
1264 @param[in] Instance Pointer to the udp instance context data.
1265 @param[in] Udp4Session Pointer to the EFI_UDP4_SESSION_DATA abstracted
1266 from the received udp datagram.
1268 @retval TRUE The udp datagram matches the receiving requirments of the
1270 @retval FALSE Otherwise.
1275 IN UDP4_INSTANCE_DATA
*Instance
,
1276 IN EFI_UDP4_SESSION_DATA
*Udp4Session
1279 EFI_UDP4_CONFIG_DATA
*ConfigData
;
1280 IP4_ADDR Destination
;
1282 ConfigData
= &Instance
->ConfigData
;
1284 if (ConfigData
->AcceptPromiscuous
) {
1286 // Always matches if this instance is in the promiscuous state.
1291 if ((!ConfigData
->AcceptAnyPort
&& (Udp4Session
->DestinationPort
!= ConfigData
->StationPort
)) ||
1292 ((ConfigData
->RemotePort
!= 0) && (Udp4Session
->SourcePort
!= ConfigData
->RemotePort
))
1295 // The local port or the remote port doesn't match.
1300 if (!EFI_IP4_EQUAL (&ConfigData
->RemoteAddress
, &mZeroIp4Addr
) &&
1301 !EFI_IP4_EQUAL (&ConfigData
->RemoteAddress
, &Udp4Session
->SourceAddress
)
1304 // This datagram doesn't come from the instance's specified sender.
1309 if (EFI_IP4_EQUAL (&ConfigData
->StationAddress
, &mZeroIp4Addr
) ||
1310 EFI_IP4_EQUAL (&Udp4Session
->DestinationAddress
, &ConfigData
->StationAddress
)
1313 // The instance is configured to receive datagrams destined to any station IP or
1314 // the destination address of this datagram matches the configured station IP.
1319 CopyMem (&Destination
, &Udp4Session
->DestinationAddress
, sizeof (IP4_ADDR
));
1321 if (IP4_IS_LOCAL_BROADCAST (Destination
) && ConfigData
->AcceptBroadcast
) {
1323 // The instance is configured to receive broadcast and this is a broadcast packet.
1328 if (IP4_IS_MULTICAST (NTOHL (Destination
)) &&
1329 NetMapFindKey (&Instance
->McastIps
, (VOID
*) (UINTN
) Destination
) != NULL
1332 // It's a multicast packet and the multicast address is accepted by this instance.
1342 This function removes the Wrap specified by Context and release relevant resources.
1344 @param[in] Event The Event this notify function registered to.
1345 @param[in] Context Pointer to the context data.
1350 Udp4RecycleRxDataWrap (
1355 UDP4_RXDATA_WRAP
*Wrap
;
1357 Wrap
= (UDP4_RXDATA_WRAP
*) Context
;
1360 // Remove the Wrap from the list it belongs to.
1362 RemoveEntryList (&Wrap
->Link
);
1365 // Free the Packet associated with this Wrap.
1367 NetbufFree (Wrap
->Packet
);
1372 gBS
->CloseEvent (Wrap
->RxData
.RecycleSignal
);
1379 This function wraps the Packet and the RxData.
1381 @param[in] Instance Pointer to the instance context data.
1382 @param[in] Packet Pointer to the buffer containing the received
1384 @param[in] RxData Pointer to the EFI_UDP4_RECEIVE_DATA of this
1387 @return Pointer to the structure wrapping the RxData and the Packet.
1392 IN UDP4_INSTANCE_DATA
*Instance
,
1394 IN EFI_UDP4_RECEIVE_DATA
*RxData
1398 UDP4_RXDATA_WRAP
*Wrap
;
1401 // Allocate buffer for the Wrap.
1403 Wrap
= AllocatePool (sizeof (UDP4_RXDATA_WRAP
) +
1404 (Packet
->BlockOpNum
- 1) * sizeof (EFI_UDP4_FRAGMENT_DATA
));
1409 InitializeListHead (&Wrap
->Link
);
1411 CopyMem (&Wrap
->RxData
, RxData
, sizeof (Wrap
->RxData
));
1414 // Create the Recycle event.
1416 Status
= gBS
->CreateEvent (
1419 Udp4RecycleRxDataWrap
,
1421 &Wrap
->RxData
.RecycleSignal
1423 if (EFI_ERROR (Status
)) {
1428 Wrap
->Packet
= Packet
;
1429 Wrap
->TimeoutTick
= Instance
->ConfigData
.ReceiveTimeout
;
1436 This function enqueues the received datagram into the instances' receiving queues.
1438 @param[in] Udp4Service Pointer to the udp service context data.
1439 @param[in] Packet Pointer to the buffer containing the received
1441 @param[in] RxData Pointer to the EFI_UDP4_RECEIVE_DATA of this
1444 @return The times this datagram is enqueued.
1449 IN UDP4_SERVICE_DATA
*Udp4Service
,
1451 IN EFI_UDP4_RECEIVE_DATA
*RxData
1455 UDP4_INSTANCE_DATA
*Instance
;
1456 UDP4_RXDATA_WRAP
*Wrap
;
1461 NET_LIST_FOR_EACH (Entry
, &Udp4Service
->ChildrenList
) {
1463 // Iterate the instances.
1465 Instance
= NET_LIST_USER_STRUCT (Entry
, UDP4_INSTANCE_DATA
, Link
);
1467 if (!Instance
->Configured
) {
1471 if (Udp4MatchDgram (Instance
, &RxData
->UdpSession
)) {
1473 // Wrap the RxData and put this Wrap into the instances RcvdDgramQue.
1475 Wrap
= Udp4WrapRxData (Instance
, Packet
, RxData
);
1480 NET_GET_REF (Packet
);
1482 InsertTailList (&Instance
->RcvdDgramQue
, &Wrap
->Link
);
1493 This function delivers the received datagrams for the specified instance.
1495 @param[in] Instance Pointer to the instance context data.
1499 Udp4InstanceDeliverDgram (
1500 IN UDP4_INSTANCE_DATA
*Instance
1503 UDP4_RXDATA_WRAP
*Wrap
;
1504 EFI_UDP4_COMPLETION_TOKEN
*Token
;
1506 EFI_UDP4_RECEIVE_DATA
*RxData
;
1509 if (!IsListEmpty (&Instance
->RcvdDgramQue
) &&
1510 !NetMapIsEmpty (&Instance
->RxTokens
)) {
1512 Wrap
= NET_LIST_HEAD (&Instance
->RcvdDgramQue
, UDP4_RXDATA_WRAP
, Link
);
1514 if (NET_BUF_SHARED (Wrap
->Packet
)) {
1516 // Duplicate the Packet if it is shared between instances.
1518 Dup
= NetbufDuplicate (Wrap
->Packet
, NULL
, 0);
1523 NetbufFree (Wrap
->Packet
);
1528 NetListRemoveHead (&Instance
->RcvdDgramQue
);
1530 Token
= (EFI_UDP4_COMPLETION_TOKEN
*) NetMapRemoveHead (&Instance
->RxTokens
, NULL
);
1533 // Build the FragmentTable and set the FragmentCount in RxData.
1535 RxData
= &Wrap
->RxData
;
1536 RxData
->FragmentCount
= Wrap
->Packet
->BlockOpNum
;
1540 (NET_FRAGMENT
*) RxData
->FragmentTable
,
1541 &RxData
->FragmentCount
1544 Token
->Status
= EFI_SUCCESS
;
1545 Token
->Packet
.RxData
= &Wrap
->RxData
;
1547 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1548 InsertTailList (&Instance
->DeliveredDgramQue
, &Wrap
->Link
);
1549 gBS
->RestoreTPL (OldTpl
);
1551 gBS
->SignalEvent (Token
->Event
);
1557 This function delivers the datagrams enqueued in the instances.
1559 @param[in] Udp4Service Pointer to the udp service context data.
1564 IN UDP4_SERVICE_DATA
*Udp4Service
1568 UDP4_INSTANCE_DATA
*Instance
;
1570 NET_LIST_FOR_EACH (Entry
, &Udp4Service
->ChildrenList
) {
1572 // Iterate the instances.
1574 Instance
= NET_LIST_USER_STRUCT (Entry
, UDP4_INSTANCE_DATA
, Link
);
1576 if (!Instance
->Configured
) {
1581 // Deliver the datagrams of this instance.
1583 Udp4InstanceDeliverDgram (Instance
);
1589 This function demultiplexes the received udp datagram to the apropriate instances.
1591 @param[in] Udp4Service Pointer to the udp service context data.
1592 @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA abstrated from
1593 the received datagram.
1594 @param[in] Packet Pointer to the buffer containing the received udp
1600 IN UDP4_SERVICE_DATA
*Udp4Service
,
1601 IN EFI_NET_SESSION_DATA
*NetSession
,
1605 EFI_UDP_HEADER
*Udp4Header
;
1607 EFI_UDP4_RECEIVE_DATA RxData
;
1608 EFI_UDP4_SESSION_DATA
*Udp4Session
;
1612 // Get the datagram header from the packet buffer.
1614 Udp4Header
= (EFI_UDP_HEADER
*) NetbufGetByte (Packet
, 0, NULL
);
1616 if (Udp4Header
->Checksum
!= 0) {
1618 // check the checksum.
1620 HeadSum
= NetPseudoHeadChecksum (
1621 NetSession
->Source
.Addr
[0],
1622 NetSession
->Dest
.Addr
[0],
1627 if (Udp4Checksum (Packet
, HeadSum
) != 0) {
1635 gRT
->GetTime (&RxData
.TimeStamp
, NULL
);
1637 Udp4Session
= &RxData
.UdpSession
;
1638 Udp4Session
->SourcePort
= NTOHS (Udp4Header
->SrcPort
);
1639 Udp4Session
->DestinationPort
= NTOHS (Udp4Header
->DstPort
);
1641 CopyMem (&Udp4Session
->SourceAddress
, &NetSession
->Source
, sizeof (EFI_IPv4_ADDRESS
));
1642 CopyMem (&Udp4Session
->DestinationAddress
, &NetSession
->Dest
, sizeof (EFI_IPv4_ADDRESS
));
1645 // Trim the UDP header.
1647 NetbufTrim (Packet
, UDP4_HEADER_SIZE
, TRUE
);
1649 RxData
.DataLength
= (UINT32
) Packet
->TotalSize
;
1652 // Try to enqueue this datagram into the instances.
1654 Enqueued
= Udp4EnqueueDgram (Udp4Service
, Packet
, &RxData
);
1656 if (Enqueued
== 0) {
1658 // Send the port unreachable ICMP packet before we free this NET_BUF
1660 Udp4SendPortUnreach (Udp4Service
->IpIo
, NetSession
, Udp4Header
);
1664 // Try to free the packet before deliver it.
1666 NetbufFree (Packet
);
1670 // Deliver the datagram.
1672 Udp4DeliverDgram (Udp4Service
);
1678 This function builds and sends out a icmp port unreachable message.
1680 @param[in] IpIo Pointer to the IP_IO instance.
1681 @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA of the packet
1682 causes this icmp error message.
1683 @param[in] Udp4Header Pointer to the udp header of the datagram causes
1684 this icmp error message.
1688 Udp4SendPortUnreach (
1690 IN EFI_NET_SESSION_DATA
*NetSession
,
1696 IP4_ICMP_ERROR_HEAD
*IcmpErrHdr
;
1697 EFI_IP4_HEADER
*IpHdr
;
1699 IP_IO_OVERRIDE Override
;
1700 IP_IO_IP_INFO
*IpSender
;
1702 IpSender
= IpIoFindSender (&IpIo
, NetSession
->IpVersion
, &NetSession
->Dest
);
1703 if (IpSender
== NULL
) {
1705 // No apropriate sender, since we cannot send out the ICMP message through
1706 // the default zero station address IP instance, abort.
1711 IpHdr
= NetSession
->IpHdr
.Ip4Hdr
;
1714 // Calculate the requried length of the icmp error message.
1716 Len
= sizeof (IP4_ICMP_ERROR_HEAD
) + (EFI_IP4_HEADER_LEN (IpHdr
) -
1717 sizeof (IP4_HEAD
)) + ICMP_ERROR_PACKET_LENGTH
;
1720 // Allocate buffer for the icmp error message.
1722 Packet
= NetbufAlloc (Len
);
1723 if (Packet
== NULL
) {
1728 // Allocate space for the IP4_ICMP_ERROR_HEAD.
1730 IcmpErrHdr
= (IP4_ICMP_ERROR_HEAD
*) NetbufAllocSpace (Packet
, Len
, FALSE
);
1731 ASSERT (IcmpErrHdr
!= NULL
);
1734 // Set the required fields for the icmp port unreachable message.
1736 IcmpErrHdr
->Head
.Type
= ICMP_TYPE_UNREACH
;
1737 IcmpErrHdr
->Head
.Code
= ICMP_CODE_UNREACH_PORT
;
1738 IcmpErrHdr
->Head
.Checksum
= 0;
1739 IcmpErrHdr
->Fourth
= 0;
1742 // Copy the IP header of the datagram tragged the error.
1744 CopyMem (&IcmpErrHdr
->IpHead
, IpHdr
, EFI_IP4_HEADER_LEN (IpHdr
));
1747 // Copy the UDP header.
1749 Ptr
= (UINT8
*) &IcmpErrHdr
->IpHead
+ EFI_IP4_HEADER_LEN (IpHdr
);
1750 CopyMem (Ptr
, Udp4Header
, ICMP_ERROR_PACKET_LENGTH
);
1753 // Calculate the checksum.
1755 IcmpErrHdr
->Head
.Checksum
= (UINT16
) ~(NetbufChecksum (Packet
));
1758 // Fill the override data.
1760 Override
.Ip4OverrideData
.DoNotFragment
= FALSE
;
1761 Override
.Ip4OverrideData
.TypeOfService
= 0;
1762 Override
.Ip4OverrideData
.TimeToLive
= 255;
1763 Override
.Ip4OverrideData
.Protocol
= EFI_IP_PROTO_ICMP
;
1765 CopyMem (&Override
.Ip4OverrideData
.SourceAddress
, &NetSession
->Dest
, sizeof (EFI_IPv4_ADDRESS
));
1766 ZeroMem (&Override
.Ip4OverrideData
.GatewayAddress
, sizeof (EFI_IPv4_ADDRESS
));
1769 // Send out this icmp packet.
1771 IpIoSend (IpIo
, Packet
, IpSender
, NULL
, NULL
, &NetSession
->Source
, &Override
);
1773 NetbufFree (Packet
);
1778 This function handles the received Icmp Error message and demultiplexes it to the
1781 @param[in] Udp4Service Pointer to the udp service context data.
1782 @param[in] IcmpError The icmp error code.
1783 @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA abstracted
1784 from the received Icmp Error packet.
1785 @param[in] Packet Pointer to the Icmp Error packet.
1790 IN UDP4_SERVICE_DATA
*Udp4Service
,
1792 IN EFI_NET_SESSION_DATA
*NetSession
,
1796 EFI_UDP_HEADER
*Udp4Header
;
1797 EFI_UDP4_SESSION_DATA Udp4Session
;
1799 UDP4_INSTANCE_DATA
*Instance
;
1801 Udp4Header
= (EFI_UDP_HEADER
*) NetbufGetByte (Packet
, 0, NULL
);
1803 CopyMem (&Udp4Session
.SourceAddress
, &NetSession
->Source
, sizeof (EFI_IPv4_ADDRESS
));
1804 CopyMem (&Udp4Session
.DestinationAddress
, &NetSession
->Dest
, sizeof (EFI_IPv4_ADDRESS
));
1806 Udp4Session
.SourcePort
= NTOHS (Udp4Header
->DstPort
);
1807 Udp4Session
.DestinationPort
= NTOHS (Udp4Header
->SrcPort
);
1809 NET_LIST_FOR_EACH (Entry
, &Udp4Service
->ChildrenList
) {
1811 // Iterate all the instances.
1813 Instance
= NET_LIST_USER_STRUCT (Entry
, UDP4_INSTANCE_DATA
, Link
);
1815 if (!Instance
->Configured
) {
1819 if (Udp4MatchDgram (Instance
, &Udp4Session
)) {
1821 // Translate the Icmp Error code according to the udp spec.
1823 Instance
->IcmpError
= IpIoGetIcmpErrStatus (IcmpError
, IP_VERSION_4
, NULL
, NULL
);
1825 if (IcmpError
> ICMP_ERR_UNREACH_PORT
) {
1826 Instance
->IcmpError
= EFI_ICMP_ERROR
;
1830 // Notify the instance with the received Icmp Error.
1832 Udp4ReportIcmpError (Instance
);
1838 NetbufFree (Packet
);
1843 This function reports the received ICMP error.
1845 @param[in] Instance Pointer to the udp instance context data.
1849 Udp4ReportIcmpError (
1850 IN UDP4_INSTANCE_DATA
*Instance
1853 EFI_UDP4_COMPLETION_TOKEN
*Token
;
1855 if (NetMapIsEmpty (&Instance
->RxTokens
)) {
1857 // There are no receive tokens to deliver the ICMP error.
1862 if (EFI_ERROR (Instance
->IcmpError
)) {
1864 // Try to get a RxToken from the RxTokens map.
1866 Token
= (EFI_UDP4_COMPLETION_TOKEN
*) NetMapRemoveHead (&Instance
->RxTokens
, NULL
);
1868 if (Token
!= NULL
) {
1870 // Report the error through the Token.
1872 Token
->Status
= Instance
->IcmpError
;
1873 gBS
->SignalEvent (Token
->Event
);
1876 // Clear the IcmpError.
1878 Instance
->IcmpError
= EFI_SUCCESS
;
1885 This function is a dummy ext-free function for the NET_BUF created for the output
1888 @param[in] Context Pointer to the context data.
1893 Udp4NetVectorExtFree (
1901 Set the Udp4 variable data.
1903 @param[in] 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 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 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 FreePool (Udp4Service
->MacString
);
2058 Udp4Service
->MacString
= NULL
;