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