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.
5 Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
13 #include <Protocol/Ip4.h>
14 #include <Protocol/Ip6.h>
16 #include <Library/NetLib.h>
19 // type and code define for ICMP protocol error
22 #define ICMP_TYPE_UNREACH 3
23 #define ICMP_TYPE_TIMXCEED 11
24 #define ICMP_TYPE_PARAMPROB 12
25 #define ICMP_TYPE_SOURCEQUENCH 4
27 #define ICMP_CODE_UNREACH_NET 0
28 #define ICMP_CODE_UNREACH_HOST 1
29 #define ICMP_CODE_UNREACH_PROTOCOL 2
30 #define ICMP_CODE_UNREACH_PORT 3
31 #define ICMP_CODE_UNREACH_NEEDFRAG 4
32 #define ICMP_CODE_UNREACH_SRCFAIL 5
33 #define ICMP_CODE_UNREACH_NET_UNKNOWN 6
34 #define ICMP_CODE_UNREACH_HOST_UNKNOWN 7
35 #define ICMP_CODE_UNREACH_ISOLATED 8
36 #define ICMP_CODE_UNREACH_NET_PROHIB 9
37 #define ICMP_CODE_UNREACH_HOST_PROHIB 10
38 #define ICMP_CODE_UNREACH_TOSNET 11
39 #define ICMP_CODE_UNREACH_TOSHOST 12
42 Get the IP header length from the struct EFI_IP4_HEADER. HeaderLength is
43 Internet header length in 32-bit words, so HeaderLength<<2 is the real
46 @param[out] HdrPtr A pointer to EFI_IP4_HEADER.
48 @return The IP header length.
50 #define EFI_IP4_HEADER_LEN(HdrPtr) ((HdrPtr)->HeaderLength << 2)
53 To types of ICMP error which consist of ICMP header, IP header and original
54 datagram's data, get length from sum of ICMP header length, IP header length
55 and first 64 bits of datagram's data length.
57 @param[in] IpHdr A pointer to EFI_IP4_HEADER.
59 @return The ICMP error length.
61 #define ICMP_ERRLEN(IpHdr) \
62 (sizeof(IP4_ICMP_HEAD) + EFI_IP4_HEADER_LEN(IpHdr) + 8)
65 Get the packet header from NET_BUF.
67 @param[out] Buf A pointer to NET_BUF.
68 @param[in] Type Header type.
70 @return The pointer to packet header.
72 #define NET_PROTO_HDR(Buf, Type) ((Type *) ((Buf)->BlockOp[0].Head))
75 extern EFI_IP4_CONFIG_DATA mIp4IoDefaultIpConfigData
;
76 extern EFI_IP6_CONFIG_DATA mIp6IoDefaultIpConfigData
;
80 /// This error will be delivered to the
81 /// listening transportation layer protocol
82 /// that consumes IpIO.
85 #define ICMP_ERR_UNREACH_NET 0
86 #define ICMP_ERR_UNREACH_HOST 1
87 #define ICMP_ERR_UNREACH_PROTOCOL 2
88 #define ICMP_ERR_UNREACH_PORT 3
89 #define ICMP_ERR_MSGSIZE 4
90 #define ICMP_ERR_UNREACH_SRCFAIL 5
91 #define ICMP_ERR_TIMXCEED_INTRANS 6
92 #define ICMP_ERR_TIMXCEED_REASS 7
93 #define ICMP_ERR_QUENCH 8
94 #define ICMP_ERR_PARAMPROB 9
96 #define ICMP6_ERR_UNREACH_NET 0
97 #define ICMP6_ERR_UNREACH_HOST 1
98 #define ICMP6_ERR_UNREACH_PROTOCOL 2
99 #define ICMP6_ERR_UNREACH_PORT 3
100 #define ICMP6_ERR_PACKAGE_TOOBIG 4
101 #define ICMP6_ERR_TIMXCEED_HOPLIMIT 5
102 #define ICMP6_ERR_TIMXCEED_REASS 6
103 #define ICMP6_ERR_PARAMPROB_HEADER 7
104 #define ICMP6_ERR_PARAMPROB_NEXHEADER 8
105 #define ICMP6_ERR_PARAMPROB_IPV6OPTION 9
108 /// The helper struct for IpIoGetIcmpErrStatus(). It is for internal use only.
116 EFI_IP4_COMPLETION_TOKEN Ip4Token
;
117 EFI_IP6_COMPLETION_TOKEN Ip6Token
;
118 } IP_IO_IP_COMPLETION_TOKEN
;
121 EFI_IP4_TRANSMIT_DATA Ip4TxData
;
122 EFI_IP6_TRANSMIT_DATA Ip6TxData
;
126 EFI_IP4_RECEIVE_DATA Ip4RxData
;
127 EFI_IP6_RECEIVE_DATA Ip6RxData
;
131 EFI_IP4_OVERRIDE_DATA Ip4OverrideData
;
132 EFI_IP6_OVERRIDE_DATA Ip6OverrideData
;
136 EFI_IP4_CONFIG_DATA Ip4CfgData
;
137 EFI_IP6_CONFIG_DATA Ip6CfgData
;
138 } IP_IO_IP_CONFIG_DATA
;
141 EFI_IP4_HEADER
*Ip4Hdr
;
142 EFI_IP6_HEADER
*Ip6Hdr
;
151 EFI_IP4_PROTOCOL
*Ip4
;
152 EFI_IP6_PROTOCOL
*Ip6
;
156 /// The IP session for an IP receive packet.
158 typedef struct _EFI_NET_SESSION_DATA
{
159 EFI_IP_ADDRESS Source
; ///< Source IP of the received packet.
160 EFI_IP_ADDRESS Dest
; ///< Destination IP of the received packet.
161 IP_IO_IP_HEADER IpHdr
; ///< IP header of the received packet.
162 UINT32 IpHdrLen
; ///< IP header length of the received packet.
163 ///< For IPv6, it includes the IP6 header
164 ///< length and extension header length. For
165 ///< IPv4, it includes the IP4 header length
166 ///< and options length.
167 UINT8 IpVersion
; ///< The IP version of the received packet.
168 } EFI_NET_SESSION_DATA
;
171 The prototype is called back when an IP packet is received.
173 @param[in] Status The result of the receive request.
174 @param[in] IcmpErr Valid when Status is EFI_ICMP_ERROR.
175 @param[in] NetSession The IP session for the received packet.
176 @param[in] Pkt The packet received.
177 @param[in] Context The data provided by the user for the received packet when
178 the callback is registered in IP_IO_OPEN_DATA::RcvdContext.
183 (EFIAPI
*PKT_RCVD_NOTIFY
) (
184 IN EFI_STATUS Status
,
186 IN EFI_NET_SESSION_DATA
*NetSession
,
192 The prototype is called back when an IP packet is sent.
194 @param[in] Status Result of the IP packet being sent.
195 @param[in] Context The data provided by user for the received packet when
196 the callback is registered in IP_IO_OPEN_DATA::SndContext.
197 @param[in] Sender A Union type to specify a pointer of EFI_IP4_PROTOCOL
199 @param[in] NotifyData The Context data specified when calling IpIoSend()
204 (EFIAPI
*PKT_SENT_NOTIFY
) (
205 IN EFI_STATUS Status
,
207 IN IP_IO_IP_PROTOCOL Sender
,
212 /// This data structure wraps Ip4/Ip6 instances. The IpIo Library uses it for all
213 /// Ip4/Ip6 operations.
215 typedef struct _IP_IO
{
217 /// The node used to link this IpIo to the active IpIo list.
222 /// The list used to maintain the IP instance for different sending purpose.
226 EFI_HANDLE Controller
;
228 EFI_HANDLE ChildHandle
;
230 // The IP instance consumed by this IP_IO
232 IP_IO_IP_PROTOCOL Ip
;
233 BOOLEAN IsConfigured
;
236 /// Some ip configuration data can be changed.
241 /// Token and event used to get data from IP.
243 IP_IO_IP_COMPLETION_TOKEN RcvToken
;
246 /// List entry used to link the token passed to IP_IO.
248 LIST_ENTRY PendingSndList
;
251 // User interface used to get notify from IP_IO
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.
263 /// The struct is for the user to pass IP configuration and callbacks to IP_IO.
264 /// It is used by IpIoOpen().
266 typedef struct _IP_IO_OPEN_DATA
{
267 IP_IO_IP_CONFIG_DATA IpConfigData
; ///< Configuration of the IP instance.
268 VOID
*RcvdContext
; ///< Context data used by receive callback.
269 VOID
*SndContext
; ///< Context data used by send callback.
270 PKT_RCVD_NOTIFY PktRcvdNotify
; ///< Receive callback.
271 PKT_SENT_NOTIFY PktSentNotify
; ///< Send callback.
275 /// Internal struct book-keeping send request of IP_IO.
277 /// An IP_IO_SEND_ENTRY will be created each time a send request is issued to
278 /// IP_IO via IpIoSend().
280 typedef struct _IP_IO_SEND_ENTRY
{
285 IP_IO_IP_PROTOCOL Ip
;
287 IP_IO_IP_COMPLETION_TOKEN SndToken
;
291 /// The IP_IO_IP_INFO is used in IpIoSend() to override the default IP instance
294 typedef struct _IP_IO_IP_INFO
{
296 IP_IO_IP_MASK PreMask
;
298 EFI_HANDLE ChildHandle
;
299 IP_IO_IP_PROTOCOL Ip
;
300 IP_IO_IP_COMPLETION_TOKEN DummyRcvToken
;
306 Create a new IP_IO instance.
308 If IpVersion is not IP_VERSION_4 or IP_VERSION_6, then ASSERT().
310 This function uses IP4/IP6 service binding protocol in Controller to create
311 an IP4/IP6 child (aka IP4/IP6 instance).
313 @param[in] Image The image handle of the driver or application that
315 @param[in] Controller The controller handle that has IP4 or IP6 service
316 binding protocol installed.
317 @param[in] IpVersion The version of the IP protocol to use, either
320 @return The pointer to a newly created IP_IO instance, or NULL if failed.
327 IN EFI_HANDLE Controller
,
332 Destroy an IP_IO instance.
334 This function is paired with IpIoCreate(). The IP_IO will be closed first.
335 Resource will be freed afterwards. See IpIoClose().
337 @param[in, out] IpIo The pointer to the IP_IO instance that needs to be
340 @retval EFI_SUCCESS The IP_IO instance was destroyed successfully.
341 @retval Others An error condition occurred.
351 Stop an IP_IO instance.
353 If Ip version is not IP_VERSION_4 or IP_VERSION_6, then ASSERT().
355 This function is paired with IpIoOpen(). The IP_IO will be unconfigured, and all
356 pending send/receive tokens will be canceled.
358 @param[in, out] IpIo The pointer to the IP_IO instance that needs to stop.
360 @retval EFI_SUCCESS The IP_IO instance stopped successfully.
361 @retval EFI_INVALID_PARAMETER Invalid input parameter.
362 @retval Others Anrror condition occurred.
372 Open an IP_IO instance for use.
374 If Ip version is not IP_VERSION_4 or IP_VERSION_6, then ASSERT().
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.
380 @param[in, out] IpIo The pointer to an IP_IO instance that needs
382 @param[in] OpenData The configuration data and callbacks for
385 @retval EFI_SUCCESS The IP_IO instance opened with OpenData
387 @retval EFI_ACCESS_DENIED The IP_IO instance is configured, avoid to
389 @retval EFI_UNSUPPORTED IPv4 RawData mode is no supported.
390 @retval EFI_INVALID_PARAMETER Invalid input parameter.
391 @retval Others Error condition occurred.
398 IN IP_IO_OPEN_DATA
*OpenData
402 Send out an IP packet.
404 This function is called after IpIoOpen(). The data to be sent is wrapped in
405 Pkt. The IP instance wrapped in IpIo is used for sending by default but can be
406 overridden by Sender. Other sending configs, like source address and gateway
407 address etc., are specified in OverrideData.
409 @param[in, out] IpIo Pointer to an IP_IO instance used for sending IP
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.
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.
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
440 Cancel the IP transmit token that wraps this Packet.
442 If IpIo is NULL, then ASSERT().
443 If Packet is NULL, then ASSERT().
445 @param[in] IpIo The pointer to the IP_IO instance.
446 @param[in] Packet The pointer to the packet of NET_BUF to cancel.
457 Add a new IP instance for sending data.
459 If IpIo is NULL, then ASSERT().
460 If Ip version is not IP_VERSION_4 or IP_VERSION_6, then ASSERT().
462 The function is used to add the IP_IO to the IP_IO sending list. The caller
463 can later use IpIoFindSender() to get the IP_IO and call IpIoSend() to send
466 @param[in, out] IpIo The pointer to an IP_IO instance to add a new IP
467 instance for sending purposes.
469 @return The pointer to the created IP_IO_IP_INFO structure; NULL if failed.
479 Configure the IP instance of this IpInfo and start the receiving if IpConfigData
482 If Ip version is not IP_VERSION_4 or IP_VERSION_6, then ASSERT().
484 @param[in, out] IpInfo The pointer to the IP_IO_IP_INFO instance.
485 @param[in, out] IpConfigData The IP4 or IP6 configure data used to configure
486 the IP instance. If NULL, the IP instance is reset.
487 If UseDefaultAddress is set to TRUE, and the configure
488 operation succeeds, the default address information
489 is written back in this IpConfigData.
491 @retval EFI_SUCCESS The IP instance of this IpInfo was configured successfully,
492 or there is no need to reconfigure it.
493 @retval Others The configuration failed.
499 IN OUT IP_IO_IP_INFO
*IpInfo
,
500 IN OUT VOID
*IpConfigData OPTIONAL
504 Destroy an IP instance maintained in IpIo->IpList for
507 If Ip version is not IP_VERSION_4 or IP_VERSION_6, then ASSERT().
509 This function pairs with IpIoAddIp(). The IpInfo is previously created by
510 IpIoAddIp(). The IP_IO_IP_INFO::RefCnt is decremented and the IP instance
511 will be destroyed if the RefCnt is zero.
513 @param[in] IpIo The pointer to the IP_IO instance.
514 @param[in] IpInfo The pointer to the IpInfo to be removed.
521 IN IP_IO_IP_INFO
*IpInfo
525 Find the first IP protocol maintained in IpIo whose local
526 address is the same as Src.
528 This function is called when the caller needs the IpIo to send data to the
529 specified Src. The IpIo was added previously by IpIoAddIp().
531 @param[in, out] IpIo The pointer to the pointer of the IP_IO instance.
532 @param[in] IpVersion The version of the IP protocol to use, either
534 @param[in] Src The local IP address.
536 @return The pointer to the IP protocol can be used for sending purpose and its local
537 address is the same with Src. NULL if failed.
545 IN EFI_IP_ADDRESS
*Src
549 Get the ICMP error map information.
551 The ErrorStatus will be returned. The IsHard and Notify are optional. If they
552 are not NULL, this routine will fill them.
554 @param[in] IcmpError IcmpError Type.
555 @param[in] IpVersion The version of the IP protocol to use,
557 @param[out] IsHard If TRUE, indicates that it is a hard error.
558 @param[out] Notify If TRUE, SockError needs to be notified.
560 @retval EFI_UNSUPPORTED Unrecognizable ICMP error code
561 @return The ICMP Error Status, such as EFI_NETWORK_UNREACHABLE.
566 IpIoGetIcmpErrStatus (
569 OUT BOOLEAN
*IsHard OPTIONAL
,
570 OUT BOOLEAN
*Notify OPTIONAL
574 Refresh the remote peer's Neighbor Cache entries.
576 This function is called when the caller needs the IpIo to refresh the existing
577 IPv6 neighbor cache entries since the neighbor is considered reachable by the
578 node has recently received a confirmation that packets sent recently to the
579 neighbor were received by its IP layer.
581 @param[in] IpIo The pointer to an IP_IO instance
582 @param[in] Neighbor The IP address of the neighbor
583 @param[in] Timeout The time in 100-ns units that this entry will
584 remain in the neighbor cache. A value of
585 zero means that the entry is permanent.
586 A value of non-zero means that the entry is
587 dynamic and will be deleted after Timeout.
589 @retval EFI_SUCCESS The operation completed successfully.
590 @retval EFI_NOT_STARTED The IpIo is not configured.
591 @retval EFI_INVALID_PARAMETER The Neighbor Address is invalid.
592 @retval EFI_NOT_FOUND The neighbor cache entry is not in the
594 @retval EFI_UNSUPPORTED IP version is IPv4, which doesn't support neighbor cache refresh.
595 @retval EFI_OUT_OF_RESOURCES Failed due to resource limitations.
600 IpIoRefreshNeighbor (
602 IN EFI_IP_ADDRESS
*Neighbor
,