2 EFI UDPv4 protocol implementation.
4 Copyright (c) 2006 - 2007, Intel Corporation.<BR>
5 All rights reserved. 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>
34 #include "Udp4Driver.h"
37 extern EFI_COMPONENT_NAME_PROTOCOL gUdp4ComponentName
;
38 extern EFI_COMPONENT_NAME2_PROTOCOL gUdp4ComponentName2
;
39 extern EFI_SERVICE_BINDING_PROTOCOL mUdp4ServiceBinding
;
40 extern EFI_UDP4_PROTOCOL mUdp4Protocol
;
41 extern UINT16 mUdp4RandomPort
;
43 #define ICMP_ERROR_PACKET_LENGTH 8
45 #define UDP4_TIMEOUT_INTERVAL (10 * TICKS_PER_MS) // 10 milliseconds
47 #define UDP4_HEADER_SIZE sizeof (EFI_UDP4_HEADER)
48 #define UDP4_MAX_DATA_SIZE 65507
50 #define UDP4_PORT_KNOWN 1024
52 #define UDP4_SERVICE_DATA_SIGNATURE SIGNATURE_32('U', 'd', 'p', '4')
54 #define UDP4_SERVICE_DATA_FROM_THIS(a) \
59 UDP4_SERVICE_DATA_SIGNATURE \
62 typedef struct _UDP4_SERVICE_DATA_
{
64 EFI_SERVICE_BINDING_PROTOCOL ServiceBinding
;
65 EFI_HANDLE ImageHandle
;
66 EFI_HANDLE ControllerHandle
;
67 LIST_ENTRY ChildrenList
;
71 EFI_EVENT TimeoutEvent
;
76 #define UDP4_INSTANCE_DATA_SIGNATURE SIGNATURE_32('U', 'd', 'p', 'I')
78 #define UDP4_INSTANCE_DATA_FROM_THIS(a) \
83 UDP4_INSTANCE_DATA_SIGNATURE \
86 typedef struct _UDP4_INSTANCE_DATA_
{
90 UDP4_SERVICE_DATA
*Udp4Service
;
91 EFI_UDP4_PROTOCOL Udp4Proto
;
92 EFI_UDP4_CONFIG_DATA ConfigData
;
93 EFI_HANDLE ChildHandle
;
102 LIST_ENTRY RcvdDgramQue
;
103 LIST_ENTRY DeliveredDgramQue
;
107 EFI_STATUS IcmpError
;
109 IP_IO_IP_INFO
*IpInfo
;
112 } UDP4_INSTANCE_DATA
;
114 typedef struct _UDP4_RXDATA_WRAP_
{
118 EFI_UDP4_RECEIVE_DATA RxData
;
122 Reads the current operational settings.
124 The GetModeData() function copies the current operational settings of this EFI
125 UDPv4 Protocol instance into user-supplied buffers. This function is used
126 optionally to retrieve the operational mode data of underlying networks or
129 @param This Pointer to the EFI_UDP4_PROTOCOL instance.
130 @param Udp4ConfigData Pointer to the buffer to receive the current configuration data.
131 @param Ip4ModeData Pointer to the EFI IPv4 Protocol mode data structure.
132 @param MnpConfigData Pointer to the managed network configuration data structure.
133 @param SnpModeData Pointer to the simple network mode data structure.
135 @retval EFI_SUCCESS The mode data was read.
136 @retval EFI_NOT_STARTED When Udp4ConfigData is queried, no configuration data is
137 available because this instance has not been started.
138 @retval EFI_INVALID_PARAMETER This is NULL.
144 IN EFI_UDP4_PROTOCOL
*This
,
145 OUT EFI_UDP4_CONFIG_DATA
*Udp4ConfigData OPTIONAL
,
146 OUT EFI_IP4_MODE_DATA
*Ip4ModeData OPTIONAL
,
147 OUT EFI_MANAGED_NETWORK_CONFIG_DATA
*MnpConfigData OPTIONAL
,
148 OUT EFI_SIMPLE_NETWORK_MODE
*SnpModeData OPTIONAL
152 Initializes, changes, or resets the operational parameters for this instance of the EFI UDPv4
155 The Configure() function is used to do the following:
156 * Initialize and start this instance of the EFI UDPv4 Protocol.
157 * Change the filtering rules and operational parameters.
158 * Reset this instance of the EFI UDPv4 Protocol.
159 Until these parameters are initialized, no network traffic can be sent or
160 received by this instance. This instance can be also reset by calling Configure()
161 with UdpConfigData set to NULL. Once reset, the receiving queue and transmitting
162 queue are flushed and no traffic is allowed through this instance.
163 With different parameters in UdpConfigData, Configure() can be used to bind
164 this instance to specified port.
166 @param[in] This Pointer to the EFI_UDP4_PROTOCOL instance.
167 @param[in] UdpConfigData Pointer to the buffer to receive the current configuration data.
169 @retval EFI_SUCCESS The configuration settings were set, changed, or reset successfully.
170 @retval EFI_NO_MAPPING When using a default address, configuration (DHCP, BOOTP,
171 RARP, etc.) is not finished yet.
172 @retval EFI_INVALID_PARAMETER One or more following conditions are TRUE:
173 @retval EFI_ALREADY_STARTED The EFI UDPv4 Protocol instance is already started/configured
174 and must be stopped/reset before it can be reconfigured.
175 @retval EFI_ACCESS_DENIED UdpConfigData. AllowDuplicatePort is FALSE
176 and UdpConfigData.StationPort is already used by
178 @retval EFI_OUT_OF_RESOURCES The EFI UDPv4 Protocol driver cannot allocate memory for this
179 EFI UDPv4 Protocol instance.
180 @retval EFI_DEVICE_ERROR An unexpected network or system error occurred and this instance
187 IN EFI_UDP4_PROTOCOL
*This
,
188 IN EFI_UDP4_CONFIG_DATA
*UdpConfigData OPTIONAL
192 Joins and leaves multicast groups.
194 The Groups() function is used to enable and disable the multicast group
195 filtering. If the JoinFlag is FALSE and the MulticastAddress is NULL, then all
196 currently joined groups are left.
198 @param[in] This Pointer to the EFI_UDP4_PROTOCOL instance.
199 @param[in] JoinFlag Set to TRUE to join a multicast group. Set to FALSE to leave one
200 or all multicast groups.
201 @param[in] MulticastAddress Pointer to multicast group address to join or leave.
203 @retval EFI_SUCCESS The operation completed successfully.
204 @retval EFI_NOT_STARTED The EFI UDPv4 Protocol instance has not been started.
205 @retval EFI_NO_MAPPING When using a default address, configuration (DHCP, BOOTP,
206 RARP, etc.) is not finished yet.
207 @retval EFI_OUT_OF_RESOURCES Could not allocate resources to join the group.
208 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
210 - JoinFlag is TRUE and MulticastAddress is NULL.
211 - JoinFlag is TRUE and *MulticastAddress is not
212 a valid multicast address.
213 @retval EFI_ALREADY_STARTED The group address is already in the group table (when
215 @retval EFI_NOT_FOUND The group address is not in the group table (when JoinFlag is
217 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
223 IN EFI_UDP4_PROTOCOL
*This
,
225 IN EFI_IPv4_ADDRESS
*MulticastAddress OPTIONAL
229 Adds and deletes routing table entries.
231 The Routes() function adds a route to or deletes a route from the routing table.
232 Routes are determined by comparing the SubnetAddress with the destination IP
233 address and arithmetically AND-ing it with the SubnetMask. The gateway address
234 must be on the same subnet as the configured station address.
235 The default route is added with SubnetAddress and SubnetMask both set to 0.0.0.0.
236 The default route matches all destination IP addresses that do not match any
238 A zero GatewayAddress is a nonroute. Packets are sent to the destination IP
239 address if it can be found in the Address Resolution Protocol (ARP) cache or
240 on the local subnet. One automatic nonroute entry will be inserted into the
241 routing table for outgoing packets that are addressed to a local subnet
242 (gateway address of 0.0.0.0).
243 Each instance of the EFI UDPv4 Protocol has its own independent routing table.
244 Instances of the EFI UDPv4 Protocol that use the default IP address will also
245 have copies of the routing table provided by the EFI_IP4_CONFIG_PROTOCOL. These
246 copies will be updated automatically whenever the IP driver reconfigures its
247 instances; as a result, the previous modification to these copies will be lost.
249 @param[in] This Pointer to the EFI_UDP4_PROTOCOL instance.
250 @param[in] DeleteRoute Set to TRUE to delete this route from the routing table.
251 Set to FALSE to add this route to the routing table.
252 @param[in] SubnetAddress The destination network address that needs to be routed.
253 @param[in] SubnetMask The subnet mask of SubnetAddress.
254 @param[in] GatewayAddress The gateway IP address for this route.
256 @retval EFI_SUCCESS The operation completed successfully.
257 @retval EFI_NOT_STARTED The EFI UDPv4 Protocol instance has not been started.
258 @retval EFI_NO_MAPPING When using a default address, configuration (DHCP, BOOTP,
259 - RARP, etc.) is not finished yet.
260 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
261 @retval EFI_OUT_OF_RESOURCES Could not add the entry to the routing table.
262 @retval EFI_NOT_FOUND This route is not in the routing table.
263 @retval EFI_ACCESS_DENIED The route is already defined in the routing table.
269 IN EFI_UDP4_PROTOCOL
*This
,
270 IN BOOLEAN DeleteRoute
,
271 IN EFI_IPv4_ADDRESS
*SubnetAddress
,
272 IN EFI_IPv4_ADDRESS
*SubnetMask
,
273 IN EFI_IPv4_ADDRESS
*GatewayAddress
277 Queues outgoing data packets into the transmit queue.
279 The Transmit() function places a sending request to this instance of the EFI
280 UDPv4 Protocol, alongside the transmit data that was filled by the user. Whenever
281 the packet in the token is sent out or some errors occur, the Token.Event will
282 be signaled and Token.Status is updated. Providing a proper notification function
283 and context for the event will enable the user to receive the notification and
286 @param[in] This Pointer to the EFI_UDP4_PROTOCOL instance.
287 @param[in] Token Pointer to the completion token that will be placed into the
290 @retval EFI_SUCCESS The data has been queued for transmission.
291 @retval EFI_NOT_STARTED This EFI UDPv4 Protocol instance has not been started.
292 @retval EFI_NO_MAPPING When using a default address, configuration (DHCP, BOOTP,
293 RARP, etc.) is not finished yet.
294 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
295 @retval EFI_ACCESS_DENIED The transmit completion token with the same
296 Token.Event was already in the transmit queue.
297 @retval EFI_NOT_READY The completion token could not be queued because the
298 transmit queue is full.
299 @retval EFI_OUT_OF_RESOURCES Could not queue the transmit data.
300 @retval EFI_NOT_FOUND There is no route to the destination network or address.
301 @retval EFI_BAD_BUFFER_SIZE The data length is greater than the maximum UDP packet
302 size. Or the length of the IP header + UDP header + data
303 length is greater than MTU if DoNotFragment is TRUE.
309 IN EFI_UDP4_PROTOCOL
*This
,
310 IN EFI_UDP4_COMPLETION_TOKEN
*Token
314 Places an asynchronous receive request into the receiving queue.
316 The Receive() function places a completion token into the receive packet queue.
317 This function is always asynchronous.
318 The caller must fill in the Token.Event field in the completion token, and this
319 field cannot be NULL. When the receive operation completes, the EFI UDPv4 Protocol
320 driver updates the Token.Status and Token.Packet.RxData fields and the Token.Event
321 is signaled. Providing a proper notification function and context for the event
322 will enable the user to receive the notification and receiving status. That
323 notification function is guaranteed to not be re-entered.
325 @param[in] This Pointer to the EFI_UDP4_PROTOCOL instance.
326 @param[in] Token Pointer to a token that is associated with
327 the receive data descriptor.
329 @retval EFI_SUCCESS The receive completion token was cached.
330 @retval EFI_NOT_STARTED This EFI UDPv4 Protocol instance has not been started.
331 @retval EFI_NO_MAPPING When using a default address, configuration (DHCP, BOOTP, RARP, etc.)
333 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
334 @retval EFI_OUT_OF_RESOURCES The receive completion token could not be queued due to a lack of system
335 resources (usually memory).
336 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
337 @retval EFI_ACCESS_DENIED A receive completion token with the same Token.Event was already in
339 @retval EFI_NOT_READY The receive request could not be queued because the receive queue is full.
345 IN EFI_UDP4_PROTOCOL
*This
,
346 IN EFI_UDP4_COMPLETION_TOKEN
*Token
350 Aborts an asynchronous transmit or receive request.
352 The Cancel() function is used to abort a pending transmit or receive request.
353 If the token is in the transmit or receive request queues, after calling this
354 function, Token.Status will be set to EFI_ABORTED and then Token.Event will be
355 signaled. If the token is not in one of the queues, which usually means that
356 the asynchronous operation has completed, this function will not signal the
357 token and EFI_NOT_FOUND is returned.
359 @param[in] This Pointer to the EFI_UDP4_PROTOCOL instance.
360 @param[in] Token Pointer to a token that has been issued by
361 EFI_UDP4_PROTOCOL.Transmit() or
362 EFI_UDP4_PROTOCOL.Receive().If NULL, all pending
365 @retval EFI_SUCCESS The asynchronous I/O request was aborted and Token.Event
366 was signaled. When Token is NULL, all pending requests are
367 aborted and their events are signaled.
368 @retval EFI_INVALID_PARAMETER This is NULL.
369 @retval EFI_NOT_STARTED This instance has not been started.
370 @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP,
371 RARP, etc.) is not finished yet.
372 @retval EFI_NOT_FOUND When Token is not NULL, the asynchronous I/O request was
373 not found in the transmit or receive queue. It has either completed
374 or was not issued by Transmit() and Receive().
380 IN EFI_UDP4_PROTOCOL
*This
,
381 IN EFI_UDP4_COMPLETION_TOKEN
*Token OPTIONAL
385 Polls for incoming data packets and processes outgoing data packets.
387 The Poll() function can be used by network drivers and applications to increase
388 the rate that data packets are moved between the communications device and the
389 transmit and receive queues.
390 In some systems, the periodic timer event in the managed network driver may not
391 poll the underlying communications device fast enough to transmit and/or receive
392 all data packets without missing incoming packets or dropping outgoing packets.
393 Drivers and applications that are experiencing packet loss should try calling
394 the Poll() function more often.
396 @param[in] This Pointer to the EFI_UDP4_PROTOCOL instance.
398 @retval EFI_SUCCESS Incoming or outgoing data was processed.
399 @retval EFI_INVALID_PARAMETER This is NULL.
400 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
401 @retval EFI_TIMEOUT Data was dropped out of the transmit and/or receive queue.
407 IN EFI_UDP4_PROTOCOL
*This
411 Create the Udp service context data.
413 @param Udp4Service Pointer to the UDP4_SERVICE_DATA.
414 @param ImageHandle The image handle of this udp4 driver.
415 @param ControllerHandle The controller handle this udp4 driver binds on.
417 @retval EFI_SUCCESS The udp4 service context data is created and
419 @retval EFI_OUT_OF_RESOURCES Cannot allocate memory.
420 @retval other Other error occurs.
425 IN OUT UDP4_SERVICE_DATA
*Udp4Service
,
426 IN EFI_HANDLE ImageHandle
,
427 IN EFI_HANDLE ControllerHandle
431 Clean the Udp service context data.
433 @param[in] Udp4Service Pointer to the UDP4_SERVICE_DATA.
438 IN UDP4_SERVICE_DATA
*Udp4Service
442 This function intializes the new created udp instance.
444 @param Udp4Service Pointer to the UDP4_SERVICE_DATA.
445 @param Instance Pointer to the un-initialized UDP4_INSTANCE_DATA.
450 IN UDP4_SERVICE_DATA
*Udp4Service
,
451 IN OUT UDP4_INSTANCE_DATA
*Instance
455 This function cleans the udp instance.
457 @param[in] Instance Pointer to the UDP4_INSTANCE_DATA to clean.
462 IN UDP4_INSTANCE_DATA
*Instance
466 This function tries to bind the udp instance according to the configured port
469 @param InstanceList Pointer to the head of the list linking the udp
471 @param ConfigData Pointer to the ConfigData of the instance to be
472 bound. ConfigData->StationPort will be assigned
473 with an available port value on success.
475 @retval EFI_SUCCESS The bound operation is completed successfully.
476 @retval EFI_ACCESS_DENIED The <Address, Port> specified by the ConfigData is
477 already used by other instance.
478 @retval EFI_OUT_OF_RESOURCES No available port resources.
483 IN LIST_ENTRY
*InstanceList
,
484 IN OUT EFI_UDP4_CONFIG_DATA
*ConfigData
488 This function is used to check whether the NewConfigData has any un-reconfigurable
489 parameters changed compared to the OldConfigData.
491 @param[in] OldConfigData Pointer to the current ConfigData the udp instance
493 @param[in] NewConfigData Pointer to the new ConfigData.
495 @retval TRUE The instance is reconfigurable.
496 @retval FALSE Otherwise.
500 Udp4IsReconfigurable (
501 IN EFI_UDP4_CONFIG_DATA
*OldConfigData
,
502 IN EFI_UDP4_CONFIG_DATA
*NewConfigData
506 This function builds the Ip4 configdata from the Udp4ConfigData.
508 @param[in] Udp4ConfigData Pointer to the EFI_UDP4_CONFIG_DATA.
509 @param[in, out] Ip4ConfigData Pointer to the EFI_IP4_CONFIG_DATA.
513 Udp4BuildIp4ConfigData (
514 IN EFI_UDP4_CONFIG_DATA
*Udp4ConfigData
,
515 IN OUT EFI_IP4_CONFIG_DATA
*Ip4ConfigData
519 This function validates the TxToken, it returns the error code according to the spec.
521 @param[in] Instance Pointer to the udp instance context data.
522 @param[in] TxToken Pointer to the token to be checked.
524 @retval EFI_SUCCESS The TxToken is valid.
525 @retval EFI_INVALID_PARAMETER One or more of the following are TRUE: This is
526 NULL. Token is NULL. Token.Event is NULL.
527 Token.Packet.TxData is NULL.
528 Token.Packet.TxData.FragmentCount is zero.
529 Token.Packet.TxData.DataLength is not equal to the
530 sum of fragment lengths. One or more of the
531 Token.Packet.TxData.FragmentTable[].
532 FragmentLength fields is zero. One or more of the
533 Token.Packet.TxData.FragmentTable[].
534 FragmentBuffer fields is NULL.
535 Token.Packet.TxData. GatewayAddress is not a
536 unicast IPv4 address if it is not NULL. One or
537 more IPv4 addresses in Token.Packet.TxData.
538 UdpSessionData are not valid unicast IPv4
539 addresses if the UdpSessionData is not NULL.
540 @retval EFI_BAD_BUFFER_SIZE The data length is greater than the maximum UDP
545 Udp4ValidateTxToken (
546 IN UDP4_INSTANCE_DATA
*Instance
,
547 IN EFI_UDP4_COMPLETION_TOKEN
*TxToken
551 This function checks whether the specified Token duplicates with the one in the Map.
553 @param[in] Map Pointer to the NET_MAP.
554 @param[in] Item Pointer to the NET_MAP_ITEM contain the pointer to
556 @param[in] Context Pointer to the Token to be checked.
558 @retval EFI_SUCCESS The Token specified by Context differs from the
560 @retval EFI_ACCESS_DENIED The Token duplicates with the one in the Item.
566 IN NET_MAP_ITEM
*Item
,
571 This function calculates the checksum for the Packet, utilizing the pre-calculated
572 pseudo HeadSum to reduce some overhead.
574 @param[in] Packet Pointer to the NET_BUF contains the udp datagram.
575 @param[in] HeadSum Checksum of the pseudo header execpt the length
578 @retval The 16-bit checksum of this udp datagram.
588 This function removes the specified Token from the TokenMap.
590 @param TokenMap Pointer to the NET_MAP containing the tokens.
591 @param Token Pointer to the Token to be removed.
593 @retval EFI_SUCCESS The specified Token is removed from the TokenMap.
594 @retval EFI_NOT_FOUND The specified Token is not found in the TokenMap.
599 IN OUT NET_MAP
*TokenMap
,
600 IN EFI_UDP4_COMPLETION_TOKEN
*Token
604 This function removes the multicast group specified by Arg from the Map.
606 @param Map Pointer to the NET_MAP.
607 @param Item Pointer to the NET_MAP_ITEM.
608 @param Arg Pointer to the Arg, it's the pointer to a
609 multicast IPv4 Address.
611 @retval EFI_SUCCESS The multicast address is removed.
612 @retval EFI_ABORTED The specified multicast address is removed and the
619 IN NET_MAP_ITEM
*Item
,
620 IN VOID
*Arg OPTIONAL
624 This function removes all the Wrap datas in the RcvdDgramQue.
626 @param[in] Instance Pointer to the udp instance context data.
631 IN UDP4_INSTANCE_DATA
*Instance
635 Cancel Udp4 tokens from the Udp4 instance.
637 @param[in] Instance Pointer to the udp instance context data.
638 @param[in] Token Pointer to the token to be canceled, if NULL, all
639 tokens in this instance will be cancelled.
641 @retval EFI_SUCCESS The Token is cancelled.
642 @retval EFI_NOT_FOUND The Token is not found.
646 Udp4InstanceCancelToken (
647 IN UDP4_INSTANCE_DATA
*Instance
,
648 IN EFI_UDP4_COMPLETION_TOKEN
*Token OPTIONAL
652 This function delivers the received datagrams for the specified instance.
654 @param[in] Instance Pointer to the instance context data.
658 Udp4InstanceDeliverDgram (
659 IN UDP4_INSTANCE_DATA
*Instance
663 This function reports the received ICMP error.
665 @param[in] Instance Pointer to the udp instance context data.
669 Udp4ReportIcmpError (
670 IN UDP4_INSTANCE_DATA
*Instance
674 This function is a dummy ext-free function for the NET_BUF created for the output
677 @param[in] Context Pointer to the context data.
681 Udp4NetVectorExtFree (
686 Set the Udp4 variable data.
688 @param Udp4Service Udp4 service data.
690 @retval EFI_OUT_OF_RESOURCES There are not enough resources to set the
692 @retval EFI_SUCCESS Set variable successfully.
693 @retval other Set variable failed.
697 Udp4SetVariableData (
698 IN UDP4_SERVICE_DATA
*Udp4Service
702 Clear the variable and free the resource.
704 @param[[in] Udp4Service Udp4 service data.
708 Udp4ClearVariableData (
709 IN UDP4_SERVICE_DATA
*Udp4Service