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