forked from tianocore/edk2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIp4Config2Impl.c
2168 lines (1765 loc) · 65.4 KB
/
Ip4Config2Impl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** @file
The implementation of EFI IPv4 Configuration II Protocol.
Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.<BR>
(C) Copyright 2015-2016 Hewlett Packard Enterprise Development LP<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include "Ip4Impl.h"
LIST_ENTRY mIp4Config2InstanceList = {&mIp4Config2InstanceList, &mIp4Config2InstanceList};
/**
The event process routine when the DHCPv4 service binding protocol is installed
in the system.
@param[in] Event Not used.
@param[in] Context Pointer to the IP4 config2 instance data.
**/
VOID
EFIAPI
Ip4Config2OnDhcp4SbInstalled (
IN EFI_EVENT Event,
IN VOID *Context
);
/**
Destroy the Dhcp4 child in IP4_CONFIG2_INSTANCE and release the resources.
@param[in, out] Instance The buffer of IP4 config2 instance to be freed.
@retval EFI_SUCCESS The child was successfully destroyed.
@retval Others Failed to destroy the child.
**/
EFI_STATUS
Ip4Config2DestroyDhcp4 (
IN OUT IP4_CONFIG2_INSTANCE *Instance
)
{
IP4_SERVICE *IpSb;
EFI_STATUS Status;
EFI_DHCP4_PROTOCOL *Dhcp4;
Dhcp4 = Instance->Dhcp4;
ASSERT (Dhcp4 != NULL);
Dhcp4->Stop (Dhcp4);
Dhcp4->Configure (Dhcp4, NULL);
Instance->Dhcp4 = NULL;
IpSb = IP4_SERVICE_FROM_IP4_CONFIG2_INSTANCE (Instance);
//
// Close DHCPv4 protocol and destroy the child.
//
Status = gBS->CloseProtocol (
Instance->Dhcp4Handle,
&gEfiDhcp4ProtocolGuid,
IpSb->Image,
IpSb->Controller
);
if (EFI_ERROR (Status)) {
return Status;
}
Status = NetLibDestroyServiceChild (
IpSb->Controller,
IpSb->Image,
&gEfiDhcp4ServiceBindingProtocolGuid,
Instance->Dhcp4Handle
);
Instance->Dhcp4Handle = NULL;
return Status;
}
/**
Update the current policy to NewPolicy. During the transition
period, the default router list
and address list in all interfaces will be released.
@param[in] IpSb The IP4 service binding instance.
@param[in] NewPolicy The new policy to be updated to.
**/
VOID
Ip4Config2OnPolicyChanged (
IN IP4_SERVICE *IpSb,
IN EFI_IP4_CONFIG2_POLICY NewPolicy
)
{
IP4_INTERFACE *IpIf;
IP4_ROUTE_TABLE *RouteTable;
//
// Currently there are only two policies: static and dhcp. Regardless of
// what transition is going on, i.e., static -> dhcp and dhcp ->
// static, we have to free default router table and all addresses.
//
if (IpSb->DefaultInterface != NULL) {
if (IpSb->DefaultRouteTable != NULL) {
Ip4FreeRouteTable (IpSb->DefaultRouteTable);
IpSb->DefaultRouteTable = NULL;
}
Ip4CancelReceive (IpSb->DefaultInterface);
Ip4FreeInterface (IpSb->DefaultInterface, NULL);
IpSb->DefaultInterface = NULL;
}
Ip4CleanAssembleTable (&IpSb->Assemble);
//
// Create new default interface and route table.
//
IpIf = Ip4CreateInterface (IpSb->Mnp, IpSb->Controller, IpSb->Image);
if (IpIf == NULL) {
return ;
}
RouteTable = Ip4CreateRouteTable ();
if (RouteTable == NULL) {
Ip4FreeInterface (IpIf, NULL);
return ;
}
IpSb->DefaultInterface = IpIf;
InsertHeadList (&IpSb->Interfaces, &IpIf->Link);
IpSb->DefaultRouteTable = RouteTable;
Ip4ReceiveFrame (IpIf, NULL, Ip4AccpetFrame, IpSb);
if (IpSb->State == IP4_SERVICE_CONFIGED || IpSb->State == IP4_SERVICE_STARTED) {
IpSb->State = IP4_SERVICE_UNSTARTED;
}
//
// Start the dhcp configuration.
//
if (NewPolicy == Ip4Config2PolicyDhcp) {
Ip4StartAutoConfig (&IpSb->Ip4Config2Instance);
}
}
/**
Signal the registered event. It is the callback routine for NetMapIterate.
@param[in] Map Points to the list of registered event.
@param[in] Item The registered event.
@param[in] Arg Not used.
@retval EFI_SUCCESS The event was signaled successfully.
**/
EFI_STATUS
EFIAPI
Ip4Config2SignalEvent (
IN NET_MAP *Map,
IN NET_MAP_ITEM *Item,
IN VOID *Arg
)
{
gBS->SignalEvent ((EFI_EVENT) Item->Key);
return EFI_SUCCESS;
}
/**
Read the configuration data from variable storage according to the VarName and
gEfiIp4Config2ProtocolGuid. It checks the integrity of variable data. If the
data is corrupted, it clears the variable data to ZERO. Otherwise, it outputs the
configuration data to IP4_CONFIG2_INSTANCE.
@param[in] VarName The pointer to the variable name
@param[in, out] Instance The pointer to the IP4 config2 instance data.
@retval EFI_NOT_FOUND The variable can not be found or already corrupted.
@retval EFI_OUT_OF_RESOURCES Fail to allocate resource to complete the operation.
@retval EFI_SUCCESS The configuration data was retrieved successfully.
**/
EFI_STATUS
Ip4Config2ReadConfigData (
IN CHAR16 *VarName,
IN OUT IP4_CONFIG2_INSTANCE *Instance
)
{
EFI_STATUS Status;
UINTN VarSize;
IP4_CONFIG2_VARIABLE *Variable;
IP4_CONFIG2_DATA_ITEM *DataItem;
UINTN Index;
IP4_CONFIG2_DATA_RECORD DataRecord;
CHAR8 *Data;
//
// Try to read the configuration variable.
//
VarSize = 0;
Status = gRT->GetVariable (
VarName,
&gEfiIp4Config2ProtocolGuid,
NULL,
&VarSize,
NULL
);
if (Status == EFI_BUFFER_TOO_SMALL) {
//
// Allocate buffer and read the config variable.
//
Variable = AllocatePool (VarSize);
if (Variable == NULL) {
return EFI_OUT_OF_RESOURCES;
}
Status = gRT->GetVariable (
VarName,
&gEfiIp4Config2ProtocolGuid,
NULL,
&VarSize,
Variable
);
if (EFI_ERROR (Status) || (UINT16) (~NetblockChecksum ((UINT8 *) Variable, (UINT32) VarSize)) != 0) {
//
// GetVariable still error or the variable is corrupted.
// Fall back to the default value.
//
FreePool (Variable);
//
// Remove the problematic variable and return EFI_NOT_FOUND, a new
// variable will be set again.
//
gRT->SetVariable (
VarName,
&gEfiIp4Config2ProtocolGuid,
IP4_CONFIG2_VARIABLE_ATTRIBUTE,
0,
NULL
);
return EFI_NOT_FOUND;
}
for (Index = 0; Index < Variable->DataRecordCount; Index++) {
CopyMem (&DataRecord, &Variable->DataRecord[Index], sizeof (DataRecord));
DataItem = &Instance->DataItem[DataRecord.DataType];
if (DATA_ATTRIB_SET (DataItem->Attribute, DATA_ATTRIB_SIZE_FIXED) &&
(DataItem->DataSize != DataRecord.DataSize)
) {
//
// Perhaps a corrupted data record...
//
continue;
}
if (!DATA_ATTRIB_SET (DataItem->Attribute, DATA_ATTRIB_SIZE_FIXED)) {
//
// This data item has variable length data.
//
DataItem->Data.Ptr = AllocatePool (DataRecord.DataSize);
if (DataItem->Data.Ptr == NULL) {
//
// no memory resource
//
continue;
}
}
Data = (CHAR8 *) Variable + DataRecord.Offset;
CopyMem (DataItem->Data.Ptr, Data, DataRecord.DataSize);
DataItem->DataSize = DataRecord.DataSize;
DataItem->Status = EFI_SUCCESS;
}
FreePool (Variable);
return EFI_SUCCESS;
}
return Status;
}
/**
Write the configuration data from IP4_CONFIG2_INSTANCE to variable storage.
@param[in] VarName The pointer to the variable name.
@param[in] Instance The pointer to the IP4 config2 instance data.
@retval EFI_OUT_OF_RESOURCES Fail to allocate resource to complete the operation.
@retval EFI_SUCCESS The configuration data is written successfully.
**/
EFI_STATUS
Ip4Config2WriteConfigData (
IN CHAR16 *VarName,
IN IP4_CONFIG2_INSTANCE *Instance
)
{
UINTN Index;
UINTN VarSize;
IP4_CONFIG2_DATA_ITEM *DataItem;
IP4_CONFIG2_VARIABLE *Variable;
IP4_CONFIG2_DATA_RECORD *DataRecord;
CHAR8 *Heap;
EFI_STATUS Status;
VarSize = sizeof (IP4_CONFIG2_VARIABLE) - sizeof (IP4_CONFIG2_DATA_RECORD);
for (Index = 0; Index < Ip4Config2DataTypeMaximum; Index++) {
DataItem = &Instance->DataItem[Index];
if (!DATA_ATTRIB_SET (DataItem->Attribute, DATA_ATTRIB_VOLATILE) && !EFI_ERROR (DataItem->Status)) {
VarSize += sizeof (IP4_CONFIG2_DATA_RECORD) + DataItem->DataSize;
}
}
Variable = AllocatePool (VarSize);
if (Variable == NULL) {
return EFI_OUT_OF_RESOURCES;
}
Heap = (CHAR8 *) Variable + VarSize;
Variable->DataRecordCount = 0;
for (Index = 0; Index < Ip4Config2DataTypeMaximum; Index++) {
DataItem = &Instance->DataItem[Index];
if (!DATA_ATTRIB_SET (DataItem->Attribute, DATA_ATTRIB_VOLATILE) && !EFI_ERROR (DataItem->Status)) {
Heap -= DataItem->DataSize;
CopyMem (Heap, DataItem->Data.Ptr, DataItem->DataSize);
DataRecord = &Variable->DataRecord[Variable->DataRecordCount];
DataRecord->DataType = (EFI_IP4_CONFIG2_DATA_TYPE) Index;
DataRecord->DataSize = (UINT32) DataItem->DataSize;
DataRecord->Offset = (UINT16) (Heap - (CHAR8 *) Variable);
Variable->DataRecordCount++;
}
}
Variable->Checksum = 0;
Variable->Checksum = (UINT16) ~NetblockChecksum ((UINT8 *) Variable, (UINT32) VarSize);
Status = gRT->SetVariable (
VarName,
&gEfiIp4Config2ProtocolGuid,
IP4_CONFIG2_VARIABLE_ATTRIBUTE,
VarSize,
Variable
);
FreePool (Variable);
return Status;
}
/**
Build a EFI_IP4_ROUTE_TABLE to be returned to the caller of GetModeData.
The EFI_IP4_ROUTE_TABLE is clumsy to use in the internal operation of the
IP4 driver.
@param[in] IpSb The IP4 service binding instance.
@param[out] Table The built IP4 route table.
@retval EFI_SUCCESS The route table is successfully build
@retval EFI_NOT_FOUND Failed to allocate the memory for the route table.
**/
EFI_STATUS
Ip4Config2BuildDefaultRouteTable (
IN IP4_SERVICE *IpSb,
OUT EFI_IP4_ROUTE_TABLE *Table
)
{
LIST_ENTRY *Entry;
IP4_ROUTE_ENTRY *RtEntry;
UINT32 Count;
INT32 Index;
if (IpSb->DefaultRouteTable == NULL) {
return EFI_NOT_FOUND;
}
Count = IpSb->DefaultRouteTable->TotalNum;
if (Count == 0) {
return EFI_NOT_FOUND;
}
//
// Copy the route entry to EFI route table. Keep the order of
// route entry copied from most specific to default route. That
// is, interlevel the route entry from the instance's route area
// and those from the default route table's route area.
//
Count = 0;
for (Index = IP4_MASK_MAX; Index >= 0; Index--) {
NET_LIST_FOR_EACH (Entry, &(IpSb->DefaultRouteTable->RouteArea[Index])) {
RtEntry = NET_LIST_USER_STRUCT (Entry, IP4_ROUTE_ENTRY, Link);
EFI_IP4 (Table[Count].SubnetAddress) = HTONL (RtEntry->Dest & RtEntry->Netmask);
EFI_IP4 (Table[Count].SubnetMask) = HTONL (RtEntry->Netmask);
EFI_IP4 (Table[Count].GatewayAddress) = HTONL (RtEntry->NextHop);
Count++;
}
}
return EFI_SUCCESS;
}
/**
The event process routine when the DHCPv4 service binding protocol is installed
in the system.
@param[in] Event Not used.
@param[in] Context The pointer to the IP4 config2 instance data.
**/
VOID
EFIAPI
Ip4Config2OnDhcp4SbInstalled (
IN EFI_EVENT Event,
IN VOID *Context
)
{
IP4_CONFIG2_INSTANCE *Instance;
Instance = (IP4_CONFIG2_INSTANCE *) Context;
if ((Instance->Dhcp4Handle != NULL) || (Instance->Policy != Ip4Config2PolicyDhcp)) {
//
// The DHCP4 child is already created or the policy is no longer DHCP.
//
return ;
}
Ip4StartAutoConfig (Instance);
}
/**
Set the station address and subnetmask for the default interface.
@param[in] IpSb The pointer to the IP4 service binding instance.
@param[in] StationAddress Ip address to be set.
@param[in] SubnetMask Subnet to be set.
@retval EFI_SUCCESS Set default address successful.
@retval Others Some errors occur in setting.
**/
EFI_STATUS
Ip4Config2SetDefaultAddr (
IN IP4_SERVICE *IpSb,
IN IP4_ADDR StationAddress,
IN IP4_ADDR SubnetMask
)
{
EFI_STATUS Status;
IP4_INTERFACE *IpIf;
IP4_PROTOCOL *Ip4Instance;
EFI_ARP_PROTOCOL *Arp;
LIST_ENTRY *Entry;
IP4_ADDR Subnet;
IP4_ROUTE_TABLE *RouteTable;
IpIf = IpSb->DefaultInterface;
ASSERT (IpIf != NULL);
if ((IpIf->Ip == StationAddress) && (IpIf->SubnetMask == SubnetMask)) {
IpSb->State = IP4_SERVICE_CONFIGED;
return EFI_SUCCESS;
}
if (IpSb->Reconfig) {
//
// The default address is changed, free the previous interface first.
//
if (IpSb->DefaultRouteTable != NULL) {
Ip4FreeRouteTable (IpSb->DefaultRouteTable);
IpSb->DefaultRouteTable = NULL;
}
Ip4CancelReceive (IpSb->DefaultInterface);
Ip4FreeInterface (IpSb->DefaultInterface, NULL);
IpSb->DefaultInterface = NULL;
//
// Create new default interface and route table.
//
IpIf = Ip4CreateInterface (IpSb->Mnp, IpSb->Controller, IpSb->Image);
if (IpIf == NULL) {
return EFI_OUT_OF_RESOURCES;
}
RouteTable = Ip4CreateRouteTable ();
if (RouteTable == NULL) {
Ip4FreeInterface (IpIf, NULL);
return EFI_OUT_OF_RESOURCES;
}
IpSb->DefaultInterface = IpIf;
InsertHeadList (&IpSb->Interfaces, &IpIf->Link);
IpSb->DefaultRouteTable = RouteTable;
Ip4ReceiveFrame (IpIf, NULL, Ip4AccpetFrame, IpSb);
}
if (IpSb->State == IP4_SERVICE_CONFIGED) {
IpSb->State = IP4_SERVICE_UNSTARTED;
}
Status = Ip4SetAddress (IpIf, StationAddress, SubnetMask);
if (EFI_ERROR (Status)) {
return Status;
}
if (IpIf->Arp != NULL) {
//
// A non-NULL IpIf->Arp here means a new ARP child is created when setting default address,
// but some IP children may have referenced the default interface before it is configured,
// these IP instances also consume this ARP protocol so they need to open it BY_CHILD_CONTROLLER.
//
Arp = NULL;
NET_LIST_FOR_EACH (Entry, &IpIf->IpInstances) {
Ip4Instance = NET_LIST_USER_STRUCT_S (Entry, IP4_PROTOCOL, AddrLink, IP4_PROTOCOL_SIGNATURE);
Status = gBS->OpenProtocol (
IpIf->ArpHandle,
&gEfiArpProtocolGuid,
(VOID **) &Arp,
gIp4DriverBinding.DriverBindingHandle,
Ip4Instance->Handle,
EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
);
if (EFI_ERROR (Status)) {
return Status;
}
}
}
//
// Add a route for the connected network.
//
Subnet = StationAddress & SubnetMask;
Ip4AddRoute (
IpSb->DefaultRouteTable,
Subnet,
SubnetMask,
IP4_ALLZERO_ADDRESS
);
IpSb->State = IP4_SERVICE_CONFIGED;
IpSb->Reconfig = FALSE;
return EFI_SUCCESS;
}
/**
Set the station address, subnetmask and gateway address for the default interface.
@param[in] Instance The pointer to the IP4 config2 instance data.
@param[in] StationAddress Ip address to be set.
@param[in] SubnetMask Subnet to be set.
@param[in] GatewayAddress Gateway to be set.
@retval EFI_SUCCESS Set default If successful.
@retval Others Errors occur as indicated.
**/
EFI_STATUS
Ip4Config2SetDefaultIf (
IN IP4_CONFIG2_INSTANCE *Instance,
IN IP4_ADDR StationAddress,
IN IP4_ADDR SubnetMask,
IN IP4_ADDR GatewayAddress
)
{
EFI_STATUS Status;
IP4_SERVICE *IpSb;
IpSb = IP4_SERVICE_FROM_IP4_CONFIG2_INSTANCE (Instance);
//
// Check whether the StationAddress/SubnetMask pair is valid.
//
if (!Ip4StationAddressValid (StationAddress, SubnetMask)) {
return EFI_INVALID_PARAMETER;
}
Status = Ip4Config2SetDefaultAddr (IpSb, StationAddress, SubnetMask);
if (EFI_ERROR (Status)) {
return Status;
}
//
// Create a route if there is a default router.
//
if (GatewayAddress != IP4_ALLZERO_ADDRESS) {
Ip4AddRoute (
IpSb->DefaultRouteTable,
IP4_ALLZERO_ADDRESS,
IP4_ALLZERO_ADDRESS,
GatewayAddress
);
}
return EFI_SUCCESS;
}
/**
Release all the DHCP related resources.
@param Instance The IP4 config2 instance.
@return None
**/
VOID
Ip4Config2CleanDhcp4 (
IN IP4_CONFIG2_INSTANCE *Instance
)
{
IP4_SERVICE *IpSb;
IpSb = IP4_SERVICE_FROM_IP4_CONFIG2_INSTANCE (Instance);
if (Instance->Dhcp4 != NULL) {
Instance->Dhcp4->Stop (Instance->Dhcp4);
gBS->CloseProtocol (
Instance->Dhcp4Handle,
&gEfiDhcp4ProtocolGuid,
IpSb->Image,
IpSb->Controller
);
Instance->Dhcp4 = NULL;
}
if (Instance->Dhcp4Handle != NULL) {
NetLibDestroyServiceChild (
IpSb->Controller,
IpSb->Image,
&gEfiDhcp4ServiceBindingProtocolGuid,
Instance->Dhcp4Handle
);
Instance->Dhcp4Handle = NULL;
}
if (Instance->Dhcp4Event != NULL) {
gBS->CloseEvent (Instance->Dhcp4Event);
Instance->Dhcp4Event = NULL;
}
}
/**
This worker function sets the DNS server list for the EFI IPv4 network
stack running on the communication device that this EFI_IP4_CONFIG2_PROTOCOL
manages. The DNS server addresses must be unicast IPv4 addresses.
@param[in] Instance The pointer to the IP4 config2 instance data.
@param[in] DataSize The size of the buffer pointed to by Data in bytes.
@param[in] Data The data buffer to set, points to an array of
EFI_IPv4_ADDRESS instances.
@retval EFI_BAD_BUFFER_SIZE The DataSize does not match the size of the type.
@retval EFI_INVALID_PARAMETER One or more fields in Data is invalid.
@retval EFI_OUT_OF_RESOURCES Failed to allocate resources to complete the operation.
@retval EFI_ABORTED The DNS server addresses to be set equal the current
configuration.
@retval EFI_SUCCESS The specified configuration data for the EFI IPv4
network stack was set.
**/
EFI_STATUS
Ip4Config2SetDnsServerWorker (
IN IP4_CONFIG2_INSTANCE *Instance,
IN UINTN DataSize,
IN VOID *Data
)
{
UINTN OldIndex;
UINTN NewIndex;
EFI_IPv4_ADDRESS *OldDns;
EFI_IPv4_ADDRESS *NewDns;
UINTN OldDnsCount;
UINTN NewDnsCount;
IP4_CONFIG2_DATA_ITEM *Item;
BOOLEAN OneAdded;
VOID *Tmp;
IP4_ADDR DnsAddress;
if ((DataSize % sizeof (EFI_IPv4_ADDRESS) != 0) || (DataSize == 0)) {
return EFI_BAD_BUFFER_SIZE;
}
Item = &Instance->DataItem[Ip4Config2DataTypeDnsServer];
NewDns = (EFI_IPv4_ADDRESS *) Data;
OldDns = Item->Data.DnsServers;
NewDnsCount = DataSize / sizeof (EFI_IPv4_ADDRESS);
OldDnsCount = Item->DataSize / sizeof (EFI_IPv4_ADDRESS);
OneAdded = FALSE;
if (NewDnsCount != OldDnsCount) {
Tmp = AllocatePool (DataSize);
if (Tmp == NULL) {
return EFI_OUT_OF_RESOURCES;
}
} else {
Tmp = NULL;
}
for (NewIndex = 0; NewIndex < NewDnsCount; NewIndex++) {
CopyMem (&DnsAddress, NewDns + NewIndex, sizeof (IP4_ADDR));
if (IP4_IS_UNSPECIFIED (NTOHL (DnsAddress)) || IP4_IS_LOCAL_BROADCAST (NTOHL (DnsAddress))) {
//
// The dns server address must be unicast.
//
if (Tmp != NULL) {
FreePool (Tmp);
}
return EFI_INVALID_PARAMETER;
}
if (OneAdded) {
//
// If any address in the new setting is not in the old settings, skip the
// comparision below.
//
continue;
}
for (OldIndex = 0; OldIndex < OldDnsCount; OldIndex++) {
if (EFI_IP4_EQUAL (NewDns + NewIndex, OldDns + OldIndex)) {
//
// If found break out.
//
break;
}
}
if (OldIndex == OldDnsCount) {
OneAdded = TRUE;
}
}
if (!OneAdded && (DataSize == Item->DataSize)) {
//
// No new item is added and the size is the same.
//
Item->Status = EFI_SUCCESS;
return EFI_ABORTED;
} else {
if (Tmp != NULL) {
if (Item->Data.Ptr != NULL) {
FreePool (Item->Data.Ptr);
}
Item->Data.Ptr = Tmp;
}
CopyMem (Item->Data.Ptr, Data, DataSize);
Item->DataSize = DataSize;
Item->Status = EFI_SUCCESS;
return EFI_SUCCESS;
}
}
/**
Callback function when DHCP process finished. It will save the
retrieved IP configure parameter from DHCP to the NVRam.
@param Event The callback event
@param Context Opaque context to the callback
@return None
**/
VOID
EFIAPI
Ip4Config2OnDhcp4Complete (
IN EFI_EVENT Event,
IN VOID *Context
)
{
IP4_CONFIG2_INSTANCE *Instance;
EFI_DHCP4_MODE_DATA Dhcp4Mode;
EFI_STATUS Status;
IP4_ADDR StationAddress;
IP4_ADDR SubnetMask;
IP4_ADDR GatewayAddress;
UINT32 Index;
UINT32 OptionCount;
EFI_DHCP4_PACKET_OPTION **OptionList;
Instance = (IP4_CONFIG2_INSTANCE *) Context;
ASSERT (Instance->Dhcp4 != NULL);
//
// Get the DHCP retrieved parameters
//
Status = Instance->Dhcp4->GetModeData (Instance->Dhcp4, &Dhcp4Mode);
if (EFI_ERROR (Status)) {
goto Exit;
}
if (Dhcp4Mode.State == Dhcp4Bound) {
StationAddress = EFI_NTOHL (Dhcp4Mode.ClientAddress);
SubnetMask = EFI_NTOHL (Dhcp4Mode.SubnetMask);
GatewayAddress = EFI_NTOHL (Dhcp4Mode.RouterAddress);
Status = Ip4Config2SetDefaultIf (Instance, StationAddress, SubnetMask, GatewayAddress);
if (EFI_ERROR (Status)) {
goto Exit;
}
//
// Parse the ACK to get required DNS server information.
//
OptionCount = 0;
OptionList = NULL;
Status = Instance->Dhcp4->Parse (Instance->Dhcp4, Dhcp4Mode.ReplyPacket, &OptionCount, OptionList);
if (Status != EFI_BUFFER_TOO_SMALL) {
goto Exit;
}
OptionList = AllocateZeroPool (OptionCount * sizeof (EFI_DHCP4_PACKET_OPTION *));
if (OptionList == NULL) {
goto Exit;
}
Status = Instance->Dhcp4->Parse (Instance->Dhcp4, Dhcp4Mode.ReplyPacket, &OptionCount, OptionList);
if (EFI_ERROR (Status)) {
FreePool (OptionList);
goto Exit;
}
for (Index = 0; Index < OptionCount; Index++) {
//
// Look for DNS Server opcode (6).
//
if (OptionList[Index]->OpCode == DHCP4_TAG_DNS_SERVER) {
if (((OptionList[Index]->Length & 0x3) != 0) || (OptionList[Index]->Length == 0)) {
break;
}
Ip4Config2SetDnsServerWorker (Instance, OptionList[Index]->Length, &OptionList[Index]->Data[0]);
break;
}
}
FreePool (OptionList);
Instance->DhcpSuccess = TRUE;
}
Exit:
Ip4Config2CleanDhcp4 (Instance);
DispatchDpc ();
}
/**
Start the DHCP configuration for this IP service instance.
It will locates the EFI_IP4_CONFIG2_PROTOCOL, then start the
DHCP configuration.
@param[in] Instance The IP4 config2 instance to configure
@retval EFI_SUCCESS The auto configuration is successfully started
@retval Others Failed to start auto configuration.
**/
EFI_STATUS
Ip4StartAutoConfig (
IN IP4_CONFIG2_INSTANCE *Instance
)
{
IP4_SERVICE *IpSb;
EFI_DHCP4_PROTOCOL *Dhcp4;
EFI_DHCP4_MODE_DATA Dhcp4Mode;
EFI_DHCP4_PACKET_OPTION *OptionList[1];
IP4_CONFIG2_DHCP4_OPTION ParaList;
EFI_STATUS Status;
IpSb = IP4_SERVICE_FROM_IP4_CONFIG2_INSTANCE (Instance);
if (IpSb->State > IP4_SERVICE_UNSTARTED) {
return EFI_SUCCESS;
}
//
// A host must not invoke DHCP configuration if it is already
// participating in the DHCP configuration process.
//
if (Instance->Dhcp4Handle != NULL) {
return EFI_SUCCESS;
}
Status = NetLibCreateServiceChild (
IpSb->Controller,
IpSb->Image,
&gEfiDhcp4ServiceBindingProtocolGuid,
&Instance->Dhcp4Handle
);
if (Status == EFI_UNSUPPORTED) {
//
// No DHCPv4 Service Binding protocol, register a notify.
//
if (Instance->Dhcp4SbNotifyEvent == NULL) {
Instance->Dhcp4SbNotifyEvent = EfiCreateProtocolNotifyEvent (
&gEfiDhcp4ServiceBindingProtocolGuid,
TPL_CALLBACK,
Ip4Config2OnDhcp4SbInstalled,
(VOID *) Instance,
&Instance->Registration
);
}
}
if (EFI_ERROR (Status)) {
return Status;
}
if (Instance->Dhcp4SbNotifyEvent != NULL) {
gBS->CloseEvent (Instance->Dhcp4SbNotifyEvent);
}
Status = gBS->OpenProtocol (
Instance->Dhcp4Handle,
&gEfiDhcp4ProtocolGuid,
(VOID **) &Instance->Dhcp4,
IpSb->Image,
IpSb->Controller,
EFI_OPEN_PROTOCOL_BY_DRIVER
);
if (EFI_ERROR (Status)) {
NetLibDestroyServiceChild (
IpSb->Controller,
IpSb->Image,
&gEfiDhcp4ServiceBindingProtocolGuid,
Instance->Dhcp4Handle
);
Instance->Dhcp4Handle = NULL;
return Status;
}
//
// Check the current DHCP status, if the DHCP process has
// already finished, return now.
//
Dhcp4 = Instance->Dhcp4;
Status = Dhcp4->GetModeData (Dhcp4, &Dhcp4Mode);
if (Dhcp4Mode.State == Dhcp4Bound) {
Ip4Config2OnDhcp4Complete (NULL, Instance);
return EFI_SUCCESS;
}
//
// Try to start the DHCP process. Use most of the current
// DHCP configuration to avoid problems if some DHCP client
// yields the control of this DHCP service to us.
//
ParaList.Head.OpCode = DHCP4_TAG_PARA_LIST;
ParaList.Head.Length = 3;
ParaList.Head.Data[0] = DHCP4_TAG_NETMASK;
ParaList.Route = DHCP4_TAG_ROUTER;
ParaList.Dns = DHCP4_TAG_DNS_SERVER;
OptionList[0] = &ParaList.Head;
Dhcp4Mode.ConfigData.OptionCount = 1;
Dhcp4Mode.ConfigData.OptionList = OptionList;
Status = Dhcp4->Configure (Dhcp4, &Dhcp4Mode.ConfigData);
if (EFI_ERROR (Status)) {
gBS->CloseProtocol (