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