1 /** @file
2   This library is used to share code between UEFI network stack modules.
3   It provides the helper routines to access UDP service. It is used by both DHCP and MTFTP.
4 
5 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7 
8 **/
9 
10 #ifndef _UDP_IO_H_
11 #define _UDP_IO_H_
12 
13 #include <Protocol/Udp4.h>
14 #include <Protocol/Udp6.h>
15 
16 #include <Library/NetLib.h>
17 
18 typedef struct _UDP_IO UDP_IO;
19 
20 ///
21 /// Signatures used by UdpIo Library.
22 ///
23 
24 #define UDP_IO_RX_SIGNATURE  SIGNATURE_32 ('U', 'D', 'P', 'R')
25 #define UDP_IO_TX_SIGNATURE  SIGNATURE_32 ('U', 'D', 'P', 'T')
26 #define UDP_IO_SIGNATURE     SIGNATURE_32 ('U', 'D', 'P', 'I')
27 
28 #define UDP_IO_UDP4_VERSION  4
29 #define UDP_IO_UDP6_VERSION  6
30 
31 ///
32 /// The UDP address pair.
33 ///
34 typedef struct {
35   EFI_IP_ADDRESS            LocalAddr;
36   UINT16                    LocalPort;
37   EFI_IP_ADDRESS            RemoteAddr;
38   UINT16                    RemotePort;
39 } UDP_END_POINT;
40 
41 /**
42   Prototype called when receiving or sending packets to or from a UDP point.
43 
44   This prototype is used by both receive and sending when calling
45   UdpIoRecvDatagram() or UdpIoSendDatagram(). When receiving, Netbuf is allocated by the
46   UDP access point and released by the user. When sending, the user allocates the the NetBuf,
47   which is then provided to the callback as a reference.
48 
49   @param[in] Packet       The packet received or sent.
50   @param[in] EndPoint     The UDP address pair corresponds to the UDP IO.
51   @param[in] IoStatus     The packet receiving or sending status.
52   @param[in] Context      The user-defined data when calling UdpIoRecvDatagram() or
53                           UdpIoSendDatagram().
54 **/
55 typedef
56 VOID
57 (EFIAPI *UDP_IO_CALLBACK) (
58   IN NET_BUF                *Packet,
59   IN UDP_END_POINT          *EndPoint,
60   IN EFI_STATUS             IoStatus,
61   IN VOID                   *Context
62   );
63 
64 ///
65 /// This structure is used internally by the UdpIo Library.
66 ///
67 /// Each receive request is wrapped in an UDP_RX_TOKEN. Upon completion,
68 /// the CallBack will be called. Only one receive request is sent to UDP at a
69 /// time. HeadLen gives the length of the application's header. UDP_IO will
70 /// make the application's header continuous before delivering up.
71 ///
72 typedef union {
73   EFI_UDP4_COMPLETION_TOKEN   Udp4;
74   EFI_UDP6_COMPLETION_TOKEN   Udp6;
75 } UDP_COMPLETION_TOKEN;
76 
77 typedef struct {
78   UINT32                      Signature;
79   UDP_IO                      *UdpIo;
80 
81   UDP_IO_CALLBACK             CallBack;
82   VOID                        *Context;
83   UINT32                      HeadLen;
84 
85   UDP_COMPLETION_TOKEN        Token;
86 } UDP_RX_TOKEN;
87 
88 
89 
90 ///
91 /// This structure is used internally by UdpIo Library.
92 ///
93 /// Each transmit request is wrapped in an UDP_TX_TOKEN. Upon completion,
94 /// the CallBack will be called. There can be several transmit requests. All transmit
95 /// requests are linked in a list.
96 ///
97 
98 typedef union {
99   EFI_UDP4_SESSION_DATA       Udp4;
100   EFI_UDP6_SESSION_DATA       Udp6;
101 } UDP_SESSION_DATA;
102 
103 typedef union {
104   EFI_UDP4_TRANSMIT_DATA      Udp4;
105   EFI_UDP6_TRANSMIT_DATA      Udp6;
106 } UDP_TRANSMIT_DATA;
107 
108 typedef struct {
109   UINT32                      Signature;
110   LIST_ENTRY                  Link;
111   UDP_IO                      *UdpIo;
112   UDP_IO_CALLBACK             CallBack;
113   NET_BUF                     *Packet;
114   VOID                        *Context;
115   EFI_IPv4_ADDRESS            Gateway;
116   UDP_SESSION_DATA            Session;
117   UDP_COMPLETION_TOKEN        Token;
118   UDP_TRANSMIT_DATA           Data;
119 } UDP_TX_TOKEN;
120 
121 ///
122 /// Type defined as UDP_IO.
123 ///
124 /// This data structure wraps the UDP instance and configuration.
125 /// UdpIo Library uses this structure for all Udp4 or Udp6 operations.
126 ///
127 struct _UDP_IO {
128   UINT32                    Signature;
129   LIST_ENTRY                Link;
130   INTN                      RefCnt;
131   UINT8                     UdpVersion;
132 
133   //
134   // Handle used to create/destroy UDP child
135   //
136   EFI_HANDLE                Controller;
137   EFI_HANDLE                Image;
138   EFI_HANDLE                UdpHandle;
139 
140   EFI_SIMPLE_NETWORK_MODE   SnpMode;
141 
142   LIST_ENTRY                SentDatagram;   ///< A list of UDP_TX_TOKEN.
143   UDP_RX_TOKEN              *RecvRequest;
144 
145   union {
146     EFI_UDP4_PROTOCOL       *Udp4;
147     EFI_UDP6_PROTOCOL       *Udp6;
148   } Protocol;
149 
150   union {
151     EFI_UDP4_CONFIG_DATA    Udp4;
152     EFI_UDP6_CONFIG_DATA    Udp6;
153   } Config;
154 };
155 
156 /**
157   The prototype called when UdpIo Library configures a UDP instance.
158 
159   The prototype is set and called when creating a UDP_IO in UdpIoCreatePort().
160 
161   @param[in] UdpIo         The UDP_IO to configure.
162   @param[in] Context       The user-defined data when calling UdpIoCreatePort().
163 
164   @retval EFI_SUCCESS  The configuration succeeded.
165   @retval Others       The UDP_IO fails to configure indicating
166                        UdpIoCreatePort() should fail.
167 **/
168 typedef
169 EFI_STATUS
170 (EFIAPI *UDP_IO_CONFIG) (
171   IN UDP_IO                 *UdpIo,
172   IN VOID                   *Context
173   );
174 
175 /**
176   The select function to decide whether to cancel the UDP_TX_TOKEN.
177 
178   @param[in] Token        The UDP_TX_TOKEN to decide whether to cancel.
179   @param[in] Context      User-defined data in UdpIoCancelDgrams().
180 
181   @retval TRUE        Cancel the UDP_TX_TOKEN.
182   @retval FALSE       Do not cancel this UDP_TX_TOKEN.
183 
184 **/
185 typedef
186 BOOLEAN
187 (EFIAPI *UDP_IO_TO_CANCEL) (
188   IN UDP_TX_TOKEN           *Token,
189   IN VOID                   *Context
190   );
191 
192 /**
193   Cancel all the sent datagram that pass the selection criteria of ToCancel.
194 
195   If ToCancel is NULL, all the datagrams are cancelled.
196   If Udp version is not UDP_IO_UDP4_VERSION or UDP_IO_UDP6_VERSION, then ASSERT().
197 
198   @param[in]  UdpIo                 The UDP_IO to cancel packet.
199   @param[in]  IoStatus              The IoStatus to return to the packet owners.
200   @param[in]  ToCancel              The select funtion to test whether to cancel this
201                                     packet or not.
202   @param[in]  Context               The opaque parameter to the ToCancel.
203 
204 **/
205 VOID
206 EFIAPI
207 UdpIoCancelDgrams (
208   IN UDP_IO                 *UdpIo,
209   IN EFI_STATUS             IoStatus,
210   IN UDP_IO_TO_CANCEL       ToCancel,        OPTIONAL
211   IN VOID                   *Context         OPTIONAL
212   );
213 
214 /**
215   Creates a UDP_IO to access the UDP service. It creates and configures
216   a UDP child.
217 
218   If Configure is NULL, then ASSERT().
219   If Udp version is not UDP_IO_UDP4_VERSION or UDP_IO_UDP6_VERSION, then ASSERT().
220 
221   It locates the UDP service binding prototype on the Controller parameter
222   uses the UDP service binding prototype to create a UDP child (also known as
223   a UDP instance) configures the UDP child by calling Configure function prototype.
224   Any failures in creating or configuring the UDP child return NULL for failure.
225 
226   @param[in]  Controller            The controller that has the UDP service binding.
227                                     protocol installed.
228   @param[in]  ImageHandle           The image handle for the driver.
229   @param[in]  Configure             The function to configure the created UDP child.
230   @param[in]  UdpVersion            The UDP protocol version, UDP4 or UDP6.
231   @param[in]  Context               The opaque parameter for the Configure funtion.
232 
233   @return The newly-created UDP_IO, or NULL if failed.
234 
235 **/
236 UDP_IO *
237 EFIAPI
238 UdpIoCreateIo (
239   IN  EFI_HANDLE            Controller,
240   IN  EFI_HANDLE            ImageHandle,
241   IN  UDP_IO_CONFIG         Configure,
242   IN  UINT8                 UdpVersion,
243   IN  VOID                  *Context
244   );
245 
246 /**
247   Free the UDP_IO and all its related resources.
248 
249   If Udp version is not UDP_IO_UDP4_VERSION or UDP_IO_UDP6_VERSION, then ASSERT().
250 
251   The function cancels all sent datagrams and receive requests.
252 
253   @param[in]  UdpIo             The UDP_IO to free.
254 
255   @retval EFI_SUCCESS           The UDP_IO is freed.
256   @retval Others                Failed to free UDP_IO.
257 
258 **/
259 EFI_STATUS
260 EFIAPI
261 UdpIoFreeIo (
262   IN  UDP_IO                *UdpIo
263   );
264 
265 /**
266   Cleans up the UDP_IO without freeing it. Call this function
267   if you intend to later re-use the UDP_IO.
268 
269   If Udp version is not UDP_IO_UDP4_VERSION or UDP_IO_UDP6_VERSION, then ASSERT().
270 
271   This function releases all transmitted datagrams and receive requests and configures NULL for the UDP instance.
272 
273   @param[in]  UdpIo                 The UDP_IO to clean up.
274 
275 **/
276 VOID
277 EFIAPI
278 UdpIoCleanIo (
279   IN  UDP_IO                *UdpIo
280   );
281 
282 /**
283   Send a packet through the UDP_IO.
284 
285   If Udp version is not UDP_IO_UDP4_VERSION or UDP_IO_UDP6_VERSION, then ASSERT().
286 
287   The packet will be wrapped in UDP_TX_TOKEN. Function Callback will be called
288   when the packet is sent. The optional parameter EndPoint overrides the default
289   address pair if specified.
290 
291   @param[in]  UdpIo                 The UDP_IO to send the packet through.
292   @param[in]  Packet                The packet to send.
293   @param[in]  EndPoint              The local and remote access point. Override the
294                                     default address pair set during configuration.
295   @param[in]  Gateway               The gateway to use.
296   @param[in]  CallBack              The function being called when packet is
297                                     transmitted or failed.
298   @param[in]  Context               The opaque parameter passed to CallBack.
299 
300   @retval EFI_OUT_OF_RESOURCES  Failed to allocate resource for the packet.
301   @retval EFI_SUCCESS           The packet is successfully delivered to UDP for
302                                 transmission.
303 
304 **/
305 EFI_STATUS
306 EFIAPI
307 UdpIoSendDatagram (
308   IN  UDP_IO                *UdpIo,
309   IN  NET_BUF               *Packet,
310   IN  UDP_END_POINT         *EndPoint OPTIONAL,
311   IN  EFI_IP_ADDRESS        *Gateway  OPTIONAL,
312   IN  UDP_IO_CALLBACK       CallBack,
313   IN  VOID                  *Context
314   );
315 
316 /**
317   Cancel a single sent datagram.
318 
319   @param[in]  UdpIo                 The UDP_IO from which to cancel the packet
320   @param[in]  Packet                The packet to cancel
321 
322 **/
323 VOID
324 EFIAPI
325 UdpIoCancelSentDatagram (
326   IN  UDP_IO                *UdpIo,
327   IN  NET_BUF               *Packet
328   );
329 
330 /**
331   Issue a receive request to the UDP_IO.
332 
333   If Udp version is not UDP_IO_UDP4_VERSION or UDP_IO_UDP6_VERSION, then ASSERT().
334 
335   This function is called when upper-layer needs packet from UDP for processing.
336   Only one receive request is acceptable at a time. Therefore, one common usage model is
337   to invoke this function inside its Callback function when the former packet
338   is processed.
339 
340   @param[in]  UdpIo                 The UDP_IO to receive the packet from.
341   @param[in]  CallBack              The call back function to execute when the packet
342                                     is received.
343   @param[in]  Context               The opaque context passed to Callback.
344   @param[in]  HeadLen               The length of the upper-layer's protocol header.
345 
346   @retval EFI_ALREADY_STARTED   There is already a pending receive request. Only
347                                 one receive request is supported at a time.
348   @retval EFI_OUT_OF_RESOURCES  Failed to allocate needed resources.
349   @retval EFI_SUCCESS           The receive request was issued successfully.
350   @retval EFI_UNSUPPORTED       The UDP version in UDP_IO is not supported.
351 
352 **/
353 EFI_STATUS
354 EFIAPI
355 UdpIoRecvDatagram (
356   IN  UDP_IO                *UdpIo,
357   IN  UDP_IO_CALLBACK       CallBack,
358   IN  VOID                  *Context,
359   IN  UINT32                HeadLen
360   );
361 
362 #endif
363 
364