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