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