3 Copyright (c) 2006 - 2007, 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 EFI UDPv4 protocol implementation
28 #include <Protocol/Ip4.h>
29 #include <Protocol/Udp4.h>
31 #include <Library/IpIoLib.h>
32 #include <Library/DebugLib.h>
33 #include <Library/UefiRuntimeServicesTableLib.h>
34 #include <Library/UefiDriverEntryPoint.h>
35 #include <Library/UefiBootServicesTableLib.h>
36 #include <Library/BaseLib.h>
37 #include <Library/UefiLib.h>
38 #include <Library/BaseMemoryLib.h>
39 #include <Library/MemoryAllocationLib.h>
41 #include "Udp4Driver.h"
44 extern EFI_COMPONENT_NAME_PROTOCOL gUdp4ComponentName
;
45 extern EFI_COMPONENT_NAME2_PROTOCOL gUdp4ComponentName2
;
46 extern EFI_SERVICE_BINDING_PROTOCOL mUdp4ServiceBinding
;
47 extern EFI_UDP4_PROTOCOL mUdp4Protocol
;
48 extern UINT16 mUdp4RandomPort
;
50 #define ICMP_ERROR_PACKET_LENGTH 8
52 #define UDP4_TIMEOUT_INTERVAL (50 * TICKS_PER_MS) // 50 milliseconds
54 #define UDP4_HEADER_SIZE sizeof (EFI_UDP4_HEADER)
55 #define UDP4_MAX_DATA_SIZE 65507
57 #define UDP4_PORT_KNOWN 1024
59 #define UDP4_SERVICE_DATA_SIGNATURE EFI_SIGNATURE_32('U', 'd', 'p', '4')
61 #define UDP4_SERVICE_DATA_FROM_THIS(a) \
66 UDP4_SERVICE_DATA_SIGNATURE \
69 typedef struct _UDP4_SERVICE_DATA_
{
71 EFI_SERVICE_BINDING_PROTOCOL ServiceBinding
;
72 EFI_HANDLE ImageHandle
;
73 EFI_HANDLE ControllerHandle
;
74 LIST_ENTRY ChildrenList
;
78 EFI_EVENT TimeoutEvent
;
83 #define UDP4_INSTANCE_DATA_SIGNATURE EFI_SIGNATURE_32('U', 'd', 'p', 'I')
85 #define UDP4_INSTANCE_DATA_FROM_THIS(a) \
90 UDP4_INSTANCE_DATA_SIGNATURE \
93 typedef struct _UDP4_INSTANCE_DATA_
{
97 UDP4_SERVICE_DATA
*Udp4Service
;
98 EFI_UDP4_PROTOCOL Udp4Proto
;
99 EFI_UDP4_CONFIG_DATA ConfigData
;
100 EFI_HANDLE ChildHandle
;
109 LIST_ENTRY RcvdDgramQue
;
110 LIST_ENTRY DeliveredDgramQue
;
114 EFI_STATUS IcmpError
;
116 IP_IO_IP_INFO
*IpInfo
;
119 } UDP4_INSTANCE_DATA
;
121 typedef struct _UDP4_RXDATA_WRAP_
{
125 EFI_UDP4_RECEIVE_DATA RxData
;
129 Reads the current operational settings.
131 The GetModeData() function copies the current operational settings of this EFI
132 UDPv4 Protocol instance into user-supplied buffers. This function is used
133 optionally to retrieve the operational mode data of underlying networks or
136 @param This Pointer to the EFI_UDP4_PROTOCOL instance.
137 @param Udp4ConfigData Pointer to the buffer to receive the current configuration data.
138 @param Ip4ModeData Pointer to the EFI IPv4 Protocol mode data structure.
139 @param MnpConfigData Pointer to the managed network configuration data structure.
140 @param SnpModeData Pointer to the simple network mode data structure.
142 @retval EFI_SUCCESS The mode data was read.
143 @retval EFI_NOT_STARTED When Udp4ConfigData is queried, no configuration data is
144 available because this instance has not been started.
145 @retval EFI_INVALID_PARAMETER This is NULL.
151 IN EFI_UDP4_PROTOCOL
*This
,
152 OUT EFI_UDP4_CONFIG_DATA
*Udp4ConfigData OPTIONAL
,
153 OUT EFI_IP4_MODE_DATA
*Ip4ModeData OPTIONAL
,
154 OUT EFI_MANAGED_NETWORK_CONFIG_DATA
*MnpConfigData OPTIONAL
,
155 OUT EFI_SIMPLE_NETWORK_MODE
*SnpModeData OPTIONAL
159 Initializes, changes, or resets the operational parameters for this instance of the EFI UDPv4
162 The Configure() function is used to do the following:
163 * Initialize and start this instance of the EFI UDPv4 Protocol.
164 * Change the filtering rules and operational parameters.
165 * Reset this instance of the EFI UDPv4 Protocol.
166 Until these parameters are initialized, no network traffic can be sent or
167 received by this instance. This instance can be also reset by calling Configure()
168 with UdpConfigData set to NULL. Once reset, the receiving queue and transmitting
169 queue are flushed and no traffic is allowed through this instance.
170 With different parameters in UdpConfigData, Configure() can be used to bind
171 this instance to specified port.
173 @param This Pointer to the EFI_UDP4_PROTOCOL instance.
174 @param UdpConfigData Pointer to the buffer to receive the current configuration data.
176 @retval EFI_SUCCESS The configuration settings were set, changed, or reset successfully.
177 @retval EFI_NO_MAPPING When using a default address, configuration (DHCP, BOOTP,
178 RARP, etc.) is not finished yet.
179 @retval EFI_INVALID_PARAMETER One or more following conditions are TRUE:
180 @retval EFI_ALREADY_STARTED The EFI UDPv4 Protocol instance is already started/configured
181 and must be stopped/reset before it can be reconfigured.
182 @retval EFI_ACCESS_DENIED UdpConfigData. AllowDuplicatePort is FALSE
183 and UdpConfigData.StationPort is already used by
185 @retval EFI_OUT_OF_RESOURCES The EFI UDPv4 Protocol driver cannot allocate memory for this
186 EFI UDPv4 Protocol instance.
187 @retval EFI_DEVICE_ERROR An unexpected network or system error occurred and this instance
194 IN EFI_UDP4_PROTOCOL
*This
,
195 IN EFI_UDP4_CONFIG_DATA
*UdpConfigData OPTIONAL
199 Joins and leaves multicast groups.
201 The Groups() function is used to enable and disable the multicast group
202 filtering. If the JoinFlag is FALSE and the MulticastAddress is NULL, then all
203 currently joined groups are left.
205 @param This Pointer to the EFI_UDP4_PROTOCOL instance.
206 @param JoinFlag Set to TRUE to join a multicast group. Set to FALSE to leave one
207 or all multicast groups.
208 @param MulticastAddress Pointer to multicast group address to join or leave.
210 @retval EFI_SUCCESS The operation completed successfully.
211 @retval EFI_NOT_STARTED The EFI UDPv4 Protocol instance has not been started.
212 @retval EFI_NO_MAPPING When using a default address, configuration (DHCP, BOOTP,
213 RARP, etc.) is not finished yet.
214 @retval EFI_OUT_OF_RESOURCES Could not allocate resources to join the group.
215 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
217 - JoinFlag is TRUE and MulticastAddress is NULL.
218 - JoinFlag is TRUE and *MulticastAddress is not
219 a valid multicast address.
220 @retval EFI_ALREADY_STARTED The group address is already in the group table (when
222 @retval EFI_NOT_FOUND The group address is not in the group table (when JoinFlag is
224 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
230 IN EFI_UDP4_PROTOCOL
*This
,
232 IN EFI_IPv4_ADDRESS
*MulticastAddress OPTIONAL
236 Adds and deletes routing table entries.
238 The Routes() function adds a route to or deletes a route from the routing table.
239 Routes are determined by comparing the SubnetAddress with the destination IP
240 address and arithmetically AND-ing it with the SubnetMask. The gateway address
241 must be on the same subnet as the configured station address.
242 The default route is added with SubnetAddress and SubnetMask both set to 0.0.0.0.
243 The default route matches all destination IP addresses that do not match any
245 A zero GatewayAddress is a nonroute. Packets are sent to the destination IP
246 address if it can be found in the Address Resolution Protocol (ARP) cache or
247 on the local subnet. One automatic nonroute entry will be inserted into the
248 routing table for outgoing packets that are addressed to a local subnet
249 (gateway address of 0.0.0.0).
250 Each instance of the EFI UDPv4 Protocol has its own independent routing table.
251 Instances of the EFI UDPv4 Protocol that use the default IP address will also
252 have copies of the routing table provided by the EFI_IP4_CONFIG_PROTOCOL. These
253 copies will be updated automatically whenever the IP driver reconfigures its
254 instances; as a result, the previous modification to these copies will be lost.
256 @param This Pointer to the EFI_UDP4_PROTOCOL instance.
257 @param DeleteRoute Set to TRUE to delete this route from the routing table.
258 Set to FALSE to add this route to the routing table.
259 @param SubnetAddress The destination network address that needs to be routed.
260 @param SubnetMask The subnet mask of SubnetAddress.
261 @param GatewayAddress The gateway IP address for this route.
263 @retval EFI_SUCCESS The operation completed successfully.
264 @retval EFI_NOT_STARTED The EFI UDPv4 Protocol instance has not been started.
265 @retval EFI_NO_MAPPING When using a default address, configuration (DHCP, BOOTP,
266 - RARP, etc.) is not finished yet.
267 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
268 @retval EFI_OUT_OF_RESOURCES Could not add the entry to the routing table.
269 @retval EFI_NOT_FOUND This route is not in the routing table.
270 @retval EFI_ACCESS_DENIED The route is already defined in the routing table.
276 IN EFI_UDP4_PROTOCOL
*This
,
277 IN BOOLEAN DeleteRoute
,
278 IN EFI_IPv4_ADDRESS
*SubnetAddress
,
279 IN EFI_IPv4_ADDRESS
*SubnetMask
,
280 IN EFI_IPv4_ADDRESS
*GatewayAddress
284 Queues outgoing data packets into the transmit queue.
286 The Transmit() function places a sending request to this instance of the EFI
287 UDPv4 Protocol, alongside the transmit data that was filled by the user. Whenever
288 the packet in the token is sent out or some errors occur, the Token.Event will
289 be signaled and Token.Status is updated. Providing a proper notification function
290 and context for the event will enable the user to receive the notification and
293 @param This Pointer to the EFI_UDP4_PROTOCOL instance.
294 @param Token Pointer to the completion token that will be placed into the
297 @retval EFI_SUCCESS The data has been queued for transmission.
298 @retval EFI_NOT_STARTED This EFI UDPv4 Protocol instance has not been started.
299 @retval EFI_NO_MAPPING When using a default address, configuration (DHCP, BOOTP,
300 RARP, etc.) is not finished yet.
301 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
302 @retval EFI_ACCESS_DENIED The transmit completion token with the same
303 Token.Event was already in the transmit queue.
304 @retval EFI_NOT_READY The completion token could not be queued because the
305 transmit queue is full.
306 @retval EFI_OUT_OF_RESOURCES Could not queue the transmit data.
307 @retval EFI_NOT_FOUND There is no route to the destination network or address.
308 @retval EFI_BAD_BUFFER_SIZE The data length is greater than the maximum UDP packet
309 size. Or the length of the IP header + UDP header + data
310 length is greater than MTU if DoNotFragment is TRUE.
316 IN EFI_UDP4_PROTOCOL
*This
,
317 IN EFI_UDP4_COMPLETION_TOKEN
*Token
321 Places an asynchronous receive request into the receiving queue.
323 The Receive() function places a completion token into the receive packet queue.
324 This function is always asynchronous.
325 The caller must fill in the Token.Event field in the completion token, and this
326 field cannot be NULL. When the receive operation completes, the EFI UDPv4 Protocol
327 driver updates the Token.Status and Token.Packet.RxData fields and the Token.Event
328 is signaled. Providing a proper notification function and context for the event
329 will enable the user to receive the notification and receiving status. That
330 notification function is guaranteed to not be re-entered.
332 @param This Pointer to the EFI_UDP4_PROTOCOL instance.
333 @param Token Pointer to a token that is associated with the receive data
336 @retval EFI_SUCCESS The receive completion token was cached.
337 @retval EFI_NOT_STARTED This EFI UDPv4 Protocol instance has not been started.
338 @retval EFI_NO_MAPPING When using a default address, configuration (DHCP, BOOTP, RARP, etc.)
340 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
341 @retval EFI_OUT_OF_RESOURCES The receive completion token could not be queued due to a lack of system
342 resources (usually memory).
343 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
344 @retval EFI_ACCESS_DENIED A receive completion token with the same Token.Event was already in
346 @retval EFI_NOT_READY The receive request could not be queued because the receive queue is full.
352 IN EFI_UDP4_PROTOCOL
*This
,
353 IN EFI_UDP4_COMPLETION_TOKEN
*Token
357 Aborts an asynchronous transmit or receive request.
359 The Cancel() function is used to abort a pending transmit or receive request.
360 If the token is in the transmit or receive request queues, after calling this
361 function, Token.Status will be set to EFI_ABORTED and then Token.Event will be
362 signaled. If the token is not in one of the queues, which usually means that
363 the asynchronous operation has completed, this function will not signal the
364 token and EFI_NOT_FOUND is returned.
366 @param This Pointer to the EFI_UDP4_PROTOCOL instance.
367 @param Token Pointer to a token that has been issued by
368 EFI_UDP4_PROTOCOL.Transmit() or
369 EFI_UDP4_PROTOCOL.Receive().If NULL, all pending
372 @retval EFI_SUCCESS The asynchronous I/O request was aborted and Token.Event
373 was signaled. When Token is NULL, all pending requests are
374 aborted and their events are signaled.
375 @retval EFI_INVALID_PARAMETER This is NULL.
376 @retval EFI_NOT_STARTED This instance has not been started.
377 @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP,
378 RARP, etc.) is not finished yet.
379 @retval EFI_NOT_FOUND When Token is not NULL, the asynchronous I/O request was
380 not found in the transmit or receive queue. It has either completed
381 or was not issued by Transmit() and Receive().
387 IN EFI_UDP4_PROTOCOL
*This
,
388 IN EFI_UDP4_COMPLETION_TOKEN
*Token OPTIONAL
392 Polls for incoming data packets and processes outgoing data packets.
394 The Poll() function can be used by network drivers and applications to increase
395 the rate that data packets are moved between the communications device and the
396 transmit and receive queues.
397 In some systems, the periodic timer event in the managed network driver may not
398 poll the underlying communications device fast enough to transmit and/or receive
399 all data packets without missing incoming packets or dropping outgoing packets.
400 Drivers and applications that are experiencing packet loss should try calling
401 the Poll() function more often.
403 @param This Pointer to the EFI_UDP4_PROTOCOL instance.
405 @retval EFI_SUCCESS Incoming or outgoing data was processed.
406 @retval EFI_INVALID_PARAMETER This is NULL.
407 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
408 @retval EFI_TIMEOUT Data was dropped out of the transmit and/or receive queue.
414 IN EFI_UDP4_PROTOCOL
*This
418 Create the Udp service context data.
420 @param Udp4Service Pointer to the UDP4_SERVICE_DATA.
421 @param ImageHandle The image handle of this udp4 driver.
422 @param ControllerHandle The controller handle this udp4 driver binds on.
424 @retval EFI_SUCCESS The udp4 service context data is created and
426 @retval EFI_OUT_OF_RESOURCES Cannot allocate memory.
427 @retval other Other error occurs.
432 IN OUT UDP4_SERVICE_DATA
*Udp4Service
,
433 IN EFI_HANDLE ImageHandle
,
434 IN EFI_HANDLE ControllerHandle
438 Clean the Udp service context data.
440 @param Udp4Service Pointer to the UDP4_SERVICE_DATA.
447 IN UDP4_SERVICE_DATA
*Udp4Service
451 This function intializes the new created udp instance.
453 @param Udp4Service Pointer to the UDP4_SERVICE_DATA.
454 @param Instance Pointer to the un-initialized UDP4_INSTANCE_DATA.
461 IN UDP4_SERVICE_DATA
*Udp4Service
,
462 IN OUT UDP4_INSTANCE_DATA
*Instance
466 This function cleans the udp instance.
468 @param Instance Pointer to the UDP4_INSTANCE_DATA to clean.
475 IN UDP4_INSTANCE_DATA
*Instance
479 This function tries to bind the udp instance according to the configured port
482 @param InstanceList Pointer to the head of the list linking the udp
484 @param ConfigData Pointer to the ConfigData of the instance to be
485 bound. ConfigData->StationPort will be assigned
486 with an available port value on success.
488 @retval EFI_SUCCESS The bound operation is completed successfully.
489 @retval EFI_ACCESS_DENIED The <Address, Port> specified by the ConfigData is
490 already used by other instance.
491 @retval EFI_OUT_OF_RESOURCES No available port resources.
496 IN LIST_ENTRY
*InstanceList
,
497 IN OUT EFI_UDP4_CONFIG_DATA
*ConfigData
501 This function is used to check whether the NewConfigData has any un-reconfigurable
502 parameters changed compared to the OldConfigData.
504 @param OldConfigData Pointer to the current ConfigData the udp instance
506 @param NewConfigData Pointer to the new ConfigData.
508 @retval TRUE The instance is reconfigurable.
509 @retval FALSE Otherwise.
513 Udp4IsReconfigurable (
514 IN EFI_UDP4_CONFIG_DATA
*OldConfigData
,
515 IN EFI_UDP4_CONFIG_DATA
*NewConfigData
519 This function builds the Ip4 configdata from the Udp4ConfigData.
521 @param Udp4ConfigData Pointer to the EFI_UDP4_CONFIG_DATA.
522 @param Ip4ConfigData Pointer to the EFI_IP4_CONFIG_DATA.
528 Udp4BuildIp4ConfigData (
529 IN EFI_UDP4_CONFIG_DATA
*Udp4ConfigData
,
530 IN OUT EFI_IP4_CONFIG_DATA
*Ip4ConfigData
534 This function validates the TxToken, it returns the error code according to the spec.
536 @param Instance Pointer to the udp instance context data.
537 @param TxToken Pointer to the token to be checked.
539 @retval EFI_SUCCESS The TxToken is valid.
540 @retval EFI_INVALID_PARAMETER One or more of the following are TRUE: This is
541 NULL. Token is NULL. Token.Event is NULL.
542 Token.Packet.TxData is NULL.
543 Token.Packet.TxData.FragmentCount is zero.
544 Token.Packet.TxData.DataLength is not equal to the
545 sum of fragment lengths. One or more of the
546 Token.Packet.TxData.FragmentTable[].
547 FragmentLength fields is zero. One or more of the
548 Token.Packet.TxData.FragmentTable[].
549 FragmentBuffer fields is NULL.
550 Token.Packet.TxData. GatewayAddress is not a
551 unicast IPv4 address if it is not NULL. One or
552 more IPv4 addresses in Token.Packet.TxData.
553 UdpSessionData are not valid unicast IPv4
554 addresses if the UdpSessionData is not NULL.
555 @retval EFI_BAD_BUFFER_SIZE The data length is greater than the maximum UDP
560 Udp4ValidateTxToken (
561 IN UDP4_INSTANCE_DATA
*Instance
,
562 IN EFI_UDP4_COMPLETION_TOKEN
*TxToken
566 This function checks whether the specified Token duplicates with the one in the Map.
568 @param Map Pointer to the NET_MAP.
569 @param Item Pointer to the NET_MAP_ITEM contain the pointer to
571 @param Context Pointer to the Token to be checked.
573 @retval EFI_SUCCESS The Token specified by Context differs from the
575 @retval EFI_ACCESS_DENIED The Token duplicates with the one in the Item.
581 IN NET_MAP_ITEM
*Item
,
586 This function calculates the checksum for the Packet, utilizing the pre-calculated
587 pseudo HeadSum to reduce some overhead.
589 @param Packet Pointer to the NET_BUF contains the udp datagram.
590 @param HeadSum Checksum of the pseudo header execpt the length
593 @return The 16-bit checksum of this udp datagram.
603 This function removes the specified Token from the TokenMap.
605 @param TokenMap Pointer to the NET_MAP containing the tokens.
606 @param Token Pointer to the Token to be removed.
608 @retval EFI_SUCCESS The specified Token is removed from the TokenMap.
609 @retval EFI_NOT_FOUND The specified Token is not found in the TokenMap.
614 IN OUT NET_MAP
*TokenMap
,
615 IN EFI_UDP4_COMPLETION_TOKEN
*Token
619 This function removes the multicast group specified by Arg from the Map.
621 @param Map Pointer to the NET_MAP.
622 @param Item Pointer to the NET_MAP_ITEM.
623 @param Arg Pointer to the Arg, it's the pointer to a
624 multicast IPv4 Address.
626 @retval EFI_SUCCESS The multicast address is removed.
627 @retval EFI_ABORTED The specified multicast address is removed and the
634 IN NET_MAP_ITEM
*Item
,
635 IN VOID
*Arg OPTIONAL
639 This function removes all the Wrap datas in the RcvdDgramQue.
641 @param Instance Pointer to the udp instance context data.
648 IN UDP4_INSTANCE_DATA
*Instance
652 Cancel Udp4 tokens from the Udp4 instance.
654 @param Instance Pointer to the udp instance context data.
655 @param Token Pointer to the token to be canceled, if NULL, all
656 tokens in this instance will be cancelled.
658 @retval EFI_SUCCESS The Token is cancelled.
659 @retval EFI_NOT_FOUND The Token is not found.
663 Udp4InstanceCancelToken (
664 IN UDP4_INSTANCE_DATA
*Instance
,
665 IN EFI_UDP4_COMPLETION_TOKEN
*Token OPTIONAL
669 This function delivers the received datagrams for the specified instance.
671 @param Instance Pointer to the instance context data.
677 Udp4InstanceDeliverDgram (
678 IN UDP4_INSTANCE_DATA
*Instance
682 This function reports the received ICMP error.
684 @param Instance Pointer to the udp instance context data.
690 Udp4ReportIcmpError (
691 IN UDP4_INSTANCE_DATA
*Instance
695 This function is a dummy ext-free function for the NET_BUF created for the output
698 @param Context Pointer to the context data.
704 Udp4NetVectorExtFree (
709 Set the Udp4 variable data.
711 @param Udp4Service Udp4 service data.
713 @retval EFI_OUT_OF_RESOURCES There are not enough resources to set the
715 @retval EFI_SUCCESS Set variable successfully.
716 @retval other Set variable failed.
720 Udp4SetVariableData (
721 IN UDP4_SERVICE_DATA
*Udp4Service
725 Clear the variable and free the resource.
727 @param Udp4Service Udp4 service data.
733 Udp4ClearVariableData (
734 IN UDP4_SERVICE_DATA
*Udp4Service