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 - 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.
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.
19 #include <Protocol/Ip4.h>
20 #include <Protocol/Ip6.h>
22 #include <Library/NetLib.h>
25 // type and code define for ICMP protocol error
28 #define ICMP_TYPE_UNREACH 3
29 #define ICMP_TYPE_TIMXCEED 11
30 #define ICMP_TYPE_PARAMPROB 12
31 #define ICMP_TYPE_SOURCEQUENCH 4
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
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
52 @param[out] HdrPtr A pointer to EFI_IP4_HEADER.
54 @return The IP header length.
56 #define EFI_IP4_HEADER_LEN(HdrPtr) ((HdrPtr)->HeaderLength << 2)
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.
63 @param[in] IpHdr A pointer to EFI_IP4_HEADER.
65 @return The ICMP error length.
67 #define ICMP_ERRLEN(IpHdr) \
68 (sizeof(IP4_ICMP_HEAD) + EFI_IP4_HEADER_LEN(IpHdr) + 8)
71 Get the packet header from NET_BUF.
73 @param[out] Buf A pointer to NET_BUF.
74 @param[in] Type Header type.
76 @return The pointer to packet header.
78 #define NET_PROTO_HDR(Buf, Type) ((Type *) ((Buf)->BlockOp[0].Head))
81 extern EFI_IP4_CONFIG_DATA mIp4IoDefaultIpConfigData
;
82 extern EFI_IP6_CONFIG_DATA mIp6IoDefaultIpConfigData
;
86 /// This error will be delivered to the
87 /// listening transportation layer protocol
88 /// that consumes IpIO.
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
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
114 /// The helper struct for IpIoGetIcmpErrStatus(). It is for internal use only.
122 EFI_IP4_COMPLETION_TOKEN Ip4Token
;
123 EFI_IP6_COMPLETION_TOKEN Ip6Token
;
124 } IP_IO_IP_COMPLETION_TOKEN
;
127 EFI_IP4_TRANSMIT_DATA Ip4TxData
;
128 EFI_IP6_TRANSMIT_DATA Ip6TxData
;
132 EFI_IP4_RECEIVE_DATA Ip4RxData
;
133 EFI_IP6_RECEIVE_DATA Ip6RxData
;
137 EFI_IP4_OVERRIDE_DATA Ip4OverrideData
;
138 EFI_IP6_OVERRIDE_DATA Ip6OverrideData
;
142 EFI_IP4_CONFIG_DATA Ip4CfgData
;
143 EFI_IP6_CONFIG_DATA Ip6CfgData
;
144 } IP_IO_IP_CONFIG_DATA
;
147 EFI_IP4_HEADER
*Ip4Hdr
;
148 EFI_IP6_HEADER
*Ip6Hdr
;
157 EFI_IP4_PROTOCOL
*Ip4
;
158 EFI_IP6_PROTOCOL
*Ip6
;
162 /// The IP session for an IP receive packet.
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
;
177 The prototype is called back when an IP packet is received.
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.
189 (EFIAPI
*PKT_RCVD_NOTIFY
) (
190 IN EFI_STATUS Status
,
192 IN EFI_NET_SESSION_DATA
*NetSession
,
198 The prototype is called back when an IP packet is sent.
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
205 @param[in] NotifyData The Context data specified when calling IpIoSend()
210 (EFIAPI
*PKT_SENT_NOTIFY
) (
211 IN EFI_STATUS Status
,
213 IN IP_IO_IP_PROTOCOL Sender
,
218 /// This data structure wraps Ip4/Ip6 instances. The IpIo Library uses it for all
219 /// Ip4/Ip6 operations.
221 typedef struct _IP_IO
{
223 /// The node used to link this IpIo to the active IpIo list.
228 /// The list used to maintain the IP instance for different sending purpose.
232 EFI_HANDLE Controller
;
234 EFI_HANDLE ChildHandle
;
236 // The IP instance consumed by this IP_IO
238 IP_IO_IP_PROTOCOL Ip
;
239 BOOLEAN IsConfigured
;
242 /// Some ip configuration data can be changed.
247 /// Token and event used to get data from IP.
249 IP_IO_IP_COMPLETION_TOKEN RcvToken
;
252 /// List entry used to link the token passed to IP_IO.
254 LIST_ENTRY PendingSndList
;
257 // User interface used to get notify from IP_IO
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.
267 /// The struct is for the user to pass IP configuration and callbacks to IP_IO.
268 /// It is used by IpIoOpen().
270 typedef struct _IP_IO_OPEN_DATA
{
271 IP_IO_IP_CONFIG_DATA IpConfigData
; ///< Configuration of the IP instance.
272 VOID
*RcvdContext
; ///< Context data used by receive callback.
273 VOID
*SndContext
; ///< Context data used by send callback.
274 PKT_RCVD_NOTIFY PktRcvdNotify
; ///< Receive callback.
275 PKT_SENT_NOTIFY PktSentNotify
; ///< Send callback.
279 /// Internal struct book-keeping send request of IP_IO.
281 /// An IP_IO_SEND_ENTRY will be created each time a send request is issued to
282 /// IP_IO via IpIoSend().
284 typedef struct _IP_IO_SEND_ENTRY
{
289 IP_IO_IP_PROTOCOL Ip
;
291 IP_IO_IP_COMPLETION_TOKEN SndToken
;
295 /// The IP_IO_IP_INFO is used in IpIoSend() to override the default IP instance
298 typedef struct _IP_IO_IP_INFO
{
300 IP_IO_IP_MASK PreMask
;
302 EFI_HANDLE ChildHandle
;
303 IP_IO_IP_PROTOCOL Ip
;
304 IP_IO_IP_COMPLETION_TOKEN DummyRcvToken
;
310 Create a new IP_IO instance.
312 This function uses IP4/IP6 service binding protocol in Controller to create
313 an IP4/IP6 child (aka IP4/IP6 instance).
315 @param[in] Image The image handle of the driver or application that
317 @param[in] Controller The controller handle that has IP4 or IP6 service
318 binding protocol installed.
319 @param[in] IpVersion The version of the IP protocol to use, either
322 @return The pointer to a newly created IP_IO instance, or NULL if failed.
329 IN EFI_HANDLE Controller
,
334 Destroy an IP_IO instance.
336 This function is paired with IpIoCreate(). The IP_IO will be closed first.
337 Resource will be freed afterwards. See IpIoClose().
339 @param[in, out] IpIo The pointer to the IP_IO instance that needs to be
342 @retval EFI_SUCCESS The IP_IO instance was destroyed successfully.
343 @retval Others An error condition occurred.
353 Stop an IP_IO instance.
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 Others Anrror condition occurred.
371 Open an IP_IO instance for use.
373 This function is called after IpIoCreate(). It is used for configuring the IP
374 instance and register the callbacks and their context data for sending and
375 receiving IP packets.
377 @param[in, out] IpIo The pointer to an IP_IO instance that needs
379 @param[in] OpenData The configuration data and callbacks for
382 @retval EFI_SUCCESS The IP_IO instance opened with OpenData
384 @retval EFI_ACCESS_DENIED The IP_IO instance is configured; avoid
386 @retval Others An error condition occurred.
393 IN IP_IO_OPEN_DATA
*OpenData
397 Send out an IP packet.
399 This function is called after IpIoOpen(). The data to be sent are wrapped in
400 Pkt. The IP instance wrapped in IpIo is used for sending by default, but can be
401 overriden by Sender. Other sending configurations, such as source address and gateway
402 address, are specified in OverrideData.
404 @param[in, out] IpIo The pointer to an IP_IO instance used for sending IP
406 @param[in, out] Pkt The pointer to the IP packet to be sent.
407 @param[in] Sender Optional. The IP protocol instance used for sending.
408 @param[in] Context The optional context data.
409 @param[in] NotifyData The optional notify data.
410 @param[in] Dest The destination IP address to send this packet to.
411 @param[in] OverrideData The data to override some configuration of the IP
412 instance used for sending.
414 @retval EFI_SUCCESS The operation completed successfully.
415 @retval EFI_NOT_STARTED The IpIo is not configured.
416 @retval EFI_OUT_OF_RESOURCES Failed due to resource limitations.
424 IN IP_IO_IP_INFO
*Sender OPTIONAL
,
425 IN VOID
*Context OPTIONAL
,
426 IN VOID
*NotifyData OPTIONAL
,
427 IN EFI_IP_ADDRESS
*Dest
,
428 IN IP_IO_OVERRIDE
*OverrideData OPTIONAL
432 Cancel the IP transmit token that wraps this Packet.
434 @param[in] IpIo The pointer to the IP_IO instance.
435 @param[in] Packet The pointer to the packet of NET_BUF to cancel.
446 Add a new IP instance for sending data.
448 The function is used to add the IP_IO to the IP_IO sending list. The caller
449 can later use IpIoFindSender() to get the IP_IO and call IpIoSend() to send
452 @param[in, out] IpIo The pointer to an IP_IO instance to add a new IP
453 instance for sending purposes.
455 @return The pointer to the created IP_IO_IP_INFO structure; NULL if failed.
465 Configure the IP instance of this IpInfo and start the receiving if IpConfigData
468 @param[in, out] IpInfo The pointer to the IP_IO_IP_INFO instance.
469 @param[in, out] IpConfigData The IP4 or IP6 configure data used to configure
470 the IP instance. If NULL, the IP instance is reset.
471 If UseDefaultAddress is set to TRUE, and the configure
472 operation succeeds, the default address information
473 is written back in this IpConfigData.
475 @retval EFI_SUCCESS The IP instance of this IpInfo was configured successfully,
476 or there is no need to reconfigure it.
477 @retval Others The configuration failed.
483 IN OUT IP_IO_IP_INFO
*IpInfo
,
484 IN OUT VOID
*IpConfigData OPTIONAL
488 Destroy an IP instance maintained in IpIo->IpList for
491 This function pairs with IpIoAddIp(). The IpInfo is previously created by
492 IpIoAddIp(). The IP_IO_IP_INFO::RefCnt is decremented and the IP instance
493 will be dstroyed if the RefCnt is zero.
495 @param[in] IpIo The pointer to the IP_IO instance.
496 @param[in] IpInfo The pointer to the IpInfo to be removed.
503 IN IP_IO_IP_INFO
*IpInfo
507 Find the first IP protocol maintained in IpIo whose local
508 address is the same as Src.
510 This function is called when the caller needs the IpIo to send data to the
511 specified Src. The IpIo was added previously by IpIoAddIp().
513 @param[in, out] IpIo The pointer to the pointer of the IP_IO instance.
514 @param[in] IpVersion The version of the IP protocol to use, either
516 @param[in] Src The local IP address.
518 @return The pointer to the IP protocol can be used for sending purpose and its local
519 address is the same with Src.
527 IN EFI_IP_ADDRESS
*Src
531 Get the ICMP error map information.
533 The ErrorStatus will be returned. The IsHard and Notify are optional. If they
534 are not NULL, this routine will fill them.
536 @param[in] IcmpError IcmpError Type.
537 @param[in] IpVersion The version of the IP protocol to use,
539 @param[out] IsHard If TRUE, indicates that it is a hard error.
540 @param[out] Notify If TRUE, SockError needs to be notified.
542 @return The ICMP Error Status, such as EFI_NETWORK_UNREACHABLE.
547 IpIoGetIcmpErrStatus (
550 OUT BOOLEAN
*IsHard OPTIONAL
,
551 OUT BOOLEAN
*Notify OPTIONAL
555 Refresh the remote peer's Neighbor Cache entries.
557 This function is called when the caller needs the IpIo to refresh the existing
558 IPv6 neighbor cache entries since the neighbor is considered reachable by the
559 node has recently received a confirmation that packets sent recently to the
560 neighbor were received by its IP layer.
562 @param[in] IpIo The pointer to an IP_IO instance
563 @param[in] Neighbor The IP address of the neighbor
564 @param[in] Timeout The time in 100-ns units that this entry will
565 remain in the neighbor cache. A value of
566 zero means that the entry is permanent.
567 A value of non-zero means that the entry is
568 dynamic and will be deleted after Timeout.
570 @retval EFI_SUCCESS The operation completed successfully.
571 @retval EFI_NOT_STARTED The IpIo is not configured.
572 @retval EFI_INVALID_PARAMETER The Neighbor Address is invalid.
573 @retval EFI_NOT_FOUND The neighbor cache entry is not in the
575 @retval EFI_OUT_OF_RESOURCES Failed due to resource limitations.
579 IpIoRefreshNeighbor (
581 IN EFI_IP_ADDRESS
*Neighbor
,