2 EFI UDPv4 protocol implementation.
4 Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
20 #include <Protocol/Ip4.h>
21 #include <Protocol/Udp4.h>
23 #include <Library/IpIoLib.h>
24 #include <Library/DebugLib.h>
25 #include <Library/UefiRuntimeServicesTableLib.h>
26 #include <Library/UefiDriverEntryPoint.h>
27 #include <Library/UefiBootServicesTableLib.h>
28 #include <Library/BaseLib.h>
29 #include <Library/UefiLib.h>
30 #include <Library/BaseMemoryLib.h>
31 #include <Library/MemoryAllocationLib.h>
32 #include <Library/TimerLib.h>
33 #include <Library/DpcLib.h>
34 #include <Library/PrintLib.h>
36 #include "Udp4Driver.h"
39 extern EFI_COMPONENT_NAME_PROTOCOL gUdp4ComponentName
;
40 extern EFI_COMPONENT_NAME2_PROTOCOL gUdp4ComponentName2
;
41 extern EFI_UNICODE_STRING_TABLE
*gUdpControllerNameTable
;
42 extern EFI_SERVICE_BINDING_PROTOCOL mUdp4ServiceBinding
;
43 extern EFI_UDP4_PROTOCOL mUdp4Protocol
;
44 extern UINT16 mUdp4RandomPort
;
46 #define ICMP_ERROR_PACKET_LENGTH 8
48 #define UDP4_TIMEOUT_INTERVAL (50 * TICKS_PER_MS) // 50 milliseconds
50 #define UDP4_HEADER_SIZE sizeof (EFI_UDP_HEADER)
51 #define UDP4_MAX_DATA_SIZE 65507
53 #define UDP4_PORT_KNOWN 1024
55 #define UDP4_SERVICE_DATA_SIGNATURE SIGNATURE_32('U', 'd', 'p', '4')
57 #define UDP4_SERVICE_DATA_FROM_THIS(a) \
62 UDP4_SERVICE_DATA_SIGNATURE \
65 typedef struct _UDP4_SERVICE_DATA_
{
67 EFI_SERVICE_BINDING_PROTOCOL ServiceBinding
;
68 EFI_HANDLE ImageHandle
;
69 EFI_HANDLE ControllerHandle
;
70 LIST_ENTRY ChildrenList
;
74 EFI_EVENT TimeoutEvent
;
79 #define UDP4_INSTANCE_DATA_SIGNATURE SIGNATURE_32('U', 'd', 'p', 'I')
81 #define UDP4_INSTANCE_DATA_FROM_THIS(a) \
86 UDP4_INSTANCE_DATA_SIGNATURE \
89 typedef struct _UDP4_INSTANCE_DATA_
{
93 UDP4_SERVICE_DATA
*Udp4Service
;
94 EFI_UDP4_PROTOCOL Udp4Proto
;
95 EFI_UDP4_CONFIG_DATA ConfigData
;
96 EFI_HANDLE ChildHandle
;
105 LIST_ENTRY RcvdDgramQue
;
106 LIST_ENTRY DeliveredDgramQue
;
110 EFI_STATUS IcmpError
;
112 IP_IO_IP_INFO
*IpInfo
;
115 } UDP4_INSTANCE_DATA
;
117 typedef struct _UDP4_RXDATA_WRAP_
{
121 EFI_UDP4_RECEIVE_DATA RxData
;
125 EFI_SERVICE_BINDING_PROTOCOL
*ServiceBinding
;
126 UINTN NumberOfChildren
;
127 EFI_HANDLE
*ChildHandleBuffer
;
128 } UDP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT
;
131 Reads the current operational settings.
133 The GetModeData() function copies the current operational settings of this EFI
134 UDPv4 Protocol instance into user-supplied buffers. This function is used
135 optionally to retrieve the operational mode data of underlying networks or
138 @param[in] This Pointer to the EFI_UDP4_PROTOCOL instance.
139 @param[out] Udp4ConfigData Pointer to the buffer to receive the current configuration data.
140 @param[out] Ip4ModeData Pointer to the EFI IPv4 Protocol mode data structure.
141 @param[out] MnpConfigData Pointer to the managed network configuration data structure.
142 @param[out] SnpModeData Pointer to the simple network mode data structure.
144 @retval EFI_SUCCESS The mode data was read.
145 @retval EFI_NOT_STARTED When Udp4ConfigData is queried, no configuration data is
146 available because this instance has not been started.
147 @retval EFI_INVALID_PARAMETER This is NULL.
153 IN EFI_UDP4_PROTOCOL
*This
,
154 OUT EFI_UDP4_CONFIG_DATA
*Udp4ConfigData OPTIONAL
,
155 OUT EFI_IP4_MODE_DATA
*Ip4ModeData OPTIONAL
,
156 OUT EFI_MANAGED_NETWORK_CONFIG_DATA
*MnpConfigData OPTIONAL
,
157 OUT EFI_SIMPLE_NETWORK_MODE
*SnpModeData OPTIONAL
161 Initializes, changes, or resets the operational parameters for this instance of the EFI UDPv4
164 The Configure() function is used to do the following:
165 * Initialize and start this instance of the EFI UDPv4 Protocol.
166 * Change the filtering rules and operational parameters.
167 * Reset this instance of the EFI UDPv4 Protocol.
168 Until these parameters are initialized, no network traffic can be sent or
169 received by this instance. This instance can be also reset by calling Configure()
170 with UdpConfigData set to NULL. Once reset, the receiving queue and transmitting
171 queue are flushed and no traffic is allowed through this instance.
172 With different parameters in UdpConfigData, Configure() can be used to bind
173 this instance to specified port.
175 @param[in] This Pointer to the EFI_UDP4_PROTOCOL instance.
176 @param[in] UdpConfigData Pointer to the buffer to receive the current configuration data.
178 @retval EFI_SUCCESS The configuration settings were set, changed, or reset successfully.
179 @retval EFI_NO_MAPPING When using a default address, configuration (DHCP, BOOTP,
180 RARP, etc.) is not finished yet.
181 @retval EFI_INVALID_PARAMETER One or more following conditions are TRUE:
182 @retval EFI_ALREADY_STARTED The EFI UDPv4 Protocol instance is already started/configured
183 and must be stopped/reset before it can be reconfigured.
184 @retval EFI_ACCESS_DENIED UdpConfigData. AllowDuplicatePort is FALSE
185 and UdpConfigData.StationPort is already used by
187 @retval EFI_OUT_OF_RESOURCES The EFI UDPv4 Protocol driver cannot allocate memory for this
188 EFI UDPv4 Protocol instance.
189 @retval EFI_DEVICE_ERROR An unexpected network or system error occurred and this instance
196 IN EFI_UDP4_PROTOCOL
*This
,
197 IN EFI_UDP4_CONFIG_DATA
*UdpConfigData OPTIONAL
201 Joins and leaves multicast groups.
203 The Groups() function is used to enable and disable the multicast group
204 filtering. If the JoinFlag is FALSE and the MulticastAddress is NULL, then all
205 currently joined groups are left.
207 @param[in] This Pointer to the EFI_UDP4_PROTOCOL instance.
208 @param[in] JoinFlag Set to TRUE to join a multicast group. Set to FALSE to leave one
209 or all multicast groups.
210 @param[in] MulticastAddress Pointer to multicast group address to join or leave.
212 @retval EFI_SUCCESS The operation completed successfully.
213 @retval EFI_NOT_STARTED The EFI UDPv4 Protocol instance has not been started.
214 @retval EFI_NO_MAPPING When using a default address, configuration (DHCP, BOOTP,
215 RARP, etc.) is not finished yet.
216 @retval EFI_OUT_OF_RESOURCES Could not allocate resources to join the group.
217 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
219 - JoinFlag is TRUE and MulticastAddress is NULL.
220 - JoinFlag is TRUE and *MulticastAddress is not
221 a valid multicast address.
222 @retval EFI_ALREADY_STARTED The group address is already in the group table (when
224 @retval EFI_NOT_FOUND The group address is not in the group table (when JoinFlag is
226 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
232 IN EFI_UDP4_PROTOCOL
*This
,
234 IN EFI_IPv4_ADDRESS
*MulticastAddress OPTIONAL
238 Adds and deletes routing table entries.
240 The Routes() function adds a route to or deletes a route from the routing table.
241 Routes are determined by comparing the SubnetAddress with the destination IP
242 address and arithmetically AND-ing it with the SubnetMask. The gateway address
243 must be on the same subnet as the configured station address.
244 The default route is added with SubnetAddress and SubnetMask both set to 0.0.0.0.
245 The default route matches all destination IP addresses that do not match any
247 A zero GatewayAddress is a nonroute. Packets are sent to the destination IP
248 address if it can be found in the Address Resolution Protocol (ARP) cache or
249 on the local subnet. One automatic nonroute entry will be inserted into the
250 routing table for outgoing packets that are addressed to a local subnet
251 (gateway address of 0.0.0.0).
252 Each instance of the EFI UDPv4 Protocol has its own independent routing table.
253 Instances of the EFI UDPv4 Protocol that use the default IP address will also
254 have copies of the routing table provided by the EFI_IP4_CONFIG_PROTOCOL. These
255 copies will be updated automatically whenever the IP driver reconfigures its
256 instances; as a result, the previous modification to these copies will be lost.
258 @param[in] This Pointer to the EFI_UDP4_PROTOCOL instance.
259 @param[in] DeleteRoute Set to TRUE to delete this route from the routing table.
260 Set to FALSE to add this route to the routing table.
261 @param[in] SubnetAddress The destination network address that needs to be routed.
262 @param[in] SubnetMask The subnet mask of SubnetAddress.
263 @param[in] GatewayAddress The gateway IP address for this route.
265 @retval EFI_SUCCESS The operation completed successfully.
266 @retval EFI_NOT_STARTED The EFI UDPv4 Protocol instance has not been started.
267 @retval EFI_NO_MAPPING When using a default address, configuration (DHCP, BOOTP,
268 - RARP, etc.) is not finished yet.
269 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
270 @retval EFI_OUT_OF_RESOURCES Could not add the entry to the routing table.
271 @retval EFI_NOT_FOUND This route is not in the routing table.
272 @retval EFI_ACCESS_DENIED The route is already defined in the routing table.
278 IN EFI_UDP4_PROTOCOL
*This
,
279 IN BOOLEAN DeleteRoute
,
280 IN EFI_IPv4_ADDRESS
*SubnetAddress
,
281 IN EFI_IPv4_ADDRESS
*SubnetMask
,
282 IN EFI_IPv4_ADDRESS
*GatewayAddress
286 Queues outgoing data packets into the transmit queue.
288 The Transmit() function places a sending request to this instance of the EFI
289 UDPv4 Protocol, alongside the transmit data that was filled by the user. Whenever
290 the packet in the token is sent out or some errors occur, the Token.Event will
291 be signaled and Token.Status is updated. Providing a proper notification function
292 and context for the event will enable the user to receive the notification and
295 @param[in] This Pointer to the EFI_UDP4_PROTOCOL instance.
296 @param[in] Token Pointer to the completion token that will be placed into the
299 @retval EFI_SUCCESS The data has been queued for transmission.
300 @retval EFI_NOT_STARTED This EFI UDPv4 Protocol instance has not been started.
301 @retval EFI_NO_MAPPING When using a default address, configuration (DHCP, BOOTP,
302 RARP, etc.) is not finished yet.
303 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
304 @retval EFI_ACCESS_DENIED The transmit completion token with the same
305 Token.Event was already in the transmit queue.
306 @retval EFI_NOT_READY The completion token could not be queued because the
307 transmit queue is full.
308 @retval EFI_OUT_OF_RESOURCES Could not queue the transmit data.
309 @retval EFI_NOT_FOUND There is no route to the destination network or address.
310 @retval EFI_BAD_BUFFER_SIZE The data length is greater than the maximum UDP packet
311 size. Or the length of the IP header + UDP header + data
312 length is greater than MTU if DoNotFragment is TRUE.
318 IN EFI_UDP4_PROTOCOL
*This
,
319 IN EFI_UDP4_COMPLETION_TOKEN
*Token
323 Places an asynchronous receive request into the receiving queue.
325 The Receive() function places a completion token into the receive packet queue.
326 This function is always asynchronous.
327 The caller must fill in the Token.Event field in the completion token, and this
328 field cannot be NULL. When the receive operation completes, the EFI UDPv4 Protocol
329 driver updates the Token.Status and Token.Packet.RxData fields and the Token.Event
330 is signaled. Providing a proper notification function and context for the event
331 will enable the user to receive the notification and receiving status. That
332 notification function is guaranteed to not be re-entered.
334 @param[in] This Pointer to the EFI_UDP4_PROTOCOL instance.
335 @param[in] Token Pointer to a token that is associated with
336 the receive data descriptor.
338 @retval EFI_SUCCESS The receive completion token was cached.
339 @retval EFI_NOT_STARTED This EFI UDPv4 Protocol instance has not been started.
340 @retval EFI_NO_MAPPING When using a default address, configuration (DHCP, BOOTP, RARP, etc.)
342 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
343 @retval EFI_OUT_OF_RESOURCES The receive completion token could not be queued due to a lack of system
344 resources (usually memory).
345 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
346 @retval EFI_ACCESS_DENIED A receive completion token with the same Token.Event was already in
348 @retval EFI_NOT_READY The receive request could not be queued because the receive queue is full.
354 IN EFI_UDP4_PROTOCOL
*This
,
355 IN EFI_UDP4_COMPLETION_TOKEN
*Token
359 Aborts an asynchronous transmit or receive request.
361 The Cancel() function is used to abort a pending transmit or receive request.
362 If the token is in the transmit or receive request queues, after calling this
363 function, Token.Status will be set to EFI_ABORTED and then Token.Event will be
364 signaled. If the token is not in one of the queues, which usually means that
365 the asynchronous operation has completed, this function will not signal the
366 token and EFI_NOT_FOUND is returned.
368 @param[in] This Pointer to the EFI_UDP4_PROTOCOL instance.
369 @param[in] Token Pointer to a token that has been issued by
370 EFI_UDP4_PROTOCOL.Transmit() or
371 EFI_UDP4_PROTOCOL.Receive().If NULL, all pending
374 @retval EFI_SUCCESS The asynchronous I/O request was aborted and Token.Event
375 was signaled. When Token is NULL, all pending requests are
376 aborted and their events are signaled.
377 @retval EFI_INVALID_PARAMETER This is NULL.
378 @retval EFI_NOT_STARTED This instance has not been started.
379 @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP,
380 RARP, etc.) is not finished yet.
381 @retval EFI_NOT_FOUND When Token is not NULL, the asynchronous I/O request was
382 not found in the transmit or receive queue. It has either completed
383 or was not issued by Transmit() and Receive().
389 IN EFI_UDP4_PROTOCOL
*This
,
390 IN EFI_UDP4_COMPLETION_TOKEN
*Token OPTIONAL
394 Polls for incoming data packets and processes outgoing data packets.
396 The Poll() function can be used by network drivers and applications to increase
397 the rate that data packets are moved between the communications device and the
398 transmit and receive queues.
399 In some systems, the periodic timer event in the managed network driver may not
400 poll the underlying communications device fast enough to transmit and/or receive
401 all data packets without missing incoming packets or dropping outgoing packets.
402 Drivers and applications that are experiencing packet loss should try calling
403 the Poll() function more often.
405 @param[in] This Pointer to the EFI_UDP4_PROTOCOL instance.
407 @retval EFI_SUCCESS Incoming or outgoing data was processed.
408 @retval EFI_INVALID_PARAMETER This is NULL.
409 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
410 @retval EFI_TIMEOUT Data was dropped out of the transmit and/or receive queue.
416 IN EFI_UDP4_PROTOCOL
*This
420 Create the Udp service context data.
422 @param[in, out] Udp4Service Pointer to the UDP4_SERVICE_DATA.
423 @param[in] ImageHandle The image handle of this udp4 driver.
424 @param[in] ControllerHandle The controller handle this udp4 driver binds on.
426 @retval EFI_SUCCESS The udp4 service context data is created and
428 @retval EFI_OUT_OF_RESOURCES Cannot allocate memory.
429 @retval other Other error occurs.
434 IN OUT UDP4_SERVICE_DATA
*Udp4Service
,
435 IN EFI_HANDLE ImageHandle
,
436 IN EFI_HANDLE ControllerHandle
440 Clean the Udp service context data.
442 @param[in] Udp4Service Pointer to the UDP4_SERVICE_DATA.
447 IN UDP4_SERVICE_DATA
*Udp4Service
451 This function intializes the new created udp instance.
453 @param[in] Udp4Service Pointer to the UDP4_SERVICE_DATA.
454 @param[in, out] Instance Pointer to the un-initialized UDP4_INSTANCE_DATA.
459 IN UDP4_SERVICE_DATA
*Udp4Service
,
460 IN OUT UDP4_INSTANCE_DATA
*Instance
464 This function cleans the udp instance.
466 @param[in] Instance Pointer to the UDP4_INSTANCE_DATA to clean.
471 IN UDP4_INSTANCE_DATA
*Instance
475 This function tries to bind the udp instance according to the configured port
478 @param[in] InstanceList Pointer to the head of the list linking the udp
480 @param[in, out] ConfigData Pointer to the ConfigData of the instance to be
481 bound. ConfigData->StationPort will be assigned
482 with an available port value on success.
484 @retval EFI_SUCCESS The bound operation is completed successfully.
485 @retval EFI_ACCESS_DENIED The <Address, Port> specified by the ConfigData is
486 already used by other instance.
487 @retval EFI_OUT_OF_RESOURCES No available port resources.
492 IN LIST_ENTRY
*InstanceList
,
493 IN OUT EFI_UDP4_CONFIG_DATA
*ConfigData
497 This function is used to check whether the NewConfigData has any un-reconfigurable
498 parameters changed compared to the OldConfigData.
500 @param[in] OldConfigData Pointer to the current ConfigData the udp instance
502 @param[in] NewConfigData Pointer to the new ConfigData.
504 @retval TRUE The instance is reconfigurable.
505 @retval FALSE Otherwise.
509 Udp4IsReconfigurable (
510 IN EFI_UDP4_CONFIG_DATA
*OldConfigData
,
511 IN EFI_UDP4_CONFIG_DATA
*NewConfigData
515 This function builds the Ip4 configdata from the Udp4ConfigData.
517 @param[in] Udp4ConfigData Pointer to the EFI_UDP4_CONFIG_DATA.
518 @param[in, out] Ip4ConfigData Pointer to the EFI_IP4_CONFIG_DATA.
522 Udp4BuildIp4ConfigData (
523 IN EFI_UDP4_CONFIG_DATA
*Udp4ConfigData
,
524 IN OUT EFI_IP4_CONFIG_DATA
*Ip4ConfigData
528 This function validates the TxToken, it returns the error code according to the spec.
530 @param[in] Instance Pointer to the udp instance context data.
531 @param[in] TxToken Pointer to the token to be checked.
533 @retval EFI_SUCCESS The TxToken is valid.
534 @retval EFI_INVALID_PARAMETER One or more of the following are TRUE: This is
535 NULL. Token is NULL. Token.Event is NULL.
536 Token.Packet.TxData is NULL.
537 Token.Packet.TxData.FragmentCount is zero.
538 Token.Packet.TxData.DataLength is not equal to the
539 sum of fragment lengths. One or more of the
540 Token.Packet.TxData.FragmentTable[].
541 FragmentLength fields is zero. One or more of the
542 Token.Packet.TxData.FragmentTable[].
543 FragmentBuffer fields is NULL.
544 Token.Packet.TxData. GatewayAddress is not a
545 unicast IPv4 address if it is not NULL. One or
546 more IPv4 addresses in Token.Packet.TxData.
547 UdpSessionData are not valid unicast IPv4
548 addresses if the UdpSessionData is not NULL.
549 @retval EFI_BAD_BUFFER_SIZE The data length is greater than the maximum UDP
554 Udp4ValidateTxToken (
555 IN UDP4_INSTANCE_DATA
*Instance
,
556 IN EFI_UDP4_COMPLETION_TOKEN
*TxToken
560 This function checks whether the specified Token duplicates with the one in the Map.
562 @param[in] Map Pointer to the NET_MAP.
563 @param[in] Item Pointer to the NET_MAP_ITEM contain the pointer to
565 @param[in] Context Pointer to the Token to be checked.
567 @retval EFI_SUCCESS The Token specified by Context differs from the
569 @retval EFI_ACCESS_DENIED The Token duplicates with the one in the Item.
576 IN NET_MAP_ITEM
*Item
,
581 This function calculates the checksum for the Packet, utilizing the pre-calculated
582 pseudo HeadSum to reduce some overhead.
584 @param[in] Packet Pointer to the NET_BUF contains the udp datagram.
585 @param[in] HeadSum Checksum of the pseudo header execpt the length
588 @retval The 16-bit checksum of this udp datagram.
598 This function removes the specified Token from the TokenMap.
600 @param[in, out] TokenMap Pointer to the NET_MAP containing the tokens.
601 @param[in] Token Pointer to the Token to be removed.
603 @retval EFI_SUCCESS The specified Token is removed from the TokenMap.
604 @retval EFI_NOT_FOUND The specified Token is not found in the TokenMap.
609 IN OUT NET_MAP
*TokenMap
,
610 IN EFI_UDP4_COMPLETION_TOKEN
*Token
614 This function removes the multicast group specified by Arg from the Map.
616 @param[in, out] Map Pointer to the NET_MAP.
617 @param[in] Item Pointer to the NET_MAP_ITEM.
618 @param[in] Arg Pointer to the Arg, it's the pointer to a
619 multicast IPv4 Address.
621 @retval EFI_SUCCESS The multicast address is removed.
622 @retval EFI_ABORTED The specified multicast address is removed and the
630 IN NET_MAP_ITEM
*Item
,
631 IN VOID
*Arg OPTIONAL
635 This function removes all the Wrap datas in the RcvdDgramQue.
637 @param[in] Instance Pointer to the udp instance context data.
642 IN UDP4_INSTANCE_DATA
*Instance
646 Cancel Udp4 tokens from the Udp4 instance.
648 @param[in] Instance Pointer to the udp instance context data.
649 @param[in] Token Pointer to the token to be canceled, if NULL, all
650 tokens in this instance will be cancelled.
652 @retval EFI_SUCCESS The Token is cancelled.
653 @retval EFI_NOT_FOUND The Token is not found.
657 Udp4InstanceCancelToken (
658 IN UDP4_INSTANCE_DATA
*Instance
,
659 IN EFI_UDP4_COMPLETION_TOKEN
*Token OPTIONAL
663 This function delivers the received datagrams for the specified instance.
665 @param[in] Instance Pointer to the instance context data.
669 Udp4InstanceDeliverDgram (
670 IN UDP4_INSTANCE_DATA
*Instance
674 This function reports the received ICMP error.
676 @param[in] Instance Pointer to the udp instance context data.
680 Udp4ReportIcmpError (
681 IN UDP4_INSTANCE_DATA
*Instance
685 This function is a dummy ext-free function for the NET_BUF created for the output
688 @param[in] Context Pointer to the context data.
693 Udp4NetVectorExtFree (
698 Set the Udp4 variable data.
700 @param[in] Udp4Service Udp4 service data.
702 @retval EFI_OUT_OF_RESOURCES There are not enough resources to set the
704 @retval EFI_SUCCESS Set variable successfully.
705 @retval other Set variable failed.
709 Udp4SetVariableData (
710 IN UDP4_SERVICE_DATA
*Udp4Service
714 Clear the variable and free the resource.
716 @param[[in] Udp4Service Udp4 service data.
720 Udp4ClearVariableData (
721 IN UDP4_SERVICE_DATA
*Udp4Service