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
;
36 Udp4FindInstanceByPort (
37 IN LIST_ENTRY
*InstanceList
,
38 IN EFI_IPv4_ADDRESS
*Address
,
53 IN ICMP_ERROR IcmpError
,
54 IN EFI_NET_SESSION_DATA
*NetSession
,
62 IN NET_MAP_ITEM
*Item
,
68 IN UDP4_INSTANCE_DATA
*Instance
,
69 IN EFI_UDP4_SESSION_DATA
*Udp4Session
74 Udp4RecycleRxDataWrap (
81 IN UDP4_INSTANCE_DATA
*Instance
,
83 IN EFI_UDP4_RECEIVE_DATA
*RxData
88 IN UDP4_SERVICE_DATA
*Udp4Service
,
90 IN EFI_UDP4_RECEIVE_DATA
*RxData
95 IN UDP4_SERVICE_DATA
*Udp4Service
100 IN UDP4_SERVICE_DATA
*Udp4Service
,
101 IN EFI_NET_SESSION_DATA
*NetSession
,
107 IN UDP4_SERVICE_DATA
*Udp4Service
,
108 IN ICMP_ERROR IcmpError
,
109 IN EFI_NET_SESSION_DATA
*NetSession
,
114 Udp4SendPortUnreach (
116 IN EFI_NET_SESSION_DATA
*NetSession
,
122 Create the Udp service context data.
124 @param Udp4Service Pointer to the UDP4_SERVICE_DATA.
125 @param ImageHandle The image handle of this udp4 driver.
126 @param ControllerHandle The controller handle this udp4 driver binds on.
128 @retval EFI_SUCCESS The udp4 service context data is created and
130 @retval EFI_OUT_OF_RESOURCES Cannot allocate memory.
131 @retval other Other error occurs.
136 IN OUT UDP4_SERVICE_DATA
*Udp4Service
,
137 IN EFI_HANDLE ImageHandle
,
138 IN EFI_HANDLE ControllerHandle
142 IP_IO_OPEN_DATA OpenData
;
144 ZeroMem (Udp4Service
, sizeof (UDP4_SERVICE_DATA
));
146 Udp4Service
->Signature
= UDP4_SERVICE_DATA_SIGNATURE
;
147 Udp4Service
->ServiceBinding
= mUdp4ServiceBinding
;
148 Udp4Service
->ImageHandle
= ImageHandle
;
149 Udp4Service
->ControllerHandle
= ControllerHandle
;
150 Udp4Service
->ChildrenNumber
= 0;
152 InitializeListHead (&Udp4Service
->ChildrenList
);
155 // Create the IpIo for this service context.
157 Udp4Service
->IpIo
= IpIoCreate (ImageHandle
, ControllerHandle
);
158 if (Udp4Service
->IpIo
== NULL
) {
159 return EFI_OUT_OF_RESOURCES
;
163 // Set the OpenData used to open the IpIo.
165 CopyMem (&OpenData
.IpConfigData
, &mIpIoDefaultIpConfigData
, sizeof (OpenData
.IpConfigData
));
166 OpenData
.IpConfigData
.AcceptBroadcast
= TRUE
;
167 OpenData
.RcvdContext
= (VOID
*) Udp4Service
;
168 OpenData
.SndContext
= NULL
;
169 OpenData
.PktRcvdNotify
= Udp4DgramRcvd
;
170 OpenData
.PktSentNotify
= Udp4DgramSent
;
173 // Configure and start the IpIo.
175 Status
= IpIoOpen (Udp4Service
->IpIo
, &OpenData
);
176 if (EFI_ERROR (Status
)) {
181 // Create the event for Udp timeout checking.
183 Status
= gBS
->CreateEvent (
184 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
188 &Udp4Service
->TimeoutEvent
190 if (EFI_ERROR (Status
)) {
195 // Start the timeout timer event.
197 Status
= gBS
->SetTimer (
198 Udp4Service
->TimeoutEvent
,
200 UDP4_TIMEOUT_INTERVAL
202 if (EFI_ERROR (Status
)) {
210 if (Udp4Service
->TimeoutEvent
!= NULL
) {
211 gBS
->CloseEvent (Udp4Service
->TimeoutEvent
);
214 IpIoDestroy (Udp4Service
->IpIo
);
221 Clean the Udp service context data.
223 @param Udp4Service Pointer to the UDP4_SERVICE_DATA.
230 IN UDP4_SERVICE_DATA
*Udp4Service
234 // Cancel the TimeoutEvent timer.
236 gBS
->SetTimer (Udp4Service
->TimeoutEvent
, TimerCancel
, 0);
239 // Close the TimeoutEvent timer.
241 gBS
->CloseEvent (Udp4Service
->TimeoutEvent
);
246 IpIoDestroy (Udp4Service
->IpIo
);
251 This function checks and timeouts the I/O datagrams holding by the corresponding
254 @param Event The event this function registered to.
255 @param Context The context data registered during the creation of
268 UDP4_SERVICE_DATA
*Udp4Service
;
270 UDP4_INSTANCE_DATA
*Instance
;
271 LIST_ENTRY
*WrapEntry
;
272 LIST_ENTRY
*NextEntry
;
273 UDP4_RXDATA_WRAP
*Wrap
;
275 Udp4Service
= (UDP4_SERVICE_DATA
*) Context
;
276 NET_CHECK_SIGNATURE (Udp4Service
, UDP4_SERVICE_DATA_SIGNATURE
);
278 NET_LIST_FOR_EACH (Entry
, &Udp4Service
->ChildrenList
) {
280 // Iterate all the instances belonging to this service context.
282 Instance
= NET_LIST_USER_STRUCT (Entry
, UDP4_INSTANCE_DATA
, Link
);
283 NET_CHECK_SIGNATURE (Instance
, UDP4_INSTANCE_DATA_SIGNATURE
);
285 if (!Instance
->Configured
|| (Instance
->ConfigData
.ReceiveTimeout
== 0)) {
287 // Skip this instance if it's not configured or no receive timeout.
292 NET_LIST_FOR_EACH_SAFE (WrapEntry
, NextEntry
, &Instance
->RcvdDgramQue
) {
294 // Iterate all the rxdatas belonging to this udp instance.
296 Wrap
= NET_LIST_USER_STRUCT (WrapEntry
, UDP4_RXDATA_WRAP
, Link
);
298 if (Wrap
->TimeoutTick
<= UDP4_TIMEOUT_INTERVAL
/ 1000) {
300 // Remove this RxData if it timeouts.
302 Udp4RecycleRxDataWrap (NULL
, (VOID
*) Wrap
);
304 Wrap
->TimeoutTick
-= UDP4_TIMEOUT_INTERVAL
/ 1000;
312 This function intializes the new created udp instance.
314 @param Udp4Service Pointer to the UDP4_SERVICE_DATA.
315 @param Instance Pointer to the un-initialized UDP4_INSTANCE_DATA.
322 IN UDP4_SERVICE_DATA
*Udp4Service
,
323 IN OUT UDP4_INSTANCE_DATA
*Instance
327 // Set the signature.
329 Instance
->Signature
= UDP4_INSTANCE_DATA_SIGNATURE
;
334 InitializeListHead (&Instance
->Link
);
335 InitializeListHead (&Instance
->RcvdDgramQue
);
336 InitializeListHead (&Instance
->DeliveredDgramQue
);
339 // Init the NET_MAPs.
341 NetMapInit (&Instance
->TxTokens
);
342 NetMapInit (&Instance
->RxTokens
);
343 NetMapInit (&Instance
->McastIps
);
346 // Save the pointer to the UDP4_SERVICE_DATA, and initialize other members.
348 Instance
->Udp4Service
= Udp4Service
;
349 CopyMem (&Instance
->Udp4Proto
, &mUdp4Protocol
, sizeof (Instance
->Udp4Proto
));
350 Instance
->IcmpError
= EFI_SUCCESS
;
351 Instance
->Configured
= FALSE
;
352 Instance
->IsNoMapping
= FALSE
;
353 Instance
->Destroyed
= FALSE
;
358 This function cleans the udp instance.
360 @param Instance Pointer to the UDP4_INSTANCE_DATA to clean.
367 IN UDP4_INSTANCE_DATA
*Instance
370 NetMapClean (&Instance
->McastIps
);
371 NetMapClean (&Instance
->RxTokens
);
372 NetMapClean (&Instance
->TxTokens
);
377 This function finds the udp instance by the specified <Address, Port> pair.
379 @param InstanceList Pointer to the head of the list linking the udp
381 @param Address Pointer to the specified IPv4 address.
382 @param Port The udp port number.
384 @retval TRUE The specified <Address, Port> pair is found.
385 @retval FALSE Otherwise.
389 Udp4FindInstanceByPort (
390 IN LIST_ENTRY
*InstanceList
,
391 IN EFI_IPv4_ADDRESS
*Address
,
396 UDP4_INSTANCE_DATA
*Instance
;
397 EFI_UDP4_CONFIG_DATA
*ConfigData
;
399 NET_LIST_FOR_EACH (Entry
, InstanceList
) {
401 // Iterate all the udp instances.
403 Instance
= NET_LIST_USER_STRUCT (Entry
, UDP4_INSTANCE_DATA
, Link
);
404 ConfigData
= &Instance
->ConfigData
;
406 if (!Instance
->Configured
|| ConfigData
->AcceptAnyPort
) {
408 // If the instance is not configured or the configdata of the instance indicates
409 // this instance accepts any port, skip it.
414 if (EFI_IP4_EQUAL (&ConfigData
->StationAddress
, Address
) &&
415 (ConfigData
->StationPort
== Port
)) {
417 // if both the address and the port are the same, return TRUE.
424 // return FALSE when matching fails.
431 This function tries to bind the udp instance according to the configured port
434 @param InstanceList Pointer to the head of the list linking the udp
436 @param ConfigData Pointer to the ConfigData of the instance to be
437 bound. ConfigData->StationPort will be assigned
438 with an available port value on success.
440 @retval EFI_SUCCESS The bound operation is completed successfully.
441 @retval EFI_ACCESS_DENIED The <Address, Port> specified by the ConfigData is
442 already used by other instance.
443 @retval EFI_OUT_OF_RESOURCES No available port resources.
448 IN LIST_ENTRY
*InstanceList
,
449 IN OUT EFI_UDP4_CONFIG_DATA
*ConfigData
452 EFI_IPv4_ADDRESS
*StationAddress
;
455 if (ConfigData
->AcceptAnyPort
) {
459 StationAddress
= &ConfigData
->StationAddress
;
461 if (ConfigData
->StationPort
!= 0) {
463 if (!ConfigData
->AllowDuplicatePort
&&
464 Udp4FindInstanceByPort (InstanceList
, StationAddress
, ConfigData
->StationPort
)) {
466 // Do not allow duplicate port and the port is already used by other instance.
468 return EFI_ACCESS_DENIED
;
472 // select a random port for this instance;
475 if (ConfigData
->AllowDuplicatePort
) {
477 // Just pick up the random port if the instance allows duplicate port.
479 ConfigData
->StationPort
= mUdp4RandomPort
;
482 StartPort
= mUdp4RandomPort
;
484 while (Udp4FindInstanceByPort(InstanceList
, StationAddress
, mUdp4RandomPort
)) {
487 if (mUdp4RandomPort
== 0) {
488 mUdp4RandomPort
= UDP4_PORT_KNOWN
;
491 if (mUdp4RandomPort
== StartPort
) {
493 // No available port.
495 return EFI_OUT_OF_RESOURCES
;
499 ConfigData
->StationPort
= mUdp4RandomPort
;
503 if (mUdp4RandomPort
== 0) {
504 mUdp4RandomPort
= UDP4_PORT_KNOWN
;
513 This function is used to check whether the NewConfigData has any un-reconfigurable
514 parameters changed compared to the OldConfigData.
516 @param OldConfigData Pointer to the current ConfigData the udp instance
518 @param NewConfigData Pointer to the new ConfigData.
520 @retval TRUE The instance is reconfigurable.
521 @retval FALSE Otherwise.
525 Udp4IsReconfigurable (
526 IN EFI_UDP4_CONFIG_DATA
*OldConfigData
,
527 IN EFI_UDP4_CONFIG_DATA
*NewConfigData
530 if ((NewConfigData
->AcceptAnyPort
!= OldConfigData
->AcceptAnyPort
) ||
531 (NewConfigData
->AcceptBroadcast
!= OldConfigData
->AcceptBroadcast
) ||
532 (NewConfigData
->AcceptPromiscuous
!= OldConfigData
->AcceptPromiscuous
) ||
533 (NewConfigData
->AllowDuplicatePort
!= OldConfigData
->AllowDuplicatePort
)
536 // The receiving filter parameters cannot be changed.
541 if ((!NewConfigData
->AcceptAnyPort
) &&
542 (NewConfigData
->StationPort
!= OldConfigData
->StationPort
)
545 // The port is not changeable.
550 if (!NewConfigData
->AcceptPromiscuous
) {
552 if (NewConfigData
->UseDefaultAddress
!= OldConfigData
->UseDefaultAddress
) {
554 // The NewConfigData differs to the old one on the UseDefaultAddress.
559 if (!NewConfigData
->UseDefaultAddress
&&
560 (!EFI_IP4_EQUAL (&NewConfigData
->StationAddress
, &OldConfigData
->StationAddress
) ||
561 !EFI_IP4_EQUAL (&NewConfigData
->SubnetMask
, &OldConfigData
->SubnetMask
))
564 // If the instance doesn't use the default address, and the new address or
565 // new subnet mask is different from the old values.
571 if (!EFI_IP4_EQUAL (&NewConfigData
->RemoteAddress
, &OldConfigData
->RemoteAddress
)) {
573 // The remoteaddress is not the same.
578 if (!EFI_IP4_EQUAL (&NewConfigData
->RemoteAddress
, &mZeroIp4Addr
) &&
579 NewConfigData
->RemotePort
!= OldConfigData
->RemotePort
582 // The RemotePort differs if it's designated in the configdata.
588 // All checks pass, return TRUE.
595 This function builds the Ip4 configdata from the Udp4ConfigData.
597 @param Udp4ConfigData Pointer to the EFI_UDP4_CONFIG_DATA.
598 @param Ip4ConfigData Pointer to the EFI_IP4_CONFIG_DATA.
604 Udp4BuildIp4ConfigData (
605 IN EFI_UDP4_CONFIG_DATA
*Udp4ConfigData
,
606 IN OUT EFI_IP4_CONFIG_DATA
*Ip4ConfigData
609 CopyMem (Ip4ConfigData
, &mIpIoDefaultIpConfigData
, sizeof (*Ip4ConfigData
));
611 Ip4ConfigData
->DefaultProtocol
= EFI_IP_PROTO_UDP
;
612 Ip4ConfigData
->AcceptBroadcast
= Udp4ConfigData
->AcceptBroadcast
;
613 Ip4ConfigData
->AcceptPromiscuous
= Udp4ConfigData
->AcceptPromiscuous
;
614 Ip4ConfigData
->UseDefaultAddress
= Udp4ConfigData
->UseDefaultAddress
;
615 CopyMem (&Ip4ConfigData
->StationAddress
, &Udp4ConfigData
->StationAddress
, sizeof (EFI_IPv4_ADDRESS
));
616 CopyMem (&Ip4ConfigData
->SubnetMask
, &Udp4ConfigData
->SubnetMask
, sizeof (EFI_IPv4_ADDRESS
));
619 // use the -1 magic number to disable the receiving process of the ip instance.
621 Ip4ConfigData
->ReceiveTimeout
= (UINT32
) (-1);
626 This function validates the TxToken, it returns the error code according to the spec.
628 @param Instance Pointer to the udp instance context data.
629 @param TxToken Pointer to the token to be checked.
631 @retval EFI_SUCCESS The TxToken is valid.
632 @retval EFI_INVALID_PARAMETER One or more of the following are TRUE: This is
633 NULL. Token is NULL. Token.Event is NULL.
634 Token.Packet.TxData is NULL.
635 Token.Packet.TxData.FragmentCount is zero.
636 Token.Packet.TxData.DataLength is not equal to the
637 sum of fragment lengths. One or more of the
638 Token.Packet.TxData.FragmentTable[].
639 FragmentLength fields is zero. One or more of the
640 Token.Packet.TxData.FragmentTable[].
641 FragmentBuffer fields is NULL.
642 Token.Packet.TxData. GatewayAddress is not a
643 unicast IPv4 address if it is not NULL. One or
644 more IPv4 addresses in Token.Packet.TxData.
645 UdpSessionData are not valid unicast IPv4
646 addresses if the UdpSessionData is not NULL.
647 @retval EFI_BAD_BUFFER_SIZE The data length is greater than the maximum UDP
652 Udp4ValidateTxToken (
653 IN UDP4_INSTANCE_DATA
*Instance
,
654 IN EFI_UDP4_COMPLETION_TOKEN
*TxToken
657 EFI_UDP4_TRANSMIT_DATA
*TxData
;
660 EFI_UDP4_CONFIG_DATA
*ConfigData
;
661 EFI_UDP4_SESSION_DATA
*UdpSessionData
;
662 IP4_ADDR SourceAddress
;
663 IP4_ADDR GatewayAddress
;
665 if (TxToken
->Event
== NULL
) {
666 return EFI_INVALID_PARAMETER
;
669 TxData
= TxToken
->Packet
.TxData
;
671 if ((TxData
== NULL
) || (TxData
->FragmentCount
== 0)) {
672 return EFI_INVALID_PARAMETER
;
676 for (Index
= 0; Index
< TxData
->FragmentCount
; Index
++) {
678 if ((TxData
->FragmentTable
[Index
].FragmentBuffer
== NULL
) ||
679 (TxData
->FragmentTable
[Index
].FragmentLength
== 0)) {
681 // if the FragmentBuffer is NULL or the FragmentLeng is zero.
683 return EFI_INVALID_PARAMETER
;
686 TotalLen
+= TxData
->FragmentTable
[Index
].FragmentLength
;
689 if (TotalLen
!= TxData
->DataLength
) {
691 // The TotalLen calculated by adding all the FragmentLeng doesn't equal to the
694 return EFI_INVALID_PARAMETER
;
697 if (TxData
->GatewayAddress
!= NULL
) {
698 CopyMem (&GatewayAddress
, TxData
->GatewayAddress
, sizeof (IP4_ADDR
));
700 if (!Ip4IsUnicast (NTOHL (GatewayAddress
), 0)) {
702 // The specified GatewayAddress is not a unicast IPv4 address while it's not 0.
704 return EFI_INVALID_PARAMETER
;
708 ConfigData
= &Instance
->ConfigData
;
709 UdpSessionData
= TxData
->UdpSessionData
;
711 if (UdpSessionData
!= NULL
) {
713 CopyMem (&SourceAddress
, &UdpSessionData
->SourceAddress
, sizeof (IP4_ADDR
));
715 if ((SourceAddress
!= 0) && !Ip4IsUnicast (HTONL (SourceAddress
), 0)) {
717 // Check whether SourceAddress is a valid IPv4 address in case it's not zero.
718 // The configured station address is used if SourceAddress is zero.
720 return EFI_INVALID_PARAMETER
;
723 if ((UdpSessionData
->DestinationPort
== 0) && (ConfigData
->RemotePort
== 0)) {
725 // Ambiguous, no avalaible DestinationPort for this token.
727 return EFI_INVALID_PARAMETER
;
730 if (EFI_IP4_EQUAL (&UdpSessionData
->DestinationAddress
, &mZeroIp4Addr
)) {
732 // The DestinationAddress specified in the UdpSessionData is 0.
734 return EFI_INVALID_PARAMETER
;
736 } else if (EFI_IP4_EQUAL (&ConfigData
->RemoteAddress
, &mZeroIp4Addr
)) {
738 // the configured RemoteAddress is all zero, and the user doens't override the
739 // destination address.
741 return EFI_INVALID_PARAMETER
;
744 if (TxData
->DataLength
> UDP4_MAX_DATA_SIZE
) {
745 return EFI_BAD_BUFFER_SIZE
;
753 This function checks whether the specified Token duplicates with the one in the Map.
755 @param Map Pointer to the NET_MAP.
756 @param Item Pointer to the NET_MAP_ITEM contain the pointer to
758 @param Context Pointer to the Token to be checked.
760 @retval EFI_SUCCESS The Token specified by Context differs from the
762 @retval EFI_ACCESS_DENIED The Token duplicates with the one in the Item.
768 IN NET_MAP_ITEM
*Item
,
772 EFI_UDP4_COMPLETION_TOKEN
*Token
;
773 EFI_UDP4_COMPLETION_TOKEN
*TokenInItem
;
775 Token
= (EFI_UDP4_COMPLETION_TOKEN
*) Context
;
776 TokenInItem
= (EFI_UDP4_COMPLETION_TOKEN
*) Item
->Key
;
778 if ((Token
== TokenInItem
) || (Token
->Event
== TokenInItem
->Event
)) {
780 // The Token duplicates with the TokenInItem in case either the two pointers are the
781 // same or the Events of these two tokens are the same.
783 return EFI_ACCESS_DENIED
;
791 This function calculates the checksum for the Packet, utilizing the pre-calculated
792 pseudo HeadSum to reduce some overhead.
794 @param Packet Pointer to the NET_BUF contains the udp datagram.
795 @param HeadSum Checksum of the pseudo header execpt the length
798 @return The 16-bit checksum of this udp datagram.
809 Checksum
= NetbufChecksum (Packet
);
810 Checksum
= NetAddChecksum (Checksum
, HeadSum
);
812 Checksum
= NetAddChecksum (Checksum
, HTONS ((UINT16
) Packet
->TotalSize
));
814 return (UINT16
) ~Checksum
;
819 This function removes the specified Token from the TokenMap.
821 @param TokenMap Pointer to the NET_MAP containing the tokens.
822 @param Token Pointer to the Token to be removed.
824 @retval EFI_SUCCESS The specified Token is removed from the TokenMap.
825 @retval EFI_NOT_FOUND The specified Token is not found in the TokenMap.
830 IN OUT NET_MAP
*TokenMap
,
831 IN EFI_UDP4_COMPLETION_TOKEN
*Token
837 // Find the Token first.
839 Item
= NetMapFindKey (TokenMap
, (VOID
*) Token
);
843 // Remove the token if it's found in the map.
845 NetMapRemoveItem (TokenMap
, Item
, NULL
);
850 return EFI_NOT_FOUND
;
855 This function is the packet transmitting notify function registered to the IpIo
856 interface. It's called to signal the udp TxToken when IpIo layer completes the
857 transmitting of the udp datagram.
859 @param Status The completion status of the output udp datagram.
860 @param Context Pointer to the context data.
861 @param Sender Pointer to the Ip sender of the udp datagram.
862 @param NotifyData Pointer to the notify data.
869 IN EFI_STATUS Status
,
875 UDP4_INSTANCE_DATA
*Instance
;
876 EFI_UDP4_COMPLETION_TOKEN
*Token
;
878 Instance
= (UDP4_INSTANCE_DATA
*) Context
;
879 Token
= (EFI_UDP4_COMPLETION_TOKEN
*) NotifyData
;
881 if (Udp4RemoveToken (&Instance
->TxTokens
, Token
) == EFI_SUCCESS
) {
883 // The token may be cancelled. Only signal it if the remove operation succeeds.
885 Token
->Status
= Status
;
886 gBS
->SignalEvent (Token
->Event
);
887 NetLibDispatchDpc ();
893 This function processes the received datagram passed up by the IpIo layer.
895 @param Status The status of this udp datagram.
896 @param IcmpError The IcmpError code, only available when Status is
898 @param NetSession Pointer to the EFI_NET_SESSION_DATA.
899 @param Packet Pointer to the NET_BUF containing the received udp
901 @param Context Pointer to the context data.
908 IN EFI_STATUS Status
,
909 IN ICMP_ERROR IcmpError
,
910 IN EFI_NET_SESSION_DATA
*NetSession
,
915 NET_CHECK_SIGNATURE (Packet
, NET_BUF_SIGNATURE
);
918 // IpIo only passes received packets with Status EFI_SUCCESS or EFI_ICMP_ERROR.
920 if (Status
== EFI_SUCCESS
) {
922 // Demultiplex the received datagram.
924 Udp4Demultiplex ((UDP4_SERVICE_DATA
*) Context
, NetSession
, Packet
);
927 // Handle the ICMP_ERROR packet.
929 Udp4IcmpHandler ((UDP4_SERVICE_DATA
*) Context
, IcmpError
, NetSession
, Packet
);
933 // Dispatch the DPC queued by the NotifyFunction of the rx token's events
934 // which are signaled with received data.
936 NetLibDispatchDpc ();
941 This function removes the multicast group specified by Arg from the Map.
943 @param Map Pointer to the NET_MAP.
944 @param Item Pointer to the NET_MAP_ITEM.
945 @param Arg Pointer to the Arg, it's the pointer to a
946 multicast IPv4 Address.
948 @retval EFI_SUCCESS The multicast address is removed.
949 @retval EFI_ABORTED The specified multicast address is removed and the
956 IN NET_MAP_ITEM
*Item
,
957 IN VOID
*Arg OPTIONAL
960 EFI_IPv4_ADDRESS
*McastIp
;
964 if ((McastIp
!= NULL
) && (!EFI_IP4_EQUAL (McastIp
, &(Item
->Key
)))) {
966 // McastIp is not NULL and the multicast address contained in the Item
967 // is not the same as McastIp.
975 NetMapRemoveItem (Map
, Item
, NULL
);
977 if (McastIp
!= NULL
) {
979 // Return EFI_ABORTED in case McastIp is not NULL to terminate the iteration.
989 This function cancels the token specified by Arg in the Map. This is a callback
990 used by Udp4InstanceCancelToken().
992 @param Map Pointer to the NET_MAP.
993 @param Item Pointer to the NET_MAP_ITEM.
994 @param Arg Pointer to the token to be cancelled, if NULL,
995 the token specified by Item is cancelled.
997 @retval EFI_SUCCESS The token is cancelled if Arg is NULL or the token
998 is not the same as that in the Item if Arg is not
1000 @retval EFI_ABORTED Arg is not NULL, and the token specified by Arg is
1007 IN NET_MAP_ITEM
*Item
,
1008 IN VOID
*Arg OPTIONAL
1011 EFI_UDP4_COMPLETION_TOKEN
*TokenToCancel
;
1015 if ((Arg
!= NULL
) && (Item
->Key
!= Arg
)) {
1019 if (Item
->Value
!= NULL
) {
1021 // If the token is a transmit token, the corresponding Packet is recorded in
1022 // Item->Value, invoke IpIo to cancel this packet first. The IpIoCancelTxToken
1023 // will invoke Udp4DgramSent, the token will be signaled and this Item will
1024 // be removed from the Map there.
1026 Packet
= (NET_BUF
*) (Item
->Value
);
1027 IpIo
= (IP_IO
*) (*((UINTN
*) &Packet
->ProtoData
[0]));
1029 IpIoCancelTxToken (IpIo
, Packet
);
1032 // The token is a receive token. Abort it and remove it from the Map.
1034 TokenToCancel
= (EFI_UDP4_COMPLETION_TOKEN
*) Item
->Key
;
1035 NetMapRemoveItem (Map
, Item
, NULL
);
1037 TokenToCancel
->Status
= EFI_ABORTED
;
1038 gBS
->SignalEvent (TokenToCancel
->Event
);
1050 This function removes all the Wrap datas in the RcvdDgramQue.
1052 @param Instance Pointer to the udp instance context data.
1058 Udp4FlushRcvdDgram (
1059 IN UDP4_INSTANCE_DATA
*Instance
1062 UDP4_RXDATA_WRAP
*Wrap
;
1064 while (!IsListEmpty (&Instance
->RcvdDgramQue
)) {
1066 // Iterate all the Wraps in the RcvdDgramQue.
1068 Wrap
= NET_LIST_HEAD (&Instance
->RcvdDgramQue
, UDP4_RXDATA_WRAP
, Link
);
1071 // The Wrap will be removed from the RcvdDgramQue by this function call.
1073 Udp4RecycleRxDataWrap (NULL
, (VOID
*) Wrap
);
1080 Cancel Udp4 tokens from the Udp4 instance.
1082 @param Instance Pointer to the udp instance context data.
1083 @param Token Pointer to the token to be canceled, if NULL, all
1084 tokens in this instance will be cancelled.
1086 @retval EFI_SUCCESS The Token is cancelled.
1087 @retval EFI_NOT_FOUND The Token is not found.
1091 Udp4InstanceCancelToken (
1092 IN UDP4_INSTANCE_DATA
*Instance
,
1093 IN EFI_UDP4_COMPLETION_TOKEN
*Token OPTIONAL
1099 // Cancel this token from the TxTokens map.
1101 Status
= NetMapIterate (&Instance
->TxTokens
, Udp4CancelTokens
, Token
);
1103 if ((Token
!= NULL
) && (Status
== EFI_ABORTED
)) {
1105 // If Token isn't NULL and Status is EFI_ABORTED, the token is cancelled from
1106 // the TxTokens, just return success.
1112 // Try to cancel this token from the RxTokens map in condition either the Token
1113 // is NULL or the specified Token is not in TxTokens.
1115 Status
= NetMapIterate (&Instance
->RxTokens
, Udp4CancelTokens
, Token
);
1117 if ((Token
!= NULL
) && (Status
== EFI_SUCCESS
)) {
1119 // If Token isn't NULL and Status is EFI_SUCCESS, the token is neither in the
1120 // TxTokens nor the RxTokens, or say, it's not found.
1122 return EFI_NOT_FOUND
;
1125 ASSERT ((Token
!= NULL
) || ((0 == NetMapGetCount (&Instance
->TxTokens
))
1126 && (0 == NetMapGetCount (&Instance
->RxTokens
))));
1133 This function matches the received udp datagram with the Instance.
1135 @param Instance Pointer to the udp instance context data.
1136 @param Udp4Session Pointer to the EFI_UDP4_SESSION_DATA abstracted
1137 from the received udp datagram.
1139 @retval TRUE The udp datagram matches the receiving requirments of the
1141 @retval FALSE Otherwise.
1146 IN UDP4_INSTANCE_DATA
*Instance
,
1147 IN EFI_UDP4_SESSION_DATA
*Udp4Session
1150 EFI_UDP4_CONFIG_DATA
*ConfigData
;
1151 IP4_ADDR Destination
;
1153 ConfigData
= &Instance
->ConfigData
;
1155 if (ConfigData
->AcceptPromiscuous
) {
1157 // Always matches if this instance is in the promiscuous state.
1162 if ((!ConfigData
->AcceptAnyPort
&& (Udp4Session
->DestinationPort
!= ConfigData
->StationPort
)) ||
1163 ((ConfigData
->RemotePort
!= 0) && (Udp4Session
->SourcePort
!= ConfigData
->RemotePort
))
1166 // The local port or the remote port doesn't match.
1171 if (!EFI_IP4_EQUAL (&ConfigData
->RemoteAddress
, &mZeroIp4Addr
) &&
1172 !EFI_IP4_EQUAL (&ConfigData
->RemoteAddress
, &Udp4Session
->SourceAddress
)
1175 // This datagram doesn't come from the instance's specified sender.
1180 if (EFI_IP4_EQUAL (&ConfigData
->StationAddress
, &mZeroIp4Addr
) ||
1181 EFI_IP4_EQUAL (&Udp4Session
->DestinationAddress
, &ConfigData
->StationAddress
)
1184 // The instance is configured to receive datagrams destined to any station IP or
1185 // the destination address of this datagram matches the configured station IP.
1190 CopyMem (&Destination
, &Udp4Session
->DestinationAddress
, sizeof (IP4_ADDR
));
1192 if (IP4_IS_LOCAL_BROADCAST (Destination
) && ConfigData
->AcceptBroadcast
) {
1194 // The instance is configured to receive broadcast and this is a broadcast packet.
1199 if (IP4_IS_MULTICAST (NTOHL (Destination
)) &&
1200 NetMapFindKey (&Instance
->McastIps
, (VOID
*) (UINTN
) Destination
) != NULL
1203 // It's a multicast packet and the multicast address is accepted by this instance.
1213 This function removes the Wrap specified by Context and release relevant resources.
1215 @param Event The Event this notify function registered to.
1216 @param Context Pointer to the context data.
1223 Udp4RecycleRxDataWrap (
1228 UDP4_RXDATA_WRAP
*Wrap
;
1230 Wrap
= (UDP4_RXDATA_WRAP
*) Context
;
1233 // Remove the Wrap from the list it belongs to.
1235 RemoveEntryList (&Wrap
->Link
);
1238 // Free the Packet associated with this Wrap.
1240 NetbufFree (Wrap
->Packet
);
1245 gBS
->CloseEvent (Wrap
->RxData
.RecycleSignal
);
1247 gBS
->FreePool (Wrap
);
1252 This function wraps the Packet and the RxData.
1254 @param Instance Pointer to the instance context data.
1255 @param Packet Pointer to the buffer containing the received
1257 @param RxData Pointer to the EFI_UDP4_RECEIVE_DATA of this
1260 @return Pointer to the structure wrapping the RxData and the Packet.
1265 IN UDP4_INSTANCE_DATA
*Instance
,
1267 IN EFI_UDP4_RECEIVE_DATA
*RxData
1271 UDP4_RXDATA_WRAP
*Wrap
;
1274 // Allocate buffer for the Wrap.
1276 Wrap
= AllocatePool (sizeof (UDP4_RXDATA_WRAP
) +
1277 (Packet
->BlockOpNum
- 1) * sizeof (EFI_UDP4_FRAGMENT_DATA
));
1282 InitializeListHead (&Wrap
->Link
);
1284 CopyMem (&Wrap
->RxData
, RxData
, sizeof (Wrap
->RxData
));
1287 // Create the Recycle event.
1289 Status
= gBS
->CreateEvent (
1292 Udp4RecycleRxDataWrap
,
1294 &Wrap
->RxData
.RecycleSignal
1296 if (EFI_ERROR (Status
)) {
1297 gBS
->FreePool (Wrap
);
1301 Wrap
->Packet
= Packet
;
1302 Wrap
->TimeoutTick
= Instance
->ConfigData
.ReceiveTimeout
;
1309 This function enqueues the received datagram into the instances' receiving queues.
1311 @param Udp4Service Pointer to the udp service context data.
1312 @param Packet Pointer to the buffer containing the received
1314 @param RxData Pointer to the EFI_UDP4_RECEIVE_DATA of this
1317 @return The times this datagram is enqueued.
1322 IN UDP4_SERVICE_DATA
*Udp4Service
,
1324 IN EFI_UDP4_RECEIVE_DATA
*RxData
1328 UDP4_INSTANCE_DATA
*Instance
;
1329 UDP4_RXDATA_WRAP
*Wrap
;
1334 NET_LIST_FOR_EACH (Entry
, &Udp4Service
->ChildrenList
) {
1336 // Iterate the instances.
1338 Instance
= NET_LIST_USER_STRUCT (Entry
, UDP4_INSTANCE_DATA
, Link
);
1340 if (!Instance
->Configured
) {
1344 if (Udp4MatchDgram (Instance
, &RxData
->UdpSession
)) {
1346 // Wrap the RxData and put this Wrap into the instances RcvdDgramQue.
1348 Wrap
= Udp4WrapRxData (Instance
, Packet
, RxData
);
1353 NET_GET_REF (Packet
);
1355 InsertTailList (&Instance
->RcvdDgramQue
, &Wrap
->Link
);
1366 This function delivers the received datagrams for the specified instance.
1368 @param Instance Pointer to the instance context data.
1374 Udp4InstanceDeliverDgram (
1375 IN UDP4_INSTANCE_DATA
*Instance
1378 UDP4_RXDATA_WRAP
*Wrap
;
1379 EFI_UDP4_COMPLETION_TOKEN
*Token
;
1381 EFI_UDP4_RECEIVE_DATA
*RxData
;
1384 if (!IsListEmpty (&Instance
->RcvdDgramQue
) &&
1385 !NetMapIsEmpty (&Instance
->RxTokens
)) {
1387 Wrap
= NET_LIST_HEAD (&Instance
->RcvdDgramQue
, UDP4_RXDATA_WRAP
, Link
);
1389 if (NET_BUF_SHARED (Wrap
->Packet
)) {
1391 // Duplicate the Packet if it is shared between instances.
1393 Dup
= NetbufDuplicate (Wrap
->Packet
, NULL
, 0);
1398 NetbufFree (Wrap
->Packet
);
1403 NetListRemoveHead (&Instance
->RcvdDgramQue
);
1405 Token
= (EFI_UDP4_COMPLETION_TOKEN
*) NetMapRemoveHead (&Instance
->RxTokens
, NULL
);
1408 // Build the FragmentTable and set the FragmentCount in RxData.
1410 RxData
= &Wrap
->RxData
;
1411 RxData
->FragmentCount
= Wrap
->Packet
->BlockOpNum
;
1415 (NET_FRAGMENT
*) RxData
->FragmentTable
,
1416 &RxData
->FragmentCount
1419 Token
->Status
= EFI_SUCCESS
;
1420 Token
->Packet
.RxData
= &Wrap
->RxData
;
1422 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1423 InsertTailList (&Instance
->DeliveredDgramQue
, &Wrap
->Link
);
1424 gBS
->RestoreTPL (OldTpl
);
1426 gBS
->SignalEvent (Token
->Event
);
1432 This function delivers the datagrams enqueued in the instances.
1434 @param Udp4Service Pointer to the udp service context data.
1441 IN UDP4_SERVICE_DATA
*Udp4Service
1445 UDP4_INSTANCE_DATA
*Instance
;
1447 NET_LIST_FOR_EACH (Entry
, &Udp4Service
->ChildrenList
) {
1449 // Iterate the instances.
1451 Instance
= NET_LIST_USER_STRUCT (Entry
, UDP4_INSTANCE_DATA
, Link
);
1453 if (!Instance
->Configured
) {
1458 // Deliver the datagrams of this instance.
1460 Udp4InstanceDeliverDgram (Instance
);
1466 This function demultiplexes the received udp datagram to the apropriate instances.
1468 @param Udp4Service Pointer to the udp service context data.
1469 @param NetSession Pointer to the EFI_NET_SESSION_DATA abstrated from
1470 the received datagram.
1471 @param Packet Pointer to the buffer containing the received udp
1479 IN UDP4_SERVICE_DATA
*Udp4Service
,
1480 IN EFI_NET_SESSION_DATA
*NetSession
,
1484 EFI_UDP4_HEADER
*Udp4Header
;
1486 EFI_UDP4_RECEIVE_DATA RxData
;
1487 EFI_UDP4_SESSION_DATA
*Udp4Session
;
1491 // Get the datagram header from the packet buffer.
1493 Udp4Header
= (EFI_UDP4_HEADER
*) NetbufGetByte (Packet
, 0, NULL
);
1495 if (Udp4Header
->Checksum
!= 0) {
1497 // check the checksum.
1499 HeadSum
= NetPseudoHeadChecksum (
1506 if (Udp4Checksum (Packet
, HeadSum
) != 0) {
1514 gRT
->GetTime (&RxData
.TimeStamp
, NULL
);
1516 Udp4Session
= &RxData
.UdpSession
;
1517 Udp4Session
->SourcePort
= NTOHS (Udp4Header
->SrcPort
);
1518 Udp4Session
->DestinationPort
= NTOHS (Udp4Header
->DstPort
);
1520 CopyMem (&Udp4Session
->SourceAddress
, &NetSession
->Source
, sizeof (EFI_IPv4_ADDRESS
));
1521 CopyMem (&Udp4Session
->DestinationAddress
, &NetSession
->Dest
, sizeof (EFI_IPv4_ADDRESS
));
1524 // Trim the UDP header.
1526 NetbufTrim (Packet
, UDP4_HEADER_SIZE
, TRUE
);
1528 RxData
.DataLength
= (UINT32
) Packet
->TotalSize
;
1531 // Try to enqueue this datagram into the instances.
1533 Enqueued
= Udp4EnqueueDgram (Udp4Service
, Packet
, &RxData
);
1535 if (Enqueued
== 0) {
1537 // Send the port unreachable ICMP packet before we free this NET_BUF
1539 Udp4SendPortUnreach (Udp4Service
->IpIo
, NetSession
, Udp4Header
);
1543 // Try to free the packet before deliver it.
1545 NetbufFree (Packet
);
1549 // Deliver the datagram.
1551 Udp4DeliverDgram (Udp4Service
);
1557 This function builds and sends out a icmp port unreachable message.
1559 @param IpIo Pointer to the IP_IO instance.
1560 @param NetSession Pointer to the EFI_NET_SESSION_DATA of the packet
1561 causes this icmp error message.
1562 @param Udp4Header Pointer to the udp header of the datagram causes
1563 this icmp error message.
1569 Udp4SendPortUnreach (
1571 IN EFI_NET_SESSION_DATA
*NetSession
,
1577 IP4_ICMP_ERROR_HEAD
*IcmpErrHdr
;
1578 EFI_IP4_HEADER
*IpHdr
;
1580 IP_IO_OVERRIDE Override
;
1581 IP_IO_IP_INFO
*IpSender
;
1583 IpSender
= IpIoFindSender (&IpIo
, NetSession
->Dest
);
1584 if (IpSender
== NULL
) {
1586 // No apropriate sender, since we cannot send out the ICMP message through
1587 // the default zero station address IP instance, abort.
1592 IpHdr
= NetSession
->IpHdr
;
1595 // Calculate the requried length of the icmp error message.
1597 Len
= sizeof (IP4_ICMP_ERROR_HEAD
) + (EFI_IP4_HEADER_LEN (IpHdr
) -
1598 sizeof (IP4_HEAD
)) + ICMP_ERROR_PACKET_LENGTH
;
1601 // Allocate buffer for the icmp error message.
1603 Packet
= NetbufAlloc (Len
);
1604 if (Packet
== NULL
) {
1609 // Allocate space for the IP4_ICMP_ERROR_HEAD.
1611 IcmpErrHdr
= (IP4_ICMP_ERROR_HEAD
*) NetbufAllocSpace (Packet
, Len
, FALSE
);
1614 // Set the required fields for the icmp port unreachable message.
1616 IcmpErrHdr
->Head
.Type
= ICMP_TYPE_UNREACH
;
1617 IcmpErrHdr
->Head
.Code
= ICMP_CODE_UNREACH_PORT
;
1618 IcmpErrHdr
->Head
.Checksum
= 0;
1619 IcmpErrHdr
->Fourth
= 0;
1622 // Copy the IP header of the datagram tragged the error.
1624 CopyMem (&IcmpErrHdr
->IpHead
, IpHdr
, EFI_IP4_HEADER_LEN (IpHdr
));
1627 // Copy the UDP header.
1629 Ptr
= (UINT8
*) &IcmpErrHdr
->IpHead
+ EFI_IP4_HEADER_LEN (IpHdr
);
1630 CopyMem (Ptr
, Udp4Header
, ICMP_ERROR_PACKET_LENGTH
);
1633 // Calculate the checksum.
1635 IcmpErrHdr
->Head
.Checksum
= (UINT16
) ~(NetbufChecksum (Packet
));
1638 // Fill the override data.
1640 Override
.DoNotFragment
= FALSE
;
1641 Override
.TypeOfService
= 0;
1642 Override
.TimeToLive
= 255;
1643 Override
.Protocol
= EFI_IP_PROTO_ICMP
;
1645 CopyMem (&Override
.SourceAddress
, &NetSession
->Dest
, sizeof (EFI_IPv4_ADDRESS
));
1646 ZeroMem (&Override
.GatewayAddress
, sizeof (EFI_IPv4_ADDRESS
));
1649 // Send out this icmp packet.
1651 IpIoSend (IpIo
, Packet
, IpSender
, NULL
, NULL
, NetSession
->Source
, &Override
);
1653 NetbufFree (Packet
);
1658 This function handles the received Icmp Error message and demultiplexes it to the
1661 @param Udp4Service Pointer to the udp service context data.
1662 @param IcmpError The icmp error code.
1663 @param NetSession Pointer to the EFI_NET_SESSION_DATA abstracted
1664 from the received Icmp Error packet.
1665 @param Packet Pointer to the Icmp Error packet.
1672 IN UDP4_SERVICE_DATA
*Udp4Service
,
1673 IN ICMP_ERROR IcmpError
,
1674 IN EFI_NET_SESSION_DATA
*NetSession
,
1678 EFI_UDP4_HEADER
*Udp4Header
;
1679 EFI_UDP4_SESSION_DATA Udp4Session
;
1681 UDP4_INSTANCE_DATA
*Instance
;
1683 Udp4Header
= (EFI_UDP4_HEADER
*) NetbufGetByte (Packet
, 0, NULL
);
1685 CopyMem (&Udp4Session
.SourceAddress
, &NetSession
->Source
, sizeof (EFI_IPv4_ADDRESS
));
1686 CopyMem (&Udp4Session
.DestinationAddress
, &NetSession
->Dest
, sizeof (EFI_IPv4_ADDRESS
));
1688 Udp4Session
.SourcePort
= NTOHS (Udp4Header
->DstPort
);
1689 Udp4Session
.DestinationPort
= NTOHS (Udp4Header
->SrcPort
);
1691 NET_LIST_FOR_EACH (Entry
, &Udp4Service
->ChildrenList
) {
1693 // Iterate all the instances.
1695 Instance
= NET_LIST_USER_STRUCT (Entry
, UDP4_INSTANCE_DATA
, Link
);
1697 if (!Instance
->Configured
||
1698 Instance
->ConfigData
.AcceptPromiscuous
||
1699 Instance
->ConfigData
.AcceptAnyPort
||
1700 EFI_IP4_EQUAL (&Instance
->ConfigData
.StationAddress
, &mZeroIp4Addr
)
1703 // Don't try to deliver the ICMP error to this instance if it is not configured,
1704 // or it's configured to be promiscuous or accept any port or accept all the
1710 if (Udp4MatchDgram (Instance
, &Udp4Session
)) {
1712 // Translate the Icmp Error code according to the udp spec.
1714 Instance
->IcmpError
= IpIoGetIcmpErrStatus (IcmpError
, NULL
, NULL
);
1716 if (IcmpError
> ICMP_ERR_UNREACH_PORT
) {
1717 Instance
->IcmpError
= EFI_ICMP_ERROR
;
1721 // Notify the instance with the received Icmp Error.
1723 Udp4ReportIcmpError (Instance
);
1729 NetbufFree (Packet
);
1734 This function reports the received ICMP error.
1736 @param Instance Pointer to the udp instance context data.
1742 Udp4ReportIcmpError (
1743 IN UDP4_INSTANCE_DATA
*Instance
1746 EFI_UDP4_COMPLETION_TOKEN
*Token
;
1748 if (NetMapIsEmpty (&Instance
->RxTokens
)) {
1750 // There are no receive tokens to deliver the ICMP error.
1755 if (EFI_ERROR (Instance
->IcmpError
)) {
1757 // Try to get a RxToken from the RxTokens map.
1759 Token
= (EFI_UDP4_COMPLETION_TOKEN
*) NetMapRemoveHead (&Instance
->RxTokens
, NULL
);
1761 if (Token
!= NULL
) {
1763 // Report the error through the Token.
1765 Token
->Status
= Instance
->IcmpError
;
1766 gBS
->SignalEvent (Token
->Event
);
1769 // Clear the IcmpError.
1771 Instance
->IcmpError
= EFI_SUCCESS
;
1778 This function is a dummy ext-free function for the NET_BUF created for the output
1781 @param Context Pointer to the context data.
1787 Udp4NetVectorExtFree (
1795 Set the Udp4 variable data.
1797 @param Udp4Service Udp4 service data.
1799 @retval EFI_OUT_OF_RESOURCES There are not enough resources to set the
1801 @retval EFI_SUCCESS Set variable successfully.
1802 @retval other Set variable failed.
1806 Udp4SetVariableData (
1807 IN UDP4_SERVICE_DATA
*Udp4Service
1810 UINT32 NumConfiguredInstance
;
1812 UINTN VariableDataSize
;
1813 EFI_UDP4_VARIABLE_DATA
*Udp4VariableData
;
1814 EFI_UDP4_SERVICE_POINT
*Udp4ServicePoint
;
1815 UDP4_INSTANCE_DATA
*Udp4Instance
;
1816 CHAR16
*NewMacString
;
1819 NumConfiguredInstance
= 0;
1822 // Go through the children list to count the configured children.
1824 NET_LIST_FOR_EACH (Entry
, &Udp4Service
->ChildrenList
) {
1825 Udp4Instance
= NET_LIST_USER_STRUCT_S (
1829 UDP4_INSTANCE_DATA_SIGNATURE
1832 if (Udp4Instance
->Configured
) {
1833 NumConfiguredInstance
++;
1838 // Calculate the size of the Udp4VariableData. As there may be no Udp4 child,
1839 // we should add extra buffer for the service points only if the number of configured
1840 // children is more than 1.
1842 VariableDataSize
= sizeof (EFI_UDP4_VARIABLE_DATA
);
1844 if (NumConfiguredInstance
> 1) {
1845 VariableDataSize
+= sizeof (EFI_UDP4_SERVICE_POINT
) * (NumConfiguredInstance
- 1);
1848 Udp4VariableData
= AllocatePool (VariableDataSize
);
1849 if (Udp4VariableData
== NULL
) {
1850 return EFI_OUT_OF_RESOURCES
;
1853 Udp4VariableData
->DriverHandle
= Udp4Service
->ImageHandle
;
1854 Udp4VariableData
->ServiceCount
= NumConfiguredInstance
;
1856 Udp4ServicePoint
= &Udp4VariableData
->Services
[0];
1859 // Go through the children list to fill the configured children's address pairs.
1861 NET_LIST_FOR_EACH (Entry
, &Udp4Service
->ChildrenList
) {
1862 Udp4Instance
= NET_LIST_USER_STRUCT_S (
1866 UDP4_INSTANCE_DATA_SIGNATURE
1869 if (Udp4Instance
->Configured
) {
1870 Udp4ServicePoint
->InstanceHandle
= Udp4Instance
->ChildHandle
;
1871 Udp4ServicePoint
->LocalAddress
= Udp4Instance
->ConfigData
.StationAddress
;
1872 Udp4ServicePoint
->LocalPort
= Udp4Instance
->ConfigData
.StationPort
;
1873 Udp4ServicePoint
->RemoteAddress
= Udp4Instance
->ConfigData
.RemoteAddress
;
1874 Udp4ServicePoint
->RemotePort
= Udp4Instance
->ConfigData
.RemotePort
;
1881 // Get the mac string.
1883 Status
= NetLibGetMacString (
1884 Udp4Service
->ControllerHandle
,
1885 Udp4Service
->ImageHandle
,
1888 if (EFI_ERROR (Status
)) {
1892 if (Udp4Service
->MacString
!= NULL
) {
1894 // The variable is set already, we're going to update it.
1896 if (StrCmp (Udp4Service
->MacString
, NewMacString
) != 0) {
1898 // The mac address is changed, delete the previous variable first.
1901 Udp4Service
->MacString
,
1902 &gEfiUdp4ServiceBindingProtocolGuid
,
1903 EFI_VARIABLE_BOOTSERVICE_ACCESS
,
1909 gBS
->FreePool (Udp4Service
->MacString
);
1912 Udp4Service
->MacString
= NewMacString
;
1914 Status
= gRT
->SetVariable (
1915 Udp4Service
->MacString
,
1916 &gEfiUdp4ServiceBindingProtocolGuid
,
1917 EFI_VARIABLE_BOOTSERVICE_ACCESS
,
1919 (VOID
*) Udp4VariableData
1924 gBS
->FreePool (Udp4VariableData
);
1931 Clear the variable and free the resource.
1933 @param Udp4Service Udp4 service data.
1939 Udp4ClearVariableData (
1940 IN UDP4_SERVICE_DATA
*Udp4Service
1943 ASSERT (Udp4Service
->MacString
!= NULL
);
1946 Udp4Service
->MacString
,
1947 &gEfiUdp4ServiceBindingProtocolGuid
,
1948 EFI_VARIABLE_BOOTSERVICE_ACCESS
,
1953 gBS
->FreePool (Udp4Service
->MacString
);
1954 Udp4Service
->MacString
= NULL
;