1 /** @file
2   EFI UDPv4 protocol implementation.
3 
4 Copyright (c) 2006 - 2014, 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
9 
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.
12 
13 **/
14 
15 #ifndef _UDP4_IMPL_H_
16 #define _UDP4_IMPL_H_
17 
18 #include <Uefi.h>
19 
20 #include <Protocol/Ip4.h>
21 #include <Protocol/Udp4.h>
22 
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>
35 
36 #include "Udp4Driver.h"
37 
38 
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;
45 
46 #define ICMP_ERROR_PACKET_LENGTH  8
47 
48 #define UDP4_TIMEOUT_INTERVAL (50 * TICKS_PER_MS)  // 50 milliseconds
49 
50 #define UDP4_HEADER_SIZE      sizeof (EFI_UDP_HEADER)
51 #define UDP4_MAX_DATA_SIZE    65507
52 
53 #define UDP4_PORT_KNOWN       1024
54 
55 #define UDP4_SERVICE_DATA_SIGNATURE  SIGNATURE_32('U', 'd', 'p', '4')
56 
57 #define UDP4_SERVICE_DATA_FROM_THIS(a) \
58   CR ( \
59   (a), \
60   UDP4_SERVICE_DATA, \
61   ServiceBinding, \
62   UDP4_SERVICE_DATA_SIGNATURE \
63   )
64 
65 typedef struct _UDP4_SERVICE_DATA_ {
66   UINT32                        Signature;
67   EFI_SERVICE_BINDING_PROTOCOL  ServiceBinding;
68   EFI_HANDLE                    ImageHandle;
69   EFI_HANDLE                    ControllerHandle;
70   LIST_ENTRY                    ChildrenList;
71   UINTN                         ChildrenNumber;
72   IP_IO                         *IpIo;
73 
74   EFI_EVENT                     TimeoutEvent;
75 } UDP4_SERVICE_DATA;
76 
77 #define UDP4_INSTANCE_DATA_SIGNATURE  SIGNATURE_32('U', 'd', 'p', 'I')
78 
79 #define UDP4_INSTANCE_DATA_FROM_THIS(a) \
80   CR ( \
81   (a), \
82   UDP4_INSTANCE_DATA, \
83   Udp4Proto, \
84   UDP4_INSTANCE_DATA_SIGNATURE \
85   )
86 
87 typedef struct _UDP4_INSTANCE_DATA_ {
88   UINT32                Signature;
89   LIST_ENTRY            Link;
90 
91   UDP4_SERVICE_DATA     *Udp4Service;
92   EFI_UDP4_PROTOCOL     Udp4Proto;
93   EFI_UDP4_CONFIG_DATA  ConfigData;
94   EFI_HANDLE            ChildHandle;
95   BOOLEAN               Configured;
96   BOOLEAN               IsNoMapping;
97 
98   NET_MAP               TxTokens;
99   NET_MAP               RxTokens;
100 
101   NET_MAP               McastIps;
102 
103   LIST_ENTRY            RcvdDgramQue;
104   LIST_ENTRY            DeliveredDgramQue;
105 
106   UINT16                HeadSum;
107 
108   EFI_STATUS            IcmpError;
109 
110   IP_IO_IP_INFO         *IpInfo;
111 
112   BOOLEAN               InDestroy;
113 } UDP4_INSTANCE_DATA;
114 
115 typedef struct _UDP4_RXDATA_WRAP_ {
116   LIST_ENTRY             Link;
117   NET_BUF                *Packet;
118   UINT32                 TimeoutTick;
119   EFI_UDP4_RECEIVE_DATA  RxData;
120 } UDP4_RXDATA_WRAP;
121 
122 typedef struct {
123   EFI_SERVICE_BINDING_PROTOCOL  *ServiceBinding;
124   UINTN                         NumberOfChildren;
125   EFI_HANDLE                    *ChildHandleBuffer;
126 } UDP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT;
127 
128 /**
129   Reads the current operational settings.
130 
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
134   drivers.
135 
136   @param[in]  This              Pointer to the EFI_UDP4_PROTOCOL instance.
137   @param[out] Udp4ConfigData    Pointer to the buffer to receive the current configuration data.
138   @param[out] Ip4ModeData       Pointer to the EFI IPv4 Protocol mode data structure.
139   @param[out] MnpConfigData     Pointer to the managed network configuration data structure.
140   @param[out] SnpModeData       Pointer to the simple network mode data structure.
141 
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.
146 
147 **/
148 EFI_STATUS
149 EFIAPI
150 Udp4GetModeData (
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
156   );
157 
158 /**
159   Initializes, changes, or resets the operational parameters for this instance of the EFI UDPv4
160   Protocol.
161 
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.
172 
173   @param[in]  This              Pointer to the EFI_UDP4_PROTOCOL instance.
174   @param[in]  UdpConfigData     Pointer to the buffer to receive the current configuration data.
175 
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
184                                 other instance.
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
188                                  was not opened.
189 
190 **/
191 EFI_STATUS
192 EFIAPI
193 Udp4Configure (
194   IN EFI_UDP4_PROTOCOL     *This,
195   IN EFI_UDP4_CONFIG_DATA  *UdpConfigData OPTIONAL
196   );
197 
198 /**
199   Joins and leaves multicast groups.
200 
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.
204 
205   @param[in]  This              Pointer to the EFI_UDP4_PROTOCOL instance.
206   @param[in]  JoinFlag          Set to TRUE to join a multicast group. Set to FALSE to leave one
207                                 or all multicast groups.
208   @param[in]  MulticastAddress  Pointer to multicast group address to join or leave.
209 
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:
216                                 - This is NULL.
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
221                                 JoinFlag is TRUE).
222   @retval EFI_NOT_FOUND         The group address is not in the group table (when JoinFlag is
223                                 FALSE).
224   @retval EFI_DEVICE_ERROR      An unexpected system or network error occurred.
225 
226 **/
227 EFI_STATUS
228 EFIAPI
229 Udp4Groups (
230   IN EFI_UDP4_PROTOCOL  *This,
231   IN BOOLEAN            JoinFlag,
232   IN EFI_IPv4_ADDRESS   *MulticastAddress OPTIONAL
233   );
234 
235 /**
236   Adds and deletes routing table entries.
237 
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
244   other routes.
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.
255 
256   @param[in]  This              Pointer to the EFI_UDP4_PROTOCOL instance.
257   @param[in]  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[in]  SubnetAddress     The destination network address that needs to be routed.
260   @param[in]  SubnetMask        The subnet mask of SubnetAddress.
261   @param[in]  GatewayAddress    The gateway IP address for this route.
262 
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.
271 
272 **/
273 EFI_STATUS
274 EFIAPI
275 Udp4Routes (
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
281   );
282 
283 /**
284   Queues outgoing data packets into the transmit queue.
285 
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
291   transmitting status.
292 
293   @param[in]  This              Pointer to the EFI_UDP4_PROTOCOL instance.
294   @param[in]  Token             Pointer to the completion token that will be placed into the
295                                 transmit queue.
296 
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.
311 
312 **/
313 EFI_STATUS
314 EFIAPI
315 Udp4Transmit (
316   IN EFI_UDP4_PROTOCOL          *This,
317   IN EFI_UDP4_COMPLETION_TOKEN  *Token
318   );
319 
320 /**
321   Places an asynchronous receive request into the receiving queue.
322 
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.
331 
332   @param[in]  This              Pointer to the EFI_UDP4_PROTOCOL instance.
333   @param[in]  Token             Pointer to a token that is associated with
334                                 the receive data descriptor.
335 
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.)
339                                 is not finished yet.
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
345                                 the receive queue.
346   @retval EFI_NOT_READY         The receive request could not be queued because the receive queue is full.
347 
348 **/
349 EFI_STATUS
350 EFIAPI
351 Udp4Receive (
352   IN EFI_UDP4_PROTOCOL          *This,
353   IN EFI_UDP4_COMPLETION_TOKEN  *Token
354   );
355 
356 /**
357   Aborts an asynchronous transmit or receive request.
358 
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.
365 
366   @param[in]  This  Pointer to the EFI_UDP4_PROTOCOL instance.
367   @param[in]  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
370                     tokens are aborted.
371 
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().
382 
383 **/
384 EFI_STATUS
385 EFIAPI
386 Udp4Cancel (
387   IN EFI_UDP4_PROTOCOL          *This,
388   IN EFI_UDP4_COMPLETION_TOKEN  *Token OPTIONAL
389   );
390 
391 /**
392   Polls for incoming data packets and processes outgoing data packets.
393 
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.
402 
403   @param[in]  This  Pointer to the EFI_UDP4_PROTOCOL instance.
404 
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.
409 
410 **/
411 EFI_STATUS
412 EFIAPI
413 Udp4Poll (
414   IN EFI_UDP4_PROTOCOL  *This
415   );
416 
417 /**
418   Create the Udp service context data.
419 
420   @param[in, out] Udp4Service      Pointer to the UDP4_SERVICE_DATA.
421   @param[in]      ImageHandle      The image handle of this udp4 driver.
422   @param[in]      ControllerHandle The controller handle this udp4 driver binds on.
423 
424   @retval EFI_SUCCESS              The udp4 service context data is created and
425                                    initialized.
426   @retval EFI_OUT_OF_RESOURCES     Cannot allocate memory.
427   @retval other                    Other error occurs.
428 
429 **/
430 EFI_STATUS
431 Udp4CreateService (
432   IN OUT UDP4_SERVICE_DATA  *Udp4Service,
433   IN     EFI_HANDLE         ImageHandle,
434   IN     EFI_HANDLE         ControllerHandle
435   );
436 
437 /**
438   Clean the Udp service context data.
439 
440   @param[in]  Udp4Service            Pointer to the UDP4_SERVICE_DATA.
441 
442 **/
443 VOID
444 Udp4CleanService (
445   IN UDP4_SERVICE_DATA  *Udp4Service
446   );
447 
448 /**
449   This function intializes the new created udp instance.
450 
451   @param[in]      Udp4Service       Pointer to the UDP4_SERVICE_DATA.
452   @param[in, out] Instance          Pointer to the un-initialized UDP4_INSTANCE_DATA.
453 
454 **/
455 VOID
456 Udp4InitInstance (
457   IN     UDP4_SERVICE_DATA   *Udp4Service,
458   IN OUT UDP4_INSTANCE_DATA  *Instance
459   );
460 
461 /**
462   This function cleans the udp instance.
463 
464   @param[in]  Instance               Pointer to the UDP4_INSTANCE_DATA to clean.
465 
466 **/
467 VOID
468 Udp4CleanInstance (
469   IN UDP4_INSTANCE_DATA  *Instance
470   );
471 
472 /**
473   This function tries to bind the udp instance according to the configured port
474   allocation strategy.
475 
476   @param[in]      InstanceList   Pointer to the head of the list linking the udp
477                                  instances.
478   @param[in, out] ConfigData     Pointer to the ConfigData of the instance to be
479                                  bound. ConfigData->StationPort will be assigned
480                                  with an available port value on success.
481 
482   @retval EFI_SUCCESS            The bound operation is completed successfully.
483   @retval EFI_ACCESS_DENIED      The <Address, Port> specified by the ConfigData is
484                                  already used by other instance.
485   @retval EFI_OUT_OF_RESOURCES   No available port resources.
486 
487 **/
488 EFI_STATUS
489 Udp4Bind (
490   IN     LIST_ENTRY            *InstanceList,
491   IN OUT EFI_UDP4_CONFIG_DATA  *ConfigData
492   );
493 
494 /**
495   This function is used to check whether the NewConfigData has any un-reconfigurable
496   parameters changed compared to the OldConfigData.
497 
498   @param[in]  OldConfigData      Pointer to the current ConfigData the udp instance
499                                  uses.
500   @param[in]  NewConfigData      Pointer to the new ConfigData.
501 
502   @retval TRUE     The instance is reconfigurable.
503   @retval FALSE    Otherwise.
504 
505 **/
506 BOOLEAN
507 Udp4IsReconfigurable (
508   IN EFI_UDP4_CONFIG_DATA  *OldConfigData,
509   IN EFI_UDP4_CONFIG_DATA  *NewConfigData
510   );
511 
512 /**
513   This function builds the Ip4 configdata from the Udp4ConfigData.
514 
515   @param[in]       Udp4ConfigData    Pointer to the EFI_UDP4_CONFIG_DATA.
516   @param[in, out]  Ip4ConfigData     Pointer to the EFI_IP4_CONFIG_DATA.
517 
518 **/
519 VOID
520 Udp4BuildIp4ConfigData (
521   IN     EFI_UDP4_CONFIG_DATA  *Udp4ConfigData,
522   IN OUT EFI_IP4_CONFIG_DATA   *Ip4ConfigData
523   );
524 
525 /**
526   This function validates the TxToken, it returns the error code according to the spec.
527 
528   @param[in]  Instance           Pointer to the udp instance context data.
529   @param[in]  TxToken            Pointer to the token to be checked.
530 
531   @retval EFI_SUCCESS            The TxToken is valid.
532   @retval EFI_INVALID_PARAMETER  One or more of the following are TRUE: This is
533                                  NULL. Token is NULL. Token.Event is NULL.
534                                  Token.Packet.TxData is NULL.
535                                  Token.Packet.TxData.FragmentCount is zero.
536                                  Token.Packet.TxData.DataLength is not equal to the
537                                  sum of fragment lengths. One or more of the
538                                  Token.Packet.TxData.FragmentTable[].
539                                  FragmentLength fields is zero. One or more of the
540                                  Token.Packet.TxData.FragmentTable[].
541                                  FragmentBuffer fields is NULL.
542                                  Token.Packet.TxData. GatewayAddress is not a
543                                  unicast IPv4 address if it is not NULL. One or
544                                  more IPv4 addresses in Token.Packet.TxData.
545                                  UdpSessionData are not valid unicast IPv4
546                                  addresses if the UdpSessionData is not NULL.
547   @retval EFI_BAD_BUFFER_SIZE    The data length is greater than the maximum UDP
548                                  packet size.
549 
550 **/
551 EFI_STATUS
552 Udp4ValidateTxToken (
553   IN UDP4_INSTANCE_DATA         *Instance,
554   IN EFI_UDP4_COMPLETION_TOKEN  *TxToken
555   );
556 
557 /**
558   This function checks whether the specified Token duplicates with the one in the Map.
559 
560   @param[in]  Map                Pointer to the NET_MAP.
561   @param[in]  Item               Pointer to the NET_MAP_ITEM contain the pointer to
562                                  the Token.
563   @param[in]  Context            Pointer to the Token to be checked.
564 
565   @retval EFI_SUCCESS            The Token specified by Context differs from the
566                                  one in the Item.
567   @retval EFI_ACCESS_DENIED      The Token duplicates with the one in the Item.
568 
569 **/
570 EFI_STATUS
571 EFIAPI
572 Udp4TokenExist (
573   IN NET_MAP       *Map,
574   IN NET_MAP_ITEM  *Item,
575   IN VOID          *Context
576   );
577 
578 /**
579   This function calculates the checksum for the Packet, utilizing the pre-calculated
580   pseudo HeadSum to reduce some overhead.
581 
582   @param[in]  Packet             Pointer to the NET_BUF contains the udp datagram.
583   @param[in]  HeadSum            Checksum of the pseudo header execpt the length
584                                  field.
585 
586   @retval The 16-bit checksum of this udp datagram.
587 
588 **/
589 UINT16
590 Udp4Checksum (
591   IN NET_BUF *Packet,
592   IN UINT16  HeadSum
593   );
594 
595 /**
596   This function removes the specified Token from the TokenMap.
597 
598   @param[in, out] TokenMap       Pointer to the NET_MAP containing the tokens.
599   @param[in]      Token          Pointer to the Token to be removed.
600 
601   @retval EFI_SUCCESS            The specified Token is removed from the TokenMap.
602   @retval EFI_NOT_FOUND          The specified Token is not found in the TokenMap.
603 
604 **/
605 EFI_STATUS
606 Udp4RemoveToken (
607   IN OUT NET_MAP                    *TokenMap,
608   IN     EFI_UDP4_COMPLETION_TOKEN  *Token
609   );
610 
611 /**
612   This function removes the multicast group specified by Arg from the Map.
613 
614   @param[in, out] Map            Pointer to the NET_MAP.
615   @param[in]      Item           Pointer to the NET_MAP_ITEM.
616   @param[in]      Arg            Pointer to the Arg, it's the pointer to a
617                                  multicast IPv4 Address.
618 
619   @retval EFI_SUCCESS            The multicast address is removed.
620   @retval EFI_ABORTED            The specified multicast address is removed and the
621                                  Arg is not NULL.
622 
623 **/
624 EFI_STATUS
625 EFIAPI
626 Udp4LeaveGroup (
627   IN OUT NET_MAP       *Map,
628   IN     NET_MAP_ITEM  *Item,
629   IN     VOID          *Arg OPTIONAL
630   );
631 
632 /**
633   This function removes all the Wrap datas in the RcvdDgramQue.
634 
635   @param[in]  Instance           Pointer to the udp instance context data.
636 
637 **/
638 VOID
639 Udp4FlushRcvdDgram (
640   IN UDP4_INSTANCE_DATA  *Instance
641   );
642 
643 /**
644   Cancel Udp4 tokens from the Udp4 instance.
645 
646   @param[in]  Instance           Pointer to the udp instance context data.
647   @param[in]  Token              Pointer to the token to be canceled, if NULL, all
648                                  tokens in this instance will be cancelled.
649 
650   @retval EFI_SUCCESS            The Token is cancelled.
651   @retval EFI_NOT_FOUND          The Token is not found.
652 
653 **/
654 EFI_STATUS
655 Udp4InstanceCancelToken (
656   IN UDP4_INSTANCE_DATA         *Instance,
657   IN EFI_UDP4_COMPLETION_TOKEN  *Token OPTIONAL
658   );
659 
660 /**
661   This function delivers the received datagrams for the specified instance.
662 
663   @param[in]  Instance               Pointer to the instance context data.
664 
665 **/
666 VOID
667 Udp4InstanceDeliverDgram (
668   IN UDP4_INSTANCE_DATA  *Instance
669   );
670 
671 /**
672   This function reports the received ICMP error.
673 
674   @param[in]  Instance               Pointer to the udp instance context data.
675 
676 **/
677 VOID
678 Udp4ReportIcmpError (
679   IN UDP4_INSTANCE_DATA  *Instance
680   );
681 
682 /**
683   This function is a dummy ext-free function for the NET_BUF created for the output
684   udp datagram.
685 
686   @param[in]  Context                Pointer to the context data.
687 
688 **/
689 VOID
690 EFIAPI
691 Udp4NetVectorExtFree (
692   VOID  *Context
693   );
694 
695 #endif
696