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