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>
40 #include <Library/TimerLib.h>
42 #include "Udp4Driver.h"
45 extern EFI_COMPONENT_NAME_PROTOCOL gUdp4ComponentName
;
46 extern EFI_COMPONENT_NAME2_PROTOCOL gUdp4ComponentName2
;
47 extern EFI_SERVICE_BINDING_PROTOCOL mUdp4ServiceBinding
;
48 extern EFI_UDP4_PROTOCOL mUdp4Protocol
;
49 extern UINT16 mUdp4RandomPort
;
51 #define ICMP_ERROR_PACKET_LENGTH 8
53 #define UDP4_TIMEOUT_INTERVAL (10 * TICKS_PER_MS) // 10 milliseconds
55 #define UDP4_HEADER_SIZE sizeof (EFI_UDP4_HEADER)
56 #define UDP4_MAX_DATA_SIZE 65507
58 #define UDP4_PORT_KNOWN 1024
60 #define UDP4_SERVICE_DATA_SIGNATURE SIGNATURE_32('U', 'd', 'p', '4')
62 #define UDP4_SERVICE_DATA_FROM_THIS(a) \
67 UDP4_SERVICE_DATA_SIGNATURE \
70 typedef struct _UDP4_SERVICE_DATA_
{
72 EFI_SERVICE_BINDING_PROTOCOL ServiceBinding
;
73 EFI_HANDLE ImageHandle
;
74 EFI_HANDLE ControllerHandle
;
75 LIST_ENTRY ChildrenList
;
79 EFI_EVENT TimeoutEvent
;
84 #define UDP4_INSTANCE_DATA_SIGNATURE SIGNATURE_32('U', 'd', 'p', 'I')
86 #define UDP4_INSTANCE_DATA_FROM_THIS(a) \
91 UDP4_INSTANCE_DATA_SIGNATURE \
94 typedef struct _UDP4_INSTANCE_DATA_
{
98 UDP4_SERVICE_DATA
*Udp4Service
;
99 EFI_UDP4_PROTOCOL Udp4Proto
;
100 EFI_UDP4_CONFIG_DATA ConfigData
;
101 EFI_HANDLE ChildHandle
;
110 LIST_ENTRY RcvdDgramQue
;
111 LIST_ENTRY DeliveredDgramQue
;
115 EFI_STATUS IcmpError
;
117 IP_IO_IP_INFO
*IpInfo
;
120 } UDP4_INSTANCE_DATA
;
122 typedef struct _UDP4_RXDATA_WRAP_
{
126 EFI_UDP4_RECEIVE_DATA RxData
;
130 Reads the current operational settings.
132 The GetModeData() function copies the current operational settings of this EFI
133 UDPv4 Protocol instance into user-supplied buffers. This function is used
134 optionally to retrieve the operational mode data of underlying networks or
137 @param This Pointer to the EFI_UDP4_PROTOCOL instance.
138 @param Udp4ConfigData Pointer to the buffer to receive the current configuration data.
139 @param Ip4ModeData Pointer to the EFI IPv4 Protocol mode data structure.
140 @param MnpConfigData Pointer to the managed network configuration data structure.
141 @param SnpModeData Pointer to the simple network mode data structure.
143 @retval EFI_SUCCESS The mode data was read.
144 @retval EFI_NOT_STARTED When Udp4ConfigData is queried, no configuration data is
145 available because this instance has not been started.
146 @retval EFI_INVALID_PARAMETER This is NULL.
152 IN EFI_UDP4_PROTOCOL
*This
,
153 OUT EFI_UDP4_CONFIG_DATA
*Udp4ConfigData OPTIONAL
,
154 OUT EFI_IP4_MODE_DATA
*Ip4ModeData OPTIONAL
,
155 OUT EFI_MANAGED_NETWORK_CONFIG_DATA
*MnpConfigData OPTIONAL
,
156 OUT EFI_SIMPLE_NETWORK_MODE
*SnpModeData OPTIONAL
160 Initializes, changes, or resets the operational parameters for this instance of the EFI UDPv4
163 The Configure() function is used to do the following:
164 * Initialize and start this instance of the EFI UDPv4 Protocol.
165 * Change the filtering rules and operational parameters.
166 * Reset this instance of the EFI UDPv4 Protocol.
167 Until these parameters are initialized, no network traffic can be sent or
168 received by this instance. This instance can be also reset by calling Configure()
169 with UdpConfigData set to NULL. Once reset, the receiving queue and transmitting
170 queue are flushed and no traffic is allowed through this instance.
171 With different parameters in UdpConfigData, Configure() can be used to bind
172 this instance to specified port.
174 @param This Pointer to the EFI_UDP4_PROTOCOL instance.
175 @param UdpConfigData Pointer to the buffer to receive the current configuration data.
177 @retval EFI_SUCCESS The configuration settings were set, changed, or reset successfully.
178 @retval EFI_NO_MAPPING When using a default address, configuration (DHCP, BOOTP,
179 RARP, etc.) is not finished yet.
180 @retval EFI_INVALID_PARAMETER One or more following conditions are TRUE:
181 @retval EFI_ALREADY_STARTED The EFI UDPv4 Protocol instance is already started/configured
182 and must be stopped/reset before it can be reconfigured.
183 @retval EFI_ACCESS_DENIED UdpConfigData. AllowDuplicatePort is FALSE
184 and UdpConfigData.StationPort is already used by
186 @retval EFI_OUT_OF_RESOURCES The EFI UDPv4 Protocol driver cannot allocate memory for this
187 EFI UDPv4 Protocol instance.
188 @retval EFI_DEVICE_ERROR An unexpected network or system error occurred and this instance
195 IN EFI_UDP4_PROTOCOL
*This
,
196 IN EFI_UDP4_CONFIG_DATA
*UdpConfigData OPTIONAL
200 Joins and leaves multicast groups.
202 The Groups() function is used to enable and disable the multicast group
203 filtering. If the JoinFlag is FALSE and the MulticastAddress is NULL, then all
204 currently joined groups are left.
206 @param This Pointer to the EFI_UDP4_PROTOCOL instance.
207 @param JoinFlag Set to TRUE to join a multicast group. Set to FALSE to leave one
208 or all multicast groups.
209 @param MulticastAddress Pointer to multicast group address to join or leave.
211 @retval EFI_SUCCESS The operation completed successfully.
212 @retval EFI_NOT_STARTED The EFI UDPv4 Protocol instance has not been started.
213 @retval EFI_NO_MAPPING When using a default address, configuration (DHCP, BOOTP,
214 RARP, etc.) is not finished yet.
215 @retval EFI_OUT_OF_RESOURCES Could not allocate resources to join the group.
216 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
218 - JoinFlag is TRUE and MulticastAddress is NULL.
219 - JoinFlag is TRUE and *MulticastAddress is not
220 a valid multicast address.
221 @retval EFI_ALREADY_STARTED The group address is already in the group table (when
223 @retval EFI_NOT_FOUND The group address is not in the group table (when JoinFlag is
225 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
231 IN EFI_UDP4_PROTOCOL
*This
,
233 IN EFI_IPv4_ADDRESS
*MulticastAddress OPTIONAL
237 Adds and deletes routing table entries.
239 The Routes() function adds a route to or deletes a route from the routing table.
240 Routes are determined by comparing the SubnetAddress with the destination IP
241 address and arithmetically AND-ing it with the SubnetMask. The gateway address
242 must be on the same subnet as the configured station address.
243 The default route is added with SubnetAddress and SubnetMask both set to 0.0.0.0.
244 The default route matches all destination IP addresses that do not match any
246 A zero GatewayAddress is a nonroute. Packets are sent to the destination IP
247 address if it can be found in the Address Resolution Protocol (ARP) cache or
248 on the local subnet. One automatic nonroute entry will be inserted into the
249 routing table for outgoing packets that are addressed to a local subnet
250 (gateway address of 0.0.0.0).
251 Each instance of the EFI UDPv4 Protocol has its own independent routing table.
252 Instances of the EFI UDPv4 Protocol that use the default IP address will also
253 have copies of the routing table provided by the EFI_IP4_CONFIG_PROTOCOL. These
254 copies will be updated automatically whenever the IP driver reconfigures its
255 instances; as a result, the previous modification to these copies will be lost.
257 @param This Pointer to the EFI_UDP4_PROTOCOL instance.
258 @param DeleteRoute Set to TRUE to delete this route from the routing table.
259 Set to FALSE to add this route to the routing table.
260 @param SubnetAddress The destination network address that needs to be routed.
261 @param SubnetMask The subnet mask of SubnetAddress.
262 @param GatewayAddress The gateway IP address for this route.
264 @retval EFI_SUCCESS The operation completed successfully.
265 @retval EFI_NOT_STARTED The EFI UDPv4 Protocol instance has not been started.
266 @retval EFI_NO_MAPPING When using a default address, configuration (DHCP, BOOTP,
267 - RARP, etc.) is not finished yet.
268 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
269 @retval EFI_OUT_OF_RESOURCES Could not add the entry to the routing table.
270 @retval EFI_NOT_FOUND This route is not in the routing table.
271 @retval EFI_ACCESS_DENIED The route is already defined in the routing table.
277 IN EFI_UDP4_PROTOCOL
*This
,
278 IN BOOLEAN DeleteRoute
,
279 IN EFI_IPv4_ADDRESS
*SubnetAddress
,
280 IN EFI_IPv4_ADDRESS
*SubnetMask
,
281 IN EFI_IPv4_ADDRESS
*GatewayAddress
285 Queues outgoing data packets into the transmit queue.
287 The Transmit() function places a sending request to this instance of the EFI
288 UDPv4 Protocol, alongside the transmit data that was filled by the user. Whenever
289 the packet in the token is sent out or some errors occur, the Token.Event will
290 be signaled and Token.Status is updated. Providing a proper notification function
291 and context for the event will enable the user to receive the notification and
294 @param This Pointer to the EFI_UDP4_PROTOCOL instance.
295 @param Token Pointer to the completion token that will be placed into the
298 @retval EFI_SUCCESS The data has been queued for transmission.
299 @retval EFI_NOT_STARTED This EFI UDPv4 Protocol instance has not been started.
300 @retval EFI_NO_MAPPING When using a default address, configuration (DHCP, BOOTP,
301 RARP, etc.) is not finished yet.
302 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
303 @retval EFI_ACCESS_DENIED The transmit completion token with the same
304 Token.Event was already in the transmit queue.
305 @retval EFI_NOT_READY The completion token could not be queued because the
306 transmit queue is full.
307 @retval EFI_OUT_OF_RESOURCES Could not queue the transmit data.
308 @retval EFI_NOT_FOUND There is no route to the destination network or address.
309 @retval EFI_BAD_BUFFER_SIZE The data length is greater than the maximum UDP packet
310 size. Or the length of the IP header + UDP header + data
311 length is greater than MTU if DoNotFragment is TRUE.
317 IN EFI_UDP4_PROTOCOL
*This
,
318 IN EFI_UDP4_COMPLETION_TOKEN
*Token
322 Places an asynchronous receive request into the receiving queue.
324 The Receive() function places a completion token into the receive packet queue.
325 This function is always asynchronous.
326 The caller must fill in the Token.Event field in the completion token, and this
327 field cannot be NULL. When the receive operation completes, the EFI UDPv4 Protocol
328 driver updates the Token.Status and Token.Packet.RxData fields and the Token.Event
329 is signaled. Providing a proper notification function and context for the event
330 will enable the user to receive the notification and receiving status. That
331 notification function is guaranteed to not be re-entered.
333 @param This Pointer to the EFI_UDP4_PROTOCOL instance.
334 @param Token Pointer to a token that is associated with the receive data
337 @retval EFI_SUCCESS The receive completion token was cached.
338 @retval EFI_NOT_STARTED This EFI UDPv4 Protocol instance has not been started.
339 @retval EFI_NO_MAPPING When using a default address, configuration (DHCP, BOOTP, RARP, etc.)
341 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
342 @retval EFI_OUT_OF_RESOURCES The receive completion token could not be queued due to a lack of system
343 resources (usually memory).
344 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
345 @retval EFI_ACCESS_DENIED A receive completion token with the same Token.Event was already in
347 @retval EFI_NOT_READY The receive request could not be queued because the receive queue is full.
353 IN EFI_UDP4_PROTOCOL
*This
,
354 IN EFI_UDP4_COMPLETION_TOKEN
*Token
358 Aborts an asynchronous transmit or receive request.
360 The Cancel() function is used to abort a pending transmit or receive request.
361 If the token is in the transmit or receive request queues, after calling this
362 function, Token.Status will be set to EFI_ABORTED and then Token.Event will be
363 signaled. If the token is not in one of the queues, which usually means that
364 the asynchronous operation has completed, this function will not signal the
365 token and EFI_NOT_FOUND is returned.
367 @param This Pointer to the EFI_UDP4_PROTOCOL instance.
368 @param Token Pointer to a token that has been issued by
369 EFI_UDP4_PROTOCOL.Transmit() or
370 EFI_UDP4_PROTOCOL.Receive().If NULL, all pending
373 @retval EFI_SUCCESS The asynchronous I/O request was aborted and Token.Event
374 was signaled. When Token is NULL, all pending requests are
375 aborted and their events are signaled.
376 @retval EFI_INVALID_PARAMETER This is NULL.
377 @retval EFI_NOT_STARTED This instance has not been started.
378 @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP,
379 RARP, etc.) is not finished yet.
380 @retval EFI_NOT_FOUND When Token is not NULL, the asynchronous I/O request was
381 not found in the transmit or receive queue. It has either completed
382 or was not issued by Transmit() and Receive().
388 IN EFI_UDP4_PROTOCOL
*This
,
389 IN EFI_UDP4_COMPLETION_TOKEN
*Token OPTIONAL
393 Polls for incoming data packets and processes outgoing data packets.
395 The Poll() function can be used by network drivers and applications to increase
396 the rate that data packets are moved between the communications device and the
397 transmit and receive queues.
398 In some systems, the periodic timer event in the managed network driver may not
399 poll the underlying communications device fast enough to transmit and/or receive
400 all data packets without missing incoming packets or dropping outgoing packets.
401 Drivers and applications that are experiencing packet loss should try calling
402 the Poll() function more often.
404 @param This Pointer to the EFI_UDP4_PROTOCOL instance.
406 @retval EFI_SUCCESS Incoming or outgoing data was processed.
407 @retval EFI_INVALID_PARAMETER This is NULL.
408 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
409 @retval EFI_TIMEOUT Data was dropped out of the transmit and/or receive queue.
415 IN EFI_UDP4_PROTOCOL
*This
419 Create the Udp service context data.
421 @param Udp4Service Pointer to the UDP4_SERVICE_DATA.
422 @param ImageHandle The image handle of this udp4 driver.
423 @param ControllerHandle The controller handle this udp4 driver binds on.
425 @retval EFI_SUCCESS The udp4 service context data is created and
427 @retval EFI_OUT_OF_RESOURCES Cannot allocate memory.
428 @retval other Other error occurs.
433 IN OUT UDP4_SERVICE_DATA
*Udp4Service
,
434 IN EFI_HANDLE ImageHandle
,
435 IN EFI_HANDLE ControllerHandle
439 Clean the Udp service context data.
441 @param Udp4Service Pointer to the UDP4_SERVICE_DATA.
448 IN UDP4_SERVICE_DATA
*Udp4Service
452 This function intializes the new created udp instance.
454 @param Udp4Service Pointer to the UDP4_SERVICE_DATA.
455 @param Instance Pointer to the un-initialized UDP4_INSTANCE_DATA.
462 IN UDP4_SERVICE_DATA
*Udp4Service
,
463 IN OUT UDP4_INSTANCE_DATA
*Instance
467 This function cleans the udp instance.
469 @param Instance Pointer to the UDP4_INSTANCE_DATA to clean.
476 IN UDP4_INSTANCE_DATA
*Instance
480 This function tries to bind the udp instance according to the configured port
483 @param InstanceList Pointer to the head of the list linking the udp
485 @param ConfigData Pointer to the ConfigData of the instance to be
486 bound. ConfigData->StationPort will be assigned
487 with an available port value on success.
489 @retval EFI_SUCCESS The bound operation is completed successfully.
490 @retval EFI_ACCESS_DENIED The <Address, Port> specified by the ConfigData is
491 already used by other instance.
492 @retval EFI_OUT_OF_RESOURCES No available port resources.
497 IN LIST_ENTRY
*InstanceList
,
498 IN OUT EFI_UDP4_CONFIG_DATA
*ConfigData
502 This function is used to check whether the NewConfigData has any un-reconfigurable
503 parameters changed compared to the OldConfigData.
505 @param OldConfigData Pointer to the current ConfigData the udp instance
507 @param NewConfigData Pointer to the new ConfigData.
509 @retval TRUE The instance is reconfigurable.
510 @retval FALSE Otherwise.
514 Udp4IsReconfigurable (
515 IN EFI_UDP4_CONFIG_DATA
*OldConfigData
,
516 IN EFI_UDP4_CONFIG_DATA
*NewConfigData
520 This function builds the Ip4 configdata from the Udp4ConfigData.
522 @param Udp4ConfigData Pointer to the EFI_UDP4_CONFIG_DATA.
523 @param Ip4ConfigData Pointer to the EFI_IP4_CONFIG_DATA.
529 Udp4BuildIp4ConfigData (
530 IN EFI_UDP4_CONFIG_DATA
*Udp4ConfigData
,
531 IN OUT EFI_IP4_CONFIG_DATA
*Ip4ConfigData
535 This function validates the TxToken, it returns the error code according to the spec.
537 @param Instance Pointer to the udp instance context data.
538 @param TxToken Pointer to the token to be checked.
540 @retval EFI_SUCCESS The TxToken is valid.
541 @retval EFI_INVALID_PARAMETER One or more of the following are TRUE: This is
542 NULL. Token is NULL. Token.Event is NULL.
543 Token.Packet.TxData is NULL.
544 Token.Packet.TxData.FragmentCount is zero.
545 Token.Packet.TxData.DataLength is not equal to the
546 sum of fragment lengths. One or more of the
547 Token.Packet.TxData.FragmentTable[].
548 FragmentLength fields is zero. One or more of the
549 Token.Packet.TxData.FragmentTable[].
550 FragmentBuffer fields is NULL.
551 Token.Packet.TxData. GatewayAddress is not a
552 unicast IPv4 address if it is not NULL. One or
553 more IPv4 addresses in Token.Packet.TxData.
554 UdpSessionData are not valid unicast IPv4
555 addresses if the UdpSessionData is not NULL.
556 @retval EFI_BAD_BUFFER_SIZE The data length is greater than the maximum UDP
561 Udp4ValidateTxToken (
562 IN UDP4_INSTANCE_DATA
*Instance
,
563 IN EFI_UDP4_COMPLETION_TOKEN
*TxToken
567 This function checks whether the specified Token duplicates with the one in the Map.
569 @param Map Pointer to the NET_MAP.
570 @param Item Pointer to the NET_MAP_ITEM contain the pointer to
572 @param Context Pointer to the Token to be checked.
574 @retval EFI_SUCCESS The Token specified by Context differs from the
576 @retval EFI_ACCESS_DENIED The Token duplicates with the one in the Item.
582 IN NET_MAP_ITEM
*Item
,
587 This function calculates the checksum for the Packet, utilizing the pre-calculated
588 pseudo HeadSum to reduce some overhead.
590 @param Packet Pointer to the NET_BUF contains the udp datagram.
591 @param HeadSum Checksum of the pseudo header execpt the length
594 @return The 16-bit checksum of this udp datagram.
604 This function removes the specified Token from the TokenMap.
606 @param TokenMap Pointer to the NET_MAP containing the tokens.
607 @param Token Pointer to the Token to be removed.
609 @retval EFI_SUCCESS The specified Token is removed from the TokenMap.
610 @retval EFI_NOT_FOUND The specified Token is not found in the TokenMap.
615 IN OUT NET_MAP
*TokenMap
,
616 IN EFI_UDP4_COMPLETION_TOKEN
*Token
620 This function removes the multicast group specified by Arg from the Map.
622 @param Map Pointer to the NET_MAP.
623 @param Item Pointer to the NET_MAP_ITEM.
624 @param Arg Pointer to the Arg, it's the pointer to a
625 multicast IPv4 Address.
627 @retval EFI_SUCCESS The multicast address is removed.
628 @retval EFI_ABORTED The specified multicast address is removed and the
635 IN NET_MAP_ITEM
*Item
,
636 IN VOID
*Arg OPTIONAL
640 This function removes all the Wrap datas in the RcvdDgramQue.
642 @param Instance Pointer to the udp instance context data.
649 IN UDP4_INSTANCE_DATA
*Instance
653 Cancel Udp4 tokens from the Udp4 instance.
655 @param Instance Pointer to the udp instance context data.
656 @param Token Pointer to the token to be canceled, if NULL, all
657 tokens in this instance will be cancelled.
659 @retval EFI_SUCCESS The Token is cancelled.
660 @retval EFI_NOT_FOUND The Token is not found.
664 Udp4InstanceCancelToken (
665 IN UDP4_INSTANCE_DATA
*Instance
,
666 IN EFI_UDP4_COMPLETION_TOKEN
*Token OPTIONAL
670 This function delivers the received datagrams for the specified instance.
672 @param Instance Pointer to the instance context data.
678 Udp4InstanceDeliverDgram (
679 IN UDP4_INSTANCE_DATA
*Instance
683 This function reports the received ICMP error.
685 @param Instance Pointer to the udp instance context data.
691 Udp4ReportIcmpError (
692 IN UDP4_INSTANCE_DATA
*Instance
696 This function is a dummy ext-free function for the NET_BUF created for the output
699 @param Context Pointer to the context data.
705 Udp4NetVectorExtFree (
710 Set the Udp4 variable data.
712 @param Udp4Service Udp4 service data.
714 @retval EFI_OUT_OF_RESOURCES There are not enough resources to set the
716 @retval EFI_SUCCESS Set variable successfully.
717 @retval other Set variable failed.
721 Udp4SetVariableData (
722 IN UDP4_SERVICE_DATA
*Udp4Service
726 Clear the variable and free the resource.
728 @param Udp4Service Udp4 service data.
734 Udp4ClearVariableData (
735 IN UDP4_SERVICE_DATA
*Udp4Service