]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Network/Ip4Dxe/Ip4Impl.c
MdeModulePkg/Ip4Dxe: return error on memory allocate failure instead of ASSERT.
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Ip4Dxe / Ip4Impl.c
CommitLineData
772db4bb 1/** @file\r
2\r
f1222593 3Copyright (c) 2005 - 2017, 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
4bb89650 553 IpInstance->InDestroy = FALSE;\r
772db4bb 554 IpInstance->Service = IpSb;\r
555\r
e48e37fc 556 InitializeListHead (&IpInstance->Link);\r
772db4bb 557 NetMapInit (&IpInstance->RxTokens);\r
558 NetMapInit (&IpInstance->TxTokens);\r
e48e37fc 559 InitializeListHead (&IpInstance->Received);\r
560 InitializeListHead (&IpInstance->Delivered);\r
561 InitializeListHead (&IpInstance->AddrLink);\r
772db4bb 562\r
e48e37fc 563 EfiInitializeLock (&IpInstance->RecycleLock, TPL_NOTIFY);\r
772db4bb 564}\r
565\r
566\r
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
ad1484c3
JW
599 EFI_IP4_CONFIG2_PROTOCOL *Ip4Config2;\r
600 EFI_IP4_CONFIG2_POLICY Policy;\r
772db4bb 601\r
602 IpSb = IpInstance->Service;\r
603\r
ad1484c3
JW
604 Ip4Config2 = NULL;\r
605\r
772db4bb 606 //\r
607 // User is changing packet filters. It must be stopped\r
608 // before the station address can be changed.\r
609 //\r
610 if (IpInstance->State == IP4_STATE_CONFIGED) {\r
611 //\r
612 // Cancel all the pending transmit/receive from upper layer\r
613 //\r
614 Status = Ip4Cancel (IpInstance, NULL);\r
615\r
616 if (EFI_ERROR (Status)) {\r
617 return EFI_DEVICE_ERROR;\r
618 }\r
619\r
687a2e5f 620 CopyMem (&IpInstance->ConfigData, Config, sizeof (IpInstance->ConfigData));\r
772db4bb 621 return EFI_SUCCESS;\r
622 }\r
623\r
624 //\r
625 // Configure a fresh IP4 protocol instance. Create a route table.\r
626 // Each IP child has its own route table, which may point to the\r
627 // default table if it is using default address.\r
628 //\r
629 Status = EFI_OUT_OF_RESOURCES;\r
630 IpInstance->RouteTable = Ip4CreateRouteTable ();\r
631\r
632 if (IpInstance->RouteTable == NULL) {\r
633 return Status;\r
634 }\r
635\r
636 //\r
637 // Set up the interface.\r
638 //\r
e48e37fc 639 CopyMem (&Ip, &Config->StationAddress, sizeof (IP4_ADDR));\r
640 CopyMem (&Netmask, &Config->SubnetMask, sizeof (IP4_ADDR));\r
772db4bb 641\r
642 Ip = NTOHL (Ip);\r
643 Netmask = NTOHL (Netmask);\r
644\r
645 if (!Config->UseDefaultAddress) {\r
646 //\r
647 // Find whether there is already an interface with the same\r
648 // station address. All the instances with the same station\r
649 // address shares one interface.\r
650 //\r
651 IpIf = Ip4FindStationAddress (IpSb, Ip, Netmask);\r
652\r
653 if (IpIf != NULL) {\r
654 NET_GET_REF (IpIf);\r
655\r
656 } else {\r
657 IpIf = Ip4CreateInterface (IpSb->Mnp, IpSb->Controller, IpSb->Image);\r
658\r
659 if (IpIf == NULL) {\r
660 goto ON_ERROR;\r
661 }\r
662\r
663 Status = Ip4SetAddress (IpIf, Ip, Netmask);\r
664\r
665 if (EFI_ERROR (Status)) {\r
666 Status = EFI_DEVICE_ERROR;\r
667 Ip4FreeInterface (IpIf, IpInstance);\r
668 goto ON_ERROR;\r
669 }\r
670\r
e48e37fc 671 InsertTailList (&IpSb->Interfaces, &IpIf->Link);\r
772db4bb 672 }\r
673\r
674 //\r
675 // Add a route to this connected network in the route table\r
676 //\r
677 Ip4AddRoute (IpInstance->RouteTable, Ip, Netmask, IP4_ALLZERO_ADDRESS);\r
678\r
679 } else {\r
680 //\r
7648748e 681 // Use the default address. Check the state.\r
772db4bb 682 //\r
683 if (IpSb->State == IP4_SERVICE_UNSTARTED) {\r
ad1484c3
JW
684 //\r
685 // Trigger the EFI_IP4_CONFIG2_PROTOCOL to retrieve the \r
686 // default IPv4 address if it is not available yet.\r
687 //\r
688 Policy = IpSb->Ip4Config2Instance.Policy;\r
689 if (Policy != Ip4Config2PolicyDhcp) {\r
690 Ip4Config2 = &IpSb->Ip4Config2Instance.Ip4Config2;\r
691 Policy = Ip4Config2PolicyDhcp;\r
692 Status= Ip4Config2->SetData (\r
693 Ip4Config2,\r
694 Ip4Config2DataTypePolicy,\r
695 sizeof (EFI_IP4_CONFIG2_POLICY),\r
696 &Policy\r
697 );\r
698 if (EFI_ERROR (Status)) {\r
699 goto ON_ERROR;\r
700 }\r
923473ed 701 }\r
772db4bb 702 }\r
703\r
704 IpIf = IpSb->DefaultInterface;\r
705 NET_GET_REF (IpSb->DefaultInterface);\r
706\r
707 //\r
708 // If default address is used, so is the default route table.\r
709 // Any route set by the instance has the precedence over the\r
710 // routes in the default route table. Link the default table\r
711 // after the instance's table. Routing will search the local\r
712 // table first.\r
713 //\r
714 NET_GET_REF (IpSb->DefaultRouteTable);\r
715 IpInstance->RouteTable->Next = IpSb->DefaultRouteTable;\r
716 }\r
717\r
718 IpInstance->Interface = IpIf;\r
216f7970 719 if (IpIf->Arp != NULL) {\r
720 Arp = NULL;\r
721 Status = gBS->OpenProtocol (\r
722 IpIf->ArpHandle,\r
723 &gEfiArpProtocolGuid,\r
724 (VOID **) &Arp,\r
725 gIp4DriverBinding.DriverBindingHandle,\r
726 IpInstance->Handle,\r
727 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER\r
728 );\r
729 if (EFI_ERROR (Status)) {\r
7648748e 730 goto ON_ERROR;\r
216f7970 731 }\r
732 }\r
e48e37fc 733 InsertTailList (&IpIf->IpInstances, &IpInstance->AddrLink);\r
772db4bb 734\r
687a2e5f 735 CopyMem (&IpInstance->ConfigData, Config, sizeof (IpInstance->ConfigData));\r
772db4bb 736 IpInstance->State = IP4_STATE_CONFIGED;\r
737\r
738 //\r
739 // Although EFI_NO_MAPPING is an error code, the IP child has been\r
740 // successfully configured and doesn't need reconfiguration when\r
741 // default address is acquired.\r
742 //\r
743 if (Config->UseDefaultAddress && IP4_NO_MAPPING (IpInstance)) {\r
744 return EFI_NO_MAPPING;\r
745 }\r
746\r
747 return EFI_SUCCESS;\r
748\r
749ON_ERROR:\r
750 Ip4FreeRouteTable (IpInstance->RouteTable);\r
751 IpInstance->RouteTable = NULL;\r
752 return Status;\r
753}\r
754\r
755\r
756/**\r
757 Clean up the IP4 child, release all the resources used by it.\r
758\r
3e8c18da 759 @param[in] IpInstance The IP4 child to clean up.\r
772db4bb 760\r
1f6729ff 761 @retval EFI_SUCCESS The IP4 child is cleaned up.\r
762 @retval EFI_DEVICE_ERROR Some resources failed to be released.\r
772db4bb 763\r
764**/\r
765EFI_STATUS\r
766Ip4CleanProtocol (\r
767 IN IP4_PROTOCOL *IpInstance\r
768 )\r
769{\r
770 if (EFI_ERROR (Ip4Cancel (IpInstance, NULL))) {\r
771 return EFI_DEVICE_ERROR;\r
772 }\r
773\r
774 if (EFI_ERROR (Ip4Groups (IpInstance, FALSE, NULL))) {\r
775 return EFI_DEVICE_ERROR;\r
776 }\r
777\r
778 //\r
779 // Some packets haven't been recycled. It is because either the\r
780 // user forgets to recycle the packets, or because the callback\r
781 // hasn't been called. Just leave it alone.\r
782 //\r
e48e37fc 783 if (!IsListEmpty (&IpInstance->Delivered)) {\r
772db4bb 784 ;\r
785 }\r
786\r
787 if (IpInstance->Interface != NULL) {\r
e48e37fc 788 RemoveEntryList (&IpInstance->AddrLink);\r
216f7970 789 if (IpInstance->Interface->Arp != NULL) {\r
790 gBS->CloseProtocol (\r
791 IpInstance->Interface->ArpHandle,\r
792 &gEfiArpProtocolGuid,\r
793 gIp4DriverBinding.DriverBindingHandle,\r
794 IpInstance->Handle\r
795 );\r
796 }\r
772db4bb 797 Ip4FreeInterface (IpInstance->Interface, IpInstance);\r
798 IpInstance->Interface = NULL;\r
799 }\r
800\r
801 if (IpInstance->RouteTable != NULL) {\r
802 if (IpInstance->RouteTable->Next != NULL) {\r
803 Ip4FreeRouteTable (IpInstance->RouteTable->Next);\r
804 }\r
805\r
806 Ip4FreeRouteTable (IpInstance->RouteTable);\r
807 IpInstance->RouteTable = NULL;\r
808 }\r
809\r
810 if (IpInstance->EfiRouteTable != NULL) {\r
766c7483 811 FreePool (IpInstance->EfiRouteTable);\r
772db4bb 812 IpInstance->EfiRouteTable = NULL;\r
813 IpInstance->EfiRouteCount = 0;\r
814 }\r
815\r
816 if (IpInstance->Groups != NULL) {\r
766c7483 817 FreePool (IpInstance->Groups);\r
772db4bb 818 IpInstance->Groups = NULL;\r
819 IpInstance->GroupCount = 0;\r
820 }\r
821\r
822 NetMapClean (&IpInstance->TxTokens);\r
823\r
824 NetMapClean (&IpInstance->RxTokens);\r
825\r
826 return EFI_SUCCESS;\r
827}\r
828\r
829\r
772db4bb 830/**\r
5405e9a6 831 Assigns an IPv4 address and subnet mask to this EFI IPv4 Protocol driver instance.\r
e2851998 832\r
5405e9a6 833 The Configure() function is used to set, change, or reset the operational\r
834 parameters and filter settings for this EFI IPv4 Protocol instance. Until these\r
835 parameters have been set, no network traffic can be sent or received by this\r
836 instance. Once the parameters have been reset (by calling this function with\r
837 IpConfigData set to NULL), no more traffic can be sent or received until these\r
838 parameters have been set again. Each EFI IPv4 Protocol instance can be started\r
839 and stopped independently of each other by enabling or disabling their receive\r
840 filter settings with the Configure() function.\r
e2851998 841\r
5405e9a6 842 When IpConfigData.UseDefaultAddress is set to FALSE, the new station address will\r
843 be appended as an alias address into the addresses list in the EFI IPv4 Protocol\r
844 driver. While set to TRUE, Configure() will trigger the EFI_IP4_CONFIG_PROTOCOL\r
845 to retrieve the default IPv4 address if it is not available yet. Clients could\r
846 frequently call GetModeData() to check the status to ensure that the default IPv4\r
847 address is ready.\r
e2851998 848\r
5405e9a6 849 If operational parameters are reset or changed, any pending transmit and receive\r
850 requests will be cancelled. Their completion token status will be set to EFI_ABORTED\r
851 and their events will be signaled.\r
852\r
3e8c18da 853 @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.\r
854 @param[in] IpConfigData Pointer to the EFI IPv4 Protocol configuration data structure.\r
5405e9a6 855\r
856 @retval EFI_SUCCESS The driver instance was successfully opened.\r
857 @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP,\r
858 RARP, etc.) is not finished yet.\r
859 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:\r
860 @retval EFI_UNSUPPORTED One or more of the following conditions is TRUE:\r
861 A configuration protocol (DHCP, BOOTP, RARP, etc.) could\r
862 not be located when clients choose to use the default IPv4\r
863 address. This EFI IPv4 Protocol implementation does not\r
864 support this requested filter or timeout setting.\r
865 @retval EFI_OUT_OF_RESOURCES The EFI IPv4 Protocol driver instance data could not be allocated.\r
866 @retval EFI_ALREADY_STARTED The interface is already open and must be stopped before the\r
867 IPv4 address or subnet mask can be changed. The interface must\r
868 also be stopped when switching to/from raw packet mode.\r
869 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. The EFI IPv4\r
870 Protocol driver instance is not opened.\r
772db4bb 871\r
872**/\r
772db4bb 873EFI_STATUS\r
874EFIAPI\r
875EfiIp4Configure (\r
876 IN EFI_IP4_PROTOCOL *This,\r
877 IN EFI_IP4_CONFIG_DATA *IpConfigData OPTIONAL\r
878 )\r
879{\r
880 IP4_PROTOCOL *IpInstance;\r
881 EFI_IP4_CONFIG_DATA *Current;\r
882 EFI_TPL OldTpl;\r
883 EFI_STATUS Status;\r
884 BOOLEAN AddrOk;\r
885 IP4_ADDR IpAddress;\r
886 IP4_ADDR SubnetMask;\r
887\r
888 //\r
889 // First, validate the parameters\r
890 //\r
891 if (This == NULL) {\r
892 return EFI_INVALID_PARAMETER;\r
893 }\r
894\r
895 IpInstance = IP4_INSTANCE_FROM_PROTOCOL (This);\r
e48e37fc 896 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
772db4bb 897\r
898 //\r
899 // Validate the configuration first.\r
900 //\r
901 if (IpConfigData != NULL) {\r
772db4bb 902\r
e48e37fc 903 CopyMem (&IpAddress, &IpConfigData->StationAddress, sizeof (IP4_ADDR));\r
904 CopyMem (&SubnetMask, &IpConfigData->SubnetMask, sizeof (IP4_ADDR));\r
772db4bb 905\r
906 IpAddress = NTOHL (IpAddress);\r
907 SubnetMask = NTOHL (SubnetMask);\r
908\r
909 //\r
910 // Check whether the station address is a valid unicast address\r
911 //\r
912 if (!IpConfigData->UseDefaultAddress) {\r
913 AddrOk = Ip4StationAddressValid (IpAddress, SubnetMask);\r
914\r
915 if (!AddrOk) {\r
916 Status = EFI_INVALID_PARAMETER;\r
917 goto ON_EXIT;\r
918 }\r
919 }\r
920\r
921 //\r
922 // User can only update packet filters when already configured.\r
923 // If it wants to change the station address, it must configure(NULL)\r
924 // the instance first.\r
925 //\r
926 if (IpInstance->State == IP4_STATE_CONFIGED) {\r
927 Current = &IpInstance->ConfigData;\r
928\r
929 if (Current->UseDefaultAddress != IpConfigData->UseDefaultAddress) {\r
930 Status = EFI_ALREADY_STARTED;\r
931 goto ON_EXIT;\r
932 }\r
933\r
934 if (!Current->UseDefaultAddress &&\r
84b5c78e 935 (!EFI_IP4_EQUAL (&Current->StationAddress, &IpConfigData->StationAddress) ||\r
936 !EFI_IP4_EQUAL (&Current->SubnetMask, &IpConfigData->SubnetMask))) {\r
772db4bb 937 Status = EFI_ALREADY_STARTED;\r
938 goto ON_EXIT;\r
939 }\r
940\r
941 if (Current->UseDefaultAddress && IP4_NO_MAPPING (IpInstance)) {\r
ae0a0b06 942 Status = EFI_NO_MAPPING;\r
943 goto ON_EXIT;\r
772db4bb 944 }\r
945 }\r
946 }\r
947\r
948 //\r
949 // Configure the instance or clean it up.\r
950 //\r
951 if (IpConfigData != NULL) {\r
952 Status = Ip4ConfigProtocol (IpInstance, IpConfigData);\r
953 } else {\r
954 Status = Ip4CleanProtocol (IpInstance);\r
955\r
956 //\r
4bb89650 957 // Consider the following valid sequence: Mnp is unloaded-->Ip Stopped-->Udp Stopped,\r
772db4bb 958 // Configure (ThisIp, NULL). If the state is changed to UNCONFIGED,\r
959 // the unload fails miserably.\r
960 //\r
961 if (IpInstance->State == IP4_STATE_CONFIGED) {\r
962 IpInstance->State = IP4_STATE_UNCONFIGED;\r
963 }\r
964 }\r
965\r
966 //\r
967 // Update the MNP's configure data. Ip4ServiceConfigMnp will check\r
968 // whether it is necessary to reconfigure the MNP.\r
969 //\r
970 Ip4ServiceConfigMnp (IpInstance->Service, FALSE);\r
971\r
772db4bb 972ON_EXIT:\r
e48e37fc 973 gBS->RestoreTPL (OldTpl);\r
772db4bb 974 return Status;\r
975\r
976}\r
977\r
978\r
979/**\r
980 Change the IP4 child's multicast setting. The caller\r
981 should make sure that the parameters is valid.\r
982\r
3e8c18da 983 @param[in] IpInstance The IP4 child to change the setting.\r
1f6729ff 984 @param[in] JoinFlag TRUE to join the group, otherwise leave it.\r
985 @param[in] GroupAddress The target group address.\r
772db4bb 986\r
1f6729ff 987 @retval EFI_ALREADY_STARTED Want to join the group, but already a member of it.\r
772db4bb 988 @retval EFI_OUT_OF_RESOURCES Failed to allocate some resources.\r
1f6729ff 989 @retval EFI_DEVICE_ERROR Failed to set the group configuraton.\r
772db4bb 990 @retval EFI_SUCCESS Successfully updated the group setting.\r
991 @retval EFI_NOT_FOUND Try to leave the group which it isn't a member.\r
992\r
993**/\r
994EFI_STATUS\r
995Ip4Groups (\r
996 IN IP4_PROTOCOL *IpInstance,\r
997 IN BOOLEAN JoinFlag,\r
998 IN EFI_IPv4_ADDRESS *GroupAddress OPTIONAL\r
999 )\r
1000{\r
1001 IP4_ADDR *Members;\r
1002 IP4_ADDR Group;\r
1003 UINT32 Index;\r
1004\r
1005 //\r
1006 // Add it to the instance's Groups, and join the group by IGMP.\r
1007 // IpInstance->Groups is in network byte order. IGMP operates in\r
1008 // host byte order\r
1009 //\r
1010 if (JoinFlag) {\r
e2851998 1011 //\r
894d038a 1012 // When JoinFlag is TRUE, GroupAddress shouldn't be NULL.\r
1013 //\r
1014 ASSERT (GroupAddress != NULL);\r
e48e37fc 1015 CopyMem (&Group, GroupAddress, sizeof (IP4_ADDR));\r
772db4bb 1016\r
1017 for (Index = 0; Index < IpInstance->GroupCount; Index++) {\r
1018 if (IpInstance->Groups[Index] == Group) {\r
1019 return EFI_ALREADY_STARTED;\r
1020 }\r
1021 }\r
1022\r
1023 Members = Ip4CombineGroups (IpInstance->Groups, IpInstance->GroupCount, Group);\r
1024\r
1025 if (Members == NULL) {\r
1026 return EFI_OUT_OF_RESOURCES;\r
1027 }\r
1028\r
1029 if (EFI_ERROR (Ip4JoinGroup (IpInstance, NTOHL (Group)))) {\r
766c7483 1030 FreePool (Members);\r
772db4bb 1031 return EFI_DEVICE_ERROR;\r
1032 }\r
1033\r
1034 if (IpInstance->Groups != NULL) {\r
766c7483 1035 FreePool (IpInstance->Groups);\r
772db4bb 1036 }\r
1037\r
1038 IpInstance->Groups = Members;\r
1039 IpInstance->GroupCount++;\r
1040\r
1041 return EFI_SUCCESS;\r
1042 }\r
1043\r
1044 //\r
1045 // Leave the group. Leave all the groups if GroupAddress is NULL.\r
1046 // Must iterate from the end to the beginning because the GroupCount\r
1047 // is decreamented each time an address is removed..\r
1048 //\r
1049 for (Index = IpInstance->GroupCount; Index > 0 ; Index--) {\r
1050 Group = IpInstance->Groups[Index - 1];\r
1051\r
84b5c78e 1052 if ((GroupAddress == NULL) || EFI_IP4_EQUAL (&Group, GroupAddress)) {\r
772db4bb 1053 if (EFI_ERROR (Ip4LeaveGroup (IpInstance, NTOHL (Group)))) {\r
1054 return EFI_DEVICE_ERROR;\r
1055 }\r
1056\r
1057 Ip4RemoveGroupAddr (IpInstance->Groups, IpInstance->GroupCount, Group);\r
1058 IpInstance->GroupCount--;\r
1059\r
1060 if (IpInstance->GroupCount == 0) {\r
1061 ASSERT (Index == 1);\r
1062\r
766c7483 1063 FreePool (IpInstance->Groups);\r
772db4bb 1064 IpInstance->Groups = NULL;\r
1065 }\r
1066\r
1067 if (GroupAddress != NULL) {\r
1068 return EFI_SUCCESS;\r
1069 }\r
1070 }\r
1071 }\r
1072\r
1073 return ((GroupAddress != NULL) ? EFI_NOT_FOUND : EFI_SUCCESS);\r
1074}\r
1075\r
1076\r
1077/**\r
5405e9a6 1078 Joins and leaves multicast groups.\r
e2851998 1079\r
5405e9a6 1080 The Groups() function is used to join and leave multicast group sessions. Joining\r
1081 a group will enable reception of matching multicast packets. Leaving a group will\r
1082 disable the multicast packet reception.\r
e2851998 1083\r
5405e9a6 1084 If JoinFlag is FALSE and GroupAddress is NULL, all joined groups will be left.\r
1085\r
3e8c18da 1086 @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.\r
1087 @param[in] JoinFlag Set to TRUE to join the multicast group session and FALSE to leave.\r
1088 @param[in] GroupAddress Pointer to the IPv4 multicast address.\r
5405e9a6 1089\r
1090 @retval EFI_SUCCESS The operation completed successfully.\r
1091 @retval EFI_INVALID_PARAMETER One or more of the following is TRUE:\r
1092 - This is NULL.\r
1093 - JoinFlag is TRUE and GroupAddress is NULL.\r
1094 - GroupAddress is not NULL and *GroupAddress is\r
1095 not a multicast IPv4 address.\r
1096 @retval EFI_NOT_STARTED This instance has not been started.\r
1097 @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP,\r
1098 RARP, etc.) is not finished yet.\r
1099 @retval EFI_OUT_OF_RESOURCES System resources could not be allocated.\r
1100 @retval EFI_UNSUPPORTED This EFI IPv4 Protocol implementation does not support multicast groups.\r
1101 @retval EFI_ALREADY_STARTED The group address is already in the group table (when\r
1102 JoinFlag is TRUE).\r
1103 @retval EFI_NOT_FOUND The group address is not in the group table (when JoinFlag is FALSE).\r
1104 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.\r
772db4bb 1105\r
1106**/\r
772db4bb 1107EFI_STATUS\r
1108EFIAPI\r
1109EfiIp4Groups (\r
1110 IN EFI_IP4_PROTOCOL *This,\r
1111 IN BOOLEAN JoinFlag,\r
1112 IN EFI_IPv4_ADDRESS *GroupAddress OPTIONAL\r
1113 )\r
1114{\r
1115 IP4_PROTOCOL *IpInstance;\r
1116 EFI_STATUS Status;\r
1117 EFI_TPL OldTpl;\r
1118 IP4_ADDR McastIp;\r
1119\r
1120 if ((This == NULL) || (JoinFlag && (GroupAddress == NULL))) {\r
1121 return EFI_INVALID_PARAMETER;\r
1122 }\r
1123\r
1124 if (GroupAddress != NULL) {\r
e48e37fc 1125 CopyMem (&McastIp, GroupAddress, sizeof (IP4_ADDR));\r
772db4bb 1126\r
1127 if (!IP4_IS_MULTICAST (NTOHL (McastIp))) {\r
1128 return EFI_INVALID_PARAMETER;\r
1129 }\r
1130 }\r
1131\r
1132 IpInstance = IP4_INSTANCE_FROM_PROTOCOL (This);\r
e48e37fc 1133 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
772db4bb 1134\r
1135 if (IpInstance->State != IP4_STATE_CONFIGED) {\r
1136 Status = EFI_NOT_STARTED;\r
1137 goto ON_EXIT;\r
1138 }\r
1139\r
1140 if (IpInstance->ConfigData.UseDefaultAddress && IP4_NO_MAPPING (IpInstance)) {\r
1141 Status = EFI_NO_MAPPING;\r
1142 goto ON_EXIT;\r
1143 }\r
1144\r
1145 Status = Ip4Groups (IpInstance, JoinFlag, GroupAddress);\r
1146\r
1147ON_EXIT:\r
e48e37fc 1148 gBS->RestoreTPL (OldTpl);\r
772db4bb 1149 return Status;\r
1150}\r
1151\r
1152\r
1153/**\r
5405e9a6 1154 Adds and deletes routing table entries.\r
1155\r
1156 The Routes() function adds a route to or deletes a route from the routing table.\r
e2851998 1157\r
5405e9a6 1158 Routes are determined by comparing the SubnetAddress with the destination IPv4\r
1159 address arithmetically AND-ed with the SubnetMask. The gateway address must be\r
1160 on the same subnet as the configured station address.\r
e2851998 1161\r
5405e9a6 1162 The default route is added with SubnetAddress and SubnetMask both set to 0.0.0.0.\r
1163 The default route matches all destination IPv4 addresses that do not match any\r
1164 other routes.\r
e2851998 1165\r
5405e9a6 1166 A GatewayAddress that is zero is a nonroute. Packets are sent to the destination\r
1167 IP address if it can be found in the ARP cache or on the local subnet. One automatic\r
1168 nonroute entry will be inserted into the routing table for outgoing packets that\r
1169 are addressed to a local subnet (gateway address of 0.0.0.0).\r
e2851998 1170\r
5405e9a6 1171 Each EFI IPv4 Protocol instance has its own independent routing table. Those EFI\r
1172 IPv4 Protocol instances that use the default IPv4 address will also have copies\r
1173 of the routing table that was provided by the EFI_IP4_CONFIG_PROTOCOL, and these\r
1174 copies will be updated whenever the EIF IPv4 Protocol driver reconfigures its\r
1175 instances. As a result, client modification to the routing table will be lost.\r
1176\r
3e8c18da 1177 @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.\r
1178 @param[in] DeleteRoute Set to TRUE to delete this route from the routing table. Set to\r
1179 FALSE to add this route to the routing table. SubnetAddress\r
1180 and SubnetMask are used as the key to each route entry.\r
1181 @param[in] SubnetAddress The address of the subnet that needs to be routed.\r
1182 @param[in] SubnetMask The subnet mask of SubnetAddress.\r
1183 @param[in] GatewayAddress The unicast gateway IPv4 address for this route.\r
5405e9a6 1184\r
1185 @retval EFI_SUCCESS The operation completed successfully.\r
1186 @retval EFI_NOT_STARTED The driver instance has not been started.\r
1187 @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP,\r
1188 RARP, etc.) is not finished yet.\r
1189 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:\r
1190 - This is NULL.\r
1191 - SubnetAddress is NULL.\r
1192 - SubnetMask is NULL.\r
1193 - GatewayAddress is NULL.\r
1194 - *SubnetAddress is not a valid subnet address.\r
1195 - *SubnetMask is not a valid subnet mask.\r
1196 - *GatewayAddress is not a valid unicast IPv4 address.\r
1197 @retval EFI_OUT_OF_RESOURCES Could not add the entry to the routing table.\r
1198 @retval EFI_NOT_FOUND This route is not in the routing table (when DeleteRoute is TRUE).\r
1199 @retval EFI_ACCESS_DENIED The route is already defined in the routing table (when\r
1200 DeleteRoute is FALSE).\r
e2851998 1201\r
772db4bb 1202**/\r
772db4bb 1203EFI_STATUS\r
1204EFIAPI\r
1205EfiIp4Routes (\r
1206 IN EFI_IP4_PROTOCOL *This,\r
1207 IN BOOLEAN DeleteRoute,\r
1208 IN EFI_IPv4_ADDRESS *SubnetAddress,\r
1209 IN EFI_IPv4_ADDRESS *SubnetMask,\r
1210 IN EFI_IPv4_ADDRESS *GatewayAddress\r
1211 )\r
1212{\r
1213 IP4_PROTOCOL *IpInstance;\r
1214 IP4_INTERFACE *IpIf;\r
1215 IP4_ADDR Dest;\r
1216 IP4_ADDR Netmask;\r
1217 IP4_ADDR Nexthop;\r
1218 EFI_STATUS Status;\r
1219 EFI_TPL OldTpl;\r
1220\r
1221 //\r
1222 // First, validate the parameters\r
1223 //\r
1224 if ((This == NULL) || (SubnetAddress == NULL) ||\r
1225 (SubnetMask == NULL) || (GatewayAddress == NULL)) {\r
1226 return EFI_INVALID_PARAMETER;\r
1227 }\r
1228\r
1229 IpInstance = IP4_INSTANCE_FROM_PROTOCOL (This);\r
e48e37fc 1230 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
772db4bb 1231\r
1232 if (IpInstance->State != IP4_STATE_CONFIGED) {\r
1233 Status = EFI_NOT_STARTED;\r
1234 goto ON_EXIT;\r
1235 }\r
1236\r
1237 if (IpInstance->ConfigData.UseDefaultAddress && IP4_NO_MAPPING (IpInstance)) {\r
1238 Status = EFI_NO_MAPPING;\r
1239 goto ON_EXIT;\r
1240 }\r
1241\r
e48e37fc 1242 CopyMem (&Dest, SubnetAddress, sizeof (IP4_ADDR));\r
1243 CopyMem (&Netmask, SubnetMask, sizeof (IP4_ADDR));\r
1244 CopyMem (&Nexthop, GatewayAddress, sizeof (IP4_ADDR));\r
772db4bb 1245\r
1246 Dest = NTOHL (Dest);\r
1247 Netmask = NTOHL (Netmask);\r
1248 Nexthop = NTOHL (Nexthop);\r
1249\r
1250 IpIf = IpInstance->Interface;\r
1251\r
1252 if (!IP4_IS_VALID_NETMASK (Netmask)) {\r
1253 Status = EFI_INVALID_PARAMETER;\r
1254 goto ON_EXIT;\r
1255 }\r
1256\r
1257 //\r
1258 // the gateway address must be a unicast on the connected network if not zero.\r
1259 //\r
1260 if ((Nexthop != IP4_ALLZERO_ADDRESS) &&\r
1261 (!IP4_NET_EQUAL (Nexthop, IpIf->Ip, IpIf->SubnetMask) ||\r
1262 IP4_IS_BROADCAST (Ip4GetNetCast (Nexthop, IpIf)))) {\r
1263\r
1264 Status = EFI_INVALID_PARAMETER;\r
1265 goto ON_EXIT;\r
1266 }\r
1267\r
1268 if (DeleteRoute) {\r
1269 Status = Ip4DelRoute (IpInstance->RouteTable, Dest, Netmask, Nexthop);\r
1270 } else {\r
1271 Status = Ip4AddRoute (IpInstance->RouteTable, Dest, Netmask, Nexthop);\r
1272 }\r
1273\r
1274ON_EXIT:\r
e48e37fc 1275 gBS->RestoreTPL (OldTpl);\r
772db4bb 1276 return Status;\r
1277}\r
1278\r
1279\r
1280/**\r
1281 Check whether the user's token or event has already\r
5405e9a6 1282 been enqueued on IP4's list.\r
772db4bb 1283\r
3e8c18da 1284 @param[in] Map The container of either user's transmit or receive\r
1285 token.\r
1f6729ff 1286 @param[in] Item Current item to check against.\r
3e8c18da 1287 @param[in] Context The Token to check againist.\r
772db4bb 1288\r
1f6729ff 1289 @retval EFI_ACCESS_DENIED The token or event has already been enqueued in IP.\r
772db4bb 1290 @retval EFI_SUCCESS The current item isn't the same token/event as the\r
1291 context.\r
1292\r
1293**/\r
772db4bb 1294EFI_STATUS\r
e798cd87 1295EFIAPI\r
772db4bb 1296Ip4TokenExist (\r
1297 IN NET_MAP *Map,\r
1298 IN NET_MAP_ITEM *Item,\r
1299 IN VOID *Context\r
1300 )\r
1301{\r
1302 EFI_IP4_COMPLETION_TOKEN *Token;\r
1303 EFI_IP4_COMPLETION_TOKEN *TokenInItem;\r
1304\r
1305 Token = (EFI_IP4_COMPLETION_TOKEN *) Context;\r
1306 TokenInItem = (EFI_IP4_COMPLETION_TOKEN *) Item->Key;\r
1307\r
1308 if ((Token == TokenInItem) || (Token->Event == TokenInItem->Event)) {\r
1309 return EFI_ACCESS_DENIED;\r
1310 }\r
1311\r
1312 return EFI_SUCCESS;\r
1313}\r
1314\r
772db4bb 1315/**\r
1316 Validate the user's token against current station address.\r
1317\r
216f7970 1318 @param[in] Token User's token to validate.\r
1319 @param[in] IpIf The IP4 child's interface.\r
1320 @param[in] RawData Set to TRUE to send unformatted packets.\r
772db4bb 1321\r
216f7970 1322 @retval EFI_INVALID_PARAMETER Some parameters are invalid.\r
772db4bb 1323 @retval EFI_BAD_BUFFER_SIZE The user's option/data is too long.\r
216f7970 1324 @retval EFI_SUCCESS The token is valid.\r
772db4bb 1325\r
1326**/\r
772db4bb 1327EFI_STATUS\r
1328Ip4TxTokenValid (\r
1329 IN EFI_IP4_COMPLETION_TOKEN *Token,\r
216f7970 1330 IN IP4_INTERFACE *IpIf,\r
1331 IN BOOLEAN RawData\r
772db4bb 1332 )\r
1333{\r
1334 EFI_IP4_TRANSMIT_DATA *TxData;\r
1335 EFI_IP4_OVERRIDE_DATA *Override;\r
1336 IP4_ADDR Src;\r
1337 IP4_ADDR Gateway;\r
1338 UINT32 Offset;\r
1339 UINT32 Index;\r
1340 UINT32 HeadLen;\r
1341\r
1342 if ((Token == NULL) || (Token->Event == NULL) || (Token->Packet.TxData == NULL)) {\r
1343 return EFI_INVALID_PARAMETER;\r
1344 }\r
1345\r
1346 TxData = Token->Packet.TxData;\r
1347\r
1348 //\r
216f7970 1349 // Check the fragment table: no empty fragment, and length isn't bogus.\r
772db4bb 1350 //\r
1351 if ((TxData->TotalDataLength == 0) || (TxData->FragmentCount == 0)) {\r
1352 return EFI_INVALID_PARAMETER;\r
1353 }\r
1354\r
1355 Offset = TxData->TotalDataLength;\r
1356\r
216f7970 1357 if (Offset > IP4_MAX_PACKET_SIZE) {\r
1358 return EFI_BAD_BUFFER_SIZE;\r
1359 }\r
1360\r
772db4bb 1361 for (Index = 0; Index < TxData->FragmentCount; Index++) {\r
1362 if ((TxData->FragmentTable[Index].FragmentBuffer == NULL) ||\r
1363 (TxData->FragmentTable[Index].FragmentLength == 0)) {\r
1364\r
1365 return EFI_INVALID_PARAMETER;\r
1366 }\r
1367\r
1368 Offset -= TxData->FragmentTable[Index].FragmentLength;\r
1369 }\r
1370\r
1371 if (Offset != 0) {\r
1372 return EFI_INVALID_PARAMETER;\r
1373 }\r
1374\r
216f7970 1375 //\r
1376 // NOTE that OptionsLength/OptionsBuffer/OverrideData are ignored if RawData\r
1377 // is TRUE.\r
1378 //\r
1379 if (RawData) {\r
1380 return EFI_SUCCESS;\r
1381 }\r
1382\r
1383 //\r
1384 // Check the IP options: no more than 40 bytes and format is OK\r
1385 //\r
1386 if (TxData->OptionsLength != 0) {\r
1387 if ((TxData->OptionsLength > 40) || (TxData->OptionsBuffer == NULL)) {\r
1388 return EFI_INVALID_PARAMETER;\r
1389 }\r
1390\r
1391 if (!Ip4OptionIsValid (TxData->OptionsBuffer, TxData->OptionsLength, FALSE)) {\r
1392 return EFI_INVALID_PARAMETER;\r
1393 }\r
1394 }\r
1395\r
772db4bb 1396 //\r
1397 // Check the source and gateway: they must be a valid unicast.\r
1398 // Gateway must also be on the connected network.\r
1399 //\r
e2851998 1400 if (TxData->OverrideData != NULL) {\r
772db4bb 1401 Override = TxData->OverrideData;\r
1402\r
e48e37fc 1403 CopyMem (&Src, &Override->SourceAddress, sizeof (IP4_ADDR));\r
1404 CopyMem (&Gateway, &Override->GatewayAddress, sizeof (IP4_ADDR));\r
772db4bb 1405\r
1406 Src = NTOHL (Src);\r
1407 Gateway = NTOHL (Gateway);\r
1408\r
1409 if ((NetGetIpClass (Src) > IP4_ADDR_CLASSC) ||\r
1410 (Src == IP4_ALLONE_ADDRESS) ||\r
1411 IP4_IS_BROADCAST (Ip4GetNetCast (Src, IpIf))) {\r
1412\r
1413 return EFI_INVALID_PARAMETER;\r
1414 }\r
1415\r
1416 //\r
1417 // If gateway isn't zero, it must be a unicast address, and\r
1418 // on the connected network.\r
1419 //\r
1420 if ((Gateway != IP4_ALLZERO_ADDRESS) &&\r
1421 ((NetGetIpClass (Gateway) > IP4_ADDR_CLASSC) ||\r
1422 !IP4_NET_EQUAL (Gateway, IpIf->Ip, IpIf->SubnetMask) ||\r
1423 IP4_IS_BROADCAST (Ip4GetNetCast (Gateway, IpIf)))) {\r
1424\r
1425 return EFI_INVALID_PARAMETER;\r
1426 }\r
1427 }\r
1428\r
1429 //\r
1430 // Check the packet length: Head length and packet length all has a limit\r
1431 //\r
1432 HeadLen = sizeof (IP4_HEAD) + ((TxData->OptionsLength + 3) &~0x03);\r
1433\r
1434 if ((HeadLen > IP4_MAX_HEADLEN) ||\r
1435 (TxData->TotalDataLength + HeadLen > IP4_MAX_PACKET_SIZE)) {\r
1436\r
1437 return EFI_BAD_BUFFER_SIZE;\r
1438 }\r
1439\r
1440 return EFI_SUCCESS;\r
1441}\r
1442\r
1443\r
1444/**\r
1445 The callback function for the net buffer which wraps the user's\r
1446 transmit token. Although it seems this function is pretty simple,\r
1447 there are some subtle things.\r
1448 When user requests the IP to transmit a packet by passing it a\r
1449 token, the token is wrapped in an IP4_TXTOKEN_WRAP and the data\r
1450 is wrapped in an net buffer. the net buffer's Free function is\r
1451 set to Ip4FreeTxToken. The Token and token wrap are added to the\r
1452 IP child's TxToken map. Then the buffer is passed to Ip4Output for\r
1453 transmission. If something error happened before that, the buffer\r
1454 is freed, which in turn will free the token wrap. The wrap may\r
1455 have been added to the TxToken map or not, and the user's event\r
1456 shouldn't be fired because we are still in the EfiIp4Transmit. If\r
1457 the buffer has been sent by Ip4Output, it should be removed from\r
1458 the TxToken map and user's event signaled. The token wrap and buffer\r
1459 are bound together. Check the comments in Ip4Output for information\r
1460 about IP fragmentation.\r
1461\r
1f6729ff 1462 @param[in] Context The token's wrap.\r
772db4bb 1463\r
1464**/\r
772db4bb 1465VOID\r
e798cd87 1466EFIAPI\r
772db4bb 1467Ip4FreeTxToken (\r
1468 IN VOID *Context\r
1469 )\r
1470{\r
1471 IP4_TXTOKEN_WRAP *Wrap;\r
1472 NET_MAP_ITEM *Item;\r
1473\r
1474 Wrap = (IP4_TXTOKEN_WRAP *) Context;\r
1475\r
a1503a32 1476 //\r
1477 // Signal IpSecRecycleEvent to inform IPsec free the memory\r
1478 //\r
1479 if (Wrap->IpSecRecycleSignal != NULL) {\r
1480 gBS->SignalEvent (Wrap->IpSecRecycleSignal);\r
1481 }\r
1482\r
772db4bb 1483 //\r
1484 // Find the token in the instance's map. EfiIp4Transmit put the\r
1485 // token to the map. If that failed, NetMapFindKey will return NULL.\r
1486 //\r
1487 Item = NetMapFindKey (&Wrap->IpInstance->TxTokens, Wrap->Token);\r
1488\r
1489 if (Item != NULL) {\r
1490 NetMapRemoveItem (&Wrap->IpInstance->TxTokens, Item, NULL);\r
1491 }\r
1492\r
1493 if (Wrap->Sent) {\r
1494 gBS->SignalEvent (Wrap->Token->Event);\r
36ee91ca 1495\r
1496 //\r
1497 // Dispatch the DPC queued by the NotifyFunction of Token->Event.\r
1498 //\r
d8d26fb2 1499 DispatchDpc ();\r
772db4bb 1500 }\r
1501\r
766c7483 1502 FreePool (Wrap);\r
772db4bb 1503}\r
1504\r
1505\r
1506/**\r
1507 The callback function to Ip4Output to update the transmit status.\r
1508\r
1509 @param Ip4Instance The Ip4Instance that request the transmit.\r
1f6729ff 1510 @param Packet The user's transmit request.\r
1511 @param IoStatus The result of the transmission.\r
1512 @param Flag Not used during transmission.\r
772db4bb 1513 @param Context The token's wrap.\r
1514\r
772db4bb 1515**/\r
772db4bb 1516VOID\r
1517Ip4OnPacketSent (\r
1518 IP4_PROTOCOL *Ip4Instance,\r
1519 NET_BUF *Packet,\r
1520 EFI_STATUS IoStatus,\r
1521 UINT32 Flag,\r
1522 VOID *Context\r
1523 )\r
1524{\r
1525 IP4_TXTOKEN_WRAP *Wrap;\r
1526\r
1527 //\r
1528 // This is the transmission request from upper layer,\r
1529 // not the IP4 driver itself.\r
1530 //\r
1531 ASSERT (Ip4Instance != NULL);\r
1532\r
1533 //\r
1534 // The first fragment of the packet has been sent. Update\r
1535 // the token's status. That is, if fragmented, the transmit's\r
1536 // status is the first fragment's status. The Wrap will be\r
1537 // release when all the fragments are release. Check the comments\r
1538 // in Ip4FreeTxToken and Ip4Output for information.\r
1539 //\r
1540 Wrap = (IP4_TXTOKEN_WRAP *) Context;\r
1541 Wrap->Token->Status = IoStatus;\r
1542\r
1543 NetbufFree (Wrap->Packet);\r
1544}\r
1545\r
1546\r
1547/**\r
5405e9a6 1548 Places outgoing data packets into the transmit queue.\r
1549\r
1550 The Transmit() function places a sending request in the transmit queue of this\r
1551 EFI IPv4 Protocol instance. Whenever the packet in the token is sent out or some\r
1552 errors occur, the event in the token will be signaled and the status is updated.\r
1553\r
3e8c18da 1554 @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.\r
1555 @param[in] Token Pointer to the transmit token.\r
5405e9a6 1556\r
1557 @retval EFI_SUCCESS The data has been queued for transmission.\r
1558 @retval EFI_NOT_STARTED This instance has not been started.\r
1559 @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP,\r
1560 RARP, etc.) is not finished yet.\r
1561 @retval EFI_INVALID_PARAMETER One or more pameters are invalid.\r
1562 @retval EFI_ACCESS_DENIED The transmit completion token with the same Token.Event\r
1563 was already in the transmit queue.\r
1564 @retval EFI_NOT_READY The completion token could not be queued because the transmit\r
e2851998 1565 queue is full.\r
5405e9a6 1566 @retval EFI_NOT_FOUND Not route is found to destination address.\r
1567 @retval EFI_OUT_OF_RESOURCES Could not queue the transmit data.\r
1568 @retval EFI_BUFFER_TOO_SMALL Token.Packet.TxData.TotalDataLength is too\r
1569 short to transmit.\r
1570 @retval EFI_BAD_BUFFER_SIZE The length of the IPv4 header + option length + total data length is\r
1571 greater than MTU (or greater than the maximum packet size if\r
1572 Token.Packet.TxData.OverrideData.\r
1f6729ff 1573 DoNotFragment is TRUE).\r
772db4bb 1574\r
1575**/\r
772db4bb 1576EFI_STATUS\r
1577EFIAPI\r
1578EfiIp4Transmit (\r
1579 IN EFI_IP4_PROTOCOL *This,\r
1580 IN EFI_IP4_COMPLETION_TOKEN *Token\r
1581 )\r
1582{\r
1583 IP4_SERVICE *IpSb;\r
1584 IP4_PROTOCOL *IpInstance;\r
1585 IP4_INTERFACE *IpIf;\r
1586 IP4_TXTOKEN_WRAP *Wrap;\r
1587 EFI_IP4_TRANSMIT_DATA *TxData;\r
1588 EFI_IP4_CONFIG_DATA *Config;\r
1589 EFI_IP4_OVERRIDE_DATA *Override;\r
1590 IP4_HEAD Head;\r
1591 IP4_ADDR GateWay;\r
1592 EFI_STATUS Status;\r
1593 EFI_TPL OldTpl;\r
1594 BOOLEAN DontFragment;\r
1595 UINT32 HeadLen;\r
216f7970 1596 UINT8 RawHdrLen;\r
1597 UINT32 OptionsLength;\r
1598 UINT8 *OptionsBuffer;\r
1599 VOID *FirstFragment;\r
772db4bb 1600\r
1601 if (This == NULL) {\r
1602 return EFI_INVALID_PARAMETER;\r
1603 }\r
1604\r
1605 IpInstance = IP4_INSTANCE_FROM_PROTOCOL (This);\r
1606\r
1607 if (IpInstance->State != IP4_STATE_CONFIGED) {\r
1608 return EFI_NOT_STARTED;\r
1609 }\r
1610\r
e48e37fc 1611 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
772db4bb 1612\r
1613 IpSb = IpInstance->Service;\r
1614 IpIf = IpInstance->Interface;\r
1615 Config = &IpInstance->ConfigData;\r
1616\r
1617 if (Config->UseDefaultAddress && IP4_NO_MAPPING (IpInstance)) {\r
1618 Status = EFI_NO_MAPPING;\r
1619 goto ON_EXIT;\r
1620 }\r
1621\r
1622 //\r
1623 // make sure that token is properly formated\r
1624 //\r
216f7970 1625 Status = Ip4TxTokenValid (Token, IpIf, Config->RawData);\r
772db4bb 1626\r
1627 if (EFI_ERROR (Status)) {\r
1628 goto ON_EXIT;\r
1629 }\r
1630\r
1631 //\r
1632 // Check whether the token or signal already existed.\r
1633 //\r
1634 if (EFI_ERROR (NetMapIterate (&IpInstance->TxTokens, Ip4TokenExist, Token))) {\r
1635 Status = EFI_ACCESS_DENIED;\r
1636 goto ON_EXIT;\r
1637 }\r
1638\r
1639 //\r
1640 // Build the IP header, need to fill in the Tos, TotalLen, Id,\r
1641 // fragment, Ttl, protocol, Src, and Dst.\r
1642 //\r
1643 TxData = Token->Packet.TxData;\r
1644\r
216f7970 1645 FirstFragment = NULL;\r
772db4bb 1646\r
216f7970 1647 if (Config->RawData) {\r
1648 //\r
1649 // When RawData is TRUE, first buffer in FragmentTable points to a raw\r
1650 // IPv4 fragment including IPv4 header and options.\r
1651 //\r
1652 FirstFragment = TxData->FragmentTable[0].FragmentBuffer;\r
1653 CopyMem (&RawHdrLen, FirstFragment, sizeof (UINT8));\r
772db4bb 1654\r
216f7970 1655 RawHdrLen = (UINT8) (RawHdrLen & 0x0f);\r
1656 if (RawHdrLen < 5) {\r
098e9f49
FS
1657 Status = EFI_INVALID_PARAMETER;\r
1658 goto ON_EXIT;\r
216f7970 1659 }\r
1660\r
1661 RawHdrLen = (UINT8) (RawHdrLen << 2);\r
1662 \r
1663 CopyMem (&Head, FirstFragment, IP4_MIN_HEADLEN);\r
1664\r
1665 Ip4NtohHead (&Head);\r
1666 HeadLen = 0;\r
1667 DontFragment = IP4_DO_NOT_FRAGMENT (Head.Fragment);\r
772db4bb 1668\r
216f7970 1669 if (!DontFragment) {\r
098e9f49
FS
1670 Status = EFI_INVALID_PARAMETER;\r
1671 goto ON_EXIT;\r
216f7970 1672 }\r
1673\r
1674 GateWay = IP4_ALLZERO_ADDRESS;\r
1675\r
1676 //\r
1677 // Get IPv4 options from first fragment.\r
1678 //\r
1679 if (RawHdrLen == IP4_MIN_HEADLEN) {\r
1680 OptionsLength = 0;\r
1681 OptionsBuffer = NULL;\r
1682 } else {\r
1683 OptionsLength = RawHdrLen - IP4_MIN_HEADLEN;\r
1684 OptionsBuffer = (UINT8 *) FirstFragment + IP4_MIN_HEADLEN;\r
1685 }\r
1686\r
1687 //\r
1688 // Trim off IPv4 header and options from first fragment.\r
1689 //\r
1690 TxData->FragmentTable[0].FragmentBuffer = (UINT8 *) FirstFragment + RawHdrLen;\r
1691 TxData->FragmentTable[0].FragmentLength = TxData->FragmentTable[0].FragmentLength - RawHdrLen;\r
772db4bb 1692 } else {\r
216f7970 1693 CopyMem (&Head.Dst, &TxData->DestinationAddress, sizeof (IP4_ADDR));\r
1694 Head.Dst = NTOHL (Head.Dst);\r
772db4bb 1695\r
216f7970 1696 if (TxData->OverrideData != NULL) {\r
1697 Override = TxData->OverrideData;\r
1698 Head.Protocol = Override->Protocol;\r
1699 Head.Tos = Override->TypeOfService;\r
1700 Head.Ttl = Override->TimeToLive;\r
1701 DontFragment = Override->DoNotFragment;\r
1702\r
1703 CopyMem (&Head.Src, &Override->SourceAddress, sizeof (IP4_ADDR));\r
1704 CopyMem (&GateWay, &Override->GatewayAddress, sizeof (IP4_ADDR));\r
1705\r
1706 Head.Src = NTOHL (Head.Src);\r
1707 GateWay = NTOHL (GateWay);\r
1708 } else {\r
1709 Head.Src = IpIf->Ip;\r
1710 GateWay = IP4_ALLZERO_ADDRESS;\r
1711 Head.Protocol = Config->DefaultProtocol;\r
1712 Head.Tos = Config->TypeOfService;\r
1713 Head.Ttl = Config->TimeToLive;\r
1714 DontFragment = Config->DoNotFragment;\r
1715 }\r
1716\r
1717 Head.Fragment = IP4_HEAD_FRAGMENT_FIELD (DontFragment, FALSE, 0);\r
1718 HeadLen = (TxData->OptionsLength + 3) & (~0x03);\r
1719\r
1720 OptionsLength = TxData->OptionsLength;\r
1721 OptionsBuffer = (UINT8 *) (TxData->OptionsBuffer);\r
1722 }\r
772db4bb 1723\r
1724 //\r
1725 // If don't fragment and fragment needed, return error\r
1726 //\r
a1503a32 1727 if (DontFragment && (TxData->TotalDataLength + HeadLen > IpSb->MaxPacketSize)) {\r
772db4bb 1728 Status = EFI_BAD_BUFFER_SIZE;\r
1729 goto ON_EXIT;\r
1730 }\r
1731\r
1732 //\r
1733 // OK, it survives all the validation check. Wrap the token in\r
1734 // a IP4_TXTOKEN_WRAP and the data in a netbuf\r
1735 //\r
1736 Status = EFI_OUT_OF_RESOURCES;\r
f6b7393c 1737 Wrap = AllocateZeroPool (sizeof (IP4_TXTOKEN_WRAP));\r
772db4bb 1738 if (Wrap == NULL) {\r
1739 goto ON_EXIT;\r
1740 }\r
1741\r
1742 Wrap->IpInstance = IpInstance;\r
1743 Wrap->Token = Token;\r
1744 Wrap->Sent = FALSE;\r
1745 Wrap->Life = IP4_US_TO_SEC (Config->TransmitTimeout);\r
1746 Wrap->Packet = NetbufFromExt (\r
1747 (NET_FRAGMENT *) TxData->FragmentTable,\r
1748 TxData->FragmentCount,\r
1749 IP4_MAX_HEADLEN,\r
1750 0,\r
1751 Ip4FreeTxToken,\r
1752 Wrap\r
1753 );\r
1754\r
1755 if (Wrap->Packet == NULL) {\r
766c7483 1756 FreePool (Wrap);\r
772db4bb 1757 goto ON_EXIT;\r
1758 }\r
1759\r
1760 Token->Status = EFI_NOT_READY;\r
1761\r
1762 if (EFI_ERROR (NetMapInsertTail (&IpInstance->TxTokens, Token, Wrap))) {\r
1763 //\r
1764 // NetbufFree will call Ip4FreeTxToken, which in turn will\r
1765 // free the IP4_TXTOKEN_WRAP. Now, the token wrap hasn't been\r
1766 // enqueued.\r
1767 //\r
216f7970 1768 if (Config->RawData) {\r
1769 //\r
1770 // Restore pointer of first fragment in RawData mode.\r
1771 //\r
1772 TxData->FragmentTable[0].FragmentBuffer = (UINT8 *) FirstFragment;\r
1773 }\r
1774\r
772db4bb 1775 NetbufFree (Wrap->Packet);\r
1776 goto ON_EXIT;\r
1777 }\r
1778\r
36ee91ca 1779 //\r
1780 // Mark the packet sent before output it. Mark it not sent again if the\r
1781 // returned status is not EFI_SUCCESS;\r
1782 //\r
1783 Wrap->Sent = TRUE;\r
1784\r
772db4bb 1785 Status = Ip4Output (\r
1786 IpSb,\r
1787 IpInstance,\r
1788 Wrap->Packet,\r
1789 &Head,\r
216f7970 1790 OptionsBuffer,\r
1791 OptionsLength,\r
772db4bb 1792 GateWay,\r
1793 Ip4OnPacketSent,\r
1794 Wrap\r
1795 );\r
1796\r
1797 if (EFI_ERROR (Status)) {\r
36ee91ca 1798 Wrap->Sent = FALSE;\r
216f7970 1799\r
1800 if (Config->RawData) {\r
1801 //\r
1802 // Restore pointer of first fragment in RawData mode.\r
1803 //\r
1804 TxData->FragmentTable[0].FragmentBuffer = (UINT8 *) FirstFragment;\r
1805 }\r
1806\r
772db4bb 1807 NetbufFree (Wrap->Packet);\r
772db4bb 1808 }\r
1809\r
216f7970 1810 if (Config->RawData) {\r
1811 //\r
1812 // Restore pointer of first fragment in RawData mode.\r
1813 //\r
1814 TxData->FragmentTable[0].FragmentBuffer = (UINT8 *) FirstFragment;\r
1815 }\r
1816\r
772db4bb 1817ON_EXIT:\r
e48e37fc 1818 gBS->RestoreTPL (OldTpl);\r
772db4bb 1819 return Status;\r
1820}\r
1821\r
1822\r
1823/**\r
5405e9a6 1824 Places a receiving request into the receiving queue.\r
e2851998 1825\r
5405e9a6 1826 The Receive() function places a completion token into the receive packet queue.\r
1827 This function is always asynchronous.\r
e2851998 1828\r
5405e9a6 1829 The Token.Event field in the completion token must be filled in by the caller\r
1830 and cannot be NULL. When the receive operation completes, the EFI IPv4 Protocol\r
1831 driver updates the Token.Status and Token.Packet.RxData fields and the Token.Event\r
1832 is signaled.\r
1833\r
3e8c18da 1834 @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.\r
1835 @param[in] Token Pointer to a token that is associated with the receive data descriptor.\r
5405e9a6 1836\r
1837 @retval EFI_SUCCESS The receive completion token was cached.\r
1838 @retval EFI_NOT_STARTED This EFI IPv4 Protocol instance has not been started.\r
1839 @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP, RARP, etc.)\r
1840 is not finished yet.\r
1841 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:\r
1842 - This is NULL.\r
1843 - Token is NULL.\r
1844 - Token.Event is NULL.\r
1845 @retval EFI_OUT_OF_RESOURCES The receive completion token could not be queued due to a lack of system\r
1846 resources (usually memory).\r
1847 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.\r
1848 The EFI IPv4 Protocol instance has been reset to startup defaults.\r
1849 EFI_ACCESS_DENIED The receive completion token with the same Token.Event was already\r
1850 in the receive queue.\r
1851 @retval EFI_NOT_READY The receive request could not be queued because the receive queue is full.\r
1852 @retval EFI_ICMP_ERROR An ICMP error packet was received.\r
772db4bb 1853\r
1854**/\r
772db4bb 1855EFI_STATUS\r
1856EFIAPI\r
1857EfiIp4Receive (\r
1858 IN EFI_IP4_PROTOCOL *This,\r
1859 IN EFI_IP4_COMPLETION_TOKEN *Token\r
1860 )\r
1861{\r
1862 IP4_PROTOCOL *IpInstance;\r
772db4bb 1863 EFI_STATUS Status;\r
1864 EFI_TPL OldTpl;\r
1865\r
1866 //\r
1867 // First validate the parameters\r
1868 //\r
1869 if ((This == NULL) || (Token == NULL) || (Token->Event == NULL)) {\r
1870 return EFI_INVALID_PARAMETER;\r
1871 }\r
1872\r
1873 IpInstance = IP4_INSTANCE_FROM_PROTOCOL (This);\r
1874\r
e48e37fc 1875 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
772db4bb 1876\r
1877 if (IpInstance->State != IP4_STATE_CONFIGED) {\r
1878 Status = EFI_NOT_STARTED;\r
1879 goto ON_EXIT;\r
1880 }\r
1881\r
772db4bb 1882 //\r
1883 // Check whether the toke is already on the receive queue.\r
1884 //\r
1885 Status = NetMapIterate (&IpInstance->RxTokens, Ip4TokenExist, Token);\r
1886\r
1887 if (EFI_ERROR (Status)) {\r
1888 Status = EFI_ACCESS_DENIED;\r
1889 goto ON_EXIT;\r
1890 }\r
1891\r
1892 //\r
1893 // Queue the token then check whether there is pending received packet.\r
1894 //\r
1895 Status = NetMapInsertTail (&IpInstance->RxTokens, Token, NULL);\r
1896\r
1897 if (EFI_ERROR (Status)) {\r
1898 goto ON_EXIT;\r
1899 }\r
1900\r
1901 Status = Ip4InstanceDeliverPacket (IpInstance);\r
1902\r
36ee91ca 1903 //\r
1904 // Dispatch the DPC queued by the NotifyFunction of this instane's receive\r
1905 // event.\r
1906 //\r
d8d26fb2 1907 DispatchDpc ();\r
36ee91ca 1908\r
772db4bb 1909ON_EXIT:\r
e48e37fc 1910 gBS->RestoreTPL (OldTpl);\r
772db4bb 1911 return Status;\r
1912}\r
1913\r
1914\r
1915/**\r
1916 Cancel the transmitted but not recycled packet. If a matching\r
1917 token is found, it will call Ip4CancelPacket to cancel the\r
1918 packet. Ip4CancelPacket will cancel all the fragments of the\r
1919 packet. When all the fragments are freed, the IP4_TXTOKEN_WRAP\r
1920 will be deleted from the Map, and user's event signalled.\r
1921 Because Ip4CancelPacket and other functions are all called in\r
1922 line, so, after Ip4CancelPacket returns, the Item has been freed.\r
1923\r
1f6729ff 1924 @param[in] Map The IP4 child's transmit queue.\r
1925 @param[in] Item The current transmitted packet to test.\r
1926 @param[in] Context The user's token to cancel.\r
772db4bb 1927\r
1928 @retval EFI_SUCCESS Continue to check the next Item.\r
1929 @retval EFI_ABORTED The user's Token (Token != NULL) is cancelled.\r
1930\r
1931**/\r
772db4bb 1932EFI_STATUS\r
e798cd87 1933EFIAPI\r
772db4bb 1934Ip4CancelTxTokens (\r
1935 IN NET_MAP *Map,\r
1936 IN NET_MAP_ITEM *Item,\r
1937 IN VOID *Context\r
1938 )\r
1939{\r
1940 EFI_IP4_COMPLETION_TOKEN *Token;\r
1941 IP4_TXTOKEN_WRAP *Wrap;\r
1942\r
1943 Token = (EFI_IP4_COMPLETION_TOKEN *) Context;\r
1944\r
1945 //\r
1946 // Return EFI_SUCCESS to check the next item in the map if\r
1947 // this one doesn't match.\r
1948 //\r
1949 if ((Token != NULL) && (Token != Item->Key)) {\r
1950 return EFI_SUCCESS;\r
1951 }\r
1952\r
1953 Wrap = (IP4_TXTOKEN_WRAP *) Item->Value;\r
1954 ASSERT (Wrap != NULL);\r
1955\r
1956 //\r
1957 // Don't access the Item, Wrap and Token's members after this point.\r
1958 // Item and wrap has been freed. And we no longer own the Token.\r
1959 //\r
1960 Ip4CancelPacket (Wrap->IpInstance->Interface, Wrap->Packet, EFI_ABORTED);\r
1961\r
1962 //\r
1963 // If only one item is to be cancel, return EFI_ABORTED to stop\r
1964 // iterating the map any more.\r
1965 //\r
1966 if (Token != NULL) {\r
1967 return EFI_ABORTED;\r
1968 }\r
1969\r
1970 return EFI_SUCCESS;\r
1971}\r
1972\r
1973\r
1974/**\r
1975 Cancel the receive request. This is quiet simple, because\r
1976 it is only enqueued in our local receive map.\r
1977\r
1f6729ff 1978 @param[in] Map The IP4 child's receive queue.\r
1979 @param[in] Item Current receive request to cancel.\r
1980 @param[in] Context The user's token to cancel.\r
772db4bb 1981\r
1982 @retval EFI_SUCCESS Continue to check the next receive request on the\r
1983 queue.\r
1984 @retval EFI_ABORTED The user's token (token != NULL) has been\r
1985 cancelled.\r
1986\r
1987**/\r
772db4bb 1988EFI_STATUS\r
e798cd87 1989EFIAPI\r
772db4bb 1990Ip4CancelRxTokens (\r
1991 IN NET_MAP *Map,\r
1992 IN NET_MAP_ITEM *Item,\r
1993 IN VOID *Context\r
1994 )\r
1995{\r
1996 EFI_IP4_COMPLETION_TOKEN *Token;\r
1997 EFI_IP4_COMPLETION_TOKEN *This;\r
1998\r
1999 Token = (EFI_IP4_COMPLETION_TOKEN *) Context;\r
2000 This = Item->Key;\r
2001\r
2002 if ((Token != NULL) && (Token != This)) {\r
2003 return EFI_SUCCESS;\r
2004 }\r
2005\r
2006 NetMapRemoveItem (Map, Item, NULL);\r
2007\r
2008 This->Status = EFI_ABORTED;\r
2009 This->Packet.RxData = NULL;\r
2010 gBS->SignalEvent (This->Event);\r
2011\r
2012 if (Token != NULL) {\r
2013 return EFI_ABORTED;\r
2014 }\r
2015\r
2016 return EFI_SUCCESS;\r
2017}\r
2018\r
2019\r
2020/**\r
2021 Cancel the user's receive/transmit request.\r
2022\r
1f6729ff 2023 @param[in] IpInstance The IP4 child.\r
3e8c18da 2024 @param[in] Token The token to cancel. If NULL, all token will be\r
772db4bb 2025 cancelled.\r
2026\r
1f6729ff 2027 @retval EFI_SUCCESS The token is cancelled.\r
772db4bb 2028 @retval EFI_NOT_FOUND The token isn't found on either the\r
1f6729ff 2029 transmit/receive queue.\r
772db4bb 2030 @retval EFI_DEVICE_ERROR Not all token is cancelled when Token is NULL.\r
2031\r
2032**/\r
2033EFI_STATUS\r
2034Ip4Cancel (\r
2035 IN IP4_PROTOCOL *IpInstance,\r
2036 IN EFI_IP4_COMPLETION_TOKEN *Token OPTIONAL\r
2037 )\r
2038{\r
2039 EFI_STATUS Status;\r
2040\r
2041 //\r
2042 // First check the transmitted packet. Ip4CancelTxTokens returns\r
2043 // EFI_ABORTED to mean that the token has been cancelled when\r
2044 // token != NULL. So, return EFI_SUCCESS for this condition.\r
2045 //\r
2046 Status = NetMapIterate (&IpInstance->TxTokens, Ip4CancelTxTokens, Token);\r
2047\r
2048 if (EFI_ERROR (Status)) {\r
2049 if ((Token != NULL) && (Status == EFI_ABORTED)) {\r
2050 return EFI_SUCCESS;\r
2051 }\r
2052\r
2053 return Status;\r
2054 }\r
2055\r
2056 //\r
2057 // Check the receive queue. Ip4CancelRxTokens also returns EFI_ABORT\r
2058 // for Token!=NULL and it is cancelled.\r
2059 //\r
2060 Status = NetMapIterate (&IpInstance->RxTokens, Ip4CancelRxTokens, Token);\r
36ee91ca 2061 //\r
2062 // Dispatch the DPCs queued by the NotifyFunction of the canceled rx token's\r
2063 // events.\r
2064 //\r
d8d26fb2 2065 DispatchDpc ();\r
772db4bb 2066 if (EFI_ERROR (Status)) {\r
2067 if ((Token != NULL) && (Status == EFI_ABORTED)) {\r
2068 return EFI_SUCCESS;\r
2069 }\r
2070\r
2071 return Status;\r
2072 }\r
2073\r
2074 //\r
2075 // OK, if the Token is found when Token != NULL, the NetMapIterate\r
2076 // will return EFI_ABORTED, which has been interrupted as EFI_SUCCESS.\r
2077 //\r
2078 if (Token != NULL) {\r
2079 return EFI_NOT_FOUND;\r
2080 }\r
2081\r
2082 //\r
2083 // If Token == NULL, cancel all the tokens. return error if no\r
2084 // all of them are cancelled.\r
2085 //\r
2086 if (!NetMapIsEmpty (&IpInstance->TxTokens) ||\r
2087 !NetMapIsEmpty (&IpInstance->RxTokens)) {\r
2088\r
2089 return EFI_DEVICE_ERROR;\r
2090 }\r
2091\r
2092 return EFI_SUCCESS;\r
2093}\r
2094\r
2095\r
2096/**\r
5405e9a6 2097 Abort an asynchronous transmit or receive request.\r
e2851998 2098\r
5405e9a6 2099 The Cancel() function is used to abort a pending transmit or receive request.\r
2100 If the token is in the transmit or receive request queues, after calling this\r
2101 function, Token->Status will be set to EFI_ABORTED and then Token->Event will\r
2102 be signaled. If the token is not in one of the queues, which usually means the\r
2103 asynchronous operation has completed, this function will not signal the token\r
2104 and EFI_NOT_FOUND is returned.\r
2105\r
3e8c18da 2106 @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.\r
2107 @param[in] Token Pointer to a token that has been issued by\r
2108 EFI_IP4_PROTOCOL.Transmit() or\r
2109 EFI_IP4_PROTOCOL.Receive(). If NULL, all pending\r
2110 tokens are aborted. Type EFI_IP4_COMPLETION_TOKEN is\r
2111 defined in EFI_IP4_PROTOCOL.Transmit().\r
5405e9a6 2112\r
2113 @retval EFI_SUCCESS The asynchronous I/O request was aborted and\r
2114 Token.->Event was signaled. When Token is NULL, all\r
2115 pending requests were aborted and their events were signaled.\r
2116 @retval EFI_INVALID_PARAMETER This is NULL.\r
2117 @retval EFI_NOT_STARTED This instance has not been started.\r
2118 @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP,\r
2119 RARP, etc.) is not finished yet.\r
2120 @retval EFI_NOT_FOUND When Token is not NULL, the asynchronous I/O request was\r
2121 not found in the transmit or receive queue. It has either completed\r
2122 or was not issued by Transmit() and Receive().\r
772db4bb 2123\r
2124**/\r
772db4bb 2125EFI_STATUS\r
2126EFIAPI\r
2127EfiIp4Cancel (\r
2128 IN EFI_IP4_PROTOCOL *This,\r
2129 IN EFI_IP4_COMPLETION_TOKEN *Token OPTIONAL\r
2130 )\r
2131{\r
2132 IP4_PROTOCOL *IpInstance;\r
2133 EFI_STATUS Status;\r
2134 EFI_TPL OldTpl;\r
2135\r
2136 if (This == NULL) {\r
2137 return EFI_INVALID_PARAMETER;\r
2138 }\r
2139\r
2140 IpInstance = IP4_INSTANCE_FROM_PROTOCOL (This);\r
2141\r
e48e37fc 2142 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
772db4bb 2143\r
2144 if (IpInstance->State != IP4_STATE_CONFIGED) {\r
2145 Status = EFI_NOT_STARTED;\r
2146 goto ON_EXIT;\r
2147 }\r
2148\r
2149 if (IpInstance->ConfigData.UseDefaultAddress && IP4_NO_MAPPING (IpInstance)) {\r
2150 Status = EFI_NO_MAPPING;\r
2151 goto ON_EXIT;\r
2152 }\r
2153\r
2154 Status = Ip4Cancel (IpInstance, Token);\r
2155\r
2156ON_EXIT:\r
e48e37fc 2157 gBS->RestoreTPL (OldTpl);\r
772db4bb 2158 return Status;\r
2159}\r
2160\r
2161\r
2162/**\r
5405e9a6 2163 Polls for incoming data packets and processes outgoing data packets.\r
e2851998 2164\r
5405e9a6 2165 The Poll() function polls for incoming data packets and processes outgoing data\r
2166 packets. Network drivers and applications can call the EFI_IP4_PROTOCOL.Poll()\r
2167 function to increase the rate that data packets are moved between the communications\r
2168 device and the transmit and receive queues.\r
e2851998 2169\r
5405e9a6 2170 In some systems the periodic timer event may not poll the underlying communications\r
2171 device fast enough to transmit and/or receive all data packets without missing\r
2172 incoming packets or dropping outgoing packets. Drivers and applications that are\r
2173 experiencing packet loss should try calling the EFI_IP4_PROTOCOL.Poll() function\r
2174 more often.\r
2175\r
3e8c18da 2176 @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.\r
5405e9a6 2177\r
2178 @retval EFI_SUCCESS Incoming or outgoing data was processed.\r
2179 @retval EFI_NOT_STARTED This EFI IPv4 Protocol instance has not been started.\r
2180 @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP,\r
2181 RARP, etc.) is not finished yet.\r
2182 @retval EFI_INVALID_PARAMETER This is NULL.\r
2183 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.\r
2184 @retval EFI_NOT_READY No incoming or outgoing data is processed.\r
2185 @retval EFI_TIMEOUT Data was dropped out of the transmit and/or receive queue.\r
2186 Consider increasing the polling rate.\r
772db4bb 2187\r
2188**/\r
772db4bb 2189EFI_STATUS\r
2190EFIAPI\r
2191EfiIp4Poll (\r
2192 IN EFI_IP4_PROTOCOL *This\r
2193 )\r
2194{\r
2195 IP4_PROTOCOL *IpInstance;\r
2196 EFI_MANAGED_NETWORK_PROTOCOL *Mnp;\r
2197\r
2198 if (This == NULL) {\r
2199 return EFI_INVALID_PARAMETER;\r
2200 }\r
2201\r
2202 IpInstance = IP4_INSTANCE_FROM_PROTOCOL (This);\r
2203\r
2204 if (IpInstance->State == IP4_STATE_UNCONFIGED) {\r
2205 return EFI_NOT_STARTED;\r
2206 }\r
2207\r
2208 Mnp = IpInstance->Service->Mnp;\r
2209\r
2210 //\r
2211 // Don't lock the Poll function to enable the deliver of\r
2212 // the packet polled up.\r
2213 //\r
2214 return Mnp->Poll (Mnp);\r
2215}\r
2216\r
772db4bb 2217/**\r
2218 Decrease the life of the transmitted packets. If it is\r
2219 decreased to zero, cancel the packet. This function is\r
96e1079f 2220 called by Ip4PacketTimerTicking which time out both the\r
772db4bb 2221 received-but-not-delivered and transmitted-but-not-recycle\r
2222 packets.\r
2223\r
3e8c18da 2224 @param[in] Map The IP4 child's transmit map.\r
1f6729ff 2225 @param[in] Item Current transmitted packet.\r
3e8c18da 2226 @param[in] Context Not used.\r
772db4bb 2227\r
1f6729ff 2228 @retval EFI_SUCCESS Always returns EFI_SUCCESS.\r
772db4bb 2229\r
2230**/\r
2231EFI_STATUS\r
e798cd87 2232EFIAPI\r
772db4bb 2233Ip4SentPacketTicking (\r
2234 IN NET_MAP *Map,\r
2235 IN NET_MAP_ITEM *Item,\r
2236 IN VOID *Context\r
2237 )\r
2238{\r
2239 IP4_TXTOKEN_WRAP *Wrap;\r
2240\r
2241 Wrap = (IP4_TXTOKEN_WRAP *) Item->Value;\r
2242 ASSERT (Wrap != NULL);\r
2243\r
2244 if ((Wrap->Life > 0) && (--Wrap->Life == 0)) {\r
2245 Ip4CancelPacket (Wrap->IpInstance->Interface, Wrap->Packet, EFI_ABORTED);\r
2246 }\r
2247\r
2248 return EFI_SUCCESS;\r
2249}\r
2250\r
2251\r
2252/**\r
2c320007
JW
2253 There are two steps for this the heart beat timer of IP4 service instance. \r
2254 First, it times out all of its IP4 children's received-but-not-delivered \r
2255 and transmitted-but-not-recycle packets, and provides time input for its \r
2256 IGMP protocol.\r
2257 Second, a dedicated timer is used to poll underlying media status. In case \r
2258 of cable swap, a new round auto configuration will be initiated. The timer \r
2259 will signal the IP4 to run DHCP configuration again. IP4 driver will free\r
2260 old IP address related resource, such as route table and Interface, then\r
2261 initiate a DHCP process to acquire new IP, eventually create route table \r
2262 for new IP address.\r
772db4bb 2263\r
3e8c18da 2264 @param[in] Event The IP4 service instance's heart beat timer.\r
2265 @param[in] Context The IP4 service instance.\r
772db4bb 2266\r
2267**/\r
2268VOID\r
2269EFIAPI\r
2270Ip4TimerTicking (\r
2271 IN EFI_EVENT Event,\r
2272 IN VOID *Context\r
2273 )\r
2274{\r
2275 IP4_SERVICE *IpSb;\r
2c320007
JW
2276 BOOLEAN OldMediaPresent;\r
2277 EFI_STATUS Status;\r
2278 EFI_SIMPLE_NETWORK_MODE SnpModeData;\r
772db4bb 2279\r
2280 IpSb = (IP4_SERVICE *) Context;\r
2281 NET_CHECK_SIGNATURE (IpSb, IP4_SERVICE_SIGNATURE);\r
2c320007
JW
2282 \r
2283 OldMediaPresent = IpSb->MediaPresent;\r
772db4bb 2284\r
2285 Ip4PacketTimerTicking (IpSb);\r
2286 Ip4IgmpTicking (IpSb);\r
2c320007
JW
2287\r
2288 //\r
2289 // Get fresh mode data from MNP, since underlying media status may change. \r
2290 // Here, it needs to mention that the MediaPresent can also be checked even if \r
2291 // EFI_NOT_STARTED returned while this MNP child driver instance isn't configured.\r
2292 //\r
2293 Status = IpSb->Mnp->GetModeData (IpSb->Mnp, NULL, &SnpModeData);\r
2294 if (EFI_ERROR (Status) && (Status != EFI_NOT_STARTED)) {\r
2295 return;\r
2296 }\r
2297\r
2298 IpSb->MediaPresent = SnpModeData.MediaPresent;\r
2299 //\r
2300 // Media transimit Unpresent to Present means new link movement is detected.\r
2301 //\r
7648748e 2302 if (!OldMediaPresent && IpSb->MediaPresent && (IpSb->Ip4Config2Instance.Policy == Ip4Config2PolicyDhcp)) {\r
2c320007
JW
2303 //\r
2304 // Signal the IP4 to run the dhcp configuration again. IP4 driver will free\r
2305 // old IP address related resource, such as route table and Interface, then \r
2306 // initiate a DHCP round to acquire new IP, eventually \r
2307 // create route table for new IP address.\r
2308 //\r
2309 if (IpSb->ReconfigEvent != NULL) {\r
2310 Status = gBS->SignalEvent (IpSb->ReconfigEvent);\r
2311 DispatchDpc ();\r
2312 }\r
2313 }\r
772db4bb 2314}\r