1 /** @file
2   Definition for IP4 pesudo interface structure.
3 
4 Copyright (c) 2005 - 2015, Intel Corporation. All rights reserved.<BR>
5 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
9 
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.
12 
13 **/
14 
15 #ifndef __EFI_IP4_IF_H__
16 #define __EFI_IP4_IF_H__
17 
18 #define IP4_FRAME_RX_SIGNATURE  SIGNATURE_32 ('I', 'P', 'F', 'R')
19 #define IP4_FRAME_TX_SIGNATURE  SIGNATURE_32 ('I', 'P', 'F', 'T')
20 #define IP4_FRAME_ARP_SIGNATURE SIGNATURE_32 ('I', 'P', 'F', 'A')
21 #define IP4_INTERFACE_SIGNATURE SIGNATURE_32 ('I', 'P', 'I', 'F')
22 
23 /**
24   This prototype is used by both receive and transmission.
25   When receiving Netbuf is allocated by IP4_INTERFACE, and
26   released by IP4. Flag shows whether the frame is received
27   as link broadcast/multicast...
28 
29   When transmitting, the Netbuf is from IP4, and provided
30   to the callback as a reference. Flag isn't used.
31 
32   @param[in] IpInstance The instance that sent or received the packet.
33                         IpInstance can be NULL which means that it is the IP4 driver
34                         itself sending the packets. IP4 driver may send packets that
35                         don't belong to any instance, such as ICMP errors, ICMP echo
36                         responses, or IGMP packets. IpInstance is used as a tag in
37                         this module.
38   @param[in] Packet     The sent or received packet.
39   @param[in] IoStatus   Status of sending or receiving.
40   @param[in] LinkFlag   Indicate if the frame is received as link broadcast/multicast.
41                         When transmitting, it is not used.
42   @param[in] Context    Additional data for callback.
43 
44   @retval None.
45 **/
46 typedef
47 VOID
48 (*IP4_FRAME_CALLBACK)(
49   IN IP4_PROTOCOL              *IpInstance       OPTIONAL,
50   IN NET_BUF                   *Packet,
51   IN EFI_STATUS                IoStatus,
52   IN UINT32                    LinkFlag,
53   IN VOID                      *Context
54   );
55 
56 ///
57 /// Each receive request is wrapped in an IP4_LINK_RX_TOKEN.
58 /// Upon completion, the Callback will be called. Only one
59 /// receive request is send to MNP. IpInstance is always NULL.
60 /// Reference MNP's spec for information.
61 ///
62 typedef struct {
63   UINT32                                Signature;
64   IP4_INTERFACE                         *Interface;
65 
66   IP4_PROTOCOL                          *IpInstance;
67   IP4_FRAME_CALLBACK                    CallBack;
68   VOID                                  *Context;
69 
70   EFI_MANAGED_NETWORK_COMPLETION_TOKEN  MnpToken;
71 } IP4_LINK_RX_TOKEN;
72 
73 ///
74 /// Each transmit request is wrapped in an IP4_LINK_TX_TOKEN.
75 /// Upon completion, the Callback will be called.
76 ///
77 typedef struct {
78   UINT32                                Signature;
79   LIST_ENTRY                            Link;
80 
81   IP4_INTERFACE                         *Interface;
82 
83   IP4_PROTOCOL                          *IpInstance;
84   IP4_FRAME_CALLBACK                    CallBack;
85   NET_BUF                               *Packet;
86   VOID                                  *Context;
87 
88   EFI_MAC_ADDRESS                       DstMac;
89   EFI_MAC_ADDRESS                       SrcMac;
90 
91   EFI_MANAGED_NETWORK_COMPLETION_TOKEN  MnpToken;
92   EFI_MANAGED_NETWORK_TRANSMIT_DATA     MnpTxData;
93 } IP4_LINK_TX_TOKEN;
94 
95 ///
96 /// Only one ARP request is requested for all the frames in
97 /// a time. It is started for the first frames to the Ip. Any
98 /// subsequent transmission frame will be linked to Frames, and
99 /// be sent all at once the ARP requests succeed.
100 ///
101 typedef struct {
102   UINT32                  Signature;
103   LIST_ENTRY              Link;
104 
105   LIST_ENTRY              Frames;
106   IP4_INTERFACE           *Interface;
107 
108   //
109   // ARP requesting staffs
110   //
111   EFI_EVENT               OnResolved;
112   IP4_ADDR                Ip;
113   EFI_MAC_ADDRESS         Mac;
114 } IP4_ARP_QUE;
115 
116 /**
117   Callback to select which frame to cancel. Caller can cancel a
118   single frame, or all the frame from an IP instance.
119 
120   @param Frame      The sending frame to check for cancellation.
121   @param Context    Additional data for callback.
122 
123   @retval TRUE      The sending of the frame should be cancelled.
124   @retval FALSE     Do not cancel the frame sending.
125 **/
126 typedef
127 BOOLEAN
128 (*IP4_FRAME_TO_CANCEL)(
129   IP4_LINK_TX_TOKEN       *Frame,
130   VOID                    *Context
131   );
132 
133 //
134 // Each IP4 instance has its own station address. All the instances
135 // with the same station address share a single interface structure.
136 // Each interface has its own ARP child, and shares one MNP child.
137 // Notice the special cases that DHCP can configure the interface
138 // with 0.0.0.0/0.0.0.0.
139 //
140 struct _IP4_INTERFACE {
141   UINT32                        Signature;
142   LIST_ENTRY                    Link;
143   INTN                          RefCnt;
144 
145   //
146   // IP address and subnet mask of the interface. It also contains
147   // the subnet/net broadcast address for quick access. The fileds
148   // are invalid if (Configured == FALSE)
149   //
150   IP4_ADDR                      Ip;
151   IP4_ADDR                      SubnetMask;
152   IP4_ADDR                      SubnetBrdcast;
153   IP4_ADDR                      NetBrdcast;
154   BOOLEAN                       Configured;
155 
156   //
157   // Handle used to create/destroy ARP child. All the IP children
158   // share one MNP which is owned by IP service binding.
159   //
160   EFI_HANDLE                    Controller;
161   EFI_HANDLE                    Image;
162 
163   EFI_MANAGED_NETWORK_PROTOCOL  *Mnp;
164   EFI_ARP_PROTOCOL              *Arp;
165   EFI_HANDLE                    ArpHandle;
166 
167   //
168   // Queues to keep the frames sent and waiting ARP request.
169   //
170   LIST_ENTRY                    ArpQues;
171   LIST_ENTRY                    SentFrames;
172   IP4_LINK_RX_TOKEN             *RecvRequest;
173 
174   //
175   // The interface's MAC and broadcast MAC address.
176   //
177   EFI_MAC_ADDRESS               Mac;
178   EFI_MAC_ADDRESS               BroadcastMac;
179   UINT32                        HwaddrLen;
180 
181   //
182   // All the IP instances that have the same IP/SubnetMask are linked
183   // together through IpInstances. If any of the instance enables
184   // promiscuous receive, PromiscRecv is true.
185   //
186   LIST_ENTRY                    IpInstances;
187   BOOLEAN                       PromiscRecv;
188 };
189 
190 /**
191   Create an IP4_INTERFACE. Delay the creation of ARP instance until
192   the interface is configured.
193 
194   @param[in]  Mnp               The shared MNP child of this IP4 service binding
195                                 instance.
196   @param[in]  Controller        The controller this IP4 service binding instance
197                                 is installed. Most like the UNDI handle.
198   @param[in]  ImageHandle       This driver's image handle.
199 
200   @return Point to the created IP4_INTERFACE, otherwise NULL.
201 
202 **/
203 IP4_INTERFACE *
204 Ip4CreateInterface (
205   IN  EFI_MANAGED_NETWORK_PROTOCOL  *Mnp,
206   IN  EFI_HANDLE                    Controller,
207   IN  EFI_HANDLE                    ImageHandle
208   );
209 
210 /**
211   Set the interface's address, create and configure
212   the ARP child if necessary.
213 
214   @param  Interface         The interface to set the address.
215   @param  IpAddr            The interface's IP address.
216   @param  SubnetMask        The interface's netmask.
217 
218   @retval EFI_SUCCESS           The interface is configured with Ip/netmask pair,
219                                 and a ARP is created for it.
220   @retval Others                Failed to set the interface's address.
221 
222 **/
223 EFI_STATUS
224 Ip4SetAddress (
225   IN OUT IP4_INTERFACE      *Interface,
226   IN     IP4_ADDR           IpAddr,
227   IN     IP4_ADDR           SubnetMask
228   );
229 
230 /**
231   Free the interface used by IpInstance. All the IP instance with
232   the same Ip/Netmask pair share the same interface. It is reference
233   counted. All the frames haven't been sent will be cancelled.
234   Because the IpInstance is optional, the caller must remove
235   IpInstance from the interface's instance list itself.
236 
237   @param[in]  Interface         The interface used by the IpInstance.
238   @param[in]  IpInstance        The Ip instance that free the interface. NULL if
239                                 the Ip driver is releasing the default interface.
240 
241   @retval EFI_SUCCESS           The interface use IpInstance is freed.
242 
243 **/
244 EFI_STATUS
245 Ip4FreeInterface (
246   IN  IP4_INTERFACE         *Interface,
247   IN  IP4_PROTOCOL          *IpInstance           OPTIONAL
248   );
249 
250 /**
251   Send a frame from the interface. If the next hop is broadcast or
252   multicast address, it is transmitted immediately. If the next hop
253   is a unicast, it will consult ARP to resolve the NextHop's MAC.
254   If some error happened, the CallBack won't be called. So, the caller
255   must test the return value, and take action when there is an error.
256 
257   @param[in]  Interface         The interface to send the frame from
258   @param[in]  IpInstance        The IP child that request the transmission.  NULL
259                                 if it is the IP4 driver itself.
260   @param[in]  Packet            The packet to transmit.
261   @param[in]  NextHop           The immediate destination to transmit the packet
262                                 to.
263   @param[in]  CallBack          Function to call back when transmit finished.
264   @param[in]  Context           Opaque parameter to the call back.
265 
266   @retval EFI_OUT_OF_RESOURCES  Failed to allocate resource to send the frame
267   @retval EFI_NO_MAPPING        Can't resolve the MAC for the nexthop
268   @retval EFI_SUCCESS           The packet is successfully transmitted.
269   @retval other                 Other error occurs.
270 
271 **/
272 EFI_STATUS
273 Ip4SendFrame (
274   IN  IP4_INTERFACE         *Interface,
275   IN  IP4_PROTOCOL          *IpInstance       OPTIONAL,
276   IN  NET_BUF               *Packet,
277   IN  IP4_ADDR              NextHop,
278   IN  IP4_FRAME_CALLBACK    CallBack,
279   IN  VOID                  *Context
280   );
281 
282 /**
283   Remove all the frames on the interface that pass the FrameToCancel,
284   either queued on ARP queues or that have already been delivered to
285   MNP and not yet recycled.
286 
287   @param[in]  Interface         Interface to remove the frames from.
288   @param[in]  IoStatus          The transmit status returned to the frames'
289                                 callback.
290   @param[in]  FrameToCancel     Function to select the frame to cancel, NULL to
291                                 select all.
292   @param[in]  Context           Opaque parameters passed to FrameToCancel.
293 
294 **/
295 VOID
296 Ip4CancelFrames (
297   IN IP4_INTERFACE          *Interface,
298   IN EFI_STATUS             IoStatus,
299   IN IP4_FRAME_TO_CANCEL    FrameToCancel    OPTIONAL,
300   IN VOID                   *Context
301   );
302 
303 /**
304   If there is a pending receive request, cancel it. Don't call
305   the receive request's callback because this function can be only
306   called if the instance or driver is tearing itself down. It
307   doesn't make sense to call it back. But it is necessary to call
308   the transmit token's callback to give it a chance to free the
309   packet and update the upper layer's transmit request status, say
310   that from the UDP.
311 
312   @param[in]  Interface         The interface used by the IpInstance
313 
314 **/
315 VOID
316 Ip4CancelReceive (
317   IN IP4_INTERFACE          *Interface
318   );
319 
320 /**
321   Request to receive the packet from the interface.
322 
323   @param[in]  Interface         The interface to receive the frames from.
324   @param[in]  IpInstance        The instance that requests the receive. NULL for
325                                 the driver itself.
326   @param[in]  CallBack          Function to call when receive finished.
327   @param[in]  Context           Opaque parameter to the callback.
328 
329   @retval EFI_ALREADY_STARTED   There is already a pending receive request.
330   @retval EFI_OUT_OF_RESOURCES  Failed to allocate resource to receive.
331   @retval EFI_SUCCESS           The recieve request has been started.
332   @retval other                 Other error occurs.
333 
334 **/
335 EFI_STATUS
336 Ip4ReceiveFrame (
337   IN  IP4_INTERFACE         *Interface,
338   IN  IP4_PROTOCOL          *IpInstance       OPTIONAL,
339   IN  IP4_FRAME_CALLBACK    CallBack,
340   IN  VOID                  *Context
341   );
342 
343 #endif
344