2 This library provides IpIo layer upon EFI IP4 Protocol.
4 Copyright (c) 2005 - 2008, Intel Corporation
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 #include <Protocol/Ip4.h>
19 #include <Library/IpIoLib.h>
20 #include <Library/NetLib.h>
23 // type and code define for ICMP protocol error got
26 #define ICMP_TYPE_UNREACH 3
27 #define ICMP_TYPE_TIMXCEED 11
28 #define ICMP_TYPE_PARAMPROB 12
29 #define ICMP_TYPE_SOURCEQUENCH 4
31 #define ICMP_CODE_UNREACH_NET 0
32 #define ICMP_CODE_UNREACH_HOST 1
33 #define ICMP_CODE_UNREACH_PROTOCOL 2
34 #define ICMP_CODE_UNREACH_PORT 3
35 #define ICMP_CODE_UNREACH_NEEDFRAG 4
36 #define ICMP_CODE_UNREACH_SRCFAIL 5
37 #define ICMP_CODE_UNREACH_NET_UNKNOWN 6
38 #define ICMP_CODE_UNREACH_HOST_UNKNOWN 7
39 #define ICMP_CODE_UNREACH_ISOLATED 8
40 #define ICMP_CODE_UNREACH_NET_PROHIB 9
41 #define ICMP_CODE_UNREACH_HOST_PROHIB 10
42 #define ICMP_CODE_UNREACH_TOSNET 11
43 #define ICMP_CODE_UNREACH_TOSHOST 12
46 /// This error will be delivered to the
47 /// listening transportation layer protocol
48 /// that consumes IpIO.
51 ICMP_ERR_UNREACH_NET
= 0,
52 ICMP_ERR_UNREACH_HOST
,
53 ICMP_ERR_UNREACH_PROTOCOL
,
54 ICMP_ERR_UNREACH_PORT
,
56 ICMP_ERR_UNREACH_SRCFAIL
,
57 ICMP_ERR_TIMXCEED_INTRANS
,
58 ICMP_ERR_TIMXCEED_REASS
,
64 /// The helper struct for IpIoGetIcmpErrStatus(). It is internal-use only.
66 typedef struct _ICMP_ERROR_INFO
{
72 Get the IP header length from EFI_IP4_HEADER struct.
74 @param HdrPtr A pointer to EFI_IP4_HEADER
76 @return The IP header length
78 #define EFI_IP4_HEADER_LEN(HdrPtr) ((HdrPtr)->HeaderLength << 2)
80 extern EFI_IP4_CONFIG_DATA mIpIoDefaultIpConfigData
;
83 /// The IP session for an IP receive packet.
85 typedef struct _EFI_NET_SESSION_DATA
{
86 IP4_ADDR Source
; ///< Source IP of the received packet
87 IP4_ADDR Dest
; ///< Destination IP of the received packet
88 EFI_IP4_HEADER
*IpHdr
; ///< IP4 header of the received packet
89 } EFI_NET_SESSION_DATA
;
92 The prototype is called back when an IP packet is received.
94 @param Status Result of the receive request
95 @param IcmpErr Valid when Status is EFI_ICMP_ERROR
96 @param NetSession The IP session for the received packet
97 @param Pkt Packet received
98 @param Context The data provided by user for the received packet when
99 the callback is registered in IP_IO_OPEN_DATA::RcvdContext.
105 IN EFI_STATUS Status
,
106 IN ICMP_ERROR IcmpErr
,
107 IN EFI_NET_SESSION_DATA
*NetSession
,
113 The prototype is called back when an IP packet is sent.
115 @param Status Result of the sending
116 @param Context The data provided by user for the received packet when
117 the callback is registered in IP_IO_OPEN_DATA::SndContext.
118 @param Sender A pointer to EFI_IP4_PROTOCOL for sender
119 @param NotifyData Context data specified when calling IpIoSend()
125 IN EFI_STATUS Status
,
132 /// The data structure wraps Ip4 instance. It is used by IpIo Library to do all
135 typedef struct _IP_IO
{
137 /// The node used to link this IpIo to the active IpIo list.
142 /// The list used to maintain the IP instance for different sending purpose.
146 EFI_HANDLE Controller
;
148 EFI_HANDLE ChildHandle
;
150 // The IP instance consumed by this IP_IO
152 EFI_IP4_PROTOCOL
*Ip
;
153 BOOLEAN IsConfigured
;
156 /// some ip config data can be changed
161 /// Token and event used to get data from IP
163 EFI_IP4_COMPLETION_TOKEN RcvToken
;
166 /// List entry used to link the token passed to IP_IO
168 LIST_ENTRY PendingSndList
;
171 // User interface used to get notify from IP_IO
173 VOID
*RcvdContext
; ///< See IP_IO_OPEN_DATA::RcvdContext
174 VOID
*SndContext
; ///< See IP_IO_OPEN_DATA::SndContext
175 PKT_RCVD_NOTIFY PktRcvdNotify
; ///< See IP_IO_OPEN_DATA::PktRcvdNotify
176 PKT_SENT_NOTIFY PktSentNotify
; ///< See IP_IO_OPEN_DATA::PktSentNotify
180 /// The struct is used for user to pass IP configuration and callbacks to IP_IO.
181 /// It is used by IpIoOpen().
183 typedef struct _IP_IO_OPEN_DATA
{
184 EFI_IP4_CONFIG_DATA IpConfigData
; ///< Configuration of the IP instance
185 VOID
*RcvdContext
; ///< Context data used by receive callback
186 VOID
*SndContext
; ///< Context data used by send callback
187 PKT_RCVD_NOTIFY PktRcvdNotify
; ///< Receive callback
188 PKT_SENT_NOTIFY PktSentNotify
; ///< Send callback
192 /// Internal struct book-keeping send request of IP_IO.
194 /// An IP_IO_SEND_ENTRY will be created each time a send request is issued to
195 /// IP_IO via IpIoSend().
197 typedef struct _IP_IO_SEND_ENTRY
{
202 EFI_IP4_PROTOCOL
*Ip
;
204 EFI_IP4_COMPLETION_TOKEN
*SndToken
;
207 typedef EFI_IP4_OVERRIDE_DATA IP_IO_OVERRIDE
;
210 /// The IP_IO_IP_INFO is used in IpIoSend() to override the default IP instance
213 typedef struct _IP_IO_IP_INFO
{
217 EFI_HANDLE ChildHandle
;
218 EFI_IP4_PROTOCOL
*Ip
;
219 EFI_IP4_COMPLETION_TOKEN DummyRcvToken
;
224 Create a new IP_IO instance.
226 This function uses IP4 service binding protocol in Controller to create an IP4
227 child (aka IP4 instance).
229 @param Image The image handle of the driver or application that
231 @param Controller The controller handle that has IP4 service binding
234 @return Pointer to a newly created IP_IO instance, or NULL if failed.
241 IN EFI_HANDLE Controller
245 Destroy an IP_IO instance.
247 This function is paired with IpIoCreate(). The IP_IO will be closed first.
248 Resource will be freed afterwards. See IpIoClose().
250 @param IpIo Pointer to the IP_IO instance that needs to be
253 @retval EFI_SUCCESS The IP_IO instance destroyed successfully.
254 @retval Other Error condition occurred.
264 Stop an IP_IO instance.
266 This function is paired with IpIoOpen(). The IP_IO will be unconfigured and all
267 the pending send/receive tokens will be canceled.
269 @param IpIo Pointer to the IP_IO instance that needs to stop.
271 @retval EFI_SUCCESS The IP_IO instance stopped successfully.
272 @retval Other Error condition occurred.
282 Open an IP_IO instance for use.
284 This function is called after IpIoCreate(). It is used for configuring the IP
285 instance and register the callbacks and their context data for sending and
286 receiving IP packets.
288 @param IpIo Pointer to an IP_IO instance that needs to open.
289 @param OpenData The configuration data and callbacks for the IP_IO
292 @retval EFI_SUCCESS The IP_IO instance opened with OpenData
294 @retval Other Error condition occurred.
301 IN IP_IO_OPEN_DATA
*OpenData
305 Send out an IP packet.
307 This function is called after IpIoOpen(). The data to be sent are wrapped in
308 Pkt. The IP instance wrapped in IpIo is used for sending by default but can be
309 overriden by Sender. Other sending configs, like source address and gateway
310 address etc., are specified in OverrideData.
312 @param IpIo Pointer to an IP_IO instance used for sending IP
314 @param Pkt Pointer to the IP packet to be sent.
315 @param Sender The IP protocol instance used for sending.
316 @param Context Optional context data
317 @param NotifyData Optional notify data
318 @param Dest The destination IP address to send this packet to.
319 @param OverrideData The data to override some configuration of the IP
320 instance used for sending.
322 @retval EFI_SUCCESS The operation is completed successfully.
323 @retval EFI_NOT_STARTED The IpIo is not configured.
324 @retval EFI_OUT_OF_RESOURCES Failed due to resource limit.
332 IN IP_IO_IP_INFO
*Sender OPTIONAL
,
333 IN VOID
*Context OPTIONAL
,
334 IN VOID
*NotifyData OPTIONAL
,
336 IN IP_IO_OVERRIDE
*OverrideData OPTIONAL
340 Cancel the IP transmit token which wraps this Packet.
342 @param IpIo Pointer to the IP_IO instance.
343 @param Packet Pointer to the packet of NET_BUF to cancel.
354 Add a new IP instance for sending data.
356 The function is used to add the IP_IO to the IP_IO sending list. The caller
357 can later use IpIoFindSender() to get the IP_IO and call IpIoSend() to send
360 @param IpIo Pointer to a IP_IO instance to add a new IP
361 instance for sending purpose.
363 @return Pointer to the created IP_IO_IP_INFO structure, NULL if failed.
373 Configure the IP instance of this IpInfo and start the receiving if Ip4ConfigData
376 @param IpInfo Pointer to the IP_IO_IP_INFO instance.
377 @param Ip4ConfigData The IP4 configure data used to configure the IP
378 instance, if NULL the IP instance is reset. If
379 UseDefaultAddress is set to TRUE, and the configure
380 operation succeeds, the default address information
381 is written back in this Ip4ConfigData.
383 @retval EFI_STATUS The status returned by IP4->Configure or
385 @retval Other Configuration fails.
391 IN IP_IO_IP_INFO
*IpInfo
,
392 IN OUT EFI_IP4_CONFIG_DATA
*Ip4ConfigData OPTIONAL
396 Destroy an IP instance maintained in IpIo->IpList for
399 This function pairs with IpIoAddIp(). The IpInfo is previously created by
400 IpIoAddIp(). The IP_IO_IP_INFO::RefCnt is decremented and the IP instance
401 will be dstroyed if the RefCnt is zero.
403 @param IpIo Pointer to the IP_IO instance.
404 @param IpInfo Pointer to the IpInfo to be removed.
411 IN IP_IO_IP_INFO
*IpInfo
415 Find the first IP protocol maintained in IpIo whose local
416 address is the same with Src.
418 This function is called when the caller needs the IpIo to send data to the
419 specified Src. The IpIo was added previously by IpIoAddIp().
421 @param IpIo Pointer to the pointer of the IP_IO instance.
422 @param Src The local IP address.
424 @return Pointer to the IP protocol can be used for sending purpose and its local
425 address is the same with Src.
436 Get the ICMP error map information.
438 The ErrorStatus will be returned. The IsHard and Notify are optional. If they
439 are not NULL, this routine will fill them.
441 @param IcmpError IcmpError Type
442 @param IsHard Whether it is a hard error
443 @param Notify Whether it need to notify SockError
445 @return ICMP Error Status
450 IpIoGetIcmpErrStatus (
451 IN ICMP_ERROR IcmpError
,
452 OUT BOOLEAN
*IsHard OPTIONAL
,
453 OUT BOOLEAN
*Notify OPTIONAL