]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Network/Ip4Dxe/Ip4Impl.c
MdeModulePkg: Fix issue about current Ip4Dxe implementation for DHCP DORA process
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Ip4Dxe / Ip4Impl.c
CommitLineData
772db4bb 1/** @file\r
2\r
1f6729ff 3Copyright (c) 2005 - 2015, Intel Corporation. All rights reserved.<BR>\r
e5eed7d3 4This program and the accompanying materials\r
772db4bb 5are licensed and made available under the terms and conditions of the BSD License\r
6which accompanies this distribution. The full text of the license may be found at\r
7http://opensource.org/licenses/bsd-license.php\r
8\r
9THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
10WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
11\r
772db4bb 12**/\r
13\r
14#include "Ip4Impl.h"\r
15\r
705f53a9 16EFI_IPSEC2_PROTOCOL *mIpSec = NULL;\r
a1503a32 17\r
5405e9a6 18/**\r
19 Gets the current operational settings for this instance of the EFI IPv4 Protocol driver.\r
e2851998 20\r
5405e9a6 21 The GetModeData() function returns the current operational mode data for this\r
22 driver instance. The data fields in EFI_IP4_MODE_DATA are read only. This\r
23 function is used optionally to retrieve the operational mode data of underlying\r
24 networks or drivers.\r
25\r
3e8c18da 26 @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.\r
27 @param[out] Ip4ModeData Pointer to the EFI IPv4 Protocol mode data structure.\r
28 @param[out] MnpConfigData Pointer to the managed network configuration data structure.\r
29 @param[out] SnpModeData Pointer to the simple network mode data structure.\r
5405e9a6 30\r
31 @retval EFI_SUCCESS The operation completed successfully.\r
32 @retval EFI_INVALID_PARAMETER This is NULL.\r
33 @retval EFI_OUT_OF_RESOURCES The required mode data could not be allocated.\r
772db4bb 34\r
5405e9a6 35**/\r
36EFI_STATUS\r
37EFIAPI\r
38EfiIp4GetModeData (\r
39 IN CONST EFI_IP4_PROTOCOL *This,\r
24af132f 40 OUT EFI_IP4_MODE_DATA *Ip4ModeData OPTIONAL,\r
41 OUT EFI_MANAGED_NETWORK_CONFIG_DATA *MnpConfigData OPTIONAL,\r
5405e9a6 42 OUT EFI_SIMPLE_NETWORK_MODE *SnpModeData OPTIONAL\r
43 );\r
e2851998 44\r
772db4bb 45/**\r
5405e9a6 46 Assigns an IPv4 address and subnet mask to this EFI IPv4 Protocol driver instance.\r
e2851998 47\r
5405e9a6 48 The Configure() function is used to set, change, or reset the operational\r
49 parameters and filter settings for this EFI IPv4 Protocol instance. Until these\r
50 parameters have been set, no network traffic can be sent or received by this\r
51 instance. Once the parameters have been reset (by calling this function with\r
52 IpConfigData set to NULL), no more traffic can be sent or received until these\r
53 parameters have been set again. Each EFI IPv4 Protocol instance can be started\r
54 and stopped independently of each other by enabling or disabling their receive\r
55 filter settings with the Configure() function.\r
e2851998 56\r
5405e9a6 57 When IpConfigData.UseDefaultAddress is set to FALSE, the new station address will\r
58 be appended as an alias address into the addresses list in the EFI IPv4 Protocol\r
59 driver. While set to TRUE, Configure() will trigger the EFI_IP4_CONFIG_PROTOCOL\r
60 to retrieve the default IPv4 address if it is not available yet. Clients could\r
61 frequently call GetModeData() to check the status to ensure that the default IPv4\r
62 address is ready.\r
e2851998 63\r
5405e9a6 64 If operational parameters are reset or changed, any pending transmit and receive\r
65 requests will be cancelled. Their completion token status will be set to EFI_ABORTED\r
66 and their events will be signaled.\r
67\r
3e8c18da 68 @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.\r
69 @param[in] IpConfigData Pointer to the EFI IPv4 Protocol configuration data structure.\r
5405e9a6 70\r
71 @retval EFI_SUCCESS The driver instance was successfully opened.\r
72 @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP,\r
73 RARP, etc.) is not finished yet.\r
74 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:\r
75 @retval EFI_UNSUPPORTED One or more of the following conditions is TRUE:\r
76 A configuration protocol (DHCP, BOOTP, RARP, etc.) could\r
77 not be located when clients choose to use the default IPv4\r
78 address. This EFI IPv4 Protocol implementation does not\r
79 support this requested filter or timeout setting.\r
80 @retval EFI_OUT_OF_RESOURCES The EFI IPv4 Protocol driver instance data could not be allocated.\r
81 @retval EFI_ALREADY_STARTED The interface is already open and must be stopped before the\r
82 IPv4 address or subnet mask can be changed. The interface must\r
83 also be stopped when switching to/from raw packet mode.\r
84 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. The EFI IPv4\r
85 Protocol driver instance is not opened.\r
772db4bb 86\r
5405e9a6 87**/\r
88EFI_STATUS\r
89EFIAPI\r
90EfiIp4Configure (\r
91 IN EFI_IP4_PROTOCOL *This,\r
92 IN EFI_IP4_CONFIG_DATA *IpConfigData OPTIONAL\r
93 );\r
e2851998 94\r
5405e9a6 95/**\r
96 Joins and leaves multicast groups.\r
e2851998 97\r
5405e9a6 98 The Groups() function is used to join and leave multicast group sessions. Joining\r
99 a group will enable reception of matching multicast packets. Leaving a group will\r
100 disable the multicast packet reception.\r
e2851998 101\r
5405e9a6 102 If JoinFlag is FALSE and GroupAddress is NULL, all joined groups will be left.\r
103\r
3e8c18da 104 @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.\r
105 @param[in] JoinFlag Set to TRUE to join the multicast group session and FALSE to leave.\r
106 @param[in] GroupAddress Pointer to the IPv4 multicast address.\r
5405e9a6 107\r
108 @retval EFI_SUCCESS The operation completed successfully.\r
109 @retval EFI_INVALID_PARAMETER One or more of the following is TRUE:\r
110 - This is NULL.\r
111 - JoinFlag is TRUE and GroupAddress is NULL.\r
112 - GroupAddress is not NULL and *GroupAddress is\r
113 not a multicast IPv4 address.\r
114 @retval EFI_NOT_STARTED This instance has not been started.\r
115 @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP,\r
116 RARP, etc.) is not finished yet.\r
117 @retval EFI_OUT_OF_RESOURCES System resources could not be allocated.\r
118 @retval EFI_UNSUPPORTED This EFI IPv4 Protocol implementation does not support multicast groups.\r
119 @retval EFI_ALREADY_STARTED The group address is already in the group table (when\r
120 JoinFlag is TRUE).\r
121 @retval EFI_NOT_FOUND The group address is not in the group table (when JoinFlag is FALSE).\r
122 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.\r
772db4bb 123\r
5405e9a6 124**/\r
125EFI_STATUS\r
126EFIAPI\r
127EfiIp4Groups (\r
128 IN EFI_IP4_PROTOCOL *This,\r
129 IN BOOLEAN JoinFlag,\r
130 IN EFI_IPv4_ADDRESS *GroupAddress OPTIONAL\r
131 );\r
e2851998 132\r
5405e9a6 133/**\r
134 Adds and deletes routing table entries.\r
135\r
136 The Routes() function adds a route to or deletes a route from the routing table.\r
e2851998 137\r
5405e9a6 138 Routes are determined by comparing the SubnetAddress with the destination IPv4\r
139 address arithmetically AND-ed with the SubnetMask. The gateway address must be\r
140 on the same subnet as the configured station address.\r
e2851998 141\r
5405e9a6 142 The default route is added with SubnetAddress and SubnetMask both set to 0.0.0.0.\r
143 The default route matches all destination IPv4 addresses that do not match any\r
144 other routes.\r
e2851998 145\r
5405e9a6 146 A GatewayAddress that is zero is a nonroute. Packets are sent to the destination\r
147 IP address if it can be found in the ARP cache or on the local subnet. One automatic\r
148 nonroute entry will be inserted into the routing table for outgoing packets that\r
149 are addressed to a local subnet (gateway address of 0.0.0.0).\r
e2851998 150\r
5405e9a6 151 Each EFI IPv4 Protocol instance has its own independent routing table. Those EFI\r
152 IPv4 Protocol instances that use the default IPv4 address will also have copies\r
153 of the routing table that was provided by the EFI_IP4_CONFIG_PROTOCOL, and these\r
154 copies will be updated whenever the EIF IPv4 Protocol driver reconfigures its\r
155 instances. As a result, client modification to the routing table will be lost.\r
156\r
3e8c18da 157 @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.\r
158 @param[in] DeleteRoute Set to TRUE to delete this route from the routing table. Set to\r
159 FALSE to add this route to the routing table. SubnetAddress\r
160 and SubnetMask are used as the key to each route entry.\r
161 @param[in] SubnetAddress The address of the subnet that needs to be routed.\r
162 @param[in] SubnetMask The subnet mask of SubnetAddress.\r
163 @param[in] GatewayAddress The unicast gateway IPv4 address for this route.\r
5405e9a6 164\r
165 @retval EFI_SUCCESS The operation completed successfully.\r
166 @retval EFI_NOT_STARTED The driver instance has not been started.\r
167 @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP,\r
168 RARP, etc.) is not finished yet.\r
169 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:\r
170 - This is NULL.\r
171 - SubnetAddress is NULL.\r
172 - SubnetMask is NULL.\r
173 - GatewayAddress is NULL.\r
174 - *SubnetAddress is not a valid subnet address.\r
175 - *SubnetMask is not a valid subnet mask.\r
176 - *GatewayAddress is not a valid unicast IPv4 address.\r
177 @retval EFI_OUT_OF_RESOURCES Could not add the entry to the routing table.\r
178 @retval EFI_NOT_FOUND This route is not in the routing table (when DeleteRoute is TRUE).\r
179 @retval EFI_ACCESS_DENIED The route is already defined in the routing table (when\r
180 DeleteRoute is FALSE).\r
e2851998 181\r
5405e9a6 182**/\r
183EFI_STATUS\r
184EFIAPI\r
185EfiIp4Routes (\r
186 IN EFI_IP4_PROTOCOL *This,\r
187 IN BOOLEAN DeleteRoute,\r
188 IN EFI_IPv4_ADDRESS *SubnetAddress,\r
189 IN EFI_IPv4_ADDRESS *SubnetMask,\r
190 IN EFI_IPv4_ADDRESS *GatewayAddress\r
191 );\r
e2851998 192\r
5405e9a6 193/**\r
194 Places outgoing data packets into the transmit queue.\r
195\r
196 The Transmit() function places a sending request in the transmit queue of this\r
197 EFI IPv4 Protocol instance. Whenever the packet in the token is sent out or some\r
198 errors occur, the event in the token will be signaled and the status is updated.\r
199\r
3e8c18da 200 @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.\r
201 @param[in] Token Pointer to the transmit token.\r
5405e9a6 202\r
203 @retval EFI_SUCCESS The data has been queued for transmission.\r
204 @retval EFI_NOT_STARTED This instance has not been started.\r
205 @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP,\r
206 RARP, etc.) is not finished yet.\r
207 @retval EFI_INVALID_PARAMETER One or more pameters are invalid.\r
208 @retval EFI_ACCESS_DENIED The transmit completion token with the same Token.Event\r
209 was already in the transmit queue.\r
210 @retval EFI_NOT_READY The completion token could not be queued because the transmit\r
e2851998 211 queue is full.\r
5405e9a6 212 @retval EFI_NOT_FOUND Not route is found to destination address.\r
213 @retval EFI_OUT_OF_RESOURCES Could not queue the transmit data.\r
214 @retval EFI_BUFFER_TOO_SMALL Token.Packet.TxData.TotalDataLength is too\r
215 short to transmit.\r
216 @retval EFI_BAD_BUFFER_SIZE The length of the IPv4 header + option length + total data length is\r
217 greater than MTU (or greater than the maximum packet size if\r
218 Token.Packet.TxData.OverrideData.\r
219 DoNotFragment is TRUE.)\r
220\r
221**/\r
222EFI_STATUS\r
223EFIAPI\r
224EfiIp4Transmit (\r
225 IN EFI_IP4_PROTOCOL *This,\r
226 IN EFI_IP4_COMPLETION_TOKEN *Token\r
227 );\r
e2851998 228\r
5405e9a6 229/**\r
230 Places a receiving request into the receiving queue.\r
e2851998 231\r
5405e9a6 232 The Receive() function places a completion token into the receive packet queue.\r
233 This function is always asynchronous.\r
e2851998 234\r
5405e9a6 235 The Token.Event field in the completion token must be filled in by the caller\r
236 and cannot be NULL. When the receive operation completes, the EFI IPv4 Protocol\r
237 driver updates the Token.Status and Token.Packet.RxData fields and the Token.Event\r
238 is signaled.\r
239\r
3e8c18da 240 @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.\r
241 @param[in] Token Pointer to a token that is associated with the receive data descriptor.\r
5405e9a6 242\r
243 @retval EFI_SUCCESS The receive completion token was cached.\r
244 @retval EFI_NOT_STARTED This EFI IPv4 Protocol instance has not been started.\r
245 @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP, RARP, etc.)\r
246 is not finished yet.\r
247 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:\r
248 - This is NULL.\r
249 - Token is NULL.\r
250 - Token.Event is NULL.\r
251 @retval EFI_OUT_OF_RESOURCES The receive completion token could not be queued due to a lack of system\r
252 resources (usually memory).\r
253 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.\r
254 The EFI IPv4 Protocol instance has been reset to startup defaults.\r
255 EFI_ACCESS_DENIED The receive completion token with the same Token.Event was already\r
256 in the receive queue.\r
257 @retval EFI_NOT_READY The receive request could not be queued because the receive queue is full.\r
258 @retval EFI_ICMP_ERROR An ICMP error packet was received.\r
259\r
260**/\r
261EFI_STATUS\r
262EFIAPI\r
263EfiIp4Receive (\r
264 IN EFI_IP4_PROTOCOL *This,\r
265 IN EFI_IP4_COMPLETION_TOKEN *Token\r
266 );\r
e2851998 267\r
5405e9a6 268/**\r
269 Abort an asynchronous transmit or receive request.\r
e2851998 270\r
5405e9a6 271 The Cancel() function is used to abort a pending transmit or receive request.\r
272 If the token is in the transmit or receive request queues, after calling this\r
273 function, Token->Status will be set to EFI_ABORTED and then Token->Event will\r
274 be signaled. If the token is not in one of the queues, which usually means the\r
275 asynchronous operation has completed, this function will not signal the token\r
276 and EFI_NOT_FOUND is returned.\r
277\r
3e8c18da 278 @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.\r
279 @param[in] Token Pointer to a token that has been issued by\r
280 EFI_IP4_PROTOCOL.Transmit() or\r
281 EFI_IP4_PROTOCOL.Receive(). If NULL, all pending\r
282 tokens are aborted. Type EFI_IP4_COMPLETION_TOKEN is\r
283 defined in EFI_IP4_PROTOCOL.Transmit().\r
5405e9a6 284\r
285 @retval EFI_SUCCESS The asynchronous I/O request was aborted and\r
286 Token.->Event was signaled. When Token is NULL, all\r
287 pending requests were aborted and their events were signaled.\r
288 @retval EFI_INVALID_PARAMETER This is NULL.\r
289 @retval EFI_NOT_STARTED This instance has not been started.\r
290 @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP,\r
291 RARP, etc.) is not finished yet.\r
292 @retval EFI_NOT_FOUND When Token is not NULL, the asynchronous I/O request was\r
293 not found in the transmit or receive queue. It has either completed\r
294 or was not issued by Transmit() and Receive().\r
295\r
296**/\r
297EFI_STATUS\r
298EFIAPI\r
299EfiIp4Cancel (\r
300 IN EFI_IP4_PROTOCOL *This,\r
301 IN EFI_IP4_COMPLETION_TOKEN *Token OPTIONAL\r
302 );\r
e2851998 303\r
5405e9a6 304/**\r
305 Polls for incoming data packets and processes outgoing data packets.\r
e2851998 306\r
5405e9a6 307 The Poll() function polls for incoming data packets and processes outgoing data\r
308 packets. Network drivers and applications can call the EFI_IP4_PROTOCOL.Poll()\r
309 function to increase the rate that data packets are moved between the communications\r
310 device and the transmit and receive queues.\r
e2851998 311\r
5405e9a6 312 In some systems the periodic timer event may not poll the underlying communications\r
313 device fast enough to transmit and/or receive all data packets without missing\r
314 incoming packets or dropping outgoing packets. Drivers and applications that are\r
315 experiencing packet loss should try calling the EFI_IP4_PROTOCOL.Poll() function\r
316 more often.\r
317\r
3e8c18da 318 @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.\r
5405e9a6 319\r
320 @retval EFI_SUCCESS Incoming or outgoing data was processed.\r
321 @retval EFI_NOT_STARTED This EFI IPv4 Protocol instance has not been started.\r
322 @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP,\r
323 RARP, etc.) is not finished yet.\r
324 @retval EFI_INVALID_PARAMETER This is NULL.\r
325 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.\r
326 @retval EFI_NOT_READY No incoming or outgoing data is processed.\r
327 @retval EFI_TIMEOUT Data was dropped out of the transmit and/or receive queue.\r
328 Consider increasing the polling rate.\r
329\r
330**/\r
331EFI_STATUS\r
332EFIAPI\r
333EfiIp4Poll (\r
334 IN EFI_IP4_PROTOCOL *This\r
335 );\r
336\r
337EFI_IP4_PROTOCOL\r
338mEfiIp4ProtocolTemplete = {\r
339 EfiIp4GetModeData,\r
340 EfiIp4Configure,\r
341 EfiIp4Groups,\r
342 EfiIp4Routes,\r
343 EfiIp4Transmit,\r
344 EfiIp4Receive,\r
345 EfiIp4Cancel,\r
346 EfiIp4Poll\r
347};\r
348\r
349/**\r
350 Gets the current operational settings for this instance of the EFI IPv4 Protocol driver.\r
e2851998 351\r
5405e9a6 352 The GetModeData() function returns the current operational mode data for this\r
353 driver instance. The data fields in EFI_IP4_MODE_DATA are read only. This\r
354 function is used optionally to retrieve the operational mode data of underlying\r
355 networks or drivers.\r
356\r
3e8c18da 357 @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.\r
358 @param[out] Ip4ModeData Pointer to the EFI IPv4 Protocol mode data structure.\r
359 @param[out] MnpConfigData Pointer to the managed network configuration data structure.\r
360 @param[out] SnpModeData Pointer to the simple network mode data structure.\r
5405e9a6 361\r
362 @retval EFI_SUCCESS The operation completed successfully.\r
363 @retval EFI_INVALID_PARAMETER This is NULL.\r
364 @retval EFI_OUT_OF_RESOURCES The required mode data could not be allocated.\r
772db4bb 365\r
366**/\r
772db4bb 367EFI_STATUS\r
368EFIAPI\r
369EfiIp4GetModeData (\r
370 IN CONST EFI_IP4_PROTOCOL *This,\r
24af132f 371 OUT EFI_IP4_MODE_DATA *Ip4ModeData OPTIONAL,\r
372 OUT EFI_MANAGED_NETWORK_CONFIG_DATA *MnpConfigData OPTIONAL,\r
772db4bb 373 OUT EFI_SIMPLE_NETWORK_MODE *SnpModeData OPTIONAL\r
374 )\r
375{\r
376 IP4_PROTOCOL *IpInstance;\r
377 IP4_SERVICE *IpSb;\r
378 EFI_IP4_CONFIG_DATA *Config;\r
379 EFI_STATUS Status;\r
380 EFI_TPL OldTpl;\r
381 IP4_ADDR Ip;\r
382\r
383 if (This == NULL) {\r
384 return EFI_INVALID_PARAMETER;\r
385 }\r
386\r
e48e37fc 387 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
772db4bb 388 IpInstance = IP4_INSTANCE_FROM_PROTOCOL (This);\r
389 IpSb = IpInstance->Service;\r
390\r
391 if (Ip4ModeData != NULL) {\r
392 //\r
393 // IsStarted is "whether the EfiIp4Configure has been called".\r
394 // IsConfigured is "whether the station address has been configured"\r
395 //\r
396 Ip4ModeData->IsStarted = (BOOLEAN)(IpInstance->State == IP4_STATE_CONFIGED);\r
687a2e5f 397 CopyMem (&Ip4ModeData->ConfigData, &IpInstance->ConfigData, sizeof (Ip4ModeData->ConfigData));\r
772db4bb 398 Ip4ModeData->IsConfigured = FALSE;\r
399\r
400 Ip4ModeData->GroupCount = IpInstance->GroupCount;\r
401 Ip4ModeData->GroupTable = (EFI_IPv4_ADDRESS *) IpInstance->Groups;\r
402\r
403 Ip4ModeData->IcmpTypeCount = 23;\r
404 Ip4ModeData->IcmpTypeList = mIp4SupportedIcmp;\r
405\r
406 Ip4ModeData->RouteTable = NULL;\r
407 Ip4ModeData->RouteCount = 0;\r
408\r
a1503a32 409 Ip4ModeData->MaxPacketSize = IpSb->MaxPacketSize;\r
410\r
772db4bb 411 //\r
412 // return the current station address for this IP child. So,\r
413 // the user can get the default address through this. Some\r
414 // application wants to know it station address even it is\r
415 // using the default one, such as a ftp server.\r
416 //\r
417 if (Ip4ModeData->IsStarted) {\r
418 Config = &Ip4ModeData->ConfigData;\r
419\r
420 Ip = HTONL (IpInstance->Interface->Ip);\r
e48e37fc 421 CopyMem (&Config->StationAddress, &Ip, sizeof (EFI_IPv4_ADDRESS));\r
772db4bb 422\r
423 Ip = HTONL (IpInstance->Interface->SubnetMask);\r
e48e37fc 424 CopyMem (&Config->SubnetMask, &Ip, sizeof (EFI_IPv4_ADDRESS));\r
772db4bb 425\r
426 Ip4ModeData->IsConfigured = IpInstance->Interface->Configured;\r
427\r
428 //\r
429 // Build a EFI route table for user from the internal route table.\r
430 //\r
431 Status = Ip4BuildEfiRouteTable (IpInstance);\r
432\r
433 if (EFI_ERROR (Status)) {\r
e48e37fc 434 gBS->RestoreTPL (OldTpl);\r
772db4bb 435 return Status;\r
436 }\r
437\r
438 Ip4ModeData->RouteTable = IpInstance->EfiRouteTable;\r
439 Ip4ModeData->RouteCount = IpInstance->EfiRouteCount;\r
440 }\r
441 }\r
442\r
dd29f3ed 443 //\r
444 // Get fresh mode data from MNP, since underlying media status may change\r
445 //\r
446 Status = IpSb->Mnp->GetModeData (IpSb->Mnp, MnpConfigData, SnpModeData);\r
772db4bb 447\r
e48e37fc 448 gBS->RestoreTPL (OldTpl);\r
dd29f3ed 449 return Status;\r
772db4bb 450}\r
451\r
452\r
453/**\r
454 Config the MNP parameter used by IP. The IP driver use one MNP\r
455 child to transmit/receive frames. By default, it configures MNP\r
456 to receive unicast/multicast/broadcast. And it will enable/disable\r
457 the promiscous receive according to whether there is IP child\r
5405e9a6 458 enable that or not. If Force is FALSE, it will iterate through\r
772db4bb 459 all the IP children to check whether the promiscuous receive\r
460 setting has been changed. If it hasn't been changed, it won't\r
5405e9a6 461 reconfigure the MNP. If Force is TRUE, the MNP is configured no\r
772db4bb 462 matter whether that is changed or not.\r
463\r
3e8c18da 464 @param[in] IpSb The IP4 service instance that is to be changed.\r
465 @param[in] Force Force the configuration or not.\r
772db4bb 466\r
467 @retval EFI_SUCCESS The MNP is successfully configured/reconfigured.\r
468 @retval Others Configuration failed.\r
469\r
470**/\r
471EFI_STATUS\r
472Ip4ServiceConfigMnp (\r
473 IN IP4_SERVICE *IpSb,\r
474 IN BOOLEAN Force\r
475 )\r
476{\r
e48e37fc 477 LIST_ENTRY *Entry;\r
478 LIST_ENTRY *ProtoEntry;\r
772db4bb 479 IP4_INTERFACE *IpIf;\r
480 IP4_PROTOCOL *IpInstance;\r
481 BOOLEAN Reconfig;\r
482 BOOLEAN PromiscReceive;\r
483 EFI_STATUS Status;\r
484\r
485 Reconfig = FALSE;\r
486 PromiscReceive = FALSE;\r
487\r
488 if (!Force) {\r
489 //\r
490 // Iterate through the IP children to check whether promiscuous\r
491 // receive setting has been changed. Update the interface's receive\r
492 // filter also.\r
493 //\r
494 NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) {\r
495\r
496 IpIf = NET_LIST_USER_STRUCT (Entry, IP4_INTERFACE, Link);\r
497 IpIf->PromiscRecv = FALSE;\r
498\r
499 NET_LIST_FOR_EACH (ProtoEntry, &IpIf->IpInstances) {\r
500 IpInstance = NET_LIST_USER_STRUCT (ProtoEntry, IP4_PROTOCOL, AddrLink);\r
501\r
502 if (IpInstance->ConfigData.AcceptPromiscuous) {\r
503 IpIf->PromiscRecv = TRUE;\r
504 PromiscReceive = TRUE;\r
505 }\r
506 }\r
507 }\r
508\r
509 //\r
510 // If promiscuous receive isn't changed, it isn't necessary to reconfigure.\r
511 //\r
512 if (PromiscReceive == IpSb->MnpConfigData.EnablePromiscuousReceive) {\r
513 return EFI_SUCCESS;\r
514 }\r
515\r
516 Reconfig = TRUE;\r
517 IpSb->MnpConfigData.EnablePromiscuousReceive = PromiscReceive;\r
518 }\r
519\r
520 Status = IpSb->Mnp->Configure (IpSb->Mnp, &IpSb->MnpConfigData);\r
521\r
522 //\r
523 // recover the original configuration if failed to set the configure.\r
524 //\r
525 if (EFI_ERROR (Status) && Reconfig) {\r
687a2e5f 526 IpSb->MnpConfigData.EnablePromiscuousReceive = (BOOLEAN) !PromiscReceive;\r
772db4bb 527 }\r
528\r
529 return Status;\r
530}\r
531\r
532\r
772db4bb 533/**\r
534 Intiialize the IP4_PROTOCOL structure to the unconfigured states.\r
535\r
536 @param IpSb The IP4 service instance.\r
537 @param IpInstance The IP4 child instance.\r
538\r
772db4bb 539**/\r
540VOID\r
541Ip4InitProtocol (\r
5405e9a6 542 IN IP4_SERVICE *IpSb,\r
543 IN OUT IP4_PROTOCOL *IpInstance\r
772db4bb 544 )\r
545{\r
546 ASSERT ((IpSb != NULL) && (IpInstance != NULL));\r
547\r
e48e37fc 548 ZeroMem (IpInstance, sizeof (IP4_PROTOCOL));\r
772db4bb 549\r
550 IpInstance->Signature = IP4_PROTOCOL_SIGNATURE;\r
687a2e5f 551 CopyMem (&IpInstance->Ip4Proto, &mEfiIp4ProtocolTemplete, sizeof (IpInstance->Ip4Proto));\r
772db4bb 552 IpInstance->State = IP4_STATE_UNCONFIGED;\r
553 IpInstance->Service = IpSb;\r
554\r
e48e37fc 555 InitializeListHead (&IpInstance->Link);\r
772db4bb 556 NetMapInit (&IpInstance->RxTokens);\r
557 NetMapInit (&IpInstance->TxTokens);\r
e48e37fc 558 InitializeListHead (&IpInstance->Received);\r
559 InitializeListHead (&IpInstance->Delivered);\r
560 InitializeListHead (&IpInstance->AddrLink);\r
772db4bb 561\r
e48e37fc 562 EfiInitializeLock (&IpInstance->RecycleLock, TPL_NOTIFY);\r
772db4bb 563}\r
564\r
565\r
1f6729ff 566\r
772db4bb 567/**\r
568 Configure the IP4 child. If the child is already configured,\r
569 change the configuration parameter. Otherwise configure it\r
570 for the first time. The caller should validate the configuration\r
571 before deliver them to it. It also don't do configure NULL.\r
572\r
3e8c18da 573 @param[in, out] IpInstance The IP4 child to configure.\r
574 @param[in] Config The configure data.\r
772db4bb 575\r
576 @retval EFI_SUCCESS The IP4 child is successfully configured.\r
577 @retval EFI_DEVICE_ERROR Failed to free the pending transive or to\r
578 configure underlying MNP or other errors.\r
579 @retval EFI_NO_MAPPING The IP4 child is configured to use default\r
580 address, but the default address hasn't been\r
581 configured. The IP4 child doesn't need to be\r
582 reconfigured when default address is configured.\r
5405e9a6 583 @retval EFI_OUT_OF_RESOURCES No more memory space is available.\r
584 @retval other Other error occurs.\r
772db4bb 585\r
586**/\r
587EFI_STATUS\r
588Ip4ConfigProtocol (\r
5405e9a6 589 IN OUT IP4_PROTOCOL *IpInstance,\r
590 IN EFI_IP4_CONFIG_DATA *Config\r
772db4bb 591 )\r
592{\r
593 IP4_SERVICE *IpSb;\r
594 IP4_INTERFACE *IpIf;\r
595 EFI_STATUS Status;\r
596 IP4_ADDR Ip;\r
597 IP4_ADDR Netmask;\r
216f7970 598 EFI_ARP_PROTOCOL *Arp;\r
772db4bb 599\r
600 IpSb = IpInstance->Service;\r
601\r
602 //\r
603 // User is changing packet filters. It must be stopped\r
604 // before the station address can be changed.\r
605 //\r
606 if (IpInstance->State == IP4_STATE_CONFIGED) {\r
607 //\r
608 // Cancel all the pending transmit/receive from upper layer\r
609 //\r
610 Status = Ip4Cancel (IpInstance, NULL);\r
611\r
612 if (EFI_ERROR (Status)) {\r
613 return EFI_DEVICE_ERROR;\r
614 }\r
615\r
687a2e5f 616 CopyMem (&IpInstance->ConfigData, Config, sizeof (IpInstance->ConfigData));\r
772db4bb 617 return EFI_SUCCESS;\r
618 }\r
619\r
620 //\r
621 // Configure a fresh IP4 protocol instance. Create a route table.\r
622 // Each IP child has its own route table, which may point to the\r
623 // default table if it is using default address.\r
624 //\r
625 Status = EFI_OUT_OF_RESOURCES;\r
626 IpInstance->RouteTable = Ip4CreateRouteTable ();\r
627\r
628 if (IpInstance->RouteTable == NULL) {\r
629 return Status;\r
630 }\r
631\r
632 //\r
633 // Set up the interface.\r
634 //\r
e48e37fc 635 CopyMem (&Ip, &Config->StationAddress, sizeof (IP4_ADDR));\r
636 CopyMem (&Netmask, &Config->SubnetMask, sizeof (IP4_ADDR));\r
772db4bb 637\r
638 Ip = NTOHL (Ip);\r
639 Netmask = NTOHL (Netmask);\r
640\r
641 if (!Config->UseDefaultAddress) {\r
642 //\r
643 // Find whether there is already an interface with the same\r
644 // station address. All the instances with the same station\r
645 // address shares one interface.\r
646 //\r
647 IpIf = Ip4FindStationAddress (IpSb, Ip, Netmask);\r
648\r
649 if (IpIf != NULL) {\r
650 NET_GET_REF (IpIf);\r
651\r
652 } else {\r
653 IpIf = Ip4CreateInterface (IpSb->Mnp, IpSb->Controller, IpSb->Image);\r
654\r
655 if (IpIf == NULL) {\r
656 goto ON_ERROR;\r
657 }\r
658\r
659 Status = Ip4SetAddress (IpIf, Ip, Netmask);\r
660\r
661 if (EFI_ERROR (Status)) {\r
662 Status = EFI_DEVICE_ERROR;\r
663 Ip4FreeInterface (IpIf, IpInstance);\r
664 goto ON_ERROR;\r
665 }\r
666\r
e48e37fc 667 InsertTailList (&IpSb->Interfaces, &IpIf->Link);\r
772db4bb 668 }\r
669\r
670 //\r
671 // Add a route to this connected network in the route table\r
672 //\r
673 Ip4AddRoute (IpInstance->RouteTable, Ip, Netmask, IP4_ALLZERO_ADDRESS);\r
674\r
675 } else {\r
676 //\r
677 // Use the default address. If the default configuration hasn't\r
678 // been started, start it.\r
679 //\r
680 if (IpSb->State == IP4_SERVICE_UNSTARTED) {\r
3d0a49ad
JW
681 Status = Ip4StartAutoConfig (&IpSb->Ip4Config2Instance);\r
682\r
683 if (EFI_ERROR (Status)) {\r
684 goto ON_ERROR;\r
685 }\r
772db4bb 686 }\r
687\r
688 IpIf = IpSb->DefaultInterface;\r
689 NET_GET_REF (IpSb->DefaultInterface);\r
690\r
691 //\r
692 // If default address is used, so is the default route table.\r
693 // Any route set by the instance has the precedence over the\r
694 // routes in the default route table. Link the default table\r
695 // after the instance's table. Routing will search the local\r
696 // table first.\r
697 //\r
698 NET_GET_REF (IpSb->DefaultRouteTable);\r
699 IpInstance->RouteTable->Next = IpSb->DefaultRouteTable;\r
700 }\r
701\r
702 IpInstance->Interface = IpIf;\r
216f7970 703 if (IpIf->Arp != NULL) {\r
704 Arp = NULL;\r
705 Status = gBS->OpenProtocol (\r
706 IpIf->ArpHandle,\r
707 &gEfiArpProtocolGuid,\r
708 (VOID **) &Arp,\r
709 gIp4DriverBinding.DriverBindingHandle,\r
710 IpInstance->Handle,\r
711 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER\r
712 );\r
713 if (EFI_ERROR (Status)) {\r
714 goto ON_ERROR;\r
715 }\r
716 }\r
e48e37fc 717 InsertTailList (&IpIf->IpInstances, &IpInstance->AddrLink);\r
772db4bb 718\r
687a2e5f 719 CopyMem (&IpInstance->ConfigData, Config, sizeof (IpInstance->ConfigData));\r
772db4bb 720 IpInstance->State = IP4_STATE_CONFIGED;\r
721\r
722 //\r
723 // Although EFI_NO_MAPPING is an error code, the IP child has been\r
724 // successfully configured and doesn't need reconfiguration when\r
725 // default address is acquired.\r
726 //\r
727 if (Config->UseDefaultAddress && IP4_NO_MAPPING (IpInstance)) {\r
728 return EFI_NO_MAPPING;\r
729 }\r
730\r
731 return EFI_SUCCESS;\r
732\r
733ON_ERROR:\r
734 Ip4FreeRouteTable (IpInstance->RouteTable);\r
735 IpInstance->RouteTable = NULL;\r
736 return Status;\r
737}\r
738\r
739\r
740/**\r
741 Clean up the IP4 child, release all the resources used by it.\r
742\r
3e8c18da 743 @param[in] IpInstance The IP4 child to clean up.\r
772db4bb 744\r
1f6729ff 745 @retval EFI_SUCCESS The IP4 child is cleaned up.\r
746 @retval EFI_DEVICE_ERROR Some resources failed to be released.\r
772db4bb 747\r
748**/\r
749EFI_STATUS\r
750Ip4CleanProtocol (\r
751 IN IP4_PROTOCOL *IpInstance\r
752 )\r
753{\r
754 if (EFI_ERROR (Ip4Cancel (IpInstance, NULL))) {\r
755 return EFI_DEVICE_ERROR;\r
756 }\r
757\r
758 if (EFI_ERROR (Ip4Groups (IpInstance, FALSE, NULL))) {\r
759 return EFI_DEVICE_ERROR;\r
760 }\r
761\r
762 //\r
763 // Some packets haven't been recycled. It is because either the\r
764 // user forgets to recycle the packets, or because the callback\r
765 // hasn't been called. Just leave it alone.\r
766 //\r
e48e37fc 767 if (!IsListEmpty (&IpInstance->Delivered)) {\r
772db4bb 768 ;\r
769 }\r
770\r
771 if (IpInstance->Interface != NULL) {\r
e48e37fc 772 RemoveEntryList (&IpInstance->AddrLink);\r
216f7970 773 if (IpInstance->Interface->Arp != NULL) {\r
774 gBS->CloseProtocol (\r
775 IpInstance->Interface->ArpHandle,\r
776 &gEfiArpProtocolGuid,\r
777 gIp4DriverBinding.DriverBindingHandle,\r
778 IpInstance->Handle\r
779 );\r
780 }\r
772db4bb 781 Ip4FreeInterface (IpInstance->Interface, IpInstance);\r
782 IpInstance->Interface = NULL;\r
783 }\r
784\r
785 if (IpInstance->RouteTable != NULL) {\r
786 if (IpInstance->RouteTable->Next != NULL) {\r
787 Ip4FreeRouteTable (IpInstance->RouteTable->Next);\r
788 }\r
789\r
790 Ip4FreeRouteTable (IpInstance->RouteTable);\r
791 IpInstance->RouteTable = NULL;\r
792 }\r
793\r
794 if (IpInstance->EfiRouteTable != NULL) {\r
766c7483 795 FreePool (IpInstance->EfiRouteTable);\r
772db4bb 796 IpInstance->EfiRouteTable = NULL;\r
797 IpInstance->EfiRouteCount = 0;\r
798 }\r
799\r
800 if (IpInstance->Groups != NULL) {\r
766c7483 801 FreePool (IpInstance->Groups);\r
772db4bb 802 IpInstance->Groups = NULL;\r
803 IpInstance->GroupCount = 0;\r
804 }\r
805\r
806 NetMapClean (&IpInstance->TxTokens);\r
807\r
808 NetMapClean (&IpInstance->RxTokens);\r
809\r
810 return EFI_SUCCESS;\r
811}\r
812\r
813\r
814/**\r
815 Validate that Ip/Netmask pair is OK to be used as station\r
816 address. Only continuous netmasks are supported. and check\r
817 that StationAddress is a unicast address on the newtwork.\r
818\r
1f6729ff 819 @param[in] Ip The IP address to validate.\r
820 @param[in] Netmask The netmaks of the IP.\r
772db4bb 821\r
1f6729ff 822 @retval TRUE The Ip/Netmask pair is valid.\r
823 @retval FALSE The Ip/Netmask pair is invalid.\r
772db4bb 824\r
825**/\r
826BOOLEAN\r
827Ip4StationAddressValid (\r
828 IN IP4_ADDR Ip,\r
829 IN IP4_ADDR Netmask\r
830 )\r
831{\r
832 IP4_ADDR NetBrdcastMask;\r
833 INTN Len;\r
834 INTN Type;\r
835\r
836 //\r
837 // Only support the station address with 0.0.0.0/0 to enable DHCP client.\r
838 //\r
839 if (Netmask == IP4_ALLZERO_ADDRESS) {\r
840 return (BOOLEAN) (Ip == IP4_ALLZERO_ADDRESS);\r
841 }\r
842\r
843 //\r
844 // Only support the continuous net masks\r
845 //\r
846 if ((Len = NetGetMaskLength (Netmask)) == IP4_MASK_NUM) {\r
847 return FALSE;\r
848 }\r
849\r
850 //\r
851 // Station address can't be class D or class E address\r
852 //\r
853 if ((Type = NetGetIpClass (Ip)) > IP4_ADDR_CLASSC) {\r
854 return FALSE;\r
855 }\r
856\r
857 //\r
858 // Station address can't be subnet broadcast/net broadcast address\r
859 //\r
860 if ((Ip == (Ip & Netmask)) || (Ip == (Ip | ~Netmask))) {\r
861 return FALSE;\r
862 }\r
863\r
2a86ff1c 864 NetBrdcastMask = gIp4AllMasks[MIN (Len, Type << 3)];\r
772db4bb 865\r
866 if (Ip == (Ip | ~NetBrdcastMask)) {\r
867 return FALSE;\r
868 }\r
869\r
870 return TRUE;\r
871}\r
872\r
873\r
874/**\r
5405e9a6 875 Assigns an IPv4 address and subnet mask to this EFI IPv4 Protocol driver instance.\r
e2851998 876\r
5405e9a6 877 The Configure() function is used to set, change, or reset the operational\r
878 parameters and filter settings for this EFI IPv4 Protocol instance. Until these\r
879 parameters have been set, no network traffic can be sent or received by this\r
880 instance. Once the parameters have been reset (by calling this function with\r
881 IpConfigData set to NULL), no more traffic can be sent or received until these\r
882 parameters have been set again. Each EFI IPv4 Protocol instance can be started\r
883 and stopped independently of each other by enabling or disabling their receive\r
884 filter settings with the Configure() function.\r
e2851998 885\r
5405e9a6 886 When IpConfigData.UseDefaultAddress is set to FALSE, the new station address will\r
887 be appended as an alias address into the addresses list in the EFI IPv4 Protocol\r
888 driver. While set to TRUE, Configure() will trigger the EFI_IP4_CONFIG_PROTOCOL\r
889 to retrieve the default IPv4 address if it is not available yet. Clients could\r
890 frequently call GetModeData() to check the status to ensure that the default IPv4\r
891 address is ready.\r
e2851998 892\r
5405e9a6 893 If operational parameters are reset or changed, any pending transmit and receive\r
894 requests will be cancelled. Their completion token status will be set to EFI_ABORTED\r
895 and their events will be signaled.\r
896\r
3e8c18da 897 @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.\r
898 @param[in] IpConfigData Pointer to the EFI IPv4 Protocol configuration data structure.\r
5405e9a6 899\r
900 @retval EFI_SUCCESS The driver instance was successfully opened.\r
901 @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP,\r
902 RARP, etc.) is not finished yet.\r
903 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:\r
904 @retval EFI_UNSUPPORTED One or more of the following conditions is TRUE:\r
905 A configuration protocol (DHCP, BOOTP, RARP, etc.) could\r
906 not be located when clients choose to use the default IPv4\r
907 address. This EFI IPv4 Protocol implementation does not\r
908 support this requested filter or timeout setting.\r
909 @retval EFI_OUT_OF_RESOURCES The EFI IPv4 Protocol driver instance data could not be allocated.\r
910 @retval EFI_ALREADY_STARTED The interface is already open and must be stopped before the\r
911 IPv4 address or subnet mask can be changed. The interface must\r
912 also be stopped when switching to/from raw packet mode.\r
913 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. The EFI IPv4\r
914 Protocol driver instance is not opened.\r
772db4bb 915\r
916**/\r
772db4bb 917EFI_STATUS\r
918EFIAPI\r
919EfiIp4Configure (\r
920 IN EFI_IP4_PROTOCOL *This,\r
921 IN EFI_IP4_CONFIG_DATA *IpConfigData OPTIONAL\r
922 )\r
923{\r
924 IP4_PROTOCOL *IpInstance;\r
925 EFI_IP4_CONFIG_DATA *Current;\r
926 EFI_TPL OldTpl;\r
927 EFI_STATUS Status;\r
928 BOOLEAN AddrOk;\r
929 IP4_ADDR IpAddress;\r
930 IP4_ADDR SubnetMask;\r
931\r
932 //\r
933 // First, validate the parameters\r
934 //\r
935 if (This == NULL) {\r
936 return EFI_INVALID_PARAMETER;\r
937 }\r
938\r
939 IpInstance = IP4_INSTANCE_FROM_PROTOCOL (This);\r
e48e37fc 940 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
772db4bb 941\r
942 //\r
943 // Validate the configuration first.\r
944 //\r
945 if (IpConfigData != NULL) {\r
772db4bb 946\r
e48e37fc 947 CopyMem (&IpAddress, &IpConfigData->StationAddress, sizeof (IP4_ADDR));\r
948 CopyMem (&SubnetMask, &IpConfigData->SubnetMask, sizeof (IP4_ADDR));\r
772db4bb 949\r
950 IpAddress = NTOHL (IpAddress);\r
951 SubnetMask = NTOHL (SubnetMask);\r
952\r
953 //\r
954 // Check whether the station address is a valid unicast address\r
955 //\r
956 if (!IpConfigData->UseDefaultAddress) {\r
957 AddrOk = Ip4StationAddressValid (IpAddress, SubnetMask);\r
958\r
959 if (!AddrOk) {\r
960 Status = EFI_INVALID_PARAMETER;\r
961 goto ON_EXIT;\r
962 }\r
963 }\r
964\r
965 //\r
966 // User can only update packet filters when already configured.\r
967 // If it wants to change the station address, it must configure(NULL)\r
968 // the instance first.\r
969 //\r
970 if (IpInstance->State == IP4_STATE_CONFIGED) {\r
971 Current = &IpInstance->ConfigData;\r
972\r
973 if (Current->UseDefaultAddress != IpConfigData->UseDefaultAddress) {\r
974 Status = EFI_ALREADY_STARTED;\r
975 goto ON_EXIT;\r
976 }\r
977\r
978 if (!Current->UseDefaultAddress &&\r
84b5c78e 979 (!EFI_IP4_EQUAL (&Current->StationAddress, &IpConfigData->StationAddress) ||\r
980 !EFI_IP4_EQUAL (&Current->SubnetMask, &IpConfigData->SubnetMask))) {\r
772db4bb 981 Status = EFI_ALREADY_STARTED;\r
982 goto ON_EXIT;\r
983 }\r
984\r
985 if (Current->UseDefaultAddress && IP4_NO_MAPPING (IpInstance)) {\r
ae0a0b06 986 Status = EFI_NO_MAPPING;\r
987 goto ON_EXIT;\r
772db4bb 988 }\r
989 }\r
990 }\r
991\r
992 //\r
993 // Configure the instance or clean it up.\r
994 //\r
995 if (IpConfigData != NULL) {\r
996 Status = Ip4ConfigProtocol (IpInstance, IpConfigData);\r
997 } else {\r
998 Status = Ip4CleanProtocol (IpInstance);\r
999\r
1000 //\r
75dce340 1001 // Don't change the state if it is DESTROY, consider the following\r
772db4bb 1002 // valid sequence: Mnp is unloaded-->Ip Stopped-->Udp Stopped,\r
1003 // Configure (ThisIp, NULL). If the state is changed to UNCONFIGED,\r
1004 // the unload fails miserably.\r
1005 //\r
1006 if (IpInstance->State == IP4_STATE_CONFIGED) {\r
1007 IpInstance->State = IP4_STATE_UNCONFIGED;\r
1008 }\r
1009 }\r
1010\r
1011 //\r
1012 // Update the MNP's configure data. Ip4ServiceConfigMnp will check\r
1013 // whether it is necessary to reconfigure the MNP.\r
1014 //\r
1015 Ip4ServiceConfigMnp (IpInstance->Service, FALSE);\r
1016\r
772db4bb 1017ON_EXIT:\r
e48e37fc 1018 gBS->RestoreTPL (OldTpl);\r
772db4bb 1019 return Status;\r
1020\r
1021}\r
1022\r
1023\r
1024/**\r
1025 Change the IP4 child's multicast setting. The caller\r
1026 should make sure that the parameters is valid.\r
1027\r
3e8c18da 1028 @param[in] IpInstance The IP4 child to change the setting.\r
1f6729ff 1029 @param[in] JoinFlag TRUE to join the group, otherwise leave it.\r
1030 @param[in] GroupAddress The target group address.\r
772db4bb 1031\r
1f6729ff 1032 @retval EFI_ALREADY_STARTED Want to join the group, but already a member of it.\r
772db4bb 1033 @retval EFI_OUT_OF_RESOURCES Failed to allocate some resources.\r
1f6729ff 1034 @retval EFI_DEVICE_ERROR Failed to set the group configuraton.\r
772db4bb 1035 @retval EFI_SUCCESS Successfully updated the group setting.\r
1036 @retval EFI_NOT_FOUND Try to leave the group which it isn't a member.\r
1037\r
1038**/\r
1039EFI_STATUS\r
1040Ip4Groups (\r
1041 IN IP4_PROTOCOL *IpInstance,\r
1042 IN BOOLEAN JoinFlag,\r
1043 IN EFI_IPv4_ADDRESS *GroupAddress OPTIONAL\r
1044 )\r
1045{\r
1046 IP4_ADDR *Members;\r
1047 IP4_ADDR Group;\r
1048 UINT32 Index;\r
1049\r
1050 //\r
1051 // Add it to the instance's Groups, and join the group by IGMP.\r
1052 // IpInstance->Groups is in network byte order. IGMP operates in\r
1053 // host byte order\r
1054 //\r
1055 if (JoinFlag) {\r
e2851998 1056 //\r
894d038a 1057 // When JoinFlag is TRUE, GroupAddress shouldn't be NULL.\r
1058 //\r
1059 ASSERT (GroupAddress != NULL);\r
e48e37fc 1060 CopyMem (&Group, GroupAddress, sizeof (IP4_ADDR));\r
772db4bb 1061\r
1062 for (Index = 0; Index < IpInstance->GroupCount; Index++) {\r
1063 if (IpInstance->Groups[Index] == Group) {\r
1064 return EFI_ALREADY_STARTED;\r
1065 }\r
1066 }\r
1067\r
1068 Members = Ip4CombineGroups (IpInstance->Groups, IpInstance->GroupCount, Group);\r
1069\r
1070 if (Members == NULL) {\r
1071 return EFI_OUT_OF_RESOURCES;\r
1072 }\r
1073\r
1074 if (EFI_ERROR (Ip4JoinGroup (IpInstance, NTOHL (Group)))) {\r
766c7483 1075 FreePool (Members);\r
772db4bb 1076 return EFI_DEVICE_ERROR;\r
1077 }\r
1078\r
1079 if (IpInstance->Groups != NULL) {\r
766c7483 1080 FreePool (IpInstance->Groups);\r
772db4bb 1081 }\r
1082\r
1083 IpInstance->Groups = Members;\r
1084 IpInstance->GroupCount++;\r
1085\r
1086 return EFI_SUCCESS;\r
1087 }\r
1088\r
1089 //\r
1090 // Leave the group. Leave all the groups if GroupAddress is NULL.\r
1091 // Must iterate from the end to the beginning because the GroupCount\r
1092 // is decreamented each time an address is removed..\r
1093 //\r
1094 for (Index = IpInstance->GroupCount; Index > 0 ; Index--) {\r
1095 Group = IpInstance->Groups[Index - 1];\r
1096\r
84b5c78e 1097 if ((GroupAddress == NULL) || EFI_IP4_EQUAL (&Group, GroupAddress)) {\r
772db4bb 1098 if (EFI_ERROR (Ip4LeaveGroup (IpInstance, NTOHL (Group)))) {\r
1099 return EFI_DEVICE_ERROR;\r
1100 }\r
1101\r
1102 Ip4RemoveGroupAddr (IpInstance->Groups, IpInstance->GroupCount, Group);\r
1103 IpInstance->GroupCount--;\r
1104\r
1105 if (IpInstance->GroupCount == 0) {\r
1106 ASSERT (Index == 1);\r
1107\r
766c7483 1108 FreePool (IpInstance->Groups);\r
772db4bb 1109 IpInstance->Groups = NULL;\r
1110 }\r
1111\r
1112 if (GroupAddress != NULL) {\r
1113 return EFI_SUCCESS;\r
1114 }\r
1115 }\r
1116 }\r
1117\r
1118 return ((GroupAddress != NULL) ? EFI_NOT_FOUND : EFI_SUCCESS);\r
1119}\r
1120\r
1121\r
1122/**\r
5405e9a6 1123 Joins and leaves multicast groups.\r
e2851998 1124\r
5405e9a6 1125 The Groups() function is used to join and leave multicast group sessions. Joining\r
1126 a group will enable reception of matching multicast packets. Leaving a group will\r
1127 disable the multicast packet reception.\r
e2851998 1128\r
5405e9a6 1129 If JoinFlag is FALSE and GroupAddress is NULL, all joined groups will be left.\r
1130\r
3e8c18da 1131 @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.\r
1132 @param[in] JoinFlag Set to TRUE to join the multicast group session and FALSE to leave.\r
1133 @param[in] GroupAddress Pointer to the IPv4 multicast address.\r
5405e9a6 1134\r
1135 @retval EFI_SUCCESS The operation completed successfully.\r
1136 @retval EFI_INVALID_PARAMETER One or more of the following is TRUE:\r
1137 - This is NULL.\r
1138 - JoinFlag is TRUE and GroupAddress is NULL.\r
1139 - GroupAddress is not NULL and *GroupAddress is\r
1140 not a multicast IPv4 address.\r
1141 @retval EFI_NOT_STARTED This instance has not been started.\r
1142 @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP,\r
1143 RARP, etc.) is not finished yet.\r
1144 @retval EFI_OUT_OF_RESOURCES System resources could not be allocated.\r
1145 @retval EFI_UNSUPPORTED This EFI IPv4 Protocol implementation does not support multicast groups.\r
1146 @retval EFI_ALREADY_STARTED The group address is already in the group table (when\r
1147 JoinFlag is TRUE).\r
1148 @retval EFI_NOT_FOUND The group address is not in the group table (when JoinFlag is FALSE).\r
1149 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.\r
772db4bb 1150\r
1151**/\r
772db4bb 1152EFI_STATUS\r
1153EFIAPI\r
1154EfiIp4Groups (\r
1155 IN EFI_IP4_PROTOCOL *This,\r
1156 IN BOOLEAN JoinFlag,\r
1157 IN EFI_IPv4_ADDRESS *GroupAddress OPTIONAL\r
1158 )\r
1159{\r
1160 IP4_PROTOCOL *IpInstance;\r
1161 EFI_STATUS Status;\r
1162 EFI_TPL OldTpl;\r
1163 IP4_ADDR McastIp;\r
1164\r
1165 if ((This == NULL) || (JoinFlag && (GroupAddress == NULL))) {\r
1166 return EFI_INVALID_PARAMETER;\r
1167 }\r
1168\r
1169 if (GroupAddress != NULL) {\r
e48e37fc 1170 CopyMem (&McastIp, GroupAddress, sizeof (IP4_ADDR));\r
772db4bb 1171\r
1172 if (!IP4_IS_MULTICAST (NTOHL (McastIp))) {\r
1173 return EFI_INVALID_PARAMETER;\r
1174 }\r
1175 }\r
1176\r
1177 IpInstance = IP4_INSTANCE_FROM_PROTOCOL (This);\r
e48e37fc 1178 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
772db4bb 1179\r
1180 if (IpInstance->State != IP4_STATE_CONFIGED) {\r
1181 Status = EFI_NOT_STARTED;\r
1182 goto ON_EXIT;\r
1183 }\r
1184\r
1185 if (IpInstance->ConfigData.UseDefaultAddress && IP4_NO_MAPPING (IpInstance)) {\r
1186 Status = EFI_NO_MAPPING;\r
1187 goto ON_EXIT;\r
1188 }\r
1189\r
1190 Status = Ip4Groups (IpInstance, JoinFlag, GroupAddress);\r
1191\r
1192ON_EXIT:\r
e48e37fc 1193 gBS->RestoreTPL (OldTpl);\r
772db4bb 1194 return Status;\r
1195}\r
1196\r
1197\r
1198/**\r
5405e9a6 1199 Adds and deletes routing table entries.\r
1200\r
1201 The Routes() function adds a route to or deletes a route from the routing table.\r
e2851998 1202\r
5405e9a6 1203 Routes are determined by comparing the SubnetAddress with the destination IPv4\r
1204 address arithmetically AND-ed with the SubnetMask. The gateway address must be\r
1205 on the same subnet as the configured station address.\r
e2851998 1206\r
5405e9a6 1207 The default route is added with SubnetAddress and SubnetMask both set to 0.0.0.0.\r
1208 The default route matches all destination IPv4 addresses that do not match any\r
1209 other routes.\r
e2851998 1210\r
5405e9a6 1211 A GatewayAddress that is zero is a nonroute. Packets are sent to the destination\r
1212 IP address if it can be found in the ARP cache or on the local subnet. One automatic\r
1213 nonroute entry will be inserted into the routing table for outgoing packets that\r
1214 are addressed to a local subnet (gateway address of 0.0.0.0).\r
e2851998 1215\r
5405e9a6 1216 Each EFI IPv4 Protocol instance has its own independent routing table. Those EFI\r
1217 IPv4 Protocol instances that use the default IPv4 address will also have copies\r
1218 of the routing table that was provided by the EFI_IP4_CONFIG_PROTOCOL, and these\r
1219 copies will be updated whenever the EIF IPv4 Protocol driver reconfigures its\r
1220 instances. As a result, client modification to the routing table will be lost.\r
1221\r
3e8c18da 1222 @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.\r
1223 @param[in] DeleteRoute Set to TRUE to delete this route from the routing table. Set to\r
1224 FALSE to add this route to the routing table. SubnetAddress\r
1225 and SubnetMask are used as the key to each route entry.\r
1226 @param[in] SubnetAddress The address of the subnet that needs to be routed.\r
1227 @param[in] SubnetMask The subnet mask of SubnetAddress.\r
1228 @param[in] GatewayAddress The unicast gateway IPv4 address for this route.\r
5405e9a6 1229\r
1230 @retval EFI_SUCCESS The operation completed successfully.\r
1231 @retval EFI_NOT_STARTED The driver instance has not been started.\r
1232 @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP,\r
1233 RARP, etc.) is not finished yet.\r
1234 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:\r
1235 - This is NULL.\r
1236 - SubnetAddress is NULL.\r
1237 - SubnetMask is NULL.\r
1238 - GatewayAddress is NULL.\r
1239 - *SubnetAddress is not a valid subnet address.\r
1240 - *SubnetMask is not a valid subnet mask.\r
1241 - *GatewayAddress is not a valid unicast IPv4 address.\r
1242 @retval EFI_OUT_OF_RESOURCES Could not add the entry to the routing table.\r
1243 @retval EFI_NOT_FOUND This route is not in the routing table (when DeleteRoute is TRUE).\r
1244 @retval EFI_ACCESS_DENIED The route is already defined in the routing table (when\r
1245 DeleteRoute is FALSE).\r
e2851998 1246\r
772db4bb 1247**/\r
772db4bb 1248EFI_STATUS\r
1249EFIAPI\r
1250EfiIp4Routes (\r
1251 IN EFI_IP4_PROTOCOL *This,\r
1252 IN BOOLEAN DeleteRoute,\r
1253 IN EFI_IPv4_ADDRESS *SubnetAddress,\r
1254 IN EFI_IPv4_ADDRESS *SubnetMask,\r
1255 IN EFI_IPv4_ADDRESS *GatewayAddress\r
1256 )\r
1257{\r
1258 IP4_PROTOCOL *IpInstance;\r
1259 IP4_INTERFACE *IpIf;\r
1260 IP4_ADDR Dest;\r
1261 IP4_ADDR Netmask;\r
1262 IP4_ADDR Nexthop;\r
1263 EFI_STATUS Status;\r
1264 EFI_TPL OldTpl;\r
1265\r
1266 //\r
1267 // First, validate the parameters\r
1268 //\r
1269 if ((This == NULL) || (SubnetAddress == NULL) ||\r
1270 (SubnetMask == NULL) || (GatewayAddress == NULL)) {\r
1271 return EFI_INVALID_PARAMETER;\r
1272 }\r
1273\r
1274 IpInstance = IP4_INSTANCE_FROM_PROTOCOL (This);\r
e48e37fc 1275 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
772db4bb 1276\r
1277 if (IpInstance->State != IP4_STATE_CONFIGED) {\r
1278 Status = EFI_NOT_STARTED;\r
1279 goto ON_EXIT;\r
1280 }\r
1281\r
1282 if (IpInstance->ConfigData.UseDefaultAddress && IP4_NO_MAPPING (IpInstance)) {\r
1283 Status = EFI_NO_MAPPING;\r
1284 goto ON_EXIT;\r
1285 }\r
1286\r
e48e37fc 1287 CopyMem (&Dest, SubnetAddress, sizeof (IP4_ADDR));\r
1288 CopyMem (&Netmask, SubnetMask, sizeof (IP4_ADDR));\r
1289 CopyMem (&Nexthop, GatewayAddress, sizeof (IP4_ADDR));\r
772db4bb 1290\r
1291 Dest = NTOHL (Dest);\r
1292 Netmask = NTOHL (Netmask);\r
1293 Nexthop = NTOHL (Nexthop);\r
1294\r
1295 IpIf = IpInstance->Interface;\r
1296\r
1297 if (!IP4_IS_VALID_NETMASK (Netmask)) {\r
1298 Status = EFI_INVALID_PARAMETER;\r
1299 goto ON_EXIT;\r
1300 }\r
1301\r
1302 //\r
1303 // the gateway address must be a unicast on the connected network if not zero.\r
1304 //\r
1305 if ((Nexthop != IP4_ALLZERO_ADDRESS) &&\r
1306 (!IP4_NET_EQUAL (Nexthop, IpIf->Ip, IpIf->SubnetMask) ||\r
1307 IP4_IS_BROADCAST (Ip4GetNetCast (Nexthop, IpIf)))) {\r
1308\r
1309 Status = EFI_INVALID_PARAMETER;\r
1310 goto ON_EXIT;\r
1311 }\r
1312\r
1313 if (DeleteRoute) {\r
1314 Status = Ip4DelRoute (IpInstance->RouteTable, Dest, Netmask, Nexthop);\r
1315 } else {\r
1316 Status = Ip4AddRoute (IpInstance->RouteTable, Dest, Netmask, Nexthop);\r
1317 }\r
1318\r
1319ON_EXIT:\r
e48e37fc 1320 gBS->RestoreTPL (OldTpl);\r
772db4bb 1321 return Status;\r
1322}\r
1323\r
1324\r
1325/**\r
1326 Check whether the user's token or event has already\r
5405e9a6 1327 been enqueued on IP4's list.\r
772db4bb 1328\r
3e8c18da 1329 @param[in] Map The container of either user's transmit or receive\r
1330 token.\r
1f6729ff 1331 @param[in] Item Current item to check against.\r
3e8c18da 1332 @param[in] Context The Token to check againist.\r
772db4bb 1333\r
1f6729ff 1334 @retval EFI_ACCESS_DENIED The token or event has already been enqueued in IP.\r
772db4bb 1335 @retval EFI_SUCCESS The current item isn't the same token/event as the\r
1336 context.\r
1337\r
1338**/\r
772db4bb 1339EFI_STATUS\r
e798cd87 1340EFIAPI\r
772db4bb 1341Ip4TokenExist (\r
1342 IN NET_MAP *Map,\r
1343 IN NET_MAP_ITEM *Item,\r
1344 IN VOID *Context\r
1345 )\r
1346{\r
1347 EFI_IP4_COMPLETION_TOKEN *Token;\r
1348 EFI_IP4_COMPLETION_TOKEN *TokenInItem;\r
1349\r
1350 Token = (EFI_IP4_COMPLETION_TOKEN *) Context;\r
1351 TokenInItem = (EFI_IP4_COMPLETION_TOKEN *) Item->Key;\r
1352\r
1353 if ((Token == TokenInItem) || (Token->Event == TokenInItem->Event)) {\r
1354 return EFI_ACCESS_DENIED;\r
1355 }\r
1356\r
1357 return EFI_SUCCESS;\r
1358}\r
1359\r
772db4bb 1360/**\r
1361 Validate the user's token against current station address.\r
1362\r
216f7970 1363 @param[in] Token User's token to validate.\r
1364 @param[in] IpIf The IP4 child's interface.\r
1365 @param[in] RawData Set to TRUE to send unformatted packets.\r
772db4bb 1366\r
216f7970 1367 @retval EFI_INVALID_PARAMETER Some parameters are invalid.\r
772db4bb 1368 @retval EFI_BAD_BUFFER_SIZE The user's option/data is too long.\r
216f7970 1369 @retval EFI_SUCCESS The token is valid.\r
772db4bb 1370\r
1371**/\r
772db4bb 1372EFI_STATUS\r
1373Ip4TxTokenValid (\r
1374 IN EFI_IP4_COMPLETION_TOKEN *Token,\r
216f7970 1375 IN IP4_INTERFACE *IpIf,\r
1376 IN BOOLEAN RawData\r
772db4bb 1377 )\r
1378{\r
1379 EFI_IP4_TRANSMIT_DATA *TxData;\r
1380 EFI_IP4_OVERRIDE_DATA *Override;\r
1381 IP4_ADDR Src;\r
1382 IP4_ADDR Gateway;\r
1383 UINT32 Offset;\r
1384 UINT32 Index;\r
1385 UINT32 HeadLen;\r
1386\r
1387 if ((Token == NULL) || (Token->Event == NULL) || (Token->Packet.TxData == NULL)) {\r
1388 return EFI_INVALID_PARAMETER;\r
1389 }\r
1390\r
1391 TxData = Token->Packet.TxData;\r
1392\r
1393 //\r
216f7970 1394 // Check the fragment table: no empty fragment, and length isn't bogus.\r
772db4bb 1395 //\r
1396 if ((TxData->TotalDataLength == 0) || (TxData->FragmentCount == 0)) {\r
1397 return EFI_INVALID_PARAMETER;\r
1398 }\r
1399\r
1400 Offset = TxData->TotalDataLength;\r
1401\r
216f7970 1402 if (Offset > IP4_MAX_PACKET_SIZE) {\r
1403 return EFI_BAD_BUFFER_SIZE;\r
1404 }\r
1405\r
772db4bb 1406 for (Index = 0; Index < TxData->FragmentCount; Index++) {\r
1407 if ((TxData->FragmentTable[Index].FragmentBuffer == NULL) ||\r
1408 (TxData->FragmentTable[Index].FragmentLength == 0)) {\r
1409\r
1410 return EFI_INVALID_PARAMETER;\r
1411 }\r
1412\r
1413 Offset -= TxData->FragmentTable[Index].FragmentLength;\r
1414 }\r
1415\r
1416 if (Offset != 0) {\r
1417 return EFI_INVALID_PARAMETER;\r
1418 }\r
1419\r
216f7970 1420 //\r
1421 // NOTE that OptionsLength/OptionsBuffer/OverrideData are ignored if RawData\r
1422 // is TRUE.\r
1423 //\r
1424 if (RawData) {\r
1425 return EFI_SUCCESS;\r
1426 }\r
1427\r
1428 //\r
1429 // Check the IP options: no more than 40 bytes and format is OK\r
1430 //\r
1431 if (TxData->OptionsLength != 0) {\r
1432 if ((TxData->OptionsLength > 40) || (TxData->OptionsBuffer == NULL)) {\r
1433 return EFI_INVALID_PARAMETER;\r
1434 }\r
1435\r
1436 if (!Ip4OptionIsValid (TxData->OptionsBuffer, TxData->OptionsLength, FALSE)) {\r
1437 return EFI_INVALID_PARAMETER;\r
1438 }\r
1439 }\r
1440\r
772db4bb 1441 //\r
1442 // Check the source and gateway: they must be a valid unicast.\r
1443 // Gateway must also be on the connected network.\r
1444 //\r
e2851998 1445 if (TxData->OverrideData != NULL) {\r
772db4bb 1446 Override = TxData->OverrideData;\r
1447\r
e48e37fc 1448 CopyMem (&Src, &Override->SourceAddress, sizeof (IP4_ADDR));\r
1449 CopyMem (&Gateway, &Override->GatewayAddress, sizeof (IP4_ADDR));\r
772db4bb 1450\r
1451 Src = NTOHL (Src);\r
1452 Gateway = NTOHL (Gateway);\r
1453\r
1454 if ((NetGetIpClass (Src) > IP4_ADDR_CLASSC) ||\r
1455 (Src == IP4_ALLONE_ADDRESS) ||\r
1456 IP4_IS_BROADCAST (Ip4GetNetCast (Src, IpIf))) {\r
1457\r
1458 return EFI_INVALID_PARAMETER;\r
1459 }\r
1460\r
1461 //\r
1462 // If gateway isn't zero, it must be a unicast address, and\r
1463 // on the connected network.\r
1464 //\r
1465 if ((Gateway != IP4_ALLZERO_ADDRESS) &&\r
1466 ((NetGetIpClass (Gateway) > IP4_ADDR_CLASSC) ||\r
1467 !IP4_NET_EQUAL (Gateway, IpIf->Ip, IpIf->SubnetMask) ||\r
1468 IP4_IS_BROADCAST (Ip4GetNetCast (Gateway, IpIf)))) {\r
1469\r
1470 return EFI_INVALID_PARAMETER;\r
1471 }\r
1472 }\r
1473\r
1474 //\r
1475 // Check the packet length: Head length and packet length all has a limit\r
1476 //\r
1477 HeadLen = sizeof (IP4_HEAD) + ((TxData->OptionsLength + 3) &~0x03);\r
1478\r
1479 if ((HeadLen > IP4_MAX_HEADLEN) ||\r
1480 (TxData->TotalDataLength + HeadLen > IP4_MAX_PACKET_SIZE)) {\r
1481\r
1482 return EFI_BAD_BUFFER_SIZE;\r
1483 }\r
1484\r
1485 return EFI_SUCCESS;\r
1486}\r
1487\r
1488\r
1489/**\r
1490 The callback function for the net buffer which wraps the user's\r
1491 transmit token. Although it seems this function is pretty simple,\r
1492 there are some subtle things.\r
1493 When user requests the IP to transmit a packet by passing it a\r
1494 token, the token is wrapped in an IP4_TXTOKEN_WRAP and the data\r
1495 is wrapped in an net buffer. the net buffer's Free function is\r
1496 set to Ip4FreeTxToken. The Token and token wrap are added to the\r
1497 IP child's TxToken map. Then the buffer is passed to Ip4Output for\r
1498 transmission. If something error happened before that, the buffer\r
1499 is freed, which in turn will free the token wrap. The wrap may\r
1500 have been added to the TxToken map or not, and the user's event\r
1501 shouldn't be fired because we are still in the EfiIp4Transmit. If\r
1502 the buffer has been sent by Ip4Output, it should be removed from\r
1503 the TxToken map and user's event signaled. The token wrap and buffer\r
1504 are bound together. Check the comments in Ip4Output for information\r
1505 about IP fragmentation.\r
1506\r
1f6729ff 1507 @param[in] Context The token's wrap.\r
772db4bb 1508\r
1509**/\r
772db4bb 1510VOID\r
e798cd87 1511EFIAPI\r
772db4bb 1512Ip4FreeTxToken (\r
1513 IN VOID *Context\r
1514 )\r
1515{\r
1516 IP4_TXTOKEN_WRAP *Wrap;\r
1517 NET_MAP_ITEM *Item;\r
1518\r
1519 Wrap = (IP4_TXTOKEN_WRAP *) Context;\r
1520\r
a1503a32 1521 //\r
1522 // Signal IpSecRecycleEvent to inform IPsec free the memory\r
1523 //\r
1524 if (Wrap->IpSecRecycleSignal != NULL) {\r
1525 gBS->SignalEvent (Wrap->IpSecRecycleSignal);\r
1526 }\r
1527\r
772db4bb 1528 //\r
1529 // Find the token in the instance's map. EfiIp4Transmit put the\r
1530 // token to the map. If that failed, NetMapFindKey will return NULL.\r
1531 //\r
1532 Item = NetMapFindKey (&Wrap->IpInstance->TxTokens, Wrap->Token);\r
1533\r
1534 if (Item != NULL) {\r
1535 NetMapRemoveItem (&Wrap->IpInstance->TxTokens, Item, NULL);\r
1536 }\r
1537\r
1538 if (Wrap->Sent) {\r
1539 gBS->SignalEvent (Wrap->Token->Event);\r
36ee91ca 1540\r
1541 //\r
1542 // Dispatch the DPC queued by the NotifyFunction of Token->Event.\r
1543 //\r
d8d26fb2 1544 DispatchDpc ();\r
772db4bb 1545 }\r
1546\r
766c7483 1547 FreePool (Wrap);\r
772db4bb 1548}\r
1549\r
1550\r
1551/**\r
1552 The callback function to Ip4Output to update the transmit status.\r
1553\r
1554 @param Ip4Instance The Ip4Instance that request the transmit.\r
1f6729ff 1555 @param Packet The user's transmit request.\r
1556 @param IoStatus The result of the transmission.\r
1557 @param Flag Not used during transmission.\r
772db4bb 1558 @param Context The token's wrap.\r
1559\r
772db4bb 1560**/\r
772db4bb 1561VOID\r
1562Ip4OnPacketSent (\r
1563 IP4_PROTOCOL *Ip4Instance,\r
1564 NET_BUF *Packet,\r
1565 EFI_STATUS IoStatus,\r
1566 UINT32 Flag,\r
1567 VOID *Context\r
1568 )\r
1569{\r
1570 IP4_TXTOKEN_WRAP *Wrap;\r
1571\r
1572 //\r
1573 // This is the transmission request from upper layer,\r
1574 // not the IP4 driver itself.\r
1575 //\r
1576 ASSERT (Ip4Instance != NULL);\r
1577\r
1578 //\r
1579 // The first fragment of the packet has been sent. Update\r
1580 // the token's status. That is, if fragmented, the transmit's\r
1581 // status is the first fragment's status. The Wrap will be\r
1582 // release when all the fragments are release. Check the comments\r
1583 // in Ip4FreeTxToken and Ip4Output for information.\r
1584 //\r
1585 Wrap = (IP4_TXTOKEN_WRAP *) Context;\r
1586 Wrap->Token->Status = IoStatus;\r
1587\r
1588 NetbufFree (Wrap->Packet);\r
1589}\r
1590\r
1591\r
1592/**\r
5405e9a6 1593 Places outgoing data packets into the transmit queue.\r
1594\r
1595 The Transmit() function places a sending request in the transmit queue of this\r
1596 EFI IPv4 Protocol instance. Whenever the packet in the token is sent out or some\r
1597 errors occur, the event in the token will be signaled and the status is updated.\r
1598\r
3e8c18da 1599 @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.\r
1600 @param[in] Token Pointer to the transmit token.\r
5405e9a6 1601\r
1602 @retval EFI_SUCCESS The data has been queued for transmission.\r
1603 @retval EFI_NOT_STARTED This instance has not been started.\r
1604 @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP,\r
1605 RARP, etc.) is not finished yet.\r
1606 @retval EFI_INVALID_PARAMETER One or more pameters are invalid.\r
1607 @retval EFI_ACCESS_DENIED The transmit completion token with the same Token.Event\r
1608 was already in the transmit queue.\r
1609 @retval EFI_NOT_READY The completion token could not be queued because the transmit\r
e2851998 1610 queue is full.\r
5405e9a6 1611 @retval EFI_NOT_FOUND Not route is found to destination address.\r
1612 @retval EFI_OUT_OF_RESOURCES Could not queue the transmit data.\r
1613 @retval EFI_BUFFER_TOO_SMALL Token.Packet.TxData.TotalDataLength is too\r
1614 short to transmit.\r
1615 @retval EFI_BAD_BUFFER_SIZE The length of the IPv4 header + option length + total data length is\r
1616 greater than MTU (or greater than the maximum packet size if\r
1617 Token.Packet.TxData.OverrideData.\r
1f6729ff 1618 DoNotFragment is TRUE).\r
772db4bb 1619\r
1620**/\r
772db4bb 1621EFI_STATUS\r
1622EFIAPI\r
1623EfiIp4Transmit (\r
1624 IN EFI_IP4_PROTOCOL *This,\r
1625 IN EFI_IP4_COMPLETION_TOKEN *Token\r
1626 )\r
1627{\r
1628 IP4_SERVICE *IpSb;\r
1629 IP4_PROTOCOL *IpInstance;\r
1630 IP4_INTERFACE *IpIf;\r
1631 IP4_TXTOKEN_WRAP *Wrap;\r
1632 EFI_IP4_TRANSMIT_DATA *TxData;\r
1633 EFI_IP4_CONFIG_DATA *Config;\r
1634 EFI_IP4_OVERRIDE_DATA *Override;\r
1635 IP4_HEAD Head;\r
1636 IP4_ADDR GateWay;\r
1637 EFI_STATUS Status;\r
1638 EFI_TPL OldTpl;\r
1639 BOOLEAN DontFragment;\r
1640 UINT32 HeadLen;\r
216f7970 1641 UINT8 RawHdrLen;\r
1642 UINT32 OptionsLength;\r
1643 UINT8 *OptionsBuffer;\r
1644 VOID *FirstFragment;\r
772db4bb 1645\r
1646 if (This == NULL) {\r
1647 return EFI_INVALID_PARAMETER;\r
1648 }\r
1649\r
1650 IpInstance = IP4_INSTANCE_FROM_PROTOCOL (This);\r
1651\r
1652 if (IpInstance->State != IP4_STATE_CONFIGED) {\r
1653 return EFI_NOT_STARTED;\r
1654 }\r
1655\r
e48e37fc 1656 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
772db4bb 1657\r
1658 IpSb = IpInstance->Service;\r
1659 IpIf = IpInstance->Interface;\r
1660 Config = &IpInstance->ConfigData;\r
1661\r
1662 if (Config->UseDefaultAddress && IP4_NO_MAPPING (IpInstance)) {\r
1663 Status = EFI_NO_MAPPING;\r
1664 goto ON_EXIT;\r
1665 }\r
1666\r
1667 //\r
1668 // make sure that token is properly formated\r
1669 //\r
216f7970 1670 Status = Ip4TxTokenValid (Token, IpIf, Config->RawData);\r
772db4bb 1671\r
1672 if (EFI_ERROR (Status)) {\r
1673 goto ON_EXIT;\r
1674 }\r
1675\r
1676 //\r
1677 // Check whether the token or signal already existed.\r
1678 //\r
1679 if (EFI_ERROR (NetMapIterate (&IpInstance->TxTokens, Ip4TokenExist, Token))) {\r
1680 Status = EFI_ACCESS_DENIED;\r
1681 goto ON_EXIT;\r
1682 }\r
1683\r
1684 //\r
1685 // Build the IP header, need to fill in the Tos, TotalLen, Id,\r
1686 // fragment, Ttl, protocol, Src, and Dst.\r
1687 //\r
1688 TxData = Token->Packet.TxData;\r
1689\r
216f7970 1690 FirstFragment = NULL;\r
772db4bb 1691\r
216f7970 1692 if (Config->RawData) {\r
1693 //\r
1694 // When RawData is TRUE, first buffer in FragmentTable points to a raw\r
1695 // IPv4 fragment including IPv4 header and options.\r
1696 //\r
1697 FirstFragment = TxData->FragmentTable[0].FragmentBuffer;\r
1698 CopyMem (&RawHdrLen, FirstFragment, sizeof (UINT8));\r
772db4bb 1699\r
216f7970 1700 RawHdrLen = (UINT8) (RawHdrLen & 0x0f);\r
1701 if (RawHdrLen < 5) {\r
098e9f49
FS
1702 Status = EFI_INVALID_PARAMETER;\r
1703 goto ON_EXIT;\r
216f7970 1704 }\r
1705\r
1706 RawHdrLen = (UINT8) (RawHdrLen << 2);\r
1707 \r
1708 CopyMem (&Head, FirstFragment, IP4_MIN_HEADLEN);\r
1709\r
1710 Ip4NtohHead (&Head);\r
1711 HeadLen = 0;\r
1712 DontFragment = IP4_DO_NOT_FRAGMENT (Head.Fragment);\r
772db4bb 1713\r
216f7970 1714 if (!DontFragment) {\r
098e9f49
FS
1715 Status = EFI_INVALID_PARAMETER;\r
1716 goto ON_EXIT;\r
216f7970 1717 }\r
1718\r
1719 GateWay = IP4_ALLZERO_ADDRESS;\r
1720\r
1721 //\r
1722 // Get IPv4 options from first fragment.\r
1723 //\r
1724 if (RawHdrLen == IP4_MIN_HEADLEN) {\r
1725 OptionsLength = 0;\r
1726 OptionsBuffer = NULL;\r
1727 } else {\r
1728 OptionsLength = RawHdrLen - IP4_MIN_HEADLEN;\r
1729 OptionsBuffer = (UINT8 *) FirstFragment + IP4_MIN_HEADLEN;\r
1730 }\r
1731\r
1732 //\r
1733 // Trim off IPv4 header and options from first fragment.\r
1734 //\r
1735 TxData->FragmentTable[0].FragmentBuffer = (UINT8 *) FirstFragment + RawHdrLen;\r
1736 TxData->FragmentTable[0].FragmentLength = TxData->FragmentTable[0].FragmentLength - RawHdrLen;\r
772db4bb 1737 } else {\r
216f7970 1738 CopyMem (&Head.Dst, &TxData->DestinationAddress, sizeof (IP4_ADDR));\r
1739 Head.Dst = NTOHL (Head.Dst);\r
772db4bb 1740\r
216f7970 1741 if (TxData->OverrideData != NULL) {\r
1742 Override = TxData->OverrideData;\r
1743 Head.Protocol = Override->Protocol;\r
1744 Head.Tos = Override->TypeOfService;\r
1745 Head.Ttl = Override->TimeToLive;\r
1746 DontFragment = Override->DoNotFragment;\r
1747\r
1748 CopyMem (&Head.Src, &Override->SourceAddress, sizeof (IP4_ADDR));\r
1749 CopyMem (&GateWay, &Override->GatewayAddress, sizeof (IP4_ADDR));\r
1750\r
1751 Head.Src = NTOHL (Head.Src);\r
1752 GateWay = NTOHL (GateWay);\r
1753 } else {\r
1754 Head.Src = IpIf->Ip;\r
1755 GateWay = IP4_ALLZERO_ADDRESS;\r
1756 Head.Protocol = Config->DefaultProtocol;\r
1757 Head.Tos = Config->TypeOfService;\r
1758 Head.Ttl = Config->TimeToLive;\r
1759 DontFragment = Config->DoNotFragment;\r
1760 }\r
1761\r
1762 Head.Fragment = IP4_HEAD_FRAGMENT_FIELD (DontFragment, FALSE, 0);\r
1763 HeadLen = (TxData->OptionsLength + 3) & (~0x03);\r
1764\r
1765 OptionsLength = TxData->OptionsLength;\r
1766 OptionsBuffer = (UINT8 *) (TxData->OptionsBuffer);\r
1767 }\r
772db4bb 1768\r
1769 //\r
1770 // If don't fragment and fragment needed, return error\r
1771 //\r
a1503a32 1772 if (DontFragment && (TxData->TotalDataLength + HeadLen > IpSb->MaxPacketSize)) {\r
772db4bb 1773 Status = EFI_BAD_BUFFER_SIZE;\r
1774 goto ON_EXIT;\r
1775 }\r
1776\r
1777 //\r
1778 // OK, it survives all the validation check. Wrap the token in\r
1779 // a IP4_TXTOKEN_WRAP and the data in a netbuf\r
1780 //\r
1781 Status = EFI_OUT_OF_RESOURCES;\r
f6b7393c 1782 Wrap = AllocateZeroPool (sizeof (IP4_TXTOKEN_WRAP));\r
772db4bb 1783 if (Wrap == NULL) {\r
1784 goto ON_EXIT;\r
1785 }\r
1786\r
1787 Wrap->IpInstance = IpInstance;\r
1788 Wrap->Token = Token;\r
1789 Wrap->Sent = FALSE;\r
1790 Wrap->Life = IP4_US_TO_SEC (Config->TransmitTimeout);\r
1791 Wrap->Packet = NetbufFromExt (\r
1792 (NET_FRAGMENT *) TxData->FragmentTable,\r
1793 TxData->FragmentCount,\r
1794 IP4_MAX_HEADLEN,\r
1795 0,\r
1796 Ip4FreeTxToken,\r
1797 Wrap\r
1798 );\r
1799\r
1800 if (Wrap->Packet == NULL) {\r
766c7483 1801 FreePool (Wrap);\r
772db4bb 1802 goto ON_EXIT;\r
1803 }\r
1804\r
1805 Token->Status = EFI_NOT_READY;\r
1806\r
1807 if (EFI_ERROR (NetMapInsertTail (&IpInstance->TxTokens, Token, Wrap))) {\r
1808 //\r
1809 // NetbufFree will call Ip4FreeTxToken, which in turn will\r
1810 // free the IP4_TXTOKEN_WRAP. Now, the token wrap hasn't been\r
1811 // enqueued.\r
1812 //\r
216f7970 1813 if (Config->RawData) {\r
1814 //\r
1815 // Restore pointer of first fragment in RawData mode.\r
1816 //\r
1817 TxData->FragmentTable[0].FragmentBuffer = (UINT8 *) FirstFragment;\r
1818 }\r
1819\r
772db4bb 1820 NetbufFree (Wrap->Packet);\r
1821 goto ON_EXIT;\r
1822 }\r
1823\r
36ee91ca 1824 //\r
1825 // Mark the packet sent before output it. Mark it not sent again if the\r
1826 // returned status is not EFI_SUCCESS;\r
1827 //\r
1828 Wrap->Sent = TRUE;\r
1829\r
772db4bb 1830 Status = Ip4Output (\r
1831 IpSb,\r
1832 IpInstance,\r
1833 Wrap->Packet,\r
1834 &Head,\r
216f7970 1835 OptionsBuffer,\r
1836 OptionsLength,\r
772db4bb 1837 GateWay,\r
1838 Ip4OnPacketSent,\r
1839 Wrap\r
1840 );\r
1841\r
1842 if (EFI_ERROR (Status)) {\r
36ee91ca 1843 Wrap->Sent = FALSE;\r
216f7970 1844\r
1845 if (Config->RawData) {\r
1846 //\r
1847 // Restore pointer of first fragment in RawData mode.\r
1848 //\r
1849 TxData->FragmentTable[0].FragmentBuffer = (UINT8 *) FirstFragment;\r
1850 }\r
1851\r
772db4bb 1852 NetbufFree (Wrap->Packet);\r
772db4bb 1853 }\r
1854\r
216f7970 1855 if (Config->RawData) {\r
1856 //\r
1857 // Restore pointer of first fragment in RawData mode.\r
1858 //\r
1859 TxData->FragmentTable[0].FragmentBuffer = (UINT8 *) FirstFragment;\r
1860 }\r
1861\r
772db4bb 1862ON_EXIT:\r
e48e37fc 1863 gBS->RestoreTPL (OldTpl);\r
772db4bb 1864 return Status;\r
1865}\r
1866\r
1867\r
1868/**\r
5405e9a6 1869 Places a receiving request into the receiving queue.\r
e2851998 1870\r
5405e9a6 1871 The Receive() function places a completion token into the receive packet queue.\r
1872 This function is always asynchronous.\r
e2851998 1873\r
5405e9a6 1874 The Token.Event field in the completion token must be filled in by the caller\r
1875 and cannot be NULL. When the receive operation completes, the EFI IPv4 Protocol\r
1876 driver updates the Token.Status and Token.Packet.RxData fields and the Token.Event\r
1877 is signaled.\r
1878\r
3e8c18da 1879 @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.\r
1880 @param[in] Token Pointer to a token that is associated with the receive data descriptor.\r
5405e9a6 1881\r
1882 @retval EFI_SUCCESS The receive completion token was cached.\r
1883 @retval EFI_NOT_STARTED This EFI IPv4 Protocol instance has not been started.\r
1884 @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP, RARP, etc.)\r
1885 is not finished yet.\r
1886 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:\r
1887 - This is NULL.\r
1888 - Token is NULL.\r
1889 - Token.Event is NULL.\r
1890 @retval EFI_OUT_OF_RESOURCES The receive completion token could not be queued due to a lack of system\r
1891 resources (usually memory).\r
1892 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.\r
1893 The EFI IPv4 Protocol instance has been reset to startup defaults.\r
1894 EFI_ACCESS_DENIED The receive completion token with the same Token.Event was already\r
1895 in the receive queue.\r
1896 @retval EFI_NOT_READY The receive request could not be queued because the receive queue is full.\r
1897 @retval EFI_ICMP_ERROR An ICMP error packet was received.\r
772db4bb 1898\r
1899**/\r
772db4bb 1900EFI_STATUS\r
1901EFIAPI\r
1902EfiIp4Receive (\r
1903 IN EFI_IP4_PROTOCOL *This,\r
1904 IN EFI_IP4_COMPLETION_TOKEN *Token\r
1905 )\r
1906{\r
1907 IP4_PROTOCOL *IpInstance;\r
772db4bb 1908 EFI_STATUS Status;\r
1909 EFI_TPL OldTpl;\r
1910\r
1911 //\r
1912 // First validate the parameters\r
1913 //\r
1914 if ((This == NULL) || (Token == NULL) || (Token->Event == NULL)) {\r
1915 return EFI_INVALID_PARAMETER;\r
1916 }\r
1917\r
1918 IpInstance = IP4_INSTANCE_FROM_PROTOCOL (This);\r
1919\r
e48e37fc 1920 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
772db4bb 1921\r
1922 if (IpInstance->State != IP4_STATE_CONFIGED) {\r
1923 Status = EFI_NOT_STARTED;\r
1924 goto ON_EXIT;\r
1925 }\r
1926\r
772db4bb 1927 //\r
1928 // Check whether the toke is already on the receive queue.\r
1929 //\r
1930 Status = NetMapIterate (&IpInstance->RxTokens, Ip4TokenExist, Token);\r
1931\r
1932 if (EFI_ERROR (Status)) {\r
1933 Status = EFI_ACCESS_DENIED;\r
1934 goto ON_EXIT;\r
1935 }\r
1936\r
1937 //\r
1938 // Queue the token then check whether there is pending received packet.\r
1939 //\r
1940 Status = NetMapInsertTail (&IpInstance->RxTokens, Token, NULL);\r
1941\r
1942 if (EFI_ERROR (Status)) {\r
1943 goto ON_EXIT;\r
1944 }\r
1945\r
1946 Status = Ip4InstanceDeliverPacket (IpInstance);\r
1947\r
36ee91ca 1948 //\r
1949 // Dispatch the DPC queued by the NotifyFunction of this instane's receive\r
1950 // event.\r
1951 //\r
d8d26fb2 1952 DispatchDpc ();\r
36ee91ca 1953\r
772db4bb 1954ON_EXIT:\r
e48e37fc 1955 gBS->RestoreTPL (OldTpl);\r
772db4bb 1956 return Status;\r
1957}\r
1958\r
1959\r
1960/**\r
1961 Cancel the transmitted but not recycled packet. If a matching\r
1962 token is found, it will call Ip4CancelPacket to cancel the\r
1963 packet. Ip4CancelPacket will cancel all the fragments of the\r
1964 packet. When all the fragments are freed, the IP4_TXTOKEN_WRAP\r
1965 will be deleted from the Map, and user's event signalled.\r
1966 Because Ip4CancelPacket and other functions are all called in\r
1967 line, so, after Ip4CancelPacket returns, the Item has been freed.\r
1968\r
1f6729ff 1969 @param[in] Map The IP4 child's transmit queue.\r
1970 @param[in] Item The current transmitted packet to test.\r
1971 @param[in] Context The user's token to cancel.\r
772db4bb 1972\r
1973 @retval EFI_SUCCESS Continue to check the next Item.\r
1974 @retval EFI_ABORTED The user's Token (Token != NULL) is cancelled.\r
1975\r
1976**/\r
772db4bb 1977EFI_STATUS\r
e798cd87 1978EFIAPI\r
772db4bb 1979Ip4CancelTxTokens (\r
1980 IN NET_MAP *Map,\r
1981 IN NET_MAP_ITEM *Item,\r
1982 IN VOID *Context\r
1983 )\r
1984{\r
1985 EFI_IP4_COMPLETION_TOKEN *Token;\r
1986 IP4_TXTOKEN_WRAP *Wrap;\r
1987\r
1988 Token = (EFI_IP4_COMPLETION_TOKEN *) Context;\r
1989\r
1990 //\r
1991 // Return EFI_SUCCESS to check the next item in the map if\r
1992 // this one doesn't match.\r
1993 //\r
1994 if ((Token != NULL) && (Token != Item->Key)) {\r
1995 return EFI_SUCCESS;\r
1996 }\r
1997\r
1998 Wrap = (IP4_TXTOKEN_WRAP *) Item->Value;\r
1999 ASSERT (Wrap != NULL);\r
2000\r
2001 //\r
2002 // Don't access the Item, Wrap and Token's members after this point.\r
2003 // Item and wrap has been freed. And we no longer own the Token.\r
2004 //\r
2005 Ip4CancelPacket (Wrap->IpInstance->Interface, Wrap->Packet, EFI_ABORTED);\r
2006\r
2007 //\r
2008 // If only one item is to be cancel, return EFI_ABORTED to stop\r
2009 // iterating the map any more.\r
2010 //\r
2011 if (Token != NULL) {\r
2012 return EFI_ABORTED;\r
2013 }\r
2014\r
2015 return EFI_SUCCESS;\r
2016}\r
2017\r
2018\r
2019/**\r
2020 Cancel the receive request. This is quiet simple, because\r
2021 it is only enqueued in our local receive map.\r
2022\r
1f6729ff 2023 @param[in] Map The IP4 child's receive queue.\r
2024 @param[in] Item Current receive request to cancel.\r
2025 @param[in] Context The user's token to cancel.\r
772db4bb 2026\r
2027 @retval EFI_SUCCESS Continue to check the next receive request on the\r
2028 queue.\r
2029 @retval EFI_ABORTED The user's token (token != NULL) has been\r
2030 cancelled.\r
2031\r
2032**/\r
772db4bb 2033EFI_STATUS\r
e798cd87 2034EFIAPI\r
772db4bb 2035Ip4CancelRxTokens (\r
2036 IN NET_MAP *Map,\r
2037 IN NET_MAP_ITEM *Item,\r
2038 IN VOID *Context\r
2039 )\r
2040{\r
2041 EFI_IP4_COMPLETION_TOKEN *Token;\r
2042 EFI_IP4_COMPLETION_TOKEN *This;\r
2043\r
2044 Token = (EFI_IP4_COMPLETION_TOKEN *) Context;\r
2045 This = Item->Key;\r
2046\r
2047 if ((Token != NULL) && (Token != This)) {\r
2048 return EFI_SUCCESS;\r
2049 }\r
2050\r
2051 NetMapRemoveItem (Map, Item, NULL);\r
2052\r
2053 This->Status = EFI_ABORTED;\r
2054 This->Packet.RxData = NULL;\r
2055 gBS->SignalEvent (This->Event);\r
2056\r
2057 if (Token != NULL) {\r
2058 return EFI_ABORTED;\r
2059 }\r
2060\r
2061 return EFI_SUCCESS;\r
2062}\r
2063\r
2064\r
2065/**\r
2066 Cancel the user's receive/transmit request.\r
2067\r
1f6729ff 2068 @param[in] IpInstance The IP4 child.\r
3e8c18da 2069 @param[in] Token The token to cancel. If NULL, all token will be\r
772db4bb 2070 cancelled.\r
2071\r
1f6729ff 2072 @retval EFI_SUCCESS The token is cancelled.\r
772db4bb 2073 @retval EFI_NOT_FOUND The token isn't found on either the\r
1f6729ff 2074 transmit/receive queue.\r
772db4bb 2075 @retval EFI_DEVICE_ERROR Not all token is cancelled when Token is NULL.\r
2076\r
2077**/\r
2078EFI_STATUS\r
2079Ip4Cancel (\r
2080 IN IP4_PROTOCOL *IpInstance,\r
2081 IN EFI_IP4_COMPLETION_TOKEN *Token OPTIONAL\r
2082 )\r
2083{\r
2084 EFI_STATUS Status;\r
2085\r
2086 //\r
2087 // First check the transmitted packet. Ip4CancelTxTokens returns\r
2088 // EFI_ABORTED to mean that the token has been cancelled when\r
2089 // token != NULL. So, return EFI_SUCCESS for this condition.\r
2090 //\r
2091 Status = NetMapIterate (&IpInstance->TxTokens, Ip4CancelTxTokens, Token);\r
2092\r
2093 if (EFI_ERROR (Status)) {\r
2094 if ((Token != NULL) && (Status == EFI_ABORTED)) {\r
2095 return EFI_SUCCESS;\r
2096 }\r
2097\r
2098 return Status;\r
2099 }\r
2100\r
2101 //\r
2102 // Check the receive queue. Ip4CancelRxTokens also returns EFI_ABORT\r
2103 // for Token!=NULL and it is cancelled.\r
2104 //\r
2105 Status = NetMapIterate (&IpInstance->RxTokens, Ip4CancelRxTokens, Token);\r
36ee91ca 2106 //\r
2107 // Dispatch the DPCs queued by the NotifyFunction of the canceled rx token's\r
2108 // events.\r
2109 //\r
d8d26fb2 2110 DispatchDpc ();\r
772db4bb 2111 if (EFI_ERROR (Status)) {\r
2112 if ((Token != NULL) && (Status == EFI_ABORTED)) {\r
2113 return EFI_SUCCESS;\r
2114 }\r
2115\r
2116 return Status;\r
2117 }\r
2118\r
2119 //\r
2120 // OK, if the Token is found when Token != NULL, the NetMapIterate\r
2121 // will return EFI_ABORTED, which has been interrupted as EFI_SUCCESS.\r
2122 //\r
2123 if (Token != NULL) {\r
2124 return EFI_NOT_FOUND;\r
2125 }\r
2126\r
2127 //\r
2128 // If Token == NULL, cancel all the tokens. return error if no\r
2129 // all of them are cancelled.\r
2130 //\r
2131 if (!NetMapIsEmpty (&IpInstance->TxTokens) ||\r
2132 !NetMapIsEmpty (&IpInstance->RxTokens)) {\r
2133\r
2134 return EFI_DEVICE_ERROR;\r
2135 }\r
2136\r
2137 return EFI_SUCCESS;\r
2138}\r
2139\r
2140\r
2141/**\r
5405e9a6 2142 Abort an asynchronous transmit or receive request.\r
e2851998 2143\r
5405e9a6 2144 The Cancel() function is used to abort a pending transmit or receive request.\r
2145 If the token is in the transmit or receive request queues, after calling this\r
2146 function, Token->Status will be set to EFI_ABORTED and then Token->Event will\r
2147 be signaled. If the token is not in one of the queues, which usually means the\r
2148 asynchronous operation has completed, this function will not signal the token\r
2149 and EFI_NOT_FOUND is returned.\r
2150\r
3e8c18da 2151 @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.\r
2152 @param[in] Token Pointer to a token that has been issued by\r
2153 EFI_IP4_PROTOCOL.Transmit() or\r
2154 EFI_IP4_PROTOCOL.Receive(). If NULL, all pending\r
2155 tokens are aborted. Type EFI_IP4_COMPLETION_TOKEN is\r
2156 defined in EFI_IP4_PROTOCOL.Transmit().\r
5405e9a6 2157\r
2158 @retval EFI_SUCCESS The asynchronous I/O request was aborted and\r
2159 Token.->Event was signaled. When Token is NULL, all\r
2160 pending requests were aborted and their events were signaled.\r
2161 @retval EFI_INVALID_PARAMETER This is NULL.\r
2162 @retval EFI_NOT_STARTED This instance has not been started.\r
2163 @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP,\r
2164 RARP, etc.) is not finished yet.\r
2165 @retval EFI_NOT_FOUND When Token is not NULL, the asynchronous I/O request was\r
2166 not found in the transmit or receive queue. It has either completed\r
2167 or was not issued by Transmit() and Receive().\r
772db4bb 2168\r
2169**/\r
772db4bb 2170EFI_STATUS\r
2171EFIAPI\r
2172EfiIp4Cancel (\r
2173 IN EFI_IP4_PROTOCOL *This,\r
2174 IN EFI_IP4_COMPLETION_TOKEN *Token OPTIONAL\r
2175 )\r
2176{\r
2177 IP4_PROTOCOL *IpInstance;\r
2178 EFI_STATUS Status;\r
2179 EFI_TPL OldTpl;\r
2180\r
2181 if (This == NULL) {\r
2182 return EFI_INVALID_PARAMETER;\r
2183 }\r
2184\r
2185 IpInstance = IP4_INSTANCE_FROM_PROTOCOL (This);\r
2186\r
e48e37fc 2187 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
772db4bb 2188\r
2189 if (IpInstance->State != IP4_STATE_CONFIGED) {\r
2190 Status = EFI_NOT_STARTED;\r
2191 goto ON_EXIT;\r
2192 }\r
2193\r
2194 if (IpInstance->ConfigData.UseDefaultAddress && IP4_NO_MAPPING (IpInstance)) {\r
2195 Status = EFI_NO_MAPPING;\r
2196 goto ON_EXIT;\r
2197 }\r
2198\r
2199 Status = Ip4Cancel (IpInstance, Token);\r
2200\r
2201ON_EXIT:\r
e48e37fc 2202 gBS->RestoreTPL (OldTpl);\r
772db4bb 2203 return Status;\r
2204}\r
2205\r
2206\r
2207/**\r
5405e9a6 2208 Polls for incoming data packets and processes outgoing data packets.\r
e2851998 2209\r
5405e9a6 2210 The Poll() function polls for incoming data packets and processes outgoing data\r
2211 packets. Network drivers and applications can call the EFI_IP4_PROTOCOL.Poll()\r
2212 function to increase the rate that data packets are moved between the communications\r
2213 device and the transmit and receive queues.\r
e2851998 2214\r
5405e9a6 2215 In some systems the periodic timer event may not poll the underlying communications\r
2216 device fast enough to transmit and/or receive all data packets without missing\r
2217 incoming packets or dropping outgoing packets. Drivers and applications that are\r
2218 experiencing packet loss should try calling the EFI_IP4_PROTOCOL.Poll() function\r
2219 more often.\r
2220\r
3e8c18da 2221 @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.\r
5405e9a6 2222\r
2223 @retval EFI_SUCCESS Incoming or outgoing data was processed.\r
2224 @retval EFI_NOT_STARTED This EFI IPv4 Protocol instance has not been started.\r
2225 @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP,\r
2226 RARP, etc.) is not finished yet.\r
2227 @retval EFI_INVALID_PARAMETER This is NULL.\r
2228 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.\r
2229 @retval EFI_NOT_READY No incoming or outgoing data is processed.\r
2230 @retval EFI_TIMEOUT Data was dropped out of the transmit and/or receive queue.\r
2231 Consider increasing the polling rate.\r
772db4bb 2232\r
2233**/\r
772db4bb 2234EFI_STATUS\r
2235EFIAPI\r
2236EfiIp4Poll (\r
2237 IN EFI_IP4_PROTOCOL *This\r
2238 )\r
2239{\r
2240 IP4_PROTOCOL *IpInstance;\r
2241 EFI_MANAGED_NETWORK_PROTOCOL *Mnp;\r
2242\r
2243 if (This == NULL) {\r
2244 return EFI_INVALID_PARAMETER;\r
2245 }\r
2246\r
2247 IpInstance = IP4_INSTANCE_FROM_PROTOCOL (This);\r
2248\r
2249 if (IpInstance->State == IP4_STATE_UNCONFIGED) {\r
2250 return EFI_NOT_STARTED;\r
2251 }\r
2252\r
2253 Mnp = IpInstance->Service->Mnp;\r
2254\r
2255 //\r
2256 // Don't lock the Poll function to enable the deliver of\r
2257 // the packet polled up.\r
2258 //\r
2259 return Mnp->Poll (Mnp);\r
2260}\r
2261\r
772db4bb 2262/**\r
2263 Decrease the life of the transmitted packets. If it is\r
2264 decreased to zero, cancel the packet. This function is\r
96e1079f 2265 called by Ip4PacketTimerTicking which time out both the\r
772db4bb 2266 received-but-not-delivered and transmitted-but-not-recycle\r
2267 packets.\r
2268\r
3e8c18da 2269 @param[in] Map The IP4 child's transmit map.\r
1f6729ff 2270 @param[in] Item Current transmitted packet.\r
3e8c18da 2271 @param[in] Context Not used.\r
772db4bb 2272\r
1f6729ff 2273 @retval EFI_SUCCESS Always returns EFI_SUCCESS.\r
772db4bb 2274\r
2275**/\r
2276EFI_STATUS\r
e798cd87 2277EFIAPI\r
772db4bb 2278Ip4SentPacketTicking (\r
2279 IN NET_MAP *Map,\r
2280 IN NET_MAP_ITEM *Item,\r
2281 IN VOID *Context\r
2282 )\r
2283{\r
2284 IP4_TXTOKEN_WRAP *Wrap;\r
2285\r
2286 Wrap = (IP4_TXTOKEN_WRAP *) Item->Value;\r
2287 ASSERT (Wrap != NULL);\r
2288\r
2289 if ((Wrap->Life > 0) && (--Wrap->Life == 0)) {\r
2290 Ip4CancelPacket (Wrap->IpInstance->Interface, Wrap->Packet, EFI_ABORTED);\r
2291 }\r
2292\r
2293 return EFI_SUCCESS;\r
2294}\r
2295\r
2296\r
2297/**\r
2298 The heart beat timer of IP4 service instance. It times out\r
2299 all of its IP4 children's received-but-not-delivered and\r
2300 transmitted-but-not-recycle packets, and provides time input\r
2301 for its IGMP protocol.\r
2302\r
3e8c18da 2303 @param[in] Event The IP4 service instance's heart beat timer.\r
2304 @param[in] Context The IP4 service instance.\r
772db4bb 2305\r
2306**/\r
2307VOID\r
2308EFIAPI\r
2309Ip4TimerTicking (\r
2310 IN EFI_EVENT Event,\r
2311 IN VOID *Context\r
2312 )\r
2313{\r
2314 IP4_SERVICE *IpSb;\r
2315\r
2316 IpSb = (IP4_SERVICE *) Context;\r
2317 NET_CHECK_SIGNATURE (IpSb, IP4_SERVICE_SIGNATURE);\r
2318\r
2319 Ip4PacketTimerTicking (IpSb);\r
2320 Ip4IgmpTicking (IpSb);\r
2321}\r