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