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