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