]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Include/Library/IpIoLib.h
MdeModulePkg/IpIoLib: Check the input parameters before use them.
[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 - 2017, 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 typedef union {
157 EFI_IP4_PROTOCOL *Ip4;
158 EFI_IP6_PROTOCOL *Ip6;
159 } IP_IO_IP_PROTOCOL;
160
161 ///
162 /// The IP session for an IP receive packet.
163 ///
164 typedef struct _EFI_NET_SESSION_DATA {
165 EFI_IP_ADDRESS Source; ///< Source IP of the received packet.
166 EFI_IP_ADDRESS Dest; ///< Destination IP of the received packet.
167 IP_IO_IP_HEADER IpHdr; ///< IP header of the received packet.
168 UINT32 IpHdrLen; ///< IP header length of the received packet.
169 ///< For IPv6, it includes the IP6 header
170 ///< length and extension header length. For
171 ///< IPv4, it includes the IP4 header length
172 ///< and options length.
173 UINT8 IpVersion; ///< The IP version of the received packet.
174 } EFI_NET_SESSION_DATA;
175
176 /**
177 The prototype is called back when an IP packet is received.
178
179 @param[in] Status The result of the receive request.
180 @param[in] IcmpErr Valid when Status is EFI_ICMP_ERROR.
181 @param[in] NetSession The IP session for the received packet.
182 @param[in] Pkt The packet received.
183 @param[in] Context The data provided by the user for the received packet when
184 the callback is registered in IP_IO_OPEN_DATA::RcvdContext.
185
186 **/
187 typedef
188 VOID
189 (EFIAPI *PKT_RCVD_NOTIFY) (
190 IN EFI_STATUS Status,
191 IN UINT8 IcmpErr,
192 IN EFI_NET_SESSION_DATA *NetSession,
193 IN NET_BUF *Pkt,
194 IN VOID *Context
195 );
196
197 /**
198 The prototype is called back when an IP packet is sent.
199
200 @param[in] Status Result of the IP packet being sent.
201 @param[in] Context The data provided by user for the received packet when
202 the callback is registered in IP_IO_OPEN_DATA::SndContext.
203 @param[in] Sender A Union type to specify a pointer of EFI_IP4_PROTOCOL
204 or EFI_IP6_PROTOCOL.
205 @param[in] NotifyData The Context data specified when calling IpIoSend()
206
207 **/
208 typedef
209 VOID
210 (EFIAPI *PKT_SENT_NOTIFY) (
211 IN EFI_STATUS Status,
212 IN VOID *Context,
213 IN IP_IO_IP_PROTOCOL Sender,
214 IN VOID *NotifyData
215 );
216
217 ///
218 /// This data structure wraps Ip4/Ip6 instances. The IpIo Library uses it for all
219 /// Ip4/Ip6 operations.
220 ///
221 typedef struct _IP_IO {
222 ///
223 /// The node used to link this IpIo to the active IpIo list.
224 ///
225 LIST_ENTRY Entry;
226
227 ///
228 /// The list used to maintain the IP instance for different sending purpose.
229 ///
230 LIST_ENTRY IpList;
231
232 EFI_HANDLE Controller;
233 EFI_HANDLE Image;
234 EFI_HANDLE ChildHandle;
235 //
236 // The IP instance consumed by this IP_IO
237 //
238 IP_IO_IP_PROTOCOL Ip;
239 BOOLEAN IsConfigured;
240
241 ///
242 /// Some ip configuration data can be changed.
243 ///
244 UINT8 Protocol;
245
246 ///
247 /// Token and event used to get data from IP.
248 ///
249 IP_IO_IP_COMPLETION_TOKEN RcvToken;
250
251 ///
252 /// List entry used to link the token passed to IP_IO.
253 ///
254 LIST_ENTRY PendingSndList;
255
256 //
257 // User interface used to get notify from IP_IO
258 //
259 VOID *RcvdContext; ///< See IP_IO_OPEN_DATA::RcvdContext.
260 VOID *SndContext; ///< See IP_IO_OPEN_DATA::SndContext.
261 PKT_RCVD_NOTIFY PktRcvdNotify; ///< See IP_IO_OPEN_DATA::PktRcvdNotify.
262 PKT_SENT_NOTIFY PktSentNotify; ///< See IP_IO_OPEN_DATA::PktSentNotify.
263 UINT8 IpVersion;
264 IP4_ADDR StationIp;
265 IP4_ADDR SubnetMask;
266 } IP_IO;
267
268 ///
269 /// The struct is for the user to pass IP configuration and callbacks to IP_IO.
270 /// It is used by IpIoOpen().
271 ///
272 typedef struct _IP_IO_OPEN_DATA {
273 IP_IO_IP_CONFIG_DATA IpConfigData; ///< Configuration of the IP instance.
274 VOID *RcvdContext; ///< Context data used by receive callback.
275 VOID *SndContext; ///< Context data used by send callback.
276 PKT_RCVD_NOTIFY PktRcvdNotify; ///< Receive callback.
277 PKT_SENT_NOTIFY PktSentNotify; ///< Send callback.
278 } IP_IO_OPEN_DATA;
279
280 ///
281 /// Internal struct book-keeping send request of IP_IO.
282 ///
283 /// An IP_IO_SEND_ENTRY will be created each time a send request is issued to
284 /// IP_IO via IpIoSend().
285 ///
286 typedef struct _IP_IO_SEND_ENTRY {
287 LIST_ENTRY Entry;
288 IP_IO *IpIo;
289 VOID *Context;
290 VOID *NotifyData;
291 IP_IO_IP_PROTOCOL Ip;
292 NET_BUF *Pkt;
293 IP_IO_IP_COMPLETION_TOKEN SndToken;
294 } IP_IO_SEND_ENTRY;
295
296 ///
297 /// The IP_IO_IP_INFO is used in IpIoSend() to override the default IP instance
298 /// in IP_IO.
299 ///
300 typedef struct _IP_IO_IP_INFO {
301 EFI_IP_ADDRESS Addr;
302 IP_IO_IP_MASK PreMask;
303 LIST_ENTRY Entry;
304 EFI_HANDLE ChildHandle;
305 IP_IO_IP_PROTOCOL Ip;
306 IP_IO_IP_COMPLETION_TOKEN DummyRcvToken;
307 INTN RefCnt;
308 UINT8 IpVersion;
309 } IP_IO_IP_INFO;
310
311 /**
312 Create a new IP_IO instance.
313
314 This function uses IP4/IP6 service binding protocol in Controller to create
315 an IP4/IP6 child (aka IP4/IP6 instance).
316
317 @param[in] Image The image handle of the driver or application that
318 consumes IP_IO.
319 @param[in] Controller The controller handle that has IP4 or IP6 service
320 binding protocol installed.
321 @param[in] IpVersion The version of the IP protocol to use, either
322 IPv4 or IPv6.
323
324 @return The pointer to a newly created IP_IO instance, or NULL if failed.
325
326 **/
327 IP_IO *
328 EFIAPI
329 IpIoCreate (
330 IN EFI_HANDLE Image,
331 IN EFI_HANDLE Controller,
332 IN UINT8 IpVersion
333 );
334
335 /**
336 Destroy an IP_IO instance.
337
338 This function is paired with IpIoCreate(). The IP_IO will be closed first.
339 Resource will be freed afterwards. See IpIoClose().
340
341 @param[in, out] IpIo The pointer to the IP_IO instance that needs to be
342 destroyed.
343
344 @retval EFI_SUCCESS The IP_IO instance was destroyed successfully.
345 @retval Others An error condition occurred.
346
347 **/
348 EFI_STATUS
349 EFIAPI
350 IpIoDestroy (
351 IN OUT IP_IO *IpIo
352 );
353
354 /**
355 Stop an IP_IO instance.
356
357 This function is paired with IpIoOpen(). The IP_IO will be unconfigured, and all
358 pending send/receive tokens will be canceled.
359
360 @param[in, out] IpIo The pointer to the IP_IO instance that needs to stop.
361
362 @retval EFI_SUCCESS The IP_IO instance stopped successfully.
363 @retval EFI_INVALID_PARAMETER Invalid input parameter.
364 @retval Others Anrror condition occurred.
365
366 **/
367 EFI_STATUS
368 EFIAPI
369 IpIoStop (
370 IN OUT IP_IO *IpIo
371 );
372
373 /**
374 Open an IP_IO instance for use.
375
376 This function is called after IpIoCreate(). It is used for configuring the IP
377 instance and register the callbacks and their context data for sending and
378 receiving IP packets.
379
380 @param[in, out] IpIo The pointer to an IP_IO instance that needs
381 to open.
382 @param[in] OpenData The configuration data and callbacks for
383 the IP_IO instance.
384
385 @retval EFI_SUCCESS The IP_IO instance opened with OpenData
386 successfully.
387 @retval EFI_ACCESS_DENIED The IP_IO instance is configured, avoid to
388 reopen it.
389 @retval EFI_UNSUPPORTED IPv4 RawData mode is no supported.
390 @retval EFI_INVALID_PARAMETER Invalid input parameter.
391 @retval Others Error condition occurred.
392
393 **/
394 EFI_STATUS
395 EFIAPI
396 IpIoOpen (
397 IN OUT IP_IO *IpIo,
398 IN IP_IO_OPEN_DATA *OpenData
399 );
400
401 /**
402 Send out an IP packet.
403
404 This function is called after IpIoOpen(). The data to be sent are wrapped in
405 Pkt. The IP instance wrapped in IpIo is used for sending by default but can be
406 overriden by Sender. Other sending configs, like source address and gateway
407 address etc., are specified in OverrideData.
408
409 @param[in, out] IpIo Pointer to an IP_IO instance used for sending IP
410 packet.
411 @param[in, out] Pkt Pointer to the IP packet to be sent.
412 @param[in] Sender The IP protocol instance used for sending.
413 @param[in] Context Optional context data.
414 @param[in] NotifyData Optional notify data.
415 @param[in] Dest The destination IP address to send this packet to.
416 This parameter is optional when using IPv6.
417 @param[in] OverrideData The data to override some configuration of the IP
418 instance used for sending.
419
420 @retval EFI_SUCCESS The operation is completed successfully.
421 @retval EFI_INVALID_PARAMETER The input parameter is not correct.
422 @retval EFI_NOT_STARTED The IpIo is not configured.
423 @retval EFI_OUT_OF_RESOURCES Failed due to resource limit.
424 @retval Others Error condition occurred.
425
426 **/
427 EFI_STATUS
428 EFIAPI
429 IpIoSend (
430 IN OUT IP_IO *IpIo,
431 IN OUT NET_BUF *Pkt,
432 IN IP_IO_IP_INFO *Sender OPTIONAL,
433 IN VOID *Context OPTIONAL,
434 IN VOID *NotifyData OPTIONAL,
435 IN EFI_IP_ADDRESS *Dest OPTIONAL,
436 IN IP_IO_OVERRIDE *OverrideData OPTIONAL
437 );
438
439 /**
440 Cancel the IP transmit token that wraps this Packet.
441
442 @param[in] IpIo The pointer to the IP_IO instance.
443 @param[in] Packet The pointer to the packet of NET_BUF to cancel.
444
445 **/
446 VOID
447 EFIAPI
448 IpIoCancelTxToken (
449 IN IP_IO *IpIo,
450 IN VOID *Packet
451 );
452
453 /**
454 Add a new IP instance for sending data.
455
456 The function is used to add the IP_IO to the IP_IO sending list. The caller
457 can later use IpIoFindSender() to get the IP_IO and call IpIoSend() to send
458 data.
459
460 @param[in, out] IpIo The pointer to an IP_IO instance to add a new IP
461 instance for sending purposes.
462
463 @return The pointer to the created IP_IO_IP_INFO structure; NULL if failed.
464
465 **/
466 IP_IO_IP_INFO *
467 EFIAPI
468 IpIoAddIp (
469 IN OUT IP_IO *IpIo
470 );
471
472 /**
473 Configure the IP instance of this IpInfo and start the receiving if IpConfigData
474 is not NULL.
475
476 @param[in, out] IpInfo The pointer to the IP_IO_IP_INFO instance.
477 @param[in, out] IpConfigData The IP4 or IP6 configure data used to configure
478 the IP instance. If NULL, the IP instance is reset.
479 If UseDefaultAddress is set to TRUE, and the configure
480 operation succeeds, the default address information
481 is written back in this IpConfigData.
482
483 @retval EFI_SUCCESS The IP instance of this IpInfo was configured successfully,
484 or there is no need to reconfigure it.
485 @retval Others The configuration failed.
486
487 **/
488 EFI_STATUS
489 EFIAPI
490 IpIoConfigIp (
491 IN OUT IP_IO_IP_INFO *IpInfo,
492 IN OUT VOID *IpConfigData OPTIONAL
493 );
494
495 /**
496 Destroy an IP instance maintained in IpIo->IpList for
497 sending purpose.
498
499 This function pairs with IpIoAddIp(). The IpInfo is previously created by
500 IpIoAddIp(). The IP_IO_IP_INFO::RefCnt is decremented and the IP instance
501 will be dstroyed if the RefCnt is zero.
502
503 @param[in] IpIo The pointer to the IP_IO instance.
504 @param[in] IpInfo The pointer to the IpInfo to be removed.
505
506 **/
507 VOID
508 EFIAPI
509 IpIoRemoveIp (
510 IN IP_IO *IpIo,
511 IN IP_IO_IP_INFO *IpInfo
512 );
513
514 /**
515 Find the first IP protocol maintained in IpIo whose local
516 address is the same as Src.
517
518 This function is called when the caller needs the IpIo to send data to the
519 specified Src. The IpIo was added previously by IpIoAddIp().
520
521 @param[in, out] IpIo The pointer to the pointer of the IP_IO instance.
522 @param[in] IpVersion The version of the IP protocol to use, either
523 IPv4 or IPv6.
524 @param[in] Src The local IP address.
525
526 @return The pointer to the IP protocol can be used for sending purpose and its local
527 address is the same with Src. NULL if failed.
528
529 **/
530 IP_IO_IP_INFO *
531 EFIAPI
532 IpIoFindSender (
533 IN OUT IP_IO **IpIo,
534 IN UINT8 IpVersion,
535 IN EFI_IP_ADDRESS *Src
536 );
537
538 /**
539 Get the ICMP error map information.
540
541 The ErrorStatus will be returned. The IsHard and Notify are optional. If they
542 are not NULL, this routine will fill them.
543
544 @param[in] IcmpError IcmpError Type.
545 @param[in] IpVersion The version of the IP protocol to use,
546 either IPv4 or IPv6.
547 @param[out] IsHard If TRUE, indicates that it is a hard error.
548 @param[out] Notify If TRUE, SockError needs to be notified.
549
550 @retval EFI_UNSUPPORTED Unrecognizable ICMP error code
551 @return The ICMP Error Status, such as EFI_NETWORK_UNREACHABLE.
552
553 **/
554 EFI_STATUS
555 EFIAPI
556 IpIoGetIcmpErrStatus (
557 IN UINT8 IcmpError,
558 IN UINT8 IpVersion,
559 OUT BOOLEAN *IsHard OPTIONAL,
560 OUT BOOLEAN *Notify OPTIONAL
561 );
562
563 /**
564 Refresh the remote peer's Neighbor Cache entries.
565
566 This function is called when the caller needs the IpIo to refresh the existing
567 IPv6 neighbor cache entries since the neighbor is considered reachable by the
568 node has recently received a confirmation that packets sent recently to the
569 neighbor were received by its IP layer.
570
571 @param[in] IpIo The pointer to an IP_IO instance
572 @param[in] Neighbor The IP address of the neighbor
573 @param[in] Timeout The time in 100-ns units that this entry will
574 remain in the neighbor cache. A value of
575 zero means that the entry is permanent.
576 A value of non-zero means that the entry is
577 dynamic and will be deleted after Timeout.
578
579 @retval EFI_SUCCESS The operation completed successfully.
580 @retval EFI_NOT_STARTED The IpIo is not configured.
581 @retval EFI_INVALID_PARAMETER The Neighbor Address is invalid.
582 @retval EFI_NOT_FOUND The neighbor cache entry is not in the
583 neighbor table.
584 @retval EFI_UNSUPPORTED IP version is IPv4, which doesn't support neighbor cache refresh.
585 @retval EFI_OUT_OF_RESOURCES Failed due to resource limitations.
586
587 **/
588 EFI_STATUS
589 IpIoRefreshNeighbor (
590 IN IP_IO *IpIo,
591 IN EFI_IP_ADDRESS *Neighbor,
592 IN UINT32 Timeout
593 );
594
595 #endif
596