]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Include/Library/IpIoLib.h
537915b882ff2458622c9670fc0bf844f9e771ae
[mirror_edk2.git] / MdeModulePkg / Include / Library / IpIoLib.h
1 /** @file
2 This library is only intended to be used by UEFI network stack modules.
3 It provides the combined IpIo layer on the EFI IP4 Protocol and EFI IP6 protocol.
4
5 Copyright (c) 2005 - 2010, Intel Corporation. All rights reserved<BR>
6 This program and the accompanying materials are licensed and made available under
7 the terms and conditions of the BSD License that accompanies this distribution.
8 The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php.
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #ifndef _IP_IO_H_
17 #define _IP_IO_H_
18
19 #include <Protocol/Ip4.h>
20 #include <Protocol/Ip6.h>
21
22 #include <Library/NetLib.h>
23
24 //
25 // type and code define for ICMP protocol error
26 // from IP
27 //
28 #define ICMP_TYPE_UNREACH 3
29 #define ICMP_TYPE_TIMXCEED 11
30 #define ICMP_TYPE_PARAMPROB 12
31 #define ICMP_TYPE_SOURCEQUENCH 4
32
33 #define ICMP_CODE_UNREACH_NET 0
34 #define ICMP_CODE_UNREACH_HOST 1
35 #define ICMP_CODE_UNREACH_PROTOCOL 2
36 #define ICMP_CODE_UNREACH_PORT 3
37 #define ICMP_CODE_UNREACH_NEEDFRAG 4
38 #define ICMP_CODE_UNREACH_SRCFAIL 5
39 #define ICMP_CODE_UNREACH_NET_UNKNOWN 6
40 #define ICMP_CODE_UNREACH_HOST_UNKNOWN 7
41 #define ICMP_CODE_UNREACH_ISOLATED 8
42 #define ICMP_CODE_UNREACH_NET_PROHIB 9
43 #define ICMP_CODE_UNREACH_HOST_PROHIB 10
44 #define ICMP_CODE_UNREACH_TOSNET 11
45 #define ICMP_CODE_UNREACH_TOSHOST 12
46
47 /**
48 Get the IP header length from the struct EFI_IP4_HEADER. HeaderLength is
49 Internet header length in 32-bit words, so HeaderLength<<2 is the real
50 length of IP header.
51
52 @param[out] HdrPtr A pointer to EFI_IP4_HEADER.
53
54 @return The IP header length.
55 **/
56 #define EFI_IP4_HEADER_LEN(HdrPtr) ((HdrPtr)->HeaderLength << 2)
57
58 /**
59 To types of ICMP error which consist of ICMP header, IP header and original
60 datagram's data, get length from sum of ICMP header length, IP header length
61 and first 64 bits of datagram's data length.
62
63 @param[in] IpHdr A pointer to EFI_IP4_HEADER.
64
65 @return The ICMP error length.
66 **/
67 #define ICMP_ERRLEN(IpHdr) \
68 (sizeof(IP4_ICMP_HEAD) + EFI_IP4_HEADER_LEN(IpHdr) + 8)
69
70 /**
71 Get the packet header from NET_BUF.
72
73 @param[out] Buf A pointer to NET_BUF.
74 @param[in] Type Header type.
75
76 @return The pointer to packet header.
77 **/
78 #define NET_PROTO_HDR(Buf, Type) ((Type *) ((Buf)->BlockOp[0].Head))
79
80
81 extern EFI_IP4_CONFIG_DATA mIp4IoDefaultIpConfigData;
82 extern EFI_IP6_CONFIG_DATA mIp6IoDefaultIpConfigData;
83
84
85 ///
86 /// This error will be delivered to the
87 /// listening transportation layer protocol
88 /// that consumes IpIO.
89 ///
90
91 #define ICMP_ERR_UNREACH_NET 0
92 #define ICMP_ERR_UNREACH_HOST 1
93 #define ICMP_ERR_UNREACH_PROTOCOL 2
94 #define ICMP_ERR_UNREACH_PORT 3
95 #define ICMP_ERR_MSGSIZE 4
96 #define ICMP_ERR_UNREACH_SRCFAIL 5
97 #define ICMP_ERR_TIMXCEED_INTRANS 6
98 #define ICMP_ERR_TIMXCEED_REASS 7
99 #define ICMP_ERR_QUENCH 8
100 #define ICMP_ERR_PARAMPROB 9
101
102 #define ICMP6_ERR_UNREACH_NET 0
103 #define ICMP6_ERR_UNREACH_HOST 1
104 #define ICMP6_ERR_UNREACH_PROTOCOL 2
105 #define ICMP6_ERR_UNREACH_PORT 3
106 #define ICMP6_ERR_PACKAGE_TOOBIG 4
107 #define ICMP6_ERR_TIMXCEED_HOPLIMIT 5
108 #define ICMP6_ERR_TIMXCEED_REASS 6
109 #define ICMP6_ERR_PARAMPROB_HEADER 7
110 #define ICMP6_ERR_PARAMPROB_NEXHEADER 8
111 #define ICMP6_ERR_PARAMPROB_IPV6OPTION 9
112
113 ///
114 /// The helper struct for IpIoGetIcmpErrStatus(). It is for internal use only.
115 ///
116 typedef struct {
117 BOOLEAN IsHard;
118 BOOLEAN Notify;
119 } ICMP_ERROR_INFO;
120
121 typedef union {
122 EFI_IP4_COMPLETION_TOKEN Ip4Token;
123 EFI_IP6_COMPLETION_TOKEN Ip6Token;
124 } IP_IO_IP_COMPLETION_TOKEN;
125
126 typedef union {
127 EFI_IP4_TRANSMIT_DATA Ip4TxData;
128 EFI_IP6_TRANSMIT_DATA Ip6TxData;
129 } IP_IO_IP_TX_DATA;
130
131 typedef union {
132 EFI_IP4_RECEIVE_DATA Ip4RxData;
133 EFI_IP6_RECEIVE_DATA Ip6RxData;
134 } IP_IO_IP_RX_DATA;
135
136 typedef union {
137 EFI_IP4_OVERRIDE_DATA Ip4OverrideData;
138 EFI_IP6_OVERRIDE_DATA Ip6OverrideData;
139 } IP_IO_OVERRIDE;
140
141 typedef union {
142 EFI_IP4_CONFIG_DATA Ip4CfgData;
143 EFI_IP6_CONFIG_DATA Ip6CfgData;
144 } IP_IO_IP_CONFIG_DATA;
145
146 typedef union {
147 EFI_IP4_HEADER *Ip4Hdr;
148 EFI_IP6_HEADER *Ip6Hdr;
149 } IP_IO_IP_HEADER;
150
151 typedef union {
152 IP4_ADDR SubnetMask;
153 UINT8 PrefixLength;
154 } IP_IO_IP_MASK;
155
156 ///
157 /// The IP session for an IP receive packet.
158 ///
159 typedef struct _EFI_NET_SESSION_DATA {
160 EFI_IP_ADDRESS Source; ///< Source IP of the received packet.
161 EFI_IP_ADDRESS Dest; ///< Destination IP of the received packet.
162 IP_IO_IP_HEADER IpHdr; ///< IP header of the received packet.
163 UINT32 IpHdrLen; ///< IP header length of the received packet.
164 ///< For IPv6, it includes the IP6 header
165 ///< length and extension header length. For
166 ///< IPv4, it includes the IP4 header length
167 ///< and options length.
168 UINT8 IpVersion; ///< The IP version of the received packet.
169 } EFI_NET_SESSION_DATA;
170
171 /**
172 The prototype is called back when an IP packet is received.
173
174 @param[in] Status The result of the receive request.
175 @param[in] IcmpErr Valid when Status is EFI_ICMP_ERROR.
176 @param[in] NetSession The IP session for the received packet.
177 @param[in] Pkt The packet received.
178 @param[in] Context The data provided by the user for the received packet when
179 the callback is registered in IP_IO_OPEN_DATA::RcvdContext.
180
181 **/
182 typedef
183 VOID
184 (EFIAPI *PKT_RCVD_NOTIFY) (
185 IN EFI_STATUS Status,
186 IN UINT8 IcmpErr,
187 IN EFI_NET_SESSION_DATA *NetSession,
188 IN NET_BUF *Pkt,
189 IN VOID *Context
190 );
191
192 /**
193 The prototype is called back when an IP packet is sent.
194
195 @param[in] Status Result of the IP packet being sent.
196 @param[in] Context The data provided by user for the received packet when
197 the callback is registered in IP_IO_OPEN_DATA::SndContext.
198 @param[in] Sender A pointer to EFI_IP4_PROTOCOL or EFI_IP6_PROTOCOL.
199 @param[in] NotifyData The Context data specified when calling IpIoSend()
200
201 **/
202 typedef
203 VOID
204 (EFIAPI *PKT_SENT_NOTIFY) (
205 IN EFI_STATUS Status,
206 IN VOID *Context,
207 IN VOID *Sender,
208 IN VOID *NotifyData
209 );
210
211 ///
212 /// This data structure wraps Ip4/Ip6 instances. The IpIo Library uses it for all
213 /// Ip4/Ip6 operations.
214 ///
215 typedef struct _IP_IO {
216 ///
217 /// The node used to link this IpIo to the active IpIo list.
218 ///
219 LIST_ENTRY Entry;
220
221 ///
222 /// The list used to maintain the IP instance for different sending purpose.
223 ///
224 LIST_ENTRY IpList;
225
226 EFI_HANDLE Controller;
227 EFI_HANDLE Image;
228 EFI_HANDLE ChildHandle;
229 //
230 // The IP instance consumed by this IP_IO
231 //
232 VOID *Ip;
233 BOOLEAN IsConfigured;
234
235 ///
236 /// Some ip configuration data can be changed.
237 ///
238 UINT8 Protocol;
239
240 ///
241 /// Token and event used to get data from IP.
242 ///
243 IP_IO_IP_COMPLETION_TOKEN RcvToken;
244
245 ///
246 /// List entry used to link the token passed to IP_IO.
247 ///
248 LIST_ENTRY PendingSndList;
249
250 //
251 // User interface used to get notify from IP_IO
252 //
253 VOID *RcvdContext; ///< See IP_IO_OPEN_DATA::RcvdContext.
254 VOID *SndContext; ///< See IP_IO_OPEN_DATA::SndContext.
255 PKT_RCVD_NOTIFY PktRcvdNotify; ///< See IP_IO_OPEN_DATA::PktRcvdNotify.
256 PKT_SENT_NOTIFY PktSentNotify; ///< See IP_IO_OPEN_DATA::PktSentNotify.
257 UINT8 IpVersion;
258 } IP_IO;
259
260 ///
261 /// The struct is for the user to pass IP configuration and callbacks to IP_IO.
262 /// It is used by IpIoOpen().
263 ///
264 typedef struct _IP_IO_OPEN_DATA {
265 IP_IO_IP_CONFIG_DATA IpConfigData; ///< Configuration of the IP instance.
266 VOID *RcvdContext; ///< Context data used by receive callback.
267 VOID *SndContext; ///< Context data used by send callback.
268 PKT_RCVD_NOTIFY PktRcvdNotify; ///< Receive callback.
269 PKT_SENT_NOTIFY PktSentNotify; ///< Send callback.
270 } IP_IO_OPEN_DATA;
271
272 ///
273 /// Internal struct book-keeping send request of IP_IO.
274 ///
275 /// An IP_IO_SEND_ENTRY will be created each time a send request is issued to
276 /// IP_IO via IpIoSend().
277 ///
278 typedef struct _IP_IO_SEND_ENTRY {
279 LIST_ENTRY Entry;
280 IP_IO *IpIo;
281 VOID *Context;
282 VOID *NotifyData;
283 VOID *Ip;
284 NET_BUF *Pkt;
285 IP_IO_IP_COMPLETION_TOKEN SndToken;
286 } IP_IO_SEND_ENTRY;
287
288 ///
289 /// The IP_IO_IP_INFO is used in IpIoSend() to override the default IP instance
290 /// in IP_IO.
291 ///
292 typedef struct _IP_IO_IP_INFO {
293 EFI_IP_ADDRESS Addr;
294 IP_IO_IP_MASK PreMask;
295 LIST_ENTRY Entry;
296 EFI_HANDLE ChildHandle;
297 VOID *Ip;
298 IP_IO_IP_COMPLETION_TOKEN DummyRcvToken;
299 INTN RefCnt;
300 UINT8 IpVersion;
301 } IP_IO_IP_INFO;
302
303 /**
304 Create a new IP_IO instance.
305
306 This function uses IP4/IP6 service binding protocol in Controller to create
307 an IP4/IP6 child (aka IP4/IP6 instance).
308
309 @param[in] Image The image handle of the driver or application that
310 consumes IP_IO.
311 @param[in] Controller The controller handle that has IP4 or IP6 service
312 binding protocol installed.
313 @param[in] IpVersion The version of the IP protocol to use, either
314 IPv4 or IPv6.
315
316 @return The pointer to a newly created IP_IO instance, or NULL if failed.
317
318 **/
319 IP_IO *
320 EFIAPI
321 IpIoCreate (
322 IN EFI_HANDLE Image,
323 IN EFI_HANDLE Controller,
324 IN UINT8 IpVersion
325 );
326
327 /**
328 Destroy an IP_IO instance.
329
330 This function is paired with IpIoCreate(). The IP_IO will be closed first.
331 Resource will be freed afterwards. See IpIoClose().
332
333 @param[in, out] IpIo The pointer to the IP_IO instance that needs to be
334 destroyed.
335
336 @retval EFI_SUCCESS The IP_IO instance was destroyed successfully.
337 @retval Others An error condition occurred.
338
339 **/
340 EFI_STATUS
341 EFIAPI
342 IpIoDestroy (
343 IN OUT IP_IO *IpIo
344 );
345
346 /**
347 Stop an IP_IO instance.
348
349 This function is paired with IpIoOpen(). The IP_IO will be unconfigured, and all
350 pending send/receive tokens will be canceled.
351
352 @param[in, out] IpIo The pointer to the IP_IO instance that needs to stop.
353
354 @retval EFI_SUCCESS The IP_IO instance stopped successfully.
355 @retval Others Anrror condition occurred.
356
357 **/
358 EFI_STATUS
359 EFIAPI
360 IpIoStop (
361 IN OUT IP_IO *IpIo
362 );
363
364 /**
365 Open an IP_IO instance for use.
366
367 This function is called after IpIoCreate(). It is used for configuring the IP
368 instance and register the callbacks and their context data for sending and
369 receiving IP packets.
370
371 @param[in, out] IpIo The pointer to an IP_IO instance that needs
372 to open.
373 @param[in] OpenData The configuration data and callbacks for
374 the IP_IO instance.
375
376 @retval EFI_SUCCESS The IP_IO instance opened with OpenData
377 successfully.
378 @retval EFI_ACCESS_DENIED The IP_IO instance is configured; avoid
379 reopening it.
380 @retval Others An error condition occurred.
381
382 **/
383 EFI_STATUS
384 EFIAPI
385 IpIoOpen (
386 IN OUT IP_IO *IpIo,
387 IN IP_IO_OPEN_DATA *OpenData
388 );
389
390 /**
391 Send out an IP packet.
392
393 This function is called after IpIoOpen(). The data to be sent are wrapped in
394 Pkt. The IP instance wrapped in IpIo is used for sending by default, but can be
395 overriden by Sender. Other sending configurations, such as source address and gateway
396 address, are specified in OverrideData.
397
398 @param[in, out] IpIo The pointer to an IP_IO instance used for sending IP
399 packet.
400 @param[in, out] Pkt The pointer to the IP packet to be sent.
401 @param[in] Sender Optional. The IP protocol instance used for sending.
402 @param[in] Context The optional context data.
403 @param[in] NotifyData The optional notify data.
404 @param[in] Dest The destination IP address to send this packet to.
405 @param[in] OverrideData The data to override some configuration of the IP
406 instance used for sending.
407
408 @retval EFI_SUCCESS The operation completed successfully.
409 @retval EFI_NOT_STARTED The IpIo is not configured.
410 @retval EFI_OUT_OF_RESOURCES Failed due to resource limitations.
411
412 **/
413 EFI_STATUS
414 EFIAPI
415 IpIoSend (
416 IN OUT IP_IO *IpIo,
417 IN OUT NET_BUF *Pkt,
418 IN IP_IO_IP_INFO *Sender OPTIONAL,
419 IN VOID *Context OPTIONAL,
420 IN VOID *NotifyData OPTIONAL,
421 IN EFI_IP_ADDRESS *Dest,
422 IN IP_IO_OVERRIDE *OverrideData OPTIONAL
423 );
424
425 /**
426 Cancel the IP transmit token that wraps this Packet.
427
428 @param[in] IpIo The pointer to the IP_IO instance.
429 @param[in] Packet The pointer to the packet of NET_BUF to cancel.
430
431 **/
432 VOID
433 EFIAPI
434 IpIoCancelTxToken (
435 IN IP_IO *IpIo,
436 IN VOID *Packet
437 );
438
439 /**
440 Add a new IP instance for sending data.
441
442 The function is used to add the IP_IO to the IP_IO sending list. The caller
443 can later use IpIoFindSender() to get the IP_IO and call IpIoSend() to send
444 data.
445
446 @param[in, out] IpIo The pointer to an IP_IO instance to add a new IP
447 instance for sending purposes.
448
449 @return The pointer to the created IP_IO_IP_INFO structure; NULL if failed.
450
451 **/
452 IP_IO_IP_INFO *
453 EFIAPI
454 IpIoAddIp (
455 IN OUT IP_IO *IpIo
456 );
457
458 /**
459 Configure the IP instance of this IpInfo and start the receiving if IpConfigData
460 is not NULL.
461
462 @param[in, out] IpInfo The pointer to the IP_IO_IP_INFO instance.
463 @param[in, out] IpConfigData The IP4 or IP6 configure data used to configure
464 the IP instance. If NULL, the IP instance is reset.
465 If UseDefaultAddress is set to TRUE, and the configure
466 operation succeeds, the default address information
467 is written back in this IpConfigData.
468
469 @retval EFI_SUCCESS The IP instance of this IpInfo was configured successfully,
470 or there is no need to reconfigure it.
471 @retval Others The configuration failed.
472
473 **/
474 EFI_STATUS
475 EFIAPI
476 IpIoConfigIp (
477 IN OUT IP_IO_IP_INFO *IpInfo,
478 IN OUT VOID *IpConfigData OPTIONAL
479 );
480
481 /**
482 Destroy an IP instance maintained in IpIo->IpList for
483 sending purpose.
484
485 This function pairs with IpIoAddIp(). The IpInfo is previously created by
486 IpIoAddIp(). The IP_IO_IP_INFO::RefCnt is decremented and the IP instance
487 will be dstroyed if the RefCnt is zero.
488
489 @param[in] IpIo The pointer to the IP_IO instance.
490 @param[in] IpInfo The pointer to the IpInfo to be removed.
491
492 **/
493 VOID
494 EFIAPI
495 IpIoRemoveIp (
496 IN IP_IO *IpIo,
497 IN IP_IO_IP_INFO *IpInfo
498 );
499
500 /**
501 Find the first IP protocol maintained in IpIo whose local
502 address is the same as Src.
503
504 This function is called when the caller needs the IpIo to send data to the
505 specified Src. The IpIo was added previously by IpIoAddIp().
506
507 @param[in, out] IpIo The pointer to the pointer of the IP_IO instance.
508 @param[in] IpVersion The version of the IP protocol to use, either
509 IPv4 or IPv6.
510 @param[in] Src The local IP address.
511
512 @return The pointer to the IP protocol can be used for sending purpose and its local
513 address is the same with Src.
514
515 **/
516 IP_IO_IP_INFO *
517 EFIAPI
518 IpIoFindSender (
519 IN OUT IP_IO **IpIo,
520 IN UINT8 IpVersion,
521 IN EFI_IP_ADDRESS *Src
522 );
523
524 /**
525 Get the ICMP error map information.
526
527 The ErrorStatus will be returned. The IsHard and Notify are optional. If they
528 are not NULL, this routine will fill them.
529
530 @param[in] IcmpError IcmpError Type.
531 @param[in] IpVersion The version of the IP protocol to use,
532 either IPv4 or IPv6.
533 @param[out] IsHard If TRUE, indicates that it is a hard error.
534 @param[out] Notify If TRUE, SockError needs to be notified.
535
536 @return The ICMP Error Status, such as EFI_NETWORK_UNREACHABLE.
537
538 **/
539 EFI_STATUS
540 EFIAPI
541 IpIoGetIcmpErrStatus (
542 IN UINT8 IcmpError,
543 IN UINT8 IpVersion,
544 OUT BOOLEAN *IsHard OPTIONAL,
545 OUT BOOLEAN *Notify OPTIONAL
546 );
547
548 /**
549 Refresh the remote peer's Neighbor Cache entries.
550
551 This function is called when the caller needs the IpIo to refresh the existing
552 IPv6 neighbor cache entries since the neighbor is considered reachable by the
553 node has recently received a confirmation that packets sent recently to the
554 neighbor were received by its IP layer.
555
556 @param[in] IpIo The pointer to an IP_IO instance
557 @param[in] Neighbor The IP address of the neighbor
558 @param[in] Timeout The time in 100-ns units that this entry will
559 remain in the neighbor cache. A value of
560 zero means that the entry is permanent.
561 A value of non-zero means that the entry is
562 dynamic and will be deleted after Timeout.
563
564 @retval EFI_SUCCESS The operation completed successfully.
565 @retval EFI_NOT_STARTED The IpIo is not configured.
566 @retval EFI_INVALID_PARAMETER The Neighbor Address is invalid.
567 @retval EFI_NOT_FOUND The neighbor cache entry is not in the
568 neighbor table.
569 @retval EFI_OUT_OF_RESOURCES Failed due to resource limitations.
570
571 **/
572 EFI_STATUS
573 IpIoRefreshNeighbor (
574 IN IP_IO *IpIo,
575 IN EFI_IP_ADDRESS *Neighbor,
576 IN UINT32 Timeout
577 );
578
579 #endif
580