1 /** @file
2   Ip4 internal functions and type defintions.
3 
4 Copyright (c) 2005 - 2015, Intel Corporation. All rights reserved.<BR>
5 (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>
6 
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution.  The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
11 
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14 
15 **/
16 
17 #ifndef __EFI_IP4_IMPL_H__
18 #define __EFI_IP4_IMPL_H__
19 
20 #include <Uefi.h>
21 
22 #include <Protocol/IpSec.h>
23 #include <Protocol/Ip4.h>
24 #include <Protocol/Ip4Config2.h>
25 #include <Protocol/Arp.h>
26 #include <Protocol/ManagedNetwork.h>
27 #include <Protocol/Dhcp4.h>
28 #include <Protocol/HiiConfigRouting.h>
29 #include <Protocol/HiiConfigAccess.h>
30 
31 #include <Library/DebugLib.h>
32 #include <Library/UefiRuntimeServicesTableLib.h>
33 #include <Library/UefiDriverEntryPoint.h>
34 #include <Library/UefiBootServicesTableLib.h>
35 #include <Library/BaseLib.h>
36 #include <Library/UefiLib.h>
37 #include <Library/NetLib.h>
38 #include <Library/BaseMemoryLib.h>
39 #include <Library/MemoryAllocationLib.h>
40 #include <Library/DpcLib.h>
41 #include <Library/PrintLib.h>
42 #include <Library/DevicePathLib.h>
43 #include <Library/HiiLib.h>
44 #include <Library/UefiHiiServicesLib.h>
45 
46 #include "Ip4Common.h"
47 #include "Ip4Driver.h"
48 #include "Ip4If.h"
49 #include "Ip4Icmp.h"
50 #include "Ip4Option.h"
51 #include "Ip4Igmp.h"
52 #include "Ip4Route.h"
53 #include "Ip4Input.h"
54 #include "Ip4Output.h"
55 #include "Ip4Config2Impl.h"
56 #include "Ip4Config2Nv.h"
57 #include "Ip4NvData.h"
58 
59 #define IP4_PROTOCOL_SIGNATURE  SIGNATURE_32 ('I', 'P', '4', 'P')
60 #define IP4_SERVICE_SIGNATURE   SIGNATURE_32 ('I', 'P', '4', 'S')
61 
62 //
63 // The state of IP4 protocol. It starts from UNCONFIGED. if it is
64 // successfully configured, it goes to CONFIGED. if configure NULL
65 // is called, it becomes UNCONFIGED again. If (partly) destroyed, it
66 // becomes DESTROY.
67 //
68 #define IP4_STATE_UNCONFIGED    0
69 #define IP4_STATE_CONFIGED      1
70 #define IP4_STATE_DESTROY       2
71 
72 //
73 // The state of IP4 service. It starts from UNSTARTED. It transits
74 // to STARTED if autoconfigure is started. If default address is
75 // configured, it becomes CONFIGED. and if partly destroyed, it goes
76 // to DESTROY.
77 //
78 #define IP4_SERVICE_UNSTARTED   0
79 #define IP4_SERVICE_STARTED     1
80 #define IP4_SERVICE_CONFIGED    2
81 #define IP4_SERVICE_DESTROY     3
82 
83 
84 ///
85 /// IP4_TXTOKEN_WRAP wraps the upper layer's transmit token.
86 /// The user's data is kept in the Packet. When fragment is
87 /// needed, each fragment of the Packet has a reference to the
88 /// Packet, no data is actually copied. The Packet will be
89 /// released when all the fragments of it have been recycled by
90 /// MNP. Upon then, the IP4_TXTOKEN_WRAP will be released, and
91 /// user's event signalled.
92 ///
93 typedef struct {
94   IP4_PROTOCOL              *IpInstance;
95   EFI_IP4_COMPLETION_TOKEN  *Token;
96   EFI_EVENT                 IpSecRecycleSignal;
97   NET_BUF                   *Packet;
98   BOOLEAN                   Sent;
99   INTN                      Life;
100 } IP4_TXTOKEN_WRAP;
101 
102 ///
103 /// IP4_IPSEC_WRAP wraps the packet received from MNP layer. The packet
104 /// will be released after it has been processed by the receiver. Upon then,
105 /// the IP4_IPSEC_WRAP will be released, and the IpSecRecycleSignal will be signaled
106 /// to notice IPsec to free the resources.
107 ///
108 typedef struct {
109   EFI_EVENT                 IpSecRecycleSignal;
110   NET_BUF                   *Packet;
111 } IP4_IPSEC_WRAP;
112 
113 ///
114 /// IP4_RXDATA_WRAP wraps the data IP4 child delivers to the
115 /// upper layers. The received packet is kept in the Packet.
116 /// The Packet itself may be constructured from some fragments.
117 /// All the fragments of the Packet is organized by a
118 /// IP4_ASSEMBLE_ENTRY structure. If the Packet is recycled by
119 /// the upper layer, the assemble entry and its associated
120 /// fragments will be freed at last.
121 ///
122 typedef struct {
123   LIST_ENTRY                Link;
124   IP4_PROTOCOL              *IpInstance;
125   NET_BUF                   *Packet;
126   EFI_IP4_RECEIVE_DATA      RxData;
127 } IP4_RXDATA_WRAP;
128 
129 
130 struct _IP4_PROTOCOL {
131   UINT32                    Signature;
132 
133   EFI_IP4_PROTOCOL          Ip4Proto;
134   EFI_HANDLE                Handle;
135   INTN                      State;
136 
137   IP4_SERVICE               *Service;
138   LIST_ENTRY                Link;       // Link to all the IP protocol from the service
139 
140   //
141   // User's transmit/receive tokens, and received/deliverd packets
142   //
143   NET_MAP                   RxTokens;
144   NET_MAP                   TxTokens;   // map between (User's Token, IP4_TXTOKE_WRAP)
145   LIST_ENTRY                Received;   // Received but not delivered packet
146   LIST_ENTRY                Delivered;  // Delivered and to be recycled packets
147   EFI_LOCK                  RecycleLock;
148 
149   //
150   // Instance's address and route tables. There are two route tables.
151   // RouteTable is used by the IP4 driver to route packet. EfiRouteTable
152   // is used to communicate the current route info to the upper layer.
153   //
154   IP4_INTERFACE             *Interface;
155   LIST_ENTRY                AddrLink;   // Ip instances with the same IP address.
156   IP4_ROUTE_TABLE           *RouteTable;
157 
158   EFI_IP4_ROUTE_TABLE       *EfiRouteTable;
159   UINT32                    EfiRouteCount;
160 
161   //
162   // IGMP data for this instance
163   //
164   IP4_ADDR                  *Groups;    // stored in network byte order
165   UINT32                    GroupCount;
166 
167   EFI_IP4_CONFIG_DATA       ConfigData;
168 
169 };
170 
171 struct _IP4_SERVICE {
172   UINT32                          Signature;
173   EFI_SERVICE_BINDING_PROTOCOL    ServiceBinding;
174   INTN                            State;
175 
176   //
177   // List of all the IP instances and interfaces, and default
178   // interface and route table and caches.
179   //
180   UINTN                           NumChildren;
181   LIST_ENTRY                      Children;
182 
183   LIST_ENTRY                      Interfaces;
184 
185   IP4_INTERFACE                   *DefaultInterface;
186   IP4_ROUTE_TABLE                 *DefaultRouteTable;
187 
188   //
189   // Ip reassemble utilities, and IGMP data
190   //
191   IP4_ASSEMBLE_TABLE              Assemble;
192   IGMP_SERVICE_DATA               IgmpCtrl;
193 
194   //
195   // Low level protocol used by this service instance
196   //
197   EFI_HANDLE                      Image;
198   EFI_HANDLE                      Controller;
199 
200   EFI_HANDLE                      MnpChildHandle;
201   EFI_MANAGED_NETWORK_PROTOCOL    *Mnp;
202 
203   EFI_MANAGED_NETWORK_CONFIG_DATA MnpConfigData;
204   EFI_SIMPLE_NETWORK_MODE         SnpMode;
205 
206   EFI_EVENT                       Timer;
207 
208   EFI_EVENT                       ReconfigEvent;
209 
210   BOOLEAN                         Reconfig;
211 
212   //
213   // Underlying media present status.
214   //
215   BOOLEAN                         MediaPresent;
216 
217   //
218   // IPv4 Configuration II Protocol instance
219   //
220   IP4_CONFIG2_INSTANCE            Ip4Config2Instance;
221 
222   CHAR16                          *MacString;
223 
224   UINT32                          MaxPacketSize;
225   UINT32                          OldMaxPacketSize; ///< The MTU before IPsec enable.
226 };
227 
228 #define IP4_INSTANCE_FROM_PROTOCOL(Ip4) \
229           CR ((Ip4), IP4_PROTOCOL, Ip4Proto, IP4_PROTOCOL_SIGNATURE)
230 
231 #define IP4_SERVICE_FROM_PROTOCOL(Sb)   \
232           CR ((Sb), IP4_SERVICE, ServiceBinding, IP4_SERVICE_SIGNATURE)
233 
234 #define IP4_SERVICE_FROM_CONFIG2_INSTANCE(This) \
235   CR (This, IP4_SERVICE, Ip4Config2Instance, IP4_SERVICE_SIGNATURE)
236 
237 
238 #define IP4_NO_MAPPING(IpInstance) (!(IpInstance)->Interface->Configured)
239 
240 extern EFI_IP4_PROTOCOL mEfiIp4ProtocolTemplete;
241 
242 /**
243   Config the MNP parameter used by IP. The IP driver use one MNP
244   child to transmit/receive frames. By default, it configures MNP
245   to receive unicast/multicast/broadcast. And it will enable/disable
246   the promiscous receive according to whether there is IP child
247   enable that or not. If Force is FALSE, it will iterate through
248   all the IP children to check whether the promiscuous receive
249   setting has been changed. If it hasn't been changed, it won't
250   reconfigure the MNP. If Force is TRUE, the MNP is configured no
251   matter whether that is changed or not.
252 
253   @param[in]  IpSb               The IP4 service instance that is to be changed.
254   @param[in]  Force              Force the configuration or not.
255 
256   @retval EFI_SUCCESS            The MNP is successfully configured/reconfigured.
257   @retval Others                 Configuration failed.
258 
259 **/
260 EFI_STATUS
261 Ip4ServiceConfigMnp (
262   IN IP4_SERVICE            *IpSb,
263   IN BOOLEAN                Force
264   );
265 
266 /**
267   Intiialize the IP4_PROTOCOL structure to the unconfigured states.
268 
269   @param  IpSb                   The IP4 service instance.
270   @param  IpInstance             The IP4 child instance.
271 
272 **/
273 VOID
274 Ip4InitProtocol (
275   IN     IP4_SERVICE            *IpSb,
276   IN OUT IP4_PROTOCOL           *IpInstance
277   );
278 
279 /**
280   Clean up the IP4 child, release all the resources used by it.
281 
282   @param[in]  IpInstance         The IP4 child to clean up.
283 
284   @retval EFI_SUCCESS            The IP4 child is cleaned up.
285   @retval EFI_DEVICE_ERROR       Some resources failed to be released.
286 
287 **/
288 EFI_STATUS
289 Ip4CleanProtocol (
290   IN  IP4_PROTOCOL          *IpInstance
291   );
292 
293 /**
294   Cancel the user's receive/transmit request.
295 
296   @param[in]  IpInstance         The IP4 child.
297   @param[in]  Token              The token to cancel. If NULL, all token will be
298                                  cancelled.
299 
300   @retval EFI_SUCCESS            The token is cancelled.
301   @retval EFI_NOT_FOUND          The token isn't found on either the
302                                  transmit/receive queue.
303   @retval EFI_DEVICE_ERROR       Not all token is cancelled when Token is NULL.
304 
305 **/
306 EFI_STATUS
307 Ip4Cancel (
308   IN IP4_PROTOCOL             *IpInstance,
309   IN EFI_IP4_COMPLETION_TOKEN *Token          OPTIONAL
310   );
311 
312 /**
313   Change the IP4 child's multicast setting. The caller
314   should make sure that the parameters is valid.
315 
316   @param[in]  IpInstance             The IP4 child to change the setting.
317   @param[in]  JoinFlag               TRUE to join the group, otherwise leave it
318   @param[in]  GroupAddress           The target group address
319 
320   @retval EFI_ALREADY_STARTED    Want to join the group, but already a member of it
321   @retval EFI_OUT_OF_RESOURCES   Failed to allocate some resources.
322   @retval EFI_DEVICE_ERROR       Failed to set the group configuraton
323   @retval EFI_SUCCESS            Successfully updated the group setting.
324   @retval EFI_NOT_FOUND          Try to leave the group which it isn't a member.
325 
326 **/
327 EFI_STATUS
328 Ip4Groups (
329   IN IP4_PROTOCOL           *IpInstance,
330   IN BOOLEAN                JoinFlag,
331   IN EFI_IPv4_ADDRESS       *GroupAddress       OPTIONAL
332   );
333 
334 /**
335   The heart beat timer of IP4 service instance. It times out
336   all of its IP4 children's received-but-not-delivered and
337   transmitted-but-not-recycle packets, and provides time input
338   for its IGMP protocol.
339 
340   @param[in]  Event                  The IP4 service instance's heart beat timer.
341   @param[in]  Context                The IP4 service instance.
342 
343 **/
344 VOID
345 EFIAPI
346 Ip4TimerTicking (
347   IN EFI_EVENT              Event,
348   IN VOID                   *Context
349   );
350 
351 /**
352   Decrease the life of the transmitted packets. If it is
353   decreased to zero, cancel the packet. This function is
354   called by Ip4PacketTimerTicking which time out both the
355   received-but-not-delivered and transmitted-but-not-recycle
356   packets.
357 
358   @param[in]  Map                    The IP4 child's transmit map.
359   @param[in]  Item                   Current transmitted packet.
360   @param[in]  Context                Not used.
361 
362   @retval EFI_SUCCESS            Always returns EFI_SUCCESS.
363 
364 **/
365 EFI_STATUS
366 EFIAPI
367 Ip4SentPacketTicking (
368   IN NET_MAP                *Map,
369   IN NET_MAP_ITEM           *Item,
370   IN VOID                   *Context
371   );
372 
373 /**
374   The callback function for the net buffer which wraps the user's
375   transmit token. Although it seems this function is pretty simple,
376   there are some subtle things.
377   When user requests the IP to transmit a packet by passing it a
378   token, the token is wrapped in an IP4_TXTOKEN_WRAP and the data
379   is wrapped in an net buffer. the net buffer's Free function is
380   set to Ip4FreeTxToken. The Token and token wrap are added to the
381   IP child's TxToken map. Then the buffer is passed to Ip4Output for
382   transmission. If something error happened before that, the buffer
383   is freed, which in turn will free the token wrap. The wrap may
384   have been added to the TxToken map or not, and the user's event
385   shouldn't be fired because we are still in the EfiIp4Transmit. If
386   the buffer has been sent by Ip4Output, it should be removed from
387   the TxToken map and user's event signaled. The token wrap and buffer
388   are bound together. Check the comments in Ip4Output for information
389   about IP fragmentation.
390 
391   @param[in]  Context                The token's wrap.
392 
393 **/
394 VOID
395 EFIAPI
396 Ip4FreeTxToken (
397   IN VOID                   *Context
398   );
399 
400 extern EFI_IPSEC2_PROTOCOL   *mIpSec;
401 extern BOOLEAN               mIpSec2Installed;
402 
403 #endif
404