2 Udp6 driver's whole implementation.
4 Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php.
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 UINT16 mUdp6RandomPort
;
21 This function checks and timeouts the I/O datagrams holding by the corresponding
24 @param[in] Event The event this function is 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 IPv6 address.
42 @param[in] Port The udp port number.
44 @retval TRUE The specified <Address, Port> pair is found.
45 @retval FALSE Otherwise.
49 Udp6FindInstanceByPort (
50 IN LIST_ENTRY
*InstanceList
,
51 IN EFI_IPv6_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 the IpIo layer completes
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 EFI_IP6_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 cancle the token specified by Arg in the Map.
100 @param[in] Map Pointer to the NET_MAP.
101 @param[in] Item Pointer to the NET_MAP_ITEM.
102 @param[in] Arg Pointer to the token to be cancelled, if NULL, all
103 the tokens in this Map will be cancelled.
104 This parameter is optional and may be NULL.
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 check if the received udp datagram matches with the Instance.
124 @param[in] Instance Pointer to the udp instance context data.
125 @param[in] Udp6Session Pointer to the EFI_UDP6_SESSION_DATA abstracted
126 from the received udp datagram.
128 @retval TRUE The udp datagram matches the receiving requirements of the Instance.
129 @retval FALSE The udp datagram doe not match the receiving requirements of the Instance.
134 IN UDP6_INSTANCE_DATA
*Instance
,
135 IN EFI_UDP6_SESSION_DATA
*Udp6Session
139 This function removes the Wrap specified by Context and releases relevant resources.
141 @param[in] Event The Event this notify function is registered to.
142 @param[in] Context Pointer to the context data.
147 Udp6RecycleRxDataWrap (
153 This function wraps the Packet into RxData.
155 @param[in] Instance Pointer to the instance context data.
156 @param[in] Packet Pointer to the buffer containing the received
158 @param[in] RxData Pointer to the EFI_UDP6_RECEIVE_DATA of this
161 @return Pointer to the structure wrapping the RxData and the Packet.
166 IN UDP6_INSTANCE_DATA
*Instance
,
168 IN EFI_UDP6_RECEIVE_DATA
*RxData
172 This function enqueues the received datagram into the instances' receiving queues.
174 @param[in] Udp6Service Pointer to the udp service context data.
175 @param[in] Packet Pointer to the buffer containing the received
177 @param[in] RxData Pointer to the EFI_UDP6_RECEIVE_DATA of this
180 @return The times this datagram is enqueued.
185 IN UDP6_SERVICE_DATA
*Udp6Service
,
187 IN EFI_UDP6_RECEIVE_DATA
*RxData
191 This function delivers the datagrams enqueued in the instances.
193 @param[in] Udp6Service Pointer to the udp service context data.
198 IN UDP6_SERVICE_DATA
*Udp6Service
202 This function demultiplexes the received udp datagram to the apropriate instances.
204 @param[in] Udp6Service Pointer to the udp service context data.
205 @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA abstrated from
206 the received datagram.
207 @param[in] Packet Pointer to the buffer containing the received udp
213 IN UDP6_SERVICE_DATA
*Udp6Service
,
214 IN EFI_NET_SESSION_DATA
*NetSession
,
219 This function handles the received Icmp Error message and demultiplexes it to the
222 @param[in] Udp6Service Pointer to the udp service context data.
223 @param[in] IcmpError The icmp error code.
224 @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA abstracted
225 from the received Icmp Error packet.
226 @param[in, out] Packet Pointer to the Icmp Error packet.
231 IN UDP6_SERVICE_DATA
*Udp6Service
,
233 IN EFI_NET_SESSION_DATA
*NetSession
,
234 IN OUT NET_BUF
*Packet
238 This function builds and sends out a icmp port unreachable message.
240 @param[in] IpIo Pointer to the IP_IO instance.
241 @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA of the packet
242 causes this icmp error message.
243 @param[in] Udp6Header Pointer to the udp header of the datagram causes
244 this icmp error message.
248 Udp6SendPortUnreach (
250 IN EFI_NET_SESSION_DATA
*NetSession
,
255 Find the key in the netmap
257 @param[in] Map The netmap to search within.
258 @param[in] Key The key to search.
260 @return The point to the item contains the Key, or NULL if Key isn't in the map.
264 Udp6MapMultiCastAddr (
270 Create the Udp service context data.
272 @param[in] Udp6Service Pointer to the UDP6_SERVICE_DATA.
273 @param[in] ImageHandle The image handle of this udp6 driver.
274 @param[in] ControllerHandle The controller handle this udp6 driver binds on.
276 @retval EFI_SUCCESS The udp6 service context data was created and
278 @retval EFI_OUT_OF_RESOURCES Cannot allocate memory.
279 @retval Others An error condition occurred.
284 IN UDP6_SERVICE_DATA
*Udp6Service
,
285 IN EFI_HANDLE ImageHandle
,
286 IN EFI_HANDLE ControllerHandle
290 IP_IO_OPEN_DATA OpenData
;
292 ZeroMem (Udp6Service
, sizeof (UDP6_SERVICE_DATA
));
294 Udp6Service
->Signature
= UDP6_SERVICE_DATA_SIGNATURE
;
295 Udp6Service
->ServiceBinding
= mUdp6ServiceBinding
;
296 Udp6Service
->ImageHandle
= ImageHandle
;
297 Udp6Service
->ControllerHandle
= ControllerHandle
;
298 Udp6Service
->ChildrenNumber
= 0;
300 InitializeListHead (&Udp6Service
->ChildrenList
);
303 // Create the IpIo for this service context.
305 Udp6Service
->IpIo
= IpIoCreate (ImageHandle
, ControllerHandle
, IP_VERSION_6
);
306 if (Udp6Service
->IpIo
== NULL
) {
307 return EFI_OUT_OF_RESOURCES
;
311 // Set the OpenData used to open the IpIo.
314 &OpenData
.IpConfigData
.Ip6CfgData
,
315 &mIp6IoDefaultIpConfigData
,
316 sizeof (EFI_IP6_CONFIG_DATA
)
318 OpenData
.RcvdContext
= (VOID
*) Udp6Service
;
319 OpenData
.SndContext
= NULL
;
320 OpenData
.PktRcvdNotify
= Udp6DgramRcvd
;
321 OpenData
.PktSentNotify
= Udp6DgramSent
;
324 // Configure and start the IpIo.
326 Status
= IpIoOpen (Udp6Service
->IpIo
, &OpenData
);
327 if (EFI_ERROR (Status
)) {
332 // Create the event for Udp timeout checking.
334 Status
= gBS
->CreateEvent (
335 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
339 &Udp6Service
->TimeoutEvent
341 if (EFI_ERROR (Status
)) {
346 // Start the timeout timer event.
348 Status
= gBS
->SetTimer (
349 Udp6Service
->TimeoutEvent
,
351 UDP6_TIMEOUT_INTERVAL
353 if (EFI_ERROR (Status
)) {
361 if (Udp6Service
->TimeoutEvent
!= NULL
) {
362 gBS
->CloseEvent (Udp6Service
->TimeoutEvent
);
365 IpIoDestroy (Udp6Service
->IpIo
);
372 Clean the Udp service context data.
374 @param[in, out] Udp6Service Pointer to the UDP6_SERVICE_DATA.
379 IN OUT UDP6_SERVICE_DATA
*Udp6Service
383 // Close the TimeoutEvent timer.
385 gBS
->CloseEvent (Udp6Service
->TimeoutEvent
);
390 IpIoDestroy (Udp6Service
->IpIo
);
395 This function checks and times out the I/O datagrams listed in the
396 UDP6_SERVICE_DATA which is specified by the input parameter Context.
399 @param[in] Event The event this function registered to.
400 @param[in] Context The context data registered during the creation of
411 UDP6_SERVICE_DATA
*Udp6Service
;
413 UDP6_INSTANCE_DATA
*Instance
;
414 LIST_ENTRY
*WrapEntry
;
415 LIST_ENTRY
*NextEntry
;
416 UDP6_RXDATA_WRAP
*Wrap
;
418 Udp6Service
= (UDP6_SERVICE_DATA
*) Context
;
419 NET_CHECK_SIGNATURE (Udp6Service
, UDP6_SERVICE_DATA_SIGNATURE
);
421 NET_LIST_FOR_EACH (Entry
, &Udp6Service
->ChildrenList
) {
423 // Iterate all the instances belonging to this service context.
425 Instance
= NET_LIST_USER_STRUCT (Entry
, UDP6_INSTANCE_DATA
, Link
);
426 NET_CHECK_SIGNATURE (Instance
, UDP6_INSTANCE_DATA_SIGNATURE
);
428 if (!Instance
->Configured
|| (Instance
->ConfigData
.ReceiveTimeout
== 0)) {
430 // Skip this instance if it's not configured or no receive timeout.
435 NET_LIST_FOR_EACH_SAFE (WrapEntry
, NextEntry
, &Instance
->RcvdDgramQue
) {
437 // Iterate all the rxdatas belonging to this udp instance.
439 Wrap
= NET_LIST_USER_STRUCT (WrapEntry
, UDP6_RXDATA_WRAP
, Link
);
441 if (Wrap
->TimeoutTick
< UDP6_TIMEOUT_INTERVAL
/ 10) {
443 // Remove this RxData if it timeouts.
445 Udp6RecycleRxDataWrap (NULL
, (VOID
*) Wrap
);
447 Wrap
->TimeoutTick
-= UDP6_TIMEOUT_INTERVAL
/ 10;
455 This function intializes the new created udp instance.
457 @param[in] Udp6Service Pointer to the UDP6_SERVICE_DATA.
458 @param[in, out] Instance Pointer to the un-initialized UDP6_INSTANCE_DATA.
463 IN UDP6_SERVICE_DATA
*Udp6Service
,
464 IN OUT UDP6_INSTANCE_DATA
*Instance
468 // Set the signature.
470 Instance
->Signature
= UDP6_INSTANCE_DATA_SIGNATURE
;
475 InitializeListHead (&Instance
->Link
);
476 InitializeListHead (&Instance
->RcvdDgramQue
);
477 InitializeListHead (&Instance
->DeliveredDgramQue
);
480 // Init the NET_MAPs.
482 NetMapInit (&Instance
->TxTokens
);
483 NetMapInit (&Instance
->RxTokens
);
484 NetMapInit (&Instance
->McastIps
);
487 // Save the pointer to the UDP6_SERVICE_DATA, and initialize other members.
489 Instance
->Udp6Service
= Udp6Service
;
490 CopyMem (&Instance
->Udp6Proto
, &mUdp6Protocol
, sizeof (EFI_UDP6_PROTOCOL
));
491 Instance
->IcmpError
= EFI_SUCCESS
;
492 Instance
->Configured
= FALSE
;
493 Instance
->IsNoMapping
= FALSE
;
494 Instance
->Destroyed
= FALSE
;
499 This function cleans the udp instance.
501 @param[in, out] Instance Pointer to the UDP6_INSTANCE_DATA to clean.
506 IN OUT UDP6_INSTANCE_DATA
*Instance
509 NetMapClean (&Instance
->McastIps
);
510 NetMapClean (&Instance
->RxTokens
);
511 NetMapClean (&Instance
->TxTokens
);
516 This function finds the udp instance by the specified <Address, Port> pair.
518 @param[in] InstanceList Pointer to the head of the list linking the udp
520 @param[in] Address Pointer to the specified IPv6 address.
521 @param[in] Port The udp port number.
523 @retval TRUE The specified <Address, Port> pair is found.
524 @retval FALSE Otherwise.
528 Udp6FindInstanceByPort (
529 IN LIST_ENTRY
*InstanceList
,
530 IN EFI_IPv6_ADDRESS
*Address
,
535 UDP6_INSTANCE_DATA
*Instance
;
536 EFI_UDP6_CONFIG_DATA
*ConfigData
;
538 NET_LIST_FOR_EACH (Entry
, InstanceList
) {
540 // Iterate all the udp instances.
542 Instance
= NET_LIST_USER_STRUCT (Entry
, UDP6_INSTANCE_DATA
, Link
);
543 ConfigData
= &Instance
->ConfigData
;
545 if (!Instance
->Configured
|| ConfigData
->AcceptAnyPort
) {
547 // If the instance is not configured, or the configdata of the instance indicates
548 // this instance accepts any port, skip it.
553 if (EFI_IP6_EQUAL (&ConfigData
->StationAddress
, Address
) &&
554 (ConfigData
->StationPort
== Port
)
557 // If both the address and the port are the same, return TRUE.
564 // Return FALSE when matching fails.
571 This function tries to bind the udp instance according to the configured port
574 @param[in] InstanceList Pointer to the head of the list linking the udp
576 @param[in] ConfigData Pointer to the ConfigData of the instance to be
579 @retval EFI_SUCCESS The bound operation completed successfully.
580 @retval EFI_ACCESS_DENIED The <Address, Port> specified by the ConfigData is
581 already used by other instance.
582 @retval EFI_OUT_OF_RESOURCES No available port resources.
587 IN LIST_ENTRY
*InstanceList
,
588 IN EFI_UDP6_CONFIG_DATA
*ConfigData
591 EFI_IPv6_ADDRESS
*StationAddress
;
594 if (ConfigData
->AcceptAnyPort
) {
598 StationAddress
= &ConfigData
->StationAddress
;
600 if (ConfigData
->StationPort
!= 0) {
602 if (!ConfigData
->AllowDuplicatePort
&&
603 Udp6FindInstanceByPort (InstanceList
, StationAddress
, ConfigData
->StationPort
)
606 // Do not allow duplicate ports and the port is already used by other instance.
608 return EFI_ACCESS_DENIED
;
612 // Select a random port for this instance.
614 if (ConfigData
->AllowDuplicatePort
) {
616 // Just pick up the random port if the instance allows duplicate port.
618 ConfigData
->StationPort
= mUdp6RandomPort
;
621 StartPort
= mUdp6RandomPort
;
623 while (Udp6FindInstanceByPort (InstanceList
, StationAddress
, mUdp6RandomPort
)) {
626 if (mUdp6RandomPort
== 0) {
627 mUdp6RandomPort
= UDP6_PORT_KNOWN
;
630 if (mUdp6RandomPort
== StartPort
) {
632 // No available port.
634 return EFI_OUT_OF_RESOURCES
;
638 ConfigData
->StationPort
= mUdp6RandomPort
;
642 if (mUdp6RandomPort
== 0) {
643 mUdp6RandomPort
= UDP6_PORT_KNOWN
;
651 This function is used to check whether the NewConfigData has any un-reconfigurable
652 parameters changed compared to the OldConfigData.
654 @param[in] OldConfigData Pointer to the current ConfigData the udp instance
656 @param[in] NewConfigData Pointer to the new ConfigData.
658 @retval TRUE The instance is reconfigurable according to the NewConfigData.
659 @retval FALSE Otherwise.
663 Udp6IsReconfigurable (
664 IN EFI_UDP6_CONFIG_DATA
*OldConfigData
,
665 IN EFI_UDP6_CONFIG_DATA
*NewConfigData
668 if ((NewConfigData
->AcceptAnyPort
!= OldConfigData
->AcceptAnyPort
) ||
669 (NewConfigData
->AcceptPromiscuous
!= OldConfigData
->AcceptPromiscuous
) ||
670 (NewConfigData
->AllowDuplicatePort
!= OldConfigData
->AllowDuplicatePort
)
673 // The receiving filter parameters cannot be changed.
678 if ((!NewConfigData
->AcceptAnyPort
) &&
679 (NewConfigData
->StationPort
!= OldConfigData
->StationPort
)
682 // The port is not changeable.
687 if (!EFI_IP6_EQUAL (&NewConfigData
->StationAddress
, &OldConfigData
->StationAddress
)) {
689 // The StationAddress is not the same.
695 if (!EFI_IP6_EQUAL (&NewConfigData
->RemoteAddress
, &OldConfigData
->RemoteAddress
)) {
697 // The remoteaddress is not the same.
702 if (!NetIp6IsUnspecifiedAddr (&NewConfigData
->RemoteAddress
) &&
703 (NewConfigData
->RemotePort
!= OldConfigData
->RemotePort
)
706 // The RemotePort differs if it's designated in the configdata.
712 // All checks pass, return TRUE.
719 This function builds the Ip6 configdata from the Udp6ConfigData.
721 @param[in] Udp6ConfigData Pointer to the EFI_UDP6_CONFIG_DATA.
722 @param[in, out] Ip6ConfigData Pointer to the EFI_IP6_CONFIG_DATA.
726 Udp6BuildIp6ConfigData (
727 IN EFI_UDP6_CONFIG_DATA
*Udp6ConfigData
,
728 IN OUT EFI_IP6_CONFIG_DATA
*Ip6ConfigData
733 &mIp6IoDefaultIpConfigData
,
734 sizeof (EFI_IP6_CONFIG_DATA
)
736 Ip6ConfigData
->DefaultProtocol
= EFI_IP_PROTO_UDP
;
737 Ip6ConfigData
->AcceptPromiscuous
= Udp6ConfigData
->AcceptPromiscuous
;
738 IP6_COPY_ADDRESS (&Ip6ConfigData
->StationAddress
, &Udp6ConfigData
->StationAddress
);
739 IP6_COPY_ADDRESS (&Ip6ConfigData
->DestinationAddress
, &Udp6ConfigData
->RemoteAddress
);
741 // Use the -1 magic number to disable the receiving process of the ip instance.
743 Ip6ConfigData
->ReceiveTimeout
= (UINT32
) (-1);
748 This function validates the TxToken. It returns the error code according to the spec.
750 @param[in] Instance Pointer to the udp instance context data.
751 @param[in] TxToken Pointer to the token to be checked.
753 @retval EFI_SUCCESS The TxToken is valid.
754 @retval EFI_INVALID_PARAMETER One or more of the following are TRUE:
756 Token.Packet.TxData is NULL;
757 Token.Packet.TxData.FragmentCount is zero;
758 Token.Packet.TxData.DataLength is not equal to the
759 sum of fragment lengths;
761 Token.Packet.TxData.FragmentTable[].FragmentLength
764 Token.Packet.TxData.FragmentTable[].FragmentBuffer
766 UdpSessionData.DestinationAddress are not valid
767 unicast IPv6 addresses if the UdpSessionData is
769 UdpSessionData.DestinationPort and
770 ConfigData.RemotePort are all zero if the
771 UdpSessionData is not NULL.
772 @retval EFI_BAD_BUFFER_SIZE The data length is greater than the maximum UDP
777 Udp6ValidateTxToken (
778 IN UDP6_INSTANCE_DATA
*Instance
,
779 IN EFI_UDP6_COMPLETION_TOKEN
*TxToken
782 EFI_UDP6_TRANSMIT_DATA
*TxData
;
785 EFI_UDP6_CONFIG_DATA
*ConfigData
;
786 EFI_UDP6_SESSION_DATA
*UdpSessionData
;
789 if (TxToken
->Event
== NULL
) {
790 return EFI_INVALID_PARAMETER
;
793 TxData
= TxToken
->Packet
.TxData
;
795 if ((TxData
== NULL
) || (TxData
->FragmentCount
== 0)) {
796 return EFI_INVALID_PARAMETER
;
800 for (Index
= 0; Index
< TxData
->FragmentCount
; Index
++) {
802 if ((TxData
->FragmentTable
[Index
].FragmentBuffer
== NULL
) ||
803 (TxData
->FragmentTable
[Index
].FragmentLength
== 0)
806 // If the FragmentBuffer is NULL, or the FragmentLeng is zero.
808 return EFI_INVALID_PARAMETER
;
811 TotalLen
+= TxData
->FragmentTable
[Index
].FragmentLength
;
814 if (TotalLen
!= TxData
->DataLength
) {
816 // The TotalLen calculated by adding all the FragmentLeng doesn't equal to the
819 return EFI_INVALID_PARAMETER
;
822 ConfigData
= &Instance
->ConfigData
;
823 UdpSessionData
= TxData
->UdpSessionData
;
825 if (UdpSessionData
!= NULL
) {
827 if ((UdpSessionData
->DestinationPort
== 0) && (ConfigData
->RemotePort
== 0)) {
829 // Ambiguous; no avalaible DestinationPort for this token.
831 return EFI_INVALID_PARAMETER
;
834 if (NetIp6IsUnspecifiedAddr (&UdpSessionData
->DestinationAddress
) &&
835 NetIp6IsUnspecifiedAddr (&ConfigData
->RemoteAddress
)
838 // The DestinationAddress is not specificed.
840 return EFI_INVALID_PARAMETER
;
843 if (!NetIp6IsUnspecifiedAddr (&UdpSessionData
->DestinationAddress
) &&
844 !NetIp6IsUnspecifiedAddr (&ConfigData
->RemoteAddress
)
847 // The ConfigData.RemoteAddress is not zero and the UdpSessionData.DestinationAddress
850 return EFI_INVALID_PARAMETER
;
852 } else if (NetIp6IsUnspecifiedAddr (&ConfigData
->RemoteAddress
)) {
854 // The configured RemoteAddress is all zero, and the user doesn't override the
855 // destination address.
857 return EFI_INVALID_PARAMETER
;
860 if (TxData
->DataLength
> UDP6_MAX_DATA_SIZE
) {
861 return EFI_BAD_BUFFER_SIZE
;
869 This function checks whether the specified Token duplicates the one in the Map.
871 @param[in] Map Pointer to the NET_MAP.
872 @param[in] Item Pointer to the NET_MAP_ITEM contain the pointer to
874 @param[in] Context Pointer to the Token to be checked.
876 @retval EFI_SUCCESS The Token specified by Context differs from the
878 @retval EFI_ACCESS_DENIED The Token duplicates with the one in the Item.
885 IN NET_MAP_ITEM
*Item
,
889 EFI_UDP6_COMPLETION_TOKEN
*Token
;
890 EFI_UDP6_COMPLETION_TOKEN
*TokenInItem
;
892 Token
= (EFI_UDP6_COMPLETION_TOKEN
*) Context
;
893 TokenInItem
= (EFI_UDP6_COMPLETION_TOKEN
*) Item
->Key
;
895 if ((Token
== TokenInItem
) || (Token
->Event
== TokenInItem
->Event
)) {
897 // The Token duplicates with the TokenInItem in case either the two pointers are the
898 // same, or the Events of these two tokens are the same.
900 return EFI_ACCESS_DENIED
;
908 This function calculates the checksum for the Packet, utilizing the pre-calculated
909 pseudo HeadSum to reduce some overhead.
911 @param[in] Packet Pointer to the NET_BUF contains the udp datagram.
912 @param[in] HeadSum Checksum of the pseudo header, execpt the length
915 @return The 16-bit checksum of this udp datagram.
926 Checksum
= NetbufChecksum (Packet
);
927 Checksum
= NetAddChecksum (Checksum
, HeadSum
);
929 Checksum
= NetAddChecksum (Checksum
, HTONS ((UINT16
) Packet
->TotalSize
));
930 Checksum
= (UINT16
) (~Checksum
);
936 This function removes the specified Token from the TokenMap.
938 @param[in] TokenMap Pointer to the NET_MAP containing the tokens.
939 @param[in] Token Pointer to the Token to be removed.
941 @retval EFI_SUCCESS The specified Token is removed from the TokenMap.
942 @retval EFI_NOT_FOUND The specified Token is not found in the TokenMap.
947 IN NET_MAP
*TokenMap
,
948 IN EFI_UDP6_COMPLETION_TOKEN
*Token
954 // Find the Token first.
956 Item
= NetMapFindKey (TokenMap
, (VOID
*) Token
);
960 // Remove the token if it's found in the map.
962 NetMapRemoveItem (TokenMap
, Item
, NULL
);
966 return EFI_NOT_FOUND
;
971 This function is the packet transmitting notify function registered to the IpIo
972 interface. It's called to signal the udp TxToken when IpIo layer completes the
973 transmitting of the udp datagram.
975 @param[in] Status The completion status of the output udp datagram.
976 @param[in] Context Pointer to the context data.
977 @param[in] Sender Specify a EFI_IP6_PROTOCOL for sending.
978 @param[in] NotifyData Pointer to the notify data.
984 IN EFI_STATUS Status
,
986 IN IP_IO_IP_PROTOCOL Sender
,
990 UDP6_INSTANCE_DATA
*Instance
;
991 EFI_UDP6_COMPLETION_TOKEN
*Token
;
993 Instance
= (UDP6_INSTANCE_DATA
*) Context
;
994 Token
= (EFI_UDP6_COMPLETION_TOKEN
*) NotifyData
;
996 if (Udp6RemoveToken (&Instance
->TxTokens
, Token
) == EFI_SUCCESS
) {
998 // The token may be cancelled. Only signal it if the remove operation succeeds.
1000 Token
->Status
= Status
;
1001 gBS
->SignalEvent (Token
->Event
);
1008 This function processes the received datagram passed up by the IpIo layer.
1010 @param[in] Status The status of this udp datagram.
1011 @param[in] IcmpError The IcmpError code, only available when Status is
1013 @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA.
1014 @param[in] Packet Pointer to the NET_BUF containing the received udp
1016 @param[in] Context Pointer to the context data.
1022 IN EFI_STATUS Status
,
1024 IN EFI_NET_SESSION_DATA
*NetSession
,
1029 NET_CHECK_SIGNATURE (Packet
, NET_BUF_SIGNATURE
);
1032 // IpIo only passes received packets with Status EFI_SUCCESS or EFI_ICMP_ERROR.
1034 if (Status
== EFI_SUCCESS
) {
1037 // Demultiplex the received datagram.
1039 Udp6Demultiplex ((UDP6_SERVICE_DATA
*) Context
, NetSession
, Packet
);
1042 // Handle the ICMP6 Error packet.
1044 Udp6IcmpHandler ((UDP6_SERVICE_DATA
*) Context
, IcmpError
, NetSession
, Packet
);
1048 // Dispatch the DPC queued by the NotifyFunction of the rx token's events
1049 // that are signaled with received data.
1056 This function removes the multicast group specified by Arg from the Map.
1058 @param[in] Map Pointer to the NET_MAP.
1059 @param[in] Item Pointer to the NET_MAP_ITEM.
1060 @param[in] Arg Pointer to the Arg, it's the pointer to a
1061 multicast IPv6 Address. This parameter is
1062 optional and may be NULL.
1064 @retval EFI_SUCCESS The multicast address is removed.
1065 @retval EFI_ABORTED The specified multicast address is removed, and the
1073 IN NET_MAP_ITEM
*Item
,
1074 IN VOID
*Arg OPTIONAL
1077 EFI_IPv6_ADDRESS
*McastIp
;
1081 if ((McastIp
!= NULL
) &&
1082 !EFI_IP6_EQUAL (McastIp
, ((EFI_IPv6_ADDRESS
*)Item
->Key
))
1085 // McastIp is not NULL and the multicast address contained in the Item
1086 // is not the same as McastIp.
1091 FreePool (Item
->Key
);
1094 // Remove this Item.
1096 NetMapRemoveItem (Map
, Item
, NULL
);
1098 if (McastIp
!= NULL
) {
1100 // Return EFI_ABORTED in case McastIp is not NULL to terminate the iteration.
1110 This function cancle the token specified by Arg in the Map.
1112 @param[in] Map Pointer to the NET_MAP.
1113 @param[in] Item Pointer to the NET_MAP_ITEM.
1114 @param[in] Arg Pointer to the token to be cancelled. If NULL, all
1115 the tokens in this Map will be cancelled.
1116 This parameter is optional and may be NULL.
1118 @retval EFI_SUCCESS The token is cancelled if Arg is NULL, or the token
1119 is not the same as that in the Item, if Arg is not
1121 @retval EFI_ABORTED Arg is not NULL, and the token specified by Arg is
1129 IN NET_MAP_ITEM
*Item
,
1130 IN VOID
*Arg OPTIONAL
1133 EFI_UDP6_COMPLETION_TOKEN
*TokenToCancel
;
1137 if ((Arg
!= NULL
) && (Item
->Key
!= Arg
)) {
1141 if (Item
->Value
!= NULL
) {
1143 // If the token is a transmit token, the corresponding Packet is recorded in
1144 // Item->Value, invoke IpIo to cancel this packet first. The IpIoCancelTxToken
1145 // will invoke Udp6DgramSent, the token will be signaled and this Item will
1146 // be removed from the Map there.
1148 Packet
= (NET_BUF
*) (Item
->Value
);
1149 IpIo
= (IP_IO
*) (*((UINTN
*) &Packet
->ProtoData
[0]));
1151 IpIoCancelTxToken (IpIo
, Packet
);
1154 // The token is a receive token. Abort it and remove it from the Map.
1156 TokenToCancel
= (EFI_UDP6_COMPLETION_TOKEN
*) Item
->Key
;
1157 NetMapRemoveItem (Map
, Item
, NULL
);
1159 TokenToCancel
->Status
= EFI_ABORTED
;
1160 gBS
->SignalEvent (TokenToCancel
->Event
);
1172 This function removes all the Wrap datas in the RcvdDgramQue.
1174 @param[in] Instance Pointer to the Udp6 Instance.
1178 Udp6FlushRcvdDgram (
1179 IN UDP6_INSTANCE_DATA
*Instance
1182 UDP6_RXDATA_WRAP
*Wrap
;
1184 while (!IsListEmpty (&Instance
->RcvdDgramQue
)) {
1186 // Iterate all the Wraps in the RcvdDgramQue.
1188 Wrap
= NET_LIST_HEAD (&Instance
->RcvdDgramQue
, UDP6_RXDATA_WRAP
, Link
);
1191 // The Wrap will be removed from the RcvdDgramQue by this function call.
1193 Udp6RecycleRxDataWrap (NULL
, (VOID
*) Wrap
);
1200 Cancel Udp6 tokens from the Udp6 instance.
1202 @param[in] Instance Pointer to the udp instance context data.
1203 @param[in] Token Pointer to the token to be canceled. If NULL, all
1204 tokens in this instance will be cancelled.
1205 This parameter is optional and may be NULL.
1207 @retval EFI_SUCCESS The Token is cancelled.
1208 @retval EFI_NOT_FOUND The Token is not found.
1212 Udp6InstanceCancelToken (
1213 IN UDP6_INSTANCE_DATA
*Instance
,
1214 IN EFI_UDP6_COMPLETION_TOKEN
*Token OPTIONAL
1220 // Cancel this token from the TxTokens map.
1222 Status
= NetMapIterate (&Instance
->TxTokens
, Udp6CancelTokens
, Token
);
1224 if ((Token
!= NULL
) && (Status
== EFI_ABORTED
)) {
1226 // If Token isn't NULL and Status is EFI_ABORTED, the token is cancelled from
1227 // the TxTokens and returns success.
1233 // Try to cancel this token from the RxTokens map in condition either the Token
1234 // is NULL or the specified Token is not in TxTokens.
1236 Status
= NetMapIterate (&Instance
->RxTokens
, Udp6CancelTokens
, Token
);
1238 if ((Token
!= NULL
) && (Status
== EFI_SUCCESS
)) {
1240 // If Token isn't NULL and Status is EFI_SUCCESS, the token is neither in the
1241 // TxTokens nor the RxTokens, or say, it's not found.
1243 return EFI_NOT_FOUND
;
1246 ASSERT ((Token
!= NULL
) ||
1247 ((0 == NetMapGetCount (&Instance
->TxTokens
)) &&
1248 (0 == NetMapGetCount (&Instance
->RxTokens
)))
1256 This function checks if the received udp datagram matches with the Instance.
1258 @param[in] Instance Pointer to the udp instance context data.
1259 @param[in] Udp6Session Pointer to the EFI_UDP6_SESSION_DATA abstracted
1260 from the received udp datagram.
1262 @retval TRUE The udp datagram matches the receiving requirements of the Instance.
1263 @retval FALSE The udp datagram does not matche the receiving requirements of the Instance.
1268 IN UDP6_INSTANCE_DATA
*Instance
,
1269 IN EFI_UDP6_SESSION_DATA
*Udp6Session
1272 EFI_UDP6_CONFIG_DATA
*ConfigData
;
1273 EFI_IPv6_ADDRESS Destination
;
1275 ConfigData
= &Instance
->ConfigData
;
1277 if (ConfigData
->AcceptPromiscuous
) {
1279 // Always matches if this instance is in the promiscuous state.
1284 if ((!ConfigData
->AcceptAnyPort
&& (Udp6Session
->DestinationPort
!= ConfigData
->StationPort
)) ||
1285 ((ConfigData
->RemotePort
!= 0) && (Udp6Session
->SourcePort
!= ConfigData
->RemotePort
))
1288 // The local port or the remote port doesn't match.
1293 if (!NetIp6IsUnspecifiedAddr (&ConfigData
->RemoteAddress
) &&
1294 !EFI_IP6_EQUAL (&ConfigData
->RemoteAddress
, &Udp6Session
->SourceAddress
)
1297 // This datagram doesn't come from the instance's specified sender.
1302 if (NetIp6IsUnspecifiedAddr (&ConfigData
->StationAddress
) ||
1303 EFI_IP6_EQUAL (&Udp6Session
->DestinationAddress
, &ConfigData
->StationAddress
)
1306 // The instance is configured to receive datagrams destinated to any station IP or
1307 // the destination address of this datagram matches the configured station IP.
1312 IP6_COPY_ADDRESS (&Destination
, &Udp6Session
->DestinationAddress
);
1314 if (IP6_IS_MULTICAST (&Destination
) &&
1315 (NULL
!= Udp6MapMultiCastAddr (&Instance
->McastIps
, &Destination
))
1318 // It's a multicast packet and the multicast address is accepted by this instance.
1328 This function removes the Wrap specified by Context and release relevant resources.
1330 @param[in] Event The Event this notify function registered to.
1331 @param[in] Context Pointer to the context data.
1336 Udp6RecycleRxDataWrap (
1341 UDP6_RXDATA_WRAP
*Wrap
;
1343 Wrap
= (UDP6_RXDATA_WRAP
*) Context
;
1346 // Remove the Wrap from the list it belongs to.
1348 RemoveEntryList (&Wrap
->Link
);
1351 // Free the Packet associated with this Wrap.
1353 NetbufFree (Wrap
->Packet
);
1358 gBS
->CloseEvent (Wrap
->RxData
.RecycleSignal
);
1365 This function wraps the Packet into RxData.
1367 @param[in] Instance Pointer to the instance context data.
1368 @param[in] Packet Pointer to the buffer containing the received
1370 @param[in] RxData Pointer to the EFI_UDP6_RECEIVE_DATA of this
1373 @return Pointer to the structure wrapping the RxData and the Packet.
1378 IN UDP6_INSTANCE_DATA
*Instance
,
1380 IN EFI_UDP6_RECEIVE_DATA
*RxData
1384 UDP6_RXDATA_WRAP
*Wrap
;
1387 // Allocate buffer for the Wrap.
1389 Wrap
= AllocateZeroPool (sizeof (UDP6_RXDATA_WRAP
) +
1390 (Packet
->BlockOpNum
- 1) * sizeof (EFI_UDP6_FRAGMENT_DATA
));
1395 InitializeListHead (&Wrap
->Link
);
1397 CopyMem (&Wrap
->RxData
, RxData
, sizeof(EFI_UDP6_RECEIVE_DATA
));
1399 // Create the Recycle event.
1401 Status
= gBS
->CreateEvent (
1404 Udp6RecycleRxDataWrap
,
1406 &Wrap
->RxData
.RecycleSignal
1408 if (EFI_ERROR (Status
)) {
1413 Wrap
->Packet
= Packet
;
1414 Wrap
->TimeoutTick
= Instance
->ConfigData
.ReceiveTimeout
;
1421 This function enqueues the received datagram into the instances' receiving queues.
1423 @param[in] Udp6Service Pointer to the udp service context data.
1424 @param[in] Packet Pointer to the buffer containing the received
1426 @param[in] RxData Pointer to the EFI_UDP6_RECEIVE_DATA of this
1429 @return The times this datagram is enqueued.
1434 IN UDP6_SERVICE_DATA
*Udp6Service
,
1436 IN EFI_UDP6_RECEIVE_DATA
*RxData
1440 UDP6_INSTANCE_DATA
*Instance
;
1441 UDP6_RXDATA_WRAP
*Wrap
;
1446 NET_LIST_FOR_EACH (Entry
, &Udp6Service
->ChildrenList
) {
1448 // Iterate the instances.
1450 Instance
= NET_LIST_USER_STRUCT (Entry
, UDP6_INSTANCE_DATA
, Link
);
1452 if (!Instance
->Configured
) {
1456 if (Udp6MatchDgram (Instance
, &RxData
->UdpSession
)) {
1458 // Wrap the RxData and put this Wrap into the instances RcvdDgramQue.
1460 Wrap
= Udp6WrapRxData (Instance
, Packet
, RxData
);
1465 NET_GET_REF (Packet
);
1467 InsertTailList (&Instance
->RcvdDgramQue
, &Wrap
->Link
);
1478 This function delivers the received datagrams to the specified instance.
1480 @param[in] Instance Pointer to the instance context data.
1484 Udp6InstanceDeliverDgram (
1485 IN UDP6_INSTANCE_DATA
*Instance
1488 UDP6_RXDATA_WRAP
*Wrap
;
1489 EFI_UDP6_COMPLETION_TOKEN
*Token
;
1491 EFI_UDP6_RECEIVE_DATA
*RxData
;
1494 if (!IsListEmpty (&Instance
->RcvdDgramQue
) &&
1495 !NetMapIsEmpty (&Instance
->RxTokens
)
1498 Wrap
= NET_LIST_HEAD (&Instance
->RcvdDgramQue
, UDP6_RXDATA_WRAP
, Link
);
1500 if (NET_BUF_SHARED (Wrap
->Packet
)) {
1502 // Duplicate the Packet if it is shared between instances.
1504 Dup
= NetbufDuplicate (Wrap
->Packet
, NULL
, 0);
1509 NetbufFree (Wrap
->Packet
);
1514 NetListRemoveHead (&Instance
->RcvdDgramQue
);
1516 Token
= (EFI_UDP6_COMPLETION_TOKEN
*) NetMapRemoveHead (&Instance
->RxTokens
, NULL
);
1519 // Build the FragmentTable and set the FragmentCount in RxData.
1521 RxData
= &Wrap
->RxData
;
1522 RxData
->FragmentCount
= Wrap
->Packet
->BlockOpNum
;
1526 (NET_FRAGMENT
*) RxData
->FragmentTable
,
1527 &RxData
->FragmentCount
1530 Token
->Status
= EFI_SUCCESS
;
1531 Token
->Packet
.RxData
= &Wrap
->RxData
;
1533 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1534 InsertTailList (&Instance
->DeliveredDgramQue
, &Wrap
->Link
);
1535 gBS
->RestoreTPL (OldTpl
);
1537 gBS
->SignalEvent (Token
->Event
);
1543 This function delivers the datagrams enqueued in the instances.
1545 @param[in] Udp6Service Pointer to the udp service context data.
1550 IN UDP6_SERVICE_DATA
*Udp6Service
1554 UDP6_INSTANCE_DATA
*Instance
;
1556 NET_LIST_FOR_EACH (Entry
, &Udp6Service
->ChildrenList
) {
1558 // Iterate the instances.
1560 Instance
= NET_LIST_USER_STRUCT (Entry
, UDP6_INSTANCE_DATA
, Link
);
1562 if (!Instance
->Configured
) {
1567 // Deliver the datagrams of this instance.
1569 Udp6InstanceDeliverDgram (Instance
);
1575 This function demultiplexes the received udp datagram to the appropriate instances.
1577 @param[in] Udp6Service Pointer to the udp service context data.
1578 @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA abstrated from
1579 the received datagram.
1580 @param[in] Packet Pointer to the buffer containing the received udp
1586 IN UDP6_SERVICE_DATA
*Udp6Service
,
1587 IN EFI_NET_SESSION_DATA
*NetSession
,
1591 EFI_UDP_HEADER
*Udp6Header
;
1593 EFI_UDP6_RECEIVE_DATA RxData
;
1594 EFI_UDP6_SESSION_DATA
*Udp6Session
;
1598 // Get the datagram header from the packet buffer.
1600 Udp6Header
= (EFI_UDP_HEADER
*) NetbufGetByte (Packet
, 0, NULL
);
1601 ASSERT (Udp6Header
!= NULL
);
1603 if (Udp6Header
->Checksum
!= 0) {
1605 // check the checksum.
1607 HeadSum
= NetIp6PseudoHeadChecksum (
1608 &NetSession
->Source
.v6
,
1609 &NetSession
->Dest
.v6
,
1614 if (Udp6Checksum (Packet
, HeadSum
) != 0) {
1622 gRT
->GetTime (&RxData
.TimeStamp
, NULL
);
1624 Udp6Session
= &RxData
.UdpSession
;
1625 Udp6Session
->SourcePort
= NTOHS (Udp6Header
->SrcPort
);
1626 Udp6Session
->DestinationPort
= NTOHS (Udp6Header
->DstPort
);
1628 IP6_COPY_ADDRESS (&Udp6Session
->SourceAddress
, &NetSession
->Source
);
1629 IP6_COPY_ADDRESS (&Udp6Session
->DestinationAddress
, &NetSession
->Dest
);
1632 // Trim the UDP header.
1634 NetbufTrim (Packet
, UDP6_HEADER_SIZE
, TRUE
);
1636 RxData
.DataLength
= (UINT32
) Packet
->TotalSize
;
1639 // Try to enqueue this datagram into the instances.
1641 Enqueued
= Udp6EnqueueDgram (Udp6Service
, Packet
, &RxData
);
1643 if (Enqueued
== 0) {
1645 // Send the port unreachable ICMP packet before we free this NET_BUF
1647 Udp6SendPortUnreach (Udp6Service
->IpIo
, NetSession
, Udp6Header
);
1651 // Try to free the packet before deliver it.
1653 NetbufFree (Packet
);
1657 // Deliver the datagram.
1659 Udp6DeliverDgram (Udp6Service
);
1665 This function builds and sends out a icmp port unreachable message.
1667 @param[in] IpIo Pointer to the IP_IO instance.
1668 @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA of the packet
1669 causes this icmp error message.
1670 @param[in] Udp6Header Pointer to the udp header of the datagram causes
1671 this icmp error message.
1675 Udp6SendPortUnreach (
1677 IN EFI_NET_SESSION_DATA
*NetSession
,
1683 IP6_ICMP_ERROR_HEAD
*IcmpErrHdr
;
1685 IP_IO_OVERRIDE Override
;
1686 IP_IO_IP_INFO
*IpSender
;
1687 EFI_IP6_MODE_DATA
*Ip6ModeData
;
1689 EFI_IP6_PROTOCOL
*Ip6Protocol
;
1694 // An ICMPv6 error message MUST NOT be originated as A packet destined to
1695 // 1) an IPv6 multicast address 2) The IPv6 Unspecified Address
1697 if (NetSession
->IpVersion
== IP_VERSION_6
) {
1698 if (NetIp6IsUnspecifiedAddr (&NetSession
->Dest
.v6
) ||
1699 IP6_IS_MULTICAST (&NetSession
->Dest
.v6
)
1706 IpSender
= IpIoFindSender (&IpIo
, NetSession
->IpVersion
, &NetSession
->Dest
);
1709 // Get the Ipv6 Mode Data.
1711 Ip6ModeData
= AllocateZeroPool (sizeof (EFI_IP6_MODE_DATA
));
1712 ASSERT (Ip6ModeData
!= NULL
);
1715 // If not finding the related IpSender use the default IpIo to send out
1716 // the port unreachable ICMP message.
1718 if (IpSender
== NULL
) {
1719 Ip6Protocol
= IpIo
->Ip
.Ip6
;
1721 Ip6Protocol
= IpSender
->Ip
.Ip6
;
1724 Status
= Ip6Protocol
->GetModeData (
1731 if (EFI_ERROR (Status
)) {
1735 // The ICMP6 packet length, includes whole invoking packet and ICMP6 error header.
1737 Len
= NetSession
->IpHdrLen
+
1738 NTOHS(((EFI_UDP_HEADER
*) Udp6Header
)->Length
) +
1739 sizeof (IP6_ICMP_ERROR_HEAD
);
1742 // If the ICMP6 packet length larger than IP MTU, adjust its length to MTU.
1744 if (Ip6ModeData
->MaxPacketSize
< Len
) {
1745 Len
= Ip6ModeData
->MaxPacketSize
;
1749 // Allocate buffer for the icmp error message.
1751 Packet
= NetbufAlloc (Len
);
1752 if (Packet
== NULL
) {
1757 // Allocate space for the IP6_ICMP_ERROR_HEAD.
1759 IcmpErrHdr
= (IP6_ICMP_ERROR_HEAD
*) NetbufAllocSpace (Packet
, Len
, FALSE
);
1760 ASSERT (IcmpErrHdr
!= NULL
);
1763 // Set the required fields for the icmp port unreachable message.
1765 IcmpErrHdr
->Head
.Type
= ICMP_V6_DEST_UNREACHABLE
;
1766 IcmpErrHdr
->Head
.Code
= ICMP_V6_PORT_UNREACHABLE
;
1767 IcmpErrHdr
->Head
.Checksum
= 0;
1768 IcmpErrHdr
->Fourth
= 0;
1771 // Copy as much of invoking Packet as possible without the ICMPv6 packet
1772 // exceeding the minimum Ipv6 MTU. The length of IP6_ICMP_ERROR_HEAD contains
1773 // the length of EFI_IP6_HEADER, so when using the length of IP6_ICMP_ERROR_HEAD
1774 // for pointer movement that fact should be considered.
1776 Ptr
= (VOID
*) &IcmpErrHdr
->Head
;
1777 Ptr
= (UINT8
*) (UINTN
) ((UINTN
) Ptr
+ sizeof (IP6_ICMP_ERROR_HEAD
) - sizeof (EFI_IP6_HEADER
));
1778 CopyMem (Ptr
, NetSession
->IpHdr
.Ip6Hdr
, NetSession
->IpHdrLen
);
1780 Ptr
+ NetSession
->IpHdrLen
,
1782 Len
- NetSession
->IpHdrLen
- sizeof (IP6_ICMP_ERROR_HEAD
) + sizeof (EFI_IP6_HEADER
)
1786 // Set the checksum as zero, and IP6 driver will calcuate it with pseudo header.
1788 IcmpErrHdr
->Head
.Checksum
= 0;
1791 // Fill the override data.
1793 Override
.Ip6OverrideData
.FlowLabel
= 0;
1794 Override
.Ip6OverrideData
.HopLimit
= 255;
1795 Override
.Ip6OverrideData
.Protocol
= IP6_ICMP
;
1798 // Send out this icmp packet.
1800 IpIoSend (IpIo
, Packet
, IpSender
, NULL
, NULL
, &NetSession
->Source
, &Override
);
1802 NetbufFree (Packet
);
1805 if (Ip6ModeData
!= NULL
) {
1806 FreePool (Ip6ModeData
);
1812 This function handles the received Icmp Error message and de-multiplexes it to the
1815 @param[in] Udp6Service Pointer to the udp service context data.
1816 @param[in] IcmpError The icmp error code.
1817 @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA abstracted
1818 from the received Icmp Error packet.
1819 @param[in, out] Packet Pointer to the Icmp Error packet.
1824 IN UDP6_SERVICE_DATA
*Udp6Service
,
1826 IN EFI_NET_SESSION_DATA
*NetSession
,
1827 IN OUT NET_BUF
*Packet
1830 EFI_UDP_HEADER
*Udp6Header
;
1831 EFI_UDP6_SESSION_DATA Udp6Session
;
1833 UDP6_INSTANCE_DATA
*Instance
;
1835 Udp6Header
= (EFI_UDP_HEADER
*) NetbufGetByte (Packet
, 0, NULL
);
1836 ASSERT (Udp6Header
!= NULL
);
1838 IP6_COPY_ADDRESS (&Udp6Session
.SourceAddress
, &NetSession
->Source
);
1839 IP6_COPY_ADDRESS (&Udp6Session
.DestinationAddress
, &NetSession
->Dest
);
1841 Udp6Session
.SourcePort
= NTOHS (Udp6Header
->DstPort
);
1842 Udp6Session
.DestinationPort
= NTOHS (Udp6Header
->SrcPort
);
1844 NET_LIST_FOR_EACH (Entry
, &Udp6Service
->ChildrenList
) {
1846 // Iterate all the instances.
1848 Instance
= NET_LIST_USER_STRUCT (Entry
, UDP6_INSTANCE_DATA
, Link
);
1850 if (!Instance
->Configured
) {
1854 if (Udp6MatchDgram (Instance
, &Udp6Session
)) {
1856 // Translate the Icmp Error code according to the udp spec.
1858 Instance
->IcmpError
= IpIoGetIcmpErrStatus (IcmpError
, IP_VERSION_6
, NULL
, NULL
);
1860 if (IcmpError
> ICMP_ERR_UNREACH_PORT
) {
1861 Instance
->IcmpError
= EFI_ICMP_ERROR
;
1865 // Notify the instance with the received Icmp Error.
1867 Udp6ReportIcmpError (Instance
);
1873 NetbufFree (Packet
);
1878 This function reports the received ICMP error.
1880 @param[in] Instance Pointer to the udp instance context data.
1884 Udp6ReportIcmpError (
1885 IN UDP6_INSTANCE_DATA
*Instance
1888 EFI_UDP6_COMPLETION_TOKEN
*Token
;
1890 if (NetMapIsEmpty (&Instance
->RxTokens
)) {
1892 // There are no receive tokens to deliver the ICMP error.
1897 if (EFI_ERROR (Instance
->IcmpError
)) {
1899 // Try to get a RxToken from the RxTokens map.
1901 Token
= (EFI_UDP6_COMPLETION_TOKEN
*) NetMapRemoveHead (&Instance
->RxTokens
, NULL
);
1903 if (Token
!= NULL
) {
1905 // Report the error through the Token.
1907 Token
->Status
= Instance
->IcmpError
;
1908 gBS
->SignalEvent (Token
->Event
);
1911 // Clear the IcmpError.
1913 Instance
->IcmpError
= EFI_SUCCESS
;
1920 This function is a dummy ext-free function for the NET_BUF created for the output
1923 @param[in] Context Pointer to the context data.
1928 Udp6NetVectorExtFree (
1936 Set the Udp6 variable data.
1938 @param[in] Udp6Service Udp6 service data.
1940 @retval EFI_OUT_OF_RESOURCES There are not enough resources to set the
1942 @retval other Set variable failed.
1946 Udp6SetVariableData (
1947 IN UDP6_SERVICE_DATA
*Udp6Service
1950 UINT32 NumConfiguredInstance
;
1952 UINTN VariableDataSize
;
1953 EFI_UDP6_VARIABLE_DATA
*Udp6VariableData
;
1954 EFI_UDP6_SERVICE_POINT
*Udp6ServicePoint
;
1955 UDP6_INSTANCE_DATA
*Udp6Instance
;
1956 CHAR16
*NewMacString
;
1959 NumConfiguredInstance
= 0;
1962 // Go through the children list to count the configured children.
1964 NET_LIST_FOR_EACH (Entry
, &Udp6Service
->ChildrenList
) {
1965 Udp6Instance
= NET_LIST_USER_STRUCT_S (
1969 UDP6_INSTANCE_DATA_SIGNATURE
1972 if (Udp6Instance
->Configured
) {
1973 NumConfiguredInstance
++;
1978 // Calculate the size of the Udp6VariableData. As there may be no Udp6 child,
1979 // we should add extra buffer for the service points only if the number of configured
1980 // children is more than 1.
1982 VariableDataSize
= sizeof (EFI_UDP6_VARIABLE_DATA
);
1984 if (NumConfiguredInstance
> 1) {
1985 VariableDataSize
+= sizeof (EFI_UDP6_SERVICE_POINT
) * (NumConfiguredInstance
- 1);
1988 Udp6VariableData
= AllocateZeroPool (VariableDataSize
);
1989 if (Udp6VariableData
== NULL
) {
1990 return EFI_OUT_OF_RESOURCES
;
1993 Udp6VariableData
->DriverHandle
= Udp6Service
->ImageHandle
;
1994 Udp6VariableData
->ServiceCount
= NumConfiguredInstance
;
1996 Udp6ServicePoint
= &Udp6VariableData
->Services
[0];
1999 // Go through the children list to fill the configured children's address pairs.
2001 NET_LIST_FOR_EACH (Entry
, &Udp6Service
->ChildrenList
) {
2002 Udp6Instance
= NET_LIST_USER_STRUCT_S (
2006 UDP6_INSTANCE_DATA_SIGNATURE
2009 if (Udp6Instance
->Configured
) {
2010 Udp6ServicePoint
->InstanceHandle
= Udp6Instance
->ChildHandle
;
2011 Udp6ServicePoint
->LocalPort
= Udp6Instance
->ConfigData
.StationPort
;
2012 Udp6ServicePoint
->RemotePort
= Udp6Instance
->ConfigData
.RemotePort
;
2015 &Udp6ServicePoint
->LocalAddress
,
2016 &Udp6Instance
->ConfigData
.StationAddress
2019 &Udp6ServicePoint
->RemoteAddress
,
2020 &Udp6Instance
->ConfigData
.RemoteAddress
2027 // Get the MAC string.
2029 Status
= NetLibGetMacString (
2030 Udp6Service
->ControllerHandle
,
2031 Udp6Service
->ImageHandle
,
2034 if (EFI_ERROR (Status
)) {
2038 if (Udp6Service
->MacString
!= NULL
) {
2040 // The variable is set already, we're going to update it.
2042 if (StrCmp (Udp6Service
->MacString
, NewMacString
) != 0) {
2044 // The MAC address is changed, delete the previous variable first.
2047 Udp6Service
->MacString
,
2048 &gEfiUdp6ServiceBindingProtocolGuid
,
2049 EFI_VARIABLE_BOOTSERVICE_ACCESS
,
2055 FreePool (Udp6Service
->MacString
);
2058 Udp6Service
->MacString
= NewMacString
;
2060 Status
= gRT
->SetVariable (
2061 Udp6Service
->MacString
,
2062 &gEfiUdp6ServiceBindingProtocolGuid
,
2063 EFI_VARIABLE_BOOTSERVICE_ACCESS
,
2065 (VOID
*) Udp6VariableData
2070 FreePool (Udp6VariableData
);
2077 Clear the variable and free the resource.
2079 @param[in, out] Udp6Service Udp6 service data.
2083 Udp6ClearVariableData (
2084 IN OUT UDP6_SERVICE_DATA
*Udp6Service
2087 ASSERT (Udp6Service
->MacString
!= NULL
);
2090 Udp6Service
->MacString
,
2091 &gEfiUdp6ServiceBindingProtocolGuid
,
2092 EFI_VARIABLE_BOOTSERVICE_ACCESS
,
2097 FreePool (Udp6Service
->MacString
);
2098 Udp6Service
->MacString
= NULL
;
2103 Find the key in the netmap.
2105 @param[in] Map The netmap to search within.
2106 @param[in] Key The key to search.
2108 @return The point to the item contains the Key, or NULL, if Key isn't in the map.
2112 Udp6MapMultiCastAddr (
2119 EFI_IPv6_ADDRESS
*Addr
;
2121 ASSERT (Map
!= NULL
);
2122 NET_LIST_FOR_EACH (Entry
, &Map
->Used
) {
2123 Item
= NET_LIST_USER_STRUCT (Entry
, NET_MAP_ITEM
, Link
);
2124 Addr
= (EFI_IPv6_ADDRESS
*) Item
->Key
;
2125 if (EFI_IP6_EQUAL (Addr
, Key
)) {